Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (C) ST-Ericsson SA 2012
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Battery temperature driver for AB8500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Author:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *	Johan Palsson <johan.palsson@stericsson.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *	Karl Komierowski <karl.komierowski@stericsson.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *	Arun R Murthy <arun.murthy@stericsson.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/mfd/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/mfd/abx500.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/mfd/abx500/ab8500.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/mfd/abx500/ab8500-bm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/iio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #define VTVOUT_V			1800
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #define BTEMP_THERMAL_LOW_LIMIT		-10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #define BTEMP_THERMAL_MED_LIMIT		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #define BTEMP_THERMAL_HIGH_LIMIT_52	52
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define BTEMP_THERMAL_HIGH_LIMIT_57	57
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define BTEMP_THERMAL_HIGH_LIMIT_62	62
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #define BTEMP_BATCTRL_CURR_SRC_7UA	7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #define BTEMP_BATCTRL_CURR_SRC_20UA	20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #define BTEMP_BATCTRL_CURR_SRC_16UA	16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #define BTEMP_BATCTRL_CURR_SRC_18UA	18
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #define BTEMP_BATCTRL_CURR_SRC_60UA	60
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #define BTEMP_BATCTRL_CURR_SRC_120UA	120
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  * struct ab8500_btemp_interrupts - ab8500 interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  * @name:	name of the interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * @isr		function pointer to the isr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) struct ab8500_btemp_interrupts {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	irqreturn_t (*isr)(int irq, void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) struct ab8500_btemp_events {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	bool batt_rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	bool btemp_high;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	bool btemp_medhigh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	bool btemp_lowmed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	bool btemp_low;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	bool ac_conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	bool usb_conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) struct ab8500_btemp_ranges {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	int btemp_high_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	int btemp_med_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	int btemp_low_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  * struct ab8500_btemp - ab8500 BTEMP device information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  * @dev:		Pointer to the structure device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  * @node:		List of AB8500 BTEMPs, hence prepared for reentrance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * @curr_source:	What current source we use, in uA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  * @bat_temp:		Dispatched battery temperature in degree Celsius
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  * @prev_bat_temp	Last measured battery temperature in degree Celsius
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  * @parent:		Pointer to the struct ab8500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  * @adc_btemp_ball:	ADC channel for the battery ball temperature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  * @adc_bat_ctrl:	ADC channel for the battery control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  * @fg:			Pointer to the struct fg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  * @bm:           	Platform specific battery management information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  * @btemp_psy:		Structure for BTEMP specific battery properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  * @events:		Structure for information about events triggered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  * @btemp_ranges:	Battery temperature range structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  * @btemp_wq:		Work queue for measuring the temperature periodically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  * @btemp_periodic_work:	Work for measuring the temperature periodically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * @initialized:	True if battery id read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) struct ab8500_btemp {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	int curr_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	int bat_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	int prev_bat_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	struct ab8500 *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	struct iio_channel *btemp_ball;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	struct iio_channel *bat_ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	struct ab8500_fg *fg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	struct abx500_bm_data *bm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	struct power_supply *btemp_psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	struct ab8500_btemp_events events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	struct ab8500_btemp_ranges btemp_ranges;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	struct workqueue_struct *btemp_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	struct delayed_work btemp_periodic_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	bool initialized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) /* BTEMP power supply properties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) static enum power_supply_property ab8500_btemp_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	POWER_SUPPLY_PROP_TECHNOLOGY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	POWER_SUPPLY_PROP_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) static LIST_HEAD(ab8500_btemp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123)  * ab8500_btemp_get() - returns a reference to the primary AB8500 BTEMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124)  * (i.e. the first BTEMP in the instance list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) struct ab8500_btemp *ab8500_btemp_get(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	return list_first_entry(&ab8500_btemp_list, struct ab8500_btemp, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) EXPORT_SYMBOL(ab8500_btemp_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133)  * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134)  * @di:		pointer to the ab8500_btemp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135)  * @v_batctrl:	measured batctrl voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136)  * @inst_curr:	measured instant current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138)  * This function returns the battery resistance that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139)  * derived from the BATCTRL voltage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140)  * Returns value in Ohms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	int v_batctrl, int inst_curr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	int rbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	if (is_ab8500_1p1_or_earlier(di->parent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 		 * For ABB cut1.0 and 1.1 BAT_CTRL is internally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 		 * connected to 1.8V through a 450k resistor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 		return (450000 * (v_batctrl)) / (1800 - v_batctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 		 * If the battery has internal NTC, we use the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		 * source to calculate the resistance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		rbs = (v_batctrl * 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		       - di->bm->gnd_lift_resistance * inst_curr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 		      / di->curr_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		 * BAT_CTRL is internally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		 * connected to 1.8V through a 80k resistor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	return rbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175)  * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176)  * @di:		pointer to the ab8500_btemp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178)  * This function returns the voltage on BATCTRL. Returns value in mV.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	int vbtemp, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	static int prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	ret = iio_read_channel_processed(di->bat_ctrl, &vbtemp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		dev_err(di->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 			"%s ADC conversion failed, using previous value",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 			__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		return prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	prev = vbtemp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	return vbtemp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197)  * ab8500_btemp_curr_source_enable() - enable/disable batctrl current source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198)  * @di:		pointer to the ab8500_btemp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199)  * @enable:	enable or disable the current source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201)  * Enable or disable the current sources for the BatCtrl AD channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	int curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	 * BATCTRL current sources are included on AB8500 cut2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	 * and future versions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	if (is_ab8500_1p1_or_earlier(di->parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	/* Only do this for batteries with internal NTC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 			curr = BAT_CTRL_7U_ENA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 			curr = BAT_CTRL_20U_ENA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		ret = abx500_mask_and_set_register_interruptible(di->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 			AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 			FORCE_BAT_CTRL_CMP_HIGH, FORCE_BAT_CTRL_CMP_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 			dev_err(di->dev, "%s failed setting cmp_force\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 				__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		 * We have to wait one 32kHz cycle before enabling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		 * the current source, since ForceBatCtrlCmpHigh needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		 * to be written in a separate cycle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		udelay(32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		ret = abx500_set_register_interruptible(di->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 			AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 			FORCE_BAT_CTRL_CMP_HIGH | curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 			dev_err(di->dev, "%s failed enabling current source\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 				__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 			goto disable_curr_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	} else if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		dev_dbg(di->dev, "Disable BATCTRL curr source\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		/* Write 0 to the curr bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		ret = abx500_mask_and_set_register_interruptible(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 			di->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 			AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 			BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 			~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 			dev_err(di->dev, "%s failed disabling current source\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 				__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 			goto disable_curr_source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		/* Enable Pull-Up and comparator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		ret = abx500_mask_and_set_register_interruptible(di->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 			AB8500_CHARGER,	AB8500_BAT_CTRL_CURRENT_SOURCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 			BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 			BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 			dev_err(di->dev, "%s failed enabling PU and comp\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 				__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 			goto enable_pu_comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		 * We have to wait one 32kHz cycle before disabling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		 * ForceBatCtrlCmpHigh since this needs to be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		 * in a separate cycle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		udelay(32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		/* Disable 'force comparator' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		ret = abx500_mask_and_set_register_interruptible(di->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 			AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 			FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 			dev_err(di->dev, "%s failed disabling force comp\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 				__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 			goto disable_force_comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	 * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	 * if we got an error above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) disable_curr_source:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	/* Write 0 to the curr bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	ret = abx500_mask_and_set_register_interruptible(di->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		dev_err(di->dev, "%s failed disabling current source\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 			__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) enable_pu_comp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	/* Enable Pull-Up and comparator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	ret = abx500_mask_and_set_register_interruptible(di->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		AB8500_CHARGER,	AB8500_BAT_CTRL_CURRENT_SOURCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		dev_err(di->dev, "%s failed enabling PU and comp\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 			__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) disable_force_comp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	 * We have to wait one 32kHz cycle before disabling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	 * ForceBatCtrlCmpHigh since this needs to be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	 * in a separate cycle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	udelay(32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	/* Disable 'force comparator' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	ret = abx500_mask_and_set_register_interruptible(di->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		dev_err(di->dev, "%s failed disabling force comp\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 			__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346)  * ab8500_btemp_get_batctrl_res() - get battery resistance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347)  * @di:		pointer to the ab8500_btemp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349)  * This function returns the battery pack identification resistance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350)  * Returns value in Ohms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	int batctrl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	int inst_curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	 * BATCTRL current sources are included on AB8500 cut2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	 * and future versions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	ret = ab8500_btemp_curr_source_enable(di, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		dev_err(di->dev, "%s curr source enabled failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	if (!di->fg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		di->fg = ab8500_fg_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	if (!di->fg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		dev_err(di->dev, "No fg found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	ret = ab8500_fg_inst_curr_start(di->fg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		dev_err(di->dev, "Failed to start current measurement\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	} while (!ab8500_fg_inst_curr_started(di->fg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		batctrl += ab8500_btemp_read_batctrl_voltage(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	} while (!ab8500_fg_inst_curr_done(di->fg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	batctrl /= i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		dev_err(di->dev, "Failed to finalize current measurement\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	ret = ab8500_btemp_curr_source_enable(di, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		dev_err(di->dev, "%s curr source disable failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		__func__, batctrl, res, inst_curr, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418)  * ab8500_btemp_res_to_temp() - resistance to temperature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419)  * @di:		pointer to the ab8500_btemp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  * @tbl:	pointer to the resiatance to temperature table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421)  * @tbl_size:	size of the resistance to temperature table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422)  * @res:	resistance to calculate the temperature from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424)  * This function returns the battery temperature in degrees Celsius
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425)  * based on the NTC resistance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	const struct abx500_res_to_temp *tbl, int tbl_size, int res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	 * Calculate the formula for the straight line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	 * Simple interpolation if we are within
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	 * the resistance table limits, extrapolate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	 * if resistance is outside the limits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	if (res > tbl[0].resist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	else if (res <= tbl[tbl_size - 1].resist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		i = tbl_size - 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		while (!(res <= tbl[i].resist &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 			res > tbl[i + 1].resist))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 			i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	return tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		(res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453)  * ab8500_btemp_measure_temp() - measure battery temperature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454)  * @di:		pointer to the ab8500_btemp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456)  * Returns battery temperature (on success) else the previous temperature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) static int ab8500_btemp_measure_temp(struct ab8500_btemp *di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	int temp, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	static int prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	int rbat, rntc, vntc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	u8 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	id = di->bm->batt_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 			id != BATTERY_UNKNOWN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		rbat = ab8500_btemp_get_batctrl_res(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		if (rbat < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 			dev_err(di->dev, "%s get batctrl res failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 				__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 			 * Return out-of-range temperature so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 			 * charging is stopped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 			return BTEMP_THERMAL_LOW_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		temp = ab8500_btemp_res_to_temp(di,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 			di->bm->bat_type[id].r_to_t_tbl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 			di->bm->bat_type[id].n_temp_tbl_elements, rbat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		ret = iio_read_channel_processed(di->btemp_ball, &vntc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 			dev_err(di->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 				"%s ADC conversion failed,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 				" using previous value\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 			return prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		 * The PCB NTC is sourced from VTVOUT via a 230kOhm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		 * resistor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		rntc = 230000 * vntc / (VTVOUT_V - vntc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		temp = ab8500_btemp_res_to_temp(di,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 			di->bm->bat_type[id].r_to_t_tbl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			di->bm->bat_type[id].n_temp_tbl_elements, rntc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		prev = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	dev_dbg(di->dev, "Battery temperature is %d\n", temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	return temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508)  * ab8500_btemp_id() - Identify the connected battery
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509)  * @di:		pointer to the ab8500_btemp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511)  * This function will try to identify the battery by reading the ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512)  * resistor. Some brands use a combined ID resistor with a NTC resistor to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513)  * both be able to identify and to read the temperature of it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) static int ab8500_btemp_id(struct ab8500_btemp *di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	u8 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	di->bm->batt_id = BATTERY_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	res =  ab8500_btemp_get_batctrl_res(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	if (res < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		dev_err(di->dev, "%s get batctrl res failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	/* BATTERY_UNKNOWN is defined on position 0, skip it! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	for (i = BATTERY_UNKNOWN + 1; i < di->bm->n_btypes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		if ((res <= di->bm->bat_type[i].resis_high) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 			(res >= di->bm->bat_type[i].resis_low)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 			dev_dbg(di->dev, "Battery detected on %s"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 				" low %d < res %d < high: %d"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 				" index: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 				di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 				"BATCTRL" : "BATTEMP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 				di->bm->bat_type[i].resis_low, res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 				di->bm->bat_type[i].resis_high, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 			di->bm->batt_id = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	if (di->bm->batt_id == BATTERY_UNKNOWN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		dev_warn(di->dev, "Battery identified as unknown"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 			", resistance %d Ohm\n", res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	 * We only have to change current source if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	 * detected type is Type 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	    di->bm->batt_id == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	return di->bm->batt_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566)  * ab8500_btemp_periodic_work() - Measuring the temperature periodically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567)  * @work:	pointer to the work_struct structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569)  * Work function for measuring the temperature periodically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) static void ab8500_btemp_periodic_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	int interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	int bat_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	struct ab8500_btemp *di = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		struct ab8500_btemp, btemp_periodic_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	if (!di->initialized) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		/* Identify the battery */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		if (ab8500_btemp_id(di) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 			dev_warn(di->dev, "failed to identify the battery\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	bat_temp = ab8500_btemp_measure_temp(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	 * Filter battery temperature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	 * Allow direct updates on temperature only if two samples result in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	 * same temperature. Else only allow 1 degree change from previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	 * reported value in the direction of the new measurement.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	if ((bat_temp == di->prev_bat_temp) || !di->initialized) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		if ((di->bat_temp != di->prev_bat_temp) || !di->initialized) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 			di->initialized = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 			di->bat_temp = bat_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 			power_supply_changed(di->btemp_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	} else if (bat_temp < di->prev_bat_temp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		di->bat_temp--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		power_supply_changed(di->btemp_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	} else if (bat_temp > di->prev_bat_temp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		di->bat_temp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		power_supply_changed(di->btemp_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	di->prev_bat_temp = bat_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	if (di->events.ac_conn || di->events.usb_conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		interval = di->bm->temp_interval_chg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		interval = di->bm->temp_interval_nochg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	/* Schedule a new measurement */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	queue_delayed_work(di->btemp_wq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		&di->btemp_periodic_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		round_jiffies(interval * HZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618)  * ab8500_btemp_batctrlindb_handler() - battery removal detected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619)  * @irq:       interrupt number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620)  * @_di:       void pointer that has to address of ab8500_btemp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622)  * Returns IRQ status(IRQ_HANDLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	struct ab8500_btemp *di = _di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	dev_err(di->dev, "Battery removal detected!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	di->events.batt_rem = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	power_supply_changed(di->btemp_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636)  * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637)  * @irq:       interrupt number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638)  * @_di:       void pointer that has to address of ab8500_btemp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640)  * Returns IRQ status(IRQ_HANDLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	struct ab8500_btemp *di = _di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	if (is_ab8500_3p3_or_earlier(di->parent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		dev_dbg(di->dev, "Ignore false btemp low irq"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			" for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		di->events.btemp_low = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		di->events.btemp_high = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		di->events.btemp_medhigh = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		di->events.btemp_lowmed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		power_supply_changed(di->btemp_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663)  * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664)  * @irq:       interrupt number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665)  * @_di:       void pointer that has to address of ab8500_btemp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667)  * Returns IRQ status(IRQ_HANDLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	struct ab8500_btemp *di = _di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	di->events.btemp_high = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	di->events.btemp_medhigh = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	di->events.btemp_lowmed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	di->events.btemp_low = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	power_supply_changed(di->btemp_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685)  * ab8500_btemp_lowmed_handler() - battery temp between low and medium
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686)  * @irq:       interrupt number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687)  * @_di:       void pointer that has to address of ab8500_btemp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689)  * Returns IRQ status(IRQ_HANDLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	struct ab8500_btemp *di = _di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	dev_dbg(di->dev, "Battery temperature is between low and medium\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	di->events.btemp_lowmed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	di->events.btemp_medhigh = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	di->events.btemp_high = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	di->events.btemp_low = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	power_supply_changed(di->btemp_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707)  * ab8500_btemp_medhigh_handler() - battery temp between medium and high
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708)  * @irq:       interrupt number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709)  * @_di:       void pointer that has to address of ab8500_btemp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711)  * Returns IRQ status(IRQ_HANDLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	struct ab8500_btemp *di = _di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	dev_dbg(di->dev, "Battery temperature is between medium and high\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	di->events.btemp_medhigh = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	di->events.btemp_lowmed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	di->events.btemp_high = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	di->events.btemp_low = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	power_supply_changed(di->btemp_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729)  * ab8500_btemp_periodic() - Periodic temperature measurements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730)  * @di:		pointer to the ab8500_btemp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731)  * @enable:	enable or disable periodic temperature measurements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733)  * Starts of stops periodic temperature measurements. Periodic measurements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734)  * should only be done when a charger is connected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) static void ab8500_btemp_periodic(struct ab8500_btemp *di,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	 * Make sure a new measurement is done directly by cancelling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	 * any pending work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	cancel_delayed_work_sync(&di->btemp_periodic_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	if (enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752)  * ab8500_btemp_get_temp() - get battery temperature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753)  * @di:		pointer to the ab8500_btemp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755)  * Returns battery temperature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) int ab8500_btemp_get_temp(struct ab8500_btemp *di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	int temp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	 * The BTEMP events are not reliabe on AB8500 cut3.3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	 * and prior versions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	if (is_ab8500_3p3_or_earlier(di->parent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		temp = di->bat_temp * 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		if (di->events.btemp_low) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 			if (temp > di->btemp_ranges.btemp_low_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 				temp = di->btemp_ranges.btemp_low_limit * 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 				temp = di->bat_temp * 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		} else if (di->events.btemp_high) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 			if (temp < di->btemp_ranges.btemp_high_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 				temp = di->btemp_ranges.btemp_high_limit * 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 				temp = di->bat_temp * 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		} else if (di->events.btemp_lowmed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 			if (temp > di->btemp_ranges.btemp_med_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 				temp = di->btemp_ranges.btemp_med_limit * 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 				temp = di->bat_temp * 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		} else if (di->events.btemp_medhigh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 			if (temp < di->btemp_ranges.btemp_med_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 				temp = di->btemp_ranges.btemp_med_limit * 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 				temp = di->bat_temp * 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 			temp = di->bat_temp * 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	return temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) EXPORT_SYMBOL(ab8500_btemp_get_temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796)  * ab8500_btemp_get_batctrl_temp() - get the temperature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797)  * @btemp:      pointer to the btemp structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799)  * Returns the batctrl temperature in millidegrees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	return btemp->bat_temp * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) EXPORT_SYMBOL(ab8500_btemp_get_batctrl_temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808)  * ab8500_btemp_get_property() - get the btemp properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809)  * @psy:        pointer to the power_supply structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810)  * @psp:        pointer to the power_supply_property structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811)  * @val:        pointer to the power_supply_propval union
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813)  * This function gets called when an application tries to get the btemp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814)  * properties by reading the sysfs files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815)  * online:	presence of the battery
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816)  * present:	presence of the battery
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817)  * technology:	battery technology
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818)  * temp:	battery temperature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819)  * Returns error code in case of failure else 0(on success)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) static int ab8500_btemp_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	struct ab8500_btemp *di = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		if (di->events.batt_rem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 			val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 			val->intval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	case POWER_SUPPLY_PROP_TECHNOLOGY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		val->intval = di->bm->bat_type[di->bm->batt_id].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	case POWER_SUPPLY_PROP_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		val->intval = ab8500_btemp_get_temp(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	struct power_supply *psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	struct power_supply *ext = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	const char **supplicants = (const char **)ext->supplied_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	struct ab8500_btemp *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	union power_supply_propval ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	psy = (struct power_supply *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	di = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	 * For all psy where the name of your driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	 * appears in any supplied_to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	if (j < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	/* Go through all properties for the psy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	for (j = 0; j < ext->desc->num_properties; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		enum power_supply_property prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		prop = ext->desc->properties[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		if (power_supply_get_property(ext, prop, &ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		switch (prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 			switch (ext->desc->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 			case POWER_SUPPLY_TYPE_MAINS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 				/* AC disconnected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 				if (!ret.intval && di->events.ac_conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 					di->events.ac_conn = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 				/* AC connected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 				else if (ret.intval && !di->events.ac_conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 					di->events.ac_conn = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 					if (!di->events.usb_conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 						ab8500_btemp_periodic(di, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 			case POWER_SUPPLY_TYPE_USB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 				/* USB disconnected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 				if (!ret.intval && di->events.usb_conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 					di->events.usb_conn = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 				/* USB connected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 				else if (ret.intval && !di->events.usb_conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 					di->events.usb_conn = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 					if (!di->events.ac_conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 						ab8500_btemp_periodic(di, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914)  * ab8500_btemp_external_power_changed() - callback for power supply changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915)  * @psy:       pointer to the structure power_supply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917)  * This function is pointing to the function pointer external_power_changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918)  * of the structure power_supply.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919)  * This function gets executed when there is a change in the external power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920)  * supply to the btemp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) static void ab8500_btemp_external_power_changed(struct power_supply *psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	struct ab8500_btemp *di = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	class_for_each_device(power_supply_class, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		di->btemp_psy, ab8500_btemp_get_ext_psy_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) /* ab8500 btemp driver interrupts and their respective isr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	{"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	{"BTEMP_LOW", ab8500_btemp_templow_handler},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	{"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	{"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	{"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) #if defined(CONFIG_PM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) static int ab8500_btemp_resume(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	struct ab8500_btemp *di = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	ab8500_btemp_periodic(di, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) static int ab8500_btemp_suspend(struct platform_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	pm_message_t state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	struct ab8500_btemp *di = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	ab8500_btemp_periodic(di, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) #define ab8500_btemp_suspend      NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) #define ab8500_btemp_resume       NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) static int ab8500_btemp_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	struct ab8500_btemp *di = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	int i, irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	/* Disable interrupts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		free_irq(irq, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	/* Delete the work queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	destroy_workqueue(di->btemp_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	flush_scheduled_work();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	power_supply_unregister(di->btemp_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) static char *supply_interface[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	"ab8500_chargalg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	"ab8500_fg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) static const struct power_supply_desc ab8500_btemp_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	.name			= "ab8500_btemp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	.type			= POWER_SUPPLY_TYPE_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	.properties		= ab8500_btemp_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	.num_properties		= ARRAY_SIZE(ab8500_btemp_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	.get_property		= ab8500_btemp_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	.external_power_changed	= ab8500_btemp_external_power_changed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) static int ab8500_btemp_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	struct device_node *np = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	struct abx500_bm_data *plat = pdev->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	struct power_supply_config psy_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	struct ab8500_btemp *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	int irq, i, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	if (!di) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		dev_err(&pdev->dev, "%s no mem for ab8500_btemp\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	if (!plat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		dev_err(&pdev->dev, "no battery management data supplied\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	di->bm = plat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	if (np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 			dev_err(&pdev->dev, "failed to get battery information\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	/* get parent data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	di->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	di->parent = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	/* Get ADC channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	di->btemp_ball = devm_iio_channel_get(&pdev->dev, "btemp_ball");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (IS_ERR(di->btemp_ball)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		if (PTR_ERR(di->btemp_ball) == -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 			return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		dev_err(&pdev->dev, "failed to get BTEMP BALL ADC channel\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		return PTR_ERR(di->btemp_ball);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	di->bat_ctrl = devm_iio_channel_get(&pdev->dev, "bat_ctrl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	if (IS_ERR(di->bat_ctrl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		if (PTR_ERR(di->bat_ctrl) == -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		dev_err(&pdev->dev, "failed to get BAT CTRL ADC channel\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		return PTR_ERR(di->bat_ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	di->initialized = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	psy_cfg.supplied_to = supply_interface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	psy_cfg.drv_data = di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	/* Create a work queue for the btemp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	di->btemp_wq =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		alloc_workqueue("ab8500_btemp_wq", WQ_MEM_RECLAIM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	if (di->btemp_wq == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		dev_err(di->dev, "failed to create work queue\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	/* Init work for measuring temperature periodically */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		ab8500_btemp_periodic_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	/* Set BTEMP thermal limits. Low and Med are fixed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		AB8500_BTEMP_HIGH_TH, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		dev_err(di->dev, "%s ab8500 read failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		goto free_btemp_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	switch (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	case BTEMP_HIGH_TH_57_0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	case BTEMP_HIGH_TH_57_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		di->btemp_ranges.btemp_high_limit =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 			BTEMP_THERMAL_HIGH_LIMIT_57;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	case BTEMP_HIGH_TH_52:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		di->btemp_ranges.btemp_high_limit =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 			BTEMP_THERMAL_HIGH_LIMIT_52;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	case BTEMP_HIGH_TH_62:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		di->btemp_ranges.btemp_high_limit =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 			BTEMP_THERMAL_HIGH_LIMIT_62;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	/* Register BTEMP power supply class */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	di->btemp_psy = power_supply_register(di->dev, &ab8500_btemp_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 					      &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	if (IS_ERR(di->btemp_psy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		dev_err(di->dev, "failed to register BTEMP psy\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		ret = PTR_ERR(di->btemp_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		goto free_btemp_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	/* Register interrupts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		if (irq < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			ret = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 			goto free_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 			IRQF_SHARED | IRQF_NO_SUSPEND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 			ab8500_btemp_irq[i].name, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 			dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 				, ab8500_btemp_irq[i].name, irq, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 			goto free_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 			ab8500_btemp_irq[i].name, irq, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	platform_set_drvdata(pdev, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	/* Kick off periodic temperature measurements */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	ab8500_btemp_periodic(di, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	list_add_tail(&di->node, &ab8500_btemp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) free_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	/* We also have to free all successfully registered irqs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	for (i = i - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		free_irq(irq, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	power_supply_unregister(di->btemp_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) free_btemp_wq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	destroy_workqueue(di->btemp_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) static const struct of_device_id ab8500_btemp_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	{ .compatible = "stericsson,ab8500-btemp", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) MODULE_DEVICE_TABLE(of, ab8500_btemp_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) static struct platform_driver ab8500_btemp_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	.probe = ab8500_btemp_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	.remove = ab8500_btemp_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	.suspend = ab8500_btemp_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	.resume = ab8500_btemp_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		.name = "ab8500-btemp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		.of_match_table = ab8500_btemp_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) static int __init ab8500_btemp_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	return platform_driver_register(&ab8500_btemp_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) static void __exit ab8500_btemp_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	platform_driver_unregister(&ab8500_btemp_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) device_initcall(ab8500_btemp_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) module_exit(ab8500_btemp_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) MODULE_ALIAS("platform:ab8500-btemp");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) MODULE_DESCRIPTION("AB8500 battery temperature driver");