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