blob: 6a4ec2f2ddb26ccef37ace5def80c28c4e1f585a [file] [log] [blame]
Guenter Roeckceeadc52009-08-20 14:49:25 -07001/*
2 * Driver for the Texas Instruments / Burr Brown INA209
3 * Bidirectional Current/Power Monitor
4 *
5 * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net>
6 *
7 * Derived from Ira W. Snyder's original driver submission
8 * Copyright (C) 2008 Paul Hays <Paul.Hays@cattail.ca>
9 * Copyright (C) 2008-2009 Ira W. Snyder <iws@ovro.caltech.edu>
10 *
11 * Aligned with ina2xx driver
12 * Copyright (C) 2012 Lothar Felten <l-felten@ti.com>
13 * Thanks to Jan Volkering
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; version 2 of the License.
18 *
19 * Datasheet:
20 * http://www.ti.com/lit/gpn/ina209
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/* register definitions */
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 /* PGA=8, full range */
63#define INA209_SHUNT_DEFAULT 10000 /* uOhm */
64
65struct ina209_data {
Guenter Roeck89de7342013-09-04 16:55:59 -070066 struct i2c_client *client;
Guenter Roeckceeadc52009-08-20 14:49:25 -070067
68 struct mutex update_lock;
69 bool valid;
70 unsigned long last_updated; /* in jiffies */
71
72 u16 regs[INA209_REGISTERS]; /* All chip registers */
73
74 u16 config_orig; /* Original configuration */
75 u16 calibration_orig; /* Original calibration */
76 u16 update_interval;
77};
78
79static struct ina209_data *ina209_update_device(struct device *dev)
80{
Guenter Roeck89de7342013-09-04 16:55:59 -070081 struct ina209_data *data = dev_get_drvdata(dev);
82 struct i2c_client *client = data->client;
Guenter Roeckceeadc52009-08-20 14:49:25 -070083 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 * Read a value from a device register and convert it to the
109 * appropriate sysfs units
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 /* LSB=10 uV. Convert to mV. */
Joe Schaackbdaf9c402017-04-18 07:15:50 -0500120 return DIV_ROUND_CLOSEST((s16)val, 100);
Guenter Roeckceeadc52009-08-20 14:49:25 -0700121
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 /* LSB=4 mV, last 3 bits unused */
130 return (val >> 3) * 4;
131
132 case INA209_CRITICAL_DAC_POS:
133 /* LSB=1 mV, in the upper 8 bits */
134 return val >> 8;
135
136 case INA209_CRITICAL_DAC_NEG:
137 /* LSB=1 mV, in the upper 8 bits */
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 /* LSB=20 mW. Convert to uW */
145 return val * 20 * 1000L;
146
147 case INA209_CURRENT:
148 /* LSB=1 mA (selected). Is in mA */
Joe Schaackbdaf9c402017-04-18 07:15:50 -0500149 return (s16)val;
Guenter Roeckceeadc52009-08-20 14:49:25 -0700150 }
151
152 /* programmer goofed */
153 WARN_ON_ONCE(1);
154 return 0;
155}
156
157/*
158 * Take a value and convert it to register format, clamping the value
159 * to the appropriate range.
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 /* Limit to +- 320 mV, 10 uV LSB */
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 * Limit to 0-32000 mV, 4 mV LSB
175 *
176 * The last three bits aren't part of the value, but we'll
177 * preserve them in their original state.
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 * Limit to -255-0 mV, 1 mV LSB
185 * Convert the value to a positive value for the register
186 *
187 * The value lives in the top 8 bits only, be careful
188 * and keep original value of other bits.
189 */
190 return (clamp_val(-val, 0, 255) << 8) | (old & 0xff);
191
192 case INA209_CRITICAL_DAC_POS:
193 /*
194 * Limit to 0-255 mV, 1 mV LSB
195 *
196 * The value lives in the top 8 bits only, be careful
197 * and keep original value of other bits.
198 */
199 return (clamp_val(val, 0, 255) << 8) | (old & 0xff);
200
201 case INA209_POWER_WARN:
202 case INA209_POWER_OVER_LIMIT:
203 /* 20 mW LSB */
204 return DIV_ROUND_CLOSEST(val, 20 * 1000);
205 }
206
207 /* Other registers are read-only, return access error */
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
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800233static ssize_t ina209_interval_store(struct device *dev,
234 struct device_attribute *da,
235 const char *buf, size_t count)
Guenter Roeckceeadc52009-08-20 14:49:25 -0700236{
Guenter Roeckceeadc52009-08-20 14:49:25 -0700237 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);
Guenter Roeck89de7342013-09-04 16:55:59 -0700252 i2c_smbus_write_word_swapped(data->client, INA209_CONFIGURATION,
253 regval);
Guenter Roeckceeadc52009-08-20 14:49:25 -0700254 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
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800260static ssize_t ina209_interval_show(struct device *dev,
Guenter Roeckceeadc52009-08-20 14:49:25 -0700261 struct device_attribute *da, char *buf)
262{
Guenter Roeck89de7342013-09-04 16:55:59 -0700263 struct ina209_data *data = dev_get_drvdata(dev);
Guenter Roeckceeadc52009-08-20 14:49:25 -0700264
265 return snprintf(buf, PAGE_SIZE, "%d\n", data->update_interval);
266}
267
268/*
269 * History is reset by writing 1 into bit 0 of the respective peak register.
270 * Since more than one peak register may be affected by the scope of a
271 * reset_history attribute write, use a bit mask in attr->index to identify
272 * which registers are affected.
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
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800282static ssize_t ina209_history_store(struct device *dev,
Guenter Roeckceeadc52009-08-20 14:49:25 -0700283 struct device_attribute *da,
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800284 const char *buf, size_t count)
Guenter Roeckceeadc52009-08-20 14:49:25 -0700285{
Guenter Roeckceeadc52009-08-20 14:49:25 -0700286 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Guenter Roeck89de7342013-09-04 16:55:59 -0700287 struct ina209_data *data = dev_get_drvdata(dev);
288 struct i2c_client *client = data->client;
Guenter Roeckceeadc52009-08-20 14:49:25 -0700289 u32 mask = attr->index;
290 long val;
291 int i, ret;
292
293 ret = kstrtol(buf, 10, &val);
294 if (ret < 0)
295 return ret;
296
297 mutex_lock(&data->update_lock);
298 for (i = 0; i < ARRAY_SIZE(ina209_reset_history_regs); i++) {
299 if (mask & (1 << i))
300 i2c_smbus_write_word_swapped(client,
301 ina209_reset_history_regs[i], 1);
302 }
303 data->valid = false;
304 mutex_unlock(&data->update_lock);
305 return count;
306}
307
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800308static ssize_t ina209_value_store(struct device *dev,
309 struct device_attribute *da,
310 const char *buf, size_t count)
Guenter Roeckceeadc52009-08-20 14:49:25 -0700311{
Guenter Roeckceeadc52009-08-20 14:49:25 -0700312 struct ina209_data *data = ina209_update_device(dev);
313 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
314 int reg = attr->index;
315 long val;
316 int ret;
317
318 if (IS_ERR(data))
319 return PTR_ERR(data);
320
321 ret = kstrtol(buf, 10, &val);
322 if (ret < 0)
323 return ret;
324
325 mutex_lock(&data->update_lock);
326 ret = ina209_to_reg(reg, data->regs[reg], val);
327 if (ret < 0) {
328 count = ret;
329 goto abort;
330 }
Guenter Roeck89de7342013-09-04 16:55:59 -0700331 i2c_smbus_write_word_swapped(data->client, reg, ret);
Guenter Roeckceeadc52009-08-20 14:49:25 -0700332 data->regs[reg] = ret;
333abort:
334 mutex_unlock(&data->update_lock);
335 return count;
336}
337
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800338static ssize_t ina209_value_show(struct device *dev,
339 struct device_attribute *da, char *buf)
Guenter Roeckceeadc52009-08-20 14:49:25 -0700340{
341 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
342 struct ina209_data *data = ina209_update_device(dev);
343 long val;
344
345 if (IS_ERR(data))
346 return PTR_ERR(data);
347
348 val = ina209_from_reg(attr->index, data->regs[attr->index]);
349 return snprintf(buf, PAGE_SIZE, "%ld\n", val);
350}
351
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800352static ssize_t ina209_alarm_show(struct device *dev,
353 struct device_attribute *da, char *buf)
Guenter Roeckceeadc52009-08-20 14:49:25 -0700354{
355 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
356 struct ina209_data *data = ina209_update_device(dev);
357 const unsigned int mask = attr->index;
358 u16 status;
359
360 if (IS_ERR(data))
361 return PTR_ERR(data);
362
363 status = data->regs[INA209_STATUS];
364
365 /*
366 * All alarms are in the INA209_STATUS register. To avoid a long
367 * switch statement, the mask is passed in attr->index
368 */
369 return snprintf(buf, PAGE_SIZE, "%u\n", !!(status & mask));
370}
371
372/* Shunt voltage, history, limits, alarms */
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800373static SENSOR_DEVICE_ATTR_RO(in0_input, ina209_value, INA209_SHUNT_VOLTAGE);
374static SENSOR_DEVICE_ATTR_RO(in0_input_highest, ina209_value,
375 INA209_SHUNT_VOLTAGE_POS_PEAK);
376static SENSOR_DEVICE_ATTR_RO(in0_input_lowest, ina209_value,
377 INA209_SHUNT_VOLTAGE_NEG_PEAK);
378static SENSOR_DEVICE_ATTR_WO(in0_reset_history, ina209_history,
379 (1 << 0) | (1 << 1));
380static SENSOR_DEVICE_ATTR_RW(in0_max, ina209_value,
381 INA209_SHUNT_VOLTAGE_POS_WARN);
382static SENSOR_DEVICE_ATTR_RW(in0_min, ina209_value,
383 INA209_SHUNT_VOLTAGE_NEG_WARN);
384static SENSOR_DEVICE_ATTR_RW(in0_crit_max, ina209_value,
385 INA209_CRITICAL_DAC_POS);
386static SENSOR_DEVICE_ATTR_RW(in0_crit_min, ina209_value,
387 INA209_CRITICAL_DAC_NEG);
Guenter Roeckceeadc52009-08-20 14:49:25 -0700388
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800389static SENSOR_DEVICE_ATTR_RO(in0_min_alarm, ina209_alarm, 1 << 11);
390static SENSOR_DEVICE_ATTR_RO(in0_max_alarm, ina209_alarm, 1 << 12);
391static SENSOR_DEVICE_ATTR_RO(in0_crit_min_alarm, ina209_alarm, 1 << 6);
392static SENSOR_DEVICE_ATTR_RO(in0_crit_max_alarm, ina209_alarm, 1 << 7);
Guenter Roeckceeadc52009-08-20 14:49:25 -0700393
394/* Bus voltage, history, limits, alarms */
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800395static SENSOR_DEVICE_ATTR_RO(in1_input, ina209_value, INA209_BUS_VOLTAGE);
396static SENSOR_DEVICE_ATTR_RO(in1_input_highest, ina209_value,
397 INA209_BUS_VOLTAGE_MAX_PEAK);
398static SENSOR_DEVICE_ATTR_RO(in1_input_lowest, ina209_value,
399 INA209_BUS_VOLTAGE_MIN_PEAK);
400static SENSOR_DEVICE_ATTR_WO(in1_reset_history, ina209_history,
401 (1 << 2) | (1 << 3));
402static SENSOR_DEVICE_ATTR_RW(in1_max, ina209_value,
403 INA209_BUS_VOLTAGE_OVER_WARN);
404static SENSOR_DEVICE_ATTR_RW(in1_min, ina209_value,
405 INA209_BUS_VOLTAGE_UNDER_WARN);
406static SENSOR_DEVICE_ATTR_RW(in1_crit_max, ina209_value,
407 INA209_BUS_VOLTAGE_OVER_LIMIT);
408static SENSOR_DEVICE_ATTR_RW(in1_crit_min, ina209_value,
409 INA209_BUS_VOLTAGE_UNDER_LIMIT);
Guenter Roeckceeadc52009-08-20 14:49:25 -0700410
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800411static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ina209_alarm, 1 << 14);
412static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ina209_alarm, 1 << 15);
413static SENSOR_DEVICE_ATTR_RO(in1_crit_min_alarm, ina209_alarm, 1 << 9);
414static SENSOR_DEVICE_ATTR_RO(in1_crit_max_alarm, ina209_alarm, 1 << 10);
Guenter Roeckceeadc52009-08-20 14:49:25 -0700415
416/* Power */
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800417static SENSOR_DEVICE_ATTR_RO(power1_input, ina209_value, INA209_POWER);
418static SENSOR_DEVICE_ATTR_RO(power1_input_highest, ina209_value,
419 INA209_POWER_PEAK);
420static SENSOR_DEVICE_ATTR_WO(power1_reset_history, ina209_history, 1 << 4);
421static SENSOR_DEVICE_ATTR_RW(power1_max, ina209_value, INA209_POWER_WARN);
422static SENSOR_DEVICE_ATTR_RW(power1_crit, ina209_value,
423 INA209_POWER_OVER_LIMIT);
Guenter Roeckceeadc52009-08-20 14:49:25 -0700424
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800425static SENSOR_DEVICE_ATTR_RO(power1_max_alarm, ina209_alarm, 1 << 13);
426static SENSOR_DEVICE_ATTR_RO(power1_crit_alarm, ina209_alarm, 1 << 8);
Guenter Roeckceeadc52009-08-20 14:49:25 -0700427
428/* Current */
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800429static SENSOR_DEVICE_ATTR_RO(curr1_input, ina209_value, INA209_CURRENT);
Guenter Roeckceeadc52009-08-20 14:49:25 -0700430
Guenter Roeck46dce7a2018-12-10 14:02:10 -0800431static SENSOR_DEVICE_ATTR_RW(update_interval, ina209_interval, 0);
Guenter Roeckceeadc52009-08-20 14:49:25 -0700432
433/*
434 * Finally, construct an array of pointers to members of the above objects,
435 * as required for sysfs_create_group()
436 */
Guenter Roeck89de7342013-09-04 16:55:59 -0700437static struct attribute *ina209_attrs[] = {
Guenter Roeckceeadc52009-08-20 14:49:25 -0700438 &sensor_dev_attr_in0_input.dev_attr.attr,
439 &sensor_dev_attr_in0_input_highest.dev_attr.attr,
440 &sensor_dev_attr_in0_input_lowest.dev_attr.attr,
441 &sensor_dev_attr_in0_reset_history.dev_attr.attr,
442 &sensor_dev_attr_in0_max.dev_attr.attr,
443 &sensor_dev_attr_in0_min.dev_attr.attr,
444 &sensor_dev_attr_in0_crit_max.dev_attr.attr,
445 &sensor_dev_attr_in0_crit_min.dev_attr.attr,
446 &sensor_dev_attr_in0_max_alarm.dev_attr.attr,
447 &sensor_dev_attr_in0_min_alarm.dev_attr.attr,
448 &sensor_dev_attr_in0_crit_max_alarm.dev_attr.attr,
449 &sensor_dev_attr_in0_crit_min_alarm.dev_attr.attr,
450
451 &sensor_dev_attr_in1_input.dev_attr.attr,
452 &sensor_dev_attr_in1_input_highest.dev_attr.attr,
453 &sensor_dev_attr_in1_input_lowest.dev_attr.attr,
454 &sensor_dev_attr_in1_reset_history.dev_attr.attr,
455 &sensor_dev_attr_in1_max.dev_attr.attr,
456 &sensor_dev_attr_in1_min.dev_attr.attr,
457 &sensor_dev_attr_in1_crit_max.dev_attr.attr,
458 &sensor_dev_attr_in1_crit_min.dev_attr.attr,
459 &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
460 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
461 &sensor_dev_attr_in1_crit_max_alarm.dev_attr.attr,
462 &sensor_dev_attr_in1_crit_min_alarm.dev_attr.attr,
463
464 &sensor_dev_attr_power1_input.dev_attr.attr,
465 &sensor_dev_attr_power1_input_highest.dev_attr.attr,
466 &sensor_dev_attr_power1_reset_history.dev_attr.attr,
467 &sensor_dev_attr_power1_max.dev_attr.attr,
468 &sensor_dev_attr_power1_crit.dev_attr.attr,
469 &sensor_dev_attr_power1_max_alarm.dev_attr.attr,
470 &sensor_dev_attr_power1_crit_alarm.dev_attr.attr,
471
472 &sensor_dev_attr_curr1_input.dev_attr.attr,
473
474 &sensor_dev_attr_update_interval.dev_attr.attr,
475
476 NULL,
477};
Guenter Roeck89de7342013-09-04 16:55:59 -0700478ATTRIBUTE_GROUPS(ina209);
Guenter Roeckceeadc52009-08-20 14:49:25 -0700479
480static void ina209_restore_conf(struct i2c_client *client,
481 struct ina209_data *data)
482{
483 /* Restore initial configuration */
484 i2c_smbus_write_word_swapped(client, INA209_CONFIGURATION,
485 data->config_orig);
486 i2c_smbus_write_word_swapped(client, INA209_CALIBRATION,
487 data->calibration_orig);
488}
489
490static int ina209_init_client(struct i2c_client *client,
491 struct ina209_data *data)
492{
493 struct ina2xx_platform_data *pdata = dev_get_platdata(&client->dev);
494 u32 shunt;
495 int reg;
496
497 reg = i2c_smbus_read_word_swapped(client, INA209_CALIBRATION);
498 if (reg < 0)
499 return reg;
500 data->calibration_orig = reg;
501
502 reg = i2c_smbus_read_word_swapped(client, INA209_CONFIGURATION);
503 if (reg < 0)
504 return reg;
505 data->config_orig = reg;
506
507 if (pdata) {
508 if (pdata->shunt_uohms <= 0)
509 return -EINVAL;
510 shunt = pdata->shunt_uohms;
511 } else if (!of_property_read_u32(client->dev.of_node, "shunt-resistor",
512 &shunt)) {
513 if (shunt == 0)
514 return -EINVAL;
515 } else {
516 shunt = data->calibration_orig ?
517 40960000 / data->calibration_orig : INA209_SHUNT_DEFAULT;
518 }
519
520 i2c_smbus_write_word_swapped(client, INA209_CONFIGURATION,
521 INA209_CONFIG_DEFAULT);
522 data->update_interval = ina209_interval_from_reg(INA209_CONFIG_DEFAULT);
523
524 /*
525 * Calibrate current LSB to 1mA. Shunt is in uOhms.
526 * See equation 13 in datasheet.
527 */
528 i2c_smbus_write_word_swapped(client, INA209_CALIBRATION,
529 clamp_val(40960000 / shunt, 1, 65535));
530
531 /* Clear status register */
532 i2c_smbus_read_word_swapped(client, INA209_STATUS);
533
534 return 0;
535}
536
537static int ina209_probe(struct i2c_client *client,
538 const struct i2c_device_id *id)
539{
540 struct i2c_adapter *adapter = client->adapter;
541 struct ina209_data *data;
Guenter Roeck89de7342013-09-04 16:55:59 -0700542 struct device *hwmon_dev;
Guenter Roeckceeadc52009-08-20 14:49:25 -0700543 int ret;
544
545 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
546 return -ENODEV;
547
548 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
549 if (!data)
550 return -ENOMEM;
551
552 i2c_set_clientdata(client, data);
Guenter Roeck89de7342013-09-04 16:55:59 -0700553 data->client = client;
Guenter Roeckceeadc52009-08-20 14:49:25 -0700554 mutex_init(&data->update_lock);
555
556 ret = ina209_init_client(client, data);
557 if (ret)
558 return ret;
559
Guenter Roeck89de7342013-09-04 16:55:59 -0700560 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
561 client->name,
562 data, ina209_groups);
563 if (IS_ERR(hwmon_dev)) {
564 ret = PTR_ERR(hwmon_dev);
Guenter Roeckceeadc52009-08-20 14:49:25 -0700565 goto out_restore_conf;
Guenter Roeckceeadc52009-08-20 14:49:25 -0700566 }
567
568 return 0;
569
Guenter Roeckceeadc52009-08-20 14:49:25 -0700570out_restore_conf:
571 ina209_restore_conf(client, data);
572 return ret;
573}
574
575static int ina209_remove(struct i2c_client *client)
576{
577 struct ina209_data *data = i2c_get_clientdata(client);
578
Guenter Roeckceeadc52009-08-20 14:49:25 -0700579 ina209_restore_conf(client, data);
580
581 return 0;
582}
583
584static const struct i2c_device_id ina209_id[] = {
585 { "ina209", 0 },
586 { }
587};
588MODULE_DEVICE_TABLE(i2c, ina209_id);
589
Guenter Roeck32c2d402019-04-04 06:41:19 -0700590static const struct of_device_id __maybe_unused ina209_of_match[] = {
Javier Martinez Canillas0b7abe52017-02-24 10:12:59 -0300591 { .compatible = "ti,ina209" },
592 { },
593};
594MODULE_DEVICE_TABLE(of, ina209_of_match);
595
Guenter Roeckceeadc52009-08-20 14:49:25 -0700596/* This is the driver that will be inserted */
597static struct i2c_driver ina209_driver = {
598 .class = I2C_CLASS_HWMON,
599 .driver = {
600 .name = "ina209",
Javier Martinez Canillas0b7abe52017-02-24 10:12:59 -0300601 .of_match_table = of_match_ptr(ina209_of_match),
Guenter Roeckceeadc52009-08-20 14:49:25 -0700602 },
603 .probe = ina209_probe,
604 .remove = ina209_remove,
605 .id_table = ina209_id,
606};
607
608module_i2c_driver(ina209_driver);
609
610MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>, Paul Hays <Paul.Hays@cattail.ca>, Guenter Roeck <linux@roeck-us.net>");
611MODULE_DESCRIPTION("INA209 driver");
612MODULE_LICENSE("GPL");