hwmon: (ina2xx) remove an unnecessary dev_get_drvdata() result check
[linux-2.6-block.git] / drivers / hwmon / ina2xx.c
CommitLineData
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 70
72a87a47
BG
71/* bit mask for reading the averaging setting in the configuration register */
72#define INA226_AVG_RD_MASK 0x0E00
73
74#define INA226_READ_AVG(reg) (((reg) & INA226_AVG_RD_MASK) >> 9)
75#define INA226_SHIFT_AVG(val) ((val) << 9)
76
77/* common attrs, ina226 attrs and NULL */
78#define INA2XX_MAX_ATTRIBUTE_GROUPS 3
79
80/*
81 * Both bus voltage and shunt voltage conversion times for ina226 are set
82 * to 0b0100 on POR, which translates to 2200 microseconds in total.
83 */
84#define INA226_TOTAL_CONV_TIME_DEFAULT 2200
85
f7c2fe38
FL
86enum ina2xx_ids { ina219, ina226 };
87
6106db25
GR
88struct ina2xx_config {
89 u16 config_default;
90 int calibration_factor;
91 int registers;
92 int shunt_div;
93 int bus_voltage_shift;
94 int bus_voltage_lsb; /* uV */
95 int power_lsb; /* uW */
96};
97
f7c2fe38 98struct ina2xx_data {
468bf0e3 99 struct i2c_client *client;
6106db25 100 const struct ina2xx_config *config;
f7c2fe38 101
509416a8 102 long rshunt;
72a87a47 103 u16 curr_config;
509416a8 104
f7c2fe38
FL
105 struct mutex update_lock;
106 bool valid;
107 unsigned long last_updated;
72a87a47 108 int update_interval; /* in jiffies */
f7c2fe38
FL
109
110 int kind;
72a87a47 111 const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
f7c2fe38
FL
112 u16 regs[INA2XX_MAX_REGISTERS];
113};
114
6106db25
GR
115static const struct ina2xx_config ina2xx_config[] = {
116 [ina219] = {
117 .config_default = INA219_CONFIG_DEFAULT,
118 .calibration_factor = 40960000,
119 .registers = INA219_REGISTERS,
120 .shunt_div = 100,
121 .bus_voltage_shift = 3,
122 .bus_voltage_lsb = 4000,
123 .power_lsb = 20000,
124 },
125 [ina226] = {
126 .config_default = INA226_CONFIG_DEFAULT,
127 .calibration_factor = 5120000,
128 .registers = INA226_REGISTERS,
129 .shunt_div = 400,
130 .bus_voltage_shift = 0,
131 .bus_voltage_lsb = 1250,
132 .power_lsb = 25000,
133 },
134};
135
72a87a47
BG
136/*
137 * Available averaging rates for ina226. The indices correspond with
138 * the bit values expected by the chip (according to the ina226 datasheet,
139 * table 3 AVG bit settings, found at
140 * http://www.ti.com/lit/ds/symlink/ina226.pdf.
141 */
142static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
143
144static int ina226_avg_bits(int avg)
145{
146 int i;
147
148 /* Get the closest average from the tab. */
149 for (i = 0; i < ARRAY_SIZE(ina226_avg_tab) - 1; i++) {
150 if (avg <= (ina226_avg_tab[i] + ina226_avg_tab[i + 1]) / 2)
151 break;
152 }
153
154 return i; /* Return 0b0111 for values greater than 1024. */
155}
156
157static int ina226_reg_to_interval(u16 config)
158{
159 int avg = ina226_avg_tab[INA226_READ_AVG(config)];
160
161 /*
162 * Multiply the total conversion time by the number of averages.
163 * Return the result in milliseconds.
164 */
165 return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
166}
167
168static u16 ina226_interval_to_reg(int interval, u16 config)
169{
170 int avg, avg_bits;
171
172 avg = DIV_ROUND_CLOSEST(interval * 1000,
173 INA226_TOTAL_CONV_TIME_DEFAULT);
174 avg_bits = ina226_avg_bits(avg);
175
176 return (config & ~INA226_AVG_RD_MASK) | INA226_SHIFT_AVG(avg_bits);
177}
178
179static void ina226_set_update_interval(struct ina2xx_data *data)
180{
181 int ms;
182
183 ms = ina226_reg_to_interval(data->curr_config);
184 data->update_interval = msecs_to_jiffies(ms);
185}
186
8a5fc795
BG
187static int ina2xx_calibrate(struct ina2xx_data *data)
188{
189 return i2c_smbus_write_word_swapped(data->client, INA2XX_CALIBRATION,
190 data->config->calibration_factor / data->rshunt);
191}
192
509416a8
BG
193/*
194 * Initialize the configuration and calibration registers.
195 */
196static int ina2xx_init(struct ina2xx_data *data)
f7c2fe38 197{
468bf0e3 198 struct i2c_client *client = data->client;
509416a8 199 int ret;
f7c2fe38 200
509416a8
BG
201 /* device configuration */
202 ret = i2c_smbus_write_word_swapped(client, INA2XX_CONFIG,
72a87a47 203 data->curr_config);
509416a8
BG
204 if (ret < 0)
205 return ret;
f7c2fe38 206
509416a8
BG
207 /*
208 * Set current LSB to 1mA, shunt is in uOhms
209 * (equation 13 in datasheet).
210 */
8a5fc795 211 return ina2xx_calibrate(data);
509416a8 212}
f7c2fe38 213
509416a8
BG
214static int ina2xx_do_update(struct device *dev)
215{
216 struct ina2xx_data *data = dev_get_drvdata(dev);
217 struct i2c_client *client = data->client;
218 int i, rv, retry;
f7c2fe38 219
509416a8 220 dev_dbg(&client->dev, "Starting ina2xx update\n");
f7c2fe38 221
509416a8 222 for (retry = 5; retry; retry--) {
f7c2fe38 223 /* Read all registers */
6106db25 224 for (i = 0; i < data->config->registers; i++) {
509416a8
BG
225 rv = i2c_smbus_read_word_swapped(client, i);
226 if (rv < 0)
227 return rv;
f7c2fe38
FL
228 data->regs[i] = rv;
229 }
509416a8
BG
230
231 /*
232 * If the current value in the calibration register is 0, the
233 * power and current registers will also remain at 0. In case
234 * the chip has been reset let's check the calibration
235 * register and reinitialize if needed.
236 */
237 if (data->regs[INA2XX_CALIBRATION] == 0) {
238 dev_warn(dev, "chip not calibrated, reinitializing\n");
239
240 rv = ina2xx_init(data);
241 if (rv < 0)
242 return rv;
243
244 /*
245 * Let's make sure the power and current registers
246 * have been updated before trying again.
247 */
248 msleep(INA2XX_MAX_DELAY);
249 continue;
250 }
251
f7c2fe38
FL
252 data->last_updated = jiffies;
253 data->valid = 1;
509416a8
BG
254
255 return 0;
f7c2fe38 256 }
509416a8
BG
257
258 /*
259 * If we're here then although all write operations succeeded, the
260 * chip still returns 0 in the calibration register. Nothing more we
261 * can do here.
262 */
263 dev_err(dev, "unable to reinitialize the chip\n");
264 return -ENODEV;
265}
266
267static struct ina2xx_data *ina2xx_update_device(struct device *dev)
268{
269 struct ina2xx_data *data = dev_get_drvdata(dev);
270 struct ina2xx_data *ret = data;
72a87a47 271 unsigned long after;
509416a8
BG
272 int rv;
273
274 mutex_lock(&data->update_lock);
275
72a87a47
BG
276 after = data->last_updated + data->update_interval;
277 if (time_after(jiffies, after) || !data->valid) {
509416a8
BG
278 rv = ina2xx_do_update(dev);
279 if (rv < 0)
280 ret = ERR_PTR(rv);
281 }
282
f7c2fe38
FL
283 mutex_unlock(&data->update_lock);
284 return ret;
285}
286
6106db25 287static int ina2xx_get_value(struct ina2xx_data *data, u8 reg)
f7c2fe38 288{
6106db25 289 int val;
f7c2fe38
FL
290
291 switch (reg) {
292 case INA2XX_SHUNT_VOLTAGE:
c0214f98
FB
293 /* signed register */
294 val = DIV_ROUND_CLOSEST((s16)data->regs[reg],
6106db25 295 data->config->shunt_div);
f7c2fe38
FL
296 break;
297 case INA2XX_BUS_VOLTAGE:
6106db25
GR
298 val = (data->regs[reg] >> data->config->bus_voltage_shift)
299 * data->config->bus_voltage_lsb;
300 val = DIV_ROUND_CLOSEST(val, 1000);
f7c2fe38
FL
301 break;
302 case INA2XX_POWER:
6106db25 303 val = data->regs[reg] * data->config->power_lsb;
f7c2fe38
FL
304 break;
305 case INA2XX_CURRENT:
c0214f98
FB
306 /* signed register, LSB=1mA (selected), in mA */
307 val = (s16)data->regs[reg];
f7c2fe38 308 break;
8a5fc795
BG
309 case INA2XX_CALIBRATION:
310 val = data->config->calibration_factor / data->regs[reg];
311 break;
f7c2fe38
FL
312 default:
313 /* programmer goofed */
314 WARN_ON_ONCE(1);
315 val = 0;
316 break;
317 }
318
319 return val;
320}
321
322static ssize_t ina2xx_show_value(struct device *dev,
323 struct device_attribute *da, char *buf)
324{
325 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
326 struct ina2xx_data *data = ina2xx_update_device(dev);
f7c2fe38
FL
327
328 if (IS_ERR(data))
329 return PTR_ERR(data);
330
6106db25
GR
331 return snprintf(buf, PAGE_SIZE, "%d\n",
332 ina2xx_get_value(data, attr->index));
f7c2fe38
FL
333}
334
8a5fc795
BG
335static ssize_t ina2xx_set_shunt(struct device *dev,
336 struct device_attribute *da,
337 const char *buf, size_t count)
338{
339 struct ina2xx_data *data = ina2xx_update_device(dev);
340 unsigned long val;
341 int status;
342
343 if (IS_ERR(data))
344 return PTR_ERR(data);
345
346 status = kstrtoul(buf, 10, &val);
347 if (status < 0)
348 return status;
349
350 if (val == 0 ||
351 /* Values greater than the calibration factor make no sense. */
352 val > data->config->calibration_factor)
353 return -EINVAL;
354
355 mutex_lock(&data->update_lock);
356 data->rshunt = val;
357 status = ina2xx_calibrate(data);
358 mutex_unlock(&data->update_lock);
359 if (status < 0)
360 return status;
361
362 return count;
363}
364
72a87a47
BG
365static ssize_t ina226_set_interval(struct device *dev,
366 struct device_attribute *da,
367 const char *buf, size_t count)
368{
369 struct ina2xx_data *data = dev_get_drvdata(dev);
370 unsigned long val;
371 int status;
372
72a87a47
BG
373 status = kstrtoul(buf, 10, &val);
374 if (status < 0)
375 return status;
376
377 if (val > INT_MAX || val == 0)
378 return -EINVAL;
379
380 mutex_lock(&data->update_lock);
381 data->curr_config = ina226_interval_to_reg(val,
382 data->regs[INA2XX_CONFIG]);
383 status = i2c_smbus_write_word_swapped(data->client,
384 INA2XX_CONFIG,
385 data->curr_config);
386
387 ina226_set_update_interval(data);
388 /* Make sure the next access re-reads all registers. */
389 data->valid = 0;
390 mutex_unlock(&data->update_lock);
391 if (status < 0)
392 return status;
393
394 return count;
395}
396
397static ssize_t ina226_show_interval(struct device *dev,
398 struct device_attribute *da, char *buf)
399{
400 struct ina2xx_data *data = ina2xx_update_device(dev);
401
402 if (IS_ERR(data))
403 return PTR_ERR(data);
404
405 /*
406 * We don't use data->update_interval here as we want to display
407 * the actual interval used by the chip and jiffies_to_msecs()
408 * doesn't seem to be accurate enough.
409 */
410 return snprintf(buf, PAGE_SIZE, "%d\n",
411 ina226_reg_to_interval(data->regs[INA2XX_CONFIG]));
412}
413
f7c2fe38 414/* shunt voltage */
f0df0fd9
GR
415static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina2xx_show_value, NULL,
416 INA2XX_SHUNT_VOLTAGE);
f7c2fe38
FL
417
418/* bus voltage */
f0df0fd9
GR
419static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina2xx_show_value, NULL,
420 INA2XX_BUS_VOLTAGE);
f7c2fe38
FL
421
422/* calculated current */
f0df0fd9
GR
423static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina2xx_show_value, NULL,
424 INA2XX_CURRENT);
f7c2fe38
FL
425
426/* calculated power */
f0df0fd9
GR
427static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina2xx_show_value, NULL,
428 INA2XX_POWER);
f7c2fe38 429
8a5fc795
BG
430/* shunt resistance */
431static SENSOR_DEVICE_ATTR(shunt_resistor, S_IRUGO | S_IWUSR,
432 ina2xx_show_value, ina2xx_set_shunt,
433 INA2XX_CALIBRATION);
434
72a87a47
BG
435/* update interval (ina226 only) */
436static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR,
437 ina226_show_interval, ina226_set_interval, 0);
438
f7c2fe38 439/* pointers to created device attributes */
468bf0e3 440static struct attribute *ina2xx_attrs[] = {
f7c2fe38
FL
441 &sensor_dev_attr_in0_input.dev_attr.attr,
442 &sensor_dev_attr_in1_input.dev_attr.attr,
443 &sensor_dev_attr_curr1_input.dev_attr.attr,
444 &sensor_dev_attr_power1_input.dev_attr.attr,
8a5fc795 445 &sensor_dev_attr_shunt_resistor.dev_attr.attr,
f7c2fe38
FL
446 NULL,
447};
72a87a47
BG
448
449static const struct attribute_group ina2xx_group = {
450 .attrs = ina2xx_attrs,
451};
452
453static struct attribute *ina226_attrs[] = {
454 &sensor_dev_attr_update_interval.dev_attr.attr,
455 NULL,
456};
457
458static const struct attribute_group ina226_group = {
459 .attrs = ina226_attrs,
460};
f7c2fe38
FL
461
462static int ina2xx_probe(struct i2c_client *client,
463 const struct i2c_device_id *id)
464{
465 struct i2c_adapter *adapter = client->adapter;
f7c2fe38 466 struct ina2xx_platform_data *pdata;
468bf0e3
GR
467 struct device *dev = &client->dev;
468 struct ina2xx_data *data;
469 struct device *hwmon_dev;
468bf0e3 470 u32 val;
72a87a47 471 int ret, group = 0;
f7c2fe38
FL
472
473 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
474 return -ENODEV;
475
468bf0e3 476 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
f7c2fe38
FL
477 if (!data)
478 return -ENOMEM;
479
468bf0e3
GR
480 if (dev_get_platdata(dev)) {
481 pdata = dev_get_platdata(dev);
509416a8 482 data->rshunt = pdata->shunt_uohms;
468bf0e3
GR
483 } else if (!of_property_read_u32(dev->of_node,
484 "shunt-resistor", &val)) {
509416a8
BG
485 data->rshunt = val;
486 } else {
487 data->rshunt = INA2XX_RSHUNT_DEFAULT;
f7c2fe38
FL
488 }
489
f7c2fe38
FL
490 /* set the device type */
491 data->kind = id->driver_data;
6106db25 492 data->config = &ina2xx_config[data->kind];
72a87a47 493 data->curr_config = data->config->config_default;
509416a8 494 data->client = client;
f7c2fe38 495
72a87a47
BG
496 /*
497 * Ina226 has a variable update_interval. For ina219 we
498 * use a constant value.
499 */
500 if (data->kind == ina226)
501 ina226_set_update_interval(data);
502 else
503 data->update_interval = HZ / INA2XX_CONVERSION_RATE;
504
e7947040
BG
505 if (data->rshunt <= 0 ||
506 data->rshunt > data->config->calibration_factor)
f975b339 507 return -ENODEV;
f975b339 508
509416a8 509 ret = ina2xx_init(data);
f975b339 510 if (ret < 0) {
509416a8 511 dev_err(dev, "error configuring the device: %d\n", ret);
f975b339
BG
512 return -ENODEV;
513 }
f7c2fe38 514
f7c2fe38
FL
515 mutex_init(&data->update_lock);
516
72a87a47
BG
517 data->groups[group++] = &ina2xx_group;
518 if (data->kind == ina226)
519 data->groups[group++] = &ina226_group;
520
468bf0e3 521 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
72a87a47 522 data, data->groups);
468bf0e3
GR
523 if (IS_ERR(hwmon_dev))
524 return PTR_ERR(hwmon_dev);
f7c2fe38 525
468bf0e3 526 dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
509416a8 527 id->name, data->rshunt);
6106db25 528
f7c2fe38 529 return 0;
f7c2fe38
FL
530}
531
532static const struct i2c_device_id ina2xx_id[] = {
533 { "ina219", ina219 },
dc92cd0c 534 { "ina220", ina219 },
f7c2fe38 535 { "ina226", ina226 },
dc92cd0c 536 { "ina230", ina226 },
f7c2fe38
FL
537 { }
538};
539MODULE_DEVICE_TABLE(i2c, ina2xx_id);
540
541static struct i2c_driver ina2xx_driver = {
542 .driver = {
543 .name = "ina2xx",
544 },
545 .probe = ina2xx_probe,
f7c2fe38
FL
546 .id_table = ina2xx_id,
547};
548
d835ca0f 549module_i2c_driver(ina2xx_driver);
f7c2fe38
FL
550
551MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
552MODULE_DESCRIPTION("ina2xx driver");
553MODULE_LICENSE("GPL");