1 // SPDX-License-Identifier: GPL-2.0-only
3 * axp288_charger.c - X-power AXP288 PMIC Charger driver
5 * Copyright (C) 2016-2017 Hans de Goede <hdegoede@redhat.com>
6 * Copyright (C) 2014 Intel Corporation
7 * Author: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
10 #include <linux/acpi.h>
11 #include <linux/bitops.h>
12 #include <linux/module.h>
13 #include <linux/device.h>
14 #include <linux/regmap.h>
15 #include <linux/workqueue.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
18 #include <linux/usb/otg.h>
19 #include <linux/notifier.h>
20 #include <linux/power_supply.h>
21 #include <linux/property.h>
22 #include <linux/mfd/axp20x.h>
23 #include <linux/extcon.h>
24 #include <linux/dmi.h>
26 #define PS_STAT_VBUS_TRIGGER BIT(0)
27 #define PS_STAT_BAT_CHRG_DIR BIT(2)
28 #define PS_STAT_VBAT_ABOVE_VHOLD BIT(3)
29 #define PS_STAT_VBUS_VALID BIT(4)
30 #define PS_STAT_VBUS_PRESENT BIT(5)
32 #define CHRG_STAT_BAT_SAFE_MODE BIT(3)
33 #define CHRG_STAT_BAT_VALID BIT(4)
34 #define CHRG_STAT_BAT_PRESENT BIT(5)
35 #define CHRG_STAT_CHARGING BIT(6)
36 #define CHRG_STAT_PMIC_OTP BIT(7)
38 #define VBUS_ISPOUT_CUR_LIM_MASK 0x03
39 #define VBUS_ISPOUT_CUR_LIM_BIT_POS 0
40 #define VBUS_ISPOUT_CUR_LIM_900MA 0x0 /* 900mA */
41 #define VBUS_ISPOUT_CUR_LIM_1500MA 0x1 /* 1500mA */
42 #define VBUS_ISPOUT_CUR_LIM_2000MA 0x2 /* 2000mA */
43 #define VBUS_ISPOUT_CUR_NO_LIM 0x3 /* 2500mA */
44 #define VBUS_ISPOUT_VHOLD_SET_MASK 0x31
45 #define VBUS_ISPOUT_VHOLD_SET_BIT_POS 0x3
46 #define VBUS_ISPOUT_VHOLD_SET_OFFSET 4000 /* 4000mV */
47 #define VBUS_ISPOUT_VHOLD_SET_LSB_RES 100 /* 100mV */
48 #define VBUS_ISPOUT_VHOLD_SET_4300MV 0x3 /* 4300mV */
49 #define VBUS_ISPOUT_VBUS_PATH_DIS BIT(7)
51 #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */
52 #define CHRG_CCCV_CC_BIT_POS 0
53 #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */
54 #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */
55 #define CHRG_CCCV_ITERM_20P BIT(4) /* 20% of CC */
56 #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */
57 #define CHRG_CCCV_CV_BIT_POS 5
58 #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */
59 #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */
60 #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */
61 #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */
62 #define CHRG_CCCV_CHG_EN BIT(7)
64 #define CNTL2_CC_TIMEOUT_MASK 0x3 /* 2 bits */
65 #define CNTL2_CC_TIMEOUT_OFFSET 6 /* 6 Hrs */
66 #define CNTL2_CC_TIMEOUT_LSB_RES 2 /* 2 Hrs */
67 #define CNTL2_CC_TIMEOUT_12HRS 0x3 /* 12 Hrs */
68 #define CNTL2_CHGLED_TYPEB BIT(4)
69 #define CNTL2_CHG_OUT_TURNON BIT(5)
70 #define CNTL2_PC_TIMEOUT_MASK 0xC0
71 #define CNTL2_PC_TIMEOUT_OFFSET 40 /* 40 mins */
72 #define CNTL2_PC_TIMEOUT_LSB_RES 10 /* 10 mins */
73 #define CNTL2_PC_TIMEOUT_70MINS 0x3
75 #define CHRG_ILIM_TEMP_LOOP_EN BIT(3)
76 #define CHRG_VBUS_ILIM_MASK 0xf0
77 #define CHRG_VBUS_ILIM_BIT_POS 4
78 #define CHRG_VBUS_ILIM_100MA 0x0 /* 100mA */
79 #define CHRG_VBUS_ILIM_500MA 0x1 /* 500mA */
80 #define CHRG_VBUS_ILIM_900MA 0x2 /* 900mA */
81 #define CHRG_VBUS_ILIM_1500MA 0x3 /* 1500mA */
82 #define CHRG_VBUS_ILIM_2000MA 0x4 /* 2000mA */
83 #define CHRG_VBUS_ILIM_2500MA 0x5 /* 2500mA */
84 #define CHRG_VBUS_ILIM_3000MA 0x6 /* 3000mA */
85 #define CHRG_VBUS_ILIM_3500MA 0x7 /* 3500mA */
86 #define CHRG_VBUS_ILIM_4000MA 0x8 /* 4000mA */
88 #define CHRG_VLTFC_0C 0xA5 /* 0 DegC */
89 #define CHRG_VHTFC_45C 0x1F /* 45 DegC */
91 #define FG_CNTL_OCV_ADJ_EN BIT(3)
93 #define CV_4100MV 4100 /* 4100mV */
94 #define CV_4150MV 4150 /* 4150mV */
95 #define CV_4200MV 4200 /* 4200mV */
96 #define CV_4350MV 4350 /* 4350mV */
98 #define AXP288_EXTCON_DEV_NAME "axp288_extcon"
99 #define USB_HOST_EXTCON_HID "INT3496"
100 #define USB_HOST_EXTCON_NAME "INT3496:00"
115 struct axp288_chrg_info {
116 struct platform_device *pdev;
117 struct regmap *regmap;
118 struct regmap_irq_chip_data *regmap_irqc;
119 int irq[CHRG_INTR_END];
120 struct power_supply *psy_usb;
124 struct work_struct work;
125 struct extcon_dev *cable;
126 struct notifier_block id_nb;
130 /* SDP/CDP/DCP USB charging cable notifications */
132 struct extcon_dev *edev;
133 struct notifier_block nb;
134 struct work_struct work;
143 static inline int axp288_charger_set_cc(struct axp288_chrg_info *info, int cc)
148 if (cc < CHRG_CCCV_CC_OFFSET)
149 cc = CHRG_CCCV_CC_OFFSET;
150 else if (cc > info->max_cc)
153 reg_val = (cc - CHRG_CCCV_CC_OFFSET) / CHRG_CCCV_CC_LSB_RES;
154 cc = (reg_val * CHRG_CCCV_CC_LSB_RES) + CHRG_CCCV_CC_OFFSET;
155 reg_val = reg_val << CHRG_CCCV_CC_BIT_POS;
157 ret = regmap_update_bits(info->regmap,
159 CHRG_CCCV_CC_MASK, reg_val);
166 static inline int axp288_charger_set_cv(struct axp288_chrg_info *info, int cv)
171 if (cv <= CV_4100MV) {
172 reg_val = CHRG_CCCV_CV_4100MV;
174 } else if (cv <= CV_4150MV) {
175 reg_val = CHRG_CCCV_CV_4150MV;
177 } else if (cv <= CV_4200MV) {
178 reg_val = CHRG_CCCV_CV_4200MV;
181 reg_val = CHRG_CCCV_CV_4350MV;
185 reg_val = reg_val << CHRG_CCCV_CV_BIT_POS;
187 ret = regmap_update_bits(info->regmap,
189 CHRG_CCCV_CV_MASK, reg_val);
197 static int axp288_charger_get_vbus_inlmt(struct axp288_chrg_info *info)
202 ret = regmap_read(info->regmap, AXP20X_CHRG_BAK_CTRL, &val);
206 val >>= CHRG_VBUS_ILIM_BIT_POS;
208 case CHRG_VBUS_ILIM_100MA:
210 case CHRG_VBUS_ILIM_500MA:
212 case CHRG_VBUS_ILIM_900MA:
214 case CHRG_VBUS_ILIM_1500MA:
216 case CHRG_VBUS_ILIM_2000MA:
218 case CHRG_VBUS_ILIM_2500MA:
220 case CHRG_VBUS_ILIM_3000MA:
222 case CHRG_VBUS_ILIM_3500MA:
225 /* All b1xxx values map to 4000 mA */
230 static inline int axp288_charger_set_vbus_inlmt(struct axp288_chrg_info *info,
236 if (inlmt >= 4000000)
237 reg_val = CHRG_VBUS_ILIM_4000MA << CHRG_VBUS_ILIM_BIT_POS;
238 else if (inlmt >= 3500000)
239 reg_val = CHRG_VBUS_ILIM_3500MA << CHRG_VBUS_ILIM_BIT_POS;
240 else if (inlmt >= 3000000)
241 reg_val = CHRG_VBUS_ILIM_3000MA << CHRG_VBUS_ILIM_BIT_POS;
242 else if (inlmt >= 2500000)
243 reg_val = CHRG_VBUS_ILIM_2500MA << CHRG_VBUS_ILIM_BIT_POS;
244 else if (inlmt >= 2000000)
245 reg_val = CHRG_VBUS_ILIM_2000MA << CHRG_VBUS_ILIM_BIT_POS;
246 else if (inlmt >= 1500000)
247 reg_val = CHRG_VBUS_ILIM_1500MA << CHRG_VBUS_ILIM_BIT_POS;
248 else if (inlmt >= 900000)
249 reg_val = CHRG_VBUS_ILIM_900MA << CHRG_VBUS_ILIM_BIT_POS;
250 else if (inlmt >= 500000)
251 reg_val = CHRG_VBUS_ILIM_500MA << CHRG_VBUS_ILIM_BIT_POS;
253 reg_val = CHRG_VBUS_ILIM_100MA << CHRG_VBUS_ILIM_BIT_POS;
255 ret = regmap_update_bits(info->regmap, AXP20X_CHRG_BAK_CTRL,
256 CHRG_VBUS_ILIM_MASK, reg_val);
258 dev_err(&info->pdev->dev, "charger BAK control %d\n", ret);
263 static int axp288_charger_vbus_path_select(struct axp288_chrg_info *info,
269 ret = regmap_update_bits(info->regmap, AXP20X_VBUS_IPSOUT_MGMT,
270 VBUS_ISPOUT_VBUS_PATH_DIS, 0);
272 ret = regmap_update_bits(info->regmap, AXP20X_VBUS_IPSOUT_MGMT,
273 VBUS_ISPOUT_VBUS_PATH_DIS, VBUS_ISPOUT_VBUS_PATH_DIS);
276 dev_err(&info->pdev->dev, "axp288 vbus path select %d\n", ret);
281 static int axp288_charger_enable_charger(struct axp288_chrg_info *info,
287 ret = regmap_update_bits(info->regmap, AXP20X_CHRG_CTRL1,
288 CHRG_CCCV_CHG_EN, CHRG_CCCV_CHG_EN);
290 ret = regmap_update_bits(info->regmap, AXP20X_CHRG_CTRL1,
291 CHRG_CCCV_CHG_EN, 0);
293 dev_err(&info->pdev->dev, "axp288 enable charger %d\n", ret);
298 static int axp288_charger_is_present(struct axp288_chrg_info *info)
300 int ret, present = 0;
303 ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
307 if (val & PS_STAT_VBUS_PRESENT)
312 static int axp288_charger_is_online(struct axp288_chrg_info *info)
317 ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
321 if (val & PS_STAT_VBUS_VALID)
326 static int axp288_get_charger_health(struct axp288_chrg_info *info)
328 int ret, pwr_stat, chrg_stat;
329 int health = POWER_SUPPLY_HEALTH_UNKNOWN;
332 ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
333 if ((ret < 0) || !(val & PS_STAT_VBUS_PRESENT))
334 goto health_read_fail;
338 ret = regmap_read(info->regmap, AXP20X_PWR_OP_MODE, &val);
340 goto health_read_fail;
344 if (!(pwr_stat & PS_STAT_VBUS_VALID))
345 health = POWER_SUPPLY_HEALTH_DEAD;
346 else if (chrg_stat & CHRG_STAT_PMIC_OTP)
347 health = POWER_SUPPLY_HEALTH_OVERHEAT;
348 else if (chrg_stat & CHRG_STAT_BAT_SAFE_MODE)
349 health = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
351 health = POWER_SUPPLY_HEALTH_GOOD;
357 static int axp288_charger_usb_set_property(struct power_supply *psy,
358 enum power_supply_property psp,
359 const union power_supply_propval *val)
361 struct axp288_chrg_info *info = power_supply_get_drvdata(psy);
366 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
367 scaled_val = min(val->intval, info->max_cc);
368 scaled_val = DIV_ROUND_CLOSEST(scaled_val, 1000);
369 ret = axp288_charger_set_cc(info, scaled_val);
371 dev_warn(&info->pdev->dev, "set charge current failed\n");
373 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
374 scaled_val = min(val->intval, info->max_cv);
375 scaled_val = DIV_ROUND_CLOSEST(scaled_val, 1000);
376 ret = axp288_charger_set_cv(info, scaled_val);
378 dev_warn(&info->pdev->dev, "set charge voltage failed\n");
380 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
381 ret = axp288_charger_set_vbus_inlmt(info, val->intval);
383 dev_warn(&info->pdev->dev, "set input current limit failed\n");
392 static int axp288_charger_usb_get_property(struct power_supply *psy,
393 enum power_supply_property psp,
394 union power_supply_propval *val)
396 struct axp288_chrg_info *info = power_supply_get_drvdata(psy);
400 case POWER_SUPPLY_PROP_PRESENT:
401 /* Check for OTG case first */
402 if (info->otg.id_short) {
406 ret = axp288_charger_is_present(info);
411 case POWER_SUPPLY_PROP_ONLINE:
412 /* Check for OTG case first */
413 if (info->otg.id_short) {
417 ret = axp288_charger_is_online(info);
422 case POWER_SUPPLY_PROP_HEALTH:
423 val->intval = axp288_get_charger_health(info);
425 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
426 val->intval = info->cc * 1000;
428 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
429 val->intval = info->max_cc * 1000;
431 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
432 val->intval = info->cv * 1000;
434 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
435 val->intval = info->max_cv * 1000;
437 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
438 ret = axp288_charger_get_vbus_inlmt(info);
450 static int axp288_charger_property_is_writeable(struct power_supply *psy,
451 enum power_supply_property psp)
456 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
457 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
458 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
468 static enum power_supply_property axp288_usb_props[] = {
469 POWER_SUPPLY_PROP_PRESENT,
470 POWER_SUPPLY_PROP_ONLINE,
471 POWER_SUPPLY_PROP_TYPE,
472 POWER_SUPPLY_PROP_HEALTH,
473 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
474 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
475 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
476 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
477 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
480 static const struct power_supply_desc axp288_charger_desc = {
481 .name = "axp288_charger",
482 .type = POWER_SUPPLY_TYPE_USB,
483 .properties = axp288_usb_props,
484 .num_properties = ARRAY_SIZE(axp288_usb_props),
485 .get_property = axp288_charger_usb_get_property,
486 .set_property = axp288_charger_usb_set_property,
487 .property_is_writeable = axp288_charger_property_is_writeable,
490 static irqreturn_t axp288_charger_irq_thread_handler(int irq, void *dev)
492 struct axp288_chrg_info *info = dev;
495 for (i = 0; i < CHRG_INTR_END; i++) {
496 if (info->irq[i] == irq)
500 if (i >= CHRG_INTR_END) {
501 dev_warn(&info->pdev->dev, "spurious interrupt!!\n");
507 dev_dbg(&info->pdev->dev, "VBUS Over Voltage INTR\n");
509 case CHARGE_DONE_IRQ:
510 dev_dbg(&info->pdev->dev, "Charging Done INTR\n");
512 case CHARGE_CHARGING_IRQ:
513 dev_dbg(&info->pdev->dev, "Start Charging IRQ\n");
515 case BAT_SAFE_QUIT_IRQ:
516 dev_dbg(&info->pdev->dev,
517 "Quit Safe Mode(restart timer) Charging IRQ\n");
519 case BAT_SAFE_ENTER_IRQ:
520 dev_dbg(&info->pdev->dev,
521 "Enter Safe Mode(timer expire) Charging IRQ\n");
524 dev_dbg(&info->pdev->dev,
525 "Quit Battery Under Temperature(CHRG) INTR\n");
528 dev_dbg(&info->pdev->dev,
529 "Hit Battery Under Temperature(CHRG) INTR\n");
532 dev_dbg(&info->pdev->dev,
533 "Quit Battery Over Temperature(CHRG) INTR\n");
536 dev_dbg(&info->pdev->dev,
537 "Hit Battery Over Temperature(CHRG) INTR\n");
540 dev_warn(&info->pdev->dev, "Spurious Interrupt!!!\n");
544 power_supply_changed(info->psy_usb);
550 * The HP Pavilion x2 10 series comes in a number of variants:
551 * Bay Trail SoC + AXP288 PMIC, DMI_BOARD_NAME: "815D"
552 * Cherry Trail SoC + AXP288 PMIC, DMI_BOARD_NAME: "813E"
553 * Cherry Trail SoC + TI PMIC, DMI_BOARD_NAME: "827C" or "82F4"
555 * The variants with the AXP288 PMIC are all kinds of special:
557 * 1. All variants use a Type-C connector which the AXP288 does not support, so
558 * when using a Type-C charger it is not recognized. Unlike most AXP288 devices,
559 * this model actually has mostly working ACPI AC / Battery code, the ACPI code
560 * "solves" this by simply setting the input_current_limit to 3A.
561 * There are still some issues with the ACPI code, so we use this native driver,
562 * and to solve the charging not working (500mA is not enough) issue we hardcode
563 * the 3A input_current_limit like the ACPI code does.
565 * 2. If no charger is connected the machine boots with the vbus-path disabled.
566 * Normally this is done when a 5V boost converter is active to avoid the PMIC
567 * trying to charge from the 5V boost converter's output. This is done when
568 * an OTG host cable is inserted and the ID pin on the micro-B receptacle is
569 * pulled low and the ID pin has an ACPI event handler associated with it
570 * which re-enables the vbus-path when the ID pin is pulled high when the
571 * OTG host cable is removed. The Type-C connector has no ID pin, there is
572 * no ID pin handler and there appears to be no 5V boost converter, so we
573 * end up not charging because the vbus-path is disabled, until we unplug
574 * the charger which automatically clears the vbus-path disable bit and then
575 * on the second plug-in of the adapter we start charging. To solve the not
576 * charging on first charger plugin we unconditionally enable the vbus-path at
577 * probe on this model, which is safe since there is no 5V boost converter.
579 static const struct dmi_system_id axp288_hp_x2_dmi_ids[] = {
582 * Bay Trail model has "Hewlett-Packard" as sys_vendor, Cherry
583 * Trail model has "HP", so we only match on product_name.
586 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
589 {} /* Terminating entry */
592 static void axp288_charger_extcon_evt_worker(struct work_struct *work)
594 struct axp288_chrg_info *info =
595 container_of(work, struct axp288_chrg_info, cable.work);
596 int ret, current_limit;
597 struct extcon_dev *edev = info->cable.edev;
600 ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
602 dev_err(&info->pdev->dev, "Error reading status (%d)\n", ret);
606 /* Offline? Disable charging and bail */
607 if (!(val & PS_STAT_VBUS_VALID)) {
608 dev_dbg(&info->pdev->dev, "USB charger disconnected\n");
609 axp288_charger_enable_charger(info, false);
610 power_supply_changed(info->psy_usb);
614 /* Determine cable/charger type */
615 if (dmi_check_system(axp288_hp_x2_dmi_ids)) {
616 /* See comment above axp288_hp_x2_dmi_ids declaration */
617 dev_dbg(&info->pdev->dev, "HP X2 with Type-C, setting inlmt to 3A\n");
618 current_limit = 3000000;
619 } else if (extcon_get_state(edev, EXTCON_CHG_USB_SDP) > 0) {
620 dev_dbg(&info->pdev->dev, "USB SDP charger is connected\n");
621 current_limit = 500000;
622 } else if (extcon_get_state(edev, EXTCON_CHG_USB_CDP) > 0) {
623 dev_dbg(&info->pdev->dev, "USB CDP charger is connected\n");
624 current_limit = 1500000;
625 } else if (extcon_get_state(edev, EXTCON_CHG_USB_DCP) > 0) {
626 dev_dbg(&info->pdev->dev, "USB DCP charger is connected\n");
627 current_limit = 2000000;
629 /* Charger type detection still in progress, bail. */
633 /* Set vbus current limit first, then enable charger */
634 ret = axp288_charger_set_vbus_inlmt(info, current_limit);
636 axp288_charger_enable_charger(info, true);
638 dev_err(&info->pdev->dev,
639 "error setting current limit (%d)\n", ret);
641 power_supply_changed(info->psy_usb);
644 static int axp288_charger_handle_cable_evt(struct notifier_block *nb,
645 unsigned long event, void *param)
647 struct axp288_chrg_info *info =
648 container_of(nb, struct axp288_chrg_info, cable.nb);
649 schedule_work(&info->cable.work);
653 static void axp288_charger_otg_evt_worker(struct work_struct *work)
655 struct axp288_chrg_info *info =
656 container_of(work, struct axp288_chrg_info, otg.work);
657 struct extcon_dev *edev = info->otg.cable;
658 int ret, usb_host = extcon_get_state(edev, EXTCON_USB_HOST);
660 dev_dbg(&info->pdev->dev, "external connector USB-Host is %s\n",
661 usb_host ? "attached" : "detached");
664 * Set usb_id_short flag to avoid running charger detection logic
667 info->otg.id_short = usb_host;
669 /* Disable VBUS path before enabling the 5V boost */
670 ret = axp288_charger_vbus_path_select(info, !info->otg.id_short);
672 dev_warn(&info->pdev->dev, "vbus path disable failed\n");
675 static int axp288_charger_handle_otg_evt(struct notifier_block *nb,
676 unsigned long event, void *param)
678 struct axp288_chrg_info *info =
679 container_of(nb, struct axp288_chrg_info, otg.id_nb);
681 schedule_work(&info->otg.work);
686 static int charger_init_hw_regs(struct axp288_chrg_info *info)
691 /* Program temperature thresholds */
692 ret = regmap_write(info->regmap, AXP20X_V_LTF_CHRG, CHRG_VLTFC_0C);
694 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
695 AXP20X_V_LTF_CHRG, ret);
699 ret = regmap_write(info->regmap, AXP20X_V_HTF_CHRG, CHRG_VHTFC_45C);
701 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
702 AXP20X_V_HTF_CHRG, ret);
706 /* Do not turn-off charger o/p after charge cycle ends */
707 ret = regmap_update_bits(info->regmap,
709 CNTL2_CHG_OUT_TURNON, CNTL2_CHG_OUT_TURNON);
711 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
712 AXP20X_CHRG_CTRL2, ret);
716 /* Setup ending condition for charging to be 10% of I(chrg) */
717 ret = regmap_update_bits(info->regmap,
719 CHRG_CCCV_ITERM_20P, 0);
721 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
722 AXP20X_CHRG_CTRL1, ret);
726 /* Disable OCV-SOC curve calibration */
727 ret = regmap_update_bits(info->regmap,
729 FG_CNTL_OCV_ADJ_EN, 0);
731 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
732 AXP20X_CC_CTRL, ret);
736 if (dmi_check_system(axp288_hp_x2_dmi_ids)) {
737 /* See comment above axp288_hp_x2_dmi_ids declaration */
738 ret = axp288_charger_vbus_path_select(info, true);
743 /* Read current charge voltage and current limit */
744 ret = regmap_read(info->regmap, AXP20X_CHRG_CTRL1, &val);
746 dev_err(&info->pdev->dev, "register(%x) read error(%d)\n",
747 AXP20X_CHRG_CTRL1, ret);
751 /* Determine charge voltage */
752 cv = (val & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS;
754 case CHRG_CCCV_CV_4100MV:
755 info->cv = CV_4100MV;
757 case CHRG_CCCV_CV_4150MV:
758 info->cv = CV_4150MV;
760 case CHRG_CCCV_CV_4200MV:
761 info->cv = CV_4200MV;
763 case CHRG_CCCV_CV_4350MV:
764 info->cv = CV_4350MV;
768 /* Determine charge current limit */
769 cc = (val & CHRG_CCCV_CC_MASK) >> CHRG_CCCV_CC_BIT_POS;
770 cc = (cc * CHRG_CCCV_CC_LSB_RES) + CHRG_CCCV_CC_OFFSET;
774 * Do not allow the user to configure higher settings then those
775 * set by the firmware
777 info->max_cv = info->cv;
778 info->max_cc = info->cc;
783 static void axp288_charger_cancel_work(void *data)
785 struct axp288_chrg_info *info = data;
787 cancel_work_sync(&info->otg.work);
788 cancel_work_sync(&info->cable.work);
791 static int axp288_charger_probe(struct platform_device *pdev)
794 struct axp288_chrg_info *info;
795 struct device *dev = &pdev->dev;
796 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
797 struct power_supply_config charger_cfg = {};
801 * On some devices the fuelgauge and charger parts of the axp288 are
802 * not used, check that the fuelgauge is enabled (CC_CTRL != 0).
804 ret = regmap_read(axp20x->regmap, AXP20X_CC_CTRL, &val);
810 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
815 info->regmap = axp20x->regmap;
816 info->regmap_irqc = axp20x->regmap_irqc;
818 info->cable.edev = extcon_get_extcon_dev(AXP288_EXTCON_DEV_NAME);
819 if (info->cable.edev == NULL) {
820 dev_dbg(&pdev->dev, "%s is not ready, probe deferred\n",
821 AXP288_EXTCON_DEV_NAME);
822 return -EPROBE_DEFER;
825 if (acpi_dev_present(USB_HOST_EXTCON_HID, NULL, -1)) {
826 info->otg.cable = extcon_get_extcon_dev(USB_HOST_EXTCON_NAME);
827 if (info->otg.cable == NULL) {
828 dev_dbg(dev, "EXTCON_USB_HOST is not ready, probe deferred\n");
829 return -EPROBE_DEFER;
832 "Using " USB_HOST_EXTCON_HID " extcon for usb-id\n");
835 platform_set_drvdata(pdev, info);
837 ret = charger_init_hw_regs(info);
841 /* Register with power supply class */
842 charger_cfg.drv_data = info;
843 info->psy_usb = devm_power_supply_register(dev, &axp288_charger_desc,
845 if (IS_ERR(info->psy_usb)) {
846 ret = PTR_ERR(info->psy_usb);
847 dev_err(dev, "failed to register power supply: %d\n", ret);
851 /* Cancel our work on cleanup, register this before the notifiers */
852 ret = devm_add_action(dev, axp288_charger_cancel_work, info);
856 /* Register for extcon notification */
857 INIT_WORK(&info->cable.work, axp288_charger_extcon_evt_worker);
858 info->cable.nb.notifier_call = axp288_charger_handle_cable_evt;
859 ret = devm_extcon_register_notifier_all(dev, info->cable.edev,
862 dev_err(dev, "failed to register cable extcon notifier\n");
865 schedule_work(&info->cable.work);
867 /* Register for OTG notification */
868 INIT_WORK(&info->otg.work, axp288_charger_otg_evt_worker);
869 info->otg.id_nb.notifier_call = axp288_charger_handle_otg_evt;
870 if (info->otg.cable) {
871 ret = devm_extcon_register_notifier(&pdev->dev, info->otg.cable,
872 EXTCON_USB_HOST, &info->otg.id_nb);
874 dev_err(dev, "failed to register EXTCON_USB_HOST notifier\n");
877 schedule_work(&info->otg.work);
880 /* Register charger interrupts */
881 for (i = 0; i < CHRG_INTR_END; i++) {
882 pirq = platform_get_irq(info->pdev, i);
886 info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq);
887 if (info->irq[i] < 0) {
888 dev_warn(&info->pdev->dev,
889 "failed to get virtual interrupt=%d\n", pirq);
892 ret = devm_request_threaded_irq(&info->pdev->dev, info->irq[i],
893 NULL, axp288_charger_irq_thread_handler,
894 IRQF_ONESHOT, info->pdev->name, info);
896 dev_err(&pdev->dev, "failed to request interrupt=%d\n",
905 static const struct platform_device_id axp288_charger_id_table[] = {
906 { .name = "axp288_charger" },
909 MODULE_DEVICE_TABLE(platform, axp288_charger_id_table);
911 static struct platform_driver axp288_charger_driver = {
912 .probe = axp288_charger_probe,
913 .id_table = axp288_charger_id_table,
915 .name = "axp288_charger",
919 module_platform_driver(axp288_charger_driver);
921 MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
922 MODULE_DESCRIPTION("X-power AXP288 Charger Driver");
923 MODULE_LICENSE("GPL v2");