Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / hwmon / ltc4245.c
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
25 /* Valid addresses are 0x20 - 0x3f
26  *
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 };
30
31 /* Insmod parameters */
32 I2C_CLIENT_INSMOD_1(ltc4245);
33
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,
44
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,
60 };
61
62 struct ltc4245_data {
63         struct device *hwmon_dev;
64
65         struct mutex update_lock;
66         bool valid;
67         unsigned long last_updated; /* in jiffies */
68
69         /* Control registers */
70         u8 cregs[0x08];
71
72         /* Voltage registers */
73         u8 vregs[0x0f];
74 };
75
76 static struct ltc4245_data *ltc4245_update_device(struct device *dev)
77 {
78         struct i2c_client *client = to_i2c_client(dev);
79         struct ltc4245_data *data = i2c_get_clientdata(client);
80         s32 val;
81         int i;
82
83         mutex_lock(&data->update_lock);
84
85         if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
86
87                 dev_dbg(&client->dev, "Starting ltc4245 update\n");
88
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;
96                 }
97
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;
105                 }
106
107                 data->last_updated = jiffies;
108                 data->valid = 1;
109         }
110
111         mutex_unlock(&data->update_lock);
112
113         return data;
114 }
115
116 /* Return the voltage from the given register in millivolts */
117 static int ltc4245_get_voltage(struct device *dev, u8 reg)
118 {
119         struct ltc4245_data *data = ltc4245_update_device(dev);
120         const u8 regval = data->vregs[reg - 0x10];
121         u32 voltage = 0;
122
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;
149         }
150
151         return voltage;
152 }
153
154 /* Return the current in the given sense register in milliAmperes */
155 static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
156 {
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;
161
162         /* The strange looking conversions that follow are fixed-point
163          * math, since we cannot do floating point in the kernel.
164          *
165          * Step 1: convert sense register to microVolts
166          * Step 2: convert voltage to milliAmperes
167          *
168          * If you play around with the V=IR equation, you come up with
169          * the following: X uV / Y mOhm == Z mA
170          *
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.
174          */
175
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;
198         }
199
200         return curr;
201 }
202
203 static ssize_t ltc4245_show_voltage(struct device *dev,
204                                     struct device_attribute *da,
205                                     char *buf)
206 {
207         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
208         const int voltage = ltc4245_get_voltage(dev, attr->index);
209
210         return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
211 }
212
213 static ssize_t ltc4245_show_current(struct device *dev,
214                                     struct device_attribute *da,
215                                     char *buf)
216 {
217         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
218         const unsigned int curr = ltc4245_get_current(dev, attr->index);
219
220         return snprintf(buf, PAGE_SIZE, "%u\n", curr);
221 }
222
223 static ssize_t ltc4245_show_power(struct device *dev,
224                                   struct device_attribute *da,
225                                   char *buf)
226 {
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);
230
231         /* current in mA * voltage in mV == power in uW */
232         const unsigned int power = abs(output_voltage * curr);
233
234         return snprintf(buf, PAGE_SIZE, "%u\n", power);
235 }
236
237 static ssize_t ltc4245_show_alarm(struct device *dev,
238                                           struct device_attribute *da,
239                                           char *buf)
240 {
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;
245
246         return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
247 }
248
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.
252  */
253
254 #define LTC4245_VOLTAGE(name, ltc4245_cmd_idx) \
255         static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
256         ltc4245_show_voltage, NULL, ltc4245_cmd_idx)
257
258 #define LTC4245_CURRENT(name, ltc4245_cmd_idx) \
259         static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
260         ltc4245_show_current, NULL, ltc4245_cmd_idx)
261
262 #define LTC4245_POWER(name, ltc4245_cmd_idx) \
263         static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
264         ltc4245_show_power, NULL, ltc4245_cmd_idx)
265
266 #define LTC4245_ALARM(name, mask, reg) \
267         static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \
268         ltc4245_show_alarm, NULL, (mask), reg)
269
270 /* Construct a sensor_device_attribute structure for each register */
271
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);
277
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);
283
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);
289
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);
295
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);
301
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);
307
308 /* GPIO voltages */
309 LTC4245_VOLTAGE(in9_input,                      LTC4245_GPIOADC1);
310 LTC4245_VOLTAGE(in10_input,                     LTC4245_GPIOADC2);
311 LTC4245_VOLTAGE(in11_input,                     LTC4245_GPIOADC3);
312
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);
318
319 /* Finally, construct an array of pointers to members of the above objects,
320  * as required for sysfs_create_group()
321  */
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,
327
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,
332
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,
337
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,
342
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,
347
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,
352
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,
356
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,
361
362         NULL,
363 };
364
365 static const struct attribute_group ltc4245_group = {
366         .attrs = ltc4245_attributes,
367 };
368
369 static int ltc4245_probe(struct i2c_client *client,
370                          const struct i2c_device_id *id)
371 {
372         struct ltc4245_data *data;
373         int ret;
374
375         data = kzalloc(sizeof(*data), GFP_KERNEL);
376         if (!data) {
377                 ret = -ENOMEM;
378                 goto out_kzalloc;
379         }
380
381         i2c_set_clientdata(client, data);
382         mutex_init(&data->update_lock);
383
384         /* Initialize the LTC4245 chip */
385         i2c_smbus_write_byte_data(client, LTC4245_FAULT1, 0x00);
386         i2c_smbus_write_byte_data(client, LTC4245_FAULT2, 0x00);
387
388         /* Register sysfs hooks */
389         ret = sysfs_create_group(&client->dev.kobj, &ltc4245_group);
390         if (ret)
391                 goto out_sysfs_create_group;
392
393         data->hwmon_dev = hwmon_device_register(&client->dev);
394         if (IS_ERR(data->hwmon_dev)) {
395                 ret = PTR_ERR(data->hwmon_dev);
396                 goto out_hwmon_device_register;
397         }
398
399         return 0;
400
401 out_hwmon_device_register:
402         sysfs_remove_group(&client->dev.kobj, &ltc4245_group);
403 out_sysfs_create_group:
404         kfree(data);
405 out_kzalloc:
406         return ret;
407 }
408
409 static int ltc4245_remove(struct i2c_client *client)
410 {
411         struct ltc4245_data *data = i2c_get_clientdata(client);
412
413         hwmon_device_unregister(data->hwmon_dev);
414         sysfs_remove_group(&client->dev.kobj, &ltc4245_group);
415
416         kfree(data);
417
418         return 0;
419 }
420
421 /* Check that some bits in a control register appear at all possible
422  * locations without changing value
423  *
424  * @client: the i2c client to use
425  * @reg: the register to read
426  * @bits: the bits to check (0xff checks all bits,
427  *                           0x03 checks only the last two bits)
428  *
429  * return -ERRNO if the register read failed
430  * return -ENODEV if the register value doesn't stay constant at all
431  * possible addresses
432  *
433  * return 0 for success
434  */
435 static int ltc4245_check_control_reg(struct i2c_client *client, u8 reg, u8 bits)
436 {
437         int i;
438         s32 v, voff1, voff2;
439
440         /* Read register and check for error */
441         v = i2c_smbus_read_byte_data(client, reg);
442         if (v < 0)
443                 return v;
444
445         v &= bits;
446
447         for (i = 0x00; i < 0xff; i += 0x20) {
448
449                 voff1 = i2c_smbus_read_byte_data(client, reg + i);
450                 if (voff1 < 0)
451                         return voff1;
452
453                 voff2 = i2c_smbus_read_byte_data(client, reg + i + 0x08);
454                 if (voff2 < 0)
455                         return voff2;
456
457                 voff1 &= bits;
458                 voff2 &= bits;
459
460                 if (v != voff1 || v != voff2)
461                         return -ENODEV;
462         }
463
464         return 0;
465 }
466
467 static int ltc4245_detect(struct i2c_client *client,
468                           int kind,
469                           struct i2c_board_info *info)
470 {
471         struct i2c_adapter *adapter = client->adapter;
472
473         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
474                 return -ENODEV;
475
476         if (kind < 0) {         /* probed detection - check the chip type */
477                 s32 v;          /* 8 bits from the chip, or -ERRNO */
478
479                 /* Chip registers 0x00-0x07 are control registers
480                  * Chip registers 0x10-0x1f are data registers
481                  *
482                  * Address bits b7-b5 are ignored. This makes the chip "repeat"
483                  * in steps of 0x20. Any control registers should appear with
484                  * the same values across all duplicated addresses.
485                  *
486                  * Register 0x02 bit b2 is reserved, expect 0
487                  * Register 0x07 bits b7 to b4 are reserved, expect 0
488                  *
489                  * Registers 0x01, 0x02 are control registers and should not
490                  * change on their own.
491                  *
492                  * Register 0x06 bits b6 and b7 are control bits, and should
493                  * not change on their own.
494                  *
495                  * Register 0x07 bits b3 to b0 are control bits, and should
496                  * not change on their own.
497                  */
498
499                 /* read register 0x02 reserved bit, expect 0 */
500                 v = i2c_smbus_read_byte_data(client, LTC4245_CONTROL);
501                 if (v < 0 || (v & 0x04) != 0)
502                         return -ENODEV;
503
504                 /* read register 0x07 reserved bits, expect 0 */
505                 v = i2c_smbus_read_byte_data(client, LTC4245_ADCADR);
506                 if (v < 0 || (v & 0xf0) != 0)
507                         return -ENODEV;
508
509                 /* check that the alert register appears at all locations */
510                 if (ltc4245_check_control_reg(client, LTC4245_ALERT, 0xff))
511                         return -ENODEV;
512
513                 /* check that the control register appears at all locations */
514                 if (ltc4245_check_control_reg(client, LTC4245_CONTROL, 0xff))
515                         return -ENODEV;
516
517                 /* check that register 0x06 bits b6 and b7 stay constant */
518                 if (ltc4245_check_control_reg(client, LTC4245_GPIO, 0xc0))
519                         return -ENODEV;
520
521                 /* check that register 0x07 bits b3-b0 stay constant */
522                 if (ltc4245_check_control_reg(client, LTC4245_ADCADR, 0x0f))
523                         return -ENODEV;
524         }
525
526         strlcpy(info->type, "ltc4245", I2C_NAME_SIZE);
527         dev_info(&adapter->dev, "ltc4245 %s at address 0x%02x\n",
528                         kind < 0 ? "probed" : "forced",
529                         client->addr);
530
531         return 0;
532 }
533
534 static const struct i2c_device_id ltc4245_id[] = {
535         { "ltc4245", ltc4245 },
536         { }
537 };
538 MODULE_DEVICE_TABLE(i2c, ltc4245_id);
539
540 /* This is the driver that will be inserted */
541 static struct i2c_driver ltc4245_driver = {
542         .class          = I2C_CLASS_HWMON,
543         .driver = {
544                 .name   = "ltc4245",
545         },
546         .probe          = ltc4245_probe,
547         .remove         = ltc4245_remove,
548         .id_table       = ltc4245_id,
549         .detect         = ltc4245_detect,
550         .address_data   = &addr_data,
551 };
552
553 static int __init ltc4245_init(void)
554 {
555         return i2c_add_driver(&ltc4245_driver);
556 }
557
558 static void __exit ltc4245_exit(void)
559 {
560         i2c_del_driver(&ltc4245_driver);
561 }
562
563 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
564 MODULE_DESCRIPTION("LTC4245 driver");
565 MODULE_LICENSE("GPL");
566
567 module_init(ltc4245_init);
568 module_exit(ltc4245_exit);