xref: /linux/drivers/power/supply/ltc4162-l-charger.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1cd900f18SMike Looijmans // SPDX-License-Identifier: GPL-2.0-or-later
2cd900f18SMike Looijmans /*
3cd900f18SMike Looijmans  *  Driver for Analog Devices (Linear Technology) LTC4162-L charger IC.
4cd900f18SMike Looijmans  *  Copyright (C) 2020, Topic Embedded Products
5cd900f18SMike Looijmans  */
6cd900f18SMike Looijmans 
7cd900f18SMike Looijmans #include <linux/module.h>
8cd900f18SMike Looijmans #include <linux/delay.h>
92ce8284cSRob Herring #include <linux/of.h>
10cd900f18SMike Looijmans #include <linux/pm_runtime.h>
11cd900f18SMike Looijmans #include <linux/power_supply.h>
12cd900f18SMike Looijmans #include <linux/i2c.h>
13cd900f18SMike Looijmans #include <linux/regmap.h>
14cd900f18SMike Looijmans 
15cd900f18SMike Looijmans /* Registers (names based on what datasheet uses) */
16cd900f18SMike Looijmans #define LTC4162L_EN_LIMIT_ALERTS_REG		0x0D
17cd900f18SMike Looijmans #define LTC4162L_EN_CHARGER_STATE_ALERTS_REG	0x0E
18cd900f18SMike Looijmans #define LTC4162L_EN_CHARGE_STATUS_ALERTS_REG	0x0F
19cd900f18SMike Looijmans #define LTC4162L_CONFIG_BITS_REG		0x14
20cd900f18SMike Looijmans #define LTC4162L_IIN_LIMIT_TARGET		0x15
21cd900f18SMike Looijmans #define LTC4162L_ARM_SHIP_MODE			0x19
22cd900f18SMike Looijmans #define LTC4162L_CHARGE_CURRENT_SETTING		0X1A
23cd900f18SMike Looijmans #define LTC4162L_VCHARGE_SETTING		0X1B
24cd900f18SMike Looijmans #define LTC4162L_C_OVER_X_THRESHOLD		0x1C
25cd900f18SMike Looijmans #define LTC4162L_MAX_CV_TIME			0X1D
26cd900f18SMike Looijmans #define LTC4162L_MAX_CHARGE_TIME		0X1E
27cd900f18SMike Looijmans #define LTC4162L_CHARGER_CONFIG_BITS		0x29
28cd900f18SMike Looijmans #define LTC4162L_CHARGER_STATE			0x34
29cd900f18SMike Looijmans #define LTC4162L_CHARGE_STATUS			0x35
30cd900f18SMike Looijmans #define LTC4162L_LIMIT_ALERTS_REG		0x36
31cd900f18SMike Looijmans #define LTC4162L_CHARGER_STATE_ALERTS_REG	0x37
32cd900f18SMike Looijmans #define LTC4162L_CHARGE_STATUS_ALERTS_REG	0x38
33cd900f18SMike Looijmans #define LTC4162L_SYSTEM_STATUS_REG		0x39
34cd900f18SMike Looijmans #define LTC4162L_VBAT				0x3A
35cd900f18SMike Looijmans #define LTC4162L_VIN				0x3B
36cd900f18SMike Looijmans #define LTC4162L_VOUT				0x3C
37cd900f18SMike Looijmans #define LTC4162L_IBAT				0x3D
38cd900f18SMike Looijmans #define LTC4162L_IIN				0x3E
39cd900f18SMike Looijmans #define LTC4162L_DIE_TEMPERATURE		0x3F
40cd900f18SMike Looijmans #define LTC4162L_THERMISTOR_VOLTAGE		0x40
41cd900f18SMike Looijmans #define LTC4162L_BSR				0x41
42cd900f18SMike Looijmans #define LTC4162L_JEITA_REGION			0x42
43cd900f18SMike Looijmans #define LTC4162L_CHEM_CELLS_REG			0x43
44cd900f18SMike Looijmans #define LTC4162L_ICHARGE_DAC			0x44
45cd900f18SMike Looijmans #define LTC4162L_VCHARGE_DAC			0x45
46cd900f18SMike Looijmans #define LTC4162L_IIN_LIMIT_DAC			0x46
47cd900f18SMike Looijmans #define LTC4162L_VBAT_FILT			0x47
48cd900f18SMike Looijmans #define LTC4162L_INPUT_UNDERVOLTAGE_DAC		0x4B
49cd900f18SMike Looijmans 
50cd900f18SMike Looijmans /* Enumeration as in datasheet. Individual bits are mutually exclusive. */
51cd900f18SMike Looijmans enum ltc4162l_state {
52cd900f18SMike Looijmans 	battery_detection = 2048,
53cd900f18SMike Looijmans 	charger_suspended = 256,
54cd900f18SMike Looijmans 	precharge = 128,   /* trickle on low bat voltage */
55cd900f18SMike Looijmans 	cc_cv_charge = 64, /* normal charge */
56cd900f18SMike Looijmans 	ntc_pause = 32,
57cd900f18SMike Looijmans 	timer_term = 16,
58cd900f18SMike Looijmans 	c_over_x_term = 8, /* battery is full */
59cd900f18SMike Looijmans 	max_charge_time_fault = 4,
60cd900f18SMike Looijmans 	bat_missing_fault = 2,
61cd900f18SMike Looijmans 	bat_short_fault = 1
62cd900f18SMike Looijmans };
63cd900f18SMike Looijmans 
64cd900f18SMike Looijmans /* Individual bits are mutually exclusive. Only active in charging states.*/
65cd900f18SMike Looijmans enum ltc4162l_charge_status {
66cd900f18SMike Looijmans 	ilim_reg_active = 32,
67cd900f18SMike Looijmans 	thermal_reg_active = 16,
68cd900f18SMike Looijmans 	vin_uvcl_active = 8,
69cd900f18SMike Looijmans 	iin_limit_active = 4,
70cd900f18SMike Looijmans 	constant_current = 2,
71cd900f18SMike Looijmans 	constant_voltage = 1,
72cd900f18SMike Looijmans 	charger_off = 0
73cd900f18SMike Looijmans };
74cd900f18SMike Looijmans 
75cd900f18SMike Looijmans /* Magic number to write to ARM_SHIP_MODE register */
76cd900f18SMike Looijmans #define LTC4162L_ARM_SHIP_MODE_MAGIC 21325
77cd900f18SMike Looijmans 
78cd900f18SMike Looijmans struct ltc4162l_info {
79cd900f18SMike Looijmans 	struct i2c_client	*client;
80cd900f18SMike Looijmans 	struct regmap		*regmap;
81cd900f18SMike Looijmans 	struct power_supply	*charger;
82cd900f18SMike Looijmans 	u32 rsnsb;	/* Series resistor that sets charge current, microOhm */
83cd900f18SMike Looijmans 	u32 rsnsi;	/* Series resistor to measure input current, microOhm */
84cd900f18SMike Looijmans 	u8 cell_count;	/* Number of connected cells, 0 while unknown */
85cd900f18SMike Looijmans };
86cd900f18SMike Looijmans 
ltc4162l_get_cell_count(struct ltc4162l_info * info)87cd900f18SMike Looijmans static u8 ltc4162l_get_cell_count(struct ltc4162l_info *info)
88cd900f18SMike Looijmans {
89cd900f18SMike Looijmans 	int ret;
90cd900f18SMike Looijmans 	unsigned int val;
91cd900f18SMike Looijmans 
92cd900f18SMike Looijmans 	/* Once read successfully */
93cd900f18SMike Looijmans 	if (info->cell_count)
94cd900f18SMike Looijmans 		return info->cell_count;
95cd900f18SMike Looijmans 
96cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_CHEM_CELLS_REG, &val);
97cd900f18SMike Looijmans 	if (ret)
98cd900f18SMike Looijmans 		return 0;
99cd900f18SMike Looijmans 
100cd900f18SMike Looijmans 	/* Lower 4 bits is the cell count, or 0 if the chip doesn't know yet */
101cd900f18SMike Looijmans 	val &= 0x0f;
102cd900f18SMike Looijmans 	if (!val)
103cd900f18SMike Looijmans 		return 0;
104cd900f18SMike Looijmans 
105cd900f18SMike Looijmans 	/* Once determined, keep the value */
106cd900f18SMike Looijmans 	info->cell_count = val;
107cd900f18SMike Looijmans 
108cd900f18SMike Looijmans 	return val;
109cd900f18SMike Looijmans };
110cd900f18SMike Looijmans 
111cd900f18SMike Looijmans /* Convert enum value to POWER_SUPPLY_STATUS value */
ltc4162l_state_decode(enum ltc4162l_state value)112cd900f18SMike Looijmans static int ltc4162l_state_decode(enum ltc4162l_state value)
113cd900f18SMike Looijmans {
114cd900f18SMike Looijmans 	switch (value) {
115cd900f18SMike Looijmans 	case precharge:
116cd900f18SMike Looijmans 	case cc_cv_charge:
117cd900f18SMike Looijmans 		return POWER_SUPPLY_STATUS_CHARGING;
118cd900f18SMike Looijmans 	case c_over_x_term:
119cd900f18SMike Looijmans 		return POWER_SUPPLY_STATUS_FULL;
120cd900f18SMike Looijmans 	case bat_missing_fault:
121cd900f18SMike Looijmans 	case bat_short_fault:
122cd900f18SMike Looijmans 		return POWER_SUPPLY_STATUS_UNKNOWN;
123cd900f18SMike Looijmans 	default:
124cd900f18SMike Looijmans 		return POWER_SUPPLY_STATUS_NOT_CHARGING;
125cd900f18SMike Looijmans 	}
126cd900f18SMike Looijmans };
127cd900f18SMike Looijmans 
ltc4162l_get_status(struct ltc4162l_info * info,union power_supply_propval * val)128cd900f18SMike Looijmans static int ltc4162l_get_status(struct ltc4162l_info *info,
129cd900f18SMike Looijmans 			       union power_supply_propval *val)
130cd900f18SMike Looijmans {
131cd900f18SMike Looijmans 	unsigned int regval;
132cd900f18SMike Looijmans 	int ret;
133cd900f18SMike Looijmans 
134cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_CHARGER_STATE, &regval);
135cd900f18SMike Looijmans 	if (ret) {
136cd900f18SMike Looijmans 		dev_err(&info->client->dev, "Failed to read CHARGER_STATE\n");
137cd900f18SMike Looijmans 		return ret;
138cd900f18SMike Looijmans 	}
139cd900f18SMike Looijmans 
140cd900f18SMike Looijmans 	val->intval = ltc4162l_state_decode(regval);
141cd900f18SMike Looijmans 
142cd900f18SMike Looijmans 	return 0;
143cd900f18SMike Looijmans }
144cd900f18SMike Looijmans 
ltc4162l_charge_status_decode(enum ltc4162l_charge_status value)145cd900f18SMike Looijmans static int ltc4162l_charge_status_decode(enum ltc4162l_charge_status value)
146cd900f18SMike Looijmans {
147cd900f18SMike Looijmans 	if (!value)
148cd900f18SMike Looijmans 		return POWER_SUPPLY_CHARGE_TYPE_NONE;
149cd900f18SMike Looijmans 
150cd900f18SMike Looijmans 	/* constant voltage/current and input_current limit are "fast" modes */
151cd900f18SMike Looijmans 	if (value <= iin_limit_active)
152cd900f18SMike Looijmans 		return POWER_SUPPLY_CHARGE_TYPE_FAST;
153cd900f18SMike Looijmans 
154cd900f18SMike Looijmans 	/* Anything that's not fast we'll return as trickle */
155cd900f18SMike Looijmans 	return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
156cd900f18SMike Looijmans }
157cd900f18SMike Looijmans 
ltc4162l_get_charge_type(struct ltc4162l_info * info,union power_supply_propval * val)158cd900f18SMike Looijmans static int ltc4162l_get_charge_type(struct ltc4162l_info *info,
159cd900f18SMike Looijmans 				    union power_supply_propval *val)
160cd900f18SMike Looijmans {
161cd900f18SMike Looijmans 	unsigned int regval;
162cd900f18SMike Looijmans 	int ret;
163cd900f18SMike Looijmans 
164cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_CHARGE_STATUS, &regval);
165cd900f18SMike Looijmans 	if (ret)
166cd900f18SMike Looijmans 		return ret;
167cd900f18SMike Looijmans 
168cd900f18SMike Looijmans 	val->intval = ltc4162l_charge_status_decode(regval);
169cd900f18SMike Looijmans 
170cd900f18SMike Looijmans 	return 0;
171cd900f18SMike Looijmans }
172cd900f18SMike Looijmans 
ltc4162l_state_to_health(enum ltc4162l_state value)173cd900f18SMike Looijmans static int ltc4162l_state_to_health(enum ltc4162l_state value)
174cd900f18SMike Looijmans {
175cd900f18SMike Looijmans 	switch (value) {
176cd900f18SMike Looijmans 	case ntc_pause:
177cd900f18SMike Looijmans 		return POWER_SUPPLY_HEALTH_OVERHEAT;
178cd900f18SMike Looijmans 	case timer_term:
179cd900f18SMike Looijmans 		return POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
180cd900f18SMike Looijmans 	case max_charge_time_fault:
181cd900f18SMike Looijmans 		return POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE;
182cd900f18SMike Looijmans 	case bat_missing_fault:
183cd900f18SMike Looijmans 		return POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
184cd900f18SMike Looijmans 	case bat_short_fault:
185cd900f18SMike Looijmans 		return POWER_SUPPLY_HEALTH_DEAD;
186cd900f18SMike Looijmans 	default:
187cd900f18SMike Looijmans 		return POWER_SUPPLY_HEALTH_GOOD;
188cd900f18SMike Looijmans 	}
189cd900f18SMike Looijmans }
190cd900f18SMike Looijmans 
ltc4162l_get_health(struct ltc4162l_info * info,union power_supply_propval * val)191cd900f18SMike Looijmans static int ltc4162l_get_health(struct ltc4162l_info *info,
192cd900f18SMike Looijmans 			       union power_supply_propval *val)
193cd900f18SMike Looijmans {
194cd900f18SMike Looijmans 	unsigned int regval;
195cd900f18SMike Looijmans 	int ret;
196cd900f18SMike Looijmans 
197cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_CHARGER_STATE, &regval);
198cd900f18SMike Looijmans 	if (ret)
199cd900f18SMike Looijmans 		return ret;
200cd900f18SMike Looijmans 
201cd900f18SMike Looijmans 	val->intval = ltc4162l_state_to_health(regval);
202cd900f18SMike Looijmans 
203cd900f18SMike Looijmans 	return 0;
204cd900f18SMike Looijmans }
205cd900f18SMike Looijmans 
ltc4162l_get_online(struct ltc4162l_info * info,union power_supply_propval * val)206cd900f18SMike Looijmans static int ltc4162l_get_online(struct ltc4162l_info *info,
207cd900f18SMike Looijmans 			       union power_supply_propval *val)
208cd900f18SMike Looijmans {
209cd900f18SMike Looijmans 	unsigned int regval;
210cd900f18SMike Looijmans 	int ret;
211cd900f18SMike Looijmans 
212cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_SYSTEM_STATUS_REG, &regval);
213cd900f18SMike Looijmans 	if (ret)
214cd900f18SMike Looijmans 		return ret;
215cd900f18SMike Looijmans 
216cd900f18SMike Looijmans 	/* BIT(2) indicates if input voltage is sufficient to charge */
217cd900f18SMike Looijmans 	val->intval = !!(regval & BIT(2));
218cd900f18SMike Looijmans 
219cd900f18SMike Looijmans 	return 0;
220cd900f18SMike Looijmans }
221cd900f18SMike Looijmans 
ltc4162l_get_vbat(struct ltc4162l_info * info,unsigned int reg,union power_supply_propval * val)222cd900f18SMike Looijmans static int ltc4162l_get_vbat(struct ltc4162l_info *info,
223cd900f18SMike Looijmans 				  unsigned int reg,
224cd900f18SMike Looijmans 				  union power_supply_propval *val)
225cd900f18SMike Looijmans {
226cd900f18SMike Looijmans 	unsigned int regval;
227cd900f18SMike Looijmans 	int ret;
228cd900f18SMike Looijmans 
229cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, reg, &regval);
230cd900f18SMike Looijmans 	if (ret)
231cd900f18SMike Looijmans 		return ret;
232cd900f18SMike Looijmans 
233cd900f18SMike Looijmans 	/* cell_count × 192.4μV/LSB */
234cd900f18SMike Looijmans 	regval *= 1924;
235cd900f18SMike Looijmans 	regval *= ltc4162l_get_cell_count(info);
236cd900f18SMike Looijmans 	regval /= 10;
237cd900f18SMike Looijmans 	val->intval = regval;
238cd900f18SMike Looijmans 
239cd900f18SMike Looijmans 	return 0;
240cd900f18SMike Looijmans }
241cd900f18SMike Looijmans 
ltc4162l_get_ibat(struct ltc4162l_info * info,union power_supply_propval * val)242cd900f18SMike Looijmans static int ltc4162l_get_ibat(struct ltc4162l_info *info,
243cd900f18SMike Looijmans 			     union power_supply_propval *val)
244cd900f18SMike Looijmans {
245cd900f18SMike Looijmans 	unsigned int regval;
246cd900f18SMike Looijmans 	int ret;
247cd900f18SMike Looijmans 
248cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_IBAT, &regval);
249cd900f18SMike Looijmans 	if (ret)
250cd900f18SMike Looijmans 		return ret;
251cd900f18SMike Looijmans 
252cd900f18SMike Looijmans 	/* Signed 16-bit number, 1.466μV / RSNSB amperes/LSB. */
253cd900f18SMike Looijmans 	ret = (s16)(regval & 0xFFFF);
254cd900f18SMike Looijmans 	val->intval = 100 * mult_frac(ret, 14660, (int)info->rsnsb);
255cd900f18SMike Looijmans 
256cd900f18SMike Looijmans 	return 0;
257cd900f18SMike Looijmans }
258cd900f18SMike Looijmans 
259cd900f18SMike Looijmans 
ltc4162l_get_input_voltage(struct ltc4162l_info * info,union power_supply_propval * val)260cd900f18SMike Looijmans static int ltc4162l_get_input_voltage(struct ltc4162l_info *info,
261cd900f18SMike Looijmans 				      union power_supply_propval *val)
262cd900f18SMike Looijmans {
263cd900f18SMike Looijmans 	unsigned int regval;
264cd900f18SMike Looijmans 	int ret;
265cd900f18SMike Looijmans 
266cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_VIN, &regval);
267cd900f18SMike Looijmans 	if (ret)
268cd900f18SMike Looijmans 		return ret;
269cd900f18SMike Looijmans 
270cd900f18SMike Looijmans 	/* 1.649mV/LSB */
271cd900f18SMike Looijmans 	val->intval =  regval * 1694;
272cd900f18SMike Looijmans 
273cd900f18SMike Looijmans 	return 0;
274cd900f18SMike Looijmans }
275cd900f18SMike Looijmans 
ltc4162l_get_input_current(struct ltc4162l_info * info,union power_supply_propval * val)276cd900f18SMike Looijmans static int ltc4162l_get_input_current(struct ltc4162l_info *info,
277cd900f18SMike Looijmans 				      union power_supply_propval *val)
278cd900f18SMike Looijmans {
279cd900f18SMike Looijmans 	unsigned int regval;
280cd900f18SMike Looijmans 	int ret;
281cd900f18SMike Looijmans 
282cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_IIN, &regval);
283cd900f18SMike Looijmans 	if (ret)
284cd900f18SMike Looijmans 		return ret;
285cd900f18SMike Looijmans 
286cd900f18SMike Looijmans 	/* Signed 16-bit number, 1.466μV / RSNSI amperes/LSB. */
287cd900f18SMike Looijmans 	ret = (s16)(regval & 0xFFFF);
288cd900f18SMike Looijmans 	ret *= 14660;
289cd900f18SMike Looijmans 	ret /= info->rsnsi;
290cd900f18SMike Looijmans 	ret *= 100;
291cd900f18SMike Looijmans 
292cd900f18SMike Looijmans 	val->intval = ret;
293cd900f18SMike Looijmans 
294cd900f18SMike Looijmans 	return 0;
295cd900f18SMike Looijmans }
296cd900f18SMike Looijmans 
ltc4162l_get_icharge(struct ltc4162l_info * info,unsigned int reg,union power_supply_propval * val)297cd900f18SMike Looijmans static int ltc4162l_get_icharge(struct ltc4162l_info *info,
298cd900f18SMike Looijmans 				unsigned int reg,
299cd900f18SMike Looijmans 				union power_supply_propval *val)
300cd900f18SMike Looijmans {
301cd900f18SMike Looijmans 	unsigned int regval;
302cd900f18SMike Looijmans 	int ret;
303cd900f18SMike Looijmans 
304cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, reg, &regval);
305cd900f18SMike Looijmans 	if (ret)
306cd900f18SMike Looijmans 		return ret;
307cd900f18SMike Looijmans 
308cd900f18SMike Looijmans 	regval &= BIT(6) - 1; /* Only the lower 5 bits */
309cd900f18SMike Looijmans 
310cd900f18SMike Looijmans 	/* The charge current servo level: (icharge_dac + 1) × 1mV/RSNSB */
311cd900f18SMike Looijmans 	++regval;
312cd900f18SMike Looijmans 	val->intval = 10000u * mult_frac(regval, 100000u, info->rsnsb);
313cd900f18SMike Looijmans 
314cd900f18SMike Looijmans 	return 0;
315cd900f18SMike Looijmans }
316cd900f18SMike Looijmans 
ltc4162l_set_icharge(struct ltc4162l_info * info,unsigned int reg,unsigned int value)317cd900f18SMike Looijmans static int ltc4162l_set_icharge(struct ltc4162l_info *info,
318cd900f18SMike Looijmans 				unsigned int reg,
319cd900f18SMike Looijmans 				unsigned int value)
320cd900f18SMike Looijmans {
321cd900f18SMike Looijmans 	value = mult_frac(value, info->rsnsb, 100000u);
322cd900f18SMike Looijmans 	value /= 10000u;
323cd900f18SMike Looijmans 
324cd900f18SMike Looijmans 	/* Round to lowest possible */
325cd900f18SMike Looijmans 	if (value)
326cd900f18SMike Looijmans 		--value;
327cd900f18SMike Looijmans 
328cd900f18SMike Looijmans 	if (value > 31)
329cd900f18SMike Looijmans 		return -EINVAL;
330cd900f18SMike Looijmans 
331cd900f18SMike Looijmans 	return regmap_write(info->regmap, reg, value);
332cd900f18SMike Looijmans }
333cd900f18SMike Looijmans 
334cd900f18SMike Looijmans 
ltc4162l_get_vcharge(struct ltc4162l_info * info,unsigned int reg,union power_supply_propval * val)335cd900f18SMike Looijmans static int ltc4162l_get_vcharge(struct ltc4162l_info *info,
336cd900f18SMike Looijmans 				unsigned int reg,
337cd900f18SMike Looijmans 				union power_supply_propval *val)
338cd900f18SMike Looijmans {
339cd900f18SMike Looijmans 	unsigned int regval;
340cd900f18SMike Looijmans 	int ret;
341cd900f18SMike Looijmans 	u32 voltage;
342cd900f18SMike Looijmans 
343cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, reg, &regval);
344cd900f18SMike Looijmans 	if (ret)
345cd900f18SMike Looijmans 		return ret;
346cd900f18SMike Looijmans 
347cd900f18SMike Looijmans 	regval &= BIT(6) - 1; /* Only the lower 5 bits */
348cd900f18SMike Looijmans 
349cd900f18SMike Looijmans 	/*
350cd900f18SMike Looijmans 	 * charge voltage setting can be computed from
351cd900f18SMike Looijmans 	 * cell_count × (vcharge_setting × 12.5mV + 3.8125V)
352cd900f18SMike Looijmans 	 * where vcharge_setting ranges from 0 to 31 (4.2V max).
353cd900f18SMike Looijmans 	 */
354cd900f18SMike Looijmans 	voltage = 3812500 + (regval * 12500);
355cd900f18SMike Looijmans 	voltage *= ltc4162l_get_cell_count(info);
356cd900f18SMike Looijmans 	val->intval = voltage;
357cd900f18SMike Looijmans 
358cd900f18SMike Looijmans 	return 0;
359cd900f18SMike Looijmans }
360cd900f18SMike Looijmans 
ltc4162l_set_vcharge(struct ltc4162l_info * info,unsigned int reg,unsigned int value)361cd900f18SMike Looijmans static int ltc4162l_set_vcharge(struct ltc4162l_info *info,
362cd900f18SMike Looijmans 				unsigned int reg,
363cd900f18SMike Looijmans 				unsigned int value)
364cd900f18SMike Looijmans {
365cd900f18SMike Looijmans 	u8 cell_count = ltc4162l_get_cell_count(info);
366cd900f18SMike Looijmans 
367cd900f18SMike Looijmans 	if (!cell_count)
368cd900f18SMike Looijmans 		return -EBUSY; /* Not available yet, try again later */
369cd900f18SMike Looijmans 
370cd900f18SMike Looijmans 	value /= cell_count;
371cd900f18SMike Looijmans 
372cd900f18SMike Looijmans 	if (value < 3812500)
373cd900f18SMike Looijmans 		return -EINVAL;
374cd900f18SMike Looijmans 
375cd900f18SMike Looijmans 	value -= 3812500;
376cd900f18SMike Looijmans 	value /= 12500;
377cd900f18SMike Looijmans 
378cd900f18SMike Looijmans 	if (value > 31)
379cd900f18SMike Looijmans 		return -EINVAL;
380cd900f18SMike Looijmans 
381cd900f18SMike Looijmans 	return regmap_write(info->regmap, reg, value);
382cd900f18SMike Looijmans }
383cd900f18SMike Looijmans 
ltc4162l_get_iin_limit_dac(struct ltc4162l_info * info,union power_supply_propval * val)384cd900f18SMike Looijmans static int ltc4162l_get_iin_limit_dac(struct ltc4162l_info *info,
385cd900f18SMike Looijmans 				     union power_supply_propval *val)
386cd900f18SMike Looijmans {
387cd900f18SMike Looijmans 	unsigned int regval;
388cd900f18SMike Looijmans 	int ret;
389cd900f18SMike Looijmans 
390cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_IIN_LIMIT_DAC, &regval);
391cd900f18SMike Looijmans 	if (ret)
392cd900f18SMike Looijmans 		return ret;
393cd900f18SMike Looijmans 
394cd900f18SMike Looijmans 	regval &= BIT(6) - 1; /* Only 6 bits */
395cd900f18SMike Looijmans 
396cd900f18SMike Looijmans 	/* (iin_limit_dac + 1) × 500μV / RSNSI */
397cd900f18SMike Looijmans 	++regval;
398cd900f18SMike Looijmans 	regval *= 5000000u;
399cd900f18SMike Looijmans 	regval /= info->rsnsi;
400cd900f18SMike Looijmans 	val->intval = 100u * regval;
401cd900f18SMike Looijmans 
402cd900f18SMike Looijmans 	return 0;
403cd900f18SMike Looijmans }
404cd900f18SMike Looijmans 
ltc4162l_set_iin_limit(struct ltc4162l_info * info,unsigned int value)405cd900f18SMike Looijmans static int ltc4162l_set_iin_limit(struct ltc4162l_info *info,
406cd900f18SMike Looijmans 				  unsigned int value)
407cd900f18SMike Looijmans {
408cd900f18SMike Looijmans 	unsigned int regval;
409cd900f18SMike Looijmans 
410cd900f18SMike Looijmans 	regval = mult_frac(value, info->rsnsi, 50000u);
411cd900f18SMike Looijmans 	regval /= 10000u;
412cd900f18SMike Looijmans 	if (regval)
413cd900f18SMike Looijmans 		--regval;
414cd900f18SMike Looijmans 	if (regval > 63)
415cd900f18SMike Looijmans 		regval = 63;
416cd900f18SMike Looijmans 
417cd900f18SMike Looijmans 	return regmap_write(info->regmap, LTC4162L_IIN_LIMIT_TARGET, regval);
418cd900f18SMike Looijmans }
419cd900f18SMike Looijmans 
ltc4162l_get_die_temp(struct ltc4162l_info * info,union power_supply_propval * val)420cd900f18SMike Looijmans static int ltc4162l_get_die_temp(struct ltc4162l_info *info,
421cd900f18SMike Looijmans 				 union power_supply_propval *val)
422cd900f18SMike Looijmans {
423cd900f18SMike Looijmans 	unsigned int regval;
424cd900f18SMike Looijmans 	int ret;
425cd900f18SMike Looijmans 
426cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_DIE_TEMPERATURE, &regval);
427cd900f18SMike Looijmans 	if (ret)
428cd900f18SMike Looijmans 		return ret;
429cd900f18SMike Looijmans 
430cd900f18SMike Looijmans 	/* die_temp × 0.0215°C/LSB - 264.4°C */
431cd900f18SMike Looijmans 	ret = (s16)(regval & 0xFFFF);
432cd900f18SMike Looijmans 	ret *= 215;
433cd900f18SMike Looijmans 	ret /= 100; /* Centidegrees scale */
434cd900f18SMike Looijmans 	ret -= 26440;
435cd900f18SMike Looijmans 	val->intval = ret;
436cd900f18SMike Looijmans 
437cd900f18SMike Looijmans 	return 0;
438cd900f18SMike Looijmans }
439cd900f18SMike Looijmans 
ltc4162l_get_term_current(struct ltc4162l_info * info,union power_supply_propval * val)440cd900f18SMike Looijmans static int ltc4162l_get_term_current(struct ltc4162l_info *info,
441cd900f18SMike Looijmans 				     union power_supply_propval *val)
442cd900f18SMike Looijmans {
443cd900f18SMike Looijmans 	unsigned int regval;
444cd900f18SMike Looijmans 	int ret;
445cd900f18SMike Looijmans 
446cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_CHARGER_CONFIG_BITS, &regval);
447cd900f18SMike Looijmans 	if (ret)
448cd900f18SMike Looijmans 		return ret;
449cd900f18SMike Looijmans 
450cd900f18SMike Looijmans 	/* Check if C_OVER_X_THRESHOLD is enabled */
451cd900f18SMike Looijmans 	if (!(regval & BIT(2))) {
452cd900f18SMike Looijmans 		val->intval = 0;
453cd900f18SMike Looijmans 		return 0;
454cd900f18SMike Looijmans 	}
455cd900f18SMike Looijmans 
456cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_C_OVER_X_THRESHOLD, &regval);
457cd900f18SMike Looijmans 	if (ret)
458cd900f18SMike Looijmans 		return ret;
459cd900f18SMike Looijmans 
460cd900f18SMike Looijmans 	/* 1.466μV / RSNSB amperes/LSB */
461cd900f18SMike Looijmans 	regval *= 14660u;
462cd900f18SMike Looijmans 	regval /= info->rsnsb;
463cd900f18SMike Looijmans 	val->intval = 100 * regval;
464cd900f18SMike Looijmans 
465cd900f18SMike Looijmans 	return 0;
466cd900f18SMike Looijmans }
467cd900f18SMike Looijmans 
ltc4162l_set_term_current(struct ltc4162l_info * info,unsigned int value)468cd900f18SMike Looijmans static int ltc4162l_set_term_current(struct ltc4162l_info *info,
469cd900f18SMike Looijmans 				     unsigned int value)
470cd900f18SMike Looijmans {
471cd900f18SMike Looijmans 	int ret;
472cd900f18SMike Looijmans 	unsigned int regval;
473cd900f18SMike Looijmans 
474cd900f18SMike Looijmans 	if (!value) {
475cd900f18SMike Looijmans 		/* Disable en_c_over_x_term when set to zero */
476cd900f18SMike Looijmans 		return regmap_update_bits(info->regmap,
477cd900f18SMike Looijmans 					  LTC4162L_CHARGER_CONFIG_BITS,
478cd900f18SMike Looijmans 					  BIT(2), 0);
479cd900f18SMike Looijmans 	}
480cd900f18SMike Looijmans 
481cd900f18SMike Looijmans 	regval = mult_frac(value, info->rsnsb, 14660u);
482cd900f18SMike Looijmans 	regval /= 100u;
483cd900f18SMike Looijmans 
484cd900f18SMike Looijmans 	ret =  regmap_write(info->regmap, LTC4162L_C_OVER_X_THRESHOLD, regval);
485cd900f18SMike Looijmans 	if (ret)
486cd900f18SMike Looijmans 		return ret;
487cd900f18SMike Looijmans 
488cd900f18SMike Looijmans 	/* Set en_c_over_x_term after changing the threshold value */
489cd900f18SMike Looijmans 	return regmap_update_bits(info->regmap, LTC4162L_CHARGER_CONFIG_BITS,
490cd900f18SMike Looijmans 				  BIT(2), BIT(2));
491cd900f18SMike Looijmans }
492cd900f18SMike Looijmans 
493cd900f18SMike Looijmans /* Custom properties */
494cd900f18SMike Looijmans static const char * const ltc4162l_charge_status_name[] = {
495cd900f18SMike Looijmans 	"ilim_reg_active", /* 32 */
496cd900f18SMike Looijmans 	"thermal_reg_active",
497cd900f18SMike Looijmans 	"vin_uvcl_active",
498cd900f18SMike Looijmans 	"iin_limit_active",
499cd900f18SMike Looijmans 	"constant_current",
500cd900f18SMike Looijmans 	"constant_voltage",
501cd900f18SMike Looijmans 	"charger_off" /* 0 */
502cd900f18SMike Looijmans };
503cd900f18SMike Looijmans 
charge_status_show(struct device * dev,struct device_attribute * attr,char * buf)504cd900f18SMike Looijmans static ssize_t charge_status_show(struct device *dev,
505cd900f18SMike Looijmans 				  struct device_attribute *attr, char *buf)
506cd900f18SMike Looijmans {
507cd900f18SMike Looijmans 	struct power_supply *psy = to_power_supply(dev);
508cd900f18SMike Looijmans 	struct ltc4162l_info *info = power_supply_get_drvdata(psy);
509cd900f18SMike Looijmans 	const char *result = ltc4162l_charge_status_name[
510cd900f18SMike Looijmans 				ARRAY_SIZE(ltc4162l_charge_status_name) - 1];
511cd900f18SMike Looijmans 	unsigned int regval;
512cd900f18SMike Looijmans 	unsigned int mask;
513cd900f18SMike Looijmans 	unsigned int index;
514cd900f18SMike Looijmans 	int ret;
515cd900f18SMike Looijmans 
516cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_CHARGE_STATUS, &regval);
517cd900f18SMike Looijmans 	if (ret)
518cd900f18SMike Looijmans 		return ret;
519cd900f18SMike Looijmans 
520cd900f18SMike Looijmans 	/* Only one bit is set according to datasheet, let's be safe here */
521cd900f18SMike Looijmans 	for (mask = 32, index = 0; mask != 0; mask >>= 1, ++index) {
522cd900f18SMike Looijmans 		if (regval & mask) {
523cd900f18SMike Looijmans 			result = ltc4162l_charge_status_name[index];
524cd900f18SMike Looijmans 			break;
525cd900f18SMike Looijmans 		}
526cd900f18SMike Looijmans 	}
527cd900f18SMike Looijmans 
528a441f3b9Sye xingchen 	return sysfs_emit(buf, "%s\n", result);
529cd900f18SMike Looijmans }
530cd900f18SMike Looijmans static DEVICE_ATTR_RO(charge_status);
531cd900f18SMike Looijmans 
vbat_show(struct device * dev,struct device_attribute * attr,char * buf)532cd900f18SMike Looijmans static ssize_t vbat_show(struct device *dev,
533cd900f18SMike Looijmans 				  struct device_attribute *attr, char *buf)
534cd900f18SMike Looijmans {
535cd900f18SMike Looijmans 	struct power_supply *psy = to_power_supply(dev);
536cd900f18SMike Looijmans 	struct ltc4162l_info *info = power_supply_get_drvdata(psy);
537cd900f18SMike Looijmans 	union power_supply_propval val;
538cd900f18SMike Looijmans 	int ret;
539cd900f18SMike Looijmans 
540cd900f18SMike Looijmans 	ret = ltc4162l_get_vbat(info, LTC4162L_VBAT, &val);
541cd900f18SMike Looijmans 	if (ret)
542cd900f18SMike Looijmans 		return ret;
543cd900f18SMike Looijmans 
544a441f3b9Sye xingchen 	return sysfs_emit(buf, "%d\n", val.intval);
545cd900f18SMike Looijmans }
546cd900f18SMike Looijmans static DEVICE_ATTR_RO(vbat);
547cd900f18SMike Looijmans 
vbat_avg_show(struct device * dev,struct device_attribute * attr,char * buf)548cd900f18SMike Looijmans static ssize_t vbat_avg_show(struct device *dev,
549cd900f18SMike Looijmans 				  struct device_attribute *attr, char *buf)
550cd900f18SMike Looijmans {
551cd900f18SMike Looijmans 	struct power_supply *psy = to_power_supply(dev);
552cd900f18SMike Looijmans 	struct ltc4162l_info *info = power_supply_get_drvdata(psy);
553cd900f18SMike Looijmans 	union power_supply_propval val;
554cd900f18SMike Looijmans 	int ret;
555cd900f18SMike Looijmans 
556cd900f18SMike Looijmans 	ret = ltc4162l_get_vbat(info, LTC4162L_VBAT_FILT, &val);
557cd900f18SMike Looijmans 	if (ret)
558cd900f18SMike Looijmans 		return ret;
559cd900f18SMike Looijmans 
560a441f3b9Sye xingchen 	return sysfs_emit(buf, "%d\n", val.intval);
561cd900f18SMike Looijmans }
562cd900f18SMike Looijmans static DEVICE_ATTR_RO(vbat_avg);
563cd900f18SMike Looijmans 
ibat_show(struct device * dev,struct device_attribute * attr,char * buf)564cd900f18SMike Looijmans static ssize_t ibat_show(struct device *dev,
565cd900f18SMike Looijmans 				  struct device_attribute *attr, char *buf)
566cd900f18SMike Looijmans {
567cd900f18SMike Looijmans 	struct power_supply *psy = to_power_supply(dev);
568cd900f18SMike Looijmans 	struct ltc4162l_info *info = power_supply_get_drvdata(psy);
569cd900f18SMike Looijmans 	union power_supply_propval val;
570cd900f18SMike Looijmans 	int ret;
571cd900f18SMike Looijmans 
572cd900f18SMike Looijmans 	ret = ltc4162l_get_ibat(info, &val);
573cd900f18SMike Looijmans 	if (ret)
574cd900f18SMike Looijmans 		return ret;
575cd900f18SMike Looijmans 
576a441f3b9Sye xingchen 	return sysfs_emit(buf, "%d\n", val.intval);
577cd900f18SMike Looijmans }
578cd900f18SMike Looijmans static DEVICE_ATTR_RO(ibat);
579cd900f18SMike Looijmans 
force_telemetry_show(struct device * dev,struct device_attribute * attr,char * buf)580cd900f18SMike Looijmans static ssize_t force_telemetry_show(struct device *dev,
581cd900f18SMike Looijmans 				    struct device_attribute *attr, char *buf)
582cd900f18SMike Looijmans {
583cd900f18SMike Looijmans 	struct power_supply *psy = to_power_supply(dev);
584cd900f18SMike Looijmans 	struct ltc4162l_info *info = power_supply_get_drvdata(psy);
585cd900f18SMike Looijmans 	unsigned int regval;
586cd900f18SMike Looijmans 	int ret;
587cd900f18SMike Looijmans 
588cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_CONFIG_BITS_REG, &regval);
589cd900f18SMike Looijmans 	if (ret)
590cd900f18SMike Looijmans 		return ret;
591cd900f18SMike Looijmans 
592a441f3b9Sye xingchen 	return sysfs_emit(buf, "%u\n", regval & BIT(2) ? 1 : 0);
593cd900f18SMike Looijmans }
594cd900f18SMike Looijmans 
force_telemetry_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)595cd900f18SMike Looijmans static ssize_t force_telemetry_store(struct device *dev,
596cd900f18SMike Looijmans 	struct device_attribute *attr,
597cd900f18SMike Looijmans 	const char *buf,
598cd900f18SMike Looijmans 	size_t count)
599cd900f18SMike Looijmans {
600cd900f18SMike Looijmans 	struct power_supply *psy = to_power_supply(dev);
601cd900f18SMike Looijmans 	struct ltc4162l_info *info = power_supply_get_drvdata(psy);
602cd900f18SMike Looijmans 	int ret;
603cd900f18SMike Looijmans 	unsigned int value;
604cd900f18SMike Looijmans 
605cd900f18SMike Looijmans 	ret = kstrtouint(buf, 0, &value);
606cd900f18SMike Looijmans 	if (ret < 0)
607cd900f18SMike Looijmans 		return ret;
608cd900f18SMike Looijmans 
609cd900f18SMike Looijmans 	ret = regmap_update_bits(info->regmap, LTC4162L_CONFIG_BITS_REG,
610cd900f18SMike Looijmans 				 BIT(2), value ? BIT(2) : 0);
611cd900f18SMike Looijmans 	if (ret < 0)
612cd900f18SMike Looijmans 		return ret;
613cd900f18SMike Looijmans 
614cd900f18SMike Looijmans 	return count;
615cd900f18SMike Looijmans }
616cd900f18SMike Looijmans 
617cd900f18SMike Looijmans static DEVICE_ATTR_RW(force_telemetry);
618cd900f18SMike Looijmans 
arm_ship_mode_show(struct device * dev,struct device_attribute * attr,char * buf)619cd900f18SMike Looijmans static ssize_t arm_ship_mode_show(struct device *dev,
620cd900f18SMike Looijmans 				    struct device_attribute *attr, char *buf)
621cd900f18SMike Looijmans {
622cd900f18SMike Looijmans 	struct power_supply *psy = to_power_supply(dev);
623cd900f18SMike Looijmans 	struct ltc4162l_info *info = power_supply_get_drvdata(psy);
624cd900f18SMike Looijmans 	unsigned int regval;
625cd900f18SMike Looijmans 	int ret;
626cd900f18SMike Looijmans 
627cd900f18SMike Looijmans 	ret = regmap_read(info->regmap, LTC4162L_ARM_SHIP_MODE, &regval);
628cd900f18SMike Looijmans 	if (ret)
629cd900f18SMike Looijmans 		return ret;
630cd900f18SMike Looijmans 
631a441f3b9Sye xingchen 	return sysfs_emit(buf, "%u\n",
632cd900f18SMike Looijmans 		regval == LTC4162L_ARM_SHIP_MODE_MAGIC ? 1 : 0);
633cd900f18SMike Looijmans }
634cd900f18SMike Looijmans 
arm_ship_mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)635cd900f18SMike Looijmans static ssize_t arm_ship_mode_store(struct device *dev,
636cd900f18SMike Looijmans 	struct device_attribute *attr,
637cd900f18SMike Looijmans 	const char *buf,
638cd900f18SMike Looijmans 	size_t count)
639cd900f18SMike Looijmans {
640cd900f18SMike Looijmans 	struct power_supply *psy = to_power_supply(dev);
641cd900f18SMike Looijmans 	struct ltc4162l_info *info = power_supply_get_drvdata(psy);
642cd900f18SMike Looijmans 	int ret;
643cd900f18SMike Looijmans 	unsigned int value;
644cd900f18SMike Looijmans 
645cd900f18SMike Looijmans 	ret = kstrtouint(buf, 0, &value);
646cd900f18SMike Looijmans 	if (ret < 0)
647cd900f18SMike Looijmans 		return ret;
648cd900f18SMike Looijmans 
649cd900f18SMike Looijmans 	ret = regmap_write(info->regmap, LTC4162L_ARM_SHIP_MODE,
650cd900f18SMike Looijmans 				value ? LTC4162L_ARM_SHIP_MODE_MAGIC : 0);
651cd900f18SMike Looijmans 	if (ret < 0)
652cd900f18SMike Looijmans 		return ret;
653cd900f18SMike Looijmans 
654cd900f18SMike Looijmans 	return count;
655cd900f18SMike Looijmans }
656cd900f18SMike Looijmans 
657cd900f18SMike Looijmans static DEVICE_ATTR_RW(arm_ship_mode);
658cd900f18SMike Looijmans 
659cd900f18SMike Looijmans static struct attribute *ltc4162l_sysfs_entries[] = {
660cd900f18SMike Looijmans 	&dev_attr_charge_status.attr,
661cd900f18SMike Looijmans 	&dev_attr_ibat.attr,
662cd900f18SMike Looijmans 	&dev_attr_vbat.attr,
663cd900f18SMike Looijmans 	&dev_attr_vbat_avg.attr,
664cd900f18SMike Looijmans 	&dev_attr_force_telemetry.attr,
665cd900f18SMike Looijmans 	&dev_attr_arm_ship_mode.attr,
666cd900f18SMike Looijmans 	NULL,
667cd900f18SMike Looijmans };
668cd900f18SMike Looijmans 
669249aacc6SRikard Falkeborn static const struct attribute_group ltc4162l_attr_group = {
670cd900f18SMike Looijmans 	.name	= NULL,	/* put in device directory */
671cd900f18SMike Looijmans 	.attrs	= ltc4162l_sysfs_entries,
672cd900f18SMike Looijmans };
673cd900f18SMike Looijmans 
674cd900f18SMike Looijmans static const struct attribute_group *ltc4162l_attr_groups[] = {
675cd900f18SMike Looijmans 	&ltc4162l_attr_group,
676cd900f18SMike Looijmans 	NULL,
677cd900f18SMike Looijmans };
678cd900f18SMike Looijmans 
ltc4162l_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)679cd900f18SMike Looijmans static int ltc4162l_get_property(struct power_supply *psy,
680cd900f18SMike Looijmans 				 enum power_supply_property psp,
681cd900f18SMike Looijmans 				 union power_supply_propval *val)
682cd900f18SMike Looijmans {
683cd900f18SMike Looijmans 	struct ltc4162l_info *info = power_supply_get_drvdata(psy);
684cd900f18SMike Looijmans 
685cd900f18SMike Looijmans 	switch (psp) {
686cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_STATUS:
687cd900f18SMike Looijmans 		return ltc4162l_get_status(info, val);
688cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
689cd900f18SMike Looijmans 		return ltc4162l_get_charge_type(info, val);
690cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_HEALTH:
691cd900f18SMike Looijmans 		return ltc4162l_get_health(info, val);
692cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_ONLINE:
693cd900f18SMike Looijmans 		return ltc4162l_get_online(info, val);
694cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
695cd900f18SMike Looijmans 		return ltc4162l_get_input_voltage(info, val);
696cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_CURRENT_NOW:
697cd900f18SMike Looijmans 		return ltc4162l_get_input_current(info, val);
698cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
699cd900f18SMike Looijmans 		return ltc4162l_get_icharge(info,
700cd900f18SMike Looijmans 				LTC4162L_ICHARGE_DAC, val);
701cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
702cd900f18SMike Looijmans 		return ltc4162l_get_icharge(info,
703cd900f18SMike Looijmans 				LTC4162L_CHARGE_CURRENT_SETTING, val);
704cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
705cd900f18SMike Looijmans 		return ltc4162l_get_vcharge(info,
706cd900f18SMike Looijmans 				LTC4162L_VCHARGE_DAC, val);
707cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
708cd900f18SMike Looijmans 		return ltc4162l_get_vcharge(info,
709cd900f18SMike Looijmans 				LTC4162L_VCHARGE_SETTING, val);
710cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
711cd900f18SMike Looijmans 		return ltc4162l_get_iin_limit_dac(info, val);
712cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_TEMP:
713cd900f18SMike Looijmans 		return ltc4162l_get_die_temp(info, val);
714cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
715cd900f18SMike Looijmans 		return ltc4162l_get_term_current(info, val);
716cd900f18SMike Looijmans 	default:
717cd900f18SMike Looijmans 		return -EINVAL;
718cd900f18SMike Looijmans 	}
719cd900f18SMike Looijmans }
720cd900f18SMike Looijmans 
ltc4162l_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)721cd900f18SMike Looijmans static int ltc4162l_set_property(struct power_supply *psy,
722cd900f18SMike Looijmans 					 enum power_supply_property psp,
723cd900f18SMike Looijmans 					 const union power_supply_propval *val)
724cd900f18SMike Looijmans {
725cd900f18SMike Looijmans 	struct ltc4162l_info *info = power_supply_get_drvdata(psy);
726cd900f18SMike Looijmans 
727cd900f18SMike Looijmans 	switch (psp) {
728cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
729cd900f18SMike Looijmans 		return ltc4162l_set_icharge(info,
730cd900f18SMike Looijmans 				LTC4162L_CHARGE_CURRENT_SETTING, val->intval);
731cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
732cd900f18SMike Looijmans 		return ltc4162l_set_vcharge(info,
733cd900f18SMike Looijmans 				LTC4162L_VCHARGE_SETTING, val->intval);
734cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
735cd900f18SMike Looijmans 		return ltc4162l_set_iin_limit(info, val->intval);
736cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
737cd900f18SMike Looijmans 		return ltc4162l_set_term_current(info, val->intval);
738cd900f18SMike Looijmans 	default:
739cd900f18SMike Looijmans 		return -EINVAL;
740cd900f18SMike Looijmans 	}
741cd900f18SMike Looijmans }
742cd900f18SMike Looijmans 
ltc4162l_property_is_writeable(struct power_supply * psy,enum power_supply_property psp)743cd900f18SMike Looijmans static int ltc4162l_property_is_writeable(struct power_supply *psy,
744cd900f18SMike Looijmans 						enum power_supply_property psp)
745cd900f18SMike Looijmans {
746cd900f18SMike Looijmans 	switch (psp) {
747cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
748cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
749cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
750cd900f18SMike Looijmans 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
751cd900f18SMike Looijmans 		return 1;
752cd900f18SMike Looijmans 	default:
753cd900f18SMike Looijmans 		return 0;
754cd900f18SMike Looijmans 	}
755cd900f18SMike Looijmans }
756cd900f18SMike Looijmans 
757cd900f18SMike Looijmans /* Charger power supply property routines */
758cd900f18SMike Looijmans static enum power_supply_property ltc4162l_properties[] = {
759cd900f18SMike Looijmans 	POWER_SUPPLY_PROP_STATUS,
760cd900f18SMike Looijmans 	POWER_SUPPLY_PROP_CHARGE_TYPE,
761cd900f18SMike Looijmans 	POWER_SUPPLY_PROP_HEALTH,
762cd900f18SMike Looijmans 	POWER_SUPPLY_PROP_ONLINE,
763cd900f18SMike Looijmans 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
764cd900f18SMike Looijmans 	POWER_SUPPLY_PROP_CURRENT_NOW,
765cd900f18SMike Looijmans 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
766cd900f18SMike Looijmans 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
767cd900f18SMike Looijmans 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
768cd900f18SMike Looijmans 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
769cd900f18SMike Looijmans 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
770cd900f18SMike Looijmans 	POWER_SUPPLY_PROP_TEMP,
771cd900f18SMike Looijmans 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
772cd900f18SMike Looijmans };
773cd900f18SMike Looijmans 
774cd900f18SMike Looijmans static const struct power_supply_desc ltc4162l_desc = {
775cd900f18SMike Looijmans 	.name		= "ltc4162-l",
776cd900f18SMike Looijmans 	.type		= POWER_SUPPLY_TYPE_MAINS,
777cd900f18SMike Looijmans 	.properties	= ltc4162l_properties,
778cd900f18SMike Looijmans 	.num_properties	= ARRAY_SIZE(ltc4162l_properties),
779cd900f18SMike Looijmans 	.get_property	= ltc4162l_get_property,
780cd900f18SMike Looijmans 	.set_property	= ltc4162l_set_property,
781cd900f18SMike Looijmans 	.property_is_writeable = ltc4162l_property_is_writeable,
782cd900f18SMike Looijmans };
783cd900f18SMike Looijmans 
ltc4162l_is_writeable_reg(struct device * dev,unsigned int reg)784cd900f18SMike Looijmans static bool ltc4162l_is_writeable_reg(struct device *dev, unsigned int reg)
785cd900f18SMike Looijmans {
786cd900f18SMike Looijmans 	/* all registers up to this one are writeable */
787cd900f18SMike Looijmans 	if (reg <= LTC4162L_CHARGER_CONFIG_BITS)
788cd900f18SMike Looijmans 		return true;
789cd900f18SMike Looijmans 
790cd900f18SMike Looijmans 	/* The ALERTS registers can be written to clear alerts */
791cd900f18SMike Looijmans 	if (reg >= LTC4162L_LIMIT_ALERTS_REG &&
792cd900f18SMike Looijmans 	    reg <= LTC4162L_CHARGE_STATUS_ALERTS_REG)
793cd900f18SMike Looijmans 		return true;
794cd900f18SMike Looijmans 
795cd900f18SMike Looijmans 	return false;
796cd900f18SMike Looijmans }
797cd900f18SMike Looijmans 
ltc4162l_is_volatile_reg(struct device * dev,unsigned int reg)798cd900f18SMike Looijmans static bool ltc4162l_is_volatile_reg(struct device *dev, unsigned int reg)
799cd900f18SMike Looijmans {
800cd900f18SMike Looijmans 	/* all registers after this one are read-only status registers */
801cd900f18SMike Looijmans 	return reg > LTC4162L_CHARGER_CONFIG_BITS;
802cd900f18SMike Looijmans }
803cd900f18SMike Looijmans 
804cd900f18SMike Looijmans static const struct regmap_config ltc4162l_regmap_config = {
805cd900f18SMike Looijmans 	.reg_bits	= 8,
806cd900f18SMike Looijmans 	.val_bits	= 16,
807cd900f18SMike Looijmans 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
808cd900f18SMike Looijmans 	.writeable_reg	= ltc4162l_is_writeable_reg,
809cd900f18SMike Looijmans 	.volatile_reg	= ltc4162l_is_volatile_reg,
810cd900f18SMike Looijmans 	.max_register	= LTC4162L_INPUT_UNDERVOLTAGE_DAC,
811cd900f18SMike Looijmans 	.cache_type	= REGCACHE_RBTREE,
812cd900f18SMike Looijmans };
813cd900f18SMike Looijmans 
ltc4162l_clear_interrupts(struct ltc4162l_info * info)814cd900f18SMike Looijmans static void ltc4162l_clear_interrupts(struct ltc4162l_info *info)
815cd900f18SMike Looijmans {
816cd900f18SMike Looijmans 	/* Acknowledge interrupt to chip by clearing all events */
817cd900f18SMike Looijmans 	regmap_write(info->regmap, LTC4162L_LIMIT_ALERTS_REG, 0);
818cd900f18SMike Looijmans 	regmap_write(info->regmap, LTC4162L_CHARGER_STATE_ALERTS_REG, 0);
819cd900f18SMike Looijmans 	regmap_write(info->regmap, LTC4162L_CHARGE_STATUS_ALERTS_REG, 0);
820cd900f18SMike Looijmans }
821cd900f18SMike Looijmans 
ltc4162l_probe(struct i2c_client * client)82297bdbe0dSUwe Kleine-König static int ltc4162l_probe(struct i2c_client *client)
823cd900f18SMike Looijmans {
824cd900f18SMike Looijmans 	struct i2c_adapter *adapter = client->adapter;
825cd900f18SMike Looijmans 	struct device *dev = &client->dev;
826cd900f18SMike Looijmans 	struct ltc4162l_info *info;
827cd900f18SMike Looijmans 	struct power_supply_config ltc4162l_config = {};
828cd900f18SMike Looijmans 	u32 value;
829cd900f18SMike Looijmans 	int ret;
830cd900f18SMike Looijmans 
831cd900f18SMike Looijmans 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
832cd900f18SMike Looijmans 		dev_err(dev, "No support for SMBUS_WORD_DATA\n");
833cd900f18SMike Looijmans 		return -ENODEV;
834cd900f18SMike Looijmans 	}
835cd900f18SMike Looijmans 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
836cd900f18SMike Looijmans 	if (!info)
837cd900f18SMike Looijmans 		return -ENOMEM;
838cd900f18SMike Looijmans 
839cd900f18SMike Looijmans 	info->client = client;
840cd900f18SMike Looijmans 	i2c_set_clientdata(client, info);
841cd900f18SMike Looijmans 
842cd900f18SMike Looijmans 	info->regmap = devm_regmap_init_i2c(client, &ltc4162l_regmap_config);
843cd900f18SMike Looijmans 	if (IS_ERR(info->regmap)) {
844cd900f18SMike Looijmans 		dev_err(dev, "Failed to initialize register map\n");
845cd900f18SMike Looijmans 		return PTR_ERR(info->regmap);
846cd900f18SMike Looijmans 	}
847cd900f18SMike Looijmans 
848cd900f18SMike Looijmans 	ret = device_property_read_u32(dev, "lltc,rsnsb-micro-ohms",
849cd900f18SMike Looijmans 				       &info->rsnsb);
850cd900f18SMike Looijmans 	if (ret) {
851cd900f18SMike Looijmans 		dev_err(dev, "Missing lltc,rsnsb-micro-ohms property\n");
852cd900f18SMike Looijmans 		return ret;
853cd900f18SMike Looijmans 	}
854cd900f18SMike Looijmans 	if (!info->rsnsb)
855cd900f18SMike Looijmans 		return -EINVAL;
856cd900f18SMike Looijmans 
857cd900f18SMike Looijmans 	ret = device_property_read_u32(dev, "lltc,rsnsi-micro-ohms",
858cd900f18SMike Looijmans 				       &info->rsnsi);
859cd900f18SMike Looijmans 	if (ret) {
860cd900f18SMike Looijmans 		dev_err(dev, "Missing lltc,rsnsi-micro-ohms property\n");
861cd900f18SMike Looijmans 		return ret;
862cd900f18SMike Looijmans 	}
863cd900f18SMike Looijmans 	if (!info->rsnsi)
864cd900f18SMike Looijmans 		return -EINVAL;
865cd900f18SMike Looijmans 
866cd900f18SMike Looijmans 	if (!device_property_read_u32(dev, "lltc,cell-count", &value))
867cd900f18SMike Looijmans 		info->cell_count = value;
868cd900f18SMike Looijmans 
869cd900f18SMike Looijmans 	ltc4162l_config.of_node = dev->of_node;
870cd900f18SMike Looijmans 	ltc4162l_config.drv_data = info;
871cd900f18SMike Looijmans 	ltc4162l_config.attr_grp = ltc4162l_attr_groups;
872cd900f18SMike Looijmans 
873cd900f18SMike Looijmans 	info->charger = devm_power_supply_register(dev, &ltc4162l_desc,
874cd900f18SMike Looijmans 						   &ltc4162l_config);
875cd900f18SMike Looijmans 	if (IS_ERR(info->charger)) {
876cd900f18SMike Looijmans 		dev_err(dev, "Failed to register charger\n");
877cd900f18SMike Looijmans 		return PTR_ERR(info->charger);
878cd900f18SMike Looijmans 	}
879cd900f18SMike Looijmans 
880cd900f18SMike Looijmans 	/* Disable the threshold alerts, we're not using them */
881cd900f18SMike Looijmans 	regmap_write(info->regmap, LTC4162L_EN_LIMIT_ALERTS_REG, 0);
882cd900f18SMike Looijmans 
883cd900f18SMike Looijmans 	/* Enable interrupts on all status changes */
884cd900f18SMike Looijmans 	regmap_write(info->regmap, LTC4162L_EN_CHARGER_STATE_ALERTS_REG,
885cd900f18SMike Looijmans 		     0x1fff);
886cd900f18SMike Looijmans 	regmap_write(info->regmap, LTC4162L_EN_CHARGE_STATUS_ALERTS_REG, 0x1f);
887cd900f18SMike Looijmans 
888cd900f18SMike Looijmans 	ltc4162l_clear_interrupts(info);
889cd900f18SMike Looijmans 
890cd900f18SMike Looijmans 	return 0;
891cd900f18SMike Looijmans }
892cd900f18SMike Looijmans 
ltc4162l_alert(struct i2c_client * client,enum i2c_alert_protocol type,unsigned int flag)893cd900f18SMike Looijmans static void ltc4162l_alert(struct i2c_client *client,
894cd900f18SMike Looijmans 			   enum i2c_alert_protocol type, unsigned int flag)
895cd900f18SMike Looijmans {
896cd900f18SMike Looijmans 	struct ltc4162l_info *info = i2c_get_clientdata(client);
897cd900f18SMike Looijmans 
898cd900f18SMike Looijmans 	if (type != I2C_PROTOCOL_SMBUS_ALERT)
899cd900f18SMike Looijmans 		return;
900cd900f18SMike Looijmans 
901cd900f18SMike Looijmans 	ltc4162l_clear_interrupts(info);
902cd900f18SMike Looijmans 	power_supply_changed(info->charger);
903cd900f18SMike Looijmans }
904cd900f18SMike Looijmans 
905cd900f18SMike Looijmans static const struct i2c_device_id ltc4162l_i2c_id_table[] = {
906*ebacfa1fSUwe Kleine-König 	{ "ltc4162-l" },
907*ebacfa1fSUwe Kleine-König 	{ }
908cd900f18SMike Looijmans };
909cd900f18SMike Looijmans MODULE_DEVICE_TABLE(i2c, ltc4162l_i2c_id_table);
910cd900f18SMike Looijmans 
9113c904b05SKrzysztof Kozlowski static const struct of_device_id ltc4162l_of_match[] __maybe_unused = {
912cd900f18SMike Looijmans 	{ .compatible = "lltc,ltc4162-l", },
913cd900f18SMike Looijmans 	{ },
914cd900f18SMike Looijmans };
915cd900f18SMike Looijmans MODULE_DEVICE_TABLE(of, ltc4162l_of_match);
916cd900f18SMike Looijmans 
917cd900f18SMike Looijmans static struct i2c_driver ltc4162l_driver = {
918fe20b1dcSUwe Kleine-König 	.probe		= ltc4162l_probe,
919cd900f18SMike Looijmans 	.alert		= ltc4162l_alert,
920cd900f18SMike Looijmans 	.id_table	= ltc4162l_i2c_id_table,
921cd900f18SMike Looijmans 	.driver = {
922cd900f18SMike Looijmans 		.name		= "ltc4162-l-charger",
923cd900f18SMike Looijmans 		.of_match_table	= of_match_ptr(ltc4162l_of_match),
924cd900f18SMike Looijmans 	},
925cd900f18SMike Looijmans };
926cd900f18SMike Looijmans module_i2c_driver(ltc4162l_driver);
927cd900f18SMike Looijmans 
928cd900f18SMike Looijmans MODULE_LICENSE("GPL");
929cd900f18SMike Looijmans MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
930cd900f18SMike Looijmans MODULE_DESCRIPTION("LTC4162-L charger driver");
931