blob: 84a2d2872b20c4172a73a045ec1c1c6223dcd27e [file] [log] [blame]
Guenter Roecke5f5c992010-06-25 11:59:54 -07001/*
2 * Driver for Linear Technology LTC4261 I2C Negative Voltage Hot Swap Controller
3 *
4 * Copyright (C) 2010 Ericsson AB.
5 *
6 * Derived from:
7 *
8 * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller
9 * Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu>
10 *
11 * Datasheet: http://cds.linear.com/docs/Datasheet/42612fb.pdf
12 *
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.
17 *
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.
22 *
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.
26 */
27
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>
Jean Delvaredcd8f392012-10-10 15:25:56 +020036#include <linux/jiffies.h>
Guenter Roecke5f5c992010-06-25 11:59:54 -070037
38/* chip registers */
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
49
50/*
51 * Fault register bits
52 */
53#define FAULT_OV (1<<0)
54#define FAULT_UV (1<<1)
55#define FAULT_OC (1<<2)
56
57struct ltc4261_data {
58 struct device *hwmon_dev;
59
60 struct mutex update_lock;
61 bool valid;
62 unsigned long last_updated; /* in jiffies */
63
64 /* Registers */
65 u8 regs[10];
66};
67
68static struct ltc4261_data *ltc4261_update_device(struct device *dev)
69{
70 struct i2c_client *client = to_i2c_client(dev);
71 struct ltc4261_data *data = i2c_get_clientdata(client);
72 struct ltc4261_data *ret = data;
73
74 mutex_lock(&data->update_lock);
75
76 if (time_after(jiffies, data->last_updated + HZ / 4) || !data->valid) {
77 int i;
78
79 /* Read registers -- 0x00 to 0x09 */
80 for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
81 int val;
82
83 val = i2c_smbus_read_byte_data(client, i);
84 if (unlikely(val < 0)) {
85 dev_dbg(dev,
Guenter Roeck69f8b742010-10-26 15:59:21 -070086 "Failed to read ADC value: error %d\n",
Guenter Roecke5f5c992010-06-25 11:59:54 -070087 val);
88 ret = ERR_PTR(val);
Frans Meulenbroeksaac9fe92012-01-02 12:03:20 +010089 data->valid = 0;
Guenter Roecke5f5c992010-06-25 11:59:54 -070090 goto abort;
91 }
92 data->regs[i] = val;
93 }
94 data->last_updated = jiffies;
95 data->valid = 1;
96 }
97abort:
98 mutex_unlock(&data->update_lock);
99 return ret;
100}
101
102/* Return the voltage from the given register in mV or mA */
103static int ltc4261_get_value(struct ltc4261_data *data, u8 reg)
104{
105 u32 val;
106
107 val = (data->regs[reg] << 2) + (data->regs[reg + 1] >> 6);
108
109 switch (reg) {
110 case LTC4261_ADIN_H:
111 case LTC4261_ADIN2_H:
112 /* 2.5mV resolution. Convert to mV. */
113 val = val * 25 / 10;
114 break;
115 case LTC4261_SENSE_H:
116 /*
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
121 * in mOhm.
122 */
123 val = val * 625 / 10;
124 break;
125 default:
126 /* If we get here, the developer messed up */
127 WARN_ON_ONCE(1);
128 val = 0;
129 break;
130 }
131
132 return val;
133}
134
135static ssize_t ltc4261_show_value(struct device *dev,
136 struct device_attribute *da, char *buf)
137{
138 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
139 struct ltc4261_data *data = ltc4261_update_device(dev);
140 int value;
141
142 if (IS_ERR(data))
143 return PTR_ERR(data);
144
145 value = ltc4261_get_value(data, attr->index);
146 return snprintf(buf, PAGE_SIZE, "%d\n", value);
147}
148
149static ssize_t ltc4261_show_bool(struct device *dev,
150 struct device_attribute *da, char *buf)
151{
152 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
153 struct i2c_client *client = to_i2c_client(dev);
154 struct ltc4261_data *data = ltc4261_update_device(dev);
155 u8 fault;
156
157 if (IS_ERR(data))
158 return PTR_ERR(data);
159
160 fault = data->regs[LTC4261_FAULT] & attr->index;
161 if (fault) /* Clear reported faults in chip register */
162 i2c_smbus_write_byte_data(client, LTC4261_FAULT, ~fault);
163
164 return snprintf(buf, PAGE_SIZE, "%d\n", fault ? 1 : 0);
165}
166
167/*
168 * These macros are used below in constructing device attribute objects
169 * for use with sysfs_create_group() to make a sysfs device file
170 * for each register.
171 */
172
173#define LTC4261_VALUE(name, ltc4261_cmd_idx) \
174 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
175 ltc4261_show_value, NULL, ltc4261_cmd_idx)
176
177#define LTC4261_BOOL(name, mask) \
178 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
179 ltc4261_show_bool, NULL, (mask))
180
181/*
182 * Input voltages.
183 */
184LTC4261_VALUE(in1_input, LTC4261_ADIN_H);
185LTC4261_VALUE(in2_input, LTC4261_ADIN2_H);
186
187/*
188 * Voltage alarms. The chip has only one set of voltage alarm status bits,
189 * triggered by input voltage alarms. In many designs, those alarms are
190 * associated with the ADIN2 sensor, due to the proximity of the ADIN2 pin
191 * to the OV pin. ADIN2 is, however, not available on all chip variants.
192 * To ensure that the alarm condition is reported to the user, report it
193 * with both voltage sensors.
194 */
195LTC4261_BOOL(in1_min_alarm, FAULT_UV);
196LTC4261_BOOL(in1_max_alarm, FAULT_OV);
197LTC4261_BOOL(in2_min_alarm, FAULT_UV);
198LTC4261_BOOL(in2_max_alarm, FAULT_OV);
199
200/* Currents (via sense resistor) */
201LTC4261_VALUE(curr1_input, LTC4261_SENSE_H);
202
203/* Overcurrent alarm */
204LTC4261_BOOL(curr1_max_alarm, FAULT_OC);
205
206static struct attribute *ltc4261_attributes[] = {
207 &sensor_dev_attr_in1_input.dev_attr.attr,
208 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
209 &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
210 &sensor_dev_attr_in2_input.dev_attr.attr,
211 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
212 &sensor_dev_attr_in2_max_alarm.dev_attr.attr,
213
214 &sensor_dev_attr_curr1_input.dev_attr.attr,
215 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
216
217 NULL,
218};
219
220static const struct attribute_group ltc4261_group = {
221 .attrs = ltc4261_attributes,
222};
223
224static int ltc4261_probe(struct i2c_client *client,
225 const struct i2c_device_id *id)
226{
227 struct i2c_adapter *adapter = client->adapter;
228 struct ltc4261_data *data;
229 int ret;
230
231 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
232 return -ENODEV;
233
234 if (i2c_smbus_read_byte_data(client, LTC4261_STATUS) < 0) {
Jean Delvare475200c2010-11-05 10:59:29 -0400235 dev_err(&client->dev, "Failed to read status register\n");
Guenter Roecke5f5c992010-06-25 11:59:54 -0700236 return -ENODEV;
237 }
238
Guenter Roeck36839282012-02-22 08:56:49 -0800239 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
240 if (!data)
241 return -ENOMEM;
Guenter Roecke5f5c992010-06-25 11:59:54 -0700242
243 i2c_set_clientdata(client, data);
244 mutex_init(&data->update_lock);
245
246 /* Clear faults */
247 i2c_smbus_write_byte_data(client, LTC4261_FAULT, 0x00);
248
249 /* Register sysfs hooks */
250 ret = sysfs_create_group(&client->dev.kobj, &ltc4261_group);
251 if (ret)
Guenter Roeck36839282012-02-22 08:56:49 -0800252 return ret;
Guenter Roecke5f5c992010-06-25 11:59:54 -0700253
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;
258 }
259
260 return 0;
261
262out_hwmon_device_register:
263 sysfs_remove_group(&client->dev.kobj, &ltc4261_group);
Guenter Roecke5f5c992010-06-25 11:59:54 -0700264 return ret;
265}
266
267static int ltc4261_remove(struct i2c_client *client)
268{
269 struct ltc4261_data *data = i2c_get_clientdata(client);
270
271 hwmon_device_unregister(data->hwmon_dev);
272 sysfs_remove_group(&client->dev.kobj, &ltc4261_group);
273
Guenter Roecke5f5c992010-06-25 11:59:54 -0700274 return 0;
275}
276
277static const struct i2c_device_id ltc4261_id[] = {
278 {"ltc4261", 0},
279 {}
280};
281
282MODULE_DEVICE_TABLE(i2c, ltc4261_id);
283
284/* This is the driver that will be inserted */
285static struct i2c_driver ltc4261_driver = {
286 .driver = {
287 .name = "ltc4261",
288 },
289 .probe = ltc4261_probe,
290 .remove = ltc4261_remove,
291 .id_table = ltc4261_id,
292};
293
Axel Linf0967ee2012-01-20 15:38:18 +0800294module_i2c_driver(ltc4261_driver);
Guenter Roecke5f5c992010-06-25 11:59:54 -0700295
Guenter Roeckbb9a80e2012-06-22 12:07:25 -0700296MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
Guenter Roecke5f5c992010-06-25 11:59:54 -0700297MODULE_DESCRIPTION("LTC4261 driver");
298MODULE_LICENSE("GPL");