2 * Driver for Linear Technology LTC4261 I2C Negative Voltage Hot Swap Controller
4 * Copyright (C) 2010 Ericsson AB.
8 * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller
9 * Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu>
11 * Datasheet: http://cds.linear.com/docs/Datasheet/42612fb.pdf
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/err.h>
32 #include <linux/slab.h>
33 #include <linux/i2c.h>
34 #include <linux/hwmon.h>
35 #include <linux/hwmon-sysfs.h>
36 #include <linux/jiffies.h>
39 #define LTC4261_STATUS 0x00 /* readonly */
40 #define LTC4261_FAULT 0x01
41 #define LTC4261_ALERT 0x02
42 #define LTC4261_CONTROL 0x03
43 #define LTC4261_SENSE_H 0x04
44 #define LTC4261_SENSE_L 0x05
45 #define LTC4261_ADIN2_H 0x06
46 #define LTC4261_ADIN2_L 0x07
47 #define LTC4261_ADIN_H 0x08
48 #define LTC4261_ADIN_L 0x09
53 #define FAULT_OV (1<<0)
54 #define FAULT_UV (1<<1)
55 #define FAULT_OC (1<<2)
58 struct i2c_client
*client
;
60 struct mutex update_lock
;
62 unsigned long last_updated
; /* in jiffies */
68 static struct ltc4261_data
*ltc4261_update_device(struct device
*dev
)
70 struct ltc4261_data
*data
= dev_get_drvdata(dev
);
71 struct i2c_client
*client
= data
->client
;
72 struct ltc4261_data
*ret
= data
;
74 mutex_lock(&data
->update_lock
);
76 if (time_after(jiffies
, data
->last_updated
+ HZ
/ 4) || !data
->valid
) {
79 /* Read registers -- 0x00 to 0x09 */
80 for (i
= 0; i
< ARRAY_SIZE(data
->regs
); i
++) {
83 val
= i2c_smbus_read_byte_data(client
, i
);
84 if (unlikely(val
< 0)) {
86 "Failed to read ADC value: error %d\n",
94 data
->last_updated
= jiffies
;
98 mutex_unlock(&data
->update_lock
);
102 /* Return the voltage from the given register in mV or mA */
103 static int ltc4261_get_value(struct ltc4261_data
*data
, u8 reg
)
107 val
= (data
->regs
[reg
] << 2) + (data
->regs
[reg
+ 1] >> 6);
111 case LTC4261_ADIN2_H
:
112 /* 2.5mV resolution. Convert to mV. */
115 case LTC4261_SENSE_H
:
117 * 62.5uV resolution. Convert to current as measured with
118 * an 1 mOhm sense resistor, in mA. If a different sense
119 * resistor is installed, calculate the actual current by
120 * dividing the reported current by the sense resistor value
123 val
= val
* 625 / 10;
126 /* If we get here, the developer messed up */
135 static ssize_t
ltc4261_show_value(struct device
*dev
,
136 struct device_attribute
*da
, char *buf
)
138 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
139 struct ltc4261_data
*data
= ltc4261_update_device(dev
);
143 return PTR_ERR(data
);
145 value
= ltc4261_get_value(data
, attr
->index
);
146 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
149 static ssize_t
ltc4261_show_bool(struct device
*dev
,
150 struct device_attribute
*da
, char *buf
)
152 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
153 struct ltc4261_data
*data
= ltc4261_update_device(dev
);
157 return PTR_ERR(data
);
159 fault
= data
->regs
[LTC4261_FAULT
] & attr
->index
;
160 if (fault
) /* Clear reported faults in chip register */
161 i2c_smbus_write_byte_data(data
->client
, LTC4261_FAULT
, ~fault
);
163 return snprintf(buf
, PAGE_SIZE
, "%d\n", fault
? 1 : 0);
169 static SENSOR_DEVICE_ATTR(in1_input
, S_IRUGO
, ltc4261_show_value
, NULL
,
171 static SENSOR_DEVICE_ATTR(in2_input
, S_IRUGO
, ltc4261_show_value
, NULL
,
175 * Voltage alarms. The chip has only one set of voltage alarm status bits,
176 * triggered by input voltage alarms. In many designs, those alarms are
177 * associated with the ADIN2 sensor, due to the proximity of the ADIN2 pin
178 * to the OV pin. ADIN2 is, however, not available on all chip variants.
179 * To ensure that the alarm condition is reported to the user, report it
180 * with both voltage sensors.
182 static SENSOR_DEVICE_ATTR(in1_min_alarm
, S_IRUGO
, ltc4261_show_bool
, NULL
,
184 static SENSOR_DEVICE_ATTR(in1_max_alarm
, S_IRUGO
, ltc4261_show_bool
, NULL
,
186 static SENSOR_DEVICE_ATTR(in2_min_alarm
, S_IRUGO
, ltc4261_show_bool
, NULL
,
188 static SENSOR_DEVICE_ATTR(in2_max_alarm
, S_IRUGO
, ltc4261_show_bool
, NULL
,
191 /* Currents (via sense resistor) */
192 static SENSOR_DEVICE_ATTR(curr1_input
, S_IRUGO
, ltc4261_show_value
, NULL
,
195 /* Overcurrent alarm */
196 static SENSOR_DEVICE_ATTR(curr1_max_alarm
, S_IRUGO
, ltc4261_show_bool
, NULL
,
199 static struct attribute
*ltc4261_attrs
[] = {
200 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
201 &sensor_dev_attr_in1_min_alarm
.dev_attr
.attr
,
202 &sensor_dev_attr_in1_max_alarm
.dev_attr
.attr
,
203 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
204 &sensor_dev_attr_in2_min_alarm
.dev_attr
.attr
,
205 &sensor_dev_attr_in2_max_alarm
.dev_attr
.attr
,
207 &sensor_dev_attr_curr1_input
.dev_attr
.attr
,
208 &sensor_dev_attr_curr1_max_alarm
.dev_attr
.attr
,
212 ATTRIBUTE_GROUPS(ltc4261
);
214 static int ltc4261_probe(struct i2c_client
*client
,
215 const struct i2c_device_id
*id
)
217 struct i2c_adapter
*adapter
= client
->adapter
;
218 struct device
*dev
= &client
->dev
;
219 struct ltc4261_data
*data
;
220 struct device
*hwmon_dev
;
222 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
225 if (i2c_smbus_read_byte_data(client
, LTC4261_STATUS
) < 0) {
226 dev_err(dev
, "Failed to read status register\n");
230 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
234 data
->client
= client
;
235 mutex_init(&data
->update_lock
);
238 i2c_smbus_write_byte_data(client
, LTC4261_FAULT
, 0x00);
240 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
243 return PTR_ERR_OR_ZERO(hwmon_dev
);
246 static const struct i2c_device_id ltc4261_id
[] = {
251 MODULE_DEVICE_TABLE(i2c
, ltc4261_id
);
253 /* This is the driver that will be inserted */
254 static struct i2c_driver ltc4261_driver
= {
258 .probe
= ltc4261_probe
,
259 .id_table
= ltc4261_id
,
262 module_i2c_driver(ltc4261_driver
);
264 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
265 MODULE_DESCRIPTION("LTC4261 driver");
266 MODULE_LICENSE("GPL");