]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/hwmon/ina3221.c
hwmon: Add support for INA3221 Triple Current/Voltage Monitors
[mirror_ubuntu-artful-kernel.git] / drivers / hwmon / ina3221.c
1 /*
2 * INA3221 Triple Current/Voltage Monitor
3 *
4 * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
5 * Andrew F. Davis <afd@ti.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 */
16
17 #include <linux/hwmon.h>
18 #include <linux/hwmon-sysfs.h>
19 #include <linux/i2c.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/regmap.h>
23
24 #define INA3221_DRIVER_NAME "ina3221"
25
26 #define INA3221_CONFIG 0x00
27 #define INA3221_SHUNT1 0x01
28 #define INA3221_BUS1 0x02
29 #define INA3221_SHUNT2 0x03
30 #define INA3221_BUS2 0x04
31 #define INA3221_SHUNT3 0x05
32 #define INA3221_BUS3 0x06
33 #define INA3221_CRIT1 0x07
34 #define INA3221_WARN1 0x08
35 #define INA3221_CRIT2 0x09
36 #define INA3221_WARN2 0x0a
37 #define INA3221_CRIT3 0x0b
38 #define INA3221_WARN3 0x0c
39 #define INA3221_MASK_ENABLE 0x0f
40
41 #define INA3221_CONFIG_MODE_SHUNT BIT(1)
42 #define INA3221_CONFIG_MODE_BUS BIT(2)
43 #define INA3221_CONFIG_MODE_CONTINUOUS BIT(3)
44
45 #define INA3221_RSHUNT_DEFAULT 10000
46
47 enum ina3221_fields {
48 /* Configuration */
49 F_RST,
50
51 /* Alert Flags */
52 F_WF3, F_WF2, F_WF1,
53 F_CF3, F_CF2, F_CF1,
54
55 /* sentinel */
56 F_MAX_FIELDS
57 };
58
59 static const struct reg_field ina3221_reg_fields[] = {
60 [F_RST] = REG_FIELD(INA3221_CONFIG, 15, 15),
61
62 [F_WF3] = REG_FIELD(INA3221_MASK_ENABLE, 3, 3),
63 [F_WF2] = REG_FIELD(INA3221_MASK_ENABLE, 4, 4),
64 [F_WF1] = REG_FIELD(INA3221_MASK_ENABLE, 5, 5),
65 [F_CF3] = REG_FIELD(INA3221_MASK_ENABLE, 7, 7),
66 [F_CF2] = REG_FIELD(INA3221_MASK_ENABLE, 8, 8),
67 [F_CF1] = REG_FIELD(INA3221_MASK_ENABLE, 9, 9),
68 };
69
70 enum ina3221_channels {
71 INA3221_CHANNEL1,
72 INA3221_CHANNEL2,
73 INA3221_CHANNEL3,
74 INA3221_NUM_CHANNELS
75 };
76
77 static const unsigned int register_channel[] = {
78 [INA3221_SHUNT1] = INA3221_CHANNEL1,
79 [INA3221_SHUNT2] = INA3221_CHANNEL2,
80 [INA3221_SHUNT3] = INA3221_CHANNEL3,
81 [INA3221_CRIT1] = INA3221_CHANNEL1,
82 [INA3221_CRIT2] = INA3221_CHANNEL2,
83 [INA3221_CRIT3] = INA3221_CHANNEL3,
84 [INA3221_WARN1] = INA3221_CHANNEL1,
85 [INA3221_WARN2] = INA3221_CHANNEL2,
86 [INA3221_WARN3] = INA3221_CHANNEL3,
87 };
88
89 /**
90 * struct ina3221_data - device specific information
91 * @regmap: Register map of the device
92 * @fields: Register fields of the device
93 * @shunt_resistors: Array of resistor values per channel
94 */
95 struct ina3221_data {
96 struct regmap *regmap;
97 struct regmap_field *fields[F_MAX_FIELDS];
98 unsigned int shunt_resistors[INA3221_NUM_CHANNELS];
99 };
100
101 static int ina3221_read_value(struct ina3221_data *ina, unsigned int reg,
102 int *val)
103 {
104 unsigned int regval;
105 int ret;
106
107 ret = regmap_read(ina->regmap, reg, &regval);
108 if (ret)
109 return ret;
110
111 *val = sign_extend32(regval >> 3, 12);
112
113 return 0;
114 }
115
116 static ssize_t ina3221_show_bus_voltage(struct device *dev,
117 struct device_attribute *attr,
118 char *buf)
119 {
120 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
121 struct ina3221_data *ina = dev_get_drvdata(dev);
122 unsigned int reg = sd_attr->index;
123 int val, voltage_mv, ret;
124
125 ret = ina3221_read_value(ina, reg, &val);
126 if (ret)
127 return ret;
128
129 voltage_mv = val * 8;
130
131 return snprintf(buf, PAGE_SIZE, "%d\n", voltage_mv);
132 }
133
134 static ssize_t ina3221_show_shunt_voltage(struct device *dev,
135 struct device_attribute *attr,
136 char *buf)
137 {
138 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
139 struct ina3221_data *ina = dev_get_drvdata(dev);
140 unsigned int reg = sd_attr->index;
141 int val, voltage_uv, ret;
142
143 ret = ina3221_read_value(ina, reg, &val);
144 if (ret)
145 return ret;
146 voltage_uv = val * 40;
147
148 return snprintf(buf, PAGE_SIZE, "%d\n", voltage_uv);
149 }
150
151 static ssize_t ina3221_show_current(struct device *dev,
152 struct device_attribute *attr, char *buf)
153 {
154 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
155 struct ina3221_data *ina = dev_get_drvdata(dev);
156 unsigned int reg = sd_attr->index;
157 unsigned int channel = register_channel[reg];
158 unsigned int resistance_uo = ina->shunt_resistors[channel];
159 int val, current_ma, voltage_nv, ret;
160
161 ret = ina3221_read_value(ina, reg, &val);
162 if (ret)
163 return ret;
164 voltage_nv = val * 40000;
165
166 current_ma = DIV_ROUND_CLOSEST(voltage_nv, resistance_uo);
167
168 return snprintf(buf, PAGE_SIZE, "%d\n", current_ma);
169 }
170
171 static ssize_t ina3221_set_current(struct device *dev,
172 struct device_attribute *attr,
173 const char *buf, size_t count)
174 {
175 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
176 struct ina3221_data *ina = dev_get_drvdata(dev);
177 unsigned int reg = sd_attr->index;
178 unsigned int channel = register_channel[reg];
179 unsigned int resistance_uo = ina->shunt_resistors[channel];
180 int val, current_ma, voltage_uv, ret;
181
182 ret = kstrtoint(buf, 0, &current_ma);
183 if (ret)
184 return ret;
185
186 /* clamp current */
187 current_ma = clamp_val(current_ma,
188 INT_MIN / resistance_uo,
189 INT_MAX / resistance_uo);
190
191 voltage_uv = DIV_ROUND_CLOSEST(current_ma * resistance_uo, 1000);
192
193 /* clamp voltage */
194 voltage_uv = clamp_val(voltage_uv, -163800, 163800);
195
196 /* 1 / 40uV(scale) << 3(register shift) = 5 */
197 val = DIV_ROUND_CLOSEST(voltage_uv, 5) & 0xfff8;
198
199 ret = regmap_write(ina->regmap, reg, val);
200 if (ret)
201 return ret;
202
203 return count;
204 }
205
206 static ssize_t ina3221_show_shunt(struct device *dev,
207 struct device_attribute *attr, char *buf)
208 {
209 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
210 struct ina3221_data *ina = dev_get_drvdata(dev);
211 unsigned int channel = sd_attr->index;
212 unsigned int resistance_uo;
213
214 resistance_uo = ina->shunt_resistors[channel];
215
216 return snprintf(buf, PAGE_SIZE, "%d\n", resistance_uo);
217 }
218
219 static ssize_t ina3221_set_shunt(struct device *dev,
220 struct device_attribute *attr,
221 const char *buf, size_t count)
222 {
223 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
224 struct ina3221_data *ina = dev_get_drvdata(dev);
225 unsigned int channel = sd_attr->index;
226 unsigned int val;
227 int ret;
228
229 ret = kstrtouint(buf, 0, &val);
230 if (ret)
231 return ret;
232
233 if (val == 0)
234 return -EINVAL;
235
236 ina->shunt_resistors[channel] = val;
237
238 return count;
239 }
240
241 static ssize_t ina3221_show_alert(struct device *dev,
242 struct device_attribute *attr, char *buf)
243 {
244 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
245 struct ina3221_data *ina = dev_get_drvdata(dev);
246 unsigned int field = sd_attr->index;
247 unsigned int regval;
248 int ret;
249
250 ret = regmap_field_read(ina->fields[field], &regval);
251 if (ret)
252 return ret;
253
254 return snprintf(buf, PAGE_SIZE, "%d\n", regval);
255 }
256
257 /* bus voltage */
258 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO,
259 ina3221_show_bus_voltage, NULL, INA3221_BUS1);
260 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO,
261 ina3221_show_bus_voltage, NULL, INA3221_BUS2);
262 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO,
263 ina3221_show_bus_voltage, NULL, INA3221_BUS3);
264
265 /* calculated current */
266 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO,
267 ina3221_show_current, NULL, INA3221_SHUNT1);
268 static SENSOR_DEVICE_ATTR(curr2_input, S_IRUGO,
269 ina3221_show_current, NULL, INA3221_SHUNT2);
270 static SENSOR_DEVICE_ATTR(curr3_input, S_IRUGO,
271 ina3221_show_current, NULL, INA3221_SHUNT3);
272
273 /* shunt resistance */
274 static SENSOR_DEVICE_ATTR(shunt1_resistor, S_IRUGO | S_IWUSR,
275 ina3221_show_shunt, ina3221_set_shunt, INA3221_CHANNEL1);
276 static SENSOR_DEVICE_ATTR(shunt2_resistor, S_IRUGO | S_IWUSR,
277 ina3221_show_shunt, ina3221_set_shunt, INA3221_CHANNEL2);
278 static SENSOR_DEVICE_ATTR(shunt3_resistor, S_IRUGO | S_IWUSR,
279 ina3221_show_shunt, ina3221_set_shunt, INA3221_CHANNEL3);
280
281 /* critical current */
282 static SENSOR_DEVICE_ATTR(curr1_crit, S_IRUGO | S_IWUSR,
283 ina3221_show_current, ina3221_set_current, INA3221_CRIT1);
284 static SENSOR_DEVICE_ATTR(curr2_crit, S_IRUGO | S_IWUSR,
285 ina3221_show_current, ina3221_set_current, INA3221_CRIT2);
286 static SENSOR_DEVICE_ATTR(curr3_crit, S_IRUGO | S_IWUSR,
287 ina3221_show_current, ina3221_set_current, INA3221_CRIT3);
288
289 /* critical current alert */
290 static SENSOR_DEVICE_ATTR(curr1_crit_alarm, S_IRUGO,
291 ina3221_show_alert, NULL, F_CF1);
292 static SENSOR_DEVICE_ATTR(curr2_crit_alarm, S_IRUGO,
293 ina3221_show_alert, NULL, F_CF2);
294 static SENSOR_DEVICE_ATTR(curr3_crit_alarm, S_IRUGO,
295 ina3221_show_alert, NULL, F_CF3);
296
297 /* warning current */
298 static SENSOR_DEVICE_ATTR(curr1_max, S_IRUGO | S_IWUSR,
299 ina3221_show_current, ina3221_set_current, INA3221_WARN1);
300 static SENSOR_DEVICE_ATTR(curr2_max, S_IRUGO | S_IWUSR,
301 ina3221_show_current, ina3221_set_current, INA3221_WARN2);
302 static SENSOR_DEVICE_ATTR(curr3_max, S_IRUGO | S_IWUSR,
303 ina3221_show_current, ina3221_set_current, INA3221_WARN3);
304
305 /* warning current alert */
306 static SENSOR_DEVICE_ATTR(curr1_max_alarm, S_IRUGO,
307 ina3221_show_alert, NULL, F_WF1);
308 static SENSOR_DEVICE_ATTR(curr2_max_alarm, S_IRUGO,
309 ina3221_show_alert, NULL, F_WF2);
310 static SENSOR_DEVICE_ATTR(curr3_max_alarm, S_IRUGO,
311 ina3221_show_alert, NULL, F_WF3);
312
313 /* shunt voltage */
314 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO,
315 ina3221_show_shunt_voltage, NULL, INA3221_SHUNT1);
316 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO,
317 ina3221_show_shunt_voltage, NULL, INA3221_SHUNT2);
318 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO,
319 ina3221_show_shunt_voltage, NULL, INA3221_SHUNT3);
320
321 static struct attribute *ina3221_attrs[] = {
322 /* channel 1 */
323 &sensor_dev_attr_in1_input.dev_attr.attr,
324 &sensor_dev_attr_curr1_input.dev_attr.attr,
325 &sensor_dev_attr_shunt1_resistor.dev_attr.attr,
326 &sensor_dev_attr_curr1_crit.dev_attr.attr,
327 &sensor_dev_attr_curr1_crit_alarm.dev_attr.attr,
328 &sensor_dev_attr_curr1_max.dev_attr.attr,
329 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
330 &sensor_dev_attr_in4_input.dev_attr.attr,
331
332 /* channel 2 */
333 &sensor_dev_attr_in2_input.dev_attr.attr,
334 &sensor_dev_attr_curr2_input.dev_attr.attr,
335 &sensor_dev_attr_shunt2_resistor.dev_attr.attr,
336 &sensor_dev_attr_curr2_crit.dev_attr.attr,
337 &sensor_dev_attr_curr2_crit_alarm.dev_attr.attr,
338 &sensor_dev_attr_curr2_max.dev_attr.attr,
339 &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
340 &sensor_dev_attr_in5_input.dev_attr.attr,
341
342 /* channel 3 */
343 &sensor_dev_attr_in3_input.dev_attr.attr,
344 &sensor_dev_attr_curr3_input.dev_attr.attr,
345 &sensor_dev_attr_shunt3_resistor.dev_attr.attr,
346 &sensor_dev_attr_curr3_crit.dev_attr.attr,
347 &sensor_dev_attr_curr3_crit_alarm.dev_attr.attr,
348 &sensor_dev_attr_curr3_max.dev_attr.attr,
349 &sensor_dev_attr_curr3_max_alarm.dev_attr.attr,
350 &sensor_dev_attr_in6_input.dev_attr.attr,
351
352 NULL,
353 };
354 ATTRIBUTE_GROUPS(ina3221);
355
356 static const struct regmap_range ina3221_yes_ranges[] = {
357 regmap_reg_range(INA3221_SHUNT1, INA3221_BUS3),
358 regmap_reg_range(INA3221_MASK_ENABLE, INA3221_MASK_ENABLE),
359 };
360
361 static const struct regmap_access_table ina3221_volatile_table = {
362 .yes_ranges = ina3221_yes_ranges,
363 .n_yes_ranges = ARRAY_SIZE(ina3221_yes_ranges),
364 };
365
366 static const struct regmap_config ina3221_regmap_config = {
367 .reg_bits = 8,
368 .val_bits = 16,
369
370 .cache_type = REGCACHE_RBTREE,
371 .volatile_table = &ina3221_volatile_table,
372 };
373
374 static int ina3221_probe(struct i2c_client *client,
375 const struct i2c_device_id *id)
376 {
377 struct device *dev = &client->dev;
378 struct ina3221_data *ina;
379 struct device *hwmon_dev;
380 int i, ret;
381
382 ina = devm_kzalloc(dev, sizeof(*ina), GFP_KERNEL);
383 if (!ina)
384 return -ENOMEM;
385
386 ina->regmap = devm_regmap_init_i2c(client, &ina3221_regmap_config);
387 if (IS_ERR(ina->regmap)) {
388 dev_err(dev, "Unable to allocate register map\n");
389 return PTR_ERR(ina->regmap);
390 }
391
392 for (i = 0; i < F_MAX_FIELDS; i++) {
393 ina->fields[i] = devm_regmap_field_alloc(dev,
394 ina->regmap,
395 ina3221_reg_fields[i]);
396 if (IS_ERR(ina->fields[i])) {
397 dev_err(dev, "Unable to allocate regmap fields\n");
398 return PTR_ERR(ina->fields[i]);
399 }
400 }
401
402 for (i = 0; i < INA3221_NUM_CHANNELS; i++)
403 ina->shunt_resistors[i] = INA3221_RSHUNT_DEFAULT;
404
405 ret = regmap_field_write(ina->fields[F_RST], true);
406 if (ret) {
407 dev_err(dev, "Unable to reset device\n");
408 return ret;
409 }
410
411 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
412 client->name,
413 ina, ina3221_groups);
414 if (IS_ERR(hwmon_dev)) {
415 dev_err(dev, "Unable to register hwmon device\n");
416 return PTR_ERR(hwmon_dev);
417 }
418
419 return 0;
420 }
421
422 static const struct of_device_id ina3221_of_match_table[] = {
423 { .compatible = "ti,ina3221", },
424 { /* sentinel */ }
425 };
426 MODULE_DEVICE_TABLE(of, ina3221_of_match_table);
427
428 static const struct i2c_device_id ina3221_ids[] = {
429 { "ina3221", 0 },
430 { /* sentinel */ }
431 };
432 MODULE_DEVICE_TABLE(i2c, ina3221_ids);
433
434 static struct i2c_driver ina3221_i2c_driver = {
435 .probe = ina3221_probe,
436 .driver = {
437 .name = INA3221_DRIVER_NAME,
438 .of_match_table = ina3221_of_match_table,
439 },
440 .id_table = ina3221_ids,
441 };
442 module_i2c_driver(ina3221_i2c_driver);
443
444 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
445 MODULE_DESCRIPTION("Texas Instruments INA3221 HWMon Driver");
446 MODULE_LICENSE("GPL v2");