2 * 1-wire client/driver for the Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC
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 power_supply_desc bat_desc;
40 struct device *w1_dev;
48 static const char model[] = "DS2781";
49 static const char manufacturer[] = "Maxim/Dallas";
51 static inline struct ds2781_device_info *
52 to_ds2781_device_info(struct power_supply *psy)
54 return power_supply_get_drvdata(psy);
57 static inline struct power_supply *to_power_supply(struct device *dev)
59 return dev_get_drvdata(dev);
62 static inline int ds2781_battery_io(struct ds2781_device_info *dev_info,
63 char *buf, int addr, size_t count, int io)
65 return w1_ds2781_io(dev_info->w1_dev, buf, addr, count, io);
68 static int w1_ds2781_read(struct ds2781_device_info *dev_info, char *buf,
69 int addr, size_t count)
71 return ds2781_battery_io(dev_info, buf, addr, count, 0);
74 static inline int ds2781_read8(struct ds2781_device_info *dev_info, u8 *val,
77 return ds2781_battery_io(dev_info, val, addr, sizeof(u8), 0);
80 static int ds2781_read16(struct ds2781_device_info *dev_info, s16 *val,
86 ret = ds2781_battery_io(dev_info, raw, addr, sizeof(raw), 0);
90 *val = (raw[0] << 8) | raw[1];
95 static inline int ds2781_read_block(struct ds2781_device_info *dev_info,
96 u8 *val, int addr, size_t count)
98 return ds2781_battery_io(dev_info, val, addr, count, 0);
101 static inline int ds2781_write(struct ds2781_device_info *dev_info, u8 *val,
102 int addr, size_t count)
104 return ds2781_battery_io(dev_info, val, addr, count, 1);
107 static inline int ds2781_store_eeprom(struct device *dev, int addr)
109 return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_COPY_DATA);
112 static inline int ds2781_recall_eeprom(struct device *dev, int addr)
114 return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_RECALL_DATA);
117 static int ds2781_save_eeprom(struct ds2781_device_info *dev_info, int reg)
121 ret = ds2781_store_eeprom(dev_info->w1_dev, reg);
125 ret = ds2781_recall_eeprom(dev_info->w1_dev, reg);
132 /* Set sense resistor value in mhos */
133 static int ds2781_set_sense_register(struct ds2781_device_info *dev_info,
138 ret = ds2781_write(dev_info, &conductance,
139 DS2781_RSNSP, sizeof(u8));
143 return ds2781_save_eeprom(dev_info, DS2781_RSNSP);
146 /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
147 static int ds2781_get_rsgain_register(struct ds2781_device_info *dev_info,
150 return ds2781_read16(dev_info, rsgain, DS2781_RSGAIN_MSB);
153 /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
154 static int ds2781_set_rsgain_register(struct ds2781_device_info *dev_info,
158 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
160 ret = ds2781_write(dev_info, raw,
161 DS2781_RSGAIN_MSB, sizeof(raw));
165 return ds2781_save_eeprom(dev_info, DS2781_RSGAIN_MSB);
168 static int ds2781_get_voltage(struct ds2781_device_info *dev_info,
175 ret = w1_ds2781_read(dev_info, val, DS2781_VOLT_MSB, 2 * sizeof(u8));
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
187 voltage_raw = (val[0] << 3) |
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;
197 static int ds2781_get_temperature(struct ds2781_device_info *dev_info,
204 ret = w1_ds2781_read(dev_info, val, DS2781_TEMP_MSB, 2 * sizeof(u8));
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
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
217 temp_raw = ((val[0]) << 3) |
219 *temp = temp_raw + (temp_raw / 4);
224 static int ds2781_get_current(struct ds2781_device_info *dev_info,
225 enum current_types type, int *current_uA)
229 u8 sense_res_raw, reg_msb;
232 * The units of measurement for current are dependent on the value of
233 * the sense resistor.
235 ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
239 if (sense_res_raw == 0) {
240 dev_err(dev_info->dev, "sense resistor value is 0\n");
243 sense_res = 1000 / sense_res_raw;
245 if (type == CURRENT_NOW)
246 reg_msb = DS2781_CURRENT_MSB;
247 else if (type == CURRENT_AVG)
248 reg_msb = DS2781_IAVG_MSB;
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
258 * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
261 ret = ds2781_read16(dev_info, ¤t_raw, reg_msb);
265 *current_uA = current_raw * (DS2781_CURRENT_UNITS / sense_res);
269 static int ds2781_get_accumulated_current(struct ds2781_device_info *dev_info,
270 int *accumulated_current)
277 * The units of measurement for accumulated current are dependent on
278 * the value of the sense resistor.
280 ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
284 if (sense_res_raw == 0) {
285 dev_err(dev_info->dev, "sense resistor value is 0\n");
288 sense_res = 1000 / sense_res_raw;
291 * The ACR value is located in 16 bits across the ACR MSB and
293 * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
295 * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
298 ret = ds2781_read16(dev_info, ¤t_raw, DS2781_ACR_MSB);
302 *accumulated_current = current_raw * (DS2781_CHARGE_UNITS / sense_res);
306 static int ds2781_get_capacity(struct ds2781_device_info *dev_info,
312 ret = ds2781_read8(dev_info, &raw, DS2781_RARC);
320 static int ds2781_get_status(struct ds2781_device_info *dev_info, int *status)
322 int ret, current_uA, capacity;
324 ret = ds2781_get_current(dev_info, CURRENT_NOW, ¤t_uA);
328 ret = ds2781_get_capacity(dev_info, &capacity);
332 if (power_supply_am_i_supplied(dev_info->bat)) {
334 *status = POWER_SUPPLY_STATUS_FULL;
335 else if (current_uA > 50000)
336 *status = POWER_SUPPLY_STATUS_CHARGING;
338 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
340 *status = POWER_SUPPLY_STATUS_DISCHARGING;
345 static int ds2781_get_charge_now(struct ds2781_device_info *dev_info,
352 * The RAAC value is located in 16 bits across the RAAC MSB and
354 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
356 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
359 ret = ds2781_read16(dev_info, &charge_raw, DS2781_RAAC_MSB);
363 *charge_now = charge_raw * 1600;
367 static int ds2781_get_control_register(struct ds2781_device_info *dev_info,
370 return ds2781_read8(dev_info, control_reg, DS2781_CONTROL);
373 static int ds2781_set_control_register(struct ds2781_device_info *dev_info,
378 ret = ds2781_write(dev_info, &control_reg,
379 DS2781_CONTROL, sizeof(u8));
383 return ds2781_save_eeprom(dev_info, DS2781_CONTROL);
386 static int ds2781_battery_get_property(struct power_supply *psy,
387 enum power_supply_property psp,
388 union power_supply_propval *val)
391 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
394 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
395 ret = ds2781_get_voltage(dev_info, &val->intval);
398 case POWER_SUPPLY_PROP_TEMP:
399 ret = ds2781_get_temperature(dev_info, &val->intval);
402 case POWER_SUPPLY_PROP_MODEL_NAME:
406 case POWER_SUPPLY_PROP_MANUFACTURER:
407 val->strval = manufacturer;
410 case POWER_SUPPLY_PROP_CURRENT_NOW:
411 ret = ds2781_get_current(dev_info, CURRENT_NOW, &val->intval);
414 case POWER_SUPPLY_PROP_CURRENT_AVG:
415 ret = ds2781_get_current(dev_info, CURRENT_AVG, &val->intval);
418 case POWER_SUPPLY_PROP_STATUS:
419 ret = ds2781_get_status(dev_info, &val->intval);
422 case POWER_SUPPLY_PROP_CAPACITY:
423 ret = ds2781_get_capacity(dev_info, &val->intval);
426 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
427 ret = ds2781_get_accumulated_current(dev_info, &val->intval);
430 case POWER_SUPPLY_PROP_CHARGE_NOW:
431 ret = ds2781_get_charge_now(dev_info, &val->intval);
441 static 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,
454 static ssize_t ds2781_get_pmod_enabled(struct device *dev,
455 struct device_attribute *attr,
460 struct power_supply *psy = to_power_supply(dev);
461 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
464 ret = ds2781_get_control_register(dev_info, &control_reg);
468 return sprintf(buf, "%d\n",
469 !!(control_reg & DS2781_CONTROL_PMOD));
472 static ssize_t ds2781_set_pmod_enabled(struct device *dev,
473 struct device_attribute *attr,
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);
483 ret = ds2781_get_control_register(dev_info, &control_reg);
487 ret = kstrtou8(buf, 0, &new_setting);
491 if ((new_setting != 0) && (new_setting != 1)) {
492 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
497 control_reg |= DS2781_CONTROL_PMOD;
499 control_reg &= ~DS2781_CONTROL_PMOD;
501 ret = ds2781_set_control_register(dev_info, control_reg);
508 static ssize_t ds2781_get_sense_resistor_value(struct device *dev,
509 struct device_attribute *attr,
514 struct power_supply *psy = to_power_supply(dev);
515 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
517 ret = ds2781_read8(dev_info, &sense_resistor, DS2781_RSNSP);
521 ret = sprintf(buf, "%d\n", sense_resistor);
525 static ssize_t ds2781_set_sense_resistor_value(struct device *dev,
526 struct device_attribute *attr,
532 struct power_supply *psy = to_power_supply(dev);
533 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
535 ret = kstrtou8(buf, 0, &new_setting);
539 ret = ds2781_set_sense_register(dev_info, new_setting);
546 static ssize_t ds2781_get_rsgain_setting(struct device *dev,
547 struct device_attribute *attr,
552 struct power_supply *psy = to_power_supply(dev);
553 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
555 ret = ds2781_get_rsgain_register(dev_info, &rsgain);
559 return sprintf(buf, "%d\n", rsgain);
562 static ssize_t ds2781_set_rsgain_setting(struct device *dev,
563 struct device_attribute *attr,
569 struct power_supply *psy = to_power_supply(dev);
570 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
572 ret = kstrtou16(buf, 0, &new_setting);
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");
582 ret = ds2781_set_rsgain_register(dev_info, new_setting);
589 static ssize_t ds2781_get_pio_pin(struct device *dev,
590 struct device_attribute *attr,
595 struct power_supply *psy = to_power_supply(dev);
596 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
598 ret = ds2781_read8(dev_info, &sfr, DS2781_SFR);
602 ret = sprintf(buf, "%d\n", sfr & DS2781_SFR_PIOSC);
606 static ssize_t ds2781_set_pio_pin(struct device *dev,
607 struct device_attribute *attr,
613 struct power_supply *psy = to_power_supply(dev);
614 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
616 ret = kstrtou8(buf, 0, &new_setting);
620 if ((new_setting != 0) && (new_setting != 1)) {
621 dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
625 ret = ds2781_write(dev_info, &new_setting,
626 DS2781_SFR, sizeof(u8));
633 static 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)
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);
642 return ds2781_read_block(dev_info, buf,
643 DS2781_EEPROM_BLOCK1_START + off, count);
646 static 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)
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);
656 ret = ds2781_write(dev_info, buf,
657 DS2781_EEPROM_BLOCK1_START + off, count);
661 ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK1_START);
668 static struct bin_attribute ds2781_param_eeprom_bin_attr = {
670 .name = "param_eeprom",
671 .mode = S_IRUGO | S_IWUSR,
673 .size = DS2781_PARAM_EEPROM_SIZE,
674 .read = ds2781_read_param_eeprom_bin,
675 .write = ds2781_write_param_eeprom_bin,
678 static 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)
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);
687 return ds2781_read_block(dev_info, buf,
688 DS2781_EEPROM_BLOCK0_START + off, count);
692 static 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)
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);
702 ret = ds2781_write(dev_info, buf,
703 DS2781_EEPROM_BLOCK0_START + off, count);
707 ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK0_START);
714 static struct bin_attribute ds2781_user_eeprom_bin_attr = {
716 .name = "user_eeprom",
717 .mode = S_IRUGO | S_IWUSR,
719 .size = DS2781_USER_EEPROM_SIZE,
720 .read = ds2781_read_user_eeprom_bin,
721 .write = ds2781_write_user_eeprom_bin,
724 static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2781_get_pmod_enabled,
725 ds2781_set_pmod_enabled);
726 static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
727 ds2781_get_sense_resistor_value, ds2781_set_sense_resistor_value);
728 static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2781_get_rsgain_setting,
729 ds2781_set_rsgain_setting);
730 static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2781_get_pio_pin,
734 static 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,
742 static const struct attribute_group ds2781_attr_group = {
743 .attrs = ds2781_attributes,
746 static int ds2781_battery_probe(struct platform_device *pdev)
748 struct power_supply_config psy_cfg = {};
750 struct ds2781_device_info *dev_info;
752 dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
756 platform_set_drvdata(pdev, dev_info);
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;
766 psy_cfg.drv_data = dev_info;
768 dev_info->bat = power_supply_register(&pdev->dev, &dev_info->bat_desc,
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);
776 ret = sysfs_create_group(&dev_info->bat->dev.kobj, &ds2781_attr_group);
778 dev_err(dev_info->dev, "failed to create sysfs group\n");
779 goto fail_unregister;
782 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj,
783 &ds2781_param_eeprom_bin_attr);
785 dev_err(dev_info->dev,
786 "failed to create param eeprom bin file");
787 goto fail_remove_group;
790 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj,
791 &ds2781_user_eeprom_bin_attr);
793 dev_err(dev_info->dev,
794 "failed to create user eeprom bin file");
795 goto fail_remove_bin_file;
800 fail_remove_bin_file:
801 sysfs_remove_bin_file(&dev_info->bat->dev.kobj,
802 &ds2781_param_eeprom_bin_attr);
804 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2781_attr_group);
806 power_supply_unregister(dev_info->bat);
811 static int ds2781_battery_remove(struct platform_device *pdev)
813 struct ds2781_device_info *dev_info = platform_get_drvdata(pdev);
816 * Remove attributes before unregistering power supply
817 * because 'bat' will be freed on power_supply_unregister() call.
819 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2781_attr_group);
821 power_supply_unregister(dev_info->bat);
826 static struct platform_driver ds2781_battery_driver = {
828 .name = "ds2781-battery",
830 .probe = ds2781_battery_probe,
831 .remove = ds2781_battery_remove,
833 module_platform_driver(ds2781_battery_driver);
835 MODULE_LICENSE("GPL");
837 MODULE_DESCRIPTION("Maxim/Dallas DS2781 Stand-Alone Fuel Gauage IC driver");
838 MODULE_ALIAS("platform:ds2781-battery");