2 * Copyright (C) ST-Ericsson SA 2012
4 * Charger driver for AB8500
6 * License Terms: GNU General Public License v2
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/notifier.h>
19 #include <linux/slab.h>
20 #include <linux/platform_device.h>
21 #include <linux/power_supply.h>
22 #include <linux/completion.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/err.h>
25 #include <linux/workqueue.h>
26 #include <linux/kobject.h>
28 #include <linux/mfd/core.h>
29 #include <linux/mfd/abx500/ab8500.h>
30 #include <linux/mfd/abx500.h>
31 #include <linux/mfd/abx500/ab8500-bm.h>
32 #include <linux/mfd/abx500/ab8500-gpadc.h>
33 #include <linux/mfd/abx500/ux500_chargalg.h>
34 #include <linux/usb/otg.h>
35 #include <linux/mutex.h>
37 /* Charger constants */
42 #define MAIN_WDOG_ENA 0x01
43 #define MAIN_WDOG_KICK 0x02
44 #define MAIN_WDOG_DIS 0x00
45 #define CHARG_WD_KICK 0x01
46 #define MAIN_CH_ENA 0x01
47 #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
48 #define USB_CH_ENA 0x01
49 #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
50 #define MAIN_CH_DET 0x01
51 #define MAIN_CH_CV_ON 0x04
52 #define USB_CH_CV_ON 0x08
53 #define VBUS_DET_DBNC100 0x02
54 #define VBUS_DET_DBNC1 0x01
55 #define OTP_ENABLE_WD 0x01
56 #define DROP_COUNT_RESET 0x01
57 #define USB_CH_DET 0x01
59 #define MAIN_CH_INPUT_CURR_SHIFT 4
60 #define VBUS_IN_CURR_LIM_SHIFT 4
61 #define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
62 #define VBUS_IN_CURR_LIM_RETRY_SET_TIME 30 /* seconds */
64 #define LED_INDICATOR_PWM_ENA 0x01
65 #define LED_INDICATOR_PWM_DIS 0x00
66 #define LED_IND_CUR_5MA 0x04
67 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
69 /* HW failure constants */
70 #define MAIN_CH_TH_PROT 0x02
71 #define VBUS_CH_NOK 0x08
72 #define USB_CH_TH_PROT 0x02
73 #define VBUS_OVV_TH 0x01
74 #define MAIN_CH_NOK 0x01
77 #define MAIN_CH_STATUS2_MAINCHGDROP 0x80
78 #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
79 #define USB_CH_VBUSDROP 0x40
80 #define USB_CH_VBUSDETDBNC 0x01
82 /* UsbLineStatus register bit masks */
83 #define AB8500_USB_LINK_STATUS 0x78
84 #define AB8505_USB_LINK_STATUS 0xF8
85 #define AB8500_STD_HOST_SUSP 0x18
86 #define USB_LINK_STATUS_SHIFT 3
88 /* Watchdog timeout constant */
89 #define WD_TIMER 0x30 /* 4min */
90 #define WD_KICK_INTERVAL (60 * HZ)
92 /* Lowest charger voltage is 3.39V -> 0x4E */
93 #define LOW_VOLT_REG 0x4E
95 /* Step up/down delay in us */
96 #define STEP_UDELAY 1000
98 #define CHARGER_STATUS_POLL 10 /* in ms */
100 #define CHG_WD_INTERVAL (60 * HZ)
102 #define AB8500_SW_CONTROL_FALLBACK 0x03
103 /* Wait for enumeration before charing in us */
104 #define WAIT_ACA_RID_ENUMERATION (5 * 1000)
105 /*External charger control*/
106 #define AB8500_SYS_CHARGER_CONTROL_REG 0x52
107 #define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
108 #define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
110 /* UsbLineStatus register - usb types */
111 enum ab8500_charger_link_status {
112 USB_STAT_NOT_CONFIGURED,
113 USB_STAT_STD_HOST_NC,
114 USB_STAT_STD_HOST_C_NS,
115 USB_STAT_STD_HOST_C_S,
116 USB_STAT_HOST_CHG_NM,
117 USB_STAT_HOST_CHG_HS,
118 USB_STAT_HOST_CHG_HS_CHIRP,
119 USB_STAT_DEDICATED_CHG,
122 USB_STAT_ACA_RID_C_NM,
123 USB_STAT_ACA_RID_C_HS,
124 USB_STAT_ACA_RID_C_HS_CHIRP,
127 USB_STAT_NOT_VALID_LINK,
129 USB_STAT_SUP_NO_IDGND_VBUS,
130 USB_STAT_SUP_IDGND_VBUS,
131 USB_STAT_CHARGER_LINE_1,
134 USB_STAT_ACA_DOCK_CHARGER,
137 enum ab8500_usb_state {
138 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
139 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
140 AB8500_BM_USB_STATE_CONFIGURED,
141 AB8500_BM_USB_STATE_SUSPEND,
142 AB8500_BM_USB_STATE_RESUME,
143 AB8500_BM_USB_STATE_MAX,
146 /* VBUS input current limits supported in AB8500 in mA */
147 #define USB_CH_IP_CUR_LVL_0P05 50
148 #define USB_CH_IP_CUR_LVL_0P09 98
149 #define USB_CH_IP_CUR_LVL_0P19 193
150 #define USB_CH_IP_CUR_LVL_0P29 290
151 #define USB_CH_IP_CUR_LVL_0P38 380
152 #define USB_CH_IP_CUR_LVL_0P45 450
153 #define USB_CH_IP_CUR_LVL_0P5 500
154 #define USB_CH_IP_CUR_LVL_0P6 600
155 #define USB_CH_IP_CUR_LVL_0P7 700
156 #define USB_CH_IP_CUR_LVL_0P8 800
157 #define USB_CH_IP_CUR_LVL_0P9 900
158 #define USB_CH_IP_CUR_LVL_1P0 1000
159 #define USB_CH_IP_CUR_LVL_1P1 1100
160 #define USB_CH_IP_CUR_LVL_1P3 1300
161 #define USB_CH_IP_CUR_LVL_1P4 1400
162 #define USB_CH_IP_CUR_LVL_1P5 1500
164 #define VBAT_TRESH_IP_CUR_RED 3800
166 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
167 struct ab8500_charger, usb_chg)
168 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
169 struct ab8500_charger, ac_chg)
172 * struct ab8500_charger_interrupts - ab8500 interupts
173 * @name: name of the interrupt
174 * @isr function pointer to the isr
176 struct ab8500_charger_interrupts {
178 irqreturn_t (*isr)(int irq, void *data);
181 struct ab8500_charger_info {
182 int charger_connected;
190 struct ab8500_charger_event_flags {
192 bool main_thermal_prot;
193 bool usb_thermal_prot;
195 bool usbchargernotok;
201 struct ab8500_charger_usb_state {
204 enum ab8500_usb_state state;
205 enum ab8500_usb_state state_tmp;
209 struct ab8500_charger_max_usb_in_curr {
216 * struct ab8500_charger - ab8500 Charger device information
217 * @dev: Pointer to the structure device
218 * @vbus_detected: VBUS detected
219 * @vbus_detected_start:
220 * VBUS detected during startup
221 * @ac_conn: This will be true when the AC charger has been plugged
222 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
224 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
226 * @vbat Battery voltage
227 * @old_vbat Previously measured battery voltage
228 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
229 * @autopower Indicate if we should have automatic pwron after pwrloss
230 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
231 * @invalid_charger_detect_state State when forcing AB to use invalid charger
232 * @is_aca_rid: Incicate if accessory is ACA type
233 * @current_stepping_sessions:
234 * Counter for current stepping sessions
235 * @parent: Pointer to the struct ab8500
236 * @gpadc: Pointer to the struct gpadc
237 * @bm: Platform specific battery management information
238 * @flags: Structure for information about events triggered
239 * @usb_state: Structure for usb stack information
240 * @max_usb_in_curr: Max USB charger input current
241 * @ac_chg: AC charger power supply
242 * @usb_chg: USB charger power supply
243 * @ac: Structure that holds the AC charger properties
244 * @usb: Structure that holds the USB charger properties
245 * @regu: Pointer to the struct regulator
246 * @charger_wq: Work queue for the IRQs and checking HW state
247 * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
248 * @pm_lock: Lock to prevent system to suspend
249 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
250 * @check_hw_failure_work: Work for checking HW state
251 * @check_usbchgnotok_work: Work for checking USB charger not ok status
252 * @kick_wd_work: Work for kicking the charger watchdog in case
253 * of ABB rev 1.* due to the watchog logic bug
254 * @ac_charger_attached_work: Work for checking if AC charger is still
256 * @usb_charger_attached_work: Work for checking if USB charger is still
258 * @ac_work: Work for checking AC charger connection
259 * @detect_usb_type_work: Work for detecting the USB type connected
260 * @usb_link_status_work: Work for checking the new USB link status
261 * @usb_state_changed_work: Work for checking USB state
262 * @attach_work: Work for detecting USB type
263 * @vbus_drop_end_work: Work for detecting VBUS drop end
264 * @check_main_thermal_prot_work:
265 * Work for checking Main thermal status
266 * @check_usb_thermal_prot_work:
267 * Work for checking USB thermal status
268 * @charger_attached_mutex: For controlling the wakelock
270 struct ab8500_charger {
273 bool vbus_detected_start;
279 bool usb_device_is_unrecognised;
282 int invalid_charger_detect_state;
284 atomic_t current_stepping_sessions;
285 struct ab8500 *parent;
286 struct ab8500_gpadc *gpadc;
287 struct abx500_bm_data *bm;
288 struct ab8500_charger_event_flags flags;
289 struct ab8500_charger_usb_state usb_state;
290 struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
291 struct ux500_charger ac_chg;
292 struct ux500_charger usb_chg;
293 struct ab8500_charger_info ac;
294 struct ab8500_charger_info usb;
295 struct regulator *regu;
296 struct workqueue_struct *charger_wq;
297 struct mutex usb_ipt_crnt_lock;
298 struct delayed_work check_vbat_work;
299 struct delayed_work check_hw_failure_work;
300 struct delayed_work check_usbchgnotok_work;
301 struct delayed_work kick_wd_work;
302 struct delayed_work usb_state_changed_work;
303 struct delayed_work attach_work;
304 struct delayed_work ac_charger_attached_work;
305 struct delayed_work usb_charger_attached_work;
306 struct delayed_work vbus_drop_end_work;
307 struct work_struct ac_work;
308 struct work_struct detect_usb_type_work;
309 struct work_struct usb_link_status_work;
310 struct work_struct check_main_thermal_prot_work;
311 struct work_struct check_usb_thermal_prot_work;
312 struct usb_phy *usb_phy;
313 struct notifier_block nb;
314 struct mutex charger_attached_mutex;
318 static enum power_supply_property ab8500_charger_ac_props[] = {
319 POWER_SUPPLY_PROP_HEALTH,
320 POWER_SUPPLY_PROP_PRESENT,
321 POWER_SUPPLY_PROP_ONLINE,
322 POWER_SUPPLY_PROP_VOLTAGE_NOW,
323 POWER_SUPPLY_PROP_VOLTAGE_AVG,
324 POWER_SUPPLY_PROP_CURRENT_NOW,
328 static enum power_supply_property ab8500_charger_usb_props[] = {
329 POWER_SUPPLY_PROP_HEALTH,
330 POWER_SUPPLY_PROP_CURRENT_AVG,
331 POWER_SUPPLY_PROP_PRESENT,
332 POWER_SUPPLY_PROP_ONLINE,
333 POWER_SUPPLY_PROP_VOLTAGE_NOW,
334 POWER_SUPPLY_PROP_VOLTAGE_AVG,
335 POWER_SUPPLY_PROP_CURRENT_NOW,
339 * Function for enabling and disabling sw fallback mode
340 * should always be disabled when no charger is connected.
342 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
351 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
353 if (is_ab8500(di->parent)) {
358 bank = AB8500_SYS_CTRL1_BLOCK;
359 reg = AB8500_SW_CONTROL_FALLBACK;
363 /* read the register containing fallback bit */
364 ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
366 dev_err(di->dev, "%d read failed\n", __LINE__);
370 if (is_ab8500(di->parent)) {
371 /* enable the OPT emulation registers */
372 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
374 dev_err(di->dev, "%d write failed\n", __LINE__);
384 /* write back the changed fallback bit value to register */
385 ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
387 dev_err(di->dev, "%d write failed\n", __LINE__);
391 if (is_ab8500(di->parent)) {
392 /* disable the set OTP registers again */
393 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
395 dev_err(di->dev, "%d write failed\n", __LINE__);
401 * ab8500_power_supply_changed - a wrapper with local extentions for
402 * power_supply_changed
403 * @di: pointer to the ab8500_charger structure
404 * @psy: pointer to power_supply_that have changed.
407 static void ab8500_power_supply_changed(struct ab8500_charger *di,
408 struct power_supply *psy)
411 * This happens if we get notifications or interrupts and
412 * the platform has been configured not to support one or
413 * other type of charging.
418 if (di->autopower_cfg) {
419 if (!di->usb.charger_connected &&
420 !di->ac.charger_connected &&
422 di->autopower = false;
423 ab8500_enable_disable_sw_fallback(di, false);
424 } else if (!di->autopower &&
425 (di->ac.charger_connected ||
426 di->usb.charger_connected)) {
427 di->autopower = true;
428 ab8500_enable_disable_sw_fallback(di, true);
431 power_supply_changed(psy);
434 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
437 if (connected != di->usb.charger_connected) {
438 dev_dbg(di->dev, "USB connected:%i\n", connected);
439 di->usb.charger_connected = connected;
442 di->flags.vbus_drop_end = false;
445 * Sometimes the platform is configured not to support
446 * USB charging and no psy has been created, but we still
447 * will get these notifications.
449 if (di->usb_chg.psy) {
450 sysfs_notify(&di->usb_chg.psy->dev.kobj, NULL,
455 mutex_lock(&di->charger_attached_mutex);
456 mutex_unlock(&di->charger_attached_mutex);
458 if (is_ab8500(di->parent))
459 queue_delayed_work(di->charger_wq,
460 &di->usb_charger_attached_work,
463 cancel_delayed_work_sync(&di->usb_charger_attached_work);
464 mutex_lock(&di->charger_attached_mutex);
465 mutex_unlock(&di->charger_attached_mutex);
471 * ab8500_charger_get_ac_voltage() - get ac charger voltage
472 * @di: pointer to the ab8500_charger structure
474 * Returns ac charger voltage (on success)
476 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
480 /* Only measure voltage if the charger is connected */
481 if (di->ac.charger_connected) {
482 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
484 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
492 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
493 * @di: pointer to the ab8500_charger structure
495 * Returns ac charger CV mode (on success) else error code
497 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
502 /* Only check CV mode if the charger is online */
503 if (di->ac.charger_online) {
504 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
505 AB8500_CH_STATUS1_REG, &val);
507 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
511 if (val & MAIN_CH_CV_ON)
521 * ab8500_charger_get_vbus_voltage() - get vbus voltage
522 * @di: pointer to the ab8500_charger structure
524 * This function returns the vbus voltage.
525 * Returns vbus voltage (on success)
527 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
531 /* Only measure voltage if the charger is connected */
532 if (di->usb.charger_connected) {
533 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
535 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
543 * ab8500_charger_get_usb_current() - get usb charger current
544 * @di: pointer to the ab8500_charger structure
546 * This function returns the usb charger current.
547 * Returns usb current (on success) and error code on failure
549 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
553 /* Only measure current if the charger is online */
554 if (di->usb.charger_online) {
555 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
557 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
565 * ab8500_charger_get_ac_current() - get ac charger current
566 * @di: pointer to the ab8500_charger structure
568 * This function returns the ac charger current.
569 * Returns ac current (on success) and error code on failure.
571 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
575 /* Only measure current if the charger is online */
576 if (di->ac.charger_online) {
577 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
579 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
587 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
588 * @di: pointer to the ab8500_charger structure
590 * Returns ac charger CV mode (on success) else error code
592 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
597 /* Only check CV mode if the charger is online */
598 if (di->usb.charger_online) {
599 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
600 AB8500_CH_USBCH_STAT1_REG, &val);
602 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
606 if (val & USB_CH_CV_ON)
618 * ab8500_charger_detect_chargers() - Detect the connected chargers
619 * @di: pointer to the ab8500_charger structure
620 * @probe: if probe, don't delay and wait for HW
622 * Returns the type of charger connected.
623 * For USB it will not mean we can actually charge from it
624 * but that there is a USB cable connected that we have to
625 * identify. This is used during startup when we don't get
626 * interrupts of the charger detection
628 * Returns an integer value, that means,
629 * NO_PW_CONN no power supply is connected
630 * AC_PW_CONN if the AC power supply is connected
631 * USB_PW_CONN if the USB power supply is connected
632 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
634 static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
636 int result = NO_PW_CONN;
640 /* Check for AC charger */
641 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
642 AB8500_CH_STATUS1_REG, &val);
644 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
648 if (val & MAIN_CH_DET)
651 /* Check for USB charger */
655 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
656 * when disconnecting ACA even though no
657 * charger was connected. Try waiting a little
658 * longer than the 100 ms of VBUS_DET_DBNC100...
662 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
663 AB8500_CH_USBCH_STAT1_REG, &val);
665 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
669 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
671 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
672 result |= USB_PW_CONN;
678 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
679 * @di: pointer to the ab8500_charger structure
680 * @link_status: the identified USB type
682 * Get the maximum current that is allowed to be drawn from the host
683 * based on the USB type.
684 * Returns error code in case of failure else 0 on success
686 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
687 enum ab8500_charger_link_status link_status)
691 di->usb_device_is_unrecognised = false;
694 * Platform only supports USB 2.0.
695 * This means that charging current from USB source
696 * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
697 * should set USB_CH_IP_CUR_LVL_0P5.
700 switch (link_status) {
701 case USB_STAT_STD_HOST_NC:
702 case USB_STAT_STD_HOST_C_NS:
703 case USB_STAT_STD_HOST_C_S:
704 dev_dbg(di->dev, "USB Type - Standard host is "
705 "detected through USB driver\n");
706 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
709 case USB_STAT_HOST_CHG_HS_CHIRP:
710 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
713 case USB_STAT_HOST_CHG_HS:
714 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
717 case USB_STAT_ACA_RID_C_HS:
718 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
721 case USB_STAT_ACA_RID_A:
723 * Dedicated charger level minus maximum current accessory
724 * can consume (900mA). Closest level is 500mA
726 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
727 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
730 case USB_STAT_ACA_RID_B:
732 * Dedicated charger level minus 120mA (20mA for ACA and
733 * 100mA for potential accessory). Closest level is 1300mA
735 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
736 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
737 di->max_usb_in_curr.usb_type_max);
740 case USB_STAT_HOST_CHG_NM:
741 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
744 case USB_STAT_DEDICATED_CHG:
745 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
748 case USB_STAT_ACA_RID_C_HS_CHIRP:
749 case USB_STAT_ACA_RID_C_NM:
750 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
753 case USB_STAT_NOT_CONFIGURED:
754 if (di->vbus_detected) {
755 di->usb_device_is_unrecognised = true;
756 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
757 di->max_usb_in_curr.usb_type_max =
758 USB_CH_IP_CUR_LVL_1P5;
761 case USB_STAT_HM_IDGND:
762 dev_err(di->dev, "USB Type - Charging not allowed\n");
763 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
766 case USB_STAT_RESERVED:
767 if (is_ab8500(di->parent)) {
768 di->flags.vbus_collapse = true;
769 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
770 "VBUS has collapsed\n");
774 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
775 di->max_usb_in_curr.usb_type_max =
776 USB_CH_IP_CUR_LVL_0P05;
777 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
779 di->max_usb_in_curr.usb_type_max);
783 case USB_STAT_CARKIT_1:
784 case USB_STAT_CARKIT_2:
785 case USB_STAT_ACA_DOCK_CHARGER:
786 case USB_STAT_CHARGER_LINE_1:
787 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
788 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
789 di->max_usb_in_curr.usb_type_max);
791 case USB_STAT_NOT_VALID_LINK:
792 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
793 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
797 dev_err(di->dev, "USB Type - Unknown\n");
798 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
803 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
804 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
805 link_status, di->max_usb_in_curr.set_max);
811 * ab8500_charger_read_usb_type() - read the type of usb connected
812 * @di: pointer to the ab8500_charger structure
814 * Detect the type of the plugged USB
815 * Returns error code in case of failure else 0 on success
817 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
822 ret = abx500_get_register_interruptible(di->dev,
823 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
825 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
828 if (is_ab8500(di->parent))
829 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
830 AB8500_USB_LINE_STAT_REG, &val);
832 ret = abx500_get_register_interruptible(di->dev,
833 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
835 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
839 /* get the USB type */
840 if (is_ab8500(di->parent))
841 val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
843 val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
844 ret = ab8500_charger_max_usb_curr(di,
845 (enum ab8500_charger_link_status) val);
851 * ab8500_charger_detect_usb_type() - get the type of usb connected
852 * @di: pointer to the ab8500_charger structure
854 * Detect the type of the plugged USB
855 * Returns error code in case of failure else 0 on success
857 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
863 * On getting the VBUS rising edge detect interrupt there
864 * is a 250ms delay after which the register UsbLineStatus
865 * is filled with valid data.
867 for (i = 0; i < 10; i++) {
869 ret = abx500_get_register_interruptible(di->dev,
870 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
872 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
875 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
879 if (is_ab8500(di->parent))
880 ret = abx500_get_register_interruptible(di->dev,
881 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
883 ret = abx500_get_register_interruptible(di->dev,
884 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
886 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
889 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
892 * Until the IT source register is read the UsbLineStatus
893 * register is not updated, hence doing the same
897 /* get the USB type */
898 if (is_ab8500(di->parent))
899 val = (val & AB8500_USB_LINK_STATUS) >>
900 USB_LINK_STATUS_SHIFT;
902 val = (val & AB8505_USB_LINK_STATUS) >>
903 USB_LINK_STATUS_SHIFT;
907 ret = ab8500_charger_max_usb_curr(di,
908 (enum ab8500_charger_link_status) val);
914 * This array maps the raw hex value to charger voltage used by the AB8500
915 * Values taken from the UM0836
917 static int ab8500_charger_voltage_map[] = {
998 static int ab8500_voltage_to_regval(int voltage)
1002 /* Special case for voltage below 3.5V */
1003 if (voltage < ab8500_charger_voltage_map[0])
1004 return LOW_VOLT_REG;
1006 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
1007 if (voltage < ab8500_charger_voltage_map[i])
1011 /* If not last element, return error */
1012 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
1013 if (voltage == ab8500_charger_voltage_map[i])
1019 static int ab8500_current_to_regval(struct ab8500_charger *di, int curr)
1023 if (curr < di->bm->chg_output_curr[0])
1026 for (i = 0; i < di->bm->n_chg_out_curr; i++) {
1027 if (curr < di->bm->chg_output_curr[i])
1031 /* If not last element, return error */
1032 i = di->bm->n_chg_out_curr - 1;
1033 if (curr == di->bm->chg_output_curr[i])
1039 static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr)
1043 if (curr < di->bm->chg_input_curr[0])
1046 for (i = 0; i < di->bm->n_chg_in_curr; i++) {
1047 if (curr < di->bm->chg_input_curr[i])
1051 /* If not last element, return error */
1052 i = di->bm->n_chg_in_curr - 1;
1053 if (curr == di->bm->chg_input_curr[i])
1060 * ab8500_charger_get_usb_cur() - get usb current
1061 * @di: pointer to the ab8500_charger structre
1063 * The usb stack provides the maximum current that can be drawn from
1064 * the standard usb host. This will be in mA.
1065 * This function converts current in mA to a value that can be written
1066 * to the register. Returns -1 if charging is not allowed
1068 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1071 switch (di->usb_state.usb_current) {
1073 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
1076 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
1079 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
1082 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
1085 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
1088 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
1092 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
1097 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1098 * @di: pointer to the ab8500_charger structure
1099 * @reg: select what charger register to check
1101 * Check if current stepping should be allowed to continue.
1102 * Checks if charger source has not collapsed. If it has, further stepping
1105 static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1108 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1109 return !di->flags.vbus_drop_end;
1115 * ab8500_charger_set_current() - set charger current
1116 * @di: pointer to the ab8500_charger structure
1117 * @ich: charger current, in mA
1118 * @reg: select what charger register to set
1120 * Set charger current.
1121 * There is no state machine in the AB to step up/down the charger
1122 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1123 * charging is started. Instead we need to implement
1124 * this charger current step-up/down here.
1125 * Returns error code in case of failure else 0(on success)
1127 static int ab8500_charger_set_current(struct ab8500_charger *di,
1131 int curr_index, prev_curr_index, shift_value, i;
1134 bool no_stepping = false;
1136 atomic_inc(&di->current_stepping_sessions);
1138 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1141 dev_err(di->dev, "%s read failed\n", __func__);
1142 goto exit_set_current;
1146 case AB8500_MCH_IPT_CURLVL_REG:
1147 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1148 prev_curr_index = (reg_value >> shift_value);
1149 curr_index = ab8500_current_to_regval(di, ich);
1150 step_udelay = STEP_UDELAY;
1151 if (!di->ac.charger_connected)
1154 case AB8500_USBCH_IPT_CRNTLVL_REG:
1155 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1156 prev_curr_index = (reg_value >> shift_value);
1157 curr_index = ab8500_vbus_in_curr_to_regval(di, ich);
1158 step_udelay = STEP_UDELAY * 100;
1160 if (!di->usb.charger_connected)
1163 case AB8500_CH_OPT_CRNTLVL_REG:
1165 prev_curr_index = (reg_value >> shift_value);
1166 curr_index = ab8500_current_to_regval(di, ich);
1167 step_udelay = STEP_UDELAY;
1168 if (curr_index && (curr_index - prev_curr_index) > 1)
1171 if (!di->usb.charger_connected && !di->ac.charger_connected)
1176 dev_err(di->dev, "%s current register not valid\n", __func__);
1178 goto exit_set_current;
1181 if (curr_index < 0) {
1182 dev_err(di->dev, "requested current limit out-of-range\n");
1184 goto exit_set_current;
1187 /* only update current if it's been changed */
1188 if (prev_curr_index == curr_index) {
1189 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1192 goto exit_set_current;
1195 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1196 __func__, ich, reg);
1199 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1200 reg, (u8)curr_index << shift_value);
1202 dev_err(di->dev, "%s write failed\n", __func__);
1203 } else if (prev_curr_index > curr_index) {
1204 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1205 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1206 (u8) i << shift_value, reg);
1207 ret = abx500_set_register_interruptible(di->dev,
1208 AB8500_CHARGER, reg, (u8)i << shift_value);
1210 dev_err(di->dev, "%s write failed\n", __func__);
1211 goto exit_set_current;
1213 if (i != curr_index)
1214 usleep_range(step_udelay, step_udelay * 2);
1218 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
1219 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1220 (u8)i << shift_value, reg);
1221 ret = abx500_set_register_interruptible(di->dev,
1222 AB8500_CHARGER, reg, (u8)i << shift_value);
1224 dev_err(di->dev, "%s write failed\n", __func__);
1225 goto exit_set_current;
1227 if (i != curr_index)
1228 usleep_range(step_udelay, step_udelay * 2);
1230 allow = ab8500_charger_check_continue_stepping(di, reg);
1235 atomic_dec(&di->current_stepping_sessions);
1241 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1242 * @di: pointer to the ab8500_charger structure
1243 * @ich_in: charger input current limit
1245 * Sets the current that can be drawn from the USB host
1246 * Returns error code in case of failure else 0(on success)
1248 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1254 /* We should always use to lowest current limit */
1255 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1256 if (di->max_usb_in_curr.set_max > 0)
1257 min_value = min(di->max_usb_in_curr.set_max, min_value);
1259 if (di->usb_state.usb_current >= 0)
1260 min_value = min(di->usb_state.usb_current, min_value);
1262 switch (min_value) {
1264 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1265 min_value = USB_CH_IP_CUR_LVL_0P05;
1268 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1269 min_value = USB_CH_IP_CUR_LVL_0P45;
1275 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1277 mutex_lock(&di->usb_ipt_crnt_lock);
1278 ret = ab8500_charger_set_current(di, min_value,
1279 AB8500_USBCH_IPT_CRNTLVL_REG);
1280 mutex_unlock(&di->usb_ipt_crnt_lock);
1286 * ab8500_charger_set_main_in_curr() - set main charger input current
1287 * @di: pointer to the ab8500_charger structure
1288 * @ich_in: input charger current, in mA
1290 * Set main charger input current.
1291 * Returns error code in case of failure else 0(on success)
1293 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1296 return ab8500_charger_set_current(di, ich_in,
1297 AB8500_MCH_IPT_CURLVL_REG);
1301 * ab8500_charger_set_output_curr() - set charger output current
1302 * @di: pointer to the ab8500_charger structure
1303 * @ich_out: output charger current, in mA
1305 * Set charger output current.
1306 * Returns error code in case of failure else 0(on success)
1308 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1311 return ab8500_charger_set_current(di, ich_out,
1312 AB8500_CH_OPT_CRNTLVL_REG);
1316 * ab8500_charger_led_en() - turn on/off chargign led
1317 * @di: pointer to the ab8500_charger structure
1318 * @on: flag to turn on/off the chargign led
1320 * Power ON/OFF charging LED indication
1321 * Returns error code in case of failure else 0(on success)
1323 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1328 /* Power ON charging LED indicator, set LED current to 5mA */
1329 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1330 AB8500_LED_INDICATOR_PWM_CTRL,
1331 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1333 dev_err(di->dev, "Power ON LED failed\n");
1336 /* LED indicator PWM duty cycle 252/256 */
1337 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1338 AB8500_LED_INDICATOR_PWM_DUTY,
1339 LED_INDICATOR_PWM_DUTY_252_256);
1341 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1345 /* Power off charging LED indicator */
1346 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1347 AB8500_LED_INDICATOR_PWM_CTRL,
1348 LED_INDICATOR_PWM_DIS);
1350 dev_err(di->dev, "Power-off LED failed\n");
1359 * ab8500_charger_ac_en() - enable or disable ac charging
1360 * @di: pointer to the ab8500_charger structure
1361 * @enable: enable/disable flag
1362 * @vset: charging voltage
1363 * @iset: charging current
1365 * Enable/Disable AC/Mains charging and turns on/off the charging led
1368 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1369 int enable, int vset, int iset)
1374 int input_curr_index;
1377 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1380 /* Check if AC is connected */
1381 if (!di->ac.charger_connected) {
1382 dev_err(di->dev, "AC charger not connected\n");
1386 /* Enable AC charging */
1387 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1390 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1391 * will be triggered everytime we enable the VDD ADC supply.
1392 * This will turn off charging for a short while.
1393 * It can be avoided by having the supply on when
1394 * there is a charger enabled. Normally the VDD ADC supply
1395 * is enabled everytime a GPADC conversion is triggered. We will
1396 * force it to be enabled from this driver to have
1397 * the GPADC module independant of the AB8500 chargers
1399 if (!di->vddadc_en_ac) {
1400 ret = regulator_enable(di->regu);
1403 "Failed to enable regulator\n");
1405 di->vddadc_en_ac = true;
1408 /* Check if the requested voltage or current is valid */
1409 volt_index = ab8500_voltage_to_regval(vset);
1410 curr_index = ab8500_current_to_regval(di, iset);
1411 input_curr_index = ab8500_current_to_regval(di,
1412 di->bm->chg_params->ac_curr_max);
1413 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1415 "Charger voltage or current too high, "
1416 "charging not started\n");
1420 /* ChVoltLevel: maximum battery charging voltage */
1421 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1422 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1424 dev_err(di->dev, "%s write failed\n", __func__);
1427 /* MainChInputCurr: current that can be drawn from the charger*/
1428 ret = ab8500_charger_set_main_in_curr(di,
1429 di->bm->chg_params->ac_curr_max);
1431 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1435 /* ChOutputCurentLevel: protected output current */
1436 ret = ab8500_charger_set_output_curr(di, iset);
1438 dev_err(di->dev, "%s "
1439 "Failed to set ChOutputCurentLevel\n",
1444 /* Check if VBAT overshoot control should be enabled */
1445 if (!di->bm->enable_overshoot)
1446 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1448 /* Enable Main Charger */
1449 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1450 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1452 dev_err(di->dev, "%s write failed\n", __func__);
1456 /* Power on charging LED indication */
1457 ret = ab8500_charger_led_en(di, true);
1459 dev_err(di->dev, "failed to enable LED\n");
1461 di->ac.charger_online = 1;
1463 /* Disable AC charging */
1464 if (is_ab8500_1p1_or_earlier(di->parent)) {
1466 * For ABB revision 1.0 and 1.1 there is a bug in the
1467 * watchdog logic. That means we have to continously
1468 * kick the charger watchdog even when no charger is
1469 * connected. This is only valid once the AC charger
1470 * has been enabled. This is a bug that is not handled
1471 * by the algorithm and the watchdog have to be kicked
1472 * by the charger driver when the AC charger
1476 queue_delayed_work(di->charger_wq,
1478 round_jiffies(WD_KICK_INTERVAL));
1482 * We can't turn off charging completely
1483 * due to a bug in AB8500 cut1.
1484 * If we do, charging will not start again.
1485 * That is why we set the lowest voltage
1486 * and current possible
1488 ret = abx500_set_register_interruptible(di->dev,
1490 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1493 "%s write failed\n", __func__);
1497 ret = ab8500_charger_set_output_curr(di, 0);
1499 dev_err(di->dev, "%s "
1500 "Failed to set ChOutputCurentLevel\n",
1505 ret = abx500_set_register_interruptible(di->dev,
1507 AB8500_MCH_CTRL1, 0);
1510 "%s write failed\n", __func__);
1515 ret = ab8500_charger_led_en(di, false);
1517 dev_err(di->dev, "failed to disable LED\n");
1519 di->ac.charger_online = 0;
1520 di->ac.wd_expired = false;
1522 /* Disable regulator if enabled */
1523 if (di->vddadc_en_ac) {
1524 regulator_disable(di->regu);
1525 di->vddadc_en_ac = false;
1528 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1530 ab8500_power_supply_changed(di, di->ac_chg.psy);
1536 * ab8500_charger_usb_en() - enable usb charging
1537 * @di: pointer to the ab8500_charger structure
1538 * @enable: enable/disable flag
1539 * @vset: charging voltage
1540 * @ich_out: charger output current
1542 * Enable/Disable USB charging and turns on/off the charging led respectively.
1543 * Returns error code in case of failure else 0(on success)
1545 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1546 int enable, int vset, int ich_out)
1553 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1556 /* Check if USB is connected */
1557 if (!di->usb.charger_connected) {
1558 dev_err(di->dev, "USB charger not connected\n");
1563 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1564 * will be triggered everytime we enable the VDD ADC supply.
1565 * This will turn off charging for a short while.
1566 * It can be avoided by having the supply on when
1567 * there is a charger enabled. Normally the VDD ADC supply
1568 * is enabled everytime a GPADC conversion is triggered. We will
1569 * force it to be enabled from this driver to have
1570 * the GPADC module independant of the AB8500 chargers
1572 if (!di->vddadc_en_usb) {
1573 ret = regulator_enable(di->regu);
1576 "Failed to enable regulator\n");
1578 di->vddadc_en_usb = true;
1581 /* Enable USB charging */
1582 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1584 /* Check if the requested voltage or current is valid */
1585 volt_index = ab8500_voltage_to_regval(vset);
1586 curr_index = ab8500_current_to_regval(di, ich_out);
1587 if (volt_index < 0 || curr_index < 0) {
1589 "Charger voltage or current too high, "
1590 "charging not started\n");
1594 /* ChVoltLevel: max voltage upto which battery can be charged */
1595 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1596 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1598 dev_err(di->dev, "%s write failed\n", __func__);
1601 /* Check if VBAT overshoot control should be enabled */
1602 if (!di->bm->enable_overshoot)
1603 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1605 /* Enable USB Charger */
1607 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
1608 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1609 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1611 dev_err(di->dev, "%s write failed\n", __func__);
1615 /* If success power on charging LED indication */
1616 ret = ab8500_charger_led_en(di, true);
1618 dev_err(di->dev, "failed to enable LED\n");
1620 di->usb.charger_online = 1;
1622 /* USBChInputCurr: current that can be drawn from the usb */
1623 ret = ab8500_charger_set_vbus_in_curr(di,
1624 di->max_usb_in_curr.usb_type_max);
1626 dev_err(di->dev, "setting USBChInputCurr failed\n");
1630 /* ChOutputCurentLevel: protected output current */
1631 ret = ab8500_charger_set_output_curr(di, ich_out);
1633 dev_err(di->dev, "%s "
1634 "Failed to set ChOutputCurentLevel\n",
1639 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1642 /* Disable USB charging */
1643 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1644 ret = abx500_set_register_interruptible(di->dev,
1646 AB8500_USBCH_CTRL1_REG, 0);
1649 "%s write failed\n", __func__);
1653 ret = ab8500_charger_led_en(di, false);
1655 dev_err(di->dev, "failed to disable LED\n");
1656 /* USBChInputCurr: current that can be drawn from the usb */
1657 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1659 dev_err(di->dev, "setting USBChInputCurr failed\n");
1663 /* ChOutputCurentLevel: protected output current */
1664 ret = ab8500_charger_set_output_curr(di, 0);
1666 dev_err(di->dev, "%s "
1667 "Failed to reset ChOutputCurentLevel\n",
1671 di->usb.charger_online = 0;
1672 di->usb.wd_expired = false;
1674 /* Disable regulator if enabled */
1675 if (di->vddadc_en_usb) {
1676 regulator_disable(di->regu);
1677 di->vddadc_en_usb = false;
1680 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1682 /* Cancel any pending Vbat check work */
1683 cancel_delayed_work(&di->check_vbat_work);
1686 ab8500_power_supply_changed(di, di->usb_chg.psy);
1691 static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1692 unsigned long event, void *data)
1695 struct device *dev = data;
1696 /*Toggle External charger control pin*/
1697 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1698 AB8500_SYS_CHARGER_CONTROL_REG,
1699 EXTERNAL_CHARGER_DISABLE_REG_VAL);
1701 dev_err(dev, "write reg failed %d\n", ret);
1704 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1705 AB8500_SYS_CHARGER_CONTROL_REG,
1706 EXTERNAL_CHARGER_ENABLE_REG_VAL);
1708 dev_err(dev, "Write reg failed %d\n", ret);
1715 * ab8500_charger_usb_check_enable() - enable usb charging
1716 * @charger: pointer to the ux500_charger structure
1717 * @vset: charging voltage
1718 * @iset: charger output current
1720 * Check if the VBUS charger has been disconnected and reconnected without
1721 * AB8500 rising an interrupt. Returns 0 on success.
1723 static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1729 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1731 if (!di->usb.charger_connected)
1734 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1735 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1737 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1740 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1742 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1743 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1745 ret = abx500_mask_and_set_register_interruptible(di->dev,
1746 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1747 DROP_COUNT_RESET, DROP_COUNT_RESET);
1749 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1753 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1755 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1764 * ab8500_charger_ac_check_enable() - enable usb charging
1765 * @charger: pointer to the ux500_charger structure
1766 * @vset: charging voltage
1767 * @iset: charger output current
1769 * Check if the AC charger has been disconnected and reconnected without
1770 * AB8500 rising an interrupt. Returns 0 on success.
1772 static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1775 u8 mainch_ctrl1 = 0;
1778 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1780 if (!di->ac.charger_connected)
1783 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1784 AB8500_MCH_CTRL1, &mainch_ctrl1);
1786 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1789 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1791 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1792 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1794 ret = abx500_mask_and_set_register_interruptible(di->dev,
1795 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1796 DROP_COUNT_RESET, DROP_COUNT_RESET);
1799 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1803 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1805 dev_err(di->dev, "failed to enable AC charger %d\n",
1814 * ab8500_charger_watchdog_kick() - kick charger watchdog
1815 * @di: pointer to the ab8500_charger structure
1817 * Kick charger watchdog
1818 * Returns error code in case of failure else 0(on success)
1820 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1823 struct ab8500_charger *di;
1825 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1826 di = to_ab8500_charger_ac_device_info(charger);
1827 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1828 di = to_ab8500_charger_usb_device_info(charger);
1832 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1833 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1835 dev_err(di->dev, "Failed to kick WD!\n");
1841 * ab8500_charger_update_charger_current() - update charger current
1842 * @di: pointer to the ab8500_charger structure
1844 * Update the charger output current for the specified charger
1845 * Returns error code in case of failure else 0(on success)
1847 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1851 struct ab8500_charger *di;
1853 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1854 di = to_ab8500_charger_ac_device_info(charger);
1855 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1856 di = to_ab8500_charger_usb_device_info(charger);
1860 ret = ab8500_charger_set_output_curr(di, ich_out);
1862 dev_err(di->dev, "%s "
1863 "Failed to set ChOutputCurentLevel\n",
1868 /* Reset the main and usb drop input current measurement counter */
1869 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1870 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
1872 dev_err(di->dev, "%s write failed\n", __func__);
1879 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1881 struct power_supply *psy;
1882 struct power_supply *ext = dev_get_drvdata(dev);
1883 const char **supplicants = (const char **)ext->supplied_to;
1884 struct ab8500_charger *di;
1885 union power_supply_propval ret;
1887 struct ux500_charger *usb_chg;
1889 usb_chg = (struct ux500_charger *)data;
1892 di = to_ab8500_charger_usb_device_info(usb_chg);
1894 /* For all psy where the driver name appears in any supplied_to */
1895 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
1899 /* Go through all properties for the psy */
1900 for (j = 0; j < ext->desc->num_properties; j++) {
1901 enum power_supply_property prop;
1902 prop = ext->desc->properties[j];
1904 if (power_supply_get_property(ext, prop, &ret))
1908 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1909 switch (ext->desc->type) {
1910 case POWER_SUPPLY_TYPE_BATTERY:
1911 di->vbat = ret.intval / 1000;
1925 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1926 * @work pointer to the work_struct structure
1928 * Due to a asic bug it is necessary to lower the input current to the vbus
1929 * charger when charging with at some specific levels. This issue is only valid
1930 * for below a certain battery voltage. This function makes sure that the
1931 * the allowed current limit isn't exceeded.
1933 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1936 struct ab8500_charger *di = container_of(work,
1937 struct ab8500_charger, check_vbat_work.work);
1939 class_for_each_device(power_supply_class, NULL,
1940 di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1942 /* First run old_vbat is 0. */
1943 if (di->old_vbat == 0)
1944 di->old_vbat = di->vbat;
1946 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1947 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1948 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1949 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1951 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1952 " old: %d\n", di->max_usb_in_curr.usb_type_max,
1953 di->vbat, di->old_vbat);
1954 ab8500_charger_set_vbus_in_curr(di,
1955 di->max_usb_in_curr.usb_type_max);
1956 power_supply_changed(di->usb_chg.psy);
1959 di->old_vbat = di->vbat;
1962 * No need to check the battery voltage every second when not close to
1965 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1966 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1969 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1973 * ab8500_charger_check_hw_failure_work() - check main charger failure
1974 * @work: pointer to the work_struct structure
1976 * Work queue function for checking the main charger status
1978 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1983 struct ab8500_charger *di = container_of(work,
1984 struct ab8500_charger, check_hw_failure_work.work);
1986 /* Check if the status bits for HW failure is still active */
1987 if (di->flags.mainextchnotok) {
1988 ret = abx500_get_register_interruptible(di->dev,
1989 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1991 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1994 if (!(reg_value & MAIN_CH_NOK)) {
1995 di->flags.mainextchnotok = false;
1996 ab8500_power_supply_changed(di, di->ac_chg.psy);
1999 if (di->flags.vbus_ovv) {
2000 ret = abx500_get_register_interruptible(di->dev,
2001 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
2004 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2007 if (!(reg_value & VBUS_OVV_TH)) {
2008 di->flags.vbus_ovv = false;
2009 ab8500_power_supply_changed(di, di->usb_chg.psy);
2012 /* If we still have a failure, schedule a new check */
2013 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2014 queue_delayed_work(di->charger_wq,
2015 &di->check_hw_failure_work, round_jiffies(HZ));
2020 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2021 * @work: pointer to the work_struct structure
2023 * Work queue function for kicking the charger watchdog.
2025 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2026 * logic. That means we have to continously kick the charger
2027 * watchdog even when no charger is connected. This is only
2028 * valid once the AC charger has been enabled. This is
2029 * a bug that is not handled by the algorithm and the
2030 * watchdog have to be kicked by the charger driver
2031 * when the AC charger is disabled
2033 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2037 struct ab8500_charger *di = container_of(work,
2038 struct ab8500_charger, kick_wd_work.work);
2040 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2041 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2043 dev_err(di->dev, "Failed to kick WD!\n");
2045 /* Schedule a new watchdog kick */
2046 queue_delayed_work(di->charger_wq,
2047 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2051 * ab8500_charger_ac_work() - work to get and set main charger status
2052 * @work: pointer to the work_struct structure
2054 * Work queue function for checking the main charger status
2056 static void ab8500_charger_ac_work(struct work_struct *work)
2060 struct ab8500_charger *di = container_of(work,
2061 struct ab8500_charger, ac_work);
2064 * Since we can't be sure that the events are received
2065 * synchronously, we have the check if the main charger is
2066 * connected by reading the status register
2068 ret = ab8500_charger_detect_chargers(di, false);
2072 if (ret & AC_PW_CONN) {
2073 di->ac.charger_connected = 1;
2076 di->ac.charger_connected = 0;
2079 ab8500_power_supply_changed(di, di->ac_chg.psy);
2080 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
2083 static void ab8500_charger_usb_attached_work(struct work_struct *work)
2085 struct ab8500_charger *di = container_of(work,
2086 struct ab8500_charger,
2087 usb_charger_attached_work.work);
2088 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2092 for (i = 0; i < 10; i++) {
2093 ret = abx500_get_register_interruptible(di->dev,
2095 AB8500_CH_USBCH_STAT1_REG,
2098 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2101 if ((statval & usbch) != usbch)
2104 msleep(CHARGER_STATUS_POLL);
2107 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2109 mutex_lock(&di->charger_attached_mutex);
2110 mutex_unlock(&di->charger_attached_mutex);
2115 queue_delayed_work(di->charger_wq,
2116 &di->usb_charger_attached_work,
2120 static void ab8500_charger_ac_attached_work(struct work_struct *work)
2123 struct ab8500_charger *di = container_of(work,
2124 struct ab8500_charger,
2125 ac_charger_attached_work.work);
2126 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2127 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2131 for (i = 0; i < 10; i++) {
2132 ret = abx500_get_register_interruptible(di->dev,
2134 AB8500_CH_STATUS2_REG,
2137 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2141 if ((statval & mainch) != mainch)
2144 msleep(CHARGER_STATUS_POLL);
2147 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2148 queue_work(di->charger_wq, &di->ac_work);
2150 mutex_lock(&di->charger_attached_mutex);
2151 mutex_unlock(&di->charger_attached_mutex);
2156 queue_delayed_work(di->charger_wq,
2157 &di->ac_charger_attached_work,
2162 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2163 * @work: Pointer to the work_struct structure
2165 * Detect the type of USB plugged
2167 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
2171 struct ab8500_charger *di = container_of(work,
2172 struct ab8500_charger, detect_usb_type_work);
2175 * Since we can't be sure that the events are received
2176 * synchronously, we have the check if is
2177 * connected by reading the status register
2179 ret = ab8500_charger_detect_chargers(di, false);
2183 if (!(ret & USB_PW_CONN)) {
2184 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2185 di->vbus_detected = false;
2186 ab8500_charger_set_usb_connected(di, false);
2187 ab8500_power_supply_changed(di, di->usb_chg.psy);
2189 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2190 di->vbus_detected = true;
2192 if (is_ab8500_1p1_or_earlier(di->parent)) {
2193 ret = ab8500_charger_detect_usb_type(di);
2195 ab8500_charger_set_usb_connected(di, true);
2196 ab8500_power_supply_changed(di,
2201 * For ABB cut2.0 and onwards we have an IRQ,
2202 * USB_LINK_STATUS that will be triggered when the USB
2203 * link status changes. The exception is USB connected
2204 * during startup. Then we don't get a
2205 * USB_LINK_STATUS IRQ
2207 if (di->vbus_detected_start) {
2208 di->vbus_detected_start = false;
2209 ret = ab8500_charger_detect_usb_type(di);
2211 ab8500_charger_set_usb_connected(di,
2213 ab8500_power_supply_changed(di,
2222 * ab8500_charger_usb_link_attach_work() - work to detect USB type
2223 * @work: pointer to the work_struct structure
2225 * Detect the type of USB plugged
2227 static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2229 struct ab8500_charger *di =
2230 container_of(work, struct ab8500_charger, attach_work.work);
2233 /* Update maximum input current if USB enumeration is not detected */
2234 if (!di->usb.charger_online) {
2235 ret = ab8500_charger_set_vbus_in_curr(di,
2236 di->max_usb_in_curr.usb_type_max);
2241 ab8500_charger_set_usb_connected(di, true);
2242 ab8500_power_supply_changed(di, di->usb_chg.psy);
2246 * ab8500_charger_usb_link_status_work() - work to detect USB type
2247 * @work: pointer to the work_struct structure
2249 * Detect the type of USB plugged
2251 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2253 int detected_chargers;
2258 struct ab8500_charger *di = container_of(work,
2259 struct ab8500_charger, usb_link_status_work);
2262 * Since we can't be sure that the events are received
2263 * synchronously, we have the check if is
2264 * connected by reading the status register
2266 detected_chargers = ab8500_charger_detect_chargers(di, false);
2267 if (detected_chargers < 0)
2271 * Some chargers that breaks the USB spec is
2272 * identified as invalid by AB8500 and it refuse
2273 * to start the charging process. but by jumping
2274 * thru a few hoops it can be forced to start.
2276 if (is_ab8500(di->parent))
2277 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2278 AB8500_USB_LINE_STAT_REG, &val);
2280 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2281 AB8500_USB_LINK1_STAT_REG, &val);
2284 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2286 dev_dbg(di->dev, "Error reading USB link status\n");
2288 if (is_ab8500(di->parent))
2289 link_status = AB8500_USB_LINK_STATUS;
2291 link_status = AB8505_USB_LINK_STATUS;
2293 if (detected_chargers & USB_PW_CONN) {
2294 if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
2295 USB_STAT_NOT_VALID_LINK &&
2296 di->invalid_charger_detect_state == 0) {
2298 "Invalid charger detected, state= 0\n");
2300 abx500_mask_and_set_register_interruptible(di->dev,
2301 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
2302 USB_CH_ENA, USB_CH_ENA);
2303 /*Enable charger detection*/
2304 abx500_mask_and_set_register_interruptible(di->dev,
2305 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2306 USB_CH_DET, USB_CH_DET);
2307 di->invalid_charger_detect_state = 1;
2308 /*exit and wait for new link status interrupt.*/
2312 if (di->invalid_charger_detect_state == 1) {
2314 "Invalid charger detected, state= 1\n");
2315 /*Stop charger detection*/
2316 abx500_mask_and_set_register_interruptible(di->dev,
2317 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2319 /*Check link status*/
2320 if (is_ab8500(di->parent))
2321 ret = abx500_get_register_interruptible(di->dev,
2322 AB8500_USB, AB8500_USB_LINE_STAT_REG,
2325 ret = abx500_get_register_interruptible(di->dev,
2326 AB8500_USB, AB8500_USB_LINK1_STAT_REG,
2329 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2330 (val & link_status) >> USB_LINK_STATUS_SHIFT);
2331 di->invalid_charger_detect_state = 2;
2334 di->invalid_charger_detect_state = 0;
2337 if (!(detected_chargers & USB_PW_CONN)) {
2338 di->vbus_detected = false;
2339 ab8500_charger_set_usb_connected(di, false);
2340 ab8500_power_supply_changed(di, di->usb_chg.psy);
2344 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2345 di->vbus_detected = true;
2346 ret = ab8500_charger_read_usb_type(di);
2348 if (ret == -ENXIO) {
2349 /* No valid charger type detected */
2350 ab8500_charger_set_usb_connected(di, false);
2351 ab8500_power_supply_changed(di, di->usb_chg.psy);
2356 if (di->usb_device_is_unrecognised) {
2358 "Potential Legacy Charger device. "
2359 "Delay work for %d msec for USB enum "
2361 WAIT_ACA_RID_ENUMERATION);
2362 queue_delayed_work(di->charger_wq,
2364 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2365 } else if (di->is_aca_rid == 1) {
2366 /* Only wait once */
2369 "%s Wait %d msec for USB enum to finish",
2370 __func__, WAIT_ACA_RID_ENUMERATION);
2371 queue_delayed_work(di->charger_wq,
2373 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2375 queue_delayed_work(di->charger_wq,
2381 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2384 unsigned long flags;
2386 struct ab8500_charger *di = container_of(work,
2387 struct ab8500_charger, usb_state_changed_work.work);
2389 if (!di->vbus_detected) {
2391 "%s !di->vbus_detected\n",
2396 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2397 di->usb_state.state = di->usb_state.state_tmp;
2398 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
2399 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2401 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2402 __func__, di->usb_state.state, di->usb_state.usb_current);
2404 switch (di->usb_state.state) {
2405 case AB8500_BM_USB_STATE_RESET_HS:
2406 case AB8500_BM_USB_STATE_RESET_FS:
2407 case AB8500_BM_USB_STATE_SUSPEND:
2408 case AB8500_BM_USB_STATE_MAX:
2409 ab8500_charger_set_usb_connected(di, false);
2410 ab8500_power_supply_changed(di, di->usb_chg.psy);
2413 case AB8500_BM_USB_STATE_RESUME:
2415 * when suspend->resume there should be delay
2416 * of 1sec for enabling charging
2419 /* Intentional fall through */
2420 case AB8500_BM_USB_STATE_CONFIGURED:
2422 * USB is configured, enable charging with the charging
2423 * input current obtained from USB driver
2425 if (!ab8500_charger_get_usb_cur(di)) {
2426 /* Update maximum input current */
2427 ret = ab8500_charger_set_vbus_in_curr(di,
2428 di->max_usb_in_curr.usb_type_max);
2432 ab8500_charger_set_usb_connected(di, true);
2433 ab8500_power_supply_changed(di, di->usb_chg.psy);
2443 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2444 * @work: pointer to the work_struct structure
2446 * Work queue function for checking the USB charger Not OK status
2448 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2454 struct ab8500_charger *di = container_of(work,
2455 struct ab8500_charger, check_usbchgnotok_work.work);
2457 /* Check if the status bit for usbchargernotok is still active */
2458 ret = abx500_get_register_interruptible(di->dev,
2459 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2461 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2464 prev_status = di->flags.usbchargernotok;
2466 if (reg_value & VBUS_CH_NOK) {
2467 di->flags.usbchargernotok = true;
2468 /* Check again in 1sec */
2469 queue_delayed_work(di->charger_wq,
2470 &di->check_usbchgnotok_work, HZ);
2472 di->flags.usbchargernotok = false;
2473 di->flags.vbus_collapse = false;
2476 if (prev_status != di->flags.usbchargernotok)
2477 ab8500_power_supply_changed(di, di->usb_chg.psy);
2481 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2482 * @work: pointer to the work_struct structure
2484 * Work queue function for checking the Main thermal prot status
2486 static void ab8500_charger_check_main_thermal_prot_work(
2487 struct work_struct *work)
2492 struct ab8500_charger *di = container_of(work,
2493 struct ab8500_charger, check_main_thermal_prot_work);
2495 /* Check if the status bit for main_thermal_prot is still active */
2496 ret = abx500_get_register_interruptible(di->dev,
2497 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2499 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2502 if (reg_value & MAIN_CH_TH_PROT)
2503 di->flags.main_thermal_prot = true;
2505 di->flags.main_thermal_prot = false;
2507 ab8500_power_supply_changed(di, di->ac_chg.psy);
2511 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2512 * @work: pointer to the work_struct structure
2514 * Work queue function for checking the USB thermal prot status
2516 static void ab8500_charger_check_usb_thermal_prot_work(
2517 struct work_struct *work)
2522 struct ab8500_charger *di = container_of(work,
2523 struct ab8500_charger, check_usb_thermal_prot_work);
2525 /* Check if the status bit for usb_thermal_prot is still active */
2526 ret = abx500_get_register_interruptible(di->dev,
2527 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2529 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2532 if (reg_value & USB_CH_TH_PROT)
2533 di->flags.usb_thermal_prot = true;
2535 di->flags.usb_thermal_prot = false;
2537 ab8500_power_supply_changed(di, di->usb_chg.psy);
2541 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2542 * @irq: interrupt number
2543 * @_di: pointer to the ab8500_charger structure
2545 * Returns IRQ status(IRQ_HANDLED)
2547 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2549 struct ab8500_charger *di = _di;
2551 dev_dbg(di->dev, "Main charger unplugged\n");
2552 queue_work(di->charger_wq, &di->ac_work);
2554 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2555 mutex_lock(&di->charger_attached_mutex);
2556 mutex_unlock(&di->charger_attached_mutex);
2562 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2563 * @irq: interrupt number
2564 * @_di: pointer to the ab8500_charger structure
2566 * Returns IRQ status(IRQ_HANDLED)
2568 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2570 struct ab8500_charger *di = _di;
2572 dev_dbg(di->dev, "Main charger plugged\n");
2573 queue_work(di->charger_wq, &di->ac_work);
2575 mutex_lock(&di->charger_attached_mutex);
2576 mutex_unlock(&di->charger_attached_mutex);
2578 if (is_ab8500(di->parent))
2579 queue_delayed_work(di->charger_wq,
2580 &di->ac_charger_attached_work,
2586 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2587 * @irq: interrupt number
2588 * @_di: pointer to the ab8500_charger structure
2590 * Returns IRQ status(IRQ_HANDLED)
2592 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2594 struct ab8500_charger *di = _di;
2596 dev_dbg(di->dev, "Main charger not ok\n");
2597 di->flags.mainextchnotok = true;
2598 ab8500_power_supply_changed(di, di->ac_chg.psy);
2600 /* Schedule a new HW failure check */
2601 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2607 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2608 * thermal protection threshold
2609 * @irq: interrupt number
2610 * @_di: pointer to the ab8500_charger structure
2612 * Returns IRQ status(IRQ_HANDLED)
2614 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2616 struct ab8500_charger *di = _di;
2619 "Die temp above Main charger thermal protection threshold\n");
2620 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2626 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2627 * thermal protection threshold
2628 * @irq: interrupt number
2629 * @_di: pointer to the ab8500_charger structure
2631 * Returns IRQ status(IRQ_HANDLED)
2633 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2635 struct ab8500_charger *di = _di;
2638 "Die temp ok for Main charger thermal protection threshold\n");
2639 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2644 static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2646 struct ab8500_charger *di = container_of(work,
2647 struct ab8500_charger, vbus_drop_end_work.work);
2651 di->flags.vbus_drop_end = false;
2653 /* Reset the drop counter */
2654 abx500_set_register_interruptible(di->dev,
2655 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
2657 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2658 AB8500_CH_USBCH_STAT2_REG, ®_value);
2660 dev_err(di->dev, "%s read failed\n", __func__);
2664 curr = di->bm->chg_input_curr[
2665 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
2667 if (di->max_usb_in_curr.calculated_max != curr) {
2668 /* USB source is collapsing */
2669 di->max_usb_in_curr.calculated_max = curr;
2671 "VBUS input current limiting to %d mA\n",
2672 di->max_usb_in_curr.calculated_max);
2675 * USB source can not give more than this amount.
2676 * Taking more will collapse the source.
2678 di->max_usb_in_curr.set_max =
2679 di->max_usb_in_curr.calculated_max;
2681 "VBUS input current limited to %d mA\n",
2682 di->max_usb_in_curr.set_max);
2685 if (di->usb.charger_connected)
2686 ab8500_charger_set_vbus_in_curr(di,
2687 di->max_usb_in_curr.usb_type_max);
2691 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2692 * @irq: interrupt number
2693 * @_di: pointer to the ab8500_charger structure
2695 * Returns IRQ status(IRQ_HANDLED)
2697 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2699 struct ab8500_charger *di = _di;
2701 di->vbus_detected = false;
2702 dev_dbg(di->dev, "VBUS falling detected\n");
2703 queue_work(di->charger_wq, &di->detect_usb_type_work);
2709 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2710 * @irq: interrupt number
2711 * @_di: pointer to the ab8500_charger structure
2713 * Returns IRQ status(IRQ_HANDLED)
2715 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2717 struct ab8500_charger *di = _di;
2719 di->vbus_detected = true;
2720 dev_dbg(di->dev, "VBUS rising detected\n");
2722 queue_work(di->charger_wq, &di->detect_usb_type_work);
2728 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2729 * @irq: interrupt number
2730 * @_di: pointer to the ab8500_charger structure
2732 * Returns IRQ status(IRQ_HANDLED)
2734 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2736 struct ab8500_charger *di = _di;
2738 dev_dbg(di->dev, "USB link status changed\n");
2740 queue_work(di->charger_wq, &di->usb_link_status_work);
2746 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2747 * thermal protection threshold
2748 * @irq: interrupt number
2749 * @_di: pointer to the ab8500_charger structure
2751 * Returns IRQ status(IRQ_HANDLED)
2753 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2755 struct ab8500_charger *di = _di;
2758 "Die temp above USB charger thermal protection threshold\n");
2759 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2765 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2766 * thermal protection threshold
2767 * @irq: interrupt number
2768 * @_di: pointer to the ab8500_charger structure
2770 * Returns IRQ status(IRQ_HANDLED)
2772 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2774 struct ab8500_charger *di = _di;
2777 "Die temp ok for USB charger thermal protection threshold\n");
2778 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2784 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2785 * @irq: interrupt number
2786 * @_di: pointer to the ab8500_charger structure
2788 * Returns IRQ status(IRQ_HANDLED)
2790 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2792 struct ab8500_charger *di = _di;
2794 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2795 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2801 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2802 * @irq: interrupt number
2803 * @_di: pointer to the ab8500_charger structure
2805 * Returns IRQ status(IRQ_HANDLED)
2807 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2809 struct ab8500_charger *di = _di;
2811 dev_dbg(di->dev, "Charger watchdog expired\n");
2814 * The charger that was online when the watchdog expired
2815 * needs to be restarted for charging to start again
2817 if (di->ac.charger_online) {
2818 di->ac.wd_expired = true;
2819 ab8500_power_supply_changed(di, di->ac_chg.psy);
2821 if (di->usb.charger_online) {
2822 di->usb.wd_expired = true;
2823 ab8500_power_supply_changed(di, di->usb_chg.psy);
2830 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2831 * @irq: interrupt number
2832 * @_di: pointer to the ab8500_charger structure
2834 * Returns IRQ status(IRQ_HANDLED)
2836 static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2838 struct ab8500_charger *di = _di;
2840 dev_dbg(di->dev, "VBUS charger drop ended\n");
2841 di->flags.vbus_drop_end = true;
2844 * VBUS might have dropped due to bad connection.
2845 * Schedule a new input limit set to the value SW requests.
2847 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
2848 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
2854 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2855 * @irq: interrupt number
2856 * @_di: pointer to the ab8500_charger structure
2858 * Returns IRQ status(IRQ_HANDLED)
2860 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2862 struct ab8500_charger *di = _di;
2864 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2865 di->flags.vbus_ovv = true;
2866 ab8500_power_supply_changed(di, di->usb_chg.psy);
2868 /* Schedule a new HW failure check */
2869 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2875 * ab8500_charger_ac_get_property() - get the ac/mains properties
2876 * @psy: pointer to the power_supply structure
2877 * @psp: pointer to the power_supply_property structure
2878 * @val: pointer to the power_supply_propval union
2880 * This function gets called when an application tries to get the ac/mains
2881 * properties by reading the sysfs files.
2882 * AC/Mains properties are online, present and voltage.
2883 * online: ac/mains charging is in progress or not
2884 * present: presence of the ac/mains
2885 * voltage: AC/Mains voltage
2886 * Returns error code in case of failure else 0(on success)
2888 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2889 enum power_supply_property psp,
2890 union power_supply_propval *val)
2892 struct ab8500_charger *di;
2895 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2898 case POWER_SUPPLY_PROP_HEALTH:
2899 if (di->flags.mainextchnotok)
2900 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2901 else if (di->ac.wd_expired || di->usb.wd_expired)
2902 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2903 else if (di->flags.main_thermal_prot)
2904 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2906 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2908 case POWER_SUPPLY_PROP_ONLINE:
2909 val->intval = di->ac.charger_online;
2911 case POWER_SUPPLY_PROP_PRESENT:
2912 val->intval = di->ac.charger_connected;
2914 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2915 ret = ab8500_charger_get_ac_voltage(di);
2917 di->ac.charger_voltage = ret;
2918 /* On error, use previous value */
2919 val->intval = di->ac.charger_voltage * 1000;
2921 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2923 * This property is used to indicate when CV mode is entered
2924 * for the AC charger
2926 di->ac.cv_active = ab8500_charger_ac_cv(di);
2927 val->intval = di->ac.cv_active;
2929 case POWER_SUPPLY_PROP_CURRENT_NOW:
2930 ret = ab8500_charger_get_ac_current(di);
2932 di->ac.charger_current = ret;
2933 val->intval = di->ac.charger_current * 1000;
2942 * ab8500_charger_usb_get_property() - get the usb properties
2943 * @psy: pointer to the power_supply structure
2944 * @psp: pointer to the power_supply_property structure
2945 * @val: pointer to the power_supply_propval union
2947 * This function gets called when an application tries to get the usb
2948 * properties by reading the sysfs files.
2949 * USB properties are online, present and voltage.
2950 * online: usb charging is in progress or not
2951 * present: presence of the usb
2952 * voltage: vbus voltage
2953 * Returns error code in case of failure else 0(on success)
2955 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2956 enum power_supply_property psp,
2957 union power_supply_propval *val)
2959 struct ab8500_charger *di;
2962 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2965 case POWER_SUPPLY_PROP_HEALTH:
2966 if (di->flags.usbchargernotok)
2967 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2968 else if (di->ac.wd_expired || di->usb.wd_expired)
2969 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2970 else if (di->flags.usb_thermal_prot)
2971 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2972 else if (di->flags.vbus_ovv)
2973 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2975 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2977 case POWER_SUPPLY_PROP_ONLINE:
2978 val->intval = di->usb.charger_online;
2980 case POWER_SUPPLY_PROP_PRESENT:
2981 val->intval = di->usb.charger_connected;
2983 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2984 ret = ab8500_charger_get_vbus_voltage(di);
2986 di->usb.charger_voltage = ret;
2987 val->intval = di->usb.charger_voltage * 1000;
2989 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2991 * This property is used to indicate when CV mode is entered
2992 * for the USB charger
2994 di->usb.cv_active = ab8500_charger_usb_cv(di);
2995 val->intval = di->usb.cv_active;
2997 case POWER_SUPPLY_PROP_CURRENT_NOW:
2998 ret = ab8500_charger_get_usb_current(di);
3000 di->usb.charger_current = ret;
3001 val->intval = di->usb.charger_current * 1000;
3003 case POWER_SUPPLY_PROP_CURRENT_AVG:
3005 * This property is used to indicate when VBUS has collapsed
3006 * due to too high output current from the USB charger
3008 if (di->flags.vbus_collapse)
3020 * ab8500_charger_init_hw_registers() - Set up charger related registers
3021 * @di: pointer to the ab8500_charger structure
3023 * Set up charger OVV, watchdog and maximum voltage registers as well as
3024 * charging of the backup battery
3026 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3029 u8 bup_vch_range = 0, vbup33_vrtcn = 0;
3031 /* Setup maximum charger current and voltage for ABB cut2.0 */
3032 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3033 ret = abx500_set_register_interruptible(di->dev,
3035 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3038 "failed to set CH_VOLT_LVL_MAX_REG\n");
3042 ret = abx500_set_register_interruptible(di->dev,
3043 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3047 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3052 if (is_ab8505_2p0(di->parent))
3053 ret = abx500_mask_and_set_register_interruptible(di->dev,
3055 AB8500_USBCH_CTRL2_REG,
3056 VBUS_AUTO_IN_CURR_LIM_ENA,
3057 VBUS_AUTO_IN_CURR_LIM_ENA);
3060 * VBUS OVV set to 6.3V and enable automatic current limitation
3062 ret = abx500_set_register_interruptible(di->dev,
3064 AB8500_USBCH_CTRL2_REG,
3065 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
3068 "failed to set automatic current limitation\n");
3072 /* Enable main watchdog in OTP */
3073 ret = abx500_set_register_interruptible(di->dev,
3074 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3076 dev_err(di->dev, "failed to enable main WD in OTP\n");
3080 /* Enable main watchdog */
3081 ret = abx500_set_register_interruptible(di->dev,
3082 AB8500_SYS_CTRL2_BLOCK,
3083 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3085 dev_err(di->dev, "failed to enable main watchdog\n");
3090 * Due to internal synchronisation, Enable and Kick watchdog bits
3091 * cannot be enabled in a single write.
3092 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3093 * between writing Enable then Kick bits.
3097 /* Kick main watchdog */
3098 ret = abx500_set_register_interruptible(di->dev,
3099 AB8500_SYS_CTRL2_BLOCK,
3100 AB8500_MAIN_WDOG_CTRL_REG,
3101 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3103 dev_err(di->dev, "failed to kick main watchdog\n");
3107 /* Disable main watchdog */
3108 ret = abx500_set_register_interruptible(di->dev,
3109 AB8500_SYS_CTRL2_BLOCK,
3110 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3112 dev_err(di->dev, "failed to disable main watchdog\n");
3116 /* Set watchdog timeout */
3117 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3118 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3120 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3124 ret = ab8500_charger_led_en(di, false);
3126 dev_err(di->dev, "failed to disable LED\n");
3130 /* Backup battery voltage and current */
3131 if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V)
3132 bup_vch_range = BUP_VCH_RANGE;
3133 if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V)
3134 vbup33_vrtcn = VBUP33_VRTCN;
3136 ret = abx500_set_register_interruptible(di->dev,
3138 AB8500_RTC_BACKUP_CHG_REG,
3139 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
3141 dev_err(di->dev, "failed to setup backup battery charging\n");
3145 /* Enable backup battery charging */
3146 ret = abx500_mask_and_set_register_interruptible(di->dev,
3147 AB8500_RTC, AB8500_RTC_CTRL_REG,
3148 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3150 dev_err(di->dev, "%s mask and set failed\n", __func__);
3159 * ab8500 charger driver interrupts and their respective isr
3161 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3162 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3163 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3164 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3165 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3166 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3167 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3168 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3169 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3170 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3171 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3172 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3173 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3174 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
3175 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
3178 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3179 unsigned long event, void *power)
3181 struct ab8500_charger *di =
3182 container_of(nb, struct ab8500_charger, nb);
3183 enum ab8500_usb_state bm_usb_state;
3184 unsigned mA = *((unsigned *)power);
3189 if (event != USB_EVENT_VBUS) {
3190 dev_dbg(di->dev, "not a standard host, returning\n");
3194 /* TODO: State is fabricate here. See if charger really needs USB
3195 * state or if mA is enough
3197 if ((di->usb_state.usb_current == 2) && (mA > 2))
3198 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3200 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3202 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3203 else if (mA >= 8) /* 8, 100, 500 */
3204 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3205 else /* Should never occur */
3206 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3208 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3209 __func__, bm_usb_state, mA);
3211 spin_lock(&di->usb_state.usb_lock);
3212 di->usb_state.state_tmp = bm_usb_state;
3213 di->usb_state.usb_current_tmp = mA;
3214 spin_unlock(&di->usb_state.usb_lock);
3217 * wait for some time until you get updates from the usb stack
3218 * and negotiations are completed
3220 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
3225 #if defined(CONFIG_PM)
3226 static int ab8500_charger_resume(struct platform_device *pdev)
3229 struct ab8500_charger *di = platform_get_drvdata(pdev);
3232 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3233 * logic. That means we have to continously kick the charger
3234 * watchdog even when no charger is connected. This is only
3235 * valid once the AC charger has been enabled. This is
3236 * a bug that is not handled by the algorithm and the
3237 * watchdog have to be kicked by the charger driver
3238 * when the AC charger is disabled
3240 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3241 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3242 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3244 dev_err(di->dev, "Failed to kick WD!\n");
3246 /* If not already pending start a new timer */
3247 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3248 round_jiffies(WD_KICK_INTERVAL));
3251 /* If we still have a HW failure, schedule a new check */
3252 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3253 queue_delayed_work(di->charger_wq,
3254 &di->check_hw_failure_work, 0);
3257 if (di->flags.vbus_drop_end)
3258 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3263 static int ab8500_charger_suspend(struct platform_device *pdev,
3266 struct ab8500_charger *di = platform_get_drvdata(pdev);
3268 /* Cancel any pending jobs */
3269 cancel_delayed_work(&di->check_hw_failure_work);
3270 cancel_delayed_work(&di->vbus_drop_end_work);
3272 flush_delayed_work(&di->attach_work);
3273 flush_delayed_work(&di->usb_charger_attached_work);
3274 flush_delayed_work(&di->ac_charger_attached_work);
3275 flush_delayed_work(&di->check_usbchgnotok_work);
3276 flush_delayed_work(&di->check_vbat_work);
3277 flush_delayed_work(&di->kick_wd_work);
3279 flush_work(&di->usb_link_status_work);
3280 flush_work(&di->ac_work);
3281 flush_work(&di->detect_usb_type_work);
3283 if (atomic_read(&di->current_stepping_sessions))
3289 #define ab8500_charger_suspend NULL
3290 #define ab8500_charger_resume NULL
3293 static struct notifier_block charger_nb = {
3294 .notifier_call = ab8500_external_charger_prepare,
3297 static int ab8500_charger_remove(struct platform_device *pdev)
3299 struct ab8500_charger *di = platform_get_drvdata(pdev);
3302 /* Disable AC charging */
3303 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3305 /* Disable USB charging */
3306 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3308 /* Disable interrupts */
3309 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3310 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3314 /* Backup battery voltage and current disable */
3315 ret = abx500_mask_and_set_register_interruptible(di->dev,
3316 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3318 dev_err(di->dev, "%s mask and set failed\n", __func__);
3320 usb_unregister_notifier(di->usb_phy, &di->nb);
3321 usb_put_phy(di->usb_phy);
3323 /* Delete the work queue */
3324 destroy_workqueue(di->charger_wq);
3326 /* Unregister external charger enable notifier */
3327 if (!di->ac_chg.enabled)
3328 blocking_notifier_chain_unregister(
3329 &charger_notifier_list, &charger_nb);
3331 flush_scheduled_work();
3332 if (di->usb_chg.enabled)
3333 power_supply_unregister(di->usb_chg.psy);
3335 if (di->ac_chg.enabled && !di->ac_chg.external)
3336 power_supply_unregister(di->ac_chg.psy);
3341 static char *supply_interface[] = {
3347 static const struct power_supply_desc ab8500_ac_chg_desc = {
3348 .name = "ab8500_ac",
3349 .type = POWER_SUPPLY_TYPE_MAINS,
3350 .properties = ab8500_charger_ac_props,
3351 .num_properties = ARRAY_SIZE(ab8500_charger_ac_props),
3352 .get_property = ab8500_charger_ac_get_property,
3355 static const struct power_supply_desc ab8500_usb_chg_desc = {
3356 .name = "ab8500_usb",
3357 .type = POWER_SUPPLY_TYPE_USB,
3358 .properties = ab8500_charger_usb_props,
3359 .num_properties = ARRAY_SIZE(ab8500_charger_usb_props),
3360 .get_property = ab8500_charger_usb_get_property,
3363 static int ab8500_charger_probe(struct platform_device *pdev)
3365 struct device_node *np = pdev->dev.of_node;
3366 struct abx500_bm_data *plat = pdev->dev.platform_data;
3367 struct power_supply_config ac_psy_cfg = {}, usb_psy_cfg = {};
3368 struct ab8500_charger *di;
3369 int irq, i, charger_status, ret = 0, ch_stat;
3371 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3373 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
3378 dev_err(&pdev->dev, "no battery management data supplied\n");
3384 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3386 dev_err(&pdev->dev, "failed to get battery information\n");
3389 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3391 di->autopower_cfg = false;
3393 /* get parent data */
3394 di->dev = &pdev->dev;
3395 di->parent = dev_get_drvdata(pdev->dev.parent);
3396 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3398 /* initialize lock */
3399 spin_lock_init(&di->usb_state.usb_lock);
3400 mutex_init(&di->usb_ipt_crnt_lock);
3402 di->autopower = false;
3403 di->invalid_charger_detect_state = 0;
3405 /* AC and USB supply config */
3406 ac_psy_cfg.supplied_to = supply_interface;
3407 ac_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3408 ac_psy_cfg.drv_data = &di->ac_chg;
3409 usb_psy_cfg.supplied_to = supply_interface;
3410 usb_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3411 usb_psy_cfg.drv_data = &di->usb_chg;
3414 /* ux500_charger sub-class */
3415 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
3416 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
3417 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3418 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3419 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3420 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3421 di->ac_chg.max_out_curr =
3422 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3423 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
3424 di->ac_chg.enabled = di->bm->ac_enabled;
3425 di->ac_chg.external = false;
3427 /*notifier for external charger enabling*/
3428 if (!di->ac_chg.enabled)
3429 blocking_notifier_chain_register(
3430 &charger_notifier_list, &charger_nb);
3433 /* ux500_charger sub-class */
3434 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
3435 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
3436 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3437 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3438 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3439 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3440 di->usb_chg.max_out_curr =
3441 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3442 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
3443 di->usb_chg.enabled = di->bm->usb_enabled;
3444 di->usb_chg.external = false;
3445 di->usb_state.usb_current = -1;
3447 /* Create a work queue for the charger */
3448 di->charger_wq = alloc_ordered_workqueue("ab8500_charger_wq",
3450 if (di->charger_wq == NULL) {
3451 dev_err(di->dev, "failed to create work queue\n");
3455 mutex_init(&di->charger_attached_mutex);
3457 /* Init work for HW failure check */
3458 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
3459 ab8500_charger_check_hw_failure_work);
3460 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
3461 ab8500_charger_check_usbchargernotok_work);
3463 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3464 ab8500_charger_ac_attached_work);
3465 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3466 ab8500_charger_usb_attached_work);
3469 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3470 * logic. That means we have to continously kick the charger
3471 * watchdog even when no charger is connected. This is only
3472 * valid once the AC charger has been enabled. This is
3473 * a bug that is not handled by the algorithm and the
3474 * watchdog have to be kicked by the charger driver
3475 * when the AC charger is disabled
3477 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
3478 ab8500_charger_kick_watchdog_work);
3480 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
3481 ab8500_charger_check_vbat_work);
3483 INIT_DELAYED_WORK(&di->attach_work,
3484 ab8500_charger_usb_link_attach_work);
3486 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3487 ab8500_charger_usb_state_changed_work);
3489 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3490 ab8500_charger_vbus_drop_end_work);
3492 /* Init work for charger detection */
3493 INIT_WORK(&di->usb_link_status_work,
3494 ab8500_charger_usb_link_status_work);
3495 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3496 INIT_WORK(&di->detect_usb_type_work,
3497 ab8500_charger_detect_usb_type_work);
3499 /* Init work for checking HW status */
3500 INIT_WORK(&di->check_main_thermal_prot_work,
3501 ab8500_charger_check_main_thermal_prot_work);
3502 INIT_WORK(&di->check_usb_thermal_prot_work,
3503 ab8500_charger_check_usb_thermal_prot_work);
3506 * VDD ADC supply needs to be enabled from this driver when there
3507 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3508 * interrupts during charging
3510 di->regu = devm_regulator_get(di->dev, "vddadc");
3511 if (IS_ERR(di->regu)) {
3512 ret = PTR_ERR(di->regu);
3513 dev_err(di->dev, "failed to get vddadc regulator\n");
3514 goto free_charger_wq;
3518 /* Initialize OVV, and other registers */
3519 ret = ab8500_charger_init_hw_registers(di);
3521 dev_err(di->dev, "failed to initialize ABB registers\n");
3522 goto free_charger_wq;
3525 /* Register AC charger class */
3526 if (di->ac_chg.enabled) {
3527 di->ac_chg.psy = power_supply_register(di->dev,
3528 &ab8500_ac_chg_desc,
3530 if (IS_ERR(di->ac_chg.psy)) {
3531 dev_err(di->dev, "failed to register AC charger\n");
3532 ret = PTR_ERR(di->ac_chg.psy);
3533 goto free_charger_wq;
3537 /* Register USB charger class */
3538 if (di->usb_chg.enabled) {
3539 di->usb_chg.psy = power_supply_register(di->dev,
3540 &ab8500_usb_chg_desc,
3542 if (IS_ERR(di->usb_chg.psy)) {
3543 dev_err(di->dev, "failed to register USB charger\n");
3544 ret = PTR_ERR(di->usb_chg.psy);
3549 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3550 if (IS_ERR_OR_NULL(di->usb_phy)) {
3551 dev_err(di->dev, "failed to get usb transceiver\n");
3555 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3556 ret = usb_register_notifier(di->usb_phy, &di->nb);
3558 dev_err(di->dev, "failed to register usb notifier\n");
3562 /* Identify the connected charger types during startup */
3563 charger_status = ab8500_charger_detect_chargers(di, true);
3564 if (charger_status & AC_PW_CONN) {
3565 di->ac.charger_connected = 1;
3567 ab8500_power_supply_changed(di, di->ac_chg.psy);
3568 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
3571 if (charger_status & USB_PW_CONN) {
3572 di->vbus_detected = true;
3573 di->vbus_detected_start = true;
3574 queue_work(di->charger_wq,
3575 &di->detect_usb_type_work);
3578 /* Register interrupts */
3579 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3580 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3581 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3582 IRQF_SHARED | IRQF_NO_SUSPEND,
3583 ab8500_charger_irq[i].name, di);
3586 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3587 , ab8500_charger_irq[i].name, irq, ret);
3590 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3591 ab8500_charger_irq[i].name, irq, ret);
3594 platform_set_drvdata(pdev, di);
3596 mutex_lock(&di->charger_attached_mutex);
3598 ch_stat = ab8500_charger_detect_chargers(di, false);
3600 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3601 if (is_ab8500(di->parent))
3602 queue_delayed_work(di->charger_wq,
3603 &di->ac_charger_attached_work,
3606 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3607 if (is_ab8500(di->parent))
3608 queue_delayed_work(di->charger_wq,
3609 &di->usb_charger_attached_work,
3613 mutex_unlock(&di->charger_attached_mutex);
3618 usb_unregister_notifier(di->usb_phy, &di->nb);
3620 /* We also have to free all successfully registered irqs */
3621 for (i = i - 1; i >= 0; i--) {
3622 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3626 usb_put_phy(di->usb_phy);
3628 if (di->usb_chg.enabled)
3629 power_supply_unregister(di->usb_chg.psy);
3631 if (di->ac_chg.enabled)
3632 power_supply_unregister(di->ac_chg.psy);
3634 destroy_workqueue(di->charger_wq);
3638 static const struct of_device_id ab8500_charger_match[] = {
3639 { .compatible = "stericsson,ab8500-charger", },
3642 MODULE_DEVICE_TABLE(of, ab8500_charger_match);
3644 static struct platform_driver ab8500_charger_driver = {
3645 .probe = ab8500_charger_probe,
3646 .remove = ab8500_charger_remove,
3647 .suspend = ab8500_charger_suspend,
3648 .resume = ab8500_charger_resume,
3650 .name = "ab8500-charger",
3651 .of_match_table = ab8500_charger_match,
3655 static int __init ab8500_charger_init(void)
3657 return platform_driver_register(&ab8500_charger_driver);
3660 static void __exit ab8500_charger_exit(void)
3662 platform_driver_unregister(&ab8500_charger_driver);
3665 subsys_initcall_sync(ab8500_charger_init);
3666 module_exit(ab8500_charger_exit);
3668 MODULE_LICENSE("GPL v2");
3669 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3670 MODULE_ALIAS("platform:ab8500-charger");
3671 MODULE_DESCRIPTION("AB8500 charger management driver");