PCI: pcie_portdriver: fix pcie_port_device_remove
[linux-2.6/mini2440.git] / drivers / hwmon / ltc4245.c
blob034b2c5158486dd0c3ccc5482b82de02098961b1
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 /* Valid addresses are 0x20 - 0x3f
27 * For now, we do not probe, since some of these addresses
28 * are known to be unfriendly to probing */
29 static const unsigned short normal_i2c[] = { I2C_CLIENT_END };
31 /* Insmod parameters */
32 I2C_CLIENT_INSMOD_1(ltc4245);
34 /* Here are names of the chip's registers (a.k.a. commands) */
35 enum ltc4245_cmd {
36 LTC4245_STATUS = 0x00, /* readonly */
37 LTC4245_ALERT = 0x01,
38 LTC4245_CONTROL = 0x02,
39 LTC4245_ON = 0x03,
40 LTC4245_FAULT1 = 0x04,
41 LTC4245_FAULT2 = 0x05,
42 LTC4245_GPIO = 0x06,
43 LTC4245_ADCADR = 0x07,
45 LTC4245_12VIN = 0x10,
46 LTC4245_12VSENSE = 0x11,
47 LTC4245_12VOUT = 0x12,
48 LTC4245_5VIN = 0x13,
49 LTC4245_5VSENSE = 0x14,
50 LTC4245_5VOUT = 0x15,
51 LTC4245_3VIN = 0x16,
52 LTC4245_3VSENSE = 0x17,
53 LTC4245_3VOUT = 0x18,
54 LTC4245_VEEIN = 0x19,
55 LTC4245_VEESENSE = 0x1a,
56 LTC4245_VEEOUT = 0x1b,
57 LTC4245_GPIOADC1 = 0x1c,
58 LTC4245_GPIOADC2 = 0x1d,
59 LTC4245_GPIOADC3 = 0x1e,
62 struct ltc4245_data {
63 struct device *hwmon_dev;
65 struct mutex update_lock;
66 bool valid;
67 unsigned long last_updated; /* in jiffies */
69 /* Control registers */
70 u8 cregs[0x08];
72 /* Voltage registers */
73 u8 vregs[0x0f];
76 static struct ltc4245_data *ltc4245_update_device(struct device *dev)
78 struct i2c_client *client = to_i2c_client(dev);
79 struct ltc4245_data *data = i2c_get_clientdata(client);
80 s32 val;
81 int i;
83 mutex_lock(&data->update_lock);
85 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
87 dev_dbg(&client->dev, "Starting ltc4245 update\n");
89 /* Read control registers -- 0x00 to 0x07 */
90 for (i = 0; i < ARRAY_SIZE(data->cregs); i++) {
91 val = i2c_smbus_read_byte_data(client, i);
92 if (unlikely(val < 0))
93 data->cregs[i] = 0;
94 else
95 data->cregs[i] = val;
98 /* Read voltage registers -- 0x10 to 0x1f */
99 for (i = 0; i < ARRAY_SIZE(data->vregs); i++) {
100 val = i2c_smbus_read_byte_data(client, i+0x10);
101 if (unlikely(val < 0))
102 data->vregs[i] = 0;
103 else
104 data->vregs[i] = val;
107 data->last_updated = jiffies;
108 data->valid = 1;
111 mutex_unlock(&data->update_lock);
113 return data;
116 /* Return the voltage from the given register in millivolts */
117 static int ltc4245_get_voltage(struct device *dev, u8 reg)
119 struct ltc4245_data *data = ltc4245_update_device(dev);
120 const u8 regval = data->vregs[reg - 0x10];
121 u32 voltage = 0;
123 switch (reg) {
124 case LTC4245_12VIN:
125 case LTC4245_12VOUT:
126 voltage = regval * 55;
127 break;
128 case LTC4245_5VIN:
129 case LTC4245_5VOUT:
130 voltage = regval * 22;
131 break;
132 case LTC4245_3VIN:
133 case LTC4245_3VOUT:
134 voltage = regval * 15;
135 break;
136 case LTC4245_VEEIN:
137 case LTC4245_VEEOUT:
138 voltage = regval * -55;
139 break;
140 case LTC4245_GPIOADC1:
141 case LTC4245_GPIOADC2:
142 case LTC4245_GPIOADC3:
143 voltage = regval * 10;
144 break;
145 default:
146 /* If we get here, the developer messed up */
147 WARN_ON_ONCE(1);
148 break;
151 return voltage;
154 /* Return the current in the given sense register in milliAmperes */
155 static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
157 struct ltc4245_data *data = ltc4245_update_device(dev);
158 const u8 regval = data->vregs[reg - 0x10];
159 unsigned int voltage;
160 unsigned int curr;
162 /* The strange looking conversions that follow are fixed-point
163 * math, since we cannot do floating point in the kernel.
165 * Step 1: convert sense register to microVolts
166 * Step 2: convert voltage to milliAmperes
168 * If you play around with the V=IR equation, you come up with
169 * the following: X uV / Y mOhm == Z mA
171 * With the resistors that are fractions of a milliOhm, we multiply
172 * the voltage and resistance by 10, to shift the decimal point.
173 * Now we can use the normal division operator again.
176 switch (reg) {
177 case LTC4245_12VSENSE:
178 voltage = regval * 250; /* voltage in uV */
179 curr = voltage / 50; /* sense resistor 50 mOhm */
180 break;
181 case LTC4245_5VSENSE:
182 voltage = regval * 125; /* voltage in uV */
183 curr = (voltage * 10) / 35; /* sense resistor 3.5 mOhm */
184 break;
185 case LTC4245_3VSENSE:
186 voltage = regval * 125; /* voltage in uV */
187 curr = (voltage * 10) / 25; /* sense resistor 2.5 mOhm */
188 break;
189 case LTC4245_VEESENSE:
190 voltage = regval * 250; /* voltage in uV */
191 curr = voltage / 100; /* sense resistor 100 mOhm */
192 break;
193 default:
194 /* If we get here, the developer messed up */
195 WARN_ON_ONCE(1);
196 curr = 0;
197 break;
200 return curr;
203 static ssize_t ltc4245_show_voltage(struct device *dev,
204 struct device_attribute *da,
205 char *buf)
207 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
208 const int voltage = ltc4245_get_voltage(dev, attr->index);
210 return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
213 static ssize_t ltc4245_show_current(struct device *dev,
214 struct device_attribute *da,
215 char *buf)
217 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
218 const unsigned int curr = ltc4245_get_current(dev, attr->index);
220 return snprintf(buf, PAGE_SIZE, "%u\n", curr);
223 static ssize_t ltc4245_show_power(struct device *dev,
224 struct device_attribute *da,
225 char *buf)
227 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
228 const unsigned int curr = ltc4245_get_current(dev, attr->index);
229 const int output_voltage = ltc4245_get_voltage(dev, attr->index+1);
231 /* current in mA * voltage in mV == power in uW */
232 const unsigned int power = abs(output_voltage * curr);
234 return snprintf(buf, PAGE_SIZE, "%u\n", power);
237 static ssize_t ltc4245_show_alarm(struct device *dev,
238 struct device_attribute *da,
239 char *buf)
241 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
242 struct ltc4245_data *data = ltc4245_update_device(dev);
243 const u8 reg = data->cregs[attr->index];
244 const u32 mask = attr->nr;
246 return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
249 /* These macros are used below in constructing device attribute objects
250 * for use with sysfs_create_group() to make a sysfs device file
251 * for each register.
254 #define LTC4245_VOLTAGE(name, ltc4245_cmd_idx) \
255 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
256 ltc4245_show_voltage, NULL, ltc4245_cmd_idx)
258 #define LTC4245_CURRENT(name, ltc4245_cmd_idx) \
259 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
260 ltc4245_show_current, NULL, ltc4245_cmd_idx)
262 #define LTC4245_POWER(name, ltc4245_cmd_idx) \
263 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
264 ltc4245_show_power, NULL, ltc4245_cmd_idx)
266 #define LTC4245_ALARM(name, mask, reg) \
267 static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \
268 ltc4245_show_alarm, NULL, (mask), reg)
270 /* Construct a sensor_device_attribute structure for each register */
272 /* Input voltages */
273 LTC4245_VOLTAGE(in1_input, LTC4245_12VIN);
274 LTC4245_VOLTAGE(in2_input, LTC4245_5VIN);
275 LTC4245_VOLTAGE(in3_input, LTC4245_3VIN);
276 LTC4245_VOLTAGE(in4_input, LTC4245_VEEIN);
278 /* Input undervoltage alarms */
279 LTC4245_ALARM(in1_min_alarm, (1 << 0), LTC4245_FAULT1);
280 LTC4245_ALARM(in2_min_alarm, (1 << 1), LTC4245_FAULT1);
281 LTC4245_ALARM(in3_min_alarm, (1 << 2), LTC4245_FAULT1);
282 LTC4245_ALARM(in4_min_alarm, (1 << 3), LTC4245_FAULT1);
284 /* Currents (via sense resistor) */
285 LTC4245_CURRENT(curr1_input, LTC4245_12VSENSE);
286 LTC4245_CURRENT(curr2_input, LTC4245_5VSENSE);
287 LTC4245_CURRENT(curr3_input, LTC4245_3VSENSE);
288 LTC4245_CURRENT(curr4_input, LTC4245_VEESENSE);
290 /* Overcurrent alarms */
291 LTC4245_ALARM(curr1_max_alarm, (1 << 4), LTC4245_FAULT1);
292 LTC4245_ALARM(curr2_max_alarm, (1 << 5), LTC4245_FAULT1);
293 LTC4245_ALARM(curr3_max_alarm, (1 << 6), LTC4245_FAULT1);
294 LTC4245_ALARM(curr4_max_alarm, (1 << 7), LTC4245_FAULT1);
296 /* Output voltages */
297 LTC4245_VOLTAGE(in5_input, LTC4245_12VOUT);
298 LTC4245_VOLTAGE(in6_input, LTC4245_5VOUT);
299 LTC4245_VOLTAGE(in7_input, LTC4245_3VOUT);
300 LTC4245_VOLTAGE(in8_input, LTC4245_VEEOUT);
302 /* Power Bad alarms */
303 LTC4245_ALARM(in5_min_alarm, (1 << 0), LTC4245_FAULT2);
304 LTC4245_ALARM(in6_min_alarm, (1 << 1), LTC4245_FAULT2);
305 LTC4245_ALARM(in7_min_alarm, (1 << 2), LTC4245_FAULT2);
306 LTC4245_ALARM(in8_min_alarm, (1 << 3), LTC4245_FAULT2);
308 /* GPIO voltages */
309 LTC4245_VOLTAGE(in9_input, LTC4245_GPIOADC1);
310 LTC4245_VOLTAGE(in10_input, LTC4245_GPIOADC2);
311 LTC4245_VOLTAGE(in11_input, LTC4245_GPIOADC3);
313 /* Power Consumption (virtual) */
314 LTC4245_POWER(power1_input, LTC4245_12VSENSE);
315 LTC4245_POWER(power2_input, LTC4245_5VSENSE);
316 LTC4245_POWER(power3_input, LTC4245_3VSENSE);
317 LTC4245_POWER(power4_input, LTC4245_VEESENSE);
319 /* Finally, construct an array of pointers to members of the above objects,
320 * as required for sysfs_create_group()
322 static struct attribute *ltc4245_attributes[] = {
323 &sensor_dev_attr_in1_input.dev_attr.attr,
324 &sensor_dev_attr_in2_input.dev_attr.attr,
325 &sensor_dev_attr_in3_input.dev_attr.attr,
326 &sensor_dev_attr_in4_input.dev_attr.attr,
328 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
329 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
330 &sensor_dev_attr_in3_min_alarm.dev_attr.attr,
331 &sensor_dev_attr_in4_min_alarm.dev_attr.attr,
333 &sensor_dev_attr_curr1_input.dev_attr.attr,
334 &sensor_dev_attr_curr2_input.dev_attr.attr,
335 &sensor_dev_attr_curr3_input.dev_attr.attr,
336 &sensor_dev_attr_curr4_input.dev_attr.attr,
338 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
339 &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
340 &sensor_dev_attr_curr3_max_alarm.dev_attr.attr,
341 &sensor_dev_attr_curr4_max_alarm.dev_attr.attr,
343 &sensor_dev_attr_in5_input.dev_attr.attr,
344 &sensor_dev_attr_in6_input.dev_attr.attr,
345 &sensor_dev_attr_in7_input.dev_attr.attr,
346 &sensor_dev_attr_in8_input.dev_attr.attr,
348 &sensor_dev_attr_in5_min_alarm.dev_attr.attr,
349 &sensor_dev_attr_in6_min_alarm.dev_attr.attr,
350 &sensor_dev_attr_in7_min_alarm.dev_attr.attr,
351 &sensor_dev_attr_in8_min_alarm.dev_attr.attr,
353 &sensor_dev_attr_in9_input.dev_attr.attr,
354 &sensor_dev_attr_in10_input.dev_attr.attr,
355 &sensor_dev_attr_in11_input.dev_attr.attr,
357 &sensor_dev_attr_power1_input.dev_attr.attr,
358 &sensor_dev_attr_power2_input.dev_attr.attr,
359 &sensor_dev_attr_power3_input.dev_attr.attr,
360 &sensor_dev_attr_power4_input.dev_attr.attr,
362 NULL,
365 static const struct attribute_group ltc4245_group = {
366 .attrs = ltc4245_attributes,
369 static int ltc4245_probe(struct i2c_client *client,
370 const struct i2c_device_id *id)
372 struct ltc4245_data *data;
373 int ret;
375 data = kzalloc(sizeof(*data), GFP_KERNEL);
376 if (!data) {
377 ret = -ENOMEM;
378 goto out_kzalloc;
381 i2c_set_clientdata(client, data);
382 mutex_init(&data->update_lock);
384 /* Initialize the LTC4245 chip */
385 /* TODO */
387 /* Register sysfs hooks */
388 ret = sysfs_create_group(&client->dev.kobj, &ltc4245_group);
389 if (ret)
390 goto out_sysfs_create_group;
392 data->hwmon_dev = hwmon_device_register(&client->dev);
393 if (IS_ERR(data->hwmon_dev)) {
394 ret = PTR_ERR(data->hwmon_dev);
395 goto out_hwmon_device_register;
398 return 0;
400 out_hwmon_device_register:
401 sysfs_remove_group(&client->dev.kobj, &ltc4245_group);
402 out_sysfs_create_group:
403 kfree(data);
404 out_kzalloc:
405 return ret;
408 static int ltc4245_remove(struct i2c_client *client)
410 struct ltc4245_data *data = i2c_get_clientdata(client);
412 hwmon_device_unregister(data->hwmon_dev);
413 sysfs_remove_group(&client->dev.kobj, &ltc4245_group);
415 kfree(data);
417 return 0;
420 /* Check that some bits in a control register appear at all possible
421 * locations without changing value
423 * @client: the i2c client to use
424 * @reg: the register to read
425 * @bits: the bits to check (0xff checks all bits,
426 * 0x03 checks only the last two bits)
428 * return -ERRNO if the register read failed
429 * return -ENODEV if the register value doesn't stay constant at all
430 * possible addresses
432 * return 0 for success
434 static int ltc4245_check_control_reg(struct i2c_client *client, u8 reg, u8 bits)
436 int i;
437 s32 v, voff1, voff2;
439 /* Read register and check for error */
440 v = i2c_smbus_read_byte_data(client, reg);
441 if (v < 0)
442 return v;
444 v &= bits;
446 for (i = 0x00; i < 0xff; i += 0x20) {
448 voff1 = i2c_smbus_read_byte_data(client, reg + i);
449 if (voff1 < 0)
450 return voff1;
452 voff2 = i2c_smbus_read_byte_data(client, reg + i + 0x08);
453 if (voff2 < 0)
454 return voff2;
456 voff1 &= bits;
457 voff2 &= bits;
459 if (v != voff1 || v != voff2)
460 return -ENODEV;
463 return 0;
466 static int ltc4245_detect(struct i2c_client *client,
467 int kind,
468 struct i2c_board_info *info)
470 struct i2c_adapter *adapter = client->adapter;
472 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
473 return -ENODEV;
475 if (kind < 0) { /* probed detection - check the chip type */
476 s32 v; /* 8 bits from the chip, or -ERRNO */
478 /* Chip registers 0x00-0x07 are control registers
479 * Chip registers 0x10-0x1f are data registers
481 * Address bits b7-b5 are ignored. This makes the chip "repeat"
482 * in steps of 0x20. Any control registers should appear with
483 * the same values across all duplicated addresses.
485 * Register 0x02 bit b2 is reserved, expect 0
486 * Register 0x07 bits b7 to b4 are reserved, expect 0
488 * Registers 0x01, 0x02 are control registers and should not
489 * change on their own.
491 * Register 0x06 bits b6 and b7 are control bits, and should
492 * not change on their own.
494 * Register 0x07 bits b3 to b0 are control bits, and should
495 * not change on their own.
498 /* read register 0x02 reserved bit, expect 0 */
499 v = i2c_smbus_read_byte_data(client, LTC4245_CONTROL);
500 if (v < 0 || (v & 0x04) != 0)
501 return -ENODEV;
503 /* read register 0x07 reserved bits, expect 0 */
504 v = i2c_smbus_read_byte_data(client, LTC4245_ADCADR);
505 if (v < 0 || (v & 0xf0) != 0)
506 return -ENODEV;
508 /* check that the alert register appears at all locations */
509 if (ltc4245_check_control_reg(client, LTC4245_ALERT, 0xff))
510 return -ENODEV;
512 /* check that the control register appears at all locations */
513 if (ltc4245_check_control_reg(client, LTC4245_CONTROL, 0xff))
514 return -ENODEV;
516 /* check that register 0x06 bits b6 and b7 stay constant */
517 if (ltc4245_check_control_reg(client, LTC4245_GPIO, 0xc0))
518 return -ENODEV;
520 /* check that register 0x07 bits b3-b0 stay constant */
521 if (ltc4245_check_control_reg(client, LTC4245_ADCADR, 0x0f))
522 return -ENODEV;
525 strlcpy(info->type, "ltc4245", I2C_NAME_SIZE);
526 dev_info(&adapter->dev, "ltc4245 %s at address 0x%02x\n",
527 kind < 0 ? "probed" : "forced",
528 client->addr);
530 return 0;
533 static const struct i2c_device_id ltc4245_id[] = {
534 { "ltc4245", ltc4245 },
537 MODULE_DEVICE_TABLE(i2c, ltc4245_id);
539 /* This is the driver that will be inserted */
540 static struct i2c_driver ltc4245_driver = {
541 .class = I2C_CLASS_HWMON,
542 .driver = {
543 .name = "ltc4245",
545 .probe = ltc4245_probe,
546 .remove = ltc4245_remove,
547 .id_table = ltc4245_id,
548 .detect = ltc4245_detect,
549 .address_data = &addr_data,
552 static int __init ltc4245_init(void)
554 return i2c_add_driver(&ltc4245_driver);
557 static void __exit ltc4245_exit(void)
559 i2c_del_driver(&ltc4245_driver);
562 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
563 MODULE_DESCRIPTION("LTC4245 driver");
564 MODULE_LICENSE("GPL");
566 module_init(ltc4245_init);
567 module_exit(ltc4245_exit);