1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/err.h>
27#include <linux/slab.h>
28#include <linux/bug.h>
29#include <linux/i2c.h>
30#include <linux/hwmon.h>
31#include <linux/hwmon-sysfs.h>
32
33#include <linux/platform_data/ina2xx.h>
34
35
36#define INA209_CONFIGURATION 0x00
37#define INA209_STATUS 0x01
38#define INA209_STATUS_MASK 0x02
39#define INA209_SHUNT_VOLTAGE 0x03
40#define INA209_BUS_VOLTAGE 0x04
41#define INA209_POWER 0x05
42#define INA209_CURRENT 0x06
43#define INA209_SHUNT_VOLTAGE_POS_PEAK 0x07
44#define INA209_SHUNT_VOLTAGE_NEG_PEAK 0x08
45#define INA209_BUS_VOLTAGE_MAX_PEAK 0x09
46#define INA209_BUS_VOLTAGE_MIN_PEAK 0x0a
47#define INA209_POWER_PEAK 0x0b
48#define INA209_SHUNT_VOLTAGE_POS_WARN 0x0c
49#define INA209_SHUNT_VOLTAGE_NEG_WARN 0x0d
50#define INA209_POWER_WARN 0x0e
51#define INA209_BUS_VOLTAGE_OVER_WARN 0x0f
52#define INA209_BUS_VOLTAGE_UNDER_WARN 0x10
53#define INA209_POWER_OVER_LIMIT 0x11
54#define INA209_BUS_VOLTAGE_OVER_LIMIT 0x12
55#define INA209_BUS_VOLTAGE_UNDER_LIMIT 0x13
56#define INA209_CRITICAL_DAC_POS 0x14
57#define INA209_CRITICAL_DAC_NEG 0x15
58#define INA209_CALIBRATION 0x16
59
60#define INA209_REGISTERS 0x17
61
62#define INA209_CONFIG_DEFAULT 0x3c47
63#define INA209_SHUNT_DEFAULT 10000
64
65struct ina209_data {
66 struct i2c_client *client;
67
68 struct mutex update_lock;
69 bool valid;
70 unsigned long last_updated;
71
72 u16 regs[INA209_REGISTERS];
73
74 u16 config_orig;
75 u16 calibration_orig;
76 u16 update_interval;
77};
78
79static struct ina209_data *ina209_update_device(struct device *dev)
80{
81 struct ina209_data *data = dev_get_drvdata(dev);
82 struct i2c_client *client = data->client;
83 struct ina209_data *ret = data;
84 s32 val;
85 int i;
86
87 mutex_lock(&data->update_lock);
88
89 if (!data->valid ||
90 time_after(jiffies, data->last_updated + data->update_interval)) {
91 for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
92 val = i2c_smbus_read_word_swapped(client, i);
93 if (val < 0) {
94 ret = ERR_PTR(val);
95 goto abort;
96 }
97 data->regs[i] = val;
98 }
99 data->last_updated = jiffies;
100 data->valid = true;
101 }
102abort:
103 mutex_unlock(&data->update_lock);
104 return ret;
105}
106
107
108
109
110
111static long ina209_from_reg(const u8 reg, const u16 val)
112{
113 switch (reg) {
114 case INA209_SHUNT_VOLTAGE:
115 case INA209_SHUNT_VOLTAGE_POS_PEAK:
116 case INA209_SHUNT_VOLTAGE_NEG_PEAK:
117 case INA209_SHUNT_VOLTAGE_POS_WARN:
118 case INA209_SHUNT_VOLTAGE_NEG_WARN:
119
120 return DIV_ROUND_CLOSEST(val, 100);
121
122 case INA209_BUS_VOLTAGE:
123 case INA209_BUS_VOLTAGE_MAX_PEAK:
124 case INA209_BUS_VOLTAGE_MIN_PEAK:
125 case INA209_BUS_VOLTAGE_OVER_WARN:
126 case INA209_BUS_VOLTAGE_UNDER_WARN:
127 case INA209_BUS_VOLTAGE_OVER_LIMIT:
128 case INA209_BUS_VOLTAGE_UNDER_LIMIT:
129
130 return (val >> 3) * 4;
131
132 case INA209_CRITICAL_DAC_POS:
133
134 return val >> 8;
135
136 case INA209_CRITICAL_DAC_NEG:
137
138 return -1 * (val >> 8);
139
140 case INA209_POWER:
141 case INA209_POWER_PEAK:
142 case INA209_POWER_WARN:
143 case INA209_POWER_OVER_LIMIT:
144
145 return val * 20 * 1000L;
146
147 case INA209_CURRENT:
148
149 return val;
150 }
151
152
153 WARN_ON_ONCE(1);
154 return 0;
155}
156
157
158
159
160
161static int ina209_to_reg(u8 reg, u16 old, long val)
162{
163 switch (reg) {
164 case INA209_SHUNT_VOLTAGE_POS_WARN:
165 case INA209_SHUNT_VOLTAGE_NEG_WARN:
166
167 return clamp_val(val, -320, 320) * 100;
168
169 case INA209_BUS_VOLTAGE_OVER_WARN:
170 case INA209_BUS_VOLTAGE_UNDER_WARN:
171 case INA209_BUS_VOLTAGE_OVER_LIMIT:
172 case INA209_BUS_VOLTAGE_UNDER_LIMIT:
173
174
175
176
177
178
179 return (DIV_ROUND_CLOSEST(clamp_val(val, 0, 32000), 4) << 3)
180 | (old & 0x7);
181
182 case INA209_CRITICAL_DAC_NEG:
183
184
185
186
187
188
189
190 return (clamp_val(-val, 0, 255) << 8) | (old & 0xff);
191
192 case INA209_CRITICAL_DAC_POS:
193
194
195
196
197
198
199 return (clamp_val(val, 0, 255) << 8) | (old & 0xff);
200
201 case INA209_POWER_WARN:
202 case INA209_POWER_OVER_LIMIT:
203
204 return DIV_ROUND_CLOSEST(val, 20 * 1000);
205 }
206
207
208 return -EACCES;
209}
210
211static int ina209_interval_from_reg(u16 reg)
212{
213 return 68 >> (15 - ((reg >> 3) & 0x0f));
214}
215
216static u16 ina209_reg_from_interval(u16 config, long interval)
217{
218 int i, adc;
219
220 if (interval <= 0) {
221 adc = 8;
222 } else {
223 adc = 15;
224 for (i = 34 + 34 / 2; i; i >>= 1) {
225 if (i < interval)
226 break;
227 adc--;
228 }
229 }
230 return (config & 0xf807) | (adc << 3) | (adc << 7);
231}
232
233static ssize_t ina209_set_interval(struct device *dev,
234 struct device_attribute *da,
235 const char *buf, size_t count)
236{
237 struct ina209_data *data = ina209_update_device(dev);
238 long val;
239 u16 regval;
240 int ret;
241
242 if (IS_ERR(data))
243 return PTR_ERR(data);
244
245 ret = kstrtol(buf, 10, &val);
246 if (ret < 0)
247 return ret;
248
249 mutex_lock(&data->update_lock);
250 regval = ina209_reg_from_interval(data->regs[INA209_CONFIGURATION],
251 val);
252 i2c_smbus_write_word_swapped(data->client, INA209_CONFIGURATION,
253 regval);
254 data->regs[INA209_CONFIGURATION] = regval;
255 data->update_interval = ina209_interval_from_reg(regval);
256 mutex_unlock(&data->update_lock);
257 return count;
258}
259
260static ssize_t ina209_show_interval(struct device *dev,
261 struct device_attribute *da, char *buf)
262{
263 struct ina209_data *data = dev_get_drvdata(dev);
264
265 return snprintf(buf, PAGE_SIZE, "%d\n", data->update_interval);
266}
267
268
269
270
271
272
273
274static u16 ina209_reset_history_regs[] = {
275 INA209_SHUNT_VOLTAGE_POS_PEAK,
276 INA209_SHUNT_VOLTAGE_NEG_PEAK,
277 INA209_BUS_VOLTAGE_MAX_PEAK,
278 INA209_BUS_VOLTAGE_MIN_PEAK,
279 INA209_POWER_PEAK
280};
281
282static ssize_t ina209_reset_history(struct device *dev,
283 struct device_attribute *da,
284 const char *buf,
285 size_t count)
286{
287 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
288 struct ina209_data *data = dev_get_drvdata(dev);
289 struct i2c_client *client = data->client;
290 u32 mask = attr->index;
291 long val;
292 int i, ret;
293
294 ret = kstrtol(buf, 10, &val);
295 if (ret < 0)
296 return ret;
297
298 mutex_lock(&data->update_lock);
299 for (i = 0; i < ARRAY_SIZE(ina209_reset_history_regs); i++) {
300 if (mask & (1 << i))
301 i2c_smbus_write_word_swapped(client,
302 ina209_reset_history_regs[i], 1);
303 }
304 data->valid = false;
305 mutex_unlock(&data->update_lock);
306 return count;
307}
308
309static ssize_t ina209_set_value(struct device *dev,
310 struct device_attribute *da,
311 const char *buf,
312 size_t count)
313{
314 struct ina209_data *data = ina209_update_device(dev);
315 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
316 int reg = attr->index;
317 long val;
318 int ret;
319
320 if (IS_ERR(data))
321 return PTR_ERR(data);
322
323 ret = kstrtol(buf, 10, &val);
324 if (ret < 0)
325 return ret;
326
327 mutex_lock(&data->update_lock);
328 ret = ina209_to_reg(reg, data->regs[reg], val);
329 if (ret < 0) {
330 count = ret;
331 goto abort;
332 }
333 i2c_smbus_write_word_swapped(data->client, reg, ret);
334 data->regs[reg] = ret;
335abort:
336 mutex_unlock(&data->update_lock);
337 return count;
338}
339
340static ssize_t ina209_show_value(struct device *dev,
341 struct device_attribute *da,
342 char *buf)
343{
344 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
345 struct ina209_data *data = ina209_update_device(dev);
346 long val;
347
348 if (IS_ERR(data))
349 return PTR_ERR(data);
350
351 val = ina209_from_reg(attr->index, data->regs[attr->index]);
352 return snprintf(buf, PAGE_SIZE, "%ld\n", val);
353}
354
355static ssize_t ina209_show_alarm(struct device *dev,
356 struct device_attribute *da,
357 char *buf)
358{
359 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
360 struct ina209_data *data = ina209_update_device(dev);
361 const unsigned int mask = attr->index;
362 u16 status;
363
364 if (IS_ERR(data))
365 return PTR_ERR(data);
366
367 status = data->regs[INA209_STATUS];
368
369
370
371
372
373 return snprintf(buf, PAGE_SIZE, "%u\n", !!(status & mask));
374}
375
376
377static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina209_show_value, NULL,
378 INA209_SHUNT_VOLTAGE);
379static SENSOR_DEVICE_ATTR(in0_input_highest, S_IRUGO, ina209_show_value, NULL,
380 INA209_SHUNT_VOLTAGE_POS_PEAK);
381static SENSOR_DEVICE_ATTR(in0_input_lowest, S_IRUGO, ina209_show_value, NULL,
382 INA209_SHUNT_VOLTAGE_NEG_PEAK);
383static SENSOR_DEVICE_ATTR(in0_reset_history, S_IWUSR, NULL,
384 ina209_reset_history, (1 << 0) | (1 << 1));
385static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR, ina209_show_value,
386 ina209_set_value, INA209_SHUNT_VOLTAGE_POS_WARN);
387static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR, ina209_show_value,
388 ina209_set_value, INA209_SHUNT_VOLTAGE_NEG_WARN);
389static SENSOR_DEVICE_ATTR(in0_crit_max, S_IRUGO | S_IWUSR, ina209_show_value,
390 ina209_set_value, INA209_CRITICAL_DAC_POS);
391static SENSOR_DEVICE_ATTR(in0_crit_min, S_IRUGO | S_IWUSR, ina209_show_value,
392 ina209_set_value, INA209_CRITICAL_DAC_NEG);
393
394static SENSOR_DEVICE_ATTR(in0_min_alarm, S_IRUGO, ina209_show_alarm, NULL,
395 1 << 11);
396static SENSOR_DEVICE_ATTR(in0_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
397 1 << 12);
398static SENSOR_DEVICE_ATTR(in0_crit_min_alarm, S_IRUGO, ina209_show_alarm, NULL,
399 1 << 6);
400static SENSOR_DEVICE_ATTR(in0_crit_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
401 1 << 7);
402
403
404static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina209_show_value, NULL,
405 INA209_BUS_VOLTAGE);
406static SENSOR_DEVICE_ATTR(in1_input_highest, S_IRUGO, ina209_show_value, NULL,
407 INA209_BUS_VOLTAGE_MAX_PEAK);
408static SENSOR_DEVICE_ATTR(in1_input_lowest, S_IRUGO, ina209_show_value, NULL,
409 INA209_BUS_VOLTAGE_MIN_PEAK);
410static SENSOR_DEVICE_ATTR(in1_reset_history, S_IWUSR, NULL,
411 ina209_reset_history, (1 << 2) | (1 << 3));
412static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR, ina209_show_value,
413 ina209_set_value, INA209_BUS_VOLTAGE_OVER_WARN);
414static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR, ina209_show_value,
415 ina209_set_value, INA209_BUS_VOLTAGE_UNDER_WARN);
416static SENSOR_DEVICE_ATTR(in1_crit_max, S_IRUGO | S_IWUSR, ina209_show_value,
417 ina209_set_value, INA209_BUS_VOLTAGE_OVER_LIMIT);
418static SENSOR_DEVICE_ATTR(in1_crit_min, S_IRUGO | S_IWUSR, ina209_show_value,
419 ina209_set_value, INA209_BUS_VOLTAGE_UNDER_LIMIT);
420
421static SENSOR_DEVICE_ATTR(in1_min_alarm, S_IRUGO, ina209_show_alarm, NULL,
422 1 << 14);
423static SENSOR_DEVICE_ATTR(in1_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
424 1 << 15);
425static SENSOR_DEVICE_ATTR(in1_crit_min_alarm, S_IRUGO, ina209_show_alarm, NULL,
426 1 << 9);
427static SENSOR_DEVICE_ATTR(in1_crit_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
428 1 << 10);
429
430
431static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina209_show_value, NULL,
432 INA209_POWER);
433static SENSOR_DEVICE_ATTR(power1_input_highest, S_IRUGO, ina209_show_value,
434 NULL, INA209_POWER_PEAK);
435static SENSOR_DEVICE_ATTR(power1_reset_history, S_IWUSR, NULL,
436 ina209_reset_history, 1 << 4);
437static SENSOR_DEVICE_ATTR(power1_max, S_IRUGO | S_IWUSR, ina209_show_value,
438 ina209_set_value, INA209_POWER_WARN);
439static SENSOR_DEVICE_ATTR(power1_crit, S_IRUGO | S_IWUSR, ina209_show_value,
440 ina209_set_value, INA209_POWER_OVER_LIMIT);
441
442static SENSOR_DEVICE_ATTR(power1_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
443 1 << 13);
444static SENSOR_DEVICE_ATTR(power1_crit_alarm, S_IRUGO, ina209_show_alarm, NULL,
445 1 << 8);
446
447
448static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina209_show_value, NULL,
449 INA209_CURRENT);
450
451static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR,
452 ina209_show_interval, ina209_set_interval, 0);
453
454
455
456
457
458static struct attribute *ina209_attrs[] = {
459 &sensor_dev_attr_in0_input.dev_attr.attr,
460 &sensor_dev_attr_in0_input_highest.dev_attr.attr,
461 &sensor_dev_attr_in0_input_lowest.dev_attr.attr,
462 &sensor_dev_attr_in0_reset_history.dev_attr.attr,
463 &sensor_dev_attr_in0_max.dev_attr.attr,
464 &sensor_dev_attr_in0_min.dev_attr.attr,
465 &sensor_dev_attr_in0_crit_max.dev_attr.attr,
466 &sensor_dev_attr_in0_crit_min.dev_attr.attr,
467 &sensor_dev_attr_in0_max_alarm.dev_attr.attr,
468 &sensor_dev_attr_in0_min_alarm.dev_attr.attr,
469 &sensor_dev_attr_in0_crit_max_alarm.dev_attr.attr,
470 &sensor_dev_attr_in0_crit_min_alarm.dev_attr.attr,
471
472 &sensor_dev_attr_in1_input.dev_attr.attr,
473 &sensor_dev_attr_in1_input_highest.dev_attr.attr,
474 &sensor_dev_attr_in1_input_lowest.dev_attr.attr,
475 &sensor_dev_attr_in1_reset_history.dev_attr.attr,
476 &sensor_dev_attr_in1_max.dev_attr.attr,
477 &sensor_dev_attr_in1_min.dev_attr.attr,
478 &sensor_dev_attr_in1_crit_max.dev_attr.attr,
479 &sensor_dev_attr_in1_crit_min.dev_attr.attr,
480 &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
481 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
482 &sensor_dev_attr_in1_crit_max_alarm.dev_attr.attr,
483 &sensor_dev_attr_in1_crit_min_alarm.dev_attr.attr,
484
485 &sensor_dev_attr_power1_input.dev_attr.attr,
486 &sensor_dev_attr_power1_input_highest.dev_attr.attr,
487 &sensor_dev_attr_power1_reset_history.dev_attr.attr,
488 &sensor_dev_attr_power1_max.dev_attr.attr,
489 &sensor_dev_attr_power1_crit.dev_attr.attr,
490 &sensor_dev_attr_power1_max_alarm.dev_attr.attr,
491 &sensor_dev_attr_power1_crit_alarm.dev_attr.attr,
492
493 &sensor_dev_attr_curr1_input.dev_attr.attr,
494
495 &sensor_dev_attr_update_interval.dev_attr.attr,
496
497 NULL,
498};
499ATTRIBUTE_GROUPS(ina209);
500
501static void ina209_restore_conf(struct i2c_client *client,
502 struct ina209_data *data)
503{
504
505 i2c_smbus_write_word_swapped(client, INA209_CONFIGURATION,
506 data->config_orig);
507 i2c_smbus_write_word_swapped(client, INA209_CALIBRATION,
508 data->calibration_orig);
509}
510
511static int ina209_init_client(struct i2c_client *client,
512 struct ina209_data *data)
513{
514 struct ina2xx_platform_data *pdata = dev_get_platdata(&client->dev);
515 u32 shunt;
516 int reg;
517
518 reg = i2c_smbus_read_word_swapped(client, INA209_CALIBRATION);
519 if (reg < 0)
520 return reg;
521 data->calibration_orig = reg;
522
523 reg = i2c_smbus_read_word_swapped(client, INA209_CONFIGURATION);
524 if (reg < 0)
525 return reg;
526 data->config_orig = reg;
527
528 if (pdata) {
529 if (pdata->shunt_uohms <= 0)
530 return -EINVAL;
531 shunt = pdata->shunt_uohms;
532 } else if (!of_property_read_u32(client->dev.of_node, "shunt-resistor",
533 &shunt)) {
534 if (shunt == 0)
535 return -EINVAL;
536 } else {
537 shunt = data->calibration_orig ?
538 40960000 / data->calibration_orig : INA209_SHUNT_DEFAULT;
539 }
540
541 i2c_smbus_write_word_swapped(client, INA209_CONFIGURATION,
542 INA209_CONFIG_DEFAULT);
543 data->update_interval = ina209_interval_from_reg(INA209_CONFIG_DEFAULT);
544
545
546
547
548
549 i2c_smbus_write_word_swapped(client, INA209_CALIBRATION,
550 clamp_val(40960000 / shunt, 1, 65535));
551
552
553 i2c_smbus_read_word_swapped(client, INA209_STATUS);
554
555 return 0;
556}
557
558static int ina209_probe(struct i2c_client *client,
559 const struct i2c_device_id *id)
560{
561 struct i2c_adapter *adapter = client->adapter;
562 struct ina209_data *data;
563 struct device *hwmon_dev;
564 int ret;
565
566 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
567 return -ENODEV;
568
569 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
570 if (!data)
571 return -ENOMEM;
572
573 i2c_set_clientdata(client, data);
574 data->client = client;
575 mutex_init(&data->update_lock);
576
577 ret = ina209_init_client(client, data);
578 if (ret)
579 return ret;
580
581 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
582 client->name,
583 data, ina209_groups);
584 if (IS_ERR(hwmon_dev)) {
585 ret = PTR_ERR(hwmon_dev);
586 goto out_restore_conf;
587 }
588
589 return 0;
590
591out_restore_conf:
592 ina209_restore_conf(client, data);
593 return ret;
594}
595
596static int ina209_remove(struct i2c_client *client)
597{
598 struct ina209_data *data = i2c_get_clientdata(client);
599
600 ina209_restore_conf(client, data);
601
602 return 0;
603}
604
605static const struct i2c_device_id ina209_id[] = {
606 { "ina209", 0 },
607 { }
608};
609MODULE_DEVICE_TABLE(i2c, ina209_id);
610
611
612static struct i2c_driver ina209_driver = {
613 .class = I2C_CLASS_HWMON,
614 .driver = {
615 .name = "ina209",
616 },
617 .probe = ina209_probe,
618 .remove = ina209_remove,
619 .id_table = ina209_id,
620};
621
622module_i2c_driver(ina209_driver);
623
624MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>, Paul Hays <Paul.Hays@cattail.ca>, Guenter Roeck <linux@roeck-us.net>");
625MODULE_DESCRIPTION("INA209 driver");
626MODULE_LICENSE("GPL");
627