xref: /linux/drivers/power/supply/charger-manager.c (revision 1260ed77798502de9c98020040d2995008de10cc)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23bb3dbbdSDonggeun Kim /*
33bb3dbbdSDonggeun Kim  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
43bb3dbbdSDonggeun Kim  * MyungJoo Ham <myungjoo.ham@samsung.com>
53bb3dbbdSDonggeun Kim  *
63bb3dbbdSDonggeun Kim  * This driver enables to monitor battery health and control charger
73bb3dbbdSDonggeun Kim  * during suspend-to-mem.
802276af2SKrzysztof Kozlowski  * Charger manager depends on other devices. Register this later than
93bb3dbbdSDonggeun Kim  * the depending devices.
103bb3dbbdSDonggeun Kim  *
113bb3dbbdSDonggeun Kim **/
123bb3dbbdSDonggeun Kim 
13e5409cbdSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14e5409cbdSJoe Perches 
153bb3dbbdSDonggeun Kim #include <linux/io.h>
163bb3dbbdSDonggeun Kim #include <linux/module.h>
173bb3dbbdSDonggeun Kim #include <linux/irq.h>
183bb3dbbdSDonggeun Kim #include <linux/interrupt.h>
193bb3dbbdSDonggeun Kim #include <linux/rtc.h>
203bb3dbbdSDonggeun Kim #include <linux/slab.h>
213bb3dbbdSDonggeun Kim #include <linux/workqueue.h>
223bb3dbbdSDonggeun Kim #include <linux/platform_device.h>
233bb3dbbdSDonggeun Kim #include <linux/power/charger-manager.h>
243bb3dbbdSDonggeun Kim #include <linux/regulator/consumer.h>
25*2d678e3eSKrzysztof Kozlowski #include <linux/string_choices.h>
263950c786SChanwoo Choi #include <linux/sysfs.h>
27856ee611SJonghwa Lee #include <linux/of.h>
285c49a625SJonghwa Lee #include <linux/thermal.h>
295c49a625SJonghwa Lee 
30c1f73028SJonathan Bakker static struct {
31c1f73028SJonathan Bakker 	const char *name;
32c1f73028SJonathan Bakker 	u64 extcon_type;
33c1f73028SJonathan Bakker } extcon_mapping[] = {
34c1f73028SJonathan Bakker 	/* Current textual representations */
35c1f73028SJonathan Bakker 	{ "USB", EXTCON_USB },
36c1f73028SJonathan Bakker 	{ "USB-HOST", EXTCON_USB_HOST },
37c1f73028SJonathan Bakker 	{ "SDP", EXTCON_CHG_USB_SDP },
38c1f73028SJonathan Bakker 	{ "DCP", EXTCON_CHG_USB_DCP },
39c1f73028SJonathan Bakker 	{ "CDP", EXTCON_CHG_USB_CDP },
40c1f73028SJonathan Bakker 	{ "ACA", EXTCON_CHG_USB_ACA },
41c1f73028SJonathan Bakker 	{ "FAST-CHARGER", EXTCON_CHG_USB_FAST },
42c1f73028SJonathan Bakker 	{ "SLOW-CHARGER", EXTCON_CHG_USB_SLOW },
43c1f73028SJonathan Bakker 	{ "WPT", EXTCON_CHG_WPT },
44c1f73028SJonathan Bakker 	{ "PD", EXTCON_CHG_USB_PD },
45c1f73028SJonathan Bakker 	{ "DOCK", EXTCON_DOCK },
46c1f73028SJonathan Bakker 	{ "JIG", EXTCON_JIG },
47c1f73028SJonathan Bakker 	{ "MECHANICAL", EXTCON_MECHANICAL },
48c1f73028SJonathan Bakker 	/* Deprecated textual representations */
49c1f73028SJonathan Bakker 	{ "TA", EXTCON_CHG_USB_SDP },
50c1f73028SJonathan Bakker 	{ "CHARGE-DOWNSTREAM", EXTCON_CHG_USB_CDP },
51c1f73028SJonathan Bakker };
52c1f73028SJonathan Bakker 
535c49a625SJonghwa Lee /*
5402276af2SKrzysztof Kozlowski  * Default temperature threshold for charging.
555c49a625SJonghwa Lee  * Every temperature units are in tenth of centigrade.
565c49a625SJonghwa Lee  */
575c49a625SJonghwa Lee #define CM_DEFAULT_RECHARGE_TEMP_DIFF	50
585c49a625SJonghwa Lee #define CM_DEFAULT_CHARGE_TEMP_MAX	500
593bb3dbbdSDonggeun Kim 
603bb3dbbdSDonggeun Kim /*
613bb3dbbdSDonggeun Kim  * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
623bb3dbbdSDonggeun Kim  * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
633bb3dbbdSDonggeun Kim  * without any delays.
643bb3dbbdSDonggeun Kim  */
653bb3dbbdSDonggeun Kim #define	CM_JIFFIES_SMALL	(2)
663bb3dbbdSDonggeun Kim 
673bb3dbbdSDonggeun Kim /* If y is valid (> 0) and smaller than x, do x = y */
683bb3dbbdSDonggeun Kim #define CM_MIN_VALID(x, y)	x = (((y > 0) && ((x) > (y))) ? (y) : (x))
693bb3dbbdSDonggeun Kim 
703bb3dbbdSDonggeun Kim /*
713bb3dbbdSDonggeun Kim  * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
723bb3dbbdSDonggeun Kim  * rtc alarm. It should be 2 or larger
733bb3dbbdSDonggeun Kim  */
743bb3dbbdSDonggeun Kim #define CM_RTC_SMALL		(2)
753bb3dbbdSDonggeun Kim 
763bb3dbbdSDonggeun Kim static LIST_HEAD(cm_list);
773bb3dbbdSDonggeun Kim static DEFINE_MUTEX(cm_list_mtx);
783bb3dbbdSDonggeun Kim 
793bb3dbbdSDonggeun Kim /* About in-suspend (suspend-again) monitoring */
80c1155c64SJonghwa Lee static struct alarm *cm_timer;
81c1155c64SJonghwa Lee 
823bb3dbbdSDonggeun Kim static bool cm_suspended;
83c1155c64SJonghwa Lee static bool cm_timer_set;
843bb3dbbdSDonggeun Kim static unsigned long cm_suspend_duration_ms;
853bb3dbbdSDonggeun Kim 
86d829dc75SChanwoo Choi /* About normal (not suspended) monitoring */
87d829dc75SChanwoo Choi static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
88d829dc75SChanwoo Choi static unsigned long next_polling; /* Next appointed polling time */
89d829dc75SChanwoo Choi static struct workqueue_struct *cm_wq; /* init at driver add */
90d829dc75SChanwoo Choi static struct delayed_work cm_monitor_work; /* init at driver add */
91d829dc75SChanwoo Choi 
923bb3dbbdSDonggeun Kim /**
933bb3dbbdSDonggeun Kim  * is_batt_present - See if the battery presents in place.
943bb3dbbdSDonggeun Kim  * @cm: the Charger Manager representing the battery.
953bb3dbbdSDonggeun Kim  */
is_batt_present(struct charger_manager * cm)963bb3dbbdSDonggeun Kim static bool is_batt_present(struct charger_manager *cm)
973bb3dbbdSDonggeun Kim {
983bb3dbbdSDonggeun Kim 	union power_supply_propval val;
99bdbe8144SKrzysztof Kozlowski 	struct power_supply *psy;
1003bb3dbbdSDonggeun Kim 	bool present = false;
1013bb3dbbdSDonggeun Kim 	int i, ret;
1023bb3dbbdSDonggeun Kim 
1033bb3dbbdSDonggeun Kim 	switch (cm->desc->battery_present) {
104d829dc75SChanwoo Choi 	case CM_BATTERY_PRESENT:
105d829dc75SChanwoo Choi 		present = true;
106d829dc75SChanwoo Choi 		break;
107d829dc75SChanwoo Choi 	case CM_NO_BATTERY:
108d829dc75SChanwoo Choi 		break;
1093bb3dbbdSDonggeun Kim 	case CM_FUEL_GAUGE:
110bdbe8144SKrzysztof Kozlowski 		psy = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
111bdbe8144SKrzysztof Kozlowski 		if (!psy)
112bdbe8144SKrzysztof Kozlowski 			break;
113bdbe8144SKrzysztof Kozlowski 
114b70229bcSKrzysztof Kozlowski 		ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_PRESENT,
115b70229bcSKrzysztof Kozlowski 				&val);
1163bb3dbbdSDonggeun Kim 		if (ret == 0 && val.intval)
1173bb3dbbdSDonggeun Kim 			present = true;
118b43eb35aSKrzysztof Kozlowski 		power_supply_put(psy);
1193bb3dbbdSDonggeun Kim 		break;
1203bb3dbbdSDonggeun Kim 	case CM_CHARGER_STAT:
121cdaf3e15SKrzysztof Kozlowski 		for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
122cdaf3e15SKrzysztof Kozlowski 			psy = power_supply_get_by_name(
123cdaf3e15SKrzysztof Kozlowski 					cm->desc->psy_charger_stat[i]);
124cdaf3e15SKrzysztof Kozlowski 			if (!psy) {
125cdaf3e15SKrzysztof Kozlowski 				dev_err(cm->dev, "Cannot find power supply \"%s\"\n",
126cdaf3e15SKrzysztof Kozlowski 					cm->desc->psy_charger_stat[i]);
127cdaf3e15SKrzysztof Kozlowski 				continue;
128cdaf3e15SKrzysztof Kozlowski 			}
129cdaf3e15SKrzysztof Kozlowski 
130b70229bcSKrzysztof Kozlowski 			ret = power_supply_get_property(psy,
131b70229bcSKrzysztof Kozlowski 				POWER_SUPPLY_PROP_PRESENT, &val);
132b43eb35aSKrzysztof Kozlowski 			power_supply_put(psy);
1333bb3dbbdSDonggeun Kim 			if (ret == 0 && val.intval) {
1343bb3dbbdSDonggeun Kim 				present = true;
1353bb3dbbdSDonggeun Kim 				break;
1363bb3dbbdSDonggeun Kim 			}
1373bb3dbbdSDonggeun Kim 		}
1383bb3dbbdSDonggeun Kim 		break;
1393bb3dbbdSDonggeun Kim 	}
1403bb3dbbdSDonggeun Kim 
1413bb3dbbdSDonggeun Kim 	return present;
1423bb3dbbdSDonggeun Kim }
1433bb3dbbdSDonggeun Kim 
1443bb3dbbdSDonggeun Kim /**
1453bb3dbbdSDonggeun Kim  * is_ext_pwr_online - See if an external power source is attached to charge
1463bb3dbbdSDonggeun Kim  * @cm: the Charger Manager representing the battery.
1473bb3dbbdSDonggeun Kim  *
1483bb3dbbdSDonggeun Kim  * Returns true if at least one of the chargers of the battery has an external
1493bb3dbbdSDonggeun Kim  * power source attached to charge the battery regardless of whether it is
1503bb3dbbdSDonggeun Kim  * actually charging or not.
1513bb3dbbdSDonggeun Kim  */
is_ext_pwr_online(struct charger_manager * cm)1523bb3dbbdSDonggeun Kim static bool is_ext_pwr_online(struct charger_manager *cm)
1533bb3dbbdSDonggeun Kim {
1543bb3dbbdSDonggeun Kim 	union power_supply_propval val;
155cdaf3e15SKrzysztof Kozlowski 	struct power_supply *psy;
1563bb3dbbdSDonggeun Kim 	bool online = false;
1573bb3dbbdSDonggeun Kim 	int i, ret;
1583bb3dbbdSDonggeun Kim 
1593bb3dbbdSDonggeun Kim 	/* If at least one of them has one, it's yes. */
160cdaf3e15SKrzysztof Kozlowski 	for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
161cdaf3e15SKrzysztof Kozlowski 		psy = power_supply_get_by_name(cm->desc->psy_charger_stat[i]);
162cdaf3e15SKrzysztof Kozlowski 		if (!psy) {
163cdaf3e15SKrzysztof Kozlowski 			dev_err(cm->dev, "Cannot find power supply \"%s\"\n",
164cdaf3e15SKrzysztof Kozlowski 					cm->desc->psy_charger_stat[i]);
165cdaf3e15SKrzysztof Kozlowski 			continue;
166cdaf3e15SKrzysztof Kozlowski 		}
167cdaf3e15SKrzysztof Kozlowski 
168b70229bcSKrzysztof Kozlowski 		ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
169b70229bcSKrzysztof Kozlowski 				&val);
170b43eb35aSKrzysztof Kozlowski 		power_supply_put(psy);
1713bb3dbbdSDonggeun Kim 		if (ret == 0 && val.intval) {
1723bb3dbbdSDonggeun Kim 			online = true;
1733bb3dbbdSDonggeun Kim 			break;
1743bb3dbbdSDonggeun Kim 		}
1753bb3dbbdSDonggeun Kim 	}
1763bb3dbbdSDonggeun Kim 
1773bb3dbbdSDonggeun Kim 	return online;
1783bb3dbbdSDonggeun Kim }
1793bb3dbbdSDonggeun Kim 
1803bb3dbbdSDonggeun Kim /**
181ad3d13eeSDonggeun Kim  * get_batt_uV - Get the voltage level of the battery
182ad3d13eeSDonggeun Kim  * @cm: the Charger Manager representing the battery.
183ad3d13eeSDonggeun Kim  * @uV: the voltage level returned.
184ad3d13eeSDonggeun Kim  *
185ad3d13eeSDonggeun Kim  * Returns 0 if there is no error.
186ad3d13eeSDonggeun Kim  * Returns a negative value on error.
187ad3d13eeSDonggeun Kim  */
get_batt_uV(struct charger_manager * cm,int * uV)188ad3d13eeSDonggeun Kim static int get_batt_uV(struct charger_manager *cm, int *uV)
189ad3d13eeSDonggeun Kim {
190ad3d13eeSDonggeun Kim 	union power_supply_propval val;
191bdbe8144SKrzysztof Kozlowski 	struct power_supply *fuel_gauge;
192ad3d13eeSDonggeun Kim 	int ret;
193ad3d13eeSDonggeun Kim 
194bdbe8144SKrzysztof Kozlowski 	fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
195bdbe8144SKrzysztof Kozlowski 	if (!fuel_gauge)
196ad3d13eeSDonggeun Kim 		return -ENODEV;
197ad3d13eeSDonggeun Kim 
198b70229bcSKrzysztof Kozlowski 	ret = power_supply_get_property(fuel_gauge,
199bb2a95c2SAxel Lin 				POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
200b43eb35aSKrzysztof Kozlowski 	power_supply_put(fuel_gauge);
201ad3d13eeSDonggeun Kim 	if (ret)
202ad3d13eeSDonggeun Kim 		return ret;
203ad3d13eeSDonggeun Kim 
204ad3d13eeSDonggeun Kim 	*uV = val.intval;
205ad3d13eeSDonggeun Kim 	return 0;
206ad3d13eeSDonggeun Kim }
207ad3d13eeSDonggeun Kim 
208ad3d13eeSDonggeun Kim /**
2093bb3dbbdSDonggeun Kim  * is_charging - Returns true if the battery is being charged.
2103bb3dbbdSDonggeun Kim  * @cm: the Charger Manager representing the battery.
2113bb3dbbdSDonggeun Kim  */
is_charging(struct charger_manager * cm)2123bb3dbbdSDonggeun Kim static bool is_charging(struct charger_manager *cm)
2133bb3dbbdSDonggeun Kim {
2143bb3dbbdSDonggeun Kim 	int i, ret;
2153bb3dbbdSDonggeun Kim 	bool charging = false;
216cdaf3e15SKrzysztof Kozlowski 	struct power_supply *psy;
2173bb3dbbdSDonggeun Kim 	union power_supply_propval val;
2183bb3dbbdSDonggeun Kim 
2193bb3dbbdSDonggeun Kim 	/* If there is no battery, it cannot be charged */
2203bb3dbbdSDonggeun Kim 	if (!is_batt_present(cm))
2213bb3dbbdSDonggeun Kim 		return false;
2223bb3dbbdSDonggeun Kim 
2233bb3dbbdSDonggeun Kim 	/* If at least one of the charger is charging, return yes */
224cdaf3e15SKrzysztof Kozlowski 	for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
225570c2234SShen Lichuan 		/* 1. The charger should not be DISABLED */
2263bb3dbbdSDonggeun Kim 		if (cm->emergency_stop)
2273bb3dbbdSDonggeun Kim 			continue;
2283bb3dbbdSDonggeun Kim 		if (!cm->charger_enabled)
2293bb3dbbdSDonggeun Kim 			continue;
2303bb3dbbdSDonggeun Kim 
231cdaf3e15SKrzysztof Kozlowski 		psy = power_supply_get_by_name(cm->desc->psy_charger_stat[i]);
232cdaf3e15SKrzysztof Kozlowski 		if (!psy) {
233cdaf3e15SKrzysztof Kozlowski 			dev_err(cm->dev, "Cannot find power supply \"%s\"\n",
234cdaf3e15SKrzysztof Kozlowski 					cm->desc->psy_charger_stat[i]);
235cdaf3e15SKrzysztof Kozlowski 			continue;
236cdaf3e15SKrzysztof Kozlowski 		}
237cdaf3e15SKrzysztof Kozlowski 
2383bb3dbbdSDonggeun Kim 		/* 2. The charger should be online (ext-power) */
239b70229bcSKrzysztof Kozlowski 		ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
240b70229bcSKrzysztof Kozlowski 				&val);
2413bb3dbbdSDonggeun Kim 		if (ret) {
242e5409cbdSJoe Perches 			dev_warn(cm->dev, "Cannot read ONLINE value from %s\n",
2433bb3dbbdSDonggeun Kim 				 cm->desc->psy_charger_stat[i]);
244b43eb35aSKrzysztof Kozlowski 			power_supply_put(psy);
2453bb3dbbdSDonggeun Kim 			continue;
2463bb3dbbdSDonggeun Kim 		}
247b43eb35aSKrzysztof Kozlowski 		if (val.intval == 0) {
248b43eb35aSKrzysztof Kozlowski 			power_supply_put(psy);
2493bb3dbbdSDonggeun Kim 			continue;
250b43eb35aSKrzysztof Kozlowski 		}
2513bb3dbbdSDonggeun Kim 
2523bb3dbbdSDonggeun Kim 		/*
2533bb3dbbdSDonggeun Kim 		 * 3. The charger should not be FULL, DISCHARGING,
2543bb3dbbdSDonggeun Kim 		 * or NOT_CHARGING.
2553bb3dbbdSDonggeun Kim 		 */
256b70229bcSKrzysztof Kozlowski 		ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS,
257b70229bcSKrzysztof Kozlowski 				&val);
258b43eb35aSKrzysztof Kozlowski 		power_supply_put(psy);
2593bb3dbbdSDonggeun Kim 		if (ret) {
260e5409cbdSJoe Perches 			dev_warn(cm->dev, "Cannot read STATUS value from %s\n",
2613bb3dbbdSDonggeun Kim 				 cm->desc->psy_charger_stat[i]);
2623bb3dbbdSDonggeun Kim 			continue;
2633bb3dbbdSDonggeun Kim 		}
2643bb3dbbdSDonggeun Kim 		if (val.intval == POWER_SUPPLY_STATUS_FULL ||
2653bb3dbbdSDonggeun Kim 				val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
2663bb3dbbdSDonggeun Kim 				val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
2673bb3dbbdSDonggeun Kim 			continue;
2683bb3dbbdSDonggeun Kim 
2693bb3dbbdSDonggeun Kim 		/* Then, this is charging. */
2703bb3dbbdSDonggeun Kim 		charging = true;
2713bb3dbbdSDonggeun Kim 		break;
2723bb3dbbdSDonggeun Kim 	}
2733bb3dbbdSDonggeun Kim 
2743bb3dbbdSDonggeun Kim 	return charging;
2753bb3dbbdSDonggeun Kim }
2763bb3dbbdSDonggeun Kim 
2773bb3dbbdSDonggeun Kim /**
2782ed9e9b6SChanwoo Choi  * is_full_charged - Returns true if the battery is fully charged.
2792ed9e9b6SChanwoo Choi  * @cm: the Charger Manager representing the battery.
2802ed9e9b6SChanwoo Choi  */
is_full_charged(struct charger_manager * cm)2812ed9e9b6SChanwoo Choi static bool is_full_charged(struct charger_manager *cm)
2822ed9e9b6SChanwoo Choi {
2832ed9e9b6SChanwoo Choi 	struct charger_desc *desc = cm->desc;
2842ed9e9b6SChanwoo Choi 	union power_supply_propval val;
285bdbe8144SKrzysztof Kozlowski 	struct power_supply *fuel_gauge;
286b43eb35aSKrzysztof Kozlowski 	bool is_full = false;
2872ed9e9b6SChanwoo Choi 	int ret = 0;
2882ed9e9b6SChanwoo Choi 	int uV;
2892ed9e9b6SChanwoo Choi 
2902ed9e9b6SChanwoo Choi 	/* If there is no battery, it cannot be charged */
2910fa11dbcSChanwoo Choi 	if (!is_batt_present(cm))
2920fa11dbcSChanwoo Choi 		return false;
2932ed9e9b6SChanwoo Choi 
294bdbe8144SKrzysztof Kozlowski 	fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
295bdbe8144SKrzysztof Kozlowski 	if (!fuel_gauge)
296bdbe8144SKrzysztof Kozlowski 		return false;
297bdbe8144SKrzysztof Kozlowski 
298e132fc6bSJonghwa Lee 	/* Full, if it's over the fullbatt voltage */
299e132fc6bSJonghwa Lee 	if (desc->fullbatt_uV > 0) {
300e132fc6bSJonghwa Lee 		ret = get_batt_uV(cm, &uV);
301e132fc6bSJonghwa Lee 		if (!ret) {
302e132fc6bSJonghwa Lee 			/* Battery is already full, checks voltage drop. */
303e132fc6bSJonghwa Lee 			if (cm->battery_status == POWER_SUPPLY_STATUS_FULL
304e132fc6bSJonghwa Lee 					&& desc->fullbatt_vchkdrop_uV)
305e132fc6bSJonghwa Lee 				uV += desc->fullbatt_vchkdrop_uV;
306e132fc6bSJonghwa Lee 			if (uV >= desc->fullbatt_uV)
307e132fc6bSJonghwa Lee 				return true;
308e132fc6bSJonghwa Lee 		}
309e132fc6bSJonghwa Lee 	}
310e132fc6bSJonghwa Lee 
311bdbe8144SKrzysztof Kozlowski 	if (desc->fullbatt_full_capacity > 0) {
3120fa11dbcSChanwoo Choi 		val.intval = 0;
3130fa11dbcSChanwoo Choi 
3142ed9e9b6SChanwoo Choi 		/* Not full if capacity of fuel gauge isn't full */
315b70229bcSKrzysztof Kozlowski 		ret = power_supply_get_property(fuel_gauge,
3162ed9e9b6SChanwoo Choi 				POWER_SUPPLY_PROP_CHARGE_FULL, &val);
317b43eb35aSKrzysztof Kozlowski 		if (!ret && val.intval > desc->fullbatt_full_capacity) {
318b43eb35aSKrzysztof Kozlowski 			is_full = true;
319b43eb35aSKrzysztof Kozlowski 			goto out;
320b43eb35aSKrzysztof Kozlowski 		}
3212ed9e9b6SChanwoo Choi 	}
3222ed9e9b6SChanwoo Choi 
3232ed9e9b6SChanwoo Choi 	/* Full, if the capacity is more than fullbatt_soc */
324bdbe8144SKrzysztof Kozlowski 	if (desc->fullbatt_soc > 0) {
3252ed9e9b6SChanwoo Choi 		val.intval = 0;
3262ed9e9b6SChanwoo Choi 
327b70229bcSKrzysztof Kozlowski 		ret = power_supply_get_property(fuel_gauge,
3280fa11dbcSChanwoo Choi 				POWER_SUPPLY_PROP_CAPACITY, &val);
329b43eb35aSKrzysztof Kozlowski 		if (!ret && val.intval >= desc->fullbatt_soc) {
330b43eb35aSKrzysztof Kozlowski 			is_full = true;
331b43eb35aSKrzysztof Kozlowski 			goto out;
332b43eb35aSKrzysztof Kozlowski 		}
3330fa11dbcSChanwoo Choi 	}
3340fa11dbcSChanwoo Choi 
335b43eb35aSKrzysztof Kozlowski out:
336b43eb35aSKrzysztof Kozlowski 	power_supply_put(fuel_gauge);
337b43eb35aSKrzysztof Kozlowski 	return is_full;
3382ed9e9b6SChanwoo Choi }
3392ed9e9b6SChanwoo Choi 
3402ed9e9b6SChanwoo Choi /**
3413bb3dbbdSDonggeun Kim  * is_polling_required - Return true if need to continue polling for this CM.
3423bb3dbbdSDonggeun Kim  * @cm: the Charger Manager representing the battery.
3433bb3dbbdSDonggeun Kim  */
is_polling_required(struct charger_manager * cm)3443bb3dbbdSDonggeun Kim static bool is_polling_required(struct charger_manager *cm)
3453bb3dbbdSDonggeun Kim {
3463bb3dbbdSDonggeun Kim 	switch (cm->desc->polling_mode) {
3473bb3dbbdSDonggeun Kim 	case CM_POLL_DISABLE:
3483bb3dbbdSDonggeun Kim 		return false;
3493bb3dbbdSDonggeun Kim 	case CM_POLL_ALWAYS:
3503bb3dbbdSDonggeun Kim 		return true;
3513bb3dbbdSDonggeun Kim 	case CM_POLL_EXTERNAL_POWER_ONLY:
3523bb3dbbdSDonggeun Kim 		return is_ext_pwr_online(cm);
3533bb3dbbdSDonggeun Kim 	case CM_POLL_CHARGING_ONLY:
3543bb3dbbdSDonggeun Kim 		return is_charging(cm);
3553bb3dbbdSDonggeun Kim 	default:
3563bb3dbbdSDonggeun Kim 		dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
3573bb3dbbdSDonggeun Kim 			 cm->desc->polling_mode);
3583bb3dbbdSDonggeun Kim 	}
3593bb3dbbdSDonggeun Kim 
3603bb3dbbdSDonggeun Kim 	return false;
3613bb3dbbdSDonggeun Kim }
3623bb3dbbdSDonggeun Kim 
3633bb3dbbdSDonggeun Kim /**
3643bb3dbbdSDonggeun Kim  * try_charger_enable - Enable/Disable chargers altogether
3653bb3dbbdSDonggeun Kim  * @cm: the Charger Manager representing the battery.
3663bb3dbbdSDonggeun Kim  * @enable: true: enable / false: disable
3673bb3dbbdSDonggeun Kim  *
3683bb3dbbdSDonggeun Kim  * Note that Charger Manager keeps the charger enabled regardless whether
3693bb3dbbdSDonggeun Kim  * the charger is charging or not (because battery is full or no external
3703bb3dbbdSDonggeun Kim  * power source exists) except when CM needs to disable chargers forcibly
37102276af2SKrzysztof Kozlowski  * because of emergency causes; when the battery is overheated or too cold.
3723bb3dbbdSDonggeun Kim  */
try_charger_enable(struct charger_manager * cm,bool enable)3733bb3dbbdSDonggeun Kim static int try_charger_enable(struct charger_manager *cm, bool enable)
3743bb3dbbdSDonggeun Kim {
3753bb3dbbdSDonggeun Kim 	int err = 0, i;
3763bb3dbbdSDonggeun Kim 	struct charger_desc *desc = cm->desc;
3773bb3dbbdSDonggeun Kim 
3788c13b6f1SBaolin Wang 	/* Ignore if it's redundant command */
379bb2a95c2SAxel Lin 	if (enable == cm->charger_enabled)
3803bb3dbbdSDonggeun Kim 		return 0;
3813bb3dbbdSDonggeun Kim 
3823bb3dbbdSDonggeun Kim 	if (enable) {
3833bb3dbbdSDonggeun Kim 		if (cm->emergency_stop)
3843bb3dbbdSDonggeun Kim 			return -EAGAIN;
3858fcfe088SChanwoo Choi 
3868fcfe088SChanwoo Choi 		/*
3878fcfe088SChanwoo Choi 		 * Save start time of charging to limit
3888fcfe088SChanwoo Choi 		 * maximum possible charging time.
3898fcfe088SChanwoo Choi 		 */
3908fcfe088SChanwoo Choi 		cm->charging_start_time = ktime_to_ms(ktime_get());
3918fcfe088SChanwoo Choi 		cm->charging_end_time = 0;
3928fcfe088SChanwoo Choi 
393dbb61fc7SChanwoo Choi 		for (i = 0 ; i < desc->num_charger_regulators ; i++) {
3943950c786SChanwoo Choi 			if (desc->charger_regulators[i].externally_control)
3953950c786SChanwoo Choi 				continue;
3963950c786SChanwoo Choi 
397dbb61fc7SChanwoo Choi 			err = regulator_enable(desc->charger_regulators[i].consumer);
398dbb61fc7SChanwoo Choi 			if (err < 0) {
399e5409cbdSJoe Perches 				dev_warn(cm->dev, "Cannot enable %s regulator\n",
400dbb61fc7SChanwoo Choi 					 desc->charger_regulators[i].regulator_name);
401dbb61fc7SChanwoo Choi 			}
402dbb61fc7SChanwoo Choi 		}
4033bb3dbbdSDonggeun Kim 	} else {
4043bb3dbbdSDonggeun Kim 		/*
4058fcfe088SChanwoo Choi 		 * Save end time of charging to maintain fully charged state
4068fcfe088SChanwoo Choi 		 * of battery after full-batt.
4078fcfe088SChanwoo Choi 		 */
4088fcfe088SChanwoo Choi 		cm->charging_start_time = 0;
4098fcfe088SChanwoo Choi 		cm->charging_end_time = ktime_to_ms(ktime_get());
4108fcfe088SChanwoo Choi 
411dbb61fc7SChanwoo Choi 		for (i = 0 ; i < desc->num_charger_regulators ; i++) {
4123950c786SChanwoo Choi 			if (desc->charger_regulators[i].externally_control)
4133950c786SChanwoo Choi 				continue;
4143950c786SChanwoo Choi 
415dbb61fc7SChanwoo Choi 			err = regulator_disable(desc->charger_regulators[i].consumer);
416dbb61fc7SChanwoo Choi 			if (err < 0) {
417e5409cbdSJoe Perches 				dev_warn(cm->dev, "Cannot disable %s regulator\n",
418dbb61fc7SChanwoo Choi 					 desc->charger_regulators[i].regulator_name);
419dbb61fc7SChanwoo Choi 			}
420dbb61fc7SChanwoo Choi 		}
421dbb61fc7SChanwoo Choi 
4223bb3dbbdSDonggeun Kim 		/*
4233bb3dbbdSDonggeun Kim 		 * Abnormal battery state - Stop charging forcibly,
4243bb3dbbdSDonggeun Kim 		 * even if charger was enabled at the other places
4253bb3dbbdSDonggeun Kim 		 */
4263bb3dbbdSDonggeun Kim 		for (i = 0; i < desc->num_charger_regulators; i++) {
4273bb3dbbdSDonggeun Kim 			if (regulator_is_enabled(
4283bb3dbbdSDonggeun Kim 				    desc->charger_regulators[i].consumer)) {
4293bb3dbbdSDonggeun Kim 				regulator_force_disable(
4303bb3dbbdSDonggeun Kim 					desc->charger_regulators[i].consumer);
431e5409cbdSJoe Perches 				dev_warn(cm->dev, "Disable regulator(%s) forcibly\n",
432bee737bcSChanwoo Choi 					 desc->charger_regulators[i].regulator_name);
4333bb3dbbdSDonggeun Kim 			}
4343bb3dbbdSDonggeun Kim 		}
4353bb3dbbdSDonggeun Kim 	}
4363bb3dbbdSDonggeun Kim 
437e132fc6bSJonghwa Lee 	if (!err)
4383bb3dbbdSDonggeun Kim 		cm->charger_enabled = enable;
4393bb3dbbdSDonggeun Kim 
4403bb3dbbdSDonggeun Kim 	return err;
4413bb3dbbdSDonggeun Kim }
4423bb3dbbdSDonggeun Kim 
4433bb3dbbdSDonggeun Kim /**
4448fcfe088SChanwoo Choi  * check_charging_duration - Monitor charging/discharging duration
4458fcfe088SChanwoo Choi  * @cm: the Charger Manager representing the battery.
4468fcfe088SChanwoo Choi  *
4478fcfe088SChanwoo Choi  * If whole charging duration exceed 'charging_max_duration_ms',
4488fcfe088SChanwoo Choi  * cm stop charging to prevent overcharge/overheat. If discharging
4498fcfe088SChanwoo Choi  * duration exceed 'discharging _max_duration_ms', charger cable is
4508fcfe088SChanwoo Choi  * attached, after full-batt, cm start charging to maintain fully
4518fcfe088SChanwoo Choi  * charged state for battery.
4528fcfe088SChanwoo Choi  */
check_charging_duration(struct charger_manager * cm)4538fcfe088SChanwoo Choi static int check_charging_duration(struct charger_manager *cm)
4548fcfe088SChanwoo Choi {
4558fcfe088SChanwoo Choi 	struct charger_desc *desc = cm->desc;
4568fcfe088SChanwoo Choi 	u64 curr = ktime_to_ms(ktime_get());
4578fcfe088SChanwoo Choi 	u64 duration;
4588fcfe088SChanwoo Choi 	int ret = false;
4598fcfe088SChanwoo Choi 
4608fcfe088SChanwoo Choi 	if (!desc->charging_max_duration_ms &&
4618fcfe088SChanwoo Choi 			!desc->discharging_max_duration_ms)
4628fcfe088SChanwoo Choi 		return ret;
4638fcfe088SChanwoo Choi 
4648fcfe088SChanwoo Choi 	if (cm->charger_enabled) {
4658fcfe088SChanwoo Choi 		duration = curr - cm->charging_start_time;
4668fcfe088SChanwoo Choi 
4678fcfe088SChanwoo Choi 		if (duration > desc->charging_max_duration_ms) {
468856ee611SJonghwa Lee 			dev_info(cm->dev, "Charging duration exceed %ums\n",
4698fcfe088SChanwoo Choi 				 desc->charging_max_duration_ms);
4708fcfe088SChanwoo Choi 			ret = true;
4718fcfe088SChanwoo Choi 		}
472e132fc6bSJonghwa Lee 	} else if (cm->battery_status == POWER_SUPPLY_STATUS_NOT_CHARGING) {
4738fcfe088SChanwoo Choi 		duration = curr - cm->charging_end_time;
4748fcfe088SChanwoo Choi 
47510a4357fSColin Ian King 		if (duration > desc->discharging_max_duration_ms) {
476856ee611SJonghwa Lee 			dev_info(cm->dev, "Discharging duration exceed %ums\n",
4778fcfe088SChanwoo Choi 				 desc->discharging_max_duration_ms);
4788fcfe088SChanwoo Choi 			ret = true;
4798fcfe088SChanwoo Choi 		}
4808fcfe088SChanwoo Choi 	}
4818fcfe088SChanwoo Choi 
4828fcfe088SChanwoo Choi 	return ret;
4838fcfe088SChanwoo Choi }
4848fcfe088SChanwoo Choi 
cm_get_battery_temperature_by_psy(struct charger_manager * cm,int * temp)485bdbe8144SKrzysztof Kozlowski static int cm_get_battery_temperature_by_psy(struct charger_manager *cm,
486bdbe8144SKrzysztof Kozlowski 					int *temp)
487bdbe8144SKrzysztof Kozlowski {
488bdbe8144SKrzysztof Kozlowski 	struct power_supply *fuel_gauge;
489b43eb35aSKrzysztof Kozlowski 	int ret;
490bdbe8144SKrzysztof Kozlowski 
491bdbe8144SKrzysztof Kozlowski 	fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
492bdbe8144SKrzysztof Kozlowski 	if (!fuel_gauge)
493bdbe8144SKrzysztof Kozlowski 		return -ENODEV;
494bdbe8144SKrzysztof Kozlowski 
495b43eb35aSKrzysztof Kozlowski 	ret = power_supply_get_property(fuel_gauge,
496bdbe8144SKrzysztof Kozlowski 				POWER_SUPPLY_PROP_TEMP,
497bdbe8144SKrzysztof Kozlowski 				(union power_supply_propval *)temp);
498b43eb35aSKrzysztof Kozlowski 	power_supply_put(fuel_gauge);
499b43eb35aSKrzysztof Kozlowski 
500b43eb35aSKrzysztof Kozlowski 	return ret;
501bdbe8144SKrzysztof Kozlowski }
502bdbe8144SKrzysztof Kozlowski 
cm_get_battery_temperature(struct charger_manager * cm,int * temp)5035c49a625SJonghwa Lee static int cm_get_battery_temperature(struct charger_manager *cm,
5045c49a625SJonghwa Lee 					int *temp)
5055c49a625SJonghwa Lee {
5065c49a625SJonghwa Lee 	int ret;
5075c49a625SJonghwa Lee 
5085c49a625SJonghwa Lee 	if (!cm->desc->measure_battery_temp)
5095c49a625SJonghwa Lee 		return -ENODEV;
5105c49a625SJonghwa Lee 
5115c49a625SJonghwa Lee #ifdef CONFIG_THERMAL
512bdbe8144SKrzysztof Kozlowski 	if (cm->tzd_batt) {
51317e8351aSSascha Hauer 		ret = thermal_zone_get_temp(cm->tzd_batt, temp);
5145c49a625SJonghwa Lee 		if (!ret)
5155c49a625SJonghwa Lee 			/* Calibrate temperature unit */
5165c49a625SJonghwa Lee 			*temp /= 100;
517bdbe8144SKrzysztof Kozlowski 	} else
5185c49a625SJonghwa Lee #endif
519bdbe8144SKrzysztof Kozlowski 	{
520bdbe8144SKrzysztof Kozlowski 		/* if-else continued from CONFIG_THERMAL */
521bdbe8144SKrzysztof Kozlowski 		ret = cm_get_battery_temperature_by_psy(cm, temp);
522bdbe8144SKrzysztof Kozlowski 	}
523bdbe8144SKrzysztof Kozlowski 
5245c49a625SJonghwa Lee 	return ret;
5255c49a625SJonghwa Lee }
5265c49a625SJonghwa Lee 
cm_check_thermal_status(struct charger_manager * cm)5275c49a625SJonghwa Lee static int cm_check_thermal_status(struct charger_manager *cm)
5285c49a625SJonghwa Lee {
5295c49a625SJonghwa Lee 	struct charger_desc *desc = cm->desc;
5305c49a625SJonghwa Lee 	int temp, upper_limit, lower_limit;
5315c49a625SJonghwa Lee 	int ret = 0;
5325c49a625SJonghwa Lee 
5335c49a625SJonghwa Lee 	ret = cm_get_battery_temperature(cm, &temp);
5345c49a625SJonghwa Lee 	if (ret) {
5355c49a625SJonghwa Lee 		/* FIXME:
5365c49a625SJonghwa Lee 		 * No information of battery temperature might
53702276af2SKrzysztof Kozlowski 		 * occur hazardous result. We have to handle it
5385c49a625SJonghwa Lee 		 * depending on battery type.
5395c49a625SJonghwa Lee 		 */
5405c49a625SJonghwa Lee 		dev_err(cm->dev, "Failed to get battery temperature\n");
5415c49a625SJonghwa Lee 		return 0;
5425c49a625SJonghwa Lee 	}
5435c49a625SJonghwa Lee 
5445c49a625SJonghwa Lee 	upper_limit = desc->temp_max;
5455c49a625SJonghwa Lee 	lower_limit = desc->temp_min;
5465c49a625SJonghwa Lee 
5475c49a625SJonghwa Lee 	if (cm->emergency_stop) {
5485c49a625SJonghwa Lee 		upper_limit -= desc->temp_diff;
5495c49a625SJonghwa Lee 		lower_limit += desc->temp_diff;
5505c49a625SJonghwa Lee 	}
5515c49a625SJonghwa Lee 
5525c49a625SJonghwa Lee 	if (temp > upper_limit)
5539584051fSJonghwa Lee 		ret = CM_BATT_OVERHEAT;
5545c49a625SJonghwa Lee 	else if (temp < lower_limit)
5559584051fSJonghwa Lee 		ret = CM_BATT_COLD;
5569584051fSJonghwa Lee 	else
5579584051fSJonghwa Lee 		ret = CM_BATT_OK;
5585c49a625SJonghwa Lee 
559e132fc6bSJonghwa Lee 	cm->emergency_stop = ret;
560e132fc6bSJonghwa Lee 
5615c49a625SJonghwa Lee 	return ret;
5625c49a625SJonghwa Lee }
5635c49a625SJonghwa Lee 
5648fcfe088SChanwoo Choi /**
565e132fc6bSJonghwa Lee  * cm_get_target_status - Check current status and get next target status.
566e132fc6bSJonghwa Lee  * @cm: the Charger Manager representing the battery.
567e132fc6bSJonghwa Lee  */
cm_get_target_status(struct charger_manager * cm)568e132fc6bSJonghwa Lee static int cm_get_target_status(struct charger_manager *cm)
569e132fc6bSJonghwa Lee {
570e132fc6bSJonghwa Lee 	if (!is_ext_pwr_online(cm))
571e132fc6bSJonghwa Lee 		return POWER_SUPPLY_STATUS_DISCHARGING;
572e132fc6bSJonghwa Lee 
573e132fc6bSJonghwa Lee 	if (cm_check_thermal_status(cm)) {
5742a0aa0faSJunlin Yang 		/* Check if discharging duration exceeds limit. */
575e132fc6bSJonghwa Lee 		if (check_charging_duration(cm))
576e132fc6bSJonghwa Lee 			goto charging_ok;
577e132fc6bSJonghwa Lee 		return POWER_SUPPLY_STATUS_NOT_CHARGING;
578e132fc6bSJonghwa Lee 	}
579e132fc6bSJonghwa Lee 
580e132fc6bSJonghwa Lee 	switch (cm->battery_status) {
581e132fc6bSJonghwa Lee 	case POWER_SUPPLY_STATUS_CHARGING:
5822a0aa0faSJunlin Yang 		/* Check if charging duration exceeds limit. */
583e132fc6bSJonghwa Lee 		if (check_charging_duration(cm))
584e132fc6bSJonghwa Lee 			return POWER_SUPPLY_STATUS_FULL;
585e132fc6bSJonghwa Lee 		fallthrough;
586e132fc6bSJonghwa Lee 	case POWER_SUPPLY_STATUS_FULL:
587e132fc6bSJonghwa Lee 		if (is_full_charged(cm))
588e132fc6bSJonghwa Lee 			return POWER_SUPPLY_STATUS_FULL;
589e132fc6bSJonghwa Lee 		fallthrough;
590e132fc6bSJonghwa Lee 	default:
591e132fc6bSJonghwa Lee 		break;
592e132fc6bSJonghwa Lee 	}
593e132fc6bSJonghwa Lee 
594e132fc6bSJonghwa Lee charging_ok:
595e132fc6bSJonghwa Lee 	/* Charging is allowed. */
596e132fc6bSJonghwa Lee 	return POWER_SUPPLY_STATUS_CHARGING;
597e132fc6bSJonghwa Lee }
598e132fc6bSJonghwa Lee 
599e132fc6bSJonghwa Lee /**
6003bb3dbbdSDonggeun Kim  * _cm_monitor - Monitor the temperature and return true for exceptions.
6013bb3dbbdSDonggeun Kim  * @cm: the Charger Manager representing the battery.
6023bb3dbbdSDonggeun Kim  *
6033bb3dbbdSDonggeun Kim  * Returns true if there is an event to notify for the battery.
6043bb3dbbdSDonggeun Kim  * (True if the status of "emergency_stop" changes)
6053bb3dbbdSDonggeun Kim  */
_cm_monitor(struct charger_manager * cm)6063bb3dbbdSDonggeun Kim static bool _cm_monitor(struct charger_manager *cm)
6073bb3dbbdSDonggeun Kim {
608e132fc6bSJonghwa Lee 	int target;
6093bb3dbbdSDonggeun Kim 
610e132fc6bSJonghwa Lee 	target = cm_get_target_status(cm);
6113bb3dbbdSDonggeun Kim 
612e132fc6bSJonghwa Lee 	try_charger_enable(cm, (target == POWER_SUPPLY_STATUS_CHARGING));
6133bb3dbbdSDonggeun Kim 
614e132fc6bSJonghwa Lee 	if (cm->battery_status != target) {
615e132fc6bSJonghwa Lee 		cm->battery_status = target;
616e132fc6bSJonghwa Lee 		power_supply_changed(cm->charger_psy);
6172ed9e9b6SChanwoo Choi 	}
6183bb3dbbdSDonggeun Kim 
619e132fc6bSJonghwa Lee 	return (cm->battery_status == POWER_SUPPLY_STATUS_NOT_CHARGING);
6203bb3dbbdSDonggeun Kim }
6213bb3dbbdSDonggeun Kim 
6223bb3dbbdSDonggeun Kim /**
6233bb3dbbdSDonggeun Kim  * cm_monitor - Monitor every battery.
6243bb3dbbdSDonggeun Kim  *
6253bb3dbbdSDonggeun Kim  * Returns true if there is an event to notify from any of the batteries.
6263bb3dbbdSDonggeun Kim  * (True if the status of "emergency_stop" changes)
6273bb3dbbdSDonggeun Kim  */
cm_monitor(void)6283bb3dbbdSDonggeun Kim static bool cm_monitor(void)
6293bb3dbbdSDonggeun Kim {
6303bb3dbbdSDonggeun Kim 	bool stop = false;
6313bb3dbbdSDonggeun Kim 	struct charger_manager *cm;
6323bb3dbbdSDonggeun Kim 
6333bb3dbbdSDonggeun Kim 	mutex_lock(&cm_list_mtx);
6343bb3dbbdSDonggeun Kim 
635bb2a95c2SAxel Lin 	list_for_each_entry(cm, &cm_list, entry) {
636bb2a95c2SAxel Lin 		if (_cm_monitor(cm))
637bb2a95c2SAxel Lin 			stop = true;
638bb2a95c2SAxel Lin 	}
6393bb3dbbdSDonggeun Kim 
6403bb3dbbdSDonggeun Kim 	mutex_unlock(&cm_list_mtx);
6413bb3dbbdSDonggeun Kim 
6423bb3dbbdSDonggeun Kim 	return stop;
6433bb3dbbdSDonggeun Kim }
6443bb3dbbdSDonggeun Kim 
645d829dc75SChanwoo Choi /**
646d829dc75SChanwoo Choi  * _setup_polling - Setup the next instance of polling.
647d829dc75SChanwoo Choi  * @work: work_struct of the function _setup_polling.
648d829dc75SChanwoo Choi  */
_setup_polling(struct work_struct * work)649d829dc75SChanwoo Choi static void _setup_polling(struct work_struct *work)
650d829dc75SChanwoo Choi {
651d829dc75SChanwoo Choi 	unsigned long min = ULONG_MAX;
652d829dc75SChanwoo Choi 	struct charger_manager *cm;
653d829dc75SChanwoo Choi 	bool keep_polling = false;
654d829dc75SChanwoo Choi 	unsigned long _next_polling;
655d829dc75SChanwoo Choi 
656d829dc75SChanwoo Choi 	mutex_lock(&cm_list_mtx);
657d829dc75SChanwoo Choi 
658d829dc75SChanwoo Choi 	list_for_each_entry(cm, &cm_list, entry) {
659d829dc75SChanwoo Choi 		if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
660d829dc75SChanwoo Choi 			keep_polling = true;
661d829dc75SChanwoo Choi 
662d829dc75SChanwoo Choi 			if (min > cm->desc->polling_interval_ms)
663d829dc75SChanwoo Choi 				min = cm->desc->polling_interval_ms;
664d829dc75SChanwoo Choi 		}
665d829dc75SChanwoo Choi 	}
666d829dc75SChanwoo Choi 
667d829dc75SChanwoo Choi 	polling_jiffy = msecs_to_jiffies(min);
668d829dc75SChanwoo Choi 	if (polling_jiffy <= CM_JIFFIES_SMALL)
669d829dc75SChanwoo Choi 		polling_jiffy = CM_JIFFIES_SMALL + 1;
670d829dc75SChanwoo Choi 
671d829dc75SChanwoo Choi 	if (!keep_polling)
672d829dc75SChanwoo Choi 		polling_jiffy = ULONG_MAX;
673d829dc75SChanwoo Choi 	if (polling_jiffy == ULONG_MAX)
674d829dc75SChanwoo Choi 		goto out;
675d829dc75SChanwoo Choi 
676d829dc75SChanwoo Choi 	WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
677d829dc75SChanwoo Choi 			    ". try it later. %s\n", __func__);
678d829dc75SChanwoo Choi 
6792fbb520dSTejun Heo 	/*
6802fbb520dSTejun Heo 	 * Use mod_delayed_work() iff the next polling interval should
6812fbb520dSTejun Heo 	 * occur before the currently scheduled one.  If @cm_monitor_work
6822fbb520dSTejun Heo 	 * isn't active, the end result is the same, so no need to worry
6832fbb520dSTejun Heo 	 * about stale @next_polling.
6842fbb520dSTejun Heo 	 */
685d829dc75SChanwoo Choi 	_next_polling = jiffies + polling_jiffy;
686d829dc75SChanwoo Choi 
6872fbb520dSTejun Heo 	if (time_before(_next_polling, next_polling)) {
68841f63c53STejun Heo 		mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
6892fbb520dSTejun Heo 		next_polling = _next_polling;
6902fbb520dSTejun Heo 	} else {
6912fbb520dSTejun Heo 		if (queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy))
6922fbb520dSTejun Heo 			next_polling = _next_polling;
693d829dc75SChanwoo Choi 	}
694d829dc75SChanwoo Choi out:
695d829dc75SChanwoo Choi 	mutex_unlock(&cm_list_mtx);
696d829dc75SChanwoo Choi }
697d829dc75SChanwoo Choi static DECLARE_WORK(setup_polling, _setup_polling);
698d829dc75SChanwoo Choi 
699d829dc75SChanwoo Choi /**
700d829dc75SChanwoo Choi  * cm_monitor_poller - The Monitor / Poller.
701d829dc75SChanwoo Choi  * @work: work_struct of the function cm_monitor_poller
702d829dc75SChanwoo Choi  *
703d829dc75SChanwoo Choi  * During non-suspended state, cm_monitor_poller is used to poll and monitor
704d829dc75SChanwoo Choi  * the batteries.
705d829dc75SChanwoo Choi  */
cm_monitor_poller(struct work_struct * work)706d829dc75SChanwoo Choi static void cm_monitor_poller(struct work_struct *work)
707d829dc75SChanwoo Choi {
708d829dc75SChanwoo Choi 	cm_monitor();
709d829dc75SChanwoo Choi 	schedule_work(&setup_polling);
710d829dc75SChanwoo Choi }
711d829dc75SChanwoo Choi 
charger_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)712ad3d13eeSDonggeun Kim static int charger_get_property(struct power_supply *psy,
713ad3d13eeSDonggeun Kim 		enum power_supply_property psp,
714ad3d13eeSDonggeun Kim 		union power_supply_propval *val)
715ad3d13eeSDonggeun Kim {
716297d716fSKrzysztof Kozlowski 	struct charger_manager *cm = power_supply_get_drvdata(psy);
717ad3d13eeSDonggeun Kim 	struct charger_desc *desc = cm->desc;
718b43eb35aSKrzysztof Kozlowski 	struct power_supply *fuel_gauge = NULL;
719df58c04cSAnton Vorontsov 	int ret = 0;
720df58c04cSAnton Vorontsov 	int uV;
721ad3d13eeSDonggeun Kim 
722ad3d13eeSDonggeun Kim 	switch (psp) {
723ad3d13eeSDonggeun Kim 	case POWER_SUPPLY_PROP_STATUS:
724e132fc6bSJonghwa Lee 		val->intval = cm->battery_status;
725ad3d13eeSDonggeun Kim 		break;
726ad3d13eeSDonggeun Kim 	case POWER_SUPPLY_PROP_HEALTH:
72795b78d53SJunlin Yang 		if (cm->emergency_stop == CM_BATT_OVERHEAT)
728ad3d13eeSDonggeun Kim 			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
72995b78d53SJunlin Yang 		else if (cm->emergency_stop == CM_BATT_COLD)
730ad3d13eeSDonggeun Kim 			val->intval = POWER_SUPPLY_HEALTH_COLD;
731ad3d13eeSDonggeun Kim 		else
732ad3d13eeSDonggeun Kim 			val->intval = POWER_SUPPLY_HEALTH_GOOD;
733ad3d13eeSDonggeun Kim 		break;
734ad3d13eeSDonggeun Kim 	case POWER_SUPPLY_PROP_PRESENT:
735ad3d13eeSDonggeun Kim 		if (is_batt_present(cm))
736ad3d13eeSDonggeun Kim 			val->intval = 1;
737ad3d13eeSDonggeun Kim 		else
738ad3d13eeSDonggeun Kim 			val->intval = 0;
739ad3d13eeSDonggeun Kim 		break;
740ad3d13eeSDonggeun Kim 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
741df58c04cSAnton Vorontsov 		ret = get_batt_uV(cm, &val->intval);
742ad3d13eeSDonggeun Kim 		break;
743ad3d13eeSDonggeun Kim 	case POWER_SUPPLY_PROP_CURRENT_NOW:
744bdbe8144SKrzysztof Kozlowski 		fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
745bdbe8144SKrzysztof Kozlowski 		if (!fuel_gauge) {
746bdbe8144SKrzysztof Kozlowski 			ret = -ENODEV;
747bdbe8144SKrzysztof Kozlowski 			break;
748bdbe8144SKrzysztof Kozlowski 		}
749b70229bcSKrzysztof Kozlowski 		ret = power_supply_get_property(fuel_gauge,
750ad3d13eeSDonggeun Kim 				POWER_SUPPLY_PROP_CURRENT_NOW, val);
751ad3d13eeSDonggeun Kim 		break;
752ad3d13eeSDonggeun Kim 	case POWER_SUPPLY_PROP_TEMP:
7535c49a625SJonghwa Lee 		return cm_get_battery_temperature(cm, &val->intval);
754ad3d13eeSDonggeun Kim 	case POWER_SUPPLY_PROP_CAPACITY:
755ad3d13eeSDonggeun Kim 		if (!is_batt_present(cm)) {
756ad3d13eeSDonggeun Kim 			/* There is no battery. Assume 100% */
757ad3d13eeSDonggeun Kim 			val->intval = 100;
758ad3d13eeSDonggeun Kim 			break;
759ad3d13eeSDonggeun Kim 		}
760ad3d13eeSDonggeun Kim 
761b43eb35aSKrzysztof Kozlowski 		fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
762b43eb35aSKrzysztof Kozlowski 		if (!fuel_gauge) {
763b43eb35aSKrzysztof Kozlowski 			ret = -ENODEV;
764b43eb35aSKrzysztof Kozlowski 			break;
765b43eb35aSKrzysztof Kozlowski 		}
766b43eb35aSKrzysztof Kozlowski 
767b70229bcSKrzysztof Kozlowski 		ret = power_supply_get_property(fuel_gauge,
768ad3d13eeSDonggeun Kim 					POWER_SUPPLY_PROP_CAPACITY, val);
769ad3d13eeSDonggeun Kim 		if (ret)
770ad3d13eeSDonggeun Kim 			break;
771ad3d13eeSDonggeun Kim 
772ad3d13eeSDonggeun Kim 		if (val->intval > 100) {
773ad3d13eeSDonggeun Kim 			val->intval = 100;
774ad3d13eeSDonggeun Kim 			break;
775ad3d13eeSDonggeun Kim 		}
776ad3d13eeSDonggeun Kim 		if (val->intval < 0)
777ad3d13eeSDonggeun Kim 			val->intval = 0;
778ad3d13eeSDonggeun Kim 
779ad3d13eeSDonggeun Kim 		/* Do not adjust SOC when charging: voltage is overrated */
780ad3d13eeSDonggeun Kim 		if (is_charging(cm))
781ad3d13eeSDonggeun Kim 			break;
782ad3d13eeSDonggeun Kim 
783ad3d13eeSDonggeun Kim 		/*
784ad3d13eeSDonggeun Kim 		 * If the capacity value is inconsistent, calibrate it base on
785ad3d13eeSDonggeun Kim 		 * the battery voltage values and the thresholds given as desc
786ad3d13eeSDonggeun Kim 		 */
787ad3d13eeSDonggeun Kim 		ret = get_batt_uV(cm, &uV);
788ad3d13eeSDonggeun Kim 		if (ret) {
789ad3d13eeSDonggeun Kim 			/* Voltage information not available. No calibration */
790ad3d13eeSDonggeun Kim 			ret = 0;
791ad3d13eeSDonggeun Kim 			break;
792ad3d13eeSDonggeun Kim 		}
793ad3d13eeSDonggeun Kim 
794ad3d13eeSDonggeun Kim 		if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
795ad3d13eeSDonggeun Kim 		    !is_charging(cm)) {
796ad3d13eeSDonggeun Kim 			val->intval = 100;
797ad3d13eeSDonggeun Kim 			break;
798ad3d13eeSDonggeun Kim 		}
799ad3d13eeSDonggeun Kim 
800ad3d13eeSDonggeun Kim 		break;
801ad3d13eeSDonggeun Kim 	case POWER_SUPPLY_PROP_ONLINE:
802ad3d13eeSDonggeun Kim 		if (is_ext_pwr_online(cm))
803ad3d13eeSDonggeun Kim 			val->intval = 1;
804ad3d13eeSDonggeun Kim 		else
805ad3d13eeSDonggeun Kim 			val->intval = 0;
806ad3d13eeSDonggeun Kim 		break;
807ad3d13eeSDonggeun Kim 	case POWER_SUPPLY_PROP_CHARGE_FULL:
808ad3d13eeSDonggeun Kim 	case POWER_SUPPLY_PROP_CHARGE_NOW:
8090a9e0f94SJonghwa Lee 		fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
810bdbe8144SKrzysztof Kozlowski 		if (!fuel_gauge) {
811bdbe8144SKrzysztof Kozlowski 			ret = -ENODEV;
812bdbe8144SKrzysztof Kozlowski 			break;
813bdbe8144SKrzysztof Kozlowski 		}
8140a9e0f94SJonghwa Lee 		ret = power_supply_get_property(fuel_gauge, psp, val);
815ad3d13eeSDonggeun Kim 		break;
816ad3d13eeSDonggeun Kim 	default:
817ad3d13eeSDonggeun Kim 		return -EINVAL;
818ad3d13eeSDonggeun Kim 	}
819b43eb35aSKrzysztof Kozlowski 	if (fuel_gauge)
820b43eb35aSKrzysztof Kozlowski 		power_supply_put(fuel_gauge);
821ad3d13eeSDonggeun Kim 	return ret;
822ad3d13eeSDonggeun Kim }
823ad3d13eeSDonggeun Kim 
824ad3d13eeSDonggeun Kim #define NUM_CHARGER_PSY_OPTIONAL	(4)
825ad3d13eeSDonggeun Kim static enum power_supply_property default_charger_props[] = {
826ad3d13eeSDonggeun Kim 	/* Guaranteed to provide */
827ad3d13eeSDonggeun Kim 	POWER_SUPPLY_PROP_STATUS,
828ad3d13eeSDonggeun Kim 	POWER_SUPPLY_PROP_HEALTH,
829ad3d13eeSDonggeun Kim 	POWER_SUPPLY_PROP_PRESENT,
830ad3d13eeSDonggeun Kim 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
831ad3d13eeSDonggeun Kim 	POWER_SUPPLY_PROP_CAPACITY,
832ad3d13eeSDonggeun Kim 	POWER_SUPPLY_PROP_ONLINE,
833ad3d13eeSDonggeun Kim 	/*
834ad3d13eeSDonggeun Kim 	 * Optional properties are:
8350a9e0f94SJonghwa Lee 	 * POWER_SUPPLY_PROP_CHARGE_FULL,
836ad3d13eeSDonggeun Kim 	 * POWER_SUPPLY_PROP_CHARGE_NOW,
837ad3d13eeSDonggeun Kim 	 * POWER_SUPPLY_PROP_CURRENT_NOW,
838cdaeb151SJonathan Bakker 	 * POWER_SUPPLY_PROP_TEMP,
839ad3d13eeSDonggeun Kim 	 */
840ad3d13eeSDonggeun Kim };
841ad3d13eeSDonggeun Kim 
842297d716fSKrzysztof Kozlowski static const struct power_supply_desc psy_default = {
843ad3d13eeSDonggeun Kim 	.name = "battery",
844ad3d13eeSDonggeun Kim 	.type = POWER_SUPPLY_TYPE_BATTERY,
845ad3d13eeSDonggeun Kim 	.properties = default_charger_props,
846ad3d13eeSDonggeun Kim 	.num_properties = ARRAY_SIZE(default_charger_props),
847ad3d13eeSDonggeun Kim 	.get_property = charger_get_property,
848ba9c9182SKrzysztof Kozlowski 	.no_thermal = true,
849ad3d13eeSDonggeun Kim };
850ad3d13eeSDonggeun Kim 
8513bb3dbbdSDonggeun Kim /**
8523bb3dbbdSDonggeun Kim  * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
8533bb3dbbdSDonggeun Kim  *		    for suspend_again.
8543bb3dbbdSDonggeun Kim  *
8553bb3dbbdSDonggeun Kim  * Returns true if the alarm is set for Charger Manager to use.
8563bb3dbbdSDonggeun Kim  * Returns false if
8573bb3dbbdSDonggeun Kim  *	cm_setup_timer fails to set an alarm,
8583bb3dbbdSDonggeun Kim  *	cm_setup_timer does not need to set an alarm for Charger Manager,
8593bb3dbbdSDonggeun Kim  *	or an alarm previously configured is to be used.
8603bb3dbbdSDonggeun Kim  */
cm_setup_timer(void)8613bb3dbbdSDonggeun Kim static bool cm_setup_timer(void)
8623bb3dbbdSDonggeun Kim {
8633bb3dbbdSDonggeun Kim 	struct charger_manager *cm;
8643bb3dbbdSDonggeun Kim 	unsigned int wakeup_ms = UINT_MAX;
865c1155c64SJonghwa Lee 	int timer_req = 0;
866c1155c64SJonghwa Lee 
867c1155c64SJonghwa Lee 	if (time_after(next_polling, jiffies))
868c1155c64SJonghwa Lee 		CM_MIN_VALID(wakeup_ms,
869c1155c64SJonghwa Lee 			jiffies_to_msecs(next_polling - jiffies));
8703bb3dbbdSDonggeun Kim 
8713bb3dbbdSDonggeun Kim 	mutex_lock(&cm_list_mtx);
8723bb3dbbdSDonggeun Kim 	list_for_each_entry(cm, &cm_list, entry) {
8733bb3dbbdSDonggeun Kim 		/* Skip if polling is not required for this CM */
8743bb3dbbdSDonggeun Kim 		if (!is_polling_required(cm) && !cm->emergency_stop)
8753bb3dbbdSDonggeun Kim 			continue;
876c1155c64SJonghwa Lee 		timer_req++;
8773bb3dbbdSDonggeun Kim 		if (cm->desc->polling_interval_ms == 0)
8783bb3dbbdSDonggeun Kim 			continue;
8793bb3dbbdSDonggeun Kim 		CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
8803bb3dbbdSDonggeun Kim 	}
8813bb3dbbdSDonggeun Kim 	mutex_unlock(&cm_list_mtx);
8823bb3dbbdSDonggeun Kim 
883c1155c64SJonghwa Lee 	if (timer_req && cm_timer) {
884c1155c64SJonghwa Lee 		ktime_t now, add;
8853bb3dbbdSDonggeun Kim 
8863bb3dbbdSDonggeun Kim 		/*
8873bb3dbbdSDonggeun Kim 		 * Set alarm with the polling interval (wakeup_ms)
888c1155c64SJonghwa Lee 		 * The alarm time should be NOW + CM_RTC_SMALL or later.
8893bb3dbbdSDonggeun Kim 		 */
890c1155c64SJonghwa Lee 		if (wakeup_ms == UINT_MAX ||
891c1155c64SJonghwa Lee 			wakeup_ms < CM_RTC_SMALL * MSEC_PER_SEC)
892c1155c64SJonghwa Lee 			wakeup_ms = 2 * CM_RTC_SMALL * MSEC_PER_SEC;
8933bb3dbbdSDonggeun Kim 
894c1155c64SJonghwa Lee 		pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms);
8953bb3dbbdSDonggeun Kim 
896c1155c64SJonghwa Lee 		now = ktime_get_boottime();
897c1155c64SJonghwa Lee 		add = ktime_set(wakeup_ms / MSEC_PER_SEC,
898c1155c64SJonghwa Lee 				(wakeup_ms % MSEC_PER_SEC) * NSEC_PER_MSEC);
899c1155c64SJonghwa Lee 		alarm_start(cm_timer, ktime_add(now, add));
9003bb3dbbdSDonggeun Kim 
901c1155c64SJonghwa Lee 		cm_suspend_duration_ms = wakeup_ms;
902c1155c64SJonghwa Lee 
903c1155c64SJonghwa Lee 		return true;
9043bb3dbbdSDonggeun Kim 	}
9053bb3dbbdSDonggeun Kim 	return false;
9063bb3dbbdSDonggeun Kim }
9073bb3dbbdSDonggeun Kim 
908bee737bcSChanwoo Choi /**
909bee737bcSChanwoo Choi  * charger_extcon_work - enable/diable charger according to the state
910bee737bcSChanwoo Choi  *			of charger cable
911bee737bcSChanwoo Choi  *
912bee737bcSChanwoo Choi  * @work: work_struct of the function charger_extcon_work.
913bee737bcSChanwoo Choi  */
charger_extcon_work(struct work_struct * work)914bee737bcSChanwoo Choi static void charger_extcon_work(struct work_struct *work)
915bee737bcSChanwoo Choi {
916bee737bcSChanwoo Choi 	struct charger_cable *cable =
917bee737bcSChanwoo Choi 			container_of(work, struct charger_cable, wq);
91845cd4fb2SChanwoo Choi 	int ret;
91945cd4fb2SChanwoo Choi 
92045cd4fb2SChanwoo Choi 	if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
92145cd4fb2SChanwoo Choi 		ret = regulator_set_current_limit(cable->charger->consumer,
92245cd4fb2SChanwoo Choi 					cable->min_uA, cable->max_uA);
92345cd4fb2SChanwoo Choi 		if (ret < 0) {
92445cd4fb2SChanwoo Choi 			pr_err("Cannot set current limit of %s (%s)\n",
92545cd4fb2SChanwoo Choi 			       cable->charger->regulator_name, cable->name);
92645cd4fb2SChanwoo Choi 			return;
92745cd4fb2SChanwoo Choi 		}
92845cd4fb2SChanwoo Choi 
92945cd4fb2SChanwoo Choi 		pr_info("Set current limit of %s : %duA ~ %duA\n",
93045cd4fb2SChanwoo Choi 			cable->charger->regulator_name,
93145cd4fb2SChanwoo Choi 			cable->min_uA, cable->max_uA);
93245cd4fb2SChanwoo Choi 	}
933bee737bcSChanwoo Choi 
9349434e453SJonghwa Lee 	cancel_delayed_work(&cm_monitor_work);
9359434e453SJonghwa Lee 	queue_delayed_work(cm_wq, &cm_monitor_work, 0);
936bee737bcSChanwoo Choi }
937bee737bcSChanwoo Choi 
938bee737bcSChanwoo Choi /**
939bee737bcSChanwoo Choi  * charger_extcon_notifier - receive the state of charger cable
940bee737bcSChanwoo Choi  *			when registered cable is attached or detached.
941bee737bcSChanwoo Choi  *
942bee737bcSChanwoo Choi  * @self: the notifier block of the charger_extcon_notifier.
943bee737bcSChanwoo Choi  * @event: the cable state.
944bee737bcSChanwoo Choi  * @ptr: the data pointer of notifier block.
945bee737bcSChanwoo Choi  */
charger_extcon_notifier(struct notifier_block * self,unsigned long event,void * ptr)946bee737bcSChanwoo Choi static int charger_extcon_notifier(struct notifier_block *self,
947bee737bcSChanwoo Choi 			unsigned long event, void *ptr)
948bee737bcSChanwoo Choi {
949bee737bcSChanwoo Choi 	struct charger_cable *cable =
950bee737bcSChanwoo Choi 		container_of(self, struct charger_cable, nb);
951bee737bcSChanwoo Choi 
9522ed9e9b6SChanwoo Choi 	/*
9532ed9e9b6SChanwoo Choi 	 * The newly state of charger cable.
9542ed9e9b6SChanwoo Choi 	 * If cable is attached, cable->attached is true.
9552ed9e9b6SChanwoo Choi 	 */
956bee737bcSChanwoo Choi 	cable->attached = event;
9572ed9e9b6SChanwoo Choi 
9582ed9e9b6SChanwoo Choi 	/*
9592ed9e9b6SChanwoo Choi 	 * Setup work for controlling charger(regulator)
9602ed9e9b6SChanwoo Choi 	 * according to charger cable.
9612ed9e9b6SChanwoo Choi 	 */
962bee737bcSChanwoo Choi 	schedule_work(&cable->wq);
963bee737bcSChanwoo Choi 
964bee737bcSChanwoo Choi 	return NOTIFY_DONE;
965bee737bcSChanwoo Choi }
966bee737bcSChanwoo Choi 
967bee737bcSChanwoo Choi /**
968bee737bcSChanwoo Choi  * charger_extcon_init - register external connector to use it
969bee737bcSChanwoo Choi  *			as the charger cable
970bee737bcSChanwoo Choi  *
971bee737bcSChanwoo Choi  * @cm: the Charger Manager representing the battery.
972bee737bcSChanwoo Choi  * @cable: the Charger cable representing the external connector.
973bee737bcSChanwoo Choi  */
charger_extcon_init(struct charger_manager * cm,struct charger_cable * cable)974bee737bcSChanwoo Choi static int charger_extcon_init(struct charger_manager *cm,
975bee737bcSChanwoo Choi 		struct charger_cable *cable)
976bee737bcSChanwoo Choi {
977c1f73028SJonathan Bakker 	int ret, i;
978c1f73028SJonathan Bakker 	u64 extcon_type = EXTCON_NONE;
979bee737bcSChanwoo Choi 
980bee737bcSChanwoo Choi 	/*
981bee737bcSChanwoo Choi 	 * Charger manager use Extcon framework to identify
982bee737bcSChanwoo Choi 	 * the charger cable among various external connector
983bee737bcSChanwoo Choi 	 * cable (e.g., TA, USB, MHL, Dock).
984bee737bcSChanwoo Choi 	 */
985bee737bcSChanwoo Choi 	INIT_WORK(&cable->wq, charger_extcon_work);
986bee737bcSChanwoo Choi 	cable->nb.notifier_call = charger_extcon_notifier;
987c1f73028SJonathan Bakker 
988c1f73028SJonathan Bakker 	cable->extcon_dev = extcon_get_extcon_dev(cable->extcon_name);
98958e4a2d2SDan Carpenter 	if (IS_ERR(cable->extcon_dev)) {
990c1f73028SJonathan Bakker 		pr_err("Cannot find extcon_dev for %s (cable: %s)\n",
991e5409cbdSJoe Perches 			cable->extcon_name, cable->name);
992c1f73028SJonathan Bakker 		return PTR_ERR(cable->extcon_dev);
993bee737bcSChanwoo Choi 	}
994bee737bcSChanwoo Choi 
995c1f73028SJonathan Bakker 	for (i = 0; i < ARRAY_SIZE(extcon_mapping); i++) {
996c1f73028SJonathan Bakker 		if (!strcmp(cable->name, extcon_mapping[i].name)) {
997c1f73028SJonathan Bakker 			extcon_type = extcon_mapping[i].extcon_type;
998c1f73028SJonathan Bakker 			break;
999c1f73028SJonathan Bakker 		}
1000c1f73028SJonathan Bakker 	}
1001c1f73028SJonathan Bakker 	if (extcon_type == EXTCON_NONE) {
1002c1f73028SJonathan Bakker 		pr_err("Cannot find cable for type %s", cable->name);
1003c1f73028SJonathan Bakker 		return -EINVAL;
1004c1f73028SJonathan Bakker 	}
1005c1f73028SJonathan Bakker 
1006c1f73028SJonathan Bakker 	cable->extcon_type = extcon_type;
1007c1f73028SJonathan Bakker 
1008c1f73028SJonathan Bakker 	ret = devm_extcon_register_notifier(cm->dev, cable->extcon_dev,
1009c1f73028SJonathan Bakker 		cable->extcon_type, &cable->nb);
1010c1f73028SJonathan Bakker 	if (ret < 0) {
1011c1f73028SJonathan Bakker 		pr_err("Cannot register extcon_dev for %s (cable: %s)\n",
1012c1f73028SJonathan Bakker 			cable->extcon_name, cable->name);
1013bee737bcSChanwoo Choi 		return ret;
1014bee737bcSChanwoo Choi 	}
1015bee737bcSChanwoo Choi 
1016c1f73028SJonathan Bakker 	return 0;
1017c1f73028SJonathan Bakker }
1018c1f73028SJonathan Bakker 
101941468a11SChanwoo Choi /**
102002276af2SKrzysztof Kozlowski  * charger_manager_register_extcon - Register extcon device to receive state
102141468a11SChanwoo Choi  *				     of charger cable.
102241468a11SChanwoo Choi  * @cm: the Charger Manager representing the battery.
102341468a11SChanwoo Choi  *
102441468a11SChanwoo Choi  * This function support EXTCON(External Connector) subsystem to detect the
102541468a11SChanwoo Choi  * state of charger cables for enabling or disabling charger(regulator) and
102641468a11SChanwoo Choi  * select the charger cable for charging among a number of external cable
102741468a11SChanwoo Choi  * according to policy of H/W board.
102841468a11SChanwoo Choi  */
charger_manager_register_extcon(struct charger_manager * cm)102941468a11SChanwoo Choi static int charger_manager_register_extcon(struct charger_manager *cm)
103041468a11SChanwoo Choi {
103141468a11SChanwoo Choi 	struct charger_desc *desc = cm->desc;
103241468a11SChanwoo Choi 	struct charger_regulator *charger;
1033c1f73028SJonathan Bakker 	unsigned long event;
1034dc6ea7d4SAndi Shyti 	int ret;
103541468a11SChanwoo Choi 	int i;
103641468a11SChanwoo Choi 	int j;
103741468a11SChanwoo Choi 
103841468a11SChanwoo Choi 	for (i = 0; i < desc->num_charger_regulators; i++) {
103941468a11SChanwoo Choi 		charger = &desc->charger_regulators[i];
104041468a11SChanwoo Choi 
104141468a11SChanwoo Choi 		charger->consumer = regulator_get(cm->dev,
104241468a11SChanwoo Choi 					charger->regulator_name);
10435a6c2208SJonghwa Lee 		if (IS_ERR(charger->consumer)) {
1044e5409cbdSJoe Perches 			dev_err(cm->dev, "Cannot find charger(%s)\n",
104541468a11SChanwoo Choi 				charger->regulator_name);
10465a6c2208SJonghwa Lee 			return PTR_ERR(charger->consumer);
104741468a11SChanwoo Choi 		}
104841468a11SChanwoo Choi 		charger->cm = cm;
104941468a11SChanwoo Choi 
105041468a11SChanwoo Choi 		for (j = 0; j < charger->num_cables; j++) {
105141468a11SChanwoo Choi 			struct charger_cable *cable = &charger->cables[j];
105241468a11SChanwoo Choi 
105341468a11SChanwoo Choi 			ret = charger_extcon_init(cm, cable);
105441468a11SChanwoo Choi 			if (ret < 0) {
1055e5409cbdSJoe Perches 				dev_err(cm->dev, "Cannot initialize charger(%s)\n",
105641468a11SChanwoo Choi 					charger->regulator_name);
1057dc6ea7d4SAndi Shyti 				return ret;
105841468a11SChanwoo Choi 			}
105941468a11SChanwoo Choi 			cable->charger = charger;
106041468a11SChanwoo Choi 			cable->cm = cm;
1061c1f73028SJonathan Bakker 
1062c1f73028SJonathan Bakker 			event = extcon_get_state(cable->extcon_dev,
1063c1f73028SJonathan Bakker 				cable->extcon_type);
1064c1f73028SJonathan Bakker 			charger_extcon_notifier(&cable->nb,
1065c1f73028SJonathan Bakker 				event, NULL);
106641468a11SChanwoo Choi 		}
106741468a11SChanwoo Choi 	}
106841468a11SChanwoo Choi 
1069dc6ea7d4SAndi Shyti 	return 0;
107041468a11SChanwoo Choi }
107141468a11SChanwoo Choi 
10723950c786SChanwoo Choi /* help function of sysfs node to control charger(regulator) */
charger_name_show(struct device * dev,struct device_attribute * attr,char * buf)10733950c786SChanwoo Choi static ssize_t charger_name_show(struct device *dev,
10743950c786SChanwoo Choi 				struct device_attribute *attr, char *buf)
10753950c786SChanwoo Choi {
10763950c786SChanwoo Choi 	struct charger_regulator *charger
10773950c786SChanwoo Choi 		= container_of(attr, struct charger_regulator, attr_name);
10783950c786SChanwoo Choi 
1079a441f3b9Sye xingchen 	return sysfs_emit(buf, "%s\n", charger->regulator_name);
10803950c786SChanwoo Choi }
10813950c786SChanwoo Choi 
charger_state_show(struct device * dev,struct device_attribute * attr,char * buf)10823950c786SChanwoo Choi static ssize_t charger_state_show(struct device *dev,
10833950c786SChanwoo Choi 				struct device_attribute *attr, char *buf)
10843950c786SChanwoo Choi {
10853950c786SChanwoo Choi 	struct charger_regulator *charger
10863950c786SChanwoo Choi 		= container_of(attr, struct charger_regulator, attr_state);
10873950c786SChanwoo Choi 	int state = 0;
10883950c786SChanwoo Choi 
10893950c786SChanwoo Choi 	if (!charger->externally_control)
10903950c786SChanwoo Choi 		state = regulator_is_enabled(charger->consumer);
10913950c786SChanwoo Choi 
1092*2d678e3eSKrzysztof Kozlowski 	return sysfs_emit(buf, "%s\n", str_enabled_disabled(state));
10933950c786SChanwoo Choi }
10943950c786SChanwoo Choi 
charger_externally_control_show(struct device * dev,struct device_attribute * attr,char * buf)10953950c786SChanwoo Choi static ssize_t charger_externally_control_show(struct device *dev,
10963950c786SChanwoo Choi 				struct device_attribute *attr, char *buf)
10973950c786SChanwoo Choi {
10983950c786SChanwoo Choi 	struct charger_regulator *charger = container_of(attr,
10993950c786SChanwoo Choi 			struct charger_regulator, attr_externally_control);
11003950c786SChanwoo Choi 
1101a441f3b9Sye xingchen 	return sysfs_emit(buf, "%d\n", charger->externally_control);
11023950c786SChanwoo Choi }
11033950c786SChanwoo Choi 
charger_externally_control_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)11043950c786SChanwoo Choi static ssize_t charger_externally_control_store(struct device *dev,
11053950c786SChanwoo Choi 				struct device_attribute *attr, const char *buf,
11063950c786SChanwoo Choi 				size_t count)
11073950c786SChanwoo Choi {
11083950c786SChanwoo Choi 	struct charger_regulator *charger
11093950c786SChanwoo Choi 		= container_of(attr, struct charger_regulator,
11103950c786SChanwoo Choi 					attr_externally_control);
11113950c786SChanwoo Choi 	struct charger_manager *cm = charger->cm;
11123950c786SChanwoo Choi 	struct charger_desc *desc = cm->desc;
11133950c786SChanwoo Choi 	int i;
11143950c786SChanwoo Choi 	int ret;
11153950c786SChanwoo Choi 	int externally_control;
11163950c786SChanwoo Choi 	int chargers_externally_control = 1;
11173950c786SChanwoo Choi 
11183950c786SChanwoo Choi 	ret = sscanf(buf, "%d", &externally_control);
11193950c786SChanwoo Choi 	if (ret == 0) {
11203950c786SChanwoo Choi 		ret = -EINVAL;
11213950c786SChanwoo Choi 		return ret;
11223950c786SChanwoo Choi 	}
11233950c786SChanwoo Choi 
11243950c786SChanwoo Choi 	if (!externally_control) {
11253950c786SChanwoo Choi 		charger->externally_control = 0;
11263950c786SChanwoo Choi 		return count;
11273950c786SChanwoo Choi 	}
11283950c786SChanwoo Choi 
11293950c786SChanwoo Choi 	for (i = 0; i < desc->num_charger_regulators; i++) {
11303950c786SChanwoo Choi 		if (&desc->charger_regulators[i] != charger &&
11313950c786SChanwoo Choi 			!desc->charger_regulators[i].externally_control) {
11323950c786SChanwoo Choi 			/*
11333950c786SChanwoo Choi 			 * At least, one charger is controlled by
11343950c786SChanwoo Choi 			 * charger-manager
11353950c786SChanwoo Choi 			 */
11363950c786SChanwoo Choi 			chargers_externally_control = 0;
11373950c786SChanwoo Choi 			break;
11383950c786SChanwoo Choi 		}
11393950c786SChanwoo Choi 	}
11403950c786SChanwoo Choi 
11413950c786SChanwoo Choi 	if (!chargers_externally_control) {
11423950c786SChanwoo Choi 		if (cm->charger_enabled) {
11433950c786SChanwoo Choi 			try_charger_enable(charger->cm, false);
11443950c786SChanwoo Choi 			charger->externally_control = externally_control;
11453950c786SChanwoo Choi 			try_charger_enable(charger->cm, true);
11463950c786SChanwoo Choi 		} else {
11473950c786SChanwoo Choi 			charger->externally_control = externally_control;
11483950c786SChanwoo Choi 		}
11493950c786SChanwoo Choi 	} else {
11503950c786SChanwoo Choi 		dev_warn(cm->dev,
1151e5409cbdSJoe Perches 			 "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n",
11523950c786SChanwoo Choi 			 charger->regulator_name);
11533950c786SChanwoo Choi 	}
11543950c786SChanwoo Choi 
11553950c786SChanwoo Choi 	return count;
11563950c786SChanwoo Choi }
11573950c786SChanwoo Choi 
115841468a11SChanwoo Choi /**
1159157ba1bbSSebastian Reichel  * charger_manager_prepare_sysfs - Prepare sysfs entry for each charger
116041468a11SChanwoo Choi  * @cm: the Charger Manager representing the battery.
116141468a11SChanwoo Choi  *
116241468a11SChanwoo Choi  * This function add sysfs entry for charger(regulator) to control charger from
116341468a11SChanwoo Choi  * user-space. If some development board use one more chargers for charging
116441468a11SChanwoo Choi  * but only need one charger on specific case which is dependent on user
116541468a11SChanwoo Choi  * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
116641468a11SChanwoo Choi  * class/power_supply/battery/charger.[index]/externally_control'. For example,
116741468a11SChanwoo Choi  * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
116841468a11SChanwoo Choi  * externally_control, this charger isn't controlled from charger-manager and
116941468a11SChanwoo Choi  * always stay off state of regulator.
117041468a11SChanwoo Choi  */
charger_manager_prepare_sysfs(struct charger_manager * cm)1171157ba1bbSSebastian Reichel static int charger_manager_prepare_sysfs(struct charger_manager *cm)
117241468a11SChanwoo Choi {
117341468a11SChanwoo Choi 	struct charger_desc *desc = cm->desc;
117441468a11SChanwoo Choi 	struct charger_regulator *charger;
117541468a11SChanwoo Choi 	int chargers_externally_control = 1;
1176efcca6bdSSebastian Reichel 	char *name;
117741468a11SChanwoo Choi 	int i;
117841468a11SChanwoo Choi 
117941468a11SChanwoo Choi 	/* Create sysfs entry to control charger(regulator) */
118041468a11SChanwoo Choi 	for (i = 0; i < desc->num_charger_regulators; i++) {
118141468a11SChanwoo Choi 		charger = &desc->charger_regulators[i];
118241468a11SChanwoo Choi 
1183efcca6bdSSebastian Reichel 		name = devm_kasprintf(cm->dev, GFP_KERNEL, "charger.%d", i);
1184efcca6bdSSebastian Reichel 		if (!name)
1185dc6ea7d4SAndi Shyti 			return -ENOMEM;
1186dc6ea7d4SAndi Shyti 
118741468a11SChanwoo Choi 		charger->attrs[0] = &charger->attr_name.attr;
118841468a11SChanwoo Choi 		charger->attrs[1] = &charger->attr_state.attr;
118941468a11SChanwoo Choi 		charger->attrs[2] = &charger->attr_externally_control.attr;
119041468a11SChanwoo Choi 		charger->attrs[3] = NULL;
1191157ba1bbSSebastian Reichel 
1192157ba1bbSSebastian Reichel 		charger->attr_grp.name = name;
1193157ba1bbSSebastian Reichel 		charger->attr_grp.attrs = charger->attrs;
1194157ba1bbSSebastian Reichel 		desc->sysfs_groups[i] = &charger->attr_grp;
119541468a11SChanwoo Choi 
119641468a11SChanwoo Choi 		sysfs_attr_init(&charger->attr_name.attr);
119741468a11SChanwoo Choi 		charger->attr_name.attr.name = "name";
119841468a11SChanwoo Choi 		charger->attr_name.attr.mode = 0444;
119941468a11SChanwoo Choi 		charger->attr_name.show = charger_name_show;
120041468a11SChanwoo Choi 
120141468a11SChanwoo Choi 		sysfs_attr_init(&charger->attr_state.attr);
120241468a11SChanwoo Choi 		charger->attr_state.attr.name = "state";
120341468a11SChanwoo Choi 		charger->attr_state.attr.mode = 0444;
120441468a11SChanwoo Choi 		charger->attr_state.show = charger_state_show;
120541468a11SChanwoo Choi 
120641468a11SChanwoo Choi 		sysfs_attr_init(&charger->attr_externally_control.attr);
120741468a11SChanwoo Choi 		charger->attr_externally_control.attr.name
120841468a11SChanwoo Choi 				= "externally_control";
120941468a11SChanwoo Choi 		charger->attr_externally_control.attr.mode = 0644;
121041468a11SChanwoo Choi 		charger->attr_externally_control.show
121141468a11SChanwoo Choi 				= charger_externally_control_show;
121241468a11SChanwoo Choi 		charger->attr_externally_control.store
121341468a11SChanwoo Choi 				= charger_externally_control_store;
121441468a11SChanwoo Choi 
121541468a11SChanwoo Choi 		if (!desc->charger_regulators[i].externally_control ||
121641468a11SChanwoo Choi 				!chargers_externally_control)
121741468a11SChanwoo Choi 			chargers_externally_control = 0;
121841468a11SChanwoo Choi 
1219e5409cbdSJoe Perches 		dev_info(cm->dev, "'%s' regulator's externally_control is %d\n",
1220e5409cbdSJoe Perches 			 charger->regulator_name, charger->externally_control);
122141468a11SChanwoo Choi 	}
122241468a11SChanwoo Choi 
122341468a11SChanwoo Choi 	if (chargers_externally_control) {
1224e5409cbdSJoe Perches 		dev_err(cm->dev, "Cannot register regulator because charger-manager must need at least one charger for charging battery\n");
1225dc6ea7d4SAndi Shyti 		return -EINVAL;
122641468a11SChanwoo Choi 	}
122741468a11SChanwoo Choi 
1228dc6ea7d4SAndi Shyti 	return 0;
122941468a11SChanwoo Choi }
123041468a11SChanwoo Choi 
cm_init_thermal_data(struct charger_manager * cm,struct power_supply * fuel_gauge,enum power_supply_property * properties,size_t * num_properties)1231bdbe8144SKrzysztof Kozlowski static int cm_init_thermal_data(struct charger_manager *cm,
12324cb38258SSebastian Reichel 		struct power_supply *fuel_gauge,
12334cb38258SSebastian Reichel 		enum power_supply_property *properties,
12344cb38258SSebastian Reichel 		size_t *num_properties)
12355c49a625SJonghwa Lee {
12365c49a625SJonghwa Lee 	struct charger_desc *desc = cm->desc;
12375c49a625SJonghwa Lee 	union power_supply_propval val;
12385c49a625SJonghwa Lee 	int ret;
12395c49a625SJonghwa Lee 
12405c49a625SJonghwa Lee 	/* Verify whether fuel gauge provides battery temperature */
1241b70229bcSKrzysztof Kozlowski 	ret = power_supply_get_property(fuel_gauge,
12425c49a625SJonghwa Lee 					POWER_SUPPLY_PROP_TEMP, &val);
12435c49a625SJonghwa Lee 
12445c49a625SJonghwa Lee 	if (!ret) {
12454cb38258SSebastian Reichel 		properties[*num_properties] = POWER_SUPPLY_PROP_TEMP;
12464cb38258SSebastian Reichel 		(*num_properties)++;
12475c49a625SJonghwa Lee 		cm->desc->measure_battery_temp = true;
12485c49a625SJonghwa Lee 	}
12495c49a625SJonghwa Lee #ifdef CONFIG_THERMAL
12505c49a625SJonghwa Lee 	if (ret && desc->thermal_zone) {
12515c49a625SJonghwa Lee 		cm->tzd_batt =
12525c49a625SJonghwa Lee 			thermal_zone_get_zone_by_name(desc->thermal_zone);
12535c49a625SJonghwa Lee 		if (IS_ERR(cm->tzd_batt))
12545c49a625SJonghwa Lee 			return PTR_ERR(cm->tzd_batt);
12555c49a625SJonghwa Lee 
12565c49a625SJonghwa Lee 		/* Use external thermometer */
1257cdaeb151SJonathan Bakker 		properties[*num_properties] = POWER_SUPPLY_PROP_TEMP;
12584cb38258SSebastian Reichel 		(*num_properties)++;
12595c49a625SJonghwa Lee 		cm->desc->measure_battery_temp = true;
12605c49a625SJonghwa Lee 		ret = 0;
12615c49a625SJonghwa Lee 	}
12625c49a625SJonghwa Lee #endif
12635c49a625SJonghwa Lee 	if (cm->desc->measure_battery_temp) {
12645c49a625SJonghwa Lee 		/* NOTICE : Default allowable minimum charge temperature is 0 */
12655c49a625SJonghwa Lee 		if (!desc->temp_max)
12665c49a625SJonghwa Lee 			desc->temp_max = CM_DEFAULT_CHARGE_TEMP_MAX;
12675c49a625SJonghwa Lee 		if (!desc->temp_diff)
12685c49a625SJonghwa Lee 			desc->temp_diff = CM_DEFAULT_RECHARGE_TEMP_DIFF;
12695c49a625SJonghwa Lee 	}
12705c49a625SJonghwa Lee 
12715c49a625SJonghwa Lee 	return ret;
12725c49a625SJonghwa Lee }
12735c49a625SJonghwa Lee 
12748fb08855SFabian Frederick static const struct of_device_id charger_manager_match[] = {
1275856ee611SJonghwa Lee 	{
1276856ee611SJonghwa Lee 		.compatible = "charger-manager",
1277856ee611SJonghwa Lee 	},
1278856ee611SJonghwa Lee 	{},
1279856ee611SJonghwa Lee };
1280073b5d5bSZou Wei MODULE_DEVICE_TABLE(of, charger_manager_match);
1281856ee611SJonghwa Lee 
of_cm_parse_desc(struct device * dev)1282434a09f9SAnton Vorontsov static struct charger_desc *of_cm_parse_desc(struct device *dev)
1283856ee611SJonghwa Lee {
1284856ee611SJonghwa Lee 	struct charger_desc *desc;
1285856ee611SJonghwa Lee 	struct device_node *np = dev->of_node;
1286856ee611SJonghwa Lee 	u32 poll_mode = CM_POLL_DISABLE;
1287856ee611SJonghwa Lee 	u32 battery_stat = CM_NO_BATTERY;
1288856ee611SJonghwa Lee 	int num_chgs = 0;
1289856ee611SJonghwa Lee 
1290856ee611SJonghwa Lee 	desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
1291856ee611SJonghwa Lee 	if (!desc)
1292856ee611SJonghwa Lee 		return ERR_PTR(-ENOMEM);
1293856ee611SJonghwa Lee 
1294856ee611SJonghwa Lee 	of_property_read_string(np, "cm-name", &desc->psy_name);
1295856ee611SJonghwa Lee 
1296856ee611SJonghwa Lee 	of_property_read_u32(np, "cm-poll-mode", &poll_mode);
1297856ee611SJonghwa Lee 	desc->polling_mode = poll_mode;
1298856ee611SJonghwa Lee 
1299856ee611SJonghwa Lee 	of_property_read_u32(np, "cm-poll-interval",
1300856ee611SJonghwa Lee 				&desc->polling_interval_ms);
1301856ee611SJonghwa Lee 
1302856ee611SJonghwa Lee 	of_property_read_u32(np, "cm-fullbatt-vchkdrop-volt",
1303856ee611SJonghwa Lee 					&desc->fullbatt_vchkdrop_uV);
1304856ee611SJonghwa Lee 	of_property_read_u32(np, "cm-fullbatt-voltage", &desc->fullbatt_uV);
1305856ee611SJonghwa Lee 	of_property_read_u32(np, "cm-fullbatt-soc", &desc->fullbatt_soc);
1306856ee611SJonghwa Lee 	of_property_read_u32(np, "cm-fullbatt-capacity",
1307856ee611SJonghwa Lee 					&desc->fullbatt_full_capacity);
1308856ee611SJonghwa Lee 
1309856ee611SJonghwa Lee 	of_property_read_u32(np, "cm-battery-stat", &battery_stat);
1310856ee611SJonghwa Lee 	desc->battery_present = battery_stat;
1311856ee611SJonghwa Lee 
1312856ee611SJonghwa Lee 	/* chargers */
1313683aa86eSJonathan Bakker 	num_chgs = of_property_count_strings(np, "cm-chargers");
1314683aa86eSJonathan Bakker 	if (num_chgs > 0) {
1315a53a68ceSBaolin Wang 		int i;
1316a53a68ceSBaolin Wang 
1317856ee611SJonghwa Lee 		/* Allocate empty bin at the tail of array */
1318a86854d0SKees Cook 		desc->psy_charger_stat = devm_kcalloc(dev,
1319a86854d0SKees Cook 						      num_chgs + 1,
1320a86854d0SKees Cook 						      sizeof(char *),
1321a86854d0SKees Cook 						      GFP_KERNEL);
1322a53a68ceSBaolin Wang 		if (!desc->psy_charger_stat)
1323a53a68ceSBaolin Wang 			return ERR_PTR(-ENOMEM);
1324a53a68ceSBaolin Wang 
1325856ee611SJonghwa Lee 		for (i = 0; i < num_chgs; i++)
1326856ee611SJonghwa Lee 			of_property_read_string_index(np, "cm-chargers",
1327856ee611SJonghwa Lee 						      i, &desc->psy_charger_stat[i]);
1328856ee611SJonghwa Lee 	}
1329856ee611SJonghwa Lee 
1330856ee611SJonghwa Lee 	of_property_read_string(np, "cm-fuel-gauge", &desc->psy_fuel_gauge);
1331856ee611SJonghwa Lee 
1332856ee611SJonghwa Lee 	of_property_read_string(np, "cm-thermal-zone", &desc->thermal_zone);
1333856ee611SJonghwa Lee 
1334856ee611SJonghwa Lee 	of_property_read_u32(np, "cm-battery-cold", &desc->temp_min);
133583425c83SRob Herring 	if (of_property_read_bool(np, "cm-battery-cold-in-minus"))
1336856ee611SJonghwa Lee 		desc->temp_min *= -1;
1337856ee611SJonghwa Lee 	of_property_read_u32(np, "cm-battery-hot", &desc->temp_max);
1338856ee611SJonghwa Lee 	of_property_read_u32(np, "cm-battery-temp-diff", &desc->temp_diff);
1339856ee611SJonghwa Lee 
1340856ee611SJonghwa Lee 	of_property_read_u32(np, "cm-charging-max",
1341856ee611SJonghwa Lee 				&desc->charging_max_duration_ms);
1342856ee611SJonghwa Lee 	of_property_read_u32(np, "cm-discharging-max",
1343856ee611SJonghwa Lee 				&desc->discharging_max_duration_ms);
1344856ee611SJonghwa Lee 
134502276af2SKrzysztof Kozlowski 	/* battery charger regulators */
1346856ee611SJonghwa Lee 	desc->num_charger_regulators = of_get_child_count(np);
1347856ee611SJonghwa Lee 	if (desc->num_charger_regulators) {
1348856ee611SJonghwa Lee 		struct charger_regulator *chg_regs;
1349856ee611SJonghwa Lee 		struct device_node *child;
1350856ee611SJonghwa Lee 
1351a86854d0SKees Cook 		chg_regs = devm_kcalloc(dev,
1352a86854d0SKees Cook 					desc->num_charger_regulators,
1353a86854d0SKees Cook 					sizeof(*chg_regs),
1354856ee611SJonghwa Lee 					GFP_KERNEL);
1355856ee611SJonghwa Lee 		if (!chg_regs)
1356856ee611SJonghwa Lee 			return ERR_PTR(-ENOMEM);
1357856ee611SJonghwa Lee 
1358856ee611SJonghwa Lee 		desc->charger_regulators = chg_regs;
1359856ee611SJonghwa Lee 
1360157ba1bbSSebastian Reichel 		desc->sysfs_groups = devm_kcalloc(dev,
1361157ba1bbSSebastian Reichel 					desc->num_charger_regulators + 1,
1362157ba1bbSSebastian Reichel 					sizeof(*desc->sysfs_groups),
1363157ba1bbSSebastian Reichel 					GFP_KERNEL);
1364157ba1bbSSebastian Reichel 		if (!desc->sysfs_groups)
1365157ba1bbSSebastian Reichel 			return ERR_PTR(-ENOMEM);
1366157ba1bbSSebastian Reichel 
1367856ee611SJonghwa Lee 		for_each_child_of_node(np, child) {
1368856ee611SJonghwa Lee 			struct charger_cable *cables;
1369856ee611SJonghwa Lee 			struct device_node *_child;
1370856ee611SJonghwa Lee 
1371856ee611SJonghwa Lee 			of_property_read_string(child, "cm-regulator-name",
1372856ee611SJonghwa Lee 					&chg_regs->regulator_name);
1373856ee611SJonghwa Lee 
1374856ee611SJonghwa Lee 			/* charger cables */
1375856ee611SJonghwa Lee 			chg_regs->num_cables = of_get_child_count(child);
1376856ee611SJonghwa Lee 			if (chg_regs->num_cables) {
1377a86854d0SKees Cook 				cables = devm_kcalloc(dev,
1378a86854d0SKees Cook 						      chg_regs->num_cables,
1379a86854d0SKees Cook 						      sizeof(*cables),
1380856ee611SJonghwa Lee 						      GFP_KERNEL);
13818e5cfb74SJulia Lawall 				if (!cables) {
13828e5cfb74SJulia Lawall 					of_node_put(child);
1383856ee611SJonghwa Lee 					return ERR_PTR(-ENOMEM);
13848e5cfb74SJulia Lawall 				}
1385856ee611SJonghwa Lee 
1386856ee611SJonghwa Lee 				chg_regs->cables = cables;
1387856ee611SJonghwa Lee 
1388856ee611SJonghwa Lee 				for_each_child_of_node(child, _child) {
1389856ee611SJonghwa Lee 					of_property_read_string(_child,
1390856ee611SJonghwa Lee 					"cm-cable-name", &cables->name);
1391856ee611SJonghwa Lee 					of_property_read_string(_child,
1392856ee611SJonghwa Lee 					"cm-cable-extcon",
1393856ee611SJonghwa Lee 					&cables->extcon_name);
1394856ee611SJonghwa Lee 					of_property_read_u32(_child,
1395856ee611SJonghwa Lee 					"cm-cable-min",
1396856ee611SJonghwa Lee 					&cables->min_uA);
1397856ee611SJonghwa Lee 					of_property_read_u32(_child,
1398856ee611SJonghwa Lee 					"cm-cable-max",
1399856ee611SJonghwa Lee 					&cables->max_uA);
1400856ee611SJonghwa Lee 					cables++;
1401856ee611SJonghwa Lee 				}
1402856ee611SJonghwa Lee 			}
1403856ee611SJonghwa Lee 			chg_regs++;
1404856ee611SJonghwa Lee 		}
1405856ee611SJonghwa Lee 	}
1406856ee611SJonghwa Lee 	return desc;
1407856ee611SJonghwa Lee }
1408856ee611SJonghwa Lee 
cm_get_drv_data(struct platform_device * pdev)1409856ee611SJonghwa Lee static inline struct charger_desc *cm_get_drv_data(struct platform_device *pdev)
1410856ee611SJonghwa Lee {
1411856ee611SJonghwa Lee 	if (pdev->dev.of_node)
1412856ee611SJonghwa Lee 		return of_cm_parse_desc(&pdev->dev);
141386515b7dSJingoo Han 	return dev_get_platdata(&pdev->dev);
1414856ee611SJonghwa Lee }
1415856ee611SJonghwa Lee 
cm_timer_func(struct alarm * alarm,ktime_t now)14162634303fSThomas Gleixner static void cm_timer_func(struct alarm *alarm, ktime_t now)
1417c1155c64SJonghwa Lee {
1418c1155c64SJonghwa Lee 	cm_timer_set = false;
1419c1155c64SJonghwa Lee }
1420c1155c64SJonghwa Lee 
charger_manager_probe(struct platform_device * pdev)14213bb3dbbdSDonggeun Kim static int charger_manager_probe(struct platform_device *pdev)
14223bb3dbbdSDonggeun Kim {
1423856ee611SJonghwa Lee 	struct charger_desc *desc = cm_get_drv_data(pdev);
14243bb3dbbdSDonggeun Kim 	struct charger_manager *cm;
1425dc6ea7d4SAndi Shyti 	int ret, i = 0;
1426ad3d13eeSDonggeun Kim 	union power_supply_propval val;
1427bdbe8144SKrzysztof Kozlowski 	struct power_supply *fuel_gauge;
14284cb38258SSebastian Reichel 	enum power_supply_property *properties;
14294cb38258SSebastian Reichel 	size_t num_properties;
1430297d716fSKrzysztof Kozlowski 	struct power_supply_config psy_cfg = {};
14313bb3dbbdSDonggeun Kim 
1432c6738d06SChanwoo Choi 	if (IS_ERR(desc)) {
1433e5409cbdSJoe Perches 		dev_err(&pdev->dev, "No platform data (desc) found\n");
1434f25a646fSBaolin Wang 		return PTR_ERR(desc);
14353bb3dbbdSDonggeun Kim 	}
14363bb3dbbdSDonggeun Kim 
143718a89d5cSChristophe JAILLET 	cm = devm_kzalloc(&pdev->dev, sizeof(*cm), GFP_KERNEL);
1438883c10a9SJonghwa Lee 	if (!cm)
1439883c10a9SJonghwa Lee 		return -ENOMEM;
14403bb3dbbdSDonggeun Kim 
14413bb3dbbdSDonggeun Kim 	/* Basic Values. Unspecified are Null or 0 */
14423bb3dbbdSDonggeun Kim 	cm->dev = &pdev->dev;
1443883c10a9SJonghwa Lee 	cm->desc = desc;
1444297d716fSKrzysztof Kozlowski 	psy_cfg.drv_data = cm;
14453bb3dbbdSDonggeun Kim 
1446c1155c64SJonghwa Lee 	/* Initialize alarm timer */
1447c1155c64SJonghwa Lee 	if (alarmtimer_get_rtcdev()) {
1448c1155c64SJonghwa Lee 		cm_timer = devm_kzalloc(cm->dev, sizeof(*cm_timer), GFP_KERNEL);
1449096fc160SChristophe JAILLET 		if (!cm_timer)
1450096fc160SChristophe JAILLET 			return -ENOMEM;
1451c1155c64SJonghwa Lee 		alarm_init(cm_timer, ALARM_BOOTTIME, cm_timer_func);
1452c1155c64SJonghwa Lee 	}
1453c1155c64SJonghwa Lee 
1454d829dc75SChanwoo Choi 	/*
14550299484eSChristophe JAILLET 	 * Some of the following do not need to be errors.
14560299484eSChristophe JAILLET 	 * Users may intentionally ignore those features.
1457d829dc75SChanwoo Choi 	 */
1458d829dc75SChanwoo Choi 	if (desc->fullbatt_uV == 0) {
1459e5409cbdSJoe Perches 		dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n");
1460d829dc75SChanwoo Choi 	}
14619584051fSJonghwa Lee 	if (!desc->fullbatt_vchkdrop_uV) {
1462e5409cbdSJoe Perches 		dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
1463d829dc75SChanwoo Choi 		desc->fullbatt_vchkdrop_uV = 0;
1464d829dc75SChanwoo Choi 	}
14652ed9e9b6SChanwoo Choi 	if (desc->fullbatt_soc == 0) {
1466e5409cbdSJoe Perches 		dev_info(&pdev->dev, "Ignoring full-battery soc(state of charge) threshold as it is not supplied\n");
14672ed9e9b6SChanwoo Choi 	}
14682ed9e9b6SChanwoo Choi 	if (desc->fullbatt_full_capacity == 0) {
1469e5409cbdSJoe Perches 		dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n");
14702ed9e9b6SChanwoo Choi 	}
1471d829dc75SChanwoo Choi 
14723bb3dbbdSDonggeun Kim 	if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
1473e5409cbdSJoe Perches 		dev_err(&pdev->dev, "charger_regulators undefined\n");
1474883c10a9SJonghwa Lee 		return -EINVAL;
14753bb3dbbdSDonggeun Kim 	}
14763bb3dbbdSDonggeun Kim 
14773bb3dbbdSDonggeun Kim 	if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
1478e5409cbdSJoe Perches 		dev_err(&pdev->dev, "No power supply defined\n");
1479883c10a9SJonghwa Lee 		return -EINVAL;
14803bb3dbbdSDonggeun Kim 	}
14813bb3dbbdSDonggeun Kim 
1482661a8886SKrzysztof Kozlowski 	if (!desc->psy_fuel_gauge) {
1483661a8886SKrzysztof Kozlowski 		dev_err(&pdev->dev, "No fuel gauge power supply defined\n");
1484661a8886SKrzysztof Kozlowski 		return -EINVAL;
1485661a8886SKrzysztof Kozlowski 	}
1486661a8886SKrzysztof Kozlowski 
1487cdaf3e15SKrzysztof Kozlowski 	/* Check if charger's supplies are present at probe */
14883bb3dbbdSDonggeun Kim 	for (i = 0; desc->psy_charger_stat[i]; i++) {
1489cdaf3e15SKrzysztof Kozlowski 		struct power_supply *psy;
1490cdaf3e15SKrzysztof Kozlowski 
1491cdaf3e15SKrzysztof Kozlowski 		psy = power_supply_get_by_name(desc->psy_charger_stat[i]);
1492cdaf3e15SKrzysztof Kozlowski 		if (!psy) {
1493e5409cbdSJoe Perches 			dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
14943bb3dbbdSDonggeun Kim 				desc->psy_charger_stat[i]);
1495883c10a9SJonghwa Lee 			return -ENODEV;
14963bb3dbbdSDonggeun Kim 		}
1497b43eb35aSKrzysztof Kozlowski 		power_supply_put(psy);
14983bb3dbbdSDonggeun Kim 	}
14993bb3dbbdSDonggeun Kim 
1500111242d6SLadislav Michl 	if (cm->desc->polling_mode != CM_POLL_DISABLE &&
1501111242d6SLadislav Michl 	    (desc->polling_interval_ms == 0 ||
1502111242d6SLadislav Michl 	     msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL)) {
15033bb3dbbdSDonggeun Kim 		dev_err(&pdev->dev, "polling_interval_ms is too small\n");
1504883c10a9SJonghwa Lee 		return -EINVAL;
15053bb3dbbdSDonggeun Kim 	}
15063bb3dbbdSDonggeun Kim 
15078fcfe088SChanwoo Choi 	if (!desc->charging_max_duration_ms ||
15088fcfe088SChanwoo Choi 			!desc->discharging_max_duration_ms) {
1509e5409cbdSJoe Perches 		dev_info(&pdev->dev, "Cannot limit charging duration checking mechanism to prevent overcharge/overheat and control discharging duration\n");
15108fcfe088SChanwoo Choi 		desc->charging_max_duration_ms = 0;
15118fcfe088SChanwoo Choi 		desc->discharging_max_duration_ms = 0;
15128fcfe088SChanwoo Choi 	}
15138fcfe088SChanwoo Choi 
15143bb3dbbdSDonggeun Kim 	platform_set_drvdata(pdev, cm);
15153bb3dbbdSDonggeun Kim 
1516297d716fSKrzysztof Kozlowski 	memcpy(&cm->charger_psy_desc, &psy_default, sizeof(psy_default));
1517bb2a95c2SAxel Lin 
151841468a11SChanwoo Choi 	if (!desc->psy_name)
1519e1402bd2SJustin Stitt 		strscpy(cm->psy_name_buf, psy_default.name,
1520e1402bd2SJustin Stitt 			sizeof(cm->psy_name_buf));
152141468a11SChanwoo Choi 	else
1522e1402bd2SJustin Stitt 		strscpy(cm->psy_name_buf, desc->psy_name,
1523e1402bd2SJustin Stitt 			sizeof(cm->psy_name_buf));
1524297d716fSKrzysztof Kozlowski 	cm->charger_psy_desc.name = cm->psy_name_buf;
1525ad3d13eeSDonggeun Kim 
1526ad3d13eeSDonggeun Kim 	/* Allocate for psy properties because they may vary */
15274cb38258SSebastian Reichel 	properties = devm_kcalloc(&pdev->dev,
1528a86854d0SKees Cook 			     ARRAY_SIZE(default_charger_props) +
1529a86854d0SKees Cook 				NUM_CHARGER_PSY_OPTIONAL,
15304cb38258SSebastian Reichel 			     sizeof(*properties), GFP_KERNEL);
15314cb38258SSebastian Reichel 	if (!properties)
1532883c10a9SJonghwa Lee 		return -ENOMEM;
1533883c10a9SJonghwa Lee 
15344cb38258SSebastian Reichel 	memcpy(properties, default_charger_props,
1535ad3d13eeSDonggeun Kim 		sizeof(enum power_supply_property) *
1536ad3d13eeSDonggeun Kim 		ARRAY_SIZE(default_charger_props));
153797ed79f4SMichał Mirosław 	num_properties = ARRAY_SIZE(default_charger_props);
1538ad3d13eeSDonggeun Kim 
1539ad3d13eeSDonggeun Kim 	/* Find which optional psy-properties are available */
1540b43eb35aSKrzysztof Kozlowski 	fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1541b43eb35aSKrzysztof Kozlowski 	if (!fuel_gauge) {
1542b43eb35aSKrzysztof Kozlowski 		dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1543b43eb35aSKrzysztof Kozlowski 			desc->psy_fuel_gauge);
1544b43eb35aSKrzysztof Kozlowski 		return -ENODEV;
1545b43eb35aSKrzysztof Kozlowski 	}
1546b70229bcSKrzysztof Kozlowski 	if (!power_supply_get_property(fuel_gauge,
15470a9e0f94SJonghwa Lee 					POWER_SUPPLY_PROP_CHARGE_FULL, &val)) {
15480a9e0f94SJonghwa Lee 		properties[num_properties] =
15490a9e0f94SJonghwa Lee 				POWER_SUPPLY_PROP_CHARGE_FULL;
15500a9e0f94SJonghwa Lee 		num_properties++;
15510a9e0f94SJonghwa Lee 	}
15520a9e0f94SJonghwa Lee 	if (!power_supply_get_property(fuel_gauge,
1553ad3d13eeSDonggeun Kim 					  POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
15544cb38258SSebastian Reichel 		properties[num_properties] =
1555ad3d13eeSDonggeun Kim 				POWER_SUPPLY_PROP_CHARGE_NOW;
15564cb38258SSebastian Reichel 		num_properties++;
1557ad3d13eeSDonggeun Kim 	}
1558b70229bcSKrzysztof Kozlowski 	if (!power_supply_get_property(fuel_gauge,
1559ad3d13eeSDonggeun Kim 					  POWER_SUPPLY_PROP_CURRENT_NOW,
1560ad3d13eeSDonggeun Kim 					  &val)) {
15614cb38258SSebastian Reichel 		properties[num_properties] =
1562ad3d13eeSDonggeun Kim 				POWER_SUPPLY_PROP_CURRENT_NOW;
15634cb38258SSebastian Reichel 		num_properties++;
1564ad3d13eeSDonggeun Kim 	}
1565bb2a95c2SAxel Lin 
15664cb38258SSebastian Reichel 	ret = cm_init_thermal_data(cm, fuel_gauge, properties, &num_properties);
15675c49a625SJonghwa Lee 	if (ret) {
15685c49a625SJonghwa Lee 		dev_err(&pdev->dev, "Failed to initialize thermal data\n");
15695c49a625SJonghwa Lee 		cm->desc->measure_battery_temp = false;
1570ad3d13eeSDonggeun Kim 	}
1571b43eb35aSKrzysztof Kozlowski 	power_supply_put(fuel_gauge);
1572ad3d13eeSDonggeun Kim 
15734cb38258SSebastian Reichel 	cm->charger_psy_desc.properties = properties;
15744cb38258SSebastian Reichel 	cm->charger_psy_desc.num_properties = num_properties;
15754cb38258SSebastian Reichel 
1576157ba1bbSSebastian Reichel 	/* Register sysfs entry for charger(regulator) */
1577157ba1bbSSebastian Reichel 	ret = charger_manager_prepare_sysfs(cm);
1578157ba1bbSSebastian Reichel 	if (ret < 0) {
1579157ba1bbSSebastian Reichel 		dev_err(&pdev->dev,
1580157ba1bbSSebastian Reichel 			"Cannot prepare sysfs entry of regulators\n");
1581157ba1bbSSebastian Reichel 		return ret;
1582157ba1bbSSebastian Reichel 	}
1583157ba1bbSSebastian Reichel 	psy_cfg.attr_grp = desc->sysfs_groups;
1584157ba1bbSSebastian Reichel 
15854970d839SKrzysztof Kozlowski 	cm->charger_psy = power_supply_register(&pdev->dev,
15864970d839SKrzysztof Kozlowski 						&cm->charger_psy_desc,
1587297d716fSKrzysztof Kozlowski 						&psy_cfg);
1588297d716fSKrzysztof Kozlowski 	if (IS_ERR(cm->charger_psy)) {
1589e5409cbdSJoe Perches 		dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n",
1590ecf896b9SKrzysztof Kozlowski 			cm->charger_psy_desc.name);
1591297d716fSKrzysztof Kozlowski 		return PTR_ERR(cm->charger_psy);
1592ad3d13eeSDonggeun Kim 	}
1593ad3d13eeSDonggeun Kim 
159441468a11SChanwoo Choi 	/* Register extcon device for charger cable */
159541468a11SChanwoo Choi 	ret = charger_manager_register_extcon(cm);
1596bee737bcSChanwoo Choi 	if (ret < 0) {
159741468a11SChanwoo Choi 		dev_err(&pdev->dev, "Cannot initialize extcon device\n");
159841468a11SChanwoo Choi 		goto err_reg_extcon;
1599bee737bcSChanwoo Choi 	}
16003950c786SChanwoo Choi 
16013bb3dbbdSDonggeun Kim 	/* Add to the list */
16023bb3dbbdSDonggeun Kim 	mutex_lock(&cm_list_mtx);
16033bb3dbbdSDonggeun Kim 	list_add(&cm->entry, &cm_list);
16043bb3dbbdSDonggeun Kim 	mutex_unlock(&cm_list_mtx);
16053bb3dbbdSDonggeun Kim 
1606dfeccb12SChanwoo Choi 	/*
160731ba6fadSBhaskar Chowdhury 	 * Charger-manager is capable of waking up the system from sleep
16088c13b6f1SBaolin Wang 	 * when event is happened through cm_notify_event()
1609dfeccb12SChanwoo Choi 	 */
1610dfeccb12SChanwoo Choi 	device_init_wakeup(&pdev->dev, true);
1611dfeccb12SChanwoo Choi 	device_set_wakeup_capable(&pdev->dev, false);
1612dfeccb12SChanwoo Choi 
1613b1022e24SChanwoo Choi 	/*
1614b1022e24SChanwoo Choi 	 * Charger-manager have to check the charging state right after
161502276af2SKrzysztof Kozlowski 	 * initialization of charger-manager and then update current charging
1616b1022e24SChanwoo Choi 	 * state.
1617b1022e24SChanwoo Choi 	 */
1618b1022e24SChanwoo Choi 	cm_monitor();
1619b1022e24SChanwoo Choi 
1620d829dc75SChanwoo Choi 	schedule_work(&setup_polling);
1621d829dc75SChanwoo Choi 
16223bb3dbbdSDonggeun Kim 	return 0;
16233bb3dbbdSDonggeun Kim 
162441468a11SChanwoo Choi err_reg_extcon:
1625c22b90dbSKrzysztof Kozlowski 	for (i = 0; i < desc->num_charger_regulators; i++)
1626bee737bcSChanwoo Choi 		regulator_put(desc->charger_regulators[i].consumer);
1627bee737bcSChanwoo Choi 
1628297d716fSKrzysztof Kozlowski 	power_supply_unregister(cm->charger_psy);
1629883c10a9SJonghwa Lee 
16303bb3dbbdSDonggeun Kim 	return ret;
16313bb3dbbdSDonggeun Kim }
16323bb3dbbdSDonggeun Kim 
charger_manager_remove(struct platform_device * pdev)1633403eebf9SUwe Kleine-König static void charger_manager_remove(struct platform_device *pdev)
16343bb3dbbdSDonggeun Kim {
16353bb3dbbdSDonggeun Kim 	struct charger_manager *cm = platform_get_drvdata(pdev);
16363bb3dbbdSDonggeun Kim 	struct charger_desc *desc = cm->desc;
1637bee737bcSChanwoo Choi 	int i = 0;
16383bb3dbbdSDonggeun Kim 
16393bb3dbbdSDonggeun Kim 	/* Remove from the list */
16403bb3dbbdSDonggeun Kim 	mutex_lock(&cm_list_mtx);
16413bb3dbbdSDonggeun Kim 	list_del(&cm->entry);
16423bb3dbbdSDonggeun Kim 	mutex_unlock(&cm_list_mtx);
16433bb3dbbdSDonggeun Kim 
1644d829dc75SChanwoo Choi 	cancel_work_sync(&setup_polling);
1645d829dc75SChanwoo Choi 	cancel_delayed_work_sync(&cm_monitor_work);
1646d829dc75SChanwoo Choi 
1647bee737bcSChanwoo Choi 	for (i = 0 ; i < desc->num_charger_regulators ; i++)
1648bee737bcSChanwoo Choi 		regulator_put(desc->charger_regulators[i].consumer);
1649bee737bcSChanwoo Choi 
1650297d716fSKrzysztof Kozlowski 	power_supply_unregister(cm->charger_psy);
1651d829dc75SChanwoo Choi 
1652d829dc75SChanwoo Choi 	try_charger_enable(cm, false);
16533bb3dbbdSDonggeun Kim }
16543bb3dbbdSDonggeun Kim 
16551bbe24d4SAxel Lin static const struct platform_device_id charger_manager_id[] = {
16563bb3dbbdSDonggeun Kim 	{ "charger-manager", 0 },
16573bb3dbbdSDonggeun Kim 	{ },
16583bb3dbbdSDonggeun Kim };
16591bbe24d4SAxel Lin MODULE_DEVICE_TABLE(platform, charger_manager_id);
16603bb3dbbdSDonggeun Kim 
cm_suspend_noirq(struct device * dev)1661dfeccb12SChanwoo Choi static int cm_suspend_noirq(struct device *dev)
1662dfeccb12SChanwoo Choi {
1663dfeccb12SChanwoo Choi 	if (device_may_wakeup(dev)) {
1664dfeccb12SChanwoo Choi 		device_set_wakeup_capable(dev, false);
1665dc6ea7d4SAndi Shyti 		return -EAGAIN;
1666dfeccb12SChanwoo Choi 	}
1667dfeccb12SChanwoo Choi 
1668dc6ea7d4SAndi Shyti 	return 0;
1669dfeccb12SChanwoo Choi }
1670dfeccb12SChanwoo Choi 
cm_need_to_awake(void)1671c1155c64SJonghwa Lee static bool cm_need_to_awake(void)
1672c1155c64SJonghwa Lee {
1673c1155c64SJonghwa Lee 	struct charger_manager *cm;
1674c1155c64SJonghwa Lee 
1675c1155c64SJonghwa Lee 	if (cm_timer)
1676c1155c64SJonghwa Lee 		return false;
1677c1155c64SJonghwa Lee 
1678c1155c64SJonghwa Lee 	mutex_lock(&cm_list_mtx);
1679c1155c64SJonghwa Lee 	list_for_each_entry(cm, &cm_list, entry) {
1680c1155c64SJonghwa Lee 		if (is_charging(cm)) {
1681c1155c64SJonghwa Lee 			mutex_unlock(&cm_list_mtx);
1682c1155c64SJonghwa Lee 			return true;
1683c1155c64SJonghwa Lee 		}
1684c1155c64SJonghwa Lee 	}
1685c1155c64SJonghwa Lee 	mutex_unlock(&cm_list_mtx);
1686c1155c64SJonghwa Lee 
1687c1155c64SJonghwa Lee 	return false;
1688c1155c64SJonghwa Lee }
1689c1155c64SJonghwa Lee 
cm_suspend_prepare(struct device * dev)16903bb3dbbdSDonggeun Kim static int cm_suspend_prepare(struct device *dev)
16913bb3dbbdSDonggeun Kim {
1692c1155c64SJonghwa Lee 	if (cm_need_to_awake())
1693c1155c64SJonghwa Lee 		return -EBUSY;
16943bb3dbbdSDonggeun Kim 
1695c1155c64SJonghwa Lee 	if (!cm_suspended)
16963bb3dbbdSDonggeun Kim 		cm_suspended = true;
16973bb3dbbdSDonggeun Kim 
1698c1155c64SJonghwa Lee 	cm_timer_set = cm_setup_timer();
1699c1155c64SJonghwa Lee 
1700c1155c64SJonghwa Lee 	if (cm_timer_set) {
1701c1155c64SJonghwa Lee 		cancel_work_sync(&setup_polling);
1702c1155c64SJonghwa Lee 		cancel_delayed_work_sync(&cm_monitor_work);
17033bb3dbbdSDonggeun Kim 	}
17043bb3dbbdSDonggeun Kim 
17053bb3dbbdSDonggeun Kim 	return 0;
17063bb3dbbdSDonggeun Kim }
17073bb3dbbdSDonggeun Kim 
cm_suspend_complete(struct device * dev)17083bb3dbbdSDonggeun Kim static void cm_suspend_complete(struct device *dev)
17093bb3dbbdSDonggeun Kim {
1710bb2a95c2SAxel Lin 	struct charger_manager *cm = dev_get_drvdata(dev);
17113bb3dbbdSDonggeun Kim 
1712c1155c64SJonghwa Lee 	if (cm_suspended)
17133bb3dbbdSDonggeun Kim 		cm_suspended = false;
1714c1155c64SJonghwa Lee 
1715c1155c64SJonghwa Lee 	if (cm_timer_set) {
1716c1155c64SJonghwa Lee 		ktime_t remain;
1717c1155c64SJonghwa Lee 
1718c1155c64SJonghwa Lee 		alarm_cancel(cm_timer);
1719c1155c64SJonghwa Lee 		cm_timer_set = false;
1720c1155c64SJonghwa Lee 		remain = alarm_expires_remaining(cm_timer);
1721c1155c64SJonghwa Lee 		cm_suspend_duration_ms -= ktime_to_ms(remain);
1722c1155c64SJonghwa Lee 		schedule_work(&setup_polling);
17233bb3dbbdSDonggeun Kim 	}
17243bb3dbbdSDonggeun Kim 
1725c1155c64SJonghwa Lee 	_cm_monitor(cm);
1726c1155c64SJonghwa Lee 
1727dfeccb12SChanwoo Choi 	device_set_wakeup_capable(cm->dev, false);
17283bb3dbbdSDonggeun Kim }
17293bb3dbbdSDonggeun Kim 
17303bb3dbbdSDonggeun Kim static const struct dev_pm_ops charger_manager_pm = {
17313bb3dbbdSDonggeun Kim 	.prepare	= cm_suspend_prepare,
1732dfeccb12SChanwoo Choi 	.suspend_noirq	= cm_suspend_noirq,
17333bb3dbbdSDonggeun Kim 	.complete	= cm_suspend_complete,
17343bb3dbbdSDonggeun Kim };
17353bb3dbbdSDonggeun Kim 
17363bb3dbbdSDonggeun Kim static struct platform_driver charger_manager_driver = {
17373bb3dbbdSDonggeun Kim 	.driver = {
17383bb3dbbdSDonggeun Kim 		.name = "charger-manager",
17393bb3dbbdSDonggeun Kim 		.pm = &charger_manager_pm,
1740856ee611SJonghwa Lee 		.of_match_table = charger_manager_match,
17413bb3dbbdSDonggeun Kim 	},
17423bb3dbbdSDonggeun Kim 	.probe = charger_manager_probe,
174383bce344SUwe Kleine-König 	.remove = charger_manager_remove,
17443bb3dbbdSDonggeun Kim 	.id_table = charger_manager_id,
17453bb3dbbdSDonggeun Kim };
17463bb3dbbdSDonggeun Kim 
charger_manager_init(void)17473bb3dbbdSDonggeun Kim static int __init charger_manager_init(void)
17483bb3dbbdSDonggeun Kim {
1749d829dc75SChanwoo Choi 	cm_wq = create_freezable_workqueue("charger_manager");
175075cf4f5aSKangjie Lu 	if (unlikely(!cm_wq))
175175cf4f5aSKangjie Lu 		return -ENOMEM;
175275cf4f5aSKangjie Lu 
1753d829dc75SChanwoo Choi 	INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1754d829dc75SChanwoo Choi 
17553bb3dbbdSDonggeun Kim 	return platform_driver_register(&charger_manager_driver);
17563bb3dbbdSDonggeun Kim }
17573bb3dbbdSDonggeun Kim late_initcall(charger_manager_init);
17583bb3dbbdSDonggeun Kim 
charger_manager_cleanup(void)17593bb3dbbdSDonggeun Kim static void __exit charger_manager_cleanup(void)
17603bb3dbbdSDonggeun Kim {
1761d829dc75SChanwoo Choi 	destroy_workqueue(cm_wq);
1762d829dc75SChanwoo Choi 	cm_wq = NULL;
1763d829dc75SChanwoo Choi 
17643bb3dbbdSDonggeun Kim 	platform_driver_unregister(&charger_manager_driver);
17653bb3dbbdSDonggeun Kim }
17663bb3dbbdSDonggeun Kim module_exit(charger_manager_cleanup);
17673bb3dbbdSDonggeun Kim 
17683bb3dbbdSDonggeun Kim MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
17693bb3dbbdSDonggeun Kim MODULE_DESCRIPTION("Charger Manager");
17703bb3dbbdSDonggeun Kim MODULE_LICENSE("GPL");
1771