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