2 * axp288_charger.c - X-power AXP288 PMIC Charger driver
4 * Copyright (C) 2014 Intel Corporation
5 * Author: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/regmap.h>
20 #include <linux/workqueue.h>
21 #include <linux/delay.h>
22 #include <linux/platform_device.h>
23 #include <linux/usb/otg.h>
24 #include <linux/notifier.h>
25 #include <linux/power_supply.h>
26 #include <linux/property.h>
27 #include <linux/mfd/axp20x.h>
28 #include <linux/extcon.h>
30 #define PS_STAT_VBUS_TRIGGER (1 << 0)
31 #define PS_STAT_BAT_CHRG_DIR (1 << 2)
32 #define PS_STAT_VBAT_ABOVE_VHOLD (1 << 3)
33 #define PS_STAT_VBUS_VALID (1 << 4)
34 #define PS_STAT_VBUS_PRESENT (1 << 5)
36 #define CHRG_STAT_BAT_SAFE_MODE (1 << 3)
37 #define CHRG_STAT_BAT_VALID (1 << 4)
38 #define CHRG_STAT_BAT_PRESENT (1 << 5)
39 #define CHRG_STAT_CHARGING (1 << 6)
40 #define CHRG_STAT_PMIC_OTP (1 << 7)
42 #define VBUS_ISPOUT_CUR_LIM_MASK 0x03
43 #define VBUS_ISPOUT_CUR_LIM_BIT_POS 0
44 #define VBUS_ISPOUT_CUR_LIM_900MA 0x0 /* 900mA */
45 #define VBUS_ISPOUT_CUR_LIM_1500MA 0x1 /* 1500mA */
46 #define VBUS_ISPOUT_CUR_LIM_2000MA 0x2 /* 2000mA */
47 #define VBUS_ISPOUT_CUR_NO_LIM 0x3 /* 2500mA */
48 #define VBUS_ISPOUT_VHOLD_SET_MASK 0x31
49 #define VBUS_ISPOUT_VHOLD_SET_BIT_POS 0x3
50 #define VBUS_ISPOUT_VHOLD_SET_OFFSET 4000 /* 4000mV */
51 #define VBUS_ISPOUT_VHOLD_SET_LSB_RES 100 /* 100mV */
52 #define VBUS_ISPOUT_VHOLD_SET_4300MV 0x3 /* 4300mV */
53 #define VBUS_ISPOUT_VBUS_PATH_DIS (1 << 7)
55 #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */
56 #define CHRG_CCCV_CC_BIT_POS 0
57 #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */
58 #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */
59 #define CHRG_CCCV_ITERM_20P (1 << 4) /* 20% of CC */
60 #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */
61 #define CHRG_CCCV_CV_BIT_POS 5
62 #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */
63 #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */
64 #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */
65 #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */
66 #define CHRG_CCCV_CHG_EN (1 << 7)
68 #define CNTL2_CC_TIMEOUT_MASK 0x3 /* 2 bits */
69 #define CNTL2_CC_TIMEOUT_OFFSET 6 /* 6 Hrs */
70 #define CNTL2_CC_TIMEOUT_LSB_RES 2 /* 2 Hrs */
71 #define CNTL2_CC_TIMEOUT_12HRS 0x3 /* 12 Hrs */
72 #define CNTL2_CHGLED_TYPEB (1 << 4)
73 #define CNTL2_CHG_OUT_TURNON (1 << 5)
74 #define CNTL2_PC_TIMEOUT_MASK 0xC0
75 #define CNTL2_PC_TIMEOUT_OFFSET 40 /* 40 mins */
76 #define CNTL2_PC_TIMEOUT_LSB_RES 10 /* 10 mins */
77 #define CNTL2_PC_TIMEOUT_70MINS 0x3
79 #define CHRG_ILIM_TEMP_LOOP_EN (1 << 3)
80 #define CHRG_VBUS_ILIM_MASK 0xf0
81 #define CHRG_VBUS_ILIM_BIT_POS 4
82 #define CHRG_VBUS_ILIM_100MA 0x0 /* 100mA */
83 #define CHRG_VBUS_ILIM_500MA 0x1 /* 500mA */
84 #define CHRG_VBUS_ILIM_900MA 0x2 /* 900mA */
85 #define CHRG_VBUS_ILIM_1500MA 0x3 /* 1500mA */
86 #define CHRG_VBUS_ILIM_2000MA 0x4 /* 2000mA */
87 #define CHRG_VBUS_ILIM_2500MA 0x5 /* 2500mA */
88 #define CHRG_VBUS_ILIM_3000MA 0x6 /* 3000mA */
90 #define CHRG_VLTFC_0C 0xA5 /* 0 DegC */
91 #define CHRG_VHTFC_45C 0x1F /* 45 DegC */
93 #define BAT_IRQ_CFG_CHRG_DONE (1 << 2)
94 #define BAT_IRQ_CFG_CHRG_START (1 << 3)
95 #define BAT_IRQ_CFG_BAT_SAFE_EXIT (1 << 4)
96 #define BAT_IRQ_CFG_BAT_SAFE_ENTER (1 << 5)
97 #define BAT_IRQ_CFG_BAT_DISCON (1 << 6)
98 #define BAT_IRQ_CFG_BAT_CONN (1 << 7)
99 #define BAT_IRQ_CFG_BAT_MASK 0xFC
101 #define TEMP_IRQ_CFG_QCBTU (1 << 4)
102 #define TEMP_IRQ_CFG_CBTU (1 << 5)
103 #define TEMP_IRQ_CFG_QCBTO (1 << 6)
104 #define TEMP_IRQ_CFG_CBTO (1 << 7)
105 #define TEMP_IRQ_CFG_MASK 0xF0
107 #define FG_CNTL_OCV_ADJ_EN (1 << 3)
109 #define CV_4100MV 4100 /* 4100mV */
110 #define CV_4150MV 4150 /* 4150mV */
111 #define CV_4200MV 4200 /* 4200mV */
112 #define CV_4350MV 4350 /* 4350mV */
114 #define CC_200MA 200 /* 200mA */
115 #define CC_600MA 600 /* 600mA */
116 #define CC_800MA 800 /* 800mA */
117 #define CC_1000MA 1000 /* 1000mA */
118 #define CC_1600MA 1600 /* 1600mA */
119 #define CC_2000MA 2000 /* 2000mA */
121 #define ILIM_100MA 100 /* 100mA */
122 #define ILIM_500MA 500 /* 500mA */
123 #define ILIM_900MA 900 /* 900mA */
124 #define ILIM_1500MA 1500 /* 1500mA */
125 #define ILIM_2000MA 2000 /* 2000mA */
126 #define ILIM_2500MA 2500 /* 2500mA */
127 #define ILIM_3000MA 3000 /* 3000mA */
129 #define AXP288_EXTCON_DEV_NAME "axp288_extcon"
144 struct axp288_chrg_info
{
145 struct platform_device
*pdev
;
146 struct axp20x_chrg_pdata
*pdata
;
147 struct regmap
*regmap
;
148 struct regmap_irq_chip_data
*regmap_irqc
;
149 int irq
[CHRG_INTR_END
];
150 struct power_supply
*psy_usb
;
155 struct work_struct work
;
156 struct extcon_dev
*cable
;
157 struct notifier_block id_nb
;
161 /* SDP/CDP/DCP USB charging cable notifications */
163 struct extcon_dev
*edev
;
165 enum power_supply_type chg_type
;
166 struct notifier_block nb
;
167 struct work_struct work
;
179 bool is_charger_enabled
;
182 static inline int axp288_charger_set_cc(struct axp288_chrg_info
*info
, int cc
)
187 if (cc
< CHRG_CCCV_CC_OFFSET
)
188 cc
= CHRG_CCCV_CC_OFFSET
;
189 else if (cc
> info
->max_cc
)
192 reg_val
= (cc
- CHRG_CCCV_CC_OFFSET
) / CHRG_CCCV_CC_LSB_RES
;
193 cc
= (reg_val
* CHRG_CCCV_CC_LSB_RES
) + CHRG_CCCV_CC_OFFSET
;
194 reg_val
= reg_val
<< CHRG_CCCV_CC_BIT_POS
;
196 ret
= regmap_update_bits(info
->regmap
,
198 CHRG_CCCV_CC_MASK
, reg_val
);
205 static inline int axp288_charger_set_cv(struct axp288_chrg_info
*info
, int cv
)
210 if (cv
<= CV_4100MV
) {
211 reg_val
= CHRG_CCCV_CV_4100MV
;
213 } else if (cv
<= CV_4150MV
) {
214 reg_val
= CHRG_CCCV_CV_4150MV
;
216 } else if (cv
<= CV_4200MV
) {
217 reg_val
= CHRG_CCCV_CV_4200MV
;
220 reg_val
= CHRG_CCCV_CV_4350MV
;
224 reg_val
= reg_val
<< CHRG_CCCV_CV_BIT_POS
;
226 ret
= regmap_update_bits(info
->regmap
,
228 CHRG_CCCV_CV_MASK
, reg_val
);
236 static inline int axp288_charger_set_vbus_inlmt(struct axp288_chrg_info
*info
,
243 /* Read in limit register */
244 ret
= regmap_read(info
->regmap
, AXP20X_CHRG_BAK_CTRL
, &val
);
248 if (inlmt
<= ILIM_100MA
) {
249 reg_val
= CHRG_VBUS_ILIM_100MA
;
251 } else if (inlmt
<= ILIM_500MA
) {
252 reg_val
= CHRG_VBUS_ILIM_500MA
;
254 } else if (inlmt
<= ILIM_900MA
) {
255 reg_val
= CHRG_VBUS_ILIM_900MA
;
257 } else if (inlmt
<= ILIM_1500MA
) {
258 reg_val
= CHRG_VBUS_ILIM_1500MA
;
260 } else if (inlmt
<= ILIM_2000MA
) {
261 reg_val
= CHRG_VBUS_ILIM_2000MA
;
263 } else if (inlmt
<= ILIM_2500MA
) {
264 reg_val
= CHRG_VBUS_ILIM_2500MA
;
267 reg_val
= CHRG_VBUS_ILIM_3000MA
;
271 reg_val
= (val
& ~CHRG_VBUS_ILIM_MASK
)
272 | (reg_val
<< CHRG_VBUS_ILIM_BIT_POS
);
273 ret
= regmap_write(info
->regmap
, AXP20X_CHRG_BAK_CTRL
, reg_val
);
277 dev_err(&info
->pdev
->dev
, "charger BAK control %d\n", ret
);
284 static int axp288_charger_vbus_path_select(struct axp288_chrg_info
*info
,
290 ret
= regmap_update_bits(info
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
,
291 VBUS_ISPOUT_VBUS_PATH_DIS
, 0);
293 ret
= regmap_update_bits(info
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
,
294 VBUS_ISPOUT_VBUS_PATH_DIS
, VBUS_ISPOUT_VBUS_PATH_DIS
);
297 dev_err(&info
->pdev
->dev
, "axp288 vbus path select %d\n", ret
);
303 static int axp288_charger_enable_charger(struct axp288_chrg_info
*info
,
309 ret
= regmap_update_bits(info
->regmap
, AXP20X_CHRG_CTRL1
,
310 CHRG_CCCV_CHG_EN
, CHRG_CCCV_CHG_EN
);
312 ret
= regmap_update_bits(info
->regmap
, AXP20X_CHRG_CTRL1
,
313 CHRG_CCCV_CHG_EN
, 0);
315 dev_err(&info
->pdev
->dev
, "axp288 enable charger %d\n", ret
);
317 info
->is_charger_enabled
= enable
;
322 static int axp288_charger_is_present(struct axp288_chrg_info
*info
)
324 int ret
, present
= 0;
327 ret
= regmap_read(info
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
331 if (val
& PS_STAT_VBUS_PRESENT
)
336 static int axp288_charger_is_online(struct axp288_chrg_info
*info
)
341 ret
= regmap_read(info
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
345 if (val
& PS_STAT_VBUS_VALID
)
350 static int axp288_get_charger_health(struct axp288_chrg_info
*info
)
352 int ret
, pwr_stat
, chrg_stat
;
353 int health
= POWER_SUPPLY_HEALTH_UNKNOWN
;
356 ret
= regmap_read(info
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
357 if ((ret
< 0) || !(val
& PS_STAT_VBUS_PRESENT
))
358 goto health_read_fail
;
362 ret
= regmap_read(info
->regmap
, AXP20X_PWR_OP_MODE
, &val
);
364 goto health_read_fail
;
368 if (!(pwr_stat
& PS_STAT_VBUS_VALID
))
369 health
= POWER_SUPPLY_HEALTH_DEAD
;
370 else if (chrg_stat
& CHRG_STAT_PMIC_OTP
)
371 health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
372 else if (chrg_stat
& CHRG_STAT_BAT_SAFE_MODE
)
373 health
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
375 health
= POWER_SUPPLY_HEALTH_GOOD
;
381 static int axp288_charger_usb_set_property(struct power_supply
*psy
,
382 enum power_supply_property psp
,
383 const union power_supply_propval
*val
)
385 struct axp288_chrg_info
*info
= power_supply_get_drvdata(psy
);
389 mutex_lock(&info
->lock
);
392 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
393 scaled_val
= min(val
->intval
, info
->max_cc
);
394 scaled_val
= DIV_ROUND_CLOSEST(scaled_val
, 1000);
395 ret
= axp288_charger_set_cc(info
, scaled_val
);
397 dev_warn(&info
->pdev
->dev
, "set charge current failed\n");
399 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
400 scaled_val
= min(val
->intval
, info
->max_cv
);
401 scaled_val
= DIV_ROUND_CLOSEST(scaled_val
, 1000);
402 ret
= axp288_charger_set_cv(info
, scaled_val
);
404 dev_warn(&info
->pdev
->dev
, "set charge voltage failed\n");
410 mutex_unlock(&info
->lock
);
414 static int axp288_charger_usb_get_property(struct power_supply
*psy
,
415 enum power_supply_property psp
,
416 union power_supply_propval
*val
)
418 struct axp288_chrg_info
*info
= power_supply_get_drvdata(psy
);
421 mutex_lock(&info
->lock
);
424 case POWER_SUPPLY_PROP_PRESENT
:
425 /* Check for OTG case first */
426 if (info
->otg
.id_short
) {
430 ret
= axp288_charger_is_present(info
);
432 goto psy_get_prop_fail
;
434 val
->intval
= info
->present
;
436 case POWER_SUPPLY_PROP_ONLINE
:
437 /* Check for OTG case first */
438 if (info
->otg
.id_short
) {
442 ret
= axp288_charger_is_online(info
);
444 goto psy_get_prop_fail
;
446 val
->intval
= info
->online
;
448 case POWER_SUPPLY_PROP_HEALTH
:
449 val
->intval
= axp288_get_charger_health(info
);
451 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
452 val
->intval
= info
->cc
* 1000;
454 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
455 val
->intval
= info
->max_cc
* 1000;
457 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
458 val
->intval
= info
->cv
* 1000;
460 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
461 val
->intval
= info
->max_cv
* 1000;
463 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
:
464 val
->intval
= info
->inlmt
* 1000;
468 goto psy_get_prop_fail
;
472 mutex_unlock(&info
->lock
);
476 static int axp288_charger_property_is_writeable(struct power_supply
*psy
,
477 enum power_supply_property psp
)
482 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
483 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
493 static enum power_supply_property axp288_usb_props
[] = {
494 POWER_SUPPLY_PROP_PRESENT
,
495 POWER_SUPPLY_PROP_ONLINE
,
496 POWER_SUPPLY_PROP_TYPE
,
497 POWER_SUPPLY_PROP_HEALTH
,
498 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
499 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
500 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
501 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
502 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
,
505 static const struct power_supply_desc axp288_charger_desc
= {
506 .name
= "axp288_charger",
507 .type
= POWER_SUPPLY_TYPE_USB
,
508 .properties
= axp288_usb_props
,
509 .num_properties
= ARRAY_SIZE(axp288_usb_props
),
510 .get_property
= axp288_charger_usb_get_property
,
511 .set_property
= axp288_charger_usb_set_property
,
512 .property_is_writeable
= axp288_charger_property_is_writeable
,
515 static irqreturn_t
axp288_charger_irq_thread_handler(int irq
, void *dev
)
517 struct axp288_chrg_info
*info
= dev
;
520 for (i
= 0; i
< CHRG_INTR_END
; i
++) {
521 if (info
->irq
[i
] == irq
)
525 if (i
>= CHRG_INTR_END
) {
526 dev_warn(&info
->pdev
->dev
, "spurious interrupt!!\n");
532 dev_dbg(&info
->pdev
->dev
, "VBUS Over Voltage INTR\n");
534 case CHARGE_DONE_IRQ
:
535 dev_dbg(&info
->pdev
->dev
, "Charging Done INTR\n");
537 case CHARGE_CHARGING_IRQ
:
538 dev_dbg(&info
->pdev
->dev
, "Start Charging IRQ\n");
540 case BAT_SAFE_QUIT_IRQ
:
541 dev_dbg(&info
->pdev
->dev
,
542 "Quit Safe Mode(restart timer) Charging IRQ\n");
544 case BAT_SAFE_ENTER_IRQ
:
545 dev_dbg(&info
->pdev
->dev
,
546 "Enter Safe Mode(timer expire) Charging IRQ\n");
549 dev_dbg(&info
->pdev
->dev
,
550 "Quit Battery Under Temperature(CHRG) INTR\n");
553 dev_dbg(&info
->pdev
->dev
,
554 "Hit Battery Under Temperature(CHRG) INTR\n");
557 dev_dbg(&info
->pdev
->dev
,
558 "Quit Battery Over Temperature(CHRG) INTR\n");
561 dev_dbg(&info
->pdev
->dev
,
562 "Hit Battery Over Temperature(CHRG) INTR\n");
565 dev_warn(&info
->pdev
->dev
, "Spurious Interrupt!!!\n");
569 power_supply_changed(info
->psy_usb
);
574 static void axp288_charger_extcon_evt_worker(struct work_struct
*work
)
576 struct axp288_chrg_info
*info
=
577 container_of(work
, struct axp288_chrg_info
, cable
.work
);
578 int ret
, current_limit
;
579 bool changed
= false;
580 struct extcon_dev
*edev
= info
->cable
.edev
;
581 bool old_connected
= info
->cable
.connected
;
583 /* Determine cable/charger type */
584 if (extcon_get_cable_state_(edev
, EXTCON_CHG_USB_SDP
) > 0) {
585 dev_dbg(&info
->pdev
->dev
, "USB SDP charger is connected");
586 info
->cable
.connected
= true;
587 info
->cable
.chg_type
= POWER_SUPPLY_TYPE_USB
;
588 } else if (extcon_get_cable_state_(edev
, EXTCON_CHG_USB_CDP
) > 0) {
589 dev_dbg(&info
->pdev
->dev
, "USB CDP charger is connected");
590 info
->cable
.connected
= true;
591 info
->cable
.chg_type
= POWER_SUPPLY_TYPE_USB_CDP
;
592 } else if (extcon_get_cable_state_(edev
, EXTCON_CHG_USB_DCP
) > 0) {
593 dev_dbg(&info
->pdev
->dev
, "USB DCP charger is connected");
594 info
->cable
.connected
= true;
595 info
->cable
.chg_type
= POWER_SUPPLY_TYPE_USB_DCP
;
598 dev_dbg(&info
->pdev
->dev
, "USB charger disconnected");
599 info
->cable
.connected
= false;
600 info
->cable
.chg_type
= POWER_SUPPLY_TYPE_USB
;
603 /* Cable status changed */
604 if (old_connected
!= info
->cable
.connected
)
610 mutex_lock(&info
->lock
);
612 if (info
->is_charger_enabled
&& !info
->cable
.connected
) {
613 info
->enable_charger
= false;
614 ret
= axp288_charger_enable_charger(info
, info
->enable_charger
);
616 dev_err(&info
->pdev
->dev
,
617 "cannot disable charger (%d)", ret
);
619 } else if (!info
->is_charger_enabled
&& info
->cable
.connected
) {
620 switch (info
->cable
.chg_type
) {
621 case POWER_SUPPLY_TYPE_USB
:
622 current_limit
= ILIM_500MA
;
624 case POWER_SUPPLY_TYPE_USB_CDP
:
625 current_limit
= ILIM_1500MA
;
627 case POWER_SUPPLY_TYPE_USB_DCP
:
628 current_limit
= ILIM_2000MA
;
636 /* Set vbus current limit first, then enable charger */
637 ret
= axp288_charger_set_vbus_inlmt(info
, current_limit
);
639 dev_err(&info
->pdev
->dev
,
640 "error setting current limit (%d)", ret
);
642 info
->enable_charger
= (current_limit
> 0);
643 ret
= axp288_charger_enable_charger(info
,
644 info
->enable_charger
);
646 dev_err(&info
->pdev
->dev
,
647 "cannot enable charger (%d)", ret
);
652 info
->health
= axp288_get_charger_health(info
);
654 mutex_unlock(&info
->lock
);
657 power_supply_changed(info
->psy_usb
);
660 static int axp288_charger_handle_cable_evt(struct notifier_block
*nb
,
661 unsigned long event
, void *param
)
663 struct axp288_chrg_info
*info
=
664 container_of(nb
, struct axp288_chrg_info
, cable
.nb
);
666 schedule_work(&info
->cable
.work
);
671 static void axp288_charger_otg_evt_worker(struct work_struct
*work
)
673 struct axp288_chrg_info
*info
=
674 container_of(work
, struct axp288_chrg_info
, otg
.work
);
677 /* Disable VBUS path before enabling the 5V boost */
678 ret
= axp288_charger_vbus_path_select(info
, !info
->otg
.id_short
);
680 dev_warn(&info
->pdev
->dev
, "vbus path disable failed\n");
683 static int axp288_charger_handle_otg_evt(struct notifier_block
*nb
,
684 unsigned long event
, void *param
)
686 struct axp288_chrg_info
*info
=
687 container_of(nb
, struct axp288_chrg_info
, otg
.id_nb
);
688 struct extcon_dev
*edev
= info
->otg
.cable
;
689 int usb_host
= extcon_get_cable_state_(edev
, EXTCON_USB_HOST
);
691 dev_dbg(&info
->pdev
->dev
, "external connector USB-Host is %s\n",
692 usb_host
? "attached" : "detached");
695 * Set usb_id_short flag to avoid running charger detection logic
698 info
->otg
.id_short
= usb_host
;
699 schedule_work(&info
->otg
.work
);
704 static void charger_init_hw_regs(struct axp288_chrg_info
*info
)
709 /* Program temperature thresholds */
710 ret
= regmap_write(info
->regmap
, AXP20X_V_LTF_CHRG
, CHRG_VLTFC_0C
);
712 dev_warn(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
713 AXP20X_V_LTF_CHRG
, ret
);
715 ret
= regmap_write(info
->regmap
, AXP20X_V_HTF_CHRG
, CHRG_VHTFC_45C
);
717 dev_warn(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
718 AXP20X_V_HTF_CHRG
, ret
);
720 /* Do not turn-off charger o/p after charge cycle ends */
721 ret
= regmap_update_bits(info
->regmap
,
723 CNTL2_CHG_OUT_TURNON
, 1);
725 dev_warn(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
726 AXP20X_CHRG_CTRL2
, ret
);
728 /* Enable interrupts */
729 ret
= regmap_update_bits(info
->regmap
,
731 BAT_IRQ_CFG_BAT_MASK
, 1);
733 dev_warn(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
734 AXP20X_IRQ2_EN
, ret
);
736 ret
= regmap_update_bits(info
->regmap
, AXP20X_IRQ3_EN
,
737 TEMP_IRQ_CFG_MASK
, 1);
739 dev_warn(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
740 AXP20X_IRQ3_EN
, ret
);
742 /* Setup ending condition for charging to be 10% of I(chrg) */
743 ret
= regmap_update_bits(info
->regmap
,
745 CHRG_CCCV_ITERM_20P
, 0);
747 dev_warn(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
748 AXP20X_CHRG_CTRL1
, ret
);
750 /* Disable OCV-SOC curve calibration */
751 ret
= regmap_update_bits(info
->regmap
,
753 FG_CNTL_OCV_ADJ_EN
, 0);
755 dev_warn(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
756 AXP20X_CC_CTRL
, ret
);
758 /* Init charging current and voltage */
759 info
->max_cc
= info
->pdata
->max_cc
;
760 info
->max_cv
= info
->pdata
->max_cv
;
762 /* Read current charge voltage and current limit */
763 ret
= regmap_read(info
->regmap
, AXP20X_CHRG_CTRL1
, &val
);
765 /* Assume default if cannot read */
766 info
->cc
= info
->pdata
->def_cc
;
767 info
->cv
= info
->pdata
->def_cv
;
769 /* Determine charge voltage */
770 cv
= (val
& CHRG_CCCV_CV_MASK
) >> CHRG_CCCV_CV_BIT_POS
;
772 case CHRG_CCCV_CV_4100MV
:
773 info
->cv
= CV_4100MV
;
775 case CHRG_CCCV_CV_4150MV
:
776 info
->cv
= CV_4150MV
;
778 case CHRG_CCCV_CV_4200MV
:
779 info
->cv
= CV_4200MV
;
781 case CHRG_CCCV_CV_4350MV
:
782 info
->cv
= CV_4350MV
;
789 /* Determine charge current limit */
790 cc
= (ret
& CHRG_CCCV_CC_MASK
) >> CHRG_CCCV_CC_BIT_POS
;
791 cc
= (cc
* CHRG_CCCV_CC_LSB_RES
) + CHRG_CCCV_CC_OFFSET
;
794 /* Program default charging voltage and current */
795 cc
= min(info
->pdata
->def_cc
, info
->max_cc
);
796 cv
= min(info
->pdata
->def_cv
, info
->max_cv
);
798 ret
= axp288_charger_set_cc(info
, cc
);
800 dev_warn(&info
->pdev
->dev
,
801 "error(%d) in setting CC\n", ret
);
803 ret
= axp288_charger_set_cv(info
, cv
);
805 dev_warn(&info
->pdev
->dev
,
806 "error(%d) in setting CV\n", ret
);
810 static int axp288_charger_probe(struct platform_device
*pdev
)
813 struct axp288_chrg_info
*info
;
814 struct axp20x_dev
*axp20x
= dev_get_drvdata(pdev
->dev
.parent
);
815 struct power_supply_config charger_cfg
= {};
817 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
822 info
->regmap
= axp20x
->regmap
;
823 info
->regmap_irqc
= axp20x
->regmap_irqc
;
824 info
->pdata
= pdev
->dev
.platform_data
;
827 /* Try ACPI provided pdata via device properties */
828 if (!device_property_present(&pdev
->dev
,
829 "axp288_charger_data\n"))
830 dev_err(&pdev
->dev
, "failed to get platform data\n");
834 info
->cable
.edev
= extcon_get_extcon_dev(AXP288_EXTCON_DEV_NAME
);
835 if (info
->cable
.edev
== NULL
) {
836 dev_dbg(&pdev
->dev
, "%s is not ready, probe deferred\n",
837 AXP288_EXTCON_DEV_NAME
);
838 return -EPROBE_DEFER
;
841 /* Register for extcon notification */
842 INIT_WORK(&info
->cable
.work
, axp288_charger_extcon_evt_worker
);
843 info
->cable
.nb
.notifier_call
= axp288_charger_handle_cable_evt
;
844 ret
= extcon_register_notifier(info
->cable
.edev
, EXTCON_CHG_USB_SDP
,
847 dev_err(&info
->pdev
->dev
,
848 "failed to register extcon notifier for SDP %d\n", ret
);
852 ret
= extcon_register_notifier(info
->cable
.edev
, EXTCON_CHG_USB_CDP
,
855 dev_err(&info
->pdev
->dev
,
856 "failed to register extcon notifier for CDP %d\n", ret
);
857 extcon_unregister_notifier(info
->cable
.edev
,
858 EXTCON_CHG_USB_SDP
, &info
->cable
.nb
);
862 ret
= extcon_register_notifier(info
->cable
.edev
, EXTCON_CHG_USB_DCP
,
865 dev_err(&info
->pdev
->dev
,
866 "failed to register extcon notifier for DCP %d\n", ret
);
867 extcon_unregister_notifier(info
->cable
.edev
,
868 EXTCON_CHG_USB_SDP
, &info
->cable
.nb
);
869 extcon_unregister_notifier(info
->cable
.edev
,
870 EXTCON_CHG_USB_CDP
, &info
->cable
.nb
);
874 platform_set_drvdata(pdev
, info
);
875 mutex_init(&info
->lock
);
877 /* Register with power supply class */
878 charger_cfg
.drv_data
= info
;
879 info
->psy_usb
= power_supply_register(&pdev
->dev
, &axp288_charger_desc
,
881 if (IS_ERR(info
->psy_usb
)) {
882 dev_err(&pdev
->dev
, "failed to register power supply charger\n");
883 ret
= PTR_ERR(info
->psy_usb
);
887 /* Register for OTG notification */
888 INIT_WORK(&info
->otg
.work
, axp288_charger_otg_evt_worker
);
889 info
->otg
.id_nb
.notifier_call
= axp288_charger_handle_otg_evt
;
890 ret
= extcon_register_notifier(info
->otg
.cable
, EXTCON_USB_HOST
,
893 dev_warn(&pdev
->dev
, "failed to register otg notifier\n");
896 info
->otg
.id_short
= extcon_get_cable_state_(
897 info
->otg
.cable
, EXTCON_USB_HOST
);
899 /* Register charger interrupts */
900 for (i
= 0; i
< CHRG_INTR_END
; i
++) {
901 pirq
= platform_get_irq(info
->pdev
, i
);
902 info
->irq
[i
] = regmap_irq_get_virq(info
->regmap_irqc
, pirq
);
903 if (info
->irq
[i
] < 0) {
904 dev_warn(&info
->pdev
->dev
,
905 "failed to get virtual interrupt=%d\n", pirq
);
907 goto intr_reg_failed
;
909 ret
= devm_request_threaded_irq(&info
->pdev
->dev
, info
->irq
[i
],
910 NULL
, axp288_charger_irq_thread_handler
,
911 IRQF_ONESHOT
, info
->pdev
->name
, info
);
913 dev_err(&pdev
->dev
, "failed to request interrupt=%d\n",
915 goto intr_reg_failed
;
919 charger_init_hw_regs(info
);
925 extcon_unregister_notifier(info
->otg
.cable
, EXTCON_USB_HOST
,
927 power_supply_unregister(info
->psy_usb
);
929 extcon_unregister_notifier(info
->cable
.edev
, EXTCON_CHG_USB_SDP
,
931 extcon_unregister_notifier(info
->cable
.edev
, EXTCON_CHG_USB_CDP
,
933 extcon_unregister_notifier(info
->cable
.edev
, EXTCON_CHG_USB_DCP
,
938 static int axp288_charger_remove(struct platform_device
*pdev
)
940 struct axp288_chrg_info
*info
= dev_get_drvdata(&pdev
->dev
);
943 extcon_unregister_notifier(info
->otg
.cable
, EXTCON_USB_HOST
,
946 extcon_unregister_notifier(info
->cable
.edev
, EXTCON_CHG_USB_SDP
,
948 extcon_unregister_notifier(info
->cable
.edev
, EXTCON_CHG_USB_CDP
,
950 extcon_unregister_notifier(info
->cable
.edev
, EXTCON_CHG_USB_DCP
,
952 power_supply_unregister(info
->psy_usb
);
957 static struct platform_driver axp288_charger_driver
= {
958 .probe
= axp288_charger_probe
,
959 .remove
= axp288_charger_remove
,
961 .name
= "axp288_charger",
965 module_platform_driver(axp288_charger_driver
);
967 MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
968 MODULE_DESCRIPTION("X-power AXP288 Charger Driver");
969 MODULE_LICENSE("GPL v2");