2 * axp288_charger.c - X-power AXP288 PMIC Charger driver
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>
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.
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.
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>
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)
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)
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)
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)
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
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 */
95 #define CHRG_VLTFC_0C 0xA5 /* 0 DegC */
96 #define CHRG_VHTFC_45C 0x1F /* 45 DegC */
98 #define FG_CNTL_OCV_ADJ_EN (1 << 3)
100 #define CV_4100MV 4100 /* 4100mV */
101 #define CV_4150MV 4150 /* 4150mV */
102 #define CV_4200MV 4200 /* 4200mV */
103 #define CV_4350MV 4350 /* 4350mV */
105 #define AXP288_EXTCON_DEV_NAME "axp288_extcon"
106 #define USB_HOST_EXTCON_HID "INT3496"
107 #define USB_HOST_EXTCON_NAME "INT3496:00"
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;
131 struct work_struct work;
132 struct extcon_dev *cable;
133 struct notifier_block id_nb;
137 /* SDP/CDP/DCP USB charging cable notifications */
139 struct extcon_dev *edev;
140 struct notifier_block nb;
141 struct work_struct work;
150 static inline int axp288_charger_set_cc(struct axp288_chrg_info *info, int cc)
155 if (cc < CHRG_CCCV_CC_OFFSET)
156 cc = CHRG_CCCV_CC_OFFSET;
157 else if (cc > info->max_cc)
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;
164 ret = regmap_update_bits(info->regmap,
166 CHRG_CCCV_CC_MASK, reg_val);
173 static inline int axp288_charger_set_cv(struct axp288_chrg_info *info, int cv)
178 if (cv <= CV_4100MV) {
179 reg_val = CHRG_CCCV_CV_4100MV;
181 } else if (cv <= CV_4150MV) {
182 reg_val = CHRG_CCCV_CV_4150MV;
184 } else if (cv <= CV_4200MV) {
185 reg_val = CHRG_CCCV_CV_4200MV;
188 reg_val = CHRG_CCCV_CV_4350MV;
192 reg_val = reg_val << CHRG_CCCV_CV_BIT_POS;
194 ret = regmap_update_bits(info->regmap,
196 CHRG_CCCV_CV_MASK, reg_val);
204 static int axp288_charger_get_vbus_inlmt(struct axp288_chrg_info *info)
209 ret = regmap_read(info->regmap, AXP20X_CHRG_BAK_CTRL, &val);
213 val >>= CHRG_VBUS_ILIM_BIT_POS;
215 case CHRG_VBUS_ILIM_100MA:
217 case CHRG_VBUS_ILIM_500MA:
219 case CHRG_VBUS_ILIM_900MA:
221 case CHRG_VBUS_ILIM_1500MA:
223 case CHRG_VBUS_ILIM_2000MA:
225 case CHRG_VBUS_ILIM_2500MA:
227 case CHRG_VBUS_ILIM_3000MA:
229 case CHRG_VBUS_ILIM_3500MA:
232 /* All b1xxx values map to 4000 mA */
237 static inline int axp288_charger_set_vbus_inlmt(struct axp288_chrg_info *info,
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;
260 reg_val = CHRG_VBUS_ILIM_100MA << CHRG_VBUS_ILIM_BIT_POS;
262 ret = regmap_update_bits(info->regmap, AXP20X_CHRG_BAK_CTRL,
263 CHRG_VBUS_ILIM_MASK, reg_val);
265 dev_err(&info->pdev->dev, "charger BAK control %d\n", ret);
270 static int axp288_charger_vbus_path_select(struct axp288_chrg_info *info,
276 ret = regmap_update_bits(info->regmap, AXP20X_VBUS_IPSOUT_MGMT,
277 VBUS_ISPOUT_VBUS_PATH_DIS, 0);
279 ret = regmap_update_bits(info->regmap, AXP20X_VBUS_IPSOUT_MGMT,
280 VBUS_ISPOUT_VBUS_PATH_DIS, VBUS_ISPOUT_VBUS_PATH_DIS);
283 dev_err(&info->pdev->dev, "axp288 vbus path select %d\n", ret);
288 static int axp288_charger_enable_charger(struct axp288_chrg_info *info,
294 ret = regmap_update_bits(info->regmap, AXP20X_CHRG_CTRL1,
295 CHRG_CCCV_CHG_EN, CHRG_CCCV_CHG_EN);
297 ret = regmap_update_bits(info->regmap, AXP20X_CHRG_CTRL1,
298 CHRG_CCCV_CHG_EN, 0);
300 dev_err(&info->pdev->dev, "axp288 enable charger %d\n", ret);
305 static int axp288_charger_is_present(struct axp288_chrg_info *info)
307 int ret, present = 0;
310 ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
314 if (val & PS_STAT_VBUS_PRESENT)
319 static int axp288_charger_is_online(struct axp288_chrg_info *info)
324 ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
328 if (val & PS_STAT_VBUS_VALID)
333 static int axp288_get_charger_health(struct axp288_chrg_info *info)
335 int ret, pwr_stat, chrg_stat;
336 int health = POWER_SUPPLY_HEALTH_UNKNOWN;
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;
345 ret = regmap_read(info->regmap, AXP20X_PWR_OP_MODE, &val);
347 goto health_read_fail;
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;
358 health = POWER_SUPPLY_HEALTH_GOOD;
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)
368 struct axp288_chrg_info *info = power_supply_get_drvdata(psy);
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);
378 dev_warn(&info->pdev->dev, "set charge current failed\n");
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);
385 dev_warn(&info->pdev->dev, "set charge voltage failed\n");
387 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
388 ret = axp288_charger_set_vbus_inlmt(info, val->intval);
390 dev_warn(&info->pdev->dev, "set input current limit failed\n");
399 static int axp288_charger_usb_get_property(struct power_supply *psy,
400 enum power_supply_property psp,
401 union power_supply_propval *val)
403 struct axp288_chrg_info *info = power_supply_get_drvdata(psy);
407 case POWER_SUPPLY_PROP_PRESENT:
408 /* Check for OTG case first */
409 if (info->otg.id_short) {
413 ret = axp288_charger_is_present(info);
418 case POWER_SUPPLY_PROP_ONLINE:
419 /* Check for OTG case first */
420 if (info->otg.id_short) {
424 ret = axp288_charger_is_online(info);
429 case POWER_SUPPLY_PROP_HEALTH:
430 val->intval = axp288_get_charger_health(info);
432 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
433 val->intval = info->cc * 1000;
435 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
436 val->intval = info->max_cc * 1000;
438 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
439 val->intval = info->cv * 1000;
441 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
442 val->intval = info->max_cv * 1000;
444 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
445 ret = axp288_charger_get_vbus_inlmt(info);
457 static int axp288_charger_property_is_writeable(struct power_supply *psy,
458 enum power_supply_property 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:
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,
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,
497 static irqreturn_t axp288_charger_irq_thread_handler(int irq, void *dev)
499 struct axp288_chrg_info *info = dev;
502 for (i = 0; i < CHRG_INTR_END; i++) {
503 if (info->irq[i] == irq)
507 if (i >= CHRG_INTR_END) {
508 dev_warn(&info->pdev->dev, "spurious interrupt!!\n");
514 dev_dbg(&info->pdev->dev, "VBUS Over Voltage INTR\n");
516 case CHARGE_DONE_IRQ:
517 dev_dbg(&info->pdev->dev, "Charging Done INTR\n");
519 case CHARGE_CHARGING_IRQ:
520 dev_dbg(&info->pdev->dev, "Start Charging IRQ\n");
522 case BAT_SAFE_QUIT_IRQ:
523 dev_dbg(&info->pdev->dev,
524 "Quit Safe Mode(restart timer) Charging IRQ\n");
526 case BAT_SAFE_ENTER_IRQ:
527 dev_dbg(&info->pdev->dev,
528 "Enter Safe Mode(timer expire) Charging IRQ\n");
531 dev_dbg(&info->pdev->dev,
532 "Quit Battery Under Temperature(CHRG) INTR\n");
535 dev_dbg(&info->pdev->dev,
536 "Hit Battery Under Temperature(CHRG) INTR\n");
539 dev_dbg(&info->pdev->dev,
540 "Quit Battery Over Temperature(CHRG) INTR\n");
543 dev_dbg(&info->pdev->dev,
544 "Hit Battery Over Temperature(CHRG) INTR\n");
547 dev_warn(&info->pdev->dev, "Spurious Interrupt!!!\n");
551 power_supply_changed(info->psy_usb);
557 * The HP Pavilion x2 10 series comes in a number of variants:
558 * Bay Trail SoC + AXP288 PMIC, Micro-USB, DMI_BOARD_NAME: "8021"
559 * Bay Trail SoC + AXP288 PMIC, Type-C, DMI_BOARD_NAME: "815D"
560 * Cherry Trail SoC + AXP288 PMIC, Type-C, DMI_BOARD_NAME: "813E"
561 * Cherry Trail SoC + TI PMIC, Type-C, DMI_BOARD_NAME: "827C" or "82F4"
563 * The variants with the AXP288 + Type-C connector are all kinds of special:
565 * 1. They use a Type-C connector which the AXP288 does not support, so when
566 * using a Type-C charger it is not recognized. Unlike most AXP288 devices,
567 * this model actually has mostly working ACPI AC / Battery code, the ACPI code
568 * "solves" this by simply setting the input_current_limit to 3A.
569 * There are still some issues with the ACPI code, so we use this native driver,
570 * and to solve the charging not working (500mA is not enough) issue we hardcode
571 * the 3A input_current_limit like the ACPI code does.
573 * 2. If no charger is connected the machine boots with the vbus-path disabled.
574 * Normally this is done when a 5V boost converter is active to avoid the PMIC
575 * trying to charge from the 5V boost converter's output. This is done when
576 * an OTG host cable is inserted and the ID pin on the micro-B receptacle is
577 * pulled low and the ID pin has an ACPI event handler associated with it
578 * which re-enables the vbus-path when the ID pin is pulled high when the
579 * OTG host cable is removed. The Type-C connector has no ID pin, there is
580 * no ID pin handler and there appears to be no 5V boost converter, so we
581 * end up not charging because the vbus-path is disabled, until we unplug
582 * the charger which automatically clears the vbus-path disable bit and then
583 * on the second plug-in of the adapter we start charging. To solve the not
584 * charging on first charger plugin we unconditionally enable the vbus-path at
585 * probe on this model, which is safe since there is no 5V boost converter.
587 static const struct dmi_system_id axp288_hp_x2_dmi_ids[] = {
590 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
591 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
592 DMI_EXACT_MATCH(DMI_BOARD_NAME, "815D"),
597 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "HP"),
598 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
599 DMI_EXACT_MATCH(DMI_BOARD_NAME, "813E"),
602 {} /* Terminating entry */
605 static void axp288_charger_extcon_evt_worker(struct work_struct *work)
607 struct axp288_chrg_info *info =
608 container_of(work, struct axp288_chrg_info, cable.work);
609 int ret, current_limit;
610 struct extcon_dev *edev = info->cable.edev;
613 ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
615 dev_err(&info->pdev->dev, "Error reading status (%d)\n", ret);
619 /* Offline? Disable charging and bail */
620 if (!(val & PS_STAT_VBUS_VALID)) {
621 dev_dbg(&info->pdev->dev, "USB charger disconnected\n");
622 axp288_charger_enable_charger(info, false);
623 power_supply_changed(info->psy_usb);
627 /* Determine cable/charger type */
628 if (dmi_check_system(axp288_hp_x2_dmi_ids)) {
629 /* See comment above axp288_hp_x2_dmi_ids declaration */
630 dev_dbg(&info->pdev->dev, "HP X2 with Type-C, setting inlmt to 3A\n");
631 current_limit = 3000000;
632 } else if (extcon_get_state(edev, EXTCON_CHG_USB_SDP) > 0) {
633 dev_dbg(&info->pdev->dev, "USB SDP charger is connected\n");
634 current_limit = 500000;
635 } else if (extcon_get_state(edev, EXTCON_CHG_USB_CDP) > 0) {
636 dev_dbg(&info->pdev->dev, "USB CDP charger is connected\n");
637 current_limit = 1500000;
638 } else if (extcon_get_state(edev, EXTCON_CHG_USB_DCP) > 0) {
639 dev_dbg(&info->pdev->dev, "USB DCP charger is connected\n");
640 current_limit = 2000000;
642 /* Charger type detection still in progress, bail. */
646 /* Set vbus current limit first, then enable charger */
647 ret = axp288_charger_set_vbus_inlmt(info, current_limit);
649 axp288_charger_enable_charger(info, true);
651 dev_err(&info->pdev->dev,
652 "error setting current limit (%d)\n", ret);
654 power_supply_changed(info->psy_usb);
657 static int axp288_charger_handle_cable_evt(struct notifier_block *nb,
658 unsigned long event, void *param)
660 struct axp288_chrg_info *info =
661 container_of(nb, struct axp288_chrg_info, cable.nb);
662 schedule_work(&info->cable.work);
666 static void axp288_charger_otg_evt_worker(struct work_struct *work)
668 struct axp288_chrg_info *info =
669 container_of(work, struct axp288_chrg_info, otg.work);
670 struct extcon_dev *edev = info->otg.cable;
671 int ret, usb_host = extcon_get_state(edev, EXTCON_USB_HOST);
673 dev_dbg(&info->pdev->dev, "external connector USB-Host is %s\n",
674 usb_host ? "attached" : "detached");
677 * Set usb_id_short flag to avoid running charger detection logic
680 info->otg.id_short = usb_host;
682 /* Disable VBUS path before enabling the 5V boost */
683 ret = axp288_charger_vbus_path_select(info, !info->otg.id_short);
685 dev_warn(&info->pdev->dev, "vbus path disable failed\n");
688 static int axp288_charger_handle_otg_evt(struct notifier_block *nb,
689 unsigned long event, void *param)
691 struct axp288_chrg_info *info =
692 container_of(nb, struct axp288_chrg_info, otg.id_nb);
694 schedule_work(&info->otg.work);
699 static int charger_init_hw_regs(struct axp288_chrg_info *info)
704 /* Program temperature thresholds */
705 ret = regmap_write(info->regmap, AXP20X_V_LTF_CHRG, CHRG_VLTFC_0C);
707 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
708 AXP20X_V_LTF_CHRG, ret);
712 ret = regmap_write(info->regmap, AXP20X_V_HTF_CHRG, CHRG_VHTFC_45C);
714 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
715 AXP20X_V_HTF_CHRG, ret);
719 /* Do not turn-off charger o/p after charge cycle ends */
720 ret = regmap_update_bits(info->regmap,
722 CNTL2_CHG_OUT_TURNON, CNTL2_CHG_OUT_TURNON);
724 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
725 AXP20X_CHRG_CTRL2, ret);
729 /* Setup ending condition for charging to be 10% of I(chrg) */
730 ret = regmap_update_bits(info->regmap,
732 CHRG_CCCV_ITERM_20P, 0);
734 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
735 AXP20X_CHRG_CTRL1, ret);
739 /* Disable OCV-SOC curve calibration */
740 ret = regmap_update_bits(info->regmap,
742 FG_CNTL_OCV_ADJ_EN, 0);
744 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
745 AXP20X_CC_CTRL, ret);
749 if (dmi_check_system(axp288_hp_x2_dmi_ids)) {
750 /* See comment above axp288_hp_x2_dmi_ids declaration */
751 ret = axp288_charger_vbus_path_select(info, true);
756 /* Read current charge voltage and current limit */
757 ret = regmap_read(info->regmap, AXP20X_CHRG_CTRL1, &val);
759 dev_err(&info->pdev->dev, "register(%x) read error(%d)\n",
760 AXP20X_CHRG_CTRL1, ret);
764 /* Determine charge voltage */
765 cv = (val & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS;
767 case CHRG_CCCV_CV_4100MV:
768 info->cv = CV_4100MV;
770 case CHRG_CCCV_CV_4150MV:
771 info->cv = CV_4150MV;
773 case CHRG_CCCV_CV_4200MV:
774 info->cv = CV_4200MV;
776 case CHRG_CCCV_CV_4350MV:
777 info->cv = CV_4350MV;
781 /* Determine charge current limit */
782 cc = (val & CHRG_CCCV_CC_MASK) >> CHRG_CCCV_CC_BIT_POS;
783 cc = (cc * CHRG_CCCV_CC_LSB_RES) + CHRG_CCCV_CC_OFFSET;
787 * Do not allow the user to configure higher settings then those
788 * set by the firmware
790 info->max_cv = info->cv;
791 info->max_cc = info->cc;
796 static void axp288_charger_cancel_work(void *data)
798 struct axp288_chrg_info *info = data;
800 cancel_work_sync(&info->otg.work);
801 cancel_work_sync(&info->cable.work);
804 static int axp288_charger_probe(struct platform_device *pdev)
807 struct axp288_chrg_info *info;
808 struct device *dev = &pdev->dev;
809 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
810 struct power_supply_config charger_cfg = {};
814 * On some devices the fuelgauge and charger parts of the axp288 are
815 * not used, check that the fuelgauge is enabled (CC_CTRL != 0).
817 ret = regmap_read(axp20x->regmap, AXP20X_CC_CTRL, &val);
823 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
828 info->regmap = axp20x->regmap;
829 info->regmap_irqc = axp20x->regmap_irqc;
831 info->cable.edev = extcon_get_extcon_dev(AXP288_EXTCON_DEV_NAME);
832 if (info->cable.edev == NULL) {
833 dev_dbg(&pdev->dev, "%s is not ready, probe deferred\n",
834 AXP288_EXTCON_DEV_NAME);
835 return -EPROBE_DEFER;
838 if (acpi_dev_present(USB_HOST_EXTCON_HID, NULL, -1)) {
839 info->otg.cable = extcon_get_extcon_dev(USB_HOST_EXTCON_NAME);
840 if (info->otg.cable == NULL) {
841 dev_dbg(dev, "EXTCON_USB_HOST is not ready, probe deferred\n");
842 return -EPROBE_DEFER;
845 "Using " USB_HOST_EXTCON_HID " extcon for usb-id\n");
848 platform_set_drvdata(pdev, info);
850 ret = charger_init_hw_regs(info);
854 /* Register with power supply class */
855 charger_cfg.drv_data = info;
856 info->psy_usb = devm_power_supply_register(dev, &axp288_charger_desc,
858 if (IS_ERR(info->psy_usb)) {
859 ret = PTR_ERR(info->psy_usb);
860 dev_err(dev, "failed to register power supply: %d\n", ret);
864 /* Cancel our work on cleanup, register this before the notifiers */
865 ret = devm_add_action(dev, axp288_charger_cancel_work, info);
869 /* Register for extcon notification */
870 INIT_WORK(&info->cable.work, axp288_charger_extcon_evt_worker);
871 info->cable.nb.notifier_call = axp288_charger_handle_cable_evt;
872 ret = devm_extcon_register_notifier_all(dev, info->cable.edev,
875 dev_err(dev, "failed to register cable extcon notifier\n");
878 schedule_work(&info->cable.work);
880 /* Register for OTG notification */
881 INIT_WORK(&info->otg.work, axp288_charger_otg_evt_worker);
882 info->otg.id_nb.notifier_call = axp288_charger_handle_otg_evt;
883 if (info->otg.cable) {
884 ret = devm_extcon_register_notifier(&pdev->dev, info->otg.cable,
885 EXTCON_USB_HOST, &info->otg.id_nb);
887 dev_err(dev, "failed to register EXTCON_USB_HOST notifier\n");
890 schedule_work(&info->otg.work);
893 /* Register charger interrupts */
894 for (i = 0; i < CHRG_INTR_END; i++) {
895 pirq = platform_get_irq(info->pdev, i);
897 dev_err(&pdev->dev, "Failed to get IRQ: %d\n", pirq);
900 info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq);
901 if (info->irq[i] < 0) {
902 dev_warn(&info->pdev->dev,
903 "failed to get virtual interrupt=%d\n", pirq);
906 ret = devm_request_threaded_irq(&info->pdev->dev, info->irq[i],
907 NULL, axp288_charger_irq_thread_handler,
908 IRQF_ONESHOT, info->pdev->name, info);
910 dev_err(&pdev->dev, "failed to request interrupt=%d\n",
919 static const struct platform_device_id axp288_charger_id_table[] = {
920 { .name = "axp288_charger" },
923 MODULE_DEVICE_TABLE(platform, axp288_charger_id_table);
925 static struct platform_driver axp288_charger_driver = {
926 .probe = axp288_charger_probe,
927 .id_table = axp288_charger_id_table,
929 .name = "axp288_charger",
933 module_platform_driver(axp288_charger_driver);
935 MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
936 MODULE_DESCRIPTION("X-power AXP288 Charger Driver");
937 MODULE_LICENSE("GPL v2");