1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * I2C client/driver for the Linear Technology LTC2941, LTC2942, LTC2943
4*4882a593Smuzhiyun * and LTC2944 Battery Gas Gauge IC
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (C) 2014 Topic Embedded Systems
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Author: Auryn Verwegen
9*4882a593Smuzhiyun * Author: Mike Looijmans
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/of_device.h>
14*4882a593Smuzhiyun #include <linux/types.h>
15*4882a593Smuzhiyun #include <linux/errno.h>
16*4882a593Smuzhiyun #include <linux/swab.h>
17*4882a593Smuzhiyun #include <linux/i2c.h>
18*4882a593Smuzhiyun #include <linux/delay.h>
19*4882a593Smuzhiyun #include <linux/power_supply.h>
20*4882a593Smuzhiyun #include <linux/slab.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #define I16_MSB(x) ((x >> 8) & 0xFF)
23*4882a593Smuzhiyun #define I16_LSB(x) (x & 0xFF)
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #define LTC294X_WORK_DELAY 10 /* Update delay in seconds */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #define LTC294X_MAX_VALUE 0xFFFF
28*4882a593Smuzhiyun #define LTC294X_MID_SUPPLY 0x7FFF
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #define LTC2941_MAX_PRESCALER_EXP 7
31*4882a593Smuzhiyun #define LTC2943_MAX_PRESCALER_EXP 6
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun enum ltc294x_reg {
34*4882a593Smuzhiyun LTC294X_REG_STATUS = 0x00,
35*4882a593Smuzhiyun LTC294X_REG_CONTROL = 0x01,
36*4882a593Smuzhiyun LTC294X_REG_ACC_CHARGE_MSB = 0x02,
37*4882a593Smuzhiyun LTC294X_REG_ACC_CHARGE_LSB = 0x03,
38*4882a593Smuzhiyun LTC294X_REG_CHARGE_THR_HIGH_MSB = 0x04,
39*4882a593Smuzhiyun LTC294X_REG_CHARGE_THR_HIGH_LSB = 0x05,
40*4882a593Smuzhiyun LTC294X_REG_CHARGE_THR_LOW_MSB = 0x06,
41*4882a593Smuzhiyun LTC294X_REG_CHARGE_THR_LOW_LSB = 0x07,
42*4882a593Smuzhiyun LTC294X_REG_VOLTAGE_MSB = 0x08,
43*4882a593Smuzhiyun LTC294X_REG_VOLTAGE_LSB = 0x09,
44*4882a593Smuzhiyun LTC2942_REG_TEMPERATURE_MSB = 0x0C,
45*4882a593Smuzhiyun LTC2942_REG_TEMPERATURE_LSB = 0x0D,
46*4882a593Smuzhiyun LTC2943_REG_CURRENT_MSB = 0x0E,
47*4882a593Smuzhiyun LTC2943_REG_CURRENT_LSB = 0x0F,
48*4882a593Smuzhiyun LTC2943_REG_TEMPERATURE_MSB = 0x14,
49*4882a593Smuzhiyun LTC2943_REG_TEMPERATURE_LSB = 0x15,
50*4882a593Smuzhiyun };
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun enum ltc294x_id {
53*4882a593Smuzhiyun LTC2941_ID,
54*4882a593Smuzhiyun LTC2942_ID,
55*4882a593Smuzhiyun LTC2943_ID,
56*4882a593Smuzhiyun LTC2944_ID,
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun #define LTC2941_REG_STATUS_CHIP_ID BIT(7)
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #define LTC2942_REG_CONTROL_MODE_SCAN (BIT(7) | BIT(6))
62*4882a593Smuzhiyun #define LTC2943_REG_CONTROL_MODE_SCAN BIT(7)
63*4882a593Smuzhiyun #define LTC294X_REG_CONTROL_PRESCALER_MASK (BIT(5) | BIT(4) | BIT(3))
64*4882a593Smuzhiyun #define LTC294X_REG_CONTROL_SHUTDOWN_MASK (BIT(0))
65*4882a593Smuzhiyun #define LTC294X_REG_CONTROL_PRESCALER_SET(x) \
66*4882a593Smuzhiyun ((x << 3) & LTC294X_REG_CONTROL_PRESCALER_MASK)
67*4882a593Smuzhiyun #define LTC294X_REG_CONTROL_ALCC_CONFIG_DISABLED 0
68*4882a593Smuzhiyun #define LTC294X_REG_CONTROL_ADC_DISABLE(x) ((x) & ~(BIT(7) | BIT(6)))
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun struct ltc294x_info {
71*4882a593Smuzhiyun struct i2c_client *client; /* I2C Client pointer */
72*4882a593Smuzhiyun struct power_supply *supply; /* Supply pointer */
73*4882a593Smuzhiyun struct power_supply_desc supply_desc; /* Supply description */
74*4882a593Smuzhiyun struct delayed_work work; /* Work scheduler */
75*4882a593Smuzhiyun enum ltc294x_id id; /* Chip type */
76*4882a593Smuzhiyun int charge; /* Last charge register content */
77*4882a593Smuzhiyun int r_sense; /* mOhm */
78*4882a593Smuzhiyun int Qlsb; /* nAh */
79*4882a593Smuzhiyun };
80*4882a593Smuzhiyun
convert_bin_to_uAh(const struct ltc294x_info * info,int Q)81*4882a593Smuzhiyun static inline int convert_bin_to_uAh(
82*4882a593Smuzhiyun const struct ltc294x_info *info, int Q)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun return ((Q * (info->Qlsb / 10))) / 100;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
convert_uAh_to_bin(const struct ltc294x_info * info,int uAh)87*4882a593Smuzhiyun static inline int convert_uAh_to_bin(
88*4882a593Smuzhiyun const struct ltc294x_info *info, int uAh)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun int Q;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun Q = (uAh * 100) / (info->Qlsb/10);
93*4882a593Smuzhiyun return (Q < LTC294X_MAX_VALUE) ? Q : LTC294X_MAX_VALUE;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
ltc294x_read_regs(struct i2c_client * client,enum ltc294x_reg reg,u8 * buf,int num_regs)96*4882a593Smuzhiyun static int ltc294x_read_regs(struct i2c_client *client,
97*4882a593Smuzhiyun enum ltc294x_reg reg, u8 *buf, int num_regs)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun int ret;
100*4882a593Smuzhiyun struct i2c_msg msgs[2] = { };
101*4882a593Smuzhiyun u8 reg_start = reg;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun msgs[0].addr = client->addr;
104*4882a593Smuzhiyun msgs[0].len = 1;
105*4882a593Smuzhiyun msgs[0].buf = ®_start;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun msgs[1].addr = client->addr;
108*4882a593Smuzhiyun msgs[1].len = num_regs;
109*4882a593Smuzhiyun msgs[1].buf = buf;
110*4882a593Smuzhiyun msgs[1].flags = I2C_M_RD;
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun ret = i2c_transfer(client->adapter, &msgs[0], 2);
113*4882a593Smuzhiyun if (ret < 0) {
114*4882a593Smuzhiyun dev_err(&client->dev, "ltc2941 read_reg failed!\n");
115*4882a593Smuzhiyun return ret;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun dev_dbg(&client->dev, "%s (%#x, %d) -> %#x\n",
119*4882a593Smuzhiyun __func__, reg, num_regs, *buf);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun return 0;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
ltc294x_write_regs(struct i2c_client * client,enum ltc294x_reg reg,const u8 * buf,int num_regs)124*4882a593Smuzhiyun static int ltc294x_write_regs(struct i2c_client *client,
125*4882a593Smuzhiyun enum ltc294x_reg reg, const u8 *buf, int num_regs)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun int ret;
128*4882a593Smuzhiyun u8 reg_start = reg;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun ret = i2c_smbus_write_i2c_block_data(client, reg_start, num_regs, buf);
131*4882a593Smuzhiyun if (ret < 0) {
132*4882a593Smuzhiyun dev_err(&client->dev, "ltc2941 write_reg failed!\n");
133*4882a593Smuzhiyun return ret;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun dev_dbg(&client->dev, "%s (%#x, %d) -> %#x\n",
137*4882a593Smuzhiyun __func__, reg, num_regs, *buf);
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun return 0;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
ltc294x_reset(const struct ltc294x_info * info,int prescaler_exp)142*4882a593Smuzhiyun static int ltc294x_reset(const struct ltc294x_info *info, int prescaler_exp)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun int ret;
145*4882a593Smuzhiyun u8 value;
146*4882a593Smuzhiyun u8 control;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /* Read status and control registers */
149*4882a593Smuzhiyun ret = ltc294x_read_regs(info->client, LTC294X_REG_CONTROL, &value, 1);
150*4882a593Smuzhiyun if (ret < 0) {
151*4882a593Smuzhiyun dev_err(&info->client->dev,
152*4882a593Smuzhiyun "Could not read registers from device\n");
153*4882a593Smuzhiyun goto error_exit;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun control = LTC294X_REG_CONTROL_PRESCALER_SET(prescaler_exp) |
157*4882a593Smuzhiyun LTC294X_REG_CONTROL_ALCC_CONFIG_DISABLED;
158*4882a593Smuzhiyun /* Put device into "monitor" mode */
159*4882a593Smuzhiyun switch (info->id) {
160*4882a593Smuzhiyun case LTC2942_ID: /* 2942 measures every 2 sec */
161*4882a593Smuzhiyun control |= LTC2942_REG_CONTROL_MODE_SCAN;
162*4882a593Smuzhiyun break;
163*4882a593Smuzhiyun case LTC2943_ID:
164*4882a593Smuzhiyun case LTC2944_ID: /* 2943 and 2944 measure every 10 sec */
165*4882a593Smuzhiyun control |= LTC2943_REG_CONTROL_MODE_SCAN;
166*4882a593Smuzhiyun break;
167*4882a593Smuzhiyun default:
168*4882a593Smuzhiyun break;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun if (value != control) {
172*4882a593Smuzhiyun ret = ltc294x_write_regs(info->client,
173*4882a593Smuzhiyun LTC294X_REG_CONTROL, &control, 1);
174*4882a593Smuzhiyun if (ret < 0) {
175*4882a593Smuzhiyun dev_err(&info->client->dev,
176*4882a593Smuzhiyun "Could not write register\n");
177*4882a593Smuzhiyun goto error_exit;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun return 0;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun error_exit:
184*4882a593Smuzhiyun return ret;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
ltc294x_read_charge_register(const struct ltc294x_info * info,enum ltc294x_reg reg)187*4882a593Smuzhiyun static int ltc294x_read_charge_register(const struct ltc294x_info *info,
188*4882a593Smuzhiyun enum ltc294x_reg reg)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun int ret;
191*4882a593Smuzhiyun u8 datar[2];
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun ret = ltc294x_read_regs(info->client, reg, &datar[0], 2);
194*4882a593Smuzhiyun if (ret < 0)
195*4882a593Smuzhiyun return ret;
196*4882a593Smuzhiyun return (datar[0] << 8) + datar[1];
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
ltc294x_get_charge(const struct ltc294x_info * info,enum ltc294x_reg reg,int * val)199*4882a593Smuzhiyun static int ltc294x_get_charge(const struct ltc294x_info *info,
200*4882a593Smuzhiyun enum ltc294x_reg reg, int *val)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun int value = ltc294x_read_charge_register(info, reg);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun if (value < 0)
205*4882a593Smuzhiyun return value;
206*4882a593Smuzhiyun /* When r_sense < 0, this counts up when the battery discharges */
207*4882a593Smuzhiyun if (info->Qlsb < 0)
208*4882a593Smuzhiyun value -= 0xFFFF;
209*4882a593Smuzhiyun *val = convert_bin_to_uAh(info, value);
210*4882a593Smuzhiyun return 0;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
ltc294x_set_charge_now(const struct ltc294x_info * info,int val)213*4882a593Smuzhiyun static int ltc294x_set_charge_now(const struct ltc294x_info *info, int val)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun int ret;
216*4882a593Smuzhiyun u8 dataw[2];
217*4882a593Smuzhiyun u8 ctrl_reg;
218*4882a593Smuzhiyun s32 value;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun value = convert_uAh_to_bin(info, val);
221*4882a593Smuzhiyun /* Direction depends on how sense+/- were connected */
222*4882a593Smuzhiyun if (info->Qlsb < 0)
223*4882a593Smuzhiyun value += 0xFFFF;
224*4882a593Smuzhiyun if ((value < 0) || (value > 0xFFFF)) /* input validation */
225*4882a593Smuzhiyun return -EINVAL;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun /* Read control register */
228*4882a593Smuzhiyun ret = ltc294x_read_regs(info->client,
229*4882a593Smuzhiyun LTC294X_REG_CONTROL, &ctrl_reg, 1);
230*4882a593Smuzhiyun if (ret < 0)
231*4882a593Smuzhiyun return ret;
232*4882a593Smuzhiyun /* Disable analog section */
233*4882a593Smuzhiyun ctrl_reg |= LTC294X_REG_CONTROL_SHUTDOWN_MASK;
234*4882a593Smuzhiyun ret = ltc294x_write_regs(info->client,
235*4882a593Smuzhiyun LTC294X_REG_CONTROL, &ctrl_reg, 1);
236*4882a593Smuzhiyun if (ret < 0)
237*4882a593Smuzhiyun return ret;
238*4882a593Smuzhiyun /* Set new charge value */
239*4882a593Smuzhiyun dataw[0] = I16_MSB(value);
240*4882a593Smuzhiyun dataw[1] = I16_LSB(value);
241*4882a593Smuzhiyun ret = ltc294x_write_regs(info->client,
242*4882a593Smuzhiyun LTC294X_REG_ACC_CHARGE_MSB, &dataw[0], 2);
243*4882a593Smuzhiyun if (ret < 0)
244*4882a593Smuzhiyun goto error_exit;
245*4882a593Smuzhiyun /* Enable analog section */
246*4882a593Smuzhiyun error_exit:
247*4882a593Smuzhiyun ctrl_reg &= ~LTC294X_REG_CONTROL_SHUTDOWN_MASK;
248*4882a593Smuzhiyun ret = ltc294x_write_regs(info->client,
249*4882a593Smuzhiyun LTC294X_REG_CONTROL, &ctrl_reg, 1);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun return ret < 0 ? ret : 0;
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
ltc294x_set_charge_thr(const struct ltc294x_info * info,enum ltc294x_reg reg,int val)254*4882a593Smuzhiyun static int ltc294x_set_charge_thr(const struct ltc294x_info *info,
255*4882a593Smuzhiyun enum ltc294x_reg reg, int val)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun u8 dataw[2];
258*4882a593Smuzhiyun s32 value;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun value = convert_uAh_to_bin(info, val);
261*4882a593Smuzhiyun /* Direction depends on how sense+/- were connected */
262*4882a593Smuzhiyun if (info->Qlsb < 0)
263*4882a593Smuzhiyun value += 0xFFFF;
264*4882a593Smuzhiyun if ((value < 0) || (value > 0xFFFF)) /* input validation */
265*4882a593Smuzhiyun return -EINVAL;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /* Set new charge value */
268*4882a593Smuzhiyun dataw[0] = I16_MSB(value);
269*4882a593Smuzhiyun dataw[1] = I16_LSB(value);
270*4882a593Smuzhiyun return ltc294x_write_regs(info->client, reg, &dataw[0], 2);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun
ltc294x_get_charge_counter(const struct ltc294x_info * info,int * val)273*4882a593Smuzhiyun static int ltc294x_get_charge_counter(
274*4882a593Smuzhiyun const struct ltc294x_info *info, int *val)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun int value = ltc294x_read_charge_register(info, LTC294X_REG_ACC_CHARGE_MSB);
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun if (value < 0)
279*4882a593Smuzhiyun return value;
280*4882a593Smuzhiyun value -= LTC294X_MID_SUPPLY;
281*4882a593Smuzhiyun *val = convert_bin_to_uAh(info, value);
282*4882a593Smuzhiyun return 0;
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun
ltc294x_get_voltage(const struct ltc294x_info * info,int * val)285*4882a593Smuzhiyun static int ltc294x_get_voltage(const struct ltc294x_info *info, int *val)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun int ret;
288*4882a593Smuzhiyun u8 datar[2];
289*4882a593Smuzhiyun u32 value;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun ret = ltc294x_read_regs(info->client,
292*4882a593Smuzhiyun LTC294X_REG_VOLTAGE_MSB, &datar[0], 2);
293*4882a593Smuzhiyun value = (datar[0] << 8) | datar[1];
294*4882a593Smuzhiyun switch (info->id) {
295*4882a593Smuzhiyun case LTC2943_ID:
296*4882a593Smuzhiyun value *= 23600 * 2;
297*4882a593Smuzhiyun value /= 0xFFFF;
298*4882a593Smuzhiyun value *= 1000 / 2;
299*4882a593Smuzhiyun break;
300*4882a593Smuzhiyun case LTC2944_ID:
301*4882a593Smuzhiyun value *= 70800 / 5*4;
302*4882a593Smuzhiyun value /= 0xFFFF;
303*4882a593Smuzhiyun value *= 1000 * 5/4;
304*4882a593Smuzhiyun break;
305*4882a593Smuzhiyun default:
306*4882a593Smuzhiyun value *= 6000 * 10;
307*4882a593Smuzhiyun value /= 0xFFFF;
308*4882a593Smuzhiyun value *= 1000 / 10;
309*4882a593Smuzhiyun break;
310*4882a593Smuzhiyun }
311*4882a593Smuzhiyun *val = value;
312*4882a593Smuzhiyun return ret;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun
ltc294x_get_current(const struct ltc294x_info * info,int * val)315*4882a593Smuzhiyun static int ltc294x_get_current(const struct ltc294x_info *info, int *val)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun int ret;
318*4882a593Smuzhiyun u8 datar[2];
319*4882a593Smuzhiyun s32 value;
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun ret = ltc294x_read_regs(info->client,
322*4882a593Smuzhiyun LTC2943_REG_CURRENT_MSB, &datar[0], 2);
323*4882a593Smuzhiyun value = (datar[0] << 8) | datar[1];
324*4882a593Smuzhiyun value -= 0x7FFF;
325*4882a593Smuzhiyun if (info->id == LTC2944_ID)
326*4882a593Smuzhiyun value *= 64000;
327*4882a593Smuzhiyun else
328*4882a593Smuzhiyun value *= 60000;
329*4882a593Smuzhiyun /* Value is in range -32k..+32k, r_sense is usually 10..50 mOhm,
330*4882a593Smuzhiyun * the formula below keeps everything in s32 range while preserving
331*4882a593Smuzhiyun * enough digits */
332*4882a593Smuzhiyun *val = 1000 * (value / (info->r_sense * 0x7FFF)); /* in uA */
333*4882a593Smuzhiyun return ret;
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun
ltc294x_get_temperature(const struct ltc294x_info * info,int * val)336*4882a593Smuzhiyun static int ltc294x_get_temperature(const struct ltc294x_info *info, int *val)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun enum ltc294x_reg reg;
339*4882a593Smuzhiyun int ret;
340*4882a593Smuzhiyun u8 datar[2];
341*4882a593Smuzhiyun u32 value;
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun if (info->id == LTC2942_ID) {
344*4882a593Smuzhiyun reg = LTC2942_REG_TEMPERATURE_MSB;
345*4882a593Smuzhiyun value = 6000; /* Full-scale is 600 Kelvin */
346*4882a593Smuzhiyun } else {
347*4882a593Smuzhiyun reg = LTC2943_REG_TEMPERATURE_MSB;
348*4882a593Smuzhiyun value = 5100; /* Full-scale is 510 Kelvin */
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun ret = ltc294x_read_regs(info->client, reg, &datar[0], 2);
351*4882a593Smuzhiyun value *= (datar[0] << 8) | datar[1];
352*4882a593Smuzhiyun /* Convert to tenths of degree Celsius */
353*4882a593Smuzhiyun *val = value / 0xFFFF - 2722;
354*4882a593Smuzhiyun return ret;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun
ltc294x_get_property(struct power_supply * psy,enum power_supply_property prop,union power_supply_propval * val)357*4882a593Smuzhiyun static int ltc294x_get_property(struct power_supply *psy,
358*4882a593Smuzhiyun enum power_supply_property prop,
359*4882a593Smuzhiyun union power_supply_propval *val)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun struct ltc294x_info *info = power_supply_get_drvdata(psy);
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun switch (prop) {
364*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_FULL:
365*4882a593Smuzhiyun return ltc294x_get_charge(info, LTC294X_REG_CHARGE_THR_HIGH_MSB,
366*4882a593Smuzhiyun &val->intval);
367*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_EMPTY:
368*4882a593Smuzhiyun return ltc294x_get_charge(info, LTC294X_REG_CHARGE_THR_LOW_MSB,
369*4882a593Smuzhiyun &val->intval);
370*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_NOW:
371*4882a593Smuzhiyun return ltc294x_get_charge(info, LTC294X_REG_ACC_CHARGE_MSB,
372*4882a593Smuzhiyun &val->intval);
373*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_COUNTER:
374*4882a593Smuzhiyun return ltc294x_get_charge_counter(info, &val->intval);
375*4882a593Smuzhiyun case POWER_SUPPLY_PROP_VOLTAGE_NOW:
376*4882a593Smuzhiyun return ltc294x_get_voltage(info, &val->intval);
377*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CURRENT_NOW:
378*4882a593Smuzhiyun return ltc294x_get_current(info, &val->intval);
379*4882a593Smuzhiyun case POWER_SUPPLY_PROP_TEMP:
380*4882a593Smuzhiyun return ltc294x_get_temperature(info, &val->intval);
381*4882a593Smuzhiyun default:
382*4882a593Smuzhiyun return -EINVAL;
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun
ltc294x_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)386*4882a593Smuzhiyun static int ltc294x_set_property(struct power_supply *psy,
387*4882a593Smuzhiyun enum power_supply_property psp,
388*4882a593Smuzhiyun const union power_supply_propval *val)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun struct ltc294x_info *info = power_supply_get_drvdata(psy);
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun switch (psp) {
393*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_FULL:
394*4882a593Smuzhiyun return ltc294x_set_charge_thr(info,
395*4882a593Smuzhiyun LTC294X_REG_CHARGE_THR_HIGH_MSB, val->intval);
396*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_EMPTY:
397*4882a593Smuzhiyun return ltc294x_set_charge_thr(info,
398*4882a593Smuzhiyun LTC294X_REG_CHARGE_THR_LOW_MSB, val->intval);
399*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_NOW:
400*4882a593Smuzhiyun return ltc294x_set_charge_now(info, val->intval);
401*4882a593Smuzhiyun default:
402*4882a593Smuzhiyun return -EPERM;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun }
405*4882a593Smuzhiyun
ltc294x_property_is_writeable(struct power_supply * psy,enum power_supply_property psp)406*4882a593Smuzhiyun static int ltc294x_property_is_writeable(
407*4882a593Smuzhiyun struct power_supply *psy, enum power_supply_property psp)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun switch (psp) {
410*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_FULL:
411*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_EMPTY:
412*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_NOW:
413*4882a593Smuzhiyun return 1;
414*4882a593Smuzhiyun default:
415*4882a593Smuzhiyun return 0;
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
ltc294x_update(struct ltc294x_info * info)419*4882a593Smuzhiyun static void ltc294x_update(struct ltc294x_info *info)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun int charge = ltc294x_read_charge_register(info, LTC294X_REG_ACC_CHARGE_MSB);
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun if (charge != info->charge) {
424*4882a593Smuzhiyun info->charge = charge;
425*4882a593Smuzhiyun power_supply_changed(info->supply);
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun
ltc294x_work(struct work_struct * work)429*4882a593Smuzhiyun static void ltc294x_work(struct work_struct *work)
430*4882a593Smuzhiyun {
431*4882a593Smuzhiyun struct ltc294x_info *info;
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun info = container_of(work, struct ltc294x_info, work.work);
434*4882a593Smuzhiyun ltc294x_update(info);
435*4882a593Smuzhiyun schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun static enum power_supply_property ltc294x_properties[] = {
439*4882a593Smuzhiyun POWER_SUPPLY_PROP_CHARGE_COUNTER,
440*4882a593Smuzhiyun POWER_SUPPLY_PROP_CHARGE_FULL,
441*4882a593Smuzhiyun POWER_SUPPLY_PROP_CHARGE_EMPTY,
442*4882a593Smuzhiyun POWER_SUPPLY_PROP_CHARGE_NOW,
443*4882a593Smuzhiyun POWER_SUPPLY_PROP_VOLTAGE_NOW,
444*4882a593Smuzhiyun POWER_SUPPLY_PROP_TEMP,
445*4882a593Smuzhiyun POWER_SUPPLY_PROP_CURRENT_NOW,
446*4882a593Smuzhiyun };
447*4882a593Smuzhiyun
ltc294x_i2c_remove(struct i2c_client * client)448*4882a593Smuzhiyun static int ltc294x_i2c_remove(struct i2c_client *client)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun struct ltc294x_info *info = i2c_get_clientdata(client);
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun cancel_delayed_work_sync(&info->work);
453*4882a593Smuzhiyun power_supply_unregister(info->supply);
454*4882a593Smuzhiyun return 0;
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun
ltc294x_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)457*4882a593Smuzhiyun static int ltc294x_i2c_probe(struct i2c_client *client,
458*4882a593Smuzhiyun const struct i2c_device_id *id)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun struct power_supply_config psy_cfg = {};
461*4882a593Smuzhiyun struct ltc294x_info *info;
462*4882a593Smuzhiyun struct device_node *np;
463*4882a593Smuzhiyun int ret;
464*4882a593Smuzhiyun u32 prescaler_exp;
465*4882a593Smuzhiyun s32 r_sense;
466*4882a593Smuzhiyun u8 status;
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
469*4882a593Smuzhiyun if (info == NULL)
470*4882a593Smuzhiyun return -ENOMEM;
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun i2c_set_clientdata(client, info);
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun np = of_node_get(client->dev.of_node);
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun info->id = (enum ltc294x_id) (uintptr_t) of_device_get_match_data(
477*4882a593Smuzhiyun &client->dev);
478*4882a593Smuzhiyun info->supply_desc.name = np->name;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun /* r_sense can be negative, when sense+ is connected to the battery
481*4882a593Smuzhiyun * instead of the sense-. This results in reversed measurements. */
482*4882a593Smuzhiyun ret = of_property_read_u32(np, "lltc,resistor-sense", &r_sense);
483*4882a593Smuzhiyun if (ret < 0) {
484*4882a593Smuzhiyun dev_err(&client->dev,
485*4882a593Smuzhiyun "Could not find lltc,resistor-sense in devicetree\n");
486*4882a593Smuzhiyun return ret;
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun info->r_sense = r_sense;
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun ret = of_property_read_u32(np, "lltc,prescaler-exponent",
491*4882a593Smuzhiyun &prescaler_exp);
492*4882a593Smuzhiyun if (ret < 0) {
493*4882a593Smuzhiyun dev_warn(&client->dev,
494*4882a593Smuzhiyun "lltc,prescaler-exponent not in devicetree\n");
495*4882a593Smuzhiyun prescaler_exp = LTC2941_MAX_PRESCALER_EXP;
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun if (info->id == LTC2943_ID) {
499*4882a593Smuzhiyun if (prescaler_exp > LTC2943_MAX_PRESCALER_EXP)
500*4882a593Smuzhiyun prescaler_exp = LTC2943_MAX_PRESCALER_EXP;
501*4882a593Smuzhiyun info->Qlsb = ((340 * 50000) / r_sense) /
502*4882a593Smuzhiyun (4096 / (1 << (2*prescaler_exp)));
503*4882a593Smuzhiyun } else {
504*4882a593Smuzhiyun if (prescaler_exp > LTC2941_MAX_PRESCALER_EXP)
505*4882a593Smuzhiyun prescaler_exp = LTC2941_MAX_PRESCALER_EXP;
506*4882a593Smuzhiyun info->Qlsb = ((85 * 50000) / r_sense) /
507*4882a593Smuzhiyun (128 / (1 << prescaler_exp));
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun /* Read status register to check for LTC2942 */
511*4882a593Smuzhiyun if (info->id == LTC2941_ID || info->id == LTC2942_ID) {
512*4882a593Smuzhiyun ret = ltc294x_read_regs(client, LTC294X_REG_STATUS, &status, 1);
513*4882a593Smuzhiyun if (ret < 0) {
514*4882a593Smuzhiyun dev_err(&client->dev,
515*4882a593Smuzhiyun "Could not read status register\n");
516*4882a593Smuzhiyun return ret;
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun if (status & LTC2941_REG_STATUS_CHIP_ID)
519*4882a593Smuzhiyun info->id = LTC2941_ID;
520*4882a593Smuzhiyun else
521*4882a593Smuzhiyun info->id = LTC2942_ID;
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun info->client = client;
525*4882a593Smuzhiyun info->supply_desc.type = POWER_SUPPLY_TYPE_BATTERY;
526*4882a593Smuzhiyun info->supply_desc.properties = ltc294x_properties;
527*4882a593Smuzhiyun switch (info->id) {
528*4882a593Smuzhiyun case LTC2944_ID:
529*4882a593Smuzhiyun case LTC2943_ID:
530*4882a593Smuzhiyun info->supply_desc.num_properties =
531*4882a593Smuzhiyun ARRAY_SIZE(ltc294x_properties);
532*4882a593Smuzhiyun break;
533*4882a593Smuzhiyun case LTC2942_ID:
534*4882a593Smuzhiyun info->supply_desc.num_properties =
535*4882a593Smuzhiyun ARRAY_SIZE(ltc294x_properties) - 1;
536*4882a593Smuzhiyun break;
537*4882a593Smuzhiyun case LTC2941_ID:
538*4882a593Smuzhiyun default:
539*4882a593Smuzhiyun info->supply_desc.num_properties =
540*4882a593Smuzhiyun ARRAY_SIZE(ltc294x_properties) - 3;
541*4882a593Smuzhiyun break;
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun info->supply_desc.get_property = ltc294x_get_property;
544*4882a593Smuzhiyun info->supply_desc.set_property = ltc294x_set_property;
545*4882a593Smuzhiyun info->supply_desc.property_is_writeable = ltc294x_property_is_writeable;
546*4882a593Smuzhiyun info->supply_desc.external_power_changed = NULL;
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun psy_cfg.drv_data = info;
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun INIT_DELAYED_WORK(&info->work, ltc294x_work);
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun ret = ltc294x_reset(info, prescaler_exp);
553*4882a593Smuzhiyun if (ret < 0) {
554*4882a593Smuzhiyun dev_err(&client->dev, "Communication with chip failed\n");
555*4882a593Smuzhiyun return ret;
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun info->supply = power_supply_register(&client->dev, &info->supply_desc,
559*4882a593Smuzhiyun &psy_cfg);
560*4882a593Smuzhiyun if (IS_ERR(info->supply)) {
561*4882a593Smuzhiyun dev_err(&client->dev, "failed to register ltc2941\n");
562*4882a593Smuzhiyun return PTR_ERR(info->supply);
563*4882a593Smuzhiyun } else {
564*4882a593Smuzhiyun schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun return 0;
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun
ltc294x_i2c_shutdown(struct i2c_client * client)570*4882a593Smuzhiyun static void ltc294x_i2c_shutdown(struct i2c_client *client)
571*4882a593Smuzhiyun {
572*4882a593Smuzhiyun struct ltc294x_info *info = i2c_get_clientdata(client);
573*4882a593Smuzhiyun int ret;
574*4882a593Smuzhiyun u8 value;
575*4882a593Smuzhiyun u8 control;
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun /* The LTC2941 does not need any special handling */
578*4882a593Smuzhiyun if (info->id == LTC2941_ID)
579*4882a593Smuzhiyun return;
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun /* Read control register */
582*4882a593Smuzhiyun ret = ltc294x_read_regs(info->client, LTC294X_REG_CONTROL, &value, 1);
583*4882a593Smuzhiyun if (ret < 0)
584*4882a593Smuzhiyun return;
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun /* Disable continuous ADC conversion as this drains the battery */
587*4882a593Smuzhiyun control = LTC294X_REG_CONTROL_ADC_DISABLE(value);
588*4882a593Smuzhiyun if (control != value)
589*4882a593Smuzhiyun ltc294x_write_regs(info->client, LTC294X_REG_CONTROL,
590*4882a593Smuzhiyun &control, 1);
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
594*4882a593Smuzhiyun
ltc294x_suspend(struct device * dev)595*4882a593Smuzhiyun static int ltc294x_suspend(struct device *dev)
596*4882a593Smuzhiyun {
597*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
598*4882a593Smuzhiyun struct ltc294x_info *info = i2c_get_clientdata(client);
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun cancel_delayed_work(&info->work);
601*4882a593Smuzhiyun return 0;
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun
ltc294x_resume(struct device * dev)604*4882a593Smuzhiyun static int ltc294x_resume(struct device *dev)
605*4882a593Smuzhiyun {
606*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
607*4882a593Smuzhiyun struct ltc294x_info *info = i2c_get_clientdata(client);
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
610*4882a593Smuzhiyun return 0;
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(ltc294x_pm_ops, ltc294x_suspend, ltc294x_resume);
614*4882a593Smuzhiyun #define LTC294X_PM_OPS (<c294x_pm_ops)
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun #else
617*4882a593Smuzhiyun #define LTC294X_PM_OPS NULL
618*4882a593Smuzhiyun #endif /* CONFIG_PM_SLEEP */
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun static const struct i2c_device_id ltc294x_i2c_id[] = {
622*4882a593Smuzhiyun { "ltc2941", LTC2941_ID, },
623*4882a593Smuzhiyun { "ltc2942", LTC2942_ID, },
624*4882a593Smuzhiyun { "ltc2943", LTC2943_ID, },
625*4882a593Smuzhiyun { "ltc2944", LTC2944_ID, },
626*4882a593Smuzhiyun { },
627*4882a593Smuzhiyun };
628*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, ltc294x_i2c_id);
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun static const struct of_device_id ltc294x_i2c_of_match[] = {
631*4882a593Smuzhiyun {
632*4882a593Smuzhiyun .compatible = "lltc,ltc2941",
633*4882a593Smuzhiyun .data = (void *)LTC2941_ID,
634*4882a593Smuzhiyun },
635*4882a593Smuzhiyun {
636*4882a593Smuzhiyun .compatible = "lltc,ltc2942",
637*4882a593Smuzhiyun .data = (void *)LTC2942_ID,
638*4882a593Smuzhiyun },
639*4882a593Smuzhiyun {
640*4882a593Smuzhiyun .compatible = "lltc,ltc2943",
641*4882a593Smuzhiyun .data = (void *)LTC2943_ID,
642*4882a593Smuzhiyun },
643*4882a593Smuzhiyun {
644*4882a593Smuzhiyun .compatible = "lltc,ltc2944",
645*4882a593Smuzhiyun .data = (void *)LTC2944_ID,
646*4882a593Smuzhiyun },
647*4882a593Smuzhiyun { },
648*4882a593Smuzhiyun };
649*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, ltc294x_i2c_of_match);
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun static struct i2c_driver ltc294x_driver = {
652*4882a593Smuzhiyun .driver = {
653*4882a593Smuzhiyun .name = "LTC2941",
654*4882a593Smuzhiyun .of_match_table = ltc294x_i2c_of_match,
655*4882a593Smuzhiyun .pm = LTC294X_PM_OPS,
656*4882a593Smuzhiyun },
657*4882a593Smuzhiyun .probe = ltc294x_i2c_probe,
658*4882a593Smuzhiyun .remove = ltc294x_i2c_remove,
659*4882a593Smuzhiyun .shutdown = ltc294x_i2c_shutdown,
660*4882a593Smuzhiyun .id_table = ltc294x_i2c_id,
661*4882a593Smuzhiyun };
662*4882a593Smuzhiyun module_i2c_driver(ltc294x_driver);
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun MODULE_AUTHOR("Auryn Verwegen, Topic Embedded Systems");
665*4882a593Smuzhiyun MODULE_AUTHOR("Mike Looijmans, Topic Embedded Products");
666*4882a593Smuzhiyun MODULE_DESCRIPTION("LTC2941/LTC2942/LTC2943/LTC2944 Battery Gas Gauge IC driver");
667*4882a593Smuzhiyun MODULE_LICENSE("GPL");
668