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