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