Loading...
1/*
2 * 1-wire client/driver for the Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC
3 *
4 * Author: Renata Sayakhova <renata@oktetlabs.ru>
5 *
6 * Based on ds2780_battery drivers
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/param.h>
17#include <linux/pm.h>
18#include <linux/platform_device.h>
19#include <linux/power_supply.h>
20#include <linux/idr.h>
21
22#include "../w1/w1.h"
23#include "../w1/slaves/w1_ds2781.h"
24
25/* Current unit measurement in uA for a 1 milli-ohm sense resistor */
26#define DS2781_CURRENT_UNITS 1563
27/* Charge unit measurement in uAh for a 1 milli-ohm sense resistor */
28#define DS2781_CHARGE_UNITS 6250
29/* Number of bytes in user EEPROM space */
30#define DS2781_USER_EEPROM_SIZE (DS2781_EEPROM_BLOCK0_END - \
31 DS2781_EEPROM_BLOCK0_START + 1)
32/* Number of bytes in parameter EEPROM space */
33#define DS2781_PARAM_EEPROM_SIZE (DS2781_EEPROM_BLOCK1_END - \
34 DS2781_EEPROM_BLOCK1_START + 1)
35
36struct ds2781_device_info {
37 struct device *dev;
38 struct power_supply bat;
39 struct device *w1_dev;
40};
41
42enum current_types {
43 CURRENT_NOW,
44 CURRENT_AVG,
45};
46
47static const char model[] = "DS2781";
48static const char manufacturer[] = "Maxim/Dallas";
49
50static inline struct ds2781_device_info *
51to_ds2781_device_info(struct power_supply *psy)
52{
53 return container_of(psy, struct ds2781_device_info, bat);
54}
55
56static inline struct power_supply *to_power_supply(struct device *dev)
57{
58 return dev_get_drvdata(dev);
59}
60
61static inline int ds2781_battery_io(struct ds2781_device_info *dev_info,
62 char *buf, int addr, size_t count, int io)
63{
64 return w1_ds2781_io(dev_info->w1_dev, buf, addr, count, io);
65}
66
67static int w1_ds2781_read(struct ds2781_device_info *dev_info, char *buf,
68 int addr, size_t count)
69{
70 return ds2781_battery_io(dev_info, buf, addr, count, 0);
71}
72
73static inline int ds2781_read8(struct ds2781_device_info *dev_info, u8 *val,
74 int addr)
75{
76 return ds2781_battery_io(dev_info, val, addr, sizeof(u8), 0);
77}
78
79static int ds2781_read16(struct ds2781_device_info *dev_info, s16 *val,
80 int addr)
81{
82 int ret;
83 u8 raw[2];
84
85 ret = ds2781_battery_io(dev_info, raw, addr, sizeof(raw), 0);
86 if (ret < 0)
87 return ret;
88
89 *val = (raw[0] << 8) | raw[1];
90
91 return 0;
92}
93
94static inline int ds2781_read_block(struct ds2781_device_info *dev_info,
95 u8 *val, int addr, size_t count)
96{
97 return ds2781_battery_io(dev_info, val, addr, count, 0);
98}
99
100static inline int ds2781_write(struct ds2781_device_info *dev_info, u8 *val,
101 int addr, size_t count)
102{
103 return ds2781_battery_io(dev_info, val, addr, count, 1);
104}
105
106static inline int ds2781_store_eeprom(struct device *dev, int addr)
107{
108 return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_COPY_DATA);
109}
110
111static inline int ds2781_recall_eeprom(struct device *dev, int addr)
112{
113 return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_RECALL_DATA);
114}
115
116static int ds2781_save_eeprom(struct ds2781_device_info *dev_info, int reg)
117{
118 int ret;
119
120 ret = ds2781_store_eeprom(dev_info->w1_dev, reg);
121 if (ret < 0)
122 return ret;
123
124 ret = ds2781_recall_eeprom(dev_info->w1_dev, reg);
125 if (ret < 0)
126 return ret;
127
128 return 0;
129}
130
131/* Set sense resistor value in mhos */
132static int ds2781_set_sense_register(struct ds2781_device_info *dev_info,
133 u8 conductance)
134{
135 int ret;
136
137 ret = ds2781_write(dev_info, &conductance,
138 DS2781_RSNSP, sizeof(u8));
139 if (ret < 0)
140 return ret;
141
142 return ds2781_save_eeprom(dev_info, DS2781_RSNSP);
143}
144
145/* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
146static int ds2781_get_rsgain_register(struct ds2781_device_info *dev_info,
147 u16 *rsgain)
148{
149 return ds2781_read16(dev_info, rsgain, DS2781_RSGAIN_MSB);
150}
151
152/* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
153static int ds2781_set_rsgain_register(struct ds2781_device_info *dev_info,
154 u16 rsgain)
155{
156 int ret;
157 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
158
159 ret = ds2781_write(dev_info, raw,
160 DS2781_RSGAIN_MSB, sizeof(raw));
161 if (ret < 0)
162 return ret;
163
164 return ds2781_save_eeprom(dev_info, DS2781_RSGAIN_MSB);
165}
166
167static int ds2781_get_voltage(struct ds2781_device_info *dev_info,
168 int *voltage_uV)
169{
170 int ret;
171 char val[2];
172 int voltage_raw;
173
174 ret = w1_ds2781_read(dev_info, val, DS2781_VOLT_MSB, 2 * sizeof(u8));
175 if (ret < 0)
176 return ret;
177 /*
178 * The voltage value is located in 10 bits across the voltage MSB
179 * and LSB registers in two's compliment form
180 * Sign bit of the voltage value is in bit 7 of the voltage MSB register
181 * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
182 * voltage MSB register
183 * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
184 * voltage LSB register
185 */
186 voltage_raw = (val[0] << 3) |
187 (val[1] >> 5);
188
189 /* DS2781 reports voltage in units of 9.76mV, but the battery class
190 * reports in units of uV, so convert by multiplying by 9760. */
191 *voltage_uV = voltage_raw * 9760;
192
193 return 0;
194}
195
196static int ds2781_get_temperature(struct ds2781_device_info *dev_info,
197 int *temp)
198{
199 int ret;
200 char val[2];
201 int temp_raw;
202
203 ret = w1_ds2781_read(dev_info, val, DS2781_TEMP_MSB, 2 * sizeof(u8));
204 if (ret < 0)
205 return ret;
206 /*
207 * The temperature value is located in 10 bits across the temperature
208 * MSB and LSB registers in two's compliment form
209 * Sign bit of the temperature value is in bit 7 of the temperature
210 * MSB register
211 * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
212 * temperature MSB register
213 * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
214 * temperature LSB register
215 */
216 temp_raw = ((val[0]) << 3) |
217 (val[1] >> 5);
218 *temp = temp_raw + (temp_raw / 4);
219
220 return 0;
221}
222
223static int ds2781_get_current(struct ds2781_device_info *dev_info,
224 enum current_types type, int *current_uA)
225{
226 int ret, sense_res;
227 s16 current_raw;
228 u8 sense_res_raw, reg_msb;
229
230 /*
231 * The units of measurement for current are dependent on the value of
232 * the sense resistor.
233 */
234 ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
235 if (ret < 0)
236 return ret;
237
238 if (sense_res_raw == 0) {
239 dev_err(dev_info->dev, "sense resistor value is 0\n");
240 return -EINVAL;
241 }
242 sense_res = 1000 / sense_res_raw;
243
244 if (type == CURRENT_NOW)
245 reg_msb = DS2781_CURRENT_MSB;
246 else if (type == CURRENT_AVG)
247 reg_msb = DS2781_IAVG_MSB;
248 else
249 return -EINVAL;
250
251 /*
252 * The current value is located in 16 bits across the current MSB
253 * and LSB registers in two's compliment form
254 * Sign bit of the current value is in bit 7 of the current MSB register
255 * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
256 * MSB register
257 * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
258 * LSB register
259 */
260 ret = ds2781_read16(dev_info, ¤t_raw, reg_msb);
261 if (ret < 0)
262 return ret;
263
264 *current_uA = current_raw * (DS2781_CURRENT_UNITS / sense_res);
265 return 0;
266}
267
268static int ds2781_get_accumulated_current(struct ds2781_device_info *dev_info,
269 int *accumulated_current)
270{
271 int ret, sense_res;
272 s16 current_raw;
273 u8 sense_res_raw;
274
275 /*
276 * The units of measurement for accumulated current are dependent on
277 * the value of the sense resistor.
278 */
279 ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
280 if (ret < 0)
281 return ret;
282
283 if (sense_res_raw == 0) {
284 dev_err(dev_info->dev, "sense resistor value is 0\n");
285 return -EINVAL;
286 }
287 sense_res = 1000 / sense_res_raw;
288
289 /*
290 * The ACR value is located in 16 bits across the ACR MSB and
291 * LSB registers
292 * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
293 * MSB register
294 * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
295 * LSB register
296 */
297 ret = ds2781_read16(dev_info, ¤t_raw, DS2781_ACR_MSB);
298 if (ret < 0)
299 return ret;
300
301 *accumulated_current = current_raw * (DS2781_CHARGE_UNITS / sense_res);
302 return 0;
303}
304
305static int ds2781_get_capacity(struct ds2781_device_info *dev_info,
306 int *capacity)
307{
308 int ret;
309 u8 raw;
310
311 ret = ds2781_read8(dev_info, &raw, DS2781_RARC);
312 if (ret < 0)
313 return ret;
314
315 *capacity = raw;
316 return 0;
317}
318
319static int ds2781_get_status(struct ds2781_device_info *dev_info, int *status)
320{
321 int ret, current_uA, capacity;
322
323 ret = ds2781_get_current(dev_info, CURRENT_NOW, ¤t_uA);
324 if (ret < 0)
325 return ret;
326
327 ret = ds2781_get_capacity(dev_info, &capacity);
328 if (ret < 0)
329 return ret;
330
331 if (power_supply_am_i_supplied(&dev_info->bat)) {
332 if (capacity == 100)
333 *status = POWER_SUPPLY_STATUS_FULL;
334 else if (current_uA > 50000)
335 *status = POWER_SUPPLY_STATUS_CHARGING;
336 else
337 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
338 } else {
339 *status = POWER_SUPPLY_STATUS_DISCHARGING;
340 }
341 return 0;
342}
343
344static int ds2781_get_charge_now(struct ds2781_device_info *dev_info,
345 int *charge_now)
346{
347 int ret;
348 u16 charge_raw;
349
350 /*
351 * The RAAC value is located in 16 bits across the RAAC MSB and
352 * LSB registers
353 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
354 * MSB register
355 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
356 * LSB register
357 */
358 ret = ds2781_read16(dev_info, &charge_raw, DS2781_RAAC_MSB);
359 if (ret < 0)
360 return ret;
361
362 *charge_now = charge_raw * 1600;
363 return 0;
364}
365
366static int ds2781_get_control_register(struct ds2781_device_info *dev_info,
367 u8 *control_reg)
368{
369 return ds2781_read8(dev_info, control_reg, DS2781_CONTROL);
370}
371
372static int ds2781_set_control_register(struct ds2781_device_info *dev_info,
373 u8 control_reg)
374{
375 int ret;
376
377 ret = ds2781_write(dev_info, &control_reg,
378 DS2781_CONTROL, sizeof(u8));
379 if (ret < 0)
380 return ret;
381
382 return ds2781_save_eeprom(dev_info, DS2781_CONTROL);
383}
384
385static int ds2781_battery_get_property(struct power_supply *psy,
386 enum power_supply_property psp,
387 union power_supply_propval *val)
388{
389 int ret = 0;
390 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
391
392 switch (psp) {
393 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
394 ret = ds2781_get_voltage(dev_info, &val->intval);
395 break;
396
397 case POWER_SUPPLY_PROP_TEMP:
398 ret = ds2781_get_temperature(dev_info, &val->intval);
399 break;
400
401 case POWER_SUPPLY_PROP_MODEL_NAME:
402 val->strval = model;
403 break;
404
405 case POWER_SUPPLY_PROP_MANUFACTURER:
406 val->strval = manufacturer;
407 break;
408
409 case POWER_SUPPLY_PROP_CURRENT_NOW:
410 ret = ds2781_get_current(dev_info, CURRENT_NOW, &val->intval);
411 break;
412
413 case POWER_SUPPLY_PROP_CURRENT_AVG:
414 ret = ds2781_get_current(dev_info, CURRENT_AVG, &val->intval);
415 break;
416
417 case POWER_SUPPLY_PROP_STATUS:
418 ret = ds2781_get_status(dev_info, &val->intval);
419 break;
420
421 case POWER_SUPPLY_PROP_CAPACITY:
422 ret = ds2781_get_capacity(dev_info, &val->intval);
423 break;
424
425 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
426 ret = ds2781_get_accumulated_current(dev_info, &val->intval);
427 break;
428
429 case POWER_SUPPLY_PROP_CHARGE_NOW:
430 ret = ds2781_get_charge_now(dev_info, &val->intval);
431 break;
432
433 default:
434 ret = -EINVAL;
435 }
436
437 return ret;
438}
439
440static enum power_supply_property ds2781_battery_props[] = {
441 POWER_SUPPLY_PROP_STATUS,
442 POWER_SUPPLY_PROP_VOLTAGE_NOW,
443 POWER_SUPPLY_PROP_TEMP,
444 POWER_SUPPLY_PROP_MODEL_NAME,
445 POWER_SUPPLY_PROP_MANUFACTURER,
446 POWER_SUPPLY_PROP_CURRENT_NOW,
447 POWER_SUPPLY_PROP_CURRENT_AVG,
448 POWER_SUPPLY_PROP_CAPACITY,
449 POWER_SUPPLY_PROP_CHARGE_COUNTER,
450 POWER_SUPPLY_PROP_CHARGE_NOW,
451};
452
453static ssize_t ds2781_get_pmod_enabled(struct device *dev,
454 struct device_attribute *attr,
455 char *buf)
456{
457 int ret;
458 u8 control_reg;
459 struct power_supply *psy = to_power_supply(dev);
460 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
461
462 /* Get power mode */
463 ret = ds2781_get_control_register(dev_info, &control_reg);
464 if (ret < 0)
465 return ret;
466
467 return sprintf(buf, "%d\n",
468 !!(control_reg & DS2781_CONTROL_PMOD));
469}
470
471static ssize_t ds2781_set_pmod_enabled(struct device *dev,
472 struct device_attribute *attr,
473 const char *buf,
474 size_t count)
475{
476 int ret;
477 u8 control_reg, new_setting;
478 struct power_supply *psy = to_power_supply(dev);
479 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
480
481 /* Set power mode */
482 ret = ds2781_get_control_register(dev_info, &control_reg);
483 if (ret < 0)
484 return ret;
485
486 ret = kstrtou8(buf, 0, &new_setting);
487 if (ret < 0)
488 return ret;
489
490 if ((new_setting != 0) && (new_setting != 1)) {
491 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
492 return -EINVAL;
493 }
494
495 if (new_setting)
496 control_reg |= DS2781_CONTROL_PMOD;
497 else
498 control_reg &= ~DS2781_CONTROL_PMOD;
499
500 ret = ds2781_set_control_register(dev_info, control_reg);
501 if (ret < 0)
502 return ret;
503
504 return count;
505}
506
507static ssize_t ds2781_get_sense_resistor_value(struct device *dev,
508 struct device_attribute *attr,
509 char *buf)
510{
511 int ret;
512 u8 sense_resistor;
513 struct power_supply *psy = to_power_supply(dev);
514 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
515
516 ret = ds2781_read8(dev_info, &sense_resistor, DS2781_RSNSP);
517 if (ret < 0)
518 return ret;
519
520 ret = sprintf(buf, "%d\n", sense_resistor);
521 return ret;
522}
523
524static ssize_t ds2781_set_sense_resistor_value(struct device *dev,
525 struct device_attribute *attr,
526 const char *buf,
527 size_t count)
528{
529 int ret;
530 u8 new_setting;
531 struct power_supply *psy = to_power_supply(dev);
532 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
533
534 ret = kstrtou8(buf, 0, &new_setting);
535 if (ret < 0)
536 return ret;
537
538 ret = ds2781_set_sense_register(dev_info, new_setting);
539 if (ret < 0)
540 return ret;
541
542 return count;
543}
544
545static ssize_t ds2781_get_rsgain_setting(struct device *dev,
546 struct device_attribute *attr,
547 char *buf)
548{
549 int ret;
550 u16 rsgain;
551 struct power_supply *psy = to_power_supply(dev);
552 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
553
554 ret = ds2781_get_rsgain_register(dev_info, &rsgain);
555 if (ret < 0)
556 return ret;
557
558 return sprintf(buf, "%d\n", rsgain);
559}
560
561static ssize_t ds2781_set_rsgain_setting(struct device *dev,
562 struct device_attribute *attr,
563 const char *buf,
564 size_t count)
565{
566 int ret;
567 u16 new_setting;
568 struct power_supply *psy = to_power_supply(dev);
569 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
570
571 ret = kstrtou16(buf, 0, &new_setting);
572 if (ret < 0)
573 return ret;
574
575 /* Gain can only be from 0 to 1.999 in steps of .001 */
576 if (new_setting > 1999) {
577 dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
578 return -EINVAL;
579 }
580
581 ret = ds2781_set_rsgain_register(dev_info, new_setting);
582 if (ret < 0)
583 return ret;
584
585 return count;
586}
587
588static ssize_t ds2781_get_pio_pin(struct device *dev,
589 struct device_attribute *attr,
590 char *buf)
591{
592 int ret;
593 u8 sfr;
594 struct power_supply *psy = to_power_supply(dev);
595 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
596
597 ret = ds2781_read8(dev_info, &sfr, DS2781_SFR);
598 if (ret < 0)
599 return ret;
600
601 ret = sprintf(buf, "%d\n", sfr & DS2781_SFR_PIOSC);
602 return ret;
603}
604
605static ssize_t ds2781_set_pio_pin(struct device *dev,
606 struct device_attribute *attr,
607 const char *buf,
608 size_t count)
609{
610 int ret;
611 u8 new_setting;
612 struct power_supply *psy = to_power_supply(dev);
613 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
614
615 ret = kstrtou8(buf, 0, &new_setting);
616 if (ret < 0)
617 return ret;
618
619 if ((new_setting != 0) && (new_setting != 1)) {
620 dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
621 return -EINVAL;
622 }
623
624 ret = ds2781_write(dev_info, &new_setting,
625 DS2781_SFR, sizeof(u8));
626 if (ret < 0)
627 return ret;
628
629 return count;
630}
631
632static ssize_t ds2781_read_param_eeprom_bin(struct file *filp,
633 struct kobject *kobj,
634 struct bin_attribute *bin_attr,
635 char *buf, loff_t off, size_t count)
636{
637 struct device *dev = container_of(kobj, struct device, kobj);
638 struct power_supply *psy = to_power_supply(dev);
639 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
640
641 count = min_t(loff_t, count, DS2781_PARAM_EEPROM_SIZE - off);
642
643 return ds2781_read_block(dev_info, buf,
644 DS2781_EEPROM_BLOCK1_START + off, count);
645}
646
647static ssize_t ds2781_write_param_eeprom_bin(struct file *filp,
648 struct kobject *kobj,
649 struct bin_attribute *bin_attr,
650 char *buf, loff_t off, size_t count)
651{
652 struct device *dev = container_of(kobj, struct device, kobj);
653 struct power_supply *psy = to_power_supply(dev);
654 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
655 int ret;
656
657 count = min_t(loff_t, count, DS2781_PARAM_EEPROM_SIZE - off);
658
659 ret = ds2781_write(dev_info, buf,
660 DS2781_EEPROM_BLOCK1_START + off, count);
661 if (ret < 0)
662 return ret;
663
664 ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK1_START);
665 if (ret < 0)
666 return ret;
667
668 return count;
669}
670
671static struct bin_attribute ds2781_param_eeprom_bin_attr = {
672 .attr = {
673 .name = "param_eeprom",
674 .mode = S_IRUGO | S_IWUSR,
675 },
676 .size = DS2781_PARAM_EEPROM_SIZE,
677 .read = ds2781_read_param_eeprom_bin,
678 .write = ds2781_write_param_eeprom_bin,
679};
680
681static ssize_t ds2781_read_user_eeprom_bin(struct file *filp,
682 struct kobject *kobj,
683 struct bin_attribute *bin_attr,
684 char *buf, loff_t off, size_t count)
685{
686 struct device *dev = container_of(kobj, struct device, kobj);
687 struct power_supply *psy = to_power_supply(dev);
688 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
689
690 count = min_t(loff_t, count, DS2781_USER_EEPROM_SIZE - off);
691
692 return ds2781_read_block(dev_info, buf,
693 DS2781_EEPROM_BLOCK0_START + off, count);
694
695}
696
697static ssize_t ds2781_write_user_eeprom_bin(struct file *filp,
698 struct kobject *kobj,
699 struct bin_attribute *bin_attr,
700 char *buf, loff_t off, size_t count)
701{
702 struct device *dev = container_of(kobj, struct device, kobj);
703 struct power_supply *psy = to_power_supply(dev);
704 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
705 int ret;
706
707 count = min_t(loff_t, count, DS2781_USER_EEPROM_SIZE - off);
708
709 ret = ds2781_write(dev_info, buf,
710 DS2781_EEPROM_BLOCK0_START + off, count);
711 if (ret < 0)
712 return ret;
713
714 ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK0_START);
715 if (ret < 0)
716 return ret;
717
718 return count;
719}
720
721static struct bin_attribute ds2781_user_eeprom_bin_attr = {
722 .attr = {
723 .name = "user_eeprom",
724 .mode = S_IRUGO | S_IWUSR,
725 },
726 .size = DS2781_USER_EEPROM_SIZE,
727 .read = ds2781_read_user_eeprom_bin,
728 .write = ds2781_write_user_eeprom_bin,
729};
730
731static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2781_get_pmod_enabled,
732 ds2781_set_pmod_enabled);
733static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
734 ds2781_get_sense_resistor_value, ds2781_set_sense_resistor_value);
735static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2781_get_rsgain_setting,
736 ds2781_set_rsgain_setting);
737static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2781_get_pio_pin,
738 ds2781_set_pio_pin);
739
740
741static struct attribute *ds2781_attributes[] = {
742 &dev_attr_pmod_enabled.attr,
743 &dev_attr_sense_resistor_value.attr,
744 &dev_attr_rsgain_setting.attr,
745 &dev_attr_pio_pin.attr,
746 NULL
747};
748
749static const struct attribute_group ds2781_attr_group = {
750 .attrs = ds2781_attributes,
751};
752
753static int ds2781_battery_probe(struct platform_device *pdev)
754{
755 int ret = 0;
756 struct ds2781_device_info *dev_info;
757
758 dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
759 if (!dev_info)
760 return -ENOMEM;
761
762 platform_set_drvdata(pdev, dev_info);
763
764 dev_info->dev = &pdev->dev;
765 dev_info->w1_dev = pdev->dev.parent;
766 dev_info->bat.name = dev_name(&pdev->dev);
767 dev_info->bat.type = POWER_SUPPLY_TYPE_BATTERY;
768 dev_info->bat.properties = ds2781_battery_props;
769 dev_info->bat.num_properties = ARRAY_SIZE(ds2781_battery_props);
770 dev_info->bat.get_property = ds2781_battery_get_property;
771
772 ret = power_supply_register(&pdev->dev, &dev_info->bat);
773 if (ret) {
774 dev_err(dev_info->dev, "failed to register battery\n");
775 goto fail;
776 }
777
778 ret = sysfs_create_group(&dev_info->bat.dev->kobj, &ds2781_attr_group);
779 if (ret) {
780 dev_err(dev_info->dev, "failed to create sysfs group\n");
781 goto fail_unregister;
782 }
783
784 ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
785 &ds2781_param_eeprom_bin_attr);
786 if (ret) {
787 dev_err(dev_info->dev,
788 "failed to create param eeprom bin file");
789 goto fail_remove_group;
790 }
791
792 ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
793 &ds2781_user_eeprom_bin_attr);
794 if (ret) {
795 dev_err(dev_info->dev,
796 "failed to create user eeprom bin file");
797 goto fail_remove_bin_file;
798 }
799
800 return 0;
801
802fail_remove_bin_file:
803 sysfs_remove_bin_file(&dev_info->bat.dev->kobj,
804 &ds2781_param_eeprom_bin_attr);
805fail_remove_group:
806 sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2781_attr_group);
807fail_unregister:
808 power_supply_unregister(&dev_info->bat);
809fail:
810 return ret;
811}
812
813static int ds2781_battery_remove(struct platform_device *pdev)
814{
815 struct ds2781_device_info *dev_info = platform_get_drvdata(pdev);
816
817 /* remove attributes */
818 sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2781_attr_group);
819
820 power_supply_unregister(&dev_info->bat);
821
822 return 0;
823}
824
825static struct platform_driver ds2781_battery_driver = {
826 .driver = {
827 .name = "ds2781-battery",
828 },
829 .probe = ds2781_battery_probe,
830 .remove = ds2781_battery_remove,
831};
832module_platform_driver(ds2781_battery_driver);
833
834MODULE_LICENSE("GPL");
835MODULE_AUTHOR("Renata Sayakhova <renata@oktetlabs.ru>");
836MODULE_DESCRIPTION("Maxim/Dallas DS2781 Stand-Alone Fuel Gauage IC driver");
837MODULE_ALIAS("platform:ds2781-battery");
838
1/*
2 * 1-wire client/driver for the Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC
3 *
4 * Author: Renata Sayakhova <renata@oktetlabs.ru>
5 *
6 * Based on ds2780_battery drivers
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/param.h>
17#include <linux/pm.h>
18#include <linux/platform_device.h>
19#include <linux/power_supply.h>
20#include <linux/idr.h>
21
22#include "../w1/w1.h"
23#include "../w1/slaves/w1_ds2781.h"
24
25/* Current unit measurement in uA for a 1 milli-ohm sense resistor */
26#define DS2781_CURRENT_UNITS 1563
27/* Charge unit measurement in uAh for a 1 milli-ohm sense resistor */
28#define DS2781_CHARGE_UNITS 6250
29/* Number of bytes in user EEPROM space */
30#define DS2781_USER_EEPROM_SIZE (DS2781_EEPROM_BLOCK0_END - \
31 DS2781_EEPROM_BLOCK0_START + 1)
32/* Number of bytes in parameter EEPROM space */
33#define DS2781_PARAM_EEPROM_SIZE (DS2781_EEPROM_BLOCK1_END - \
34 DS2781_EEPROM_BLOCK1_START + 1)
35
36struct ds2781_device_info {
37 struct device *dev;
38 struct power_supply *bat;
39 struct power_supply_desc bat_desc;
40 struct device *w1_dev;
41};
42
43enum current_types {
44 CURRENT_NOW,
45 CURRENT_AVG,
46};
47
48static const char model[] = "DS2781";
49static const char manufacturer[] = "Maxim/Dallas";
50
51static inline struct ds2781_device_info *
52to_ds2781_device_info(struct power_supply *psy)
53{
54 return power_supply_get_drvdata(psy);
55}
56
57static inline struct power_supply *to_power_supply(struct device *dev)
58{
59 return dev_get_drvdata(dev);
60}
61
62static inline int ds2781_battery_io(struct ds2781_device_info *dev_info,
63 char *buf, int addr, size_t count, int io)
64{
65 return w1_ds2781_io(dev_info->w1_dev, buf, addr, count, io);
66}
67
68static int w1_ds2781_read(struct ds2781_device_info *dev_info, char *buf,
69 int addr, size_t count)
70{
71 return ds2781_battery_io(dev_info, buf, addr, count, 0);
72}
73
74static inline int ds2781_read8(struct ds2781_device_info *dev_info, u8 *val,
75 int addr)
76{
77 return ds2781_battery_io(dev_info, val, addr, sizeof(u8), 0);
78}
79
80static int ds2781_read16(struct ds2781_device_info *dev_info, s16 *val,
81 int addr)
82{
83 int ret;
84 u8 raw[2];
85
86 ret = ds2781_battery_io(dev_info, raw, addr, sizeof(raw), 0);
87 if (ret < 0)
88 return ret;
89
90 *val = (raw[0] << 8) | raw[1];
91
92 return 0;
93}
94
95static inline int ds2781_read_block(struct ds2781_device_info *dev_info,
96 u8 *val, int addr, size_t count)
97{
98 return ds2781_battery_io(dev_info, val, addr, count, 0);
99}
100
101static inline int ds2781_write(struct ds2781_device_info *dev_info, u8 *val,
102 int addr, size_t count)
103{
104 return ds2781_battery_io(dev_info, val, addr, count, 1);
105}
106
107static inline int ds2781_store_eeprom(struct device *dev, int addr)
108{
109 return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_COPY_DATA);
110}
111
112static inline int ds2781_recall_eeprom(struct device *dev, int addr)
113{
114 return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_RECALL_DATA);
115}
116
117static int ds2781_save_eeprom(struct ds2781_device_info *dev_info, int reg)
118{
119 int ret;
120
121 ret = ds2781_store_eeprom(dev_info->w1_dev, reg);
122 if (ret < 0)
123 return ret;
124
125 ret = ds2781_recall_eeprom(dev_info->w1_dev, reg);
126 if (ret < 0)
127 return ret;
128
129 return 0;
130}
131
132/* Set sense resistor value in mhos */
133static int ds2781_set_sense_register(struct ds2781_device_info *dev_info,
134 u8 conductance)
135{
136 int ret;
137
138 ret = ds2781_write(dev_info, &conductance,
139 DS2781_RSNSP, sizeof(u8));
140 if (ret < 0)
141 return ret;
142
143 return ds2781_save_eeprom(dev_info, DS2781_RSNSP);
144}
145
146/* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
147static int ds2781_get_rsgain_register(struct ds2781_device_info *dev_info,
148 u16 *rsgain)
149{
150 return ds2781_read16(dev_info, rsgain, DS2781_RSGAIN_MSB);
151}
152
153/* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
154static int ds2781_set_rsgain_register(struct ds2781_device_info *dev_info,
155 u16 rsgain)
156{
157 int ret;
158 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
159
160 ret = ds2781_write(dev_info, raw,
161 DS2781_RSGAIN_MSB, sizeof(raw));
162 if (ret < 0)
163 return ret;
164
165 return ds2781_save_eeprom(dev_info, DS2781_RSGAIN_MSB);
166}
167
168static int ds2781_get_voltage(struct ds2781_device_info *dev_info,
169 int *voltage_uV)
170{
171 int ret;
172 char val[2];
173 int voltage_raw;
174
175 ret = w1_ds2781_read(dev_info, val, DS2781_VOLT_MSB, 2 * sizeof(u8));
176 if (ret < 0)
177 return ret;
178 /*
179 * The voltage value is located in 10 bits across the voltage MSB
180 * and LSB registers in two's compliment form
181 * Sign bit of the voltage value is in bit 7 of the voltage MSB register
182 * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
183 * voltage MSB register
184 * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
185 * voltage LSB register
186 */
187 voltage_raw = (val[0] << 3) |
188 (val[1] >> 5);
189
190 /* DS2781 reports voltage in units of 9.76mV, but the battery class
191 * reports in units of uV, so convert by multiplying by 9760. */
192 *voltage_uV = voltage_raw * 9760;
193
194 return 0;
195}
196
197static int ds2781_get_temperature(struct ds2781_device_info *dev_info,
198 int *temp)
199{
200 int ret;
201 char val[2];
202 int temp_raw;
203
204 ret = w1_ds2781_read(dev_info, val, DS2781_TEMP_MSB, 2 * sizeof(u8));
205 if (ret < 0)
206 return ret;
207 /*
208 * The temperature value is located in 10 bits across the temperature
209 * MSB and LSB registers in two's compliment form
210 * Sign bit of the temperature value is in bit 7 of the temperature
211 * MSB register
212 * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
213 * temperature MSB register
214 * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
215 * temperature LSB register
216 */
217 temp_raw = ((val[0]) << 3) |
218 (val[1] >> 5);
219 *temp = temp_raw + (temp_raw / 4);
220
221 return 0;
222}
223
224static int ds2781_get_current(struct ds2781_device_info *dev_info,
225 enum current_types type, int *current_uA)
226{
227 int ret, sense_res;
228 s16 current_raw;
229 u8 sense_res_raw, reg_msb;
230
231 /*
232 * The units of measurement for current are dependent on the value of
233 * the sense resistor.
234 */
235 ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
236 if (ret < 0)
237 return ret;
238
239 if (sense_res_raw == 0) {
240 dev_err(dev_info->dev, "sense resistor value is 0\n");
241 return -EINVAL;
242 }
243 sense_res = 1000 / sense_res_raw;
244
245 if (type == CURRENT_NOW)
246 reg_msb = DS2781_CURRENT_MSB;
247 else if (type == CURRENT_AVG)
248 reg_msb = DS2781_IAVG_MSB;
249 else
250 return -EINVAL;
251
252 /*
253 * The current value is located in 16 bits across the current MSB
254 * and LSB registers in two's compliment form
255 * Sign bit of the current value is in bit 7 of the current MSB register
256 * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
257 * MSB register
258 * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
259 * LSB register
260 */
261 ret = ds2781_read16(dev_info, ¤t_raw, reg_msb);
262 if (ret < 0)
263 return ret;
264
265 *current_uA = current_raw * (DS2781_CURRENT_UNITS / sense_res);
266 return 0;
267}
268
269static int ds2781_get_accumulated_current(struct ds2781_device_info *dev_info,
270 int *accumulated_current)
271{
272 int ret, sense_res;
273 s16 current_raw;
274 u8 sense_res_raw;
275
276 /*
277 * The units of measurement for accumulated current are dependent on
278 * the value of the sense resistor.
279 */
280 ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
281 if (ret < 0)
282 return ret;
283
284 if (sense_res_raw == 0) {
285 dev_err(dev_info->dev, "sense resistor value is 0\n");
286 return -EINVAL;
287 }
288 sense_res = 1000 / sense_res_raw;
289
290 /*
291 * The ACR value is located in 16 bits across the ACR MSB and
292 * LSB registers
293 * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
294 * MSB register
295 * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
296 * LSB register
297 */
298 ret = ds2781_read16(dev_info, ¤t_raw, DS2781_ACR_MSB);
299 if (ret < 0)
300 return ret;
301
302 *accumulated_current = current_raw * (DS2781_CHARGE_UNITS / sense_res);
303 return 0;
304}
305
306static int ds2781_get_capacity(struct ds2781_device_info *dev_info,
307 int *capacity)
308{
309 int ret;
310 u8 raw;
311
312 ret = ds2781_read8(dev_info, &raw, DS2781_RARC);
313 if (ret < 0)
314 return ret;
315
316 *capacity = raw;
317 return 0;
318}
319
320static int ds2781_get_status(struct ds2781_device_info *dev_info, int *status)
321{
322 int ret, current_uA, capacity;
323
324 ret = ds2781_get_current(dev_info, CURRENT_NOW, ¤t_uA);
325 if (ret < 0)
326 return ret;
327
328 ret = ds2781_get_capacity(dev_info, &capacity);
329 if (ret < 0)
330 return ret;
331
332 if (power_supply_am_i_supplied(dev_info->bat)) {
333 if (capacity == 100)
334 *status = POWER_SUPPLY_STATUS_FULL;
335 else if (current_uA > 50000)
336 *status = POWER_SUPPLY_STATUS_CHARGING;
337 else
338 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
339 } else {
340 *status = POWER_SUPPLY_STATUS_DISCHARGING;
341 }
342 return 0;
343}
344
345static int ds2781_get_charge_now(struct ds2781_device_info *dev_info,
346 int *charge_now)
347{
348 int ret;
349 u16 charge_raw;
350
351 /*
352 * The RAAC value is located in 16 bits across the RAAC MSB and
353 * LSB registers
354 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
355 * MSB register
356 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
357 * LSB register
358 */
359 ret = ds2781_read16(dev_info, &charge_raw, DS2781_RAAC_MSB);
360 if (ret < 0)
361 return ret;
362
363 *charge_now = charge_raw * 1600;
364 return 0;
365}
366
367static int ds2781_get_control_register(struct ds2781_device_info *dev_info,
368 u8 *control_reg)
369{
370 return ds2781_read8(dev_info, control_reg, DS2781_CONTROL);
371}
372
373static int ds2781_set_control_register(struct ds2781_device_info *dev_info,
374 u8 control_reg)
375{
376 int ret;
377
378 ret = ds2781_write(dev_info, &control_reg,
379 DS2781_CONTROL, sizeof(u8));
380 if (ret < 0)
381 return ret;
382
383 return ds2781_save_eeprom(dev_info, DS2781_CONTROL);
384}
385
386static int ds2781_battery_get_property(struct power_supply *psy,
387 enum power_supply_property psp,
388 union power_supply_propval *val)
389{
390 int ret = 0;
391 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
392
393 switch (psp) {
394 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
395 ret = ds2781_get_voltage(dev_info, &val->intval);
396 break;
397
398 case POWER_SUPPLY_PROP_TEMP:
399 ret = ds2781_get_temperature(dev_info, &val->intval);
400 break;
401
402 case POWER_SUPPLY_PROP_MODEL_NAME:
403 val->strval = model;
404 break;
405
406 case POWER_SUPPLY_PROP_MANUFACTURER:
407 val->strval = manufacturer;
408 break;
409
410 case POWER_SUPPLY_PROP_CURRENT_NOW:
411 ret = ds2781_get_current(dev_info, CURRENT_NOW, &val->intval);
412 break;
413
414 case POWER_SUPPLY_PROP_CURRENT_AVG:
415 ret = ds2781_get_current(dev_info, CURRENT_AVG, &val->intval);
416 break;
417
418 case POWER_SUPPLY_PROP_STATUS:
419 ret = ds2781_get_status(dev_info, &val->intval);
420 break;
421
422 case POWER_SUPPLY_PROP_CAPACITY:
423 ret = ds2781_get_capacity(dev_info, &val->intval);
424 break;
425
426 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
427 ret = ds2781_get_accumulated_current(dev_info, &val->intval);
428 break;
429
430 case POWER_SUPPLY_PROP_CHARGE_NOW:
431 ret = ds2781_get_charge_now(dev_info, &val->intval);
432 break;
433
434 default:
435 ret = -EINVAL;
436 }
437
438 return ret;
439}
440
441static enum power_supply_property ds2781_battery_props[] = {
442 POWER_SUPPLY_PROP_STATUS,
443 POWER_SUPPLY_PROP_VOLTAGE_NOW,
444 POWER_SUPPLY_PROP_TEMP,
445 POWER_SUPPLY_PROP_MODEL_NAME,
446 POWER_SUPPLY_PROP_MANUFACTURER,
447 POWER_SUPPLY_PROP_CURRENT_NOW,
448 POWER_SUPPLY_PROP_CURRENT_AVG,
449 POWER_SUPPLY_PROP_CAPACITY,
450 POWER_SUPPLY_PROP_CHARGE_COUNTER,
451 POWER_SUPPLY_PROP_CHARGE_NOW,
452};
453
454static ssize_t ds2781_get_pmod_enabled(struct device *dev,
455 struct device_attribute *attr,
456 char *buf)
457{
458 int ret;
459 u8 control_reg;
460 struct power_supply *psy = to_power_supply(dev);
461 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
462
463 /* Get power mode */
464 ret = ds2781_get_control_register(dev_info, &control_reg);
465 if (ret < 0)
466 return ret;
467
468 return sprintf(buf, "%d\n",
469 !!(control_reg & DS2781_CONTROL_PMOD));
470}
471
472static ssize_t ds2781_set_pmod_enabled(struct device *dev,
473 struct device_attribute *attr,
474 const char *buf,
475 size_t count)
476{
477 int ret;
478 u8 control_reg, new_setting;
479 struct power_supply *psy = to_power_supply(dev);
480 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
481
482 /* Set power mode */
483 ret = ds2781_get_control_register(dev_info, &control_reg);
484 if (ret < 0)
485 return ret;
486
487 ret = kstrtou8(buf, 0, &new_setting);
488 if (ret < 0)
489 return ret;
490
491 if ((new_setting != 0) && (new_setting != 1)) {
492 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
493 return -EINVAL;
494 }
495
496 if (new_setting)
497 control_reg |= DS2781_CONTROL_PMOD;
498 else
499 control_reg &= ~DS2781_CONTROL_PMOD;
500
501 ret = ds2781_set_control_register(dev_info, control_reg);
502 if (ret < 0)
503 return ret;
504
505 return count;
506}
507
508static ssize_t ds2781_get_sense_resistor_value(struct device *dev,
509 struct device_attribute *attr,
510 char *buf)
511{
512 int ret;
513 u8 sense_resistor;
514 struct power_supply *psy = to_power_supply(dev);
515 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
516
517 ret = ds2781_read8(dev_info, &sense_resistor, DS2781_RSNSP);
518 if (ret < 0)
519 return ret;
520
521 ret = sprintf(buf, "%d\n", sense_resistor);
522 return ret;
523}
524
525static ssize_t ds2781_set_sense_resistor_value(struct device *dev,
526 struct device_attribute *attr,
527 const char *buf,
528 size_t count)
529{
530 int ret;
531 u8 new_setting;
532 struct power_supply *psy = to_power_supply(dev);
533 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
534
535 ret = kstrtou8(buf, 0, &new_setting);
536 if (ret < 0)
537 return ret;
538
539 ret = ds2781_set_sense_register(dev_info, new_setting);
540 if (ret < 0)
541 return ret;
542
543 return count;
544}
545
546static ssize_t ds2781_get_rsgain_setting(struct device *dev,
547 struct device_attribute *attr,
548 char *buf)
549{
550 int ret;
551 u16 rsgain;
552 struct power_supply *psy = to_power_supply(dev);
553 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
554
555 ret = ds2781_get_rsgain_register(dev_info, &rsgain);
556 if (ret < 0)
557 return ret;
558
559 return sprintf(buf, "%d\n", rsgain);
560}
561
562static ssize_t ds2781_set_rsgain_setting(struct device *dev,
563 struct device_attribute *attr,
564 const char *buf,
565 size_t count)
566{
567 int ret;
568 u16 new_setting;
569 struct power_supply *psy = to_power_supply(dev);
570 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
571
572 ret = kstrtou16(buf, 0, &new_setting);
573 if (ret < 0)
574 return ret;
575
576 /* Gain can only be from 0 to 1.999 in steps of .001 */
577 if (new_setting > 1999) {
578 dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
579 return -EINVAL;
580 }
581
582 ret = ds2781_set_rsgain_register(dev_info, new_setting);
583 if (ret < 0)
584 return ret;
585
586 return count;
587}
588
589static ssize_t ds2781_get_pio_pin(struct device *dev,
590 struct device_attribute *attr,
591 char *buf)
592{
593 int ret;
594 u8 sfr;
595 struct power_supply *psy = to_power_supply(dev);
596 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
597
598 ret = ds2781_read8(dev_info, &sfr, DS2781_SFR);
599 if (ret < 0)
600 return ret;
601
602 ret = sprintf(buf, "%d\n", sfr & DS2781_SFR_PIOSC);
603 return ret;
604}
605
606static ssize_t ds2781_set_pio_pin(struct device *dev,
607 struct device_attribute *attr,
608 const char *buf,
609 size_t count)
610{
611 int ret;
612 u8 new_setting;
613 struct power_supply *psy = to_power_supply(dev);
614 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
615
616 ret = kstrtou8(buf, 0, &new_setting);
617 if (ret < 0)
618 return ret;
619
620 if ((new_setting != 0) && (new_setting != 1)) {
621 dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
622 return -EINVAL;
623 }
624
625 ret = ds2781_write(dev_info, &new_setting,
626 DS2781_SFR, sizeof(u8));
627 if (ret < 0)
628 return ret;
629
630 return count;
631}
632
633static ssize_t ds2781_read_param_eeprom_bin(struct file *filp,
634 struct kobject *kobj,
635 struct bin_attribute *bin_attr,
636 char *buf, loff_t off, size_t count)
637{
638 struct device *dev = container_of(kobj, struct device, kobj);
639 struct power_supply *psy = to_power_supply(dev);
640 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
641
642 return ds2781_read_block(dev_info, buf,
643 DS2781_EEPROM_BLOCK1_START + off, count);
644}
645
646static ssize_t ds2781_write_param_eeprom_bin(struct file *filp,
647 struct kobject *kobj,
648 struct bin_attribute *bin_attr,
649 char *buf, loff_t off, size_t count)
650{
651 struct device *dev = container_of(kobj, struct device, kobj);
652 struct power_supply *psy = to_power_supply(dev);
653 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
654 int ret;
655
656 ret = ds2781_write(dev_info, buf,
657 DS2781_EEPROM_BLOCK1_START + off, count);
658 if (ret < 0)
659 return ret;
660
661 ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK1_START);
662 if (ret < 0)
663 return ret;
664
665 return count;
666}
667
668static struct bin_attribute ds2781_param_eeprom_bin_attr = {
669 .attr = {
670 .name = "param_eeprom",
671 .mode = S_IRUGO | S_IWUSR,
672 },
673 .size = DS2781_PARAM_EEPROM_SIZE,
674 .read = ds2781_read_param_eeprom_bin,
675 .write = ds2781_write_param_eeprom_bin,
676};
677
678static ssize_t ds2781_read_user_eeprom_bin(struct file *filp,
679 struct kobject *kobj,
680 struct bin_attribute *bin_attr,
681 char *buf, loff_t off, size_t count)
682{
683 struct device *dev = container_of(kobj, struct device, kobj);
684 struct power_supply *psy = to_power_supply(dev);
685 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
686
687 return ds2781_read_block(dev_info, buf,
688 DS2781_EEPROM_BLOCK0_START + off, count);
689
690}
691
692static ssize_t ds2781_write_user_eeprom_bin(struct file *filp,
693 struct kobject *kobj,
694 struct bin_attribute *bin_attr,
695 char *buf, loff_t off, size_t count)
696{
697 struct device *dev = container_of(kobj, struct device, kobj);
698 struct power_supply *psy = to_power_supply(dev);
699 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
700 int ret;
701
702 ret = ds2781_write(dev_info, buf,
703 DS2781_EEPROM_BLOCK0_START + off, count);
704 if (ret < 0)
705 return ret;
706
707 ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK0_START);
708 if (ret < 0)
709 return ret;
710
711 return count;
712}
713
714static struct bin_attribute ds2781_user_eeprom_bin_attr = {
715 .attr = {
716 .name = "user_eeprom",
717 .mode = S_IRUGO | S_IWUSR,
718 },
719 .size = DS2781_USER_EEPROM_SIZE,
720 .read = ds2781_read_user_eeprom_bin,
721 .write = ds2781_write_user_eeprom_bin,
722};
723
724static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2781_get_pmod_enabled,
725 ds2781_set_pmod_enabled);
726static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
727 ds2781_get_sense_resistor_value, ds2781_set_sense_resistor_value);
728static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2781_get_rsgain_setting,
729 ds2781_set_rsgain_setting);
730static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2781_get_pio_pin,
731 ds2781_set_pio_pin);
732
733
734static struct attribute *ds2781_attributes[] = {
735 &dev_attr_pmod_enabled.attr,
736 &dev_attr_sense_resistor_value.attr,
737 &dev_attr_rsgain_setting.attr,
738 &dev_attr_pio_pin.attr,
739 NULL
740};
741
742static const struct attribute_group ds2781_attr_group = {
743 .attrs = ds2781_attributes,
744};
745
746static int ds2781_battery_probe(struct platform_device *pdev)
747{
748 struct power_supply_config psy_cfg = {};
749 int ret = 0;
750 struct ds2781_device_info *dev_info;
751
752 dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
753 if (!dev_info)
754 return -ENOMEM;
755
756 platform_set_drvdata(pdev, dev_info);
757
758 dev_info->dev = &pdev->dev;
759 dev_info->w1_dev = pdev->dev.parent;
760 dev_info->bat_desc.name = dev_name(&pdev->dev);
761 dev_info->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
762 dev_info->bat_desc.properties = ds2781_battery_props;
763 dev_info->bat_desc.num_properties = ARRAY_SIZE(ds2781_battery_props);
764 dev_info->bat_desc.get_property = ds2781_battery_get_property;
765
766 psy_cfg.drv_data = dev_info;
767
768 dev_info->bat = power_supply_register(&pdev->dev, &dev_info->bat_desc,
769 &psy_cfg);
770 if (IS_ERR(dev_info->bat)) {
771 dev_err(dev_info->dev, "failed to register battery\n");
772 ret = PTR_ERR(dev_info->bat);
773 goto fail;
774 }
775
776 ret = sysfs_create_group(&dev_info->bat->dev.kobj, &ds2781_attr_group);
777 if (ret) {
778 dev_err(dev_info->dev, "failed to create sysfs group\n");
779 goto fail_unregister;
780 }
781
782 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj,
783 &ds2781_param_eeprom_bin_attr);
784 if (ret) {
785 dev_err(dev_info->dev,
786 "failed to create param eeprom bin file");
787 goto fail_remove_group;
788 }
789
790 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj,
791 &ds2781_user_eeprom_bin_attr);
792 if (ret) {
793 dev_err(dev_info->dev,
794 "failed to create user eeprom bin file");
795 goto fail_remove_bin_file;
796 }
797
798 return 0;
799
800fail_remove_bin_file:
801 sysfs_remove_bin_file(&dev_info->bat->dev.kobj,
802 &ds2781_param_eeprom_bin_attr);
803fail_remove_group:
804 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2781_attr_group);
805fail_unregister:
806 power_supply_unregister(dev_info->bat);
807fail:
808 return ret;
809}
810
811static int ds2781_battery_remove(struct platform_device *pdev)
812{
813 struct ds2781_device_info *dev_info = platform_get_drvdata(pdev);
814
815 /*
816 * Remove attributes before unregistering power supply
817 * because 'bat' will be freed on power_supply_unregister() call.
818 */
819 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2781_attr_group);
820
821 power_supply_unregister(dev_info->bat);
822
823 return 0;
824}
825
826static struct platform_driver ds2781_battery_driver = {
827 .driver = {
828 .name = "ds2781-battery",
829 },
830 .probe = ds2781_battery_probe,
831 .remove = ds2781_battery_remove,
832};
833module_platform_driver(ds2781_battery_driver);
834
835MODULE_LICENSE("GPL");
836MODULE_AUTHOR("Renata Sayakhova <renata@oktetlabs.ru>");
837MODULE_DESCRIPTION("Maxim/Dallas DS2781 Stand-Alone Fuel Gauage IC driver");
838MODULE_ALIAS("platform:ds2781-battery");
839