16 #include <linux/module.h>
17 #include <linux/slab.h>
25 #include "../w1/slaves/w1_ds2780.h"
28 #define DS2780_CURRENT_UNITS 1563
30 #define DS2780_CHARGE_UNITS 6250
32 #define DS2780_USER_EEPROM_SIZE (DS2780_EEPROM_BLOCK0_END - \
33 DS2780_EEPROM_BLOCK0_START + 1)
35 #define DS2780_PARAM_EEPROM_SIZE (DS2780_EEPROM_BLOCK1_END - \
36 DS2780_EEPROM_BLOCK1_START + 1)
49 static const char model[] =
"DS2780";
72 return ds2780_battery_io(dev_info, val, addr,
sizeof(
u8), 0);
81 ret = ds2780_battery_io(dev_info, raw, addr,
sizeof(raw), 0);
85 *val = (raw[0] << 8) | raw[1];
91 u8 *val,
int addr,
size_t count)
93 return ds2780_battery_io(dev_info, val, addr, count, 0);
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)
107 static inline int ds2780_recall_eeprom(
struct device *
dev,
int addr)
116 ret = ds2780_store_eeprom(dev_info->
w1_dev, reg);
120 ret = ds2780_recall_eeprom(dev_info->
w1_dev, reg);
133 ret = ds2780_write(dev_info, &conductance,
153 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
155 ret = ds2780_write(dev_info, raw,
178 ret = ds2780_read16(dev_info, &voltage_raw,
187 *voltage_uV = (voltage_raw / 32) * 4880;
207 ret = ds2780_read16(dev_info, &temperature_raw,
218 *temperature = ((temperature_raw / 32) * 125) / 100;
227 u8 sense_res_raw, reg_msb;
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;
259 ret = ds2780_read16(dev_info, ¤t_raw, reg_msb);
268 int *accumulated_current)
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;
322 ret = ds2780_get_current(dev_info,
CURRENT_NOW, ¤t_uA);
326 ret = ds2780_get_capacity(dev_info, &capacity);
332 else if (current_uA == 0)
334 else if (current_uA < 0)
360 *charge_now = charge_raw * 1600;
375 ret = ds2780_write(dev_info, &control_reg,
383 static int ds2780_battery_get_property(
struct power_supply *psy,
392 ret = ds2780_get_voltage(dev_info, &val->
intval);
396 ret = ds2780_get_temperature(dev_info, &val->
intval);
416 ret = ds2780_get_status(dev_info, &val->
intval);
420 ret = ds2780_get_capacity(dev_info, &val->
intval);
424 ret = ds2780_get_accumulated_current(dev_info, &val->
intval);
428 ret = ds2780_get_charge_now(dev_info, &val->
intval);
461 ret = ds2780_get_control_register(dev_info, &control_reg);
475 u8 control_reg, new_setting;
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");
498 ret = ds2780_set_control_register(dev_info, control_reg);
505 static ssize_t ds2780_get_sense_resistor_value(
struct device *dev,
518 ret =
sprintf(buf,
"%d\n", sense_resistor);
522 static ssize_t ds2780_set_sense_resistor_value(
struct device *dev,
532 ret =
kstrtou8(buf, 0, &new_setting);
536 ret = ds2780_set_sense_register(dev_info, new_setting);
552 ret = ds2780_get_rsgain_register(dev_info, &rsgain);
556 return sprintf(buf,
"%d\n", rsgain);
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);
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,
630 static ssize_t ds2780_read_param_eeprom_bin(
struct file *filp,
633 char *buf, loff_t off,
size_t count)
639 count =
min_t(loff_t, count,
643 return ds2780_read_block(dev_info, buf,
647 static ssize_t ds2780_write_param_eeprom_bin(
struct file *filp,
650 char *buf, loff_t off,
size_t count)
657 count =
min_t(loff_t, count,
661 ret = ds2780_write(dev_info, buf,
675 .name =
"param_eeprom",
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,
686 char *buf, loff_t off,
size_t count)
692 count =
min_t(loff_t, count,
696 return ds2780_read_block(dev_info, buf,
700 static ssize_t ds2780_write_user_eeprom_bin(
struct file *filp,
703 char *buf, loff_t off,
size_t count)
710 count =
min_t(loff_t, count,
714 ret = ds2780_write(dev_info, buf,
728 .name =
"user_eeprom",
732 .read = ds2780_read_user_eeprom_bin,
733 .write = ds2780_write_user_eeprom_bin,
737 ds2780_set_pmod_enabled);
739 ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value);
741 ds2780_set_rsgain_setting);
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,
755 .attrs = ds2780_attributes,
763 dev_info = kzalloc(
sizeof(*dev_info),
GFP_KERNEL);
769 platform_set_drvdata(pdev, dev_info);
771 dev_info->
dev = &pdev->
dev;
773 dev_info->
bat.name = dev_name(&pdev->
dev);
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;
781 dev_err(dev_info->
dev,
"failed to register battery\n");
787 dev_err(dev_info->
dev,
"failed to create sysfs group\n");
788 goto fail_unregister;
792 &ds2780_param_eeprom_bin_attr);
795 "failed to create param eeprom bin file");
796 goto fail_remove_group;
800 &ds2780_user_eeprom_bin_attr);
803 "failed to create user eeprom bin file");
804 goto fail_remove_bin_file;
809 fail_remove_bin_file:
811 &ds2780_param_eeprom_bin_attr);
837 .name =
"ds2780-battery",
839 .probe = ds2780_battery_probe,