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 device *w1_dev;
49 static const char model[] = "DS2780";
50 static const char manufacturer[] = "Maxim/Dallas";
52 static inline struct ds2780_device_info *
53 to_ds2780_device_info(struct power_supply *psy)
55 return container_of(psy, struct ds2780_device_info, bat);
58 static inline struct power_supply *to_power_supply(struct device *dev)
60 return dev_get_drvdata(dev);
63 static inline int ds2780_battery_io(struct ds2780_device_info *dev_info,
64 char *buf, int addr, size_t count, int io)
66 return w1_ds2780_io(dev_info->w1_dev, buf, addr, count, io);
69 static inline int ds2780_read8(struct ds2780_device_info *dev_info, u8 *val,
72 return ds2780_battery_io(dev_info, val, addr, sizeof(u8), 0);
75 static int ds2780_read16(struct ds2780_device_info *dev_info, s16 *val,
81 ret = ds2780_battery_io(dev_info, raw, addr, sizeof(raw), 0);
85 *val = (raw[0] << 8) | raw[1];
90 static inline int ds2780_read_block(struct ds2780_device_info *dev_info,
91 u8 *val, int addr, size_t count)
93 return ds2780_battery_io(dev_info, val, addr, count, 0);
96 static inline int ds2780_write(struct ds2780_device_info *dev_info, u8 *val,
97 int addr, size_t count)
99 return ds2780_battery_io(dev_info, val, addr, count, 1);
102 static inline int ds2780_store_eeprom(struct device *dev, int addr)
104 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_COPY_DATA);
107 static inline int ds2780_recall_eeprom(struct device *dev, int addr)
109 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_RECALL_DATA);
112 static int ds2780_save_eeprom(struct ds2780_device_info *dev_info, int reg)
116 ret = ds2780_store_eeprom(dev_info->w1_dev, reg);
120 ret = ds2780_recall_eeprom(dev_info->w1_dev, reg);
127 /* Set sense resistor value in mhos */
128 static int ds2780_set_sense_register(struct ds2780_device_info *dev_info,
133 ret = ds2780_write(dev_info, &conductance,
134 DS2780_RSNSP_REG, sizeof(u8));
138 return ds2780_save_eeprom(dev_info, DS2780_RSNSP_REG);
141 /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
142 static int ds2780_get_rsgain_register(struct ds2780_device_info *dev_info,
145 return ds2780_read16(dev_info, rsgain, DS2780_RSGAIN_MSB_REG);
148 /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
149 static int ds2780_set_rsgain_register(struct ds2780_device_info *dev_info,
153 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
155 ret = ds2780_write(dev_info, raw,
156 DS2780_RSGAIN_MSB_REG, sizeof(raw));
160 return ds2780_save_eeprom(dev_info, DS2780_RSGAIN_MSB_REG);
163 static int ds2780_get_voltage(struct ds2780_device_info *dev_info,
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 ret = ds2780_read16(dev_info, &voltage_raw,
179 DS2780_VOLT_MSB_REG);
184 * DS2780 reports voltage in units of 4.88mV, but the battery class
185 * reports in units of uV, so convert by multiplying by 4880.
187 *voltage_uV = (voltage_raw / 32) * 4880;
191 static int ds2780_get_temperature(struct ds2780_device_info *dev_info,
198 * The temperature value is located in 10 bits across the temperature
199 * MSB and LSB registers in two's compliment form
200 * Sign bit of the temperature value is in bit 7 of the temperature
202 * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
203 * temperature MSB register
204 * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
205 * temperature LSB register
207 ret = ds2780_read16(dev_info, &temperature_raw,
208 DS2780_TEMP_MSB_REG);
213 * Temperature is measured in units of 0.125 degrees celcius, the
214 * power_supply class measures temperature in tenths of degrees
215 * celsius. The temperature value is stored as a 10 bit number, plus
216 * sign in the upper bits of a 16 bit register.
218 *temperature = ((temperature_raw / 32) * 125) / 100;
222 static int ds2780_get_current(struct ds2780_device_info *dev_info,
223 enum current_types type, int *current_uA)
227 u8 sense_res_raw, reg_msb;
230 * The units of measurement for current are dependent on the value of
231 * the sense resistor.
233 ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
237 if (sense_res_raw == 0) {
238 dev_err(dev_info->dev, "sense resistor value is 0\n");
241 sense_res = 1000 / sense_res_raw;
243 if (type == CURRENT_NOW)
244 reg_msb = DS2780_CURRENT_MSB_REG;
245 else if (type == CURRENT_AVG)
246 reg_msb = DS2780_IAVG_MSB_REG;
251 * The current value is located in 16 bits across the current MSB
252 * and LSB registers in two's compliment form
253 * Sign bit of the current value is in bit 7 of the current MSB register
254 * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
256 * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
259 ret = ds2780_read16(dev_info, ¤t_raw, reg_msb);
263 *current_uA = current_raw * (DS2780_CURRENT_UNITS / sense_res);
267 static int ds2780_get_accumulated_current(struct ds2780_device_info *dev_info,
268 int *accumulated_current)
275 * The units of measurement for accumulated current are dependent on
276 * the value of the sense resistor.
278 ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
282 if (sense_res_raw == 0) {
283 dev_err(dev_info->dev, "sense resistor value is 0\n");
286 sense_res = 1000 / sense_res_raw;
289 * The ACR value is located in 16 bits across the ACR MSB and
291 * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
293 * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
296 ret = ds2780_read16(dev_info, ¤t_raw, DS2780_ACR_MSB_REG);
300 *accumulated_current = current_raw * (DS2780_CHARGE_UNITS / sense_res);
304 static int ds2780_get_capacity(struct ds2780_device_info *dev_info,
310 ret = ds2780_read8(dev_info, &raw, DS2780_RARC_REG);
318 static int ds2780_get_status(struct ds2780_device_info *dev_info, int *status)
320 int ret, current_uA, capacity;
322 ret = ds2780_get_current(dev_info, CURRENT_NOW, ¤t_uA);
326 ret = ds2780_get_capacity(dev_info, &capacity);
331 *status = POWER_SUPPLY_STATUS_FULL;
332 else if (current_uA == 0)
333 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
334 else if (current_uA < 0)
335 *status = POWER_SUPPLY_STATUS_DISCHARGING;
337 *status = POWER_SUPPLY_STATUS_CHARGING;
342 static int ds2780_get_charge_now(struct ds2780_device_info *dev_info,
349 * The RAAC value is located in 16 bits across the RAAC MSB and
351 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
353 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
356 ret = ds2780_read16(dev_info, &charge_raw, DS2780_RAAC_MSB_REG);
360 *charge_now = charge_raw * 1600;
364 static int ds2780_get_control_register(struct ds2780_device_info *dev_info,
367 return ds2780_read8(dev_info, control_reg, DS2780_CONTROL_REG);
370 static int ds2780_set_control_register(struct ds2780_device_info *dev_info,
375 ret = ds2780_write(dev_info, &control_reg,
376 DS2780_CONTROL_REG, sizeof(u8));
380 return ds2780_save_eeprom(dev_info, DS2780_CONTROL_REG);
383 static int ds2780_battery_get_property(struct power_supply *psy,
384 enum power_supply_property psp,
385 union power_supply_propval *val)
388 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
391 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
392 ret = ds2780_get_voltage(dev_info, &val->intval);
395 case POWER_SUPPLY_PROP_TEMP:
396 ret = ds2780_get_temperature(dev_info, &val->intval);
399 case POWER_SUPPLY_PROP_MODEL_NAME:
403 case POWER_SUPPLY_PROP_MANUFACTURER:
404 val->strval = manufacturer;
407 case POWER_SUPPLY_PROP_CURRENT_NOW:
408 ret = ds2780_get_current(dev_info, CURRENT_NOW, &val->intval);
411 case POWER_SUPPLY_PROP_CURRENT_AVG:
412 ret = ds2780_get_current(dev_info, CURRENT_AVG, &val->intval);
415 case POWER_SUPPLY_PROP_STATUS:
416 ret = ds2780_get_status(dev_info, &val->intval);
419 case POWER_SUPPLY_PROP_CAPACITY:
420 ret = ds2780_get_capacity(dev_info, &val->intval);
423 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
424 ret = ds2780_get_accumulated_current(dev_info, &val->intval);
427 case POWER_SUPPLY_PROP_CHARGE_NOW:
428 ret = ds2780_get_charge_now(dev_info, &val->intval);
438 static enum power_supply_property ds2780_battery_props[] = {
439 POWER_SUPPLY_PROP_STATUS,
440 POWER_SUPPLY_PROP_VOLTAGE_NOW,
441 POWER_SUPPLY_PROP_TEMP,
442 POWER_SUPPLY_PROP_MODEL_NAME,
443 POWER_SUPPLY_PROP_MANUFACTURER,
444 POWER_SUPPLY_PROP_CURRENT_NOW,
445 POWER_SUPPLY_PROP_CURRENT_AVG,
446 POWER_SUPPLY_PROP_CAPACITY,
447 POWER_SUPPLY_PROP_CHARGE_COUNTER,
448 POWER_SUPPLY_PROP_CHARGE_NOW,
451 static ssize_t ds2780_get_pmod_enabled(struct device *dev,
452 struct device_attribute *attr,
457 struct power_supply *psy = to_power_supply(dev);
458 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
461 ret = ds2780_get_control_register(dev_info, &control_reg);
465 return sprintf(buf, "%d\n",
466 !!(control_reg & DS2780_CONTROL_REG_PMOD));
469 static ssize_t ds2780_set_pmod_enabled(struct device *dev,
470 struct device_attribute *attr,
475 u8 control_reg, new_setting;
476 struct power_supply *psy = to_power_supply(dev);
477 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
480 ret = ds2780_get_control_register(dev_info, &control_reg);
484 ret = kstrtou8(buf, 0, &new_setting);
488 if ((new_setting != 0) && (new_setting != 1)) {
489 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
494 control_reg |= DS2780_CONTROL_REG_PMOD;
496 control_reg &= ~DS2780_CONTROL_REG_PMOD;
498 ret = ds2780_set_control_register(dev_info, control_reg);
505 static ssize_t ds2780_get_sense_resistor_value(struct device *dev,
506 struct device_attribute *attr,
511 struct power_supply *psy = to_power_supply(dev);
512 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
514 ret = ds2780_read8(dev_info, &sense_resistor, DS2780_RSNSP_REG);
518 ret = sprintf(buf, "%d\n", sense_resistor);
522 static ssize_t ds2780_set_sense_resistor_value(struct device *dev,
523 struct device_attribute *attr,
529 struct power_supply *psy = to_power_supply(dev);
530 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
532 ret = kstrtou8(buf, 0, &new_setting);
536 ret = ds2780_set_sense_register(dev_info, new_setting);
543 static ssize_t ds2780_get_rsgain_setting(struct device *dev,
544 struct device_attribute *attr,
549 struct power_supply *psy = to_power_supply(dev);
550 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
552 ret = ds2780_get_rsgain_register(dev_info, &rsgain);
556 return sprintf(buf, "%d\n", rsgain);
559 static ssize_t ds2780_set_rsgain_setting(struct device *dev,
560 struct device_attribute *attr,
566 struct power_supply *psy = to_power_supply(dev);
567 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
569 ret = kstrtou16(buf, 0, &new_setting);
573 /* Gain can only be from 0 to 1.999 in steps of .001 */
574 if (new_setting > 1999) {
575 dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
579 ret = ds2780_set_rsgain_register(dev_info, new_setting);
586 static ssize_t ds2780_get_pio_pin(struct device *dev,
587 struct device_attribute *attr,
592 struct power_supply *psy = to_power_supply(dev);
593 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
595 ret = ds2780_read8(dev_info, &sfr, DS2780_SFR_REG);
599 ret = sprintf(buf, "%d\n", sfr & DS2780_SFR_REG_PIOSC);
603 static ssize_t ds2780_set_pio_pin(struct device *dev,
604 struct device_attribute *attr,
610 struct power_supply *psy = to_power_supply(dev);
611 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
613 ret = kstrtou8(buf, 0, &new_setting);
617 if ((new_setting != 0) && (new_setting != 1)) {
618 dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
622 ret = ds2780_write(dev_info, &new_setting,
623 DS2780_SFR_REG, sizeof(u8));
630 static ssize_t ds2780_read_param_eeprom_bin(struct file *filp,
631 struct kobject *kobj,
632 struct bin_attribute *bin_attr,
633 char *buf, loff_t off, size_t count)
635 struct device *dev = container_of(kobj, struct device, kobj);
636 struct power_supply *psy = to_power_supply(dev);
637 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
639 count = min_t(loff_t, count,
640 DS2780_EEPROM_BLOCK1_END -
641 DS2780_EEPROM_BLOCK1_START + 1 - off);
643 return ds2780_read_block(dev_info, buf,
644 DS2780_EEPROM_BLOCK1_START + off, count);
647 static ssize_t ds2780_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 ds2780_device_info *dev_info = to_ds2780_device_info(psy);
657 count = min_t(loff_t, count,
658 DS2780_EEPROM_BLOCK1_END -
659 DS2780_EEPROM_BLOCK1_START + 1 - off);
661 ret = ds2780_write(dev_info, buf,
662 DS2780_EEPROM_BLOCK1_START + off, count);
666 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK1_START);
673 static struct bin_attribute ds2780_param_eeprom_bin_attr = {
675 .name = "param_eeprom",
676 .mode = S_IRUGO | S_IWUSR,
678 .size = DS2780_EEPROM_BLOCK1_END - DS2780_EEPROM_BLOCK1_START + 1,
679 .read = ds2780_read_param_eeprom_bin,
680 .write = ds2780_write_param_eeprom_bin,
683 static ssize_t ds2780_read_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 ds2780_device_info *dev_info = to_ds2780_device_info(psy);
692 count = min_t(loff_t, count,
693 DS2780_EEPROM_BLOCK0_END -
694 DS2780_EEPROM_BLOCK0_START + 1 - off);
696 return ds2780_read_block(dev_info, buf,
697 DS2780_EEPROM_BLOCK0_START + off, count);
700 static ssize_t ds2780_write_user_eeprom_bin(struct file *filp,
701 struct kobject *kobj,
702 struct bin_attribute *bin_attr,
703 char *buf, loff_t off, size_t count)
705 struct device *dev = container_of(kobj, struct device, kobj);
706 struct power_supply *psy = to_power_supply(dev);
707 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
710 count = min_t(loff_t, count,
711 DS2780_EEPROM_BLOCK0_END -
712 DS2780_EEPROM_BLOCK0_START + 1 - off);
714 ret = ds2780_write(dev_info, buf,
715 DS2780_EEPROM_BLOCK0_START + off, count);
719 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK0_START);
726 static struct bin_attribute ds2780_user_eeprom_bin_attr = {
728 .name = "user_eeprom",
729 .mode = S_IRUGO | S_IWUSR,
731 .size = DS2780_EEPROM_BLOCK0_END - DS2780_EEPROM_BLOCK0_START + 1,
732 .read = ds2780_read_user_eeprom_bin,
733 .write = ds2780_write_user_eeprom_bin,
736 static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2780_get_pmod_enabled,
737 ds2780_set_pmod_enabled);
738 static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
739 ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value);
740 static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2780_get_rsgain_setting,
741 ds2780_set_rsgain_setting);
742 static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2780_get_pio_pin,
746 static struct attribute *ds2780_attributes[] = {
747 &dev_attr_pmod_enabled.attr,
748 &dev_attr_sense_resistor_value.attr,
749 &dev_attr_rsgain_setting.attr,
750 &dev_attr_pio_pin.attr,
754 static const struct attribute_group ds2780_attr_group = {
755 .attrs = ds2780_attributes,
758 static int ds2780_battery_probe(struct platform_device *pdev)
761 struct ds2780_device_info *dev_info;
763 dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
769 platform_set_drvdata(pdev, dev_info);
771 dev_info->dev = &pdev->dev;
772 dev_info->w1_dev = pdev->dev.parent;
773 dev_info->bat.name = dev_name(&pdev->dev);
774 dev_info->bat.type = POWER_SUPPLY_TYPE_BATTERY;
775 dev_info->bat.properties = ds2780_battery_props;
776 dev_info->bat.num_properties = ARRAY_SIZE(ds2780_battery_props);
777 dev_info->bat.get_property = ds2780_battery_get_property;
779 ret = power_supply_register(&pdev->dev, &dev_info->bat);
781 dev_err(dev_info->dev, "failed to register battery\n");
785 ret = sysfs_create_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
787 dev_err(dev_info->dev, "failed to create sysfs group\n");
788 goto fail_unregister;
791 ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
792 &ds2780_param_eeprom_bin_attr);
794 dev_err(dev_info->dev,
795 "failed to create param eeprom bin file");
796 goto fail_remove_group;
799 ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
800 &ds2780_user_eeprom_bin_attr);
802 dev_err(dev_info->dev,
803 "failed to create user eeprom bin file");
804 goto fail_remove_bin_file;
809 fail_remove_bin_file:
810 sysfs_remove_bin_file(&dev_info->bat.dev->kobj,
811 &ds2780_param_eeprom_bin_attr);
813 sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
815 power_supply_unregister(&dev_info->bat);
820 static int ds2780_battery_remove(struct platform_device *pdev)
822 struct ds2780_device_info *dev_info = platform_get_drvdata(pdev);
824 /* remove attributes */
825 sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
827 power_supply_unregister(&dev_info->bat);
832 static struct platform_driver ds2780_battery_driver = {
834 .name = "ds2780-battery",
836 .probe = ds2780_battery_probe,
837 .remove = ds2780_battery_remove,
840 module_platform_driver(ds2780_battery_driver);
842 MODULE_LICENSE("GPL");
843 MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
844 MODULE_DESCRIPTION("Maxim/Dallas DS2780 Stand-Alone Fuel Gauage IC driver");
845 MODULE_ALIAS("platform:ds2780-battery");