1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Linear Technology LTC4261 I2C Negative Voltage Hot Swap Controller
5 * Copyright (C) 2010 Ericsson AB.
9 * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller
10 * Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu>
12 * Datasheet: http://cds.linear.com/docs/Datasheet/42612fb.pdf
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/i2c.h>
21 #include <linux/hwmon.h>
22 #include <linux/hwmon-sysfs.h>
23 #include <linux/jiffies.h>
26 #define LTC4261_STATUS 0x00 /* readonly */
27 #define LTC4261_FAULT 0x01
28 #define LTC4261_ALERT 0x02
29 #define LTC4261_CONTROL 0x03
30 #define LTC4261_SENSE_H 0x04
31 #define LTC4261_SENSE_L 0x05
32 #define LTC4261_ADIN2_H 0x06
33 #define LTC4261_ADIN2_L 0x07
34 #define LTC4261_ADIN_H 0x08
35 #define LTC4261_ADIN_L 0x09
40 #define FAULT_OV (1<<0)
41 #define FAULT_UV (1<<1)
42 #define FAULT_OC (1<<2)
45 struct i2c_client
*client
;
47 struct mutex update_lock
;
49 unsigned long last_updated
; /* in jiffies */
55 static struct ltc4261_data
*ltc4261_update_device(struct device
*dev
)
57 struct ltc4261_data
*data
= dev_get_drvdata(dev
);
58 struct i2c_client
*client
= data
->client
;
59 struct ltc4261_data
*ret
= data
;
61 mutex_lock(&data
->update_lock
);
63 if (time_after(jiffies
, data
->last_updated
+ HZ
/ 4) || !data
->valid
) {
66 /* Read registers -- 0x00 to 0x09 */
67 for (i
= 0; i
< ARRAY_SIZE(data
->regs
); i
++) {
70 val
= i2c_smbus_read_byte_data(client
, i
);
71 if (unlikely(val
< 0)) {
73 "Failed to read ADC value: error %d\n",
81 data
->last_updated
= jiffies
;
85 mutex_unlock(&data
->update_lock
);
89 /* Return the voltage from the given register in mV or mA */
90 static int ltc4261_get_value(struct ltc4261_data
*data
, u8 reg
)
94 val
= (data
->regs
[reg
] << 2) + (data
->regs
[reg
+ 1] >> 6);
99 /* 2.5mV resolution. Convert to mV. */
102 case LTC4261_SENSE_H
:
104 * 62.5uV resolution. Convert to current as measured with
105 * an 1 mOhm sense resistor, in mA. If a different sense
106 * resistor is installed, calculate the actual current by
107 * dividing the reported current by the sense resistor value
110 val
= val
* 625 / 10;
113 /* If we get here, the developer messed up */
122 static ssize_t
ltc4261_value_show(struct device
*dev
,
123 struct device_attribute
*da
, char *buf
)
125 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
126 struct ltc4261_data
*data
= ltc4261_update_device(dev
);
130 return PTR_ERR(data
);
132 value
= ltc4261_get_value(data
, attr
->index
);
133 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
136 static ssize_t
ltc4261_bool_show(struct device
*dev
,
137 struct device_attribute
*da
, char *buf
)
139 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
140 struct ltc4261_data
*data
= ltc4261_update_device(dev
);
144 return PTR_ERR(data
);
146 fault
= data
->regs
[LTC4261_FAULT
] & attr
->index
;
147 if (fault
) /* Clear reported faults in chip register */
148 i2c_smbus_write_byte_data(data
->client
, LTC4261_FAULT
, ~fault
);
150 return snprintf(buf
, PAGE_SIZE
, "%d\n", fault
? 1 : 0);
156 static SENSOR_DEVICE_ATTR_RO(in1_input
, ltc4261_value
, LTC4261_ADIN_H
);
157 static SENSOR_DEVICE_ATTR_RO(in2_input
, ltc4261_value
, LTC4261_ADIN2_H
);
160 * Voltage alarms. The chip has only one set of voltage alarm status bits,
161 * triggered by input voltage alarms. In many designs, those alarms are
162 * associated with the ADIN2 sensor, due to the proximity of the ADIN2 pin
163 * to the OV pin. ADIN2 is, however, not available on all chip variants.
164 * To ensure that the alarm condition is reported to the user, report it
165 * with both voltage sensors.
167 static SENSOR_DEVICE_ATTR_RO(in1_min_alarm
, ltc4261_bool
, FAULT_UV
);
168 static SENSOR_DEVICE_ATTR_RO(in1_max_alarm
, ltc4261_bool
, FAULT_OV
);
169 static SENSOR_DEVICE_ATTR_RO(in2_min_alarm
, ltc4261_bool
, FAULT_UV
);
170 static SENSOR_DEVICE_ATTR_RO(in2_max_alarm
, ltc4261_bool
, FAULT_OV
);
172 /* Currents (via sense resistor) */
173 static SENSOR_DEVICE_ATTR_RO(curr1_input
, ltc4261_value
, LTC4261_SENSE_H
);
175 /* Overcurrent alarm */
176 static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm
, ltc4261_bool
, FAULT_OC
);
178 static struct attribute
*ltc4261_attrs
[] = {
179 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
180 &sensor_dev_attr_in1_min_alarm
.dev_attr
.attr
,
181 &sensor_dev_attr_in1_max_alarm
.dev_attr
.attr
,
182 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
183 &sensor_dev_attr_in2_min_alarm
.dev_attr
.attr
,
184 &sensor_dev_attr_in2_max_alarm
.dev_attr
.attr
,
186 &sensor_dev_attr_curr1_input
.dev_attr
.attr
,
187 &sensor_dev_attr_curr1_max_alarm
.dev_attr
.attr
,
191 ATTRIBUTE_GROUPS(ltc4261
);
193 static int ltc4261_probe(struct i2c_client
*client
,
194 const struct i2c_device_id
*id
)
196 struct i2c_adapter
*adapter
= client
->adapter
;
197 struct device
*dev
= &client
->dev
;
198 struct ltc4261_data
*data
;
199 struct device
*hwmon_dev
;
201 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
204 if (i2c_smbus_read_byte_data(client
, LTC4261_STATUS
) < 0) {
205 dev_err(dev
, "Failed to read status register\n");
209 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
213 data
->client
= client
;
214 mutex_init(&data
->update_lock
);
217 i2c_smbus_write_byte_data(client
, LTC4261_FAULT
, 0x00);
219 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
222 return PTR_ERR_OR_ZERO(hwmon_dev
);
225 static const struct i2c_device_id ltc4261_id
[] = {
230 MODULE_DEVICE_TABLE(i2c
, ltc4261_id
);
232 /* This is the driver that will be inserted */
233 static struct i2c_driver ltc4261_driver
= {
237 .probe
= ltc4261_probe
,
238 .id_table
= ltc4261_id
,
241 module_i2c_driver(ltc4261_driver
);
243 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
244 MODULE_DESCRIPTION("LTC4261 driver");
245 MODULE_LICENSE("GPL");