blob: e01feba909c3688ea152b75d6c23b645948632ec [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>
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040038
39#include <linux/platform_data/ina2xx.h>
40
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
48
49/* INA226 register definitions */
50#define INA226_MASK_ENABLE 0x06
51#define INA226_ALERT_LIMIT 0x07
52#define INA226_DIE_ID 0xFF
53
54
55/* register count */
56#define INA219_REGISTERS 6
57#define INA226_REGISTERS 8
58
59#define INA2XX_MAX_REGISTERS 8
60
61/* settings - depend on use case */
62#define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
63#define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
64
65/* worst case is 68.10 ms (~14.6Hz, ina219) */
66#define INA2XX_CONVERSION_RATE 15
67
68enum ina2xx_ids { ina219, ina226 };
69
Guenter Roeck6106db22012-05-12 11:21:01 -070070struct ina2xx_config {
71 u16 config_default;
72 int calibration_factor;
73 int registers;
74 int shunt_div;
75 int bus_voltage_shift;
76 int bus_voltage_lsb; /* uV */
77 int power_lsb; /* uW */
78};
79
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040080struct ina2xx_data {
Guenter Roeck468bf0e2013-09-02 13:16:19 -070081 struct i2c_client *client;
Guenter Roeck6106db22012-05-12 11:21:01 -070082 const struct ina2xx_config *config;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040083
84 struct mutex update_lock;
85 bool valid;
86 unsigned long last_updated;
87
88 int kind;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -040089 u16 regs[INA2XX_MAX_REGISTERS];
90};
91
Guenter Roeck6106db22012-05-12 11:21:01 -070092static const struct ina2xx_config ina2xx_config[] = {
93 [ina219] = {
94 .config_default = INA219_CONFIG_DEFAULT,
95 .calibration_factor = 40960000,
96 .registers = INA219_REGISTERS,
97 .shunt_div = 100,
98 .bus_voltage_shift = 3,
99 .bus_voltage_lsb = 4000,
100 .power_lsb = 20000,
101 },
102 [ina226] = {
103 .config_default = INA226_CONFIG_DEFAULT,
104 .calibration_factor = 5120000,
105 .registers = INA226_REGISTERS,
106 .shunt_div = 400,
107 .bus_voltage_shift = 0,
108 .bus_voltage_lsb = 1250,
109 .power_lsb = 25000,
110 },
111};
112
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400113static struct ina2xx_data *ina2xx_update_device(struct device *dev)
114{
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700115 struct ina2xx_data *data = dev_get_drvdata(dev);
116 struct i2c_client *client = data->client;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400117 struct ina2xx_data *ret = data;
118
119 mutex_lock(&data->update_lock);
120
121 if (time_after(jiffies, data->last_updated +
122 HZ / INA2XX_CONVERSION_RATE) || !data->valid) {
123
124 int i;
125
126 dev_dbg(&client->dev, "Starting ina2xx update\n");
127
128 /* Read all registers */
Guenter Roeck6106db22012-05-12 11:21:01 -0700129 for (i = 0; i < data->config->registers; i++) {
Guenter Roeck080b98e2012-09-11 08:22:14 -0700130 int rv = i2c_smbus_read_word_swapped(client, i);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400131 if (rv < 0) {
132 ret = ERR_PTR(rv);
133 goto abort;
134 }
135 data->regs[i] = rv;
136 }
137 data->last_updated = jiffies;
138 data->valid = 1;
139 }
140abort:
141 mutex_unlock(&data->update_lock);
142 return ret;
143}
144
Guenter Roeck6106db22012-05-12 11:21:01 -0700145static int ina2xx_get_value(struct ina2xx_data *data, u8 reg)
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400146{
Guenter Roeck6106db22012-05-12 11:21:01 -0700147 int val;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400148
149 switch (reg) {
150 case INA2XX_SHUNT_VOLTAGE:
Fabio Baltieric0214f92014-06-08 22:06:24 +0100151 /* signed register */
152 val = DIV_ROUND_CLOSEST((s16)data->regs[reg],
Guenter Roeck6106db22012-05-12 11:21:01 -0700153 data->config->shunt_div);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400154 break;
155 case INA2XX_BUS_VOLTAGE:
Guenter Roeck6106db22012-05-12 11:21:01 -0700156 val = (data->regs[reg] >> data->config->bus_voltage_shift)
157 * data->config->bus_voltage_lsb;
158 val = DIV_ROUND_CLOSEST(val, 1000);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400159 break;
160 case INA2XX_POWER:
Guenter Roeck6106db22012-05-12 11:21:01 -0700161 val = data->regs[reg] * data->config->power_lsb;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400162 break;
163 case INA2XX_CURRENT:
Fabio Baltieric0214f92014-06-08 22:06:24 +0100164 /* signed register, LSB=1mA (selected), in mA */
165 val = (s16)data->regs[reg];
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400166 break;
167 default:
168 /* programmer goofed */
169 WARN_ON_ONCE(1);
170 val = 0;
171 break;
172 }
173
174 return val;
175}
176
177static ssize_t ina2xx_show_value(struct device *dev,
178 struct device_attribute *da, char *buf)
179{
180 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
181 struct ina2xx_data *data = ina2xx_update_device(dev);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400182
183 if (IS_ERR(data))
184 return PTR_ERR(data);
185
Guenter Roeck6106db22012-05-12 11:21:01 -0700186 return snprintf(buf, PAGE_SIZE, "%d\n",
187 ina2xx_get_value(data, attr->index));
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400188}
189
190/* shunt voltage */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800191static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL,
192 INA2XX_SHUNT_VOLTAGE);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400193
194/* bus voltage */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800195static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL,
196 INA2XX_BUS_VOLTAGE);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400197
198/* calculated current */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800199static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL,
200 INA2XX_CURRENT);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400201
202/* calculated power */
Guenter Roeckf0df0fd2013-01-10 10:04:06 -0800203static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL,
204 INA2XX_POWER);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400205
206/* pointers to created device attributes */
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700207static struct attribute *ina2xx_attrs[] = {
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400208 &sensor_dev_attr_in0_input.dev_attr.attr,
209 &sensor_dev_attr_in1_input.dev_attr.attr,
210 &sensor_dev_attr_curr1_input.dev_attr.attr,
211 &sensor_dev_attr_power1_input.dev_attr.attr,
212 NULL,
213};
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700214ATTRIBUTE_GROUPS(ina2xx);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400215
216static int ina2xx_probe(struct i2c_client *client,
217 const struct i2c_device_id *id)
218{
219 struct i2c_adapter *adapter = client->adapter;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400220 struct ina2xx_platform_data *pdata;
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700221 struct device *dev = &client->dev;
222 struct ina2xx_data *data;
223 struct device *hwmon_dev;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400224 long shunt = 10000; /* default shunt value 10mOhms */
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700225 u32 val;
Bartosz Golaszewskif975b332014-11-27 10:59:06 +0100226 int ret;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400227
228 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
229 return -ENODEV;
230
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700231 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400232 if (!data)
233 return -ENOMEM;
234
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700235 if (dev_get_platdata(dev)) {
236 pdata = dev_get_platdata(dev);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400237 shunt = pdata->shunt_uohms;
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700238 } else if (!of_property_read_u32(dev->of_node,
239 "shunt-resistor", &val)) {
240 shunt = val;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400241 }
242
243 if (shunt <= 0)
244 return -ENODEV;
245
246 /* set the device type */
247 data->kind = id->driver_data;
Guenter Roeck6106db22012-05-12 11:21:01 -0700248 data->config = &ina2xx_config[data->kind];
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400249
Guenter Roeck6106db22012-05-12 11:21:01 -0700250 /* device configuration */
Bartosz Golaszewskif975b332014-11-27 10:59:06 +0100251 ret = i2c_smbus_write_word_swapped(client, INA2XX_CONFIG,
252 data->config->config_default);
253 if (ret < 0) {
254 dev_err(dev,
255 "error writing to the config register: %d", ret);
256 return -ENODEV;
257 }
258
259 /*
260 * Set current LSB to 1mA, shunt is in uOhms
261 * (equation 13 in datasheet).
262 */
263 ret = i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION,
264 data->config->calibration_factor / shunt);
265 if (ret < 0) {
266 dev_err(dev,
267 "error writing to the calibration register: %d", ret);
268 return -ENODEV;
269 }
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400270
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700271 data->client = client;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400272 mutex_init(&data->update_lock);
273
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700274 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
275 data, ina2xx_groups);
276 if (IS_ERR(hwmon_dev))
277 return PTR_ERR(hwmon_dev);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400278
Guenter Roeck468bf0e2013-09-02 13:16:19 -0700279 dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
Guenter Roeck6106db22012-05-12 11:21:01 -0700280 id->name, shunt);
281
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400282 return 0;
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400283}
284
285static const struct i2c_device_id ina2xx_id[] = {
286 { "ina219", ina219 },
Guenter Roeckdc92cd02012-05-12 11:33:11 -0700287 { "ina220", ina219 },
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400288 { "ina226", ina226 },
Guenter Roeckdc92cd02012-05-12 11:33:11 -0700289 { "ina230", ina226 },
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400290 { }
291};
292MODULE_DEVICE_TABLE(i2c, ina2xx_id);
293
294static struct i2c_driver ina2xx_driver = {
295 .driver = {
296 .name = "ina2xx",
297 },
298 .probe = ina2xx_probe,
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400299 .id_table = ina2xx_id,
300};
301
Wei Yongjund835ca02012-10-08 20:40:35 +0800302module_i2c_driver(ina2xx_driver);
Felten, Lotharf7c2fe32012-05-12 04:36:38 -0400303
304MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
305MODULE_DESCRIPTION("ina2xx driver");
306MODULE_LICENSE("GPL");