2 * 1-wire client/driver for the Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC
4 * Author: Renata Sayakhova <renata@oktetlabs.ru>
6 * Based on ds2780_battery drivers
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.
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/param.h>
18 #include <linux/platform_device.h>
19 #include <linux/power_supply.h>
20 #include <linux/idr.h>
23 #include "../w1/slaves/w1_ds2781.h"
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)
36 struct ds2781_device_info {
38 struct power_supply bat;
39 struct device *w1_dev;
47 static const char model[] = "DS2781";
48 static const char manufacturer[] = "Maxim/Dallas";
50 static inline struct ds2781_device_info *
51 to_ds2781_device_info(struct power_supply *psy)
53 return container_of(psy, struct ds2781_device_info, bat);
56 static inline struct power_supply *to_power_supply(struct device *dev)
58 return dev_get_drvdata(dev);
61 static inline int ds2781_battery_io(struct ds2781_device_info *dev_info,
62 char *buf, int addr, size_t count, int io)
64 return w1_ds2781_io(dev_info->w1_dev, buf, addr, count, io);
67 static int w1_ds2781_read(struct ds2781_device_info *dev_info, char *buf,
68 int addr, size_t count)
70 return ds2781_battery_io(dev_info, buf, addr, count, 0);
73 static inline int ds2781_read8(struct ds2781_device_info *dev_info, u8 *val,
76 return ds2781_battery_io(dev_info, val, addr, sizeof(u8), 0);
79 static int ds2781_read16(struct ds2781_device_info *dev_info, s16 *val,
85 ret = ds2781_battery_io(dev_info, raw, addr, sizeof(raw), 0);
89 *val = (raw[0] << 8) | raw[1];
94 static inline int ds2781_read_block(struct ds2781_device_info *dev_info,
95 u8 *val, int addr, size_t count)
97 return ds2781_battery_io(dev_info, val, addr, count, 0);
100 static inline int ds2781_write(struct ds2781_device_info *dev_info, u8 *val,
101 int addr, size_t count)
103 return ds2781_battery_io(dev_info, val, addr, count, 1);
106 static inline int ds2781_store_eeprom(struct device *dev, int addr)
108 return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_COPY_DATA);
111 static inline int ds2781_recall_eeprom(struct device *dev, int addr)
113 return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_RECALL_DATA);
116 static int ds2781_save_eeprom(struct ds2781_device_info *dev_info, int reg)
120 ret = ds2781_store_eeprom(dev_info->w1_dev, reg);
124 ret = ds2781_recall_eeprom(dev_info->w1_dev, reg);
131 /* Set sense resistor value in mhos */
132 static int ds2781_set_sense_register(struct ds2781_device_info *dev_info,
137 ret = ds2781_write(dev_info, &conductance,
138 DS2781_RSNSP, sizeof(u8));
142 return ds2781_save_eeprom(dev_info, DS2781_RSNSP);
145 /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
146 static int ds2781_get_rsgain_register(struct ds2781_device_info *dev_info,
149 return ds2781_read16(dev_info, rsgain, DS2781_RSGAIN_MSB);
152 /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
153 static int ds2781_set_rsgain_register(struct ds2781_device_info *dev_info,
157 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
159 ret = ds2781_write(dev_info, raw,
160 DS2781_RSGAIN_MSB, sizeof(raw));
164 return ds2781_save_eeprom(dev_info, DS2781_RSGAIN_MSB);
167 static int ds2781_get_voltage(struct ds2781_device_info *dev_info,
174 ret = w1_ds2781_read(dev_info, val, DS2781_VOLT_MSB, 2 * sizeof(u8));
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
186 voltage_raw = (val[0] << 3) |
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;
196 static int ds2781_get_temperature(struct ds2781_device_info *dev_info,
203 ret = w1_ds2781_read(dev_info, val, DS2781_TEMP_MSB, 2 * sizeof(u8));
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
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
216 temp_raw = ((val[0]) << 3) |
218 *temp = temp_raw + (temp_raw / 4);
223 static int ds2781_get_current(struct ds2781_device_info *dev_info,
224 enum current_types type, int *current_uA)
228 u8 sense_res_raw, reg_msb;
231 * The units of measurement for current are dependent on the value of
232 * the sense resistor.
234 ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
238 if (sense_res_raw == 0) {
239 dev_err(dev_info->dev, "sense resistor value is 0\n");
242 sense_res = 1000 / sense_res_raw;
244 if (type == CURRENT_NOW)
245 reg_msb = DS2781_CURRENT_MSB;
246 else if (type == CURRENT_AVG)
247 reg_msb = DS2781_IAVG_MSB;
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
257 * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
260 ret = ds2781_read16(dev_info, ¤t_raw, reg_msb);
264 *current_uA = current_raw * (DS2781_CURRENT_UNITS / sense_res);
268 static int ds2781_get_accumulated_current(struct ds2781_device_info *dev_info,
269 int *accumulated_current)
276 * The units of measurement for accumulated current are dependent on
277 * the value of the sense resistor.
279 ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
283 if (sense_res_raw == 0) {
284 dev_err(dev_info->dev, "sense resistor value is 0\n");
287 sense_res = 1000 / sense_res_raw;
290 * The ACR value is located in 16 bits across the ACR MSB and
292 * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
294 * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
297 ret = ds2781_read16(dev_info, ¤t_raw, DS2781_ACR_MSB);
301 *accumulated_current = current_raw * (DS2781_CHARGE_UNITS / sense_res);
305 static int ds2781_get_capacity(struct ds2781_device_info *dev_info,
311 ret = ds2781_read8(dev_info, &raw, DS2781_RARC);
319 static int ds2781_get_status(struct ds2781_device_info *dev_info, int *status)
321 int ret, current_uA, capacity;
323 ret = ds2781_get_current(dev_info, CURRENT_NOW, ¤t_uA);
327 ret = ds2781_get_capacity(dev_info, &capacity);
331 if (power_supply_am_i_supplied(&dev_info->bat)) {
333 *status = POWER_SUPPLY_STATUS_FULL;
334 else if (current_uA > 50000)
335 *status = POWER_SUPPLY_STATUS_CHARGING;
337 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
339 *status = POWER_SUPPLY_STATUS_DISCHARGING;
344 static int ds2781_get_charge_now(struct ds2781_device_info *dev_info,
351 * The RAAC value is located in 16 bits across the RAAC MSB and
353 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
355 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
358 ret = ds2781_read16(dev_info, &charge_raw, DS2781_RAAC_MSB);
362 *charge_now = charge_raw * 1600;
366 static int ds2781_get_control_register(struct ds2781_device_info *dev_info,
369 return ds2781_read8(dev_info, control_reg, DS2781_CONTROL);
372 static int ds2781_set_control_register(struct ds2781_device_info *dev_info,
377 ret = ds2781_write(dev_info, &control_reg,
378 DS2781_CONTROL, sizeof(u8));
382 return ds2781_save_eeprom(dev_info, DS2781_CONTROL);
385 static int ds2781_battery_get_property(struct power_supply *psy,
386 enum power_supply_property psp,
387 union power_supply_propval *val)
390 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
393 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
394 ret = ds2781_get_voltage(dev_info, &val->intval);
397 case POWER_SUPPLY_PROP_TEMP:
398 ret = ds2781_get_temperature(dev_info, &val->intval);
401 case POWER_SUPPLY_PROP_MODEL_NAME:
405 case POWER_SUPPLY_PROP_MANUFACTURER:
406 val->strval = manufacturer;
409 case POWER_SUPPLY_PROP_CURRENT_NOW:
410 ret = ds2781_get_current(dev_info, CURRENT_NOW, &val->intval);
413 case POWER_SUPPLY_PROP_CURRENT_AVG:
414 ret = ds2781_get_current(dev_info, CURRENT_AVG, &val->intval);
417 case POWER_SUPPLY_PROP_STATUS:
418 ret = ds2781_get_status(dev_info, &val->intval);
421 case POWER_SUPPLY_PROP_CAPACITY:
422 ret = ds2781_get_capacity(dev_info, &val->intval);
425 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
426 ret = ds2781_get_accumulated_current(dev_info, &val->intval);
429 case POWER_SUPPLY_PROP_CHARGE_NOW:
430 ret = ds2781_get_charge_now(dev_info, &val->intval);
440 static 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,
453 static ssize_t ds2781_get_pmod_enabled(struct device *dev,
454 struct device_attribute *attr,
459 struct power_supply *psy = to_power_supply(dev);
460 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
463 ret = ds2781_get_control_register(dev_info, &control_reg);
467 return sprintf(buf, "%d\n",
468 !!(control_reg & DS2781_CONTROL_PMOD));
471 static ssize_t ds2781_set_pmod_enabled(struct device *dev,
472 struct device_attribute *attr,
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);
482 ret = ds2781_get_control_register(dev_info, &control_reg);
486 ret = kstrtou8(buf, 0, &new_setting);
490 if ((new_setting != 0) && (new_setting != 1)) {
491 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
496 control_reg |= DS2781_CONTROL_PMOD;
498 control_reg &= ~DS2781_CONTROL_PMOD;
500 ret = ds2781_set_control_register(dev_info, control_reg);
507 static ssize_t ds2781_get_sense_resistor_value(struct device *dev,
508 struct device_attribute *attr,
513 struct power_supply *psy = to_power_supply(dev);
514 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
516 ret = ds2781_read8(dev_info, &sense_resistor, DS2781_RSNSP);
520 ret = sprintf(buf, "%d\n", sense_resistor);
524 static ssize_t ds2781_set_sense_resistor_value(struct device *dev,
525 struct device_attribute *attr,
531 struct power_supply *psy = to_power_supply(dev);
532 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
534 ret = kstrtou8(buf, 0, &new_setting);
538 ret = ds2781_set_sense_register(dev_info, new_setting);
545 static ssize_t ds2781_get_rsgain_setting(struct device *dev,
546 struct device_attribute *attr,
551 struct power_supply *psy = to_power_supply(dev);
552 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
554 ret = ds2781_get_rsgain_register(dev_info, &rsgain);
558 return sprintf(buf, "%d\n", rsgain);
561 static ssize_t ds2781_set_rsgain_setting(struct device *dev,
562 struct device_attribute *attr,
568 struct power_supply *psy = to_power_supply(dev);
569 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
571 ret = kstrtou16(buf, 0, &new_setting);
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");
581 ret = ds2781_set_rsgain_register(dev_info, new_setting);
588 static ssize_t ds2781_get_pio_pin(struct device *dev,
589 struct device_attribute *attr,
594 struct power_supply *psy = to_power_supply(dev);
595 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
597 ret = ds2781_read8(dev_info, &sfr, DS2781_SFR);
601 ret = sprintf(buf, "%d\n", sfr & DS2781_SFR_PIOSC);
605 static ssize_t ds2781_set_pio_pin(struct device *dev,
606 struct device_attribute *attr,
612 struct power_supply *psy = to_power_supply(dev);
613 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
615 ret = kstrtou8(buf, 0, &new_setting);
619 if ((new_setting != 0) && (new_setting != 1)) {
620 dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
624 ret = ds2781_write(dev_info, &new_setting,
625 DS2781_SFR, sizeof(u8));
632 static 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)
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);
641 count = min_t(loff_t, count, DS2781_PARAM_EEPROM_SIZE - off);
643 return ds2781_read_block(dev_info, buf,
644 DS2781_EEPROM_BLOCK1_START + off, count);
647 static 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)
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);
657 count = min_t(loff_t, count, DS2781_PARAM_EEPROM_SIZE - off);
659 ret = ds2781_write(dev_info, buf,
660 DS2781_EEPROM_BLOCK1_START + off, count);
664 ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK1_START);
671 static struct bin_attribute ds2781_param_eeprom_bin_attr = {
673 .name = "param_eeprom",
674 .mode = S_IRUGO | S_IWUSR,
676 .size = DS2781_PARAM_EEPROM_SIZE,
677 .read = ds2781_read_param_eeprom_bin,
678 .write = ds2781_write_param_eeprom_bin,
681 static 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)
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);
690 count = min_t(loff_t, count, DS2781_USER_EEPROM_SIZE - off);
692 return ds2781_read_block(dev_info, buf,
693 DS2781_EEPROM_BLOCK0_START + off, count);
697 static 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)
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);
707 count = min_t(loff_t, count, DS2781_USER_EEPROM_SIZE - off);
709 ret = ds2781_write(dev_info, buf,
710 DS2781_EEPROM_BLOCK0_START + off, count);
714 ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK0_START);
721 static struct bin_attribute ds2781_user_eeprom_bin_attr = {
723 .name = "user_eeprom",
724 .mode = S_IRUGO | S_IWUSR,
726 .size = DS2781_USER_EEPROM_SIZE,
727 .read = ds2781_read_user_eeprom_bin,
728 .write = ds2781_write_user_eeprom_bin,
731 static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2781_get_pmod_enabled,
732 ds2781_set_pmod_enabled);
733 static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
734 ds2781_get_sense_resistor_value, ds2781_set_sense_resistor_value);
735 static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2781_get_rsgain_setting,
736 ds2781_set_rsgain_setting);
737 static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2781_get_pio_pin,
741 static 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,
749 static const struct attribute_group ds2781_attr_group = {
750 .attrs = ds2781_attributes,
753 static int ds2781_battery_probe(struct platform_device *pdev)
756 struct ds2781_device_info *dev_info;
758 dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
762 platform_set_drvdata(pdev, dev_info);
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;
772 ret = power_supply_register(&pdev->dev, &dev_info->bat);
774 dev_err(dev_info->dev, "failed to register battery\n");
778 ret = sysfs_create_group(&dev_info->bat.dev->kobj, &ds2781_attr_group);
780 dev_err(dev_info->dev, "failed to create sysfs group\n");
781 goto fail_unregister;
784 ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
785 &ds2781_param_eeprom_bin_attr);
787 dev_err(dev_info->dev,
788 "failed to create param eeprom bin file");
789 goto fail_remove_group;
792 ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
793 &ds2781_user_eeprom_bin_attr);
795 dev_err(dev_info->dev,
796 "failed to create user eeprom bin file");
797 goto fail_remove_bin_file;
802 fail_remove_bin_file:
803 sysfs_remove_bin_file(&dev_info->bat.dev->kobj,
804 &ds2781_param_eeprom_bin_attr);
806 sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2781_attr_group);
808 power_supply_unregister(&dev_info->bat);
813 static int ds2781_battery_remove(struct platform_device *pdev)
815 struct ds2781_device_info *dev_info = platform_get_drvdata(pdev);
817 /* remove attributes */
818 sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2781_attr_group);
820 power_supply_unregister(&dev_info->bat);
825 static struct platform_driver ds2781_battery_driver = {
827 .name = "ds2781-battery",
829 .probe = ds2781_battery_probe,
830 .remove = ds2781_battery_remove,
832 module_platform_driver(ds2781_battery_driver);
834 MODULE_LICENSE("GPL");
835 MODULE_AUTHOR("Renata Sayakhova <renata@oktetlabs.ru>");
836 MODULE_DESCRIPTION("Maxim/Dallas DS2781 Stand-Alone Fuel Gauage IC driver");
837 MODULE_ALIAS("platform:ds2781-battery");