xref: /openbmc/linux/drivers/power/supply/axp288_charger.c (revision 45cc842d5b75ba8f9a958f2dd12b95c6dd0452bd)
1 /*
2  * axp288_charger.c - X-power AXP288 PMIC Charger driver
3  *
4  * Copyright (C) 2016-2017 Hans de Goede <hdegoede@redhat.com>
5  * Copyright (C) 2014 Intel Corporation
6  * Author: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17 
18 #include <linux/acpi.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/regmap.h>
22 #include <linux/workqueue.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
25 #include <linux/usb/otg.h>
26 #include <linux/notifier.h>
27 #include <linux/power_supply.h>
28 #include <linux/property.h>
29 #include <linux/mfd/axp20x.h>
30 #include <linux/extcon.h>
31 
32 #define PS_STAT_VBUS_TRIGGER		(1 << 0)
33 #define PS_STAT_BAT_CHRG_DIR		(1 << 2)
34 #define PS_STAT_VBAT_ABOVE_VHOLD	(1 << 3)
35 #define PS_STAT_VBUS_VALID		(1 << 4)
36 #define PS_STAT_VBUS_PRESENT		(1 << 5)
37 
38 #define CHRG_STAT_BAT_SAFE_MODE		(1 << 3)
39 #define CHRG_STAT_BAT_VALID		(1 << 4)
40 #define CHRG_STAT_BAT_PRESENT		(1 << 5)
41 #define CHRG_STAT_CHARGING		(1 << 6)
42 #define CHRG_STAT_PMIC_OTP		(1 << 7)
43 
44 #define VBUS_ISPOUT_CUR_LIM_MASK	0x03
45 #define VBUS_ISPOUT_CUR_LIM_BIT_POS	0
46 #define VBUS_ISPOUT_CUR_LIM_900MA	0x0	/* 900mA */
47 #define VBUS_ISPOUT_CUR_LIM_1500MA	0x1	/* 1500mA */
48 #define VBUS_ISPOUT_CUR_LIM_2000MA	0x2	/* 2000mA */
49 #define VBUS_ISPOUT_CUR_NO_LIM		0x3	/* 2500mA */
50 #define VBUS_ISPOUT_VHOLD_SET_MASK	0x31
51 #define VBUS_ISPOUT_VHOLD_SET_BIT_POS	0x3
52 #define VBUS_ISPOUT_VHOLD_SET_OFFSET	4000	/* 4000mV */
53 #define VBUS_ISPOUT_VHOLD_SET_LSB_RES	100	/* 100mV */
54 #define VBUS_ISPOUT_VHOLD_SET_4300MV	0x3	/* 4300mV */
55 #define VBUS_ISPOUT_VBUS_PATH_DIS	(1 << 7)
56 
57 #define CHRG_CCCV_CC_MASK		0xf		/* 4 bits */
58 #define CHRG_CCCV_CC_BIT_POS		0
59 #define CHRG_CCCV_CC_OFFSET		200		/* 200mA */
60 #define CHRG_CCCV_CC_LSB_RES		200		/* 200mA */
61 #define CHRG_CCCV_ITERM_20P		(1 << 4)	/* 20% of CC */
62 #define CHRG_CCCV_CV_MASK		0x60		/* 2 bits */
63 #define CHRG_CCCV_CV_BIT_POS		5
64 #define CHRG_CCCV_CV_4100MV		0x0		/* 4.10V */
65 #define CHRG_CCCV_CV_4150MV		0x1		/* 4.15V */
66 #define CHRG_CCCV_CV_4200MV		0x2		/* 4.20V */
67 #define CHRG_CCCV_CV_4350MV		0x3		/* 4.35V */
68 #define CHRG_CCCV_CHG_EN		(1 << 7)
69 
70 #define CNTL2_CC_TIMEOUT_MASK		0x3	/* 2 bits */
71 #define CNTL2_CC_TIMEOUT_OFFSET		6	/* 6 Hrs */
72 #define CNTL2_CC_TIMEOUT_LSB_RES	2	/* 2 Hrs */
73 #define CNTL2_CC_TIMEOUT_12HRS		0x3	/* 12 Hrs */
74 #define CNTL2_CHGLED_TYPEB		(1 << 4)
75 #define CNTL2_CHG_OUT_TURNON		(1 << 5)
76 #define CNTL2_PC_TIMEOUT_MASK		0xC0
77 #define CNTL2_PC_TIMEOUT_OFFSET		40	/* 40 mins */
78 #define CNTL2_PC_TIMEOUT_LSB_RES	10	/* 10 mins */
79 #define CNTL2_PC_TIMEOUT_70MINS		0x3
80 
81 #define CHRG_ILIM_TEMP_LOOP_EN		(1 << 3)
82 #define CHRG_VBUS_ILIM_MASK		0xf0
83 #define CHRG_VBUS_ILIM_BIT_POS		4
84 #define CHRG_VBUS_ILIM_100MA		0x0	/* 100mA */
85 #define CHRG_VBUS_ILIM_500MA		0x1	/* 500mA */
86 #define CHRG_VBUS_ILIM_900MA		0x2	/* 900mA */
87 #define CHRG_VBUS_ILIM_1500MA		0x3	/* 1500mA */
88 #define CHRG_VBUS_ILIM_2000MA		0x4	/* 2000mA */
89 #define CHRG_VBUS_ILIM_2500MA		0x5	/* 2500mA */
90 #define CHRG_VBUS_ILIM_3000MA		0x6	/* 3000mA */
91 
92 #define CHRG_VLTFC_0C			0xA5	/* 0 DegC */
93 #define CHRG_VHTFC_45C			0x1F	/* 45 DegC */
94 
95 #define FG_CNTL_OCV_ADJ_EN		(1 << 3)
96 
97 #define CV_4100MV			4100	/* 4100mV */
98 #define CV_4150MV			4150	/* 4150mV */
99 #define CV_4200MV			4200	/* 4200mV */
100 #define CV_4350MV			4350	/* 4350mV */
101 
102 #define AXP288_EXTCON_DEV_NAME		"axp288_extcon"
103 #define USB_HOST_EXTCON_HID		"INT3496"
104 #define USB_HOST_EXTCON_NAME		"INT3496:00"
105 
106 enum {
107 	VBUS_OV_IRQ = 0,
108 	CHARGE_DONE_IRQ,
109 	CHARGE_CHARGING_IRQ,
110 	BAT_SAFE_QUIT_IRQ,
111 	BAT_SAFE_ENTER_IRQ,
112 	QCBTU_IRQ,
113 	CBTU_IRQ,
114 	QCBTO_IRQ,
115 	CBTO_IRQ,
116 	CHRG_INTR_END,
117 };
118 
119 struct axp288_chrg_info {
120 	struct platform_device *pdev;
121 	struct regmap *regmap;
122 	struct regmap_irq_chip_data *regmap_irqc;
123 	int irq[CHRG_INTR_END];
124 	struct power_supply *psy_usb;
125 
126 	/* OTG/Host mode */
127 	struct {
128 		struct work_struct work;
129 		struct extcon_dev *cable;
130 		struct notifier_block id_nb;
131 		bool id_short;
132 	} otg;
133 
134 	/* SDP/CDP/DCP USB charging cable notifications */
135 	struct {
136 		struct extcon_dev *edev;
137 		struct notifier_block nb;
138 		struct work_struct work;
139 	} cable;
140 
141 	int cc;
142 	int cv;
143 	int max_cc;
144 	int max_cv;
145 };
146 
147 static inline int axp288_charger_set_cc(struct axp288_chrg_info *info, int cc)
148 {
149 	u8 reg_val;
150 	int ret;
151 
152 	if (cc < CHRG_CCCV_CC_OFFSET)
153 		cc = CHRG_CCCV_CC_OFFSET;
154 	else if (cc > info->max_cc)
155 		cc = info->max_cc;
156 
157 	reg_val = (cc - CHRG_CCCV_CC_OFFSET) / CHRG_CCCV_CC_LSB_RES;
158 	cc = (reg_val * CHRG_CCCV_CC_LSB_RES) + CHRG_CCCV_CC_OFFSET;
159 	reg_val = reg_val << CHRG_CCCV_CC_BIT_POS;
160 
161 	ret = regmap_update_bits(info->regmap,
162 				AXP20X_CHRG_CTRL1,
163 				CHRG_CCCV_CC_MASK, reg_val);
164 	if (ret >= 0)
165 		info->cc = cc;
166 
167 	return ret;
168 }
169 
170 static inline int axp288_charger_set_cv(struct axp288_chrg_info *info, int cv)
171 {
172 	u8 reg_val;
173 	int ret;
174 
175 	if (cv <= CV_4100MV) {
176 		reg_val = CHRG_CCCV_CV_4100MV;
177 		cv = CV_4100MV;
178 	} else if (cv <= CV_4150MV) {
179 		reg_val = CHRG_CCCV_CV_4150MV;
180 		cv = CV_4150MV;
181 	} else if (cv <= CV_4200MV) {
182 		reg_val = CHRG_CCCV_CV_4200MV;
183 		cv = CV_4200MV;
184 	} else {
185 		reg_val = CHRG_CCCV_CV_4350MV;
186 		cv = CV_4350MV;
187 	}
188 
189 	reg_val = reg_val << CHRG_CCCV_CV_BIT_POS;
190 
191 	ret = regmap_update_bits(info->regmap,
192 				AXP20X_CHRG_CTRL1,
193 				CHRG_CCCV_CV_MASK, reg_val);
194 
195 	if (ret >= 0)
196 		info->cv = cv;
197 
198 	return ret;
199 }
200 
201 static int axp288_charger_get_vbus_inlmt(struct axp288_chrg_info *info)
202 {
203 	unsigned int val;
204 	int ret;
205 
206 	ret = regmap_read(info->regmap, AXP20X_CHRG_BAK_CTRL, &val);
207 	if (ret < 0)
208 		return ret;
209 
210 	val >>= CHRG_VBUS_ILIM_BIT_POS;
211 	switch (val) {
212 	case CHRG_VBUS_ILIM_100MA:
213 		return 100000;
214 	case CHRG_VBUS_ILIM_500MA:
215 		return 500000;
216 	case CHRG_VBUS_ILIM_900MA:
217 		return 900000;
218 	case CHRG_VBUS_ILIM_1500MA:
219 		return 1500000;
220 	case CHRG_VBUS_ILIM_2000MA:
221 		return 2000000;
222 	case CHRG_VBUS_ILIM_2500MA:
223 		return 2500000;
224 	case CHRG_VBUS_ILIM_3000MA:
225 		return 3000000;
226 	default:
227 		dev_warn(&info->pdev->dev, "Unknown ilim reg val: %d\n", val);
228 		return 0;
229 	}
230 }
231 
232 static inline int axp288_charger_set_vbus_inlmt(struct axp288_chrg_info *info,
233 					   int inlmt)
234 {
235 	int ret;
236 	u8 reg_val;
237 
238 	if (inlmt >= 3000000)
239 		reg_val = CHRG_VBUS_ILIM_3000MA << CHRG_VBUS_ILIM_BIT_POS;
240 	else if (inlmt >= 2500000)
241 		reg_val = CHRG_VBUS_ILIM_2500MA << CHRG_VBUS_ILIM_BIT_POS;
242 	else if (inlmt >= 2000000)
243 		reg_val = CHRG_VBUS_ILIM_2000MA << CHRG_VBUS_ILIM_BIT_POS;
244 	else if (inlmt >= 1500000)
245 		reg_val = CHRG_VBUS_ILIM_1500MA << CHRG_VBUS_ILIM_BIT_POS;
246 	else if (inlmt >= 900000)
247 		reg_val = CHRG_VBUS_ILIM_900MA << CHRG_VBUS_ILIM_BIT_POS;
248 	else if (inlmt >= 500000)
249 		reg_val = CHRG_VBUS_ILIM_500MA << CHRG_VBUS_ILIM_BIT_POS;
250 	else
251 		reg_val = CHRG_VBUS_ILIM_100MA << CHRG_VBUS_ILIM_BIT_POS;
252 
253 	ret = regmap_update_bits(info->regmap, AXP20X_CHRG_BAK_CTRL,
254 				 CHRG_VBUS_ILIM_MASK, reg_val);
255 	if (ret < 0)
256 		dev_err(&info->pdev->dev, "charger BAK control %d\n", ret);
257 
258 	return ret;
259 }
260 
261 static int axp288_charger_vbus_path_select(struct axp288_chrg_info *info,
262 								bool enable)
263 {
264 	int ret;
265 
266 	if (enable)
267 		ret = regmap_update_bits(info->regmap, AXP20X_VBUS_IPSOUT_MGMT,
268 					VBUS_ISPOUT_VBUS_PATH_DIS, 0);
269 	else
270 		ret = regmap_update_bits(info->regmap, AXP20X_VBUS_IPSOUT_MGMT,
271 			VBUS_ISPOUT_VBUS_PATH_DIS, VBUS_ISPOUT_VBUS_PATH_DIS);
272 
273 	if (ret < 0)
274 		dev_err(&info->pdev->dev, "axp288 vbus path select %d\n", ret);
275 
276 	return ret;
277 }
278 
279 static int axp288_charger_enable_charger(struct axp288_chrg_info *info,
280 								bool enable)
281 {
282 	int ret;
283 
284 	if (enable)
285 		ret = regmap_update_bits(info->regmap, AXP20X_CHRG_CTRL1,
286 				CHRG_CCCV_CHG_EN, CHRG_CCCV_CHG_EN);
287 	else
288 		ret = regmap_update_bits(info->regmap, AXP20X_CHRG_CTRL1,
289 				CHRG_CCCV_CHG_EN, 0);
290 	if (ret < 0)
291 		dev_err(&info->pdev->dev, "axp288 enable charger %d\n", ret);
292 
293 	return ret;
294 }
295 
296 static int axp288_charger_is_present(struct axp288_chrg_info *info)
297 {
298 	int ret, present = 0;
299 	unsigned int val;
300 
301 	ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
302 	if (ret < 0)
303 		return ret;
304 
305 	if (val & PS_STAT_VBUS_PRESENT)
306 		present = 1;
307 	return present;
308 }
309 
310 static int axp288_charger_is_online(struct axp288_chrg_info *info)
311 {
312 	int ret, online = 0;
313 	unsigned int val;
314 
315 	ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
316 	if (ret < 0)
317 		return ret;
318 
319 	if (val & PS_STAT_VBUS_VALID)
320 		online = 1;
321 	return online;
322 }
323 
324 static int axp288_get_charger_health(struct axp288_chrg_info *info)
325 {
326 	int ret, pwr_stat, chrg_stat;
327 	int health = POWER_SUPPLY_HEALTH_UNKNOWN;
328 	unsigned int val;
329 
330 	ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
331 	if ((ret < 0) || !(val & PS_STAT_VBUS_PRESENT))
332 		goto health_read_fail;
333 	else
334 		pwr_stat = val;
335 
336 	ret = regmap_read(info->regmap, AXP20X_PWR_OP_MODE, &val);
337 	if (ret < 0)
338 		goto health_read_fail;
339 	else
340 		chrg_stat = val;
341 
342 	if (!(pwr_stat & PS_STAT_VBUS_VALID))
343 		health = POWER_SUPPLY_HEALTH_DEAD;
344 	else if (chrg_stat & CHRG_STAT_PMIC_OTP)
345 		health = POWER_SUPPLY_HEALTH_OVERHEAT;
346 	else if (chrg_stat & CHRG_STAT_BAT_SAFE_MODE)
347 		health = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
348 	else
349 		health = POWER_SUPPLY_HEALTH_GOOD;
350 
351 health_read_fail:
352 	return health;
353 }
354 
355 static int axp288_charger_usb_set_property(struct power_supply *psy,
356 				    enum power_supply_property psp,
357 				    const union power_supply_propval *val)
358 {
359 	struct axp288_chrg_info *info = power_supply_get_drvdata(psy);
360 	int ret = 0;
361 	int scaled_val;
362 
363 	switch (psp) {
364 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
365 		scaled_val = min(val->intval, info->max_cc);
366 		scaled_val = DIV_ROUND_CLOSEST(scaled_val, 1000);
367 		ret = axp288_charger_set_cc(info, scaled_val);
368 		if (ret < 0)
369 			dev_warn(&info->pdev->dev, "set charge current failed\n");
370 		break;
371 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
372 		scaled_val = min(val->intval, info->max_cv);
373 		scaled_val = DIV_ROUND_CLOSEST(scaled_val, 1000);
374 		ret = axp288_charger_set_cv(info, scaled_val);
375 		if (ret < 0)
376 			dev_warn(&info->pdev->dev, "set charge voltage failed\n");
377 		break;
378 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
379 		ret = axp288_charger_set_vbus_inlmt(info, val->intval);
380 		if (ret < 0)
381 			dev_warn(&info->pdev->dev, "set input current limit failed\n");
382 		break;
383 	default:
384 		ret = -EINVAL;
385 	}
386 
387 	return ret;
388 }
389 
390 static int axp288_charger_usb_get_property(struct power_supply *psy,
391 				    enum power_supply_property psp,
392 				    union power_supply_propval *val)
393 {
394 	struct axp288_chrg_info *info = power_supply_get_drvdata(psy);
395 	int ret;
396 
397 	switch (psp) {
398 	case POWER_SUPPLY_PROP_PRESENT:
399 		/* Check for OTG case first */
400 		if (info->otg.id_short) {
401 			val->intval = 0;
402 			break;
403 		}
404 		ret = axp288_charger_is_present(info);
405 		if (ret < 0)
406 			return ret;
407 		val->intval = ret;
408 		break;
409 	case POWER_SUPPLY_PROP_ONLINE:
410 		/* Check for OTG case first */
411 		if (info->otg.id_short) {
412 			val->intval = 0;
413 			break;
414 		}
415 		ret = axp288_charger_is_online(info);
416 		if (ret < 0)
417 			return ret;
418 		val->intval = ret;
419 		break;
420 	case POWER_SUPPLY_PROP_HEALTH:
421 		val->intval = axp288_get_charger_health(info);
422 		break;
423 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
424 		val->intval = info->cc * 1000;
425 		break;
426 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
427 		val->intval = info->max_cc * 1000;
428 		break;
429 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
430 		val->intval = info->cv * 1000;
431 		break;
432 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
433 		val->intval = info->max_cv * 1000;
434 		break;
435 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
436 		ret = axp288_charger_get_vbus_inlmt(info);
437 		if (ret < 0)
438 			return ret;
439 		val->intval = ret;
440 		break;
441 	default:
442 		return -EINVAL;
443 	}
444 
445 	return 0;
446 }
447 
448 static int axp288_charger_property_is_writeable(struct power_supply *psy,
449 		enum power_supply_property psp)
450 {
451 	int ret;
452 
453 	switch (psp) {
454 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
455 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
456 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
457 		ret = 1;
458 		break;
459 	default:
460 		ret = 0;
461 	}
462 
463 	return ret;
464 }
465 
466 static enum power_supply_property axp288_usb_props[] = {
467 	POWER_SUPPLY_PROP_PRESENT,
468 	POWER_SUPPLY_PROP_ONLINE,
469 	POWER_SUPPLY_PROP_TYPE,
470 	POWER_SUPPLY_PROP_HEALTH,
471 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
472 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
473 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
474 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
475 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
476 };
477 
478 static const struct power_supply_desc axp288_charger_desc = {
479 	.name			= "axp288_charger",
480 	.type			= POWER_SUPPLY_TYPE_USB,
481 	.properties		= axp288_usb_props,
482 	.num_properties		= ARRAY_SIZE(axp288_usb_props),
483 	.get_property		= axp288_charger_usb_get_property,
484 	.set_property		= axp288_charger_usb_set_property,
485 	.property_is_writeable	= axp288_charger_property_is_writeable,
486 };
487 
488 static irqreturn_t axp288_charger_irq_thread_handler(int irq, void *dev)
489 {
490 	struct axp288_chrg_info *info = dev;
491 	int i;
492 
493 	for (i = 0; i < CHRG_INTR_END; i++) {
494 		if (info->irq[i] == irq)
495 			break;
496 	}
497 
498 	if (i >= CHRG_INTR_END) {
499 		dev_warn(&info->pdev->dev, "spurious interrupt!!\n");
500 		return IRQ_NONE;
501 	}
502 
503 	switch (i) {
504 	case VBUS_OV_IRQ:
505 		dev_dbg(&info->pdev->dev, "VBUS Over Voltage INTR\n");
506 		break;
507 	case CHARGE_DONE_IRQ:
508 		dev_dbg(&info->pdev->dev, "Charging Done INTR\n");
509 		break;
510 	case CHARGE_CHARGING_IRQ:
511 		dev_dbg(&info->pdev->dev, "Start Charging IRQ\n");
512 		break;
513 	case BAT_SAFE_QUIT_IRQ:
514 		dev_dbg(&info->pdev->dev,
515 			"Quit Safe Mode(restart timer) Charging IRQ\n");
516 		break;
517 	case BAT_SAFE_ENTER_IRQ:
518 		dev_dbg(&info->pdev->dev,
519 			"Enter Safe Mode(timer expire) Charging IRQ\n");
520 		break;
521 	case QCBTU_IRQ:
522 		dev_dbg(&info->pdev->dev,
523 			"Quit Battery Under Temperature(CHRG) INTR\n");
524 		break;
525 	case CBTU_IRQ:
526 		dev_dbg(&info->pdev->dev,
527 			"Hit Battery Under Temperature(CHRG) INTR\n");
528 		break;
529 	case QCBTO_IRQ:
530 		dev_dbg(&info->pdev->dev,
531 			"Quit Battery Over Temperature(CHRG) INTR\n");
532 		break;
533 	case CBTO_IRQ:
534 		dev_dbg(&info->pdev->dev,
535 			"Hit Battery Over Temperature(CHRG) INTR\n");
536 		break;
537 	default:
538 		dev_warn(&info->pdev->dev, "Spurious Interrupt!!!\n");
539 		goto out;
540 	}
541 
542 	power_supply_changed(info->psy_usb);
543 out:
544 	return IRQ_HANDLED;
545 }
546 
547 static void axp288_charger_extcon_evt_worker(struct work_struct *work)
548 {
549 	struct axp288_chrg_info *info =
550 	    container_of(work, struct axp288_chrg_info, cable.work);
551 	int ret, current_limit;
552 	struct extcon_dev *edev = info->cable.edev;
553 	unsigned int val;
554 
555 	ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
556 	if (ret < 0) {
557 		dev_err(&info->pdev->dev, "Error reading status (%d)\n", ret);
558 		return;
559 	}
560 
561 	/* Offline? Disable charging and bail */
562 	if (!(val & PS_STAT_VBUS_VALID)) {
563 		dev_dbg(&info->pdev->dev, "USB charger disconnected\n");
564 		axp288_charger_enable_charger(info, false);
565 		power_supply_changed(info->psy_usb);
566 		return;
567 	}
568 
569 	/* Determine cable/charger type */
570 	if (extcon_get_state(edev, EXTCON_CHG_USB_SDP) > 0) {
571 		dev_dbg(&info->pdev->dev, "USB SDP charger is connected\n");
572 		current_limit = 500000;
573 	} else if (extcon_get_state(edev, EXTCON_CHG_USB_CDP) > 0) {
574 		dev_dbg(&info->pdev->dev, "USB CDP charger is connected\n");
575 		current_limit = 1500000;
576 	} else if (extcon_get_state(edev, EXTCON_CHG_USB_DCP) > 0) {
577 		dev_dbg(&info->pdev->dev, "USB DCP charger is connected\n");
578 		current_limit = 2000000;
579 	} else {
580 		/* Charger type detection still in progress, bail. */
581 		return;
582 	}
583 
584 	/* Set vbus current limit first, then enable charger */
585 	ret = axp288_charger_set_vbus_inlmt(info, current_limit);
586 	if (ret == 0)
587 		axp288_charger_enable_charger(info, true);
588 	else
589 		dev_err(&info->pdev->dev,
590 			"error setting current limit (%d)\n", ret);
591 
592 	power_supply_changed(info->psy_usb);
593 }
594 
595 static int axp288_charger_handle_cable_evt(struct notifier_block *nb,
596 					   unsigned long event, void *param)
597 {
598 	struct axp288_chrg_info *info =
599 		container_of(nb, struct axp288_chrg_info, cable.nb);
600 	schedule_work(&info->cable.work);
601 	return NOTIFY_OK;
602 }
603 
604 static void axp288_charger_otg_evt_worker(struct work_struct *work)
605 {
606 	struct axp288_chrg_info *info =
607 	    container_of(work, struct axp288_chrg_info, otg.work);
608 	struct extcon_dev *edev = info->otg.cable;
609 	int ret, usb_host = extcon_get_state(edev, EXTCON_USB_HOST);
610 
611 	dev_dbg(&info->pdev->dev, "external connector USB-Host is %s\n",
612 				usb_host ? "attached" : "detached");
613 
614 	/*
615 	 * Set usb_id_short flag to avoid running charger detection logic
616 	 * in case usb host.
617 	 */
618 	info->otg.id_short = usb_host;
619 
620 	/* Disable VBUS path before enabling the 5V boost */
621 	ret = axp288_charger_vbus_path_select(info, !info->otg.id_short);
622 	if (ret < 0)
623 		dev_warn(&info->pdev->dev, "vbus path disable failed\n");
624 }
625 
626 static int axp288_charger_handle_otg_evt(struct notifier_block *nb,
627 				   unsigned long event, void *param)
628 {
629 	struct axp288_chrg_info *info =
630 	    container_of(nb, struct axp288_chrg_info, otg.id_nb);
631 
632 	schedule_work(&info->otg.work);
633 
634 	return NOTIFY_OK;
635 }
636 
637 static int charger_init_hw_regs(struct axp288_chrg_info *info)
638 {
639 	int ret, cc, cv;
640 	unsigned int val;
641 
642 	/* Program temperature thresholds */
643 	ret = regmap_write(info->regmap, AXP20X_V_LTF_CHRG, CHRG_VLTFC_0C);
644 	if (ret < 0) {
645 		dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
646 							AXP20X_V_LTF_CHRG, ret);
647 		return ret;
648 	}
649 
650 	ret = regmap_write(info->regmap, AXP20X_V_HTF_CHRG, CHRG_VHTFC_45C);
651 	if (ret < 0) {
652 		dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
653 							AXP20X_V_HTF_CHRG, ret);
654 		return ret;
655 	}
656 
657 	/* Do not turn-off charger o/p after charge cycle ends */
658 	ret = regmap_update_bits(info->regmap,
659 				AXP20X_CHRG_CTRL2,
660 				CNTL2_CHG_OUT_TURNON, CNTL2_CHG_OUT_TURNON);
661 	if (ret < 0) {
662 		dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
663 						AXP20X_CHRG_CTRL2, ret);
664 		return ret;
665 	}
666 
667 	/* Setup ending condition for charging to be 10% of I(chrg) */
668 	ret = regmap_update_bits(info->regmap,
669 				AXP20X_CHRG_CTRL1,
670 				CHRG_CCCV_ITERM_20P, 0);
671 	if (ret < 0) {
672 		dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
673 						AXP20X_CHRG_CTRL1, ret);
674 		return ret;
675 	}
676 
677 	/* Disable OCV-SOC curve calibration */
678 	ret = regmap_update_bits(info->regmap,
679 				AXP20X_CC_CTRL,
680 				FG_CNTL_OCV_ADJ_EN, 0);
681 	if (ret < 0) {
682 		dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
683 						AXP20X_CC_CTRL, ret);
684 		return ret;
685 	}
686 
687 	/* Read current charge voltage and current limit */
688 	ret = regmap_read(info->regmap, AXP20X_CHRG_CTRL1, &val);
689 	if (ret < 0) {
690 		dev_err(&info->pdev->dev, "register(%x) read error(%d)\n",
691 			AXP20X_CHRG_CTRL1, ret);
692 		return ret;
693 	}
694 
695 	/* Determine charge voltage */
696 	cv = (val & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS;
697 	switch (cv) {
698 	case CHRG_CCCV_CV_4100MV:
699 		info->cv = CV_4100MV;
700 		break;
701 	case CHRG_CCCV_CV_4150MV:
702 		info->cv = CV_4150MV;
703 		break;
704 	case CHRG_CCCV_CV_4200MV:
705 		info->cv = CV_4200MV;
706 		break;
707 	case CHRG_CCCV_CV_4350MV:
708 		info->cv = CV_4350MV;
709 		break;
710 	}
711 
712 	/* Determine charge current limit */
713 	cc = (ret & CHRG_CCCV_CC_MASK) >> CHRG_CCCV_CC_BIT_POS;
714 	cc = (cc * CHRG_CCCV_CC_LSB_RES) + CHRG_CCCV_CC_OFFSET;
715 	info->cc = cc;
716 
717 	/*
718 	 * Do not allow the user to configure higher settings then those
719 	 * set by the firmware
720 	 */
721 	info->max_cv = info->cv;
722 	info->max_cc = info->cc;
723 
724 	return 0;
725 }
726 
727 static void axp288_charger_cancel_work(void *data)
728 {
729 	struct axp288_chrg_info *info = data;
730 
731 	cancel_work_sync(&info->otg.work);
732 	cancel_work_sync(&info->cable.work);
733 }
734 
735 static int axp288_charger_probe(struct platform_device *pdev)
736 {
737 	int ret, i, pirq;
738 	struct axp288_chrg_info *info;
739 	struct device *dev = &pdev->dev;
740 	struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
741 	struct power_supply_config charger_cfg = {};
742 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
743 	if (!info)
744 		return -ENOMEM;
745 
746 	info->pdev = pdev;
747 	info->regmap = axp20x->regmap;
748 	info->regmap_irqc = axp20x->regmap_irqc;
749 
750 	info->cable.edev = extcon_get_extcon_dev(AXP288_EXTCON_DEV_NAME);
751 	if (info->cable.edev == NULL) {
752 		dev_dbg(&pdev->dev, "%s is not ready, probe deferred\n",
753 			AXP288_EXTCON_DEV_NAME);
754 		return -EPROBE_DEFER;
755 	}
756 
757 	if (acpi_dev_present(USB_HOST_EXTCON_HID, NULL, -1)) {
758 		info->otg.cable = extcon_get_extcon_dev(USB_HOST_EXTCON_NAME);
759 		if (info->otg.cable == NULL) {
760 			dev_dbg(dev, "EXTCON_USB_HOST is not ready, probe deferred\n");
761 			return -EPROBE_DEFER;
762 		}
763 		dev_info(&pdev->dev,
764 			 "Using " USB_HOST_EXTCON_HID " extcon for usb-id\n");
765 	}
766 
767 	platform_set_drvdata(pdev, info);
768 
769 	ret = charger_init_hw_regs(info);
770 	if (ret)
771 		return ret;
772 
773 	/* Register with power supply class */
774 	charger_cfg.drv_data = info;
775 	info->psy_usb = devm_power_supply_register(dev, &axp288_charger_desc,
776 						   &charger_cfg);
777 	if (IS_ERR(info->psy_usb)) {
778 		ret = PTR_ERR(info->psy_usb);
779 		dev_err(dev, "failed to register power supply: %d\n", ret);
780 		return ret;
781 	}
782 
783 	/* Cancel our work on cleanup, register this before the notifiers */
784 	ret = devm_add_action(dev, axp288_charger_cancel_work, info);
785 	if (ret)
786 		return ret;
787 
788 	/* Register for extcon notification */
789 	INIT_WORK(&info->cable.work, axp288_charger_extcon_evt_worker);
790 	info->cable.nb.notifier_call = axp288_charger_handle_cable_evt;
791 	ret = devm_extcon_register_notifier_all(dev, info->cable.edev,
792 						&info->cable.nb);
793 	if (ret) {
794 		dev_err(dev, "failed to register cable extcon notifier\n");
795 		return ret;
796 	}
797 	schedule_work(&info->cable.work);
798 
799 	/* Register for OTG notification */
800 	INIT_WORK(&info->otg.work, axp288_charger_otg_evt_worker);
801 	info->otg.id_nb.notifier_call = axp288_charger_handle_otg_evt;
802 	if (info->otg.cable) {
803 		ret = devm_extcon_register_notifier(&pdev->dev, info->otg.cable,
804 					EXTCON_USB_HOST, &info->otg.id_nb);
805 		if (ret) {
806 			dev_err(dev, "failed to register EXTCON_USB_HOST notifier\n");
807 			return ret;
808 		}
809 		schedule_work(&info->otg.work);
810 	}
811 
812 	/* Register charger interrupts */
813 	for (i = 0; i < CHRG_INTR_END; i++) {
814 		pirq = platform_get_irq(info->pdev, i);
815 		info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq);
816 		if (info->irq[i] < 0) {
817 			dev_warn(&info->pdev->dev,
818 				"failed to get virtual interrupt=%d\n", pirq);
819 			return info->irq[i];
820 		}
821 		ret = devm_request_threaded_irq(&info->pdev->dev, info->irq[i],
822 					NULL, axp288_charger_irq_thread_handler,
823 					IRQF_ONESHOT, info->pdev->name, info);
824 		if (ret) {
825 			dev_err(&pdev->dev, "failed to request interrupt=%d\n",
826 								info->irq[i]);
827 			return ret;
828 		}
829 	}
830 
831 	return 0;
832 }
833 
834 static const struct platform_device_id axp288_charger_id_table[] = {
835 	{ .name = "axp288_charger" },
836 	{},
837 };
838 MODULE_DEVICE_TABLE(platform, axp288_charger_id_table);
839 
840 static struct platform_driver axp288_charger_driver = {
841 	.probe = axp288_charger_probe,
842 	.id_table = axp288_charger_id_table,
843 	.driver = {
844 		.name = "axp288_charger",
845 	},
846 };
847 
848 module_platform_driver(axp288_charger_driver);
849 
850 MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
851 MODULE_DESCRIPTION("X-power AXP288 Charger Driver");
852 MODULE_LICENSE("GPL v2");
853