2 * Copyright (C) ST-Ericsson SA 2012
4 * Charger driver for AB8500
6 * License Terms: GNU General Public License v2
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/platform_device.h>
20 #include <linux/power_supply.h>
21 #include <linux/completion.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/err.h>
24 #include <linux/workqueue.h>
25 #include <linux/kobject.h>
27 #include <linux/mfd/core.h>
28 #include <linux/mfd/abx500/ab8500.h>
29 #include <linux/mfd/abx500.h>
30 #include <linux/mfd/abx500/ab8500-bm.h>
31 #include <linux/mfd/abx500/ab8500-gpadc.h>
32 #include <linux/mfd/abx500/ux500_chargalg.h>
33 #include <linux/usb/otg.h>
35 /* Charger constants */
40 #define MAIN_WDOG_ENA 0x01
41 #define MAIN_WDOG_KICK 0x02
42 #define MAIN_WDOG_DIS 0x00
43 #define CHARG_WD_KICK 0x01
44 #define MAIN_CH_ENA 0x01
45 #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
46 #define USB_CH_ENA 0x01
47 #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
48 #define MAIN_CH_DET 0x01
49 #define MAIN_CH_CV_ON 0x04
50 #define USB_CH_CV_ON 0x08
51 #define VBUS_DET_DBNC100 0x02
52 #define VBUS_DET_DBNC1 0x01
53 #define OTP_ENABLE_WD 0x01
55 #define MAIN_CH_INPUT_CURR_SHIFT 4
56 #define VBUS_IN_CURR_LIM_SHIFT 4
58 #define LED_INDICATOR_PWM_ENA 0x01
59 #define LED_INDICATOR_PWM_DIS 0x00
60 #define LED_IND_CUR_5MA 0x04
61 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
63 /* HW failure constants */
64 #define MAIN_CH_TH_PROT 0x02
65 #define VBUS_CH_NOK 0x08
66 #define USB_CH_TH_PROT 0x02
67 #define VBUS_OVV_TH 0x01
68 #define MAIN_CH_NOK 0x01
71 /* UsbLineStatus register bit masks */
72 #define AB8500_USB_LINK_STATUS 0x78
73 #define AB8500_STD_HOST_SUSP 0x18
75 /* Watchdog timeout constant */
76 #define WD_TIMER 0x30 /* 4min */
77 #define WD_KICK_INTERVAL (60 * HZ)
79 /* Lowest charger voltage is 3.39V -> 0x4E */
80 #define LOW_VOLT_REG 0x4E
82 /* Step up/down delay in us */
83 #define STEP_UDELAY 1000
85 /* UsbLineStatus register - usb types */
86 enum ab8500_charger_link_status {
87 USB_STAT_NOT_CONFIGURED,
89 USB_STAT_STD_HOST_C_NS,
90 USB_STAT_STD_HOST_C_S,
93 USB_STAT_HOST_CHG_HS_CHIRP,
94 USB_STAT_DEDICATED_CHG,
97 USB_STAT_ACA_RID_C_NM,
98 USB_STAT_ACA_RID_C_HS,
99 USB_STAT_ACA_RID_C_HS_CHIRP,
102 USB_STAT_NOT_VALID_LINK,
105 enum ab8500_usb_state {
106 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
107 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
108 AB8500_BM_USB_STATE_CONFIGURED,
109 AB8500_BM_USB_STATE_SUSPEND,
110 AB8500_BM_USB_STATE_RESUME,
111 AB8500_BM_USB_STATE_MAX,
114 /* VBUS input current limits supported in AB8500 in mA */
115 #define USB_CH_IP_CUR_LVL_0P05 50
116 #define USB_CH_IP_CUR_LVL_0P09 98
117 #define USB_CH_IP_CUR_LVL_0P19 193
118 #define USB_CH_IP_CUR_LVL_0P29 290
119 #define USB_CH_IP_CUR_LVL_0P38 380
120 #define USB_CH_IP_CUR_LVL_0P45 450
121 #define USB_CH_IP_CUR_LVL_0P5 500
122 #define USB_CH_IP_CUR_LVL_0P6 600
123 #define USB_CH_IP_CUR_LVL_0P7 700
124 #define USB_CH_IP_CUR_LVL_0P8 800
125 #define USB_CH_IP_CUR_LVL_0P9 900
126 #define USB_CH_IP_CUR_LVL_1P0 1000
127 #define USB_CH_IP_CUR_LVL_1P1 1100
128 #define USB_CH_IP_CUR_LVL_1P3 1300
129 #define USB_CH_IP_CUR_LVL_1P4 1400
130 #define USB_CH_IP_CUR_LVL_1P5 1500
132 #define VBAT_TRESH_IP_CUR_RED 3800
134 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
135 struct ab8500_charger, usb_chg)
136 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
137 struct ab8500_charger, ac_chg)
140 * struct ab8500_charger_interrupts - ab8500 interupts
141 * @name: name of the interrupt
142 * @isr function pointer to the isr
144 struct ab8500_charger_interrupts {
146 irqreturn_t (*isr)(int irq, void *data);
149 struct ab8500_charger_info {
150 int charger_connected;
157 struct ab8500_charger_event_flags {
159 bool main_thermal_prot;
160 bool usb_thermal_prot;
162 bool usbchargernotok;
167 struct ab8500_charger_usb_state {
170 enum ab8500_usb_state state;
175 * struct ab8500_charger - ab8500 Charger device information
176 * @dev: Pointer to the structure device
177 * @max_usb_in_curr: Max USB charger input current
178 * @vbus_detected: VBUS detected
179 * @vbus_detected_start:
180 * VBUS detected during startup
181 * @ac_conn: This will be true when the AC charger has been plugged
182 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
184 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
186 * @vbat Battery voltage
187 * @old_vbat Previously measured battery voltage
188 * @autopower Indicate if we should have automatic pwron after pwrloss
189 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
190 * @parent: Pointer to the struct ab8500
191 * @gpadc: Pointer to the struct gpadc
192 * @bm: Platform specific battery management information
193 * @flags: Structure for information about events triggered
194 * @usb_state: Structure for usb stack information
195 * @ac_chg: AC charger power supply
196 * @usb_chg: USB charger power supply
197 * @ac: Structure that holds the AC charger properties
198 * @usb: Structure that holds the USB charger properties
199 * @regu: Pointer to the struct regulator
200 * @charger_wq: Work queue for the IRQs and checking HW state
201 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
202 * @check_hw_failure_work: Work for checking HW state
203 * @check_usbchgnotok_work: Work for checking USB charger not ok status
204 * @kick_wd_work: Work for kicking the charger watchdog in case
205 * of ABB rev 1.* due to the watchog logic bug
206 * @ac_work: Work for checking AC charger connection
207 * @detect_usb_type_work: Work for detecting the USB type connected
208 * @usb_link_status_work: Work for checking the new USB link status
209 * @usb_state_changed_work: Work for checking USB state
210 * @check_main_thermal_prot_work:
211 * Work for checking Main thermal status
212 * @check_usb_thermal_prot_work:
213 * Work for checking USB thermal status
215 struct ab8500_charger {
219 bool vbus_detected_start;
227 struct ab8500 *parent;
228 struct ab8500_gpadc *gpadc;
229 struct abx500_bm_data *bm;
230 struct ab8500_charger_event_flags flags;
231 struct ab8500_charger_usb_state usb_state;
232 struct ux500_charger ac_chg;
233 struct ux500_charger usb_chg;
234 struct ab8500_charger_info ac;
235 struct ab8500_charger_info usb;
236 struct regulator *regu;
237 struct workqueue_struct *charger_wq;
238 struct delayed_work check_vbat_work;
239 struct delayed_work check_hw_failure_work;
240 struct delayed_work check_usbchgnotok_work;
241 struct delayed_work kick_wd_work;
242 struct work_struct ac_work;
243 struct work_struct detect_usb_type_work;
244 struct work_struct usb_link_status_work;
245 struct work_struct usb_state_changed_work;
246 struct work_struct check_main_thermal_prot_work;
247 struct work_struct check_usb_thermal_prot_work;
248 struct usb_phy *usb_phy;
249 struct notifier_block nb;
253 static enum power_supply_property ab8500_charger_ac_props[] = {
254 POWER_SUPPLY_PROP_HEALTH,
255 POWER_SUPPLY_PROP_PRESENT,
256 POWER_SUPPLY_PROP_ONLINE,
257 POWER_SUPPLY_PROP_VOLTAGE_NOW,
258 POWER_SUPPLY_PROP_VOLTAGE_AVG,
259 POWER_SUPPLY_PROP_CURRENT_NOW,
263 static enum power_supply_property ab8500_charger_usb_props[] = {
264 POWER_SUPPLY_PROP_HEALTH,
265 POWER_SUPPLY_PROP_CURRENT_AVG,
266 POWER_SUPPLY_PROP_PRESENT,
267 POWER_SUPPLY_PROP_ONLINE,
268 POWER_SUPPLY_PROP_VOLTAGE_NOW,
269 POWER_SUPPLY_PROP_VOLTAGE_AVG,
270 POWER_SUPPLY_PROP_CURRENT_NOW,
274 * ab8500_power_loss_handling - set how we handle powerloss.
275 * @di: pointer to the ab8500_charger structure
277 * Magic nummbers are from STE HW department.
279 static void ab8500_power_loss_handling(struct ab8500_charger *di)
284 dev_dbg(di->dev, "Autopower : %d\n", di->autopower);
286 /* read the autopower register */
287 ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, ®);
289 dev_err(di->dev, "%d write failed\n", __LINE__);
293 /* enable the OPT emulation registers */
294 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
296 dev_err(di->dev, "%d write failed\n", __LINE__);
305 /* write back the changed value to autopower reg */
306 ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg);
308 dev_err(di->dev, "%d write failed\n", __LINE__);
312 /* disable the set OTP registers again */
313 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
315 dev_err(di->dev, "%d write failed\n", __LINE__);
321 * ab8500_power_supply_changed - a wrapper with local extentions for
322 * power_supply_changed
323 * @di: pointer to the ab8500_charger structure
324 * @psy: pointer to power_supply_that have changed.
327 static void ab8500_power_supply_changed(struct ab8500_charger *di,
328 struct power_supply *psy)
330 if (di->autopower_cfg) {
331 if (!di->usb.charger_connected &&
332 !di->ac.charger_connected &&
334 di->autopower = false;
335 ab8500_power_loss_handling(di);
336 } else if (!di->autopower &&
337 (di->ac.charger_connected ||
338 di->usb.charger_connected)) {
339 di->autopower = true;
340 ab8500_power_loss_handling(di);
343 power_supply_changed(psy);
346 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
349 if (connected != di->usb.charger_connected) {
350 dev_dbg(di->dev, "USB connected:%i\n", connected);
351 di->usb.charger_connected = connected;
352 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
357 * ab8500_charger_get_ac_voltage() - get ac charger voltage
358 * @di: pointer to the ab8500_charger structure
360 * Returns ac charger voltage (on success)
362 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
366 /* Only measure voltage if the charger is connected */
367 if (di->ac.charger_connected) {
368 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
370 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
378 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
379 * @di: pointer to the ab8500_charger structure
381 * Returns ac charger CV mode (on success) else error code
383 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
388 /* Only check CV mode if the charger is online */
389 if (di->ac.charger_online) {
390 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
391 AB8500_CH_STATUS1_REG, &val);
393 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
397 if (val & MAIN_CH_CV_ON)
407 * ab8500_charger_get_vbus_voltage() - get vbus voltage
408 * @di: pointer to the ab8500_charger structure
410 * This function returns the vbus voltage.
411 * Returns vbus voltage (on success)
413 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
417 /* Only measure voltage if the charger is connected */
418 if (di->usb.charger_connected) {
419 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
421 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
429 * ab8500_charger_get_usb_current() - get usb charger current
430 * @di: pointer to the ab8500_charger structure
432 * This function returns the usb charger current.
433 * Returns usb current (on success) and error code on failure
435 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
439 /* Only measure current if the charger is online */
440 if (di->usb.charger_online) {
441 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
443 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
451 * ab8500_charger_get_ac_current() - get ac charger current
452 * @di: pointer to the ab8500_charger structure
454 * This function returns the ac charger current.
455 * Returns ac current (on success) and error code on failure.
457 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
461 /* Only measure current if the charger is online */
462 if (di->ac.charger_online) {
463 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
465 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
473 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
474 * @di: pointer to the ab8500_charger structure
476 * Returns ac charger CV mode (on success) else error code
478 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
483 /* Only check CV mode if the charger is online */
484 if (di->usb.charger_online) {
485 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
486 AB8500_CH_USBCH_STAT1_REG, &val);
488 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
492 if (val & USB_CH_CV_ON)
504 * ab8500_charger_detect_chargers() - Detect the connected chargers
505 * @di: pointer to the ab8500_charger structure
507 * Returns the type of charger connected.
508 * For USB it will not mean we can actually charge from it
509 * but that there is a USB cable connected that we have to
510 * identify. This is used during startup when we don't get
511 * interrupts of the charger detection
513 * Returns an integer value, that means,
514 * NO_PW_CONN no power supply is connected
515 * AC_PW_CONN if the AC power supply is connected
516 * USB_PW_CONN if the USB power supply is connected
517 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
519 static int ab8500_charger_detect_chargers(struct ab8500_charger *di)
521 int result = NO_PW_CONN;
525 /* Check for AC charger */
526 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
527 AB8500_CH_STATUS1_REG, &val);
529 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
533 if (val & MAIN_CH_DET)
536 /* Check for USB charger */
537 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
538 AB8500_CH_USBCH_STAT1_REG, &val);
540 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
544 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
545 result |= USB_PW_CONN;
551 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
552 * @di: pointer to the ab8500_charger structure
553 * @link_status: the identified USB type
555 * Get the maximum current that is allowed to be drawn from the host
556 * based on the USB type.
557 * Returns error code in case of failure else 0 on success
559 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
560 enum ab8500_charger_link_status link_status)
564 switch (link_status) {
565 case USB_STAT_STD_HOST_NC:
566 case USB_STAT_STD_HOST_C_NS:
567 case USB_STAT_STD_HOST_C_S:
568 dev_dbg(di->dev, "USB Type - Standard host is "
569 "detected through USB driver\n");
570 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
572 case USB_STAT_HOST_CHG_HS_CHIRP:
573 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
575 case USB_STAT_HOST_CHG_HS:
576 case USB_STAT_ACA_RID_C_HS:
577 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
579 case USB_STAT_ACA_RID_A:
581 * Dedicated charger level minus maximum current accessory
582 * can consume (300mA). Closest level is 1100mA
584 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1;
586 case USB_STAT_ACA_RID_B:
588 * Dedicated charger level minus 120mA (20mA for ACA and
589 * 100mA for potential accessory). Closest level is 1300mA
591 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
593 case USB_STAT_DEDICATED_CHG:
594 case USB_STAT_HOST_CHG_NM:
595 case USB_STAT_ACA_RID_C_HS_CHIRP:
596 case USB_STAT_ACA_RID_C_NM:
597 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
599 case USB_STAT_RESERVED:
601 * This state is used to indicate that VBUS has dropped below
602 * the detection level 4 times in a row. This is due to the
603 * charger output current is set to high making the charger
604 * voltage collapse. This have to be propagated through to
605 * chargalg. This is done using the property
606 * POWER_SUPPLY_PROP_CURRENT_AVG = 1
608 di->flags.vbus_collapse = true;
609 dev_dbg(di->dev, "USB Type - USB_STAT_RESERVED "
610 "VBUS has collapsed\n");
613 case USB_STAT_HM_IDGND:
614 case USB_STAT_NOT_CONFIGURED:
615 case USB_STAT_NOT_VALID_LINK:
616 dev_err(di->dev, "USB Type - Charging not allowed\n");
617 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
621 dev_err(di->dev, "USB Type - Unknown\n");
622 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
627 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
628 link_status, di->max_usb_in_curr);
634 * ab8500_charger_read_usb_type() - read the type of usb connected
635 * @di: pointer to the ab8500_charger structure
637 * Detect the type of the plugged USB
638 * Returns error code in case of failure else 0 on success
640 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
645 ret = abx500_get_register_interruptible(di->dev,
646 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
648 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
651 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
652 AB8500_USB_LINE_STAT_REG, &val);
654 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
658 /* get the USB type */
659 val = (val & AB8500_USB_LINK_STATUS) >> 3;
660 ret = ab8500_charger_max_usb_curr(di,
661 (enum ab8500_charger_link_status) val);
667 * ab8500_charger_detect_usb_type() - get the type of usb connected
668 * @di: pointer to the ab8500_charger structure
670 * Detect the type of the plugged USB
671 * Returns error code in case of failure else 0 on success
673 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
679 * On getting the VBUS rising edge detect interrupt there
680 * is a 250ms delay after which the register UsbLineStatus
681 * is filled with valid data.
683 for (i = 0; i < 10; i++) {
685 ret = abx500_get_register_interruptible(di->dev,
686 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
689 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
692 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
693 AB8500_USB_LINE_STAT_REG, &val);
695 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
699 * Until the IT source register is read the UsbLineStatus
700 * register is not updated, hence doing the same
704 /* get the USB type */
705 val = (val & AB8500_USB_LINK_STATUS) >> 3;
709 ret = ab8500_charger_max_usb_curr(di,
710 (enum ab8500_charger_link_status) val);
716 * This array maps the raw hex value to charger voltage used by the AB8500
717 * Values taken from the UM0836
719 static int ab8500_charger_voltage_map[] = {
801 * This array maps the raw hex value to charger current used by the AB8500
802 * Values taken from the UM0836
804 static int ab8500_charger_current_map[] = {
823 * This array maps the raw hex value to VBUS input current used by the AB8500
824 * Values taken from the UM0836
826 static int ab8500_charger_vbus_in_curr_map[] = {
827 USB_CH_IP_CUR_LVL_0P05,
828 USB_CH_IP_CUR_LVL_0P09,
829 USB_CH_IP_CUR_LVL_0P19,
830 USB_CH_IP_CUR_LVL_0P29,
831 USB_CH_IP_CUR_LVL_0P38,
832 USB_CH_IP_CUR_LVL_0P45,
833 USB_CH_IP_CUR_LVL_0P5,
834 USB_CH_IP_CUR_LVL_0P6,
835 USB_CH_IP_CUR_LVL_0P7,
836 USB_CH_IP_CUR_LVL_0P8,
837 USB_CH_IP_CUR_LVL_0P9,
838 USB_CH_IP_CUR_LVL_1P0,
839 USB_CH_IP_CUR_LVL_1P1,
840 USB_CH_IP_CUR_LVL_1P3,
841 USB_CH_IP_CUR_LVL_1P4,
842 USB_CH_IP_CUR_LVL_1P5,
845 static int ab8500_voltage_to_regval(int voltage)
849 /* Special case for voltage below 3.5V */
850 if (voltage < ab8500_charger_voltage_map[0])
853 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
854 if (voltage < ab8500_charger_voltage_map[i])
858 /* If not last element, return error */
859 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
860 if (voltage == ab8500_charger_voltage_map[i])
866 static int ab8500_current_to_regval(int curr)
870 if (curr < ab8500_charger_current_map[0])
873 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
874 if (curr < ab8500_charger_current_map[i])
878 /* If not last element, return error */
879 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
880 if (curr == ab8500_charger_current_map[i])
886 static int ab8500_vbus_in_curr_to_regval(int curr)
890 if (curr < ab8500_charger_vbus_in_curr_map[0])
893 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
894 if (curr < ab8500_charger_vbus_in_curr_map[i])
898 /* If not last element, return error */
899 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
900 if (curr == ab8500_charger_vbus_in_curr_map[i])
907 * ab8500_charger_get_usb_cur() - get usb current
908 * @di: pointer to the ab8500_charger structre
910 * The usb stack provides the maximum current that can be drawn from
911 * the standard usb host. This will be in mA.
912 * This function converts current in mA to a value that can be written
913 * to the register. Returns -1 if charging is not allowed
915 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
917 switch (di->usb_state.usb_current) {
919 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
922 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
925 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
928 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
931 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
934 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
942 * ab8500_charger_set_current() - set charger current
943 * @di: pointer to the ab8500_charger structure
944 * @ich: charger current, in mA
945 * @reg: select what charger register to set
947 * Set charger current.
948 * There is no state machine in the AB to step up/down the charger
949 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
950 * charging is started. Instead we need to implement
951 * this charger current step-up/down here.
952 * Returns error code in case of failure else 0(on success)
954 static int ab8500_charger_set_current(struct ab8500_charger *di,
958 int curr_index, prev_curr_index, shift_value;
962 case AB8500_MCH_IPT_CURLVL_REG:
963 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
964 curr_index = ab8500_current_to_regval(ich);
966 case AB8500_USBCH_IPT_CRNTLVL_REG:
967 shift_value = VBUS_IN_CURR_LIM_SHIFT;
968 curr_index = ab8500_vbus_in_curr_to_regval(ich);
970 case AB8500_CH_OPT_CRNTLVL_REG:
972 curr_index = ab8500_current_to_regval(ich);
975 dev_err(di->dev, "%s current register not valid\n", __func__);
979 if (curr_index < 0) {
980 dev_err(di->dev, "requested current limit out-of-range\n");
984 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
987 dev_err(di->dev, "%s read failed\n", __func__);
990 prev_curr_index = (reg_value >> shift_value);
992 /* only update current if it's been changed */
993 if (prev_curr_index == curr_index)
996 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
999 if (prev_curr_index > curr_index) {
1000 for (i = prev_curr_index - 1; i >= curr_index; i--) {
1001 ret = abx500_set_register_interruptible(di->dev,
1002 AB8500_CHARGER, reg, (u8) i << shift_value);
1004 dev_err(di->dev, "%s write failed\n", __func__);
1007 usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
1010 for (i = prev_curr_index + 1; i <= curr_index; i++) {
1011 ret = abx500_set_register_interruptible(di->dev,
1012 AB8500_CHARGER, reg, (u8) i << shift_value);
1014 dev_err(di->dev, "%s write failed\n", __func__);
1017 usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
1024 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1025 * @di: pointer to the ab8500_charger structure
1026 * @ich_in: charger input current limit
1028 * Sets the current that can be drawn from the USB host
1029 * Returns error code in case of failure else 0(on success)
1031 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1036 /* We should always use to lowest current limit */
1037 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1039 switch (min_value) {
1041 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1042 min_value = USB_CH_IP_CUR_LVL_0P05;
1045 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1046 min_value = USB_CH_IP_CUR_LVL_0P45;
1052 return ab8500_charger_set_current(di, min_value,
1053 AB8500_USBCH_IPT_CRNTLVL_REG);
1057 * ab8500_charger_set_main_in_curr() - set main charger input current
1058 * @di: pointer to the ab8500_charger structure
1059 * @ich_in: input charger current, in mA
1061 * Set main charger input current.
1062 * Returns error code in case of failure else 0(on success)
1064 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1067 return ab8500_charger_set_current(di, ich_in,
1068 AB8500_MCH_IPT_CURLVL_REG);
1072 * ab8500_charger_set_output_curr() - set charger output current
1073 * @di: pointer to the ab8500_charger structure
1074 * @ich_out: output charger current, in mA
1076 * Set charger output current.
1077 * Returns error code in case of failure else 0(on success)
1079 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1082 return ab8500_charger_set_current(di, ich_out,
1083 AB8500_CH_OPT_CRNTLVL_REG);
1087 * ab8500_charger_led_en() - turn on/off chargign led
1088 * @di: pointer to the ab8500_charger structure
1089 * @on: flag to turn on/off the chargign led
1091 * Power ON/OFF charging LED indication
1092 * Returns error code in case of failure else 0(on success)
1094 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1099 /* Power ON charging LED indicator, set LED current to 5mA */
1100 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1101 AB8500_LED_INDICATOR_PWM_CTRL,
1102 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1104 dev_err(di->dev, "Power ON LED failed\n");
1107 /* LED indicator PWM duty cycle 252/256 */
1108 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1109 AB8500_LED_INDICATOR_PWM_DUTY,
1110 LED_INDICATOR_PWM_DUTY_252_256);
1112 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1116 /* Power off charging LED indicator */
1117 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1118 AB8500_LED_INDICATOR_PWM_CTRL,
1119 LED_INDICATOR_PWM_DIS);
1121 dev_err(di->dev, "Power-off LED failed\n");
1130 * ab8500_charger_ac_en() - enable or disable ac charging
1131 * @di: pointer to the ab8500_charger structure
1132 * @enable: enable/disable flag
1133 * @vset: charging voltage
1134 * @iset: charging current
1136 * Enable/Disable AC/Mains charging and turns on/off the charging led
1139 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1140 int enable, int vset, int iset)
1145 int input_curr_index;
1148 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1151 /* Check if AC is connected */
1152 if (!di->ac.charger_connected) {
1153 dev_err(di->dev, "AC charger not connected\n");
1157 /* Enable AC charging */
1158 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1161 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1162 * will be triggered everytime we enable the VDD ADC supply.
1163 * This will turn off charging for a short while.
1164 * It can be avoided by having the supply on when
1165 * there is a charger enabled. Normally the VDD ADC supply
1166 * is enabled everytime a GPADC conversion is triggered. We will
1167 * force it to be enabled from this driver to have
1168 * the GPADC module independant of the AB8500 chargers
1170 if (!di->vddadc_en_ac) {
1171 regulator_enable(di->regu);
1172 di->vddadc_en_ac = true;
1175 /* Check if the requested voltage or current is valid */
1176 volt_index = ab8500_voltage_to_regval(vset);
1177 curr_index = ab8500_current_to_regval(iset);
1178 input_curr_index = ab8500_current_to_regval(
1179 di->bm->chg_params->ac_curr_max);
1180 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1182 "Charger voltage or current too high, "
1183 "charging not started\n");
1187 /* ChVoltLevel: maximum battery charging voltage */
1188 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1189 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1191 dev_err(di->dev, "%s write failed\n", __func__);
1194 /* MainChInputCurr: current that can be drawn from the charger*/
1195 ret = ab8500_charger_set_main_in_curr(di,
1196 di->bm->chg_params->ac_curr_max);
1198 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1202 /* ChOutputCurentLevel: protected output current */
1203 ret = ab8500_charger_set_output_curr(di, iset);
1205 dev_err(di->dev, "%s "
1206 "Failed to set ChOutputCurentLevel\n",
1211 /* Check if VBAT overshoot control should be enabled */
1212 if (!di->bm->enable_overshoot)
1213 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1215 /* Enable Main Charger */
1216 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1217 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1219 dev_err(di->dev, "%s write failed\n", __func__);
1223 /* Power on charging LED indication */
1224 ret = ab8500_charger_led_en(di, true);
1226 dev_err(di->dev, "failed to enable LED\n");
1228 di->ac.charger_online = 1;
1230 /* Disable AC charging */
1231 if (is_ab8500_1p1_or_earlier(di->parent)) {
1233 * For ABB revision 1.0 and 1.1 there is a bug in the
1234 * watchdog logic. That means we have to continously
1235 * kick the charger watchdog even when no charger is
1236 * connected. This is only valid once the AC charger
1237 * has been enabled. This is a bug that is not handled
1238 * by the algorithm and the watchdog have to be kicked
1239 * by the charger driver when the AC charger
1243 queue_delayed_work(di->charger_wq,
1245 round_jiffies(WD_KICK_INTERVAL));
1249 * We can't turn off charging completely
1250 * due to a bug in AB8500 cut1.
1251 * If we do, charging will not start again.
1252 * That is why we set the lowest voltage
1253 * and current possible
1255 ret = abx500_set_register_interruptible(di->dev,
1257 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1260 "%s write failed\n", __func__);
1264 ret = ab8500_charger_set_output_curr(di, 0);
1266 dev_err(di->dev, "%s "
1267 "Failed to set ChOutputCurentLevel\n",
1272 ret = abx500_set_register_interruptible(di->dev,
1274 AB8500_MCH_CTRL1, 0);
1277 "%s write failed\n", __func__);
1282 ret = ab8500_charger_led_en(di, false);
1284 dev_err(di->dev, "failed to disable LED\n");
1286 di->ac.charger_online = 0;
1287 di->ac.wd_expired = false;
1289 /* Disable regulator if enabled */
1290 if (di->vddadc_en_ac) {
1291 regulator_disable(di->regu);
1292 di->vddadc_en_ac = false;
1295 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1297 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1303 * ab8500_charger_usb_en() - enable usb charging
1304 * @di: pointer to the ab8500_charger structure
1305 * @enable: enable/disable flag
1306 * @vset: charging voltage
1307 * @ich_out: charger output current
1309 * Enable/Disable USB charging and turns on/off the charging led respectively.
1310 * Returns error code in case of failure else 0(on success)
1312 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1313 int enable, int vset, int ich_out)
1320 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1323 /* Check if USB is connected */
1324 if (!di->usb.charger_connected) {
1325 dev_err(di->dev, "USB charger not connected\n");
1330 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1331 * will be triggered everytime we enable the VDD ADC supply.
1332 * This will turn off charging for a short while.
1333 * It can be avoided by having the supply on when
1334 * there is a charger enabled. Normally the VDD ADC supply
1335 * is enabled everytime a GPADC conversion is triggered. We will
1336 * force it to be enabled from this driver to have
1337 * the GPADC module independant of the AB8500 chargers
1339 if (!di->vddadc_en_usb) {
1340 regulator_enable(di->regu);
1341 di->vddadc_en_usb = true;
1344 /* Enable USB charging */
1345 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1347 /* Check if the requested voltage or current is valid */
1348 volt_index = ab8500_voltage_to_regval(vset);
1349 curr_index = ab8500_current_to_regval(ich_out);
1350 if (volt_index < 0 || curr_index < 0) {
1352 "Charger voltage or current too high, "
1353 "charging not started\n");
1357 /* ChVoltLevel: max voltage upto which battery can be charged */
1358 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1359 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1361 dev_err(di->dev, "%s write failed\n", __func__);
1364 /* USBChInputCurr: current that can be drawn from the usb */
1365 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1367 dev_err(di->dev, "setting USBChInputCurr failed\n");
1370 /* ChOutputCurentLevel: protected output current */
1371 ret = ab8500_charger_set_output_curr(di, ich_out);
1373 dev_err(di->dev, "%s "
1374 "Failed to set ChOutputCurentLevel\n",
1378 /* Check if VBAT overshoot control should be enabled */
1379 if (!di->bm->enable_overshoot)
1380 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1382 /* Enable USB Charger */
1383 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1384 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1386 dev_err(di->dev, "%s write failed\n", __func__);
1390 /* If success power on charging LED indication */
1391 ret = ab8500_charger_led_en(di, true);
1393 dev_err(di->dev, "failed to enable LED\n");
1395 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1397 di->usb.charger_online = 1;
1399 /* Disable USB charging */
1400 ret = abx500_set_register_interruptible(di->dev,
1402 AB8500_USBCH_CTRL1_REG, 0);
1405 "%s write failed\n", __func__);
1409 ret = ab8500_charger_led_en(di, false);
1411 dev_err(di->dev, "failed to disable LED\n");
1413 di->usb.charger_online = 0;
1414 di->usb.wd_expired = false;
1416 /* Disable regulator if enabled */
1417 if (di->vddadc_en_usb) {
1418 regulator_disable(di->regu);
1419 di->vddadc_en_usb = false;
1422 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1424 /* Cancel any pending Vbat check work */
1425 if (delayed_work_pending(&di->check_vbat_work))
1426 cancel_delayed_work(&di->check_vbat_work);
1429 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1435 * ab8500_charger_watchdog_kick() - kick charger watchdog
1436 * @di: pointer to the ab8500_charger structure
1438 * Kick charger watchdog
1439 * Returns error code in case of failure else 0(on success)
1441 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1444 struct ab8500_charger *di;
1446 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1447 di = to_ab8500_charger_ac_device_info(charger);
1448 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1449 di = to_ab8500_charger_usb_device_info(charger);
1453 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1454 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1456 dev_err(di->dev, "Failed to kick WD!\n");
1462 * ab8500_charger_update_charger_current() - update charger current
1463 * @di: pointer to the ab8500_charger structure
1465 * Update the charger output current for the specified charger
1466 * Returns error code in case of failure else 0(on success)
1468 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1472 struct ab8500_charger *di;
1474 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1475 di = to_ab8500_charger_ac_device_info(charger);
1476 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1477 di = to_ab8500_charger_usb_device_info(charger);
1481 ret = ab8500_charger_set_output_curr(di, ich_out);
1483 dev_err(di->dev, "%s "
1484 "Failed to set ChOutputCurentLevel\n",
1489 /* Reset the main and usb drop input current measurement counter */
1490 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1491 AB8500_CHARGER_CTRL,
1494 dev_err(di->dev, "%s write failed\n", __func__);
1501 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1503 struct power_supply *psy;
1504 struct power_supply *ext;
1505 struct ab8500_charger *di;
1506 union power_supply_propval ret;
1508 bool psy_found = false;
1509 struct ux500_charger *usb_chg;
1511 usb_chg = (struct ux500_charger *)data;
1512 psy = &usb_chg->psy;
1514 di = to_ab8500_charger_usb_device_info(usb_chg);
1516 ext = dev_get_drvdata(dev);
1518 /* For all psy where the driver name appears in any supplied_to */
1519 for (i = 0; i < ext->num_supplicants; i++) {
1520 if (!strcmp(ext->supplied_to[i], psy->name))
1527 /* Go through all properties for the psy */
1528 for (j = 0; j < ext->num_properties; j++) {
1529 enum power_supply_property prop;
1530 prop = ext->properties[j];
1532 if (ext->get_property(ext, prop, &ret))
1536 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1537 switch (ext->type) {
1538 case POWER_SUPPLY_TYPE_BATTERY:
1539 di->vbat = ret.intval / 1000;
1553 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1554 * @work pointer to the work_struct structure
1556 * Due to a asic bug it is necessary to lower the input current to the vbus
1557 * charger when charging with at some specific levels. This issue is only valid
1558 * for below a certain battery voltage. This function makes sure that the
1559 * the allowed current limit isn't exceeded.
1561 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1564 struct ab8500_charger *di = container_of(work,
1565 struct ab8500_charger, check_vbat_work.work);
1567 class_for_each_device(power_supply_class, NULL,
1568 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1570 /* First run old_vbat is 0. */
1571 if (di->old_vbat == 0)
1572 di->old_vbat = di->vbat;
1574 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1575 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1576 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1577 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1579 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1580 " old: %d\n", di->max_usb_in_curr, di->vbat,
1582 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1583 power_supply_changed(&di->usb_chg.psy);
1586 di->old_vbat = di->vbat;
1589 * No need to check the battery voltage every second when not close to
1592 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1593 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1596 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1600 * ab8500_charger_check_hw_failure_work() - check main charger failure
1601 * @work: pointer to the work_struct structure
1603 * Work queue function for checking the main charger status
1605 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1610 struct ab8500_charger *di = container_of(work,
1611 struct ab8500_charger, check_hw_failure_work.work);
1613 /* Check if the status bits for HW failure is still active */
1614 if (di->flags.mainextchnotok) {
1615 ret = abx500_get_register_interruptible(di->dev,
1616 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1618 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1621 if (!(reg_value & MAIN_CH_NOK)) {
1622 di->flags.mainextchnotok = false;
1623 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1626 if (di->flags.vbus_ovv) {
1627 ret = abx500_get_register_interruptible(di->dev,
1628 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1631 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1634 if (!(reg_value & VBUS_OVV_TH)) {
1635 di->flags.vbus_ovv = false;
1636 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1639 /* If we still have a failure, schedule a new check */
1640 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1641 queue_delayed_work(di->charger_wq,
1642 &di->check_hw_failure_work, round_jiffies(HZ));
1647 * ab8500_charger_kick_watchdog_work() - kick the watchdog
1648 * @work: pointer to the work_struct structure
1650 * Work queue function for kicking the charger watchdog.
1652 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
1653 * logic. That means we have to continously kick the charger
1654 * watchdog even when no charger is connected. This is only
1655 * valid once the AC charger has been enabled. This is
1656 * a bug that is not handled by the algorithm and the
1657 * watchdog have to be kicked by the charger driver
1658 * when the AC charger is disabled
1660 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
1664 struct ab8500_charger *di = container_of(work,
1665 struct ab8500_charger, kick_wd_work.work);
1667 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1668 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1670 dev_err(di->dev, "Failed to kick WD!\n");
1672 /* Schedule a new watchdog kick */
1673 queue_delayed_work(di->charger_wq,
1674 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
1678 * ab8500_charger_ac_work() - work to get and set main charger status
1679 * @work: pointer to the work_struct structure
1681 * Work queue function for checking the main charger status
1683 static void ab8500_charger_ac_work(struct work_struct *work)
1687 struct ab8500_charger *di = container_of(work,
1688 struct ab8500_charger, ac_work);
1691 * Since we can't be sure that the events are received
1692 * synchronously, we have the check if the main charger is
1693 * connected by reading the status register
1695 ret = ab8500_charger_detect_chargers(di);
1699 if (ret & AC_PW_CONN) {
1700 di->ac.charger_connected = 1;
1703 di->ac.charger_connected = 0;
1706 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1707 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
1711 * ab8500_charger_detect_usb_type_work() - work to detect USB type
1712 * @work: Pointer to the work_struct structure
1714 * Detect the type of USB plugged
1716 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
1720 struct ab8500_charger *di = container_of(work,
1721 struct ab8500_charger, detect_usb_type_work);
1724 * Since we can't be sure that the events are received
1725 * synchronously, we have the check if is
1726 * connected by reading the status register
1728 ret = ab8500_charger_detect_chargers(di);
1732 if (!(ret & USB_PW_CONN)) {
1733 di->vbus_detected = 0;
1734 ab8500_charger_set_usb_connected(di, false);
1735 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1737 di->vbus_detected = 1;
1739 if (is_ab8500_1p1_or_earlier(di->parent)) {
1740 ret = ab8500_charger_detect_usb_type(di);
1742 ab8500_charger_set_usb_connected(di, true);
1743 ab8500_power_supply_changed(di,
1747 /* For ABB cut2.0 and onwards we have an IRQ,
1748 * USB_LINK_STATUS that will be triggered when the USB
1749 * link status changes. The exception is USB connected
1750 * during startup. Then we don't get a
1751 * USB_LINK_STATUS IRQ
1753 if (di->vbus_detected_start) {
1754 di->vbus_detected_start = false;
1755 ret = ab8500_charger_detect_usb_type(di);
1757 ab8500_charger_set_usb_connected(di,
1759 ab8500_power_supply_changed(di,
1768 * ab8500_charger_usb_link_status_work() - work to detect USB type
1769 * @work: pointer to the work_struct structure
1771 * Detect the type of USB plugged
1773 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
1777 struct ab8500_charger *di = container_of(work,
1778 struct ab8500_charger, usb_link_status_work);
1781 * Since we can't be sure that the events are received
1782 * synchronously, we have the check if is
1783 * connected by reading the status register
1785 ret = ab8500_charger_detect_chargers(di);
1789 if (!(ret & USB_PW_CONN)) {
1790 di->vbus_detected = 0;
1791 ab8500_charger_set_usb_connected(di, false);
1792 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1794 di->vbus_detected = 1;
1795 ret = ab8500_charger_read_usb_type(di);
1797 /* Update maximum input current */
1798 ret = ab8500_charger_set_vbus_in_curr(di,
1799 di->max_usb_in_curr);
1803 ab8500_charger_set_usb_connected(di, true);
1804 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1805 } else if (ret == -ENXIO) {
1806 /* No valid charger type detected */
1807 ab8500_charger_set_usb_connected(di, false);
1808 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1813 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
1816 unsigned long flags;
1818 struct ab8500_charger *di = container_of(work,
1819 struct ab8500_charger, usb_state_changed_work);
1821 if (!di->vbus_detected)
1824 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
1825 di->usb_state.usb_changed = false;
1826 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
1829 * wait for some time until you get updates from the usb stack
1830 * and negotiations are completed
1834 if (di->usb_state.usb_changed)
1837 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
1838 __func__, di->usb_state.state, di->usb_state.usb_current);
1840 switch (di->usb_state.state) {
1841 case AB8500_BM_USB_STATE_RESET_HS:
1842 case AB8500_BM_USB_STATE_RESET_FS:
1843 case AB8500_BM_USB_STATE_SUSPEND:
1844 case AB8500_BM_USB_STATE_MAX:
1845 ab8500_charger_set_usb_connected(di, false);
1846 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1849 case AB8500_BM_USB_STATE_RESUME:
1851 * when suspend->resume there should be delay
1852 * of 1sec for enabling charging
1855 /* Intentional fall through */
1856 case AB8500_BM_USB_STATE_CONFIGURED:
1858 * USB is configured, enable charging with the charging
1859 * input current obtained from USB driver
1861 if (!ab8500_charger_get_usb_cur(di)) {
1862 /* Update maximum input current */
1863 ret = ab8500_charger_set_vbus_in_curr(di,
1864 di->max_usb_in_curr);
1868 ab8500_charger_set_usb_connected(di, true);
1869 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1879 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
1880 * @work: pointer to the work_struct structure
1882 * Work queue function for checking the USB charger Not OK status
1884 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
1890 struct ab8500_charger *di = container_of(work,
1891 struct ab8500_charger, check_usbchgnotok_work.work);
1893 /* Check if the status bit for usbchargernotok is still active */
1894 ret = abx500_get_register_interruptible(di->dev,
1895 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
1897 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1900 prev_status = di->flags.usbchargernotok;
1902 if (reg_value & VBUS_CH_NOK) {
1903 di->flags.usbchargernotok = true;
1904 /* Check again in 1sec */
1905 queue_delayed_work(di->charger_wq,
1906 &di->check_usbchgnotok_work, HZ);
1908 di->flags.usbchargernotok = false;
1909 di->flags.vbus_collapse = false;
1912 if (prev_status != di->flags.usbchargernotok)
1913 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1917 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
1918 * @work: pointer to the work_struct structure
1920 * Work queue function for checking the Main thermal prot status
1922 static void ab8500_charger_check_main_thermal_prot_work(
1923 struct work_struct *work)
1928 struct ab8500_charger *di = container_of(work,
1929 struct ab8500_charger, check_main_thermal_prot_work);
1931 /* Check if the status bit for main_thermal_prot is still active */
1932 ret = abx500_get_register_interruptible(di->dev,
1933 AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value);
1935 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1938 if (reg_value & MAIN_CH_TH_PROT)
1939 di->flags.main_thermal_prot = true;
1941 di->flags.main_thermal_prot = false;
1943 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1947 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
1948 * @work: pointer to the work_struct structure
1950 * Work queue function for checking the USB thermal prot status
1952 static void ab8500_charger_check_usb_thermal_prot_work(
1953 struct work_struct *work)
1958 struct ab8500_charger *di = container_of(work,
1959 struct ab8500_charger, check_usb_thermal_prot_work);
1961 /* Check if the status bit for usb_thermal_prot is still active */
1962 ret = abx500_get_register_interruptible(di->dev,
1963 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value);
1965 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1968 if (reg_value & USB_CH_TH_PROT)
1969 di->flags.usb_thermal_prot = true;
1971 di->flags.usb_thermal_prot = false;
1973 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1977 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
1978 * @irq: interrupt number
1979 * @_di: pointer to the ab8500_charger structure
1981 * Returns IRQ status(IRQ_HANDLED)
1983 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
1985 struct ab8500_charger *di = _di;
1987 dev_dbg(di->dev, "Main charger unplugged\n");
1988 queue_work(di->charger_wq, &di->ac_work);
1994 * ab8500_charger_mainchplugdet_handler() - main charger plugged
1995 * @irq: interrupt number
1996 * @_di: pointer to the ab8500_charger structure
1998 * Returns IRQ status(IRQ_HANDLED)
2000 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2002 struct ab8500_charger *di = _di;
2004 dev_dbg(di->dev, "Main charger plugged\n");
2005 queue_work(di->charger_wq, &di->ac_work);
2011 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2012 * @irq: interrupt number
2013 * @_di: pointer to the ab8500_charger structure
2015 * Returns IRQ status(IRQ_HANDLED)
2017 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2019 struct ab8500_charger *di = _di;
2021 dev_dbg(di->dev, "Main charger not ok\n");
2022 di->flags.mainextchnotok = true;
2023 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2025 /* Schedule a new HW failure check */
2026 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2032 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2033 * thermal protection threshold
2034 * @irq: interrupt number
2035 * @_di: pointer to the ab8500_charger structure
2037 * Returns IRQ status(IRQ_HANDLED)
2039 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2041 struct ab8500_charger *di = _di;
2044 "Die temp above Main charger thermal protection threshold\n");
2045 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2051 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2052 * thermal protection threshold
2053 * @irq: interrupt number
2054 * @_di: pointer to the ab8500_charger structure
2056 * Returns IRQ status(IRQ_HANDLED)
2058 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2060 struct ab8500_charger *di = _di;
2063 "Die temp ok for Main charger thermal protection threshold\n");
2064 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2070 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2071 * @irq: interrupt number
2072 * @_di: pointer to the ab8500_charger structure
2074 * Returns IRQ status(IRQ_HANDLED)
2076 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2078 struct ab8500_charger *di = _di;
2080 dev_dbg(di->dev, "VBUS falling detected\n");
2081 queue_work(di->charger_wq, &di->detect_usb_type_work);
2087 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2088 * @irq: interrupt number
2089 * @_di: pointer to the ab8500_charger structure
2091 * Returns IRQ status(IRQ_HANDLED)
2093 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2095 struct ab8500_charger *di = _di;
2097 di->vbus_detected = true;
2098 dev_dbg(di->dev, "VBUS rising detected\n");
2099 queue_work(di->charger_wq, &di->detect_usb_type_work);
2105 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2106 * @irq: interrupt number
2107 * @_di: pointer to the ab8500_charger structure
2109 * Returns IRQ status(IRQ_HANDLED)
2111 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2113 struct ab8500_charger *di = _di;
2115 dev_dbg(di->dev, "USB link status changed\n");
2117 queue_work(di->charger_wq, &di->usb_link_status_work);
2123 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2124 * thermal protection threshold
2125 * @irq: interrupt number
2126 * @_di: pointer to the ab8500_charger structure
2128 * Returns IRQ status(IRQ_HANDLED)
2130 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2132 struct ab8500_charger *di = _di;
2135 "Die temp above USB charger thermal protection threshold\n");
2136 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2142 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2143 * thermal protection threshold
2144 * @irq: interrupt number
2145 * @_di: pointer to the ab8500_charger structure
2147 * Returns IRQ status(IRQ_HANDLED)
2149 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2151 struct ab8500_charger *di = _di;
2154 "Die temp ok for USB charger thermal protection threshold\n");
2155 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2161 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2162 * @irq: interrupt number
2163 * @_di: pointer to the ab8500_charger structure
2165 * Returns IRQ status(IRQ_HANDLED)
2167 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2169 struct ab8500_charger *di = _di;
2171 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2172 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2178 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2179 * @irq: interrupt number
2180 * @_di: pointer to the ab8500_charger structure
2182 * Returns IRQ status(IRQ_HANDLED)
2184 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2186 struct ab8500_charger *di = _di;
2188 dev_dbg(di->dev, "Charger watchdog expired\n");
2191 * The charger that was online when the watchdog expired
2192 * needs to be restarted for charging to start again
2194 if (di->ac.charger_online) {
2195 di->ac.wd_expired = true;
2196 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2198 if (di->usb.charger_online) {
2199 di->usb.wd_expired = true;
2200 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2207 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2208 * @irq: interrupt number
2209 * @_di: pointer to the ab8500_charger structure
2211 * Returns IRQ status(IRQ_HANDLED)
2213 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2215 struct ab8500_charger *di = _di;
2217 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2218 di->flags.vbus_ovv = true;
2219 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2221 /* Schedule a new HW failure check */
2222 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2228 * ab8500_charger_ac_get_property() - get the ac/mains properties
2229 * @psy: pointer to the power_supply structure
2230 * @psp: pointer to the power_supply_property structure
2231 * @val: pointer to the power_supply_propval union
2233 * This function gets called when an application tries to get the ac/mains
2234 * properties by reading the sysfs files.
2235 * AC/Mains properties are online, present and voltage.
2236 * online: ac/mains charging is in progress or not
2237 * present: presence of the ac/mains
2238 * voltage: AC/Mains voltage
2239 * Returns error code in case of failure else 0(on success)
2241 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2242 enum power_supply_property psp,
2243 union power_supply_propval *val)
2245 struct ab8500_charger *di;
2247 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2250 case POWER_SUPPLY_PROP_HEALTH:
2251 if (di->flags.mainextchnotok)
2252 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2253 else if (di->ac.wd_expired || di->usb.wd_expired)
2254 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2255 else if (di->flags.main_thermal_prot)
2256 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2258 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2260 case POWER_SUPPLY_PROP_ONLINE:
2261 val->intval = di->ac.charger_online;
2263 case POWER_SUPPLY_PROP_PRESENT:
2264 val->intval = di->ac.charger_connected;
2266 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2267 di->ac.charger_voltage = ab8500_charger_get_ac_voltage(di);
2268 val->intval = di->ac.charger_voltage * 1000;
2270 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2272 * This property is used to indicate when CV mode is entered
2273 * for the AC charger
2275 di->ac.cv_active = ab8500_charger_ac_cv(di);
2276 val->intval = di->ac.cv_active;
2278 case POWER_SUPPLY_PROP_CURRENT_NOW:
2279 val->intval = ab8500_charger_get_ac_current(di) * 1000;
2288 * ab8500_charger_usb_get_property() - get the usb properties
2289 * @psy: pointer to the power_supply structure
2290 * @psp: pointer to the power_supply_property structure
2291 * @val: pointer to the power_supply_propval union
2293 * This function gets called when an application tries to get the usb
2294 * properties by reading the sysfs files.
2295 * USB properties are online, present and voltage.
2296 * online: usb charging is in progress or not
2297 * present: presence of the usb
2298 * voltage: vbus voltage
2299 * Returns error code in case of failure else 0(on success)
2301 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2302 enum power_supply_property psp,
2303 union power_supply_propval *val)
2305 struct ab8500_charger *di;
2307 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2310 case POWER_SUPPLY_PROP_HEALTH:
2311 if (di->flags.usbchargernotok)
2312 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2313 else if (di->ac.wd_expired || di->usb.wd_expired)
2314 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2315 else if (di->flags.usb_thermal_prot)
2316 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2317 else if (di->flags.vbus_ovv)
2318 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2320 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2322 case POWER_SUPPLY_PROP_ONLINE:
2323 val->intval = di->usb.charger_online;
2325 case POWER_SUPPLY_PROP_PRESENT:
2326 val->intval = di->usb.charger_connected;
2328 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2329 di->usb.charger_voltage = ab8500_charger_get_vbus_voltage(di);
2330 val->intval = di->usb.charger_voltage * 1000;
2332 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2334 * This property is used to indicate when CV mode is entered
2335 * for the USB charger
2337 di->usb.cv_active = ab8500_charger_usb_cv(di);
2338 val->intval = di->usb.cv_active;
2340 case POWER_SUPPLY_PROP_CURRENT_NOW:
2341 val->intval = ab8500_charger_get_usb_current(di) * 1000;
2343 case POWER_SUPPLY_PROP_CURRENT_AVG:
2345 * This property is used to indicate when VBUS has collapsed
2346 * due to too high output current from the USB charger
2348 if (di->flags.vbus_collapse)
2360 * ab8500_charger_init_hw_registers() - Set up charger related registers
2361 * @di: pointer to the ab8500_charger structure
2363 * Set up charger OVV, watchdog and maximum voltage registers as well as
2364 * charging of the backup battery
2366 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
2370 /* Setup maximum charger current and voltage for ABB cut2.0 */
2371 if (!is_ab8500_1p1_or_earlier(di->parent)) {
2372 ret = abx500_set_register_interruptible(di->dev,
2374 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
2377 "failed to set CH_VOLT_LVL_MAX_REG\n");
2381 ret = abx500_set_register_interruptible(di->dev,
2383 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
2386 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2391 /* VBUS OVV set to 6.3V and enable automatic current limitiation */
2392 ret = abx500_set_register_interruptible(di->dev,
2394 AB8500_USBCH_CTRL2_REG,
2395 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
2397 dev_err(di->dev, "failed to set VBUS OVV\n");
2401 /* Enable main watchdog in OTP */
2402 ret = abx500_set_register_interruptible(di->dev,
2403 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
2405 dev_err(di->dev, "failed to enable main WD in OTP\n");
2409 /* Enable main watchdog */
2410 ret = abx500_set_register_interruptible(di->dev,
2411 AB8500_SYS_CTRL2_BLOCK,
2412 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
2414 dev_err(di->dev, "faile to enable main watchdog\n");
2419 * Due to internal synchronisation, Enable and Kick watchdog bits
2420 * cannot be enabled in a single write.
2421 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
2422 * between writing Enable then Kick bits.
2426 /* Kick main watchdog */
2427 ret = abx500_set_register_interruptible(di->dev,
2428 AB8500_SYS_CTRL2_BLOCK,
2429 AB8500_MAIN_WDOG_CTRL_REG,
2430 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
2432 dev_err(di->dev, "failed to kick main watchdog\n");
2436 /* Disable main watchdog */
2437 ret = abx500_set_register_interruptible(di->dev,
2438 AB8500_SYS_CTRL2_BLOCK,
2439 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
2441 dev_err(di->dev, "failed to disable main watchdog\n");
2445 /* Set watchdog timeout */
2446 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2447 AB8500_CH_WD_TIMER_REG, WD_TIMER);
2449 dev_err(di->dev, "failed to set charger watchdog timeout\n");
2453 /* Backup battery voltage and current */
2454 ret = abx500_set_register_interruptible(di->dev,
2456 AB8500_RTC_BACKUP_CHG_REG,
2457 di->bm->bkup_bat_v |
2458 di->bm->bkup_bat_i);
2460 dev_err(di->dev, "failed to setup backup battery charging\n");
2464 /* Enable backup battery charging */
2465 abx500_mask_and_set_register_interruptible(di->dev,
2466 AB8500_RTC, AB8500_RTC_CTRL_REG,
2467 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
2469 dev_err(di->dev, "%s mask and set failed\n", __func__);
2476 * ab8500 charger driver interrupts and their respective isr
2478 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
2479 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
2480 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
2481 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
2482 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
2483 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
2484 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
2485 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
2486 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
2487 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
2488 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
2489 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
2490 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
2491 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
2494 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
2495 unsigned long event, void *power)
2497 struct ab8500_charger *di =
2498 container_of(nb, struct ab8500_charger, nb);
2499 enum ab8500_usb_state bm_usb_state;
2500 unsigned mA = *((unsigned *)power);
2502 if (event != USB_EVENT_VBUS) {
2503 dev_dbg(di->dev, "not a standard host, returning\n");
2507 /* TODO: State is fabricate here. See if charger really needs USB
2508 * state or if mA is enough
2510 if ((di->usb_state.usb_current == 2) && (mA > 2))
2511 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
2513 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
2515 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
2516 else if (mA >= 8) /* 8, 100, 500 */
2517 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
2518 else /* Should never occur */
2519 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
2521 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
2522 __func__, bm_usb_state, mA);
2524 spin_lock(&di->usb_state.usb_lock);
2525 di->usb_state.usb_changed = true;
2526 spin_unlock(&di->usb_state.usb_lock);
2528 di->usb_state.state = bm_usb_state;
2529 di->usb_state.usb_current = mA;
2531 queue_work(di->charger_wq, &di->usb_state_changed_work);
2536 #if defined(CONFIG_PM)
2537 static int ab8500_charger_resume(struct platform_device *pdev)
2540 struct ab8500_charger *di = platform_get_drvdata(pdev);
2543 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2544 * logic. That means we have to continously kick the charger
2545 * watchdog even when no charger is connected. This is only
2546 * valid once the AC charger has been enabled. This is
2547 * a bug that is not handled by the algorithm and the
2548 * watchdog have to be kicked by the charger driver
2549 * when the AC charger is disabled
2551 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
2552 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2553 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2555 dev_err(di->dev, "Failed to kick WD!\n");
2557 /* If not already pending start a new timer */
2558 if (!delayed_work_pending(
2559 &di->kick_wd_work)) {
2560 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
2561 round_jiffies(WD_KICK_INTERVAL));
2565 /* If we still have a HW failure, schedule a new check */
2566 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2567 queue_delayed_work(di->charger_wq,
2568 &di->check_hw_failure_work, 0);
2574 static int ab8500_charger_suspend(struct platform_device *pdev,
2577 struct ab8500_charger *di = platform_get_drvdata(pdev);
2579 /* Cancel any pending HW failure check */
2580 if (delayed_work_pending(&di->check_hw_failure_work))
2581 cancel_delayed_work(&di->check_hw_failure_work);
2586 #define ab8500_charger_suspend NULL
2587 #define ab8500_charger_resume NULL
2590 static int ab8500_charger_remove(struct platform_device *pdev)
2592 struct ab8500_charger *di = platform_get_drvdata(pdev);
2595 /* Disable AC charging */
2596 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
2598 /* Disable USB charging */
2599 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
2601 /* Disable interrupts */
2602 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2603 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2607 /* Backup battery voltage and current disable */
2608 ret = abx500_mask_and_set_register_interruptible(di->dev,
2609 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
2611 dev_err(di->dev, "%s mask and set failed\n", __func__);
2613 usb_unregister_notifier(di->usb_phy, &di->nb);
2614 usb_put_phy(di->usb_phy);
2616 /* Delete the work queue */
2617 destroy_workqueue(di->charger_wq);
2619 flush_scheduled_work();
2620 power_supply_unregister(&di->usb_chg.psy);
2621 power_supply_unregister(&di->ac_chg.psy);
2622 platform_set_drvdata(pdev, NULL);
2627 static char *supply_interface[] = {
2633 static int ab8500_charger_probe(struct platform_device *pdev)
2635 struct device_node *np = pdev->dev.of_node;
2636 struct abx500_bm_data *plat = pdev->dev.platform_data;
2637 struct ab8500_charger *di;
2638 int irq, i, charger_status, ret = 0;
2640 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
2642 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
2647 dev_err(&pdev->dev, "no battery management data supplied\n");
2653 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
2655 dev_err(&pdev->dev, "failed to get battery information\n");
2658 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
2660 di->autopower_cfg = false;
2662 /* get parent data */
2663 di->dev = &pdev->dev;
2664 di->parent = dev_get_drvdata(pdev->dev.parent);
2665 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2667 /* initialize lock */
2668 spin_lock_init(&di->usb_state.usb_lock);
2670 di->autopower = false;
2673 /* power_supply base class */
2674 di->ac_chg.psy.name = "ab8500_ac";
2675 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
2676 di->ac_chg.psy.properties = ab8500_charger_ac_props;
2677 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
2678 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
2679 di->ac_chg.psy.supplied_to = supply_interface;
2680 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
2681 /* ux500_charger sub-class */
2682 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
2683 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2684 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2685 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
2686 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2687 di->ac_chg.max_out_curr = ab8500_charger_current_map[
2688 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2691 /* power_supply base class */
2692 di->usb_chg.psy.name = "ab8500_usb";
2693 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
2694 di->usb_chg.psy.properties = ab8500_charger_usb_props;
2695 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
2696 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
2697 di->usb_chg.psy.supplied_to = supply_interface;
2698 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
2699 /* ux500_charger sub-class */
2700 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
2701 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2702 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2703 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
2704 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2705 di->usb_chg.max_out_curr = ab8500_charger_current_map[
2706 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2709 /* Create a work queue for the charger */
2711 create_singlethread_workqueue("ab8500_charger_wq");
2712 if (di->charger_wq == NULL) {
2713 dev_err(di->dev, "failed to create work queue\n");
2717 /* Init work for HW failure check */
2718 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
2719 ab8500_charger_check_hw_failure_work);
2720 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
2721 ab8500_charger_check_usbchargernotok_work);
2724 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2725 * logic. That means we have to continously kick the charger
2726 * watchdog even when no charger is connected. This is only
2727 * valid once the AC charger has been enabled. This is
2728 * a bug that is not handled by the algorithm and the
2729 * watchdog have to be kicked by the charger driver
2730 * when the AC charger is disabled
2732 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
2733 ab8500_charger_kick_watchdog_work);
2735 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
2736 ab8500_charger_check_vbat_work);
2738 /* Init work for charger detection */
2739 INIT_WORK(&di->usb_link_status_work,
2740 ab8500_charger_usb_link_status_work);
2741 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
2742 INIT_WORK(&di->detect_usb_type_work,
2743 ab8500_charger_detect_usb_type_work);
2745 INIT_WORK(&di->usb_state_changed_work,
2746 ab8500_charger_usb_state_changed_work);
2748 /* Init work for checking HW status */
2749 INIT_WORK(&di->check_main_thermal_prot_work,
2750 ab8500_charger_check_main_thermal_prot_work);
2751 INIT_WORK(&di->check_usb_thermal_prot_work,
2752 ab8500_charger_check_usb_thermal_prot_work);
2755 * VDD ADC supply needs to be enabled from this driver when there
2756 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
2757 * interrupts during charging
2759 di->regu = devm_regulator_get(di->dev, "vddadc");
2760 if (IS_ERR(di->regu)) {
2761 ret = PTR_ERR(di->regu);
2762 dev_err(di->dev, "failed to get vddadc regulator\n");
2763 goto free_charger_wq;
2767 /* Initialize OVV, and other registers */
2768 ret = ab8500_charger_init_hw_registers(di);
2770 dev_err(di->dev, "failed to initialize ABB registers\n");
2771 goto free_charger_wq;
2774 /* Register AC charger class */
2775 ret = power_supply_register(di->dev, &di->ac_chg.psy);
2777 dev_err(di->dev, "failed to register AC charger\n");
2778 goto free_charger_wq;
2781 /* Register USB charger class */
2782 ret = power_supply_register(di->dev, &di->usb_chg.psy);
2784 dev_err(di->dev, "failed to register USB charger\n");
2788 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
2789 if (IS_ERR_OR_NULL(di->usb_phy)) {
2790 dev_err(di->dev, "failed to get usb transceiver\n");
2794 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
2795 ret = usb_register_notifier(di->usb_phy, &di->nb);
2797 dev_err(di->dev, "failed to register usb notifier\n");
2801 /* Identify the connected charger types during startup */
2802 charger_status = ab8500_charger_detect_chargers(di);
2803 if (charger_status & AC_PW_CONN) {
2804 di->ac.charger_connected = 1;
2806 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2807 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2810 if (charger_status & USB_PW_CONN) {
2811 dev_dbg(di->dev, "VBUS Detect during startup\n");
2812 di->vbus_detected = true;
2813 di->vbus_detected_start = true;
2814 queue_work(di->charger_wq,
2815 &di->detect_usb_type_work);
2818 /* Register interrupts */
2819 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2820 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2821 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
2822 IRQF_SHARED | IRQF_NO_SUSPEND,
2823 ab8500_charger_irq[i].name, di);
2826 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
2827 , ab8500_charger_irq[i].name, irq, ret);
2830 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
2831 ab8500_charger_irq[i].name, irq, ret);
2834 platform_set_drvdata(pdev, di);
2839 usb_unregister_notifier(di->usb_phy, &di->nb);
2841 /* We also have to free all successfully registered irqs */
2842 for (i = i - 1; i >= 0; i--) {
2843 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2847 usb_put_phy(di->usb_phy);
2849 power_supply_unregister(&di->usb_chg.psy);
2851 power_supply_unregister(&di->ac_chg.psy);
2853 destroy_workqueue(di->charger_wq);
2857 static const struct of_device_id ab8500_charger_match[] = {
2858 { .compatible = "stericsson,ab8500-charger", },
2862 static struct platform_driver ab8500_charger_driver = {
2863 .probe = ab8500_charger_probe,
2864 .remove = ab8500_charger_remove,
2865 .suspend = ab8500_charger_suspend,
2866 .resume = ab8500_charger_resume,
2868 .name = "ab8500-charger",
2869 .owner = THIS_MODULE,
2870 .of_match_table = ab8500_charger_match,
2874 static int __init ab8500_charger_init(void)
2876 return platform_driver_register(&ab8500_charger_driver);
2879 static void __exit ab8500_charger_exit(void)
2881 platform_driver_unregister(&ab8500_charger_driver);
2884 subsys_initcall_sync(ab8500_charger_init);
2885 module_exit(ab8500_charger_exit);
2887 MODULE_LICENSE("GPL v2");
2888 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
2889 MODULE_ALIAS("platform:ab8500-charger");
2890 MODULE_DESCRIPTION("AB8500 charger management driver");