1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) ST-Ericsson SA 2012
4  *
5  * Charger driver for AB8500
6  *
7  * Author:
8  *	Johan Palsson <johan.palsson@stericsson.com>
9  *	Karl Komierowski <karl.komierowski@stericsson.com>
10  *	Arun R Murthy <arun.murthy@stericsson.com>
11  */
12 
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/notifier.h>
19 #include <linux/slab.h>
20 #include <linux/platform_device.h>
21 #include <linux/power_supply.h>
22 #include <linux/completion.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/err.h>
25 #include <linux/workqueue.h>
26 #include <linux/kobject.h>
27 #include <linux/of.h>
28 #include <linux/mfd/core.h>
29 #include <linux/mfd/abx500/ab8500.h>
30 #include <linux/mfd/abx500.h>
31 #include <linux/usb/otg.h>
32 #include <linux/mutex.h>
33 #include <linux/iio/consumer.h>
34 
35 #include "ab8500-bm.h"
36 #include "ab8500-chargalg.h"
37 
38 /* Charger constants */
39 #define NO_PW_CONN			0
40 #define AC_PW_CONN			1
41 #define USB_PW_CONN			2
42 
43 #define MAIN_WDOG_ENA			0x01
44 #define MAIN_WDOG_KICK			0x02
45 #define MAIN_WDOG_DIS			0x00
46 #define CHARG_WD_KICK			0x01
47 #define MAIN_CH_ENA			0x01
48 #define MAIN_CH_NO_OVERSHOOT_ENA_N	0x02
49 #define USB_CH_ENA			0x01
50 #define USB_CHG_NO_OVERSHOOT_ENA_N	0x02
51 #define MAIN_CH_DET			0x01
52 #define MAIN_CH_CV_ON			0x04
53 #define USB_CH_CV_ON			0x08
54 #define VBUS_DET_DBNC100		0x02
55 #define VBUS_DET_DBNC1			0x01
56 #define OTP_ENABLE_WD			0x01
57 #define DROP_COUNT_RESET		0x01
58 #define USB_CH_DET			0x01
59 
60 #define MAIN_CH_INPUT_CURR_SHIFT	4
61 #define VBUS_IN_CURR_LIM_SHIFT		4
62 #define AUTO_VBUS_IN_CURR_LIM_SHIFT	4
63 #define VBUS_IN_CURR_LIM_RETRY_SET_TIME	30 /* seconds */
64 
65 #define LED_INDICATOR_PWM_ENA		0x01
66 #define LED_INDICATOR_PWM_DIS		0x00
67 #define LED_IND_CUR_5MA			0x04
68 #define LED_INDICATOR_PWM_DUTY_252_256	0xBF
69 
70 /* HW failure constants */
71 #define MAIN_CH_TH_PROT			0x02
72 #define VBUS_CH_NOK			0x08
73 #define USB_CH_TH_PROT			0x02
74 #define VBUS_OVV_TH			0x01
75 #define MAIN_CH_NOK			0x01
76 #define VBUS_DET			0x80
77 
78 #define MAIN_CH_STATUS2_MAINCHGDROP		0x80
79 #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC	0x40
80 #define USB_CH_VBUSDROP				0x40
81 #define USB_CH_VBUSDETDBNC			0x01
82 
83 /* UsbLineStatus register bit masks */
84 #define AB8500_USB_LINK_STATUS		0x78
85 #define AB8505_USB_LINK_STATUS		0xF8
86 #define AB8500_STD_HOST_SUSP		0x18
87 #define USB_LINK_STATUS_SHIFT		3
88 
89 /* Watchdog timeout constant */
90 #define WD_TIMER			0x30 /* 4min */
91 #define WD_KICK_INTERVAL		(60 * HZ)
92 
93 /* Lowest charger voltage is 3.39V -> 0x4E */
94 #define LOW_VOLT_REG			0x4E
95 
96 /* Step up/down delay in us */
97 #define STEP_UDELAY			1000
98 
99 #define CHARGER_STATUS_POLL 10 /* in ms */
100 
101 #define CHG_WD_INTERVAL			(60 * HZ)
102 
103 #define AB8500_SW_CONTROL_FALLBACK	0x03
104 /* Wait for enumeration before charing in us */
105 #define WAIT_ACA_RID_ENUMERATION	(5 * 1000)
106 /*External charger control*/
107 #define AB8500_SYS_CHARGER_CONTROL_REG		0x52
108 #define EXTERNAL_CHARGER_DISABLE_REG_VAL	0x03
109 #define EXTERNAL_CHARGER_ENABLE_REG_VAL		0x07
110 
111 /* UsbLineStatus register - usb types */
112 enum ab8500_charger_link_status {
113 	USB_STAT_NOT_CONFIGURED,
114 	USB_STAT_STD_HOST_NC,
115 	USB_STAT_STD_HOST_C_NS,
116 	USB_STAT_STD_HOST_C_S,
117 	USB_STAT_HOST_CHG_NM,
118 	USB_STAT_HOST_CHG_HS,
119 	USB_STAT_HOST_CHG_HS_CHIRP,
120 	USB_STAT_DEDICATED_CHG,
121 	USB_STAT_ACA_RID_A,
122 	USB_STAT_ACA_RID_B,
123 	USB_STAT_ACA_RID_C_NM,
124 	USB_STAT_ACA_RID_C_HS,
125 	USB_STAT_ACA_RID_C_HS_CHIRP,
126 	USB_STAT_HM_IDGND,
127 	USB_STAT_RESERVED,
128 	USB_STAT_NOT_VALID_LINK,
129 	USB_STAT_PHY_EN,
130 	USB_STAT_SUP_NO_IDGND_VBUS,
131 	USB_STAT_SUP_IDGND_VBUS,
132 	USB_STAT_CHARGER_LINE_1,
133 	USB_STAT_CARKIT_1,
134 	USB_STAT_CARKIT_2,
135 	USB_STAT_ACA_DOCK_CHARGER,
136 };
137 
138 enum ab8500_usb_state {
139 	AB8500_BM_USB_STATE_RESET_HS,	/* HighSpeed Reset */
140 	AB8500_BM_USB_STATE_RESET_FS,	/* FullSpeed/LowSpeed Reset */
141 	AB8500_BM_USB_STATE_CONFIGURED,
142 	AB8500_BM_USB_STATE_SUSPEND,
143 	AB8500_BM_USB_STATE_RESUME,
144 	AB8500_BM_USB_STATE_MAX,
145 };
146 
147 /* VBUS input current limits supported in AB8500 in mA */
148 #define USB_CH_IP_CUR_LVL_0P05		50
149 #define USB_CH_IP_CUR_LVL_0P09		98
150 #define USB_CH_IP_CUR_LVL_0P19		193
151 #define USB_CH_IP_CUR_LVL_0P29		290
152 #define USB_CH_IP_CUR_LVL_0P38		380
153 #define USB_CH_IP_CUR_LVL_0P45		450
154 #define USB_CH_IP_CUR_LVL_0P5		500
155 #define USB_CH_IP_CUR_LVL_0P6		600
156 #define USB_CH_IP_CUR_LVL_0P7		700
157 #define USB_CH_IP_CUR_LVL_0P8		800
158 #define USB_CH_IP_CUR_LVL_0P9		900
159 #define USB_CH_IP_CUR_LVL_1P0		1000
160 #define USB_CH_IP_CUR_LVL_1P1		1100
161 #define USB_CH_IP_CUR_LVL_1P3		1300
162 #define USB_CH_IP_CUR_LVL_1P4		1400
163 #define USB_CH_IP_CUR_LVL_1P5		1500
164 
165 #define VBAT_TRESH_IP_CUR_RED		3800
166 
167 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
168 	struct ab8500_charger, usb_chg)
169 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
170 	struct ab8500_charger, ac_chg)
171 
172 /**
173  * struct ab8500_charger_interrupts - ab8500 interupts
174  * @name:	name of the interrupt
175  * @isr		function pointer to the isr
176  */
177 struct ab8500_charger_interrupts {
178 	char *name;
179 	irqreturn_t (*isr)(int irq, void *data);
180 };
181 
182 struct ab8500_charger_info {
183 	int charger_connected;
184 	int charger_online;
185 	int charger_voltage;
186 	int cv_active;
187 	bool wd_expired;
188 	int charger_current;
189 };
190 
191 struct ab8500_charger_event_flags {
192 	bool mainextchnotok;
193 	bool main_thermal_prot;
194 	bool usb_thermal_prot;
195 	bool vbus_ovv;
196 	bool usbchargernotok;
197 	bool chgwdexp;
198 	bool vbus_collapse;
199 	bool vbus_drop_end;
200 };
201 
202 struct ab8500_charger_usb_state {
203 	int usb_current;
204 	int usb_current_tmp;
205 	enum ab8500_usb_state state;
206 	enum ab8500_usb_state state_tmp;
207 	spinlock_t usb_lock;
208 };
209 
210 struct ab8500_charger_max_usb_in_curr {
211 	int usb_type_max;
212 	int set_max;
213 	int calculated_max;
214 };
215 
216 /**
217  * struct ab8500_charger - ab8500 Charger device information
218  * @dev:		Pointer to the structure device
219  * @vbus_detected:	VBUS detected
220  * @vbus_detected_start:
221  *			VBUS detected during startup
222  * @ac_conn:		This will be true when the AC charger has been plugged
223  * @vddadc_en_ac:	Indicate if VDD ADC supply is enabled because AC
224  *			charger is enabled
225  * @vddadc_en_usb:	Indicate if VDD ADC supply is enabled because USB
226  *			charger is enabled
227  * @vbat		Battery voltage
228  * @old_vbat		Previously measured battery voltage
229  * @usb_device_is_unrecognised	USB device is unrecognised by the hardware
230  * @autopower		Indicate if we should have automatic pwron after pwrloss
231  * @autopower_cfg	platform specific power config support for "pwron after pwrloss"
232  * @invalid_charger_detect_state State when forcing AB to use invalid charger
233  * @is_aca_rid:		Incicate if accessory is ACA type
234  * @current_stepping_sessions:
235  *			Counter for current stepping sessions
236  * @parent:		Pointer to the struct ab8500
237  * @adc_main_charger_v	ADC channel for main charger voltage
238  * @adc_main_charger_c	ADC channel for main charger current
239  * @adc_vbus_v		ADC channel for USB charger voltage
240  * @adc_usb_charger_c	ADC channel for USB charger current
241  * @bm:           	Platform specific battery management information
242  * @flags:		Structure for information about events triggered
243  * @usb_state:		Structure for usb stack information
244  * @max_usb_in_curr:	Max USB charger input current
245  * @ac_chg:		AC charger power supply
246  * @usb_chg:		USB charger power supply
247  * @ac:			Structure that holds the AC charger properties
248  * @usb:		Structure that holds the USB charger properties
249  * @regu:		Pointer to the struct regulator
250  * @charger_wq:		Work queue for the IRQs and checking HW state
251  * @usb_ipt_crnt_lock:	Lock to protect VBUS input current setting from mutuals
252  * @pm_lock:		Lock to prevent system to suspend
253  * @check_vbat_work	Work for checking vbat threshold to adjust vbus current
254  * @check_hw_failure_work:	Work for checking HW state
255  * @check_usbchgnotok_work:	Work for checking USB charger not ok status
256  * @kick_wd_work:		Work for kicking the charger watchdog in case
257  *				of ABB rev 1.* due to the watchog logic bug
258  * @ac_charger_attached_work:	Work for checking if AC charger is still
259  *				connected
260  * @usb_charger_attached_work:	Work for checking if USB charger is still
261  *				connected
262  * @ac_work:			Work for checking AC charger connection
263  * @detect_usb_type_work:	Work for detecting the USB type connected
264  * @usb_link_status_work:	Work for checking the new USB link status
265  * @usb_state_changed_work:	Work for checking USB state
266  * @attach_work:		Work for detecting USB type
267  * @vbus_drop_end_work:		Work for detecting VBUS drop end
268  * @check_main_thermal_prot_work:
269  *				Work for checking Main thermal status
270  * @check_usb_thermal_prot_work:
271  *				Work for checking USB thermal status
272  * @charger_attached_mutex:	For controlling the wakelock
273  */
274 struct ab8500_charger {
275 	struct device *dev;
276 	bool vbus_detected;
277 	bool vbus_detected_start;
278 	bool ac_conn;
279 	bool vddadc_en_ac;
280 	bool vddadc_en_usb;
281 	int vbat;
282 	int old_vbat;
283 	bool usb_device_is_unrecognised;
284 	bool autopower;
285 	bool autopower_cfg;
286 	int invalid_charger_detect_state;
287 	int is_aca_rid;
288 	atomic_t current_stepping_sessions;
289 	struct ab8500 *parent;
290 	struct iio_channel *adc_main_charger_v;
291 	struct iio_channel *adc_main_charger_c;
292 	struct iio_channel *adc_vbus_v;
293 	struct iio_channel *adc_usb_charger_c;
294 	struct abx500_bm_data *bm;
295 	struct ab8500_charger_event_flags flags;
296 	struct ab8500_charger_usb_state usb_state;
297 	struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
298 	struct ux500_charger ac_chg;
299 	struct ux500_charger usb_chg;
300 	struct ab8500_charger_info ac;
301 	struct ab8500_charger_info usb;
302 	struct regulator *regu;
303 	struct workqueue_struct *charger_wq;
304 	struct mutex usb_ipt_crnt_lock;
305 	struct delayed_work check_vbat_work;
306 	struct delayed_work check_hw_failure_work;
307 	struct delayed_work check_usbchgnotok_work;
308 	struct delayed_work kick_wd_work;
309 	struct delayed_work usb_state_changed_work;
310 	struct delayed_work attach_work;
311 	struct delayed_work ac_charger_attached_work;
312 	struct delayed_work usb_charger_attached_work;
313 	struct delayed_work vbus_drop_end_work;
314 	struct work_struct ac_work;
315 	struct work_struct detect_usb_type_work;
316 	struct work_struct usb_link_status_work;
317 	struct work_struct check_main_thermal_prot_work;
318 	struct work_struct check_usb_thermal_prot_work;
319 	struct usb_phy *usb_phy;
320 	struct notifier_block nb;
321 	struct mutex charger_attached_mutex;
322 };
323 
324 /* AC properties */
325 static enum power_supply_property ab8500_charger_ac_props[] = {
326 	POWER_SUPPLY_PROP_HEALTH,
327 	POWER_SUPPLY_PROP_PRESENT,
328 	POWER_SUPPLY_PROP_ONLINE,
329 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
330 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
331 	POWER_SUPPLY_PROP_CURRENT_NOW,
332 };
333 
334 /* USB properties */
335 static enum power_supply_property ab8500_charger_usb_props[] = {
336 	POWER_SUPPLY_PROP_HEALTH,
337 	POWER_SUPPLY_PROP_CURRENT_AVG,
338 	POWER_SUPPLY_PROP_PRESENT,
339 	POWER_SUPPLY_PROP_ONLINE,
340 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
341 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
342 	POWER_SUPPLY_PROP_CURRENT_NOW,
343 };
344 
345 /*
346  * Function for enabling and disabling sw fallback mode
347  * should always be disabled when no charger is connected.
348  */
349 static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
350 		bool fallback)
351 {
352 	u8 val;
353 	u8 reg;
354 	u8 bank;
355 	u8 bit;
356 	int ret;
357 
358 	dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
359 
360 	if (is_ab8500(di->parent)) {
361 		bank = 0x15;
362 		reg = 0x0;
363 		bit = 3;
364 	} else {
365 		bank = AB8500_SYS_CTRL1_BLOCK;
366 		reg = AB8500_SW_CONTROL_FALLBACK;
367 		bit = 0;
368 	}
369 
370 	/* read the register containing fallback bit */
371 	ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
372 	if (ret < 0) {
373 		dev_err(di->dev, "%d read failed\n", __LINE__);
374 		return;
375 	}
376 
377 	if (is_ab8500(di->parent)) {
378 		/* enable the OPT emulation registers */
379 		ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
380 		if (ret) {
381 			dev_err(di->dev, "%d write failed\n", __LINE__);
382 			goto disable_otp;
383 		}
384 	}
385 
386 	if (fallback)
387 		val |= (1 << bit);
388 	else
389 		val &= ~(1 << bit);
390 
391 	/* write back the changed fallback bit value to register */
392 	ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
393 	if (ret) {
394 		dev_err(di->dev, "%d write failed\n", __LINE__);
395 	}
396 
397 disable_otp:
398 	if (is_ab8500(di->parent)) {
399 		/* disable the set OTP registers again */
400 		ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
401 		if (ret) {
402 			dev_err(di->dev, "%d write failed\n", __LINE__);
403 		}
404 	}
405 }
406 
407 /**
408  * ab8500_power_supply_changed - a wrapper with local extensions for
409  * power_supply_changed
410  * @di:	  pointer to the ab8500_charger structure
411  * @psy:  pointer to power_supply_that have changed.
412  *
413  */
414 static void ab8500_power_supply_changed(struct ab8500_charger *di,
415 					struct power_supply *psy)
416 {
417 	if (di->autopower_cfg) {
418 		if (!di->usb.charger_connected &&
419 		    !di->ac.charger_connected &&
420 		    di->autopower) {
421 			di->autopower = false;
422 			ab8500_enable_disable_sw_fallback(di, false);
423 		} else if (!di->autopower &&
424 			   (di->ac.charger_connected ||
425 			    di->usb.charger_connected)) {
426 			di->autopower = true;
427 			ab8500_enable_disable_sw_fallback(di, true);
428 		}
429 	}
430 	power_supply_changed(psy);
431 }
432 
433 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
434 	bool connected)
435 {
436 	if (connected != di->usb.charger_connected) {
437 		dev_dbg(di->dev, "USB connected:%i\n", connected);
438 		di->usb.charger_connected = connected;
439 
440 		if (!connected)
441 			di->flags.vbus_drop_end = false;
442 
443 		sysfs_notify(&di->usb_chg.psy->dev.kobj, NULL, "present");
444 
445 		if (connected) {
446 			mutex_lock(&di->charger_attached_mutex);
447 			mutex_unlock(&di->charger_attached_mutex);
448 
449 			if (is_ab8500(di->parent))
450 				queue_delayed_work(di->charger_wq,
451 					   &di->usb_charger_attached_work,
452 					   HZ);
453 		} else {
454 			cancel_delayed_work_sync(&di->usb_charger_attached_work);
455 			mutex_lock(&di->charger_attached_mutex);
456 			mutex_unlock(&di->charger_attached_mutex);
457 		}
458 	}
459 }
460 
461 /**
462  * ab8500_charger_get_ac_voltage() - get ac charger voltage
463  * @di:		pointer to the ab8500_charger structure
464  *
465  * Returns ac charger voltage (on success)
466  */
467 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
468 {
469 	int vch, ret;
470 
471 	/* Only measure voltage if the charger is connected */
472 	if (di->ac.charger_connected) {
473 		ret = iio_read_channel_processed(di->adc_main_charger_v, &vch);
474 		if (ret < 0)
475 			dev_err(di->dev, "%s ADC conv failed,\n", __func__);
476 	} else {
477 		vch = 0;
478 	}
479 	return vch;
480 }
481 
482 /**
483  * ab8500_charger_ac_cv() - check if the main charger is in CV mode
484  * @di:		pointer to the ab8500_charger structure
485  *
486  * Returns ac charger CV mode (on success) else error code
487  */
488 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
489 {
490 	u8 val;
491 	int ret = 0;
492 
493 	/* Only check CV mode if the charger is online */
494 	if (di->ac.charger_online) {
495 		ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
496 			AB8500_CH_STATUS1_REG, &val);
497 		if (ret < 0) {
498 			dev_err(di->dev, "%s ab8500 read failed\n", __func__);
499 			return 0;
500 		}
501 
502 		if (val & MAIN_CH_CV_ON)
503 			ret = 1;
504 		else
505 			ret = 0;
506 	}
507 
508 	return ret;
509 }
510 
511 /**
512  * ab8500_charger_get_vbus_voltage() - get vbus voltage
513  * @di:		pointer to the ab8500_charger structure
514  *
515  * This function returns the vbus voltage.
516  * Returns vbus voltage (on success)
517  */
518 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
519 {
520 	int vch, ret;
521 
522 	/* Only measure voltage if the charger is connected */
523 	if (di->usb.charger_connected) {
524 		ret = iio_read_channel_processed(di->adc_vbus_v, &vch);
525 		if (ret < 0)
526 			dev_err(di->dev, "%s ADC conv failed,\n", __func__);
527 	} else {
528 		vch = 0;
529 	}
530 	return vch;
531 }
532 
533 /**
534  * ab8500_charger_get_usb_current() - get usb charger current
535  * @di:		pointer to the ab8500_charger structure
536  *
537  * This function returns the usb charger current.
538  * Returns usb current (on success) and error code on failure
539  */
540 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
541 {
542 	int ich, ret;
543 
544 	/* Only measure current if the charger is online */
545 	if (di->usb.charger_online) {
546 		ret = iio_read_channel_processed(di->adc_usb_charger_c, &ich);
547 		if (ret < 0)
548 			dev_err(di->dev, "%s ADC conv failed,\n", __func__);
549 	} else {
550 		ich = 0;
551 	}
552 	return ich;
553 }
554 
555 /**
556  * ab8500_charger_get_ac_current() - get ac charger current
557  * @di:		pointer to the ab8500_charger structure
558  *
559  * This function returns the ac charger current.
560  * Returns ac current (on success) and error code on failure.
561  */
562 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
563 {
564 	int ich, ret;
565 
566 	/* Only measure current if the charger is online */
567 	if (di->ac.charger_online) {
568 		ret = iio_read_channel_processed(di->adc_main_charger_c, &ich);
569 		if (ret < 0)
570 			dev_err(di->dev, "%s ADC conv failed,\n", __func__);
571 	} else {
572 		ich = 0;
573 	}
574 	return ich;
575 }
576 
577 /**
578  * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
579  * @di:		pointer to the ab8500_charger structure
580  *
581  * Returns ac charger CV mode (on success) else error code
582  */
583 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
584 {
585 	int ret;
586 	u8 val;
587 
588 	/* Only check CV mode if the charger is online */
589 	if (di->usb.charger_online) {
590 		ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
591 			AB8500_CH_USBCH_STAT1_REG, &val);
592 		if (ret < 0) {
593 			dev_err(di->dev, "%s ab8500 read failed\n", __func__);
594 			return 0;
595 		}
596 
597 		if (val & USB_CH_CV_ON)
598 			ret = 1;
599 		else
600 			ret = 0;
601 	} else {
602 		ret = 0;
603 	}
604 
605 	return ret;
606 }
607 
608 /**
609  * ab8500_charger_detect_chargers() - Detect the connected chargers
610  * @di:		pointer to the ab8500_charger structure
611  * @probe:	if probe, don't delay and wait for HW
612  *
613  * Returns the type of charger connected.
614  * For USB it will not mean we can actually charge from it
615  * but that there is a USB cable connected that we have to
616  * identify. This is used during startup when we don't get
617  * interrupts of the charger detection
618  *
619  * Returns an integer value, that means,
620  * NO_PW_CONN  no power supply is connected
621  * AC_PW_CONN  if the AC power supply is connected
622  * USB_PW_CONN  if the USB power supply is connected
623  * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
624  */
625 static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
626 {
627 	int result = NO_PW_CONN;
628 	int ret;
629 	u8 val;
630 
631 	/* Check for AC charger */
632 	ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
633 		AB8500_CH_STATUS1_REG, &val);
634 	if (ret < 0) {
635 		dev_err(di->dev, "%s ab8500 read failed\n", __func__);
636 		return ret;
637 	}
638 
639 	if (val & MAIN_CH_DET)
640 		result = AC_PW_CONN;
641 
642 	/* Check for USB charger */
643 
644 	if (!probe) {
645 		/*
646 		 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
647 		 * when disconnecting ACA even though no
648 		 * charger was connected. Try waiting a little
649 		 * longer than the 100 ms of VBUS_DET_DBNC100...
650 		 */
651 		msleep(110);
652 	}
653 	ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
654 		AB8500_CH_USBCH_STAT1_REG, &val);
655 	if (ret < 0) {
656 		dev_err(di->dev, "%s ab8500 read failed\n", __func__);
657 		return ret;
658 	}
659 	dev_dbg(di->dev,
660 		"%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
661 		val);
662 	if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
663 		result |= USB_PW_CONN;
664 
665 	return result;
666 }
667 
668 /**
669  * ab8500_charger_max_usb_curr() - get the max curr for the USB type
670  * @di:			pointer to the ab8500_charger structure
671  * @link_status:	the identified USB type
672  *
673  * Get the maximum current that is allowed to be drawn from the host
674  * based on the USB type.
675  * Returns error code in case of failure else 0 on success
676  */
677 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
678 		enum ab8500_charger_link_status link_status)
679 {
680 	int ret = 0;
681 
682 	di->usb_device_is_unrecognised = false;
683 
684 	/*
685 	 * Platform only supports USB 2.0.
686 	 * This means that charging current from USB source
687 	 * is maximum 500 mA. Every occurrence of USB_STAT_*_HOST_*
688 	 * should set USB_CH_IP_CUR_LVL_0P5.
689 	 */
690 
691 	switch (link_status) {
692 	case USB_STAT_STD_HOST_NC:
693 	case USB_STAT_STD_HOST_C_NS:
694 	case USB_STAT_STD_HOST_C_S:
695 		dev_dbg(di->dev, "USB Type - Standard host is "
696 			"detected through USB driver\n");
697 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
698 		di->is_aca_rid = 0;
699 		break;
700 	case USB_STAT_HOST_CHG_HS_CHIRP:
701 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
702 		di->is_aca_rid = 0;
703 		break;
704 	case USB_STAT_HOST_CHG_HS:
705 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
706 		di->is_aca_rid = 0;
707 		break;
708 	case USB_STAT_ACA_RID_C_HS:
709 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
710 		di->is_aca_rid = 0;
711 		break;
712 	case USB_STAT_ACA_RID_A:
713 		/*
714 		 * Dedicated charger level minus maximum current accessory
715 		 * can consume (900mA). Closest level is 500mA
716 		 */
717 		dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
718 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
719 		di->is_aca_rid = 1;
720 		break;
721 	case USB_STAT_ACA_RID_B:
722 		/*
723 		 * Dedicated charger level minus 120mA (20mA for ACA and
724 		 * 100mA for potential accessory). Closest level is 1300mA
725 		 */
726 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
727 		dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
728 				di->max_usb_in_curr.usb_type_max);
729 		di->is_aca_rid = 1;
730 		break;
731 	case USB_STAT_HOST_CHG_NM:
732 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
733 		di->is_aca_rid = 0;
734 		break;
735 	case USB_STAT_DEDICATED_CHG:
736 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
737 		di->is_aca_rid = 0;
738 		break;
739 	case USB_STAT_ACA_RID_C_HS_CHIRP:
740 	case USB_STAT_ACA_RID_C_NM:
741 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
742 		di->is_aca_rid = 1;
743 		break;
744 	case USB_STAT_NOT_CONFIGURED:
745 		if (di->vbus_detected) {
746 			di->usb_device_is_unrecognised = true;
747 			dev_dbg(di->dev, "USB Type - Legacy charger.\n");
748 			di->max_usb_in_curr.usb_type_max =
749 						USB_CH_IP_CUR_LVL_1P5;
750 			break;
751 		}
752 		fallthrough;
753 	case USB_STAT_HM_IDGND:
754 		dev_err(di->dev, "USB Type - Charging not allowed\n");
755 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
756 		ret = -ENXIO;
757 		break;
758 	case USB_STAT_RESERVED:
759 		if (is_ab8500(di->parent)) {
760 			di->flags.vbus_collapse = true;
761 			dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
762 						"VBUS has collapsed\n");
763 			ret = -ENXIO;
764 			break;
765 		} else {
766 			dev_dbg(di->dev, "USB Type - Charging not allowed\n");
767 			di->max_usb_in_curr.usb_type_max =
768 						USB_CH_IP_CUR_LVL_0P05;
769 			dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
770 				link_status,
771 				di->max_usb_in_curr.usb_type_max);
772 			ret = -ENXIO;
773 			break;
774 		}
775 	case USB_STAT_CARKIT_1:
776 	case USB_STAT_CARKIT_2:
777 	case USB_STAT_ACA_DOCK_CHARGER:
778 	case USB_STAT_CHARGER_LINE_1:
779 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
780 		dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
781 				di->max_usb_in_curr.usb_type_max);
782 		break;
783 	case USB_STAT_NOT_VALID_LINK:
784 		dev_err(di->dev, "USB Type invalid - try charging anyway\n");
785 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
786 		break;
787 
788 	default:
789 		dev_err(di->dev, "USB Type - Unknown\n");
790 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
791 		ret = -ENXIO;
792 		break;
793 	}
794 
795 	di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
796 	dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
797 		link_status, di->max_usb_in_curr.set_max);
798 
799 	return ret;
800 }
801 
802 /**
803  * ab8500_charger_read_usb_type() - read the type of usb connected
804  * @di:		pointer to the ab8500_charger structure
805  *
806  * Detect the type of the plugged USB
807  * Returns error code in case of failure else 0 on success
808  */
809 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
810 {
811 	int ret;
812 	u8 val;
813 
814 	ret = abx500_get_register_interruptible(di->dev,
815 		AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
816 	if (ret < 0) {
817 		dev_err(di->dev, "%s ab8500 read failed\n", __func__);
818 		return ret;
819 	}
820 	if (is_ab8500(di->parent))
821 		ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
822 			AB8500_USB_LINE_STAT_REG, &val);
823 	else
824 		ret = abx500_get_register_interruptible(di->dev,
825 			AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
826 	if (ret < 0) {
827 		dev_err(di->dev, "%s ab8500 read failed\n", __func__);
828 		return ret;
829 	}
830 
831 	/* get the USB type */
832 	if (is_ab8500(di->parent))
833 		val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
834 	else
835 		val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
836 	ret = ab8500_charger_max_usb_curr(di,
837 		(enum ab8500_charger_link_status) val);
838 
839 	return ret;
840 }
841 
842 /**
843  * ab8500_charger_detect_usb_type() - get the type of usb connected
844  * @di:		pointer to the ab8500_charger structure
845  *
846  * Detect the type of the plugged USB
847  * Returns error code in case of failure else 0 on success
848  */
849 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
850 {
851 	int i, ret;
852 	u8 val;
853 
854 	/*
855 	 * On getting the VBUS rising edge detect interrupt there
856 	 * is a 250ms delay after which the register UsbLineStatus
857 	 * is filled with valid data.
858 	 */
859 	for (i = 0; i < 10; i++) {
860 		msleep(250);
861 		ret = abx500_get_register_interruptible(di->dev,
862 			AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
863 			&val);
864 		dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
865 			__func__, val);
866 		if (ret < 0) {
867 			dev_err(di->dev, "%s ab8500 read failed\n", __func__);
868 			return ret;
869 		}
870 
871 		if (is_ab8500(di->parent))
872 			ret = abx500_get_register_interruptible(di->dev,
873 				AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
874 		else
875 			ret = abx500_get_register_interruptible(di->dev,
876 				AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
877 		if (ret < 0) {
878 			dev_err(di->dev, "%s ab8500 read failed\n", __func__);
879 			return ret;
880 		}
881 		dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
882 			val);
883 		/*
884 		 * Until the IT source register is read the UsbLineStatus
885 		 * register is not updated, hence doing the same
886 		 * Revisit this:
887 		 */
888 
889 		/* get the USB type */
890 		if (is_ab8500(di->parent))
891 			val = (val & AB8500_USB_LINK_STATUS) >>
892 							USB_LINK_STATUS_SHIFT;
893 		else
894 			val = (val & AB8505_USB_LINK_STATUS) >>
895 							USB_LINK_STATUS_SHIFT;
896 		if (val)
897 			break;
898 	}
899 	ret = ab8500_charger_max_usb_curr(di,
900 		(enum ab8500_charger_link_status) val);
901 
902 	return ret;
903 }
904 
905 /*
906  * This array maps the raw hex value to charger voltage used by the AB8500
907  * Values taken from the UM0836
908  */
909 static int ab8500_charger_voltage_map[] = {
910 	3500 ,
911 	3525 ,
912 	3550 ,
913 	3575 ,
914 	3600 ,
915 	3625 ,
916 	3650 ,
917 	3675 ,
918 	3700 ,
919 	3725 ,
920 	3750 ,
921 	3775 ,
922 	3800 ,
923 	3825 ,
924 	3850 ,
925 	3875 ,
926 	3900 ,
927 	3925 ,
928 	3950 ,
929 	3975 ,
930 	4000 ,
931 	4025 ,
932 	4050 ,
933 	4060 ,
934 	4070 ,
935 	4080 ,
936 	4090 ,
937 	4100 ,
938 	4110 ,
939 	4120 ,
940 	4130 ,
941 	4140 ,
942 	4150 ,
943 	4160 ,
944 	4170 ,
945 	4180 ,
946 	4190 ,
947 	4200 ,
948 	4210 ,
949 	4220 ,
950 	4230 ,
951 	4240 ,
952 	4250 ,
953 	4260 ,
954 	4270 ,
955 	4280 ,
956 	4290 ,
957 	4300 ,
958 	4310 ,
959 	4320 ,
960 	4330 ,
961 	4340 ,
962 	4350 ,
963 	4360 ,
964 	4370 ,
965 	4380 ,
966 	4390 ,
967 	4400 ,
968 	4410 ,
969 	4420 ,
970 	4430 ,
971 	4440 ,
972 	4450 ,
973 	4460 ,
974 	4470 ,
975 	4480 ,
976 	4490 ,
977 	4500 ,
978 	4510 ,
979 	4520 ,
980 	4530 ,
981 	4540 ,
982 	4550 ,
983 	4560 ,
984 	4570 ,
985 	4580 ,
986 	4590 ,
987 	4600 ,
988 };
989 
990 static int ab8500_voltage_to_regval(int voltage)
991 {
992 	int i;
993 
994 	/* Special case for voltage below 3.5V */
995 	if (voltage < ab8500_charger_voltage_map[0])
996 		return LOW_VOLT_REG;
997 
998 	for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
999 		if (voltage < ab8500_charger_voltage_map[i])
1000 			return i - 1;
1001 	}
1002 
1003 	/* If not last element, return error */
1004 	i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
1005 	if (voltage == ab8500_charger_voltage_map[i])
1006 		return i;
1007 	else
1008 		return -1;
1009 }
1010 
1011 static int ab8500_current_to_regval(struct ab8500_charger *di, int curr)
1012 {
1013 	int i;
1014 
1015 	if (curr < di->bm->chg_output_curr[0])
1016 		return 0;
1017 
1018 	for (i = 0; i < di->bm->n_chg_out_curr; i++) {
1019 		if (curr < di->bm->chg_output_curr[i])
1020 			return i - 1;
1021 	}
1022 
1023 	/* If not last element, return error */
1024 	i = di->bm->n_chg_out_curr - 1;
1025 	if (curr == di->bm->chg_output_curr[i])
1026 		return i;
1027 	else
1028 		return -1;
1029 }
1030 
1031 static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr)
1032 {
1033 	int i;
1034 
1035 	if (curr < di->bm->chg_input_curr[0])
1036 		return 0;
1037 
1038 	for (i = 0; i < di->bm->n_chg_in_curr; i++) {
1039 		if (curr < di->bm->chg_input_curr[i])
1040 			return i - 1;
1041 	}
1042 
1043 	/* If not last element, return error */
1044 	i = di->bm->n_chg_in_curr - 1;
1045 	if (curr == di->bm->chg_input_curr[i])
1046 		return i;
1047 	else
1048 		return -1;
1049 }
1050 
1051 /**
1052  * ab8500_charger_get_usb_cur() - get usb current
1053  * @di:		pointer to the ab8500_charger structre
1054  *
1055  * The usb stack provides the maximum current that can be drawn from
1056  * the standard usb host. This will be in mA.
1057  * This function converts current in mA to a value that can be written
1058  * to the register. Returns -1 if charging is not allowed
1059  */
1060 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1061 {
1062 	int ret = 0;
1063 	switch (di->usb_state.usb_current) {
1064 	case 100:
1065 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
1066 		break;
1067 	case 200:
1068 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
1069 		break;
1070 	case 300:
1071 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
1072 		break;
1073 	case 400:
1074 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
1075 		break;
1076 	case 500:
1077 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
1078 		break;
1079 	default:
1080 		di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
1081 		ret = -EPERM;
1082 		break;
1083 	}
1084 	di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
1085 	return ret;
1086 }
1087 
1088 /**
1089  * ab8500_charger_check_continue_stepping() - Check to allow stepping
1090  * @di:		pointer to the ab8500_charger structure
1091  * @reg:	select what charger register to check
1092  *
1093  * Check if current stepping should be allowed to continue.
1094  * Checks if charger source has not collapsed. If it has, further stepping
1095  * is not allowed.
1096  */
1097 static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1098 						   int reg)
1099 {
1100 	if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1101 		return !di->flags.vbus_drop_end;
1102 	else
1103 		return true;
1104 }
1105 
1106 /**
1107  * ab8500_charger_set_current() - set charger current
1108  * @di:		pointer to the ab8500_charger structure
1109  * @ich:	charger current, in mA
1110  * @reg:	select what charger register to set
1111  *
1112  * Set charger current.
1113  * There is no state machine in the AB to step up/down the charger
1114  * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1115  * charging is started. Instead we need to implement
1116  * this charger current step-up/down here.
1117  * Returns error code in case of failure else 0(on success)
1118  */
1119 static int ab8500_charger_set_current(struct ab8500_charger *di,
1120 	int ich, int reg)
1121 {
1122 	int ret = 0;
1123 	int curr_index, prev_curr_index, shift_value, i;
1124 	u8 reg_value;
1125 	u32 step_udelay;
1126 	bool no_stepping = false;
1127 
1128 	atomic_inc(&di->current_stepping_sessions);
1129 
1130 	ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1131 		reg, &reg_value);
1132 	if (ret < 0) {
1133 		dev_err(di->dev, "%s read failed\n", __func__);
1134 		goto exit_set_current;
1135 	}
1136 
1137 	switch (reg) {
1138 	case AB8500_MCH_IPT_CURLVL_REG:
1139 		shift_value = MAIN_CH_INPUT_CURR_SHIFT;
1140 		prev_curr_index = (reg_value >> shift_value);
1141 		curr_index = ab8500_current_to_regval(di, ich);
1142 		step_udelay = STEP_UDELAY;
1143 		if (!di->ac.charger_connected)
1144 			no_stepping = true;
1145 		break;
1146 	case AB8500_USBCH_IPT_CRNTLVL_REG:
1147 		shift_value = VBUS_IN_CURR_LIM_SHIFT;
1148 		prev_curr_index = (reg_value >> shift_value);
1149 		curr_index = ab8500_vbus_in_curr_to_regval(di, ich);
1150 		step_udelay = STEP_UDELAY * 100;
1151 
1152 		if (!di->usb.charger_connected)
1153 			no_stepping = true;
1154 		break;
1155 	case AB8500_CH_OPT_CRNTLVL_REG:
1156 		shift_value = 0;
1157 		prev_curr_index = (reg_value >> shift_value);
1158 		curr_index = ab8500_current_to_regval(di, ich);
1159 		step_udelay = STEP_UDELAY;
1160 		if (curr_index && (curr_index - prev_curr_index) > 1)
1161 			step_udelay *= 100;
1162 
1163 		if (!di->usb.charger_connected && !di->ac.charger_connected)
1164 			no_stepping = true;
1165 
1166 		break;
1167 	default:
1168 		dev_err(di->dev, "%s current register not valid\n", __func__);
1169 		ret = -ENXIO;
1170 		goto exit_set_current;
1171 	}
1172 
1173 	if (curr_index < 0) {
1174 		dev_err(di->dev, "requested current limit out-of-range\n");
1175 		ret = -ENXIO;
1176 		goto exit_set_current;
1177 	}
1178 
1179 	/* only update current if it's been changed */
1180 	if (prev_curr_index == curr_index) {
1181 		dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1182 			__func__, reg);
1183 		ret = 0;
1184 		goto exit_set_current;
1185 	}
1186 
1187 	dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1188 		__func__, ich, reg);
1189 
1190 	if (no_stepping) {
1191 		ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1192 					reg, (u8)curr_index << shift_value);
1193 		if (ret)
1194 			dev_err(di->dev, "%s write failed\n", __func__);
1195 	} else if (prev_curr_index > curr_index) {
1196 		for (i = prev_curr_index - 1; i >= curr_index; i--) {
1197 			dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1198 				(u8) i << shift_value, reg);
1199 			ret = abx500_set_register_interruptible(di->dev,
1200 				AB8500_CHARGER, reg, (u8)i << shift_value);
1201 			if (ret) {
1202 				dev_err(di->dev, "%s write failed\n", __func__);
1203 				goto exit_set_current;
1204 			}
1205 			if (i != curr_index)
1206 				usleep_range(step_udelay, step_udelay * 2);
1207 		}
1208 	} else {
1209 		bool allow = true;
1210 		for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
1211 			dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1212 				(u8)i << shift_value, reg);
1213 			ret = abx500_set_register_interruptible(di->dev,
1214 				AB8500_CHARGER, reg, (u8)i << shift_value);
1215 			if (ret) {
1216 				dev_err(di->dev, "%s write failed\n", __func__);
1217 				goto exit_set_current;
1218 			}
1219 			if (i != curr_index)
1220 				usleep_range(step_udelay, step_udelay * 2);
1221 
1222 			allow = ab8500_charger_check_continue_stepping(di, reg);
1223 		}
1224 	}
1225 
1226 exit_set_current:
1227 	atomic_dec(&di->current_stepping_sessions);
1228 
1229 	return ret;
1230 }
1231 
1232 /**
1233  * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1234  * @di:		pointer to the ab8500_charger structure
1235  * @ich_in:	charger input current limit
1236  *
1237  * Sets the current that can be drawn from the USB host
1238  * Returns error code in case of failure else 0(on success)
1239  */
1240 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1241 		int ich_in)
1242 {
1243 	int min_value;
1244 	int ret;
1245 
1246 	/* We should always use to lowest current limit */
1247 	min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
1248 	if (di->max_usb_in_curr.set_max > 0)
1249 		min_value = min(di->max_usb_in_curr.set_max, min_value);
1250 
1251 	if (di->usb_state.usb_current >= 0)
1252 		min_value = min(di->usb_state.usb_current, min_value);
1253 
1254 	switch (min_value) {
1255 	case 100:
1256 		if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1257 			min_value = USB_CH_IP_CUR_LVL_0P05;
1258 		break;
1259 	case 500:
1260 		if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1261 			min_value = USB_CH_IP_CUR_LVL_0P45;
1262 		break;
1263 	default:
1264 		break;
1265 	}
1266 
1267 	dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1268 
1269 	mutex_lock(&di->usb_ipt_crnt_lock);
1270 	ret = ab8500_charger_set_current(di, min_value,
1271 		AB8500_USBCH_IPT_CRNTLVL_REG);
1272 	mutex_unlock(&di->usb_ipt_crnt_lock);
1273 
1274 	return ret;
1275 }
1276 
1277 /**
1278  * ab8500_charger_set_main_in_curr() - set main charger input current
1279  * @di:		pointer to the ab8500_charger structure
1280  * @ich_in:	input charger current, in mA
1281  *
1282  * Set main charger input current.
1283  * Returns error code in case of failure else 0(on success)
1284  */
1285 static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1286 	int ich_in)
1287 {
1288 	return ab8500_charger_set_current(di, ich_in,
1289 		AB8500_MCH_IPT_CURLVL_REG);
1290 }
1291 
1292 /**
1293  * ab8500_charger_set_output_curr() - set charger output current
1294  * @di:		pointer to the ab8500_charger structure
1295  * @ich_out:	output charger current, in mA
1296  *
1297  * Set charger output current.
1298  * Returns error code in case of failure else 0(on success)
1299  */
1300 static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1301 	int ich_out)
1302 {
1303 	return ab8500_charger_set_current(di, ich_out,
1304 		AB8500_CH_OPT_CRNTLVL_REG);
1305 }
1306 
1307 /**
1308  * ab8500_charger_led_en() - turn on/off chargign led
1309  * @di:		pointer to the ab8500_charger structure
1310  * @on:		flag to turn on/off the chargign led
1311  *
1312  * Power ON/OFF charging LED indication
1313  * Returns error code in case of failure else 0(on success)
1314  */
1315 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1316 {
1317 	int ret;
1318 
1319 	if (on) {
1320 		/* Power ON charging LED indicator, set LED current to 5mA */
1321 		ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1322 			AB8500_LED_INDICATOR_PWM_CTRL,
1323 			(LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1324 		if (ret) {
1325 			dev_err(di->dev, "Power ON LED failed\n");
1326 			return ret;
1327 		}
1328 		/* LED indicator PWM duty cycle 252/256 */
1329 		ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1330 			AB8500_LED_INDICATOR_PWM_DUTY,
1331 			LED_INDICATOR_PWM_DUTY_252_256);
1332 		if (ret) {
1333 			dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1334 			return ret;
1335 		}
1336 	} else {
1337 		/* Power off charging LED indicator */
1338 		ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1339 			AB8500_LED_INDICATOR_PWM_CTRL,
1340 			LED_INDICATOR_PWM_DIS);
1341 		if (ret) {
1342 			dev_err(di->dev, "Power-off LED failed\n");
1343 			return ret;
1344 		}
1345 	}
1346 
1347 	return ret;
1348 }
1349 
1350 /**
1351  * ab8500_charger_ac_en() - enable or disable ac charging
1352  * @di:		pointer to the ab8500_charger structure
1353  * @enable:	enable/disable flag
1354  * @vset:	charging voltage
1355  * @iset:	charging current
1356  *
1357  * Enable/Disable AC/Mains charging and turns on/off the charging led
1358  * respectively.
1359  **/
1360 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1361 	int enable, int vset, int iset)
1362 {
1363 	int ret;
1364 	int volt_index;
1365 	int curr_index;
1366 	int input_curr_index;
1367 	u8 overshoot = 0;
1368 
1369 	struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1370 
1371 	if (enable) {
1372 		/* Check if AC is connected */
1373 		if (!di->ac.charger_connected) {
1374 			dev_err(di->dev, "AC charger not connected\n");
1375 			return -ENXIO;
1376 		}
1377 
1378 		/* Enable AC charging */
1379 		dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1380 
1381 		/*
1382 		 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1383 		 * will be triggered every time we enable the VDD ADC supply.
1384 		 * This will turn off charging for a short while.
1385 		 * It can be avoided by having the supply on when
1386 		 * there is a charger enabled. Normally the VDD ADC supply
1387 		 * is enabled every time a GPADC conversion is triggered.
1388 		 * We will force it to be enabled from this driver to have
1389 		 * the GPADC module independent of the AB8500 chargers
1390 		 */
1391 		if (!di->vddadc_en_ac) {
1392 			ret = regulator_enable(di->regu);
1393 			if (ret)
1394 				dev_warn(di->dev,
1395 					"Failed to enable regulator\n");
1396 			else
1397 				di->vddadc_en_ac = true;
1398 		}
1399 
1400 		/* Check if the requested voltage or current is valid */
1401 		volt_index = ab8500_voltage_to_regval(vset);
1402 		curr_index = ab8500_current_to_regval(di, iset);
1403 		input_curr_index = ab8500_current_to_regval(di,
1404 			di->bm->chg_params->ac_curr_max);
1405 		if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1406 			dev_err(di->dev,
1407 				"Charger voltage or current too high, "
1408 				"charging not started\n");
1409 			return -ENXIO;
1410 		}
1411 
1412 		/* ChVoltLevel: maximum battery charging voltage */
1413 		ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1414 			AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1415 		if (ret) {
1416 			dev_err(di->dev, "%s write failed\n", __func__);
1417 			return ret;
1418 		}
1419 		/* MainChInputCurr: current that can be drawn from the charger*/
1420 		ret = ab8500_charger_set_main_in_curr(di,
1421 			di->bm->chg_params->ac_curr_max);
1422 		if (ret) {
1423 			dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1424 				__func__);
1425 			return ret;
1426 		}
1427 		/* ChOutputCurentLevel: protected output current */
1428 		ret = ab8500_charger_set_output_curr(di, iset);
1429 		if (ret) {
1430 			dev_err(di->dev, "%s "
1431 				"Failed to set ChOutputCurentLevel\n",
1432 				__func__);
1433 			return ret;
1434 		}
1435 
1436 		/* Check if VBAT overshoot control should be enabled */
1437 		if (!di->bm->enable_overshoot)
1438 			overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1439 
1440 		/* Enable Main Charger */
1441 		ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1442 			AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1443 		if (ret) {
1444 			dev_err(di->dev, "%s write failed\n", __func__);
1445 			return ret;
1446 		}
1447 
1448 		/* Power on charging LED indication */
1449 		ret = ab8500_charger_led_en(di, true);
1450 		if (ret < 0)
1451 			dev_err(di->dev, "failed to enable LED\n");
1452 
1453 		di->ac.charger_online = 1;
1454 	} else {
1455 		/* Disable AC charging */
1456 		if (is_ab8500_1p1_or_earlier(di->parent)) {
1457 			/*
1458 			 * For ABB revision 1.0 and 1.1 there is a bug in the
1459 			 * watchdog logic. That means we have to continuously
1460 			 * kick the charger watchdog even when no charger is
1461 			 * connected. This is only valid once the AC charger
1462 			 * has been enabled. This is a bug that is not handled
1463 			 * by the algorithm and the watchdog have to be kicked
1464 			 * by the charger driver when the AC charger
1465 			 * is disabled
1466 			 */
1467 			if (di->ac_conn) {
1468 				queue_delayed_work(di->charger_wq,
1469 					&di->kick_wd_work,
1470 					round_jiffies(WD_KICK_INTERVAL));
1471 			}
1472 
1473 			/*
1474 			 * We can't turn off charging completely
1475 			 * due to a bug in AB8500 cut1.
1476 			 * If we do, charging will not start again.
1477 			 * That is why we set the lowest voltage
1478 			 * and current possible
1479 			 */
1480 			ret = abx500_set_register_interruptible(di->dev,
1481 				AB8500_CHARGER,
1482 				AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1483 			if (ret) {
1484 				dev_err(di->dev,
1485 					"%s write failed\n", __func__);
1486 				return ret;
1487 			}
1488 
1489 			ret = ab8500_charger_set_output_curr(di, 0);
1490 			if (ret) {
1491 				dev_err(di->dev, "%s "
1492 					"Failed to set ChOutputCurentLevel\n",
1493 					__func__);
1494 				return ret;
1495 			}
1496 		} else {
1497 			ret = abx500_set_register_interruptible(di->dev,
1498 				AB8500_CHARGER,
1499 				AB8500_MCH_CTRL1, 0);
1500 			if (ret) {
1501 				dev_err(di->dev,
1502 					"%s write failed\n", __func__);
1503 				return ret;
1504 			}
1505 		}
1506 
1507 		ret = ab8500_charger_led_en(di, false);
1508 		if (ret < 0)
1509 			dev_err(di->dev, "failed to disable LED\n");
1510 
1511 		di->ac.charger_online = 0;
1512 		di->ac.wd_expired = false;
1513 
1514 		/* Disable regulator if enabled */
1515 		if (di->vddadc_en_ac) {
1516 			regulator_disable(di->regu);
1517 			di->vddadc_en_ac = false;
1518 		}
1519 
1520 		dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1521 	}
1522 	ab8500_power_supply_changed(di, di->ac_chg.psy);
1523 
1524 	return ret;
1525 }
1526 
1527 /**
1528  * ab8500_charger_usb_en() - enable usb charging
1529  * @di:		pointer to the ab8500_charger structure
1530  * @enable:	enable/disable flag
1531  * @vset:	charging voltage
1532  * @ich_out:	charger output current
1533  *
1534  * Enable/Disable USB charging and turns on/off the charging led respectively.
1535  * Returns error code in case of failure else 0(on success)
1536  */
1537 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1538 	int enable, int vset, int ich_out)
1539 {
1540 	int ret;
1541 	int volt_index;
1542 	int curr_index;
1543 	u8 overshoot = 0;
1544 
1545 	struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1546 
1547 	if (enable) {
1548 		/* Check if USB is connected */
1549 		if (!di->usb.charger_connected) {
1550 			dev_err(di->dev, "USB charger not connected\n");
1551 			return -ENXIO;
1552 		}
1553 
1554 		/*
1555 		 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1556 		 * will be triggered every time we enable the VDD ADC supply.
1557 		 * This will turn off charging for a short while.
1558 		 * It can be avoided by having the supply on when
1559 		 * there is a charger enabled. Normally the VDD ADC supply
1560 		 * is enabled every time a GPADC conversion is triggered.
1561 		 * We will force it to be enabled from this driver to have
1562 		 * the GPADC module independent of the AB8500 chargers
1563 		 */
1564 		if (!di->vddadc_en_usb) {
1565 			ret = regulator_enable(di->regu);
1566 			if (ret)
1567 				dev_warn(di->dev,
1568 					"Failed to enable regulator\n");
1569 			else
1570 				di->vddadc_en_usb = true;
1571 		}
1572 
1573 		/* Enable USB charging */
1574 		dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1575 
1576 		/* Check if the requested voltage or current is valid */
1577 		volt_index = ab8500_voltage_to_regval(vset);
1578 		curr_index = ab8500_current_to_regval(di, ich_out);
1579 		if (volt_index < 0 || curr_index < 0) {
1580 			dev_err(di->dev,
1581 				"Charger voltage or current too high, "
1582 				"charging not started\n");
1583 			return -ENXIO;
1584 		}
1585 
1586 		/*
1587 		 * ChVoltLevel: max voltage up to which battery can be
1588 		 * charged
1589 		 */
1590 		ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1591 			AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1592 		if (ret) {
1593 			dev_err(di->dev, "%s write failed\n", __func__);
1594 			return ret;
1595 		}
1596 		/* Check if VBAT overshoot control should be enabled */
1597 		if (!di->bm->enable_overshoot)
1598 			overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1599 
1600 		/* Enable USB Charger */
1601 		dev_dbg(di->dev,
1602 			"Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
1603 		ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1604 			AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1605 		if (ret) {
1606 			dev_err(di->dev, "%s write failed\n", __func__);
1607 			return ret;
1608 		}
1609 
1610 		/* If success power on charging LED indication */
1611 		ret = ab8500_charger_led_en(di, true);
1612 		if (ret < 0)
1613 			dev_err(di->dev, "failed to enable LED\n");
1614 
1615 		di->usb.charger_online = 1;
1616 
1617 		/* USBChInputCurr: current that can be drawn from the usb */
1618 		ret = ab8500_charger_set_vbus_in_curr(di,
1619 					di->max_usb_in_curr.usb_type_max);
1620 		if (ret) {
1621 			dev_err(di->dev, "setting USBChInputCurr failed\n");
1622 			return ret;
1623 		}
1624 
1625 		/* ChOutputCurentLevel: protected output current */
1626 		ret = ab8500_charger_set_output_curr(di, ich_out);
1627 		if (ret) {
1628 			dev_err(di->dev, "%s "
1629 				"Failed to set ChOutputCurentLevel\n",
1630 				__func__);
1631 			return ret;
1632 		}
1633 
1634 		queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1635 
1636 	} else {
1637 		/* Disable USB charging */
1638 		dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1639 		ret = abx500_set_register_interruptible(di->dev,
1640 			AB8500_CHARGER,
1641 			AB8500_USBCH_CTRL1_REG, 0);
1642 		if (ret) {
1643 			dev_err(di->dev,
1644 				"%s write failed\n", __func__);
1645 			return ret;
1646 		}
1647 
1648 		ret = ab8500_charger_led_en(di, false);
1649 		if (ret < 0)
1650 			dev_err(di->dev, "failed to disable LED\n");
1651 		/* USBChInputCurr: current that can be drawn from the usb */
1652 		ret = ab8500_charger_set_vbus_in_curr(di, 0);
1653 		if (ret) {
1654 			dev_err(di->dev, "setting USBChInputCurr failed\n");
1655 			return ret;
1656 		}
1657 
1658 		/* ChOutputCurentLevel: protected output current */
1659 		ret = ab8500_charger_set_output_curr(di, 0);
1660 		if (ret) {
1661 			dev_err(di->dev, "%s "
1662 				"Failed to reset ChOutputCurentLevel\n",
1663 				__func__);
1664 			return ret;
1665 		}
1666 		di->usb.charger_online = 0;
1667 		di->usb.wd_expired = false;
1668 
1669 		/* Disable regulator if enabled */
1670 		if (di->vddadc_en_usb) {
1671 			regulator_disable(di->regu);
1672 			di->vddadc_en_usb = false;
1673 		}
1674 
1675 		dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1676 
1677 		/* Cancel any pending Vbat check work */
1678 		cancel_delayed_work(&di->check_vbat_work);
1679 
1680 	}
1681 	ab8500_power_supply_changed(di, di->usb_chg.psy);
1682 
1683 	return ret;
1684 }
1685 
1686 static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1687 				unsigned long event, void *data)
1688 {
1689 	int ret;
1690 	struct device *dev = data;
1691 	/*Toggle External charger control pin*/
1692 	ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1693 				  AB8500_SYS_CHARGER_CONTROL_REG,
1694 				  EXTERNAL_CHARGER_DISABLE_REG_VAL);
1695 	if (ret < 0) {
1696 		dev_err(dev, "write reg failed %d\n", ret);
1697 		goto out;
1698 	}
1699 	ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1700 				  AB8500_SYS_CHARGER_CONTROL_REG,
1701 				  EXTERNAL_CHARGER_ENABLE_REG_VAL);
1702 	if (ret < 0)
1703 		dev_err(dev, "Write reg failed %d\n", ret);
1704 
1705 out:
1706 	return ret;
1707 }
1708 
1709 /**
1710  * ab8500_charger_usb_check_enable() - enable usb charging
1711  * @charger:	pointer to the ux500_charger structure
1712  * @vset:	charging voltage
1713  * @iset:	charger output current
1714  *
1715  * Check if the VBUS charger has been disconnected and reconnected without
1716  * AB8500 rising an interrupt. Returns 0 on success.
1717  */
1718 static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1719 	int vset, int iset)
1720 {
1721 	u8 usbch_ctrl1 = 0;
1722 	int ret = 0;
1723 
1724 	struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1725 
1726 	if (!di->usb.charger_connected)
1727 		return ret;
1728 
1729 	ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1730 				AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1731 	if (ret < 0) {
1732 		dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1733 		return ret;
1734 	}
1735 	dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1736 
1737 	if (!(usbch_ctrl1 & USB_CH_ENA)) {
1738 		dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1739 
1740 		ret = abx500_mask_and_set_register_interruptible(di->dev,
1741 					AB8500_CHARGER, AB8500_CHARGER_CTRL,
1742 					DROP_COUNT_RESET, DROP_COUNT_RESET);
1743 		if (ret < 0) {
1744 			dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1745 			return ret;
1746 		}
1747 
1748 		ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1749 		if (ret < 0) {
1750 			dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1751 					__LINE__);
1752 			return ret;
1753 		}
1754 	}
1755 	return ret;
1756 }
1757 
1758 /**
1759  * ab8500_charger_ac_check_enable() - enable usb charging
1760  * @charger:	pointer to the ux500_charger structure
1761  * @vset:	charging voltage
1762  * @iset:	charger output current
1763  *
1764  * Check if the AC charger has been disconnected and reconnected without
1765  * AB8500 rising an interrupt. Returns 0 on success.
1766  */
1767 static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1768 	int vset, int iset)
1769 {
1770 	u8 mainch_ctrl1 = 0;
1771 	int ret = 0;
1772 
1773 	struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1774 
1775 	if (!di->ac.charger_connected)
1776 		return ret;
1777 
1778 	ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1779 				AB8500_MCH_CTRL1, &mainch_ctrl1);
1780 	if (ret < 0) {
1781 		dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1782 		return ret;
1783 	}
1784 	dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1785 
1786 	if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1787 		dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1788 
1789 		ret = abx500_mask_and_set_register_interruptible(di->dev,
1790 					AB8500_CHARGER, AB8500_CHARGER_CTRL,
1791 					DROP_COUNT_RESET, DROP_COUNT_RESET);
1792 
1793 		if (ret < 0) {
1794 			dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1795 			return ret;
1796 		}
1797 
1798 		ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1799 		if (ret < 0) {
1800 			dev_err(di->dev, "failed to enable AC charger %d\n",
1801 				__LINE__);
1802 			return ret;
1803 		}
1804 	}
1805 	return ret;
1806 }
1807 
1808 /**
1809  * ab8500_charger_watchdog_kick() - kick charger watchdog
1810  * @di:		pointer to the ab8500_charger structure
1811  *
1812  * Kick charger watchdog
1813  * Returns error code in case of failure else 0(on success)
1814  */
1815 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1816 {
1817 	int ret;
1818 	struct ab8500_charger *di;
1819 
1820 	if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1821 		di = to_ab8500_charger_ac_device_info(charger);
1822 	else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1823 		di = to_ab8500_charger_usb_device_info(charger);
1824 	else
1825 		return -ENXIO;
1826 
1827 	ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1828 		AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1829 	if (ret)
1830 		dev_err(di->dev, "Failed to kick WD!\n");
1831 
1832 	return ret;
1833 }
1834 
1835 /**
1836  * ab8500_charger_update_charger_current() - update charger current
1837  * @di:		pointer to the ab8500_charger structure
1838  *
1839  * Update the charger output current for the specified charger
1840  * Returns error code in case of failure else 0(on success)
1841  */
1842 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1843 		int ich_out)
1844 {
1845 	int ret;
1846 	struct ab8500_charger *di;
1847 
1848 	if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
1849 		di = to_ab8500_charger_ac_device_info(charger);
1850 	else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
1851 		di = to_ab8500_charger_usb_device_info(charger);
1852 	else
1853 		return -ENXIO;
1854 
1855 	ret = ab8500_charger_set_output_curr(di, ich_out);
1856 	if (ret) {
1857 		dev_err(di->dev, "%s "
1858 			"Failed to set ChOutputCurentLevel\n",
1859 			__func__);
1860 		return ret;
1861 	}
1862 
1863 	/* Reset the main and usb drop input current measurement counter */
1864 	ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1865 				AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
1866 	if (ret) {
1867 		dev_err(di->dev, "%s write failed\n", __func__);
1868 		return ret;
1869 	}
1870 
1871 	return ret;
1872 }
1873 
1874 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1875 {
1876 	struct power_supply *psy;
1877 	struct power_supply *ext = dev_get_drvdata(dev);
1878 	const char **supplicants = (const char **)ext->supplied_to;
1879 	struct ab8500_charger *di;
1880 	union power_supply_propval ret;
1881 	int j;
1882 	struct ux500_charger *usb_chg;
1883 
1884 	usb_chg = (struct ux500_charger *)data;
1885 	psy = usb_chg->psy;
1886 
1887 	di = to_ab8500_charger_usb_device_info(usb_chg);
1888 
1889 	/* For all psy where the driver name appears in any supplied_to */
1890 	j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
1891 	if (j < 0)
1892 		return 0;
1893 
1894 	/* Go through all properties for the psy */
1895 	for (j = 0; j < ext->desc->num_properties; j++) {
1896 		enum power_supply_property prop;
1897 		prop = ext->desc->properties[j];
1898 
1899 		if (power_supply_get_property(ext, prop, &ret))
1900 			continue;
1901 
1902 		switch (prop) {
1903 		case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1904 			switch (ext->desc->type) {
1905 			case POWER_SUPPLY_TYPE_BATTERY:
1906 				di->vbat = ret.intval / 1000;
1907 				break;
1908 			default:
1909 				break;
1910 			}
1911 			break;
1912 		default:
1913 			break;
1914 		}
1915 	}
1916 	return 0;
1917 }
1918 
1919 /**
1920  * ab8500_charger_check_vbat_work() - keep vbus current within spec
1921  * @work	pointer to the work_struct structure
1922  *
1923  * Due to a asic bug it is necessary to lower the input current to the vbus
1924  * charger when charging with at some specific levels. This issue is only valid
1925  * for below a certain battery voltage. This function makes sure that the
1926  * the allowed current limit isn't exceeded.
1927  */
1928 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1929 {
1930 	int t = 10;
1931 	struct ab8500_charger *di = container_of(work,
1932 		struct ab8500_charger, check_vbat_work.work);
1933 
1934 	class_for_each_device(power_supply_class, NULL,
1935 		di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1936 
1937 	/* First run old_vbat is 0. */
1938 	if (di->old_vbat == 0)
1939 		di->old_vbat = di->vbat;
1940 
1941 	if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1942 		di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1943 		(di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1944 		di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1945 
1946 		dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1947 			" old: %d\n", di->max_usb_in_curr.usb_type_max,
1948 			di->vbat, di->old_vbat);
1949 		ab8500_charger_set_vbus_in_curr(di,
1950 					di->max_usb_in_curr.usb_type_max);
1951 		power_supply_changed(di->usb_chg.psy);
1952 	}
1953 
1954 	di->old_vbat = di->vbat;
1955 
1956 	/*
1957 	 * No need to check the battery voltage every second when not close to
1958 	 * the threshold.
1959 	 */
1960 	if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1961 		(di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1962 			t = 1;
1963 
1964 	queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1965 }
1966 
1967 /**
1968  * ab8500_charger_check_hw_failure_work() - check main charger failure
1969  * @work:	pointer to the work_struct structure
1970  *
1971  * Work queue function for checking the main charger status
1972  */
1973 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1974 {
1975 	int ret;
1976 	u8 reg_value;
1977 
1978 	struct ab8500_charger *di = container_of(work,
1979 		struct ab8500_charger, check_hw_failure_work.work);
1980 
1981 	/* Check if the status bits for HW failure is still active */
1982 	if (di->flags.mainextchnotok) {
1983 		ret = abx500_get_register_interruptible(di->dev,
1984 			AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
1985 		if (ret < 0) {
1986 			dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1987 			return;
1988 		}
1989 		if (!(reg_value & MAIN_CH_NOK)) {
1990 			di->flags.mainextchnotok = false;
1991 			ab8500_power_supply_changed(di, di->ac_chg.psy);
1992 		}
1993 	}
1994 	if (di->flags.vbus_ovv) {
1995 		ret = abx500_get_register_interruptible(di->dev,
1996 			AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1997 			&reg_value);
1998 		if (ret < 0) {
1999 			dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2000 			return;
2001 		}
2002 		if (!(reg_value & VBUS_OVV_TH)) {
2003 			di->flags.vbus_ovv = false;
2004 			ab8500_power_supply_changed(di, di->usb_chg.psy);
2005 		}
2006 	}
2007 	/* If we still have a failure, schedule a new check */
2008 	if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2009 		queue_delayed_work(di->charger_wq,
2010 			&di->check_hw_failure_work, round_jiffies(HZ));
2011 	}
2012 }
2013 
2014 /**
2015  * ab8500_charger_kick_watchdog_work() - kick the watchdog
2016  * @work:	pointer to the work_struct structure
2017  *
2018  * Work queue function for kicking the charger watchdog.
2019  *
2020  * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2021  * logic. That means we have to continuously kick the charger
2022  * watchdog even when no charger is connected. This is only
2023  * valid once the AC charger has been enabled. This is
2024  * a bug that is not handled by the algorithm and the
2025  * watchdog have to be kicked by the charger driver
2026  * when the AC charger is disabled
2027  */
2028 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2029 {
2030 	int ret;
2031 
2032 	struct ab8500_charger *di = container_of(work,
2033 		struct ab8500_charger, kick_wd_work.work);
2034 
2035 	ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2036 		AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2037 	if (ret)
2038 		dev_err(di->dev, "Failed to kick WD!\n");
2039 
2040 	/* Schedule a new watchdog kick */
2041 	queue_delayed_work(di->charger_wq,
2042 		&di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2043 }
2044 
2045 /**
2046  * ab8500_charger_ac_work() - work to get and set main charger status
2047  * @work:	pointer to the work_struct structure
2048  *
2049  * Work queue function for checking the main charger status
2050  */
2051 static void ab8500_charger_ac_work(struct work_struct *work)
2052 {
2053 	int ret;
2054 
2055 	struct ab8500_charger *di = container_of(work,
2056 		struct ab8500_charger, ac_work);
2057 
2058 	/*
2059 	 * Since we can't be sure that the events are received
2060 	 * synchronously, we have the check if the main charger is
2061 	 * connected by reading the status register
2062 	 */
2063 	ret = ab8500_charger_detect_chargers(di, false);
2064 	if (ret < 0)
2065 		return;
2066 
2067 	if (ret & AC_PW_CONN) {
2068 		di->ac.charger_connected = 1;
2069 		di->ac_conn = true;
2070 	} else {
2071 		di->ac.charger_connected = 0;
2072 	}
2073 
2074 	ab8500_power_supply_changed(di, di->ac_chg.psy);
2075 	sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
2076 }
2077 
2078 static void ab8500_charger_usb_attached_work(struct work_struct *work)
2079 {
2080 	struct ab8500_charger *di = container_of(work,
2081 						 struct ab8500_charger,
2082 						 usb_charger_attached_work.work);
2083 	int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2084 	int ret, i;
2085 	u8 statval;
2086 
2087 	for (i = 0; i < 10; i++) {
2088 		ret = abx500_get_register_interruptible(di->dev,
2089 							AB8500_CHARGER,
2090 							AB8500_CH_USBCH_STAT1_REG,
2091 							&statval);
2092 		if (ret < 0) {
2093 			dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2094 			goto reschedule;
2095 		}
2096 		if ((statval & usbch) != usbch)
2097 			goto reschedule;
2098 
2099 		msleep(CHARGER_STATUS_POLL);
2100 	}
2101 
2102 	ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2103 
2104 	mutex_lock(&di->charger_attached_mutex);
2105 	mutex_unlock(&di->charger_attached_mutex);
2106 
2107 	return;
2108 
2109 reschedule:
2110 	queue_delayed_work(di->charger_wq,
2111 			   &di->usb_charger_attached_work,
2112 			   HZ);
2113 }
2114 
2115 static void ab8500_charger_ac_attached_work(struct work_struct *work)
2116 {
2117 
2118 	struct ab8500_charger *di = container_of(work,
2119 						 struct ab8500_charger,
2120 						 ac_charger_attached_work.work);
2121 	int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2122 		      MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2123 	int ret, i;
2124 	u8 statval;
2125 
2126 	for (i = 0; i < 10; i++) {
2127 		ret = abx500_get_register_interruptible(di->dev,
2128 							AB8500_CHARGER,
2129 							AB8500_CH_STATUS2_REG,
2130 							&statval);
2131 		if (ret < 0) {
2132 			dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2133 			goto reschedule;
2134 		}
2135 
2136 		if ((statval & mainch) != mainch)
2137 			goto reschedule;
2138 
2139 		msleep(CHARGER_STATUS_POLL);
2140 	}
2141 
2142 	ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2143 	queue_work(di->charger_wq, &di->ac_work);
2144 
2145 	mutex_lock(&di->charger_attached_mutex);
2146 	mutex_unlock(&di->charger_attached_mutex);
2147 
2148 	return;
2149 
2150 reschedule:
2151 	queue_delayed_work(di->charger_wq,
2152 			   &di->ac_charger_attached_work,
2153 			   HZ);
2154 }
2155 
2156 /**
2157  * ab8500_charger_detect_usb_type_work() - work to detect USB type
2158  * @work:	Pointer to the work_struct structure
2159  *
2160  * Detect the type of USB plugged
2161  */
2162 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
2163 {
2164 	int ret;
2165 
2166 	struct ab8500_charger *di = container_of(work,
2167 		struct ab8500_charger, detect_usb_type_work);
2168 
2169 	/*
2170 	 * Since we can't be sure that the events are received
2171 	 * synchronously, we have the check if is
2172 	 * connected by reading the status register
2173 	 */
2174 	ret = ab8500_charger_detect_chargers(di, false);
2175 	if (ret < 0)
2176 		return;
2177 
2178 	if (!(ret & USB_PW_CONN)) {
2179 		dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2180 		di->vbus_detected = false;
2181 		ab8500_charger_set_usb_connected(di, false);
2182 		ab8500_power_supply_changed(di, di->usb_chg.psy);
2183 	} else {
2184 		dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2185 		di->vbus_detected = true;
2186 
2187 		if (is_ab8500_1p1_or_earlier(di->parent)) {
2188 			ret = ab8500_charger_detect_usb_type(di);
2189 			if (!ret) {
2190 				ab8500_charger_set_usb_connected(di, true);
2191 				ab8500_power_supply_changed(di,
2192 							    di->usb_chg.psy);
2193 			}
2194 		} else {
2195 			/*
2196 			 * For ABB cut2.0 and onwards we have an IRQ,
2197 			 * USB_LINK_STATUS that will be triggered when the USB
2198 			 * link status changes. The exception is USB connected
2199 			 * during startup. Then we don't get a
2200 			 * USB_LINK_STATUS IRQ
2201 			 */
2202 			if (di->vbus_detected_start) {
2203 				di->vbus_detected_start = false;
2204 				ret = ab8500_charger_detect_usb_type(di);
2205 				if (!ret) {
2206 					ab8500_charger_set_usb_connected(di,
2207 						true);
2208 					ab8500_power_supply_changed(di,
2209 						di->usb_chg.psy);
2210 				}
2211 			}
2212 		}
2213 	}
2214 }
2215 
2216 /**
2217  * ab8500_charger_usb_link_attach_work() - work to detect USB type
2218  * @work:	pointer to the work_struct structure
2219  *
2220  * Detect the type of USB plugged
2221  */
2222 static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2223 {
2224 	struct ab8500_charger *di =
2225 		container_of(work, struct ab8500_charger, attach_work.work);
2226 	int ret;
2227 
2228 	/* Update maximum input current if USB enumeration is not detected */
2229 	if (!di->usb.charger_online) {
2230 		ret = ab8500_charger_set_vbus_in_curr(di,
2231 					di->max_usb_in_curr.usb_type_max);
2232 		if (ret)
2233 			return;
2234 	}
2235 
2236 	ab8500_charger_set_usb_connected(di, true);
2237 	ab8500_power_supply_changed(di, di->usb_chg.psy);
2238 }
2239 
2240 /**
2241  * ab8500_charger_usb_link_status_work() - work to detect USB type
2242  * @work:	pointer to the work_struct structure
2243  *
2244  * Detect the type of USB plugged
2245  */
2246 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2247 {
2248 	int detected_chargers;
2249 	int ret;
2250 	u8 val;
2251 	u8 link_status;
2252 
2253 	struct ab8500_charger *di = container_of(work,
2254 		struct ab8500_charger, usb_link_status_work);
2255 
2256 	/*
2257 	 * Since we can't be sure that the events are received
2258 	 * synchronously, we have the check if  is
2259 	 * connected by reading the status register
2260 	 */
2261 	detected_chargers = ab8500_charger_detect_chargers(di, false);
2262 	if (detected_chargers < 0)
2263 		return;
2264 
2265 	/*
2266 	 * Some chargers that breaks the USB spec is
2267 	 * identified as invalid by AB8500 and it refuse
2268 	 * to start the charging process. but by jumping
2269 	 * through a few hoops it can be forced to start.
2270 	 */
2271 	if (is_ab8500(di->parent))
2272 		ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2273 					AB8500_USB_LINE_STAT_REG, &val);
2274 	else
2275 		ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2276 					AB8500_USB_LINK1_STAT_REG, &val);
2277 
2278 	if (ret >= 0)
2279 		dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2280 	else
2281 		dev_dbg(di->dev, "Error reading USB link status\n");
2282 
2283 	if (is_ab8500(di->parent))
2284 		link_status = AB8500_USB_LINK_STATUS;
2285 	else
2286 		link_status = AB8505_USB_LINK_STATUS;
2287 
2288 	if (detected_chargers & USB_PW_CONN) {
2289 		if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
2290 				USB_STAT_NOT_VALID_LINK &&
2291 				di->invalid_charger_detect_state == 0) {
2292 			dev_dbg(di->dev,
2293 					"Invalid charger detected, state= 0\n");
2294 			/*Enable charger*/
2295 			abx500_mask_and_set_register_interruptible(di->dev,
2296 					AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
2297 					USB_CH_ENA, USB_CH_ENA);
2298 			/*Enable charger detection*/
2299 			abx500_mask_and_set_register_interruptible(di->dev,
2300 					AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2301 					USB_CH_DET, USB_CH_DET);
2302 			di->invalid_charger_detect_state = 1;
2303 			/*exit and wait for new link status interrupt.*/
2304 			return;
2305 
2306 		}
2307 		if (di->invalid_charger_detect_state == 1) {
2308 			dev_dbg(di->dev,
2309 					"Invalid charger detected, state= 1\n");
2310 			/*Stop charger detection*/
2311 			abx500_mask_and_set_register_interruptible(di->dev,
2312 					AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
2313 					USB_CH_DET, 0x00);
2314 			/*Check link status*/
2315 			if (is_ab8500(di->parent))
2316 				ret = abx500_get_register_interruptible(di->dev,
2317 					AB8500_USB, AB8500_USB_LINE_STAT_REG,
2318 					&val);
2319 			else
2320 				ret = abx500_get_register_interruptible(di->dev,
2321 					AB8500_USB, AB8500_USB_LINK1_STAT_REG,
2322 					&val);
2323 
2324 			dev_dbg(di->dev, "USB link status= 0x%02x\n",
2325 				(val & link_status) >> USB_LINK_STATUS_SHIFT);
2326 			di->invalid_charger_detect_state = 2;
2327 		}
2328 	} else {
2329 		di->invalid_charger_detect_state = 0;
2330 	}
2331 
2332 	if (!(detected_chargers & USB_PW_CONN)) {
2333 		di->vbus_detected = false;
2334 		ab8500_charger_set_usb_connected(di, false);
2335 		ab8500_power_supply_changed(di, di->usb_chg.psy);
2336 		return;
2337 	}
2338 
2339 	dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2340 	di->vbus_detected = true;
2341 	ret = ab8500_charger_read_usb_type(di);
2342 	if (ret) {
2343 		if (ret == -ENXIO) {
2344 			/* No valid charger type detected */
2345 			ab8500_charger_set_usb_connected(di, false);
2346 			ab8500_power_supply_changed(di, di->usb_chg.psy);
2347 		}
2348 		return;
2349 	}
2350 
2351 	if (di->usb_device_is_unrecognised) {
2352 		dev_dbg(di->dev,
2353 			"Potential Legacy Charger device. "
2354 			"Delay work for %d msec for USB enum "
2355 			"to finish",
2356 			WAIT_ACA_RID_ENUMERATION);
2357 		queue_delayed_work(di->charger_wq,
2358 				   &di->attach_work,
2359 				   msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2360 	} else if (di->is_aca_rid == 1) {
2361 		/* Only wait once */
2362 		di->is_aca_rid++;
2363 		dev_dbg(di->dev,
2364 			"%s Wait %d msec for USB enum to finish",
2365 			__func__, WAIT_ACA_RID_ENUMERATION);
2366 		queue_delayed_work(di->charger_wq,
2367 				   &di->attach_work,
2368 				   msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2369 	} else {
2370 		queue_delayed_work(di->charger_wq,
2371 				   &di->attach_work,
2372 				   0);
2373 	}
2374 }
2375 
2376 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2377 {
2378 	int ret;
2379 	unsigned long flags;
2380 
2381 	struct ab8500_charger *di = container_of(work,
2382 		struct ab8500_charger, usb_state_changed_work.work);
2383 
2384 	if (!di->vbus_detected)	{
2385 		dev_dbg(di->dev,
2386 			"%s !di->vbus_detected\n",
2387 			__func__);
2388 		return;
2389 	}
2390 
2391 	spin_lock_irqsave(&di->usb_state.usb_lock, flags);
2392 	di->usb_state.state = di->usb_state.state_tmp;
2393 	di->usb_state.usb_current = di->usb_state.usb_current_tmp;
2394 	spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2395 
2396 	dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2397 		__func__, di->usb_state.state, di->usb_state.usb_current);
2398 
2399 	switch (di->usb_state.state) {
2400 	case AB8500_BM_USB_STATE_RESET_HS:
2401 	case AB8500_BM_USB_STATE_RESET_FS:
2402 	case AB8500_BM_USB_STATE_SUSPEND:
2403 	case AB8500_BM_USB_STATE_MAX:
2404 		ab8500_charger_set_usb_connected(di, false);
2405 		ab8500_power_supply_changed(di, di->usb_chg.psy);
2406 		break;
2407 
2408 	case AB8500_BM_USB_STATE_RESUME:
2409 		/*
2410 		 * when suspend->resume there should be delay
2411 		 * of 1sec for enabling charging
2412 		 */
2413 		msleep(1000);
2414 		fallthrough;
2415 	case AB8500_BM_USB_STATE_CONFIGURED:
2416 		/*
2417 		 * USB is configured, enable charging with the charging
2418 		 * input current obtained from USB driver
2419 		 */
2420 		if (!ab8500_charger_get_usb_cur(di)) {
2421 			/* Update maximum input current */
2422 			ret = ab8500_charger_set_vbus_in_curr(di,
2423 					di->max_usb_in_curr.usb_type_max);
2424 			if (ret)
2425 				return;
2426 
2427 			ab8500_charger_set_usb_connected(di, true);
2428 			ab8500_power_supply_changed(di, di->usb_chg.psy);
2429 		}
2430 		break;
2431 
2432 	default:
2433 		break;
2434 	}
2435 }
2436 
2437 /**
2438  * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2439  * @work:	pointer to the work_struct structure
2440  *
2441  * Work queue function for checking the USB charger Not OK status
2442  */
2443 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2444 {
2445 	int ret;
2446 	u8 reg_value;
2447 	bool prev_status;
2448 
2449 	struct ab8500_charger *di = container_of(work,
2450 		struct ab8500_charger, check_usbchgnotok_work.work);
2451 
2452 	/* Check if the status bit for usbchargernotok is still active */
2453 	ret = abx500_get_register_interruptible(di->dev,
2454 		AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2455 	if (ret < 0) {
2456 		dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2457 		return;
2458 	}
2459 	prev_status = di->flags.usbchargernotok;
2460 
2461 	if (reg_value & VBUS_CH_NOK) {
2462 		di->flags.usbchargernotok = true;
2463 		/* Check again in 1sec */
2464 		queue_delayed_work(di->charger_wq,
2465 			&di->check_usbchgnotok_work, HZ);
2466 	} else {
2467 		di->flags.usbchargernotok = false;
2468 		di->flags.vbus_collapse = false;
2469 	}
2470 
2471 	if (prev_status != di->flags.usbchargernotok)
2472 		ab8500_power_supply_changed(di, di->usb_chg.psy);
2473 }
2474 
2475 /**
2476  * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2477  * @work:	pointer to the work_struct structure
2478  *
2479  * Work queue function for checking the Main thermal prot status
2480  */
2481 static void ab8500_charger_check_main_thermal_prot_work(
2482 	struct work_struct *work)
2483 {
2484 	int ret;
2485 	u8 reg_value;
2486 
2487 	struct ab8500_charger *di = container_of(work,
2488 		struct ab8500_charger, check_main_thermal_prot_work);
2489 
2490 	/* Check if the status bit for main_thermal_prot is still active */
2491 	ret = abx500_get_register_interruptible(di->dev,
2492 		AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2493 	if (ret < 0) {
2494 		dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2495 		return;
2496 	}
2497 	if (reg_value & MAIN_CH_TH_PROT)
2498 		di->flags.main_thermal_prot = true;
2499 	else
2500 		di->flags.main_thermal_prot = false;
2501 
2502 	ab8500_power_supply_changed(di, di->ac_chg.psy);
2503 }
2504 
2505 /**
2506  * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2507  * @work:	pointer to the work_struct structure
2508  *
2509  * Work queue function for checking the USB thermal prot status
2510  */
2511 static void ab8500_charger_check_usb_thermal_prot_work(
2512 	struct work_struct *work)
2513 {
2514 	int ret;
2515 	u8 reg_value;
2516 
2517 	struct ab8500_charger *di = container_of(work,
2518 		struct ab8500_charger, check_usb_thermal_prot_work);
2519 
2520 	/* Check if the status bit for usb_thermal_prot is still active */
2521 	ret = abx500_get_register_interruptible(di->dev,
2522 		AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2523 	if (ret < 0) {
2524 		dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2525 		return;
2526 	}
2527 	if (reg_value & USB_CH_TH_PROT)
2528 		di->flags.usb_thermal_prot = true;
2529 	else
2530 		di->flags.usb_thermal_prot = false;
2531 
2532 	ab8500_power_supply_changed(di, di->usb_chg.psy);
2533 }
2534 
2535 /**
2536  * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2537  * @irq:       interrupt number
2538  * @_di:       pointer to the ab8500_charger structure
2539  *
2540  * Returns IRQ status(IRQ_HANDLED)
2541  */
2542 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2543 {
2544 	struct ab8500_charger *di = _di;
2545 
2546 	dev_dbg(di->dev, "Main charger unplugged\n");
2547 	queue_work(di->charger_wq, &di->ac_work);
2548 
2549 	cancel_delayed_work_sync(&di->ac_charger_attached_work);
2550 	mutex_lock(&di->charger_attached_mutex);
2551 	mutex_unlock(&di->charger_attached_mutex);
2552 
2553 	return IRQ_HANDLED;
2554 }
2555 
2556 /**
2557  * ab8500_charger_mainchplugdet_handler() - main charger plugged
2558  * @irq:       interrupt number
2559  * @_di:       pointer to the ab8500_charger structure
2560  *
2561  * Returns IRQ status(IRQ_HANDLED)
2562  */
2563 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2564 {
2565 	struct ab8500_charger *di = _di;
2566 
2567 	dev_dbg(di->dev, "Main charger plugged\n");
2568 	queue_work(di->charger_wq, &di->ac_work);
2569 
2570 	mutex_lock(&di->charger_attached_mutex);
2571 	mutex_unlock(&di->charger_attached_mutex);
2572 
2573 	if (is_ab8500(di->parent))
2574 		queue_delayed_work(di->charger_wq,
2575 			   &di->ac_charger_attached_work,
2576 			   HZ);
2577 	return IRQ_HANDLED;
2578 }
2579 
2580 /**
2581  * ab8500_charger_mainextchnotok_handler() - main charger not ok
2582  * @irq:       interrupt number
2583  * @_di:       pointer to the ab8500_charger structure
2584  *
2585  * Returns IRQ status(IRQ_HANDLED)
2586  */
2587 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2588 {
2589 	struct ab8500_charger *di = _di;
2590 
2591 	dev_dbg(di->dev, "Main charger not ok\n");
2592 	di->flags.mainextchnotok = true;
2593 	ab8500_power_supply_changed(di, di->ac_chg.psy);
2594 
2595 	/* Schedule a new HW failure check */
2596 	queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2597 
2598 	return IRQ_HANDLED;
2599 }
2600 
2601 /**
2602  * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2603  * thermal protection threshold
2604  * @irq:       interrupt number
2605  * @_di:       pointer to the ab8500_charger structure
2606  *
2607  * Returns IRQ status(IRQ_HANDLED)
2608  */
2609 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2610 {
2611 	struct ab8500_charger *di = _di;
2612 
2613 	dev_dbg(di->dev,
2614 		"Die temp above Main charger thermal protection threshold\n");
2615 	queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2616 
2617 	return IRQ_HANDLED;
2618 }
2619 
2620 /**
2621  * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2622  * thermal protection threshold
2623  * @irq:       interrupt number
2624  * @_di:       pointer to the ab8500_charger structure
2625  *
2626  * Returns IRQ status(IRQ_HANDLED)
2627  */
2628 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2629 {
2630 	struct ab8500_charger *di = _di;
2631 
2632 	dev_dbg(di->dev,
2633 		"Die temp ok for Main charger thermal protection threshold\n");
2634 	queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2635 
2636 	return IRQ_HANDLED;
2637 }
2638 
2639 static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2640 {
2641 	struct ab8500_charger *di = container_of(work,
2642 		struct ab8500_charger, vbus_drop_end_work.work);
2643 	int ret, curr;
2644 	u8 reg_value;
2645 
2646 	di->flags.vbus_drop_end = false;
2647 
2648 	/* Reset the drop counter */
2649 	abx500_set_register_interruptible(di->dev,
2650 				  AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
2651 
2652 	ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2653 			AB8500_CH_USBCH_STAT2_REG, &reg_value);
2654 	if (ret < 0) {
2655 		dev_err(di->dev, "%s read failed\n", __func__);
2656 		return;
2657 	}
2658 
2659 	curr = di->bm->chg_input_curr[
2660 		reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
2661 
2662 	if (di->max_usb_in_curr.calculated_max != curr) {
2663 		/* USB source is collapsing */
2664 		di->max_usb_in_curr.calculated_max = curr;
2665 		dev_dbg(di->dev,
2666 			 "VBUS input current limiting to %d mA\n",
2667 			 di->max_usb_in_curr.calculated_max);
2668 	} else {
2669 		/*
2670 		 * USB source can not give more than this amount.
2671 		 * Taking more will collapse the source.
2672 		 */
2673 		di->max_usb_in_curr.set_max =
2674 			di->max_usb_in_curr.calculated_max;
2675 		dev_dbg(di->dev,
2676 			 "VBUS input current limited to %d mA\n",
2677 			 di->max_usb_in_curr.set_max);
2678 	}
2679 
2680 	if (di->usb.charger_connected)
2681 		ab8500_charger_set_vbus_in_curr(di,
2682 					di->max_usb_in_curr.usb_type_max);
2683 }
2684 
2685 /**
2686  * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2687  * @irq:       interrupt number
2688  * @_di:       pointer to the ab8500_charger structure
2689  *
2690  * Returns IRQ status(IRQ_HANDLED)
2691  */
2692 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2693 {
2694 	struct ab8500_charger *di = _di;
2695 
2696 	di->vbus_detected = false;
2697 	dev_dbg(di->dev, "VBUS falling detected\n");
2698 	queue_work(di->charger_wq, &di->detect_usb_type_work);
2699 
2700 	return IRQ_HANDLED;
2701 }
2702 
2703 /**
2704  * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2705  * @irq:       interrupt number
2706  * @_di:       pointer to the ab8500_charger structure
2707  *
2708  * Returns IRQ status(IRQ_HANDLED)
2709  */
2710 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2711 {
2712 	struct ab8500_charger *di = _di;
2713 
2714 	di->vbus_detected = true;
2715 	dev_dbg(di->dev, "VBUS rising detected\n");
2716 
2717 	queue_work(di->charger_wq, &di->detect_usb_type_work);
2718 
2719 	return IRQ_HANDLED;
2720 }
2721 
2722 /**
2723  * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2724  * @irq:       interrupt number
2725  * @_di:       pointer to the ab8500_charger structure
2726  *
2727  * Returns IRQ status(IRQ_HANDLED)
2728  */
2729 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2730 {
2731 	struct ab8500_charger *di = _di;
2732 
2733 	dev_dbg(di->dev, "USB link status changed\n");
2734 
2735 	queue_work(di->charger_wq, &di->usb_link_status_work);
2736 
2737 	return IRQ_HANDLED;
2738 }
2739 
2740 /**
2741  * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2742  * thermal protection threshold
2743  * @irq:       interrupt number
2744  * @_di:       pointer to the ab8500_charger structure
2745  *
2746  * Returns IRQ status(IRQ_HANDLED)
2747  */
2748 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2749 {
2750 	struct ab8500_charger *di = _di;
2751 
2752 	dev_dbg(di->dev,
2753 		"Die temp above USB charger thermal protection threshold\n");
2754 	queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2755 
2756 	return IRQ_HANDLED;
2757 }
2758 
2759 /**
2760  * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2761  * thermal protection threshold
2762  * @irq:       interrupt number
2763  * @_di:       pointer to the ab8500_charger structure
2764  *
2765  * Returns IRQ status(IRQ_HANDLED)
2766  */
2767 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2768 {
2769 	struct ab8500_charger *di = _di;
2770 
2771 	dev_dbg(di->dev,
2772 		"Die temp ok for USB charger thermal protection threshold\n");
2773 	queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2774 
2775 	return IRQ_HANDLED;
2776 }
2777 
2778 /**
2779  * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2780  * @irq:       interrupt number
2781  * @_di:       pointer to the ab8500_charger structure
2782  *
2783  * Returns IRQ status(IRQ_HANDLED)
2784  */
2785 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2786 {
2787 	struct ab8500_charger *di = _di;
2788 
2789 	dev_dbg(di->dev, "Not allowed USB charger detected\n");
2790 	queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2791 
2792 	return IRQ_HANDLED;
2793 }
2794 
2795 /**
2796  * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2797  * @irq:       interrupt number
2798  * @_di:       pointer to the ab8500_charger structure
2799  *
2800  * Returns IRQ status(IRQ_HANDLED)
2801  */
2802 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2803 {
2804 	struct ab8500_charger *di = _di;
2805 
2806 	dev_dbg(di->dev, "Charger watchdog expired\n");
2807 
2808 	/*
2809 	 * The charger that was online when the watchdog expired
2810 	 * needs to be restarted for charging to start again
2811 	 */
2812 	if (di->ac.charger_online) {
2813 		di->ac.wd_expired = true;
2814 		ab8500_power_supply_changed(di, di->ac_chg.psy);
2815 	}
2816 	if (di->usb.charger_online) {
2817 		di->usb.wd_expired = true;
2818 		ab8500_power_supply_changed(di, di->usb_chg.psy);
2819 	}
2820 
2821 	return IRQ_HANDLED;
2822 }
2823 
2824 /**
2825  * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2826  * @irq:       interrupt number
2827  * @_di:       pointer to the ab8500_charger structure
2828  *
2829  * Returns IRQ status(IRQ_HANDLED)
2830  */
2831 static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2832 {
2833 	struct ab8500_charger *di = _di;
2834 
2835 	dev_dbg(di->dev, "VBUS charger drop ended\n");
2836 	di->flags.vbus_drop_end = true;
2837 
2838 	/*
2839 	 * VBUS might have dropped due to bad connection.
2840 	 * Schedule a new input limit set to the value SW requests.
2841 	 */
2842 	queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
2843 			   round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
2844 
2845 	return IRQ_HANDLED;
2846 }
2847 
2848 /**
2849  * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2850  * @irq:       interrupt number
2851  * @_di:       pointer to the ab8500_charger structure
2852  *
2853  * Returns IRQ status(IRQ_HANDLED)
2854  */
2855 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2856 {
2857 	struct ab8500_charger *di = _di;
2858 
2859 	dev_dbg(di->dev, "VBUS overvoltage detected\n");
2860 	di->flags.vbus_ovv = true;
2861 	ab8500_power_supply_changed(di, di->usb_chg.psy);
2862 
2863 	/* Schedule a new HW failure check */
2864 	queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2865 
2866 	return IRQ_HANDLED;
2867 }
2868 
2869 /**
2870  * ab8500_charger_ac_get_property() - get the ac/mains properties
2871  * @psy:       pointer to the power_supply structure
2872  * @psp:       pointer to the power_supply_property structure
2873  * @val:       pointer to the power_supply_propval union
2874  *
2875  * This function gets called when an application tries to get the ac/mains
2876  * properties by reading the sysfs files.
2877  * AC/Mains properties are online, present and voltage.
2878  * online:     ac/mains charging is in progress or not
2879  * present:    presence of the ac/mains
2880  * voltage:    AC/Mains voltage
2881  * Returns error code in case of failure else 0(on success)
2882  */
2883 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2884 	enum power_supply_property psp,
2885 	union power_supply_propval *val)
2886 {
2887 	struct ab8500_charger *di;
2888 	int ret;
2889 
2890 	di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2891 
2892 	switch (psp) {
2893 	case POWER_SUPPLY_PROP_HEALTH:
2894 		if (di->flags.mainextchnotok)
2895 			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2896 		else if (di->ac.wd_expired || di->usb.wd_expired)
2897 			val->intval = POWER_SUPPLY_HEALTH_DEAD;
2898 		else if (di->flags.main_thermal_prot)
2899 			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2900 		else
2901 			val->intval = POWER_SUPPLY_HEALTH_GOOD;
2902 		break;
2903 	case POWER_SUPPLY_PROP_ONLINE:
2904 		val->intval = di->ac.charger_online;
2905 		break;
2906 	case POWER_SUPPLY_PROP_PRESENT:
2907 		val->intval = di->ac.charger_connected;
2908 		break;
2909 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2910 		ret = ab8500_charger_get_ac_voltage(di);
2911 		if (ret >= 0)
2912 			di->ac.charger_voltage = ret;
2913 		/* On error, use previous value */
2914 		val->intval = di->ac.charger_voltage * 1000;
2915 		break;
2916 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2917 		/*
2918 		 * This property is used to indicate when CV mode is entered
2919 		 * for the AC charger
2920 		 */
2921 		di->ac.cv_active = ab8500_charger_ac_cv(di);
2922 		val->intval = di->ac.cv_active;
2923 		break;
2924 	case POWER_SUPPLY_PROP_CURRENT_NOW:
2925 		ret = ab8500_charger_get_ac_current(di);
2926 		if (ret >= 0)
2927 			di->ac.charger_current = ret;
2928 		val->intval = di->ac.charger_current * 1000;
2929 		break;
2930 	default:
2931 		return -EINVAL;
2932 	}
2933 	return 0;
2934 }
2935 
2936 /**
2937  * ab8500_charger_usb_get_property() - get the usb properties
2938  * @psy:        pointer to the power_supply structure
2939  * @psp:        pointer to the power_supply_property structure
2940  * @val:        pointer to the power_supply_propval union
2941  *
2942  * This function gets called when an application tries to get the usb
2943  * properties by reading the sysfs files.
2944  * USB properties are online, present and voltage.
2945  * online:     usb charging is in progress or not
2946  * present:    presence of the usb
2947  * voltage:    vbus voltage
2948  * Returns error code in case of failure else 0(on success)
2949  */
2950 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2951 	enum power_supply_property psp,
2952 	union power_supply_propval *val)
2953 {
2954 	struct ab8500_charger *di;
2955 	int ret;
2956 
2957 	di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2958 
2959 	switch (psp) {
2960 	case POWER_SUPPLY_PROP_HEALTH:
2961 		if (di->flags.usbchargernotok)
2962 			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2963 		else if (di->ac.wd_expired || di->usb.wd_expired)
2964 			val->intval = POWER_SUPPLY_HEALTH_DEAD;
2965 		else if (di->flags.usb_thermal_prot)
2966 			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2967 		else if (di->flags.vbus_ovv)
2968 			val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2969 		else
2970 			val->intval = POWER_SUPPLY_HEALTH_GOOD;
2971 		break;
2972 	case POWER_SUPPLY_PROP_ONLINE:
2973 		val->intval = di->usb.charger_online;
2974 		break;
2975 	case POWER_SUPPLY_PROP_PRESENT:
2976 		val->intval = di->usb.charger_connected;
2977 		break;
2978 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2979 		ret = ab8500_charger_get_vbus_voltage(di);
2980 		if (ret >= 0)
2981 			di->usb.charger_voltage = ret;
2982 		val->intval = di->usb.charger_voltage * 1000;
2983 		break;
2984 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2985 		/*
2986 		 * This property is used to indicate when CV mode is entered
2987 		 * for the USB charger
2988 		 */
2989 		di->usb.cv_active = ab8500_charger_usb_cv(di);
2990 		val->intval = di->usb.cv_active;
2991 		break;
2992 	case POWER_SUPPLY_PROP_CURRENT_NOW:
2993 		ret = ab8500_charger_get_usb_current(di);
2994 		if (ret >= 0)
2995 			di->usb.charger_current = ret;
2996 		val->intval = di->usb.charger_current * 1000;
2997 		break;
2998 	case POWER_SUPPLY_PROP_CURRENT_AVG:
2999 		/*
3000 		 * This property is used to indicate when VBUS has collapsed
3001 		 * due to too high output current from the USB charger
3002 		 */
3003 		if (di->flags.vbus_collapse)
3004 			val->intval = 1;
3005 		else
3006 			val->intval = 0;
3007 		break;
3008 	default:
3009 		return -EINVAL;
3010 	}
3011 	return 0;
3012 }
3013 
3014 /**
3015  * ab8500_charger_init_hw_registers() - Set up charger related registers
3016  * @di:		pointer to the ab8500_charger structure
3017  *
3018  * Set up charger OVV, watchdog and maximum voltage registers as well as
3019  * charging of the backup battery
3020  */
3021 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3022 {
3023 	int ret = 0;
3024 
3025 	/* Setup maximum charger current and voltage for ABB cut2.0 */
3026 	if (!is_ab8500_1p1_or_earlier(di->parent)) {
3027 		ret = abx500_set_register_interruptible(di->dev,
3028 			AB8500_CHARGER,
3029 			AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3030 		if (ret) {
3031 			dev_err(di->dev,
3032 				"failed to set CH_VOLT_LVL_MAX_REG\n");
3033 			goto out;
3034 		}
3035 
3036 		ret = abx500_set_register_interruptible(di->dev,
3037 			AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
3038 			CH_OP_CUR_LVL_1P6);
3039 		if (ret) {
3040 			dev_err(di->dev,
3041 				"failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3042 			goto out;
3043 		}
3044 	}
3045 
3046 	if (is_ab8505_2p0(di->parent))
3047 		ret = abx500_mask_and_set_register_interruptible(di->dev,
3048 			AB8500_CHARGER,
3049 			AB8500_USBCH_CTRL2_REG,
3050 			VBUS_AUTO_IN_CURR_LIM_ENA,
3051 			VBUS_AUTO_IN_CURR_LIM_ENA);
3052 	else
3053 		/*
3054 		 * VBUS OVV set to 6.3V and enable automatic current limitation
3055 		 */
3056 		ret = abx500_set_register_interruptible(di->dev,
3057 			AB8500_CHARGER,
3058 			AB8500_USBCH_CTRL2_REG,
3059 			VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
3060 	if (ret) {
3061 		dev_err(di->dev,
3062 			"failed to set automatic current limitation\n");
3063 		goto out;
3064 	}
3065 
3066 	/* Enable main watchdog in OTP */
3067 	ret = abx500_set_register_interruptible(di->dev,
3068 		AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3069 	if (ret) {
3070 		dev_err(di->dev, "failed to enable main WD in OTP\n");
3071 		goto out;
3072 	}
3073 
3074 	/* Enable main watchdog */
3075 	ret = abx500_set_register_interruptible(di->dev,
3076 		AB8500_SYS_CTRL2_BLOCK,
3077 		AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3078 	if (ret) {
3079 		dev_err(di->dev, "failed to enable main watchdog\n");
3080 		goto out;
3081 	}
3082 
3083 	/*
3084 	 * Due to internal synchronisation, Enable and Kick watchdog bits
3085 	 * cannot be enabled in a single write.
3086 	 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3087 	 * between writing Enable then Kick bits.
3088 	 */
3089 	udelay(63);
3090 
3091 	/* Kick main watchdog */
3092 	ret = abx500_set_register_interruptible(di->dev,
3093 		AB8500_SYS_CTRL2_BLOCK,
3094 		AB8500_MAIN_WDOG_CTRL_REG,
3095 		(MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3096 	if (ret) {
3097 		dev_err(di->dev, "failed to kick main watchdog\n");
3098 		goto out;
3099 	}
3100 
3101 	/* Disable main watchdog */
3102 	ret = abx500_set_register_interruptible(di->dev,
3103 		AB8500_SYS_CTRL2_BLOCK,
3104 		AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3105 	if (ret) {
3106 		dev_err(di->dev, "failed to disable main watchdog\n");
3107 		goto out;
3108 	}
3109 
3110 	/* Set watchdog timeout */
3111 	ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3112 		AB8500_CH_WD_TIMER_REG, WD_TIMER);
3113 	if (ret) {
3114 		dev_err(di->dev, "failed to set charger watchdog timeout\n");
3115 		goto out;
3116 	}
3117 
3118 	ret = ab8500_charger_led_en(di, false);
3119 	if (ret < 0) {
3120 		dev_err(di->dev, "failed to disable LED\n");
3121 		goto out;
3122 	}
3123 
3124 	ret = abx500_set_register_interruptible(di->dev,
3125 		AB8500_RTC,
3126 		AB8500_RTC_BACKUP_CHG_REG,
3127 		(di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
3128 	if (ret) {
3129 		dev_err(di->dev, "failed to setup backup battery charging\n");
3130 		goto out;
3131 	}
3132 
3133 	/* Enable backup battery charging */
3134 	ret = abx500_mask_and_set_register_interruptible(di->dev,
3135 		AB8500_RTC, AB8500_RTC_CTRL_REG,
3136 		RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3137 	if (ret < 0) {
3138 		dev_err(di->dev, "%s mask and set failed\n", __func__);
3139 		goto out;
3140 	}
3141 
3142 out:
3143 	return ret;
3144 }
3145 
3146 /*
3147  * ab8500 charger driver interrupts and their respective isr
3148  */
3149 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3150 	{"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3151 	{"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3152 	{"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3153 	{"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3154 	{"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3155 	{"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3156 	{"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3157 	{"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3158 	{"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3159 	{"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3160 	{"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3161 	{"VBUS_OVV", ab8500_charger_vbusovv_handler},
3162 	{"CH_WD_EXP", ab8500_charger_chwdexp_handler},
3163 	{"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
3164 };
3165 
3166 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3167 		unsigned long event, void *power)
3168 {
3169 	struct ab8500_charger *di =
3170 		container_of(nb, struct ab8500_charger, nb);
3171 	enum ab8500_usb_state bm_usb_state;
3172 	unsigned mA = *((unsigned *)power);
3173 
3174 	if (!di)
3175 		return NOTIFY_DONE;
3176 
3177 	if (event != USB_EVENT_VBUS) {
3178 		dev_dbg(di->dev, "not a standard host, returning\n");
3179 		return NOTIFY_DONE;
3180 	}
3181 
3182 	/* TODO: State is fabricate  here. See if charger really needs USB
3183 	 * state or if mA is enough
3184 	 */
3185 	if ((di->usb_state.usb_current == 2) && (mA > 2))
3186 		bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3187 	else if (mA == 0)
3188 		bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3189 	else if (mA == 2)
3190 		bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3191 	else if (mA >= 8) /* 8, 100, 500 */
3192 		bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3193 	else /* Should never occur */
3194 		bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3195 
3196 	dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3197 		__func__, bm_usb_state, mA);
3198 
3199 	spin_lock(&di->usb_state.usb_lock);
3200 	di->usb_state.state_tmp = bm_usb_state;
3201 	di->usb_state.usb_current_tmp = mA;
3202 	spin_unlock(&di->usb_state.usb_lock);
3203 
3204 	/*
3205 	 * wait for some time until you get updates from the usb stack
3206 	 * and negotiations are completed
3207 	 */
3208 	queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
3209 
3210 	return NOTIFY_OK;
3211 }
3212 
3213 static int __maybe_unused ab8500_charger_resume(struct device *dev)
3214 {
3215 	int ret;
3216 	struct ab8500_charger *di = dev_get_drvdata(dev);
3217 
3218 	/*
3219 	 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3220 	 * logic. That means we have to continuously kick the charger
3221 	 * watchdog even when no charger is connected. This is only
3222 	 * valid once the AC charger has been enabled. This is
3223 	 * a bug that is not handled by the algorithm and the
3224 	 * watchdog have to be kicked by the charger driver
3225 	 * when the AC charger is disabled
3226 	 */
3227 	if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3228 		ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3229 			AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3230 		if (ret)
3231 			dev_err(di->dev, "Failed to kick WD!\n");
3232 
3233 		/* If not already pending start a new timer */
3234 		queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3235 				   round_jiffies(WD_KICK_INTERVAL));
3236 	}
3237 
3238 	/* If we still have a HW failure, schedule a new check */
3239 	if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3240 		queue_delayed_work(di->charger_wq,
3241 			&di->check_hw_failure_work, 0);
3242 	}
3243 
3244 	if (di->flags.vbus_drop_end)
3245 		queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3246 
3247 	return 0;
3248 }
3249 
3250 static int __maybe_unused ab8500_charger_suspend(struct device *dev)
3251 {
3252 	struct ab8500_charger *di = dev_get_drvdata(dev);
3253 
3254 	/* Cancel any pending jobs */
3255 	cancel_delayed_work(&di->check_hw_failure_work);
3256 	cancel_delayed_work(&di->vbus_drop_end_work);
3257 
3258 	flush_delayed_work(&di->attach_work);
3259 	flush_delayed_work(&di->usb_charger_attached_work);
3260 	flush_delayed_work(&di->ac_charger_attached_work);
3261 	flush_delayed_work(&di->check_usbchgnotok_work);
3262 	flush_delayed_work(&di->check_vbat_work);
3263 	flush_delayed_work(&di->kick_wd_work);
3264 
3265 	flush_work(&di->usb_link_status_work);
3266 	flush_work(&di->ac_work);
3267 	flush_work(&di->detect_usb_type_work);
3268 
3269 	if (atomic_read(&di->current_stepping_sessions))
3270 		return -EAGAIN;
3271 
3272 	return 0;
3273 }
3274 
3275 static struct notifier_block charger_nb = {
3276 	.notifier_call = ab8500_external_charger_prepare,
3277 };
3278 
3279 static int ab8500_charger_remove(struct platform_device *pdev)
3280 {
3281 	struct ab8500_charger *di = platform_get_drvdata(pdev);
3282 	int i, irq, ret;
3283 
3284 	/* Disable AC charging */
3285 	ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3286 
3287 	/* Disable USB charging */
3288 	ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3289 
3290 	/* Disable interrupts */
3291 	for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3292 		irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3293 		free_irq(irq, di);
3294 	}
3295 
3296 	/* Backup battery voltage and current disable */
3297 	ret = abx500_mask_and_set_register_interruptible(di->dev,
3298 		AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3299 	if (ret < 0)
3300 		dev_err(di->dev, "%s mask and set failed\n", __func__);
3301 
3302 	usb_unregister_notifier(di->usb_phy, &di->nb);
3303 	usb_put_phy(di->usb_phy);
3304 
3305 	/* Delete the work queue */
3306 	destroy_workqueue(di->charger_wq);
3307 
3308 	/* Unregister external charger enable notifier */
3309 	if (!di->ac_chg.enabled)
3310 		blocking_notifier_chain_unregister(
3311 			&charger_notifier_list, &charger_nb);
3312 
3313 	flush_scheduled_work();
3314 	if (di->usb_chg.enabled)
3315 		power_supply_unregister(di->usb_chg.psy);
3316 
3317 	if (di->ac_chg.enabled && !di->ac_chg.external)
3318 		power_supply_unregister(di->ac_chg.psy);
3319 
3320 	return 0;
3321 }
3322 
3323 static char *supply_interface[] = {
3324 	"ab8500_chargalg",
3325 	"ab8500_fg",
3326 	"ab8500_btemp",
3327 };
3328 
3329 static const struct power_supply_desc ab8500_ac_chg_desc = {
3330 	.name		= "ab8500_ac",
3331 	.type		= POWER_SUPPLY_TYPE_MAINS,
3332 	.properties	= ab8500_charger_ac_props,
3333 	.num_properties	= ARRAY_SIZE(ab8500_charger_ac_props),
3334 	.get_property	= ab8500_charger_ac_get_property,
3335 };
3336 
3337 static const struct power_supply_desc ab8500_usb_chg_desc = {
3338 	.name		= "ab8500_usb",
3339 	.type		= POWER_SUPPLY_TYPE_USB,
3340 	.properties	= ab8500_charger_usb_props,
3341 	.num_properties	= ARRAY_SIZE(ab8500_charger_usb_props),
3342 	.get_property	= ab8500_charger_usb_get_property,
3343 };
3344 
3345 static int ab8500_charger_probe(struct platform_device *pdev)
3346 {
3347 	struct device_node *np = pdev->dev.of_node;
3348 	struct power_supply_config ac_psy_cfg = {}, usb_psy_cfg = {};
3349 	struct ab8500_charger *di;
3350 	int irq, i, charger_status, ret = 0, ch_stat;
3351 	struct device *dev = &pdev->dev;
3352 
3353 	di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
3354 	if (!di)
3355 		return -ENOMEM;
3356 
3357 	di->bm = &ab8500_bm_data;
3358 
3359 	ret = ab8500_bm_of_probe(dev, np, di->bm);
3360 	if (ret) {
3361 		dev_err(dev, "failed to get battery information\n");
3362 		return ret;
3363 	}
3364 	di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3365 
3366 	/* get parent data */
3367 	di->dev = dev;
3368 	di->parent = dev_get_drvdata(pdev->dev.parent);
3369 
3370 	/* Get ADC channels */
3371 	di->adc_main_charger_v = devm_iio_channel_get(dev, "main_charger_v");
3372 	if (IS_ERR(di->adc_main_charger_v)) {
3373 		ret = dev_err_probe(dev, PTR_ERR(di->adc_main_charger_v),
3374 				    "failed to get ADC main charger voltage\n");
3375 		return ret;
3376 	}
3377 	di->adc_main_charger_c = devm_iio_channel_get(dev, "main_charger_c");
3378 	if (IS_ERR(di->adc_main_charger_c)) {
3379 		ret = dev_err_probe(dev, PTR_ERR(di->adc_main_charger_c),
3380 				    "failed to get ADC main charger current\n");
3381 		return ret;
3382 	}
3383 	di->adc_vbus_v = devm_iio_channel_get(dev, "vbus_v");
3384 	if (IS_ERR(di->adc_vbus_v)) {
3385 		ret = dev_err_probe(dev, PTR_ERR(di->adc_vbus_v),
3386 				    "failed to get ADC USB charger voltage\n");
3387 		return ret;
3388 	}
3389 	di->adc_usb_charger_c = devm_iio_channel_get(dev, "usb_charger_c");
3390 	if (IS_ERR(di->adc_usb_charger_c)) {
3391 		ret = dev_err_probe(dev, PTR_ERR(di->adc_usb_charger_c),
3392 				    "failed to get ADC USB charger current\n");
3393 		return ret;
3394 	}
3395 
3396 	/* initialize lock */
3397 	spin_lock_init(&di->usb_state.usb_lock);
3398 	mutex_init(&di->usb_ipt_crnt_lock);
3399 
3400 	di->autopower = false;
3401 	di->invalid_charger_detect_state = 0;
3402 
3403 	/* AC and USB supply config */
3404 	ac_psy_cfg.supplied_to = supply_interface;
3405 	ac_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3406 	ac_psy_cfg.drv_data = &di->ac_chg;
3407 	usb_psy_cfg.supplied_to = supply_interface;
3408 	usb_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3409 	usb_psy_cfg.drv_data = &di->usb_chg;
3410 
3411 	/* AC supply */
3412 	/* ux500_charger sub-class */
3413 	di->ac_chg.ops.enable = &ab8500_charger_ac_en;
3414 	di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
3415 	di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3416 	di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3417 	di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3418 		ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3419 	di->ac_chg.max_out_curr =
3420 		di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3421 	di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
3422 	di->ac_chg.enabled = di->bm->ac_enabled;
3423 	di->ac_chg.external = false;
3424 
3425 	/*notifier for external charger enabling*/
3426 	if (!di->ac_chg.enabled)
3427 		blocking_notifier_chain_register(
3428 			&charger_notifier_list, &charger_nb);
3429 
3430 	/* USB supply */
3431 	/* ux500_charger sub-class */
3432 	di->usb_chg.ops.enable = &ab8500_charger_usb_en;
3433 	di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
3434 	di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3435 	di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3436 	di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3437 		ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3438 	di->usb_chg.max_out_curr =
3439 		di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
3440 	di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
3441 	di->usb_chg.enabled = di->bm->usb_enabled;
3442 	di->usb_chg.external = false;
3443 	di->usb_state.usb_current = -1;
3444 
3445 	/* Create a work queue for the charger */
3446 	di->charger_wq = alloc_ordered_workqueue("ab8500_charger_wq",
3447 						 WQ_MEM_RECLAIM);
3448 	if (di->charger_wq == NULL) {
3449 		dev_err(dev, "failed to create work queue\n");
3450 		return -ENOMEM;
3451 	}
3452 
3453 	mutex_init(&di->charger_attached_mutex);
3454 
3455 	/* Init work for HW failure check */
3456 	INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
3457 		ab8500_charger_check_hw_failure_work);
3458 	INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
3459 		ab8500_charger_check_usbchargernotok_work);
3460 
3461 	INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3462 			  ab8500_charger_ac_attached_work);
3463 	INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3464 			  ab8500_charger_usb_attached_work);
3465 
3466 	/*
3467 	 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3468 	 * logic. That means we have to continuously kick the charger
3469 	 * watchdog even when no charger is connected. This is only
3470 	 * valid once the AC charger has been enabled. This is
3471 	 * a bug that is not handled by the algorithm and the
3472 	 * watchdog have to be kicked by the charger driver
3473 	 * when the AC charger is disabled
3474 	 */
3475 	INIT_DEFERRABLE_WORK(&di->kick_wd_work,
3476 		ab8500_charger_kick_watchdog_work);
3477 
3478 	INIT_DEFERRABLE_WORK(&di->check_vbat_work,
3479 		ab8500_charger_check_vbat_work);
3480 
3481 	INIT_DELAYED_WORK(&di->attach_work,
3482 		ab8500_charger_usb_link_attach_work);
3483 
3484 	INIT_DELAYED_WORK(&di->usb_state_changed_work,
3485 		ab8500_charger_usb_state_changed_work);
3486 
3487 	INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3488 		ab8500_charger_vbus_drop_end_work);
3489 
3490 	/* Init work for charger detection */
3491 	INIT_WORK(&di->usb_link_status_work,
3492 		ab8500_charger_usb_link_status_work);
3493 	INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3494 	INIT_WORK(&di->detect_usb_type_work,
3495 		ab8500_charger_detect_usb_type_work);
3496 
3497 	/* Init work for checking HW status */
3498 	INIT_WORK(&di->check_main_thermal_prot_work,
3499 		ab8500_charger_check_main_thermal_prot_work);
3500 	INIT_WORK(&di->check_usb_thermal_prot_work,
3501 		ab8500_charger_check_usb_thermal_prot_work);
3502 
3503 	/*
3504 	 * VDD ADC supply needs to be enabled from this driver when there
3505 	 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3506 	 * interrupts during charging
3507 	 */
3508 	di->regu = devm_regulator_get(dev, "vddadc");
3509 	if (IS_ERR(di->regu)) {
3510 		ret = PTR_ERR(di->regu);
3511 		dev_err(dev, "failed to get vddadc regulator\n");
3512 		goto free_charger_wq;
3513 	}
3514 
3515 
3516 	/* Initialize OVV, and other registers */
3517 	ret = ab8500_charger_init_hw_registers(di);
3518 	if (ret) {
3519 		dev_err(dev, "failed to initialize ABB registers\n");
3520 		goto free_charger_wq;
3521 	}
3522 
3523 	/* Register AC charger class */
3524 	if (di->ac_chg.enabled) {
3525 		di->ac_chg.psy = power_supply_register(dev,
3526 						       &ab8500_ac_chg_desc,
3527 						       &ac_psy_cfg);
3528 		if (IS_ERR(di->ac_chg.psy)) {
3529 			dev_err(dev, "failed to register AC charger\n");
3530 			ret = PTR_ERR(di->ac_chg.psy);
3531 			goto free_charger_wq;
3532 		}
3533 	}
3534 
3535 	/* Register USB charger class */
3536 	if (di->usb_chg.enabled) {
3537 		di->usb_chg.psy = power_supply_register(dev,
3538 							&ab8500_usb_chg_desc,
3539 							&usb_psy_cfg);
3540 		if (IS_ERR(di->usb_chg.psy)) {
3541 			dev_err(dev, "failed to register USB charger\n");
3542 			ret = PTR_ERR(di->usb_chg.psy);
3543 			goto free_ac;
3544 		}
3545 	}
3546 
3547 	di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
3548 	if (IS_ERR_OR_NULL(di->usb_phy)) {
3549 		dev_err(dev, "failed to get usb transceiver\n");
3550 		ret = -EINVAL;
3551 		goto free_usb;
3552 	}
3553 	di->nb.notifier_call = ab8500_charger_usb_notifier_call;
3554 	ret = usb_register_notifier(di->usb_phy, &di->nb);
3555 	if (ret) {
3556 		dev_err(dev, "failed to register usb notifier\n");
3557 		goto put_usb_phy;
3558 	}
3559 
3560 	/* Identify the connected charger types during startup */
3561 	charger_status = ab8500_charger_detect_chargers(di, true);
3562 	if (charger_status & AC_PW_CONN) {
3563 		di->ac.charger_connected = 1;
3564 		di->ac_conn = true;
3565 		ab8500_power_supply_changed(di, di->ac_chg.psy);
3566 		sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
3567 	}
3568 
3569 	if (charger_status & USB_PW_CONN) {
3570 		di->vbus_detected = true;
3571 		di->vbus_detected_start = true;
3572 		queue_work(di->charger_wq,
3573 			&di->detect_usb_type_work);
3574 	}
3575 
3576 	/* Register interrupts */
3577 	for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3578 		irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3579 		if (irq < 0) {
3580 			ret = irq;
3581 			goto free_irq;
3582 		}
3583 
3584 		ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3585 			IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
3586 			ab8500_charger_irq[i].name, di);
3587 
3588 		if (ret != 0) {
3589 			dev_err(dev, "failed to request %s IRQ %d: %d\n"
3590 				, ab8500_charger_irq[i].name, irq, ret);
3591 			goto free_irq;
3592 		}
3593 		dev_dbg(dev, "Requested %s IRQ %d: %d\n",
3594 			ab8500_charger_irq[i].name, irq, ret);
3595 	}
3596 
3597 	platform_set_drvdata(pdev, di);
3598 
3599 	mutex_lock(&di->charger_attached_mutex);
3600 
3601 	ch_stat = ab8500_charger_detect_chargers(di, false);
3602 
3603 	if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3604 		if (is_ab8500(di->parent))
3605 			queue_delayed_work(di->charger_wq,
3606 					   &di->ac_charger_attached_work,
3607 					   HZ);
3608 	}
3609 	if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3610 		if (is_ab8500(di->parent))
3611 			queue_delayed_work(di->charger_wq,
3612 					   &di->usb_charger_attached_work,
3613 					   HZ);
3614 	}
3615 
3616 	mutex_unlock(&di->charger_attached_mutex);
3617 
3618 	return ret;
3619 
3620 free_irq:
3621 	usb_unregister_notifier(di->usb_phy, &di->nb);
3622 
3623 	/* We also have to free all successfully registered irqs */
3624 	for (i = i - 1; i >= 0; i--) {
3625 		irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3626 		free_irq(irq, di);
3627 	}
3628 put_usb_phy:
3629 	usb_put_phy(di->usb_phy);
3630 free_usb:
3631 	if (di->usb_chg.enabled)
3632 		power_supply_unregister(di->usb_chg.psy);
3633 free_ac:
3634 	if (di->ac_chg.enabled)
3635 		power_supply_unregister(di->ac_chg.psy);
3636 free_charger_wq:
3637 	destroy_workqueue(di->charger_wq);
3638 	return ret;
3639 }
3640 
3641 static SIMPLE_DEV_PM_OPS(ab8500_charger_pm_ops, ab8500_charger_suspend, ab8500_charger_resume);
3642 
3643 static const struct of_device_id ab8500_charger_match[] = {
3644 	{ .compatible = "stericsson,ab8500-charger", },
3645 	{ },
3646 };
3647 
3648 static struct platform_driver ab8500_charger_driver = {
3649 	.probe = ab8500_charger_probe,
3650 	.remove = ab8500_charger_remove,
3651 	.driver = {
3652 		.name = "ab8500-charger",
3653 		.of_match_table = ab8500_charger_match,
3654 		.pm = &ab8500_charger_pm_ops,
3655 	},
3656 };
3657 
3658 static int __init ab8500_charger_init(void)
3659 {
3660 	return platform_driver_register(&ab8500_charger_driver);
3661 }
3662 
3663 static void __exit ab8500_charger_exit(void)
3664 {
3665 	platform_driver_unregister(&ab8500_charger_driver);
3666 }
3667 
3668 subsys_initcall_sync(ab8500_charger_init);
3669 module_exit(ab8500_charger_exit);
3670 
3671 MODULE_LICENSE("GPL v2");
3672 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3673 MODULE_ALIAS("platform:ab8500-charger");
3674 MODULE_DESCRIPTION("AB8500 charger management driver");
3675