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