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