2 * Copyright (C) ST-Ericsson SA 2012
4 * Charger driver for AB8500
6 * License Terms: GNU General Public License v2
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/platform_device.h>
20 #include <linux/power_supply.h>
21 #include <linux/completion.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/err.h>
24 #include <linux/workqueue.h>
25 #include <linux/kobject.h>
27 #include <linux/mfd/core.h>
28 #include <linux/mfd/abx500/ab8500.h>
29 #include <linux/mfd/abx500.h>
30 #include <linux/mfd/abx500/ab8500-bm.h>
31 #include <linux/mfd/abx500/ab8500-gpadc.h>
32 #include <linux/mfd/abx500/ux500_chargalg.h>
33 #include <linux/usb/otg.h>
34 #include <linux/mutex.h>
36 /* Charger constants */
41 #define MAIN_WDOG_ENA 0x01
42 #define MAIN_WDOG_KICK 0x02
43 #define MAIN_WDOG_DIS 0x00
44 #define CHARG_WD_KICK 0x01
45 #define MAIN_CH_ENA 0x01
46 #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
47 #define USB_CH_ENA 0x01
48 #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
49 #define MAIN_CH_DET 0x01
50 #define MAIN_CH_CV_ON 0x04
51 #define USB_CH_CV_ON 0x08
52 #define VBUS_DET_DBNC100 0x02
53 #define VBUS_DET_DBNC1 0x01
54 #define OTP_ENABLE_WD 0x01
56 #define MAIN_CH_INPUT_CURR_SHIFT 4
57 #define VBUS_IN_CURR_LIM_SHIFT 4
58 #define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
60 #define LED_INDICATOR_PWM_ENA 0x01
61 #define LED_INDICATOR_PWM_DIS 0x00
62 #define LED_IND_CUR_5MA 0x04
63 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
65 /* HW failure constants */
66 #define MAIN_CH_TH_PROT 0x02
67 #define VBUS_CH_NOK 0x08
68 #define USB_CH_TH_PROT 0x02
69 #define VBUS_OVV_TH 0x01
70 #define MAIN_CH_NOK 0x01
73 #define MAIN_CH_STATUS2_MAINCHGDROP 0x80
74 #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
75 #define USB_CH_VBUSDROP 0x40
76 #define USB_CH_VBUSDETDBNC 0x01
78 /* UsbLineStatus register bit masks */
79 #define AB8500_USB_LINK_STATUS 0x78
80 #define AB8500_STD_HOST_SUSP 0x18
82 /* Watchdog timeout constant */
83 #define WD_TIMER 0x30 /* 4min */
84 #define WD_KICK_INTERVAL (60 * HZ)
86 /* Lowest charger voltage is 3.39V -> 0x4E */
87 #define LOW_VOLT_REG 0x4E
89 /* Step up/down delay in us */
90 #define STEP_UDELAY 1000
92 #define CHARGER_STATUS_POLL 10 /* in ms */
94 #define CHG_WD_INTERVAL (60 * HZ)
96 #define AB8500_SW_CONTROL_FALLBACK 0x03
97 /* Wait for enumeration before charing in us */
98 #define WAIT_ACA_RID_ENUMERATION (5 * 1000)
100 /* UsbLineStatus register - usb types */
101 enum ab8500_charger_link_status
{
102 USB_STAT_NOT_CONFIGURED
,
103 USB_STAT_STD_HOST_NC
,
104 USB_STAT_STD_HOST_C_NS
,
105 USB_STAT_STD_HOST_C_S
,
106 USB_STAT_HOST_CHG_NM
,
107 USB_STAT_HOST_CHG_HS
,
108 USB_STAT_HOST_CHG_HS_CHIRP
,
109 USB_STAT_DEDICATED_CHG
,
112 USB_STAT_ACA_RID_C_NM
,
113 USB_STAT_ACA_RID_C_HS
,
114 USB_STAT_ACA_RID_C_HS_CHIRP
,
117 USB_STAT_NOT_VALID_LINK
,
119 USB_STAT_SUP_NO_IDGND_VBUS
,
120 USB_STAT_SUP_IDGND_VBUS
,
121 USB_STAT_CHARGER_LINE_1
,
124 USB_STAT_ACA_DOCK_CHARGER
,
127 enum ab8500_usb_state
{
128 AB8500_BM_USB_STATE_RESET_HS
, /* HighSpeed Reset */
129 AB8500_BM_USB_STATE_RESET_FS
, /* FullSpeed/LowSpeed Reset */
130 AB8500_BM_USB_STATE_CONFIGURED
,
131 AB8500_BM_USB_STATE_SUSPEND
,
132 AB8500_BM_USB_STATE_RESUME
,
133 AB8500_BM_USB_STATE_MAX
,
136 /* VBUS input current limits supported in AB8500 in mA */
137 #define USB_CH_IP_CUR_LVL_0P05 50
138 #define USB_CH_IP_CUR_LVL_0P09 98
139 #define USB_CH_IP_CUR_LVL_0P19 193
140 #define USB_CH_IP_CUR_LVL_0P29 290
141 #define USB_CH_IP_CUR_LVL_0P38 380
142 #define USB_CH_IP_CUR_LVL_0P45 450
143 #define USB_CH_IP_CUR_LVL_0P5 500
144 #define USB_CH_IP_CUR_LVL_0P6 600
145 #define USB_CH_IP_CUR_LVL_0P7 700
146 #define USB_CH_IP_CUR_LVL_0P8 800
147 #define USB_CH_IP_CUR_LVL_0P9 900
148 #define USB_CH_IP_CUR_LVL_1P0 1000
149 #define USB_CH_IP_CUR_LVL_1P1 1100
150 #define USB_CH_IP_CUR_LVL_1P3 1300
151 #define USB_CH_IP_CUR_LVL_1P4 1400
152 #define USB_CH_IP_CUR_LVL_1P5 1500
154 #define VBAT_TRESH_IP_CUR_RED 3800
156 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
157 struct ab8500_charger, usb_chg)
158 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
159 struct ab8500_charger, ac_chg)
162 * struct ab8500_charger_interrupts - ab8500 interupts
163 * @name: name of the interrupt
164 * @isr function pointer to the isr
166 struct ab8500_charger_interrupts
{
168 irqreturn_t (*isr
)(int irq
, void *data
);
171 struct ab8500_charger_info
{
172 int charger_connected
;
180 struct ab8500_charger_event_flags
{
182 bool main_thermal_prot
;
183 bool usb_thermal_prot
;
185 bool usbchargernotok
;
191 struct ab8500_charger_usb_state
{
194 enum ab8500_usb_state state
;
195 enum ab8500_usb_state state_tmp
;
200 * struct ab8500_charger - ab8500 Charger device information
201 * @dev: Pointer to the structure device
202 * @max_usb_in_curr: Max USB charger input current
203 * @vbus_detected: VBUS detected
204 * @vbus_detected_start:
205 * VBUS detected during startup
206 * @ac_conn: This will be true when the AC charger has been plugged
207 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
209 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
211 * @vbat Battery voltage
212 * @old_vbat Previously measured battery voltage
213 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
214 * @autopower Indicate if we should have automatic pwron after pwrloss
215 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
216 * @invalid_charger_detect_state State when forcing AB to use invalid charger
217 * @is_usb_host: Indicate if last detected USB type is host
218 * @is_aca_rid: Incicate if accessory is ACA type
219 * @current_stepping_sessions:
220 * Counter for current stepping sessions
221 * @parent: Pointer to the struct ab8500
222 * @gpadc: Pointer to the struct gpadc
223 * @bm: Platform specific battery management information
224 * @flags: Structure for information about events triggered
225 * @usb_state: Structure for usb stack information
226 * @ac_chg: AC charger power supply
227 * @usb_chg: USB charger power supply
228 * @ac: Structure that holds the AC charger properties
229 * @usb: Structure that holds the USB charger properties
230 * @regu: Pointer to the struct regulator
231 * @charger_wq: Work queue for the IRQs and checking HW state
232 * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
233 * @pm_lock: Lock to prevent system to suspend
234 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
235 * @check_hw_failure_work: Work for checking HW state
236 * @check_usbchgnotok_work: Work for checking USB charger not ok status
237 * @kick_wd_work: Work for kicking the charger watchdog in case
238 * of ABB rev 1.* due to the watchog logic bug
239 * @ac_charger_attached_work: Work for checking if AC charger is still
241 * @usb_charger_attached_work: Work for checking if USB charger is still
243 * @ac_work: Work for checking AC charger connection
244 * @detect_usb_type_work: Work for detecting the USB type connected
245 * @usb_link_status_work: Work for checking the new USB link status
246 * @usb_state_changed_work: Work for checking USB state
247 * @attach_work: Work for detecting USB type
248 * @vbus_drop_end_work: Work for detecting VBUS drop end
249 * @check_main_thermal_prot_work:
250 * Work for checking Main thermal status
251 * @check_usb_thermal_prot_work:
252 * Work for checking USB thermal status
253 * @charger_attached_mutex: For controlling the wakelock
255 struct ab8500_charger
{
259 bool vbus_detected_start
;
265 bool usb_device_is_unrecognised
;
268 int invalid_charger_detect_state
;
271 atomic_t current_stepping_sessions
;
272 struct ab8500
*parent
;
273 struct ab8500_gpadc
*gpadc
;
274 struct abx500_bm_data
*bm
;
275 struct ab8500_charger_event_flags flags
;
276 struct ab8500_charger_usb_state usb_state
;
277 struct ux500_charger ac_chg
;
278 struct ux500_charger usb_chg
;
279 struct ab8500_charger_info ac
;
280 struct ab8500_charger_info usb
;
281 struct regulator
*regu
;
282 struct workqueue_struct
*charger_wq
;
283 struct mutex usb_ipt_crnt_lock
;
284 struct delayed_work check_vbat_work
;
285 struct delayed_work check_hw_failure_work
;
286 struct delayed_work check_usbchgnotok_work
;
287 struct delayed_work kick_wd_work
;
288 struct delayed_work usb_state_changed_work
;
289 struct delayed_work attach_work
;
290 struct delayed_work ac_charger_attached_work
;
291 struct delayed_work usb_charger_attached_work
;
292 struct delayed_work vbus_drop_end_work
;
293 struct work_struct ac_work
;
294 struct work_struct detect_usb_type_work
;
295 struct work_struct usb_link_status_work
;
296 struct work_struct check_main_thermal_prot_work
;
297 struct work_struct check_usb_thermal_prot_work
;
298 struct usb_phy
*usb_phy
;
299 struct notifier_block nb
;
300 struct mutex charger_attached_mutex
;
304 static enum power_supply_property ab8500_charger_ac_props
[] = {
305 POWER_SUPPLY_PROP_HEALTH
,
306 POWER_SUPPLY_PROP_PRESENT
,
307 POWER_SUPPLY_PROP_ONLINE
,
308 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
309 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
310 POWER_SUPPLY_PROP_CURRENT_NOW
,
314 static enum power_supply_property ab8500_charger_usb_props
[] = {
315 POWER_SUPPLY_PROP_HEALTH
,
316 POWER_SUPPLY_PROP_CURRENT_AVG
,
317 POWER_SUPPLY_PROP_PRESENT
,
318 POWER_SUPPLY_PROP_ONLINE
,
319 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
320 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
321 POWER_SUPPLY_PROP_CURRENT_NOW
,
325 * Function for enabling and disabling sw fallback mode
326 * should always be disabled when no charger is connected.
328 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger
*di
,
337 dev_dbg(di
->dev
, "SW Fallback: %d\n", fallback
);
339 if (is_ab8500(di
->parent
)) {
344 bank
= AB8500_SYS_CTRL1_BLOCK
;
345 reg
= AB8500_SW_CONTROL_FALLBACK
;
349 /* read the register containing fallback bit */
350 ret
= abx500_get_register_interruptible(di
->dev
, bank
, reg
, &val
);
352 dev_err(di
->dev
, "%d read failed\n", __LINE__
);
356 if (is_ab8500(di
->parent
)) {
357 /* enable the OPT emulation registers */
358 ret
= abx500_set_register_interruptible(di
->dev
, 0x11, 0x00, 0x2);
360 dev_err(di
->dev
, "%d write failed\n", __LINE__
);
370 /* write back the changed fallback bit value to register */
371 ret
= abx500_set_register_interruptible(di
->dev
, bank
, reg
, val
);
373 dev_err(di
->dev
, "%d write failed\n", __LINE__
);
377 if (is_ab8500(di
->parent
)) {
378 /* disable the set OTP registers again */
379 ret
= abx500_set_register_interruptible(di
->dev
, 0x11, 0x00, 0x0);
381 dev_err(di
->dev
, "%d write failed\n", __LINE__
);
387 * ab8500_power_supply_changed - a wrapper with local extentions for
388 * power_supply_changed
389 * @di: pointer to the ab8500_charger structure
390 * @psy: pointer to power_supply_that have changed.
393 static void ab8500_power_supply_changed(struct ab8500_charger
*di
,
394 struct power_supply
*psy
)
396 if (di
->autopower_cfg
) {
397 if (!di
->usb
.charger_connected
&&
398 !di
->ac
.charger_connected
&&
400 di
->autopower
= false;
401 ab8500_enable_disable_sw_fallback(di
, false);
402 } else if (!di
->autopower
&&
403 (di
->ac
.charger_connected
||
404 di
->usb
.charger_connected
)) {
405 di
->autopower
= true;
406 ab8500_enable_disable_sw_fallback(di
, true);
409 power_supply_changed(psy
);
412 static void ab8500_charger_set_usb_connected(struct ab8500_charger
*di
,
415 if (connected
!= di
->usb
.charger_connected
) {
416 dev_dbg(di
->dev
, "USB connected:%i\n", connected
);
417 di
->usb
.charger_connected
= connected
;
418 sysfs_notify(&di
->usb_chg
.psy
.dev
->kobj
, NULL
, "present");
421 mutex_lock(&di
->charger_attached_mutex
);
422 mutex_unlock(&di
->charger_attached_mutex
);
424 queue_delayed_work(di
->charger_wq
,
425 &di
->usb_charger_attached_work
,
428 cancel_delayed_work_sync(&di
->usb_charger_attached_work
);
429 mutex_lock(&di
->charger_attached_mutex
);
430 mutex_unlock(&di
->charger_attached_mutex
);
436 * ab8500_charger_get_ac_voltage() - get ac charger voltage
437 * @di: pointer to the ab8500_charger structure
439 * Returns ac charger voltage (on success)
441 static int ab8500_charger_get_ac_voltage(struct ab8500_charger
*di
)
445 /* Only measure voltage if the charger is connected */
446 if (di
->ac
.charger_connected
) {
447 vch
= ab8500_gpadc_convert(di
->gpadc
, MAIN_CHARGER_V
);
449 dev_err(di
->dev
, "%s gpadc conv failed,\n", __func__
);
457 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
458 * @di: pointer to the ab8500_charger structure
460 * Returns ac charger CV mode (on success) else error code
462 static int ab8500_charger_ac_cv(struct ab8500_charger
*di
)
467 /* Only check CV mode if the charger is online */
468 if (di
->ac
.charger_online
) {
469 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
470 AB8500_CH_STATUS1_REG
, &val
);
472 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
476 if (val
& MAIN_CH_CV_ON
)
486 * ab8500_charger_get_vbus_voltage() - get vbus voltage
487 * @di: pointer to the ab8500_charger structure
489 * This function returns the vbus voltage.
490 * Returns vbus voltage (on success)
492 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger
*di
)
496 /* Only measure voltage if the charger is connected */
497 if (di
->usb
.charger_connected
) {
498 vch
= ab8500_gpadc_convert(di
->gpadc
, VBUS_V
);
500 dev_err(di
->dev
, "%s gpadc conv failed\n", __func__
);
508 * ab8500_charger_get_usb_current() - get usb charger current
509 * @di: pointer to the ab8500_charger structure
511 * This function returns the usb charger current.
512 * Returns usb current (on success) and error code on failure
514 static int ab8500_charger_get_usb_current(struct ab8500_charger
*di
)
518 /* Only measure current if the charger is online */
519 if (di
->usb
.charger_online
) {
520 ich
= ab8500_gpadc_convert(di
->gpadc
, USB_CHARGER_C
);
522 dev_err(di
->dev
, "%s gpadc conv failed\n", __func__
);
530 * ab8500_charger_get_ac_current() - get ac charger current
531 * @di: pointer to the ab8500_charger structure
533 * This function returns the ac charger current.
534 * Returns ac current (on success) and error code on failure.
536 static int ab8500_charger_get_ac_current(struct ab8500_charger
*di
)
540 /* Only measure current if the charger is online */
541 if (di
->ac
.charger_online
) {
542 ich
= ab8500_gpadc_convert(di
->gpadc
, MAIN_CHARGER_C
);
544 dev_err(di
->dev
, "%s gpadc conv failed\n", __func__
);
552 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
553 * @di: pointer to the ab8500_charger structure
555 * Returns ac charger CV mode (on success) else error code
557 static int ab8500_charger_usb_cv(struct ab8500_charger
*di
)
562 /* Only check CV mode if the charger is online */
563 if (di
->usb
.charger_online
) {
564 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
565 AB8500_CH_USBCH_STAT1_REG
, &val
);
567 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
571 if (val
& USB_CH_CV_ON
)
583 * ab8500_charger_detect_chargers() - Detect the connected chargers
584 * @di: pointer to the ab8500_charger structure
585 * @probe: if probe, don't delay and wait for HW
587 * Returns the type of charger connected.
588 * For USB it will not mean we can actually charge from it
589 * but that there is a USB cable connected that we have to
590 * identify. This is used during startup when we don't get
591 * interrupts of the charger detection
593 * Returns an integer value, that means,
594 * NO_PW_CONN no power supply is connected
595 * AC_PW_CONN if the AC power supply is connected
596 * USB_PW_CONN if the USB power supply is connected
597 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
599 static int ab8500_charger_detect_chargers(struct ab8500_charger
*di
, bool probe
)
601 int result
= NO_PW_CONN
;
605 /* Check for AC charger */
606 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
607 AB8500_CH_STATUS1_REG
, &val
);
609 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
613 if (val
& MAIN_CH_DET
)
616 /* Check for USB charger */
620 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
621 * when disconnecting ACA even though no
622 * charger was connected. Try waiting a little
623 * longer than the 100 ms of VBUS_DET_DBNC100...
627 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
628 AB8500_CH_USBCH_STAT1_REG
, &val
);
630 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
634 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__
,
636 if ((val
& VBUS_DET_DBNC1
) && (val
& VBUS_DET_DBNC100
))
637 result
|= USB_PW_CONN
;
643 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
644 * @di: pointer to the ab8500_charger structure
645 * @link_status: the identified USB type
647 * Get the maximum current that is allowed to be drawn from the host
648 * based on the USB type.
649 * Returns error code in case of failure else 0 on success
651 static int ab8500_charger_max_usb_curr(struct ab8500_charger
*di
,
652 enum ab8500_charger_link_status link_status
)
656 di
->usb_device_is_unrecognised
= false;
659 * Platform only supports USB 2.0.
660 * This means that charging current from USB source
661 * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
662 * should set USB_CH_IP_CUR_LVL_0P5.
665 switch (link_status
) {
666 case USB_STAT_STD_HOST_NC
:
667 case USB_STAT_STD_HOST_C_NS
:
668 case USB_STAT_STD_HOST_C_S
:
669 dev_dbg(di
->dev
, "USB Type - Standard host is "
670 "detected through USB driver\n");
671 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P5
;
672 di
->is_usb_host
= true;
675 case USB_STAT_HOST_CHG_HS_CHIRP
:
676 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P5
;
677 di
->is_usb_host
= true;
680 case USB_STAT_HOST_CHG_HS
:
681 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P5
;
682 di
->is_usb_host
= true;
685 case USB_STAT_ACA_RID_C_HS
:
686 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P9
;
687 di
->is_usb_host
= false;
690 case USB_STAT_ACA_RID_A
:
692 * Dedicated charger level minus maximum current accessory
693 * can consume (900mA). Closest level is 500mA
695 dev_dbg(di
->dev
, "USB_STAT_ACA_RID_A detected\n");
696 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P5
;
697 di
->is_usb_host
= false;
700 case USB_STAT_ACA_RID_B
:
702 * Dedicated charger level minus 120mA (20mA for ACA and
703 * 100mA for potential accessory). Closest level is 1300mA
705 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_1P3
;
706 dev_dbg(di
->dev
, "USB Type - 0x%02x MaxCurr: %d", link_status
,
707 di
->max_usb_in_curr
);
708 di
->is_usb_host
= false;
711 case USB_STAT_HOST_CHG_NM
:
712 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P5
;
713 di
->is_usb_host
= true;
716 case USB_STAT_DEDICATED_CHG
:
717 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_1P5
;
718 di
->is_usb_host
= false;
721 case USB_STAT_ACA_RID_C_HS_CHIRP
:
722 case USB_STAT_ACA_RID_C_NM
:
723 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_1P5
;
724 di
->is_usb_host
= false;
727 case USB_STAT_NOT_CONFIGURED
:
728 if (di
->vbus_detected
) {
729 di
->usb_device_is_unrecognised
= true;
730 dev_dbg(di
->dev
, "USB Type - Legacy charger.\n");
731 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_1P5
;
734 case USB_STAT_HM_IDGND
:
735 dev_err(di
->dev
, "USB Type - Charging not allowed\n");
736 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P05
;
739 case USB_STAT_RESERVED
:
740 if (is_ab8500(di
->parent
)) {
741 di
->flags
.vbus_collapse
= true;
742 dev_err(di
->dev
, "USB Type - USB_STAT_RESERVED "
743 "VBUS has collapsed\n");
747 if (is_ab9540(di
->parent
) || is_ab8505(di
->parent
)) {
748 dev_dbg(di
->dev
, "USB Type - Charging not allowed\n");
749 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P05
;
750 dev_dbg(di
->dev
, "USB Type - 0x%02x MaxCurr: %d",
751 link_status
, di
->max_usb_in_curr
);
756 case USB_STAT_CARKIT_1
:
757 case USB_STAT_CARKIT_2
:
758 case USB_STAT_ACA_DOCK_CHARGER
:
759 case USB_STAT_CHARGER_LINE_1
:
760 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P5
;
761 dev_dbg(di
->dev
, "USB Type - 0x%02x MaxCurr: %d", link_status
,
762 di
->max_usb_in_curr
);
763 case USB_STAT_NOT_VALID_LINK
:
764 dev_err(di
->dev
, "USB Type invalid - try charging anyway\n");
765 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P5
;
769 dev_err(di
->dev
, "USB Type - Unknown\n");
770 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P05
;
775 dev_dbg(di
->dev
, "USB Type - 0x%02x MaxCurr: %d",
776 link_status
, di
->max_usb_in_curr
);
782 * ab8500_charger_read_usb_type() - read the type of usb connected
783 * @di: pointer to the ab8500_charger structure
785 * Detect the type of the plugged USB
786 * Returns error code in case of failure else 0 on success
788 static int ab8500_charger_read_usb_type(struct ab8500_charger
*di
)
793 ret
= abx500_get_register_interruptible(di
->dev
,
794 AB8500_INTERRUPT
, AB8500_IT_SOURCE21_REG
, &val
);
796 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
799 if (is_ab8500(di
->parent
)) {
800 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_USB
,
801 AB8500_USB_LINE_STAT_REG
, &val
);
803 if (is_ab9540(di
->parent
) || is_ab8505(di
->parent
))
804 ret
= abx500_get_register_interruptible(di
->dev
,
805 AB8500_USB
, AB8500_USB_LINK1_STAT_REG
, &val
);
808 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
812 /* get the USB type */
813 val
= (val
& AB8500_USB_LINK_STATUS
) >> 3;
814 ret
= ab8500_charger_max_usb_curr(di
,
815 (enum ab8500_charger_link_status
) val
);
821 * ab8500_charger_detect_usb_type() - get the type of usb connected
822 * @di: pointer to the ab8500_charger structure
824 * Detect the type of the plugged USB
825 * Returns error code in case of failure else 0 on success
827 static int ab8500_charger_detect_usb_type(struct ab8500_charger
*di
)
833 * On getting the VBUS rising edge detect interrupt there
834 * is a 250ms delay after which the register UsbLineStatus
835 * is filled with valid data.
837 for (i
= 0; i
< 10; i
++) {
839 ret
= abx500_get_register_interruptible(di
->dev
,
840 AB8500_INTERRUPT
, AB8500_IT_SOURCE21_REG
,
842 dev_dbg(di
->dev
, "%s AB8500_IT_SOURCE21_REG %x\n",
845 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
849 if (is_ab8500(di
->parent
))
850 ret
= abx500_get_register_interruptible(di
->dev
,
851 AB8500_USB
, AB8500_USB_LINE_STAT_REG
, &val
);
853 ret
= abx500_get_register_interruptible(di
->dev
,
854 AB8500_USB
, AB8500_USB_LINK1_STAT_REG
, &val
);
856 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
859 dev_dbg(di
->dev
, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__
,
862 * Until the IT source register is read the UsbLineStatus
863 * register is not updated, hence doing the same
867 /* get the USB type */
868 val
= (val
& AB8500_USB_LINK_STATUS
) >> 3;
872 ret
= ab8500_charger_max_usb_curr(di
,
873 (enum ab8500_charger_link_status
) val
);
879 * This array maps the raw hex value to charger voltage used by the AB8500
880 * Values taken from the UM0836
882 static int ab8500_charger_voltage_map
[] = {
964 * This array maps the raw hex value to charger current used by the AB8500
965 * Values taken from the UM0836
967 static int ab8500_charger_current_map
[] = {
986 * This array maps the raw hex value to VBUS input current used by the AB8500
987 * Values taken from the UM0836
989 static int ab8500_charger_vbus_in_curr_map
[] = {
990 USB_CH_IP_CUR_LVL_0P05
,
991 USB_CH_IP_CUR_LVL_0P09
,
992 USB_CH_IP_CUR_LVL_0P19
,
993 USB_CH_IP_CUR_LVL_0P29
,
994 USB_CH_IP_CUR_LVL_0P38
,
995 USB_CH_IP_CUR_LVL_0P45
,
996 USB_CH_IP_CUR_LVL_0P5
,
997 USB_CH_IP_CUR_LVL_0P6
,
998 USB_CH_IP_CUR_LVL_0P7
,
999 USB_CH_IP_CUR_LVL_0P8
,
1000 USB_CH_IP_CUR_LVL_0P9
,
1001 USB_CH_IP_CUR_LVL_1P0
,
1002 USB_CH_IP_CUR_LVL_1P1
,
1003 USB_CH_IP_CUR_LVL_1P3
,
1004 USB_CH_IP_CUR_LVL_1P4
,
1005 USB_CH_IP_CUR_LVL_1P5
,
1008 static int ab8500_voltage_to_regval(int voltage
)
1012 /* Special case for voltage below 3.5V */
1013 if (voltage
< ab8500_charger_voltage_map
[0])
1014 return LOW_VOLT_REG
;
1016 for (i
= 1; i
< ARRAY_SIZE(ab8500_charger_voltage_map
); i
++) {
1017 if (voltage
< ab8500_charger_voltage_map
[i
])
1021 /* If not last element, return error */
1022 i
= ARRAY_SIZE(ab8500_charger_voltage_map
) - 1;
1023 if (voltage
== ab8500_charger_voltage_map
[i
])
1029 static int ab8500_current_to_regval(int curr
)
1033 if (curr
< ab8500_charger_current_map
[0])
1036 for (i
= 0; i
< ARRAY_SIZE(ab8500_charger_current_map
); i
++) {
1037 if (curr
< ab8500_charger_current_map
[i
])
1041 /* If not last element, return error */
1042 i
= ARRAY_SIZE(ab8500_charger_current_map
) - 1;
1043 if (curr
== ab8500_charger_current_map
[i
])
1049 static int ab8500_vbus_in_curr_to_regval(int curr
)
1053 if (curr
< ab8500_charger_vbus_in_curr_map
[0])
1056 for (i
= 0; i
< ARRAY_SIZE(ab8500_charger_vbus_in_curr_map
); i
++) {
1057 if (curr
< ab8500_charger_vbus_in_curr_map
[i
])
1061 /* If not last element, return error */
1062 i
= ARRAY_SIZE(ab8500_charger_vbus_in_curr_map
) - 1;
1063 if (curr
== ab8500_charger_vbus_in_curr_map
[i
])
1070 * ab8500_charger_get_usb_cur() - get usb current
1071 * @di: pointer to the ab8500_charger structre
1073 * The usb stack provides the maximum current that can be drawn from
1074 * the standard usb host. This will be in mA.
1075 * This function converts current in mA to a value that can be written
1076 * to the register. Returns -1 if charging is not allowed
1078 static int ab8500_charger_get_usb_cur(struct ab8500_charger
*di
)
1080 switch (di
->usb_state
.usb_current
) {
1082 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P09
;
1085 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P19
;
1088 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P29
;
1091 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P38
;
1094 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P5
;
1097 di
->max_usb_in_curr
= USB_CH_IP_CUR_LVL_0P05
;
1105 * ab8500_charger_set_current() - set charger current
1106 * @di: pointer to the ab8500_charger structure
1107 * @ich: charger current, in mA
1108 * @reg: select what charger register to set
1110 * Set charger current.
1111 * There is no state machine in the AB to step up/down the charger
1112 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1113 * charging is started. Instead we need to implement
1114 * this charger current step-up/down here.
1115 * Returns error code in case of failure else 0(on success)
1117 static int ab8500_charger_set_current(struct ab8500_charger
*di
,
1121 int auto_curr_index
, curr_index
, prev_curr_index
, shift_value
, i
;
1124 bool no_stepping
= false;
1126 atomic_inc(&di
->current_stepping_sessions
);
1128 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
1131 dev_err(di
->dev
, "%s read failed\n", __func__
);
1132 goto exit_set_current
;
1136 case AB8500_MCH_IPT_CURLVL_REG
:
1137 shift_value
= MAIN_CH_INPUT_CURR_SHIFT
;
1138 prev_curr_index
= (reg_value
>> shift_value
);
1139 curr_index
= ab8500_current_to_regval(ich
);
1140 step_udelay
= STEP_UDELAY
;
1141 if (!di
->ac
.charger_connected
)
1144 case AB8500_USBCH_IPT_CRNTLVL_REG
:
1145 shift_value
= VBUS_IN_CURR_LIM_SHIFT
;
1146 prev_curr_index
= (reg_value
>> shift_value
);
1147 curr_index
= ab8500_vbus_in_curr_to_regval(ich
);
1148 step_udelay
= STEP_UDELAY
* 100;
1150 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
1151 AB8500_CH_USBCH_STAT2_REG
, ®_value
);
1153 dev_err(di
->dev
, "%s read failed\n", __func__
);
1154 goto exit_set_current
;
1157 reg_value
>> AUTO_VBUS_IN_CURR_LIM_SHIFT
;
1159 dev_dbg(di
->dev
, "%s Auto VBUS curr is %d mA\n",
1161 ab8500_charger_vbus_in_curr_map
[auto_curr_index
]);
1163 prev_curr_index
= min(prev_curr_index
, auto_curr_index
);
1165 if (!di
->usb
.charger_connected
)
1168 case AB8500_CH_OPT_CRNTLVL_REG
:
1170 prev_curr_index
= (reg_value
>> shift_value
);
1171 curr_index
= ab8500_current_to_regval(ich
);
1172 step_udelay
= STEP_UDELAY
;
1173 if (curr_index
&& (curr_index
- prev_curr_index
) > 1)
1176 if (!di
->usb
.charger_connected
&& !di
->ac
.charger_connected
)
1181 dev_err(di
->dev
, "%s current register not valid\n", __func__
);
1183 goto exit_set_current
;
1186 if (curr_index
< 0) {
1187 dev_err(di
->dev
, "requested current limit out-of-range\n");
1189 goto exit_set_current
;
1192 /* only update current if it's been changed */
1193 if (prev_curr_index
== curr_index
) {
1194 dev_dbg(di
->dev
, "%s current not changed for reg: 0x%02x\n",
1197 goto exit_set_current
;
1200 dev_dbg(di
->dev
, "%s set charger current: %d mA for reg: 0x%02x\n",
1201 __func__
, ich
, reg
);
1204 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1205 reg
, (u8
)curr_index
<< shift_value
);
1207 dev_err(di
->dev
, "%s write failed\n", __func__
);
1208 } else if (prev_curr_index
> curr_index
) {
1209 for (i
= prev_curr_index
- 1; i
>= curr_index
; i
--) {
1210 dev_dbg(di
->dev
, "curr change_1 to: %x for 0x%02x\n",
1211 (u8
) i
<< shift_value
, reg
);
1212 ret
= abx500_set_register_interruptible(di
->dev
,
1213 AB8500_CHARGER
, reg
, (u8
)i
<< shift_value
);
1215 dev_err(di
->dev
, "%s write failed\n", __func__
);
1216 goto exit_set_current
;
1218 if (i
!= curr_index
)
1219 usleep_range(step_udelay
, step_udelay
* 2);
1222 for (i
= prev_curr_index
+ 1; i
<= curr_index
; i
++) {
1223 dev_dbg(di
->dev
, "curr change_2 to: %x for 0x%02x\n",
1224 (u8
)i
<< shift_value
, reg
);
1225 ret
= abx500_set_register_interruptible(di
->dev
,
1226 AB8500_CHARGER
, reg
, (u8
)i
<< shift_value
);
1228 dev_err(di
->dev
, "%s write failed\n", __func__
);
1229 goto exit_set_current
;
1231 if (i
!= curr_index
)
1232 usleep_range(step_udelay
, step_udelay
* 2);
1237 atomic_dec(&di
->current_stepping_sessions
);
1243 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1244 * @di: pointer to the ab8500_charger structure
1245 * @ich_in: charger input current limit
1247 * Sets the current that can be drawn from the USB host
1248 * Returns error code in case of failure else 0(on success)
1250 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger
*di
,
1256 /* We should always use to lowest current limit */
1257 min_value
= min(di
->bm
->chg_params
->usb_curr_max
, ich_in
);
1259 switch (min_value
) {
1261 if (di
->vbat
< VBAT_TRESH_IP_CUR_RED
)
1262 min_value
= USB_CH_IP_CUR_LVL_0P05
;
1265 if (di
->vbat
< VBAT_TRESH_IP_CUR_RED
)
1266 min_value
= USB_CH_IP_CUR_LVL_0P45
;
1272 dev_info(di
->dev
, "VBUS input current limit set to %d mA\n", min_value
);
1274 mutex_lock(&di
->usb_ipt_crnt_lock
);
1275 ret
= ab8500_charger_set_current(di
, min_value
,
1276 AB8500_USBCH_IPT_CRNTLVL_REG
);
1277 mutex_unlock(&di
->usb_ipt_crnt_lock
);
1283 * ab8500_charger_set_main_in_curr() - set main charger input current
1284 * @di: pointer to the ab8500_charger structure
1285 * @ich_in: input charger current, in mA
1287 * Set main charger input current.
1288 * Returns error code in case of failure else 0(on success)
1290 static int ab8500_charger_set_main_in_curr(struct ab8500_charger
*di
,
1293 return ab8500_charger_set_current(di
, ich_in
,
1294 AB8500_MCH_IPT_CURLVL_REG
);
1298 * ab8500_charger_set_output_curr() - set charger output current
1299 * @di: pointer to the ab8500_charger structure
1300 * @ich_out: output charger current, in mA
1302 * Set charger output current.
1303 * Returns error code in case of failure else 0(on success)
1305 static int ab8500_charger_set_output_curr(struct ab8500_charger
*di
,
1308 return ab8500_charger_set_current(di
, ich_out
,
1309 AB8500_CH_OPT_CRNTLVL_REG
);
1313 * ab8500_charger_led_en() - turn on/off chargign led
1314 * @di: pointer to the ab8500_charger structure
1315 * @on: flag to turn on/off the chargign led
1317 * Power ON/OFF charging LED indication
1318 * Returns error code in case of failure else 0(on success)
1320 static int ab8500_charger_led_en(struct ab8500_charger
*di
, int on
)
1325 /* Power ON charging LED indicator, set LED current to 5mA */
1326 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1327 AB8500_LED_INDICATOR_PWM_CTRL
,
1328 (LED_IND_CUR_5MA
| LED_INDICATOR_PWM_ENA
));
1330 dev_err(di
->dev
, "Power ON LED failed\n");
1333 /* LED indicator PWM duty cycle 252/256 */
1334 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1335 AB8500_LED_INDICATOR_PWM_DUTY
,
1336 LED_INDICATOR_PWM_DUTY_252_256
);
1338 dev_err(di
->dev
, "Set LED PWM duty cycle failed\n");
1342 /* Power off charging LED indicator */
1343 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1344 AB8500_LED_INDICATOR_PWM_CTRL
,
1345 LED_INDICATOR_PWM_DIS
);
1347 dev_err(di
->dev
, "Power-off LED failed\n");
1356 * ab8500_charger_ac_en() - enable or disable ac charging
1357 * @di: pointer to the ab8500_charger structure
1358 * @enable: enable/disable flag
1359 * @vset: charging voltage
1360 * @iset: charging current
1362 * Enable/Disable AC/Mains charging and turns on/off the charging led
1365 static int ab8500_charger_ac_en(struct ux500_charger
*charger
,
1366 int enable
, int vset
, int iset
)
1371 int input_curr_index
;
1374 struct ab8500_charger
*di
= to_ab8500_charger_ac_device_info(charger
);
1377 /* Check if AC is connected */
1378 if (!di
->ac
.charger_connected
) {
1379 dev_err(di
->dev
, "AC charger not connected\n");
1383 /* Enable AC charging */
1384 dev_dbg(di
->dev
, "Enable AC: %dmV %dmA\n", vset
, iset
);
1387 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1388 * will be triggered everytime we enable the VDD ADC supply.
1389 * This will turn off charging for a short while.
1390 * It can be avoided by having the supply on when
1391 * there is a charger enabled. Normally the VDD ADC supply
1392 * is enabled everytime a GPADC conversion is triggered. We will
1393 * force it to be enabled from this driver to have
1394 * the GPADC module independant of the AB8500 chargers
1396 if (!di
->vddadc_en_ac
) {
1397 regulator_enable(di
->regu
);
1398 di
->vddadc_en_ac
= true;
1401 /* Check if the requested voltage or current is valid */
1402 volt_index
= ab8500_voltage_to_regval(vset
);
1403 curr_index
= ab8500_current_to_regval(iset
);
1404 input_curr_index
= ab8500_current_to_regval(
1405 di
->bm
->chg_params
->ac_curr_max
);
1406 if (volt_index
< 0 || curr_index
< 0 || input_curr_index
< 0) {
1408 "Charger voltage or current too high, "
1409 "charging not started\n");
1413 /* ChVoltLevel: maximum battery charging voltage */
1414 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1415 AB8500_CH_VOLT_LVL_REG
, (u8
) volt_index
);
1417 dev_err(di
->dev
, "%s write failed\n", __func__
);
1420 /* MainChInputCurr: current that can be drawn from the charger*/
1421 ret
= ab8500_charger_set_main_in_curr(di
,
1422 di
->bm
->chg_params
->ac_curr_max
);
1424 dev_err(di
->dev
, "%s Failed to set MainChInputCurr\n",
1428 /* ChOutputCurentLevel: protected output current */
1429 ret
= ab8500_charger_set_output_curr(di
, iset
);
1431 dev_err(di
->dev
, "%s "
1432 "Failed to set ChOutputCurentLevel\n",
1437 /* Check if VBAT overshoot control should be enabled */
1438 if (!di
->bm
->enable_overshoot
)
1439 overshoot
= MAIN_CH_NO_OVERSHOOT_ENA_N
;
1441 /* Enable Main Charger */
1442 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1443 AB8500_MCH_CTRL1
, MAIN_CH_ENA
| overshoot
);
1445 dev_err(di
->dev
, "%s write failed\n", __func__
);
1449 /* Power on charging LED indication */
1450 ret
= ab8500_charger_led_en(di
, true);
1452 dev_err(di
->dev
, "failed to enable LED\n");
1454 di
->ac
.charger_online
= 1;
1456 /* Disable AC charging */
1457 if (is_ab8500_1p1_or_earlier(di
->parent
)) {
1459 * For ABB revision 1.0 and 1.1 there is a bug in the
1460 * watchdog logic. That means we have to continously
1461 * kick the charger watchdog even when no charger is
1462 * connected. This is only valid once the AC charger
1463 * has been enabled. This is a bug that is not handled
1464 * by the algorithm and the watchdog have to be kicked
1465 * by the charger driver when the AC charger
1469 queue_delayed_work(di
->charger_wq
,
1471 round_jiffies(WD_KICK_INTERVAL
));
1475 * We can't turn off charging completely
1476 * due to a bug in AB8500 cut1.
1477 * If we do, charging will not start again.
1478 * That is why we set the lowest voltage
1479 * and current possible
1481 ret
= abx500_set_register_interruptible(di
->dev
,
1483 AB8500_CH_VOLT_LVL_REG
, CH_VOL_LVL_3P5
);
1486 "%s write failed\n", __func__
);
1490 ret
= ab8500_charger_set_output_curr(di
, 0);
1492 dev_err(di
->dev
, "%s "
1493 "Failed to set ChOutputCurentLevel\n",
1498 ret
= abx500_set_register_interruptible(di
->dev
,
1500 AB8500_MCH_CTRL1
, 0);
1503 "%s write failed\n", __func__
);
1508 ret
= ab8500_charger_led_en(di
, false);
1510 dev_err(di
->dev
, "failed to disable LED\n");
1512 di
->ac
.charger_online
= 0;
1513 di
->ac
.wd_expired
= false;
1515 /* Disable regulator if enabled */
1516 if (di
->vddadc_en_ac
) {
1517 regulator_disable(di
->regu
);
1518 di
->vddadc_en_ac
= false;
1521 dev_dbg(di
->dev
, "%s Disabled AC charging\n", __func__
);
1523 ab8500_power_supply_changed(di
, &di
->ac_chg
.psy
);
1529 * ab8500_charger_usb_en() - enable usb charging
1530 * @di: pointer to the ab8500_charger structure
1531 * @enable: enable/disable flag
1532 * @vset: charging voltage
1533 * @ich_out: charger output current
1535 * Enable/Disable USB charging and turns on/off the charging led respectively.
1536 * Returns error code in case of failure else 0(on success)
1538 static int ab8500_charger_usb_en(struct ux500_charger
*charger
,
1539 int enable
, int vset
, int ich_out
)
1546 struct ab8500_charger
*di
= to_ab8500_charger_usb_device_info(charger
);
1549 /* Check if USB is connected */
1550 if (!di
->usb
.charger_connected
) {
1551 dev_err(di
->dev
, "USB charger not connected\n");
1556 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1557 * will be triggered everytime we enable the VDD ADC supply.
1558 * This will turn off charging for a short while.
1559 * It can be avoided by having the supply on when
1560 * there is a charger enabled. Normally the VDD ADC supply
1561 * is enabled everytime a GPADC conversion is triggered. We will
1562 * force it to be enabled from this driver to have
1563 * the GPADC module independant of the AB8500 chargers
1565 if (!di
->vddadc_en_usb
) {
1566 regulator_enable(di
->regu
);
1567 di
->vddadc_en_usb
= true;
1570 /* Enable USB charging */
1571 dev_dbg(di
->dev
, "Enable USB: %dmV %dmA\n", vset
, ich_out
);
1573 /* Check if the requested voltage or current is valid */
1574 volt_index
= ab8500_voltage_to_regval(vset
);
1575 curr_index
= ab8500_current_to_regval(ich_out
);
1576 if (volt_index
< 0 || curr_index
< 0) {
1578 "Charger voltage or current too high, "
1579 "charging not started\n");
1583 /* ChVoltLevel: max voltage upto which battery can be charged */
1584 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1585 AB8500_CH_VOLT_LVL_REG
, (u8
) volt_index
);
1587 dev_err(di
->dev
, "%s write failed\n", __func__
);
1590 /* Check if VBAT overshoot control should be enabled */
1591 if (!di
->bm
->enable_overshoot
)
1592 overshoot
= USB_CHG_NO_OVERSHOOT_ENA_N
;
1594 /* Enable USB Charger */
1596 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
1597 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1598 AB8500_USBCH_CTRL1_REG
, USB_CH_ENA
| overshoot
);
1600 dev_err(di
->dev
, "%s write failed\n", __func__
);
1604 /* If success power on charging LED indication */
1605 ret
= ab8500_charger_led_en(di
, true);
1607 dev_err(di
->dev
, "failed to enable LED\n");
1609 di
->usb
.charger_online
= 1;
1611 /* USBChInputCurr: current that can be drawn from the usb */
1612 ret
= ab8500_charger_set_vbus_in_curr(di
, di
->max_usb_in_curr
);
1614 dev_err(di
->dev
, "setting USBChInputCurr failed\n");
1618 /* ChOutputCurentLevel: protected output current */
1619 ret
= ab8500_charger_set_output_curr(di
, ich_out
);
1621 dev_err(di
->dev
, "%s "
1622 "Failed to set ChOutputCurentLevel\n",
1627 queue_delayed_work(di
->charger_wq
, &di
->check_vbat_work
, HZ
);
1630 /* Disable USB charging */
1631 dev_dbg(di
->dev
, "%s Disabled USB charging\n", __func__
);
1632 ret
= abx500_set_register_interruptible(di
->dev
,
1634 AB8500_USBCH_CTRL1_REG
, 0);
1637 "%s write failed\n", __func__
);
1641 ret
= ab8500_charger_led_en(di
, false);
1643 dev_err(di
->dev
, "failed to disable LED\n");
1644 /* USBChInputCurr: current that can be drawn from the usb */
1645 ret
= ab8500_charger_set_vbus_in_curr(di
, 0);
1647 dev_err(di
->dev
, "setting USBChInputCurr failed\n");
1651 /* ChOutputCurentLevel: protected output current */
1652 ret
= ab8500_charger_set_output_curr(di
, 0);
1654 dev_err(di
->dev
, "%s "
1655 "Failed to reset ChOutputCurentLevel\n",
1659 di
->usb
.charger_online
= 0;
1660 di
->usb
.wd_expired
= false;
1662 /* Disable regulator if enabled */
1663 if (di
->vddadc_en_usb
) {
1664 regulator_disable(di
->regu
);
1665 di
->vddadc_en_usb
= false;
1668 dev_dbg(di
->dev
, "%s Disabled USB charging\n", __func__
);
1670 /* Cancel any pending Vbat check work */
1671 if (delayed_work_pending(&di
->check_vbat_work
))
1672 cancel_delayed_work(&di
->check_vbat_work
);
1675 ab8500_power_supply_changed(di
, &di
->usb_chg
.psy
);
1681 * ab8500_charger_watchdog_kick() - kick charger watchdog
1682 * @di: pointer to the ab8500_charger structure
1684 * Kick charger watchdog
1685 * Returns error code in case of failure else 0(on success)
1687 static int ab8500_charger_watchdog_kick(struct ux500_charger
*charger
)
1690 struct ab8500_charger
*di
;
1692 if (charger
->psy
.type
== POWER_SUPPLY_TYPE_MAINS
)
1693 di
= to_ab8500_charger_ac_device_info(charger
);
1694 else if (charger
->psy
.type
== POWER_SUPPLY_TYPE_USB
)
1695 di
= to_ab8500_charger_usb_device_info(charger
);
1699 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1700 AB8500_CHARG_WD_CTRL
, CHARG_WD_KICK
);
1702 dev_err(di
->dev
, "Failed to kick WD!\n");
1708 * ab8500_charger_update_charger_current() - update charger current
1709 * @di: pointer to the ab8500_charger structure
1711 * Update the charger output current for the specified charger
1712 * Returns error code in case of failure else 0(on success)
1714 static int ab8500_charger_update_charger_current(struct ux500_charger
*charger
,
1718 struct ab8500_charger
*di
;
1720 if (charger
->psy
.type
== POWER_SUPPLY_TYPE_MAINS
)
1721 di
= to_ab8500_charger_ac_device_info(charger
);
1722 else if (charger
->psy
.type
== POWER_SUPPLY_TYPE_USB
)
1723 di
= to_ab8500_charger_usb_device_info(charger
);
1727 ret
= ab8500_charger_set_output_curr(di
, ich_out
);
1729 dev_err(di
->dev
, "%s "
1730 "Failed to set ChOutputCurentLevel\n",
1735 /* Reset the main and usb drop input current measurement counter */
1736 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1737 AB8500_CHARGER_CTRL
,
1740 dev_err(di
->dev
, "%s write failed\n", __func__
);
1747 static int ab8500_charger_get_ext_psy_data(struct device
*dev
, void *data
)
1749 struct power_supply
*psy
;
1750 struct power_supply
*ext
;
1751 struct ab8500_charger
*di
;
1752 union power_supply_propval ret
;
1754 bool psy_found
= false;
1755 struct ux500_charger
*usb_chg
;
1757 usb_chg
= (struct ux500_charger
*)data
;
1758 psy
= &usb_chg
->psy
;
1760 di
= to_ab8500_charger_usb_device_info(usb_chg
);
1762 ext
= dev_get_drvdata(dev
);
1764 /* For all psy where the driver name appears in any supplied_to */
1765 for (i
= 0; i
< ext
->num_supplicants
; i
++) {
1766 if (!strcmp(ext
->supplied_to
[i
], psy
->name
))
1773 /* Go through all properties for the psy */
1774 for (j
= 0; j
< ext
->num_properties
; j
++) {
1775 enum power_supply_property prop
;
1776 prop
= ext
->properties
[j
];
1778 if (ext
->get_property(ext
, prop
, &ret
))
1782 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
1783 switch (ext
->type
) {
1784 case POWER_SUPPLY_TYPE_BATTERY
:
1785 di
->vbat
= ret
.intval
/ 1000;
1799 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1800 * @work pointer to the work_struct structure
1802 * Due to a asic bug it is necessary to lower the input current to the vbus
1803 * charger when charging with at some specific levels. This issue is only valid
1804 * for below a certain battery voltage. This function makes sure that the
1805 * the allowed current limit isn't exceeded.
1807 static void ab8500_charger_check_vbat_work(struct work_struct
*work
)
1810 struct ab8500_charger
*di
= container_of(work
,
1811 struct ab8500_charger
, check_vbat_work
.work
);
1813 class_for_each_device(power_supply_class
, NULL
,
1814 &di
->usb_chg
.psy
, ab8500_charger_get_ext_psy_data
);
1816 /* First run old_vbat is 0. */
1817 if (di
->old_vbat
== 0)
1818 di
->old_vbat
= di
->vbat
;
1820 if (!((di
->old_vbat
<= VBAT_TRESH_IP_CUR_RED
&&
1821 di
->vbat
<= VBAT_TRESH_IP_CUR_RED
) ||
1822 (di
->old_vbat
> VBAT_TRESH_IP_CUR_RED
&&
1823 di
->vbat
> VBAT_TRESH_IP_CUR_RED
))) {
1825 dev_dbg(di
->dev
, "Vbat did cross threshold, curr: %d, new: %d,"
1826 " old: %d\n", di
->max_usb_in_curr
, di
->vbat
,
1828 ab8500_charger_set_vbus_in_curr(di
, di
->max_usb_in_curr
);
1829 power_supply_changed(&di
->usb_chg
.psy
);
1832 di
->old_vbat
= di
->vbat
;
1835 * No need to check the battery voltage every second when not close to
1838 if (di
->vbat
< (VBAT_TRESH_IP_CUR_RED
+ 100) &&
1839 (di
->vbat
> (VBAT_TRESH_IP_CUR_RED
- 100)))
1842 queue_delayed_work(di
->charger_wq
, &di
->check_vbat_work
, t
* HZ
);
1846 * ab8500_charger_check_hw_failure_work() - check main charger failure
1847 * @work: pointer to the work_struct structure
1849 * Work queue function for checking the main charger status
1851 static void ab8500_charger_check_hw_failure_work(struct work_struct
*work
)
1856 struct ab8500_charger
*di
= container_of(work
,
1857 struct ab8500_charger
, check_hw_failure_work
.work
);
1859 /* Check if the status bits for HW failure is still active */
1860 if (di
->flags
.mainextchnotok
) {
1861 ret
= abx500_get_register_interruptible(di
->dev
,
1862 AB8500_CHARGER
, AB8500_CH_STATUS2_REG
, ®_value
);
1864 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
1867 if (!(reg_value
& MAIN_CH_NOK
)) {
1868 di
->flags
.mainextchnotok
= false;
1869 ab8500_power_supply_changed(di
, &di
->ac_chg
.psy
);
1872 if (di
->flags
.vbus_ovv
) {
1873 ret
= abx500_get_register_interruptible(di
->dev
,
1874 AB8500_CHARGER
, AB8500_CH_USBCH_STAT2_REG
,
1877 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
1880 if (!(reg_value
& VBUS_OVV_TH
)) {
1881 di
->flags
.vbus_ovv
= false;
1882 ab8500_power_supply_changed(di
, &di
->usb_chg
.psy
);
1885 /* If we still have a failure, schedule a new check */
1886 if (di
->flags
.mainextchnotok
|| di
->flags
.vbus_ovv
) {
1887 queue_delayed_work(di
->charger_wq
,
1888 &di
->check_hw_failure_work
, round_jiffies(HZ
));
1893 * ab8500_charger_kick_watchdog_work() - kick the watchdog
1894 * @work: pointer to the work_struct structure
1896 * Work queue function for kicking the charger watchdog.
1898 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
1899 * logic. That means we have to continously kick the charger
1900 * watchdog even when no charger is connected. This is only
1901 * valid once the AC charger has been enabled. This is
1902 * a bug that is not handled by the algorithm and the
1903 * watchdog have to be kicked by the charger driver
1904 * when the AC charger is disabled
1906 static void ab8500_charger_kick_watchdog_work(struct work_struct
*work
)
1910 struct ab8500_charger
*di
= container_of(work
,
1911 struct ab8500_charger
, kick_wd_work
.work
);
1913 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1914 AB8500_CHARG_WD_CTRL
, CHARG_WD_KICK
);
1916 dev_err(di
->dev
, "Failed to kick WD!\n");
1918 /* Schedule a new watchdog kick */
1919 queue_delayed_work(di
->charger_wq
,
1920 &di
->kick_wd_work
, round_jiffies(WD_KICK_INTERVAL
));
1924 * ab8500_charger_ac_work() - work to get and set main charger status
1925 * @work: pointer to the work_struct structure
1927 * Work queue function for checking the main charger status
1929 static void ab8500_charger_ac_work(struct work_struct
*work
)
1933 struct ab8500_charger
*di
= container_of(work
,
1934 struct ab8500_charger
, ac_work
);
1937 * Since we can't be sure that the events are received
1938 * synchronously, we have the check if the main charger is
1939 * connected by reading the status register
1941 ret
= ab8500_charger_detect_chargers(di
, false);
1945 if (ret
& AC_PW_CONN
) {
1946 di
->ac
.charger_connected
= 1;
1949 di
->ac
.charger_connected
= 0;
1952 ab8500_power_supply_changed(di
, &di
->ac_chg
.psy
);
1953 sysfs_notify(&di
->ac_chg
.psy
.dev
->kobj
, NULL
, "present");
1956 static void ab8500_charger_usb_attached_work(struct work_struct
*work
)
1958 struct ab8500_charger
*di
= container_of(work
,
1959 struct ab8500_charger
,
1960 usb_charger_attached_work
.work
);
1961 int usbch
= (USB_CH_VBUSDROP
| USB_CH_VBUSDETDBNC
);
1965 for (i
= 0; i
< 10; i
++) {
1966 ret
= abx500_get_register_interruptible(di
->dev
,
1968 AB8500_CH_USBCH_STAT1_REG
,
1971 dev_err(di
->dev
, "ab8500 read failed %d\n", __LINE__
);
1974 if ((statval
& usbch
) != usbch
)
1977 msleep(CHARGER_STATUS_POLL
);
1980 ab8500_charger_usb_en(&di
->usb_chg
, 0, 0, 0);
1982 mutex_lock(&di
->charger_attached_mutex
);
1983 mutex_unlock(&di
->charger_attached_mutex
);
1988 queue_delayed_work(di
->charger_wq
,
1989 &di
->usb_charger_attached_work
,
1993 static void ab8500_charger_ac_attached_work(struct work_struct
*work
)
1996 struct ab8500_charger
*di
= container_of(work
,
1997 struct ab8500_charger
,
1998 ac_charger_attached_work
.work
);
1999 int mainch
= (MAIN_CH_STATUS2_MAINCHGDROP
|
2000 MAIN_CH_STATUS2_MAINCHARGERDETDBNC
);
2004 for (i
= 0; i
< 10; i
++) {
2005 ret
= abx500_get_register_interruptible(di
->dev
,
2007 AB8500_CH_STATUS2_REG
,
2010 dev_err(di
->dev
, "ab8500 read failed %d\n", __LINE__
);
2014 if ((statval
& mainch
) != mainch
)
2017 msleep(CHARGER_STATUS_POLL
);
2020 ab8500_charger_ac_en(&di
->ac_chg
, 0, 0, 0);
2021 queue_work(di
->charger_wq
, &di
->ac_work
);
2023 mutex_lock(&di
->charger_attached_mutex
);
2024 mutex_unlock(&di
->charger_attached_mutex
);
2029 queue_delayed_work(di
->charger_wq
,
2030 &di
->ac_charger_attached_work
,
2035 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2036 * @work: Pointer to the work_struct structure
2038 * Detect the type of USB plugged
2040 static void ab8500_charger_detect_usb_type_work(struct work_struct
*work
)
2044 struct ab8500_charger
*di
= container_of(work
,
2045 struct ab8500_charger
, detect_usb_type_work
);
2048 * Since we can't be sure that the events are received
2049 * synchronously, we have the check if is
2050 * connected by reading the status register
2052 ret
= ab8500_charger_detect_chargers(di
, false);
2056 if (!(ret
& USB_PW_CONN
)) {
2057 dev_dbg(di
->dev
, "%s di->vbus_detected = false\n", __func__
);
2058 di
->vbus_detected
= false;
2059 ab8500_charger_set_usb_connected(di
, false);
2060 ab8500_power_supply_changed(di
, &di
->usb_chg
.psy
);
2062 dev_dbg(di
->dev
, "%s di->vbus_detected = true\n", __func__
);
2063 di
->vbus_detected
= true;
2065 if (is_ab8500_1p1_or_earlier(di
->parent
)) {
2066 ret
= ab8500_charger_detect_usb_type(di
);
2068 ab8500_charger_set_usb_connected(di
, true);
2069 ab8500_power_supply_changed(di
,
2074 * For ABB cut2.0 and onwards we have an IRQ,
2075 * USB_LINK_STATUS that will be triggered when the USB
2076 * link status changes. The exception is USB connected
2077 * during startup. Then we don't get a
2078 * USB_LINK_STATUS IRQ
2080 if (di
->vbus_detected_start
) {
2081 di
->vbus_detected_start
= false;
2082 ret
= ab8500_charger_detect_usb_type(di
);
2084 ab8500_charger_set_usb_connected(di
,
2086 ab8500_power_supply_changed(di
,
2095 * ab8500_charger_usb_link_attach_work() - work to detect USB type
2096 * @work: pointer to the work_struct structure
2098 * Detect the type of USB plugged
2100 static void ab8500_charger_usb_link_attach_work(struct work_struct
*work
)
2102 struct ab8500_charger
*di
=
2103 container_of(work
, struct ab8500_charger
, attach_work
.work
);
2106 /* Update maximum input current if USB enumeration is not detected */
2107 if (!di
->usb
.charger_online
) {
2108 ret
= ab8500_charger_set_vbus_in_curr(di
, di
->max_usb_in_curr
);
2113 ab8500_charger_set_usb_connected(di
, true);
2114 ab8500_power_supply_changed(di
, &di
->usb_chg
.psy
);
2118 * ab8500_charger_usb_link_status_work() - work to detect USB type
2119 * @work: pointer to the work_struct structure
2121 * Detect the type of USB plugged
2123 static void ab8500_charger_usb_link_status_work(struct work_struct
*work
)
2125 int detected_chargers
;
2129 struct ab8500_charger
*di
= container_of(work
,
2130 struct ab8500_charger
, usb_link_status_work
);
2133 * Since we can't be sure that the events are received
2134 * synchronously, we have the check if is
2135 * connected by reading the status register
2137 detected_chargers
= ab8500_charger_detect_chargers(di
, false);
2138 if (detected_chargers
< 0)
2142 * Some chargers that breaks the USB spec is
2143 * identified as invalid by AB8500 and it refuse
2144 * to start the charging process. but by jumping
2145 * thru a few hoops it can be forced to start.
2147 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_USB
,
2148 AB8500_USB_LINE_STAT_REG
, &val
);
2150 dev_dbg(di
->dev
, "UsbLineStatus register = 0x%02x\n", val
);
2152 dev_dbg(di
->dev
, "Error reading USB link status\n");
2154 if (detected_chargers
& USB_PW_CONN
) {
2155 if (((val
& AB8500_USB_LINK_STATUS
) >> 3) == USB_STAT_NOT_VALID_LINK
&&
2156 di
->invalid_charger_detect_state
== 0) {
2157 dev_dbg(di
->dev
, "Invalid charger detected, state= 0\n");
2159 abx500_mask_and_set_register_interruptible(di
->dev
,
2160 AB8500_CHARGER
, AB8500_USBCH_CTRL1_REG
, 0x01, 0x01);
2161 /*Enable charger detection*/
2162 abx500_mask_and_set_register_interruptible(di
->dev
, AB8500_USB
,
2163 AB8500_MCH_IPT_CURLVL_REG
, 0x01, 0x01);
2164 di
->invalid_charger_detect_state
= 1;
2165 /*exit and wait for new link status interrupt.*/
2169 if (di
->invalid_charger_detect_state
== 1) {
2170 dev_dbg(di
->dev
, "Invalid charger detected, state= 1\n");
2171 /*Stop charger detection*/
2172 abx500_mask_and_set_register_interruptible(di
->dev
, AB8500_USB
,
2173 AB8500_MCH_IPT_CURLVL_REG
, 0x01, 0x00);
2174 /*Check link status*/
2175 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_USB
,
2176 AB8500_USB_LINE_STAT_REG
, &val
);
2177 dev_dbg(di
->dev
, "USB link status= 0x%02x\n",
2178 (val
& AB8500_USB_LINK_STATUS
) >> 3);
2179 di
->invalid_charger_detect_state
= 2;
2182 di
->invalid_charger_detect_state
= 0;
2185 if (!(detected_chargers
& USB_PW_CONN
)) {
2186 di
->vbus_detected
= false;
2187 ab8500_charger_set_usb_connected(di
, false);
2188 ab8500_power_supply_changed(di
, &di
->usb_chg
.psy
);
2192 dev_dbg(di
->dev
,"%s di->vbus_detected = true\n",__func__
);
2193 di
->vbus_detected
= true;
2194 ret
= ab8500_charger_read_usb_type(di
);
2196 if (ret
== -ENXIO
) {
2197 /* No valid charger type detected */
2198 ab8500_charger_set_usb_connected(di
, false);
2199 ab8500_power_supply_changed(di
, &di
->usb_chg
.psy
);
2204 if (di
->usb_device_is_unrecognised
) {
2206 "Potential Legacy Charger device. "
2207 "Delay work for %d msec for USB enum "
2209 WAIT_ACA_RID_ENUMERATION
);
2210 queue_delayed_work(di
->charger_wq
,
2212 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION
));
2213 } else if (di
->is_aca_rid
== 1) {
2214 /* Only wait once */
2217 "%s Wait %d msec for USB enum to finish",
2218 __func__
, WAIT_ACA_RID_ENUMERATION
);
2219 queue_delayed_work(di
->charger_wq
,
2221 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION
));
2223 queue_delayed_work(di
->charger_wq
,
2229 static void ab8500_charger_usb_state_changed_work(struct work_struct
*work
)
2232 unsigned long flags
;
2234 struct ab8500_charger
*di
= container_of(work
,
2235 struct ab8500_charger
, usb_state_changed_work
.work
);
2237 if (!di
->vbus_detected
) {
2239 "%s !di->vbus_detected\n",
2244 spin_lock_irqsave(&di
->usb_state
.usb_lock
, flags
);
2245 di
->usb_state
.state
= di
->usb_state
.state_tmp
;
2246 di
->usb_state
.usb_current
= di
->usb_state
.usb_current_tmp
;
2247 spin_unlock_irqrestore(&di
->usb_state
.usb_lock
, flags
);
2249 dev_dbg(di
->dev
, "%s USB state: 0x%02x mA: %d\n",
2250 __func__
, di
->usb_state
.state
, di
->usb_state
.usb_current
);
2252 switch (di
->usb_state
.state
) {
2253 case AB8500_BM_USB_STATE_RESET_HS
:
2254 case AB8500_BM_USB_STATE_RESET_FS
:
2255 case AB8500_BM_USB_STATE_SUSPEND
:
2256 case AB8500_BM_USB_STATE_MAX
:
2257 ab8500_charger_set_usb_connected(di
, false);
2258 ab8500_power_supply_changed(di
, &di
->usb_chg
.psy
);
2261 case AB8500_BM_USB_STATE_RESUME
:
2263 * when suspend->resume there should be delay
2264 * of 1sec for enabling charging
2267 /* Intentional fall through */
2268 case AB8500_BM_USB_STATE_CONFIGURED
:
2270 * USB is configured, enable charging with the charging
2271 * input current obtained from USB driver
2273 if (!ab8500_charger_get_usb_cur(di
)) {
2274 /* Update maximum input current */
2275 ret
= ab8500_charger_set_vbus_in_curr(di
,
2276 di
->max_usb_in_curr
);
2280 ab8500_charger_set_usb_connected(di
, true);
2281 ab8500_power_supply_changed(di
, &di
->usb_chg
.psy
);
2291 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2292 * @work: pointer to the work_struct structure
2294 * Work queue function for checking the USB charger Not OK status
2296 static void ab8500_charger_check_usbchargernotok_work(struct work_struct
*work
)
2302 struct ab8500_charger
*di
= container_of(work
,
2303 struct ab8500_charger
, check_usbchgnotok_work
.work
);
2305 /* Check if the status bit for usbchargernotok is still active */
2306 ret
= abx500_get_register_interruptible(di
->dev
,
2307 AB8500_CHARGER
, AB8500_CH_USBCH_STAT2_REG
, ®_value
);
2309 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
2312 prev_status
= di
->flags
.usbchargernotok
;
2314 if (reg_value
& VBUS_CH_NOK
) {
2315 di
->flags
.usbchargernotok
= true;
2316 /* Check again in 1sec */
2317 queue_delayed_work(di
->charger_wq
,
2318 &di
->check_usbchgnotok_work
, HZ
);
2320 di
->flags
.usbchargernotok
= false;
2321 di
->flags
.vbus_collapse
= false;
2324 if (prev_status
!= di
->flags
.usbchargernotok
)
2325 ab8500_power_supply_changed(di
, &di
->usb_chg
.psy
);
2329 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2330 * @work: pointer to the work_struct structure
2332 * Work queue function for checking the Main thermal prot status
2334 static void ab8500_charger_check_main_thermal_prot_work(
2335 struct work_struct
*work
)
2340 struct ab8500_charger
*di
= container_of(work
,
2341 struct ab8500_charger
, check_main_thermal_prot_work
);
2343 /* Check if the status bit for main_thermal_prot is still active */
2344 ret
= abx500_get_register_interruptible(di
->dev
,
2345 AB8500_CHARGER
, AB8500_CH_STATUS2_REG
, ®_value
);
2347 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
2350 if (reg_value
& MAIN_CH_TH_PROT
)
2351 di
->flags
.main_thermal_prot
= true;
2353 di
->flags
.main_thermal_prot
= false;
2355 ab8500_power_supply_changed(di
, &di
->ac_chg
.psy
);
2359 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2360 * @work: pointer to the work_struct structure
2362 * Work queue function for checking the USB thermal prot status
2364 static void ab8500_charger_check_usb_thermal_prot_work(
2365 struct work_struct
*work
)
2370 struct ab8500_charger
*di
= container_of(work
,
2371 struct ab8500_charger
, check_usb_thermal_prot_work
);
2373 /* Check if the status bit for usb_thermal_prot is still active */
2374 ret
= abx500_get_register_interruptible(di
->dev
,
2375 AB8500_CHARGER
, AB8500_CH_USBCH_STAT2_REG
, ®_value
);
2377 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
2380 if (reg_value
& USB_CH_TH_PROT
)
2381 di
->flags
.usb_thermal_prot
= true;
2383 di
->flags
.usb_thermal_prot
= false;
2385 ab8500_power_supply_changed(di
, &di
->usb_chg
.psy
);
2389 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2390 * @irq: interrupt number
2391 * @_di: pointer to the ab8500_charger structure
2393 * Returns IRQ status(IRQ_HANDLED)
2395 static irqreturn_t
ab8500_charger_mainchunplugdet_handler(int irq
, void *_di
)
2397 struct ab8500_charger
*di
= _di
;
2399 dev_dbg(di
->dev
, "Main charger unplugged\n");
2400 queue_work(di
->charger_wq
, &di
->ac_work
);
2402 cancel_delayed_work_sync(&di
->ac_charger_attached_work
);
2403 mutex_lock(&di
->charger_attached_mutex
);
2404 mutex_unlock(&di
->charger_attached_mutex
);
2410 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2411 * @irq: interrupt number
2412 * @_di: pointer to the ab8500_charger structure
2414 * Returns IRQ status(IRQ_HANDLED)
2416 static irqreturn_t
ab8500_charger_mainchplugdet_handler(int irq
, void *_di
)
2418 struct ab8500_charger
*di
= _di
;
2420 dev_dbg(di
->dev
, "Main charger plugged\n");
2421 queue_work(di
->charger_wq
, &di
->ac_work
);
2423 mutex_lock(&di
->charger_attached_mutex
);
2424 mutex_unlock(&di
->charger_attached_mutex
);
2425 queue_delayed_work(di
->charger_wq
,
2426 &di
->ac_charger_attached_work
,
2432 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2433 * @irq: interrupt number
2434 * @_di: pointer to the ab8500_charger structure
2436 * Returns IRQ status(IRQ_HANDLED)
2438 static irqreturn_t
ab8500_charger_mainextchnotok_handler(int irq
, void *_di
)
2440 struct ab8500_charger
*di
= _di
;
2442 dev_dbg(di
->dev
, "Main charger not ok\n");
2443 di
->flags
.mainextchnotok
= true;
2444 ab8500_power_supply_changed(di
, &di
->ac_chg
.psy
);
2446 /* Schedule a new HW failure check */
2447 queue_delayed_work(di
->charger_wq
, &di
->check_hw_failure_work
, 0);
2453 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2454 * thermal protection threshold
2455 * @irq: interrupt number
2456 * @_di: pointer to the ab8500_charger structure
2458 * Returns IRQ status(IRQ_HANDLED)
2460 static irqreturn_t
ab8500_charger_mainchthprotr_handler(int irq
, void *_di
)
2462 struct ab8500_charger
*di
= _di
;
2465 "Die temp above Main charger thermal protection threshold\n");
2466 queue_work(di
->charger_wq
, &di
->check_main_thermal_prot_work
);
2472 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2473 * thermal protection threshold
2474 * @irq: interrupt number
2475 * @_di: pointer to the ab8500_charger structure
2477 * Returns IRQ status(IRQ_HANDLED)
2479 static irqreturn_t
ab8500_charger_mainchthprotf_handler(int irq
, void *_di
)
2481 struct ab8500_charger
*di
= _di
;
2484 "Die temp ok for Main charger thermal protection threshold\n");
2485 queue_work(di
->charger_wq
, &di
->check_main_thermal_prot_work
);
2490 static void ab8500_charger_vbus_drop_end_work(struct work_struct
*work
)
2492 struct ab8500_charger
*di
= container_of(work
,
2493 struct ab8500_charger
, vbus_drop_end_work
.work
);
2495 di
->flags
.vbus_drop_end
= false;
2497 /* Reset the drop counter */
2498 abx500_set_register_interruptible(di
->dev
,
2499 AB8500_CHARGER
, AB8500_CHARGER_CTRL
, 0x01);
2501 if (di
->usb
.charger_connected
)
2502 ab8500_charger_set_vbus_in_curr(di
, di
->max_usb_in_curr
);
2506 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2507 * @irq: interrupt number
2508 * @_di: pointer to the ab8500_charger structure
2510 * Returns IRQ status(IRQ_HANDLED)
2512 static irqreturn_t
ab8500_charger_vbusdetf_handler(int irq
, void *_di
)
2514 struct ab8500_charger
*di
= _di
;
2516 di
->vbus_detected
= false;
2517 dev_dbg(di
->dev
, "VBUS falling detected\n");
2518 queue_work(di
->charger_wq
, &di
->detect_usb_type_work
);
2524 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2525 * @irq: interrupt number
2526 * @_di: pointer to the ab8500_charger structure
2528 * Returns IRQ status(IRQ_HANDLED)
2530 static irqreturn_t
ab8500_charger_vbusdetr_handler(int irq
, void *_di
)
2532 struct ab8500_charger
*di
= _di
;
2534 di
->vbus_detected
= true;
2535 dev_dbg(di
->dev
, "VBUS rising detected\n");
2537 queue_work(di
->charger_wq
, &di
->detect_usb_type_work
);
2543 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2544 * @irq: interrupt number
2545 * @_di: pointer to the ab8500_charger structure
2547 * Returns IRQ status(IRQ_HANDLED)
2549 static irqreturn_t
ab8500_charger_usblinkstatus_handler(int irq
, void *_di
)
2551 struct ab8500_charger
*di
= _di
;
2553 dev_dbg(di
->dev
, "USB link status changed\n");
2555 queue_work(di
->charger_wq
, &di
->usb_link_status_work
);
2561 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2562 * thermal protection threshold
2563 * @irq: interrupt number
2564 * @_di: pointer to the ab8500_charger structure
2566 * Returns IRQ status(IRQ_HANDLED)
2568 static irqreturn_t
ab8500_charger_usbchthprotr_handler(int irq
, void *_di
)
2570 struct ab8500_charger
*di
= _di
;
2573 "Die temp above USB charger thermal protection threshold\n");
2574 queue_work(di
->charger_wq
, &di
->check_usb_thermal_prot_work
);
2580 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2581 * thermal protection threshold
2582 * @irq: interrupt number
2583 * @_di: pointer to the ab8500_charger structure
2585 * Returns IRQ status(IRQ_HANDLED)
2587 static irqreturn_t
ab8500_charger_usbchthprotf_handler(int irq
, void *_di
)
2589 struct ab8500_charger
*di
= _di
;
2592 "Die temp ok for USB charger thermal protection threshold\n");
2593 queue_work(di
->charger_wq
, &di
->check_usb_thermal_prot_work
);
2599 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2600 * @irq: interrupt number
2601 * @_di: pointer to the ab8500_charger structure
2603 * Returns IRQ status(IRQ_HANDLED)
2605 static irqreturn_t
ab8500_charger_usbchargernotokr_handler(int irq
, void *_di
)
2607 struct ab8500_charger
*di
= _di
;
2609 dev_dbg(di
->dev
, "Not allowed USB charger detected\n");
2610 queue_delayed_work(di
->charger_wq
, &di
->check_usbchgnotok_work
, 0);
2616 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2617 * @irq: interrupt number
2618 * @_di: pointer to the ab8500_charger structure
2620 * Returns IRQ status(IRQ_HANDLED)
2622 static irqreturn_t
ab8500_charger_chwdexp_handler(int irq
, void *_di
)
2624 struct ab8500_charger
*di
= _di
;
2626 dev_dbg(di
->dev
, "Charger watchdog expired\n");
2629 * The charger that was online when the watchdog expired
2630 * needs to be restarted for charging to start again
2632 if (di
->ac
.charger_online
) {
2633 di
->ac
.wd_expired
= true;
2634 ab8500_power_supply_changed(di
, &di
->ac_chg
.psy
);
2636 if (di
->usb
.charger_online
) {
2637 di
->usb
.wd_expired
= true;
2638 ab8500_power_supply_changed(di
, &di
->usb_chg
.psy
);
2645 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2646 * @irq: interrupt number
2647 * @_di: pointer to the ab8500_charger structure
2649 * Returns IRQ status(IRQ_HANDLED)
2651 static irqreturn_t
ab8500_charger_vbuschdropend_handler(int irq
, void *_di
)
2653 struct ab8500_charger
*di
= _di
;
2655 dev_dbg(di
->dev
, "VBUS charger drop ended\n");
2656 di
->flags
.vbus_drop_end
= true;
2657 queue_delayed_work(di
->charger_wq
, &di
->vbus_drop_end_work
,
2658 round_jiffies(30 * HZ
));
2664 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2665 * @irq: interrupt number
2666 * @_di: pointer to the ab8500_charger structure
2668 * Returns IRQ status(IRQ_HANDLED)
2670 static irqreturn_t
ab8500_charger_vbusovv_handler(int irq
, void *_di
)
2672 struct ab8500_charger
*di
= _di
;
2674 dev_dbg(di
->dev
, "VBUS overvoltage detected\n");
2675 di
->flags
.vbus_ovv
= true;
2676 ab8500_power_supply_changed(di
, &di
->usb_chg
.psy
);
2678 /* Schedule a new HW failure check */
2679 queue_delayed_work(di
->charger_wq
, &di
->check_hw_failure_work
, 0);
2685 * ab8500_charger_ac_get_property() - get the ac/mains properties
2686 * @psy: pointer to the power_supply structure
2687 * @psp: pointer to the power_supply_property structure
2688 * @val: pointer to the power_supply_propval union
2690 * This function gets called when an application tries to get the ac/mains
2691 * properties by reading the sysfs files.
2692 * AC/Mains properties are online, present and voltage.
2693 * online: ac/mains charging is in progress or not
2694 * present: presence of the ac/mains
2695 * voltage: AC/Mains voltage
2696 * Returns error code in case of failure else 0(on success)
2698 static int ab8500_charger_ac_get_property(struct power_supply
*psy
,
2699 enum power_supply_property psp
,
2700 union power_supply_propval
*val
)
2702 struct ab8500_charger
*di
;
2705 di
= to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy
));
2708 case POWER_SUPPLY_PROP_HEALTH
:
2709 if (di
->flags
.mainextchnotok
)
2710 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
2711 else if (di
->ac
.wd_expired
|| di
->usb
.wd_expired
)
2712 val
->intval
= POWER_SUPPLY_HEALTH_DEAD
;
2713 else if (di
->flags
.main_thermal_prot
)
2714 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
2716 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
2718 case POWER_SUPPLY_PROP_ONLINE
:
2719 val
->intval
= di
->ac
.charger_online
;
2721 case POWER_SUPPLY_PROP_PRESENT
:
2722 val
->intval
= di
->ac
.charger_connected
;
2724 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
2725 ret
= ab8500_charger_get_ac_voltage(di
);
2727 di
->ac
.charger_voltage
= ret
;
2728 /* On error, use previous value */
2729 val
->intval
= di
->ac
.charger_voltage
* 1000;
2731 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
2733 * This property is used to indicate when CV mode is entered
2734 * for the AC charger
2736 di
->ac
.cv_active
= ab8500_charger_ac_cv(di
);
2737 val
->intval
= di
->ac
.cv_active
;
2739 case POWER_SUPPLY_PROP_CURRENT_NOW
:
2740 ret
= ab8500_charger_get_ac_current(di
);
2742 di
->ac
.charger_current
= ret
;
2743 val
->intval
= di
->ac
.charger_current
* 1000;
2752 * ab8500_charger_usb_get_property() - get the usb properties
2753 * @psy: pointer to the power_supply structure
2754 * @psp: pointer to the power_supply_property structure
2755 * @val: pointer to the power_supply_propval union
2757 * This function gets called when an application tries to get the usb
2758 * properties by reading the sysfs files.
2759 * USB properties are online, present and voltage.
2760 * online: usb charging is in progress or not
2761 * present: presence of the usb
2762 * voltage: vbus voltage
2763 * Returns error code in case of failure else 0(on success)
2765 static int ab8500_charger_usb_get_property(struct power_supply
*psy
,
2766 enum power_supply_property psp
,
2767 union power_supply_propval
*val
)
2769 struct ab8500_charger
*di
;
2772 di
= to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy
));
2775 case POWER_SUPPLY_PROP_HEALTH
:
2776 if (di
->flags
.usbchargernotok
)
2777 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
2778 else if (di
->ac
.wd_expired
|| di
->usb
.wd_expired
)
2779 val
->intval
= POWER_SUPPLY_HEALTH_DEAD
;
2780 else if (di
->flags
.usb_thermal_prot
)
2781 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
2782 else if (di
->flags
.vbus_ovv
)
2783 val
->intval
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
2785 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
2787 case POWER_SUPPLY_PROP_ONLINE
:
2788 val
->intval
= di
->usb
.charger_online
;
2790 case POWER_SUPPLY_PROP_PRESENT
:
2791 val
->intval
= di
->usb
.charger_connected
;
2793 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
2794 ret
= ab8500_charger_get_vbus_voltage(di
);
2796 di
->usb
.charger_voltage
= ret
;
2797 val
->intval
= di
->usb
.charger_voltage
* 1000;
2799 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
2801 * This property is used to indicate when CV mode is entered
2802 * for the USB charger
2804 di
->usb
.cv_active
= ab8500_charger_usb_cv(di
);
2805 val
->intval
= di
->usb
.cv_active
;
2807 case POWER_SUPPLY_PROP_CURRENT_NOW
:
2808 ret
= ab8500_charger_get_usb_current(di
);
2810 di
->usb
.charger_current
= ret
;
2811 val
->intval
= di
->usb
.charger_current
* 1000;
2813 case POWER_SUPPLY_PROP_CURRENT_AVG
:
2815 * This property is used to indicate when VBUS has collapsed
2816 * due to too high output current from the USB charger
2818 if (di
->flags
.vbus_collapse
)
2830 * ab8500_charger_init_hw_registers() - Set up charger related registers
2831 * @di: pointer to the ab8500_charger structure
2833 * Set up charger OVV, watchdog and maximum voltage registers as well as
2834 * charging of the backup battery
2836 static int ab8500_charger_init_hw_registers(struct ab8500_charger
*di
)
2840 /* Setup maximum charger current and voltage for ABB cut2.0 */
2841 if (!is_ab8500_1p1_or_earlier(di
->parent
)) {
2842 ret
= abx500_set_register_interruptible(di
->dev
,
2844 AB8500_CH_VOLT_LVL_MAX_REG
, CH_VOL_LVL_4P6
);
2847 "failed to set CH_VOLT_LVL_MAX_REG\n");
2851 ret
= abx500_set_register_interruptible(di
->dev
,
2853 AB8500_CH_OPT_CRNTLVL_MAX_REG
, CH_OP_CUR_LVL_1P6
);
2856 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2861 if (is_ab9540_2p0(di
->parent
) || is_ab8505_2p0(di
->parent
))
2862 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
2864 AB8500_USBCH_CTRL2_REG
,
2865 VBUS_AUTO_IN_CURR_LIM_ENA
,
2866 VBUS_AUTO_IN_CURR_LIM_ENA
);
2869 * VBUS OVV set to 6.3V and enable automatic current limitation
2871 ret
= abx500_set_register_interruptible(di
->dev
,
2873 AB8500_USBCH_CTRL2_REG
,
2874 VBUS_OVV_SELECT_6P3V
| VBUS_AUTO_IN_CURR_LIM_ENA
);
2877 "failed to set automatic current limitation\n");
2881 /* Enable main watchdog in OTP */
2882 ret
= abx500_set_register_interruptible(di
->dev
,
2883 AB8500_OTP_EMUL
, AB8500_OTP_CONF_15
, OTP_ENABLE_WD
);
2885 dev_err(di
->dev
, "failed to enable main WD in OTP\n");
2889 /* Enable main watchdog */
2890 ret
= abx500_set_register_interruptible(di
->dev
,
2891 AB8500_SYS_CTRL2_BLOCK
,
2892 AB8500_MAIN_WDOG_CTRL_REG
, MAIN_WDOG_ENA
);
2894 dev_err(di
->dev
, "faile to enable main watchdog\n");
2899 * Due to internal synchronisation, Enable and Kick watchdog bits
2900 * cannot be enabled in a single write.
2901 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
2902 * between writing Enable then Kick bits.
2906 /* Kick main watchdog */
2907 ret
= abx500_set_register_interruptible(di
->dev
,
2908 AB8500_SYS_CTRL2_BLOCK
,
2909 AB8500_MAIN_WDOG_CTRL_REG
,
2910 (MAIN_WDOG_ENA
| MAIN_WDOG_KICK
));
2912 dev_err(di
->dev
, "failed to kick main watchdog\n");
2916 /* Disable main watchdog */
2917 ret
= abx500_set_register_interruptible(di
->dev
,
2918 AB8500_SYS_CTRL2_BLOCK
,
2919 AB8500_MAIN_WDOG_CTRL_REG
, MAIN_WDOG_DIS
);
2921 dev_err(di
->dev
, "failed to disable main watchdog\n");
2925 /* Set watchdog timeout */
2926 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
2927 AB8500_CH_WD_TIMER_REG
, WD_TIMER
);
2929 dev_err(di
->dev
, "failed to set charger watchdog timeout\n");
2933 /* Set charger watchdog timeout */
2934 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
2935 AB8500_CH_WD_TIMER_REG
, WD_TIMER
);
2937 dev_err(di
->dev
, "failed to set charger watchdog timeout\n");
2941 ret
= ab8500_charger_led_en(di
, false);
2943 dev_err(di
->dev
, "failed to disable LED\n");
2947 /* Backup battery voltage and current */
2948 ret
= abx500_set_register_interruptible(di
->dev
,
2950 AB8500_RTC_BACKUP_CHG_REG
,
2951 di
->bm
->bkup_bat_v
|
2952 di
->bm
->bkup_bat_i
);
2954 dev_err(di
->dev
, "failed to setup backup battery charging\n");
2958 /* Enable backup battery charging */
2959 abx500_mask_and_set_register_interruptible(di
->dev
,
2960 AB8500_RTC
, AB8500_RTC_CTRL_REG
,
2961 RTC_BUP_CH_ENA
, RTC_BUP_CH_ENA
);
2963 dev_err(di
->dev
, "%s mask and set failed\n", __func__
);
2970 * ab8500 charger driver interrupts and their respective isr
2972 static struct ab8500_charger_interrupts ab8500_charger_irq
[] = {
2973 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler
},
2974 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler
},
2975 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler
},
2976 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler
},
2977 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler
},
2978 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler
},
2979 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler
},
2980 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler
},
2981 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler
},
2982 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler
},
2983 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler
},
2984 {"VBUS_OVV", ab8500_charger_vbusovv_handler
},
2985 {"CH_WD_EXP", ab8500_charger_chwdexp_handler
},
2986 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler
},
2989 static int ab8500_charger_usb_notifier_call(struct notifier_block
*nb
,
2990 unsigned long event
, void *power
)
2992 struct ab8500_charger
*di
=
2993 container_of(nb
, struct ab8500_charger
, nb
);
2994 enum ab8500_usb_state bm_usb_state
;
2995 unsigned mA
= *((unsigned *)power
);
3000 if (event
!= USB_EVENT_VBUS
) {
3001 dev_dbg(di
->dev
, "not a standard host, returning\n");
3005 /* TODO: State is fabricate here. See if charger really needs USB
3006 * state or if mA is enough
3008 if ((di
->usb_state
.usb_current
== 2) && (mA
> 2))
3009 bm_usb_state
= AB8500_BM_USB_STATE_RESUME
;
3011 bm_usb_state
= AB8500_BM_USB_STATE_RESET_HS
;
3013 bm_usb_state
= AB8500_BM_USB_STATE_SUSPEND
;
3014 else if (mA
>= 8) /* 8, 100, 500 */
3015 bm_usb_state
= AB8500_BM_USB_STATE_CONFIGURED
;
3016 else /* Should never occur */
3017 bm_usb_state
= AB8500_BM_USB_STATE_RESET_FS
;
3019 dev_dbg(di
->dev
, "%s usb_state: 0x%02x mA: %d\n",
3020 __func__
, bm_usb_state
, mA
);
3022 spin_lock(&di
->usb_state
.usb_lock
);
3023 di
->usb_state
.state_tmp
= bm_usb_state
;
3024 di
->usb_state
.usb_current_tmp
= mA
;
3025 spin_unlock(&di
->usb_state
.usb_lock
);
3028 * wait for some time until you get updates from the usb stack
3029 * and negotiations are completed
3031 queue_delayed_work(di
->charger_wq
, &di
->usb_state_changed_work
, HZ
/2);
3036 #if defined(CONFIG_PM)
3037 static int ab8500_charger_resume(struct platform_device
*pdev
)
3040 struct ab8500_charger
*di
= platform_get_drvdata(pdev
);
3043 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3044 * logic. That means we have to continously kick the charger
3045 * watchdog even when no charger is connected. This is only
3046 * valid once the AC charger has been enabled. This is
3047 * a bug that is not handled by the algorithm and the
3048 * watchdog have to be kicked by the charger driver
3049 * when the AC charger is disabled
3051 if (di
->ac_conn
&& is_ab8500_1p1_or_earlier(di
->parent
)) {
3052 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
3053 AB8500_CHARG_WD_CTRL
, CHARG_WD_KICK
);
3055 dev_err(di
->dev
, "Failed to kick WD!\n");
3057 /* If not already pending start a new timer */
3058 if (!delayed_work_pending(
3059 &di
->kick_wd_work
)) {
3060 queue_delayed_work(di
->charger_wq
, &di
->kick_wd_work
,
3061 round_jiffies(WD_KICK_INTERVAL
));
3065 /* If we still have a HW failure, schedule a new check */
3066 if (di
->flags
.mainextchnotok
|| di
->flags
.vbus_ovv
) {
3067 queue_delayed_work(di
->charger_wq
,
3068 &di
->check_hw_failure_work
, 0);
3071 if (di
->flags
.vbus_drop_end
)
3072 queue_delayed_work(di
->charger_wq
, &di
->vbus_drop_end_work
, 0);
3077 static int ab8500_charger_suspend(struct platform_device
*pdev
,
3080 struct ab8500_charger
*di
= platform_get_drvdata(pdev
);
3082 /* Cancel any pending HW failure check */
3083 if (delayed_work_pending(&di
->check_hw_failure_work
))
3084 cancel_delayed_work(&di
->check_hw_failure_work
);
3086 if (delayed_work_pending(&di
->vbus_drop_end_work
))
3087 cancel_delayed_work(&di
->vbus_drop_end_work
);
3089 flush_delayed_work(&di
->attach_work
);
3090 flush_delayed_work(&di
->usb_charger_attached_work
);
3091 flush_delayed_work(&di
->ac_charger_attached_work
);
3092 flush_delayed_work(&di
->check_usbchgnotok_work
);
3093 flush_delayed_work(&di
->check_vbat_work
);
3094 flush_delayed_work(&di
->kick_wd_work
);
3096 flush_work(&di
->usb_link_status_work
);
3097 flush_work(&di
->ac_work
);
3098 flush_work(&di
->detect_usb_type_work
);
3100 if (atomic_read(&di
->current_stepping_sessions
))
3106 #define ab8500_charger_suspend NULL
3107 #define ab8500_charger_resume NULL
3110 static int ab8500_charger_remove(struct platform_device
*pdev
)
3112 struct ab8500_charger
*di
= platform_get_drvdata(pdev
);
3115 /* Disable AC charging */
3116 ab8500_charger_ac_en(&di
->ac_chg
, false, 0, 0);
3118 /* Disable USB charging */
3119 ab8500_charger_usb_en(&di
->usb_chg
, false, 0, 0);
3121 /* Disable interrupts */
3122 for (i
= 0; i
< ARRAY_SIZE(ab8500_charger_irq
); i
++) {
3123 irq
= platform_get_irq_byname(pdev
, ab8500_charger_irq
[i
].name
);
3127 /* Backup battery voltage and current disable */
3128 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
3129 AB8500_RTC
, AB8500_RTC_CTRL_REG
, RTC_BUP_CH_ENA
, 0);
3131 dev_err(di
->dev
, "%s mask and set failed\n", __func__
);
3133 usb_unregister_notifier(di
->usb_phy
, &di
->nb
);
3134 usb_put_phy(di
->usb_phy
);
3136 /* Delete the work queue */
3137 destroy_workqueue(di
->charger_wq
);
3139 flush_scheduled_work();
3140 if(di
->usb_chg
.enabled
)
3141 power_supply_unregister(&di
->usb_chg
.psy
);
3142 #if !defined(CONFIG_CHARGER_PM2301)
3143 if(di
->ac_chg
.enabled
)
3144 power_supply_unregister(&di
->ac_chg
.psy
);
3146 platform_set_drvdata(pdev
, NULL
);
3151 static char *supply_interface
[] = {
3157 static int ab8500_charger_probe(struct platform_device
*pdev
)
3159 struct device_node
*np
= pdev
->dev
.of_node
;
3160 struct abx500_bm_data
*plat
= pdev
->dev
.platform_data
;
3161 struct ab8500_charger
*di
;
3162 int irq
, i
, charger_status
, ret
= 0, ch_stat
;
3164 di
= devm_kzalloc(&pdev
->dev
, sizeof(*di
), GFP_KERNEL
);
3166 dev_err(&pdev
->dev
, "%s no mem for ab8500_charger\n", __func__
);
3171 dev_err(&pdev
->dev
, "no battery management data supplied\n");
3177 ret
= ab8500_bm_of_probe(&pdev
->dev
, np
, di
->bm
);
3179 dev_err(&pdev
->dev
, "failed to get battery information\n");
3182 di
->autopower_cfg
= of_property_read_bool(np
, "autopower_cfg");
3184 di
->autopower_cfg
= false;
3186 /* get parent data */
3187 di
->dev
= &pdev
->dev
;
3188 di
->parent
= dev_get_drvdata(pdev
->dev
.parent
);
3189 di
->gpadc
= ab8500_gpadc_get("ab8500-gpadc.0");
3191 /* initialize lock */
3192 spin_lock_init(&di
->usb_state
.usb_lock
);
3193 mutex_init(&di
->usb_ipt_crnt_lock
);
3195 di
->autopower
= false;
3196 di
->invalid_charger_detect_state
= 0;
3199 /* power_supply base class */
3200 di
->ac_chg
.psy
.name
= "ab8500_ac";
3201 di
->ac_chg
.psy
.type
= POWER_SUPPLY_TYPE_MAINS
;
3202 di
->ac_chg
.psy
.properties
= ab8500_charger_ac_props
;
3203 di
->ac_chg
.psy
.num_properties
= ARRAY_SIZE(ab8500_charger_ac_props
);
3204 di
->ac_chg
.psy
.get_property
= ab8500_charger_ac_get_property
;
3205 di
->ac_chg
.psy
.supplied_to
= supply_interface
;
3206 di
->ac_chg
.psy
.num_supplicants
= ARRAY_SIZE(supply_interface
),
3207 /* ux500_charger sub-class */
3208 di
->ac_chg
.ops
.enable
= &ab8500_charger_ac_en
;
3209 di
->ac_chg
.ops
.kick_wd
= &ab8500_charger_watchdog_kick
;
3210 di
->ac_chg
.ops
.update_curr
= &ab8500_charger_update_charger_current
;
3211 di
->ac_chg
.max_out_volt
= ab8500_charger_voltage_map
[
3212 ARRAY_SIZE(ab8500_charger_voltage_map
) - 1];
3213 di
->ac_chg
.max_out_curr
= ab8500_charger_current_map
[
3214 ARRAY_SIZE(ab8500_charger_current_map
) - 1];
3215 di
->ac_chg
.wdt_refresh
= CHG_WD_INTERVAL
;
3216 di
->ac_chg
.enabled
= di
->bm
->ac_enabled
;
3217 di
->ac_chg
.external
= false;
3220 /* power_supply base class */
3221 di
->usb_chg
.psy
.name
= "ab8500_usb";
3222 di
->usb_chg
.psy
.type
= POWER_SUPPLY_TYPE_USB
;
3223 di
->usb_chg
.psy
.properties
= ab8500_charger_usb_props
;
3224 di
->usb_chg
.psy
.num_properties
= ARRAY_SIZE(ab8500_charger_usb_props
);
3225 di
->usb_chg
.psy
.get_property
= ab8500_charger_usb_get_property
;
3226 di
->usb_chg
.psy
.supplied_to
= supply_interface
;
3227 di
->usb_chg
.psy
.num_supplicants
= ARRAY_SIZE(supply_interface
),
3228 /* ux500_charger sub-class */
3229 di
->usb_chg
.ops
.enable
= &ab8500_charger_usb_en
;
3230 di
->usb_chg
.ops
.kick_wd
= &ab8500_charger_watchdog_kick
;
3231 di
->usb_chg
.ops
.update_curr
= &ab8500_charger_update_charger_current
;
3232 di
->usb_chg
.max_out_volt
= ab8500_charger_voltage_map
[
3233 ARRAY_SIZE(ab8500_charger_voltage_map
) - 1];
3234 di
->usb_chg
.max_out_curr
= ab8500_charger_current_map
[
3235 ARRAY_SIZE(ab8500_charger_current_map
) - 1];
3236 di
->usb_chg
.wdt_refresh
= CHG_WD_INTERVAL
;
3237 di
->usb_chg
.enabled
= di
->bm
->usb_enabled
;
3238 di
->usb_chg
.external
= false;
3240 /* Create a work queue for the charger */
3242 create_singlethread_workqueue("ab8500_charger_wq");
3243 if (di
->charger_wq
== NULL
) {
3244 dev_err(di
->dev
, "failed to create work queue\n");
3248 mutex_init(&di
->charger_attached_mutex
);
3250 /* Init work for HW failure check */
3251 INIT_DEFERRABLE_WORK(&di
->check_hw_failure_work
,
3252 ab8500_charger_check_hw_failure_work
);
3253 INIT_DEFERRABLE_WORK(&di
->check_usbchgnotok_work
,
3254 ab8500_charger_check_usbchargernotok_work
);
3256 INIT_DELAYED_WORK(&di
->ac_charger_attached_work
,
3257 ab8500_charger_ac_attached_work
);
3258 INIT_DELAYED_WORK(&di
->usb_charger_attached_work
,
3259 ab8500_charger_usb_attached_work
);
3262 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3263 * logic. That means we have to continously kick the charger
3264 * watchdog even when no charger is connected. This is only
3265 * valid once the AC charger has been enabled. This is
3266 * a bug that is not handled by the algorithm and the
3267 * watchdog have to be kicked by the charger driver
3268 * when the AC charger is disabled
3270 INIT_DEFERRABLE_WORK(&di
->kick_wd_work
,
3271 ab8500_charger_kick_watchdog_work
);
3273 INIT_DEFERRABLE_WORK(&di
->check_vbat_work
,
3274 ab8500_charger_check_vbat_work
);
3276 INIT_DELAYED_WORK(&di
->attach_work
,
3277 ab8500_charger_usb_link_attach_work
);
3279 INIT_DELAYED_WORK(&di
->usb_state_changed_work
,
3280 ab8500_charger_usb_state_changed_work
);
3282 INIT_DELAYED_WORK(&di
->vbus_drop_end_work
,
3283 ab8500_charger_vbus_drop_end_work
);
3285 /* Init work for charger detection */
3286 INIT_WORK(&di
->usb_link_status_work
,
3287 ab8500_charger_usb_link_status_work
);
3288 INIT_WORK(&di
->ac_work
, ab8500_charger_ac_work
);
3289 INIT_WORK(&di
->detect_usb_type_work
,
3290 ab8500_charger_detect_usb_type_work
);
3292 /* Init work for checking HW status */
3293 INIT_WORK(&di
->check_main_thermal_prot_work
,
3294 ab8500_charger_check_main_thermal_prot_work
);
3295 INIT_WORK(&di
->check_usb_thermal_prot_work
,
3296 ab8500_charger_check_usb_thermal_prot_work
);
3299 * VDD ADC supply needs to be enabled from this driver when there
3300 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3301 * interrupts during charging
3303 di
->regu
= devm_regulator_get(di
->dev
, "vddadc");
3304 if (IS_ERR(di
->regu
)) {
3305 ret
= PTR_ERR(di
->regu
);
3306 dev_err(di
->dev
, "failed to get vddadc regulator\n");
3307 goto free_charger_wq
;
3311 /* Initialize OVV, and other registers */
3312 ret
= ab8500_charger_init_hw_registers(di
);
3314 dev_err(di
->dev
, "failed to initialize ABB registers\n");
3315 goto free_charger_wq
;
3318 /* Register AC charger class */
3319 if(di
->ac_chg
.enabled
) {
3320 ret
= power_supply_register(di
->dev
, &di
->ac_chg
.psy
);
3322 dev_err(di
->dev
, "failed to register AC charger\n");
3323 goto free_charger_wq
;
3327 /* Register USB charger class */
3328 if(di
->usb_chg
.enabled
) {
3329 ret
= power_supply_register(di
->dev
, &di
->usb_chg
.psy
);
3331 dev_err(di
->dev
, "failed to register USB charger\n");
3336 di
->usb_phy
= usb_get_phy(USB_PHY_TYPE_USB2
);
3337 if (IS_ERR_OR_NULL(di
->usb_phy
)) {
3338 dev_err(di
->dev
, "failed to get usb transceiver\n");
3342 di
->nb
.notifier_call
= ab8500_charger_usb_notifier_call
;
3343 ret
= usb_register_notifier(di
->usb_phy
, &di
->nb
);
3345 dev_err(di
->dev
, "failed to register usb notifier\n");
3349 /* Identify the connected charger types during startup */
3350 charger_status
= ab8500_charger_detect_chargers(di
, true);
3351 if (charger_status
& AC_PW_CONN
) {
3352 di
->ac
.charger_connected
= 1;
3354 ab8500_power_supply_changed(di
, &di
->ac_chg
.psy
);
3355 sysfs_notify(&di
->ac_chg
.psy
.dev
->kobj
, NULL
, "present");
3358 if (charger_status
& USB_PW_CONN
) {
3359 di
->vbus_detected
= true;
3360 di
->vbus_detected_start
= true;
3361 queue_work(di
->charger_wq
,
3362 &di
->detect_usb_type_work
);
3365 /* Register interrupts */
3366 for (i
= 0; i
< ARRAY_SIZE(ab8500_charger_irq
); i
++) {
3367 irq
= platform_get_irq_byname(pdev
, ab8500_charger_irq
[i
].name
);
3368 ret
= request_threaded_irq(irq
, NULL
, ab8500_charger_irq
[i
].isr
,
3369 IRQF_SHARED
| IRQF_NO_SUSPEND
,
3370 ab8500_charger_irq
[i
].name
, di
);
3373 dev_err(di
->dev
, "failed to request %s IRQ %d: %d\n"
3374 , ab8500_charger_irq
[i
].name
, irq
, ret
);
3377 dev_dbg(di
->dev
, "Requested %s IRQ %d: %d\n",
3378 ab8500_charger_irq
[i
].name
, irq
, ret
);
3381 platform_set_drvdata(pdev
, di
);
3383 mutex_lock(&di
->charger_attached_mutex
);
3385 ch_stat
= ab8500_charger_detect_chargers(di
, false);
3387 if ((ch_stat
& AC_PW_CONN
) == AC_PW_CONN
) {
3388 queue_delayed_work(di
->charger_wq
,
3389 &di
->ac_charger_attached_work
,
3392 if ((ch_stat
& USB_PW_CONN
) == USB_PW_CONN
) {
3393 queue_delayed_work(di
->charger_wq
,
3394 &di
->usb_charger_attached_work
,
3398 mutex_unlock(&di
->charger_attached_mutex
);
3403 usb_unregister_notifier(di
->usb_phy
, &di
->nb
);
3405 /* We also have to free all successfully registered irqs */
3406 for (i
= i
- 1; i
>= 0; i
--) {
3407 irq
= platform_get_irq_byname(pdev
, ab8500_charger_irq
[i
].name
);
3411 usb_put_phy(di
->usb_phy
);
3413 if(di
->usb_chg
.enabled
)
3414 power_supply_unregister(&di
->usb_chg
.psy
);
3416 if(di
->ac_chg
.enabled
)
3417 power_supply_unregister(&di
->ac_chg
.psy
);
3419 destroy_workqueue(di
->charger_wq
);
3423 static const struct of_device_id ab8500_charger_match
[] = {
3424 { .compatible
= "stericsson,ab8500-charger", },
3428 static struct platform_driver ab8500_charger_driver
= {
3429 .probe
= ab8500_charger_probe
,
3430 .remove
= ab8500_charger_remove
,
3431 .suspend
= ab8500_charger_suspend
,
3432 .resume
= ab8500_charger_resume
,
3434 .name
= "ab8500-charger",
3435 .owner
= THIS_MODULE
,
3436 .of_match_table
= ab8500_charger_match
,
3440 static int __init
ab8500_charger_init(void)
3442 return platform_driver_register(&ab8500_charger_driver
);
3445 static void __exit
ab8500_charger_exit(void)
3447 platform_driver_unregister(&ab8500_charger_driver
);
3450 subsys_initcall_sync(ab8500_charger_init
);
3451 module_exit(ab8500_charger_exit
);
3453 MODULE_LICENSE("GPL v2");
3454 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3455 MODULE_ALIAS("platform:ab8500-charger");
3456 MODULE_DESCRIPTION("AB8500 charger management driver");