thinkpad-acpi: constrain IBM-era support to IBM boxes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / ltc4245.c
blob21d201befc2cdb7aeebb11c495f5ba3c10e74215
1 /*
2 * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller
4 * Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu>
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.
10 * This driver is based on the ds1621 and ina209 drivers.
12 * Datasheet:
13 * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1140,P19392,D13517
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>
25 /* Here are names of the chip's registers (a.k.a. commands) */
26 enum 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,
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,
48 LTC4245_GPIOADC = 0x1c,
51 struct ltc4245_data {
52 struct device *hwmon_dev;
54 struct mutex update_lock;
55 bool valid;
56 unsigned long last_updated; /* in jiffies */
58 /* Control registers */
59 u8 cregs[0x08];
61 /* Voltage registers */
62 u8 vregs[0x0d];
65 static struct ltc4245_data *ltc4245_update_device(struct device *dev)
67 struct i2c_client *client = to_i2c_client(dev);
68 struct ltc4245_data *data = i2c_get_clientdata(client);
69 s32 val;
70 int i;
72 mutex_lock(&data->update_lock);
74 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
76 dev_dbg(&client->dev, "Starting ltc4245 update\n");
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;
87 /* Read voltage registers -- 0x10 to 0x1c */
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;
96 data->last_updated = jiffies;
97 data->valid = 1;
100 mutex_unlock(&data->update_lock);
102 return data;
105 /* Return the voltage from the given register in millivolts */
106 static int ltc4245_get_voltage(struct device *dev, u8 reg)
108 struct ltc4245_data *data = ltc4245_update_device(dev);
109 const u8 regval = data->vregs[reg - 0x10];
110 u32 voltage = 0;
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;
129 case LTC4245_GPIOADC:
130 voltage = regval * 10;
131 break;
132 default:
133 /* If we get here, the developer messed up */
134 WARN_ON_ONCE(1);
135 break;
138 return voltage;
141 /* Return the current in the given sense register in milliAmperes */
142 static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
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;
149 /* The strange looking conversions that follow are fixed-point
150 * math, since we cannot do floating point in the kernel.
152 * Step 1: convert sense register to microVolts
153 * Step 2: convert voltage to milliAmperes
155 * If you play around with the V=IR equation, you come up with
156 * the following: X uV / Y mOhm == Z mA
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.
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;
187 return curr;
190 static ssize_t ltc4245_show_voltage(struct device *dev,
191 struct device_attribute *da,
192 char *buf)
194 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
195 const int voltage = ltc4245_get_voltage(dev, attr->index);
197 return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
200 static ssize_t ltc4245_show_current(struct device *dev,
201 struct device_attribute *da,
202 char *buf)
204 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
205 const unsigned int curr = ltc4245_get_current(dev, attr->index);
207 return snprintf(buf, PAGE_SIZE, "%u\n", curr);
210 static ssize_t ltc4245_show_power(struct device *dev,
211 struct device_attribute *da,
212 char *buf)
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);
218 /* current in mA * voltage in mV == power in uW */
219 const unsigned int power = abs(output_voltage * curr);
221 return snprintf(buf, PAGE_SIZE, "%u\n", power);
224 static ssize_t ltc4245_show_alarm(struct device *dev,
225 struct device_attribute *da,
226 char *buf)
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;
233 return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
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.
241 #define LTC4245_VOLTAGE(name, ltc4245_cmd_idx) \
242 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
243 ltc4245_show_voltage, NULL, ltc4245_cmd_idx)
245 #define LTC4245_CURRENT(name, ltc4245_cmd_idx) \
246 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
247 ltc4245_show_current, NULL, ltc4245_cmd_idx)
249 #define LTC4245_POWER(name, ltc4245_cmd_idx) \
250 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
251 ltc4245_show_power, NULL, ltc4245_cmd_idx)
253 #define LTC4245_ALARM(name, mask, reg) \
254 static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \
255 ltc4245_show_alarm, NULL, (mask), reg)
257 /* Construct a sensor_device_attribute structure for each register */
259 /* Input voltages */
260 LTC4245_VOLTAGE(in1_input, LTC4245_12VIN);
261 LTC4245_VOLTAGE(in2_input, LTC4245_5VIN);
262 LTC4245_VOLTAGE(in3_input, LTC4245_3VIN);
263 LTC4245_VOLTAGE(in4_input, LTC4245_VEEIN);
265 /* Input undervoltage alarms */
266 LTC4245_ALARM(in1_min_alarm, (1 << 0), LTC4245_FAULT1);
267 LTC4245_ALARM(in2_min_alarm, (1 << 1), LTC4245_FAULT1);
268 LTC4245_ALARM(in3_min_alarm, (1 << 2), LTC4245_FAULT1);
269 LTC4245_ALARM(in4_min_alarm, (1 << 3), LTC4245_FAULT1);
271 /* Currents (via sense resistor) */
272 LTC4245_CURRENT(curr1_input, LTC4245_12VSENSE);
273 LTC4245_CURRENT(curr2_input, LTC4245_5VSENSE);
274 LTC4245_CURRENT(curr3_input, LTC4245_3VSENSE);
275 LTC4245_CURRENT(curr4_input, LTC4245_VEESENSE);
277 /* Overcurrent alarms */
278 LTC4245_ALARM(curr1_max_alarm, (1 << 4), LTC4245_FAULT1);
279 LTC4245_ALARM(curr2_max_alarm, (1 << 5), LTC4245_FAULT1);
280 LTC4245_ALARM(curr3_max_alarm, (1 << 6), LTC4245_FAULT1);
281 LTC4245_ALARM(curr4_max_alarm, (1 << 7), LTC4245_FAULT1);
283 /* Output voltages */
284 LTC4245_VOLTAGE(in5_input, LTC4245_12VOUT);
285 LTC4245_VOLTAGE(in6_input, LTC4245_5VOUT);
286 LTC4245_VOLTAGE(in7_input, LTC4245_3VOUT);
287 LTC4245_VOLTAGE(in8_input, LTC4245_VEEOUT);
289 /* Power Bad alarms */
290 LTC4245_ALARM(in5_min_alarm, (1 << 0), LTC4245_FAULT2);
291 LTC4245_ALARM(in6_min_alarm, (1 << 1), LTC4245_FAULT2);
292 LTC4245_ALARM(in7_min_alarm, (1 << 2), LTC4245_FAULT2);
293 LTC4245_ALARM(in8_min_alarm, (1 << 3), LTC4245_FAULT2);
295 /* GPIO voltages */
296 LTC4245_VOLTAGE(in9_input, LTC4245_GPIOADC);
298 /* Power Consumption (virtual) */
299 LTC4245_POWER(power1_input, LTC4245_12VSENSE);
300 LTC4245_POWER(power2_input, LTC4245_5VSENSE);
301 LTC4245_POWER(power3_input, LTC4245_3VSENSE);
302 LTC4245_POWER(power4_input, LTC4245_VEESENSE);
304 /* Finally, construct an array of pointers to members of the above objects,
305 * as required for sysfs_create_group()
307 static 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,
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,
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,
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,
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,
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,
338 &sensor_dev_attr_in9_input.dev_attr.attr,
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,
345 NULL,
348 static const struct attribute_group ltc4245_group = {
349 .attrs = ltc4245_attributes,
352 static int ltc4245_probe(struct i2c_client *client,
353 const struct i2c_device_id *id)
355 struct i2c_adapter *adapter = client->adapter;
356 struct ltc4245_data *data;
357 int ret;
359 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
360 return -ENODEV;
362 data = kzalloc(sizeof(*data), GFP_KERNEL);
363 if (!data) {
364 ret = -ENOMEM;
365 goto out_kzalloc;
368 i2c_set_clientdata(client, data);
369 mutex_init(&data->update_lock);
371 /* Initialize the LTC4245 chip */
372 i2c_smbus_write_byte_data(client, LTC4245_FAULT1, 0x00);
373 i2c_smbus_write_byte_data(client, LTC4245_FAULT2, 0x00);
375 /* Register sysfs hooks */
376 ret = sysfs_create_group(&client->dev.kobj, &ltc4245_group);
377 if (ret)
378 goto out_sysfs_create_group;
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;
386 return 0;
388 out_hwmon_device_register:
389 sysfs_remove_group(&client->dev.kobj, &ltc4245_group);
390 out_sysfs_create_group:
391 kfree(data);
392 out_kzalloc:
393 return ret;
396 static int ltc4245_remove(struct i2c_client *client)
398 struct ltc4245_data *data = i2c_get_clientdata(client);
400 hwmon_device_unregister(data->hwmon_dev);
401 sysfs_remove_group(&client->dev.kobj, &ltc4245_group);
403 kfree(data);
405 return 0;
408 static const struct i2c_device_id ltc4245_id[] = {
409 { "ltc4245", 0 },
412 MODULE_DEVICE_TABLE(i2c, ltc4245_id);
414 /* This is the driver that will be inserted */
415 static struct i2c_driver ltc4245_driver = {
416 .driver = {
417 .name = "ltc4245",
419 .probe = ltc4245_probe,
420 .remove = ltc4245_remove,
421 .id_table = ltc4245_id,
424 static int __init ltc4245_init(void)
426 return i2c_add_driver(&ltc4245_driver);
429 static void __exit ltc4245_exit(void)
431 i2c_del_driver(&ltc4245_driver);
434 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
435 MODULE_DESCRIPTION("LTC4245 driver");
436 MODULE_LICENSE("GPL");
438 module_init(ltc4245_init);
439 module_exit(ltc4245_exit);