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