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
59 #define LED_INDICATOR_PWM_ENA 0x01
60 #define LED_INDICATOR_PWM_DIS 0x00
61 #define LED_IND_CUR_5MA 0x04
62 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
64 /* HW failure constants */
65 #define MAIN_CH_TH_PROT 0x02
66 #define VBUS_CH_NOK 0x08
67 #define USB_CH_TH_PROT 0x02
68 #define VBUS_OVV_TH 0x01
69 #define MAIN_CH_NOK 0x01
72 #define MAIN_CH_STATUS2_MAINCHGDROP 0x80
73 #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
74 #define USB_CH_VBUSDROP 0x40
75 #define USB_CH_VBUSDETDBNC 0x01
77 /* UsbLineStatus register bit masks */
78 #define AB8500_USB_LINK_STATUS 0x78
79 #define AB8500_STD_HOST_SUSP 0x18
81 /* Watchdog timeout constant */
82 #define WD_TIMER 0x30 /* 4min */
83 #define WD_KICK_INTERVAL (60 * HZ)
85 /* Lowest charger voltage is 3.39V -> 0x4E */
86 #define LOW_VOLT_REG 0x4E
88 /* Step up/down delay in us */
89 #define STEP_UDELAY 1000
91 /* Wait for enumeration before charging in ms */
92 #define WAIT_FOR_USB_ENUMERATION 5 * 1000
94 #define CHARGER_STATUS_POLL 10 /* in ms */
96 #define CHG_WD_INTERVAL (60 * HZ)
98 #define AB8500_SW_CONTROL_FALLBACK 0x03
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;
190 struct ab8500_charger_usb_state {
193 enum ab8500_usb_state state;
198 * struct ab8500_charger - ab8500 Charger device information
199 * @dev: Pointer to the structure device
200 * @max_usb_in_curr: Max USB charger input current
201 * @vbus_detected: VBUS detected
202 * @vbus_detected_start:
203 * VBUS detected during startup
204 * @ac_conn: This will be true when the AC charger has been plugged
205 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
207 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
209 * @vbat Battery voltage
210 * @old_vbat Previously measured battery voltage
211 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
212 * @autopower Indicate if we should have automatic pwron after pwrloss
213 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
214 * @invalid_charger_detect_state State when forcing AB to use invalid charger
215 * @parent: Pointer to the struct ab8500
216 * @gpadc: Pointer to the struct gpadc
217 * @bm: Platform specific battery management information
218 * @flags: Structure for information about events triggered
219 * @usb_state: Structure for usb stack information
220 * @ac_chg: AC charger power supply
221 * @usb_chg: USB charger power supply
222 * @ac: Structure that holds the AC charger properties
223 * @usb: Structure that holds the USB charger properties
224 * @regu: Pointer to the struct regulator
225 * @charger_wq: Work queue for the IRQs and checking HW state
226 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
227 * @check_hw_failure_work: Work for checking HW state
228 * @check_usbchgnotok_work: Work for checking USB charger not ok status
229 * @kick_wd_work: Work for kicking the charger watchdog in case
230 * of ABB rev 1.* due to the watchog logic bug
231 * @attach_work: Work for checking the usb enumeration
232 * @ac_charger_attached_work: Work for checking if AC charger is still
234 * @usb_charger_attached_work: Work for checking if USB charger is still
236 * @ac_work: Work for checking AC charger connection
237 * @detect_usb_type_work: Work for detecting the USB type connected
238 * @usb_link_status_work: Work for checking the new USB link status
239 * @usb_state_changed_work: Work for checking USB state
240 * @check_main_thermal_prot_work:
241 * Work for checking Main thermal status
242 * @check_usb_thermal_prot_work:
243 * Work for checking USB thermal status
244 * @charger_attached_mutex: For controlling the wakelock
246 struct ab8500_charger {
250 bool vbus_detected_start;
256 bool usb_device_is_unrecognised;
259 int invalid_charger_detect_state;
260 struct ab8500 *parent;
261 struct ab8500_gpadc *gpadc;
262 struct abx500_bm_data *bm;
263 struct ab8500_charger_event_flags flags;
264 struct ab8500_charger_usb_state usb_state;
265 struct ux500_charger ac_chg;
266 struct ux500_charger usb_chg;
267 struct ab8500_charger_info ac;
268 struct ab8500_charger_info usb;
269 struct regulator *regu;
270 struct workqueue_struct *charger_wq;
271 struct delayed_work check_vbat_work;
272 struct delayed_work check_hw_failure_work;
273 struct delayed_work check_usbchgnotok_work;
274 struct delayed_work kick_wd_work;
275 struct delayed_work attach_work;
276 struct delayed_work ac_charger_attached_work;
277 struct delayed_work usb_charger_attached_work;
278 struct work_struct ac_work;
279 struct work_struct detect_usb_type_work;
280 struct work_struct usb_link_status_work;
281 struct work_struct usb_state_changed_work;
282 struct work_struct check_main_thermal_prot_work;
283 struct work_struct check_usb_thermal_prot_work;
284 struct usb_phy *usb_phy;
285 struct notifier_block nb;
286 struct mutex charger_attached_mutex;
290 static enum power_supply_property ab8500_charger_ac_props[] = {
291 POWER_SUPPLY_PROP_HEALTH,
292 POWER_SUPPLY_PROP_PRESENT,
293 POWER_SUPPLY_PROP_ONLINE,
294 POWER_SUPPLY_PROP_VOLTAGE_NOW,
295 POWER_SUPPLY_PROP_VOLTAGE_AVG,
296 POWER_SUPPLY_PROP_CURRENT_NOW,
300 static enum power_supply_property ab8500_charger_usb_props[] = {
301 POWER_SUPPLY_PROP_HEALTH,
302 POWER_SUPPLY_PROP_CURRENT_AVG,
303 POWER_SUPPLY_PROP_PRESENT,
304 POWER_SUPPLY_PROP_ONLINE,
305 POWER_SUPPLY_PROP_VOLTAGE_NOW,
306 POWER_SUPPLY_PROP_VOLTAGE_AVG,
307 POWER_SUPPLY_PROP_CURRENT_NOW,
311 * Function for enabling and disabling sw fallback mode
312 * should always be disabled when no charger is connected.
314 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
323 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
325 if (is_ab8500(di->parent)) {
330 bank = AB8500_SYS_CTRL1_BLOCK;
331 reg = AB8500_SW_CONTROL_FALLBACK;
335 /* read the register containing fallback bit */
336 ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
338 dev_err(di->dev, "%d read failed\n", __LINE__);
342 if (is_ab8500(di->parent)) {
343 /* enable the OPT emulation registers */
344 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
346 dev_err(di->dev, "%d write failed\n", __LINE__);
356 /* write back the changed fallback bit value to register */
357 ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
359 dev_err(di->dev, "%d write failed\n", __LINE__);
363 if (is_ab8500(di->parent)) {
364 /* disable the set OTP registers again */
365 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
367 dev_err(di->dev, "%d write failed\n", __LINE__);
373 * ab8500_power_supply_changed - a wrapper with local extentions for
374 * power_supply_changed
375 * @di: pointer to the ab8500_charger structure
376 * @psy: pointer to power_supply_that have changed.
379 static void ab8500_power_supply_changed(struct ab8500_charger *di,
380 struct power_supply *psy)
382 if (di->autopower_cfg) {
383 if (!di->usb.charger_connected &&
384 !di->ac.charger_connected &&
386 di->autopower = false;
387 ab8500_enable_disable_sw_fallback(di, false);
388 } else if (!di->autopower &&
389 (di->ac.charger_connected ||
390 di->usb.charger_connected)) {
391 di->autopower = true;
392 ab8500_enable_disable_sw_fallback(di, true);
395 power_supply_changed(psy);
398 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
401 if (connected != di->usb.charger_connected) {
402 dev_dbg(di->dev, "USB connected:%i\n", connected);
403 di->usb.charger_connected = connected;
404 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
407 mutex_lock(&di->charger_attached_mutex);
408 mutex_unlock(&di->charger_attached_mutex);
410 queue_delayed_work(di->charger_wq,
411 &di->usb_charger_attached_work,
414 cancel_delayed_work_sync(&di->usb_charger_attached_work);
415 mutex_lock(&di->charger_attached_mutex);
416 mutex_unlock(&di->charger_attached_mutex);
422 * ab8500_charger_get_ac_voltage() - get ac charger voltage
423 * @di: pointer to the ab8500_charger structure
425 * Returns ac charger voltage (on success)
427 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
431 /* Only measure voltage if the charger is connected */
432 if (di->ac.charger_connected) {
433 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
435 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
443 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
444 * @di: pointer to the ab8500_charger structure
446 * Returns ac charger CV mode (on success) else error code
448 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
453 /* Only check CV mode if the charger is online */
454 if (di->ac.charger_online) {
455 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
456 AB8500_CH_STATUS1_REG, &val);
458 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
462 if (val & MAIN_CH_CV_ON)
472 * ab8500_charger_get_vbus_voltage() - get vbus voltage
473 * @di: pointer to the ab8500_charger structure
475 * This function returns the vbus voltage.
476 * Returns vbus voltage (on success)
478 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
482 /* Only measure voltage if the charger is connected */
483 if (di->usb.charger_connected) {
484 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
486 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
494 * ab8500_charger_get_usb_current() - get usb charger current
495 * @di: pointer to the ab8500_charger structure
497 * This function returns the usb charger current.
498 * Returns usb current (on success) and error code on failure
500 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
504 /* Only measure current if the charger is online */
505 if (di->usb.charger_online) {
506 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
508 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
516 * ab8500_charger_get_ac_current() - get ac charger current
517 * @di: pointer to the ab8500_charger structure
519 * This function returns the ac charger current.
520 * Returns ac current (on success) and error code on failure.
522 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
526 /* Only measure current if the charger is online */
527 if (di->ac.charger_online) {
528 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
530 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
538 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
539 * @di: pointer to the ab8500_charger structure
541 * Returns ac charger CV mode (on success) else error code
543 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
548 /* Only check CV mode if the charger is online */
549 if (di->usb.charger_online) {
550 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
551 AB8500_CH_USBCH_STAT1_REG, &val);
553 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
557 if (val & USB_CH_CV_ON)
569 * ab8500_charger_detect_chargers() - Detect the connected chargers
570 * @di: pointer to the ab8500_charger structure
572 * Returns the type of charger connected.
573 * For USB it will not mean we can actually charge from it
574 * but that there is a USB cable connected that we have to
575 * identify. This is used during startup when we don't get
576 * interrupts of the charger detection
578 * Returns an integer value, that means,
579 * NO_PW_CONN no power supply is connected
580 * AC_PW_CONN if the AC power supply is connected
581 * USB_PW_CONN if the USB power supply is connected
582 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
584 static int ab8500_charger_detect_chargers(struct ab8500_charger *di)
586 int result = NO_PW_CONN;
590 /* Check for AC charger */
591 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
592 AB8500_CH_STATUS1_REG, &val);
594 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
598 if (val & MAIN_CH_DET)
601 /* Check for USB charger */
602 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
603 AB8500_CH_USBCH_STAT1_REG, &val);
605 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
609 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
610 result |= USB_PW_CONN;
616 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
617 * @di: pointer to the ab8500_charger structure
618 * @link_status: the identified USB type
620 * Get the maximum current that is allowed to be drawn from the host
621 * based on the USB type.
622 * Returns error code in case of failure else 0 on success
624 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
625 enum ab8500_charger_link_status link_status)
629 di->usb_device_is_unrecognised = false;
631 switch (link_status) {
632 case USB_STAT_STD_HOST_NC:
633 case USB_STAT_STD_HOST_C_NS:
634 case USB_STAT_STD_HOST_C_S:
635 dev_dbg(di->dev, "USB Type - Standard host is "
636 "detected through USB driver\n");
637 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
639 case USB_STAT_HOST_CHG_HS_CHIRP:
640 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
641 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
642 di->max_usb_in_curr);
644 case USB_STAT_HOST_CHG_HS:
645 case USB_STAT_ACA_RID_C_HS:
646 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
647 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
648 di->max_usb_in_curr);
650 case USB_STAT_ACA_RID_A:
652 * Dedicated charger level minus maximum current accessory
653 * can consume (300mA). Closest level is 1100mA
655 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1;
656 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
657 di->max_usb_in_curr);
659 case USB_STAT_ACA_RID_B:
661 * Dedicated charger level minus 120mA (20mA for ACA and
662 * 100mA for potential accessory). Closest level is 1300mA
664 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
665 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
666 di->max_usb_in_curr);
668 case USB_STAT_HOST_CHG_NM:
669 case USB_STAT_DEDICATED_CHG:
670 case USB_STAT_ACA_RID_C_NM:
671 case USB_STAT_ACA_RID_C_HS_CHIRP:
672 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
673 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
674 di->max_usb_in_curr);
676 case USB_STAT_NOT_CONFIGURED:
677 if (di->vbus_detected) {
678 di->usb_device_is_unrecognised = true;
679 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
680 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
683 case USB_STAT_HM_IDGND:
684 dev_err(di->dev, "USB Type - Charging not allowed\n");
685 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
688 case USB_STAT_RESERVED:
689 if (is_ab8500(di->parent)) {
690 di->flags.vbus_collapse = true;
691 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
692 "VBUS has collapsed\n");
696 if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
697 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
698 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
699 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
700 link_status, di->max_usb_in_curr);
705 case USB_STAT_CARKIT_1:
706 case USB_STAT_CARKIT_2:
707 case USB_STAT_ACA_DOCK_CHARGER:
708 case USB_STAT_CHARGER_LINE_1:
709 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
710 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
711 di->max_usb_in_curr);
712 case USB_STAT_NOT_VALID_LINK:
713 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
714 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
718 dev_err(di->dev, "USB Type - Unknown\n");
719 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
724 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
725 link_status, di->max_usb_in_curr);
731 * ab8500_charger_read_usb_type() - read the type of usb connected
732 * @di: pointer to the ab8500_charger structure
734 * Detect the type of the plugged USB
735 * Returns error code in case of failure else 0 on success
737 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
742 ret = abx500_get_register_interruptible(di->dev,
743 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
745 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
748 if (is_ab8500(di->parent)) {
749 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
750 AB8500_USB_LINE_STAT_REG, &val);
752 if (is_ab9540(di->parent) || is_ab8505(di->parent))
753 ret = abx500_get_register_interruptible(di->dev,
754 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
757 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
761 /* get the USB type */
762 val = (val & AB8500_USB_LINK_STATUS) >> 3;
763 ret = ab8500_charger_max_usb_curr(di,
764 (enum ab8500_charger_link_status) val);
770 * ab8500_charger_detect_usb_type() - get the type of usb connected
771 * @di: pointer to the ab8500_charger structure
773 * Detect the type of the plugged USB
774 * Returns error code in case of failure else 0 on success
776 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
782 * On getting the VBUS rising edge detect interrupt there
783 * is a 250ms delay after which the register UsbLineStatus
784 * is filled with valid data.
786 for (i = 0; i < 10; i++) {
788 ret = abx500_get_register_interruptible(di->dev,
789 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
792 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
796 if (is_ab8500(di->parent))
797 ret = abx500_get_register_interruptible(di->dev,
798 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
800 ret = abx500_get_register_interruptible(di->dev,
801 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
803 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
807 * Until the IT source register is read the UsbLineStatus
808 * register is not updated, hence doing the same
812 /* get the USB type */
813 val = (val & AB8500_USB_LINK_STATUS) >> 3;
817 ret = ab8500_charger_max_usb_curr(di,
818 (enum ab8500_charger_link_status) val);
824 * This array maps the raw hex value to charger voltage used by the AB8500
825 * Values taken from the UM0836
827 static int ab8500_charger_voltage_map[] = {
909 * This array maps the raw hex value to charger current used by the AB8500
910 * Values taken from the UM0836
912 static int ab8500_charger_current_map[] = {
931 * This array maps the raw hex value to VBUS input current used by the AB8500
932 * Values taken from the UM0836
934 static int ab8500_charger_vbus_in_curr_map[] = {
935 USB_CH_IP_CUR_LVL_0P05,
936 USB_CH_IP_CUR_LVL_0P09,
937 USB_CH_IP_CUR_LVL_0P19,
938 USB_CH_IP_CUR_LVL_0P29,
939 USB_CH_IP_CUR_LVL_0P38,
940 USB_CH_IP_CUR_LVL_0P45,
941 USB_CH_IP_CUR_LVL_0P5,
942 USB_CH_IP_CUR_LVL_0P6,
943 USB_CH_IP_CUR_LVL_0P7,
944 USB_CH_IP_CUR_LVL_0P8,
945 USB_CH_IP_CUR_LVL_0P9,
946 USB_CH_IP_CUR_LVL_1P0,
947 USB_CH_IP_CUR_LVL_1P1,
948 USB_CH_IP_CUR_LVL_1P3,
949 USB_CH_IP_CUR_LVL_1P4,
950 USB_CH_IP_CUR_LVL_1P5,
953 static int ab8500_voltage_to_regval(int voltage)
957 /* Special case for voltage below 3.5V */
958 if (voltage < ab8500_charger_voltage_map[0])
961 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
962 if (voltage < ab8500_charger_voltage_map[i])
966 /* If not last element, return error */
967 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
968 if (voltage == ab8500_charger_voltage_map[i])
974 static int ab8500_current_to_regval(int curr)
978 if (curr < ab8500_charger_current_map[0])
981 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
982 if (curr < ab8500_charger_current_map[i])
986 /* If not last element, return error */
987 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
988 if (curr == ab8500_charger_current_map[i])
994 static int ab8500_vbus_in_curr_to_regval(int curr)
998 if (curr < ab8500_charger_vbus_in_curr_map[0])
1001 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
1002 if (curr < ab8500_charger_vbus_in_curr_map[i])
1006 /* If not last element, return error */
1007 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
1008 if (curr == ab8500_charger_vbus_in_curr_map[i])
1015 * ab8500_charger_get_usb_cur() - get usb current
1016 * @di: pointer to the ab8500_charger structre
1018 * The usb stack provides the maximum current that can be drawn from
1019 * the standard usb host. This will be in mA.
1020 * This function converts current in mA to a value that can be written
1021 * to the register. Returns -1 if charging is not allowed
1023 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1025 switch (di->usb_state.usb_current) {
1027 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
1030 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
1033 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
1036 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
1039 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
1042 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
1050 * ab8500_charger_set_current() - set charger current
1051 * @di: pointer to the ab8500_charger structure
1052 * @ich: charger current, in mA
1053 * @reg: select what charger register to set
1055 * Set charger current.
1056 * There is no state machine in the AB to step up/down the charger
1057 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1058 * charging is started. Instead we need to implement
1059 * this charger current step-up/down here.
1060 * Returns error code in case of failure else 0(on success)
1062 static int ab8500_charger_set_current(struct ab8500_charger *di,
1066 int curr_index, prev_curr_index, shift_value;
1070 case AB8500_MCH_IPT_CURLVL_REG:
1071 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1072 curr_index = ab8500_current_to_regval(ich);
1074 case AB8500_USBCH_IPT_CRNTLVL_REG:
1075 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1076 curr_index = ab8500_vbus_in_curr_to_regval(ich);
1078 case AB8500_CH_OPT_CRNTLVL_REG:
1080 curr_index = ab8500_current_to_regval(ich);
1083 dev_err(di->dev, "%s current register not valid\n", __func__);
1087 if (curr_index < 0) {
1088 dev_err(di->dev, "requested current limit out-of-range\n");
1092 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1095 dev_err(di->dev, "%s read failed\n", __func__);
1098 prev_curr_index = (reg_value >> shift_value);
1100 /* only update current if it's been changed */
1101 if (prev_curr_index == curr_index)
1104 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1105 __func__, ich, reg);
1107 if (prev_curr_index > curr_index) {
1108 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1109 ret = abx500_set_register_interruptible(di->dev,
1110 AB8500_CHARGER, reg, (u8) i << shift_value);
1112 dev_err(di->dev, "%s write failed\n", __func__);
1115 usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
1118 for (i = prev_curr_index + 1; i <= curr_index; i++) {
1119 ret = abx500_set_register_interruptible(di->dev,
1120 AB8500_CHARGER, reg, (u8) i << shift_value);
1122 dev_err(di->dev, "%s write failed\n", __func__);
1125 usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
1132 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1133 * @di: pointer to the ab8500_charger structure
1134 * @ich_in: charger input current limit
1136 * Sets the current that can be drawn from the USB host
1137 * Returns error code in case of failure else 0(on success)
1139 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1144 /* We should always use to lowest current limit */
1145 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1147 switch (min_value) {
1149 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1150 min_value = USB_CH_IP_CUR_LVL_0P05;
1153 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1154 min_value = USB_CH_IP_CUR_LVL_0P45;
1160 return ab8500_charger_set_current(di, min_value,
1161 AB8500_USBCH_IPT_CRNTLVL_REG);
1165 * ab8500_charger_set_main_in_curr() - set main charger input current
1166 * @di: pointer to the ab8500_charger structure
1167 * @ich_in: input charger current, in mA
1169 * Set main charger input current.
1170 * Returns error code in case of failure else 0(on success)
1172 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1175 return ab8500_charger_set_current(di, ich_in,
1176 AB8500_MCH_IPT_CURLVL_REG);
1180 * ab8500_charger_set_output_curr() - set charger output current
1181 * @di: pointer to the ab8500_charger structure
1182 * @ich_out: output charger current, in mA
1184 * Set charger output current.
1185 * Returns error code in case of failure else 0(on success)
1187 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1190 return ab8500_charger_set_current(di, ich_out,
1191 AB8500_CH_OPT_CRNTLVL_REG);
1195 * ab8500_charger_led_en() - turn on/off chargign led
1196 * @di: pointer to the ab8500_charger structure
1197 * @on: flag to turn on/off the chargign led
1199 * Power ON/OFF charging LED indication
1200 * Returns error code in case of failure else 0(on success)
1202 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1207 /* Power ON charging LED indicator, set LED current to 5mA */
1208 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1209 AB8500_LED_INDICATOR_PWM_CTRL,
1210 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1212 dev_err(di->dev, "Power ON LED failed\n");
1215 /* LED indicator PWM duty cycle 252/256 */
1216 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1217 AB8500_LED_INDICATOR_PWM_DUTY,
1218 LED_INDICATOR_PWM_DUTY_252_256);
1220 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1224 /* Power off charging LED indicator */
1225 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1226 AB8500_LED_INDICATOR_PWM_CTRL,
1227 LED_INDICATOR_PWM_DIS);
1229 dev_err(di->dev, "Power-off LED failed\n");
1238 * ab8500_charger_ac_en() - enable or disable ac charging
1239 * @di: pointer to the ab8500_charger structure
1240 * @enable: enable/disable flag
1241 * @vset: charging voltage
1242 * @iset: charging current
1244 * Enable/Disable AC/Mains charging and turns on/off the charging led
1247 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1248 int enable, int vset, int iset)
1253 int input_curr_index;
1256 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1259 /* Check if AC is connected */
1260 if (!di->ac.charger_connected) {
1261 dev_err(di->dev, "AC charger not connected\n");
1265 /* Enable AC charging */
1266 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1269 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1270 * will be triggered everytime we enable the VDD ADC supply.
1271 * This will turn off charging for a short while.
1272 * It can be avoided by having the supply on when
1273 * there is a charger enabled. Normally the VDD ADC supply
1274 * is enabled everytime a GPADC conversion is triggered. We will
1275 * force it to be enabled from this driver to have
1276 * the GPADC module independant of the AB8500 chargers
1278 if (!di->vddadc_en_ac) {
1279 regulator_enable(di->regu);
1280 di->vddadc_en_ac = true;
1283 /* Check if the requested voltage or current is valid */
1284 volt_index = ab8500_voltage_to_regval(vset);
1285 curr_index = ab8500_current_to_regval(iset);
1286 input_curr_index = ab8500_current_to_regval(
1287 di->bm->chg_params->ac_curr_max);
1288 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1290 "Charger voltage or current too high, "
1291 "charging not started\n");
1295 /* ChVoltLevel: maximum battery charging voltage */
1296 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1297 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1299 dev_err(di->dev, "%s write failed\n", __func__);
1302 /* MainChInputCurr: current that can be drawn from the charger*/
1303 ret = ab8500_charger_set_main_in_curr(di,
1304 di->bm->chg_params->ac_curr_max);
1306 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1310 /* ChOutputCurentLevel: protected output current */
1311 ret = ab8500_charger_set_output_curr(di, iset);
1313 dev_err(di->dev, "%s "
1314 "Failed to set ChOutputCurentLevel\n",
1319 /* Check if VBAT overshoot control should be enabled */
1320 if (!di->bm->enable_overshoot)
1321 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1323 /* Enable Main Charger */
1324 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1325 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1327 dev_err(di->dev, "%s write failed\n", __func__);
1331 /* Power on charging LED indication */
1332 ret = ab8500_charger_led_en(di, true);
1334 dev_err(di->dev, "failed to enable LED\n");
1336 di->ac.charger_online = 1;
1338 /* Disable AC charging */
1339 if (is_ab8500_1p1_or_earlier(di->parent)) {
1341 * For ABB revision 1.0 and 1.1 there is a bug in the
1342 * watchdog logic. That means we have to continously
1343 * kick the charger watchdog even when no charger is
1344 * connected. This is only valid once the AC charger
1345 * has been enabled. This is a bug that is not handled
1346 * by the algorithm and the watchdog have to be kicked
1347 * by the charger driver when the AC charger
1351 queue_delayed_work(di->charger_wq,
1353 round_jiffies(WD_KICK_INTERVAL));
1357 * We can't turn off charging completely
1358 * due to a bug in AB8500 cut1.
1359 * If we do, charging will not start again.
1360 * That is why we set the lowest voltage
1361 * and current possible
1363 ret = abx500_set_register_interruptible(di->dev,
1365 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1368 "%s write failed\n", __func__);
1372 ret = ab8500_charger_set_output_curr(di, 0);
1374 dev_err(di->dev, "%s "
1375 "Failed to set ChOutputCurentLevel\n",
1380 ret = abx500_set_register_interruptible(di->dev,
1382 AB8500_MCH_CTRL1, 0);
1385 "%s write failed\n", __func__);
1390 ret = ab8500_charger_led_en(di, false);
1392 dev_err(di->dev, "failed to disable LED\n");
1394 di->ac.charger_online = 0;
1395 di->ac.wd_expired = false;
1397 /* Disable regulator if enabled */
1398 if (di->vddadc_en_ac) {
1399 regulator_disable(di->regu);
1400 di->vddadc_en_ac = false;
1403 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1405 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1411 * ab8500_charger_usb_en() - enable usb charging
1412 * @di: pointer to the ab8500_charger structure
1413 * @enable: enable/disable flag
1414 * @vset: charging voltage
1415 * @ich_out: charger output current
1417 * Enable/Disable USB charging and turns on/off the charging led respectively.
1418 * Returns error code in case of failure else 0(on success)
1420 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1421 int enable, int vset, int ich_out)
1428 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1431 /* Check if USB is connected */
1432 if (!di->usb.charger_connected) {
1433 dev_err(di->dev, "USB charger not connected\n");
1438 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1439 * will be triggered everytime we enable the VDD ADC supply.
1440 * This will turn off charging for a short while.
1441 * It can be avoided by having the supply on when
1442 * there is a charger enabled. Normally the VDD ADC supply
1443 * is enabled everytime a GPADC conversion is triggered. We will
1444 * force it to be enabled from this driver to have
1445 * the GPADC module independant of the AB8500 chargers
1447 if (!di->vddadc_en_usb) {
1448 regulator_enable(di->regu);
1449 di->vddadc_en_usb = true;
1452 /* Enable USB charging */
1453 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1455 /* Check if the requested voltage or current is valid */
1456 volt_index = ab8500_voltage_to_regval(vset);
1457 curr_index = ab8500_current_to_regval(ich_out);
1458 if (volt_index < 0 || curr_index < 0) {
1460 "Charger voltage or current too high, "
1461 "charging not started\n");
1465 /* ChVoltLevel: max voltage upto which battery can be charged */
1466 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1467 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1469 dev_err(di->dev, "%s write failed\n", __func__);
1472 /* USBChInputCurr: current that can be drawn from the usb */
1473 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1475 dev_err(di->dev, "setting USBChInputCurr failed\n");
1478 /* ChOutputCurentLevel: protected output current */
1479 ret = ab8500_charger_set_output_curr(di, ich_out);
1481 dev_err(di->dev, "%s "
1482 "Failed to set ChOutputCurentLevel\n",
1486 /* Check if VBAT overshoot control should be enabled */
1487 if (!di->bm->enable_overshoot)
1488 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1490 /* Enable USB Charger */
1491 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1492 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1494 dev_err(di->dev, "%s write failed\n", __func__);
1498 /* If success power on charging LED indication */
1499 ret = ab8500_charger_led_en(di, true);
1501 dev_err(di->dev, "failed to enable LED\n");
1503 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1505 di->usb.charger_online = 1;
1507 /* Disable USB charging */
1508 ret = abx500_set_register_interruptible(di->dev,
1510 AB8500_USBCH_CTRL1_REG, 0);
1513 "%s write failed\n", __func__);
1517 ret = ab8500_charger_led_en(di, false);
1519 dev_err(di->dev, "failed to disable LED\n");
1521 di->usb.charger_online = 0;
1522 di->usb.wd_expired = false;
1524 /* Disable regulator if enabled */
1525 if (di->vddadc_en_usb) {
1526 regulator_disable(di->regu);
1527 di->vddadc_en_usb = false;
1530 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1532 /* Cancel any pending Vbat check work */
1533 if (delayed_work_pending(&di->check_vbat_work))
1534 cancel_delayed_work(&di->check_vbat_work);
1537 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1543 * ab8500_charger_watchdog_kick() - kick charger watchdog
1544 * @di: pointer to the ab8500_charger structure
1546 * Kick charger watchdog
1547 * Returns error code in case of failure else 0(on success)
1549 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1552 struct ab8500_charger *di;
1554 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1555 di = to_ab8500_charger_ac_device_info(charger);
1556 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1557 di = to_ab8500_charger_usb_device_info(charger);
1561 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1562 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1564 dev_err(di->dev, "Failed to kick WD!\n");
1570 * ab8500_charger_update_charger_current() - update charger current
1571 * @di: pointer to the ab8500_charger structure
1573 * Update the charger output current for the specified charger
1574 * Returns error code in case of failure else 0(on success)
1576 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1580 struct ab8500_charger *di;
1582 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1583 di = to_ab8500_charger_ac_device_info(charger);
1584 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1585 di = to_ab8500_charger_usb_device_info(charger);
1589 ret = ab8500_charger_set_output_curr(di, ich_out);
1591 dev_err(di->dev, "%s "
1592 "Failed to set ChOutputCurentLevel\n",
1597 /* Reset the main and usb drop input current measurement counter */
1598 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1599 AB8500_CHARGER_CTRL,
1602 dev_err(di->dev, "%s write failed\n", __func__);
1609 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1611 struct power_supply *psy;
1612 struct power_supply *ext;
1613 struct ab8500_charger *di;
1614 union power_supply_propval ret;
1616 bool psy_found = false;
1617 struct ux500_charger *usb_chg;
1619 usb_chg = (struct ux500_charger *)data;
1620 psy = &usb_chg->psy;
1622 di = to_ab8500_charger_usb_device_info(usb_chg);
1624 ext = dev_get_drvdata(dev);
1626 /* For all psy where the driver name appears in any supplied_to */
1627 for (i = 0; i < ext->num_supplicants; i++) {
1628 if (!strcmp(ext->supplied_to[i], psy->name))
1635 /* Go through all properties for the psy */
1636 for (j = 0; j < ext->num_properties; j++) {
1637 enum power_supply_property prop;
1638 prop = ext->properties[j];
1640 if (ext->get_property(ext, prop, &ret))
1644 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1645 switch (ext->type) {
1646 case POWER_SUPPLY_TYPE_BATTERY:
1647 di->vbat = ret.intval / 1000;
1661 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1662 * @work pointer to the work_struct structure
1664 * Due to a asic bug it is necessary to lower the input current to the vbus
1665 * charger when charging with at some specific levels. This issue is only valid
1666 * for below a certain battery voltage. This function makes sure that the
1667 * the allowed current limit isn't exceeded.
1669 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1672 struct ab8500_charger *di = container_of(work,
1673 struct ab8500_charger, check_vbat_work.work);
1675 class_for_each_device(power_supply_class, NULL,
1676 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1678 /* First run old_vbat is 0. */
1679 if (di->old_vbat == 0)
1680 di->old_vbat = di->vbat;
1682 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1683 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1684 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1685 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1687 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1688 " old: %d\n", di->max_usb_in_curr, di->vbat,
1690 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1691 power_supply_changed(&di->usb_chg.psy);
1694 di->old_vbat = di->vbat;
1697 * No need to check the battery voltage every second when not close to
1700 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1701 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1704 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1708 * ab8500_charger_check_hw_failure_work() - check main charger failure
1709 * @work: pointer to the work_struct structure
1711 * Work queue function for checking the main charger status
1713 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1718 struct ab8500_charger *di = container_of(work,
1719 struct ab8500_charger, check_hw_failure_work.work);
1721 /* Check if the status bits for HW failure is still active */
1722 if (di->flags.mainextchnotok) {
1723 ret = abx500_get_register_interruptible(di->dev,
1724 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1726 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1729 if (!(reg_value & MAIN_CH_NOK)) {
1730 di->flags.mainextchnotok = false;
1731 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1734 if (di->flags.vbus_ovv) {
1735 ret = abx500_get_register_interruptible(di->dev,
1736 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1739 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1742 if (!(reg_value & VBUS_OVV_TH)) {
1743 di->flags.vbus_ovv = false;
1744 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1747 /* If we still have a failure, schedule a new check */
1748 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1749 queue_delayed_work(di->charger_wq,
1750 &di->check_hw_failure_work, round_jiffies(HZ));
1755 * ab8500_charger_kick_watchdog_work() - kick the watchdog
1756 * @work: pointer to the work_struct structure
1758 * Work queue function for kicking the charger watchdog.
1760 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
1761 * logic. That means we have to continously kick the charger
1762 * watchdog even when no charger is connected. This is only
1763 * valid once the AC charger has been enabled. This is
1764 * a bug that is not handled by the algorithm and the
1765 * watchdog have to be kicked by the charger driver
1766 * when the AC charger is disabled
1768 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
1772 struct ab8500_charger *di = container_of(work,
1773 struct ab8500_charger, kick_wd_work.work);
1775 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1776 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1778 dev_err(di->dev, "Failed to kick WD!\n");
1780 /* Schedule a new watchdog kick */
1781 queue_delayed_work(di->charger_wq,
1782 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
1786 * ab8500_charger_ac_work() - work to get and set main charger status
1787 * @work: pointer to the work_struct structure
1789 * Work queue function for checking the main charger status
1791 static void ab8500_charger_ac_work(struct work_struct *work)
1795 struct ab8500_charger *di = container_of(work,
1796 struct ab8500_charger, ac_work);
1799 * Since we can't be sure that the events are received
1800 * synchronously, we have the check if the main charger is
1801 * connected by reading the status register
1803 ret = ab8500_charger_detect_chargers(di);
1807 if (ret & AC_PW_CONN) {
1808 di->ac.charger_connected = 1;
1811 di->ac.charger_connected = 0;
1814 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1815 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
1818 static void ab8500_charger_usb_attached_work(struct work_struct *work)
1820 struct ab8500_charger *di = container_of(work,
1821 struct ab8500_charger,
1822 usb_charger_attached_work.work);
1823 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
1827 for (i = 0; i < 10; i++) {
1828 ret = abx500_get_register_interruptible(di->dev,
1830 AB8500_CH_USBCH_STAT1_REG,
1833 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1836 if ((statval & usbch) != usbch)
1839 msleep(CHARGER_STATUS_POLL);
1842 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
1844 mutex_lock(&di->charger_attached_mutex);
1845 mutex_unlock(&di->charger_attached_mutex);
1850 queue_delayed_work(di->charger_wq,
1851 &di->usb_charger_attached_work,
1855 static void ab8500_charger_ac_attached_work(struct work_struct *work)
1858 struct ab8500_charger *di = container_of(work,
1859 struct ab8500_charger,
1860 ac_charger_attached_work.work);
1861 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
1862 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
1866 for (i = 0; i < 10; i++) {
1867 ret = abx500_get_register_interruptible(di->dev,
1869 AB8500_CH_STATUS2_REG,
1872 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1876 if ((statval & mainch) != mainch)
1879 msleep(CHARGER_STATUS_POLL);
1882 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
1883 queue_work(di->charger_wq, &di->ac_work);
1885 mutex_lock(&di->charger_attached_mutex);
1886 mutex_unlock(&di->charger_attached_mutex);
1891 queue_delayed_work(di->charger_wq,
1892 &di->ac_charger_attached_work,
1897 * ab8500_charger_detect_usb_type_work() - work to detect USB type
1898 * @work: Pointer to the work_struct structure
1900 * Detect the type of USB plugged
1902 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
1906 struct ab8500_charger *di = container_of(work,
1907 struct ab8500_charger, detect_usb_type_work);
1910 * Since we can't be sure that the events are received
1911 * synchronously, we have the check if is
1912 * connected by reading the status register
1914 ret = ab8500_charger_detect_chargers(di);
1918 if (!(ret & USB_PW_CONN)) {
1919 di->vbus_detected = 0;
1920 ab8500_charger_set_usb_connected(di, false);
1921 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1923 di->vbus_detected = 1;
1925 if (is_ab8500_1p1_or_earlier(di->parent)) {
1926 ret = ab8500_charger_detect_usb_type(di);
1928 ab8500_charger_set_usb_connected(di, true);
1929 ab8500_power_supply_changed(di,
1933 /* For ABB cut2.0 and onwards we have an IRQ,
1934 * USB_LINK_STATUS that will be triggered when the USB
1935 * link status changes. The exception is USB connected
1936 * during startup. Then we don't get a
1937 * USB_LINK_STATUS IRQ
1939 if (di->vbus_detected_start) {
1940 di->vbus_detected_start = false;
1941 ret = ab8500_charger_detect_usb_type(di);
1943 ab8500_charger_set_usb_connected(di,
1945 ab8500_power_supply_changed(di,
1954 * ab8500_charger_usb_link_attach_work() - delayd work to detect USB type
1955 * @work: pointer to the work_struct structure
1957 * Detect the type of USB plugged
1959 static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
1961 struct ab8500_charger *di =
1962 container_of(work, struct ab8500_charger, attach_work.work);
1965 /* Update maximum input current if USB enumeration is not detected */
1966 if (!di->usb.charger_online) {
1967 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1972 ab8500_charger_set_usb_connected(di, true);
1973 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1977 * ab8500_charger_usb_link_status_work() - work to detect USB type
1978 * @work: pointer to the work_struct structure
1980 * Detect the type of USB plugged
1982 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
1984 int detected_chargers;
1988 struct ab8500_charger *di = container_of(work,
1989 struct ab8500_charger, usb_link_status_work);
1992 * Since we can't be sure that the events are received
1993 * synchronously, we have the check if is
1994 * connected by reading the status register
1996 detected_chargers = ab8500_charger_detect_chargers(di);
1997 if (detected_chargers < 0)
2001 * Some chargers that breaks the USB spec is
2002 * identified as invalid by AB8500 and it refuse
2003 * to start the charging process. but by jumping
2004 * thru a few hoops it can be forced to start.
2006 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2007 AB8500_USB_LINE_STAT_REG, &val);
2009 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2011 dev_dbg(di->dev, "Error reading USB link status\n");
2013 if (detected_chargers & USB_PW_CONN) {
2014 if (((val & AB8500_USB_LINK_STATUS) >> 3) == USB_STAT_NOT_VALID_LINK &&
2015 di->invalid_charger_detect_state == 0) {
2016 dev_dbg(di->dev, "Invalid charger detected, state= 0\n");
2018 abx500_mask_and_set_register_interruptible(di->dev,
2019 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG, 0x01, 0x01);
2020 /*Enable charger detection*/
2021 abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2022 AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x01);
2023 di->invalid_charger_detect_state = 1;
2024 /*exit and wait for new link status interrupt.*/
2028 if (di->invalid_charger_detect_state == 1) {
2029 dev_dbg(di->dev, "Invalid charger detected, state= 1\n");
2030 /*Stop charger detection*/
2031 abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2032 AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x00);
2033 /*Check link status*/
2034 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2035 AB8500_USB_LINE_STAT_REG, &val);
2036 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2037 (val & AB8500_USB_LINK_STATUS) >> 3);
2038 di->invalid_charger_detect_state = 2;
2041 di->invalid_charger_detect_state = 0;
2044 if (!(detected_chargers & USB_PW_CONN)) {
2045 di->vbus_detected = 0;
2046 ab8500_charger_set_usb_connected(di, false);
2047 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2051 di->vbus_detected = 1;
2052 ret = ab8500_charger_read_usb_type(di);
2054 if (di->usb_device_is_unrecognised) {
2056 "Potential Legacy Charger device. "
2057 "Delay work for %d msec for USB enum "
2059 WAIT_FOR_USB_ENUMERATION);
2060 queue_delayed_work(di->charger_wq,
2062 msecs_to_jiffies(WAIT_FOR_USB_ENUMERATION));
2064 queue_delayed_work(di->charger_wq,
2065 &di->attach_work, 0);
2067 } else if (ret == -ENXIO) {
2068 /* No valid charger type detected */
2069 ab8500_charger_set_usb_connected(di, false);
2070 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2074 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2077 unsigned long flags;
2079 struct ab8500_charger *di = container_of(work,
2080 struct ab8500_charger, usb_state_changed_work);
2082 if (!di->vbus_detected)
2085 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2086 di->usb_state.usb_changed = false;
2087 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2090 * wait for some time until you get updates from the usb stack
2091 * and negotiations are completed
2095 if (di->usb_state.usb_changed)
2098 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2099 __func__, di->usb_state.state, di->usb_state.usb_current);
2101 switch (di->usb_state.state) {
2102 case AB8500_BM_USB_STATE_RESET_HS:
2103 case AB8500_BM_USB_STATE_RESET_FS:
2104 case AB8500_BM_USB_STATE_SUSPEND:
2105 case AB8500_BM_USB_STATE_MAX:
2106 ab8500_charger_set_usb_connected(di, false);
2107 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2110 case AB8500_BM_USB_STATE_RESUME:
2112 * when suspend->resume there should be delay
2113 * of 1sec for enabling charging
2116 /* Intentional fall through */
2117 case AB8500_BM_USB_STATE_CONFIGURED:
2119 * USB is configured, enable charging with the charging
2120 * input current obtained from USB driver
2122 if (!ab8500_charger_get_usb_cur(di)) {
2123 /* Update maximum input current */
2124 ret = ab8500_charger_set_vbus_in_curr(di,
2125 di->max_usb_in_curr);
2129 ab8500_charger_set_usb_connected(di, true);
2130 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2140 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2141 * @work: pointer to the work_struct structure
2143 * Work queue function for checking the USB charger Not OK status
2145 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2151 struct ab8500_charger *di = container_of(work,
2152 struct ab8500_charger, check_usbchgnotok_work.work);
2154 /* Check if the status bit for usbchargernotok is still active */
2155 ret = abx500_get_register_interruptible(di->dev,
2156 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2158 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2161 prev_status = di->flags.usbchargernotok;
2163 if (reg_value & VBUS_CH_NOK) {
2164 di->flags.usbchargernotok = true;
2165 /* Check again in 1sec */
2166 queue_delayed_work(di->charger_wq,
2167 &di->check_usbchgnotok_work, HZ);
2169 di->flags.usbchargernotok = false;
2170 di->flags.vbus_collapse = false;
2173 if (prev_status != di->flags.usbchargernotok)
2174 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2178 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2179 * @work: pointer to the work_struct structure
2181 * Work queue function for checking the Main thermal prot status
2183 static void ab8500_charger_check_main_thermal_prot_work(
2184 struct work_struct *work)
2189 struct ab8500_charger *di = container_of(work,
2190 struct ab8500_charger, check_main_thermal_prot_work);
2192 /* Check if the status bit for main_thermal_prot is still active */
2193 ret = abx500_get_register_interruptible(di->dev,
2194 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2196 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2199 if (reg_value & MAIN_CH_TH_PROT)
2200 di->flags.main_thermal_prot = true;
2202 di->flags.main_thermal_prot = false;
2204 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2208 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2209 * @work: pointer to the work_struct structure
2211 * Work queue function for checking the USB thermal prot status
2213 static void ab8500_charger_check_usb_thermal_prot_work(
2214 struct work_struct *work)
2219 struct ab8500_charger *di = container_of(work,
2220 struct ab8500_charger, check_usb_thermal_prot_work);
2222 /* Check if the status bit for usb_thermal_prot is still active */
2223 ret = abx500_get_register_interruptible(di->dev,
2224 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2226 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2229 if (reg_value & USB_CH_TH_PROT)
2230 di->flags.usb_thermal_prot = true;
2232 di->flags.usb_thermal_prot = false;
2234 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2238 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2239 * @irq: interrupt number
2240 * @_di: pointer to the ab8500_charger structure
2242 * Returns IRQ status(IRQ_HANDLED)
2244 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2246 struct ab8500_charger *di = _di;
2248 dev_dbg(di->dev, "Main charger unplugged\n");
2249 queue_work(di->charger_wq, &di->ac_work);
2251 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2252 mutex_lock(&di->charger_attached_mutex);
2253 mutex_unlock(&di->charger_attached_mutex);
2259 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2260 * @irq: interrupt number
2261 * @_di: pointer to the ab8500_charger structure
2263 * Returns IRQ status(IRQ_HANDLED)
2265 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2267 struct ab8500_charger *di = _di;
2269 dev_dbg(di->dev, "Main charger plugged\n");
2270 queue_work(di->charger_wq, &di->ac_work);
2272 mutex_lock(&di->charger_attached_mutex);
2273 mutex_unlock(&di->charger_attached_mutex);
2274 queue_delayed_work(di->charger_wq,
2275 &di->ac_charger_attached_work,
2281 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2282 * @irq: interrupt number
2283 * @_di: pointer to the ab8500_charger structure
2285 * Returns IRQ status(IRQ_HANDLED)
2287 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2289 struct ab8500_charger *di = _di;
2291 dev_dbg(di->dev, "Main charger not ok\n");
2292 di->flags.mainextchnotok = true;
2293 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2295 /* Schedule a new HW failure check */
2296 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2302 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2303 * thermal protection threshold
2304 * @irq: interrupt number
2305 * @_di: pointer to the ab8500_charger structure
2307 * Returns IRQ status(IRQ_HANDLED)
2309 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2311 struct ab8500_charger *di = _di;
2314 "Die temp above Main charger thermal protection threshold\n");
2315 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2321 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2322 * thermal protection threshold
2323 * @irq: interrupt number
2324 * @_di: pointer to the ab8500_charger structure
2326 * Returns IRQ status(IRQ_HANDLED)
2328 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2330 struct ab8500_charger *di = _di;
2333 "Die temp ok for Main charger thermal protection threshold\n");
2334 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2340 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2341 * @irq: interrupt number
2342 * @_di: pointer to the ab8500_charger structure
2344 * Returns IRQ status(IRQ_HANDLED)
2346 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2348 struct ab8500_charger *di = _di;
2350 dev_dbg(di->dev, "VBUS falling detected\n");
2351 queue_work(di->charger_wq, &di->detect_usb_type_work);
2357 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2358 * @irq: interrupt number
2359 * @_di: pointer to the ab8500_charger structure
2361 * Returns IRQ status(IRQ_HANDLED)
2363 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2365 struct ab8500_charger *di = _di;
2367 di->vbus_detected = true;
2368 dev_dbg(di->dev, "VBUS rising detected\n");
2369 queue_work(di->charger_wq, &di->detect_usb_type_work);
2375 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2376 * @irq: interrupt number
2377 * @_di: pointer to the ab8500_charger structure
2379 * Returns IRQ status(IRQ_HANDLED)
2381 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2383 struct ab8500_charger *di = _di;
2385 dev_dbg(di->dev, "USB link status changed\n");
2387 queue_work(di->charger_wq, &di->usb_link_status_work);
2393 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2394 * thermal protection threshold
2395 * @irq: interrupt number
2396 * @_di: pointer to the ab8500_charger structure
2398 * Returns IRQ status(IRQ_HANDLED)
2400 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2402 struct ab8500_charger *di = _di;
2405 "Die temp above USB charger thermal protection threshold\n");
2406 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2412 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2413 * thermal protection threshold
2414 * @irq: interrupt number
2415 * @_di: pointer to the ab8500_charger structure
2417 * Returns IRQ status(IRQ_HANDLED)
2419 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2421 struct ab8500_charger *di = _di;
2424 "Die temp ok for USB charger thermal protection threshold\n");
2425 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2431 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2432 * @irq: interrupt number
2433 * @_di: pointer to the ab8500_charger structure
2435 * Returns IRQ status(IRQ_HANDLED)
2437 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2439 struct ab8500_charger *di = _di;
2441 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2442 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2448 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2449 * @irq: interrupt number
2450 * @_di: pointer to the ab8500_charger structure
2452 * Returns IRQ status(IRQ_HANDLED)
2454 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2456 struct ab8500_charger *di = _di;
2458 dev_dbg(di->dev, "Charger watchdog expired\n");
2461 * The charger that was online when the watchdog expired
2462 * needs to be restarted for charging to start again
2464 if (di->ac.charger_online) {
2465 di->ac.wd_expired = true;
2466 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2468 if (di->usb.charger_online) {
2469 di->usb.wd_expired = true;
2470 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2477 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2478 * @irq: interrupt number
2479 * @_di: pointer to the ab8500_charger structure
2481 * Returns IRQ status(IRQ_HANDLED)
2483 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2485 struct ab8500_charger *di = _di;
2487 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2488 di->flags.vbus_ovv = true;
2489 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2491 /* Schedule a new HW failure check */
2492 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2498 * ab8500_charger_ac_get_property() - get the ac/mains properties
2499 * @psy: pointer to the power_supply structure
2500 * @psp: pointer to the power_supply_property structure
2501 * @val: pointer to the power_supply_propval union
2503 * This function gets called when an application tries to get the ac/mains
2504 * properties by reading the sysfs files.
2505 * AC/Mains properties are online, present and voltage.
2506 * online: ac/mains charging is in progress or not
2507 * present: presence of the ac/mains
2508 * voltage: AC/Mains voltage
2509 * Returns error code in case of failure else 0(on success)
2511 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2512 enum power_supply_property psp,
2513 union power_supply_propval *val)
2515 struct ab8500_charger *di;
2518 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2521 case POWER_SUPPLY_PROP_HEALTH:
2522 if (di->flags.mainextchnotok)
2523 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2524 else if (di->ac.wd_expired || di->usb.wd_expired)
2525 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2526 else if (di->flags.main_thermal_prot)
2527 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2529 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2531 case POWER_SUPPLY_PROP_ONLINE:
2532 val->intval = di->ac.charger_online;
2534 case POWER_SUPPLY_PROP_PRESENT:
2535 val->intval = di->ac.charger_connected;
2537 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2538 ret = ab8500_charger_get_ac_voltage(di);
2540 di->ac.charger_voltage = ret;
2541 /* On error, use previous value */
2542 val->intval = di->ac.charger_voltage * 1000;
2544 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2546 * This property is used to indicate when CV mode is entered
2547 * for the AC charger
2549 di->ac.cv_active = ab8500_charger_ac_cv(di);
2550 val->intval = di->ac.cv_active;
2552 case POWER_SUPPLY_PROP_CURRENT_NOW:
2553 ret = ab8500_charger_get_ac_current(di);
2555 di->ac.charger_current = ret;
2556 val->intval = di->ac.charger_current * 1000;
2565 * ab8500_charger_usb_get_property() - get the usb properties
2566 * @psy: pointer to the power_supply structure
2567 * @psp: pointer to the power_supply_property structure
2568 * @val: pointer to the power_supply_propval union
2570 * This function gets called when an application tries to get the usb
2571 * properties by reading the sysfs files.
2572 * USB properties are online, present and voltage.
2573 * online: usb charging is in progress or not
2574 * present: presence of the usb
2575 * voltage: vbus voltage
2576 * Returns error code in case of failure else 0(on success)
2578 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2579 enum power_supply_property psp,
2580 union power_supply_propval *val)
2582 struct ab8500_charger *di;
2585 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2588 case POWER_SUPPLY_PROP_HEALTH:
2589 if (di->flags.usbchargernotok)
2590 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2591 else if (di->ac.wd_expired || di->usb.wd_expired)
2592 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2593 else if (di->flags.usb_thermal_prot)
2594 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2595 else if (di->flags.vbus_ovv)
2596 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2598 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2600 case POWER_SUPPLY_PROP_ONLINE:
2601 val->intval = di->usb.charger_online;
2603 case POWER_SUPPLY_PROP_PRESENT:
2604 val->intval = di->usb.charger_connected;
2606 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2607 ret = ab8500_charger_get_vbus_voltage(di);
2609 di->usb.charger_voltage = ret;
2610 val->intval = di->usb.charger_voltage * 1000;
2612 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2614 * This property is used to indicate when CV mode is entered
2615 * for the USB charger
2617 di->usb.cv_active = ab8500_charger_usb_cv(di);
2618 val->intval = di->usb.cv_active;
2620 case POWER_SUPPLY_PROP_CURRENT_NOW:
2621 ret = ab8500_charger_get_usb_current(di);
2623 di->usb.charger_current = ret;
2624 val->intval = di->usb.charger_current * 1000;
2626 case POWER_SUPPLY_PROP_CURRENT_AVG:
2628 * This property is used to indicate when VBUS has collapsed
2629 * due to too high output current from the USB charger
2631 if (di->flags.vbus_collapse)
2643 * ab8500_charger_init_hw_registers() - Set up charger related registers
2644 * @di: pointer to the ab8500_charger structure
2646 * Set up charger OVV, watchdog and maximum voltage registers as well as
2647 * charging of the backup battery
2649 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
2653 /* Setup maximum charger current and voltage for ABB cut2.0 */
2654 if (!is_ab8500_1p1_or_earlier(di->parent)) {
2655 ret = abx500_set_register_interruptible(di->dev,
2657 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
2660 "failed to set CH_VOLT_LVL_MAX_REG\n");
2664 ret = abx500_set_register_interruptible(di->dev,
2666 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
2669 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2674 /* VBUS OVV set to 6.3V and enable automatic current limitiation */
2675 ret = abx500_set_register_interruptible(di->dev,
2677 AB8500_USBCH_CTRL2_REG,
2678 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
2680 dev_err(di->dev, "failed to set VBUS OVV\n");
2684 /* Enable main watchdog in OTP */
2685 ret = abx500_set_register_interruptible(di->dev,
2686 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
2688 dev_err(di->dev, "failed to enable main WD in OTP\n");
2692 /* Enable main watchdog */
2693 ret = abx500_set_register_interruptible(di->dev,
2694 AB8500_SYS_CTRL2_BLOCK,
2695 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
2697 dev_err(di->dev, "faile to enable main watchdog\n");
2702 * Due to internal synchronisation, Enable and Kick watchdog bits
2703 * cannot be enabled in a single write.
2704 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
2705 * between writing Enable then Kick bits.
2709 /* Kick main watchdog */
2710 ret = abx500_set_register_interruptible(di->dev,
2711 AB8500_SYS_CTRL2_BLOCK,
2712 AB8500_MAIN_WDOG_CTRL_REG,
2713 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
2715 dev_err(di->dev, "failed to kick main watchdog\n");
2719 /* Disable main watchdog */
2720 ret = abx500_set_register_interruptible(di->dev,
2721 AB8500_SYS_CTRL2_BLOCK,
2722 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
2724 dev_err(di->dev, "failed to disable main watchdog\n");
2728 /* Set watchdog timeout */
2729 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2730 AB8500_CH_WD_TIMER_REG, WD_TIMER);
2732 dev_err(di->dev, "failed to set charger watchdog timeout\n");
2736 /* Backup battery voltage and current */
2737 ret = abx500_set_register_interruptible(di->dev,
2739 AB8500_RTC_BACKUP_CHG_REG,
2740 di->bm->bkup_bat_v |
2741 di->bm->bkup_bat_i);
2743 dev_err(di->dev, "failed to setup backup battery charging\n");
2747 /* Enable backup battery charging */
2748 abx500_mask_and_set_register_interruptible(di->dev,
2749 AB8500_RTC, AB8500_RTC_CTRL_REG,
2750 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
2752 dev_err(di->dev, "%s mask and set failed\n", __func__);
2759 * ab8500 charger driver interrupts and their respective isr
2761 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
2762 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
2763 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
2764 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
2765 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
2766 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
2767 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
2768 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
2769 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
2770 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
2771 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
2772 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
2773 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
2774 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
2777 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
2778 unsigned long event, void *power)
2780 struct ab8500_charger *di =
2781 container_of(nb, struct ab8500_charger, nb);
2782 enum ab8500_usb_state bm_usb_state;
2783 unsigned mA = *((unsigned *)power);
2788 if (event != USB_EVENT_VBUS) {
2789 dev_dbg(di->dev, "not a standard host, returning\n");
2793 /* TODO: State is fabricate here. See if charger really needs USB
2794 * state or if mA is enough
2796 if ((di->usb_state.usb_current == 2) && (mA > 2))
2797 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
2799 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
2801 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
2802 else if (mA >= 8) /* 8, 100, 500 */
2803 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
2804 else /* Should never occur */
2805 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
2807 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
2808 __func__, bm_usb_state, mA);
2810 spin_lock(&di->usb_state.usb_lock);
2811 di->usb_state.usb_changed = true;
2812 spin_unlock(&di->usb_state.usb_lock);
2814 di->usb_state.state = bm_usb_state;
2815 di->usb_state.usb_current = mA;
2817 queue_work(di->charger_wq, &di->usb_state_changed_work);
2822 #if defined(CONFIG_PM)
2823 static int ab8500_charger_resume(struct platform_device *pdev)
2826 struct ab8500_charger *di = platform_get_drvdata(pdev);
2829 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2830 * logic. That means we have to continously kick the charger
2831 * watchdog even when no charger is connected. This is only
2832 * valid once the AC charger has been enabled. This is
2833 * a bug that is not handled by the algorithm and the
2834 * watchdog have to be kicked by the charger driver
2835 * when the AC charger is disabled
2837 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
2838 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2839 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2841 dev_err(di->dev, "Failed to kick WD!\n");
2843 /* If not already pending start a new timer */
2844 if (!delayed_work_pending(
2845 &di->kick_wd_work)) {
2846 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
2847 round_jiffies(WD_KICK_INTERVAL));
2851 /* If we still have a HW failure, schedule a new check */
2852 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2853 queue_delayed_work(di->charger_wq,
2854 &di->check_hw_failure_work, 0);
2860 static int ab8500_charger_suspend(struct platform_device *pdev,
2863 struct ab8500_charger *di = platform_get_drvdata(pdev);
2865 /* Cancel any pending HW failure check */
2866 if (delayed_work_pending(&di->check_hw_failure_work))
2867 cancel_delayed_work(&di->check_hw_failure_work);
2872 #define ab8500_charger_suspend NULL
2873 #define ab8500_charger_resume NULL
2876 static int ab8500_charger_remove(struct platform_device *pdev)
2878 struct ab8500_charger *di = platform_get_drvdata(pdev);
2881 /* Disable AC charging */
2882 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
2884 /* Disable USB charging */
2885 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
2887 /* Disable interrupts */
2888 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2889 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2893 /* Backup battery voltage and current disable */
2894 ret = abx500_mask_and_set_register_interruptible(di->dev,
2895 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
2897 dev_err(di->dev, "%s mask and set failed\n", __func__);
2899 usb_unregister_notifier(di->usb_phy, &di->nb);
2900 usb_put_phy(di->usb_phy);
2902 /* Delete the work queue */
2903 destroy_workqueue(di->charger_wq);
2905 flush_scheduled_work();
2906 if(di->usb_chg.enabled)
2907 power_supply_unregister(&di->usb_chg.psy);
2908 if(di->ac_chg.enabled)
2909 power_supply_unregister(&di->ac_chg.psy);
2911 platform_set_drvdata(pdev, NULL);
2916 static char *supply_interface[] = {
2922 static int ab8500_charger_probe(struct platform_device *pdev)
2924 struct device_node *np = pdev->dev.of_node;
2925 struct abx500_bm_data *plat = pdev->dev.platform_data;
2926 struct ab8500_charger *di;
2927 int irq, i, charger_status, ret = 0, ch_stat;
2929 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
2931 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
2936 dev_err(&pdev->dev, "no battery management data supplied\n");
2942 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
2944 dev_err(&pdev->dev, "failed to get battery information\n");
2947 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
2949 di->autopower_cfg = false;
2951 /* get parent data */
2952 di->dev = &pdev->dev;
2953 di->parent = dev_get_drvdata(pdev->dev.parent);
2954 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2956 /* initialize lock */
2957 spin_lock_init(&di->usb_state.usb_lock);
2959 di->autopower = false;
2960 di->invalid_charger_detect_state = 0;
2963 /* power_supply base class */
2964 di->ac_chg.psy.name = "ab8500_ac";
2965 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
2966 di->ac_chg.psy.properties = ab8500_charger_ac_props;
2967 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
2968 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
2969 di->ac_chg.psy.supplied_to = supply_interface;
2970 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
2971 /* ux500_charger sub-class */
2972 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
2973 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2974 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2975 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
2976 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2977 di->ac_chg.max_out_curr = ab8500_charger_current_map[
2978 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2979 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
2980 di->ac_chg.enabled = di->pdata->ac_enabled;
2981 di->ac_chg.external = false;
2984 /* power_supply base class */
2985 di->usb_chg.psy.name = "ab8500_usb";
2986 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
2987 di->usb_chg.psy.properties = ab8500_charger_usb_props;
2988 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
2989 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
2990 di->usb_chg.psy.supplied_to = supply_interface;
2991 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
2992 /* ux500_charger sub-class */
2993 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
2994 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2995 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2996 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
2997 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2998 di->usb_chg.max_out_curr = ab8500_charger_current_map[
2999 ARRAY_SIZE(ab8500_charger_current_map) - 1];
3000 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
3001 di->usb_chg.enabled = di->pdata->usb_enabled;
3002 di->usb_chg.external = false;
3004 /* Create a work queue for the charger */
3006 create_singlethread_workqueue("ab8500_charger_wq");
3007 if (di->charger_wq == NULL) {
3008 dev_err(di->dev, "failed to create work queue\n");
3012 mutex_init(&di->charger_attached_mutex);
3014 /* Init work for HW failure check */
3015 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
3016 ab8500_charger_check_hw_failure_work);
3017 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
3018 ab8500_charger_check_usbchargernotok_work);
3020 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3021 ab8500_charger_ac_attached_work);
3022 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3023 ab8500_charger_usb_attached_work);
3026 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3027 * logic. That means we have to continously kick the charger
3028 * watchdog even when no charger is connected. This is only
3029 * valid once the AC charger has been enabled. This is
3030 * a bug that is not handled by the algorithm and the
3031 * watchdog have to be kicked by the charger driver
3032 * when the AC charger is disabled
3034 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
3035 ab8500_charger_kick_watchdog_work);
3037 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
3038 ab8500_charger_check_vbat_work);
3040 INIT_DELAYED_WORK(&di->attach_work,
3041 ab8500_charger_usb_link_attach_work);
3043 /* Init work for charger detection */
3044 INIT_WORK(&di->usb_link_status_work,
3045 ab8500_charger_usb_link_status_work);
3046 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3047 INIT_WORK(&di->detect_usb_type_work,
3048 ab8500_charger_detect_usb_type_work);
3050 INIT_WORK(&di->usb_state_changed_work,
3051 ab8500_charger_usb_state_changed_work);
3053 /* Init work for checking HW status */
3054 INIT_WORK(&di->check_main_thermal_prot_work,
3055 ab8500_charger_check_main_thermal_prot_work);
3056 INIT_WORK(&di->check_usb_thermal_prot_work,
3057 ab8500_charger_check_usb_thermal_prot_work);
3060 * VDD ADC supply needs to be enabled from this driver when there
3061 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3062 * interrupts during charging
3064 di->regu = devm_regulator_get(di->dev, "vddadc");
3065 if (IS_ERR(di->regu)) {
3066 ret = PTR_ERR(di->regu);
3067 dev_err(di->dev, "failed to get vddadc regulator\n");
3068 goto free_charger_wq;
3072 /* Initialize OVV, and other registers */
3073 ret = ab8500_charger_init_hw_registers(di);
3075 dev_err(di->dev, "failed to initialize ABB registers\n");
3076 goto free_charger_wq;
3079 /* Register AC charger class */
3080 if(di->ac_chg.enabled) {
3081 ret = power_supply_register(di->dev, &di->ac_chg.psy);
3083 dev_err(di->dev, "failed to register AC charger\n");
3084 goto free_charger_wq;
3088 /* Register USB charger class */
3089 if(di->usb_chg.enabled) {
3090 ret = power_supply_register(di->dev, &di->usb_chg.psy);
3092 dev_err(di->dev, "failed to register USB charger\n");
3097 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3098 if (IS_ERR_OR_NULL(di->usb_phy)) {
3099 dev_err(di->dev, "failed to get usb transceiver\n");
3103 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3104 ret = usb_register_notifier(di->usb_phy, &di->nb);
3106 dev_err(di->dev, "failed to register usb notifier\n");
3110 /* Identify the connected charger types during startup */
3111 charger_status = ab8500_charger_detect_chargers(di);
3112 if (charger_status & AC_PW_CONN) {
3113 di->ac.charger_connected = 1;
3115 ab8500_power_supply_changed(di, &di->ac_chg.psy);
3116 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
3119 if (charger_status & USB_PW_CONN) {
3120 di->vbus_detected = true;
3121 di->vbus_detected_start = true;
3122 queue_work(di->charger_wq,
3123 &di->detect_usb_type_work);
3126 /* Register interrupts */
3127 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3128 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3129 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3130 IRQF_SHARED | IRQF_NO_SUSPEND,
3131 ab8500_charger_irq[i].name, di);
3134 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3135 , ab8500_charger_irq[i].name, irq, ret);
3138 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3139 ab8500_charger_irq[i].name, irq, ret);
3142 platform_set_drvdata(pdev, di);
3144 mutex_lock(&di->charger_attached_mutex);
3146 ch_stat = ab8500_charger_detect_chargers(di);
3148 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3149 queue_delayed_work(di->charger_wq,
3150 &di->ac_charger_attached_work,
3153 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3154 queue_delayed_work(di->charger_wq,
3155 &di->usb_charger_attached_work,
3159 mutex_unlock(&di->charger_attached_mutex);
3164 usb_unregister_notifier(di->usb_phy, &di->nb);
3166 /* We also have to free all successfully registered irqs */
3167 for (i = i - 1; i >= 0; i--) {
3168 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3172 usb_put_phy(di->usb_phy);
3174 if(di->usb_chg.enabled)
3175 power_supply_unregister(&di->usb_chg.psy);
3177 if(di->ac_chg.enabled)
3178 power_supply_unregister(&di->ac_chg.psy);
3180 destroy_workqueue(di->charger_wq);
3184 static const struct of_device_id ab8500_charger_match[] = {
3185 { .compatible = "stericsson,ab8500-charger", },
3189 static struct platform_driver ab8500_charger_driver = {
3190 .probe = ab8500_charger_probe,
3191 .remove = ab8500_charger_remove,
3192 .suspend = ab8500_charger_suspend,
3193 .resume = ab8500_charger_resume,
3195 .name = "ab8500-charger",
3196 .owner = THIS_MODULE,
3197 .of_match_table = ab8500_charger_match,
3201 static int __init ab8500_charger_init(void)
3203 return platform_driver_register(&ab8500_charger_driver);
3206 static void __exit ab8500_charger_exit(void)
3208 platform_driver_unregister(&ab8500_charger_driver);
3211 subsys_initcall_sync(ab8500_charger_init);
3212 module_exit(ab8500_charger_exit);
3214 MODULE_LICENSE("GPL v2");
3215 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3216 MODULE_ALIAS("platform:ab8500-charger");
3217 MODULE_DESCRIPTION("AB8500 charger management driver");