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>
32 #define PS_STAT_VBUS_TRIGGER (1 << 0)
33 #define PS_STAT_BAT_CHRG_DIR (1 << 2)
34 #define PS_STAT_VBAT_ABOVE_VHOLD (1 << 3)
35 #define PS_STAT_VBUS_VALID (1 << 4)
36 #define PS_STAT_VBUS_PRESENT (1 << 5)
38 #define CHRG_STAT_BAT_SAFE_MODE (1 << 3)
39 #define CHRG_STAT_BAT_VALID (1 << 4)
40 #define CHRG_STAT_BAT_PRESENT (1 << 5)
41 #define CHRG_STAT_CHARGING (1 << 6)
42 #define CHRG_STAT_PMIC_OTP (1 << 7)
44 #define VBUS_ISPOUT_CUR_LIM_MASK 0x03
45 #define VBUS_ISPOUT_CUR_LIM_BIT_POS 0
46 #define VBUS_ISPOUT_CUR_LIM_900MA 0x0 /* 900mA */
47 #define VBUS_ISPOUT_CUR_LIM_1500MA 0x1 /* 1500mA */
48 #define VBUS_ISPOUT_CUR_LIM_2000MA 0x2 /* 2000mA */
49 #define VBUS_ISPOUT_CUR_NO_LIM 0x3 /* 2500mA */
50 #define VBUS_ISPOUT_VHOLD_SET_MASK 0x31
51 #define VBUS_ISPOUT_VHOLD_SET_BIT_POS 0x3
52 #define VBUS_ISPOUT_VHOLD_SET_OFFSET 4000 /* 4000mV */
53 #define VBUS_ISPOUT_VHOLD_SET_LSB_RES 100 /* 100mV */
54 #define VBUS_ISPOUT_VHOLD_SET_4300MV 0x3 /* 4300mV */
55 #define VBUS_ISPOUT_VBUS_PATH_DIS (1 << 7)
57 #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */
58 #define CHRG_CCCV_CC_BIT_POS 0
59 #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */
60 #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */
61 #define CHRG_CCCV_ITERM_20P (1 << 4) /* 20% of CC */
62 #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */
63 #define CHRG_CCCV_CV_BIT_POS 5
64 #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */
65 #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */
66 #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */
67 #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */
68 #define CHRG_CCCV_CHG_EN (1 << 7)
70 #define CNTL2_CC_TIMEOUT_MASK 0x3 /* 2 bits */
71 #define CNTL2_CC_TIMEOUT_OFFSET 6 /* 6 Hrs */
72 #define CNTL2_CC_TIMEOUT_LSB_RES 2 /* 2 Hrs */
73 #define CNTL2_CC_TIMEOUT_12HRS 0x3 /* 12 Hrs */
74 #define CNTL2_CHGLED_TYPEB (1 << 4)
75 #define CNTL2_CHG_OUT_TURNON (1 << 5)
76 #define CNTL2_PC_TIMEOUT_MASK 0xC0
77 #define CNTL2_PC_TIMEOUT_OFFSET 40 /* 40 mins */
78 #define CNTL2_PC_TIMEOUT_LSB_RES 10 /* 10 mins */
79 #define CNTL2_PC_TIMEOUT_70MINS 0x3
81 #define CHRG_ILIM_TEMP_LOOP_EN (1 << 3)
82 #define CHRG_VBUS_ILIM_MASK 0xf0
83 #define CHRG_VBUS_ILIM_BIT_POS 4
84 #define CHRG_VBUS_ILIM_100MA 0x0 /* 100mA */
85 #define CHRG_VBUS_ILIM_500MA 0x1 /* 500mA */
86 #define CHRG_VBUS_ILIM_900MA 0x2 /* 900mA */
87 #define CHRG_VBUS_ILIM_1500MA 0x3 /* 1500mA */
88 #define CHRG_VBUS_ILIM_2000MA 0x4 /* 2000mA */
89 #define CHRG_VBUS_ILIM_2500MA 0x5 /* 2500mA */
90 #define CHRG_VBUS_ILIM_3000MA 0x6 /* 3000mA */
92 #define CHRG_VLTFC_0C 0xA5 /* 0 DegC */
93 #define CHRG_VHTFC_45C 0x1F /* 45 DegC */
95 #define FG_CNTL_OCV_ADJ_EN (1 << 3)
97 #define CV_4100MV 4100 /* 4100mV */
98 #define CV_4150MV 4150 /* 4150mV */
99 #define CV_4200MV 4200 /* 4200mV */
100 #define CV_4350MV 4350 /* 4350mV */
102 #define AXP288_EXTCON_DEV_NAME "axp288_extcon"
103 #define USB_HOST_EXTCON_HID "INT3496"
104 #define USB_HOST_EXTCON_NAME "INT3496:00"
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
;
128 struct work_struct work
;
129 struct extcon_dev
*cable
;
130 struct notifier_block id_nb
;
134 /* SDP/CDP/DCP USB charging cable notifications */
136 struct extcon_dev
*edev
;
137 struct notifier_block nb
;
138 struct work_struct work
;
147 static inline int axp288_charger_set_cc(struct axp288_chrg_info
*info
, int cc
)
152 if (cc
< CHRG_CCCV_CC_OFFSET
)
153 cc
= CHRG_CCCV_CC_OFFSET
;
154 else if (cc
> info
->max_cc
)
157 reg_val
= (cc
- CHRG_CCCV_CC_OFFSET
) / CHRG_CCCV_CC_LSB_RES
;
158 cc
= (reg_val
* CHRG_CCCV_CC_LSB_RES
) + CHRG_CCCV_CC_OFFSET
;
159 reg_val
= reg_val
<< CHRG_CCCV_CC_BIT_POS
;
161 ret
= regmap_update_bits(info
->regmap
,
163 CHRG_CCCV_CC_MASK
, reg_val
);
170 static inline int axp288_charger_set_cv(struct axp288_chrg_info
*info
, int cv
)
175 if (cv
<= CV_4100MV
) {
176 reg_val
= CHRG_CCCV_CV_4100MV
;
178 } else if (cv
<= CV_4150MV
) {
179 reg_val
= CHRG_CCCV_CV_4150MV
;
181 } else if (cv
<= CV_4200MV
) {
182 reg_val
= CHRG_CCCV_CV_4200MV
;
185 reg_val
= CHRG_CCCV_CV_4350MV
;
189 reg_val
= reg_val
<< CHRG_CCCV_CV_BIT_POS
;
191 ret
= regmap_update_bits(info
->regmap
,
193 CHRG_CCCV_CV_MASK
, reg_val
);
201 static int axp288_charger_get_vbus_inlmt(struct axp288_chrg_info
*info
)
206 ret
= regmap_read(info
->regmap
, AXP20X_CHRG_BAK_CTRL
, &val
);
210 val
>>= CHRG_VBUS_ILIM_BIT_POS
;
212 case CHRG_VBUS_ILIM_100MA
:
214 case CHRG_VBUS_ILIM_500MA
:
216 case CHRG_VBUS_ILIM_900MA
:
218 case CHRG_VBUS_ILIM_1500MA
:
220 case CHRG_VBUS_ILIM_2000MA
:
222 case CHRG_VBUS_ILIM_2500MA
:
224 case CHRG_VBUS_ILIM_3000MA
:
227 dev_warn(&info
->pdev
->dev
, "Unknown ilim reg val: %d\n", val
);
232 static inline int axp288_charger_set_vbus_inlmt(struct axp288_chrg_info
*info
,
238 if (inlmt
>= 3000000)
239 reg_val
= CHRG_VBUS_ILIM_3000MA
<< CHRG_VBUS_ILIM_BIT_POS
;
240 else if (inlmt
>= 2500000)
241 reg_val
= CHRG_VBUS_ILIM_2500MA
<< CHRG_VBUS_ILIM_BIT_POS
;
242 else if (inlmt
>= 2000000)
243 reg_val
= CHRG_VBUS_ILIM_2000MA
<< CHRG_VBUS_ILIM_BIT_POS
;
244 else if (inlmt
>= 1500000)
245 reg_val
= CHRG_VBUS_ILIM_1500MA
<< CHRG_VBUS_ILIM_BIT_POS
;
246 else if (inlmt
>= 900000)
247 reg_val
= CHRG_VBUS_ILIM_900MA
<< CHRG_VBUS_ILIM_BIT_POS
;
248 else if (inlmt
>= 500000)
249 reg_val
= CHRG_VBUS_ILIM_500MA
<< CHRG_VBUS_ILIM_BIT_POS
;
251 reg_val
= CHRG_VBUS_ILIM_100MA
<< CHRG_VBUS_ILIM_BIT_POS
;
253 ret
= regmap_update_bits(info
->regmap
, AXP20X_CHRG_BAK_CTRL
,
254 CHRG_VBUS_ILIM_MASK
, reg_val
);
256 dev_err(&info
->pdev
->dev
, "charger BAK control %d\n", ret
);
261 static int axp288_charger_vbus_path_select(struct axp288_chrg_info
*info
,
267 ret
= regmap_update_bits(info
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
,
268 VBUS_ISPOUT_VBUS_PATH_DIS
, 0);
270 ret
= regmap_update_bits(info
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
,
271 VBUS_ISPOUT_VBUS_PATH_DIS
, VBUS_ISPOUT_VBUS_PATH_DIS
);
274 dev_err(&info
->pdev
->dev
, "axp288 vbus path select %d\n", ret
);
279 static int axp288_charger_enable_charger(struct axp288_chrg_info
*info
,
285 ret
= regmap_update_bits(info
->regmap
, AXP20X_CHRG_CTRL1
,
286 CHRG_CCCV_CHG_EN
, CHRG_CCCV_CHG_EN
);
288 ret
= regmap_update_bits(info
->regmap
, AXP20X_CHRG_CTRL1
,
289 CHRG_CCCV_CHG_EN
, 0);
291 dev_err(&info
->pdev
->dev
, "axp288 enable charger %d\n", ret
);
296 static int axp288_charger_is_present(struct axp288_chrg_info
*info
)
298 int ret
, present
= 0;
301 ret
= regmap_read(info
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
305 if (val
& PS_STAT_VBUS_PRESENT
)
310 static int axp288_charger_is_online(struct axp288_chrg_info
*info
)
315 ret
= regmap_read(info
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
319 if (val
& PS_STAT_VBUS_VALID
)
324 static int axp288_get_charger_health(struct axp288_chrg_info
*info
)
326 int ret
, pwr_stat
, chrg_stat
;
327 int health
= POWER_SUPPLY_HEALTH_UNKNOWN
;
330 ret
= regmap_read(info
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
331 if ((ret
< 0) || !(val
& PS_STAT_VBUS_PRESENT
))
332 goto health_read_fail
;
336 ret
= regmap_read(info
->regmap
, AXP20X_PWR_OP_MODE
, &val
);
338 goto health_read_fail
;
342 if (!(pwr_stat
& PS_STAT_VBUS_VALID
))
343 health
= POWER_SUPPLY_HEALTH_DEAD
;
344 else if (chrg_stat
& CHRG_STAT_PMIC_OTP
)
345 health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
346 else if (chrg_stat
& CHRG_STAT_BAT_SAFE_MODE
)
347 health
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
349 health
= POWER_SUPPLY_HEALTH_GOOD
;
355 static int axp288_charger_usb_set_property(struct power_supply
*psy
,
356 enum power_supply_property psp
,
357 const union power_supply_propval
*val
)
359 struct axp288_chrg_info
*info
= power_supply_get_drvdata(psy
);
364 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
365 scaled_val
= min(val
->intval
, info
->max_cc
);
366 scaled_val
= DIV_ROUND_CLOSEST(scaled_val
, 1000);
367 ret
= axp288_charger_set_cc(info
, scaled_val
);
369 dev_warn(&info
->pdev
->dev
, "set charge current failed\n");
371 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
372 scaled_val
= min(val
->intval
, info
->max_cv
);
373 scaled_val
= DIV_ROUND_CLOSEST(scaled_val
, 1000);
374 ret
= axp288_charger_set_cv(info
, scaled_val
);
376 dev_warn(&info
->pdev
->dev
, "set charge voltage failed\n");
378 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
379 ret
= axp288_charger_set_vbus_inlmt(info
, val
->intval
);
381 dev_warn(&info
->pdev
->dev
, "set input current limit failed\n");
390 static int axp288_charger_usb_get_property(struct power_supply
*psy
,
391 enum power_supply_property psp
,
392 union power_supply_propval
*val
)
394 struct axp288_chrg_info
*info
= power_supply_get_drvdata(psy
);
398 case POWER_SUPPLY_PROP_PRESENT
:
399 /* Check for OTG case first */
400 if (info
->otg
.id_short
) {
404 ret
= axp288_charger_is_present(info
);
409 case POWER_SUPPLY_PROP_ONLINE
:
410 /* Check for OTG case first */
411 if (info
->otg
.id_short
) {
415 ret
= axp288_charger_is_online(info
);
420 case POWER_SUPPLY_PROP_HEALTH
:
421 val
->intval
= axp288_get_charger_health(info
);
423 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
424 val
->intval
= info
->cc
* 1000;
426 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
427 val
->intval
= info
->max_cc
* 1000;
429 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
430 val
->intval
= info
->cv
* 1000;
432 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
433 val
->intval
= info
->max_cv
* 1000;
435 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
436 ret
= axp288_charger_get_vbus_inlmt(info
);
448 static int axp288_charger_property_is_writeable(struct power_supply
*psy
,
449 enum power_supply_property psp
)
454 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
455 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
456 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
466 static enum power_supply_property axp288_usb_props
[] = {
467 POWER_SUPPLY_PROP_PRESENT
,
468 POWER_SUPPLY_PROP_ONLINE
,
469 POWER_SUPPLY_PROP_TYPE
,
470 POWER_SUPPLY_PROP_HEALTH
,
471 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
472 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
473 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
474 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
475 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
478 static const struct power_supply_desc axp288_charger_desc
= {
479 .name
= "axp288_charger",
480 .type
= POWER_SUPPLY_TYPE_USB
,
481 .properties
= axp288_usb_props
,
482 .num_properties
= ARRAY_SIZE(axp288_usb_props
),
483 .get_property
= axp288_charger_usb_get_property
,
484 .set_property
= axp288_charger_usb_set_property
,
485 .property_is_writeable
= axp288_charger_property_is_writeable
,
488 static irqreturn_t
axp288_charger_irq_thread_handler(int irq
, void *dev
)
490 struct axp288_chrg_info
*info
= dev
;
493 for (i
= 0; i
< CHRG_INTR_END
; i
++) {
494 if (info
->irq
[i
] == irq
)
498 if (i
>= CHRG_INTR_END
) {
499 dev_warn(&info
->pdev
->dev
, "spurious interrupt!!\n");
505 dev_dbg(&info
->pdev
->dev
, "VBUS Over Voltage INTR\n");
507 case CHARGE_DONE_IRQ
:
508 dev_dbg(&info
->pdev
->dev
, "Charging Done INTR\n");
510 case CHARGE_CHARGING_IRQ
:
511 dev_dbg(&info
->pdev
->dev
, "Start Charging IRQ\n");
513 case BAT_SAFE_QUIT_IRQ
:
514 dev_dbg(&info
->pdev
->dev
,
515 "Quit Safe Mode(restart timer) Charging IRQ\n");
517 case BAT_SAFE_ENTER_IRQ
:
518 dev_dbg(&info
->pdev
->dev
,
519 "Enter Safe Mode(timer expire) Charging IRQ\n");
522 dev_dbg(&info
->pdev
->dev
,
523 "Quit Battery Under Temperature(CHRG) INTR\n");
526 dev_dbg(&info
->pdev
->dev
,
527 "Hit Battery Under Temperature(CHRG) INTR\n");
530 dev_dbg(&info
->pdev
->dev
,
531 "Quit Battery Over Temperature(CHRG) INTR\n");
534 dev_dbg(&info
->pdev
->dev
,
535 "Hit Battery Over Temperature(CHRG) INTR\n");
538 dev_warn(&info
->pdev
->dev
, "Spurious Interrupt!!!\n");
542 power_supply_changed(info
->psy_usb
);
547 static void axp288_charger_extcon_evt_worker(struct work_struct
*work
)
549 struct axp288_chrg_info
*info
=
550 container_of(work
, struct axp288_chrg_info
, cable
.work
);
551 int ret
, current_limit
;
552 struct extcon_dev
*edev
= info
->cable
.edev
;
555 ret
= regmap_read(info
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
557 dev_err(&info
->pdev
->dev
, "Error reading status (%d)\n", ret
);
561 /* Offline? Disable charging and bail */
562 if (!(val
& PS_STAT_VBUS_VALID
)) {
563 dev_dbg(&info
->pdev
->dev
, "USB charger disconnected\n");
564 axp288_charger_enable_charger(info
, false);
565 power_supply_changed(info
->psy_usb
);
569 /* Determine cable/charger type */
570 if (extcon_get_state(edev
, EXTCON_CHG_USB_SDP
) > 0) {
571 dev_dbg(&info
->pdev
->dev
, "USB SDP charger is connected\n");
572 current_limit
= 500000;
573 } else if (extcon_get_state(edev
, EXTCON_CHG_USB_CDP
) > 0) {
574 dev_dbg(&info
->pdev
->dev
, "USB CDP charger is connected\n");
575 current_limit
= 1500000;
576 } else if (extcon_get_state(edev
, EXTCON_CHG_USB_DCP
) > 0) {
577 dev_dbg(&info
->pdev
->dev
, "USB DCP charger is connected\n");
578 current_limit
= 2000000;
580 /* Charger type detection still in progress, bail. */
584 /* Set vbus current limit first, then enable charger */
585 ret
= axp288_charger_set_vbus_inlmt(info
, current_limit
);
587 axp288_charger_enable_charger(info
, true);
589 dev_err(&info
->pdev
->dev
,
590 "error setting current limit (%d)\n", ret
);
592 power_supply_changed(info
->psy_usb
);
595 static int axp288_charger_handle_cable_evt(struct notifier_block
*nb
,
596 unsigned long event
, void *param
)
598 struct axp288_chrg_info
*info
=
599 container_of(nb
, struct axp288_chrg_info
, cable
.nb
);
600 schedule_work(&info
->cable
.work
);
604 static void axp288_charger_otg_evt_worker(struct work_struct
*work
)
606 struct axp288_chrg_info
*info
=
607 container_of(work
, struct axp288_chrg_info
, otg
.work
);
608 struct extcon_dev
*edev
= info
->otg
.cable
;
609 int ret
, usb_host
= extcon_get_state(edev
, EXTCON_USB_HOST
);
611 dev_dbg(&info
->pdev
->dev
, "external connector USB-Host is %s\n",
612 usb_host
? "attached" : "detached");
615 * Set usb_id_short flag to avoid running charger detection logic
618 info
->otg
.id_short
= usb_host
;
620 /* Disable VBUS path before enabling the 5V boost */
621 ret
= axp288_charger_vbus_path_select(info
, !info
->otg
.id_short
);
623 dev_warn(&info
->pdev
->dev
, "vbus path disable failed\n");
626 static int axp288_charger_handle_otg_evt(struct notifier_block
*nb
,
627 unsigned long event
, void *param
)
629 struct axp288_chrg_info
*info
=
630 container_of(nb
, struct axp288_chrg_info
, otg
.id_nb
);
632 schedule_work(&info
->otg
.work
);
637 static int charger_init_hw_regs(struct axp288_chrg_info
*info
)
642 /* Program temperature thresholds */
643 ret
= regmap_write(info
->regmap
, AXP20X_V_LTF_CHRG
, CHRG_VLTFC_0C
);
645 dev_err(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
646 AXP20X_V_LTF_CHRG
, ret
);
650 ret
= regmap_write(info
->regmap
, AXP20X_V_HTF_CHRG
, CHRG_VHTFC_45C
);
652 dev_err(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
653 AXP20X_V_HTF_CHRG
, ret
);
657 /* Do not turn-off charger o/p after charge cycle ends */
658 ret
= regmap_update_bits(info
->regmap
,
660 CNTL2_CHG_OUT_TURNON
, CNTL2_CHG_OUT_TURNON
);
662 dev_err(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
663 AXP20X_CHRG_CTRL2
, ret
);
667 /* Setup ending condition for charging to be 10% of I(chrg) */
668 ret
= regmap_update_bits(info
->regmap
,
670 CHRG_CCCV_ITERM_20P
, 0);
672 dev_err(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
673 AXP20X_CHRG_CTRL1
, ret
);
677 /* Disable OCV-SOC curve calibration */
678 ret
= regmap_update_bits(info
->regmap
,
680 FG_CNTL_OCV_ADJ_EN
, 0);
682 dev_err(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
683 AXP20X_CC_CTRL
, ret
);
687 /* Read current charge voltage and current limit */
688 ret
= regmap_read(info
->regmap
, AXP20X_CHRG_CTRL1
, &val
);
690 dev_err(&info
->pdev
->dev
, "register(%x) read error(%d)\n",
691 AXP20X_CHRG_CTRL1
, ret
);
695 /* Determine charge voltage */
696 cv
= (val
& CHRG_CCCV_CV_MASK
) >> CHRG_CCCV_CV_BIT_POS
;
698 case CHRG_CCCV_CV_4100MV
:
699 info
->cv
= CV_4100MV
;
701 case CHRG_CCCV_CV_4150MV
:
702 info
->cv
= CV_4150MV
;
704 case CHRG_CCCV_CV_4200MV
:
705 info
->cv
= CV_4200MV
;
707 case CHRG_CCCV_CV_4350MV
:
708 info
->cv
= CV_4350MV
;
712 /* Determine charge current limit */
713 cc
= (ret
& CHRG_CCCV_CC_MASK
) >> CHRG_CCCV_CC_BIT_POS
;
714 cc
= (cc
* CHRG_CCCV_CC_LSB_RES
) + CHRG_CCCV_CC_OFFSET
;
718 * Do not allow the user to configure higher settings then those
719 * set by the firmware
721 info
->max_cv
= info
->cv
;
722 info
->max_cc
= info
->cc
;
727 static void axp288_charger_cancel_work(void *data
)
729 struct axp288_chrg_info
*info
= data
;
731 cancel_work_sync(&info
->otg
.work
);
732 cancel_work_sync(&info
->cable
.work
);
735 static int axp288_charger_probe(struct platform_device
*pdev
)
738 struct axp288_chrg_info
*info
;
739 struct device
*dev
= &pdev
->dev
;
740 struct axp20x_dev
*axp20x
= dev_get_drvdata(pdev
->dev
.parent
);
741 struct power_supply_config charger_cfg
= {};
742 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
747 info
->regmap
= axp20x
->regmap
;
748 info
->regmap_irqc
= axp20x
->regmap_irqc
;
750 info
->cable
.edev
= extcon_get_extcon_dev(AXP288_EXTCON_DEV_NAME
);
751 if (info
->cable
.edev
== NULL
) {
752 dev_dbg(&pdev
->dev
, "%s is not ready, probe deferred\n",
753 AXP288_EXTCON_DEV_NAME
);
754 return -EPROBE_DEFER
;
757 if (acpi_dev_present(USB_HOST_EXTCON_HID
, NULL
, -1)) {
758 info
->otg
.cable
= extcon_get_extcon_dev(USB_HOST_EXTCON_NAME
);
759 if (info
->otg
.cable
== NULL
) {
760 dev_dbg(dev
, "EXTCON_USB_HOST is not ready, probe deferred\n");
761 return -EPROBE_DEFER
;
764 "Using " USB_HOST_EXTCON_HID
" extcon for usb-id\n");
767 platform_set_drvdata(pdev
, info
);
769 ret
= charger_init_hw_regs(info
);
773 /* Register with power supply class */
774 charger_cfg
.drv_data
= info
;
775 info
->psy_usb
= devm_power_supply_register(dev
, &axp288_charger_desc
,
777 if (IS_ERR(info
->psy_usb
)) {
778 ret
= PTR_ERR(info
->psy_usb
);
779 dev_err(dev
, "failed to register power supply: %d\n", ret
);
783 /* Cancel our work on cleanup, register this before the notifiers */
784 ret
= devm_add_action(dev
, axp288_charger_cancel_work
, info
);
788 /* Register for extcon notification */
789 INIT_WORK(&info
->cable
.work
, axp288_charger_extcon_evt_worker
);
790 info
->cable
.nb
.notifier_call
= axp288_charger_handle_cable_evt
;
791 ret
= devm_extcon_register_notifier_all(dev
, info
->cable
.edev
,
794 dev_err(dev
, "failed to register cable extcon notifier\n");
797 schedule_work(&info
->cable
.work
);
799 /* Register for OTG notification */
800 INIT_WORK(&info
->otg
.work
, axp288_charger_otg_evt_worker
);
801 info
->otg
.id_nb
.notifier_call
= axp288_charger_handle_otg_evt
;
802 if (info
->otg
.cable
) {
803 ret
= devm_extcon_register_notifier(&pdev
->dev
, info
->otg
.cable
,
804 EXTCON_USB_HOST
, &info
->otg
.id_nb
);
806 dev_err(dev
, "failed to register EXTCON_USB_HOST notifier\n");
809 schedule_work(&info
->otg
.work
);
812 /* Register charger interrupts */
813 for (i
= 0; i
< CHRG_INTR_END
; i
++) {
814 pirq
= platform_get_irq(info
->pdev
, i
);
815 info
->irq
[i
] = regmap_irq_get_virq(info
->regmap_irqc
, pirq
);
816 if (info
->irq
[i
] < 0) {
817 dev_warn(&info
->pdev
->dev
,
818 "failed to get virtual interrupt=%d\n", pirq
);
821 ret
= devm_request_threaded_irq(&info
->pdev
->dev
, info
->irq
[i
],
822 NULL
, axp288_charger_irq_thread_handler
,
823 IRQF_ONESHOT
, info
->pdev
->name
, info
);
825 dev_err(&pdev
->dev
, "failed to request interrupt=%d\n",
834 static const struct platform_device_id axp288_charger_id_table
[] = {
835 { .name
= "axp288_charger" },
838 MODULE_DEVICE_TABLE(platform
, axp288_charger_id_table
);
840 static struct platform_driver axp288_charger_driver
= {
841 .probe
= axp288_charger_probe
,
842 .id_table
= axp288_charger_id_table
,
844 .name
= "axp288_charger",
848 module_platform_driver(axp288_charger_driver
);
850 MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
851 MODULE_DESCRIPTION("X-power AXP288 Charger Driver");
852 MODULE_LICENSE("GPL v2");