1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/err.h>
20#include <linux/slab.h>
21#include <linux/i2c.h>
22#include <linux/hwmon.h>
23#include <linux/hwmon-sysfs.h>
24#include <linux/jiffies.h>
25#include <linux/i2c/ltc4245.h>
26
27
28enum ltc4245_cmd {
29 LTC4245_STATUS = 0x00,
30 LTC4245_ALERT = 0x01,
31 LTC4245_CONTROL = 0x02,
32 LTC4245_ON = 0x03,
33 LTC4245_FAULT1 = 0x04,
34 LTC4245_FAULT2 = 0x05,
35 LTC4245_GPIO = 0x06,
36 LTC4245_ADCADR = 0x07,
37
38 LTC4245_12VIN = 0x10,
39 LTC4245_12VSENSE = 0x11,
40 LTC4245_12VOUT = 0x12,
41 LTC4245_5VIN = 0x13,
42 LTC4245_5VSENSE = 0x14,
43 LTC4245_5VOUT = 0x15,
44 LTC4245_3VIN = 0x16,
45 LTC4245_3VSENSE = 0x17,
46 LTC4245_3VOUT = 0x18,
47 LTC4245_VEEIN = 0x19,
48 LTC4245_VEESENSE = 0x1a,
49 LTC4245_VEEOUT = 0x1b,
50 LTC4245_GPIOADC = 0x1c,
51};
52
53struct ltc4245_data {
54 struct device *hwmon_dev;
55
56 struct mutex update_lock;
57 bool valid;
58 unsigned long last_updated;
59
60
61 u8 cregs[0x08];
62
63
64 u8 vregs[0x0d];
65
66
67 bool use_extra_gpios;
68 int gpios[3];
69};
70
71
72
73
74
75
76
77
78static void ltc4245_update_gpios(struct device *dev)
79{
80 struct i2c_client *client = to_i2c_client(dev);
81 struct ltc4245_data *data = i2c_get_clientdata(client);
82 u8 gpio_curr, gpio_next, gpio_reg;
83 int i;
84
85
86 if (!data->use_extra_gpios) {
87 data->gpios[0] = data->vregs[LTC4245_GPIOADC - 0x10];
88 return;
89 }
90
91
92
93
94
95 if (time_after(jiffies, data->last_updated + 5 * HZ)) {
96 dev_dbg(&client->dev, "Marking GPIOs invalid\n");
97 for (i = 0; i < ARRAY_SIZE(data->gpios); i++)
98 data->gpios[i] = -EAGAIN;
99 }
100
101
102
103
104
105
106
107
108 gpio_curr = (data->cregs[LTC4245_GPIO] & 0xc0) >> 6;
109 if (gpio_curr > 0)
110 gpio_curr -= 1;
111
112
113 data->gpios[gpio_curr] = data->vregs[LTC4245_GPIOADC - 0x10];
114
115
116 gpio_next = (gpio_curr + 1) % ARRAY_SIZE(data->gpios);
117
118
119
120
121
122 gpio_reg = (data->cregs[LTC4245_GPIO] & 0x3f) | ((gpio_next + 1) << 6);
123
124
125 i2c_smbus_write_byte_data(client, LTC4245_GPIO, gpio_reg);
126
127
128 data->cregs[LTC4245_GPIO] = gpio_reg;
129}
130
131static struct ltc4245_data *ltc4245_update_device(struct device *dev)
132{
133 struct i2c_client *client = to_i2c_client(dev);
134 struct ltc4245_data *data = i2c_get_clientdata(client);
135 s32 val;
136 int i;
137
138 mutex_lock(&data->update_lock);
139
140 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
141
142 dev_dbg(&client->dev, "Starting ltc4245 update\n");
143
144
145 for (i = 0; i < ARRAY_SIZE(data->cregs); i++) {
146 val = i2c_smbus_read_byte_data(client, i);
147 if (unlikely(val < 0))
148 data->cregs[i] = 0;
149 else
150 data->cregs[i] = val;
151 }
152
153
154 for (i = 0; i < ARRAY_SIZE(data->vregs); i++) {
155 val = i2c_smbus_read_byte_data(client, i+0x10);
156 if (unlikely(val < 0))
157 data->vregs[i] = 0;
158 else
159 data->vregs[i] = val;
160 }
161
162
163 ltc4245_update_gpios(dev);
164
165 data->last_updated = jiffies;
166 data->valid = 1;
167 }
168
169 mutex_unlock(&data->update_lock);
170
171 return data;
172}
173
174
175static int ltc4245_get_voltage(struct device *dev, u8 reg)
176{
177 struct ltc4245_data *data = ltc4245_update_device(dev);
178 const u8 regval = data->vregs[reg - 0x10];
179 u32 voltage = 0;
180
181 switch (reg) {
182 case LTC4245_12VIN:
183 case LTC4245_12VOUT:
184 voltage = regval * 55;
185 break;
186 case LTC4245_5VIN:
187 case LTC4245_5VOUT:
188 voltage = regval * 22;
189 break;
190 case LTC4245_3VIN:
191 case LTC4245_3VOUT:
192 voltage = regval * 15;
193 break;
194 case LTC4245_VEEIN:
195 case LTC4245_VEEOUT:
196 voltage = regval * -55;
197 break;
198 case LTC4245_GPIOADC:
199 voltage = regval * 10;
200 break;
201 default:
202
203 WARN_ON_ONCE(1);
204 break;
205 }
206
207 return voltage;
208}
209
210
211static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
212{
213 struct ltc4245_data *data = ltc4245_update_device(dev);
214 const u8 regval = data->vregs[reg - 0x10];
215 unsigned int voltage;
216 unsigned int curr;
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 switch (reg) {
234 case LTC4245_12VSENSE:
235 voltage = regval * 250;
236 curr = voltage / 50;
237 break;
238 case LTC4245_5VSENSE:
239 voltage = regval * 125;
240 curr = (voltage * 10) / 35;
241 break;
242 case LTC4245_3VSENSE:
243 voltage = regval * 125;
244 curr = (voltage * 10) / 25;
245 break;
246 case LTC4245_VEESENSE:
247 voltage = regval * 250;
248 curr = voltage / 100;
249 break;
250 default:
251
252 WARN_ON_ONCE(1);
253 curr = 0;
254 break;
255 }
256
257 return curr;
258}
259
260static ssize_t ltc4245_show_voltage(struct device *dev,
261 struct device_attribute *da,
262 char *buf)
263{
264 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
265 const int voltage = ltc4245_get_voltage(dev, attr->index);
266
267 return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
268}
269
270static ssize_t ltc4245_show_current(struct device *dev,
271 struct device_attribute *da,
272 char *buf)
273{
274 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
275 const unsigned int curr = ltc4245_get_current(dev, attr->index);
276
277 return snprintf(buf, PAGE_SIZE, "%u\n", curr);
278}
279
280static ssize_t ltc4245_show_power(struct device *dev,
281 struct device_attribute *da,
282 char *buf)
283{
284 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
285 const unsigned int curr = ltc4245_get_current(dev, attr->index);
286 const int output_voltage = ltc4245_get_voltage(dev, attr->index+1);
287
288
289 const unsigned int power = abs(output_voltage * curr);
290
291 return snprintf(buf, PAGE_SIZE, "%u\n", power);
292}
293
294static ssize_t ltc4245_show_alarm(struct device *dev,
295 struct device_attribute *da,
296 char *buf)
297{
298 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
299 struct ltc4245_data *data = ltc4245_update_device(dev);
300 const u8 reg = data->cregs[attr->index];
301 const u32 mask = attr->nr;
302
303 return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
304}
305
306static ssize_t ltc4245_show_gpio(struct device *dev,
307 struct device_attribute *da,
308 char *buf)
309{
310 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
311 struct ltc4245_data *data = ltc4245_update_device(dev);
312 int val = data->gpios[attr->index];
313
314
315 if (val < 0)
316 return val;
317
318
319 return snprintf(buf, PAGE_SIZE, "%u\n", val * 10);
320}
321
322
323
324
325
326
327
328#define LTC4245_VOLTAGE(name, ltc4245_cmd_idx) \
329 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
330 ltc4245_show_voltage, NULL, ltc4245_cmd_idx)
331
332#define LTC4245_CURRENT(name, ltc4245_cmd_idx) \
333 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
334 ltc4245_show_current, NULL, ltc4245_cmd_idx)
335
336#define LTC4245_POWER(name, ltc4245_cmd_idx) \
337 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
338 ltc4245_show_power, NULL, ltc4245_cmd_idx)
339
340#define LTC4245_ALARM(name, mask, reg) \
341 static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \
342 ltc4245_show_alarm, NULL, (mask), reg)
343
344#define LTC4245_GPIO_VOLTAGE(name, gpio_num) \
345 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
346 ltc4245_show_gpio, NULL, gpio_num)
347
348
349
350
351LTC4245_VOLTAGE(in1_input, LTC4245_12VIN);
352LTC4245_VOLTAGE(in2_input, LTC4245_5VIN);
353LTC4245_VOLTAGE(in3_input, LTC4245_3VIN);
354LTC4245_VOLTAGE(in4_input, LTC4245_VEEIN);
355
356
357LTC4245_ALARM(in1_min_alarm, (1 << 0), LTC4245_FAULT1);
358LTC4245_ALARM(in2_min_alarm, (1 << 1), LTC4245_FAULT1);
359LTC4245_ALARM(in3_min_alarm, (1 << 2), LTC4245_FAULT1);
360LTC4245_ALARM(in4_min_alarm, (1 << 3), LTC4245_FAULT1);
361
362
363LTC4245_CURRENT(curr1_input, LTC4245_12VSENSE);
364LTC4245_CURRENT(curr2_input, LTC4245_5VSENSE);
365LTC4245_CURRENT(curr3_input, LTC4245_3VSENSE);
366LTC4245_CURRENT(curr4_input, LTC4245_VEESENSE);
367
368
369LTC4245_ALARM(curr1_max_alarm, (1 << 4), LTC4245_FAULT1);
370LTC4245_ALARM(curr2_max_alarm, (1 << 5), LTC4245_FAULT1);
371LTC4245_ALARM(curr3_max_alarm, (1 << 6), LTC4245_FAULT1);
372LTC4245_ALARM(curr4_max_alarm, (1 << 7), LTC4245_FAULT1);
373
374
375LTC4245_VOLTAGE(in5_input, LTC4245_12VOUT);
376LTC4245_VOLTAGE(in6_input, LTC4245_5VOUT);
377LTC4245_VOLTAGE(in7_input, LTC4245_3VOUT);
378LTC4245_VOLTAGE(in8_input, LTC4245_VEEOUT);
379
380
381LTC4245_ALARM(in5_min_alarm, (1 << 0), LTC4245_FAULT2);
382LTC4245_ALARM(in6_min_alarm, (1 << 1), LTC4245_FAULT2);
383LTC4245_ALARM(in7_min_alarm, (1 << 2), LTC4245_FAULT2);
384LTC4245_ALARM(in8_min_alarm, (1 << 3), LTC4245_FAULT2);
385
386
387LTC4245_GPIO_VOLTAGE(in9_input, 0);
388LTC4245_GPIO_VOLTAGE(in10_input, 1);
389LTC4245_GPIO_VOLTAGE(in11_input, 2);
390
391
392LTC4245_POWER(power1_input, LTC4245_12VSENSE);
393LTC4245_POWER(power2_input, LTC4245_5VSENSE);
394LTC4245_POWER(power3_input, LTC4245_3VSENSE);
395LTC4245_POWER(power4_input, LTC4245_VEESENSE);
396
397
398
399
400
401static struct attribute *ltc4245_std_attributes[] = {
402 &sensor_dev_attr_in1_input.dev_attr.attr,
403 &sensor_dev_attr_in2_input.dev_attr.attr,
404 &sensor_dev_attr_in3_input.dev_attr.attr,
405 &sensor_dev_attr_in4_input.dev_attr.attr,
406
407 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
408 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
409 &sensor_dev_attr_in3_min_alarm.dev_attr.attr,
410 &sensor_dev_attr_in4_min_alarm.dev_attr.attr,
411
412 &sensor_dev_attr_curr1_input.dev_attr.attr,
413 &sensor_dev_attr_curr2_input.dev_attr.attr,
414 &sensor_dev_attr_curr3_input.dev_attr.attr,
415 &sensor_dev_attr_curr4_input.dev_attr.attr,
416
417 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
418 &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
419 &sensor_dev_attr_curr3_max_alarm.dev_attr.attr,
420 &sensor_dev_attr_curr4_max_alarm.dev_attr.attr,
421
422 &sensor_dev_attr_in5_input.dev_attr.attr,
423 &sensor_dev_attr_in6_input.dev_attr.attr,
424 &sensor_dev_attr_in7_input.dev_attr.attr,
425 &sensor_dev_attr_in8_input.dev_attr.attr,
426
427 &sensor_dev_attr_in5_min_alarm.dev_attr.attr,
428 &sensor_dev_attr_in6_min_alarm.dev_attr.attr,
429 &sensor_dev_attr_in7_min_alarm.dev_attr.attr,
430 &sensor_dev_attr_in8_min_alarm.dev_attr.attr,
431
432 &sensor_dev_attr_in9_input.dev_attr.attr,
433
434 &sensor_dev_attr_power1_input.dev_attr.attr,
435 &sensor_dev_attr_power2_input.dev_attr.attr,
436 &sensor_dev_attr_power3_input.dev_attr.attr,
437 &sensor_dev_attr_power4_input.dev_attr.attr,
438
439 NULL,
440};
441
442static struct attribute *ltc4245_gpio_attributes[] = {
443 &sensor_dev_attr_in10_input.dev_attr.attr,
444 &sensor_dev_attr_in11_input.dev_attr.attr,
445 NULL,
446};
447
448static const struct attribute_group ltc4245_std_group = {
449 .attrs = ltc4245_std_attributes,
450};
451
452static const struct attribute_group ltc4245_gpio_group = {
453 .attrs = ltc4245_gpio_attributes,
454};
455
456static int ltc4245_sysfs_create_groups(struct i2c_client *client)
457{
458 struct ltc4245_data *data = i2c_get_clientdata(client);
459 struct device *dev = &client->dev;
460 int ret;
461
462
463 ret = sysfs_create_group(&dev->kobj, <c4245_std_group);
464 if (ret) {
465 dev_err(dev, "unable to register standard attributes\n");
466 return ret;
467 }
468
469
470 if (data->use_extra_gpios) {
471 ret = sysfs_create_group(&dev->kobj, <c4245_gpio_group);
472 if (ret) {
473 dev_err(dev, "unable to register gpio attributes\n");
474 sysfs_remove_group(&dev->kobj, <c4245_std_group);
475 return ret;
476 }
477 }
478
479 return 0;
480}
481
482static void ltc4245_sysfs_remove_groups(struct i2c_client *client)
483{
484 struct ltc4245_data *data = i2c_get_clientdata(client);
485 struct device *dev = &client->dev;
486
487 if (data->use_extra_gpios)
488 sysfs_remove_group(&dev->kobj, <c4245_gpio_group);
489
490 sysfs_remove_group(&dev->kobj, <c4245_std_group);
491}
492
493static bool ltc4245_use_extra_gpios(struct i2c_client *client)
494{
495 struct ltc4245_platform_data *pdata = dev_get_platdata(&client->dev);
496#ifdef CONFIG_OF
497 struct device_node *np = client->dev.of_node;
498#endif
499
500
501 if (pdata)
502 return pdata->use_extra_gpios;
503
504#ifdef CONFIG_OF
505
506 if (of_find_property(np, "ltc4245,use-extra-gpios", NULL))
507 return true;
508#endif
509
510 return false;
511}
512
513static int ltc4245_probe(struct i2c_client *client,
514 const struct i2c_device_id *id)
515{
516 struct i2c_adapter *adapter = client->adapter;
517 struct ltc4245_data *data;
518 int ret;
519
520 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
521 return -ENODEV;
522
523 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
524 if (!data)
525 return -ENOMEM;
526
527 i2c_set_clientdata(client, data);
528 mutex_init(&data->update_lock);
529 data->use_extra_gpios = ltc4245_use_extra_gpios(client);
530
531
532 i2c_smbus_write_byte_data(client, LTC4245_FAULT1, 0x00);
533 i2c_smbus_write_byte_data(client, LTC4245_FAULT2, 0x00);
534
535
536 ret = ltc4245_sysfs_create_groups(client);
537 if (ret)
538 return ret;
539
540 data->hwmon_dev = hwmon_device_register(&client->dev);
541 if (IS_ERR(data->hwmon_dev)) {
542 ret = PTR_ERR(data->hwmon_dev);
543 goto out_hwmon_device_register;
544 }
545
546 return 0;
547
548out_hwmon_device_register:
549 ltc4245_sysfs_remove_groups(client);
550 return ret;
551}
552
553static int ltc4245_remove(struct i2c_client *client)
554{
555 struct ltc4245_data *data = i2c_get_clientdata(client);
556
557 hwmon_device_unregister(data->hwmon_dev);
558 ltc4245_sysfs_remove_groups(client);
559
560 return 0;
561}
562
563static const struct i2c_device_id ltc4245_id[] = {
564 { "ltc4245", 0 },
565 { }
566};
567MODULE_DEVICE_TABLE(i2c, ltc4245_id);
568
569
570static struct i2c_driver ltc4245_driver = {
571 .driver = {
572 .name = "ltc4245",
573 },
574 .probe = ltc4245_probe,
575 .remove = ltc4245_remove,
576 .id_table = ltc4245_id,
577};
578
579module_i2c_driver(ltc4245_driver);
580
581MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
582MODULE_DESCRIPTION("LTC4245 driver");
583MODULE_LICENSE("GPL");
584