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