174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e5f5c99aSGuenter Roeck /*
3e5f5c99aSGuenter Roeck * Driver for Linear Technology LTC4261 I2C Negative Voltage Hot Swap Controller
4e5f5c99aSGuenter Roeck *
5e5f5c99aSGuenter Roeck * Copyright (C) 2010 Ericsson AB.
6e5f5c99aSGuenter Roeck *
7e5f5c99aSGuenter Roeck * Derived from:
8e5f5c99aSGuenter Roeck *
9e5f5c99aSGuenter Roeck * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller
10e5f5c99aSGuenter Roeck * Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu>
11e5f5c99aSGuenter Roeck *
12e5f5c99aSGuenter Roeck * Datasheet: http://cds.linear.com/docs/Datasheet/42612fb.pdf
13e5f5c99aSGuenter Roeck */
14e5f5c99aSGuenter Roeck
15e5f5c99aSGuenter Roeck #include <linux/kernel.h>
16e5f5c99aSGuenter Roeck #include <linux/module.h>
17e5f5c99aSGuenter Roeck #include <linux/init.h>
18e5f5c99aSGuenter Roeck #include <linux/err.h>
19e5f5c99aSGuenter Roeck #include <linux/slab.h>
20e5f5c99aSGuenter Roeck #include <linux/i2c.h>
21e5f5c99aSGuenter Roeck #include <linux/hwmon.h>
22e5f5c99aSGuenter Roeck #include <linux/hwmon-sysfs.h>
23dcd8f392SJean Delvare #include <linux/jiffies.h>
24e5f5c99aSGuenter Roeck
25e5f5c99aSGuenter Roeck /* chip registers */
26e5f5c99aSGuenter Roeck #define LTC4261_STATUS 0x00 /* readonly */
27e5f5c99aSGuenter Roeck #define LTC4261_FAULT 0x01
28e5f5c99aSGuenter Roeck #define LTC4261_ALERT 0x02
29e5f5c99aSGuenter Roeck #define LTC4261_CONTROL 0x03
30e5f5c99aSGuenter Roeck #define LTC4261_SENSE_H 0x04
31e5f5c99aSGuenter Roeck #define LTC4261_SENSE_L 0x05
32e5f5c99aSGuenter Roeck #define LTC4261_ADIN2_H 0x06
33e5f5c99aSGuenter Roeck #define LTC4261_ADIN2_L 0x07
34e5f5c99aSGuenter Roeck #define LTC4261_ADIN_H 0x08
35e5f5c99aSGuenter Roeck #define LTC4261_ADIN_L 0x09
36e5f5c99aSGuenter Roeck
37e5f5c99aSGuenter Roeck /*
38e5f5c99aSGuenter Roeck * Fault register bits
39e5f5c99aSGuenter Roeck */
40e5f5c99aSGuenter Roeck #define FAULT_OV (1<<0)
41e5f5c99aSGuenter Roeck #define FAULT_UV (1<<1)
42e5f5c99aSGuenter Roeck #define FAULT_OC (1<<2)
43e5f5c99aSGuenter Roeck
44e5f5c99aSGuenter Roeck struct ltc4261_data {
4538f150feSGuenter Roeck struct i2c_client *client;
46e5f5c99aSGuenter Roeck
47e5f5c99aSGuenter Roeck struct mutex update_lock;
48e5f5c99aSGuenter Roeck bool valid;
49e5f5c99aSGuenter Roeck unsigned long last_updated; /* in jiffies */
50e5f5c99aSGuenter Roeck
51e5f5c99aSGuenter Roeck /* Registers */
52e5f5c99aSGuenter Roeck u8 regs[10];
53e5f5c99aSGuenter Roeck };
54e5f5c99aSGuenter Roeck
ltc4261_update_device(struct device * dev)55e5f5c99aSGuenter Roeck static struct ltc4261_data *ltc4261_update_device(struct device *dev)
56e5f5c99aSGuenter Roeck {
5738f150feSGuenter Roeck struct ltc4261_data *data = dev_get_drvdata(dev);
5838f150feSGuenter Roeck struct i2c_client *client = data->client;
59e5f5c99aSGuenter Roeck struct ltc4261_data *ret = data;
60e5f5c99aSGuenter Roeck
61e5f5c99aSGuenter Roeck mutex_lock(&data->update_lock);
62e5f5c99aSGuenter Roeck
63e5f5c99aSGuenter Roeck if (time_after(jiffies, data->last_updated + HZ / 4) || !data->valid) {
64e5f5c99aSGuenter Roeck int i;
65e5f5c99aSGuenter Roeck
66e5f5c99aSGuenter Roeck /* Read registers -- 0x00 to 0x09 */
67e5f5c99aSGuenter Roeck for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
68e5f5c99aSGuenter Roeck int val;
69e5f5c99aSGuenter Roeck
70e5f5c99aSGuenter Roeck val = i2c_smbus_read_byte_data(client, i);
71e5f5c99aSGuenter Roeck if (unlikely(val < 0)) {
72e5f5c99aSGuenter Roeck dev_dbg(dev,
7369f8b741SGuenter Roeck "Failed to read ADC value: error %d\n",
74e5f5c99aSGuenter Roeck val);
75e5f5c99aSGuenter Roeck ret = ERR_PTR(val);
76952a11caSPaul Fertser data->valid = false;
77e5f5c99aSGuenter Roeck goto abort;
78e5f5c99aSGuenter Roeck }
79e5f5c99aSGuenter Roeck data->regs[i] = val;
80e5f5c99aSGuenter Roeck }
81e5f5c99aSGuenter Roeck data->last_updated = jiffies;
82952a11caSPaul Fertser data->valid = true;
83e5f5c99aSGuenter Roeck }
84e5f5c99aSGuenter Roeck abort:
85e5f5c99aSGuenter Roeck mutex_unlock(&data->update_lock);
86e5f5c99aSGuenter Roeck return ret;
87e5f5c99aSGuenter Roeck }
88e5f5c99aSGuenter Roeck
89e5f5c99aSGuenter Roeck /* Return the voltage from the given register in mV or mA */
ltc4261_get_value(struct ltc4261_data * data,u8 reg)90e5f5c99aSGuenter Roeck static int ltc4261_get_value(struct ltc4261_data *data, u8 reg)
91e5f5c99aSGuenter Roeck {
92e5f5c99aSGuenter Roeck u32 val;
93e5f5c99aSGuenter Roeck
94e5f5c99aSGuenter Roeck val = (data->regs[reg] << 2) + (data->regs[reg + 1] >> 6);
95e5f5c99aSGuenter Roeck
96e5f5c99aSGuenter Roeck switch (reg) {
97e5f5c99aSGuenter Roeck case LTC4261_ADIN_H:
98e5f5c99aSGuenter Roeck case LTC4261_ADIN2_H:
99e5f5c99aSGuenter Roeck /* 2.5mV resolution. Convert to mV. */
100e5f5c99aSGuenter Roeck val = val * 25 / 10;
101e5f5c99aSGuenter Roeck break;
102e5f5c99aSGuenter Roeck case LTC4261_SENSE_H:
103e5f5c99aSGuenter Roeck /*
104e5f5c99aSGuenter Roeck * 62.5uV resolution. Convert to current as measured with
105e5f5c99aSGuenter Roeck * an 1 mOhm sense resistor, in mA. If a different sense
106e5f5c99aSGuenter Roeck * resistor is installed, calculate the actual current by
107e5f5c99aSGuenter Roeck * dividing the reported current by the sense resistor value
108e5f5c99aSGuenter Roeck * in mOhm.
109e5f5c99aSGuenter Roeck */
110e5f5c99aSGuenter Roeck val = val * 625 / 10;
111e5f5c99aSGuenter Roeck break;
112e5f5c99aSGuenter Roeck default:
113e5f5c99aSGuenter Roeck /* If we get here, the developer messed up */
114e5f5c99aSGuenter Roeck WARN_ON_ONCE(1);
115e5f5c99aSGuenter Roeck val = 0;
116e5f5c99aSGuenter Roeck break;
117e5f5c99aSGuenter Roeck }
118e5f5c99aSGuenter Roeck
119e5f5c99aSGuenter Roeck return val;
120e5f5c99aSGuenter Roeck }
121e5f5c99aSGuenter Roeck
ltc4261_value_show(struct device * dev,struct device_attribute * da,char * buf)122decb23dcSGuenter Roeck static ssize_t ltc4261_value_show(struct device *dev,
123e5f5c99aSGuenter Roeck struct device_attribute *da, char *buf)
124e5f5c99aSGuenter Roeck {
125e5f5c99aSGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
126e5f5c99aSGuenter Roeck struct ltc4261_data *data = ltc4261_update_device(dev);
127e5f5c99aSGuenter Roeck int value;
128e5f5c99aSGuenter Roeck
129e5f5c99aSGuenter Roeck if (IS_ERR(data))
130e5f5c99aSGuenter Roeck return PTR_ERR(data);
131e5f5c99aSGuenter Roeck
132e5f5c99aSGuenter Roeck value = ltc4261_get_value(data, attr->index);
1331f4d4af4SGuenter Roeck return sysfs_emit(buf, "%d\n", value);
134e5f5c99aSGuenter Roeck }
135e5f5c99aSGuenter Roeck
ltc4261_bool_show(struct device * dev,struct device_attribute * da,char * buf)136decb23dcSGuenter Roeck static ssize_t ltc4261_bool_show(struct device *dev,
137e5f5c99aSGuenter Roeck struct device_attribute *da, char *buf)
138e5f5c99aSGuenter Roeck {
139e5f5c99aSGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
140e5f5c99aSGuenter Roeck struct ltc4261_data *data = ltc4261_update_device(dev);
141e5f5c99aSGuenter Roeck u8 fault;
142e5f5c99aSGuenter Roeck
143e5f5c99aSGuenter Roeck if (IS_ERR(data))
144e5f5c99aSGuenter Roeck return PTR_ERR(data);
145e5f5c99aSGuenter Roeck
146e5f5c99aSGuenter Roeck fault = data->regs[LTC4261_FAULT] & attr->index;
147e5f5c99aSGuenter Roeck if (fault) /* Clear reported faults in chip register */
14838f150feSGuenter Roeck i2c_smbus_write_byte_data(data->client, LTC4261_FAULT, ~fault);
149e5f5c99aSGuenter Roeck
1501f4d4af4SGuenter Roeck return sysfs_emit(buf, "%d\n", fault ? 1 : 0);
151e5f5c99aSGuenter Roeck }
152e5f5c99aSGuenter Roeck
153e5f5c99aSGuenter Roeck /*
154e5f5c99aSGuenter Roeck * Input voltages.
155e5f5c99aSGuenter Roeck */
156decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4261_value, LTC4261_ADIN_H);
157decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4261_value, LTC4261_ADIN2_H);
158e5f5c99aSGuenter Roeck
159e5f5c99aSGuenter Roeck /*
160e5f5c99aSGuenter Roeck * Voltage alarms. The chip has only one set of voltage alarm status bits,
161e5f5c99aSGuenter Roeck * triggered by input voltage alarms. In many designs, those alarms are
162e5f5c99aSGuenter Roeck * associated with the ADIN2 sensor, due to the proximity of the ADIN2 pin
163e5f5c99aSGuenter Roeck * to the OV pin. ADIN2 is, however, not available on all chip variants.
164e5f5c99aSGuenter Roeck * To ensure that the alarm condition is reported to the user, report it
165e5f5c99aSGuenter Roeck * with both voltage sensors.
166e5f5c99aSGuenter Roeck */
167decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ltc4261_bool, FAULT_UV);
168decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ltc4261_bool, FAULT_OV);
169decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in2_min_alarm, ltc4261_bool, FAULT_UV);
170decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in2_max_alarm, ltc4261_bool, FAULT_OV);
171e5f5c99aSGuenter Roeck
172e5f5c99aSGuenter Roeck /* Currents (via sense resistor) */
173decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4261_value, LTC4261_SENSE_H);
174e5f5c99aSGuenter Roeck
175e5f5c99aSGuenter Roeck /* Overcurrent alarm */
176decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm, ltc4261_bool, FAULT_OC);
177e5f5c99aSGuenter Roeck
17838f150feSGuenter Roeck static struct attribute *ltc4261_attrs[] = {
179e5f5c99aSGuenter Roeck &sensor_dev_attr_in1_input.dev_attr.attr,
180e5f5c99aSGuenter Roeck &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
181e5f5c99aSGuenter Roeck &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
182e5f5c99aSGuenter Roeck &sensor_dev_attr_in2_input.dev_attr.attr,
183e5f5c99aSGuenter Roeck &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
184e5f5c99aSGuenter Roeck &sensor_dev_attr_in2_max_alarm.dev_attr.attr,
185e5f5c99aSGuenter Roeck
186e5f5c99aSGuenter Roeck &sensor_dev_attr_curr1_input.dev_attr.attr,
187e5f5c99aSGuenter Roeck &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
188e5f5c99aSGuenter Roeck
189e5f5c99aSGuenter Roeck NULL,
190e5f5c99aSGuenter Roeck };
19138f150feSGuenter Roeck ATTRIBUTE_GROUPS(ltc4261);
192e5f5c99aSGuenter Roeck
ltc4261_probe(struct i2c_client * client)19367487038SStephen Kitt static int ltc4261_probe(struct i2c_client *client)
194e5f5c99aSGuenter Roeck {
195e5f5c99aSGuenter Roeck struct i2c_adapter *adapter = client->adapter;
19638f150feSGuenter Roeck struct device *dev = &client->dev;
197e5f5c99aSGuenter Roeck struct ltc4261_data *data;
19838f150feSGuenter Roeck struct device *hwmon_dev;
199e5f5c99aSGuenter Roeck
200e5f5c99aSGuenter Roeck if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
201e5f5c99aSGuenter Roeck return -ENODEV;
202e5f5c99aSGuenter Roeck
203e5f5c99aSGuenter Roeck if (i2c_smbus_read_byte_data(client, LTC4261_STATUS) < 0) {
20438f150feSGuenter Roeck dev_err(dev, "Failed to read status register\n");
205e5f5c99aSGuenter Roeck return -ENODEV;
206e5f5c99aSGuenter Roeck }
207e5f5c99aSGuenter Roeck
20838f150feSGuenter Roeck data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
20936839287SGuenter Roeck if (!data)
21036839287SGuenter Roeck return -ENOMEM;
211e5f5c99aSGuenter Roeck
21238f150feSGuenter Roeck data->client = client;
213e5f5c99aSGuenter Roeck mutex_init(&data->update_lock);
214e5f5c99aSGuenter Roeck
215e5f5c99aSGuenter Roeck /* Clear faults */
216e5f5c99aSGuenter Roeck i2c_smbus_write_byte_data(client, LTC4261_FAULT, 0x00);
217e5f5c99aSGuenter Roeck
21838f150feSGuenter Roeck hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
21938f150feSGuenter Roeck data,
22038f150feSGuenter Roeck ltc4261_groups);
2212a253c47SFengguang Wu return PTR_ERR_OR_ZERO(hwmon_dev);
222e5f5c99aSGuenter Roeck }
223e5f5c99aSGuenter Roeck
224e5f5c99aSGuenter Roeck static const struct i2c_device_id ltc4261_id[] = {
225e5f5c99aSGuenter Roeck {"ltc4261", 0},
226e5f5c99aSGuenter Roeck {}
227e5f5c99aSGuenter Roeck };
228e5f5c99aSGuenter Roeck
229e5f5c99aSGuenter Roeck MODULE_DEVICE_TABLE(i2c, ltc4261_id);
230e5f5c99aSGuenter Roeck
231e5f5c99aSGuenter Roeck /* This is the driver that will be inserted */
232e5f5c99aSGuenter Roeck static struct i2c_driver ltc4261_driver = {
233e5f5c99aSGuenter Roeck .driver = {
234e5f5c99aSGuenter Roeck .name = "ltc4261",
235e5f5c99aSGuenter Roeck },
236*1975d167SUwe Kleine-König .probe = ltc4261_probe,
237e5f5c99aSGuenter Roeck .id_table = ltc4261_id,
238e5f5c99aSGuenter Roeck };
239e5f5c99aSGuenter Roeck
240f0967eeaSAxel Lin module_i2c_driver(ltc4261_driver);
241e5f5c99aSGuenter Roeck
242bb9a80e5SGuenter Roeck MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
243e5f5c99aSGuenter Roeck MODULE_DESCRIPTION("LTC4261 driver");
244e5f5c99aSGuenter Roeck MODULE_LICENSE("GPL");
245