Merge branch 'linux-next' of git://git.open-osd.org/linux-open-osd
[deliverable/linux.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>
36
37#include <linux/platform_data/ina2xx.h>
38
39/* common register definitions */
40#define INA2XX_CONFIG 0x00
41#define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
42#define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
43#define INA2XX_POWER 0x03 /* readonly */
44#define INA2XX_CURRENT 0x04 /* readonly */
45#define INA2XX_CALIBRATION 0x05
46
47/* INA226 register definitions */
48#define INA226_MASK_ENABLE 0x06
49#define INA226_ALERT_LIMIT 0x07
50#define INA226_DIE_ID 0xFF
51
52
53/* register count */
54#define INA219_REGISTERS 6
55#define INA226_REGISTERS 8
56
57#define INA2XX_MAX_REGISTERS 8
58
59/* settings - depend on use case */
60#define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
61#define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
62
63/* worst case is 68.10 ms (~14.6Hz, ina219) */
64#define INA2XX_CONVERSION_RATE 15
65
66enum ina2xx_ids { ina219, ina226 };
67
6106db25
GR
68struct ina2xx_config {
69 u16 config_default;
70 int calibration_factor;
71 int registers;
72 int shunt_div;
73 int bus_voltage_shift;
74 int bus_voltage_lsb; /* uV */
75 int power_lsb; /* uW */
76};
77
f7c2fe38
FL
78struct ina2xx_data {
79 struct device *hwmon_dev;
6106db25 80 const struct ina2xx_config *config;
f7c2fe38
FL
81
82 struct mutex update_lock;
83 bool valid;
84 unsigned long last_updated;
85
86 int kind;
f7c2fe38
FL
87 u16 regs[INA2XX_MAX_REGISTERS];
88};
89
6106db25
GR
90static const struct ina2xx_config ina2xx_config[] = {
91 [ina219] = {
92 .config_default = INA219_CONFIG_DEFAULT,
93 .calibration_factor = 40960000,
94 .registers = INA219_REGISTERS,
95 .shunt_div = 100,
96 .bus_voltage_shift = 3,
97 .bus_voltage_lsb = 4000,
98 .power_lsb = 20000,
99 },
100 [ina226] = {
101 .config_default = INA226_CONFIG_DEFAULT,
102 .calibration_factor = 5120000,
103 .registers = INA226_REGISTERS,
104 .shunt_div = 400,
105 .bus_voltage_shift = 0,
106 .bus_voltage_lsb = 1250,
107 .power_lsb = 25000,
108 },
109};
110
f7c2fe38
FL
111static struct ina2xx_data *ina2xx_update_device(struct device *dev)
112{
113 struct i2c_client *client = to_i2c_client(dev);
114 struct ina2xx_data *data = i2c_get_clientdata(client);
115 struct ina2xx_data *ret = data;
116
117 mutex_lock(&data->update_lock);
118
119 if (time_after(jiffies, data->last_updated +
120 HZ / INA2XX_CONVERSION_RATE) || !data->valid) {
121
122 int i;
123
124 dev_dbg(&client->dev, "Starting ina2xx update\n");
125
126 /* Read all registers */
6106db25 127 for (i = 0; i < data->config->registers; i++) {
080b98e9 128 int rv = i2c_smbus_read_word_swapped(client, i);
f7c2fe38
FL
129 if (rv < 0) {
130 ret = ERR_PTR(rv);
131 goto abort;
132 }
133 data->regs[i] = rv;
134 }
135 data->last_updated = jiffies;
136 data->valid = 1;
137 }
138abort:
139 mutex_unlock(&data->update_lock);
140 return ret;
141}
142
6106db25 143static int ina2xx_get_value(struct ina2xx_data *data, u8 reg)
f7c2fe38 144{
6106db25 145 int val;
f7c2fe38
FL
146
147 switch (reg) {
148 case INA2XX_SHUNT_VOLTAGE:
6106db25
GR
149 val = DIV_ROUND_CLOSEST(data->regs[reg],
150 data->config->shunt_div);
f7c2fe38
FL
151 break;
152 case INA2XX_BUS_VOLTAGE:
6106db25
GR
153 val = (data->regs[reg] >> data->config->bus_voltage_shift)
154 * data->config->bus_voltage_lsb;
155 val = DIV_ROUND_CLOSEST(val, 1000);
f7c2fe38
FL
156 break;
157 case INA2XX_POWER:
6106db25 158 val = data->regs[reg] * data->config->power_lsb;
f7c2fe38
FL
159 break;
160 case INA2XX_CURRENT:
161 /* LSB=1mA (selected). Is in mA */
6106db25 162 val = data->regs[reg];
f7c2fe38
FL
163 break;
164 default:
165 /* programmer goofed */
166 WARN_ON_ONCE(1);
167 val = 0;
168 break;
169 }
170
171 return val;
172}
173
174static ssize_t ina2xx_show_value(struct device *dev,
175 struct device_attribute *da, char *buf)
176{
177 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
178 struct ina2xx_data *data = ina2xx_update_device(dev);
f7c2fe38
FL
179
180 if (IS_ERR(data))
181 return PTR_ERR(data);
182
6106db25
GR
183 return snprintf(buf, PAGE_SIZE, "%d\n",
184 ina2xx_get_value(data, attr->index));
f7c2fe38
FL
185}
186
187/* shunt voltage */
188static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, \
189 ina2xx_show_value, NULL, INA2XX_SHUNT_VOLTAGE);
190
191/* bus voltage */
192static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, \
193 ina2xx_show_value, NULL, INA2XX_BUS_VOLTAGE);
194
195/* calculated current */
196static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, \
197 ina2xx_show_value, NULL, INA2XX_CURRENT);
198
199/* calculated power */
200static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, \
201 ina2xx_show_value, NULL, INA2XX_POWER);
202
203/* pointers to created device attributes */
204static struct attribute *ina2xx_attributes[] = {
205 &sensor_dev_attr_in0_input.dev_attr.attr,
206 &sensor_dev_attr_in1_input.dev_attr.attr,
207 &sensor_dev_attr_curr1_input.dev_attr.attr,
208 &sensor_dev_attr_power1_input.dev_attr.attr,
209 NULL,
210};
211
212static const struct attribute_group ina2xx_group = {
213 .attrs = ina2xx_attributes,
214};
215
216static int ina2xx_probe(struct i2c_client *client,
217 const struct i2c_device_id *id)
218{
219 struct i2c_adapter *adapter = client->adapter;
220 struct ina2xx_data *data;
221 struct ina2xx_platform_data *pdata;
6106db25 222 int ret;
f7c2fe38
FL
223 long shunt = 10000; /* default shunt value 10mOhms */
224
225 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
226 return -ENODEV;
227
228 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
229 if (!data)
230 return -ENOMEM;
231
232 if (client->dev.platform_data) {
233 pdata =
234 (struct ina2xx_platform_data *)client->dev.platform_data;
235 shunt = pdata->shunt_uohms;
236 }
237
238 if (shunt <= 0)
239 return -ENODEV;
240
241 /* set the device type */
242 data->kind = id->driver_data;
6106db25 243 data->config = &ina2xx_config[data->kind];
f7c2fe38 244
6106db25
GR
245 /* device configuration */
246 i2c_smbus_write_word_swapped(client, INA2XX_CONFIG,
247 data->config->config_default);
248 /* set current LSB to 1mA, shunt is in uOhms */
249 /* (equation 13 in datasheet) */
250 i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION,
251 data->config->calibration_factor / shunt);
f7c2fe38
FL
252
253 i2c_set_clientdata(client, data);
254 mutex_init(&data->update_lock);
255
256 ret = sysfs_create_group(&client->dev.kobj, &ina2xx_group);
257 if (ret)
258 return ret;
259
260 data->hwmon_dev = hwmon_device_register(&client->dev);
261 if (IS_ERR(data->hwmon_dev)) {
262 ret = PTR_ERR(data->hwmon_dev);
263 goto out_err_hwmon;
264 }
265
6106db25
GR
266 dev_info(&client->dev, "power monitor %s (Rshunt = %li uOhm)\n",
267 id->name, shunt);
268
f7c2fe38
FL
269 return 0;
270
271out_err_hwmon:
272 sysfs_remove_group(&client->dev.kobj, &ina2xx_group);
273 return ret;
274}
275
276static int ina2xx_remove(struct i2c_client *client)
277{
278 struct ina2xx_data *data = i2c_get_clientdata(client);
279
280 hwmon_device_unregister(data->hwmon_dev);
281 sysfs_remove_group(&client->dev.kobj, &ina2xx_group);
282
283 return 0;
284}
285
286static const struct i2c_device_id ina2xx_id[] = {
287 { "ina219", ina219 },
dc92cd0c 288 { "ina220", ina219 },
f7c2fe38 289 { "ina226", ina226 },
dc92cd0c 290 { "ina230", ina226 },
f7c2fe38
FL
291 { }
292};
293MODULE_DEVICE_TABLE(i2c, ina2xx_id);
294
295static struct i2c_driver ina2xx_driver = {
296 .driver = {
297 .name = "ina2xx",
298 },
299 .probe = ina2xx_probe,
300 .remove = ina2xx_remove,
301 .id_table = ina2xx_id,
302};
303
304static int __init ina2xx_init(void)
305{
306 return i2c_add_driver(&ina2xx_driver);
307}
308
309static void __exit ina2xx_exit(void)
310{
311 i2c_del_driver(&ina2xx_driver);
312}
313
314MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
315MODULE_DESCRIPTION("ina2xx driver");
316MODULE_LICENSE("GPL");
317
318module_init(ina2xx_init);
319module_exit(ina2xx_exit);
This page took 0.06969 seconds and 5 git commands to generate.