2 * Copyright (C) ST-Ericsson SA 2012
4 * Charger driver for AB8500
6 * License Terms: GNU General Public License v2
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/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 case USB_STAT_HM_IDGND:
746 dev_err(di->dev, "USB Type - Charging not allowed\n");
747 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
750 case USB_STAT_RESERVED:
751 if (is_ab8500(di->parent)) {
752 di->flags.vbus_collapse = true;
753 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
754 "VBUS has collapsed\n");
758 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
759 di->max_usb_in_curr.usb_type_max =
760 USB_CH_IP_CUR_LVL_0P05;
761 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
763 di->max_usb_in_curr.usb_type_max);
767 case USB_STAT_CARKIT_1:
768 case USB_STAT_CARKIT_2:
769 case USB_STAT_ACA_DOCK_CHARGER:
770 case USB_STAT_CHARGER_LINE_1:
771 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
772 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
773 di->max_usb_in_curr.usb_type_max);
775 case USB_STAT_NOT_VALID_LINK:
776 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
777 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
781 dev_err(di->dev, "USB Type - Unknown\n");
782 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
787 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
788 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
789 link_status, di->max_usb_in_curr.set_max);
795 * ab8500_charger_read_usb_type() - read the type of usb connected
796 * @di: pointer to the ab8500_charger structure
798 * Detect the type of the plugged USB
799 * Returns error code in case of failure else 0 on success
801 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
806 ret = abx500_get_register_interruptible(di->dev,
807 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
809 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
812 if (is_ab8500(di->parent))
813 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
814 AB8500_USB_LINE_STAT_REG, &val);
816 ret = abx500_get_register_interruptible(di->dev,
817 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
819 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
823 /* get the USB type */
824 if (is_ab8500(di->parent))
825 val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
827 val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
828 ret = ab8500_charger_max_usb_curr(di,
829 (enum ab8500_charger_link_status) val);
835 * ab8500_charger_detect_usb_type() - get the type of usb connected
836 * @di: pointer to the ab8500_charger structure
838 * Detect the type of the plugged USB
839 * Returns error code in case of failure else 0 on success
841 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
847 * On getting the VBUS rising edge detect interrupt there
848 * is a 250ms delay after which the register UsbLineStatus
849 * is filled with valid data.
851 for (i = 0; i < 10; i++) {
853 ret = abx500_get_register_interruptible(di->dev,
854 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
856 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
859 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
863 if (is_ab8500(di->parent))
864 ret = abx500_get_register_interruptible(di->dev,
865 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
867 ret = abx500_get_register_interruptible(di->dev,
868 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
870 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
873 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
876 * Until the IT source register is read the UsbLineStatus
877 * register is not updated, hence doing the same
881 /* get the USB type */
882 if (is_ab8500(di->parent))
883 val = (val & AB8500_USB_LINK_STATUS) >>
884 USB_LINK_STATUS_SHIFT;
886 val = (val & AB8505_USB_LINK_STATUS) >>
887 USB_LINK_STATUS_SHIFT;
891 ret = ab8500_charger_max_usb_curr(di,
892 (enum ab8500_charger_link_status) val);
898 * This array maps the raw hex value to charger voltage used by the AB8500
899 * Values taken from the UM0836
901 static int ab8500_charger_voltage_map[] = {
982 static int ab8500_voltage_to_regval(int voltage)
986 /* Special case for voltage below 3.5V */
987 if (voltage < ab8500_charger_voltage_map[0])
990 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
991 if (voltage < ab8500_charger_voltage_map[i])
995 /* If not last element, return error */
996 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
997 if (voltage == ab8500_charger_voltage_map[i])
1003 static int ab8500_current_to_regval(struct ab8500_charger *di, int curr)
1007 if (curr < di->bm->chg_output_curr[0])
1010 for (i = 0; i < di->bm->n_chg_out_curr; i++) {
1011 if (curr < di->bm->chg_output_curr[i])
1015 /* If not last element, return error */
1016 i = di->bm->n_chg_out_curr - 1;
1017 if (curr == di->bm->chg_output_curr[i])
1023 static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr)
1027 if (curr < di->bm->chg_input_curr[0])
1030 for (i = 0; i < di->bm->n_chg_in_curr; i++) {
1031 if (curr < di->bm->chg_input_curr[i])
1035 /* If not last element, return error */
1036 i = di->bm->n_chg_in_curr - 1;
1037 if (curr == di->bm->chg_input_curr[i])
1044 * ab8500_charger_get_usb_cur() - get usb current
1045 * @di: pointer to the ab8500_charger structre
1047 * The usb stack provides the maximum current that can be drawn from
1048 * the standard usb host. This will be in mA.
1049 * This function converts current in mA to a value that can be written
1050 * to the register. Returns -1 if charging is not allowed
1052 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1055 switch (di->usb_state.usb_current) {
1057 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
1060 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
1063 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
1066 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
1069 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
1072 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
1076 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
1081 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1082 * @di: pointer to the ab8500_charger structure
1083 * @reg: select what charger register to check
1085 * Check if current stepping should be allowed to continue.
1086 * Checks if charger source has not collapsed. If it has, further stepping
1089 static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1092 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1093 return !di->flags.vbus_drop_end;
1099 * ab8500_charger_set_current() - set charger current
1100 * @di: pointer to the ab8500_charger structure
1101 * @ich: charger current, in mA
1102 * @reg: select what charger register to set
1104 * Set charger current.
1105 * There is no state machine in the AB to step up/down the charger
1106 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1107 * charging is started. Instead we need to implement
1108 * this charger current step-up/down here.
1109 * Returns error code in case of failure else 0(on success)
1111 static int ab8500_charger_set_current(struct ab8500_charger *di,
1115 int curr_index, prev_curr_index, shift_value, i;
1118 bool no_stepping = false;
1120 atomic_inc(&di->current_stepping_sessions);
1122 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1125 dev_err(di->dev, "%s read failed\n", __func__);
1126 goto exit_set_current;
1130 case AB8500_MCH_IPT_CURLVL_REG:
1131 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1132 prev_curr_index = (reg_value >> shift_value);
1133 curr_index = ab8500_current_to_regval(di, ich);
1134 step_udelay = STEP_UDELAY;
1135 if (!di->ac.charger_connected)
1138 case AB8500_USBCH_IPT_CRNTLVL_REG:
1139 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1140 prev_curr_index = (reg_value >> shift_value);
1141 curr_index = ab8500_vbus_in_curr_to_regval(di, ich);
1142 step_udelay = STEP_UDELAY * 100;
1144 if (!di->usb.charger_connected)
1147 case AB8500_CH_OPT_CRNTLVL_REG:
1149 prev_curr_index = (reg_value >> shift_value);
1150 curr_index = ab8500_current_to_regval(di, ich);
1151 step_udelay = STEP_UDELAY;
1152 if (curr_index && (curr_index - prev_curr_index) > 1)
1155 if (!di->usb.charger_connected && !di->ac.charger_connected)
1160 dev_err(di->dev, "%s current register not valid\n", __func__);
1162 goto exit_set_current;
1165 if (curr_index < 0) {
1166 dev_err(di->dev, "requested current limit out-of-range\n");
1168 goto exit_set_current;
1171 /* only update current if it's been changed */
1172 if (prev_curr_index == curr_index) {
1173 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1176 goto exit_set_current;
1179 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1180 __func__, ich, reg);
1183 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1184 reg, (u8)curr_index << shift_value);
1186 dev_err(di->dev, "%s write failed\n", __func__);
1187 } else if (prev_curr_index > curr_index) {
1188 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1189 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1190 (u8) i << shift_value, reg);
1191 ret = abx500_set_register_interruptible(di->dev,
1192 AB8500_CHARGER, reg, (u8)i << shift_value);
1194 dev_err(di->dev, "%s write failed\n", __func__);
1195 goto exit_set_current;
1197 if (i != curr_index)
1198 usleep_range(step_udelay, step_udelay * 2);
1202 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
1203 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1204 (u8)i << shift_value, reg);
1205 ret = abx500_set_register_interruptible(di->dev,
1206 AB8500_CHARGER, reg, (u8)i << shift_value);
1208 dev_err(di->dev, "%s write failed\n", __func__);
1209 goto exit_set_current;
1211 if (i != curr_index)
1212 usleep_range(step_udelay, step_udelay * 2);
1214 allow = ab8500_charger_check_continue_stepping(di, reg);
1219 atomic_dec(&di->current_stepping_sessions);
1225 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1226 * @di: pointer to the ab8500_charger structure
1227 * @ich_in: charger input current limit
1229 * Sets the current that can be drawn from the USB host
1230 * Returns error code in case of failure else 0(on success)
1232 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1238 /* We should always use to lowest current limit */
1239 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1240 if (di->max_usb_in_curr.set_max > 0)
1241 min_value = min(di->max_usb_in_curr.set_max, min_value);
1243 if (di->usb_state.usb_current >= 0)
1244 min_value = min(di->usb_state.usb_current, min_value);
1246 switch (min_value) {
1248 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1249 min_value = USB_CH_IP_CUR_LVL_0P05;
1252 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1253 min_value = USB_CH_IP_CUR_LVL_0P45;
1259 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1261 mutex_lock(&di->usb_ipt_crnt_lock);
1262 ret = ab8500_charger_set_current(di, min_value,
1263 AB8500_USBCH_IPT_CRNTLVL_REG);
1264 mutex_unlock(&di->usb_ipt_crnt_lock);
1270 * ab8500_charger_set_main_in_curr() - set main charger input current
1271 * @di: pointer to the ab8500_charger structure
1272 * @ich_in: input charger current, in mA
1274 * Set main charger input current.
1275 * Returns error code in case of failure else 0(on success)
1277 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1280 return ab8500_charger_set_current(di, ich_in,
1281 AB8500_MCH_IPT_CURLVL_REG);
1285 * ab8500_charger_set_output_curr() - set charger output current
1286 * @di: pointer to the ab8500_charger structure
1287 * @ich_out: output charger current, in mA
1289 * Set charger output current.
1290 * Returns error code in case of failure else 0(on success)
1292 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1295 return ab8500_charger_set_current(di, ich_out,
1296 AB8500_CH_OPT_CRNTLVL_REG);
1300 * ab8500_charger_led_en() - turn on/off chargign led
1301 * @di: pointer to the ab8500_charger structure
1302 * @on: flag to turn on/off the chargign led
1304 * Power ON/OFF charging LED indication
1305 * Returns error code in case of failure else 0(on success)
1307 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1312 /* Power ON charging LED indicator, set LED current to 5mA */
1313 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1314 AB8500_LED_INDICATOR_PWM_CTRL,
1315 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1317 dev_err(di->dev, "Power ON LED failed\n");
1320 /* LED indicator PWM duty cycle 252/256 */
1321 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1322 AB8500_LED_INDICATOR_PWM_DUTY,
1323 LED_INDICATOR_PWM_DUTY_252_256);
1325 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1329 /* Power off charging LED indicator */
1330 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1331 AB8500_LED_INDICATOR_PWM_CTRL,
1332 LED_INDICATOR_PWM_DIS);
1334 dev_err(di->dev, "Power-off LED failed\n");
1343 * ab8500_charger_ac_en() - enable or disable ac charging
1344 * @di: pointer to the ab8500_charger structure
1345 * @enable: enable/disable flag
1346 * @vset: charging voltage
1347 * @iset: charging current
1349 * Enable/Disable AC/Mains charging and turns on/off the charging led
1352 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1353 int enable, int vset, int iset)
1358 int input_curr_index;
1361 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1364 /* Check if AC is connected */
1365 if (!di->ac.charger_connected) {
1366 dev_err(di->dev, "AC charger not connected\n");
1370 /* Enable AC charging */
1371 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1374 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1375 * will be triggered everytime we enable the VDD ADC supply.
1376 * This will turn off charging for a short while.
1377 * It can be avoided by having the supply on when
1378 * there is a charger enabled. Normally the VDD ADC supply
1379 * is enabled everytime a GPADC conversion is triggered. We will
1380 * force it to be enabled from this driver to have
1381 * the GPADC module independant of the AB8500 chargers
1383 if (!di->vddadc_en_ac) {
1384 ret = regulator_enable(di->regu);
1387 "Failed to enable regulator\n");
1389 di->vddadc_en_ac = true;
1392 /* Check if the requested voltage or current is valid */
1393 volt_index = ab8500_voltage_to_regval(vset);
1394 curr_index = ab8500_current_to_regval(di, iset);
1395 input_curr_index = ab8500_current_to_regval(di,
1396 di->bm->chg_params->ac_curr_max);
1397 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1399 "Charger voltage or current too high, "
1400 "charging not started\n");
1404 /* ChVoltLevel: maximum battery charging voltage */
1405 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1406 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1408 dev_err(di->dev, "%s write failed\n", __func__);
1411 /* MainChInputCurr: current that can be drawn from the charger*/
1412 ret = ab8500_charger_set_main_in_curr(di,
1413 di->bm->chg_params->ac_curr_max);
1415 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1419 /* ChOutputCurentLevel: protected output current */
1420 ret = ab8500_charger_set_output_curr(di, iset);
1422 dev_err(di->dev, "%s "
1423 "Failed to set ChOutputCurentLevel\n",
1428 /* Check if VBAT overshoot control should be enabled */
1429 if (!di->bm->enable_overshoot)
1430 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1432 /* Enable Main Charger */
1433 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1434 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1436 dev_err(di->dev, "%s write failed\n", __func__);
1440 /* Power on charging LED indication */
1441 ret = ab8500_charger_led_en(di, true);
1443 dev_err(di->dev, "failed to enable LED\n");
1445 di->ac.charger_online = 1;
1447 /* Disable AC charging */
1448 if (is_ab8500_1p1_or_earlier(di->parent)) {
1450 * For ABB revision 1.0 and 1.1 there is a bug in the
1451 * watchdog logic. That means we have to continously
1452 * kick the charger watchdog even when no charger is
1453 * connected. This is only valid once the AC charger
1454 * has been enabled. This is a bug that is not handled
1455 * by the algorithm and the watchdog have to be kicked
1456 * by the charger driver when the AC charger
1460 queue_delayed_work(di->charger_wq,
1462 round_jiffies(WD_KICK_INTERVAL));
1466 * We can't turn off charging completely
1467 * due to a bug in AB8500 cut1.
1468 * If we do, charging will not start again.
1469 * That is why we set the lowest voltage
1470 * and current possible
1472 ret = abx500_set_register_interruptible(di->dev,
1474 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1477 "%s write failed\n", __func__);
1481 ret = ab8500_charger_set_output_curr(di, 0);
1483 dev_err(di->dev, "%s "
1484 "Failed to set ChOutputCurentLevel\n",
1489 ret = abx500_set_register_interruptible(di->dev,
1491 AB8500_MCH_CTRL1, 0);
1494 "%s write failed\n", __func__);
1499 ret = ab8500_charger_led_en(di, false);
1501 dev_err(di->dev, "failed to disable LED\n");
1503 di->ac.charger_online = 0;
1504 di->ac.wd_expired = false;
1506 /* Disable regulator if enabled */
1507 if (di->vddadc_en_ac) {
1508 regulator_disable(di->regu);
1509 di->vddadc_en_ac = false;
1512 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1514 ab8500_power_supply_changed(di, di->ac_chg.psy);
1520 * ab8500_charger_usb_en() - enable usb charging
1521 * @di: pointer to the ab8500_charger structure
1522 * @enable: enable/disable flag
1523 * @vset: charging voltage
1524 * @ich_out: charger output current
1526 * Enable/Disable USB charging and turns on/off the charging led respectively.
1527 * Returns error code in case of failure else 0(on success)
1529 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1530 int enable, int vset, int ich_out)
1537 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1540 /* Check if USB is connected */
1541 if (!di->usb.charger_connected) {
1542 dev_err(di->dev, "USB charger not connected\n");
1547 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1548 * will be triggered everytime we enable the VDD ADC supply.
1549 * This will turn off charging for a short while.
1550 * It can be avoided by having the supply on when
1551 * there is a charger enabled. Normally the VDD ADC supply
1552 * is enabled everytime a GPADC conversion is triggered. We will
1553 * force it to be enabled from this driver to have
1554 * the GPADC module independant of the AB8500 chargers
1556 if (!di->vddadc_en_usb) {
1557 ret = regulator_enable(di->regu);
1560 "Failed to enable regulator\n");
1562 di->vddadc_en_usb = true;
1565 /* Enable USB charging */
1566 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1568 /* Check if the requested voltage or current is valid */
1569 volt_index = ab8500_voltage_to_regval(vset);
1570 curr_index = ab8500_current_to_regval(di, ich_out);
1571 if (volt_index < 0 || curr_index < 0) {
1573 "Charger voltage or current too high, "
1574 "charging not started\n");
1578 /* ChVoltLevel: max voltage upto which battery can be charged */
1579 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1580 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1582 dev_err(di->dev, "%s write failed\n", __func__);
1585 /* Check if VBAT overshoot control should be enabled */
1586 if (!di->bm->enable_overshoot)
1587 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1589 /* Enable USB Charger */
1591 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
1592 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1593 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1595 dev_err(di->dev, "%s write failed\n", __func__);
1599 /* If success power on charging LED indication */
1600 ret = ab8500_charger_led_en(di, true);
1602 dev_err(di->dev, "failed to enable LED\n");
1604 di->usb.charger_online = 1;
1606 /* USBChInputCurr: current that can be drawn from the usb */
1607 ret = ab8500_charger_set_vbus_in_curr(di,
1608 di->max_usb_in_curr.usb_type_max);
1610 dev_err(di->dev, "setting USBChInputCurr failed\n");
1614 /* ChOutputCurentLevel: protected output current */
1615 ret = ab8500_charger_set_output_curr(di, ich_out);
1617 dev_err(di->dev, "%s "
1618 "Failed to set ChOutputCurentLevel\n",
1623 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1626 /* Disable USB charging */
1627 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1628 ret = abx500_set_register_interruptible(di->dev,
1630 AB8500_USBCH_CTRL1_REG, 0);
1633 "%s write failed\n", __func__);
1637 ret = ab8500_charger_led_en(di, false);
1639 dev_err(di->dev, "failed to disable LED\n");
1640 /* USBChInputCurr: current that can be drawn from the usb */
1641 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1643 dev_err(di->dev, "setting USBChInputCurr failed\n");
1647 /* ChOutputCurentLevel: protected output current */
1648 ret = ab8500_charger_set_output_curr(di, 0);
1650 dev_err(di->dev, "%s "
1651 "Failed to reset ChOutputCurentLevel\n",
1655 di->usb.charger_online = 0;
1656 di->usb.wd_expired = false;
1658 /* Disable regulator if enabled */
1659 if (di->vddadc_en_usb) {
1660 regulator_disable(di->regu);
1661 di->vddadc_en_usb = false;
1664 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1666 /* Cancel any pending Vbat check work */
1667 cancel_delayed_work(&di->check_vbat_work);
1670 ab8500_power_supply_changed(di, di->usb_chg.psy);
1675 static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1676 unsigned long event, void *data)
1679 struct device *dev = data;
1680 /*Toggle External charger control pin*/
1681 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1682 AB8500_SYS_CHARGER_CONTROL_REG,
1683 EXTERNAL_CHARGER_DISABLE_REG_VAL);
1685 dev_err(dev, "write reg failed %d\n", ret);
1688 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1689 AB8500_SYS_CHARGER_CONTROL_REG,
1690 EXTERNAL_CHARGER_ENABLE_REG_VAL);
1692 dev_err(dev, "Write reg failed %d\n", ret);
1699 * ab8500_charger_usb_check_enable() - enable usb charging
1700 * @charger: pointer to the ux500_charger structure
1701 * @vset: charging voltage
1702 * @iset: charger output current
1704 * Check if the VBUS charger has been disconnected and reconnected without
1705 * AB8500 rising an interrupt. Returns 0 on success.
1707 static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1713 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1715 if (!di->usb.charger_connected)
1718 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1719 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1721 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1724 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1726 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1727 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1729 ret = abx500_mask_and_set_register_interruptible(di->dev,
1730 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1731 DROP_COUNT_RESET, DROP_COUNT_RESET);
1733 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1737 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1739 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1748 * ab8500_charger_ac_check_enable() - enable usb charging
1749 * @charger: pointer to the ux500_charger structure
1750 * @vset: charging voltage
1751 * @iset: charger output current
1753 * Check if the AC charger has been disconnected and reconnected without
1754 * AB8500 rising an interrupt. Returns 0 on success.
1756 static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1759 u8 mainch_ctrl1 = 0;
1762 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1764 if (!di->ac.charger_connected)
1767 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1768 AB8500_MCH_CTRL1, &mainch_ctrl1);
1770 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1773 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1775 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1776 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1778 ret = abx500_mask_and_set_register_interruptible(di->dev,
1779 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1780 DROP_COUNT_RESET, DROP_COUNT_RESET);
1783 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1787 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1789 dev_err(di->dev, "failed to enable AC charger %d\n",
1798 * ab8500_charger_watchdog_kick() - kick charger watchdog
1799 * @di: pointer to the ab8500_charger structure
1801 * Kick charger watchdog
1802 * Returns error code in case of failure else 0(on success)
1804 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1807 struct ab8500_charger *di;
1809 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1810 di = to_ab8500_charger_ac_device_info(charger);
1811 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1812 di = to_ab8500_charger_usb_device_info(charger);
1816 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1817 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1819 dev_err(di->dev, "Failed to kick WD!\n");
1825 * ab8500_charger_update_charger_current() - update charger current
1826 * @di: pointer to the ab8500_charger structure
1828 * Update the charger output current for the specified charger
1829 * Returns error code in case of failure else 0(on success)
1831 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1835 struct ab8500_charger *di;
1837 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1838 di = to_ab8500_charger_ac_device_info(charger);
1839 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1840 di = to_ab8500_charger_usb_device_info(charger);
1844 ret = ab8500_charger_set_output_curr(di, ich_out);
1846 dev_err(di->dev, "%s "
1847 "Failed to set ChOutputCurentLevel\n",
1852 /* Reset the main and usb drop input current measurement counter */
1853 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1854 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
1856 dev_err(di->dev, "%s write failed\n", __func__);
1863 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1865 struct power_supply *psy;
1866 struct power_supply *ext = dev_get_drvdata(dev);
1867 const char **supplicants = (const char **)ext->supplied_to;
1868 struct ab8500_charger *di;
1869 union power_supply_propval ret;
1871 struct ux500_charger *usb_chg;
1873 usb_chg = (struct ux500_charger *)data;
1876 di = to_ab8500_charger_usb_device_info(usb_chg);
1878 /* For all psy where the driver name appears in any supplied_to */
1879 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
1883 /* Go through all properties for the psy */
1884 for (j = 0; j < ext->desc->num_properties; j++) {
1885 enum power_supply_property prop;
1886 prop = ext->desc->properties[j];
1888 if (power_supply_get_property(ext, prop, &ret))
1892 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1893 switch (ext->desc->type) {
1894 case POWER_SUPPLY_TYPE_BATTERY:
1895 di->vbat = ret.intval / 1000;
1909 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1910 * @work pointer to the work_struct structure
1912 * Due to a asic bug it is necessary to lower the input current to the vbus
1913 * charger when charging with at some specific levels. This issue is only valid
1914 * for below a certain battery voltage. This function makes sure that the
1915 * the allowed current limit isn't exceeded.
1917 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1920 struct ab8500_charger *di = container_of(work,
1921 struct ab8500_charger, check_vbat_work.work);
1923 class_for_each_device(power_supply_class, NULL,
1924 di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1926 /* First run old_vbat is 0. */
1927 if (di->old_vbat == 0)
1928 di->old_vbat = di->vbat;
1930 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1931 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1932 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1933 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1935 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1936 " old: %d\n", di->max_usb_in_curr.usb_type_max,
1937 di->vbat, di->old_vbat);
1938 ab8500_charger_set_vbus_in_curr(di,
1939 di->max_usb_in_curr.usb_type_max);
1940 power_supply_changed(di->usb_chg.psy);
1943 di->old_vbat = di->vbat;
1946 * No need to check the battery voltage every second when not close to
1949 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1950 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1953 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1957 * ab8500_charger_check_hw_failure_work() - check main charger failure
1958 * @work: pointer to the work_struct structure
1960 * Work queue function for checking the main charger status
1962 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1967 struct ab8500_charger *di = container_of(work,
1968 struct ab8500_charger, check_hw_failure_work.work);
1970 /* Check if the status bits for HW failure is still active */
1971 if (di->flags.mainextchnotok) {
1972 ret = abx500_get_register_interruptible(di->dev,
1973 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1975 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1978 if (!(reg_value & MAIN_CH_NOK)) {
1979 di->flags.mainextchnotok = false;
1980 ab8500_power_supply_changed(di, di->ac_chg.psy);
1983 if (di->flags.vbus_ovv) {
1984 ret = abx500_get_register_interruptible(di->dev,
1985 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1988 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1991 if (!(reg_value & VBUS_OVV_TH)) {
1992 di->flags.vbus_ovv = false;
1993 ab8500_power_supply_changed(di, di->usb_chg.psy);
1996 /* If we still have a failure, schedule a new check */
1997 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1998 queue_delayed_work(di->charger_wq,
1999 &di->check_hw_failure_work, round_jiffies(HZ));
2004 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2005 * @work: pointer to the work_struct structure
2007 * Work queue function for kicking the charger watchdog.
2009 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2010 * logic. That means we have to continously kick the charger
2011 * watchdog even when no charger is connected. This is only
2012 * valid once the AC charger has been enabled. This is
2013 * a bug that is not handled by the algorithm and the
2014 * watchdog have to be kicked by the charger driver
2015 * when the AC charger is disabled
2017 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2021 struct ab8500_charger *di = container_of(work,
2022 struct ab8500_charger, kick_wd_work.work);
2024 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2025 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2027 dev_err(di->dev, "Failed to kick WD!\n");
2029 /* Schedule a new watchdog kick */
2030 queue_delayed_work(di->charger_wq,
2031 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2035 * ab8500_charger_ac_work() - work to get and set main charger status
2036 * @work: pointer to the work_struct structure
2038 * Work queue function for checking the main charger status
2040 static void ab8500_charger_ac_work(struct work_struct *work)
2044 struct ab8500_charger *di = container_of(work,
2045 struct ab8500_charger, ac_work);
2048 * Since we can't be sure that the events are received
2049 * synchronously, we have the check if the main charger is
2050 * connected by reading the status register
2052 ret = ab8500_charger_detect_chargers(di, false);
2056 if (ret & AC_PW_CONN) {
2057 di->ac.charger_connected = 1;
2060 di->ac.charger_connected = 0;
2063 ab8500_power_supply_changed(di, di->ac_chg.psy);
2064 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
2067 static void ab8500_charger_usb_attached_work(struct work_struct *work)
2069 struct ab8500_charger *di = container_of(work,
2070 struct ab8500_charger,
2071 usb_charger_attached_work.work);
2072 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2076 for (i = 0; i < 10; i++) {
2077 ret = abx500_get_register_interruptible(di->dev,
2079 AB8500_CH_USBCH_STAT1_REG,
2082 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2085 if ((statval & usbch) != usbch)
2088 msleep(CHARGER_STATUS_POLL);
2091 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2093 mutex_lock(&di->charger_attached_mutex);
2094 mutex_unlock(&di->charger_attached_mutex);
2099 queue_delayed_work(di->charger_wq,
2100 &di->usb_charger_attached_work,
2104 static void ab8500_charger_ac_attached_work(struct work_struct *work)
2107 struct ab8500_charger *di = container_of(work,
2108 struct ab8500_charger,
2109 ac_charger_attached_work.work);
2110 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2111 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2115 for (i = 0; i < 10; i++) {
2116 ret = abx500_get_register_interruptible(di->dev,
2118 AB8500_CH_STATUS2_REG,
2121 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2125 if ((statval & mainch) != mainch)
2128 msleep(CHARGER_STATUS_POLL);
2131 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2132 queue_work(di->charger_wq, &di->ac_work);
2134 mutex_lock(&di->charger_attached_mutex);
2135 mutex_unlock(&di->charger_attached_mutex);
2140 queue_delayed_work(di->charger_wq,
2141 &di->ac_charger_attached_work,
2146 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2147 * @work: Pointer to the work_struct structure
2149 * Detect the type of USB plugged
2151 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
2155 struct ab8500_charger *di = container_of(work,
2156 struct ab8500_charger, detect_usb_type_work);
2159 * Since we can't be sure that the events are received
2160 * synchronously, we have the check if is
2161 * connected by reading the status register
2163 ret = ab8500_charger_detect_chargers(di, false);
2167 if (!(ret & USB_PW_CONN)) {
2168 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2169 di->vbus_detected = false;
2170 ab8500_charger_set_usb_connected(di, false);
2171 ab8500_power_supply_changed(di, di->usb_chg.psy);
2173 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2174 di->vbus_detected = true;
2176 if (is_ab8500_1p1_or_earlier(di->parent)) {
2177 ret = ab8500_charger_detect_usb_type(di);
2179 ab8500_charger_set_usb_connected(di, true);
2180 ab8500_power_supply_changed(di,
2185 * For ABB cut2.0 and onwards we have an IRQ,
2186 * USB_LINK_STATUS that will be triggered when the USB
2187 * link status changes. The exception is USB connected
2188 * during startup. Then we don't get a
2189 * USB_LINK_STATUS IRQ
2191 if (di->vbus_detected_start) {
2192 di->vbus_detected_start = false;
2193 ret = ab8500_charger_detect_usb_type(di);
2195 ab8500_charger_set_usb_connected(di,
2197 ab8500_power_supply_changed(di,
2206 * ab8500_charger_usb_link_attach_work() - work to detect USB type
2207 * @work: pointer to the work_struct structure
2209 * Detect the type of USB plugged
2211 static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2213 struct ab8500_charger *di =
2214 container_of(work, struct ab8500_charger, attach_work.work);
2217 /* Update maximum input current if USB enumeration is not detected */
2218 if (!di->usb.charger_online) {
2219 ret = ab8500_charger_set_vbus_in_curr(di,
2220 di->max_usb_in_curr.usb_type_max);
2225 ab8500_charger_set_usb_connected(di, true);
2226 ab8500_power_supply_changed(di, di->usb_chg.psy);
2230 * ab8500_charger_usb_link_status_work() - work to detect USB type
2231 * @work: pointer to the work_struct structure
2233 * Detect the type of USB plugged
2235 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2237 int detected_chargers;
2242 struct ab8500_charger *di = container_of(work,
2243 struct ab8500_charger, usb_link_status_work);
2246 * Since we can't be sure that the events are received
2247 * synchronously, we have the check if is
2248 * connected by reading the status register
2250 detected_chargers = ab8500_charger_detect_chargers(di, false);
2251 if (detected_chargers < 0)
2255 * Some chargers that breaks the USB spec is
2256 * identified as invalid by AB8500 and it refuse
2257 * to start the charging process. but by jumping
2258 * thru a few hoops it can be forced to start.
2260 if (is_ab8500(di->parent))
2261 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2262 AB8500_USB_LINE_STAT_REG, &val);
2264 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2265 AB8500_USB_LINK1_STAT_REG, &val);
2268 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2270 dev_dbg(di->dev, "Error reading USB link status\n");
2272 if (is_ab8500(di->parent))
2273 link_status = AB8500_USB_LINK_STATUS;
2275 link_status = AB8505_USB_LINK_STATUS;
2277 if (detected_chargers & USB_PW_CONN) {
2278 if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
2279 USB_STAT_NOT_VALID_LINK &&
2280 di->invalid_charger_detect_state == 0) {
2282 "Invalid charger detected, state= 0\n");
2284 abx500_mask_and_set_register_interruptible(di->dev,
2285 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
2286 USB_CH_ENA, USB_CH_ENA);
2287 /*Enable charger detection*/
2288 abx500_mask_and_set_register_interruptible(di->dev,
2289 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2290 USB_CH_DET, USB_CH_DET);
2291 di->invalid_charger_detect_state = 1;
2292 /*exit and wait for new link status interrupt.*/
2296 if (di->invalid_charger_detect_state == 1) {
2298 "Invalid charger detected, state= 1\n");
2299 /*Stop charger detection*/
2300 abx500_mask_and_set_register_interruptible(di->dev,
2301 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2303 /*Check link status*/
2304 if (is_ab8500(di->parent))
2305 ret = abx500_get_register_interruptible(di->dev,
2306 AB8500_USB, AB8500_USB_LINE_STAT_REG,
2309 ret = abx500_get_register_interruptible(di->dev,
2310 AB8500_USB, AB8500_USB_LINK1_STAT_REG,
2313 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2314 (val & link_status) >> USB_LINK_STATUS_SHIFT);
2315 di->invalid_charger_detect_state = 2;
2318 di->invalid_charger_detect_state = 0;
2321 if (!(detected_chargers & USB_PW_CONN)) {
2322 di->vbus_detected = false;
2323 ab8500_charger_set_usb_connected(di, false);
2324 ab8500_power_supply_changed(di, di->usb_chg.psy);
2328 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2329 di->vbus_detected = true;
2330 ret = ab8500_charger_read_usb_type(di);
2332 if (ret == -ENXIO) {
2333 /* No valid charger type detected */
2334 ab8500_charger_set_usb_connected(di, false);
2335 ab8500_power_supply_changed(di, di->usb_chg.psy);
2340 if (di->usb_device_is_unrecognised) {
2342 "Potential Legacy Charger device. "
2343 "Delay work for %d msec for USB enum "
2345 WAIT_ACA_RID_ENUMERATION);
2346 queue_delayed_work(di->charger_wq,
2348 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2349 } else if (di->is_aca_rid == 1) {
2350 /* Only wait once */
2353 "%s Wait %d msec for USB enum to finish",
2354 __func__, WAIT_ACA_RID_ENUMERATION);
2355 queue_delayed_work(di->charger_wq,
2357 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2359 queue_delayed_work(di->charger_wq,
2365 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2368 unsigned long flags;
2370 struct ab8500_charger *di = container_of(work,
2371 struct ab8500_charger, usb_state_changed_work.work);
2373 if (!di->vbus_detected) {
2375 "%s !di->vbus_detected\n",
2380 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2381 di->usb_state.state = di->usb_state.state_tmp;
2382 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
2383 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2385 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2386 __func__, di->usb_state.state, di->usb_state.usb_current);
2388 switch (di->usb_state.state) {
2389 case AB8500_BM_USB_STATE_RESET_HS:
2390 case AB8500_BM_USB_STATE_RESET_FS:
2391 case AB8500_BM_USB_STATE_SUSPEND:
2392 case AB8500_BM_USB_STATE_MAX:
2393 ab8500_charger_set_usb_connected(di, false);
2394 ab8500_power_supply_changed(di, di->usb_chg.psy);
2397 case AB8500_BM_USB_STATE_RESUME:
2399 * when suspend->resume there should be delay
2400 * of 1sec for enabling charging
2403 /* Intentional fall through */
2404 case AB8500_BM_USB_STATE_CONFIGURED:
2406 * USB is configured, enable charging with the charging
2407 * input current obtained from USB driver
2409 if (!ab8500_charger_get_usb_cur(di)) {
2410 /* Update maximum input current */
2411 ret = ab8500_charger_set_vbus_in_curr(di,
2412 di->max_usb_in_curr.usb_type_max);
2416 ab8500_charger_set_usb_connected(di, true);
2417 ab8500_power_supply_changed(di, di->usb_chg.psy);
2427 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2428 * @work: pointer to the work_struct structure
2430 * Work queue function for checking the USB charger Not OK status
2432 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2438 struct ab8500_charger *di = container_of(work,
2439 struct ab8500_charger, check_usbchgnotok_work.work);
2441 /* Check if the status bit for usbchargernotok is still active */
2442 ret = abx500_get_register_interruptible(di->dev,
2443 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2445 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2448 prev_status = di->flags.usbchargernotok;
2450 if (reg_value & VBUS_CH_NOK) {
2451 di->flags.usbchargernotok = true;
2452 /* Check again in 1sec */
2453 queue_delayed_work(di->charger_wq,
2454 &di->check_usbchgnotok_work, HZ);
2456 di->flags.usbchargernotok = false;
2457 di->flags.vbus_collapse = false;
2460 if (prev_status != di->flags.usbchargernotok)
2461 ab8500_power_supply_changed(di, di->usb_chg.psy);
2465 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2466 * @work: pointer to the work_struct structure
2468 * Work queue function for checking the Main thermal prot status
2470 static void ab8500_charger_check_main_thermal_prot_work(
2471 struct work_struct *work)
2476 struct ab8500_charger *di = container_of(work,
2477 struct ab8500_charger, check_main_thermal_prot_work);
2479 /* Check if the status bit for main_thermal_prot is still active */
2480 ret = abx500_get_register_interruptible(di->dev,
2481 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2483 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2486 if (reg_value & MAIN_CH_TH_PROT)
2487 di->flags.main_thermal_prot = true;
2489 di->flags.main_thermal_prot = false;
2491 ab8500_power_supply_changed(di, di->ac_chg.psy);
2495 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2496 * @work: pointer to the work_struct structure
2498 * Work queue function for checking the USB thermal prot status
2500 static void ab8500_charger_check_usb_thermal_prot_work(
2501 struct work_struct *work)
2506 struct ab8500_charger *di = container_of(work,
2507 struct ab8500_charger, check_usb_thermal_prot_work);
2509 /* Check if the status bit for usb_thermal_prot is still active */
2510 ret = abx500_get_register_interruptible(di->dev,
2511 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2513 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2516 if (reg_value & USB_CH_TH_PROT)
2517 di->flags.usb_thermal_prot = true;
2519 di->flags.usb_thermal_prot = false;
2521 ab8500_power_supply_changed(di, di->usb_chg.psy);
2525 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2526 * @irq: interrupt number
2527 * @_di: pointer to the ab8500_charger structure
2529 * Returns IRQ status(IRQ_HANDLED)
2531 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2533 struct ab8500_charger *di = _di;
2535 dev_dbg(di->dev, "Main charger unplugged\n");
2536 queue_work(di->charger_wq, &di->ac_work);
2538 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2539 mutex_lock(&di->charger_attached_mutex);
2540 mutex_unlock(&di->charger_attached_mutex);
2546 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2547 * @irq: interrupt number
2548 * @_di: pointer to the ab8500_charger structure
2550 * Returns IRQ status(IRQ_HANDLED)
2552 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2554 struct ab8500_charger *di = _di;
2556 dev_dbg(di->dev, "Main charger plugged\n");
2557 queue_work(di->charger_wq, &di->ac_work);
2559 mutex_lock(&di->charger_attached_mutex);
2560 mutex_unlock(&di->charger_attached_mutex);
2562 if (is_ab8500(di->parent))
2563 queue_delayed_work(di->charger_wq,
2564 &di->ac_charger_attached_work,
2570 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2571 * @irq: interrupt number
2572 * @_di: pointer to the ab8500_charger structure
2574 * Returns IRQ status(IRQ_HANDLED)
2576 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2578 struct ab8500_charger *di = _di;
2580 dev_dbg(di->dev, "Main charger not ok\n");
2581 di->flags.mainextchnotok = true;
2582 ab8500_power_supply_changed(di, di->ac_chg.psy);
2584 /* Schedule a new HW failure check */
2585 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2591 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2592 * thermal protection threshold
2593 * @irq: interrupt number
2594 * @_di: pointer to the ab8500_charger structure
2596 * Returns IRQ status(IRQ_HANDLED)
2598 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2600 struct ab8500_charger *di = _di;
2603 "Die temp above Main charger thermal protection threshold\n");
2604 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2610 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2611 * thermal protection threshold
2612 * @irq: interrupt number
2613 * @_di: pointer to the ab8500_charger structure
2615 * Returns IRQ status(IRQ_HANDLED)
2617 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2619 struct ab8500_charger *di = _di;
2622 "Die temp ok for Main charger thermal protection threshold\n");
2623 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2628 static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2630 struct ab8500_charger *di = container_of(work,
2631 struct ab8500_charger, vbus_drop_end_work.work);
2635 di->flags.vbus_drop_end = false;
2637 /* Reset the drop counter */
2638 abx500_set_register_interruptible(di->dev,
2639 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
2641 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2642 AB8500_CH_USBCH_STAT2_REG, ®_value);
2644 dev_err(di->dev, "%s read failed\n", __func__);
2648 curr = di->bm->chg_input_curr[
2649 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
2651 if (di->max_usb_in_curr.calculated_max != curr) {
2652 /* USB source is collapsing */
2653 di->max_usb_in_curr.calculated_max = curr;
2655 "VBUS input current limiting to %d mA\n",
2656 di->max_usb_in_curr.calculated_max);
2659 * USB source can not give more than this amount.
2660 * Taking more will collapse the source.
2662 di->max_usb_in_curr.set_max =
2663 di->max_usb_in_curr.calculated_max;
2665 "VBUS input current limited to %d mA\n",
2666 di->max_usb_in_curr.set_max);
2669 if (di->usb.charger_connected)
2670 ab8500_charger_set_vbus_in_curr(di,
2671 di->max_usb_in_curr.usb_type_max);
2675 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2676 * @irq: interrupt number
2677 * @_di: pointer to the ab8500_charger structure
2679 * Returns IRQ status(IRQ_HANDLED)
2681 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2683 struct ab8500_charger *di = _di;
2685 di->vbus_detected = false;
2686 dev_dbg(di->dev, "VBUS falling detected\n");
2687 queue_work(di->charger_wq, &di->detect_usb_type_work);
2693 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2694 * @irq: interrupt number
2695 * @_di: pointer to the ab8500_charger structure
2697 * Returns IRQ status(IRQ_HANDLED)
2699 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2701 struct ab8500_charger *di = _di;
2703 di->vbus_detected = true;
2704 dev_dbg(di->dev, "VBUS rising detected\n");
2706 queue_work(di->charger_wq, &di->detect_usb_type_work);
2712 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2713 * @irq: interrupt number
2714 * @_di: pointer to the ab8500_charger structure
2716 * Returns IRQ status(IRQ_HANDLED)
2718 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2720 struct ab8500_charger *di = _di;
2722 dev_dbg(di->dev, "USB link status changed\n");
2724 queue_work(di->charger_wq, &di->usb_link_status_work);
2730 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2731 * thermal protection threshold
2732 * @irq: interrupt number
2733 * @_di: pointer to the ab8500_charger structure
2735 * Returns IRQ status(IRQ_HANDLED)
2737 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2739 struct ab8500_charger *di = _di;
2742 "Die temp above USB charger thermal protection threshold\n");
2743 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2749 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2750 * thermal protection threshold
2751 * @irq: interrupt number
2752 * @_di: pointer to the ab8500_charger structure
2754 * Returns IRQ status(IRQ_HANDLED)
2756 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2758 struct ab8500_charger *di = _di;
2761 "Die temp ok for USB charger thermal protection threshold\n");
2762 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2768 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2769 * @irq: interrupt number
2770 * @_di: pointer to the ab8500_charger structure
2772 * Returns IRQ status(IRQ_HANDLED)
2774 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2776 struct ab8500_charger *di = _di;
2778 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2779 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2785 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2786 * @irq: interrupt number
2787 * @_di: pointer to the ab8500_charger structure
2789 * Returns IRQ status(IRQ_HANDLED)
2791 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2793 struct ab8500_charger *di = _di;
2795 dev_dbg(di->dev, "Charger watchdog expired\n");
2798 * The charger that was online when the watchdog expired
2799 * needs to be restarted for charging to start again
2801 if (di->ac.charger_online) {
2802 di->ac.wd_expired = true;
2803 ab8500_power_supply_changed(di, di->ac_chg.psy);
2805 if (di->usb.charger_online) {
2806 di->usb.wd_expired = true;
2807 ab8500_power_supply_changed(di, di->usb_chg.psy);
2814 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2815 * @irq: interrupt number
2816 * @_di: pointer to the ab8500_charger structure
2818 * Returns IRQ status(IRQ_HANDLED)
2820 static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2822 struct ab8500_charger *di = _di;
2824 dev_dbg(di->dev, "VBUS charger drop ended\n");
2825 di->flags.vbus_drop_end = true;
2828 * VBUS might have dropped due to bad connection.
2829 * Schedule a new input limit set to the value SW requests.
2831 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
2832 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
2838 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2839 * @irq: interrupt number
2840 * @_di: pointer to the ab8500_charger structure
2842 * Returns IRQ status(IRQ_HANDLED)
2844 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2846 struct ab8500_charger *di = _di;
2848 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2849 di->flags.vbus_ovv = true;
2850 ab8500_power_supply_changed(di, di->usb_chg.psy);
2852 /* Schedule a new HW failure check */
2853 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2859 * ab8500_charger_ac_get_property() - get the ac/mains properties
2860 * @psy: pointer to the power_supply structure
2861 * @psp: pointer to the power_supply_property structure
2862 * @val: pointer to the power_supply_propval union
2864 * This function gets called when an application tries to get the ac/mains
2865 * properties by reading the sysfs files.
2866 * AC/Mains properties are online, present and voltage.
2867 * online: ac/mains charging is in progress or not
2868 * present: presence of the ac/mains
2869 * voltage: AC/Mains voltage
2870 * Returns error code in case of failure else 0(on success)
2872 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2873 enum power_supply_property psp,
2874 union power_supply_propval *val)
2876 struct ab8500_charger *di;
2879 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2882 case POWER_SUPPLY_PROP_HEALTH:
2883 if (di->flags.mainextchnotok)
2884 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2885 else if (di->ac.wd_expired || di->usb.wd_expired)
2886 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2887 else if (di->flags.main_thermal_prot)
2888 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2890 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2892 case POWER_SUPPLY_PROP_ONLINE:
2893 val->intval = di->ac.charger_online;
2895 case POWER_SUPPLY_PROP_PRESENT:
2896 val->intval = di->ac.charger_connected;
2898 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2899 ret = ab8500_charger_get_ac_voltage(di);
2901 di->ac.charger_voltage = ret;
2902 /* On error, use previous value */
2903 val->intval = di->ac.charger_voltage * 1000;
2905 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2907 * This property is used to indicate when CV mode is entered
2908 * for the AC charger
2910 di->ac.cv_active = ab8500_charger_ac_cv(di);
2911 val->intval = di->ac.cv_active;
2913 case POWER_SUPPLY_PROP_CURRENT_NOW:
2914 ret = ab8500_charger_get_ac_current(di);
2916 di->ac.charger_current = ret;
2917 val->intval = di->ac.charger_current * 1000;
2926 * ab8500_charger_usb_get_property() - get the usb properties
2927 * @psy: pointer to the power_supply structure
2928 * @psp: pointer to the power_supply_property structure
2929 * @val: pointer to the power_supply_propval union
2931 * This function gets called when an application tries to get the usb
2932 * properties by reading the sysfs files.
2933 * USB properties are online, present and voltage.
2934 * online: usb charging is in progress or not
2935 * present: presence of the usb
2936 * voltage: vbus voltage
2937 * Returns error code in case of failure else 0(on success)
2939 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2940 enum power_supply_property psp,
2941 union power_supply_propval *val)
2943 struct ab8500_charger *di;
2946 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2949 case POWER_SUPPLY_PROP_HEALTH:
2950 if (di->flags.usbchargernotok)
2951 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2952 else if (di->ac.wd_expired || di->usb.wd_expired)
2953 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2954 else if (di->flags.usb_thermal_prot)
2955 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2956 else if (di->flags.vbus_ovv)
2957 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2959 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2961 case POWER_SUPPLY_PROP_ONLINE:
2962 val->intval = di->usb.charger_online;
2964 case POWER_SUPPLY_PROP_PRESENT:
2965 val->intval = di->usb.charger_connected;
2967 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2968 ret = ab8500_charger_get_vbus_voltage(di);
2970 di->usb.charger_voltage = ret;
2971 val->intval = di->usb.charger_voltage * 1000;
2973 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2975 * This property is used to indicate when CV mode is entered
2976 * for the USB charger
2978 di->usb.cv_active = ab8500_charger_usb_cv(di);
2979 val->intval = di->usb.cv_active;
2981 case POWER_SUPPLY_PROP_CURRENT_NOW:
2982 ret = ab8500_charger_get_usb_current(di);
2984 di->usb.charger_current = ret;
2985 val->intval = di->usb.charger_current * 1000;
2987 case POWER_SUPPLY_PROP_CURRENT_AVG:
2989 * This property is used to indicate when VBUS has collapsed
2990 * due to too high output current from the USB charger
2992 if (di->flags.vbus_collapse)
3004 * ab8500_charger_init_hw_registers() - Set up charger related registers
3005 * @di: pointer to the ab8500_charger structure
3007 * Set up charger OVV, watchdog and maximum voltage registers as well as
3008 * charging of the backup battery
3010 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3013 u8 bup_vch_range = 0, vbup33_vrtcn = 0;
3015 /* Setup maximum charger current and voltage for ABB cut2.0 */
3016 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3017 ret = abx500_set_register_interruptible(di->dev,
3019 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3022 "failed to set CH_VOLT_LVL_MAX_REG\n");
3026 ret = abx500_set_register_interruptible(di->dev,
3027 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3031 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3036 if (is_ab8505_2p0(di->parent))
3037 ret = abx500_mask_and_set_register_interruptible(di->dev,
3039 AB8500_USBCH_CTRL2_REG,
3040 VBUS_AUTO_IN_CURR_LIM_ENA,
3041 VBUS_AUTO_IN_CURR_LIM_ENA);
3044 * VBUS OVV set to 6.3V and enable automatic current limitation
3046 ret = abx500_set_register_interruptible(di->dev,
3048 AB8500_USBCH_CTRL2_REG,
3049 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
3052 "failed to set automatic current limitation\n");
3056 /* Enable main watchdog in OTP */
3057 ret = abx500_set_register_interruptible(di->dev,
3058 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3060 dev_err(di->dev, "failed to enable main WD in OTP\n");
3064 /* Enable main watchdog */
3065 ret = abx500_set_register_interruptible(di->dev,
3066 AB8500_SYS_CTRL2_BLOCK,
3067 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3069 dev_err(di->dev, "failed to enable main watchdog\n");
3074 * Due to internal synchronisation, Enable and Kick watchdog bits
3075 * cannot be enabled in a single write.
3076 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3077 * between writing Enable then Kick bits.
3081 /* Kick main watchdog */
3082 ret = abx500_set_register_interruptible(di->dev,
3083 AB8500_SYS_CTRL2_BLOCK,
3084 AB8500_MAIN_WDOG_CTRL_REG,
3085 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3087 dev_err(di->dev, "failed to kick main watchdog\n");
3091 /* Disable main watchdog */
3092 ret = abx500_set_register_interruptible(di->dev,
3093 AB8500_SYS_CTRL2_BLOCK,
3094 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3096 dev_err(di->dev, "failed to disable main watchdog\n");
3100 /* Set watchdog timeout */
3101 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3102 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3104 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3108 ret = ab8500_charger_led_en(di, false);
3110 dev_err(di->dev, "failed to disable LED\n");
3114 /* Backup battery voltage and current */
3115 if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V)
3116 bup_vch_range = BUP_VCH_RANGE;
3117 if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V)
3118 vbup33_vrtcn = VBUP33_VRTCN;
3120 ret = abx500_set_register_interruptible(di->dev,
3122 AB8500_RTC_BACKUP_CHG_REG,
3123 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
3125 dev_err(di->dev, "failed to setup backup battery charging\n");
3129 /* Enable backup battery charging */
3130 ret = abx500_mask_and_set_register_interruptible(di->dev,
3131 AB8500_RTC, AB8500_RTC_CTRL_REG,
3132 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3134 dev_err(di->dev, "%s mask and set failed\n", __func__);
3143 * ab8500 charger driver interrupts and their respective isr
3145 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3146 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3147 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3148 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3149 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3150 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3151 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3152 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3153 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3154 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3155 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3156 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3157 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3158 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
3159 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
3162 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3163 unsigned long event, void *power)
3165 struct ab8500_charger *di =
3166 container_of(nb, struct ab8500_charger, nb);
3167 enum ab8500_usb_state bm_usb_state;
3168 unsigned mA = *((unsigned *)power);
3173 if (event != USB_EVENT_VBUS) {
3174 dev_dbg(di->dev, "not a standard host, returning\n");
3178 /* TODO: State is fabricate here. See if charger really needs USB
3179 * state or if mA is enough
3181 if ((di->usb_state.usb_current == 2) && (mA > 2))
3182 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3184 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3186 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3187 else if (mA >= 8) /* 8, 100, 500 */
3188 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3189 else /* Should never occur */
3190 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3192 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3193 __func__, bm_usb_state, mA);
3195 spin_lock(&di->usb_state.usb_lock);
3196 di->usb_state.state_tmp = bm_usb_state;
3197 di->usb_state.usb_current_tmp = mA;
3198 spin_unlock(&di->usb_state.usb_lock);
3201 * wait for some time until you get updates from the usb stack
3202 * and negotiations are completed
3204 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
3209 #if defined(CONFIG_PM)
3210 static int ab8500_charger_resume(struct platform_device *pdev)
3213 struct ab8500_charger *di = platform_get_drvdata(pdev);
3216 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3217 * logic. That means we have to continously kick the charger
3218 * watchdog even when no charger is connected. This is only
3219 * valid once the AC charger has been enabled. This is
3220 * a bug that is not handled by the algorithm and the
3221 * watchdog have to be kicked by the charger driver
3222 * when the AC charger is disabled
3224 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3225 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3226 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3228 dev_err(di->dev, "Failed to kick WD!\n");
3230 /* If not already pending start a new timer */
3231 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3232 round_jiffies(WD_KICK_INTERVAL));
3235 /* If we still have a HW failure, schedule a new check */
3236 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3237 queue_delayed_work(di->charger_wq,
3238 &di->check_hw_failure_work, 0);
3241 if (di->flags.vbus_drop_end)
3242 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3247 static int ab8500_charger_suspend(struct platform_device *pdev,
3250 struct ab8500_charger *di = platform_get_drvdata(pdev);
3252 /* Cancel any pending jobs */
3253 cancel_delayed_work(&di->check_hw_failure_work);
3254 cancel_delayed_work(&di->vbus_drop_end_work);
3256 flush_delayed_work(&di->attach_work);
3257 flush_delayed_work(&di->usb_charger_attached_work);
3258 flush_delayed_work(&di->ac_charger_attached_work);
3259 flush_delayed_work(&di->check_usbchgnotok_work);
3260 flush_delayed_work(&di->check_vbat_work);
3261 flush_delayed_work(&di->kick_wd_work);
3263 flush_work(&di->usb_link_status_work);
3264 flush_work(&di->ac_work);
3265 flush_work(&di->detect_usb_type_work);
3267 if (atomic_read(&di->current_stepping_sessions))
3273 #define ab8500_charger_suspend NULL
3274 #define ab8500_charger_resume NULL
3277 static struct notifier_block charger_nb = {
3278 .notifier_call = ab8500_external_charger_prepare,
3281 static int ab8500_charger_remove(struct platform_device *pdev)
3283 struct ab8500_charger *di = platform_get_drvdata(pdev);
3286 /* Disable AC charging */
3287 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3289 /* Disable USB charging */
3290 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3292 /* Disable interrupts */
3293 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3294 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3298 /* Backup battery voltage and current disable */
3299 ret = abx500_mask_and_set_register_interruptible(di->dev,
3300 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3302 dev_err(di->dev, "%s mask and set failed\n", __func__);
3304 usb_unregister_notifier(di->usb_phy, &di->nb);
3305 usb_put_phy(di->usb_phy);
3307 /* Delete the work queue */
3308 destroy_workqueue(di->charger_wq);
3310 /* Unregister external charger enable notifier */
3311 if (!di->ac_chg.enabled)
3312 blocking_notifier_chain_unregister(
3313 &charger_notifier_list, &charger_nb);
3315 flush_scheduled_work();
3316 if (di->usb_chg.enabled)
3317 power_supply_unregister(di->usb_chg.psy);
3319 if (di->ac_chg.enabled && !di->ac_chg.external)
3320 power_supply_unregister(di->ac_chg.psy);
3325 static char *supply_interface[] = {
3331 static const struct power_supply_desc ab8500_ac_chg_desc = {
3332 .name = "ab8500_ac",
3333 .type = POWER_SUPPLY_TYPE_MAINS,
3334 .properties = ab8500_charger_ac_props,
3335 .num_properties = ARRAY_SIZE(ab8500_charger_ac_props),
3336 .get_property = ab8500_charger_ac_get_property,
3339 static const struct power_supply_desc ab8500_usb_chg_desc = {
3340 .name = "ab8500_usb",
3341 .type = POWER_SUPPLY_TYPE_USB,
3342 .properties = ab8500_charger_usb_props,
3343 .num_properties = ARRAY_SIZE(ab8500_charger_usb_props),
3344 .get_property = ab8500_charger_usb_get_property,
3347 static int ab8500_charger_probe(struct platform_device *pdev)
3349 struct device_node *np = pdev->dev.of_node;
3350 struct abx500_bm_data *plat = pdev->dev.platform_data;
3351 struct power_supply_config ac_psy_cfg = {}, usb_psy_cfg = {};
3352 struct ab8500_charger *di;
3353 int irq, i, charger_status, ret = 0, ch_stat;
3355 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3357 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
3362 dev_err(&pdev->dev, "no battery management data supplied\n");
3368 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3370 dev_err(&pdev->dev, "failed to get battery information\n");
3373 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3375 di->autopower_cfg = false;
3377 /* get parent data */
3378 di->dev = &pdev->dev;
3379 di->parent = dev_get_drvdata(pdev->dev.parent);
3380 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3382 /* initialize lock */
3383 spin_lock_init(&di->usb_state.usb_lock);
3384 mutex_init(&di->usb_ipt_crnt_lock);
3386 di->autopower = false;
3387 di->invalid_charger_detect_state = 0;
3389 /* AC and USB supply config */
3390 ac_psy_cfg.supplied_to = supply_interface;
3391 ac_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3392 ac_psy_cfg.drv_data = &di->ac_chg;
3393 usb_psy_cfg.supplied_to = supply_interface;
3394 usb_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3395 usb_psy_cfg.drv_data = &di->usb_chg;
3398 /* ux500_charger sub-class */
3399 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
3400 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
3401 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3402 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3403 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3404 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3405 di->ac_chg.max_out_curr =
3406 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3407 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
3408 di->ac_chg.enabled = di->bm->ac_enabled;
3409 di->ac_chg.external = false;
3411 /*notifier for external charger enabling*/
3412 if (!di->ac_chg.enabled)
3413 blocking_notifier_chain_register(
3414 &charger_notifier_list, &charger_nb);
3417 /* ux500_charger sub-class */
3418 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
3419 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
3420 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3421 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3422 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3423 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3424 di->usb_chg.max_out_curr =
3425 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3426 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
3427 di->usb_chg.enabled = di->bm->usb_enabled;
3428 di->usb_chg.external = false;
3429 di->usb_state.usb_current = -1;
3431 /* Create a work queue for the charger */
3432 di->charger_wq = alloc_ordered_workqueue("ab8500_charger_wq",
3434 if (di->charger_wq == NULL) {
3435 dev_err(di->dev, "failed to create work queue\n");
3439 mutex_init(&di->charger_attached_mutex);
3441 /* Init work for HW failure check */
3442 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
3443 ab8500_charger_check_hw_failure_work);
3444 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
3445 ab8500_charger_check_usbchargernotok_work);
3447 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3448 ab8500_charger_ac_attached_work);
3449 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3450 ab8500_charger_usb_attached_work);
3453 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3454 * logic. That means we have to continously kick the charger
3455 * watchdog even when no charger is connected. This is only
3456 * valid once the AC charger has been enabled. This is
3457 * a bug that is not handled by the algorithm and the
3458 * watchdog have to be kicked by the charger driver
3459 * when the AC charger is disabled
3461 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
3462 ab8500_charger_kick_watchdog_work);
3464 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
3465 ab8500_charger_check_vbat_work);
3467 INIT_DELAYED_WORK(&di->attach_work,
3468 ab8500_charger_usb_link_attach_work);
3470 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3471 ab8500_charger_usb_state_changed_work);
3473 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3474 ab8500_charger_vbus_drop_end_work);
3476 /* Init work for charger detection */
3477 INIT_WORK(&di->usb_link_status_work,
3478 ab8500_charger_usb_link_status_work);
3479 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3480 INIT_WORK(&di->detect_usb_type_work,
3481 ab8500_charger_detect_usb_type_work);
3483 /* Init work for checking HW status */
3484 INIT_WORK(&di->check_main_thermal_prot_work,
3485 ab8500_charger_check_main_thermal_prot_work);
3486 INIT_WORK(&di->check_usb_thermal_prot_work,
3487 ab8500_charger_check_usb_thermal_prot_work);
3490 * VDD ADC supply needs to be enabled from this driver when there
3491 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3492 * interrupts during charging
3494 di->regu = devm_regulator_get(di->dev, "vddadc");
3495 if (IS_ERR(di->regu)) {
3496 ret = PTR_ERR(di->regu);
3497 dev_err(di->dev, "failed to get vddadc regulator\n");
3498 goto free_charger_wq;
3502 /* Initialize OVV, and other registers */
3503 ret = ab8500_charger_init_hw_registers(di);
3505 dev_err(di->dev, "failed to initialize ABB registers\n");
3506 goto free_charger_wq;
3509 /* Register AC charger class */
3510 if (di->ac_chg.enabled) {
3511 di->ac_chg.psy = power_supply_register(di->dev,
3512 &ab8500_ac_chg_desc,
3514 if (IS_ERR(di->ac_chg.psy)) {
3515 dev_err(di->dev, "failed to register AC charger\n");
3516 ret = PTR_ERR(di->ac_chg.psy);
3517 goto free_charger_wq;
3521 /* Register USB charger class */
3522 if (di->usb_chg.enabled) {
3523 di->usb_chg.psy = power_supply_register(di->dev,
3524 &ab8500_usb_chg_desc,
3526 if (IS_ERR(di->usb_chg.psy)) {
3527 dev_err(di->dev, "failed to register USB charger\n");
3528 ret = PTR_ERR(di->usb_chg.psy);
3533 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3534 if (IS_ERR_OR_NULL(di->usb_phy)) {
3535 dev_err(di->dev, "failed to get usb transceiver\n");
3539 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3540 ret = usb_register_notifier(di->usb_phy, &di->nb);
3542 dev_err(di->dev, "failed to register usb notifier\n");
3546 /* Identify the connected charger types during startup */
3547 charger_status = ab8500_charger_detect_chargers(di, true);
3548 if (charger_status & AC_PW_CONN) {
3549 di->ac.charger_connected = 1;
3551 ab8500_power_supply_changed(di, di->ac_chg.psy);
3552 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
3555 if (charger_status & USB_PW_CONN) {
3556 di->vbus_detected = true;
3557 di->vbus_detected_start = true;
3558 queue_work(di->charger_wq,
3559 &di->detect_usb_type_work);
3562 /* Register interrupts */
3563 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3564 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3565 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3566 IRQF_SHARED | IRQF_NO_SUSPEND,
3567 ab8500_charger_irq[i].name, di);
3570 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3571 , ab8500_charger_irq[i].name, irq, ret);
3574 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3575 ab8500_charger_irq[i].name, irq, ret);
3578 platform_set_drvdata(pdev, di);
3580 mutex_lock(&di->charger_attached_mutex);
3582 ch_stat = ab8500_charger_detect_chargers(di, false);
3584 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3585 if (is_ab8500(di->parent))
3586 queue_delayed_work(di->charger_wq,
3587 &di->ac_charger_attached_work,
3590 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3591 if (is_ab8500(di->parent))
3592 queue_delayed_work(di->charger_wq,
3593 &di->usb_charger_attached_work,
3597 mutex_unlock(&di->charger_attached_mutex);
3602 usb_unregister_notifier(di->usb_phy, &di->nb);
3604 /* We also have to free all successfully registered irqs */
3605 for (i = i - 1; i >= 0; i--) {
3606 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3610 usb_put_phy(di->usb_phy);
3612 if (di->usb_chg.enabled)
3613 power_supply_unregister(di->usb_chg.psy);
3615 if (di->ac_chg.enabled)
3616 power_supply_unregister(di->ac_chg.psy);
3618 destroy_workqueue(di->charger_wq);
3622 static const struct of_device_id ab8500_charger_match[] = {
3623 { .compatible = "stericsson,ab8500-charger", },
3627 static struct platform_driver ab8500_charger_driver = {
3628 .probe = ab8500_charger_probe,
3629 .remove = ab8500_charger_remove,
3630 .suspend = ab8500_charger_suspend,
3631 .resume = ab8500_charger_resume,
3633 .name = "ab8500-charger",
3634 .of_match_table = ab8500_charger_match,
3638 static int __init ab8500_charger_init(void)
3640 return platform_driver_register(&ab8500_charger_driver);
3643 static void __exit ab8500_charger_exit(void)
3645 platform_driver_unregister(&ab8500_charger_driver);
3648 subsys_initcall_sync(ab8500_charger_init);
3649 module_exit(ab8500_charger_exit);
3651 MODULE_LICENSE("GPL v2");
3652 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3653 MODULE_ALIAS("platform:ab8500-charger");
3654 MODULE_DESCRIPTION("AB8500 charger management driver");