18c0984e5SSebastian Reichel /*
28c0984e5SSebastian Reichel  * Copyright (C) ST-Ericsson SA 2012
38c0984e5SSebastian Reichel  *
48c0984e5SSebastian Reichel  * Battery temperature driver for AB8500
58c0984e5SSebastian Reichel  *
68c0984e5SSebastian Reichel  * License Terms: GNU General Public License v2
78c0984e5SSebastian Reichel  * Author:
88c0984e5SSebastian Reichel  *	Johan Palsson <johan.palsson@stericsson.com>
98c0984e5SSebastian Reichel  *	Karl Komierowski <karl.komierowski@stericsson.com>
108c0984e5SSebastian Reichel  *	Arun R Murthy <arun.murthy@stericsson.com>
118c0984e5SSebastian Reichel  */
128c0984e5SSebastian Reichel 
138c0984e5SSebastian Reichel #include <linux/init.h>
148c0984e5SSebastian Reichel #include <linux/module.h>
158c0984e5SSebastian Reichel #include <linux/device.h>
168c0984e5SSebastian Reichel #include <linux/interrupt.h>
178c0984e5SSebastian Reichel #include <linux/delay.h>
188c0984e5SSebastian Reichel #include <linux/slab.h>
198c0984e5SSebastian Reichel #include <linux/platform_device.h>
208c0984e5SSebastian Reichel #include <linux/power_supply.h>
218c0984e5SSebastian Reichel #include <linux/completion.h>
228c0984e5SSebastian Reichel #include <linux/workqueue.h>
238c0984e5SSebastian Reichel #include <linux/jiffies.h>
248c0984e5SSebastian Reichel #include <linux/of.h>
258c0984e5SSebastian Reichel #include <linux/mfd/core.h>
268c0984e5SSebastian Reichel #include <linux/mfd/abx500.h>
278c0984e5SSebastian Reichel #include <linux/mfd/abx500/ab8500.h>
288c0984e5SSebastian Reichel #include <linux/mfd/abx500/ab8500-bm.h>
298c0984e5SSebastian Reichel #include <linux/mfd/abx500/ab8500-gpadc.h>
308c0984e5SSebastian Reichel 
318c0984e5SSebastian Reichel #define VTVOUT_V			1800
328c0984e5SSebastian Reichel 
338c0984e5SSebastian Reichel #define BTEMP_THERMAL_LOW_LIMIT		-10
348c0984e5SSebastian Reichel #define BTEMP_THERMAL_MED_LIMIT		0
358c0984e5SSebastian Reichel #define BTEMP_THERMAL_HIGH_LIMIT_52	52
368c0984e5SSebastian Reichel #define BTEMP_THERMAL_HIGH_LIMIT_57	57
378c0984e5SSebastian Reichel #define BTEMP_THERMAL_HIGH_LIMIT_62	62
388c0984e5SSebastian Reichel 
398c0984e5SSebastian Reichel #define BTEMP_BATCTRL_CURR_SRC_7UA	7
408c0984e5SSebastian Reichel #define BTEMP_BATCTRL_CURR_SRC_20UA	20
418c0984e5SSebastian Reichel 
428c0984e5SSebastian Reichel #define BTEMP_BATCTRL_CURR_SRC_16UA	16
438c0984e5SSebastian Reichel #define BTEMP_BATCTRL_CURR_SRC_18UA	18
448c0984e5SSebastian Reichel 
458c0984e5SSebastian Reichel #define BTEMP_BATCTRL_CURR_SRC_60UA	60
468c0984e5SSebastian Reichel #define BTEMP_BATCTRL_CURR_SRC_120UA	120
478c0984e5SSebastian Reichel 
488c0984e5SSebastian Reichel /**
498c0984e5SSebastian Reichel  * struct ab8500_btemp_interrupts - ab8500 interrupts
508c0984e5SSebastian Reichel  * @name:	name of the interrupt
518c0984e5SSebastian Reichel  * @isr		function pointer to the isr
528c0984e5SSebastian Reichel  */
538c0984e5SSebastian Reichel struct ab8500_btemp_interrupts {
548c0984e5SSebastian Reichel 	char *name;
558c0984e5SSebastian Reichel 	irqreturn_t (*isr)(int irq, void *data);
568c0984e5SSebastian Reichel };
578c0984e5SSebastian Reichel 
588c0984e5SSebastian Reichel struct ab8500_btemp_events {
598c0984e5SSebastian Reichel 	bool batt_rem;
608c0984e5SSebastian Reichel 	bool btemp_high;
618c0984e5SSebastian Reichel 	bool btemp_medhigh;
628c0984e5SSebastian Reichel 	bool btemp_lowmed;
638c0984e5SSebastian Reichel 	bool btemp_low;
648c0984e5SSebastian Reichel 	bool ac_conn;
658c0984e5SSebastian Reichel 	bool usb_conn;
668c0984e5SSebastian Reichel };
678c0984e5SSebastian Reichel 
688c0984e5SSebastian Reichel struct ab8500_btemp_ranges {
698c0984e5SSebastian Reichel 	int btemp_high_limit;
708c0984e5SSebastian Reichel 	int btemp_med_limit;
718c0984e5SSebastian Reichel 	int btemp_low_limit;
728c0984e5SSebastian Reichel };
738c0984e5SSebastian Reichel 
748c0984e5SSebastian Reichel /**
758c0984e5SSebastian Reichel  * struct ab8500_btemp - ab8500 BTEMP device information
768c0984e5SSebastian Reichel  * @dev:		Pointer to the structure device
778c0984e5SSebastian Reichel  * @node:		List of AB8500 BTEMPs, hence prepared for reentrance
788c0984e5SSebastian Reichel  * @curr_source:	What current source we use, in uA
79c56ca24aSColin Ian King  * @bat_temp:		Dispatched battery temperature in degree Celsius
80c56ca24aSColin Ian King  * @prev_bat_temp	Last measured battery temperature in degree Celsius
818c0984e5SSebastian Reichel  * @parent:		Pointer to the struct ab8500
828c0984e5SSebastian Reichel  * @gpadc:		Pointer to the struct gpadc
838c0984e5SSebastian Reichel  * @fg:			Pointer to the struct fg
848c0984e5SSebastian Reichel  * @bm:           	Platform specific battery management information
858c0984e5SSebastian Reichel  * @btemp_psy:		Structure for BTEMP specific battery properties
868c0984e5SSebastian Reichel  * @events:		Structure for information about events triggered
878c0984e5SSebastian Reichel  * @btemp_ranges:	Battery temperature range structure
888c0984e5SSebastian Reichel  * @btemp_wq:		Work queue for measuring the temperature periodically
898c0984e5SSebastian Reichel  * @btemp_periodic_work:	Work for measuring the temperature periodically
908c0984e5SSebastian Reichel  * @initialized:	True if battery id read.
918c0984e5SSebastian Reichel  */
928c0984e5SSebastian Reichel struct ab8500_btemp {
938c0984e5SSebastian Reichel 	struct device *dev;
948c0984e5SSebastian Reichel 	struct list_head node;
958c0984e5SSebastian Reichel 	int curr_source;
968c0984e5SSebastian Reichel 	int bat_temp;
978c0984e5SSebastian Reichel 	int prev_bat_temp;
988c0984e5SSebastian Reichel 	struct ab8500 *parent;
998c0984e5SSebastian Reichel 	struct ab8500_gpadc *gpadc;
1008c0984e5SSebastian Reichel 	struct ab8500_fg *fg;
1018c0984e5SSebastian Reichel 	struct abx500_bm_data *bm;
1028c0984e5SSebastian Reichel 	struct power_supply *btemp_psy;
1038c0984e5SSebastian Reichel 	struct ab8500_btemp_events events;
1048c0984e5SSebastian Reichel 	struct ab8500_btemp_ranges btemp_ranges;
1058c0984e5SSebastian Reichel 	struct workqueue_struct *btemp_wq;
1068c0984e5SSebastian Reichel 	struct delayed_work btemp_periodic_work;
1078c0984e5SSebastian Reichel 	bool initialized;
1088c0984e5SSebastian Reichel };
1098c0984e5SSebastian Reichel 
1108c0984e5SSebastian Reichel /* BTEMP power supply properties */
1118c0984e5SSebastian Reichel static enum power_supply_property ab8500_btemp_props[] = {
1128c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
1138c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ONLINE,
1148c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
1158c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
1168c0984e5SSebastian Reichel };
1178c0984e5SSebastian Reichel 
1188c0984e5SSebastian Reichel static LIST_HEAD(ab8500_btemp_list);
1198c0984e5SSebastian Reichel 
1208c0984e5SSebastian Reichel /**
1218c0984e5SSebastian Reichel  * ab8500_btemp_get() - returns a reference to the primary AB8500 BTEMP
1228c0984e5SSebastian Reichel  * (i.e. the first BTEMP in the instance list)
1238c0984e5SSebastian Reichel  */
1248c0984e5SSebastian Reichel struct ab8500_btemp *ab8500_btemp_get(void)
1258c0984e5SSebastian Reichel {
1268c0984e5SSebastian Reichel 	struct ab8500_btemp *btemp;
1278c0984e5SSebastian Reichel 	btemp = list_first_entry(&ab8500_btemp_list, struct ab8500_btemp, node);
1288c0984e5SSebastian Reichel 
1298c0984e5SSebastian Reichel 	return btemp;
1308c0984e5SSebastian Reichel }
1318c0984e5SSebastian Reichel EXPORT_SYMBOL(ab8500_btemp_get);
1328c0984e5SSebastian Reichel 
1338c0984e5SSebastian Reichel /**
1348c0984e5SSebastian Reichel  * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
1358c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
1368c0984e5SSebastian Reichel  * @v_batctrl:	measured batctrl voltage
1378c0984e5SSebastian Reichel  * @inst_curr:	measured instant current
1388c0984e5SSebastian Reichel  *
1398c0984e5SSebastian Reichel  * This function returns the battery resistance that is
1408c0984e5SSebastian Reichel  * derived from the BATCTRL voltage.
1418c0984e5SSebastian Reichel  * Returns value in Ohms.
1428c0984e5SSebastian Reichel  */
1438c0984e5SSebastian Reichel static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
1448c0984e5SSebastian Reichel 	int v_batctrl, int inst_curr)
1458c0984e5SSebastian Reichel {
1468c0984e5SSebastian Reichel 	int rbs;
1478c0984e5SSebastian Reichel 
1488c0984e5SSebastian Reichel 	if (is_ab8500_1p1_or_earlier(di->parent)) {
1498c0984e5SSebastian Reichel 		/*
1508c0984e5SSebastian Reichel 		 * For ABB cut1.0 and 1.1 BAT_CTRL is internally
1518c0984e5SSebastian Reichel 		 * connected to 1.8V through a 450k resistor
1528c0984e5SSebastian Reichel 		 */
1538c0984e5SSebastian Reichel 		return (450000 * (v_batctrl)) / (1800 - v_batctrl);
1548c0984e5SSebastian Reichel 	}
1558c0984e5SSebastian Reichel 
1568c0984e5SSebastian Reichel 	if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL) {
1578c0984e5SSebastian Reichel 		/*
1588c0984e5SSebastian Reichel 		 * If the battery has internal NTC, we use the current
1598c0984e5SSebastian Reichel 		 * source to calculate the resistance.
1608c0984e5SSebastian Reichel 		 */
1618c0984e5SSebastian Reichel 		rbs = (v_batctrl * 1000
1628c0984e5SSebastian Reichel 		       - di->bm->gnd_lift_resistance * inst_curr)
1638c0984e5SSebastian Reichel 		      / di->curr_source;
1648c0984e5SSebastian Reichel 	} else {
1658c0984e5SSebastian Reichel 		/*
1668c0984e5SSebastian Reichel 		 * BAT_CTRL is internally
1678c0984e5SSebastian Reichel 		 * connected to 1.8V through a 80k resistor
1688c0984e5SSebastian Reichel 		 */
1698c0984e5SSebastian Reichel 		rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
1708c0984e5SSebastian Reichel 	}
1718c0984e5SSebastian Reichel 
1728c0984e5SSebastian Reichel 	return rbs;
1738c0984e5SSebastian Reichel }
1748c0984e5SSebastian Reichel 
1758c0984e5SSebastian Reichel /**
1768c0984e5SSebastian Reichel  * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
1778c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
1788c0984e5SSebastian Reichel  *
1798c0984e5SSebastian Reichel  * This function returns the voltage on BATCTRL. Returns value in mV.
1808c0984e5SSebastian Reichel  */
1818c0984e5SSebastian Reichel static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
1828c0984e5SSebastian Reichel {
1838c0984e5SSebastian Reichel 	int vbtemp;
1848c0984e5SSebastian Reichel 	static int prev;
1858c0984e5SSebastian Reichel 
1868c0984e5SSebastian Reichel 	vbtemp = ab8500_gpadc_convert(di->gpadc, BAT_CTRL);
1878c0984e5SSebastian Reichel 	if (vbtemp < 0) {
1888c0984e5SSebastian Reichel 		dev_err(di->dev,
1898c0984e5SSebastian Reichel 			"%s gpadc conversion failed, using previous value",
1908c0984e5SSebastian Reichel 			__func__);
1918c0984e5SSebastian Reichel 		return prev;
1928c0984e5SSebastian Reichel 	}
1938c0984e5SSebastian Reichel 	prev = vbtemp;
1948c0984e5SSebastian Reichel 	return vbtemp;
1958c0984e5SSebastian Reichel }
1968c0984e5SSebastian Reichel 
1978c0984e5SSebastian Reichel /**
1988c0984e5SSebastian Reichel  * ab8500_btemp_curr_source_enable() - enable/disable batctrl current source
1998c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
2008c0984e5SSebastian Reichel  * @enable:	enable or disable the current source
2018c0984e5SSebastian Reichel  *
2028c0984e5SSebastian Reichel  * Enable or disable the current sources for the BatCtrl AD channel
2038c0984e5SSebastian Reichel  */
2048c0984e5SSebastian Reichel static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di,
2058c0984e5SSebastian Reichel 	bool enable)
2068c0984e5SSebastian Reichel {
2078c0984e5SSebastian Reichel 	int curr;
2088c0984e5SSebastian Reichel 	int ret = 0;
2098c0984e5SSebastian Reichel 
2108c0984e5SSebastian Reichel 	/*
2118c0984e5SSebastian Reichel 	 * BATCTRL current sources are included on AB8500 cut2.0
2128c0984e5SSebastian Reichel 	 * and future versions
2138c0984e5SSebastian Reichel 	 */
2148c0984e5SSebastian Reichel 	if (is_ab8500_1p1_or_earlier(di->parent))
2158c0984e5SSebastian Reichel 		return 0;
2168c0984e5SSebastian Reichel 
2178c0984e5SSebastian Reichel 	/* Only do this for batteries with internal NTC */
2188c0984e5SSebastian Reichel 	if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) {
2198c0984e5SSebastian Reichel 
2208c0984e5SSebastian Reichel 		if (is_ab8540(di->parent)) {
2218c0984e5SSebastian Reichel 			if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_60UA)
2228c0984e5SSebastian Reichel 				curr = BAT_CTRL_60U_ENA;
2238c0984e5SSebastian Reichel 			else
2248c0984e5SSebastian Reichel 				curr = BAT_CTRL_120U_ENA;
2258c0984e5SSebastian Reichel 		} else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
2268c0984e5SSebastian Reichel 			if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_16UA)
2278c0984e5SSebastian Reichel 				curr = BAT_CTRL_16U_ENA;
2288c0984e5SSebastian Reichel 			else
2298c0984e5SSebastian Reichel 				curr = BAT_CTRL_18U_ENA;
2308c0984e5SSebastian Reichel 		} else {
2318c0984e5SSebastian Reichel 			if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA)
2328c0984e5SSebastian Reichel 				curr = BAT_CTRL_7U_ENA;
2338c0984e5SSebastian Reichel 			else
2348c0984e5SSebastian Reichel 				curr = BAT_CTRL_20U_ENA;
2358c0984e5SSebastian Reichel 		}
2368c0984e5SSebastian Reichel 
2378c0984e5SSebastian Reichel 		dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source);
2388c0984e5SSebastian Reichel 
2398c0984e5SSebastian Reichel 		ret = abx500_mask_and_set_register_interruptible(di->dev,
2408c0984e5SSebastian Reichel 			AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
2418c0984e5SSebastian Reichel 			FORCE_BAT_CTRL_CMP_HIGH, FORCE_BAT_CTRL_CMP_HIGH);
2428c0984e5SSebastian Reichel 		if (ret) {
2438c0984e5SSebastian Reichel 			dev_err(di->dev, "%s failed setting cmp_force\n",
2448c0984e5SSebastian Reichel 				__func__);
2458c0984e5SSebastian Reichel 			return ret;
2468c0984e5SSebastian Reichel 		}
2478c0984e5SSebastian Reichel 
2488c0984e5SSebastian Reichel 		/*
2498c0984e5SSebastian Reichel 		 * We have to wait one 32kHz cycle before enabling
2508c0984e5SSebastian Reichel 		 * the current source, since ForceBatCtrlCmpHigh needs
2518c0984e5SSebastian Reichel 		 * to be written in a separate cycle
2528c0984e5SSebastian Reichel 		 */
2538c0984e5SSebastian Reichel 		udelay(32);
2548c0984e5SSebastian Reichel 
2558c0984e5SSebastian Reichel 		ret = abx500_set_register_interruptible(di->dev,
2568c0984e5SSebastian Reichel 			AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
2578c0984e5SSebastian Reichel 			FORCE_BAT_CTRL_CMP_HIGH | curr);
2588c0984e5SSebastian Reichel 		if (ret) {
2598c0984e5SSebastian Reichel 			dev_err(di->dev, "%s failed enabling current source\n",
2608c0984e5SSebastian Reichel 				__func__);
2618c0984e5SSebastian Reichel 			goto disable_curr_source;
2628c0984e5SSebastian Reichel 		}
2638c0984e5SSebastian Reichel 	} else if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) {
2648c0984e5SSebastian Reichel 		dev_dbg(di->dev, "Disable BATCTRL curr source\n");
2658c0984e5SSebastian Reichel 
2668c0984e5SSebastian Reichel 		if (is_ab8540(di->parent)) {
2678c0984e5SSebastian Reichel 			/* Write 0 to the curr bits */
2688c0984e5SSebastian Reichel 			ret = abx500_mask_and_set_register_interruptible(
2698c0984e5SSebastian Reichel 				di->dev,
2708c0984e5SSebastian Reichel 				AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
2718c0984e5SSebastian Reichel 				BAT_CTRL_60U_ENA | BAT_CTRL_120U_ENA,
2728c0984e5SSebastian Reichel 				~(BAT_CTRL_60U_ENA | BAT_CTRL_120U_ENA));
2738c0984e5SSebastian Reichel 		} else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
2748c0984e5SSebastian Reichel 			/* Write 0 to the curr bits */
2758c0984e5SSebastian Reichel 			ret = abx500_mask_and_set_register_interruptible(
2768c0984e5SSebastian Reichel 				di->dev,
2778c0984e5SSebastian Reichel 				AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
2788c0984e5SSebastian Reichel 				BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA,
2798c0984e5SSebastian Reichel 				~(BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA));
2808c0984e5SSebastian Reichel 		} else {
2818c0984e5SSebastian Reichel 			/* Write 0 to the curr bits */
2828c0984e5SSebastian Reichel 			ret = abx500_mask_and_set_register_interruptible(
2838c0984e5SSebastian Reichel 				di->dev,
2848c0984e5SSebastian Reichel 				AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
2858c0984e5SSebastian Reichel 				BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
2868c0984e5SSebastian Reichel 				~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
2878c0984e5SSebastian Reichel 		}
2888c0984e5SSebastian Reichel 
2898c0984e5SSebastian Reichel 		if (ret) {
2908c0984e5SSebastian Reichel 			dev_err(di->dev, "%s failed disabling current source\n",
2918c0984e5SSebastian Reichel 				__func__);
2928c0984e5SSebastian Reichel 			goto disable_curr_source;
2938c0984e5SSebastian Reichel 		}
2948c0984e5SSebastian Reichel 
2958c0984e5SSebastian Reichel 		/* Enable Pull-Up and comparator */
2968c0984e5SSebastian Reichel 		ret = abx500_mask_and_set_register_interruptible(di->dev,
2978c0984e5SSebastian Reichel 			AB8500_CHARGER,	AB8500_BAT_CTRL_CURRENT_SOURCE,
2988c0984e5SSebastian Reichel 			BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
2998c0984e5SSebastian Reichel 			BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
3008c0984e5SSebastian Reichel 		if (ret) {
3018c0984e5SSebastian Reichel 			dev_err(di->dev, "%s failed enabling PU and comp\n",
3028c0984e5SSebastian Reichel 				__func__);
3038c0984e5SSebastian Reichel 			goto enable_pu_comp;
3048c0984e5SSebastian Reichel 		}
3058c0984e5SSebastian Reichel 
3068c0984e5SSebastian Reichel 		/*
3078c0984e5SSebastian Reichel 		 * We have to wait one 32kHz cycle before disabling
3088c0984e5SSebastian Reichel 		 * ForceBatCtrlCmpHigh since this needs to be written
3098c0984e5SSebastian Reichel 		 * in a separate cycle
3108c0984e5SSebastian Reichel 		 */
3118c0984e5SSebastian Reichel 		udelay(32);
3128c0984e5SSebastian Reichel 
3138c0984e5SSebastian Reichel 		/* Disable 'force comparator' */
3148c0984e5SSebastian Reichel 		ret = abx500_mask_and_set_register_interruptible(di->dev,
3158c0984e5SSebastian Reichel 			AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
3168c0984e5SSebastian Reichel 			FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
3178c0984e5SSebastian Reichel 		if (ret) {
3188c0984e5SSebastian Reichel 			dev_err(di->dev, "%s failed disabling force comp\n",
3198c0984e5SSebastian Reichel 				__func__);
3208c0984e5SSebastian Reichel 			goto disable_force_comp;
3218c0984e5SSebastian Reichel 		}
3228c0984e5SSebastian Reichel 	}
3238c0984e5SSebastian Reichel 	return ret;
3248c0984e5SSebastian Reichel 
3258c0984e5SSebastian Reichel 	/*
3268c0984e5SSebastian Reichel 	 * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time
3278c0984e5SSebastian Reichel 	 * if we got an error above
3288c0984e5SSebastian Reichel 	 */
3298c0984e5SSebastian Reichel disable_curr_source:
3308c0984e5SSebastian Reichel 	if (is_ab8540(di->parent)) {
3318c0984e5SSebastian Reichel 		/* Write 0 to the curr bits */
3328c0984e5SSebastian Reichel 		ret = abx500_mask_and_set_register_interruptible(di->dev,
3338c0984e5SSebastian Reichel 			AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
3348c0984e5SSebastian Reichel 			BAT_CTRL_60U_ENA | BAT_CTRL_120U_ENA,
3358c0984e5SSebastian Reichel 			~(BAT_CTRL_60U_ENA | BAT_CTRL_120U_ENA));
3368c0984e5SSebastian Reichel 	} else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
3378c0984e5SSebastian Reichel 		/* Write 0 to the curr bits */
3388c0984e5SSebastian Reichel 		ret = abx500_mask_and_set_register_interruptible(di->dev,
3398c0984e5SSebastian Reichel 			AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
3408c0984e5SSebastian Reichel 			BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA,
3418c0984e5SSebastian Reichel 			~(BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA));
3428c0984e5SSebastian Reichel 	} else {
3438c0984e5SSebastian Reichel 		/* Write 0 to the curr bits */
3448c0984e5SSebastian Reichel 		ret = abx500_mask_and_set_register_interruptible(di->dev,
3458c0984e5SSebastian Reichel 			AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
3468c0984e5SSebastian Reichel 			BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
3478c0984e5SSebastian Reichel 			~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
3488c0984e5SSebastian Reichel 	}
3498c0984e5SSebastian Reichel 
3508c0984e5SSebastian Reichel 	if (ret) {
3518c0984e5SSebastian Reichel 		dev_err(di->dev, "%s failed disabling current source\n",
3528c0984e5SSebastian Reichel 			__func__);
3538c0984e5SSebastian Reichel 		return ret;
3548c0984e5SSebastian Reichel 	}
3558c0984e5SSebastian Reichel enable_pu_comp:
3568c0984e5SSebastian Reichel 	/* Enable Pull-Up and comparator */
3578c0984e5SSebastian Reichel 	ret = abx500_mask_and_set_register_interruptible(di->dev,
3588c0984e5SSebastian Reichel 		AB8500_CHARGER,	AB8500_BAT_CTRL_CURRENT_SOURCE,
3598c0984e5SSebastian Reichel 		BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
3608c0984e5SSebastian Reichel 		BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
3618c0984e5SSebastian Reichel 	if (ret) {
3628c0984e5SSebastian Reichel 		dev_err(di->dev, "%s failed enabling PU and comp\n",
3638c0984e5SSebastian Reichel 			__func__);
3648c0984e5SSebastian Reichel 		return ret;
3658c0984e5SSebastian Reichel 	}
3668c0984e5SSebastian Reichel 
3678c0984e5SSebastian Reichel disable_force_comp:
3688c0984e5SSebastian Reichel 	/*
3698c0984e5SSebastian Reichel 	 * We have to wait one 32kHz cycle before disabling
3708c0984e5SSebastian Reichel 	 * ForceBatCtrlCmpHigh since this needs to be written
3718c0984e5SSebastian Reichel 	 * in a separate cycle
3728c0984e5SSebastian Reichel 	 */
3738c0984e5SSebastian Reichel 	udelay(32);
3748c0984e5SSebastian Reichel 
3758c0984e5SSebastian Reichel 	/* Disable 'force comparator' */
3768c0984e5SSebastian Reichel 	ret = abx500_mask_and_set_register_interruptible(di->dev,
3778c0984e5SSebastian Reichel 		AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
3788c0984e5SSebastian Reichel 		FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
3798c0984e5SSebastian Reichel 	if (ret) {
3808c0984e5SSebastian Reichel 		dev_err(di->dev, "%s failed disabling force comp\n",
3818c0984e5SSebastian Reichel 			__func__);
3828c0984e5SSebastian Reichel 		return ret;
3838c0984e5SSebastian Reichel 	}
3848c0984e5SSebastian Reichel 
3858c0984e5SSebastian Reichel 	return ret;
3868c0984e5SSebastian Reichel }
3878c0984e5SSebastian Reichel 
3888c0984e5SSebastian Reichel /**
3898c0984e5SSebastian Reichel  * ab8500_btemp_get_batctrl_res() - get battery resistance
3908c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
3918c0984e5SSebastian Reichel  *
3928c0984e5SSebastian Reichel  * This function returns the battery pack identification resistance.
3938c0984e5SSebastian Reichel  * Returns value in Ohms.
3948c0984e5SSebastian Reichel  */
3958c0984e5SSebastian Reichel static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di)
3968c0984e5SSebastian Reichel {
3978c0984e5SSebastian Reichel 	int ret;
3988c0984e5SSebastian Reichel 	int batctrl = 0;
3998c0984e5SSebastian Reichel 	int res;
4008c0984e5SSebastian Reichel 	int inst_curr;
4018c0984e5SSebastian Reichel 	int i;
4028c0984e5SSebastian Reichel 
4038c0984e5SSebastian Reichel 	/*
4048c0984e5SSebastian Reichel 	 * BATCTRL current sources are included on AB8500 cut2.0
4058c0984e5SSebastian Reichel 	 * and future versions
4068c0984e5SSebastian Reichel 	 */
4078c0984e5SSebastian Reichel 	ret = ab8500_btemp_curr_source_enable(di, true);
4088c0984e5SSebastian Reichel 	if (ret) {
4098c0984e5SSebastian Reichel 		dev_err(di->dev, "%s curr source enabled failed\n", __func__);
4108c0984e5SSebastian Reichel 		return ret;
4118c0984e5SSebastian Reichel 	}
4128c0984e5SSebastian Reichel 
4138c0984e5SSebastian Reichel 	if (!di->fg)
4148c0984e5SSebastian Reichel 		di->fg = ab8500_fg_get();
4158c0984e5SSebastian Reichel 	if (!di->fg) {
4168c0984e5SSebastian Reichel 		dev_err(di->dev, "No fg found\n");
4178c0984e5SSebastian Reichel 		return -EINVAL;
4188c0984e5SSebastian Reichel 	}
4198c0984e5SSebastian Reichel 
4208c0984e5SSebastian Reichel 	ret = ab8500_fg_inst_curr_start(di->fg);
4218c0984e5SSebastian Reichel 
4228c0984e5SSebastian Reichel 	if (ret) {
4238c0984e5SSebastian Reichel 		dev_err(di->dev, "Failed to start current measurement\n");
4248c0984e5SSebastian Reichel 		return ret;
4258c0984e5SSebastian Reichel 	}
4268c0984e5SSebastian Reichel 
4278c0984e5SSebastian Reichel 	do {
4288c0984e5SSebastian Reichel 		msleep(20);
4298c0984e5SSebastian Reichel 	} while (!ab8500_fg_inst_curr_started(di->fg));
4308c0984e5SSebastian Reichel 
4318c0984e5SSebastian Reichel 	i = 0;
4328c0984e5SSebastian Reichel 
4338c0984e5SSebastian Reichel 	do {
4348c0984e5SSebastian Reichel 		batctrl += ab8500_btemp_read_batctrl_voltage(di);
4358c0984e5SSebastian Reichel 		i++;
4368c0984e5SSebastian Reichel 		msleep(20);
4378c0984e5SSebastian Reichel 	} while (!ab8500_fg_inst_curr_done(di->fg));
4388c0984e5SSebastian Reichel 	batctrl /= i;
4398c0984e5SSebastian Reichel 
4408c0984e5SSebastian Reichel 	ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
4418c0984e5SSebastian Reichel 	if (ret) {
4428c0984e5SSebastian Reichel 		dev_err(di->dev, "Failed to finalize current measurement\n");
4438c0984e5SSebastian Reichel 		return ret;
4448c0984e5SSebastian Reichel 	}
4458c0984e5SSebastian Reichel 
4468c0984e5SSebastian Reichel 	res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
4478c0984e5SSebastian Reichel 
4488c0984e5SSebastian Reichel 	ret = ab8500_btemp_curr_source_enable(di, false);
4498c0984e5SSebastian Reichel 	if (ret) {
4508c0984e5SSebastian Reichel 		dev_err(di->dev, "%s curr source disable failed\n", __func__);
4518c0984e5SSebastian Reichel 		return ret;
4528c0984e5SSebastian Reichel 	}
4538c0984e5SSebastian Reichel 
4548c0984e5SSebastian Reichel 	dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
4558c0984e5SSebastian Reichel 		__func__, batctrl, res, inst_curr, i);
4568c0984e5SSebastian Reichel 
4578c0984e5SSebastian Reichel 	return res;
4588c0984e5SSebastian Reichel }
4598c0984e5SSebastian Reichel 
4608c0984e5SSebastian Reichel /**
4618c0984e5SSebastian Reichel  * ab8500_btemp_res_to_temp() - resistance to temperature
4628c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
4638c0984e5SSebastian Reichel  * @tbl:	pointer to the resiatance to temperature table
4648c0984e5SSebastian Reichel  * @tbl_size:	size of the resistance to temperature table
4658c0984e5SSebastian Reichel  * @res:	resistance to calculate the temperature from
4668c0984e5SSebastian Reichel  *
467c56ca24aSColin Ian King  * This function returns the battery temperature in degrees Celsius
4688c0984e5SSebastian Reichel  * based on the NTC resistance.
4698c0984e5SSebastian Reichel  */
4708c0984e5SSebastian Reichel static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di,
4718c0984e5SSebastian Reichel 	const struct abx500_res_to_temp *tbl, int tbl_size, int res)
4728c0984e5SSebastian Reichel {
4738c0984e5SSebastian Reichel 	int i, temp;
4748c0984e5SSebastian Reichel 	/*
4758c0984e5SSebastian Reichel 	 * Calculate the formula for the straight line
4768c0984e5SSebastian Reichel 	 * Simple interpolation if we are within
4778c0984e5SSebastian Reichel 	 * the resistance table limits, extrapolate
4788c0984e5SSebastian Reichel 	 * if resistance is outside the limits.
4798c0984e5SSebastian Reichel 	 */
4808c0984e5SSebastian Reichel 	if (res > tbl[0].resist)
4818c0984e5SSebastian Reichel 		i = 0;
4828c0984e5SSebastian Reichel 	else if (res <= tbl[tbl_size - 1].resist)
4838c0984e5SSebastian Reichel 		i = tbl_size - 2;
4848c0984e5SSebastian Reichel 	else {
4858c0984e5SSebastian Reichel 		i = 0;
4868c0984e5SSebastian Reichel 		while (!(res <= tbl[i].resist &&
4878c0984e5SSebastian Reichel 			res > tbl[i + 1].resist))
4888c0984e5SSebastian Reichel 			i++;
4898c0984e5SSebastian Reichel 	}
4908c0984e5SSebastian Reichel 
4918c0984e5SSebastian Reichel 	temp = tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) *
4928c0984e5SSebastian Reichel 		(res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
4938c0984e5SSebastian Reichel 	return temp;
4948c0984e5SSebastian Reichel }
4958c0984e5SSebastian Reichel 
4968c0984e5SSebastian Reichel /**
4978c0984e5SSebastian Reichel  * ab8500_btemp_measure_temp() - measure battery temperature
4988c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
4998c0984e5SSebastian Reichel  *
5008c0984e5SSebastian Reichel  * Returns battery temperature (on success) else the previous temperature
5018c0984e5SSebastian Reichel  */
5028c0984e5SSebastian Reichel static int ab8500_btemp_measure_temp(struct ab8500_btemp *di)
5038c0984e5SSebastian Reichel {
5048c0984e5SSebastian Reichel 	int temp;
5058c0984e5SSebastian Reichel 	static int prev;
5068c0984e5SSebastian Reichel 	int rbat, rntc, vntc;
5078c0984e5SSebastian Reichel 	u8 id;
5088c0984e5SSebastian Reichel 
5098c0984e5SSebastian Reichel 	id = di->bm->batt_id;
5108c0984e5SSebastian Reichel 
5118c0984e5SSebastian Reichel 	if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
5128c0984e5SSebastian Reichel 			id != BATTERY_UNKNOWN) {
5138c0984e5SSebastian Reichel 
5148c0984e5SSebastian Reichel 		rbat = ab8500_btemp_get_batctrl_res(di);
5158c0984e5SSebastian Reichel 		if (rbat < 0) {
5168c0984e5SSebastian Reichel 			dev_err(di->dev, "%s get batctrl res failed\n",
5178c0984e5SSebastian Reichel 				__func__);
5188c0984e5SSebastian Reichel 			/*
5198c0984e5SSebastian Reichel 			 * Return out-of-range temperature so that
5208c0984e5SSebastian Reichel 			 * charging is stopped
5218c0984e5SSebastian Reichel 			 */
5228c0984e5SSebastian Reichel 			return BTEMP_THERMAL_LOW_LIMIT;
5238c0984e5SSebastian Reichel 		}
5248c0984e5SSebastian Reichel 
5258c0984e5SSebastian Reichel 		temp = ab8500_btemp_res_to_temp(di,
5268c0984e5SSebastian Reichel 			di->bm->bat_type[id].r_to_t_tbl,
5278c0984e5SSebastian Reichel 			di->bm->bat_type[id].n_temp_tbl_elements, rbat);
5288c0984e5SSebastian Reichel 	} else {
5298c0984e5SSebastian Reichel 		vntc = ab8500_gpadc_convert(di->gpadc, BTEMP_BALL);
5308c0984e5SSebastian Reichel 		if (vntc < 0) {
5318c0984e5SSebastian Reichel 			dev_err(di->dev,
5328c0984e5SSebastian Reichel 				"%s gpadc conversion failed,"
5338c0984e5SSebastian Reichel 				" using previous value\n", __func__);
5348c0984e5SSebastian Reichel 			return prev;
5358c0984e5SSebastian Reichel 		}
5368c0984e5SSebastian Reichel 		/*
5378c0984e5SSebastian Reichel 		 * The PCB NTC is sourced from VTVOUT via a 230kOhm
5388c0984e5SSebastian Reichel 		 * resistor.
5398c0984e5SSebastian Reichel 		 */
5408c0984e5SSebastian Reichel 		rntc = 230000 * vntc / (VTVOUT_V - vntc);
5418c0984e5SSebastian Reichel 
5428c0984e5SSebastian Reichel 		temp = ab8500_btemp_res_to_temp(di,
5438c0984e5SSebastian Reichel 			di->bm->bat_type[id].r_to_t_tbl,
5448c0984e5SSebastian Reichel 			di->bm->bat_type[id].n_temp_tbl_elements, rntc);
5458c0984e5SSebastian Reichel 		prev = temp;
5468c0984e5SSebastian Reichel 	}
5478c0984e5SSebastian Reichel 	dev_dbg(di->dev, "Battery temperature is %d\n", temp);
5488c0984e5SSebastian Reichel 	return temp;
5498c0984e5SSebastian Reichel }
5508c0984e5SSebastian Reichel 
5518c0984e5SSebastian Reichel /**
5528c0984e5SSebastian Reichel  * ab8500_btemp_id() - Identify the connected battery
5538c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
5548c0984e5SSebastian Reichel  *
5558c0984e5SSebastian Reichel  * This function will try to identify the battery by reading the ID
5568c0984e5SSebastian Reichel  * resistor. Some brands use a combined ID resistor with a NTC resistor to
5578c0984e5SSebastian Reichel  * both be able to identify and to read the temperature of it.
5588c0984e5SSebastian Reichel  */
5598c0984e5SSebastian Reichel static int ab8500_btemp_id(struct ab8500_btemp *di)
5608c0984e5SSebastian Reichel {
5618c0984e5SSebastian Reichel 	int res;
5628c0984e5SSebastian Reichel 	u8 i;
5638c0984e5SSebastian Reichel 	if (is_ab8540(di->parent))
5648c0984e5SSebastian Reichel 		di->curr_source = BTEMP_BATCTRL_CURR_SRC_60UA;
5658c0984e5SSebastian Reichel 	else if (is_ab9540(di->parent) || is_ab8505(di->parent))
5668c0984e5SSebastian Reichel 		di->curr_source = BTEMP_BATCTRL_CURR_SRC_16UA;
5678c0984e5SSebastian Reichel 	else
5688c0984e5SSebastian Reichel 		di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
5698c0984e5SSebastian Reichel 
5708c0984e5SSebastian Reichel 	di->bm->batt_id = BATTERY_UNKNOWN;
5718c0984e5SSebastian Reichel 
5728c0984e5SSebastian Reichel 	res =  ab8500_btemp_get_batctrl_res(di);
5738c0984e5SSebastian Reichel 	if (res < 0) {
5748c0984e5SSebastian Reichel 		dev_err(di->dev, "%s get batctrl res failed\n", __func__);
5758c0984e5SSebastian Reichel 		return -ENXIO;
5768c0984e5SSebastian Reichel 	}
5778c0984e5SSebastian Reichel 
5788c0984e5SSebastian Reichel 	/* BATTERY_UNKNOWN is defined on position 0, skip it! */
5798c0984e5SSebastian Reichel 	for (i = BATTERY_UNKNOWN + 1; i < di->bm->n_btypes; i++) {
5808c0984e5SSebastian Reichel 		if ((res <= di->bm->bat_type[i].resis_high) &&
5818c0984e5SSebastian Reichel 			(res >= di->bm->bat_type[i].resis_low)) {
5828c0984e5SSebastian Reichel 			dev_dbg(di->dev, "Battery detected on %s"
5838c0984e5SSebastian Reichel 				" low %d < res %d < high: %d"
5848c0984e5SSebastian Reichel 				" index: %d\n",
5858c0984e5SSebastian Reichel 				di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL ?
5868c0984e5SSebastian Reichel 				"BATCTRL" : "BATTEMP",
5878c0984e5SSebastian Reichel 				di->bm->bat_type[i].resis_low, res,
5888c0984e5SSebastian Reichel 				di->bm->bat_type[i].resis_high, i);
5898c0984e5SSebastian Reichel 
5908c0984e5SSebastian Reichel 			di->bm->batt_id = i;
5918c0984e5SSebastian Reichel 			break;
5928c0984e5SSebastian Reichel 		}
5938c0984e5SSebastian Reichel 	}
5948c0984e5SSebastian Reichel 
5958c0984e5SSebastian Reichel 	if (di->bm->batt_id == BATTERY_UNKNOWN) {
5968c0984e5SSebastian Reichel 		dev_warn(di->dev, "Battery identified as unknown"
5978c0984e5SSebastian Reichel 			", resistance %d Ohm\n", res);
5988c0984e5SSebastian Reichel 		return -ENXIO;
5998c0984e5SSebastian Reichel 	}
6008c0984e5SSebastian Reichel 
6018c0984e5SSebastian Reichel 	/*
6028c0984e5SSebastian Reichel 	 * We only have to change current source if the
6038c0984e5SSebastian Reichel 	 * detected type is Type 1.
6048c0984e5SSebastian Reichel 	 */
6058c0984e5SSebastian Reichel 	if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
6068c0984e5SSebastian Reichel 	    di->bm->batt_id == 1) {
6078c0984e5SSebastian Reichel 		if (is_ab8540(di->parent)) {
6088c0984e5SSebastian Reichel 			dev_dbg(di->dev,
6098c0984e5SSebastian Reichel 				"Set BATCTRL current source to 60uA\n");
6108c0984e5SSebastian Reichel 			di->curr_source = BTEMP_BATCTRL_CURR_SRC_60UA;
6118c0984e5SSebastian Reichel 		} else if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
6128c0984e5SSebastian Reichel 			dev_dbg(di->dev,
6138c0984e5SSebastian Reichel 				"Set BATCTRL current source to 16uA\n");
6148c0984e5SSebastian Reichel 			di->curr_source = BTEMP_BATCTRL_CURR_SRC_16UA;
6158c0984e5SSebastian Reichel 		} else {
6168c0984e5SSebastian Reichel 			dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n");
6178c0984e5SSebastian Reichel 			di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA;
6188c0984e5SSebastian Reichel 		}
6198c0984e5SSebastian Reichel 	}
6208c0984e5SSebastian Reichel 
6218c0984e5SSebastian Reichel 	return di->bm->batt_id;
6228c0984e5SSebastian Reichel }
6238c0984e5SSebastian Reichel 
6248c0984e5SSebastian Reichel /**
6258c0984e5SSebastian Reichel  * ab8500_btemp_periodic_work() - Measuring the temperature periodically
6268c0984e5SSebastian Reichel  * @work:	pointer to the work_struct structure
6278c0984e5SSebastian Reichel  *
6288c0984e5SSebastian Reichel  * Work function for measuring the temperature periodically
6298c0984e5SSebastian Reichel  */
6308c0984e5SSebastian Reichel static void ab8500_btemp_periodic_work(struct work_struct *work)
6318c0984e5SSebastian Reichel {
6328c0984e5SSebastian Reichel 	int interval;
6338c0984e5SSebastian Reichel 	int bat_temp;
6348c0984e5SSebastian Reichel 	struct ab8500_btemp *di = container_of(work,
6358c0984e5SSebastian Reichel 		struct ab8500_btemp, btemp_periodic_work.work);
6368c0984e5SSebastian Reichel 
6378c0984e5SSebastian Reichel 	if (!di->initialized) {
6388c0984e5SSebastian Reichel 		/* Identify the battery */
6398c0984e5SSebastian Reichel 		if (ab8500_btemp_id(di) < 0)
6408c0984e5SSebastian Reichel 			dev_warn(di->dev, "failed to identify the battery\n");
6418c0984e5SSebastian Reichel 	}
6428c0984e5SSebastian Reichel 
6438c0984e5SSebastian Reichel 	bat_temp = ab8500_btemp_measure_temp(di);
6448c0984e5SSebastian Reichel 	/*
6458c0984e5SSebastian Reichel 	 * Filter battery temperature.
6468c0984e5SSebastian Reichel 	 * Allow direct updates on temperature only if two samples result in
6478c0984e5SSebastian Reichel 	 * same temperature. Else only allow 1 degree change from previous
6488c0984e5SSebastian Reichel 	 * reported value in the direction of the new measurement.
6498c0984e5SSebastian Reichel 	 */
6508c0984e5SSebastian Reichel 	if ((bat_temp == di->prev_bat_temp) || !di->initialized) {
6518c0984e5SSebastian Reichel 		if ((di->bat_temp != di->prev_bat_temp) || !di->initialized) {
6528c0984e5SSebastian Reichel 			di->initialized = true;
6538c0984e5SSebastian Reichel 			di->bat_temp = bat_temp;
6548c0984e5SSebastian Reichel 			power_supply_changed(di->btemp_psy);
6558c0984e5SSebastian Reichel 		}
6568c0984e5SSebastian Reichel 	} else if (bat_temp < di->prev_bat_temp) {
6578c0984e5SSebastian Reichel 		di->bat_temp--;
6588c0984e5SSebastian Reichel 		power_supply_changed(di->btemp_psy);
6598c0984e5SSebastian Reichel 	} else if (bat_temp > di->prev_bat_temp) {
6608c0984e5SSebastian Reichel 		di->bat_temp++;
6618c0984e5SSebastian Reichel 		power_supply_changed(di->btemp_psy);
6628c0984e5SSebastian Reichel 	}
6638c0984e5SSebastian Reichel 	di->prev_bat_temp = bat_temp;
6648c0984e5SSebastian Reichel 
6658c0984e5SSebastian Reichel 	if (di->events.ac_conn || di->events.usb_conn)
6668c0984e5SSebastian Reichel 		interval = di->bm->temp_interval_chg;
6678c0984e5SSebastian Reichel 	else
6688c0984e5SSebastian Reichel 		interval = di->bm->temp_interval_nochg;
6698c0984e5SSebastian Reichel 
6708c0984e5SSebastian Reichel 	/* Schedule a new measurement */
6718c0984e5SSebastian Reichel 	queue_delayed_work(di->btemp_wq,
6728c0984e5SSebastian Reichel 		&di->btemp_periodic_work,
6738c0984e5SSebastian Reichel 		round_jiffies(interval * HZ));
6748c0984e5SSebastian Reichel }
6758c0984e5SSebastian Reichel 
6768c0984e5SSebastian Reichel /**
6778c0984e5SSebastian Reichel  * ab8500_btemp_batctrlindb_handler() - battery removal detected
6788c0984e5SSebastian Reichel  * @irq:       interrupt number
6798c0984e5SSebastian Reichel  * @_di:       void pointer that has to address of ab8500_btemp
6808c0984e5SSebastian Reichel  *
6818c0984e5SSebastian Reichel  * Returns IRQ status(IRQ_HANDLED)
6828c0984e5SSebastian Reichel  */
6838c0984e5SSebastian Reichel static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
6848c0984e5SSebastian Reichel {
6858c0984e5SSebastian Reichel 	struct ab8500_btemp *di = _di;
6868c0984e5SSebastian Reichel 	dev_err(di->dev, "Battery removal detected!\n");
6878c0984e5SSebastian Reichel 
6888c0984e5SSebastian Reichel 	di->events.batt_rem = true;
6898c0984e5SSebastian Reichel 	power_supply_changed(di->btemp_psy);
6908c0984e5SSebastian Reichel 
6918c0984e5SSebastian Reichel 	return IRQ_HANDLED;
6928c0984e5SSebastian Reichel }
6938c0984e5SSebastian Reichel 
6948c0984e5SSebastian Reichel /**
6958c0984e5SSebastian Reichel  * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
6968c0984e5SSebastian Reichel  * @irq:       interrupt number
6978c0984e5SSebastian Reichel  * @_di:       void pointer that has to address of ab8500_btemp
6988c0984e5SSebastian Reichel  *
6998c0984e5SSebastian Reichel  * Returns IRQ status(IRQ_HANDLED)
7008c0984e5SSebastian Reichel  */
7018c0984e5SSebastian Reichel static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
7028c0984e5SSebastian Reichel {
7038c0984e5SSebastian Reichel 	struct ab8500_btemp *di = _di;
7048c0984e5SSebastian Reichel 
7058c0984e5SSebastian Reichel 	if (is_ab8500_3p3_or_earlier(di->parent)) {
7068c0984e5SSebastian Reichel 		dev_dbg(di->dev, "Ignore false btemp low irq"
7078c0984e5SSebastian Reichel 			" for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
7088c0984e5SSebastian Reichel 	} else {
7098c0984e5SSebastian Reichel 		dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
7108c0984e5SSebastian Reichel 
7118c0984e5SSebastian Reichel 		di->events.btemp_low = true;
7128c0984e5SSebastian Reichel 		di->events.btemp_high = false;
7138c0984e5SSebastian Reichel 		di->events.btemp_medhigh = false;
7148c0984e5SSebastian Reichel 		di->events.btemp_lowmed = false;
7158c0984e5SSebastian Reichel 		power_supply_changed(di->btemp_psy);
7168c0984e5SSebastian Reichel 	}
7178c0984e5SSebastian Reichel 
7188c0984e5SSebastian Reichel 	return IRQ_HANDLED;
7198c0984e5SSebastian Reichel }
7208c0984e5SSebastian Reichel 
7218c0984e5SSebastian Reichel /**
7228c0984e5SSebastian Reichel  * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
7238c0984e5SSebastian Reichel  * @irq:       interrupt number
7248c0984e5SSebastian Reichel  * @_di:       void pointer that has to address of ab8500_btemp
7258c0984e5SSebastian Reichel  *
7268c0984e5SSebastian Reichel  * Returns IRQ status(IRQ_HANDLED)
7278c0984e5SSebastian Reichel  */
7288c0984e5SSebastian Reichel static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
7298c0984e5SSebastian Reichel {
7308c0984e5SSebastian Reichel 	struct ab8500_btemp *di = _di;
7318c0984e5SSebastian Reichel 
7328c0984e5SSebastian Reichel 	dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
7338c0984e5SSebastian Reichel 
7348c0984e5SSebastian Reichel 	di->events.btemp_high = true;
7358c0984e5SSebastian Reichel 	di->events.btemp_medhigh = false;
7368c0984e5SSebastian Reichel 	di->events.btemp_lowmed = false;
7378c0984e5SSebastian Reichel 	di->events.btemp_low = false;
7388c0984e5SSebastian Reichel 	power_supply_changed(di->btemp_psy);
7398c0984e5SSebastian Reichel 
7408c0984e5SSebastian Reichel 	return IRQ_HANDLED;
7418c0984e5SSebastian Reichel }
7428c0984e5SSebastian Reichel 
7438c0984e5SSebastian Reichel /**
7448c0984e5SSebastian Reichel  * ab8500_btemp_lowmed_handler() - battery temp between low and medium
7458c0984e5SSebastian Reichel  * @irq:       interrupt number
7468c0984e5SSebastian Reichel  * @_di:       void pointer that has to address of ab8500_btemp
7478c0984e5SSebastian Reichel  *
7488c0984e5SSebastian Reichel  * Returns IRQ status(IRQ_HANDLED)
7498c0984e5SSebastian Reichel  */
7508c0984e5SSebastian Reichel static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
7518c0984e5SSebastian Reichel {
7528c0984e5SSebastian Reichel 	struct ab8500_btemp *di = _di;
7538c0984e5SSebastian Reichel 
7548c0984e5SSebastian Reichel 	dev_dbg(di->dev, "Battery temperature is between low and medium\n");
7558c0984e5SSebastian Reichel 
7568c0984e5SSebastian Reichel 	di->events.btemp_lowmed = true;
7578c0984e5SSebastian Reichel 	di->events.btemp_medhigh = false;
7588c0984e5SSebastian Reichel 	di->events.btemp_high = false;
7598c0984e5SSebastian Reichel 	di->events.btemp_low = false;
7608c0984e5SSebastian Reichel 	power_supply_changed(di->btemp_psy);
7618c0984e5SSebastian Reichel 
7628c0984e5SSebastian Reichel 	return IRQ_HANDLED;
7638c0984e5SSebastian Reichel }
7648c0984e5SSebastian Reichel 
7658c0984e5SSebastian Reichel /**
7668c0984e5SSebastian Reichel  * ab8500_btemp_medhigh_handler() - battery temp between medium and high
7678c0984e5SSebastian Reichel  * @irq:       interrupt number
7688c0984e5SSebastian Reichel  * @_di:       void pointer that has to address of ab8500_btemp
7698c0984e5SSebastian Reichel  *
7708c0984e5SSebastian Reichel  * Returns IRQ status(IRQ_HANDLED)
7718c0984e5SSebastian Reichel  */
7728c0984e5SSebastian Reichel static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
7738c0984e5SSebastian Reichel {
7748c0984e5SSebastian Reichel 	struct ab8500_btemp *di = _di;
7758c0984e5SSebastian Reichel 
7768c0984e5SSebastian Reichel 	dev_dbg(di->dev, "Battery temperature is between medium and high\n");
7778c0984e5SSebastian Reichel 
7788c0984e5SSebastian Reichel 	di->events.btemp_medhigh = true;
7798c0984e5SSebastian Reichel 	di->events.btemp_lowmed = false;
7808c0984e5SSebastian Reichel 	di->events.btemp_high = false;
7818c0984e5SSebastian Reichel 	di->events.btemp_low = false;
7828c0984e5SSebastian Reichel 	power_supply_changed(di->btemp_psy);
7838c0984e5SSebastian Reichel 
7848c0984e5SSebastian Reichel 	return IRQ_HANDLED;
7858c0984e5SSebastian Reichel }
7868c0984e5SSebastian Reichel 
7878c0984e5SSebastian Reichel /**
7888c0984e5SSebastian Reichel  * ab8500_btemp_periodic() - Periodic temperature measurements
7898c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
7908c0984e5SSebastian Reichel  * @enable:	enable or disable periodic temperature measurements
7918c0984e5SSebastian Reichel  *
7928c0984e5SSebastian Reichel  * Starts of stops periodic temperature measurements. Periodic measurements
7938c0984e5SSebastian Reichel  * should only be done when a charger is connected.
7948c0984e5SSebastian Reichel  */
7958c0984e5SSebastian Reichel static void ab8500_btemp_periodic(struct ab8500_btemp *di,
7968c0984e5SSebastian Reichel 	bool enable)
7978c0984e5SSebastian Reichel {
7988c0984e5SSebastian Reichel 	dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
7998c0984e5SSebastian Reichel 		enable);
8008c0984e5SSebastian Reichel 	/*
8018c0984e5SSebastian Reichel 	 * Make sure a new measurement is done directly by cancelling
8028c0984e5SSebastian Reichel 	 * any pending work
8038c0984e5SSebastian Reichel 	 */
8048c0984e5SSebastian Reichel 	cancel_delayed_work_sync(&di->btemp_periodic_work);
8058c0984e5SSebastian Reichel 
8068c0984e5SSebastian Reichel 	if (enable)
8078c0984e5SSebastian Reichel 		queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
8088c0984e5SSebastian Reichel }
8098c0984e5SSebastian Reichel 
8108c0984e5SSebastian Reichel /**
8118c0984e5SSebastian Reichel  * ab8500_btemp_get_temp() - get battery temperature
8128c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
8138c0984e5SSebastian Reichel  *
8148c0984e5SSebastian Reichel  * Returns battery temperature
8158c0984e5SSebastian Reichel  */
8168c0984e5SSebastian Reichel int ab8500_btemp_get_temp(struct ab8500_btemp *di)
8178c0984e5SSebastian Reichel {
8188c0984e5SSebastian Reichel 	int temp = 0;
8198c0984e5SSebastian Reichel 
8208c0984e5SSebastian Reichel 	/*
8218c0984e5SSebastian Reichel 	 * The BTEMP events are not reliabe on AB8500 cut3.3
8228c0984e5SSebastian Reichel 	 * and prior versions
8238c0984e5SSebastian Reichel 	 */
8248c0984e5SSebastian Reichel 	if (is_ab8500_3p3_or_earlier(di->parent)) {
8258c0984e5SSebastian Reichel 		temp = di->bat_temp * 10;
8268c0984e5SSebastian Reichel 	} else {
8278c0984e5SSebastian Reichel 		if (di->events.btemp_low) {
8288c0984e5SSebastian Reichel 			if (temp > di->btemp_ranges.btemp_low_limit)
8298c0984e5SSebastian Reichel 				temp = di->btemp_ranges.btemp_low_limit * 10;
8308c0984e5SSebastian Reichel 			else
8318c0984e5SSebastian Reichel 				temp = di->bat_temp * 10;
8328c0984e5SSebastian Reichel 		} else if (di->events.btemp_high) {
8338c0984e5SSebastian Reichel 			if (temp < di->btemp_ranges.btemp_high_limit)
8348c0984e5SSebastian Reichel 				temp = di->btemp_ranges.btemp_high_limit * 10;
8358c0984e5SSebastian Reichel 			else
8368c0984e5SSebastian Reichel 				temp = di->bat_temp * 10;
8378c0984e5SSebastian Reichel 		} else if (di->events.btemp_lowmed) {
8388c0984e5SSebastian Reichel 			if (temp > di->btemp_ranges.btemp_med_limit)
8398c0984e5SSebastian Reichel 				temp = di->btemp_ranges.btemp_med_limit * 10;
8408c0984e5SSebastian Reichel 			else
8418c0984e5SSebastian Reichel 				temp = di->bat_temp * 10;
8428c0984e5SSebastian Reichel 		} else if (di->events.btemp_medhigh) {
8438c0984e5SSebastian Reichel 			if (temp < di->btemp_ranges.btemp_med_limit)
8448c0984e5SSebastian Reichel 				temp = di->btemp_ranges.btemp_med_limit * 10;
8458c0984e5SSebastian Reichel 			else
8468c0984e5SSebastian Reichel 				temp = di->bat_temp * 10;
8478c0984e5SSebastian Reichel 		} else
8488c0984e5SSebastian Reichel 			temp = di->bat_temp * 10;
8498c0984e5SSebastian Reichel 	}
8508c0984e5SSebastian Reichel 	return temp;
8518c0984e5SSebastian Reichel }
8528c0984e5SSebastian Reichel EXPORT_SYMBOL(ab8500_btemp_get_temp);
8538c0984e5SSebastian Reichel 
8548c0984e5SSebastian Reichel /**
8558c0984e5SSebastian Reichel  * ab8500_btemp_get_batctrl_temp() - get the temperature
8568c0984e5SSebastian Reichel  * @btemp:      pointer to the btemp structure
8578c0984e5SSebastian Reichel  *
8588c0984e5SSebastian Reichel  * Returns the batctrl temperature in millidegrees
8598c0984e5SSebastian Reichel  */
8608c0984e5SSebastian Reichel int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp)
8618c0984e5SSebastian Reichel {
8628c0984e5SSebastian Reichel 	return btemp->bat_temp * 1000;
8638c0984e5SSebastian Reichel }
8648c0984e5SSebastian Reichel EXPORT_SYMBOL(ab8500_btemp_get_batctrl_temp);
8658c0984e5SSebastian Reichel 
8668c0984e5SSebastian Reichel /**
8678c0984e5SSebastian Reichel  * ab8500_btemp_get_property() - get the btemp properties
8688c0984e5SSebastian Reichel  * @psy:        pointer to the power_supply structure
8698c0984e5SSebastian Reichel  * @psp:        pointer to the power_supply_property structure
8708c0984e5SSebastian Reichel  * @val:        pointer to the power_supply_propval union
8718c0984e5SSebastian Reichel  *
8728c0984e5SSebastian Reichel  * This function gets called when an application tries to get the btemp
8738c0984e5SSebastian Reichel  * properties by reading the sysfs files.
8748c0984e5SSebastian Reichel  * online:	presence of the battery
8758c0984e5SSebastian Reichel  * present:	presence of the battery
8768c0984e5SSebastian Reichel  * technology:	battery technology
8778c0984e5SSebastian Reichel  * temp:	battery temperature
8788c0984e5SSebastian Reichel  * Returns error code in case of failure else 0(on success)
8798c0984e5SSebastian Reichel  */
8808c0984e5SSebastian Reichel static int ab8500_btemp_get_property(struct power_supply *psy,
8818c0984e5SSebastian Reichel 	enum power_supply_property psp,
8828c0984e5SSebastian Reichel 	union power_supply_propval *val)
8838c0984e5SSebastian Reichel {
8848c0984e5SSebastian Reichel 	struct ab8500_btemp *di = power_supply_get_drvdata(psy);
8858c0984e5SSebastian Reichel 
8868c0984e5SSebastian Reichel 	switch (psp) {
8878c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_PRESENT:
8888c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ONLINE:
8898c0984e5SSebastian Reichel 		if (di->events.batt_rem)
8908c0984e5SSebastian Reichel 			val->intval = 0;
8918c0984e5SSebastian Reichel 		else
8928c0984e5SSebastian Reichel 			val->intval = 1;
8938c0984e5SSebastian Reichel 		break;
8948c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TECHNOLOGY:
8958c0984e5SSebastian Reichel 		val->intval = di->bm->bat_type[di->bm->batt_id].name;
8968c0984e5SSebastian Reichel 		break;
8978c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TEMP:
8988c0984e5SSebastian Reichel 		val->intval = ab8500_btemp_get_temp(di);
8998c0984e5SSebastian Reichel 		break;
9008c0984e5SSebastian Reichel 	default:
9018c0984e5SSebastian Reichel 		return -EINVAL;
9028c0984e5SSebastian Reichel 	}
9038c0984e5SSebastian Reichel 	return 0;
9048c0984e5SSebastian Reichel }
9058c0984e5SSebastian Reichel 
9068c0984e5SSebastian Reichel static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
9078c0984e5SSebastian Reichel {
9088c0984e5SSebastian Reichel 	struct power_supply *psy;
9098c0984e5SSebastian Reichel 	struct power_supply *ext = dev_get_drvdata(dev);
9108c0984e5SSebastian Reichel 	const char **supplicants = (const char **)ext->supplied_to;
9118c0984e5SSebastian Reichel 	struct ab8500_btemp *di;
9128c0984e5SSebastian Reichel 	union power_supply_propval ret;
9138c0984e5SSebastian Reichel 	int j;
9148c0984e5SSebastian Reichel 
9158c0984e5SSebastian Reichel 	psy = (struct power_supply *)data;
9168c0984e5SSebastian Reichel 	di = power_supply_get_drvdata(psy);
9178c0984e5SSebastian Reichel 
9188c0984e5SSebastian Reichel 	/*
9198c0984e5SSebastian Reichel 	 * For all psy where the name of your driver
9208c0984e5SSebastian Reichel 	 * appears in any supplied_to
9218c0984e5SSebastian Reichel 	 */
9228c0984e5SSebastian Reichel 	j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
9238c0984e5SSebastian Reichel 	if (j < 0)
9248c0984e5SSebastian Reichel 		return 0;
9258c0984e5SSebastian Reichel 
9268c0984e5SSebastian Reichel 	/* Go through all properties for the psy */
9278c0984e5SSebastian Reichel 	for (j = 0; j < ext->desc->num_properties; j++) {
9288c0984e5SSebastian Reichel 		enum power_supply_property prop;
9298c0984e5SSebastian Reichel 		prop = ext->desc->properties[j];
9308c0984e5SSebastian Reichel 
9318c0984e5SSebastian Reichel 		if (power_supply_get_property(ext, prop, &ret))
9328c0984e5SSebastian Reichel 			continue;
9338c0984e5SSebastian Reichel 
9348c0984e5SSebastian Reichel 		switch (prop) {
9358c0984e5SSebastian Reichel 		case POWER_SUPPLY_PROP_PRESENT:
9368c0984e5SSebastian Reichel 			switch (ext->desc->type) {
9378c0984e5SSebastian Reichel 			case POWER_SUPPLY_TYPE_MAINS:
9388c0984e5SSebastian Reichel 				/* AC disconnected */
9398c0984e5SSebastian Reichel 				if (!ret.intval && di->events.ac_conn) {
9408c0984e5SSebastian Reichel 					di->events.ac_conn = false;
9418c0984e5SSebastian Reichel 				}
9428c0984e5SSebastian Reichel 				/* AC connected */
9438c0984e5SSebastian Reichel 				else if (ret.intval && !di->events.ac_conn) {
9448c0984e5SSebastian Reichel 					di->events.ac_conn = true;
9458c0984e5SSebastian Reichel 					if (!di->events.usb_conn)
9468c0984e5SSebastian Reichel 						ab8500_btemp_periodic(di, true);
9478c0984e5SSebastian Reichel 				}
9488c0984e5SSebastian Reichel 				break;
9498c0984e5SSebastian Reichel 			case POWER_SUPPLY_TYPE_USB:
9508c0984e5SSebastian Reichel 				/* USB disconnected */
9518c0984e5SSebastian Reichel 				if (!ret.intval && di->events.usb_conn) {
9528c0984e5SSebastian Reichel 					di->events.usb_conn = false;
9538c0984e5SSebastian Reichel 				}
9548c0984e5SSebastian Reichel 				/* USB connected */
9558c0984e5SSebastian Reichel 				else if (ret.intval && !di->events.usb_conn) {
9568c0984e5SSebastian Reichel 					di->events.usb_conn = true;
9578c0984e5SSebastian Reichel 					if (!di->events.ac_conn)
9588c0984e5SSebastian Reichel 						ab8500_btemp_periodic(di, true);
9598c0984e5SSebastian Reichel 				}
9608c0984e5SSebastian Reichel 				break;
9618c0984e5SSebastian Reichel 			default:
9628c0984e5SSebastian Reichel 				break;
9638c0984e5SSebastian Reichel 			}
9648c0984e5SSebastian Reichel 			break;
9658c0984e5SSebastian Reichel 		default:
9668c0984e5SSebastian Reichel 			break;
9678c0984e5SSebastian Reichel 		}
9688c0984e5SSebastian Reichel 	}
9698c0984e5SSebastian Reichel 	return 0;
9708c0984e5SSebastian Reichel }
9718c0984e5SSebastian Reichel 
9728c0984e5SSebastian Reichel /**
9738c0984e5SSebastian Reichel  * ab8500_btemp_external_power_changed() - callback for power supply changes
9748c0984e5SSebastian Reichel  * @psy:       pointer to the structure power_supply
9758c0984e5SSebastian Reichel  *
9768c0984e5SSebastian Reichel  * This function is pointing to the function pointer external_power_changed
9778c0984e5SSebastian Reichel  * of the structure power_supply.
9788c0984e5SSebastian Reichel  * This function gets executed when there is a change in the external power
9798c0984e5SSebastian Reichel  * supply to the btemp.
9808c0984e5SSebastian Reichel  */
9818c0984e5SSebastian Reichel static void ab8500_btemp_external_power_changed(struct power_supply *psy)
9828c0984e5SSebastian Reichel {
9838c0984e5SSebastian Reichel 	struct ab8500_btemp *di = power_supply_get_drvdata(psy);
9848c0984e5SSebastian Reichel 
9858c0984e5SSebastian Reichel 	class_for_each_device(power_supply_class, NULL,
9868c0984e5SSebastian Reichel 		di->btemp_psy, ab8500_btemp_get_ext_psy_data);
9878c0984e5SSebastian Reichel }
9888c0984e5SSebastian Reichel 
9898c0984e5SSebastian Reichel /* ab8500 btemp driver interrupts and their respective isr */
9908c0984e5SSebastian Reichel static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
9918c0984e5SSebastian Reichel 	{"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
9928c0984e5SSebastian Reichel 	{"BTEMP_LOW", ab8500_btemp_templow_handler},
9938c0984e5SSebastian Reichel 	{"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
9948c0984e5SSebastian Reichel 	{"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
9958c0984e5SSebastian Reichel 	{"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
9968c0984e5SSebastian Reichel };
9978c0984e5SSebastian Reichel 
9988c0984e5SSebastian Reichel #if defined(CONFIG_PM)
9998c0984e5SSebastian Reichel static int ab8500_btemp_resume(struct platform_device *pdev)
10008c0984e5SSebastian Reichel {
10018c0984e5SSebastian Reichel 	struct ab8500_btemp *di = platform_get_drvdata(pdev);
10028c0984e5SSebastian Reichel 
10038c0984e5SSebastian Reichel 	ab8500_btemp_periodic(di, true);
10048c0984e5SSebastian Reichel 
10058c0984e5SSebastian Reichel 	return 0;
10068c0984e5SSebastian Reichel }
10078c0984e5SSebastian Reichel 
10088c0984e5SSebastian Reichel static int ab8500_btemp_suspend(struct platform_device *pdev,
10098c0984e5SSebastian Reichel 	pm_message_t state)
10108c0984e5SSebastian Reichel {
10118c0984e5SSebastian Reichel 	struct ab8500_btemp *di = platform_get_drvdata(pdev);
10128c0984e5SSebastian Reichel 
10138c0984e5SSebastian Reichel 	ab8500_btemp_periodic(di, false);
10148c0984e5SSebastian Reichel 
10158c0984e5SSebastian Reichel 	return 0;
10168c0984e5SSebastian Reichel }
10178c0984e5SSebastian Reichel #else
10188c0984e5SSebastian Reichel #define ab8500_btemp_suspend      NULL
10198c0984e5SSebastian Reichel #define ab8500_btemp_resume       NULL
10208c0984e5SSebastian Reichel #endif
10218c0984e5SSebastian Reichel 
10228c0984e5SSebastian Reichel static int ab8500_btemp_remove(struct platform_device *pdev)
10238c0984e5SSebastian Reichel {
10248c0984e5SSebastian Reichel 	struct ab8500_btemp *di = platform_get_drvdata(pdev);
10258c0984e5SSebastian Reichel 	int i, irq;
10268c0984e5SSebastian Reichel 
10278c0984e5SSebastian Reichel 	/* Disable interrupts */
10288c0984e5SSebastian Reichel 	for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
10298c0984e5SSebastian Reichel 		irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
10308c0984e5SSebastian Reichel 		free_irq(irq, di);
10318c0984e5SSebastian Reichel 	}
10328c0984e5SSebastian Reichel 
10338c0984e5SSebastian Reichel 	/* Delete the work queue */
10348c0984e5SSebastian Reichel 	destroy_workqueue(di->btemp_wq);
10358c0984e5SSebastian Reichel 
10368c0984e5SSebastian Reichel 	flush_scheduled_work();
10378c0984e5SSebastian Reichel 	power_supply_unregister(di->btemp_psy);
10388c0984e5SSebastian Reichel 
10398c0984e5SSebastian Reichel 	return 0;
10408c0984e5SSebastian Reichel }
10418c0984e5SSebastian Reichel 
10428c0984e5SSebastian Reichel static char *supply_interface[] = {
10438c0984e5SSebastian Reichel 	"ab8500_chargalg",
10448c0984e5SSebastian Reichel 	"ab8500_fg",
10458c0984e5SSebastian Reichel };
10468c0984e5SSebastian Reichel 
10478c0984e5SSebastian Reichel static const struct power_supply_desc ab8500_btemp_desc = {
10488c0984e5SSebastian Reichel 	.name			= "ab8500_btemp",
10498c0984e5SSebastian Reichel 	.type			= POWER_SUPPLY_TYPE_BATTERY,
10508c0984e5SSebastian Reichel 	.properties		= ab8500_btemp_props,
10518c0984e5SSebastian Reichel 	.num_properties		= ARRAY_SIZE(ab8500_btemp_props),
10528c0984e5SSebastian Reichel 	.get_property		= ab8500_btemp_get_property,
10538c0984e5SSebastian Reichel 	.external_power_changed	= ab8500_btemp_external_power_changed,
10548c0984e5SSebastian Reichel };
10558c0984e5SSebastian Reichel 
10568c0984e5SSebastian Reichel static int ab8500_btemp_probe(struct platform_device *pdev)
10578c0984e5SSebastian Reichel {
10588c0984e5SSebastian Reichel 	struct device_node *np = pdev->dev.of_node;
10598c0984e5SSebastian Reichel 	struct abx500_bm_data *plat = pdev->dev.platform_data;
10608c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = {};
10618c0984e5SSebastian Reichel 	struct ab8500_btemp *di;
10628c0984e5SSebastian Reichel 	int irq, i, ret = 0;
10638c0984e5SSebastian Reichel 	u8 val;
10648c0984e5SSebastian Reichel 
10658c0984e5SSebastian Reichel 	di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
10668c0984e5SSebastian Reichel 	if (!di) {
10678c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "%s no mem for ab8500_btemp\n", __func__);
10688c0984e5SSebastian Reichel 		return -ENOMEM;
10698c0984e5SSebastian Reichel 	}
10708c0984e5SSebastian Reichel 
10718c0984e5SSebastian Reichel 	if (!plat) {
10728c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "no battery management data supplied\n");
10738c0984e5SSebastian Reichel 		return -EINVAL;
10748c0984e5SSebastian Reichel 	}
10758c0984e5SSebastian Reichel 	di->bm = plat;
10768c0984e5SSebastian Reichel 
10778c0984e5SSebastian Reichel 	if (np) {
10788c0984e5SSebastian Reichel 		ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
10798c0984e5SSebastian Reichel 		if (ret) {
10808c0984e5SSebastian Reichel 			dev_err(&pdev->dev, "failed to get battery information\n");
10818c0984e5SSebastian Reichel 			return ret;
10828c0984e5SSebastian Reichel 		}
10838c0984e5SSebastian Reichel 	}
10848c0984e5SSebastian Reichel 
10858c0984e5SSebastian Reichel 	/* get parent data */
10868c0984e5SSebastian Reichel 	di->dev = &pdev->dev;
10878c0984e5SSebastian Reichel 	di->parent = dev_get_drvdata(pdev->dev.parent);
10888c0984e5SSebastian Reichel 	di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
10898c0984e5SSebastian Reichel 
10908c0984e5SSebastian Reichel 	di->initialized = false;
10918c0984e5SSebastian Reichel 
10928c0984e5SSebastian Reichel 	psy_cfg.supplied_to = supply_interface;
10938c0984e5SSebastian Reichel 	psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
10948c0984e5SSebastian Reichel 	psy_cfg.drv_data = di;
10958c0984e5SSebastian Reichel 
10968c0984e5SSebastian Reichel 	/* Create a work queue for the btemp */
10978c0984e5SSebastian Reichel 	di->btemp_wq =
1098a8dd5b68SBhaktipriya Shridhar 		alloc_workqueue("ab8500_btemp_wq", WQ_MEM_RECLAIM, 0);
10998c0984e5SSebastian Reichel 	if (di->btemp_wq == NULL) {
11008c0984e5SSebastian Reichel 		dev_err(di->dev, "failed to create work queue\n");
11018c0984e5SSebastian Reichel 		return -ENOMEM;
11028c0984e5SSebastian Reichel 	}
11038c0984e5SSebastian Reichel 
11048c0984e5SSebastian Reichel 	/* Init work for measuring temperature periodically */
11058c0984e5SSebastian Reichel 	INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
11068c0984e5SSebastian Reichel 		ab8500_btemp_periodic_work);
11078c0984e5SSebastian Reichel 
11088c0984e5SSebastian Reichel 	/* Set BTEMP thermal limits. Low and Med are fixed */
11098c0984e5SSebastian Reichel 	di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
11108c0984e5SSebastian Reichel 	di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
11118c0984e5SSebastian Reichel 
11128c0984e5SSebastian Reichel 	ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
11138c0984e5SSebastian Reichel 		AB8500_BTEMP_HIGH_TH, &val);
11148c0984e5SSebastian Reichel 	if (ret < 0) {
11158c0984e5SSebastian Reichel 		dev_err(di->dev, "%s ab8500 read failed\n", __func__);
11168c0984e5SSebastian Reichel 		goto free_btemp_wq;
11178c0984e5SSebastian Reichel 	}
11188c0984e5SSebastian Reichel 	switch (val) {
11198c0984e5SSebastian Reichel 	case BTEMP_HIGH_TH_57_0:
11208c0984e5SSebastian Reichel 	case BTEMP_HIGH_TH_57_1:
11218c0984e5SSebastian Reichel 		di->btemp_ranges.btemp_high_limit =
11228c0984e5SSebastian Reichel 			BTEMP_THERMAL_HIGH_LIMIT_57;
11238c0984e5SSebastian Reichel 		break;
11248c0984e5SSebastian Reichel 	case BTEMP_HIGH_TH_52:
11258c0984e5SSebastian Reichel 		di->btemp_ranges.btemp_high_limit =
11268c0984e5SSebastian Reichel 			BTEMP_THERMAL_HIGH_LIMIT_52;
11278c0984e5SSebastian Reichel 		break;
11288c0984e5SSebastian Reichel 	case BTEMP_HIGH_TH_62:
11298c0984e5SSebastian Reichel 		di->btemp_ranges.btemp_high_limit =
11308c0984e5SSebastian Reichel 			BTEMP_THERMAL_HIGH_LIMIT_62;
11318c0984e5SSebastian Reichel 		break;
11328c0984e5SSebastian Reichel 	}
11338c0984e5SSebastian Reichel 
11348c0984e5SSebastian Reichel 	/* Register BTEMP power supply class */
11358c0984e5SSebastian Reichel 	di->btemp_psy = power_supply_register(di->dev, &ab8500_btemp_desc,
11368c0984e5SSebastian Reichel 					      &psy_cfg);
11378c0984e5SSebastian Reichel 	if (IS_ERR(di->btemp_psy)) {
11388c0984e5SSebastian Reichel 		dev_err(di->dev, "failed to register BTEMP psy\n");
11398c0984e5SSebastian Reichel 		ret = PTR_ERR(di->btemp_psy);
11408c0984e5SSebastian Reichel 		goto free_btemp_wq;
11418c0984e5SSebastian Reichel 	}
11428c0984e5SSebastian Reichel 
11438c0984e5SSebastian Reichel 	/* Register interrupts */
11448c0984e5SSebastian Reichel 	for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
11458c0984e5SSebastian Reichel 		irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
11468c0984e5SSebastian Reichel 		ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr,
11478c0984e5SSebastian Reichel 			IRQF_SHARED | IRQF_NO_SUSPEND,
11488c0984e5SSebastian Reichel 			ab8500_btemp_irq[i].name, di);
11498c0984e5SSebastian Reichel 
11508c0984e5SSebastian Reichel 		if (ret) {
11518c0984e5SSebastian Reichel 			dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
11528c0984e5SSebastian Reichel 				, ab8500_btemp_irq[i].name, irq, ret);
11538c0984e5SSebastian Reichel 			goto free_irq;
11548c0984e5SSebastian Reichel 		}
11558c0984e5SSebastian Reichel 		dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
11568c0984e5SSebastian Reichel 			ab8500_btemp_irq[i].name, irq, ret);
11578c0984e5SSebastian Reichel 	}
11588c0984e5SSebastian Reichel 
11598c0984e5SSebastian Reichel 	platform_set_drvdata(pdev, di);
11608c0984e5SSebastian Reichel 
11618c0984e5SSebastian Reichel 	/* Kick off periodic temperature measurements */
11628c0984e5SSebastian Reichel 	ab8500_btemp_periodic(di, true);
11638c0984e5SSebastian Reichel 	list_add_tail(&di->node, &ab8500_btemp_list);
11648c0984e5SSebastian Reichel 
11658c0984e5SSebastian Reichel 	return ret;
11668c0984e5SSebastian Reichel 
11678c0984e5SSebastian Reichel free_irq:
11688c0984e5SSebastian Reichel 	power_supply_unregister(di->btemp_psy);
11698c0984e5SSebastian Reichel 
11708c0984e5SSebastian Reichel 	/* We also have to free all successfully registered irqs */
11718c0984e5SSebastian Reichel 	for (i = i - 1; i >= 0; i--) {
11728c0984e5SSebastian Reichel 		irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
11738c0984e5SSebastian Reichel 		free_irq(irq, di);
11748c0984e5SSebastian Reichel 	}
11758c0984e5SSebastian Reichel free_btemp_wq:
11768c0984e5SSebastian Reichel 	destroy_workqueue(di->btemp_wq);
11778c0984e5SSebastian Reichel 	return ret;
11788c0984e5SSebastian Reichel }
11798c0984e5SSebastian Reichel 
11808c0984e5SSebastian Reichel static const struct of_device_id ab8500_btemp_match[] = {
11818c0984e5SSebastian Reichel 	{ .compatible = "stericsson,ab8500-btemp", },
11828c0984e5SSebastian Reichel 	{ },
11838c0984e5SSebastian Reichel };
11848c0984e5SSebastian Reichel 
11858c0984e5SSebastian Reichel static struct platform_driver ab8500_btemp_driver = {
11868c0984e5SSebastian Reichel 	.probe = ab8500_btemp_probe,
11878c0984e5SSebastian Reichel 	.remove = ab8500_btemp_remove,
11888c0984e5SSebastian Reichel 	.suspend = ab8500_btemp_suspend,
11898c0984e5SSebastian Reichel 	.resume = ab8500_btemp_resume,
11908c0984e5SSebastian Reichel 	.driver = {
11918c0984e5SSebastian Reichel 		.name = "ab8500-btemp",
11928c0984e5SSebastian Reichel 		.of_match_table = ab8500_btemp_match,
11938c0984e5SSebastian Reichel 	},
11948c0984e5SSebastian Reichel };
11958c0984e5SSebastian Reichel 
11968c0984e5SSebastian Reichel static int __init ab8500_btemp_init(void)
11978c0984e5SSebastian Reichel {
11988c0984e5SSebastian Reichel 	return platform_driver_register(&ab8500_btemp_driver);
11998c0984e5SSebastian Reichel }
12008c0984e5SSebastian Reichel 
12018c0984e5SSebastian Reichel static void __exit ab8500_btemp_exit(void)
12028c0984e5SSebastian Reichel {
12038c0984e5SSebastian Reichel 	platform_driver_unregister(&ab8500_btemp_driver);
12048c0984e5SSebastian Reichel }
12058c0984e5SSebastian Reichel 
12068c0984e5SSebastian Reichel device_initcall(ab8500_btemp_init);
12078c0984e5SSebastian Reichel module_exit(ab8500_btemp_exit);
12088c0984e5SSebastian Reichel 
12098c0984e5SSebastian Reichel MODULE_LICENSE("GPL v2");
12108c0984e5SSebastian Reichel MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
12118c0984e5SSebastian Reichel MODULE_ALIAS("platform:ab8500-btemp");
12128c0984e5SSebastian Reichel MODULE_DESCRIPTION("AB8500 battery temperature driver");
1213