1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2012
5 * Charger driver for AB8500
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/notifier.h>
19 #include <linux/slab.h>
20 #include <linux/platform_device.h>
21 #include <linux/power_supply.h>
22 #include <linux/completion.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/err.h>
25 #include <linux/workqueue.h>
26 #include <linux/kobject.h>
28 #include <linux/mfd/core.h>
29 #include <linux/mfd/abx500/ab8500.h>
30 #include <linux/mfd/abx500.h>
31 #include <linux/mfd/abx500/ab8500-bm.h>
32 #include <linux/mfd/abx500/ab8500-gpadc.h>
33 #include <linux/mfd/abx500/ux500_chargalg.h>
34 #include <linux/usb/otg.h>
35 #include <linux/mutex.h>
37 /* Charger constants */
42 #define MAIN_WDOG_ENA 0x01
43 #define MAIN_WDOG_KICK 0x02
44 #define MAIN_WDOG_DIS 0x00
45 #define CHARG_WD_KICK 0x01
46 #define MAIN_CH_ENA 0x01
47 #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
48 #define USB_CH_ENA 0x01
49 #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
50 #define MAIN_CH_DET 0x01
51 #define MAIN_CH_CV_ON 0x04
52 #define USB_CH_CV_ON 0x08
53 #define VBUS_DET_DBNC100 0x02
54 #define VBUS_DET_DBNC1 0x01
55 #define OTP_ENABLE_WD 0x01
56 #define DROP_COUNT_RESET 0x01
57 #define USB_CH_DET 0x01
59 #define MAIN_CH_INPUT_CURR_SHIFT 4
60 #define VBUS_IN_CURR_LIM_SHIFT 4
61 #define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
62 #define VBUS_IN_CURR_LIM_RETRY_SET_TIME 30 /* seconds */
64 #define LED_INDICATOR_PWM_ENA 0x01
65 #define LED_INDICATOR_PWM_DIS 0x00
66 #define LED_IND_CUR_5MA 0x04
67 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
69 /* HW failure constants */
70 #define MAIN_CH_TH_PROT 0x02
71 #define VBUS_CH_NOK 0x08
72 #define USB_CH_TH_PROT 0x02
73 #define VBUS_OVV_TH 0x01
74 #define MAIN_CH_NOK 0x01
77 #define MAIN_CH_STATUS2_MAINCHGDROP 0x80
78 #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
79 #define USB_CH_VBUSDROP 0x40
80 #define USB_CH_VBUSDETDBNC 0x01
82 /* UsbLineStatus register bit masks */
83 #define AB8500_USB_LINK_STATUS 0x78
84 #define AB8505_USB_LINK_STATUS 0xF8
85 #define AB8500_STD_HOST_SUSP 0x18
86 #define USB_LINK_STATUS_SHIFT 3
88 /* Watchdog timeout constant */
89 #define WD_TIMER 0x30 /* 4min */
90 #define WD_KICK_INTERVAL (60 * HZ)
92 /* Lowest charger voltage is 3.39V -> 0x4E */
93 #define LOW_VOLT_REG 0x4E
95 /* Step up/down delay in us */
96 #define STEP_UDELAY 1000
98 #define CHARGER_STATUS_POLL 10 /* in ms */
100 #define CHG_WD_INTERVAL (60 * HZ)
102 #define AB8500_SW_CONTROL_FALLBACK 0x03
103 /* Wait for enumeration before charing in us */
104 #define WAIT_ACA_RID_ENUMERATION (5 * 1000)
105 /*External charger control*/
106 #define AB8500_SYS_CHARGER_CONTROL_REG 0x52
107 #define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
108 #define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
110 /* UsbLineStatus register - usb types */
111 enum ab8500_charger_link_status {
112 USB_STAT_NOT_CONFIGURED,
113 USB_STAT_STD_HOST_NC,
114 USB_STAT_STD_HOST_C_NS,
115 USB_STAT_STD_HOST_C_S,
116 USB_STAT_HOST_CHG_NM,
117 USB_STAT_HOST_CHG_HS,
118 USB_STAT_HOST_CHG_HS_CHIRP,
119 USB_STAT_DEDICATED_CHG,
122 USB_STAT_ACA_RID_C_NM,
123 USB_STAT_ACA_RID_C_HS,
124 USB_STAT_ACA_RID_C_HS_CHIRP,
127 USB_STAT_NOT_VALID_LINK,
129 USB_STAT_SUP_NO_IDGND_VBUS,
130 USB_STAT_SUP_IDGND_VBUS,
131 USB_STAT_CHARGER_LINE_1,
134 USB_STAT_ACA_DOCK_CHARGER,
137 enum ab8500_usb_state {
138 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
139 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
140 AB8500_BM_USB_STATE_CONFIGURED,
141 AB8500_BM_USB_STATE_SUSPEND,
142 AB8500_BM_USB_STATE_RESUME,
143 AB8500_BM_USB_STATE_MAX,
146 /* VBUS input current limits supported in AB8500 in mA */
147 #define USB_CH_IP_CUR_LVL_0P05 50
148 #define USB_CH_IP_CUR_LVL_0P09 98
149 #define USB_CH_IP_CUR_LVL_0P19 193
150 #define USB_CH_IP_CUR_LVL_0P29 290
151 #define USB_CH_IP_CUR_LVL_0P38 380
152 #define USB_CH_IP_CUR_LVL_0P45 450
153 #define USB_CH_IP_CUR_LVL_0P5 500
154 #define USB_CH_IP_CUR_LVL_0P6 600
155 #define USB_CH_IP_CUR_LVL_0P7 700
156 #define USB_CH_IP_CUR_LVL_0P8 800
157 #define USB_CH_IP_CUR_LVL_0P9 900
158 #define USB_CH_IP_CUR_LVL_1P0 1000
159 #define USB_CH_IP_CUR_LVL_1P1 1100
160 #define USB_CH_IP_CUR_LVL_1P3 1300
161 #define USB_CH_IP_CUR_LVL_1P4 1400
162 #define USB_CH_IP_CUR_LVL_1P5 1500
164 #define VBAT_TRESH_IP_CUR_RED 3800
166 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
167 struct ab8500_charger, usb_chg)
168 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
169 struct ab8500_charger, ac_chg)
172 * struct ab8500_charger_interrupts - ab8500 interupts
173 * @name: name of the interrupt
174 * @isr function pointer to the isr
176 struct ab8500_charger_interrupts {
178 irqreturn_t (*isr)(int irq, void *data);
181 struct ab8500_charger_info {
182 int charger_connected;
190 struct ab8500_charger_event_flags {
192 bool main_thermal_prot;
193 bool usb_thermal_prot;
195 bool usbchargernotok;
201 struct ab8500_charger_usb_state {
204 enum ab8500_usb_state state;
205 enum ab8500_usb_state state_tmp;
209 struct ab8500_charger_max_usb_in_curr {
216 * struct ab8500_charger - ab8500 Charger device information
217 * @dev: Pointer to the structure device
218 * @vbus_detected: VBUS detected
219 * @vbus_detected_start:
220 * VBUS detected during startup
221 * @ac_conn: This will be true when the AC charger has been plugged
222 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
224 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
226 * @vbat Battery voltage
227 * @old_vbat Previously measured battery voltage
228 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
229 * @autopower Indicate if we should have automatic pwron after pwrloss
230 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
231 * @invalid_charger_detect_state State when forcing AB to use invalid charger
232 * @is_aca_rid: Incicate if accessory is ACA type
233 * @current_stepping_sessions:
234 * Counter for current stepping sessions
235 * @parent: Pointer to the struct ab8500
236 * @gpadc: Pointer to the struct gpadc
237 * @bm: Platform specific battery management information
238 * @flags: Structure for information about events triggered
239 * @usb_state: Structure for usb stack information
240 * @max_usb_in_curr: Max USB charger input current
241 * @ac_chg: AC charger power supply
242 * @usb_chg: USB charger power supply
243 * @ac: Structure that holds the AC charger properties
244 * @usb: Structure that holds the USB charger properties
245 * @regu: Pointer to the struct regulator
246 * @charger_wq: Work queue for the IRQs and checking HW state
247 * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
248 * @pm_lock: Lock to prevent system to suspend
249 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
250 * @check_hw_failure_work: Work for checking HW state
251 * @check_usbchgnotok_work: Work for checking USB charger not ok status
252 * @kick_wd_work: Work for kicking the charger watchdog in case
253 * of ABB rev 1.* due to the watchog logic bug
254 * @ac_charger_attached_work: Work for checking if AC charger is still
256 * @usb_charger_attached_work: Work for checking if USB charger is still
258 * @ac_work: Work for checking AC charger connection
259 * @detect_usb_type_work: Work for detecting the USB type connected
260 * @usb_link_status_work: Work for checking the new USB link status
261 * @usb_state_changed_work: Work for checking USB state
262 * @attach_work: Work for detecting USB type
263 * @vbus_drop_end_work: Work for detecting VBUS drop end
264 * @check_main_thermal_prot_work:
265 * Work for checking Main thermal status
266 * @check_usb_thermal_prot_work:
267 * Work for checking USB thermal status
268 * @charger_attached_mutex: For controlling the wakelock
270 struct ab8500_charger {
273 bool vbus_detected_start;
279 bool usb_device_is_unrecognised;
282 int invalid_charger_detect_state;
284 atomic_t current_stepping_sessions;
285 struct ab8500 *parent;
286 struct ab8500_gpadc *gpadc;
287 struct abx500_bm_data *bm;
288 struct ab8500_charger_event_flags flags;
289 struct ab8500_charger_usb_state usb_state;
290 struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
291 struct ux500_charger ac_chg;
292 struct ux500_charger usb_chg;
293 struct ab8500_charger_info ac;
294 struct ab8500_charger_info usb;
295 struct regulator *regu;
296 struct workqueue_struct *charger_wq;
297 struct mutex usb_ipt_crnt_lock;
298 struct delayed_work check_vbat_work;
299 struct delayed_work check_hw_failure_work;
300 struct delayed_work check_usbchgnotok_work;
301 struct delayed_work kick_wd_work;
302 struct delayed_work usb_state_changed_work;
303 struct delayed_work attach_work;
304 struct delayed_work ac_charger_attached_work;
305 struct delayed_work usb_charger_attached_work;
306 struct delayed_work vbus_drop_end_work;
307 struct work_struct ac_work;
308 struct work_struct detect_usb_type_work;
309 struct work_struct usb_link_status_work;
310 struct work_struct check_main_thermal_prot_work;
311 struct work_struct check_usb_thermal_prot_work;
312 struct usb_phy *usb_phy;
313 struct notifier_block nb;
314 struct mutex charger_attached_mutex;
318 static enum power_supply_property ab8500_charger_ac_props[] = {
319 POWER_SUPPLY_PROP_HEALTH,
320 POWER_SUPPLY_PROP_PRESENT,
321 POWER_SUPPLY_PROP_ONLINE,
322 POWER_SUPPLY_PROP_VOLTAGE_NOW,
323 POWER_SUPPLY_PROP_VOLTAGE_AVG,
324 POWER_SUPPLY_PROP_CURRENT_NOW,
328 static enum power_supply_property ab8500_charger_usb_props[] = {
329 POWER_SUPPLY_PROP_HEALTH,
330 POWER_SUPPLY_PROP_CURRENT_AVG,
331 POWER_SUPPLY_PROP_PRESENT,
332 POWER_SUPPLY_PROP_ONLINE,
333 POWER_SUPPLY_PROP_VOLTAGE_NOW,
334 POWER_SUPPLY_PROP_VOLTAGE_AVG,
335 POWER_SUPPLY_PROP_CURRENT_NOW,
339 * Function for enabling and disabling sw fallback mode
340 * should always be disabled when no charger is connected.
342 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
351 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
353 if (is_ab8500(di->parent)) {
358 bank = AB8500_SYS_CTRL1_BLOCK;
359 reg = AB8500_SW_CONTROL_FALLBACK;
363 /* read the register containing fallback bit */
364 ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
366 dev_err(di->dev, "%d read failed\n", __LINE__);
370 if (is_ab8500(di->parent)) {
371 /* enable the OPT emulation registers */
372 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
374 dev_err(di->dev, "%d write failed\n", __LINE__);
384 /* write back the changed fallback bit value to register */
385 ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
387 dev_err(di->dev, "%d write failed\n", __LINE__);
391 if (is_ab8500(di->parent)) {
392 /* disable the set OTP registers again */
393 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
395 dev_err(di->dev, "%d write failed\n", __LINE__);
401 * ab8500_power_supply_changed - a wrapper with local extentions for
402 * power_supply_changed
403 * @di: pointer to the ab8500_charger structure
404 * @psy: pointer to power_supply_that have changed.
407 static void ab8500_power_supply_changed(struct ab8500_charger *di,
408 struct power_supply *psy)
410 if (di->autopower_cfg) {
411 if (!di->usb.charger_connected &&
412 !di->ac.charger_connected &&
414 di->autopower = false;
415 ab8500_enable_disable_sw_fallback(di, false);
416 } else if (!di->autopower &&
417 (di->ac.charger_connected ||
418 di->usb.charger_connected)) {
419 di->autopower = true;
420 ab8500_enable_disable_sw_fallback(di, true);
423 power_supply_changed(psy);
426 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
429 if (connected != di->usb.charger_connected) {
430 dev_dbg(di->dev, "USB connected:%i\n", connected);
431 di->usb.charger_connected = connected;
434 di->flags.vbus_drop_end = false;
436 sysfs_notify(&di->usb_chg.psy->dev.kobj, NULL, "present");
439 mutex_lock(&di->charger_attached_mutex);
440 mutex_unlock(&di->charger_attached_mutex);
442 if (is_ab8500(di->parent))
443 queue_delayed_work(di->charger_wq,
444 &di->usb_charger_attached_work,
447 cancel_delayed_work_sync(&di->usb_charger_attached_work);
448 mutex_lock(&di->charger_attached_mutex);
449 mutex_unlock(&di->charger_attached_mutex);
455 * ab8500_charger_get_ac_voltage() - get ac charger voltage
456 * @di: pointer to the ab8500_charger structure
458 * Returns ac charger voltage (on success)
460 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
464 /* Only measure voltage if the charger is connected */
465 if (di->ac.charger_connected) {
466 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
468 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
476 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
477 * @di: pointer to the ab8500_charger structure
479 * Returns ac charger CV mode (on success) else error code
481 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
486 /* Only check CV mode if the charger is online */
487 if (di->ac.charger_online) {
488 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
489 AB8500_CH_STATUS1_REG, &val);
491 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
495 if (val & MAIN_CH_CV_ON)
505 * ab8500_charger_get_vbus_voltage() - get vbus voltage
506 * @di: pointer to the ab8500_charger structure
508 * This function returns the vbus voltage.
509 * Returns vbus voltage (on success)
511 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
515 /* Only measure voltage if the charger is connected */
516 if (di->usb.charger_connected) {
517 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
519 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
527 * ab8500_charger_get_usb_current() - get usb charger current
528 * @di: pointer to the ab8500_charger structure
530 * This function returns the usb charger current.
531 * Returns usb current (on success) and error code on failure
533 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
537 /* Only measure current if the charger is online */
538 if (di->usb.charger_online) {
539 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
541 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
549 * ab8500_charger_get_ac_current() - get ac charger current
550 * @di: pointer to the ab8500_charger structure
552 * This function returns the ac charger current.
553 * Returns ac current (on success) and error code on failure.
555 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
559 /* Only measure current if the charger is online */
560 if (di->ac.charger_online) {
561 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
563 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
571 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
572 * @di: pointer to the ab8500_charger structure
574 * Returns ac charger CV mode (on success) else error code
576 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
581 /* Only check CV mode if the charger is online */
582 if (di->usb.charger_online) {
583 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
584 AB8500_CH_USBCH_STAT1_REG, &val);
586 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
590 if (val & USB_CH_CV_ON)
602 * ab8500_charger_detect_chargers() - Detect the connected chargers
603 * @di: pointer to the ab8500_charger structure
604 * @probe: if probe, don't delay and wait for HW
606 * Returns the type of charger connected.
607 * For USB it will not mean we can actually charge from it
608 * but that there is a USB cable connected that we have to
609 * identify. This is used during startup when we don't get
610 * interrupts of the charger detection
612 * Returns an integer value, that means,
613 * NO_PW_CONN no power supply is connected
614 * AC_PW_CONN if the AC power supply is connected
615 * USB_PW_CONN if the USB power supply is connected
616 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
618 static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
620 int result = NO_PW_CONN;
624 /* Check for AC charger */
625 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
626 AB8500_CH_STATUS1_REG, &val);
628 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
632 if (val & MAIN_CH_DET)
635 /* Check for USB charger */
639 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
640 * when disconnecting ACA even though no
641 * charger was connected. Try waiting a little
642 * longer than the 100 ms of VBUS_DET_DBNC100...
646 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
647 AB8500_CH_USBCH_STAT1_REG, &val);
649 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
653 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
655 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
656 result |= USB_PW_CONN;
662 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
663 * @di: pointer to the ab8500_charger structure
664 * @link_status: the identified USB type
666 * Get the maximum current that is allowed to be drawn from the host
667 * based on the USB type.
668 * Returns error code in case of failure else 0 on success
670 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
671 enum ab8500_charger_link_status link_status)
675 di->usb_device_is_unrecognised = false;
678 * Platform only supports USB 2.0.
679 * This means that charging current from USB source
680 * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
681 * should set USB_CH_IP_CUR_LVL_0P5.
684 switch (link_status) {
685 case USB_STAT_STD_HOST_NC:
686 case USB_STAT_STD_HOST_C_NS:
687 case USB_STAT_STD_HOST_C_S:
688 dev_dbg(di->dev, "USB Type - Standard host is "
689 "detected through USB driver\n");
690 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
693 case USB_STAT_HOST_CHG_HS_CHIRP:
694 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
697 case USB_STAT_HOST_CHG_HS:
698 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
701 case USB_STAT_ACA_RID_C_HS:
702 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
705 case USB_STAT_ACA_RID_A:
707 * Dedicated charger level minus maximum current accessory
708 * can consume (900mA). Closest level is 500mA
710 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
711 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
714 case USB_STAT_ACA_RID_B:
716 * Dedicated charger level minus 120mA (20mA for ACA and
717 * 100mA for potential accessory). Closest level is 1300mA
719 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
720 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
721 di->max_usb_in_curr.usb_type_max);
724 case USB_STAT_HOST_CHG_NM:
725 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
728 case USB_STAT_DEDICATED_CHG:
729 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
732 case USB_STAT_ACA_RID_C_HS_CHIRP:
733 case USB_STAT_ACA_RID_C_NM:
734 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
737 case USB_STAT_NOT_CONFIGURED:
738 if (di->vbus_detected) {
739 di->usb_device_is_unrecognised = true;
740 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
741 di->max_usb_in_curr.usb_type_max =
742 USB_CH_IP_CUR_LVL_1P5;
745 /* Else, fall through */
746 case USB_STAT_HM_IDGND:
747 dev_err(di->dev, "USB Type - Charging not allowed\n");
748 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
751 case USB_STAT_RESERVED:
752 if (is_ab8500(di->parent)) {
753 di->flags.vbus_collapse = true;
754 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
755 "VBUS has collapsed\n");
759 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
760 di->max_usb_in_curr.usb_type_max =
761 USB_CH_IP_CUR_LVL_0P05;
762 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
764 di->max_usb_in_curr.usb_type_max);
768 case USB_STAT_CARKIT_1:
769 case USB_STAT_CARKIT_2:
770 case USB_STAT_ACA_DOCK_CHARGER:
771 case USB_STAT_CHARGER_LINE_1:
772 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
773 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
774 di->max_usb_in_curr.usb_type_max);
776 case USB_STAT_NOT_VALID_LINK:
777 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
778 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
782 dev_err(di->dev, "USB Type - Unknown\n");
783 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
788 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
789 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
790 link_status, di->max_usb_in_curr.set_max);
796 * ab8500_charger_read_usb_type() - read the type of usb connected
797 * @di: pointer to the ab8500_charger structure
799 * Detect the type of the plugged USB
800 * Returns error code in case of failure else 0 on success
802 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
807 ret = abx500_get_register_interruptible(di->dev,
808 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
810 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
813 if (is_ab8500(di->parent))
814 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
815 AB8500_USB_LINE_STAT_REG, &val);
817 ret = abx500_get_register_interruptible(di->dev,
818 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
820 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
824 /* get the USB type */
825 if (is_ab8500(di->parent))
826 val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
828 val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
829 ret = ab8500_charger_max_usb_curr(di,
830 (enum ab8500_charger_link_status) val);
836 * ab8500_charger_detect_usb_type() - get the type of usb connected
837 * @di: pointer to the ab8500_charger structure
839 * Detect the type of the plugged USB
840 * Returns error code in case of failure else 0 on success
842 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
848 * On getting the VBUS rising edge detect interrupt there
849 * is a 250ms delay after which the register UsbLineStatus
850 * is filled with valid data.
852 for (i = 0; i < 10; i++) {
854 ret = abx500_get_register_interruptible(di->dev,
855 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
857 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
860 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
864 if (is_ab8500(di->parent))
865 ret = abx500_get_register_interruptible(di->dev,
866 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
868 ret = abx500_get_register_interruptible(di->dev,
869 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
871 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
874 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
877 * Until the IT source register is read the UsbLineStatus
878 * register is not updated, hence doing the same
882 /* get the USB type */
883 if (is_ab8500(di->parent))
884 val = (val & AB8500_USB_LINK_STATUS) >>
885 USB_LINK_STATUS_SHIFT;
887 val = (val & AB8505_USB_LINK_STATUS) >>
888 USB_LINK_STATUS_SHIFT;
892 ret = ab8500_charger_max_usb_curr(di,
893 (enum ab8500_charger_link_status) val);
899 * This array maps the raw hex value to charger voltage used by the AB8500
900 * Values taken from the UM0836
902 static int ab8500_charger_voltage_map[] = {
983 static int ab8500_voltage_to_regval(int voltage)
987 /* Special case for voltage below 3.5V */
988 if (voltage < ab8500_charger_voltage_map[0])
991 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
992 if (voltage < ab8500_charger_voltage_map[i])
996 /* If not last element, return error */
997 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
998 if (voltage == ab8500_charger_voltage_map[i])
1004 static int ab8500_current_to_regval(struct ab8500_charger *di, int curr)
1008 if (curr < di->bm->chg_output_curr[0])
1011 for (i = 0; i < di->bm->n_chg_out_curr; i++) {
1012 if (curr < di->bm->chg_output_curr[i])
1016 /* If not last element, return error */
1017 i = di->bm->n_chg_out_curr - 1;
1018 if (curr == di->bm->chg_output_curr[i])
1024 static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr)
1028 if (curr < di->bm->chg_input_curr[0])
1031 for (i = 0; i < di->bm->n_chg_in_curr; i++) {
1032 if (curr < di->bm->chg_input_curr[i])
1036 /* If not last element, return error */
1037 i = di->bm->n_chg_in_curr - 1;
1038 if (curr == di->bm->chg_input_curr[i])
1045 * ab8500_charger_get_usb_cur() - get usb current
1046 * @di: pointer to the ab8500_charger structre
1048 * The usb stack provides the maximum current that can be drawn from
1049 * the standard usb host. This will be in mA.
1050 * This function converts current in mA to a value that can be written
1051 * to the register. Returns -1 if charging is not allowed
1053 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1056 switch (di->usb_state.usb_current) {
1058 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
1061 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
1064 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
1067 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
1070 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
1073 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
1077 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
1082 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1083 * @di: pointer to the ab8500_charger structure
1084 * @reg: select what charger register to check
1086 * Check if current stepping should be allowed to continue.
1087 * Checks if charger source has not collapsed. If it has, further stepping
1090 static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1093 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1094 return !di->flags.vbus_drop_end;
1100 * ab8500_charger_set_current() - set charger current
1101 * @di: pointer to the ab8500_charger structure
1102 * @ich: charger current, in mA
1103 * @reg: select what charger register to set
1105 * Set charger current.
1106 * There is no state machine in the AB to step up/down the charger
1107 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1108 * charging is started. Instead we need to implement
1109 * this charger current step-up/down here.
1110 * Returns error code in case of failure else 0(on success)
1112 static int ab8500_charger_set_current(struct ab8500_charger *di,
1116 int curr_index, prev_curr_index, shift_value, i;
1119 bool no_stepping = false;
1121 atomic_inc(&di->current_stepping_sessions);
1123 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1126 dev_err(di->dev, "%s read failed\n", __func__);
1127 goto exit_set_current;
1131 case AB8500_MCH_IPT_CURLVL_REG:
1132 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1133 prev_curr_index = (reg_value >> shift_value);
1134 curr_index = ab8500_current_to_regval(di, ich);
1135 step_udelay = STEP_UDELAY;
1136 if (!di->ac.charger_connected)
1139 case AB8500_USBCH_IPT_CRNTLVL_REG:
1140 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1141 prev_curr_index = (reg_value >> shift_value);
1142 curr_index = ab8500_vbus_in_curr_to_regval(di, ich);
1143 step_udelay = STEP_UDELAY * 100;
1145 if (!di->usb.charger_connected)
1148 case AB8500_CH_OPT_CRNTLVL_REG:
1150 prev_curr_index = (reg_value >> shift_value);
1151 curr_index = ab8500_current_to_regval(di, ich);
1152 step_udelay = STEP_UDELAY;
1153 if (curr_index && (curr_index - prev_curr_index) > 1)
1156 if (!di->usb.charger_connected && !di->ac.charger_connected)
1161 dev_err(di->dev, "%s current register not valid\n", __func__);
1163 goto exit_set_current;
1166 if (curr_index < 0) {
1167 dev_err(di->dev, "requested current limit out-of-range\n");
1169 goto exit_set_current;
1172 /* only update current if it's been changed */
1173 if (prev_curr_index == curr_index) {
1174 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1177 goto exit_set_current;
1180 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1181 __func__, ich, reg);
1184 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1185 reg, (u8)curr_index << shift_value);
1187 dev_err(di->dev, "%s write failed\n", __func__);
1188 } else if (prev_curr_index > curr_index) {
1189 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1190 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1191 (u8) i << shift_value, reg);
1192 ret = abx500_set_register_interruptible(di->dev,
1193 AB8500_CHARGER, reg, (u8)i << shift_value);
1195 dev_err(di->dev, "%s write failed\n", __func__);
1196 goto exit_set_current;
1198 if (i != curr_index)
1199 usleep_range(step_udelay, step_udelay * 2);
1203 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
1204 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1205 (u8)i << shift_value, reg);
1206 ret = abx500_set_register_interruptible(di->dev,
1207 AB8500_CHARGER, reg, (u8)i << shift_value);
1209 dev_err(di->dev, "%s write failed\n", __func__);
1210 goto exit_set_current;
1212 if (i != curr_index)
1213 usleep_range(step_udelay, step_udelay * 2);
1215 allow = ab8500_charger_check_continue_stepping(di, reg);
1220 atomic_dec(&di->current_stepping_sessions);
1226 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1227 * @di: pointer to the ab8500_charger structure
1228 * @ich_in: charger input current limit
1230 * Sets the current that can be drawn from the USB host
1231 * Returns error code in case of failure else 0(on success)
1233 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1239 /* We should always use to lowest current limit */
1240 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1241 if (di->max_usb_in_curr.set_max > 0)
1242 min_value = min(di->max_usb_in_curr.set_max, min_value);
1244 if (di->usb_state.usb_current >= 0)
1245 min_value = min(di->usb_state.usb_current, min_value);
1247 switch (min_value) {
1249 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1250 min_value = USB_CH_IP_CUR_LVL_0P05;
1253 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1254 min_value = USB_CH_IP_CUR_LVL_0P45;
1260 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1262 mutex_lock(&di->usb_ipt_crnt_lock);
1263 ret = ab8500_charger_set_current(di, min_value,
1264 AB8500_USBCH_IPT_CRNTLVL_REG);
1265 mutex_unlock(&di->usb_ipt_crnt_lock);
1271 * ab8500_charger_set_main_in_curr() - set main charger input current
1272 * @di: pointer to the ab8500_charger structure
1273 * @ich_in: input charger current, in mA
1275 * Set main charger input current.
1276 * Returns error code in case of failure else 0(on success)
1278 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1281 return ab8500_charger_set_current(di, ich_in,
1282 AB8500_MCH_IPT_CURLVL_REG);
1286 * ab8500_charger_set_output_curr() - set charger output current
1287 * @di: pointer to the ab8500_charger structure
1288 * @ich_out: output charger current, in mA
1290 * Set charger output current.
1291 * Returns error code in case of failure else 0(on success)
1293 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1296 return ab8500_charger_set_current(di, ich_out,
1297 AB8500_CH_OPT_CRNTLVL_REG);
1301 * ab8500_charger_led_en() - turn on/off chargign led
1302 * @di: pointer to the ab8500_charger structure
1303 * @on: flag to turn on/off the chargign led
1305 * Power ON/OFF charging LED indication
1306 * Returns error code in case of failure else 0(on success)
1308 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1313 /* Power ON charging LED indicator, set LED current to 5mA */
1314 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1315 AB8500_LED_INDICATOR_PWM_CTRL,
1316 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1318 dev_err(di->dev, "Power ON LED failed\n");
1321 /* LED indicator PWM duty cycle 252/256 */
1322 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1323 AB8500_LED_INDICATOR_PWM_DUTY,
1324 LED_INDICATOR_PWM_DUTY_252_256);
1326 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1330 /* Power off charging LED indicator */
1331 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1332 AB8500_LED_INDICATOR_PWM_CTRL,
1333 LED_INDICATOR_PWM_DIS);
1335 dev_err(di->dev, "Power-off LED failed\n");
1344 * ab8500_charger_ac_en() - enable or disable ac charging
1345 * @di: pointer to the ab8500_charger structure
1346 * @enable: enable/disable flag
1347 * @vset: charging voltage
1348 * @iset: charging current
1350 * Enable/Disable AC/Mains charging and turns on/off the charging led
1353 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1354 int enable, int vset, int iset)
1359 int input_curr_index;
1362 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1365 /* Check if AC is connected */
1366 if (!di->ac.charger_connected) {
1367 dev_err(di->dev, "AC charger not connected\n");
1371 /* Enable AC charging */
1372 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1375 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1376 * will be triggered everytime we enable the VDD ADC supply.
1377 * This will turn off charging for a short while.
1378 * It can be avoided by having the supply on when
1379 * there is a charger enabled. Normally the VDD ADC supply
1380 * is enabled everytime a GPADC conversion is triggered. We will
1381 * force it to be enabled from this driver to have
1382 * the GPADC module independant of the AB8500 chargers
1384 if (!di->vddadc_en_ac) {
1385 ret = regulator_enable(di->regu);
1388 "Failed to enable regulator\n");
1390 di->vddadc_en_ac = true;
1393 /* Check if the requested voltage or current is valid */
1394 volt_index = ab8500_voltage_to_regval(vset);
1395 curr_index = ab8500_current_to_regval(di, iset);
1396 input_curr_index = ab8500_current_to_regval(di,
1397 di->bm->chg_params->ac_curr_max);
1398 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1400 "Charger voltage or current too high, "
1401 "charging not started\n");
1405 /* ChVoltLevel: maximum battery charging voltage */
1406 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1407 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1409 dev_err(di->dev, "%s write failed\n", __func__);
1412 /* MainChInputCurr: current that can be drawn from the charger*/
1413 ret = ab8500_charger_set_main_in_curr(di,
1414 di->bm->chg_params->ac_curr_max);
1416 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1420 /* ChOutputCurentLevel: protected output current */
1421 ret = ab8500_charger_set_output_curr(di, iset);
1423 dev_err(di->dev, "%s "
1424 "Failed to set ChOutputCurentLevel\n",
1429 /* Check if VBAT overshoot control should be enabled */
1430 if (!di->bm->enable_overshoot)
1431 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1433 /* Enable Main Charger */
1434 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1435 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1437 dev_err(di->dev, "%s write failed\n", __func__);
1441 /* Power on charging LED indication */
1442 ret = ab8500_charger_led_en(di, true);
1444 dev_err(di->dev, "failed to enable LED\n");
1446 di->ac.charger_online = 1;
1448 /* Disable AC charging */
1449 if (is_ab8500_1p1_or_earlier(di->parent)) {
1451 * For ABB revision 1.0 and 1.1 there is a bug in the
1452 * watchdog logic. That means we have to continously
1453 * kick the charger watchdog even when no charger is
1454 * connected. This is only valid once the AC charger
1455 * has been enabled. This is a bug that is not handled
1456 * by the algorithm and the watchdog have to be kicked
1457 * by the charger driver when the AC charger
1461 queue_delayed_work(di->charger_wq,
1463 round_jiffies(WD_KICK_INTERVAL));
1467 * We can't turn off charging completely
1468 * due to a bug in AB8500 cut1.
1469 * If we do, charging will not start again.
1470 * That is why we set the lowest voltage
1471 * and current possible
1473 ret = abx500_set_register_interruptible(di->dev,
1475 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1478 "%s write failed\n", __func__);
1482 ret = ab8500_charger_set_output_curr(di, 0);
1484 dev_err(di->dev, "%s "
1485 "Failed to set ChOutputCurentLevel\n",
1490 ret = abx500_set_register_interruptible(di->dev,
1492 AB8500_MCH_CTRL1, 0);
1495 "%s write failed\n", __func__);
1500 ret = ab8500_charger_led_en(di, false);
1502 dev_err(di->dev, "failed to disable LED\n");
1504 di->ac.charger_online = 0;
1505 di->ac.wd_expired = false;
1507 /* Disable regulator if enabled */
1508 if (di->vddadc_en_ac) {
1509 regulator_disable(di->regu);
1510 di->vddadc_en_ac = false;
1513 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1515 ab8500_power_supply_changed(di, di->ac_chg.psy);
1521 * ab8500_charger_usb_en() - enable usb charging
1522 * @di: pointer to the ab8500_charger structure
1523 * @enable: enable/disable flag
1524 * @vset: charging voltage
1525 * @ich_out: charger output current
1527 * Enable/Disable USB charging and turns on/off the charging led respectively.
1528 * Returns error code in case of failure else 0(on success)
1530 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1531 int enable, int vset, int ich_out)
1538 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1541 /* Check if USB is connected */
1542 if (!di->usb.charger_connected) {
1543 dev_err(di->dev, "USB charger not connected\n");
1548 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1549 * will be triggered everytime we enable the VDD ADC supply.
1550 * This will turn off charging for a short while.
1551 * It can be avoided by having the supply on when
1552 * there is a charger enabled. Normally the VDD ADC supply
1553 * is enabled everytime a GPADC conversion is triggered. We will
1554 * force it to be enabled from this driver to have
1555 * the GPADC module independant of the AB8500 chargers
1557 if (!di->vddadc_en_usb) {
1558 ret = regulator_enable(di->regu);
1561 "Failed to enable regulator\n");
1563 di->vddadc_en_usb = true;
1566 /* Enable USB charging */
1567 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1569 /* Check if the requested voltage or current is valid */
1570 volt_index = ab8500_voltage_to_regval(vset);
1571 curr_index = ab8500_current_to_regval(di, ich_out);
1572 if (volt_index < 0 || curr_index < 0) {
1574 "Charger voltage or current too high, "
1575 "charging not started\n");
1579 /* ChVoltLevel: max voltage upto which battery can be charged */
1580 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1581 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1583 dev_err(di->dev, "%s write failed\n", __func__);
1586 /* Check if VBAT overshoot control should be enabled */
1587 if (!di->bm->enable_overshoot)
1588 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1590 /* Enable USB Charger */
1592 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
1593 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1594 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1596 dev_err(di->dev, "%s write failed\n", __func__);
1600 /* If success power on charging LED indication */
1601 ret = ab8500_charger_led_en(di, true);
1603 dev_err(di->dev, "failed to enable LED\n");
1605 di->usb.charger_online = 1;
1607 /* USBChInputCurr: current that can be drawn from the usb */
1608 ret = ab8500_charger_set_vbus_in_curr(di,
1609 di->max_usb_in_curr.usb_type_max);
1611 dev_err(di->dev, "setting USBChInputCurr failed\n");
1615 /* ChOutputCurentLevel: protected output current */
1616 ret = ab8500_charger_set_output_curr(di, ich_out);
1618 dev_err(di->dev, "%s "
1619 "Failed to set ChOutputCurentLevel\n",
1624 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1627 /* Disable USB charging */
1628 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1629 ret = abx500_set_register_interruptible(di->dev,
1631 AB8500_USBCH_CTRL1_REG, 0);
1634 "%s write failed\n", __func__);
1638 ret = ab8500_charger_led_en(di, false);
1640 dev_err(di->dev, "failed to disable LED\n");
1641 /* USBChInputCurr: current that can be drawn from the usb */
1642 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1644 dev_err(di->dev, "setting USBChInputCurr failed\n");
1648 /* ChOutputCurentLevel: protected output current */
1649 ret = ab8500_charger_set_output_curr(di, 0);
1651 dev_err(di->dev, "%s "
1652 "Failed to reset ChOutputCurentLevel\n",
1656 di->usb.charger_online = 0;
1657 di->usb.wd_expired = false;
1659 /* Disable regulator if enabled */
1660 if (di->vddadc_en_usb) {
1661 regulator_disable(di->regu);
1662 di->vddadc_en_usb = false;
1665 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1667 /* Cancel any pending Vbat check work */
1668 cancel_delayed_work(&di->check_vbat_work);
1671 ab8500_power_supply_changed(di, di->usb_chg.psy);
1676 static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1677 unsigned long event, void *data)
1680 struct device *dev = data;
1681 /*Toggle External charger control pin*/
1682 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1683 AB8500_SYS_CHARGER_CONTROL_REG,
1684 EXTERNAL_CHARGER_DISABLE_REG_VAL);
1686 dev_err(dev, "write reg failed %d\n", ret);
1689 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1690 AB8500_SYS_CHARGER_CONTROL_REG,
1691 EXTERNAL_CHARGER_ENABLE_REG_VAL);
1693 dev_err(dev, "Write reg failed %d\n", ret);
1700 * ab8500_charger_usb_check_enable() - enable usb charging
1701 * @charger: pointer to the ux500_charger structure
1702 * @vset: charging voltage
1703 * @iset: charger output current
1705 * Check if the VBUS charger has been disconnected and reconnected without
1706 * AB8500 rising an interrupt. Returns 0 on success.
1708 static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1714 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1716 if (!di->usb.charger_connected)
1719 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1720 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1722 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1725 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1727 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1728 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1730 ret = abx500_mask_and_set_register_interruptible(di->dev,
1731 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1732 DROP_COUNT_RESET, DROP_COUNT_RESET);
1734 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1738 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1740 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1749 * ab8500_charger_ac_check_enable() - enable usb charging
1750 * @charger: pointer to the ux500_charger structure
1751 * @vset: charging voltage
1752 * @iset: charger output current
1754 * Check if the AC charger has been disconnected and reconnected without
1755 * AB8500 rising an interrupt. Returns 0 on success.
1757 static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1760 u8 mainch_ctrl1 = 0;
1763 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1765 if (!di->ac.charger_connected)
1768 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1769 AB8500_MCH_CTRL1, &mainch_ctrl1);
1771 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1774 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1776 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1777 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1779 ret = abx500_mask_and_set_register_interruptible(di->dev,
1780 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1781 DROP_COUNT_RESET, DROP_COUNT_RESET);
1784 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1788 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1790 dev_err(di->dev, "failed to enable AC charger %d\n",
1799 * ab8500_charger_watchdog_kick() - kick charger watchdog
1800 * @di: pointer to the ab8500_charger structure
1802 * Kick charger watchdog
1803 * Returns error code in case of failure else 0(on success)
1805 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1808 struct ab8500_charger *di;
1810 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1811 di = to_ab8500_charger_ac_device_info(charger);
1812 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1813 di = to_ab8500_charger_usb_device_info(charger);
1817 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1818 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1820 dev_err(di->dev, "Failed to kick WD!\n");
1826 * ab8500_charger_update_charger_current() - update charger current
1827 * @di: pointer to the ab8500_charger structure
1829 * Update the charger output current for the specified charger
1830 * Returns error code in case of failure else 0(on success)
1832 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1836 struct ab8500_charger *di;
1838 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1839 di = to_ab8500_charger_ac_device_info(charger);
1840 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1841 di = to_ab8500_charger_usb_device_info(charger);
1845 ret = ab8500_charger_set_output_curr(di, ich_out);
1847 dev_err(di->dev, "%s "
1848 "Failed to set ChOutputCurentLevel\n",
1853 /* Reset the main and usb drop input current measurement counter */
1854 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1855 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
1857 dev_err(di->dev, "%s write failed\n", __func__);
1864 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1866 struct power_supply *psy;
1867 struct power_supply *ext = dev_get_drvdata(dev);
1868 const char **supplicants = (const char **)ext->supplied_to;
1869 struct ab8500_charger *di;
1870 union power_supply_propval ret;
1872 struct ux500_charger *usb_chg;
1874 usb_chg = (struct ux500_charger *)data;
1877 di = to_ab8500_charger_usb_device_info(usb_chg);
1879 /* For all psy where the driver name appears in any supplied_to */
1880 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
1884 /* Go through all properties for the psy */
1885 for (j = 0; j < ext->desc->num_properties; j++) {
1886 enum power_supply_property prop;
1887 prop = ext->desc->properties[j];
1889 if (power_supply_get_property(ext, prop, &ret))
1893 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1894 switch (ext->desc->type) {
1895 case POWER_SUPPLY_TYPE_BATTERY:
1896 di->vbat = ret.intval / 1000;
1910 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1911 * @work pointer to the work_struct structure
1913 * Due to a asic bug it is necessary to lower the input current to the vbus
1914 * charger when charging with at some specific levels. This issue is only valid
1915 * for below a certain battery voltage. This function makes sure that the
1916 * the allowed current limit isn't exceeded.
1918 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1921 struct ab8500_charger *di = container_of(work,
1922 struct ab8500_charger, check_vbat_work.work);
1924 class_for_each_device(power_supply_class, NULL,
1925 di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1927 /* First run old_vbat is 0. */
1928 if (di->old_vbat == 0)
1929 di->old_vbat = di->vbat;
1931 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1932 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1933 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1934 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1936 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1937 " old: %d\n", di->max_usb_in_curr.usb_type_max,
1938 di->vbat, di->old_vbat);
1939 ab8500_charger_set_vbus_in_curr(di,
1940 di->max_usb_in_curr.usb_type_max);
1941 power_supply_changed(di->usb_chg.psy);
1944 di->old_vbat = di->vbat;
1947 * No need to check the battery voltage every second when not close to
1950 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1951 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1954 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1958 * ab8500_charger_check_hw_failure_work() - check main charger failure
1959 * @work: pointer to the work_struct structure
1961 * Work queue function for checking the main charger status
1963 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1968 struct ab8500_charger *di = container_of(work,
1969 struct ab8500_charger, check_hw_failure_work.work);
1971 /* Check if the status bits for HW failure is still active */
1972 if (di->flags.mainextchnotok) {
1973 ret = abx500_get_register_interruptible(di->dev,
1974 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1976 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1979 if (!(reg_value & MAIN_CH_NOK)) {
1980 di->flags.mainextchnotok = false;
1981 ab8500_power_supply_changed(di, di->ac_chg.psy);
1984 if (di->flags.vbus_ovv) {
1985 ret = abx500_get_register_interruptible(di->dev,
1986 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1989 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1992 if (!(reg_value & VBUS_OVV_TH)) {
1993 di->flags.vbus_ovv = false;
1994 ab8500_power_supply_changed(di, di->usb_chg.psy);
1997 /* If we still have a failure, schedule a new check */
1998 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1999 queue_delayed_work(di->charger_wq,
2000 &di->check_hw_failure_work, round_jiffies(HZ));
2005 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2006 * @work: pointer to the work_struct structure
2008 * Work queue function for kicking the charger watchdog.
2010 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2011 * logic. That means we have to continously kick the charger
2012 * watchdog even when no charger is connected. This is only
2013 * valid once the AC charger has been enabled. This is
2014 * a bug that is not handled by the algorithm and the
2015 * watchdog have to be kicked by the charger driver
2016 * when the AC charger is disabled
2018 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2022 struct ab8500_charger *di = container_of(work,
2023 struct ab8500_charger, kick_wd_work.work);
2025 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2026 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2028 dev_err(di->dev, "Failed to kick WD!\n");
2030 /* Schedule a new watchdog kick */
2031 queue_delayed_work(di->charger_wq,
2032 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2036 * ab8500_charger_ac_work() - work to get and set main charger status
2037 * @work: pointer to the work_struct structure
2039 * Work queue function for checking the main charger status
2041 static void ab8500_charger_ac_work(struct work_struct *work)
2045 struct ab8500_charger *di = container_of(work,
2046 struct ab8500_charger, ac_work);
2049 * Since we can't be sure that the events are received
2050 * synchronously, we have the check if the main charger is
2051 * connected by reading the status register
2053 ret = ab8500_charger_detect_chargers(di, false);
2057 if (ret & AC_PW_CONN) {
2058 di->ac.charger_connected = 1;
2061 di->ac.charger_connected = 0;
2064 ab8500_power_supply_changed(di, di->ac_chg.psy);
2065 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
2068 static void ab8500_charger_usb_attached_work(struct work_struct *work)
2070 struct ab8500_charger *di = container_of(work,
2071 struct ab8500_charger,
2072 usb_charger_attached_work.work);
2073 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2077 for (i = 0; i < 10; i++) {
2078 ret = abx500_get_register_interruptible(di->dev,
2080 AB8500_CH_USBCH_STAT1_REG,
2083 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2086 if ((statval & usbch) != usbch)
2089 msleep(CHARGER_STATUS_POLL);
2092 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2094 mutex_lock(&di->charger_attached_mutex);
2095 mutex_unlock(&di->charger_attached_mutex);
2100 queue_delayed_work(di->charger_wq,
2101 &di->usb_charger_attached_work,
2105 static void ab8500_charger_ac_attached_work(struct work_struct *work)
2108 struct ab8500_charger *di = container_of(work,
2109 struct ab8500_charger,
2110 ac_charger_attached_work.work);
2111 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2112 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2116 for (i = 0; i < 10; i++) {
2117 ret = abx500_get_register_interruptible(di->dev,
2119 AB8500_CH_STATUS2_REG,
2122 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2126 if ((statval & mainch) != mainch)
2129 msleep(CHARGER_STATUS_POLL);
2132 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2133 queue_work(di->charger_wq, &di->ac_work);
2135 mutex_lock(&di->charger_attached_mutex);
2136 mutex_unlock(&di->charger_attached_mutex);
2141 queue_delayed_work(di->charger_wq,
2142 &di->ac_charger_attached_work,
2147 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2148 * @work: Pointer to the work_struct structure
2150 * Detect the type of USB plugged
2152 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
2156 struct ab8500_charger *di = container_of(work,
2157 struct ab8500_charger, detect_usb_type_work);
2160 * Since we can't be sure that the events are received
2161 * synchronously, we have the check if is
2162 * connected by reading the status register
2164 ret = ab8500_charger_detect_chargers(di, false);
2168 if (!(ret & USB_PW_CONN)) {
2169 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2170 di->vbus_detected = false;
2171 ab8500_charger_set_usb_connected(di, false);
2172 ab8500_power_supply_changed(di, di->usb_chg.psy);
2174 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2175 di->vbus_detected = true;
2177 if (is_ab8500_1p1_or_earlier(di->parent)) {
2178 ret = ab8500_charger_detect_usb_type(di);
2180 ab8500_charger_set_usb_connected(di, true);
2181 ab8500_power_supply_changed(di,
2186 * For ABB cut2.0 and onwards we have an IRQ,
2187 * USB_LINK_STATUS that will be triggered when the USB
2188 * link status changes. The exception is USB connected
2189 * during startup. Then we don't get a
2190 * USB_LINK_STATUS IRQ
2192 if (di->vbus_detected_start) {
2193 di->vbus_detected_start = false;
2194 ret = ab8500_charger_detect_usb_type(di);
2196 ab8500_charger_set_usb_connected(di,
2198 ab8500_power_supply_changed(di,
2207 * ab8500_charger_usb_link_attach_work() - work to detect USB type
2208 * @work: pointer to the work_struct structure
2210 * Detect the type of USB plugged
2212 static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2214 struct ab8500_charger *di =
2215 container_of(work, struct ab8500_charger, attach_work.work);
2218 /* Update maximum input current if USB enumeration is not detected */
2219 if (!di->usb.charger_online) {
2220 ret = ab8500_charger_set_vbus_in_curr(di,
2221 di->max_usb_in_curr.usb_type_max);
2226 ab8500_charger_set_usb_connected(di, true);
2227 ab8500_power_supply_changed(di, di->usb_chg.psy);
2231 * ab8500_charger_usb_link_status_work() - work to detect USB type
2232 * @work: pointer to the work_struct structure
2234 * Detect the type of USB plugged
2236 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2238 int detected_chargers;
2243 struct ab8500_charger *di = container_of(work,
2244 struct ab8500_charger, usb_link_status_work);
2247 * Since we can't be sure that the events are received
2248 * synchronously, we have the check if is
2249 * connected by reading the status register
2251 detected_chargers = ab8500_charger_detect_chargers(di, false);
2252 if (detected_chargers < 0)
2256 * Some chargers that breaks the USB spec is
2257 * identified as invalid by AB8500 and it refuse
2258 * to start the charging process. but by jumping
2259 * thru a few hoops it can be forced to start.
2261 if (is_ab8500(di->parent))
2262 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2263 AB8500_USB_LINE_STAT_REG, &val);
2265 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2266 AB8500_USB_LINK1_STAT_REG, &val);
2269 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2271 dev_dbg(di->dev, "Error reading USB link status\n");
2273 if (is_ab8500(di->parent))
2274 link_status = AB8500_USB_LINK_STATUS;
2276 link_status = AB8505_USB_LINK_STATUS;
2278 if (detected_chargers & USB_PW_CONN) {
2279 if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
2280 USB_STAT_NOT_VALID_LINK &&
2281 di->invalid_charger_detect_state == 0) {
2283 "Invalid charger detected, state= 0\n");
2285 abx500_mask_and_set_register_interruptible(di->dev,
2286 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
2287 USB_CH_ENA, USB_CH_ENA);
2288 /*Enable charger detection*/
2289 abx500_mask_and_set_register_interruptible(di->dev,
2290 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2291 USB_CH_DET, USB_CH_DET);
2292 di->invalid_charger_detect_state = 1;
2293 /*exit and wait for new link status interrupt.*/
2297 if (di->invalid_charger_detect_state == 1) {
2299 "Invalid charger detected, state= 1\n");
2300 /*Stop charger detection*/
2301 abx500_mask_and_set_register_interruptible(di->dev,
2302 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2304 /*Check link status*/
2305 if (is_ab8500(di->parent))
2306 ret = abx500_get_register_interruptible(di->dev,
2307 AB8500_USB, AB8500_USB_LINE_STAT_REG,
2310 ret = abx500_get_register_interruptible(di->dev,
2311 AB8500_USB, AB8500_USB_LINK1_STAT_REG,
2314 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2315 (val & link_status) >> USB_LINK_STATUS_SHIFT);
2316 di->invalid_charger_detect_state = 2;
2319 di->invalid_charger_detect_state = 0;
2322 if (!(detected_chargers & USB_PW_CONN)) {
2323 di->vbus_detected = false;
2324 ab8500_charger_set_usb_connected(di, false);
2325 ab8500_power_supply_changed(di, di->usb_chg.psy);
2329 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2330 di->vbus_detected = true;
2331 ret = ab8500_charger_read_usb_type(di);
2333 if (ret == -ENXIO) {
2334 /* No valid charger type detected */
2335 ab8500_charger_set_usb_connected(di, false);
2336 ab8500_power_supply_changed(di, di->usb_chg.psy);
2341 if (di->usb_device_is_unrecognised) {
2343 "Potential Legacy Charger device. "
2344 "Delay work for %d msec for USB enum "
2346 WAIT_ACA_RID_ENUMERATION);
2347 queue_delayed_work(di->charger_wq,
2349 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2350 } else if (di->is_aca_rid == 1) {
2351 /* Only wait once */
2354 "%s Wait %d msec for USB enum to finish",
2355 __func__, WAIT_ACA_RID_ENUMERATION);
2356 queue_delayed_work(di->charger_wq,
2358 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2360 queue_delayed_work(di->charger_wq,
2366 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2369 unsigned long flags;
2371 struct ab8500_charger *di = container_of(work,
2372 struct ab8500_charger, usb_state_changed_work.work);
2374 if (!di->vbus_detected) {
2376 "%s !di->vbus_detected\n",
2381 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2382 di->usb_state.state = di->usb_state.state_tmp;
2383 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
2384 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2386 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2387 __func__, di->usb_state.state, di->usb_state.usb_current);
2389 switch (di->usb_state.state) {
2390 case AB8500_BM_USB_STATE_RESET_HS:
2391 case AB8500_BM_USB_STATE_RESET_FS:
2392 case AB8500_BM_USB_STATE_SUSPEND:
2393 case AB8500_BM_USB_STATE_MAX:
2394 ab8500_charger_set_usb_connected(di, false);
2395 ab8500_power_supply_changed(di, di->usb_chg.psy);
2398 case AB8500_BM_USB_STATE_RESUME:
2400 * when suspend->resume there should be delay
2401 * of 1sec for enabling charging
2404 /* Intentional fall through */
2405 case AB8500_BM_USB_STATE_CONFIGURED:
2407 * USB is configured, enable charging with the charging
2408 * input current obtained from USB driver
2410 if (!ab8500_charger_get_usb_cur(di)) {
2411 /* Update maximum input current */
2412 ret = ab8500_charger_set_vbus_in_curr(di,
2413 di->max_usb_in_curr.usb_type_max);
2417 ab8500_charger_set_usb_connected(di, true);
2418 ab8500_power_supply_changed(di, di->usb_chg.psy);
2428 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2429 * @work: pointer to the work_struct structure
2431 * Work queue function for checking the USB charger Not OK status
2433 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2439 struct ab8500_charger *di = container_of(work,
2440 struct ab8500_charger, check_usbchgnotok_work.work);
2442 /* Check if the status bit for usbchargernotok is still active */
2443 ret = abx500_get_register_interruptible(di->dev,
2444 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2446 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2449 prev_status = di->flags.usbchargernotok;
2451 if (reg_value & VBUS_CH_NOK) {
2452 di->flags.usbchargernotok = true;
2453 /* Check again in 1sec */
2454 queue_delayed_work(di->charger_wq,
2455 &di->check_usbchgnotok_work, HZ);
2457 di->flags.usbchargernotok = false;
2458 di->flags.vbus_collapse = false;
2461 if (prev_status != di->flags.usbchargernotok)
2462 ab8500_power_supply_changed(di, di->usb_chg.psy);
2466 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2467 * @work: pointer to the work_struct structure
2469 * Work queue function for checking the Main thermal prot status
2471 static void ab8500_charger_check_main_thermal_prot_work(
2472 struct work_struct *work)
2477 struct ab8500_charger *di = container_of(work,
2478 struct ab8500_charger, check_main_thermal_prot_work);
2480 /* Check if the status bit for main_thermal_prot is still active */
2481 ret = abx500_get_register_interruptible(di->dev,
2482 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2484 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2487 if (reg_value & MAIN_CH_TH_PROT)
2488 di->flags.main_thermal_prot = true;
2490 di->flags.main_thermal_prot = false;
2492 ab8500_power_supply_changed(di, di->ac_chg.psy);
2496 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2497 * @work: pointer to the work_struct structure
2499 * Work queue function for checking the USB thermal prot status
2501 static void ab8500_charger_check_usb_thermal_prot_work(
2502 struct work_struct *work)
2507 struct ab8500_charger *di = container_of(work,
2508 struct ab8500_charger, check_usb_thermal_prot_work);
2510 /* Check if the status bit for usb_thermal_prot is still active */
2511 ret = abx500_get_register_interruptible(di->dev,
2512 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2514 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2517 if (reg_value & USB_CH_TH_PROT)
2518 di->flags.usb_thermal_prot = true;
2520 di->flags.usb_thermal_prot = false;
2522 ab8500_power_supply_changed(di, di->usb_chg.psy);
2526 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2527 * @irq: interrupt number
2528 * @_di: pointer to the ab8500_charger structure
2530 * Returns IRQ status(IRQ_HANDLED)
2532 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2534 struct ab8500_charger *di = _di;
2536 dev_dbg(di->dev, "Main charger unplugged\n");
2537 queue_work(di->charger_wq, &di->ac_work);
2539 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2540 mutex_lock(&di->charger_attached_mutex);
2541 mutex_unlock(&di->charger_attached_mutex);
2547 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2548 * @irq: interrupt number
2549 * @_di: pointer to the ab8500_charger structure
2551 * Returns IRQ status(IRQ_HANDLED)
2553 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2555 struct ab8500_charger *di = _di;
2557 dev_dbg(di->dev, "Main charger plugged\n");
2558 queue_work(di->charger_wq, &di->ac_work);
2560 mutex_lock(&di->charger_attached_mutex);
2561 mutex_unlock(&di->charger_attached_mutex);
2563 if (is_ab8500(di->parent))
2564 queue_delayed_work(di->charger_wq,
2565 &di->ac_charger_attached_work,
2571 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2572 * @irq: interrupt number
2573 * @_di: pointer to the ab8500_charger structure
2575 * Returns IRQ status(IRQ_HANDLED)
2577 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2579 struct ab8500_charger *di = _di;
2581 dev_dbg(di->dev, "Main charger not ok\n");
2582 di->flags.mainextchnotok = true;
2583 ab8500_power_supply_changed(di, di->ac_chg.psy);
2585 /* Schedule a new HW failure check */
2586 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2592 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2593 * thermal protection threshold
2594 * @irq: interrupt number
2595 * @_di: pointer to the ab8500_charger structure
2597 * Returns IRQ status(IRQ_HANDLED)
2599 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2601 struct ab8500_charger *di = _di;
2604 "Die temp above Main charger thermal protection threshold\n");
2605 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2611 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2612 * thermal protection threshold
2613 * @irq: interrupt number
2614 * @_di: pointer to the ab8500_charger structure
2616 * Returns IRQ status(IRQ_HANDLED)
2618 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2620 struct ab8500_charger *di = _di;
2623 "Die temp ok for Main charger thermal protection threshold\n");
2624 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2629 static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2631 struct ab8500_charger *di = container_of(work,
2632 struct ab8500_charger, vbus_drop_end_work.work);
2636 di->flags.vbus_drop_end = false;
2638 /* Reset the drop counter */
2639 abx500_set_register_interruptible(di->dev,
2640 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
2642 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2643 AB8500_CH_USBCH_STAT2_REG, ®_value);
2645 dev_err(di->dev, "%s read failed\n", __func__);
2649 curr = di->bm->chg_input_curr[
2650 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
2652 if (di->max_usb_in_curr.calculated_max != curr) {
2653 /* USB source is collapsing */
2654 di->max_usb_in_curr.calculated_max = curr;
2656 "VBUS input current limiting to %d mA\n",
2657 di->max_usb_in_curr.calculated_max);
2660 * USB source can not give more than this amount.
2661 * Taking more will collapse the source.
2663 di->max_usb_in_curr.set_max =
2664 di->max_usb_in_curr.calculated_max;
2666 "VBUS input current limited to %d mA\n",
2667 di->max_usb_in_curr.set_max);
2670 if (di->usb.charger_connected)
2671 ab8500_charger_set_vbus_in_curr(di,
2672 di->max_usb_in_curr.usb_type_max);
2676 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2677 * @irq: interrupt number
2678 * @_di: pointer to the ab8500_charger structure
2680 * Returns IRQ status(IRQ_HANDLED)
2682 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2684 struct ab8500_charger *di = _di;
2686 di->vbus_detected = false;
2687 dev_dbg(di->dev, "VBUS falling detected\n");
2688 queue_work(di->charger_wq, &di->detect_usb_type_work);
2694 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2695 * @irq: interrupt number
2696 * @_di: pointer to the ab8500_charger structure
2698 * Returns IRQ status(IRQ_HANDLED)
2700 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2702 struct ab8500_charger *di = _di;
2704 di->vbus_detected = true;
2705 dev_dbg(di->dev, "VBUS rising detected\n");
2707 queue_work(di->charger_wq, &di->detect_usb_type_work);
2713 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2714 * @irq: interrupt number
2715 * @_di: pointer to the ab8500_charger structure
2717 * Returns IRQ status(IRQ_HANDLED)
2719 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2721 struct ab8500_charger *di = _di;
2723 dev_dbg(di->dev, "USB link status changed\n");
2725 queue_work(di->charger_wq, &di->usb_link_status_work);
2731 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2732 * thermal protection threshold
2733 * @irq: interrupt number
2734 * @_di: pointer to the ab8500_charger structure
2736 * Returns IRQ status(IRQ_HANDLED)
2738 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2740 struct ab8500_charger *di = _di;
2743 "Die temp above USB charger thermal protection threshold\n");
2744 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2750 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2751 * thermal protection threshold
2752 * @irq: interrupt number
2753 * @_di: pointer to the ab8500_charger structure
2755 * Returns IRQ status(IRQ_HANDLED)
2757 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2759 struct ab8500_charger *di = _di;
2762 "Die temp ok for USB charger thermal protection threshold\n");
2763 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2769 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2770 * @irq: interrupt number
2771 * @_di: pointer to the ab8500_charger structure
2773 * Returns IRQ status(IRQ_HANDLED)
2775 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2777 struct ab8500_charger *di = _di;
2779 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2780 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2786 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2787 * @irq: interrupt number
2788 * @_di: pointer to the ab8500_charger structure
2790 * Returns IRQ status(IRQ_HANDLED)
2792 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2794 struct ab8500_charger *di = _di;
2796 dev_dbg(di->dev, "Charger watchdog expired\n");
2799 * The charger that was online when the watchdog expired
2800 * needs to be restarted for charging to start again
2802 if (di->ac.charger_online) {
2803 di->ac.wd_expired = true;
2804 ab8500_power_supply_changed(di, di->ac_chg.psy);
2806 if (di->usb.charger_online) {
2807 di->usb.wd_expired = true;
2808 ab8500_power_supply_changed(di, di->usb_chg.psy);
2815 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2816 * @irq: interrupt number
2817 * @_di: pointer to the ab8500_charger structure
2819 * Returns IRQ status(IRQ_HANDLED)
2821 static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2823 struct ab8500_charger *di = _di;
2825 dev_dbg(di->dev, "VBUS charger drop ended\n");
2826 di->flags.vbus_drop_end = true;
2829 * VBUS might have dropped due to bad connection.
2830 * Schedule a new input limit set to the value SW requests.
2832 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
2833 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
2839 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2840 * @irq: interrupt number
2841 * @_di: pointer to the ab8500_charger structure
2843 * Returns IRQ status(IRQ_HANDLED)
2845 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2847 struct ab8500_charger *di = _di;
2849 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2850 di->flags.vbus_ovv = true;
2851 ab8500_power_supply_changed(di, di->usb_chg.psy);
2853 /* Schedule a new HW failure check */
2854 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2860 * ab8500_charger_ac_get_property() - get the ac/mains properties
2861 * @psy: pointer to the power_supply structure
2862 * @psp: pointer to the power_supply_property structure
2863 * @val: pointer to the power_supply_propval union
2865 * This function gets called when an application tries to get the ac/mains
2866 * properties by reading the sysfs files.
2867 * AC/Mains properties are online, present and voltage.
2868 * online: ac/mains charging is in progress or not
2869 * present: presence of the ac/mains
2870 * voltage: AC/Mains voltage
2871 * Returns error code in case of failure else 0(on success)
2873 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2874 enum power_supply_property psp,
2875 union power_supply_propval *val)
2877 struct ab8500_charger *di;
2880 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2883 case POWER_SUPPLY_PROP_HEALTH:
2884 if (di->flags.mainextchnotok)
2885 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2886 else if (di->ac.wd_expired || di->usb.wd_expired)
2887 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2888 else if (di->flags.main_thermal_prot)
2889 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2891 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2893 case POWER_SUPPLY_PROP_ONLINE:
2894 val->intval = di->ac.charger_online;
2896 case POWER_SUPPLY_PROP_PRESENT:
2897 val->intval = di->ac.charger_connected;
2899 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2900 ret = ab8500_charger_get_ac_voltage(di);
2902 di->ac.charger_voltage = ret;
2903 /* On error, use previous value */
2904 val->intval = di->ac.charger_voltage * 1000;
2906 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2908 * This property is used to indicate when CV mode is entered
2909 * for the AC charger
2911 di->ac.cv_active = ab8500_charger_ac_cv(di);
2912 val->intval = di->ac.cv_active;
2914 case POWER_SUPPLY_PROP_CURRENT_NOW:
2915 ret = ab8500_charger_get_ac_current(di);
2917 di->ac.charger_current = ret;
2918 val->intval = di->ac.charger_current * 1000;
2927 * ab8500_charger_usb_get_property() - get the usb properties
2928 * @psy: pointer to the power_supply structure
2929 * @psp: pointer to the power_supply_property structure
2930 * @val: pointer to the power_supply_propval union
2932 * This function gets called when an application tries to get the usb
2933 * properties by reading the sysfs files.
2934 * USB properties are online, present and voltage.
2935 * online: usb charging is in progress or not
2936 * present: presence of the usb
2937 * voltage: vbus voltage
2938 * Returns error code in case of failure else 0(on success)
2940 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2941 enum power_supply_property psp,
2942 union power_supply_propval *val)
2944 struct ab8500_charger *di;
2947 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2950 case POWER_SUPPLY_PROP_HEALTH:
2951 if (di->flags.usbchargernotok)
2952 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2953 else if (di->ac.wd_expired || di->usb.wd_expired)
2954 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2955 else if (di->flags.usb_thermal_prot)
2956 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2957 else if (di->flags.vbus_ovv)
2958 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2960 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2962 case POWER_SUPPLY_PROP_ONLINE:
2963 val->intval = di->usb.charger_online;
2965 case POWER_SUPPLY_PROP_PRESENT:
2966 val->intval = di->usb.charger_connected;
2968 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2969 ret = ab8500_charger_get_vbus_voltage(di);
2971 di->usb.charger_voltage = ret;
2972 val->intval = di->usb.charger_voltage * 1000;
2974 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2976 * This property is used to indicate when CV mode is entered
2977 * for the USB charger
2979 di->usb.cv_active = ab8500_charger_usb_cv(di);
2980 val->intval = di->usb.cv_active;
2982 case POWER_SUPPLY_PROP_CURRENT_NOW:
2983 ret = ab8500_charger_get_usb_current(di);
2985 di->usb.charger_current = ret;
2986 val->intval = di->usb.charger_current * 1000;
2988 case POWER_SUPPLY_PROP_CURRENT_AVG:
2990 * This property is used to indicate when VBUS has collapsed
2991 * due to too high output current from the USB charger
2993 if (di->flags.vbus_collapse)
3005 * ab8500_charger_init_hw_registers() - Set up charger related registers
3006 * @di: pointer to the ab8500_charger structure
3008 * Set up charger OVV, watchdog and maximum voltage registers as well as
3009 * charging of the backup battery
3011 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3014 u8 bup_vch_range = 0, vbup33_vrtcn = 0;
3016 /* Setup maximum charger current and voltage for ABB cut2.0 */
3017 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3018 ret = abx500_set_register_interruptible(di->dev,
3020 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3023 "failed to set CH_VOLT_LVL_MAX_REG\n");
3027 ret = abx500_set_register_interruptible(di->dev,
3028 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3032 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3037 if (is_ab8505_2p0(di->parent))
3038 ret = abx500_mask_and_set_register_interruptible(di->dev,
3040 AB8500_USBCH_CTRL2_REG,
3041 VBUS_AUTO_IN_CURR_LIM_ENA,
3042 VBUS_AUTO_IN_CURR_LIM_ENA);
3045 * VBUS OVV set to 6.3V and enable automatic current limitation
3047 ret = abx500_set_register_interruptible(di->dev,
3049 AB8500_USBCH_CTRL2_REG,
3050 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
3053 "failed to set automatic current limitation\n");
3057 /* Enable main watchdog in OTP */
3058 ret = abx500_set_register_interruptible(di->dev,
3059 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3061 dev_err(di->dev, "failed to enable main WD in OTP\n");
3065 /* Enable main watchdog */
3066 ret = abx500_set_register_interruptible(di->dev,
3067 AB8500_SYS_CTRL2_BLOCK,
3068 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3070 dev_err(di->dev, "failed to enable main watchdog\n");
3075 * Due to internal synchronisation, Enable and Kick watchdog bits
3076 * cannot be enabled in a single write.
3077 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3078 * between writing Enable then Kick bits.
3082 /* Kick main watchdog */
3083 ret = abx500_set_register_interruptible(di->dev,
3084 AB8500_SYS_CTRL2_BLOCK,
3085 AB8500_MAIN_WDOG_CTRL_REG,
3086 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3088 dev_err(di->dev, "failed to kick main watchdog\n");
3092 /* Disable main watchdog */
3093 ret = abx500_set_register_interruptible(di->dev,
3094 AB8500_SYS_CTRL2_BLOCK,
3095 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3097 dev_err(di->dev, "failed to disable main watchdog\n");
3101 /* Set watchdog timeout */
3102 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3103 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3105 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3109 ret = ab8500_charger_led_en(di, false);
3111 dev_err(di->dev, "failed to disable LED\n");
3115 /* Backup battery voltage and current */
3116 if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V)
3117 bup_vch_range = BUP_VCH_RANGE;
3118 if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V)
3119 vbup33_vrtcn = VBUP33_VRTCN;
3121 ret = abx500_set_register_interruptible(di->dev,
3123 AB8500_RTC_BACKUP_CHG_REG,
3124 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
3126 dev_err(di->dev, "failed to setup backup battery charging\n");
3130 /* Enable backup battery charging */
3131 ret = abx500_mask_and_set_register_interruptible(di->dev,
3132 AB8500_RTC, AB8500_RTC_CTRL_REG,
3133 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3135 dev_err(di->dev, "%s mask and set failed\n", __func__);
3144 * ab8500 charger driver interrupts and their respective isr
3146 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3147 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3148 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3149 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3150 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3151 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3152 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3153 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3154 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3155 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3156 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3157 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3158 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3159 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
3160 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
3163 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3164 unsigned long event, void *power)
3166 struct ab8500_charger *di =
3167 container_of(nb, struct ab8500_charger, nb);
3168 enum ab8500_usb_state bm_usb_state;
3169 unsigned mA = *((unsigned *)power);
3174 if (event != USB_EVENT_VBUS) {
3175 dev_dbg(di->dev, "not a standard host, returning\n");
3179 /* TODO: State is fabricate here. See if charger really needs USB
3180 * state or if mA is enough
3182 if ((di->usb_state.usb_current == 2) && (mA > 2))
3183 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3185 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3187 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3188 else if (mA >= 8) /* 8, 100, 500 */
3189 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3190 else /* Should never occur */
3191 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3193 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3194 __func__, bm_usb_state, mA);
3196 spin_lock(&di->usb_state.usb_lock);
3197 di->usb_state.state_tmp = bm_usb_state;
3198 di->usb_state.usb_current_tmp = mA;
3199 spin_unlock(&di->usb_state.usb_lock);
3202 * wait for some time until you get updates from the usb stack
3203 * and negotiations are completed
3205 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
3210 #if defined(CONFIG_PM)
3211 static int ab8500_charger_resume(struct platform_device *pdev)
3214 struct ab8500_charger *di = platform_get_drvdata(pdev);
3217 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3218 * logic. That means we have to continously kick the charger
3219 * watchdog even when no charger is connected. This is only
3220 * valid once the AC charger has been enabled. This is
3221 * a bug that is not handled by the algorithm and the
3222 * watchdog have to be kicked by the charger driver
3223 * when the AC charger is disabled
3225 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3226 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3227 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3229 dev_err(di->dev, "Failed to kick WD!\n");
3231 /* If not already pending start a new timer */
3232 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3233 round_jiffies(WD_KICK_INTERVAL));
3236 /* If we still have a HW failure, schedule a new check */
3237 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3238 queue_delayed_work(di->charger_wq,
3239 &di->check_hw_failure_work, 0);
3242 if (di->flags.vbus_drop_end)
3243 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3248 static int ab8500_charger_suspend(struct platform_device *pdev,
3251 struct ab8500_charger *di = platform_get_drvdata(pdev);
3253 /* Cancel any pending jobs */
3254 cancel_delayed_work(&di->check_hw_failure_work);
3255 cancel_delayed_work(&di->vbus_drop_end_work);
3257 flush_delayed_work(&di->attach_work);
3258 flush_delayed_work(&di->usb_charger_attached_work);
3259 flush_delayed_work(&di->ac_charger_attached_work);
3260 flush_delayed_work(&di->check_usbchgnotok_work);
3261 flush_delayed_work(&di->check_vbat_work);
3262 flush_delayed_work(&di->kick_wd_work);
3264 flush_work(&di->usb_link_status_work);
3265 flush_work(&di->ac_work);
3266 flush_work(&di->detect_usb_type_work);
3268 if (atomic_read(&di->current_stepping_sessions))
3274 #define ab8500_charger_suspend NULL
3275 #define ab8500_charger_resume NULL
3278 static struct notifier_block charger_nb = {
3279 .notifier_call = ab8500_external_charger_prepare,
3282 static int ab8500_charger_remove(struct platform_device *pdev)
3284 struct ab8500_charger *di = platform_get_drvdata(pdev);
3287 /* Disable AC charging */
3288 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3290 /* Disable USB charging */
3291 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3293 /* Disable interrupts */
3294 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3295 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3299 /* Backup battery voltage and current disable */
3300 ret = abx500_mask_and_set_register_interruptible(di->dev,
3301 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3303 dev_err(di->dev, "%s mask and set failed\n", __func__);
3305 usb_unregister_notifier(di->usb_phy, &di->nb);
3306 usb_put_phy(di->usb_phy);
3308 /* Delete the work queue */
3309 destroy_workqueue(di->charger_wq);
3311 /* Unregister external charger enable notifier */
3312 if (!di->ac_chg.enabled)
3313 blocking_notifier_chain_unregister(
3314 &charger_notifier_list, &charger_nb);
3316 flush_scheduled_work();
3317 if (di->usb_chg.enabled)
3318 power_supply_unregister(di->usb_chg.psy);
3320 if (di->ac_chg.enabled && !di->ac_chg.external)
3321 power_supply_unregister(di->ac_chg.psy);
3326 static char *supply_interface[] = {
3332 static const struct power_supply_desc ab8500_ac_chg_desc = {
3333 .name = "ab8500_ac",
3334 .type = POWER_SUPPLY_TYPE_MAINS,
3335 .properties = ab8500_charger_ac_props,
3336 .num_properties = ARRAY_SIZE(ab8500_charger_ac_props),
3337 .get_property = ab8500_charger_ac_get_property,
3340 static const struct power_supply_desc ab8500_usb_chg_desc = {
3341 .name = "ab8500_usb",
3342 .type = POWER_SUPPLY_TYPE_USB,
3343 .properties = ab8500_charger_usb_props,
3344 .num_properties = ARRAY_SIZE(ab8500_charger_usb_props),
3345 .get_property = ab8500_charger_usb_get_property,
3348 static int ab8500_charger_probe(struct platform_device *pdev)
3350 struct device_node *np = pdev->dev.of_node;
3351 struct abx500_bm_data *plat = pdev->dev.platform_data;
3352 struct power_supply_config ac_psy_cfg = {}, usb_psy_cfg = {};
3353 struct ab8500_charger *di;
3354 int irq, i, charger_status, ret = 0, ch_stat;
3356 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3358 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
3363 dev_err(&pdev->dev, "no battery management data supplied\n");
3369 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3371 dev_err(&pdev->dev, "failed to get battery information\n");
3374 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3376 di->autopower_cfg = false;
3378 /* get parent data */
3379 di->dev = &pdev->dev;
3380 di->parent = dev_get_drvdata(pdev->dev.parent);
3381 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3383 /* initialize lock */
3384 spin_lock_init(&di->usb_state.usb_lock);
3385 mutex_init(&di->usb_ipt_crnt_lock);
3387 di->autopower = false;
3388 di->invalid_charger_detect_state = 0;
3390 /* AC and USB supply config */
3391 ac_psy_cfg.supplied_to = supply_interface;
3392 ac_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3393 ac_psy_cfg.drv_data = &di->ac_chg;
3394 usb_psy_cfg.supplied_to = supply_interface;
3395 usb_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3396 usb_psy_cfg.drv_data = &di->usb_chg;
3399 /* ux500_charger sub-class */
3400 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
3401 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
3402 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3403 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3404 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3405 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3406 di->ac_chg.max_out_curr =
3407 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3408 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
3409 di->ac_chg.enabled = di->bm->ac_enabled;
3410 di->ac_chg.external = false;
3412 /*notifier for external charger enabling*/
3413 if (!di->ac_chg.enabled)
3414 blocking_notifier_chain_register(
3415 &charger_notifier_list, &charger_nb);
3418 /* ux500_charger sub-class */
3419 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
3420 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
3421 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3422 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3423 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3424 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3425 di->usb_chg.max_out_curr =
3426 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3427 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
3428 di->usb_chg.enabled = di->bm->usb_enabled;
3429 di->usb_chg.external = false;
3430 di->usb_state.usb_current = -1;
3432 /* Create a work queue for the charger */
3433 di->charger_wq = alloc_ordered_workqueue("ab8500_charger_wq",
3435 if (di->charger_wq == NULL) {
3436 dev_err(di->dev, "failed to create work queue\n");
3440 mutex_init(&di->charger_attached_mutex);
3442 /* Init work for HW failure check */
3443 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
3444 ab8500_charger_check_hw_failure_work);
3445 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
3446 ab8500_charger_check_usbchargernotok_work);
3448 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3449 ab8500_charger_ac_attached_work);
3450 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3451 ab8500_charger_usb_attached_work);
3454 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3455 * logic. That means we have to continously kick the charger
3456 * watchdog even when no charger is connected. This is only
3457 * valid once the AC charger has been enabled. This is
3458 * a bug that is not handled by the algorithm and the
3459 * watchdog have to be kicked by the charger driver
3460 * when the AC charger is disabled
3462 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
3463 ab8500_charger_kick_watchdog_work);
3465 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
3466 ab8500_charger_check_vbat_work);
3468 INIT_DELAYED_WORK(&di->attach_work,
3469 ab8500_charger_usb_link_attach_work);
3471 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3472 ab8500_charger_usb_state_changed_work);
3474 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3475 ab8500_charger_vbus_drop_end_work);
3477 /* Init work for charger detection */
3478 INIT_WORK(&di->usb_link_status_work,
3479 ab8500_charger_usb_link_status_work);
3480 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3481 INIT_WORK(&di->detect_usb_type_work,
3482 ab8500_charger_detect_usb_type_work);
3484 /* Init work for checking HW status */
3485 INIT_WORK(&di->check_main_thermal_prot_work,
3486 ab8500_charger_check_main_thermal_prot_work);
3487 INIT_WORK(&di->check_usb_thermal_prot_work,
3488 ab8500_charger_check_usb_thermal_prot_work);
3491 * VDD ADC supply needs to be enabled from this driver when there
3492 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3493 * interrupts during charging
3495 di->regu = devm_regulator_get(di->dev, "vddadc");
3496 if (IS_ERR(di->regu)) {
3497 ret = PTR_ERR(di->regu);
3498 dev_err(di->dev, "failed to get vddadc regulator\n");
3499 goto free_charger_wq;
3503 /* Initialize OVV, and other registers */
3504 ret = ab8500_charger_init_hw_registers(di);
3506 dev_err(di->dev, "failed to initialize ABB registers\n");
3507 goto free_charger_wq;
3510 /* Register AC charger class */
3511 if (di->ac_chg.enabled) {
3512 di->ac_chg.psy = power_supply_register(di->dev,
3513 &ab8500_ac_chg_desc,
3515 if (IS_ERR(di->ac_chg.psy)) {
3516 dev_err(di->dev, "failed to register AC charger\n");
3517 ret = PTR_ERR(di->ac_chg.psy);
3518 goto free_charger_wq;
3522 /* Register USB charger class */
3523 if (di->usb_chg.enabled) {
3524 di->usb_chg.psy = power_supply_register(di->dev,
3525 &ab8500_usb_chg_desc,
3527 if (IS_ERR(di->usb_chg.psy)) {
3528 dev_err(di->dev, "failed to register USB charger\n");
3529 ret = PTR_ERR(di->usb_chg.psy);
3534 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3535 if (IS_ERR_OR_NULL(di->usb_phy)) {
3536 dev_err(di->dev, "failed to get usb transceiver\n");
3540 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3541 ret = usb_register_notifier(di->usb_phy, &di->nb);
3543 dev_err(di->dev, "failed to register usb notifier\n");
3547 /* Identify the connected charger types during startup */
3548 charger_status = ab8500_charger_detect_chargers(di, true);
3549 if (charger_status & AC_PW_CONN) {
3550 di->ac.charger_connected = 1;
3552 ab8500_power_supply_changed(di, di->ac_chg.psy);
3553 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
3556 if (charger_status & USB_PW_CONN) {
3557 di->vbus_detected = true;
3558 di->vbus_detected_start = true;
3559 queue_work(di->charger_wq,
3560 &di->detect_usb_type_work);
3563 /* Register interrupts */
3564 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3565 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3566 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3567 IRQF_SHARED | IRQF_NO_SUSPEND,
3568 ab8500_charger_irq[i].name, di);
3571 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3572 , ab8500_charger_irq[i].name, irq, ret);
3575 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3576 ab8500_charger_irq[i].name, irq, ret);
3579 platform_set_drvdata(pdev, di);
3581 mutex_lock(&di->charger_attached_mutex);
3583 ch_stat = ab8500_charger_detect_chargers(di, false);
3585 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3586 if (is_ab8500(di->parent))
3587 queue_delayed_work(di->charger_wq,
3588 &di->ac_charger_attached_work,
3591 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3592 if (is_ab8500(di->parent))
3593 queue_delayed_work(di->charger_wq,
3594 &di->usb_charger_attached_work,
3598 mutex_unlock(&di->charger_attached_mutex);
3603 usb_unregister_notifier(di->usb_phy, &di->nb);
3605 /* We also have to free all successfully registered irqs */
3606 for (i = i - 1; i >= 0; i--) {
3607 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3611 usb_put_phy(di->usb_phy);
3613 if (di->usb_chg.enabled)
3614 power_supply_unregister(di->usb_chg.psy);
3616 if (di->ac_chg.enabled)
3617 power_supply_unregister(di->ac_chg.psy);
3619 destroy_workqueue(di->charger_wq);
3623 static const struct of_device_id ab8500_charger_match[] = {
3624 { .compatible = "stericsson,ab8500-charger", },
3628 static struct platform_driver ab8500_charger_driver = {
3629 .probe = ab8500_charger_probe,
3630 .remove = ab8500_charger_remove,
3631 .suspend = ab8500_charger_suspend,
3632 .resume = ab8500_charger_resume,
3634 .name = "ab8500-charger",
3635 .of_match_table = ab8500_charger_match,
3639 static int __init ab8500_charger_init(void)
3641 return platform_driver_register(&ab8500_charger_driver);
3644 static void __exit ab8500_charger_exit(void)
3646 platform_driver_unregister(&ab8500_charger_driver);
3649 subsys_initcall_sync(ab8500_charger_init);
3650 module_exit(ab8500_charger_exit);
3652 MODULE_LICENSE("GPL v2");
3653 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3654 MODULE_ALIAS("platform:ab8500-charger");
3655 MODULE_DESCRIPTION("AB8500 charger management driver");