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 /* UsbLineStatus register - usb types */
83 enum ab8500_charger_link_status {
84 USB_STAT_NOT_CONFIGURED,
86 USB_STAT_STD_HOST_C_NS,
87 USB_STAT_STD_HOST_C_S,
90 USB_STAT_HOST_CHG_HS_CHIRP,
91 USB_STAT_DEDICATED_CHG,
94 USB_STAT_ACA_RID_C_NM,
95 USB_STAT_ACA_RID_C_HS,
96 USB_STAT_ACA_RID_C_HS_CHIRP,
99 USB_STAT_NOT_VALID_LINK,
102 enum ab8500_usb_state {
103 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
104 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
105 AB8500_BM_USB_STATE_CONFIGURED,
106 AB8500_BM_USB_STATE_SUSPEND,
107 AB8500_BM_USB_STATE_RESUME,
108 AB8500_BM_USB_STATE_MAX,
111 /* VBUS input current limits supported in AB8500 in mA */
112 #define USB_CH_IP_CUR_LVL_0P05 50
113 #define USB_CH_IP_CUR_LVL_0P09 98
114 #define USB_CH_IP_CUR_LVL_0P19 193
115 #define USB_CH_IP_CUR_LVL_0P29 290
116 #define USB_CH_IP_CUR_LVL_0P38 380
117 #define USB_CH_IP_CUR_LVL_0P45 450
118 #define USB_CH_IP_CUR_LVL_0P5 500
119 #define USB_CH_IP_CUR_LVL_0P6 600
120 #define USB_CH_IP_CUR_LVL_0P7 700
121 #define USB_CH_IP_CUR_LVL_0P8 800
122 #define USB_CH_IP_CUR_LVL_0P9 900
123 #define USB_CH_IP_CUR_LVL_1P0 1000
124 #define USB_CH_IP_CUR_LVL_1P1 1100
125 #define USB_CH_IP_CUR_LVL_1P3 1300
126 #define USB_CH_IP_CUR_LVL_1P4 1400
127 #define USB_CH_IP_CUR_LVL_1P5 1500
129 #define VBAT_TRESH_IP_CUR_RED 3800
131 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
132 struct ab8500_charger, usb_chg)
133 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
134 struct ab8500_charger, ac_chg)
137 * struct ab8500_charger_interrupts - ab8500 interupts
138 * @name: name of the interrupt
139 * @isr function pointer to the isr
141 struct ab8500_charger_interrupts {
143 irqreturn_t (*isr)(int irq, void *data);
146 struct ab8500_charger_info {
147 int charger_connected;
154 struct ab8500_charger_event_flags {
156 bool main_thermal_prot;
157 bool usb_thermal_prot;
159 bool usbchargernotok;
164 struct ab8500_charger_usb_state {
167 enum ab8500_usb_state state;
172 * struct ab8500_charger - ab8500 Charger device information
173 * @dev: Pointer to the structure device
174 * @max_usb_in_curr: Max USB charger input current
175 * @vbus_detected: VBUS detected
176 * @vbus_detected_start:
177 * VBUS detected during startup
178 * @ac_conn: This will be true when the AC charger has been plugged
179 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
181 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
183 * @vbat Battery voltage
184 * @old_vbat Previously measured battery voltage
185 * @autopower Indicate if we should have automatic pwron after pwrloss
186 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
187 * @parent: Pointer to the struct ab8500
188 * @gpadc: Pointer to the struct gpadc
189 * @bat: Pointer to the abx500_bm platform data
190 * @flags: Structure for information about events triggered
191 * @usb_state: Structure for usb stack information
192 * @ac_chg: AC charger power supply
193 * @usb_chg: USB charger power supply
194 * @ac: Structure that holds the AC charger properties
195 * @usb: Structure that holds the USB charger properties
196 * @regu: Pointer to the struct regulator
197 * @charger_wq: Work queue for the IRQs and checking HW state
198 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
199 * @check_hw_failure_work: Work for checking HW state
200 * @check_usbchgnotok_work: Work for checking USB charger not ok status
201 * @kick_wd_work: Work for kicking the charger watchdog in case
202 * of ABB rev 1.* due to the watchog logic bug
203 * @ac_work: Work for checking AC charger connection
204 * @detect_usb_type_work: Work for detecting the USB type connected
205 * @usb_link_status_work: Work for checking the new USB link status
206 * @usb_state_changed_work: Work for checking USB state
207 * @check_main_thermal_prot_work:
208 * Work for checking Main thermal status
209 * @check_usb_thermal_prot_work:
210 * Work for checking USB thermal status
212 struct ab8500_charger {
216 bool vbus_detected_start;
224 struct ab8500 *parent;
225 struct ab8500_gpadc *gpadc;
226 struct abx500_bm_data *bat;
227 struct ab8500_charger_event_flags flags;
228 struct ab8500_charger_usb_state usb_state;
229 struct ux500_charger ac_chg;
230 struct ux500_charger usb_chg;
231 struct ab8500_charger_info ac;
232 struct ab8500_charger_info usb;
233 struct regulator *regu;
234 struct workqueue_struct *charger_wq;
235 struct delayed_work check_vbat_work;
236 struct delayed_work check_hw_failure_work;
237 struct delayed_work check_usbchgnotok_work;
238 struct delayed_work kick_wd_work;
239 struct work_struct ac_work;
240 struct work_struct detect_usb_type_work;
241 struct work_struct usb_link_status_work;
242 struct work_struct usb_state_changed_work;
243 struct work_struct check_main_thermal_prot_work;
244 struct work_struct check_usb_thermal_prot_work;
245 struct usb_phy *usb_phy;
246 struct notifier_block nb;
250 static enum power_supply_property ab8500_charger_ac_props[] = {
251 POWER_SUPPLY_PROP_HEALTH,
252 POWER_SUPPLY_PROP_PRESENT,
253 POWER_SUPPLY_PROP_ONLINE,
254 POWER_SUPPLY_PROP_VOLTAGE_NOW,
255 POWER_SUPPLY_PROP_VOLTAGE_AVG,
256 POWER_SUPPLY_PROP_CURRENT_NOW,
260 static enum power_supply_property ab8500_charger_usb_props[] = {
261 POWER_SUPPLY_PROP_HEALTH,
262 POWER_SUPPLY_PROP_CURRENT_AVG,
263 POWER_SUPPLY_PROP_PRESENT,
264 POWER_SUPPLY_PROP_ONLINE,
265 POWER_SUPPLY_PROP_VOLTAGE_NOW,
266 POWER_SUPPLY_PROP_VOLTAGE_AVG,
267 POWER_SUPPLY_PROP_CURRENT_NOW,
271 * ab8500_power_loss_handling - set how we handle powerloss.
272 * @di: pointer to the ab8500_charger structure
274 * Magic nummbers are from STE HW department.
276 static void ab8500_power_loss_handling(struct ab8500_charger *di)
281 dev_dbg(di->dev, "Autopower : %d\n", di->autopower);
283 /* read the autopower register */
284 ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, ®);
286 dev_err(di->dev, "%d write failed\n", __LINE__);
290 /* enable the OPT emulation registers */
291 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
293 dev_err(di->dev, "%d write failed\n", __LINE__);
302 /* write back the changed value to autopower reg */
303 ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg);
305 dev_err(di->dev, "%d write failed\n", __LINE__);
309 /* disable the set OTP registers again */
310 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
312 dev_err(di->dev, "%d write failed\n", __LINE__);
318 * ab8500_power_supply_changed - a wrapper with local extentions for
319 * power_supply_changed
320 * @di: pointer to the ab8500_charger structure
321 * @psy: pointer to power_supply_that have changed.
324 static void ab8500_power_supply_changed(struct ab8500_charger *di,
325 struct power_supply *psy)
327 if (di->autopower_cfg) {
328 if (!di->usb.charger_connected &&
329 !di->ac.charger_connected &&
331 di->autopower = false;
332 ab8500_power_loss_handling(di);
333 } else if (!di->autopower &&
334 (di->ac.charger_connected ||
335 di->usb.charger_connected)) {
336 di->autopower = true;
337 ab8500_power_loss_handling(di);
340 power_supply_changed(psy);
343 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
346 if (connected != di->usb.charger_connected) {
347 dev_dbg(di->dev, "USB connected:%i\n", connected);
348 di->usb.charger_connected = connected;
349 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
354 * ab8500_charger_get_ac_voltage() - get ac charger voltage
355 * @di: pointer to the ab8500_charger structure
357 * Returns ac charger voltage (on success)
359 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
363 /* Only measure voltage if the charger is connected */
364 if (di->ac.charger_connected) {
365 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
367 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
375 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
376 * @di: pointer to the ab8500_charger structure
378 * Returns ac charger CV mode (on success) else error code
380 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
385 /* Only check CV mode if the charger is online */
386 if (di->ac.charger_online) {
387 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
388 AB8500_CH_STATUS1_REG, &val);
390 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
394 if (val & MAIN_CH_CV_ON)
404 * ab8500_charger_get_vbus_voltage() - get vbus voltage
405 * @di: pointer to the ab8500_charger structure
407 * This function returns the vbus voltage.
408 * Returns vbus voltage (on success)
410 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
414 /* Only measure voltage if the charger is connected */
415 if (di->usb.charger_connected) {
416 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
418 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
426 * ab8500_charger_get_usb_current() - get usb charger current
427 * @di: pointer to the ab8500_charger structure
429 * This function returns the usb charger current.
430 * Returns usb current (on success) and error code on failure
432 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
436 /* Only measure current if the charger is online */
437 if (di->usb.charger_online) {
438 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
440 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
448 * ab8500_charger_get_ac_current() - get ac charger current
449 * @di: pointer to the ab8500_charger structure
451 * This function returns the ac charger current.
452 * Returns ac current (on success) and error code on failure.
454 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
458 /* Only measure current if the charger is online */
459 if (di->ac.charger_online) {
460 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
462 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
470 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
471 * @di: pointer to the ab8500_charger structure
473 * Returns ac charger CV mode (on success) else error code
475 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
480 /* Only check CV mode if the charger is online */
481 if (di->usb.charger_online) {
482 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
483 AB8500_CH_USBCH_STAT1_REG, &val);
485 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
489 if (val & USB_CH_CV_ON)
501 * ab8500_charger_detect_chargers() - Detect the connected chargers
502 * @di: pointer to the ab8500_charger structure
504 * Returns the type of charger connected.
505 * For USB it will not mean we can actually charge from it
506 * but that there is a USB cable connected that we have to
507 * identify. This is used during startup when we don't get
508 * interrupts of the charger detection
510 * Returns an integer value, that means,
511 * NO_PW_CONN no power supply is connected
512 * AC_PW_CONN if the AC power supply is connected
513 * USB_PW_CONN if the USB power supply is connected
514 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
516 static int ab8500_charger_detect_chargers(struct ab8500_charger *di)
518 int result = NO_PW_CONN;
522 /* Check for AC charger */
523 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
524 AB8500_CH_STATUS1_REG, &val);
526 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
530 if (val & MAIN_CH_DET)
533 /* Check for USB charger */
534 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
535 AB8500_CH_USBCH_STAT1_REG, &val);
537 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
541 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
542 result |= USB_PW_CONN;
548 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
549 * @di: pointer to the ab8500_charger structure
550 * @link_status: the identified USB type
552 * Get the maximum current that is allowed to be drawn from the host
553 * based on the USB type.
554 * Returns error code in case of failure else 0 on success
556 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
557 enum ab8500_charger_link_status link_status)
561 switch (link_status) {
562 case USB_STAT_STD_HOST_NC:
563 case USB_STAT_STD_HOST_C_NS:
564 case USB_STAT_STD_HOST_C_S:
565 dev_dbg(di->dev, "USB Type - Standard host is "
566 "detected through USB driver\n");
567 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
569 case USB_STAT_HOST_CHG_HS_CHIRP:
570 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
572 case USB_STAT_HOST_CHG_HS:
573 case USB_STAT_ACA_RID_C_HS:
574 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
576 case USB_STAT_ACA_RID_A:
578 * Dedicated charger level minus maximum current accessory
579 * can consume (300mA). Closest level is 1100mA
581 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1;
583 case USB_STAT_ACA_RID_B:
585 * Dedicated charger level minus 120mA (20mA for ACA and
586 * 100mA for potential accessory). Closest level is 1300mA
588 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
590 case USB_STAT_DEDICATED_CHG:
591 case USB_STAT_HOST_CHG_NM:
592 case USB_STAT_ACA_RID_C_HS_CHIRP:
593 case USB_STAT_ACA_RID_C_NM:
594 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
596 case USB_STAT_RESERVED:
598 * This state is used to indicate that VBUS has dropped below
599 * the detection level 4 times in a row. This is due to the
600 * charger output current is set to high making the charger
601 * voltage collapse. This have to be propagated through to
602 * chargalg. This is done using the property
603 * POWER_SUPPLY_PROP_CURRENT_AVG = 1
605 di->flags.vbus_collapse = true;
606 dev_dbg(di->dev, "USB Type - USB_STAT_RESERVED "
607 "VBUS has collapsed\n");
610 case USB_STAT_HM_IDGND:
611 case USB_STAT_NOT_CONFIGURED:
612 case USB_STAT_NOT_VALID_LINK:
613 dev_err(di->dev, "USB Type - Charging not allowed\n");
614 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
618 dev_err(di->dev, "USB Type - Unknown\n");
619 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
624 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
625 link_status, di->max_usb_in_curr);
631 * ab8500_charger_read_usb_type() - read the type of usb connected
632 * @di: pointer to the ab8500_charger structure
634 * Detect the type of the plugged USB
635 * Returns error code in case of failure else 0 on success
637 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
642 ret = abx500_get_register_interruptible(di->dev,
643 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
645 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
648 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
649 AB8500_USB_LINE_STAT_REG, &val);
651 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
655 /* get the USB type */
656 val = (val & AB8500_USB_LINK_STATUS) >> 3;
657 ret = ab8500_charger_max_usb_curr(di,
658 (enum ab8500_charger_link_status) val);
664 * ab8500_charger_detect_usb_type() - get the type of usb connected
665 * @di: pointer to the ab8500_charger structure
667 * Detect the type of the plugged USB
668 * Returns error code in case of failure else 0 on success
670 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
676 * On getting the VBUS rising edge detect interrupt there
677 * is a 250ms delay after which the register UsbLineStatus
678 * is filled with valid data.
680 for (i = 0; i < 10; i++) {
682 ret = abx500_get_register_interruptible(di->dev,
683 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
686 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
689 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
690 AB8500_USB_LINE_STAT_REG, &val);
692 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
696 * Until the IT source register is read the UsbLineStatus
697 * register is not updated, hence doing the same
701 /* get the USB type */
702 val = (val & AB8500_USB_LINK_STATUS) >> 3;
706 ret = ab8500_charger_max_usb_curr(di,
707 (enum ab8500_charger_link_status) val);
713 * This array maps the raw hex value to charger voltage used by the AB8500
714 * Values taken from the UM0836
716 static int ab8500_charger_voltage_map[] = {
798 * This array maps the raw hex value to charger current used by the AB8500
799 * Values taken from the UM0836
801 static int ab8500_charger_current_map[] = {
820 * This array maps the raw hex value to VBUS input current used by the AB8500
821 * Values taken from the UM0836
823 static int ab8500_charger_vbus_in_curr_map[] = {
824 USB_CH_IP_CUR_LVL_0P05,
825 USB_CH_IP_CUR_LVL_0P09,
826 USB_CH_IP_CUR_LVL_0P19,
827 USB_CH_IP_CUR_LVL_0P29,
828 USB_CH_IP_CUR_LVL_0P38,
829 USB_CH_IP_CUR_LVL_0P45,
830 USB_CH_IP_CUR_LVL_0P5,
831 USB_CH_IP_CUR_LVL_0P6,
832 USB_CH_IP_CUR_LVL_0P7,
833 USB_CH_IP_CUR_LVL_0P8,
834 USB_CH_IP_CUR_LVL_0P9,
835 USB_CH_IP_CUR_LVL_1P0,
836 USB_CH_IP_CUR_LVL_1P1,
837 USB_CH_IP_CUR_LVL_1P3,
838 USB_CH_IP_CUR_LVL_1P4,
839 USB_CH_IP_CUR_LVL_1P5,
842 static int ab8500_voltage_to_regval(int voltage)
846 /* Special case for voltage below 3.5V */
847 if (voltage < ab8500_charger_voltage_map[0])
850 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
851 if (voltage < ab8500_charger_voltage_map[i])
855 /* If not last element, return error */
856 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
857 if (voltage == ab8500_charger_voltage_map[i])
863 static int ab8500_current_to_regval(int curr)
867 if (curr < ab8500_charger_current_map[0])
870 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
871 if (curr < ab8500_charger_current_map[i])
875 /* If not last element, return error */
876 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
877 if (curr == ab8500_charger_current_map[i])
883 static int ab8500_vbus_in_curr_to_regval(int curr)
887 if (curr < ab8500_charger_vbus_in_curr_map[0])
890 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
891 if (curr < ab8500_charger_vbus_in_curr_map[i])
895 /* If not last element, return error */
896 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
897 if (curr == ab8500_charger_vbus_in_curr_map[i])
904 * ab8500_charger_get_usb_cur() - get usb current
905 * @di: pointer to the ab8500_charger structre
907 * The usb stack provides the maximum current that can be drawn from
908 * the standard usb host. This will be in mA.
909 * This function converts current in mA to a value that can be written
910 * to the register. Returns -1 if charging is not allowed
912 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
914 switch (di->usb_state.usb_current) {
916 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
919 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
922 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
925 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
928 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
931 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
939 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
940 * @di: pointer to the ab8500_charger structure
941 * @ich_in: charger input current limit
943 * Sets the current that can be drawn from the USB host
944 * Returns error code in case of failure else 0(on success)
946 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
950 int input_curr_index;
953 /* We should always use to lowest current limit */
954 min_value = min(di->bat->chg_params->usb_curr_max, ich_in);
958 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
959 min_value = USB_CH_IP_CUR_LVL_0P05;
962 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
963 min_value = USB_CH_IP_CUR_LVL_0P45;
969 input_curr_index = ab8500_vbus_in_curr_to_regval(min_value);
970 if (input_curr_index < 0) {
971 dev_err(di->dev, "VBUS input current limit too high\n");
975 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
976 AB8500_USBCH_IPT_CRNTLVL_REG,
977 input_curr_index << VBUS_IN_CURR_LIM_SHIFT);
979 dev_err(di->dev, "%s write failed\n", __func__);
985 * ab8500_charger_led_en() - turn on/off chargign led
986 * @di: pointer to the ab8500_charger structure
987 * @on: flag to turn on/off the chargign led
989 * Power ON/OFF charging LED indication
990 * Returns error code in case of failure else 0(on success)
992 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
997 /* Power ON charging LED indicator, set LED current to 5mA */
998 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
999 AB8500_LED_INDICATOR_PWM_CTRL,
1000 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1002 dev_err(di->dev, "Power ON LED failed\n");
1005 /* LED indicator PWM duty cycle 252/256 */
1006 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1007 AB8500_LED_INDICATOR_PWM_DUTY,
1008 LED_INDICATOR_PWM_DUTY_252_256);
1010 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1014 /* Power off charging LED indicator */
1015 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1016 AB8500_LED_INDICATOR_PWM_CTRL,
1017 LED_INDICATOR_PWM_DIS);
1019 dev_err(di->dev, "Power-off LED failed\n");
1028 * ab8500_charger_ac_en() - enable or disable ac charging
1029 * @di: pointer to the ab8500_charger structure
1030 * @enable: enable/disable flag
1031 * @vset: charging voltage
1032 * @iset: charging current
1034 * Enable/Disable AC/Mains charging and turns on/off the charging led
1037 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1038 int enable, int vset, int iset)
1043 int input_curr_index;
1046 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1049 /* Check if AC is connected */
1050 if (!di->ac.charger_connected) {
1051 dev_err(di->dev, "AC charger not connected\n");
1055 /* Enable AC charging */
1056 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1059 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1060 * will be triggered everytime we enable the VDD ADC supply.
1061 * This will turn off charging for a short while.
1062 * It can be avoided by having the supply on when
1063 * there is a charger enabled. Normally the VDD ADC supply
1064 * is enabled everytime a GPADC conversion is triggered. We will
1065 * force it to be enabled from this driver to have
1066 * the GPADC module independant of the AB8500 chargers
1068 if (!di->vddadc_en_ac) {
1069 regulator_enable(di->regu);
1070 di->vddadc_en_ac = true;
1073 /* Check if the requested voltage or current is valid */
1074 volt_index = ab8500_voltage_to_regval(vset);
1075 curr_index = ab8500_current_to_regval(iset);
1076 input_curr_index = ab8500_current_to_regval(
1077 di->bat->chg_params->ac_curr_max);
1078 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1080 "Charger voltage or current too high, "
1081 "charging not started\n");
1085 /* ChVoltLevel: maximum battery charging voltage */
1086 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1087 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1089 dev_err(di->dev, "%s write failed\n", __func__);
1092 /* MainChInputCurr: current that can be drawn from the charger*/
1093 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1094 AB8500_MCH_IPT_CURLVL_REG,
1095 input_curr_index << MAIN_CH_INPUT_CURR_SHIFT);
1097 dev_err(di->dev, "%s write failed\n", __func__);
1100 /* ChOutputCurentLevel: protected output current */
1101 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1102 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index);
1104 dev_err(di->dev, "%s write failed\n", __func__);
1108 /* Check if VBAT overshoot control should be enabled */
1109 if (!di->bat->enable_overshoot)
1110 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1112 /* Enable Main Charger */
1113 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1114 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1116 dev_err(di->dev, "%s write failed\n", __func__);
1120 /* Power on charging LED indication */
1121 ret = ab8500_charger_led_en(di, true);
1123 dev_err(di->dev, "failed to enable LED\n");
1125 di->ac.charger_online = 1;
1127 /* Disable AC charging */
1128 if (is_ab8500_1p1_or_earlier(di->parent)) {
1130 * For ABB revision 1.0 and 1.1 there is a bug in the
1131 * watchdog logic. That means we have to continously
1132 * kick the charger watchdog even when no charger is
1133 * connected. This is only valid once the AC charger
1134 * has been enabled. This is a bug that is not handled
1135 * by the algorithm and the watchdog have to be kicked
1136 * by the charger driver when the AC charger
1140 queue_delayed_work(di->charger_wq,
1142 round_jiffies(WD_KICK_INTERVAL));
1146 * We can't turn off charging completely
1147 * due to a bug in AB8500 cut1.
1148 * If we do, charging will not start again.
1149 * That is why we set the lowest voltage
1150 * and current possible
1152 ret = abx500_set_register_interruptible(di->dev,
1154 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1157 "%s write failed\n", __func__);
1161 ret = abx500_set_register_interruptible(di->dev,
1163 AB8500_CH_OPT_CRNTLVL_REG, CH_OP_CUR_LVL_0P1);
1166 "%s write failed\n", __func__);
1170 ret = abx500_set_register_interruptible(di->dev,
1172 AB8500_MCH_CTRL1, 0);
1175 "%s write failed\n", __func__);
1180 ret = ab8500_charger_led_en(di, false);
1182 dev_err(di->dev, "failed to disable LED\n");
1184 di->ac.charger_online = 0;
1185 di->ac.wd_expired = false;
1187 /* Disable regulator if enabled */
1188 if (di->vddadc_en_ac) {
1189 regulator_disable(di->regu);
1190 di->vddadc_en_ac = false;
1193 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1195 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1201 * ab8500_charger_usb_en() - enable usb charging
1202 * @di: pointer to the ab8500_charger structure
1203 * @enable: enable/disable flag
1204 * @vset: charging voltage
1205 * @ich_out: charger output current
1207 * Enable/Disable USB charging and turns on/off the charging led respectively.
1208 * Returns error code in case of failure else 0(on success)
1210 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1211 int enable, int vset, int ich_out)
1218 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1221 /* Check if USB is connected */
1222 if (!di->usb.charger_connected) {
1223 dev_err(di->dev, "USB charger not connected\n");
1228 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1229 * will be triggered everytime we enable the VDD ADC supply.
1230 * This will turn off charging for a short while.
1231 * It can be avoided by having the supply on when
1232 * there is a charger enabled. Normally the VDD ADC supply
1233 * is enabled everytime a GPADC conversion is triggered. We will
1234 * force it to be enabled from this driver to have
1235 * the GPADC module independant of the AB8500 chargers
1237 if (!di->vddadc_en_usb) {
1238 regulator_enable(di->regu);
1239 di->vddadc_en_usb = true;
1242 /* Enable USB charging */
1243 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1245 /* Check if the requested voltage or current is valid */
1246 volt_index = ab8500_voltage_to_regval(vset);
1247 curr_index = ab8500_current_to_regval(ich_out);
1248 if (volt_index < 0 || curr_index < 0) {
1250 "Charger voltage or current too high, "
1251 "charging not started\n");
1255 /* ChVoltLevel: max voltage upto which battery can be charged */
1256 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1257 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1259 dev_err(di->dev, "%s write failed\n", __func__);
1262 /* USBChInputCurr: current that can be drawn from the usb */
1263 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1265 dev_err(di->dev, "setting USBChInputCurr failed\n");
1268 /* ChOutputCurentLevel: protected output current */
1269 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1270 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index);
1272 dev_err(di->dev, "%s write failed\n", __func__);
1275 /* Check if VBAT overshoot control should be enabled */
1276 if (!di->bat->enable_overshoot)
1277 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1279 /* Enable USB Charger */
1280 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1281 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1283 dev_err(di->dev, "%s write failed\n", __func__);
1287 /* If success power on charging LED indication */
1288 ret = ab8500_charger_led_en(di, true);
1290 dev_err(di->dev, "failed to enable LED\n");
1292 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1294 di->usb.charger_online = 1;
1296 /* Disable USB charging */
1297 ret = abx500_set_register_interruptible(di->dev,
1299 AB8500_USBCH_CTRL1_REG, 0);
1302 "%s write failed\n", __func__);
1306 ret = ab8500_charger_led_en(di, false);
1308 dev_err(di->dev, "failed to disable LED\n");
1310 di->usb.charger_online = 0;
1311 di->usb.wd_expired = false;
1313 /* Disable regulator if enabled */
1314 if (di->vddadc_en_usb) {
1315 regulator_disable(di->regu);
1316 di->vddadc_en_usb = false;
1319 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1321 /* Cancel any pending Vbat check work */
1322 if (delayed_work_pending(&di->check_vbat_work))
1323 cancel_delayed_work(&di->check_vbat_work);
1326 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1332 * ab8500_charger_watchdog_kick() - kick charger watchdog
1333 * @di: pointer to the ab8500_charger structure
1335 * Kick charger watchdog
1336 * Returns error code in case of failure else 0(on success)
1338 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1341 struct ab8500_charger *di;
1343 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1344 di = to_ab8500_charger_ac_device_info(charger);
1345 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1346 di = to_ab8500_charger_usb_device_info(charger);
1350 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1351 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1353 dev_err(di->dev, "Failed to kick WD!\n");
1359 * ab8500_charger_update_charger_current() - update charger current
1360 * @di: pointer to the ab8500_charger structure
1362 * Update the charger output current for the specified charger
1363 * Returns error code in case of failure else 0(on success)
1365 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1370 struct ab8500_charger *di;
1372 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1373 di = to_ab8500_charger_ac_device_info(charger);
1374 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1375 di = to_ab8500_charger_usb_device_info(charger);
1379 curr_index = ab8500_current_to_regval(ich_out);
1380 if (curr_index < 0) {
1382 "Charger current too high, "
1383 "charging not started\n");
1387 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1388 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index);
1390 dev_err(di->dev, "%s write failed\n", __func__);
1394 /* Reset the main and usb drop input current measurement counter */
1395 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1396 AB8500_CHARGER_CTRL,
1399 dev_err(di->dev, "%s write failed\n", __func__);
1406 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1408 struct power_supply *psy;
1409 struct power_supply *ext;
1410 struct ab8500_charger *di;
1411 union power_supply_propval ret;
1413 bool psy_found = false;
1414 struct ux500_charger *usb_chg;
1416 usb_chg = (struct ux500_charger *)data;
1417 psy = &usb_chg->psy;
1419 di = to_ab8500_charger_usb_device_info(usb_chg);
1421 ext = dev_get_drvdata(dev);
1423 /* For all psy where the driver name appears in any supplied_to */
1424 for (i = 0; i < ext->num_supplicants; i++) {
1425 if (!strcmp(ext->supplied_to[i], psy->name))
1432 /* Go through all properties for the psy */
1433 for (j = 0; j < ext->num_properties; j++) {
1434 enum power_supply_property prop;
1435 prop = ext->properties[j];
1437 if (ext->get_property(ext, prop, &ret))
1441 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1442 switch (ext->type) {
1443 case POWER_SUPPLY_TYPE_BATTERY:
1444 di->vbat = ret.intval / 1000;
1458 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1459 * @work pointer to the work_struct structure
1461 * Due to a asic bug it is necessary to lower the input current to the vbus
1462 * charger when charging with at some specific levels. This issue is only valid
1463 * for below a certain battery voltage. This function makes sure that the
1464 * the allowed current limit isn't exceeded.
1466 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1469 struct ab8500_charger *di = container_of(work,
1470 struct ab8500_charger, check_vbat_work.work);
1472 class_for_each_device(power_supply_class, NULL,
1473 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1475 /* First run old_vbat is 0. */
1476 if (di->old_vbat == 0)
1477 di->old_vbat = di->vbat;
1479 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1480 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1481 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1482 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1484 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1485 " old: %d\n", di->max_usb_in_curr, di->vbat,
1487 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1488 power_supply_changed(&di->usb_chg.psy);
1491 di->old_vbat = di->vbat;
1494 * No need to check the battery voltage every second when not close to
1497 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1498 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1501 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1505 * ab8500_charger_check_hw_failure_work() - check main charger failure
1506 * @work: pointer to the work_struct structure
1508 * Work queue function for checking the main charger status
1510 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1515 struct ab8500_charger *di = container_of(work,
1516 struct ab8500_charger, check_hw_failure_work.work);
1518 /* Check if the status bits for HW failure is still active */
1519 if (di->flags.mainextchnotok) {
1520 ret = abx500_get_register_interruptible(di->dev,
1521 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1523 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1526 if (!(reg_value & MAIN_CH_NOK)) {
1527 di->flags.mainextchnotok = false;
1528 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1531 if (di->flags.vbus_ovv) {
1532 ret = abx500_get_register_interruptible(di->dev,
1533 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1536 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1539 if (!(reg_value & VBUS_OVV_TH)) {
1540 di->flags.vbus_ovv = false;
1541 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1544 /* If we still have a failure, schedule a new check */
1545 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1546 queue_delayed_work(di->charger_wq,
1547 &di->check_hw_failure_work, round_jiffies(HZ));
1552 * ab8500_charger_kick_watchdog_work() - kick the watchdog
1553 * @work: pointer to the work_struct structure
1555 * Work queue function for kicking the charger watchdog.
1557 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
1558 * logic. That means we have to continously kick the charger
1559 * watchdog even when no charger is connected. This is only
1560 * valid once the AC charger has been enabled. This is
1561 * a bug that is not handled by the algorithm and the
1562 * watchdog have to be kicked by the charger driver
1563 * when the AC charger is disabled
1565 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
1569 struct ab8500_charger *di = container_of(work,
1570 struct ab8500_charger, kick_wd_work.work);
1572 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1573 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1575 dev_err(di->dev, "Failed to kick WD!\n");
1577 /* Schedule a new watchdog kick */
1578 queue_delayed_work(di->charger_wq,
1579 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
1583 * ab8500_charger_ac_work() - work to get and set main charger status
1584 * @work: pointer to the work_struct structure
1586 * Work queue function for checking the main charger status
1588 static void ab8500_charger_ac_work(struct work_struct *work)
1592 struct ab8500_charger *di = container_of(work,
1593 struct ab8500_charger, ac_work);
1596 * Since we can't be sure that the events are received
1597 * synchronously, we have the check if the main charger is
1598 * connected by reading the status register
1600 ret = ab8500_charger_detect_chargers(di);
1604 if (ret & AC_PW_CONN) {
1605 di->ac.charger_connected = 1;
1608 di->ac.charger_connected = 0;
1611 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1612 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
1616 * ab8500_charger_detect_usb_type_work() - work to detect USB type
1617 * @work: Pointer to the work_struct structure
1619 * Detect the type of USB plugged
1621 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
1625 struct ab8500_charger *di = container_of(work,
1626 struct ab8500_charger, detect_usb_type_work);
1629 * Since we can't be sure that the events are received
1630 * synchronously, we have the check if is
1631 * connected by reading the status register
1633 ret = ab8500_charger_detect_chargers(di);
1637 if (!(ret & USB_PW_CONN)) {
1638 di->vbus_detected = 0;
1639 ab8500_charger_set_usb_connected(di, false);
1640 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1642 di->vbus_detected = 1;
1644 if (is_ab8500_1p1_or_earlier(di->parent)) {
1645 ret = ab8500_charger_detect_usb_type(di);
1647 ab8500_charger_set_usb_connected(di, true);
1648 ab8500_power_supply_changed(di,
1652 /* For ABB cut2.0 and onwards we have an IRQ,
1653 * USB_LINK_STATUS that will be triggered when the USB
1654 * link status changes. The exception is USB connected
1655 * during startup. Then we don't get a
1656 * USB_LINK_STATUS IRQ
1658 if (di->vbus_detected_start) {
1659 di->vbus_detected_start = false;
1660 ret = ab8500_charger_detect_usb_type(di);
1662 ab8500_charger_set_usb_connected(di,
1664 ab8500_power_supply_changed(di,
1673 * ab8500_charger_usb_link_status_work() - work to detect USB type
1674 * @work: pointer to the work_struct structure
1676 * Detect the type of USB plugged
1678 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
1682 struct ab8500_charger *di = container_of(work,
1683 struct ab8500_charger, usb_link_status_work);
1686 * Since we can't be sure that the events are received
1687 * synchronously, we have the check if is
1688 * connected by reading the status register
1690 ret = ab8500_charger_detect_chargers(di);
1694 if (!(ret & USB_PW_CONN)) {
1695 di->vbus_detected = 0;
1696 ab8500_charger_set_usb_connected(di, false);
1697 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1699 di->vbus_detected = 1;
1700 ret = ab8500_charger_read_usb_type(di);
1702 /* Update maximum input current */
1703 ret = ab8500_charger_set_vbus_in_curr(di,
1704 di->max_usb_in_curr);
1708 ab8500_charger_set_usb_connected(di, true);
1709 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1710 } else if (ret == -ENXIO) {
1711 /* No valid charger type detected */
1712 ab8500_charger_set_usb_connected(di, false);
1713 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1718 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
1721 unsigned long flags;
1723 struct ab8500_charger *di = container_of(work,
1724 struct ab8500_charger, usb_state_changed_work);
1726 if (!di->vbus_detected)
1729 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
1730 di->usb_state.usb_changed = false;
1731 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
1734 * wait for some time until you get updates from the usb stack
1735 * and negotiations are completed
1739 if (di->usb_state.usb_changed)
1742 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
1743 __func__, di->usb_state.state, di->usb_state.usb_current);
1745 switch (di->usb_state.state) {
1746 case AB8500_BM_USB_STATE_RESET_HS:
1747 case AB8500_BM_USB_STATE_RESET_FS:
1748 case AB8500_BM_USB_STATE_SUSPEND:
1749 case AB8500_BM_USB_STATE_MAX:
1750 ab8500_charger_set_usb_connected(di, false);
1751 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1754 case AB8500_BM_USB_STATE_RESUME:
1756 * when suspend->resume there should be delay
1757 * of 1sec for enabling charging
1760 /* Intentional fall through */
1761 case AB8500_BM_USB_STATE_CONFIGURED:
1763 * USB is configured, enable charging with the charging
1764 * input current obtained from USB driver
1766 if (!ab8500_charger_get_usb_cur(di)) {
1767 /* Update maximum input current */
1768 ret = ab8500_charger_set_vbus_in_curr(di,
1769 di->max_usb_in_curr);
1773 ab8500_charger_set_usb_connected(di, true);
1774 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1784 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
1785 * @work: pointer to the work_struct structure
1787 * Work queue function for checking the USB charger Not OK status
1789 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
1795 struct ab8500_charger *di = container_of(work,
1796 struct ab8500_charger, check_usbchgnotok_work.work);
1798 /* Check if the status bit for usbchargernotok is still active */
1799 ret = abx500_get_register_interruptible(di->dev,
1800 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
1802 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1805 prev_status = di->flags.usbchargernotok;
1807 if (reg_value & VBUS_CH_NOK) {
1808 di->flags.usbchargernotok = true;
1809 /* Check again in 1sec */
1810 queue_delayed_work(di->charger_wq,
1811 &di->check_usbchgnotok_work, HZ);
1813 di->flags.usbchargernotok = false;
1814 di->flags.vbus_collapse = false;
1817 if (prev_status != di->flags.usbchargernotok)
1818 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1822 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
1823 * @work: pointer to the work_struct structure
1825 * Work queue function for checking the Main thermal prot status
1827 static void ab8500_charger_check_main_thermal_prot_work(
1828 struct work_struct *work)
1833 struct ab8500_charger *di = container_of(work,
1834 struct ab8500_charger, check_main_thermal_prot_work);
1836 /* Check if the status bit for main_thermal_prot is still active */
1837 ret = abx500_get_register_interruptible(di->dev,
1838 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1840 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1843 if (reg_value & MAIN_CH_TH_PROT)
1844 di->flags.main_thermal_prot = true;
1846 di->flags.main_thermal_prot = false;
1848 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1852 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
1853 * @work: pointer to the work_struct structure
1855 * Work queue function for checking the USB thermal prot status
1857 static void ab8500_charger_check_usb_thermal_prot_work(
1858 struct work_struct *work)
1863 struct ab8500_charger *di = container_of(work,
1864 struct ab8500_charger, check_usb_thermal_prot_work);
1866 /* Check if the status bit for usb_thermal_prot is still active */
1867 ret = abx500_get_register_interruptible(di->dev,
1868 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
1870 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1873 if (reg_value & USB_CH_TH_PROT)
1874 di->flags.usb_thermal_prot = true;
1876 di->flags.usb_thermal_prot = false;
1878 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1882 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
1883 * @irq: interrupt number
1884 * @_di: pointer to the ab8500_charger structure
1886 * Returns IRQ status(IRQ_HANDLED)
1888 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
1890 struct ab8500_charger *di = _di;
1892 dev_dbg(di->dev, "Main charger unplugged\n");
1893 queue_work(di->charger_wq, &di->ac_work);
1899 * ab8500_charger_mainchplugdet_handler() - main charger plugged
1900 * @irq: interrupt number
1901 * @_di: pointer to the ab8500_charger structure
1903 * Returns IRQ status(IRQ_HANDLED)
1905 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
1907 struct ab8500_charger *di = _di;
1909 dev_dbg(di->dev, "Main charger plugged\n");
1910 queue_work(di->charger_wq, &di->ac_work);
1916 * ab8500_charger_mainextchnotok_handler() - main charger not ok
1917 * @irq: interrupt number
1918 * @_di: pointer to the ab8500_charger structure
1920 * Returns IRQ status(IRQ_HANDLED)
1922 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
1924 struct ab8500_charger *di = _di;
1926 dev_dbg(di->dev, "Main charger not ok\n");
1927 di->flags.mainextchnotok = true;
1928 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1930 /* Schedule a new HW failure check */
1931 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
1937 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
1938 * thermal protection threshold
1939 * @irq: interrupt number
1940 * @_di: pointer to the ab8500_charger structure
1942 * Returns IRQ status(IRQ_HANDLED)
1944 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
1946 struct ab8500_charger *di = _di;
1949 "Die temp above Main charger thermal protection threshold\n");
1950 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
1956 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
1957 * thermal protection threshold
1958 * @irq: interrupt number
1959 * @_di: pointer to the ab8500_charger structure
1961 * Returns IRQ status(IRQ_HANDLED)
1963 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
1965 struct ab8500_charger *di = _di;
1968 "Die temp ok for Main charger thermal protection threshold\n");
1969 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
1975 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
1976 * @irq: interrupt number
1977 * @_di: pointer to the ab8500_charger structure
1979 * Returns IRQ status(IRQ_HANDLED)
1981 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
1983 struct ab8500_charger *di = _di;
1985 dev_dbg(di->dev, "VBUS falling detected\n");
1986 queue_work(di->charger_wq, &di->detect_usb_type_work);
1992 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
1993 * @irq: interrupt number
1994 * @_di: pointer to the ab8500_charger structure
1996 * Returns IRQ status(IRQ_HANDLED)
1998 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2000 struct ab8500_charger *di = _di;
2002 di->vbus_detected = true;
2003 dev_dbg(di->dev, "VBUS rising detected\n");
2004 queue_work(di->charger_wq, &di->detect_usb_type_work);
2010 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2011 * @irq: interrupt number
2012 * @_di: pointer to the ab8500_charger structure
2014 * Returns IRQ status(IRQ_HANDLED)
2016 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2018 struct ab8500_charger *di = _di;
2020 dev_dbg(di->dev, "USB link status changed\n");
2022 queue_work(di->charger_wq, &di->usb_link_status_work);
2028 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2029 * thermal protection threshold
2030 * @irq: interrupt number
2031 * @_di: pointer to the ab8500_charger structure
2033 * Returns IRQ status(IRQ_HANDLED)
2035 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2037 struct ab8500_charger *di = _di;
2040 "Die temp above USB charger thermal protection threshold\n");
2041 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2047 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2048 * thermal protection threshold
2049 * @irq: interrupt number
2050 * @_di: pointer to the ab8500_charger structure
2052 * Returns IRQ status(IRQ_HANDLED)
2054 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2056 struct ab8500_charger *di = _di;
2059 "Die temp ok for USB charger thermal protection threshold\n");
2060 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2066 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2067 * @irq: interrupt number
2068 * @_di: pointer to the ab8500_charger structure
2070 * Returns IRQ status(IRQ_HANDLED)
2072 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2074 struct ab8500_charger *di = _di;
2076 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2077 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2083 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2084 * @irq: interrupt number
2085 * @_di: pointer to the ab8500_charger structure
2087 * Returns IRQ status(IRQ_HANDLED)
2089 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2091 struct ab8500_charger *di = _di;
2093 dev_dbg(di->dev, "Charger watchdog expired\n");
2096 * The charger that was online when the watchdog expired
2097 * needs to be restarted for charging to start again
2099 if (di->ac.charger_online) {
2100 di->ac.wd_expired = true;
2101 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2103 if (di->usb.charger_online) {
2104 di->usb.wd_expired = true;
2105 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2112 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2113 * @irq: interrupt number
2114 * @_di: pointer to the ab8500_charger structure
2116 * Returns IRQ status(IRQ_HANDLED)
2118 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2120 struct ab8500_charger *di = _di;
2122 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2123 di->flags.vbus_ovv = true;
2124 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2126 /* Schedule a new HW failure check */
2127 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2133 * ab8500_charger_ac_get_property() - get the ac/mains properties
2134 * @psy: pointer to the power_supply structure
2135 * @psp: pointer to the power_supply_property structure
2136 * @val: pointer to the power_supply_propval union
2138 * This function gets called when an application tries to get the ac/mains
2139 * properties by reading the sysfs files.
2140 * AC/Mains properties are online, present and voltage.
2141 * online: ac/mains charging is in progress or not
2142 * present: presence of the ac/mains
2143 * voltage: AC/Mains voltage
2144 * Returns error code in case of failure else 0(on success)
2146 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2147 enum power_supply_property psp,
2148 union power_supply_propval *val)
2150 struct ab8500_charger *di;
2152 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2155 case POWER_SUPPLY_PROP_HEALTH:
2156 if (di->flags.mainextchnotok)
2157 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2158 else if (di->ac.wd_expired || di->usb.wd_expired)
2159 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2160 else if (di->flags.main_thermal_prot)
2161 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2163 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2165 case POWER_SUPPLY_PROP_ONLINE:
2166 val->intval = di->ac.charger_online;
2168 case POWER_SUPPLY_PROP_PRESENT:
2169 val->intval = di->ac.charger_connected;
2171 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2172 di->ac.charger_voltage = ab8500_charger_get_ac_voltage(di);
2173 val->intval = di->ac.charger_voltage * 1000;
2175 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2177 * This property is used to indicate when CV mode is entered
2178 * for the AC charger
2180 di->ac.cv_active = ab8500_charger_ac_cv(di);
2181 val->intval = di->ac.cv_active;
2183 case POWER_SUPPLY_PROP_CURRENT_NOW:
2184 val->intval = ab8500_charger_get_ac_current(di) * 1000;
2193 * ab8500_charger_usb_get_property() - get the usb properties
2194 * @psy: pointer to the power_supply structure
2195 * @psp: pointer to the power_supply_property structure
2196 * @val: pointer to the power_supply_propval union
2198 * This function gets called when an application tries to get the usb
2199 * properties by reading the sysfs files.
2200 * USB properties are online, present and voltage.
2201 * online: usb charging is in progress or not
2202 * present: presence of the usb
2203 * voltage: vbus voltage
2204 * Returns error code in case of failure else 0(on success)
2206 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2207 enum power_supply_property psp,
2208 union power_supply_propval *val)
2210 struct ab8500_charger *di;
2212 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2215 case POWER_SUPPLY_PROP_HEALTH:
2216 if (di->flags.usbchargernotok)
2217 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2218 else if (di->ac.wd_expired || di->usb.wd_expired)
2219 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2220 else if (di->flags.usb_thermal_prot)
2221 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2222 else if (di->flags.vbus_ovv)
2223 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2225 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2227 case POWER_SUPPLY_PROP_ONLINE:
2228 val->intval = di->usb.charger_online;
2230 case POWER_SUPPLY_PROP_PRESENT:
2231 val->intval = di->usb.charger_connected;
2233 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2234 di->usb.charger_voltage = ab8500_charger_get_vbus_voltage(di);
2235 val->intval = di->usb.charger_voltage * 1000;
2237 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2239 * This property is used to indicate when CV mode is entered
2240 * for the USB charger
2242 di->usb.cv_active = ab8500_charger_usb_cv(di);
2243 val->intval = di->usb.cv_active;
2245 case POWER_SUPPLY_PROP_CURRENT_NOW:
2246 val->intval = ab8500_charger_get_usb_current(di) * 1000;
2248 case POWER_SUPPLY_PROP_CURRENT_AVG:
2250 * This property is used to indicate when VBUS has collapsed
2251 * due to too high output current from the USB charger
2253 if (di->flags.vbus_collapse)
2265 * ab8500_charger_init_hw_registers() - Set up charger related registers
2266 * @di: pointer to the ab8500_charger structure
2268 * Set up charger OVV, watchdog and maximum voltage registers as well as
2269 * charging of the backup battery
2271 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
2275 /* Setup maximum charger current and voltage for ABB cut2.0 */
2276 if (!is_ab8500_1p1_or_earlier(di->parent)) {
2277 ret = abx500_set_register_interruptible(di->dev,
2279 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
2282 "failed to set CH_VOLT_LVL_MAX_REG\n");
2286 ret = abx500_set_register_interruptible(di->dev,
2288 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
2291 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2296 /* VBUS OVV set to 6.3V and enable automatic current limitiation */
2297 ret = abx500_set_register_interruptible(di->dev,
2299 AB8500_USBCH_CTRL2_REG,
2300 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
2302 dev_err(di->dev, "failed to set VBUS OVV\n");
2306 /* Enable main watchdog in OTP */
2307 ret = abx500_set_register_interruptible(di->dev,
2308 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
2310 dev_err(di->dev, "failed to enable main WD in OTP\n");
2314 /* Enable main watchdog */
2315 ret = abx500_set_register_interruptible(di->dev,
2316 AB8500_SYS_CTRL2_BLOCK,
2317 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
2319 dev_err(di->dev, "faile to enable main watchdog\n");
2324 * Due to internal synchronisation, Enable and Kick watchdog bits
2325 * cannot be enabled in a single write.
2326 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
2327 * between writing Enable then Kick bits.
2331 /* Kick main watchdog */
2332 ret = abx500_set_register_interruptible(di->dev,
2333 AB8500_SYS_CTRL2_BLOCK,
2334 AB8500_MAIN_WDOG_CTRL_REG,
2335 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
2337 dev_err(di->dev, "failed to kick main watchdog\n");
2341 /* Disable main watchdog */
2342 ret = abx500_set_register_interruptible(di->dev,
2343 AB8500_SYS_CTRL2_BLOCK,
2344 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
2346 dev_err(di->dev, "failed to disable main watchdog\n");
2350 /* Set watchdog timeout */
2351 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2352 AB8500_CH_WD_TIMER_REG, WD_TIMER);
2354 dev_err(di->dev, "failed to set charger watchdog timeout\n");
2358 /* Backup battery voltage and current */
2359 ret = abx500_set_register_interruptible(di->dev,
2361 AB8500_RTC_BACKUP_CHG_REG,
2362 di->bat->bkup_bat_v |
2363 di->bat->bkup_bat_i);
2365 dev_err(di->dev, "failed to setup backup battery charging\n");
2369 /* Enable backup battery charging */
2370 abx500_mask_and_set_register_interruptible(di->dev,
2371 AB8500_RTC, AB8500_RTC_CTRL_REG,
2372 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
2374 dev_err(di->dev, "%s mask and set failed\n", __func__);
2381 * ab8500 charger driver interrupts and their respective isr
2383 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
2384 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
2385 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
2386 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
2387 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
2388 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
2389 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
2390 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
2391 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
2392 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
2393 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
2394 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
2395 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
2396 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
2399 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
2400 unsigned long event, void *power)
2402 struct ab8500_charger *di =
2403 container_of(nb, struct ab8500_charger, nb);
2404 enum ab8500_usb_state bm_usb_state;
2405 unsigned mA = *((unsigned *)power);
2407 if (event != USB_EVENT_VBUS) {
2408 dev_dbg(di->dev, "not a standard host, returning\n");
2412 /* TODO: State is fabricate here. See if charger really needs USB
2413 * state or if mA is enough
2415 if ((di->usb_state.usb_current == 2) && (mA > 2))
2416 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
2418 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
2420 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
2421 else if (mA >= 8) /* 8, 100, 500 */
2422 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
2423 else /* Should never occur */
2424 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
2426 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
2427 __func__, bm_usb_state, mA);
2429 spin_lock(&di->usb_state.usb_lock);
2430 di->usb_state.usb_changed = true;
2431 spin_unlock(&di->usb_state.usb_lock);
2433 di->usb_state.state = bm_usb_state;
2434 di->usb_state.usb_current = mA;
2436 queue_work(di->charger_wq, &di->usb_state_changed_work);
2441 #if defined(CONFIG_PM)
2442 static int ab8500_charger_resume(struct platform_device *pdev)
2445 struct ab8500_charger *di = platform_get_drvdata(pdev);
2448 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2449 * logic. That means we have to continously kick the charger
2450 * watchdog even when no charger is connected. This is only
2451 * valid once the AC charger has been enabled. This is
2452 * a bug that is not handled by the algorithm and the
2453 * watchdog have to be kicked by the charger driver
2454 * when the AC charger is disabled
2456 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
2457 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2458 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2460 dev_err(di->dev, "Failed to kick WD!\n");
2462 /* If not already pending start a new timer */
2463 if (!delayed_work_pending(
2464 &di->kick_wd_work)) {
2465 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
2466 round_jiffies(WD_KICK_INTERVAL));
2470 /* If we still have a HW failure, schedule a new check */
2471 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2472 queue_delayed_work(di->charger_wq,
2473 &di->check_hw_failure_work, 0);
2479 static int ab8500_charger_suspend(struct platform_device *pdev,
2482 struct ab8500_charger *di = platform_get_drvdata(pdev);
2484 /* Cancel any pending HW failure check */
2485 if (delayed_work_pending(&di->check_hw_failure_work))
2486 cancel_delayed_work(&di->check_hw_failure_work);
2491 #define ab8500_charger_suspend NULL
2492 #define ab8500_charger_resume NULL
2495 static int ab8500_charger_remove(struct platform_device *pdev)
2497 struct ab8500_charger *di = platform_get_drvdata(pdev);
2500 /* Disable AC charging */
2501 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
2503 /* Disable USB charging */
2504 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
2506 /* Disable interrupts */
2507 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2508 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2512 /* disable the regulator */
2513 regulator_put(di->regu);
2515 /* Backup battery voltage and current disable */
2516 ret = abx500_mask_and_set_register_interruptible(di->dev,
2517 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
2519 dev_err(di->dev, "%s mask and set failed\n", __func__);
2521 usb_unregister_notifier(di->usb_phy, &di->nb);
2522 usb_put_phy(di->usb_phy);
2524 /* Delete the work queue */
2525 destroy_workqueue(di->charger_wq);
2527 flush_scheduled_work();
2528 power_supply_unregister(&di->usb_chg.psy);
2529 power_supply_unregister(&di->ac_chg.psy);
2530 platform_set_drvdata(pdev, NULL);
2535 static char *supply_interface[] = {
2541 static int ab8500_charger_probe(struct platform_device *pdev)
2543 struct device_node *np = pdev->dev.of_node;
2544 struct ab8500_charger *di;
2545 int irq, i, charger_status, ret = 0;
2547 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
2549 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
2552 di->bat = pdev->mfd_cell->platform_data;
2555 ret = bmdevs_of_probe(&pdev->dev, np, &di->bat);
2558 "failed to get battery information\n");
2561 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
2563 dev_err(&pdev->dev, "missing dt node for ab8500_charger\n");
2567 dev_info(&pdev->dev, "falling back to legacy platform data\n");
2568 di->autopower_cfg = false;
2571 /* get parent data */
2572 di->dev = &pdev->dev;
2573 di->parent = dev_get_drvdata(pdev->dev.parent);
2574 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2576 /* initialize lock */
2577 spin_lock_init(&di->usb_state.usb_lock);
2579 di->autopower = false;
2582 /* power_supply base class */
2583 di->ac_chg.psy.name = "ab8500_ac";
2584 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
2585 di->ac_chg.psy.properties = ab8500_charger_ac_props;
2586 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
2587 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
2588 di->ac_chg.psy.supplied_to = supply_interface;
2589 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
2590 /* ux500_charger sub-class */
2591 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
2592 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2593 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2594 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
2595 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2596 di->ac_chg.max_out_curr = ab8500_charger_current_map[
2597 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2600 /* power_supply base class */
2601 di->usb_chg.psy.name = "ab8500_usb";
2602 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
2603 di->usb_chg.psy.properties = ab8500_charger_usb_props;
2604 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
2605 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
2606 di->usb_chg.psy.supplied_to = supply_interface;
2607 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
2608 /* ux500_charger sub-class */
2609 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
2610 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2611 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2612 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
2613 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2614 di->usb_chg.max_out_curr = ab8500_charger_current_map[
2615 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2618 /* Create a work queue for the charger */
2620 create_singlethread_workqueue("ab8500_charger_wq");
2621 if (di->charger_wq == NULL) {
2622 dev_err(di->dev, "failed to create work queue\n");
2626 /* Init work for HW failure check */
2627 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
2628 ab8500_charger_check_hw_failure_work);
2629 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
2630 ab8500_charger_check_usbchargernotok_work);
2633 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2634 * logic. That means we have to continously kick the charger
2635 * watchdog even when no charger is connected. This is only
2636 * valid once the AC charger has been enabled. This is
2637 * a bug that is not handled by the algorithm and the
2638 * watchdog have to be kicked by the charger driver
2639 * when the AC charger is disabled
2641 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
2642 ab8500_charger_kick_watchdog_work);
2644 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
2645 ab8500_charger_check_vbat_work);
2647 /* Init work for charger detection */
2648 INIT_WORK(&di->usb_link_status_work,
2649 ab8500_charger_usb_link_status_work);
2650 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
2651 INIT_WORK(&di->detect_usb_type_work,
2652 ab8500_charger_detect_usb_type_work);
2654 INIT_WORK(&di->usb_state_changed_work,
2655 ab8500_charger_usb_state_changed_work);
2657 /* Init work for checking HW status */
2658 INIT_WORK(&di->check_main_thermal_prot_work,
2659 ab8500_charger_check_main_thermal_prot_work);
2660 INIT_WORK(&di->check_usb_thermal_prot_work,
2661 ab8500_charger_check_usb_thermal_prot_work);
2664 * VDD ADC supply needs to be enabled from this driver when there
2665 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
2666 * interrupts during charging
2668 di->regu = regulator_get(di->dev, "vddadc");
2669 if (IS_ERR(di->regu)) {
2670 ret = PTR_ERR(di->regu);
2671 dev_err(di->dev, "failed to get vddadc regulator\n");
2672 goto free_charger_wq;
2676 /* Initialize OVV, and other registers */
2677 ret = ab8500_charger_init_hw_registers(di);
2679 dev_err(di->dev, "failed to initialize ABB registers\n");
2680 goto free_regulator;
2683 /* Register AC charger class */
2684 ret = power_supply_register(di->dev, &di->ac_chg.psy);
2686 dev_err(di->dev, "failed to register AC charger\n");
2687 goto free_regulator;
2690 /* Register USB charger class */
2691 ret = power_supply_register(di->dev, &di->usb_chg.psy);
2693 dev_err(di->dev, "failed to register USB charger\n");
2697 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
2698 if (IS_ERR_OR_NULL(di->usb_phy)) {
2699 dev_err(di->dev, "failed to get usb transceiver\n");
2703 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
2704 ret = usb_register_notifier(di->usb_phy, &di->nb);
2706 dev_err(di->dev, "failed to register usb notifier\n");
2710 /* Identify the connected charger types during startup */
2711 charger_status = ab8500_charger_detect_chargers(di);
2712 if (charger_status & AC_PW_CONN) {
2713 di->ac.charger_connected = 1;
2715 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2716 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2719 if (charger_status & USB_PW_CONN) {
2720 dev_dbg(di->dev, "VBUS Detect during startup\n");
2721 di->vbus_detected = true;
2722 di->vbus_detected_start = true;
2723 queue_work(di->charger_wq,
2724 &di->detect_usb_type_work);
2727 /* Register interrupts */
2728 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2729 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2730 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
2731 IRQF_SHARED | IRQF_NO_SUSPEND,
2732 ab8500_charger_irq[i].name, di);
2735 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
2736 , ab8500_charger_irq[i].name, irq, ret);
2739 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
2740 ab8500_charger_irq[i].name, irq, ret);
2743 platform_set_drvdata(pdev, di);
2748 usb_unregister_notifier(di->usb_phy, &di->nb);
2750 /* We also have to free all successfully registered irqs */
2751 for (i = i - 1; i >= 0; i--) {
2752 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2756 usb_put_phy(di->usb_phy);
2758 power_supply_unregister(&di->usb_chg.psy);
2760 power_supply_unregister(&di->ac_chg.psy);
2762 regulator_put(di->regu);
2764 destroy_workqueue(di->charger_wq);
2768 static const struct of_device_id ab8500_charger_match[] = {
2769 { .compatible = "stericsson,ab8500-charger", },
2773 static struct platform_driver ab8500_charger_driver = {
2774 .probe = ab8500_charger_probe,
2775 .remove = ab8500_charger_remove,
2776 .suspend = ab8500_charger_suspend,
2777 .resume = ab8500_charger_resume,
2779 .name = "ab8500-charger",
2780 .owner = THIS_MODULE,
2781 .of_match_table = ab8500_charger_match,
2785 static int __init ab8500_charger_init(void)
2787 return platform_driver_register(&ab8500_charger_driver);
2790 static void __exit ab8500_charger_exit(void)
2792 platform_driver_unregister(&ab8500_charger_driver);
2795 subsys_initcall_sync(ab8500_charger_init);
2796 module_exit(ab8500_charger_exit);
2798 MODULE_LICENSE("GPL v2");
2799 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
2800 MODULE_ALIAS("platform:ab8500-charger");
2801 MODULE_DESCRIPTION("AB8500 charger management driver");