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.
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>
24 #include <linux/jiffies.h>
25 #include <linux/i2c/ltc4245.h>
27 /* Here are names of the chip's registers (a.k.a. commands) */
29 LTC4245_STATUS
= 0x00, /* readonly */
31 LTC4245_CONTROL
= 0x02,
33 LTC4245_FAULT1
= 0x04,
34 LTC4245_FAULT2
= 0x05,
36 LTC4245_ADCADR
= 0x07,
39 LTC4245_12VSENSE
= 0x11,
40 LTC4245_12VOUT
= 0x12,
42 LTC4245_5VSENSE
= 0x14,
45 LTC4245_3VSENSE
= 0x17,
48 LTC4245_VEESENSE
= 0x1a,
49 LTC4245_VEEOUT
= 0x1b,
50 LTC4245_GPIOADC
= 0x1c,
54 struct device
*hwmon_dev
;
56 struct mutex update_lock
;
58 unsigned long last_updated
; /* in jiffies */
60 /* Control registers */
63 /* Voltage registers */
66 /* GPIO ADC registers */
72 * Update the readings from the GPIO pins. If the driver has been configured to
73 * sample all GPIO's as analog voltages, a round-robin sampling method is used.
74 * Otherwise, only the configured GPIO pin is sampled.
76 * LOCKING: must hold data->update_lock
78 static void ltc4245_update_gpios(struct device
*dev
)
80 struct i2c_client
*client
= to_i2c_client(dev
);
81 struct ltc4245_data
*data
= i2c_get_clientdata(client
);
82 u8 gpio_curr
, gpio_next
, gpio_reg
;
85 /* no extra gpio support, we're basically done */
86 if (!data
->use_extra_gpios
) {
87 data
->gpios
[0] = data
->vregs
[LTC4245_GPIOADC
- 0x10];
92 * If the last reading was too long ago, then we mark all old GPIO
93 * readings as stale by setting them to -EAGAIN
95 if (time_after(jiffies
, data
->last_updated
+ 5 * HZ
)) {
96 dev_dbg(&client
->dev
, "Marking GPIOs invalid\n");
97 for (i
= 0; i
< ARRAY_SIZE(data
->gpios
); i
++)
98 data
->gpios
[i
] = -EAGAIN
;
102 * Get the current GPIO pin
104 * The datasheet calls these GPIO[1-3], but we'll calculate the zero
105 * based array index instead, and call them GPIO[0-2]. This is much
106 * easier to think about.
108 gpio_curr
= (data
->cregs
[LTC4245_GPIO
] & 0xc0) >> 6;
112 /* Read the GPIO voltage from the GPIOADC register */
113 data
->gpios
[gpio_curr
] = data
->vregs
[LTC4245_GPIOADC
- 0x10];
115 /* Find the next GPIO pin to read */
116 gpio_next
= (gpio_curr
+ 1) % ARRAY_SIZE(data
->gpios
);
119 * Calculate the correct setting for the GPIO register so it will
120 * sample the next GPIO pin
122 gpio_reg
= (data
->cregs
[LTC4245_GPIO
] & 0x3f) | ((gpio_next
+ 1) << 6);
124 /* Update the GPIO register */
125 i2c_smbus_write_byte_data(client
, LTC4245_GPIO
, gpio_reg
);
127 /* Update saved data */
128 data
->cregs
[LTC4245_GPIO
] = gpio_reg
;
131 static struct ltc4245_data
*ltc4245_update_device(struct device
*dev
)
133 struct i2c_client
*client
= to_i2c_client(dev
);
134 struct ltc4245_data
*data
= i2c_get_clientdata(client
);
138 mutex_lock(&data
->update_lock
);
140 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
142 dev_dbg(&client
->dev
, "Starting ltc4245 update\n");
144 /* Read control registers -- 0x00 to 0x07 */
145 for (i
= 0; i
< ARRAY_SIZE(data
->cregs
); i
++) {
146 val
= i2c_smbus_read_byte_data(client
, i
);
147 if (unlikely(val
< 0))
150 data
->cregs
[i
] = val
;
153 /* Read voltage registers -- 0x10 to 0x1c */
154 for (i
= 0; i
< ARRAY_SIZE(data
->vregs
); i
++) {
155 val
= i2c_smbus_read_byte_data(client
, i
+0x10);
156 if (unlikely(val
< 0))
159 data
->vregs
[i
] = val
;
162 /* Update GPIO readings */
163 ltc4245_update_gpios(dev
);
165 data
->last_updated
= jiffies
;
169 mutex_unlock(&data
->update_lock
);
174 /* Return the voltage from the given register in millivolts */
175 static int ltc4245_get_voltage(struct device
*dev
, u8 reg
)
177 struct ltc4245_data
*data
= ltc4245_update_device(dev
);
178 const u8 regval
= data
->vregs
[reg
- 0x10];
184 voltage
= regval
* 55;
188 voltage
= regval
* 22;
192 voltage
= regval
* 15;
196 voltage
= regval
* -55;
198 case LTC4245_GPIOADC
:
199 voltage
= regval
* 10;
202 /* If we get here, the developer messed up */
210 /* Return the current in the given sense register in milliAmperes */
211 static unsigned int ltc4245_get_current(struct device
*dev
, u8 reg
)
213 struct ltc4245_data
*data
= ltc4245_update_device(dev
);
214 const u8 regval
= data
->vregs
[reg
- 0x10];
215 unsigned int voltage
;
219 * The strange looking conversions that follow are fixed-point
220 * math, since we cannot do floating point in the kernel.
222 * Step 1: convert sense register to microVolts
223 * Step 2: convert voltage to milliAmperes
225 * If you play around with the V=IR equation, you come up with
226 * the following: X uV / Y mOhm == Z mA
228 * With the resistors that are fractions of a milliOhm, we multiply
229 * the voltage and resistance by 10, to shift the decimal point.
230 * Now we can use the normal division operator again.
234 case LTC4245_12VSENSE
:
235 voltage
= regval
* 250; /* voltage in uV */
236 curr
= voltage
/ 50; /* sense resistor 50 mOhm */
238 case LTC4245_5VSENSE
:
239 voltage
= regval
* 125; /* voltage in uV */
240 curr
= (voltage
* 10) / 35; /* sense resistor 3.5 mOhm */
242 case LTC4245_3VSENSE
:
243 voltage
= regval
* 125; /* voltage in uV */
244 curr
= (voltage
* 10) / 25; /* sense resistor 2.5 mOhm */
246 case LTC4245_VEESENSE
:
247 voltage
= regval
* 250; /* voltage in uV */
248 curr
= voltage
/ 100; /* sense resistor 100 mOhm */
251 /* If we get here, the developer messed up */
260 static ssize_t
ltc4245_show_voltage(struct device
*dev
,
261 struct device_attribute
*da
,
264 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
265 const int voltage
= ltc4245_get_voltage(dev
, attr
->index
);
267 return snprintf(buf
, PAGE_SIZE
, "%d\n", voltage
);
270 static ssize_t
ltc4245_show_current(struct device
*dev
,
271 struct device_attribute
*da
,
274 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
275 const unsigned int curr
= ltc4245_get_current(dev
, attr
->index
);
277 return snprintf(buf
, PAGE_SIZE
, "%u\n", curr
);
280 static ssize_t
ltc4245_show_power(struct device
*dev
,
281 struct device_attribute
*da
,
284 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
285 const unsigned int curr
= ltc4245_get_current(dev
, attr
->index
);
286 const int output_voltage
= ltc4245_get_voltage(dev
, attr
->index
+1);
288 /* current in mA * voltage in mV == power in uW */
289 const unsigned int power
= abs(output_voltage
* curr
);
291 return snprintf(buf
, PAGE_SIZE
, "%u\n", power
);
294 static ssize_t
ltc4245_show_alarm(struct device
*dev
,
295 struct device_attribute
*da
,
298 struct sensor_device_attribute_2
*attr
= to_sensor_dev_attr_2(da
);
299 struct ltc4245_data
*data
= ltc4245_update_device(dev
);
300 const u8 reg
= data
->cregs
[attr
->index
];
301 const u32 mask
= attr
->nr
;
303 return snprintf(buf
, PAGE_SIZE
, "%u\n", (reg
& mask
) ? 1 : 0);
306 static ssize_t
ltc4245_show_gpio(struct device
*dev
,
307 struct device_attribute
*da
,
310 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
311 struct ltc4245_data
*data
= ltc4245_update_device(dev
);
312 int val
= data
->gpios
[attr
->index
];
314 /* handle stale GPIO's */
318 /* Convert to millivolts and print */
319 return snprintf(buf
, PAGE_SIZE
, "%u\n", val
* 10);
322 /* Construct a sensor_device_attribute structure for each register */
325 static SENSOR_DEVICE_ATTR(in1_input
, S_IRUGO
, ltc4245_show_voltage
, NULL
,
327 static SENSOR_DEVICE_ATTR(in2_input
, S_IRUGO
, ltc4245_show_voltage
, NULL
,
329 static SENSOR_DEVICE_ATTR(in3_input
, S_IRUGO
, ltc4245_show_voltage
, NULL
,
331 static SENSOR_DEVICE_ATTR(in4_input
, S_IRUGO
, ltc4245_show_voltage
, NULL
,
334 /* Input undervoltage alarms */
335 static SENSOR_DEVICE_ATTR_2(in1_min_alarm
, S_IRUGO
, ltc4245_show_alarm
, NULL
,
336 1 << 0, LTC4245_FAULT1
);
337 static SENSOR_DEVICE_ATTR_2(in2_min_alarm
, S_IRUGO
, ltc4245_show_alarm
, NULL
,
338 1 << 1, LTC4245_FAULT1
);
339 static SENSOR_DEVICE_ATTR_2(in3_min_alarm
, S_IRUGO
, ltc4245_show_alarm
, NULL
,
340 1 << 2, LTC4245_FAULT1
);
341 static SENSOR_DEVICE_ATTR_2(in4_min_alarm
, S_IRUGO
, ltc4245_show_alarm
, NULL
,
342 1 << 3, LTC4245_FAULT1
);
344 /* Currents (via sense resistor) */
345 static SENSOR_DEVICE_ATTR(curr1_input
, S_IRUGO
, ltc4245_show_current
, NULL
,
347 static SENSOR_DEVICE_ATTR(curr2_input
, S_IRUGO
, ltc4245_show_current
, NULL
,
349 static SENSOR_DEVICE_ATTR(curr3_input
, S_IRUGO
, ltc4245_show_current
, NULL
,
351 static SENSOR_DEVICE_ATTR(curr4_input
, S_IRUGO
, ltc4245_show_current
, NULL
,
354 /* Overcurrent alarms */
355 static SENSOR_DEVICE_ATTR_2(curr1_max_alarm
, S_IRUGO
, ltc4245_show_alarm
, NULL
,
356 1 << 4, LTC4245_FAULT1
);
357 static SENSOR_DEVICE_ATTR_2(curr2_max_alarm
, S_IRUGO
, ltc4245_show_alarm
, NULL
,
358 1 << 5, LTC4245_FAULT1
);
359 static SENSOR_DEVICE_ATTR_2(curr3_max_alarm
, S_IRUGO
, ltc4245_show_alarm
, NULL
,
360 1 << 6, LTC4245_FAULT1
);
361 static SENSOR_DEVICE_ATTR_2(curr4_max_alarm
, S_IRUGO
, ltc4245_show_alarm
, NULL
,
362 1 << 7, LTC4245_FAULT1
);
364 /* Output voltages */
365 static SENSOR_DEVICE_ATTR(in5_input
, S_IRUGO
, ltc4245_show_voltage
, NULL
,
367 static SENSOR_DEVICE_ATTR(in6_input
, S_IRUGO
, ltc4245_show_voltage
, NULL
,
369 static SENSOR_DEVICE_ATTR(in7_input
, S_IRUGO
, ltc4245_show_voltage
, NULL
,
371 static SENSOR_DEVICE_ATTR(in8_input
, S_IRUGO
, ltc4245_show_voltage
, NULL
,
374 /* Power Bad alarms */
375 static SENSOR_DEVICE_ATTR_2(in5_min_alarm
, S_IRUGO
, ltc4245_show_alarm
, NULL
,
376 1 << 0, LTC4245_FAULT2
);
377 static SENSOR_DEVICE_ATTR_2(in6_min_alarm
, S_IRUGO
, ltc4245_show_alarm
, NULL
,
378 1 << 1, LTC4245_FAULT2
);
379 static SENSOR_DEVICE_ATTR_2(in7_min_alarm
, S_IRUGO
, ltc4245_show_alarm
, NULL
,
380 1 << 2, LTC4245_FAULT2
);
381 static SENSOR_DEVICE_ATTR_2(in8_min_alarm
, S_IRUGO
, ltc4245_show_alarm
, NULL
,
382 1 << 3, LTC4245_FAULT2
);
385 static SENSOR_DEVICE_ATTR(in9_input
, S_IRUGO
, ltc4245_show_gpio
, NULL
, 0);
386 static SENSOR_DEVICE_ATTR(in10_input
, S_IRUGO
, ltc4245_show_gpio
, NULL
, 1);
387 static SENSOR_DEVICE_ATTR(in11_input
, S_IRUGO
, ltc4245_show_gpio
, NULL
, 2);
389 /* Power Consumption (virtual) */
390 static SENSOR_DEVICE_ATTR(power1_input
, S_IRUGO
, ltc4245_show_power
, NULL
,
392 static SENSOR_DEVICE_ATTR(power2_input
, S_IRUGO
, ltc4245_show_power
, NULL
,
394 static SENSOR_DEVICE_ATTR(power3_input
, S_IRUGO
, ltc4245_show_power
, NULL
,
396 static SENSOR_DEVICE_ATTR(power4_input
, S_IRUGO
, ltc4245_show_power
, NULL
,
400 * Finally, construct an array of pointers to members of the above objects,
401 * as required for sysfs_create_group()
403 static struct attribute
*ltc4245_std_attributes
[] = {
404 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
405 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
406 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
407 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
409 &sensor_dev_attr_in1_min_alarm
.dev_attr
.attr
,
410 &sensor_dev_attr_in2_min_alarm
.dev_attr
.attr
,
411 &sensor_dev_attr_in3_min_alarm
.dev_attr
.attr
,
412 &sensor_dev_attr_in4_min_alarm
.dev_attr
.attr
,
414 &sensor_dev_attr_curr1_input
.dev_attr
.attr
,
415 &sensor_dev_attr_curr2_input
.dev_attr
.attr
,
416 &sensor_dev_attr_curr3_input
.dev_attr
.attr
,
417 &sensor_dev_attr_curr4_input
.dev_attr
.attr
,
419 &sensor_dev_attr_curr1_max_alarm
.dev_attr
.attr
,
420 &sensor_dev_attr_curr2_max_alarm
.dev_attr
.attr
,
421 &sensor_dev_attr_curr3_max_alarm
.dev_attr
.attr
,
422 &sensor_dev_attr_curr4_max_alarm
.dev_attr
.attr
,
424 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
425 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
426 &sensor_dev_attr_in7_input
.dev_attr
.attr
,
427 &sensor_dev_attr_in8_input
.dev_attr
.attr
,
429 &sensor_dev_attr_in5_min_alarm
.dev_attr
.attr
,
430 &sensor_dev_attr_in6_min_alarm
.dev_attr
.attr
,
431 &sensor_dev_attr_in7_min_alarm
.dev_attr
.attr
,
432 &sensor_dev_attr_in8_min_alarm
.dev_attr
.attr
,
434 &sensor_dev_attr_in9_input
.dev_attr
.attr
,
436 &sensor_dev_attr_power1_input
.dev_attr
.attr
,
437 &sensor_dev_attr_power2_input
.dev_attr
.attr
,
438 &sensor_dev_attr_power3_input
.dev_attr
.attr
,
439 &sensor_dev_attr_power4_input
.dev_attr
.attr
,
444 static struct attribute
*ltc4245_gpio_attributes
[] = {
445 &sensor_dev_attr_in10_input
.dev_attr
.attr
,
446 &sensor_dev_attr_in11_input
.dev_attr
.attr
,
450 static const struct attribute_group ltc4245_std_group
= {
451 .attrs
= ltc4245_std_attributes
,
454 static const struct attribute_group ltc4245_gpio_group
= {
455 .attrs
= ltc4245_gpio_attributes
,
458 static int ltc4245_sysfs_create_groups(struct i2c_client
*client
)
460 struct ltc4245_data
*data
= i2c_get_clientdata(client
);
461 struct device
*dev
= &client
->dev
;
464 /* register the standard sysfs attributes */
465 ret
= sysfs_create_group(&dev
->kobj
, <c4245_std_group
);
467 dev_err(dev
, "unable to register standard attributes\n");
471 /* if we're using the extra gpio support, register it's attributes */
472 if (data
->use_extra_gpios
) {
473 ret
= sysfs_create_group(&dev
->kobj
, <c4245_gpio_group
);
475 dev_err(dev
, "unable to register gpio attributes\n");
476 sysfs_remove_group(&dev
->kobj
, <c4245_std_group
);
484 static void ltc4245_sysfs_remove_groups(struct i2c_client
*client
)
486 struct ltc4245_data
*data
= i2c_get_clientdata(client
);
487 struct device
*dev
= &client
->dev
;
489 if (data
->use_extra_gpios
)
490 sysfs_remove_group(&dev
->kobj
, <c4245_gpio_group
);
492 sysfs_remove_group(&dev
->kobj
, <c4245_std_group
);
495 static bool ltc4245_use_extra_gpios(struct i2c_client
*client
)
497 struct ltc4245_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
499 struct device_node
*np
= client
->dev
.of_node
;
502 /* prefer platform data */
504 return pdata
->use_extra_gpios
;
508 if (of_find_property(np
, "ltc4245,use-extra-gpios", NULL
))
515 static int ltc4245_probe(struct i2c_client
*client
,
516 const struct i2c_device_id
*id
)
518 struct i2c_adapter
*adapter
= client
->adapter
;
519 struct ltc4245_data
*data
;
522 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
525 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
529 i2c_set_clientdata(client
, data
);
530 mutex_init(&data
->update_lock
);
531 data
->use_extra_gpios
= ltc4245_use_extra_gpios(client
);
533 /* Initialize the LTC4245 chip */
534 i2c_smbus_write_byte_data(client
, LTC4245_FAULT1
, 0x00);
535 i2c_smbus_write_byte_data(client
, LTC4245_FAULT2
, 0x00);
537 /* Register sysfs hooks */
538 ret
= ltc4245_sysfs_create_groups(client
);
542 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
543 if (IS_ERR(data
->hwmon_dev
)) {
544 ret
= PTR_ERR(data
->hwmon_dev
);
545 goto out_hwmon_device_register
;
550 out_hwmon_device_register
:
551 ltc4245_sysfs_remove_groups(client
);
555 static int ltc4245_remove(struct i2c_client
*client
)
557 struct ltc4245_data
*data
= i2c_get_clientdata(client
);
559 hwmon_device_unregister(data
->hwmon_dev
);
560 ltc4245_sysfs_remove_groups(client
);
565 static const struct i2c_device_id ltc4245_id
[] = {
569 MODULE_DEVICE_TABLE(i2c
, ltc4245_id
);
571 /* This is the driver that will be inserted */
572 static struct i2c_driver ltc4245_driver
= {
576 .probe
= ltc4245_probe
,
577 .remove
= ltc4245_remove
,
578 .id_table
= ltc4245_id
,
581 module_i2c_driver(ltc4245_driver
);
583 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
584 MODULE_DESCRIPTION("LTC4245 driver");
585 MODULE_LICENSE("GPL");