1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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#include <linux/jiffies.h>
37
38
39#define LTC4261_STATUS 0x00
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
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;
63
64
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
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,
86 "Failed to read ADC value: error %d\n",
87 val);
88 ret = ERR_PTR(val);
89 data->valid = 0;
90 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
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
113 val = val * 25 / 10;
114 break;
115 case LTC4261_SENSE_H:
116
117
118
119
120
121
122
123 val = val * 625 / 10;
124 break;
125 default:
126
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)
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
169
170static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4261_show_value, NULL,
171 LTC4261_ADIN_H);
172static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4261_show_value, NULL,
173 LTC4261_ADIN2_H);
174
175
176
177
178
179
180
181
182
183static SENSOR_DEVICE_ATTR(in1_min_alarm, S_IRUGO, ltc4261_show_bool, NULL,
184 FAULT_UV);
185static SENSOR_DEVICE_ATTR(in1_max_alarm, S_IRUGO, ltc4261_show_bool, NULL,
186 FAULT_OV);
187static SENSOR_DEVICE_ATTR(in2_min_alarm, S_IRUGO, ltc4261_show_bool, NULL,
188 FAULT_UV);
189static SENSOR_DEVICE_ATTR(in2_max_alarm, S_IRUGO, ltc4261_show_bool, NULL,
190 FAULT_OV);
191
192
193static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4261_show_value, NULL,
194 LTC4261_SENSE_H);
195
196
197static SENSOR_DEVICE_ATTR(curr1_max_alarm, S_IRUGO, ltc4261_show_bool, NULL,
198 FAULT_OC);
199
200static struct attribute *ltc4261_attributes[] = {
201 &sensor_dev_attr_in1_input.dev_attr.attr,
202 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
203 &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
204 &sensor_dev_attr_in2_input.dev_attr.attr,
205 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
206 &sensor_dev_attr_in2_max_alarm.dev_attr.attr,
207
208 &sensor_dev_attr_curr1_input.dev_attr.attr,
209 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
210
211 NULL,
212};
213
214static const struct attribute_group ltc4261_group = {
215 .attrs = ltc4261_attributes,
216};
217
218static int ltc4261_probe(struct i2c_client *client,
219 const struct i2c_device_id *id)
220{
221 struct i2c_adapter *adapter = client->adapter;
222 struct ltc4261_data *data;
223 int ret;
224
225 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
226 return -ENODEV;
227
228 if (i2c_smbus_read_byte_data(client, LTC4261_STATUS) < 0) {
229 dev_err(&client->dev, "Failed to read status register\n");
230 return -ENODEV;
231 }
232
233 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
234 if (!data)
235 return -ENOMEM;
236
237 i2c_set_clientdata(client, data);
238 mutex_init(&data->update_lock);
239
240
241 i2c_smbus_write_byte_data(client, LTC4261_FAULT, 0x00);
242
243
244 ret = sysfs_create_group(&client->dev.kobj, <c4261_group);
245 if (ret)
246 return ret;
247
248 data->hwmon_dev = hwmon_device_register(&client->dev);
249 if (IS_ERR(data->hwmon_dev)) {
250 ret = PTR_ERR(data->hwmon_dev);
251 goto out_hwmon_device_register;
252 }
253
254 return 0;
255
256out_hwmon_device_register:
257 sysfs_remove_group(&client->dev.kobj, <c4261_group);
258 return ret;
259}
260
261static int ltc4261_remove(struct i2c_client *client)
262{
263 struct ltc4261_data *data = i2c_get_clientdata(client);
264
265 hwmon_device_unregister(data->hwmon_dev);
266 sysfs_remove_group(&client->dev.kobj, <c4261_group);
267
268 return 0;
269}
270
271static const struct i2c_device_id ltc4261_id[] = {
272 {"ltc4261", 0},
273 {}
274};
275
276MODULE_DEVICE_TABLE(i2c, ltc4261_id);
277
278
279static struct i2c_driver ltc4261_driver = {
280 .driver = {
281 .name = "ltc4261",
282 },
283 .probe = ltc4261_probe,
284 .remove = ltc4261_remove,
285 .id_table = ltc4261_id,
286};
287
288module_i2c_driver(ltc4261_driver);
289
290MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
291MODULE_DESCRIPTION("LTC4261 driver");
292MODULE_LICENSE("GPL");
293