2 * 1-wire client/driver for the Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC
4 * Copyright (C) 2010 Indesign, LLC
6 * Author: Clifton Barnes <cabarnes@indesign-llc.com>
8 * Based on ds2760_battery and ds2782_battery drivers
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/param.h>
20 #include <linux/platform_device.h>
21 #include <linux/power_supply.h>
22 #include <linux/idr.h>
25 #include "../w1/slaves/w1_ds2780.h"
27 /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
28 #define DS2780_CURRENT_UNITS 1563
29 /* Charge unit measurement in uAh for a 1 milli-ohm sense resistor */
30 #define DS2780_CHARGE_UNITS 6250
31 /* Number of bytes in user EEPROM space */
32 #define DS2780_USER_EEPROM_SIZE (DS2780_EEPROM_BLOCK0_END - \
33 DS2780_EEPROM_BLOCK0_START + 1)
34 /* Number of bytes in parameter EEPROM space */
35 #define DS2780_PARAM_EEPROM_SIZE (DS2780_EEPROM_BLOCK1_END - \
36 DS2780_EEPROM_BLOCK1_START + 1)
38 struct ds2780_device_info {
40 struct power_supply *bat;
41 struct power_supply_desc bat_desc;
42 struct device *w1_dev;
50 static const char model[] = "DS2780";
51 static const char manufacturer[] = "Maxim/Dallas";
53 static inline struct ds2780_device_info *
54 to_ds2780_device_info(struct power_supply *psy)
56 return power_supply_get_drvdata(psy);
59 static inline struct power_supply *to_power_supply(struct device *dev)
61 return dev_get_drvdata(dev);
64 static inline int ds2780_battery_io(struct ds2780_device_info *dev_info,
65 char *buf, int addr, size_t count, int io)
67 return w1_ds2780_io(dev_info->w1_dev, buf, addr, count, io);
70 static inline int ds2780_read8(struct ds2780_device_info *dev_info, u8 *val,
73 return ds2780_battery_io(dev_info, val, addr, sizeof(u8), 0);
76 static int ds2780_read16(struct ds2780_device_info *dev_info, s16 *val,
82 ret = ds2780_battery_io(dev_info, raw, addr, sizeof(raw), 0);
86 *val = (raw[0] << 8) | raw[1];
91 static inline int ds2780_read_block(struct ds2780_device_info *dev_info,
92 u8 *val, int addr, size_t count)
94 return ds2780_battery_io(dev_info, val, addr, count, 0);
97 static inline int ds2780_write(struct ds2780_device_info *dev_info, u8 *val,
98 int addr, size_t count)
100 return ds2780_battery_io(dev_info, val, addr, count, 1);
103 static inline int ds2780_store_eeprom(struct device *dev, int addr)
105 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_COPY_DATA);
108 static inline int ds2780_recall_eeprom(struct device *dev, int addr)
110 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_RECALL_DATA);
113 static int ds2780_save_eeprom(struct ds2780_device_info *dev_info, int reg)
117 ret = ds2780_store_eeprom(dev_info->w1_dev, reg);
121 ret = ds2780_recall_eeprom(dev_info->w1_dev, reg);
128 /* Set sense resistor value in mhos */
129 static int ds2780_set_sense_register(struct ds2780_device_info *dev_info,
134 ret = ds2780_write(dev_info, &conductance,
135 DS2780_RSNSP_REG, sizeof(u8));
139 return ds2780_save_eeprom(dev_info, DS2780_RSNSP_REG);
142 /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
143 static int ds2780_get_rsgain_register(struct ds2780_device_info *dev_info,
146 return ds2780_read16(dev_info, rsgain, DS2780_RSGAIN_MSB_REG);
149 /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
150 static int ds2780_set_rsgain_register(struct ds2780_device_info *dev_info,
154 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
156 ret = ds2780_write(dev_info, raw,
157 DS2780_RSGAIN_MSB_REG, sizeof(raw));
161 return ds2780_save_eeprom(dev_info, DS2780_RSGAIN_MSB_REG);
164 static int ds2780_get_voltage(struct ds2780_device_info *dev_info,
171 * The voltage value is located in 10 bits across the voltage MSB
172 * and LSB registers in two's compliment form
173 * Sign bit of the voltage value is in bit 7 of the voltage MSB register
174 * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
175 * voltage MSB register
176 * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
177 * voltage LSB register
179 ret = ds2780_read16(dev_info, &voltage_raw,
180 DS2780_VOLT_MSB_REG);
185 * DS2780 reports voltage in units of 4.88mV, but the battery class
186 * reports in units of uV, so convert by multiplying by 4880.
188 *voltage_uV = (voltage_raw / 32) * 4880;
192 static int ds2780_get_temperature(struct ds2780_device_info *dev_info,
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 ret = ds2780_read16(dev_info, &temperature_raw,
209 DS2780_TEMP_MSB_REG);
214 * Temperature is measured in units of 0.125 degrees celcius, the
215 * power_supply class measures temperature in tenths of degrees
216 * celsius. The temperature value is stored as a 10 bit number, plus
217 * sign in the upper bits of a 16 bit register.
219 *temperature = ((temperature_raw / 32) * 125) / 100;
223 static int ds2780_get_current(struct ds2780_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 = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
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 = DS2780_CURRENT_MSB_REG;
246 else if (type == CURRENT_AVG)
247 reg_msb = DS2780_IAVG_MSB_REG;
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 = ds2780_read16(dev_info, ¤t_raw, reg_msb);
264 *current_uA = current_raw * (DS2780_CURRENT_UNITS / sense_res);
268 static int ds2780_get_accumulated_current(struct ds2780_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 = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
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 = ds2780_read16(dev_info, ¤t_raw, DS2780_ACR_MSB_REG);
301 *accumulated_current = current_raw * (DS2780_CHARGE_UNITS / sense_res);
305 static int ds2780_get_capacity(struct ds2780_device_info *dev_info,
311 ret = ds2780_read8(dev_info, &raw, DS2780_RARC_REG);
319 static int ds2780_get_status(struct ds2780_device_info *dev_info, int *status)
321 int ret, current_uA, capacity;
323 ret = ds2780_get_current(dev_info, CURRENT_NOW, ¤t_uA);
327 ret = ds2780_get_capacity(dev_info, &capacity);
332 *status = POWER_SUPPLY_STATUS_FULL;
333 else if (current_uA == 0)
334 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
335 else if (current_uA < 0)
336 *status = POWER_SUPPLY_STATUS_DISCHARGING;
338 *status = POWER_SUPPLY_STATUS_CHARGING;
343 static int ds2780_get_charge_now(struct ds2780_device_info *dev_info,
350 * The RAAC value is located in 16 bits across the RAAC MSB and
352 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
354 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
357 ret = ds2780_read16(dev_info, &charge_raw, DS2780_RAAC_MSB_REG);
361 *charge_now = charge_raw * 1600;
365 static int ds2780_get_control_register(struct ds2780_device_info *dev_info,
368 return ds2780_read8(dev_info, control_reg, DS2780_CONTROL_REG);
371 static int ds2780_set_control_register(struct ds2780_device_info *dev_info,
376 ret = ds2780_write(dev_info, &control_reg,
377 DS2780_CONTROL_REG, sizeof(u8));
381 return ds2780_save_eeprom(dev_info, DS2780_CONTROL_REG);
384 static int ds2780_battery_get_property(struct power_supply *psy,
385 enum power_supply_property psp,
386 union power_supply_propval *val)
389 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
392 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
393 ret = ds2780_get_voltage(dev_info, &val->intval);
396 case POWER_SUPPLY_PROP_TEMP:
397 ret = ds2780_get_temperature(dev_info, &val->intval);
400 case POWER_SUPPLY_PROP_MODEL_NAME:
404 case POWER_SUPPLY_PROP_MANUFACTURER:
405 val->strval = manufacturer;
408 case POWER_SUPPLY_PROP_CURRENT_NOW:
409 ret = ds2780_get_current(dev_info, CURRENT_NOW, &val->intval);
412 case POWER_SUPPLY_PROP_CURRENT_AVG:
413 ret = ds2780_get_current(dev_info, CURRENT_AVG, &val->intval);
416 case POWER_SUPPLY_PROP_STATUS:
417 ret = ds2780_get_status(dev_info, &val->intval);
420 case POWER_SUPPLY_PROP_CAPACITY:
421 ret = ds2780_get_capacity(dev_info, &val->intval);
424 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
425 ret = ds2780_get_accumulated_current(dev_info, &val->intval);
428 case POWER_SUPPLY_PROP_CHARGE_NOW:
429 ret = ds2780_get_charge_now(dev_info, &val->intval);
439 static enum power_supply_property ds2780_battery_props[] = {
440 POWER_SUPPLY_PROP_STATUS,
441 POWER_SUPPLY_PROP_VOLTAGE_NOW,
442 POWER_SUPPLY_PROP_TEMP,
443 POWER_SUPPLY_PROP_MODEL_NAME,
444 POWER_SUPPLY_PROP_MANUFACTURER,
445 POWER_SUPPLY_PROP_CURRENT_NOW,
446 POWER_SUPPLY_PROP_CURRENT_AVG,
447 POWER_SUPPLY_PROP_CAPACITY,
448 POWER_SUPPLY_PROP_CHARGE_COUNTER,
449 POWER_SUPPLY_PROP_CHARGE_NOW,
452 static ssize_t ds2780_get_pmod_enabled(struct device *dev,
453 struct device_attribute *attr,
458 struct power_supply *psy = to_power_supply(dev);
459 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
462 ret = ds2780_get_control_register(dev_info, &control_reg);
466 return sprintf(buf, "%d\n",
467 !!(control_reg & DS2780_CONTROL_REG_PMOD));
470 static ssize_t ds2780_set_pmod_enabled(struct device *dev,
471 struct device_attribute *attr,
476 u8 control_reg, new_setting;
477 struct power_supply *psy = to_power_supply(dev);
478 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
481 ret = ds2780_get_control_register(dev_info, &control_reg);
485 ret = kstrtou8(buf, 0, &new_setting);
489 if ((new_setting != 0) && (new_setting != 1)) {
490 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
495 control_reg |= DS2780_CONTROL_REG_PMOD;
497 control_reg &= ~DS2780_CONTROL_REG_PMOD;
499 ret = ds2780_set_control_register(dev_info, control_reg);
506 static ssize_t ds2780_get_sense_resistor_value(struct device *dev,
507 struct device_attribute *attr,
512 struct power_supply *psy = to_power_supply(dev);
513 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
515 ret = ds2780_read8(dev_info, &sense_resistor, DS2780_RSNSP_REG);
519 ret = sprintf(buf, "%d\n", sense_resistor);
523 static ssize_t ds2780_set_sense_resistor_value(struct device *dev,
524 struct device_attribute *attr,
530 struct power_supply *psy = to_power_supply(dev);
531 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
533 ret = kstrtou8(buf, 0, &new_setting);
537 ret = ds2780_set_sense_register(dev_info, new_setting);
544 static ssize_t ds2780_get_rsgain_setting(struct device *dev,
545 struct device_attribute *attr,
550 struct power_supply *psy = to_power_supply(dev);
551 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
553 ret = ds2780_get_rsgain_register(dev_info, &rsgain);
557 return sprintf(buf, "%d\n", rsgain);
560 static ssize_t ds2780_set_rsgain_setting(struct device *dev,
561 struct device_attribute *attr,
567 struct power_supply *psy = to_power_supply(dev);
568 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
570 ret = kstrtou16(buf, 0, &new_setting);
574 /* Gain can only be from 0 to 1.999 in steps of .001 */
575 if (new_setting > 1999) {
576 dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
580 ret = ds2780_set_rsgain_register(dev_info, new_setting);
587 static ssize_t ds2780_get_pio_pin(struct device *dev,
588 struct device_attribute *attr,
593 struct power_supply *psy = to_power_supply(dev);
594 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
596 ret = ds2780_read8(dev_info, &sfr, DS2780_SFR_REG);
600 ret = sprintf(buf, "%d\n", sfr & DS2780_SFR_REG_PIOSC);
604 static ssize_t ds2780_set_pio_pin(struct device *dev,
605 struct device_attribute *attr,
611 struct power_supply *psy = to_power_supply(dev);
612 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
614 ret = kstrtou8(buf, 0, &new_setting);
618 if ((new_setting != 0) && (new_setting != 1)) {
619 dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
623 ret = ds2780_write(dev_info, &new_setting,
624 DS2780_SFR_REG, sizeof(u8));
631 static ssize_t ds2780_read_param_eeprom_bin(struct file *filp,
632 struct kobject *kobj,
633 struct bin_attribute *bin_attr,
634 char *buf, loff_t off, size_t count)
636 struct device *dev = container_of(kobj, struct device, kobj);
637 struct power_supply *psy = to_power_supply(dev);
638 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
640 count = min_t(loff_t, count,
641 DS2780_EEPROM_BLOCK1_END -
642 DS2780_EEPROM_BLOCK1_START + 1 - off);
644 return ds2780_read_block(dev_info, buf,
645 DS2780_EEPROM_BLOCK1_START + off, count);
648 static ssize_t ds2780_write_param_eeprom_bin(struct file *filp,
649 struct kobject *kobj,
650 struct bin_attribute *bin_attr,
651 char *buf, loff_t off, size_t count)
653 struct device *dev = container_of(kobj, struct device, kobj);
654 struct power_supply *psy = to_power_supply(dev);
655 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
658 count = min_t(loff_t, count,
659 DS2780_EEPROM_BLOCK1_END -
660 DS2780_EEPROM_BLOCK1_START + 1 - off);
662 ret = ds2780_write(dev_info, buf,
663 DS2780_EEPROM_BLOCK1_START + off, count);
667 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK1_START);
674 static struct bin_attribute ds2780_param_eeprom_bin_attr = {
676 .name = "param_eeprom",
677 .mode = S_IRUGO | S_IWUSR,
679 .size = DS2780_EEPROM_BLOCK1_END - DS2780_EEPROM_BLOCK1_START + 1,
680 .read = ds2780_read_param_eeprom_bin,
681 .write = ds2780_write_param_eeprom_bin,
684 static ssize_t ds2780_read_user_eeprom_bin(struct file *filp,
685 struct kobject *kobj,
686 struct bin_attribute *bin_attr,
687 char *buf, loff_t off, size_t count)
689 struct device *dev = container_of(kobj, struct device, kobj);
690 struct power_supply *psy = to_power_supply(dev);
691 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
693 count = min_t(loff_t, count,
694 DS2780_EEPROM_BLOCK0_END -
695 DS2780_EEPROM_BLOCK0_START + 1 - off);
697 return ds2780_read_block(dev_info, buf,
698 DS2780_EEPROM_BLOCK0_START + off, count);
701 static ssize_t ds2780_write_user_eeprom_bin(struct file *filp,
702 struct kobject *kobj,
703 struct bin_attribute *bin_attr,
704 char *buf, loff_t off, size_t count)
706 struct device *dev = container_of(kobj, struct device, kobj);
707 struct power_supply *psy = to_power_supply(dev);
708 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
711 count = min_t(loff_t, count,
712 DS2780_EEPROM_BLOCK0_END -
713 DS2780_EEPROM_BLOCK0_START + 1 - off);
715 ret = ds2780_write(dev_info, buf,
716 DS2780_EEPROM_BLOCK0_START + off, count);
720 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK0_START);
727 static struct bin_attribute ds2780_user_eeprom_bin_attr = {
729 .name = "user_eeprom",
730 .mode = S_IRUGO | S_IWUSR,
732 .size = DS2780_EEPROM_BLOCK0_END - DS2780_EEPROM_BLOCK0_START + 1,
733 .read = ds2780_read_user_eeprom_bin,
734 .write = ds2780_write_user_eeprom_bin,
737 static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2780_get_pmod_enabled,
738 ds2780_set_pmod_enabled);
739 static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
740 ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value);
741 static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2780_get_rsgain_setting,
742 ds2780_set_rsgain_setting);
743 static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2780_get_pio_pin,
747 static struct attribute *ds2780_attributes[] = {
748 &dev_attr_pmod_enabled.attr,
749 &dev_attr_sense_resistor_value.attr,
750 &dev_attr_rsgain_setting.attr,
751 &dev_attr_pio_pin.attr,
755 static const struct attribute_group ds2780_attr_group = {
756 .attrs = ds2780_attributes,
759 static int ds2780_battery_probe(struct platform_device *pdev)
761 struct power_supply_config psy_cfg = {};
763 struct ds2780_device_info *dev_info;
765 dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
771 platform_set_drvdata(pdev, dev_info);
773 dev_info->dev = &pdev->dev;
774 dev_info->w1_dev = pdev->dev.parent;
775 dev_info->bat_desc.name = dev_name(&pdev->dev);
776 dev_info->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
777 dev_info->bat_desc.properties = ds2780_battery_props;
778 dev_info->bat_desc.num_properties = ARRAY_SIZE(ds2780_battery_props);
779 dev_info->bat_desc.get_property = ds2780_battery_get_property;
781 psy_cfg.drv_data = dev_info;
783 dev_info->bat = power_supply_register(&pdev->dev, &dev_info->bat_desc,
785 if (IS_ERR(dev_info->bat)) {
786 dev_err(dev_info->dev, "failed to register battery\n");
787 ret = PTR_ERR(dev_info->bat);
791 ret = sysfs_create_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
793 dev_err(dev_info->dev, "failed to create sysfs group\n");
794 goto fail_unregister;
797 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj,
798 &ds2780_param_eeprom_bin_attr);
800 dev_err(dev_info->dev,
801 "failed to create param eeprom bin file");
802 goto fail_remove_group;
805 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj,
806 &ds2780_user_eeprom_bin_attr);
808 dev_err(dev_info->dev,
809 "failed to create user eeprom bin file");
810 goto fail_remove_bin_file;
815 fail_remove_bin_file:
816 sysfs_remove_bin_file(&dev_info->bat->dev.kobj,
817 &ds2780_param_eeprom_bin_attr);
819 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
821 power_supply_unregister(dev_info->bat);
826 static int ds2780_battery_remove(struct platform_device *pdev)
828 struct ds2780_device_info *dev_info = platform_get_drvdata(pdev);
831 * Remove attributes before unregistering power supply
832 * because 'bat' will be freed on power_supply_unregister() call.
834 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
836 power_supply_unregister(dev_info->bat);
841 static struct platform_driver ds2780_battery_driver = {
843 .name = "ds2780-battery",
845 .probe = ds2780_battery_probe,
846 .remove = ds2780_battery_remove,
849 module_platform_driver(ds2780_battery_driver);
851 MODULE_LICENSE("GPL");
852 MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
853 MODULE_DESCRIPTION("Maxim/Dallas DS2780 Stand-Alone Fuel Gauage IC driver");
854 MODULE_ALIAS("platform:ds2780-battery");