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