blob: 62e38fa8cda23c790635664364ba90e6d8292414 [file] [log] [blame]
Felten, Lotharf7c2fe32012-05-12 04:36:38 -04001/*
2 * Driver for Texas Instruments INA219, INA226 power monitor chips
3 *
4 * INA219:
5 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
6 * Datasheet: http://www.ti.com/product/ina219
7 *
Guenter Roeckdc92cd02012-05-12 11:33:11 -07008 * INA220:
9 * Bi-Directional Current/Power Monitor with I2C Interface
10 * Datasheet: http://www.ti.com/product/ina220
11 *
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040012 * INA226:
13 * Bi-Directional Current/Power Monitor with I2C Interface
14 * Datasheet: http://www.ti.com/product/ina226
15 *
Guenter Roeckdc92cd02012-05-12 11:33:11 -070016 * INA230:
17 * Bi-directional Current/Power Monitor with I2C Interface
18 * Datasheet: http://www.ti.com/product/ina230
19 *
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040020 * Copyright (C) 2012 Lothar Felten <l-felten@ti.com>
21 * Thanks to Jan Volkering
22 *
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; version 2 of the License.
26 */
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/err.h>
32#include <linux/slab.h>
33#include <linux/i2c.h>
34#include <linux/hwmon.h>
35#include <linux/hwmon-sysfs.h>
Jean Delvaredcd8f392012-10-10 15:25:56 +020036#include <linux/jiffies.h>
Javier Martinez Canillasbd0ddd42017-02-24 10:13:00 -030037#include <linux/of_device.h>
Tang Yuantian31e7ad72013-06-19 14:50:20 +080038#include <linux/of.h>
Bartosz Golaszewski509416a2015-01-05 15:20:52 +010039#include <linux/delay.h>
Bartosz Golaszewskid38df342015-04-16 12:43:34 -070040#include <linux/util_macros.h>
Marc Titingera0de56c2015-10-28 12:04:53 +010041#include <linux/regmap.h>
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040042
43#include <linux/platform_data/ina2xx.h>
44
45/* common register definitions */
46#define INA2XX_CONFIG 0x00
47#define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
48#define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
49#define INA2XX_POWER 0x03 /* readonly */
50#define INA2XX_CURRENT 0x04 /* readonly */
51#define INA2XX_CALIBRATION 0x05
52
53/* INA226 register definitions */
54#define INA226_MASK_ENABLE 0x06
55#define INA226_ALERT_LIMIT 0x07
56#define INA226_DIE_ID 0xFF
57
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040058/* register count */
59#define INA219_REGISTERS 6
60#define INA226_REGISTERS 8
61
62#define INA2XX_MAX_REGISTERS 8
63
64/* settings - depend on use case */
65#define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
66#define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
67
68/* worst case is 68.10 ms (~14.6Hz, ina219) */
69#define INA2XX_CONVERSION_RATE 15
Bartosz Golaszewski509416a2015-01-05 15:20:52 +010070#define INA2XX_MAX_DELAY 69 /* worst case delay in ms */
71
72#define INA2XX_RSHUNT_DEFAULT 10000
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040073
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +010074/* bit mask for reading the averaging setting in the configuration register */
75#define INA226_AVG_RD_MASK 0x0E00
76
77#define INA226_READ_AVG(reg) (((reg) & INA226_AVG_RD_MASK) >> 9)
78#define INA226_SHIFT_AVG(val) ((val) << 9)
79
80/* common attrs, ina226 attrs and NULL */
81#define INA2XX_MAX_ATTRIBUTE_GROUPS 3
82
83/*
84 * Both bus voltage and shunt voltage conversion times for ina226 are set
85 * to 0b0100 on POR, which translates to 2200 microseconds in total.
86 */
87#define INA226_TOTAL_CONV_TIME_DEFAULT 2200
88
Marc Titingera0de56c2015-10-28 12:04:53 +010089static struct regmap_config ina2xx_regmap_config = {
90 .reg_bits = 8,
91 .val_bits = 16,
92};
93
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040094enum ina2xx_ids { ina219, ina226 };
95
Guenter Roeck6106db22012-05-12 11:21:01 -070096struct ina2xx_config {
97 u16 config_default;
98 int calibration_factor;
99 int registers;
100 int shunt_div;
101 int bus_voltage_shift;
102 int bus_voltage_lsb; /* uV */
103 int power_lsb; /* uW */
104};
105
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400106struct ina2xx_data {
Guenter Roeck6106db22012-05-12 11:21:01 -0700107 const struct ina2xx_config *config;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400108
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100109 long rshunt;
Marc Titingera0de56c2015-10-28 12:04:53 +0100110 struct mutex config_lock;
111 struct regmap *regmap;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400112
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100113 const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400114};
115
Guenter Roeck6106db22012-05-12 11:21:01 -0700116static const struct ina2xx_config ina2xx_config[] = {
117 [ina219] = {
118 .config_default = INA219_CONFIG_DEFAULT,
119 .calibration_factor = 40960000,
120 .registers = INA219_REGISTERS,
121 .shunt_div = 100,
122 .bus_voltage_shift = 3,
123 .bus_voltage_lsb = 4000,
124 .power_lsb = 20000,
125 },
126 [ina226] = {
127 .config_default = INA226_CONFIG_DEFAULT,
128 .calibration_factor = 5120000,
129 .registers = INA226_REGISTERS,
130 .shunt_div = 400,
131 .bus_voltage_shift = 0,
132 .bus_voltage_lsb = 1250,
133 .power_lsb = 25000,
134 },
135};
136
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100137/*
138 * Available averaging rates for ina226. The indices correspond with
139 * the bit values expected by the chip (according to the ina226 datasheet,
140 * table 3 AVG bit settings, found at
141 * http://www.ti.com/lit/ds/symlink/ina226.pdf.
142 */
143static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
144
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100145static int ina226_reg_to_interval(u16 config)
146{
147 int avg = ina226_avg_tab[INA226_READ_AVG(config)];
148
149 /*
150 * Multiply the total conversion time by the number of averages.
151 * Return the result in milliseconds.
152 */
153 return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
154}
155
Marc Titingera0de56c2015-10-28 12:04:53 +0100156/*
157 * Return the new, shifted AVG field value of CONFIG register,
158 * to use with regmap_update_bits
159 */
160static u16 ina226_interval_to_reg(int interval)
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100161{
162 int avg, avg_bits;
163
164 avg = DIV_ROUND_CLOSEST(interval * 1000,
165 INA226_TOTAL_CONV_TIME_DEFAULT);
Bartosz Golaszewskid38df342015-04-16 12:43:34 -0700166 avg_bits = find_closest(avg, ina226_avg_tab,
167 ARRAY_SIZE(ina226_avg_tab));
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100168
Marc Titingera0de56c2015-10-28 12:04:53 +0100169 return INA226_SHIFT_AVG(avg_bits);
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100170}
171
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100172static int ina2xx_calibrate(struct ina2xx_data *data)
173{
Bartosz Golaszewskib721fe22015-01-12 14:47:22 +0100174 u16 val = DIV_ROUND_CLOSEST(data->config->calibration_factor,
175 data->rshunt);
176
Marc Titingera0de56c2015-10-28 12:04:53 +0100177 return regmap_write(data->regmap, INA2XX_CALIBRATION, val);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100178}
179
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100180/*
181 * Initialize the configuration and calibration registers.
182 */
183static int ina2xx_init(struct ina2xx_data *data)
184{
Marc Titingera0de56c2015-10-28 12:04:53 +0100185 int ret = regmap_write(data->regmap, INA2XX_CONFIG,
186 data->config->config_default);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100187 if (ret < 0)
188 return ret;
189
190 /*
191 * Set current LSB to 1mA, shunt is in uOhms
192 * (equation 13 in datasheet).
193 */
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100194 return ina2xx_calibrate(data);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100195}
196
Marc Titingera0de56c2015-10-28 12:04:53 +0100197static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400198{
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700199 struct ina2xx_data *data = dev_get_drvdata(dev);
Marc Titingera0de56c2015-10-28 12:04:53 +0100200 int ret, retry;
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100201
Marc Titingera0de56c2015-10-28 12:04:53 +0100202 dev_dbg(dev, "Starting register %d read\n", reg);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100203
204 for (retry = 5; retry; retry--) {
Marc Titingera0de56c2015-10-28 12:04:53 +0100205
206 ret = regmap_read(data->regmap, reg, regval);
207 if (ret < 0)
208 return ret;
209
210 dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100211
212 /*
213 * If the current value in the calibration register is 0, the
214 * power and current registers will also remain at 0. In case
215 * the chip has been reset let's check the calibration
216 * register and reinitialize if needed.
Marc Titingera0de56c2015-10-28 12:04:53 +0100217 * We do that extra read of the calibration register if there
218 * is some hint of a chip reset.
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100219 */
Marc Titingera0de56c2015-10-28 12:04:53 +0100220 if (*regval == 0) {
221 unsigned int cal;
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100222
Marc Titingera0de56c2015-10-28 12:04:53 +0100223 ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
224 &cal);
225 if (ret < 0)
226 return ret;
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100227
Marc Titingera0de56c2015-10-28 12:04:53 +0100228 if (cal == 0) {
229 dev_warn(dev, "chip not calibrated, reinitializing\n");
230
231 ret = ina2xx_init(data);
232 if (ret < 0)
233 return ret;
234 /*
235 * Let's make sure the power and current
236 * registers have been updated before trying
237 * again.
238 */
239 msleep(INA2XX_MAX_DELAY);
240 continue;
241 }
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100242 }
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100243 return 0;
244 }
245
246 /*
247 * If we're here then although all write operations succeeded, the
248 * chip still returns 0 in the calibration register. Nothing more we
249 * can do here.
250 */
251 dev_err(dev, "unable to reinitialize the chip\n");
252 return -ENODEV;
253}
254
Marc Titingera0de56c2015-10-28 12:04:53 +0100255static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
256 unsigned int regval)
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400257{
Guenter Roeck6106db22012-05-12 11:21:01 -0700258 int val;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400259
260 switch (reg) {
261 case INA2XX_SHUNT_VOLTAGE:
Fabio Baltieric0214f92014-06-08 22:06:24 +0100262 /* signed register */
Marc Titingera0de56c2015-10-28 12:04:53 +0100263 val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400264 break;
265 case INA2XX_BUS_VOLTAGE:
Marc Titingera0de56c2015-10-28 12:04:53 +0100266 val = (regval >> data->config->bus_voltage_shift)
Guenter Roeck6106db22012-05-12 11:21:01 -0700267 * data->config->bus_voltage_lsb;
268 val = DIV_ROUND_CLOSEST(val, 1000);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400269 break;
270 case INA2XX_POWER:
Marc Titingera0de56c2015-10-28 12:04:53 +0100271 val = regval * data->config->power_lsb;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400272 break;
273 case INA2XX_CURRENT:
Fabio Baltieric0214f92014-06-08 22:06:24 +0100274 /* signed register, LSB=1mA (selected), in mA */
Marc Titingera0de56c2015-10-28 12:04:53 +0100275 val = (s16)regval;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400276 break;
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100277 case INA2XX_CALIBRATION:
Bartosz Golaszewskib721fe22015-01-12 14:47:22 +0100278 val = DIV_ROUND_CLOSEST(data->config->calibration_factor,
Marc Titingera0de56c2015-10-28 12:04:53 +0100279 regval);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100280 break;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400281 default:
282 /* programmer goofed */
283 WARN_ON_ONCE(1);
284 val = 0;
285 break;
286 }
287
288 return val;
289}
290
291static ssize_t ina2xx_show_value(struct device *dev,
292 struct device_attribute *da, char *buf)
293{
294 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Marc Titingera0de56c2015-10-28 12:04:53 +0100295 struct ina2xx_data *data = dev_get_drvdata(dev);
296 unsigned int regval;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400297
Marc Titingera0de56c2015-10-28 12:04:53 +0100298 int err = ina2xx_read_reg(dev, attr->index, &regval);
299
300 if (err < 0)
301 return err;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400302
Guenter Roeck6106db22012-05-12 11:21:01 -0700303 return snprintf(buf, PAGE_SIZE, "%d\n",
Marc Titingera0de56c2015-10-28 12:04:53 +0100304 ina2xx_get_value(data, attr->index, regval));
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400305}
306
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100307static ssize_t ina2xx_set_shunt(struct device *dev,
308 struct device_attribute *da,
309 const char *buf, size_t count)
310{
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100311 unsigned long val;
312 int status;
Marc Titingera0de56c2015-10-28 12:04:53 +0100313 struct ina2xx_data *data = dev_get_drvdata(dev);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100314
315 status = kstrtoul(buf, 10, &val);
316 if (status < 0)
317 return status;
318
319 if (val == 0 ||
320 /* Values greater than the calibration factor make no sense. */
321 val > data->config->calibration_factor)
322 return -EINVAL;
323
Marc Titingera0de56c2015-10-28 12:04:53 +0100324 mutex_lock(&data->config_lock);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100325 data->rshunt = val;
326 status = ina2xx_calibrate(data);
Marc Titingera0de56c2015-10-28 12:04:53 +0100327 mutex_unlock(&data->config_lock);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100328 if (status < 0)
329 return status;
330
331 return count;
332}
333
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100334static ssize_t ina226_set_interval(struct device *dev,
335 struct device_attribute *da,
336 const char *buf, size_t count)
337{
338 struct ina2xx_data *data = dev_get_drvdata(dev);
339 unsigned long val;
340 int status;
341
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100342 status = kstrtoul(buf, 10, &val);
343 if (status < 0)
344 return status;
345
346 if (val > INT_MAX || val == 0)
347 return -EINVAL;
348
Marc Titingera0de56c2015-10-28 12:04:53 +0100349 status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
350 INA226_AVG_RD_MASK,
351 ina226_interval_to_reg(val));
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100352 if (status < 0)
353 return status;
354
355 return count;
356}
357
358static ssize_t ina226_show_interval(struct device *dev,
359 struct device_attribute *da, char *buf)
360{
Marc Titingera0de56c2015-10-28 12:04:53 +0100361 struct ina2xx_data *data = dev_get_drvdata(dev);
362 int status;
363 unsigned int regval;
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100364
Marc Titingera0de56c2015-10-28 12:04:53 +0100365 status = regmap_read(data->regmap, INA2XX_CONFIG, &regval);
366 if (status)
367 return status;
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100368
Marc Titingera0de56c2015-10-28 12:04:53 +0100369 return snprintf(buf, PAGE_SIZE, "%d\n", ina226_reg_to_interval(regval));
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100370}
371
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400372/* shunt voltage */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800373static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL,
374 INA2XX_SHUNT_VOLTAGE);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400375
376/* bus voltage */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800377static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL,
378 INA2XX_BUS_VOLTAGE);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400379
380/* calculated current */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800381static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL,
382 INA2XX_CURRENT);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400383
384/* calculated power */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800385static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL,
386 INA2XX_POWER);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400387
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100388/* shunt resistance */
389static SENSOR_DEVICE_ATTR(shunt_resistor, S_IRUGO | S_IWUSR,
390 ina2xx_show_value, ina2xx_set_shunt,
391 INA2XX_CALIBRATION);
392
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100393/* update interval (ina226 only) */
394static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR,
395 ina226_show_interval, ina226_set_interval, 0);
396
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400397/* pointers to created device attributes */
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700398static struct attribute *ina2xx_attrs[] = {
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400399 &sensor_dev_attr_in0_input.dev_attr.attr,
400 &sensor_dev_attr_in1_input.dev_attr.attr,
401 &sensor_dev_attr_curr1_input.dev_attr.attr,
402 &sensor_dev_attr_power1_input.dev_attr.attr,
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100403 &sensor_dev_attr_shunt_resistor.dev_attr.attr,
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400404 NULL,
405};
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100406
407static const struct attribute_group ina2xx_group = {
408 .attrs = ina2xx_attrs,
409};
410
411static struct attribute *ina226_attrs[] = {
412 &sensor_dev_attr_update_interval.dev_attr.attr,
413 NULL,
414};
415
416static const struct attribute_group ina226_group = {
417 .attrs = ina226_attrs,
418};
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400419
420static int ina2xx_probe(struct i2c_client *client,
421 const struct i2c_device_id *id)
422{
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700423 struct device *dev = &client->dev;
424 struct ina2xx_data *data;
425 struct device *hwmon_dev;
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700426 u32 val;
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100427 int ret, group = 0;
Javier Martinez Canillasbd0ddd42017-02-24 10:13:00 -0300428 enum ina2xx_ids chip;
429
430 if (client->dev.of_node)
431 chip = (enum ina2xx_ids)of_device_get_match_data(&client->dev);
432 else
433 chip = id->driver_data;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400434
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700435 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400436 if (!data)
437 return -ENOMEM;
438
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400439 /* set the device type */
Javier Martinez Canillasbd0ddd42017-02-24 10:13:00 -0300440 data->config = &ina2xx_config[chip];
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100441
Marc Titinger001e2e72015-10-27 10:51:08 +0100442 if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
443 struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
444
445 if (pdata)
446 val = pdata->shunt_uohms;
447 else
448 val = INA2XX_RSHUNT_DEFAULT;
449 }
450
451 if (val <= 0 || val > data->config->calibration_factor)
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100452 return -ENODEV;
453
Marc Titinger001e2e72015-10-27 10:51:08 +0100454 data->rshunt = val;
455
Marc Titingera0de56c2015-10-28 12:04:53 +0100456 ina2xx_regmap_config.max_register = data->config->registers;
457
458 data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
459 if (IS_ERR(data->regmap)) {
460 dev_err(dev, "failed to allocate register map\n");
461 return PTR_ERR(data->regmap);
462 }
463
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100464 ret = ina2xx_init(data);
465 if (ret < 0) {
466 dev_err(dev, "error configuring the device: %d\n", ret);
467 return -ENODEV;
468 }
469
Marc Titingera0de56c2015-10-28 12:04:53 +0100470 mutex_init(&data->config_lock);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400471
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100472 data->groups[group++] = &ina2xx_group;
Marc Titinger5aa4e832015-10-29 10:07:17 +0100473 if (id->driver_data == ina226)
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100474 data->groups[group++] = &ina226_group;
475
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700476 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100477 data, data->groups);
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700478 if (IS_ERR(hwmon_dev))
479 return PTR_ERR(hwmon_dev);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400480
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700481 dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100482 id->name, data->rshunt);
Guenter Roeck6106db22012-05-12 11:21:01 -0700483
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400484 return 0;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400485}
486
487static const struct i2c_device_id ina2xx_id[] = {
488 { "ina219", ina219 },
Guenter Roeckdc92cd02012-05-12 11:33:11 -0700489 { "ina220", ina219 },
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400490 { "ina226", ina226 },
Guenter Roeckdc92cd02012-05-12 11:33:11 -0700491 { "ina230", ina226 },
Kevin Hilmanadd513b2015-01-14 17:34:58 -0800492 { "ina231", ina226 },
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400493 { }
494};
495MODULE_DEVICE_TABLE(i2c, ina2xx_id);
496
Javier Martinez Canillasbd0ddd42017-02-24 10:13:00 -0300497static const struct of_device_id ina2xx_of_match[] = {
498 {
499 .compatible = "ti,ina219",
500 .data = (void *)ina219
501 },
502 {
503 .compatible = "ti,ina220",
504 .data = (void *)ina219
505 },
506 {
507 .compatible = "ti,ina226",
508 .data = (void *)ina226
509 },
510 {
511 .compatible = "ti,ina230",
512 .data = (void *)ina226
513 },
514 {
515 .compatible = "ti,ina231",
516 .data = (void *)ina226
517 },
518 { },
519};
520MODULE_DEVICE_TABLE(of, ina2xx_of_match);
521
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400522static struct i2c_driver ina2xx_driver = {
523 .driver = {
524 .name = "ina2xx",
Javier Martinez Canillasbd0ddd42017-02-24 10:13:00 -0300525 .of_match_table = of_match_ptr(ina2xx_of_match),
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400526 },
527 .probe = ina2xx_probe,
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400528 .id_table = ina2xx_id,
529};
530
Wei Yongjund835ca02012-10-08 20:40:35 +0800531module_i2c_driver(ina2xx_driver);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400532
533MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
534MODULE_DESCRIPTION("ina2xx driver");
535MODULE_LICENSE("GPL");