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>
25 #define PS_STAT_VBUS_TRIGGER BIT(0)
26 #define PS_STAT_BAT_CHRG_DIR BIT(2)
27 #define PS_STAT_VBAT_ABOVE_VHOLD BIT(3)
28 #define PS_STAT_VBUS_VALID BIT(4)
29 #define PS_STAT_VBUS_PRESENT BIT(5)
31 #define CHRG_STAT_BAT_SAFE_MODE BIT(3)
32 #define CHRG_STAT_BAT_VALID BIT(4)
33 #define CHRG_STAT_BAT_PRESENT BIT(5)
34 #define CHRG_STAT_CHARGING BIT(6)
35 #define CHRG_STAT_PMIC_OTP BIT(7)
37 #define VBUS_ISPOUT_CUR_LIM_MASK 0x03
38 #define VBUS_ISPOUT_CUR_LIM_BIT_POS 0
39 #define VBUS_ISPOUT_CUR_LIM_900MA 0x0 /* 900mA */
40 #define VBUS_ISPOUT_CUR_LIM_1500MA 0x1 /* 1500mA */
41 #define VBUS_ISPOUT_CUR_LIM_2000MA 0x2 /* 2000mA */
42 #define VBUS_ISPOUT_CUR_NO_LIM 0x3 /* 2500mA */
43 #define VBUS_ISPOUT_VHOLD_SET_MASK 0x31
44 #define VBUS_ISPOUT_VHOLD_SET_BIT_POS 0x3
45 #define VBUS_ISPOUT_VHOLD_SET_OFFSET 4000 /* 4000mV */
46 #define VBUS_ISPOUT_VHOLD_SET_LSB_RES 100 /* 100mV */
47 #define VBUS_ISPOUT_VHOLD_SET_4300MV 0x3 /* 4300mV */
48 #define VBUS_ISPOUT_VBUS_PATH_DIS BIT(7)
50 #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */
51 #define CHRG_CCCV_CC_BIT_POS 0
52 #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */
53 #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */
54 #define CHRG_CCCV_ITERM_20P BIT(4) /* 20% of CC */
55 #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */
56 #define CHRG_CCCV_CV_BIT_POS 5
57 #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */
58 #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */
59 #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */
60 #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */
61 #define CHRG_CCCV_CHG_EN BIT(7)
63 #define CNTL2_CC_TIMEOUT_MASK 0x3 /* 2 bits */
64 #define CNTL2_CC_TIMEOUT_OFFSET 6 /* 6 Hrs */
65 #define CNTL2_CC_TIMEOUT_LSB_RES 2 /* 2 Hrs */
66 #define CNTL2_CC_TIMEOUT_12HRS 0x3 /* 12 Hrs */
67 #define CNTL2_CHGLED_TYPEB BIT(4)
68 #define CNTL2_CHG_OUT_TURNON BIT(5)
69 #define CNTL2_PC_TIMEOUT_MASK 0xC0
70 #define CNTL2_PC_TIMEOUT_OFFSET 40 /* 40 mins */
71 #define CNTL2_PC_TIMEOUT_LSB_RES 10 /* 10 mins */
72 #define CNTL2_PC_TIMEOUT_70MINS 0x3
74 #define CHRG_ILIM_TEMP_LOOP_EN BIT(3)
75 #define CHRG_VBUS_ILIM_MASK 0xf0
76 #define CHRG_VBUS_ILIM_BIT_POS 4
77 #define CHRG_VBUS_ILIM_100MA 0x0 /* 100mA */
78 #define CHRG_VBUS_ILIM_500MA 0x1 /* 500mA */
79 #define CHRG_VBUS_ILIM_900MA 0x2 /* 900mA */
80 #define CHRG_VBUS_ILIM_1500MA 0x3 /* 1500mA */
81 #define CHRG_VBUS_ILIM_2000MA 0x4 /* 2000mA */
82 #define CHRG_VBUS_ILIM_2500MA 0x5 /* 2500mA */
83 #define CHRG_VBUS_ILIM_3000MA 0x6 /* 3000mA */
84 #define CHRG_VBUS_ILIM_3500MA 0x7 /* 3500mA */
85 #define CHRG_VBUS_ILIM_4000MA 0x8 /* 4000mA */
87 #define CHRG_VLTFC_0C 0xA5 /* 0 DegC */
88 #define CHRG_VHTFC_45C 0x1F /* 45 DegC */
90 #define FG_CNTL_OCV_ADJ_EN BIT(3)
92 #define CV_4100MV 4100 /* 4100mV */
93 #define CV_4150MV 4150 /* 4150mV */
94 #define CV_4200MV 4200 /* 4200mV */
95 #define CV_4350MV 4350 /* 4350mV */
97 #define AXP288_EXTCON_DEV_NAME "axp288_extcon"
98 #define USB_HOST_EXTCON_HID "INT3496"
99 #define USB_HOST_EXTCON_NAME "INT3496:00"
114 struct axp288_chrg_info
{
115 struct platform_device
*pdev
;
116 struct regmap
*regmap
;
117 struct regmap_irq_chip_data
*regmap_irqc
;
118 int irq
[CHRG_INTR_END
];
119 struct power_supply
*psy_usb
;
123 struct work_struct work
;
124 struct extcon_dev
*cable
;
125 struct notifier_block id_nb
;
129 /* SDP/CDP/DCP USB charging cable notifications */
131 struct extcon_dev
*edev
;
132 struct notifier_block nb
;
133 struct work_struct work
;
142 static inline int axp288_charger_set_cc(struct axp288_chrg_info
*info
, int cc
)
147 if (cc
< CHRG_CCCV_CC_OFFSET
)
148 cc
= CHRG_CCCV_CC_OFFSET
;
149 else if (cc
> info
->max_cc
)
152 reg_val
= (cc
- CHRG_CCCV_CC_OFFSET
) / CHRG_CCCV_CC_LSB_RES
;
153 cc
= (reg_val
* CHRG_CCCV_CC_LSB_RES
) + CHRG_CCCV_CC_OFFSET
;
154 reg_val
= reg_val
<< CHRG_CCCV_CC_BIT_POS
;
156 ret
= regmap_update_bits(info
->regmap
,
158 CHRG_CCCV_CC_MASK
, reg_val
);
165 static inline int axp288_charger_set_cv(struct axp288_chrg_info
*info
, int cv
)
170 if (cv
<= CV_4100MV
) {
171 reg_val
= CHRG_CCCV_CV_4100MV
;
173 } else if (cv
<= CV_4150MV
) {
174 reg_val
= CHRG_CCCV_CV_4150MV
;
176 } else if (cv
<= CV_4200MV
) {
177 reg_val
= CHRG_CCCV_CV_4200MV
;
180 reg_val
= CHRG_CCCV_CV_4350MV
;
184 reg_val
= reg_val
<< CHRG_CCCV_CV_BIT_POS
;
186 ret
= regmap_update_bits(info
->regmap
,
188 CHRG_CCCV_CV_MASK
, reg_val
);
196 static int axp288_charger_get_vbus_inlmt(struct axp288_chrg_info
*info
)
201 ret
= regmap_read(info
->regmap
, AXP20X_CHRG_BAK_CTRL
, &val
);
205 val
>>= CHRG_VBUS_ILIM_BIT_POS
;
207 case CHRG_VBUS_ILIM_100MA
:
209 case CHRG_VBUS_ILIM_500MA
:
211 case CHRG_VBUS_ILIM_900MA
:
213 case CHRG_VBUS_ILIM_1500MA
:
215 case CHRG_VBUS_ILIM_2000MA
:
217 case CHRG_VBUS_ILIM_2500MA
:
219 case CHRG_VBUS_ILIM_3000MA
:
221 case CHRG_VBUS_ILIM_3500MA
:
224 /* All b1xxx values map to 4000 mA */
229 static inline int axp288_charger_set_vbus_inlmt(struct axp288_chrg_info
*info
,
235 if (inlmt
>= 4000000)
236 reg_val
= CHRG_VBUS_ILIM_4000MA
<< CHRG_VBUS_ILIM_BIT_POS
;
237 else if (inlmt
>= 3500000)
238 reg_val
= CHRG_VBUS_ILIM_3500MA
<< CHRG_VBUS_ILIM_BIT_POS
;
239 else if (inlmt
>= 3000000)
240 reg_val
= CHRG_VBUS_ILIM_3000MA
<< CHRG_VBUS_ILIM_BIT_POS
;
241 else if (inlmt
>= 2500000)
242 reg_val
= CHRG_VBUS_ILIM_2500MA
<< CHRG_VBUS_ILIM_BIT_POS
;
243 else if (inlmt
>= 2000000)
244 reg_val
= CHRG_VBUS_ILIM_2000MA
<< CHRG_VBUS_ILIM_BIT_POS
;
245 else if (inlmt
>= 1500000)
246 reg_val
= CHRG_VBUS_ILIM_1500MA
<< CHRG_VBUS_ILIM_BIT_POS
;
247 else if (inlmt
>= 900000)
248 reg_val
= CHRG_VBUS_ILIM_900MA
<< CHRG_VBUS_ILIM_BIT_POS
;
249 else if (inlmt
>= 500000)
250 reg_val
= CHRG_VBUS_ILIM_500MA
<< CHRG_VBUS_ILIM_BIT_POS
;
252 reg_val
= CHRG_VBUS_ILIM_100MA
<< CHRG_VBUS_ILIM_BIT_POS
;
254 ret
= regmap_update_bits(info
->regmap
, AXP20X_CHRG_BAK_CTRL
,
255 CHRG_VBUS_ILIM_MASK
, reg_val
);
257 dev_err(&info
->pdev
->dev
, "charger BAK control %d\n", ret
);
262 static int axp288_charger_vbus_path_select(struct axp288_chrg_info
*info
,
268 ret
= regmap_update_bits(info
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
,
269 VBUS_ISPOUT_VBUS_PATH_DIS
, 0);
271 ret
= regmap_update_bits(info
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
,
272 VBUS_ISPOUT_VBUS_PATH_DIS
, VBUS_ISPOUT_VBUS_PATH_DIS
);
275 dev_err(&info
->pdev
->dev
, "axp288 vbus path select %d\n", ret
);
280 static int axp288_charger_enable_charger(struct axp288_chrg_info
*info
,
286 ret
= regmap_update_bits(info
->regmap
, AXP20X_CHRG_CTRL1
,
287 CHRG_CCCV_CHG_EN
, CHRG_CCCV_CHG_EN
);
289 ret
= regmap_update_bits(info
->regmap
, AXP20X_CHRG_CTRL1
,
290 CHRG_CCCV_CHG_EN
, 0);
292 dev_err(&info
->pdev
->dev
, "axp288 enable charger %d\n", ret
);
297 static int axp288_charger_is_present(struct axp288_chrg_info
*info
)
299 int ret
, present
= 0;
302 ret
= regmap_read(info
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
306 if (val
& PS_STAT_VBUS_PRESENT
)
311 static int axp288_charger_is_online(struct axp288_chrg_info
*info
)
316 ret
= regmap_read(info
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
320 if (val
& PS_STAT_VBUS_VALID
)
325 static int axp288_get_charger_health(struct axp288_chrg_info
*info
)
327 int ret
, pwr_stat
, chrg_stat
;
328 int health
= POWER_SUPPLY_HEALTH_UNKNOWN
;
331 ret
= regmap_read(info
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
332 if ((ret
< 0) || !(val
& PS_STAT_VBUS_PRESENT
))
333 goto health_read_fail
;
337 ret
= regmap_read(info
->regmap
, AXP20X_PWR_OP_MODE
, &val
);
339 goto health_read_fail
;
343 if (!(pwr_stat
& PS_STAT_VBUS_VALID
))
344 health
= POWER_SUPPLY_HEALTH_DEAD
;
345 else if (chrg_stat
& CHRG_STAT_PMIC_OTP
)
346 health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
347 else if (chrg_stat
& CHRG_STAT_BAT_SAFE_MODE
)
348 health
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
350 health
= POWER_SUPPLY_HEALTH_GOOD
;
356 static int axp288_charger_usb_set_property(struct power_supply
*psy
,
357 enum power_supply_property psp
,
358 const union power_supply_propval
*val
)
360 struct axp288_chrg_info
*info
= power_supply_get_drvdata(psy
);
365 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
366 scaled_val
= min(val
->intval
, info
->max_cc
);
367 scaled_val
= DIV_ROUND_CLOSEST(scaled_val
, 1000);
368 ret
= axp288_charger_set_cc(info
, scaled_val
);
370 dev_warn(&info
->pdev
->dev
, "set charge current failed\n");
372 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
373 scaled_val
= min(val
->intval
, info
->max_cv
);
374 scaled_val
= DIV_ROUND_CLOSEST(scaled_val
, 1000);
375 ret
= axp288_charger_set_cv(info
, scaled_val
);
377 dev_warn(&info
->pdev
->dev
, "set charge voltage failed\n");
379 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
380 ret
= axp288_charger_set_vbus_inlmt(info
, val
->intval
);
382 dev_warn(&info
->pdev
->dev
, "set input current limit failed\n");
391 static int axp288_charger_usb_get_property(struct power_supply
*psy
,
392 enum power_supply_property psp
,
393 union power_supply_propval
*val
)
395 struct axp288_chrg_info
*info
= power_supply_get_drvdata(psy
);
399 case POWER_SUPPLY_PROP_PRESENT
:
400 /* Check for OTG case first */
401 if (info
->otg
.id_short
) {
405 ret
= axp288_charger_is_present(info
);
410 case POWER_SUPPLY_PROP_ONLINE
:
411 /* Check for OTG case first */
412 if (info
->otg
.id_short
) {
416 ret
= axp288_charger_is_online(info
);
421 case POWER_SUPPLY_PROP_HEALTH
:
422 val
->intval
= axp288_get_charger_health(info
);
424 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
425 val
->intval
= info
->cc
* 1000;
427 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
428 val
->intval
= info
->max_cc
* 1000;
430 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
431 val
->intval
= info
->cv
* 1000;
433 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
434 val
->intval
= info
->max_cv
* 1000;
436 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
437 ret
= axp288_charger_get_vbus_inlmt(info
);
449 static int axp288_charger_property_is_writeable(struct power_supply
*psy
,
450 enum power_supply_property psp
)
455 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
456 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
457 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
467 static enum power_supply_property axp288_usb_props
[] = {
468 POWER_SUPPLY_PROP_PRESENT
,
469 POWER_SUPPLY_PROP_ONLINE
,
470 POWER_SUPPLY_PROP_TYPE
,
471 POWER_SUPPLY_PROP_HEALTH
,
472 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
473 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
474 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
475 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
476 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
479 static const struct power_supply_desc axp288_charger_desc
= {
480 .name
= "axp288_charger",
481 .type
= POWER_SUPPLY_TYPE_USB
,
482 .properties
= axp288_usb_props
,
483 .num_properties
= ARRAY_SIZE(axp288_usb_props
),
484 .get_property
= axp288_charger_usb_get_property
,
485 .set_property
= axp288_charger_usb_set_property
,
486 .property_is_writeable
= axp288_charger_property_is_writeable
,
489 static irqreturn_t
axp288_charger_irq_thread_handler(int irq
, void *dev
)
491 struct axp288_chrg_info
*info
= dev
;
494 for (i
= 0; i
< CHRG_INTR_END
; i
++) {
495 if (info
->irq
[i
] == irq
)
499 if (i
>= CHRG_INTR_END
) {
500 dev_warn(&info
->pdev
->dev
, "spurious interrupt!!\n");
506 dev_dbg(&info
->pdev
->dev
, "VBUS Over Voltage INTR\n");
508 case CHARGE_DONE_IRQ
:
509 dev_dbg(&info
->pdev
->dev
, "Charging Done INTR\n");
511 case CHARGE_CHARGING_IRQ
:
512 dev_dbg(&info
->pdev
->dev
, "Start Charging IRQ\n");
514 case BAT_SAFE_QUIT_IRQ
:
515 dev_dbg(&info
->pdev
->dev
,
516 "Quit Safe Mode(restart timer) Charging IRQ\n");
518 case BAT_SAFE_ENTER_IRQ
:
519 dev_dbg(&info
->pdev
->dev
,
520 "Enter Safe Mode(timer expire) Charging IRQ\n");
523 dev_dbg(&info
->pdev
->dev
,
524 "Quit Battery Under Temperature(CHRG) INTR\n");
527 dev_dbg(&info
->pdev
->dev
,
528 "Hit Battery Under Temperature(CHRG) INTR\n");
531 dev_dbg(&info
->pdev
->dev
,
532 "Quit Battery Over Temperature(CHRG) INTR\n");
535 dev_dbg(&info
->pdev
->dev
,
536 "Hit Battery Over Temperature(CHRG) INTR\n");
539 dev_warn(&info
->pdev
->dev
, "Spurious Interrupt!!!\n");
543 power_supply_changed(info
->psy_usb
);
548 static void axp288_charger_extcon_evt_worker(struct work_struct
*work
)
550 struct axp288_chrg_info
*info
=
551 container_of(work
, struct axp288_chrg_info
, cable
.work
);
552 int ret
, current_limit
;
553 struct extcon_dev
*edev
= info
->cable
.edev
;
556 ret
= regmap_read(info
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
558 dev_err(&info
->pdev
->dev
, "Error reading status (%d)\n", ret
);
562 /* Offline? Disable charging and bail */
563 if (!(val
& PS_STAT_VBUS_VALID
)) {
564 dev_dbg(&info
->pdev
->dev
, "USB charger disconnected\n");
565 axp288_charger_enable_charger(info
, false);
566 power_supply_changed(info
->psy_usb
);
570 /* Determine cable/charger type */
571 if (extcon_get_state(edev
, EXTCON_CHG_USB_SDP
) > 0) {
572 dev_dbg(&info
->pdev
->dev
, "USB SDP charger is connected\n");
573 current_limit
= 500000;
574 } else if (extcon_get_state(edev
, EXTCON_CHG_USB_CDP
) > 0) {
575 dev_dbg(&info
->pdev
->dev
, "USB CDP charger is connected\n");
576 current_limit
= 1500000;
577 } else if (extcon_get_state(edev
, EXTCON_CHG_USB_DCP
) > 0) {
578 dev_dbg(&info
->pdev
->dev
, "USB DCP charger is connected\n");
579 current_limit
= 2000000;
581 /* Charger type detection still in progress, bail. */
585 /* Set vbus current limit first, then enable charger */
586 ret
= axp288_charger_set_vbus_inlmt(info
, current_limit
);
588 axp288_charger_enable_charger(info
, true);
590 dev_err(&info
->pdev
->dev
,
591 "error setting current limit (%d)\n", ret
);
593 power_supply_changed(info
->psy_usb
);
596 static int axp288_charger_handle_cable_evt(struct notifier_block
*nb
,
597 unsigned long event
, void *param
)
599 struct axp288_chrg_info
*info
=
600 container_of(nb
, struct axp288_chrg_info
, cable
.nb
);
601 schedule_work(&info
->cable
.work
);
605 static void axp288_charger_otg_evt_worker(struct work_struct
*work
)
607 struct axp288_chrg_info
*info
=
608 container_of(work
, struct axp288_chrg_info
, otg
.work
);
609 struct extcon_dev
*edev
= info
->otg
.cable
;
610 int ret
, usb_host
= extcon_get_state(edev
, EXTCON_USB_HOST
);
612 dev_dbg(&info
->pdev
->dev
, "external connector USB-Host is %s\n",
613 usb_host
? "attached" : "detached");
616 * Set usb_id_short flag to avoid running charger detection logic
619 info
->otg
.id_short
= usb_host
;
621 /* Disable VBUS path before enabling the 5V boost */
622 ret
= axp288_charger_vbus_path_select(info
, !info
->otg
.id_short
);
624 dev_warn(&info
->pdev
->dev
, "vbus path disable failed\n");
627 static int axp288_charger_handle_otg_evt(struct notifier_block
*nb
,
628 unsigned long event
, void *param
)
630 struct axp288_chrg_info
*info
=
631 container_of(nb
, struct axp288_chrg_info
, otg
.id_nb
);
633 schedule_work(&info
->otg
.work
);
638 static int charger_init_hw_regs(struct axp288_chrg_info
*info
)
643 /* Program temperature thresholds */
644 ret
= regmap_write(info
->regmap
, AXP20X_V_LTF_CHRG
, CHRG_VLTFC_0C
);
646 dev_err(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
647 AXP20X_V_LTF_CHRG
, ret
);
651 ret
= regmap_write(info
->regmap
, AXP20X_V_HTF_CHRG
, CHRG_VHTFC_45C
);
653 dev_err(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
654 AXP20X_V_HTF_CHRG
, ret
);
658 /* Do not turn-off charger o/p after charge cycle ends */
659 ret
= regmap_update_bits(info
->regmap
,
661 CNTL2_CHG_OUT_TURNON
, CNTL2_CHG_OUT_TURNON
);
663 dev_err(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
664 AXP20X_CHRG_CTRL2
, ret
);
668 /* Setup ending condition for charging to be 10% of I(chrg) */
669 ret
= regmap_update_bits(info
->regmap
,
671 CHRG_CCCV_ITERM_20P
, 0);
673 dev_err(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
674 AXP20X_CHRG_CTRL1
, ret
);
678 /* Disable OCV-SOC curve calibration */
679 ret
= regmap_update_bits(info
->regmap
,
681 FG_CNTL_OCV_ADJ_EN
, 0);
683 dev_err(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
684 AXP20X_CC_CTRL
, ret
);
688 /* Read current charge voltage and current limit */
689 ret
= regmap_read(info
->regmap
, AXP20X_CHRG_CTRL1
, &val
);
691 dev_err(&info
->pdev
->dev
, "register(%x) read error(%d)\n",
692 AXP20X_CHRG_CTRL1
, ret
);
696 /* Determine charge voltage */
697 cv
= (val
& CHRG_CCCV_CV_MASK
) >> CHRG_CCCV_CV_BIT_POS
;
699 case CHRG_CCCV_CV_4100MV
:
700 info
->cv
= CV_4100MV
;
702 case CHRG_CCCV_CV_4150MV
:
703 info
->cv
= CV_4150MV
;
705 case CHRG_CCCV_CV_4200MV
:
706 info
->cv
= CV_4200MV
;
708 case CHRG_CCCV_CV_4350MV
:
709 info
->cv
= CV_4350MV
;
713 /* Determine charge current limit */
714 cc
= (val
& CHRG_CCCV_CC_MASK
) >> CHRG_CCCV_CC_BIT_POS
;
715 cc
= (cc
* CHRG_CCCV_CC_LSB_RES
) + CHRG_CCCV_CC_OFFSET
;
719 * Do not allow the user to configure higher settings then those
720 * set by the firmware
722 info
->max_cv
= info
->cv
;
723 info
->max_cc
= info
->cc
;
728 static void axp288_charger_cancel_work(void *data
)
730 struct axp288_chrg_info
*info
= data
;
732 cancel_work_sync(&info
->otg
.work
);
733 cancel_work_sync(&info
->cable
.work
);
736 static int axp288_charger_probe(struct platform_device
*pdev
)
739 struct axp288_chrg_info
*info
;
740 struct device
*dev
= &pdev
->dev
;
741 struct axp20x_dev
*axp20x
= dev_get_drvdata(pdev
->dev
.parent
);
742 struct power_supply_config charger_cfg
= {};
746 * On some devices the fuelgauge and charger parts of the axp288 are
747 * not used, check that the fuelgauge is enabled (CC_CTRL != 0).
749 ret
= regmap_read(axp20x
->regmap
, AXP20X_CC_CTRL
, &val
);
755 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
760 info
->regmap
= axp20x
->regmap
;
761 info
->regmap_irqc
= axp20x
->regmap_irqc
;
763 info
->cable
.edev
= extcon_get_extcon_dev(AXP288_EXTCON_DEV_NAME
);
764 if (info
->cable
.edev
== NULL
) {
765 dev_dbg(&pdev
->dev
, "%s is not ready, probe deferred\n",
766 AXP288_EXTCON_DEV_NAME
);
767 return -EPROBE_DEFER
;
770 if (acpi_dev_present(USB_HOST_EXTCON_HID
, NULL
, -1)) {
771 info
->otg
.cable
= extcon_get_extcon_dev(USB_HOST_EXTCON_NAME
);
772 if (info
->otg
.cable
== NULL
) {
773 dev_dbg(dev
, "EXTCON_USB_HOST is not ready, probe deferred\n");
774 return -EPROBE_DEFER
;
777 "Using " USB_HOST_EXTCON_HID
" extcon for usb-id\n");
780 platform_set_drvdata(pdev
, info
);
782 ret
= charger_init_hw_regs(info
);
786 /* Register with power supply class */
787 charger_cfg
.drv_data
= info
;
788 info
->psy_usb
= devm_power_supply_register(dev
, &axp288_charger_desc
,
790 if (IS_ERR(info
->psy_usb
)) {
791 ret
= PTR_ERR(info
->psy_usb
);
792 dev_err(dev
, "failed to register power supply: %d\n", ret
);
796 /* Cancel our work on cleanup, register this before the notifiers */
797 ret
= devm_add_action(dev
, axp288_charger_cancel_work
, info
);
801 /* Register for extcon notification */
802 INIT_WORK(&info
->cable
.work
, axp288_charger_extcon_evt_worker
);
803 info
->cable
.nb
.notifier_call
= axp288_charger_handle_cable_evt
;
804 ret
= devm_extcon_register_notifier_all(dev
, info
->cable
.edev
,
807 dev_err(dev
, "failed to register cable extcon notifier\n");
810 schedule_work(&info
->cable
.work
);
812 /* Register for OTG notification */
813 INIT_WORK(&info
->otg
.work
, axp288_charger_otg_evt_worker
);
814 info
->otg
.id_nb
.notifier_call
= axp288_charger_handle_otg_evt
;
815 if (info
->otg
.cable
) {
816 ret
= devm_extcon_register_notifier(&pdev
->dev
, info
->otg
.cable
,
817 EXTCON_USB_HOST
, &info
->otg
.id_nb
);
819 dev_err(dev
, "failed to register EXTCON_USB_HOST notifier\n");
822 schedule_work(&info
->otg
.work
);
825 /* Register charger interrupts */
826 for (i
= 0; i
< CHRG_INTR_END
; i
++) {
827 pirq
= platform_get_irq(info
->pdev
, i
);
829 dev_err(&pdev
->dev
, "Failed to get IRQ: %d\n", pirq
);
832 info
->irq
[i
] = regmap_irq_get_virq(info
->regmap_irqc
, pirq
);
833 if (info
->irq
[i
] < 0) {
834 dev_warn(&info
->pdev
->dev
,
835 "failed to get virtual interrupt=%d\n", pirq
);
838 ret
= devm_request_threaded_irq(&info
->pdev
->dev
, info
->irq
[i
],
839 NULL
, axp288_charger_irq_thread_handler
,
840 IRQF_ONESHOT
, info
->pdev
->name
, info
);
842 dev_err(&pdev
->dev
, "failed to request interrupt=%d\n",
851 static const struct platform_device_id axp288_charger_id_table
[] = {
852 { .name
= "axp288_charger" },
855 MODULE_DEVICE_TABLE(platform
, axp288_charger_id_table
);
857 static struct platform_driver axp288_charger_driver
= {
858 .probe
= axp288_charger_probe
,
859 .id_table
= axp288_charger_id_table
,
861 .name
= "axp288_charger",
865 module_platform_driver(axp288_charger_driver
);
867 MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
868 MODULE_DESCRIPTION("X-power AXP288 Charger Driver");
869 MODULE_LICENSE("GPL v2");