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 /* Convert to microvolt, IIO returns millivolt */
491 ret
= iio_read_channel_processed_scale(di
->adc_main_charger_v
,
494 dev_err(di
->dev
, "%s ADC conv failed\n", __func__
);
504 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
505 * @di: pointer to the ab8500_charger structure
507 * Returns ac charger CV mode (on success) else error code
509 static int ab8500_charger_ac_cv(struct ab8500_charger
*di
)
514 /* Only check CV mode if the charger is online */
515 if (di
->ac
.charger_online
) {
516 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
517 AB8500_CH_STATUS1_REG
, &val
);
519 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
523 if (val
& MAIN_CH_CV_ON
)
533 * ab8500_charger_get_vbus_voltage() - get vbus voltage
534 * @di: pointer to the ab8500_charger structure
536 * This function returns the vbus voltage.
537 * Returns vbus voltage in microvolt (on success)
539 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger
*di
)
543 /* Only measure voltage if the charger is connected */
544 if (di
->usb
.charger_connected
) {
545 /* Convert to microvolt, IIO returns millivolt */
546 ret
= iio_read_channel_processed_scale(di
->adc_vbus_v
,
549 dev_err(di
->dev
, "%s ADC conv failed\n", __func__
);
559 * ab8500_charger_get_usb_current() - get usb charger current
560 * @di: pointer to the ab8500_charger structure
562 * This function returns the usb charger current.
563 * Returns usb current in microamperes (on success) and error code on failure
565 static int ab8500_charger_get_usb_current(struct ab8500_charger
*di
)
569 /* Only measure current if the charger is online */
570 if (di
->usb
.charger_online
) {
571 /* Return microamperes */
572 ret
= iio_read_channel_processed_scale(di
->adc_usb_charger_c
,
575 dev_err(di
->dev
, "%s ADC conv failed\n", __func__
);
585 * ab8500_charger_get_ac_current() - get ac charger current
586 * @di: pointer to the ab8500_charger structure
588 * This function returns the ac charger current.
589 * Returns ac current in microamperes (on success) and error code on failure.
591 static int ab8500_charger_get_ac_current(struct ab8500_charger
*di
)
595 /* Only measure current if the charger is online */
596 if (di
->ac
.charger_online
) {
597 /* Return microamperes */
598 ret
= iio_read_channel_processed_scale(di
->adc_main_charger_c
,
601 dev_err(di
->dev
, "%s ADC conv failed\n", __func__
);
611 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
612 * @di: pointer to the ab8500_charger structure
614 * Returns ac charger CV mode (on success) else error code
616 static int ab8500_charger_usb_cv(struct ab8500_charger
*di
)
621 /* Only check CV mode if the charger is online */
622 if (di
->usb
.charger_online
) {
623 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
624 AB8500_CH_USBCH_STAT1_REG
, &val
);
626 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
630 if (val
& USB_CH_CV_ON
)
642 * ab8500_charger_detect_chargers() - Detect the connected chargers
643 * @di: pointer to the ab8500_charger structure
644 * @probe: if probe, don't delay and wait for HW
646 * Returns the type of charger connected.
647 * For USB it will not mean we can actually charge from it
648 * but that there is a USB cable connected that we have to
649 * identify. This is used during startup when we don't get
650 * interrupts of the charger detection
652 * Returns an integer value, that means,
653 * NO_PW_CONN no power supply is connected
654 * AC_PW_CONN if the AC power supply is connected
655 * USB_PW_CONN if the USB power supply is connected
656 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
658 static int ab8500_charger_detect_chargers(struct ab8500_charger
*di
, bool probe
)
660 int result
= NO_PW_CONN
;
664 /* Check for AC charger */
665 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
666 AB8500_CH_STATUS1_REG
, &val
);
668 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
672 if (val
& MAIN_CH_DET
)
675 /* Check for USB charger */
679 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
680 * when disconnecting ACA even though no
681 * charger was connected. Try waiting a little
682 * longer than the 100 ms of VBUS_DET_DBNC100...
686 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
687 AB8500_CH_USBCH_STAT1_REG
, &val
);
689 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
693 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__
,
695 if ((val
& VBUS_DET_DBNC1
) && (val
& VBUS_DET_DBNC100
))
696 result
|= USB_PW_CONN
;
702 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
703 * @di: pointer to the ab8500_charger structure
704 * @link_status: the identified USB type
706 * Get the maximum current that is allowed to be drawn from the host
707 * based on the USB type.
708 * Returns error code in case of failure else 0 on success
710 static int ab8500_charger_max_usb_curr(struct ab8500_charger
*di
,
711 enum ab8500_charger_link_status link_status
)
715 di
->usb_device_is_unrecognised
= false;
718 * Platform only supports USB 2.0.
719 * This means that charging current from USB source
720 * is maximum 500 mA. Every occurrence of USB_STAT_*_HOST_*
721 * should set USB_CH_IP_CUR_LVL_0P5.
724 switch (link_status
) {
725 case USB_STAT_STD_HOST_NC
:
726 case USB_STAT_STD_HOST_C_NS
:
727 case USB_STAT_STD_HOST_C_S
:
728 dev_dbg(di
->dev
, "USB Type - Standard host is "
729 "detected through USB driver\n");
730 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P5
;
733 case USB_STAT_HOST_CHG_HS_CHIRP
:
734 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P5
;
737 case USB_STAT_HOST_CHG_HS
:
738 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P5
;
741 case USB_STAT_ACA_RID_C_HS
:
742 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P9
;
745 case USB_STAT_ACA_RID_A
:
747 * Dedicated charger level minus maximum current accessory
748 * can consume (900mA). Closest level is 500mA
750 dev_dbg(di
->dev
, "USB_STAT_ACA_RID_A detected\n");
751 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P5
;
754 case USB_STAT_ACA_RID_B
:
756 * Dedicated charger level minus 120mA (20mA for ACA and
757 * 100mA for potential accessory). Closest level is 1300mA
759 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_1P3
;
760 dev_dbg(di
->dev
, "USB Type - 0x%02x MaxCurr: %d", link_status
,
761 di
->max_usb_in_curr
.usb_type_max_ua
);
764 case USB_STAT_HOST_CHG_NM
:
765 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P5
;
768 case USB_STAT_DEDICATED_CHG
:
769 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_1P5
;
772 case USB_STAT_ACA_RID_C_HS_CHIRP
:
773 case USB_STAT_ACA_RID_C_NM
:
774 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_1P5
;
777 case USB_STAT_NOT_CONFIGURED
:
778 if (di
->vbus_detected
) {
779 di
->usb_device_is_unrecognised
= true;
780 dev_dbg(di
->dev
, "USB Type - Legacy charger.\n");
781 di
->max_usb_in_curr
.usb_type_max_ua
=
782 USB_CH_IP_CUR_LVL_1P5
;
786 case USB_STAT_HM_IDGND
:
787 dev_err(di
->dev
, "USB Type - Charging not allowed\n");
788 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P05
;
791 case USB_STAT_RESERVED
:
792 if (is_ab8500(di
->parent
)) {
793 di
->flags
.vbus_collapse
= true;
794 dev_err(di
->dev
, "USB Type - USB_STAT_RESERVED "
795 "VBUS has collapsed\n");
799 dev_dbg(di
->dev
, "USB Type - Charging not allowed\n");
800 di
->max_usb_in_curr
.usb_type_max_ua
=
801 USB_CH_IP_CUR_LVL_0P05
;
802 dev_dbg(di
->dev
, "USB Type - 0x%02x MaxCurr: %d",
804 di
->max_usb_in_curr
.usb_type_max_ua
);
808 case USB_STAT_CARKIT_1
:
809 case USB_STAT_CARKIT_2
:
810 case USB_STAT_ACA_DOCK_CHARGER
:
811 case USB_STAT_CHARGER_LINE_1
:
812 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P5
;
813 dev_dbg(di
->dev
, "USB Type - 0x%02x MaxCurr: %d", link_status
,
814 di
->max_usb_in_curr
.usb_type_max_ua
);
816 case USB_STAT_NOT_VALID_LINK
:
817 dev_err(di
->dev
, "USB Type invalid - try charging anyway\n");
818 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P5
;
822 dev_err(di
->dev
, "USB Type - Unknown\n");
823 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P05
;
828 di
->max_usb_in_curr
.set_max_ua
= di
->max_usb_in_curr
.usb_type_max_ua
;
829 dev_dbg(di
->dev
, "USB Type - 0x%02x MaxCurr: %d",
830 link_status
, di
->max_usb_in_curr
.set_max_ua
);
836 * ab8500_charger_read_usb_type() - read the type of usb connected
837 * @di: pointer to the ab8500_charger structure
839 * Detect the type of the plugged USB
840 * Returns error code in case of failure else 0 on success
842 static int ab8500_charger_read_usb_type(struct ab8500_charger
*di
)
847 ret
= abx500_get_register_interruptible(di
->dev
,
848 AB8500_INTERRUPT
, AB8500_IT_SOURCE21_REG
, &val
);
850 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
853 if (is_ab8500(di
->parent
))
854 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_USB
,
855 AB8500_USB_LINE_STAT_REG
, &val
);
857 ret
= abx500_get_register_interruptible(di
->dev
,
858 AB8500_USB
, AB8500_USB_LINK1_STAT_REG
, &val
);
860 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
864 /* get the USB type */
865 if (is_ab8500(di
->parent
))
866 val
= (val
& AB8500_USB_LINK_STATUS
) >> USB_LINK_STATUS_SHIFT
;
868 val
= (val
& AB8505_USB_LINK_STATUS
) >> USB_LINK_STATUS_SHIFT
;
869 ret
= ab8500_charger_max_usb_curr(di
,
870 (enum ab8500_charger_link_status
) val
);
876 * ab8500_charger_detect_usb_type() - get the type of usb connected
877 * @di: pointer to the ab8500_charger structure
879 * Detect the type of the plugged USB
880 * Returns error code in case of failure else 0 on success
882 static int ab8500_charger_detect_usb_type(struct ab8500_charger
*di
)
888 * On getting the VBUS rising edge detect interrupt there
889 * is a 250ms delay after which the register UsbLineStatus
890 * is filled with valid data.
892 for (i
= 0; i
< 10; i
++) {
894 ret
= abx500_get_register_interruptible(di
->dev
,
895 AB8500_INTERRUPT
, AB8500_IT_SOURCE21_REG
,
897 dev_dbg(di
->dev
, "%s AB8500_IT_SOURCE21_REG %x\n",
900 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
904 if (is_ab8500(di
->parent
))
905 ret
= abx500_get_register_interruptible(di
->dev
,
906 AB8500_USB
, AB8500_USB_LINE_STAT_REG
, &val
);
908 ret
= abx500_get_register_interruptible(di
->dev
,
909 AB8500_USB
, AB8500_USB_LINK1_STAT_REG
, &val
);
911 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
914 dev_dbg(di
->dev
, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__
,
917 * Until the IT source register is read the UsbLineStatus
918 * register is not updated, hence doing the same
922 /* get the USB type */
923 if (is_ab8500(di
->parent
))
924 val
= (val
& AB8500_USB_LINK_STATUS
) >>
925 USB_LINK_STATUS_SHIFT
;
927 val
= (val
& AB8505_USB_LINK_STATUS
) >>
928 USB_LINK_STATUS_SHIFT
;
932 ret
= ab8500_charger_max_usb_curr(di
,
933 (enum ab8500_charger_link_status
) val
);
939 * This array maps the raw hex value to charger voltage used by the AB8500
940 * Values taken from the UM0836, in microvolt.
942 static int ab8500_charger_voltage_map
[] = {
1023 static int ab8500_voltage_to_regval(int voltage_uv
)
1027 /* Special case for voltage below 3.5V */
1028 if (voltage_uv
< ab8500_charger_voltage_map
[0])
1029 return LOW_VOLT_REG
;
1031 for (i
= 1; i
< ARRAY_SIZE(ab8500_charger_voltage_map
); i
++) {
1032 if (voltage_uv
< ab8500_charger_voltage_map
[i
])
1036 /* If not last element, return error */
1037 i
= ARRAY_SIZE(ab8500_charger_voltage_map
) - 1;
1038 if (voltage_uv
== ab8500_charger_voltage_map
[i
])
1044 /* This array maps the raw register value to charger input current */
1045 static int ab8500_charge_input_curr_map
[] = {
1046 50000, 98000, 193000, 290000, 380000, 450000, 500000, 600000,
1047 700000, 800000, 900000, 1000000, 1100000, 1300000, 1400000, 1500000,
1050 /* This array maps the raw register value to charger output current */
1051 static int ab8500_charge_output_curr_map
[] = {
1052 100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000,
1053 900000, 1000000, 1100000, 1200000, 1300000, 1400000, 1500000, 1500000,
1056 static int ab8500_current_to_regval(struct ab8500_charger
*di
, int curr_ua
)
1060 if (curr_ua
< ab8500_charge_output_curr_map
[0])
1063 for (i
= 0; i
< ARRAY_SIZE(ab8500_charge_output_curr_map
); i
++) {
1064 if (curr_ua
< ab8500_charge_output_curr_map
[i
])
1068 /* If not last element, return error */
1069 i
= ARRAY_SIZE(ab8500_charge_output_curr_map
) - 1;
1070 if (curr_ua
== ab8500_charge_output_curr_map
[i
])
1076 static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger
*di
, int curr_ua
)
1080 if (curr_ua
< ab8500_charge_input_curr_map
[0])
1083 for (i
= 0; i
< ARRAY_SIZE(ab8500_charge_input_curr_map
); i
++) {
1084 if (curr_ua
< ab8500_charge_input_curr_map
[i
])
1088 /* If not last element, return error */
1089 i
= ARRAY_SIZE(ab8500_charge_input_curr_map
) - 1;
1090 if (curr_ua
== ab8500_charge_input_curr_map
[i
])
1097 * ab8500_charger_get_usb_cur() - get usb current
1098 * @di: pointer to the ab8500_charger structure
1100 * The usb stack provides the maximum current that can be drawn from
1101 * the standard usb host. This will be in uA.
1102 * This function converts current in uA to a value that can be written
1103 * to the register. Returns -1 if charging is not allowed
1105 static int ab8500_charger_get_usb_cur(struct ab8500_charger
*di
)
1108 switch (di
->usb_state
.usb_current_ua
) {
1110 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P09
;
1113 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P19
;
1116 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P29
;
1119 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P38
;
1122 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P5
;
1125 di
->max_usb_in_curr
.usb_type_max_ua
= USB_CH_IP_CUR_LVL_0P05
;
1129 di
->max_usb_in_curr
.set_max_ua
= di
->max_usb_in_curr
.usb_type_max_ua
;
1134 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1135 * @di: pointer to the ab8500_charger structure
1136 * @reg: select what charger register to check
1138 * Check if current stepping should be allowed to continue.
1139 * Checks if charger source has not collapsed. If it has, further stepping
1142 static bool ab8500_charger_check_continue_stepping(struct ab8500_charger
*di
,
1145 if (reg
== AB8500_USBCH_IPT_CRNTLVL_REG
)
1146 return !di
->flags
.vbus_drop_end
;
1152 * ab8500_charger_set_current() - set charger current
1153 * @di: pointer to the ab8500_charger structure
1154 * @ich_ua: charger current, in uA
1155 * @reg: select what charger register to set
1157 * Set charger current.
1158 * There is no state machine in the AB to step up/down the charger
1159 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1160 * charging is started. Instead we need to implement
1161 * this charger current step-up/down here.
1162 * Returns error code in case of failure else 0(on success)
1164 static int ab8500_charger_set_current(struct ab8500_charger
*di
,
1165 int ich_ua
, int reg
)
1168 int curr_index
, prev_curr_index
, shift_value
, i
;
1171 bool no_stepping
= false;
1173 atomic_inc(&di
->current_stepping_sessions
);
1175 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
1178 dev_err(di
->dev
, "%s read failed\n", __func__
);
1179 goto exit_set_current
;
1183 case AB8500_MCH_IPT_CURLVL_REG
:
1184 shift_value
= MAIN_CH_INPUT_CURR_SHIFT
;
1185 prev_curr_index
= (reg_value
>> shift_value
);
1186 curr_index
= ab8500_current_to_regval(di
, ich_ua
);
1187 step_udelay
= STEP_UDELAY
;
1188 if (!di
->ac
.charger_connected
)
1191 case AB8500_USBCH_IPT_CRNTLVL_REG
:
1192 shift_value
= VBUS_IN_CURR_LIM_SHIFT
;
1193 prev_curr_index
= (reg_value
>> shift_value
);
1194 curr_index
= ab8500_vbus_in_curr_to_regval(di
, ich_ua
);
1195 step_udelay
= STEP_UDELAY
* 100;
1197 if (!di
->usb
.charger_connected
)
1200 case AB8500_CH_OPT_CRNTLVL_REG
:
1202 prev_curr_index
= (reg_value
>> shift_value
);
1203 curr_index
= ab8500_current_to_regval(di
, ich_ua
);
1204 step_udelay
= STEP_UDELAY
;
1205 if (curr_index
&& (curr_index
- prev_curr_index
) > 1)
1208 if (!di
->usb
.charger_connected
&& !di
->ac
.charger_connected
)
1213 dev_err(di
->dev
, "%s current register not valid\n", __func__
);
1215 goto exit_set_current
;
1218 if (curr_index
< 0) {
1219 dev_err(di
->dev
, "requested current limit out-of-range\n");
1221 goto exit_set_current
;
1224 /* only update current if it's been changed */
1225 if (prev_curr_index
== curr_index
) {
1226 dev_dbg(di
->dev
, "%s current not changed for reg: 0x%02x\n",
1229 goto exit_set_current
;
1232 dev_dbg(di
->dev
, "%s set charger current: %d uA for reg: 0x%02x\n",
1233 __func__
, ich_ua
, reg
);
1236 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1237 reg
, (u8
)curr_index
<< shift_value
);
1239 dev_err(di
->dev
, "%s write failed\n", __func__
);
1240 } else if (prev_curr_index
> curr_index
) {
1241 for (i
= prev_curr_index
- 1; i
>= curr_index
; i
--) {
1242 dev_dbg(di
->dev
, "curr change_1 to: %x for 0x%02x\n",
1243 (u8
) i
<< shift_value
, reg
);
1244 ret
= abx500_set_register_interruptible(di
->dev
,
1245 AB8500_CHARGER
, reg
, (u8
)i
<< shift_value
);
1247 dev_err(di
->dev
, "%s write failed\n", __func__
);
1248 goto exit_set_current
;
1250 if (i
!= curr_index
)
1251 usleep_range(step_udelay
, step_udelay
* 2);
1255 for (i
= prev_curr_index
+ 1; i
<= curr_index
&& allow
; i
++) {
1256 dev_dbg(di
->dev
, "curr change_2 to: %x for 0x%02x\n",
1257 (u8
)i
<< shift_value
, reg
);
1258 ret
= abx500_set_register_interruptible(di
->dev
,
1259 AB8500_CHARGER
, reg
, (u8
)i
<< shift_value
);
1261 dev_err(di
->dev
, "%s write failed\n", __func__
);
1262 goto exit_set_current
;
1264 if (i
!= curr_index
)
1265 usleep_range(step_udelay
, step_udelay
* 2);
1267 allow
= ab8500_charger_check_continue_stepping(di
, reg
);
1272 atomic_dec(&di
->current_stepping_sessions
);
1278 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1279 * @di: pointer to the ab8500_charger structure
1280 * @ich_in_ua: charger input current limit in microampere
1282 * Sets the current that can be drawn from the USB host
1283 * Returns error code in case of failure else 0(on success)
1285 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger
*di
,
1291 /* We should always use to lowest current limit */
1292 min_value
= min(di
->bm
->chg_params
->usb_curr_max_ua
, ich_in_ua
);
1293 if (di
->max_usb_in_curr
.set_max_ua
> 0)
1294 min_value
= min(di
->max_usb_in_curr
.set_max_ua
, min_value
);
1296 if (di
->usb_state
.usb_current_ua
>= 0)
1297 min_value
= min(di
->usb_state
.usb_current_ua
, min_value
);
1299 switch (min_value
) {
1301 if (di
->vbat
< VBAT_TRESH_IP_CUR_RED
)
1302 min_value
= USB_CH_IP_CUR_LVL_0P05
;
1305 if (di
->vbat
< VBAT_TRESH_IP_CUR_RED
)
1306 min_value
= USB_CH_IP_CUR_LVL_0P45
;
1312 dev_info(di
->dev
, "VBUS input current limit set to %d uA\n", min_value
);
1314 mutex_lock(&di
->usb_ipt_crnt_lock
);
1315 ret
= ab8500_charger_set_current(di
, min_value
,
1316 AB8500_USBCH_IPT_CRNTLVL_REG
);
1317 mutex_unlock(&di
->usb_ipt_crnt_lock
);
1323 * ab8500_charger_set_main_in_curr() - set main charger input current
1324 * @di: pointer to the ab8500_charger structure
1325 * @ich_in_ua: input charger current, in uA
1327 * Set main charger input current.
1328 * Returns error code in case of failure else 0(on success)
1330 static int ab8500_charger_set_main_in_curr(struct ab8500_charger
*di
,
1333 return ab8500_charger_set_current(di
, ich_in_ua
,
1334 AB8500_MCH_IPT_CURLVL_REG
);
1338 * ab8500_charger_set_output_curr() - set charger output current
1339 * @di: pointer to the ab8500_charger structure
1340 * @ich_out_ua: output charger current, in uA
1342 * Set charger output current.
1343 * Returns error code in case of failure else 0(on success)
1345 static int ab8500_charger_set_output_curr(struct ab8500_charger
*di
,
1348 return ab8500_charger_set_current(di
, ich_out_ua
,
1349 AB8500_CH_OPT_CRNTLVL_REG
);
1353 * ab8500_charger_led_en() - turn on/off chargign led
1354 * @di: pointer to the ab8500_charger structure
1355 * @on: flag to turn on/off the chargign led
1357 * Power ON/OFF charging LED indication
1358 * Returns error code in case of failure else 0(on success)
1360 static int ab8500_charger_led_en(struct ab8500_charger
*di
, int on
)
1365 /* Power ON charging LED indicator, set LED current to 5mA */
1366 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1367 AB8500_LED_INDICATOR_PWM_CTRL
,
1368 (LED_IND_CUR_5MA
| LED_INDICATOR_PWM_ENA
));
1370 dev_err(di
->dev
, "Power ON LED failed\n");
1373 /* LED indicator PWM duty cycle 252/256 */
1374 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1375 AB8500_LED_INDICATOR_PWM_DUTY
,
1376 LED_INDICATOR_PWM_DUTY_252_256
);
1378 dev_err(di
->dev
, "Set LED PWM duty cycle failed\n");
1382 /* Power off charging LED indicator */
1383 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1384 AB8500_LED_INDICATOR_PWM_CTRL
,
1385 LED_INDICATOR_PWM_DIS
);
1387 dev_err(di
->dev
, "Power-off LED failed\n");
1396 * ab8500_charger_ac_en() - enable or disable ac charging
1397 * @di: pointer to the ab8500_charger structure
1398 * @enable: enable/disable flag
1399 * @vset_uv: charging voltage in microvolt
1400 * @iset_ua: charging current in microampere
1402 * Enable/Disable AC/Mains charging and turns on/off the charging led
1405 static int ab8500_charger_ac_en(struct ux500_charger
*charger
,
1406 int enable
, int vset_uv
, int iset_ua
)
1411 int input_curr_index
;
1414 struct ab8500_charger
*di
= to_ab8500_charger_ac_device_info(charger
);
1417 /* Check if AC is connected */
1418 if (!di
->ac
.charger_connected
) {
1419 dev_err(di
->dev
, "AC charger not connected\n");
1423 /* Enable AC charging */
1424 dev_dbg(di
->dev
, "Enable AC: %duV %duA\n", vset_uv
, iset_ua
);
1427 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1428 * will be triggered every time we enable the VDD ADC supply.
1429 * This will turn off charging for a short while.
1430 * It can be avoided by having the supply on when
1431 * there is a charger enabled. Normally the VDD ADC supply
1432 * is enabled every time a GPADC conversion is triggered.
1433 * We will force it to be enabled from this driver to have
1434 * the GPADC module independent of the AB8500 chargers
1436 if (!di
->vddadc_en_ac
) {
1437 ret
= regulator_enable(di
->regu
);
1440 "Failed to enable regulator\n");
1442 di
->vddadc_en_ac
= true;
1445 /* Check if the requested voltage or current is valid */
1446 volt_index
= ab8500_voltage_to_regval(vset_uv
);
1447 curr_index
= ab8500_current_to_regval(di
, iset_ua
);
1448 input_curr_index
= ab8500_current_to_regval(di
,
1449 di
->bm
->chg_params
->ac_curr_max_ua
);
1450 if (volt_index
< 0 || curr_index
< 0 || input_curr_index
< 0) {
1452 "Charger voltage or current too high, "
1453 "charging not started\n");
1457 /* ChVoltLevel: maximum battery charging voltage */
1458 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1459 AB8500_CH_VOLT_LVL_REG
, (u8
) volt_index
);
1461 dev_err(di
->dev
, "%s write failed\n", __func__
);
1464 /* MainChInputCurr: current that can be drawn from the charger*/
1465 ret
= ab8500_charger_set_main_in_curr(di
,
1466 di
->bm
->chg_params
->ac_curr_max_ua
);
1468 dev_err(di
->dev
, "%s Failed to set MainChInputCurr\n",
1472 /* ChOutputCurentLevel: protected output current */
1473 ret
= ab8500_charger_set_output_curr(di
, iset_ua
);
1475 dev_err(di
->dev
, "%s "
1476 "Failed to set ChOutputCurentLevel\n",
1481 /* Check if VBAT overshoot control should be enabled */
1482 if (!di
->bm
->enable_overshoot
)
1483 overshoot
= MAIN_CH_NO_OVERSHOOT_ENA_N
;
1485 /* Enable Main Charger */
1486 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1487 AB8500_MCH_CTRL1
, MAIN_CH_ENA
| overshoot
);
1489 dev_err(di
->dev
, "%s write failed\n", __func__
);
1493 /* Power on charging LED indication */
1494 ret
= ab8500_charger_led_en(di
, true);
1496 dev_err(di
->dev
, "failed to enable LED\n");
1498 di
->ac
.charger_online
= 1;
1500 /* Disable AC charging */
1501 if (is_ab8500_1p1_or_earlier(di
->parent
)) {
1503 * For ABB revision 1.0 and 1.1 there is a bug in the
1504 * watchdog logic. That means we have to continuously
1505 * kick the charger watchdog even when no charger is
1506 * connected. This is only valid once the AC charger
1507 * has been enabled. This is a bug that is not handled
1508 * by the algorithm and the watchdog have to be kicked
1509 * by the charger driver when the AC charger
1513 queue_delayed_work(di
->charger_wq
,
1515 round_jiffies(WD_KICK_INTERVAL
));
1519 * We can't turn off charging completely
1520 * due to a bug in AB8500 cut1.
1521 * If we do, charging will not start again.
1522 * That is why we set the lowest voltage
1523 * and current possible
1525 ret
= abx500_set_register_interruptible(di
->dev
,
1527 AB8500_CH_VOLT_LVL_REG
, CH_VOL_LVL_3P5
);
1530 "%s write failed\n", __func__
);
1534 ret
= ab8500_charger_set_output_curr(di
, 0);
1536 dev_err(di
->dev
, "%s "
1537 "Failed to set ChOutputCurentLevel\n",
1542 ret
= abx500_set_register_interruptible(di
->dev
,
1544 AB8500_MCH_CTRL1
, 0);
1547 "%s write failed\n", __func__
);
1552 ret
= ab8500_charger_led_en(di
, false);
1554 dev_err(di
->dev
, "failed to disable LED\n");
1556 di
->ac
.charger_online
= 0;
1557 di
->ac
.wd_expired
= false;
1559 /* Disable regulator if enabled */
1560 if (di
->vddadc_en_ac
) {
1561 regulator_disable(di
->regu
);
1562 di
->vddadc_en_ac
= false;
1565 dev_dbg(di
->dev
, "%s Disabled AC charging\n", __func__
);
1567 ab8500_power_supply_changed(di
, di
->ac_chg
.psy
);
1573 * ab8500_charger_usb_en() - enable usb charging
1574 * @di: pointer to the ab8500_charger structure
1575 * @enable: enable/disable flag
1576 * @vset_uv: charging voltage in microvolt
1577 * @ich_out_ua: charger output current in microampere
1579 * Enable/Disable USB charging and turns on/off the charging led respectively.
1580 * Returns error code in case of failure else 0(on success)
1582 static int ab8500_charger_usb_en(struct ux500_charger
*charger
,
1583 int enable
, int vset_uv
, int ich_out_ua
)
1590 struct ab8500_charger
*di
= to_ab8500_charger_usb_device_info(charger
);
1593 /* Check if USB is connected */
1594 if (!di
->usb
.charger_connected
) {
1595 dev_err(di
->dev
, "USB charger not connected\n");
1600 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1601 * will be triggered every time we enable the VDD ADC supply.
1602 * This will turn off charging for a short while.
1603 * It can be avoided by having the supply on when
1604 * there is a charger enabled. Normally the VDD ADC supply
1605 * is enabled every time a GPADC conversion is triggered.
1606 * We will force it to be enabled from this driver to have
1607 * the GPADC module independent of the AB8500 chargers
1609 if (!di
->vddadc_en_usb
) {
1610 ret
= regulator_enable(di
->regu
);
1613 "Failed to enable regulator\n");
1615 di
->vddadc_en_usb
= true;
1618 /* Enable USB charging */
1619 dev_dbg(di
->dev
, "Enable USB: %d uV %d uA\n", vset_uv
, ich_out_ua
);
1621 /* Check if the requested voltage or current is valid */
1622 volt_index
= ab8500_voltage_to_regval(vset_uv
);
1623 curr_index
= ab8500_current_to_regval(di
, ich_out_ua
);
1624 if (volt_index
< 0 || curr_index
< 0) {
1626 "Charger voltage or current too high, "
1627 "charging not started\n");
1632 * ChVoltLevel: max voltage up to which battery can be
1635 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1636 AB8500_CH_VOLT_LVL_REG
, (u8
) volt_index
);
1638 dev_err(di
->dev
, "%s write failed\n", __func__
);
1641 /* Check if VBAT overshoot control should be enabled */
1642 if (!di
->bm
->enable_overshoot
)
1643 overshoot
= USB_CHG_NO_OVERSHOOT_ENA_N
;
1645 /* Enable USB Charger */
1647 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
1648 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1649 AB8500_USBCH_CTRL1_REG
, USB_CH_ENA
| overshoot
);
1651 dev_err(di
->dev
, "%s write failed\n", __func__
);
1655 /* If success power on charging LED indication */
1656 ret
= ab8500_charger_led_en(di
, true);
1658 dev_err(di
->dev
, "failed to enable LED\n");
1660 di
->usb
.charger_online
= 1;
1662 /* USBChInputCurr: current that can be drawn from the usb */
1663 ret
= ab8500_charger_set_vbus_in_curr(di
,
1664 di
->max_usb_in_curr
.usb_type_max_ua
);
1666 dev_err(di
->dev
, "setting USBChInputCurr failed\n");
1670 /* ChOutputCurentLevel: protected output current */
1671 ret
= ab8500_charger_set_output_curr(di
, ich_out_ua
);
1673 dev_err(di
->dev
, "%s "
1674 "Failed to set ChOutputCurentLevel\n",
1679 queue_delayed_work(di
->charger_wq
, &di
->check_vbat_work
, HZ
);
1682 /* Disable USB charging */
1683 dev_dbg(di
->dev
, "%s Disabled USB charging\n", __func__
);
1684 ret
= abx500_set_register_interruptible(di
->dev
,
1686 AB8500_USBCH_CTRL1_REG
, 0);
1689 "%s write failed\n", __func__
);
1693 ret
= ab8500_charger_led_en(di
, false);
1695 dev_err(di
->dev
, "failed to disable LED\n");
1696 /* USBChInputCurr: current that can be drawn from the usb */
1697 ret
= ab8500_charger_set_vbus_in_curr(di
, 0);
1699 dev_err(di
->dev
, "setting USBChInputCurr failed\n");
1703 /* ChOutputCurentLevel: protected output current */
1704 ret
= ab8500_charger_set_output_curr(di
, 0);
1706 dev_err(di
->dev
, "%s "
1707 "Failed to reset ChOutputCurentLevel\n",
1711 di
->usb
.charger_online
= 0;
1712 di
->usb
.wd_expired
= false;
1714 /* Disable regulator if enabled */
1715 if (di
->vddadc_en_usb
) {
1716 regulator_disable(di
->regu
);
1717 di
->vddadc_en_usb
= false;
1720 dev_dbg(di
->dev
, "%s Disabled USB charging\n", __func__
);
1722 /* Cancel any pending Vbat check work */
1723 cancel_delayed_work(&di
->check_vbat_work
);
1726 ab8500_power_supply_changed(di
, di
->usb_chg
.psy
);
1732 * ab8500_charger_usb_check_enable() - enable usb charging
1733 * @charger: pointer to the ux500_charger structure
1734 * @vset_uv: charging voltage in microvolt
1735 * @iset_ua: charger output current in microampere
1737 * Check if the VBUS charger has been disconnected and reconnected without
1738 * AB8500 rising an interrupt. Returns 0 on success.
1740 static int ab8500_charger_usb_check_enable(struct ux500_charger
*charger
,
1741 int vset_uv
, int iset_ua
)
1746 struct ab8500_charger
*di
= to_ab8500_charger_usb_device_info(charger
);
1748 if (!di
->usb
.charger_connected
)
1751 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
1752 AB8500_USBCH_CTRL1_REG
, &usbch_ctrl1
);
1754 dev_err(di
->dev
, "ab8500 read failed %d\n", __LINE__
);
1757 dev_dbg(di
->dev
, "USB charger ctrl: 0x%02x\n", usbch_ctrl1
);
1759 if (!(usbch_ctrl1
& USB_CH_ENA
)) {
1760 dev_info(di
->dev
, "Charging has been disabled abnormally and will be re-enabled\n");
1762 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
1763 AB8500_CHARGER
, AB8500_CHARGER_CTRL
,
1764 DROP_COUNT_RESET
, DROP_COUNT_RESET
);
1766 dev_err(di
->dev
, "ab8500 write failed %d\n", __LINE__
);
1770 ret
= ab8500_charger_usb_en(&di
->usb_chg
, true, vset_uv
, iset_ua
);
1772 dev_err(di
->dev
, "Failed to enable VBUS charger %d\n",
1781 * ab8500_charger_ac_check_enable() - enable usb charging
1782 * @charger: pointer to the ux500_charger structure
1783 * @vset_uv: charging voltage in microvolt
1784 * @iset_ua: charger output current in micrompere
1786 * Check if the AC charger has been disconnected and reconnected without
1787 * AB8500 rising an interrupt. Returns 0 on success.
1789 static int ab8500_charger_ac_check_enable(struct ux500_charger
*charger
,
1790 int vset_uv
, int iset_ua
)
1792 u8 mainch_ctrl1
= 0;
1795 struct ab8500_charger
*di
= to_ab8500_charger_ac_device_info(charger
);
1797 if (!di
->ac
.charger_connected
)
1800 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
1801 AB8500_MCH_CTRL1
, &mainch_ctrl1
);
1803 dev_err(di
->dev
, "ab8500 read failed %d\n", __LINE__
);
1806 dev_dbg(di
->dev
, "AC charger ctrl: 0x%02x\n", mainch_ctrl1
);
1808 if (!(mainch_ctrl1
& MAIN_CH_ENA
)) {
1809 dev_info(di
->dev
, "Charging has been disabled abnormally and will be re-enabled\n");
1811 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
1812 AB8500_CHARGER
, AB8500_CHARGER_CTRL
,
1813 DROP_COUNT_RESET
, DROP_COUNT_RESET
);
1816 dev_err(di
->dev
, "ab8500 write failed %d\n", __LINE__
);
1820 ret
= ab8500_charger_ac_en(&di
->usb_chg
, true, vset_uv
, iset_ua
);
1822 dev_err(di
->dev
, "failed to enable AC charger %d\n",
1831 * ab8500_charger_watchdog_kick() - kick charger watchdog
1832 * @di: pointer to the ab8500_charger structure
1834 * Kick charger watchdog
1835 * Returns error code in case of failure else 0(on success)
1837 static int ab8500_charger_watchdog_kick(struct ux500_charger
*charger
)
1840 struct ab8500_charger
*di
;
1842 if (charger
->psy
->desc
->type
== POWER_SUPPLY_TYPE_MAINS
)
1843 di
= to_ab8500_charger_ac_device_info(charger
);
1844 else if (charger
->psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
)
1845 di
= to_ab8500_charger_usb_device_info(charger
);
1849 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1850 AB8500_CHARG_WD_CTRL
, CHARG_WD_KICK
);
1852 dev_err(di
->dev
, "Failed to kick WD!\n");
1858 * ab8500_charger_update_charger_current() - update charger current
1859 * @charger: pointer to the ab8500_charger structure
1860 * @ich_out_ua: desired output current in microampere
1862 * Update the charger output current for the specified charger
1863 * Returns error code in case of failure else 0(on success)
1865 static int ab8500_charger_update_charger_current(struct ux500_charger
*charger
,
1869 struct ab8500_charger
*di
;
1871 if (charger
->psy
->desc
->type
== POWER_SUPPLY_TYPE_MAINS
)
1872 di
= to_ab8500_charger_ac_device_info(charger
);
1873 else if (charger
->psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
)
1874 di
= to_ab8500_charger_usb_device_info(charger
);
1878 ret
= ab8500_charger_set_output_curr(di
, ich_out_ua
);
1880 dev_err(di
->dev
, "%s "
1881 "Failed to set ChOutputCurentLevel\n",
1886 /* Reset the main and usb drop input current measurement counter */
1887 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
1888 AB8500_CHARGER_CTRL
, DROP_COUNT_RESET
);
1890 dev_err(di
->dev
, "%s write failed\n", __func__
);
1897 static int ab8500_charger_get_ext_psy_data(struct device
*dev
, void *data
)
1899 struct power_supply
*psy
;
1900 struct power_supply
*ext
= dev_get_drvdata(dev
);
1901 const char **supplicants
= (const char **)ext
->supplied_to
;
1902 struct ab8500_charger
*di
;
1903 union power_supply_propval ret
;
1905 struct ux500_charger
*usb_chg
;
1907 usb_chg
= (struct ux500_charger
*)data
;
1910 di
= to_ab8500_charger_usb_device_info(usb_chg
);
1913 * For all psy where the driver name appears in any supplied_to
1914 * in practice what we will find will always be "ab8500_fg" as
1915 * the fuel gauge is responsible of keeping track of VBAT.
1917 j
= match_string(supplicants
, ext
->num_supplicants
, psy
->desc
->name
);
1921 /* Go through all properties for the psy */
1922 for (j
= 0; j
< ext
->desc
->num_properties
; j
++) {
1923 enum power_supply_property prop
;
1924 prop
= ext
->desc
->properties
[j
];
1926 if (power_supply_get_property(ext
, prop
, &ret
))
1930 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
1931 switch (ext
->desc
->type
) {
1932 case POWER_SUPPLY_TYPE_BATTERY
:
1933 /* This will always be "ab8500_fg" */
1934 dev_dbg(di
->dev
, "get VBAT from %s\n",
1935 dev_name(&ext
->dev
));
1936 di
->vbat
= ret
.intval
;
1950 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1951 * @work pointer to the work_struct structure
1953 * Due to a asic bug it is necessary to lower the input current to the vbus
1954 * charger when charging with at some specific levels. This issue is only valid
1955 * for below a certain battery voltage. This function makes sure that
1956 * the allowed current limit isn't exceeded.
1958 static void ab8500_charger_check_vbat_work(struct work_struct
*work
)
1961 struct ab8500_charger
*di
= container_of(work
,
1962 struct ab8500_charger
, check_vbat_work
.work
);
1964 power_supply_for_each_device(&di
->usb_chg
, ab8500_charger_get_ext_psy_data
);
1966 /* First run old_vbat is 0. */
1967 if (di
->old_vbat
== 0)
1968 di
->old_vbat
= di
->vbat
;
1970 if (!((di
->old_vbat
<= VBAT_TRESH_IP_CUR_RED
&&
1971 di
->vbat
<= VBAT_TRESH_IP_CUR_RED
) ||
1972 (di
->old_vbat
> VBAT_TRESH_IP_CUR_RED
&&
1973 di
->vbat
> VBAT_TRESH_IP_CUR_RED
))) {
1975 dev_dbg(di
->dev
, "Vbat did cross threshold, curr: %d, new: %d,"
1976 " old: %d\n", di
->max_usb_in_curr
.usb_type_max_ua
,
1977 di
->vbat
, di
->old_vbat
);
1978 ab8500_charger_set_vbus_in_curr(di
,
1979 di
->max_usb_in_curr
.usb_type_max_ua
);
1980 power_supply_changed(di
->usb_chg
.psy
);
1983 di
->old_vbat
= di
->vbat
;
1986 * No need to check the battery voltage every second when not close to
1989 if (di
->vbat
< (VBAT_TRESH_IP_CUR_RED
+ 100000) &&
1990 (di
->vbat
> (VBAT_TRESH_IP_CUR_RED
- 100000)))
1993 queue_delayed_work(di
->charger_wq
, &di
->check_vbat_work
, t
* HZ
);
1997 * ab8500_charger_check_hw_failure_work() - check main charger failure
1998 * @work: pointer to the work_struct structure
2000 * Work queue function for checking the main charger status
2002 static void ab8500_charger_check_hw_failure_work(struct work_struct
*work
)
2007 struct ab8500_charger
*di
= container_of(work
,
2008 struct ab8500_charger
, check_hw_failure_work
.work
);
2010 /* Check if the status bits for HW failure is still active */
2011 if (di
->flags
.mainextchnotok
) {
2012 ret
= abx500_get_register_interruptible(di
->dev
,
2013 AB8500_CHARGER
, AB8500_CH_STATUS2_REG
, ®_value
);
2015 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
2018 if (!(reg_value
& MAIN_CH_NOK
)) {
2019 di
->flags
.mainextchnotok
= false;
2020 ab8500_power_supply_changed(di
, di
->ac_chg
.psy
);
2023 if (di
->flags
.vbus_ovv
) {
2024 ret
= abx500_get_register_interruptible(di
->dev
,
2025 AB8500_CHARGER
, AB8500_CH_USBCH_STAT2_REG
,
2028 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
2031 if (!(reg_value
& VBUS_OVV_TH
)) {
2032 di
->flags
.vbus_ovv
= false;
2033 ab8500_power_supply_changed(di
, di
->usb_chg
.psy
);
2036 /* If we still have a failure, schedule a new check */
2037 if (di
->flags
.mainextchnotok
|| di
->flags
.vbus_ovv
) {
2038 queue_delayed_work(di
->charger_wq
,
2039 &di
->check_hw_failure_work
, round_jiffies(HZ
));
2044 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2045 * @work: pointer to the work_struct structure
2047 * Work queue function for kicking the charger watchdog.
2049 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2050 * logic. That means we have to continuously kick the charger
2051 * watchdog even when no charger is connected. This is only
2052 * valid once the AC charger has been enabled. This is
2053 * a bug that is not handled by the algorithm and the
2054 * watchdog have to be kicked by the charger driver
2055 * when the AC charger is disabled
2057 static void ab8500_charger_kick_watchdog_work(struct work_struct
*work
)
2061 struct ab8500_charger
*di
= container_of(work
,
2062 struct ab8500_charger
, kick_wd_work
.work
);
2064 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
2065 AB8500_CHARG_WD_CTRL
, CHARG_WD_KICK
);
2067 dev_err(di
->dev
, "Failed to kick WD!\n");
2069 /* Schedule a new watchdog kick */
2070 queue_delayed_work(di
->charger_wq
,
2071 &di
->kick_wd_work
, round_jiffies(WD_KICK_INTERVAL
));
2075 * ab8500_charger_ac_work() - work to get and set main charger status
2076 * @work: pointer to the work_struct structure
2078 * Work queue function for checking the main charger status
2080 static void ab8500_charger_ac_work(struct work_struct
*work
)
2084 struct ab8500_charger
*di
= container_of(work
,
2085 struct ab8500_charger
, ac_work
);
2088 * Since we can't be sure that the events are received
2089 * synchronously, we have the check if the main charger is
2090 * connected by reading the status register
2092 ret
= ab8500_charger_detect_chargers(di
, false);
2096 if (ret
& AC_PW_CONN
) {
2097 di
->ac
.charger_connected
= 1;
2100 di
->ac
.charger_connected
= 0;
2103 ab8500_power_supply_changed(di
, di
->ac_chg
.psy
);
2104 sysfs_notify(&di
->ac_chg
.psy
->dev
.kobj
, NULL
, "present");
2107 static void ab8500_charger_usb_attached_work(struct work_struct
*work
)
2109 struct ab8500_charger
*di
= container_of(work
,
2110 struct ab8500_charger
,
2111 usb_charger_attached_work
.work
);
2112 int usbch
= (USB_CH_VBUSDROP
| USB_CH_VBUSDETDBNC
);
2116 for (i
= 0; i
< 10; i
++) {
2117 ret
= abx500_get_register_interruptible(di
->dev
,
2119 AB8500_CH_USBCH_STAT1_REG
,
2122 dev_err(di
->dev
, "ab8500 read failed %d\n", __LINE__
);
2125 if ((statval
& usbch
) != usbch
)
2128 msleep(CHARGER_STATUS_POLL
);
2131 ab8500_charger_usb_en(&di
->usb_chg
, 0, 0, 0);
2133 mutex_lock(&di
->charger_attached_mutex
);
2134 mutex_unlock(&di
->charger_attached_mutex
);
2139 queue_delayed_work(di
->charger_wq
,
2140 &di
->usb_charger_attached_work
,
2144 static void ab8500_charger_ac_attached_work(struct work_struct
*work
)
2147 struct ab8500_charger
*di
= container_of(work
,
2148 struct ab8500_charger
,
2149 ac_charger_attached_work
.work
);
2150 int mainch
= (MAIN_CH_STATUS2_MAINCHGDROP
|
2151 MAIN_CH_STATUS2_MAINCHARGERDETDBNC
);
2155 for (i
= 0; i
< 10; i
++) {
2156 ret
= abx500_get_register_interruptible(di
->dev
,
2158 AB8500_CH_STATUS2_REG
,
2161 dev_err(di
->dev
, "ab8500 read failed %d\n", __LINE__
);
2165 if ((statval
& mainch
) != mainch
)
2168 msleep(CHARGER_STATUS_POLL
);
2171 ab8500_charger_ac_en(&di
->ac_chg
, 0, 0, 0);
2172 queue_work(di
->charger_wq
, &di
->ac_work
);
2174 mutex_lock(&di
->charger_attached_mutex
);
2175 mutex_unlock(&di
->charger_attached_mutex
);
2180 queue_delayed_work(di
->charger_wq
,
2181 &di
->ac_charger_attached_work
,
2186 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2187 * @work: Pointer to the work_struct structure
2189 * Detect the type of USB plugged
2191 static void ab8500_charger_detect_usb_type_work(struct work_struct
*work
)
2195 struct ab8500_charger
*di
= container_of(work
,
2196 struct ab8500_charger
, detect_usb_type_work
);
2199 * Since we can't be sure that the events are received
2200 * synchronously, we have the check if is
2201 * connected by reading the status register
2203 ret
= ab8500_charger_detect_chargers(di
, false);
2207 if (!(ret
& USB_PW_CONN
)) {
2208 dev_dbg(di
->dev
, "%s di->vbus_detected = false\n", __func__
);
2209 di
->vbus_detected
= false;
2210 ab8500_charger_set_usb_connected(di
, false);
2211 ab8500_power_supply_changed(di
, di
->usb_chg
.psy
);
2213 dev_dbg(di
->dev
, "%s di->vbus_detected = true\n", __func__
);
2214 di
->vbus_detected
= true;
2216 if (is_ab8500_1p1_or_earlier(di
->parent
)) {
2217 ret
= ab8500_charger_detect_usb_type(di
);
2219 ab8500_charger_set_usb_connected(di
, true);
2220 ab8500_power_supply_changed(di
,
2225 * For ABB cut2.0 and onwards we have an IRQ,
2226 * USB_LINK_STATUS that will be triggered when the USB
2227 * link status changes. The exception is USB connected
2228 * during startup. Then we don't get a
2229 * USB_LINK_STATUS IRQ
2231 if (di
->vbus_detected_start
) {
2232 di
->vbus_detected_start
= false;
2233 ret
= ab8500_charger_detect_usb_type(di
);
2235 ab8500_charger_set_usb_connected(di
,
2237 ab8500_power_supply_changed(di
,
2246 * ab8500_charger_usb_link_attach_work() - work to detect USB type
2247 * @work: pointer to the work_struct structure
2249 * Detect the type of USB plugged
2251 static void ab8500_charger_usb_link_attach_work(struct work_struct
*work
)
2253 struct ab8500_charger
*di
=
2254 container_of(work
, struct ab8500_charger
, attach_work
.work
);
2257 /* Update maximum input current if USB enumeration is not detected */
2258 if (!di
->usb
.charger_online
) {
2259 ret
= ab8500_charger_set_vbus_in_curr(di
,
2260 di
->max_usb_in_curr
.usb_type_max_ua
);
2265 ab8500_charger_set_usb_connected(di
, true);
2266 ab8500_power_supply_changed(di
, di
->usb_chg
.psy
);
2270 * ab8500_charger_usb_link_status_work() - work to detect USB type
2271 * @work: pointer to the work_struct structure
2273 * Detect the type of USB plugged
2275 static void ab8500_charger_usb_link_status_work(struct work_struct
*work
)
2277 int detected_chargers
;
2282 struct ab8500_charger
*di
= container_of(work
,
2283 struct ab8500_charger
, usb_link_status_work
);
2286 * Since we can't be sure that the events are received
2287 * synchronously, we have the check if is
2288 * connected by reading the status register
2290 detected_chargers
= ab8500_charger_detect_chargers(di
, false);
2291 if (detected_chargers
< 0)
2295 * Some chargers that breaks the USB spec is
2296 * identified as invalid by AB8500 and it refuse
2297 * to start the charging process. but by jumping
2298 * through a few hoops it can be forced to start.
2300 if (is_ab8500(di
->parent
))
2301 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_USB
,
2302 AB8500_USB_LINE_STAT_REG
, &val
);
2304 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_USB
,
2305 AB8500_USB_LINK1_STAT_REG
, &val
);
2308 dev_dbg(di
->dev
, "UsbLineStatus register = 0x%02x\n", val
);
2310 dev_dbg(di
->dev
, "Error reading USB link status\n");
2312 if (is_ab8500(di
->parent
))
2313 link_status
= AB8500_USB_LINK_STATUS
;
2315 link_status
= AB8505_USB_LINK_STATUS
;
2317 if (detected_chargers
& USB_PW_CONN
) {
2318 if (((val
& link_status
) >> USB_LINK_STATUS_SHIFT
) ==
2319 USB_STAT_NOT_VALID_LINK
&&
2320 di
->invalid_charger_detect_state
== 0) {
2322 "Invalid charger detected, state= 0\n");
2324 abx500_mask_and_set_register_interruptible(di
->dev
,
2325 AB8500_CHARGER
, AB8500_USBCH_CTRL1_REG
,
2326 USB_CH_ENA
, USB_CH_ENA
);
2327 /*Enable charger detection*/
2328 abx500_mask_and_set_register_interruptible(di
->dev
,
2329 AB8500_USB
, AB8500_USB_LINE_CTRL2_REG
,
2330 USB_CH_DET
, USB_CH_DET
);
2331 di
->invalid_charger_detect_state
= 1;
2332 /*exit and wait for new link status interrupt.*/
2336 if (di
->invalid_charger_detect_state
== 1) {
2338 "Invalid charger detected, state= 1\n");
2339 /*Stop charger detection*/
2340 abx500_mask_and_set_register_interruptible(di
->dev
,
2341 AB8500_USB
, AB8500_USB_LINE_CTRL2_REG
,
2343 /*Check link status*/
2344 if (is_ab8500(di
->parent
))
2345 ret
= abx500_get_register_interruptible(di
->dev
,
2346 AB8500_USB
, AB8500_USB_LINE_STAT_REG
,
2349 ret
= abx500_get_register_interruptible(di
->dev
,
2350 AB8500_USB
, AB8500_USB_LINK1_STAT_REG
,
2353 dev_dbg(di
->dev
, "USB link status= 0x%02x\n",
2354 (val
& link_status
) >> USB_LINK_STATUS_SHIFT
);
2355 di
->invalid_charger_detect_state
= 2;
2358 di
->invalid_charger_detect_state
= 0;
2361 if (!(detected_chargers
& USB_PW_CONN
)) {
2362 di
->vbus_detected
= false;
2363 ab8500_charger_set_usb_connected(di
, false);
2364 ab8500_power_supply_changed(di
, di
->usb_chg
.psy
);
2368 dev_dbg(di
->dev
,"%s di->vbus_detected = true\n",__func__
);
2369 di
->vbus_detected
= true;
2370 ret
= ab8500_charger_read_usb_type(di
);
2372 if (ret
== -ENXIO
) {
2373 /* No valid charger type detected */
2374 ab8500_charger_set_usb_connected(di
, false);
2375 ab8500_power_supply_changed(di
, di
->usb_chg
.psy
);
2380 if (di
->usb_device_is_unrecognised
) {
2382 "Potential Legacy Charger device. "
2383 "Delay work for %d msec for USB enum "
2385 WAIT_ACA_RID_ENUMERATION
);
2386 queue_delayed_work(di
->charger_wq
,
2388 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION
));
2389 } else if (di
->is_aca_rid
== 1) {
2390 /* Only wait once */
2393 "%s Wait %d msec for USB enum to finish",
2394 __func__
, WAIT_ACA_RID_ENUMERATION
);
2395 queue_delayed_work(di
->charger_wq
,
2397 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION
));
2399 queue_delayed_work(di
->charger_wq
,
2405 static void ab8500_charger_usb_state_changed_work(struct work_struct
*work
)
2408 unsigned long flags
;
2410 struct ab8500_charger
*di
= container_of(work
,
2411 struct ab8500_charger
, usb_state_changed_work
.work
);
2413 if (!di
->vbus_detected
) {
2415 "%s !di->vbus_detected\n",
2420 spin_lock_irqsave(&di
->usb_state
.usb_lock
, flags
);
2421 di
->usb_state
.state
= di
->usb_state
.state_tmp
;
2422 di
->usb_state
.usb_current_ua
= di
->usb_state
.usb_current_tmp_ua
;
2423 spin_unlock_irqrestore(&di
->usb_state
.usb_lock
, flags
);
2425 dev_dbg(di
->dev
, "%s USB state: 0x%02x uA: %d\n",
2426 __func__
, di
->usb_state
.state
, di
->usb_state
.usb_current_ua
);
2428 switch (di
->usb_state
.state
) {
2429 case AB8500_BM_USB_STATE_RESET_HS
:
2430 case AB8500_BM_USB_STATE_RESET_FS
:
2431 case AB8500_BM_USB_STATE_SUSPEND
:
2432 case AB8500_BM_USB_STATE_MAX
:
2433 ab8500_charger_set_usb_connected(di
, false);
2434 ab8500_power_supply_changed(di
, di
->usb_chg
.psy
);
2437 case AB8500_BM_USB_STATE_RESUME
:
2439 * when suspend->resume there should be delay
2440 * of 1sec for enabling charging
2444 case AB8500_BM_USB_STATE_CONFIGURED
:
2446 * USB is configured, enable charging with the charging
2447 * input current obtained from USB driver
2449 if (!ab8500_charger_get_usb_cur(di
)) {
2450 /* Update maximum input current */
2451 ret
= ab8500_charger_set_vbus_in_curr(di
,
2452 di
->max_usb_in_curr
.usb_type_max_ua
);
2456 ab8500_charger_set_usb_connected(di
, true);
2457 ab8500_power_supply_changed(di
, di
->usb_chg
.psy
);
2467 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2468 * @work: pointer to the work_struct structure
2470 * Work queue function for checking the USB charger Not OK status
2472 static void ab8500_charger_check_usbchargernotok_work(struct work_struct
*work
)
2478 struct ab8500_charger
*di
= container_of(work
,
2479 struct ab8500_charger
, check_usbchgnotok_work
.work
);
2481 /* Check if the status bit for usbchargernotok is still active */
2482 ret
= abx500_get_register_interruptible(di
->dev
,
2483 AB8500_CHARGER
, AB8500_CH_USBCH_STAT2_REG
, ®_value
);
2485 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
2488 prev_status
= di
->flags
.usbchargernotok
;
2490 if (reg_value
& VBUS_CH_NOK
) {
2491 di
->flags
.usbchargernotok
= true;
2492 /* Check again in 1sec */
2493 queue_delayed_work(di
->charger_wq
,
2494 &di
->check_usbchgnotok_work
, HZ
);
2496 di
->flags
.usbchargernotok
= false;
2497 di
->flags
.vbus_collapse
= false;
2500 if (prev_status
!= di
->flags
.usbchargernotok
)
2501 ab8500_power_supply_changed(di
, di
->usb_chg
.psy
);
2505 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2506 * @work: pointer to the work_struct structure
2508 * Work queue function for checking the Main thermal prot status
2510 static void ab8500_charger_check_main_thermal_prot_work(
2511 struct work_struct
*work
)
2516 struct ab8500_charger
*di
= container_of(work
,
2517 struct ab8500_charger
, check_main_thermal_prot_work
);
2519 /* Check if the status bit for main_thermal_prot is still active */
2520 ret
= abx500_get_register_interruptible(di
->dev
,
2521 AB8500_CHARGER
, AB8500_CH_STATUS2_REG
, ®_value
);
2523 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
2526 if (reg_value
& MAIN_CH_TH_PROT
)
2527 di
->flags
.main_thermal_prot
= true;
2529 di
->flags
.main_thermal_prot
= false;
2531 ab8500_power_supply_changed(di
, di
->ac_chg
.psy
);
2535 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2536 * @work: pointer to the work_struct structure
2538 * Work queue function for checking the USB thermal prot status
2540 static void ab8500_charger_check_usb_thermal_prot_work(
2541 struct work_struct
*work
)
2546 struct ab8500_charger
*di
= container_of(work
,
2547 struct ab8500_charger
, check_usb_thermal_prot_work
);
2549 /* Check if the status bit for usb_thermal_prot is still active */
2550 ret
= abx500_get_register_interruptible(di
->dev
,
2551 AB8500_CHARGER
, AB8500_CH_USBCH_STAT2_REG
, ®_value
);
2553 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
2556 if (reg_value
& USB_CH_TH_PROT
)
2557 di
->flags
.usb_thermal_prot
= true;
2559 di
->flags
.usb_thermal_prot
= false;
2561 ab8500_power_supply_changed(di
, di
->usb_chg
.psy
);
2565 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2566 * @irq: interrupt number
2567 * @_di: pointer to the ab8500_charger structure
2569 * Returns IRQ status(IRQ_HANDLED)
2571 static irqreturn_t
ab8500_charger_mainchunplugdet_handler(int irq
, void *_di
)
2573 struct ab8500_charger
*di
= _di
;
2575 dev_dbg(di
->dev
, "Main charger unplugged\n");
2576 queue_work(di
->charger_wq
, &di
->ac_work
);
2578 cancel_delayed_work_sync(&di
->ac_charger_attached_work
);
2579 mutex_lock(&di
->charger_attached_mutex
);
2580 mutex_unlock(&di
->charger_attached_mutex
);
2586 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2587 * @irq: interrupt number
2588 * @_di: pointer to the ab8500_charger structure
2590 * Returns IRQ status(IRQ_HANDLED)
2592 static irqreturn_t
ab8500_charger_mainchplugdet_handler(int irq
, void *_di
)
2594 struct ab8500_charger
*di
= _di
;
2596 dev_dbg(di
->dev
, "Main charger plugged\n");
2597 queue_work(di
->charger_wq
, &di
->ac_work
);
2599 mutex_lock(&di
->charger_attached_mutex
);
2600 mutex_unlock(&di
->charger_attached_mutex
);
2602 if (is_ab8500(di
->parent
))
2603 queue_delayed_work(di
->charger_wq
,
2604 &di
->ac_charger_attached_work
,
2610 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2611 * @irq: interrupt number
2612 * @_di: pointer to the ab8500_charger structure
2614 * Returns IRQ status(IRQ_HANDLED)
2616 static irqreturn_t
ab8500_charger_mainextchnotok_handler(int irq
, void *_di
)
2618 struct ab8500_charger
*di
= _di
;
2620 dev_dbg(di
->dev
, "Main charger not ok\n");
2621 di
->flags
.mainextchnotok
= true;
2622 ab8500_power_supply_changed(di
, di
->ac_chg
.psy
);
2624 /* Schedule a new HW failure check */
2625 queue_delayed_work(di
->charger_wq
, &di
->check_hw_failure_work
, 0);
2631 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2632 * thermal protection threshold
2633 * @irq: interrupt number
2634 * @_di: pointer to the ab8500_charger structure
2636 * Returns IRQ status(IRQ_HANDLED)
2638 static irqreturn_t
ab8500_charger_mainchthprotr_handler(int irq
, void *_di
)
2640 struct ab8500_charger
*di
= _di
;
2643 "Die temp above Main charger thermal protection threshold\n");
2644 queue_work(di
->charger_wq
, &di
->check_main_thermal_prot_work
);
2650 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2651 * thermal protection threshold
2652 * @irq: interrupt number
2653 * @_di: pointer to the ab8500_charger structure
2655 * Returns IRQ status(IRQ_HANDLED)
2657 static irqreturn_t
ab8500_charger_mainchthprotf_handler(int irq
, void *_di
)
2659 struct ab8500_charger
*di
= _di
;
2662 "Die temp ok for Main charger thermal protection threshold\n");
2663 queue_work(di
->charger_wq
, &di
->check_main_thermal_prot_work
);
2668 static void ab8500_charger_vbus_drop_end_work(struct work_struct
*work
)
2670 struct ab8500_charger
*di
= container_of(work
,
2671 struct ab8500_charger
, vbus_drop_end_work
.work
);
2675 di
->flags
.vbus_drop_end
= false;
2677 /* Reset the drop counter */
2678 abx500_set_register_interruptible(di
->dev
,
2679 AB8500_CHARGER
, AB8500_CHARGER_CTRL
, 0x01);
2681 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_CHARGER
,
2682 AB8500_CH_USBCH_STAT2_REG
, ®_value
);
2684 dev_err(di
->dev
, "%s read failed\n", __func__
);
2688 curr_ua
= ab8500_charge_input_curr_map
[
2689 reg_value
>> AUTO_VBUS_IN_CURR_LIM_SHIFT
];
2691 if (di
->max_usb_in_curr
.calculated_max_ua
!= curr_ua
) {
2692 /* USB source is collapsing */
2693 di
->max_usb_in_curr
.calculated_max_ua
= curr_ua
;
2695 "VBUS input current limiting to %d uA\n",
2696 di
->max_usb_in_curr
.calculated_max_ua
);
2699 * USB source can not give more than this amount.
2700 * Taking more will collapse the source.
2702 di
->max_usb_in_curr
.set_max_ua
=
2703 di
->max_usb_in_curr
.calculated_max_ua
;
2705 "VBUS input current limited to %d uA\n",
2706 di
->max_usb_in_curr
.set_max_ua
);
2709 if (di
->usb
.charger_connected
)
2710 ab8500_charger_set_vbus_in_curr(di
,
2711 di
->max_usb_in_curr
.usb_type_max_ua
);
2715 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2716 * @irq: interrupt number
2717 * @_di: pointer to the ab8500_charger structure
2719 * Returns IRQ status(IRQ_HANDLED)
2721 static irqreturn_t
ab8500_charger_vbusdetf_handler(int irq
, void *_di
)
2723 struct ab8500_charger
*di
= _di
;
2725 di
->vbus_detected
= false;
2726 dev_dbg(di
->dev
, "VBUS falling detected\n");
2727 queue_work(di
->charger_wq
, &di
->detect_usb_type_work
);
2733 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2734 * @irq: interrupt number
2735 * @_di: pointer to the ab8500_charger structure
2737 * Returns IRQ status(IRQ_HANDLED)
2739 static irqreturn_t
ab8500_charger_vbusdetr_handler(int irq
, void *_di
)
2741 struct ab8500_charger
*di
= _di
;
2743 di
->vbus_detected
= true;
2744 dev_dbg(di
->dev
, "VBUS rising detected\n");
2746 queue_work(di
->charger_wq
, &di
->detect_usb_type_work
);
2752 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2753 * @irq: interrupt number
2754 * @_di: pointer to the ab8500_charger structure
2756 * Returns IRQ status(IRQ_HANDLED)
2758 static irqreturn_t
ab8500_charger_usblinkstatus_handler(int irq
, void *_di
)
2760 struct ab8500_charger
*di
= _di
;
2762 dev_dbg(di
->dev
, "USB link status changed\n");
2764 queue_work(di
->charger_wq
, &di
->usb_link_status_work
);
2770 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2771 * thermal protection threshold
2772 * @irq: interrupt number
2773 * @_di: pointer to the ab8500_charger structure
2775 * Returns IRQ status(IRQ_HANDLED)
2777 static irqreturn_t
ab8500_charger_usbchthprotr_handler(int irq
, void *_di
)
2779 struct ab8500_charger
*di
= _di
;
2782 "Die temp above USB charger thermal protection threshold\n");
2783 queue_work(di
->charger_wq
, &di
->check_usb_thermal_prot_work
);
2789 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2790 * thermal protection threshold
2791 * @irq: interrupt number
2792 * @_di: pointer to the ab8500_charger structure
2794 * Returns IRQ status(IRQ_HANDLED)
2796 static irqreturn_t
ab8500_charger_usbchthprotf_handler(int irq
, void *_di
)
2798 struct ab8500_charger
*di
= _di
;
2801 "Die temp ok for USB charger thermal protection threshold\n");
2802 queue_work(di
->charger_wq
, &di
->check_usb_thermal_prot_work
);
2808 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2809 * @irq: interrupt number
2810 * @_di: pointer to the ab8500_charger structure
2812 * Returns IRQ status(IRQ_HANDLED)
2814 static irqreturn_t
ab8500_charger_usbchargernotokr_handler(int irq
, void *_di
)
2816 struct ab8500_charger
*di
= _di
;
2818 dev_dbg(di
->dev
, "Not allowed USB charger detected\n");
2819 queue_delayed_work(di
->charger_wq
, &di
->check_usbchgnotok_work
, 0);
2825 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2826 * @irq: interrupt number
2827 * @_di: pointer to the ab8500_charger structure
2829 * Returns IRQ status(IRQ_HANDLED)
2831 static irqreturn_t
ab8500_charger_chwdexp_handler(int irq
, void *_di
)
2833 struct ab8500_charger
*di
= _di
;
2835 dev_dbg(di
->dev
, "Charger watchdog expired\n");
2838 * The charger that was online when the watchdog expired
2839 * needs to be restarted for charging to start again
2841 if (di
->ac
.charger_online
) {
2842 di
->ac
.wd_expired
= true;
2843 ab8500_power_supply_changed(di
, di
->ac_chg
.psy
);
2845 if (di
->usb
.charger_online
) {
2846 di
->usb
.wd_expired
= true;
2847 ab8500_power_supply_changed(di
, di
->usb_chg
.psy
);
2854 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2855 * @irq: interrupt number
2856 * @_di: pointer to the ab8500_charger structure
2858 * Returns IRQ status(IRQ_HANDLED)
2860 static irqreturn_t
ab8500_charger_vbuschdropend_handler(int irq
, void *_di
)
2862 struct ab8500_charger
*di
= _di
;
2864 dev_dbg(di
->dev
, "VBUS charger drop ended\n");
2865 di
->flags
.vbus_drop_end
= true;
2868 * VBUS might have dropped due to bad connection.
2869 * Schedule a new input limit set to the value SW requests.
2871 queue_delayed_work(di
->charger_wq
, &di
->vbus_drop_end_work
,
2872 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME
* HZ
));
2878 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2879 * @irq: interrupt number
2880 * @_di: pointer to the ab8500_charger structure
2882 * Returns IRQ status(IRQ_HANDLED)
2884 static irqreturn_t
ab8500_charger_vbusovv_handler(int irq
, void *_di
)
2886 struct ab8500_charger
*di
= _di
;
2888 dev_dbg(di
->dev
, "VBUS overvoltage detected\n");
2889 di
->flags
.vbus_ovv
= true;
2890 ab8500_power_supply_changed(di
, di
->usb_chg
.psy
);
2892 /* Schedule a new HW failure check */
2893 queue_delayed_work(di
->charger_wq
, &di
->check_hw_failure_work
, 0);
2899 * ab8500_charger_ac_get_property() - get the ac/mains properties
2900 * @psy: pointer to the power_supply structure
2901 * @psp: pointer to the power_supply_property structure
2902 * @val: pointer to the power_supply_propval union
2904 * This function gets called when an application tries to get the ac/mains
2905 * properties by reading the sysfs files.
2906 * AC/Mains properties are online, present and voltage.
2907 * online: ac/mains charging is in progress or not
2908 * present: presence of the ac/mains
2909 * voltage: AC/Mains voltage
2910 * Returns error code in case of failure else 0(on success)
2912 static int ab8500_charger_ac_get_property(struct power_supply
*psy
,
2913 enum power_supply_property psp
,
2914 union power_supply_propval
*val
)
2916 struct ab8500_charger
*di
;
2919 di
= to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy
));
2922 case POWER_SUPPLY_PROP_HEALTH
:
2923 if (di
->flags
.mainextchnotok
)
2924 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
2925 else if (di
->ac
.wd_expired
|| di
->usb
.wd_expired
)
2926 val
->intval
= POWER_SUPPLY_HEALTH_DEAD
;
2927 else if (di
->flags
.main_thermal_prot
)
2928 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
2930 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
2932 case POWER_SUPPLY_PROP_ONLINE
:
2933 val
->intval
= di
->ac
.charger_online
;
2935 case POWER_SUPPLY_PROP_PRESENT
:
2936 val
->intval
= di
->ac
.charger_connected
;
2938 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
2939 ret
= ab8500_charger_get_ac_voltage(di
);
2941 di
->ac
.charger_voltage_uv
= ret
;
2942 /* On error, use previous value */
2943 val
->intval
= di
->ac
.charger_voltage_uv
;
2945 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
2947 * This property is used to indicate when CV mode is entered
2948 * for the AC charger
2950 di
->ac
.cv_active
= ab8500_charger_ac_cv(di
);
2951 val
->intval
= di
->ac
.cv_active
;
2953 case POWER_SUPPLY_PROP_CURRENT_NOW
:
2954 ret
= ab8500_charger_get_ac_current(di
);
2956 di
->ac
.charger_current_ua
= ret
;
2957 val
->intval
= di
->ac
.charger_current_ua
;
2966 * ab8500_charger_usb_get_property() - get the usb properties
2967 * @psy: pointer to the power_supply structure
2968 * @psp: pointer to the power_supply_property structure
2969 * @val: pointer to the power_supply_propval union
2971 * This function gets called when an application tries to get the usb
2972 * properties by reading the sysfs files.
2973 * USB properties are online, present and voltage.
2974 * online: usb charging is in progress or not
2975 * present: presence of the usb
2976 * voltage: vbus voltage
2977 * Returns error code in case of failure else 0(on success)
2979 static int ab8500_charger_usb_get_property(struct power_supply
*psy
,
2980 enum power_supply_property psp
,
2981 union power_supply_propval
*val
)
2983 struct ab8500_charger
*di
;
2986 di
= to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy
));
2989 case POWER_SUPPLY_PROP_HEALTH
:
2990 if (di
->flags
.usbchargernotok
)
2991 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
2992 else if (di
->ac
.wd_expired
|| di
->usb
.wd_expired
)
2993 val
->intval
= POWER_SUPPLY_HEALTH_DEAD
;
2994 else if (di
->flags
.usb_thermal_prot
)
2995 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
2996 else if (di
->flags
.vbus_ovv
)
2997 val
->intval
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
2999 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
3001 case POWER_SUPPLY_PROP_ONLINE
:
3002 val
->intval
= di
->usb
.charger_online
;
3004 case POWER_SUPPLY_PROP_PRESENT
:
3005 val
->intval
= di
->usb
.charger_connected
;
3007 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
3008 ret
= ab8500_charger_get_vbus_voltage(di
);
3010 di
->usb
.charger_voltage_uv
= ret
;
3011 val
->intval
= di
->usb
.charger_voltage_uv
;
3013 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
3015 * This property is used to indicate when CV mode is entered
3016 * for the USB charger
3018 di
->usb
.cv_active
= ab8500_charger_usb_cv(di
);
3019 val
->intval
= di
->usb
.cv_active
;
3021 case POWER_SUPPLY_PROP_CURRENT_NOW
:
3022 ret
= ab8500_charger_get_usb_current(di
);
3024 di
->usb
.charger_current_ua
= ret
;
3025 val
->intval
= di
->usb
.charger_current_ua
;
3027 case POWER_SUPPLY_PROP_CURRENT_AVG
:
3029 * This property is used to indicate when VBUS has collapsed
3030 * due to too high output current from the USB charger
3032 if (di
->flags
.vbus_collapse
)
3044 * ab8500_charger_init_hw_registers() - Set up charger related registers
3045 * @di: pointer to the ab8500_charger structure
3047 * Set up charger OVV, watchdog and maximum voltage registers as well as
3048 * charging of the backup battery
3050 static int ab8500_charger_init_hw_registers(struct ab8500_charger
*di
)
3054 /* Setup maximum charger current and voltage for ABB cut2.0 */
3055 if (!is_ab8500_1p1_or_earlier(di
->parent
)) {
3056 ret
= abx500_set_register_interruptible(di
->dev
,
3058 AB8500_CH_VOLT_LVL_MAX_REG
, CH_VOL_LVL_4P6
);
3061 "failed to set CH_VOLT_LVL_MAX_REG\n");
3065 ret
= abx500_set_register_interruptible(di
->dev
,
3066 AB8500_CHARGER
, AB8500_CH_OPT_CRNTLVL_MAX_REG
,
3070 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3075 if (is_ab8505_2p0(di
->parent
))
3076 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
3078 AB8500_USBCH_CTRL2_REG
,
3079 VBUS_AUTO_IN_CURR_LIM_ENA
,
3080 VBUS_AUTO_IN_CURR_LIM_ENA
);
3083 * VBUS OVV set to 6.3V and enable automatic current limitation
3085 ret
= abx500_set_register_interruptible(di
->dev
,
3087 AB8500_USBCH_CTRL2_REG
,
3088 VBUS_OVV_SELECT_6P3V
| VBUS_AUTO_IN_CURR_LIM_ENA
);
3091 "failed to set automatic current limitation\n");
3095 /* Enable main watchdog in OTP */
3096 ret
= abx500_set_register_interruptible(di
->dev
,
3097 AB8500_OTP_EMUL
, AB8500_OTP_CONF_15
, OTP_ENABLE_WD
);
3099 dev_err(di
->dev
, "failed to enable main WD in OTP\n");
3103 /* Enable main watchdog */
3104 ret
= abx500_set_register_interruptible(di
->dev
,
3105 AB8500_SYS_CTRL2_BLOCK
,
3106 AB8500_MAIN_WDOG_CTRL_REG
, MAIN_WDOG_ENA
);
3108 dev_err(di
->dev
, "failed to enable main watchdog\n");
3113 * Due to internal synchronisation, Enable and Kick watchdog bits
3114 * cannot be enabled in a single write.
3115 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3116 * between writing Enable then Kick bits.
3120 /* Kick main watchdog */
3121 ret
= abx500_set_register_interruptible(di
->dev
,
3122 AB8500_SYS_CTRL2_BLOCK
,
3123 AB8500_MAIN_WDOG_CTRL_REG
,
3124 (MAIN_WDOG_ENA
| MAIN_WDOG_KICK
));
3126 dev_err(di
->dev
, "failed to kick main watchdog\n");
3130 /* Disable main watchdog */
3131 ret
= abx500_set_register_interruptible(di
->dev
,
3132 AB8500_SYS_CTRL2_BLOCK
,
3133 AB8500_MAIN_WDOG_CTRL_REG
, MAIN_WDOG_DIS
);
3135 dev_err(di
->dev
, "failed to disable main watchdog\n");
3139 /* Set watchdog timeout */
3140 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
3141 AB8500_CH_WD_TIMER_REG
, WD_TIMER
);
3143 dev_err(di
->dev
, "failed to set charger watchdog timeout\n");
3147 ret
= ab8500_charger_led_en(di
, false);
3149 dev_err(di
->dev
, "failed to disable LED\n");
3153 ret
= abx500_set_register_interruptible(di
->dev
,
3155 AB8500_RTC_BACKUP_CHG_REG
,
3156 (di
->bm
->bkup_bat_v
& 0x3) | di
->bm
->bkup_bat_i
);
3158 dev_err(di
->dev
, "failed to setup backup battery charging\n");
3162 /* Enable backup battery charging */
3163 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
3164 AB8500_RTC
, AB8500_RTC_CTRL_REG
,
3165 RTC_BUP_CH_ENA
, RTC_BUP_CH_ENA
);
3167 dev_err(di
->dev
, "%s mask and set failed\n", __func__
);
3176 * ab8500 charger driver interrupts and their respective isr
3178 static struct ab8500_charger_interrupts ab8500_charger_irq
[] = {
3179 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler
},
3180 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler
},
3181 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler
},
3182 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler
},
3183 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler
},
3184 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler
},
3185 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler
},
3186 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler
},
3187 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler
},
3188 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler
},
3189 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler
},
3190 {"VBUS_OVV", ab8500_charger_vbusovv_handler
},
3191 {"CH_WD_EXP", ab8500_charger_chwdexp_handler
},
3192 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler
},
3195 static int ab8500_charger_usb_notifier_call(struct notifier_block
*nb
,
3196 unsigned long event
, void *power
)
3198 struct ab8500_charger
*di
=
3199 container_of(nb
, struct ab8500_charger
, nb
);
3200 enum ab8500_usb_state bm_usb_state
;
3202 * FIXME: it appears the AB8500 PHY never sends what it should here.
3203 * Fix the PHY driver to properly notify the desired current.
3204 * Also broadcast microampere and not milliampere.
3206 unsigned mA
= *((unsigned *)power
);
3208 if (event
!= USB_EVENT_VBUS
) {
3209 dev_dbg(di
->dev
, "not a standard host, returning\n");
3213 /* TODO: State is fabricate here. See if charger really needs USB
3214 * state or if mA is enough
3216 if ((di
->usb_state
.usb_current_ua
== 2000) && (mA
> 2))
3217 bm_usb_state
= AB8500_BM_USB_STATE_RESUME
;
3219 bm_usb_state
= AB8500_BM_USB_STATE_RESET_HS
;
3221 bm_usb_state
= AB8500_BM_USB_STATE_SUSPEND
;
3222 else if (mA
>= 8) /* 8, 100, 500 */
3223 bm_usb_state
= AB8500_BM_USB_STATE_CONFIGURED
;
3224 else /* Should never occur */
3225 bm_usb_state
= AB8500_BM_USB_STATE_RESET_FS
;
3227 dev_dbg(di
->dev
, "%s usb_state: 0x%02x mA: %d\n",
3228 __func__
, bm_usb_state
, mA
);
3230 spin_lock(&di
->usb_state
.usb_lock
);
3231 di
->usb_state
.state_tmp
= bm_usb_state
;
3232 /* FIXME: broadcast ua instead, see above */
3233 di
->usb_state
.usb_current_tmp_ua
= mA
* 1000;
3234 spin_unlock(&di
->usb_state
.usb_lock
);
3237 * wait for some time until you get updates from the usb stack
3238 * and negotiations are completed
3240 queue_delayed_work(di
->charger_wq
, &di
->usb_state_changed_work
, HZ
/2);
3245 static int __maybe_unused
ab8500_charger_resume(struct device
*dev
)
3248 struct ab8500_charger
*di
= dev_get_drvdata(dev
);
3251 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3252 * logic. That means we have to continuously kick the charger
3253 * watchdog even when no charger is connected. This is only
3254 * valid once the AC charger has been enabled. This is
3255 * a bug that is not handled by the algorithm and the
3256 * watchdog have to be kicked by the charger driver
3257 * when the AC charger is disabled
3259 if (di
->ac_conn
&& is_ab8500_1p1_or_earlier(di
->parent
)) {
3260 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_CHARGER
,
3261 AB8500_CHARG_WD_CTRL
, CHARG_WD_KICK
);
3263 dev_err(di
->dev
, "Failed to kick WD!\n");
3265 /* If not already pending start a new timer */
3266 queue_delayed_work(di
->charger_wq
, &di
->kick_wd_work
,
3267 round_jiffies(WD_KICK_INTERVAL
));
3270 /* If we still have a HW failure, schedule a new check */
3271 if (di
->flags
.mainextchnotok
|| di
->flags
.vbus_ovv
) {
3272 queue_delayed_work(di
->charger_wq
,
3273 &di
->check_hw_failure_work
, 0);
3276 if (di
->flags
.vbus_drop_end
)
3277 queue_delayed_work(di
->charger_wq
, &di
->vbus_drop_end_work
, 0);
3282 static int __maybe_unused
ab8500_charger_suspend(struct device
*dev
)
3284 struct ab8500_charger
*di
= dev_get_drvdata(dev
);
3286 /* Cancel any pending jobs */
3287 cancel_delayed_work(&di
->check_hw_failure_work
);
3288 cancel_delayed_work(&di
->vbus_drop_end_work
);
3290 flush_delayed_work(&di
->attach_work
);
3291 flush_delayed_work(&di
->usb_charger_attached_work
);
3292 flush_delayed_work(&di
->ac_charger_attached_work
);
3293 flush_delayed_work(&di
->check_usbchgnotok_work
);
3294 flush_delayed_work(&di
->check_vbat_work
);
3295 flush_delayed_work(&di
->kick_wd_work
);
3297 flush_work(&di
->usb_link_status_work
);
3298 flush_work(&di
->ac_work
);
3299 flush_work(&di
->detect_usb_type_work
);
3301 if (atomic_read(&di
->current_stepping_sessions
))
3307 static char *supply_interface
[] = {
3313 static const struct power_supply_desc ab8500_ac_chg_desc
= {
3314 .name
= "ab8500_ac",
3315 .type
= POWER_SUPPLY_TYPE_MAINS
,
3316 .properties
= ab8500_charger_ac_props
,
3317 .num_properties
= ARRAY_SIZE(ab8500_charger_ac_props
),
3318 .get_property
= ab8500_charger_ac_get_property
,
3321 static const struct power_supply_desc ab8500_usb_chg_desc
= {
3322 .name
= "ab8500_usb",
3323 .type
= POWER_SUPPLY_TYPE_USB
,
3324 .properties
= ab8500_charger_usb_props
,
3325 .num_properties
= ARRAY_SIZE(ab8500_charger_usb_props
),
3326 .get_property
= ab8500_charger_usb_get_property
,
3329 static int ab8500_charger_bind(struct device
*dev
)
3331 struct ab8500_charger
*di
= dev_get_drvdata(dev
);
3335 /* Create a work queue for the charger */
3336 di
->charger_wq
= alloc_ordered_workqueue("ab8500_charger_wq",
3338 if (di
->charger_wq
== NULL
) {
3339 dev_err(dev
, "failed to create work queue\n");
3343 ch_stat
= ab8500_charger_detect_chargers(di
, false);
3345 if (ch_stat
& AC_PW_CONN
) {
3346 if (is_ab8500(di
->parent
))
3347 queue_delayed_work(di
->charger_wq
,
3348 &di
->ac_charger_attached_work
,
3351 if (ch_stat
& USB_PW_CONN
) {
3352 if (is_ab8500(di
->parent
))
3353 queue_delayed_work(di
->charger_wq
,
3354 &di
->usb_charger_attached_work
,
3356 di
->vbus_detected
= true;
3357 di
->vbus_detected_start
= true;
3358 queue_work(di
->charger_wq
,
3359 &di
->detect_usb_type_work
);
3362 ret
= component_bind_all(dev
, di
);
3364 dev_err(dev
, "can't bind component devices\n");
3365 destroy_workqueue(di
->charger_wq
);
3372 static void ab8500_charger_unbind(struct device
*dev
)
3374 struct ab8500_charger
*di
= dev_get_drvdata(dev
);
3377 /* Disable AC charging */
3378 ab8500_charger_ac_en(&di
->ac_chg
, false, 0, 0);
3380 /* Disable USB charging */
3381 ab8500_charger_usb_en(&di
->usb_chg
, false, 0, 0);
3383 /* Backup battery voltage and current disable */
3384 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
3385 AB8500_RTC
, AB8500_RTC_CTRL_REG
, RTC_BUP_CH_ENA
, 0);
3387 dev_err(di
->dev
, "%s mask and set failed\n", __func__
);
3389 /* Delete the work queue */
3390 destroy_workqueue(di
->charger_wq
);
3392 /* Unbind fg, btemp, algorithm */
3393 component_unbind_all(dev
, di
);
3396 static const struct component_master_ops ab8500_charger_comp_ops
= {
3397 .bind
= ab8500_charger_bind
,
3398 .unbind
= ab8500_charger_unbind
,
3401 static struct platform_driver
*const ab8500_charger_component_drivers
[] = {
3403 &ab8500_btemp_driver
,
3404 &ab8500_chargalg_driver
,
3407 static int ab8500_charger_probe(struct platform_device
*pdev
)
3409 struct device
*dev
= &pdev
->dev
;
3410 struct device_node
*np
= dev
->of_node
;
3411 struct component_match
*match
= NULL
;
3412 struct power_supply_config ac_psy_cfg
= {}, usb_psy_cfg
= {};
3413 struct ab8500_charger
*di
;
3418 di
= devm_kzalloc(dev
, sizeof(*di
), GFP_KERNEL
);
3422 di
->bm
= &ab8500_bm_data
;
3424 di
->autopower_cfg
= of_property_read_bool(np
, "autopower_cfg");
3426 /* get parent data */
3428 di
->parent
= dev_get_drvdata(pdev
->dev
.parent
);
3430 /* Get ADC channels */
3431 if (!is_ab8505(di
->parent
)) {
3432 di
->adc_main_charger_v
= devm_iio_channel_get(dev
, "main_charger_v");
3433 if (IS_ERR(di
->adc_main_charger_v
)) {
3434 ret
= dev_err_probe(dev
, PTR_ERR(di
->adc_main_charger_v
),
3435 "failed to get ADC main charger voltage\n");
3438 di
->adc_main_charger_c
= devm_iio_channel_get(dev
, "main_charger_c");
3439 if (IS_ERR(di
->adc_main_charger_c
)) {
3440 ret
= dev_err_probe(dev
, PTR_ERR(di
->adc_main_charger_c
),
3441 "failed to get ADC main charger current\n");
3445 di
->adc_vbus_v
= devm_iio_channel_get(dev
, "vbus_v");
3446 if (IS_ERR(di
->adc_vbus_v
)) {
3447 ret
= dev_err_probe(dev
, PTR_ERR(di
->adc_vbus_v
),
3448 "failed to get ADC USB charger voltage\n");
3451 di
->adc_usb_charger_c
= devm_iio_channel_get(dev
, "usb_charger_c");
3452 if (IS_ERR(di
->adc_usb_charger_c
)) {
3453 ret
= dev_err_probe(dev
, PTR_ERR(di
->adc_usb_charger_c
),
3454 "failed to get ADC USB charger current\n");
3459 * VDD ADC supply needs to be enabled from this driver when there
3460 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3461 * interrupts during charging
3463 di
->regu
= devm_regulator_get(dev
, "vddadc");
3464 if (IS_ERR(di
->regu
)) {
3465 ret
= PTR_ERR(di
->regu
);
3466 dev_err(dev
, "failed to get vddadc regulator\n");
3470 /* Request interrupts */
3471 for (i
= 0; i
< ARRAY_SIZE(ab8500_charger_irq
); i
++) {
3472 irq
= platform_get_irq_byname(pdev
, ab8500_charger_irq
[i
].name
);
3476 ret
= devm_request_threaded_irq(dev
,
3477 irq
, NULL
, ab8500_charger_irq
[i
].isr
,
3478 IRQF_SHARED
| IRQF_NO_SUSPEND
| IRQF_ONESHOT
,
3479 ab8500_charger_irq
[i
].name
, di
);
3482 dev_err(dev
, "failed to request %s IRQ %d: %d\n"
3483 , ab8500_charger_irq
[i
].name
, irq
, ret
);
3486 dev_dbg(dev
, "Requested %s IRQ %d: %d\n",
3487 ab8500_charger_irq
[i
].name
, irq
, ret
);
3490 /* initialize lock */
3491 spin_lock_init(&di
->usb_state
.usb_lock
);
3492 mutex_init(&di
->usb_ipt_crnt_lock
);
3494 di
->autopower
= false;
3495 di
->invalid_charger_detect_state
= 0;
3497 /* AC and USB supply config */
3498 ac_psy_cfg
.of_node
= np
;
3499 ac_psy_cfg
.supplied_to
= supply_interface
;
3500 ac_psy_cfg
.num_supplicants
= ARRAY_SIZE(supply_interface
);
3501 ac_psy_cfg
.drv_data
= &di
->ac_chg
;
3502 usb_psy_cfg
.of_node
= np
;
3503 usb_psy_cfg
.supplied_to
= supply_interface
;
3504 usb_psy_cfg
.num_supplicants
= ARRAY_SIZE(supply_interface
);
3505 usb_psy_cfg
.drv_data
= &di
->usb_chg
;
3508 /* ux500_charger sub-class */
3509 di
->ac_chg
.ops
.enable
= &ab8500_charger_ac_en
;
3510 di
->ac_chg
.ops
.check_enable
= &ab8500_charger_ac_check_enable
;
3511 di
->ac_chg
.ops
.kick_wd
= &ab8500_charger_watchdog_kick
;
3512 di
->ac_chg
.ops
.update_curr
= &ab8500_charger_update_charger_current
;
3513 di
->ac_chg
.max_out_volt_uv
= ab8500_charger_voltage_map
[
3514 ARRAY_SIZE(ab8500_charger_voltage_map
) - 1];
3515 di
->ac_chg
.max_out_curr_ua
=
3516 ab8500_charge_output_curr_map
[ARRAY_SIZE(ab8500_charge_output_curr_map
) - 1];
3517 di
->ac_chg
.wdt_refresh
= CHG_WD_INTERVAL
;
3519 * The AB8505 only supports USB charging. If we are not the
3520 * AB8505, register an AC charger.
3522 * TODO: if this should be opt-in, add DT properties for this.
3524 if (!is_ab8505(di
->parent
))
3525 di
->ac_chg
.enabled
= true;
3528 /* ux500_charger sub-class */
3529 di
->usb_chg
.ops
.enable
= &ab8500_charger_usb_en
;
3530 di
->usb_chg
.ops
.check_enable
= &ab8500_charger_usb_check_enable
;
3531 di
->usb_chg
.ops
.kick_wd
= &ab8500_charger_watchdog_kick
;
3532 di
->usb_chg
.ops
.update_curr
= &ab8500_charger_update_charger_current
;
3533 di
->usb_chg
.max_out_volt_uv
= ab8500_charger_voltage_map
[
3534 ARRAY_SIZE(ab8500_charger_voltage_map
) - 1];
3535 di
->usb_chg
.max_out_curr_ua
=
3536 ab8500_charge_output_curr_map
[ARRAY_SIZE(ab8500_charge_output_curr_map
) - 1];
3537 di
->usb_chg
.wdt_refresh
= CHG_WD_INTERVAL
;
3538 di
->usb_state
.usb_current_ua
= -1;
3540 mutex_init(&di
->charger_attached_mutex
);
3542 /* Init work for HW failure check */
3543 INIT_DEFERRABLE_WORK(&di
->check_hw_failure_work
,
3544 ab8500_charger_check_hw_failure_work
);
3545 INIT_DEFERRABLE_WORK(&di
->check_usbchgnotok_work
,
3546 ab8500_charger_check_usbchargernotok_work
);
3548 INIT_DELAYED_WORK(&di
->ac_charger_attached_work
,
3549 ab8500_charger_ac_attached_work
);
3550 INIT_DELAYED_WORK(&di
->usb_charger_attached_work
,
3551 ab8500_charger_usb_attached_work
);
3554 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3555 * logic. That means we have to continuously kick the charger
3556 * watchdog even when no charger is connected. This is only
3557 * valid once the AC charger has been enabled. This is
3558 * a bug that is not handled by the algorithm and the
3559 * watchdog have to be kicked by the charger driver
3560 * when the AC charger is disabled
3562 INIT_DEFERRABLE_WORK(&di
->kick_wd_work
,
3563 ab8500_charger_kick_watchdog_work
);
3565 INIT_DEFERRABLE_WORK(&di
->check_vbat_work
,
3566 ab8500_charger_check_vbat_work
);
3568 INIT_DELAYED_WORK(&di
->attach_work
,
3569 ab8500_charger_usb_link_attach_work
);
3571 INIT_DELAYED_WORK(&di
->usb_state_changed_work
,
3572 ab8500_charger_usb_state_changed_work
);
3574 INIT_DELAYED_WORK(&di
->vbus_drop_end_work
,
3575 ab8500_charger_vbus_drop_end_work
);
3577 /* Init work for charger detection */
3578 INIT_WORK(&di
->usb_link_status_work
,
3579 ab8500_charger_usb_link_status_work
);
3580 INIT_WORK(&di
->ac_work
, ab8500_charger_ac_work
);
3581 INIT_WORK(&di
->detect_usb_type_work
,
3582 ab8500_charger_detect_usb_type_work
);
3584 /* Init work for checking HW status */
3585 INIT_WORK(&di
->check_main_thermal_prot_work
,
3586 ab8500_charger_check_main_thermal_prot_work
);
3587 INIT_WORK(&di
->check_usb_thermal_prot_work
,
3588 ab8500_charger_check_usb_thermal_prot_work
);
3591 /* Initialize OVV, and other registers */
3592 ret
= ab8500_charger_init_hw_registers(di
);
3594 dev_err(dev
, "failed to initialize ABB registers\n");
3598 /* Register AC charger class */
3599 if (di
->ac_chg
.enabled
) {
3600 di
->ac_chg
.psy
= devm_power_supply_register(dev
,
3601 &ab8500_ac_chg_desc
,
3603 if (IS_ERR(di
->ac_chg
.psy
)) {
3604 dev_err(dev
, "failed to register AC charger\n");
3605 return PTR_ERR(di
->ac_chg
.psy
);
3609 /* Register USB charger class */
3610 di
->usb_chg
.psy
= devm_power_supply_register(dev
,
3611 &ab8500_usb_chg_desc
,
3613 if (IS_ERR(di
->usb_chg
.psy
)) {
3614 dev_err(dev
, "failed to register USB charger\n");
3615 return PTR_ERR(di
->usb_chg
.psy
);
3619 * Check what battery we have, since we always have the USB
3620 * psy, use that as a handle.
3622 ret
= ab8500_bm_of_probe(di
->usb_chg
.psy
, di
->bm
);
3624 return dev_err_probe(dev
, ret
,
3625 "failed to get battery information\n");
3627 /* Identify the connected charger types during startup */
3628 charger_status
= ab8500_charger_detect_chargers(di
, true);
3629 if (charger_status
& AC_PW_CONN
) {
3630 di
->ac
.charger_connected
= 1;
3632 ab8500_power_supply_changed(di
, di
->ac_chg
.psy
);
3633 sysfs_notify(&di
->ac_chg
.psy
->dev
.kobj
, NULL
, "present");
3636 platform_set_drvdata(pdev
, di
);
3638 /* Create something that will match the subdrivers when we bind */
3639 for (i
= 0; i
< ARRAY_SIZE(ab8500_charger_component_drivers
); i
++) {
3640 struct device_driver
*drv
= &ab8500_charger_component_drivers
[i
]->driver
;
3641 struct device
*p
= NULL
, *d
;
3643 while ((d
= platform_find_device_by_driver(p
, drv
))) {
3645 component_match_add(dev
, &match
, component_compare_dev
, d
);
3651 dev_err(dev
, "no matching components\n");
3653 goto remove_ab8500_bm
;
3655 if (IS_ERR(match
)) {
3656 dev_err(dev
, "could not create component match\n");
3657 ret
= PTR_ERR(match
);
3658 goto remove_ab8500_bm
;
3661 di
->usb_phy
= usb_get_phy(USB_PHY_TYPE_USB2
);
3662 if (IS_ERR_OR_NULL(di
->usb_phy
)) {
3663 dev_err(dev
, "failed to get usb transceiver\n");
3665 goto remove_ab8500_bm
;
3667 di
->nb
.notifier_call
= ab8500_charger_usb_notifier_call
;
3668 ret
= usb_register_notifier(di
->usb_phy
, &di
->nb
);
3670 dev_err(dev
, "failed to register usb notifier\n");
3674 ret
= component_master_add_with_match(&pdev
->dev
,
3675 &ab8500_charger_comp_ops
,
3678 dev_err(dev
, "failed to add component master\n");
3685 usb_unregister_notifier(di
->usb_phy
, &di
->nb
);
3687 usb_put_phy(di
->usb_phy
);
3689 ab8500_bm_of_remove(di
->usb_chg
.psy
, di
->bm
);
3693 static void ab8500_charger_remove(struct platform_device
*pdev
)
3695 struct ab8500_charger
*di
= platform_get_drvdata(pdev
);
3697 component_master_del(&pdev
->dev
, &ab8500_charger_comp_ops
);
3699 usb_unregister_notifier(di
->usb_phy
, &di
->nb
);
3700 ab8500_bm_of_remove(di
->usb_chg
.psy
, di
->bm
);
3701 usb_put_phy(di
->usb_phy
);
3704 static SIMPLE_DEV_PM_OPS(ab8500_charger_pm_ops
, ab8500_charger_suspend
, ab8500_charger_resume
);
3706 static const struct of_device_id ab8500_charger_match
[] = {
3707 { .compatible
= "stericsson,ab8500-charger", },
3710 MODULE_DEVICE_TABLE(of
, ab8500_charger_match
);
3712 static struct platform_driver ab8500_charger_driver
= {
3713 .probe
= ab8500_charger_probe
,
3714 .remove
= ab8500_charger_remove
,
3716 .name
= "ab8500-charger",
3717 .of_match_table
= ab8500_charger_match
,
3718 .pm
= &ab8500_charger_pm_ops
,
3722 static int __init
ab8500_charger_init(void)
3726 ret
= platform_register_drivers(ab8500_charger_component_drivers
,
3727 ARRAY_SIZE(ab8500_charger_component_drivers
));
3731 ret
= platform_driver_register(&ab8500_charger_driver
);
3733 platform_unregister_drivers(ab8500_charger_component_drivers
,
3734 ARRAY_SIZE(ab8500_charger_component_drivers
));
3741 static void __exit
ab8500_charger_exit(void)
3743 platform_unregister_drivers(ab8500_charger_component_drivers
,
3744 ARRAY_SIZE(ab8500_charger_component_drivers
));
3745 platform_driver_unregister(&ab8500_charger_driver
);
3748 module_init(ab8500_charger_init
);
3749 module_exit(ab8500_charger_exit
);
3751 MODULE_LICENSE("GPL v2");
3752 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3753 MODULE_ALIAS("platform:ab8500-charger");
3754 MODULE_DESCRIPTION("AB8500 charger management driver");