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