]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/hwmon/ltc4245.c
gpio: Fix build breakage
[karo-tx-linux.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 #include <linux/jiffies.h>
25 #include <linux/i2c/ltc4245.h>
26
27 /* Here are names of the chip's registers (a.k.a. commands) */
28 enum ltc4245_cmd {
29         LTC4245_STATUS                  = 0x00, /* readonly */
30         LTC4245_ALERT                   = 0x01,
31         LTC4245_CONTROL                 = 0x02,
32         LTC4245_ON                      = 0x03,
33         LTC4245_FAULT1                  = 0x04,
34         LTC4245_FAULT2                  = 0x05,
35         LTC4245_GPIO                    = 0x06,
36         LTC4245_ADCADR                  = 0x07,
37
38         LTC4245_12VIN                   = 0x10,
39         LTC4245_12VSENSE                = 0x11,
40         LTC4245_12VOUT                  = 0x12,
41         LTC4245_5VIN                    = 0x13,
42         LTC4245_5VSENSE                 = 0x14,
43         LTC4245_5VOUT                   = 0x15,
44         LTC4245_3VIN                    = 0x16,
45         LTC4245_3VSENSE                 = 0x17,
46         LTC4245_3VOUT                   = 0x18,
47         LTC4245_VEEIN                   = 0x19,
48         LTC4245_VEESENSE                = 0x1a,
49         LTC4245_VEEOUT                  = 0x1b,
50         LTC4245_GPIOADC                 = 0x1c,
51 };
52
53 struct ltc4245_data {
54         struct i2c_client *client;
55
56         const struct attribute_group *groups[3];
57
58         struct mutex update_lock;
59         bool valid;
60         unsigned long last_updated; /* in jiffies */
61
62         /* Control registers */
63         u8 cregs[0x08];
64
65         /* Voltage registers */
66         u8 vregs[0x0d];
67
68         /* GPIO ADC registers */
69         bool use_extra_gpios;
70         int gpios[3];
71 };
72
73 /*
74  * Update the readings from the GPIO pins. If the driver has been configured to
75  * sample all GPIO's as analog voltages, a round-robin sampling method is used.
76  * Otherwise, only the configured GPIO pin is sampled.
77  *
78  * LOCKING: must hold data->update_lock
79  */
80 static void ltc4245_update_gpios(struct device *dev)
81 {
82         struct ltc4245_data *data = dev_get_drvdata(dev);
83         struct i2c_client *client = data->client;
84         u8 gpio_curr, gpio_next, gpio_reg;
85         int i;
86
87         /* no extra gpio support, we're basically done */
88         if (!data->use_extra_gpios) {
89                 data->gpios[0] = data->vregs[LTC4245_GPIOADC - 0x10];
90                 return;
91         }
92
93         /*
94          * If the last reading was too long ago, then we mark all old GPIO
95          * readings as stale by setting them to -EAGAIN
96          */
97         if (time_after(jiffies, data->last_updated + 5 * HZ)) {
98                 dev_dbg(&client->dev, "Marking GPIOs invalid\n");
99                 for (i = 0; i < ARRAY_SIZE(data->gpios); i++)
100                         data->gpios[i] = -EAGAIN;
101         }
102
103         /*
104          * Get the current GPIO pin
105          *
106          * The datasheet calls these GPIO[1-3], but we'll calculate the zero
107          * based array index instead, and call them GPIO[0-2]. This is much
108          * easier to think about.
109          */
110         gpio_curr = (data->cregs[LTC4245_GPIO] & 0xc0) >> 6;
111         if (gpio_curr > 0)
112                 gpio_curr -= 1;
113
114         /* Read the GPIO voltage from the GPIOADC register */
115         data->gpios[gpio_curr] = data->vregs[LTC4245_GPIOADC - 0x10];
116
117         /* Find the next GPIO pin to read */
118         gpio_next = (gpio_curr + 1) % ARRAY_SIZE(data->gpios);
119
120         /*
121          * Calculate the correct setting for the GPIO register so it will
122          * sample the next GPIO pin
123          */
124         gpio_reg = (data->cregs[LTC4245_GPIO] & 0x3f) | ((gpio_next + 1) << 6);
125
126         /* Update the GPIO register */
127         i2c_smbus_write_byte_data(client, LTC4245_GPIO, gpio_reg);
128
129         /* Update saved data */
130         data->cregs[LTC4245_GPIO] = gpio_reg;
131 }
132
133 static struct ltc4245_data *ltc4245_update_device(struct device *dev)
134 {
135         struct ltc4245_data *data = dev_get_drvdata(dev);
136         struct i2c_client *client = data->client;
137         s32 val;
138         int i;
139
140         mutex_lock(&data->update_lock);
141
142         if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
143
144                 dev_dbg(&client->dev, "Starting ltc4245 update\n");
145
146                 /* Read control registers -- 0x00 to 0x07 */
147                 for (i = 0; i < ARRAY_SIZE(data->cregs); i++) {
148                         val = i2c_smbus_read_byte_data(client, i);
149                         if (unlikely(val < 0))
150                                 data->cregs[i] = 0;
151                         else
152                                 data->cregs[i] = val;
153                 }
154
155                 /* Read voltage registers -- 0x10 to 0x1c */
156                 for (i = 0; i < ARRAY_SIZE(data->vregs); i++) {
157                         val = i2c_smbus_read_byte_data(client, i+0x10);
158                         if (unlikely(val < 0))
159                                 data->vregs[i] = 0;
160                         else
161                                 data->vregs[i] = val;
162                 }
163
164                 /* Update GPIO readings */
165                 ltc4245_update_gpios(dev);
166
167                 data->last_updated = jiffies;
168                 data->valid = 1;
169         }
170
171         mutex_unlock(&data->update_lock);
172
173         return data;
174 }
175
176 /* Return the voltage from the given register in millivolts */
177 static int ltc4245_get_voltage(struct device *dev, u8 reg)
178 {
179         struct ltc4245_data *data = ltc4245_update_device(dev);
180         const u8 regval = data->vregs[reg - 0x10];
181         u32 voltage = 0;
182
183         switch (reg) {
184         case LTC4245_12VIN:
185         case LTC4245_12VOUT:
186                 voltage = regval * 55;
187                 break;
188         case LTC4245_5VIN:
189         case LTC4245_5VOUT:
190                 voltage = regval * 22;
191                 break;
192         case LTC4245_3VIN:
193         case LTC4245_3VOUT:
194                 voltage = regval * 15;
195                 break;
196         case LTC4245_VEEIN:
197         case LTC4245_VEEOUT:
198                 voltage = regval * -55;
199                 break;
200         case LTC4245_GPIOADC:
201                 voltage = regval * 10;
202                 break;
203         default:
204                 /* If we get here, the developer messed up */
205                 WARN_ON_ONCE(1);
206                 break;
207         }
208
209         return voltage;
210 }
211
212 /* Return the current in the given sense register in milliAmperes */
213 static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
214 {
215         struct ltc4245_data *data = ltc4245_update_device(dev);
216         const u8 regval = data->vregs[reg - 0x10];
217         unsigned int voltage;
218         unsigned int curr;
219
220         /*
221          * The strange looking conversions that follow are fixed-point
222          * math, since we cannot do floating point in the kernel.
223          *
224          * Step 1: convert sense register to microVolts
225          * Step 2: convert voltage to milliAmperes
226          *
227          * If you play around with the V=IR equation, you come up with
228          * the following: X uV / Y mOhm == Z mA
229          *
230          * With the resistors that are fractions of a milliOhm, we multiply
231          * the voltage and resistance by 10, to shift the decimal point.
232          * Now we can use the normal division operator again.
233          */
234
235         switch (reg) {
236         case LTC4245_12VSENSE:
237                 voltage = regval * 250; /* voltage in uV */
238                 curr = voltage / 50; /* sense resistor 50 mOhm */
239                 break;
240         case LTC4245_5VSENSE:
241                 voltage = regval * 125; /* voltage in uV */
242                 curr = (voltage * 10) / 35; /* sense resistor 3.5 mOhm */
243                 break;
244         case LTC4245_3VSENSE:
245                 voltage = regval * 125; /* voltage in uV */
246                 curr = (voltage * 10) / 25; /* sense resistor 2.5 mOhm */
247                 break;
248         case LTC4245_VEESENSE:
249                 voltage = regval * 250; /* voltage in uV */
250                 curr = voltage / 100; /* sense resistor 100 mOhm */
251                 break;
252         default:
253                 /* If we get here, the developer messed up */
254                 WARN_ON_ONCE(1);
255                 curr = 0;
256                 break;
257         }
258
259         return curr;
260 }
261
262 static ssize_t ltc4245_show_voltage(struct device *dev,
263                                     struct device_attribute *da,
264                                     char *buf)
265 {
266         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
267         const int voltage = ltc4245_get_voltage(dev, attr->index);
268
269         return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
270 }
271
272 static ssize_t ltc4245_show_current(struct device *dev,
273                                     struct device_attribute *da,
274                                     char *buf)
275 {
276         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
277         const unsigned int curr = ltc4245_get_current(dev, attr->index);
278
279         return snprintf(buf, PAGE_SIZE, "%u\n", curr);
280 }
281
282 static ssize_t ltc4245_show_power(struct device *dev,
283                                   struct device_attribute *da,
284                                   char *buf)
285 {
286         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
287         const unsigned int curr = ltc4245_get_current(dev, attr->index);
288         const int output_voltage = ltc4245_get_voltage(dev, attr->index+1);
289
290         /* current in mA * voltage in mV == power in uW */
291         const unsigned int power = abs(output_voltage * curr);
292
293         return snprintf(buf, PAGE_SIZE, "%u\n", power);
294 }
295
296 static ssize_t ltc4245_show_alarm(struct device *dev,
297                                           struct device_attribute *da,
298                                           char *buf)
299 {
300         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
301         struct ltc4245_data *data = ltc4245_update_device(dev);
302         const u8 reg = data->cregs[attr->index];
303         const u32 mask = attr->nr;
304
305         return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
306 }
307
308 static ssize_t ltc4245_show_gpio(struct device *dev,
309                                  struct device_attribute *da,
310                                  char *buf)
311 {
312         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
313         struct ltc4245_data *data = ltc4245_update_device(dev);
314         int val = data->gpios[attr->index];
315
316         /* handle stale GPIO's */
317         if (val < 0)
318                 return val;
319
320         /* Convert to millivolts and print */
321         return snprintf(buf, PAGE_SIZE, "%u\n", val * 10);
322 }
323
324 /* Construct a sensor_device_attribute structure for each register */
325
326 /* Input voltages */
327 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4245_show_voltage, NULL,
328                           LTC4245_12VIN);
329 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4245_show_voltage, NULL,
330                           LTC4245_5VIN);
331 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, ltc4245_show_voltage, NULL,
332                           LTC4245_3VIN);
333 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, ltc4245_show_voltage, NULL,
334                           LTC4245_VEEIN);
335
336 /* Input undervoltage alarms */
337 static SENSOR_DEVICE_ATTR_2(in1_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
338                             1 << 0, LTC4245_FAULT1);
339 static SENSOR_DEVICE_ATTR_2(in2_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
340                             1 << 1, LTC4245_FAULT1);
341 static SENSOR_DEVICE_ATTR_2(in3_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
342                             1 << 2, LTC4245_FAULT1);
343 static SENSOR_DEVICE_ATTR_2(in4_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
344                             1 << 3, LTC4245_FAULT1);
345
346 /* Currents (via sense resistor) */
347 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4245_show_current, NULL,
348                           LTC4245_12VSENSE);
349 static SENSOR_DEVICE_ATTR(curr2_input, S_IRUGO, ltc4245_show_current, NULL,
350                           LTC4245_5VSENSE);
351 static SENSOR_DEVICE_ATTR(curr3_input, S_IRUGO, ltc4245_show_current, NULL,
352                           LTC4245_3VSENSE);
353 static SENSOR_DEVICE_ATTR(curr4_input, S_IRUGO, ltc4245_show_current, NULL,
354                           LTC4245_VEESENSE);
355
356 /* Overcurrent alarms */
357 static SENSOR_DEVICE_ATTR_2(curr1_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
358                             1 << 4, LTC4245_FAULT1);
359 static SENSOR_DEVICE_ATTR_2(curr2_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
360                             1 << 5, LTC4245_FAULT1);
361 static SENSOR_DEVICE_ATTR_2(curr3_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
362                             1 << 6, LTC4245_FAULT1);
363 static SENSOR_DEVICE_ATTR_2(curr4_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
364                             1 << 7, LTC4245_FAULT1);
365
366 /* Output voltages */
367 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, ltc4245_show_voltage, NULL,
368                           LTC4245_12VOUT);
369 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, ltc4245_show_voltage, NULL,
370                           LTC4245_5VOUT);
371 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, ltc4245_show_voltage, NULL,
372                           LTC4245_3VOUT);
373 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, ltc4245_show_voltage, NULL,
374                           LTC4245_VEEOUT);
375
376 /* Power Bad alarms */
377 static SENSOR_DEVICE_ATTR_2(in5_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
378                             1 << 0, LTC4245_FAULT2);
379 static SENSOR_DEVICE_ATTR_2(in6_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
380                             1 << 1, LTC4245_FAULT2);
381 static SENSOR_DEVICE_ATTR_2(in7_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
382                             1 << 2, LTC4245_FAULT2);
383 static SENSOR_DEVICE_ATTR_2(in8_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
384                             1 << 3, LTC4245_FAULT2);
385
386 /* GPIO voltages */
387 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, ltc4245_show_gpio, NULL, 0);
388 static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, ltc4245_show_gpio, NULL, 1);
389 static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, ltc4245_show_gpio, NULL, 2);
390
391 /* Power Consumption (virtual) */
392 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ltc4245_show_power, NULL,
393                           LTC4245_12VSENSE);
394 static SENSOR_DEVICE_ATTR(power2_input, S_IRUGO, ltc4245_show_power, NULL,
395                           LTC4245_5VSENSE);
396 static SENSOR_DEVICE_ATTR(power3_input, S_IRUGO, ltc4245_show_power, NULL,
397                           LTC4245_3VSENSE);
398 static SENSOR_DEVICE_ATTR(power4_input, S_IRUGO, ltc4245_show_power, NULL,
399                           LTC4245_VEESENSE);
400
401 /*
402  * Finally, construct an array of pointers to members of the above objects,
403  * as required for sysfs_create_group()
404  */
405 static struct attribute *ltc4245_std_attributes[] = {
406         &sensor_dev_attr_in1_input.dev_attr.attr,
407         &sensor_dev_attr_in2_input.dev_attr.attr,
408         &sensor_dev_attr_in3_input.dev_attr.attr,
409         &sensor_dev_attr_in4_input.dev_attr.attr,
410
411         &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
412         &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
413         &sensor_dev_attr_in3_min_alarm.dev_attr.attr,
414         &sensor_dev_attr_in4_min_alarm.dev_attr.attr,
415
416         &sensor_dev_attr_curr1_input.dev_attr.attr,
417         &sensor_dev_attr_curr2_input.dev_attr.attr,
418         &sensor_dev_attr_curr3_input.dev_attr.attr,
419         &sensor_dev_attr_curr4_input.dev_attr.attr,
420
421         &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
422         &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
423         &sensor_dev_attr_curr3_max_alarm.dev_attr.attr,
424         &sensor_dev_attr_curr4_max_alarm.dev_attr.attr,
425
426         &sensor_dev_attr_in5_input.dev_attr.attr,
427         &sensor_dev_attr_in6_input.dev_attr.attr,
428         &sensor_dev_attr_in7_input.dev_attr.attr,
429         &sensor_dev_attr_in8_input.dev_attr.attr,
430
431         &sensor_dev_attr_in5_min_alarm.dev_attr.attr,
432         &sensor_dev_attr_in6_min_alarm.dev_attr.attr,
433         &sensor_dev_attr_in7_min_alarm.dev_attr.attr,
434         &sensor_dev_attr_in8_min_alarm.dev_attr.attr,
435
436         &sensor_dev_attr_in9_input.dev_attr.attr,
437
438         &sensor_dev_attr_power1_input.dev_attr.attr,
439         &sensor_dev_attr_power2_input.dev_attr.attr,
440         &sensor_dev_attr_power3_input.dev_attr.attr,
441         &sensor_dev_attr_power4_input.dev_attr.attr,
442
443         NULL,
444 };
445
446 static struct attribute *ltc4245_gpio_attributes[] = {
447         &sensor_dev_attr_in10_input.dev_attr.attr,
448         &sensor_dev_attr_in11_input.dev_attr.attr,
449         NULL,
450 };
451
452 static const struct attribute_group ltc4245_std_group = {
453         .attrs = ltc4245_std_attributes,
454 };
455
456 static const struct attribute_group ltc4245_gpio_group = {
457         .attrs = ltc4245_gpio_attributes,
458 };
459
460 static void ltc4245_sysfs_add_groups(struct ltc4245_data *data)
461 {
462         /* standard sysfs attributes */
463         data->groups[0] = &ltc4245_std_group;
464
465         /* if we're using the extra gpio support, register it's attributes */
466         if (data->use_extra_gpios)
467                 data->groups[1] = &ltc4245_gpio_group;
468 }
469
470 static bool ltc4245_use_extra_gpios(struct i2c_client *client)
471 {
472         struct ltc4245_platform_data *pdata = dev_get_platdata(&client->dev);
473 #ifdef CONFIG_OF
474         struct device_node *np = client->dev.of_node;
475 #endif
476
477         /* prefer platform data */
478         if (pdata)
479                 return pdata->use_extra_gpios;
480
481 #ifdef CONFIG_OF
482         /* fallback on OF */
483         if (of_find_property(np, "ltc4245,use-extra-gpios", NULL))
484                 return true;
485 #endif
486
487         return false;
488 }
489
490 static int ltc4245_probe(struct i2c_client *client,
491                          const struct i2c_device_id *id)
492 {
493         struct i2c_adapter *adapter = client->adapter;
494         struct ltc4245_data *data;
495         struct device *hwmon_dev;
496
497         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
498                 return -ENODEV;
499
500         data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
501         if (!data)
502                 return -ENOMEM;
503
504         data->client = client;
505         mutex_init(&data->update_lock);
506         data->use_extra_gpios = ltc4245_use_extra_gpios(client);
507
508         /* Initialize the LTC4245 chip */
509         i2c_smbus_write_byte_data(client, LTC4245_FAULT1, 0x00);
510         i2c_smbus_write_byte_data(client, LTC4245_FAULT2, 0x00);
511
512         /* Add sysfs hooks */
513         ltc4245_sysfs_add_groups(data);
514
515         hwmon_dev = hwmon_device_register_with_groups(&client->dev,
516                                                       client->name, data,
517                                                       data->groups);
518         if (IS_ERR(hwmon_dev))
519                 return PTR_ERR(hwmon_dev);
520
521         i2c_set_clientdata(client, hwmon_dev);
522
523         return 0;
524 }
525
526 static int ltc4245_remove(struct i2c_client *client)
527 {
528         struct device *hwmon_dev = i2c_get_clientdata(client);
529
530         hwmon_device_unregister(hwmon_dev);
531
532         return 0;
533 }
534
535 static const struct i2c_device_id ltc4245_id[] = {
536         { "ltc4245", 0 },
537         { }
538 };
539 MODULE_DEVICE_TABLE(i2c, ltc4245_id);
540
541 /* This is the driver that will be inserted */
542 static struct i2c_driver ltc4245_driver = {
543         .driver = {
544                 .name   = "ltc4245",
545         },
546         .probe          = ltc4245_probe,
547         .remove         = ltc4245_remove,
548         .id_table       = ltc4245_id,
549 };
550
551 module_i2c_driver(ltc4245_driver);
552
553 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
554 MODULE_DESCRIPTION("LTC4245 driver");
555 MODULE_LICENSE("GPL");