1 // SPDX-License-Identifier: GPL-2.0-only
3 * 1-wire client/driver for the Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC
5 * Author: Renata Sayakhova <renata@oktetlabs.ru>
7 * Based on ds2780_battery drivers
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/param.h>
14 #include <linux/platform_device.h>
15 #include <linux/power_supply.h>
16 #include <linux/idr.h>
19 #include "../../w1/slaves/w1_ds2781.h"
21 /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
22 #define DS2781_CURRENT_UNITS 1563
23 /* Charge unit measurement in uAh for a 1 milli-ohm sense resistor */
24 #define DS2781_CHARGE_UNITS 6250
25 /* Number of bytes in user EEPROM space */
26 #define DS2781_USER_EEPROM_SIZE (DS2781_EEPROM_BLOCK0_END - \
27 DS2781_EEPROM_BLOCK0_START + 1)
28 /* Number of bytes in parameter EEPROM space */
29 #define DS2781_PARAM_EEPROM_SIZE (DS2781_EEPROM_BLOCK1_END - \
30 DS2781_EEPROM_BLOCK1_START + 1)
32 struct ds2781_device_info
{
34 struct power_supply
*bat
;
35 struct power_supply_desc bat_desc
;
36 struct device
*w1_dev
;
44 static const char model
[] = "DS2781";
45 static const char manufacturer
[] = "Maxim/Dallas";
47 static inline struct ds2781_device_info
*
48 to_ds2781_device_info(struct power_supply
*psy
)
50 return power_supply_get_drvdata(psy
);
53 static inline int ds2781_battery_io(struct ds2781_device_info
*dev_info
,
54 char *buf
, int addr
, size_t count
, int io
)
56 return w1_ds2781_io(dev_info
->w1_dev
, buf
, addr
, count
, io
);
59 static int w1_ds2781_read(struct ds2781_device_info
*dev_info
, char *buf
,
60 int addr
, size_t count
)
62 return ds2781_battery_io(dev_info
, buf
, addr
, count
, 0);
65 static inline int ds2781_read8(struct ds2781_device_info
*dev_info
, u8
*val
,
68 return ds2781_battery_io(dev_info
, val
, addr
, sizeof(u8
), 0);
71 static int ds2781_read16(struct ds2781_device_info
*dev_info
, s16
*val
,
77 ret
= ds2781_battery_io(dev_info
, raw
, addr
, sizeof(raw
), 0);
81 *val
= (raw
[0] << 8) | raw
[1];
86 static inline int ds2781_read_block(struct ds2781_device_info
*dev_info
,
87 u8
*val
, int addr
, size_t count
)
89 return ds2781_battery_io(dev_info
, val
, addr
, count
, 0);
92 static inline int ds2781_write(struct ds2781_device_info
*dev_info
, u8
*val
,
93 int addr
, size_t count
)
95 return ds2781_battery_io(dev_info
, val
, addr
, count
, 1);
98 static inline int ds2781_store_eeprom(struct device
*dev
, int addr
)
100 return w1_ds2781_eeprom_cmd(dev
, addr
, W1_DS2781_COPY_DATA
);
103 static inline int ds2781_recall_eeprom(struct device
*dev
, int addr
)
105 return w1_ds2781_eeprom_cmd(dev
, addr
, W1_DS2781_RECALL_DATA
);
108 static int ds2781_save_eeprom(struct ds2781_device_info
*dev_info
, int reg
)
112 ret
= ds2781_store_eeprom(dev_info
->w1_dev
, reg
);
116 ret
= ds2781_recall_eeprom(dev_info
->w1_dev
, reg
);
123 /* Set sense resistor value in mhos */
124 static int ds2781_set_sense_register(struct ds2781_device_info
*dev_info
,
129 ret
= ds2781_write(dev_info
, &conductance
,
130 DS2781_RSNSP
, sizeof(u8
));
134 return ds2781_save_eeprom(dev_info
, DS2781_RSNSP
);
137 /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
138 static int ds2781_get_rsgain_register(struct ds2781_device_info
*dev_info
,
141 return ds2781_read16(dev_info
, rsgain
, DS2781_RSGAIN_MSB
);
144 /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
145 static int ds2781_set_rsgain_register(struct ds2781_device_info
*dev_info
,
149 u8 raw
[] = {rsgain
>> 8, rsgain
& 0xFF};
151 ret
= ds2781_write(dev_info
, raw
,
152 DS2781_RSGAIN_MSB
, sizeof(raw
));
156 return ds2781_save_eeprom(dev_info
, DS2781_RSGAIN_MSB
);
159 static int ds2781_get_voltage(struct ds2781_device_info
*dev_info
,
166 ret
= w1_ds2781_read(dev_info
, val
, DS2781_VOLT_MSB
, 2 * sizeof(u8
));
170 * The voltage value is located in 10 bits across the voltage MSB
171 * and LSB registers in two's compliment form
172 * Sign bit of the voltage value is in bit 7 of the voltage MSB register
173 * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
174 * voltage MSB register
175 * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
176 * voltage LSB register
178 voltage_raw
= (val
[0] << 3) |
181 /* DS2781 reports voltage in units of 9.76mV, but the battery class
182 * reports in units of uV, so convert by multiplying by 9760. */
183 *voltage_uV
= voltage_raw
* 9760;
188 static int ds2781_get_temperature(struct ds2781_device_info
*dev_info
,
195 ret
= w1_ds2781_read(dev_info
, val
, DS2781_TEMP_MSB
, 2 * sizeof(u8
));
199 * The temperature value is located in 10 bits across the temperature
200 * MSB and LSB registers in two's compliment form
201 * Sign bit of the temperature value is in bit 7 of the temperature
203 * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
204 * temperature MSB register
205 * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
206 * temperature LSB register
208 temp_raw
= ((val
[0]) << 3) |
210 *temp
= temp_raw
+ (temp_raw
/ 4);
215 static int ds2781_get_current(struct ds2781_device_info
*dev_info
,
216 enum current_types type
, int *current_uA
)
220 u8 sense_res_raw
, reg_msb
;
223 * The units of measurement for current are dependent on the value of
224 * the sense resistor.
226 ret
= ds2781_read8(dev_info
, &sense_res_raw
, DS2781_RSNSP
);
230 if (sense_res_raw
== 0) {
231 dev_err(dev_info
->dev
, "sense resistor value is 0\n");
234 sense_res
= 1000 / sense_res_raw
;
236 if (type
== CURRENT_NOW
)
237 reg_msb
= DS2781_CURRENT_MSB
;
238 else if (type
== CURRENT_AVG
)
239 reg_msb
= DS2781_IAVG_MSB
;
244 * The current value is located in 16 bits across the current MSB
245 * and LSB registers in two's compliment form
246 * Sign bit of the current value is in bit 7 of the current MSB register
247 * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
249 * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
252 ret
= ds2781_read16(dev_info
, ¤t_raw
, reg_msb
);
256 *current_uA
= current_raw
* (DS2781_CURRENT_UNITS
/ sense_res
);
260 static int ds2781_get_accumulated_current(struct ds2781_device_info
*dev_info
,
261 int *accumulated_current
)
268 * The units of measurement for accumulated current are dependent on
269 * the value of the sense resistor.
271 ret
= ds2781_read8(dev_info
, &sense_res_raw
, DS2781_RSNSP
);
275 if (sense_res_raw
== 0) {
276 dev_err(dev_info
->dev
, "sense resistor value is 0\n");
279 sense_res
= 1000 / sense_res_raw
;
282 * The ACR value is located in 16 bits across the ACR MSB and
284 * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
286 * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
289 ret
= ds2781_read16(dev_info
, ¤t_raw
, DS2781_ACR_MSB
);
293 *accumulated_current
= current_raw
* (DS2781_CHARGE_UNITS
/ sense_res
);
297 static int ds2781_get_capacity(struct ds2781_device_info
*dev_info
,
303 ret
= ds2781_read8(dev_info
, &raw
, DS2781_RARC
);
311 static int ds2781_get_status(struct ds2781_device_info
*dev_info
, int *status
)
313 int ret
, current_uA
, capacity
;
315 ret
= ds2781_get_current(dev_info
, CURRENT_NOW
, ¤t_uA
);
319 ret
= ds2781_get_capacity(dev_info
, &capacity
);
323 if (power_supply_am_i_supplied(dev_info
->bat
)) {
325 *status
= POWER_SUPPLY_STATUS_FULL
;
326 else if (current_uA
> 50000)
327 *status
= POWER_SUPPLY_STATUS_CHARGING
;
329 *status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
331 *status
= POWER_SUPPLY_STATUS_DISCHARGING
;
336 static int ds2781_get_charge_now(struct ds2781_device_info
*dev_info
,
343 * The RAAC value is located in 16 bits across the RAAC MSB and
345 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
347 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
350 ret
= ds2781_read16(dev_info
, &charge_raw
, DS2781_RAAC_MSB
);
354 *charge_now
= charge_raw
* 1600;
358 static int ds2781_get_control_register(struct ds2781_device_info
*dev_info
,
361 return ds2781_read8(dev_info
, control_reg
, DS2781_CONTROL
);
364 static int ds2781_set_control_register(struct ds2781_device_info
*dev_info
,
369 ret
= ds2781_write(dev_info
, &control_reg
,
370 DS2781_CONTROL
, sizeof(u8
));
374 return ds2781_save_eeprom(dev_info
, DS2781_CONTROL
);
377 static int ds2781_battery_get_property(struct power_supply
*psy
,
378 enum power_supply_property psp
,
379 union power_supply_propval
*val
)
382 struct ds2781_device_info
*dev_info
= to_ds2781_device_info(psy
);
385 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
386 ret
= ds2781_get_voltage(dev_info
, &val
->intval
);
389 case POWER_SUPPLY_PROP_TEMP
:
390 ret
= ds2781_get_temperature(dev_info
, &val
->intval
);
393 case POWER_SUPPLY_PROP_MODEL_NAME
:
397 case POWER_SUPPLY_PROP_MANUFACTURER
:
398 val
->strval
= manufacturer
;
401 case POWER_SUPPLY_PROP_CURRENT_NOW
:
402 ret
= ds2781_get_current(dev_info
, CURRENT_NOW
, &val
->intval
);
405 case POWER_SUPPLY_PROP_CURRENT_AVG
:
406 ret
= ds2781_get_current(dev_info
, CURRENT_AVG
, &val
->intval
);
409 case POWER_SUPPLY_PROP_STATUS
:
410 ret
= ds2781_get_status(dev_info
, &val
->intval
);
413 case POWER_SUPPLY_PROP_CAPACITY
:
414 ret
= ds2781_get_capacity(dev_info
, &val
->intval
);
417 case POWER_SUPPLY_PROP_CHARGE_COUNTER
:
418 ret
= ds2781_get_accumulated_current(dev_info
, &val
->intval
);
421 case POWER_SUPPLY_PROP_CHARGE_NOW
:
422 ret
= ds2781_get_charge_now(dev_info
, &val
->intval
);
432 static enum power_supply_property ds2781_battery_props
[] = {
433 POWER_SUPPLY_PROP_STATUS
,
434 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
435 POWER_SUPPLY_PROP_TEMP
,
436 POWER_SUPPLY_PROP_MODEL_NAME
,
437 POWER_SUPPLY_PROP_MANUFACTURER
,
438 POWER_SUPPLY_PROP_CURRENT_NOW
,
439 POWER_SUPPLY_PROP_CURRENT_AVG
,
440 POWER_SUPPLY_PROP_CAPACITY
,
441 POWER_SUPPLY_PROP_CHARGE_COUNTER
,
442 POWER_SUPPLY_PROP_CHARGE_NOW
,
445 static ssize_t
ds2781_get_pmod_enabled(struct device
*dev
,
446 struct device_attribute
*attr
,
451 struct power_supply
*psy
= to_power_supply(dev
);
452 struct ds2781_device_info
*dev_info
= to_ds2781_device_info(psy
);
455 ret
= ds2781_get_control_register(dev_info
, &control_reg
);
459 return sprintf(buf
, "%d\n",
460 !!(control_reg
& DS2781_CONTROL_PMOD
));
463 static ssize_t
ds2781_set_pmod_enabled(struct device
*dev
,
464 struct device_attribute
*attr
,
469 u8 control_reg
, new_setting
;
470 struct power_supply
*psy
= to_power_supply(dev
);
471 struct ds2781_device_info
*dev_info
= to_ds2781_device_info(psy
);
474 ret
= ds2781_get_control_register(dev_info
, &control_reg
);
478 ret
= kstrtou8(buf
, 0, &new_setting
);
482 if ((new_setting
!= 0) && (new_setting
!= 1)) {
483 dev_err(dev_info
->dev
, "Invalid pmod setting (0 or 1)\n");
488 control_reg
|= DS2781_CONTROL_PMOD
;
490 control_reg
&= ~DS2781_CONTROL_PMOD
;
492 ret
= ds2781_set_control_register(dev_info
, control_reg
);
499 static ssize_t
ds2781_get_sense_resistor_value(struct device
*dev
,
500 struct device_attribute
*attr
,
505 struct power_supply
*psy
= to_power_supply(dev
);
506 struct ds2781_device_info
*dev_info
= to_ds2781_device_info(psy
);
508 ret
= ds2781_read8(dev_info
, &sense_resistor
, DS2781_RSNSP
);
512 ret
= sprintf(buf
, "%d\n", sense_resistor
);
516 static ssize_t
ds2781_set_sense_resistor_value(struct device
*dev
,
517 struct device_attribute
*attr
,
523 struct power_supply
*psy
= to_power_supply(dev
);
524 struct ds2781_device_info
*dev_info
= to_ds2781_device_info(psy
);
526 ret
= kstrtou8(buf
, 0, &new_setting
);
530 ret
= ds2781_set_sense_register(dev_info
, new_setting
);
537 static ssize_t
ds2781_get_rsgain_setting(struct device
*dev
,
538 struct device_attribute
*attr
,
543 struct power_supply
*psy
= to_power_supply(dev
);
544 struct ds2781_device_info
*dev_info
= to_ds2781_device_info(psy
);
546 ret
= ds2781_get_rsgain_register(dev_info
, &rsgain
);
550 return sprintf(buf
, "%d\n", rsgain
);
553 static ssize_t
ds2781_set_rsgain_setting(struct device
*dev
,
554 struct device_attribute
*attr
,
560 struct power_supply
*psy
= to_power_supply(dev
);
561 struct ds2781_device_info
*dev_info
= to_ds2781_device_info(psy
);
563 ret
= kstrtou16(buf
, 0, &new_setting
);
567 /* Gain can only be from 0 to 1.999 in steps of .001 */
568 if (new_setting
> 1999) {
569 dev_err(dev_info
->dev
, "Invalid rsgain setting (0 - 1999)\n");
573 ret
= ds2781_set_rsgain_register(dev_info
, new_setting
);
580 static ssize_t
ds2781_get_pio_pin(struct device
*dev
,
581 struct device_attribute
*attr
,
586 struct power_supply
*psy
= to_power_supply(dev
);
587 struct ds2781_device_info
*dev_info
= to_ds2781_device_info(psy
);
589 ret
= ds2781_read8(dev_info
, &sfr
, DS2781_SFR
);
593 ret
= sprintf(buf
, "%d\n", sfr
& DS2781_SFR_PIOSC
);
597 static ssize_t
ds2781_set_pio_pin(struct device
*dev
,
598 struct device_attribute
*attr
,
604 struct power_supply
*psy
= to_power_supply(dev
);
605 struct ds2781_device_info
*dev_info
= to_ds2781_device_info(psy
);
607 ret
= kstrtou8(buf
, 0, &new_setting
);
611 if ((new_setting
!= 0) && (new_setting
!= 1)) {
612 dev_err(dev_info
->dev
, "Invalid pio_pin setting (0 or 1)\n");
616 ret
= ds2781_write(dev_info
, &new_setting
,
617 DS2781_SFR
, sizeof(u8
));
624 static ssize_t
ds2781_read_param_eeprom_bin(struct file
*filp
,
625 struct kobject
*kobj
,
626 struct bin_attribute
*bin_attr
,
627 char *buf
, loff_t off
, size_t count
)
629 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
630 struct power_supply
*psy
= to_power_supply(dev
);
631 struct ds2781_device_info
*dev_info
= to_ds2781_device_info(psy
);
633 return ds2781_read_block(dev_info
, buf
,
634 DS2781_EEPROM_BLOCK1_START
+ off
, count
);
637 static ssize_t
ds2781_write_param_eeprom_bin(struct file
*filp
,
638 struct kobject
*kobj
,
639 struct bin_attribute
*bin_attr
,
640 char *buf
, loff_t off
, size_t count
)
642 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
643 struct power_supply
*psy
= to_power_supply(dev
);
644 struct ds2781_device_info
*dev_info
= to_ds2781_device_info(psy
);
647 ret
= ds2781_write(dev_info
, buf
,
648 DS2781_EEPROM_BLOCK1_START
+ off
, count
);
652 ret
= ds2781_save_eeprom(dev_info
, DS2781_EEPROM_BLOCK1_START
);
659 static struct bin_attribute ds2781_param_eeprom_bin_attr
= {
661 .name
= "param_eeprom",
662 .mode
= S_IRUGO
| S_IWUSR
,
664 .size
= DS2781_PARAM_EEPROM_SIZE
,
665 .read
= ds2781_read_param_eeprom_bin
,
666 .write
= ds2781_write_param_eeprom_bin
,
669 static ssize_t
ds2781_read_user_eeprom_bin(struct file
*filp
,
670 struct kobject
*kobj
,
671 struct bin_attribute
*bin_attr
,
672 char *buf
, loff_t off
, size_t count
)
674 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
675 struct power_supply
*psy
= to_power_supply(dev
);
676 struct ds2781_device_info
*dev_info
= to_ds2781_device_info(psy
);
678 return ds2781_read_block(dev_info
, buf
,
679 DS2781_EEPROM_BLOCK0_START
+ off
, count
);
683 static ssize_t
ds2781_write_user_eeprom_bin(struct file
*filp
,
684 struct kobject
*kobj
,
685 struct bin_attribute
*bin_attr
,
686 char *buf
, loff_t off
, size_t count
)
688 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
689 struct power_supply
*psy
= to_power_supply(dev
);
690 struct ds2781_device_info
*dev_info
= to_ds2781_device_info(psy
);
693 ret
= ds2781_write(dev_info
, buf
,
694 DS2781_EEPROM_BLOCK0_START
+ off
, count
);
698 ret
= ds2781_save_eeprom(dev_info
, DS2781_EEPROM_BLOCK0_START
);
705 static struct bin_attribute ds2781_user_eeprom_bin_attr
= {
707 .name
= "user_eeprom",
708 .mode
= S_IRUGO
| S_IWUSR
,
710 .size
= DS2781_USER_EEPROM_SIZE
,
711 .read
= ds2781_read_user_eeprom_bin
,
712 .write
= ds2781_write_user_eeprom_bin
,
715 static DEVICE_ATTR(pmod_enabled
, S_IRUGO
| S_IWUSR
, ds2781_get_pmod_enabled
,
716 ds2781_set_pmod_enabled
);
717 static DEVICE_ATTR(sense_resistor_value
, S_IRUGO
| S_IWUSR
,
718 ds2781_get_sense_resistor_value
, ds2781_set_sense_resistor_value
);
719 static DEVICE_ATTR(rsgain_setting
, S_IRUGO
| S_IWUSR
, ds2781_get_rsgain_setting
,
720 ds2781_set_rsgain_setting
);
721 static DEVICE_ATTR(pio_pin
, S_IRUGO
| S_IWUSR
, ds2781_get_pio_pin
,
724 static struct attribute
*ds2781_sysfs_attrs
[] = {
725 &dev_attr_pmod_enabled
.attr
,
726 &dev_attr_sense_resistor_value
.attr
,
727 &dev_attr_rsgain_setting
.attr
,
728 &dev_attr_pio_pin
.attr
,
732 static struct bin_attribute
*ds2781_sysfs_bin_attrs
[] = {
733 &ds2781_param_eeprom_bin_attr
,
734 &ds2781_user_eeprom_bin_attr
,
738 static const struct attribute_group ds2781_sysfs_group
= {
739 .attrs
= ds2781_sysfs_attrs
,
740 .bin_attrs
= ds2781_sysfs_bin_attrs
,
744 static const struct attribute_group
*ds2781_sysfs_groups
[] = {
749 static int ds2781_battery_probe(struct platform_device
*pdev
)
751 struct power_supply_config psy_cfg
= {};
752 struct ds2781_device_info
*dev_info
;
754 dev_info
= devm_kzalloc(&pdev
->dev
, sizeof(*dev_info
), GFP_KERNEL
);
758 platform_set_drvdata(pdev
, dev_info
);
760 dev_info
->dev
= &pdev
->dev
;
761 dev_info
->w1_dev
= pdev
->dev
.parent
;
762 dev_info
->bat_desc
.name
= dev_name(&pdev
->dev
);
763 dev_info
->bat_desc
.type
= POWER_SUPPLY_TYPE_BATTERY
;
764 dev_info
->bat_desc
.properties
= ds2781_battery_props
;
765 dev_info
->bat_desc
.num_properties
= ARRAY_SIZE(ds2781_battery_props
);
766 dev_info
->bat_desc
.get_property
= ds2781_battery_get_property
;
768 psy_cfg
.drv_data
= dev_info
;
769 psy_cfg
.attr_grp
= ds2781_sysfs_groups
;
771 dev_info
->bat
= devm_power_supply_register(&pdev
->dev
,
774 if (IS_ERR(dev_info
->bat
)) {
775 dev_err(dev_info
->dev
, "failed to register battery\n");
776 return PTR_ERR(dev_info
->bat
);
782 static struct platform_driver ds2781_battery_driver
= {
784 .name
= "ds2781-battery",
786 .probe
= ds2781_battery_probe
,
788 module_platform_driver(ds2781_battery_driver
);
790 MODULE_LICENSE("GPL");
791 MODULE_AUTHOR("Renata Sayakhova <renata@oktetlabs.ru>");
792 MODULE_DESCRIPTION("Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC driver");
793 MODULE_ALIAS("platform:ds2781-battery");