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