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 #define CHARGER_STATUS_POLL 10 /* in ms */
93 /* UsbLineStatus register - usb types */
94 enum ab8500_charger_link_status {
95 USB_STAT_NOT_CONFIGURED,
97 USB_STAT_STD_HOST_C_NS,
98 USB_STAT_STD_HOST_C_S,
100 USB_STAT_HOST_CHG_HS,
101 USB_STAT_HOST_CHG_HS_CHIRP,
102 USB_STAT_DEDICATED_CHG,
105 USB_STAT_ACA_RID_C_NM,
106 USB_STAT_ACA_RID_C_HS,
107 USB_STAT_ACA_RID_C_HS_CHIRP,
110 USB_STAT_NOT_VALID_LINK,
113 enum ab8500_usb_state {
114 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
115 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
116 AB8500_BM_USB_STATE_CONFIGURED,
117 AB8500_BM_USB_STATE_SUSPEND,
118 AB8500_BM_USB_STATE_RESUME,
119 AB8500_BM_USB_STATE_MAX,
122 /* VBUS input current limits supported in AB8500 in mA */
123 #define USB_CH_IP_CUR_LVL_0P05 50
124 #define USB_CH_IP_CUR_LVL_0P09 98
125 #define USB_CH_IP_CUR_LVL_0P19 193
126 #define USB_CH_IP_CUR_LVL_0P29 290
127 #define USB_CH_IP_CUR_LVL_0P38 380
128 #define USB_CH_IP_CUR_LVL_0P45 450
129 #define USB_CH_IP_CUR_LVL_0P5 500
130 #define USB_CH_IP_CUR_LVL_0P6 600
131 #define USB_CH_IP_CUR_LVL_0P7 700
132 #define USB_CH_IP_CUR_LVL_0P8 800
133 #define USB_CH_IP_CUR_LVL_0P9 900
134 #define USB_CH_IP_CUR_LVL_1P0 1000
135 #define USB_CH_IP_CUR_LVL_1P1 1100
136 #define USB_CH_IP_CUR_LVL_1P3 1300
137 #define USB_CH_IP_CUR_LVL_1P4 1400
138 #define USB_CH_IP_CUR_LVL_1P5 1500
140 #define VBAT_TRESH_IP_CUR_RED 3800
142 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
143 struct ab8500_charger, usb_chg)
144 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
145 struct ab8500_charger, ac_chg)
148 * struct ab8500_charger_interrupts - ab8500 interupts
149 * @name: name of the interrupt
150 * @isr function pointer to the isr
152 struct ab8500_charger_interrupts {
154 irqreturn_t (*isr)(int irq, void *data);
157 struct ab8500_charger_info {
158 int charger_connected;
165 struct ab8500_charger_event_flags {
167 bool main_thermal_prot;
168 bool usb_thermal_prot;
170 bool usbchargernotok;
175 struct ab8500_charger_usb_state {
178 enum ab8500_usb_state state;
183 * struct ab8500_charger - ab8500 Charger device information
184 * @dev: Pointer to the structure device
185 * @max_usb_in_curr: Max USB charger input current
186 * @vbus_detected: VBUS detected
187 * @vbus_detected_start:
188 * VBUS detected during startup
189 * @ac_conn: This will be true when the AC charger has been plugged
190 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
192 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
194 * @vbat Battery voltage
195 * @old_vbat Previously measured battery voltage
196 * @autopower Indicate if we should have automatic pwron after pwrloss
197 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
198 * @parent: Pointer to the struct ab8500
199 * @gpadc: Pointer to the struct gpadc
200 * @bm: Platform specific battery management information
201 * @flags: Structure for information about events triggered
202 * @usb_state: Structure for usb stack information
203 * @ac_chg: AC charger power supply
204 * @usb_chg: USB charger power supply
205 * @ac: Structure that holds the AC charger properties
206 * @usb: Structure that holds the USB charger properties
207 * @regu: Pointer to the struct regulator
208 * @charger_wq: Work queue for the IRQs and checking HW state
209 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
210 * @check_hw_failure_work: Work for checking HW state
211 * @check_usbchgnotok_work: Work for checking USB charger not ok status
212 * @kick_wd_work: Work for kicking the charger watchdog in case
213 * of ABB rev 1.* due to the watchog logic bug
214 * @ac_charger_attached_work: Work for checking if AC charger is still
216 * @usb_charger_attached_work: Work for checking if USB charger is still
218 * @ac_work: Work for checking AC charger connection
219 * @detect_usb_type_work: Work for detecting the USB type connected
220 * @usb_link_status_work: Work for checking the new USB link status
221 * @usb_state_changed_work: Work for checking USB state
222 * @check_main_thermal_prot_work:
223 * Work for checking Main thermal status
224 * @check_usb_thermal_prot_work:
225 * Work for checking USB thermal status
226 * @charger_attached_mutex: For controlling the wakelock
228 struct ab8500_charger {
232 bool vbus_detected_start;
240 struct ab8500 *parent;
241 struct ab8500_gpadc *gpadc;
242 struct abx500_bm_data *bm;
243 struct ab8500_charger_event_flags flags;
244 struct ab8500_charger_usb_state usb_state;
245 struct ux500_charger ac_chg;
246 struct ux500_charger usb_chg;
247 struct ab8500_charger_info ac;
248 struct ab8500_charger_info usb;
249 struct regulator *regu;
250 struct workqueue_struct *charger_wq;
251 struct delayed_work check_vbat_work;
252 struct delayed_work check_hw_failure_work;
253 struct delayed_work check_usbchgnotok_work;
254 struct delayed_work kick_wd_work;
255 struct delayed_work ac_charger_attached_work;
256 struct delayed_work usb_charger_attached_work;
257 struct work_struct ac_work;
258 struct work_struct detect_usb_type_work;
259 struct work_struct usb_link_status_work;
260 struct work_struct usb_state_changed_work;
261 struct work_struct check_main_thermal_prot_work;
262 struct work_struct check_usb_thermal_prot_work;
263 struct usb_phy *usb_phy;
264 struct notifier_block nb;
265 struct mutex charger_attached_mutex;
269 static enum power_supply_property ab8500_charger_ac_props[] = {
270 POWER_SUPPLY_PROP_HEALTH,
271 POWER_SUPPLY_PROP_PRESENT,
272 POWER_SUPPLY_PROP_ONLINE,
273 POWER_SUPPLY_PROP_VOLTAGE_NOW,
274 POWER_SUPPLY_PROP_VOLTAGE_AVG,
275 POWER_SUPPLY_PROP_CURRENT_NOW,
279 static enum power_supply_property ab8500_charger_usb_props[] = {
280 POWER_SUPPLY_PROP_HEALTH,
281 POWER_SUPPLY_PROP_CURRENT_AVG,
282 POWER_SUPPLY_PROP_PRESENT,
283 POWER_SUPPLY_PROP_ONLINE,
284 POWER_SUPPLY_PROP_VOLTAGE_NOW,
285 POWER_SUPPLY_PROP_VOLTAGE_AVG,
286 POWER_SUPPLY_PROP_CURRENT_NOW,
290 * Function for enabling and disabling sw fallback mode
291 * should always be disabled when no charger is connected.
293 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
299 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
301 /* read the register containing fallback bit */
302 ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, ®);
304 dev_err(di->dev, "%d write failed\n", __LINE__);
308 /* enable the OPT emulation registers */
309 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
311 dev_err(di->dev, "%d write failed\n", __LINE__);
320 /* write back the changed fallback bit value to register */
321 ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg);
323 dev_err(di->dev, "%d write failed\n", __LINE__);
327 /* disable the set OTP registers again */
328 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
330 dev_err(di->dev, "%d write failed\n", __LINE__);
336 * ab8500_power_supply_changed - a wrapper with local extentions for
337 * power_supply_changed
338 * @di: pointer to the ab8500_charger structure
339 * @psy: pointer to power_supply_that have changed.
342 static void ab8500_power_supply_changed(struct ab8500_charger *di,
343 struct power_supply *psy)
345 if (di->autopower_cfg) {
346 if (!di->usb.charger_connected &&
347 !di->ac.charger_connected &&
349 di->autopower = false;
350 ab8500_enable_disable_sw_fallback(di, false);
351 } else if (!di->autopower &&
352 (di->ac.charger_connected ||
353 di->usb.charger_connected)) {
354 di->autopower = true;
355 ab8500_enable_disable_sw_fallback(di, true);
358 power_supply_changed(psy);
361 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
364 if (connected != di->usb.charger_connected) {
365 dev_dbg(di->dev, "USB connected:%i\n", connected);
366 di->usb.charger_connected = connected;
367 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
370 mutex_lock(&di->charger_attached_mutex);
371 mutex_unlock(&di->charger_attached_mutex);
373 queue_delayed_work(di->charger_wq,
374 &di->usb_charger_attached_work,
377 cancel_delayed_work_sync(&di->usb_charger_attached_work);
378 mutex_lock(&di->charger_attached_mutex);
379 mutex_unlock(&di->charger_attached_mutex);
385 * ab8500_charger_get_ac_voltage() - get ac charger voltage
386 * @di: pointer to the ab8500_charger structure
388 * Returns ac charger voltage (on success)
390 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
394 /* Only measure voltage if the charger is connected */
395 if (di->ac.charger_connected) {
396 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
398 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
406 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
407 * @di: pointer to the ab8500_charger structure
409 * Returns ac charger CV mode (on success) else error code
411 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
416 /* Only check CV mode if the charger is online */
417 if (di->ac.charger_online) {
418 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
419 AB8500_CH_STATUS1_REG, &val);
421 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
425 if (val & MAIN_CH_CV_ON)
435 * ab8500_charger_get_vbus_voltage() - get vbus voltage
436 * @di: pointer to the ab8500_charger structure
438 * This function returns the vbus voltage.
439 * Returns vbus voltage (on success)
441 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
445 /* Only measure voltage if the charger is connected */
446 if (di->usb.charger_connected) {
447 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
449 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
457 * ab8500_charger_get_usb_current() - get usb charger current
458 * @di: pointer to the ab8500_charger structure
460 * This function returns the usb charger current.
461 * Returns usb current (on success) and error code on failure
463 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
467 /* Only measure current if the charger is online */
468 if (di->usb.charger_online) {
469 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
471 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
479 * ab8500_charger_get_ac_current() - get ac charger current
480 * @di: pointer to the ab8500_charger structure
482 * This function returns the ac charger current.
483 * Returns ac current (on success) and error code on failure.
485 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
489 /* Only measure current if the charger is online */
490 if (di->ac.charger_online) {
491 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
493 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
501 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
502 * @di: pointer to the ab8500_charger structure
504 * Returns ac charger CV mode (on success) else error code
506 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
511 /* Only check CV mode if the charger is online */
512 if (di->usb.charger_online) {
513 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
514 AB8500_CH_USBCH_STAT1_REG, &val);
516 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
520 if (val & USB_CH_CV_ON)
532 * ab8500_charger_detect_chargers() - Detect the connected chargers
533 * @di: pointer to the ab8500_charger structure
535 * Returns the type of charger connected.
536 * For USB it will not mean we can actually charge from it
537 * but that there is a USB cable connected that we have to
538 * identify. This is used during startup when we don't get
539 * interrupts of the charger detection
541 * Returns an integer value, that means,
542 * NO_PW_CONN no power supply is connected
543 * AC_PW_CONN if the AC power supply is connected
544 * USB_PW_CONN if the USB power supply is connected
545 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
547 static int ab8500_charger_detect_chargers(struct ab8500_charger *di)
549 int result = NO_PW_CONN;
553 /* Check for AC charger */
554 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
555 AB8500_CH_STATUS1_REG, &val);
557 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
561 if (val & MAIN_CH_DET)
564 /* Check for USB charger */
565 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
566 AB8500_CH_USBCH_STAT1_REG, &val);
568 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
572 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
573 result |= USB_PW_CONN;
579 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
580 * @di: pointer to the ab8500_charger structure
581 * @link_status: the identified USB type
583 * Get the maximum current that is allowed to be drawn from the host
584 * based on the USB type.
585 * Returns error code in case of failure else 0 on success
587 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
588 enum ab8500_charger_link_status link_status)
592 switch (link_status) {
593 case USB_STAT_STD_HOST_NC:
594 case USB_STAT_STD_HOST_C_NS:
595 case USB_STAT_STD_HOST_C_S:
596 dev_dbg(di->dev, "USB Type - Standard host is "
597 "detected through USB driver\n");
598 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
600 case USB_STAT_HOST_CHG_HS_CHIRP:
601 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
603 case USB_STAT_HOST_CHG_HS:
604 case USB_STAT_ACA_RID_C_HS:
605 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
607 case USB_STAT_ACA_RID_A:
609 * Dedicated charger level minus maximum current accessory
610 * can consume (300mA). Closest level is 1100mA
612 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1;
614 case USB_STAT_ACA_RID_B:
616 * Dedicated charger level minus 120mA (20mA for ACA and
617 * 100mA for potential accessory). Closest level is 1300mA
619 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
621 case USB_STAT_DEDICATED_CHG:
622 case USB_STAT_HOST_CHG_NM:
623 case USB_STAT_ACA_RID_C_HS_CHIRP:
624 case USB_STAT_ACA_RID_C_NM:
625 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
627 case USB_STAT_RESERVED:
629 * This state is used to indicate that VBUS has dropped below
630 * the detection level 4 times in a row. This is due to the
631 * charger output current is set to high making the charger
632 * voltage collapse. This have to be propagated through to
633 * chargalg. This is done using the property
634 * POWER_SUPPLY_PROP_CURRENT_AVG = 1
636 di->flags.vbus_collapse = true;
637 dev_dbg(di->dev, "USB Type - USB_STAT_RESERVED "
638 "VBUS has collapsed\n");
641 case USB_STAT_HM_IDGND:
642 case USB_STAT_NOT_CONFIGURED:
643 case USB_STAT_NOT_VALID_LINK:
644 dev_err(di->dev, "USB Type - Charging not allowed\n");
645 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
649 dev_err(di->dev, "USB Type - Unknown\n");
650 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
655 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
656 link_status, di->max_usb_in_curr);
662 * ab8500_charger_read_usb_type() - read the type of usb connected
663 * @di: pointer to the ab8500_charger structure
665 * Detect the type of the plugged USB
666 * Returns error code in case of failure else 0 on success
668 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
673 ret = abx500_get_register_interruptible(di->dev,
674 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
676 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
679 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
680 AB8500_USB_LINE_STAT_REG, &val);
682 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
686 /* get the USB type */
687 val = (val & AB8500_USB_LINK_STATUS) >> 3;
688 ret = ab8500_charger_max_usb_curr(di,
689 (enum ab8500_charger_link_status) val);
695 * ab8500_charger_detect_usb_type() - get the type of usb connected
696 * @di: pointer to the ab8500_charger structure
698 * Detect the type of the plugged USB
699 * Returns error code in case of failure else 0 on success
701 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
707 * On getting the VBUS rising edge detect interrupt there
708 * is a 250ms delay after which the register UsbLineStatus
709 * is filled with valid data.
711 for (i = 0; i < 10; i++) {
713 ret = abx500_get_register_interruptible(di->dev,
714 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
717 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
720 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
721 AB8500_USB_LINE_STAT_REG, &val);
723 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
727 * Until the IT source register is read the UsbLineStatus
728 * register is not updated, hence doing the same
732 /* get the USB type */
733 val = (val & AB8500_USB_LINK_STATUS) >> 3;
737 ret = ab8500_charger_max_usb_curr(di,
738 (enum ab8500_charger_link_status) val);
744 * This array maps the raw hex value to charger voltage used by the AB8500
745 * Values taken from the UM0836
747 static int ab8500_charger_voltage_map[] = {
829 * This array maps the raw hex value to charger current used by the AB8500
830 * Values taken from the UM0836
832 static int ab8500_charger_current_map[] = {
851 * This array maps the raw hex value to VBUS input current used by the AB8500
852 * Values taken from the UM0836
854 static int ab8500_charger_vbus_in_curr_map[] = {
855 USB_CH_IP_CUR_LVL_0P05,
856 USB_CH_IP_CUR_LVL_0P09,
857 USB_CH_IP_CUR_LVL_0P19,
858 USB_CH_IP_CUR_LVL_0P29,
859 USB_CH_IP_CUR_LVL_0P38,
860 USB_CH_IP_CUR_LVL_0P45,
861 USB_CH_IP_CUR_LVL_0P5,
862 USB_CH_IP_CUR_LVL_0P6,
863 USB_CH_IP_CUR_LVL_0P7,
864 USB_CH_IP_CUR_LVL_0P8,
865 USB_CH_IP_CUR_LVL_0P9,
866 USB_CH_IP_CUR_LVL_1P0,
867 USB_CH_IP_CUR_LVL_1P1,
868 USB_CH_IP_CUR_LVL_1P3,
869 USB_CH_IP_CUR_LVL_1P4,
870 USB_CH_IP_CUR_LVL_1P5,
873 static int ab8500_voltage_to_regval(int voltage)
877 /* Special case for voltage below 3.5V */
878 if (voltage < ab8500_charger_voltage_map[0])
881 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
882 if (voltage < ab8500_charger_voltage_map[i])
886 /* If not last element, return error */
887 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
888 if (voltage == ab8500_charger_voltage_map[i])
894 static int ab8500_current_to_regval(int curr)
898 if (curr < ab8500_charger_current_map[0])
901 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
902 if (curr < ab8500_charger_current_map[i])
906 /* If not last element, return error */
907 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
908 if (curr == ab8500_charger_current_map[i])
914 static int ab8500_vbus_in_curr_to_regval(int curr)
918 if (curr < ab8500_charger_vbus_in_curr_map[0])
921 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
922 if (curr < ab8500_charger_vbus_in_curr_map[i])
926 /* If not last element, return error */
927 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
928 if (curr == ab8500_charger_vbus_in_curr_map[i])
935 * ab8500_charger_get_usb_cur() - get usb current
936 * @di: pointer to the ab8500_charger structre
938 * The usb stack provides the maximum current that can be drawn from
939 * the standard usb host. This will be in mA.
940 * This function converts current in mA to a value that can be written
941 * to the register. Returns -1 if charging is not allowed
943 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
945 switch (di->usb_state.usb_current) {
947 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
950 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
953 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
956 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
959 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
962 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
970 * ab8500_charger_set_current() - set charger current
971 * @di: pointer to the ab8500_charger structure
972 * @ich: charger current, in mA
973 * @reg: select what charger register to set
975 * Set charger current.
976 * There is no state machine in the AB to step up/down the charger
977 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
978 * charging is started. Instead we need to implement
979 * this charger current step-up/down here.
980 * Returns error code in case of failure else 0(on success)
982 static int ab8500_charger_set_current(struct ab8500_charger *di,
986 int curr_index, prev_curr_index, shift_value;
990 case AB8500_MCH_IPT_CURLVL_REG:
991 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
992 curr_index = ab8500_current_to_regval(ich);
994 case AB8500_USBCH_IPT_CRNTLVL_REG:
995 shift_value = VBUS_IN_CURR_LIM_SHIFT;
996 curr_index = ab8500_vbus_in_curr_to_regval(ich);
998 case AB8500_CH_OPT_CRNTLVL_REG:
1000 curr_index = ab8500_current_to_regval(ich);
1003 dev_err(di->dev, "%s current register not valid\n", __func__);
1007 if (curr_index < 0) {
1008 dev_err(di->dev, "requested current limit out-of-range\n");
1012 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1015 dev_err(di->dev, "%s read failed\n", __func__);
1018 prev_curr_index = (reg_value >> shift_value);
1020 /* only update current if it's been changed */
1021 if (prev_curr_index == curr_index)
1024 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1025 __func__, ich, reg);
1027 if (prev_curr_index > curr_index) {
1028 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1029 ret = abx500_set_register_interruptible(di->dev,
1030 AB8500_CHARGER, reg, (u8) i << shift_value);
1032 dev_err(di->dev, "%s write failed\n", __func__);
1035 usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
1038 for (i = prev_curr_index + 1; i <= curr_index; i++) {
1039 ret = abx500_set_register_interruptible(di->dev,
1040 AB8500_CHARGER, reg, (u8) i << shift_value);
1042 dev_err(di->dev, "%s write failed\n", __func__);
1045 usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
1052 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1053 * @di: pointer to the ab8500_charger structure
1054 * @ich_in: charger input current limit
1056 * Sets the current that can be drawn from the USB host
1057 * Returns error code in case of failure else 0(on success)
1059 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1064 /* We should always use to lowest current limit */
1065 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1067 switch (min_value) {
1069 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1070 min_value = USB_CH_IP_CUR_LVL_0P05;
1073 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1074 min_value = USB_CH_IP_CUR_LVL_0P45;
1080 return ab8500_charger_set_current(di, min_value,
1081 AB8500_USBCH_IPT_CRNTLVL_REG);
1085 * ab8500_charger_set_main_in_curr() - set main charger input current
1086 * @di: pointer to the ab8500_charger structure
1087 * @ich_in: input charger current, in mA
1089 * Set main charger input current.
1090 * Returns error code in case of failure else 0(on success)
1092 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1095 return ab8500_charger_set_current(di, ich_in,
1096 AB8500_MCH_IPT_CURLVL_REG);
1100 * ab8500_charger_set_output_curr() - set charger output current
1101 * @di: pointer to the ab8500_charger structure
1102 * @ich_out: output charger current, in mA
1104 * Set charger output current.
1105 * Returns error code in case of failure else 0(on success)
1107 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1110 return ab8500_charger_set_current(di, ich_out,
1111 AB8500_CH_OPT_CRNTLVL_REG);
1115 * ab8500_charger_led_en() - turn on/off chargign led
1116 * @di: pointer to the ab8500_charger structure
1117 * @on: flag to turn on/off the chargign led
1119 * Power ON/OFF charging LED indication
1120 * Returns error code in case of failure else 0(on success)
1122 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1127 /* Power ON charging LED indicator, set LED current to 5mA */
1128 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1129 AB8500_LED_INDICATOR_PWM_CTRL,
1130 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1132 dev_err(di->dev, "Power ON LED failed\n");
1135 /* LED indicator PWM duty cycle 252/256 */
1136 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1137 AB8500_LED_INDICATOR_PWM_DUTY,
1138 LED_INDICATOR_PWM_DUTY_252_256);
1140 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1144 /* Power off charging LED indicator */
1145 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1146 AB8500_LED_INDICATOR_PWM_CTRL,
1147 LED_INDICATOR_PWM_DIS);
1149 dev_err(di->dev, "Power-off LED failed\n");
1158 * ab8500_charger_ac_en() - enable or disable ac charging
1159 * @di: pointer to the ab8500_charger structure
1160 * @enable: enable/disable flag
1161 * @vset: charging voltage
1162 * @iset: charging current
1164 * Enable/Disable AC/Mains charging and turns on/off the charging led
1167 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1168 int enable, int vset, int iset)
1173 int input_curr_index;
1176 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1179 /* Check if AC is connected */
1180 if (!di->ac.charger_connected) {
1181 dev_err(di->dev, "AC charger not connected\n");
1185 /* Enable AC charging */
1186 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1189 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1190 * will be triggered everytime we enable the VDD ADC supply.
1191 * This will turn off charging for a short while.
1192 * It can be avoided by having the supply on when
1193 * there is a charger enabled. Normally the VDD ADC supply
1194 * is enabled everytime a GPADC conversion is triggered. We will
1195 * force it to be enabled from this driver to have
1196 * the GPADC module independant of the AB8500 chargers
1198 if (!di->vddadc_en_ac) {
1199 regulator_enable(di->regu);
1200 di->vddadc_en_ac = true;
1203 /* Check if the requested voltage or current is valid */
1204 volt_index = ab8500_voltage_to_regval(vset);
1205 curr_index = ab8500_current_to_regval(iset);
1206 input_curr_index = ab8500_current_to_regval(
1207 di->bm->chg_params->ac_curr_max);
1208 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1210 "Charger voltage or current too high, "
1211 "charging not started\n");
1215 /* ChVoltLevel: maximum battery charging voltage */
1216 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1217 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1219 dev_err(di->dev, "%s write failed\n", __func__);
1222 /* MainChInputCurr: current that can be drawn from the charger*/
1223 ret = ab8500_charger_set_main_in_curr(di,
1224 di->bm->chg_params->ac_curr_max);
1226 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1230 /* ChOutputCurentLevel: protected output current */
1231 ret = ab8500_charger_set_output_curr(di, iset);
1233 dev_err(di->dev, "%s "
1234 "Failed to set ChOutputCurentLevel\n",
1239 /* Check if VBAT overshoot control should be enabled */
1240 if (!di->bm->enable_overshoot)
1241 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1243 /* Enable Main Charger */
1244 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1245 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1247 dev_err(di->dev, "%s write failed\n", __func__);
1251 /* Power on charging LED indication */
1252 ret = ab8500_charger_led_en(di, true);
1254 dev_err(di->dev, "failed to enable LED\n");
1256 di->ac.charger_online = 1;
1258 /* Disable AC charging */
1259 if (is_ab8500_1p1_or_earlier(di->parent)) {
1261 * For ABB revision 1.0 and 1.1 there is a bug in the
1262 * watchdog logic. That means we have to continously
1263 * kick the charger watchdog even when no charger is
1264 * connected. This is only valid once the AC charger
1265 * has been enabled. This is a bug that is not handled
1266 * by the algorithm and the watchdog have to be kicked
1267 * by the charger driver when the AC charger
1271 queue_delayed_work(di->charger_wq,
1273 round_jiffies(WD_KICK_INTERVAL));
1277 * We can't turn off charging completely
1278 * due to a bug in AB8500 cut1.
1279 * If we do, charging will not start again.
1280 * That is why we set the lowest voltage
1281 * and current possible
1283 ret = abx500_set_register_interruptible(di->dev,
1285 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1288 "%s write failed\n", __func__);
1292 ret = ab8500_charger_set_output_curr(di, 0);
1294 dev_err(di->dev, "%s "
1295 "Failed to set ChOutputCurentLevel\n",
1300 ret = abx500_set_register_interruptible(di->dev,
1302 AB8500_MCH_CTRL1, 0);
1305 "%s write failed\n", __func__);
1310 ret = ab8500_charger_led_en(di, false);
1312 dev_err(di->dev, "failed to disable LED\n");
1314 di->ac.charger_online = 0;
1315 di->ac.wd_expired = false;
1317 /* Disable regulator if enabled */
1318 if (di->vddadc_en_ac) {
1319 regulator_disable(di->regu);
1320 di->vddadc_en_ac = false;
1323 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1325 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1331 * ab8500_charger_usb_en() - enable usb charging
1332 * @di: pointer to the ab8500_charger structure
1333 * @enable: enable/disable flag
1334 * @vset: charging voltage
1335 * @ich_out: charger output current
1337 * Enable/Disable USB charging and turns on/off the charging led respectively.
1338 * Returns error code in case of failure else 0(on success)
1340 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1341 int enable, int vset, int ich_out)
1348 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1351 /* Check if USB is connected */
1352 if (!di->usb.charger_connected) {
1353 dev_err(di->dev, "USB charger not connected\n");
1358 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1359 * will be triggered everytime we enable the VDD ADC supply.
1360 * This will turn off charging for a short while.
1361 * It can be avoided by having the supply on when
1362 * there is a charger enabled. Normally the VDD ADC supply
1363 * is enabled everytime a GPADC conversion is triggered. We will
1364 * force it to be enabled from this driver to have
1365 * the GPADC module independant of the AB8500 chargers
1367 if (!di->vddadc_en_usb) {
1368 regulator_enable(di->regu);
1369 di->vddadc_en_usb = true;
1372 /* Enable USB charging */
1373 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1375 /* Check if the requested voltage or current is valid */
1376 volt_index = ab8500_voltage_to_regval(vset);
1377 curr_index = ab8500_current_to_regval(ich_out);
1378 if (volt_index < 0 || curr_index < 0) {
1380 "Charger voltage or current too high, "
1381 "charging not started\n");
1385 /* ChVoltLevel: max voltage upto which battery can be charged */
1386 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1387 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1389 dev_err(di->dev, "%s write failed\n", __func__);
1392 /* USBChInputCurr: current that can be drawn from the usb */
1393 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1395 dev_err(di->dev, "setting USBChInputCurr failed\n");
1398 /* ChOutputCurentLevel: protected output current */
1399 ret = ab8500_charger_set_output_curr(di, ich_out);
1401 dev_err(di->dev, "%s "
1402 "Failed to set ChOutputCurentLevel\n",
1406 /* Check if VBAT overshoot control should be enabled */
1407 if (!di->bm->enable_overshoot)
1408 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1410 /* Enable USB Charger */
1411 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1412 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1414 dev_err(di->dev, "%s write failed\n", __func__);
1418 /* If success power on charging LED indication */
1419 ret = ab8500_charger_led_en(di, true);
1421 dev_err(di->dev, "failed to enable LED\n");
1423 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1425 di->usb.charger_online = 1;
1427 /* Disable USB charging */
1428 ret = abx500_set_register_interruptible(di->dev,
1430 AB8500_USBCH_CTRL1_REG, 0);
1433 "%s write failed\n", __func__);
1437 ret = ab8500_charger_led_en(di, false);
1439 dev_err(di->dev, "failed to disable LED\n");
1441 di->usb.charger_online = 0;
1442 di->usb.wd_expired = false;
1444 /* Disable regulator if enabled */
1445 if (di->vddadc_en_usb) {
1446 regulator_disable(di->regu);
1447 di->vddadc_en_usb = false;
1450 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1452 /* Cancel any pending Vbat check work */
1453 if (delayed_work_pending(&di->check_vbat_work))
1454 cancel_delayed_work(&di->check_vbat_work);
1457 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1463 * ab8500_charger_watchdog_kick() - kick charger watchdog
1464 * @di: pointer to the ab8500_charger structure
1466 * Kick charger watchdog
1467 * Returns error code in case of failure else 0(on success)
1469 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1472 struct ab8500_charger *di;
1474 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1475 di = to_ab8500_charger_ac_device_info(charger);
1476 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1477 di = to_ab8500_charger_usb_device_info(charger);
1481 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1482 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1484 dev_err(di->dev, "Failed to kick WD!\n");
1490 * ab8500_charger_update_charger_current() - update charger current
1491 * @di: pointer to the ab8500_charger structure
1493 * Update the charger output current for the specified charger
1494 * Returns error code in case of failure else 0(on success)
1496 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1500 struct ab8500_charger *di;
1502 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1503 di = to_ab8500_charger_ac_device_info(charger);
1504 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1505 di = to_ab8500_charger_usb_device_info(charger);
1509 ret = ab8500_charger_set_output_curr(di, ich_out);
1511 dev_err(di->dev, "%s "
1512 "Failed to set ChOutputCurentLevel\n",
1517 /* Reset the main and usb drop input current measurement counter */
1518 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1519 AB8500_CHARGER_CTRL,
1522 dev_err(di->dev, "%s write failed\n", __func__);
1529 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1531 struct power_supply *psy;
1532 struct power_supply *ext;
1533 struct ab8500_charger *di;
1534 union power_supply_propval ret;
1536 bool psy_found = false;
1537 struct ux500_charger *usb_chg;
1539 usb_chg = (struct ux500_charger *)data;
1540 psy = &usb_chg->psy;
1542 di = to_ab8500_charger_usb_device_info(usb_chg);
1544 ext = dev_get_drvdata(dev);
1546 /* For all psy where the driver name appears in any supplied_to */
1547 for (i = 0; i < ext->num_supplicants; i++) {
1548 if (!strcmp(ext->supplied_to[i], psy->name))
1555 /* Go through all properties for the psy */
1556 for (j = 0; j < ext->num_properties; j++) {
1557 enum power_supply_property prop;
1558 prop = ext->properties[j];
1560 if (ext->get_property(ext, prop, &ret))
1564 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1565 switch (ext->type) {
1566 case POWER_SUPPLY_TYPE_BATTERY:
1567 di->vbat = ret.intval / 1000;
1581 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1582 * @work pointer to the work_struct structure
1584 * Due to a asic bug it is necessary to lower the input current to the vbus
1585 * charger when charging with at some specific levels. This issue is only valid
1586 * for below a certain battery voltage. This function makes sure that the
1587 * the allowed current limit isn't exceeded.
1589 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1592 struct ab8500_charger *di = container_of(work,
1593 struct ab8500_charger, check_vbat_work.work);
1595 class_for_each_device(power_supply_class, NULL,
1596 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1598 /* First run old_vbat is 0. */
1599 if (di->old_vbat == 0)
1600 di->old_vbat = di->vbat;
1602 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1603 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1604 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1605 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1607 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1608 " old: %d\n", di->max_usb_in_curr, di->vbat,
1610 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1611 power_supply_changed(&di->usb_chg.psy);
1614 di->old_vbat = di->vbat;
1617 * No need to check the battery voltage every second when not close to
1620 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1621 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1624 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1628 * ab8500_charger_check_hw_failure_work() - check main charger failure
1629 * @work: pointer to the work_struct structure
1631 * Work queue function for checking the main charger status
1633 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1638 struct ab8500_charger *di = container_of(work,
1639 struct ab8500_charger, check_hw_failure_work.work);
1641 /* Check if the status bits for HW failure is still active */
1642 if (di->flags.mainextchnotok) {
1643 ret = abx500_get_register_interruptible(di->dev,
1644 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1646 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1649 if (!(reg_value & MAIN_CH_NOK)) {
1650 di->flags.mainextchnotok = false;
1651 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1654 if (di->flags.vbus_ovv) {
1655 ret = abx500_get_register_interruptible(di->dev,
1656 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1659 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1662 if (!(reg_value & VBUS_OVV_TH)) {
1663 di->flags.vbus_ovv = false;
1664 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1667 /* If we still have a failure, schedule a new check */
1668 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1669 queue_delayed_work(di->charger_wq,
1670 &di->check_hw_failure_work, round_jiffies(HZ));
1675 * ab8500_charger_kick_watchdog_work() - kick the watchdog
1676 * @work: pointer to the work_struct structure
1678 * Work queue function for kicking the charger watchdog.
1680 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
1681 * logic. That means we have to continously kick the charger
1682 * watchdog even when no charger is connected. This is only
1683 * valid once the AC charger has been enabled. This is
1684 * a bug that is not handled by the algorithm and the
1685 * watchdog have to be kicked by the charger driver
1686 * when the AC charger is disabled
1688 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
1692 struct ab8500_charger *di = container_of(work,
1693 struct ab8500_charger, kick_wd_work.work);
1695 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1696 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1698 dev_err(di->dev, "Failed to kick WD!\n");
1700 /* Schedule a new watchdog kick */
1701 queue_delayed_work(di->charger_wq,
1702 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
1706 * ab8500_charger_ac_work() - work to get and set main charger status
1707 * @work: pointer to the work_struct structure
1709 * Work queue function for checking the main charger status
1711 static void ab8500_charger_ac_work(struct work_struct *work)
1715 struct ab8500_charger *di = container_of(work,
1716 struct ab8500_charger, ac_work);
1719 * Since we can't be sure that the events are received
1720 * synchronously, we have the check if the main charger is
1721 * connected by reading the status register
1723 ret = ab8500_charger_detect_chargers(di);
1727 if (ret & AC_PW_CONN) {
1728 di->ac.charger_connected = 1;
1731 di->ac.charger_connected = 0;
1734 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1735 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
1738 static void ab8500_charger_usb_attached_work(struct work_struct *work)
1740 struct ab8500_charger *di = container_of(work,
1741 struct ab8500_charger,
1742 usb_charger_attached_work.work);
1743 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
1747 for (i = 0; i < 10; i++) {
1748 ret = abx500_get_register_interruptible(di->dev,
1750 AB8500_CH_USBCH_STAT1_REG,
1753 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1756 if ((statval & usbch) != usbch)
1759 msleep(CHARGER_STATUS_POLL);
1762 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
1764 mutex_lock(&di->charger_attached_mutex);
1765 mutex_unlock(&di->charger_attached_mutex);
1770 queue_delayed_work(di->charger_wq,
1771 &di->usb_charger_attached_work,
1775 static void ab8500_charger_ac_attached_work(struct work_struct *work)
1778 struct ab8500_charger *di = container_of(work,
1779 struct ab8500_charger,
1780 ac_charger_attached_work.work);
1781 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
1782 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
1786 for (i = 0; i < 10; i++) {
1787 ret = abx500_get_register_interruptible(di->dev,
1789 AB8500_CH_STATUS2_REG,
1792 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1796 if ((statval & mainch) != mainch)
1799 msleep(CHARGER_STATUS_POLL);
1802 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
1803 queue_work(di->charger_wq, &di->ac_work);
1805 mutex_lock(&di->charger_attached_mutex);
1806 mutex_unlock(&di->charger_attached_mutex);
1811 queue_delayed_work(di->charger_wq,
1812 &di->ac_charger_attached_work,
1817 * ab8500_charger_detect_usb_type_work() - work to detect USB type
1818 * @work: Pointer to the work_struct structure
1820 * Detect the type of USB plugged
1822 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
1826 struct ab8500_charger *di = container_of(work,
1827 struct ab8500_charger, detect_usb_type_work);
1830 * Since we can't be sure that the events are received
1831 * synchronously, we have the check if is
1832 * connected by reading the status register
1834 ret = ab8500_charger_detect_chargers(di);
1838 if (!(ret & USB_PW_CONN)) {
1839 di->vbus_detected = 0;
1840 ab8500_charger_set_usb_connected(di, false);
1841 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1843 di->vbus_detected = 1;
1845 if (is_ab8500_1p1_or_earlier(di->parent)) {
1846 ret = ab8500_charger_detect_usb_type(di);
1848 ab8500_charger_set_usb_connected(di, true);
1849 ab8500_power_supply_changed(di,
1853 /* For ABB cut2.0 and onwards we have an IRQ,
1854 * USB_LINK_STATUS that will be triggered when the USB
1855 * link status changes. The exception is USB connected
1856 * during startup. Then we don't get a
1857 * USB_LINK_STATUS IRQ
1859 if (di->vbus_detected_start) {
1860 di->vbus_detected_start = false;
1861 ret = ab8500_charger_detect_usb_type(di);
1863 ab8500_charger_set_usb_connected(di,
1865 ab8500_power_supply_changed(di,
1874 * ab8500_charger_usb_link_status_work() - work to detect USB type
1875 * @work: pointer to the work_struct structure
1877 * Detect the type of USB plugged
1879 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
1883 struct ab8500_charger *di = container_of(work,
1884 struct ab8500_charger, usb_link_status_work);
1887 * Since we can't be sure that the events are received
1888 * synchronously, we have the check if is
1889 * connected by reading the status register
1891 ret = ab8500_charger_detect_chargers(di);
1895 if (!(ret & USB_PW_CONN)) {
1896 di->vbus_detected = 0;
1897 ab8500_charger_set_usb_connected(di, false);
1898 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1900 di->vbus_detected = 1;
1901 ret = ab8500_charger_read_usb_type(di);
1903 /* Update maximum input current */
1904 ret = ab8500_charger_set_vbus_in_curr(di,
1905 di->max_usb_in_curr);
1909 ab8500_charger_set_usb_connected(di, true);
1910 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1911 } else if (ret == -ENXIO) {
1912 /* No valid charger type detected */
1913 ab8500_charger_set_usb_connected(di, false);
1914 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1919 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
1922 unsigned long flags;
1924 struct ab8500_charger *di = container_of(work,
1925 struct ab8500_charger, usb_state_changed_work);
1927 if (!di->vbus_detected)
1930 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
1931 di->usb_state.usb_changed = false;
1932 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
1935 * wait for some time until you get updates from the usb stack
1936 * and negotiations are completed
1940 if (di->usb_state.usb_changed)
1943 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
1944 __func__, di->usb_state.state, di->usb_state.usb_current);
1946 switch (di->usb_state.state) {
1947 case AB8500_BM_USB_STATE_RESET_HS:
1948 case AB8500_BM_USB_STATE_RESET_FS:
1949 case AB8500_BM_USB_STATE_SUSPEND:
1950 case AB8500_BM_USB_STATE_MAX:
1951 ab8500_charger_set_usb_connected(di, false);
1952 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1955 case AB8500_BM_USB_STATE_RESUME:
1957 * when suspend->resume there should be delay
1958 * of 1sec for enabling charging
1961 /* Intentional fall through */
1962 case AB8500_BM_USB_STATE_CONFIGURED:
1964 * USB is configured, enable charging with the charging
1965 * input current obtained from USB driver
1967 if (!ab8500_charger_get_usb_cur(di)) {
1968 /* Update maximum input current */
1969 ret = ab8500_charger_set_vbus_in_curr(di,
1970 di->max_usb_in_curr);
1974 ab8500_charger_set_usb_connected(di, true);
1975 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1985 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
1986 * @work: pointer to the work_struct structure
1988 * Work queue function for checking the USB charger Not OK status
1990 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
1996 struct ab8500_charger *di = container_of(work,
1997 struct ab8500_charger, check_usbchgnotok_work.work);
1999 /* Check if the status bit for usbchargernotok is still active */
2000 ret = abx500_get_register_interruptible(di->dev,
2001 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2003 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2006 prev_status = di->flags.usbchargernotok;
2008 if (reg_value & VBUS_CH_NOK) {
2009 di->flags.usbchargernotok = true;
2010 /* Check again in 1sec */
2011 queue_delayed_work(di->charger_wq,
2012 &di->check_usbchgnotok_work, HZ);
2014 di->flags.usbchargernotok = false;
2015 di->flags.vbus_collapse = false;
2018 if (prev_status != di->flags.usbchargernotok)
2019 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2023 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2024 * @work: pointer to the work_struct structure
2026 * Work queue function for checking the Main thermal prot status
2028 static void ab8500_charger_check_main_thermal_prot_work(
2029 struct work_struct *work)
2034 struct ab8500_charger *di = container_of(work,
2035 struct ab8500_charger, check_main_thermal_prot_work);
2037 /* Check if the status bit for main_thermal_prot is still active */
2038 ret = abx500_get_register_interruptible(di->dev,
2039 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
2041 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2044 if (reg_value & MAIN_CH_TH_PROT)
2045 di->flags.main_thermal_prot = true;
2047 di->flags.main_thermal_prot = false;
2049 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2053 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2054 * @work: pointer to the work_struct structure
2056 * Work queue function for checking the USB thermal prot status
2058 static void ab8500_charger_check_usb_thermal_prot_work(
2059 struct work_struct *work)
2064 struct ab8500_charger *di = container_of(work,
2065 struct ab8500_charger, check_usb_thermal_prot_work);
2067 /* Check if the status bit for usb_thermal_prot is still active */
2068 ret = abx500_get_register_interruptible(di->dev,
2069 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
2071 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2074 if (reg_value & USB_CH_TH_PROT)
2075 di->flags.usb_thermal_prot = true;
2077 di->flags.usb_thermal_prot = false;
2079 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2083 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2084 * @irq: interrupt number
2085 * @_di: pointer to the ab8500_charger structure
2087 * Returns IRQ status(IRQ_HANDLED)
2089 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2091 struct ab8500_charger *di = _di;
2093 dev_dbg(di->dev, "Main charger unplugged\n");
2094 queue_work(di->charger_wq, &di->ac_work);
2096 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2097 mutex_lock(&di->charger_attached_mutex);
2098 mutex_unlock(&di->charger_attached_mutex);
2104 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2105 * @irq: interrupt number
2106 * @_di: pointer to the ab8500_charger structure
2108 * Returns IRQ status(IRQ_HANDLED)
2110 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2112 struct ab8500_charger *di = _di;
2114 dev_dbg(di->dev, "Main charger plugged\n");
2115 queue_work(di->charger_wq, &di->ac_work);
2117 mutex_lock(&di->charger_attached_mutex);
2118 mutex_unlock(&di->charger_attached_mutex);
2119 queue_delayed_work(di->charger_wq,
2120 &di->ac_charger_attached_work,
2126 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2127 * @irq: interrupt number
2128 * @_di: pointer to the ab8500_charger structure
2130 * Returns IRQ status(IRQ_HANDLED)
2132 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2134 struct ab8500_charger *di = _di;
2136 dev_dbg(di->dev, "Main charger not ok\n");
2137 di->flags.mainextchnotok = true;
2138 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2140 /* Schedule a new HW failure check */
2141 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2147 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2148 * thermal protection threshold
2149 * @irq: interrupt number
2150 * @_di: pointer to the ab8500_charger structure
2152 * Returns IRQ status(IRQ_HANDLED)
2154 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2156 struct ab8500_charger *di = _di;
2159 "Die temp above Main charger thermal protection threshold\n");
2160 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2166 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2167 * thermal protection threshold
2168 * @irq: interrupt number
2169 * @_di: pointer to the ab8500_charger structure
2171 * Returns IRQ status(IRQ_HANDLED)
2173 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2175 struct ab8500_charger *di = _di;
2178 "Die temp ok for Main charger thermal protection threshold\n");
2179 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2185 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2186 * @irq: interrupt number
2187 * @_di: pointer to the ab8500_charger structure
2189 * Returns IRQ status(IRQ_HANDLED)
2191 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2193 struct ab8500_charger *di = _di;
2195 dev_dbg(di->dev, "VBUS falling detected\n");
2196 queue_work(di->charger_wq, &di->detect_usb_type_work);
2202 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2203 * @irq: interrupt number
2204 * @_di: pointer to the ab8500_charger structure
2206 * Returns IRQ status(IRQ_HANDLED)
2208 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2210 struct ab8500_charger *di = _di;
2212 di->vbus_detected = true;
2213 dev_dbg(di->dev, "VBUS rising detected\n");
2214 queue_work(di->charger_wq, &di->detect_usb_type_work);
2220 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2221 * @irq: interrupt number
2222 * @_di: pointer to the ab8500_charger structure
2224 * Returns IRQ status(IRQ_HANDLED)
2226 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2228 struct ab8500_charger *di = _di;
2230 dev_dbg(di->dev, "USB link status changed\n");
2232 queue_work(di->charger_wq, &di->usb_link_status_work);
2238 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2239 * thermal protection threshold
2240 * @irq: interrupt number
2241 * @_di: pointer to the ab8500_charger structure
2243 * Returns IRQ status(IRQ_HANDLED)
2245 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2247 struct ab8500_charger *di = _di;
2250 "Die temp above USB charger thermal protection threshold\n");
2251 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2257 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2258 * thermal protection threshold
2259 * @irq: interrupt number
2260 * @_di: pointer to the ab8500_charger structure
2262 * Returns IRQ status(IRQ_HANDLED)
2264 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2266 struct ab8500_charger *di = _di;
2269 "Die temp ok for USB charger thermal protection threshold\n");
2270 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2276 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2277 * @irq: interrupt number
2278 * @_di: pointer to the ab8500_charger structure
2280 * Returns IRQ status(IRQ_HANDLED)
2282 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2284 struct ab8500_charger *di = _di;
2286 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2287 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2293 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2294 * @irq: interrupt number
2295 * @_di: pointer to the ab8500_charger structure
2297 * Returns IRQ status(IRQ_HANDLED)
2299 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2301 struct ab8500_charger *di = _di;
2303 dev_dbg(di->dev, "Charger watchdog expired\n");
2306 * The charger that was online when the watchdog expired
2307 * needs to be restarted for charging to start again
2309 if (di->ac.charger_online) {
2310 di->ac.wd_expired = true;
2311 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2313 if (di->usb.charger_online) {
2314 di->usb.wd_expired = true;
2315 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2322 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2323 * @irq: interrupt number
2324 * @_di: pointer to the ab8500_charger structure
2326 * Returns IRQ status(IRQ_HANDLED)
2328 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2330 struct ab8500_charger *di = _di;
2332 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2333 di->flags.vbus_ovv = true;
2334 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2336 /* Schedule a new HW failure check */
2337 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2343 * ab8500_charger_ac_get_property() - get the ac/mains properties
2344 * @psy: pointer to the power_supply structure
2345 * @psp: pointer to the power_supply_property structure
2346 * @val: pointer to the power_supply_propval union
2348 * This function gets called when an application tries to get the ac/mains
2349 * properties by reading the sysfs files.
2350 * AC/Mains properties are online, present and voltage.
2351 * online: ac/mains charging is in progress or not
2352 * present: presence of the ac/mains
2353 * voltage: AC/Mains voltage
2354 * Returns error code in case of failure else 0(on success)
2356 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2357 enum power_supply_property psp,
2358 union power_supply_propval *val)
2360 struct ab8500_charger *di;
2362 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2365 case POWER_SUPPLY_PROP_HEALTH:
2366 if (di->flags.mainextchnotok)
2367 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2368 else if (di->ac.wd_expired || di->usb.wd_expired)
2369 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2370 else if (di->flags.main_thermal_prot)
2371 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2373 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2375 case POWER_SUPPLY_PROP_ONLINE:
2376 val->intval = di->ac.charger_online;
2378 case POWER_SUPPLY_PROP_PRESENT:
2379 val->intval = di->ac.charger_connected;
2381 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2382 di->ac.charger_voltage = ab8500_charger_get_ac_voltage(di);
2383 val->intval = di->ac.charger_voltage * 1000;
2385 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2387 * This property is used to indicate when CV mode is entered
2388 * for the AC charger
2390 di->ac.cv_active = ab8500_charger_ac_cv(di);
2391 val->intval = di->ac.cv_active;
2393 case POWER_SUPPLY_PROP_CURRENT_NOW:
2394 val->intval = ab8500_charger_get_ac_current(di) * 1000;
2403 * ab8500_charger_usb_get_property() - get the usb properties
2404 * @psy: pointer to the power_supply structure
2405 * @psp: pointer to the power_supply_property structure
2406 * @val: pointer to the power_supply_propval union
2408 * This function gets called when an application tries to get the usb
2409 * properties by reading the sysfs files.
2410 * USB properties are online, present and voltage.
2411 * online: usb charging is in progress or not
2412 * present: presence of the usb
2413 * voltage: vbus voltage
2414 * Returns error code in case of failure else 0(on success)
2416 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2417 enum power_supply_property psp,
2418 union power_supply_propval *val)
2420 struct ab8500_charger *di;
2422 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2425 case POWER_SUPPLY_PROP_HEALTH:
2426 if (di->flags.usbchargernotok)
2427 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2428 else if (di->ac.wd_expired || di->usb.wd_expired)
2429 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2430 else if (di->flags.usb_thermal_prot)
2431 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2432 else if (di->flags.vbus_ovv)
2433 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2435 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2437 case POWER_SUPPLY_PROP_ONLINE:
2438 val->intval = di->usb.charger_online;
2440 case POWER_SUPPLY_PROP_PRESENT:
2441 val->intval = di->usb.charger_connected;
2443 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2444 di->usb.charger_voltage = ab8500_charger_get_vbus_voltage(di);
2445 val->intval = di->usb.charger_voltage * 1000;
2447 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2449 * This property is used to indicate when CV mode is entered
2450 * for the USB charger
2452 di->usb.cv_active = ab8500_charger_usb_cv(di);
2453 val->intval = di->usb.cv_active;
2455 case POWER_SUPPLY_PROP_CURRENT_NOW:
2456 val->intval = ab8500_charger_get_usb_current(di) * 1000;
2458 case POWER_SUPPLY_PROP_CURRENT_AVG:
2460 * This property is used to indicate when VBUS has collapsed
2461 * due to too high output current from the USB charger
2463 if (di->flags.vbus_collapse)
2475 * ab8500_charger_init_hw_registers() - Set up charger related registers
2476 * @di: pointer to the ab8500_charger structure
2478 * Set up charger OVV, watchdog and maximum voltage registers as well as
2479 * charging of the backup battery
2481 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
2485 /* Setup maximum charger current and voltage for ABB cut2.0 */
2486 if (!is_ab8500_1p1_or_earlier(di->parent)) {
2487 ret = abx500_set_register_interruptible(di->dev,
2489 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
2492 "failed to set CH_VOLT_LVL_MAX_REG\n");
2496 ret = abx500_set_register_interruptible(di->dev,
2498 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
2501 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2506 /* VBUS OVV set to 6.3V and enable automatic current limitiation */
2507 ret = abx500_set_register_interruptible(di->dev,
2509 AB8500_USBCH_CTRL2_REG,
2510 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
2512 dev_err(di->dev, "failed to set VBUS OVV\n");
2516 /* Enable main watchdog in OTP */
2517 ret = abx500_set_register_interruptible(di->dev,
2518 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
2520 dev_err(di->dev, "failed to enable main WD in OTP\n");
2524 /* Enable main watchdog */
2525 ret = abx500_set_register_interruptible(di->dev,
2526 AB8500_SYS_CTRL2_BLOCK,
2527 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
2529 dev_err(di->dev, "faile to enable main watchdog\n");
2534 * Due to internal synchronisation, Enable and Kick watchdog bits
2535 * cannot be enabled in a single write.
2536 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
2537 * between writing Enable then Kick bits.
2541 /* Kick main watchdog */
2542 ret = abx500_set_register_interruptible(di->dev,
2543 AB8500_SYS_CTRL2_BLOCK,
2544 AB8500_MAIN_WDOG_CTRL_REG,
2545 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
2547 dev_err(di->dev, "failed to kick main watchdog\n");
2551 /* Disable main watchdog */
2552 ret = abx500_set_register_interruptible(di->dev,
2553 AB8500_SYS_CTRL2_BLOCK,
2554 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
2556 dev_err(di->dev, "failed to disable main watchdog\n");
2560 /* Set watchdog timeout */
2561 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2562 AB8500_CH_WD_TIMER_REG, WD_TIMER);
2564 dev_err(di->dev, "failed to set charger watchdog timeout\n");
2568 /* Backup battery voltage and current */
2569 ret = abx500_set_register_interruptible(di->dev,
2571 AB8500_RTC_BACKUP_CHG_REG,
2572 di->bm->bkup_bat_v |
2573 di->bm->bkup_bat_i);
2575 dev_err(di->dev, "failed to setup backup battery charging\n");
2579 /* Enable backup battery charging */
2580 abx500_mask_and_set_register_interruptible(di->dev,
2581 AB8500_RTC, AB8500_RTC_CTRL_REG,
2582 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
2584 dev_err(di->dev, "%s mask and set failed\n", __func__);
2591 * ab8500 charger driver interrupts and their respective isr
2593 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
2594 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
2595 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
2596 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
2597 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
2598 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
2599 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
2600 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
2601 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
2602 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
2603 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
2604 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
2605 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
2606 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
2609 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
2610 unsigned long event, void *power)
2612 struct ab8500_charger *di =
2613 container_of(nb, struct ab8500_charger, nb);
2614 enum ab8500_usb_state bm_usb_state;
2615 unsigned mA = *((unsigned *)power);
2617 if (event != USB_EVENT_VBUS) {
2618 dev_dbg(di->dev, "not a standard host, returning\n");
2622 /* TODO: State is fabricate here. See if charger really needs USB
2623 * state or if mA is enough
2625 if ((di->usb_state.usb_current == 2) && (mA > 2))
2626 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
2628 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
2630 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
2631 else if (mA >= 8) /* 8, 100, 500 */
2632 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
2633 else /* Should never occur */
2634 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
2636 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
2637 __func__, bm_usb_state, mA);
2639 spin_lock(&di->usb_state.usb_lock);
2640 di->usb_state.usb_changed = true;
2641 spin_unlock(&di->usb_state.usb_lock);
2643 di->usb_state.state = bm_usb_state;
2644 di->usb_state.usb_current = mA;
2646 queue_work(di->charger_wq, &di->usb_state_changed_work);
2651 #if defined(CONFIG_PM)
2652 static int ab8500_charger_resume(struct platform_device *pdev)
2655 struct ab8500_charger *di = platform_get_drvdata(pdev);
2658 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2659 * logic. That means we have to continously kick the charger
2660 * watchdog even when no charger is connected. This is only
2661 * valid once the AC charger has been enabled. This is
2662 * a bug that is not handled by the algorithm and the
2663 * watchdog have to be kicked by the charger driver
2664 * when the AC charger is disabled
2666 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
2667 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2668 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2670 dev_err(di->dev, "Failed to kick WD!\n");
2672 /* If not already pending start a new timer */
2673 if (!delayed_work_pending(
2674 &di->kick_wd_work)) {
2675 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
2676 round_jiffies(WD_KICK_INTERVAL));
2680 /* If we still have a HW failure, schedule a new check */
2681 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2682 queue_delayed_work(di->charger_wq,
2683 &di->check_hw_failure_work, 0);
2689 static int ab8500_charger_suspend(struct platform_device *pdev,
2692 struct ab8500_charger *di = platform_get_drvdata(pdev);
2694 /* Cancel any pending HW failure check */
2695 if (delayed_work_pending(&di->check_hw_failure_work))
2696 cancel_delayed_work(&di->check_hw_failure_work);
2701 #define ab8500_charger_suspend NULL
2702 #define ab8500_charger_resume NULL
2705 static int ab8500_charger_remove(struct platform_device *pdev)
2707 struct ab8500_charger *di = platform_get_drvdata(pdev);
2710 /* Disable AC charging */
2711 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
2713 /* Disable USB charging */
2714 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
2716 /* Disable interrupts */
2717 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2718 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2722 /* Backup battery voltage and current disable */
2723 ret = abx500_mask_and_set_register_interruptible(di->dev,
2724 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
2726 dev_err(di->dev, "%s mask and set failed\n", __func__);
2728 usb_unregister_notifier(di->usb_phy, &di->nb);
2729 usb_put_phy(di->usb_phy);
2731 /* Delete the work queue */
2732 destroy_workqueue(di->charger_wq);
2734 flush_scheduled_work();
2735 power_supply_unregister(&di->usb_chg.psy);
2736 power_supply_unregister(&di->ac_chg.psy);
2737 platform_set_drvdata(pdev, NULL);
2742 static char *supply_interface[] = {
2748 static int ab8500_charger_probe(struct platform_device *pdev)
2750 struct device_node *np = pdev->dev.of_node;
2751 struct abx500_bm_data *plat = pdev->dev.platform_data;
2752 struct ab8500_charger *di;
2753 int irq, i, charger_status, ret = 0, ch_stat;
2755 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
2757 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
2762 dev_err(&pdev->dev, "no battery management data supplied\n");
2768 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
2770 dev_err(&pdev->dev, "failed to get battery information\n");
2773 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
2775 di->autopower_cfg = false;
2777 /* get parent data */
2778 di->dev = &pdev->dev;
2779 di->parent = dev_get_drvdata(pdev->dev.parent);
2780 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2782 /* initialize lock */
2783 spin_lock_init(&di->usb_state.usb_lock);
2785 di->autopower = false;
2788 /* power_supply base class */
2789 di->ac_chg.psy.name = "ab8500_ac";
2790 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
2791 di->ac_chg.psy.properties = ab8500_charger_ac_props;
2792 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
2793 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
2794 di->ac_chg.psy.supplied_to = supply_interface;
2795 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
2796 /* ux500_charger sub-class */
2797 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
2798 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2799 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2800 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
2801 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2802 di->ac_chg.max_out_curr = ab8500_charger_current_map[
2803 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2806 /* power_supply base class */
2807 di->usb_chg.psy.name = "ab8500_usb";
2808 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
2809 di->usb_chg.psy.properties = ab8500_charger_usb_props;
2810 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
2811 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
2812 di->usb_chg.psy.supplied_to = supply_interface;
2813 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
2814 /* ux500_charger sub-class */
2815 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
2816 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2817 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2818 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
2819 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2820 di->usb_chg.max_out_curr = ab8500_charger_current_map[
2821 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2824 /* Create a work queue for the charger */
2826 create_singlethread_workqueue("ab8500_charger_wq");
2827 if (di->charger_wq == NULL) {
2828 dev_err(di->dev, "failed to create work queue\n");
2832 mutex_init(&di->charger_attached_mutex);
2834 /* Init work for HW failure check */
2835 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
2836 ab8500_charger_check_hw_failure_work);
2837 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
2838 ab8500_charger_check_usbchargernotok_work);
2840 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
2841 ab8500_charger_ac_attached_work);
2842 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
2843 ab8500_charger_usb_attached_work);
2846 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2847 * logic. That means we have to continously kick the charger
2848 * watchdog even when no charger is connected. This is only
2849 * valid once the AC charger has been enabled. This is
2850 * a bug that is not handled by the algorithm and the
2851 * watchdog have to be kicked by the charger driver
2852 * when the AC charger is disabled
2854 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
2855 ab8500_charger_kick_watchdog_work);
2857 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
2858 ab8500_charger_check_vbat_work);
2860 /* Init work for charger detection */
2861 INIT_WORK(&di->usb_link_status_work,
2862 ab8500_charger_usb_link_status_work);
2863 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
2864 INIT_WORK(&di->detect_usb_type_work,
2865 ab8500_charger_detect_usb_type_work);
2867 INIT_WORK(&di->usb_state_changed_work,
2868 ab8500_charger_usb_state_changed_work);
2870 /* Init work for checking HW status */
2871 INIT_WORK(&di->check_main_thermal_prot_work,
2872 ab8500_charger_check_main_thermal_prot_work);
2873 INIT_WORK(&di->check_usb_thermal_prot_work,
2874 ab8500_charger_check_usb_thermal_prot_work);
2877 * VDD ADC supply needs to be enabled from this driver when there
2878 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
2879 * interrupts during charging
2881 di->regu = devm_regulator_get(di->dev, "vddadc");
2882 if (IS_ERR(di->regu)) {
2883 ret = PTR_ERR(di->regu);
2884 dev_err(di->dev, "failed to get vddadc regulator\n");
2885 goto free_charger_wq;
2889 /* Initialize OVV, and other registers */
2890 ret = ab8500_charger_init_hw_registers(di);
2892 dev_err(di->dev, "failed to initialize ABB registers\n");
2893 goto free_charger_wq;
2896 /* Register AC charger class */
2897 ret = power_supply_register(di->dev, &di->ac_chg.psy);
2899 dev_err(di->dev, "failed to register AC charger\n");
2900 goto free_charger_wq;
2903 /* Register USB charger class */
2904 ret = power_supply_register(di->dev, &di->usb_chg.psy);
2906 dev_err(di->dev, "failed to register USB charger\n");
2910 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
2911 if (IS_ERR_OR_NULL(di->usb_phy)) {
2912 dev_err(di->dev, "failed to get usb transceiver\n");
2916 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
2917 ret = usb_register_notifier(di->usb_phy, &di->nb);
2919 dev_err(di->dev, "failed to register usb notifier\n");
2923 /* Identify the connected charger types during startup */
2924 charger_status = ab8500_charger_detect_chargers(di);
2925 if (charger_status & AC_PW_CONN) {
2926 di->ac.charger_connected = 1;
2928 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2929 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2932 if (charger_status & USB_PW_CONN) {
2933 dev_dbg(di->dev, "VBUS Detect during startup\n");
2934 di->vbus_detected = true;
2935 di->vbus_detected_start = true;
2936 queue_work(di->charger_wq,
2937 &di->detect_usb_type_work);
2940 /* Register interrupts */
2941 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2942 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2943 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
2944 IRQF_SHARED | IRQF_NO_SUSPEND,
2945 ab8500_charger_irq[i].name, di);
2948 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
2949 , ab8500_charger_irq[i].name, irq, ret);
2952 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
2953 ab8500_charger_irq[i].name, irq, ret);
2956 platform_set_drvdata(pdev, di);
2958 mutex_lock(&di->charger_attached_mutex);
2960 ch_stat = ab8500_charger_detect_chargers(di);
2962 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
2963 queue_delayed_work(di->charger_wq,
2964 &di->ac_charger_attached_work,
2967 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
2968 queue_delayed_work(di->charger_wq,
2969 &di->usb_charger_attached_work,
2973 mutex_unlock(&di->charger_attached_mutex);
2978 usb_unregister_notifier(di->usb_phy, &di->nb);
2980 /* We also have to free all successfully registered irqs */
2981 for (i = i - 1; i >= 0; i--) {
2982 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2986 usb_put_phy(di->usb_phy);
2988 power_supply_unregister(&di->usb_chg.psy);
2990 power_supply_unregister(&di->ac_chg.psy);
2992 destroy_workqueue(di->charger_wq);
2996 static const struct of_device_id ab8500_charger_match[] = {
2997 { .compatible = "stericsson,ab8500-charger", },
3001 static struct platform_driver ab8500_charger_driver = {
3002 .probe = ab8500_charger_probe,
3003 .remove = ab8500_charger_remove,
3004 .suspend = ab8500_charger_suspend,
3005 .resume = ab8500_charger_resume,
3007 .name = "ab8500-charger",
3008 .owner = THIS_MODULE,
3009 .of_match_table = ab8500_charger_match,
3013 static int __init ab8500_charger_init(void)
3015 return platform_driver_register(&ab8500_charger_driver);
3018 static void __exit ab8500_charger_exit(void)
3020 platform_driver_unregister(&ab8500_charger_driver);
3023 subsys_initcall_sync(ab8500_charger_init);
3024 module_exit(ab8500_charger_exit);
3026 MODULE_LICENSE("GPL v2");
3027 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3028 MODULE_ALIAS("platform:ab8500-charger");
3029 MODULE_DESCRIPTION("AB8500 charger management driver");