Commit | Line | Data |
---|---|---|
f7c2fe38 FL |
1 | /* |
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 | * | |
dc92cd0c GR |
8 | * INA220: |
9 | * Bi-Directional Current/Power Monitor with I2C Interface | |
10 | * Datasheet: http://www.ti.com/product/ina220 | |
11 | * | |
f7c2fe38 FL |
12 | * INA226: |
13 | * Bi-Directional Current/Power Monitor with I2C Interface | |
14 | * Datasheet: http://www.ti.com/product/ina226 | |
15 | * | |
dc92cd0c GR |
16 | * INA230: |
17 | * Bi-directional Current/Power Monitor with I2C Interface | |
18 | * Datasheet: http://www.ti.com/product/ina230 | |
19 | * | |
f7c2fe38 FL |
20 | * 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> | |
dcd8f392 | 36 | #include <linux/jiffies.h> |
31e7ad74 | 37 | #include <linux/of.h> |
509416a8 | 38 | #include <linux/delay.h> |
f7c2fe38 FL |
39 | |
40 | #include <linux/platform_data/ina2xx.h> | |
41 | ||
42 | /* common register definitions */ | |
43 | #define INA2XX_CONFIG 0x00 | |
44 | #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */ | |
45 | #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */ | |
46 | #define INA2XX_POWER 0x03 /* readonly */ | |
47 | #define INA2XX_CURRENT 0x04 /* readonly */ | |
48 | #define INA2XX_CALIBRATION 0x05 | |
49 | ||
50 | /* INA226 register definitions */ | |
51 | #define INA226_MASK_ENABLE 0x06 | |
52 | #define INA226_ALERT_LIMIT 0x07 | |
53 | #define INA226_DIE_ID 0xFF | |
54 | ||
f7c2fe38 FL |
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 | |
509416a8 BG |
67 | #define INA2XX_MAX_DELAY 69 /* worst case delay in ms */ |
68 | ||
69 | #define INA2XX_RSHUNT_DEFAULT 10000 | |
f7c2fe38 FL |
70 | |
71 | enum ina2xx_ids { ina219, ina226 }; | |
72 | ||
6106db25 GR |
73 | struct ina2xx_config { |
74 | u16 config_default; | |
75 | int calibration_factor; | |
76 | int registers; | |
77 | int shunt_div; | |
78 | int bus_voltage_shift; | |
79 | int bus_voltage_lsb; /* uV */ | |
80 | int power_lsb; /* uW */ | |
81 | }; | |
82 | ||
f7c2fe38 | 83 | struct ina2xx_data { |
468bf0e3 | 84 | struct i2c_client *client; |
6106db25 | 85 | const struct ina2xx_config *config; |
f7c2fe38 | 86 | |
509416a8 BG |
87 | long rshunt; |
88 | ||
f7c2fe38 FL |
89 | struct mutex update_lock; |
90 | bool valid; | |
91 | unsigned long last_updated; | |
92 | ||
93 | int kind; | |
f7c2fe38 FL |
94 | u16 regs[INA2XX_MAX_REGISTERS]; |
95 | }; | |
96 | ||
6106db25 GR |
97 | static const struct ina2xx_config ina2xx_config[] = { |
98 | [ina219] = { | |
99 | .config_default = INA219_CONFIG_DEFAULT, | |
100 | .calibration_factor = 40960000, | |
101 | .registers = INA219_REGISTERS, | |
102 | .shunt_div = 100, | |
103 | .bus_voltage_shift = 3, | |
104 | .bus_voltage_lsb = 4000, | |
105 | .power_lsb = 20000, | |
106 | }, | |
107 | [ina226] = { | |
108 | .config_default = INA226_CONFIG_DEFAULT, | |
109 | .calibration_factor = 5120000, | |
110 | .registers = INA226_REGISTERS, | |
111 | .shunt_div = 400, | |
112 | .bus_voltage_shift = 0, | |
113 | .bus_voltage_lsb = 1250, | |
114 | .power_lsb = 25000, | |
115 | }, | |
116 | }; | |
117 | ||
509416a8 BG |
118 | /* |
119 | * Initialize the configuration and calibration registers. | |
120 | */ | |
121 | static int ina2xx_init(struct ina2xx_data *data) | |
f7c2fe38 | 122 | { |
468bf0e3 | 123 | struct i2c_client *client = data->client; |
509416a8 | 124 | int ret; |
f7c2fe38 | 125 | |
509416a8 BG |
126 | /* device configuration */ |
127 | ret = i2c_smbus_write_word_swapped(client, INA2XX_CONFIG, | |
128 | data->config->config_default); | |
129 | if (ret < 0) | |
130 | return ret; | |
f7c2fe38 | 131 | |
509416a8 BG |
132 | /* |
133 | * Set current LSB to 1mA, shunt is in uOhms | |
134 | * (equation 13 in datasheet). | |
135 | */ | |
136 | return i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION, | |
137 | data->config->calibration_factor / data->rshunt); | |
138 | } | |
f7c2fe38 | 139 | |
509416a8 BG |
140 | static int ina2xx_do_update(struct device *dev) |
141 | { | |
142 | struct ina2xx_data *data = dev_get_drvdata(dev); | |
143 | struct i2c_client *client = data->client; | |
144 | int i, rv, retry; | |
f7c2fe38 | 145 | |
509416a8 | 146 | dev_dbg(&client->dev, "Starting ina2xx update\n"); |
f7c2fe38 | 147 | |
509416a8 | 148 | for (retry = 5; retry; retry--) { |
f7c2fe38 | 149 | /* Read all registers */ |
6106db25 | 150 | for (i = 0; i < data->config->registers; i++) { |
509416a8 BG |
151 | rv = i2c_smbus_read_word_swapped(client, i); |
152 | if (rv < 0) | |
153 | return rv; | |
f7c2fe38 FL |
154 | data->regs[i] = rv; |
155 | } | |
509416a8 BG |
156 | |
157 | /* | |
158 | * If the current value in the calibration register is 0, the | |
159 | * power and current registers will also remain at 0. In case | |
160 | * the chip has been reset let's check the calibration | |
161 | * register and reinitialize if needed. | |
162 | */ | |
163 | if (data->regs[INA2XX_CALIBRATION] == 0) { | |
164 | dev_warn(dev, "chip not calibrated, reinitializing\n"); | |
165 | ||
166 | rv = ina2xx_init(data); | |
167 | if (rv < 0) | |
168 | return rv; | |
169 | ||
170 | /* | |
171 | * Let's make sure the power and current registers | |
172 | * have been updated before trying again. | |
173 | */ | |
174 | msleep(INA2XX_MAX_DELAY); | |
175 | continue; | |
176 | } | |
177 | ||
f7c2fe38 FL |
178 | data->last_updated = jiffies; |
179 | data->valid = 1; | |
509416a8 BG |
180 | |
181 | return 0; | |
f7c2fe38 | 182 | } |
509416a8 BG |
183 | |
184 | /* | |
185 | * If we're here then although all write operations succeeded, the | |
186 | * chip still returns 0 in the calibration register. Nothing more we | |
187 | * can do here. | |
188 | */ | |
189 | dev_err(dev, "unable to reinitialize the chip\n"); | |
190 | return -ENODEV; | |
191 | } | |
192 | ||
193 | static struct ina2xx_data *ina2xx_update_device(struct device *dev) | |
194 | { | |
195 | struct ina2xx_data *data = dev_get_drvdata(dev); | |
196 | struct ina2xx_data *ret = data; | |
197 | int rv; | |
198 | ||
199 | mutex_lock(&data->update_lock); | |
200 | ||
201 | if (time_after(jiffies, data->last_updated + | |
202 | HZ / INA2XX_CONVERSION_RATE) || !data->valid) { | |
203 | rv = ina2xx_do_update(dev); | |
204 | if (rv < 0) | |
205 | ret = ERR_PTR(rv); | |
206 | } | |
207 | ||
f7c2fe38 FL |
208 | mutex_unlock(&data->update_lock); |
209 | return ret; | |
210 | } | |
211 | ||
6106db25 | 212 | static int ina2xx_get_value(struct ina2xx_data *data, u8 reg) |
f7c2fe38 | 213 | { |
6106db25 | 214 | int val; |
f7c2fe38 FL |
215 | |
216 | switch (reg) { | |
217 | case INA2XX_SHUNT_VOLTAGE: | |
c0214f98 FB |
218 | /* signed register */ |
219 | val = DIV_ROUND_CLOSEST((s16)data->regs[reg], | |
6106db25 | 220 | data->config->shunt_div); |
f7c2fe38 FL |
221 | break; |
222 | case INA2XX_BUS_VOLTAGE: | |
6106db25 GR |
223 | val = (data->regs[reg] >> data->config->bus_voltage_shift) |
224 | * data->config->bus_voltage_lsb; | |
225 | val = DIV_ROUND_CLOSEST(val, 1000); | |
f7c2fe38 FL |
226 | break; |
227 | case INA2XX_POWER: | |
6106db25 | 228 | val = data->regs[reg] * data->config->power_lsb; |
f7c2fe38 FL |
229 | break; |
230 | case INA2XX_CURRENT: | |
c0214f98 FB |
231 | /* signed register, LSB=1mA (selected), in mA */ |
232 | val = (s16)data->regs[reg]; | |
f7c2fe38 FL |
233 | break; |
234 | default: | |
235 | /* programmer goofed */ | |
236 | WARN_ON_ONCE(1); | |
237 | val = 0; | |
238 | break; | |
239 | } | |
240 | ||
241 | return val; | |
242 | } | |
243 | ||
244 | static ssize_t ina2xx_show_value(struct device *dev, | |
245 | struct device_attribute *da, char *buf) | |
246 | { | |
247 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | |
248 | struct ina2xx_data *data = ina2xx_update_device(dev); | |
f7c2fe38 FL |
249 | |
250 | if (IS_ERR(data)) | |
251 | return PTR_ERR(data); | |
252 | ||
6106db25 GR |
253 | return snprintf(buf, PAGE_SIZE, "%d\n", |
254 | ina2xx_get_value(data, attr->index)); | |
f7c2fe38 FL |
255 | } |
256 | ||
257 | /* shunt voltage */ | |
f0df0fd9 GR |
258 | static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL, |
259 | INA2XX_SHUNT_VOLTAGE); | |
f7c2fe38 FL |
260 | |
261 | /* bus voltage */ | |
f0df0fd9 GR |
262 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL, |
263 | INA2XX_BUS_VOLTAGE); | |
f7c2fe38 FL |
264 | |
265 | /* calculated current */ | |
f0df0fd9 GR |
266 | static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL, |
267 | INA2XX_CURRENT); | |
f7c2fe38 FL |
268 | |
269 | /* calculated power */ | |
f0df0fd9 GR |
270 | static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL, |
271 | INA2XX_POWER); | |
f7c2fe38 FL |
272 | |
273 | /* pointers to created device attributes */ | |
468bf0e3 | 274 | static struct attribute *ina2xx_attrs[] = { |
f7c2fe38 FL |
275 | &sensor_dev_attr_in0_input.dev_attr.attr, |
276 | &sensor_dev_attr_in1_input.dev_attr.attr, | |
277 | &sensor_dev_attr_curr1_input.dev_attr.attr, | |
278 | &sensor_dev_attr_power1_input.dev_attr.attr, | |
279 | NULL, | |
280 | }; | |
468bf0e3 | 281 | ATTRIBUTE_GROUPS(ina2xx); |
f7c2fe38 FL |
282 | |
283 | static int ina2xx_probe(struct i2c_client *client, | |
284 | const struct i2c_device_id *id) | |
285 | { | |
286 | struct i2c_adapter *adapter = client->adapter; | |
f7c2fe38 | 287 | struct ina2xx_platform_data *pdata; |
468bf0e3 GR |
288 | struct device *dev = &client->dev; |
289 | struct ina2xx_data *data; | |
290 | struct device *hwmon_dev; | |
468bf0e3 | 291 | u32 val; |
f975b339 | 292 | int ret; |
f7c2fe38 FL |
293 | |
294 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) | |
295 | return -ENODEV; | |
296 | ||
468bf0e3 | 297 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
f7c2fe38 FL |
298 | if (!data) |
299 | return -ENOMEM; | |
300 | ||
468bf0e3 GR |
301 | if (dev_get_platdata(dev)) { |
302 | pdata = dev_get_platdata(dev); | |
509416a8 | 303 | data->rshunt = pdata->shunt_uohms; |
468bf0e3 GR |
304 | } else if (!of_property_read_u32(dev->of_node, |
305 | "shunt-resistor", &val)) { | |
509416a8 BG |
306 | data->rshunt = val; |
307 | } else { | |
308 | data->rshunt = INA2XX_RSHUNT_DEFAULT; | |
f7c2fe38 FL |
309 | } |
310 | ||
f7c2fe38 FL |
311 | /* set the device type */ |
312 | data->kind = id->driver_data; | |
6106db25 | 313 | data->config = &ina2xx_config[data->kind]; |
509416a8 | 314 | data->client = client; |
f7c2fe38 | 315 | |
509416a8 | 316 | if (data->rshunt <= 0) |
f975b339 | 317 | return -ENODEV; |
f975b339 | 318 | |
509416a8 | 319 | ret = ina2xx_init(data); |
f975b339 | 320 | if (ret < 0) { |
509416a8 | 321 | dev_err(dev, "error configuring the device: %d\n", ret); |
f975b339 BG |
322 | return -ENODEV; |
323 | } | |
f7c2fe38 | 324 | |
f7c2fe38 FL |
325 | mutex_init(&data->update_lock); |
326 | ||
468bf0e3 GR |
327 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
328 | data, ina2xx_groups); | |
329 | if (IS_ERR(hwmon_dev)) | |
330 | return PTR_ERR(hwmon_dev); | |
f7c2fe38 | 331 | |
468bf0e3 | 332 | dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n", |
509416a8 | 333 | id->name, data->rshunt); |
6106db25 | 334 | |
f7c2fe38 | 335 | return 0; |
f7c2fe38 FL |
336 | } |
337 | ||
338 | static const struct i2c_device_id ina2xx_id[] = { | |
339 | { "ina219", ina219 }, | |
dc92cd0c | 340 | { "ina220", ina219 }, |
f7c2fe38 | 341 | { "ina226", ina226 }, |
dc92cd0c | 342 | { "ina230", ina226 }, |
f7c2fe38 FL |
343 | { } |
344 | }; | |
345 | MODULE_DEVICE_TABLE(i2c, ina2xx_id); | |
346 | ||
347 | static struct i2c_driver ina2xx_driver = { | |
348 | .driver = { | |
349 | .name = "ina2xx", | |
350 | }, | |
351 | .probe = ina2xx_probe, | |
f7c2fe38 FL |
352 | .id_table = ina2xx_id, |
353 | }; | |
354 | ||
d835ca0f | 355 | module_i2c_driver(ina2xx_driver); |
f7c2fe38 FL |
356 | |
357 | MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>"); | |
358 | MODULE_DESCRIPTION("ina2xx driver"); | |
359 | MODULE_LICENSE("GPL"); |