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>
35 /* Charger constants */
40 #define MAIN_WDOG_ENA 0x01
41 #define MAIN_WDOG_KICK 0x02
42 #define MAIN_WDOG_DIS 0x00
43 #define CHARG_WD_KICK 0x01
44 #define MAIN_CH_ENA 0x01
45 #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
46 #define USB_CH_ENA 0x01
47 #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
48 #define MAIN_CH_DET 0x01
49 #define MAIN_CH_CV_ON 0x04
50 #define USB_CH_CV_ON 0x08
51 #define VBUS_DET_DBNC100 0x02
52 #define VBUS_DET_DBNC1 0x01
53 #define OTP_ENABLE_WD 0x01
55 #define MAIN_CH_INPUT_CURR_SHIFT 4
56 #define VBUS_IN_CURR_LIM_SHIFT 4
58 #define LED_INDICATOR_PWM_ENA 0x01
59 #define LED_INDICATOR_PWM_DIS 0x00
60 #define LED_IND_CUR_5MA 0x04
61 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
63 /* HW failure constants */
64 #define MAIN_CH_TH_PROT 0x02
65 #define VBUS_CH_NOK 0x08
66 #define USB_CH_TH_PROT 0x02
67 #define VBUS_OVV_TH 0x01
68 #define MAIN_CH_NOK 0x01
71 /* UsbLineStatus register bit masks */
72 #define AB8500_USB_LINK_STATUS 0x78
73 #define AB8500_STD_HOST_SUSP 0x18
75 /* Watchdog timeout constant */
76 #define WD_TIMER 0x30 /* 4min */
77 #define WD_KICK_INTERVAL (60 * HZ)
79 /* Lowest charger voltage is 3.39V -> 0x4E */
80 #define LOW_VOLT_REG 0x4E
82 /* Step up/down delay in us */
83 #define STEP_UDELAY 1000
85 /* UsbLineStatus register - usb types */
86 enum ab8500_charger_link_status {
87 USB_STAT_NOT_CONFIGURED,
89 USB_STAT_STD_HOST_C_NS,
90 USB_STAT_STD_HOST_C_S,
93 USB_STAT_HOST_CHG_HS_CHIRP,
94 USB_STAT_DEDICATED_CHG,
97 USB_STAT_ACA_RID_C_NM,
98 USB_STAT_ACA_RID_C_HS,
99 USB_STAT_ACA_RID_C_HS_CHIRP,
102 USB_STAT_NOT_VALID_LINK,
105 enum ab8500_usb_state {
106 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
107 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
108 AB8500_BM_USB_STATE_CONFIGURED,
109 AB8500_BM_USB_STATE_SUSPEND,
110 AB8500_BM_USB_STATE_RESUME,
111 AB8500_BM_USB_STATE_MAX,
114 /* VBUS input current limits supported in AB8500 in mA */
115 #define USB_CH_IP_CUR_LVL_0P05 50
116 #define USB_CH_IP_CUR_LVL_0P09 98
117 #define USB_CH_IP_CUR_LVL_0P19 193
118 #define USB_CH_IP_CUR_LVL_0P29 290
119 #define USB_CH_IP_CUR_LVL_0P38 380
120 #define USB_CH_IP_CUR_LVL_0P45 450
121 #define USB_CH_IP_CUR_LVL_0P5 500
122 #define USB_CH_IP_CUR_LVL_0P6 600
123 #define USB_CH_IP_CUR_LVL_0P7 700
124 #define USB_CH_IP_CUR_LVL_0P8 800
125 #define USB_CH_IP_CUR_LVL_0P9 900
126 #define USB_CH_IP_CUR_LVL_1P0 1000
127 #define USB_CH_IP_CUR_LVL_1P1 1100
128 #define USB_CH_IP_CUR_LVL_1P3 1300
129 #define USB_CH_IP_CUR_LVL_1P4 1400
130 #define USB_CH_IP_CUR_LVL_1P5 1500
132 #define VBAT_TRESH_IP_CUR_RED 3800
134 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
135 struct ab8500_charger, usb_chg)
136 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
137 struct ab8500_charger, ac_chg)
140 * struct ab8500_charger_interrupts - ab8500 interupts
141 * @name: name of the interrupt
142 * @isr function pointer to the isr
144 struct ab8500_charger_interrupts {
146 irqreturn_t (*isr)(int irq, void *data);
149 struct ab8500_charger_info {
150 int charger_connected;
157 struct ab8500_charger_event_flags {
159 bool main_thermal_prot;
160 bool usb_thermal_prot;
162 bool usbchargernotok;
167 struct ab8500_charger_usb_state {
170 enum ab8500_usb_state state;
175 * struct ab8500_charger - ab8500 Charger device information
176 * @dev: Pointer to the structure device
177 * @max_usb_in_curr: Max USB charger input current
178 * @vbus_detected: VBUS detected
179 * @vbus_detected_start:
180 * VBUS detected during startup
181 * @ac_conn: This will be true when the AC charger has been plugged
182 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
184 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
186 * @vbat Battery voltage
187 * @old_vbat Previously measured battery voltage
188 * @autopower Indicate if we should have automatic pwron after pwrloss
189 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
190 * @parent: Pointer to the struct ab8500
191 * @gpadc: Pointer to the struct gpadc
192 * @bm: Platform specific battery management information
193 * @flags: Structure for information about events triggered
194 * @usb_state: Structure for usb stack information
195 * @ac_chg: AC charger power supply
196 * @usb_chg: USB charger power supply
197 * @ac: Structure that holds the AC charger properties
198 * @usb: Structure that holds the USB charger properties
199 * @regu: Pointer to the struct regulator
200 * @charger_wq: Work queue for the IRQs and checking HW state
201 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
202 * @check_hw_failure_work: Work for checking HW state
203 * @check_usbchgnotok_work: Work for checking USB charger not ok status
204 * @kick_wd_work: Work for kicking the charger watchdog in case
205 * of ABB rev 1.* due to the watchog logic bug
206 * @ac_work: Work for checking AC charger connection
207 * @detect_usb_type_work: Work for detecting the USB type connected
208 * @usb_link_status_work: Work for checking the new USB link status
209 * @usb_state_changed_work: Work for checking USB state
210 * @check_main_thermal_prot_work:
211 * Work for checking Main thermal status
212 * @check_usb_thermal_prot_work:
213 * Work for checking USB thermal status
215 struct ab8500_charger {
219 bool vbus_detected_start;
227 struct ab8500 *parent;
228 struct ab8500_gpadc *gpadc;
229 struct abx500_bm_data *bm;
230 struct ab8500_charger_event_flags flags;
231 struct ab8500_charger_usb_state usb_state;
232 struct ux500_charger ac_chg;
233 struct ux500_charger usb_chg;
234 struct ab8500_charger_info ac;
235 struct ab8500_charger_info usb;
236 struct regulator *regu;
237 struct workqueue_struct *charger_wq;
238 struct delayed_work check_vbat_work;
239 struct delayed_work check_hw_failure_work;
240 struct delayed_work check_usbchgnotok_work;
241 struct delayed_work kick_wd_work;
242 struct work_struct ac_work;
243 struct work_struct detect_usb_type_work;
244 struct work_struct usb_link_status_work;
245 struct work_struct usb_state_changed_work;
246 struct work_struct check_main_thermal_prot_work;
247 struct work_struct check_usb_thermal_prot_work;
248 struct usb_phy *usb_phy;
249 struct notifier_block nb;
253 static enum power_supply_property ab8500_charger_ac_props[] = {
254 POWER_SUPPLY_PROP_HEALTH,
255 POWER_SUPPLY_PROP_PRESENT,
256 POWER_SUPPLY_PROP_ONLINE,
257 POWER_SUPPLY_PROP_VOLTAGE_NOW,
258 POWER_SUPPLY_PROP_VOLTAGE_AVG,
259 POWER_SUPPLY_PROP_CURRENT_NOW,
263 static enum power_supply_property ab8500_charger_usb_props[] = {
264 POWER_SUPPLY_PROP_HEALTH,
265 POWER_SUPPLY_PROP_CURRENT_AVG,
266 POWER_SUPPLY_PROP_PRESENT,
267 POWER_SUPPLY_PROP_ONLINE,
268 POWER_SUPPLY_PROP_VOLTAGE_NOW,
269 POWER_SUPPLY_PROP_VOLTAGE_AVG,
270 POWER_SUPPLY_PROP_CURRENT_NOW,
274 * Function for enabling and disabling sw fallback mode
275 * should always be disabled when no charger is connected.
277 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
283 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
285 /* read the register containing fallback bit */
286 ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, ®);
288 dev_err(di->dev, "%d write failed\n", __LINE__);
292 /* enable the OPT emulation registers */
293 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
295 dev_err(di->dev, "%d write failed\n", __LINE__);
304 /* write back the changed fallback bit value to register */
305 ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg);
307 dev_err(di->dev, "%d write failed\n", __LINE__);
311 /* disable the set OTP registers again */
312 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
314 dev_err(di->dev, "%d write failed\n", __LINE__);
320 * ab8500_power_supply_changed - a wrapper with local extentions for
321 * power_supply_changed
322 * @di: pointer to the ab8500_charger structure
323 * @psy: pointer to power_supply_that have changed.
326 static void ab8500_power_supply_changed(struct ab8500_charger *di,
327 struct power_supply *psy)
329 if (di->autopower_cfg) {
330 if (!di->usb.charger_connected &&
331 !di->ac.charger_connected &&
333 di->autopower = false;
334 ab8500_enable_disable_sw_fallback(di, false);
335 } else if (!di->autopower &&
336 (di->ac.charger_connected ||
337 di->usb.charger_connected)) {
338 di->autopower = true;
339 ab8500_enable_disable_sw_fallback(di, true);
342 power_supply_changed(psy);
345 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
348 if (connected != di->usb.charger_connected) {
349 dev_dbg(di->dev, "USB connected:%i\n", connected);
350 di->usb.charger_connected = connected;
351 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
356 * ab8500_charger_get_ac_voltage() - get ac charger voltage
357 * @di: pointer to the ab8500_charger structure
359 * Returns ac charger voltage (on success)
361 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
365 /* Only measure voltage if the charger is connected */
366 if (di->ac.charger_connected) {
367 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
369 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
377 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
378 * @di: pointer to the ab8500_charger structure
380 * Returns ac charger CV mode (on success) else error code
382 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
387 /* Only check CV mode if the charger is online */
388 if (di->ac.charger_online) {
389 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
390 AB8500_CH_STATUS1_REG, &val);
392 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
396 if (val & MAIN_CH_CV_ON)
406 * ab8500_charger_get_vbus_voltage() - get vbus voltage
407 * @di: pointer to the ab8500_charger structure
409 * This function returns the vbus voltage.
410 * Returns vbus voltage (on success)
412 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
416 /* Only measure voltage if the charger is connected */
417 if (di->usb.charger_connected) {
418 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
420 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
428 * ab8500_charger_get_usb_current() - get usb charger current
429 * @di: pointer to the ab8500_charger structure
431 * This function returns the usb charger current.
432 * Returns usb current (on success) and error code on failure
434 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
438 /* Only measure current if the charger is online */
439 if (di->usb.charger_online) {
440 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
442 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
450 * ab8500_charger_get_ac_current() - get ac charger current
451 * @di: pointer to the ab8500_charger structure
453 * This function returns the ac charger current.
454 * Returns ac current (on success) and error code on failure.
456 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
460 /* Only measure current if the charger is online */
461 if (di->ac.charger_online) {
462 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
464 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
472 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
473 * @di: pointer to the ab8500_charger structure
475 * Returns ac charger CV mode (on success) else error code
477 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
482 /* Only check CV mode if the charger is online */
483 if (di->usb.charger_online) {
484 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
485 AB8500_CH_USBCH_STAT1_REG, &val);
487 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
491 if (val & USB_CH_CV_ON)
503 * ab8500_charger_detect_chargers() - Detect the connected chargers
504 * @di: pointer to the ab8500_charger structure
506 * Returns the type of charger connected.
507 * For USB it will not mean we can actually charge from it
508 * but that there is a USB cable connected that we have to
509 * identify. This is used during startup when we don't get
510 * interrupts of the charger detection
512 * Returns an integer value, that means,
513 * NO_PW_CONN no power supply is connected
514 * AC_PW_CONN if the AC power supply is connected
515 * USB_PW_CONN if the USB power supply is connected
516 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
518 static int ab8500_charger_detect_chargers(struct ab8500_charger *di)
520 int result = NO_PW_CONN;
524 /* Check for AC charger */
525 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
526 AB8500_CH_STATUS1_REG, &val);
528 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
532 if (val & MAIN_CH_DET)
535 /* Check for USB charger */
536 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
537 AB8500_CH_USBCH_STAT1_REG, &val);
539 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
543 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
544 result |= USB_PW_CONN;
550 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
551 * @di: pointer to the ab8500_charger structure
552 * @link_status: the identified USB type
554 * Get the maximum current that is allowed to be drawn from the host
555 * based on the USB type.
556 * Returns error code in case of failure else 0 on success
558 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
559 enum ab8500_charger_link_status link_status)
563 switch (link_status) {
564 case USB_STAT_STD_HOST_NC:
565 case USB_STAT_STD_HOST_C_NS:
566 case USB_STAT_STD_HOST_C_S:
567 dev_dbg(di->dev, "USB Type - Standard host is "
568 "detected through USB driver\n");
569 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
571 case USB_STAT_HOST_CHG_HS_CHIRP:
572 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
574 case USB_STAT_HOST_CHG_HS:
575 case USB_STAT_ACA_RID_C_HS:
576 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
578 case USB_STAT_ACA_RID_A:
580 * Dedicated charger level minus maximum current accessory
581 * can consume (300mA). Closest level is 1100mA
583 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1;
585 case USB_STAT_ACA_RID_B:
587 * Dedicated charger level minus 120mA (20mA for ACA and
588 * 100mA for potential accessory). Closest level is 1300mA
590 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
592 case USB_STAT_DEDICATED_CHG:
593 case USB_STAT_HOST_CHG_NM:
594 case USB_STAT_ACA_RID_C_HS_CHIRP:
595 case USB_STAT_ACA_RID_C_NM:
596 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
598 case USB_STAT_RESERVED:
600 * This state is used to indicate that VBUS has dropped below
601 * the detection level 4 times in a row. This is due to the
602 * charger output current is set to high making the charger
603 * voltage collapse. This have to be propagated through to
604 * chargalg. This is done using the property
605 * POWER_SUPPLY_PROP_CURRENT_AVG = 1
607 di->flags.vbus_collapse = true;
608 dev_dbg(di->dev, "USB Type - USB_STAT_RESERVED "
609 "VBUS has collapsed\n");
612 case USB_STAT_HM_IDGND:
613 case USB_STAT_NOT_CONFIGURED:
614 case USB_STAT_NOT_VALID_LINK:
615 dev_err(di->dev, "USB Type - Charging not allowed\n");
616 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
620 dev_err(di->dev, "USB Type - Unknown\n");
621 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
626 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
627 link_status, di->max_usb_in_curr);
633 * ab8500_charger_read_usb_type() - read the type of usb connected
634 * @di: pointer to the ab8500_charger structure
636 * Detect the type of the plugged USB
637 * Returns error code in case of failure else 0 on success
639 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
644 ret = abx500_get_register_interruptible(di->dev,
645 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
647 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
650 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
651 AB8500_USB_LINE_STAT_REG, &val);
653 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
657 /* get the USB type */
658 val = (val & AB8500_USB_LINK_STATUS) >> 3;
659 ret = ab8500_charger_max_usb_curr(di,
660 (enum ab8500_charger_link_status) val);
666 * ab8500_charger_detect_usb_type() - get the type of usb connected
667 * @di: pointer to the ab8500_charger structure
669 * Detect the type of the plugged USB
670 * Returns error code in case of failure else 0 on success
672 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
678 * On getting the VBUS rising edge detect interrupt there
679 * is a 250ms delay after which the register UsbLineStatus
680 * is filled with valid data.
682 for (i = 0; i < 10; i++) {
684 ret = abx500_get_register_interruptible(di->dev,
685 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
688 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
691 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
692 AB8500_USB_LINE_STAT_REG, &val);
694 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
698 * Until the IT source register is read the UsbLineStatus
699 * register is not updated, hence doing the same
703 /* get the USB type */
704 val = (val & AB8500_USB_LINK_STATUS) >> 3;
708 ret = ab8500_charger_max_usb_curr(di,
709 (enum ab8500_charger_link_status) val);
715 * This array maps the raw hex value to charger voltage used by the AB8500
716 * Values taken from the UM0836
718 static int ab8500_charger_voltage_map[] = {
800 * This array maps the raw hex value to charger current used by the AB8500
801 * Values taken from the UM0836
803 static int ab8500_charger_current_map[] = {
822 * This array maps the raw hex value to VBUS input current used by the AB8500
823 * Values taken from the UM0836
825 static int ab8500_charger_vbus_in_curr_map[] = {
826 USB_CH_IP_CUR_LVL_0P05,
827 USB_CH_IP_CUR_LVL_0P09,
828 USB_CH_IP_CUR_LVL_0P19,
829 USB_CH_IP_CUR_LVL_0P29,
830 USB_CH_IP_CUR_LVL_0P38,
831 USB_CH_IP_CUR_LVL_0P45,
832 USB_CH_IP_CUR_LVL_0P5,
833 USB_CH_IP_CUR_LVL_0P6,
834 USB_CH_IP_CUR_LVL_0P7,
835 USB_CH_IP_CUR_LVL_0P8,
836 USB_CH_IP_CUR_LVL_0P9,
837 USB_CH_IP_CUR_LVL_1P0,
838 USB_CH_IP_CUR_LVL_1P1,
839 USB_CH_IP_CUR_LVL_1P3,
840 USB_CH_IP_CUR_LVL_1P4,
841 USB_CH_IP_CUR_LVL_1P5,
844 static int ab8500_voltage_to_regval(int voltage)
848 /* Special case for voltage below 3.5V */
849 if (voltage < ab8500_charger_voltage_map[0])
852 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
853 if (voltage < ab8500_charger_voltage_map[i])
857 /* If not last element, return error */
858 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
859 if (voltage == ab8500_charger_voltage_map[i])
865 static int ab8500_current_to_regval(int curr)
869 if (curr < ab8500_charger_current_map[0])
872 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
873 if (curr < ab8500_charger_current_map[i])
877 /* If not last element, return error */
878 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
879 if (curr == ab8500_charger_current_map[i])
885 static int ab8500_vbus_in_curr_to_regval(int curr)
889 if (curr < ab8500_charger_vbus_in_curr_map[0])
892 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
893 if (curr < ab8500_charger_vbus_in_curr_map[i])
897 /* If not last element, return error */
898 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
899 if (curr == ab8500_charger_vbus_in_curr_map[i])
906 * ab8500_charger_get_usb_cur() - get usb current
907 * @di: pointer to the ab8500_charger structre
909 * The usb stack provides the maximum current that can be drawn from
910 * the standard usb host. This will be in mA.
911 * This function converts current in mA to a value that can be written
912 * to the register. Returns -1 if charging is not allowed
914 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
916 switch (di->usb_state.usb_current) {
918 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
921 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
924 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
927 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
930 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
933 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
941 * ab8500_charger_set_current() - set charger current
942 * @di: pointer to the ab8500_charger structure
943 * @ich: charger current, in mA
944 * @reg: select what charger register to set
946 * Set charger current.
947 * There is no state machine in the AB to step up/down the charger
948 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
949 * charging is started. Instead we need to implement
950 * this charger current step-up/down here.
951 * Returns error code in case of failure else 0(on success)
953 static int ab8500_charger_set_current(struct ab8500_charger *di,
957 int curr_index, prev_curr_index, shift_value;
961 case AB8500_MCH_IPT_CURLVL_REG:
962 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
963 curr_index = ab8500_current_to_regval(ich);
965 case AB8500_USBCH_IPT_CRNTLVL_REG:
966 shift_value = VBUS_IN_CURR_LIM_SHIFT;
967 curr_index = ab8500_vbus_in_curr_to_regval(ich);
969 case AB8500_CH_OPT_CRNTLVL_REG:
971 curr_index = ab8500_current_to_regval(ich);
974 dev_err(di->dev, "%s current register not valid\n", __func__);
978 if (curr_index < 0) {
979 dev_err(di->dev, "requested current limit out-of-range\n");
983 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
986 dev_err(di->dev, "%s read failed\n", __func__);
989 prev_curr_index = (reg_value >> shift_value);
991 /* only update current if it's been changed */
992 if (prev_curr_index == curr_index)
995 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
998 if (prev_curr_index > curr_index) {
999 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1000 ret = abx500_set_register_interruptible(di->dev,
1001 AB8500_CHARGER, reg, (u8) i << shift_value);
1003 dev_err(di->dev, "%s write failed\n", __func__);
1006 usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
1009 for (i = prev_curr_index + 1; i <= curr_index; i++) {
1010 ret = abx500_set_register_interruptible(di->dev,
1011 AB8500_CHARGER, reg, (u8) i << shift_value);
1013 dev_err(di->dev, "%s write failed\n", __func__);
1016 usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
1023 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1024 * @di: pointer to the ab8500_charger structure
1025 * @ich_in: charger input current limit
1027 * Sets the current that can be drawn from the USB host
1028 * Returns error code in case of failure else 0(on success)
1030 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1035 /* We should always use to lowest current limit */
1036 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1038 switch (min_value) {
1040 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1041 min_value = USB_CH_IP_CUR_LVL_0P05;
1044 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1045 min_value = USB_CH_IP_CUR_LVL_0P45;
1051 return ab8500_charger_set_current(di, min_value,
1052 AB8500_USBCH_IPT_CRNTLVL_REG);
1056 * ab8500_charger_set_main_in_curr() - set main charger input current
1057 * @di: pointer to the ab8500_charger structure
1058 * @ich_in: input charger current, in mA
1060 * Set main charger input current.
1061 * Returns error code in case of failure else 0(on success)
1063 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1066 return ab8500_charger_set_current(di, ich_in,
1067 AB8500_MCH_IPT_CURLVL_REG);
1071 * ab8500_charger_set_output_curr() - set charger output current
1072 * @di: pointer to the ab8500_charger structure
1073 * @ich_out: output charger current, in mA
1075 * Set charger output current.
1076 * Returns error code in case of failure else 0(on success)
1078 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1081 return ab8500_charger_set_current(di, ich_out,
1082 AB8500_CH_OPT_CRNTLVL_REG);
1086 * ab8500_charger_led_en() - turn on/off chargign led
1087 * @di: pointer to the ab8500_charger structure
1088 * @on: flag to turn on/off the chargign led
1090 * Power ON/OFF charging LED indication
1091 * Returns error code in case of failure else 0(on success)
1093 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1098 /* Power ON charging LED indicator, set LED current to 5mA */
1099 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1100 AB8500_LED_INDICATOR_PWM_CTRL,
1101 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1103 dev_err(di->dev, "Power ON LED failed\n");
1106 /* LED indicator PWM duty cycle 252/256 */
1107 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1108 AB8500_LED_INDICATOR_PWM_DUTY,
1109 LED_INDICATOR_PWM_DUTY_252_256);
1111 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1115 /* Power off charging LED indicator */
1116 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1117 AB8500_LED_INDICATOR_PWM_CTRL,
1118 LED_INDICATOR_PWM_DIS);
1120 dev_err(di->dev, "Power-off LED failed\n");
1129 * ab8500_charger_ac_en() - enable or disable ac charging
1130 * @di: pointer to the ab8500_charger structure
1131 * @enable: enable/disable flag
1132 * @vset: charging voltage
1133 * @iset: charging current
1135 * Enable/Disable AC/Mains charging and turns on/off the charging led
1138 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1139 int enable, int vset, int iset)
1144 int input_curr_index;
1147 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1150 /* Check if AC is connected */
1151 if (!di->ac.charger_connected) {
1152 dev_err(di->dev, "AC charger not connected\n");
1156 /* Enable AC charging */
1157 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1160 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1161 * will be triggered everytime we enable the VDD ADC supply.
1162 * This will turn off charging for a short while.
1163 * It can be avoided by having the supply on when
1164 * there is a charger enabled. Normally the VDD ADC supply
1165 * is enabled everytime a GPADC conversion is triggered. We will
1166 * force it to be enabled from this driver to have
1167 * the GPADC module independant of the AB8500 chargers
1169 if (!di->vddadc_en_ac) {
1170 regulator_enable(di->regu);
1171 di->vddadc_en_ac = true;
1174 /* Check if the requested voltage or current is valid */
1175 volt_index = ab8500_voltage_to_regval(vset);
1176 curr_index = ab8500_current_to_regval(iset);
1177 input_curr_index = ab8500_current_to_regval(
1178 di->bm->chg_params->ac_curr_max);
1179 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1181 "Charger voltage or current too high, "
1182 "charging not started\n");
1186 /* ChVoltLevel: maximum battery charging voltage */
1187 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1188 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1190 dev_err(di->dev, "%s write failed\n", __func__);
1193 /* MainChInputCurr: current that can be drawn from the charger*/
1194 ret = ab8500_charger_set_main_in_curr(di,
1195 di->bm->chg_params->ac_curr_max);
1197 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1201 /* ChOutputCurentLevel: protected output current */
1202 ret = ab8500_charger_set_output_curr(di, iset);
1204 dev_err(di->dev, "%s "
1205 "Failed to set ChOutputCurentLevel\n",
1210 /* Check if VBAT overshoot control should be enabled */
1211 if (!di->bm->enable_overshoot)
1212 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1214 /* Enable Main Charger */
1215 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1216 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1218 dev_err(di->dev, "%s write failed\n", __func__);
1222 /* Power on charging LED indication */
1223 ret = ab8500_charger_led_en(di, true);
1225 dev_err(di->dev, "failed to enable LED\n");
1227 di->ac.charger_online = 1;
1229 /* Disable AC charging */
1230 if (is_ab8500_1p1_or_earlier(di->parent)) {
1232 * For ABB revision 1.0 and 1.1 there is a bug in the
1233 * watchdog logic. That means we have to continously
1234 * kick the charger watchdog even when no charger is
1235 * connected. This is only valid once the AC charger
1236 * has been enabled. This is a bug that is not handled
1237 * by the algorithm and the watchdog have to be kicked
1238 * by the charger driver when the AC charger
1242 queue_delayed_work(di->charger_wq,
1244 round_jiffies(WD_KICK_INTERVAL));
1248 * We can't turn off charging completely
1249 * due to a bug in AB8500 cut1.
1250 * If we do, charging will not start again.
1251 * That is why we set the lowest voltage
1252 * and current possible
1254 ret = abx500_set_register_interruptible(di->dev,
1256 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1259 "%s write failed\n", __func__);
1263 ret = ab8500_charger_set_output_curr(di, 0);
1265 dev_err(di->dev, "%s "
1266 "Failed to set ChOutputCurentLevel\n",
1271 ret = abx500_set_register_interruptible(di->dev,
1273 AB8500_MCH_CTRL1, 0);
1276 "%s write failed\n", __func__);
1281 ret = ab8500_charger_led_en(di, false);
1283 dev_err(di->dev, "failed to disable LED\n");
1285 di->ac.charger_online = 0;
1286 di->ac.wd_expired = false;
1288 /* Disable regulator if enabled */
1289 if (di->vddadc_en_ac) {
1290 regulator_disable(di->regu);
1291 di->vddadc_en_ac = false;
1294 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1296 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1302 * ab8500_charger_usb_en() - enable usb charging
1303 * @di: pointer to the ab8500_charger structure
1304 * @enable: enable/disable flag
1305 * @vset: charging voltage
1306 * @ich_out: charger output current
1308 * Enable/Disable USB charging and turns on/off the charging led respectively.
1309 * Returns error code in case of failure else 0(on success)
1311 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1312 int enable, int vset, int ich_out)
1319 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1322 /* Check if USB is connected */
1323 if (!di->usb.charger_connected) {
1324 dev_err(di->dev, "USB charger not connected\n");
1329 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1330 * will be triggered everytime we enable the VDD ADC supply.
1331 * This will turn off charging for a short while.
1332 * It can be avoided by having the supply on when
1333 * there is a charger enabled. Normally the VDD ADC supply
1334 * is enabled everytime a GPADC conversion is triggered. We will
1335 * force it to be enabled from this driver to have
1336 * the GPADC module independant of the AB8500 chargers
1338 if (!di->vddadc_en_usb) {
1339 regulator_enable(di->regu);
1340 di->vddadc_en_usb = true;
1343 /* Enable USB charging */
1344 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1346 /* Check if the requested voltage or current is valid */
1347 volt_index = ab8500_voltage_to_regval(vset);
1348 curr_index = ab8500_current_to_regval(ich_out);
1349 if (volt_index < 0 || curr_index < 0) {
1351 "Charger voltage or current too high, "
1352 "charging not started\n");
1356 /* ChVoltLevel: max voltage upto which battery can be charged */
1357 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1358 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1360 dev_err(di->dev, "%s write failed\n", __func__);
1363 /* USBChInputCurr: current that can be drawn from the usb */
1364 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1366 dev_err(di->dev, "setting USBChInputCurr failed\n");
1369 /* ChOutputCurentLevel: protected output current */
1370 ret = ab8500_charger_set_output_curr(di, ich_out);
1372 dev_err(di->dev, "%s "
1373 "Failed to set ChOutputCurentLevel\n",
1377 /* Check if VBAT overshoot control should be enabled */
1378 if (!di->bm->enable_overshoot)
1379 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1381 /* Enable USB Charger */
1382 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1383 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1385 dev_err(di->dev, "%s write failed\n", __func__);
1389 /* If success power on charging LED indication */
1390 ret = ab8500_charger_led_en(di, true);
1392 dev_err(di->dev, "failed to enable LED\n");
1394 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1396 di->usb.charger_online = 1;
1398 /* Disable USB charging */
1399 ret = abx500_set_register_interruptible(di->dev,
1401 AB8500_USBCH_CTRL1_REG, 0);
1404 "%s write failed\n", __func__);
1408 ret = ab8500_charger_led_en(di, false);
1410 dev_err(di->dev, "failed to disable LED\n");
1412 di->usb.charger_online = 0;
1413 di->usb.wd_expired = false;
1415 /* Disable regulator if enabled */
1416 if (di->vddadc_en_usb) {
1417 regulator_disable(di->regu);
1418 di->vddadc_en_usb = false;
1421 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1423 /* Cancel any pending Vbat check work */
1424 if (delayed_work_pending(&di->check_vbat_work))
1425 cancel_delayed_work(&di->check_vbat_work);
1428 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1434 * ab8500_charger_watchdog_kick() - kick charger watchdog
1435 * @di: pointer to the ab8500_charger structure
1437 * Kick charger watchdog
1438 * Returns error code in case of failure else 0(on success)
1440 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1443 struct ab8500_charger *di;
1445 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1446 di = to_ab8500_charger_ac_device_info(charger);
1447 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1448 di = to_ab8500_charger_usb_device_info(charger);
1452 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1453 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1455 dev_err(di->dev, "Failed to kick WD!\n");
1461 * ab8500_charger_update_charger_current() - update charger current
1462 * @di: pointer to the ab8500_charger structure
1464 * Update the charger output current for the specified charger
1465 * Returns error code in case of failure else 0(on success)
1467 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1471 struct ab8500_charger *di;
1473 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1474 di = to_ab8500_charger_ac_device_info(charger);
1475 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1476 di = to_ab8500_charger_usb_device_info(charger);
1480 ret = ab8500_charger_set_output_curr(di, ich_out);
1482 dev_err(di->dev, "%s "
1483 "Failed to set ChOutputCurentLevel\n",
1488 /* Reset the main and usb drop input current measurement counter */
1489 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1490 AB8500_CHARGER_CTRL,
1493 dev_err(di->dev, "%s write failed\n", __func__);
1500 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1502 struct power_supply *psy;
1503 struct power_supply *ext;
1504 struct ab8500_charger *di;
1505 union power_supply_propval ret;
1507 bool psy_found = false;
1508 struct ux500_charger *usb_chg;
1510 usb_chg = (struct ux500_charger *)data;
1511 psy = &usb_chg->psy;
1513 di = to_ab8500_charger_usb_device_info(usb_chg);
1515 ext = dev_get_drvdata(dev);
1517 /* For all psy where the driver name appears in any supplied_to */
1518 for (i = 0; i < ext->num_supplicants; i++) {
1519 if (!strcmp(ext->supplied_to[i], psy->name))
1526 /* Go through all properties for the psy */
1527 for (j = 0; j < ext->num_properties; j++) {
1528 enum power_supply_property prop;
1529 prop = ext->properties[j];
1531 if (ext->get_property(ext, prop, &ret))
1535 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1536 switch (ext->type) {
1537 case POWER_SUPPLY_TYPE_BATTERY:
1538 di->vbat = ret.intval / 1000;
1552 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1553 * @work pointer to the work_struct structure
1555 * Due to a asic bug it is necessary to lower the input current to the vbus
1556 * charger when charging with at some specific levels. This issue is only valid
1557 * for below a certain battery voltage. This function makes sure that the
1558 * the allowed current limit isn't exceeded.
1560 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1563 struct ab8500_charger *di = container_of(work,
1564 struct ab8500_charger, check_vbat_work.work);
1566 class_for_each_device(power_supply_class, NULL,
1567 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1569 /* First run old_vbat is 0. */
1570 if (di->old_vbat == 0)
1571 di->old_vbat = di->vbat;
1573 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1574 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1575 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1576 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1578 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1579 " old: %d\n", di->max_usb_in_curr, di->vbat,
1581 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1582 power_supply_changed(&di->usb_chg.psy);
1585 di->old_vbat = di->vbat;
1588 * No need to check the battery voltage every second when not close to
1591 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1592 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1595 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1599 * ab8500_charger_check_hw_failure_work() - check main charger failure
1600 * @work: pointer to the work_struct structure
1602 * Work queue function for checking the main charger status
1604 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1609 struct ab8500_charger *di = container_of(work,
1610 struct ab8500_charger, check_hw_failure_work.work);
1612 /* Check if the status bits for HW failure is still active */
1613 if (di->flags.mainextchnotok) {
1614 ret = abx500_get_register_interruptible(di->dev,
1615 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1617 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1620 if (!(reg_value & MAIN_CH_NOK)) {
1621 di->flags.mainextchnotok = false;
1622 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1625 if (di->flags.vbus_ovv) {
1626 ret = abx500_get_register_interruptible(di->dev,
1627 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1630 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1633 if (!(reg_value & VBUS_OVV_TH)) {
1634 di->flags.vbus_ovv = false;
1635 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1638 /* If we still have a failure, schedule a new check */
1639 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1640 queue_delayed_work(di->charger_wq,
1641 &di->check_hw_failure_work, round_jiffies(HZ));
1646 * ab8500_charger_kick_watchdog_work() - kick the watchdog
1647 * @work: pointer to the work_struct structure
1649 * Work queue function for kicking the charger watchdog.
1651 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
1652 * logic. That means we have to continously kick the charger
1653 * watchdog even when no charger is connected. This is only
1654 * valid once the AC charger has been enabled. This is
1655 * a bug that is not handled by the algorithm and the
1656 * watchdog have to be kicked by the charger driver
1657 * when the AC charger is disabled
1659 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
1663 struct ab8500_charger *di = container_of(work,
1664 struct ab8500_charger, kick_wd_work.work);
1666 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1667 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1669 dev_err(di->dev, "Failed to kick WD!\n");
1671 /* Schedule a new watchdog kick */
1672 queue_delayed_work(di->charger_wq,
1673 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
1677 * ab8500_charger_ac_work() - work to get and set main charger status
1678 * @work: pointer to the work_struct structure
1680 * Work queue function for checking the main charger status
1682 static void ab8500_charger_ac_work(struct work_struct *work)
1686 struct ab8500_charger *di = container_of(work,
1687 struct ab8500_charger, ac_work);
1690 * Since we can't be sure that the events are received
1691 * synchronously, we have the check if the main charger is
1692 * connected by reading the status register
1694 ret = ab8500_charger_detect_chargers(di);
1698 if (ret & AC_PW_CONN) {
1699 di->ac.charger_connected = 1;
1702 di->ac.charger_connected = 0;
1705 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1706 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
1710 * ab8500_charger_detect_usb_type_work() - work to detect USB type
1711 * @work: Pointer to the work_struct structure
1713 * Detect the type of USB plugged
1715 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
1719 struct ab8500_charger *di = container_of(work,
1720 struct ab8500_charger, detect_usb_type_work);
1723 * Since we can't be sure that the events are received
1724 * synchronously, we have the check if is
1725 * connected by reading the status register
1727 ret = ab8500_charger_detect_chargers(di);
1731 if (!(ret & USB_PW_CONN)) {
1732 di->vbus_detected = 0;
1733 ab8500_charger_set_usb_connected(di, false);
1734 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1736 di->vbus_detected = 1;
1738 if (is_ab8500_1p1_or_earlier(di->parent)) {
1739 ret = ab8500_charger_detect_usb_type(di);
1741 ab8500_charger_set_usb_connected(di, true);
1742 ab8500_power_supply_changed(di,
1746 /* For ABB cut2.0 and onwards we have an IRQ,
1747 * USB_LINK_STATUS that will be triggered when the USB
1748 * link status changes. The exception is USB connected
1749 * during startup. Then we don't get a
1750 * USB_LINK_STATUS IRQ
1752 if (di->vbus_detected_start) {
1753 di->vbus_detected_start = false;
1754 ret = ab8500_charger_detect_usb_type(di);
1756 ab8500_charger_set_usb_connected(di,
1758 ab8500_power_supply_changed(di,
1767 * ab8500_charger_usb_link_status_work() - work to detect USB type
1768 * @work: pointer to the work_struct structure
1770 * Detect the type of USB plugged
1772 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
1776 struct ab8500_charger *di = container_of(work,
1777 struct ab8500_charger, usb_link_status_work);
1780 * Since we can't be sure that the events are received
1781 * synchronously, we have the check if is
1782 * connected by reading the status register
1784 ret = ab8500_charger_detect_chargers(di);
1788 if (!(ret & USB_PW_CONN)) {
1789 di->vbus_detected = 0;
1790 ab8500_charger_set_usb_connected(di, false);
1791 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1793 di->vbus_detected = 1;
1794 ret = ab8500_charger_read_usb_type(di);
1796 /* Update maximum input current */
1797 ret = ab8500_charger_set_vbus_in_curr(di,
1798 di->max_usb_in_curr);
1802 ab8500_charger_set_usb_connected(di, true);
1803 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1804 } else if (ret == -ENXIO) {
1805 /* No valid charger type detected */
1806 ab8500_charger_set_usb_connected(di, false);
1807 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1812 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
1815 unsigned long flags;
1817 struct ab8500_charger *di = container_of(work,
1818 struct ab8500_charger, usb_state_changed_work);
1820 if (!di->vbus_detected)
1823 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
1824 di->usb_state.usb_changed = false;
1825 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
1828 * wait for some time until you get updates from the usb stack
1829 * and negotiations are completed
1833 if (di->usb_state.usb_changed)
1836 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
1837 __func__, di->usb_state.state, di->usb_state.usb_current);
1839 switch (di->usb_state.state) {
1840 case AB8500_BM_USB_STATE_RESET_HS:
1841 case AB8500_BM_USB_STATE_RESET_FS:
1842 case AB8500_BM_USB_STATE_SUSPEND:
1843 case AB8500_BM_USB_STATE_MAX:
1844 ab8500_charger_set_usb_connected(di, false);
1845 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1848 case AB8500_BM_USB_STATE_RESUME:
1850 * when suspend->resume there should be delay
1851 * of 1sec for enabling charging
1854 /* Intentional fall through */
1855 case AB8500_BM_USB_STATE_CONFIGURED:
1857 * USB is configured, enable charging with the charging
1858 * input current obtained from USB driver
1860 if (!ab8500_charger_get_usb_cur(di)) {
1861 /* Update maximum input current */
1862 ret = ab8500_charger_set_vbus_in_curr(di,
1863 di->max_usb_in_curr);
1867 ab8500_charger_set_usb_connected(di, true);
1868 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1878 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
1879 * @work: pointer to the work_struct structure
1881 * Work queue function for checking the USB charger Not OK status
1883 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
1889 struct ab8500_charger *di = container_of(work,
1890 struct ab8500_charger, check_usbchgnotok_work.work);
1892 /* Check if the status bit for usbchargernotok is still active */
1893 ret = abx500_get_register_interruptible(di->dev,
1894 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
1896 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1899 prev_status = di->flags.usbchargernotok;
1901 if (reg_value & VBUS_CH_NOK) {
1902 di->flags.usbchargernotok = true;
1903 /* Check again in 1sec */
1904 queue_delayed_work(di->charger_wq,
1905 &di->check_usbchgnotok_work, HZ);
1907 di->flags.usbchargernotok = false;
1908 di->flags.vbus_collapse = false;
1911 if (prev_status != di->flags.usbchargernotok)
1912 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1916 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
1917 * @work: pointer to the work_struct structure
1919 * Work queue function for checking the Main thermal prot status
1921 static void ab8500_charger_check_main_thermal_prot_work(
1922 struct work_struct *work)
1927 struct ab8500_charger *di = container_of(work,
1928 struct ab8500_charger, check_main_thermal_prot_work);
1930 /* Check if the status bit for main_thermal_prot is still active */
1931 ret = abx500_get_register_interruptible(di->dev,
1932 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1934 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1937 if (reg_value & MAIN_CH_TH_PROT)
1938 di->flags.main_thermal_prot = true;
1940 di->flags.main_thermal_prot = false;
1942 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1946 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
1947 * @work: pointer to the work_struct structure
1949 * Work queue function for checking the USB thermal prot status
1951 static void ab8500_charger_check_usb_thermal_prot_work(
1952 struct work_struct *work)
1957 struct ab8500_charger *di = container_of(work,
1958 struct ab8500_charger, check_usb_thermal_prot_work);
1960 /* Check if the status bit for usb_thermal_prot is still active */
1961 ret = abx500_get_register_interruptible(di->dev,
1962 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
1964 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1967 if (reg_value & USB_CH_TH_PROT)
1968 di->flags.usb_thermal_prot = true;
1970 di->flags.usb_thermal_prot = false;
1972 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1976 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
1977 * @irq: interrupt number
1978 * @_di: pointer to the ab8500_charger structure
1980 * Returns IRQ status(IRQ_HANDLED)
1982 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
1984 struct ab8500_charger *di = _di;
1986 dev_dbg(di->dev, "Main charger unplugged\n");
1987 queue_work(di->charger_wq, &di->ac_work);
1993 * ab8500_charger_mainchplugdet_handler() - main charger plugged
1994 * @irq: interrupt number
1995 * @_di: pointer to the ab8500_charger structure
1997 * Returns IRQ status(IRQ_HANDLED)
1999 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2001 struct ab8500_charger *di = _di;
2003 dev_dbg(di->dev, "Main charger plugged\n");
2004 queue_work(di->charger_wq, &di->ac_work);
2010 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2011 * @irq: interrupt number
2012 * @_di: pointer to the ab8500_charger structure
2014 * Returns IRQ status(IRQ_HANDLED)
2016 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2018 struct ab8500_charger *di = _di;
2020 dev_dbg(di->dev, "Main charger not ok\n");
2021 di->flags.mainextchnotok = true;
2022 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2024 /* Schedule a new HW failure check */
2025 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2031 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2032 * thermal protection threshold
2033 * @irq: interrupt number
2034 * @_di: pointer to the ab8500_charger structure
2036 * Returns IRQ status(IRQ_HANDLED)
2038 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2040 struct ab8500_charger *di = _di;
2043 "Die temp above Main charger thermal protection threshold\n");
2044 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2050 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2051 * thermal protection threshold
2052 * @irq: interrupt number
2053 * @_di: pointer to the ab8500_charger structure
2055 * Returns IRQ status(IRQ_HANDLED)
2057 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2059 struct ab8500_charger *di = _di;
2062 "Die temp ok for Main charger thermal protection threshold\n");
2063 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2069 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2070 * @irq: interrupt number
2071 * @_di: pointer to the ab8500_charger structure
2073 * Returns IRQ status(IRQ_HANDLED)
2075 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2077 struct ab8500_charger *di = _di;
2079 dev_dbg(di->dev, "VBUS falling detected\n");
2080 queue_work(di->charger_wq, &di->detect_usb_type_work);
2086 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2087 * @irq: interrupt number
2088 * @_di: pointer to the ab8500_charger structure
2090 * Returns IRQ status(IRQ_HANDLED)
2092 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2094 struct ab8500_charger *di = _di;
2096 di->vbus_detected = true;
2097 dev_dbg(di->dev, "VBUS rising detected\n");
2098 queue_work(di->charger_wq, &di->detect_usb_type_work);
2104 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2105 * @irq: interrupt number
2106 * @_di: pointer to the ab8500_charger structure
2108 * Returns IRQ status(IRQ_HANDLED)
2110 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2112 struct ab8500_charger *di = _di;
2114 dev_dbg(di->dev, "USB link status changed\n");
2116 queue_work(di->charger_wq, &di->usb_link_status_work);
2122 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2123 * thermal protection threshold
2124 * @irq: interrupt number
2125 * @_di: pointer to the ab8500_charger structure
2127 * Returns IRQ status(IRQ_HANDLED)
2129 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2131 struct ab8500_charger *di = _di;
2134 "Die temp above USB charger thermal protection threshold\n");
2135 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2141 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2142 * thermal protection threshold
2143 * @irq: interrupt number
2144 * @_di: pointer to the ab8500_charger structure
2146 * Returns IRQ status(IRQ_HANDLED)
2148 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2150 struct ab8500_charger *di = _di;
2153 "Die temp ok for USB charger thermal protection threshold\n");
2154 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2160 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2161 * @irq: interrupt number
2162 * @_di: pointer to the ab8500_charger structure
2164 * Returns IRQ status(IRQ_HANDLED)
2166 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2168 struct ab8500_charger *di = _di;
2170 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2171 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2177 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2178 * @irq: interrupt number
2179 * @_di: pointer to the ab8500_charger structure
2181 * Returns IRQ status(IRQ_HANDLED)
2183 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2185 struct ab8500_charger *di = _di;
2187 dev_dbg(di->dev, "Charger watchdog expired\n");
2190 * The charger that was online when the watchdog expired
2191 * needs to be restarted for charging to start again
2193 if (di->ac.charger_online) {
2194 di->ac.wd_expired = true;
2195 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2197 if (di->usb.charger_online) {
2198 di->usb.wd_expired = true;
2199 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2206 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2207 * @irq: interrupt number
2208 * @_di: pointer to the ab8500_charger structure
2210 * Returns IRQ status(IRQ_HANDLED)
2212 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2214 struct ab8500_charger *di = _di;
2216 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2217 di->flags.vbus_ovv = true;
2218 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2220 /* Schedule a new HW failure check */
2221 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2227 * ab8500_charger_ac_get_property() - get the ac/mains properties
2228 * @psy: pointer to the power_supply structure
2229 * @psp: pointer to the power_supply_property structure
2230 * @val: pointer to the power_supply_propval union
2232 * This function gets called when an application tries to get the ac/mains
2233 * properties by reading the sysfs files.
2234 * AC/Mains properties are online, present and voltage.
2235 * online: ac/mains charging is in progress or not
2236 * present: presence of the ac/mains
2237 * voltage: AC/Mains voltage
2238 * Returns error code in case of failure else 0(on success)
2240 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2241 enum power_supply_property psp,
2242 union power_supply_propval *val)
2244 struct ab8500_charger *di;
2246 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2249 case POWER_SUPPLY_PROP_HEALTH:
2250 if (di->flags.mainextchnotok)
2251 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2252 else if (di->ac.wd_expired || di->usb.wd_expired)
2253 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2254 else if (di->flags.main_thermal_prot)
2255 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2257 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2259 case POWER_SUPPLY_PROP_ONLINE:
2260 val->intval = di->ac.charger_online;
2262 case POWER_SUPPLY_PROP_PRESENT:
2263 val->intval = di->ac.charger_connected;
2265 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2266 di->ac.charger_voltage = ab8500_charger_get_ac_voltage(di);
2267 val->intval = di->ac.charger_voltage * 1000;
2269 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2271 * This property is used to indicate when CV mode is entered
2272 * for the AC charger
2274 di->ac.cv_active = ab8500_charger_ac_cv(di);
2275 val->intval = di->ac.cv_active;
2277 case POWER_SUPPLY_PROP_CURRENT_NOW:
2278 val->intval = ab8500_charger_get_ac_current(di) * 1000;
2287 * ab8500_charger_usb_get_property() - get the usb properties
2288 * @psy: pointer to the power_supply structure
2289 * @psp: pointer to the power_supply_property structure
2290 * @val: pointer to the power_supply_propval union
2292 * This function gets called when an application tries to get the usb
2293 * properties by reading the sysfs files.
2294 * USB properties are online, present and voltage.
2295 * online: usb charging is in progress or not
2296 * present: presence of the usb
2297 * voltage: vbus voltage
2298 * Returns error code in case of failure else 0(on success)
2300 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2301 enum power_supply_property psp,
2302 union power_supply_propval *val)
2304 struct ab8500_charger *di;
2306 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2309 case POWER_SUPPLY_PROP_HEALTH:
2310 if (di->flags.usbchargernotok)
2311 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2312 else if (di->ac.wd_expired || di->usb.wd_expired)
2313 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2314 else if (di->flags.usb_thermal_prot)
2315 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2316 else if (di->flags.vbus_ovv)
2317 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2319 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2321 case POWER_SUPPLY_PROP_ONLINE:
2322 val->intval = di->usb.charger_online;
2324 case POWER_SUPPLY_PROP_PRESENT:
2325 val->intval = di->usb.charger_connected;
2327 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2328 di->usb.charger_voltage = ab8500_charger_get_vbus_voltage(di);
2329 val->intval = di->usb.charger_voltage * 1000;
2331 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2333 * This property is used to indicate when CV mode is entered
2334 * for the USB charger
2336 di->usb.cv_active = ab8500_charger_usb_cv(di);
2337 val->intval = di->usb.cv_active;
2339 case POWER_SUPPLY_PROP_CURRENT_NOW:
2340 val->intval = ab8500_charger_get_usb_current(di) * 1000;
2342 case POWER_SUPPLY_PROP_CURRENT_AVG:
2344 * This property is used to indicate when VBUS has collapsed
2345 * due to too high output current from the USB charger
2347 if (di->flags.vbus_collapse)
2359 * ab8500_charger_init_hw_registers() - Set up charger related registers
2360 * @di: pointer to the ab8500_charger structure
2362 * Set up charger OVV, watchdog and maximum voltage registers as well as
2363 * charging of the backup battery
2365 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
2369 /* Setup maximum charger current and voltage for ABB cut2.0 */
2370 if (!is_ab8500_1p1_or_earlier(di->parent)) {
2371 ret = abx500_set_register_interruptible(di->dev,
2373 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
2376 "failed to set CH_VOLT_LVL_MAX_REG\n");
2380 ret = abx500_set_register_interruptible(di->dev,
2382 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
2385 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2390 /* VBUS OVV set to 6.3V and enable automatic current limitiation */
2391 ret = abx500_set_register_interruptible(di->dev,
2393 AB8500_USBCH_CTRL2_REG,
2394 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
2396 dev_err(di->dev, "failed to set VBUS OVV\n");
2400 /* Enable main watchdog in OTP */
2401 ret = abx500_set_register_interruptible(di->dev,
2402 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
2404 dev_err(di->dev, "failed to enable main WD in OTP\n");
2408 /* Enable main watchdog */
2409 ret = abx500_set_register_interruptible(di->dev,
2410 AB8500_SYS_CTRL2_BLOCK,
2411 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
2413 dev_err(di->dev, "faile to enable main watchdog\n");
2418 * Due to internal synchronisation, Enable and Kick watchdog bits
2419 * cannot be enabled in a single write.
2420 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
2421 * between writing Enable then Kick bits.
2425 /* Kick main watchdog */
2426 ret = abx500_set_register_interruptible(di->dev,
2427 AB8500_SYS_CTRL2_BLOCK,
2428 AB8500_MAIN_WDOG_CTRL_REG,
2429 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
2431 dev_err(di->dev, "failed to kick main watchdog\n");
2435 /* Disable main watchdog */
2436 ret = abx500_set_register_interruptible(di->dev,
2437 AB8500_SYS_CTRL2_BLOCK,
2438 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
2440 dev_err(di->dev, "failed to disable main watchdog\n");
2444 /* Set watchdog timeout */
2445 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2446 AB8500_CH_WD_TIMER_REG, WD_TIMER);
2448 dev_err(di->dev, "failed to set charger watchdog timeout\n");
2452 /* Backup battery voltage and current */
2453 ret = abx500_set_register_interruptible(di->dev,
2455 AB8500_RTC_BACKUP_CHG_REG,
2456 di->bm->bkup_bat_v |
2457 di->bm->bkup_bat_i);
2459 dev_err(di->dev, "failed to setup backup battery charging\n");
2463 /* Enable backup battery charging */
2464 abx500_mask_and_set_register_interruptible(di->dev,
2465 AB8500_RTC, AB8500_RTC_CTRL_REG,
2466 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
2468 dev_err(di->dev, "%s mask and set failed\n", __func__);
2475 * ab8500 charger driver interrupts and their respective isr
2477 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
2478 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
2479 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
2480 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
2481 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
2482 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
2483 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
2484 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
2485 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
2486 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
2487 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
2488 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
2489 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
2490 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
2493 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
2494 unsigned long event, void *power)
2496 struct ab8500_charger *di =
2497 container_of(nb, struct ab8500_charger, nb);
2498 enum ab8500_usb_state bm_usb_state;
2499 unsigned mA = *((unsigned *)power);
2501 if (event != USB_EVENT_VBUS) {
2502 dev_dbg(di->dev, "not a standard host, returning\n");
2506 /* TODO: State is fabricate here. See if charger really needs USB
2507 * state or if mA is enough
2509 if ((di->usb_state.usb_current == 2) && (mA > 2))
2510 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
2512 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
2514 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
2515 else if (mA >= 8) /* 8, 100, 500 */
2516 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
2517 else /* Should never occur */
2518 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
2520 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
2521 __func__, bm_usb_state, mA);
2523 spin_lock(&di->usb_state.usb_lock);
2524 di->usb_state.usb_changed = true;
2525 spin_unlock(&di->usb_state.usb_lock);
2527 di->usb_state.state = bm_usb_state;
2528 di->usb_state.usb_current = mA;
2530 queue_work(di->charger_wq, &di->usb_state_changed_work);
2535 #if defined(CONFIG_PM)
2536 static int ab8500_charger_resume(struct platform_device *pdev)
2539 struct ab8500_charger *di = platform_get_drvdata(pdev);
2542 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2543 * logic. That means we have to continously kick the charger
2544 * watchdog even when no charger is connected. This is only
2545 * valid once the AC charger has been enabled. This is
2546 * a bug that is not handled by the algorithm and the
2547 * watchdog have to be kicked by the charger driver
2548 * when the AC charger is disabled
2550 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
2551 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2552 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2554 dev_err(di->dev, "Failed to kick WD!\n");
2556 /* If not already pending start a new timer */
2557 if (!delayed_work_pending(
2558 &di->kick_wd_work)) {
2559 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
2560 round_jiffies(WD_KICK_INTERVAL));
2564 /* If we still have a HW failure, schedule a new check */
2565 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2566 queue_delayed_work(di->charger_wq,
2567 &di->check_hw_failure_work, 0);
2573 static int ab8500_charger_suspend(struct platform_device *pdev,
2576 struct ab8500_charger *di = platform_get_drvdata(pdev);
2578 /* Cancel any pending HW failure check */
2579 if (delayed_work_pending(&di->check_hw_failure_work))
2580 cancel_delayed_work(&di->check_hw_failure_work);
2585 #define ab8500_charger_suspend NULL
2586 #define ab8500_charger_resume NULL
2589 static int ab8500_charger_remove(struct platform_device *pdev)
2591 struct ab8500_charger *di = platform_get_drvdata(pdev);
2594 /* Disable AC charging */
2595 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
2597 /* Disable USB charging */
2598 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
2600 /* Disable interrupts */
2601 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2602 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2606 /* Backup battery voltage and current disable */
2607 ret = abx500_mask_and_set_register_interruptible(di->dev,
2608 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
2610 dev_err(di->dev, "%s mask and set failed\n", __func__);
2612 usb_unregister_notifier(di->usb_phy, &di->nb);
2613 usb_put_phy(di->usb_phy);
2615 /* Delete the work queue */
2616 destroy_workqueue(di->charger_wq);
2618 flush_scheduled_work();
2619 power_supply_unregister(&di->usb_chg.psy);
2620 power_supply_unregister(&di->ac_chg.psy);
2621 platform_set_drvdata(pdev, NULL);
2626 static char *supply_interface[] = {
2632 static int ab8500_charger_probe(struct platform_device *pdev)
2634 struct device_node *np = pdev->dev.of_node;
2635 struct abx500_bm_data *plat = pdev->dev.platform_data;
2636 struct ab8500_charger *di;
2637 int irq, i, charger_status, ret = 0;
2639 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
2641 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
2646 dev_err(&pdev->dev, "no battery management data supplied\n");
2652 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
2654 dev_err(&pdev->dev, "failed to get battery information\n");
2657 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
2659 di->autopower_cfg = false;
2661 /* get parent data */
2662 di->dev = &pdev->dev;
2663 di->parent = dev_get_drvdata(pdev->dev.parent);
2664 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2666 /* initialize lock */
2667 spin_lock_init(&di->usb_state.usb_lock);
2669 di->autopower = false;
2672 /* power_supply base class */
2673 di->ac_chg.psy.name = "ab8500_ac";
2674 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
2675 di->ac_chg.psy.properties = ab8500_charger_ac_props;
2676 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
2677 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
2678 di->ac_chg.psy.supplied_to = supply_interface;
2679 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
2680 /* ux500_charger sub-class */
2681 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
2682 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2683 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2684 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
2685 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2686 di->ac_chg.max_out_curr = ab8500_charger_current_map[
2687 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2690 /* power_supply base class */
2691 di->usb_chg.psy.name = "ab8500_usb";
2692 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
2693 di->usb_chg.psy.properties = ab8500_charger_usb_props;
2694 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
2695 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
2696 di->usb_chg.psy.supplied_to = supply_interface;
2697 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
2698 /* ux500_charger sub-class */
2699 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
2700 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2701 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2702 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
2703 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2704 di->usb_chg.max_out_curr = ab8500_charger_current_map[
2705 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2708 /* Create a work queue for the charger */
2710 create_singlethread_workqueue("ab8500_charger_wq");
2711 if (di->charger_wq == NULL) {
2712 dev_err(di->dev, "failed to create work queue\n");
2716 /* Init work for HW failure check */
2717 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
2718 ab8500_charger_check_hw_failure_work);
2719 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
2720 ab8500_charger_check_usbchargernotok_work);
2723 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2724 * logic. That means we have to continously kick the charger
2725 * watchdog even when no charger is connected. This is only
2726 * valid once the AC charger has been enabled. This is
2727 * a bug that is not handled by the algorithm and the
2728 * watchdog have to be kicked by the charger driver
2729 * when the AC charger is disabled
2731 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
2732 ab8500_charger_kick_watchdog_work);
2734 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
2735 ab8500_charger_check_vbat_work);
2737 /* Init work for charger detection */
2738 INIT_WORK(&di->usb_link_status_work,
2739 ab8500_charger_usb_link_status_work);
2740 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
2741 INIT_WORK(&di->detect_usb_type_work,
2742 ab8500_charger_detect_usb_type_work);
2744 INIT_WORK(&di->usb_state_changed_work,
2745 ab8500_charger_usb_state_changed_work);
2747 /* Init work for checking HW status */
2748 INIT_WORK(&di->check_main_thermal_prot_work,
2749 ab8500_charger_check_main_thermal_prot_work);
2750 INIT_WORK(&di->check_usb_thermal_prot_work,
2751 ab8500_charger_check_usb_thermal_prot_work);
2754 * VDD ADC supply needs to be enabled from this driver when there
2755 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
2756 * interrupts during charging
2758 di->regu = devm_regulator_get(di->dev, "vddadc");
2759 if (IS_ERR(di->regu)) {
2760 ret = PTR_ERR(di->regu);
2761 dev_err(di->dev, "failed to get vddadc regulator\n");
2762 goto free_charger_wq;
2766 /* Initialize OVV, and other registers */
2767 ret = ab8500_charger_init_hw_registers(di);
2769 dev_err(di->dev, "failed to initialize ABB registers\n");
2770 goto free_charger_wq;
2773 /* Register AC charger class */
2774 ret = power_supply_register(di->dev, &di->ac_chg.psy);
2776 dev_err(di->dev, "failed to register AC charger\n");
2777 goto free_charger_wq;
2780 /* Register USB charger class */
2781 ret = power_supply_register(di->dev, &di->usb_chg.psy);
2783 dev_err(di->dev, "failed to register USB charger\n");
2787 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
2788 if (IS_ERR_OR_NULL(di->usb_phy)) {
2789 dev_err(di->dev, "failed to get usb transceiver\n");
2793 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
2794 ret = usb_register_notifier(di->usb_phy, &di->nb);
2796 dev_err(di->dev, "failed to register usb notifier\n");
2800 /* Identify the connected charger types during startup */
2801 charger_status = ab8500_charger_detect_chargers(di);
2802 if (charger_status & AC_PW_CONN) {
2803 di->ac.charger_connected = 1;
2805 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2806 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2809 if (charger_status & USB_PW_CONN) {
2810 dev_dbg(di->dev, "VBUS Detect during startup\n");
2811 di->vbus_detected = true;
2812 di->vbus_detected_start = true;
2813 queue_work(di->charger_wq,
2814 &di->detect_usb_type_work);
2817 /* Register interrupts */
2818 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2819 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2820 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
2821 IRQF_SHARED | IRQF_NO_SUSPEND,
2822 ab8500_charger_irq[i].name, di);
2825 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
2826 , ab8500_charger_irq[i].name, irq, ret);
2829 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
2830 ab8500_charger_irq[i].name, irq, ret);
2833 platform_set_drvdata(pdev, di);
2838 usb_unregister_notifier(di->usb_phy, &di->nb);
2840 /* We also have to free all successfully registered irqs */
2841 for (i = i - 1; i >= 0; i--) {
2842 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2846 usb_put_phy(di->usb_phy);
2848 power_supply_unregister(&di->usb_chg.psy);
2850 power_supply_unregister(&di->ac_chg.psy);
2852 destroy_workqueue(di->charger_wq);
2856 static const struct of_device_id ab8500_charger_match[] = {
2857 { .compatible = "stericsson,ab8500-charger", },
2861 static struct platform_driver ab8500_charger_driver = {
2862 .probe = ab8500_charger_probe,
2863 .remove = ab8500_charger_remove,
2864 .suspend = ab8500_charger_suspend,
2865 .resume = ab8500_charger_resume,
2867 .name = "ab8500-charger",
2868 .owner = THIS_MODULE,
2869 .of_match_table = ab8500_charger_match,
2873 static int __init ab8500_charger_init(void)
2875 return platform_driver_register(&ab8500_charger_driver);
2878 static void __exit ab8500_charger_exit(void)
2880 platform_driver_unregister(&ab8500_charger_driver);
2883 subsys_initcall_sync(ab8500_charger_init);
2884 module_exit(ab8500_charger_exit);
2886 MODULE_LICENSE("GPL v2");
2887 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
2888 MODULE_ALIAS("platform:ab8500-charger");
2889 MODULE_DESCRIPTION("AB8500 charger management driver");