Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * I2C client/driver for the Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2009 Bluewater Systems Ltd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Ryan Mallon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * DS2786 added by Yulia Vilensky <vilensky@compulab.co.il>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * UEvent sending added by Evgeny Romanov <romanov@neurosoft.ru>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/swab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/ds2782_battery.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define DS2782_REG_RARC		0x06	/* Remaining active relative capacity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define DS278x_REG_VOLT_MSB	0x0c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define DS278x_REG_TEMP_MSB	0x0a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define DS278x_REG_CURRENT_MSB	0x0e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /* EEPROM Block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define DS2782_REG_RSNSP	0x69	/* Sense resistor value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define DS2782_CURRENT_UNITS	1563
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define DS2786_REG_RARC		0x02	/* Remaining active relative capacity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define DS2786_CURRENT_UNITS	25
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define DS278x_DELAY		1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) struct ds278x_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) struct ds278x_battery_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	int (*get_battery_current)(struct ds278x_info *info, int *current_uA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int (*get_battery_voltage)(struct ds278x_info *info, int *voltage_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int (*get_battery_capacity)(struct ds278x_info *info, int *capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define to_ds278x_info(x) power_supply_get_drvdata(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) struct ds278x_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct i2c_client	*client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct power_supply	*battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct power_supply_desc	battery_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	const struct ds278x_battery_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	struct delayed_work	bat_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	int			id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int                     rsns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	int			capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	int			status;		/* State Of Charge */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static DEFINE_IDR(battery_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static DEFINE_MUTEX(battery_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static inline int ds278x_read_reg(struct ds278x_info *info, int reg, u8 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	ret = i2c_smbus_read_byte_data(info->client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		dev_err(&info->client->dev, "register read failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	*val = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static inline int ds278x_read_reg16(struct ds278x_info *info, int reg_msb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 				    s16 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	ret = i2c_smbus_read_word_data(info->client, reg_msb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		dev_err(&info->client->dev, "register read failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	*val = swab16(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) static int ds278x_get_temp(struct ds278x_info *info, int *temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	s16 raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	 * Temperature is measured in units of 0.125 degrees celcius, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 * power_supply class measures temperature in tenths of degrees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	 * celsius. The temperature value is stored as a 10 bit number, plus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	 * sign in the upper bits of a 16 bit register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	err = ds278x_read_reg16(info, DS278x_REG_TEMP_MSB, &raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	*temp = ((raw / 32) * 125) / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int ds2782_get_current(struct ds278x_info *info, int *current_uA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	int sense_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	u8 sense_res_raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	s16 raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	 * The units of measurement for current are dependent on the value of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	 * the sense resistor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	err = ds278x_read_reg(info, DS2782_REG_RSNSP, &sense_res_raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (sense_res_raw == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		dev_err(&info->client->dev, "sense resistor value is 0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	sense_res = 1000 / sense_res_raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	dev_dbg(&info->client->dev, "sense resistor = %d milli-ohms\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		sense_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	*current_uA = raw * (DS2782_CURRENT_UNITS / sense_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int ds2782_get_voltage(struct ds278x_info *info, int *voltage_uV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	s16 raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	 * Voltage is measured in units of 4.88mV. The voltage is stored as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	 * a 10-bit number plus sign, in the upper bits of a 16-bit register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	*voltage_uV = (raw / 32) * 4800;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static int ds2782_get_capacity(struct ds278x_info *info, int *capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	u8 raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	err = ds278x_read_reg(info, DS2782_REG_RARC, &raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	*capacity = raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static int ds2786_get_current(struct ds278x_info *info, int *current_uA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	s16 raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	*current_uA = (raw / 16) * (DS2786_CURRENT_UNITS / info->rsns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static int ds2786_get_voltage(struct ds278x_info *info, int *voltage_uV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	s16 raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	 * Voltage is measured in units of 1.22mV. The voltage is stored as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	 * a 12-bit number plus sign, in the upper bits of a 16-bit register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	*voltage_uV = (raw / 8) * 1220;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static int ds2786_get_capacity(struct ds278x_info *info, int *capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	u8 raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	err = ds278x_read_reg(info, DS2786_REG_RARC, &raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	/* Relative capacity is displayed with resolution 0.5 % */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	*capacity = raw/2 ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static int ds278x_get_status(struct ds278x_info *info, int *status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	int current_uA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	int capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	err = info->ops->get_battery_current(info, &current_uA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	err = info->ops->get_battery_capacity(info, &capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	info->capacity = capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (capacity == 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		*status = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	else if (current_uA == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		*status = POWER_SUPPLY_STATUS_NOT_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	else if (current_uA < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		*status = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		*status = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static int ds278x_battery_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 				       enum power_supply_property prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 				       union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	struct ds278x_info *info = to_ds278x_info(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	switch (prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		ret = ds278x_get_status(info, &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	case POWER_SUPPLY_PROP_CAPACITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		ret = info->ops->get_battery_capacity(info, &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		ret = info->ops->get_battery_voltage(info, &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		ret = info->ops->get_battery_current(info, &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	case POWER_SUPPLY_PROP_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		ret = ds278x_get_temp(info, &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static void ds278x_bat_update(struct ds278x_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	int old_status = info->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	int old_capacity = info->capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	ds278x_get_status(info, &info->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	if ((old_status != info->status) || (old_capacity != info->capacity))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		power_supply_changed(info->battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static void ds278x_bat_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	struct ds278x_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	info = container_of(work, struct ds278x_info, bat_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	ds278x_bat_update(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	schedule_delayed_work(&info->bat_work, DS278x_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static enum power_supply_property ds278x_battery_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	POWER_SUPPLY_PROP_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	POWER_SUPPLY_PROP_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static void ds278x_power_supply_init(struct power_supply_desc *battery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	battery->type			= POWER_SUPPLY_TYPE_BATTERY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	battery->properties		= ds278x_battery_props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	battery->num_properties		= ARRAY_SIZE(ds278x_battery_props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	battery->get_property		= ds278x_battery_get_property;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	battery->external_power_changed	= NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static int ds278x_battery_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	struct ds278x_info *info = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	int id = info->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	power_supply_unregister(info->battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	cancel_delayed_work_sync(&info->bat_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	kfree(info->battery_desc.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	kfree(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	mutex_lock(&battery_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	idr_remove(&battery_id, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	mutex_unlock(&battery_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static int ds278x_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	struct ds278x_info *info = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	cancel_delayed_work(&info->bat_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static int ds278x_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	struct ds278x_info *info = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	schedule_delayed_work(&info->bat_work, DS278x_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) #endif /* CONFIG_PM_SLEEP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static SIMPLE_DEV_PM_OPS(ds278x_battery_pm_ops, ds278x_suspend, ds278x_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) enum ds278x_num_id {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	DS2782 = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	DS2786,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static const struct ds278x_battery_ops ds278x_ops[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	[DS2782] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		.get_battery_current  = ds2782_get_current,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		.get_battery_voltage  = ds2782_get_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		.get_battery_capacity = ds2782_get_capacity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	[DS2786] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		.get_battery_current  = ds2786_get_current,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		.get_battery_voltage  = ds2786_get_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		.get_battery_capacity = ds2786_get_capacity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static int ds278x_battery_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 				const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	struct ds278x_platform_data *pdata = client->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	struct power_supply_config psy_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	struct ds278x_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	int num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	 * ds2786 should have the sense resistor value set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	 * in the platform data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (id->driver_data == DS2786 && !pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		dev_err(&client->dev, "missing platform data for ds2786\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	/* Get an ID for this battery */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	mutex_lock(&battery_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	ret = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	mutex_unlock(&battery_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		goto fail_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	num = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	info = kzalloc(sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	if (!info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		goto fail_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	info->battery_desc.name = kasprintf(GFP_KERNEL, "%s-%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 					    client->name, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	if (!info->battery_desc.name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		goto fail_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (id->driver_data == DS2786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		info->rsns = pdata->rsns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	i2c_set_clientdata(client, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	info->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	info->id = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	info->ops  = &ds278x_ops[id->driver_data];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	ds278x_power_supply_init(&info->battery_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	psy_cfg.drv_data = info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	info->capacity = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	info->status = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	INIT_DELAYED_WORK(&info->bat_work, ds278x_bat_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	info->battery = power_supply_register(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 					      &info->battery_desc, &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	if (IS_ERR(info->battery)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		dev_err(&client->dev, "failed to register battery\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		ret = PTR_ERR(info->battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		goto fail_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		schedule_delayed_work(&info->bat_work, DS278x_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) fail_register:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	kfree(info->battery_desc.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) fail_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	kfree(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) fail_info:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	mutex_lock(&battery_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	idr_remove(&battery_id, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	mutex_unlock(&battery_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) fail_id:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) static const struct i2c_device_id ds278x_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	{"ds2782", DS2782},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	{"ds2786", DS2786},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) MODULE_DEVICE_TABLE(i2c, ds278x_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) static struct i2c_driver ds278x_battery_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	.driver 	= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		.name	= "ds2782-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		.pm	= &ds278x_battery_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	.probe		= ds278x_battery_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	.remove		= ds278x_battery_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	.id_table	= ds278x_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) module_i2c_driver(ds278x_battery_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) MODULE_AUTHOR("Ryan Mallon");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) MODULE_DESCRIPTION("Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) MODULE_LICENSE("GPL");