Back to home page

OSCL-LXR

 
 

    


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