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