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