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>
26 #include <linux/mfd/abx500/ab8500.h>
27 #include <linux/mfd/abx500.h>
28 #include <linux/mfd/abx500/ab8500-bm.h>
29 #include <linux/mfd/abx500/ab8500-gpadc.h>
30 #include <linux/mfd/abx500/ux500_chargalg.h>
31 #include <linux/usb/otg.h>
33 /* Charger constants */
38 #define MAIN_WDOG_ENA 0x01
39 #define MAIN_WDOG_KICK 0x02
40 #define MAIN_WDOG_DIS 0x00
41 #define CHARG_WD_KICK 0x01
42 #define MAIN_CH_ENA 0x01
43 #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
44 #define USB_CH_ENA 0x01
45 #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
46 #define MAIN_CH_DET 0x01
47 #define MAIN_CH_CV_ON 0x04
48 #define USB_CH_CV_ON 0x08
49 #define VBUS_DET_DBNC100 0x02
50 #define VBUS_DET_DBNC1 0x01
51 #define OTP_ENABLE_WD 0x01
53 #define MAIN_CH_INPUT_CURR_SHIFT 4
54 #define VBUS_IN_CURR_LIM_SHIFT 4
56 #define LED_INDICATOR_PWM_ENA 0x01
57 #define LED_INDICATOR_PWM_DIS 0x00
58 #define LED_IND_CUR_5MA 0x04
59 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
61 /* HW failure constants */
62 #define MAIN_CH_TH_PROT 0x02
63 #define VBUS_CH_NOK 0x08
64 #define USB_CH_TH_PROT 0x02
65 #define VBUS_OVV_TH 0x01
66 #define MAIN_CH_NOK 0x01
69 /* UsbLineStatus register bit masks */
70 #define AB8500_USB_LINK_STATUS 0x78
71 #define AB8500_STD_HOST_SUSP 0x18
73 /* Watchdog timeout constant */
74 #define WD_TIMER 0x30 /* 4min */
75 #define WD_KICK_INTERVAL (60 * HZ)
77 /* Lowest charger voltage is 3.39V -> 0x4E */
78 #define LOW_VOLT_REG 0x4E
80 /* UsbLineStatus register - usb types */
81 enum ab8500_charger_link_status {
82 USB_STAT_NOT_CONFIGURED,
84 USB_STAT_STD_HOST_C_NS,
85 USB_STAT_STD_HOST_C_S,
88 USB_STAT_HOST_CHG_HS_CHIRP,
89 USB_STAT_DEDICATED_CHG,
92 USB_STAT_ACA_RID_C_NM,
93 USB_STAT_ACA_RID_C_HS,
94 USB_STAT_ACA_RID_C_HS_CHIRP,
97 USB_STAT_NOT_VALID_LINK,
100 enum ab8500_usb_state {
101 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
102 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
103 AB8500_BM_USB_STATE_CONFIGURED,
104 AB8500_BM_USB_STATE_SUSPEND,
105 AB8500_BM_USB_STATE_RESUME,
106 AB8500_BM_USB_STATE_MAX,
109 /* VBUS input current limits supported in AB8500 in mA */
110 #define USB_CH_IP_CUR_LVL_0P05 50
111 #define USB_CH_IP_CUR_LVL_0P09 98
112 #define USB_CH_IP_CUR_LVL_0P19 193
113 #define USB_CH_IP_CUR_LVL_0P29 290
114 #define USB_CH_IP_CUR_LVL_0P38 380
115 #define USB_CH_IP_CUR_LVL_0P45 450
116 #define USB_CH_IP_CUR_LVL_0P5 500
117 #define USB_CH_IP_CUR_LVL_0P6 600
118 #define USB_CH_IP_CUR_LVL_0P7 700
119 #define USB_CH_IP_CUR_LVL_0P8 800
120 #define USB_CH_IP_CUR_LVL_0P9 900
121 #define USB_CH_IP_CUR_LVL_1P0 1000
122 #define USB_CH_IP_CUR_LVL_1P1 1100
123 #define USB_CH_IP_CUR_LVL_1P3 1300
124 #define USB_CH_IP_CUR_LVL_1P4 1400
125 #define USB_CH_IP_CUR_LVL_1P5 1500
127 #define VBAT_TRESH_IP_CUR_RED 3800
129 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
130 struct ab8500_charger, usb_chg)
131 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
132 struct ab8500_charger, ac_chg)
135 * struct ab8500_charger_interrupts - ab8500 interupts
136 * @name: name of the interrupt
137 * @isr function pointer to the isr
139 struct ab8500_charger_interrupts {
141 irqreturn_t (*isr)(int irq, void *data);
144 struct ab8500_charger_info {
145 int charger_connected;
152 struct ab8500_charger_event_flags {
154 bool main_thermal_prot;
155 bool usb_thermal_prot;
157 bool usbchargernotok;
162 struct ab8500_charger_usb_state {
165 enum ab8500_usb_state state;
170 * struct ab8500_charger - ab8500 Charger device information
171 * @dev: Pointer to the structure device
172 * @max_usb_in_curr: Max USB charger input current
173 * @vbus_detected: VBUS detected
174 * @vbus_detected_start:
175 * VBUS detected during startup
176 * @ac_conn: This will be true when the AC charger has been plugged
177 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
179 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
181 * @vbat Battery voltage
182 * @old_vbat Previously measured battery voltage
183 * @autopower Indicate if we should have automatic pwron after pwrloss
184 * @parent: Pointer to the struct ab8500
185 * @gpadc: Pointer to the struct gpadc
186 * @pdata: Pointer to the abx500_charger platform data
187 * @bat: Pointer to the abx500_bm platform data
188 * @flags: Structure for information about events triggered
189 * @usb_state: Structure for usb stack information
190 * @ac_chg: AC charger power supply
191 * @usb_chg: USB charger power supply
192 * @ac: Structure that holds the AC charger properties
193 * @usb: Structure that holds the USB charger properties
194 * @regu: Pointer to the struct regulator
195 * @charger_wq: Work queue for the IRQs and checking HW state
196 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
197 * @check_hw_failure_work: Work for checking HW state
198 * @check_usbchgnotok_work: Work for checking USB charger not ok status
199 * @kick_wd_work: Work for kicking the charger watchdog in case
200 * of ABB rev 1.* due to the watchog logic bug
201 * @ac_work: Work for checking AC charger connection
202 * @detect_usb_type_work: Work for detecting the USB type connected
203 * @usb_link_status_work: Work for checking the new USB link status
204 * @usb_state_changed_work: Work for checking USB state
205 * @check_main_thermal_prot_work:
206 * Work for checking Main thermal status
207 * @check_usb_thermal_prot_work:
208 * Work for checking USB thermal status
210 struct ab8500_charger {
214 bool vbus_detected_start;
221 struct ab8500 *parent;
222 struct ab8500_gpadc *gpadc;
223 struct abx500_charger_platform_data *pdata;
224 struct abx500_bm_data *bat;
225 struct ab8500_charger_event_flags flags;
226 struct ab8500_charger_usb_state usb_state;
227 struct ux500_charger ac_chg;
228 struct ux500_charger usb_chg;
229 struct ab8500_charger_info ac;
230 struct ab8500_charger_info usb;
231 struct regulator *regu;
232 struct workqueue_struct *charger_wq;
233 struct delayed_work check_vbat_work;
234 struct delayed_work check_hw_failure_work;
235 struct delayed_work check_usbchgnotok_work;
236 struct delayed_work kick_wd_work;
237 struct work_struct ac_work;
238 struct work_struct detect_usb_type_work;
239 struct work_struct usb_link_status_work;
240 struct work_struct usb_state_changed_work;
241 struct work_struct check_main_thermal_prot_work;
242 struct work_struct check_usb_thermal_prot_work;
243 struct usb_phy *usb_phy;
244 struct notifier_block nb;
248 static enum power_supply_property ab8500_charger_ac_props[] = {
249 POWER_SUPPLY_PROP_HEALTH,
250 POWER_SUPPLY_PROP_PRESENT,
251 POWER_SUPPLY_PROP_ONLINE,
252 POWER_SUPPLY_PROP_VOLTAGE_NOW,
253 POWER_SUPPLY_PROP_VOLTAGE_AVG,
254 POWER_SUPPLY_PROP_CURRENT_NOW,
258 static enum power_supply_property ab8500_charger_usb_props[] = {
259 POWER_SUPPLY_PROP_HEALTH,
260 POWER_SUPPLY_PROP_CURRENT_AVG,
261 POWER_SUPPLY_PROP_PRESENT,
262 POWER_SUPPLY_PROP_ONLINE,
263 POWER_SUPPLY_PROP_VOLTAGE_NOW,
264 POWER_SUPPLY_PROP_VOLTAGE_AVG,
265 POWER_SUPPLY_PROP_CURRENT_NOW,
269 * ab8500_power_loss_handling - set how we handle powerloss.
270 * @di: pointer to the ab8500_charger structure
272 * Magic nummbers are from STE HW department.
274 static void ab8500_power_loss_handling(struct ab8500_charger *di)
279 dev_dbg(di->dev, "Autopower : %d\n", di->autopower);
281 /* read the autopower register */
282 ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, ®);
284 dev_err(di->dev, "%d write failed\n", __LINE__);
288 /* enable the OPT emulation registers */
289 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
291 dev_err(di->dev, "%d write failed\n", __LINE__);
300 /* write back the changed value to autopower reg */
301 ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg);
303 dev_err(di->dev, "%d write failed\n", __LINE__);
307 /* disable the set OTP registers again */
308 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
310 dev_err(di->dev, "%d write failed\n", __LINE__);
316 * ab8500_power_supply_changed - a wrapper with local extentions for
317 * power_supply_changed
318 * @di: pointer to the ab8500_charger structure
319 * @psy: pointer to power_supply_that have changed.
322 static void ab8500_power_supply_changed(struct ab8500_charger *di,
323 struct power_supply *psy)
325 if (di->pdata->autopower_cfg) {
326 if (!di->usb.charger_connected &&
327 !di->ac.charger_connected &&
329 di->autopower = false;
330 ab8500_power_loss_handling(di);
331 } else if (!di->autopower &&
332 (di->ac.charger_connected ||
333 di->usb.charger_connected)) {
334 di->autopower = true;
335 ab8500_power_loss_handling(di);
338 power_supply_changed(psy);
341 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
344 if (connected != di->usb.charger_connected) {
345 dev_dbg(di->dev, "USB connected:%i\n", connected);
346 di->usb.charger_connected = connected;
347 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
352 * ab8500_charger_get_ac_voltage() - get ac charger voltage
353 * @di: pointer to the ab8500_charger structure
355 * Returns ac charger voltage (on success)
357 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
361 /* Only measure voltage if the charger is connected */
362 if (di->ac.charger_connected) {
363 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
365 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
373 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
374 * @di: pointer to the ab8500_charger structure
376 * Returns ac charger CV mode (on success) else error code
378 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
383 /* Only check CV mode if the charger is online */
384 if (di->ac.charger_online) {
385 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
386 AB8500_CH_STATUS1_REG, &val);
388 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
392 if (val & MAIN_CH_CV_ON)
402 * ab8500_charger_get_vbus_voltage() - get vbus voltage
403 * @di: pointer to the ab8500_charger structure
405 * This function returns the vbus voltage.
406 * Returns vbus voltage (on success)
408 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
412 /* Only measure voltage if the charger is connected */
413 if (di->usb.charger_connected) {
414 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
416 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
424 * ab8500_charger_get_usb_current() - get usb charger current
425 * @di: pointer to the ab8500_charger structure
427 * This function returns the usb charger current.
428 * Returns usb current (on success) and error code on failure
430 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
434 /* Only measure current if the charger is online */
435 if (di->usb.charger_online) {
436 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
438 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
446 * ab8500_charger_get_ac_current() - get ac charger current
447 * @di: pointer to the ab8500_charger structure
449 * This function returns the ac charger current.
450 * Returns ac current (on success) and error code on failure.
452 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
456 /* Only measure current if the charger is online */
457 if (di->ac.charger_online) {
458 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
460 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
468 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
469 * @di: pointer to the ab8500_charger structure
471 * Returns ac charger CV mode (on success) else error code
473 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
478 /* Only check CV mode if the charger is online */
479 if (di->usb.charger_online) {
480 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
481 AB8500_CH_USBCH_STAT1_REG, &val);
483 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
487 if (val & USB_CH_CV_ON)
499 * ab8500_charger_detect_chargers() - Detect the connected chargers
500 * @di: pointer to the ab8500_charger structure
502 * Returns the type of charger connected.
503 * For USB it will not mean we can actually charge from it
504 * but that there is a USB cable connected that we have to
505 * identify. This is used during startup when we don't get
506 * interrupts of the charger detection
508 * Returns an integer value, that means,
509 * NO_PW_CONN no power supply is connected
510 * AC_PW_CONN if the AC power supply is connected
511 * USB_PW_CONN if the USB power supply is connected
512 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
514 static int ab8500_charger_detect_chargers(struct ab8500_charger *di)
516 int result = NO_PW_CONN;
520 /* Check for AC charger */
521 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
522 AB8500_CH_STATUS1_REG, &val);
524 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
528 if (val & MAIN_CH_DET)
531 /* Check for USB charger */
532 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
533 AB8500_CH_USBCH_STAT1_REG, &val);
535 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
539 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
540 result |= USB_PW_CONN;
546 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
547 * @di: pointer to the ab8500_charger structure
548 * @link_status: the identified USB type
550 * Get the maximum current that is allowed to be drawn from the host
551 * based on the USB type.
552 * Returns error code in case of failure else 0 on success
554 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
555 enum ab8500_charger_link_status link_status)
559 switch (link_status) {
560 case USB_STAT_STD_HOST_NC:
561 case USB_STAT_STD_HOST_C_NS:
562 case USB_STAT_STD_HOST_C_S:
563 dev_dbg(di->dev, "USB Type - Standard host is "
564 "detected through USB driver\n");
565 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
567 case USB_STAT_HOST_CHG_HS_CHIRP:
568 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
570 case USB_STAT_HOST_CHG_HS:
571 case USB_STAT_ACA_RID_C_HS:
572 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
574 case USB_STAT_ACA_RID_A:
576 * Dedicated charger level minus maximum current accessory
577 * can consume (300mA). Closest level is 1100mA
579 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1;
581 case USB_STAT_ACA_RID_B:
583 * Dedicated charger level minus 120mA (20mA for ACA and
584 * 100mA for potential accessory). Closest level is 1300mA
586 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
588 case USB_STAT_DEDICATED_CHG:
589 case USB_STAT_HOST_CHG_NM:
590 case USB_STAT_ACA_RID_C_HS_CHIRP:
591 case USB_STAT_ACA_RID_C_NM:
592 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
594 case USB_STAT_RESERVED:
596 * This state is used to indicate that VBUS has dropped below
597 * the detection level 4 times in a row. This is due to the
598 * charger output current is set to high making the charger
599 * voltage collapse. This have to be propagated through to
600 * chargalg. This is done using the property
601 * POWER_SUPPLY_PROP_CURRENT_AVG = 1
603 di->flags.vbus_collapse = true;
604 dev_dbg(di->dev, "USB Type - USB_STAT_RESERVED "
605 "VBUS has collapsed\n");
608 case USB_STAT_HM_IDGND:
609 case USB_STAT_NOT_CONFIGURED:
610 case USB_STAT_NOT_VALID_LINK:
611 dev_err(di->dev, "USB Type - Charging not allowed\n");
612 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
616 dev_err(di->dev, "USB Type - Unknown\n");
617 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
622 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
623 link_status, di->max_usb_in_curr);
629 * ab8500_charger_read_usb_type() - read the type of usb connected
630 * @di: pointer to the ab8500_charger structure
632 * Detect the type of the plugged USB
633 * Returns error code in case of failure else 0 on success
635 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
640 ret = abx500_get_register_interruptible(di->dev,
641 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
643 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
646 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
647 AB8500_USB_LINE_STAT_REG, &val);
649 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
653 /* get the USB type */
654 val = (val & AB8500_USB_LINK_STATUS) >> 3;
655 ret = ab8500_charger_max_usb_curr(di,
656 (enum ab8500_charger_link_status) val);
662 * ab8500_charger_detect_usb_type() - get 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_detect_usb_type(struct ab8500_charger *di)
674 * On getting the VBUS rising edge detect interrupt there
675 * is a 250ms delay after which the register UsbLineStatus
676 * is filled with valid data.
678 for (i = 0; i < 10; i++) {
680 ret = abx500_get_register_interruptible(di->dev,
681 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
684 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
687 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
688 AB8500_USB_LINE_STAT_REG, &val);
690 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
694 * Until the IT source register is read the UsbLineStatus
695 * register is not updated, hence doing the same
699 /* get the USB type */
700 val = (val & AB8500_USB_LINK_STATUS) >> 3;
704 ret = ab8500_charger_max_usb_curr(di,
705 (enum ab8500_charger_link_status) val);
711 * This array maps the raw hex value to charger voltage used by the AB8500
712 * Values taken from the UM0836
714 static int ab8500_charger_voltage_map[] = {
796 * This array maps the raw hex value to charger current used by the AB8500
797 * Values taken from the UM0836
799 static int ab8500_charger_current_map[] = {
818 * This array maps the raw hex value to VBUS input current used by the AB8500
819 * Values taken from the UM0836
821 static int ab8500_charger_vbus_in_curr_map[] = {
822 USB_CH_IP_CUR_LVL_0P05,
823 USB_CH_IP_CUR_LVL_0P09,
824 USB_CH_IP_CUR_LVL_0P19,
825 USB_CH_IP_CUR_LVL_0P29,
826 USB_CH_IP_CUR_LVL_0P38,
827 USB_CH_IP_CUR_LVL_0P45,
828 USB_CH_IP_CUR_LVL_0P5,
829 USB_CH_IP_CUR_LVL_0P6,
830 USB_CH_IP_CUR_LVL_0P7,
831 USB_CH_IP_CUR_LVL_0P8,
832 USB_CH_IP_CUR_LVL_0P9,
833 USB_CH_IP_CUR_LVL_1P0,
834 USB_CH_IP_CUR_LVL_1P1,
835 USB_CH_IP_CUR_LVL_1P3,
836 USB_CH_IP_CUR_LVL_1P4,
837 USB_CH_IP_CUR_LVL_1P5,
840 static int ab8500_voltage_to_regval(int voltage)
844 /* Special case for voltage below 3.5V */
845 if (voltage < ab8500_charger_voltage_map[0])
848 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
849 if (voltage < ab8500_charger_voltage_map[i])
853 /* If not last element, return error */
854 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
855 if (voltage == ab8500_charger_voltage_map[i])
861 static int ab8500_current_to_regval(int curr)
865 if (curr < ab8500_charger_current_map[0])
868 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
869 if (curr < ab8500_charger_current_map[i])
873 /* If not last element, return error */
874 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
875 if (curr == ab8500_charger_current_map[i])
881 static int ab8500_vbus_in_curr_to_regval(int curr)
885 if (curr < ab8500_charger_vbus_in_curr_map[0])
888 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
889 if (curr < ab8500_charger_vbus_in_curr_map[i])
893 /* If not last element, return error */
894 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
895 if (curr == ab8500_charger_vbus_in_curr_map[i])
902 * ab8500_charger_get_usb_cur() - get usb current
903 * @di: pointer to the ab8500_charger structre
905 * The usb stack provides the maximum current that can be drawn from
906 * the standard usb host. This will be in mA.
907 * This function converts current in mA to a value that can be written
908 * to the register. Returns -1 if charging is not allowed
910 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
912 switch (di->usb_state.usb_current) {
914 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
917 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
920 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
923 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
926 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
929 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
937 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
938 * @di: pointer to the ab8500_charger structure
939 * @ich_in: charger input current limit
941 * Sets the current that can be drawn from the USB host
942 * Returns error code in case of failure else 0(on success)
944 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
948 int input_curr_index;
951 /* We should always use to lowest current limit */
952 min_value = min(di->bat->chg_params->usb_curr_max, ich_in);
956 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
957 min_value = USB_CH_IP_CUR_LVL_0P05;
960 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
961 min_value = USB_CH_IP_CUR_LVL_0P45;
967 input_curr_index = ab8500_vbus_in_curr_to_regval(min_value);
968 if (input_curr_index < 0) {
969 dev_err(di->dev, "VBUS input current limit too high\n");
973 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
974 AB8500_USBCH_IPT_CRNTLVL_REG,
975 input_curr_index << VBUS_IN_CURR_LIM_SHIFT);
977 dev_err(di->dev, "%s write failed\n", __func__);
983 * ab8500_charger_led_en() - turn on/off chargign led
984 * @di: pointer to the ab8500_charger structure
985 * @on: flag to turn on/off the chargign led
987 * Power ON/OFF charging LED indication
988 * Returns error code in case of failure else 0(on success)
990 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
995 /* Power ON charging LED indicator, set LED current to 5mA */
996 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
997 AB8500_LED_INDICATOR_PWM_CTRL,
998 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1000 dev_err(di->dev, "Power ON LED failed\n");
1003 /* LED indicator PWM duty cycle 252/256 */
1004 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1005 AB8500_LED_INDICATOR_PWM_DUTY,
1006 LED_INDICATOR_PWM_DUTY_252_256);
1008 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1012 /* Power off charging LED indicator */
1013 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1014 AB8500_LED_INDICATOR_PWM_CTRL,
1015 LED_INDICATOR_PWM_DIS);
1017 dev_err(di->dev, "Power-off LED failed\n");
1026 * ab8500_charger_ac_en() - enable or disable ac charging
1027 * @di: pointer to the ab8500_charger structure
1028 * @enable: enable/disable flag
1029 * @vset: charging voltage
1030 * @iset: charging current
1032 * Enable/Disable AC/Mains charging and turns on/off the charging led
1035 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1036 int enable, int vset, int iset)
1041 int input_curr_index;
1044 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1047 /* Check if AC is connected */
1048 if (!di->ac.charger_connected) {
1049 dev_err(di->dev, "AC charger not connected\n");
1053 /* Enable AC charging */
1054 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1057 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1058 * will be triggered everytime we enable the VDD ADC supply.
1059 * This will turn off charging for a short while.
1060 * It can be avoided by having the supply on when
1061 * there is a charger enabled. Normally the VDD ADC supply
1062 * is enabled everytime a GPADC conversion is triggered. We will
1063 * force it to be enabled from this driver to have
1064 * the GPADC module independant of the AB8500 chargers
1066 if (!di->vddadc_en_ac) {
1067 regulator_enable(di->regu);
1068 di->vddadc_en_ac = true;
1071 /* Check if the requested voltage or current is valid */
1072 volt_index = ab8500_voltage_to_regval(vset);
1073 curr_index = ab8500_current_to_regval(iset);
1074 input_curr_index = ab8500_current_to_regval(
1075 di->bat->chg_params->ac_curr_max);
1076 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1078 "Charger voltage or current too high, "
1079 "charging not started\n");
1083 /* ChVoltLevel: maximum battery charging voltage */
1084 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1085 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1087 dev_err(di->dev, "%s write failed\n", __func__);
1090 /* MainChInputCurr: current that can be drawn from the charger*/
1091 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1092 AB8500_MCH_IPT_CURLVL_REG,
1093 input_curr_index << MAIN_CH_INPUT_CURR_SHIFT);
1095 dev_err(di->dev, "%s write failed\n", __func__);
1098 /* ChOutputCurentLevel: protected output current */
1099 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1100 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index);
1102 dev_err(di->dev, "%s write failed\n", __func__);
1106 /* Check if VBAT overshoot control should be enabled */
1107 if (!di->bat->enable_overshoot)
1108 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1110 /* Enable Main Charger */
1111 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1112 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1114 dev_err(di->dev, "%s write failed\n", __func__);
1118 /* Power on charging LED indication */
1119 ret = ab8500_charger_led_en(di, true);
1121 dev_err(di->dev, "failed to enable LED\n");
1123 di->ac.charger_online = 1;
1125 /* Disable AC charging */
1126 if (is_ab8500_1p1_or_earlier(di->parent)) {
1128 * For ABB revision 1.0 and 1.1 there is a bug in the
1129 * watchdog logic. That means we have to continously
1130 * kick the charger watchdog even when no charger is
1131 * connected. This is only valid once the AC charger
1132 * has been enabled. This is a bug that is not handled
1133 * by the algorithm and the watchdog have to be kicked
1134 * by the charger driver when the AC charger
1138 queue_delayed_work(di->charger_wq,
1140 round_jiffies(WD_KICK_INTERVAL));
1144 * We can't turn off charging completely
1145 * due to a bug in AB8500 cut1.
1146 * If we do, charging will not start again.
1147 * That is why we set the lowest voltage
1148 * and current possible
1150 ret = abx500_set_register_interruptible(di->dev,
1152 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1155 "%s write failed\n", __func__);
1159 ret = abx500_set_register_interruptible(di->dev,
1161 AB8500_CH_OPT_CRNTLVL_REG, CH_OP_CUR_LVL_0P1);
1164 "%s write failed\n", __func__);
1168 ret = abx500_set_register_interruptible(di->dev,
1170 AB8500_MCH_CTRL1, 0);
1173 "%s write failed\n", __func__);
1178 ret = ab8500_charger_led_en(di, false);
1180 dev_err(di->dev, "failed to disable LED\n");
1182 di->ac.charger_online = 0;
1183 di->ac.wd_expired = false;
1185 /* Disable regulator if enabled */
1186 if (di->vddadc_en_ac) {
1187 regulator_disable(di->regu);
1188 di->vddadc_en_ac = false;
1191 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1193 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1199 * ab8500_charger_usb_en() - enable usb charging
1200 * @di: pointer to the ab8500_charger structure
1201 * @enable: enable/disable flag
1202 * @vset: charging voltage
1203 * @ich_out: charger output current
1205 * Enable/Disable USB charging and turns on/off the charging led respectively.
1206 * Returns error code in case of failure else 0(on success)
1208 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1209 int enable, int vset, int ich_out)
1216 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1219 /* Check if USB is connected */
1220 if (!di->usb.charger_connected) {
1221 dev_err(di->dev, "USB charger not connected\n");
1226 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1227 * will be triggered everytime we enable the VDD ADC supply.
1228 * This will turn off charging for a short while.
1229 * It can be avoided by having the supply on when
1230 * there is a charger enabled. Normally the VDD ADC supply
1231 * is enabled everytime a GPADC conversion is triggered. We will
1232 * force it to be enabled from this driver to have
1233 * the GPADC module independant of the AB8500 chargers
1235 if (!di->vddadc_en_usb) {
1236 regulator_enable(di->regu);
1237 di->vddadc_en_usb = true;
1240 /* Enable USB charging */
1241 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1243 /* Check if the requested voltage or current is valid */
1244 volt_index = ab8500_voltage_to_regval(vset);
1245 curr_index = ab8500_current_to_regval(ich_out);
1246 if (volt_index < 0 || curr_index < 0) {
1248 "Charger voltage or current too high, "
1249 "charging not started\n");
1253 /* ChVoltLevel: max voltage upto which battery can be charged */
1254 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1255 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1257 dev_err(di->dev, "%s write failed\n", __func__);
1260 /* USBChInputCurr: current that can be drawn from the usb */
1261 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1263 dev_err(di->dev, "setting USBChInputCurr failed\n");
1266 /* ChOutputCurentLevel: protected output current */
1267 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1268 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index);
1270 dev_err(di->dev, "%s write failed\n", __func__);
1273 /* Check if VBAT overshoot control should be enabled */
1274 if (!di->bat->enable_overshoot)
1275 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1277 /* Enable USB Charger */
1278 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1279 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1281 dev_err(di->dev, "%s write failed\n", __func__);
1285 /* If success power on charging LED indication */
1286 ret = ab8500_charger_led_en(di, true);
1288 dev_err(di->dev, "failed to enable LED\n");
1290 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1292 di->usb.charger_online = 1;
1294 /* Disable USB charging */
1295 ret = abx500_set_register_interruptible(di->dev,
1297 AB8500_USBCH_CTRL1_REG, 0);
1300 "%s write failed\n", __func__);
1304 ret = ab8500_charger_led_en(di, false);
1306 dev_err(di->dev, "failed to disable LED\n");
1308 di->usb.charger_online = 0;
1309 di->usb.wd_expired = false;
1311 /* Disable regulator if enabled */
1312 if (di->vddadc_en_usb) {
1313 regulator_disable(di->regu);
1314 di->vddadc_en_usb = false;
1317 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1319 /* Cancel any pending Vbat check work */
1320 if (delayed_work_pending(&di->check_vbat_work))
1321 cancel_delayed_work(&di->check_vbat_work);
1324 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1330 * ab8500_charger_watchdog_kick() - kick charger watchdog
1331 * @di: pointer to the ab8500_charger structure
1333 * Kick charger watchdog
1334 * Returns error code in case of failure else 0(on success)
1336 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1339 struct ab8500_charger *di;
1341 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1342 di = to_ab8500_charger_ac_device_info(charger);
1343 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1344 di = to_ab8500_charger_usb_device_info(charger);
1348 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1349 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1351 dev_err(di->dev, "Failed to kick WD!\n");
1357 * ab8500_charger_update_charger_current() - update charger current
1358 * @di: pointer to the ab8500_charger structure
1360 * Update the charger output current for the specified charger
1361 * Returns error code in case of failure else 0(on success)
1363 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1368 struct ab8500_charger *di;
1370 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1371 di = to_ab8500_charger_ac_device_info(charger);
1372 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1373 di = to_ab8500_charger_usb_device_info(charger);
1377 curr_index = ab8500_current_to_regval(ich_out);
1378 if (curr_index < 0) {
1380 "Charger current too high, "
1381 "charging not started\n");
1385 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1386 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index);
1388 dev_err(di->dev, "%s write failed\n", __func__);
1392 /* Reset the main and usb drop input current measurement counter */
1393 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1394 AB8500_CHARGER_CTRL,
1397 dev_err(di->dev, "%s write failed\n", __func__);
1404 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1406 struct power_supply *psy;
1407 struct power_supply *ext;
1408 struct ab8500_charger *di;
1409 union power_supply_propval ret;
1411 bool psy_found = false;
1412 struct ux500_charger *usb_chg;
1414 usb_chg = (struct ux500_charger *)data;
1415 psy = &usb_chg->psy;
1417 di = to_ab8500_charger_usb_device_info(usb_chg);
1419 ext = dev_get_drvdata(dev);
1421 /* For all psy where the driver name appears in any supplied_to */
1422 for (i = 0; i < ext->num_supplicants; i++) {
1423 if (!strcmp(ext->supplied_to[i], psy->name))
1430 /* Go through all properties for the psy */
1431 for (j = 0; j < ext->num_properties; j++) {
1432 enum power_supply_property prop;
1433 prop = ext->properties[j];
1435 if (ext->get_property(ext, prop, &ret))
1439 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1440 switch (ext->type) {
1441 case POWER_SUPPLY_TYPE_BATTERY:
1442 di->vbat = ret.intval / 1000;
1456 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1457 * @work pointer to the work_struct structure
1459 * Due to a asic bug it is necessary to lower the input current to the vbus
1460 * charger when charging with at some specific levels. This issue is only valid
1461 * for below a certain battery voltage. This function makes sure that the
1462 * the allowed current limit isn't exceeded.
1464 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1467 struct ab8500_charger *di = container_of(work,
1468 struct ab8500_charger, check_vbat_work.work);
1470 class_for_each_device(power_supply_class, NULL,
1471 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1473 /* First run old_vbat is 0. */
1474 if (di->old_vbat == 0)
1475 di->old_vbat = di->vbat;
1477 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1478 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1479 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1480 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1482 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1483 " old: %d\n", di->max_usb_in_curr, di->vbat,
1485 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1486 power_supply_changed(&di->usb_chg.psy);
1489 di->old_vbat = di->vbat;
1492 * No need to check the battery voltage every second when not close to
1495 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1496 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1499 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1503 * ab8500_charger_check_hw_failure_work() - check main charger failure
1504 * @work: pointer to the work_struct structure
1506 * Work queue function for checking the main charger status
1508 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1513 struct ab8500_charger *di = container_of(work,
1514 struct ab8500_charger, check_hw_failure_work.work);
1516 /* Check if the status bits for HW failure is still active */
1517 if (di->flags.mainextchnotok) {
1518 ret = abx500_get_register_interruptible(di->dev,
1519 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1521 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1524 if (!(reg_value & MAIN_CH_NOK)) {
1525 di->flags.mainextchnotok = false;
1526 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1529 if (di->flags.vbus_ovv) {
1530 ret = abx500_get_register_interruptible(di->dev,
1531 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1534 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1537 if (!(reg_value & VBUS_OVV_TH)) {
1538 di->flags.vbus_ovv = false;
1539 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1542 /* If we still have a failure, schedule a new check */
1543 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1544 queue_delayed_work(di->charger_wq,
1545 &di->check_hw_failure_work, round_jiffies(HZ));
1550 * ab8500_charger_kick_watchdog_work() - kick the watchdog
1551 * @work: pointer to the work_struct structure
1553 * Work queue function for kicking the charger watchdog.
1555 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
1556 * logic. That means we have to continously kick the charger
1557 * watchdog even when no charger is connected. This is only
1558 * valid once the AC charger has been enabled. This is
1559 * a bug that is not handled by the algorithm and the
1560 * watchdog have to be kicked by the charger driver
1561 * when the AC charger is disabled
1563 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
1567 struct ab8500_charger *di = container_of(work,
1568 struct ab8500_charger, kick_wd_work.work);
1570 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1571 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1573 dev_err(di->dev, "Failed to kick WD!\n");
1575 /* Schedule a new watchdog kick */
1576 queue_delayed_work(di->charger_wq,
1577 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
1581 * ab8500_charger_ac_work() - work to get and set main charger status
1582 * @work: pointer to the work_struct structure
1584 * Work queue function for checking the main charger status
1586 static void ab8500_charger_ac_work(struct work_struct *work)
1590 struct ab8500_charger *di = container_of(work,
1591 struct ab8500_charger, ac_work);
1594 * Since we can't be sure that the events are received
1595 * synchronously, we have the check if the main charger is
1596 * connected by reading the status register
1598 ret = ab8500_charger_detect_chargers(di);
1602 if (ret & AC_PW_CONN) {
1603 di->ac.charger_connected = 1;
1606 di->ac.charger_connected = 0;
1609 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1610 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
1614 * ab8500_charger_detect_usb_type_work() - work to detect USB type
1615 * @work: Pointer to the work_struct structure
1617 * Detect the type of USB plugged
1619 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
1623 struct ab8500_charger *di = container_of(work,
1624 struct ab8500_charger, detect_usb_type_work);
1627 * Since we can't be sure that the events are received
1628 * synchronously, we have the check if is
1629 * connected by reading the status register
1631 ret = ab8500_charger_detect_chargers(di);
1635 if (!(ret & USB_PW_CONN)) {
1636 di->vbus_detected = 0;
1637 ab8500_charger_set_usb_connected(di, false);
1638 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1640 di->vbus_detected = 1;
1642 if (is_ab8500_1p1_or_earlier(di->parent)) {
1643 ret = ab8500_charger_detect_usb_type(di);
1645 ab8500_charger_set_usb_connected(di, true);
1646 ab8500_power_supply_changed(di,
1650 /* For ABB cut2.0 and onwards we have an IRQ,
1651 * USB_LINK_STATUS that will be triggered when the USB
1652 * link status changes. The exception is USB connected
1653 * during startup. Then we don't get a
1654 * USB_LINK_STATUS IRQ
1656 if (di->vbus_detected_start) {
1657 di->vbus_detected_start = false;
1658 ret = ab8500_charger_detect_usb_type(di);
1660 ab8500_charger_set_usb_connected(di,
1662 ab8500_power_supply_changed(di,
1671 * ab8500_charger_usb_link_status_work() - work to detect USB type
1672 * @work: pointer to the work_struct structure
1674 * Detect the type of USB plugged
1676 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
1680 struct ab8500_charger *di = container_of(work,
1681 struct ab8500_charger, usb_link_status_work);
1684 * Since we can't be sure that the events are received
1685 * synchronously, we have the check if is
1686 * connected by reading the status register
1688 ret = ab8500_charger_detect_chargers(di);
1692 if (!(ret & USB_PW_CONN)) {
1693 di->vbus_detected = 0;
1694 ab8500_charger_set_usb_connected(di, false);
1695 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1697 di->vbus_detected = 1;
1698 ret = ab8500_charger_read_usb_type(di);
1700 /* Update maximum input current */
1701 ret = ab8500_charger_set_vbus_in_curr(di,
1702 di->max_usb_in_curr);
1706 ab8500_charger_set_usb_connected(di, true);
1707 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1708 } else if (ret == -ENXIO) {
1709 /* No valid charger type detected */
1710 ab8500_charger_set_usb_connected(di, false);
1711 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1716 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
1719 unsigned long flags;
1721 struct ab8500_charger *di = container_of(work,
1722 struct ab8500_charger, usb_state_changed_work);
1724 if (!di->vbus_detected)
1727 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
1728 di->usb_state.usb_changed = false;
1729 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
1732 * wait for some time until you get updates from the usb stack
1733 * and negotiations are completed
1737 if (di->usb_state.usb_changed)
1740 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
1741 __func__, di->usb_state.state, di->usb_state.usb_current);
1743 switch (di->usb_state.state) {
1744 case AB8500_BM_USB_STATE_RESET_HS:
1745 case AB8500_BM_USB_STATE_RESET_FS:
1746 case AB8500_BM_USB_STATE_SUSPEND:
1747 case AB8500_BM_USB_STATE_MAX:
1748 ab8500_charger_set_usb_connected(di, false);
1749 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1752 case AB8500_BM_USB_STATE_RESUME:
1754 * when suspend->resume there should be delay
1755 * of 1sec for enabling charging
1758 /* Intentional fall through */
1759 case AB8500_BM_USB_STATE_CONFIGURED:
1761 * USB is configured, enable charging with the charging
1762 * input current obtained from USB driver
1764 if (!ab8500_charger_get_usb_cur(di)) {
1765 /* Update maximum input current */
1766 ret = ab8500_charger_set_vbus_in_curr(di,
1767 di->max_usb_in_curr);
1771 ab8500_charger_set_usb_connected(di, true);
1772 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1782 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
1783 * @work: pointer to the work_struct structure
1785 * Work queue function for checking the USB charger Not OK status
1787 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
1793 struct ab8500_charger *di = container_of(work,
1794 struct ab8500_charger, check_usbchgnotok_work.work);
1796 /* Check if the status bit for usbchargernotok is still active */
1797 ret = abx500_get_register_interruptible(di->dev,
1798 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
1800 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1803 prev_status = di->flags.usbchargernotok;
1805 if (reg_value & VBUS_CH_NOK) {
1806 di->flags.usbchargernotok = true;
1807 /* Check again in 1sec */
1808 queue_delayed_work(di->charger_wq,
1809 &di->check_usbchgnotok_work, HZ);
1811 di->flags.usbchargernotok = false;
1812 di->flags.vbus_collapse = false;
1815 if (prev_status != di->flags.usbchargernotok)
1816 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1820 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
1821 * @work: pointer to the work_struct structure
1823 * Work queue function for checking the Main thermal prot status
1825 static void ab8500_charger_check_main_thermal_prot_work(
1826 struct work_struct *work)
1831 struct ab8500_charger *di = container_of(work,
1832 struct ab8500_charger, check_main_thermal_prot_work);
1834 /* Check if the status bit for main_thermal_prot is still active */
1835 ret = abx500_get_register_interruptible(di->dev,
1836 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1838 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1841 if (reg_value & MAIN_CH_TH_PROT)
1842 di->flags.main_thermal_prot = true;
1844 di->flags.main_thermal_prot = false;
1846 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1850 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
1851 * @work: pointer to the work_struct structure
1853 * Work queue function for checking the USB thermal prot status
1855 static void ab8500_charger_check_usb_thermal_prot_work(
1856 struct work_struct *work)
1861 struct ab8500_charger *di = container_of(work,
1862 struct ab8500_charger, check_usb_thermal_prot_work);
1864 /* Check if the status bit for usb_thermal_prot is still active */
1865 ret = abx500_get_register_interruptible(di->dev,
1866 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
1868 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1871 if (reg_value & USB_CH_TH_PROT)
1872 di->flags.usb_thermal_prot = true;
1874 di->flags.usb_thermal_prot = false;
1876 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1880 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
1881 * @irq: interrupt number
1882 * @_di: pointer to the ab8500_charger structure
1884 * Returns IRQ status(IRQ_HANDLED)
1886 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
1888 struct ab8500_charger *di = _di;
1890 dev_dbg(di->dev, "Main charger unplugged\n");
1891 queue_work(di->charger_wq, &di->ac_work);
1897 * ab8500_charger_mainchplugdet_handler() - main charger plugged
1898 * @irq: interrupt number
1899 * @_di: pointer to the ab8500_charger structure
1901 * Returns IRQ status(IRQ_HANDLED)
1903 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
1905 struct ab8500_charger *di = _di;
1907 dev_dbg(di->dev, "Main charger plugged\n");
1908 queue_work(di->charger_wq, &di->ac_work);
1914 * ab8500_charger_mainextchnotok_handler() - main charger not ok
1915 * @irq: interrupt number
1916 * @_di: pointer to the ab8500_charger structure
1918 * Returns IRQ status(IRQ_HANDLED)
1920 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
1922 struct ab8500_charger *di = _di;
1924 dev_dbg(di->dev, "Main charger not ok\n");
1925 di->flags.mainextchnotok = true;
1926 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1928 /* Schedule a new HW failure check */
1929 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
1935 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
1936 * thermal protection threshold
1937 * @irq: interrupt number
1938 * @_di: pointer to the ab8500_charger structure
1940 * Returns IRQ status(IRQ_HANDLED)
1942 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
1944 struct ab8500_charger *di = _di;
1947 "Die temp above Main charger thermal protection threshold\n");
1948 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
1954 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
1955 * thermal protection threshold
1956 * @irq: interrupt number
1957 * @_di: pointer to the ab8500_charger structure
1959 * Returns IRQ status(IRQ_HANDLED)
1961 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
1963 struct ab8500_charger *di = _di;
1966 "Die temp ok for Main charger thermal protection threshold\n");
1967 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
1973 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
1974 * @irq: interrupt number
1975 * @_di: pointer to the ab8500_charger structure
1977 * Returns IRQ status(IRQ_HANDLED)
1979 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
1981 struct ab8500_charger *di = _di;
1983 dev_dbg(di->dev, "VBUS falling detected\n");
1984 queue_work(di->charger_wq, &di->detect_usb_type_work);
1990 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
1991 * @irq: interrupt number
1992 * @_di: pointer to the ab8500_charger structure
1994 * Returns IRQ status(IRQ_HANDLED)
1996 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
1998 struct ab8500_charger *di = _di;
2000 di->vbus_detected = true;
2001 dev_dbg(di->dev, "VBUS rising detected\n");
2002 queue_work(di->charger_wq, &di->detect_usb_type_work);
2008 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2009 * @irq: interrupt number
2010 * @_di: pointer to the ab8500_charger structure
2012 * Returns IRQ status(IRQ_HANDLED)
2014 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2016 struct ab8500_charger *di = _di;
2018 dev_dbg(di->dev, "USB link status changed\n");
2020 queue_work(di->charger_wq, &di->usb_link_status_work);
2026 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2027 * thermal protection threshold
2028 * @irq: interrupt number
2029 * @_di: pointer to the ab8500_charger structure
2031 * Returns IRQ status(IRQ_HANDLED)
2033 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2035 struct ab8500_charger *di = _di;
2038 "Die temp above USB charger thermal protection threshold\n");
2039 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2045 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2046 * thermal protection threshold
2047 * @irq: interrupt number
2048 * @_di: pointer to the ab8500_charger structure
2050 * Returns IRQ status(IRQ_HANDLED)
2052 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2054 struct ab8500_charger *di = _di;
2057 "Die temp ok for USB charger thermal protection threshold\n");
2058 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2064 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2065 * @irq: interrupt number
2066 * @_di: pointer to the ab8500_charger structure
2068 * Returns IRQ status(IRQ_HANDLED)
2070 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2072 struct ab8500_charger *di = _di;
2074 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2075 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2081 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2082 * @irq: interrupt number
2083 * @_di: pointer to the ab8500_charger structure
2085 * Returns IRQ status(IRQ_HANDLED)
2087 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2089 struct ab8500_charger *di = _di;
2091 dev_dbg(di->dev, "Charger watchdog expired\n");
2094 * The charger that was online when the watchdog expired
2095 * needs to be restarted for charging to start again
2097 if (di->ac.charger_online) {
2098 di->ac.wd_expired = true;
2099 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2101 if (di->usb.charger_online) {
2102 di->usb.wd_expired = true;
2103 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2110 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2111 * @irq: interrupt number
2112 * @_di: pointer to the ab8500_charger structure
2114 * Returns IRQ status(IRQ_HANDLED)
2116 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2118 struct ab8500_charger *di = _di;
2120 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2121 di->flags.vbus_ovv = true;
2122 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2124 /* Schedule a new HW failure check */
2125 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2131 * ab8500_charger_ac_get_property() - get the ac/mains properties
2132 * @psy: pointer to the power_supply structure
2133 * @psp: pointer to the power_supply_property structure
2134 * @val: pointer to the power_supply_propval union
2136 * This function gets called when an application tries to get the ac/mains
2137 * properties by reading the sysfs files.
2138 * AC/Mains properties are online, present and voltage.
2139 * online: ac/mains charging is in progress or not
2140 * present: presence of the ac/mains
2141 * voltage: AC/Mains voltage
2142 * Returns error code in case of failure else 0(on success)
2144 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2145 enum power_supply_property psp,
2146 union power_supply_propval *val)
2148 struct ab8500_charger *di;
2150 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2153 case POWER_SUPPLY_PROP_HEALTH:
2154 if (di->flags.mainextchnotok)
2155 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2156 else if (di->ac.wd_expired || di->usb.wd_expired)
2157 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2158 else if (di->flags.main_thermal_prot)
2159 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2161 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2163 case POWER_SUPPLY_PROP_ONLINE:
2164 val->intval = di->ac.charger_online;
2166 case POWER_SUPPLY_PROP_PRESENT:
2167 val->intval = di->ac.charger_connected;
2169 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2170 di->ac.charger_voltage = ab8500_charger_get_ac_voltage(di);
2171 val->intval = di->ac.charger_voltage * 1000;
2173 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2175 * This property is used to indicate when CV mode is entered
2176 * for the AC charger
2178 di->ac.cv_active = ab8500_charger_ac_cv(di);
2179 val->intval = di->ac.cv_active;
2181 case POWER_SUPPLY_PROP_CURRENT_NOW:
2182 val->intval = ab8500_charger_get_ac_current(di) * 1000;
2191 * ab8500_charger_usb_get_property() - get the usb properties
2192 * @psy: pointer to the power_supply structure
2193 * @psp: pointer to the power_supply_property structure
2194 * @val: pointer to the power_supply_propval union
2196 * This function gets called when an application tries to get the usb
2197 * properties by reading the sysfs files.
2198 * USB properties are online, present and voltage.
2199 * online: usb charging is in progress or not
2200 * present: presence of the usb
2201 * voltage: vbus voltage
2202 * Returns error code in case of failure else 0(on success)
2204 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2205 enum power_supply_property psp,
2206 union power_supply_propval *val)
2208 struct ab8500_charger *di;
2210 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2213 case POWER_SUPPLY_PROP_HEALTH:
2214 if (di->flags.usbchargernotok)
2215 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2216 else if (di->ac.wd_expired || di->usb.wd_expired)
2217 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2218 else if (di->flags.usb_thermal_prot)
2219 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2220 else if (di->flags.vbus_ovv)
2221 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2223 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2225 case POWER_SUPPLY_PROP_ONLINE:
2226 val->intval = di->usb.charger_online;
2228 case POWER_SUPPLY_PROP_PRESENT:
2229 val->intval = di->usb.charger_connected;
2231 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2232 di->usb.charger_voltage = ab8500_charger_get_vbus_voltage(di);
2233 val->intval = di->usb.charger_voltage * 1000;
2235 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2237 * This property is used to indicate when CV mode is entered
2238 * for the USB charger
2240 di->usb.cv_active = ab8500_charger_usb_cv(di);
2241 val->intval = di->usb.cv_active;
2243 case POWER_SUPPLY_PROP_CURRENT_NOW:
2244 val->intval = ab8500_charger_get_usb_current(di) * 1000;
2246 case POWER_SUPPLY_PROP_CURRENT_AVG:
2248 * This property is used to indicate when VBUS has collapsed
2249 * due to too high output current from the USB charger
2251 if (di->flags.vbus_collapse)
2263 * ab8500_charger_init_hw_registers() - Set up charger related registers
2264 * @di: pointer to the ab8500_charger structure
2266 * Set up charger OVV, watchdog and maximum voltage registers as well as
2267 * charging of the backup battery
2269 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
2273 /* Setup maximum charger current and voltage for ABB cut2.0 */
2274 if (!is_ab8500_1p1_or_earlier(di->parent)) {
2275 ret = abx500_set_register_interruptible(di->dev,
2277 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
2280 "failed to set CH_VOLT_LVL_MAX_REG\n");
2284 ret = abx500_set_register_interruptible(di->dev,
2286 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
2289 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2294 /* VBUS OVV set to 6.3V and enable automatic current limitiation */
2295 ret = abx500_set_register_interruptible(di->dev,
2297 AB8500_USBCH_CTRL2_REG,
2298 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
2300 dev_err(di->dev, "failed to set VBUS OVV\n");
2304 /* Enable main watchdog in OTP */
2305 ret = abx500_set_register_interruptible(di->dev,
2306 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
2308 dev_err(di->dev, "failed to enable main WD in OTP\n");
2312 /* Enable main watchdog */
2313 ret = abx500_set_register_interruptible(di->dev,
2314 AB8500_SYS_CTRL2_BLOCK,
2315 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
2317 dev_err(di->dev, "faile to enable main watchdog\n");
2322 * Due to internal synchronisation, Enable and Kick watchdog bits
2323 * cannot be enabled in a single write.
2324 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
2325 * between writing Enable then Kick bits.
2329 /* Kick main watchdog */
2330 ret = abx500_set_register_interruptible(di->dev,
2331 AB8500_SYS_CTRL2_BLOCK,
2332 AB8500_MAIN_WDOG_CTRL_REG,
2333 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
2335 dev_err(di->dev, "failed to kick main watchdog\n");
2339 /* Disable main watchdog */
2340 ret = abx500_set_register_interruptible(di->dev,
2341 AB8500_SYS_CTRL2_BLOCK,
2342 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
2344 dev_err(di->dev, "failed to disable main watchdog\n");
2348 /* Set watchdog timeout */
2349 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2350 AB8500_CH_WD_TIMER_REG, WD_TIMER);
2352 dev_err(di->dev, "failed to set charger watchdog timeout\n");
2356 /* Backup battery voltage and current */
2357 ret = abx500_set_register_interruptible(di->dev,
2359 AB8500_RTC_BACKUP_CHG_REG,
2360 di->bat->bkup_bat_v |
2361 di->bat->bkup_bat_i);
2363 dev_err(di->dev, "failed to setup backup battery charging\n");
2367 /* Enable backup battery charging */
2368 abx500_mask_and_set_register_interruptible(di->dev,
2369 AB8500_RTC, AB8500_RTC_CTRL_REG,
2370 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
2372 dev_err(di->dev, "%s mask and set failed\n", __func__);
2379 * ab8500 charger driver interrupts and their respective isr
2381 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
2382 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
2383 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
2384 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
2385 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
2386 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
2387 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
2388 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
2389 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
2390 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
2391 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
2392 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
2393 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
2394 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
2397 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
2398 unsigned long event, void *power)
2400 struct ab8500_charger *di =
2401 container_of(nb, struct ab8500_charger, nb);
2402 enum ab8500_usb_state bm_usb_state;
2403 unsigned mA = *((unsigned *)power);
2405 if (event != USB_EVENT_VBUS) {
2406 dev_dbg(di->dev, "not a standard host, returning\n");
2410 /* TODO: State is fabricate here. See if charger really needs USB
2411 * state or if mA is enough
2413 if ((di->usb_state.usb_current == 2) && (mA > 2))
2414 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
2416 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
2418 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
2419 else if (mA >= 8) /* 8, 100, 500 */
2420 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
2421 else /* Should never occur */
2422 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
2424 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
2425 __func__, bm_usb_state, mA);
2427 spin_lock(&di->usb_state.usb_lock);
2428 di->usb_state.usb_changed = true;
2429 spin_unlock(&di->usb_state.usb_lock);
2431 di->usb_state.state = bm_usb_state;
2432 di->usb_state.usb_current = mA;
2434 queue_work(di->charger_wq, &di->usb_state_changed_work);
2439 #if defined(CONFIG_PM)
2440 static int ab8500_charger_resume(struct platform_device *pdev)
2443 struct ab8500_charger *di = platform_get_drvdata(pdev);
2446 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2447 * logic. That means we have to continously kick the charger
2448 * watchdog even when no charger is connected. This is only
2449 * valid once the AC charger has been enabled. This is
2450 * a bug that is not handled by the algorithm and the
2451 * watchdog have to be kicked by the charger driver
2452 * when the AC charger is disabled
2454 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
2455 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2456 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2458 dev_err(di->dev, "Failed to kick WD!\n");
2460 /* If not already pending start a new timer */
2461 if (!delayed_work_pending(
2462 &di->kick_wd_work)) {
2463 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
2464 round_jiffies(WD_KICK_INTERVAL));
2468 /* If we still have a HW failure, schedule a new check */
2469 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2470 queue_delayed_work(di->charger_wq,
2471 &di->check_hw_failure_work, 0);
2477 static int ab8500_charger_suspend(struct platform_device *pdev,
2480 struct ab8500_charger *di = platform_get_drvdata(pdev);
2482 /* Cancel any pending HW failure check */
2483 if (delayed_work_pending(&di->check_hw_failure_work))
2484 cancel_delayed_work(&di->check_hw_failure_work);
2489 #define ab8500_charger_suspend NULL
2490 #define ab8500_charger_resume NULL
2493 static int __devexit ab8500_charger_remove(struct platform_device *pdev)
2495 struct ab8500_charger *di = platform_get_drvdata(pdev);
2498 /* Disable AC charging */
2499 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
2501 /* Disable USB charging */
2502 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
2504 /* Disable interrupts */
2505 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2506 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2510 /* disable the regulator */
2511 regulator_put(di->regu);
2513 /* Backup battery voltage and current disable */
2514 ret = abx500_mask_and_set_register_interruptible(di->dev,
2515 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
2517 dev_err(di->dev, "%s mask and set failed\n", __func__);
2519 usb_unregister_notifier(di->usb_phy, &di->nb);
2520 usb_put_phy(di->usb_phy);
2522 /* Delete the work queue */
2523 destroy_workqueue(di->charger_wq);
2525 flush_scheduled_work();
2526 power_supply_unregister(&di->usb_chg.psy);
2527 power_supply_unregister(&di->ac_chg.psy);
2528 platform_set_drvdata(pdev, NULL);
2534 static int __devinit ab8500_charger_probe(struct platform_device *pdev)
2536 int irq, i, charger_status, ret = 0;
2537 struct abx500_bm_plat_data *plat_data = pdev->dev.platform_data;
2538 struct ab8500_charger *di;
2541 dev_err(&pdev->dev, "No platform data\n");
2545 di = kzalloc(sizeof(*di), GFP_KERNEL);
2549 /* get parent data */
2550 di->dev = &pdev->dev;
2551 di->parent = dev_get_drvdata(pdev->dev.parent);
2552 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2554 /* initialize lock */
2555 spin_lock_init(&di->usb_state.usb_lock);
2557 /* get charger specific platform data */
2558 di->pdata = plat_data->charger;
2560 dev_err(di->dev, "no charger platform data supplied\n");
2562 goto free_device_info;
2565 /* get battery specific platform data */
2566 di->bat = plat_data->battery;
2568 dev_err(di->dev, "no battery platform data supplied\n");
2570 goto free_device_info;
2573 di->autopower = false;
2576 /* power_supply base class */
2577 di->ac_chg.psy.name = "ab8500_ac";
2578 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
2579 di->ac_chg.psy.properties = ab8500_charger_ac_props;
2580 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
2581 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
2582 di->ac_chg.psy.supplied_to = di->pdata->supplied_to;
2583 di->ac_chg.psy.num_supplicants = di->pdata->num_supplicants;
2584 /* ux500_charger sub-class */
2585 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
2586 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2587 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2588 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
2589 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2590 di->ac_chg.max_out_curr = ab8500_charger_current_map[
2591 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2594 /* power_supply base class */
2595 di->usb_chg.psy.name = "ab8500_usb";
2596 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
2597 di->usb_chg.psy.properties = ab8500_charger_usb_props;
2598 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
2599 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
2600 di->usb_chg.psy.supplied_to = di->pdata->supplied_to;
2601 di->usb_chg.psy.num_supplicants = di->pdata->num_supplicants;
2602 /* ux500_charger sub-class */
2603 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
2604 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2605 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2606 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
2607 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2608 di->usb_chg.max_out_curr = ab8500_charger_current_map[
2609 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2612 /* Create a work queue for the charger */
2614 create_singlethread_workqueue("ab8500_charger_wq");
2615 if (di->charger_wq == NULL) {
2616 dev_err(di->dev, "failed to create work queue\n");
2617 goto free_device_info;
2620 /* Init work for HW failure check */
2621 INIT_DELAYED_WORK_DEFERRABLE(&di->check_hw_failure_work,
2622 ab8500_charger_check_hw_failure_work);
2623 INIT_DELAYED_WORK_DEFERRABLE(&di->check_usbchgnotok_work,
2624 ab8500_charger_check_usbchargernotok_work);
2627 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2628 * logic. That means we have to continously kick the charger
2629 * watchdog even when no charger is connected. This is only
2630 * valid once the AC charger has been enabled. This is
2631 * a bug that is not handled by the algorithm and the
2632 * watchdog have to be kicked by the charger driver
2633 * when the AC charger is disabled
2635 INIT_DELAYED_WORK_DEFERRABLE(&di->kick_wd_work,
2636 ab8500_charger_kick_watchdog_work);
2638 INIT_DELAYED_WORK_DEFERRABLE(&di->check_vbat_work,
2639 ab8500_charger_check_vbat_work);
2641 /* Init work for charger detection */
2642 INIT_WORK(&di->usb_link_status_work,
2643 ab8500_charger_usb_link_status_work);
2644 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
2645 INIT_WORK(&di->detect_usb_type_work,
2646 ab8500_charger_detect_usb_type_work);
2648 INIT_WORK(&di->usb_state_changed_work,
2649 ab8500_charger_usb_state_changed_work);
2651 /* Init work for checking HW status */
2652 INIT_WORK(&di->check_main_thermal_prot_work,
2653 ab8500_charger_check_main_thermal_prot_work);
2654 INIT_WORK(&di->check_usb_thermal_prot_work,
2655 ab8500_charger_check_usb_thermal_prot_work);
2658 * VDD ADC supply needs to be enabled from this driver when there
2659 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
2660 * interrupts during charging
2662 di->regu = regulator_get(di->dev, "vddadc");
2663 if (IS_ERR(di->regu)) {
2664 ret = PTR_ERR(di->regu);
2665 dev_err(di->dev, "failed to get vddadc regulator\n");
2666 goto free_charger_wq;
2670 /* Initialize OVV, and other registers */
2671 ret = ab8500_charger_init_hw_registers(di);
2673 dev_err(di->dev, "failed to initialize ABB registers\n");
2674 goto free_regulator;
2677 /* Register AC charger class */
2678 ret = power_supply_register(di->dev, &di->ac_chg.psy);
2680 dev_err(di->dev, "failed to register AC charger\n");
2681 goto free_regulator;
2684 /* Register USB charger class */
2685 ret = power_supply_register(di->dev, &di->usb_chg.psy);
2687 dev_err(di->dev, "failed to register USB charger\n");
2691 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
2692 if (IS_ERR_OR_NULL(di->usb_phy)) {
2693 dev_err(di->dev, "failed to get usb transceiver\n");
2697 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
2698 ret = usb_register_notifier(di->usb_phy, &di->nb);
2700 dev_err(di->dev, "failed to register usb notifier\n");
2704 /* Identify the connected charger types during startup */
2705 charger_status = ab8500_charger_detect_chargers(di);
2706 if (charger_status & AC_PW_CONN) {
2707 di->ac.charger_connected = 1;
2709 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2710 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2713 if (charger_status & USB_PW_CONN) {
2714 dev_dbg(di->dev, "VBUS Detect during startup\n");
2715 di->vbus_detected = true;
2716 di->vbus_detected_start = true;
2717 queue_work(di->charger_wq,
2718 &di->detect_usb_type_work);
2721 /* Register interrupts */
2722 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2723 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2724 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
2725 IRQF_SHARED | IRQF_NO_SUSPEND,
2726 ab8500_charger_irq[i].name, di);
2729 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
2730 , ab8500_charger_irq[i].name, irq, ret);
2733 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
2734 ab8500_charger_irq[i].name, irq, ret);
2737 platform_set_drvdata(pdev, di);
2742 usb_unregister_notifier(di->usb_phy, &di->nb);
2744 /* We also have to free all successfully registered irqs */
2745 for (i = i - 1; i >= 0; i--) {
2746 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2750 usb_put_phy(di->usb_phy);
2752 power_supply_unregister(&di->usb_chg.psy);
2754 power_supply_unregister(&di->ac_chg.psy);
2756 regulator_put(di->regu);
2758 destroy_workqueue(di->charger_wq);
2765 static struct platform_driver ab8500_charger_driver = {
2766 .probe = ab8500_charger_probe,
2767 .remove = __devexit_p(ab8500_charger_remove),
2768 .suspend = ab8500_charger_suspend,
2769 .resume = ab8500_charger_resume,
2771 .name = "ab8500-charger",
2772 .owner = THIS_MODULE,
2776 static int __init ab8500_charger_init(void)
2778 return platform_driver_register(&ab8500_charger_driver);
2781 static void __exit ab8500_charger_exit(void)
2783 platform_driver_unregister(&ab8500_charger_driver);
2786 subsys_initcall_sync(ab8500_charger_init);
2787 module_exit(ab8500_charger_exit);
2789 MODULE_LICENSE("GPL v2");
2790 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
2791 MODULE_ALIAS("platform:ab8500-charger");
2792 MODULE_DESCRIPTION("AB8500 charger management driver");