1 // SPDX-License-Identifier: GPL-2.0
2 // BQ256XX Battery Charger Driver
3 // Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
4 
5 #include <linux/err.h>
6 #include <linux/i2c.h>
7 #include <linux/init.h>
8 #include <linux/interrupt.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/power_supply.h>
13 #include <linux/regmap.h>
14 #include <linux/types.h>
15 #include <linux/usb/phy.h>
16 #include <linux/device.h>
17 #include <linux/moduleparam.h>
18 #include <linux/slab.h>
19 #include <linux/acpi.h>
20 
21 #define BQ256XX_MANUFACTURER "Texas Instruments"
22 
23 #define BQ256XX_INPUT_CURRENT_LIMIT		0x00
24 #define BQ256XX_CHARGER_CONTROL_0		0x01
25 #define BQ256XX_CHARGE_CURRENT_LIMIT		0x02
26 #define BQ256XX_PRECHG_AND_TERM_CURR_LIM	0x03
27 #define BQ256XX_BATTERY_VOLTAGE_LIMIT		0x04
28 #define BQ256XX_CHARGER_CONTROL_1		0x05
29 #define BQ256XX_CHARGER_CONTROL_2		0x06
30 #define BQ256XX_CHARGER_CONTROL_3		0x07
31 #define BQ256XX_CHARGER_STATUS_0		0x08
32 #define BQ256XX_CHARGER_STATUS_1		0x09
33 #define BQ256XX_CHARGER_STATUS_2		0x0a
34 #define BQ256XX_PART_INFORMATION		0x0b
35 #define BQ256XX_CHARGER_CONTROL_4		0x0c
36 
37 #define BQ256XX_IINDPM_MASK		GENMASK(4, 0)
38 #define BQ256XX_IINDPM_STEP_uA		100000
39 #define BQ256XX_IINDPM_OFFSET_uA	100000
40 #define BQ256XX_IINDPM_MIN_uA		100000
41 #define BQ256XX_IINDPM_MAX_uA		3200000
42 #define BQ256XX_IINDPM_DEF_uA		2400000
43 
44 #define BQ256XX_TS_IGNORE		BIT(6)
45 #define BQ256XX_TS_IGNORE_SHIFT		6
46 
47 #define BQ256XX_VINDPM_MASK		GENMASK(3, 0)
48 #define BQ256XX_VINDPM_STEP_uV		100000
49 #define BQ256XX_VINDPM_OFFSET_uV	3900000
50 #define BQ256XX_VINDPM_MIN_uV		3900000
51 #define BQ256XX_VINDPM_MAX_uV		5400000
52 #define BQ256XX_VINDPM_DEF_uV		4500000
53 
54 #define BQ256XX_VBATREG_MASK		GENMASK(7, 3)
55 #define BQ2560X_VBATREG_STEP_uV		32000
56 #define BQ2560X_VBATREG_OFFSET_uV	3856000
57 #define BQ2560X_VBATREG_MIN_uV		3856000
58 #define BQ2560X_VBATREG_MAX_uV		4624000
59 #define BQ2560X_VBATREG_DEF_uV		4208000
60 #define BQ25601D_VBATREG_OFFSET_uV	3847000
61 #define BQ25601D_VBATREG_MIN_uV		3847000
62 #define BQ25601D_VBATREG_MAX_uV		4615000
63 #define BQ25601D_VBATREG_DEF_uV		4199000
64 #define BQ2561X_VBATREG_STEP_uV		10000
65 #define BQ25611D_VBATREG_MIN_uV		3494000
66 #define BQ25611D_VBATREG_MAX_uV		4510000
67 #define BQ25611D_VBATREG_DEF_uV		4190000
68 #define BQ25618_VBATREG_MIN_uV		3504000
69 #define BQ25618_VBATREG_MAX_uV		4500000
70 #define BQ25618_VBATREG_DEF_uV		4200000
71 #define BQ256XX_VBATREG_BIT_SHIFT	3
72 #define BQ2561X_VBATREG_THRESH		0x8
73 #define BQ25611D_VBATREG_THRESH_uV	4290000
74 #define BQ25618_VBATREG_THRESH_uV	4300000
75 
76 #define BQ256XX_CHG_CONFIG_MASK		BIT(4)
77 #define BQ256XX_CHG_CONFIG_BIT_SHIFT	4
78 
79 #define BQ256XX_ITERM_MASK		GENMASK(3, 0)
80 #define BQ256XX_ITERM_STEP_uA		60000
81 #define BQ256XX_ITERM_OFFSET_uA		60000
82 #define BQ256XX_ITERM_MIN_uA		60000
83 #define BQ256XX_ITERM_MAX_uA		780000
84 #define BQ256XX_ITERM_DEF_uA		180000
85 #define BQ25618_ITERM_STEP_uA		20000
86 #define BQ25618_ITERM_OFFSET_uA		20000
87 #define BQ25618_ITERM_MIN_uA		20000
88 #define BQ25618_ITERM_MAX_uA		260000
89 #define BQ25618_ITERM_DEF_uA		60000
90 
91 #define BQ256XX_IPRECHG_MASK		GENMASK(7, 4)
92 #define BQ256XX_IPRECHG_STEP_uA		60000
93 #define BQ256XX_IPRECHG_OFFSET_uA	60000
94 #define BQ256XX_IPRECHG_MIN_uA		60000
95 #define BQ256XX_IPRECHG_MAX_uA		780000
96 #define BQ256XX_IPRECHG_DEF_uA		180000
97 #define BQ25618_IPRECHG_STEP_uA		20000
98 #define BQ25618_IPRECHG_OFFSET_uA	20000
99 #define BQ25618_IPRECHG_MIN_uA		20000
100 #define BQ25618_IPRECHG_MAX_uA		260000
101 #define BQ25618_IPRECHG_DEF_uA		40000
102 #define BQ256XX_IPRECHG_BIT_SHIFT	4
103 
104 #define BQ256XX_ICHG_MASK		GENMASK(5, 0)
105 #define BQ256XX_ICHG_STEP_uA		60000
106 #define BQ256XX_ICHG_MIN_uA		0
107 #define BQ256XX_ICHG_MAX_uA		3000000
108 #define BQ2560X_ICHG_DEF_uA		2040000
109 #define BQ25611D_ICHG_DEF_uA		1020000
110 #define BQ25618_ICHG_STEP_uA		20000
111 #define BQ25618_ICHG_MIN_uA		0
112 #define BQ25618_ICHG_MAX_uA		1500000
113 #define BQ25618_ICHG_DEF_uA		340000
114 #define BQ25618_ICHG_THRESH		0x3c
115 #define BQ25618_ICHG_THRESH_uA		1180000
116 
117 #define BQ256XX_VBUS_STAT_MASK		GENMASK(7, 5)
118 #define BQ256XX_VBUS_STAT_NO_INPUT	0
119 #define BQ256XX_VBUS_STAT_USB_SDP	BIT(5)
120 #define BQ256XX_VBUS_STAT_USB_CDP	BIT(6)
121 #define BQ256XX_VBUS_STAT_USB_DCP	(BIT(6) | BIT(5))
122 #define BQ256XX_VBUS_STAT_USB_OTG	(BIT(7) | BIT(6) | BIT(5))
123 
124 #define BQ256XX_CHRG_STAT_MASK		GENMASK(4, 3)
125 #define BQ256XX_CHRG_STAT_NOT_CHRGING	0
126 #define BQ256XX_CHRG_STAT_PRECHRGING	BIT(3)
127 #define BQ256XX_CHRG_STAT_FAST_CHRGING	BIT(4)
128 #define BQ256XX_CHRG_STAT_CHRG_TERM	(BIT(4) | BIT(3))
129 
130 #define BQ256XX_PG_STAT_MASK		BIT(2)
131 #define BQ256XX_WDT_FAULT_MASK		BIT(7)
132 #define BQ256XX_CHRG_FAULT_MASK		GENMASK(5, 4)
133 #define BQ256XX_CHRG_FAULT_NORMAL	0
134 #define BQ256XX_CHRG_FAULT_INPUT	BIT(4)
135 #define BQ256XX_CHRG_FAULT_THERM	BIT(5)
136 #define BQ256XX_CHRG_FAULT_CST_EXPIRE	(BIT(5) | BIT(4))
137 #define BQ256XX_BAT_FAULT_MASK		BIT(3)
138 #define BQ256XX_NTC_FAULT_MASK		GENMASK(2, 0)
139 #define BQ256XX_NTC_FAULT_WARM		BIT(1)
140 #define BQ256XX_NTC_FAULT_COOL		(BIT(1) | BIT(0))
141 #define BQ256XX_NTC_FAULT_COLD		(BIT(2) | BIT(0))
142 #define BQ256XX_NTC_FAULT_HOT		(BIT(2) | BIT(1))
143 
144 #define BQ256XX_NUM_WD_VAL	4
145 #define BQ256XX_WATCHDOG_MASK	GENMASK(5, 4)
146 #define BQ256XX_WATCHDOG_MAX	1600000
147 #define BQ256XX_WATCHDOG_DIS	0
148 #define BQ256XX_WDT_BIT_SHIFT	4
149 
150 #define BQ256XX_REG_RST		BIT(7)
151 
152 /**
153  * struct bq256xx_init_data -
154  * @ichg: fast charge current
155  * @iindpm: input current limit
156  * @vbatreg: charge voltage
157  * @iterm: termination current
158  * @iprechg: precharge current
159  * @vindpm: input voltage limit
160  * @ichg_max: maximum fast charge current
161  * @vbatreg_max: maximum charge voltage
162  * @ts_ignore: TS_IGNORE flag
163  */
164 struct bq256xx_init_data {
165 	u32 ichg;
166 	u32 iindpm;
167 	u32 vbatreg;
168 	u32 iterm;
169 	u32 iprechg;
170 	u32 vindpm;
171 	u32 ichg_max;
172 	u32 vbatreg_max;
173 	bool ts_ignore;
174 };
175 
176 /**
177  * struct bq256xx_state -
178  * @vbus_stat: VBUS status according to BQ256XX_CHARGER_STATUS_0
179  * @chrg_stat: charging status according to BQ256XX_CHARGER_STATUS_0
180  * @online: PG status according to BQ256XX_CHARGER_STATUS_0
181  *
182  * @wdt_fault: watchdog fault according to BQ256XX_CHARGER_STATUS_1
183  * @bat_fault: battery fault according to BQ256XX_CHARGER_STATUS_1
184  * @chrg_fault: charging fault according to BQ256XX_CHARGER_STATUS_1
185  * @ntc_fault: TS fault according to BQ256XX_CHARGER_STATUS_1
186  */
187 struct bq256xx_state {
188 	u8 vbus_stat;
189 	u8 chrg_stat;
190 	bool online;
191 
192 	u8 wdt_fault;
193 	u8 bat_fault;
194 	u8 chrg_fault;
195 	u8 ntc_fault;
196 };
197 
198 enum bq256xx_id {
199 	BQ25600,
200 	BQ25600D,
201 	BQ25601,
202 	BQ25601D,
203 	BQ25618,
204 	BQ25619,
205 	BQ25611D,
206 };
207 
208 /**
209  * struct bq256xx_device -
210  * @client: i2c client structure
211  * @regmap: register map structure
212  * @dev: device structure
213  * @charger: power supply registered for the charger
214  * @battery: power supply registered for the battery
215  * @lock: mutex lock structure
216  *
217  * @usb2_phy: usb_phy identifier
218  * @usb3_phy: usb_phy identifier
219  * @usb_nb: notifier block
220  * @usb_work: usb work queue
221  * @usb_event: usb_event code
222  *
223  * @model_name: i2c name string
224  *
225  * @init_data: initialization data
226  * @chip_info: device variant information
227  * @state: device status and faults
228  * @watchdog_timer: watchdog timer value in milliseconds
229  */
230 struct bq256xx_device {
231 	struct i2c_client *client;
232 	struct device *dev;
233 	struct power_supply *charger;
234 	struct power_supply *battery;
235 	struct mutex lock;
236 	struct regmap *regmap;
237 
238 	struct usb_phy *usb2_phy;
239 	struct usb_phy *usb3_phy;
240 	struct notifier_block usb_nb;
241 	struct work_struct usb_work;
242 	unsigned long usb_event;
243 
244 	char model_name[I2C_NAME_SIZE];
245 
246 	struct bq256xx_init_data init_data;
247 	const struct bq256xx_chip_info *chip_info;
248 	struct bq256xx_state state;
249 	int watchdog_timer;
250 };
251 
252 /**
253  * struct bq256xx_chip_info -
254  * @model_id: device instance
255  *
256  * @bq256xx_regmap_config: regmap configuration struct
257  * @bq256xx_get_ichg: pointer to instance specific get_ichg function
258  * @bq256xx_get_iindpm: pointer to instance specific get_iindpm function
259  * @bq256xx_get_vbatreg: pointer to instance specific get_vbatreg function
260  * @bq256xx_get_iterm: pointer to instance specific get_iterm function
261  * @bq256xx_get_iprechg: pointer to instance specific get_iprechg function
262  * @bq256xx_get_vindpm: pointer to instance specific get_vindpm function
263  *
264  * @bq256xx_set_ichg: pointer to instance specific set_ichg function
265  * @bq256xx_set_iindpm: pointer to instance specific set_iindpm function
266  * @bq256xx_set_vbatreg: pointer to instance specific set_vbatreg function
267  * @bq256xx_set_iterm: pointer to instance specific set_iterm function
268  * @bq256xx_set_iprechg: pointer to instance specific set_iprechg function
269  * @bq256xx_set_vindpm: pointer to instance specific set_vindpm function
270  * @bq256xx_set_charge_type: pointer to instance specific set_charge_type function
271  * @bq256xx_set_ts_ignore: pointer to instance specific set_ts_ignore function
272  *
273  * @bq256xx_def_ichg: default ichg value in microamps
274  * @bq256xx_def_iindpm: default iindpm value in microamps
275  * @bq256xx_def_vbatreg: default vbatreg value in microvolts
276  * @bq256xx_def_iterm: default iterm value in microamps
277  * @bq256xx_def_iprechg: default iprechg value in microamps
278  * @bq256xx_def_vindpm: default vindpm value in microvolts
279  *
280  * @bq256xx_max_ichg: maximum charge current in microamps
281  * @bq256xx_max_vbatreg: maximum battery regulation voltage in microvolts
282  *
283  * @has_usb_detect: indicates whether device has BC1.2 detection
284  */
285 struct bq256xx_chip_info {
286 	int model_id;
287 
288 	const struct regmap_config *bq256xx_regmap_config;
289 
290 	int (*bq256xx_get_ichg)(struct bq256xx_device *bq);
291 	int (*bq256xx_get_iindpm)(struct bq256xx_device *bq);
292 	int (*bq256xx_get_vbatreg)(struct bq256xx_device *bq);
293 	int (*bq256xx_get_iterm)(struct bq256xx_device *bq);
294 	int (*bq256xx_get_iprechg)(struct bq256xx_device *bq);
295 	int (*bq256xx_get_vindpm)(struct bq256xx_device *bq);
296 
297 	int (*bq256xx_set_ichg)(struct bq256xx_device *bq, int ichg);
298 	int (*bq256xx_set_iindpm)(struct bq256xx_device *bq, int iindpm);
299 	int (*bq256xx_set_vbatreg)(struct bq256xx_device *bq, int vbatreg);
300 	int (*bq256xx_set_iterm)(struct bq256xx_device *bq, int iterm);
301 	int (*bq256xx_set_iprechg)(struct bq256xx_device *bq, int iprechg);
302 	int (*bq256xx_set_vindpm)(struct bq256xx_device *bq, int vindpm);
303 	int (*bq256xx_set_charge_type)(struct bq256xx_device *bq, int type);
304 	int (*bq256xx_set_ts_ignore)(struct bq256xx_device *bq, bool ts_ignore);
305 
306 	int bq256xx_def_ichg;
307 	int bq256xx_def_iindpm;
308 	int bq256xx_def_vbatreg;
309 	int bq256xx_def_iterm;
310 	int bq256xx_def_iprechg;
311 	int bq256xx_def_vindpm;
312 
313 	int bq256xx_max_ichg;
314 	int bq256xx_max_vbatreg;
315 
316 	bool has_usb_detect;
317 };
318 
319 static int bq256xx_watchdog_time[BQ256XX_NUM_WD_VAL] = {
320 	0, 40000, 80000, 1600000
321 };
322 
323 static const int bq25611d_vbatreg_values[] = {
324 	3494000, 3590000, 3686000, 3790000, 3894000, 3990000, 4090000, 4140000,
325 	4190000
326 };
327 
328 static const int bq25618_619_vbatreg_values[] = {
329 	3504000, 3600000, 3696000, 3800000, 3904000, 4000000, 4100000, 4150000,
330 	4200000
331 };
332 
333 static const int bq25618_619_ichg_values[] = {
334 	1290000, 1360000, 1430000, 1500000
335 };
336 
337 static enum power_supply_usb_type bq256xx_usb_type[] = {
338 	POWER_SUPPLY_USB_TYPE_SDP,
339 	POWER_SUPPLY_USB_TYPE_CDP,
340 	POWER_SUPPLY_USB_TYPE_DCP,
341 	POWER_SUPPLY_USB_TYPE_UNKNOWN,
342 	POWER_SUPPLY_USB_TYPE_ACA,
343 };
344 
bq256xx_array_parse(int array_size,int val,const int array[])345 static int bq256xx_array_parse(int array_size, int val, const int array[])
346 {
347 	int i = 0;
348 
349 	if (val < array[i])
350 		return i - 1;
351 
352 	if (val >= array[array_size - 1])
353 		return array_size - 1;
354 
355 	for (i = 1; i < array_size; i++) {
356 		if (val == array[i])
357 			return i;
358 
359 		if (val > array[i - 1] && val < array[i]) {
360 			if (val < array[i])
361 				return i - 1;
362 			else
363 				return i;
364 		}
365 	}
366 	return -EINVAL;
367 }
368 
bq256xx_usb_notifier(struct notifier_block * nb,unsigned long val,void * priv)369 static int bq256xx_usb_notifier(struct notifier_block *nb, unsigned long val,
370 				void *priv)
371 {
372 	struct bq256xx_device *bq =
373 			container_of(nb, struct bq256xx_device, usb_nb);
374 
375 	bq->usb_event = val;
376 	queue_work(system_power_efficient_wq, &bq->usb_work);
377 
378 	return NOTIFY_OK;
379 }
380 
bq256xx_usb_work(struct work_struct * data)381 static void bq256xx_usb_work(struct work_struct *data)
382 {
383 	struct bq256xx_device *bq =
384 			container_of(data, struct bq256xx_device, usb_work);
385 
386 	switch (bq->usb_event) {
387 	case USB_EVENT_ID:
388 		break;
389 	case USB_EVENT_NONE:
390 		power_supply_changed(bq->charger);
391 		break;
392 	default:
393 		dev_err(bq->dev, "Error switching to charger mode.\n");
394 		break;
395 	}
396 }
397 
398 static struct reg_default bq2560x_reg_defs[] = {
399 	{BQ256XX_INPUT_CURRENT_LIMIT, 0x17},
400 	{BQ256XX_CHARGER_CONTROL_0, 0x1a},
401 	{BQ256XX_CHARGE_CURRENT_LIMIT, 0xa2},
402 	{BQ256XX_PRECHG_AND_TERM_CURR_LIM, 0x22},
403 	{BQ256XX_BATTERY_VOLTAGE_LIMIT, 0x58},
404 	{BQ256XX_CHARGER_CONTROL_1, 0x9f},
405 	{BQ256XX_CHARGER_CONTROL_2, 0x66},
406 	{BQ256XX_CHARGER_CONTROL_3, 0x4c},
407 };
408 
409 static struct reg_default bq25611d_reg_defs[] = {
410 	{BQ256XX_INPUT_CURRENT_LIMIT, 0x17},
411 	{BQ256XX_CHARGER_CONTROL_0, 0x1a},
412 	{BQ256XX_CHARGE_CURRENT_LIMIT, 0x91},
413 	{BQ256XX_PRECHG_AND_TERM_CURR_LIM, 0x12},
414 	{BQ256XX_BATTERY_VOLTAGE_LIMIT, 0x40},
415 	{BQ256XX_CHARGER_CONTROL_1, 0x9e},
416 	{BQ256XX_CHARGER_CONTROL_2, 0xe6},
417 	{BQ256XX_CHARGER_CONTROL_3, 0x4c},
418 	{BQ256XX_PART_INFORMATION, 0x54},
419 	{BQ256XX_CHARGER_CONTROL_4, 0x75},
420 };
421 
422 static struct reg_default bq25618_619_reg_defs[] = {
423 	{BQ256XX_INPUT_CURRENT_LIMIT, 0x17},
424 	{BQ256XX_CHARGER_CONTROL_0, 0x1a},
425 	{BQ256XX_CHARGE_CURRENT_LIMIT, 0x91},
426 	{BQ256XX_PRECHG_AND_TERM_CURR_LIM, 0x12},
427 	{BQ256XX_BATTERY_VOLTAGE_LIMIT, 0x40},
428 	{BQ256XX_CHARGER_CONTROL_1, 0x9e},
429 	{BQ256XX_CHARGER_CONTROL_2, 0xe6},
430 	{BQ256XX_CHARGER_CONTROL_3, 0x4c},
431 	{BQ256XX_PART_INFORMATION, 0x2c},
432 	{BQ256XX_CHARGER_CONTROL_4, 0x75},
433 };
434 
bq256xx_get_state(struct bq256xx_device * bq,struct bq256xx_state * state)435 static int bq256xx_get_state(struct bq256xx_device *bq,
436 				struct bq256xx_state *state)
437 {
438 	unsigned int charger_status_0;
439 	unsigned int charger_status_1;
440 	int ret;
441 
442 	ret = regmap_read(bq->regmap, BQ256XX_CHARGER_STATUS_0,
443 						&charger_status_0);
444 	if (ret)
445 		return ret;
446 
447 	ret = regmap_read(bq->regmap, BQ256XX_CHARGER_STATUS_1,
448 						&charger_status_1);
449 	if (ret)
450 		return ret;
451 
452 	state->vbus_stat = charger_status_0 & BQ256XX_VBUS_STAT_MASK;
453 	state->chrg_stat = charger_status_0 & BQ256XX_CHRG_STAT_MASK;
454 	state->online = charger_status_0 & BQ256XX_PG_STAT_MASK;
455 
456 	state->wdt_fault = charger_status_1 & BQ256XX_WDT_FAULT_MASK;
457 	state->bat_fault = charger_status_1 & BQ256XX_BAT_FAULT_MASK;
458 	state->chrg_fault = charger_status_1 & BQ256XX_CHRG_FAULT_MASK;
459 	state->ntc_fault = charger_status_1 & BQ256XX_NTC_FAULT_MASK;
460 
461 	return 0;
462 }
463 
bq256xx_set_charge_type(struct bq256xx_device * bq,int type)464 static int bq256xx_set_charge_type(struct bq256xx_device *bq, int type)
465 {
466 	int chg_config = 0;
467 
468 	switch (type) {
469 	case POWER_SUPPLY_CHARGE_TYPE_NONE:
470 		chg_config = 0x0;
471 		break;
472 	case POWER_SUPPLY_CHARGE_TYPE_TRICKLE:
473 	case POWER_SUPPLY_CHARGE_TYPE_FAST:
474 		chg_config = 0x1;
475 		break;
476 	default:
477 		return -EINVAL;
478 	}
479 
480 	return regmap_update_bits(bq->regmap, BQ256XX_CHARGER_CONTROL_0,
481 				BQ256XX_CHG_CONFIG_MASK,
482 				(chg_config ? 1 : 0) << BQ256XX_CHG_CONFIG_BIT_SHIFT);
483 }
484 
bq256xx_get_ichg_curr(struct bq256xx_device * bq)485 static int bq256xx_get_ichg_curr(struct bq256xx_device *bq)
486 {
487 	unsigned int charge_current_limit;
488 	unsigned int ichg_reg_code;
489 	int ret;
490 
491 	ret = regmap_read(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT,
492 						&charge_current_limit);
493 	if (ret)
494 		return ret;
495 
496 	ichg_reg_code = charge_current_limit & BQ256XX_ICHG_MASK;
497 
498 	return ichg_reg_code * BQ256XX_ICHG_STEP_uA;
499 }
500 
bq25618_619_get_ichg_curr(struct bq256xx_device * bq)501 static int bq25618_619_get_ichg_curr(struct bq256xx_device *bq)
502 {
503 	unsigned int charge_current_limit;
504 	unsigned int ichg_reg_code;
505 	int ret;
506 
507 	ret = regmap_read(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT,
508 						&charge_current_limit);
509 	if (ret)
510 		return ret;
511 
512 	ichg_reg_code = charge_current_limit & BQ256XX_ICHG_MASK;
513 
514 	if (ichg_reg_code < BQ25618_ICHG_THRESH)
515 		return ichg_reg_code * BQ25618_ICHG_STEP_uA;
516 
517 	return bq25618_619_ichg_values[ichg_reg_code - BQ25618_ICHG_THRESH];
518 }
519 
bq256xx_set_ichg_curr(struct bq256xx_device * bq,int ichg)520 static int bq256xx_set_ichg_curr(struct bq256xx_device *bq, int ichg)
521 {
522 	unsigned int ichg_reg_code;
523 	int ichg_max = bq->init_data.ichg_max;
524 
525 	ichg = clamp(ichg, BQ256XX_ICHG_MIN_uA, ichg_max);
526 	ichg_reg_code = ichg / BQ256XX_ICHG_STEP_uA;
527 
528 	return regmap_update_bits(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT,
529 					BQ256XX_ICHG_MASK, ichg_reg_code);
530 }
531 
bq25618_619_set_ichg_curr(struct bq256xx_device * bq,int ichg)532 static int bq25618_619_set_ichg_curr(struct bq256xx_device *bq, int ichg)
533 {
534 	int array_size = ARRAY_SIZE(bq25618_619_ichg_values);
535 	unsigned int ichg_reg_code;
536 	int ichg_max = bq->init_data.ichg_max;
537 
538 	ichg = clamp(ichg, BQ25618_ICHG_MIN_uA, ichg_max);
539 
540 	if (ichg <= BQ25618_ICHG_THRESH_uA) {
541 		ichg_reg_code = ichg / BQ25618_ICHG_STEP_uA;
542 	} else {
543 		ichg_reg_code = bq256xx_array_parse(array_size, ichg,
544 			bq25618_619_ichg_values) + BQ25618_ICHG_THRESH;
545 	}
546 
547 	return regmap_update_bits(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT,
548 					BQ256XX_ICHG_MASK, ichg_reg_code);
549 }
550 
bq25618_619_get_chrg_volt(struct bq256xx_device * bq)551 static int bq25618_619_get_chrg_volt(struct bq256xx_device *bq)
552 {
553 	unsigned int battery_volt_lim;
554 	unsigned int vbatreg_reg_code;
555 	int ret;
556 
557 	ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
558 							&battery_volt_lim);
559 
560 	if (ret)
561 		return ret;
562 
563 	vbatreg_reg_code = (battery_volt_lim & BQ256XX_VBATREG_MASK) >>
564 						BQ256XX_VBATREG_BIT_SHIFT;
565 
566 	if (vbatreg_reg_code > BQ2561X_VBATREG_THRESH)
567 		return ((vbatreg_reg_code - BQ2561X_VBATREG_THRESH) *
568 					BQ2561X_VBATREG_STEP_uV) +
569 					BQ25618_VBATREG_THRESH_uV;
570 
571 	return bq25618_619_vbatreg_values[vbatreg_reg_code];
572 }
573 
bq25611d_get_chrg_volt(struct bq256xx_device * bq)574 static int bq25611d_get_chrg_volt(struct bq256xx_device *bq)
575 {
576 	unsigned int battery_volt_lim;
577 	unsigned int vbatreg_reg_code;
578 	int ret;
579 
580 	ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
581 							&battery_volt_lim);
582 	if (ret)
583 		return ret;
584 
585 	vbatreg_reg_code = (battery_volt_lim & BQ256XX_VBATREG_MASK) >>
586 						BQ256XX_VBATREG_BIT_SHIFT;
587 
588 	if (vbatreg_reg_code > BQ2561X_VBATREG_THRESH)
589 		return ((vbatreg_reg_code - BQ2561X_VBATREG_THRESH) *
590 					BQ2561X_VBATREG_STEP_uV) +
591 					BQ25611D_VBATREG_THRESH_uV;
592 
593 	return bq25611d_vbatreg_values[vbatreg_reg_code];
594 }
595 
bq2560x_get_chrg_volt(struct bq256xx_device * bq)596 static int bq2560x_get_chrg_volt(struct bq256xx_device *bq)
597 {
598 	unsigned int battery_volt_lim;
599 	unsigned int vbatreg_reg_code;
600 	int ret;
601 
602 	ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
603 							&battery_volt_lim);
604 	if (ret)
605 		return ret;
606 
607 	vbatreg_reg_code = (battery_volt_lim & BQ256XX_VBATREG_MASK) >>
608 						BQ256XX_VBATREG_BIT_SHIFT;
609 
610 	return (vbatreg_reg_code * BQ2560X_VBATREG_STEP_uV)
611 					+ BQ2560X_VBATREG_OFFSET_uV;
612 }
613 
bq25601d_get_chrg_volt(struct bq256xx_device * bq)614 static int bq25601d_get_chrg_volt(struct bq256xx_device *bq)
615 {
616 	unsigned int battery_volt_lim;
617 	unsigned int vbatreg_reg_code;
618 	int ret;
619 
620 	ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
621 							&battery_volt_lim);
622 	if (ret)
623 		return ret;
624 
625 	vbatreg_reg_code = (battery_volt_lim & BQ256XX_VBATREG_MASK) >>
626 						BQ256XX_VBATREG_BIT_SHIFT;
627 
628 	return (vbatreg_reg_code * BQ2560X_VBATREG_STEP_uV)
629 					+ BQ25601D_VBATREG_OFFSET_uV;
630 }
631 
bq25618_619_set_chrg_volt(struct bq256xx_device * bq,int vbatreg)632 static int bq25618_619_set_chrg_volt(struct bq256xx_device *bq, int vbatreg)
633 {
634 	int array_size = ARRAY_SIZE(bq25618_619_vbatreg_values);
635 	unsigned int vbatreg_reg_code;
636 	int vbatreg_max = bq->init_data.vbatreg_max;
637 
638 	vbatreg = clamp(vbatreg, BQ25618_VBATREG_MIN_uV, vbatreg_max);
639 
640 	if (vbatreg > BQ25618_VBATREG_THRESH_uV)
641 		vbatreg_reg_code = ((vbatreg -
642 		BQ25618_VBATREG_THRESH_uV) /
643 		(BQ2561X_VBATREG_STEP_uV)) + BQ2561X_VBATREG_THRESH;
644 	else {
645 		vbatreg_reg_code = bq256xx_array_parse(array_size, vbatreg,
646 						bq25618_619_vbatreg_values);
647 	}
648 
649 	return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
650 				BQ256XX_VBATREG_MASK, vbatreg_reg_code <<
651 						BQ256XX_VBATREG_BIT_SHIFT);
652 }
653 
bq25611d_set_chrg_volt(struct bq256xx_device * bq,int vbatreg)654 static int bq25611d_set_chrg_volt(struct bq256xx_device *bq, int vbatreg)
655 {
656 	int array_size = ARRAY_SIZE(bq25611d_vbatreg_values);
657 	unsigned int vbatreg_reg_code;
658 	int vbatreg_max = bq->init_data.vbatreg_max;
659 
660 	vbatreg = clamp(vbatreg, BQ25611D_VBATREG_MIN_uV, vbatreg_max);
661 
662 	if (vbatreg > BQ25611D_VBATREG_THRESH_uV)
663 		vbatreg_reg_code = ((vbatreg -
664 		BQ25611D_VBATREG_THRESH_uV) /
665 		(BQ2561X_VBATREG_STEP_uV)) + BQ2561X_VBATREG_THRESH;
666 	else {
667 		vbatreg_reg_code = bq256xx_array_parse(array_size, vbatreg,
668 						bq25611d_vbatreg_values);
669 	}
670 
671 	return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
672 				BQ256XX_VBATREG_MASK, vbatreg_reg_code <<
673 						BQ256XX_VBATREG_BIT_SHIFT);
674 }
675 
bq2560x_set_chrg_volt(struct bq256xx_device * bq,int vbatreg)676 static int bq2560x_set_chrg_volt(struct bq256xx_device *bq, int vbatreg)
677 {
678 	unsigned int vbatreg_reg_code;
679 	int vbatreg_max = bq->init_data.vbatreg_max;
680 
681 	vbatreg = clamp(vbatreg, BQ2560X_VBATREG_MIN_uV, vbatreg_max);
682 
683 	vbatreg_reg_code = (vbatreg - BQ2560X_VBATREG_OFFSET_uV) /
684 						BQ2560X_VBATREG_STEP_uV;
685 
686 	return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
687 				BQ256XX_VBATREG_MASK, vbatreg_reg_code <<
688 						BQ256XX_VBATREG_BIT_SHIFT);
689 }
690 
bq25601d_set_chrg_volt(struct bq256xx_device * bq,int vbatreg)691 static int bq25601d_set_chrg_volt(struct bq256xx_device *bq, int vbatreg)
692 {
693 	unsigned int vbatreg_reg_code;
694 	int vbatreg_max = bq->init_data.vbatreg_max;
695 
696 	vbatreg = clamp(vbatreg, BQ25601D_VBATREG_MIN_uV, vbatreg_max);
697 
698 	vbatreg_reg_code = (vbatreg - BQ25601D_VBATREG_OFFSET_uV) /
699 						BQ2560X_VBATREG_STEP_uV;
700 
701 	return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
702 				BQ256XX_VBATREG_MASK, vbatreg_reg_code <<
703 						BQ256XX_VBATREG_BIT_SHIFT);
704 }
705 
bq256xx_set_ts_ignore(struct bq256xx_device * bq,bool ts_ignore)706 static int bq256xx_set_ts_ignore(struct bq256xx_device *bq, bool ts_ignore)
707 {
708 	return regmap_update_bits(bq->regmap, BQ256XX_INPUT_CURRENT_LIMIT,
709 				BQ256XX_TS_IGNORE, (ts_ignore ? 1 : 0) << BQ256XX_TS_IGNORE_SHIFT);
710 }
711 
bq256xx_get_prechrg_curr(struct bq256xx_device * bq)712 static int bq256xx_get_prechrg_curr(struct bq256xx_device *bq)
713 {
714 	unsigned int prechg_and_term_curr_lim;
715 	unsigned int iprechg_reg_code;
716 	int ret;
717 
718 	ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
719 						&prechg_and_term_curr_lim);
720 	if (ret)
721 		return ret;
722 
723 	iprechg_reg_code = (prechg_and_term_curr_lim & BQ256XX_IPRECHG_MASK)
724 						>> BQ256XX_IPRECHG_BIT_SHIFT;
725 
726 	return (iprechg_reg_code * BQ256XX_IPRECHG_STEP_uA) +
727 						BQ256XX_IPRECHG_OFFSET_uA;
728 }
729 
bq256xx_set_prechrg_curr(struct bq256xx_device * bq,int iprechg)730 static int bq256xx_set_prechrg_curr(struct bq256xx_device *bq, int iprechg)
731 {
732 	unsigned int iprechg_reg_code;
733 
734 	iprechg = clamp(iprechg, BQ256XX_IPRECHG_MIN_uA,
735 						BQ256XX_IPRECHG_MAX_uA);
736 
737 	iprechg_reg_code = ((iprechg - BQ256XX_IPRECHG_OFFSET_uA) /
738 			BQ256XX_IPRECHG_STEP_uA) << BQ256XX_IPRECHG_BIT_SHIFT;
739 
740 	return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
741 				BQ256XX_IPRECHG_MASK, iprechg_reg_code);
742 }
743 
bq25618_619_get_prechrg_curr(struct bq256xx_device * bq)744 static int bq25618_619_get_prechrg_curr(struct bq256xx_device *bq)
745 {
746 	unsigned int prechg_and_term_curr_lim;
747 	unsigned int iprechg_reg_code;
748 	int ret;
749 
750 	ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
751 						&prechg_and_term_curr_lim);
752 	if (ret)
753 		return ret;
754 
755 	iprechg_reg_code = (prechg_and_term_curr_lim & BQ256XX_IPRECHG_MASK)
756 						>> BQ256XX_IPRECHG_BIT_SHIFT;
757 
758 	return (iprechg_reg_code * BQ25618_IPRECHG_STEP_uA) +
759 						BQ25618_IPRECHG_OFFSET_uA;
760 }
761 
bq25618_619_set_prechrg_curr(struct bq256xx_device * bq,int iprechg)762 static int bq25618_619_set_prechrg_curr(struct bq256xx_device *bq, int iprechg)
763 {
764 	unsigned int iprechg_reg_code;
765 
766 	iprechg = clamp(iprechg, BQ25618_IPRECHG_MIN_uA,
767 						BQ25618_IPRECHG_MAX_uA);
768 
769 	iprechg_reg_code = ((iprechg - BQ25618_IPRECHG_OFFSET_uA) /
770 			BQ25618_IPRECHG_STEP_uA) << BQ256XX_IPRECHG_BIT_SHIFT;
771 
772 	return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
773 				BQ256XX_IPRECHG_MASK, iprechg_reg_code);
774 }
775 
bq256xx_get_term_curr(struct bq256xx_device * bq)776 static int bq256xx_get_term_curr(struct bq256xx_device *bq)
777 {
778 	unsigned int prechg_and_term_curr_lim;
779 	unsigned int iterm_reg_code;
780 	int ret;
781 
782 	ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
783 						&prechg_and_term_curr_lim);
784 	if (ret)
785 		return ret;
786 
787 	iterm_reg_code = prechg_and_term_curr_lim & BQ256XX_ITERM_MASK;
788 
789 	return (iterm_reg_code * BQ256XX_ITERM_STEP_uA) +
790 						BQ256XX_ITERM_OFFSET_uA;
791 }
792 
bq256xx_set_term_curr(struct bq256xx_device * bq,int iterm)793 static int bq256xx_set_term_curr(struct bq256xx_device *bq, int iterm)
794 {
795 	unsigned int iterm_reg_code;
796 
797 	iterm = clamp(iterm, BQ256XX_ITERM_MIN_uA, BQ256XX_ITERM_MAX_uA);
798 
799 	iterm_reg_code = (iterm - BQ256XX_ITERM_OFFSET_uA) /
800 							BQ256XX_ITERM_STEP_uA;
801 
802 	return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
803 				BQ256XX_ITERM_MASK, iterm_reg_code);
804 }
805 
bq25618_619_get_term_curr(struct bq256xx_device * bq)806 static int bq25618_619_get_term_curr(struct bq256xx_device *bq)
807 {
808 	unsigned int prechg_and_term_curr_lim;
809 	unsigned int iterm_reg_code;
810 	int ret;
811 
812 	ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
813 						&prechg_and_term_curr_lim);
814 	if (ret)
815 		return ret;
816 
817 	iterm_reg_code = prechg_and_term_curr_lim & BQ256XX_ITERM_MASK;
818 
819 	return (iterm_reg_code * BQ25618_ITERM_STEP_uA) +
820 						BQ25618_ITERM_OFFSET_uA;
821 }
822 
bq25618_619_set_term_curr(struct bq256xx_device * bq,int iterm)823 static int bq25618_619_set_term_curr(struct bq256xx_device *bq, int iterm)
824 {
825 	unsigned int iterm_reg_code;
826 
827 	iterm = clamp(iterm, BQ25618_ITERM_MIN_uA, BQ25618_ITERM_MAX_uA);
828 
829 	iterm_reg_code = (iterm - BQ25618_ITERM_OFFSET_uA) /
830 							BQ25618_ITERM_STEP_uA;
831 
832 	return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
833 				BQ256XX_ITERM_MASK, iterm_reg_code);
834 }
835 
bq256xx_get_input_volt_lim(struct bq256xx_device * bq)836 static int bq256xx_get_input_volt_lim(struct bq256xx_device *bq)
837 {
838 	unsigned int charger_control_2;
839 	unsigned int vindpm_reg_code;
840 	int ret;
841 
842 	ret = regmap_read(bq->regmap, BQ256XX_CHARGER_CONTROL_2,
843 						&charger_control_2);
844 	if (ret)
845 		return ret;
846 
847 	vindpm_reg_code = charger_control_2 & BQ256XX_VINDPM_MASK;
848 
849 	return (vindpm_reg_code * BQ256XX_VINDPM_STEP_uV) +
850 						BQ256XX_VINDPM_OFFSET_uV;
851 }
852 
bq256xx_set_input_volt_lim(struct bq256xx_device * bq,int vindpm)853 static int bq256xx_set_input_volt_lim(struct bq256xx_device *bq, int vindpm)
854 {
855 	unsigned int vindpm_reg_code;
856 
857 	vindpm = clamp(vindpm, BQ256XX_VINDPM_MIN_uV, BQ256XX_VINDPM_MAX_uV);
858 
859 	vindpm_reg_code = (vindpm - BQ256XX_VINDPM_OFFSET_uV) /
860 						BQ256XX_VINDPM_STEP_uV;
861 
862 	return regmap_update_bits(bq->regmap, BQ256XX_CHARGER_CONTROL_2,
863 					BQ256XX_VINDPM_MASK, vindpm_reg_code);
864 }
865 
bq256xx_get_input_curr_lim(struct bq256xx_device * bq)866 static int bq256xx_get_input_curr_lim(struct bq256xx_device *bq)
867 {
868 	unsigned int input_current_limit;
869 	unsigned int iindpm_reg_code;
870 	int ret;
871 
872 	ret = regmap_read(bq->regmap, BQ256XX_INPUT_CURRENT_LIMIT,
873 						&input_current_limit);
874 	if (ret)
875 		return ret;
876 
877 	iindpm_reg_code = input_current_limit & BQ256XX_IINDPM_MASK;
878 
879 	return (iindpm_reg_code * BQ256XX_IINDPM_STEP_uA) +
880 						BQ256XX_IINDPM_OFFSET_uA;
881 }
882 
bq256xx_set_input_curr_lim(struct bq256xx_device * bq,int iindpm)883 static int bq256xx_set_input_curr_lim(struct bq256xx_device *bq, int iindpm)
884 {
885 	unsigned int iindpm_reg_code;
886 
887 	iindpm = clamp(iindpm, BQ256XX_IINDPM_MIN_uA, BQ256XX_IINDPM_MAX_uA);
888 
889 	iindpm_reg_code = (iindpm - BQ256XX_IINDPM_OFFSET_uA) /
890 							BQ256XX_IINDPM_STEP_uA;
891 
892 	return regmap_update_bits(bq->regmap, BQ256XX_INPUT_CURRENT_LIMIT,
893 					BQ256XX_IINDPM_MASK, iindpm_reg_code);
894 }
895 
bq256xx_charger_reset(void * data)896 static void bq256xx_charger_reset(void *data)
897 {
898 	struct bq256xx_device *bq = data;
899 
900 	regmap_update_bits(bq->regmap, BQ256XX_PART_INFORMATION,
901 					BQ256XX_REG_RST, BQ256XX_REG_RST);
902 
903 	if (!IS_ERR_OR_NULL(bq->usb2_phy))
904 		usb_unregister_notifier(bq->usb2_phy, &bq->usb_nb);
905 
906 	if (!IS_ERR_OR_NULL(bq->usb3_phy))
907 		usb_unregister_notifier(bq->usb3_phy, &bq->usb_nb);
908 }
909 
bq256xx_set_charger_property(struct power_supply * psy,enum power_supply_property prop,const union power_supply_propval * val)910 static int bq256xx_set_charger_property(struct power_supply *psy,
911 		enum power_supply_property prop,
912 		const union power_supply_propval *val)
913 {
914 	struct bq256xx_device *bq = power_supply_get_drvdata(psy);
915 	int ret = -EINVAL;
916 
917 	switch (prop) {
918 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
919 		ret = bq->chip_info->bq256xx_set_iindpm(bq, val->intval);
920 		if (ret)
921 			return ret;
922 		break;
923 
924 	case POWER_SUPPLY_PROP_STATUS:
925 		break;
926 
927 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
928 		ret = bq->chip_info->bq256xx_set_vbatreg(bq, val->intval);
929 		if (ret)
930 			return ret;
931 		break;
932 
933 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
934 		ret = bq->chip_info->bq256xx_set_ichg(bq, val->intval);
935 		if (ret)
936 			return ret;
937 		break;
938 
939 	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
940 		ret = bq->chip_info->bq256xx_set_iprechg(bq, val->intval);
941 		if (ret)
942 			return ret;
943 		break;
944 
945 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
946 		ret = bq->chip_info->bq256xx_set_iterm(bq, val->intval);
947 		if (ret)
948 			return ret;
949 		break;
950 
951 	case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
952 		ret = bq->chip_info->bq256xx_set_vindpm(bq, val->intval);
953 		if (ret)
954 			return ret;
955 		break;
956 
957 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
958 		ret = bq->chip_info->bq256xx_set_charge_type(bq, val->intval);
959 		if (ret)
960 			return ret;
961 		break;
962 
963 	default:
964 		break;
965 	}
966 
967 	return ret;
968 }
969 
970 
bq256xx_get_battery_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)971 static int bq256xx_get_battery_property(struct power_supply *psy,
972 				enum power_supply_property psp,
973 				union power_supply_propval *val)
974 {
975 	struct bq256xx_device *bq = power_supply_get_drvdata(psy);
976 
977 	switch (psp) {
978 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
979 		val->intval = bq->init_data.ichg_max;
980 		break;
981 
982 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
983 		val->intval = bq->init_data.vbatreg_max;
984 		break;
985 
986 	default:
987 		return -EINVAL;
988 	}
989 
990 	return 0;
991 }
992 
bq256xx_get_charger_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)993 static int bq256xx_get_charger_property(struct power_supply *psy,
994 				enum power_supply_property psp,
995 				union power_supply_propval *val)
996 {
997 	struct bq256xx_device *bq = power_supply_get_drvdata(psy);
998 	struct bq256xx_state state;
999 	int ret = 0;
1000 
1001 	mutex_lock(&bq->lock);
1002 	ret = bq256xx_get_state(bq, &state);
1003 	mutex_unlock(&bq->lock);
1004 	if (ret)
1005 		return ret;
1006 
1007 	switch (psp) {
1008 	case POWER_SUPPLY_PROP_STATUS:
1009 		if (state.vbus_stat == BQ256XX_VBUS_STAT_NO_INPUT ||
1010 		    state.vbus_stat == BQ256XX_VBUS_STAT_USB_OTG)
1011 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1012 		else if (state.chrg_stat == BQ256XX_CHRG_STAT_NOT_CHRGING)
1013 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1014 		else if (state.chrg_stat == BQ256XX_CHRG_STAT_CHRG_TERM)
1015 			val->intval = POWER_SUPPLY_STATUS_FULL;
1016 		else
1017 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
1018 		break;
1019 
1020 	case POWER_SUPPLY_PROP_HEALTH:
1021 		val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
1022 		if (state.wdt_fault) {
1023 			val->intval =
1024 				POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE;
1025 		} else if (state.bat_fault) {
1026 			val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1027 		} else {
1028 			switch (state.chrg_stat) {
1029 			case BQ256XX_CHRG_FAULT_INPUT:
1030 				val->intval =
1031 					POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1032 				break;
1033 			case BQ256XX_CHRG_FAULT_THERM:
1034 				val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
1035 				break;
1036 			case BQ256XX_CHRG_FAULT_CST_EXPIRE:
1037 				val->intval =
1038 				POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
1039 				break;
1040 			default:
1041 				break;
1042 			}
1043 
1044 			switch (state.ntc_fault) {
1045 			case BQ256XX_NTC_FAULT_WARM:
1046 				val->intval = POWER_SUPPLY_HEALTH_WARM;
1047 				break;
1048 			case BQ256XX_NTC_FAULT_COOL:
1049 				val->intval = POWER_SUPPLY_HEALTH_COOL;
1050 				break;
1051 			case BQ256XX_NTC_FAULT_COLD:
1052 				val->intval = POWER_SUPPLY_HEALTH_COLD;
1053 				break;
1054 			case BQ256XX_NTC_FAULT_HOT:
1055 				val->intval = POWER_SUPPLY_HEALTH_HOT;
1056 				break;
1057 			default:
1058 				val->intval = POWER_SUPPLY_HEALTH_GOOD;
1059 				break;
1060 			}
1061 		}
1062 		break;
1063 
1064 	case POWER_SUPPLY_PROP_USB_TYPE:
1065 		if (bq->chip_info->has_usb_detect) {
1066 			switch (state.vbus_stat) {
1067 			case BQ256XX_VBUS_STAT_USB_SDP:
1068 				val->intval = POWER_SUPPLY_USB_TYPE_SDP;
1069 				break;
1070 			case BQ256XX_VBUS_STAT_USB_CDP:
1071 				val->intval = POWER_SUPPLY_USB_TYPE_CDP;
1072 				break;
1073 			case BQ256XX_VBUS_STAT_USB_DCP:
1074 				val->intval = POWER_SUPPLY_USB_TYPE_DCP;
1075 				break;
1076 			case BQ256XX_VBUS_STAT_USB_OTG:
1077 				val->intval = POWER_SUPPLY_USB_TYPE_ACA;
1078 				break;
1079 			default:
1080 				val->intval = POWER_SUPPLY_USB_TYPE_UNKNOWN;
1081 				break;
1082 			}
1083 		} else {
1084 			switch (state.vbus_stat) {
1085 			case BQ256XX_VBUS_STAT_USB_SDP:
1086 				val->intval = POWER_SUPPLY_USB_TYPE_SDP;
1087 				break;
1088 			case BQ256XX_VBUS_STAT_USB_OTG:
1089 				val->intval = POWER_SUPPLY_USB_TYPE_ACA;
1090 				break;
1091 			default:
1092 				val->intval = POWER_SUPPLY_USB_TYPE_UNKNOWN;
1093 				break;
1094 			}
1095 		}
1096 		break;
1097 
1098 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
1099 		switch (state.chrg_stat) {
1100 		case BQ256XX_CHRG_STAT_NOT_CHRGING:
1101 			val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
1102 			break;
1103 		case BQ256XX_CHRG_STAT_PRECHRGING:
1104 			val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1105 			break;
1106 		case BQ256XX_CHRG_STAT_FAST_CHRGING:
1107 			val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
1108 			break;
1109 		case BQ256XX_CHRG_STAT_CHRG_TERM:
1110 			val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1111 			break;
1112 		default:
1113 			val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
1114 		}
1115 		break;
1116 
1117 	case POWER_SUPPLY_PROP_MANUFACTURER:
1118 		val->strval = BQ256XX_MANUFACTURER;
1119 		break;
1120 
1121 	case POWER_SUPPLY_PROP_MODEL_NAME:
1122 		val->strval = bq->model_name;
1123 		break;
1124 
1125 	case POWER_SUPPLY_PROP_ONLINE:
1126 		val->intval = state.online;
1127 		break;
1128 
1129 	case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
1130 		ret = bq->chip_info->bq256xx_get_vindpm(bq);
1131 		if (ret < 0)
1132 			return ret;
1133 		val->intval = ret;
1134 		break;
1135 
1136 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
1137 		ret = bq->chip_info->bq256xx_get_iindpm(bq);
1138 		if (ret < 0)
1139 			return ret;
1140 		val->intval = ret;
1141 		break;
1142 
1143 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1144 		ret = bq->chip_info->bq256xx_get_vbatreg(bq);
1145 		if (ret < 0)
1146 			return ret;
1147 		val->intval = ret;
1148 		break;
1149 
1150 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1151 		ret = bq->chip_info->bq256xx_get_ichg(bq);
1152 		if (ret < 0)
1153 			return ret;
1154 		val->intval = ret;
1155 		break;
1156 
1157 	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
1158 		ret = bq->chip_info->bq256xx_get_iprechg(bq);
1159 		if (ret < 0)
1160 			return ret;
1161 		val->intval = ret;
1162 		break;
1163 
1164 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
1165 		ret = bq->chip_info->bq256xx_get_iterm(bq);
1166 		if (ret < 0)
1167 			return ret;
1168 		val->intval = ret;
1169 		break;
1170 
1171 	default:
1172 		return -EINVAL;
1173 	}
1174 
1175 	return ret;
1176 }
1177 
bq256xx_state_changed(struct bq256xx_device * bq,struct bq256xx_state * new_state)1178 static bool bq256xx_state_changed(struct bq256xx_device *bq,
1179 				  struct bq256xx_state *new_state)
1180 {
1181 	struct bq256xx_state old_state;
1182 
1183 	mutex_lock(&bq->lock);
1184 	old_state = bq->state;
1185 	mutex_unlock(&bq->lock);
1186 
1187 	return memcmp(&old_state, new_state, sizeof(struct bq256xx_state)) != 0;
1188 }
1189 
bq256xx_irq_handler_thread(int irq,void * private)1190 static irqreturn_t bq256xx_irq_handler_thread(int irq, void *private)
1191 {
1192 	struct bq256xx_device *bq = private;
1193 	struct bq256xx_state state;
1194 	int ret;
1195 
1196 	ret = bq256xx_get_state(bq, &state);
1197 	if (ret < 0)
1198 		goto irq_out;
1199 
1200 	if (!bq256xx_state_changed(bq, &state))
1201 		goto irq_out;
1202 
1203 	mutex_lock(&bq->lock);
1204 	bq->state = state;
1205 	mutex_unlock(&bq->lock);
1206 
1207 	power_supply_changed(bq->charger);
1208 
1209 irq_out:
1210 	return IRQ_HANDLED;
1211 }
1212 
1213 static enum power_supply_property bq256xx_power_supply_props[] = {
1214 	POWER_SUPPLY_PROP_MANUFACTURER,
1215 	POWER_SUPPLY_PROP_MODEL_NAME,
1216 	POWER_SUPPLY_PROP_STATUS,
1217 	POWER_SUPPLY_PROP_ONLINE,
1218 	POWER_SUPPLY_PROP_HEALTH,
1219 	POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT,
1220 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
1221 	POWER_SUPPLY_PROP_CHARGE_TYPE,
1222 	POWER_SUPPLY_PROP_USB_TYPE,
1223 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1224 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1225 	POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
1226 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
1227 };
1228 
1229 static enum power_supply_property bq256xx_battery_props[] = {
1230 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
1231 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
1232 };
1233 
bq256xx_property_is_writeable(struct power_supply * psy,enum power_supply_property prop)1234 static int bq256xx_property_is_writeable(struct power_supply *psy,
1235 					 enum power_supply_property prop)
1236 {
1237 	switch (prop) {
1238 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
1239 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1240 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1241 	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
1242 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
1243 	case POWER_SUPPLY_PROP_STATUS:
1244 	case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
1245 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
1246 		return true;
1247 	default:
1248 		return false;
1249 	}
1250 }
1251 
1252 static const struct power_supply_desc bq256xx_power_supply_desc = {
1253 	.name = "bq256xx-charger",
1254 	.type = POWER_SUPPLY_TYPE_USB,
1255 	.usb_types = bq256xx_usb_type,
1256 	.num_usb_types = ARRAY_SIZE(bq256xx_usb_type),
1257 	.properties = bq256xx_power_supply_props,
1258 	.num_properties = ARRAY_SIZE(bq256xx_power_supply_props),
1259 	.get_property = bq256xx_get_charger_property,
1260 	.set_property = bq256xx_set_charger_property,
1261 	.property_is_writeable = bq256xx_property_is_writeable,
1262 };
1263 
1264 static struct power_supply_desc bq256xx_battery_desc = {
1265 	.name			= "bq256xx-battery",
1266 	.type			= POWER_SUPPLY_TYPE_BATTERY,
1267 	.get_property		= bq256xx_get_battery_property,
1268 	.properties		= bq256xx_battery_props,
1269 	.num_properties		= ARRAY_SIZE(bq256xx_battery_props),
1270 	.property_is_writeable	= bq256xx_property_is_writeable,
1271 };
1272 
1273 
bq256xx_is_volatile_reg(struct device * dev,unsigned int reg)1274 static bool bq256xx_is_volatile_reg(struct device *dev, unsigned int reg)
1275 {
1276 	switch (reg) {
1277 	case BQ256XX_INPUT_CURRENT_LIMIT:
1278 	case BQ256XX_CHARGER_STATUS_0...BQ256XX_CHARGER_STATUS_2:
1279 		return true;
1280 	default:
1281 		return false;
1282 	}
1283 }
1284 
1285 static const struct regmap_config bq25600_regmap_config = {
1286 	.reg_bits = 8,
1287 	.val_bits = 8,
1288 
1289 	.max_register = BQ256XX_PART_INFORMATION,
1290 	.reg_defaults	= bq2560x_reg_defs,
1291 	.num_reg_defaults = ARRAY_SIZE(bq2560x_reg_defs),
1292 	.cache_type = REGCACHE_FLAT,
1293 	.volatile_reg = bq256xx_is_volatile_reg,
1294 };
1295 
1296 static const struct regmap_config bq25611d_regmap_config = {
1297 	.reg_bits = 8,
1298 	.val_bits = 8,
1299 
1300 	.max_register = BQ256XX_CHARGER_CONTROL_4,
1301 	.reg_defaults	= bq25611d_reg_defs,
1302 	.num_reg_defaults = ARRAY_SIZE(bq25611d_reg_defs),
1303 	.cache_type = REGCACHE_FLAT,
1304 	.volatile_reg = bq256xx_is_volatile_reg,
1305 };
1306 
1307 static const struct regmap_config bq25618_619_regmap_config = {
1308 	.reg_bits = 8,
1309 	.val_bits = 8,
1310 
1311 	.max_register = BQ256XX_CHARGER_CONTROL_4,
1312 	.reg_defaults	= bq25618_619_reg_defs,
1313 	.num_reg_defaults = ARRAY_SIZE(bq25618_619_reg_defs),
1314 	.cache_type = REGCACHE_FLAT,
1315 	.volatile_reg = bq256xx_is_volatile_reg,
1316 };
1317 
1318 static const struct bq256xx_chip_info bq256xx_chip_info_tbl[] = {
1319 	[BQ25600] = {
1320 		.model_id = BQ25600,
1321 		.bq256xx_regmap_config = &bq25600_regmap_config,
1322 		.bq256xx_get_ichg = bq256xx_get_ichg_curr,
1323 		.bq256xx_get_iindpm = bq256xx_get_input_curr_lim,
1324 		.bq256xx_get_vbatreg = bq2560x_get_chrg_volt,
1325 		.bq256xx_get_iterm = bq256xx_get_term_curr,
1326 		.bq256xx_get_iprechg = bq256xx_get_prechrg_curr,
1327 		.bq256xx_get_vindpm = bq256xx_get_input_volt_lim,
1328 		.bq256xx_set_ts_ignore = NULL,
1329 
1330 		.bq256xx_set_ichg = bq256xx_set_ichg_curr,
1331 		.bq256xx_set_iindpm = bq256xx_set_input_curr_lim,
1332 		.bq256xx_set_vbatreg = bq2560x_set_chrg_volt,
1333 		.bq256xx_set_iterm = bq256xx_set_term_curr,
1334 		.bq256xx_set_iprechg = bq256xx_set_prechrg_curr,
1335 		.bq256xx_set_vindpm = bq256xx_set_input_volt_lim,
1336 		.bq256xx_set_charge_type = bq256xx_set_charge_type,
1337 
1338 		.bq256xx_def_ichg = BQ2560X_ICHG_DEF_uA,
1339 		.bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA,
1340 		.bq256xx_def_vbatreg = BQ2560X_VBATREG_DEF_uV,
1341 		.bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA,
1342 		.bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA,
1343 		.bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV,
1344 
1345 		.bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA,
1346 		.bq256xx_max_vbatreg = BQ2560X_VBATREG_MAX_uV,
1347 
1348 		.has_usb_detect = false,
1349 	},
1350 
1351 	[BQ25600D] = {
1352 		.model_id = BQ25600D,
1353 		.bq256xx_regmap_config = &bq25600_regmap_config,
1354 		.bq256xx_get_ichg = bq256xx_get_ichg_curr,
1355 		.bq256xx_get_iindpm = bq256xx_get_input_curr_lim,
1356 		.bq256xx_get_vbatreg = bq2560x_get_chrg_volt,
1357 		.bq256xx_get_iterm = bq256xx_get_term_curr,
1358 		.bq256xx_get_iprechg = bq256xx_get_prechrg_curr,
1359 		.bq256xx_get_vindpm = bq256xx_get_input_volt_lim,
1360 
1361 		.bq256xx_set_ichg = bq256xx_set_ichg_curr,
1362 		.bq256xx_set_iindpm = bq256xx_set_input_curr_lim,
1363 		.bq256xx_set_vbatreg = bq2560x_set_chrg_volt,
1364 		.bq256xx_set_iterm = bq256xx_set_term_curr,
1365 		.bq256xx_set_iprechg = bq256xx_set_prechrg_curr,
1366 		.bq256xx_set_vindpm = bq256xx_set_input_volt_lim,
1367 		.bq256xx_set_charge_type = bq256xx_set_charge_type,
1368 		.bq256xx_set_ts_ignore = NULL,
1369 
1370 		.bq256xx_def_ichg = BQ2560X_ICHG_DEF_uA,
1371 		.bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA,
1372 		.bq256xx_def_vbatreg = BQ2560X_VBATREG_DEF_uV,
1373 		.bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA,
1374 		.bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA,
1375 		.bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV,
1376 
1377 		.bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA,
1378 		.bq256xx_max_vbatreg = BQ2560X_VBATREG_MAX_uV,
1379 
1380 		.has_usb_detect = true,
1381 	},
1382 
1383 	[BQ25601] = {
1384 		.model_id = BQ25601,
1385 		.bq256xx_regmap_config = &bq25600_regmap_config,
1386 		.bq256xx_get_ichg = bq256xx_get_ichg_curr,
1387 		.bq256xx_get_iindpm = bq256xx_get_input_curr_lim,
1388 		.bq256xx_get_vbatreg = bq2560x_get_chrg_volt,
1389 		.bq256xx_get_iterm = bq256xx_get_term_curr,
1390 		.bq256xx_get_iprechg = bq256xx_get_prechrg_curr,
1391 		.bq256xx_get_vindpm = bq256xx_get_input_volt_lim,
1392 
1393 		.bq256xx_set_ichg = bq256xx_set_ichg_curr,
1394 		.bq256xx_set_iindpm = bq256xx_set_input_curr_lim,
1395 		.bq256xx_set_vbatreg = bq2560x_set_chrg_volt,
1396 		.bq256xx_set_iterm = bq256xx_set_term_curr,
1397 		.bq256xx_set_iprechg = bq256xx_set_prechrg_curr,
1398 		.bq256xx_set_vindpm = bq256xx_set_input_volt_lim,
1399 		.bq256xx_set_charge_type = bq256xx_set_charge_type,
1400 		.bq256xx_set_ts_ignore = NULL,
1401 
1402 		.bq256xx_def_ichg = BQ2560X_ICHG_DEF_uA,
1403 		.bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA,
1404 		.bq256xx_def_vbatreg = BQ2560X_VBATREG_DEF_uV,
1405 		.bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA,
1406 		.bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA,
1407 		.bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV,
1408 
1409 		.bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA,
1410 		.bq256xx_max_vbatreg = BQ2560X_VBATREG_MAX_uV,
1411 
1412 		.has_usb_detect = false,
1413 	},
1414 
1415 	[BQ25601D] = {
1416 		.model_id = BQ25601D,
1417 		.bq256xx_regmap_config = &bq25600_regmap_config,
1418 		.bq256xx_get_ichg = bq256xx_get_ichg_curr,
1419 		.bq256xx_get_iindpm = bq256xx_get_input_curr_lim,
1420 		.bq256xx_get_vbatreg = bq25601d_get_chrg_volt,
1421 		.bq256xx_get_iterm = bq256xx_get_term_curr,
1422 		.bq256xx_get_iprechg = bq256xx_get_prechrg_curr,
1423 		.bq256xx_get_vindpm = bq256xx_get_input_volt_lim,
1424 
1425 		.bq256xx_set_ichg = bq256xx_set_ichg_curr,
1426 		.bq256xx_set_iindpm = bq256xx_set_input_curr_lim,
1427 		.bq256xx_set_vbatreg = bq25601d_set_chrg_volt,
1428 		.bq256xx_set_iterm = bq256xx_set_term_curr,
1429 		.bq256xx_set_iprechg = bq256xx_set_prechrg_curr,
1430 		.bq256xx_set_vindpm = bq256xx_set_input_volt_lim,
1431 		.bq256xx_set_charge_type = bq256xx_set_charge_type,
1432 		.bq256xx_set_ts_ignore = NULL,
1433 
1434 		.bq256xx_def_ichg = BQ2560X_ICHG_DEF_uA,
1435 		.bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA,
1436 		.bq256xx_def_vbatreg = BQ2560X_VBATREG_DEF_uV,
1437 		.bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA,
1438 		.bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA,
1439 		.bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV,
1440 
1441 		.bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA,
1442 		.bq256xx_max_vbatreg = BQ2560X_VBATREG_MAX_uV,
1443 
1444 		.has_usb_detect = true,
1445 	},
1446 
1447 	[BQ25611D] = {
1448 		.model_id = BQ25611D,
1449 		.bq256xx_regmap_config = &bq25611d_regmap_config,
1450 		.bq256xx_get_ichg = bq256xx_get_ichg_curr,
1451 		.bq256xx_get_iindpm = bq256xx_get_input_curr_lim,
1452 		.bq256xx_get_vbatreg = bq25611d_get_chrg_volt,
1453 		.bq256xx_get_iterm = bq256xx_get_term_curr,
1454 		.bq256xx_get_iprechg = bq256xx_get_prechrg_curr,
1455 		.bq256xx_get_vindpm = bq256xx_get_input_volt_lim,
1456 
1457 		.bq256xx_set_ichg = bq256xx_set_ichg_curr,
1458 		.bq256xx_set_iindpm = bq256xx_set_input_curr_lim,
1459 		.bq256xx_set_vbatreg = bq25611d_set_chrg_volt,
1460 		.bq256xx_set_iterm = bq256xx_set_term_curr,
1461 		.bq256xx_set_iprechg = bq256xx_set_prechrg_curr,
1462 		.bq256xx_set_vindpm = bq256xx_set_input_volt_lim,
1463 		.bq256xx_set_charge_type = bq256xx_set_charge_type,
1464 		.bq256xx_set_ts_ignore = bq256xx_set_ts_ignore,
1465 
1466 		.bq256xx_def_ichg = BQ25611D_ICHG_DEF_uA,
1467 		.bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA,
1468 		.bq256xx_def_vbatreg = BQ25611D_VBATREG_DEF_uV,
1469 		.bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA,
1470 		.bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA,
1471 		.bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV,
1472 
1473 		.bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA,
1474 		.bq256xx_max_vbatreg = BQ25611D_VBATREG_MAX_uV,
1475 
1476 		.has_usb_detect = true,
1477 	},
1478 
1479 	[BQ25618] = {
1480 		.model_id = BQ25618,
1481 		.bq256xx_regmap_config = &bq25618_619_regmap_config,
1482 		.bq256xx_get_ichg = bq25618_619_get_ichg_curr,
1483 		.bq256xx_get_iindpm = bq256xx_get_input_curr_lim,
1484 		.bq256xx_get_vbatreg = bq25618_619_get_chrg_volt,
1485 		.bq256xx_get_iterm = bq25618_619_get_term_curr,
1486 		.bq256xx_get_iprechg = bq25618_619_get_prechrg_curr,
1487 		.bq256xx_get_vindpm = bq256xx_get_input_volt_lim,
1488 
1489 		.bq256xx_set_ichg = bq25618_619_set_ichg_curr,
1490 		.bq256xx_set_iindpm = bq256xx_set_input_curr_lim,
1491 		.bq256xx_set_vbatreg = bq25618_619_set_chrg_volt,
1492 		.bq256xx_set_iterm = bq25618_619_set_term_curr,
1493 		.bq256xx_set_iprechg = bq25618_619_set_prechrg_curr,
1494 		.bq256xx_set_vindpm = bq256xx_set_input_volt_lim,
1495 		.bq256xx_set_charge_type = bq256xx_set_charge_type,
1496 		.bq256xx_set_ts_ignore = bq256xx_set_ts_ignore,
1497 
1498 		.bq256xx_def_ichg = BQ25618_ICHG_DEF_uA,
1499 		.bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA,
1500 		.bq256xx_def_vbatreg = BQ25618_VBATREG_DEF_uV,
1501 		.bq256xx_def_iterm = BQ25618_ITERM_DEF_uA,
1502 		.bq256xx_def_iprechg = BQ25618_IPRECHG_DEF_uA,
1503 		.bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV,
1504 
1505 		.bq256xx_max_ichg = BQ25618_ICHG_MAX_uA,
1506 		.bq256xx_max_vbatreg = BQ25618_VBATREG_MAX_uV,
1507 
1508 		.has_usb_detect = false,
1509 	},
1510 
1511 	[BQ25619] = {
1512 		.model_id = BQ25619,
1513 		.bq256xx_regmap_config = &bq25618_619_regmap_config,
1514 		.bq256xx_get_ichg = bq25618_619_get_ichg_curr,
1515 		.bq256xx_get_iindpm = bq256xx_get_input_curr_lim,
1516 		.bq256xx_get_vbatreg = bq25618_619_get_chrg_volt,
1517 		.bq256xx_get_iterm = bq25618_619_get_term_curr,
1518 		.bq256xx_get_iprechg = bq25618_619_get_prechrg_curr,
1519 		.bq256xx_get_vindpm = bq256xx_get_input_volt_lim,
1520 
1521 		.bq256xx_set_ichg = bq25618_619_set_ichg_curr,
1522 		.bq256xx_set_iindpm = bq256xx_set_input_curr_lim,
1523 		.bq256xx_set_vbatreg = bq25618_619_set_chrg_volt,
1524 		.bq256xx_set_iterm = bq25618_619_set_term_curr,
1525 		.bq256xx_set_iprechg = bq25618_619_set_prechrg_curr,
1526 		.bq256xx_set_vindpm = bq256xx_set_input_volt_lim,
1527 		.bq256xx_set_charge_type = bq256xx_set_charge_type,
1528 		.bq256xx_set_ts_ignore = bq256xx_set_ts_ignore,
1529 
1530 		.bq256xx_def_ichg = BQ25618_ICHG_DEF_uA,
1531 		.bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA,
1532 		.bq256xx_def_vbatreg = BQ25618_VBATREG_DEF_uV,
1533 		.bq256xx_def_iterm = BQ25618_ITERM_DEF_uA,
1534 		.bq256xx_def_iprechg = BQ25618_IPRECHG_DEF_uA,
1535 		.bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV,
1536 
1537 		.bq256xx_max_ichg = BQ25618_ICHG_MAX_uA,
1538 		.bq256xx_max_vbatreg = BQ25618_VBATREG_MAX_uV,
1539 
1540 		.has_usb_detect = false,
1541 	},
1542 };
1543 
bq256xx_power_supply_init(struct bq256xx_device * bq,struct power_supply_config * psy_cfg,struct device * dev)1544 static int bq256xx_power_supply_init(struct bq256xx_device *bq,
1545 		struct power_supply_config *psy_cfg, struct device *dev)
1546 {
1547 	bq->charger = devm_power_supply_register(bq->dev,
1548 						 &bq256xx_power_supply_desc,
1549 						 psy_cfg);
1550 	if (IS_ERR(bq->charger)) {
1551 		dev_err(dev, "power supply register charger failed\n");
1552 		return PTR_ERR(bq->charger);
1553 	}
1554 
1555 	bq->battery = devm_power_supply_register(bq->dev,
1556 						      &bq256xx_battery_desc,
1557 						      psy_cfg);
1558 	if (IS_ERR(bq->battery)) {
1559 		dev_err(dev, "power supply register battery failed\n");
1560 		return PTR_ERR(bq->battery);
1561 	}
1562 	return 0;
1563 }
1564 
bq256xx_hw_init(struct bq256xx_device * bq)1565 static int bq256xx_hw_init(struct bq256xx_device *bq)
1566 {
1567 	struct power_supply_battery_info *bat_info;
1568 	int wd_reg_val = BQ256XX_WATCHDOG_DIS;
1569 	int ret = 0;
1570 	int i;
1571 
1572 	for (i = 0; i < BQ256XX_NUM_WD_VAL; i++) {
1573 		if (bq->watchdog_timer == bq256xx_watchdog_time[i]) {
1574 			wd_reg_val = i;
1575 			break;
1576 		}
1577 		if (i + 1 < BQ256XX_NUM_WD_VAL &&
1578 		    bq->watchdog_timer > bq256xx_watchdog_time[i] &&
1579 		    bq->watchdog_timer < bq256xx_watchdog_time[i + 1])
1580 			wd_reg_val = i;
1581 	}
1582 	ret = regmap_update_bits(bq->regmap, BQ256XX_CHARGER_CONTROL_1,
1583 				 BQ256XX_WATCHDOG_MASK, wd_reg_val <<
1584 						BQ256XX_WDT_BIT_SHIFT);
1585 	if (ret)
1586 		return ret;
1587 
1588 	ret = power_supply_get_battery_info(bq->charger, &bat_info);
1589 	if (ret == -ENOMEM)
1590 		return ret;
1591 
1592 	if (ret) {
1593 		dev_warn(bq->dev, "battery info missing, default values will be applied\n");
1594 
1595 		bat_info->constant_charge_current_max_ua =
1596 				bq->chip_info->bq256xx_def_ichg;
1597 
1598 		bat_info->constant_charge_voltage_max_uv =
1599 				bq->chip_info->bq256xx_def_vbatreg;
1600 
1601 		bat_info->precharge_current_ua =
1602 				bq->chip_info->bq256xx_def_iprechg;
1603 
1604 		bat_info->charge_term_current_ua =
1605 				bq->chip_info->bq256xx_def_iterm;
1606 
1607 		bq->init_data.ichg_max =
1608 				bq->chip_info->bq256xx_max_ichg;
1609 
1610 		bq->init_data.vbatreg_max =
1611 				bq->chip_info->bq256xx_max_vbatreg;
1612 	} else {
1613 		bq->init_data.ichg_max =
1614 			bat_info->constant_charge_current_max_ua;
1615 
1616 		bq->init_data.vbatreg_max =
1617 			bat_info->constant_charge_voltage_max_uv;
1618 	}
1619 
1620 	ret = bq->chip_info->bq256xx_set_vindpm(bq, bq->init_data.vindpm);
1621 	if (ret)
1622 		return ret;
1623 
1624 	ret = bq->chip_info->bq256xx_set_iindpm(bq, bq->init_data.iindpm);
1625 	if (ret)
1626 		return ret;
1627 
1628 	ret = bq->chip_info->bq256xx_set_ichg(bq,
1629 				bq->chip_info->bq256xx_def_ichg);
1630 	if (ret)
1631 		return ret;
1632 
1633 	ret = bq->chip_info->bq256xx_set_iprechg(bq,
1634 				bat_info->precharge_current_ua);
1635 	if (ret)
1636 		return ret;
1637 
1638 	ret = bq->chip_info->bq256xx_set_vbatreg(bq,
1639 				bq->chip_info->bq256xx_def_vbatreg);
1640 	if (ret)
1641 		return ret;
1642 
1643 	ret = bq->chip_info->bq256xx_set_iterm(bq,
1644 				bat_info->charge_term_current_ua);
1645 	if (ret)
1646 		return ret;
1647 
1648 	if (bq->chip_info->bq256xx_set_ts_ignore) {
1649 		ret = bq->chip_info->bq256xx_set_ts_ignore(bq, bq->init_data.ts_ignore);
1650 		if (ret)
1651 			return ret;
1652 	}
1653 
1654 	power_supply_put_battery_info(bq->charger, bat_info);
1655 
1656 	return 0;
1657 }
1658 
bq256xx_parse_dt(struct bq256xx_device * bq,struct power_supply_config * psy_cfg,struct device * dev)1659 static int bq256xx_parse_dt(struct bq256xx_device *bq,
1660 		struct power_supply_config *psy_cfg, struct device *dev)
1661 {
1662 	int ret = 0;
1663 
1664 	psy_cfg->drv_data = bq;
1665 	psy_cfg->of_node = dev->of_node;
1666 
1667 	ret = device_property_read_u32(bq->dev, "ti,watchdog-timeout-ms",
1668 				       &bq->watchdog_timer);
1669 	if (ret)
1670 		bq->watchdog_timer = BQ256XX_WATCHDOG_DIS;
1671 
1672 	if (bq->watchdog_timer > BQ256XX_WATCHDOG_MAX ||
1673 	    bq->watchdog_timer < BQ256XX_WATCHDOG_DIS)
1674 		return -EINVAL;
1675 
1676 	ret = device_property_read_u32(bq->dev,
1677 				       "input-voltage-limit-microvolt",
1678 				       &bq->init_data.vindpm);
1679 	if (ret)
1680 		bq->init_data.vindpm = bq->chip_info->bq256xx_def_vindpm;
1681 
1682 	ret = device_property_read_u32(bq->dev,
1683 				       "input-current-limit-microamp",
1684 				       &bq->init_data.iindpm);
1685 	if (ret)
1686 		bq->init_data.iindpm = bq->chip_info->bq256xx_def_iindpm;
1687 
1688 	bq->init_data.ts_ignore = device_property_read_bool(bq->dev, "ti,no-thermistor");
1689 
1690 	return 0;
1691 }
1692 
bq256xx_probe(struct i2c_client * client)1693 static int bq256xx_probe(struct i2c_client *client)
1694 {
1695 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
1696 	struct device *dev = &client->dev;
1697 	struct bq256xx_device *bq;
1698 	struct power_supply_config psy_cfg = { };
1699 
1700 	int ret;
1701 
1702 	bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
1703 	if (!bq)
1704 		return -ENOMEM;
1705 
1706 	bq->client = client;
1707 	bq->dev = dev;
1708 	bq->chip_info = &bq256xx_chip_info_tbl[id->driver_data];
1709 
1710 	mutex_init(&bq->lock);
1711 
1712 	strncpy(bq->model_name, id->name, I2C_NAME_SIZE);
1713 
1714 	bq->regmap = devm_regmap_init_i2c(client,
1715 					bq->chip_info->bq256xx_regmap_config);
1716 
1717 	if (IS_ERR(bq->regmap)) {
1718 		dev_err(dev, "Failed to allocate register map\n");
1719 		return PTR_ERR(bq->regmap);
1720 	}
1721 
1722 	i2c_set_clientdata(client, bq);
1723 
1724 	ret = bq256xx_parse_dt(bq, &psy_cfg, dev);
1725 	if (ret) {
1726 		dev_err(dev, "Failed to read device tree properties%d\n", ret);
1727 		return ret;
1728 	}
1729 
1730 	ret = devm_add_action_or_reset(dev, bq256xx_charger_reset, bq);
1731 	if (ret)
1732 		return ret;
1733 
1734 	/* OTG reporting */
1735 	bq->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1736 	if (!IS_ERR_OR_NULL(bq->usb2_phy)) {
1737 		INIT_WORK(&bq->usb_work, bq256xx_usb_work);
1738 		bq->usb_nb.notifier_call = bq256xx_usb_notifier;
1739 		usb_register_notifier(bq->usb2_phy, &bq->usb_nb);
1740 	}
1741 
1742 	bq->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1743 	if (!IS_ERR_OR_NULL(bq->usb3_phy)) {
1744 		INIT_WORK(&bq->usb_work, bq256xx_usb_work);
1745 		bq->usb_nb.notifier_call = bq256xx_usb_notifier;
1746 		usb_register_notifier(bq->usb3_phy, &bq->usb_nb);
1747 	}
1748 
1749 	if (client->irq) {
1750 		ret = devm_request_threaded_irq(dev, client->irq, NULL,
1751 						bq256xx_irq_handler_thread,
1752 						IRQF_TRIGGER_FALLING |
1753 						IRQF_ONESHOT,
1754 						dev_name(&client->dev), bq);
1755 		if (ret < 0) {
1756 			dev_err(dev, "get irq fail: %d\n", ret);
1757 			return ret;
1758 		}
1759 	}
1760 
1761 	ret = bq256xx_power_supply_init(bq, &psy_cfg, dev);
1762 	if (ret) {
1763 		dev_err(dev, "Failed to register power supply\n");
1764 		return ret;
1765 	}
1766 
1767 	ret = bq256xx_hw_init(bq);
1768 	if (ret) {
1769 		dev_err(dev, "Cannot initialize the chip.\n");
1770 		return ret;
1771 	}
1772 
1773 	return ret;
1774 }
1775 
1776 static const struct i2c_device_id bq256xx_i2c_ids[] = {
1777 	{ "bq25600", BQ25600 },
1778 	{ "bq25600d", BQ25600D },
1779 	{ "bq25601", BQ25601 },
1780 	{ "bq25601d", BQ25601D },
1781 	{ "bq25611d", BQ25611D },
1782 	{ "bq25618", BQ25618 },
1783 	{ "bq25619", BQ25619 },
1784 	{},
1785 };
1786 MODULE_DEVICE_TABLE(i2c, bq256xx_i2c_ids);
1787 
1788 static const struct of_device_id bq256xx_of_match[] = {
1789 	{ .compatible = "ti,bq25600", .data = (void *)BQ25600 },
1790 	{ .compatible = "ti,bq25600d", .data = (void *)BQ25600D },
1791 	{ .compatible = "ti,bq25601", .data = (void *)BQ25601 },
1792 	{ .compatible = "ti,bq25601d", .data = (void *)BQ25601D },
1793 	{ .compatible = "ti,bq25611d", .data = (void *)BQ25611D },
1794 	{ .compatible = "ti,bq25618", .data = (void *)BQ25618 },
1795 	{ .compatible = "ti,bq25619", .data = (void *)BQ25619 },
1796 	{ },
1797 };
1798 MODULE_DEVICE_TABLE(of, bq256xx_of_match);
1799 
1800 static const struct acpi_device_id bq256xx_acpi_match[] = {
1801 	{ "bq25600", BQ25600 },
1802 	{ "bq25600d", BQ25600D },
1803 	{ "bq25601", BQ25601 },
1804 	{ "bq25601d", BQ25601D },
1805 	{ "bq25611d", BQ25611D },
1806 	{ "bq25618", BQ25618 },
1807 	{ "bq25619", BQ25619 },
1808 	{},
1809 };
1810 MODULE_DEVICE_TABLE(acpi, bq256xx_acpi_match);
1811 
1812 static struct i2c_driver bq256xx_driver = {
1813 	.driver = {
1814 		.name = "bq256xx-charger",
1815 		.of_match_table = bq256xx_of_match,
1816 		.acpi_match_table = bq256xx_acpi_match,
1817 	},
1818 	.probe = bq256xx_probe,
1819 	.id_table = bq256xx_i2c_ids,
1820 };
1821 module_i2c_driver(bq256xx_driver);
1822 
1823 MODULE_AUTHOR("Ricardo Rivera-Matos <r-rivera-matos@ti.com>");
1824 MODULE_DESCRIPTION("bq256xx charger driver");
1825 MODULE_LICENSE("GPL v2");
1826