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 /* UsbLineStatus register - usb types */
97 enum ab8500_charger_link_status {
98 USB_STAT_NOT_CONFIGURED,
100 USB_STAT_STD_HOST_C_NS,
101 USB_STAT_STD_HOST_C_S,
102 USB_STAT_HOST_CHG_NM,
103 USB_STAT_HOST_CHG_HS,
104 USB_STAT_HOST_CHG_HS_CHIRP,
105 USB_STAT_DEDICATED_CHG,
108 USB_STAT_ACA_RID_C_NM,
109 USB_STAT_ACA_RID_C_HS,
110 USB_STAT_ACA_RID_C_HS_CHIRP,
113 USB_STAT_NOT_VALID_LINK,
115 USB_STAT_SUP_NO_IDGND_VBUS,
116 USB_STAT_SUP_IDGND_VBUS,
117 USB_STAT_CHARGER_LINE_1,
120 USB_STAT_ACA_DOCK_CHARGER,
123 enum ab8500_usb_state {
124 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
125 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
126 AB8500_BM_USB_STATE_CONFIGURED,
127 AB8500_BM_USB_STATE_SUSPEND,
128 AB8500_BM_USB_STATE_RESUME,
129 AB8500_BM_USB_STATE_MAX,
132 /* VBUS input current limits supported in AB8500 in mA */
133 #define USB_CH_IP_CUR_LVL_0P05 50
134 #define USB_CH_IP_CUR_LVL_0P09 98
135 #define USB_CH_IP_CUR_LVL_0P19 193
136 #define USB_CH_IP_CUR_LVL_0P29 290
137 #define USB_CH_IP_CUR_LVL_0P38 380
138 #define USB_CH_IP_CUR_LVL_0P45 450
139 #define USB_CH_IP_CUR_LVL_0P5 500
140 #define USB_CH_IP_CUR_LVL_0P6 600
141 #define USB_CH_IP_CUR_LVL_0P7 700
142 #define USB_CH_IP_CUR_LVL_0P8 800
143 #define USB_CH_IP_CUR_LVL_0P9 900
144 #define USB_CH_IP_CUR_LVL_1P0 1000
145 #define USB_CH_IP_CUR_LVL_1P1 1100
146 #define USB_CH_IP_CUR_LVL_1P3 1300
147 #define USB_CH_IP_CUR_LVL_1P4 1400
148 #define USB_CH_IP_CUR_LVL_1P5 1500
150 #define VBAT_TRESH_IP_CUR_RED 3800
152 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
153 struct ab8500_charger, usb_chg)
154 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
155 struct ab8500_charger, ac_chg)
158 * struct ab8500_charger_interrupts - ab8500 interupts
159 * @name: name of the interrupt
160 * @isr function pointer to the isr
162 struct ab8500_charger_interrupts {
164 irqreturn_t (*isr)(int irq, void *data);
167 struct ab8500_charger_info {
168 int charger_connected;
176 struct ab8500_charger_event_flags {
178 bool main_thermal_prot;
179 bool usb_thermal_prot;
181 bool usbchargernotok;
186 struct ab8500_charger_usb_state {
189 enum ab8500_usb_state state;
194 * struct ab8500_charger - ab8500 Charger device information
195 * @dev: Pointer to the structure device
196 * @max_usb_in_curr: Max USB charger input current
197 * @vbus_detected: VBUS detected
198 * @vbus_detected_start:
199 * VBUS detected during startup
200 * @ac_conn: This will be true when the AC charger has been plugged
201 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
203 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
205 * @vbat Battery voltage
206 * @old_vbat Previously measured battery voltage
207 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
208 * @autopower Indicate if we should have automatic pwron after pwrloss
209 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
210 * @invalid_charger_detect_state State when forcing AB to use invalid charger
211 * @parent: Pointer to the struct ab8500
212 * @gpadc: Pointer to the struct gpadc
213 * @bm: Platform specific battery management information
214 * @flags: Structure for information about events triggered
215 * @usb_state: Structure for usb stack information
216 * @ac_chg: AC charger power supply
217 * @usb_chg: USB charger power supply
218 * @ac: Structure that holds the AC charger properties
219 * @usb: Structure that holds the USB charger properties
220 * @regu: Pointer to the struct regulator
221 * @charger_wq: Work queue for the IRQs and checking HW state
222 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
223 * @check_hw_failure_work: Work for checking HW state
224 * @check_usbchgnotok_work: Work for checking USB charger not ok status
225 * @kick_wd_work: Work for kicking the charger watchdog in case
226 * of ABB rev 1.* due to the watchog logic bug
227 * @attach_work: Work for checking the usb enumeration
228 * @ac_charger_attached_work: Work for checking if AC charger is still
230 * @usb_charger_attached_work: Work for checking if USB charger is still
232 * @ac_work: Work for checking AC charger connection
233 * @detect_usb_type_work: Work for detecting the USB type connected
234 * @usb_link_status_work: Work for checking the new USB link status
235 * @usb_state_changed_work: Work for checking USB state
236 * @check_main_thermal_prot_work:
237 * Work for checking Main thermal status
238 * @check_usb_thermal_prot_work:
239 * Work for checking USB thermal status
240 * @charger_attached_mutex: For controlling the wakelock
242 struct ab8500_charger {
246 bool vbus_detected_start;
252 bool usb_device_is_unrecognised;
255 int invalid_charger_detect_state;
256 struct ab8500 *parent;
257 struct ab8500_gpadc *gpadc;
258 struct abx500_bm_data *bm;
259 struct ab8500_charger_event_flags flags;
260 struct ab8500_charger_usb_state usb_state;
261 struct ux500_charger ac_chg;
262 struct ux500_charger usb_chg;
263 struct ab8500_charger_info ac;
264 struct ab8500_charger_info usb;
265 struct regulator *regu;
266 struct workqueue_struct *charger_wq;
267 struct delayed_work check_vbat_work;
268 struct delayed_work check_hw_failure_work;
269 struct delayed_work check_usbchgnotok_work;
270 struct delayed_work kick_wd_work;
271 struct delayed_work attach_work;
272 struct delayed_work ac_charger_attached_work;
273 struct delayed_work usb_charger_attached_work;
274 struct work_struct ac_work;
275 struct work_struct detect_usb_type_work;
276 struct work_struct usb_link_status_work;
277 struct work_struct usb_state_changed_work;
278 struct work_struct check_main_thermal_prot_work;
279 struct work_struct check_usb_thermal_prot_work;
280 struct usb_phy *usb_phy;
281 struct notifier_block nb;
282 struct mutex charger_attached_mutex;
286 static enum power_supply_property ab8500_charger_ac_props[] = {
287 POWER_SUPPLY_PROP_HEALTH,
288 POWER_SUPPLY_PROP_PRESENT,
289 POWER_SUPPLY_PROP_ONLINE,
290 POWER_SUPPLY_PROP_VOLTAGE_NOW,
291 POWER_SUPPLY_PROP_VOLTAGE_AVG,
292 POWER_SUPPLY_PROP_CURRENT_NOW,
296 static enum power_supply_property ab8500_charger_usb_props[] = {
297 POWER_SUPPLY_PROP_HEALTH,
298 POWER_SUPPLY_PROP_CURRENT_AVG,
299 POWER_SUPPLY_PROP_PRESENT,
300 POWER_SUPPLY_PROP_ONLINE,
301 POWER_SUPPLY_PROP_VOLTAGE_NOW,
302 POWER_SUPPLY_PROP_VOLTAGE_AVG,
303 POWER_SUPPLY_PROP_CURRENT_NOW,
307 * Function for enabling and disabling sw fallback mode
308 * should always be disabled when no charger is connected.
310 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
316 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
318 /* read the register containing fallback bit */
319 ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, ®);
321 dev_err(di->dev, "%d write failed\n", __LINE__);
325 /* enable the OPT emulation registers */
326 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
328 dev_err(di->dev, "%d write failed\n", __LINE__);
337 /* write back the changed fallback bit value to register */
338 ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg);
340 dev_err(di->dev, "%d write failed\n", __LINE__);
344 /* disable the set OTP registers again */
345 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
347 dev_err(di->dev, "%d write failed\n", __LINE__);
353 * ab8500_power_supply_changed - a wrapper with local extentions for
354 * power_supply_changed
355 * @di: pointer to the ab8500_charger structure
356 * @psy: pointer to power_supply_that have changed.
359 static void ab8500_power_supply_changed(struct ab8500_charger *di,
360 struct power_supply *psy)
362 if (di->autopower_cfg) {
363 if (!di->usb.charger_connected &&
364 !di->ac.charger_connected &&
366 di->autopower = false;
367 ab8500_enable_disable_sw_fallback(di, false);
368 } else if (!di->autopower &&
369 (di->ac.charger_connected ||
370 di->usb.charger_connected)) {
371 di->autopower = true;
372 ab8500_enable_disable_sw_fallback(di, true);
375 power_supply_changed(psy);
378 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
381 if (connected != di->usb.charger_connected) {
382 dev_dbg(di->dev, "USB connected:%i\n", connected);
383 di->usb.charger_connected = connected;
384 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
387 mutex_lock(&di->charger_attached_mutex);
388 mutex_unlock(&di->charger_attached_mutex);
390 queue_delayed_work(di->charger_wq,
391 &di->usb_charger_attached_work,
394 cancel_delayed_work_sync(&di->usb_charger_attached_work);
395 mutex_lock(&di->charger_attached_mutex);
396 mutex_unlock(&di->charger_attached_mutex);
402 * ab8500_charger_get_ac_voltage() - get ac charger voltage
403 * @di: pointer to the ab8500_charger structure
405 * Returns ac charger voltage (on success)
407 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
411 /* Only measure voltage if the charger is connected */
412 if (di->ac.charger_connected) {
413 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
415 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
423 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
424 * @di: pointer to the ab8500_charger structure
426 * Returns ac charger CV mode (on success) else error code
428 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
433 /* Only check CV mode if the charger is online */
434 if (di->ac.charger_online) {
435 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
436 AB8500_CH_STATUS1_REG, &val);
438 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
442 if (val & MAIN_CH_CV_ON)
452 * ab8500_charger_get_vbus_voltage() - get vbus voltage
453 * @di: pointer to the ab8500_charger structure
455 * This function returns the vbus voltage.
456 * Returns vbus voltage (on success)
458 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
462 /* Only measure voltage if the charger is connected */
463 if (di->usb.charger_connected) {
464 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
466 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
474 * ab8500_charger_get_usb_current() - get usb charger current
475 * @di: pointer to the ab8500_charger structure
477 * This function returns the usb charger current.
478 * Returns usb current (on success) and error code on failure
480 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
484 /* Only measure current if the charger is online */
485 if (di->usb.charger_online) {
486 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
488 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
496 * ab8500_charger_get_ac_current() - get ac charger current
497 * @di: pointer to the ab8500_charger structure
499 * This function returns the ac charger current.
500 * Returns ac current (on success) and error code on failure.
502 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
506 /* Only measure current if the charger is online */
507 if (di->ac.charger_online) {
508 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
510 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
518 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
519 * @di: pointer to the ab8500_charger structure
521 * Returns ac charger CV mode (on success) else error code
523 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
528 /* Only check CV mode if the charger is online */
529 if (di->usb.charger_online) {
530 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
531 AB8500_CH_USBCH_STAT1_REG, &val);
533 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
537 if (val & USB_CH_CV_ON)
549 * ab8500_charger_detect_chargers() - Detect the connected chargers
550 * @di: pointer to the ab8500_charger structure
552 * Returns the type of charger connected.
553 * For USB it will not mean we can actually charge from it
554 * but that there is a USB cable connected that we have to
555 * identify. This is used during startup when we don't get
556 * interrupts of the charger detection
558 * Returns an integer value, that means,
559 * NO_PW_CONN no power supply is connected
560 * AC_PW_CONN if the AC power supply is connected
561 * USB_PW_CONN if the USB power supply is connected
562 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
564 static int ab8500_charger_detect_chargers(struct ab8500_charger *di)
566 int result = NO_PW_CONN;
570 /* Check for AC charger */
571 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
572 AB8500_CH_STATUS1_REG, &val);
574 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
578 if (val & MAIN_CH_DET)
581 /* Check for USB charger */
582 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
583 AB8500_CH_USBCH_STAT1_REG, &val);
585 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
589 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
590 result |= USB_PW_CONN;
596 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
597 * @di: pointer to the ab8500_charger structure
598 * @link_status: the identified USB type
600 * Get the maximum current that is allowed to be drawn from the host
601 * based on the USB type.
602 * Returns error code in case of failure else 0 on success
604 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
605 enum ab8500_charger_link_status link_status)
609 di->usb_device_is_unrecognised = false;
611 switch (link_status) {
612 case USB_STAT_STD_HOST_NC:
613 case USB_STAT_STD_HOST_C_NS:
614 case USB_STAT_STD_HOST_C_S:
615 dev_dbg(di->dev, "USB Type - Standard host is "
616 "detected through USB driver\n");
617 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
619 case USB_STAT_HOST_CHG_HS_CHIRP:
620 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
621 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
622 di->max_usb_in_curr);
624 case USB_STAT_HOST_CHG_HS:
625 case USB_STAT_ACA_RID_C_HS:
626 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
627 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
628 di->max_usb_in_curr);
630 case USB_STAT_ACA_RID_A:
632 * Dedicated charger level minus maximum current accessory
633 * can consume (300mA). Closest level is 1100mA
635 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1;
636 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
637 di->max_usb_in_curr);
639 case USB_STAT_ACA_RID_B:
641 * Dedicated charger level minus 120mA (20mA for ACA and
642 * 100mA for potential accessory). Closest level is 1300mA
644 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
645 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
646 di->max_usb_in_curr);
648 case USB_STAT_HOST_CHG_NM:
649 case USB_STAT_DEDICATED_CHG:
650 case USB_STAT_ACA_RID_C_NM:
651 case USB_STAT_ACA_RID_C_HS_CHIRP:
652 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
653 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
654 di->max_usb_in_curr);
656 case USB_STAT_NOT_CONFIGURED:
657 if (di->vbus_detected) {
658 di->usb_device_is_unrecognised = true;
659 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
660 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
663 case USB_STAT_HM_IDGND:
664 dev_err(di->dev, "USB Type - Charging not allowed\n");
665 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
668 case USB_STAT_RESERVED:
669 if (is_ab8500(di->parent)) {
670 di->flags.vbus_collapse = true;
671 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
672 "VBUS has collapsed\n");
676 if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
677 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
678 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
679 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
680 link_status, di->max_usb_in_curr);
685 case USB_STAT_CARKIT_1:
686 case USB_STAT_CARKIT_2:
687 case USB_STAT_ACA_DOCK_CHARGER:
688 case USB_STAT_CHARGER_LINE_1:
689 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
690 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
691 di->max_usb_in_curr);
692 case USB_STAT_NOT_VALID_LINK:
693 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
694 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
698 dev_err(di->dev, "USB Type - Unknown\n");
699 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
704 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
705 link_status, di->max_usb_in_curr);
711 * ab8500_charger_read_usb_type() - read the type of usb connected
712 * @di: pointer to the ab8500_charger structure
714 * Detect the type of the plugged USB
715 * Returns error code in case of failure else 0 on success
717 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
722 ret = abx500_get_register_interruptible(di->dev,
723 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
725 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
728 if (is_ab8500(di->parent)) {
729 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
730 AB8500_USB_LINE_STAT_REG, &val);
732 if (is_ab9540(di->parent) || is_ab8505(di->parent))
733 ret = abx500_get_register_interruptible(di->dev,
734 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
737 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
741 /* get the USB type */
742 val = (val & AB8500_USB_LINK_STATUS) >> 3;
743 ret = ab8500_charger_max_usb_curr(di,
744 (enum ab8500_charger_link_status) val);
750 * ab8500_charger_detect_usb_type() - get the type of usb connected
751 * @di: pointer to the ab8500_charger structure
753 * Detect the type of the plugged USB
754 * Returns error code in case of failure else 0 on success
756 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
762 * On getting the VBUS rising edge detect interrupt there
763 * is a 250ms delay after which the register UsbLineStatus
764 * is filled with valid data.
766 for (i = 0; i < 10; i++) {
768 ret = abx500_get_register_interruptible(di->dev,
769 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
772 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
776 if (is_ab8500(di->parent))
777 ret = abx500_get_register_interruptible(di->dev,
778 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
780 ret = abx500_get_register_interruptible(di->dev,
781 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
783 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
787 * Until the IT source register is read the UsbLineStatus
788 * register is not updated, hence doing the same
792 /* get the USB type */
793 val = (val & AB8500_USB_LINK_STATUS) >> 3;
797 ret = ab8500_charger_max_usb_curr(di,
798 (enum ab8500_charger_link_status) val);
804 * This array maps the raw hex value to charger voltage used by the AB8500
805 * Values taken from the UM0836
807 static int ab8500_charger_voltage_map[] = {
889 * This array maps the raw hex value to charger current used by the AB8500
890 * Values taken from the UM0836
892 static int ab8500_charger_current_map[] = {
911 * This array maps the raw hex value to VBUS input current used by the AB8500
912 * Values taken from the UM0836
914 static int ab8500_charger_vbus_in_curr_map[] = {
915 USB_CH_IP_CUR_LVL_0P05,
916 USB_CH_IP_CUR_LVL_0P09,
917 USB_CH_IP_CUR_LVL_0P19,
918 USB_CH_IP_CUR_LVL_0P29,
919 USB_CH_IP_CUR_LVL_0P38,
920 USB_CH_IP_CUR_LVL_0P45,
921 USB_CH_IP_CUR_LVL_0P5,
922 USB_CH_IP_CUR_LVL_0P6,
923 USB_CH_IP_CUR_LVL_0P7,
924 USB_CH_IP_CUR_LVL_0P8,
925 USB_CH_IP_CUR_LVL_0P9,
926 USB_CH_IP_CUR_LVL_1P0,
927 USB_CH_IP_CUR_LVL_1P1,
928 USB_CH_IP_CUR_LVL_1P3,
929 USB_CH_IP_CUR_LVL_1P4,
930 USB_CH_IP_CUR_LVL_1P5,
933 static int ab8500_voltage_to_regval(int voltage)
937 /* Special case for voltage below 3.5V */
938 if (voltage < ab8500_charger_voltage_map[0])
941 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
942 if (voltage < ab8500_charger_voltage_map[i])
946 /* If not last element, return error */
947 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
948 if (voltage == ab8500_charger_voltage_map[i])
954 static int ab8500_current_to_regval(int curr)
958 if (curr < ab8500_charger_current_map[0])
961 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
962 if (curr < ab8500_charger_current_map[i])
966 /* If not last element, return error */
967 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
968 if (curr == ab8500_charger_current_map[i])
974 static int ab8500_vbus_in_curr_to_regval(int curr)
978 if (curr < ab8500_charger_vbus_in_curr_map[0])
981 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
982 if (curr < ab8500_charger_vbus_in_curr_map[i])
986 /* If not last element, return error */
987 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
988 if (curr == ab8500_charger_vbus_in_curr_map[i])
995 * ab8500_charger_get_usb_cur() - get usb current
996 * @di: pointer to the ab8500_charger structre
998 * The usb stack provides the maximum current that can be drawn from
999 * the standard usb host. This will be in mA.
1000 * This function converts current in mA to a value that can be written
1001 * to the register. Returns -1 if charging is not allowed
1003 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1005 switch (di->usb_state.usb_current) {
1007 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
1010 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
1013 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
1016 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
1019 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
1022 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
1030 * ab8500_charger_set_current() - set charger current
1031 * @di: pointer to the ab8500_charger structure
1032 * @ich: charger current, in mA
1033 * @reg: select what charger register to set
1035 * Set charger current.
1036 * There is no state machine in the AB to step up/down the charger
1037 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1038 * charging is started. Instead we need to implement
1039 * this charger current step-up/down here.
1040 * Returns error code in case of failure else 0(on success)
1042 static int ab8500_charger_set_current(struct ab8500_charger *di,
1046 int curr_index, prev_curr_index, shift_value;
1050 case AB8500_MCH_IPT_CURLVL_REG:
1051 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1052 curr_index = ab8500_current_to_regval(ich);
1054 case AB8500_USBCH_IPT_CRNTLVL_REG:
1055 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1056 curr_index = ab8500_vbus_in_curr_to_regval(ich);
1058 case AB8500_CH_OPT_CRNTLVL_REG:
1060 curr_index = ab8500_current_to_regval(ich);
1063 dev_err(di->dev, "%s current register not valid\n", __func__);
1067 if (curr_index < 0) {
1068 dev_err(di->dev, "requested current limit out-of-range\n");
1072 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1075 dev_err(di->dev, "%s read failed\n", __func__);
1078 prev_curr_index = (reg_value >> shift_value);
1080 /* only update current if it's been changed */
1081 if (prev_curr_index == curr_index)
1084 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1085 __func__, ich, reg);
1087 if (prev_curr_index > curr_index) {
1088 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1089 ret = abx500_set_register_interruptible(di->dev,
1090 AB8500_CHARGER, reg, (u8) i << shift_value);
1092 dev_err(di->dev, "%s write failed\n", __func__);
1095 usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
1098 for (i = prev_curr_index + 1; i <= curr_index; i++) {
1099 ret = abx500_set_register_interruptible(di->dev,
1100 AB8500_CHARGER, reg, (u8) i << shift_value);
1102 dev_err(di->dev, "%s write failed\n", __func__);
1105 usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
1112 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1113 * @di: pointer to the ab8500_charger structure
1114 * @ich_in: charger input current limit
1116 * Sets the current that can be drawn from the USB host
1117 * Returns error code in case of failure else 0(on success)
1119 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1124 /* We should always use to lowest current limit */
1125 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1127 switch (min_value) {
1129 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1130 min_value = USB_CH_IP_CUR_LVL_0P05;
1133 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1134 min_value = USB_CH_IP_CUR_LVL_0P45;
1140 return ab8500_charger_set_current(di, min_value,
1141 AB8500_USBCH_IPT_CRNTLVL_REG);
1145 * ab8500_charger_set_main_in_curr() - set main charger input current
1146 * @di: pointer to the ab8500_charger structure
1147 * @ich_in: input charger current, in mA
1149 * Set main charger input current.
1150 * Returns error code in case of failure else 0(on success)
1152 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1155 return ab8500_charger_set_current(di, ich_in,
1156 AB8500_MCH_IPT_CURLVL_REG);
1160 * ab8500_charger_set_output_curr() - set charger output current
1161 * @di: pointer to the ab8500_charger structure
1162 * @ich_out: output charger current, in mA
1164 * Set charger output current.
1165 * Returns error code in case of failure else 0(on success)
1167 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1170 return ab8500_charger_set_current(di, ich_out,
1171 AB8500_CH_OPT_CRNTLVL_REG);
1175 * ab8500_charger_led_en() - turn on/off chargign led
1176 * @di: pointer to the ab8500_charger structure
1177 * @on: flag to turn on/off the chargign led
1179 * Power ON/OFF charging LED indication
1180 * Returns error code in case of failure else 0(on success)
1182 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1187 /* Power ON charging LED indicator, set LED current to 5mA */
1188 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1189 AB8500_LED_INDICATOR_PWM_CTRL,
1190 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1192 dev_err(di->dev, "Power ON LED failed\n");
1195 /* LED indicator PWM duty cycle 252/256 */
1196 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1197 AB8500_LED_INDICATOR_PWM_DUTY,
1198 LED_INDICATOR_PWM_DUTY_252_256);
1200 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1204 /* Power off charging LED indicator */
1205 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1206 AB8500_LED_INDICATOR_PWM_CTRL,
1207 LED_INDICATOR_PWM_DIS);
1209 dev_err(di->dev, "Power-off LED failed\n");
1218 * ab8500_charger_ac_en() - enable or disable ac charging
1219 * @di: pointer to the ab8500_charger structure
1220 * @enable: enable/disable flag
1221 * @vset: charging voltage
1222 * @iset: charging current
1224 * Enable/Disable AC/Mains charging and turns on/off the charging led
1227 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1228 int enable, int vset, int iset)
1233 int input_curr_index;
1236 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1239 /* Check if AC is connected */
1240 if (!di->ac.charger_connected) {
1241 dev_err(di->dev, "AC charger not connected\n");
1245 /* Enable AC charging */
1246 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1249 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1250 * will be triggered everytime we enable the VDD ADC supply.
1251 * This will turn off charging for a short while.
1252 * It can be avoided by having the supply on when
1253 * there is a charger enabled. Normally the VDD ADC supply
1254 * is enabled everytime a GPADC conversion is triggered. We will
1255 * force it to be enabled from this driver to have
1256 * the GPADC module independant of the AB8500 chargers
1258 if (!di->vddadc_en_ac) {
1259 regulator_enable(di->regu);
1260 di->vddadc_en_ac = true;
1263 /* Check if the requested voltage or current is valid */
1264 volt_index = ab8500_voltage_to_regval(vset);
1265 curr_index = ab8500_current_to_regval(iset);
1266 input_curr_index = ab8500_current_to_regval(
1267 di->bm->chg_params->ac_curr_max);
1268 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1270 "Charger voltage or current too high, "
1271 "charging not started\n");
1275 /* ChVoltLevel: maximum battery charging voltage */
1276 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1277 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1279 dev_err(di->dev, "%s write failed\n", __func__);
1282 /* MainChInputCurr: current that can be drawn from the charger*/
1283 ret = ab8500_charger_set_main_in_curr(di,
1284 di->bm->chg_params->ac_curr_max);
1286 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1290 /* ChOutputCurentLevel: protected output current */
1291 ret = ab8500_charger_set_output_curr(di, iset);
1293 dev_err(di->dev, "%s "
1294 "Failed to set ChOutputCurentLevel\n",
1299 /* Check if VBAT overshoot control should be enabled */
1300 if (!di->bm->enable_overshoot)
1301 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1303 /* Enable Main Charger */
1304 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1305 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1307 dev_err(di->dev, "%s write failed\n", __func__);
1311 /* Power on charging LED indication */
1312 ret = ab8500_charger_led_en(di, true);
1314 dev_err(di->dev, "failed to enable LED\n");
1316 di->ac.charger_online = 1;
1318 /* Disable AC charging */
1319 if (is_ab8500_1p1_or_earlier(di->parent)) {
1321 * For ABB revision 1.0 and 1.1 there is a bug in the
1322 * watchdog logic. That means we have to continously
1323 * kick the charger watchdog even when no charger is
1324 * connected. This is only valid once the AC charger
1325 * has been enabled. This is a bug that is not handled
1326 * by the algorithm and the watchdog have to be kicked
1327 * by the charger driver when the AC charger
1331 queue_delayed_work(di->charger_wq,
1333 round_jiffies(WD_KICK_INTERVAL));
1337 * We can't turn off charging completely
1338 * due to a bug in AB8500 cut1.
1339 * If we do, charging will not start again.
1340 * That is why we set the lowest voltage
1341 * and current possible
1343 ret = abx500_set_register_interruptible(di->dev,
1345 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1348 "%s write failed\n", __func__);
1352 ret = ab8500_charger_set_output_curr(di, 0);
1354 dev_err(di->dev, "%s "
1355 "Failed to set ChOutputCurentLevel\n",
1360 ret = abx500_set_register_interruptible(di->dev,
1362 AB8500_MCH_CTRL1, 0);
1365 "%s write failed\n", __func__);
1370 ret = ab8500_charger_led_en(di, false);
1372 dev_err(di->dev, "failed to disable LED\n");
1374 di->ac.charger_online = 0;
1375 di->ac.wd_expired = false;
1377 /* Disable regulator if enabled */
1378 if (di->vddadc_en_ac) {
1379 regulator_disable(di->regu);
1380 di->vddadc_en_ac = false;
1383 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1385 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1391 * ab8500_charger_usb_en() - enable usb charging
1392 * @di: pointer to the ab8500_charger structure
1393 * @enable: enable/disable flag
1394 * @vset: charging voltage
1395 * @ich_out: charger output current
1397 * Enable/Disable USB charging and turns on/off the charging led respectively.
1398 * Returns error code in case of failure else 0(on success)
1400 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1401 int enable, int vset, int ich_out)
1408 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1411 /* Check if USB is connected */
1412 if (!di->usb.charger_connected) {
1413 dev_err(di->dev, "USB charger not connected\n");
1418 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1419 * will be triggered everytime we enable the VDD ADC supply.
1420 * This will turn off charging for a short while.
1421 * It can be avoided by having the supply on when
1422 * there is a charger enabled. Normally the VDD ADC supply
1423 * is enabled everytime a GPADC conversion is triggered. We will
1424 * force it to be enabled from this driver to have
1425 * the GPADC module independant of the AB8500 chargers
1427 if (!di->vddadc_en_usb) {
1428 regulator_enable(di->regu);
1429 di->vddadc_en_usb = true;
1432 /* Enable USB charging */
1433 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1435 /* Check if the requested voltage or current is valid */
1436 volt_index = ab8500_voltage_to_regval(vset);
1437 curr_index = ab8500_current_to_regval(ich_out);
1438 if (volt_index < 0 || curr_index < 0) {
1440 "Charger voltage or current too high, "
1441 "charging not started\n");
1445 /* ChVoltLevel: max voltage upto which battery can be charged */
1446 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1447 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1449 dev_err(di->dev, "%s write failed\n", __func__);
1452 /* USBChInputCurr: current that can be drawn from the usb */
1453 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1455 dev_err(di->dev, "setting USBChInputCurr failed\n");
1458 /* ChOutputCurentLevel: protected output current */
1459 ret = ab8500_charger_set_output_curr(di, ich_out);
1461 dev_err(di->dev, "%s "
1462 "Failed to set ChOutputCurentLevel\n",
1466 /* Check if VBAT overshoot control should be enabled */
1467 if (!di->bm->enable_overshoot)
1468 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1470 /* Enable USB Charger */
1471 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1472 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1474 dev_err(di->dev, "%s write failed\n", __func__);
1478 /* If success power on charging LED indication */
1479 ret = ab8500_charger_led_en(di, true);
1481 dev_err(di->dev, "failed to enable LED\n");
1483 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1485 di->usb.charger_online = 1;
1487 /* Disable USB charging */
1488 ret = abx500_set_register_interruptible(di->dev,
1490 AB8500_USBCH_CTRL1_REG, 0);
1493 "%s write failed\n", __func__);
1497 ret = ab8500_charger_led_en(di, false);
1499 dev_err(di->dev, "failed to disable LED\n");
1501 di->usb.charger_online = 0;
1502 di->usb.wd_expired = false;
1504 /* Disable regulator if enabled */
1505 if (di->vddadc_en_usb) {
1506 regulator_disable(di->regu);
1507 di->vddadc_en_usb = false;
1510 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1512 /* Cancel any pending Vbat check work */
1513 if (delayed_work_pending(&di->check_vbat_work))
1514 cancel_delayed_work(&di->check_vbat_work);
1517 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1523 * ab8500_charger_watchdog_kick() - kick charger watchdog
1524 * @di: pointer to the ab8500_charger structure
1526 * Kick charger watchdog
1527 * Returns error code in case of failure else 0(on success)
1529 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1532 struct ab8500_charger *di;
1534 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1535 di = to_ab8500_charger_ac_device_info(charger);
1536 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1537 di = to_ab8500_charger_usb_device_info(charger);
1541 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1542 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1544 dev_err(di->dev, "Failed to kick WD!\n");
1550 * ab8500_charger_update_charger_current() - update charger current
1551 * @di: pointer to the ab8500_charger structure
1553 * Update the charger output current for the specified charger
1554 * Returns error code in case of failure else 0(on success)
1556 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1560 struct ab8500_charger *di;
1562 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1563 di = to_ab8500_charger_ac_device_info(charger);
1564 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1565 di = to_ab8500_charger_usb_device_info(charger);
1569 ret = ab8500_charger_set_output_curr(di, ich_out);
1571 dev_err(di->dev, "%s "
1572 "Failed to set ChOutputCurentLevel\n",
1577 /* Reset the main and usb drop input current measurement counter */
1578 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1579 AB8500_CHARGER_CTRL,
1582 dev_err(di->dev, "%s write failed\n", __func__);
1589 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1591 struct power_supply *psy;
1592 struct power_supply *ext;
1593 struct ab8500_charger *di;
1594 union power_supply_propval ret;
1596 bool psy_found = false;
1597 struct ux500_charger *usb_chg;
1599 usb_chg = (struct ux500_charger *)data;
1600 psy = &usb_chg->psy;
1602 di = to_ab8500_charger_usb_device_info(usb_chg);
1604 ext = dev_get_drvdata(dev);
1606 /* For all psy where the driver name appears in any supplied_to */
1607 for (i = 0; i < ext->num_supplicants; i++) {
1608 if (!strcmp(ext->supplied_to[i], psy->name))
1615 /* Go through all properties for the psy */
1616 for (j = 0; j < ext->num_properties; j++) {
1617 enum power_supply_property prop;
1618 prop = ext->properties[j];
1620 if (ext->get_property(ext, prop, &ret))
1624 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1625 switch (ext->type) {
1626 case POWER_SUPPLY_TYPE_BATTERY:
1627 di->vbat = ret.intval / 1000;
1641 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1642 * @work pointer to the work_struct structure
1644 * Due to a asic bug it is necessary to lower the input current to the vbus
1645 * charger when charging with at some specific levels. This issue is only valid
1646 * for below a certain battery voltage. This function makes sure that the
1647 * the allowed current limit isn't exceeded.
1649 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1652 struct ab8500_charger *di = container_of(work,
1653 struct ab8500_charger, check_vbat_work.work);
1655 class_for_each_device(power_supply_class, NULL,
1656 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1658 /* First run old_vbat is 0. */
1659 if (di->old_vbat == 0)
1660 di->old_vbat = di->vbat;
1662 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1663 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1664 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1665 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1667 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1668 " old: %d\n", di->max_usb_in_curr, di->vbat,
1670 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1671 power_supply_changed(&di->usb_chg.psy);
1674 di->old_vbat = di->vbat;
1677 * No need to check the battery voltage every second when not close to
1680 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1681 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1684 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1688 * ab8500_charger_check_hw_failure_work() - check main charger failure
1689 * @work: pointer to the work_struct structure
1691 * Work queue function for checking the main charger status
1693 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1698 struct ab8500_charger *di = container_of(work,
1699 struct ab8500_charger, check_hw_failure_work.work);
1701 /* Check if the status bits for HW failure is still active */
1702 if (di->flags.mainextchnotok) {
1703 ret = abx500_get_register_interruptible(di->dev,
1704 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1706 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1709 if (!(reg_value & MAIN_CH_NOK)) {
1710 di->flags.mainextchnotok = false;
1711 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1714 if (di->flags.vbus_ovv) {
1715 ret = abx500_get_register_interruptible(di->dev,
1716 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1719 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1722 if (!(reg_value & VBUS_OVV_TH)) {
1723 di->flags.vbus_ovv = false;
1724 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1727 /* If we still have a failure, schedule a new check */
1728 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1729 queue_delayed_work(di->charger_wq,
1730 &di->check_hw_failure_work, round_jiffies(HZ));
1735 * ab8500_charger_kick_watchdog_work() - kick the watchdog
1736 * @work: pointer to the work_struct structure
1738 * Work queue function for kicking the charger watchdog.
1740 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
1741 * logic. That means we have to continously kick the charger
1742 * watchdog even when no charger is connected. This is only
1743 * valid once the AC charger has been enabled. This is
1744 * a bug that is not handled by the algorithm and the
1745 * watchdog have to be kicked by the charger driver
1746 * when the AC charger is disabled
1748 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
1752 struct ab8500_charger *di = container_of(work,
1753 struct ab8500_charger, kick_wd_work.work);
1755 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1756 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1758 dev_err(di->dev, "Failed to kick WD!\n");
1760 /* Schedule a new watchdog kick */
1761 queue_delayed_work(di->charger_wq,
1762 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
1766 * ab8500_charger_ac_work() - work to get and set main charger status
1767 * @work: pointer to the work_struct structure
1769 * Work queue function for checking the main charger status
1771 static void ab8500_charger_ac_work(struct work_struct *work)
1775 struct ab8500_charger *di = container_of(work,
1776 struct ab8500_charger, ac_work);
1779 * Since we can't be sure that the events are received
1780 * synchronously, we have the check if the main charger is
1781 * connected by reading the status register
1783 ret = ab8500_charger_detect_chargers(di);
1787 if (ret & AC_PW_CONN) {
1788 di->ac.charger_connected = 1;
1791 di->ac.charger_connected = 0;
1794 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1795 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
1798 static void ab8500_charger_usb_attached_work(struct work_struct *work)
1800 struct ab8500_charger *di = container_of(work,
1801 struct ab8500_charger,
1802 usb_charger_attached_work.work);
1803 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
1807 for (i = 0; i < 10; i++) {
1808 ret = abx500_get_register_interruptible(di->dev,
1810 AB8500_CH_USBCH_STAT1_REG,
1813 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1816 if ((statval & usbch) != usbch)
1819 msleep(CHARGER_STATUS_POLL);
1822 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
1824 mutex_lock(&di->charger_attached_mutex);
1825 mutex_unlock(&di->charger_attached_mutex);
1830 queue_delayed_work(di->charger_wq,
1831 &di->usb_charger_attached_work,
1835 static void ab8500_charger_ac_attached_work(struct work_struct *work)
1838 struct ab8500_charger *di = container_of(work,
1839 struct ab8500_charger,
1840 ac_charger_attached_work.work);
1841 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
1842 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
1846 for (i = 0; i < 10; i++) {
1847 ret = abx500_get_register_interruptible(di->dev,
1849 AB8500_CH_STATUS2_REG,
1852 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1856 if ((statval & mainch) != mainch)
1859 msleep(CHARGER_STATUS_POLL);
1862 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
1863 queue_work(di->charger_wq, &di->ac_work);
1865 mutex_lock(&di->charger_attached_mutex);
1866 mutex_unlock(&di->charger_attached_mutex);
1871 queue_delayed_work(di->charger_wq,
1872 &di->ac_charger_attached_work,
1877 * ab8500_charger_detect_usb_type_work() - work to detect USB type
1878 * @work: Pointer to the work_struct structure
1880 * Detect the type of USB plugged
1882 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
1886 struct ab8500_charger *di = container_of(work,
1887 struct ab8500_charger, detect_usb_type_work);
1890 * Since we can't be sure that the events are received
1891 * synchronously, we have the check if is
1892 * connected by reading the status register
1894 ret = ab8500_charger_detect_chargers(di);
1898 if (!(ret & USB_PW_CONN)) {
1899 di->vbus_detected = 0;
1900 ab8500_charger_set_usb_connected(di, false);
1901 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1903 di->vbus_detected = 1;
1905 if (is_ab8500_1p1_or_earlier(di->parent)) {
1906 ret = ab8500_charger_detect_usb_type(di);
1908 ab8500_charger_set_usb_connected(di, true);
1909 ab8500_power_supply_changed(di,
1913 /* For ABB cut2.0 and onwards we have an IRQ,
1914 * USB_LINK_STATUS that will be triggered when the USB
1915 * link status changes. The exception is USB connected
1916 * during startup. Then we don't get a
1917 * USB_LINK_STATUS IRQ
1919 if (di->vbus_detected_start) {
1920 di->vbus_detected_start = false;
1921 ret = ab8500_charger_detect_usb_type(di);
1923 ab8500_charger_set_usb_connected(di,
1925 ab8500_power_supply_changed(di,
1934 * ab8500_charger_usb_link_attach_work() - delayd work to detect USB type
1935 * @work: pointer to the work_struct structure
1937 * Detect the type of USB plugged
1939 static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
1941 struct ab8500_charger *di =
1942 container_of(work, struct ab8500_charger, attach_work.work);
1945 /* Update maximum input current if USB enumeration is not detected */
1946 if (!di->usb.charger_online) {
1947 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1952 ab8500_charger_set_usb_connected(di, true);
1953 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1957 * ab8500_charger_usb_link_status_work() - work to detect USB type
1958 * @work: pointer to the work_struct structure
1960 * Detect the type of USB plugged
1962 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
1964 int detected_chargers;
1968 struct ab8500_charger *di = container_of(work,
1969 struct ab8500_charger, usb_link_status_work);
1972 * Since we can't be sure that the events are received
1973 * synchronously, we have the check if is
1974 * connected by reading the status register
1976 detected_chargers = ab8500_charger_detect_chargers(di);
1977 if (detected_chargers < 0)
1981 * Some chargers that breaks the USB spec is
1982 * identified as invalid by AB8500 and it refuse
1983 * to start the charging process. but by jumping
1984 * thru a few hoops it can be forced to start.
1986 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
1987 AB8500_USB_LINE_STAT_REG, &val);
1989 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
1991 dev_dbg(di->dev, "Error reading USB link status\n");
1993 if (detected_chargers & USB_PW_CONN) {
1994 if (((val & AB8500_USB_LINK_STATUS) >> 3) == USB_STAT_NOT_VALID_LINK &&
1995 di->invalid_charger_detect_state == 0) {
1996 dev_dbg(di->dev, "Invalid charger detected, state= 0\n");
1998 abx500_mask_and_set_register_interruptible(di->dev,
1999 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG, 0x01, 0x01);
2000 /*Enable charger detection*/
2001 abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2002 AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x01);
2003 di->invalid_charger_detect_state = 1;
2004 /*exit and wait for new link status interrupt.*/
2008 if (di->invalid_charger_detect_state == 1) {
2009 dev_dbg(di->dev, "Invalid charger detected, state= 1\n");
2010 /*Stop charger detection*/
2011 abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2012 AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x00);
2013 /*Check link status*/
2014 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2015 AB8500_USB_LINE_STAT_REG, &val);
2016 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2017 (val & AB8500_USB_LINK_STATUS) >> 3);
2018 di->invalid_charger_detect_state = 2;
2021 di->invalid_charger_detect_state = 0;
2024 if (!(detected_chargers & USB_PW_CONN)) {
2025 di->vbus_detected = 0;
2026 ab8500_charger_set_usb_connected(di, false);
2027 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2031 di->vbus_detected = 1;
2032 ret = ab8500_charger_read_usb_type(di);
2034 if (di->usb_device_is_unrecognised) {
2036 "Potential Legacy Charger device. "
2037 "Delay work for %d msec for USB enum "
2039 WAIT_FOR_USB_ENUMERATION);
2040 queue_delayed_work(di->charger_wq,
2042 msecs_to_jiffies(WAIT_FOR_USB_ENUMERATION));
2044 queue_delayed_work(di->charger_wq,
2045 &di->attach_work, 0);
2047 } else if (ret == -ENXIO) {
2048 /* No valid charger type detected */
2049 ab8500_charger_set_usb_connected(di, false);
2050 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2054 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2057 unsigned long flags;
2059 struct ab8500_charger *di = container_of(work,
2060 struct ab8500_charger, usb_state_changed_work);
2062 if (!di->vbus_detected)
2065 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2066 di->usb_state.usb_changed = false;
2067 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2070 * wait for some time until you get updates from the usb stack
2071 * and negotiations are completed
2075 if (di->usb_state.usb_changed)
2078 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2079 __func__, di->usb_state.state, di->usb_state.usb_current);
2081 switch (di->usb_state.state) {
2082 case AB8500_BM_USB_STATE_RESET_HS:
2083 case AB8500_BM_USB_STATE_RESET_FS:
2084 case AB8500_BM_USB_STATE_SUSPEND:
2085 case AB8500_BM_USB_STATE_MAX:
2086 ab8500_charger_set_usb_connected(di, false);
2087 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2090 case AB8500_BM_USB_STATE_RESUME:
2092 * when suspend->resume there should be delay
2093 * of 1sec for enabling charging
2096 /* Intentional fall through */
2097 case AB8500_BM_USB_STATE_CONFIGURED:
2099 * USB is configured, enable charging with the charging
2100 * input current obtained from USB driver
2102 if (!ab8500_charger_get_usb_cur(di)) {
2103 /* Update maximum input current */
2104 ret = ab8500_charger_set_vbus_in_curr(di,
2105 di->max_usb_in_curr);
2109 ab8500_charger_set_usb_connected(di, true);
2110 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2120 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2121 * @work: pointer to the work_struct structure
2123 * Work queue function for checking the USB charger Not OK status
2125 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2131 struct ab8500_charger *di = container_of(work,
2132 struct ab8500_charger, check_usbchgnotok_work.work);
2134 /* Check if the status bit for usbchargernotok is still active */
2135 ret = abx500_get_register_interruptible(di->dev,
2136 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2138 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2141 prev_status = di->flags.usbchargernotok;
2143 if (reg_value & VBUS_CH_NOK) {
2144 di->flags.usbchargernotok = true;
2145 /* Check again in 1sec */
2146 queue_delayed_work(di->charger_wq,
2147 &di->check_usbchgnotok_work, HZ);
2149 di->flags.usbchargernotok = false;
2150 di->flags.vbus_collapse = false;
2153 if (prev_status != di->flags.usbchargernotok)
2154 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2158 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2159 * @work: pointer to the work_struct structure
2161 * Work queue function for checking the Main thermal prot status
2163 static void ab8500_charger_check_main_thermal_prot_work(
2164 struct work_struct *work)
2169 struct ab8500_charger *di = container_of(work,
2170 struct ab8500_charger, check_main_thermal_prot_work);
2172 /* Check if the status bit for main_thermal_prot is still active */
2173 ret = abx500_get_register_interruptible(di->dev,
2174 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2176 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2179 if (reg_value & MAIN_CH_TH_PROT)
2180 di->flags.main_thermal_prot = true;
2182 di->flags.main_thermal_prot = false;
2184 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2188 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2189 * @work: pointer to the work_struct structure
2191 * Work queue function for checking the USB thermal prot status
2193 static void ab8500_charger_check_usb_thermal_prot_work(
2194 struct work_struct *work)
2199 struct ab8500_charger *di = container_of(work,
2200 struct ab8500_charger, check_usb_thermal_prot_work);
2202 /* Check if the status bit for usb_thermal_prot is still active */
2203 ret = abx500_get_register_interruptible(di->dev,
2204 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2206 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2209 if (reg_value & USB_CH_TH_PROT)
2210 di->flags.usb_thermal_prot = true;
2212 di->flags.usb_thermal_prot = false;
2214 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2218 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2219 * @irq: interrupt number
2220 * @_di: pointer to the ab8500_charger structure
2222 * Returns IRQ status(IRQ_HANDLED)
2224 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2226 struct ab8500_charger *di = _di;
2228 dev_dbg(di->dev, "Main charger unplugged\n");
2229 queue_work(di->charger_wq, &di->ac_work);
2231 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2232 mutex_lock(&di->charger_attached_mutex);
2233 mutex_unlock(&di->charger_attached_mutex);
2239 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2240 * @irq: interrupt number
2241 * @_di: pointer to the ab8500_charger structure
2243 * Returns IRQ status(IRQ_HANDLED)
2245 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2247 struct ab8500_charger *di = _di;
2249 dev_dbg(di->dev, "Main charger plugged\n");
2250 queue_work(di->charger_wq, &di->ac_work);
2252 mutex_lock(&di->charger_attached_mutex);
2253 mutex_unlock(&di->charger_attached_mutex);
2254 queue_delayed_work(di->charger_wq,
2255 &di->ac_charger_attached_work,
2261 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2262 * @irq: interrupt number
2263 * @_di: pointer to the ab8500_charger structure
2265 * Returns IRQ status(IRQ_HANDLED)
2267 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2269 struct ab8500_charger *di = _di;
2271 dev_dbg(di->dev, "Main charger not ok\n");
2272 di->flags.mainextchnotok = true;
2273 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2275 /* Schedule a new HW failure check */
2276 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2282 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2283 * thermal protection threshold
2284 * @irq: interrupt number
2285 * @_di: pointer to the ab8500_charger structure
2287 * Returns IRQ status(IRQ_HANDLED)
2289 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2291 struct ab8500_charger *di = _di;
2294 "Die temp above Main charger thermal protection threshold\n");
2295 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2301 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2302 * thermal protection threshold
2303 * @irq: interrupt number
2304 * @_di: pointer to the ab8500_charger structure
2306 * Returns IRQ status(IRQ_HANDLED)
2308 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2310 struct ab8500_charger *di = _di;
2313 "Die temp ok for Main charger thermal protection threshold\n");
2314 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2320 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2321 * @irq: interrupt number
2322 * @_di: pointer to the ab8500_charger structure
2324 * Returns IRQ status(IRQ_HANDLED)
2326 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2328 struct ab8500_charger *di = _di;
2330 dev_dbg(di->dev, "VBUS falling detected\n");
2331 queue_work(di->charger_wq, &di->detect_usb_type_work);
2337 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2338 * @irq: interrupt number
2339 * @_di: pointer to the ab8500_charger structure
2341 * Returns IRQ status(IRQ_HANDLED)
2343 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2345 struct ab8500_charger *di = _di;
2347 di->vbus_detected = true;
2348 dev_dbg(di->dev, "VBUS rising detected\n");
2349 queue_work(di->charger_wq, &di->detect_usb_type_work);
2355 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2356 * @irq: interrupt number
2357 * @_di: pointer to the ab8500_charger structure
2359 * Returns IRQ status(IRQ_HANDLED)
2361 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2363 struct ab8500_charger *di = _di;
2365 dev_dbg(di->dev, "USB link status changed\n");
2367 queue_work(di->charger_wq, &di->usb_link_status_work);
2373 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2374 * thermal protection threshold
2375 * @irq: interrupt number
2376 * @_di: pointer to the ab8500_charger structure
2378 * Returns IRQ status(IRQ_HANDLED)
2380 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2382 struct ab8500_charger *di = _di;
2385 "Die temp above USB charger thermal protection threshold\n");
2386 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2392 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2393 * thermal protection threshold
2394 * @irq: interrupt number
2395 * @_di: pointer to the ab8500_charger structure
2397 * Returns IRQ status(IRQ_HANDLED)
2399 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2401 struct ab8500_charger *di = _di;
2404 "Die temp ok for USB charger thermal protection threshold\n");
2405 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2411 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2412 * @irq: interrupt number
2413 * @_di: pointer to the ab8500_charger structure
2415 * Returns IRQ status(IRQ_HANDLED)
2417 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2419 struct ab8500_charger *di = _di;
2421 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2422 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2428 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2429 * @irq: interrupt number
2430 * @_di: pointer to the ab8500_charger structure
2432 * Returns IRQ status(IRQ_HANDLED)
2434 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2436 struct ab8500_charger *di = _di;
2438 dev_dbg(di->dev, "Charger watchdog expired\n");
2441 * The charger that was online when the watchdog expired
2442 * needs to be restarted for charging to start again
2444 if (di->ac.charger_online) {
2445 di->ac.wd_expired = true;
2446 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2448 if (di->usb.charger_online) {
2449 di->usb.wd_expired = true;
2450 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2457 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2458 * @irq: interrupt number
2459 * @_di: pointer to the ab8500_charger structure
2461 * Returns IRQ status(IRQ_HANDLED)
2463 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2465 struct ab8500_charger *di = _di;
2467 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2468 di->flags.vbus_ovv = true;
2469 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2471 /* Schedule a new HW failure check */
2472 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2478 * ab8500_charger_ac_get_property() - get the ac/mains properties
2479 * @psy: pointer to the power_supply structure
2480 * @psp: pointer to the power_supply_property structure
2481 * @val: pointer to the power_supply_propval union
2483 * This function gets called when an application tries to get the ac/mains
2484 * properties by reading the sysfs files.
2485 * AC/Mains properties are online, present and voltage.
2486 * online: ac/mains charging is in progress or not
2487 * present: presence of the ac/mains
2488 * voltage: AC/Mains voltage
2489 * Returns error code in case of failure else 0(on success)
2491 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2492 enum power_supply_property psp,
2493 union power_supply_propval *val)
2495 struct ab8500_charger *di;
2498 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2501 case POWER_SUPPLY_PROP_HEALTH:
2502 if (di->flags.mainextchnotok)
2503 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2504 else if (di->ac.wd_expired || di->usb.wd_expired)
2505 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2506 else if (di->flags.main_thermal_prot)
2507 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2509 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2511 case POWER_SUPPLY_PROP_ONLINE:
2512 val->intval = di->ac.charger_online;
2514 case POWER_SUPPLY_PROP_PRESENT:
2515 val->intval = di->ac.charger_connected;
2517 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2518 ret = ab8500_charger_get_ac_voltage(di);
2520 di->ac.charger_voltage = ret;
2521 /* On error, use previous value */
2522 val->intval = di->ac.charger_voltage * 1000;
2524 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2526 * This property is used to indicate when CV mode is entered
2527 * for the AC charger
2529 di->ac.cv_active = ab8500_charger_ac_cv(di);
2530 val->intval = di->ac.cv_active;
2532 case POWER_SUPPLY_PROP_CURRENT_NOW:
2533 ret = ab8500_charger_get_ac_current(di);
2535 di->ac.charger_current = ret;
2536 val->intval = di->ac.charger_current * 1000;
2545 * ab8500_charger_usb_get_property() - get the usb properties
2546 * @psy: pointer to the power_supply structure
2547 * @psp: pointer to the power_supply_property structure
2548 * @val: pointer to the power_supply_propval union
2550 * This function gets called when an application tries to get the usb
2551 * properties by reading the sysfs files.
2552 * USB properties are online, present and voltage.
2553 * online: usb charging is in progress or not
2554 * present: presence of the usb
2555 * voltage: vbus voltage
2556 * Returns error code in case of failure else 0(on success)
2558 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2559 enum power_supply_property psp,
2560 union power_supply_propval *val)
2562 struct ab8500_charger *di;
2565 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2568 case POWER_SUPPLY_PROP_HEALTH:
2569 if (di->flags.usbchargernotok)
2570 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2571 else if (di->ac.wd_expired || di->usb.wd_expired)
2572 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2573 else if (di->flags.usb_thermal_prot)
2574 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2575 else if (di->flags.vbus_ovv)
2576 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2578 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2580 case POWER_SUPPLY_PROP_ONLINE:
2581 val->intval = di->usb.charger_online;
2583 case POWER_SUPPLY_PROP_PRESENT:
2584 val->intval = di->usb.charger_connected;
2586 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2587 ret = ab8500_charger_get_vbus_voltage(di);
2589 di->usb.charger_voltage = ret;
2590 val->intval = di->usb.charger_voltage * 1000;
2592 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2594 * This property is used to indicate when CV mode is entered
2595 * for the USB charger
2597 di->usb.cv_active = ab8500_charger_usb_cv(di);
2598 val->intval = di->usb.cv_active;
2600 case POWER_SUPPLY_PROP_CURRENT_NOW:
2601 ret = ab8500_charger_get_usb_current(di);
2603 di->usb.charger_current = ret;
2604 val->intval = di->usb.charger_current * 1000;
2606 case POWER_SUPPLY_PROP_CURRENT_AVG:
2608 * This property is used to indicate when VBUS has collapsed
2609 * due to too high output current from the USB charger
2611 if (di->flags.vbus_collapse)
2623 * ab8500_charger_init_hw_registers() - Set up charger related registers
2624 * @di: pointer to the ab8500_charger structure
2626 * Set up charger OVV, watchdog and maximum voltage registers as well as
2627 * charging of the backup battery
2629 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
2633 /* Setup maximum charger current and voltage for ABB cut2.0 */
2634 if (!is_ab8500_1p1_or_earlier(di->parent)) {
2635 ret = abx500_set_register_interruptible(di->dev,
2637 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
2640 "failed to set CH_VOLT_LVL_MAX_REG\n");
2644 ret = abx500_set_register_interruptible(di->dev,
2646 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
2649 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2654 /* VBUS OVV set to 6.3V and enable automatic current limitiation */
2655 ret = abx500_set_register_interruptible(di->dev,
2657 AB8500_USBCH_CTRL2_REG,
2658 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
2660 dev_err(di->dev, "failed to set VBUS OVV\n");
2664 /* Enable main watchdog in OTP */
2665 ret = abx500_set_register_interruptible(di->dev,
2666 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
2668 dev_err(di->dev, "failed to enable main WD in OTP\n");
2672 /* Enable main watchdog */
2673 ret = abx500_set_register_interruptible(di->dev,
2674 AB8500_SYS_CTRL2_BLOCK,
2675 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
2677 dev_err(di->dev, "faile to enable main watchdog\n");
2682 * Due to internal synchronisation, Enable and Kick watchdog bits
2683 * cannot be enabled in a single write.
2684 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
2685 * between writing Enable then Kick bits.
2689 /* Kick main watchdog */
2690 ret = abx500_set_register_interruptible(di->dev,
2691 AB8500_SYS_CTRL2_BLOCK,
2692 AB8500_MAIN_WDOG_CTRL_REG,
2693 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
2695 dev_err(di->dev, "failed to kick main watchdog\n");
2699 /* Disable main watchdog */
2700 ret = abx500_set_register_interruptible(di->dev,
2701 AB8500_SYS_CTRL2_BLOCK,
2702 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
2704 dev_err(di->dev, "failed to disable main watchdog\n");
2708 /* Set watchdog timeout */
2709 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2710 AB8500_CH_WD_TIMER_REG, WD_TIMER);
2712 dev_err(di->dev, "failed to set charger watchdog timeout\n");
2716 /* Backup battery voltage and current */
2717 ret = abx500_set_register_interruptible(di->dev,
2719 AB8500_RTC_BACKUP_CHG_REG,
2720 di->bm->bkup_bat_v |
2721 di->bm->bkup_bat_i);
2723 dev_err(di->dev, "failed to setup backup battery charging\n");
2727 /* Enable backup battery charging */
2728 abx500_mask_and_set_register_interruptible(di->dev,
2729 AB8500_RTC, AB8500_RTC_CTRL_REG,
2730 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
2732 dev_err(di->dev, "%s mask and set failed\n", __func__);
2739 * ab8500 charger driver interrupts and their respective isr
2741 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
2742 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
2743 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
2744 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
2745 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
2746 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
2747 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
2748 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
2749 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
2750 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
2751 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
2752 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
2753 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
2754 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
2757 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
2758 unsigned long event, void *power)
2760 struct ab8500_charger *di =
2761 container_of(nb, struct ab8500_charger, nb);
2762 enum ab8500_usb_state bm_usb_state;
2763 unsigned mA = *((unsigned *)power);
2765 if (event != USB_EVENT_VBUS) {
2766 dev_dbg(di->dev, "not a standard host, returning\n");
2770 /* TODO: State is fabricate here. See if charger really needs USB
2771 * state or if mA is enough
2773 if ((di->usb_state.usb_current == 2) && (mA > 2))
2774 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
2776 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
2778 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
2779 else if (mA >= 8) /* 8, 100, 500 */
2780 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
2781 else /* Should never occur */
2782 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
2784 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
2785 __func__, bm_usb_state, mA);
2787 spin_lock(&di->usb_state.usb_lock);
2788 di->usb_state.usb_changed = true;
2789 spin_unlock(&di->usb_state.usb_lock);
2791 di->usb_state.state = bm_usb_state;
2792 di->usb_state.usb_current = mA;
2794 queue_work(di->charger_wq, &di->usb_state_changed_work);
2799 #if defined(CONFIG_PM)
2800 static int ab8500_charger_resume(struct platform_device *pdev)
2803 struct ab8500_charger *di = platform_get_drvdata(pdev);
2806 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2807 * logic. That means we have to continously kick the charger
2808 * watchdog even when no charger is connected. This is only
2809 * valid once the AC charger has been enabled. This is
2810 * a bug that is not handled by the algorithm and the
2811 * watchdog have to be kicked by the charger driver
2812 * when the AC charger is disabled
2814 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
2815 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2816 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2818 dev_err(di->dev, "Failed to kick WD!\n");
2820 /* If not already pending start a new timer */
2821 if (!delayed_work_pending(
2822 &di->kick_wd_work)) {
2823 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
2824 round_jiffies(WD_KICK_INTERVAL));
2828 /* If we still have a HW failure, schedule a new check */
2829 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2830 queue_delayed_work(di->charger_wq,
2831 &di->check_hw_failure_work, 0);
2837 static int ab8500_charger_suspend(struct platform_device *pdev,
2840 struct ab8500_charger *di = platform_get_drvdata(pdev);
2842 /* Cancel any pending HW failure check */
2843 if (delayed_work_pending(&di->check_hw_failure_work))
2844 cancel_delayed_work(&di->check_hw_failure_work);
2849 #define ab8500_charger_suspend NULL
2850 #define ab8500_charger_resume NULL
2853 static int ab8500_charger_remove(struct platform_device *pdev)
2855 struct ab8500_charger *di = platform_get_drvdata(pdev);
2858 /* Disable AC charging */
2859 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
2861 /* Disable USB charging */
2862 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
2864 /* Disable interrupts */
2865 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2866 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2870 /* Backup battery voltage and current disable */
2871 ret = abx500_mask_and_set_register_interruptible(di->dev,
2872 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
2874 dev_err(di->dev, "%s mask and set failed\n", __func__);
2876 usb_unregister_notifier(di->usb_phy, &di->nb);
2877 usb_put_phy(di->usb_phy);
2879 /* Delete the work queue */
2880 destroy_workqueue(di->charger_wq);
2882 flush_scheduled_work();
2883 if(di->usb_chg.enabled)
2884 power_supply_unregister(&di->usb_chg.psy);
2885 if(di->ac_chg.enabled)
2886 power_supply_unregister(&di->ac_chg.psy);
2888 platform_set_drvdata(pdev, NULL);
2893 static char *supply_interface[] = {
2899 static int ab8500_charger_probe(struct platform_device *pdev)
2901 struct device_node *np = pdev->dev.of_node;
2902 struct abx500_bm_data *plat = pdev->dev.platform_data;
2903 struct ab8500_charger *di;
2904 int irq, i, charger_status, ret = 0, ch_stat;
2906 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
2908 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
2913 dev_err(&pdev->dev, "no battery management data supplied\n");
2919 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
2921 dev_err(&pdev->dev, "failed to get battery information\n");
2924 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
2926 di->autopower_cfg = false;
2928 /* get parent data */
2929 di->dev = &pdev->dev;
2930 di->parent = dev_get_drvdata(pdev->dev.parent);
2931 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2933 /* initialize lock */
2934 spin_lock_init(&di->usb_state.usb_lock);
2936 di->autopower = false;
2937 di->invalid_charger_detect_state = 0;
2940 /* power_supply base class */
2941 di->ac_chg.psy.name = "ab8500_ac";
2942 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
2943 di->ac_chg.psy.properties = ab8500_charger_ac_props;
2944 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
2945 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
2946 di->ac_chg.psy.supplied_to = supply_interface;
2947 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
2948 /* ux500_charger sub-class */
2949 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
2950 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2951 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2952 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
2953 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2954 di->ac_chg.max_out_curr = ab8500_charger_current_map[
2955 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2956 di->ac_chg.enabled = di->pdata->ac_enabled;
2959 /* power_supply base class */
2960 di->usb_chg.psy.name = "ab8500_usb";
2961 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
2962 di->usb_chg.psy.properties = ab8500_charger_usb_props;
2963 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
2964 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
2965 di->usb_chg.psy.supplied_to = supply_interface;
2966 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
2967 /* ux500_charger sub-class */
2968 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
2969 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2970 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2971 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
2972 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2973 di->usb_chg.max_out_curr = ab8500_charger_current_map[
2974 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2975 di->usb_chg.enabled = di->pdata->usb_enabled;
2977 /* Create a work queue for the charger */
2979 create_singlethread_workqueue("ab8500_charger_wq");
2980 if (di->charger_wq == NULL) {
2981 dev_err(di->dev, "failed to create work queue\n");
2985 mutex_init(&di->charger_attached_mutex);
2987 /* Init work for HW failure check */
2988 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
2989 ab8500_charger_check_hw_failure_work);
2990 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
2991 ab8500_charger_check_usbchargernotok_work);
2993 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
2994 ab8500_charger_ac_attached_work);
2995 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
2996 ab8500_charger_usb_attached_work);
2999 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3000 * logic. That means we have to continously kick the charger
3001 * watchdog even when no charger is connected. This is only
3002 * valid once the AC charger has been enabled. This is
3003 * a bug that is not handled by the algorithm and the
3004 * watchdog have to be kicked by the charger driver
3005 * when the AC charger is disabled
3007 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
3008 ab8500_charger_kick_watchdog_work);
3010 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
3011 ab8500_charger_check_vbat_work);
3013 INIT_DELAYED_WORK(&di->attach_work,
3014 ab8500_charger_usb_link_attach_work);
3016 /* Init work for charger detection */
3017 INIT_WORK(&di->usb_link_status_work,
3018 ab8500_charger_usb_link_status_work);
3019 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3020 INIT_WORK(&di->detect_usb_type_work,
3021 ab8500_charger_detect_usb_type_work);
3023 INIT_WORK(&di->usb_state_changed_work,
3024 ab8500_charger_usb_state_changed_work);
3026 /* Init work for checking HW status */
3027 INIT_WORK(&di->check_main_thermal_prot_work,
3028 ab8500_charger_check_main_thermal_prot_work);
3029 INIT_WORK(&di->check_usb_thermal_prot_work,
3030 ab8500_charger_check_usb_thermal_prot_work);
3033 * VDD ADC supply needs to be enabled from this driver when there
3034 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3035 * interrupts during charging
3037 di->regu = devm_regulator_get(di->dev, "vddadc");
3038 if (IS_ERR(di->regu)) {
3039 ret = PTR_ERR(di->regu);
3040 dev_err(di->dev, "failed to get vddadc regulator\n");
3041 goto free_charger_wq;
3045 /* Initialize OVV, and other registers */
3046 ret = ab8500_charger_init_hw_registers(di);
3048 dev_err(di->dev, "failed to initialize ABB registers\n");
3049 goto free_charger_wq;
3052 /* Register AC charger class */
3053 if(di->ac_chg.enabled) {
3054 ret = power_supply_register(di->dev, &di->ac_chg.psy);
3056 dev_err(di->dev, "failed to register AC charger\n");
3057 goto free_charger_wq;
3061 /* Register USB charger class */
3062 if(di->usb_chg.enabled) {
3063 ret = power_supply_register(di->dev, &di->usb_chg.psy);
3065 dev_err(di->dev, "failed to register USB charger\n");
3070 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3071 if (IS_ERR_OR_NULL(di->usb_phy)) {
3072 dev_err(di->dev, "failed to get usb transceiver\n");
3076 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3077 ret = usb_register_notifier(di->usb_phy, &di->nb);
3079 dev_err(di->dev, "failed to register usb notifier\n");
3083 /* Identify the connected charger types during startup */
3084 charger_status = ab8500_charger_detect_chargers(di);
3085 if (charger_status & AC_PW_CONN) {
3086 di->ac.charger_connected = 1;
3088 ab8500_power_supply_changed(di, &di->ac_chg.psy);
3089 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
3092 if (charger_status & USB_PW_CONN) {
3093 di->vbus_detected = true;
3094 di->vbus_detected_start = true;
3095 queue_work(di->charger_wq,
3096 &di->detect_usb_type_work);
3099 /* Register interrupts */
3100 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3101 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3102 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3103 IRQF_SHARED | IRQF_NO_SUSPEND,
3104 ab8500_charger_irq[i].name, di);
3107 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3108 , ab8500_charger_irq[i].name, irq, ret);
3111 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3112 ab8500_charger_irq[i].name, irq, ret);
3115 platform_set_drvdata(pdev, di);
3117 mutex_lock(&di->charger_attached_mutex);
3119 ch_stat = ab8500_charger_detect_chargers(di);
3121 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3122 queue_delayed_work(di->charger_wq,
3123 &di->ac_charger_attached_work,
3126 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3127 queue_delayed_work(di->charger_wq,
3128 &di->usb_charger_attached_work,
3132 mutex_unlock(&di->charger_attached_mutex);
3137 usb_unregister_notifier(di->usb_phy, &di->nb);
3139 /* We also have to free all successfully registered irqs */
3140 for (i = i - 1; i >= 0; i--) {
3141 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3145 usb_put_phy(di->usb_phy);
3147 if(di->usb_chg.enabled)
3148 power_supply_unregister(&di->usb_chg.psy);
3150 if(di->ac_chg.enabled)
3151 power_supply_unregister(&di->ac_chg.psy);
3153 destroy_workqueue(di->charger_wq);
3157 static const struct of_device_id ab8500_charger_match[] = {
3158 { .compatible = "stericsson,ab8500-charger", },
3162 static struct platform_driver ab8500_charger_driver = {
3163 .probe = ab8500_charger_probe,
3164 .remove = ab8500_charger_remove,
3165 .suspend = ab8500_charger_suspend,
3166 .resume = ab8500_charger_resume,
3168 .name = "ab8500-charger",
3169 .owner = THIS_MODULE,
3170 .of_match_table = ab8500_charger_match,
3174 static int __init ab8500_charger_init(void)
3176 return platform_driver_register(&ab8500_charger_driver);
3179 static void __exit ab8500_charger_exit(void)
3181 platform_driver_unregister(&ab8500_charger_driver);
3184 subsys_initcall_sync(ab8500_charger_init);
3185 module_exit(ab8500_charger_exit);
3187 MODULE_LICENSE("GPL v2");
3188 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3189 MODULE_ALIAS("platform:ab8500-charger");
3190 MODULE_DESCRIPTION("AB8500 charger management driver");