1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28c0984e5SSebastian Reichel /*
38c0984e5SSebastian Reichel * 1-wire client/driver for the Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC
48c0984e5SSebastian Reichel *
58c0984e5SSebastian Reichel * Author: Renata Sayakhova <renata@oktetlabs.ru>
68c0984e5SSebastian Reichel *
78c0984e5SSebastian Reichel * Based on ds2780_battery drivers
88c0984e5SSebastian Reichel */
98c0984e5SSebastian Reichel
108c0984e5SSebastian Reichel #include <linux/module.h>
118c0984e5SSebastian Reichel #include <linux/slab.h>
128c0984e5SSebastian Reichel #include <linux/param.h>
138c0984e5SSebastian Reichel #include <linux/pm.h>
148c0984e5SSebastian Reichel #include <linux/platform_device.h>
158c0984e5SSebastian Reichel #include <linux/power_supply.h>
168c0984e5SSebastian Reichel #include <linux/idr.h>
178c0984e5SSebastian Reichel
18de0d6dbdSAndrew F. Davis #include <linux/w1.h>
198c0984e5SSebastian Reichel #include "../../w1/slaves/w1_ds2781.h"
208c0984e5SSebastian Reichel
218c0984e5SSebastian Reichel /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
228c0984e5SSebastian Reichel #define DS2781_CURRENT_UNITS 1563
238c0984e5SSebastian Reichel /* Charge unit measurement in uAh for a 1 milli-ohm sense resistor */
248c0984e5SSebastian Reichel #define DS2781_CHARGE_UNITS 6250
258c0984e5SSebastian Reichel /* Number of bytes in user EEPROM space */
268c0984e5SSebastian Reichel #define DS2781_USER_EEPROM_SIZE (DS2781_EEPROM_BLOCK0_END - \
278c0984e5SSebastian Reichel DS2781_EEPROM_BLOCK0_START + 1)
288c0984e5SSebastian Reichel /* Number of bytes in parameter EEPROM space */
298c0984e5SSebastian Reichel #define DS2781_PARAM_EEPROM_SIZE (DS2781_EEPROM_BLOCK1_END - \
308c0984e5SSebastian Reichel DS2781_EEPROM_BLOCK1_START + 1)
318c0984e5SSebastian Reichel
328c0984e5SSebastian Reichel struct ds2781_device_info {
338c0984e5SSebastian Reichel struct device *dev;
348c0984e5SSebastian Reichel struct power_supply *bat;
358c0984e5SSebastian Reichel struct power_supply_desc bat_desc;
368c0984e5SSebastian Reichel struct device *w1_dev;
378c0984e5SSebastian Reichel };
388c0984e5SSebastian Reichel
398c0984e5SSebastian Reichel enum current_types {
408c0984e5SSebastian Reichel CURRENT_NOW,
418c0984e5SSebastian Reichel CURRENT_AVG,
428c0984e5SSebastian Reichel };
438c0984e5SSebastian Reichel
448c0984e5SSebastian Reichel static const char model[] = "DS2781";
458c0984e5SSebastian Reichel static const char manufacturer[] = "Maxim/Dallas";
468c0984e5SSebastian Reichel
478c0984e5SSebastian Reichel static inline struct ds2781_device_info *
to_ds2781_device_info(struct power_supply * psy)488c0984e5SSebastian Reichel to_ds2781_device_info(struct power_supply *psy)
498c0984e5SSebastian Reichel {
508c0984e5SSebastian Reichel return power_supply_get_drvdata(psy);
518c0984e5SSebastian Reichel }
528c0984e5SSebastian Reichel
ds2781_battery_io(struct ds2781_device_info * dev_info,char * buf,int addr,size_t count,int io)538c0984e5SSebastian Reichel static inline int ds2781_battery_io(struct ds2781_device_info *dev_info,
548c0984e5SSebastian Reichel char *buf, int addr, size_t count, int io)
558c0984e5SSebastian Reichel {
568c0984e5SSebastian Reichel return w1_ds2781_io(dev_info->w1_dev, buf, addr, count, io);
578c0984e5SSebastian Reichel }
588c0984e5SSebastian Reichel
w1_ds2781_read(struct ds2781_device_info * dev_info,char * buf,int addr,size_t count)598c0984e5SSebastian Reichel static int w1_ds2781_read(struct ds2781_device_info *dev_info, char *buf,
608c0984e5SSebastian Reichel int addr, size_t count)
618c0984e5SSebastian Reichel {
628c0984e5SSebastian Reichel return ds2781_battery_io(dev_info, buf, addr, count, 0);
638c0984e5SSebastian Reichel }
648c0984e5SSebastian Reichel
ds2781_read8(struct ds2781_device_info * dev_info,u8 * val,int addr)658c0984e5SSebastian Reichel static inline int ds2781_read8(struct ds2781_device_info *dev_info, u8 *val,
668c0984e5SSebastian Reichel int addr)
678c0984e5SSebastian Reichel {
688c0984e5SSebastian Reichel return ds2781_battery_io(dev_info, val, addr, sizeof(u8), 0);
698c0984e5SSebastian Reichel }
708c0984e5SSebastian Reichel
ds2781_read16(struct ds2781_device_info * dev_info,s16 * val,int addr)718c0984e5SSebastian Reichel static int ds2781_read16(struct ds2781_device_info *dev_info, s16 *val,
728c0984e5SSebastian Reichel int addr)
738c0984e5SSebastian Reichel {
748c0984e5SSebastian Reichel int ret;
758c0984e5SSebastian Reichel u8 raw[2];
768c0984e5SSebastian Reichel
778c0984e5SSebastian Reichel ret = ds2781_battery_io(dev_info, raw, addr, sizeof(raw), 0);
788c0984e5SSebastian Reichel if (ret < 0)
798c0984e5SSebastian Reichel return ret;
808c0984e5SSebastian Reichel
818c0984e5SSebastian Reichel *val = (raw[0] << 8) | raw[1];
828c0984e5SSebastian Reichel
838c0984e5SSebastian Reichel return 0;
848c0984e5SSebastian Reichel }
858c0984e5SSebastian Reichel
ds2781_read_block(struct ds2781_device_info * dev_info,u8 * val,int addr,size_t count)868c0984e5SSebastian Reichel static inline int ds2781_read_block(struct ds2781_device_info *dev_info,
878c0984e5SSebastian Reichel u8 *val, int addr, size_t count)
888c0984e5SSebastian Reichel {
898c0984e5SSebastian Reichel return ds2781_battery_io(dev_info, val, addr, count, 0);
908c0984e5SSebastian Reichel }
918c0984e5SSebastian Reichel
ds2781_write(struct ds2781_device_info * dev_info,u8 * val,int addr,size_t count)928c0984e5SSebastian Reichel static inline int ds2781_write(struct ds2781_device_info *dev_info, u8 *val,
938c0984e5SSebastian Reichel int addr, size_t count)
948c0984e5SSebastian Reichel {
958c0984e5SSebastian Reichel return ds2781_battery_io(dev_info, val, addr, count, 1);
968c0984e5SSebastian Reichel }
978c0984e5SSebastian Reichel
ds2781_store_eeprom(struct device * dev,int addr)988c0984e5SSebastian Reichel static inline int ds2781_store_eeprom(struct device *dev, int addr)
998c0984e5SSebastian Reichel {
1008c0984e5SSebastian Reichel return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_COPY_DATA);
1018c0984e5SSebastian Reichel }
1028c0984e5SSebastian Reichel
ds2781_recall_eeprom(struct device * dev,int addr)1038c0984e5SSebastian Reichel static inline int ds2781_recall_eeprom(struct device *dev, int addr)
1048c0984e5SSebastian Reichel {
1058c0984e5SSebastian Reichel return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_RECALL_DATA);
1068c0984e5SSebastian Reichel }
1078c0984e5SSebastian Reichel
ds2781_save_eeprom(struct ds2781_device_info * dev_info,int reg)1088c0984e5SSebastian Reichel static int ds2781_save_eeprom(struct ds2781_device_info *dev_info, int reg)
1098c0984e5SSebastian Reichel {
1108c0984e5SSebastian Reichel int ret;
1118c0984e5SSebastian Reichel
1128c0984e5SSebastian Reichel ret = ds2781_store_eeprom(dev_info->w1_dev, reg);
1138c0984e5SSebastian Reichel if (ret < 0)
1148c0984e5SSebastian Reichel return ret;
1158c0984e5SSebastian Reichel
1168c0984e5SSebastian Reichel ret = ds2781_recall_eeprom(dev_info->w1_dev, reg);
1178c0984e5SSebastian Reichel if (ret < 0)
1188c0984e5SSebastian Reichel return ret;
1198c0984e5SSebastian Reichel
1208c0984e5SSebastian Reichel return 0;
1218c0984e5SSebastian Reichel }
1228c0984e5SSebastian Reichel
1238c0984e5SSebastian Reichel /* Set sense resistor value in mhos */
ds2781_set_sense_register(struct ds2781_device_info * dev_info,u8 conductance)1248c0984e5SSebastian Reichel static int ds2781_set_sense_register(struct ds2781_device_info *dev_info,
1258c0984e5SSebastian Reichel u8 conductance)
1268c0984e5SSebastian Reichel {
1278c0984e5SSebastian Reichel int ret;
1288c0984e5SSebastian Reichel
1298c0984e5SSebastian Reichel ret = ds2781_write(dev_info, &conductance,
1308c0984e5SSebastian Reichel DS2781_RSNSP, sizeof(u8));
1318c0984e5SSebastian Reichel if (ret < 0)
1328c0984e5SSebastian Reichel return ret;
1338c0984e5SSebastian Reichel
1348c0984e5SSebastian Reichel return ds2781_save_eeprom(dev_info, DS2781_RSNSP);
1358c0984e5SSebastian Reichel }
1368c0984e5SSebastian Reichel
1378c0984e5SSebastian Reichel /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
ds2781_get_rsgain_register(struct ds2781_device_info * dev_info,u16 * rsgain)1388c0984e5SSebastian Reichel static int ds2781_get_rsgain_register(struct ds2781_device_info *dev_info,
1398c0984e5SSebastian Reichel u16 *rsgain)
1408c0984e5SSebastian Reichel {
1418c0984e5SSebastian Reichel return ds2781_read16(dev_info, rsgain, DS2781_RSGAIN_MSB);
1428c0984e5SSebastian Reichel }
1438c0984e5SSebastian Reichel
1448c0984e5SSebastian Reichel /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
ds2781_set_rsgain_register(struct ds2781_device_info * dev_info,u16 rsgain)1458c0984e5SSebastian Reichel static int ds2781_set_rsgain_register(struct ds2781_device_info *dev_info,
1468c0984e5SSebastian Reichel u16 rsgain)
1478c0984e5SSebastian Reichel {
1488c0984e5SSebastian Reichel int ret;
1498c0984e5SSebastian Reichel u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
1508c0984e5SSebastian Reichel
1518c0984e5SSebastian Reichel ret = ds2781_write(dev_info, raw,
1528c0984e5SSebastian Reichel DS2781_RSGAIN_MSB, sizeof(raw));
1538c0984e5SSebastian Reichel if (ret < 0)
1548c0984e5SSebastian Reichel return ret;
1558c0984e5SSebastian Reichel
1568c0984e5SSebastian Reichel return ds2781_save_eeprom(dev_info, DS2781_RSGAIN_MSB);
1578c0984e5SSebastian Reichel }
1588c0984e5SSebastian Reichel
ds2781_get_voltage(struct ds2781_device_info * dev_info,int * voltage_uV)1598c0984e5SSebastian Reichel static int ds2781_get_voltage(struct ds2781_device_info *dev_info,
1608c0984e5SSebastian Reichel int *voltage_uV)
1618c0984e5SSebastian Reichel {
1628c0984e5SSebastian Reichel int ret;
1638c0984e5SSebastian Reichel char val[2];
1648c0984e5SSebastian Reichel int voltage_raw;
1658c0984e5SSebastian Reichel
1668c0984e5SSebastian Reichel ret = w1_ds2781_read(dev_info, val, DS2781_VOLT_MSB, 2 * sizeof(u8));
1678c0984e5SSebastian Reichel if (ret < 0)
1688c0984e5SSebastian Reichel return ret;
1698c0984e5SSebastian Reichel /*
1708c0984e5SSebastian Reichel * The voltage value is located in 10 bits across the voltage MSB
1713f41e742SWang Qing * and LSB registers in two's complement form
1728c0984e5SSebastian Reichel * Sign bit of the voltage value is in bit 7 of the voltage MSB register
1738c0984e5SSebastian Reichel * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
1748c0984e5SSebastian Reichel * voltage MSB register
1758c0984e5SSebastian Reichel * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
1768c0984e5SSebastian Reichel * voltage LSB register
1778c0984e5SSebastian Reichel */
1788c0984e5SSebastian Reichel voltage_raw = (val[0] << 3) |
1798c0984e5SSebastian Reichel (val[1] >> 5);
1808c0984e5SSebastian Reichel
1818c0984e5SSebastian Reichel /* DS2781 reports voltage in units of 9.76mV, but the battery class
1828c0984e5SSebastian Reichel * reports in units of uV, so convert by multiplying by 9760. */
1838c0984e5SSebastian Reichel *voltage_uV = voltage_raw * 9760;
1848c0984e5SSebastian Reichel
1858c0984e5SSebastian Reichel return 0;
1868c0984e5SSebastian Reichel }
1878c0984e5SSebastian Reichel
ds2781_get_temperature(struct ds2781_device_info * dev_info,int * temp)1888c0984e5SSebastian Reichel static int ds2781_get_temperature(struct ds2781_device_info *dev_info,
1898c0984e5SSebastian Reichel int *temp)
1908c0984e5SSebastian Reichel {
1918c0984e5SSebastian Reichel int ret;
1928c0984e5SSebastian Reichel char val[2];
1938c0984e5SSebastian Reichel int temp_raw;
1948c0984e5SSebastian Reichel
1958c0984e5SSebastian Reichel ret = w1_ds2781_read(dev_info, val, DS2781_TEMP_MSB, 2 * sizeof(u8));
1968c0984e5SSebastian Reichel if (ret < 0)
1978c0984e5SSebastian Reichel return ret;
1988c0984e5SSebastian Reichel /*
1998c0984e5SSebastian Reichel * The temperature value is located in 10 bits across the temperature
2003f41e742SWang Qing * MSB and LSB registers in two's complement form
2018c0984e5SSebastian Reichel * Sign bit of the temperature value is in bit 7 of the temperature
2028c0984e5SSebastian Reichel * MSB register
2038c0984e5SSebastian Reichel * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
2048c0984e5SSebastian Reichel * temperature MSB register
2058c0984e5SSebastian Reichel * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
2068c0984e5SSebastian Reichel * temperature LSB register
2078c0984e5SSebastian Reichel */
2088c0984e5SSebastian Reichel temp_raw = ((val[0]) << 3) |
2098c0984e5SSebastian Reichel (val[1] >> 5);
2108c0984e5SSebastian Reichel *temp = temp_raw + (temp_raw / 4);
2118c0984e5SSebastian Reichel
2128c0984e5SSebastian Reichel return 0;
2138c0984e5SSebastian Reichel }
2148c0984e5SSebastian Reichel
ds2781_get_current(struct ds2781_device_info * dev_info,enum current_types type,int * current_uA)2158c0984e5SSebastian Reichel static int ds2781_get_current(struct ds2781_device_info *dev_info,
2168c0984e5SSebastian Reichel enum current_types type, int *current_uA)
2178c0984e5SSebastian Reichel {
2188c0984e5SSebastian Reichel int ret, sense_res;
2198c0984e5SSebastian Reichel s16 current_raw;
2208c0984e5SSebastian Reichel u8 sense_res_raw, reg_msb;
2218c0984e5SSebastian Reichel
2228c0984e5SSebastian Reichel /*
2238c0984e5SSebastian Reichel * The units of measurement for current are dependent on the value of
2248c0984e5SSebastian Reichel * the sense resistor.
2258c0984e5SSebastian Reichel */
2268c0984e5SSebastian Reichel ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
2278c0984e5SSebastian Reichel if (ret < 0)
2288c0984e5SSebastian Reichel return ret;
2298c0984e5SSebastian Reichel
2308c0984e5SSebastian Reichel if (sense_res_raw == 0) {
2318c0984e5SSebastian Reichel dev_err(dev_info->dev, "sense resistor value is 0\n");
2328c0984e5SSebastian Reichel return -EINVAL;
2338c0984e5SSebastian Reichel }
2348c0984e5SSebastian Reichel sense_res = 1000 / sense_res_raw;
2358c0984e5SSebastian Reichel
2368c0984e5SSebastian Reichel if (type == CURRENT_NOW)
2378c0984e5SSebastian Reichel reg_msb = DS2781_CURRENT_MSB;
2388c0984e5SSebastian Reichel else if (type == CURRENT_AVG)
2398c0984e5SSebastian Reichel reg_msb = DS2781_IAVG_MSB;
2408c0984e5SSebastian Reichel else
2418c0984e5SSebastian Reichel return -EINVAL;
2428c0984e5SSebastian Reichel
2438c0984e5SSebastian Reichel /*
2448c0984e5SSebastian Reichel * The current value is located in 16 bits across the current MSB
2453f41e742SWang Qing * and LSB registers in two's complement form
2468c0984e5SSebastian Reichel * Sign bit of the current value is in bit 7 of the current MSB register
2478c0984e5SSebastian Reichel * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
2488c0984e5SSebastian Reichel * MSB register
2498c0984e5SSebastian Reichel * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
2508c0984e5SSebastian Reichel * LSB register
2518c0984e5SSebastian Reichel */
2528c0984e5SSebastian Reichel ret = ds2781_read16(dev_info, ¤t_raw, reg_msb);
2538c0984e5SSebastian Reichel if (ret < 0)
2548c0984e5SSebastian Reichel return ret;
2558c0984e5SSebastian Reichel
2568c0984e5SSebastian Reichel *current_uA = current_raw * (DS2781_CURRENT_UNITS / sense_res);
2578c0984e5SSebastian Reichel return 0;
2588c0984e5SSebastian Reichel }
2598c0984e5SSebastian Reichel
ds2781_get_accumulated_current(struct ds2781_device_info * dev_info,int * accumulated_current)2608c0984e5SSebastian Reichel static int ds2781_get_accumulated_current(struct ds2781_device_info *dev_info,
2618c0984e5SSebastian Reichel int *accumulated_current)
2628c0984e5SSebastian Reichel {
2638c0984e5SSebastian Reichel int ret, sense_res;
2648c0984e5SSebastian Reichel s16 current_raw;
2658c0984e5SSebastian Reichel u8 sense_res_raw;
2668c0984e5SSebastian Reichel
2678c0984e5SSebastian Reichel /*
2688c0984e5SSebastian Reichel * The units of measurement for accumulated current are dependent on
2698c0984e5SSebastian Reichel * the value of the sense resistor.
2708c0984e5SSebastian Reichel */
2718c0984e5SSebastian Reichel ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
2728c0984e5SSebastian Reichel if (ret < 0)
2738c0984e5SSebastian Reichel return ret;
2748c0984e5SSebastian Reichel
2758c0984e5SSebastian Reichel if (sense_res_raw == 0) {
2768c0984e5SSebastian Reichel dev_err(dev_info->dev, "sense resistor value is 0\n");
2778c0984e5SSebastian Reichel return -EINVAL;
2788c0984e5SSebastian Reichel }
2798c0984e5SSebastian Reichel sense_res = 1000 / sense_res_raw;
2808c0984e5SSebastian Reichel
2818c0984e5SSebastian Reichel /*
2828c0984e5SSebastian Reichel * The ACR value is located in 16 bits across the ACR MSB and
2838c0984e5SSebastian Reichel * LSB registers
2848c0984e5SSebastian Reichel * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
2858c0984e5SSebastian Reichel * MSB register
2868c0984e5SSebastian Reichel * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
2878c0984e5SSebastian Reichel * LSB register
2888c0984e5SSebastian Reichel */
2898c0984e5SSebastian Reichel ret = ds2781_read16(dev_info, ¤t_raw, DS2781_ACR_MSB);
2908c0984e5SSebastian Reichel if (ret < 0)
2918c0984e5SSebastian Reichel return ret;
2928c0984e5SSebastian Reichel
2938c0984e5SSebastian Reichel *accumulated_current = current_raw * (DS2781_CHARGE_UNITS / sense_res);
2948c0984e5SSebastian Reichel return 0;
2958c0984e5SSebastian Reichel }
2968c0984e5SSebastian Reichel
ds2781_get_capacity(struct ds2781_device_info * dev_info,int * capacity)2978c0984e5SSebastian Reichel static int ds2781_get_capacity(struct ds2781_device_info *dev_info,
2988c0984e5SSebastian Reichel int *capacity)
2998c0984e5SSebastian Reichel {
3008c0984e5SSebastian Reichel int ret;
3018c0984e5SSebastian Reichel u8 raw;
3028c0984e5SSebastian Reichel
3038c0984e5SSebastian Reichel ret = ds2781_read8(dev_info, &raw, DS2781_RARC);
3048c0984e5SSebastian Reichel if (ret < 0)
3058c0984e5SSebastian Reichel return ret;
3068c0984e5SSebastian Reichel
3078c0984e5SSebastian Reichel *capacity = raw;
3088c0984e5SSebastian Reichel return 0;
3098c0984e5SSebastian Reichel }
3108c0984e5SSebastian Reichel
ds2781_get_status(struct ds2781_device_info * dev_info,int * status)3118c0984e5SSebastian Reichel static int ds2781_get_status(struct ds2781_device_info *dev_info, int *status)
3128c0984e5SSebastian Reichel {
3138c0984e5SSebastian Reichel int ret, current_uA, capacity;
3148c0984e5SSebastian Reichel
3158c0984e5SSebastian Reichel ret = ds2781_get_current(dev_info, CURRENT_NOW, ¤t_uA);
3168c0984e5SSebastian Reichel if (ret < 0)
3178c0984e5SSebastian Reichel return ret;
3188c0984e5SSebastian Reichel
3198c0984e5SSebastian Reichel ret = ds2781_get_capacity(dev_info, &capacity);
3208c0984e5SSebastian Reichel if (ret < 0)
3218c0984e5SSebastian Reichel return ret;
3228c0984e5SSebastian Reichel
3238c0984e5SSebastian Reichel if (power_supply_am_i_supplied(dev_info->bat)) {
3248c0984e5SSebastian Reichel if (capacity == 100)
3258c0984e5SSebastian Reichel *status = POWER_SUPPLY_STATUS_FULL;
3268c0984e5SSebastian Reichel else if (current_uA > 50000)
3278c0984e5SSebastian Reichel *status = POWER_SUPPLY_STATUS_CHARGING;
3288c0984e5SSebastian Reichel else
3298c0984e5SSebastian Reichel *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
3308c0984e5SSebastian Reichel } else {
3318c0984e5SSebastian Reichel *status = POWER_SUPPLY_STATUS_DISCHARGING;
3328c0984e5SSebastian Reichel }
3338c0984e5SSebastian Reichel return 0;
3348c0984e5SSebastian Reichel }
3358c0984e5SSebastian Reichel
ds2781_get_charge_now(struct ds2781_device_info * dev_info,int * charge_now)3368c0984e5SSebastian Reichel static int ds2781_get_charge_now(struct ds2781_device_info *dev_info,
3378c0984e5SSebastian Reichel int *charge_now)
3388c0984e5SSebastian Reichel {
3398c0984e5SSebastian Reichel int ret;
3408c0984e5SSebastian Reichel u16 charge_raw;
3418c0984e5SSebastian Reichel
3428c0984e5SSebastian Reichel /*
3438c0984e5SSebastian Reichel * The RAAC value is located in 16 bits across the RAAC MSB and
3448c0984e5SSebastian Reichel * LSB registers
3458c0984e5SSebastian Reichel * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
3468c0984e5SSebastian Reichel * MSB register
3478c0984e5SSebastian Reichel * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
3488c0984e5SSebastian Reichel * LSB register
3498c0984e5SSebastian Reichel */
3508c0984e5SSebastian Reichel ret = ds2781_read16(dev_info, &charge_raw, DS2781_RAAC_MSB);
3518c0984e5SSebastian Reichel if (ret < 0)
3528c0984e5SSebastian Reichel return ret;
3538c0984e5SSebastian Reichel
3548c0984e5SSebastian Reichel *charge_now = charge_raw * 1600;
3558c0984e5SSebastian Reichel return 0;
3568c0984e5SSebastian Reichel }
3578c0984e5SSebastian Reichel
ds2781_get_control_register(struct ds2781_device_info * dev_info,u8 * control_reg)3588c0984e5SSebastian Reichel static int ds2781_get_control_register(struct ds2781_device_info *dev_info,
3598c0984e5SSebastian Reichel u8 *control_reg)
3608c0984e5SSebastian Reichel {
3618c0984e5SSebastian Reichel return ds2781_read8(dev_info, control_reg, DS2781_CONTROL);
3628c0984e5SSebastian Reichel }
3638c0984e5SSebastian Reichel
ds2781_set_control_register(struct ds2781_device_info * dev_info,u8 control_reg)3648c0984e5SSebastian Reichel static int ds2781_set_control_register(struct ds2781_device_info *dev_info,
3658c0984e5SSebastian Reichel u8 control_reg)
3668c0984e5SSebastian Reichel {
3678c0984e5SSebastian Reichel int ret;
3688c0984e5SSebastian Reichel
3698c0984e5SSebastian Reichel ret = ds2781_write(dev_info, &control_reg,
3708c0984e5SSebastian Reichel DS2781_CONTROL, sizeof(u8));
3718c0984e5SSebastian Reichel if (ret < 0)
3728c0984e5SSebastian Reichel return ret;
3738c0984e5SSebastian Reichel
3748c0984e5SSebastian Reichel return ds2781_save_eeprom(dev_info, DS2781_CONTROL);
3758c0984e5SSebastian Reichel }
3768c0984e5SSebastian Reichel
ds2781_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)3778c0984e5SSebastian Reichel static int ds2781_battery_get_property(struct power_supply *psy,
3788c0984e5SSebastian Reichel enum power_supply_property psp,
3798c0984e5SSebastian Reichel union power_supply_propval *val)
3808c0984e5SSebastian Reichel {
3818c0984e5SSebastian Reichel int ret = 0;
3828c0984e5SSebastian Reichel struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
3838c0984e5SSebastian Reichel
3848c0984e5SSebastian Reichel switch (psp) {
3858c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_VOLTAGE_NOW:
3868c0984e5SSebastian Reichel ret = ds2781_get_voltage(dev_info, &val->intval);
3878c0984e5SSebastian Reichel break;
3888c0984e5SSebastian Reichel
3898c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_TEMP:
3908c0984e5SSebastian Reichel ret = ds2781_get_temperature(dev_info, &val->intval);
3918c0984e5SSebastian Reichel break;
3928c0984e5SSebastian Reichel
3938c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_MODEL_NAME:
3948c0984e5SSebastian Reichel val->strval = model;
3958c0984e5SSebastian Reichel break;
3968c0984e5SSebastian Reichel
3978c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_MANUFACTURER:
3988c0984e5SSebastian Reichel val->strval = manufacturer;
3998c0984e5SSebastian Reichel break;
4008c0984e5SSebastian Reichel
4018c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CURRENT_NOW:
4028c0984e5SSebastian Reichel ret = ds2781_get_current(dev_info, CURRENT_NOW, &val->intval);
4038c0984e5SSebastian Reichel break;
4048c0984e5SSebastian Reichel
4058c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CURRENT_AVG:
4068c0984e5SSebastian Reichel ret = ds2781_get_current(dev_info, CURRENT_AVG, &val->intval);
4078c0984e5SSebastian Reichel break;
4088c0984e5SSebastian Reichel
4098c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_STATUS:
4108c0984e5SSebastian Reichel ret = ds2781_get_status(dev_info, &val->intval);
4118c0984e5SSebastian Reichel break;
4128c0984e5SSebastian Reichel
4138c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CAPACITY:
4148c0984e5SSebastian Reichel ret = ds2781_get_capacity(dev_info, &val->intval);
4158c0984e5SSebastian Reichel break;
4168c0984e5SSebastian Reichel
4178c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_COUNTER:
4188c0984e5SSebastian Reichel ret = ds2781_get_accumulated_current(dev_info, &val->intval);
4198c0984e5SSebastian Reichel break;
4208c0984e5SSebastian Reichel
4218c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CHARGE_NOW:
4228c0984e5SSebastian Reichel ret = ds2781_get_charge_now(dev_info, &val->intval);
4238c0984e5SSebastian Reichel break;
4248c0984e5SSebastian Reichel
4258c0984e5SSebastian Reichel default:
4268c0984e5SSebastian Reichel ret = -EINVAL;
4278c0984e5SSebastian Reichel }
4288c0984e5SSebastian Reichel
4298c0984e5SSebastian Reichel return ret;
4308c0984e5SSebastian Reichel }
4318c0984e5SSebastian Reichel
4328c0984e5SSebastian Reichel static enum power_supply_property ds2781_battery_props[] = {
4338c0984e5SSebastian Reichel POWER_SUPPLY_PROP_STATUS,
4348c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW,
4358c0984e5SSebastian Reichel POWER_SUPPLY_PROP_TEMP,
4368c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MODEL_NAME,
4378c0984e5SSebastian Reichel POWER_SUPPLY_PROP_MANUFACTURER,
4388c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CURRENT_NOW,
4398c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CURRENT_AVG,
4408c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY,
4418c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_COUNTER,
4428c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CHARGE_NOW,
4438c0984e5SSebastian Reichel };
4448c0984e5SSebastian Reichel
ds2781_get_pmod_enabled(struct device * dev,struct device_attribute * attr,char * buf)4458c0984e5SSebastian Reichel static ssize_t ds2781_get_pmod_enabled(struct device *dev,
4468c0984e5SSebastian Reichel struct device_attribute *attr,
4478c0984e5SSebastian Reichel char *buf)
4488c0984e5SSebastian Reichel {
4498c0984e5SSebastian Reichel int ret;
4508c0984e5SSebastian Reichel u8 control_reg;
4518c0984e5SSebastian Reichel struct power_supply *psy = to_power_supply(dev);
4528c0984e5SSebastian Reichel struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
4538c0984e5SSebastian Reichel
4548c0984e5SSebastian Reichel /* Get power mode */
4558c0984e5SSebastian Reichel ret = ds2781_get_control_register(dev_info, &control_reg);
4568c0984e5SSebastian Reichel if (ret < 0)
4578c0984e5SSebastian Reichel return ret;
4588c0984e5SSebastian Reichel
459*a441f3b9Sye xingchen return sysfs_emit(buf, "%d\n",
4608c0984e5SSebastian Reichel !!(control_reg & DS2781_CONTROL_PMOD));
4618c0984e5SSebastian Reichel }
4628c0984e5SSebastian Reichel
ds2781_set_pmod_enabled(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4638c0984e5SSebastian Reichel static ssize_t ds2781_set_pmod_enabled(struct device *dev,
4648c0984e5SSebastian Reichel struct device_attribute *attr,
4658c0984e5SSebastian Reichel const char *buf,
4668c0984e5SSebastian Reichel size_t count)
4678c0984e5SSebastian Reichel {
4688c0984e5SSebastian Reichel int ret;
4698c0984e5SSebastian Reichel u8 control_reg, new_setting;
4708c0984e5SSebastian Reichel struct power_supply *psy = to_power_supply(dev);
4718c0984e5SSebastian Reichel struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
4728c0984e5SSebastian Reichel
4738c0984e5SSebastian Reichel /* Set power mode */
4748c0984e5SSebastian Reichel ret = ds2781_get_control_register(dev_info, &control_reg);
4758c0984e5SSebastian Reichel if (ret < 0)
4768c0984e5SSebastian Reichel return ret;
4778c0984e5SSebastian Reichel
4788c0984e5SSebastian Reichel ret = kstrtou8(buf, 0, &new_setting);
4798c0984e5SSebastian Reichel if (ret < 0)
4808c0984e5SSebastian Reichel return ret;
4818c0984e5SSebastian Reichel
4828c0984e5SSebastian Reichel if ((new_setting != 0) && (new_setting != 1)) {
4838c0984e5SSebastian Reichel dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
4848c0984e5SSebastian Reichel return -EINVAL;
4858c0984e5SSebastian Reichel }
4868c0984e5SSebastian Reichel
4878c0984e5SSebastian Reichel if (new_setting)
4888c0984e5SSebastian Reichel control_reg |= DS2781_CONTROL_PMOD;
4898c0984e5SSebastian Reichel else
4908c0984e5SSebastian Reichel control_reg &= ~DS2781_CONTROL_PMOD;
4918c0984e5SSebastian Reichel
4928c0984e5SSebastian Reichel ret = ds2781_set_control_register(dev_info, control_reg);
4938c0984e5SSebastian Reichel if (ret < 0)
4948c0984e5SSebastian Reichel return ret;
4958c0984e5SSebastian Reichel
4968c0984e5SSebastian Reichel return count;
4978c0984e5SSebastian Reichel }
4988c0984e5SSebastian Reichel
ds2781_get_sense_resistor_value(struct device * dev,struct device_attribute * attr,char * buf)4998c0984e5SSebastian Reichel static ssize_t ds2781_get_sense_resistor_value(struct device *dev,
5008c0984e5SSebastian Reichel struct device_attribute *attr,
5018c0984e5SSebastian Reichel char *buf)
5028c0984e5SSebastian Reichel {
5038c0984e5SSebastian Reichel int ret;
5048c0984e5SSebastian Reichel u8 sense_resistor;
5058c0984e5SSebastian Reichel struct power_supply *psy = to_power_supply(dev);
5068c0984e5SSebastian Reichel struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
5078c0984e5SSebastian Reichel
5088c0984e5SSebastian Reichel ret = ds2781_read8(dev_info, &sense_resistor, DS2781_RSNSP);
5098c0984e5SSebastian Reichel if (ret < 0)
5108c0984e5SSebastian Reichel return ret;
5118c0984e5SSebastian Reichel
512*a441f3b9Sye xingchen ret = sysfs_emit(buf, "%d\n", sense_resistor);
5138c0984e5SSebastian Reichel return ret;
5148c0984e5SSebastian Reichel }
5158c0984e5SSebastian Reichel
ds2781_set_sense_resistor_value(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)5168c0984e5SSebastian Reichel static ssize_t ds2781_set_sense_resistor_value(struct device *dev,
5178c0984e5SSebastian Reichel struct device_attribute *attr,
5188c0984e5SSebastian Reichel const char *buf,
5198c0984e5SSebastian Reichel size_t count)
5208c0984e5SSebastian Reichel {
5218c0984e5SSebastian Reichel int ret;
5228c0984e5SSebastian Reichel u8 new_setting;
5238c0984e5SSebastian Reichel struct power_supply *psy = to_power_supply(dev);
5248c0984e5SSebastian Reichel struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
5258c0984e5SSebastian Reichel
5268c0984e5SSebastian Reichel ret = kstrtou8(buf, 0, &new_setting);
5278c0984e5SSebastian Reichel if (ret < 0)
5288c0984e5SSebastian Reichel return ret;
5298c0984e5SSebastian Reichel
5308c0984e5SSebastian Reichel ret = ds2781_set_sense_register(dev_info, new_setting);
5318c0984e5SSebastian Reichel if (ret < 0)
5328c0984e5SSebastian Reichel return ret;
5338c0984e5SSebastian Reichel
5348c0984e5SSebastian Reichel return count;
5358c0984e5SSebastian Reichel }
5368c0984e5SSebastian Reichel
ds2781_get_rsgain_setting(struct device * dev,struct device_attribute * attr,char * buf)5378c0984e5SSebastian Reichel static ssize_t ds2781_get_rsgain_setting(struct device *dev,
5388c0984e5SSebastian Reichel struct device_attribute *attr,
5398c0984e5SSebastian Reichel char *buf)
5408c0984e5SSebastian Reichel {
5418c0984e5SSebastian Reichel int ret;
5428c0984e5SSebastian Reichel u16 rsgain;
5438c0984e5SSebastian Reichel struct power_supply *psy = to_power_supply(dev);
5448c0984e5SSebastian Reichel struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
5458c0984e5SSebastian Reichel
5468c0984e5SSebastian Reichel ret = ds2781_get_rsgain_register(dev_info, &rsgain);
5478c0984e5SSebastian Reichel if (ret < 0)
5488c0984e5SSebastian Reichel return ret;
5498c0984e5SSebastian Reichel
550*a441f3b9Sye xingchen return sysfs_emit(buf, "%d\n", rsgain);
5518c0984e5SSebastian Reichel }
5528c0984e5SSebastian Reichel
ds2781_set_rsgain_setting(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)5538c0984e5SSebastian Reichel static ssize_t ds2781_set_rsgain_setting(struct device *dev,
5548c0984e5SSebastian Reichel struct device_attribute *attr,
5558c0984e5SSebastian Reichel const char *buf,
5568c0984e5SSebastian Reichel size_t count)
5578c0984e5SSebastian Reichel {
5588c0984e5SSebastian Reichel int ret;
5598c0984e5SSebastian Reichel u16 new_setting;
5608c0984e5SSebastian Reichel struct power_supply *psy = to_power_supply(dev);
5618c0984e5SSebastian Reichel struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
5628c0984e5SSebastian Reichel
5638c0984e5SSebastian Reichel ret = kstrtou16(buf, 0, &new_setting);
5648c0984e5SSebastian Reichel if (ret < 0)
5658c0984e5SSebastian Reichel return ret;
5668c0984e5SSebastian Reichel
5678c0984e5SSebastian Reichel /* Gain can only be from 0 to 1.999 in steps of .001 */
5688c0984e5SSebastian Reichel if (new_setting > 1999) {
5698c0984e5SSebastian Reichel dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
5708c0984e5SSebastian Reichel return -EINVAL;
5718c0984e5SSebastian Reichel }
5728c0984e5SSebastian Reichel
5738c0984e5SSebastian Reichel ret = ds2781_set_rsgain_register(dev_info, new_setting);
5748c0984e5SSebastian Reichel if (ret < 0)
5758c0984e5SSebastian Reichel return ret;
5768c0984e5SSebastian Reichel
5778c0984e5SSebastian Reichel return count;
5788c0984e5SSebastian Reichel }
5798c0984e5SSebastian Reichel
ds2781_get_pio_pin(struct device * dev,struct device_attribute * attr,char * buf)5808c0984e5SSebastian Reichel static ssize_t ds2781_get_pio_pin(struct device *dev,
5818c0984e5SSebastian Reichel struct device_attribute *attr,
5828c0984e5SSebastian Reichel char *buf)
5838c0984e5SSebastian Reichel {
5848c0984e5SSebastian Reichel int ret;
5858c0984e5SSebastian Reichel u8 sfr;
5868c0984e5SSebastian Reichel struct power_supply *psy = to_power_supply(dev);
5878c0984e5SSebastian Reichel struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
5888c0984e5SSebastian Reichel
5898c0984e5SSebastian Reichel ret = ds2781_read8(dev_info, &sfr, DS2781_SFR);
5908c0984e5SSebastian Reichel if (ret < 0)
5918c0984e5SSebastian Reichel return ret;
5928c0984e5SSebastian Reichel
593*a441f3b9Sye xingchen ret = sysfs_emit(buf, "%d\n", sfr & DS2781_SFR_PIOSC);
5948c0984e5SSebastian Reichel return ret;
5958c0984e5SSebastian Reichel }
5968c0984e5SSebastian Reichel
ds2781_set_pio_pin(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)5978c0984e5SSebastian Reichel static ssize_t ds2781_set_pio_pin(struct device *dev,
5988c0984e5SSebastian Reichel struct device_attribute *attr,
5998c0984e5SSebastian Reichel const char *buf,
6008c0984e5SSebastian Reichel size_t count)
6018c0984e5SSebastian Reichel {
6028c0984e5SSebastian Reichel int ret;
6038c0984e5SSebastian Reichel u8 new_setting;
6048c0984e5SSebastian Reichel struct power_supply *psy = to_power_supply(dev);
6058c0984e5SSebastian Reichel struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
6068c0984e5SSebastian Reichel
6078c0984e5SSebastian Reichel ret = kstrtou8(buf, 0, &new_setting);
6088c0984e5SSebastian Reichel if (ret < 0)
6098c0984e5SSebastian Reichel return ret;
6108c0984e5SSebastian Reichel
6118c0984e5SSebastian Reichel if ((new_setting != 0) && (new_setting != 1)) {
6128c0984e5SSebastian Reichel dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
6138c0984e5SSebastian Reichel return -EINVAL;
6148c0984e5SSebastian Reichel }
6158c0984e5SSebastian Reichel
6168c0984e5SSebastian Reichel ret = ds2781_write(dev_info, &new_setting,
6178c0984e5SSebastian Reichel DS2781_SFR, sizeof(u8));
6188c0984e5SSebastian Reichel if (ret < 0)
6198c0984e5SSebastian Reichel return ret;
6208c0984e5SSebastian Reichel
6218c0984e5SSebastian Reichel return count;
6228c0984e5SSebastian Reichel }
6238c0984e5SSebastian Reichel
ds2781_read_param_eeprom_bin(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)6248c0984e5SSebastian Reichel static ssize_t ds2781_read_param_eeprom_bin(struct file *filp,
6258c0984e5SSebastian Reichel struct kobject *kobj,
6268c0984e5SSebastian Reichel struct bin_attribute *bin_attr,
6278c0984e5SSebastian Reichel char *buf, loff_t off, size_t count)
6288c0984e5SSebastian Reichel {
629c77b26e3Sdongjian struct device *dev = kobj_to_dev(kobj);
6308c0984e5SSebastian Reichel struct power_supply *psy = to_power_supply(dev);
6318c0984e5SSebastian Reichel struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
6328c0984e5SSebastian Reichel
6338c0984e5SSebastian Reichel return ds2781_read_block(dev_info, buf,
6348c0984e5SSebastian Reichel DS2781_EEPROM_BLOCK1_START + off, count);
6358c0984e5SSebastian Reichel }
6368c0984e5SSebastian Reichel
ds2781_write_param_eeprom_bin(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)6378c0984e5SSebastian Reichel static ssize_t ds2781_write_param_eeprom_bin(struct file *filp,
6388c0984e5SSebastian Reichel struct kobject *kobj,
6398c0984e5SSebastian Reichel struct bin_attribute *bin_attr,
6408c0984e5SSebastian Reichel char *buf, loff_t off, size_t count)
6418c0984e5SSebastian Reichel {
642c77b26e3Sdongjian struct device *dev = kobj_to_dev(kobj);
6438c0984e5SSebastian Reichel struct power_supply *psy = to_power_supply(dev);
6448c0984e5SSebastian Reichel struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
6458c0984e5SSebastian Reichel int ret;
6468c0984e5SSebastian Reichel
6478c0984e5SSebastian Reichel ret = ds2781_write(dev_info, buf,
6488c0984e5SSebastian Reichel DS2781_EEPROM_BLOCK1_START + off, count);
6498c0984e5SSebastian Reichel if (ret < 0)
6508c0984e5SSebastian Reichel return ret;
6518c0984e5SSebastian Reichel
6528c0984e5SSebastian Reichel ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK1_START);
6538c0984e5SSebastian Reichel if (ret < 0)
6548c0984e5SSebastian Reichel return ret;
6558c0984e5SSebastian Reichel
6568c0984e5SSebastian Reichel return count;
6578c0984e5SSebastian Reichel }
6588c0984e5SSebastian Reichel
659711aebcfSSebastian Reichel static struct bin_attribute ds2781_param_eeprom_bin_attr = {
6608c0984e5SSebastian Reichel .attr = {
6618c0984e5SSebastian Reichel .name = "param_eeprom",
6628c0984e5SSebastian Reichel .mode = S_IRUGO | S_IWUSR,
6638c0984e5SSebastian Reichel },
6648c0984e5SSebastian Reichel .size = DS2781_PARAM_EEPROM_SIZE,
6658c0984e5SSebastian Reichel .read = ds2781_read_param_eeprom_bin,
6668c0984e5SSebastian Reichel .write = ds2781_write_param_eeprom_bin,
6678c0984e5SSebastian Reichel };
6688c0984e5SSebastian Reichel
ds2781_read_user_eeprom_bin(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)6698c0984e5SSebastian Reichel static ssize_t ds2781_read_user_eeprom_bin(struct file *filp,
6708c0984e5SSebastian Reichel struct kobject *kobj,
6718c0984e5SSebastian Reichel struct bin_attribute *bin_attr,
6728c0984e5SSebastian Reichel char *buf, loff_t off, size_t count)
6738c0984e5SSebastian Reichel {
674c77b26e3Sdongjian struct device *dev = kobj_to_dev(kobj);
6758c0984e5SSebastian Reichel struct power_supply *psy = to_power_supply(dev);
6768c0984e5SSebastian Reichel struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
6778c0984e5SSebastian Reichel
6788c0984e5SSebastian Reichel return ds2781_read_block(dev_info, buf,
6798c0984e5SSebastian Reichel DS2781_EEPROM_BLOCK0_START + off, count);
6808c0984e5SSebastian Reichel
6818c0984e5SSebastian Reichel }
6828c0984e5SSebastian Reichel
ds2781_write_user_eeprom_bin(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)6838c0984e5SSebastian Reichel static ssize_t ds2781_write_user_eeprom_bin(struct file *filp,
6848c0984e5SSebastian Reichel struct kobject *kobj,
6858c0984e5SSebastian Reichel struct bin_attribute *bin_attr,
6868c0984e5SSebastian Reichel char *buf, loff_t off, size_t count)
6878c0984e5SSebastian Reichel {
688c77b26e3Sdongjian struct device *dev = kobj_to_dev(kobj);
6898c0984e5SSebastian Reichel struct power_supply *psy = to_power_supply(dev);
6908c0984e5SSebastian Reichel struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
6918c0984e5SSebastian Reichel int ret;
6928c0984e5SSebastian Reichel
6938c0984e5SSebastian Reichel ret = ds2781_write(dev_info, buf,
6948c0984e5SSebastian Reichel DS2781_EEPROM_BLOCK0_START + off, count);
6958c0984e5SSebastian Reichel if (ret < 0)
6968c0984e5SSebastian Reichel return ret;
6978c0984e5SSebastian Reichel
6988c0984e5SSebastian Reichel ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK0_START);
6998c0984e5SSebastian Reichel if (ret < 0)
7008c0984e5SSebastian Reichel return ret;
7018c0984e5SSebastian Reichel
7028c0984e5SSebastian Reichel return count;
7038c0984e5SSebastian Reichel }
7048c0984e5SSebastian Reichel
705711aebcfSSebastian Reichel static struct bin_attribute ds2781_user_eeprom_bin_attr = {
7068c0984e5SSebastian Reichel .attr = {
7078c0984e5SSebastian Reichel .name = "user_eeprom",
7088c0984e5SSebastian Reichel .mode = S_IRUGO | S_IWUSR,
7098c0984e5SSebastian Reichel },
7108c0984e5SSebastian Reichel .size = DS2781_USER_EEPROM_SIZE,
7118c0984e5SSebastian Reichel .read = ds2781_read_user_eeprom_bin,
7128c0984e5SSebastian Reichel .write = ds2781_write_user_eeprom_bin,
7138c0984e5SSebastian Reichel };
7148c0984e5SSebastian Reichel
7158c0984e5SSebastian Reichel static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2781_get_pmod_enabled,
7168c0984e5SSebastian Reichel ds2781_set_pmod_enabled);
7178c0984e5SSebastian Reichel static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
7188c0984e5SSebastian Reichel ds2781_get_sense_resistor_value, ds2781_set_sense_resistor_value);
7198c0984e5SSebastian Reichel static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2781_get_rsgain_setting,
7208c0984e5SSebastian Reichel ds2781_set_rsgain_setting);
7218c0984e5SSebastian Reichel static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2781_get_pio_pin,
7228c0984e5SSebastian Reichel ds2781_set_pio_pin);
7238c0984e5SSebastian Reichel
724a45cefb0SSebastian Reichel static struct attribute *ds2781_sysfs_attrs[] = {
7258c0984e5SSebastian Reichel &dev_attr_pmod_enabled.attr,
7268c0984e5SSebastian Reichel &dev_attr_sense_resistor_value.attr,
7278c0984e5SSebastian Reichel &dev_attr_rsgain_setting.attr,
7288c0984e5SSebastian Reichel &dev_attr_pio_pin.attr,
7298c0984e5SSebastian Reichel NULL
7308c0984e5SSebastian Reichel };
7318c0984e5SSebastian Reichel
732711aebcfSSebastian Reichel static struct bin_attribute *ds2781_sysfs_bin_attrs[] = {
733711aebcfSSebastian Reichel &ds2781_param_eeprom_bin_attr,
734711aebcfSSebastian Reichel &ds2781_user_eeprom_bin_attr,
735711aebcfSSebastian Reichel NULL,
736711aebcfSSebastian Reichel };
737711aebcfSSebastian Reichel
738711aebcfSSebastian Reichel static const struct attribute_group ds2781_sysfs_group = {
739711aebcfSSebastian Reichel .attrs = ds2781_sysfs_attrs,
740711aebcfSSebastian Reichel .bin_attrs = ds2781_sysfs_bin_attrs,
741711aebcfSSebastian Reichel
742711aebcfSSebastian Reichel };
743711aebcfSSebastian Reichel
744711aebcfSSebastian Reichel static const struct attribute_group *ds2781_sysfs_groups[] = {
745711aebcfSSebastian Reichel &ds2781_sysfs_group,
746711aebcfSSebastian Reichel NULL,
747711aebcfSSebastian Reichel };
7488c0984e5SSebastian Reichel
ds2781_battery_probe(struct platform_device * pdev)7498c0984e5SSebastian Reichel static int ds2781_battery_probe(struct platform_device *pdev)
7508c0984e5SSebastian Reichel {
7518c0984e5SSebastian Reichel struct power_supply_config psy_cfg = {};
7528c0984e5SSebastian Reichel struct ds2781_device_info *dev_info;
7538c0984e5SSebastian Reichel
7548c0984e5SSebastian Reichel dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
7558c0984e5SSebastian Reichel if (!dev_info)
7568c0984e5SSebastian Reichel return -ENOMEM;
7578c0984e5SSebastian Reichel
7588c0984e5SSebastian Reichel platform_set_drvdata(pdev, dev_info);
7598c0984e5SSebastian Reichel
7608c0984e5SSebastian Reichel dev_info->dev = &pdev->dev;
7618c0984e5SSebastian Reichel dev_info->w1_dev = pdev->dev.parent;
7628c0984e5SSebastian Reichel dev_info->bat_desc.name = dev_name(&pdev->dev);
7638c0984e5SSebastian Reichel dev_info->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
7648c0984e5SSebastian Reichel dev_info->bat_desc.properties = ds2781_battery_props;
7658c0984e5SSebastian Reichel dev_info->bat_desc.num_properties = ARRAY_SIZE(ds2781_battery_props);
7668c0984e5SSebastian Reichel dev_info->bat_desc.get_property = ds2781_battery_get_property;
7678c0984e5SSebastian Reichel
7688c0984e5SSebastian Reichel psy_cfg.drv_data = dev_info;
769a45cefb0SSebastian Reichel psy_cfg.attr_grp = ds2781_sysfs_groups;
7708c0984e5SSebastian Reichel
771fbd17e58SSebastian Reichel dev_info->bat = devm_power_supply_register(&pdev->dev,
772fbd17e58SSebastian Reichel &dev_info->bat_desc,
7738c0984e5SSebastian Reichel &psy_cfg);
7748c0984e5SSebastian Reichel if (IS_ERR(dev_info->bat)) {
7758c0984e5SSebastian Reichel dev_err(dev_info->dev, "failed to register battery\n");
776711aebcfSSebastian Reichel return PTR_ERR(dev_info->bat);
7778c0984e5SSebastian Reichel }
7788c0984e5SSebastian Reichel
7798c0984e5SSebastian Reichel return 0;
7808c0984e5SSebastian Reichel }
7818c0984e5SSebastian Reichel
7828c0984e5SSebastian Reichel static struct platform_driver ds2781_battery_driver = {
7838c0984e5SSebastian Reichel .driver = {
7848c0984e5SSebastian Reichel .name = "ds2781-battery",
7858c0984e5SSebastian Reichel },
7868c0984e5SSebastian Reichel .probe = ds2781_battery_probe,
7878c0984e5SSebastian Reichel };
7888c0984e5SSebastian Reichel module_platform_driver(ds2781_battery_driver);
7898c0984e5SSebastian Reichel
7908c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
7918c0984e5SSebastian Reichel MODULE_AUTHOR("Renata Sayakhova <renata@oktetlabs.ru>");
792415d602bSColin Ian King MODULE_DESCRIPTION("Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC driver");
7938c0984e5SSebastian Reichel MODULE_ALIAS("platform:ds2781-battery");
7948c0984e5SSebastian Reichel
795