2 * Driver for Texas Instruments INA219, INA226 power monitor chips
5 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
6 * Datasheet: http://www.ti.com/product/ina219
9 * Bi-Directional Current/Power Monitor with I2C Interface
10 * Datasheet: http://www.ti.com/product/ina226
12 * Copyright (C) 2012 Lothar Felten <l-felten@ti.com>
13 * Thanks to Jan Volkering
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.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/err.h>
24 #include <linux/slab.h>
25 #include <linux/i2c.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
29 #include <linux/platform_data/ina2xx.h>
31 /* common register definitions */
32 #define INA2XX_CONFIG 0x00
33 #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
34 #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
35 #define INA2XX_POWER 0x03 /* readonly */
36 #define INA2XX_CURRENT 0x04 /* readonly */
37 #define INA2XX_CALIBRATION 0x05
39 /* INA226 register definitions */
40 #define INA226_MASK_ENABLE 0x06
41 #define INA226_ALERT_LIMIT 0x07
42 #define INA226_DIE_ID 0xFF
46 #define INA219_REGISTERS 6
47 #define INA226_REGISTERS 8
49 #define INA2XX_MAX_REGISTERS 8
51 /* settings - depend on use case */
52 #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
53 #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
55 /* worst case is 68.10 ms (~14.6Hz, ina219) */
56 #define INA2XX_CONVERSION_RATE 15
58 enum ina2xx_ids
{ ina219
, ina226
};
61 struct device
*hwmon_dev
;
63 struct mutex update_lock
;
65 unsigned long last_updated
;
69 u16 regs
[INA2XX_MAX_REGISTERS
];
72 int ina2xx_read_word(struct i2c_client
*client
, int reg
)
74 int val
= i2c_smbus_read_word_data(client
, reg
);
75 if (unlikely(val
< 0)) {
77 "Failed to read register: %d\n", reg
);
80 return be16_to_cpu(val
);
83 void ina2xx_write_word(struct i2c_client
*client
, int reg
, int data
)
85 i2c_smbus_write_word_data(client
, reg
, cpu_to_be16(data
));
88 static struct ina2xx_data
*ina2xx_update_device(struct device
*dev
)
90 struct i2c_client
*client
= to_i2c_client(dev
);
91 struct ina2xx_data
*data
= i2c_get_clientdata(client
);
92 struct ina2xx_data
*ret
= data
;
94 mutex_lock(&data
->update_lock
);
96 if (time_after(jiffies
, data
->last_updated
+
97 HZ
/ INA2XX_CONVERSION_RATE
) || !data
->valid
) {
101 dev_dbg(&client
->dev
, "Starting ina2xx update\n");
103 /* Read all registers */
104 for (i
= 0; i
< data
->registers
; i
++) {
105 int rv
= ina2xx_read_word(client
, i
);
112 data
->last_updated
= jiffies
;
116 mutex_unlock(&data
->update_lock
);
120 static int ina219_get_value(struct ina2xx_data
*data
, u8 reg
)
123 * calculate exact value for the given register
124 * we assume default power-on reset settings:
125 * bus voltage range 32V
127 * adc 1 & 2 -> conversion time 532uS
128 * mode is continuous shunt and bus
129 * calibration value is INA219_CALIBRATION_VALUE
131 int val
= data
->regs
[reg
];
134 case INA2XX_SHUNT_VOLTAGE
:
135 /* LSB=10uV. Convert to mV. */
136 val
= DIV_ROUND_CLOSEST(val
, 100);
138 case INA2XX_BUS_VOLTAGE
:
139 /* LSB=4mV. Register is not right aligned, convert to mV. */
140 val
= (val
>> 3) * 4;
143 /* LSB=20mW. Convert to uW */
144 val
= val
* 20 * 1000;
147 /* LSB=1mA (selected). Is in mA */
150 /* programmer goofed */
159 static int ina226_get_value(struct ina2xx_data
*data
, u8 reg
)
162 * calculate exact value for the given register
163 * we assume default power-on reset settings:
164 * bus voltage range 32V
166 * adc 1 & 2 -> conversion time 532uS
167 * mode is continuous shunt and bus
168 * calibration value is INA226_CALIBRATION_VALUE
170 int val
= data
->regs
[reg
];
173 case INA2XX_SHUNT_VOLTAGE
:
174 /* LSB=2.5uV. Convert to mV. */
175 val
= DIV_ROUND_CLOSEST(val
, 400);
177 case INA2XX_BUS_VOLTAGE
:
178 /* LSB=1.25mV. Convert to mV. */
179 val
= val
+ DIV_ROUND_CLOSEST(val
, 4);
182 /* LSB=25mW. Convert to uW */
183 val
= val
* 25 * 1000;
186 /* LSB=1mA (selected). Is in mA */
189 /* programmer goofed */
198 static ssize_t
ina2xx_show_value(struct device
*dev
,
199 struct device_attribute
*da
, char *buf
)
201 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
202 struct ina2xx_data
*data
= ina2xx_update_device(dev
);
206 return PTR_ERR(data
);
208 switch (data
->kind
) {
210 value
= ina219_get_value(data
, attr
->index
);
213 value
= ina226_get_value(data
, attr
->index
);
219 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
223 static SENSOR_DEVICE_ATTR(in0_input
, S_IRUGO
, \
224 ina2xx_show_value
, NULL
, INA2XX_SHUNT_VOLTAGE
);
227 static SENSOR_DEVICE_ATTR(in1_input
, S_IRUGO
, \
228 ina2xx_show_value
, NULL
, INA2XX_BUS_VOLTAGE
);
230 /* calculated current */
231 static SENSOR_DEVICE_ATTR(curr1_input
, S_IRUGO
, \
232 ina2xx_show_value
, NULL
, INA2XX_CURRENT
);
234 /* calculated power */
235 static SENSOR_DEVICE_ATTR(power1_input
, S_IRUGO
, \
236 ina2xx_show_value
, NULL
, INA2XX_POWER
);
238 /* pointers to created device attributes */
239 static struct attribute
*ina2xx_attributes
[] = {
240 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
241 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
242 &sensor_dev_attr_curr1_input
.dev_attr
.attr
,
243 &sensor_dev_attr_power1_input
.dev_attr
.attr
,
247 static const struct attribute_group ina2xx_group
= {
248 .attrs
= ina2xx_attributes
,
251 static int ina2xx_probe(struct i2c_client
*client
,
252 const struct i2c_device_id
*id
)
254 struct i2c_adapter
*adapter
= client
->adapter
;
255 struct ina2xx_data
*data
;
256 struct ina2xx_platform_data
*pdata
;
258 long shunt
= 10000; /* default shunt value 10mOhms */
260 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
263 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
267 if (client
->dev
.platform_data
) {
269 (struct ina2xx_platform_data
*)client
->dev
.platform_data
;
270 shunt
= pdata
->shunt_uohms
;
276 /* set the device type */
277 data
->kind
= id
->driver_data
;
279 switch (data
->kind
) {
281 /* device configuration */
282 ina2xx_write_word(client
, INA2XX_CONFIG
, INA219_CONFIG_DEFAULT
);
284 /* set current LSB to 1mA, shunt is in uOhms */
285 /* (equation 13 in datasheet) */
286 ina2xx_write_word(client
, INA2XX_CALIBRATION
, 40960000 / shunt
);
287 dev_info(&client
->dev
,
288 "power monitor INA219 (Rshunt = %li uOhm)\n", shunt
);
289 data
->registers
= INA219_REGISTERS
;
292 /* device configuration */
293 ina2xx_write_word(client
, INA2XX_CONFIG
, INA226_CONFIG_DEFAULT
);
295 /* set current LSB to 1mA, shunt is in uOhms */
296 /* (equation 1 in datasheet)*/
297 ina2xx_write_word(client
, INA2XX_CALIBRATION
, 5120000 / shunt
);
298 dev_info(&client
->dev
,
299 "power monitor INA226 (Rshunt = %li uOhm)\n", shunt
);
300 data
->registers
= INA226_REGISTERS
;
303 /* unknown device id */
307 i2c_set_clientdata(client
, data
);
308 mutex_init(&data
->update_lock
);
310 ret
= sysfs_create_group(&client
->dev
.kobj
, &ina2xx_group
);
314 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
315 if (IS_ERR(data
->hwmon_dev
)) {
316 ret
= PTR_ERR(data
->hwmon_dev
);
323 sysfs_remove_group(&client
->dev
.kobj
, &ina2xx_group
);
327 static int ina2xx_remove(struct i2c_client
*client
)
329 struct ina2xx_data
*data
= i2c_get_clientdata(client
);
331 hwmon_device_unregister(data
->hwmon_dev
);
332 sysfs_remove_group(&client
->dev
.kobj
, &ina2xx_group
);
337 static const struct i2c_device_id ina2xx_id
[] = {
338 { "ina219", ina219
},
339 { "ina226", ina226
},
342 MODULE_DEVICE_TABLE(i2c
, ina2xx_id
);
344 static struct i2c_driver ina2xx_driver
= {
348 .probe
= ina2xx_probe
,
349 .remove
= ina2xx_remove
,
350 .id_table
= ina2xx_id
,
353 static int __init
ina2xx_init(void)
355 return i2c_add_driver(&ina2xx_driver
);
358 static void __exit
ina2xx_exit(void)
360 i2c_del_driver(&ina2xx_driver
);
363 MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
364 MODULE_DESCRIPTION("ina2xx driver");
365 MODULE_LICENSE("GPL");
367 module_init(ina2xx_init
);
368 module_exit(ina2xx_exit
);