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/ina220
13 * Bi-Directional Current/Power Monitor with I2C Interface
14 * Datasheet: http://www.ti.com/product/ina226
17 * Bi-directional Current/Power Monitor with I2C Interface
18 * Datasheet: http://www.ti.com/product/ina230
20 * Copyright (C) 2012 Lothar Felten <l-felten@ti.com>
21 * Thanks to Jan Volkering
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.
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>
36 #include <linux/jiffies.h>
39 #include <linux/platform_data/ina2xx.h>
41 /* common register definitions */
42 #define INA2XX_CONFIG 0x00
43 #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
44 #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
45 #define INA2XX_POWER 0x03 /* readonly */
46 #define INA2XX_CURRENT 0x04 /* readonly */
47 #define INA2XX_CALIBRATION 0x05
49 /* INA226 register definitions */
50 #define INA226_MASK_ENABLE 0x06
51 #define INA226_ALERT_LIMIT 0x07
52 #define INA226_DIE_ID 0xFF
56 #define INA219_REGISTERS 6
57 #define INA226_REGISTERS 8
59 #define INA2XX_MAX_REGISTERS 8
61 /* settings - depend on use case */
62 #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
63 #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
65 /* worst case is 68.10 ms (~14.6Hz, ina219) */
66 #define INA2XX_CONVERSION_RATE 15
68 enum ina2xx_ids
{ ina219
, ina226
};
70 struct ina2xx_config
{
72 int calibration_factor
;
75 int bus_voltage_shift
;
76 int bus_voltage_lsb
; /* uV */
77 int power_lsb
; /* uW */
81 struct i2c_client
*client
;
82 const struct ina2xx_config
*config
;
84 struct mutex update_lock
;
86 unsigned long last_updated
;
89 u16 regs
[INA2XX_MAX_REGISTERS
];
92 static const struct ina2xx_config ina2xx_config
[] = {
94 .config_default
= INA219_CONFIG_DEFAULT
,
95 .calibration_factor
= 40960000,
96 .registers
= INA219_REGISTERS
,
98 .bus_voltage_shift
= 3,
99 .bus_voltage_lsb
= 4000,
103 .config_default
= INA226_CONFIG_DEFAULT
,
104 .calibration_factor
= 5120000,
105 .registers
= INA226_REGISTERS
,
107 .bus_voltage_shift
= 0,
108 .bus_voltage_lsb
= 1250,
113 static struct ina2xx_data
*ina2xx_update_device(struct device
*dev
)
115 struct ina2xx_data
*data
= dev_get_drvdata(dev
);
116 struct i2c_client
*client
= data
->client
;
117 struct ina2xx_data
*ret
= data
;
119 mutex_lock(&data
->update_lock
);
121 if (time_after(jiffies
, data
->last_updated
+
122 HZ
/ INA2XX_CONVERSION_RATE
) || !data
->valid
) {
126 dev_dbg(&client
->dev
, "Starting ina2xx update\n");
128 /* Read all registers */
129 for (i
= 0; i
< data
->config
->registers
; i
++) {
130 int rv
= i2c_smbus_read_word_swapped(client
, i
);
137 data
->last_updated
= jiffies
;
141 mutex_unlock(&data
->update_lock
);
145 static int ina2xx_get_value(struct ina2xx_data
*data
, u8 reg
)
150 case INA2XX_SHUNT_VOLTAGE
:
151 val
= DIV_ROUND_CLOSEST(data
->regs
[reg
],
152 data
->config
->shunt_div
);
154 case INA2XX_BUS_VOLTAGE
:
155 val
= (data
->regs
[reg
] >> data
->config
->bus_voltage_shift
)
156 * data
->config
->bus_voltage_lsb
;
157 val
= DIV_ROUND_CLOSEST(val
, 1000);
160 val
= data
->regs
[reg
] * data
->config
->power_lsb
;
163 /* LSB=1mA (selected). Is in mA */
164 val
= data
->regs
[reg
];
167 /* programmer goofed */
176 static ssize_t
ina2xx_show_value(struct device
*dev
,
177 struct device_attribute
*da
, char *buf
)
179 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
180 struct ina2xx_data
*data
= ina2xx_update_device(dev
);
183 return PTR_ERR(data
);
185 return snprintf(buf
, PAGE_SIZE
, "%d\n",
186 ina2xx_get_value(data
, attr
->index
));
190 static SENSOR_DEVICE_ATTR(in0_input
, S_IRUGO
, ina2xx_show_value
, NULL
,
191 INA2XX_SHUNT_VOLTAGE
);
194 static SENSOR_DEVICE_ATTR(in1_input
, S_IRUGO
, ina2xx_show_value
, NULL
,
197 /* calculated current */
198 static SENSOR_DEVICE_ATTR(curr1_input
, S_IRUGO
, ina2xx_show_value
, NULL
,
201 /* calculated power */
202 static SENSOR_DEVICE_ATTR(power1_input
, S_IRUGO
, ina2xx_show_value
, NULL
,
205 /* pointers to created device attributes */
206 static struct attribute
*ina2xx_attrs
[] = {
207 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
208 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
209 &sensor_dev_attr_curr1_input
.dev_attr
.attr
,
210 &sensor_dev_attr_power1_input
.dev_attr
.attr
,
213 ATTRIBUTE_GROUPS(ina2xx
);
215 static int ina2xx_probe(struct i2c_client
*client
,
216 const struct i2c_device_id
*id
)
218 struct i2c_adapter
*adapter
= client
->adapter
;
219 struct ina2xx_platform_data
*pdata
;
220 struct device
*dev
= &client
->dev
;
221 struct ina2xx_data
*data
;
222 struct device
*hwmon_dev
;
223 long shunt
= 10000; /* default shunt value 10mOhms */
226 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
229 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
233 if (dev_get_platdata(dev
)) {
234 pdata
= dev_get_platdata(dev
);
235 shunt
= pdata
->shunt_uohms
;
236 } else if (!of_property_read_u32(dev
->of_node
,
237 "shunt-resistor", &val
)) {
244 /* set the device type */
245 data
->kind
= id
->driver_data
;
246 data
->config
= &ina2xx_config
[data
->kind
];
248 /* device configuration */
249 i2c_smbus_write_word_swapped(client
, INA2XX_CONFIG
,
250 data
->config
->config_default
);
251 /* set current LSB to 1mA, shunt is in uOhms */
252 /* (equation 13 in datasheet) */
253 i2c_smbus_write_word_swapped(client
, INA2XX_CALIBRATION
,
254 data
->config
->calibration_factor
/ shunt
);
256 data
->client
= client
;
257 mutex_init(&data
->update_lock
);
259 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
260 data
, ina2xx_groups
);
261 if (IS_ERR(hwmon_dev
))
262 return PTR_ERR(hwmon_dev
);
264 dev_info(dev
, "power monitor %s (Rshunt = %li uOhm)\n",
270 static const struct i2c_device_id ina2xx_id
[] = {
271 { "ina219", ina219
},
272 { "ina220", ina219
},
273 { "ina226", ina226
},
274 { "ina230", ina226
},
277 MODULE_DEVICE_TABLE(i2c
, ina2xx_id
);
279 static struct i2c_driver ina2xx_driver
= {
283 .probe
= ina2xx_probe
,
284 .id_table
= ina2xx_id
,
287 module_i2c_driver(ina2xx_driver
);
289 MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
290 MODULE_DESCRIPTION("ina2xx driver");
291 MODULE_LICENSE("GPL");