10376148fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28c0984e5SSebastian Reichel /*
38c0984e5SSebastian Reichel  * Copyright (C) ST-Ericsson SA 2012
48c0984e5SSebastian Reichel  *
58c0984e5SSebastian Reichel  * Battery temperature driver for AB8500
68c0984e5SSebastian Reichel  *
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>
161c1f13a0SLinus Walleij #include <linux/component.h>
178c0984e5SSebastian Reichel #include <linux/interrupt.h>
188c0984e5SSebastian Reichel #include <linux/delay.h>
198c0984e5SSebastian Reichel #include <linux/slab.h>
208c0984e5SSebastian Reichel #include <linux/platform_device.h>
218c0984e5SSebastian Reichel #include <linux/power_supply.h>
228c0984e5SSebastian Reichel #include <linux/completion.h>
238c0984e5SSebastian Reichel #include <linux/workqueue.h>
248c0984e5SSebastian Reichel #include <linux/jiffies.h>
258c0984e5SSebastian Reichel #include <linux/of.h>
268c0984e5SSebastian Reichel #include <linux/mfd/core.h>
278c0984e5SSebastian Reichel #include <linux/mfd/abx500.h>
288c0984e5SSebastian Reichel #include <linux/mfd/abx500/ab8500.h>
292b0e7ac0SLinus Walleij #include <linux/thermal.h>
300a8686e3SLinus Walleij #include <linux/iio/consumer.h>
31f8205474SLinus Walleij #include <linux/fixp-arith.h>
328c0984e5SSebastian Reichel 
33417c0fc2SLinus Walleij #include "ab8500-bm.h"
34417c0fc2SLinus Walleij 
358c0984e5SSebastian Reichel #define BTEMP_THERMAL_LOW_LIMIT		-10
368c0984e5SSebastian Reichel #define BTEMP_THERMAL_MED_LIMIT		0
378c0984e5SSebastian Reichel #define BTEMP_THERMAL_HIGH_LIMIT_52	52
388c0984e5SSebastian Reichel #define BTEMP_THERMAL_HIGH_LIMIT_57	57
398c0984e5SSebastian Reichel #define BTEMP_THERMAL_HIGH_LIMIT_62	62
408c0984e5SSebastian Reichel 
418c0984e5SSebastian Reichel #define BTEMP_BATCTRL_CURR_SRC_7UA	7
428c0984e5SSebastian Reichel #define BTEMP_BATCTRL_CURR_SRC_20UA	20
438c0984e5SSebastian Reichel 
448c0984e5SSebastian Reichel #define BTEMP_BATCTRL_CURR_SRC_16UA	16
458c0984e5SSebastian Reichel #define BTEMP_BATCTRL_CURR_SRC_18UA	18
468c0984e5SSebastian Reichel 
478c0984e5SSebastian Reichel #define BTEMP_BATCTRL_CURR_SRC_60UA	60
488c0984e5SSebastian Reichel #define BTEMP_BATCTRL_CURR_SRC_120UA	120
498c0984e5SSebastian Reichel 
508c0984e5SSebastian Reichel /**
518c0984e5SSebastian Reichel  * struct ab8500_btemp_interrupts - ab8500 interrupts
528c0984e5SSebastian Reichel  * @name:	name of the interrupt
538c0984e5SSebastian Reichel  * @isr		function pointer to the isr
548c0984e5SSebastian Reichel  */
558c0984e5SSebastian Reichel struct ab8500_btemp_interrupts {
568c0984e5SSebastian Reichel 	char *name;
578c0984e5SSebastian Reichel 	irqreturn_t (*isr)(int irq, void *data);
588c0984e5SSebastian Reichel };
598c0984e5SSebastian Reichel 
608c0984e5SSebastian Reichel struct ab8500_btemp_events {
618c0984e5SSebastian Reichel 	bool batt_rem;
628c0984e5SSebastian Reichel 	bool btemp_high;
638c0984e5SSebastian Reichel 	bool btemp_medhigh;
648c0984e5SSebastian Reichel 	bool btemp_lowmed;
658c0984e5SSebastian Reichel 	bool btemp_low;
668c0984e5SSebastian Reichel 	bool ac_conn;
678c0984e5SSebastian Reichel 	bool usb_conn;
688c0984e5SSebastian Reichel };
698c0984e5SSebastian Reichel 
708c0984e5SSebastian Reichel struct ab8500_btemp_ranges {
718c0984e5SSebastian Reichel 	int btemp_high_limit;
728c0984e5SSebastian Reichel 	int btemp_med_limit;
738c0984e5SSebastian Reichel 	int btemp_low_limit;
748c0984e5SSebastian Reichel };
758c0984e5SSebastian Reichel 
768c0984e5SSebastian Reichel /**
778c0984e5SSebastian Reichel  * struct ab8500_btemp - ab8500 BTEMP device information
788c0984e5SSebastian Reichel  * @dev:		Pointer to the structure device
798c0984e5SSebastian Reichel  * @node:		List of AB8500 BTEMPs, hence prepared for reentrance
808c0984e5SSebastian Reichel  * @curr_source:	What current source we use, in uA
81c56ca24aSColin Ian King  * @bat_temp:		Dispatched battery temperature in degree Celsius
82c56ca24aSColin Ian King  * @prev_bat_temp	Last measured battery temperature in degree Celsius
838c0984e5SSebastian Reichel  * @parent:		Pointer to the struct ab8500
842b0e7ac0SLinus Walleij  * @tz:			Thermal zone for the battery
850a8686e3SLinus Walleij  * @adc_bat_ctrl:	ADC channel for the battery control
868c0984e5SSebastian Reichel  * @fg:			Pointer to the struct fg
878c0984e5SSebastian Reichel  * @bm:           	Platform specific battery management information
888c0984e5SSebastian Reichel  * @btemp_psy:		Structure for BTEMP specific battery properties
898c0984e5SSebastian Reichel  * @events:		Structure for information about events triggered
908c0984e5SSebastian Reichel  * @btemp_ranges:	Battery temperature range structure
918c0984e5SSebastian Reichel  * @btemp_wq:		Work queue for measuring the temperature periodically
928c0984e5SSebastian Reichel  * @btemp_periodic_work:	Work for measuring the temperature periodically
938c0984e5SSebastian Reichel  * @initialized:	True if battery id read.
948c0984e5SSebastian Reichel  */
958c0984e5SSebastian Reichel struct ab8500_btemp {
968c0984e5SSebastian Reichel 	struct device *dev;
978c0984e5SSebastian Reichel 	struct list_head node;
988c0984e5SSebastian Reichel 	int curr_source;
998c0984e5SSebastian Reichel 	int bat_temp;
1008c0984e5SSebastian Reichel 	int prev_bat_temp;
1018c0984e5SSebastian Reichel 	struct ab8500 *parent;
1022b0e7ac0SLinus Walleij 	struct thermal_zone_device *tz;
1030a8686e3SLinus Walleij 	struct iio_channel *bat_ctrl;
1048c0984e5SSebastian Reichel 	struct ab8500_fg *fg;
105484a9cc3SLinus Walleij 	struct ab8500_bm_data *bm;
1068c0984e5SSebastian Reichel 	struct power_supply *btemp_psy;
1078c0984e5SSebastian Reichel 	struct ab8500_btemp_events events;
1088c0984e5SSebastian Reichel 	struct ab8500_btemp_ranges btemp_ranges;
1098c0984e5SSebastian Reichel 	struct workqueue_struct *btemp_wq;
1108c0984e5SSebastian Reichel 	struct delayed_work btemp_periodic_work;
1118c0984e5SSebastian Reichel 	bool initialized;
1128c0984e5SSebastian Reichel };
1138c0984e5SSebastian Reichel 
1148c0984e5SSebastian Reichel /* BTEMP power supply properties */
1158c0984e5SSebastian Reichel static enum power_supply_property ab8500_btemp_props[] = {
1168c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
1178c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ONLINE,
1188c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
1198c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
1208c0984e5SSebastian Reichel };
1218c0984e5SSebastian Reichel 
1228c0984e5SSebastian Reichel static LIST_HEAD(ab8500_btemp_list);
1238c0984e5SSebastian Reichel 
1248c0984e5SSebastian Reichel /**
1258c0984e5SSebastian Reichel  * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
1268c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
1278c0984e5SSebastian Reichel  * @v_batctrl:	measured batctrl voltage
1288c0984e5SSebastian Reichel  * @inst_curr:	measured instant current
1298c0984e5SSebastian Reichel  *
1308c0984e5SSebastian Reichel  * This function returns the battery resistance that is
1318c0984e5SSebastian Reichel  * derived from the BATCTRL voltage.
1328c0984e5SSebastian Reichel  * Returns value in Ohms.
1338c0984e5SSebastian Reichel  */
1348c0984e5SSebastian Reichel static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
1358c0984e5SSebastian Reichel 	int v_batctrl, int inst_curr)
1368c0984e5SSebastian Reichel {
1378c0984e5SSebastian Reichel 	if (is_ab8500_1p1_or_earlier(di->parent)) {
1388c0984e5SSebastian Reichel 		/*
1398c0984e5SSebastian Reichel 		 * For ABB cut1.0 and 1.1 BAT_CTRL is internally
1408c0984e5SSebastian Reichel 		 * connected to 1.8V through a 450k resistor
1418c0984e5SSebastian Reichel 		 */
1428c0984e5SSebastian Reichel 		return (450000 * (v_batctrl)) / (1800 - v_batctrl);
1438c0984e5SSebastian Reichel 	}
1448c0984e5SSebastian Reichel 
1458c0984e5SSebastian Reichel 	/*
1468c0984e5SSebastian Reichel 	 * BAT_CTRL is internally
1478c0984e5SSebastian Reichel 	 * connected to 1.8V through a 80k resistor
1488c0984e5SSebastian Reichel 	 */
149d58964beSLinus Walleij 	return (80000 * (v_batctrl)) / (1800 - v_batctrl);
1508c0984e5SSebastian Reichel }
1518c0984e5SSebastian Reichel 
1528c0984e5SSebastian Reichel /**
1538c0984e5SSebastian Reichel  * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
1548c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
1558c0984e5SSebastian Reichel  *
1568c0984e5SSebastian Reichel  * This function returns the voltage on BATCTRL. Returns value in mV.
1578c0984e5SSebastian Reichel  */
1588c0984e5SSebastian Reichel static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
1598c0984e5SSebastian Reichel {
1600a8686e3SLinus Walleij 	int vbtemp, ret;
1618c0984e5SSebastian Reichel 	static int prev;
1628c0984e5SSebastian Reichel 
1630a8686e3SLinus Walleij 	ret = iio_read_channel_processed(di->bat_ctrl, &vbtemp);
1640a8686e3SLinus Walleij 	if (ret < 0) {
1658c0984e5SSebastian Reichel 		dev_err(di->dev,
1660a8686e3SLinus Walleij 			"%s ADC conversion failed, using previous value",
1678c0984e5SSebastian Reichel 			__func__);
1688c0984e5SSebastian Reichel 		return prev;
1698c0984e5SSebastian Reichel 	}
1708c0984e5SSebastian Reichel 	prev = vbtemp;
1718c0984e5SSebastian Reichel 	return vbtemp;
1728c0984e5SSebastian Reichel }
1738c0984e5SSebastian Reichel 
1748c0984e5SSebastian Reichel /**
1758c0984e5SSebastian Reichel  * ab8500_btemp_get_batctrl_res() - get battery resistance
1768c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
1778c0984e5SSebastian Reichel  *
1788c0984e5SSebastian Reichel  * This function returns the battery pack identification resistance.
1798c0984e5SSebastian Reichel  * Returns value in Ohms.
1808c0984e5SSebastian Reichel  */
1818c0984e5SSebastian Reichel static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di)
1828c0984e5SSebastian Reichel {
1838c0984e5SSebastian Reichel 	int ret;
1848c0984e5SSebastian Reichel 	int batctrl = 0;
1858c0984e5SSebastian Reichel 	int res;
1868c0984e5SSebastian Reichel 	int inst_curr;
1878c0984e5SSebastian Reichel 	int i;
1888c0984e5SSebastian Reichel 
1898c0984e5SSebastian Reichel 	if (!di->fg)
1908c0984e5SSebastian Reichel 		di->fg = ab8500_fg_get();
1918c0984e5SSebastian Reichel 	if (!di->fg) {
1928c0984e5SSebastian Reichel 		dev_err(di->dev, "No fg found\n");
1938c0984e5SSebastian Reichel 		return -EINVAL;
1948c0984e5SSebastian Reichel 	}
1958c0984e5SSebastian Reichel 
1968c0984e5SSebastian Reichel 	ret = ab8500_fg_inst_curr_start(di->fg);
1978c0984e5SSebastian Reichel 
1988c0984e5SSebastian Reichel 	if (ret) {
1998c0984e5SSebastian Reichel 		dev_err(di->dev, "Failed to start current measurement\n");
2008c0984e5SSebastian Reichel 		return ret;
2018c0984e5SSebastian Reichel 	}
2028c0984e5SSebastian Reichel 
2038c0984e5SSebastian Reichel 	do {
2048c0984e5SSebastian Reichel 		msleep(20);
2058c0984e5SSebastian Reichel 	} while (!ab8500_fg_inst_curr_started(di->fg));
2068c0984e5SSebastian Reichel 
2078c0984e5SSebastian Reichel 	i = 0;
2088c0984e5SSebastian Reichel 
2098c0984e5SSebastian Reichel 	do {
2108c0984e5SSebastian Reichel 		batctrl += ab8500_btemp_read_batctrl_voltage(di);
2118c0984e5SSebastian Reichel 		i++;
2128c0984e5SSebastian Reichel 		msleep(20);
2138c0984e5SSebastian Reichel 	} while (!ab8500_fg_inst_curr_done(di->fg));
2148c0984e5SSebastian Reichel 	batctrl /= i;
2158c0984e5SSebastian Reichel 
2168c0984e5SSebastian Reichel 	ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
2178c0984e5SSebastian Reichel 	if (ret) {
2188c0984e5SSebastian Reichel 		dev_err(di->dev, "Failed to finalize current measurement\n");
2198c0984e5SSebastian Reichel 		return ret;
2208c0984e5SSebastian Reichel 	}
2218c0984e5SSebastian Reichel 
2228c0984e5SSebastian Reichel 	res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
2238c0984e5SSebastian Reichel 
2248c0984e5SSebastian Reichel 	dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
2258c0984e5SSebastian Reichel 		__func__, batctrl, res, inst_curr, i);
2268c0984e5SSebastian Reichel 
2278c0984e5SSebastian Reichel 	return res;
2288c0984e5SSebastian Reichel }
2298c0984e5SSebastian Reichel 
2308c0984e5SSebastian Reichel /**
2318c0984e5SSebastian Reichel  * ab8500_btemp_id() - Identify the connected battery
2328c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
2338c0984e5SSebastian Reichel  *
2348c0984e5SSebastian Reichel  * This function will try to identify the battery by reading the ID
2358c0984e5SSebastian Reichel  * resistor. Some brands use a combined ID resistor with a NTC resistor to
2368c0984e5SSebastian Reichel  * both be able to identify and to read the temperature of it.
2378c0984e5SSebastian Reichel  */
2388c0984e5SSebastian Reichel static int ab8500_btemp_id(struct ab8500_btemp *di)
2398c0984e5SSebastian Reichel {
2401f918e0fSLinus Walleij 	struct power_supply_battery_info *bi = di->bm->bi;
2418c0984e5SSebastian Reichel 	int res;
2428c0984e5SSebastian Reichel 
2434c4268dcSLinus Walleij 	di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
2448c0984e5SSebastian Reichel 
2458c0984e5SSebastian Reichel 	res =  ab8500_btemp_get_batctrl_res(di);
2468c0984e5SSebastian Reichel 	if (res < 0) {
2478c0984e5SSebastian Reichel 		dev_err(di->dev, "%s get batctrl res failed\n", __func__);
2488c0984e5SSebastian Reichel 		return -ENXIO;
2498c0984e5SSebastian Reichel 	}
2508c0984e5SSebastian Reichel 
2511f918e0fSLinus Walleij 	if (power_supply_battery_bti_in_range(bi, res)) {
2521f918e0fSLinus Walleij 		dev_info(di->dev, "Battery detected on BATCTRL (pin C3)"
2531f918e0fSLinus Walleij 			 " resistance %d Ohm = %d Ohm +/- %d%%\n",
2541f918e0fSLinus Walleij 			 res, bi->bti_resistance_ohm,
2551f918e0fSLinus Walleij 			 bi->bti_resistance_tolerance);
256e5dff305SLinus Walleij 	} else {
2578c0984e5SSebastian Reichel 		dev_warn(di->dev, "Battery identified as unknown"
2588c0984e5SSebastian Reichel 			 ", resistance %d Ohm\n", res);
2598c0984e5SSebastian Reichel 		return -ENXIO;
2608c0984e5SSebastian Reichel 	}
2618c0984e5SSebastian Reichel 
262e5dff305SLinus Walleij 	return 0;
2638c0984e5SSebastian Reichel }
2648c0984e5SSebastian Reichel 
2658c0984e5SSebastian Reichel /**
2668c0984e5SSebastian Reichel  * ab8500_btemp_periodic_work() - Measuring the temperature periodically
2678c0984e5SSebastian Reichel  * @work:	pointer to the work_struct structure
2688c0984e5SSebastian Reichel  *
2698c0984e5SSebastian Reichel  * Work function for measuring the temperature periodically
2708c0984e5SSebastian Reichel  */
2718c0984e5SSebastian Reichel static void ab8500_btemp_periodic_work(struct work_struct *work)
2728c0984e5SSebastian Reichel {
2738c0984e5SSebastian Reichel 	int interval;
2748c0984e5SSebastian Reichel 	int bat_temp;
2758c0984e5SSebastian Reichel 	struct ab8500_btemp *di = container_of(work,
2768c0984e5SSebastian Reichel 		struct ab8500_btemp, btemp_periodic_work.work);
2772b0e7ac0SLinus Walleij 	/* Assume 25 degrees celsius as start temperature */
2782b0e7ac0SLinus Walleij 	static int prev = 25;
2792b0e7ac0SLinus Walleij 	int ret;
2808c0984e5SSebastian Reichel 
2818c0984e5SSebastian Reichel 	if (!di->initialized) {
2828c0984e5SSebastian Reichel 		/* Identify the battery */
2838c0984e5SSebastian Reichel 		if (ab8500_btemp_id(di) < 0)
2848c0984e5SSebastian Reichel 			dev_warn(di->dev, "failed to identify the battery\n");
2858c0984e5SSebastian Reichel 	}
2868c0984e5SSebastian Reichel 
2872b0e7ac0SLinus Walleij 	/* Failover if a reading is erroneous, use last meausurement */
2882b0e7ac0SLinus Walleij 	ret = thermal_zone_get_temp(di->tz, &bat_temp);
2892b0e7ac0SLinus Walleij 	if (ret) {
2902b0e7ac0SLinus Walleij 		dev_err(di->dev, "error reading temperature\n");
2912b0e7ac0SLinus Walleij 		bat_temp = prev;
2922b0e7ac0SLinus Walleij 	} else {
2932b0e7ac0SLinus Walleij 		/* Convert from millicentigrades to centigrades */
2942b0e7ac0SLinus Walleij 		bat_temp /= 1000;
2952b0e7ac0SLinus Walleij 		prev = bat_temp;
2962b0e7ac0SLinus Walleij 	}
2972b0e7ac0SLinus Walleij 
2988c0984e5SSebastian Reichel 	/*
2998c0984e5SSebastian Reichel 	 * Filter battery temperature.
3008c0984e5SSebastian Reichel 	 * Allow direct updates on temperature only if two samples result in
3018c0984e5SSebastian Reichel 	 * same temperature. Else only allow 1 degree change from previous
3028c0984e5SSebastian Reichel 	 * reported value in the direction of the new measurement.
3038c0984e5SSebastian Reichel 	 */
3048c0984e5SSebastian Reichel 	if ((bat_temp == di->prev_bat_temp) || !di->initialized) {
3058c0984e5SSebastian Reichel 		if ((di->bat_temp != di->prev_bat_temp) || !di->initialized) {
3068c0984e5SSebastian Reichel 			di->initialized = true;
3078c0984e5SSebastian Reichel 			di->bat_temp = bat_temp;
3088c0984e5SSebastian Reichel 			power_supply_changed(di->btemp_psy);
3098c0984e5SSebastian Reichel 		}
3108c0984e5SSebastian Reichel 	} else if (bat_temp < di->prev_bat_temp) {
3118c0984e5SSebastian Reichel 		di->bat_temp--;
3128c0984e5SSebastian Reichel 		power_supply_changed(di->btemp_psy);
3138c0984e5SSebastian Reichel 	} else if (bat_temp > di->prev_bat_temp) {
3148c0984e5SSebastian Reichel 		di->bat_temp++;
3158c0984e5SSebastian Reichel 		power_supply_changed(di->btemp_psy);
3168c0984e5SSebastian Reichel 	}
3178c0984e5SSebastian Reichel 	di->prev_bat_temp = bat_temp;
3188c0984e5SSebastian Reichel 
3198c0984e5SSebastian Reichel 	if (di->events.ac_conn || di->events.usb_conn)
3208c0984e5SSebastian Reichel 		interval = di->bm->temp_interval_chg;
3218c0984e5SSebastian Reichel 	else
3228c0984e5SSebastian Reichel 		interval = di->bm->temp_interval_nochg;
3238c0984e5SSebastian Reichel 
3248c0984e5SSebastian Reichel 	/* Schedule a new measurement */
3258c0984e5SSebastian Reichel 	queue_delayed_work(di->btemp_wq,
3268c0984e5SSebastian Reichel 		&di->btemp_periodic_work,
3278c0984e5SSebastian Reichel 		round_jiffies(interval * HZ));
3288c0984e5SSebastian Reichel }
3298c0984e5SSebastian Reichel 
3308c0984e5SSebastian Reichel /**
3318c0984e5SSebastian Reichel  * ab8500_btemp_batctrlindb_handler() - battery removal detected
3328c0984e5SSebastian Reichel  * @irq:       interrupt number
3338c0984e5SSebastian Reichel  * @_di:       void pointer that has to address of ab8500_btemp
3348c0984e5SSebastian Reichel  *
3358c0984e5SSebastian Reichel  * Returns IRQ status(IRQ_HANDLED)
3368c0984e5SSebastian Reichel  */
3378c0984e5SSebastian Reichel static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
3388c0984e5SSebastian Reichel {
3398c0984e5SSebastian Reichel 	struct ab8500_btemp *di = _di;
3408c0984e5SSebastian Reichel 	dev_err(di->dev, "Battery removal detected!\n");
3418c0984e5SSebastian Reichel 
3428c0984e5SSebastian Reichel 	di->events.batt_rem = true;
3438c0984e5SSebastian Reichel 	power_supply_changed(di->btemp_psy);
3448c0984e5SSebastian Reichel 
3458c0984e5SSebastian Reichel 	return IRQ_HANDLED;
3468c0984e5SSebastian Reichel }
3478c0984e5SSebastian Reichel 
3488c0984e5SSebastian Reichel /**
3498c0984e5SSebastian Reichel  * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
3508c0984e5SSebastian Reichel  * @irq:       interrupt number
3518c0984e5SSebastian Reichel  * @_di:       void pointer that has to address of ab8500_btemp
3528c0984e5SSebastian Reichel  *
3538c0984e5SSebastian Reichel  * Returns IRQ status(IRQ_HANDLED)
3548c0984e5SSebastian Reichel  */
3558c0984e5SSebastian Reichel static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
3568c0984e5SSebastian Reichel {
3578c0984e5SSebastian Reichel 	struct ab8500_btemp *di = _di;
3588c0984e5SSebastian Reichel 
3598c0984e5SSebastian Reichel 	if (is_ab8500_3p3_or_earlier(di->parent)) {
3608c0984e5SSebastian Reichel 		dev_dbg(di->dev, "Ignore false btemp low irq"
3618c0984e5SSebastian Reichel 			" for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
3628c0984e5SSebastian Reichel 	} else {
3638c0984e5SSebastian Reichel 		dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
3648c0984e5SSebastian Reichel 
3658c0984e5SSebastian Reichel 		di->events.btemp_low = true;
3668c0984e5SSebastian Reichel 		di->events.btemp_high = false;
3678c0984e5SSebastian Reichel 		di->events.btemp_medhigh = false;
3688c0984e5SSebastian Reichel 		di->events.btemp_lowmed = false;
3698c0984e5SSebastian Reichel 		power_supply_changed(di->btemp_psy);
3708c0984e5SSebastian Reichel 	}
3718c0984e5SSebastian Reichel 
3728c0984e5SSebastian Reichel 	return IRQ_HANDLED;
3738c0984e5SSebastian Reichel }
3748c0984e5SSebastian Reichel 
3758c0984e5SSebastian Reichel /**
3768c0984e5SSebastian Reichel  * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
3778c0984e5SSebastian Reichel  * @irq:       interrupt number
3788c0984e5SSebastian Reichel  * @_di:       void pointer that has to address of ab8500_btemp
3798c0984e5SSebastian Reichel  *
3808c0984e5SSebastian Reichel  * Returns IRQ status(IRQ_HANDLED)
3818c0984e5SSebastian Reichel  */
3828c0984e5SSebastian Reichel static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
3838c0984e5SSebastian Reichel {
3848c0984e5SSebastian Reichel 	struct ab8500_btemp *di = _di;
3858c0984e5SSebastian Reichel 
3868c0984e5SSebastian Reichel 	dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
3878c0984e5SSebastian Reichel 
3888c0984e5SSebastian Reichel 	di->events.btemp_high = true;
3898c0984e5SSebastian Reichel 	di->events.btemp_medhigh = false;
3908c0984e5SSebastian Reichel 	di->events.btemp_lowmed = false;
3918c0984e5SSebastian Reichel 	di->events.btemp_low = false;
3928c0984e5SSebastian Reichel 	power_supply_changed(di->btemp_psy);
3938c0984e5SSebastian Reichel 
3948c0984e5SSebastian Reichel 	return IRQ_HANDLED;
3958c0984e5SSebastian Reichel }
3968c0984e5SSebastian Reichel 
3978c0984e5SSebastian Reichel /**
3988c0984e5SSebastian Reichel  * ab8500_btemp_lowmed_handler() - battery temp between low and medium
3998c0984e5SSebastian Reichel  * @irq:       interrupt number
4008c0984e5SSebastian Reichel  * @_di:       void pointer that has to address of ab8500_btemp
4018c0984e5SSebastian Reichel  *
4028c0984e5SSebastian Reichel  * Returns IRQ status(IRQ_HANDLED)
4038c0984e5SSebastian Reichel  */
4048c0984e5SSebastian Reichel static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
4058c0984e5SSebastian Reichel {
4068c0984e5SSebastian Reichel 	struct ab8500_btemp *di = _di;
4078c0984e5SSebastian Reichel 
4088c0984e5SSebastian Reichel 	dev_dbg(di->dev, "Battery temperature is between low and medium\n");
4098c0984e5SSebastian Reichel 
4108c0984e5SSebastian Reichel 	di->events.btemp_lowmed = true;
4118c0984e5SSebastian Reichel 	di->events.btemp_medhigh = false;
4128c0984e5SSebastian Reichel 	di->events.btemp_high = false;
4138c0984e5SSebastian Reichel 	di->events.btemp_low = false;
4148c0984e5SSebastian Reichel 	power_supply_changed(di->btemp_psy);
4158c0984e5SSebastian Reichel 
4168c0984e5SSebastian Reichel 	return IRQ_HANDLED;
4178c0984e5SSebastian Reichel }
4188c0984e5SSebastian Reichel 
4198c0984e5SSebastian Reichel /**
4208c0984e5SSebastian Reichel  * ab8500_btemp_medhigh_handler() - battery temp between medium and high
4218c0984e5SSebastian Reichel  * @irq:       interrupt number
4228c0984e5SSebastian Reichel  * @_di:       void pointer that has to address of ab8500_btemp
4238c0984e5SSebastian Reichel  *
4248c0984e5SSebastian Reichel  * Returns IRQ status(IRQ_HANDLED)
4258c0984e5SSebastian Reichel  */
4268c0984e5SSebastian Reichel static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
4278c0984e5SSebastian Reichel {
4288c0984e5SSebastian Reichel 	struct ab8500_btemp *di = _di;
4298c0984e5SSebastian Reichel 
4308c0984e5SSebastian Reichel 	dev_dbg(di->dev, "Battery temperature is between medium and high\n");
4318c0984e5SSebastian Reichel 
4328c0984e5SSebastian Reichel 	di->events.btemp_medhigh = true;
4338c0984e5SSebastian Reichel 	di->events.btemp_lowmed = false;
4348c0984e5SSebastian Reichel 	di->events.btemp_high = false;
4358c0984e5SSebastian Reichel 	di->events.btemp_low = false;
4368c0984e5SSebastian Reichel 	power_supply_changed(di->btemp_psy);
4378c0984e5SSebastian Reichel 
4388c0984e5SSebastian Reichel 	return IRQ_HANDLED;
4398c0984e5SSebastian Reichel }
4408c0984e5SSebastian Reichel 
4418c0984e5SSebastian Reichel /**
4428c0984e5SSebastian Reichel  * ab8500_btemp_periodic() - Periodic temperature measurements
4438c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
4448c0984e5SSebastian Reichel  * @enable:	enable or disable periodic temperature measurements
4458c0984e5SSebastian Reichel  *
4468c0984e5SSebastian Reichel  * Starts of stops periodic temperature measurements. Periodic measurements
4478c0984e5SSebastian Reichel  * should only be done when a charger is connected.
4488c0984e5SSebastian Reichel  */
4498c0984e5SSebastian Reichel static void ab8500_btemp_periodic(struct ab8500_btemp *di,
4508c0984e5SSebastian Reichel 	bool enable)
4518c0984e5SSebastian Reichel {
4528c0984e5SSebastian Reichel 	dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
4538c0984e5SSebastian Reichel 		enable);
4548c0984e5SSebastian Reichel 	/*
4558c0984e5SSebastian Reichel 	 * Make sure a new measurement is done directly by cancelling
4568c0984e5SSebastian Reichel 	 * any pending work
4578c0984e5SSebastian Reichel 	 */
4588c0984e5SSebastian Reichel 	cancel_delayed_work_sync(&di->btemp_periodic_work);
4598c0984e5SSebastian Reichel 
4608c0984e5SSebastian Reichel 	if (enable)
4618c0984e5SSebastian Reichel 		queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
4628c0984e5SSebastian Reichel }
4638c0984e5SSebastian Reichel 
4648c0984e5SSebastian Reichel /**
4658c0984e5SSebastian Reichel  * ab8500_btemp_get_temp() - get battery temperature
4668c0984e5SSebastian Reichel  * @di:		pointer to the ab8500_btemp structure
4678c0984e5SSebastian Reichel  *
4688c0984e5SSebastian Reichel  * Returns battery temperature
4698c0984e5SSebastian Reichel  */
470417c0fc2SLinus Walleij static int ab8500_btemp_get_temp(struct ab8500_btemp *di)
4718c0984e5SSebastian Reichel {
4728c0984e5SSebastian Reichel 	int temp = 0;
4738c0984e5SSebastian Reichel 
4748c0984e5SSebastian Reichel 	/*
4758c0984e5SSebastian Reichel 	 * The BTEMP events are not reliabe on AB8500 cut3.3
4768c0984e5SSebastian Reichel 	 * and prior versions
4778c0984e5SSebastian Reichel 	 */
4788c0984e5SSebastian Reichel 	if (is_ab8500_3p3_or_earlier(di->parent)) {
4798c0984e5SSebastian Reichel 		temp = di->bat_temp * 10;
4808c0984e5SSebastian Reichel 	} else {
4818c0984e5SSebastian Reichel 		if (di->events.btemp_low) {
4828c0984e5SSebastian Reichel 			if (temp > di->btemp_ranges.btemp_low_limit)
4838c0984e5SSebastian Reichel 				temp = di->btemp_ranges.btemp_low_limit * 10;
4848c0984e5SSebastian Reichel 			else
4858c0984e5SSebastian Reichel 				temp = di->bat_temp * 10;
4868c0984e5SSebastian Reichel 		} else if (di->events.btemp_high) {
4878c0984e5SSebastian Reichel 			if (temp < di->btemp_ranges.btemp_high_limit)
4888c0984e5SSebastian Reichel 				temp = di->btemp_ranges.btemp_high_limit * 10;
4898c0984e5SSebastian Reichel 			else
4908c0984e5SSebastian Reichel 				temp = di->bat_temp * 10;
4918c0984e5SSebastian Reichel 		} else if (di->events.btemp_lowmed) {
4928c0984e5SSebastian Reichel 			if (temp > di->btemp_ranges.btemp_med_limit)
4938c0984e5SSebastian Reichel 				temp = di->btemp_ranges.btemp_med_limit * 10;
4948c0984e5SSebastian Reichel 			else
4958c0984e5SSebastian Reichel 				temp = di->bat_temp * 10;
4968c0984e5SSebastian Reichel 		} else if (di->events.btemp_medhigh) {
4978c0984e5SSebastian Reichel 			if (temp < di->btemp_ranges.btemp_med_limit)
4988c0984e5SSebastian Reichel 				temp = di->btemp_ranges.btemp_med_limit * 10;
4998c0984e5SSebastian Reichel 			else
5008c0984e5SSebastian Reichel 				temp = di->bat_temp * 10;
5018c0984e5SSebastian Reichel 		} else
5028c0984e5SSebastian Reichel 			temp = di->bat_temp * 10;
5038c0984e5SSebastian Reichel 	}
5048c0984e5SSebastian Reichel 	return temp;
5058c0984e5SSebastian Reichel }
5068c0984e5SSebastian Reichel 
5078c0984e5SSebastian Reichel /**
5088c0984e5SSebastian Reichel  * ab8500_btemp_get_property() - get the btemp properties
5098c0984e5SSebastian Reichel  * @psy:        pointer to the power_supply structure
5108c0984e5SSebastian Reichel  * @psp:        pointer to the power_supply_property structure
5118c0984e5SSebastian Reichel  * @val:        pointer to the power_supply_propval union
5128c0984e5SSebastian Reichel  *
5138c0984e5SSebastian Reichel  * This function gets called when an application tries to get the btemp
5148c0984e5SSebastian Reichel  * properties by reading the sysfs files.
5158c0984e5SSebastian Reichel  * online:	presence of the battery
5168c0984e5SSebastian Reichel  * present:	presence of the battery
5178c0984e5SSebastian Reichel  * technology:	battery technology
5188c0984e5SSebastian Reichel  * temp:	battery temperature
5198c0984e5SSebastian Reichel  * Returns error code in case of failure else 0(on success)
5208c0984e5SSebastian Reichel  */
5218c0984e5SSebastian Reichel static int ab8500_btemp_get_property(struct power_supply *psy,
5228c0984e5SSebastian Reichel 	enum power_supply_property psp,
5238c0984e5SSebastian Reichel 	union power_supply_propval *val)
5248c0984e5SSebastian Reichel {
5258c0984e5SSebastian Reichel 	struct ab8500_btemp *di = power_supply_get_drvdata(psy);
5268c0984e5SSebastian Reichel 
5278c0984e5SSebastian Reichel 	switch (psp) {
5288c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_PRESENT:
5298c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ONLINE:
5308c0984e5SSebastian Reichel 		if (di->events.batt_rem)
5318c0984e5SSebastian Reichel 			val->intval = 0;
5328c0984e5SSebastian Reichel 		else
5338c0984e5SSebastian Reichel 			val->intval = 1;
5348c0984e5SSebastian Reichel 		break;
5358c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TECHNOLOGY:
53625fd3303SLinus Walleij 		if (di->bm->bi)
53725fd3303SLinus Walleij 			val->intval = di->bm->bi->technology;
53825fd3303SLinus Walleij 		else
53925fd3303SLinus Walleij 			val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
5408c0984e5SSebastian Reichel 		break;
5418c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TEMP:
5428c0984e5SSebastian Reichel 		val->intval = ab8500_btemp_get_temp(di);
5438c0984e5SSebastian Reichel 		break;
5448c0984e5SSebastian Reichel 	default:
5458c0984e5SSebastian Reichel 		return -EINVAL;
5468c0984e5SSebastian Reichel 	}
5478c0984e5SSebastian Reichel 	return 0;
5488c0984e5SSebastian Reichel }
5498c0984e5SSebastian Reichel 
5508c0984e5SSebastian Reichel static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
5518c0984e5SSebastian Reichel {
5528c0984e5SSebastian Reichel 	struct power_supply *psy;
5538c0984e5SSebastian Reichel 	struct power_supply *ext = dev_get_drvdata(dev);
5548c0984e5SSebastian Reichel 	const char **supplicants = (const char **)ext->supplied_to;
5558c0984e5SSebastian Reichel 	struct ab8500_btemp *di;
5568c0984e5SSebastian Reichel 	union power_supply_propval ret;
5578c0984e5SSebastian Reichel 	int j;
5588c0984e5SSebastian Reichel 
5598c0984e5SSebastian Reichel 	psy = (struct power_supply *)data;
5608c0984e5SSebastian Reichel 	di = power_supply_get_drvdata(psy);
5618c0984e5SSebastian Reichel 
5628c0984e5SSebastian Reichel 	/*
5638c0984e5SSebastian Reichel 	 * For all psy where the name of your driver
5648c0984e5SSebastian Reichel 	 * appears in any supplied_to
5658c0984e5SSebastian Reichel 	 */
5668c0984e5SSebastian Reichel 	j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
5678c0984e5SSebastian Reichel 	if (j < 0)
5688c0984e5SSebastian Reichel 		return 0;
5698c0984e5SSebastian Reichel 
5708c0984e5SSebastian Reichel 	/* Go through all properties for the psy */
5718c0984e5SSebastian Reichel 	for (j = 0; j < ext->desc->num_properties; j++) {
5728c0984e5SSebastian Reichel 		enum power_supply_property prop;
5738c0984e5SSebastian Reichel 		prop = ext->desc->properties[j];
5748c0984e5SSebastian Reichel 
5758c0984e5SSebastian Reichel 		if (power_supply_get_property(ext, prop, &ret))
5768c0984e5SSebastian Reichel 			continue;
5778c0984e5SSebastian Reichel 
5788c0984e5SSebastian Reichel 		switch (prop) {
5798c0984e5SSebastian Reichel 		case POWER_SUPPLY_PROP_PRESENT:
5808c0984e5SSebastian Reichel 			switch (ext->desc->type) {
5818c0984e5SSebastian Reichel 			case POWER_SUPPLY_TYPE_MAINS:
5828c0984e5SSebastian Reichel 				/* AC disconnected */
5838c0984e5SSebastian Reichel 				if (!ret.intval && di->events.ac_conn) {
5848c0984e5SSebastian Reichel 					di->events.ac_conn = false;
5858c0984e5SSebastian Reichel 				}
5868c0984e5SSebastian Reichel 				/* AC connected */
5878c0984e5SSebastian Reichel 				else if (ret.intval && !di->events.ac_conn) {
5888c0984e5SSebastian Reichel 					di->events.ac_conn = true;
5898c0984e5SSebastian Reichel 					if (!di->events.usb_conn)
5908c0984e5SSebastian Reichel 						ab8500_btemp_periodic(di, true);
5918c0984e5SSebastian Reichel 				}
5928c0984e5SSebastian Reichel 				break;
5938c0984e5SSebastian Reichel 			case POWER_SUPPLY_TYPE_USB:
5948c0984e5SSebastian Reichel 				/* USB disconnected */
5958c0984e5SSebastian Reichel 				if (!ret.intval && di->events.usb_conn) {
5968c0984e5SSebastian Reichel 					di->events.usb_conn = false;
5978c0984e5SSebastian Reichel 				}
5988c0984e5SSebastian Reichel 				/* USB connected */
5998c0984e5SSebastian Reichel 				else if (ret.intval && !di->events.usb_conn) {
6008c0984e5SSebastian Reichel 					di->events.usb_conn = true;
6018c0984e5SSebastian Reichel 					if (!di->events.ac_conn)
6028c0984e5SSebastian Reichel 						ab8500_btemp_periodic(di, true);
6038c0984e5SSebastian Reichel 				}
6048c0984e5SSebastian Reichel 				break;
6058c0984e5SSebastian Reichel 			default:
6068c0984e5SSebastian Reichel 				break;
6078c0984e5SSebastian Reichel 			}
6088c0984e5SSebastian Reichel 			break;
6098c0984e5SSebastian Reichel 		default:
6108c0984e5SSebastian Reichel 			break;
6118c0984e5SSebastian Reichel 		}
6128c0984e5SSebastian Reichel 	}
6138c0984e5SSebastian Reichel 	return 0;
6148c0984e5SSebastian Reichel }
6158c0984e5SSebastian Reichel 
6168c0984e5SSebastian Reichel /**
6178c0984e5SSebastian Reichel  * ab8500_btemp_external_power_changed() - callback for power supply changes
6188c0984e5SSebastian Reichel  * @psy:       pointer to the structure power_supply
6198c0984e5SSebastian Reichel  *
6208c0984e5SSebastian Reichel  * This function is pointing to the function pointer external_power_changed
6218c0984e5SSebastian Reichel  * of the structure power_supply.
6228c0984e5SSebastian Reichel  * This function gets executed when there is a change in the external power
6238c0984e5SSebastian Reichel  * supply to the btemp.
6248c0984e5SSebastian Reichel  */
6258c0984e5SSebastian Reichel static void ab8500_btemp_external_power_changed(struct power_supply *psy)
6268c0984e5SSebastian Reichel {
6278c0984e5SSebastian Reichel 	struct ab8500_btemp *di = power_supply_get_drvdata(psy);
6288c0984e5SSebastian Reichel 
6298c0984e5SSebastian Reichel 	class_for_each_device(power_supply_class, NULL,
6308c0984e5SSebastian Reichel 		di->btemp_psy, ab8500_btemp_get_ext_psy_data);
6318c0984e5SSebastian Reichel }
6328c0984e5SSebastian Reichel 
6338c0984e5SSebastian Reichel /* ab8500 btemp driver interrupts and their respective isr */
6348c0984e5SSebastian Reichel static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
6358c0984e5SSebastian Reichel 	{"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
6368c0984e5SSebastian Reichel 	{"BTEMP_LOW", ab8500_btemp_templow_handler},
6378c0984e5SSebastian Reichel 	{"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
6388c0984e5SSebastian Reichel 	{"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
6398c0984e5SSebastian Reichel 	{"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
6408c0984e5SSebastian Reichel };
6418c0984e5SSebastian Reichel 
642f8efa0a8SLinus Walleij static int __maybe_unused ab8500_btemp_resume(struct device *dev)
6438c0984e5SSebastian Reichel {
644f8efa0a8SLinus Walleij 	struct ab8500_btemp *di = dev_get_drvdata(dev);
6458c0984e5SSebastian Reichel 
6468c0984e5SSebastian Reichel 	ab8500_btemp_periodic(di, true);
6478c0984e5SSebastian Reichel 
6488c0984e5SSebastian Reichel 	return 0;
6498c0984e5SSebastian Reichel }
6508c0984e5SSebastian Reichel 
651f8efa0a8SLinus Walleij static int __maybe_unused ab8500_btemp_suspend(struct device *dev)
6528c0984e5SSebastian Reichel {
653f8efa0a8SLinus Walleij 	struct ab8500_btemp *di = dev_get_drvdata(dev);
6548c0984e5SSebastian Reichel 
6558c0984e5SSebastian Reichel 	ab8500_btemp_periodic(di, false);
6568c0984e5SSebastian Reichel 
6578c0984e5SSebastian Reichel 	return 0;
6588c0984e5SSebastian Reichel }
6598c0984e5SSebastian Reichel 
6608c0984e5SSebastian Reichel static char *supply_interface[] = {
6618c0984e5SSebastian Reichel 	"ab8500_chargalg",
6628c0984e5SSebastian Reichel 	"ab8500_fg",
6638c0984e5SSebastian Reichel };
6648c0984e5SSebastian Reichel 
6658c0984e5SSebastian Reichel static const struct power_supply_desc ab8500_btemp_desc = {
6668c0984e5SSebastian Reichel 	.name			= "ab8500_btemp",
6678c0984e5SSebastian Reichel 	.type			= POWER_SUPPLY_TYPE_BATTERY,
6688c0984e5SSebastian Reichel 	.properties		= ab8500_btemp_props,
6698c0984e5SSebastian Reichel 	.num_properties		= ARRAY_SIZE(ab8500_btemp_props),
6708c0984e5SSebastian Reichel 	.get_property		= ab8500_btemp_get_property,
6718c0984e5SSebastian Reichel 	.external_power_changed	= ab8500_btemp_external_power_changed,
6728c0984e5SSebastian Reichel };
6738c0984e5SSebastian Reichel 
6741c1f13a0SLinus Walleij static int ab8500_btemp_bind(struct device *dev, struct device *master,
6751c1f13a0SLinus Walleij 			     void *data)
6761c1f13a0SLinus Walleij {
6771c1f13a0SLinus Walleij 	struct ab8500_btemp *di = dev_get_drvdata(dev);
6781c1f13a0SLinus Walleij 
6791c1f13a0SLinus Walleij 	/* Create a work queue for the btemp */
6801c1f13a0SLinus Walleij 	di->btemp_wq =
6811c1f13a0SLinus Walleij 		alloc_workqueue("ab8500_btemp_wq", WQ_MEM_RECLAIM, 0);
6821c1f13a0SLinus Walleij 	if (di->btemp_wq == NULL) {
6831c1f13a0SLinus Walleij 		dev_err(dev, "failed to create work queue\n");
6841c1f13a0SLinus Walleij 		return -ENOMEM;
6851c1f13a0SLinus Walleij 	}
6861c1f13a0SLinus Walleij 
6871c1f13a0SLinus Walleij 	/* Kick off periodic temperature measurements */
6881c1f13a0SLinus Walleij 	ab8500_btemp_periodic(di, true);
6891c1f13a0SLinus Walleij 
6901c1f13a0SLinus Walleij 	return 0;
6911c1f13a0SLinus Walleij }
6921c1f13a0SLinus Walleij 
6931c1f13a0SLinus Walleij static void ab8500_btemp_unbind(struct device *dev, struct device *master,
6941c1f13a0SLinus Walleij 				void *data)
6951c1f13a0SLinus Walleij {
6961c1f13a0SLinus Walleij 	struct ab8500_btemp *di = dev_get_drvdata(dev);
6971c1f13a0SLinus Walleij 
6981c1f13a0SLinus Walleij 	/* Delete the work queue */
6991c1f13a0SLinus Walleij 	destroy_workqueue(di->btemp_wq);
7001c1f13a0SLinus Walleij }
7011c1f13a0SLinus Walleij 
7021c1f13a0SLinus Walleij static const struct component_ops ab8500_btemp_component_ops = {
7031c1f13a0SLinus Walleij 	.bind = ab8500_btemp_bind,
7041c1f13a0SLinus Walleij 	.unbind = ab8500_btemp_unbind,
7051c1f13a0SLinus Walleij };
7061c1f13a0SLinus Walleij 
7078c0984e5SSebastian Reichel static int ab8500_btemp_probe(struct platform_device *pdev)
7088c0984e5SSebastian Reichel {
7098c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = {};
710ad89cb5fSLinus Walleij 	struct device *dev = &pdev->dev;
7118c0984e5SSebastian Reichel 	struct ab8500_btemp *di;
7128c0984e5SSebastian Reichel 	int irq, i, ret = 0;
7138c0984e5SSebastian Reichel 	u8 val;
7148c0984e5SSebastian Reichel 
715ad89cb5fSLinus Walleij 	di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
716ad89cb5fSLinus Walleij 	if (!di)
7178c0984e5SSebastian Reichel 		return -ENOMEM;
7188c0984e5SSebastian Reichel 
719417c0fc2SLinus Walleij 	di->bm = &ab8500_bm_data;
7208c0984e5SSebastian Reichel 
7218c0984e5SSebastian Reichel 	/* get parent data */
722ad89cb5fSLinus Walleij 	di->dev = dev;
7238c0984e5SSebastian Reichel 	di->parent = dev_get_drvdata(pdev->dev.parent);
7240a8686e3SLinus Walleij 
7252b0e7ac0SLinus Walleij 	/* Get thermal zone and ADC */
7262b0e7ac0SLinus Walleij 	di->tz = thermal_zone_get_zone_by_name("battery-thermal");
7272b0e7ac0SLinus Walleij 	if (IS_ERR(di->tz)) {
728*767e6843SLinus Walleij 		ret = PTR_ERR(di->tz);
729*767e6843SLinus Walleij 		/*
730*767e6843SLinus Walleij 		 * This usually just means we are probing before the thermal
731*767e6843SLinus Walleij 		 * zone, so just defer.
732*767e6843SLinus Walleij 		 */
733*767e6843SLinus Walleij 		if (ret == -ENODEV)
734*767e6843SLinus Walleij 			ret = -EPROBE_DEFER;
735*767e6843SLinus Walleij 		return dev_err_probe(dev, ret,
7362b0e7ac0SLinus Walleij 				     "failed to get battery thermal zone\n");
7370a8686e3SLinus Walleij 	}
738ad89cb5fSLinus Walleij 	di->bat_ctrl = devm_iio_channel_get(dev, "bat_ctrl");
7390a8686e3SLinus Walleij 	if (IS_ERR(di->bat_ctrl)) {
74036f1de0dSLinus Walleij 		ret = dev_err_probe(dev, PTR_ERR(di->bat_ctrl),
74136f1de0dSLinus Walleij 				    "failed to get BAT CTRL ADC channel\n");
74236f1de0dSLinus Walleij 		return ret;
7430a8686e3SLinus Walleij 	}
7448c0984e5SSebastian Reichel 
7458c0984e5SSebastian Reichel 	di->initialized = false;
7468c0984e5SSebastian Reichel 
7478c0984e5SSebastian Reichel 	psy_cfg.supplied_to = supply_interface;
7488c0984e5SSebastian Reichel 	psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
7498c0984e5SSebastian Reichel 	psy_cfg.drv_data = di;
7508c0984e5SSebastian Reichel 
7518c0984e5SSebastian Reichel 	/* Init work for measuring temperature periodically */
7528c0984e5SSebastian Reichel 	INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
7538c0984e5SSebastian Reichel 		ab8500_btemp_periodic_work);
7548c0984e5SSebastian Reichel 
7558c0984e5SSebastian Reichel 	/* Set BTEMP thermal limits. Low and Med are fixed */
7568c0984e5SSebastian Reichel 	di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
7578c0984e5SSebastian Reichel 	di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
7588c0984e5SSebastian Reichel 
759ad89cb5fSLinus Walleij 	ret = abx500_get_register_interruptible(dev, AB8500_CHARGER,
7608c0984e5SSebastian Reichel 		AB8500_BTEMP_HIGH_TH, &val);
7618c0984e5SSebastian Reichel 	if (ret < 0) {
762ad89cb5fSLinus Walleij 		dev_err(dev, "%s ab8500 read failed\n", __func__);
7631c1f13a0SLinus Walleij 		return ret;
7648c0984e5SSebastian Reichel 	}
7658c0984e5SSebastian Reichel 	switch (val) {
7668c0984e5SSebastian Reichel 	case BTEMP_HIGH_TH_57_0:
7678c0984e5SSebastian Reichel 	case BTEMP_HIGH_TH_57_1:
7688c0984e5SSebastian Reichel 		di->btemp_ranges.btemp_high_limit =
7698c0984e5SSebastian Reichel 			BTEMP_THERMAL_HIGH_LIMIT_57;
7708c0984e5SSebastian Reichel 		break;
7718c0984e5SSebastian Reichel 	case BTEMP_HIGH_TH_52:
7728c0984e5SSebastian Reichel 		di->btemp_ranges.btemp_high_limit =
7738c0984e5SSebastian Reichel 			BTEMP_THERMAL_HIGH_LIMIT_52;
7748c0984e5SSebastian Reichel 		break;
7758c0984e5SSebastian Reichel 	case BTEMP_HIGH_TH_62:
7768c0984e5SSebastian Reichel 		di->btemp_ranges.btemp_high_limit =
7778c0984e5SSebastian Reichel 			BTEMP_THERMAL_HIGH_LIMIT_62;
7788c0984e5SSebastian Reichel 		break;
7798c0984e5SSebastian Reichel 	}
7808c0984e5SSebastian Reichel 
7818c0984e5SSebastian Reichel 	/* Register BTEMP power supply class */
7821c1f13a0SLinus Walleij 	di->btemp_psy = devm_power_supply_register(dev, &ab8500_btemp_desc,
7838c0984e5SSebastian Reichel 						   &psy_cfg);
7848c0984e5SSebastian Reichel 	if (IS_ERR(di->btemp_psy)) {
785ad89cb5fSLinus Walleij 		dev_err(dev, "failed to register BTEMP psy\n");
7861c1f13a0SLinus Walleij 		return PTR_ERR(di->btemp_psy);
7878c0984e5SSebastian Reichel 	}
7888c0984e5SSebastian Reichel 
7898c0984e5SSebastian Reichel 	/* Register interrupts */
7908c0984e5SSebastian Reichel 	for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
7918c0984e5SSebastian Reichel 		irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
7921c1f13a0SLinus Walleij 		if (irq < 0)
7931c1f13a0SLinus Walleij 			return irq;
794b10e9700SKrzysztof Kozlowski 
7951c1f13a0SLinus Walleij 		ret = devm_request_threaded_irq(dev, irq, NULL,
7961c1f13a0SLinus Walleij 			ab8500_btemp_irq[i].isr,
797df12470cSTian Tao 			IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
7988c0984e5SSebastian Reichel 			ab8500_btemp_irq[i].name, di);
7998c0984e5SSebastian Reichel 
8008c0984e5SSebastian Reichel 		if (ret) {
801ad89cb5fSLinus Walleij 			dev_err(dev, "failed to request %s IRQ %d: %d\n"
8028c0984e5SSebastian Reichel 				, ab8500_btemp_irq[i].name, irq, ret);
8031c1f13a0SLinus Walleij 			return ret;
8048c0984e5SSebastian Reichel 		}
805ad89cb5fSLinus Walleij 		dev_dbg(dev, "Requested %s IRQ %d: %d\n",
8068c0984e5SSebastian Reichel 			ab8500_btemp_irq[i].name, irq, ret);
8078c0984e5SSebastian Reichel 	}
8088c0984e5SSebastian Reichel 
8098c0984e5SSebastian Reichel 	platform_set_drvdata(pdev, di);
8108c0984e5SSebastian Reichel 
8118c0984e5SSebastian Reichel 	list_add_tail(&di->node, &ab8500_btemp_list);
8128c0984e5SSebastian Reichel 
8131c1f13a0SLinus Walleij 	return component_add(dev, &ab8500_btemp_component_ops);
8148c0984e5SSebastian Reichel }
815ccc023a5SKrzysztof Kozlowski 
8161c1f13a0SLinus Walleij static int ab8500_btemp_remove(struct platform_device *pdev)
8171c1f13a0SLinus Walleij {
8181c1f13a0SLinus Walleij 	component_del(&pdev->dev, &ab8500_btemp_component_ops);
8191c1f13a0SLinus Walleij 
8201c1f13a0SLinus Walleij 	return 0;
8218c0984e5SSebastian Reichel }
8228c0984e5SSebastian Reichel 
823f8efa0a8SLinus Walleij static SIMPLE_DEV_PM_OPS(ab8500_btemp_pm_ops, ab8500_btemp_suspend, ab8500_btemp_resume);
824f8efa0a8SLinus Walleij 
8258c0984e5SSebastian Reichel static const struct of_device_id ab8500_btemp_match[] = {
8268c0984e5SSebastian Reichel 	{ .compatible = "stericsson,ab8500-btemp", },
8278c0984e5SSebastian Reichel 	{ },
8288c0984e5SSebastian Reichel };
829dfe52db1SZou Wei MODULE_DEVICE_TABLE(of, ab8500_btemp_match);
8308c0984e5SSebastian Reichel 
8311c1f13a0SLinus Walleij struct platform_driver ab8500_btemp_driver = {
8328c0984e5SSebastian Reichel 	.probe = ab8500_btemp_probe,
8338c0984e5SSebastian Reichel 	.remove = ab8500_btemp_remove,
8348c0984e5SSebastian Reichel 	.driver = {
8358c0984e5SSebastian Reichel 		.name = "ab8500-btemp",
8368c0984e5SSebastian Reichel 		.of_match_table = ab8500_btemp_match,
837f8efa0a8SLinus Walleij 		.pm = &ab8500_btemp_pm_ops,
8388c0984e5SSebastian Reichel 	},
8398c0984e5SSebastian Reichel };
8408c0984e5SSebastian Reichel MODULE_LICENSE("GPL v2");
8418c0984e5SSebastian Reichel MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
8428c0984e5SSebastian Reichel MODULE_ALIAS("platform:ab8500-btemp");
8438c0984e5SSebastian Reichel MODULE_DESCRIPTION("AB8500 battery temperature driver");
844