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/notifier.h>
27 #include <linux/property.h>
28 #include <linux/mfd/axp20x.h>
29 #include <linux/extcon.h>
31 #define PS_STAT_VBUS_TRIGGER (1 << 0)
32 #define PS_STAT_BAT_CHRG_DIR (1 << 2)
33 #define PS_STAT_VBAT_ABOVE_VHOLD (1 << 3)
34 #define PS_STAT_VBUS_VALID (1 << 4)
35 #define PS_STAT_VBUS_PRESENT (1 << 5)
37 #define CHRG_STAT_BAT_SAFE_MODE (1 << 3)
38 #define CHRG_STAT_BAT_VALID (1 << 4)
39 #define CHRG_STAT_BAT_PRESENT (1 << 5)
40 #define CHRG_STAT_CHARGING (1 << 6)
41 #define CHRG_STAT_PMIC_OTP (1 << 7)
43 #define VBUS_ISPOUT_CUR_LIM_MASK 0x03
44 #define VBUS_ISPOUT_CUR_LIM_BIT_POS 0
45 #define VBUS_ISPOUT_CUR_LIM_900MA 0x0 /* 900mA */
46 #define VBUS_ISPOUT_CUR_LIM_1500MA 0x1 /* 1500mA */
47 #define VBUS_ISPOUT_CUR_LIM_2000MA 0x2 /* 2000mA */
48 #define VBUS_ISPOUT_CUR_NO_LIM 0x3 /* 2500mA */
49 #define VBUS_ISPOUT_VHOLD_SET_MASK 0x31
50 #define VBUS_ISPOUT_VHOLD_SET_BIT_POS 0x3
51 #define VBUS_ISPOUT_VHOLD_SET_OFFSET 4000 /* 4000mV */
52 #define VBUS_ISPOUT_VHOLD_SET_LSB_RES 100 /* 100mV */
53 #define VBUS_ISPOUT_VHOLD_SET_4300MV 0x3 /* 4300mV */
54 #define VBUS_ISPOUT_VBUS_PATH_DIS (1 << 7)
56 #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */
57 #define CHRG_CCCV_CC_BIT_POS 0
58 #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */
59 #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */
60 #define CHRG_CCCV_ITERM_20P (1 << 4) /* 20% of CC */
61 #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */
62 #define CHRG_CCCV_CV_BIT_POS 5
63 #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */
64 #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */
65 #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */
66 #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */
67 #define CHRG_CCCV_CHG_EN (1 << 7)
69 #define CNTL2_CC_TIMEOUT_MASK 0x3 /* 2 bits */
70 #define CNTL2_CC_TIMEOUT_OFFSET 6 /* 6 Hrs */
71 #define CNTL2_CC_TIMEOUT_LSB_RES 2 /* 2 Hrs */
72 #define CNTL2_CC_TIMEOUT_12HRS 0x3 /* 12 Hrs */
73 #define CNTL2_CHGLED_TYPEB (1 << 4)
74 #define CNTL2_CHG_OUT_TURNON (1 << 5)
75 #define CNTL2_PC_TIMEOUT_MASK 0xC0
76 #define CNTL2_PC_TIMEOUT_OFFSET 40 /* 40 mins */
77 #define CNTL2_PC_TIMEOUT_LSB_RES 10 /* 10 mins */
78 #define CNTL2_PC_TIMEOUT_70MINS 0x3
80 #define CHRG_ILIM_TEMP_LOOP_EN (1 << 3)
81 #define CHRG_VBUS_ILIM_MASK 0xf0
82 #define CHRG_VBUS_ILIM_BIT_POS 4
83 #define CHRG_VBUS_ILIM_100MA 0x0 /* 100mA */
84 #define CHRG_VBUS_ILIM_500MA 0x1 /* 500mA */
85 #define CHRG_VBUS_ILIM_900MA 0x2 /* 900mA */
86 #define CHRG_VBUS_ILIM_1500MA 0x3 /* 1500mA */
87 #define CHRG_VBUS_ILIM_2000MA 0x4 /* 2000mA */
88 #define CHRG_VBUS_ILIM_2500MA 0x5 /* 2500mA */
89 #define CHRG_VBUS_ILIM_3000MA 0x6 /* 3000mA */
91 #define CHRG_VLTFC_0C 0xA5 /* 0 DegC */
92 #define CHRG_VHTFC_45C 0x1F /* 45 DegC */
94 #define BAT_IRQ_CFG_CHRG_DONE (1 << 2)
95 #define BAT_IRQ_CFG_CHRG_START (1 << 3)
96 #define BAT_IRQ_CFG_BAT_SAFE_EXIT (1 << 4)
97 #define BAT_IRQ_CFG_BAT_SAFE_ENTER (1 << 5)
98 #define BAT_IRQ_CFG_BAT_DISCON (1 << 6)
99 #define BAT_IRQ_CFG_BAT_CONN (1 << 7)
100 #define BAT_IRQ_CFG_BAT_MASK 0xFC
102 #define TEMP_IRQ_CFG_QCBTU (1 << 4)
103 #define TEMP_IRQ_CFG_CBTU (1 << 5)
104 #define TEMP_IRQ_CFG_QCBTO (1 << 6)
105 #define TEMP_IRQ_CFG_CBTO (1 << 7)
106 #define TEMP_IRQ_CFG_MASK 0xF0
108 #define FG_CNTL_OCV_ADJ_EN (1 << 3)
110 #define CV_4100MV 4100 /* 4100mV */
111 #define CV_4150MV 4150 /* 4150mV */
112 #define CV_4200MV 4200 /* 4200mV */
113 #define CV_4350MV 4350 /* 4350mV */
115 #define CC_200MA 200 /* 200mA */
116 #define CC_600MA 600 /* 600mA */
117 #define CC_800MA 800 /* 800mA */
118 #define CC_1000MA 1000 /* 1000mA */
119 #define CC_1600MA 1600 /* 1600mA */
120 #define CC_2000MA 2000 /* 2000mA */
122 #define ILIM_100MA 100 /* 100mA */
123 #define ILIM_500MA 500 /* 500mA */
124 #define ILIM_900MA 900 /* 900mA */
125 #define ILIM_1500MA 1500 /* 1500mA */
126 #define ILIM_2000MA 2000 /* 2000mA */
127 #define ILIM_2500MA 2500 /* 2500mA */
128 #define ILIM_3000MA 3000 /* 3000mA */
130 #define AXP288_EXTCON_DEV_NAME "axp288_extcon"
145 struct axp288_chrg_info
{
146 struct platform_device
*pdev
;
147 struct axp20x_chrg_pdata
*pdata
;
148 struct regmap
*regmap
;
149 struct regmap_irq_chip_data
*regmap_irqc
;
150 int irq
[CHRG_INTR_END
];
151 struct power_supply
*psy_usb
;
156 struct work_struct work
;
157 struct extcon_dev
*cable
;
158 struct notifier_block id_nb
;
162 /* SDP/CDP/DCP USB charging cable notifications */
164 struct extcon_dev
*edev
;
166 enum power_supply_type chg_type
;
167 struct notifier_block nb
;
168 struct work_struct work
;
180 bool is_charger_enabled
;
183 static inline int axp288_charger_set_cc(struct axp288_chrg_info
*info
, int cc
)
188 if (cc
< CHRG_CCCV_CC_OFFSET
)
189 cc
= CHRG_CCCV_CC_OFFSET
;
190 else if (cc
> info
->max_cc
)
193 reg_val
= (cc
- CHRG_CCCV_CC_OFFSET
) / CHRG_CCCV_CC_LSB_RES
;
194 cc
= (reg_val
* CHRG_CCCV_CC_LSB_RES
) + CHRG_CCCV_CC_OFFSET
;
195 reg_val
= reg_val
<< CHRG_CCCV_CC_BIT_POS
;
197 ret
= regmap_update_bits(info
->regmap
,
199 CHRG_CCCV_CC_MASK
, reg_val
);
206 static inline int axp288_charger_set_cv(struct axp288_chrg_info
*info
, int cv
)
211 if (cv
<= CV_4100MV
) {
212 reg_val
= CHRG_CCCV_CV_4100MV
;
214 } else if (cv
<= CV_4150MV
) {
215 reg_val
= CHRG_CCCV_CV_4150MV
;
217 } else if (cv
<= CV_4200MV
) {
218 reg_val
= CHRG_CCCV_CV_4200MV
;
221 reg_val
= CHRG_CCCV_CV_4350MV
;
225 reg_val
= reg_val
<< CHRG_CCCV_CV_BIT_POS
;
227 ret
= regmap_update_bits(info
->regmap
,
229 CHRG_CCCV_CV_MASK
, reg_val
);
237 static inline int axp288_charger_set_vbus_inlmt(struct axp288_chrg_info
*info
,
244 /* Read in limit register */
245 ret
= regmap_read(info
->regmap
, AXP20X_CHRG_BAK_CTRL
, &val
);
249 if (inlmt
<= ILIM_100MA
) {
250 reg_val
= CHRG_VBUS_ILIM_100MA
;
252 } else if (inlmt
<= ILIM_500MA
) {
253 reg_val
= CHRG_VBUS_ILIM_500MA
;
255 } else if (inlmt
<= ILIM_900MA
) {
256 reg_val
= CHRG_VBUS_ILIM_900MA
;
258 } else if (inlmt
<= ILIM_1500MA
) {
259 reg_val
= CHRG_VBUS_ILIM_1500MA
;
261 } else if (inlmt
<= ILIM_2000MA
) {
262 reg_val
= CHRG_VBUS_ILIM_2000MA
;
264 } else if (inlmt
<= ILIM_2500MA
) {
265 reg_val
= CHRG_VBUS_ILIM_2500MA
;
268 reg_val
= CHRG_VBUS_ILIM_3000MA
;
272 reg_val
= (val
& ~CHRG_VBUS_ILIM_MASK
)
273 | (reg_val
<< CHRG_VBUS_ILIM_BIT_POS
);
274 ret
= regmap_write(info
->regmap
, AXP20X_CHRG_BAK_CTRL
, reg_val
);
278 dev_err(&info
->pdev
->dev
, "charger BAK control %d\n", ret
);
285 static int axp288_charger_vbus_path_select(struct axp288_chrg_info
*info
,
291 ret
= regmap_update_bits(info
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
,
292 VBUS_ISPOUT_VBUS_PATH_DIS
, 0);
294 ret
= regmap_update_bits(info
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
,
295 VBUS_ISPOUT_VBUS_PATH_DIS
, VBUS_ISPOUT_VBUS_PATH_DIS
);
298 dev_err(&info
->pdev
->dev
, "axp288 vbus path select %d\n", ret
);
304 static int axp288_charger_enable_charger(struct axp288_chrg_info
*info
,
310 ret
= regmap_update_bits(info
->regmap
, AXP20X_CHRG_CTRL1
,
311 CHRG_CCCV_CHG_EN
, CHRG_CCCV_CHG_EN
);
313 ret
= regmap_update_bits(info
->regmap
, AXP20X_CHRG_CTRL1
,
314 CHRG_CCCV_CHG_EN
, 0);
316 dev_err(&info
->pdev
->dev
, "axp288 enable charger %d\n", ret
);
318 info
->is_charger_enabled
= enable
;
323 static int axp288_charger_is_present(struct axp288_chrg_info
*info
)
325 int ret
, present
= 0;
328 ret
= regmap_read(info
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
332 if (val
& PS_STAT_VBUS_PRESENT
)
337 static int axp288_charger_is_online(struct axp288_chrg_info
*info
)
342 ret
= regmap_read(info
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
346 if (val
& PS_STAT_VBUS_VALID
)
351 static int axp288_get_charger_health(struct axp288_chrg_info
*info
)
353 int ret
, pwr_stat
, chrg_stat
;
354 int health
= POWER_SUPPLY_HEALTH_UNKNOWN
;
357 ret
= regmap_read(info
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
358 if ((ret
< 0) || !(val
& PS_STAT_VBUS_PRESENT
))
359 goto health_read_fail
;
363 ret
= regmap_read(info
->regmap
, AXP20X_PWR_OP_MODE
, &val
);
365 goto health_read_fail
;
369 if (!(pwr_stat
& PS_STAT_VBUS_VALID
))
370 health
= POWER_SUPPLY_HEALTH_DEAD
;
371 else if (chrg_stat
& CHRG_STAT_PMIC_OTP
)
372 health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
373 else if (chrg_stat
& CHRG_STAT_BAT_SAFE_MODE
)
374 health
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
376 health
= POWER_SUPPLY_HEALTH_GOOD
;
382 static int axp288_charger_usb_set_property(struct power_supply
*psy
,
383 enum power_supply_property psp
,
384 const union power_supply_propval
*val
)
386 struct axp288_chrg_info
*info
= power_supply_get_drvdata(psy
);
390 mutex_lock(&info
->lock
);
393 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
394 scaled_val
= min(val
->intval
, info
->max_cc
);
395 scaled_val
= DIV_ROUND_CLOSEST(scaled_val
, 1000);
396 ret
= axp288_charger_set_cc(info
, scaled_val
);
398 dev_warn(&info
->pdev
->dev
, "set charge current failed\n");
400 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
401 scaled_val
= min(val
->intval
, info
->max_cv
);
402 scaled_val
= DIV_ROUND_CLOSEST(scaled_val
, 1000);
403 ret
= axp288_charger_set_cv(info
, scaled_val
);
405 dev_warn(&info
->pdev
->dev
, "set charge voltage failed\n");
411 mutex_unlock(&info
->lock
);
415 static int axp288_charger_usb_get_property(struct power_supply
*psy
,
416 enum power_supply_property psp
,
417 union power_supply_propval
*val
)
419 struct axp288_chrg_info
*info
= power_supply_get_drvdata(psy
);
422 mutex_lock(&info
->lock
);
425 case POWER_SUPPLY_PROP_PRESENT
:
426 /* Check for OTG case first */
427 if (info
->otg
.id_short
) {
431 ret
= axp288_charger_is_present(info
);
433 goto psy_get_prop_fail
;
435 val
->intval
= info
->present
;
437 case POWER_SUPPLY_PROP_ONLINE
:
438 /* Check for OTG case first */
439 if (info
->otg
.id_short
) {
443 ret
= axp288_charger_is_online(info
);
445 goto psy_get_prop_fail
;
447 val
->intval
= info
->online
;
449 case POWER_SUPPLY_PROP_HEALTH
:
450 val
->intval
= axp288_get_charger_health(info
);
452 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
453 val
->intval
= info
->cc
* 1000;
455 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
456 val
->intval
= info
->max_cc
* 1000;
458 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
459 val
->intval
= info
->cv
* 1000;
461 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
462 val
->intval
= info
->max_cv
* 1000;
464 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
:
465 val
->intval
= info
->inlmt
* 1000;
469 goto psy_get_prop_fail
;
473 mutex_unlock(&info
->lock
);
477 static int axp288_charger_property_is_writeable(struct power_supply
*psy
,
478 enum power_supply_property psp
)
483 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
484 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
494 static enum power_supply_property axp288_usb_props
[] = {
495 POWER_SUPPLY_PROP_PRESENT
,
496 POWER_SUPPLY_PROP_ONLINE
,
497 POWER_SUPPLY_PROP_TYPE
,
498 POWER_SUPPLY_PROP_HEALTH
,
499 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
500 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
501 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
502 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
503 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
,
506 static const struct power_supply_desc axp288_charger_desc
= {
507 .name
= "axp288_charger",
508 .type
= POWER_SUPPLY_TYPE_USB
,
509 .properties
= axp288_usb_props
,
510 .num_properties
= ARRAY_SIZE(axp288_usb_props
),
511 .get_property
= axp288_charger_usb_get_property
,
512 .set_property
= axp288_charger_usb_set_property
,
513 .property_is_writeable
= axp288_charger_property_is_writeable
,
516 static irqreturn_t
axp288_charger_irq_thread_handler(int irq
, void *dev
)
518 struct axp288_chrg_info
*info
= dev
;
521 for (i
= 0; i
< CHRG_INTR_END
; i
++) {
522 if (info
->irq
[i
] == irq
)
526 if (i
>= CHRG_INTR_END
) {
527 dev_warn(&info
->pdev
->dev
, "spurious interrupt!!\n");
533 dev_dbg(&info
->pdev
->dev
, "VBUS Over Voltage INTR\n");
535 case CHARGE_DONE_IRQ
:
536 dev_dbg(&info
->pdev
->dev
, "Charging Done INTR\n");
538 case CHARGE_CHARGING_IRQ
:
539 dev_dbg(&info
->pdev
->dev
, "Start Charging IRQ\n");
541 case BAT_SAFE_QUIT_IRQ
:
542 dev_dbg(&info
->pdev
->dev
,
543 "Quit Safe Mode(restart timer) Charging IRQ\n");
545 case BAT_SAFE_ENTER_IRQ
:
546 dev_dbg(&info
->pdev
->dev
,
547 "Enter Safe Mode(timer expire) Charging IRQ\n");
550 dev_dbg(&info
->pdev
->dev
,
551 "Quit Battery Under Temperature(CHRG) INTR\n");
554 dev_dbg(&info
->pdev
->dev
,
555 "Hit Battery Under Temperature(CHRG) INTR\n");
558 dev_dbg(&info
->pdev
->dev
,
559 "Quit Battery Over Temperature(CHRG) INTR\n");
562 dev_dbg(&info
->pdev
->dev
,
563 "Hit Battery Over Temperature(CHRG) INTR\n");
566 dev_warn(&info
->pdev
->dev
, "Spurious Interrupt!!!\n");
570 power_supply_changed(info
->psy_usb
);
575 static void axp288_charger_extcon_evt_worker(struct work_struct
*work
)
577 struct axp288_chrg_info
*info
=
578 container_of(work
, struct axp288_chrg_info
, cable
.work
);
579 int ret
, current_limit
;
580 bool changed
= false;
581 struct extcon_dev
*edev
= info
->cable
.edev
;
582 bool old_connected
= info
->cable
.connected
;
584 /* Determine cable/charger type */
585 if (extcon_get_cable_state_(edev
, EXTCON_CHG_USB_SDP
) > 0) {
586 dev_dbg(&info
->pdev
->dev
, "USB SDP charger is connected");
587 info
->cable
.connected
= true;
588 info
->cable
.chg_type
= POWER_SUPPLY_TYPE_USB
;
589 } else if (extcon_get_cable_state_(edev
, EXTCON_CHG_USB_CDP
) > 0) {
590 dev_dbg(&info
->pdev
->dev
, "USB CDP charger is connected");
591 info
->cable
.connected
= true;
592 info
->cable
.chg_type
= POWER_SUPPLY_TYPE_USB_CDP
;
593 } else if (extcon_get_cable_state_(edev
, EXTCON_CHG_USB_DCP
) > 0) {
594 dev_dbg(&info
->pdev
->dev
, "USB DCP charger is connected");
595 info
->cable
.connected
= true;
596 info
->cable
.chg_type
= POWER_SUPPLY_TYPE_USB_DCP
;
599 dev_dbg(&info
->pdev
->dev
, "USB charger disconnected");
600 info
->cable
.connected
= false;
601 info
->cable
.chg_type
= POWER_SUPPLY_TYPE_USB
;
604 /* Cable status changed */
605 if (old_connected
!= info
->cable
.connected
)
611 mutex_lock(&info
->lock
);
613 if (info
->is_charger_enabled
&& !info
->cable
.connected
) {
614 info
->enable_charger
= false;
615 ret
= axp288_charger_enable_charger(info
, info
->enable_charger
);
617 dev_err(&info
->pdev
->dev
,
618 "cannot disable charger (%d)", ret
);
620 } else if (!info
->is_charger_enabled
&& info
->cable
.connected
) {
621 switch (info
->cable
.chg_type
) {
622 case POWER_SUPPLY_TYPE_USB
:
623 current_limit
= ILIM_500MA
;
625 case POWER_SUPPLY_TYPE_USB_CDP
:
626 current_limit
= ILIM_1500MA
;
628 case POWER_SUPPLY_TYPE_USB_DCP
:
629 current_limit
= ILIM_2000MA
;
637 /* Set vbus current limit first, then enable charger */
638 ret
= axp288_charger_set_vbus_inlmt(info
, current_limit
);
640 dev_err(&info
->pdev
->dev
,
641 "error setting current limit (%d)", ret
);
643 info
->enable_charger
= (current_limit
> 0);
644 ret
= axp288_charger_enable_charger(info
,
645 info
->enable_charger
);
647 dev_err(&info
->pdev
->dev
,
648 "cannot enable charger (%d)", ret
);
653 info
->health
= axp288_get_charger_health(info
);
655 mutex_unlock(&info
->lock
);
658 power_supply_changed(info
->psy_usb
);
661 static int axp288_charger_handle_cable_evt(struct notifier_block
*nb
,
662 unsigned long event
, void *param
)
664 struct axp288_chrg_info
*info
=
665 container_of(nb
, struct axp288_chrg_info
, cable
.nb
);
667 schedule_work(&info
->cable
.work
);
672 static void axp288_charger_otg_evt_worker(struct work_struct
*work
)
674 struct axp288_chrg_info
*info
=
675 container_of(work
, struct axp288_chrg_info
, otg
.work
);
678 /* Disable VBUS path before enabling the 5V boost */
679 ret
= axp288_charger_vbus_path_select(info
, !info
->otg
.id_short
);
681 dev_warn(&info
->pdev
->dev
, "vbus path disable failed\n");
684 static int axp288_charger_handle_otg_evt(struct notifier_block
*nb
,
685 unsigned long event
, void *param
)
687 struct axp288_chrg_info
*info
=
688 container_of(nb
, struct axp288_chrg_info
, otg
.id_nb
);
689 struct extcon_dev
*edev
= info
->otg
.cable
;
690 int usb_host
= extcon_get_cable_state_(edev
, EXTCON_USB_HOST
);
692 dev_dbg(&info
->pdev
->dev
, "external connector USB-Host is %s\n",
693 usb_host
? "attached" : "detached");
696 * Set usb_id_short flag to avoid running charger detection logic
699 info
->otg
.id_short
= usb_host
;
700 schedule_work(&info
->otg
.work
);
705 static void charger_init_hw_regs(struct axp288_chrg_info
*info
)
710 /* Program temperature thresholds */
711 ret
= regmap_write(info
->regmap
, AXP20X_V_LTF_CHRG
, CHRG_VLTFC_0C
);
713 dev_warn(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
714 AXP20X_V_LTF_CHRG
, ret
);
716 ret
= regmap_write(info
->regmap
, AXP20X_V_HTF_CHRG
, CHRG_VHTFC_45C
);
718 dev_warn(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
719 AXP20X_V_HTF_CHRG
, ret
);
721 /* Do not turn-off charger o/p after charge cycle ends */
722 ret
= regmap_update_bits(info
->regmap
,
724 CNTL2_CHG_OUT_TURNON
, 1);
726 dev_warn(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
727 AXP20X_CHRG_CTRL2
, ret
);
729 /* Enable interrupts */
730 ret
= regmap_update_bits(info
->regmap
,
732 BAT_IRQ_CFG_BAT_MASK
, 1);
734 dev_warn(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
735 AXP20X_IRQ2_EN
, ret
);
737 ret
= regmap_update_bits(info
->regmap
, AXP20X_IRQ3_EN
,
738 TEMP_IRQ_CFG_MASK
, 1);
740 dev_warn(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
741 AXP20X_IRQ3_EN
, ret
);
743 /* Setup ending condition for charging to be 10% of I(chrg) */
744 ret
= regmap_update_bits(info
->regmap
,
746 CHRG_CCCV_ITERM_20P
, 0);
748 dev_warn(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
749 AXP20X_CHRG_CTRL1
, ret
);
751 /* Disable OCV-SOC curve calibration */
752 ret
= regmap_update_bits(info
->regmap
,
754 FG_CNTL_OCV_ADJ_EN
, 0);
756 dev_warn(&info
->pdev
->dev
, "register(%x) write error(%d)\n",
757 AXP20X_CC_CTRL
, ret
);
759 /* Init charging current and voltage */
760 info
->max_cc
= info
->pdata
->max_cc
;
761 info
->max_cv
= info
->pdata
->max_cv
;
763 /* Read current charge voltage and current limit */
764 ret
= regmap_read(info
->regmap
, AXP20X_CHRG_CTRL1
, &val
);
766 /* Assume default if cannot read */
767 info
->cc
= info
->pdata
->def_cc
;
768 info
->cv
= info
->pdata
->def_cv
;
770 /* Determine charge voltage */
771 cv
= (val
& CHRG_CCCV_CV_MASK
) >> CHRG_CCCV_CV_BIT_POS
;
773 case CHRG_CCCV_CV_4100MV
:
774 info
->cv
= CV_4100MV
;
776 case CHRG_CCCV_CV_4150MV
:
777 info
->cv
= CV_4150MV
;
779 case CHRG_CCCV_CV_4200MV
:
780 info
->cv
= CV_4200MV
;
782 case CHRG_CCCV_CV_4350MV
:
783 info
->cv
= CV_4350MV
;
790 /* Determine charge current limit */
791 cc
= (ret
& CHRG_CCCV_CC_MASK
) >> CHRG_CCCV_CC_BIT_POS
;
792 cc
= (cc
* CHRG_CCCV_CC_LSB_RES
) + CHRG_CCCV_CC_OFFSET
;
795 /* Program default charging voltage and current */
796 cc
= min(info
->pdata
->def_cc
, info
->max_cc
);
797 cv
= min(info
->pdata
->def_cv
, info
->max_cv
);
799 ret
= axp288_charger_set_cc(info
, cc
);
801 dev_warn(&info
->pdev
->dev
,
802 "error(%d) in setting CC\n", ret
);
804 ret
= axp288_charger_set_cv(info
, cv
);
806 dev_warn(&info
->pdev
->dev
,
807 "error(%d) in setting CV\n", ret
);
811 static int axp288_charger_probe(struct platform_device
*pdev
)
814 struct axp288_chrg_info
*info
;
815 struct axp20x_dev
*axp20x
= dev_get_drvdata(pdev
->dev
.parent
);
816 struct power_supply_config charger_cfg
= {};
818 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
823 info
->regmap
= axp20x
->regmap
;
824 info
->regmap_irqc
= axp20x
->regmap_irqc
;
825 info
->pdata
= pdev
->dev
.platform_data
;
828 /* Try ACPI provided pdata via device properties */
829 if (!device_property_present(&pdev
->dev
,
830 "axp288_charger_data\n"))
831 dev_err(&pdev
->dev
, "failed to get platform data\n");
835 info
->cable
.edev
= extcon_get_extcon_dev(AXP288_EXTCON_DEV_NAME
);
836 if (info
->cable
.edev
== NULL
) {
837 dev_dbg(&pdev
->dev
, "%s is not ready, probe deferred\n",
838 AXP288_EXTCON_DEV_NAME
);
839 return -EPROBE_DEFER
;
842 /* Register for extcon notification */
843 INIT_WORK(&info
->cable
.work
, axp288_charger_extcon_evt_worker
);
844 info
->cable
.nb
.notifier_call
= axp288_charger_handle_cable_evt
;
845 ret
= extcon_register_notifier(info
->cable
.edev
, EXTCON_CHG_USB_SDP
,
848 dev_err(&info
->pdev
->dev
,
849 "failed to register extcon notifier for SDP %d\n", ret
);
853 ret
= extcon_register_notifier(info
->cable
.edev
, EXTCON_CHG_USB_CDP
,
856 dev_err(&info
->pdev
->dev
,
857 "failed to register extcon notifier for CDP %d\n", ret
);
858 extcon_unregister_notifier(info
->cable
.edev
,
859 EXTCON_CHG_USB_SDP
, &info
->cable
.nb
);
863 ret
= extcon_register_notifier(info
->cable
.edev
, EXTCON_CHG_USB_DCP
,
866 dev_err(&info
->pdev
->dev
,
867 "failed to register extcon notifier for DCP %d\n", ret
);
868 extcon_unregister_notifier(info
->cable
.edev
,
869 EXTCON_CHG_USB_SDP
, &info
->cable
.nb
);
870 extcon_unregister_notifier(info
->cable
.edev
,
871 EXTCON_CHG_USB_CDP
, &info
->cable
.nb
);
875 platform_set_drvdata(pdev
, info
);
876 mutex_init(&info
->lock
);
878 /* Register with power supply class */
879 charger_cfg
.drv_data
= info
;
880 info
->psy_usb
= power_supply_register(&pdev
->dev
, &axp288_charger_desc
,
882 if (IS_ERR(info
->psy_usb
)) {
883 dev_err(&pdev
->dev
, "failed to register power supply charger\n");
884 ret
= PTR_ERR(info
->psy_usb
);
888 /* Register for OTG notification */
889 INIT_WORK(&info
->otg
.work
, axp288_charger_otg_evt_worker
);
890 info
->otg
.id_nb
.notifier_call
= axp288_charger_handle_otg_evt
;
891 ret
= extcon_register_notifier(info
->otg
.cable
, EXTCON_USB_HOST
,
894 dev_warn(&pdev
->dev
, "failed to register otg notifier\n");
897 info
->otg
.id_short
= extcon_get_cable_state_(
898 info
->otg
.cable
, EXTCON_USB_HOST
);
900 /* Register charger interrupts */
901 for (i
= 0; i
< CHRG_INTR_END
; i
++) {
902 pirq
= platform_get_irq(info
->pdev
, i
);
903 info
->irq
[i
] = regmap_irq_get_virq(info
->regmap_irqc
, pirq
);
904 if (info
->irq
[i
] < 0) {
905 dev_warn(&info
->pdev
->dev
,
906 "failed to get virtual interrupt=%d\n", pirq
);
908 goto intr_reg_failed
;
910 ret
= devm_request_threaded_irq(&info
->pdev
->dev
, info
->irq
[i
],
911 NULL
, axp288_charger_irq_thread_handler
,
912 IRQF_ONESHOT
, info
->pdev
->name
, info
);
914 dev_err(&pdev
->dev
, "failed to request interrupt=%d\n",
916 goto intr_reg_failed
;
920 charger_init_hw_regs(info
);
926 extcon_unregister_notifier(info
->otg
.cable
, EXTCON_USB_HOST
,
928 power_supply_unregister(info
->psy_usb
);
930 extcon_unregister_notifier(info
->cable
.edev
, EXTCON_CHG_USB_SDP
,
932 extcon_unregister_notifier(info
->cable
.edev
, EXTCON_CHG_USB_CDP
,
934 extcon_unregister_notifier(info
->cable
.edev
, EXTCON_CHG_USB_DCP
,
939 static int axp288_charger_remove(struct platform_device
*pdev
)
941 struct axp288_chrg_info
*info
= dev_get_drvdata(&pdev
->dev
);
944 extcon_unregister_notifier(info
->otg
.cable
, EXTCON_USB_HOST
,
947 extcon_unregister_notifier(info
->cable
.edev
, EXTCON_CHG_USB_SDP
,
949 extcon_unregister_notifier(info
->cable
.edev
, EXTCON_CHG_USB_CDP
,
951 extcon_unregister_notifier(info
->cable
.edev
, EXTCON_CHG_USB_DCP
,
953 power_supply_unregister(info
->psy_usb
);
958 static struct platform_driver axp288_charger_driver
= {
959 .probe
= axp288_charger_probe
,
960 .remove
= axp288_charger_remove
,
962 .name
= "axp288_charger",
966 module_platform_driver(axp288_charger_driver
);
968 MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
969 MODULE_DESCRIPTION("X-power AXP288 Charger Driver");
970 MODULE_LICENSE("GPL v2");