ltc2945.c (178b01eccfb0b8149682f61388400bd3d903dddc) ltc2945.c (b11f3d47c0e74e6c0515e31788651713a3a94a50)
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Driver for Linear Technology LTC2945 I2C Power Monitor
4 *
5 * Copyright (c) 2014 Guenter Roeck
6 */
7
8#include <linux/kernel.h>

--- 50 unchanged lines hidden (view full) ---

59#define CONTROL_TEST_MODE (1 << 4)
60
61static const struct of_device_id __maybe_unused ltc2945_of_match[] = {
62 { .compatible = "adi,ltc2945" },
63 { }
64};
65MODULE_DEVICE_TABLE(of, ltc2945_of_match);
66
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Driver for Linear Technology LTC2945 I2C Power Monitor
4 *
5 * Copyright (c) 2014 Guenter Roeck
6 */
7
8#include <linux/kernel.h>

--- 50 unchanged lines hidden (view full) ---

59#define CONTROL_TEST_MODE (1 << 4)
60
61static const struct of_device_id __maybe_unused ltc2945_of_match[] = {
62 { .compatible = "adi,ltc2945" },
63 { }
64};
65MODULE_DEVICE_TABLE(of, ltc2945_of_match);
66
67/**
68 * struct ltc2945_data - LTC2945 device data
69 * @regmap: regmap device
70 * @shunt_resistor: shunt resistor value in micro ohms (1000 by default)
71 */
72struct ltc2945_data {
73 struct regmap *regmap;
74 u32 shunt_resistor;
75};
76
67static inline bool is_power_reg(u8 reg)
68{
69 return reg < LTC2945_SENSE_H;
70}
71
72/* Return the value from the given register in uW, mV, or mA */
73static long long ltc2945_reg_to_val(struct device *dev, u8 reg)
74{
77static inline bool is_power_reg(u8 reg)
78{
79 return reg < LTC2945_SENSE_H;
80}
81
82/* Return the value from the given register in uW, mV, or mA */
83static long long ltc2945_reg_to_val(struct device *dev, u8 reg)
84{
75 struct regmap *regmap = dev_get_drvdata(dev);
85 struct ltc2945_data *data = dev_get_drvdata(dev);
86 struct regmap *regmap = data->regmap;
87 u32 shunt_resistor = data->shunt_resistor;
76 unsigned int control;
77 u8 buf[3];
78 long long val;
79 int ret;
80
81 ret = regmap_bulk_read(regmap, reg, buf,
82 is_power_reg(reg) ? 3 : 2);
83 if (ret < 0)
84 return ret;
85
86 if (is_power_reg(reg)) {
88 unsigned int control;
89 u8 buf[3];
90 long long val;
91 int ret;
92
93 ret = regmap_bulk_read(regmap, reg, buf,
94 is_power_reg(reg) ? 3 : 2);
95 if (ret < 0)
96 return ret;
97
98 if (is_power_reg(reg)) {
87 /* power */
99 /* 24-bit power */
88 val = (buf[0] << 16) + (buf[1] << 8) + buf[2];
89 } else {
100 val = (buf[0] << 16) + (buf[1] << 8) + buf[2];
101 } else {
90 /* current, voltage */
102 /* 12-bit current, voltage */
91 val = (buf[0] << 4) + (buf[1] >> 4);
92 }
93
94 switch (reg) {
95 case LTC2945_POWER_H:
96 case LTC2945_MAX_POWER_H:
97 case LTC2945_MIN_POWER_H:
98 case LTC2945_MAX_POWER_THRES_H:
99 case LTC2945_MIN_POWER_THRES_H:
100 /*
103 val = (buf[0] << 4) + (buf[1] >> 4);
104 }
105
106 switch (reg) {
107 case LTC2945_POWER_H:
108 case LTC2945_MAX_POWER_H:
109 case LTC2945_MIN_POWER_H:
110 case LTC2945_MAX_POWER_THRES_H:
111 case LTC2945_MIN_POWER_THRES_H:
112 /*
101 * Convert to uW by assuming current is measured with
102 * an 1mOhm sense resistor, similar to current
103 * measurements.
113 * Convert to uW
104 * Control register bit 0 selects if voltage at SENSE+/VDD
105 * or voltage at ADIN is used to measure power.
106 */
107 ret = regmap_read(regmap, LTC2945_CONTROL, &control);
108 if (ret < 0)
109 return ret;
110 if (control & CONTROL_MULT_SELECT) {
111 /* 25 mV * 25 uV = 0.625 uV resolution. */
112 val *= 625LL;
113 } else {
114 /* 0.5 mV * 25 uV = 0.0125 uV resolution. */
115 val = (val * 25LL) >> 1;
116 }
114 * Control register bit 0 selects if voltage at SENSE+/VDD
115 * or voltage at ADIN is used to measure power.
116 */
117 ret = regmap_read(regmap, LTC2945_CONTROL, &control);
118 if (ret < 0)
119 return ret;
120 if (control & CONTROL_MULT_SELECT) {
121 /* 25 mV * 25 uV = 0.625 uV resolution. */
122 val *= 625LL;
123 } else {
124 /* 0.5 mV * 25 uV = 0.0125 uV resolution. */
125 val = (val * 25LL) >> 1;
126 }
127 val *= 1000;
128 /* Overflow check: Assuming max 24-bit power, val is at most 53 bits right now. */
129 val = DIV_ROUND_CLOSEST_ULL(val, shunt_resistor);
130 /*
131 * Overflow check: After division, depending on shunt resistor,
132 * val can still be > 32 bits so returning long long makes sense
133 */
134
117 break;
118 case LTC2945_VIN_H:
119 case LTC2945_MAX_VIN_H:
120 case LTC2945_MIN_VIN_H:
121 case LTC2945_MAX_VIN_THRES_H:
122 case LTC2945_MIN_VIN_THRES_H:
123 /* 25 mV resolution. Convert to mV. */
124 val *= 25;

--- 6 unchanged lines hidden (view full) ---

131 /* 0.5mV resolution. Convert to mV. */
132 val = val >> 1;
133 break;
134 case LTC2945_SENSE_H:
135 case LTC2945_MAX_SENSE_H:
136 case LTC2945_MIN_SENSE_H:
137 case LTC2945_MAX_SENSE_THRES_H:
138 case LTC2945_MIN_SENSE_THRES_H:
135 break;
136 case LTC2945_VIN_H:
137 case LTC2945_MAX_VIN_H:
138 case LTC2945_MIN_VIN_H:
139 case LTC2945_MAX_VIN_THRES_H:
140 case LTC2945_MIN_VIN_THRES_H:
141 /* 25 mV resolution. Convert to mV. */
142 val *= 25;

--- 6 unchanged lines hidden (view full) ---

149 /* 0.5mV resolution. Convert to mV. */
150 val = val >> 1;
151 break;
152 case LTC2945_SENSE_H:
153 case LTC2945_MAX_SENSE_H:
154 case LTC2945_MIN_SENSE_H:
155 case LTC2945_MAX_SENSE_THRES_H:
156 case LTC2945_MIN_SENSE_THRES_H:
139 /*
140 * 25 uV resolution. Convert to current as measured with
141 * an 1 mOhm sense resistor, in mA. If a different sense
142 * resistor is installed, calculate the actual current by
143 * dividing the reported current by the sense resistor value
144 * in mOhm.
145 */
146 val *= 25;
157 /* 25 uV resolution. Convert to mA. */
158 val *= 25 * 1000;
159 /* Overflow check: Assuming max 12-bit sense, val is at most 27 bits right now */
160 val = DIV_ROUND_CLOSEST_ULL(val, shunt_resistor);
161 /* Overflow check: After division, <= 27 bits */
147 break;
148 default:
149 return -EINVAL;
150 }
151 return val;
152}
153
162 break;
163 default:
164 return -EINVAL;
165 }
166 return val;
167}
168
154static int ltc2945_val_to_reg(struct device *dev, u8 reg,
155 unsigned long val)
169static long long ltc2945_val_to_reg(struct device *dev, u8 reg,
170 unsigned long long val)
156{
171{
157 struct regmap *regmap = dev_get_drvdata(dev);
172 struct ltc2945_data *data = dev_get_drvdata(dev);
173 struct regmap *regmap = data->regmap;
174 u32 shunt_resistor = data->shunt_resistor;
158 unsigned int control;
159 int ret;
160
175 unsigned int control;
176 int ret;
177
178 /* Ensure we don't overflow */
179 val = clamp_val(val, 0, U32_MAX);
180
161 switch (reg) {
162 case LTC2945_POWER_H:
163 case LTC2945_MAX_POWER_H:
164 case LTC2945_MIN_POWER_H:
165 case LTC2945_MAX_POWER_THRES_H:
166 case LTC2945_MIN_POWER_THRES_H:
167 /*
181 switch (reg) {
182 case LTC2945_POWER_H:
183 case LTC2945_MAX_POWER_H:
184 case LTC2945_MIN_POWER_H:
185 case LTC2945_MAX_POWER_THRES_H:
186 case LTC2945_MIN_POWER_THRES_H:
187 /*
168 * Convert to register value by assuming current is measured
169 * with an 1mOhm sense resistor, similar to current
170 * measurements.
171 * Control register bit 0 selects if voltage at SENSE+/VDD
172 * or voltage at ADIN is used to measure power, which in turn
173 * determines register calculations.
174 */
175 ret = regmap_read(regmap, LTC2945_CONTROL, &control);
176 if (ret < 0)
177 return ret;
178 if (control & CONTROL_MULT_SELECT) {
179 /* 25 mV * 25 uV = 0.625 uV resolution. */
188 * Control register bit 0 selects if voltage at SENSE+/VDD
189 * or voltage at ADIN is used to measure power, which in turn
190 * determines register calculations.
191 */
192 ret = regmap_read(regmap, LTC2945_CONTROL, &control);
193 if (ret < 0)
194 return ret;
195 if (control & CONTROL_MULT_SELECT) {
196 /* 25 mV * 25 uV = 0.625 uV resolution. */
180 val = DIV_ROUND_CLOSEST(val, 625);
197 val *= shunt_resistor;
198 /* Overflow check: Assuming 32-bit val and shunt resistor, val <= 64bits */
199 val = DIV_ROUND_CLOSEST_ULL(val, 625 * 1000);
200 /* Overflow check: val is now <= 44 bits */
181 } else {
201 } else {
182 /*
183 * 0.5 mV * 25 uV = 0.0125 uV resolution.
184 * Divide first to avoid overflow;
185 * accept loss of accuracy.
186 */
187 val = DIV_ROUND_CLOSEST(val, 25) * 2;
202 /* 0.5 mV * 25 uV = 0.0125 uV resolution. */
203 val *= shunt_resistor;
204 /* Overflow check: Assuming 32-bit val and shunt resistor, val <= 64bits */
205 val = DIV_ROUND_CLOSEST_ULL(val, 25 * 1000) * 2;
206 /* Overflow check: val is now <= 51 bits */
188 }
189 break;
190 case LTC2945_VIN_H:
191 case LTC2945_MAX_VIN_H:
192 case LTC2945_MIN_VIN_H:
193 case LTC2945_MAX_VIN_THRES_H:
194 case LTC2945_MIN_VIN_THRES_H:
195 /* 25 mV resolution. */
207 }
208 break;
209 case LTC2945_VIN_H:
210 case LTC2945_MAX_VIN_H:
211 case LTC2945_MIN_VIN_H:
212 case LTC2945_MAX_VIN_THRES_H:
213 case LTC2945_MIN_VIN_THRES_H:
214 /* 25 mV resolution. */
196 val /= 25;
215 val = DIV_ROUND_CLOSEST_ULL(val, 25);
197 break;
198 case LTC2945_ADIN_H:
199 case LTC2945_MAX_ADIN_H:
200 case LTC2945_MIN_ADIN_THRES_H:
201 case LTC2945_MAX_ADIN_THRES_H:
202 case LTC2945_MIN_ADIN_H:
203 /* 0.5mV resolution. */
204 val *= 2;
205 break;
206 case LTC2945_SENSE_H:
207 case LTC2945_MAX_SENSE_H:
208 case LTC2945_MIN_SENSE_H:
209 case LTC2945_MAX_SENSE_THRES_H:
210 case LTC2945_MIN_SENSE_THRES_H:
216 break;
217 case LTC2945_ADIN_H:
218 case LTC2945_MAX_ADIN_H:
219 case LTC2945_MIN_ADIN_THRES_H:
220 case LTC2945_MAX_ADIN_THRES_H:
221 case LTC2945_MIN_ADIN_H:
222 /* 0.5mV resolution. */
223 val *= 2;
224 break;
225 case LTC2945_SENSE_H:
226 case LTC2945_MAX_SENSE_H:
227 case LTC2945_MIN_SENSE_H:
228 case LTC2945_MAX_SENSE_THRES_H:
229 case LTC2945_MIN_SENSE_THRES_H:
211 /*
212 * 25 uV resolution. Convert to current as measured with
213 * an 1 mOhm sense resistor, in mA. If a different sense
214 * resistor is installed, calculate the actual current by
215 * dividing the reported current by the sense resistor value
216 * in mOhm.
217 */
218 val = DIV_ROUND_CLOSEST(val, 25);
230 /* 25 uV resolution. Convert to mA. */
231 val *= shunt_resistor;
232 /* Overflow check: Assuming 32-bit val and 32-bit shunt resistor, val is 64bits */
233 val = DIV_ROUND_CLOSEST_ULL(val, 25 * 1000);
234 /* Overflow check: val is now <= 50 bits */
219 break;
220 default:
221 return -EINVAL;
222 }
223 return val;
224}
225
226static ssize_t ltc2945_value_show(struct device *dev,

--- 8 unchanged lines hidden (view full) ---

235 return sysfs_emit(buf, "%lld\n", value);
236}
237
238static ssize_t ltc2945_value_store(struct device *dev,
239 struct device_attribute *da,
240 const char *buf, size_t count)
241{
242 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
235 break;
236 default:
237 return -EINVAL;
238 }
239 return val;
240}
241
242static ssize_t ltc2945_value_show(struct device *dev,

--- 8 unchanged lines hidden (view full) ---

251 return sysfs_emit(buf, "%lld\n", value);
252}
253
254static ssize_t ltc2945_value_store(struct device *dev,
255 struct device_attribute *da,
256 const char *buf, size_t count)
257{
258 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
243 struct regmap *regmap = dev_get_drvdata(dev);
259 struct ltc2945_data *data = dev_get_drvdata(dev);
260 struct regmap *regmap = data->regmap;
244 u8 reg = attr->index;
261 u8 reg = attr->index;
245 unsigned long val;
262 unsigned int val;
246 u8 regbuf[3];
247 int num_regs;
263 u8 regbuf[3];
264 int num_regs;
248 int regval;
265 long long regval;
249 int ret;
250
266 int ret;
267
251 ret = kstrtoul(buf, 10, &val);
268 ret = kstrtouint(buf, 10, &val);
252 if (ret)
253 return ret;
254
255 /* convert to register value, then clamp and write result */
256 regval = ltc2945_val_to_reg(dev, reg, val);
257 if (regval < 0)
258 return regval;
259 if (is_power_reg(reg)) {

--- 12 unchanged lines hidden (view full) ---

272 return ret < 0 ? ret : count;
273}
274
275static ssize_t ltc2945_history_store(struct device *dev,
276 struct device_attribute *da,
277 const char *buf, size_t count)
278{
279 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
269 if (ret)
270 return ret;
271
272 /* convert to register value, then clamp and write result */
273 regval = ltc2945_val_to_reg(dev, reg, val);
274 if (regval < 0)
275 return regval;
276 if (is_power_reg(reg)) {

--- 12 unchanged lines hidden (view full) ---

289 return ret < 0 ? ret : count;
290}
291
292static ssize_t ltc2945_history_store(struct device *dev,
293 struct device_attribute *da,
294 const char *buf, size_t count)
295{
296 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
280 struct regmap *regmap = dev_get_drvdata(dev);
297 struct ltc2945_data *data = dev_get_drvdata(dev);
298 struct regmap *regmap = data->regmap;
281 u8 reg = attr->index;
282 int num_regs = is_power_reg(reg) ? 3 : 2;
283 u8 buf_min[3] = { 0xff, 0xff, 0xff };
284 u8 buf_max[3] = { 0, 0, 0 };
285 unsigned long val;
286 int ret;
287
288 ret = kstrtoul(buf, 10, &val);

--- 35 unchanged lines hidden (view full) ---

324
325 return ret ? : count;
326}
327
328static ssize_t ltc2945_bool_show(struct device *dev,
329 struct device_attribute *da, char *buf)
330{
331 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
299 u8 reg = attr->index;
300 int num_regs = is_power_reg(reg) ? 3 : 2;
301 u8 buf_min[3] = { 0xff, 0xff, 0xff };
302 u8 buf_max[3] = { 0, 0, 0 };
303 unsigned long val;
304 int ret;
305
306 ret = kstrtoul(buf, 10, &val);

--- 35 unchanged lines hidden (view full) ---

342
343 return ret ? : count;
344}
345
346static ssize_t ltc2945_bool_show(struct device *dev,
347 struct device_attribute *da, char *buf)
348{
349 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
332 struct regmap *regmap = dev_get_drvdata(dev);
350 struct ltc2945_data *data = dev_get_drvdata(dev);
351 struct regmap *regmap = data->regmap;
333 unsigned int fault;
334 int ret;
335
336 ret = regmap_read(regmap, LTC2945_FAULT, &fault);
337 if (ret < 0)
338 return ret;
339
340 fault &= attr->index;

--- 112 unchanged lines hidden (view full) ---

453 .max_register = LTC2945_MIN_ADIN_THRES_L,
454};
455
456static int ltc2945_probe(struct i2c_client *client)
457{
458 struct device *dev = &client->dev;
459 struct device *hwmon_dev;
460 struct regmap *regmap;
352 unsigned int fault;
353 int ret;
354
355 ret = regmap_read(regmap, LTC2945_FAULT, &fault);
356 if (ret < 0)
357 return ret;
358
359 fault &= attr->index;

--- 112 unchanged lines hidden (view full) ---

472 .max_register = LTC2945_MIN_ADIN_THRES_L,
473};
474
475static int ltc2945_probe(struct i2c_client *client)
476{
477 struct device *dev = &client->dev;
478 struct device *hwmon_dev;
479 struct regmap *regmap;
480 struct ltc2945_data *data;
461
481
482 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
483 if (!data)
484 return -ENOMEM;
485 dev_set_drvdata(dev, data);
486
462 regmap = devm_regmap_init_i2c(client, &ltc2945_regmap_config);
463 if (IS_ERR(regmap)) {
464 dev_err(dev, "failed to allocate register map\n");
465 return PTR_ERR(regmap);
466 }
467
487 regmap = devm_regmap_init_i2c(client, &ltc2945_regmap_config);
488 if (IS_ERR(regmap)) {
489 dev_err(dev, "failed to allocate register map\n");
490 return PTR_ERR(regmap);
491 }
492
493 data->regmap = regmap;
494 if (device_property_read_u32(dev, "shunt-resistor-micro-ohms",
495 &data->shunt_resistor))
496 data->shunt_resistor = 1000;
497
498 if (data->shunt_resistor == 0)
499 return -EINVAL;
500
468 /* Clear faults */
469 regmap_write(regmap, LTC2945_FAULT, 0x00);
470
471 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
501 /* Clear faults */
502 regmap_write(regmap, LTC2945_FAULT, 0x00);
503
504 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
472 regmap,
505 data,
473 ltc2945_groups);
474 return PTR_ERR_OR_ZERO(hwmon_dev);
475}
476
477static const struct i2c_device_id ltc2945_id[] = {
478 {"ltc2945", 0},
479 { }
480};

--- 17 unchanged lines hidden ---
506 ltc2945_groups);
507 return PTR_ERR_OR_ZERO(hwmon_dev);
508}
509
510static const struct i2c_device_id ltc2945_id[] = {
511 {"ltc2945", 0},
512 { }
513};

--- 17 unchanged lines hidden ---