2 * Copyright (C) ST-Ericsson SA 2012
4 * Charger driver for AB8500
6 * License Terms: GNU General Public License v2
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/platform_device.h>
20 #include <linux/power_supply.h>
21 #include <linux/completion.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/err.h>
24 #include <linux/workqueue.h>
25 #include <linux/kobject.h>
27 #include <linux/mfd/core.h>
28 #include <linux/mfd/abx500/ab8500.h>
29 #include <linux/mfd/abx500.h>
30 #include <linux/mfd/abx500/ab8500-bm.h>
31 #include <linux/mfd/abx500/ab8500-gpadc.h>
32 #include <linux/mfd/abx500/ux500_chargalg.h>
33 #include <linux/usb/otg.h>
34 #include <linux/mutex.h>
36 /* Charger constants */
41 #define MAIN_WDOG_ENA 0x01
42 #define MAIN_WDOG_KICK 0x02
43 #define MAIN_WDOG_DIS 0x00
44 #define CHARG_WD_KICK 0x01
45 #define MAIN_CH_ENA 0x01
46 #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
47 #define USB_CH_ENA 0x01
48 #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
49 #define MAIN_CH_DET 0x01
50 #define MAIN_CH_CV_ON 0x04
51 #define USB_CH_CV_ON 0x08
52 #define VBUS_DET_DBNC100 0x02
53 #define VBUS_DET_DBNC1 0x01
54 #define OTP_ENABLE_WD 0x01
56 #define MAIN_CH_INPUT_CURR_SHIFT 4
57 #define VBUS_IN_CURR_LIM_SHIFT 4
59 #define LED_INDICATOR_PWM_ENA 0x01
60 #define LED_INDICATOR_PWM_DIS 0x00
61 #define LED_IND_CUR_5MA 0x04
62 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
64 /* HW failure constants */
65 #define MAIN_CH_TH_PROT 0x02
66 #define VBUS_CH_NOK 0x08
67 #define USB_CH_TH_PROT 0x02
68 #define VBUS_OVV_TH 0x01
69 #define MAIN_CH_NOK 0x01
72 #define MAIN_CH_STATUS2_MAINCHGDROP 0x80
73 #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
74 #define USB_CH_VBUSDROP 0x40
75 #define USB_CH_VBUSDETDBNC 0x01
77 /* UsbLineStatus register bit masks */
78 #define AB8500_USB_LINK_STATUS 0x78
79 #define AB8500_STD_HOST_SUSP 0x18
81 /* Watchdog timeout constant */
82 #define WD_TIMER 0x30 /* 4min */
83 #define WD_KICK_INTERVAL (60 * HZ)
85 /* Lowest charger voltage is 3.39V -> 0x4E */
86 #define LOW_VOLT_REG 0x4E
88 /* Step up/down delay in us */
89 #define STEP_UDELAY 1000
91 /* Wait for enumeration before charging in ms */
92 #define WAIT_FOR_USB_ENUMERATION 5 * 1000
94 #define CHARGER_STATUS_POLL 10 /* in ms */
96 /* UsbLineStatus register - usb types */
97 enum ab8500_charger_link_status {
98 USB_STAT_NOT_CONFIGURED,
100 USB_STAT_STD_HOST_C_NS,
101 USB_STAT_STD_HOST_C_S,
102 USB_STAT_HOST_CHG_NM,
103 USB_STAT_HOST_CHG_HS,
104 USB_STAT_HOST_CHG_HS_CHIRP,
105 USB_STAT_DEDICATED_CHG,
108 USB_STAT_ACA_RID_C_NM,
109 USB_STAT_ACA_RID_C_HS,
110 USB_STAT_ACA_RID_C_HS_CHIRP,
113 USB_STAT_NOT_VALID_LINK,
115 USB_STAT_SUP_NO_IDGND_VBUS,
116 USB_STAT_SUP_IDGND_VBUS,
117 USB_STAT_CHARGER_LINE_1,
120 USB_STAT_ACA_DOCK_CHARGER,
123 enum ab8500_usb_state {
124 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
125 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
126 AB8500_BM_USB_STATE_CONFIGURED,
127 AB8500_BM_USB_STATE_SUSPEND,
128 AB8500_BM_USB_STATE_RESUME,
129 AB8500_BM_USB_STATE_MAX,
132 /* VBUS input current limits supported in AB8500 in mA */
133 #define USB_CH_IP_CUR_LVL_0P05 50
134 #define USB_CH_IP_CUR_LVL_0P09 98
135 #define USB_CH_IP_CUR_LVL_0P19 193
136 #define USB_CH_IP_CUR_LVL_0P29 290
137 #define USB_CH_IP_CUR_LVL_0P38 380
138 #define USB_CH_IP_CUR_LVL_0P45 450
139 #define USB_CH_IP_CUR_LVL_0P5 500
140 #define USB_CH_IP_CUR_LVL_0P6 600
141 #define USB_CH_IP_CUR_LVL_0P7 700
142 #define USB_CH_IP_CUR_LVL_0P8 800
143 #define USB_CH_IP_CUR_LVL_0P9 900
144 #define USB_CH_IP_CUR_LVL_1P0 1000
145 #define USB_CH_IP_CUR_LVL_1P1 1100
146 #define USB_CH_IP_CUR_LVL_1P3 1300
147 #define USB_CH_IP_CUR_LVL_1P4 1400
148 #define USB_CH_IP_CUR_LVL_1P5 1500
150 #define VBAT_TRESH_IP_CUR_RED 3800
152 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
153 struct ab8500_charger, usb_chg)
154 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
155 struct ab8500_charger, ac_chg)
158 * struct ab8500_charger_interrupts - ab8500 interupts
159 * @name: name of the interrupt
160 * @isr function pointer to the isr
162 struct ab8500_charger_interrupts {
164 irqreturn_t (*isr)(int irq, void *data);
167 struct ab8500_charger_info {
168 int charger_connected;
176 struct ab8500_charger_event_flags {
178 bool main_thermal_prot;
179 bool usb_thermal_prot;
181 bool usbchargernotok;
186 struct ab8500_charger_usb_state {
189 enum ab8500_usb_state state;
194 * struct ab8500_charger - ab8500 Charger device information
195 * @dev: Pointer to the structure device
196 * @max_usb_in_curr: Max USB charger input current
197 * @vbus_detected: VBUS detected
198 * @vbus_detected_start:
199 * VBUS detected during startup
200 * @ac_conn: This will be true when the AC charger has been plugged
201 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
203 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
205 * @vbat Battery voltage
206 * @old_vbat Previously measured battery voltage
207 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
208 * @autopower Indicate if we should have automatic pwron after pwrloss
209 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
210 * @parent: Pointer to the struct ab8500
211 * @gpadc: Pointer to the struct gpadc
212 * @bm: Platform specific battery management information
213 * @flags: Structure for information about events triggered
214 * @usb_state: Structure for usb stack information
215 * @ac_chg: AC charger power supply
216 * @usb_chg: USB charger power supply
217 * @ac: Structure that holds the AC charger properties
218 * @usb: Structure that holds the USB charger properties
219 * @regu: Pointer to the struct regulator
220 * @charger_wq: Work queue for the IRQs and checking HW state
221 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
222 * @check_hw_failure_work: Work for checking HW state
223 * @check_usbchgnotok_work: Work for checking USB charger not ok status
224 * @kick_wd_work: Work for kicking the charger watchdog in case
225 * of ABB rev 1.* due to the watchog logic bug
226 * @attach_work: Work for checking the usb enumeration
227 * @ac_charger_attached_work: Work for checking if AC charger is still
229 * @usb_charger_attached_work: Work for checking if USB charger is still
231 * @ac_work: Work for checking AC charger connection
232 * @detect_usb_type_work: Work for detecting the USB type connected
233 * @usb_link_status_work: Work for checking the new USB link status
234 * @usb_state_changed_work: Work for checking USB state
235 * @check_main_thermal_prot_work:
236 * Work for checking Main thermal status
237 * @check_usb_thermal_prot_work:
238 * Work for checking USB thermal status
239 * @charger_attached_mutex: For controlling the wakelock
241 struct ab8500_charger {
245 bool vbus_detected_start;
251 bool usb_device_is_unrecognised;
254 struct ab8500 *parent;
255 struct ab8500_gpadc *gpadc;
256 struct abx500_bm_data *bm;
257 struct ab8500_charger_event_flags flags;
258 struct ab8500_charger_usb_state usb_state;
259 struct ux500_charger ac_chg;
260 struct ux500_charger usb_chg;
261 struct ab8500_charger_info ac;
262 struct ab8500_charger_info usb;
263 struct regulator *regu;
264 struct workqueue_struct *charger_wq;
265 struct delayed_work check_vbat_work;
266 struct delayed_work check_hw_failure_work;
267 struct delayed_work check_usbchgnotok_work;
268 struct delayed_work kick_wd_work;
269 struct delayed_work attach_work;
270 struct delayed_work ac_charger_attached_work;
271 struct delayed_work usb_charger_attached_work;
272 struct work_struct ac_work;
273 struct work_struct detect_usb_type_work;
274 struct work_struct usb_link_status_work;
275 struct work_struct usb_state_changed_work;
276 struct work_struct check_main_thermal_prot_work;
277 struct work_struct check_usb_thermal_prot_work;
278 struct usb_phy *usb_phy;
279 struct notifier_block nb;
280 struct mutex charger_attached_mutex;
284 static enum power_supply_property ab8500_charger_ac_props[] = {
285 POWER_SUPPLY_PROP_HEALTH,
286 POWER_SUPPLY_PROP_PRESENT,
287 POWER_SUPPLY_PROP_ONLINE,
288 POWER_SUPPLY_PROP_VOLTAGE_NOW,
289 POWER_SUPPLY_PROP_VOLTAGE_AVG,
290 POWER_SUPPLY_PROP_CURRENT_NOW,
294 static enum power_supply_property ab8500_charger_usb_props[] = {
295 POWER_SUPPLY_PROP_HEALTH,
296 POWER_SUPPLY_PROP_CURRENT_AVG,
297 POWER_SUPPLY_PROP_PRESENT,
298 POWER_SUPPLY_PROP_ONLINE,
299 POWER_SUPPLY_PROP_VOLTAGE_NOW,
300 POWER_SUPPLY_PROP_VOLTAGE_AVG,
301 POWER_SUPPLY_PROP_CURRENT_NOW,
305 * Function for enabling and disabling sw fallback mode
306 * should always be disabled when no charger is connected.
308 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
314 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
316 /* read the register containing fallback bit */
317 ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, ®);
319 dev_err(di->dev, "%d write failed\n", __LINE__);
323 /* enable the OPT emulation registers */
324 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
326 dev_err(di->dev, "%d write failed\n", __LINE__);
335 /* write back the changed fallback bit value to register */
336 ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg);
338 dev_err(di->dev, "%d write failed\n", __LINE__);
342 /* disable the set OTP registers again */
343 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
345 dev_err(di->dev, "%d write failed\n", __LINE__);
351 * ab8500_power_supply_changed - a wrapper with local extentions for
352 * power_supply_changed
353 * @di: pointer to the ab8500_charger structure
354 * @psy: pointer to power_supply_that have changed.
357 static void ab8500_power_supply_changed(struct ab8500_charger *di,
358 struct power_supply *psy)
360 if (di->autopower_cfg) {
361 if (!di->usb.charger_connected &&
362 !di->ac.charger_connected &&
364 di->autopower = false;
365 ab8500_enable_disable_sw_fallback(di, false);
366 } else if (!di->autopower &&
367 (di->ac.charger_connected ||
368 di->usb.charger_connected)) {
369 di->autopower = true;
370 ab8500_enable_disable_sw_fallback(di, true);
373 power_supply_changed(psy);
376 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
379 if (connected != di->usb.charger_connected) {
380 dev_dbg(di->dev, "USB connected:%i\n", connected);
381 di->usb.charger_connected = connected;
382 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
385 mutex_lock(&di->charger_attached_mutex);
386 mutex_unlock(&di->charger_attached_mutex);
388 queue_delayed_work(di->charger_wq,
389 &di->usb_charger_attached_work,
392 cancel_delayed_work_sync(&di->usb_charger_attached_work);
393 mutex_lock(&di->charger_attached_mutex);
394 mutex_unlock(&di->charger_attached_mutex);
400 * ab8500_charger_get_ac_voltage() - get ac charger voltage
401 * @di: pointer to the ab8500_charger structure
403 * Returns ac charger voltage (on success)
405 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
409 /* Only measure voltage if the charger is connected */
410 if (di->ac.charger_connected) {
411 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
413 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
421 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
422 * @di: pointer to the ab8500_charger structure
424 * Returns ac charger CV mode (on success) else error code
426 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
431 /* Only check CV mode if the charger is online */
432 if (di->ac.charger_online) {
433 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
434 AB8500_CH_STATUS1_REG, &val);
436 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
440 if (val & MAIN_CH_CV_ON)
450 * ab8500_charger_get_vbus_voltage() - get vbus voltage
451 * @di: pointer to the ab8500_charger structure
453 * This function returns the vbus voltage.
454 * Returns vbus voltage (on success)
456 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
460 /* Only measure voltage if the charger is connected */
461 if (di->usb.charger_connected) {
462 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
464 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
472 * ab8500_charger_get_usb_current() - get usb charger current
473 * @di: pointer to the ab8500_charger structure
475 * This function returns the usb charger current.
476 * Returns usb current (on success) and error code on failure
478 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
482 /* Only measure current if the charger is online */
483 if (di->usb.charger_online) {
484 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
486 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
494 * ab8500_charger_get_ac_current() - get ac charger current
495 * @di: pointer to the ab8500_charger structure
497 * This function returns the ac charger current.
498 * Returns ac current (on success) and error code on failure.
500 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
504 /* Only measure current if the charger is online */
505 if (di->ac.charger_online) {
506 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
508 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
516 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
517 * @di: pointer to the ab8500_charger structure
519 * Returns ac charger CV mode (on success) else error code
521 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
526 /* Only check CV mode if the charger is online */
527 if (di->usb.charger_online) {
528 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
529 AB8500_CH_USBCH_STAT1_REG, &val);
531 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
535 if (val & USB_CH_CV_ON)
547 * ab8500_charger_detect_chargers() - Detect the connected chargers
548 * @di: pointer to the ab8500_charger structure
550 * Returns the type of charger connected.
551 * For USB it will not mean we can actually charge from it
552 * but that there is a USB cable connected that we have to
553 * identify. This is used during startup when we don't get
554 * interrupts of the charger detection
556 * Returns an integer value, that means,
557 * NO_PW_CONN no power supply is connected
558 * AC_PW_CONN if the AC power supply is connected
559 * USB_PW_CONN if the USB power supply is connected
560 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
562 static int ab8500_charger_detect_chargers(struct ab8500_charger *di)
564 int result = NO_PW_CONN;
568 /* Check for AC charger */
569 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
570 AB8500_CH_STATUS1_REG, &val);
572 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
576 if (val & MAIN_CH_DET)
579 /* Check for USB charger */
580 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
581 AB8500_CH_USBCH_STAT1_REG, &val);
583 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
587 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
588 result |= USB_PW_CONN;
594 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
595 * @di: pointer to the ab8500_charger structure
596 * @link_status: the identified USB type
598 * Get the maximum current that is allowed to be drawn from the host
599 * based on the USB type.
600 * Returns error code in case of failure else 0 on success
602 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
603 enum ab8500_charger_link_status link_status)
607 di->usb_device_is_unrecognised = false;
609 switch (link_status) {
610 case USB_STAT_STD_HOST_NC:
611 case USB_STAT_STD_HOST_C_NS:
612 case USB_STAT_STD_HOST_C_S:
613 dev_dbg(di->dev, "USB Type - Standard host is "
614 "detected through USB driver\n");
615 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
617 case USB_STAT_HOST_CHG_HS_CHIRP:
618 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
619 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
620 di->max_usb_in_curr);
622 case USB_STAT_HOST_CHG_HS:
623 case USB_STAT_ACA_RID_C_HS:
624 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
625 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
626 di->max_usb_in_curr);
628 case USB_STAT_ACA_RID_A:
630 * Dedicated charger level minus maximum current accessory
631 * can consume (300mA). Closest level is 1100mA
633 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1;
634 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
635 di->max_usb_in_curr);
637 case USB_STAT_ACA_RID_B:
639 * Dedicated charger level minus 120mA (20mA for ACA and
640 * 100mA for potential accessory). Closest level is 1300mA
642 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
643 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
644 di->max_usb_in_curr);
646 case USB_STAT_HOST_CHG_NM:
647 case USB_STAT_DEDICATED_CHG:
648 case USB_STAT_ACA_RID_C_NM:
649 case USB_STAT_ACA_RID_C_HS_CHIRP:
650 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
651 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
652 di->max_usb_in_curr);
654 case USB_STAT_NOT_CONFIGURED:
655 if (di->vbus_detected) {
656 di->usb_device_is_unrecognised = true;
657 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
658 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
661 case USB_STAT_HM_IDGND:
662 case USB_STAT_NOT_VALID_LINK:
663 dev_err(di->dev, "USB Type - Charging not allowed\n");
664 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
667 case USB_STAT_RESERVED:
668 if (is_ab8500(di->parent)) {
669 di->flags.vbus_collapse = true;
670 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
671 "VBUS has collapsed\n");
675 if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
676 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
677 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
678 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
679 link_status, di->max_usb_in_curr);
684 case USB_STAT_CARKIT_1:
685 case USB_STAT_CARKIT_2:
686 case USB_STAT_ACA_DOCK_CHARGER:
687 case USB_STAT_CHARGER_LINE_1:
688 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
689 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
690 di->max_usb_in_curr);
694 dev_err(di->dev, "USB Type - Unknown\n");
695 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
700 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
701 link_status, di->max_usb_in_curr);
707 * ab8500_charger_read_usb_type() - read the type of usb connected
708 * @di: pointer to the ab8500_charger structure
710 * Detect the type of the plugged USB
711 * Returns error code in case of failure else 0 on success
713 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
718 ret = abx500_get_register_interruptible(di->dev,
719 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
721 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
724 if (is_ab8500(di->parent)) {
725 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
726 AB8500_USB_LINE_STAT_REG, &val);
728 if (is_ab9540(di->parent) || is_ab8505(di->parent))
729 ret = abx500_get_register_interruptible(di->dev,
730 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
733 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
737 /* get the USB type */
738 val = (val & AB8500_USB_LINK_STATUS) >> 3;
739 ret = ab8500_charger_max_usb_curr(di,
740 (enum ab8500_charger_link_status) val);
746 * ab8500_charger_detect_usb_type() - get the type of usb connected
747 * @di: pointer to the ab8500_charger structure
749 * Detect the type of the plugged USB
750 * Returns error code in case of failure else 0 on success
752 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
758 * On getting the VBUS rising edge detect interrupt there
759 * is a 250ms delay after which the register UsbLineStatus
760 * is filled with valid data.
762 for (i = 0; i < 10; i++) {
764 ret = abx500_get_register_interruptible(di->dev,
765 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
768 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
772 if (is_ab8500(di->parent))
773 ret = abx500_get_register_interruptible(di->dev,
774 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
776 ret = abx500_get_register_interruptible(di->dev,
777 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
779 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
783 * Until the IT source register is read the UsbLineStatus
784 * register is not updated, hence doing the same
788 /* get the USB type */
789 val = (val & AB8500_USB_LINK_STATUS) >> 3;
793 ret = ab8500_charger_max_usb_curr(di,
794 (enum ab8500_charger_link_status) val);
800 * This array maps the raw hex value to charger voltage used by the AB8500
801 * Values taken from the UM0836
803 static int ab8500_charger_voltage_map[] = {
885 * This array maps the raw hex value to charger current used by the AB8500
886 * Values taken from the UM0836
888 static int ab8500_charger_current_map[] = {
907 * This array maps the raw hex value to VBUS input current used by the AB8500
908 * Values taken from the UM0836
910 static int ab8500_charger_vbus_in_curr_map[] = {
911 USB_CH_IP_CUR_LVL_0P05,
912 USB_CH_IP_CUR_LVL_0P09,
913 USB_CH_IP_CUR_LVL_0P19,
914 USB_CH_IP_CUR_LVL_0P29,
915 USB_CH_IP_CUR_LVL_0P38,
916 USB_CH_IP_CUR_LVL_0P45,
917 USB_CH_IP_CUR_LVL_0P5,
918 USB_CH_IP_CUR_LVL_0P6,
919 USB_CH_IP_CUR_LVL_0P7,
920 USB_CH_IP_CUR_LVL_0P8,
921 USB_CH_IP_CUR_LVL_0P9,
922 USB_CH_IP_CUR_LVL_1P0,
923 USB_CH_IP_CUR_LVL_1P1,
924 USB_CH_IP_CUR_LVL_1P3,
925 USB_CH_IP_CUR_LVL_1P4,
926 USB_CH_IP_CUR_LVL_1P5,
929 static int ab8500_voltage_to_regval(int voltage)
933 /* Special case for voltage below 3.5V */
934 if (voltage < ab8500_charger_voltage_map[0])
937 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
938 if (voltage < ab8500_charger_voltage_map[i])
942 /* If not last element, return error */
943 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
944 if (voltage == ab8500_charger_voltage_map[i])
950 static int ab8500_current_to_regval(int curr)
954 if (curr < ab8500_charger_current_map[0])
957 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
958 if (curr < ab8500_charger_current_map[i])
962 /* If not last element, return error */
963 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
964 if (curr == ab8500_charger_current_map[i])
970 static int ab8500_vbus_in_curr_to_regval(int curr)
974 if (curr < ab8500_charger_vbus_in_curr_map[0])
977 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
978 if (curr < ab8500_charger_vbus_in_curr_map[i])
982 /* If not last element, return error */
983 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
984 if (curr == ab8500_charger_vbus_in_curr_map[i])
991 * ab8500_charger_get_usb_cur() - get usb current
992 * @di: pointer to the ab8500_charger structre
994 * The usb stack provides the maximum current that can be drawn from
995 * the standard usb host. This will be in mA.
996 * This function converts current in mA to a value that can be written
997 * to the register. Returns -1 if charging is not allowed
999 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1001 switch (di->usb_state.usb_current) {
1003 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
1006 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
1009 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
1012 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
1015 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
1018 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
1026 * ab8500_charger_set_current() - set charger current
1027 * @di: pointer to the ab8500_charger structure
1028 * @ich: charger current, in mA
1029 * @reg: select what charger register to set
1031 * Set charger current.
1032 * There is no state machine in the AB to step up/down the charger
1033 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1034 * charging is started. Instead we need to implement
1035 * this charger current step-up/down here.
1036 * Returns error code in case of failure else 0(on success)
1038 static int ab8500_charger_set_current(struct ab8500_charger *di,
1042 int curr_index, prev_curr_index, shift_value;
1046 case AB8500_MCH_IPT_CURLVL_REG:
1047 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1048 curr_index = ab8500_current_to_regval(ich);
1050 case AB8500_USBCH_IPT_CRNTLVL_REG:
1051 shift_value = VBUS_IN_CURR_LIM_SHIFT;
1052 curr_index = ab8500_vbus_in_curr_to_regval(ich);
1054 case AB8500_CH_OPT_CRNTLVL_REG:
1056 curr_index = ab8500_current_to_regval(ich);
1059 dev_err(di->dev, "%s current register not valid\n", __func__);
1063 if (curr_index < 0) {
1064 dev_err(di->dev, "requested current limit out-of-range\n");
1068 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1071 dev_err(di->dev, "%s read failed\n", __func__);
1074 prev_curr_index = (reg_value >> shift_value);
1076 /* only update current if it's been changed */
1077 if (prev_curr_index == curr_index)
1080 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1081 __func__, ich, reg);
1083 if (prev_curr_index > curr_index) {
1084 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1085 ret = abx500_set_register_interruptible(di->dev,
1086 AB8500_CHARGER, reg, (u8) i << shift_value);
1088 dev_err(di->dev, "%s write failed\n", __func__);
1091 usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
1094 for (i = prev_curr_index + 1; i <= curr_index; i++) {
1095 ret = abx500_set_register_interruptible(di->dev,
1096 AB8500_CHARGER, reg, (u8) i << shift_value);
1098 dev_err(di->dev, "%s write failed\n", __func__);
1101 usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
1108 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1109 * @di: pointer to the ab8500_charger structure
1110 * @ich_in: charger input current limit
1112 * Sets the current that can be drawn from the USB host
1113 * Returns error code in case of failure else 0(on success)
1115 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1120 /* We should always use to lowest current limit */
1121 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1123 switch (min_value) {
1125 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1126 min_value = USB_CH_IP_CUR_LVL_0P05;
1129 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1130 min_value = USB_CH_IP_CUR_LVL_0P45;
1136 return ab8500_charger_set_current(di, min_value,
1137 AB8500_USBCH_IPT_CRNTLVL_REG);
1141 * ab8500_charger_set_main_in_curr() - set main charger input current
1142 * @di: pointer to the ab8500_charger structure
1143 * @ich_in: input charger current, in mA
1145 * Set main charger input current.
1146 * Returns error code in case of failure else 0(on success)
1148 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1151 return ab8500_charger_set_current(di, ich_in,
1152 AB8500_MCH_IPT_CURLVL_REG);
1156 * ab8500_charger_set_output_curr() - set charger output current
1157 * @di: pointer to the ab8500_charger structure
1158 * @ich_out: output charger current, in mA
1160 * Set charger output current.
1161 * Returns error code in case of failure else 0(on success)
1163 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1166 return ab8500_charger_set_current(di, ich_out,
1167 AB8500_CH_OPT_CRNTLVL_REG);
1171 * ab8500_charger_led_en() - turn on/off chargign led
1172 * @di: pointer to the ab8500_charger structure
1173 * @on: flag to turn on/off the chargign led
1175 * Power ON/OFF charging LED indication
1176 * Returns error code in case of failure else 0(on success)
1178 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1183 /* Power ON charging LED indicator, set LED current to 5mA */
1184 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1185 AB8500_LED_INDICATOR_PWM_CTRL,
1186 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1188 dev_err(di->dev, "Power ON LED failed\n");
1191 /* LED indicator PWM duty cycle 252/256 */
1192 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1193 AB8500_LED_INDICATOR_PWM_DUTY,
1194 LED_INDICATOR_PWM_DUTY_252_256);
1196 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1200 /* Power off charging LED indicator */
1201 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1202 AB8500_LED_INDICATOR_PWM_CTRL,
1203 LED_INDICATOR_PWM_DIS);
1205 dev_err(di->dev, "Power-off LED failed\n");
1214 * ab8500_charger_ac_en() - enable or disable ac charging
1215 * @di: pointer to the ab8500_charger structure
1216 * @enable: enable/disable flag
1217 * @vset: charging voltage
1218 * @iset: charging current
1220 * Enable/Disable AC/Mains charging and turns on/off the charging led
1223 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1224 int enable, int vset, int iset)
1229 int input_curr_index;
1232 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1235 /* Check if AC is connected */
1236 if (!di->ac.charger_connected) {
1237 dev_err(di->dev, "AC charger not connected\n");
1241 /* Enable AC charging */
1242 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1245 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1246 * will be triggered everytime we enable the VDD ADC supply.
1247 * This will turn off charging for a short while.
1248 * It can be avoided by having the supply on when
1249 * there is a charger enabled. Normally the VDD ADC supply
1250 * is enabled everytime a GPADC conversion is triggered. We will
1251 * force it to be enabled from this driver to have
1252 * the GPADC module independant of the AB8500 chargers
1254 if (!di->vddadc_en_ac) {
1255 regulator_enable(di->regu);
1256 di->vddadc_en_ac = true;
1259 /* Check if the requested voltage or current is valid */
1260 volt_index = ab8500_voltage_to_regval(vset);
1261 curr_index = ab8500_current_to_regval(iset);
1262 input_curr_index = ab8500_current_to_regval(
1263 di->bm->chg_params->ac_curr_max);
1264 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1266 "Charger voltage or current too high, "
1267 "charging not started\n");
1271 /* ChVoltLevel: maximum battery charging voltage */
1272 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1273 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1275 dev_err(di->dev, "%s write failed\n", __func__);
1278 /* MainChInputCurr: current that can be drawn from the charger*/
1279 ret = ab8500_charger_set_main_in_curr(di,
1280 di->bm->chg_params->ac_curr_max);
1282 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1286 /* ChOutputCurentLevel: protected output current */
1287 ret = ab8500_charger_set_output_curr(di, iset);
1289 dev_err(di->dev, "%s "
1290 "Failed to set ChOutputCurentLevel\n",
1295 /* Check if VBAT overshoot control should be enabled */
1296 if (!di->bm->enable_overshoot)
1297 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1299 /* Enable Main Charger */
1300 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1301 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1303 dev_err(di->dev, "%s write failed\n", __func__);
1307 /* Power on charging LED indication */
1308 ret = ab8500_charger_led_en(di, true);
1310 dev_err(di->dev, "failed to enable LED\n");
1312 di->ac.charger_online = 1;
1314 /* Disable AC charging */
1315 if (is_ab8500_1p1_or_earlier(di->parent)) {
1317 * For ABB revision 1.0 and 1.1 there is a bug in the
1318 * watchdog logic. That means we have to continously
1319 * kick the charger watchdog even when no charger is
1320 * connected. This is only valid once the AC charger
1321 * has been enabled. This is a bug that is not handled
1322 * by the algorithm and the watchdog have to be kicked
1323 * by the charger driver when the AC charger
1327 queue_delayed_work(di->charger_wq,
1329 round_jiffies(WD_KICK_INTERVAL));
1333 * We can't turn off charging completely
1334 * due to a bug in AB8500 cut1.
1335 * If we do, charging will not start again.
1336 * That is why we set the lowest voltage
1337 * and current possible
1339 ret = abx500_set_register_interruptible(di->dev,
1341 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1344 "%s write failed\n", __func__);
1348 ret = ab8500_charger_set_output_curr(di, 0);
1350 dev_err(di->dev, "%s "
1351 "Failed to set ChOutputCurentLevel\n",
1356 ret = abx500_set_register_interruptible(di->dev,
1358 AB8500_MCH_CTRL1, 0);
1361 "%s write failed\n", __func__);
1366 ret = ab8500_charger_led_en(di, false);
1368 dev_err(di->dev, "failed to disable LED\n");
1370 di->ac.charger_online = 0;
1371 di->ac.wd_expired = false;
1373 /* Disable regulator if enabled */
1374 if (di->vddadc_en_ac) {
1375 regulator_disable(di->regu);
1376 di->vddadc_en_ac = false;
1379 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1381 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1387 * ab8500_charger_usb_en() - enable usb charging
1388 * @di: pointer to the ab8500_charger structure
1389 * @enable: enable/disable flag
1390 * @vset: charging voltage
1391 * @ich_out: charger output current
1393 * Enable/Disable USB charging and turns on/off the charging led respectively.
1394 * Returns error code in case of failure else 0(on success)
1396 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1397 int enable, int vset, int ich_out)
1404 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1407 /* Check if USB is connected */
1408 if (!di->usb.charger_connected) {
1409 dev_err(di->dev, "USB charger not connected\n");
1414 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1415 * will be triggered everytime we enable the VDD ADC supply.
1416 * This will turn off charging for a short while.
1417 * It can be avoided by having the supply on when
1418 * there is a charger enabled. Normally the VDD ADC supply
1419 * is enabled everytime a GPADC conversion is triggered. We will
1420 * force it to be enabled from this driver to have
1421 * the GPADC module independant of the AB8500 chargers
1423 if (!di->vddadc_en_usb) {
1424 regulator_enable(di->regu);
1425 di->vddadc_en_usb = true;
1428 /* Enable USB charging */
1429 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1431 /* Check if the requested voltage or current is valid */
1432 volt_index = ab8500_voltage_to_regval(vset);
1433 curr_index = ab8500_current_to_regval(ich_out);
1434 if (volt_index < 0 || curr_index < 0) {
1436 "Charger voltage or current too high, "
1437 "charging not started\n");
1441 /* ChVoltLevel: max voltage upto which battery can be charged */
1442 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1443 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1445 dev_err(di->dev, "%s write failed\n", __func__);
1448 /* USBChInputCurr: current that can be drawn from the usb */
1449 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1451 dev_err(di->dev, "setting USBChInputCurr failed\n");
1454 /* ChOutputCurentLevel: protected output current */
1455 ret = ab8500_charger_set_output_curr(di, ich_out);
1457 dev_err(di->dev, "%s "
1458 "Failed to set ChOutputCurentLevel\n",
1462 /* Check if VBAT overshoot control should be enabled */
1463 if (!di->bm->enable_overshoot)
1464 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1466 /* Enable USB Charger */
1467 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1468 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1470 dev_err(di->dev, "%s write failed\n", __func__);
1474 /* If success power on charging LED indication */
1475 ret = ab8500_charger_led_en(di, true);
1477 dev_err(di->dev, "failed to enable LED\n");
1479 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1481 di->usb.charger_online = 1;
1483 /* Disable USB charging */
1484 ret = abx500_set_register_interruptible(di->dev,
1486 AB8500_USBCH_CTRL1_REG, 0);
1489 "%s write failed\n", __func__);
1493 ret = ab8500_charger_led_en(di, false);
1495 dev_err(di->dev, "failed to disable LED\n");
1497 di->usb.charger_online = 0;
1498 di->usb.wd_expired = false;
1500 /* Disable regulator if enabled */
1501 if (di->vddadc_en_usb) {
1502 regulator_disable(di->regu);
1503 di->vddadc_en_usb = false;
1506 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1508 /* Cancel any pending Vbat check work */
1509 if (delayed_work_pending(&di->check_vbat_work))
1510 cancel_delayed_work(&di->check_vbat_work);
1513 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1519 * ab8500_charger_watchdog_kick() - kick charger watchdog
1520 * @di: pointer to the ab8500_charger structure
1522 * Kick charger watchdog
1523 * Returns error code in case of failure else 0(on success)
1525 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1528 struct ab8500_charger *di;
1530 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1531 di = to_ab8500_charger_ac_device_info(charger);
1532 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1533 di = to_ab8500_charger_usb_device_info(charger);
1537 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1538 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1540 dev_err(di->dev, "Failed to kick WD!\n");
1546 * ab8500_charger_update_charger_current() - update charger current
1547 * @di: pointer to the ab8500_charger structure
1549 * Update the charger output current for the specified charger
1550 * Returns error code in case of failure else 0(on success)
1552 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1556 struct ab8500_charger *di;
1558 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1559 di = to_ab8500_charger_ac_device_info(charger);
1560 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1561 di = to_ab8500_charger_usb_device_info(charger);
1565 ret = ab8500_charger_set_output_curr(di, ich_out);
1567 dev_err(di->dev, "%s "
1568 "Failed to set ChOutputCurentLevel\n",
1573 /* Reset the main and usb drop input current measurement counter */
1574 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1575 AB8500_CHARGER_CTRL,
1578 dev_err(di->dev, "%s write failed\n", __func__);
1585 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1587 struct power_supply *psy;
1588 struct power_supply *ext;
1589 struct ab8500_charger *di;
1590 union power_supply_propval ret;
1592 bool psy_found = false;
1593 struct ux500_charger *usb_chg;
1595 usb_chg = (struct ux500_charger *)data;
1596 psy = &usb_chg->psy;
1598 di = to_ab8500_charger_usb_device_info(usb_chg);
1600 ext = dev_get_drvdata(dev);
1602 /* For all psy where the driver name appears in any supplied_to */
1603 for (i = 0; i < ext->num_supplicants; i++) {
1604 if (!strcmp(ext->supplied_to[i], psy->name))
1611 /* Go through all properties for the psy */
1612 for (j = 0; j < ext->num_properties; j++) {
1613 enum power_supply_property prop;
1614 prop = ext->properties[j];
1616 if (ext->get_property(ext, prop, &ret))
1620 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1621 switch (ext->type) {
1622 case POWER_SUPPLY_TYPE_BATTERY:
1623 di->vbat = ret.intval / 1000;
1637 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1638 * @work pointer to the work_struct structure
1640 * Due to a asic bug it is necessary to lower the input current to the vbus
1641 * charger when charging with at some specific levels. This issue is only valid
1642 * for below a certain battery voltage. This function makes sure that the
1643 * the allowed current limit isn't exceeded.
1645 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1648 struct ab8500_charger *di = container_of(work,
1649 struct ab8500_charger, check_vbat_work.work);
1651 class_for_each_device(power_supply_class, NULL,
1652 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1654 /* First run old_vbat is 0. */
1655 if (di->old_vbat == 0)
1656 di->old_vbat = di->vbat;
1658 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1659 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1660 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1661 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1663 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1664 " old: %d\n", di->max_usb_in_curr, di->vbat,
1666 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1667 power_supply_changed(&di->usb_chg.psy);
1670 di->old_vbat = di->vbat;
1673 * No need to check the battery voltage every second when not close to
1676 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1677 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1680 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1684 * ab8500_charger_check_hw_failure_work() - check main charger failure
1685 * @work: pointer to the work_struct structure
1687 * Work queue function for checking the main charger status
1689 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1694 struct ab8500_charger *di = container_of(work,
1695 struct ab8500_charger, check_hw_failure_work.work);
1697 /* Check if the status bits for HW failure is still active */
1698 if (di->flags.mainextchnotok) {
1699 ret = abx500_get_register_interruptible(di->dev,
1700 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1702 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1705 if (!(reg_value & MAIN_CH_NOK)) {
1706 di->flags.mainextchnotok = false;
1707 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1710 if (di->flags.vbus_ovv) {
1711 ret = abx500_get_register_interruptible(di->dev,
1712 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1715 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1718 if (!(reg_value & VBUS_OVV_TH)) {
1719 di->flags.vbus_ovv = false;
1720 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1723 /* If we still have a failure, schedule a new check */
1724 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1725 queue_delayed_work(di->charger_wq,
1726 &di->check_hw_failure_work, round_jiffies(HZ));
1731 * ab8500_charger_kick_watchdog_work() - kick the watchdog
1732 * @work: pointer to the work_struct structure
1734 * Work queue function for kicking the charger watchdog.
1736 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
1737 * logic. That means we have to continously kick the charger
1738 * watchdog even when no charger is connected. This is only
1739 * valid once the AC charger has been enabled. This is
1740 * a bug that is not handled by the algorithm and the
1741 * watchdog have to be kicked by the charger driver
1742 * when the AC charger is disabled
1744 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
1748 struct ab8500_charger *di = container_of(work,
1749 struct ab8500_charger, kick_wd_work.work);
1751 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1752 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1754 dev_err(di->dev, "Failed to kick WD!\n");
1756 /* Schedule a new watchdog kick */
1757 queue_delayed_work(di->charger_wq,
1758 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
1762 * ab8500_charger_ac_work() - work to get and set main charger status
1763 * @work: pointer to the work_struct structure
1765 * Work queue function for checking the main charger status
1767 static void ab8500_charger_ac_work(struct work_struct *work)
1771 struct ab8500_charger *di = container_of(work,
1772 struct ab8500_charger, ac_work);
1775 * Since we can't be sure that the events are received
1776 * synchronously, we have the check if the main charger is
1777 * connected by reading the status register
1779 ret = ab8500_charger_detect_chargers(di);
1783 if (ret & AC_PW_CONN) {
1784 di->ac.charger_connected = 1;
1787 di->ac.charger_connected = 0;
1790 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1791 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
1794 static void ab8500_charger_usb_attached_work(struct work_struct *work)
1796 struct ab8500_charger *di = container_of(work,
1797 struct ab8500_charger,
1798 usb_charger_attached_work.work);
1799 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
1803 for (i = 0; i < 10; i++) {
1804 ret = abx500_get_register_interruptible(di->dev,
1806 AB8500_CH_USBCH_STAT1_REG,
1809 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1812 if ((statval & usbch) != usbch)
1815 msleep(CHARGER_STATUS_POLL);
1818 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
1820 mutex_lock(&di->charger_attached_mutex);
1821 mutex_unlock(&di->charger_attached_mutex);
1826 queue_delayed_work(di->charger_wq,
1827 &di->usb_charger_attached_work,
1831 static void ab8500_charger_ac_attached_work(struct work_struct *work)
1834 struct ab8500_charger *di = container_of(work,
1835 struct ab8500_charger,
1836 ac_charger_attached_work.work);
1837 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
1838 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
1842 for (i = 0; i < 10; i++) {
1843 ret = abx500_get_register_interruptible(di->dev,
1845 AB8500_CH_STATUS2_REG,
1848 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1852 if ((statval & mainch) != mainch)
1855 msleep(CHARGER_STATUS_POLL);
1858 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
1859 queue_work(di->charger_wq, &di->ac_work);
1861 mutex_lock(&di->charger_attached_mutex);
1862 mutex_unlock(&di->charger_attached_mutex);
1867 queue_delayed_work(di->charger_wq,
1868 &di->ac_charger_attached_work,
1873 * ab8500_charger_detect_usb_type_work() - work to detect USB type
1874 * @work: Pointer to the work_struct structure
1876 * Detect the type of USB plugged
1878 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
1882 struct ab8500_charger *di = container_of(work,
1883 struct ab8500_charger, detect_usb_type_work);
1886 * Since we can't be sure that the events are received
1887 * synchronously, we have the check if is
1888 * connected by reading the status register
1890 ret = ab8500_charger_detect_chargers(di);
1894 if (!(ret & USB_PW_CONN)) {
1895 di->vbus_detected = 0;
1896 ab8500_charger_set_usb_connected(di, false);
1897 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1899 di->vbus_detected = 1;
1901 if (is_ab8500_1p1_or_earlier(di->parent)) {
1902 ret = ab8500_charger_detect_usb_type(di);
1904 ab8500_charger_set_usb_connected(di, true);
1905 ab8500_power_supply_changed(di,
1909 /* For ABB cut2.0 and onwards we have an IRQ,
1910 * USB_LINK_STATUS that will be triggered when the USB
1911 * link status changes. The exception is USB connected
1912 * during startup. Then we don't get a
1913 * USB_LINK_STATUS IRQ
1915 if (di->vbus_detected_start) {
1916 di->vbus_detected_start = false;
1917 ret = ab8500_charger_detect_usb_type(di);
1919 ab8500_charger_set_usb_connected(di,
1921 ab8500_power_supply_changed(di,
1930 * ab8500_charger_usb_link_attach_work() - delayd work to detect USB type
1931 * @work: pointer to the work_struct structure
1933 * Detect the type of USB plugged
1935 static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
1937 struct ab8500_charger *di =
1938 container_of(work, struct ab8500_charger, attach_work.work);
1941 /* Update maximum input current if USB enumeration is not detected */
1942 if (!di->usb.charger_online) {
1943 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1948 ab8500_charger_set_usb_connected(di, true);
1949 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1953 * ab8500_charger_usb_link_status_work() - work to detect USB type
1954 * @work: pointer to the work_struct structure
1956 * Detect the type of USB plugged
1958 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
1962 struct ab8500_charger *di = container_of(work,
1963 struct ab8500_charger, usb_link_status_work);
1966 * Since we can't be sure that the events are received
1967 * synchronously, we have the check if is
1968 * connected by reading the status register
1970 ret = ab8500_charger_detect_chargers(di);
1974 if (!(ret & USB_PW_CONN)) {
1975 di->vbus_detected = 0;
1976 ab8500_charger_set_usb_connected(di, false);
1977 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1981 di->vbus_detected = 1;
1982 ret = ab8500_charger_read_usb_type(di);
1984 if (di->usb_device_is_unrecognised) {
1986 "Potential Legacy Charger device. "
1987 "Delay work for %d msec for USB enum "
1989 WAIT_FOR_USB_ENUMERATION);
1990 queue_delayed_work(di->charger_wq,
1992 msecs_to_jiffies(WAIT_FOR_USB_ENUMERATION));
1994 queue_delayed_work(di->charger_wq,
1995 &di->attach_work, 0);
1997 } else if (ret == -ENXIO) {
1998 /* No valid charger type detected */
1999 ab8500_charger_set_usb_connected(di, false);
2000 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2004 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2007 unsigned long flags;
2009 struct ab8500_charger *di = container_of(work,
2010 struct ab8500_charger, usb_state_changed_work);
2012 if (!di->vbus_detected)
2015 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2016 di->usb_state.usb_changed = false;
2017 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2020 * wait for some time until you get updates from the usb stack
2021 * and negotiations are completed
2025 if (di->usb_state.usb_changed)
2028 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2029 __func__, di->usb_state.state, di->usb_state.usb_current);
2031 switch (di->usb_state.state) {
2032 case AB8500_BM_USB_STATE_RESET_HS:
2033 case AB8500_BM_USB_STATE_RESET_FS:
2034 case AB8500_BM_USB_STATE_SUSPEND:
2035 case AB8500_BM_USB_STATE_MAX:
2036 ab8500_charger_set_usb_connected(di, false);
2037 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2040 case AB8500_BM_USB_STATE_RESUME:
2042 * when suspend->resume there should be delay
2043 * of 1sec for enabling charging
2046 /* Intentional fall through */
2047 case AB8500_BM_USB_STATE_CONFIGURED:
2049 * USB is configured, enable charging with the charging
2050 * input current obtained from USB driver
2052 if (!ab8500_charger_get_usb_cur(di)) {
2053 /* Update maximum input current */
2054 ret = ab8500_charger_set_vbus_in_curr(di,
2055 di->max_usb_in_curr);
2059 ab8500_charger_set_usb_connected(di, true);
2060 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2070 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2071 * @work: pointer to the work_struct structure
2073 * Work queue function for checking the USB charger Not OK status
2075 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2081 struct ab8500_charger *di = container_of(work,
2082 struct ab8500_charger, check_usbchgnotok_work.work);
2084 /* Check if the status bit for usbchargernotok is still active */
2085 ret = abx500_get_register_interruptible(di->dev,
2086 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2088 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2091 prev_status = di->flags.usbchargernotok;
2093 if (reg_value & VBUS_CH_NOK) {
2094 di->flags.usbchargernotok = true;
2095 /* Check again in 1sec */
2096 queue_delayed_work(di->charger_wq,
2097 &di->check_usbchgnotok_work, HZ);
2099 di->flags.usbchargernotok = false;
2100 di->flags.vbus_collapse = false;
2103 if (prev_status != di->flags.usbchargernotok)
2104 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2108 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2109 * @work: pointer to the work_struct structure
2111 * Work queue function for checking the Main thermal prot status
2113 static void ab8500_charger_check_main_thermal_prot_work(
2114 struct work_struct *work)
2119 struct ab8500_charger *di = container_of(work,
2120 struct ab8500_charger, check_main_thermal_prot_work);
2122 /* Check if the status bit for main_thermal_prot is still active */
2123 ret = abx500_get_register_interruptible(di->dev,
2124 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2126 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2129 if (reg_value & MAIN_CH_TH_PROT)
2130 di->flags.main_thermal_prot = true;
2132 di->flags.main_thermal_prot = false;
2134 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2138 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2139 * @work: pointer to the work_struct structure
2141 * Work queue function for checking the USB thermal prot status
2143 static void ab8500_charger_check_usb_thermal_prot_work(
2144 struct work_struct *work)
2149 struct ab8500_charger *di = container_of(work,
2150 struct ab8500_charger, check_usb_thermal_prot_work);
2152 /* Check if the status bit for usb_thermal_prot is still active */
2153 ret = abx500_get_register_interruptible(di->dev,
2154 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2156 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2159 if (reg_value & USB_CH_TH_PROT)
2160 di->flags.usb_thermal_prot = true;
2162 di->flags.usb_thermal_prot = false;
2164 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2168 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2169 * @irq: interrupt number
2170 * @_di: pointer to the ab8500_charger structure
2172 * Returns IRQ status(IRQ_HANDLED)
2174 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2176 struct ab8500_charger *di = _di;
2178 dev_dbg(di->dev, "Main charger unplugged\n");
2179 queue_work(di->charger_wq, &di->ac_work);
2181 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2182 mutex_lock(&di->charger_attached_mutex);
2183 mutex_unlock(&di->charger_attached_mutex);
2189 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2190 * @irq: interrupt number
2191 * @_di: pointer to the ab8500_charger structure
2193 * Returns IRQ status(IRQ_HANDLED)
2195 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2197 struct ab8500_charger *di = _di;
2199 dev_dbg(di->dev, "Main charger plugged\n");
2200 queue_work(di->charger_wq, &di->ac_work);
2202 mutex_lock(&di->charger_attached_mutex);
2203 mutex_unlock(&di->charger_attached_mutex);
2204 queue_delayed_work(di->charger_wq,
2205 &di->ac_charger_attached_work,
2211 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2212 * @irq: interrupt number
2213 * @_di: pointer to the ab8500_charger structure
2215 * Returns IRQ status(IRQ_HANDLED)
2217 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2219 struct ab8500_charger *di = _di;
2221 dev_dbg(di->dev, "Main charger not ok\n");
2222 di->flags.mainextchnotok = true;
2223 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2225 /* Schedule a new HW failure check */
2226 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2232 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2233 * thermal protection threshold
2234 * @irq: interrupt number
2235 * @_di: pointer to the ab8500_charger structure
2237 * Returns IRQ status(IRQ_HANDLED)
2239 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2241 struct ab8500_charger *di = _di;
2244 "Die temp above Main charger thermal protection threshold\n");
2245 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2251 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2252 * thermal protection threshold
2253 * @irq: interrupt number
2254 * @_di: pointer to the ab8500_charger structure
2256 * Returns IRQ status(IRQ_HANDLED)
2258 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2260 struct ab8500_charger *di = _di;
2263 "Die temp ok for Main charger thermal protection threshold\n");
2264 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2270 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2271 * @irq: interrupt number
2272 * @_di: pointer to the ab8500_charger structure
2274 * Returns IRQ status(IRQ_HANDLED)
2276 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2278 struct ab8500_charger *di = _di;
2280 dev_dbg(di->dev, "VBUS falling detected\n");
2281 queue_work(di->charger_wq, &di->detect_usb_type_work);
2287 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2288 * @irq: interrupt number
2289 * @_di: pointer to the ab8500_charger structure
2291 * Returns IRQ status(IRQ_HANDLED)
2293 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2295 struct ab8500_charger *di = _di;
2297 di->vbus_detected = true;
2298 dev_dbg(di->dev, "VBUS rising detected\n");
2299 queue_work(di->charger_wq, &di->detect_usb_type_work);
2305 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2306 * @irq: interrupt number
2307 * @_di: pointer to the ab8500_charger structure
2309 * Returns IRQ status(IRQ_HANDLED)
2311 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2313 struct ab8500_charger *di = _di;
2315 dev_dbg(di->dev, "USB link status changed\n");
2317 queue_work(di->charger_wq, &di->usb_link_status_work);
2323 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2324 * thermal protection threshold
2325 * @irq: interrupt number
2326 * @_di: pointer to the ab8500_charger structure
2328 * Returns IRQ status(IRQ_HANDLED)
2330 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2332 struct ab8500_charger *di = _di;
2335 "Die temp above USB charger thermal protection threshold\n");
2336 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2342 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2343 * thermal protection threshold
2344 * @irq: interrupt number
2345 * @_di: pointer to the ab8500_charger structure
2347 * Returns IRQ status(IRQ_HANDLED)
2349 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2351 struct ab8500_charger *di = _di;
2354 "Die temp ok for USB charger thermal protection threshold\n");
2355 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2361 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2362 * @irq: interrupt number
2363 * @_di: pointer to the ab8500_charger structure
2365 * Returns IRQ status(IRQ_HANDLED)
2367 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2369 struct ab8500_charger *di = _di;
2371 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2372 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2378 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2379 * @irq: interrupt number
2380 * @_di: pointer to the ab8500_charger structure
2382 * Returns IRQ status(IRQ_HANDLED)
2384 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2386 struct ab8500_charger *di = _di;
2388 dev_dbg(di->dev, "Charger watchdog expired\n");
2391 * The charger that was online when the watchdog expired
2392 * needs to be restarted for charging to start again
2394 if (di->ac.charger_online) {
2395 di->ac.wd_expired = true;
2396 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2398 if (di->usb.charger_online) {
2399 di->usb.wd_expired = true;
2400 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2407 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2408 * @irq: interrupt number
2409 * @_di: pointer to the ab8500_charger structure
2411 * Returns IRQ status(IRQ_HANDLED)
2413 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2415 struct ab8500_charger *di = _di;
2417 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2418 di->flags.vbus_ovv = true;
2419 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2421 /* Schedule a new HW failure check */
2422 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2428 * ab8500_charger_ac_get_property() - get the ac/mains properties
2429 * @psy: pointer to the power_supply structure
2430 * @psp: pointer to the power_supply_property structure
2431 * @val: pointer to the power_supply_propval union
2433 * This function gets called when an application tries to get the ac/mains
2434 * properties by reading the sysfs files.
2435 * AC/Mains properties are online, present and voltage.
2436 * online: ac/mains charging is in progress or not
2437 * present: presence of the ac/mains
2438 * voltage: AC/Mains voltage
2439 * Returns error code in case of failure else 0(on success)
2441 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2442 enum power_supply_property psp,
2443 union power_supply_propval *val)
2445 struct ab8500_charger *di;
2448 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2451 case POWER_SUPPLY_PROP_HEALTH:
2452 if (di->flags.mainextchnotok)
2453 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2454 else if (di->ac.wd_expired || di->usb.wd_expired)
2455 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2456 else if (di->flags.main_thermal_prot)
2457 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2459 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2461 case POWER_SUPPLY_PROP_ONLINE:
2462 val->intval = di->ac.charger_online;
2464 case POWER_SUPPLY_PROP_PRESENT:
2465 val->intval = di->ac.charger_connected;
2467 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2468 ret = ab8500_charger_get_ac_voltage(di);
2470 di->ac.charger_voltage = ret;
2471 /* On error, use previous value */
2472 val->intval = di->ac.charger_voltage * 1000;
2474 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2476 * This property is used to indicate when CV mode is entered
2477 * for the AC charger
2479 di->ac.cv_active = ab8500_charger_ac_cv(di);
2480 val->intval = di->ac.cv_active;
2482 case POWER_SUPPLY_PROP_CURRENT_NOW:
2483 ret = ab8500_charger_get_ac_current(di);
2485 di->ac.charger_current = ret;
2486 val->intval = di->ac.charger_current * 1000;
2495 * ab8500_charger_usb_get_property() - get the usb properties
2496 * @psy: pointer to the power_supply structure
2497 * @psp: pointer to the power_supply_property structure
2498 * @val: pointer to the power_supply_propval union
2500 * This function gets called when an application tries to get the usb
2501 * properties by reading the sysfs files.
2502 * USB properties are online, present and voltage.
2503 * online: usb charging is in progress or not
2504 * present: presence of the usb
2505 * voltage: vbus voltage
2506 * Returns error code in case of failure else 0(on success)
2508 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2509 enum power_supply_property psp,
2510 union power_supply_propval *val)
2512 struct ab8500_charger *di;
2515 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2518 case POWER_SUPPLY_PROP_HEALTH:
2519 if (di->flags.usbchargernotok)
2520 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2521 else if (di->ac.wd_expired || di->usb.wd_expired)
2522 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2523 else if (di->flags.usb_thermal_prot)
2524 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2525 else if (di->flags.vbus_ovv)
2526 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2528 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2530 case POWER_SUPPLY_PROP_ONLINE:
2531 val->intval = di->usb.charger_online;
2533 case POWER_SUPPLY_PROP_PRESENT:
2534 val->intval = di->usb.charger_connected;
2536 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2537 ret = ab8500_charger_get_vbus_voltage(di);
2539 di->usb.charger_voltage = ret;
2540 val->intval = di->usb.charger_voltage * 1000;
2542 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2544 * This property is used to indicate when CV mode is entered
2545 * for the USB charger
2547 di->usb.cv_active = ab8500_charger_usb_cv(di);
2548 val->intval = di->usb.cv_active;
2550 case POWER_SUPPLY_PROP_CURRENT_NOW:
2551 ret = ab8500_charger_get_usb_current(di);
2553 di->usb.charger_current = ret;
2554 val->intval = di->usb.charger_current * 1000;
2556 case POWER_SUPPLY_PROP_CURRENT_AVG:
2558 * This property is used to indicate when VBUS has collapsed
2559 * due to too high output current from the USB charger
2561 if (di->flags.vbus_collapse)
2573 * ab8500_charger_init_hw_registers() - Set up charger related registers
2574 * @di: pointer to the ab8500_charger structure
2576 * Set up charger OVV, watchdog and maximum voltage registers as well as
2577 * charging of the backup battery
2579 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
2583 /* Setup maximum charger current and voltage for ABB cut2.0 */
2584 if (!is_ab8500_1p1_or_earlier(di->parent)) {
2585 ret = abx500_set_register_interruptible(di->dev,
2587 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
2590 "failed to set CH_VOLT_LVL_MAX_REG\n");
2594 ret = abx500_set_register_interruptible(di->dev,
2596 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
2599 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2604 /* VBUS OVV set to 6.3V and enable automatic current limitiation */
2605 ret = abx500_set_register_interruptible(di->dev,
2607 AB8500_USBCH_CTRL2_REG,
2608 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
2610 dev_err(di->dev, "failed to set VBUS OVV\n");
2614 /* Enable main watchdog in OTP */
2615 ret = abx500_set_register_interruptible(di->dev,
2616 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
2618 dev_err(di->dev, "failed to enable main WD in OTP\n");
2622 /* Enable main watchdog */
2623 ret = abx500_set_register_interruptible(di->dev,
2624 AB8500_SYS_CTRL2_BLOCK,
2625 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
2627 dev_err(di->dev, "faile to enable main watchdog\n");
2632 * Due to internal synchronisation, Enable and Kick watchdog bits
2633 * cannot be enabled in a single write.
2634 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
2635 * between writing Enable then Kick bits.
2639 /* Kick main watchdog */
2640 ret = abx500_set_register_interruptible(di->dev,
2641 AB8500_SYS_CTRL2_BLOCK,
2642 AB8500_MAIN_WDOG_CTRL_REG,
2643 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
2645 dev_err(di->dev, "failed to kick main watchdog\n");
2649 /* Disable main watchdog */
2650 ret = abx500_set_register_interruptible(di->dev,
2651 AB8500_SYS_CTRL2_BLOCK,
2652 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
2654 dev_err(di->dev, "failed to disable main watchdog\n");
2658 /* Set watchdog timeout */
2659 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2660 AB8500_CH_WD_TIMER_REG, WD_TIMER);
2662 dev_err(di->dev, "failed to set charger watchdog timeout\n");
2666 /* Backup battery voltage and current */
2667 ret = abx500_set_register_interruptible(di->dev,
2669 AB8500_RTC_BACKUP_CHG_REG,
2670 di->bm->bkup_bat_v |
2671 di->bm->bkup_bat_i);
2673 dev_err(di->dev, "failed to setup backup battery charging\n");
2677 /* Enable backup battery charging */
2678 abx500_mask_and_set_register_interruptible(di->dev,
2679 AB8500_RTC, AB8500_RTC_CTRL_REG,
2680 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
2682 dev_err(di->dev, "%s mask and set failed\n", __func__);
2689 * ab8500 charger driver interrupts and their respective isr
2691 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
2692 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
2693 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
2694 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
2695 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
2696 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
2697 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
2698 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
2699 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
2700 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
2701 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
2702 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
2703 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
2704 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
2707 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
2708 unsigned long event, void *power)
2710 struct ab8500_charger *di =
2711 container_of(nb, struct ab8500_charger, nb);
2712 enum ab8500_usb_state bm_usb_state;
2713 unsigned mA = *((unsigned *)power);
2715 if (event != USB_EVENT_VBUS) {
2716 dev_dbg(di->dev, "not a standard host, returning\n");
2720 /* TODO: State is fabricate here. See if charger really needs USB
2721 * state or if mA is enough
2723 if ((di->usb_state.usb_current == 2) && (mA > 2))
2724 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
2726 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
2728 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
2729 else if (mA >= 8) /* 8, 100, 500 */
2730 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
2731 else /* Should never occur */
2732 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
2734 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
2735 __func__, bm_usb_state, mA);
2737 spin_lock(&di->usb_state.usb_lock);
2738 di->usb_state.usb_changed = true;
2739 spin_unlock(&di->usb_state.usb_lock);
2741 di->usb_state.state = bm_usb_state;
2742 di->usb_state.usb_current = mA;
2744 queue_work(di->charger_wq, &di->usb_state_changed_work);
2749 #if defined(CONFIG_PM)
2750 static int ab8500_charger_resume(struct platform_device *pdev)
2753 struct ab8500_charger *di = platform_get_drvdata(pdev);
2756 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2757 * logic. That means we have to continously kick the charger
2758 * watchdog even when no charger is connected. This is only
2759 * valid once the AC charger has been enabled. This is
2760 * a bug that is not handled by the algorithm and the
2761 * watchdog have to be kicked by the charger driver
2762 * when the AC charger is disabled
2764 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
2765 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2766 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2768 dev_err(di->dev, "Failed to kick WD!\n");
2770 /* If not already pending start a new timer */
2771 if (!delayed_work_pending(
2772 &di->kick_wd_work)) {
2773 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
2774 round_jiffies(WD_KICK_INTERVAL));
2778 /* If we still have a HW failure, schedule a new check */
2779 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2780 queue_delayed_work(di->charger_wq,
2781 &di->check_hw_failure_work, 0);
2787 static int ab8500_charger_suspend(struct platform_device *pdev,
2790 struct ab8500_charger *di = platform_get_drvdata(pdev);
2792 /* Cancel any pending HW failure check */
2793 if (delayed_work_pending(&di->check_hw_failure_work))
2794 cancel_delayed_work(&di->check_hw_failure_work);
2799 #define ab8500_charger_suspend NULL
2800 #define ab8500_charger_resume NULL
2803 static int ab8500_charger_remove(struct platform_device *pdev)
2805 struct ab8500_charger *di = platform_get_drvdata(pdev);
2808 /* Disable AC charging */
2809 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
2811 /* Disable USB charging */
2812 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
2814 /* Disable interrupts */
2815 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2816 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2820 /* Backup battery voltage and current disable */
2821 ret = abx500_mask_and_set_register_interruptible(di->dev,
2822 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
2824 dev_err(di->dev, "%s mask and set failed\n", __func__);
2826 usb_unregister_notifier(di->usb_phy, &di->nb);
2827 usb_put_phy(di->usb_phy);
2829 /* Delete the work queue */
2830 destroy_workqueue(di->charger_wq);
2832 flush_scheduled_work();
2833 power_supply_unregister(&di->usb_chg.psy);
2834 power_supply_unregister(&di->ac_chg.psy);
2835 platform_set_drvdata(pdev, NULL);
2840 static char *supply_interface[] = {
2846 static int ab8500_charger_probe(struct platform_device *pdev)
2848 struct device_node *np = pdev->dev.of_node;
2849 struct abx500_bm_data *plat = pdev->dev.platform_data;
2850 struct ab8500_charger *di;
2851 int irq, i, charger_status, ret = 0, ch_stat;
2853 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
2855 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
2860 dev_err(&pdev->dev, "no battery management data supplied\n");
2866 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
2868 dev_err(&pdev->dev, "failed to get battery information\n");
2871 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
2873 di->autopower_cfg = false;
2875 /* get parent data */
2876 di->dev = &pdev->dev;
2877 di->parent = dev_get_drvdata(pdev->dev.parent);
2878 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2880 /* initialize lock */
2881 spin_lock_init(&di->usb_state.usb_lock);
2883 di->autopower = false;
2886 /* power_supply base class */
2887 di->ac_chg.psy.name = "ab8500_ac";
2888 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
2889 di->ac_chg.psy.properties = ab8500_charger_ac_props;
2890 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
2891 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
2892 di->ac_chg.psy.supplied_to = supply_interface;
2893 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
2894 /* ux500_charger sub-class */
2895 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
2896 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2897 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2898 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
2899 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2900 di->ac_chg.max_out_curr = ab8500_charger_current_map[
2901 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2904 /* power_supply base class */
2905 di->usb_chg.psy.name = "ab8500_usb";
2906 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
2907 di->usb_chg.psy.properties = ab8500_charger_usb_props;
2908 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
2909 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
2910 di->usb_chg.psy.supplied_to = supply_interface;
2911 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
2912 /* ux500_charger sub-class */
2913 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
2914 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2915 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2916 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
2917 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2918 di->usb_chg.max_out_curr = ab8500_charger_current_map[
2919 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2922 /* Create a work queue for the charger */
2924 create_singlethread_workqueue("ab8500_charger_wq");
2925 if (di->charger_wq == NULL) {
2926 dev_err(di->dev, "failed to create work queue\n");
2930 mutex_init(&di->charger_attached_mutex);
2932 /* Init work for HW failure check */
2933 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
2934 ab8500_charger_check_hw_failure_work);
2935 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
2936 ab8500_charger_check_usbchargernotok_work);
2938 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
2939 ab8500_charger_ac_attached_work);
2940 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
2941 ab8500_charger_usb_attached_work);
2944 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2945 * logic. That means we have to continously kick the charger
2946 * watchdog even when no charger is connected. This is only
2947 * valid once the AC charger has been enabled. This is
2948 * a bug that is not handled by the algorithm and the
2949 * watchdog have to be kicked by the charger driver
2950 * when the AC charger is disabled
2952 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
2953 ab8500_charger_kick_watchdog_work);
2955 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
2956 ab8500_charger_check_vbat_work);
2958 INIT_DELAYED_WORK(&di->attach_work,
2959 ab8500_charger_usb_link_attach_work);
2961 /* Init work for charger detection */
2962 INIT_WORK(&di->usb_link_status_work,
2963 ab8500_charger_usb_link_status_work);
2964 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
2965 INIT_WORK(&di->detect_usb_type_work,
2966 ab8500_charger_detect_usb_type_work);
2968 INIT_WORK(&di->usb_state_changed_work,
2969 ab8500_charger_usb_state_changed_work);
2971 /* Init work for checking HW status */
2972 INIT_WORK(&di->check_main_thermal_prot_work,
2973 ab8500_charger_check_main_thermal_prot_work);
2974 INIT_WORK(&di->check_usb_thermal_prot_work,
2975 ab8500_charger_check_usb_thermal_prot_work);
2978 * VDD ADC supply needs to be enabled from this driver when there
2979 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
2980 * interrupts during charging
2982 di->regu = devm_regulator_get(di->dev, "vddadc");
2983 if (IS_ERR(di->regu)) {
2984 ret = PTR_ERR(di->regu);
2985 dev_err(di->dev, "failed to get vddadc regulator\n");
2986 goto free_charger_wq;
2990 /* Initialize OVV, and other registers */
2991 ret = ab8500_charger_init_hw_registers(di);
2993 dev_err(di->dev, "failed to initialize ABB registers\n");
2994 goto free_charger_wq;
2997 /* Register AC charger class */
2998 ret = power_supply_register(di->dev, &di->ac_chg.psy);
3000 dev_err(di->dev, "failed to register AC charger\n");
3001 goto free_charger_wq;
3004 /* Register USB charger class */
3005 ret = power_supply_register(di->dev, &di->usb_chg.psy);
3007 dev_err(di->dev, "failed to register USB charger\n");
3011 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3012 if (IS_ERR_OR_NULL(di->usb_phy)) {
3013 dev_err(di->dev, "failed to get usb transceiver\n");
3017 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3018 ret = usb_register_notifier(di->usb_phy, &di->nb);
3020 dev_err(di->dev, "failed to register usb notifier\n");
3024 /* Identify the connected charger types during startup */
3025 charger_status = ab8500_charger_detect_chargers(di);
3026 if (charger_status & AC_PW_CONN) {
3027 di->ac.charger_connected = 1;
3029 ab8500_power_supply_changed(di, &di->ac_chg.psy);
3030 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
3033 if (charger_status & USB_PW_CONN) {
3034 di->vbus_detected = true;
3035 di->vbus_detected_start = true;
3036 queue_work(di->charger_wq,
3037 &di->detect_usb_type_work);
3040 /* Register interrupts */
3041 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3042 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3043 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3044 IRQF_SHARED | IRQF_NO_SUSPEND,
3045 ab8500_charger_irq[i].name, di);
3048 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3049 , ab8500_charger_irq[i].name, irq, ret);
3052 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3053 ab8500_charger_irq[i].name, irq, ret);
3056 platform_set_drvdata(pdev, di);
3058 mutex_lock(&di->charger_attached_mutex);
3060 ch_stat = ab8500_charger_detect_chargers(di);
3062 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3063 queue_delayed_work(di->charger_wq,
3064 &di->ac_charger_attached_work,
3067 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3068 queue_delayed_work(di->charger_wq,
3069 &di->usb_charger_attached_work,
3073 mutex_unlock(&di->charger_attached_mutex);
3078 usb_unregister_notifier(di->usb_phy, &di->nb);
3080 /* We also have to free all successfully registered irqs */
3081 for (i = i - 1; i >= 0; i--) {
3082 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3086 usb_put_phy(di->usb_phy);
3088 power_supply_unregister(&di->usb_chg.psy);
3090 power_supply_unregister(&di->ac_chg.psy);
3092 destroy_workqueue(di->charger_wq);
3096 static const struct of_device_id ab8500_charger_match[] = {
3097 { .compatible = "stericsson,ab8500-charger", },
3101 static struct platform_driver ab8500_charger_driver = {
3102 .probe = ab8500_charger_probe,
3103 .remove = ab8500_charger_remove,
3104 .suspend = ab8500_charger_suspend,
3105 .resume = ab8500_charger_resume,
3107 .name = "ab8500-charger",
3108 .owner = THIS_MODULE,
3109 .of_match_table = ab8500_charger_match,
3113 static int __init ab8500_charger_init(void)
3115 return platform_driver_register(&ab8500_charger_driver);
3118 static void __exit ab8500_charger_exit(void)
3120 platform_driver_unregister(&ab8500_charger_driver);
3123 subsys_initcall_sync(ab8500_charger_init);
3124 module_exit(ab8500_charger_exit);
3126 MODULE_LICENSE("GPL v2");
3127 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3128 MODULE_ALIAS("platform:ab8500-charger");
3129 MODULE_DESCRIPTION("AB8500 charger management driver");