xref: /openbmc/linux/drivers/power/supply/ds2780_battery.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28c0984e5SSebastian Reichel /*
38c0984e5SSebastian Reichel  * 1-wire client/driver for the Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC
48c0984e5SSebastian Reichel  *
58c0984e5SSebastian Reichel  * Copyright (C) 2010 Indesign, LLC
68c0984e5SSebastian Reichel  *
78c0984e5SSebastian Reichel  * Author: Clifton Barnes <cabarnes@indesign-llc.com>
88c0984e5SSebastian Reichel  *
98c0984e5SSebastian Reichel  * Based on ds2760_battery and ds2782_battery drivers
108c0984e5SSebastian Reichel  */
118c0984e5SSebastian Reichel 
128c0984e5SSebastian Reichel #include <linux/module.h>
138c0984e5SSebastian Reichel #include <linux/slab.h>
148c0984e5SSebastian Reichel #include <linux/param.h>
158c0984e5SSebastian Reichel #include <linux/pm.h>
168c0984e5SSebastian Reichel #include <linux/platform_device.h>
178c0984e5SSebastian Reichel #include <linux/power_supply.h>
188c0984e5SSebastian Reichel #include <linux/idr.h>
198c0984e5SSebastian Reichel 
20de0d6dbdSAndrew F. Davis #include <linux/w1.h>
218c0984e5SSebastian Reichel #include "../../w1/slaves/w1_ds2780.h"
228c0984e5SSebastian Reichel 
238c0984e5SSebastian Reichel /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
248c0984e5SSebastian Reichel #define DS2780_CURRENT_UNITS	1563
258c0984e5SSebastian Reichel /* Charge unit measurement in uAh for a 1 milli-ohm sense resistor */
268c0984e5SSebastian Reichel #define DS2780_CHARGE_UNITS		6250
278c0984e5SSebastian Reichel /* Number of bytes in user EEPROM space */
288c0984e5SSebastian Reichel #define DS2780_USER_EEPROM_SIZE		(DS2780_EEPROM_BLOCK0_END - \
298c0984e5SSebastian Reichel 					DS2780_EEPROM_BLOCK0_START + 1)
308c0984e5SSebastian Reichel /* Number of bytes in parameter EEPROM space */
318c0984e5SSebastian Reichel #define DS2780_PARAM_EEPROM_SIZE	(DS2780_EEPROM_BLOCK1_END - \
328c0984e5SSebastian Reichel 					DS2780_EEPROM_BLOCK1_START + 1)
338c0984e5SSebastian Reichel 
348c0984e5SSebastian Reichel struct ds2780_device_info {
358c0984e5SSebastian Reichel 	struct device *dev;
368c0984e5SSebastian Reichel 	struct power_supply *bat;
378c0984e5SSebastian Reichel 	struct power_supply_desc bat_desc;
388c0984e5SSebastian Reichel 	struct device *w1_dev;
398c0984e5SSebastian Reichel };
408c0984e5SSebastian Reichel 
418c0984e5SSebastian Reichel enum current_types {
428c0984e5SSebastian Reichel 	CURRENT_NOW,
438c0984e5SSebastian Reichel 	CURRENT_AVG,
448c0984e5SSebastian Reichel };
458c0984e5SSebastian Reichel 
468c0984e5SSebastian Reichel static const char model[] = "DS2780";
478c0984e5SSebastian Reichel static const char manufacturer[] = "Maxim/Dallas";
488c0984e5SSebastian Reichel 
498c0984e5SSebastian Reichel static inline struct ds2780_device_info *
to_ds2780_device_info(struct power_supply * psy)508c0984e5SSebastian Reichel to_ds2780_device_info(struct power_supply *psy)
518c0984e5SSebastian Reichel {
528c0984e5SSebastian Reichel 	return power_supply_get_drvdata(psy);
538c0984e5SSebastian Reichel }
548c0984e5SSebastian Reichel 
ds2780_battery_io(struct ds2780_device_info * dev_info,char * buf,int addr,size_t count,int io)558c0984e5SSebastian Reichel static inline int ds2780_battery_io(struct ds2780_device_info *dev_info,
568c0984e5SSebastian Reichel 	char *buf, int addr, size_t count, int io)
578c0984e5SSebastian Reichel {
588c0984e5SSebastian Reichel 	return w1_ds2780_io(dev_info->w1_dev, buf, addr, count, io);
598c0984e5SSebastian Reichel }
608c0984e5SSebastian Reichel 
ds2780_read8(struct ds2780_device_info * dev_info,u8 * val,int addr)618c0984e5SSebastian Reichel static inline int ds2780_read8(struct ds2780_device_info *dev_info, u8 *val,
628c0984e5SSebastian Reichel 	int addr)
638c0984e5SSebastian Reichel {
648c0984e5SSebastian Reichel 	return ds2780_battery_io(dev_info, val, addr, sizeof(u8), 0);
658c0984e5SSebastian Reichel }
668c0984e5SSebastian Reichel 
ds2780_read16(struct ds2780_device_info * dev_info,s16 * val,int addr)678c0984e5SSebastian Reichel static int ds2780_read16(struct ds2780_device_info *dev_info, s16 *val,
688c0984e5SSebastian Reichel 	int addr)
698c0984e5SSebastian Reichel {
708c0984e5SSebastian Reichel 	int ret;
718c0984e5SSebastian Reichel 	u8 raw[2];
728c0984e5SSebastian Reichel 
738c0984e5SSebastian Reichel 	ret = ds2780_battery_io(dev_info, raw, addr, sizeof(raw), 0);
748c0984e5SSebastian Reichel 	if (ret < 0)
758c0984e5SSebastian Reichel 		return ret;
768c0984e5SSebastian Reichel 
778c0984e5SSebastian Reichel 	*val = (raw[0] << 8) | raw[1];
788c0984e5SSebastian Reichel 
798c0984e5SSebastian Reichel 	return 0;
808c0984e5SSebastian Reichel }
818c0984e5SSebastian Reichel 
ds2780_read_block(struct ds2780_device_info * dev_info,u8 * val,int addr,size_t count)828c0984e5SSebastian Reichel static inline int ds2780_read_block(struct ds2780_device_info *dev_info,
838c0984e5SSebastian Reichel 	u8 *val, int addr, size_t count)
848c0984e5SSebastian Reichel {
858c0984e5SSebastian Reichel 	return ds2780_battery_io(dev_info, val, addr, count, 0);
868c0984e5SSebastian Reichel }
878c0984e5SSebastian Reichel 
ds2780_write(struct ds2780_device_info * dev_info,u8 * val,int addr,size_t count)888c0984e5SSebastian Reichel static inline int ds2780_write(struct ds2780_device_info *dev_info, u8 *val,
898c0984e5SSebastian Reichel 	int addr, size_t count)
908c0984e5SSebastian Reichel {
918c0984e5SSebastian Reichel 	return ds2780_battery_io(dev_info, val, addr, count, 1);
928c0984e5SSebastian Reichel }
938c0984e5SSebastian Reichel 
ds2780_store_eeprom(struct device * dev,int addr)948c0984e5SSebastian Reichel static inline int ds2780_store_eeprom(struct device *dev, int addr)
958c0984e5SSebastian Reichel {
968c0984e5SSebastian Reichel 	return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_COPY_DATA);
978c0984e5SSebastian Reichel }
988c0984e5SSebastian Reichel 
ds2780_recall_eeprom(struct device * dev,int addr)998c0984e5SSebastian Reichel static inline int ds2780_recall_eeprom(struct device *dev, int addr)
1008c0984e5SSebastian Reichel {
1018c0984e5SSebastian Reichel 	return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_RECALL_DATA);
1028c0984e5SSebastian Reichel }
1038c0984e5SSebastian Reichel 
ds2780_save_eeprom(struct ds2780_device_info * dev_info,int reg)1048c0984e5SSebastian Reichel static int ds2780_save_eeprom(struct ds2780_device_info *dev_info, int reg)
1058c0984e5SSebastian Reichel {
1068c0984e5SSebastian Reichel 	int ret;
1078c0984e5SSebastian Reichel 
1088c0984e5SSebastian Reichel 	ret = ds2780_store_eeprom(dev_info->w1_dev, reg);
1098c0984e5SSebastian Reichel 	if (ret < 0)
1108c0984e5SSebastian Reichel 		return ret;
1118c0984e5SSebastian Reichel 
1128c0984e5SSebastian Reichel 	ret = ds2780_recall_eeprom(dev_info->w1_dev, reg);
1138c0984e5SSebastian Reichel 	if (ret < 0)
1148c0984e5SSebastian Reichel 		return ret;
1158c0984e5SSebastian Reichel 
1168c0984e5SSebastian Reichel 	return 0;
1178c0984e5SSebastian Reichel }
1188c0984e5SSebastian Reichel 
1198c0984e5SSebastian Reichel /* Set sense resistor value in mhos */
ds2780_set_sense_register(struct ds2780_device_info * dev_info,u8 conductance)1208c0984e5SSebastian Reichel static int ds2780_set_sense_register(struct ds2780_device_info *dev_info,
1218c0984e5SSebastian Reichel 	u8 conductance)
1228c0984e5SSebastian Reichel {
1238c0984e5SSebastian Reichel 	int ret;
1248c0984e5SSebastian Reichel 
1258c0984e5SSebastian Reichel 	ret = ds2780_write(dev_info, &conductance,
1268c0984e5SSebastian Reichel 				DS2780_RSNSP_REG, sizeof(u8));
1278c0984e5SSebastian Reichel 	if (ret < 0)
1288c0984e5SSebastian Reichel 		return ret;
1298c0984e5SSebastian Reichel 
1308c0984e5SSebastian Reichel 	return ds2780_save_eeprom(dev_info, DS2780_RSNSP_REG);
1318c0984e5SSebastian Reichel }
1328c0984e5SSebastian Reichel 
1338c0984e5SSebastian Reichel /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
ds2780_get_rsgain_register(struct ds2780_device_info * dev_info,u16 * rsgain)1348c0984e5SSebastian Reichel static int ds2780_get_rsgain_register(struct ds2780_device_info *dev_info,
1358c0984e5SSebastian Reichel 	u16 *rsgain)
1368c0984e5SSebastian Reichel {
1378c0984e5SSebastian Reichel 	return ds2780_read16(dev_info, rsgain, DS2780_RSGAIN_MSB_REG);
1388c0984e5SSebastian Reichel }
1398c0984e5SSebastian Reichel 
1408c0984e5SSebastian Reichel /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
ds2780_set_rsgain_register(struct ds2780_device_info * dev_info,u16 rsgain)1418c0984e5SSebastian Reichel static int ds2780_set_rsgain_register(struct ds2780_device_info *dev_info,
1428c0984e5SSebastian Reichel 	u16 rsgain)
1438c0984e5SSebastian Reichel {
1448c0984e5SSebastian Reichel 	int ret;
1458c0984e5SSebastian Reichel 	u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
1468c0984e5SSebastian Reichel 
1478c0984e5SSebastian Reichel 	ret = ds2780_write(dev_info, raw,
1488c0984e5SSebastian Reichel 				DS2780_RSGAIN_MSB_REG, sizeof(raw));
1498c0984e5SSebastian Reichel 	if (ret < 0)
1508c0984e5SSebastian Reichel 		return ret;
1518c0984e5SSebastian Reichel 
1528c0984e5SSebastian Reichel 	return ds2780_save_eeprom(dev_info, DS2780_RSGAIN_MSB_REG);
1538c0984e5SSebastian Reichel }
1548c0984e5SSebastian Reichel 
ds2780_get_voltage(struct ds2780_device_info * dev_info,int * voltage_uV)1558c0984e5SSebastian Reichel static int ds2780_get_voltage(struct ds2780_device_info *dev_info,
1568c0984e5SSebastian Reichel 	int *voltage_uV)
1578c0984e5SSebastian Reichel {
1588c0984e5SSebastian Reichel 	int ret;
1598c0984e5SSebastian Reichel 	s16 voltage_raw;
1608c0984e5SSebastian Reichel 
1618c0984e5SSebastian Reichel 	/*
1628c0984e5SSebastian Reichel 	 * The voltage value is located in 10 bits across the voltage MSB
1633f41e742SWang Qing 	 * and LSB registers in two's complement form
1648c0984e5SSebastian Reichel 	 * Sign bit of the voltage value is in bit 7 of the voltage MSB register
1658c0984e5SSebastian Reichel 	 * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
1668c0984e5SSebastian Reichel 	 * voltage MSB register
1678c0984e5SSebastian Reichel 	 * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
1688c0984e5SSebastian Reichel 	 * voltage LSB register
1698c0984e5SSebastian Reichel 	 */
1708c0984e5SSebastian Reichel 	ret = ds2780_read16(dev_info, &voltage_raw,
1718c0984e5SSebastian Reichel 				DS2780_VOLT_MSB_REG);
1728c0984e5SSebastian Reichel 	if (ret < 0)
1738c0984e5SSebastian Reichel 		return ret;
1748c0984e5SSebastian Reichel 
1758c0984e5SSebastian Reichel 	/*
1768c0984e5SSebastian Reichel 	 * DS2780 reports voltage in units of 4.88mV, but the battery class
1778c0984e5SSebastian Reichel 	 * reports in units of uV, so convert by multiplying by 4880.
1788c0984e5SSebastian Reichel 	 */
1798c0984e5SSebastian Reichel 	*voltage_uV = (voltage_raw / 32) * 4880;
1808c0984e5SSebastian Reichel 	return 0;
1818c0984e5SSebastian Reichel }
1828c0984e5SSebastian Reichel 
ds2780_get_temperature(struct ds2780_device_info * dev_info,int * temperature)1838c0984e5SSebastian Reichel static int ds2780_get_temperature(struct ds2780_device_info *dev_info,
1848c0984e5SSebastian Reichel 	int *temperature)
1858c0984e5SSebastian Reichel {
1868c0984e5SSebastian Reichel 	int ret;
1878c0984e5SSebastian Reichel 	s16 temperature_raw;
1888c0984e5SSebastian Reichel 
1898c0984e5SSebastian Reichel 	/*
1908c0984e5SSebastian Reichel 	 * The temperature value is located in 10 bits across the temperature
1913f41e742SWang Qing 	 * MSB and LSB registers in two's complement form
1928c0984e5SSebastian Reichel 	 * Sign bit of the temperature value is in bit 7 of the temperature
1938c0984e5SSebastian Reichel 	 * MSB register
1948c0984e5SSebastian Reichel 	 * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
1958c0984e5SSebastian Reichel 	 * temperature MSB register
1968c0984e5SSebastian Reichel 	 * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
1978c0984e5SSebastian Reichel 	 * temperature LSB register
1988c0984e5SSebastian Reichel 	 */
1998c0984e5SSebastian Reichel 	ret = ds2780_read16(dev_info, &temperature_raw,
2008c0984e5SSebastian Reichel 				DS2780_TEMP_MSB_REG);
2018c0984e5SSebastian Reichel 	if (ret < 0)
2028c0984e5SSebastian Reichel 		return ret;
2038c0984e5SSebastian Reichel 
2048c0984e5SSebastian Reichel 	/*
2058c0984e5SSebastian Reichel 	 * Temperature is measured in units of 0.125 degrees celcius, the
2068c0984e5SSebastian Reichel 	 * power_supply class measures temperature in tenths of degrees
2078c0984e5SSebastian Reichel 	 * celsius. The temperature value is stored as a 10 bit number, plus
2088c0984e5SSebastian Reichel 	 * sign in the upper bits of a 16 bit register.
2098c0984e5SSebastian Reichel 	 */
2108c0984e5SSebastian Reichel 	*temperature = ((temperature_raw / 32) * 125) / 100;
2118c0984e5SSebastian Reichel 	return 0;
2128c0984e5SSebastian Reichel }
2138c0984e5SSebastian Reichel 
ds2780_get_current(struct ds2780_device_info * dev_info,enum current_types type,int * current_uA)2148c0984e5SSebastian Reichel static int ds2780_get_current(struct ds2780_device_info *dev_info,
2158c0984e5SSebastian Reichel 	enum current_types type, int *current_uA)
2168c0984e5SSebastian Reichel {
2178c0984e5SSebastian Reichel 	int ret, sense_res;
2188c0984e5SSebastian Reichel 	s16 current_raw;
2198c0984e5SSebastian Reichel 	u8 sense_res_raw, reg_msb;
2208c0984e5SSebastian Reichel 
2218c0984e5SSebastian Reichel 	/*
2228c0984e5SSebastian Reichel 	 * The units of measurement for current are dependent on the value of
2238c0984e5SSebastian Reichel 	 * the sense resistor.
2248c0984e5SSebastian Reichel 	 */
2258c0984e5SSebastian Reichel 	ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
2268c0984e5SSebastian Reichel 	if (ret < 0)
2278c0984e5SSebastian Reichel 		return ret;
2288c0984e5SSebastian Reichel 
2298c0984e5SSebastian Reichel 	if (sense_res_raw == 0) {
2308c0984e5SSebastian Reichel 		dev_err(dev_info->dev, "sense resistor value is 0\n");
2318c0984e5SSebastian Reichel 		return -EINVAL;
2328c0984e5SSebastian Reichel 	}
2338c0984e5SSebastian Reichel 	sense_res = 1000 / sense_res_raw;
2348c0984e5SSebastian Reichel 
2358c0984e5SSebastian Reichel 	if (type == CURRENT_NOW)
2368c0984e5SSebastian Reichel 		reg_msb = DS2780_CURRENT_MSB_REG;
2378c0984e5SSebastian Reichel 	else if (type == CURRENT_AVG)
2388c0984e5SSebastian Reichel 		reg_msb = DS2780_IAVG_MSB_REG;
2398c0984e5SSebastian Reichel 	else
2408c0984e5SSebastian Reichel 		return -EINVAL;
2418c0984e5SSebastian Reichel 
2428c0984e5SSebastian Reichel 	/*
2438c0984e5SSebastian Reichel 	 * The current value is located in 16 bits across the current MSB
2443f41e742SWang Qing 	 * and LSB registers in two's complement form
2458c0984e5SSebastian Reichel 	 * Sign bit of the current value is in bit 7 of the current MSB register
2468c0984e5SSebastian Reichel 	 * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
2478c0984e5SSebastian Reichel 	 * MSB register
2488c0984e5SSebastian Reichel 	 * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
2498c0984e5SSebastian Reichel 	 * LSB register
2508c0984e5SSebastian Reichel 	 */
2518c0984e5SSebastian Reichel 	ret = ds2780_read16(dev_info, &current_raw, reg_msb);
2528c0984e5SSebastian Reichel 	if (ret < 0)
2538c0984e5SSebastian Reichel 		return ret;
2548c0984e5SSebastian Reichel 
2558c0984e5SSebastian Reichel 	*current_uA = current_raw * (DS2780_CURRENT_UNITS / sense_res);
2568c0984e5SSebastian Reichel 	return 0;
2578c0984e5SSebastian Reichel }
2588c0984e5SSebastian Reichel 
ds2780_get_accumulated_current(struct ds2780_device_info * dev_info,int * accumulated_current)2598c0984e5SSebastian Reichel static int ds2780_get_accumulated_current(struct ds2780_device_info *dev_info,
2608c0984e5SSebastian Reichel 	int *accumulated_current)
2618c0984e5SSebastian Reichel {
2628c0984e5SSebastian Reichel 	int ret, sense_res;
2638c0984e5SSebastian Reichel 	s16 current_raw;
2648c0984e5SSebastian Reichel 	u8 sense_res_raw;
2658c0984e5SSebastian Reichel 
2668c0984e5SSebastian Reichel 	/*
2678c0984e5SSebastian Reichel 	 * The units of measurement for accumulated current are dependent on
2688c0984e5SSebastian Reichel 	 * the value of the sense resistor.
2698c0984e5SSebastian Reichel 	 */
2708c0984e5SSebastian Reichel 	ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
2718c0984e5SSebastian Reichel 	if (ret < 0)
2728c0984e5SSebastian Reichel 		return ret;
2738c0984e5SSebastian Reichel 
2748c0984e5SSebastian Reichel 	if (sense_res_raw == 0) {
2758c0984e5SSebastian Reichel 		dev_err(dev_info->dev, "sense resistor value is 0\n");
2768c0984e5SSebastian Reichel 		return -ENXIO;
2778c0984e5SSebastian Reichel 	}
2788c0984e5SSebastian Reichel 	sense_res = 1000 / sense_res_raw;
2798c0984e5SSebastian Reichel 
2808c0984e5SSebastian Reichel 	/*
2818c0984e5SSebastian Reichel 	 * The ACR value is located in 16 bits across the ACR MSB and
2828c0984e5SSebastian Reichel 	 * LSB registers
2838c0984e5SSebastian Reichel 	 * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
2848c0984e5SSebastian Reichel 	 * MSB register
2858c0984e5SSebastian Reichel 	 * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
2868c0984e5SSebastian Reichel 	 * LSB register
2878c0984e5SSebastian Reichel 	 */
2888c0984e5SSebastian Reichel 	ret = ds2780_read16(dev_info, &current_raw, DS2780_ACR_MSB_REG);
2898c0984e5SSebastian Reichel 	if (ret < 0)
2908c0984e5SSebastian Reichel 		return ret;
2918c0984e5SSebastian Reichel 
2928c0984e5SSebastian Reichel 	*accumulated_current = current_raw * (DS2780_CHARGE_UNITS / sense_res);
2938c0984e5SSebastian Reichel 	return 0;
2948c0984e5SSebastian Reichel }
2958c0984e5SSebastian Reichel 
ds2780_get_capacity(struct ds2780_device_info * dev_info,int * capacity)2968c0984e5SSebastian Reichel static int ds2780_get_capacity(struct ds2780_device_info *dev_info,
2978c0984e5SSebastian Reichel 	int *capacity)
2988c0984e5SSebastian Reichel {
2998c0984e5SSebastian Reichel 	int ret;
3008c0984e5SSebastian Reichel 	u8 raw;
3018c0984e5SSebastian Reichel 
3028c0984e5SSebastian Reichel 	ret = ds2780_read8(dev_info, &raw, DS2780_RARC_REG);
3038c0984e5SSebastian Reichel 	if (ret < 0)
3048c0984e5SSebastian Reichel 		return ret;
3058c0984e5SSebastian Reichel 
3068c0984e5SSebastian Reichel 	*capacity = raw;
3078c0984e5SSebastian Reichel 	return raw;
3088c0984e5SSebastian Reichel }
3098c0984e5SSebastian Reichel 
ds2780_get_status(struct ds2780_device_info * dev_info,int * status)3108c0984e5SSebastian Reichel static int ds2780_get_status(struct ds2780_device_info *dev_info, int *status)
3118c0984e5SSebastian Reichel {
3128c0984e5SSebastian Reichel 	int ret, current_uA, capacity;
3138c0984e5SSebastian Reichel 
3148c0984e5SSebastian Reichel 	ret = ds2780_get_current(dev_info, CURRENT_NOW, &current_uA);
3158c0984e5SSebastian Reichel 	if (ret < 0)
3168c0984e5SSebastian Reichel 		return ret;
3178c0984e5SSebastian Reichel 
3188c0984e5SSebastian Reichel 	ret = ds2780_get_capacity(dev_info, &capacity);
3198c0984e5SSebastian Reichel 	if (ret < 0)
3208c0984e5SSebastian Reichel 		return ret;
3218c0984e5SSebastian Reichel 
3228c0984e5SSebastian Reichel 	if (capacity == 100)
3238c0984e5SSebastian Reichel 		*status = POWER_SUPPLY_STATUS_FULL;
3248c0984e5SSebastian Reichel 	else if (current_uA == 0)
3258c0984e5SSebastian Reichel 		*status = POWER_SUPPLY_STATUS_NOT_CHARGING;
3268c0984e5SSebastian Reichel 	else if (current_uA < 0)
3278c0984e5SSebastian Reichel 		*status = POWER_SUPPLY_STATUS_DISCHARGING;
3288c0984e5SSebastian Reichel 	else
3298c0984e5SSebastian Reichel 		*status = POWER_SUPPLY_STATUS_CHARGING;
3308c0984e5SSebastian Reichel 
3318c0984e5SSebastian Reichel 	return 0;
3328c0984e5SSebastian Reichel }
3338c0984e5SSebastian Reichel 
ds2780_get_charge_now(struct ds2780_device_info * dev_info,int * charge_now)3348c0984e5SSebastian Reichel static int ds2780_get_charge_now(struct ds2780_device_info *dev_info,
3358c0984e5SSebastian Reichel 	int *charge_now)
3368c0984e5SSebastian Reichel {
3378c0984e5SSebastian Reichel 	int ret;
3388c0984e5SSebastian Reichel 	u16 charge_raw;
3398c0984e5SSebastian Reichel 
3408c0984e5SSebastian Reichel 	/*
3418c0984e5SSebastian Reichel 	 * The RAAC value is located in 16 bits across the RAAC MSB and
3428c0984e5SSebastian Reichel 	 * LSB registers
3438c0984e5SSebastian Reichel 	 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
3448c0984e5SSebastian Reichel 	 * MSB register
3458c0984e5SSebastian Reichel 	 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
3468c0984e5SSebastian Reichel 	 * LSB register
3478c0984e5SSebastian Reichel 	 */
3488c0984e5SSebastian Reichel 	ret = ds2780_read16(dev_info, &charge_raw, DS2780_RAAC_MSB_REG);
3498c0984e5SSebastian Reichel 	if (ret < 0)
3508c0984e5SSebastian Reichel 		return ret;
3518c0984e5SSebastian Reichel 
3528c0984e5SSebastian Reichel 	*charge_now = charge_raw * 1600;
3538c0984e5SSebastian Reichel 	return 0;
3548c0984e5SSebastian Reichel }
3558c0984e5SSebastian Reichel 
ds2780_get_control_register(struct ds2780_device_info * dev_info,u8 * control_reg)3568c0984e5SSebastian Reichel static int ds2780_get_control_register(struct ds2780_device_info *dev_info,
3578c0984e5SSebastian Reichel 	u8 *control_reg)
3588c0984e5SSebastian Reichel {
3598c0984e5SSebastian Reichel 	return ds2780_read8(dev_info, control_reg, DS2780_CONTROL_REG);
3608c0984e5SSebastian Reichel }
3618c0984e5SSebastian Reichel 
ds2780_set_control_register(struct ds2780_device_info * dev_info,u8 control_reg)3628c0984e5SSebastian Reichel static int ds2780_set_control_register(struct ds2780_device_info *dev_info,
3638c0984e5SSebastian Reichel 	u8 control_reg)
3648c0984e5SSebastian Reichel {
3658c0984e5SSebastian Reichel 	int ret;
3668c0984e5SSebastian Reichel 
3678c0984e5SSebastian Reichel 	ret = ds2780_write(dev_info, &control_reg,
3688c0984e5SSebastian Reichel 				DS2780_CONTROL_REG, sizeof(u8));
3698c0984e5SSebastian Reichel 	if (ret < 0)
3708c0984e5SSebastian Reichel 		return ret;
3718c0984e5SSebastian Reichel 
3728c0984e5SSebastian Reichel 	return ds2780_save_eeprom(dev_info, DS2780_CONTROL_REG);
3738c0984e5SSebastian Reichel }
3748c0984e5SSebastian Reichel 
ds2780_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)3758c0984e5SSebastian Reichel static int ds2780_battery_get_property(struct power_supply *psy,
3768c0984e5SSebastian Reichel 	enum power_supply_property psp,
3778c0984e5SSebastian Reichel 	union power_supply_propval *val)
3788c0984e5SSebastian Reichel {
3798c0984e5SSebastian Reichel 	int ret = 0;
3808c0984e5SSebastian Reichel 	struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
3818c0984e5SSebastian Reichel 
3828c0984e5SSebastian Reichel 	switch (psp) {
3838c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
3848c0984e5SSebastian Reichel 		ret = ds2780_get_voltage(dev_info, &val->intval);
3858c0984e5SSebastian Reichel 		break;
3868c0984e5SSebastian Reichel 
3878c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TEMP:
3888c0984e5SSebastian Reichel 		ret = ds2780_get_temperature(dev_info, &val->intval);
3898c0984e5SSebastian Reichel 		break;
3908c0984e5SSebastian Reichel 
3918c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_MODEL_NAME:
3928c0984e5SSebastian Reichel 		val->strval = model;
3938c0984e5SSebastian Reichel 		break;
3948c0984e5SSebastian Reichel 
3958c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_MANUFACTURER:
3968c0984e5SSebastian Reichel 		val->strval = manufacturer;
3978c0984e5SSebastian Reichel 		break;
3988c0984e5SSebastian Reichel 
3998c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CURRENT_NOW:
4008c0984e5SSebastian Reichel 		ret = ds2780_get_current(dev_info, CURRENT_NOW, &val->intval);
4018c0984e5SSebastian Reichel 		break;
4028c0984e5SSebastian Reichel 
4038c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CURRENT_AVG:
4048c0984e5SSebastian Reichel 		ret = ds2780_get_current(dev_info, CURRENT_AVG, &val->intval);
4058c0984e5SSebastian Reichel 		break;
4068c0984e5SSebastian Reichel 
4078c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
4088c0984e5SSebastian Reichel 		ret = ds2780_get_status(dev_info, &val->intval);
4098c0984e5SSebastian Reichel 		break;
4108c0984e5SSebastian Reichel 
4118c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY:
4128c0984e5SSebastian Reichel 		ret = ds2780_get_capacity(dev_info, &val->intval);
4138c0984e5SSebastian Reichel 		break;
4148c0984e5SSebastian Reichel 
4158c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
4168c0984e5SSebastian Reichel 		ret = ds2780_get_accumulated_current(dev_info, &val->intval);
4178c0984e5SSebastian Reichel 		break;
4188c0984e5SSebastian Reichel 
4198c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_NOW:
4208c0984e5SSebastian Reichel 		ret = ds2780_get_charge_now(dev_info, &val->intval);
4218c0984e5SSebastian Reichel 		break;
4228c0984e5SSebastian Reichel 
4238c0984e5SSebastian Reichel 	default:
4248c0984e5SSebastian Reichel 		ret = -EINVAL;
4258c0984e5SSebastian Reichel 	}
4268c0984e5SSebastian Reichel 
4278c0984e5SSebastian Reichel 	return ret;
4288c0984e5SSebastian Reichel }
4298c0984e5SSebastian Reichel 
4308c0984e5SSebastian Reichel static enum power_supply_property ds2780_battery_props[] = {
4318c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
4328c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
4338c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
4348c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MODEL_NAME,
4358c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
4368c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
4378c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_AVG,
4388c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
4398c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
4408c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
4418c0984e5SSebastian Reichel };
4428c0984e5SSebastian Reichel 
ds2780_get_pmod_enabled(struct device * dev,struct device_attribute * attr,char * buf)4438c0984e5SSebastian Reichel static ssize_t ds2780_get_pmod_enabled(struct device *dev,
4448c0984e5SSebastian Reichel 	struct device_attribute *attr,
4458c0984e5SSebastian Reichel 	char *buf)
4468c0984e5SSebastian Reichel {
4478c0984e5SSebastian Reichel 	int ret;
4488c0984e5SSebastian Reichel 	u8 control_reg;
4498c0984e5SSebastian Reichel 	struct power_supply *psy = to_power_supply(dev);
4508c0984e5SSebastian Reichel 	struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
4518c0984e5SSebastian Reichel 
4528c0984e5SSebastian Reichel 	/* Get power mode */
4538c0984e5SSebastian Reichel 	ret = ds2780_get_control_register(dev_info, &control_reg);
4548c0984e5SSebastian Reichel 	if (ret < 0)
4558c0984e5SSebastian Reichel 		return ret;
4568c0984e5SSebastian Reichel 
457*a441f3b9Sye xingchen 	return sysfs_emit(buf, "%d\n",
4588c0984e5SSebastian Reichel 		 !!(control_reg & DS2780_CONTROL_REG_PMOD));
4598c0984e5SSebastian Reichel }
4608c0984e5SSebastian Reichel 
ds2780_set_pmod_enabled(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4618c0984e5SSebastian Reichel static ssize_t ds2780_set_pmod_enabled(struct device *dev,
4628c0984e5SSebastian Reichel 	struct device_attribute *attr,
4638c0984e5SSebastian Reichel 	const char *buf,
4648c0984e5SSebastian Reichel 	size_t count)
4658c0984e5SSebastian Reichel {
4668c0984e5SSebastian Reichel 	int ret;
4678c0984e5SSebastian Reichel 	u8 control_reg, new_setting;
4688c0984e5SSebastian Reichel 	struct power_supply *psy = to_power_supply(dev);
4698c0984e5SSebastian Reichel 	struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
4708c0984e5SSebastian Reichel 
4718c0984e5SSebastian Reichel 	/* Set power mode */
4728c0984e5SSebastian Reichel 	ret = ds2780_get_control_register(dev_info, &control_reg);
4738c0984e5SSebastian Reichel 	if (ret < 0)
4748c0984e5SSebastian Reichel 		return ret;
4758c0984e5SSebastian Reichel 
4768c0984e5SSebastian Reichel 	ret = kstrtou8(buf, 0, &new_setting);
4778c0984e5SSebastian Reichel 	if (ret < 0)
4788c0984e5SSebastian Reichel 		return ret;
4798c0984e5SSebastian Reichel 
4808c0984e5SSebastian Reichel 	if ((new_setting != 0) && (new_setting != 1)) {
4818c0984e5SSebastian Reichel 		dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
4828c0984e5SSebastian Reichel 		return -EINVAL;
4838c0984e5SSebastian Reichel 	}
4848c0984e5SSebastian Reichel 
4858c0984e5SSebastian Reichel 	if (new_setting)
4868c0984e5SSebastian Reichel 		control_reg |= DS2780_CONTROL_REG_PMOD;
4878c0984e5SSebastian Reichel 	else
4888c0984e5SSebastian Reichel 		control_reg &= ~DS2780_CONTROL_REG_PMOD;
4898c0984e5SSebastian Reichel 
4908c0984e5SSebastian Reichel 	ret = ds2780_set_control_register(dev_info, control_reg);
4918c0984e5SSebastian Reichel 	if (ret < 0)
4928c0984e5SSebastian Reichel 		return ret;
4938c0984e5SSebastian Reichel 
4948c0984e5SSebastian Reichel 	return count;
4958c0984e5SSebastian Reichel }
4968c0984e5SSebastian Reichel 
ds2780_get_sense_resistor_value(struct device * dev,struct device_attribute * attr,char * buf)4978c0984e5SSebastian Reichel static ssize_t ds2780_get_sense_resistor_value(struct device *dev,
4988c0984e5SSebastian Reichel 	struct device_attribute *attr,
4998c0984e5SSebastian Reichel 	char *buf)
5008c0984e5SSebastian Reichel {
5018c0984e5SSebastian Reichel 	int ret;
5028c0984e5SSebastian Reichel 	u8 sense_resistor;
5038c0984e5SSebastian Reichel 	struct power_supply *psy = to_power_supply(dev);
5048c0984e5SSebastian Reichel 	struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
5058c0984e5SSebastian Reichel 
5068c0984e5SSebastian Reichel 	ret = ds2780_read8(dev_info, &sense_resistor, DS2780_RSNSP_REG);
5078c0984e5SSebastian Reichel 	if (ret < 0)
5088c0984e5SSebastian Reichel 		return ret;
5098c0984e5SSebastian Reichel 
510*a441f3b9Sye xingchen 	ret = sysfs_emit(buf, "%d\n", sense_resistor);
5118c0984e5SSebastian Reichel 	return ret;
5128c0984e5SSebastian Reichel }
5138c0984e5SSebastian Reichel 
ds2780_set_sense_resistor_value(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)5148c0984e5SSebastian Reichel static ssize_t ds2780_set_sense_resistor_value(struct device *dev,
5158c0984e5SSebastian Reichel 	struct device_attribute *attr,
5168c0984e5SSebastian Reichel 	const char *buf,
5178c0984e5SSebastian Reichel 	size_t count)
5188c0984e5SSebastian Reichel {
5198c0984e5SSebastian Reichel 	int ret;
5208c0984e5SSebastian Reichel 	u8 new_setting;
5218c0984e5SSebastian Reichel 	struct power_supply *psy = to_power_supply(dev);
5228c0984e5SSebastian Reichel 	struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
5238c0984e5SSebastian Reichel 
5248c0984e5SSebastian Reichel 	ret = kstrtou8(buf, 0, &new_setting);
5258c0984e5SSebastian Reichel 	if (ret < 0)
5268c0984e5SSebastian Reichel 		return ret;
5278c0984e5SSebastian Reichel 
5288c0984e5SSebastian Reichel 	ret = ds2780_set_sense_register(dev_info, new_setting);
5298c0984e5SSebastian Reichel 	if (ret < 0)
5308c0984e5SSebastian Reichel 		return ret;
5318c0984e5SSebastian Reichel 
5328c0984e5SSebastian Reichel 	return count;
5338c0984e5SSebastian Reichel }
5348c0984e5SSebastian Reichel 
ds2780_get_rsgain_setting(struct device * dev,struct device_attribute * attr,char * buf)5358c0984e5SSebastian Reichel static ssize_t ds2780_get_rsgain_setting(struct device *dev,
5368c0984e5SSebastian Reichel 	struct device_attribute *attr,
5378c0984e5SSebastian Reichel 	char *buf)
5388c0984e5SSebastian Reichel {
5398c0984e5SSebastian Reichel 	int ret;
5408c0984e5SSebastian Reichel 	u16 rsgain;
5418c0984e5SSebastian Reichel 	struct power_supply *psy = to_power_supply(dev);
5428c0984e5SSebastian Reichel 	struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
5438c0984e5SSebastian Reichel 
5448c0984e5SSebastian Reichel 	ret = ds2780_get_rsgain_register(dev_info, &rsgain);
5458c0984e5SSebastian Reichel 	if (ret < 0)
5468c0984e5SSebastian Reichel 		return ret;
5478c0984e5SSebastian Reichel 
548*a441f3b9Sye xingchen 	return sysfs_emit(buf, "%d\n", rsgain);
5498c0984e5SSebastian Reichel }
5508c0984e5SSebastian Reichel 
ds2780_set_rsgain_setting(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)5518c0984e5SSebastian Reichel static ssize_t ds2780_set_rsgain_setting(struct device *dev,
5528c0984e5SSebastian Reichel 	struct device_attribute *attr,
5538c0984e5SSebastian Reichel 	const char *buf,
5548c0984e5SSebastian Reichel 	size_t count)
5558c0984e5SSebastian Reichel {
5568c0984e5SSebastian Reichel 	int ret;
5578c0984e5SSebastian Reichel 	u16 new_setting;
5588c0984e5SSebastian Reichel 	struct power_supply *psy = to_power_supply(dev);
5598c0984e5SSebastian Reichel 	struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
5608c0984e5SSebastian Reichel 
5618c0984e5SSebastian Reichel 	ret = kstrtou16(buf, 0, &new_setting);
5628c0984e5SSebastian Reichel 	if (ret < 0)
5638c0984e5SSebastian Reichel 		return ret;
5648c0984e5SSebastian Reichel 
5658c0984e5SSebastian Reichel 	/* Gain can only be from 0 to 1.999 in steps of .001 */
5668c0984e5SSebastian Reichel 	if (new_setting > 1999) {
5678c0984e5SSebastian Reichel 		dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
5688c0984e5SSebastian Reichel 		return -EINVAL;
5698c0984e5SSebastian Reichel 	}
5708c0984e5SSebastian Reichel 
5718c0984e5SSebastian Reichel 	ret = ds2780_set_rsgain_register(dev_info, new_setting);
5728c0984e5SSebastian Reichel 	if (ret < 0)
5738c0984e5SSebastian Reichel 		return ret;
5748c0984e5SSebastian Reichel 
5758c0984e5SSebastian Reichel 	return count;
5768c0984e5SSebastian Reichel }
5778c0984e5SSebastian Reichel 
ds2780_get_pio_pin(struct device * dev,struct device_attribute * attr,char * buf)5788c0984e5SSebastian Reichel static ssize_t ds2780_get_pio_pin(struct device *dev,
5798c0984e5SSebastian Reichel 	struct device_attribute *attr,
5808c0984e5SSebastian Reichel 	char *buf)
5818c0984e5SSebastian Reichel {
5828c0984e5SSebastian Reichel 	int ret;
5838c0984e5SSebastian Reichel 	u8 sfr;
5848c0984e5SSebastian Reichel 	struct power_supply *psy = to_power_supply(dev);
5858c0984e5SSebastian Reichel 	struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
5868c0984e5SSebastian Reichel 
5878c0984e5SSebastian Reichel 	ret = ds2780_read8(dev_info, &sfr, DS2780_SFR_REG);
5888c0984e5SSebastian Reichel 	if (ret < 0)
5898c0984e5SSebastian Reichel 		return ret;
5908c0984e5SSebastian Reichel 
591*a441f3b9Sye xingchen 	ret = sysfs_emit(buf, "%d\n", sfr & DS2780_SFR_REG_PIOSC);
5928c0984e5SSebastian Reichel 	return ret;
5938c0984e5SSebastian Reichel }
5948c0984e5SSebastian Reichel 
ds2780_set_pio_pin(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)5958c0984e5SSebastian Reichel static ssize_t ds2780_set_pio_pin(struct device *dev,
5968c0984e5SSebastian Reichel 	struct device_attribute *attr,
5978c0984e5SSebastian Reichel 	const char *buf,
5988c0984e5SSebastian Reichel 	size_t count)
5998c0984e5SSebastian Reichel {
6008c0984e5SSebastian Reichel 	int ret;
6018c0984e5SSebastian Reichel 	u8 new_setting;
6028c0984e5SSebastian Reichel 	struct power_supply *psy = to_power_supply(dev);
6038c0984e5SSebastian Reichel 	struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
6048c0984e5SSebastian Reichel 
6058c0984e5SSebastian Reichel 	ret = kstrtou8(buf, 0, &new_setting);
6068c0984e5SSebastian Reichel 	if (ret < 0)
6078c0984e5SSebastian Reichel 		return ret;
6088c0984e5SSebastian Reichel 
6098c0984e5SSebastian Reichel 	if ((new_setting != 0) && (new_setting != 1)) {
6108c0984e5SSebastian Reichel 		dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
6118c0984e5SSebastian Reichel 		return -EINVAL;
6128c0984e5SSebastian Reichel 	}
6138c0984e5SSebastian Reichel 
6148c0984e5SSebastian Reichel 	ret = ds2780_write(dev_info, &new_setting,
6158c0984e5SSebastian Reichel 				DS2780_SFR_REG, sizeof(u8));
6168c0984e5SSebastian Reichel 	if (ret < 0)
6178c0984e5SSebastian Reichel 		return ret;
6188c0984e5SSebastian Reichel 
6198c0984e5SSebastian Reichel 	return count;
6208c0984e5SSebastian Reichel }
6218c0984e5SSebastian Reichel 
ds2780_read_param_eeprom_bin(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)6228c0984e5SSebastian Reichel static ssize_t ds2780_read_param_eeprom_bin(struct file *filp,
6238c0984e5SSebastian Reichel 				struct kobject *kobj,
6248c0984e5SSebastian Reichel 				struct bin_attribute *bin_attr,
6258c0984e5SSebastian Reichel 				char *buf, loff_t off, size_t count)
6268c0984e5SSebastian Reichel {
627f856b2f2STian Tao 	struct device *dev = kobj_to_dev(kobj);
6288c0984e5SSebastian Reichel 	struct power_supply *psy = to_power_supply(dev);
6298c0984e5SSebastian Reichel 	struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
6308c0984e5SSebastian Reichel 
6318c0984e5SSebastian Reichel 	return ds2780_read_block(dev_info, buf,
6328c0984e5SSebastian Reichel 				DS2780_EEPROM_BLOCK1_START + off, count);
6338c0984e5SSebastian Reichel }
6348c0984e5SSebastian Reichel 
ds2780_write_param_eeprom_bin(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)6358c0984e5SSebastian Reichel static ssize_t ds2780_write_param_eeprom_bin(struct file *filp,
6368c0984e5SSebastian Reichel 				struct kobject *kobj,
6378c0984e5SSebastian Reichel 				struct bin_attribute *bin_attr,
6388c0984e5SSebastian Reichel 				char *buf, loff_t off, size_t count)
6398c0984e5SSebastian Reichel {
640f856b2f2STian Tao 	struct device *dev = kobj_to_dev(kobj);
6418c0984e5SSebastian Reichel 	struct power_supply *psy = to_power_supply(dev);
6428c0984e5SSebastian Reichel 	struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
6438c0984e5SSebastian Reichel 	int ret;
6448c0984e5SSebastian Reichel 
6458c0984e5SSebastian Reichel 	ret = ds2780_write(dev_info, buf,
6468c0984e5SSebastian Reichel 				DS2780_EEPROM_BLOCK1_START + off, count);
6478c0984e5SSebastian Reichel 	if (ret < 0)
6488c0984e5SSebastian Reichel 		return ret;
6498c0984e5SSebastian Reichel 
6508c0984e5SSebastian Reichel 	ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK1_START);
6518c0984e5SSebastian Reichel 	if (ret < 0)
6528c0984e5SSebastian Reichel 		return ret;
6538c0984e5SSebastian Reichel 
6548c0984e5SSebastian Reichel 	return count;
6558c0984e5SSebastian Reichel }
6568c0984e5SSebastian Reichel 
65788635b6dSSebastian Reichel static struct bin_attribute ds2780_param_eeprom_bin_attr = {
6588c0984e5SSebastian Reichel 	.attr = {
6598c0984e5SSebastian Reichel 		.name = "param_eeprom",
6608c0984e5SSebastian Reichel 		.mode = S_IRUGO | S_IWUSR,
6618c0984e5SSebastian Reichel 	},
6628c0984e5SSebastian Reichel 	.size = DS2780_PARAM_EEPROM_SIZE,
6638c0984e5SSebastian Reichel 	.read = ds2780_read_param_eeprom_bin,
6648c0984e5SSebastian Reichel 	.write = ds2780_write_param_eeprom_bin,
6658c0984e5SSebastian Reichel };
6668c0984e5SSebastian Reichel 
ds2780_read_user_eeprom_bin(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)6678c0984e5SSebastian Reichel static ssize_t ds2780_read_user_eeprom_bin(struct file *filp,
6688c0984e5SSebastian Reichel 				struct kobject *kobj,
6698c0984e5SSebastian Reichel 				struct bin_attribute *bin_attr,
6708c0984e5SSebastian Reichel 				char *buf, loff_t off, size_t count)
6718c0984e5SSebastian Reichel {
672f856b2f2STian Tao 	struct device *dev = kobj_to_dev(kobj);
6738c0984e5SSebastian Reichel 	struct power_supply *psy = to_power_supply(dev);
6748c0984e5SSebastian Reichel 	struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
6758c0984e5SSebastian Reichel 
6768c0984e5SSebastian Reichel 	return ds2780_read_block(dev_info, buf,
6778c0984e5SSebastian Reichel 				DS2780_EEPROM_BLOCK0_START + off, count);
6788c0984e5SSebastian Reichel }
6798c0984e5SSebastian Reichel 
ds2780_write_user_eeprom_bin(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)6808c0984e5SSebastian Reichel static ssize_t ds2780_write_user_eeprom_bin(struct file *filp,
6818c0984e5SSebastian Reichel 				struct kobject *kobj,
6828c0984e5SSebastian Reichel 				struct bin_attribute *bin_attr,
6838c0984e5SSebastian Reichel 				char *buf, loff_t off, size_t count)
6848c0984e5SSebastian Reichel {
685f856b2f2STian Tao 	struct device *dev = kobj_to_dev(kobj);
6868c0984e5SSebastian Reichel 	struct power_supply *psy = to_power_supply(dev);
6878c0984e5SSebastian Reichel 	struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
6888c0984e5SSebastian Reichel 	int ret;
6898c0984e5SSebastian Reichel 
6908c0984e5SSebastian Reichel 	ret = ds2780_write(dev_info, buf,
6918c0984e5SSebastian Reichel 				DS2780_EEPROM_BLOCK0_START + off, count);
6928c0984e5SSebastian Reichel 	if (ret < 0)
6938c0984e5SSebastian Reichel 		return ret;
6948c0984e5SSebastian Reichel 
6958c0984e5SSebastian Reichel 	ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK0_START);
6968c0984e5SSebastian Reichel 	if (ret < 0)
6978c0984e5SSebastian Reichel 		return ret;
6988c0984e5SSebastian Reichel 
6998c0984e5SSebastian Reichel 	return count;
7008c0984e5SSebastian Reichel }
7018c0984e5SSebastian Reichel 
70288635b6dSSebastian Reichel static struct bin_attribute ds2780_user_eeprom_bin_attr = {
7038c0984e5SSebastian Reichel 	.attr = {
7048c0984e5SSebastian Reichel 		.name = "user_eeprom",
7058c0984e5SSebastian Reichel 		.mode = S_IRUGO | S_IWUSR,
7068c0984e5SSebastian Reichel 	},
7078c0984e5SSebastian Reichel 	.size = DS2780_USER_EEPROM_SIZE,
7088c0984e5SSebastian Reichel 	.read = ds2780_read_user_eeprom_bin,
7098c0984e5SSebastian Reichel 	.write = ds2780_write_user_eeprom_bin,
7108c0984e5SSebastian Reichel };
7118c0984e5SSebastian Reichel 
7128c0984e5SSebastian Reichel static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2780_get_pmod_enabled,
7138c0984e5SSebastian Reichel 	ds2780_set_pmod_enabled);
7148c0984e5SSebastian Reichel static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
7158c0984e5SSebastian Reichel 	ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value);
7168c0984e5SSebastian Reichel static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2780_get_rsgain_setting,
7178c0984e5SSebastian Reichel 	ds2780_set_rsgain_setting);
7188c0984e5SSebastian Reichel static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2780_get_pio_pin,
7198c0984e5SSebastian Reichel 	ds2780_set_pio_pin);
7208c0984e5SSebastian Reichel 
721b10474aaSSebastian Reichel static struct attribute *ds2780_sysfs_attrs[] = {
7228c0984e5SSebastian Reichel 	&dev_attr_pmod_enabled.attr,
7238c0984e5SSebastian Reichel 	&dev_attr_sense_resistor_value.attr,
7248c0984e5SSebastian Reichel 	&dev_attr_rsgain_setting.attr,
7258c0984e5SSebastian Reichel 	&dev_attr_pio_pin.attr,
7268c0984e5SSebastian Reichel 	NULL
7278c0984e5SSebastian Reichel };
7288c0984e5SSebastian Reichel 
72988635b6dSSebastian Reichel static struct bin_attribute *ds2780_sysfs_bin_attrs[] = {
73088635b6dSSebastian Reichel 	&ds2780_param_eeprom_bin_attr,
73188635b6dSSebastian Reichel 	&ds2780_user_eeprom_bin_attr,
73288635b6dSSebastian Reichel 	NULL
73388635b6dSSebastian Reichel };
73488635b6dSSebastian Reichel 
73588635b6dSSebastian Reichel static const struct attribute_group ds2780_sysfs_group = {
73688635b6dSSebastian Reichel 	.attrs = ds2780_sysfs_attrs,
73788635b6dSSebastian Reichel 	.bin_attrs = ds2780_sysfs_bin_attrs,
73888635b6dSSebastian Reichel };
73988635b6dSSebastian Reichel 
74088635b6dSSebastian Reichel static const struct attribute_group *ds2780_sysfs_groups[] = {
74188635b6dSSebastian Reichel 	&ds2780_sysfs_group,
74288635b6dSSebastian Reichel 	NULL,
74388635b6dSSebastian Reichel };
7448c0984e5SSebastian Reichel 
ds2780_battery_probe(struct platform_device * pdev)7458c0984e5SSebastian Reichel static int ds2780_battery_probe(struct platform_device *pdev)
7468c0984e5SSebastian Reichel {
7478c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = {};
7488c0984e5SSebastian Reichel 	struct ds2780_device_info *dev_info;
7498c0984e5SSebastian Reichel 
7508c0984e5SSebastian Reichel 	dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
75188635b6dSSebastian Reichel 	if (!dev_info)
75288635b6dSSebastian Reichel 		return -ENOMEM;
7538c0984e5SSebastian Reichel 
7548c0984e5SSebastian Reichel 	platform_set_drvdata(pdev, dev_info);
7558c0984e5SSebastian Reichel 
7568c0984e5SSebastian Reichel 	dev_info->dev			= &pdev->dev;
7578c0984e5SSebastian Reichel 	dev_info->w1_dev		= pdev->dev.parent;
7588c0984e5SSebastian Reichel 	dev_info->bat_desc.name		= dev_name(&pdev->dev);
7598c0984e5SSebastian Reichel 	dev_info->bat_desc.type		= POWER_SUPPLY_TYPE_BATTERY;
7608c0984e5SSebastian Reichel 	dev_info->bat_desc.properties	= ds2780_battery_props;
7618c0984e5SSebastian Reichel 	dev_info->bat_desc.num_properties = ARRAY_SIZE(ds2780_battery_props);
7628c0984e5SSebastian Reichel 	dev_info->bat_desc.get_property	= ds2780_battery_get_property;
7638c0984e5SSebastian Reichel 
7648c0984e5SSebastian Reichel 	psy_cfg.drv_data		= dev_info;
765b10474aaSSebastian Reichel 	psy_cfg.attr_grp		= ds2780_sysfs_groups;
7668c0984e5SSebastian Reichel 
76763fac6cbSSebastian Reichel 	dev_info->bat = devm_power_supply_register(&pdev->dev,
76863fac6cbSSebastian Reichel 						   &dev_info->bat_desc,
7698c0984e5SSebastian Reichel 						   &psy_cfg);
7708c0984e5SSebastian Reichel 	if (IS_ERR(dev_info->bat)) {
7718c0984e5SSebastian Reichel 		dev_err(dev_info->dev, "failed to register battery\n");
77288635b6dSSebastian Reichel 		return PTR_ERR(dev_info->bat);
7738c0984e5SSebastian Reichel 	}
7748c0984e5SSebastian Reichel 
7758c0984e5SSebastian Reichel 	return 0;
7768c0984e5SSebastian Reichel }
7778c0984e5SSebastian Reichel 
7788c0984e5SSebastian Reichel static struct platform_driver ds2780_battery_driver = {
7798c0984e5SSebastian Reichel 	.driver = {
7808c0984e5SSebastian Reichel 		.name = "ds2780-battery",
7818c0984e5SSebastian Reichel 	},
7828c0984e5SSebastian Reichel 	.probe	  = ds2780_battery_probe,
7838c0984e5SSebastian Reichel };
7848c0984e5SSebastian Reichel 
7858c0984e5SSebastian Reichel module_platform_driver(ds2780_battery_driver);
7868c0984e5SSebastian Reichel 
7878c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
7888c0984e5SSebastian Reichel MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
789415d602bSColin Ian King MODULE_DESCRIPTION("Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC driver");
7908c0984e5SSebastian Reichel MODULE_ALIAS("platform:ds2780-battery");
791