Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * 1-wire client/driver for the Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC
0004  *
0005  * Copyright (C) 2010 Indesign, LLC
0006  *
0007  * Author: Clifton Barnes <cabarnes@indesign-llc.com>
0008  *
0009  * Based on ds2760_battery and ds2782_battery drivers
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include <linux/param.h>
0015 #include <linux/pm.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/power_supply.h>
0018 #include <linux/idr.h>
0019 
0020 #include <linux/w1.h>
0021 #include "../../w1/slaves/w1_ds2780.h"
0022 
0023 /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
0024 #define DS2780_CURRENT_UNITS    1563
0025 /* Charge unit measurement in uAh for a 1 milli-ohm sense resistor */
0026 #define DS2780_CHARGE_UNITS     6250
0027 /* Number of bytes in user EEPROM space */
0028 #define DS2780_USER_EEPROM_SIZE     (DS2780_EEPROM_BLOCK0_END - \
0029                     DS2780_EEPROM_BLOCK0_START + 1)
0030 /* Number of bytes in parameter EEPROM space */
0031 #define DS2780_PARAM_EEPROM_SIZE    (DS2780_EEPROM_BLOCK1_END - \
0032                     DS2780_EEPROM_BLOCK1_START + 1)
0033 
0034 struct ds2780_device_info {
0035     struct device *dev;
0036     struct power_supply *bat;
0037     struct power_supply_desc bat_desc;
0038     struct device *w1_dev;
0039 };
0040 
0041 enum current_types {
0042     CURRENT_NOW,
0043     CURRENT_AVG,
0044 };
0045 
0046 static const char model[] = "DS2780";
0047 static const char manufacturer[] = "Maxim/Dallas";
0048 
0049 static inline struct ds2780_device_info *
0050 to_ds2780_device_info(struct power_supply *psy)
0051 {
0052     return power_supply_get_drvdata(psy);
0053 }
0054 
0055 static inline int ds2780_battery_io(struct ds2780_device_info *dev_info,
0056     char *buf, int addr, size_t count, int io)
0057 {
0058     return w1_ds2780_io(dev_info->w1_dev, buf, addr, count, io);
0059 }
0060 
0061 static inline int ds2780_read8(struct ds2780_device_info *dev_info, u8 *val,
0062     int addr)
0063 {
0064     return ds2780_battery_io(dev_info, val, addr, sizeof(u8), 0);
0065 }
0066 
0067 static int ds2780_read16(struct ds2780_device_info *dev_info, s16 *val,
0068     int addr)
0069 {
0070     int ret;
0071     u8 raw[2];
0072 
0073     ret = ds2780_battery_io(dev_info, raw, addr, sizeof(raw), 0);
0074     if (ret < 0)
0075         return ret;
0076 
0077     *val = (raw[0] << 8) | raw[1];
0078 
0079     return 0;
0080 }
0081 
0082 static inline int ds2780_read_block(struct ds2780_device_info *dev_info,
0083     u8 *val, int addr, size_t count)
0084 {
0085     return ds2780_battery_io(dev_info, val, addr, count, 0);
0086 }
0087 
0088 static inline int ds2780_write(struct ds2780_device_info *dev_info, u8 *val,
0089     int addr, size_t count)
0090 {
0091     return ds2780_battery_io(dev_info, val, addr, count, 1);
0092 }
0093 
0094 static inline int ds2780_store_eeprom(struct device *dev, int addr)
0095 {
0096     return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_COPY_DATA);
0097 }
0098 
0099 static inline int ds2780_recall_eeprom(struct device *dev, int addr)
0100 {
0101     return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_RECALL_DATA);
0102 }
0103 
0104 static int ds2780_save_eeprom(struct ds2780_device_info *dev_info, int reg)
0105 {
0106     int ret;
0107 
0108     ret = ds2780_store_eeprom(dev_info->w1_dev, reg);
0109     if (ret < 0)
0110         return ret;
0111 
0112     ret = ds2780_recall_eeprom(dev_info->w1_dev, reg);
0113     if (ret < 0)
0114         return ret;
0115 
0116     return 0;
0117 }
0118 
0119 /* Set sense resistor value in mhos */
0120 static int ds2780_set_sense_register(struct ds2780_device_info *dev_info,
0121     u8 conductance)
0122 {
0123     int ret;
0124 
0125     ret = ds2780_write(dev_info, &conductance,
0126                 DS2780_RSNSP_REG, sizeof(u8));
0127     if (ret < 0)
0128         return ret;
0129 
0130     return ds2780_save_eeprom(dev_info, DS2780_RSNSP_REG);
0131 }
0132 
0133 /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
0134 static int ds2780_get_rsgain_register(struct ds2780_device_info *dev_info,
0135     u16 *rsgain)
0136 {
0137     return ds2780_read16(dev_info, rsgain, DS2780_RSGAIN_MSB_REG);
0138 }
0139 
0140 /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
0141 static int ds2780_set_rsgain_register(struct ds2780_device_info *dev_info,
0142     u16 rsgain)
0143 {
0144     int ret;
0145     u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
0146 
0147     ret = ds2780_write(dev_info, raw,
0148                 DS2780_RSGAIN_MSB_REG, sizeof(raw));
0149     if (ret < 0)
0150         return ret;
0151 
0152     return ds2780_save_eeprom(dev_info, DS2780_RSGAIN_MSB_REG);
0153 }
0154 
0155 static int ds2780_get_voltage(struct ds2780_device_info *dev_info,
0156     int *voltage_uV)
0157 {
0158     int ret;
0159     s16 voltage_raw;
0160 
0161     /*
0162      * The voltage value is located in 10 bits across the voltage MSB
0163      * and LSB registers in two's complement form
0164      * Sign bit of the voltage value is in bit 7 of the voltage MSB register
0165      * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
0166      * voltage MSB register
0167      * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
0168      * voltage LSB register
0169      */
0170     ret = ds2780_read16(dev_info, &voltage_raw,
0171                 DS2780_VOLT_MSB_REG);
0172     if (ret < 0)
0173         return ret;
0174 
0175     /*
0176      * DS2780 reports voltage in units of 4.88mV, but the battery class
0177      * reports in units of uV, so convert by multiplying by 4880.
0178      */
0179     *voltage_uV = (voltage_raw / 32) * 4880;
0180     return 0;
0181 }
0182 
0183 static int ds2780_get_temperature(struct ds2780_device_info *dev_info,
0184     int *temperature)
0185 {
0186     int ret;
0187     s16 temperature_raw;
0188 
0189     /*
0190      * The temperature value is located in 10 bits across the temperature
0191      * MSB and LSB registers in two's complement form
0192      * Sign bit of the temperature value is in bit 7 of the temperature
0193      * MSB register
0194      * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
0195      * temperature MSB register
0196      * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
0197      * temperature LSB register
0198      */
0199     ret = ds2780_read16(dev_info, &temperature_raw,
0200                 DS2780_TEMP_MSB_REG);
0201     if (ret < 0)
0202         return ret;
0203 
0204     /*
0205      * Temperature is measured in units of 0.125 degrees celcius, the
0206      * power_supply class measures temperature in tenths of degrees
0207      * celsius. The temperature value is stored as a 10 bit number, plus
0208      * sign in the upper bits of a 16 bit register.
0209      */
0210     *temperature = ((temperature_raw / 32) * 125) / 100;
0211     return 0;
0212 }
0213 
0214 static int ds2780_get_current(struct ds2780_device_info *dev_info,
0215     enum current_types type, int *current_uA)
0216 {
0217     int ret, sense_res;
0218     s16 current_raw;
0219     u8 sense_res_raw, reg_msb;
0220 
0221     /*
0222      * The units of measurement for current are dependent on the value of
0223      * the sense resistor.
0224      */
0225     ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
0226     if (ret < 0)
0227         return ret;
0228 
0229     if (sense_res_raw == 0) {
0230         dev_err(dev_info->dev, "sense resistor value is 0\n");
0231         return -EINVAL;
0232     }
0233     sense_res = 1000 / sense_res_raw;
0234 
0235     if (type == CURRENT_NOW)
0236         reg_msb = DS2780_CURRENT_MSB_REG;
0237     else if (type == CURRENT_AVG)
0238         reg_msb = DS2780_IAVG_MSB_REG;
0239     else
0240         return -EINVAL;
0241 
0242     /*
0243      * The current value is located in 16 bits across the current MSB
0244      * and LSB registers in two's complement form
0245      * Sign bit of the current value is in bit 7 of the current MSB register
0246      * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
0247      * MSB register
0248      * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
0249      * LSB register
0250      */
0251     ret = ds2780_read16(dev_info, &current_raw, reg_msb);
0252     if (ret < 0)
0253         return ret;
0254 
0255     *current_uA = current_raw * (DS2780_CURRENT_UNITS / sense_res);
0256     return 0;
0257 }
0258 
0259 static int ds2780_get_accumulated_current(struct ds2780_device_info *dev_info,
0260     int *accumulated_current)
0261 {
0262     int ret, sense_res;
0263     s16 current_raw;
0264     u8 sense_res_raw;
0265 
0266     /*
0267      * The units of measurement for accumulated current are dependent on
0268      * the value of the sense resistor.
0269      */
0270     ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
0271     if (ret < 0)
0272         return ret;
0273 
0274     if (sense_res_raw == 0) {
0275         dev_err(dev_info->dev, "sense resistor value is 0\n");
0276         return -ENXIO;
0277     }
0278     sense_res = 1000 / sense_res_raw;
0279 
0280     /*
0281      * The ACR value is located in 16 bits across the ACR MSB and
0282      * LSB registers
0283      * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
0284      * MSB register
0285      * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
0286      * LSB register
0287      */
0288     ret = ds2780_read16(dev_info, &current_raw, DS2780_ACR_MSB_REG);
0289     if (ret < 0)
0290         return ret;
0291 
0292     *accumulated_current = current_raw * (DS2780_CHARGE_UNITS / sense_res);
0293     return 0;
0294 }
0295 
0296 static int ds2780_get_capacity(struct ds2780_device_info *dev_info,
0297     int *capacity)
0298 {
0299     int ret;
0300     u8 raw;
0301 
0302     ret = ds2780_read8(dev_info, &raw, DS2780_RARC_REG);
0303     if (ret < 0)
0304         return ret;
0305 
0306     *capacity = raw;
0307     return raw;
0308 }
0309 
0310 static int ds2780_get_status(struct ds2780_device_info *dev_info, int *status)
0311 {
0312     int ret, current_uA, capacity;
0313 
0314     ret = ds2780_get_current(dev_info, CURRENT_NOW, &current_uA);
0315     if (ret < 0)
0316         return ret;
0317 
0318     ret = ds2780_get_capacity(dev_info, &capacity);
0319     if (ret < 0)
0320         return ret;
0321 
0322     if (capacity == 100)
0323         *status = POWER_SUPPLY_STATUS_FULL;
0324     else if (current_uA == 0)
0325         *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
0326     else if (current_uA < 0)
0327         *status = POWER_SUPPLY_STATUS_DISCHARGING;
0328     else
0329         *status = POWER_SUPPLY_STATUS_CHARGING;
0330 
0331     return 0;
0332 }
0333 
0334 static int ds2780_get_charge_now(struct ds2780_device_info *dev_info,
0335     int *charge_now)
0336 {
0337     int ret;
0338     u16 charge_raw;
0339 
0340     /*
0341      * The RAAC value is located in 16 bits across the RAAC MSB and
0342      * LSB registers
0343      * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
0344      * MSB register
0345      * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
0346      * LSB register
0347      */
0348     ret = ds2780_read16(dev_info, &charge_raw, DS2780_RAAC_MSB_REG);
0349     if (ret < 0)
0350         return ret;
0351 
0352     *charge_now = charge_raw * 1600;
0353     return 0;
0354 }
0355 
0356 static int ds2780_get_control_register(struct ds2780_device_info *dev_info,
0357     u8 *control_reg)
0358 {
0359     return ds2780_read8(dev_info, control_reg, DS2780_CONTROL_REG);
0360 }
0361 
0362 static int ds2780_set_control_register(struct ds2780_device_info *dev_info,
0363     u8 control_reg)
0364 {
0365     int ret;
0366 
0367     ret = ds2780_write(dev_info, &control_reg,
0368                 DS2780_CONTROL_REG, sizeof(u8));
0369     if (ret < 0)
0370         return ret;
0371 
0372     return ds2780_save_eeprom(dev_info, DS2780_CONTROL_REG);
0373 }
0374 
0375 static int ds2780_battery_get_property(struct power_supply *psy,
0376     enum power_supply_property psp,
0377     union power_supply_propval *val)
0378 {
0379     int ret = 0;
0380     struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
0381 
0382     switch (psp) {
0383     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0384         ret = ds2780_get_voltage(dev_info, &val->intval);
0385         break;
0386 
0387     case POWER_SUPPLY_PROP_TEMP:
0388         ret = ds2780_get_temperature(dev_info, &val->intval);
0389         break;
0390 
0391     case POWER_SUPPLY_PROP_MODEL_NAME:
0392         val->strval = model;
0393         break;
0394 
0395     case POWER_SUPPLY_PROP_MANUFACTURER:
0396         val->strval = manufacturer;
0397         break;
0398 
0399     case POWER_SUPPLY_PROP_CURRENT_NOW:
0400         ret = ds2780_get_current(dev_info, CURRENT_NOW, &val->intval);
0401         break;
0402 
0403     case POWER_SUPPLY_PROP_CURRENT_AVG:
0404         ret = ds2780_get_current(dev_info, CURRENT_AVG, &val->intval);
0405         break;
0406 
0407     case POWER_SUPPLY_PROP_STATUS:
0408         ret = ds2780_get_status(dev_info, &val->intval);
0409         break;
0410 
0411     case POWER_SUPPLY_PROP_CAPACITY:
0412         ret = ds2780_get_capacity(dev_info, &val->intval);
0413         break;
0414 
0415     case POWER_SUPPLY_PROP_CHARGE_COUNTER:
0416         ret = ds2780_get_accumulated_current(dev_info, &val->intval);
0417         break;
0418 
0419     case POWER_SUPPLY_PROP_CHARGE_NOW:
0420         ret = ds2780_get_charge_now(dev_info, &val->intval);
0421         break;
0422 
0423     default:
0424         ret = -EINVAL;
0425     }
0426 
0427     return ret;
0428 }
0429 
0430 static enum power_supply_property ds2780_battery_props[] = {
0431     POWER_SUPPLY_PROP_STATUS,
0432     POWER_SUPPLY_PROP_VOLTAGE_NOW,
0433     POWER_SUPPLY_PROP_TEMP,
0434     POWER_SUPPLY_PROP_MODEL_NAME,
0435     POWER_SUPPLY_PROP_MANUFACTURER,
0436     POWER_SUPPLY_PROP_CURRENT_NOW,
0437     POWER_SUPPLY_PROP_CURRENT_AVG,
0438     POWER_SUPPLY_PROP_CAPACITY,
0439     POWER_SUPPLY_PROP_CHARGE_COUNTER,
0440     POWER_SUPPLY_PROP_CHARGE_NOW,
0441 };
0442 
0443 static ssize_t ds2780_get_pmod_enabled(struct device *dev,
0444     struct device_attribute *attr,
0445     char *buf)
0446 {
0447     int ret;
0448     u8 control_reg;
0449     struct power_supply *psy = to_power_supply(dev);
0450     struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
0451 
0452     /* Get power mode */
0453     ret = ds2780_get_control_register(dev_info, &control_reg);
0454     if (ret < 0)
0455         return ret;
0456 
0457     return sprintf(buf, "%d\n",
0458          !!(control_reg & DS2780_CONTROL_REG_PMOD));
0459 }
0460 
0461 static ssize_t ds2780_set_pmod_enabled(struct device *dev,
0462     struct device_attribute *attr,
0463     const char *buf,
0464     size_t count)
0465 {
0466     int ret;
0467     u8 control_reg, new_setting;
0468     struct power_supply *psy = to_power_supply(dev);
0469     struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
0470 
0471     /* Set power mode */
0472     ret = ds2780_get_control_register(dev_info, &control_reg);
0473     if (ret < 0)
0474         return ret;
0475 
0476     ret = kstrtou8(buf, 0, &new_setting);
0477     if (ret < 0)
0478         return ret;
0479 
0480     if ((new_setting != 0) && (new_setting != 1)) {
0481         dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
0482         return -EINVAL;
0483     }
0484 
0485     if (new_setting)
0486         control_reg |= DS2780_CONTROL_REG_PMOD;
0487     else
0488         control_reg &= ~DS2780_CONTROL_REG_PMOD;
0489 
0490     ret = ds2780_set_control_register(dev_info, control_reg);
0491     if (ret < 0)
0492         return ret;
0493 
0494     return count;
0495 }
0496 
0497 static ssize_t ds2780_get_sense_resistor_value(struct device *dev,
0498     struct device_attribute *attr,
0499     char *buf)
0500 {
0501     int ret;
0502     u8 sense_resistor;
0503     struct power_supply *psy = to_power_supply(dev);
0504     struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
0505 
0506     ret = ds2780_read8(dev_info, &sense_resistor, DS2780_RSNSP_REG);
0507     if (ret < 0)
0508         return ret;
0509 
0510     ret = sprintf(buf, "%d\n", sense_resistor);
0511     return ret;
0512 }
0513 
0514 static ssize_t ds2780_set_sense_resistor_value(struct device *dev,
0515     struct device_attribute *attr,
0516     const char *buf,
0517     size_t count)
0518 {
0519     int ret;
0520     u8 new_setting;
0521     struct power_supply *psy = to_power_supply(dev);
0522     struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
0523 
0524     ret = kstrtou8(buf, 0, &new_setting);
0525     if (ret < 0)
0526         return ret;
0527 
0528     ret = ds2780_set_sense_register(dev_info, new_setting);
0529     if (ret < 0)
0530         return ret;
0531 
0532     return count;
0533 }
0534 
0535 static ssize_t ds2780_get_rsgain_setting(struct device *dev,
0536     struct device_attribute *attr,
0537     char *buf)
0538 {
0539     int ret;
0540     u16 rsgain;
0541     struct power_supply *psy = to_power_supply(dev);
0542     struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
0543 
0544     ret = ds2780_get_rsgain_register(dev_info, &rsgain);
0545     if (ret < 0)
0546         return ret;
0547 
0548     return sprintf(buf, "%d\n", rsgain);
0549 }
0550 
0551 static ssize_t ds2780_set_rsgain_setting(struct device *dev,
0552     struct device_attribute *attr,
0553     const char *buf,
0554     size_t count)
0555 {
0556     int ret;
0557     u16 new_setting;
0558     struct power_supply *psy = to_power_supply(dev);
0559     struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
0560 
0561     ret = kstrtou16(buf, 0, &new_setting);
0562     if (ret < 0)
0563         return ret;
0564 
0565     /* Gain can only be from 0 to 1.999 in steps of .001 */
0566     if (new_setting > 1999) {
0567         dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
0568         return -EINVAL;
0569     }
0570 
0571     ret = ds2780_set_rsgain_register(dev_info, new_setting);
0572     if (ret < 0)
0573         return ret;
0574 
0575     return count;
0576 }
0577 
0578 static ssize_t ds2780_get_pio_pin(struct device *dev,
0579     struct device_attribute *attr,
0580     char *buf)
0581 {
0582     int ret;
0583     u8 sfr;
0584     struct power_supply *psy = to_power_supply(dev);
0585     struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
0586 
0587     ret = ds2780_read8(dev_info, &sfr, DS2780_SFR_REG);
0588     if (ret < 0)
0589         return ret;
0590 
0591     ret = sprintf(buf, "%d\n", sfr & DS2780_SFR_REG_PIOSC);
0592     return ret;
0593 }
0594 
0595 static ssize_t ds2780_set_pio_pin(struct device *dev,
0596     struct device_attribute *attr,
0597     const char *buf,
0598     size_t count)
0599 {
0600     int ret;
0601     u8 new_setting;
0602     struct power_supply *psy = to_power_supply(dev);
0603     struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
0604 
0605     ret = kstrtou8(buf, 0, &new_setting);
0606     if (ret < 0)
0607         return ret;
0608 
0609     if ((new_setting != 0) && (new_setting != 1)) {
0610         dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
0611         return -EINVAL;
0612     }
0613 
0614     ret = ds2780_write(dev_info, &new_setting,
0615                 DS2780_SFR_REG, sizeof(u8));
0616     if (ret < 0)
0617         return ret;
0618 
0619     return count;
0620 }
0621 
0622 static ssize_t ds2780_read_param_eeprom_bin(struct file *filp,
0623                 struct kobject *kobj,
0624                 struct bin_attribute *bin_attr,
0625                 char *buf, loff_t off, size_t count)
0626 {
0627     struct device *dev = kobj_to_dev(kobj);
0628     struct power_supply *psy = to_power_supply(dev);
0629     struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
0630 
0631     return ds2780_read_block(dev_info, buf,
0632                 DS2780_EEPROM_BLOCK1_START + off, count);
0633 }
0634 
0635 static ssize_t ds2780_write_param_eeprom_bin(struct file *filp,
0636                 struct kobject *kobj,
0637                 struct bin_attribute *bin_attr,
0638                 char *buf, loff_t off, size_t count)
0639 {
0640     struct device *dev = kobj_to_dev(kobj);
0641     struct power_supply *psy = to_power_supply(dev);
0642     struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
0643     int ret;
0644 
0645     ret = ds2780_write(dev_info, buf,
0646                 DS2780_EEPROM_BLOCK1_START + off, count);
0647     if (ret < 0)
0648         return ret;
0649 
0650     ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK1_START);
0651     if (ret < 0)
0652         return ret;
0653 
0654     return count;
0655 }
0656 
0657 static struct bin_attribute ds2780_param_eeprom_bin_attr = {
0658     .attr = {
0659         .name = "param_eeprom",
0660         .mode = S_IRUGO | S_IWUSR,
0661     },
0662     .size = DS2780_PARAM_EEPROM_SIZE,
0663     .read = ds2780_read_param_eeprom_bin,
0664     .write = ds2780_write_param_eeprom_bin,
0665 };
0666 
0667 static ssize_t ds2780_read_user_eeprom_bin(struct file *filp,
0668                 struct kobject *kobj,
0669                 struct bin_attribute *bin_attr,
0670                 char *buf, loff_t off, size_t count)
0671 {
0672     struct device *dev = kobj_to_dev(kobj);
0673     struct power_supply *psy = to_power_supply(dev);
0674     struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
0675 
0676     return ds2780_read_block(dev_info, buf,
0677                 DS2780_EEPROM_BLOCK0_START + off, count);
0678 }
0679 
0680 static ssize_t ds2780_write_user_eeprom_bin(struct file *filp,
0681                 struct kobject *kobj,
0682                 struct bin_attribute *bin_attr,
0683                 char *buf, loff_t off, size_t count)
0684 {
0685     struct device *dev = kobj_to_dev(kobj);
0686     struct power_supply *psy = to_power_supply(dev);
0687     struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
0688     int ret;
0689 
0690     ret = ds2780_write(dev_info, buf,
0691                 DS2780_EEPROM_BLOCK0_START + off, count);
0692     if (ret < 0)
0693         return ret;
0694 
0695     ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK0_START);
0696     if (ret < 0)
0697         return ret;
0698 
0699     return count;
0700 }
0701 
0702 static struct bin_attribute ds2780_user_eeprom_bin_attr = {
0703     .attr = {
0704         .name = "user_eeprom",
0705         .mode = S_IRUGO | S_IWUSR,
0706     },
0707     .size = DS2780_USER_EEPROM_SIZE,
0708     .read = ds2780_read_user_eeprom_bin,
0709     .write = ds2780_write_user_eeprom_bin,
0710 };
0711 
0712 static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2780_get_pmod_enabled,
0713     ds2780_set_pmod_enabled);
0714 static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
0715     ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value);
0716 static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2780_get_rsgain_setting,
0717     ds2780_set_rsgain_setting);
0718 static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2780_get_pio_pin,
0719     ds2780_set_pio_pin);
0720 
0721 static struct attribute *ds2780_sysfs_attrs[] = {
0722     &dev_attr_pmod_enabled.attr,
0723     &dev_attr_sense_resistor_value.attr,
0724     &dev_attr_rsgain_setting.attr,
0725     &dev_attr_pio_pin.attr,
0726     NULL
0727 };
0728 
0729 static struct bin_attribute *ds2780_sysfs_bin_attrs[] = {
0730     &ds2780_param_eeprom_bin_attr,
0731     &ds2780_user_eeprom_bin_attr,
0732     NULL
0733 };
0734 
0735 static const struct attribute_group ds2780_sysfs_group = {
0736     .attrs = ds2780_sysfs_attrs,
0737     .bin_attrs = ds2780_sysfs_bin_attrs,
0738 };
0739 
0740 static const struct attribute_group *ds2780_sysfs_groups[] = {
0741     &ds2780_sysfs_group,
0742     NULL,
0743 };
0744 
0745 static int ds2780_battery_probe(struct platform_device *pdev)
0746 {
0747     struct power_supply_config psy_cfg = {};
0748     struct ds2780_device_info *dev_info;
0749 
0750     dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
0751     if (!dev_info)
0752         return -ENOMEM;
0753 
0754     platform_set_drvdata(pdev, dev_info);
0755 
0756     dev_info->dev           = &pdev->dev;
0757     dev_info->w1_dev        = pdev->dev.parent;
0758     dev_info->bat_desc.name     = dev_name(&pdev->dev);
0759     dev_info->bat_desc.type     = POWER_SUPPLY_TYPE_BATTERY;
0760     dev_info->bat_desc.properties   = ds2780_battery_props;
0761     dev_info->bat_desc.num_properties = ARRAY_SIZE(ds2780_battery_props);
0762     dev_info->bat_desc.get_property = ds2780_battery_get_property;
0763 
0764     psy_cfg.drv_data        = dev_info;
0765     psy_cfg.attr_grp        = ds2780_sysfs_groups;
0766 
0767     dev_info->bat = devm_power_supply_register(&pdev->dev,
0768                            &dev_info->bat_desc,
0769                            &psy_cfg);
0770     if (IS_ERR(dev_info->bat)) {
0771         dev_err(dev_info->dev, "failed to register battery\n");
0772         return PTR_ERR(dev_info->bat);
0773     }
0774 
0775     return 0;
0776 }
0777 
0778 static struct platform_driver ds2780_battery_driver = {
0779     .driver = {
0780         .name = "ds2780-battery",
0781     },
0782     .probe    = ds2780_battery_probe,
0783 };
0784 
0785 module_platform_driver(ds2780_battery_driver);
0786 
0787 MODULE_LICENSE("GPL");
0788 MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
0789 MODULE_DESCRIPTION("Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC driver");
0790 MODULE_ALIAS("platform:ds2780-battery");