Debian Codegraph @

drivers/power/supply/axp288_charger.c

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