1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
// SPDX-License-Identifier: GPL-2.0
/*
* MAX77705 voltage and current hwmon driver.
*
* Copyright (C) 2025 Dzmitry Sankouski <dsankouski@gmail.com>
*/
#include <linux/err.h>
#include <linux/hwmon-sysfs.h>
#include <linux/hwmon.h>
#include <linux/kernel.h>
#include <linux/mfd/max77705-private.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
struct channel_desc {
u8 reg;
u8 avg_reg;
const char *const label;
// register resolution. nano Volts for voltage, nano Amperes for current
u32 resolution;
};
static const struct channel_desc current_channel_desc[] = {
{
.reg = IIN_REG,
.label = "IIN_REG",
.resolution = 125000
},
{
.reg = ISYS_REG,
.avg_reg = AVGISYS_REG,
.label = "ISYS_REG",
.resolution = 312500
}
};
static const struct channel_desc voltage_channel_desc[] = {
{
.reg = VBYP_REG,
.label = "VBYP_REG",
.resolution = 427246
},
{
.reg = VSYS_REG,
.label = "VSYS_REG",
.resolution = 156250
}
};
static int max77705_read_and_convert(struct regmap *regmap, u8 reg, u32 res,
bool is_signed, long *val)
{
int ret;
u32 regval;
ret = regmap_read(regmap, reg, ®val);
if (ret < 0)
return ret;
if (is_signed)
*val = mult_frac((long)sign_extend32(regval, 15), res, 1000000);
else
*val = mult_frac((long)regval, res, 1000000);
return 0;
}
static umode_t max77705_is_visible(const void *data,
enum hwmon_sensor_types type,
u32 attr, int channel)
{
switch (type) {
case hwmon_in:
switch (attr) {
case hwmon_in_input:
case hwmon_in_label:
return 0444;
default:
break;
}
break;
case hwmon_curr:
switch (attr) {
case hwmon_curr_input:
case hwmon_in_label:
return 0444;
case hwmon_curr_average:
if (current_channel_desc[channel].avg_reg)
return 0444;
break;
default:
break;
}
break;
default:
break;
}
return 0;
}
static int max77705_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
int channel, const char **buf)
{
switch (type) {
case hwmon_curr:
switch (attr) {
case hwmon_in_label:
*buf = current_channel_desc[channel].label;
return 0;
default:
return -EOPNOTSUPP;
}
case hwmon_in:
switch (attr) {
case hwmon_in_label:
*buf = voltage_channel_desc[channel].label;
return 0;
default:
return -EOPNOTSUPP;
}
default:
return -EOPNOTSUPP;
}
}
static int max77705_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{
struct regmap *regmap = dev_get_drvdata(dev);
u8 reg;
u32 res;
switch (type) {
case hwmon_curr:
switch (attr) {
case hwmon_curr_input:
reg = current_channel_desc[channel].reg;
res = current_channel_desc[channel].resolution;
return max77705_read_and_convert(regmap, reg, res, true, val);
case hwmon_curr_average:
reg = current_channel_desc[channel].avg_reg;
res = current_channel_desc[channel].resolution;
return max77705_read_and_convert(regmap, reg, res, true, val);
default:
return -EOPNOTSUPP;
}
case hwmon_in:
switch (attr) {
case hwmon_in_input:
reg = voltage_channel_desc[channel].reg;
res = voltage_channel_desc[channel].resolution;
return max77705_read_and_convert(regmap, reg, res, false, val);
default:
return -EOPNOTSUPP;
}
default:
return -EOPNOTSUPP;
}
return 0;
}
static const struct hwmon_ops max77705_hwmon_ops = {
.is_visible = max77705_is_visible,
.read = max77705_read,
.read_string = max77705_read_string,
};
static const struct hwmon_channel_info *max77705_info[] = {
HWMON_CHANNEL_INFO(in,
HWMON_I_INPUT | HWMON_I_LABEL,
HWMON_I_INPUT | HWMON_I_LABEL
),
HWMON_CHANNEL_INFO(curr,
HWMON_C_INPUT | HWMON_C_LABEL,
HWMON_C_INPUT | HWMON_C_AVERAGE | HWMON_C_LABEL
),
NULL
};
static const struct hwmon_chip_info max77705_chip_info = {
.ops = &max77705_hwmon_ops,
.info = max77705_info,
};
static int max77705_hwmon_probe(struct platform_device *pdev)
{
struct device *hwmon_dev;
struct regmap *regmap;
regmap = dev_get_regmap(pdev->dev.parent, NULL);
if (!regmap)
return -ENODEV;
hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, "max77705", regmap,
&max77705_chip_info, NULL);
if (IS_ERR(hwmon_dev))
return dev_err_probe(&pdev->dev, PTR_ERR(hwmon_dev),
"Unable to register hwmon device\n");
return 0;
};
static struct platform_driver max77705_hwmon_driver = {
.driver = {
.name = "max77705-hwmon",
},
.probe = max77705_hwmon_probe,
};
module_platform_driver(max77705_hwmon_driver);
MODULE_AUTHOR("Dzmitry Sankouski <dsankouski@gmail.com>");
MODULE_DESCRIPTION("MAX77705 monitor driver");
MODULE_LICENSE("GPL");
|