14 #include <linux/module.h>
15 #include <linux/slab.h>
23 #include "../w1/slaves/w1_ds2781.h"
26 #define DS2781_CURRENT_UNITS 1563
28 #define DS2781_CHARGE_UNITS 6250
30 #define DS2781_USER_EEPROM_SIZE (DS2781_EEPROM_BLOCK0_END - \
31 DS2781_EEPROM_BLOCK0_START + 1)
33 #define DS2781_PARAM_EEPROM_SIZE (DS2781_EEPROM_BLOCK1_END - \
34 DS2781_EEPROM_BLOCK1_START + 1)
47 static const char model[] =
"DS2781";
70 return ds2781_battery_io(dev_info, buf, addr, count, 0);
76 return ds2781_battery_io(dev_info, val, addr,
sizeof(
u8), 0);
85 ret = ds2781_battery_io(dev_info, raw, addr,
sizeof(raw), 0);
89 *val = (raw[0] << 8) | raw[1];
95 u8 *val,
int addr,
size_t count)
97 return ds2781_battery_io(dev_info, val, addr, count, 0);
101 int addr,
size_t count)
103 return ds2781_battery_io(dev_info, val, addr, count, 1);
106 static inline int ds2781_store_eeprom(
struct device *
dev,
int addr)
111 static inline int ds2781_recall_eeprom(
struct device *
dev,
int addr)
120 ret = ds2781_store_eeprom(dev_info->
w1_dev, reg);
124 ret = ds2781_recall_eeprom(dev_info->
w1_dev, reg);
137 ret = ds2781_write(dev_info, &conductance,
157 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
159 ret = ds2781_write(dev_info, raw,
186 voltage_raw = (val[0] << 3) |
191 *voltage_uV = voltage_raw * 9760;
216 temp_raw = ((val[0]) << 3) |
218 *temp = temp_raw + (temp_raw / 4);
228 u8 sense_res_raw, reg_msb;
234 ret = ds2781_read8(dev_info, &sense_res_raw,
DS2781_RSNSP);
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;
260 ret = ds2781_read16(dev_info, ¤t_raw, reg_msb);
269 int *accumulated_current)
279 ret = ds2781_read8(dev_info, &sense_res_raw,
DS2781_RSNSP);
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;
323 ret = ds2781_get_current(dev_info,
CURRENT_NOW, ¤t_uA);
327 ret = ds2781_get_capacity(dev_info, &capacity);
334 else if (current_uA > 50000)
362 *charge_now = charge_raw * 1600;
377 ret = ds2781_write(dev_info, &control_reg,
385 static int ds2781_battery_get_property(
struct power_supply *psy,
394 ret = ds2781_get_voltage(dev_info, &val->
intval);
398 ret = ds2781_get_temperature(dev_info, &val->
intval);
418 ret = ds2781_get_status(dev_info, &val->
intval);
422 ret = ds2781_get_capacity(dev_info, &val->
intval);
426 ret = ds2781_get_accumulated_current(dev_info, &val->
intval);
430 ret = ds2781_get_charge_now(dev_info, &val->
intval);
463 ret = ds2781_get_control_register(dev_info, &control_reg);
477 u8 control_reg, new_setting;
482 ret = ds2781_get_control_register(dev_info, &control_reg);
486 ret =
kstrtou8(buf, 0, &new_setting);
490 if ((new_setting != 0) && (new_setting != 1)) {
491 dev_err(dev_info->
dev,
"Invalid pmod setting (0 or 1)\n");
500 ret = ds2781_set_control_register(dev_info, control_reg);
507 static ssize_t ds2781_get_sense_resistor_value(
struct device *dev,
516 ret = ds2781_read8(dev_info, &sense_resistor,
DS2781_RSNSP);
520 ret =
sprintf(buf,
"%d\n", sense_resistor);
524 static ssize_t ds2781_set_sense_resistor_value(
struct device *dev,
534 ret =
kstrtou8(buf, 0, &new_setting);
538 ret = ds2781_set_sense_register(dev_info, new_setting);
554 ret = ds2781_get_rsgain_register(dev_info, &rsgain);
558 return sprintf(buf,
"%d\n", rsgain);
576 if (new_setting > 1999) {
577 dev_err(dev_info->
dev,
"Invalid rsgain setting (0 - 1999)\n");
581 ret = ds2781_set_rsgain_register(dev_info, new_setting);
597 ret = ds2781_read8(dev_info, &sfr,
DS2781_SFR);
615 ret =
kstrtou8(buf, 0, &new_setting);
619 if ((new_setting != 0) && (new_setting != 1)) {
620 dev_err(dev_info->
dev,
"Invalid pio_pin setting (0 or 1)\n");
624 ret = ds2781_write(dev_info, &new_setting,
632 static ssize_t ds2781_read_param_eeprom_bin(
struct file *filp,
635 char *buf, loff_t off,
size_t count)
643 return ds2781_read_block(dev_info, buf,
647 static ssize_t ds2781_write_param_eeprom_bin(
struct file *filp,
650 char *buf, loff_t off,
size_t count)
659 ret = ds2781_write(dev_info, buf,
673 .name =
"param_eeprom",
677 .read = ds2781_read_param_eeprom_bin,
678 .write = ds2781_write_param_eeprom_bin,
681 static ssize_t ds2781_read_user_eeprom_bin(
struct file *filp,
684 char *buf, loff_t off,
size_t count)
692 return ds2781_read_block(dev_info, buf,
697 static ssize_t ds2781_write_user_eeprom_bin(
struct file *filp,
700 char *buf, loff_t off,
size_t count)
709 ret = ds2781_write(dev_info, buf,
723 .name =
"user_eeprom",
727 .read = ds2781_read_user_eeprom_bin,
728 .write = ds2781_write_user_eeprom_bin,
732 ds2781_set_pmod_enabled);
734 ds2781_get_sense_resistor_value, ds2781_set_sense_resistor_value);
736 ds2781_set_rsgain_setting);
741 static struct attribute *ds2781_attributes[] = {
742 &dev_attr_pmod_enabled.attr,
743 &dev_attr_sense_resistor_value.attr,
744 &dev_attr_rsgain_setting.attr,
745 &dev_attr_pio_pin.attr,
750 .attrs = ds2781_attributes,
762 platform_set_drvdata(pdev, dev_info);
764 dev_info->
dev = &pdev->
dev;
766 dev_info->
bat.name = dev_name(&pdev->
dev);
768 dev_info->
bat.properties = ds2781_battery_props;
769 dev_info->
bat.num_properties =
ARRAY_SIZE(ds2781_battery_props);
770 dev_info->
bat.get_property = ds2781_battery_get_property;
774 dev_err(dev_info->
dev,
"failed to register battery\n");
780 dev_err(dev_info->
dev,
"failed to create sysfs group\n");
781 goto fail_unregister;
785 &ds2781_param_eeprom_bin_attr);
788 "failed to create param eeprom bin file");
789 goto fail_remove_group;
793 &ds2781_user_eeprom_bin_attr);
796 "failed to create user eeprom bin file");
797 goto fail_remove_bin_file;
802 fail_remove_bin_file:
804 &ds2781_param_eeprom_bin_attr);
827 .name =
"ds2781-battery",
829 .probe = ds2781_battery_probe,