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 int ds2780_battery_io(struct ds2780_device_info *dev_info,
60 char *buf, int addr, size_t count, int io)
62 return w1_ds2780_io(dev_info->w1_dev, buf, addr, count, io);
65 static inline int ds2780_read8(struct ds2780_device_info *dev_info, u8 *val,
68 return ds2780_battery_io(dev_info, val, addr, sizeof(u8), 0);
71 static int ds2780_read16(struct ds2780_device_info *dev_info, s16 *val,
77 ret = ds2780_battery_io(dev_info, raw, addr, sizeof(raw), 0);
81 *val = (raw[0] << 8) | raw[1];
86 static inline int ds2780_read_block(struct ds2780_device_info *dev_info,
87 u8 *val, int addr, size_t count)
89 return ds2780_battery_io(dev_info, val, addr, count, 0);
92 static inline int ds2780_write(struct ds2780_device_info *dev_info, u8 *val,
93 int addr, size_t count)
95 return ds2780_battery_io(dev_info, val, addr, count, 1);
98 static inline int ds2780_store_eeprom(struct device *dev, int addr)
100 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_COPY_DATA);
103 static inline int ds2780_recall_eeprom(struct device *dev, int addr)
105 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_RECALL_DATA);
108 static int ds2780_save_eeprom(struct ds2780_device_info *dev_info, int reg)
112 ret = ds2780_store_eeprom(dev_info->w1_dev, reg);
116 ret = ds2780_recall_eeprom(dev_info->w1_dev, reg);
123 /* Set sense resistor value in mhos */
124 static int ds2780_set_sense_register(struct ds2780_device_info *dev_info,
129 ret = ds2780_write(dev_info, &conductance,
130 DS2780_RSNSP_REG, sizeof(u8));
134 return ds2780_save_eeprom(dev_info, DS2780_RSNSP_REG);
137 /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
138 static int ds2780_get_rsgain_register(struct ds2780_device_info *dev_info,
141 return ds2780_read16(dev_info, rsgain, DS2780_RSGAIN_MSB_REG);
144 /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
145 static int ds2780_set_rsgain_register(struct ds2780_device_info *dev_info,
149 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
151 ret = ds2780_write(dev_info, raw,
152 DS2780_RSGAIN_MSB_REG, sizeof(raw));
156 return ds2780_save_eeprom(dev_info, DS2780_RSGAIN_MSB_REG);
159 static int ds2780_get_voltage(struct ds2780_device_info *dev_info,
166 * The voltage value is located in 10 bits across the voltage MSB
167 * and LSB registers in two's compliment form
168 * Sign bit of the voltage value is in bit 7 of the voltage MSB register
169 * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
170 * voltage MSB register
171 * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
172 * voltage LSB register
174 ret = ds2780_read16(dev_info, &voltage_raw,
175 DS2780_VOLT_MSB_REG);
180 * DS2780 reports voltage in units of 4.88mV, but the battery class
181 * reports in units of uV, so convert by multiplying by 4880.
183 *voltage_uV = (voltage_raw / 32) * 4880;
187 static int ds2780_get_temperature(struct ds2780_device_info *dev_info,
194 * The temperature value is located in 10 bits across the temperature
195 * MSB and LSB registers in two's compliment form
196 * Sign bit of the temperature value is in bit 7 of the temperature
198 * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
199 * temperature MSB register
200 * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
201 * temperature LSB register
203 ret = ds2780_read16(dev_info, &temperature_raw,
204 DS2780_TEMP_MSB_REG);
209 * Temperature is measured in units of 0.125 degrees celcius, the
210 * power_supply class measures temperature in tenths of degrees
211 * celsius. The temperature value is stored as a 10 bit number, plus
212 * sign in the upper bits of a 16 bit register.
214 *temperature = ((temperature_raw / 32) * 125) / 100;
218 static int ds2780_get_current(struct ds2780_device_info *dev_info,
219 enum current_types type, int *current_uA)
223 u8 sense_res_raw, reg_msb;
226 * The units of measurement for current are dependent on the value of
227 * the sense resistor.
229 ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
233 if (sense_res_raw == 0) {
234 dev_err(dev_info->dev, "sense resistor value is 0\n");
237 sense_res = 1000 / sense_res_raw;
239 if (type == CURRENT_NOW)
240 reg_msb = DS2780_CURRENT_MSB_REG;
241 else if (type == CURRENT_AVG)
242 reg_msb = DS2780_IAVG_MSB_REG;
247 * The current value is located in 16 bits across the current MSB
248 * and LSB registers in two's compliment form
249 * Sign bit of the current value is in bit 7 of the current MSB register
250 * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
252 * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
255 ret = ds2780_read16(dev_info, ¤t_raw, reg_msb);
259 *current_uA = current_raw * (DS2780_CURRENT_UNITS / sense_res);
263 static int ds2780_get_accumulated_current(struct ds2780_device_info *dev_info,
264 int *accumulated_current)
271 * The units of measurement for accumulated current are dependent on
272 * the value of the sense resistor.
274 ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
278 if (sense_res_raw == 0) {
279 dev_err(dev_info->dev, "sense resistor value is 0\n");
282 sense_res = 1000 / sense_res_raw;
285 * The ACR value is located in 16 bits across the ACR MSB and
287 * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
289 * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
292 ret = ds2780_read16(dev_info, ¤t_raw, DS2780_ACR_MSB_REG);
296 *accumulated_current = current_raw * (DS2780_CHARGE_UNITS / sense_res);
300 static int ds2780_get_capacity(struct ds2780_device_info *dev_info,
306 ret = ds2780_read8(dev_info, &raw, DS2780_RARC_REG);
314 static int ds2780_get_status(struct ds2780_device_info *dev_info, int *status)
316 int ret, current_uA, capacity;
318 ret = ds2780_get_current(dev_info, CURRENT_NOW, ¤t_uA);
322 ret = ds2780_get_capacity(dev_info, &capacity);
327 *status = POWER_SUPPLY_STATUS_FULL;
328 else if (current_uA == 0)
329 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
330 else if (current_uA < 0)
331 *status = POWER_SUPPLY_STATUS_DISCHARGING;
333 *status = POWER_SUPPLY_STATUS_CHARGING;
338 static int ds2780_get_charge_now(struct ds2780_device_info *dev_info,
345 * The RAAC value is located in 16 bits across the RAAC MSB and
347 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
349 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
352 ret = ds2780_read16(dev_info, &charge_raw, DS2780_RAAC_MSB_REG);
356 *charge_now = charge_raw * 1600;
360 static int ds2780_get_control_register(struct ds2780_device_info *dev_info,
363 return ds2780_read8(dev_info, control_reg, DS2780_CONTROL_REG);
366 static int ds2780_set_control_register(struct ds2780_device_info *dev_info,
371 ret = ds2780_write(dev_info, &control_reg,
372 DS2780_CONTROL_REG, sizeof(u8));
376 return ds2780_save_eeprom(dev_info, DS2780_CONTROL_REG);
379 static int ds2780_battery_get_property(struct power_supply *psy,
380 enum power_supply_property psp,
381 union power_supply_propval *val)
384 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
387 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
388 ret = ds2780_get_voltage(dev_info, &val->intval);
391 case POWER_SUPPLY_PROP_TEMP:
392 ret = ds2780_get_temperature(dev_info, &val->intval);
395 case POWER_SUPPLY_PROP_MODEL_NAME:
399 case POWER_SUPPLY_PROP_MANUFACTURER:
400 val->strval = manufacturer;
403 case POWER_SUPPLY_PROP_CURRENT_NOW:
404 ret = ds2780_get_current(dev_info, CURRENT_NOW, &val->intval);
407 case POWER_SUPPLY_PROP_CURRENT_AVG:
408 ret = ds2780_get_current(dev_info, CURRENT_AVG, &val->intval);
411 case POWER_SUPPLY_PROP_STATUS:
412 ret = ds2780_get_status(dev_info, &val->intval);
415 case POWER_SUPPLY_PROP_CAPACITY:
416 ret = ds2780_get_capacity(dev_info, &val->intval);
419 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
420 ret = ds2780_get_accumulated_current(dev_info, &val->intval);
423 case POWER_SUPPLY_PROP_CHARGE_NOW:
424 ret = ds2780_get_charge_now(dev_info, &val->intval);
434 static enum power_supply_property ds2780_battery_props[] = {
435 POWER_SUPPLY_PROP_STATUS,
436 POWER_SUPPLY_PROP_VOLTAGE_NOW,
437 POWER_SUPPLY_PROP_TEMP,
438 POWER_SUPPLY_PROP_MODEL_NAME,
439 POWER_SUPPLY_PROP_MANUFACTURER,
440 POWER_SUPPLY_PROP_CURRENT_NOW,
441 POWER_SUPPLY_PROP_CURRENT_AVG,
442 POWER_SUPPLY_PROP_CAPACITY,
443 POWER_SUPPLY_PROP_CHARGE_COUNTER,
444 POWER_SUPPLY_PROP_CHARGE_NOW,
447 static ssize_t ds2780_get_pmod_enabled(struct device *dev,
448 struct device_attribute *attr,
453 struct power_supply *psy = to_power_supply(dev);
454 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
457 ret = ds2780_get_control_register(dev_info, &control_reg);
461 return sprintf(buf, "%d\n",
462 !!(control_reg & DS2780_CONTROL_REG_PMOD));
465 static ssize_t ds2780_set_pmod_enabled(struct device *dev,
466 struct device_attribute *attr,
471 u8 control_reg, new_setting;
472 struct power_supply *psy = to_power_supply(dev);
473 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
476 ret = ds2780_get_control_register(dev_info, &control_reg);
480 ret = kstrtou8(buf, 0, &new_setting);
484 if ((new_setting != 0) && (new_setting != 1)) {
485 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
490 control_reg |= DS2780_CONTROL_REG_PMOD;
492 control_reg &= ~DS2780_CONTROL_REG_PMOD;
494 ret = ds2780_set_control_register(dev_info, control_reg);
501 static ssize_t ds2780_get_sense_resistor_value(struct device *dev,
502 struct device_attribute *attr,
507 struct power_supply *psy = to_power_supply(dev);
508 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
510 ret = ds2780_read8(dev_info, &sense_resistor, DS2780_RSNSP_REG);
514 ret = sprintf(buf, "%d\n", sense_resistor);
518 static ssize_t ds2780_set_sense_resistor_value(struct device *dev,
519 struct device_attribute *attr,
525 struct power_supply *psy = to_power_supply(dev);
526 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
528 ret = kstrtou8(buf, 0, &new_setting);
532 ret = ds2780_set_sense_register(dev_info, new_setting);
539 static ssize_t ds2780_get_rsgain_setting(struct device *dev,
540 struct device_attribute *attr,
545 struct power_supply *psy = to_power_supply(dev);
546 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
548 ret = ds2780_get_rsgain_register(dev_info, &rsgain);
552 return sprintf(buf, "%d\n", rsgain);
555 static ssize_t ds2780_set_rsgain_setting(struct device *dev,
556 struct device_attribute *attr,
562 struct power_supply *psy = to_power_supply(dev);
563 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
565 ret = kstrtou16(buf, 0, &new_setting);
569 /* Gain can only be from 0 to 1.999 in steps of .001 */
570 if (new_setting > 1999) {
571 dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
575 ret = ds2780_set_rsgain_register(dev_info, new_setting);
582 static ssize_t ds2780_get_pio_pin(struct device *dev,
583 struct device_attribute *attr,
588 struct power_supply *psy = to_power_supply(dev);
589 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
591 ret = ds2780_read8(dev_info, &sfr, DS2780_SFR_REG);
595 ret = sprintf(buf, "%d\n", sfr & DS2780_SFR_REG_PIOSC);
599 static ssize_t ds2780_set_pio_pin(struct device *dev,
600 struct device_attribute *attr,
606 struct power_supply *psy = to_power_supply(dev);
607 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
609 ret = kstrtou8(buf, 0, &new_setting);
613 if ((new_setting != 0) && (new_setting != 1)) {
614 dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
618 ret = ds2780_write(dev_info, &new_setting,
619 DS2780_SFR_REG, sizeof(u8));
626 static ssize_t ds2780_read_param_eeprom_bin(struct file *filp,
627 struct kobject *kobj,
628 struct bin_attribute *bin_attr,
629 char *buf, loff_t off, size_t count)
631 struct device *dev = container_of(kobj, struct device, kobj);
632 struct power_supply *psy = to_power_supply(dev);
633 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
635 return ds2780_read_block(dev_info, buf,
636 DS2780_EEPROM_BLOCK1_START + off, count);
639 static ssize_t ds2780_write_param_eeprom_bin(struct file *filp,
640 struct kobject *kobj,
641 struct bin_attribute *bin_attr,
642 char *buf, loff_t off, size_t count)
644 struct device *dev = container_of(kobj, struct device, kobj);
645 struct power_supply *psy = to_power_supply(dev);
646 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
649 ret = ds2780_write(dev_info, buf,
650 DS2780_EEPROM_BLOCK1_START + off, count);
654 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK1_START);
661 static const struct bin_attribute ds2780_param_eeprom_bin_attr = {
663 .name = "param_eeprom",
664 .mode = S_IRUGO | S_IWUSR,
666 .size = DS2780_PARAM_EEPROM_SIZE,
667 .read = ds2780_read_param_eeprom_bin,
668 .write = ds2780_write_param_eeprom_bin,
671 static ssize_t ds2780_read_user_eeprom_bin(struct file *filp,
672 struct kobject *kobj,
673 struct bin_attribute *bin_attr,
674 char *buf, loff_t off, size_t count)
676 struct device *dev = container_of(kobj, struct device, kobj);
677 struct power_supply *psy = to_power_supply(dev);
678 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
680 return ds2780_read_block(dev_info, buf,
681 DS2780_EEPROM_BLOCK0_START + off, count);
684 static ssize_t ds2780_write_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);
694 ret = ds2780_write(dev_info, buf,
695 DS2780_EEPROM_BLOCK0_START + off, count);
699 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK0_START);
706 static const struct bin_attribute ds2780_user_eeprom_bin_attr = {
708 .name = "user_eeprom",
709 .mode = S_IRUGO | S_IWUSR,
711 .size = DS2780_USER_EEPROM_SIZE,
712 .read = ds2780_read_user_eeprom_bin,
713 .write = ds2780_write_user_eeprom_bin,
716 static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2780_get_pmod_enabled,
717 ds2780_set_pmod_enabled);
718 static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
719 ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value);
720 static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2780_get_rsgain_setting,
721 ds2780_set_rsgain_setting);
722 static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2780_get_pio_pin,
726 static struct attribute *ds2780_attributes[] = {
727 &dev_attr_pmod_enabled.attr,
728 &dev_attr_sense_resistor_value.attr,
729 &dev_attr_rsgain_setting.attr,
730 &dev_attr_pio_pin.attr,
734 static const struct attribute_group ds2780_attr_group = {
735 .attrs = ds2780_attributes,
738 static int ds2780_battery_probe(struct platform_device *pdev)
740 struct power_supply_config psy_cfg = {};
742 struct ds2780_device_info *dev_info;
744 dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
750 platform_set_drvdata(pdev, dev_info);
752 dev_info->dev = &pdev->dev;
753 dev_info->w1_dev = pdev->dev.parent;
754 dev_info->bat_desc.name = dev_name(&pdev->dev);
755 dev_info->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
756 dev_info->bat_desc.properties = ds2780_battery_props;
757 dev_info->bat_desc.num_properties = ARRAY_SIZE(ds2780_battery_props);
758 dev_info->bat_desc.get_property = ds2780_battery_get_property;
760 psy_cfg.drv_data = dev_info;
762 dev_info->bat = power_supply_register(&pdev->dev, &dev_info->bat_desc,
764 if (IS_ERR(dev_info->bat)) {
765 dev_err(dev_info->dev, "failed to register battery\n");
766 ret = PTR_ERR(dev_info->bat);
770 ret = sysfs_create_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
772 dev_err(dev_info->dev, "failed to create sysfs group\n");
773 goto fail_unregister;
776 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj,
777 &ds2780_param_eeprom_bin_attr);
779 dev_err(dev_info->dev,
780 "failed to create param eeprom bin file");
781 goto fail_remove_group;
784 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj,
785 &ds2780_user_eeprom_bin_attr);
787 dev_err(dev_info->dev,
788 "failed to create user eeprom bin file");
789 goto fail_remove_bin_file;
794 fail_remove_bin_file:
795 sysfs_remove_bin_file(&dev_info->bat->dev.kobj,
796 &ds2780_param_eeprom_bin_attr);
798 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
800 power_supply_unregister(dev_info->bat);
805 static int ds2780_battery_remove(struct platform_device *pdev)
807 struct ds2780_device_info *dev_info = platform_get_drvdata(pdev);
810 * Remove attributes before unregistering power supply
811 * because 'bat' will be freed on power_supply_unregister() call.
813 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
815 power_supply_unregister(dev_info->bat);
820 static struct platform_driver ds2780_battery_driver = {
822 .name = "ds2780-battery",
824 .probe = ds2780_battery_probe,
825 .remove = ds2780_battery_remove,
828 module_platform_driver(ds2780_battery_driver);
830 MODULE_LICENSE("GPL");
831 MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
832 MODULE_DESCRIPTION("Maxim/Dallas DS2780 Stand-Alone Fuel Gauage IC driver");
833 MODULE_ALIAS("platform:ds2780-battery");