2 * Copyright (C) ST-Ericsson SA 2012
4 * Charger driver for AB8500
6 * License Terms: GNU General Public License v2
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/notifier.h>
19 #include <linux/slab.h>
20 #include <linux/platform_device.h>
21 #include <linux/power_supply.h>
22 #include <linux/completion.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/err.h>
25 #include <linux/workqueue.h>
26 #include <linux/kobject.h>
28 #include <linux/mfd/core.h>
29 #include <linux/mfd/abx500/ab8500.h>
30 #include <linux/mfd/abx500.h>
31 #include <linux/mfd/abx500/ab8500-bm.h>
32 #include <linux/mfd/abx500/ab8500-gpadc.h>
33 #include <linux/mfd/abx500/ux500_chargalg.h>
34 #include <linux/usb/otg.h>
35 #include <linux/mutex.h>
37 /* Charger constants */
42 #define MAIN_WDOG_ENA 0x01
43 #define MAIN_WDOG_KICK 0x02
44 #define MAIN_WDOG_DIS 0x00
45 #define CHARG_WD_KICK 0x01
46 #define MAIN_CH_ENA 0x01
47 #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
48 #define USB_CH_ENA 0x01
49 #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
50 #define MAIN_CH_DET 0x01
51 #define MAIN_CH_CV_ON 0x04
52 #define USB_CH_CV_ON 0x08
53 #define VBUS_DET_DBNC100 0x02
54 #define VBUS_DET_DBNC1 0x01
55 #define OTP_ENABLE_WD 0x01
56 #define DROP_COUNT_RESET 0x01
57 #define USB_CH_DET 0x01
59 #define MAIN_CH_INPUT_CURR_SHIFT 4
60 #define VBUS_IN_CURR_LIM_SHIFT 4
61 #define AB8540_VBUS_IN_CURR_LIM_SHIFT 2
62 #define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
63 #define AB8540_AUTO_VBUS_IN_CURR_MASK 0x3F
64 #define VBUS_IN_CURR_LIM_RETRY_SET_TIME 30 /* seconds */
66 #define LED_INDICATOR_PWM_ENA 0x01
67 #define LED_INDICATOR_PWM_DIS 0x00
68 #define LED_IND_CUR_5MA 0x04
69 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
71 /* HW failure constants */
72 #define MAIN_CH_TH_PROT 0x02
73 #define VBUS_CH_NOK 0x08
74 #define USB_CH_TH_PROT 0x02
75 #define VBUS_OVV_TH 0x01
76 #define MAIN_CH_NOK 0x01
79 #define MAIN_CH_STATUS2_MAINCHGDROP 0x80
80 #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
81 #define USB_CH_VBUSDROP 0x40
82 #define USB_CH_VBUSDETDBNC 0x01
84 /* UsbLineStatus register bit masks */
85 #define AB8500_USB_LINK_STATUS 0x78
86 #define AB8505_USB_LINK_STATUS 0xF8
87 #define AB8500_STD_HOST_SUSP 0x18
88 #define USB_LINK_STATUS_SHIFT 3
90 /* Watchdog timeout constant */
91 #define WD_TIMER 0x30 /* 4min */
92 #define WD_KICK_INTERVAL (60 * HZ)
94 /* Lowest charger voltage is 3.39V -> 0x4E */
95 #define LOW_VOLT_REG 0x4E
97 /* Step up/down delay in us */
98 #define STEP_UDELAY 1000
100 #define CHARGER_STATUS_POLL 10 /* in ms */
102 #define CHG_WD_INTERVAL (60 * HZ)
104 #define AB8500_SW_CONTROL_FALLBACK 0x03
105 /* Wait for enumeration before charing in us */
106 #define WAIT_ACA_RID_ENUMERATION (5 * 1000)
107 /*External charger control*/
108 #define AB8500_SYS_CHARGER_CONTROL_REG 0x52
109 #define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
110 #define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
112 /* UsbLineStatus register - usb types */
113 enum ab8500_charger_link_status {
114 USB_STAT_NOT_CONFIGURED,
115 USB_STAT_STD_HOST_NC,
116 USB_STAT_STD_HOST_C_NS,
117 USB_STAT_STD_HOST_C_S,
118 USB_STAT_HOST_CHG_NM,
119 USB_STAT_HOST_CHG_HS,
120 USB_STAT_HOST_CHG_HS_CHIRP,
121 USB_STAT_DEDICATED_CHG,
124 USB_STAT_ACA_RID_C_NM,
125 USB_STAT_ACA_RID_C_HS,
126 USB_STAT_ACA_RID_C_HS_CHIRP,
129 USB_STAT_NOT_VALID_LINK,
131 USB_STAT_SUP_NO_IDGND_VBUS,
132 USB_STAT_SUP_IDGND_VBUS,
133 USB_STAT_CHARGER_LINE_1,
136 USB_STAT_ACA_DOCK_CHARGER,
139 enum ab8500_usb_state {
140 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
141 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
142 AB8500_BM_USB_STATE_CONFIGURED,
143 AB8500_BM_USB_STATE_SUSPEND,
144 AB8500_BM_USB_STATE_RESUME,
145 AB8500_BM_USB_STATE_MAX,
148 /* VBUS input current limits supported in AB8500 in mA */
149 #define USB_CH_IP_CUR_LVL_0P05 50
150 #define USB_CH_IP_CUR_LVL_0P09 98
151 #define USB_CH_IP_CUR_LVL_0P19 193
152 #define USB_CH_IP_CUR_LVL_0P29 290
153 #define USB_CH_IP_CUR_LVL_0P38 380
154 #define USB_CH_IP_CUR_LVL_0P45 450
155 #define USB_CH_IP_CUR_LVL_0P5 500
156 #define USB_CH_IP_CUR_LVL_0P6 600
157 #define USB_CH_IP_CUR_LVL_0P7 700
158 #define USB_CH_IP_CUR_LVL_0P8 800
159 #define USB_CH_IP_CUR_LVL_0P9 900
160 #define USB_CH_IP_CUR_LVL_1P0 1000
161 #define USB_CH_IP_CUR_LVL_1P1 1100
162 #define USB_CH_IP_CUR_LVL_1P3 1300
163 #define USB_CH_IP_CUR_LVL_1P4 1400
164 #define USB_CH_IP_CUR_LVL_1P5 1500
166 #define VBAT_TRESH_IP_CUR_RED 3800
168 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
169 struct ab8500_charger, usb_chg)
170 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
171 struct ab8500_charger, ac_chg)
174 * struct ab8500_charger_interrupts - ab8500 interupts
175 * @name: name of the interrupt
176 * @isr function pointer to the isr
178 struct ab8500_charger_interrupts {
180 irqreturn_t (*isr)(int irq, void *data);
183 struct ab8500_charger_info {
184 int charger_connected;
192 struct ab8500_charger_event_flags {
194 bool main_thermal_prot;
195 bool usb_thermal_prot;
197 bool usbchargernotok;
203 struct ab8500_charger_usb_state {
206 enum ab8500_usb_state state;
207 enum ab8500_usb_state state_tmp;
211 struct ab8500_charger_max_usb_in_curr {
218 * struct ab8500_charger - ab8500 Charger device information
219 * @dev: Pointer to the structure device
220 * @vbus_detected: VBUS detected
221 * @vbus_detected_start:
222 * VBUS detected during startup
223 * @ac_conn: This will be true when the AC charger has been plugged
224 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
226 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
228 * @vbat Battery voltage
229 * @old_vbat Previously measured battery voltage
230 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
231 * @autopower Indicate if we should have automatic pwron after pwrloss
232 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
233 * @invalid_charger_detect_state State when forcing AB to use invalid charger
234 * @is_aca_rid: Incicate if accessory is ACA type
235 * @current_stepping_sessions:
236 * Counter for current stepping sessions
237 * @parent: Pointer to the struct ab8500
238 * @gpadc: Pointer to the struct gpadc
239 * @bm: Platform specific battery management information
240 * @flags: Structure for information about events triggered
241 * @usb_state: Structure for usb stack information
242 * @max_usb_in_curr: Max USB charger input current
243 * @ac_chg: AC charger power supply
244 * @usb_chg: USB charger power supply
245 * @ac: Structure that holds the AC charger properties
246 * @usb: Structure that holds the USB charger properties
247 * @regu: Pointer to the struct regulator
248 * @charger_wq: Work queue for the IRQs and checking HW state
249 * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
250 * @pm_lock: Lock to prevent system to suspend
251 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
252 * @check_hw_failure_work: Work for checking HW state
253 * @check_usbchgnotok_work: Work for checking USB charger not ok status
254 * @kick_wd_work: Work for kicking the charger watchdog in case
255 * of ABB rev 1.* due to the watchog logic bug
256 * @ac_charger_attached_work: Work for checking if AC charger is still
258 * @usb_charger_attached_work: Work for checking if USB charger is still
260 * @ac_work: Work for checking AC charger connection
261 * @detect_usb_type_work: Work for detecting the USB type connected
262 * @usb_link_status_work: Work for checking the new USB link status
263 * @usb_state_changed_work: Work for checking USB state
264 * @attach_work: Work for detecting USB type
265 * @vbus_drop_end_work: Work for detecting VBUS drop end
266 * @check_main_thermal_prot_work:
267 * Work for checking Main thermal status
268 * @check_usb_thermal_prot_work:
269 * Work for checking USB thermal status
270 * @charger_attached_mutex: For controlling the wakelock
272 struct ab8500_charger {
275 bool vbus_detected_start;
281 bool usb_device_is_unrecognised;
284 int invalid_charger_detect_state;
286 atomic_t current_stepping_sessions;
287 struct ab8500 *parent;
288 struct ab8500_gpadc *gpadc;
289 struct abx500_bm_data *bm;
290 struct ab8500_charger_event_flags flags;
291 struct ab8500_charger_usb_state usb_state;
292 struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
293 struct ux500_charger ac_chg;
294 struct ux500_charger usb_chg;
295 struct ab8500_charger_info ac;
296 struct ab8500_charger_info usb;
297 struct regulator *regu;
298 struct workqueue_struct *charger_wq;
299 struct mutex usb_ipt_crnt_lock;
300 struct delayed_work check_vbat_work;
301 struct delayed_work check_hw_failure_work;
302 struct delayed_work check_usbchgnotok_work;
303 struct delayed_work kick_wd_work;
304 struct delayed_work usb_state_changed_work;
305 struct delayed_work attach_work;
306 struct delayed_work ac_charger_attached_work;
307 struct delayed_work usb_charger_attached_work;
308 struct delayed_work vbus_drop_end_work;
309 struct work_struct ac_work;
310 struct work_struct detect_usb_type_work;
311 struct work_struct usb_link_status_work;
312 struct work_struct check_main_thermal_prot_work;
313 struct work_struct check_usb_thermal_prot_work;
314 struct usb_phy *usb_phy;
315 struct notifier_block nb;
316 struct mutex charger_attached_mutex;
320 static enum power_supply_property ab8500_charger_ac_props[] = {
321 POWER_SUPPLY_PROP_HEALTH,
322 POWER_SUPPLY_PROP_PRESENT,
323 POWER_SUPPLY_PROP_ONLINE,
324 POWER_SUPPLY_PROP_VOLTAGE_NOW,
325 POWER_SUPPLY_PROP_VOLTAGE_AVG,
326 POWER_SUPPLY_PROP_CURRENT_NOW,
330 static enum power_supply_property ab8500_charger_usb_props[] = {
331 POWER_SUPPLY_PROP_HEALTH,
332 POWER_SUPPLY_PROP_CURRENT_AVG,
333 POWER_SUPPLY_PROP_PRESENT,
334 POWER_SUPPLY_PROP_ONLINE,
335 POWER_SUPPLY_PROP_VOLTAGE_NOW,
336 POWER_SUPPLY_PROP_VOLTAGE_AVG,
337 POWER_SUPPLY_PROP_CURRENT_NOW,
341 * Function for enabling and disabling sw fallback mode
342 * should always be disabled when no charger is connected.
344 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
353 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
355 if (is_ab8500(di->parent)) {
360 bank = AB8500_SYS_CTRL1_BLOCK;
361 reg = AB8500_SW_CONTROL_FALLBACK;
365 /* read the register containing fallback bit */
366 ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
368 dev_err(di->dev, "%d read failed\n", __LINE__);
372 if (is_ab8500(di->parent)) {
373 /* enable the OPT emulation registers */
374 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
376 dev_err(di->dev, "%d write failed\n", __LINE__);
386 /* write back the changed fallback bit value to register */
387 ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
389 dev_err(di->dev, "%d write failed\n", __LINE__);
393 if (is_ab8500(di->parent)) {
394 /* disable the set OTP registers again */
395 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
397 dev_err(di->dev, "%d write failed\n", __LINE__);
403 * ab8500_power_supply_changed - a wrapper with local extentions for
404 * power_supply_changed
405 * @di: pointer to the ab8500_charger structure
406 * @psy: pointer to power_supply_that have changed.
409 static void ab8500_power_supply_changed(struct ab8500_charger *di,
410 struct power_supply *psy)
413 * This happens if we get notifications or interrupts and
414 * the platform has been configured not to support one or
415 * other type of charging.
420 if (di->autopower_cfg) {
421 if (!di->usb.charger_connected &&
422 !di->ac.charger_connected &&
424 di->autopower = false;
425 ab8500_enable_disable_sw_fallback(di, false);
426 } else if (!di->autopower &&
427 (di->ac.charger_connected ||
428 di->usb.charger_connected)) {
429 di->autopower = true;
430 ab8500_enable_disable_sw_fallback(di, true);
433 power_supply_changed(psy);
436 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
439 if (connected != di->usb.charger_connected) {
440 dev_dbg(di->dev, "USB connected:%i\n", connected);
441 di->usb.charger_connected = connected;
444 di->flags.vbus_drop_end = false;
447 * Sometimes the platform is configured not to support
448 * USB charging and no psy has been created, but we still
449 * will get these notifications.
451 if (di->usb_chg.psy) {
452 sysfs_notify(&di->usb_chg.psy->dev.kobj, NULL,
457 mutex_lock(&di->charger_attached_mutex);
458 mutex_unlock(&di->charger_attached_mutex);
460 if (is_ab8500(di->parent))
461 queue_delayed_work(di->charger_wq,
462 &di->usb_charger_attached_work,
465 cancel_delayed_work_sync(&di->usb_charger_attached_work);
466 mutex_lock(&di->charger_attached_mutex);
467 mutex_unlock(&di->charger_attached_mutex);
473 * ab8500_charger_get_ac_voltage() - get ac charger voltage
474 * @di: pointer to the ab8500_charger structure
476 * Returns ac charger voltage (on success)
478 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
482 /* Only measure voltage if the charger is connected */
483 if (di->ac.charger_connected) {
484 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
486 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
494 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
495 * @di: pointer to the ab8500_charger structure
497 * Returns ac charger CV mode (on success) else error code
499 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
504 /* Only check CV mode if the charger is online */
505 if (di->ac.charger_online) {
506 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
507 AB8500_CH_STATUS1_REG, &val);
509 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
513 if (val & MAIN_CH_CV_ON)
523 * ab8500_charger_get_vbus_voltage() - get vbus voltage
524 * @di: pointer to the ab8500_charger structure
526 * This function returns the vbus voltage.
527 * Returns vbus voltage (on success)
529 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
533 /* Only measure voltage if the charger is connected */
534 if (di->usb.charger_connected) {
535 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
537 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
545 * ab8500_charger_get_usb_current() - get usb charger current
546 * @di: pointer to the ab8500_charger structure
548 * This function returns the usb charger current.
549 * Returns usb current (on success) and error code on failure
551 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
555 /* Only measure current if the charger is online */
556 if (di->usb.charger_online) {
557 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
559 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
567 * ab8500_charger_get_ac_current() - get ac charger current
568 * @di: pointer to the ab8500_charger structure
570 * This function returns the ac charger current.
571 * Returns ac current (on success) and error code on failure.
573 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
577 /* Only measure current if the charger is online */
578 if (di->ac.charger_online) {
579 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
581 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
589 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
590 * @di: pointer to the ab8500_charger structure
592 * Returns ac charger CV mode (on success) else error code
594 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
599 /* Only check CV mode if the charger is online */
600 if (di->usb.charger_online) {
601 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
602 AB8500_CH_USBCH_STAT1_REG, &val);
604 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
608 if (val & USB_CH_CV_ON)
620 * ab8500_charger_detect_chargers() - Detect the connected chargers
621 * @di: pointer to the ab8500_charger structure
622 * @probe: if probe, don't delay and wait for HW
624 * Returns the type of charger connected.
625 * For USB it will not mean we can actually charge from it
626 * but that there is a USB cable connected that we have to
627 * identify. This is used during startup when we don't get
628 * interrupts of the charger detection
630 * Returns an integer value, that means,
631 * NO_PW_CONN no power supply is connected
632 * AC_PW_CONN if the AC power supply is connected
633 * USB_PW_CONN if the USB power supply is connected
634 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
636 static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
638 int result = NO_PW_CONN;
642 /* Check for AC charger */
643 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
644 AB8500_CH_STATUS1_REG, &val);
646 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
650 if (val & MAIN_CH_DET)
653 /* Check for USB charger */
657 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
658 * when disconnecting ACA even though no
659 * charger was connected. Try waiting a little
660 * longer than the 100 ms of VBUS_DET_DBNC100...
664 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
665 AB8500_CH_USBCH_STAT1_REG, &val);
667 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
671 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
673 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
674 result |= USB_PW_CONN;
680 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
681 * @di: pointer to the ab8500_charger structure
682 * @link_status: the identified USB type
684 * Get the maximum current that is allowed to be drawn from the host
685 * based on the USB type.
686 * Returns error code in case of failure else 0 on success
688 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
689 enum ab8500_charger_link_status link_status)
693 di->usb_device_is_unrecognised = false;
696 * Platform only supports USB 2.0.
697 * This means that charging current from USB source
698 * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
699 * should set USB_CH_IP_CUR_LVL_0P5.
702 switch (link_status) {
703 case USB_STAT_STD_HOST_NC:
704 case USB_STAT_STD_HOST_C_NS:
705 case USB_STAT_STD_HOST_C_S:
706 dev_dbg(di->dev, "USB Type - Standard host is "
707 "detected through USB driver\n");
708 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
711 case USB_STAT_HOST_CHG_HS_CHIRP:
712 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
715 case USB_STAT_HOST_CHG_HS:
716 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
719 case USB_STAT_ACA_RID_C_HS:
720 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
723 case USB_STAT_ACA_RID_A:
725 * Dedicated charger level minus maximum current accessory
726 * can consume (900mA). Closest level is 500mA
728 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
729 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
732 case USB_STAT_ACA_RID_B:
734 * Dedicated charger level minus 120mA (20mA for ACA and
735 * 100mA for potential accessory). Closest level is 1300mA
737 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
738 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
739 di->max_usb_in_curr.usb_type_max);
742 case USB_STAT_HOST_CHG_NM:
743 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
746 case USB_STAT_DEDICATED_CHG:
747 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
750 case USB_STAT_ACA_RID_C_HS_CHIRP:
751 case USB_STAT_ACA_RID_C_NM:
752 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
755 case USB_STAT_NOT_CONFIGURED:
756 if (di->vbus_detected) {
757 di->usb_device_is_unrecognised = true;
758 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
759 di->max_usb_in_curr.usb_type_max =
760 USB_CH_IP_CUR_LVL_1P5;
763 case USB_STAT_HM_IDGND:
764 dev_err(di->dev, "USB Type - Charging not allowed\n");
765 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
768 case USB_STAT_RESERVED:
769 if (is_ab8500(di->parent)) {
770 di->flags.vbus_collapse = true;
771 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
772 "VBUS has collapsed\n");
776 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
777 di->max_usb_in_curr.usb_type_max =
778 USB_CH_IP_CUR_LVL_0P05;
779 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
781 di->max_usb_in_curr.usb_type_max);
785 case USB_STAT_CARKIT_1:
786 case USB_STAT_CARKIT_2:
787 case USB_STAT_ACA_DOCK_CHARGER:
788 case USB_STAT_CHARGER_LINE_1:
789 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
790 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
791 di->max_usb_in_curr.usb_type_max);
793 case USB_STAT_NOT_VALID_LINK:
794 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
795 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
799 dev_err(di->dev, "USB Type - Unknown\n");
800 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
805 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
806 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
807 link_status, di->max_usb_in_curr.set_max);
813 * ab8500_charger_read_usb_type() - read the type of usb connected
814 * @di: pointer to the ab8500_charger structure
816 * Detect the type of the plugged USB
817 * Returns error code in case of failure else 0 on success
819 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
824 ret = abx500_get_register_interruptible(di->dev,
825 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
827 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
830 if (is_ab8500(di->parent))
831 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
832 AB8500_USB_LINE_STAT_REG, &val);
834 ret = abx500_get_register_interruptible(di->dev,
835 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
837 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
841 /* get the USB type */
842 if (is_ab8500(di->parent))
843 val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
845 val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
846 ret = ab8500_charger_max_usb_curr(di,
847 (enum ab8500_charger_link_status) val);
853 * ab8500_charger_detect_usb_type() - get the type of usb connected
854 * @di: pointer to the ab8500_charger structure
856 * Detect the type of the plugged USB
857 * Returns error code in case of failure else 0 on success
859 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
865 * On getting the VBUS rising edge detect interrupt there
866 * is a 250ms delay after which the register UsbLineStatus
867 * is filled with valid data.
869 for (i = 0; i < 10; i++) {
871 ret = abx500_get_register_interruptible(di->dev,
872 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
874 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
877 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
881 if (is_ab8500(di->parent))
882 ret = abx500_get_register_interruptible(di->dev,
883 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
885 ret = abx500_get_register_interruptible(di->dev,
886 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
888 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
891 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
894 * Until the IT source register is read the UsbLineStatus
895 * register is not updated, hence doing the same
899 /* get the USB type */
900 if (is_ab8500(di->parent))
901 val = (val & AB8500_USB_LINK_STATUS) >>
902 USB_LINK_STATUS_SHIFT;
904 val = (val & AB8505_USB_LINK_STATUS) >>
905 USB_LINK_STATUS_SHIFT;
909 ret = ab8500_charger_max_usb_curr(di,
910 (enum ab8500_charger_link_status) val);
916 * This array maps the raw hex value to charger voltage used by the AB8500
917 * Values taken from the UM0836
919 static int ab8500_charger_voltage_map[] = {
1000 static int ab8500_voltage_to_regval(int voltage)
1004 /* Special case for voltage below 3.5V */
1005 if (voltage < ab8500_charger_voltage_map[0])
1006 return LOW_VOLT_REG;
1008 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
1009 if (voltage < ab8500_charger_voltage_map[i])
1013 /* If not last element, return error */
1014 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
1015 if (voltage == ab8500_charger_voltage_map[i])
1021 static int ab8500_current_to_regval(struct ab8500_charger *di, int curr)
1025 if (curr < di->bm->chg_output_curr[0])
1028 for (i = 0; i < di->bm->n_chg_out_curr; i++) {
1029 if (curr < di->bm->chg_output_curr[i])
1033 /* If not last element, return error */
1034 i = di->bm->n_chg_out_curr - 1;
1035 if (curr == di->bm->chg_output_curr[i])
1041 static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr)
1045 if (curr < di->bm->chg_input_curr[0])
1048 for (i = 0; i < di->bm->n_chg_in_curr; i++) {
1049 if (curr < di->bm->chg_input_curr[i])
1053 /* If not last element, return error */
1054 i = di->bm->n_chg_in_curr - 1;
1055 if (curr == di->bm->chg_input_curr[i])
1062 * ab8500_charger_get_usb_cur() - get usb current
1063 * @di: pointer to the ab8500_charger structre
1065 * The usb stack provides the maximum current that can be drawn from
1066 * the standard usb host. This will be in mA.
1067 * This function converts current in mA to a value that can be written
1068 * to the register. Returns -1 if charging is not allowed
1070 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1073 switch (di->usb_state.usb_current) {
1075 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
1078 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
1081 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
1084 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
1087 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
1090 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
1094 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
1099 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1100 * @di: pointer to the ab8500_charger structure
1101 * @reg: select what charger register to check
1103 * Check if current stepping should be allowed to continue.
1104 * Checks if charger source has not collapsed. If it has, further stepping
1107 static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1110 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1111 return !di->flags.vbus_drop_end;
1117 * ab8500_charger_set_current() - set charger current
1118 * @di: pointer to the ab8500_charger structure
1119 * @ich: charger current, in mA
1120 * @reg: select what charger register to set
1122 * Set charger current.
1123 * There is no state machine in the AB to step up/down the charger
1124 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1125 * charging is started. Instead we need to implement
1126 * this charger current step-up/down here.
1127 * Returns error code in case of failure else 0(on success)
1129 static int ab8500_charger_set_current(struct ab8500_charger *di,
1133 int curr_index, prev_curr_index, shift_value, i;
1136 bool no_stepping = false;
1138 atomic_inc(&di->current_stepping_sessions);
1140 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1143 dev_err(di->dev, "%s read failed\n", __func__);
1144 goto exit_set_current;
1148 case AB8500_MCH_IPT_CURLVL_REG:
1149 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1150 prev_curr_index = (reg_value >> shift_value);
1151 curr_index = ab8500_current_to_regval(di, ich);
1152 step_udelay = STEP_UDELAY;
1153 if (!di->ac.charger_connected)
1156 case AB8500_USBCH_IPT_CRNTLVL_REG:
1157 if (is_ab8540(di->parent))
1158 shift_value = AB8540_VBUS_IN_CURR_LIM_SHIFT;
1160 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1161 prev_curr_index = (reg_value >> shift_value);
1162 curr_index = ab8500_vbus_in_curr_to_regval(di, ich);
1163 step_udelay = STEP_UDELAY * 100;
1165 if (!di->usb.charger_connected)
1168 case AB8500_CH_OPT_CRNTLVL_REG:
1170 prev_curr_index = (reg_value >> shift_value);
1171 curr_index = ab8500_current_to_regval(di, ich);
1172 step_udelay = STEP_UDELAY;
1173 if (curr_index && (curr_index - prev_curr_index) > 1)
1176 if (!di->usb.charger_connected && !di->ac.charger_connected)
1181 dev_err(di->dev, "%s current register not valid\n", __func__);
1183 goto exit_set_current;
1186 if (curr_index < 0) {
1187 dev_err(di->dev, "requested current limit out-of-range\n");
1189 goto exit_set_current;
1192 /* only update current if it's been changed */
1193 if (prev_curr_index == curr_index) {
1194 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1197 goto exit_set_current;
1200 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1201 __func__, ich, reg);
1204 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1205 reg, (u8)curr_index << shift_value);
1207 dev_err(di->dev, "%s write failed\n", __func__);
1208 } else if (prev_curr_index > curr_index) {
1209 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1210 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1211 (u8) i << shift_value, reg);
1212 ret = abx500_set_register_interruptible(di->dev,
1213 AB8500_CHARGER, reg, (u8)i << shift_value);
1215 dev_err(di->dev, "%s write failed\n", __func__);
1216 goto exit_set_current;
1218 if (i != curr_index)
1219 usleep_range(step_udelay, step_udelay * 2);
1223 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
1224 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1225 (u8)i << shift_value, reg);
1226 ret = abx500_set_register_interruptible(di->dev,
1227 AB8500_CHARGER, reg, (u8)i << shift_value);
1229 dev_err(di->dev, "%s write failed\n", __func__);
1230 goto exit_set_current;
1232 if (i != curr_index)
1233 usleep_range(step_udelay, step_udelay * 2);
1235 allow = ab8500_charger_check_continue_stepping(di, reg);
1240 atomic_dec(&di->current_stepping_sessions);
1246 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1247 * @di: pointer to the ab8500_charger structure
1248 * @ich_in: charger input current limit
1250 * Sets the current that can be drawn from the USB host
1251 * Returns error code in case of failure else 0(on success)
1253 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1259 /* We should always use to lowest current limit */
1260 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1261 if (di->max_usb_in_curr.set_max > 0)
1262 min_value = min(di->max_usb_in_curr.set_max, min_value);
1264 if (di->usb_state.usb_current >= 0)
1265 min_value = min(di->usb_state.usb_current, min_value);
1267 switch (min_value) {
1269 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1270 min_value = USB_CH_IP_CUR_LVL_0P05;
1273 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1274 min_value = USB_CH_IP_CUR_LVL_0P45;
1280 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1282 mutex_lock(&di->usb_ipt_crnt_lock);
1283 ret = ab8500_charger_set_current(di, min_value,
1284 AB8500_USBCH_IPT_CRNTLVL_REG);
1285 mutex_unlock(&di->usb_ipt_crnt_lock);
1291 * ab8500_charger_set_main_in_curr() - set main charger input current
1292 * @di: pointer to the ab8500_charger structure
1293 * @ich_in: input charger current, in mA
1295 * Set main charger input current.
1296 * Returns error code in case of failure else 0(on success)
1298 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1301 return ab8500_charger_set_current(di, ich_in,
1302 AB8500_MCH_IPT_CURLVL_REG);
1306 * ab8500_charger_set_output_curr() - set charger output current
1307 * @di: pointer to the ab8500_charger structure
1308 * @ich_out: output charger current, in mA
1310 * Set charger output current.
1311 * Returns error code in case of failure else 0(on success)
1313 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1316 return ab8500_charger_set_current(di, ich_out,
1317 AB8500_CH_OPT_CRNTLVL_REG);
1321 * ab8500_charger_led_en() - turn on/off chargign led
1322 * @di: pointer to the ab8500_charger structure
1323 * @on: flag to turn on/off the chargign led
1325 * Power ON/OFF charging LED indication
1326 * Returns error code in case of failure else 0(on success)
1328 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1333 /* Power ON charging LED indicator, set LED current to 5mA */
1334 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1335 AB8500_LED_INDICATOR_PWM_CTRL,
1336 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1338 dev_err(di->dev, "Power ON LED failed\n");
1341 /* LED indicator PWM duty cycle 252/256 */
1342 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1343 AB8500_LED_INDICATOR_PWM_DUTY,
1344 LED_INDICATOR_PWM_DUTY_252_256);
1346 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1350 /* Power off charging LED indicator */
1351 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1352 AB8500_LED_INDICATOR_PWM_CTRL,
1353 LED_INDICATOR_PWM_DIS);
1355 dev_err(di->dev, "Power-off LED failed\n");
1364 * ab8500_charger_ac_en() - enable or disable ac charging
1365 * @di: pointer to the ab8500_charger structure
1366 * @enable: enable/disable flag
1367 * @vset: charging voltage
1368 * @iset: charging current
1370 * Enable/Disable AC/Mains charging and turns on/off the charging led
1373 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1374 int enable, int vset, int iset)
1379 int input_curr_index;
1382 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1385 /* Check if AC is connected */
1386 if (!di->ac.charger_connected) {
1387 dev_err(di->dev, "AC charger not connected\n");
1391 /* Enable AC charging */
1392 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1395 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1396 * will be triggered everytime we enable the VDD ADC supply.
1397 * This will turn off charging for a short while.
1398 * It can be avoided by having the supply on when
1399 * there is a charger enabled. Normally the VDD ADC supply
1400 * is enabled everytime a GPADC conversion is triggered. We will
1401 * force it to be enabled from this driver to have
1402 * the GPADC module independant of the AB8500 chargers
1404 if (!di->vddadc_en_ac) {
1405 ret = regulator_enable(di->regu);
1408 "Failed to enable regulator\n");
1410 di->vddadc_en_ac = true;
1413 /* Check if the requested voltage or current is valid */
1414 volt_index = ab8500_voltage_to_regval(vset);
1415 curr_index = ab8500_current_to_regval(di, iset);
1416 input_curr_index = ab8500_current_to_regval(di,
1417 di->bm->chg_params->ac_curr_max);
1418 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1420 "Charger voltage or current too high, "
1421 "charging not started\n");
1425 /* ChVoltLevel: maximum battery charging voltage */
1426 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1427 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1429 dev_err(di->dev, "%s write failed\n", __func__);
1432 /* MainChInputCurr: current that can be drawn from the charger*/
1433 ret = ab8500_charger_set_main_in_curr(di,
1434 di->bm->chg_params->ac_curr_max);
1436 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1440 /* ChOutputCurentLevel: protected output current */
1441 ret = ab8500_charger_set_output_curr(di, iset);
1443 dev_err(di->dev, "%s "
1444 "Failed to set ChOutputCurentLevel\n",
1449 /* Check if VBAT overshoot control should be enabled */
1450 if (!di->bm->enable_overshoot)
1451 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1453 /* Enable Main Charger */
1454 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1455 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1457 dev_err(di->dev, "%s write failed\n", __func__);
1461 /* Power on charging LED indication */
1462 ret = ab8500_charger_led_en(di, true);
1464 dev_err(di->dev, "failed to enable LED\n");
1466 di->ac.charger_online = 1;
1468 /* Disable AC charging */
1469 if (is_ab8500_1p1_or_earlier(di->parent)) {
1471 * For ABB revision 1.0 and 1.1 there is a bug in the
1472 * watchdog logic. That means we have to continously
1473 * kick the charger watchdog even when no charger is
1474 * connected. This is only valid once the AC charger
1475 * has been enabled. This is a bug that is not handled
1476 * by the algorithm and the watchdog have to be kicked
1477 * by the charger driver when the AC charger
1481 queue_delayed_work(di->charger_wq,
1483 round_jiffies(WD_KICK_INTERVAL));
1487 * We can't turn off charging completely
1488 * due to a bug in AB8500 cut1.
1489 * If we do, charging will not start again.
1490 * That is why we set the lowest voltage
1491 * and current possible
1493 ret = abx500_set_register_interruptible(di->dev,
1495 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1498 "%s write failed\n", __func__);
1502 ret = ab8500_charger_set_output_curr(di, 0);
1504 dev_err(di->dev, "%s "
1505 "Failed to set ChOutputCurentLevel\n",
1510 ret = abx500_set_register_interruptible(di->dev,
1512 AB8500_MCH_CTRL1, 0);
1515 "%s write failed\n", __func__);
1520 ret = ab8500_charger_led_en(di, false);
1522 dev_err(di->dev, "failed to disable LED\n");
1524 di->ac.charger_online = 0;
1525 di->ac.wd_expired = false;
1527 /* Disable regulator if enabled */
1528 if (di->vddadc_en_ac) {
1529 regulator_disable(di->regu);
1530 di->vddadc_en_ac = false;
1533 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1535 ab8500_power_supply_changed(di, di->ac_chg.psy);
1541 * ab8500_charger_usb_en() - enable usb charging
1542 * @di: pointer to the ab8500_charger structure
1543 * @enable: enable/disable flag
1544 * @vset: charging voltage
1545 * @ich_out: charger output current
1547 * Enable/Disable USB charging and turns on/off the charging led respectively.
1548 * Returns error code in case of failure else 0(on success)
1550 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1551 int enable, int vset, int ich_out)
1558 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1561 /* Check if USB is connected */
1562 if (!di->usb.charger_connected) {
1563 dev_err(di->dev, "USB charger not connected\n");
1568 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1569 * will be triggered everytime we enable the VDD ADC supply.
1570 * This will turn off charging for a short while.
1571 * It can be avoided by having the supply on when
1572 * there is a charger enabled. Normally the VDD ADC supply
1573 * is enabled everytime a GPADC conversion is triggered. We will
1574 * force it to be enabled from this driver to have
1575 * the GPADC module independant of the AB8500 chargers
1577 if (!di->vddadc_en_usb) {
1578 ret = regulator_enable(di->regu);
1581 "Failed to enable regulator\n");
1583 di->vddadc_en_usb = true;
1586 /* Enable USB charging */
1587 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1589 /* Check if the requested voltage or current is valid */
1590 volt_index = ab8500_voltage_to_regval(vset);
1591 curr_index = ab8500_current_to_regval(di, ich_out);
1592 if (volt_index < 0 || curr_index < 0) {
1594 "Charger voltage or current too high, "
1595 "charging not started\n");
1599 /* ChVoltLevel: max voltage upto which battery can be charged */
1600 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1601 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1603 dev_err(di->dev, "%s write failed\n", __func__);
1606 /* Check if VBAT overshoot control should be enabled */
1607 if (!di->bm->enable_overshoot)
1608 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1610 /* Enable USB Charger */
1612 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
1613 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1614 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1616 dev_err(di->dev, "%s write failed\n", __func__);
1620 /* If success power on charging LED indication */
1621 ret = ab8500_charger_led_en(di, true);
1623 dev_err(di->dev, "failed to enable LED\n");
1625 di->usb.charger_online = 1;
1627 /* USBChInputCurr: current that can be drawn from the usb */
1628 ret = ab8500_charger_set_vbus_in_curr(di,
1629 di->max_usb_in_curr.usb_type_max);
1631 dev_err(di->dev, "setting USBChInputCurr failed\n");
1635 /* ChOutputCurentLevel: protected output current */
1636 ret = ab8500_charger_set_output_curr(di, ich_out);
1638 dev_err(di->dev, "%s "
1639 "Failed to set ChOutputCurentLevel\n",
1644 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1647 /* Disable USB charging */
1648 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1649 ret = abx500_set_register_interruptible(di->dev,
1651 AB8500_USBCH_CTRL1_REG, 0);
1654 "%s write failed\n", __func__);
1658 ret = ab8500_charger_led_en(di, false);
1660 dev_err(di->dev, "failed to disable LED\n");
1661 /* USBChInputCurr: current that can be drawn from the usb */
1662 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1664 dev_err(di->dev, "setting USBChInputCurr failed\n");
1668 /* ChOutputCurentLevel: protected output current */
1669 ret = ab8500_charger_set_output_curr(di, 0);
1671 dev_err(di->dev, "%s "
1672 "Failed to reset ChOutputCurentLevel\n",
1676 di->usb.charger_online = 0;
1677 di->usb.wd_expired = false;
1679 /* Disable regulator if enabled */
1680 if (di->vddadc_en_usb) {
1681 regulator_disable(di->regu);
1682 di->vddadc_en_usb = false;
1685 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1687 /* Cancel any pending Vbat check work */
1688 cancel_delayed_work(&di->check_vbat_work);
1691 ab8500_power_supply_changed(di, di->usb_chg.psy);
1696 static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1697 unsigned long event, void *data)
1700 struct device *dev = data;
1701 /*Toggle External charger control pin*/
1702 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1703 AB8500_SYS_CHARGER_CONTROL_REG,
1704 EXTERNAL_CHARGER_DISABLE_REG_VAL);
1706 dev_err(dev, "write reg failed %d\n", ret);
1709 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1710 AB8500_SYS_CHARGER_CONTROL_REG,
1711 EXTERNAL_CHARGER_ENABLE_REG_VAL);
1713 dev_err(dev, "Write reg failed %d\n", ret);
1720 * ab8500_charger_usb_check_enable() - enable usb charging
1721 * @charger: pointer to the ux500_charger structure
1722 * @vset: charging voltage
1723 * @iset: charger output current
1725 * Check if the VBUS charger has been disconnected and reconnected without
1726 * AB8500 rising an interrupt. Returns 0 on success.
1728 static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1734 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1736 if (!di->usb.charger_connected)
1739 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1740 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1742 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1745 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1747 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1748 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1750 ret = abx500_mask_and_set_register_interruptible(di->dev,
1751 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1752 DROP_COUNT_RESET, DROP_COUNT_RESET);
1754 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1758 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1760 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1769 * ab8500_charger_ac_check_enable() - enable usb charging
1770 * @charger: pointer to the ux500_charger structure
1771 * @vset: charging voltage
1772 * @iset: charger output current
1774 * Check if the AC charger has been disconnected and reconnected without
1775 * AB8500 rising an interrupt. Returns 0 on success.
1777 static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1780 u8 mainch_ctrl1 = 0;
1783 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1785 if (!di->ac.charger_connected)
1788 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1789 AB8500_MCH_CTRL1, &mainch_ctrl1);
1791 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1794 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1796 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1797 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1799 ret = abx500_mask_and_set_register_interruptible(di->dev,
1800 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1801 DROP_COUNT_RESET, DROP_COUNT_RESET);
1804 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1808 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1810 dev_err(di->dev, "failed to enable AC charger %d\n",
1819 * ab8500_charger_watchdog_kick() - kick charger watchdog
1820 * @di: pointer to the ab8500_charger structure
1822 * Kick charger watchdog
1823 * Returns error code in case of failure else 0(on success)
1825 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1828 struct ab8500_charger *di;
1830 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1831 di = to_ab8500_charger_ac_device_info(charger);
1832 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1833 di = to_ab8500_charger_usb_device_info(charger);
1837 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1838 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1840 dev_err(di->dev, "Failed to kick WD!\n");
1846 * ab8500_charger_update_charger_current() - update charger current
1847 * @di: pointer to the ab8500_charger structure
1849 * Update the charger output current for the specified charger
1850 * Returns error code in case of failure else 0(on success)
1852 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1856 struct ab8500_charger *di;
1858 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1859 di = to_ab8500_charger_ac_device_info(charger);
1860 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1861 di = to_ab8500_charger_usb_device_info(charger);
1865 ret = ab8500_charger_set_output_curr(di, ich_out);
1867 dev_err(di->dev, "%s "
1868 "Failed to set ChOutputCurentLevel\n",
1873 /* Reset the main and usb drop input current measurement counter */
1874 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1875 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
1877 dev_err(di->dev, "%s write failed\n", __func__);
1885 * ab8540_charger_power_path_enable() - enable usb power path mode
1886 * @charger: pointer to the ux500_charger structure
1887 * @enable: enable/disable flag
1889 * Enable or disable the power path for usb mode
1890 * Returns error code in case of failure else 0(on success)
1892 static int ab8540_charger_power_path_enable(struct ux500_charger *charger,
1896 struct ab8500_charger *di;
1898 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1899 di = to_ab8500_charger_usb_device_info(charger);
1903 ret = abx500_mask_and_set_register_interruptible(di->dev,
1904 AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
1905 BUS_POWER_PATH_MODE_ENA, enable);
1907 dev_err(di->dev, "%s write failed\n", __func__);
1916 * ab8540_charger_usb_pre_chg_enable() - enable usb pre change
1917 * @charger: pointer to the ux500_charger structure
1918 * @enable: enable/disable flag
1920 * Enable or disable the pre-chage for usb mode
1921 * Returns error code in case of failure else 0(on success)
1923 static int ab8540_charger_usb_pre_chg_enable(struct ux500_charger *charger,
1927 struct ab8500_charger *di;
1929 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1930 di = to_ab8500_charger_usb_device_info(charger);
1934 ret = abx500_mask_and_set_register_interruptible(di->dev,
1935 AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
1936 BUS_POWER_PATH_PRECHG_ENA, enable);
1938 dev_err(di->dev, "%s write failed\n", __func__);
1945 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1947 struct power_supply *psy;
1948 struct power_supply *ext = dev_get_drvdata(dev);
1949 const char **supplicants = (const char **)ext->supplied_to;
1950 struct ab8500_charger *di;
1951 union power_supply_propval ret;
1953 struct ux500_charger *usb_chg;
1955 usb_chg = (struct ux500_charger *)data;
1958 di = to_ab8500_charger_usb_device_info(usb_chg);
1960 /* For all psy where the driver name appears in any supplied_to */
1961 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
1965 /* Go through all properties for the psy */
1966 for (j = 0; j < ext->desc->num_properties; j++) {
1967 enum power_supply_property prop;
1968 prop = ext->desc->properties[j];
1970 if (power_supply_get_property(ext, prop, &ret))
1974 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1975 switch (ext->desc->type) {
1976 case POWER_SUPPLY_TYPE_BATTERY:
1977 di->vbat = ret.intval / 1000;
1991 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1992 * @work pointer to the work_struct structure
1994 * Due to a asic bug it is necessary to lower the input current to the vbus
1995 * charger when charging with at some specific levels. This issue is only valid
1996 * for below a certain battery voltage. This function makes sure that the
1997 * the allowed current limit isn't exceeded.
1999 static void ab8500_charger_check_vbat_work(struct work_struct *work)
2002 struct ab8500_charger *di = container_of(work,
2003 struct ab8500_charger, check_vbat_work.work);
2005 class_for_each_device(power_supply_class, NULL,
2006 di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
2008 /* First run old_vbat is 0. */
2009 if (di->old_vbat == 0)
2010 di->old_vbat = di->vbat;
2012 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
2013 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
2014 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
2015 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
2017 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
2018 " old: %d\n", di->max_usb_in_curr.usb_type_max,
2019 di->vbat, di->old_vbat);
2020 ab8500_charger_set_vbus_in_curr(di,
2021 di->max_usb_in_curr.usb_type_max);
2022 power_supply_changed(di->usb_chg.psy);
2025 di->old_vbat = di->vbat;
2028 * No need to check the battery voltage every second when not close to
2031 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
2032 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
2035 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
2039 * ab8500_charger_check_hw_failure_work() - check main charger failure
2040 * @work: pointer to the work_struct structure
2042 * Work queue function for checking the main charger status
2044 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
2049 struct ab8500_charger *di = container_of(work,
2050 struct ab8500_charger, check_hw_failure_work.work);
2052 /* Check if the status bits for HW failure is still active */
2053 if (di->flags.mainextchnotok) {
2054 ret = abx500_get_register_interruptible(di->dev,
2055 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2057 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2060 if (!(reg_value & MAIN_CH_NOK)) {
2061 di->flags.mainextchnotok = false;
2062 ab8500_power_supply_changed(di, di->ac_chg.psy);
2065 if (di->flags.vbus_ovv) {
2066 ret = abx500_get_register_interruptible(di->dev,
2067 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
2070 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2073 if (!(reg_value & VBUS_OVV_TH)) {
2074 di->flags.vbus_ovv = false;
2075 ab8500_power_supply_changed(di, di->usb_chg.psy);
2078 /* If we still have a failure, schedule a new check */
2079 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2080 queue_delayed_work(di->charger_wq,
2081 &di->check_hw_failure_work, round_jiffies(HZ));
2086 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2087 * @work: pointer to the work_struct structure
2089 * Work queue function for kicking the charger watchdog.
2091 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2092 * logic. That means we have to continously kick the charger
2093 * watchdog even when no charger is connected. This is only
2094 * valid once the AC charger has been enabled. This is
2095 * a bug that is not handled by the algorithm and the
2096 * watchdog have to be kicked by the charger driver
2097 * when the AC charger is disabled
2099 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2103 struct ab8500_charger *di = container_of(work,
2104 struct ab8500_charger, kick_wd_work.work);
2106 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2107 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2109 dev_err(di->dev, "Failed to kick WD!\n");
2111 /* Schedule a new watchdog kick */
2112 queue_delayed_work(di->charger_wq,
2113 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2117 * ab8500_charger_ac_work() - work to get and set main charger status
2118 * @work: pointer to the work_struct structure
2120 * Work queue function for checking the main charger status
2122 static void ab8500_charger_ac_work(struct work_struct *work)
2126 struct ab8500_charger *di = container_of(work,
2127 struct ab8500_charger, ac_work);
2130 * Since we can't be sure that the events are received
2131 * synchronously, we have the check if the main charger is
2132 * connected by reading the status register
2134 ret = ab8500_charger_detect_chargers(di, false);
2138 if (ret & AC_PW_CONN) {
2139 di->ac.charger_connected = 1;
2142 di->ac.charger_connected = 0;
2145 ab8500_power_supply_changed(di, di->ac_chg.psy);
2146 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
2149 static void ab8500_charger_usb_attached_work(struct work_struct *work)
2151 struct ab8500_charger *di = container_of(work,
2152 struct ab8500_charger,
2153 usb_charger_attached_work.work);
2154 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2158 for (i = 0; i < 10; i++) {
2159 ret = abx500_get_register_interruptible(di->dev,
2161 AB8500_CH_USBCH_STAT1_REG,
2164 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2167 if ((statval & usbch) != usbch)
2170 msleep(CHARGER_STATUS_POLL);
2173 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2175 mutex_lock(&di->charger_attached_mutex);
2176 mutex_unlock(&di->charger_attached_mutex);
2181 queue_delayed_work(di->charger_wq,
2182 &di->usb_charger_attached_work,
2186 static void ab8500_charger_ac_attached_work(struct work_struct *work)
2189 struct ab8500_charger *di = container_of(work,
2190 struct ab8500_charger,
2191 ac_charger_attached_work.work);
2192 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2193 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2197 for (i = 0; i < 10; i++) {
2198 ret = abx500_get_register_interruptible(di->dev,
2200 AB8500_CH_STATUS2_REG,
2203 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2207 if ((statval & mainch) != mainch)
2210 msleep(CHARGER_STATUS_POLL);
2213 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2214 queue_work(di->charger_wq, &di->ac_work);
2216 mutex_lock(&di->charger_attached_mutex);
2217 mutex_unlock(&di->charger_attached_mutex);
2222 queue_delayed_work(di->charger_wq,
2223 &di->ac_charger_attached_work,
2228 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2229 * @work: Pointer to the work_struct structure
2231 * Detect the type of USB plugged
2233 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
2237 struct ab8500_charger *di = container_of(work,
2238 struct ab8500_charger, detect_usb_type_work);
2241 * Since we can't be sure that the events are received
2242 * synchronously, we have the check if is
2243 * connected by reading the status register
2245 ret = ab8500_charger_detect_chargers(di, false);
2249 if (!(ret & USB_PW_CONN)) {
2250 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2251 di->vbus_detected = false;
2252 ab8500_charger_set_usb_connected(di, false);
2253 ab8500_power_supply_changed(di, di->usb_chg.psy);
2255 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2256 di->vbus_detected = true;
2258 if (is_ab8500_1p1_or_earlier(di->parent)) {
2259 ret = ab8500_charger_detect_usb_type(di);
2261 ab8500_charger_set_usb_connected(di, true);
2262 ab8500_power_supply_changed(di,
2267 * For ABB cut2.0 and onwards we have an IRQ,
2268 * USB_LINK_STATUS that will be triggered when the USB
2269 * link status changes. The exception is USB connected
2270 * during startup. Then we don't get a
2271 * USB_LINK_STATUS IRQ
2273 if (di->vbus_detected_start) {
2274 di->vbus_detected_start = false;
2275 ret = ab8500_charger_detect_usb_type(di);
2277 ab8500_charger_set_usb_connected(di,
2279 ab8500_power_supply_changed(di,
2288 * ab8500_charger_usb_link_attach_work() - work to detect USB type
2289 * @work: pointer to the work_struct structure
2291 * Detect the type of USB plugged
2293 static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2295 struct ab8500_charger *di =
2296 container_of(work, struct ab8500_charger, attach_work.work);
2299 /* Update maximum input current if USB enumeration is not detected */
2300 if (!di->usb.charger_online) {
2301 ret = ab8500_charger_set_vbus_in_curr(di,
2302 di->max_usb_in_curr.usb_type_max);
2307 ab8500_charger_set_usb_connected(di, true);
2308 ab8500_power_supply_changed(di, di->usb_chg.psy);
2312 * ab8500_charger_usb_link_status_work() - work to detect USB type
2313 * @work: pointer to the work_struct structure
2315 * Detect the type of USB plugged
2317 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2319 int detected_chargers;
2324 struct ab8500_charger *di = container_of(work,
2325 struct ab8500_charger, usb_link_status_work);
2328 * Since we can't be sure that the events are received
2329 * synchronously, we have the check if is
2330 * connected by reading the status register
2332 detected_chargers = ab8500_charger_detect_chargers(di, false);
2333 if (detected_chargers < 0)
2337 * Some chargers that breaks the USB spec is
2338 * identified as invalid by AB8500 and it refuse
2339 * to start the charging process. but by jumping
2340 * thru a few hoops it can be forced to start.
2342 if (is_ab8500(di->parent))
2343 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2344 AB8500_USB_LINE_STAT_REG, &val);
2346 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2347 AB8500_USB_LINK1_STAT_REG, &val);
2350 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2352 dev_dbg(di->dev, "Error reading USB link status\n");
2354 if (is_ab8500(di->parent))
2355 link_status = AB8500_USB_LINK_STATUS;
2357 link_status = AB8505_USB_LINK_STATUS;
2359 if (detected_chargers & USB_PW_CONN) {
2360 if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
2361 USB_STAT_NOT_VALID_LINK &&
2362 di->invalid_charger_detect_state == 0) {
2364 "Invalid charger detected, state= 0\n");
2366 abx500_mask_and_set_register_interruptible(di->dev,
2367 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
2368 USB_CH_ENA, USB_CH_ENA);
2369 /*Enable charger detection*/
2370 abx500_mask_and_set_register_interruptible(di->dev,
2371 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2372 USB_CH_DET, USB_CH_DET);
2373 di->invalid_charger_detect_state = 1;
2374 /*exit and wait for new link status interrupt.*/
2378 if (di->invalid_charger_detect_state == 1) {
2380 "Invalid charger detected, state= 1\n");
2381 /*Stop charger detection*/
2382 abx500_mask_and_set_register_interruptible(di->dev,
2383 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2385 /*Check link status*/
2386 if (is_ab8500(di->parent))
2387 ret = abx500_get_register_interruptible(di->dev,
2388 AB8500_USB, AB8500_USB_LINE_STAT_REG,
2391 ret = abx500_get_register_interruptible(di->dev,
2392 AB8500_USB, AB8500_USB_LINK1_STAT_REG,
2395 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2396 (val & link_status) >> USB_LINK_STATUS_SHIFT);
2397 di->invalid_charger_detect_state = 2;
2400 di->invalid_charger_detect_state = 0;
2403 if (!(detected_chargers & USB_PW_CONN)) {
2404 di->vbus_detected = false;
2405 ab8500_charger_set_usb_connected(di, false);
2406 ab8500_power_supply_changed(di, di->usb_chg.psy);
2410 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2411 di->vbus_detected = true;
2412 ret = ab8500_charger_read_usb_type(di);
2414 if (ret == -ENXIO) {
2415 /* No valid charger type detected */
2416 ab8500_charger_set_usb_connected(di, false);
2417 ab8500_power_supply_changed(di, di->usb_chg.psy);
2422 if (di->usb_device_is_unrecognised) {
2424 "Potential Legacy Charger device. "
2425 "Delay work for %d msec for USB enum "
2427 WAIT_ACA_RID_ENUMERATION);
2428 queue_delayed_work(di->charger_wq,
2430 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2431 } else if (di->is_aca_rid == 1) {
2432 /* Only wait once */
2435 "%s Wait %d msec for USB enum to finish",
2436 __func__, WAIT_ACA_RID_ENUMERATION);
2437 queue_delayed_work(di->charger_wq,
2439 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2441 queue_delayed_work(di->charger_wq,
2447 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2450 unsigned long flags;
2452 struct ab8500_charger *di = container_of(work,
2453 struct ab8500_charger, usb_state_changed_work.work);
2455 if (!di->vbus_detected) {
2457 "%s !di->vbus_detected\n",
2462 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2463 di->usb_state.state = di->usb_state.state_tmp;
2464 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
2465 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2467 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2468 __func__, di->usb_state.state, di->usb_state.usb_current);
2470 switch (di->usb_state.state) {
2471 case AB8500_BM_USB_STATE_RESET_HS:
2472 case AB8500_BM_USB_STATE_RESET_FS:
2473 case AB8500_BM_USB_STATE_SUSPEND:
2474 case AB8500_BM_USB_STATE_MAX:
2475 ab8500_charger_set_usb_connected(di, false);
2476 ab8500_power_supply_changed(di, di->usb_chg.psy);
2479 case AB8500_BM_USB_STATE_RESUME:
2481 * when suspend->resume there should be delay
2482 * of 1sec for enabling charging
2485 /* Intentional fall through */
2486 case AB8500_BM_USB_STATE_CONFIGURED:
2488 * USB is configured, enable charging with the charging
2489 * input current obtained from USB driver
2491 if (!ab8500_charger_get_usb_cur(di)) {
2492 /* Update maximum input current */
2493 ret = ab8500_charger_set_vbus_in_curr(di,
2494 di->max_usb_in_curr.usb_type_max);
2498 ab8500_charger_set_usb_connected(di, true);
2499 ab8500_power_supply_changed(di, di->usb_chg.psy);
2509 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2510 * @work: pointer to the work_struct structure
2512 * Work queue function for checking the USB charger Not OK status
2514 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2520 struct ab8500_charger *di = container_of(work,
2521 struct ab8500_charger, check_usbchgnotok_work.work);
2523 /* Check if the status bit for usbchargernotok is still active */
2524 ret = abx500_get_register_interruptible(di->dev,
2525 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2527 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2530 prev_status = di->flags.usbchargernotok;
2532 if (reg_value & VBUS_CH_NOK) {
2533 di->flags.usbchargernotok = true;
2534 /* Check again in 1sec */
2535 queue_delayed_work(di->charger_wq,
2536 &di->check_usbchgnotok_work, HZ);
2538 di->flags.usbchargernotok = false;
2539 di->flags.vbus_collapse = false;
2542 if (prev_status != di->flags.usbchargernotok)
2543 ab8500_power_supply_changed(di, di->usb_chg.psy);
2547 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2548 * @work: pointer to the work_struct structure
2550 * Work queue function for checking the Main thermal prot status
2552 static void ab8500_charger_check_main_thermal_prot_work(
2553 struct work_struct *work)
2558 struct ab8500_charger *di = container_of(work,
2559 struct ab8500_charger, check_main_thermal_prot_work);
2561 /* Check if the status bit for main_thermal_prot is still active */
2562 ret = abx500_get_register_interruptible(di->dev,
2563 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2565 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2568 if (reg_value & MAIN_CH_TH_PROT)
2569 di->flags.main_thermal_prot = true;
2571 di->flags.main_thermal_prot = false;
2573 ab8500_power_supply_changed(di, di->ac_chg.psy);
2577 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2578 * @work: pointer to the work_struct structure
2580 * Work queue function for checking the USB thermal prot status
2582 static void ab8500_charger_check_usb_thermal_prot_work(
2583 struct work_struct *work)
2588 struct ab8500_charger *di = container_of(work,
2589 struct ab8500_charger, check_usb_thermal_prot_work);
2591 /* Check if the status bit for usb_thermal_prot is still active */
2592 ret = abx500_get_register_interruptible(di->dev,
2593 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2595 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2598 if (reg_value & USB_CH_TH_PROT)
2599 di->flags.usb_thermal_prot = true;
2601 di->flags.usb_thermal_prot = false;
2603 ab8500_power_supply_changed(di, di->usb_chg.psy);
2607 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2608 * @irq: interrupt number
2609 * @_di: pointer to the ab8500_charger structure
2611 * Returns IRQ status(IRQ_HANDLED)
2613 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2615 struct ab8500_charger *di = _di;
2617 dev_dbg(di->dev, "Main charger unplugged\n");
2618 queue_work(di->charger_wq, &di->ac_work);
2620 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2621 mutex_lock(&di->charger_attached_mutex);
2622 mutex_unlock(&di->charger_attached_mutex);
2628 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2629 * @irq: interrupt number
2630 * @_di: pointer to the ab8500_charger structure
2632 * Returns IRQ status(IRQ_HANDLED)
2634 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2636 struct ab8500_charger *di = _di;
2638 dev_dbg(di->dev, "Main charger plugged\n");
2639 queue_work(di->charger_wq, &di->ac_work);
2641 mutex_lock(&di->charger_attached_mutex);
2642 mutex_unlock(&di->charger_attached_mutex);
2644 if (is_ab8500(di->parent))
2645 queue_delayed_work(di->charger_wq,
2646 &di->ac_charger_attached_work,
2652 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2653 * @irq: interrupt number
2654 * @_di: pointer to the ab8500_charger structure
2656 * Returns IRQ status(IRQ_HANDLED)
2658 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2660 struct ab8500_charger *di = _di;
2662 dev_dbg(di->dev, "Main charger not ok\n");
2663 di->flags.mainextchnotok = true;
2664 ab8500_power_supply_changed(di, di->ac_chg.psy);
2666 /* Schedule a new HW failure check */
2667 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2673 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2674 * thermal protection threshold
2675 * @irq: interrupt number
2676 * @_di: pointer to the ab8500_charger structure
2678 * Returns IRQ status(IRQ_HANDLED)
2680 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2682 struct ab8500_charger *di = _di;
2685 "Die temp above Main charger thermal protection threshold\n");
2686 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2692 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2693 * thermal protection threshold
2694 * @irq: interrupt number
2695 * @_di: pointer to the ab8500_charger structure
2697 * Returns IRQ status(IRQ_HANDLED)
2699 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2701 struct ab8500_charger *di = _di;
2704 "Die temp ok for Main charger thermal protection threshold\n");
2705 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2710 static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2712 struct ab8500_charger *di = container_of(work,
2713 struct ab8500_charger, vbus_drop_end_work.work);
2717 di->flags.vbus_drop_end = false;
2719 /* Reset the drop counter */
2720 abx500_set_register_interruptible(di->dev,
2721 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
2723 if (is_ab8540(di->parent))
2724 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2725 AB8540_CH_USBCH_STAT3_REG, ®_value);
2727 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2728 AB8500_CH_USBCH_STAT2_REG, ®_value);
2730 dev_err(di->dev, "%s read failed\n", __func__);
2734 if (is_ab8540(di->parent))
2735 curr = di->bm->chg_input_curr[
2736 reg_value & AB8540_AUTO_VBUS_IN_CURR_MASK];
2738 curr = di->bm->chg_input_curr[
2739 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
2741 if (di->max_usb_in_curr.calculated_max != curr) {
2742 /* USB source is collapsing */
2743 di->max_usb_in_curr.calculated_max = curr;
2745 "VBUS input current limiting to %d mA\n",
2746 di->max_usb_in_curr.calculated_max);
2749 * USB source can not give more than this amount.
2750 * Taking more will collapse the source.
2752 di->max_usb_in_curr.set_max =
2753 di->max_usb_in_curr.calculated_max;
2755 "VBUS input current limited to %d mA\n",
2756 di->max_usb_in_curr.set_max);
2759 if (di->usb.charger_connected)
2760 ab8500_charger_set_vbus_in_curr(di,
2761 di->max_usb_in_curr.usb_type_max);
2765 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2766 * @irq: interrupt number
2767 * @_di: pointer to the ab8500_charger structure
2769 * Returns IRQ status(IRQ_HANDLED)
2771 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2773 struct ab8500_charger *di = _di;
2775 di->vbus_detected = false;
2776 dev_dbg(di->dev, "VBUS falling detected\n");
2777 queue_work(di->charger_wq, &di->detect_usb_type_work);
2783 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2784 * @irq: interrupt number
2785 * @_di: pointer to the ab8500_charger structure
2787 * Returns IRQ status(IRQ_HANDLED)
2789 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2791 struct ab8500_charger *di = _di;
2793 di->vbus_detected = true;
2794 dev_dbg(di->dev, "VBUS rising detected\n");
2796 queue_work(di->charger_wq, &di->detect_usb_type_work);
2802 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2803 * @irq: interrupt number
2804 * @_di: pointer to the ab8500_charger structure
2806 * Returns IRQ status(IRQ_HANDLED)
2808 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2810 struct ab8500_charger *di = _di;
2812 dev_dbg(di->dev, "USB link status changed\n");
2814 queue_work(di->charger_wq, &di->usb_link_status_work);
2820 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2821 * thermal protection threshold
2822 * @irq: interrupt number
2823 * @_di: pointer to the ab8500_charger structure
2825 * Returns IRQ status(IRQ_HANDLED)
2827 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2829 struct ab8500_charger *di = _di;
2832 "Die temp above USB charger thermal protection threshold\n");
2833 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2839 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2840 * thermal protection threshold
2841 * @irq: interrupt number
2842 * @_di: pointer to the ab8500_charger structure
2844 * Returns IRQ status(IRQ_HANDLED)
2846 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2848 struct ab8500_charger *di = _di;
2851 "Die temp ok for USB charger thermal protection threshold\n");
2852 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2858 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2859 * @irq: interrupt number
2860 * @_di: pointer to the ab8500_charger structure
2862 * Returns IRQ status(IRQ_HANDLED)
2864 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2866 struct ab8500_charger *di = _di;
2868 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2869 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2875 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2876 * @irq: interrupt number
2877 * @_di: pointer to the ab8500_charger structure
2879 * Returns IRQ status(IRQ_HANDLED)
2881 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2883 struct ab8500_charger *di = _di;
2885 dev_dbg(di->dev, "Charger watchdog expired\n");
2888 * The charger that was online when the watchdog expired
2889 * needs to be restarted for charging to start again
2891 if (di->ac.charger_online) {
2892 di->ac.wd_expired = true;
2893 ab8500_power_supply_changed(di, di->ac_chg.psy);
2895 if (di->usb.charger_online) {
2896 di->usb.wd_expired = true;
2897 ab8500_power_supply_changed(di, di->usb_chg.psy);
2904 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2905 * @irq: interrupt number
2906 * @_di: pointer to the ab8500_charger structure
2908 * Returns IRQ status(IRQ_HANDLED)
2910 static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2912 struct ab8500_charger *di = _di;
2914 dev_dbg(di->dev, "VBUS charger drop ended\n");
2915 di->flags.vbus_drop_end = true;
2918 * VBUS might have dropped due to bad connection.
2919 * Schedule a new input limit set to the value SW requests.
2921 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
2922 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
2928 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2929 * @irq: interrupt number
2930 * @_di: pointer to the ab8500_charger structure
2932 * Returns IRQ status(IRQ_HANDLED)
2934 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2936 struct ab8500_charger *di = _di;
2938 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2939 di->flags.vbus_ovv = true;
2940 ab8500_power_supply_changed(di, di->usb_chg.psy);
2942 /* Schedule a new HW failure check */
2943 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2949 * ab8500_charger_ac_get_property() - get the ac/mains properties
2950 * @psy: pointer to the power_supply structure
2951 * @psp: pointer to the power_supply_property structure
2952 * @val: pointer to the power_supply_propval union
2954 * This function gets called when an application tries to get the ac/mains
2955 * properties by reading the sysfs files.
2956 * AC/Mains properties are online, present and voltage.
2957 * online: ac/mains charging is in progress or not
2958 * present: presence of the ac/mains
2959 * voltage: AC/Mains voltage
2960 * Returns error code in case of failure else 0(on success)
2962 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2963 enum power_supply_property psp,
2964 union power_supply_propval *val)
2966 struct ab8500_charger *di;
2969 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2972 case POWER_SUPPLY_PROP_HEALTH:
2973 if (di->flags.mainextchnotok)
2974 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2975 else if (di->ac.wd_expired || di->usb.wd_expired)
2976 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2977 else if (di->flags.main_thermal_prot)
2978 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2980 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2982 case POWER_SUPPLY_PROP_ONLINE:
2983 val->intval = di->ac.charger_online;
2985 case POWER_SUPPLY_PROP_PRESENT:
2986 val->intval = di->ac.charger_connected;
2988 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2989 ret = ab8500_charger_get_ac_voltage(di);
2991 di->ac.charger_voltage = ret;
2992 /* On error, use previous value */
2993 val->intval = di->ac.charger_voltage * 1000;
2995 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2997 * This property is used to indicate when CV mode is entered
2998 * for the AC charger
3000 di->ac.cv_active = ab8500_charger_ac_cv(di);
3001 val->intval = di->ac.cv_active;
3003 case POWER_SUPPLY_PROP_CURRENT_NOW:
3004 ret = ab8500_charger_get_ac_current(di);
3006 di->ac.charger_current = ret;
3007 val->intval = di->ac.charger_current * 1000;
3016 * ab8500_charger_usb_get_property() - get the usb properties
3017 * @psy: pointer to the power_supply structure
3018 * @psp: pointer to the power_supply_property structure
3019 * @val: pointer to the power_supply_propval union
3021 * This function gets called when an application tries to get the usb
3022 * properties by reading the sysfs files.
3023 * USB properties are online, present and voltage.
3024 * online: usb charging is in progress or not
3025 * present: presence of the usb
3026 * voltage: vbus voltage
3027 * Returns error code in case of failure else 0(on success)
3029 static int ab8500_charger_usb_get_property(struct power_supply *psy,
3030 enum power_supply_property psp,
3031 union power_supply_propval *val)
3033 struct ab8500_charger *di;
3036 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
3039 case POWER_SUPPLY_PROP_HEALTH:
3040 if (di->flags.usbchargernotok)
3041 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
3042 else if (di->ac.wd_expired || di->usb.wd_expired)
3043 val->intval = POWER_SUPPLY_HEALTH_DEAD;
3044 else if (di->flags.usb_thermal_prot)
3045 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
3046 else if (di->flags.vbus_ovv)
3047 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
3049 val->intval = POWER_SUPPLY_HEALTH_GOOD;
3051 case POWER_SUPPLY_PROP_ONLINE:
3052 val->intval = di->usb.charger_online;
3054 case POWER_SUPPLY_PROP_PRESENT:
3055 val->intval = di->usb.charger_connected;
3057 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
3058 ret = ab8500_charger_get_vbus_voltage(di);
3060 di->usb.charger_voltage = ret;
3061 val->intval = di->usb.charger_voltage * 1000;
3063 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
3065 * This property is used to indicate when CV mode is entered
3066 * for the USB charger
3068 di->usb.cv_active = ab8500_charger_usb_cv(di);
3069 val->intval = di->usb.cv_active;
3071 case POWER_SUPPLY_PROP_CURRENT_NOW:
3072 ret = ab8500_charger_get_usb_current(di);
3074 di->usb.charger_current = ret;
3075 val->intval = di->usb.charger_current * 1000;
3077 case POWER_SUPPLY_PROP_CURRENT_AVG:
3079 * This property is used to indicate when VBUS has collapsed
3080 * due to too high output current from the USB charger
3082 if (di->flags.vbus_collapse)
3094 * ab8500_charger_init_hw_registers() - Set up charger related registers
3095 * @di: pointer to the ab8500_charger structure
3097 * Set up charger OVV, watchdog and maximum voltage registers as well as
3098 * charging of the backup battery
3100 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3103 u8 bup_vch_range = 0, vbup33_vrtcn = 0;
3105 /* Setup maximum charger current and voltage for ABB cut2.0 */
3106 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3107 ret = abx500_set_register_interruptible(di->dev,
3109 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3112 "failed to set CH_VOLT_LVL_MAX_REG\n");
3116 if (is_ab8540(di->parent))
3117 ret = abx500_set_register_interruptible(di->dev,
3118 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3121 ret = abx500_set_register_interruptible(di->dev,
3122 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3126 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3131 if (is_ab9540_2p0(di->parent) || is_ab9540_3p0(di->parent)
3132 || is_ab8505_2p0(di->parent) || is_ab8540(di->parent))
3133 ret = abx500_mask_and_set_register_interruptible(di->dev,
3135 AB8500_USBCH_CTRL2_REG,
3136 VBUS_AUTO_IN_CURR_LIM_ENA,
3137 VBUS_AUTO_IN_CURR_LIM_ENA);
3140 * VBUS OVV set to 6.3V and enable automatic current limitation
3142 ret = abx500_set_register_interruptible(di->dev,
3144 AB8500_USBCH_CTRL2_REG,
3145 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
3148 "failed to set automatic current limitation\n");
3152 /* Enable main watchdog in OTP */
3153 ret = abx500_set_register_interruptible(di->dev,
3154 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3156 dev_err(di->dev, "failed to enable main WD in OTP\n");
3160 /* Enable main watchdog */
3161 ret = abx500_set_register_interruptible(di->dev,
3162 AB8500_SYS_CTRL2_BLOCK,
3163 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3165 dev_err(di->dev, "faile to enable main watchdog\n");
3170 * Due to internal synchronisation, Enable and Kick watchdog bits
3171 * cannot be enabled in a single write.
3172 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3173 * between writing Enable then Kick bits.
3177 /* Kick main watchdog */
3178 ret = abx500_set_register_interruptible(di->dev,
3179 AB8500_SYS_CTRL2_BLOCK,
3180 AB8500_MAIN_WDOG_CTRL_REG,
3181 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3183 dev_err(di->dev, "failed to kick main watchdog\n");
3187 /* Disable main watchdog */
3188 ret = abx500_set_register_interruptible(di->dev,
3189 AB8500_SYS_CTRL2_BLOCK,
3190 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3192 dev_err(di->dev, "failed to disable main watchdog\n");
3196 /* Set watchdog timeout */
3197 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3198 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3200 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3204 ret = ab8500_charger_led_en(di, false);
3206 dev_err(di->dev, "failed to disable LED\n");
3210 /* Backup battery voltage and current */
3211 if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V)
3212 bup_vch_range = BUP_VCH_RANGE;
3213 if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V)
3214 vbup33_vrtcn = VBUP33_VRTCN;
3216 ret = abx500_set_register_interruptible(di->dev,
3218 AB8500_RTC_BACKUP_CHG_REG,
3219 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
3221 dev_err(di->dev, "failed to setup backup battery charging\n");
3224 if (is_ab8540(di->parent)) {
3225 ret = abx500_set_register_interruptible(di->dev,
3227 AB8500_RTC_CTRL1_REG,
3228 bup_vch_range | vbup33_vrtcn);
3231 "failed to setup backup battery charging\n");
3236 /* Enable backup battery charging */
3237 ret = abx500_mask_and_set_register_interruptible(di->dev,
3238 AB8500_RTC, AB8500_RTC_CTRL_REG,
3239 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3241 dev_err(di->dev, "%s mask and set failed\n", __func__);
3245 if (is_ab8540(di->parent)) {
3246 ret = abx500_mask_and_set_register_interruptible(di->dev,
3247 AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
3248 BUS_VSYS_VOL_SELECT_MASK, BUS_VSYS_VOL_SELECT_3P6V);
3251 "failed to setup usb power path vsys voltage\n");
3254 ret = abx500_mask_and_set_register_interruptible(di->dev,
3255 AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
3256 BUS_PP_PRECHG_CURRENT_MASK, 0);
3259 "failed to setup usb power path precharge current\n");
3269 * ab8500 charger driver interrupts and their respective isr
3271 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3272 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3273 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3274 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3275 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3276 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3277 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3278 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3279 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3280 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3281 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3282 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3283 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3284 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
3285 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
3288 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3289 unsigned long event, void *power)
3291 struct ab8500_charger *di =
3292 container_of(nb, struct ab8500_charger, nb);
3293 enum ab8500_usb_state bm_usb_state;
3294 unsigned mA = *((unsigned *)power);
3299 if (event != USB_EVENT_VBUS) {
3300 dev_dbg(di->dev, "not a standard host, returning\n");
3304 /* TODO: State is fabricate here. See if charger really needs USB
3305 * state or if mA is enough
3307 if ((di->usb_state.usb_current == 2) && (mA > 2))
3308 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3310 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3312 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3313 else if (mA >= 8) /* 8, 100, 500 */
3314 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3315 else /* Should never occur */
3316 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3318 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3319 __func__, bm_usb_state, mA);
3321 spin_lock(&di->usb_state.usb_lock);
3322 di->usb_state.state_tmp = bm_usb_state;
3323 di->usb_state.usb_current_tmp = mA;
3324 spin_unlock(&di->usb_state.usb_lock);
3327 * wait for some time until you get updates from the usb stack
3328 * and negotiations are completed
3330 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
3335 #if defined(CONFIG_PM)
3336 static int ab8500_charger_resume(struct platform_device *pdev)
3339 struct ab8500_charger *di = platform_get_drvdata(pdev);
3342 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3343 * logic. That means we have to continously kick the charger
3344 * watchdog even when no charger is connected. This is only
3345 * valid once the AC charger has been enabled. This is
3346 * a bug that is not handled by the algorithm and the
3347 * watchdog have to be kicked by the charger driver
3348 * when the AC charger is disabled
3350 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3351 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3352 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3354 dev_err(di->dev, "Failed to kick WD!\n");
3356 /* If not already pending start a new timer */
3357 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3358 round_jiffies(WD_KICK_INTERVAL));
3361 /* If we still have a HW failure, schedule a new check */
3362 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3363 queue_delayed_work(di->charger_wq,
3364 &di->check_hw_failure_work, 0);
3367 if (di->flags.vbus_drop_end)
3368 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3373 static int ab8500_charger_suspend(struct platform_device *pdev,
3376 struct ab8500_charger *di = platform_get_drvdata(pdev);
3378 /* Cancel any pending jobs */
3379 cancel_delayed_work(&di->check_hw_failure_work);
3380 cancel_delayed_work(&di->vbus_drop_end_work);
3382 flush_delayed_work(&di->attach_work);
3383 flush_delayed_work(&di->usb_charger_attached_work);
3384 flush_delayed_work(&di->ac_charger_attached_work);
3385 flush_delayed_work(&di->check_usbchgnotok_work);
3386 flush_delayed_work(&di->check_vbat_work);
3387 flush_delayed_work(&di->kick_wd_work);
3389 flush_work(&di->usb_link_status_work);
3390 flush_work(&di->ac_work);
3391 flush_work(&di->detect_usb_type_work);
3393 if (atomic_read(&di->current_stepping_sessions))
3399 #define ab8500_charger_suspend NULL
3400 #define ab8500_charger_resume NULL
3403 static struct notifier_block charger_nb = {
3404 .notifier_call = ab8500_external_charger_prepare,
3407 static int ab8500_charger_remove(struct platform_device *pdev)
3409 struct ab8500_charger *di = platform_get_drvdata(pdev);
3412 /* Disable AC charging */
3413 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3415 /* Disable USB charging */
3416 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3418 /* Disable interrupts */
3419 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3420 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3424 /* Backup battery voltage and current disable */
3425 ret = abx500_mask_and_set_register_interruptible(di->dev,
3426 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3428 dev_err(di->dev, "%s mask and set failed\n", __func__);
3430 usb_unregister_notifier(di->usb_phy, &di->nb);
3431 usb_put_phy(di->usb_phy);
3433 /* Delete the work queue */
3434 destroy_workqueue(di->charger_wq);
3436 /* Unregister external charger enable notifier */
3437 if (!di->ac_chg.enabled)
3438 blocking_notifier_chain_unregister(
3439 &charger_notifier_list, &charger_nb);
3441 flush_scheduled_work();
3442 if (di->usb_chg.enabled)
3443 power_supply_unregister(di->usb_chg.psy);
3445 if (di->ac_chg.enabled && !di->ac_chg.external)
3446 power_supply_unregister(di->ac_chg.psy);
3451 static char *supply_interface[] = {
3457 static const struct power_supply_desc ab8500_ac_chg_desc = {
3458 .name = "ab8500_ac",
3459 .type = POWER_SUPPLY_TYPE_MAINS,
3460 .properties = ab8500_charger_ac_props,
3461 .num_properties = ARRAY_SIZE(ab8500_charger_ac_props),
3462 .get_property = ab8500_charger_ac_get_property,
3465 static const struct power_supply_desc ab8500_usb_chg_desc = {
3466 .name = "ab8500_usb",
3467 .type = POWER_SUPPLY_TYPE_USB,
3468 .properties = ab8500_charger_usb_props,
3469 .num_properties = ARRAY_SIZE(ab8500_charger_usb_props),
3470 .get_property = ab8500_charger_usb_get_property,
3473 static int ab8500_charger_probe(struct platform_device *pdev)
3475 struct device_node *np = pdev->dev.of_node;
3476 struct abx500_bm_data *plat = pdev->dev.platform_data;
3477 struct power_supply_config ac_psy_cfg = {}, usb_psy_cfg = {};
3478 struct ab8500_charger *di;
3479 int irq, i, charger_status, ret = 0, ch_stat;
3481 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3483 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
3488 dev_err(&pdev->dev, "no battery management data supplied\n");
3494 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3496 dev_err(&pdev->dev, "failed to get battery information\n");
3499 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3501 di->autopower_cfg = false;
3503 /* get parent data */
3504 di->dev = &pdev->dev;
3505 di->parent = dev_get_drvdata(pdev->dev.parent);
3506 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3508 /* initialize lock */
3509 spin_lock_init(&di->usb_state.usb_lock);
3510 mutex_init(&di->usb_ipt_crnt_lock);
3512 di->autopower = false;
3513 di->invalid_charger_detect_state = 0;
3515 /* AC and USB supply config */
3516 ac_psy_cfg.supplied_to = supply_interface;
3517 ac_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3518 ac_psy_cfg.drv_data = &di->ac_chg;
3519 usb_psy_cfg.supplied_to = supply_interface;
3520 usb_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3521 usb_psy_cfg.drv_data = &di->usb_chg;
3524 /* ux500_charger sub-class */
3525 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
3526 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
3527 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3528 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3529 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3530 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3531 di->ac_chg.max_out_curr =
3532 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3533 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
3534 di->ac_chg.enabled = di->bm->ac_enabled;
3535 di->ac_chg.external = false;
3537 /*notifier for external charger enabling*/
3538 if (!di->ac_chg.enabled)
3539 blocking_notifier_chain_register(
3540 &charger_notifier_list, &charger_nb);
3543 /* ux500_charger sub-class */
3544 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
3545 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
3546 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3547 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3548 di->usb_chg.ops.pp_enable = &ab8540_charger_power_path_enable;
3549 di->usb_chg.ops.pre_chg_enable = &ab8540_charger_usb_pre_chg_enable;
3550 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3551 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3552 di->usb_chg.max_out_curr =
3553 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3554 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
3555 di->usb_chg.enabled = di->bm->usb_enabled;
3556 di->usb_chg.external = false;
3557 di->usb_chg.power_path = di->bm->usb_power_path;
3558 di->usb_state.usb_current = -1;
3560 /* Create a work queue for the charger */
3561 di->charger_wq = alloc_ordered_workqueue("ab8500_charger_wq",
3563 if (di->charger_wq == NULL) {
3564 dev_err(di->dev, "failed to create work queue\n");
3568 mutex_init(&di->charger_attached_mutex);
3570 /* Init work for HW failure check */
3571 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
3572 ab8500_charger_check_hw_failure_work);
3573 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
3574 ab8500_charger_check_usbchargernotok_work);
3576 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3577 ab8500_charger_ac_attached_work);
3578 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3579 ab8500_charger_usb_attached_work);
3582 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3583 * logic. That means we have to continously kick the charger
3584 * watchdog even when no charger is connected. This is only
3585 * valid once the AC charger has been enabled. This is
3586 * a bug that is not handled by the algorithm and the
3587 * watchdog have to be kicked by the charger driver
3588 * when the AC charger is disabled
3590 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
3591 ab8500_charger_kick_watchdog_work);
3593 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
3594 ab8500_charger_check_vbat_work);
3596 INIT_DELAYED_WORK(&di->attach_work,
3597 ab8500_charger_usb_link_attach_work);
3599 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3600 ab8500_charger_usb_state_changed_work);
3602 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3603 ab8500_charger_vbus_drop_end_work);
3605 /* Init work for charger detection */
3606 INIT_WORK(&di->usb_link_status_work,
3607 ab8500_charger_usb_link_status_work);
3608 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3609 INIT_WORK(&di->detect_usb_type_work,
3610 ab8500_charger_detect_usb_type_work);
3612 /* Init work for checking HW status */
3613 INIT_WORK(&di->check_main_thermal_prot_work,
3614 ab8500_charger_check_main_thermal_prot_work);
3615 INIT_WORK(&di->check_usb_thermal_prot_work,
3616 ab8500_charger_check_usb_thermal_prot_work);
3619 * VDD ADC supply needs to be enabled from this driver when there
3620 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3621 * interrupts during charging
3623 di->regu = devm_regulator_get(di->dev, "vddadc");
3624 if (IS_ERR(di->regu)) {
3625 ret = PTR_ERR(di->regu);
3626 dev_err(di->dev, "failed to get vddadc regulator\n");
3627 goto free_charger_wq;
3631 /* Initialize OVV, and other registers */
3632 ret = ab8500_charger_init_hw_registers(di);
3634 dev_err(di->dev, "failed to initialize ABB registers\n");
3635 goto free_charger_wq;
3638 /* Register AC charger class */
3639 if (di->ac_chg.enabled) {
3640 di->ac_chg.psy = power_supply_register(di->dev,
3641 &ab8500_ac_chg_desc,
3643 if (IS_ERR(di->ac_chg.psy)) {
3644 dev_err(di->dev, "failed to register AC charger\n");
3645 ret = PTR_ERR(di->ac_chg.psy);
3646 goto free_charger_wq;
3650 /* Register USB charger class */
3651 if (di->usb_chg.enabled) {
3652 di->usb_chg.psy = power_supply_register(di->dev,
3653 &ab8500_usb_chg_desc,
3655 if (IS_ERR(di->usb_chg.psy)) {
3656 dev_err(di->dev, "failed to register USB charger\n");
3657 ret = PTR_ERR(di->usb_chg.psy);
3662 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3663 if (IS_ERR_OR_NULL(di->usb_phy)) {
3664 dev_err(di->dev, "failed to get usb transceiver\n");
3668 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3669 ret = usb_register_notifier(di->usb_phy, &di->nb);
3671 dev_err(di->dev, "failed to register usb notifier\n");
3675 /* Identify the connected charger types during startup */
3676 charger_status = ab8500_charger_detect_chargers(di, true);
3677 if (charger_status & AC_PW_CONN) {
3678 di->ac.charger_connected = 1;
3680 ab8500_power_supply_changed(di, di->ac_chg.psy);
3681 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
3684 if (charger_status & USB_PW_CONN) {
3685 di->vbus_detected = true;
3686 di->vbus_detected_start = true;
3687 queue_work(di->charger_wq,
3688 &di->detect_usb_type_work);
3691 /* Register interrupts */
3692 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3693 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3694 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3695 IRQF_SHARED | IRQF_NO_SUSPEND,
3696 ab8500_charger_irq[i].name, di);
3699 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3700 , ab8500_charger_irq[i].name, irq, ret);
3703 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3704 ab8500_charger_irq[i].name, irq, ret);
3707 platform_set_drvdata(pdev, di);
3709 mutex_lock(&di->charger_attached_mutex);
3711 ch_stat = ab8500_charger_detect_chargers(di, false);
3713 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3714 if (is_ab8500(di->parent))
3715 queue_delayed_work(di->charger_wq,
3716 &di->ac_charger_attached_work,
3719 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3720 if (is_ab8500(di->parent))
3721 queue_delayed_work(di->charger_wq,
3722 &di->usb_charger_attached_work,
3726 mutex_unlock(&di->charger_attached_mutex);
3731 usb_unregister_notifier(di->usb_phy, &di->nb);
3733 /* We also have to free all successfully registered irqs */
3734 for (i = i - 1; i >= 0; i--) {
3735 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3739 usb_put_phy(di->usb_phy);
3741 if (di->usb_chg.enabled)
3742 power_supply_unregister(di->usb_chg.psy);
3744 if (di->ac_chg.enabled)
3745 power_supply_unregister(di->ac_chg.psy);
3747 destroy_workqueue(di->charger_wq);
3751 static const struct of_device_id ab8500_charger_match[] = {
3752 { .compatible = "stericsson,ab8500-charger", },
3755 MODULE_DEVICE_TABLE(of, ab8500_charger_match);
3757 static struct platform_driver ab8500_charger_driver = {
3758 .probe = ab8500_charger_probe,
3759 .remove = ab8500_charger_remove,
3760 .suspend = ab8500_charger_suspend,
3761 .resume = ab8500_charger_resume,
3763 .name = "ab8500-charger",
3764 .of_match_table = ab8500_charger_match,
3768 static int __init ab8500_charger_init(void)
3770 return platform_driver_register(&ab8500_charger_driver);
3773 static void __exit ab8500_charger_exit(void)
3775 platform_driver_unregister(&ab8500_charger_driver);
3778 subsys_initcall_sync(ab8500_charger_init);
3779 module_exit(ab8500_charger_exit);
3781 MODULE_LICENSE("GPL v2");
3782 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3783 MODULE_ALIAS("platform:ab8500-charger");
3784 MODULE_DESCRIPTION("AB8500 charger management driver");