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/component.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/notifier.h>
20 #include <linux/slab.h>
21 #include <linux/platform_device.h>
22 #include <linux/power_supply.h>
23 #include <linux/completion.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/err.h>
26 #include <linux/workqueue.h>
27 #include <linux/kobject.h>
29 #include <linux/mfd/core.h>
30 #include <linux/mfd/abx500/ab8500.h>
31 #include <linux/mfd/abx500.h>
32 #include <linux/usb/otg.h>
33 #include <linux/mutex.h>
34 #include <linux/iio/consumer.h>
36 #include "ab8500-bm.h"
37 #include "ab8500-chargalg.h"
39 /* Charger constants */
44 #define MAIN_WDOG_ENA 0x01
45 #define MAIN_WDOG_KICK 0x02
46 #define MAIN_WDOG_DIS 0x00
47 #define CHARG_WD_KICK 0x01
48 #define MAIN_CH_ENA 0x01
49 #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
50 #define USB_CH_ENA 0x01
51 #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
52 #define MAIN_CH_DET 0x01
53 #define MAIN_CH_CV_ON 0x04
54 #define USB_CH_CV_ON 0x08
55 #define VBUS_DET_DBNC100 0x02
56 #define VBUS_DET_DBNC1 0x01
57 #define OTP_ENABLE_WD 0x01
58 #define DROP_COUNT_RESET 0x01
59 #define USB_CH_DET 0x01
61 #define MAIN_CH_INPUT_CURR_SHIFT 4
62 #define VBUS_IN_CURR_LIM_SHIFT 4
63 #define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
64 #define VBUS_IN_CURR_LIM_RETRY_SET_TIME 30 /* seconds */
66 #define LED_INDICATOR_PWM_ENA 0x01
67 #define LED_INDICATOR_PWM_DIS 0x00
68 #define LED_IND_CUR_5MA 0x04
69 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
71 /* HW failure constants */
72 #define MAIN_CH_TH_PROT 0x02
73 #define VBUS_CH_NOK 0x08
74 #define USB_CH_TH_PROT 0x02
75 #define VBUS_OVV_TH 0x01
76 #define MAIN_CH_NOK 0x01
79 #define MAIN_CH_STATUS2_MAINCHGDROP 0x80
80 #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
81 #define USB_CH_VBUSDROP 0x40
82 #define USB_CH_VBUSDETDBNC 0x01
84 /* UsbLineStatus register bit masks */
85 #define AB8500_USB_LINK_STATUS 0x78
86 #define AB8505_USB_LINK_STATUS 0xF8
87 #define AB8500_STD_HOST_SUSP 0x18
88 #define USB_LINK_STATUS_SHIFT 3
90 /* Watchdog timeout constant */
91 #define WD_TIMER 0x30 /* 4min */
92 #define WD_KICK_INTERVAL (60 * HZ)
94 /* Lowest charger voltage is 3.39V -> 0x4E */
95 #define LOW_VOLT_REG 0x4E
97 /* Step up/down delay in us */
98 #define STEP_UDELAY 1000
100 #define CHARGER_STATUS_POLL 10 /* in ms */
102 #define CHG_WD_INTERVAL (60 * HZ)
104 #define AB8500_SW_CONTROL_FALLBACK 0x03
105 /* Wait for enumeration before charing in us */
106 #define WAIT_ACA_RID_ENUMERATION (5 * 1000)
107 /*External charger control*/
108 #define AB8500_SYS_CHARGER_CONTROL_REG 0x52
109 #define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
110 #define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
112 /* UsbLineStatus register - usb types */
113 enum ab8500_charger_link_status {
114 USB_STAT_NOT_CONFIGURED,
115 USB_STAT_STD_HOST_NC,
116 USB_STAT_STD_HOST_C_NS,
117 USB_STAT_STD_HOST_C_S,
118 USB_STAT_HOST_CHG_NM,
119 USB_STAT_HOST_CHG_HS,
120 USB_STAT_HOST_CHG_HS_CHIRP,
121 USB_STAT_DEDICATED_CHG,
124 USB_STAT_ACA_RID_C_NM,
125 USB_STAT_ACA_RID_C_HS,
126 USB_STAT_ACA_RID_C_HS_CHIRP,
129 USB_STAT_NOT_VALID_LINK,
131 USB_STAT_SUP_NO_IDGND_VBUS,
132 USB_STAT_SUP_IDGND_VBUS,
133 USB_STAT_CHARGER_LINE_1,
136 USB_STAT_ACA_DOCK_CHARGER,
139 enum ab8500_usb_state {
140 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
141 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
142 AB8500_BM_USB_STATE_CONFIGURED,
143 AB8500_BM_USB_STATE_SUSPEND,
144 AB8500_BM_USB_STATE_RESUME,
145 AB8500_BM_USB_STATE_MAX,
148 /* VBUS input current limits supported in AB8500 in uA */
149 #define USB_CH_IP_CUR_LVL_0P05 50000
150 #define USB_CH_IP_CUR_LVL_0P09 98000
151 #define USB_CH_IP_CUR_LVL_0P19 193000
152 #define USB_CH_IP_CUR_LVL_0P29 290000
153 #define USB_CH_IP_CUR_LVL_0P38 380000
154 #define USB_CH_IP_CUR_LVL_0P45 450000
155 #define USB_CH_IP_CUR_LVL_0P5 500000
156 #define USB_CH_IP_CUR_LVL_0P6 600000
157 #define USB_CH_IP_CUR_LVL_0P7 700000
158 #define USB_CH_IP_CUR_LVL_0P8 800000
159 #define USB_CH_IP_CUR_LVL_0P9 900000
160 #define USB_CH_IP_CUR_LVL_1P0 1000000
161 #define USB_CH_IP_CUR_LVL_1P1 1100000
162 #define USB_CH_IP_CUR_LVL_1P3 1300000
163 #define USB_CH_IP_CUR_LVL_1P4 1400000
164 #define USB_CH_IP_CUR_LVL_1P5 1500000
166 #define VBAT_TRESH_IP_CUR_RED 3800000
168 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
169 struct ab8500_charger, usb_chg)
170 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
171 struct ab8500_charger, ac_chg)
174 * struct ab8500_charger_interrupts - ab8500 interrupts
175 * @name: name of the interrupt
176 * @isr function pointer to the isr
178 struct ab8500_charger_interrupts {
180 irqreturn_t (*isr)(int irq, void *data);
183 struct ab8500_charger_info {
184 int charger_connected;
186 int charger_voltage_uv;
189 int charger_current_ua;
192 struct ab8500_charger_event_flags {
194 bool main_thermal_prot;
195 bool usb_thermal_prot;
197 bool usbchargernotok;
203 struct ab8500_charger_usb_state {
205 int usb_current_tmp_ua;
206 enum ab8500_usb_state state;
207 enum ab8500_usb_state state_tmp;
211 struct ab8500_charger_max_usb_in_curr {
214 int calculated_max_ua;
218 * struct ab8500_charger - ab8500 Charger device information
219 * @dev: Pointer to the structure device
220 * @vbus_detected: VBUS detected
221 * @vbus_detected_start:
222 * VBUS detected during startup
223 * @ac_conn: This will be true when the AC charger has been plugged
224 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
226 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
228 * @vbat Battery voltage
229 * @old_vbat Previously measured battery voltage
230 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
231 * @autopower Indicate if we should have automatic pwron after pwrloss
232 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
233 * @invalid_charger_detect_state State when forcing AB to use invalid charger
234 * @is_aca_rid: Incicate if accessory is ACA type
235 * @current_stepping_sessions:
236 * Counter for current stepping sessions
237 * @parent: Pointer to the struct ab8500
238 * @adc_main_charger_v ADC channel for main charger voltage
239 * @adc_main_charger_c ADC channel for main charger current
240 * @adc_vbus_v ADC channel for USB charger voltage
241 * @adc_usb_charger_c ADC channel for USB charger current
242 * @bm: Platform specific battery management information
243 * @flags: Structure for information about events triggered
244 * @usb_state: Structure for usb stack information
245 * @max_usb_in_curr: Max USB charger input current
246 * @ac_chg: AC charger power supply
247 * @usb_chg: USB charger power supply
248 * @ac: Structure that holds the AC charger properties
249 * @usb: Structure that holds the USB charger properties
250 * @regu: Pointer to the struct regulator
251 * @charger_wq: Work queue for the IRQs and checking HW state
252 * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
253 * @pm_lock: Lock to prevent system to suspend
254 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
255 * @check_hw_failure_work: Work for checking HW state
256 * @check_usbchgnotok_work: Work for checking USB charger not ok status
257 * @kick_wd_work: Work for kicking the charger watchdog in case
258 * of ABB rev 1.* due to the watchog logic bug
259 * @ac_charger_attached_work: Work for checking if AC charger is still
261 * @usb_charger_attached_work: Work for checking if USB charger is still
263 * @ac_work: Work for checking AC charger connection
264 * @detect_usb_type_work: Work for detecting the USB type connected
265 * @usb_link_status_work: Work for checking the new USB link status
266 * @usb_state_changed_work: Work for checking USB state
267 * @attach_work: Work for detecting USB type
268 * @vbus_drop_end_work: Work for detecting VBUS drop end
269 * @check_main_thermal_prot_work:
270 * Work for checking Main thermal status
271 * @check_usb_thermal_prot_work:
272 * Work for checking USB thermal status
273 * @charger_attached_mutex: For controlling the wakelock
275 struct ab8500_charger {
278 bool vbus_detected_start;
284 bool usb_device_is_unrecognised;
287 int invalid_charger_detect_state;
289 atomic_t current_stepping_sessions;
290 struct ab8500 *parent;
291 struct iio_channel *adc_main_charger_v;
292 struct iio_channel *adc_main_charger_c;
293 struct iio_channel *adc_vbus_v;
294 struct iio_channel *adc_usb_charger_c;
295 struct ab8500_bm_data *bm;
296 struct ab8500_charger_event_flags flags;
297 struct ab8500_charger_usb_state usb_state;
298 struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
299 struct ux500_charger ac_chg;
300 struct ux500_charger usb_chg;
301 struct ab8500_charger_info ac;
302 struct ab8500_charger_info usb;
303 struct regulator *regu;
304 struct workqueue_struct *charger_wq;
305 struct mutex usb_ipt_crnt_lock;
306 struct delayed_work check_vbat_work;
307 struct delayed_work check_hw_failure_work;
308 struct delayed_work check_usbchgnotok_work;
309 struct delayed_work kick_wd_work;
310 struct delayed_work usb_state_changed_work;
311 struct delayed_work attach_work;
312 struct delayed_work ac_charger_attached_work;
313 struct delayed_work usb_charger_attached_work;
314 struct delayed_work vbus_drop_end_work;
315 struct work_struct ac_work;
316 struct work_struct detect_usb_type_work;
317 struct work_struct usb_link_status_work;
318 struct work_struct check_main_thermal_prot_work;
319 struct work_struct check_usb_thermal_prot_work;
320 struct usb_phy *usb_phy;
321 struct notifier_block nb;
322 struct mutex charger_attached_mutex;
326 static enum power_supply_property ab8500_charger_ac_props[] = {
327 POWER_SUPPLY_PROP_HEALTH,
328 POWER_SUPPLY_PROP_PRESENT,
329 POWER_SUPPLY_PROP_ONLINE,
330 POWER_SUPPLY_PROP_VOLTAGE_NOW,
331 POWER_SUPPLY_PROP_VOLTAGE_AVG,
332 POWER_SUPPLY_PROP_CURRENT_NOW,
336 static enum power_supply_property ab8500_charger_usb_props[] = {
337 POWER_SUPPLY_PROP_HEALTH,
338 POWER_SUPPLY_PROP_CURRENT_AVG,
339 POWER_SUPPLY_PROP_PRESENT,
340 POWER_SUPPLY_PROP_ONLINE,
341 POWER_SUPPLY_PROP_VOLTAGE_NOW,
342 POWER_SUPPLY_PROP_VOLTAGE_AVG,
343 POWER_SUPPLY_PROP_CURRENT_NOW,
347 * Function for enabling and disabling sw fallback mode
348 * should always be disabled when no charger is connected.
350 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
359 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
361 if (is_ab8500(di->parent)) {
366 bank = AB8500_SYS_CTRL1_BLOCK;
367 reg = AB8500_SW_CONTROL_FALLBACK;
371 /* read the register containing fallback bit */
372 ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
374 dev_err(di->dev, "%d read failed\n", __LINE__);
378 if (is_ab8500(di->parent)) {
379 /* enable the OPT emulation registers */
380 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
382 dev_err(di->dev, "%d write failed\n", __LINE__);
392 /* write back the changed fallback bit value to register */
393 ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
395 dev_err(di->dev, "%d write failed\n", __LINE__);
399 if (is_ab8500(di->parent)) {
400 /* disable the set OTP registers again */
401 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
403 dev_err(di->dev, "%d write failed\n", __LINE__);
409 * ab8500_power_supply_changed - a wrapper with local extensions for
410 * power_supply_changed
411 * @di: pointer to the ab8500_charger structure
412 * @psy: pointer to power_supply_that have changed.
415 static void ab8500_power_supply_changed(struct ab8500_charger *di,
416 struct power_supply *psy)
419 * This happens if we get notifications or interrupts and
420 * the platform has been configured not to support one or
421 * other type of charging.
426 if (di->autopower_cfg) {
427 if (!di->usb.charger_connected &&
428 !di->ac.charger_connected &&
430 di->autopower = false;
431 ab8500_enable_disable_sw_fallback(di, false);
432 } else if (!di->autopower &&
433 (di->ac.charger_connected ||
434 di->usb.charger_connected)) {
435 di->autopower = true;
436 ab8500_enable_disable_sw_fallback(di, true);
439 power_supply_changed(psy);
442 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
445 if (connected != di->usb.charger_connected) {
446 dev_dbg(di->dev, "USB connected:%i\n", connected);
447 di->usb.charger_connected = connected;
450 di->flags.vbus_drop_end = false;
453 * Sometimes the platform is configured not to support
454 * USB charging and no psy has been created, but we still
455 * will get these notifications.
457 if (di->usb_chg.psy) {
458 sysfs_notify(&di->usb_chg.psy->dev.kobj, NULL,
463 mutex_lock(&di->charger_attached_mutex);
464 mutex_unlock(&di->charger_attached_mutex);
466 if (is_ab8500(di->parent))
467 queue_delayed_work(di->charger_wq,
468 &di->usb_charger_attached_work,
471 cancel_delayed_work_sync(&di->usb_charger_attached_work);
472 mutex_lock(&di->charger_attached_mutex);
473 mutex_unlock(&di->charger_attached_mutex);
479 * ab8500_charger_get_ac_voltage() - get ac charger voltage
480 * @di: pointer to the ab8500_charger structure
482 * Returns ac charger voltage in microvolt (on success)
484 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
488 /* Only measure voltage if the charger is connected */
489 if (di->ac.charger_connected) {
490 ret = iio_read_channel_processed(di->adc_main_charger_v, &vch);
492 dev_err(di->dev, "%s ADC conv failed,\n", __func__);
496 /* Convert to microvolt, IIO returns millivolt */
501 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
502 * @di: pointer to the ab8500_charger structure
504 * Returns ac charger CV mode (on success) else error code
506 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
511 /* Only check CV mode if the charger is online */
512 if (di->ac.charger_online) {
513 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
514 AB8500_CH_STATUS1_REG, &val);
516 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
520 if (val & MAIN_CH_CV_ON)
530 * ab8500_charger_get_vbus_voltage() - get vbus voltage
531 * @di: pointer to the ab8500_charger structure
533 * This function returns the vbus voltage.
534 * Returns vbus voltage in microvolt (on success)
536 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
540 /* Only measure voltage if the charger is connected */
541 if (di->usb.charger_connected) {
542 ret = iio_read_channel_processed(di->adc_vbus_v, &vch);
544 dev_err(di->dev, "%s ADC conv failed,\n", __func__);
548 /* Convert to microvolt, IIO returns millivolt */
553 * ab8500_charger_get_usb_current() - get usb charger current
554 * @di: pointer to the ab8500_charger structure
556 * This function returns the usb charger current.
557 * Returns usb current in microamperes (on success) and error code on failure
559 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
563 /* Only measure current if the charger is online */
564 if (di->usb.charger_online) {
565 ret = iio_read_channel_processed(di->adc_usb_charger_c, &ich);
567 dev_err(di->dev, "%s ADC conv failed,\n", __func__);
571 /* Return microamperes */
576 * ab8500_charger_get_ac_current() - get ac charger current
577 * @di: pointer to the ab8500_charger structure
579 * This function returns the ac charger current.
580 * Returns ac current in microamperes (on success) and error code on failure.
582 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
586 /* Only measure current if the charger is online */
587 if (di->ac.charger_online) {
588 ret = iio_read_channel_processed(di->adc_main_charger_c, &ich);
590 dev_err(di->dev, "%s ADC conv failed,\n", __func__);
594 /* Return microamperes */
599 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
600 * @di: pointer to the ab8500_charger structure
602 * Returns ac charger CV mode (on success) else error code
604 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
609 /* Only check CV mode if the charger is online */
610 if (di->usb.charger_online) {
611 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
612 AB8500_CH_USBCH_STAT1_REG, &val);
614 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
618 if (val & USB_CH_CV_ON)
630 * ab8500_charger_detect_chargers() - Detect the connected chargers
631 * @di: pointer to the ab8500_charger structure
632 * @probe: if probe, don't delay and wait for HW
634 * Returns the type of charger connected.
635 * For USB it will not mean we can actually charge from it
636 * but that there is a USB cable connected that we have to
637 * identify. This is used during startup when we don't get
638 * interrupts of the charger detection
640 * Returns an integer value, that means,
641 * NO_PW_CONN no power supply is connected
642 * AC_PW_CONN if the AC power supply is connected
643 * USB_PW_CONN if the USB power supply is connected
644 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
646 static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
648 int result = NO_PW_CONN;
652 /* Check for AC charger */
653 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
654 AB8500_CH_STATUS1_REG, &val);
656 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
660 if (val & MAIN_CH_DET)
663 /* Check for USB charger */
667 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
668 * when disconnecting ACA even though no
669 * charger was connected. Try waiting a little
670 * longer than the 100 ms of VBUS_DET_DBNC100...
674 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
675 AB8500_CH_USBCH_STAT1_REG, &val);
677 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
681 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
683 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
684 result |= USB_PW_CONN;
690 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
691 * @di: pointer to the ab8500_charger structure
692 * @link_status: the identified USB type
694 * Get the maximum current that is allowed to be drawn from the host
695 * based on the USB type.
696 * Returns error code in case of failure else 0 on success
698 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
699 enum ab8500_charger_link_status link_status)
703 di->usb_device_is_unrecognised = false;
706 * Platform only supports USB 2.0.
707 * This means that charging current from USB source
708 * is maximum 500 mA. Every occurrence of USB_STAT_*_HOST_*
709 * should set USB_CH_IP_CUR_LVL_0P5.
712 switch (link_status) {
713 case USB_STAT_STD_HOST_NC:
714 case USB_STAT_STD_HOST_C_NS:
715 case USB_STAT_STD_HOST_C_S:
716 dev_dbg(di->dev, "USB Type - Standard host is "
717 "detected through USB driver\n");
718 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
721 case USB_STAT_HOST_CHG_HS_CHIRP:
722 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
725 case USB_STAT_HOST_CHG_HS:
726 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
729 case USB_STAT_ACA_RID_C_HS:
730 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P9;
733 case USB_STAT_ACA_RID_A:
735 * Dedicated charger level minus maximum current accessory
736 * can consume (900mA). Closest level is 500mA
738 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
739 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
742 case USB_STAT_ACA_RID_B:
744 * Dedicated charger level minus 120mA (20mA for ACA and
745 * 100mA for potential accessory). Closest level is 1300mA
747 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_1P3;
748 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
749 di->max_usb_in_curr.usb_type_max_ua);
752 case USB_STAT_HOST_CHG_NM:
753 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
756 case USB_STAT_DEDICATED_CHG:
757 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_1P5;
760 case USB_STAT_ACA_RID_C_HS_CHIRP:
761 case USB_STAT_ACA_RID_C_NM:
762 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_1P5;
765 case USB_STAT_NOT_CONFIGURED:
766 if (di->vbus_detected) {
767 di->usb_device_is_unrecognised = true;
768 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
769 di->max_usb_in_curr.usb_type_max_ua =
770 USB_CH_IP_CUR_LVL_1P5;
774 case USB_STAT_HM_IDGND:
775 dev_err(di->dev, "USB Type - Charging not allowed\n");
776 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P05;
779 case USB_STAT_RESERVED:
780 if (is_ab8500(di->parent)) {
781 di->flags.vbus_collapse = true;
782 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
783 "VBUS has collapsed\n");
787 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
788 di->max_usb_in_curr.usb_type_max_ua =
789 USB_CH_IP_CUR_LVL_0P05;
790 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
792 di->max_usb_in_curr.usb_type_max_ua);
796 case USB_STAT_CARKIT_1:
797 case USB_STAT_CARKIT_2:
798 case USB_STAT_ACA_DOCK_CHARGER:
799 case USB_STAT_CHARGER_LINE_1:
800 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
801 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
802 di->max_usb_in_curr.usb_type_max_ua);
804 case USB_STAT_NOT_VALID_LINK:
805 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
806 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
810 dev_err(di->dev, "USB Type - Unknown\n");
811 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P05;
816 di->max_usb_in_curr.set_max_ua = di->max_usb_in_curr.usb_type_max_ua;
817 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
818 link_status, di->max_usb_in_curr.set_max_ua);
824 * ab8500_charger_read_usb_type() - read the type of usb connected
825 * @di: pointer to the ab8500_charger structure
827 * Detect the type of the plugged USB
828 * Returns error code in case of failure else 0 on success
830 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
835 ret = abx500_get_register_interruptible(di->dev,
836 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
838 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
841 if (is_ab8500(di->parent))
842 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
843 AB8500_USB_LINE_STAT_REG, &val);
845 ret = abx500_get_register_interruptible(di->dev,
846 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
848 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
852 /* get the USB type */
853 if (is_ab8500(di->parent))
854 val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
856 val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
857 ret = ab8500_charger_max_usb_curr(di,
858 (enum ab8500_charger_link_status) val);
864 * ab8500_charger_detect_usb_type() - get the type of usb connected
865 * @di: pointer to the ab8500_charger structure
867 * Detect the type of the plugged USB
868 * Returns error code in case of failure else 0 on success
870 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
876 * On getting the VBUS rising edge detect interrupt there
877 * is a 250ms delay after which the register UsbLineStatus
878 * is filled with valid data.
880 for (i = 0; i < 10; i++) {
882 ret = abx500_get_register_interruptible(di->dev,
883 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
885 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
888 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
892 if (is_ab8500(di->parent))
893 ret = abx500_get_register_interruptible(di->dev,
894 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
896 ret = abx500_get_register_interruptible(di->dev,
897 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
899 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
902 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
905 * Until the IT source register is read the UsbLineStatus
906 * register is not updated, hence doing the same
910 /* get the USB type */
911 if (is_ab8500(di->parent))
912 val = (val & AB8500_USB_LINK_STATUS) >>
913 USB_LINK_STATUS_SHIFT;
915 val = (val & AB8505_USB_LINK_STATUS) >>
916 USB_LINK_STATUS_SHIFT;
920 ret = ab8500_charger_max_usb_curr(di,
921 (enum ab8500_charger_link_status) val);
927 * This array maps the raw hex value to charger voltage used by the AB8500
928 * Values taken from the UM0836, in microvolt.
930 static int ab8500_charger_voltage_map[] = {
1011 static int ab8500_voltage_to_regval(int voltage_uv)
1015 /* Special case for voltage below 3.5V */
1016 if (voltage_uv < ab8500_charger_voltage_map[0])
1017 return LOW_VOLT_REG;
1019 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
1020 if (voltage_uv < ab8500_charger_voltage_map[i])
1024 /* If not last element, return error */
1025 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
1026 if (voltage_uv == ab8500_charger_voltage_map[i])
1032 /* This array maps the raw register value to charger input current */
1033 static int ab8500_charge_input_curr_map[] = {
1034 50000, 98000, 193000, 290000, 380000, 450000, 500000, 600000,
1035 700000, 800000, 900000, 1000000, 1100000, 1300000, 1400000, 1500000,
1038 /* This array maps the raw register value to charger output current */
1039 static int ab8500_charge_output_curr_map[] = {
1040 100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000,
1041 900000, 1000000, 1100000, 1200000, 1300000, 1400000, 1500000, 1500000,
1044 static int ab8500_current_to_regval(struct ab8500_charger *di, int curr_ua)
1048 if (curr_ua < ab8500_charge_output_curr_map[0])
1051 for (i = 0; i < ARRAY_SIZE(ab8500_charge_output_curr_map); i++) {
1052 if (curr_ua < ab8500_charge_output_curr_map[i])
1056 /* If not last element, return error */
1057 i = ARRAY_SIZE(ab8500_charge_output_curr_map) - 1;
1058 if (curr_ua == ab8500_charge_output_curr_map[i])
1064 static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr_ua)
1068 if (curr_ua < ab8500_charge_input_curr_map[0])
1071 for (i = 0; i < ARRAY_SIZE(ab8500_charge_input_curr_map); i++) {
1072 if (curr_ua < ab8500_charge_input_curr_map[i])
1076 /* If not last element, return error */
1077 i = ARRAY_SIZE(ab8500_charge_input_curr_map) - 1;
1078 if (curr_ua == ab8500_charge_input_curr_map[i])
1085 * ab8500_charger_get_usb_cur() - get usb current
1086 * @di: pointer to the ab8500_charger structure
1088 * The usb stack provides the maximum current that can be drawn from
1089 * the standard usb host. This will be in uA.
1090 * This function converts current in uA to a value that can be written
1091 * to the register. Returns -1 if charging is not allowed
1093 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1096 switch (di->usb_state.usb_current_ua) {
1098 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P09;
1101 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P19;
1104 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P29;
1107 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P38;
1110 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
1113 di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P05;
1117 di->max_usb_in_curr.set_max_ua = di->max_usb_in_curr.usb_type_max_ua;
1122 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1123 * @di: pointer to the ab8500_charger structure
1124 * @reg: select what charger register to check
1126 * Check if current stepping should be allowed to continue.
1127 * Checks if charger source has not collapsed. If it has, further stepping
1130 static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1133 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1134 return !di->flags.vbus_drop_end;
1140 * ab8500_charger_set_current() - set charger current
1141 * @di: pointer to the ab8500_charger structure
1142 * @ich_ua: charger current, in uA
1143 * @reg: select what charger register to set
1145 * Set charger current.
1146 * There is no state machine in the AB to step up/down the charger
1147 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1148 * charging is started. Instead we need to implement
1149 * this charger current step-up/down here.
1150 * Returns error code in case of failure else 0(on success)
1152 static int ab8500_charger_set_current(struct ab8500_charger *di,
1153 int ich_ua, int reg)
1156 int curr_index, prev_curr_index, shift_value, i;
1159 bool no_stepping = false;
1161 atomic_inc(&di->current_stepping_sessions);
1163 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1166 dev_err(di->dev, "%s read failed\n", __func__);
1167 goto exit_set_current;
1171 case AB8500_MCH_IPT_CURLVL_REG:
1172 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1173 prev_curr_index = (reg_value >> shift_value);
1174 curr_index = ab8500_current_to_regval(di, ich_ua);
1175 step_udelay = STEP_UDELAY;
1176 if (!di->ac.charger_connected)
1179 case AB8500_USBCH_IPT_CRNTLVL_REG:
1180 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1181 prev_curr_index = (reg_value >> shift_value);
1182 curr_index = ab8500_vbus_in_curr_to_regval(di, ich_ua);
1183 step_udelay = STEP_UDELAY * 100;
1185 if (!di->usb.charger_connected)
1188 case AB8500_CH_OPT_CRNTLVL_REG:
1190 prev_curr_index = (reg_value >> shift_value);
1191 curr_index = ab8500_current_to_regval(di, ich_ua);
1192 step_udelay = STEP_UDELAY;
1193 if (curr_index && (curr_index - prev_curr_index) > 1)
1196 if (!di->usb.charger_connected && !di->ac.charger_connected)
1201 dev_err(di->dev, "%s current register not valid\n", __func__);
1203 goto exit_set_current;
1206 if (curr_index < 0) {
1207 dev_err(di->dev, "requested current limit out-of-range\n");
1209 goto exit_set_current;
1212 /* only update current if it's been changed */
1213 if (prev_curr_index == curr_index) {
1214 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1217 goto exit_set_current;
1220 dev_dbg(di->dev, "%s set charger current: %d uA for reg: 0x%02x\n",
1221 __func__, ich_ua, reg);
1224 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1225 reg, (u8)curr_index << shift_value);
1227 dev_err(di->dev, "%s write failed\n", __func__);
1228 } else if (prev_curr_index > curr_index) {
1229 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1230 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1231 (u8) i << shift_value, reg);
1232 ret = abx500_set_register_interruptible(di->dev,
1233 AB8500_CHARGER, reg, (u8)i << shift_value);
1235 dev_err(di->dev, "%s write failed\n", __func__);
1236 goto exit_set_current;
1238 if (i != curr_index)
1239 usleep_range(step_udelay, step_udelay * 2);
1243 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
1244 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1245 (u8)i << shift_value, reg);
1246 ret = abx500_set_register_interruptible(di->dev,
1247 AB8500_CHARGER, reg, (u8)i << shift_value);
1249 dev_err(di->dev, "%s write failed\n", __func__);
1250 goto exit_set_current;
1252 if (i != curr_index)
1253 usleep_range(step_udelay, step_udelay * 2);
1255 allow = ab8500_charger_check_continue_stepping(di, reg);
1260 atomic_dec(&di->current_stepping_sessions);
1266 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1267 * @di: pointer to the ab8500_charger structure
1268 * @ich_in_ua: charger input current limit in microampere
1270 * Sets the current that can be drawn from the USB host
1271 * Returns error code in case of failure else 0(on success)
1273 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1279 /* We should always use to lowest current limit */
1280 min_value = min(di->bm->chg_params->usb_curr_max_ua, ich_in_ua);
1281 if (di->max_usb_in_curr.set_max_ua > 0)
1282 min_value = min(di->max_usb_in_curr.set_max_ua, min_value);
1284 if (di->usb_state.usb_current_ua >= 0)
1285 min_value = min(di->usb_state.usb_current_ua, min_value);
1287 switch (min_value) {
1289 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1290 min_value = USB_CH_IP_CUR_LVL_0P05;
1293 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1294 min_value = USB_CH_IP_CUR_LVL_0P45;
1300 dev_info(di->dev, "VBUS input current limit set to %d uA\n", min_value);
1302 mutex_lock(&di->usb_ipt_crnt_lock);
1303 ret = ab8500_charger_set_current(di, min_value,
1304 AB8500_USBCH_IPT_CRNTLVL_REG);
1305 mutex_unlock(&di->usb_ipt_crnt_lock);
1311 * ab8500_charger_set_main_in_curr() - set main charger input current
1312 * @di: pointer to the ab8500_charger structure
1313 * @ich_in_ua: input charger current, in uA
1315 * Set main charger input current.
1316 * Returns error code in case of failure else 0(on success)
1318 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1321 return ab8500_charger_set_current(di, ich_in_ua,
1322 AB8500_MCH_IPT_CURLVL_REG);
1326 * ab8500_charger_set_output_curr() - set charger output current
1327 * @di: pointer to the ab8500_charger structure
1328 * @ich_out_ua: output charger current, in uA
1330 * Set charger output current.
1331 * Returns error code in case of failure else 0(on success)
1333 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1336 return ab8500_charger_set_current(di, ich_out_ua,
1337 AB8500_CH_OPT_CRNTLVL_REG);
1341 * ab8500_charger_led_en() - turn on/off chargign led
1342 * @di: pointer to the ab8500_charger structure
1343 * @on: flag to turn on/off the chargign led
1345 * Power ON/OFF charging LED indication
1346 * Returns error code in case of failure else 0(on success)
1348 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1353 /* Power ON charging LED indicator, set LED current to 5mA */
1354 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1355 AB8500_LED_INDICATOR_PWM_CTRL,
1356 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1358 dev_err(di->dev, "Power ON LED failed\n");
1361 /* LED indicator PWM duty cycle 252/256 */
1362 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1363 AB8500_LED_INDICATOR_PWM_DUTY,
1364 LED_INDICATOR_PWM_DUTY_252_256);
1366 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1370 /* Power off charging LED indicator */
1371 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1372 AB8500_LED_INDICATOR_PWM_CTRL,
1373 LED_INDICATOR_PWM_DIS);
1375 dev_err(di->dev, "Power-off LED failed\n");
1384 * ab8500_charger_ac_en() - enable or disable ac charging
1385 * @di: pointer to the ab8500_charger structure
1386 * @enable: enable/disable flag
1387 * @vset_uv: charging voltage in microvolt
1388 * @iset_ua: charging current in microampere
1390 * Enable/Disable AC/Mains charging and turns on/off the charging led
1393 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1394 int enable, int vset_uv, int iset_ua)
1399 int input_curr_index;
1402 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1405 /* Check if AC is connected */
1406 if (!di->ac.charger_connected) {
1407 dev_err(di->dev, "AC charger not connected\n");
1411 /* Enable AC charging */
1412 dev_dbg(di->dev, "Enable AC: %duV %duA\n", vset_uv, iset_ua);
1415 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1416 * will be triggered every time we enable the VDD ADC supply.
1417 * This will turn off charging for a short while.
1418 * It can be avoided by having the supply on when
1419 * there is a charger enabled. Normally the VDD ADC supply
1420 * is enabled every time a GPADC conversion is triggered.
1421 * We will force it to be enabled from this driver to have
1422 * the GPADC module independent of the AB8500 chargers
1424 if (!di->vddadc_en_ac) {
1425 ret = regulator_enable(di->regu);
1428 "Failed to enable regulator\n");
1430 di->vddadc_en_ac = true;
1433 /* Check if the requested voltage or current is valid */
1434 volt_index = ab8500_voltage_to_regval(vset_uv);
1435 curr_index = ab8500_current_to_regval(di, iset_ua);
1436 input_curr_index = ab8500_current_to_regval(di,
1437 di->bm->chg_params->ac_curr_max_ua);
1438 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1440 "Charger voltage or current too high, "
1441 "charging not started\n");
1445 /* ChVoltLevel: maximum battery charging voltage */
1446 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1447 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1449 dev_err(di->dev, "%s write failed\n", __func__);
1452 /* MainChInputCurr: current that can be drawn from the charger*/
1453 ret = ab8500_charger_set_main_in_curr(di,
1454 di->bm->chg_params->ac_curr_max_ua);
1456 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1460 /* ChOutputCurentLevel: protected output current */
1461 ret = ab8500_charger_set_output_curr(di, iset_ua);
1463 dev_err(di->dev, "%s "
1464 "Failed to set ChOutputCurentLevel\n",
1469 /* Check if VBAT overshoot control should be enabled */
1470 if (!di->bm->enable_overshoot)
1471 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1473 /* Enable Main Charger */
1474 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1475 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1477 dev_err(di->dev, "%s write failed\n", __func__);
1481 /* Power on charging LED indication */
1482 ret = ab8500_charger_led_en(di, true);
1484 dev_err(di->dev, "failed to enable LED\n");
1486 di->ac.charger_online = 1;
1488 /* Disable AC charging */
1489 if (is_ab8500_1p1_or_earlier(di->parent)) {
1491 * For ABB revision 1.0 and 1.1 there is a bug in the
1492 * watchdog logic. That means we have to continuously
1493 * kick the charger watchdog even when no charger is
1494 * connected. This is only valid once the AC charger
1495 * has been enabled. This is a bug that is not handled
1496 * by the algorithm and the watchdog have to be kicked
1497 * by the charger driver when the AC charger
1501 queue_delayed_work(di->charger_wq,
1503 round_jiffies(WD_KICK_INTERVAL));
1507 * We can't turn off charging completely
1508 * due to a bug in AB8500 cut1.
1509 * If we do, charging will not start again.
1510 * That is why we set the lowest voltage
1511 * and current possible
1513 ret = abx500_set_register_interruptible(di->dev,
1515 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1518 "%s write failed\n", __func__);
1522 ret = ab8500_charger_set_output_curr(di, 0);
1524 dev_err(di->dev, "%s "
1525 "Failed to set ChOutputCurentLevel\n",
1530 ret = abx500_set_register_interruptible(di->dev,
1532 AB8500_MCH_CTRL1, 0);
1535 "%s write failed\n", __func__);
1540 ret = ab8500_charger_led_en(di, false);
1542 dev_err(di->dev, "failed to disable LED\n");
1544 di->ac.charger_online = 0;
1545 di->ac.wd_expired = false;
1547 /* Disable regulator if enabled */
1548 if (di->vddadc_en_ac) {
1549 regulator_disable(di->regu);
1550 di->vddadc_en_ac = false;
1553 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1555 ab8500_power_supply_changed(di, di->ac_chg.psy);
1561 * ab8500_charger_usb_en() - enable usb charging
1562 * @di: pointer to the ab8500_charger structure
1563 * @enable: enable/disable flag
1564 * @vset_uv: charging voltage in microvolt
1565 * @ich_out_ua: charger output current in microampere
1567 * Enable/Disable USB charging and turns on/off the charging led respectively.
1568 * Returns error code in case of failure else 0(on success)
1570 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1571 int enable, int vset_uv, int ich_out_ua)
1578 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1581 /* Check if USB is connected */
1582 if (!di->usb.charger_connected) {
1583 dev_err(di->dev, "USB charger not connected\n");
1588 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1589 * will be triggered every time we enable the VDD ADC supply.
1590 * This will turn off charging for a short while.
1591 * It can be avoided by having the supply on when
1592 * there is a charger enabled. Normally the VDD ADC supply
1593 * is enabled every time a GPADC conversion is triggered.
1594 * We will force it to be enabled from this driver to have
1595 * the GPADC module independent of the AB8500 chargers
1597 if (!di->vddadc_en_usb) {
1598 ret = regulator_enable(di->regu);
1601 "Failed to enable regulator\n");
1603 di->vddadc_en_usb = true;
1606 /* Enable USB charging */
1607 dev_dbg(di->dev, "Enable USB: %d uV %d uA\n", vset_uv, ich_out_ua);
1609 /* Check if the requested voltage or current is valid */
1610 volt_index = ab8500_voltage_to_regval(vset_uv);
1611 curr_index = ab8500_current_to_regval(di, ich_out_ua);
1612 if (volt_index < 0 || curr_index < 0) {
1614 "Charger voltage or current too high, "
1615 "charging not started\n");
1620 * ChVoltLevel: max voltage up to which battery can be
1623 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1624 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1626 dev_err(di->dev, "%s write failed\n", __func__);
1629 /* Check if VBAT overshoot control should be enabled */
1630 if (!di->bm->enable_overshoot)
1631 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1633 /* Enable USB Charger */
1635 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
1636 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1637 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1639 dev_err(di->dev, "%s write failed\n", __func__);
1643 /* If success power on charging LED indication */
1644 ret = ab8500_charger_led_en(di, true);
1646 dev_err(di->dev, "failed to enable LED\n");
1648 di->usb.charger_online = 1;
1650 /* USBChInputCurr: current that can be drawn from the usb */
1651 ret = ab8500_charger_set_vbus_in_curr(di,
1652 di->max_usb_in_curr.usb_type_max_ua);
1654 dev_err(di->dev, "setting USBChInputCurr failed\n");
1658 /* ChOutputCurentLevel: protected output current */
1659 ret = ab8500_charger_set_output_curr(di, ich_out_ua);
1661 dev_err(di->dev, "%s "
1662 "Failed to set ChOutputCurentLevel\n",
1667 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1670 /* Disable USB charging */
1671 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1672 ret = abx500_set_register_interruptible(di->dev,
1674 AB8500_USBCH_CTRL1_REG, 0);
1677 "%s write failed\n", __func__);
1681 ret = ab8500_charger_led_en(di, false);
1683 dev_err(di->dev, "failed to disable LED\n");
1684 /* USBChInputCurr: current that can be drawn from the usb */
1685 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1687 dev_err(di->dev, "setting USBChInputCurr failed\n");
1691 /* ChOutputCurentLevel: protected output current */
1692 ret = ab8500_charger_set_output_curr(di, 0);
1694 dev_err(di->dev, "%s "
1695 "Failed to reset ChOutputCurentLevel\n",
1699 di->usb.charger_online = 0;
1700 di->usb.wd_expired = false;
1702 /* Disable regulator if enabled */
1703 if (di->vddadc_en_usb) {
1704 regulator_disable(di->regu);
1705 di->vddadc_en_usb = false;
1708 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1710 /* Cancel any pending Vbat check work */
1711 cancel_delayed_work(&di->check_vbat_work);
1714 ab8500_power_supply_changed(di, di->usb_chg.psy);
1720 * ab8500_charger_usb_check_enable() - enable usb charging
1721 * @charger: pointer to the ux500_charger structure
1722 * @vset_uv: charging voltage in microvolt
1723 * @iset_ua: charger output current in microampere
1725 * Check if the VBUS charger has been disconnected and reconnected without
1726 * AB8500 rising an interrupt. Returns 0 on success.
1728 static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1729 int vset_uv, int iset_ua)
1734 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1736 if (!di->usb.charger_connected)
1739 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1740 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1742 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1745 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1747 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1748 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1750 ret = abx500_mask_and_set_register_interruptible(di->dev,
1751 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1752 DROP_COUNT_RESET, DROP_COUNT_RESET);
1754 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1758 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset_uv, iset_ua);
1760 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1769 * ab8500_charger_ac_check_enable() - enable usb charging
1770 * @charger: pointer to the ux500_charger structure
1771 * @vset_uv: charging voltage in microvolt
1772 * @iset_ua: charger output current in micrompere
1774 * Check if the AC charger has been disconnected and reconnected without
1775 * AB8500 rising an interrupt. Returns 0 on success.
1777 static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1778 int vset_uv, int iset_ua)
1780 u8 mainch_ctrl1 = 0;
1783 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1785 if (!di->ac.charger_connected)
1788 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1789 AB8500_MCH_CTRL1, &mainch_ctrl1);
1791 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1794 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1796 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1797 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1799 ret = abx500_mask_and_set_register_interruptible(di->dev,
1800 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1801 DROP_COUNT_RESET, DROP_COUNT_RESET);
1804 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1808 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset_uv, iset_ua);
1810 dev_err(di->dev, "failed to enable AC charger %d\n",
1819 * ab8500_charger_watchdog_kick() - kick charger watchdog
1820 * @di: pointer to the ab8500_charger structure
1822 * Kick charger watchdog
1823 * Returns error code in case of failure else 0(on success)
1825 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1828 struct ab8500_charger *di;
1830 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1831 di = to_ab8500_charger_ac_device_info(charger);
1832 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1833 di = to_ab8500_charger_usb_device_info(charger);
1837 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1838 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1840 dev_err(di->dev, "Failed to kick WD!\n");
1846 * ab8500_charger_update_charger_current() - update charger current
1847 * @charger: pointer to the ab8500_charger structure
1848 * @ich_out_ua: desired output current in microampere
1850 * Update the charger output current for the specified charger
1851 * Returns error code in case of failure else 0(on success)
1853 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1857 struct ab8500_charger *di;
1859 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1860 di = to_ab8500_charger_ac_device_info(charger);
1861 else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1862 di = to_ab8500_charger_usb_device_info(charger);
1866 ret = ab8500_charger_set_output_curr(di, ich_out_ua);
1868 dev_err(di->dev, "%s "
1869 "Failed to set ChOutputCurentLevel\n",
1874 /* Reset the main and usb drop input current measurement counter */
1875 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1876 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
1878 dev_err(di->dev, "%s write failed\n", __func__);
1885 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1887 struct power_supply *psy;
1888 struct power_supply *ext = dev_get_drvdata(dev);
1889 const char **supplicants = (const char **)ext->supplied_to;
1890 struct ab8500_charger *di;
1891 union power_supply_propval ret;
1893 struct ux500_charger *usb_chg;
1895 usb_chg = (struct ux500_charger *)data;
1898 di = to_ab8500_charger_usb_device_info(usb_chg);
1901 * For all psy where the driver name appears in any supplied_to
1902 * in practice what we will find will always be "ab8500_fg" as
1903 * the fuel gauge is responsible of keeping track of VBAT.
1905 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
1909 /* Go through all properties for the psy */
1910 for (j = 0; j < ext->desc->num_properties; j++) {
1911 enum power_supply_property prop;
1912 prop = ext->desc->properties[j];
1914 if (power_supply_get_property(ext, prop, &ret))
1918 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1919 switch (ext->desc->type) {
1920 case POWER_SUPPLY_TYPE_BATTERY:
1921 /* This will always be "ab8500_fg" */
1922 dev_dbg(di->dev, "get VBAT from %s\n",
1923 dev_name(&ext->dev));
1924 di->vbat = ret.intval;
1938 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1939 * @work pointer to the work_struct structure
1941 * Due to a asic bug it is necessary to lower the input current to the vbus
1942 * charger when charging with at some specific levels. This issue is only valid
1943 * for below a certain battery voltage. This function makes sure that
1944 * the allowed current limit isn't exceeded.
1946 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1949 struct ab8500_charger *di = container_of(work,
1950 struct ab8500_charger, check_vbat_work.work);
1952 class_for_each_device(power_supply_class, NULL,
1953 &di->usb_chg, ab8500_charger_get_ext_psy_data);
1955 /* First run old_vbat is 0. */
1956 if (di->old_vbat == 0)
1957 di->old_vbat = di->vbat;
1959 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1960 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1961 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1962 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1964 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1965 " old: %d\n", di->max_usb_in_curr.usb_type_max_ua,
1966 di->vbat, di->old_vbat);
1967 ab8500_charger_set_vbus_in_curr(di,
1968 di->max_usb_in_curr.usb_type_max_ua);
1969 power_supply_changed(di->usb_chg.psy);
1972 di->old_vbat = di->vbat;
1975 * No need to check the battery voltage every second when not close to
1978 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100000) &&
1979 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100000)))
1982 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1986 * ab8500_charger_check_hw_failure_work() - check main charger failure
1987 * @work: pointer to the work_struct structure
1989 * Work queue function for checking the main charger status
1991 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1996 struct ab8500_charger *di = container_of(work,
1997 struct ab8500_charger, check_hw_failure_work.work);
1999 /* Check if the status bits for HW failure is still active */
2000 if (di->flags.mainextchnotok) {
2001 ret = abx500_get_register_interruptible(di->dev,
2002 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2004 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2007 if (!(reg_value & MAIN_CH_NOK)) {
2008 di->flags.mainextchnotok = false;
2009 ab8500_power_supply_changed(di, di->ac_chg.psy);
2012 if (di->flags.vbus_ovv) {
2013 ret = abx500_get_register_interruptible(di->dev,
2014 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
2017 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2020 if (!(reg_value & VBUS_OVV_TH)) {
2021 di->flags.vbus_ovv = false;
2022 ab8500_power_supply_changed(di, di->usb_chg.psy);
2025 /* If we still have a failure, schedule a new check */
2026 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2027 queue_delayed_work(di->charger_wq,
2028 &di->check_hw_failure_work, round_jiffies(HZ));
2033 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2034 * @work: pointer to the work_struct structure
2036 * Work queue function for kicking the charger watchdog.
2038 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2039 * logic. That means we have to continuously kick the charger
2040 * watchdog even when no charger is connected. This is only
2041 * valid once the AC charger has been enabled. This is
2042 * a bug that is not handled by the algorithm and the
2043 * watchdog have to be kicked by the charger driver
2044 * when the AC charger is disabled
2046 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2050 struct ab8500_charger *di = container_of(work,
2051 struct ab8500_charger, kick_wd_work.work);
2053 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2054 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2056 dev_err(di->dev, "Failed to kick WD!\n");
2058 /* Schedule a new watchdog kick */
2059 queue_delayed_work(di->charger_wq,
2060 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2064 * ab8500_charger_ac_work() - work to get and set main charger status
2065 * @work: pointer to the work_struct structure
2067 * Work queue function for checking the main charger status
2069 static void ab8500_charger_ac_work(struct work_struct *work)
2073 struct ab8500_charger *di = container_of(work,
2074 struct ab8500_charger, ac_work);
2077 * Since we can't be sure that the events are received
2078 * synchronously, we have the check if the main charger is
2079 * connected by reading the status register
2081 ret = ab8500_charger_detect_chargers(di, false);
2085 if (ret & AC_PW_CONN) {
2086 di->ac.charger_connected = 1;
2089 di->ac.charger_connected = 0;
2092 ab8500_power_supply_changed(di, di->ac_chg.psy);
2093 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
2096 static void ab8500_charger_usb_attached_work(struct work_struct *work)
2098 struct ab8500_charger *di = container_of(work,
2099 struct ab8500_charger,
2100 usb_charger_attached_work.work);
2101 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2105 for (i = 0; i < 10; i++) {
2106 ret = abx500_get_register_interruptible(di->dev,
2108 AB8500_CH_USBCH_STAT1_REG,
2111 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2114 if ((statval & usbch) != usbch)
2117 msleep(CHARGER_STATUS_POLL);
2120 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2122 mutex_lock(&di->charger_attached_mutex);
2123 mutex_unlock(&di->charger_attached_mutex);
2128 queue_delayed_work(di->charger_wq,
2129 &di->usb_charger_attached_work,
2133 static void ab8500_charger_ac_attached_work(struct work_struct *work)
2136 struct ab8500_charger *di = container_of(work,
2137 struct ab8500_charger,
2138 ac_charger_attached_work.work);
2139 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2140 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2144 for (i = 0; i < 10; i++) {
2145 ret = abx500_get_register_interruptible(di->dev,
2147 AB8500_CH_STATUS2_REG,
2150 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2154 if ((statval & mainch) != mainch)
2157 msleep(CHARGER_STATUS_POLL);
2160 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2161 queue_work(di->charger_wq, &di->ac_work);
2163 mutex_lock(&di->charger_attached_mutex);
2164 mutex_unlock(&di->charger_attached_mutex);
2169 queue_delayed_work(di->charger_wq,
2170 &di->ac_charger_attached_work,
2175 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2176 * @work: Pointer to the work_struct structure
2178 * Detect the type of USB plugged
2180 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
2184 struct ab8500_charger *di = container_of(work,
2185 struct ab8500_charger, detect_usb_type_work);
2188 * Since we can't be sure that the events are received
2189 * synchronously, we have the check if is
2190 * connected by reading the status register
2192 ret = ab8500_charger_detect_chargers(di, false);
2196 if (!(ret & USB_PW_CONN)) {
2197 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2198 di->vbus_detected = false;
2199 ab8500_charger_set_usb_connected(di, false);
2200 ab8500_power_supply_changed(di, di->usb_chg.psy);
2202 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2203 di->vbus_detected = true;
2205 if (is_ab8500_1p1_or_earlier(di->parent)) {
2206 ret = ab8500_charger_detect_usb_type(di);
2208 ab8500_charger_set_usb_connected(di, true);
2209 ab8500_power_supply_changed(di,
2214 * For ABB cut2.0 and onwards we have an IRQ,
2215 * USB_LINK_STATUS that will be triggered when the USB
2216 * link status changes. The exception is USB connected
2217 * during startup. Then we don't get a
2218 * USB_LINK_STATUS IRQ
2220 if (di->vbus_detected_start) {
2221 di->vbus_detected_start = false;
2222 ret = ab8500_charger_detect_usb_type(di);
2224 ab8500_charger_set_usb_connected(di,
2226 ab8500_power_supply_changed(di,
2235 * ab8500_charger_usb_link_attach_work() - work to detect USB type
2236 * @work: pointer to the work_struct structure
2238 * Detect the type of USB plugged
2240 static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2242 struct ab8500_charger *di =
2243 container_of(work, struct ab8500_charger, attach_work.work);
2246 /* Update maximum input current if USB enumeration is not detected */
2247 if (!di->usb.charger_online) {
2248 ret = ab8500_charger_set_vbus_in_curr(di,
2249 di->max_usb_in_curr.usb_type_max_ua);
2254 ab8500_charger_set_usb_connected(di, true);
2255 ab8500_power_supply_changed(di, di->usb_chg.psy);
2259 * ab8500_charger_usb_link_status_work() - work to detect USB type
2260 * @work: pointer to the work_struct structure
2262 * Detect the type of USB plugged
2264 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2266 int detected_chargers;
2271 struct ab8500_charger *di = container_of(work,
2272 struct ab8500_charger, usb_link_status_work);
2275 * Since we can't be sure that the events are received
2276 * synchronously, we have the check if is
2277 * connected by reading the status register
2279 detected_chargers = ab8500_charger_detect_chargers(di, false);
2280 if (detected_chargers < 0)
2284 * Some chargers that breaks the USB spec is
2285 * identified as invalid by AB8500 and it refuse
2286 * to start the charging process. but by jumping
2287 * through a few hoops it can be forced to start.
2289 if (is_ab8500(di->parent))
2290 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2291 AB8500_USB_LINE_STAT_REG, &val);
2293 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2294 AB8500_USB_LINK1_STAT_REG, &val);
2297 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2299 dev_dbg(di->dev, "Error reading USB link status\n");
2301 if (is_ab8500(di->parent))
2302 link_status = AB8500_USB_LINK_STATUS;
2304 link_status = AB8505_USB_LINK_STATUS;
2306 if (detected_chargers & USB_PW_CONN) {
2307 if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
2308 USB_STAT_NOT_VALID_LINK &&
2309 di->invalid_charger_detect_state == 0) {
2311 "Invalid charger detected, state= 0\n");
2313 abx500_mask_and_set_register_interruptible(di->dev,
2314 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
2315 USB_CH_ENA, USB_CH_ENA);
2316 /*Enable charger detection*/
2317 abx500_mask_and_set_register_interruptible(di->dev,
2318 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2319 USB_CH_DET, USB_CH_DET);
2320 di->invalid_charger_detect_state = 1;
2321 /*exit and wait for new link status interrupt.*/
2325 if (di->invalid_charger_detect_state == 1) {
2327 "Invalid charger detected, state= 1\n");
2328 /*Stop charger detection*/
2329 abx500_mask_and_set_register_interruptible(di->dev,
2330 AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2332 /*Check link status*/
2333 if (is_ab8500(di->parent))
2334 ret = abx500_get_register_interruptible(di->dev,
2335 AB8500_USB, AB8500_USB_LINE_STAT_REG,
2338 ret = abx500_get_register_interruptible(di->dev,
2339 AB8500_USB, AB8500_USB_LINK1_STAT_REG,
2342 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2343 (val & link_status) >> USB_LINK_STATUS_SHIFT);
2344 di->invalid_charger_detect_state = 2;
2347 di->invalid_charger_detect_state = 0;
2350 if (!(detected_chargers & USB_PW_CONN)) {
2351 di->vbus_detected = false;
2352 ab8500_charger_set_usb_connected(di, false);
2353 ab8500_power_supply_changed(di, di->usb_chg.psy);
2357 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2358 di->vbus_detected = true;
2359 ret = ab8500_charger_read_usb_type(di);
2361 if (ret == -ENXIO) {
2362 /* No valid charger type detected */
2363 ab8500_charger_set_usb_connected(di, false);
2364 ab8500_power_supply_changed(di, di->usb_chg.psy);
2369 if (di->usb_device_is_unrecognised) {
2371 "Potential Legacy Charger device. "
2372 "Delay work for %d msec for USB enum "
2374 WAIT_ACA_RID_ENUMERATION);
2375 queue_delayed_work(di->charger_wq,
2377 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2378 } else if (di->is_aca_rid == 1) {
2379 /* Only wait once */
2382 "%s Wait %d msec for USB enum to finish",
2383 __func__, WAIT_ACA_RID_ENUMERATION);
2384 queue_delayed_work(di->charger_wq,
2386 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2388 queue_delayed_work(di->charger_wq,
2394 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2397 unsigned long flags;
2399 struct ab8500_charger *di = container_of(work,
2400 struct ab8500_charger, usb_state_changed_work.work);
2402 if (!di->vbus_detected) {
2404 "%s !di->vbus_detected\n",
2409 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2410 di->usb_state.state = di->usb_state.state_tmp;
2411 di->usb_state.usb_current_ua = di->usb_state.usb_current_tmp_ua;
2412 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2414 dev_dbg(di->dev, "%s USB state: 0x%02x uA: %d\n",
2415 __func__, di->usb_state.state, di->usb_state.usb_current_ua);
2417 switch (di->usb_state.state) {
2418 case AB8500_BM_USB_STATE_RESET_HS:
2419 case AB8500_BM_USB_STATE_RESET_FS:
2420 case AB8500_BM_USB_STATE_SUSPEND:
2421 case AB8500_BM_USB_STATE_MAX:
2422 ab8500_charger_set_usb_connected(di, false);
2423 ab8500_power_supply_changed(di, di->usb_chg.psy);
2426 case AB8500_BM_USB_STATE_RESUME:
2428 * when suspend->resume there should be delay
2429 * of 1sec for enabling charging
2433 case AB8500_BM_USB_STATE_CONFIGURED:
2435 * USB is configured, enable charging with the charging
2436 * input current obtained from USB driver
2438 if (!ab8500_charger_get_usb_cur(di)) {
2439 /* Update maximum input current */
2440 ret = ab8500_charger_set_vbus_in_curr(di,
2441 di->max_usb_in_curr.usb_type_max_ua);
2445 ab8500_charger_set_usb_connected(di, true);
2446 ab8500_power_supply_changed(di, di->usb_chg.psy);
2456 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2457 * @work: pointer to the work_struct structure
2459 * Work queue function for checking the USB charger Not OK status
2461 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2467 struct ab8500_charger *di = container_of(work,
2468 struct ab8500_charger, check_usbchgnotok_work.work);
2470 /* Check if the status bit for usbchargernotok is still active */
2471 ret = abx500_get_register_interruptible(di->dev,
2472 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2474 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2477 prev_status = di->flags.usbchargernotok;
2479 if (reg_value & VBUS_CH_NOK) {
2480 di->flags.usbchargernotok = true;
2481 /* Check again in 1sec */
2482 queue_delayed_work(di->charger_wq,
2483 &di->check_usbchgnotok_work, HZ);
2485 di->flags.usbchargernotok = false;
2486 di->flags.vbus_collapse = false;
2489 if (prev_status != di->flags.usbchargernotok)
2490 ab8500_power_supply_changed(di, di->usb_chg.psy);
2494 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2495 * @work: pointer to the work_struct structure
2497 * Work queue function for checking the Main thermal prot status
2499 static void ab8500_charger_check_main_thermal_prot_work(
2500 struct work_struct *work)
2505 struct ab8500_charger *di = container_of(work,
2506 struct ab8500_charger, check_main_thermal_prot_work);
2508 /* Check if the status bit for main_thermal_prot is still active */
2509 ret = abx500_get_register_interruptible(di->dev,
2510 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2512 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2515 if (reg_value & MAIN_CH_TH_PROT)
2516 di->flags.main_thermal_prot = true;
2518 di->flags.main_thermal_prot = false;
2520 ab8500_power_supply_changed(di, di->ac_chg.psy);
2524 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2525 * @work: pointer to the work_struct structure
2527 * Work queue function for checking the USB thermal prot status
2529 static void ab8500_charger_check_usb_thermal_prot_work(
2530 struct work_struct *work)
2535 struct ab8500_charger *di = container_of(work,
2536 struct ab8500_charger, check_usb_thermal_prot_work);
2538 /* Check if the status bit for usb_thermal_prot is still active */
2539 ret = abx500_get_register_interruptible(di->dev,
2540 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2542 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2545 if (reg_value & USB_CH_TH_PROT)
2546 di->flags.usb_thermal_prot = true;
2548 di->flags.usb_thermal_prot = false;
2550 ab8500_power_supply_changed(di, di->usb_chg.psy);
2554 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2555 * @irq: interrupt number
2556 * @_di: pointer to the ab8500_charger structure
2558 * Returns IRQ status(IRQ_HANDLED)
2560 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2562 struct ab8500_charger *di = _di;
2564 dev_dbg(di->dev, "Main charger unplugged\n");
2565 queue_work(di->charger_wq, &di->ac_work);
2567 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2568 mutex_lock(&di->charger_attached_mutex);
2569 mutex_unlock(&di->charger_attached_mutex);
2575 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2576 * @irq: interrupt number
2577 * @_di: pointer to the ab8500_charger structure
2579 * Returns IRQ status(IRQ_HANDLED)
2581 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2583 struct ab8500_charger *di = _di;
2585 dev_dbg(di->dev, "Main charger plugged\n");
2586 queue_work(di->charger_wq, &di->ac_work);
2588 mutex_lock(&di->charger_attached_mutex);
2589 mutex_unlock(&di->charger_attached_mutex);
2591 if (is_ab8500(di->parent))
2592 queue_delayed_work(di->charger_wq,
2593 &di->ac_charger_attached_work,
2599 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2600 * @irq: interrupt number
2601 * @_di: pointer to the ab8500_charger structure
2603 * Returns IRQ status(IRQ_HANDLED)
2605 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2607 struct ab8500_charger *di = _di;
2609 dev_dbg(di->dev, "Main charger not ok\n");
2610 di->flags.mainextchnotok = true;
2611 ab8500_power_supply_changed(di, di->ac_chg.psy);
2613 /* Schedule a new HW failure check */
2614 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2620 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2621 * thermal protection threshold
2622 * @irq: interrupt number
2623 * @_di: pointer to the ab8500_charger structure
2625 * Returns IRQ status(IRQ_HANDLED)
2627 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2629 struct ab8500_charger *di = _di;
2632 "Die temp above Main charger thermal protection threshold\n");
2633 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2639 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2640 * thermal protection threshold
2641 * @irq: interrupt number
2642 * @_di: pointer to the ab8500_charger structure
2644 * Returns IRQ status(IRQ_HANDLED)
2646 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2648 struct ab8500_charger *di = _di;
2651 "Die temp ok for Main charger thermal protection threshold\n");
2652 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2657 static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2659 struct ab8500_charger *di = container_of(work,
2660 struct ab8500_charger, vbus_drop_end_work.work);
2664 di->flags.vbus_drop_end = false;
2666 /* Reset the drop counter */
2667 abx500_set_register_interruptible(di->dev,
2668 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
2670 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2671 AB8500_CH_USBCH_STAT2_REG, ®_value);
2673 dev_err(di->dev, "%s read failed\n", __func__);
2677 curr_ua = ab8500_charge_input_curr_map[
2678 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
2680 if (di->max_usb_in_curr.calculated_max_ua != curr_ua) {
2681 /* USB source is collapsing */
2682 di->max_usb_in_curr.calculated_max_ua = curr_ua;
2684 "VBUS input current limiting to %d uA\n",
2685 di->max_usb_in_curr.calculated_max_ua);
2688 * USB source can not give more than this amount.
2689 * Taking more will collapse the source.
2691 di->max_usb_in_curr.set_max_ua =
2692 di->max_usb_in_curr.calculated_max_ua;
2694 "VBUS input current limited to %d uA\n",
2695 di->max_usb_in_curr.set_max_ua);
2698 if (di->usb.charger_connected)
2699 ab8500_charger_set_vbus_in_curr(di,
2700 di->max_usb_in_curr.usb_type_max_ua);
2704 * ab8500_charger_vbusdetf_handler() - VBUS falling 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_vbusdetf_handler(int irq, void *_di)
2712 struct ab8500_charger *di = _di;
2714 di->vbus_detected = false;
2715 dev_dbg(di->dev, "VBUS falling detected\n");
2716 queue_work(di->charger_wq, &di->detect_usb_type_work);
2722 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2723 * @irq: interrupt number
2724 * @_di: pointer to the ab8500_charger structure
2726 * Returns IRQ status(IRQ_HANDLED)
2728 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2730 struct ab8500_charger *di = _di;
2732 di->vbus_detected = true;
2733 dev_dbg(di->dev, "VBUS rising detected\n");
2735 queue_work(di->charger_wq, &di->detect_usb_type_work);
2741 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2742 * @irq: interrupt number
2743 * @_di: pointer to the ab8500_charger structure
2745 * Returns IRQ status(IRQ_HANDLED)
2747 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2749 struct ab8500_charger *di = _di;
2751 dev_dbg(di->dev, "USB link status changed\n");
2753 queue_work(di->charger_wq, &di->usb_link_status_work);
2759 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2760 * thermal protection threshold
2761 * @irq: interrupt number
2762 * @_di: pointer to the ab8500_charger structure
2764 * Returns IRQ status(IRQ_HANDLED)
2766 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2768 struct ab8500_charger *di = _di;
2771 "Die temp above USB charger thermal protection threshold\n");
2772 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2778 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2779 * thermal protection threshold
2780 * @irq: interrupt number
2781 * @_di: pointer to the ab8500_charger structure
2783 * Returns IRQ status(IRQ_HANDLED)
2785 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2787 struct ab8500_charger *di = _di;
2790 "Die temp ok for USB charger thermal protection threshold\n");
2791 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2797 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2798 * @irq: interrupt number
2799 * @_di: pointer to the ab8500_charger structure
2801 * Returns IRQ status(IRQ_HANDLED)
2803 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2805 struct ab8500_charger *di = _di;
2807 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2808 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2814 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2815 * @irq: interrupt number
2816 * @_di: pointer to the ab8500_charger structure
2818 * Returns IRQ status(IRQ_HANDLED)
2820 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2822 struct ab8500_charger *di = _di;
2824 dev_dbg(di->dev, "Charger watchdog expired\n");
2827 * The charger that was online when the watchdog expired
2828 * needs to be restarted for charging to start again
2830 if (di->ac.charger_online) {
2831 di->ac.wd_expired = true;
2832 ab8500_power_supply_changed(di, di->ac_chg.psy);
2834 if (di->usb.charger_online) {
2835 di->usb.wd_expired = true;
2836 ab8500_power_supply_changed(di, di->usb_chg.psy);
2843 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2844 * @irq: interrupt number
2845 * @_di: pointer to the ab8500_charger structure
2847 * Returns IRQ status(IRQ_HANDLED)
2849 static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2851 struct ab8500_charger *di = _di;
2853 dev_dbg(di->dev, "VBUS charger drop ended\n");
2854 di->flags.vbus_drop_end = true;
2857 * VBUS might have dropped due to bad connection.
2858 * Schedule a new input limit set to the value SW requests.
2860 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
2861 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
2867 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2868 * @irq: interrupt number
2869 * @_di: pointer to the ab8500_charger structure
2871 * Returns IRQ status(IRQ_HANDLED)
2873 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2875 struct ab8500_charger *di = _di;
2877 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2878 di->flags.vbus_ovv = true;
2879 ab8500_power_supply_changed(di, di->usb_chg.psy);
2881 /* Schedule a new HW failure check */
2882 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2888 * ab8500_charger_ac_get_property() - get the ac/mains properties
2889 * @psy: pointer to the power_supply structure
2890 * @psp: pointer to the power_supply_property structure
2891 * @val: pointer to the power_supply_propval union
2893 * This function gets called when an application tries to get the ac/mains
2894 * properties by reading the sysfs files.
2895 * AC/Mains properties are online, present and voltage.
2896 * online: ac/mains charging is in progress or not
2897 * present: presence of the ac/mains
2898 * voltage: AC/Mains voltage
2899 * Returns error code in case of failure else 0(on success)
2901 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2902 enum power_supply_property psp,
2903 union power_supply_propval *val)
2905 struct ab8500_charger *di;
2908 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2911 case POWER_SUPPLY_PROP_HEALTH:
2912 if (di->flags.mainextchnotok)
2913 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2914 else if (di->ac.wd_expired || di->usb.wd_expired)
2915 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2916 else if (di->flags.main_thermal_prot)
2917 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2919 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2921 case POWER_SUPPLY_PROP_ONLINE:
2922 val->intval = di->ac.charger_online;
2924 case POWER_SUPPLY_PROP_PRESENT:
2925 val->intval = di->ac.charger_connected;
2927 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2928 ret = ab8500_charger_get_ac_voltage(di);
2930 di->ac.charger_voltage_uv = ret;
2931 /* On error, use previous value */
2932 val->intval = di->ac.charger_voltage_uv;
2934 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2936 * This property is used to indicate when CV mode is entered
2937 * for the AC charger
2939 di->ac.cv_active = ab8500_charger_ac_cv(di);
2940 val->intval = di->ac.cv_active;
2942 case POWER_SUPPLY_PROP_CURRENT_NOW:
2943 ret = ab8500_charger_get_ac_current(di);
2945 di->ac.charger_current_ua = ret;
2946 val->intval = di->ac.charger_current_ua;
2955 * ab8500_charger_usb_get_property() - get the usb properties
2956 * @psy: pointer to the power_supply structure
2957 * @psp: pointer to the power_supply_property structure
2958 * @val: pointer to the power_supply_propval union
2960 * This function gets called when an application tries to get the usb
2961 * properties by reading the sysfs files.
2962 * USB properties are online, present and voltage.
2963 * online: usb charging is in progress or not
2964 * present: presence of the usb
2965 * voltage: vbus voltage
2966 * Returns error code in case of failure else 0(on success)
2968 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2969 enum power_supply_property psp,
2970 union power_supply_propval *val)
2972 struct ab8500_charger *di;
2975 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2978 case POWER_SUPPLY_PROP_HEALTH:
2979 if (di->flags.usbchargernotok)
2980 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2981 else if (di->ac.wd_expired || di->usb.wd_expired)
2982 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2983 else if (di->flags.usb_thermal_prot)
2984 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2985 else if (di->flags.vbus_ovv)
2986 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2988 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2990 case POWER_SUPPLY_PROP_ONLINE:
2991 val->intval = di->usb.charger_online;
2993 case POWER_SUPPLY_PROP_PRESENT:
2994 val->intval = di->usb.charger_connected;
2996 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2997 ret = ab8500_charger_get_vbus_voltage(di);
2999 di->usb.charger_voltage_uv = ret;
3000 val->intval = di->usb.charger_voltage_uv;
3002 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
3004 * This property is used to indicate when CV mode is entered
3005 * for the USB charger
3007 di->usb.cv_active = ab8500_charger_usb_cv(di);
3008 val->intval = di->usb.cv_active;
3010 case POWER_SUPPLY_PROP_CURRENT_NOW:
3011 ret = ab8500_charger_get_usb_current(di);
3013 di->usb.charger_current_ua = ret;
3014 val->intval = di->usb.charger_current_ua;
3016 case POWER_SUPPLY_PROP_CURRENT_AVG:
3018 * This property is used to indicate when VBUS has collapsed
3019 * due to too high output current from the USB charger
3021 if (di->flags.vbus_collapse)
3033 * ab8500_charger_init_hw_registers() - Set up charger related registers
3034 * @di: pointer to the ab8500_charger structure
3036 * Set up charger OVV, watchdog and maximum voltage registers as well as
3037 * charging of the backup battery
3039 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3043 /* Setup maximum charger current and voltage for ABB cut2.0 */
3044 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3045 ret = abx500_set_register_interruptible(di->dev,
3047 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3050 "failed to set CH_VOLT_LVL_MAX_REG\n");
3054 ret = abx500_set_register_interruptible(di->dev,
3055 AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3059 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3064 if (is_ab8505_2p0(di->parent))
3065 ret = abx500_mask_and_set_register_interruptible(di->dev,
3067 AB8500_USBCH_CTRL2_REG,
3068 VBUS_AUTO_IN_CURR_LIM_ENA,
3069 VBUS_AUTO_IN_CURR_LIM_ENA);
3072 * VBUS OVV set to 6.3V and enable automatic current limitation
3074 ret = abx500_set_register_interruptible(di->dev,
3076 AB8500_USBCH_CTRL2_REG,
3077 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
3080 "failed to set automatic current limitation\n");
3084 /* Enable main watchdog in OTP */
3085 ret = abx500_set_register_interruptible(di->dev,
3086 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3088 dev_err(di->dev, "failed to enable main WD in OTP\n");
3092 /* Enable main watchdog */
3093 ret = abx500_set_register_interruptible(di->dev,
3094 AB8500_SYS_CTRL2_BLOCK,
3095 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3097 dev_err(di->dev, "failed to enable main watchdog\n");
3102 * Due to internal synchronisation, Enable and Kick watchdog bits
3103 * cannot be enabled in a single write.
3104 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3105 * between writing Enable then Kick bits.
3109 /* Kick main watchdog */
3110 ret = abx500_set_register_interruptible(di->dev,
3111 AB8500_SYS_CTRL2_BLOCK,
3112 AB8500_MAIN_WDOG_CTRL_REG,
3113 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3115 dev_err(di->dev, "failed to kick main watchdog\n");
3119 /* Disable main watchdog */
3120 ret = abx500_set_register_interruptible(di->dev,
3121 AB8500_SYS_CTRL2_BLOCK,
3122 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3124 dev_err(di->dev, "failed to disable main watchdog\n");
3128 /* Set watchdog timeout */
3129 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3130 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3132 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3136 ret = ab8500_charger_led_en(di, false);
3138 dev_err(di->dev, "failed to disable LED\n");
3142 ret = abx500_set_register_interruptible(di->dev,
3144 AB8500_RTC_BACKUP_CHG_REG,
3145 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
3147 dev_err(di->dev, "failed to setup backup battery charging\n");
3151 /* Enable backup battery charging */
3152 ret = abx500_mask_and_set_register_interruptible(di->dev,
3153 AB8500_RTC, AB8500_RTC_CTRL_REG,
3154 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3156 dev_err(di->dev, "%s mask and set failed\n", __func__);
3165 * ab8500 charger driver interrupts and their respective isr
3167 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3168 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3169 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3170 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3171 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3172 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3173 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3174 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3175 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3176 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3177 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3178 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3179 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3180 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
3181 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
3184 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3185 unsigned long event, void *power)
3187 struct ab8500_charger *di =
3188 container_of(nb, struct ab8500_charger, nb);
3189 enum ab8500_usb_state bm_usb_state;
3191 * FIXME: it appears the AB8500 PHY never sends what it should here.
3192 * Fix the PHY driver to properly notify the desired current.
3193 * Also broadcast microampere and not milliampere.
3195 unsigned mA = *((unsigned *)power);
3197 if (event != USB_EVENT_VBUS) {
3198 dev_dbg(di->dev, "not a standard host, returning\n");
3202 /* TODO: State is fabricate here. See if charger really needs USB
3203 * state or if mA is enough
3205 if ((di->usb_state.usb_current_ua == 2000) && (mA > 2))
3206 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3208 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3210 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3211 else if (mA >= 8) /* 8, 100, 500 */
3212 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3213 else /* Should never occur */
3214 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3216 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3217 __func__, bm_usb_state, mA);
3219 spin_lock(&di->usb_state.usb_lock);
3220 di->usb_state.state_tmp = bm_usb_state;
3221 /* FIXME: broadcast ua instead, see above */
3222 di->usb_state.usb_current_tmp_ua = mA * 1000;
3223 spin_unlock(&di->usb_state.usb_lock);
3226 * wait for some time until you get updates from the usb stack
3227 * and negotiations are completed
3229 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
3234 static int __maybe_unused ab8500_charger_resume(struct device *dev)
3237 struct ab8500_charger *di = dev_get_drvdata(dev);
3240 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3241 * logic. That means we have to continuously kick the charger
3242 * watchdog even when no charger is connected. This is only
3243 * valid once the AC charger has been enabled. This is
3244 * a bug that is not handled by the algorithm and the
3245 * watchdog have to be kicked by the charger driver
3246 * when the AC charger is disabled
3248 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3249 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3250 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3252 dev_err(di->dev, "Failed to kick WD!\n");
3254 /* If not already pending start a new timer */
3255 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3256 round_jiffies(WD_KICK_INTERVAL));
3259 /* If we still have a HW failure, schedule a new check */
3260 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3261 queue_delayed_work(di->charger_wq,
3262 &di->check_hw_failure_work, 0);
3265 if (di->flags.vbus_drop_end)
3266 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3271 static int __maybe_unused ab8500_charger_suspend(struct device *dev)
3273 struct ab8500_charger *di = dev_get_drvdata(dev);
3275 /* Cancel any pending jobs */
3276 cancel_delayed_work(&di->check_hw_failure_work);
3277 cancel_delayed_work(&di->vbus_drop_end_work);
3279 flush_delayed_work(&di->attach_work);
3280 flush_delayed_work(&di->usb_charger_attached_work);
3281 flush_delayed_work(&di->ac_charger_attached_work);
3282 flush_delayed_work(&di->check_usbchgnotok_work);
3283 flush_delayed_work(&di->check_vbat_work);
3284 flush_delayed_work(&di->kick_wd_work);
3286 flush_work(&di->usb_link_status_work);
3287 flush_work(&di->ac_work);
3288 flush_work(&di->detect_usb_type_work);
3290 if (atomic_read(&di->current_stepping_sessions))
3296 static char *supply_interface[] = {
3302 static const struct power_supply_desc ab8500_ac_chg_desc = {
3303 .name = "ab8500_ac",
3304 .type = POWER_SUPPLY_TYPE_MAINS,
3305 .properties = ab8500_charger_ac_props,
3306 .num_properties = ARRAY_SIZE(ab8500_charger_ac_props),
3307 .get_property = ab8500_charger_ac_get_property,
3310 static const struct power_supply_desc ab8500_usb_chg_desc = {
3311 .name = "ab8500_usb",
3312 .type = POWER_SUPPLY_TYPE_USB,
3313 .properties = ab8500_charger_usb_props,
3314 .num_properties = ARRAY_SIZE(ab8500_charger_usb_props),
3315 .get_property = ab8500_charger_usb_get_property,
3318 static int ab8500_charger_bind(struct device *dev)
3320 struct ab8500_charger *di = dev_get_drvdata(dev);
3324 /* Create a work queue for the charger */
3325 di->charger_wq = alloc_ordered_workqueue("ab8500_charger_wq",
3327 if (di->charger_wq == NULL) {
3328 dev_err(dev, "failed to create work queue\n");
3332 ch_stat = ab8500_charger_detect_chargers(di, false);
3334 if (ch_stat & AC_PW_CONN) {
3335 if (is_ab8500(di->parent))
3336 queue_delayed_work(di->charger_wq,
3337 &di->ac_charger_attached_work,
3340 if (ch_stat & USB_PW_CONN) {
3341 if (is_ab8500(di->parent))
3342 queue_delayed_work(di->charger_wq,
3343 &di->usb_charger_attached_work,
3345 di->vbus_detected = true;
3346 di->vbus_detected_start = true;
3347 queue_work(di->charger_wq,
3348 &di->detect_usb_type_work);
3351 ret = component_bind_all(dev, di);
3353 dev_err(dev, "can't bind component devices\n");
3354 destroy_workqueue(di->charger_wq);
3361 static void ab8500_charger_unbind(struct device *dev)
3363 struct ab8500_charger *di = dev_get_drvdata(dev);
3366 /* Disable AC charging */
3367 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3369 /* Disable USB charging */
3370 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3372 /* Backup battery voltage and current disable */
3373 ret = abx500_mask_and_set_register_interruptible(di->dev,
3374 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3376 dev_err(di->dev, "%s mask and set failed\n", __func__);
3378 /* Delete the work queue */
3379 destroy_workqueue(di->charger_wq);
3381 /* Unbind fg, btemp, algorithm */
3382 component_unbind_all(dev, di);
3385 static const struct component_master_ops ab8500_charger_comp_ops = {
3386 .bind = ab8500_charger_bind,
3387 .unbind = ab8500_charger_unbind,
3390 static struct platform_driver *const ab8500_charger_component_drivers[] = {
3392 &ab8500_btemp_driver,
3393 &ab8500_chargalg_driver,
3396 static int ab8500_charger_probe(struct platform_device *pdev)
3398 struct device *dev = &pdev->dev;
3399 struct device_node *np = dev->of_node;
3400 struct component_match *match = NULL;
3401 struct power_supply_config ac_psy_cfg = {}, usb_psy_cfg = {};
3402 struct ab8500_charger *di;
3407 di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
3411 di->bm = &ab8500_bm_data;
3413 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3415 /* get parent data */
3417 di->parent = dev_get_drvdata(pdev->dev.parent);
3419 /* Get ADC channels */
3420 if (!is_ab8505(di->parent)) {
3421 di->adc_main_charger_v = devm_iio_channel_get(dev, "main_charger_v");
3422 if (IS_ERR(di->adc_main_charger_v)) {
3423 ret = dev_err_probe(dev, PTR_ERR(di->adc_main_charger_v),
3424 "failed to get ADC main charger voltage\n");
3427 di->adc_main_charger_c = devm_iio_channel_get(dev, "main_charger_c");
3428 if (IS_ERR(di->adc_main_charger_c)) {
3429 ret = dev_err_probe(dev, PTR_ERR(di->adc_main_charger_c),
3430 "failed to get ADC main charger current\n");
3434 di->adc_vbus_v = devm_iio_channel_get(dev, "vbus_v");
3435 if (IS_ERR(di->adc_vbus_v)) {
3436 ret = dev_err_probe(dev, PTR_ERR(di->adc_vbus_v),
3437 "failed to get ADC USB charger voltage\n");
3440 di->adc_usb_charger_c = devm_iio_channel_get(dev, "usb_charger_c");
3441 if (IS_ERR(di->adc_usb_charger_c)) {
3442 ret = dev_err_probe(dev, PTR_ERR(di->adc_usb_charger_c),
3443 "failed to get ADC USB charger current\n");
3448 * VDD ADC supply needs to be enabled from this driver when there
3449 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3450 * interrupts during charging
3452 di->regu = devm_regulator_get(dev, "vddadc");
3453 if (IS_ERR(di->regu)) {
3454 ret = PTR_ERR(di->regu);
3455 dev_err(dev, "failed to get vddadc regulator\n");
3459 /* Request interrupts */
3460 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3461 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3465 ret = devm_request_threaded_irq(dev,
3466 irq, NULL, ab8500_charger_irq[i].isr,
3467 IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
3468 ab8500_charger_irq[i].name, di);
3471 dev_err(dev, "failed to request %s IRQ %d: %d\n"
3472 , ab8500_charger_irq[i].name, irq, ret);
3475 dev_dbg(dev, "Requested %s IRQ %d: %d\n",
3476 ab8500_charger_irq[i].name, irq, ret);
3479 /* initialize lock */
3480 spin_lock_init(&di->usb_state.usb_lock);
3481 mutex_init(&di->usb_ipt_crnt_lock);
3483 di->autopower = false;
3484 di->invalid_charger_detect_state = 0;
3486 /* AC and USB supply config */
3487 ac_psy_cfg.of_node = np;
3488 ac_psy_cfg.supplied_to = supply_interface;
3489 ac_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3490 ac_psy_cfg.drv_data = &di->ac_chg;
3491 usb_psy_cfg.of_node = np;
3492 usb_psy_cfg.supplied_to = supply_interface;
3493 usb_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3494 usb_psy_cfg.drv_data = &di->usb_chg;
3497 /* ux500_charger sub-class */
3498 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
3499 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
3500 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3501 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3502 di->ac_chg.max_out_volt_uv = ab8500_charger_voltage_map[
3503 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3504 di->ac_chg.max_out_curr_ua =
3505 ab8500_charge_output_curr_map[ARRAY_SIZE(ab8500_charge_output_curr_map) - 1];
3506 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
3508 * The AB8505 only supports USB charging. If we are not the
3509 * AB8505, register an AC charger.
3511 * TODO: if this should be opt-in, add DT properties for this.
3513 if (!is_ab8505(di->parent))
3514 di->ac_chg.enabled = true;
3517 /* ux500_charger sub-class */
3518 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
3519 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
3520 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3521 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3522 di->usb_chg.max_out_volt_uv = ab8500_charger_voltage_map[
3523 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3524 di->usb_chg.max_out_curr_ua =
3525 ab8500_charge_output_curr_map[ARRAY_SIZE(ab8500_charge_output_curr_map) - 1];
3526 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
3527 di->usb_state.usb_current_ua = -1;
3529 mutex_init(&di->charger_attached_mutex);
3531 /* Init work for HW failure check */
3532 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
3533 ab8500_charger_check_hw_failure_work);
3534 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
3535 ab8500_charger_check_usbchargernotok_work);
3537 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3538 ab8500_charger_ac_attached_work);
3539 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3540 ab8500_charger_usb_attached_work);
3543 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3544 * logic. That means we have to continuously kick the charger
3545 * watchdog even when no charger is connected. This is only
3546 * valid once the AC charger has been enabled. This is
3547 * a bug that is not handled by the algorithm and the
3548 * watchdog have to be kicked by the charger driver
3549 * when the AC charger is disabled
3551 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
3552 ab8500_charger_kick_watchdog_work);
3554 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
3555 ab8500_charger_check_vbat_work);
3557 INIT_DELAYED_WORK(&di->attach_work,
3558 ab8500_charger_usb_link_attach_work);
3560 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3561 ab8500_charger_usb_state_changed_work);
3563 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3564 ab8500_charger_vbus_drop_end_work);
3566 /* Init work for charger detection */
3567 INIT_WORK(&di->usb_link_status_work,
3568 ab8500_charger_usb_link_status_work);
3569 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3570 INIT_WORK(&di->detect_usb_type_work,
3571 ab8500_charger_detect_usb_type_work);
3573 /* Init work for checking HW status */
3574 INIT_WORK(&di->check_main_thermal_prot_work,
3575 ab8500_charger_check_main_thermal_prot_work);
3576 INIT_WORK(&di->check_usb_thermal_prot_work,
3577 ab8500_charger_check_usb_thermal_prot_work);
3580 /* Initialize OVV, and other registers */
3581 ret = ab8500_charger_init_hw_registers(di);
3583 dev_err(dev, "failed to initialize ABB registers\n");
3587 /* Register AC charger class */
3588 if (di->ac_chg.enabled) {
3589 di->ac_chg.psy = devm_power_supply_register(dev,
3590 &ab8500_ac_chg_desc,
3592 if (IS_ERR(di->ac_chg.psy)) {
3593 dev_err(dev, "failed to register AC charger\n");
3594 return PTR_ERR(di->ac_chg.psy);
3598 /* Register USB charger class */
3599 di->usb_chg.psy = devm_power_supply_register(dev,
3600 &ab8500_usb_chg_desc,
3602 if (IS_ERR(di->usb_chg.psy)) {
3603 dev_err(dev, "failed to register USB charger\n");
3604 return PTR_ERR(di->usb_chg.psy);
3608 * Check what battery we have, since we always have the USB
3609 * psy, use that as a handle.
3611 ret = ab8500_bm_of_probe(di->usb_chg.psy, di->bm);
3613 return dev_err_probe(dev, ret,
3614 "failed to get battery information\n");
3616 /* Identify the connected charger types during startup */
3617 charger_status = ab8500_charger_detect_chargers(di, true);
3618 if (charger_status & AC_PW_CONN) {
3619 di->ac.charger_connected = 1;
3621 ab8500_power_supply_changed(di, di->ac_chg.psy);
3622 sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
3625 platform_set_drvdata(pdev, di);
3627 /* Create something that will match the subdrivers when we bind */
3628 for (i = 0; i < ARRAY_SIZE(ab8500_charger_component_drivers); i++) {
3629 struct device_driver *drv = &ab8500_charger_component_drivers[i]->driver;
3630 struct device *p = NULL, *d;
3632 while ((d = platform_find_device_by_driver(p, drv))) {
3634 component_match_add(dev, &match, component_compare_dev, d);
3640 dev_err(dev, "no matching components\n");
3642 goto remove_ab8500_bm;
3644 if (IS_ERR(match)) {
3645 dev_err(dev, "could not create component match\n");
3646 ret = PTR_ERR(match);
3647 goto remove_ab8500_bm;
3650 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3651 if (IS_ERR_OR_NULL(di->usb_phy)) {
3652 dev_err(dev, "failed to get usb transceiver\n");
3654 goto remove_ab8500_bm;
3656 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3657 ret = usb_register_notifier(di->usb_phy, &di->nb);
3659 dev_err(dev, "failed to register usb notifier\n");
3663 ret = component_master_add_with_match(&pdev->dev,
3664 &ab8500_charger_comp_ops,
3667 dev_err(dev, "failed to add component master\n");
3674 usb_unregister_notifier(di->usb_phy, &di->nb);
3676 usb_put_phy(di->usb_phy);
3678 ab8500_bm_of_remove(di->usb_chg.psy, di->bm);
3682 static int ab8500_charger_remove(struct platform_device *pdev)
3684 struct ab8500_charger *di = platform_get_drvdata(pdev);
3686 component_master_del(&pdev->dev, &ab8500_charger_comp_ops);
3688 usb_unregister_notifier(di->usb_phy, &di->nb);
3689 ab8500_bm_of_remove(di->usb_chg.psy, di->bm);
3690 usb_put_phy(di->usb_phy);
3695 static SIMPLE_DEV_PM_OPS(ab8500_charger_pm_ops, ab8500_charger_suspend, ab8500_charger_resume);
3697 static const struct of_device_id ab8500_charger_match[] = {
3698 { .compatible = "stericsson,ab8500-charger", },
3701 MODULE_DEVICE_TABLE(of, ab8500_charger_match);
3703 static struct platform_driver ab8500_charger_driver = {
3704 .probe = ab8500_charger_probe,
3705 .remove = ab8500_charger_remove,
3707 .name = "ab8500-charger",
3708 .of_match_table = ab8500_charger_match,
3709 .pm = &ab8500_charger_pm_ops,
3713 static int __init ab8500_charger_init(void)
3717 ret = platform_register_drivers(ab8500_charger_component_drivers,
3718 ARRAY_SIZE(ab8500_charger_component_drivers));
3722 ret = platform_driver_register(&ab8500_charger_driver);
3724 platform_unregister_drivers(ab8500_charger_component_drivers,
3725 ARRAY_SIZE(ab8500_charger_component_drivers));
3732 static void __exit ab8500_charger_exit(void)
3734 platform_unregister_drivers(ab8500_charger_component_drivers,
3735 ARRAY_SIZE(ab8500_charger_component_drivers));
3736 platform_driver_unregister(&ab8500_charger_driver);
3739 module_init(ab8500_charger_init);
3740 module_exit(ab8500_charger_exit);
3742 MODULE_LICENSE("GPL v2");
3743 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3744 MODULE_ALIAS("platform:ab8500-charger");
3745 MODULE_DESCRIPTION("AB8500 charger management driver");