1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2012
5 * Charger driver for AB8500
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/notifier.h>
19 #include <linux/slab.h>
20 #include <linux/platform_device.h>
21 #include <linux/power_supply.h>
22 #include <linux/completion.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/err.h>
25 #include <linux/workqueue.h>
26 #include <linux/kobject.h>
28 #include <linux/mfd/core.h>
29 #include <linux/mfd/abx500/ab8500.h>
30 #include <linux/mfd/abx500.h>
31 #include <linux/usb/otg.h>
32 #include <linux/mutex.h>
33 #include <linux/iio/consumer.h>
35 #include "ab8500-bm.h"
36 #include "ab8500-chargalg.h"
38 /* Charger constants */
43 #define MAIN_WDOG_ENA 0x01
44 #define MAIN_WDOG_KICK 0x02
45 #define MAIN_WDOG_DIS 0x00
46 #define CHARG_WD_KICK 0x01
47 #define MAIN_CH_ENA 0x01
48 #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
49 #define USB_CH_ENA 0x01
50 #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
51 #define MAIN_CH_DET 0x01
52 #define MAIN_CH_CV_ON 0x04
53 #define USB_CH_CV_ON 0x08
54 #define VBUS_DET_DBNC100 0x02
55 #define VBUS_DET_DBNC1 0x01
56 #define OTP_ENABLE_WD 0x01
57 #define DROP_COUNT_RESET 0x01
58 #define USB_CH_DET 0x01
60 #define MAIN_CH_INPUT_CURR_SHIFT 4
61 #define VBUS_IN_CURR_LIM_SHIFT 4
62 #define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
63 #define VBUS_IN_CURR_LIM_RETRY_SET_TIME 30 /* seconds */
65 #define LED_INDICATOR_PWM_ENA 0x01
66 #define LED_INDICATOR_PWM_DIS 0x00
67 #define LED_IND_CUR_5MA 0x04
68 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
70 /* HW failure constants */
71 #define MAIN_CH_TH_PROT 0x02
72 #define VBUS_CH_NOK 0x08
73 #define USB_CH_TH_PROT 0x02
74 #define VBUS_OVV_TH 0x01
75 #define MAIN_CH_NOK 0x01
78 #define MAIN_CH_STATUS2_MAINCHGDROP 0x80
79 #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
80 #define USB_CH_VBUSDROP 0x40
81 #define USB_CH_VBUSDETDBNC 0x01
83 /* UsbLineStatus register bit masks */
84 #define AB8500_USB_LINK_STATUS 0x78
85 #define AB8505_USB_LINK_STATUS 0xF8
86 #define AB8500_STD_HOST_SUSP 0x18
87 #define USB_LINK_STATUS_SHIFT 3
89 /* Watchdog timeout constant */
90 #define WD_TIMER 0x30 /* 4min */
91 #define WD_KICK_INTERVAL (60 * HZ)
93 /* Lowest charger voltage is 3.39V -> 0x4E */
94 #define LOW_VOLT_REG 0x4E
96 /* Step up/down delay in us */
97 #define STEP_UDELAY 1000
99 #define CHARGER_STATUS_POLL 10 /* in ms */
101 #define CHG_WD_INTERVAL (60 * HZ)
103 #define AB8500_SW_CONTROL_FALLBACK 0x03
104 /* Wait for enumeration before charing in us */
105 #define WAIT_ACA_RID_ENUMERATION (5 * 1000)
106 /*External charger control*/
107 #define AB8500_SYS_CHARGER_CONTROL_REG 0x52
108 #define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
109 #define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
111 /* UsbLineStatus register - usb types */
112 enum ab8500_charger_link_status {
113 USB_STAT_NOT_CONFIGURED,
114 USB_STAT_STD_HOST_NC,
115 USB_STAT_STD_HOST_C_NS,
116 USB_STAT_STD_HOST_C_S,
117 USB_STAT_HOST_CHG_NM,
118 USB_STAT_HOST_CHG_HS,
119 USB_STAT_HOST_CHG_HS_CHIRP,
120 USB_STAT_DEDICATED_CHG,
123 USB_STAT_ACA_RID_C_NM,
124 USB_STAT_ACA_RID_C_HS,
125 USB_STAT_ACA_RID_C_HS_CHIRP,
128 USB_STAT_NOT_VALID_LINK,
130 USB_STAT_SUP_NO_IDGND_VBUS,
131 USB_STAT_SUP_IDGND_VBUS,
132 USB_STAT_CHARGER_LINE_1,
135 USB_STAT_ACA_DOCK_CHARGER,
138 enum ab8500_usb_state {
139 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
140 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
141 AB8500_BM_USB_STATE_CONFIGURED,
142 AB8500_BM_USB_STATE_SUSPEND,
143 AB8500_BM_USB_STATE_RESUME,
144 AB8500_BM_USB_STATE_MAX,
147 /* VBUS input current limits supported in AB8500 in mA */
148 #define USB_CH_IP_CUR_LVL_0P05 50
149 #define USB_CH_IP_CUR_LVL_0P09 98
150 #define USB_CH_IP_CUR_LVL_0P19 193
151 #define USB_CH_IP_CUR_LVL_0P29 290
152 #define USB_CH_IP_CUR_LVL_0P38 380
153 #define USB_CH_IP_CUR_LVL_0P45 450
154 #define USB_CH_IP_CUR_LVL_0P5 500
155 #define USB_CH_IP_CUR_LVL_0P6 600
156 #define USB_CH_IP_CUR_LVL_0P7 700
157 #define USB_CH_IP_CUR_LVL_0P8 800
158 #define USB_CH_IP_CUR_LVL_0P9 900
159 #define USB_CH_IP_CUR_LVL_1P0 1000
160 #define USB_CH_IP_CUR_LVL_1P1 1100
161 #define USB_CH_IP_CUR_LVL_1P3 1300
162 #define USB_CH_IP_CUR_LVL_1P4 1400
163 #define USB_CH_IP_CUR_LVL_1P5 1500
165 #define VBAT_TRESH_IP_CUR_RED 3800
167 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
168 struct ab8500_charger, usb_chg)
169 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
170 struct ab8500_charger, ac_chg)
173 * struct ab8500_charger_interrupts - ab8500 interupts
174 * @name: name of the interrupt
175 * @isr function pointer to the isr
177 struct ab8500_charger_interrupts {
179 irqreturn_t (*isr)(int irq, void *data);
182 struct ab8500_charger_info {
183 int charger_connected;
191 struct ab8500_charger_event_flags {
193 bool main_thermal_prot;
194 bool usb_thermal_prot;
196 bool usbchargernotok;
202 struct ab8500_charger_usb_state {
205 enum ab8500_usb_state state;
206 enum ab8500_usb_state state_tmp;
210 struct ab8500_charger_max_usb_in_curr {
217 * struct ab8500_charger - ab8500 Charger device information
218 * @dev: Pointer to the structure device
219 * @vbus_detected: VBUS detected
220 * @vbus_detected_start:
221 * VBUS detected during startup
222 * @ac_conn: This will be true when the AC charger has been plugged
223 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
225 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
227 * @vbat Battery voltage
228 * @old_vbat Previously measured battery voltage
229 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
230 * @autopower Indicate if we should have automatic pwron after pwrloss
231 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
232 * @invalid_charger_detect_state State when forcing AB to use invalid charger
233 * @is_aca_rid: Incicate if accessory is ACA type
234 * @current_stepping_sessions:
235 * Counter for current stepping sessions
236 * @parent: Pointer to the struct ab8500
237 * @adc_main_charger_v ADC channel for main charger voltage
238 * @adc_main_charger_c ADC channel for main charger current
239 * @adc_vbus_v ADC channel for USB charger voltage
240 * @adc_usb_charger_c ADC channel for USB charger current
241 * @bm: Platform specific battery management information
242 * @flags: Structure for information about events triggered
243 * @usb_state: Structure for usb stack information
244 * @max_usb_in_curr: Max USB charger input current
245 * @ac_chg: AC charger power supply
246 * @usb_chg: USB charger power supply
247 * @ac: Structure that holds the AC charger properties
248 * @usb: Structure that holds the USB charger properties
249 * @regu: Pointer to the struct regulator
250 * @charger_wq: Work queue for the IRQs and checking HW state
251 * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
252 * @pm_lock: Lock to prevent system to suspend
253 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
254 * @check_hw_failure_work: Work for checking HW state
255 * @check_usbchgnotok_work: Work for checking USB charger not ok status
256 * @kick_wd_work: Work for kicking the charger watchdog in case
257 * of ABB rev 1.* due to the watchog logic bug
258 * @ac_charger_attached_work: Work for checking if AC charger is still
260 * @usb_charger_attached_work: Work for checking if USB charger is still
262 * @ac_work: Work for checking AC charger connection
263 * @detect_usb_type_work: Work for detecting the USB type connected
264 * @usb_link_status_work: Work for checking the new USB link status
265 * @usb_state_changed_work: Work for checking USB state
266 * @attach_work: Work for detecting USB type
267 * @vbus_drop_end_work: Work for detecting VBUS drop end
268 * @check_main_thermal_prot_work:
269 * Work for checking Main thermal status
270 * @check_usb_thermal_prot_work:
271 * Work for checking USB thermal status
272 * @charger_attached_mutex: For controlling the wakelock
274 struct ab8500_charger {
277 bool vbus_detected_start;
283 bool usb_device_is_unrecognised;
286 int invalid_charger_detect_state;
288 atomic_t current_stepping_sessions;
289 struct ab8500 *parent;
290 struct iio_channel *adc_main_charger_v;
291 struct iio_channel *adc_main_charger_c;
292 struct iio_channel *adc_vbus_v;
293 struct iio_channel *adc_usb_charger_c;
294 struct abx500_bm_data *bm;
295 struct ab8500_charger_event_flags flags;
296 struct ab8500_charger_usb_state usb_state;
297 struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
298 struct ux500_charger ac_chg;
299 struct ux500_charger usb_chg;
300 struct ab8500_charger_info ac;
301 struct ab8500_charger_info usb;
302 struct regulator *regu;
303 struct workqueue_struct *charger_wq;
304 struct mutex usb_ipt_crnt_lock;
305 struct delayed_work check_vbat_work;
306 struct delayed_work check_hw_failure_work;
307 struct delayed_work check_usbchgnotok_work;
308 struct delayed_work kick_wd_work;
309 struct delayed_work usb_state_changed_work;
310 struct delayed_work attach_work;
311 struct delayed_work ac_charger_attached_work;
312 struct delayed_work usb_charger_attached_work;
313 struct delayed_work vbus_drop_end_work;
314 struct work_struct ac_work;
315 struct work_struct detect_usb_type_work;
316 struct work_struct usb_link_status_work;
317 struct work_struct check_main_thermal_prot_work;
318 struct work_struct check_usb_thermal_prot_work;
319 struct usb_phy *usb_phy;
320 struct notifier_block nb;
321 struct mutex charger_attached_mutex;
325 static enum power_supply_property ab8500_charger_ac_props[] = {
326 POWER_SUPPLY_PROP_HEALTH,
327 POWER_SUPPLY_PROP_PRESENT,
328 POWER_SUPPLY_PROP_ONLINE,
329 POWER_SUPPLY_PROP_VOLTAGE_NOW,
330 POWER_SUPPLY_PROP_VOLTAGE_AVG,
331 POWER_SUPPLY_PROP_CURRENT_NOW,
335 static enum power_supply_property ab8500_charger_usb_props[] = {
336 POWER_SUPPLY_PROP_HEALTH,
337 POWER_SUPPLY_PROP_CURRENT_AVG,
338 POWER_SUPPLY_PROP_PRESENT,
339 POWER_SUPPLY_PROP_ONLINE,
340 POWER_SUPPLY_PROP_VOLTAGE_NOW,
341 POWER_SUPPLY_PROP_VOLTAGE_AVG,
342 POWER_SUPPLY_PROP_CURRENT_NOW,
346 * Function for enabling and disabling sw fallback mode
347 * should always be disabled when no charger is connected.
349 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
358 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
360 if (is_ab8500(di->parent)) {
365 bank = AB8500_SYS_CTRL1_BLOCK;
366 reg = AB8500_SW_CONTROL_FALLBACK;
370 /* read the register containing fallback bit */
371 ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
373 dev_err(di->dev, "%d read failed\n", __LINE__);
377 if (is_ab8500(di->parent)) {
378 /* enable the OPT emulation registers */
379 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
381 dev_err(di->dev, "%d write failed\n", __LINE__);
391 /* write back the changed fallback bit value to register */
392 ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
394 dev_err(di->dev, "%d write failed\n", __LINE__);
398 if (is_ab8500(di->parent)) {
399 /* disable the set OTP registers again */
400 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
402 dev_err(di->dev, "%d write failed\n", __LINE__);
408 * ab8500_power_supply_changed - a wrapper with local extensions for
409 * power_supply_changed
410 * @di: pointer to the ab8500_charger structure
411 * @psy: pointer to power_supply_that have changed.
414 static void ab8500_power_supply_changed(struct ab8500_charger *di,
415 struct power_supply *psy)
417 if (di->autopower_cfg) {
418 if (!di->usb.charger_connected &&
419 !di->ac.charger_connected &&
421 di->autopower = false;
422 ab8500_enable_disable_sw_fallback(di, false);
423 } else if (!di->autopower &&
424 (di->ac.charger_connected ||
425 di->usb.charger_connected)) {
426 di->autopower = true;
427 ab8500_enable_disable_sw_fallback(di, true);
430 power_supply_changed(psy);
433 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
436 if (connected != di->usb.charger_connected) {
437 dev_dbg(di->dev, "USB connected:%i\n", connected);
438 di->usb.charger_connected = connected;
441 di->flags.vbus_drop_end = false;
443 sysfs_notify(&di->usb_chg.psy->dev.kobj, NULL, "present");
446 mutex_lock(&di->charger_attached_mutex);
447 mutex_unlock(&di->charger_attached_mutex);
449 if (is_ab8500(di->parent))
450 queue_delayed_work(di->charger_wq,
451 &di->usb_charger_attached_work,
454 cancel_delayed_work_sync(&di->usb_charger_attached_work);
455 mutex_lock(&di->charger_attached_mutex);
456 mutex_unlock(&di->charger_attached_mutex);
462 * ab8500_charger_get_ac_voltage() - get ac charger voltage
463 * @di: pointer to the ab8500_charger structure
465 * Returns ac charger voltage (on success)
467 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
471 /* Only measure voltage if the charger is connected */
472 if (di->ac.charger_connected) {
473 ret = iio_read_channel_processed(di->adc_main_charger_v, &vch);
475 dev_err(di->dev, "%s ADC conv failed,\n", __func__);
483 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
484 * @di: pointer to the ab8500_charger structure
486 * Returns ac charger CV mode (on success) else error code
488 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
493 /* Only check CV mode if the charger is online */
494 if (di->ac.charger_online) {
495 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
496 AB8500_CH_STATUS1_REG, &val);
498 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
502 if (val & MAIN_CH_CV_ON)
512 * ab8500_charger_get_vbus_voltage() - get vbus voltage
513 * @di: pointer to the ab8500_charger structure
515 * This function returns the vbus voltage.
516 * Returns vbus voltage (on success)
518 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
522 /* Only measure voltage if the charger is connected */
523 if (di->usb.charger_connected) {
524 ret = iio_read_channel_processed(di->adc_vbus_v, &vch);
526 dev_err(di->dev, "%s ADC conv failed,\n", __func__);
534 * ab8500_charger_get_usb_current() - get usb charger current
535 * @di: pointer to the ab8500_charger structure
537 * This function returns the usb charger current.
538 * Returns usb current (on success) and error code on failure
540 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
544 /* Only measure current if the charger is online */
545 if (di->usb.charger_online) {
546 ret = iio_read_channel_processed(di->adc_usb_charger_c, &ich);
548 dev_err(di->dev, "%s ADC conv failed,\n", __func__);
556 * ab8500_charger_get_ac_current() - get ac charger current
557 * @di: pointer to the ab8500_charger structure
559 * This function returns the ac charger current.
560 * Returns ac current (on success) and error code on failure.
562 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
566 /* Only measure current if the charger is online */
567 if (di->ac.charger_online) {
568 ret = iio_read_channel_processed(di->adc_main_charger_c, &ich);
570 dev_err(di->dev, "%s ADC conv failed,\n", __func__);
578 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
579 * @di: pointer to the ab8500_charger structure
581 * Returns ac charger CV mode (on success) else error code
583 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
588 /* Only check CV mode if the charger is online */
589 if (di->usb.charger_online) {
590 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
591 AB8500_CH_USBCH_STAT1_REG, &val);
593 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
597 if (val & USB_CH_CV_ON)
609 * ab8500_charger_detect_chargers() - Detect the connected chargers
610 * @di: pointer to the ab8500_charger structure
611 * @probe: if probe, don't delay and wait for HW
613 * Returns the type of charger connected.
614 * For USB it will not mean we can actually charge from it
615 * but that there is a USB cable connected that we have to
616 * identify. This is used during startup when we don't get
617 * interrupts of the charger detection
619 * Returns an integer value, that means,
620 * NO_PW_CONN no power supply is connected
621 * AC_PW_CONN if the AC power supply is connected
622 * USB_PW_CONN if the USB power supply is connected
623 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
625 static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
627 int result = NO_PW_CONN;
631 /* Check for AC charger */
632 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
633 AB8500_CH_STATUS1_REG, &val);
635 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
639 if (val & MAIN_CH_DET)
642 /* Check for USB charger */
646 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
647 * when disconnecting ACA even though no
648 * charger was connected. Try waiting a little
649 * longer than the 100 ms of VBUS_DET_DBNC100...
653 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
654 AB8500_CH_USBCH_STAT1_REG, &val);
656 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
660 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
662 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
663 result |= USB_PW_CONN;
669 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
670 * @di: pointer to the ab8500_charger structure
671 * @link_status: the identified USB type
673 * Get the maximum current that is allowed to be drawn from the host
674 * based on the USB type.
675 * Returns error code in case of failure else 0 on success
677 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
678 enum ab8500_charger_link_status link_status)
682 di->usb_device_is_unrecognised = false;
685 * Platform only supports USB 2.0.
686 * This means that charging current from USB source
687 * is maximum 500 mA. Every occurrence of USB_STAT_*_HOST_*
688 * should set USB_CH_IP_CUR_LVL_0P5.
691 switch (link_status) {
692 case USB_STAT_STD_HOST_NC:
693 case USB_STAT_STD_HOST_C_NS:
694 case USB_STAT_STD_HOST_C_S:
695 dev_dbg(di->dev, "USB Type - Standard host is "
696 "detected through USB driver\n");
697 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
700 case USB_STAT_HOST_CHG_HS_CHIRP:
701 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
704 case USB_STAT_HOST_CHG_HS:
705 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
708 case USB_STAT_ACA_RID_C_HS:
709 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
712 case USB_STAT_ACA_RID_A:
714 * Dedicated charger level minus maximum current accessory
715 * can consume (900mA). Closest level is 500mA
717 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
718 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
721 case USB_STAT_ACA_RID_B:
723 * Dedicated charger level minus 120mA (20mA for ACA and
724 * 100mA for potential accessory). Closest level is 1300mA
726 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
727 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
728 di->max_usb_in_curr.usb_type_max);
731 case USB_STAT_HOST_CHG_NM:
732 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
735 case USB_STAT_DEDICATED_CHG:
736 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
739 case USB_STAT_ACA_RID_C_HS_CHIRP:
740 case USB_STAT_ACA_RID_C_NM:
741 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
744 case USB_STAT_NOT_CONFIGURED:
745 if (di->vbus_detected) {
746 di->usb_device_is_unrecognised = true;
747 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
748 di->max_usb_in_curr.usb_type_max =
749 USB_CH_IP_CUR_LVL_1P5;
753 case USB_STAT_HM_IDGND:
754 dev_err(di->dev, "USB Type - Charging not allowed\n");
755 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
758 case USB_STAT_RESERVED:
759 if (is_ab8500(di->parent)) {
760 di->flags.vbus_collapse = true;
761 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
762 "VBUS has collapsed\n");
766 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
767 di->max_usb_in_curr.usb_type_max =
768 USB_CH_IP_CUR_LVL_0P05;
769 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
771 di->max_usb_in_curr.usb_type_max);
775 case USB_STAT_CARKIT_1:
776 case USB_STAT_CARKIT_2:
777 case USB_STAT_ACA_DOCK_CHARGER:
778 case USB_STAT_CHARGER_LINE_1:
779 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
780 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
781 di->max_usb_in_curr.usb_type_max);
783 case USB_STAT_NOT_VALID_LINK:
784 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
785 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
789 dev_err(di->dev, "USB Type - Unknown\n");
790 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
795 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
796 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
797 link_status, di->max_usb_in_curr.set_max);
803 * ab8500_charger_read_usb_type() - read the type of usb connected
804 * @di: pointer to the ab8500_charger structure
806 * Detect the type of the plugged USB
807 * Returns error code in case of failure else 0 on success
809 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
814 ret = abx500_get_register_interruptible(di->dev,
815 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
817 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
820 if (is_ab8500(di->parent))
821 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
822 AB8500_USB_LINE_STAT_REG, &val);
824 ret = abx500_get_register_interruptible(di->dev,
825 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
827 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
831 /* get the USB type */
832 if (is_ab8500(di->parent))
833 val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
835 val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
836 ret = ab8500_charger_max_usb_curr(di,
837 (enum ab8500_charger_link_status) val);
843 * ab8500_charger_detect_usb_type() - get the type of usb connected
844 * @di: pointer to the ab8500_charger structure
846 * Detect the type of the plugged USB
847 * Returns error code in case of failure else 0 on success
849 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
855 * On getting the VBUS rising edge detect interrupt there
856 * is a 250ms delay after which the register UsbLineStatus
857 * is filled with valid data.
859 for (i = 0; i < 10; i++) {
861 ret = abx500_get_register_interruptible(di->dev,
862 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
864 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
867 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
871 if (is_ab8500(di->parent))
872 ret = abx500_get_register_interruptible(di->dev,
873 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
875 ret = abx500_get_register_interruptible(di->dev,
876 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
878 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
881 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
884 * Until the IT source register is read the UsbLineStatus
885 * register is not updated, hence doing the same
889 /* get the USB type */
890 if (is_ab8500(di->parent))
891 val = (val & AB8500_USB_LINK_STATUS) >>
892 USB_LINK_STATUS_SHIFT;
894 val = (val & AB8505_USB_LINK_STATUS) >>
895 USB_LINK_STATUS_SHIFT;
899 ret = ab8500_charger_max_usb_curr(di,
900 (enum ab8500_charger_link_status) val);
906 * This array maps the raw hex value to charger voltage used by the AB8500
907 * Values taken from the UM0836
909 static int ab8500_charger_voltage_map[] = {
990 static int ab8500_voltage_to_regval(int voltage)
994 /* Special case for voltage below 3.5V */
995 if (voltage < ab8500_charger_voltage_map[0])
998 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
999 if (voltage < ab8500_charger_voltage_map[i])
1003 /* If not last element, return error */
1004 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
1005 if (voltage == ab8500_charger_voltage_map[i])
1011 static int ab8500_current_to_regval(struct ab8500_charger *di, int curr)
1015 if (curr < di->bm->chg_output_curr[0])
1018 for (i = 0; i < di->bm->n_chg_out_curr; i++) {
1019 if (curr < di->bm->chg_output_curr[i])
1023 /* If not last element, return error */
1024 i = di->bm->n_chg_out_curr - 1;
1025 if (curr == di->bm->chg_output_curr[i])
1031 static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr)
1035 if (curr < di->bm->chg_input_curr[0])
1038 for (i = 0; i < di->bm->n_chg_in_curr; i++) {
1039 if (curr < di->bm->chg_input_curr[i])
1043 /* If not last element, return error */
1044 i = di->bm->n_chg_in_curr - 1;
1045 if (curr == di->bm->chg_input_curr[i])
1052 * ab8500_charger_get_usb_cur() - get usb current
1053 * @di: pointer to the ab8500_charger structre
1055 * The usb stack provides the maximum current that can be drawn from
1056 * the standard usb host. This will be in mA.
1057 * This function converts current in mA to a value that can be written
1058 * to the register. Returns -1 if charging is not allowed
1060 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1063 switch (di->usb_state.usb_current) {
1065 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
1068 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
1071 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
1074 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
1077 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
1080 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
1084 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
1089 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1090 * @di: pointer to the ab8500_charger structure
1091 * @reg: select what charger register to check
1093 * Check if current stepping should be allowed to continue.
1094 * Checks if charger source has not collapsed. If it has, further stepping
1097 static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1100 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1101 return !di->flags.vbus_drop_end;
1107 * ab8500_charger_set_current() - set charger current
1108 * @di: pointer to the ab8500_charger structure
1109 * @ich: charger current, in mA
1110 * @reg: select what charger register to set
1112 * Set charger current.
1113 * There is no state machine in the AB to step up/down the charger
1114 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1115 * charging is started. Instead we need to implement
1116 * this charger current step-up/down here.
1117 * Returns error code in case of failure else 0(on success)
1119 static int ab8500_charger_set_current(struct ab8500_charger *di,
1123 int curr_index, prev_curr_index, shift_value, i;
1126 bool no_stepping = false;
1128 atomic_inc(&di->current_stepping_sessions);
1130 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1133 dev_err(di->dev, "%s read failed\n", __func__);
1134 goto exit_set_current;
1138 case AB8500_MCH_IPT_CURLVL_REG:
1139 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1140 prev_curr_index = (reg_value >> shift_value);
1141 curr_index = ab8500_current_to_regval(di, ich);
1142 step_udelay = STEP_UDELAY;
1143 if (!di->ac.charger_connected)
1146 case AB8500_USBCH_IPT_CRNTLVL_REG:
1147 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1148 prev_curr_index = (reg_value >> shift_value);
1149 curr_index = ab8500_vbus_in_curr_to_regval(di, ich);
1150 step_udelay = STEP_UDELAY * 100;
1152 if (!di->usb.charger_connected)
1155 case AB8500_CH_OPT_CRNTLVL_REG:
1157 prev_curr_index = (reg_value >> shift_value);
1158 curr_index = ab8500_current_to_regval(di, ich);
1159 step_udelay = STEP_UDELAY;
1160 if (curr_index && (curr_index - prev_curr_index) > 1)
1163 if (!di->usb.charger_connected && !di->ac.charger_connected)
1168 dev_err(di->dev, "%s current register not valid\n", __func__);
1170 goto exit_set_current;
1173 if (curr_index < 0) {
1174 dev_err(di->dev, "requested current limit out-of-range\n");
1176 goto exit_set_current;
1179 /* only update current if it's been changed */
1180 if (prev_curr_index == curr_index) {
1181 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1184 goto exit_set_current;
1187 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1188 __func__, ich, reg);
1191 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1192 reg, (u8)curr_index << shift_value);
1194 dev_err(di->dev, "%s write failed\n", __func__);
1195 } else if (prev_curr_index > curr_index) {
1196 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1197 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1198 (u8) i << shift_value, reg);
1199 ret = abx500_set_register_interruptible(di->dev,
1200 AB8500_CHARGER, reg, (u8)i << shift_value);
1202 dev_err(di->dev, "%s write failed\n", __func__);
1203 goto exit_set_current;
1205 if (i != curr_index)
1206 usleep_range(step_udelay, step_udelay * 2);
1210 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
1211 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1212 (u8)i << shift_value, reg);
1213 ret = abx500_set_register_interruptible(di->dev,
1214 AB8500_CHARGER, reg, (u8)i << shift_value);
1216 dev_err(di->dev, "%s write failed\n", __func__);
1217 goto exit_set_current;
1219 if (i != curr_index)
1220 usleep_range(step_udelay, step_udelay * 2);
1222 allow = ab8500_charger_check_continue_stepping(di, reg);
1227 atomic_dec(&di->current_stepping_sessions);
1233 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1234 * @di: pointer to the ab8500_charger structure
1235 * @ich_in: charger input current limit
1237 * Sets the current that can be drawn from the USB host
1238 * Returns error code in case of failure else 0(on success)
1240 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1246 /* We should always use to lowest current limit */
1247 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1248 if (di->max_usb_in_curr.set_max > 0)
1249 min_value = min(di->max_usb_in_curr.set_max, min_value);
1251 if (di->usb_state.usb_current >= 0)
1252 min_value = min(di->usb_state.usb_current, min_value);
1254 switch (min_value) {
1256 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1257 min_value = USB_CH_IP_CUR_LVL_0P05;
1260 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1261 min_value = USB_CH_IP_CUR_LVL_0P45;
1267 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1269 mutex_lock(&di->usb_ipt_crnt_lock);
1270 ret = ab8500_charger_set_current(di, min_value,
1271 AB8500_USBCH_IPT_CRNTLVL_REG);
1272 mutex_unlock(&di->usb_ipt_crnt_lock);
1278 * ab8500_charger_set_main_in_curr() - set main charger input current
1279 * @di: pointer to the ab8500_charger structure
1280 * @ich_in: input charger current, in mA
1282 * Set main charger input current.
1283 * Returns error code in case of failure else 0(on success)
1285 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1288 return ab8500_charger_set_current(di, ich_in,
1289 AB8500_MCH_IPT_CURLVL_REG);
1293 * ab8500_charger_set_output_curr() - set charger output current
1294 * @di: pointer to the ab8500_charger structure
1295 * @ich_out: output charger current, in mA
1297 * Set charger output current.
1298 * Returns error code in case of failure else 0(on success)
1300 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1303 return ab8500_charger_set_current(di, ich_out,
1304 AB8500_CH_OPT_CRNTLVL_REG);
1308 * ab8500_charger_led_en() - turn on/off chargign led
1309 * @di: pointer to the ab8500_charger structure
1310 * @on: flag to turn on/off the chargign led
1312 * Power ON/OFF charging LED indication
1313 * Returns error code in case of failure else 0(on success)
1315 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1320 /* Power ON charging LED indicator, set LED current to 5mA */
1321 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1322 AB8500_LED_INDICATOR_PWM_CTRL,
1323 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1325 dev_err(di->dev, "Power ON LED failed\n");
1328 /* LED indicator PWM duty cycle 252/256 */
1329 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1330 AB8500_LED_INDICATOR_PWM_DUTY,
1331 LED_INDICATOR_PWM_DUTY_252_256);
1333 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1337 /* Power off charging LED indicator */
1338 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1339 AB8500_LED_INDICATOR_PWM_CTRL,
1340 LED_INDICATOR_PWM_DIS);
1342 dev_err(di->dev, "Power-off LED failed\n");
1351 * ab8500_charger_ac_en() - enable or disable ac charging
1352 * @di: pointer to the ab8500_charger structure
1353 * @enable: enable/disable flag
1354 * @vset: charging voltage
1355 * @iset: charging current
1357 * Enable/Disable AC/Mains charging and turns on/off the charging led
1360 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1361 int enable, int vset, int iset)
1366 int input_curr_index;
1369 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1372 /* Check if AC is connected */
1373 if (!di->ac.charger_connected) {
1374 dev_err(di->dev, "AC charger not connected\n");
1378 /* Enable AC charging */
1379 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1382 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1383 * will be triggered every time we enable the VDD ADC supply.
1384 * This will turn off charging for a short while.
1385 * It can be avoided by having the supply on when
1386 * there is a charger enabled. Normally the VDD ADC supply
1387 * is enabled every time a GPADC conversion is triggered.
1388 * We will force it to be enabled from this driver to have
1389 * the GPADC module independent of the AB8500 chargers
1391 if (!di->vddadc_en_ac) {
1392 ret = regulator_enable(di->regu);
1395 "Failed to enable regulator\n");
1397 di->vddadc_en_ac = true;
1400 /* Check if the requested voltage or current is valid */
1401 volt_index = ab8500_voltage_to_regval(vset);
1402 curr_index = ab8500_current_to_regval(di, iset);
1403 input_curr_index = ab8500_current_to_regval(di,
1404 di->bm->chg_params->ac_curr_max);
1405 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1407 "Charger voltage or current too high, "
1408 "charging not started\n");
1412 /* ChVoltLevel: maximum battery charging voltage */
1413 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1414 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1416 dev_err(di->dev, "%s write failed\n", __func__);
1419 /* MainChInputCurr: current that can be drawn from the charger*/
1420 ret = ab8500_charger_set_main_in_curr(di,
1421 di->bm->chg_params->ac_curr_max);
1423 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1427 /* ChOutputCurentLevel: protected output current */
1428 ret = ab8500_charger_set_output_curr(di, iset);
1430 dev_err(di->dev, "%s "
1431 "Failed to set ChOutputCurentLevel\n",
1436 /* Check if VBAT overshoot control should be enabled */
1437 if (!di->bm->enable_overshoot)
1438 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1440 /* Enable Main Charger */
1441 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1442 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1444 dev_err(di->dev, "%s write failed\n", __func__);
1448 /* Power on charging LED indication */
1449 ret = ab8500_charger_led_en(di, true);
1451 dev_err(di->dev, "failed to enable LED\n");
1453 di->ac.charger_online = 1;
1455 /* Disable AC charging */
1456 if (is_ab8500_1p1_or_earlier(di->parent)) {
1458 * For ABB revision 1.0 and 1.1 there is a bug in the
1459 * watchdog logic. That means we have to continuously
1460 * kick the charger watchdog even when no charger is
1461 * connected. This is only valid once the AC charger
1462 * has been enabled. This is a bug that is not handled
1463 * by the algorithm and the watchdog have to be kicked
1464 * by the charger driver when the AC charger
1468 queue_delayed_work(di->charger_wq,
1470 round_jiffies(WD_KICK_INTERVAL));
1474 * We can't turn off charging completely
1475 * due to a bug in AB8500 cut1.
1476 * If we do, charging will not start again.
1477 * That is why we set the lowest voltage
1478 * and current possible
1480 ret = abx500_set_register_interruptible(di->dev,
1482 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1485 "%s write failed\n", __func__);
1489 ret = ab8500_charger_set_output_curr(di, 0);
1491 dev_err(di->dev, "%s "
1492 "Failed to set ChOutputCurentLevel\n",
1497 ret = abx500_set_register_interruptible(di->dev,
1499 AB8500_MCH_CTRL1, 0);
1502 "%s write failed\n", __func__);
1507 ret = ab8500_charger_led_en(di, false);
1509 dev_err(di->dev, "failed to disable LED\n");
1511 di->ac.charger_online = 0;
1512 di->ac.wd_expired = false;
1514 /* Disable regulator if enabled */
1515 if (di->vddadc_en_ac) {
1516 regulator_disable(di->regu);
1517 di->vddadc_en_ac = false;
1520 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1522 ab8500_power_supply_changed(di, di->ac_chg.psy);
1528 * ab8500_charger_usb_en() - enable usb charging
1529 * @di: pointer to the ab8500_charger structure
1530 * @enable: enable/disable flag
1531 * @vset: charging voltage
1532 * @ich_out: charger output current
1534 * Enable/Disable USB charging and turns on/off the charging led respectively.
1535 * Returns error code in case of failure else 0(on success)
1537 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1538 int enable, int vset, int ich_out)
1545 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1548 /* Check if USB is connected */
1549 if (!di->usb.charger_connected) {
1550 dev_err(di->dev, "USB charger not connected\n");
1555 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1556 * will be triggered every time we enable the VDD ADC supply.
1557 * This will turn off charging for a short while.
1558 * It can be avoided by having the supply on when
1559 * there is a charger enabled. Normally the VDD ADC supply
1560 * is enabled every time a GPADC conversion is triggered.
1561 * We will force it to be enabled from this driver to have
1562 * the GPADC module independent of the AB8500 chargers
1564 if (!di->vddadc_en_usb) {
1565 ret = regulator_enable(di->regu);
1568 "Failed to enable regulator\n");
1570 di->vddadc_en_usb = true;
1573 /* Enable USB charging */
1574 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1576 /* Check if the requested voltage or current is valid */
1577 volt_index = ab8500_voltage_to_regval(vset);
1578 curr_index = ab8500_current_to_regval(di, ich_out);
1579 if (volt_index < 0 || curr_index < 0) {
1581 "Charger voltage or current too high, "
1582 "charging not started\n");
1587 * ChVoltLevel: max voltage up to which battery can be
1590 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1591 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1593 dev_err(di->dev, "%s write failed\n", __func__);
1596 /* Check if VBAT overshoot control should be enabled */
1597 if (!di->bm->enable_overshoot)
1598 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1600 /* Enable USB Charger */
1602 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
1603 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1604 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1606 dev_err(di->dev, "%s write failed\n", __func__);
1610 /* If success power on charging LED indication */
1611 ret = ab8500_charger_led_en(di, true);
1613 dev_err(di->dev, "failed to enable LED\n");
1615 di->usb.charger_online = 1;
1617 /* USBChInputCurr: current that can be drawn from the usb */
1618 ret = ab8500_charger_set_vbus_in_curr(di,
1619 di->max_usb_in_curr.usb_type_max);
1621 dev_err(di->dev, "setting USBChInputCurr failed\n");
1625 /* ChOutputCurentLevel: protected output current */
1626 ret = ab8500_charger_set_output_curr(di, ich_out);
1628 dev_err(di->dev, "%s "
1629 "Failed to set ChOutputCurentLevel\n",
1634 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1637 /* Disable USB charging */
1638 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1639 ret = abx500_set_register_interruptible(di->dev,
1641 AB8500_USBCH_CTRL1_REG, 0);
1644 "%s write failed\n", __func__);
1648 ret = ab8500_charger_led_en(di, false);
1650 dev_err(di->dev, "failed to disable LED\n");
1651 /* USBChInputCurr: current that can be drawn from the usb */
1652 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1654 dev_err(di->dev, "setting USBChInputCurr failed\n");
1658 /* ChOutputCurentLevel: protected output current */
1659 ret = ab8500_charger_set_output_curr(di, 0);
1661 dev_err(di->dev, "%s "
1662 "Failed to reset ChOutputCurentLevel\n",
1666 di->usb.charger_online = 0;
1667 di->usb.wd_expired = false;
1669 /* Disable regulator if enabled */
1670 if (di->vddadc_en_usb) {
1671 regulator_disable(di->regu);
1672 di->vddadc_en_usb = false;
1675 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1677 /* Cancel any pending Vbat check work */
1678 cancel_delayed_work(&di->check_vbat_work);
1681 ab8500_power_supply_changed(di, di->usb_chg.psy);
1686 static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1687 unsigned long event, void *data)
1690 struct device *dev = data;
1691 /*Toggle External charger control pin*/
1692 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1693 AB8500_SYS_CHARGER_CONTROL_REG,
1694 EXTERNAL_CHARGER_DISABLE_REG_VAL);
1696 dev_err(dev, "write reg failed %d\n", ret);
1699 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1700 AB8500_SYS_CHARGER_CONTROL_REG,
1701 EXTERNAL_CHARGER_ENABLE_REG_VAL);
1703 dev_err(dev, "Write reg failed %d\n", ret);
1710 * ab8500_charger_usb_check_enable() - enable usb charging
1711 * @charger: pointer to the ux500_charger structure
1712 * @vset: charging voltage
1713 * @iset: charger output current
1715 * Check if the VBUS charger has been disconnected and reconnected without
1716 * AB8500 rising an interrupt. Returns 0 on success.
1718 static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1724 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1726 if (!di->usb.charger_connected)
1729 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1730 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1732 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1735 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1737 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1738 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1740 ret = abx500_mask_and_set_register_interruptible(di->dev,
1741 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1742 DROP_COUNT_RESET, DROP_COUNT_RESET);
1744 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1748 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1750 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1759 * ab8500_charger_ac_check_enable() - enable usb charging
1760 * @charger: pointer to the ux500_charger structure
1761 * @vset: charging voltage
1762 * @iset: charger output current
1764 * Check if the AC charger has been disconnected and reconnected without
1765 * AB8500 rising an interrupt. Returns 0 on success.
1767 static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1770 u8 mainch_ctrl1 = 0;
1773 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1775 if (!di->ac.charger_connected)
1778 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1779 AB8500_MCH_CTRL1, &mainch_ctrl1);
1781 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1784 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1786 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1787 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1789 ret = abx500_mask_and_set_register_interruptible(di->dev,
1790 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1791 DROP_COUNT_RESET, DROP_COUNT_RESET);
1794 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1798 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1800 dev_err(di->dev, "failed to enable AC charger %d\n",
1809 * ab8500_charger_watchdog_kick() - kick charger watchdog
1810 * @di: pointer to the ab8500_charger structure
1812 * Kick charger watchdog
1813 * Returns error code in case of failure else 0(on success)
1815 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1818 struct ab8500_charger *di;
1820 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1821 di = to_ab8500_charger_ac_device_info(charger);
1822 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1823 di = to_ab8500_charger_usb_device_info(charger);
1827 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1828 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1830 dev_err(di->dev, "Failed to kick WD!\n");
1836 * ab8500_charger_update_charger_current() - update charger current
1837 * @di: pointer to the ab8500_charger structure
1839 * Update the charger output current for the specified charger
1840 * Returns error code in case of failure else 0(on success)
1842 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1846 struct ab8500_charger *di;
1848 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1849 di = to_ab8500_charger_ac_device_info(charger);
1850 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1851 di = to_ab8500_charger_usb_device_info(charger);
1855 ret = ab8500_charger_set_output_curr(di, ich_out);
1857 dev_err(di->dev, "%s "
1858 "Failed to set ChOutputCurentLevel\n",
1863 /* Reset the main and usb drop input current measurement counter */
1864 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1865 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
1867 dev_err(di->dev, "%s write failed\n", __func__);
1874 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1876 struct power_supply *psy;
1877 struct power_supply *ext = dev_get_drvdata(dev);
1878 const char **supplicants = (const char **)ext->supplied_to;
1879 struct ab8500_charger *di;
1880 union power_supply_propval ret;
1882 struct ux500_charger *usb_chg;
1884 usb_chg = (struct ux500_charger *)data;
1887 di = to_ab8500_charger_usb_device_info(usb_chg);
1889 /* For all psy where the driver name appears in any supplied_to */
1890 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
1894 /* Go through all properties for the psy */
1895 for (j = 0; j < ext->desc->num_properties; j++) {
1896 enum power_supply_property prop;
1897 prop = ext->desc->properties[j];
1899 if (power_supply_get_property(ext, prop, &ret))
1903 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1904 switch (ext->desc->type) {
1905 case POWER_SUPPLY_TYPE_BATTERY:
1906 di->vbat = ret.intval / 1000;
1920 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1921 * @work pointer to the work_struct structure
1923 * Due to a asic bug it is necessary to lower the input current to the vbus
1924 * charger when charging with at some specific levels. This issue is only valid
1925 * for below a certain battery voltage. This function makes sure that the
1926 * the allowed current limit isn't exceeded.
1928 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1931 struct ab8500_charger *di = container_of(work,
1932 struct ab8500_charger, check_vbat_work.work);
1934 class_for_each_device(power_supply_class, NULL,
1935 di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1937 /* First run old_vbat is 0. */
1938 if (di->old_vbat == 0)
1939 di->old_vbat = di->vbat;
1941 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1942 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1943 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1944 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1946 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1947 " old: %d\n", di->max_usb_in_curr.usb_type_max,
1948 di->vbat, di->old_vbat);
1949 ab8500_charger_set_vbus_in_curr(di,
1950 di->max_usb_in_curr.usb_type_max);
1951 power_supply_changed(di->usb_chg.psy);
1954 di->old_vbat = di->vbat;
1957 * No need to check the battery voltage every second when not close to
1960 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1961 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1964 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1968 * ab8500_charger_check_hw_failure_work() - check main charger failure
1969 * @work: pointer to the work_struct structure
1971 * Work queue function for checking the main charger status
1973 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1978 struct ab8500_charger *di = container_of(work,
1979 struct ab8500_charger, check_hw_failure_work.work);
1981 /* Check if the status bits for HW failure is still active */
1982 if (di->flags.mainextchnotok) {
1983 ret = abx500_get_register_interruptible(di->dev,
1984 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1986 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1989 if (!(reg_value & MAIN_CH_NOK)) {
1990 di->flags.mainextchnotok = false;
1991 ab8500_power_supply_changed(di, di->ac_chg.psy);
1994 if (di->flags.vbus_ovv) {
1995 ret = abx500_get_register_interruptible(di->dev,
1996 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1999 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2002 if (!(reg_value & VBUS_OVV_TH)) {
2003 di->flags.vbus_ovv = false;
2004 ab8500_power_supply_changed(di, di->usb_chg.psy);
2007 /* If we still have a failure, schedule a new check */
2008 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2009 queue_delayed_work(di->charger_wq,
2010 &di->check_hw_failure_work, round_jiffies(HZ));
2015 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2016 * @work: pointer to the work_struct structure
2018 * Work queue function for kicking the charger watchdog.
2020 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2021 * logic. That means we have to continuously kick the charger
2022 * watchdog even when no charger is connected. This is only
2023 * valid once the AC charger has been enabled. This is
2024 * a bug that is not handled by the algorithm and the
2025 * watchdog have to be kicked by the charger driver
2026 * when the AC charger is disabled
2028 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2032 struct ab8500_charger *di = container_of(work,
2033 struct ab8500_charger, kick_wd_work.work);
2035 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2036 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2038 dev_err(di->dev, "Failed to kick WD!\n");
2040 /* Schedule a new watchdog kick */
2041 queue_delayed_work(di->charger_wq,
2042 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2046 * ab8500_charger_ac_work() - work to get and set main charger status
2047 * @work: pointer to the work_struct structure
2049 * Work queue function for checking the main charger status
2051 static void ab8500_charger_ac_work(struct work_struct *work)
2055 struct ab8500_charger *di = container_of(work,
2056 struct ab8500_charger, ac_work);
2059 * Since we can't be sure that the events are received
2060 * synchronously, we have the check if the main charger is
2061 * connected by reading the status register
2063 ret = ab8500_charger_detect_chargers(di, false);
2067 if (ret & AC_PW_CONN) {
2068 di->ac.charger_connected = 1;
2071 di->ac.charger_connected = 0;
2074 ab8500_power_supply_changed(di, di->ac_chg.psy);
2075 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
2078 static void ab8500_charger_usb_attached_work(struct work_struct *work)
2080 struct ab8500_charger *di = container_of(work,
2081 struct ab8500_charger,
2082 usb_charger_attached_work.work);
2083 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2087 for (i = 0; i < 10; i++) {
2088 ret = abx500_get_register_interruptible(di->dev,
2090 AB8500_CH_USBCH_STAT1_REG,
2093 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2096 if ((statval & usbch) != usbch)
2099 msleep(CHARGER_STATUS_POLL);
2102 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2104 mutex_lock(&di->charger_attached_mutex);
2105 mutex_unlock(&di->charger_attached_mutex);
2110 queue_delayed_work(di->charger_wq,
2111 &di->usb_charger_attached_work,
2115 static void ab8500_charger_ac_attached_work(struct work_struct *work)
2118 struct ab8500_charger *di = container_of(work,
2119 struct ab8500_charger,
2120 ac_charger_attached_work.work);
2121 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2122 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2126 for (i = 0; i < 10; i++) {
2127 ret = abx500_get_register_interruptible(di->dev,
2129 AB8500_CH_STATUS2_REG,
2132 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2136 if ((statval & mainch) != mainch)
2139 msleep(CHARGER_STATUS_POLL);
2142 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2143 queue_work(di->charger_wq, &di->ac_work);
2145 mutex_lock(&di->charger_attached_mutex);
2146 mutex_unlock(&di->charger_attached_mutex);
2151 queue_delayed_work(di->charger_wq,
2152 &di->ac_charger_attached_work,
2157 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2158 * @work: Pointer to the work_struct structure
2160 * Detect the type of USB plugged
2162 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
2166 struct ab8500_charger *di = container_of(work,
2167 struct ab8500_charger, detect_usb_type_work);
2170 * Since we can't be sure that the events are received
2171 * synchronously, we have the check if is
2172 * connected by reading the status register
2174 ret = ab8500_charger_detect_chargers(di, false);
2178 if (!(ret & USB_PW_CONN)) {
2179 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2180 di->vbus_detected = false;
2181 ab8500_charger_set_usb_connected(di, false);
2182 ab8500_power_supply_changed(di, di->usb_chg.psy);
2184 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2185 di->vbus_detected = true;
2187 if (is_ab8500_1p1_or_earlier(di->parent)) {
2188 ret = ab8500_charger_detect_usb_type(di);
2190 ab8500_charger_set_usb_connected(di, true);
2191 ab8500_power_supply_changed(di,
2196 * For ABB cut2.0 and onwards we have an IRQ,
2197 * USB_LINK_STATUS that will be triggered when the USB
2198 * link status changes. The exception is USB connected
2199 * during startup. Then we don't get a
2200 * USB_LINK_STATUS IRQ
2202 if (di->vbus_detected_start) {
2203 di->vbus_detected_start = false;
2204 ret = ab8500_charger_detect_usb_type(di);
2206 ab8500_charger_set_usb_connected(di,
2208 ab8500_power_supply_changed(di,
2217 * ab8500_charger_usb_link_attach_work() - work to detect USB type
2218 * @work: pointer to the work_struct structure
2220 * Detect the type of USB plugged
2222 static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2224 struct ab8500_charger *di =
2225 container_of(work, struct ab8500_charger, attach_work.work);
2228 /* Update maximum input current if USB enumeration is not detected */
2229 if (!di->usb.charger_online) {
2230 ret = ab8500_charger_set_vbus_in_curr(di,
2231 di->max_usb_in_curr.usb_type_max);
2236 ab8500_charger_set_usb_connected(di, true);
2237 ab8500_power_supply_changed(di, di->usb_chg.psy);
2241 * ab8500_charger_usb_link_status_work() - work to detect USB type
2242 * @work: pointer to the work_struct structure
2244 * Detect the type of USB plugged
2246 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2248 int detected_chargers;
2253 struct ab8500_charger *di = container_of(work,
2254 struct ab8500_charger, usb_link_status_work);
2257 * Since we can't be sure that the events are received
2258 * synchronously, we have the check if is
2259 * connected by reading the status register
2261 detected_chargers = ab8500_charger_detect_chargers(di, false);
2262 if (detected_chargers < 0)
2266 * Some chargers that breaks the USB spec is
2267 * identified as invalid by AB8500 and it refuse
2268 * to start the charging process. but by jumping
2269 * through a few hoops it can be forced to start.
2271 if (is_ab8500(di->parent))
2272 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2273 AB8500_USB_LINE_STAT_REG, &val);
2275 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2276 AB8500_USB_LINK1_STAT_REG, &val);
2279 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2281 dev_dbg(di->dev, "Error reading USB link status\n");
2283 if (is_ab8500(di->parent))
2284 link_status = AB8500_USB_LINK_STATUS;
2286 link_status = AB8505_USB_LINK_STATUS;
2288 if (detected_chargers & USB_PW_CONN) {
2289 if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
2290 USB_STAT_NOT_VALID_LINK &&
2291 di->invalid_charger_detect_state == 0) {
2293 "Invalid charger detected, state= 0\n");
2295 abx500_mask_and_set_register_interruptible(di->dev,
2296 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
2297 USB_CH_ENA, USB_CH_ENA);
2298 /*Enable charger detection*/
2299 abx500_mask_and_set_register_interruptible(di->dev,
2300 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2301 USB_CH_DET, USB_CH_DET);
2302 di->invalid_charger_detect_state = 1;
2303 /*exit and wait for new link status interrupt.*/
2307 if (di->invalid_charger_detect_state == 1) {
2309 "Invalid charger detected, state= 1\n");
2310 /*Stop charger detection*/
2311 abx500_mask_and_set_register_interruptible(di->dev,
2312 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2314 /*Check link status*/
2315 if (is_ab8500(di->parent))
2316 ret = abx500_get_register_interruptible(di->dev,
2317 AB8500_USB, AB8500_USB_LINE_STAT_REG,
2320 ret = abx500_get_register_interruptible(di->dev,
2321 AB8500_USB, AB8500_USB_LINK1_STAT_REG,
2324 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2325 (val & link_status) >> USB_LINK_STATUS_SHIFT);
2326 di->invalid_charger_detect_state = 2;
2329 di->invalid_charger_detect_state = 0;
2332 if (!(detected_chargers & USB_PW_CONN)) {
2333 di->vbus_detected = false;
2334 ab8500_charger_set_usb_connected(di, false);
2335 ab8500_power_supply_changed(di, di->usb_chg.psy);
2339 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2340 di->vbus_detected = true;
2341 ret = ab8500_charger_read_usb_type(di);
2343 if (ret == -ENXIO) {
2344 /* No valid charger type detected */
2345 ab8500_charger_set_usb_connected(di, false);
2346 ab8500_power_supply_changed(di, di->usb_chg.psy);
2351 if (di->usb_device_is_unrecognised) {
2353 "Potential Legacy Charger device. "
2354 "Delay work for %d msec for USB enum "
2356 WAIT_ACA_RID_ENUMERATION);
2357 queue_delayed_work(di->charger_wq,
2359 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2360 } else if (di->is_aca_rid == 1) {
2361 /* Only wait once */
2364 "%s Wait %d msec for USB enum to finish",
2365 __func__, WAIT_ACA_RID_ENUMERATION);
2366 queue_delayed_work(di->charger_wq,
2368 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2370 queue_delayed_work(di->charger_wq,
2376 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2379 unsigned long flags;
2381 struct ab8500_charger *di = container_of(work,
2382 struct ab8500_charger, usb_state_changed_work.work);
2384 if (!di->vbus_detected) {
2386 "%s !di->vbus_detected\n",
2391 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2392 di->usb_state.state = di->usb_state.state_tmp;
2393 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
2394 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2396 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2397 __func__, di->usb_state.state, di->usb_state.usb_current);
2399 switch (di->usb_state.state) {
2400 case AB8500_BM_USB_STATE_RESET_HS:
2401 case AB8500_BM_USB_STATE_RESET_FS:
2402 case AB8500_BM_USB_STATE_SUSPEND:
2403 case AB8500_BM_USB_STATE_MAX:
2404 ab8500_charger_set_usb_connected(di, false);
2405 ab8500_power_supply_changed(di, di->usb_chg.psy);
2408 case AB8500_BM_USB_STATE_RESUME:
2410 * when suspend->resume there should be delay
2411 * of 1sec for enabling charging
2415 case AB8500_BM_USB_STATE_CONFIGURED:
2417 * USB is configured, enable charging with the charging
2418 * input current obtained from USB driver
2420 if (!ab8500_charger_get_usb_cur(di)) {
2421 /* Update maximum input current */
2422 ret = ab8500_charger_set_vbus_in_curr(di,
2423 di->max_usb_in_curr.usb_type_max);
2427 ab8500_charger_set_usb_connected(di, true);
2428 ab8500_power_supply_changed(di, di->usb_chg.psy);
2438 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2439 * @work: pointer to the work_struct structure
2441 * Work queue function for checking the USB charger Not OK status
2443 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2449 struct ab8500_charger *di = container_of(work,
2450 struct ab8500_charger, check_usbchgnotok_work.work);
2452 /* Check if the status bit for usbchargernotok is still active */
2453 ret = abx500_get_register_interruptible(di->dev,
2454 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2456 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2459 prev_status = di->flags.usbchargernotok;
2461 if (reg_value & VBUS_CH_NOK) {
2462 di->flags.usbchargernotok = true;
2463 /* Check again in 1sec */
2464 queue_delayed_work(di->charger_wq,
2465 &di->check_usbchgnotok_work, HZ);
2467 di->flags.usbchargernotok = false;
2468 di->flags.vbus_collapse = false;
2471 if (prev_status != di->flags.usbchargernotok)
2472 ab8500_power_supply_changed(di, di->usb_chg.psy);
2476 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2477 * @work: pointer to the work_struct structure
2479 * Work queue function for checking the Main thermal prot status
2481 static void ab8500_charger_check_main_thermal_prot_work(
2482 struct work_struct *work)
2487 struct ab8500_charger *di = container_of(work,
2488 struct ab8500_charger, check_main_thermal_prot_work);
2490 /* Check if the status bit for main_thermal_prot is still active */
2491 ret = abx500_get_register_interruptible(di->dev,
2492 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2494 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2497 if (reg_value & MAIN_CH_TH_PROT)
2498 di->flags.main_thermal_prot = true;
2500 di->flags.main_thermal_prot = false;
2502 ab8500_power_supply_changed(di, di->ac_chg.psy);
2506 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2507 * @work: pointer to the work_struct structure
2509 * Work queue function for checking the USB thermal prot status
2511 static void ab8500_charger_check_usb_thermal_prot_work(
2512 struct work_struct *work)
2517 struct ab8500_charger *di = container_of(work,
2518 struct ab8500_charger, check_usb_thermal_prot_work);
2520 /* Check if the status bit for usb_thermal_prot is still active */
2521 ret = abx500_get_register_interruptible(di->dev,
2522 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2524 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2527 if (reg_value & USB_CH_TH_PROT)
2528 di->flags.usb_thermal_prot = true;
2530 di->flags.usb_thermal_prot = false;
2532 ab8500_power_supply_changed(di, di->usb_chg.psy);
2536 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2537 * @irq: interrupt number
2538 * @_di: pointer to the ab8500_charger structure
2540 * Returns IRQ status(IRQ_HANDLED)
2542 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2544 struct ab8500_charger *di = _di;
2546 dev_dbg(di->dev, "Main charger unplugged\n");
2547 queue_work(di->charger_wq, &di->ac_work);
2549 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2550 mutex_lock(&di->charger_attached_mutex);
2551 mutex_unlock(&di->charger_attached_mutex);
2557 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2558 * @irq: interrupt number
2559 * @_di: pointer to the ab8500_charger structure
2561 * Returns IRQ status(IRQ_HANDLED)
2563 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2565 struct ab8500_charger *di = _di;
2567 dev_dbg(di->dev, "Main charger plugged\n");
2568 queue_work(di->charger_wq, &di->ac_work);
2570 mutex_lock(&di->charger_attached_mutex);
2571 mutex_unlock(&di->charger_attached_mutex);
2573 if (is_ab8500(di->parent))
2574 queue_delayed_work(di->charger_wq,
2575 &di->ac_charger_attached_work,
2581 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2582 * @irq: interrupt number
2583 * @_di: pointer to the ab8500_charger structure
2585 * Returns IRQ status(IRQ_HANDLED)
2587 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2589 struct ab8500_charger *di = _di;
2591 dev_dbg(di->dev, "Main charger not ok\n");
2592 di->flags.mainextchnotok = true;
2593 ab8500_power_supply_changed(di, di->ac_chg.psy);
2595 /* Schedule a new HW failure check */
2596 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2602 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2603 * thermal protection threshold
2604 * @irq: interrupt number
2605 * @_di: pointer to the ab8500_charger structure
2607 * Returns IRQ status(IRQ_HANDLED)
2609 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2611 struct ab8500_charger *di = _di;
2614 "Die temp above Main charger thermal protection threshold\n");
2615 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2621 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2622 * thermal protection threshold
2623 * @irq: interrupt number
2624 * @_di: pointer to the ab8500_charger structure
2626 * Returns IRQ status(IRQ_HANDLED)
2628 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2630 struct ab8500_charger *di = _di;
2633 "Die temp ok for Main charger thermal protection threshold\n");
2634 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2639 static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2641 struct ab8500_charger *di = container_of(work,
2642 struct ab8500_charger, vbus_drop_end_work.work);
2646 di->flags.vbus_drop_end = false;
2648 /* Reset the drop counter */
2649 abx500_set_register_interruptible(di->dev,
2650 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
2652 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2653 AB8500_CH_USBCH_STAT2_REG, ®_value);
2655 dev_err(di->dev, "%s read failed\n", __func__);
2659 curr = di->bm->chg_input_curr[
2660 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
2662 if (di->max_usb_in_curr.calculated_max != curr) {
2663 /* USB source is collapsing */
2664 di->max_usb_in_curr.calculated_max = curr;
2666 "VBUS input current limiting to %d mA\n",
2667 di->max_usb_in_curr.calculated_max);
2670 * USB source can not give more than this amount.
2671 * Taking more will collapse the source.
2673 di->max_usb_in_curr.set_max =
2674 di->max_usb_in_curr.calculated_max;
2676 "VBUS input current limited to %d mA\n",
2677 di->max_usb_in_curr.set_max);
2680 if (di->usb.charger_connected)
2681 ab8500_charger_set_vbus_in_curr(di,
2682 di->max_usb_in_curr.usb_type_max);
2686 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2687 * @irq: interrupt number
2688 * @_di: pointer to the ab8500_charger structure
2690 * Returns IRQ status(IRQ_HANDLED)
2692 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2694 struct ab8500_charger *di = _di;
2696 di->vbus_detected = false;
2697 dev_dbg(di->dev, "VBUS falling detected\n");
2698 queue_work(di->charger_wq, &di->detect_usb_type_work);
2704 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2705 * @irq: interrupt number
2706 * @_di: pointer to the ab8500_charger structure
2708 * Returns IRQ status(IRQ_HANDLED)
2710 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2712 struct ab8500_charger *di = _di;
2714 di->vbus_detected = true;
2715 dev_dbg(di->dev, "VBUS rising detected\n");
2717 queue_work(di->charger_wq, &di->detect_usb_type_work);
2723 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2724 * @irq: interrupt number
2725 * @_di: pointer to the ab8500_charger structure
2727 * Returns IRQ status(IRQ_HANDLED)
2729 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2731 struct ab8500_charger *di = _di;
2733 dev_dbg(di->dev, "USB link status changed\n");
2735 queue_work(di->charger_wq, &di->usb_link_status_work);
2741 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2742 * thermal protection threshold
2743 * @irq: interrupt number
2744 * @_di: pointer to the ab8500_charger structure
2746 * Returns IRQ status(IRQ_HANDLED)
2748 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2750 struct ab8500_charger *di = _di;
2753 "Die temp above USB charger thermal protection threshold\n");
2754 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2760 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2761 * thermal protection threshold
2762 * @irq: interrupt number
2763 * @_di: pointer to the ab8500_charger structure
2765 * Returns IRQ status(IRQ_HANDLED)
2767 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2769 struct ab8500_charger *di = _di;
2772 "Die temp ok for USB charger thermal protection threshold\n");
2773 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2779 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2780 * @irq: interrupt number
2781 * @_di: pointer to the ab8500_charger structure
2783 * Returns IRQ status(IRQ_HANDLED)
2785 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2787 struct ab8500_charger *di = _di;
2789 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2790 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2796 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2797 * @irq: interrupt number
2798 * @_di: pointer to the ab8500_charger structure
2800 * Returns IRQ status(IRQ_HANDLED)
2802 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2804 struct ab8500_charger *di = _di;
2806 dev_dbg(di->dev, "Charger watchdog expired\n");
2809 * The charger that was online when the watchdog expired
2810 * needs to be restarted for charging to start again
2812 if (di->ac.charger_online) {
2813 di->ac.wd_expired = true;
2814 ab8500_power_supply_changed(di, di->ac_chg.psy);
2816 if (di->usb.charger_online) {
2817 di->usb.wd_expired = true;
2818 ab8500_power_supply_changed(di, di->usb_chg.psy);
2825 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2826 * @irq: interrupt number
2827 * @_di: pointer to the ab8500_charger structure
2829 * Returns IRQ status(IRQ_HANDLED)
2831 static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2833 struct ab8500_charger *di = _di;
2835 dev_dbg(di->dev, "VBUS charger drop ended\n");
2836 di->flags.vbus_drop_end = true;
2839 * VBUS might have dropped due to bad connection.
2840 * Schedule a new input limit set to the value SW requests.
2842 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
2843 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
2849 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2850 * @irq: interrupt number
2851 * @_di: pointer to the ab8500_charger structure
2853 * Returns IRQ status(IRQ_HANDLED)
2855 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2857 struct ab8500_charger *di = _di;
2859 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2860 di->flags.vbus_ovv = true;
2861 ab8500_power_supply_changed(di, di->usb_chg.psy);
2863 /* Schedule a new HW failure check */
2864 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2870 * ab8500_charger_ac_get_property() - get the ac/mains properties
2871 * @psy: pointer to the power_supply structure
2872 * @psp: pointer to the power_supply_property structure
2873 * @val: pointer to the power_supply_propval union
2875 * This function gets called when an application tries to get the ac/mains
2876 * properties by reading the sysfs files.
2877 * AC/Mains properties are online, present and voltage.
2878 * online: ac/mains charging is in progress or not
2879 * present: presence of the ac/mains
2880 * voltage: AC/Mains voltage
2881 * Returns error code in case of failure else 0(on success)
2883 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2884 enum power_supply_property psp,
2885 union power_supply_propval *val)
2887 struct ab8500_charger *di;
2890 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2893 case POWER_SUPPLY_PROP_HEALTH:
2894 if (di->flags.mainextchnotok)
2895 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2896 else if (di->ac.wd_expired || di->usb.wd_expired)
2897 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2898 else if (di->flags.main_thermal_prot)
2899 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2901 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2903 case POWER_SUPPLY_PROP_ONLINE:
2904 val->intval = di->ac.charger_online;
2906 case POWER_SUPPLY_PROP_PRESENT:
2907 val->intval = di->ac.charger_connected;
2909 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2910 ret = ab8500_charger_get_ac_voltage(di);
2912 di->ac.charger_voltage = ret;
2913 /* On error, use previous value */
2914 val->intval = di->ac.charger_voltage * 1000;
2916 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2918 * This property is used to indicate when CV mode is entered
2919 * for the AC charger
2921 di->ac.cv_active = ab8500_charger_ac_cv(di);
2922 val->intval = di->ac.cv_active;
2924 case POWER_SUPPLY_PROP_CURRENT_NOW:
2925 ret = ab8500_charger_get_ac_current(di);
2927 di->ac.charger_current = ret;
2928 val->intval = di->ac.charger_current * 1000;
2937 * ab8500_charger_usb_get_property() - get the usb properties
2938 * @psy: pointer to the power_supply structure
2939 * @psp: pointer to the power_supply_property structure
2940 * @val: pointer to the power_supply_propval union
2942 * This function gets called when an application tries to get the usb
2943 * properties by reading the sysfs files.
2944 * USB properties are online, present and voltage.
2945 * online: usb charging is in progress or not
2946 * present: presence of the usb
2947 * voltage: vbus voltage
2948 * Returns error code in case of failure else 0(on success)
2950 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2951 enum power_supply_property psp,
2952 union power_supply_propval *val)
2954 struct ab8500_charger *di;
2957 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2960 case POWER_SUPPLY_PROP_HEALTH:
2961 if (di->flags.usbchargernotok)
2962 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2963 else if (di->ac.wd_expired || di->usb.wd_expired)
2964 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2965 else if (di->flags.usb_thermal_prot)
2966 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2967 else if (di->flags.vbus_ovv)
2968 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2970 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2972 case POWER_SUPPLY_PROP_ONLINE:
2973 val->intval = di->usb.charger_online;
2975 case POWER_SUPPLY_PROP_PRESENT:
2976 val->intval = di->usb.charger_connected;
2978 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2979 ret = ab8500_charger_get_vbus_voltage(di);
2981 di->usb.charger_voltage = ret;
2982 val->intval = di->usb.charger_voltage * 1000;
2984 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2986 * This property is used to indicate when CV mode is entered
2987 * for the USB charger
2989 di->usb.cv_active = ab8500_charger_usb_cv(di);
2990 val->intval = di->usb.cv_active;
2992 case POWER_SUPPLY_PROP_CURRENT_NOW:
2993 ret = ab8500_charger_get_usb_current(di);
2995 di->usb.charger_current = ret;
2996 val->intval = di->usb.charger_current * 1000;
2998 case POWER_SUPPLY_PROP_CURRENT_AVG:
3000 * This property is used to indicate when VBUS has collapsed
3001 * due to too high output current from the USB charger
3003 if (di->flags.vbus_collapse)
3015 * ab8500_charger_init_hw_registers() - Set up charger related registers
3016 * @di: pointer to the ab8500_charger structure
3018 * Set up charger OVV, watchdog and maximum voltage registers as well as
3019 * charging of the backup battery
3021 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3025 /* Setup maximum charger current and voltage for ABB cut2.0 */
3026 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3027 ret = abx500_set_register_interruptible(di->dev,
3029 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3032 "failed to set CH_VOLT_LVL_MAX_REG\n");
3036 ret = abx500_set_register_interruptible(di->dev,
3037 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3041 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3046 if (is_ab8505_2p0(di->parent))
3047 ret = abx500_mask_and_set_register_interruptible(di->dev,
3049 AB8500_USBCH_CTRL2_REG,
3050 VBUS_AUTO_IN_CURR_LIM_ENA,
3051 VBUS_AUTO_IN_CURR_LIM_ENA);
3054 * VBUS OVV set to 6.3V and enable automatic current limitation
3056 ret = abx500_set_register_interruptible(di->dev,
3058 AB8500_USBCH_CTRL2_REG,
3059 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
3062 "failed to set automatic current limitation\n");
3066 /* Enable main watchdog in OTP */
3067 ret = abx500_set_register_interruptible(di->dev,
3068 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3070 dev_err(di->dev, "failed to enable main WD in OTP\n");
3074 /* Enable main watchdog */
3075 ret = abx500_set_register_interruptible(di->dev,
3076 AB8500_SYS_CTRL2_BLOCK,
3077 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3079 dev_err(di->dev, "failed to enable main watchdog\n");
3084 * Due to internal synchronisation, Enable and Kick watchdog bits
3085 * cannot be enabled in a single write.
3086 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3087 * between writing Enable then Kick bits.
3091 /* Kick main watchdog */
3092 ret = abx500_set_register_interruptible(di->dev,
3093 AB8500_SYS_CTRL2_BLOCK,
3094 AB8500_MAIN_WDOG_CTRL_REG,
3095 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3097 dev_err(di->dev, "failed to kick main watchdog\n");
3101 /* Disable main watchdog */
3102 ret = abx500_set_register_interruptible(di->dev,
3103 AB8500_SYS_CTRL2_BLOCK,
3104 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3106 dev_err(di->dev, "failed to disable main watchdog\n");
3110 /* Set watchdog timeout */
3111 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3112 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3114 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3118 ret = ab8500_charger_led_en(di, false);
3120 dev_err(di->dev, "failed to disable LED\n");
3124 ret = abx500_set_register_interruptible(di->dev,
3126 AB8500_RTC_BACKUP_CHG_REG,
3127 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
3129 dev_err(di->dev, "failed to setup backup battery charging\n");
3133 /* Enable backup battery charging */
3134 ret = abx500_mask_and_set_register_interruptible(di->dev,
3135 AB8500_RTC, AB8500_RTC_CTRL_REG,
3136 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3138 dev_err(di->dev, "%s mask and set failed\n", __func__);
3147 * ab8500 charger driver interrupts and their respective isr
3149 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3150 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3151 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3152 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3153 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3154 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3155 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3156 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3157 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3158 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3159 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3160 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3161 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3162 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
3163 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
3166 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3167 unsigned long event, void *power)
3169 struct ab8500_charger *di =
3170 container_of(nb, struct ab8500_charger, nb);
3171 enum ab8500_usb_state bm_usb_state;
3172 unsigned mA = *((unsigned *)power);
3177 if (event != USB_EVENT_VBUS) {
3178 dev_dbg(di->dev, "not a standard host, returning\n");
3182 /* TODO: State is fabricate here. See if charger really needs USB
3183 * state or if mA is enough
3185 if ((di->usb_state.usb_current == 2) && (mA > 2))
3186 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3188 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3190 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3191 else if (mA >= 8) /* 8, 100, 500 */
3192 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3193 else /* Should never occur */
3194 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3196 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3197 __func__, bm_usb_state, mA);
3199 spin_lock(&di->usb_state.usb_lock);
3200 di->usb_state.state_tmp = bm_usb_state;
3201 di->usb_state.usb_current_tmp = mA;
3202 spin_unlock(&di->usb_state.usb_lock);
3205 * wait for some time until you get updates from the usb stack
3206 * and negotiations are completed
3208 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
3213 static int __maybe_unused ab8500_charger_resume(struct device *dev)
3216 struct ab8500_charger *di = dev_get_drvdata(dev);
3219 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3220 * logic. That means we have to continuously kick the charger
3221 * watchdog even when no charger is connected. This is only
3222 * valid once the AC charger has been enabled. This is
3223 * a bug that is not handled by the algorithm and the
3224 * watchdog have to be kicked by the charger driver
3225 * when the AC charger is disabled
3227 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3228 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3229 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3231 dev_err(di->dev, "Failed to kick WD!\n");
3233 /* If not already pending start a new timer */
3234 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3235 round_jiffies(WD_KICK_INTERVAL));
3238 /* If we still have a HW failure, schedule a new check */
3239 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3240 queue_delayed_work(di->charger_wq,
3241 &di->check_hw_failure_work, 0);
3244 if (di->flags.vbus_drop_end)
3245 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3250 static int __maybe_unused ab8500_charger_suspend(struct device *dev)
3252 struct ab8500_charger *di = dev_get_drvdata(dev);
3254 /* Cancel any pending jobs */
3255 cancel_delayed_work(&di->check_hw_failure_work);
3256 cancel_delayed_work(&di->vbus_drop_end_work);
3258 flush_delayed_work(&di->attach_work);
3259 flush_delayed_work(&di->usb_charger_attached_work);
3260 flush_delayed_work(&di->ac_charger_attached_work);
3261 flush_delayed_work(&di->check_usbchgnotok_work);
3262 flush_delayed_work(&di->check_vbat_work);
3263 flush_delayed_work(&di->kick_wd_work);
3265 flush_work(&di->usb_link_status_work);
3266 flush_work(&di->ac_work);
3267 flush_work(&di->detect_usb_type_work);
3269 if (atomic_read(&di->current_stepping_sessions))
3275 static struct notifier_block charger_nb = {
3276 .notifier_call = ab8500_external_charger_prepare,
3279 static int ab8500_charger_remove(struct platform_device *pdev)
3281 struct ab8500_charger *di = platform_get_drvdata(pdev);
3284 /* Disable AC charging */
3285 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3287 /* Disable USB charging */
3288 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3290 /* Disable interrupts */
3291 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3292 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3296 /* Backup battery voltage and current disable */
3297 ret = abx500_mask_and_set_register_interruptible(di->dev,
3298 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3300 dev_err(di->dev, "%s mask and set failed\n", __func__);
3302 usb_unregister_notifier(di->usb_phy, &di->nb);
3303 usb_put_phy(di->usb_phy);
3305 /* Delete the work queue */
3306 destroy_workqueue(di->charger_wq);
3308 /* Unregister external charger enable notifier */
3309 if (!di->ac_chg.enabled)
3310 blocking_notifier_chain_unregister(
3311 &charger_notifier_list, &charger_nb);
3313 flush_scheduled_work();
3314 if (di->usb_chg.enabled)
3315 power_supply_unregister(di->usb_chg.psy);
3317 if (di->ac_chg.enabled && !di->ac_chg.external)
3318 power_supply_unregister(di->ac_chg.psy);
3323 static char *supply_interface[] = {
3329 static const struct power_supply_desc ab8500_ac_chg_desc = {
3330 .name = "ab8500_ac",
3331 .type = POWER_SUPPLY_TYPE_MAINS,
3332 .properties = ab8500_charger_ac_props,
3333 .num_properties = ARRAY_SIZE(ab8500_charger_ac_props),
3334 .get_property = ab8500_charger_ac_get_property,
3337 static const struct power_supply_desc ab8500_usb_chg_desc = {
3338 .name = "ab8500_usb",
3339 .type = POWER_SUPPLY_TYPE_USB,
3340 .properties = ab8500_charger_usb_props,
3341 .num_properties = ARRAY_SIZE(ab8500_charger_usb_props),
3342 .get_property = ab8500_charger_usb_get_property,
3345 static int ab8500_charger_probe(struct platform_device *pdev)
3347 struct device_node *np = pdev->dev.of_node;
3348 struct power_supply_config ac_psy_cfg = {}, usb_psy_cfg = {};
3349 struct ab8500_charger *di;
3350 int irq, i, charger_status, ret = 0, ch_stat;
3351 struct device *dev = &pdev->dev;
3353 di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
3357 di->bm = &ab8500_bm_data;
3359 ret = ab8500_bm_of_probe(dev, np, di->bm);
3361 dev_err(dev, "failed to get battery information\n");
3364 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3366 /* get parent data */
3368 di->parent = dev_get_drvdata(pdev->dev.parent);
3370 /* Get ADC channels */
3371 di->adc_main_charger_v = devm_iio_channel_get(dev, "main_charger_v");
3372 if (IS_ERR(di->adc_main_charger_v)) {
3373 ret = dev_err_probe(dev, PTR_ERR(di->adc_main_charger_v),
3374 "failed to get ADC main charger voltage\n");
3377 di->adc_main_charger_c = devm_iio_channel_get(dev, "main_charger_c");
3378 if (IS_ERR(di->adc_main_charger_c)) {
3379 ret = dev_err_probe(dev, PTR_ERR(di->adc_main_charger_c),
3380 "failed to get ADC main charger current\n");
3383 di->adc_vbus_v = devm_iio_channel_get(dev, "vbus_v");
3384 if (IS_ERR(di->adc_vbus_v)) {
3385 ret = dev_err_probe(dev, PTR_ERR(di->adc_vbus_v),
3386 "failed to get ADC USB charger voltage\n");
3389 di->adc_usb_charger_c = devm_iio_channel_get(dev, "usb_charger_c");
3390 if (IS_ERR(di->adc_usb_charger_c)) {
3391 ret = dev_err_probe(dev, PTR_ERR(di->adc_usb_charger_c),
3392 "failed to get ADC USB charger current\n");
3396 /* initialize lock */
3397 spin_lock_init(&di->usb_state.usb_lock);
3398 mutex_init(&di->usb_ipt_crnt_lock);
3400 di->autopower = false;
3401 di->invalid_charger_detect_state = 0;
3403 /* AC and USB supply config */
3404 ac_psy_cfg.supplied_to = supply_interface;
3405 ac_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3406 ac_psy_cfg.drv_data = &di->ac_chg;
3407 usb_psy_cfg.supplied_to = supply_interface;
3408 usb_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3409 usb_psy_cfg.drv_data = &di->usb_chg;
3412 /* ux500_charger sub-class */
3413 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
3414 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
3415 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3416 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3417 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3418 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3419 di->ac_chg.max_out_curr =
3420 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3421 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
3422 di->ac_chg.enabled = di->bm->ac_enabled;
3423 di->ac_chg.external = false;
3425 /*notifier for external charger enabling*/
3426 if (!di->ac_chg.enabled)
3427 blocking_notifier_chain_register(
3428 &charger_notifier_list, &charger_nb);
3431 /* ux500_charger sub-class */
3432 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
3433 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
3434 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3435 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3436 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3437 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3438 di->usb_chg.max_out_curr =
3439 di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3440 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
3441 di->usb_chg.enabled = di->bm->usb_enabled;
3442 di->usb_chg.external = false;
3443 di->usb_state.usb_current = -1;
3445 /* Create a work queue for the charger */
3446 di->charger_wq = alloc_ordered_workqueue("ab8500_charger_wq",
3448 if (di->charger_wq == NULL) {
3449 dev_err(dev, "failed to create work queue\n");
3453 mutex_init(&di->charger_attached_mutex);
3455 /* Init work for HW failure check */
3456 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
3457 ab8500_charger_check_hw_failure_work);
3458 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
3459 ab8500_charger_check_usbchargernotok_work);
3461 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3462 ab8500_charger_ac_attached_work);
3463 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3464 ab8500_charger_usb_attached_work);
3467 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3468 * logic. That means we have to continuously kick the charger
3469 * watchdog even when no charger is connected. This is only
3470 * valid once the AC charger has been enabled. This is
3471 * a bug that is not handled by the algorithm and the
3472 * watchdog have to be kicked by the charger driver
3473 * when the AC charger is disabled
3475 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
3476 ab8500_charger_kick_watchdog_work);
3478 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
3479 ab8500_charger_check_vbat_work);
3481 INIT_DELAYED_WORK(&di->attach_work,
3482 ab8500_charger_usb_link_attach_work);
3484 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3485 ab8500_charger_usb_state_changed_work);
3487 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3488 ab8500_charger_vbus_drop_end_work);
3490 /* Init work for charger detection */
3491 INIT_WORK(&di->usb_link_status_work,
3492 ab8500_charger_usb_link_status_work);
3493 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3494 INIT_WORK(&di->detect_usb_type_work,
3495 ab8500_charger_detect_usb_type_work);
3497 /* Init work for checking HW status */
3498 INIT_WORK(&di->check_main_thermal_prot_work,
3499 ab8500_charger_check_main_thermal_prot_work);
3500 INIT_WORK(&di->check_usb_thermal_prot_work,
3501 ab8500_charger_check_usb_thermal_prot_work);
3504 * VDD ADC supply needs to be enabled from this driver when there
3505 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3506 * interrupts during charging
3508 di->regu = devm_regulator_get(dev, "vddadc");
3509 if (IS_ERR(di->regu)) {
3510 ret = PTR_ERR(di->regu);
3511 dev_err(dev, "failed to get vddadc regulator\n");
3512 goto free_charger_wq;
3516 /* Initialize OVV, and other registers */
3517 ret = ab8500_charger_init_hw_registers(di);
3519 dev_err(dev, "failed to initialize ABB registers\n");
3520 goto free_charger_wq;
3523 /* Register AC charger class */
3524 if (di->ac_chg.enabled) {
3525 di->ac_chg.psy = power_supply_register(dev,
3526 &ab8500_ac_chg_desc,
3528 if (IS_ERR(di->ac_chg.psy)) {
3529 dev_err(dev, "failed to register AC charger\n");
3530 ret = PTR_ERR(di->ac_chg.psy);
3531 goto free_charger_wq;
3535 /* Register USB charger class */
3536 if (di->usb_chg.enabled) {
3537 di->usb_chg.psy = power_supply_register(dev,
3538 &ab8500_usb_chg_desc,
3540 if (IS_ERR(di->usb_chg.psy)) {
3541 dev_err(dev, "failed to register USB charger\n");
3542 ret = PTR_ERR(di->usb_chg.psy);
3547 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3548 if (IS_ERR_OR_NULL(di->usb_phy)) {
3549 dev_err(dev, "failed to get usb transceiver\n");
3553 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3554 ret = usb_register_notifier(di->usb_phy, &di->nb);
3556 dev_err(dev, "failed to register usb notifier\n");
3560 /* Identify the connected charger types during startup */
3561 charger_status = ab8500_charger_detect_chargers(di, true);
3562 if (charger_status & AC_PW_CONN) {
3563 di->ac.charger_connected = 1;
3565 ab8500_power_supply_changed(di, di->ac_chg.psy);
3566 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
3569 if (charger_status & USB_PW_CONN) {
3570 di->vbus_detected = true;
3571 di->vbus_detected_start = true;
3572 queue_work(di->charger_wq,
3573 &di->detect_usb_type_work);
3576 /* Register interrupts */
3577 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3578 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3584 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3585 IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
3586 ab8500_charger_irq[i].name, di);
3589 dev_err(dev, "failed to request %s IRQ %d: %d\n"
3590 , ab8500_charger_irq[i].name, irq, ret);
3593 dev_dbg(dev, "Requested %s IRQ %d: %d\n",
3594 ab8500_charger_irq[i].name, irq, ret);
3597 platform_set_drvdata(pdev, di);
3599 mutex_lock(&di->charger_attached_mutex);
3601 ch_stat = ab8500_charger_detect_chargers(di, false);
3603 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3604 if (is_ab8500(di->parent))
3605 queue_delayed_work(di->charger_wq,
3606 &di->ac_charger_attached_work,
3609 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3610 if (is_ab8500(di->parent))
3611 queue_delayed_work(di->charger_wq,
3612 &di->usb_charger_attached_work,
3616 mutex_unlock(&di->charger_attached_mutex);
3621 usb_unregister_notifier(di->usb_phy, &di->nb);
3623 /* We also have to free all successfully registered irqs */
3624 for (i = i - 1; i >= 0; i--) {
3625 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3629 usb_put_phy(di->usb_phy);
3631 if (di->usb_chg.enabled)
3632 power_supply_unregister(di->usb_chg.psy);
3634 if (di->ac_chg.enabled)
3635 power_supply_unregister(di->ac_chg.psy);
3637 destroy_workqueue(di->charger_wq);
3641 static SIMPLE_DEV_PM_OPS(ab8500_charger_pm_ops, ab8500_charger_suspend, ab8500_charger_resume);
3643 static const struct of_device_id ab8500_charger_match[] = {
3644 { .compatible = "stericsson,ab8500-charger", },
3648 static struct platform_driver ab8500_charger_driver = {
3649 .probe = ab8500_charger_probe,
3650 .remove = ab8500_charger_remove,
3652 .name = "ab8500-charger",
3653 .of_match_table = ab8500_charger_match,
3654 .pm = &ab8500_charger_pm_ops,
3658 static int __init ab8500_charger_init(void)
3660 return platform_driver_register(&ab8500_charger_driver);
3663 static void __exit ab8500_charger_exit(void)
3665 platform_driver_unregister(&ab8500_charger_driver);
3668 subsys_initcall_sync(ab8500_charger_init);
3669 module_exit(ab8500_charger_exit);
3671 MODULE_LICENSE("GPL v2");
3672 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3673 MODULE_ALIAS("platform:ab8500-charger");
3674 MODULE_DESCRIPTION("AB8500 charger management driver");