blob: b24f1d3045f04386f7dc3a1c5af4b7ee7724e891 [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>
Tang Yuantian31e7ad72013-06-19 14:50:20 +080037#include <linux/of.h>
Bartosz Golaszewski509416a2015-01-05 15:20:52 +010038#include <linux/delay.h>
Bartosz Golaszewskid38df342015-04-16 12:43:34 -070039#include <linux/util_macros.h>
Marc Titingera0de56c2015-10-28 12:04:53 +010040#include <linux/regmap.h>
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040041
42#include <linux/platform_data/ina2xx.h>
43
44/* common register definitions */
45#define INA2XX_CONFIG 0x00
46#define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
47#define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
48#define INA2XX_POWER 0x03 /* readonly */
49#define INA2XX_CURRENT 0x04 /* readonly */
50#define INA2XX_CALIBRATION 0x05
51
52/* INA226 register definitions */
53#define INA226_MASK_ENABLE 0x06
54#define INA226_ALERT_LIMIT 0x07
55#define INA226_DIE_ID 0xFF
56
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040057/* register count */
58#define INA219_REGISTERS 6
59#define INA226_REGISTERS 8
60
61#define INA2XX_MAX_REGISTERS 8
62
63/* settings - depend on use case */
64#define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
65#define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
66
67/* worst case is 68.10 ms (~14.6Hz, ina219) */
68#define INA2XX_CONVERSION_RATE 15
Bartosz Golaszewski509416a2015-01-05 15:20:52 +010069#define INA2XX_MAX_DELAY 69 /* worst case delay in ms */
70
71#define INA2XX_RSHUNT_DEFAULT 10000
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040072
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +010073/* bit mask for reading the averaging setting in the configuration register */
74#define INA226_AVG_RD_MASK 0x0E00
75
76#define INA226_READ_AVG(reg) (((reg) & INA226_AVG_RD_MASK) >> 9)
77#define INA226_SHIFT_AVG(val) ((val) << 9)
78
79/* common attrs, ina226 attrs and NULL */
80#define INA2XX_MAX_ATTRIBUTE_GROUPS 3
81
82/*
83 * Both bus voltage and shunt voltage conversion times for ina226 are set
84 * to 0b0100 on POR, which translates to 2200 microseconds in total.
85 */
86#define INA226_TOTAL_CONV_TIME_DEFAULT 2200
87
Marc Titingera0de56c2015-10-28 12:04:53 +010088static struct regmap_config ina2xx_regmap_config = {
89 .reg_bits = 8,
90 .val_bits = 16,
91};
92
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040093enum ina2xx_ids { ina219, ina226 };
94
Guenter Roeck6106db22012-05-12 11:21:01 -070095struct ina2xx_config {
96 u16 config_default;
97 int calibration_factor;
98 int registers;
99 int shunt_div;
100 int bus_voltage_shift;
101 int bus_voltage_lsb; /* uV */
102 int power_lsb; /* uW */
103};
104
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400105struct ina2xx_data {
Guenter Roeck6106db22012-05-12 11:21:01 -0700106 const struct ina2xx_config *config;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400107
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100108 long rshunt;
Marc Titingera0de56c2015-10-28 12:04:53 +0100109 struct mutex config_lock;
110 struct regmap *regmap;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400111
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100112 const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400113};
114
Guenter Roeck6106db22012-05-12 11:21:01 -0700115static const struct ina2xx_config ina2xx_config[] = {
116 [ina219] = {
117 .config_default = INA219_CONFIG_DEFAULT,
118 .calibration_factor = 40960000,
119 .registers = INA219_REGISTERS,
120 .shunt_div = 100,
121 .bus_voltage_shift = 3,
122 .bus_voltage_lsb = 4000,
123 .power_lsb = 20000,
124 },
125 [ina226] = {
126 .config_default = INA226_CONFIG_DEFAULT,
127 .calibration_factor = 5120000,
128 .registers = INA226_REGISTERS,
129 .shunt_div = 400,
130 .bus_voltage_shift = 0,
131 .bus_voltage_lsb = 1250,
132 .power_lsb = 25000,
133 },
134};
135
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100136/*
137 * Available averaging rates for ina226. The indices correspond with
138 * the bit values expected by the chip (according to the ina226 datasheet,
139 * table 3 AVG bit settings, found at
140 * http://www.ti.com/lit/ds/symlink/ina226.pdf.
141 */
142static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
143
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100144static int ina226_reg_to_interval(u16 config)
145{
146 int avg = ina226_avg_tab[INA226_READ_AVG(config)];
147
148 /*
149 * Multiply the total conversion time by the number of averages.
150 * Return the result in milliseconds.
151 */
152 return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
153}
154
Marc Titingera0de56c2015-10-28 12:04:53 +0100155/*
156 * Return the new, shifted AVG field value of CONFIG register,
157 * to use with regmap_update_bits
158 */
159static u16 ina226_interval_to_reg(int interval)
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100160{
161 int avg, avg_bits;
162
163 avg = DIV_ROUND_CLOSEST(interval * 1000,
164 INA226_TOTAL_CONV_TIME_DEFAULT);
Bartosz Golaszewskid38df342015-04-16 12:43:34 -0700165 avg_bits = find_closest(avg, ina226_avg_tab,
166 ARRAY_SIZE(ina226_avg_tab));
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100167
Marc Titingera0de56c2015-10-28 12:04:53 +0100168 return INA226_SHIFT_AVG(avg_bits);
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100169}
170
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100171static int ina2xx_calibrate(struct ina2xx_data *data)
172{
Bartosz Golaszewskib721fe22015-01-12 14:47:22 +0100173 u16 val = DIV_ROUND_CLOSEST(data->config->calibration_factor,
174 data->rshunt);
175
Marc Titingera0de56c2015-10-28 12:04:53 +0100176 return regmap_write(data->regmap, INA2XX_CALIBRATION, val);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100177}
178
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100179/*
180 * Initialize the configuration and calibration registers.
181 */
182static int ina2xx_init(struct ina2xx_data *data)
183{
Marc Titingera0de56c2015-10-28 12:04:53 +0100184 int ret = regmap_write(data->regmap, INA2XX_CONFIG,
185 data->config->config_default);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100186 if (ret < 0)
187 return ret;
188
189 /*
190 * Set current LSB to 1mA, shunt is in uOhms
191 * (equation 13 in datasheet).
192 */
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100193 return ina2xx_calibrate(data);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100194}
195
Marc Titingera0de56c2015-10-28 12:04:53 +0100196static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400197{
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700198 struct ina2xx_data *data = dev_get_drvdata(dev);
Marc Titingera0de56c2015-10-28 12:04:53 +0100199 int ret, retry;
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100200
Marc Titingera0de56c2015-10-28 12:04:53 +0100201 dev_dbg(dev, "Starting register %d read\n", reg);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100202
203 for (retry = 5; retry; retry--) {
Marc Titingera0de56c2015-10-28 12:04:53 +0100204
205 ret = regmap_read(data->regmap, reg, regval);
206 if (ret < 0)
207 return ret;
208
209 dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100210
211 /*
212 * If the current value in the calibration register is 0, the
213 * power and current registers will also remain at 0. In case
214 * the chip has been reset let's check the calibration
215 * register and reinitialize if needed.
Marc Titingera0de56c2015-10-28 12:04:53 +0100216 * We do that extra read of the calibration register if there
217 * is some hint of a chip reset.
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100218 */
Marc Titingera0de56c2015-10-28 12:04:53 +0100219 if (*regval == 0) {
220 unsigned int cal;
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100221
Marc Titingera0de56c2015-10-28 12:04:53 +0100222 ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
223 &cal);
224 if (ret < 0)
225 return ret;
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100226
Marc Titingera0de56c2015-10-28 12:04:53 +0100227 if (cal == 0) {
228 dev_warn(dev, "chip not calibrated, reinitializing\n");
229
230 ret = ina2xx_init(data);
231 if (ret < 0)
232 return ret;
233 /*
234 * Let's make sure the power and current
235 * registers have been updated before trying
236 * again.
237 */
238 msleep(INA2XX_MAX_DELAY);
239 continue;
240 }
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100241 }
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100242 return 0;
243 }
244
245 /*
246 * If we're here then although all write operations succeeded, the
247 * chip still returns 0 in the calibration register. Nothing more we
248 * can do here.
249 */
250 dev_err(dev, "unable to reinitialize the chip\n");
251 return -ENODEV;
252}
253
Marc Titingera0de56c2015-10-28 12:04:53 +0100254static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
255 unsigned int regval)
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400256{
Guenter Roeck6106db22012-05-12 11:21:01 -0700257 int val;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400258
259 switch (reg) {
260 case INA2XX_SHUNT_VOLTAGE:
Fabio Baltieric0214f92014-06-08 22:06:24 +0100261 /* signed register */
Marc Titingera0de56c2015-10-28 12:04:53 +0100262 val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400263 break;
264 case INA2XX_BUS_VOLTAGE:
Marc Titingera0de56c2015-10-28 12:04:53 +0100265 val = (regval >> data->config->bus_voltage_shift)
Guenter Roeck6106db22012-05-12 11:21:01 -0700266 * data->config->bus_voltage_lsb;
267 val = DIV_ROUND_CLOSEST(val, 1000);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400268 break;
269 case INA2XX_POWER:
Marc Titingera0de56c2015-10-28 12:04:53 +0100270 val = regval * data->config->power_lsb;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400271 break;
272 case INA2XX_CURRENT:
Fabio Baltieric0214f92014-06-08 22:06:24 +0100273 /* signed register, LSB=1mA (selected), in mA */
Marc Titingera0de56c2015-10-28 12:04:53 +0100274 val = (s16)regval;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400275 break;
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100276 case INA2XX_CALIBRATION:
Bartosz Golaszewskib721fe22015-01-12 14:47:22 +0100277 val = DIV_ROUND_CLOSEST(data->config->calibration_factor,
Marc Titingera0de56c2015-10-28 12:04:53 +0100278 regval);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100279 break;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400280 default:
281 /* programmer goofed */
282 WARN_ON_ONCE(1);
283 val = 0;
284 break;
285 }
286
287 return val;
288}
289
290static ssize_t ina2xx_show_value(struct device *dev,
291 struct device_attribute *da, char *buf)
292{
293 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Marc Titingera0de56c2015-10-28 12:04:53 +0100294 struct ina2xx_data *data = dev_get_drvdata(dev);
295 unsigned int regval;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400296
Marc Titingera0de56c2015-10-28 12:04:53 +0100297 int err = ina2xx_read_reg(dev, attr->index, &regval);
298
299 if (err < 0)
300 return err;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400301
Guenter Roeck6106db22012-05-12 11:21:01 -0700302 return snprintf(buf, PAGE_SIZE, "%d\n",
Marc Titingera0de56c2015-10-28 12:04:53 +0100303 ina2xx_get_value(data, attr->index, regval));
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400304}
305
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100306static ssize_t ina2xx_set_shunt(struct device *dev,
307 struct device_attribute *da,
308 const char *buf, size_t count)
309{
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100310 unsigned long val;
311 int status;
Marc Titingera0de56c2015-10-28 12:04:53 +0100312 struct ina2xx_data *data = dev_get_drvdata(dev);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100313
314 status = kstrtoul(buf, 10, &val);
315 if (status < 0)
316 return status;
317
318 if (val == 0 ||
319 /* Values greater than the calibration factor make no sense. */
320 val > data->config->calibration_factor)
321 return -EINVAL;
322
Marc Titingera0de56c2015-10-28 12:04:53 +0100323 mutex_lock(&data->config_lock);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100324 data->rshunt = val;
325 status = ina2xx_calibrate(data);
Marc Titingera0de56c2015-10-28 12:04:53 +0100326 mutex_unlock(&data->config_lock);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100327 if (status < 0)
328 return status;
329
330 return count;
331}
332
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100333static ssize_t ina226_set_interval(struct device *dev,
334 struct device_attribute *da,
335 const char *buf, size_t count)
336{
337 struct ina2xx_data *data = dev_get_drvdata(dev);
338 unsigned long val;
339 int status;
340
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100341 status = kstrtoul(buf, 10, &val);
342 if (status < 0)
343 return status;
344
345 if (val > INT_MAX || val == 0)
346 return -EINVAL;
347
Marc Titingera0de56c2015-10-28 12:04:53 +0100348 status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
349 INA226_AVG_RD_MASK,
350 ina226_interval_to_reg(val));
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100351 if (status < 0)
352 return status;
353
354 return count;
355}
356
357static ssize_t ina226_show_interval(struct device *dev,
358 struct device_attribute *da, char *buf)
359{
Marc Titingera0de56c2015-10-28 12:04:53 +0100360 struct ina2xx_data *data = dev_get_drvdata(dev);
361 int status;
362 unsigned int regval;
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100363
Marc Titingera0de56c2015-10-28 12:04:53 +0100364 status = regmap_read(data->regmap, INA2XX_CONFIG, &regval);
365 if (status)
366 return status;
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100367
Marc Titingera0de56c2015-10-28 12:04:53 +0100368 return snprintf(buf, PAGE_SIZE, "%d\n", ina226_reg_to_interval(regval));
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100369}
370
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400371/* shunt voltage */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800372static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL,
373 INA2XX_SHUNT_VOLTAGE);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400374
375/* bus voltage */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800376static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL,
377 INA2XX_BUS_VOLTAGE);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400378
379/* calculated current */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800380static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL,
381 INA2XX_CURRENT);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400382
383/* calculated power */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800384static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL,
385 INA2XX_POWER);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400386
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100387/* shunt resistance */
388static SENSOR_DEVICE_ATTR(shunt_resistor, S_IRUGO | S_IWUSR,
389 ina2xx_show_value, ina2xx_set_shunt,
390 INA2XX_CALIBRATION);
391
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100392/* update interval (ina226 only) */
393static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR,
394 ina226_show_interval, ina226_set_interval, 0);
395
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400396/* pointers to created device attributes */
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700397static struct attribute *ina2xx_attrs[] = {
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400398 &sensor_dev_attr_in0_input.dev_attr.attr,
399 &sensor_dev_attr_in1_input.dev_attr.attr,
400 &sensor_dev_attr_curr1_input.dev_attr.attr,
401 &sensor_dev_attr_power1_input.dev_attr.attr,
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100402 &sensor_dev_attr_shunt_resistor.dev_attr.attr,
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400403 NULL,
404};
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100405
406static const struct attribute_group ina2xx_group = {
407 .attrs = ina2xx_attrs,
408};
409
410static struct attribute *ina226_attrs[] = {
411 &sensor_dev_attr_update_interval.dev_attr.attr,
412 NULL,
413};
414
415static const struct attribute_group ina226_group = {
416 .attrs = ina226_attrs,
417};
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400418
419static int ina2xx_probe(struct i2c_client *client,
420 const struct i2c_device_id *id)
421{
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700422 struct device *dev = &client->dev;
423 struct ina2xx_data *data;
424 struct device *hwmon_dev;
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700425 u32 val;
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100426 int ret, group = 0;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400427
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700428 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400429 if (!data)
430 return -ENOMEM;
431
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400432 /* set the device type */
Marc Titinger5aa4e832015-10-29 10:07:17 +0100433 data->config = &ina2xx_config[id->driver_data];
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100434
Marc Titinger001e2e72015-10-27 10:51:08 +0100435 if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
436 struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
437
438 if (pdata)
439 val = pdata->shunt_uohms;
440 else
441 val = INA2XX_RSHUNT_DEFAULT;
442 }
443
444 if (val <= 0 || val > data->config->calibration_factor)
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100445 return -ENODEV;
446
Marc Titinger001e2e72015-10-27 10:51:08 +0100447 data->rshunt = val;
448
Marc Titingera0de56c2015-10-28 12:04:53 +0100449 ina2xx_regmap_config.max_register = data->config->registers;
450
451 data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
452 if (IS_ERR(data->regmap)) {
453 dev_err(dev, "failed to allocate register map\n");
454 return PTR_ERR(data->regmap);
455 }
456
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100457 ret = ina2xx_init(data);
458 if (ret < 0) {
459 dev_err(dev, "error configuring the device: %d\n", ret);
460 return -ENODEV;
461 }
462
Marc Titingera0de56c2015-10-28 12:04:53 +0100463 mutex_init(&data->config_lock);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400464
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100465 data->groups[group++] = &ina2xx_group;
Marc Titinger5aa4e832015-10-29 10:07:17 +0100466 if (id->driver_data == ina226)
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100467 data->groups[group++] = &ina226_group;
468
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700469 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100470 data, data->groups);
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700471 if (IS_ERR(hwmon_dev))
472 return PTR_ERR(hwmon_dev);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400473
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700474 dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100475 id->name, data->rshunt);
Guenter Roeck6106db22012-05-12 11:21:01 -0700476
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400477 return 0;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400478}
479
480static const struct i2c_device_id ina2xx_id[] = {
481 { "ina219", ina219 },
Guenter Roeckdc92cd02012-05-12 11:33:11 -0700482 { "ina220", ina219 },
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400483 { "ina226", ina226 },
Guenter Roeckdc92cd02012-05-12 11:33:11 -0700484 { "ina230", ina226 },
Kevin Hilmanadd513b2015-01-14 17:34:58 -0800485 { "ina231", ina226 },
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400486 { }
487};
488MODULE_DEVICE_TABLE(i2c, ina2xx_id);
489
490static struct i2c_driver ina2xx_driver = {
491 .driver = {
492 .name = "ina2xx",
493 },
494 .probe = ina2xx_probe,
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400495 .id_table = ina2xx_id,
496};
497
Wei Yongjund835ca02012-10-08 20:40:35 +0800498module_i2c_driver(ina2xx_driver);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400499
500MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
501MODULE_DESCRIPTION("ina2xx driver");
502MODULE_LICENSE("GPL");