]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/hwmon/ltc4245.c
hwmon: (ltc4245) Read only one GPIO pin
[net-next-2.6.git] / drivers / hwmon / ltc4245.c
CommitLineData
6e34b187
IS
1/*
2 * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller
3 *
4 * Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This driver is based on the ds1621 and ina209 drivers.
11 *
12 * Datasheet:
13 * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1140,P19392,D13517
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/err.h>
20#include <linux/slab.h>
21#include <linux/i2c.h>
22#include <linux/hwmon.h>
23#include <linux/hwmon-sysfs.h>
24
6e34b187
IS
25/* Here are names of the chip's registers (a.k.a. commands) */
26enum ltc4245_cmd {
27 LTC4245_STATUS = 0x00, /* readonly */
28 LTC4245_ALERT = 0x01,
29 LTC4245_CONTROL = 0x02,
30 LTC4245_ON = 0x03,
31 LTC4245_FAULT1 = 0x04,
32 LTC4245_FAULT2 = 0x05,
33 LTC4245_GPIO = 0x06,
34 LTC4245_ADCADR = 0x07,
35
36 LTC4245_12VIN = 0x10,
37 LTC4245_12VSENSE = 0x11,
38 LTC4245_12VOUT = 0x12,
39 LTC4245_5VIN = 0x13,
40 LTC4245_5VSENSE = 0x14,
41 LTC4245_5VOUT = 0x15,
42 LTC4245_3VIN = 0x16,
43 LTC4245_3VSENSE = 0x17,
44 LTC4245_3VOUT = 0x18,
45 LTC4245_VEEIN = 0x19,
46 LTC4245_VEESENSE = 0x1a,
47 LTC4245_VEEOUT = 0x1b,
df16dd53 48 LTC4245_GPIOADC = 0x1c,
6e34b187
IS
49};
50
51struct ltc4245_data {
52 struct device *hwmon_dev;
53
54 struct mutex update_lock;
55 bool valid;
56 unsigned long last_updated; /* in jiffies */
57
58 /* Control registers */
59 u8 cregs[0x08];
60
61 /* Voltage registers */
df16dd53 62 u8 vregs[0x0d];
6e34b187
IS
63};
64
65static struct ltc4245_data *ltc4245_update_device(struct device *dev)
66{
67 struct i2c_client *client = to_i2c_client(dev);
68 struct ltc4245_data *data = i2c_get_clientdata(client);
69 s32 val;
70 int i;
71
72 mutex_lock(&data->update_lock);
73
74 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
75
76 dev_dbg(&client->dev, "Starting ltc4245 update\n");
77
78 /* Read control registers -- 0x00 to 0x07 */
79 for (i = 0; i < ARRAY_SIZE(data->cregs); i++) {
80 val = i2c_smbus_read_byte_data(client, i);
81 if (unlikely(val < 0))
82 data->cregs[i] = 0;
83 else
84 data->cregs[i] = val;
85 }
86
df16dd53 87 /* Read voltage registers -- 0x10 to 0x1c */
6e34b187
IS
88 for (i = 0; i < ARRAY_SIZE(data->vregs); i++) {
89 val = i2c_smbus_read_byte_data(client, i+0x10);
90 if (unlikely(val < 0))
91 data->vregs[i] = 0;
92 else
93 data->vregs[i] = val;
94 }
95
96 data->last_updated = jiffies;
97 data->valid = 1;
98 }
99
100 mutex_unlock(&data->update_lock);
101
102 return data;
103}
104
105/* Return the voltage from the given register in millivolts */
106static int ltc4245_get_voltage(struct device *dev, u8 reg)
107{
108 struct ltc4245_data *data = ltc4245_update_device(dev);
109 const u8 regval = data->vregs[reg - 0x10];
110 u32 voltage = 0;
111
112 switch (reg) {
113 case LTC4245_12VIN:
114 case LTC4245_12VOUT:
115 voltage = regval * 55;
116 break;
117 case LTC4245_5VIN:
118 case LTC4245_5VOUT:
119 voltage = regval * 22;
120 break;
121 case LTC4245_3VIN:
122 case LTC4245_3VOUT:
123 voltage = regval * 15;
124 break;
125 case LTC4245_VEEIN:
126 case LTC4245_VEEOUT:
127 voltage = regval * -55;
128 break;
df16dd53 129 case LTC4245_GPIOADC:
6e34b187
IS
130 voltage = regval * 10;
131 break;
132 default:
133 /* If we get here, the developer messed up */
134 WARN_ON_ONCE(1);
135 break;
136 }
137
138 return voltage;
139}
140
141/* Return the current in the given sense register in milliAmperes */
142static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
143{
144 struct ltc4245_data *data = ltc4245_update_device(dev);
145 const u8 regval = data->vregs[reg - 0x10];
146 unsigned int voltage;
147 unsigned int curr;
148
149 /* The strange looking conversions that follow are fixed-point
150 * math, since we cannot do floating point in the kernel.
151 *
152 * Step 1: convert sense register to microVolts
153 * Step 2: convert voltage to milliAmperes
154 *
155 * If you play around with the V=IR equation, you come up with
156 * the following: X uV / Y mOhm == Z mA
157 *
158 * With the resistors that are fractions of a milliOhm, we multiply
159 * the voltage and resistance by 10, to shift the decimal point.
160 * Now we can use the normal division operator again.
161 */
162
163 switch (reg) {
164 case LTC4245_12VSENSE:
165 voltage = regval * 250; /* voltage in uV */
166 curr = voltage / 50; /* sense resistor 50 mOhm */
167 break;
168 case LTC4245_5VSENSE:
169 voltage = regval * 125; /* voltage in uV */
170 curr = (voltage * 10) / 35; /* sense resistor 3.5 mOhm */
171 break;
172 case LTC4245_3VSENSE:
173 voltage = regval * 125; /* voltage in uV */
174 curr = (voltage * 10) / 25; /* sense resistor 2.5 mOhm */
175 break;
176 case LTC4245_VEESENSE:
177 voltage = regval * 250; /* voltage in uV */
178 curr = voltage / 100; /* sense resistor 100 mOhm */
179 break;
180 default:
181 /* If we get here, the developer messed up */
182 WARN_ON_ONCE(1);
183 curr = 0;
184 break;
185 }
186
187 return curr;
188}
189
190static ssize_t ltc4245_show_voltage(struct device *dev,
191 struct device_attribute *da,
192 char *buf)
193{
194 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
195 const int voltage = ltc4245_get_voltage(dev, attr->index);
196
197 return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
198}
199
200static ssize_t ltc4245_show_current(struct device *dev,
201 struct device_attribute *da,
202 char *buf)
203{
204 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
205 const unsigned int curr = ltc4245_get_current(dev, attr->index);
206
207 return snprintf(buf, PAGE_SIZE, "%u\n", curr);
208}
209
210static ssize_t ltc4245_show_power(struct device *dev,
211 struct device_attribute *da,
212 char *buf)
213{
214 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
215 const unsigned int curr = ltc4245_get_current(dev, attr->index);
216 const int output_voltage = ltc4245_get_voltage(dev, attr->index+1);
217
218 /* current in mA * voltage in mV == power in uW */
219 const unsigned int power = abs(output_voltage * curr);
220
221 return snprintf(buf, PAGE_SIZE, "%u\n", power);
222}
223
224static ssize_t ltc4245_show_alarm(struct device *dev,
225 struct device_attribute *da,
226 char *buf)
227{
228 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
229 struct ltc4245_data *data = ltc4245_update_device(dev);
230 const u8 reg = data->cregs[attr->index];
231 const u32 mask = attr->nr;
232
233 return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
234}
235
236/* These macros are used below in constructing device attribute objects
237 * for use with sysfs_create_group() to make a sysfs device file
238 * for each register.
239 */
240
241#define LTC4245_VOLTAGE(name, ltc4245_cmd_idx) \
242 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
243 ltc4245_show_voltage, NULL, ltc4245_cmd_idx)
244
245#define LTC4245_CURRENT(name, ltc4245_cmd_idx) \
246 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
247 ltc4245_show_current, NULL, ltc4245_cmd_idx)
248
249#define LTC4245_POWER(name, ltc4245_cmd_idx) \
250 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
251 ltc4245_show_power, NULL, ltc4245_cmd_idx)
252
253#define LTC4245_ALARM(name, mask, reg) \
254 static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \
255 ltc4245_show_alarm, NULL, (mask), reg)
256
257/* Construct a sensor_device_attribute structure for each register */
258
259/* Input voltages */
260LTC4245_VOLTAGE(in1_input, LTC4245_12VIN);
261LTC4245_VOLTAGE(in2_input, LTC4245_5VIN);
262LTC4245_VOLTAGE(in3_input, LTC4245_3VIN);
263LTC4245_VOLTAGE(in4_input, LTC4245_VEEIN);
264
265/* Input undervoltage alarms */
266LTC4245_ALARM(in1_min_alarm, (1 << 0), LTC4245_FAULT1);
267LTC4245_ALARM(in2_min_alarm, (1 << 1), LTC4245_FAULT1);
268LTC4245_ALARM(in3_min_alarm, (1 << 2), LTC4245_FAULT1);
269LTC4245_ALARM(in4_min_alarm, (1 << 3), LTC4245_FAULT1);
270
271/* Currents (via sense resistor) */
272LTC4245_CURRENT(curr1_input, LTC4245_12VSENSE);
273LTC4245_CURRENT(curr2_input, LTC4245_5VSENSE);
274LTC4245_CURRENT(curr3_input, LTC4245_3VSENSE);
275LTC4245_CURRENT(curr4_input, LTC4245_VEESENSE);
276
277/* Overcurrent alarms */
278LTC4245_ALARM(curr1_max_alarm, (1 << 4), LTC4245_FAULT1);
279LTC4245_ALARM(curr2_max_alarm, (1 << 5), LTC4245_FAULT1);
280LTC4245_ALARM(curr3_max_alarm, (1 << 6), LTC4245_FAULT1);
281LTC4245_ALARM(curr4_max_alarm, (1 << 7), LTC4245_FAULT1);
282
283/* Output voltages */
284LTC4245_VOLTAGE(in5_input, LTC4245_12VOUT);
285LTC4245_VOLTAGE(in6_input, LTC4245_5VOUT);
286LTC4245_VOLTAGE(in7_input, LTC4245_3VOUT);
287LTC4245_VOLTAGE(in8_input, LTC4245_VEEOUT);
288
289/* Power Bad alarms */
290LTC4245_ALARM(in5_min_alarm, (1 << 0), LTC4245_FAULT2);
291LTC4245_ALARM(in6_min_alarm, (1 << 1), LTC4245_FAULT2);
292LTC4245_ALARM(in7_min_alarm, (1 << 2), LTC4245_FAULT2);
293LTC4245_ALARM(in8_min_alarm, (1 << 3), LTC4245_FAULT2);
294
295/* GPIO voltages */
df16dd53 296LTC4245_VOLTAGE(in9_input, LTC4245_GPIOADC);
6e34b187
IS
297
298/* Power Consumption (virtual) */
299LTC4245_POWER(power1_input, LTC4245_12VSENSE);
300LTC4245_POWER(power2_input, LTC4245_5VSENSE);
301LTC4245_POWER(power3_input, LTC4245_3VSENSE);
302LTC4245_POWER(power4_input, LTC4245_VEESENSE);
303
304/* Finally, construct an array of pointers to members of the above objects,
305 * as required for sysfs_create_group()
306 */
307static struct attribute *ltc4245_attributes[] = {
308 &sensor_dev_attr_in1_input.dev_attr.attr,
309 &sensor_dev_attr_in2_input.dev_attr.attr,
310 &sensor_dev_attr_in3_input.dev_attr.attr,
311 &sensor_dev_attr_in4_input.dev_attr.attr,
312
313 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
314 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
315 &sensor_dev_attr_in3_min_alarm.dev_attr.attr,
316 &sensor_dev_attr_in4_min_alarm.dev_attr.attr,
317
318 &sensor_dev_attr_curr1_input.dev_attr.attr,
319 &sensor_dev_attr_curr2_input.dev_attr.attr,
320 &sensor_dev_attr_curr3_input.dev_attr.attr,
321 &sensor_dev_attr_curr4_input.dev_attr.attr,
322
323 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
324 &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
325 &sensor_dev_attr_curr3_max_alarm.dev_attr.attr,
326 &sensor_dev_attr_curr4_max_alarm.dev_attr.attr,
327
328 &sensor_dev_attr_in5_input.dev_attr.attr,
329 &sensor_dev_attr_in6_input.dev_attr.attr,
330 &sensor_dev_attr_in7_input.dev_attr.attr,
331 &sensor_dev_attr_in8_input.dev_attr.attr,
332
333 &sensor_dev_attr_in5_min_alarm.dev_attr.attr,
334 &sensor_dev_attr_in6_min_alarm.dev_attr.attr,
335 &sensor_dev_attr_in7_min_alarm.dev_attr.attr,
336 &sensor_dev_attr_in8_min_alarm.dev_attr.attr,
337
338 &sensor_dev_attr_in9_input.dev_attr.attr,
6e34b187
IS
339
340 &sensor_dev_attr_power1_input.dev_attr.attr,
341 &sensor_dev_attr_power2_input.dev_attr.attr,
342 &sensor_dev_attr_power3_input.dev_attr.attr,
343 &sensor_dev_attr_power4_input.dev_attr.attr,
344
345 NULL,
346};
347
348static const struct attribute_group ltc4245_group = {
349 .attrs = ltc4245_attributes,
350};
351
352static int ltc4245_probe(struct i2c_client *client,
353 const struct i2c_device_id *id)
354{
2d2a7cff 355 struct i2c_adapter *adapter = client->adapter;
6e34b187
IS
356 struct ltc4245_data *data;
357 int ret;
358
2d2a7cff
JD
359 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
360 return -ENODEV;
361
6e34b187
IS
362 data = kzalloc(sizeof(*data), GFP_KERNEL);
363 if (!data) {
364 ret = -ENOMEM;
365 goto out_kzalloc;
366 }
367
368 i2c_set_clientdata(client, data);
369 mutex_init(&data->update_lock);
370
371 /* Initialize the LTC4245 chip */
58f055e5
IS
372 i2c_smbus_write_byte_data(client, LTC4245_FAULT1, 0x00);
373 i2c_smbus_write_byte_data(client, LTC4245_FAULT2, 0x00);
6e34b187
IS
374
375 /* Register sysfs hooks */
376 ret = sysfs_create_group(&client->dev.kobj, &ltc4245_group);
377 if (ret)
378 goto out_sysfs_create_group;
379
380 data->hwmon_dev = hwmon_device_register(&client->dev);
381 if (IS_ERR(data->hwmon_dev)) {
382 ret = PTR_ERR(data->hwmon_dev);
383 goto out_hwmon_device_register;
384 }
385
386 return 0;
387
388out_hwmon_device_register:
389 sysfs_remove_group(&client->dev.kobj, &ltc4245_group);
390out_sysfs_create_group:
391 kfree(data);
392out_kzalloc:
393 return ret;
394}
395
396static int ltc4245_remove(struct i2c_client *client)
397{
398 struct ltc4245_data *data = i2c_get_clientdata(client);
399
400 hwmon_device_unregister(data->hwmon_dev);
401 sysfs_remove_group(&client->dev.kobj, &ltc4245_group);
402
403 kfree(data);
404
405 return 0;
406}
407
6e34b187 408static const struct i2c_device_id ltc4245_id[] = {
2d2a7cff 409 { "ltc4245", 0 },
6e34b187
IS
410 { }
411};
412MODULE_DEVICE_TABLE(i2c, ltc4245_id);
413
414/* This is the driver that will be inserted */
415static struct i2c_driver ltc4245_driver = {
6e34b187
IS
416 .driver = {
417 .name = "ltc4245",
418 },
419 .probe = ltc4245_probe,
420 .remove = ltc4245_remove,
421 .id_table = ltc4245_id,
6e34b187
IS
422};
423
424static int __init ltc4245_init(void)
425{
426 return i2c_add_driver(&ltc4245_driver);
427}
428
429static void __exit ltc4245_exit(void)
430{
431 i2c_del_driver(&ltc4245_driver);
432}
433
434MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
435MODULE_DESCRIPTION("LTC4245 driver");
436MODULE_LICENSE("GPL");
437
438module_init(ltc4245_init);
439module_exit(ltc4245_exit);