blob: e9e6aeabbf842ffd941e0476a185884adac85147 [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;
Maciej Purski5d389b12017-11-22 16:32:15 +010098 int calibration_value;
Guenter Roeck6106db22012-05-12 11:21:01 -070099 int registers;
100 int shunt_div;
101 int bus_voltage_shift;
102 int bus_voltage_lsb; /* uV */
Maciej Purski5d389b12017-11-22 16:32:15 +0100103 int power_lsb_factor;
Guenter Roeck6106db22012-05-12 11:21:01 -0700104};
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;
Maciej Purski5d389b12017-11-22 16:32:15 +0100110 long current_lsb_uA;
111 long power_lsb_uW;
Marc Titingera0de56c2015-10-28 12:04:53 +0100112 struct mutex config_lock;
113 struct regmap *regmap;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400114
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100115 const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400116};
117
Guenter Roeck6106db22012-05-12 11:21:01 -0700118static const struct ina2xx_config ina2xx_config[] = {
119 [ina219] = {
120 .config_default = INA219_CONFIG_DEFAULT,
Maciej Purski5d389b12017-11-22 16:32:15 +0100121 .calibration_value = 4096,
Guenter Roeck6106db22012-05-12 11:21:01 -0700122 .registers = INA219_REGISTERS,
123 .shunt_div = 100,
124 .bus_voltage_shift = 3,
125 .bus_voltage_lsb = 4000,
Maciej Purski5d389b12017-11-22 16:32:15 +0100126 .power_lsb_factor = 20,
Guenter Roeck6106db22012-05-12 11:21:01 -0700127 },
128 [ina226] = {
129 .config_default = INA226_CONFIG_DEFAULT,
Maciej Purski5d389b12017-11-22 16:32:15 +0100130 .calibration_value = 2048,
Guenter Roeck6106db22012-05-12 11:21:01 -0700131 .registers = INA226_REGISTERS,
132 .shunt_div = 400,
133 .bus_voltage_shift = 0,
134 .bus_voltage_lsb = 1250,
Maciej Purski5d389b12017-11-22 16:32:15 +0100135 .power_lsb_factor = 25,
Guenter Roeck6106db22012-05-12 11:21:01 -0700136 },
137};
138
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100139/*
140 * Available averaging rates for ina226. The indices correspond with
141 * the bit values expected by the chip (according to the ina226 datasheet,
142 * table 3 AVG bit settings, found at
143 * http://www.ti.com/lit/ds/symlink/ina226.pdf.
144 */
145static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
146
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100147static int ina226_reg_to_interval(u16 config)
148{
149 int avg = ina226_avg_tab[INA226_READ_AVG(config)];
150
151 /*
152 * Multiply the total conversion time by the number of averages.
153 * Return the result in milliseconds.
154 */
155 return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
156}
157
Marc Titingera0de56c2015-10-28 12:04:53 +0100158/*
159 * Return the new, shifted AVG field value of CONFIG register,
160 * to use with regmap_update_bits
161 */
162static u16 ina226_interval_to_reg(int interval)
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100163{
164 int avg, avg_bits;
165
166 avg = DIV_ROUND_CLOSEST(interval * 1000,
167 INA226_TOTAL_CONV_TIME_DEFAULT);
Bartosz Golaszewskid38df342015-04-16 12:43:34 -0700168 avg_bits = find_closest(avg, ina226_avg_tab,
169 ARRAY_SIZE(ina226_avg_tab));
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100170
Marc Titingera0de56c2015-10-28 12:04:53 +0100171 return INA226_SHIFT_AVG(avg_bits);
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100172}
173
Maciej Purski5d389b12017-11-22 16:32:15 +0100174/*
175 * Calibration register is set to the best value, which eliminates
176 * truncation errors on calculating current register in hardware.
177 * According to datasheet (eq. 3) the best values are 2048 for
178 * ina226 and 4096 for ina219. They are hardcoded as calibration_value.
179 */
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100180static int ina2xx_calibrate(struct ina2xx_data *data)
181{
Maciej Purski5d389b12017-11-22 16:32:15 +0100182 return regmap_write(data->regmap, INA2XX_CALIBRATION,
183 data->config->calibration_value);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100184}
185
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100186/*
187 * Initialize the configuration and calibration registers.
188 */
189static int ina2xx_init(struct ina2xx_data *data)
190{
Marc Titingera0de56c2015-10-28 12:04:53 +0100191 int ret = regmap_write(data->regmap, INA2XX_CONFIG,
192 data->config->config_default);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100193 if (ret < 0)
194 return ret;
195
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100196 return ina2xx_calibrate(data);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100197}
198
Marc Titingera0de56c2015-10-28 12:04:53 +0100199static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400200{
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700201 struct ina2xx_data *data = dev_get_drvdata(dev);
Marc Titingera0de56c2015-10-28 12:04:53 +0100202 int ret, retry;
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100203
Marc Titingera0de56c2015-10-28 12:04:53 +0100204 dev_dbg(dev, "Starting register %d read\n", reg);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100205
206 for (retry = 5; retry; retry--) {
Marc Titingera0de56c2015-10-28 12:04:53 +0100207
208 ret = regmap_read(data->regmap, reg, regval);
209 if (ret < 0)
210 return ret;
211
212 dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100213
214 /*
215 * If the current value in the calibration register is 0, the
216 * power and current registers will also remain at 0. In case
217 * the chip has been reset let's check the calibration
218 * register and reinitialize if needed.
Marc Titingera0de56c2015-10-28 12:04:53 +0100219 * We do that extra read of the calibration register if there
220 * is some hint of a chip reset.
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100221 */
Marc Titingera0de56c2015-10-28 12:04:53 +0100222 if (*regval == 0) {
223 unsigned int cal;
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100224
Marc Titingera0de56c2015-10-28 12:04:53 +0100225 ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
226 &cal);
227 if (ret < 0)
228 return ret;
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100229
Marc Titingera0de56c2015-10-28 12:04:53 +0100230 if (cal == 0) {
231 dev_warn(dev, "chip not calibrated, reinitializing\n");
232
233 ret = ina2xx_init(data);
234 if (ret < 0)
235 return ret;
236 /*
237 * Let's make sure the power and current
238 * registers have been updated before trying
239 * again.
240 */
241 msleep(INA2XX_MAX_DELAY);
242 continue;
243 }
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100244 }
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100245 return 0;
246 }
247
248 /*
249 * If we're here then although all write operations succeeded, the
250 * chip still returns 0 in the calibration register. Nothing more we
251 * can do here.
252 */
253 dev_err(dev, "unable to reinitialize the chip\n");
254 return -ENODEV;
255}
256
Marc Titingera0de56c2015-10-28 12:04:53 +0100257static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
258 unsigned int regval)
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400259{
Guenter Roeck6106db22012-05-12 11:21:01 -0700260 int val;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400261
262 switch (reg) {
263 case INA2XX_SHUNT_VOLTAGE:
Fabio Baltieric0214f92014-06-08 22:06:24 +0100264 /* signed register */
Marc Titingera0de56c2015-10-28 12:04:53 +0100265 val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400266 break;
267 case INA2XX_BUS_VOLTAGE:
Marc Titingera0de56c2015-10-28 12:04:53 +0100268 val = (regval >> data->config->bus_voltage_shift)
Guenter Roeck6106db22012-05-12 11:21:01 -0700269 * data->config->bus_voltage_lsb;
270 val = DIV_ROUND_CLOSEST(val, 1000);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400271 break;
272 case INA2XX_POWER:
Maciej Purski5d389b12017-11-22 16:32:15 +0100273 val = regval * data->power_lsb_uW;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400274 break;
275 case INA2XX_CURRENT:
Maciej Purski5d389b12017-11-22 16:32:15 +0100276 /* signed register, result in mA */
277 val = regval * data->current_lsb_uA;
278 val = DIV_ROUND_CLOSEST(val, 1000);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400279 break;
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100280 case INA2XX_CALIBRATION:
Maciej Purski5d389b12017-11-22 16:32:15 +0100281 val = regval;
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100282 break;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400283 default:
284 /* programmer goofed */
285 WARN_ON_ONCE(1);
286 val = 0;
287 break;
288 }
289
290 return val;
291}
292
293static ssize_t ina2xx_show_value(struct device *dev,
294 struct device_attribute *da, char *buf)
295{
296 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Marc Titingera0de56c2015-10-28 12:04:53 +0100297 struct ina2xx_data *data = dev_get_drvdata(dev);
298 unsigned int regval;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400299
Marc Titingera0de56c2015-10-28 12:04:53 +0100300 int err = ina2xx_read_reg(dev, attr->index, &regval);
301
302 if (err < 0)
303 return err;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400304
Guenter Roeck6106db22012-05-12 11:21:01 -0700305 return snprintf(buf, PAGE_SIZE, "%d\n",
Marc Titingera0de56c2015-10-28 12:04:53 +0100306 ina2xx_get_value(data, attr->index, regval));
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400307}
308
Maciej Purski5d389b12017-11-22 16:32:15 +0100309/*
310 * In order to keep calibration register value fixed, the product
311 * of current_lsb and shunt_resistor should also be fixed and equal
312 * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order
313 * to keep the scale.
314 */
315static int ina2xx_set_shunt(struct ina2xx_data *data, long val)
316{
317 unsigned int dividend = DIV_ROUND_CLOSEST(1000000000,
318 data->config->shunt_div);
319 if (val <= 0 || val > dividend)
320 return -EINVAL;
321
322 mutex_lock(&data->config_lock);
323 data->rshunt = val;
324 data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val);
325 data->power_lsb_uW = data->config->power_lsb_factor *
326 data->current_lsb_uA;
327 mutex_unlock(&data->config_lock);
328
329 return 0;
330}
331
332static ssize_t ina2xx_store_shunt(struct device *dev,
333 struct device_attribute *da,
334 const char *buf, size_t count)
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100335{
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100336 unsigned long val;
337 int status;
Marc Titingera0de56c2015-10-28 12:04:53 +0100338 struct ina2xx_data *data = dev_get_drvdata(dev);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100339
340 status = kstrtoul(buf, 10, &val);
341 if (status < 0)
342 return status;
343
Maciej Purski5d389b12017-11-22 16:32:15 +0100344 status = ina2xx_set_shunt(data, val);
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100345 if (status < 0)
346 return status;
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100347 return count;
348}
349
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100350static ssize_t ina226_set_interval(struct device *dev,
351 struct device_attribute *da,
352 const char *buf, size_t count)
353{
354 struct ina2xx_data *data = dev_get_drvdata(dev);
355 unsigned long val;
356 int status;
357
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100358 status = kstrtoul(buf, 10, &val);
359 if (status < 0)
360 return status;
361
362 if (val > INT_MAX || val == 0)
363 return -EINVAL;
364
Marc Titingera0de56c2015-10-28 12:04:53 +0100365 status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
366 INA226_AVG_RD_MASK,
367 ina226_interval_to_reg(val));
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100368 if (status < 0)
369 return status;
370
371 return count;
372}
373
374static ssize_t ina226_show_interval(struct device *dev,
375 struct device_attribute *da, char *buf)
376{
Marc Titingera0de56c2015-10-28 12:04:53 +0100377 struct ina2xx_data *data = dev_get_drvdata(dev);
378 int status;
379 unsigned int regval;
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100380
Marc Titingera0de56c2015-10-28 12:04:53 +0100381 status = regmap_read(data->regmap, INA2XX_CONFIG, &regval);
382 if (status)
383 return status;
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100384
Marc Titingera0de56c2015-10-28 12:04:53 +0100385 return snprintf(buf, PAGE_SIZE, "%d\n", ina226_reg_to_interval(regval));
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100386}
387
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400388/* shunt voltage */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800389static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL,
390 INA2XX_SHUNT_VOLTAGE);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400391
392/* bus voltage */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800393static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL,
394 INA2XX_BUS_VOLTAGE);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400395
396/* calculated current */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800397static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL,
398 INA2XX_CURRENT);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400399
400/* calculated power */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800401static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL,
402 INA2XX_POWER);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400403
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100404/* shunt resistance */
405static SENSOR_DEVICE_ATTR(shunt_resistor, S_IRUGO | S_IWUSR,
Maciej Purski5d389b12017-11-22 16:32:15 +0100406 ina2xx_show_value, ina2xx_store_shunt,
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100407 INA2XX_CALIBRATION);
408
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100409/* update interval (ina226 only) */
410static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR,
411 ina226_show_interval, ina226_set_interval, 0);
412
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400413/* pointers to created device attributes */
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700414static struct attribute *ina2xx_attrs[] = {
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400415 &sensor_dev_attr_in0_input.dev_attr.attr,
416 &sensor_dev_attr_in1_input.dev_attr.attr,
417 &sensor_dev_attr_curr1_input.dev_attr.attr,
418 &sensor_dev_attr_power1_input.dev_attr.attr,
Bartosz Golaszewski8a5fc792015-01-05 15:20:55 +0100419 &sensor_dev_attr_shunt_resistor.dev_attr.attr,
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400420 NULL,
421};
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100422
423static const struct attribute_group ina2xx_group = {
424 .attrs = ina2xx_attrs,
425};
426
427static struct attribute *ina226_attrs[] = {
428 &sensor_dev_attr_update_interval.dev_attr.attr,
429 NULL,
430};
431
432static const struct attribute_group ina226_group = {
433 .attrs = ina226_attrs,
434};
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400435
436static int ina2xx_probe(struct i2c_client *client,
437 const struct i2c_device_id *id)
438{
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700439 struct device *dev = &client->dev;
440 struct ina2xx_data *data;
441 struct device *hwmon_dev;
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700442 u32 val;
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100443 int ret, group = 0;
Javier Martinez Canillasbd0ddd42017-02-24 10:13:00 -0300444 enum ina2xx_ids chip;
445
446 if (client->dev.of_node)
447 chip = (enum ina2xx_ids)of_device_get_match_data(&client->dev);
448 else
449 chip = id->driver_data;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400450
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700451 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400452 if (!data)
453 return -ENOMEM;
454
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400455 /* set the device type */
Javier Martinez Canillasbd0ddd42017-02-24 10:13:00 -0300456 data->config = &ina2xx_config[chip];
Marek Szyprowski0c4c5862018-01-15 14:58:21 +0100457 mutex_init(&data->config_lock);
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100458
Marc Titinger001e2e72015-10-27 10:51:08 +0100459 if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
460 struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
461
462 if (pdata)
463 val = pdata->shunt_uohms;
464 else
465 val = INA2XX_RSHUNT_DEFAULT;
466 }
467
Maciej Purski5d389b12017-11-22 16:32:15 +0100468 ina2xx_set_shunt(data, val);
Marc Titinger001e2e72015-10-27 10:51:08 +0100469
Marc Titingera0de56c2015-10-28 12:04:53 +0100470 ina2xx_regmap_config.max_register = data->config->registers;
471
472 data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
473 if (IS_ERR(data->regmap)) {
474 dev_err(dev, "failed to allocate register map\n");
475 return PTR_ERR(data->regmap);
476 }
477
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100478 ret = ina2xx_init(data);
479 if (ret < 0) {
480 dev_err(dev, "error configuring the device: %d\n", ret);
481 return -ENODEV;
482 }
483
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100484 data->groups[group++] = &ina2xx_group;
Marc Titinger5aa4e832015-10-29 10:07:17 +0100485 if (id->driver_data == ina226)
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100486 data->groups[group++] = &ina226_group;
487
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700488 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
Bartosz Golaszewski72a87a42015-01-09 17:03:42 +0100489 data, data->groups);
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700490 if (IS_ERR(hwmon_dev))
491 return PTR_ERR(hwmon_dev);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400492
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700493 dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
Bartosz Golaszewski509416a2015-01-05 15:20:52 +0100494 id->name, data->rshunt);
Guenter Roeck6106db22012-05-12 11:21:01 -0700495
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400496 return 0;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400497}
498
499static const struct i2c_device_id ina2xx_id[] = {
500 { "ina219", ina219 },
Guenter Roeckdc92cd02012-05-12 11:33:11 -0700501 { "ina220", ina219 },
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400502 { "ina226", ina226 },
Guenter Roeckdc92cd02012-05-12 11:33:11 -0700503 { "ina230", ina226 },
Kevin Hilmanadd513b2015-01-14 17:34:58 -0800504 { "ina231", ina226 },
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400505 { }
506};
507MODULE_DEVICE_TABLE(i2c, ina2xx_id);
508
Javier Martinez Canillasbd0ddd42017-02-24 10:13:00 -0300509static const struct of_device_id ina2xx_of_match[] = {
510 {
511 .compatible = "ti,ina219",
512 .data = (void *)ina219
513 },
514 {
515 .compatible = "ti,ina220",
516 .data = (void *)ina219
517 },
518 {
519 .compatible = "ti,ina226",
520 .data = (void *)ina226
521 },
522 {
523 .compatible = "ti,ina230",
524 .data = (void *)ina226
525 },
526 {
527 .compatible = "ti,ina231",
528 .data = (void *)ina226
529 },
530 { },
531};
532MODULE_DEVICE_TABLE(of, ina2xx_of_match);
533
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400534static struct i2c_driver ina2xx_driver = {
535 .driver = {
536 .name = "ina2xx",
Javier Martinez Canillasbd0ddd42017-02-24 10:13:00 -0300537 .of_match_table = of_match_ptr(ina2xx_of_match),
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400538 },
539 .probe = ina2xx_probe,
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400540 .id_table = ina2xx_id,
541};
542
Wei Yongjund835ca02012-10-08 20:40:35 +0800543module_i2c_driver(ina2xx_driver);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400544
545MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
546MODULE_DESCRIPTION("ina2xx driver");
547MODULE_LICENSE("GPL");