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 AB8540_VBUS_IN_CURR_LIM_SHIFT 2
62 #define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
63 #define AB8540_AUTO_VBUS_IN_CURR_MASK 0x3F
64 #define VBUS_IN_CURR_LIM_RETRY_SET_TIME 30 /* seconds */
66 #define LED_INDICATOR_PWM_ENA 0x01
67 #define LED_INDICATOR_PWM_DIS 0x00
68 #define LED_IND_CUR_5MA 0x04
69 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
71 /* HW failure constants */
72 #define MAIN_CH_TH_PROT 0x02
73 #define VBUS_CH_NOK 0x08
74 #define USB_CH_TH_PROT 0x02
75 #define VBUS_OVV_TH 0x01
76 #define MAIN_CH_NOK 0x01
79 #define MAIN_CH_STATUS2_MAINCHGDROP 0x80
80 #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
81 #define USB_CH_VBUSDROP 0x40
82 #define USB_CH_VBUSDETDBNC 0x01
84 /* UsbLineStatus register bit masks */
85 #define AB8500_USB_LINK_STATUS 0x78
86 #define AB8505_USB_LINK_STATUS 0xF8
87 #define AB8500_STD_HOST_SUSP 0x18
88 #define USB_LINK_STATUS_SHIFT 3
90 /* Watchdog timeout constant */
91 #define WD_TIMER 0x30 /* 4min */
92 #define WD_KICK_INTERVAL (60 * HZ)
94 /* Lowest charger voltage is 3.39V -> 0x4E */
95 #define LOW_VOLT_REG 0x4E
97 /* Step up/down delay in us */
98 #define STEP_UDELAY 1000
100 #define CHARGER_STATUS_POLL 10 /* in ms */
102 #define CHG_WD_INTERVAL (60 * HZ)
104 #define AB8500_SW_CONTROL_FALLBACK 0x03
105 /* Wait for enumeration before charing in us */
106 #define WAIT_ACA_RID_ENUMERATION (5 * 1000)
107 /*External charger control*/
108 #define AB8500_SYS_CHARGER_CONTROL_REG 0x52
109 #define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
110 #define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
112 /* UsbLineStatus register - usb types */
113 enum ab8500_charger_link_status {
114 USB_STAT_NOT_CONFIGURED,
115 USB_STAT_STD_HOST_NC,
116 USB_STAT_STD_HOST_C_NS,
117 USB_STAT_STD_HOST_C_S,
118 USB_STAT_HOST_CHG_NM,
119 USB_STAT_HOST_CHG_HS,
120 USB_STAT_HOST_CHG_HS_CHIRP,
121 USB_STAT_DEDICATED_CHG,
124 USB_STAT_ACA_RID_C_NM,
125 USB_STAT_ACA_RID_C_HS,
126 USB_STAT_ACA_RID_C_HS_CHIRP,
129 USB_STAT_NOT_VALID_LINK,
131 USB_STAT_SUP_NO_IDGND_VBUS,
132 USB_STAT_SUP_IDGND_VBUS,
133 USB_STAT_CHARGER_LINE_1,
136 USB_STAT_ACA_DOCK_CHARGER,
139 enum ab8500_usb_state {
140 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
141 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
142 AB8500_BM_USB_STATE_CONFIGURED,
143 AB8500_BM_USB_STATE_SUSPEND,
144 AB8500_BM_USB_STATE_RESUME,
145 AB8500_BM_USB_STATE_MAX,
148 /* VBUS input current limits supported in AB8500 in mA */
149 #define USB_CH_IP_CUR_LVL_0P05 50
150 #define USB_CH_IP_CUR_LVL_0P09 98
151 #define USB_CH_IP_CUR_LVL_0P19 193
152 #define USB_CH_IP_CUR_LVL_0P29 290
153 #define USB_CH_IP_CUR_LVL_0P38 380
154 #define USB_CH_IP_CUR_LVL_0P45 450
155 #define USB_CH_IP_CUR_LVL_0P5 500
156 #define USB_CH_IP_CUR_LVL_0P6 600
157 #define USB_CH_IP_CUR_LVL_0P7 700
158 #define USB_CH_IP_CUR_LVL_0P8 800
159 #define USB_CH_IP_CUR_LVL_0P9 900
160 #define USB_CH_IP_CUR_LVL_1P0 1000
161 #define USB_CH_IP_CUR_LVL_1P1 1100
162 #define USB_CH_IP_CUR_LVL_1P3 1300
163 #define USB_CH_IP_CUR_LVL_1P4 1400
164 #define USB_CH_IP_CUR_LVL_1P5 1500
166 #define VBAT_TRESH_IP_CUR_RED 3800
168 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
169 struct ab8500_charger, usb_chg)
170 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
171 struct ab8500_charger, ac_chg)
174 * struct ab8500_charger_interrupts - ab8500 interupts
175 * @name: name of the interrupt
176 * @isr function pointer to the isr
178 struct ab8500_charger_interrupts {
180 irqreturn_t (*isr)(int irq, void *data);
183 struct ab8500_charger_info {
184 int charger_connected;
192 struct ab8500_charger_event_flags {
194 bool main_thermal_prot;
195 bool usb_thermal_prot;
197 bool usbchargernotok;
203 struct ab8500_charger_usb_state {
206 enum ab8500_usb_state state;
207 enum ab8500_usb_state state_tmp;
211 struct ab8500_charger_max_usb_in_curr {
218 * struct ab8500_charger - ab8500 Charger device information
219 * @dev: Pointer to the structure device
220 * @vbus_detected: VBUS detected
221 * @vbus_detected_start:
222 * VBUS detected during startup
223 * @ac_conn: This will be true when the AC charger has been plugged
224 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
226 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
228 * @vbat Battery voltage
229 * @old_vbat Previously measured battery voltage
230 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
231 * @autopower Indicate if we should have automatic pwron after pwrloss
232 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
233 * @invalid_charger_detect_state State when forcing AB to use invalid charger
234 * @is_aca_rid: Incicate if accessory is ACA type
235 * @current_stepping_sessions:
236 * Counter for current stepping sessions
237 * @parent: Pointer to the struct ab8500
238 * @gpadc: Pointer to the struct gpadc
239 * @bm: Platform specific battery management information
240 * @flags: Structure for information about events triggered
241 * @usb_state: Structure for usb stack information
242 * @max_usb_in_curr: Max USB charger input current
243 * @ac_chg: AC charger power supply
244 * @usb_chg: USB charger power supply
245 * @ac: Structure that holds the AC charger properties
246 * @usb: Structure that holds the USB charger properties
247 * @regu: Pointer to the struct regulator
248 * @charger_wq: Work queue for the IRQs and checking HW state
249 * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
250 * @pm_lock: Lock to prevent system to suspend
251 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
252 * @check_hw_failure_work: Work for checking HW state
253 * @check_usbchgnotok_work: Work for checking USB charger not ok status
254 * @kick_wd_work: Work for kicking the charger watchdog in case
255 * of ABB rev 1.* due to the watchog logic bug
256 * @ac_charger_attached_work: Work for checking if AC charger is still
258 * @usb_charger_attached_work: Work for checking if USB charger is still
260 * @ac_work: Work for checking AC charger connection
261 * @detect_usb_type_work: Work for detecting the USB type connected
262 * @usb_link_status_work: Work for checking the new USB link status
263 * @usb_state_changed_work: Work for checking USB state
264 * @attach_work: Work for detecting USB type
265 * @vbus_drop_end_work: Work for detecting VBUS drop end
266 * @check_main_thermal_prot_work:
267 * Work for checking Main thermal status
268 * @check_usb_thermal_prot_work:
269 * Work for checking USB thermal status
270 * @charger_attached_mutex: For controlling the wakelock
272 struct ab8500_charger {
275 bool vbus_detected_start;
281 bool usb_device_is_unrecognised;
284 int invalid_charger_detect_state;
286 atomic_t current_stepping_sessions;
287 struct ab8500 *parent;
288 struct ab8500_gpadc *gpadc;
289 struct abx500_bm_data *bm;
290 struct ab8500_charger_event_flags flags;
291 struct ab8500_charger_usb_state usb_state;
292 struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
293 struct ux500_charger ac_chg;
294 struct ux500_charger usb_chg;
295 struct ab8500_charger_info ac;
296 struct ab8500_charger_info usb;
297 struct regulator *regu;
298 struct workqueue_struct *charger_wq;
299 struct mutex usb_ipt_crnt_lock;
300 struct delayed_work check_vbat_work;
301 struct delayed_work check_hw_failure_work;
302 struct delayed_work check_usbchgnotok_work;
303 struct delayed_work kick_wd_work;
304 struct delayed_work usb_state_changed_work;
305 struct delayed_work attach_work;
306 struct delayed_work ac_charger_attached_work;
307 struct delayed_work usb_charger_attached_work;
308 struct delayed_work vbus_drop_end_work;
309 struct work_struct ac_work;
310 struct work_struct detect_usb_type_work;
311 struct work_struct usb_link_status_work;
312 struct work_struct check_main_thermal_prot_work;
313 struct work_struct check_usb_thermal_prot_work;
314 struct usb_phy *usb_phy;
315 struct notifier_block nb;
316 struct mutex charger_attached_mutex;
320 static enum power_supply_property ab8500_charger_ac_props[] = {
321 POWER_SUPPLY_PROP_HEALTH,
322 POWER_SUPPLY_PROP_PRESENT,
323 POWER_SUPPLY_PROP_ONLINE,
324 POWER_SUPPLY_PROP_VOLTAGE_NOW,
325 POWER_SUPPLY_PROP_VOLTAGE_AVG,
326 POWER_SUPPLY_PROP_CURRENT_NOW,
330 static enum power_supply_property ab8500_charger_usb_props[] = {
331 POWER_SUPPLY_PROP_HEALTH,
332 POWER_SUPPLY_PROP_CURRENT_AVG,
333 POWER_SUPPLY_PROP_PRESENT,
334 POWER_SUPPLY_PROP_ONLINE,
335 POWER_SUPPLY_PROP_VOLTAGE_NOW,
336 POWER_SUPPLY_PROP_VOLTAGE_AVG,
337 POWER_SUPPLY_PROP_CURRENT_NOW,
341 * Function for enabling and disabling sw fallback mode
342 * should always be disabled when no charger is connected.
344 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
353 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
355 if (is_ab8500(di->parent)) {
360 bank = AB8500_SYS_CTRL1_BLOCK;
361 reg = AB8500_SW_CONTROL_FALLBACK;
365 /* read the register containing fallback bit */
366 ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
368 dev_err(di->dev, "%d read failed\n", __LINE__);
372 if (is_ab8500(di->parent)) {
373 /* enable the OPT emulation registers */
374 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
376 dev_err(di->dev, "%d write failed\n", __LINE__);
386 /* write back the changed fallback bit value to register */
387 ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
389 dev_err(di->dev, "%d write failed\n", __LINE__);
393 if (is_ab8500(di->parent)) {
394 /* disable the set OTP registers again */
395 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
397 dev_err(di->dev, "%d write failed\n", __LINE__);
403 * ab8500_power_supply_changed - a wrapper with local extentions for
404 * power_supply_changed
405 * @di: pointer to the ab8500_charger structure
406 * @psy: pointer to power_supply_that have changed.
409 static void ab8500_power_supply_changed(struct ab8500_charger *di,
410 struct power_supply *psy)
412 if (di->autopower_cfg) {
413 if (!di->usb.charger_connected &&
414 !di->ac.charger_connected &&
416 di->autopower = false;
417 ab8500_enable_disable_sw_fallback(di, false);
418 } else if (!di->autopower &&
419 (di->ac.charger_connected ||
420 di->usb.charger_connected)) {
421 di->autopower = true;
422 ab8500_enable_disable_sw_fallback(di, true);
425 power_supply_changed(psy);
428 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
431 if (connected != di->usb.charger_connected) {
432 dev_dbg(di->dev, "USB connected:%i\n", connected);
433 di->usb.charger_connected = connected;
436 di->flags.vbus_drop_end = false;
438 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
441 mutex_lock(&di->charger_attached_mutex);
442 mutex_unlock(&di->charger_attached_mutex);
444 if (is_ab8500(di->parent))
445 queue_delayed_work(di->charger_wq,
446 &di->usb_charger_attached_work,
449 cancel_delayed_work_sync(&di->usb_charger_attached_work);
450 mutex_lock(&di->charger_attached_mutex);
451 mutex_unlock(&di->charger_attached_mutex);
457 * ab8500_charger_get_ac_voltage() - get ac charger voltage
458 * @di: pointer to the ab8500_charger structure
460 * Returns ac charger voltage (on success)
462 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
466 /* Only measure voltage if the charger is connected */
467 if (di->ac.charger_connected) {
468 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
470 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
478 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
479 * @di: pointer to the ab8500_charger structure
481 * Returns ac charger CV mode (on success) else error code
483 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
488 /* Only check CV mode if the charger is online */
489 if (di->ac.charger_online) {
490 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
491 AB8500_CH_STATUS1_REG, &val);
493 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
497 if (val & MAIN_CH_CV_ON)
507 * ab8500_charger_get_vbus_voltage() - get vbus voltage
508 * @di: pointer to the ab8500_charger structure
510 * This function returns the vbus voltage.
511 * Returns vbus voltage (on success)
513 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
517 /* Only measure voltage if the charger is connected */
518 if (di->usb.charger_connected) {
519 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
521 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
529 * ab8500_charger_get_usb_current() - get usb charger current
530 * @di: pointer to the ab8500_charger structure
532 * This function returns the usb charger current.
533 * Returns usb current (on success) and error code on failure
535 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
539 /* Only measure current if the charger is online */
540 if (di->usb.charger_online) {
541 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
543 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
551 * ab8500_charger_get_ac_current() - get ac charger current
552 * @di: pointer to the ab8500_charger structure
554 * This function returns the ac charger current.
555 * Returns ac current (on success) and error code on failure.
557 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
561 /* Only measure current if the charger is online */
562 if (di->ac.charger_online) {
563 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
565 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
573 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
574 * @di: pointer to the ab8500_charger structure
576 * Returns ac charger CV mode (on success) else error code
578 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
583 /* Only check CV mode if the charger is online */
584 if (di->usb.charger_online) {
585 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
586 AB8500_CH_USBCH_STAT1_REG, &val);
588 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
592 if (val & USB_CH_CV_ON)
604 * ab8500_charger_detect_chargers() - Detect the connected chargers
605 * @di: pointer to the ab8500_charger structure
606 * @probe: if probe, don't delay and wait for HW
608 * Returns the type of charger connected.
609 * For USB it will not mean we can actually charge from it
610 * but that there is a USB cable connected that we have to
611 * identify. This is used during startup when we don't get
612 * interrupts of the charger detection
614 * Returns an integer value, that means,
615 * NO_PW_CONN no power supply is connected
616 * AC_PW_CONN if the AC power supply is connected
617 * USB_PW_CONN if the USB power supply is connected
618 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
620 static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
622 int result = NO_PW_CONN;
626 /* Check for AC charger */
627 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
628 AB8500_CH_STATUS1_REG, &val);
630 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
634 if (val & MAIN_CH_DET)
637 /* Check for USB charger */
641 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
642 * when disconnecting ACA even though no
643 * charger was connected. Try waiting a little
644 * longer than the 100 ms of VBUS_DET_DBNC100...
648 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
649 AB8500_CH_USBCH_STAT1_REG, &val);
651 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
655 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
657 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
658 result |= USB_PW_CONN;
664 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
665 * @di: pointer to the ab8500_charger structure
666 * @link_status: the identified USB type
668 * Get the maximum current that is allowed to be drawn from the host
669 * based on the USB type.
670 * Returns error code in case of failure else 0 on success
672 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
673 enum ab8500_charger_link_status link_status)
677 di->usb_device_is_unrecognised = false;
680 * Platform only supports USB 2.0.
681 * This means that charging current from USB source
682 * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
683 * should set USB_CH_IP_CUR_LVL_0P5.
686 switch (link_status) {
687 case USB_STAT_STD_HOST_NC:
688 case USB_STAT_STD_HOST_C_NS:
689 case USB_STAT_STD_HOST_C_S:
690 dev_dbg(di->dev, "USB Type - Standard host is "
691 "detected through USB driver\n");
692 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
695 case USB_STAT_HOST_CHG_HS_CHIRP:
696 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
699 case USB_STAT_HOST_CHG_HS:
700 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
703 case USB_STAT_ACA_RID_C_HS:
704 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
707 case USB_STAT_ACA_RID_A:
709 * Dedicated charger level minus maximum current accessory
710 * can consume (900mA). Closest level is 500mA
712 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
713 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
716 case USB_STAT_ACA_RID_B:
718 * Dedicated charger level minus 120mA (20mA for ACA and
719 * 100mA for potential accessory). Closest level is 1300mA
721 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
722 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
723 di->max_usb_in_curr.usb_type_max);
726 case USB_STAT_HOST_CHG_NM:
727 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
730 case USB_STAT_DEDICATED_CHG:
731 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
734 case USB_STAT_ACA_RID_C_HS_CHIRP:
735 case USB_STAT_ACA_RID_C_NM:
736 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
739 case USB_STAT_NOT_CONFIGURED:
740 if (di->vbus_detected) {
741 di->usb_device_is_unrecognised = true;
742 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
743 di->max_usb_in_curr.usb_type_max =
744 USB_CH_IP_CUR_LVL_1P5;
747 case USB_STAT_HM_IDGND:
748 dev_err(di->dev, "USB Type - Charging not allowed\n");
749 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
752 case USB_STAT_RESERVED:
753 if (is_ab8500(di->parent)) {
754 di->flags.vbus_collapse = true;
755 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
756 "VBUS has collapsed\n");
760 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
761 di->max_usb_in_curr.usb_type_max =
762 USB_CH_IP_CUR_LVL_0P05;
763 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
765 di->max_usb_in_curr.usb_type_max);
770 case USB_STAT_CARKIT_1:
771 case USB_STAT_CARKIT_2:
772 case USB_STAT_ACA_DOCK_CHARGER:
773 case USB_STAT_CHARGER_LINE_1:
774 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
775 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
776 di->max_usb_in_curr.usb_type_max);
777 case USB_STAT_NOT_VALID_LINK:
778 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
779 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
783 dev_err(di->dev, "USB Type - Unknown\n");
784 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
789 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
790 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
791 link_status, di->max_usb_in_curr.set_max);
797 * ab8500_charger_read_usb_type() - read the type of usb connected
798 * @di: pointer to the ab8500_charger structure
800 * Detect the type of the plugged USB
801 * Returns error code in case of failure else 0 on success
803 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
808 ret = abx500_get_register_interruptible(di->dev,
809 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
811 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
814 if (is_ab8500(di->parent))
815 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
816 AB8500_USB_LINE_STAT_REG, &val);
818 ret = abx500_get_register_interruptible(di->dev,
819 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
821 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
825 /* get the USB type */
826 if (is_ab8500(di->parent))
827 val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
829 val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
830 ret = ab8500_charger_max_usb_curr(di,
831 (enum ab8500_charger_link_status) val);
837 * ab8500_charger_detect_usb_type() - get the type of usb connected
838 * @di: pointer to the ab8500_charger structure
840 * Detect the type of the plugged USB
841 * Returns error code in case of failure else 0 on success
843 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
849 * On getting the VBUS rising edge detect interrupt there
850 * is a 250ms delay after which the register UsbLineStatus
851 * is filled with valid data.
853 for (i = 0; i < 10; i++) {
855 ret = abx500_get_register_interruptible(di->dev,
856 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
858 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
861 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
865 if (is_ab8500(di->parent))
866 ret = abx500_get_register_interruptible(di->dev,
867 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
869 ret = abx500_get_register_interruptible(di->dev,
870 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
872 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
875 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
878 * Until the IT source register is read the UsbLineStatus
879 * register is not updated, hence doing the same
883 /* get the USB type */
884 if (is_ab8500(di->parent))
885 val = (val & AB8500_USB_LINK_STATUS) >>
886 USB_LINK_STATUS_SHIFT;
888 val = (val & AB8505_USB_LINK_STATUS) >>
889 USB_LINK_STATUS_SHIFT;
893 ret = ab8500_charger_max_usb_curr(di,
894 (enum ab8500_charger_link_status) val);
900 * This array maps the raw hex value to charger voltage used by the AB8500
901 * Values taken from the UM0836
903 static int ab8500_charger_voltage_map[] = {
984 static int ab8500_voltage_to_regval(int voltage)
988 /* Special case for voltage below 3.5V */
989 if (voltage < ab8500_charger_voltage_map[0])
992 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
993 if (voltage < ab8500_charger_voltage_map[i])
997 /* If not last element, return error */
998 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
999 if (voltage == ab8500_charger_voltage_map[i])
1005 static int ab8500_current_to_regval(struct ab8500_charger *di, int curr)
1009 if (curr < di->bm->chg_output_curr[0])
1012 for (i = 0; i < di->bm->n_chg_out_curr; i++) {
1013 if (curr < di->bm->chg_output_curr[i])
1017 /* If not last element, return error */
1018 i = di->bm->n_chg_out_curr - 1;
1019 if (curr == di->bm->chg_output_curr[i])
1025 static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr)
1029 if (curr < di->bm->chg_input_curr[0])
1032 for (i = 0; i < di->bm->n_chg_in_curr; i++) {
1033 if (curr < di->bm->chg_input_curr[i])
1037 /* If not last element, return error */
1038 i = di->bm->n_chg_in_curr - 1;
1039 if (curr == di->bm->chg_input_curr[i])
1046 * ab8500_charger_get_usb_cur() - get usb current
1047 * @di: pointer to the ab8500_charger structre
1049 * The usb stack provides the maximum current that can be drawn from
1050 * the standard usb host. This will be in mA.
1051 * This function converts current in mA to a value that can be written
1052 * to the register. Returns -1 if charging is not allowed
1054 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1057 switch (di->usb_state.usb_current) {
1059 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
1062 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
1065 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
1068 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
1071 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
1074 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
1078 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
1083 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1084 * @di: pointer to the ab8500_charger structure
1085 * @reg: select what charger register to check
1087 * Check if current stepping should be allowed to continue.
1088 * Checks if charger source has not collapsed. If it has, further stepping
1091 static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1094 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1095 return !di->flags.vbus_drop_end;
1101 * ab8500_charger_set_current() - set charger current
1102 * @di: pointer to the ab8500_charger structure
1103 * @ich: charger current, in mA
1104 * @reg: select what charger register to set
1106 * Set charger current.
1107 * There is no state machine in the AB to step up/down the charger
1108 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1109 * charging is started. Instead we need to implement
1110 * this charger current step-up/down here.
1111 * Returns error code in case of failure else 0(on success)
1113 static int ab8500_charger_set_current(struct ab8500_charger *di,
1117 int curr_index, prev_curr_index, shift_value, i;
1120 bool no_stepping = false;
1122 atomic_inc(&di->current_stepping_sessions);
1124 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1127 dev_err(di->dev, "%s read failed\n", __func__);
1128 goto exit_set_current;
1132 case AB8500_MCH_IPT_CURLVL_REG:
1133 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1134 prev_curr_index = (reg_value >> shift_value);
1135 curr_index = ab8500_current_to_regval(di, ich);
1136 step_udelay = STEP_UDELAY;
1137 if (!di->ac.charger_connected)
1140 case AB8500_USBCH_IPT_CRNTLVL_REG:
1141 if (is_ab8540(di->parent))
1142 shift_value = AB8540_VBUS_IN_CURR_LIM_SHIFT;
1144 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1145 prev_curr_index = (reg_value >> shift_value);
1146 curr_index = ab8500_vbus_in_curr_to_regval(di, ich);
1147 step_udelay = STEP_UDELAY * 100;
1149 if (!di->usb.charger_connected)
1152 case AB8500_CH_OPT_CRNTLVL_REG:
1154 prev_curr_index = (reg_value >> shift_value);
1155 curr_index = ab8500_current_to_regval(di, ich);
1156 step_udelay = STEP_UDELAY;
1157 if (curr_index && (curr_index - prev_curr_index) > 1)
1160 if (!di->usb.charger_connected && !di->ac.charger_connected)
1165 dev_err(di->dev, "%s current register not valid\n", __func__);
1167 goto exit_set_current;
1170 if (curr_index < 0) {
1171 dev_err(di->dev, "requested current limit out-of-range\n");
1173 goto exit_set_current;
1176 /* only update current if it's been changed */
1177 if (prev_curr_index == curr_index) {
1178 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1181 goto exit_set_current;
1184 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1185 __func__, ich, reg);
1188 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1189 reg, (u8)curr_index << shift_value);
1191 dev_err(di->dev, "%s write failed\n", __func__);
1192 } else if (prev_curr_index > curr_index) {
1193 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1194 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1195 (u8) i << shift_value, reg);
1196 ret = abx500_set_register_interruptible(di->dev,
1197 AB8500_CHARGER, reg, (u8)i << shift_value);
1199 dev_err(di->dev, "%s write failed\n", __func__);
1200 goto exit_set_current;
1202 if (i != curr_index)
1203 usleep_range(step_udelay, step_udelay * 2);
1207 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
1208 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1209 (u8)i << shift_value, reg);
1210 ret = abx500_set_register_interruptible(di->dev,
1211 AB8500_CHARGER, reg, (u8)i << shift_value);
1213 dev_err(di->dev, "%s write failed\n", __func__);
1214 goto exit_set_current;
1216 if (i != curr_index)
1217 usleep_range(step_udelay, step_udelay * 2);
1219 allow = ab8500_charger_check_continue_stepping(di, reg);
1224 atomic_dec(&di->current_stepping_sessions);
1230 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1231 * @di: pointer to the ab8500_charger structure
1232 * @ich_in: charger input current limit
1234 * Sets the current that can be drawn from the USB host
1235 * Returns error code in case of failure else 0(on success)
1237 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1243 /* We should always use to lowest current limit */
1244 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1245 if (di->max_usb_in_curr.set_max > 0)
1246 min_value = min(di->max_usb_in_curr.set_max, min_value);
1248 if (di->usb_state.usb_current >= 0)
1249 min_value = min(di->usb_state.usb_current, min_value);
1251 switch (min_value) {
1253 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1254 min_value = USB_CH_IP_CUR_LVL_0P05;
1257 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1258 min_value = USB_CH_IP_CUR_LVL_0P45;
1264 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1266 mutex_lock(&di->usb_ipt_crnt_lock);
1267 ret = ab8500_charger_set_current(di, min_value,
1268 AB8500_USBCH_IPT_CRNTLVL_REG);
1269 mutex_unlock(&di->usb_ipt_crnt_lock);
1275 * ab8500_charger_set_main_in_curr() - set main charger input current
1276 * @di: pointer to the ab8500_charger structure
1277 * @ich_in: input charger current, in mA
1279 * Set main charger input current.
1280 * Returns error code in case of failure else 0(on success)
1282 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1285 return ab8500_charger_set_current(di, ich_in,
1286 AB8500_MCH_IPT_CURLVL_REG);
1290 * ab8500_charger_set_output_curr() - set charger output current
1291 * @di: pointer to the ab8500_charger structure
1292 * @ich_out: output charger current, in mA
1294 * Set charger output current.
1295 * Returns error code in case of failure else 0(on success)
1297 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1300 return ab8500_charger_set_current(di, ich_out,
1301 AB8500_CH_OPT_CRNTLVL_REG);
1305 * ab8500_charger_led_en() - turn on/off chargign led
1306 * @di: pointer to the ab8500_charger structure
1307 * @on: flag to turn on/off the chargign led
1309 * Power ON/OFF charging LED indication
1310 * Returns error code in case of failure else 0(on success)
1312 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1317 /* Power ON charging LED indicator, set LED current to 5mA */
1318 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1319 AB8500_LED_INDICATOR_PWM_CTRL,
1320 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1322 dev_err(di->dev, "Power ON LED failed\n");
1325 /* LED indicator PWM duty cycle 252/256 */
1326 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1327 AB8500_LED_INDICATOR_PWM_DUTY,
1328 LED_INDICATOR_PWM_DUTY_252_256);
1330 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1334 /* Power off charging LED indicator */
1335 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1336 AB8500_LED_INDICATOR_PWM_CTRL,
1337 LED_INDICATOR_PWM_DIS);
1339 dev_err(di->dev, "Power-off LED failed\n");
1348 * ab8500_charger_ac_en() - enable or disable ac charging
1349 * @di: pointer to the ab8500_charger structure
1350 * @enable: enable/disable flag
1351 * @vset: charging voltage
1352 * @iset: charging current
1354 * Enable/Disable AC/Mains charging and turns on/off the charging led
1357 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1358 int enable, int vset, int iset)
1363 int input_curr_index;
1366 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1369 /* Check if AC is connected */
1370 if (!di->ac.charger_connected) {
1371 dev_err(di->dev, "AC charger not connected\n");
1375 /* Enable AC charging */
1376 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1379 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1380 * will be triggered everytime we enable the VDD ADC supply.
1381 * This will turn off charging for a short while.
1382 * It can be avoided by having the supply on when
1383 * there is a charger enabled. Normally the VDD ADC supply
1384 * is enabled everytime a GPADC conversion is triggered. We will
1385 * force it to be enabled from this driver to have
1386 * the GPADC module independant of the AB8500 chargers
1388 if (!di->vddadc_en_ac) {
1389 regulator_enable(di->regu);
1390 di->vddadc_en_ac = true;
1393 /* Check if the requested voltage or current is valid */
1394 volt_index = ab8500_voltage_to_regval(vset);
1395 curr_index = ab8500_current_to_regval(di, iset);
1396 input_curr_index = ab8500_current_to_regval(di,
1397 di->bm->chg_params->ac_curr_max);
1398 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1400 "Charger voltage or current too high, "
1401 "charging not started\n");
1405 /* ChVoltLevel: maximum battery charging voltage */
1406 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1407 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1409 dev_err(di->dev, "%s write failed\n", __func__);
1412 /* MainChInputCurr: current that can be drawn from the charger*/
1413 ret = ab8500_charger_set_main_in_curr(di,
1414 di->bm->chg_params->ac_curr_max);
1416 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1420 /* ChOutputCurentLevel: protected output current */
1421 ret = ab8500_charger_set_output_curr(di, iset);
1423 dev_err(di->dev, "%s "
1424 "Failed to set ChOutputCurentLevel\n",
1429 /* Check if VBAT overshoot control should be enabled */
1430 if (!di->bm->enable_overshoot)
1431 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1433 /* Enable Main Charger */
1434 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1435 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1437 dev_err(di->dev, "%s write failed\n", __func__);
1441 /* Power on charging LED indication */
1442 ret = ab8500_charger_led_en(di, true);
1444 dev_err(di->dev, "failed to enable LED\n");
1446 di->ac.charger_online = 1;
1448 /* Disable AC charging */
1449 if (is_ab8500_1p1_or_earlier(di->parent)) {
1451 * For ABB revision 1.0 and 1.1 there is a bug in the
1452 * watchdog logic. That means we have to continously
1453 * kick the charger watchdog even when no charger is
1454 * connected. This is only valid once the AC charger
1455 * has been enabled. This is a bug that is not handled
1456 * by the algorithm and the watchdog have to be kicked
1457 * by the charger driver when the AC charger
1461 queue_delayed_work(di->charger_wq,
1463 round_jiffies(WD_KICK_INTERVAL));
1467 * We can't turn off charging completely
1468 * due to a bug in AB8500 cut1.
1469 * If we do, charging will not start again.
1470 * That is why we set the lowest voltage
1471 * and current possible
1473 ret = abx500_set_register_interruptible(di->dev,
1475 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1478 "%s write failed\n", __func__);
1482 ret = ab8500_charger_set_output_curr(di, 0);
1484 dev_err(di->dev, "%s "
1485 "Failed to set ChOutputCurentLevel\n",
1490 ret = abx500_set_register_interruptible(di->dev,
1492 AB8500_MCH_CTRL1, 0);
1495 "%s write failed\n", __func__);
1500 ret = ab8500_charger_led_en(di, false);
1502 dev_err(di->dev, "failed to disable LED\n");
1504 di->ac.charger_online = 0;
1505 di->ac.wd_expired = false;
1507 /* Disable regulator if enabled */
1508 if (di->vddadc_en_ac) {
1509 regulator_disable(di->regu);
1510 di->vddadc_en_ac = false;
1513 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1515 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1521 * ab8500_charger_usb_en() - enable usb charging
1522 * @di: pointer to the ab8500_charger structure
1523 * @enable: enable/disable flag
1524 * @vset: charging voltage
1525 * @ich_out: charger output current
1527 * Enable/Disable USB charging and turns on/off the charging led respectively.
1528 * Returns error code in case of failure else 0(on success)
1530 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1531 int enable, int vset, int ich_out)
1538 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1541 /* Check if USB is connected */
1542 if (!di->usb.charger_connected) {
1543 dev_err(di->dev, "USB charger not connected\n");
1548 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1549 * will be triggered everytime we enable the VDD ADC supply.
1550 * This will turn off charging for a short while.
1551 * It can be avoided by having the supply on when
1552 * there is a charger enabled. Normally the VDD ADC supply
1553 * is enabled everytime a GPADC conversion is triggered. We will
1554 * force it to be enabled from this driver to have
1555 * the GPADC module independant of the AB8500 chargers
1557 if (!di->vddadc_en_usb) {
1558 regulator_enable(di->regu);
1559 di->vddadc_en_usb = true;
1562 /* Enable USB charging */
1563 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1565 /* Check if the requested voltage or current is valid */
1566 volt_index = ab8500_voltage_to_regval(vset);
1567 curr_index = ab8500_current_to_regval(di, ich_out);
1568 if (volt_index < 0 || curr_index < 0) {
1570 "Charger voltage or current too high, "
1571 "charging not started\n");
1575 /* ChVoltLevel: max voltage upto which battery can be charged */
1576 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1577 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1579 dev_err(di->dev, "%s write failed\n", __func__);
1582 /* Check if VBAT overshoot control should be enabled */
1583 if (!di->bm->enable_overshoot)
1584 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1586 /* Enable USB Charger */
1588 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
1589 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1590 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1592 dev_err(di->dev, "%s write failed\n", __func__);
1596 /* If success power on charging LED indication */
1597 ret = ab8500_charger_led_en(di, true);
1599 dev_err(di->dev, "failed to enable LED\n");
1601 di->usb.charger_online = 1;
1603 /* USBChInputCurr: current that can be drawn from the usb */
1604 ret = ab8500_charger_set_vbus_in_curr(di,
1605 di->max_usb_in_curr.usb_type_max);
1607 dev_err(di->dev, "setting USBChInputCurr failed\n");
1611 /* ChOutputCurentLevel: protected output current */
1612 ret = ab8500_charger_set_output_curr(di, ich_out);
1614 dev_err(di->dev, "%s "
1615 "Failed to set ChOutputCurentLevel\n",
1620 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1623 /* Disable USB charging */
1624 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1625 ret = abx500_set_register_interruptible(di->dev,
1627 AB8500_USBCH_CTRL1_REG, 0);
1630 "%s write failed\n", __func__);
1634 ret = ab8500_charger_led_en(di, false);
1636 dev_err(di->dev, "failed to disable LED\n");
1637 /* USBChInputCurr: current that can be drawn from the usb */
1638 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1640 dev_err(di->dev, "setting USBChInputCurr failed\n");
1644 /* ChOutputCurentLevel: protected output current */
1645 ret = ab8500_charger_set_output_curr(di, 0);
1647 dev_err(di->dev, "%s "
1648 "Failed to reset ChOutputCurentLevel\n",
1652 di->usb.charger_online = 0;
1653 di->usb.wd_expired = false;
1655 /* Disable regulator if enabled */
1656 if (di->vddadc_en_usb) {
1657 regulator_disable(di->regu);
1658 di->vddadc_en_usb = false;
1661 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1663 /* Cancel any pending Vbat check work */
1664 cancel_delayed_work(&di->check_vbat_work);
1667 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1672 static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1673 unsigned long event, void *data)
1676 struct device *dev = data;
1677 /*Toggle External charger control pin*/
1678 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1679 AB8500_SYS_CHARGER_CONTROL_REG,
1680 EXTERNAL_CHARGER_DISABLE_REG_VAL);
1682 dev_err(dev, "write reg failed %d\n", ret);
1685 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1686 AB8500_SYS_CHARGER_CONTROL_REG,
1687 EXTERNAL_CHARGER_ENABLE_REG_VAL);
1689 dev_err(dev, "Write reg failed %d\n", ret);
1696 * ab8500_charger_usb_check_enable() - enable usb charging
1697 * @charger: pointer to the ux500_charger structure
1698 * @vset: charging voltage
1699 * @iset: charger output current
1701 * Check if the VBUS charger has been disconnected and reconnected without
1702 * AB8500 rising an interrupt. Returns 0 on success.
1704 static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1710 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1712 if (!di->usb.charger_connected)
1715 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1716 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1718 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1721 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1723 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1724 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1726 ret = abx500_mask_and_set_register_interruptible(di->dev,
1727 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1728 DROP_COUNT_RESET, DROP_COUNT_RESET);
1730 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1734 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1736 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1745 * ab8500_charger_ac_check_enable() - enable usb charging
1746 * @charger: pointer to the ux500_charger structure
1747 * @vset: charging voltage
1748 * @iset: charger output current
1750 * Check if the AC charger has been disconnected and reconnected without
1751 * AB8500 rising an interrupt. Returns 0 on success.
1753 static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1756 u8 mainch_ctrl1 = 0;
1759 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1761 if (!di->ac.charger_connected)
1764 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1765 AB8500_MCH_CTRL1, &mainch_ctrl1);
1767 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1770 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1772 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1773 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1775 ret = abx500_mask_and_set_register_interruptible(di->dev,
1776 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1777 DROP_COUNT_RESET, DROP_COUNT_RESET);
1780 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1784 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1786 dev_err(di->dev, "failed to enable AC charger %d\n",
1795 * ab8500_charger_watchdog_kick() - kick charger watchdog
1796 * @di: pointer to the ab8500_charger structure
1798 * Kick charger watchdog
1799 * Returns error code in case of failure else 0(on success)
1801 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1804 struct ab8500_charger *di;
1806 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1807 di = to_ab8500_charger_ac_device_info(charger);
1808 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1809 di = to_ab8500_charger_usb_device_info(charger);
1813 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1814 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1816 dev_err(di->dev, "Failed to kick WD!\n");
1822 * ab8500_charger_update_charger_current() - update charger current
1823 * @di: pointer to the ab8500_charger structure
1825 * Update the charger output current for the specified charger
1826 * Returns error code in case of failure else 0(on success)
1828 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1832 struct ab8500_charger *di;
1834 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1835 di = to_ab8500_charger_ac_device_info(charger);
1836 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1837 di = to_ab8500_charger_usb_device_info(charger);
1841 ret = ab8500_charger_set_output_curr(di, ich_out);
1843 dev_err(di->dev, "%s "
1844 "Failed to set ChOutputCurentLevel\n",
1849 /* Reset the main and usb drop input current measurement counter */
1850 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1851 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
1853 dev_err(di->dev, "%s write failed\n", __func__);
1861 * ab8540_charger_power_path_enable() - enable usb power path mode
1862 * @charger: pointer to the ux500_charger structure
1863 * @enable: enable/disable flag
1865 * Enable or disable the power path for usb mode
1866 * Returns error code in case of failure else 0(on success)
1868 static int ab8540_charger_power_path_enable(struct ux500_charger *charger,
1872 struct ab8500_charger *di;
1874 if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1875 di = to_ab8500_charger_usb_device_info(charger);
1879 ret = abx500_mask_and_set_register_interruptible(di->dev,
1880 AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
1881 BUS_POWER_PATH_MODE_ENA, enable);
1883 dev_err(di->dev, "%s write failed\n", __func__);
1892 * ab8540_charger_usb_pre_chg_enable() - enable usb pre change
1893 * @charger: pointer to the ux500_charger structure
1894 * @enable: enable/disable flag
1896 * Enable or disable the pre-chage for usb mode
1897 * Returns error code in case of failure else 0(on success)
1899 static int ab8540_charger_usb_pre_chg_enable(struct ux500_charger *charger,
1903 struct ab8500_charger *di;
1905 if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1906 di = to_ab8500_charger_usb_device_info(charger);
1910 ret = abx500_mask_and_set_register_interruptible(di->dev,
1911 AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
1912 BUS_POWER_PATH_PRECHG_ENA, enable);
1914 dev_err(di->dev, "%s write failed\n", __func__);
1921 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1923 struct power_supply *psy;
1924 struct power_supply *ext;
1925 struct ab8500_charger *di;
1926 union power_supply_propval ret;
1928 bool psy_found = false;
1929 struct ux500_charger *usb_chg;
1931 usb_chg = (struct ux500_charger *)data;
1932 psy = &usb_chg->psy;
1934 di = to_ab8500_charger_usb_device_info(usb_chg);
1936 ext = dev_get_drvdata(dev);
1938 /* For all psy where the driver name appears in any supplied_to */
1939 for (i = 0; i < ext->num_supplicants; i++) {
1940 if (!strcmp(ext->supplied_to[i], psy->name))
1947 /* Go through all properties for the psy */
1948 for (j = 0; j < ext->num_properties; j++) {
1949 enum power_supply_property prop;
1950 prop = ext->properties[j];
1952 if (ext->get_property(ext, prop, &ret))
1956 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1957 switch (ext->type) {
1958 case POWER_SUPPLY_TYPE_BATTERY:
1959 di->vbat = ret.intval / 1000;
1973 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1974 * @work pointer to the work_struct structure
1976 * Due to a asic bug it is necessary to lower the input current to the vbus
1977 * charger when charging with at some specific levels. This issue is only valid
1978 * for below a certain battery voltage. This function makes sure that the
1979 * the allowed current limit isn't exceeded.
1981 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1984 struct ab8500_charger *di = container_of(work,
1985 struct ab8500_charger, check_vbat_work.work);
1987 class_for_each_device(power_supply_class, NULL,
1988 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1990 /* First run old_vbat is 0. */
1991 if (di->old_vbat == 0)
1992 di->old_vbat = di->vbat;
1994 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1995 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1996 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1997 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1999 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
2000 " old: %d\n", di->max_usb_in_curr.usb_type_max,
2001 di->vbat, di->old_vbat);
2002 ab8500_charger_set_vbus_in_curr(di,
2003 di->max_usb_in_curr.usb_type_max);
2004 power_supply_changed(&di->usb_chg.psy);
2007 di->old_vbat = di->vbat;
2010 * No need to check the battery voltage every second when not close to
2013 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
2014 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
2017 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
2021 * ab8500_charger_check_hw_failure_work() - check main charger failure
2022 * @work: pointer to the work_struct structure
2024 * Work queue function for checking the main charger status
2026 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
2031 struct ab8500_charger *di = container_of(work,
2032 struct ab8500_charger, check_hw_failure_work.work);
2034 /* Check if the status bits for HW failure is still active */
2035 if (di->flags.mainextchnotok) {
2036 ret = abx500_get_register_interruptible(di->dev,
2037 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2039 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2042 if (!(reg_value & MAIN_CH_NOK)) {
2043 di->flags.mainextchnotok = false;
2044 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2047 if (di->flags.vbus_ovv) {
2048 ret = abx500_get_register_interruptible(di->dev,
2049 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
2052 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2055 if (!(reg_value & VBUS_OVV_TH)) {
2056 di->flags.vbus_ovv = false;
2057 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2060 /* If we still have a failure, schedule a new check */
2061 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2062 queue_delayed_work(di->charger_wq,
2063 &di->check_hw_failure_work, round_jiffies(HZ));
2068 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2069 * @work: pointer to the work_struct structure
2071 * Work queue function for kicking the charger watchdog.
2073 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2074 * logic. That means we have to continously kick the charger
2075 * watchdog even when no charger is connected. This is only
2076 * valid once the AC charger has been enabled. This is
2077 * a bug that is not handled by the algorithm and the
2078 * watchdog have to be kicked by the charger driver
2079 * when the AC charger is disabled
2081 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2085 struct ab8500_charger *di = container_of(work,
2086 struct ab8500_charger, kick_wd_work.work);
2088 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2089 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2091 dev_err(di->dev, "Failed to kick WD!\n");
2093 /* Schedule a new watchdog kick */
2094 queue_delayed_work(di->charger_wq,
2095 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2099 * ab8500_charger_ac_work() - work to get and set main charger status
2100 * @work: pointer to the work_struct structure
2102 * Work queue function for checking the main charger status
2104 static void ab8500_charger_ac_work(struct work_struct *work)
2108 struct ab8500_charger *di = container_of(work,
2109 struct ab8500_charger, ac_work);
2112 * Since we can't be sure that the events are received
2113 * synchronously, we have the check if the main charger is
2114 * connected by reading the status register
2116 ret = ab8500_charger_detect_chargers(di, false);
2120 if (ret & AC_PW_CONN) {
2121 di->ac.charger_connected = 1;
2124 di->ac.charger_connected = 0;
2127 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2128 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2131 static void ab8500_charger_usb_attached_work(struct work_struct *work)
2133 struct ab8500_charger *di = container_of(work,
2134 struct ab8500_charger,
2135 usb_charger_attached_work.work);
2136 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2140 for (i = 0; i < 10; i++) {
2141 ret = abx500_get_register_interruptible(di->dev,
2143 AB8500_CH_USBCH_STAT1_REG,
2146 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2149 if ((statval & usbch) != usbch)
2152 msleep(CHARGER_STATUS_POLL);
2155 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2157 mutex_lock(&di->charger_attached_mutex);
2158 mutex_unlock(&di->charger_attached_mutex);
2163 queue_delayed_work(di->charger_wq,
2164 &di->usb_charger_attached_work,
2168 static void ab8500_charger_ac_attached_work(struct work_struct *work)
2171 struct ab8500_charger *di = container_of(work,
2172 struct ab8500_charger,
2173 ac_charger_attached_work.work);
2174 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2175 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2179 for (i = 0; i < 10; i++) {
2180 ret = abx500_get_register_interruptible(di->dev,
2182 AB8500_CH_STATUS2_REG,
2185 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2189 if ((statval & mainch) != mainch)
2192 msleep(CHARGER_STATUS_POLL);
2195 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2196 queue_work(di->charger_wq, &di->ac_work);
2198 mutex_lock(&di->charger_attached_mutex);
2199 mutex_unlock(&di->charger_attached_mutex);
2204 queue_delayed_work(di->charger_wq,
2205 &di->ac_charger_attached_work,
2210 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2211 * @work: Pointer to the work_struct structure
2213 * Detect the type of USB plugged
2215 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
2219 struct ab8500_charger *di = container_of(work,
2220 struct ab8500_charger, detect_usb_type_work);
2223 * Since we can't be sure that the events are received
2224 * synchronously, we have the check if is
2225 * connected by reading the status register
2227 ret = ab8500_charger_detect_chargers(di, false);
2231 if (!(ret & USB_PW_CONN)) {
2232 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2233 di->vbus_detected = false;
2234 ab8500_charger_set_usb_connected(di, false);
2235 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2237 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2238 di->vbus_detected = true;
2240 if (is_ab8500_1p1_or_earlier(di->parent)) {
2241 ret = ab8500_charger_detect_usb_type(di);
2243 ab8500_charger_set_usb_connected(di, true);
2244 ab8500_power_supply_changed(di,
2249 * For ABB cut2.0 and onwards we have an IRQ,
2250 * USB_LINK_STATUS that will be triggered when the USB
2251 * link status changes. The exception is USB connected
2252 * during startup. Then we don't get a
2253 * USB_LINK_STATUS IRQ
2255 if (di->vbus_detected_start) {
2256 di->vbus_detected_start = false;
2257 ret = ab8500_charger_detect_usb_type(di);
2259 ab8500_charger_set_usb_connected(di,
2261 ab8500_power_supply_changed(di,
2270 * ab8500_charger_usb_link_attach_work() - work to detect USB type
2271 * @work: pointer to the work_struct structure
2273 * Detect the type of USB plugged
2275 static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2277 struct ab8500_charger *di =
2278 container_of(work, struct ab8500_charger, attach_work.work);
2281 /* Update maximum input current if USB enumeration is not detected */
2282 if (!di->usb.charger_online) {
2283 ret = ab8500_charger_set_vbus_in_curr(di,
2284 di->max_usb_in_curr.usb_type_max);
2289 ab8500_charger_set_usb_connected(di, true);
2290 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2294 * ab8500_charger_usb_link_status_work() - work to detect USB type
2295 * @work: pointer to the work_struct structure
2297 * Detect the type of USB plugged
2299 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2301 int detected_chargers;
2306 struct ab8500_charger *di = container_of(work,
2307 struct ab8500_charger, usb_link_status_work);
2310 * Since we can't be sure that the events are received
2311 * synchronously, we have the check if is
2312 * connected by reading the status register
2314 detected_chargers = ab8500_charger_detect_chargers(di, false);
2315 if (detected_chargers < 0)
2319 * Some chargers that breaks the USB spec is
2320 * identified as invalid by AB8500 and it refuse
2321 * to start the charging process. but by jumping
2322 * thru a few hoops it can be forced to start.
2324 if (is_ab8500(di->parent))
2325 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2326 AB8500_USB_LINE_STAT_REG, &val);
2328 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2329 AB8500_USB_LINK1_STAT_REG, &val);
2332 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2334 dev_dbg(di->dev, "Error reading USB link status\n");
2336 if (is_ab8500(di->parent))
2337 link_status = AB8500_USB_LINK_STATUS;
2339 link_status = AB8505_USB_LINK_STATUS;
2341 if (detected_chargers & USB_PW_CONN) {
2342 if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
2343 USB_STAT_NOT_VALID_LINK &&
2344 di->invalid_charger_detect_state == 0) {
2346 "Invalid charger detected, state= 0\n");
2348 abx500_mask_and_set_register_interruptible(di->dev,
2349 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
2350 USB_CH_ENA, USB_CH_ENA);
2351 /*Enable charger detection*/
2352 abx500_mask_and_set_register_interruptible(di->dev,
2353 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2354 USB_CH_DET, USB_CH_DET);
2355 di->invalid_charger_detect_state = 1;
2356 /*exit and wait for new link status interrupt.*/
2360 if (di->invalid_charger_detect_state == 1) {
2362 "Invalid charger detected, state= 1\n");
2363 /*Stop charger detection*/
2364 abx500_mask_and_set_register_interruptible(di->dev,
2365 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2367 /*Check link status*/
2368 if (is_ab8500(di->parent))
2369 ret = abx500_get_register_interruptible(di->dev,
2370 AB8500_USB, AB8500_USB_LINE_STAT_REG,
2373 ret = abx500_get_register_interruptible(di->dev,
2374 AB8500_USB, AB8500_USB_LINK1_STAT_REG,
2377 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2378 (val & link_status) >> USB_LINK_STATUS_SHIFT);
2379 di->invalid_charger_detect_state = 2;
2382 di->invalid_charger_detect_state = 0;
2385 if (!(detected_chargers & USB_PW_CONN)) {
2386 di->vbus_detected = false;
2387 ab8500_charger_set_usb_connected(di, false);
2388 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2392 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2393 di->vbus_detected = true;
2394 ret = ab8500_charger_read_usb_type(di);
2396 if (ret == -ENXIO) {
2397 /* No valid charger type detected */
2398 ab8500_charger_set_usb_connected(di, false);
2399 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2404 if (di->usb_device_is_unrecognised) {
2406 "Potential Legacy Charger device. "
2407 "Delay work for %d msec for USB enum "
2409 WAIT_ACA_RID_ENUMERATION);
2410 queue_delayed_work(di->charger_wq,
2412 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2413 } else if (di->is_aca_rid == 1) {
2414 /* Only wait once */
2417 "%s Wait %d msec for USB enum to finish",
2418 __func__, WAIT_ACA_RID_ENUMERATION);
2419 queue_delayed_work(di->charger_wq,
2421 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2423 queue_delayed_work(di->charger_wq,
2429 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2432 unsigned long flags;
2434 struct ab8500_charger *di = container_of(work,
2435 struct ab8500_charger, usb_state_changed_work.work);
2437 if (!di->vbus_detected) {
2439 "%s !di->vbus_detected\n",
2444 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2445 di->usb_state.state = di->usb_state.state_tmp;
2446 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
2447 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2449 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2450 __func__, di->usb_state.state, di->usb_state.usb_current);
2452 switch (di->usb_state.state) {
2453 case AB8500_BM_USB_STATE_RESET_HS:
2454 case AB8500_BM_USB_STATE_RESET_FS:
2455 case AB8500_BM_USB_STATE_SUSPEND:
2456 case AB8500_BM_USB_STATE_MAX:
2457 ab8500_charger_set_usb_connected(di, false);
2458 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2461 case AB8500_BM_USB_STATE_RESUME:
2463 * when suspend->resume there should be delay
2464 * of 1sec for enabling charging
2467 /* Intentional fall through */
2468 case AB8500_BM_USB_STATE_CONFIGURED:
2470 * USB is configured, enable charging with the charging
2471 * input current obtained from USB driver
2473 if (!ab8500_charger_get_usb_cur(di)) {
2474 /* Update maximum input current */
2475 ret = ab8500_charger_set_vbus_in_curr(di,
2476 di->max_usb_in_curr.usb_type_max);
2480 ab8500_charger_set_usb_connected(di, true);
2481 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2491 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2492 * @work: pointer to the work_struct structure
2494 * Work queue function for checking the USB charger Not OK status
2496 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2502 struct ab8500_charger *di = container_of(work,
2503 struct ab8500_charger, check_usbchgnotok_work.work);
2505 /* Check if the status bit for usbchargernotok is still active */
2506 ret = abx500_get_register_interruptible(di->dev,
2507 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2509 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2512 prev_status = di->flags.usbchargernotok;
2514 if (reg_value & VBUS_CH_NOK) {
2515 di->flags.usbchargernotok = true;
2516 /* Check again in 1sec */
2517 queue_delayed_work(di->charger_wq,
2518 &di->check_usbchgnotok_work, HZ);
2520 di->flags.usbchargernotok = false;
2521 di->flags.vbus_collapse = false;
2524 if (prev_status != di->flags.usbchargernotok)
2525 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2529 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2530 * @work: pointer to the work_struct structure
2532 * Work queue function for checking the Main thermal prot status
2534 static void ab8500_charger_check_main_thermal_prot_work(
2535 struct work_struct *work)
2540 struct ab8500_charger *di = container_of(work,
2541 struct ab8500_charger, check_main_thermal_prot_work);
2543 /* Check if the status bit for main_thermal_prot is still active */
2544 ret = abx500_get_register_interruptible(di->dev,
2545 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2547 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2550 if (reg_value & MAIN_CH_TH_PROT)
2551 di->flags.main_thermal_prot = true;
2553 di->flags.main_thermal_prot = false;
2555 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2559 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2560 * @work: pointer to the work_struct structure
2562 * Work queue function for checking the USB thermal prot status
2564 static void ab8500_charger_check_usb_thermal_prot_work(
2565 struct work_struct *work)
2570 struct ab8500_charger *di = container_of(work,
2571 struct ab8500_charger, check_usb_thermal_prot_work);
2573 /* Check if the status bit for usb_thermal_prot is still active */
2574 ret = abx500_get_register_interruptible(di->dev,
2575 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2577 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2580 if (reg_value & USB_CH_TH_PROT)
2581 di->flags.usb_thermal_prot = true;
2583 di->flags.usb_thermal_prot = false;
2585 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2589 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2590 * @irq: interrupt number
2591 * @_di: pointer to the ab8500_charger structure
2593 * Returns IRQ status(IRQ_HANDLED)
2595 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2597 struct ab8500_charger *di = _di;
2599 dev_dbg(di->dev, "Main charger unplugged\n");
2600 queue_work(di->charger_wq, &di->ac_work);
2602 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2603 mutex_lock(&di->charger_attached_mutex);
2604 mutex_unlock(&di->charger_attached_mutex);
2610 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2611 * @irq: interrupt number
2612 * @_di: pointer to the ab8500_charger structure
2614 * Returns IRQ status(IRQ_HANDLED)
2616 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2618 struct ab8500_charger *di = _di;
2620 dev_dbg(di->dev, "Main charger plugged\n");
2621 queue_work(di->charger_wq, &di->ac_work);
2623 mutex_lock(&di->charger_attached_mutex);
2624 mutex_unlock(&di->charger_attached_mutex);
2626 if (is_ab8500(di->parent))
2627 queue_delayed_work(di->charger_wq,
2628 &di->ac_charger_attached_work,
2634 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2635 * @irq: interrupt number
2636 * @_di: pointer to the ab8500_charger structure
2638 * Returns IRQ status(IRQ_HANDLED)
2640 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2642 struct ab8500_charger *di = _di;
2644 dev_dbg(di->dev, "Main charger not ok\n");
2645 di->flags.mainextchnotok = true;
2646 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2648 /* Schedule a new HW failure check */
2649 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2655 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2656 * thermal protection threshold
2657 * @irq: interrupt number
2658 * @_di: pointer to the ab8500_charger structure
2660 * Returns IRQ status(IRQ_HANDLED)
2662 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2664 struct ab8500_charger *di = _di;
2667 "Die temp above Main charger thermal protection threshold\n");
2668 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2674 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2675 * thermal protection threshold
2676 * @irq: interrupt number
2677 * @_di: pointer to the ab8500_charger structure
2679 * Returns IRQ status(IRQ_HANDLED)
2681 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2683 struct ab8500_charger *di = _di;
2686 "Die temp ok for Main charger thermal protection threshold\n");
2687 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2692 static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2694 struct ab8500_charger *di = container_of(work,
2695 struct ab8500_charger, vbus_drop_end_work.work);
2699 di->flags.vbus_drop_end = false;
2701 /* Reset the drop counter */
2702 abx500_set_register_interruptible(di->dev,
2703 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
2705 if (is_ab8540(di->parent))
2706 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2707 AB8540_CH_USBCH_STAT3_REG, ®_value);
2709 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2710 AB8500_CH_USBCH_STAT2_REG, ®_value);
2712 dev_err(di->dev, "%s read failed\n", __func__);
2716 if (is_ab8540(di->parent))
2717 curr = di->bm->chg_input_curr[
2718 reg_value & AB8540_AUTO_VBUS_IN_CURR_MASK];
2720 curr = di->bm->chg_input_curr[
2721 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
2723 if (di->max_usb_in_curr.calculated_max != curr) {
2724 /* USB source is collapsing */
2725 di->max_usb_in_curr.calculated_max = curr;
2727 "VBUS input current limiting to %d mA\n",
2728 di->max_usb_in_curr.calculated_max);
2731 * USB source can not give more than this amount.
2732 * Taking more will collapse the source.
2734 di->max_usb_in_curr.set_max =
2735 di->max_usb_in_curr.calculated_max;
2737 "VBUS input current limited to %d mA\n",
2738 di->max_usb_in_curr.set_max);
2741 if (di->usb.charger_connected)
2742 ab8500_charger_set_vbus_in_curr(di,
2743 di->max_usb_in_curr.usb_type_max);
2747 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2748 * @irq: interrupt number
2749 * @_di: pointer to the ab8500_charger structure
2751 * Returns IRQ status(IRQ_HANDLED)
2753 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2755 struct ab8500_charger *di = _di;
2757 di->vbus_detected = false;
2758 dev_dbg(di->dev, "VBUS falling detected\n");
2759 queue_work(di->charger_wq, &di->detect_usb_type_work);
2765 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2766 * @irq: interrupt number
2767 * @_di: pointer to the ab8500_charger structure
2769 * Returns IRQ status(IRQ_HANDLED)
2771 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2773 struct ab8500_charger *di = _di;
2775 di->vbus_detected = true;
2776 dev_dbg(di->dev, "VBUS rising detected\n");
2778 queue_work(di->charger_wq, &di->detect_usb_type_work);
2784 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2785 * @irq: interrupt number
2786 * @_di: pointer to the ab8500_charger structure
2788 * Returns IRQ status(IRQ_HANDLED)
2790 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2792 struct ab8500_charger *di = _di;
2794 dev_dbg(di->dev, "USB link status changed\n");
2796 queue_work(di->charger_wq, &di->usb_link_status_work);
2802 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2803 * thermal protection threshold
2804 * @irq: interrupt number
2805 * @_di: pointer to the ab8500_charger structure
2807 * Returns IRQ status(IRQ_HANDLED)
2809 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2811 struct ab8500_charger *di = _di;
2814 "Die temp above USB charger thermal protection threshold\n");
2815 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2821 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2822 * thermal protection threshold
2823 * @irq: interrupt number
2824 * @_di: pointer to the ab8500_charger structure
2826 * Returns IRQ status(IRQ_HANDLED)
2828 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2830 struct ab8500_charger *di = _di;
2833 "Die temp ok for USB charger thermal protection threshold\n");
2834 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2840 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2841 * @irq: interrupt number
2842 * @_di: pointer to the ab8500_charger structure
2844 * Returns IRQ status(IRQ_HANDLED)
2846 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2848 struct ab8500_charger *di = _di;
2850 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2851 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2857 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2858 * @irq: interrupt number
2859 * @_di: pointer to the ab8500_charger structure
2861 * Returns IRQ status(IRQ_HANDLED)
2863 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2865 struct ab8500_charger *di = _di;
2867 dev_dbg(di->dev, "Charger watchdog expired\n");
2870 * The charger that was online when the watchdog expired
2871 * needs to be restarted for charging to start again
2873 if (di->ac.charger_online) {
2874 di->ac.wd_expired = true;
2875 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2877 if (di->usb.charger_online) {
2878 di->usb.wd_expired = true;
2879 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2886 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2887 * @irq: interrupt number
2888 * @_di: pointer to the ab8500_charger structure
2890 * Returns IRQ status(IRQ_HANDLED)
2892 static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2894 struct ab8500_charger *di = _di;
2896 dev_dbg(di->dev, "VBUS charger drop ended\n");
2897 di->flags.vbus_drop_end = true;
2900 * VBUS might have dropped due to bad connection.
2901 * Schedule a new input limit set to the value SW requests.
2903 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
2904 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
2910 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2911 * @irq: interrupt number
2912 * @_di: pointer to the ab8500_charger structure
2914 * Returns IRQ status(IRQ_HANDLED)
2916 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2918 struct ab8500_charger *di = _di;
2920 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2921 di->flags.vbus_ovv = true;
2922 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2924 /* Schedule a new HW failure check */
2925 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2931 * ab8500_charger_ac_get_property() - get the ac/mains properties
2932 * @psy: pointer to the power_supply structure
2933 * @psp: pointer to the power_supply_property structure
2934 * @val: pointer to the power_supply_propval union
2936 * This function gets called when an application tries to get the ac/mains
2937 * properties by reading the sysfs files.
2938 * AC/Mains properties are online, present and voltage.
2939 * online: ac/mains charging is in progress or not
2940 * present: presence of the ac/mains
2941 * voltage: AC/Mains voltage
2942 * Returns error code in case of failure else 0(on success)
2944 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2945 enum power_supply_property psp,
2946 union power_supply_propval *val)
2948 struct ab8500_charger *di;
2951 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2954 case POWER_SUPPLY_PROP_HEALTH:
2955 if (di->flags.mainextchnotok)
2956 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2957 else if (di->ac.wd_expired || di->usb.wd_expired)
2958 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2959 else if (di->flags.main_thermal_prot)
2960 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2962 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2964 case POWER_SUPPLY_PROP_ONLINE:
2965 val->intval = di->ac.charger_online;
2967 case POWER_SUPPLY_PROP_PRESENT:
2968 val->intval = di->ac.charger_connected;
2970 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2971 ret = ab8500_charger_get_ac_voltage(di);
2973 di->ac.charger_voltage = ret;
2974 /* On error, use previous value */
2975 val->intval = di->ac.charger_voltage * 1000;
2977 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2979 * This property is used to indicate when CV mode is entered
2980 * for the AC charger
2982 di->ac.cv_active = ab8500_charger_ac_cv(di);
2983 val->intval = di->ac.cv_active;
2985 case POWER_SUPPLY_PROP_CURRENT_NOW:
2986 ret = ab8500_charger_get_ac_current(di);
2988 di->ac.charger_current = ret;
2989 val->intval = di->ac.charger_current * 1000;
2998 * ab8500_charger_usb_get_property() - get the usb properties
2999 * @psy: pointer to the power_supply structure
3000 * @psp: pointer to the power_supply_property structure
3001 * @val: pointer to the power_supply_propval union
3003 * This function gets called when an application tries to get the usb
3004 * properties by reading the sysfs files.
3005 * USB properties are online, present and voltage.
3006 * online: usb charging is in progress or not
3007 * present: presence of the usb
3008 * voltage: vbus voltage
3009 * Returns error code in case of failure else 0(on success)
3011 static int ab8500_charger_usb_get_property(struct power_supply *psy,
3012 enum power_supply_property psp,
3013 union power_supply_propval *val)
3015 struct ab8500_charger *di;
3018 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
3021 case POWER_SUPPLY_PROP_HEALTH:
3022 if (di->flags.usbchargernotok)
3023 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
3024 else if (di->ac.wd_expired || di->usb.wd_expired)
3025 val->intval = POWER_SUPPLY_HEALTH_DEAD;
3026 else if (di->flags.usb_thermal_prot)
3027 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
3028 else if (di->flags.vbus_ovv)
3029 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
3031 val->intval = POWER_SUPPLY_HEALTH_GOOD;
3033 case POWER_SUPPLY_PROP_ONLINE:
3034 val->intval = di->usb.charger_online;
3036 case POWER_SUPPLY_PROP_PRESENT:
3037 val->intval = di->usb.charger_connected;
3039 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
3040 ret = ab8500_charger_get_vbus_voltage(di);
3042 di->usb.charger_voltage = ret;
3043 val->intval = di->usb.charger_voltage * 1000;
3045 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
3047 * This property is used to indicate when CV mode is entered
3048 * for the USB charger
3050 di->usb.cv_active = ab8500_charger_usb_cv(di);
3051 val->intval = di->usb.cv_active;
3053 case POWER_SUPPLY_PROP_CURRENT_NOW:
3054 ret = ab8500_charger_get_usb_current(di);
3056 di->usb.charger_current = ret;
3057 val->intval = di->usb.charger_current * 1000;
3059 case POWER_SUPPLY_PROP_CURRENT_AVG:
3061 * This property is used to indicate when VBUS has collapsed
3062 * due to too high output current from the USB charger
3064 if (di->flags.vbus_collapse)
3076 * ab8500_charger_init_hw_registers() - Set up charger related registers
3077 * @di: pointer to the ab8500_charger structure
3079 * Set up charger OVV, watchdog and maximum voltage registers as well as
3080 * charging of the backup battery
3082 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3085 u8 bup_vch_range = 0, vbup33_vrtcn = 0;
3087 /* Setup maximum charger current and voltage for ABB cut2.0 */
3088 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3089 ret = abx500_set_register_interruptible(di->dev,
3091 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3094 "failed to set CH_VOLT_LVL_MAX_REG\n");
3098 if (is_ab8540(di->parent))
3099 ret = abx500_set_register_interruptible(di->dev,
3100 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3103 ret = abx500_set_register_interruptible(di->dev,
3104 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3108 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3113 if (is_ab9540_2p0(di->parent) || is_ab9540_3p0(di->parent)
3114 || is_ab8505_2p0(di->parent) || is_ab8540(di->parent))
3115 ret = abx500_mask_and_set_register_interruptible(di->dev,
3117 AB8500_USBCH_CTRL2_REG,
3118 VBUS_AUTO_IN_CURR_LIM_ENA,
3119 VBUS_AUTO_IN_CURR_LIM_ENA);
3122 * VBUS OVV set to 6.3V and enable automatic current limitation
3124 ret = abx500_set_register_interruptible(di->dev,
3126 AB8500_USBCH_CTRL2_REG,
3127 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
3130 "failed to set automatic current limitation\n");
3134 /* Enable main watchdog in OTP */
3135 ret = abx500_set_register_interruptible(di->dev,
3136 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3138 dev_err(di->dev, "failed to enable main WD in OTP\n");
3142 /* Enable main watchdog */
3143 ret = abx500_set_register_interruptible(di->dev,
3144 AB8500_SYS_CTRL2_BLOCK,
3145 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3147 dev_err(di->dev, "faile to enable main watchdog\n");
3152 * Due to internal synchronisation, Enable and Kick watchdog bits
3153 * cannot be enabled in a single write.
3154 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3155 * between writing Enable then Kick bits.
3159 /* Kick main watchdog */
3160 ret = abx500_set_register_interruptible(di->dev,
3161 AB8500_SYS_CTRL2_BLOCK,
3162 AB8500_MAIN_WDOG_CTRL_REG,
3163 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3165 dev_err(di->dev, "failed to kick main watchdog\n");
3169 /* Disable main watchdog */
3170 ret = abx500_set_register_interruptible(di->dev,
3171 AB8500_SYS_CTRL2_BLOCK,
3172 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3174 dev_err(di->dev, "failed to disable main watchdog\n");
3178 /* Set watchdog timeout */
3179 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3180 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3182 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3186 ret = ab8500_charger_led_en(di, false);
3188 dev_err(di->dev, "failed to disable LED\n");
3192 /* Backup battery voltage and current */
3193 if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V)
3194 bup_vch_range = BUP_VCH_RANGE;
3195 if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V)
3196 vbup33_vrtcn = VBUP33_VRTCN;
3198 ret = abx500_set_register_interruptible(di->dev,
3200 AB8500_RTC_BACKUP_CHG_REG,
3201 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
3203 dev_err(di->dev, "failed to setup backup battery charging\n");
3206 if (is_ab8540(di->parent)) {
3207 ret = abx500_set_register_interruptible(di->dev,
3209 AB8500_RTC_CTRL1_REG,
3210 bup_vch_range | vbup33_vrtcn);
3213 "failed to setup backup battery charging\n");
3218 /* Enable backup battery charging */
3219 abx500_mask_and_set_register_interruptible(di->dev,
3220 AB8500_RTC, AB8500_RTC_CTRL_REG,
3221 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3223 dev_err(di->dev, "%s mask and set failed\n", __func__);
3225 if (is_ab8540(di->parent)) {
3226 ret = abx500_mask_and_set_register_interruptible(di->dev,
3227 AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
3228 BUS_VSYS_VOL_SELECT_MASK, BUS_VSYS_VOL_SELECT_3P6V);
3231 "failed to setup usb power path vsys voltage\n");
3234 ret = abx500_mask_and_set_register_interruptible(di->dev,
3235 AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
3236 BUS_PP_PRECHG_CURRENT_MASK, 0);
3239 "failed to setup usb power path prechage current\n");
3249 * ab8500 charger driver interrupts and their respective isr
3251 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3252 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3253 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3254 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3255 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3256 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3257 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3258 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3259 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3260 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3261 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3262 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3263 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3264 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
3265 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
3268 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3269 unsigned long event, void *power)
3271 struct ab8500_charger *di =
3272 container_of(nb, struct ab8500_charger, nb);
3273 enum ab8500_usb_state bm_usb_state;
3274 unsigned mA = *((unsigned *)power);
3279 if (event != USB_EVENT_VBUS) {
3280 dev_dbg(di->dev, "not a standard host, returning\n");
3284 /* TODO: State is fabricate here. See if charger really needs USB
3285 * state or if mA is enough
3287 if ((di->usb_state.usb_current == 2) && (mA > 2))
3288 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3290 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3292 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3293 else if (mA >= 8) /* 8, 100, 500 */
3294 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3295 else /* Should never occur */
3296 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3298 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3299 __func__, bm_usb_state, mA);
3301 spin_lock(&di->usb_state.usb_lock);
3302 di->usb_state.state_tmp = bm_usb_state;
3303 di->usb_state.usb_current_tmp = mA;
3304 spin_unlock(&di->usb_state.usb_lock);
3307 * wait for some time until you get updates from the usb stack
3308 * and negotiations are completed
3310 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
3315 #if defined(CONFIG_PM)
3316 static int ab8500_charger_resume(struct platform_device *pdev)
3319 struct ab8500_charger *di = platform_get_drvdata(pdev);
3322 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3323 * logic. That means we have to continously kick the charger
3324 * watchdog even when no charger is connected. This is only
3325 * valid once the AC charger has been enabled. This is
3326 * a bug that is not handled by the algorithm and the
3327 * watchdog have to be kicked by the charger driver
3328 * when the AC charger is disabled
3330 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3331 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3332 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3334 dev_err(di->dev, "Failed to kick WD!\n");
3336 /* If not already pending start a new timer */
3337 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3338 round_jiffies(WD_KICK_INTERVAL));
3341 /* If we still have a HW failure, schedule a new check */
3342 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3343 queue_delayed_work(di->charger_wq,
3344 &di->check_hw_failure_work, 0);
3347 if (di->flags.vbus_drop_end)
3348 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3353 static int ab8500_charger_suspend(struct platform_device *pdev,
3356 struct ab8500_charger *di = platform_get_drvdata(pdev);
3358 /* Cancel any pending jobs */
3359 cancel_delayed_work(&di->check_hw_failure_work);
3360 cancel_delayed_work(&di->vbus_drop_end_work);
3362 flush_delayed_work(&di->attach_work);
3363 flush_delayed_work(&di->usb_charger_attached_work);
3364 flush_delayed_work(&di->ac_charger_attached_work);
3365 flush_delayed_work(&di->check_usbchgnotok_work);
3366 flush_delayed_work(&di->check_vbat_work);
3367 flush_delayed_work(&di->kick_wd_work);
3369 flush_work(&di->usb_link_status_work);
3370 flush_work(&di->ac_work);
3371 flush_work(&di->detect_usb_type_work);
3373 if (atomic_read(&di->current_stepping_sessions))
3379 #define ab8500_charger_suspend NULL
3380 #define ab8500_charger_resume NULL
3383 static struct notifier_block charger_nb = {
3384 .notifier_call = ab8500_external_charger_prepare,
3387 static int ab8500_charger_remove(struct platform_device *pdev)
3389 struct ab8500_charger *di = platform_get_drvdata(pdev);
3392 /* Disable AC charging */
3393 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3395 /* Disable USB charging */
3396 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3398 /* Disable interrupts */
3399 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3400 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3404 /* Backup battery voltage and current disable */
3405 ret = abx500_mask_and_set_register_interruptible(di->dev,
3406 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3408 dev_err(di->dev, "%s mask and set failed\n", __func__);
3410 usb_unregister_notifier(di->usb_phy, &di->nb);
3411 usb_put_phy(di->usb_phy);
3413 /* Delete the work queue */
3414 destroy_workqueue(di->charger_wq);
3416 /* Unregister external charger enable notifier */
3417 if (!di->ac_chg.enabled)
3418 blocking_notifier_chain_unregister(
3419 &charger_notifier_list, &charger_nb);
3421 flush_scheduled_work();
3422 if (di->usb_chg.enabled)
3423 power_supply_unregister(&di->usb_chg.psy);
3425 if (di->ac_chg.enabled && !di->ac_chg.external)
3426 power_supply_unregister(&di->ac_chg.psy);
3431 static char *supply_interface[] = {
3437 static int ab8500_charger_probe(struct platform_device *pdev)
3439 struct device_node *np = pdev->dev.of_node;
3440 struct abx500_bm_data *plat = pdev->dev.platform_data;
3441 struct ab8500_charger *di;
3442 int irq, i, charger_status, ret = 0, ch_stat;
3444 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3446 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
3451 dev_err(&pdev->dev, "no battery management data supplied\n");
3457 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3459 dev_err(&pdev->dev, "failed to get battery information\n");
3462 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3464 di->autopower_cfg = false;
3466 /* get parent data */
3467 di->dev = &pdev->dev;
3468 di->parent = dev_get_drvdata(pdev->dev.parent);
3469 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3471 /* initialize lock */
3472 spin_lock_init(&di->usb_state.usb_lock);
3473 mutex_init(&di->usb_ipt_crnt_lock);
3475 di->autopower = false;
3476 di->invalid_charger_detect_state = 0;
3479 /* power_supply base class */
3480 di->ac_chg.psy.name = "ab8500_ac";
3481 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
3482 di->ac_chg.psy.properties = ab8500_charger_ac_props;
3483 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
3484 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
3485 di->ac_chg.psy.supplied_to = supply_interface;
3486 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
3487 /* ux500_charger sub-class */
3488 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
3489 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
3490 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3491 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3492 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3493 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3494 di->ac_chg.max_out_curr =
3495 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3496 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
3497 di->ac_chg.enabled = di->bm->ac_enabled;
3498 di->ac_chg.external = false;
3500 /*notifier for external charger enabling*/
3501 if (!di->ac_chg.enabled)
3502 blocking_notifier_chain_register(
3503 &charger_notifier_list, &charger_nb);
3506 /* power_supply base class */
3507 di->usb_chg.psy.name = "ab8500_usb";
3508 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
3509 di->usb_chg.psy.properties = ab8500_charger_usb_props;
3510 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
3511 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
3512 di->usb_chg.psy.supplied_to = supply_interface;
3513 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
3514 /* ux500_charger sub-class */
3515 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
3516 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
3517 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3518 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3519 di->usb_chg.ops.pp_enable = &ab8540_charger_power_path_enable;
3520 di->usb_chg.ops.pre_chg_enable = &ab8540_charger_usb_pre_chg_enable;
3521 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3522 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3523 di->usb_chg.max_out_curr =
3524 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3525 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
3526 di->usb_chg.enabled = di->bm->usb_enabled;
3527 di->usb_chg.external = false;
3528 di->usb_chg.power_path = di->bm->usb_power_path;
3529 di->usb_state.usb_current = -1;
3531 /* Create a work queue for the charger */
3533 create_singlethread_workqueue("ab8500_charger_wq");
3534 if (di->charger_wq == NULL) {
3535 dev_err(di->dev, "failed to create work queue\n");
3539 mutex_init(&di->charger_attached_mutex);
3541 /* Init work for HW failure check */
3542 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
3543 ab8500_charger_check_hw_failure_work);
3544 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
3545 ab8500_charger_check_usbchargernotok_work);
3547 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3548 ab8500_charger_ac_attached_work);
3549 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3550 ab8500_charger_usb_attached_work);
3553 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3554 * logic. That means we have to continously kick the charger
3555 * watchdog even when no charger is connected. This is only
3556 * valid once the AC charger has been enabled. This is
3557 * a bug that is not handled by the algorithm and the
3558 * watchdog have to be kicked by the charger driver
3559 * when the AC charger is disabled
3561 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
3562 ab8500_charger_kick_watchdog_work);
3564 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
3565 ab8500_charger_check_vbat_work);
3567 INIT_DELAYED_WORK(&di->attach_work,
3568 ab8500_charger_usb_link_attach_work);
3570 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3571 ab8500_charger_usb_state_changed_work);
3573 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3574 ab8500_charger_vbus_drop_end_work);
3576 /* Init work for charger detection */
3577 INIT_WORK(&di->usb_link_status_work,
3578 ab8500_charger_usb_link_status_work);
3579 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3580 INIT_WORK(&di->detect_usb_type_work,
3581 ab8500_charger_detect_usb_type_work);
3583 /* Init work for checking HW status */
3584 INIT_WORK(&di->check_main_thermal_prot_work,
3585 ab8500_charger_check_main_thermal_prot_work);
3586 INIT_WORK(&di->check_usb_thermal_prot_work,
3587 ab8500_charger_check_usb_thermal_prot_work);
3590 * VDD ADC supply needs to be enabled from this driver when there
3591 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3592 * interrupts during charging
3594 di->regu = devm_regulator_get(di->dev, "vddadc");
3595 if (IS_ERR(di->regu)) {
3596 ret = PTR_ERR(di->regu);
3597 dev_err(di->dev, "failed to get vddadc regulator\n");
3598 goto free_charger_wq;
3602 /* Initialize OVV, and other registers */
3603 ret = ab8500_charger_init_hw_registers(di);
3605 dev_err(di->dev, "failed to initialize ABB registers\n");
3606 goto free_charger_wq;
3609 /* Register AC charger class */
3610 if (di->ac_chg.enabled) {
3611 ret = power_supply_register(di->dev, &di->ac_chg.psy);
3613 dev_err(di->dev, "failed to register AC charger\n");
3614 goto free_charger_wq;
3618 /* Register USB charger class */
3619 if (di->usb_chg.enabled) {
3620 ret = power_supply_register(di->dev, &di->usb_chg.psy);
3622 dev_err(di->dev, "failed to register USB charger\n");
3627 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3628 if (IS_ERR_OR_NULL(di->usb_phy)) {
3629 dev_err(di->dev, "failed to get usb transceiver\n");
3633 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3634 ret = usb_register_notifier(di->usb_phy, &di->nb);
3636 dev_err(di->dev, "failed to register usb notifier\n");
3640 /* Identify the connected charger types during startup */
3641 charger_status = ab8500_charger_detect_chargers(di, true);
3642 if (charger_status & AC_PW_CONN) {
3643 di->ac.charger_connected = 1;
3645 ab8500_power_supply_changed(di, &di->ac_chg.psy);
3646 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
3649 if (charger_status & USB_PW_CONN) {
3650 di->vbus_detected = true;
3651 di->vbus_detected_start = true;
3652 queue_work(di->charger_wq,
3653 &di->detect_usb_type_work);
3656 /* Register interrupts */
3657 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3658 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3659 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3660 IRQF_SHARED | IRQF_NO_SUSPEND,
3661 ab8500_charger_irq[i].name, di);
3664 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3665 , ab8500_charger_irq[i].name, irq, ret);
3668 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3669 ab8500_charger_irq[i].name, irq, ret);
3672 platform_set_drvdata(pdev, di);
3674 mutex_lock(&di->charger_attached_mutex);
3676 ch_stat = ab8500_charger_detect_chargers(di, false);
3678 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3679 if (is_ab8500(di->parent))
3680 queue_delayed_work(di->charger_wq,
3681 &di->ac_charger_attached_work,
3684 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3685 if (is_ab8500(di->parent))
3686 queue_delayed_work(di->charger_wq,
3687 &di->usb_charger_attached_work,
3691 mutex_unlock(&di->charger_attached_mutex);
3696 usb_unregister_notifier(di->usb_phy, &di->nb);
3698 /* We also have to free all successfully registered irqs */
3699 for (i = i - 1; i >= 0; i--) {
3700 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3704 usb_put_phy(di->usb_phy);
3706 if (di->usb_chg.enabled)
3707 power_supply_unregister(&di->usb_chg.psy);
3709 if (di->ac_chg.enabled)
3710 power_supply_unregister(&di->ac_chg.psy);
3712 destroy_workqueue(di->charger_wq);
3716 static const struct of_device_id ab8500_charger_match[] = {
3717 { .compatible = "stericsson,ab8500-charger", },
3721 static struct platform_driver ab8500_charger_driver = {
3722 .probe = ab8500_charger_probe,
3723 .remove = ab8500_charger_remove,
3724 .suspend = ab8500_charger_suspend,
3725 .resume = ab8500_charger_resume,
3727 .name = "ab8500-charger",
3728 .owner = THIS_MODULE,
3729 .of_match_table = ab8500_charger_match,
3733 static int __init ab8500_charger_init(void)
3735 return platform_driver_register(&ab8500_charger_driver);
3738 static void __exit ab8500_charger_exit(void)
3740 platform_driver_unregister(&ab8500_charger_driver);
3743 subsys_initcall_sync(ab8500_charger_init);
3744 module_exit(ab8500_charger_exit);
3746 MODULE_LICENSE("GPL v2");
3747 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3748 MODULE_ALIAS("platform:ab8500-charger");
3749 MODULE_DESCRIPTION("AB8500 charger management driver");