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>
38 #define LTC4261_STATUS 0x00 /* readonly */
39 #define LTC4261_FAULT 0x01
40 #define LTC4261_ALERT 0x02
41 #define LTC4261_CONTROL 0x03
42 #define LTC4261_SENSE_H 0x04
43 #define LTC4261_SENSE_L 0x05
44 #define LTC4261_ADIN2_H 0x06
45 #define LTC4261_ADIN2_L 0x07
46 #define LTC4261_ADIN_H 0x08
47 #define LTC4261_ADIN_L 0x09
52 #define FAULT_OV (1<<0)
53 #define FAULT_UV (1<<1)
54 #define FAULT_OC (1<<2)
57 struct device
*hwmon_dev
;
59 struct mutex update_lock
;
61 unsigned long last_updated
; /* in jiffies */
67 static struct ltc4261_data
*ltc4261_update_device(struct device
*dev
)
69 struct i2c_client
*client
= to_i2c_client(dev
);
70 struct ltc4261_data
*data
= i2c_get_clientdata(client
);
71 struct ltc4261_data
*ret
= data
;
73 mutex_lock(&data
->update_lock
);
75 if (time_after(jiffies
, data
->last_updated
+ HZ
/ 4) || !data
->valid
) {
78 /* Read registers -- 0x00 to 0x09 */
79 for (i
= 0; i
< ARRAY_SIZE(data
->regs
); i
++) {
82 val
= i2c_smbus_read_byte_data(client
, i
);
83 if (unlikely(val
< 0)) {
85 "Failed to read ADC value: error %d\n",
92 data
->last_updated
= jiffies
;
96 mutex_unlock(&data
->update_lock
);
100 /* Return the voltage from the given register in mV or mA */
101 static int ltc4261_get_value(struct ltc4261_data
*data
, u8 reg
)
105 val
= (data
->regs
[reg
] << 2) + (data
->regs
[reg
+ 1] >> 6);
109 case LTC4261_ADIN2_H
:
110 /* 2.5mV resolution. Convert to mV. */
113 case LTC4261_SENSE_H
:
115 * 62.5uV resolution. Convert to current as measured with
116 * an 1 mOhm sense resistor, in mA. If a different sense
117 * resistor is installed, calculate the actual current by
118 * dividing the reported current by the sense resistor value
121 val
= val
* 625 / 10;
124 /* If we get here, the developer messed up */
133 static ssize_t
ltc4261_show_value(struct device
*dev
,
134 struct device_attribute
*da
, char *buf
)
136 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
137 struct ltc4261_data
*data
= ltc4261_update_device(dev
);
141 return PTR_ERR(data
);
143 value
= ltc4261_get_value(data
, attr
->index
);
144 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
147 static ssize_t
ltc4261_show_bool(struct device
*dev
,
148 struct device_attribute
*da
, char *buf
)
150 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
151 struct i2c_client
*client
= to_i2c_client(dev
);
152 struct ltc4261_data
*data
= ltc4261_update_device(dev
);
156 return PTR_ERR(data
);
158 fault
= data
->regs
[LTC4261_FAULT
] & attr
->index
;
159 if (fault
) /* Clear reported faults in chip register */
160 i2c_smbus_write_byte_data(client
, LTC4261_FAULT
, ~fault
);
162 return snprintf(buf
, PAGE_SIZE
, "%d\n", fault
? 1 : 0);
166 * These macros are used below in constructing device attribute objects
167 * for use with sysfs_create_group() to make a sysfs device file
171 #define LTC4261_VALUE(name, ltc4261_cmd_idx) \
172 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
173 ltc4261_show_value, NULL, ltc4261_cmd_idx)
175 #define LTC4261_BOOL(name, mask) \
176 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
177 ltc4261_show_bool, NULL, (mask))
182 LTC4261_VALUE(in1_input
, LTC4261_ADIN_H
);
183 LTC4261_VALUE(in2_input
, LTC4261_ADIN2_H
);
186 * Voltage alarms. The chip has only one set of voltage alarm status bits,
187 * triggered by input voltage alarms. In many designs, those alarms are
188 * associated with the ADIN2 sensor, due to the proximity of the ADIN2 pin
189 * to the OV pin. ADIN2 is, however, not available on all chip variants.
190 * To ensure that the alarm condition is reported to the user, report it
191 * with both voltage sensors.
193 LTC4261_BOOL(in1_min_alarm
, FAULT_UV
);
194 LTC4261_BOOL(in1_max_alarm
, FAULT_OV
);
195 LTC4261_BOOL(in2_min_alarm
, FAULT_UV
);
196 LTC4261_BOOL(in2_max_alarm
, FAULT_OV
);
198 /* Currents (via sense resistor) */
199 LTC4261_VALUE(curr1_input
, LTC4261_SENSE_H
);
201 /* Overcurrent alarm */
202 LTC4261_BOOL(curr1_max_alarm
, FAULT_OC
);
204 static struct attribute
*ltc4261_attributes
[] = {
205 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
206 &sensor_dev_attr_in1_min_alarm
.dev_attr
.attr
,
207 &sensor_dev_attr_in1_max_alarm
.dev_attr
.attr
,
208 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
209 &sensor_dev_attr_in2_min_alarm
.dev_attr
.attr
,
210 &sensor_dev_attr_in2_max_alarm
.dev_attr
.attr
,
212 &sensor_dev_attr_curr1_input
.dev_attr
.attr
,
213 &sensor_dev_attr_curr1_max_alarm
.dev_attr
.attr
,
218 static const struct attribute_group ltc4261_group
= {
219 .attrs
= ltc4261_attributes
,
222 static int ltc4261_probe(struct i2c_client
*client
,
223 const struct i2c_device_id
*id
)
225 struct i2c_adapter
*adapter
= client
->adapter
;
226 struct ltc4261_data
*data
;
229 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
232 if (i2c_smbus_read_byte_data(client
, LTC4261_STATUS
) < 0) {
233 dev_err(&client
->dev
, "Failed to read status register\n");
237 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
243 i2c_set_clientdata(client
, data
);
244 mutex_init(&data
->update_lock
);
247 i2c_smbus_write_byte_data(client
, LTC4261_FAULT
, 0x00);
249 /* Register sysfs hooks */
250 ret
= sysfs_create_group(&client
->dev
.kobj
, <c4261_group
);
252 goto out_sysfs_create_group
;
254 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
255 if (IS_ERR(data
->hwmon_dev
)) {
256 ret
= PTR_ERR(data
->hwmon_dev
);
257 goto out_hwmon_device_register
;
262 out_hwmon_device_register
:
263 sysfs_remove_group(&client
->dev
.kobj
, <c4261_group
);
264 out_sysfs_create_group
:
270 static int ltc4261_remove(struct i2c_client
*client
)
272 struct ltc4261_data
*data
= i2c_get_clientdata(client
);
274 hwmon_device_unregister(data
->hwmon_dev
);
275 sysfs_remove_group(&client
->dev
.kobj
, <c4261_group
);
282 static const struct i2c_device_id ltc4261_id
[] = {
287 MODULE_DEVICE_TABLE(i2c
, ltc4261_id
);
289 /* This is the driver that will be inserted */
290 static struct i2c_driver ltc4261_driver
= {
294 .probe
= ltc4261_probe
,
295 .remove
= ltc4261_remove
,
296 .id_table
= ltc4261_id
,
299 static int __init
ltc4261_init(void)
301 return i2c_add_driver(<c4261_driver
);
304 static void __exit
ltc4261_exit(void)
306 i2c_del_driver(<c4261_driver
);
309 MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>");
310 MODULE_DESCRIPTION("LTC4261 driver");
311 MODULE_LICENSE("GPL");
313 module_init(ltc4261_init
);
314 module_exit(ltc4261_exit
);