2 * 1-wire client/driver for the Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC
4 * Copyright (C) 2010 Indesign, LLC
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 return ds2780_read_block(dev_info, buf,
641 DS2780_EEPROM_BLOCK1_START + off, count);
644 static ssize_t ds2780_write_param_eeprom_bin(struct file *filp,
645 struct kobject *kobj,
646 struct bin_attribute *bin_attr,
647 char *buf, loff_t off, size_t count)
649 struct device *dev = container_of(kobj, struct device, kobj);
650 struct power_supply *psy = to_power_supply(dev);
651 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
654 ret = ds2780_write(dev_info, buf,
655 DS2780_EEPROM_BLOCK1_START + off, count);
659 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK1_START);
666 static struct bin_attribute ds2780_param_eeprom_bin_attr = {
668 .name = "param_eeprom",
669 .mode = S_IRUGO | S_IWUSR,
671 .size = DS2780_PARAM_EEPROM_SIZE,
672 .read = ds2780_read_param_eeprom_bin,
673 .write = ds2780_write_param_eeprom_bin,
676 static ssize_t ds2780_read_user_eeprom_bin(struct file *filp,
677 struct kobject *kobj,
678 struct bin_attribute *bin_attr,
679 char *buf, loff_t off, size_t count)
681 struct device *dev = container_of(kobj, struct device, kobj);
682 struct power_supply *psy = to_power_supply(dev);
683 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
685 return ds2780_read_block(dev_info, buf,
686 DS2780_EEPROM_BLOCK0_START + off, count);
689 static ssize_t ds2780_write_user_eeprom_bin(struct file *filp,
690 struct kobject *kobj,
691 struct bin_attribute *bin_attr,
692 char *buf, loff_t off, size_t count)
694 struct device *dev = container_of(kobj, struct device, kobj);
695 struct power_supply *psy = to_power_supply(dev);
696 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
699 ret = ds2780_write(dev_info, buf,
700 DS2780_EEPROM_BLOCK0_START + off, count);
704 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK0_START);
711 static struct bin_attribute ds2780_user_eeprom_bin_attr = {
713 .name = "user_eeprom",
714 .mode = S_IRUGO | S_IWUSR,
716 .size = DS2780_USER_EEPROM_SIZE,
717 .read = ds2780_read_user_eeprom_bin,
718 .write = ds2780_write_user_eeprom_bin,
721 static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2780_get_pmod_enabled,
722 ds2780_set_pmod_enabled);
723 static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
724 ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value);
725 static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2780_get_rsgain_setting,
726 ds2780_set_rsgain_setting);
727 static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2780_get_pio_pin,
731 static struct attribute *ds2780_attributes[] = {
732 &dev_attr_pmod_enabled.attr,
733 &dev_attr_sense_resistor_value.attr,
734 &dev_attr_rsgain_setting.attr,
735 &dev_attr_pio_pin.attr,
739 static const struct attribute_group ds2780_attr_group = {
740 .attrs = ds2780_attributes,
743 static int ds2780_battery_probe(struct platform_device *pdev)
745 struct power_supply_config psy_cfg = {};
747 struct ds2780_device_info *dev_info;
749 dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
755 platform_set_drvdata(pdev, dev_info);
757 dev_info->dev = &pdev->dev;
758 dev_info->w1_dev = pdev->dev.parent;
759 dev_info->bat_desc.name = dev_name(&pdev->dev);
760 dev_info->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
761 dev_info->bat_desc.properties = ds2780_battery_props;
762 dev_info->bat_desc.num_properties = ARRAY_SIZE(ds2780_battery_props);
763 dev_info->bat_desc.get_property = ds2780_battery_get_property;
765 psy_cfg.drv_data = dev_info;
767 dev_info->bat = power_supply_register(&pdev->dev, &dev_info->bat_desc,
769 if (IS_ERR(dev_info->bat)) {
770 dev_err(dev_info->dev, "failed to register battery\n");
771 ret = PTR_ERR(dev_info->bat);
775 ret = sysfs_create_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
777 dev_err(dev_info->dev, "failed to create sysfs group\n");
778 goto fail_unregister;
781 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj,
782 &ds2780_param_eeprom_bin_attr);
784 dev_err(dev_info->dev,
785 "failed to create param eeprom bin file");
786 goto fail_remove_group;
789 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj,
790 &ds2780_user_eeprom_bin_attr);
792 dev_err(dev_info->dev,
793 "failed to create user eeprom bin file");
794 goto fail_remove_bin_file;
799 fail_remove_bin_file:
800 sysfs_remove_bin_file(&dev_info->bat->dev.kobj,
801 &ds2780_param_eeprom_bin_attr);
803 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
805 power_supply_unregister(dev_info->bat);
810 static int ds2780_battery_remove(struct platform_device *pdev)
812 struct ds2780_device_info *dev_info = platform_get_drvdata(pdev);
815 * Remove attributes before unregistering power supply
816 * because 'bat' will be freed on power_supply_unregister() call.
818 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
820 power_supply_unregister(dev_info->bat);
825 static struct platform_driver ds2780_battery_driver = {
827 .name = "ds2780-battery",
829 .probe = ds2780_battery_probe,
830 .remove = ds2780_battery_remove,
833 module_platform_driver(ds2780_battery_driver);
835 MODULE_LICENSE("GPL");
837 MODULE_DESCRIPTION("Maxim/Dallas DS2780 Stand-Alone Fuel Gauage IC driver");
838 MODULE_ALIAS("platform:ds2780-battery");