12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2eb982001SEduardo Valentin /*
3eb982001SEduardo Valentin  * TI Bandgap temperature sensor driver
4eb982001SEduardo Valentin  *
5eb982001SEduardo Valentin  * Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/
6eb982001SEduardo Valentin  * Author: J Keerthy <j-keerthy@ti.com>
7eb982001SEduardo Valentin  * Author: Moiz Sonasath <m-sonasath@ti.com>
8eb982001SEduardo Valentin  * Couple of fixes, DT and MFD adaptation:
9eb982001SEduardo Valentin  *   Eduardo Valentin <eduardo.valentin@ti.com>
10eb982001SEduardo Valentin  */
11eb982001SEduardo Valentin 
12eb982001SEduardo Valentin #include <linux/clk.h>
135093402eSAdam Ford #include <linux/cpu_pm.h>
145093402eSAdam Ford #include <linux/device.h>
155a4a8235SZhen Lei #include <linux/err.h>
165a4a8235SZhen Lei #include <linux/export.h>
175a4a8235SZhen Lei #include <linux/gpio/consumer.h>
185a4a8235SZhen Lei #include <linux/init.h>
195a4a8235SZhen Lei #include <linux/interrupt.h>
205a4a8235SZhen Lei #include <linux/io.h>
215a4a8235SZhen Lei #include <linux/iopoll.h>
225a4a8235SZhen Lei #include <linux/kernel.h>
235a4a8235SZhen Lei #include <linux/module.h>
245093402eSAdam Ford #include <linux/of.h>
255a4a8235SZhen Lei #include <linux/of_device.h>
265a4a8235SZhen Lei #include <linux/of_irq.h>
275a4a8235SZhen Lei #include <linux/of_platform.h>
285a4a8235SZhen Lei #include <linux/platform_device.h>
295a4a8235SZhen Lei #include <linux/pm.h>
305a4a8235SZhen Lei #include <linux/pm_runtime.h>
315a4a8235SZhen Lei #include <linux/reboot.h>
325a4a8235SZhen Lei #include <linux/spinlock.h>
335a4a8235SZhen Lei #include <linux/sys_soc.h>
345a4a8235SZhen Lei #include <linux/types.h>
35eb982001SEduardo Valentin 
36eb982001SEduardo Valentin #include "ti-bandgap.h"
37eb982001SEduardo Valentin 
3895d079efSPavel Machek static int ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id);
395093402eSAdam Ford #ifdef CONFIG_PM_SLEEP
405093402eSAdam Ford static int bandgap_omap_cpu_notifier(struct notifier_block *nb,
415093402eSAdam Ford 				  unsigned long cmd, void *v);
425093402eSAdam Ford #endif
4395d079efSPavel Machek 
44eb982001SEduardo Valentin /***   Helper functions to access registers and their bitfields   ***/
45eb982001SEduardo Valentin 
46eb982001SEduardo Valentin /**
47eb982001SEduardo Valentin  * ti_bandgap_readl() - simple read helper function
48eb982001SEduardo Valentin  * @bgp: pointer to ti_bandgap structure
49eb982001SEduardo Valentin  * @reg: desired register (offset) to be read
50eb982001SEduardo Valentin  *
51eb982001SEduardo Valentin  * Helper function to read bandgap registers. It uses the io remapped area.
52eb982001SEduardo Valentin  * Return: the register value.
53eb982001SEduardo Valentin  */
ti_bandgap_readl(struct ti_bandgap * bgp,u32 reg)54eb982001SEduardo Valentin static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
55eb982001SEduardo Valentin {
56eb982001SEduardo Valentin 	return readl(bgp->base + reg);
57eb982001SEduardo Valentin }
58eb982001SEduardo Valentin 
59eb982001SEduardo Valentin /**
60eb982001SEduardo Valentin  * ti_bandgap_writel() - simple write helper function
61eb982001SEduardo Valentin  * @bgp: pointer to ti_bandgap structure
62eb982001SEduardo Valentin  * @val: desired register value to be written
63eb982001SEduardo Valentin  * @reg: desired register (offset) to be written
64eb982001SEduardo Valentin  *
65eb982001SEduardo Valentin  * Helper function to write bandgap registers. It uses the io remapped area.
66eb982001SEduardo Valentin  */
ti_bandgap_writel(struct ti_bandgap * bgp,u32 val,u32 reg)67eb982001SEduardo Valentin static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
68eb982001SEduardo Valentin {
69eb982001SEduardo Valentin 	writel(val, bgp->base + reg);
70eb982001SEduardo Valentin }
71eb982001SEduardo Valentin 
72eb982001SEduardo Valentin /**
73eb982001SEduardo Valentin  * DOC: macro to update bits.
74eb982001SEduardo Valentin  *
75eb982001SEduardo Valentin  * RMW_BITS() - used to read, modify and update bandgap bitfields.
76eb982001SEduardo Valentin  *            The value passed will be shifted.
77eb982001SEduardo Valentin  */
78eb982001SEduardo Valentin #define RMW_BITS(bgp, id, reg, mask, val)			\
79eb982001SEduardo Valentin do {								\
80eb982001SEduardo Valentin 	struct temp_sensor_registers *t;			\
81eb982001SEduardo Valentin 	u32 r;							\
82eb982001SEduardo Valentin 								\
83eb982001SEduardo Valentin 	t = bgp->conf->sensors[(id)].registers;		\
84eb982001SEduardo Valentin 	r = ti_bandgap_readl(bgp, t->reg);			\
85eb982001SEduardo Valentin 	r &= ~t->mask;						\
86eb982001SEduardo Valentin 	r |= (val) << __ffs(t->mask);				\
87eb982001SEduardo Valentin 	ti_bandgap_writel(bgp, r, t->reg);			\
88eb982001SEduardo Valentin } while (0)
89eb982001SEduardo Valentin 
90eb982001SEduardo Valentin /***   Basic helper functions   ***/
91eb982001SEduardo Valentin 
92eb982001SEduardo Valentin /**
93eb982001SEduardo Valentin  * ti_bandgap_power() - controls the power state of a bandgap device
94eb982001SEduardo Valentin  * @bgp: pointer to ti_bandgap structure
95eb982001SEduardo Valentin  * @on: desired power state (1 - on, 0 - off)
96eb982001SEduardo Valentin  *
97eb982001SEduardo Valentin  * Used to power on/off a bandgap device instance. Only used on those
98eb982001SEduardo Valentin  * that features tempsoff bit.
99eb982001SEduardo Valentin  *
100eb982001SEduardo Valentin  * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
101eb982001SEduardo Valentin  */
ti_bandgap_power(struct ti_bandgap * bgp,bool on)102eb982001SEduardo Valentin static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
103eb982001SEduardo Valentin {
104e34238bfSPavel Machek 	int i;
105eb982001SEduardo Valentin 
106e34238bfSPavel Machek 	if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH))
107e34238bfSPavel Machek 		return -ENOTSUPP;
108eb982001SEduardo Valentin 
109eb982001SEduardo Valentin 	for (i = 0; i < bgp->conf->sensor_count; i++)
110eb982001SEduardo Valentin 		/* active on 0 */
111eb982001SEduardo Valentin 		RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
112e34238bfSPavel Machek 	return 0;
113eb982001SEduardo Valentin }
114eb982001SEduardo Valentin 
115eb982001SEduardo Valentin /**
11679010636SKeerthy  * ti_errata814_bandgap_read_temp() - helper function to read dra7 sensor temperature
11779010636SKeerthy  * @bgp: pointer to ti_bandgap structure
11879010636SKeerthy  * @reg: desired register (offset) to be read
11979010636SKeerthy  *
12079010636SKeerthy  * Function to read dra7 bandgap sensor temperature. This is done separately
12179010636SKeerthy  * so as to workaround the errata "Bandgap Temperature read Dtemp can be
12279010636SKeerthy  * corrupted" - Errata ID: i814".
12379010636SKeerthy  * Read accesses to registers listed below can be corrupted due to incorrect
12479010636SKeerthy  * resynchronization between clock domains.
12579010636SKeerthy  * Read access to registers below can be corrupted :
12679010636SKeerthy  * CTRL_CORE_DTEMP_MPU/GPU/CORE/DSPEVE/IVA_n (n = 0 to 4)
12779010636SKeerthy  * CTRL_CORE_TEMP_SENSOR_MPU/GPU/CORE/DSPEVE/IVA_n
12879010636SKeerthy  *
12979010636SKeerthy  * Return: the register value.
13079010636SKeerthy  */
ti_errata814_bandgap_read_temp(struct ti_bandgap * bgp,u32 reg)13179010636SKeerthy static u32 ti_errata814_bandgap_read_temp(struct ti_bandgap *bgp,  u32 reg)
13279010636SKeerthy {
13379010636SKeerthy 	u32 val1, val2;
13479010636SKeerthy 
13579010636SKeerthy 	val1 = ti_bandgap_readl(bgp, reg);
13679010636SKeerthy 	val2 = ti_bandgap_readl(bgp, reg);
13779010636SKeerthy 
13879010636SKeerthy 	/* If both times we read the same value then that is right */
13979010636SKeerthy 	if (val1 == val2)
14079010636SKeerthy 		return val1;
14179010636SKeerthy 
14279010636SKeerthy 	/* if val1 and val2 are different read it third time */
14379010636SKeerthy 	return ti_bandgap_readl(bgp, reg);
14479010636SKeerthy }
14579010636SKeerthy 
14679010636SKeerthy /**
147eb982001SEduardo Valentin  * ti_bandgap_read_temp() - helper function to read sensor temperature
148eb982001SEduardo Valentin  * @bgp: pointer to ti_bandgap structure
149eb982001SEduardo Valentin  * @id: bandgap sensor id
150eb982001SEduardo Valentin  *
151eb982001SEduardo Valentin  * Function to concentrate the steps to read sensor temperature register.
152eb982001SEduardo Valentin  * This function is desired because, depending on bandgap device version,
153eb982001SEduardo Valentin  * it might be needed to freeze the bandgap state machine, before fetching
154eb982001SEduardo Valentin  * the register value.
155eb982001SEduardo Valentin  *
156eb982001SEduardo Valentin  * Return: temperature in ADC values.
157eb982001SEduardo Valentin  */
ti_bandgap_read_temp(struct ti_bandgap * bgp,int id)158eb982001SEduardo Valentin static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
159eb982001SEduardo Valentin {
160eb982001SEduardo Valentin 	struct temp_sensor_registers *tsr;
161eb982001SEduardo Valentin 	u32 temp, reg;
162eb982001SEduardo Valentin 
163eb982001SEduardo Valentin 	tsr = bgp->conf->sensors[id].registers;
164eb982001SEduardo Valentin 	reg = tsr->temp_sensor_ctrl;
165eb982001SEduardo Valentin 
166eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
167eb982001SEduardo Valentin 		RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
168eb982001SEduardo Valentin 		/*
169eb982001SEduardo Valentin 		 * In case we cannot read from cur_dtemp / dtemp_0,
170eb982001SEduardo Valentin 		 * then we read from the last valid temp read
171eb982001SEduardo Valentin 		 */
172eb982001SEduardo Valentin 		reg = tsr->ctrl_dtemp_1;
173eb982001SEduardo Valentin 	}
174eb982001SEduardo Valentin 
175eb982001SEduardo Valentin 	/* read temperature */
17679010636SKeerthy 	if (TI_BANDGAP_HAS(bgp, ERRATA_814))
17779010636SKeerthy 		temp = ti_errata814_bandgap_read_temp(bgp, reg);
17879010636SKeerthy 	else
179eb982001SEduardo Valentin 		temp = ti_bandgap_readl(bgp, reg);
18079010636SKeerthy 
181eb982001SEduardo Valentin 	temp &= tsr->bgap_dtemp_mask;
182eb982001SEduardo Valentin 
183eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
184eb982001SEduardo Valentin 		RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
185eb982001SEduardo Valentin 
186eb982001SEduardo Valentin 	return temp;
187eb982001SEduardo Valentin }
188eb982001SEduardo Valentin 
189eb982001SEduardo Valentin /***   IRQ handlers   ***/
190eb982001SEduardo Valentin 
191eb982001SEduardo Valentin /**
192eb982001SEduardo Valentin  * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
193eb982001SEduardo Valentin  * @irq: IRQ number
194eb982001SEduardo Valentin  * @data: private data (struct ti_bandgap *)
195eb982001SEduardo Valentin  *
196eb982001SEduardo Valentin  * This is the Talert handler. Use it only if bandgap device features
197eb982001SEduardo Valentin  * HAS(TALERT). This handler goes over all sensors and checks their
198eb982001SEduardo Valentin  * conditions and acts accordingly. In case there are events pending,
199eb982001SEduardo Valentin  * it will reset the event mask to wait for the opposite event (next event).
200eb982001SEduardo Valentin  * Every time there is a new event, it will be reported to thermal layer.
201eb982001SEduardo Valentin  *
202eb982001SEduardo Valentin  * Return: IRQ_HANDLED
203eb982001SEduardo Valentin  */
ti_bandgap_talert_irq_handler(int irq,void * data)204eb982001SEduardo Valentin static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
205eb982001SEduardo Valentin {
206eb982001SEduardo Valentin 	struct ti_bandgap *bgp = data;
207eb982001SEduardo Valentin 	struct temp_sensor_registers *tsr;
208eb982001SEduardo Valentin 	u32 t_hot = 0, t_cold = 0, ctrl;
209eb982001SEduardo Valentin 	int i;
210eb982001SEduardo Valentin 
211eb982001SEduardo Valentin 	spin_lock(&bgp->lock);
212eb982001SEduardo Valentin 	for (i = 0; i < bgp->conf->sensor_count; i++) {
213eb982001SEduardo Valentin 		tsr = bgp->conf->sensors[i].registers;
214eb982001SEduardo Valentin 		ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
215eb982001SEduardo Valentin 
216eb982001SEduardo Valentin 		/* Read the status of t_hot */
217eb982001SEduardo Valentin 		t_hot = ctrl & tsr->status_hot_mask;
218eb982001SEduardo Valentin 
219eb982001SEduardo Valentin 		/* Read the status of t_cold */
220eb982001SEduardo Valentin 		t_cold = ctrl & tsr->status_cold_mask;
221eb982001SEduardo Valentin 
222eb982001SEduardo Valentin 		if (!t_cold && !t_hot)
223eb982001SEduardo Valentin 			continue;
224eb982001SEduardo Valentin 
225eb982001SEduardo Valentin 		ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
226eb982001SEduardo Valentin 		/*
227eb982001SEduardo Valentin 		 * One TALERT interrupt: Two sources
228eb982001SEduardo Valentin 		 * If the interrupt is due to t_hot then mask t_hot and
2298b74a003SJason Wang 		 * unmask t_cold else mask t_cold and unmask t_hot
230eb982001SEduardo Valentin 		 */
231eb982001SEduardo Valentin 		if (t_hot) {
232eb982001SEduardo Valentin 			ctrl &= ~tsr->mask_hot_mask;
233eb982001SEduardo Valentin 			ctrl |= tsr->mask_cold_mask;
234eb982001SEduardo Valentin 		} else if (t_cold) {
235eb982001SEduardo Valentin 			ctrl &= ~tsr->mask_cold_mask;
236eb982001SEduardo Valentin 			ctrl |= tsr->mask_hot_mask;
237eb982001SEduardo Valentin 		}
238eb982001SEduardo Valentin 
239eb982001SEduardo Valentin 		ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
240eb982001SEduardo Valentin 
241eb982001SEduardo Valentin 		dev_dbg(bgp->dev,
242eb982001SEduardo Valentin 			"%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
243eb982001SEduardo Valentin 			__func__, bgp->conf->sensors[i].domain,
244eb982001SEduardo Valentin 			t_hot, t_cold);
245eb982001SEduardo Valentin 
246eb982001SEduardo Valentin 		/* report temperature to whom may concern */
247eb982001SEduardo Valentin 		if (bgp->conf->report_temperature)
248eb982001SEduardo Valentin 			bgp->conf->report_temperature(bgp, i);
249eb982001SEduardo Valentin 	}
250eb982001SEduardo Valentin 	spin_unlock(&bgp->lock);
251eb982001SEduardo Valentin 
252eb982001SEduardo Valentin 	return IRQ_HANDLED;
253eb982001SEduardo Valentin }
254eb982001SEduardo Valentin 
255eb982001SEduardo Valentin /**
256eb982001SEduardo Valentin  * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
257eb982001SEduardo Valentin  * @irq: IRQ number
258eb982001SEduardo Valentin  * @data: private data (unused)
259eb982001SEduardo Valentin  *
260eb982001SEduardo Valentin  * This is the Tshut handler. Use it only if bandgap device features
261eb982001SEduardo Valentin  * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
262eb982001SEduardo Valentin  * the system.
263eb982001SEduardo Valentin  *
264eb982001SEduardo Valentin  * Return: IRQ_HANDLED
265eb982001SEduardo Valentin  */
ti_bandgap_tshut_irq_handler(int irq,void * data)266eb982001SEduardo Valentin static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
267eb982001SEduardo Valentin {
268eb982001SEduardo Valentin 	pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
269eb982001SEduardo Valentin 		 __func__);
270eb982001SEduardo Valentin 
271eb982001SEduardo Valentin 	orderly_poweroff(true);
272eb982001SEduardo Valentin 
273eb982001SEduardo Valentin 	return IRQ_HANDLED;
274eb982001SEduardo Valentin }
275eb982001SEduardo Valentin 
276eb982001SEduardo Valentin /***   Helper functions which manipulate conversion ADC <-> mi Celsius   ***/
277eb982001SEduardo Valentin 
278eb982001SEduardo Valentin /**
279eb982001SEduardo Valentin  * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
280eb982001SEduardo Valentin  * @bgp: struct ti_bandgap pointer
281eb982001SEduardo Valentin  * @adc_val: value in ADC representation
282eb982001SEduardo Valentin  * @t: address where to write the resulting temperature in mCelsius
283eb982001SEduardo Valentin  *
284eb982001SEduardo Valentin  * Simple conversion from ADC representation to mCelsius. In case the ADC value
285eb982001SEduardo Valentin  * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
286eb982001SEduardo Valentin  * The conversion table is indexed by the ADC values.
287eb982001SEduardo Valentin  *
288eb982001SEduardo Valentin  * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
289eb982001SEduardo Valentin  * argument is out of the ADC conv table range.
290eb982001SEduardo Valentin  */
291eb982001SEduardo Valentin static
ti_bandgap_adc_to_mcelsius(struct ti_bandgap * bgp,int adc_val,int * t)292eb982001SEduardo Valentin int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
293eb982001SEduardo Valentin {
294eb982001SEduardo Valentin 	const struct ti_bandgap_data *conf = bgp->conf;
295eb982001SEduardo Valentin 
296eb982001SEduardo Valentin 	/* look up for temperature in the table and return the temperature */
297e34238bfSPavel Machek 	if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val)
298e34238bfSPavel Machek 		return -ERANGE;
299eb982001SEduardo Valentin 
300eb982001SEduardo Valentin 	*t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
301e34238bfSPavel Machek 	return 0;
302eb982001SEduardo Valentin }
303eb982001SEduardo Valentin 
304eb982001SEduardo Valentin /**
305eb982001SEduardo Valentin  * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
306eb982001SEduardo Valentin  * @bgp: struct ti_bandgap pointer
307eb982001SEduardo Valentin  * @id: bandgap sensor id
308eb982001SEduardo Valentin  *
309eb982001SEduardo Valentin  * Checks if the bandgap pointer is valid and if the sensor id is also
310eb982001SEduardo Valentin  * applicable.
311eb982001SEduardo Valentin  *
312eb982001SEduardo Valentin  * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
313eb982001SEduardo Valentin  * @id cannot index @bgp sensors.
314eb982001SEduardo Valentin  */
ti_bandgap_validate(struct ti_bandgap * bgp,int id)315eb982001SEduardo Valentin static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
316eb982001SEduardo Valentin {
317*e98153a8SLi Zetao 	if (IS_ERR_OR_NULL(bgp)) {
318eb982001SEduardo Valentin 		pr_err("%s: invalid bandgap pointer\n", __func__);
319e34238bfSPavel Machek 		return -EINVAL;
320eb982001SEduardo Valentin 	}
321eb982001SEduardo Valentin 
322eb982001SEduardo Valentin 	if ((id < 0) || (id >= bgp->conf->sensor_count)) {
323eb982001SEduardo Valentin 		dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
324eb982001SEduardo Valentin 			__func__, id);
325e34238bfSPavel Machek 		return -ERANGE;
326eb982001SEduardo Valentin 	}
327eb982001SEduardo Valentin 
328e34238bfSPavel Machek 	return 0;
329eb982001SEduardo Valentin }
330eb982001SEduardo Valentin 
331eb982001SEduardo Valentin /**
332eb982001SEduardo Valentin  * ti_bandgap_read_counter() - read the sensor counter
333eb982001SEduardo Valentin  * @bgp: pointer to bandgap instance
334eb982001SEduardo Valentin  * @id: sensor id
335eb982001SEduardo Valentin  * @interval: resulting update interval in miliseconds
336eb982001SEduardo Valentin  */
ti_bandgap_read_counter(struct ti_bandgap * bgp,int id,int * interval)337eb982001SEduardo Valentin static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
338eb982001SEduardo Valentin 				    int *interval)
339eb982001SEduardo Valentin {
340eb982001SEduardo Valentin 	struct temp_sensor_registers *tsr;
341eb982001SEduardo Valentin 	int time;
342eb982001SEduardo Valentin 
343eb982001SEduardo Valentin 	tsr = bgp->conf->sensors[id].registers;
344eb982001SEduardo Valentin 	time = ti_bandgap_readl(bgp, tsr->bgap_counter);
345eb982001SEduardo Valentin 	time = (time & tsr->counter_mask) >>
346eb982001SEduardo Valentin 					__ffs(tsr->counter_mask);
347eb982001SEduardo Valentin 	time = time * 1000 / bgp->clk_rate;
348eb982001SEduardo Valentin 	*interval = time;
349eb982001SEduardo Valentin }
350eb982001SEduardo Valentin 
351eb982001SEduardo Valentin /**
352eb982001SEduardo Valentin  * ti_bandgap_read_counter_delay() - read the sensor counter delay
353eb982001SEduardo Valentin  * @bgp: pointer to bandgap instance
354eb982001SEduardo Valentin  * @id: sensor id
355eb982001SEduardo Valentin  * @interval: resulting update interval in miliseconds
356eb982001SEduardo Valentin  */
ti_bandgap_read_counter_delay(struct ti_bandgap * bgp,int id,int * interval)357eb982001SEduardo Valentin static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
358eb982001SEduardo Valentin 					  int *interval)
359eb982001SEduardo Valentin {
360eb982001SEduardo Valentin 	struct temp_sensor_registers *tsr;
361eb982001SEduardo Valentin 	int reg_val;
362eb982001SEduardo Valentin 
363eb982001SEduardo Valentin 	tsr = bgp->conf->sensors[id].registers;
364eb982001SEduardo Valentin 
365eb982001SEduardo Valentin 	reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
366eb982001SEduardo Valentin 	reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
367eb982001SEduardo Valentin 				__ffs(tsr->mask_counter_delay_mask);
368eb982001SEduardo Valentin 	switch (reg_val) {
369eb982001SEduardo Valentin 	case 0:
370eb982001SEduardo Valentin 		*interval = 0;
371eb982001SEduardo Valentin 		break;
372eb982001SEduardo Valentin 	case 1:
373eb982001SEduardo Valentin 		*interval = 1;
374eb982001SEduardo Valentin 		break;
375eb982001SEduardo Valentin 	case 2:
376eb982001SEduardo Valentin 		*interval = 10;
377eb982001SEduardo Valentin 		break;
378eb982001SEduardo Valentin 	case 3:
379eb982001SEduardo Valentin 		*interval = 100;
380eb982001SEduardo Valentin 		break;
381eb982001SEduardo Valentin 	case 4:
382eb982001SEduardo Valentin 		*interval = 250;
383eb982001SEduardo Valentin 		break;
384eb982001SEduardo Valentin 	case 5:
385eb982001SEduardo Valentin 		*interval = 500;
386eb982001SEduardo Valentin 		break;
387eb982001SEduardo Valentin 	default:
388eb982001SEduardo Valentin 		dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
389eb982001SEduardo Valentin 			 reg_val);
390eb982001SEduardo Valentin 	}
391eb982001SEduardo Valentin }
392eb982001SEduardo Valentin 
393eb982001SEduardo Valentin /**
394eb982001SEduardo Valentin  * ti_bandgap_read_update_interval() - read the sensor update interval
395eb982001SEduardo Valentin  * @bgp: pointer to bandgap instance
396eb982001SEduardo Valentin  * @id: sensor id
397eb982001SEduardo Valentin  * @interval: resulting update interval in miliseconds
398eb982001SEduardo Valentin  *
399eb982001SEduardo Valentin  * Return: 0 on success or the proper error code
400eb982001SEduardo Valentin  */
ti_bandgap_read_update_interval(struct ti_bandgap * bgp,int id,int * interval)401eb982001SEduardo Valentin int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
402eb982001SEduardo Valentin 				    int *interval)
403eb982001SEduardo Valentin {
404eb982001SEduardo Valentin 	int ret = 0;
405eb982001SEduardo Valentin 
406eb982001SEduardo Valentin 	ret = ti_bandgap_validate(bgp, id);
407eb982001SEduardo Valentin 	if (ret)
408eb982001SEduardo Valentin 		goto exit;
409eb982001SEduardo Valentin 
410eb982001SEduardo Valentin 	if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
411eb982001SEduardo Valentin 	    !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
412eb982001SEduardo Valentin 		ret = -ENOTSUPP;
413eb982001SEduardo Valentin 		goto exit;
414eb982001SEduardo Valentin 	}
415eb982001SEduardo Valentin 
416eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, COUNTER)) {
417eb982001SEduardo Valentin 		ti_bandgap_read_counter(bgp, id, interval);
418eb982001SEduardo Valentin 		goto exit;
419eb982001SEduardo Valentin 	}
420eb982001SEduardo Valentin 
421eb982001SEduardo Valentin 	ti_bandgap_read_counter_delay(bgp, id, interval);
422eb982001SEduardo Valentin exit:
423eb982001SEduardo Valentin 	return ret;
424eb982001SEduardo Valentin }
425eb982001SEduardo Valentin 
426eb982001SEduardo Valentin /**
427eb982001SEduardo Valentin  * ti_bandgap_write_counter_delay() - set the counter_delay
428eb982001SEduardo Valentin  * @bgp: pointer to bandgap instance
429eb982001SEduardo Valentin  * @id: sensor id
430eb982001SEduardo Valentin  * @interval: desired update interval in miliseconds
431eb982001SEduardo Valentin  *
432eb982001SEduardo Valentin  * Return: 0 on success or the proper error code
433eb982001SEduardo Valentin  */
ti_bandgap_write_counter_delay(struct ti_bandgap * bgp,int id,u32 interval)434eb982001SEduardo Valentin static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
435eb982001SEduardo Valentin 					  u32 interval)
436eb982001SEduardo Valentin {
437eb982001SEduardo Valentin 	int rval;
438eb982001SEduardo Valentin 
439eb982001SEduardo Valentin 	switch (interval) {
440eb982001SEduardo Valentin 	case 0: /* Immediate conversion */
441eb982001SEduardo Valentin 		rval = 0x0;
442eb982001SEduardo Valentin 		break;
443eb982001SEduardo Valentin 	case 1: /* Conversion after ever 1ms */
444eb982001SEduardo Valentin 		rval = 0x1;
445eb982001SEduardo Valentin 		break;
446eb982001SEduardo Valentin 	case 10: /* Conversion after ever 10ms */
447eb982001SEduardo Valentin 		rval = 0x2;
448eb982001SEduardo Valentin 		break;
449eb982001SEduardo Valentin 	case 100: /* Conversion after ever 100ms */
450eb982001SEduardo Valentin 		rval = 0x3;
451eb982001SEduardo Valentin 		break;
452eb982001SEduardo Valentin 	case 250: /* Conversion after ever 250ms */
453eb982001SEduardo Valentin 		rval = 0x4;
454eb982001SEduardo Valentin 		break;
455eb982001SEduardo Valentin 	case 500: /* Conversion after ever 500ms */
456eb982001SEduardo Valentin 		rval = 0x5;
457eb982001SEduardo Valentin 		break;
458eb982001SEduardo Valentin 	default:
459eb982001SEduardo Valentin 		dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
460eb982001SEduardo Valentin 		return -EINVAL;
461eb982001SEduardo Valentin 	}
462eb982001SEduardo Valentin 
463eb982001SEduardo Valentin 	spin_lock(&bgp->lock);
464eb982001SEduardo Valentin 	RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
465eb982001SEduardo Valentin 	spin_unlock(&bgp->lock);
466eb982001SEduardo Valentin 
467eb982001SEduardo Valentin 	return 0;
468eb982001SEduardo Valentin }
469eb982001SEduardo Valentin 
470eb982001SEduardo Valentin /**
471eb982001SEduardo Valentin  * ti_bandgap_write_counter() - set the bandgap sensor counter
472eb982001SEduardo Valentin  * @bgp: pointer to bandgap instance
473eb982001SEduardo Valentin  * @id: sensor id
474eb982001SEduardo Valentin  * @interval: desired update interval in miliseconds
475eb982001SEduardo Valentin  */
ti_bandgap_write_counter(struct ti_bandgap * bgp,int id,u32 interval)476eb982001SEduardo Valentin static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
477eb982001SEduardo Valentin 				     u32 interval)
478eb982001SEduardo Valentin {
479eb982001SEduardo Valentin 	interval = interval * bgp->clk_rate / 1000;
480eb982001SEduardo Valentin 	spin_lock(&bgp->lock);
481eb982001SEduardo Valentin 	RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
482eb982001SEduardo Valentin 	spin_unlock(&bgp->lock);
483eb982001SEduardo Valentin }
484eb982001SEduardo Valentin 
485eb982001SEduardo Valentin /**
486eb982001SEduardo Valentin  * ti_bandgap_write_update_interval() - set the update interval
487eb982001SEduardo Valentin  * @bgp: pointer to bandgap instance
488eb982001SEduardo Valentin  * @id: sensor id
489eb982001SEduardo Valentin  * @interval: desired update interval in miliseconds
490eb982001SEduardo Valentin  *
491eb982001SEduardo Valentin  * Return: 0 on success or the proper error code
492eb982001SEduardo Valentin  */
ti_bandgap_write_update_interval(struct ti_bandgap * bgp,int id,u32 interval)493eb982001SEduardo Valentin int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
494eb982001SEduardo Valentin 				     int id, u32 interval)
495eb982001SEduardo Valentin {
496eb982001SEduardo Valentin 	int ret = ti_bandgap_validate(bgp, id);
497eb982001SEduardo Valentin 	if (ret)
498eb982001SEduardo Valentin 		goto exit;
499eb982001SEduardo Valentin 
500eb982001SEduardo Valentin 	if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
501eb982001SEduardo Valentin 	    !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
502eb982001SEduardo Valentin 		ret = -ENOTSUPP;
503eb982001SEduardo Valentin 		goto exit;
504eb982001SEduardo Valentin 	}
505eb982001SEduardo Valentin 
506eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, COUNTER)) {
507eb982001SEduardo Valentin 		ti_bandgap_write_counter(bgp, id, interval);
508eb982001SEduardo Valentin 		goto exit;
509eb982001SEduardo Valentin 	}
510eb982001SEduardo Valentin 
511eb982001SEduardo Valentin 	ret = ti_bandgap_write_counter_delay(bgp, id, interval);
512eb982001SEduardo Valentin exit:
513eb982001SEduardo Valentin 	return ret;
514eb982001SEduardo Valentin }
515eb982001SEduardo Valentin 
516eb982001SEduardo Valentin /**
517eb982001SEduardo Valentin  * ti_bandgap_read_temperature() - report current temperature
518eb982001SEduardo Valentin  * @bgp: pointer to bandgap instance
519eb982001SEduardo Valentin  * @id: sensor id
520eb982001SEduardo Valentin  * @temperature: resulting temperature
521eb982001SEduardo Valentin  *
522eb982001SEduardo Valentin  * Return: 0 on success or the proper error code
523eb982001SEduardo Valentin  */
ti_bandgap_read_temperature(struct ti_bandgap * bgp,int id,int * temperature)524eb982001SEduardo Valentin int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
525eb982001SEduardo Valentin 				int *temperature)
526eb982001SEduardo Valentin {
527eb982001SEduardo Valentin 	u32 temp;
528eb982001SEduardo Valentin 	int ret;
529eb982001SEduardo Valentin 
530eb982001SEduardo Valentin 	ret = ti_bandgap_validate(bgp, id);
531eb982001SEduardo Valentin 	if (ret)
532eb982001SEduardo Valentin 		return ret;
533eb982001SEduardo Valentin 
53495d079efSPavel Machek 	if (!TI_BANDGAP_HAS(bgp, MODE_CONFIG)) {
53595d079efSPavel Machek 		ret = ti_bandgap_force_single_read(bgp, id);
53695d079efSPavel Machek 		if (ret)
53795d079efSPavel Machek 			return ret;
53895d079efSPavel Machek 	}
53995d079efSPavel Machek 
540eb982001SEduardo Valentin 	spin_lock(&bgp->lock);
541eb982001SEduardo Valentin 	temp = ti_bandgap_read_temp(bgp, id);
542eb982001SEduardo Valentin 	spin_unlock(&bgp->lock);
543eb982001SEduardo Valentin 
544e34238bfSPavel Machek 	ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
545eb982001SEduardo Valentin 	if (ret)
546eb982001SEduardo Valentin 		return -EIO;
547eb982001SEduardo Valentin 
548eb982001SEduardo Valentin 	*temperature = temp;
549eb982001SEduardo Valentin 
550eb982001SEduardo Valentin 	return 0;
551eb982001SEduardo Valentin }
552eb982001SEduardo Valentin 
553eb982001SEduardo Valentin /**
554eb982001SEduardo Valentin  * ti_bandgap_set_sensor_data() - helper function to store thermal
555eb982001SEduardo Valentin  * framework related data.
556eb982001SEduardo Valentin  * @bgp: pointer to bandgap instance
557eb982001SEduardo Valentin  * @id: sensor id
558eb982001SEduardo Valentin  * @data: thermal framework related data to be stored
559eb982001SEduardo Valentin  *
560eb982001SEduardo Valentin  * Return: 0 on success or the proper error code
561eb982001SEduardo Valentin  */
ti_bandgap_set_sensor_data(struct ti_bandgap * bgp,int id,void * data)562eb982001SEduardo Valentin int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
563eb982001SEduardo Valentin {
564eb982001SEduardo Valentin 	int ret = ti_bandgap_validate(bgp, id);
565eb982001SEduardo Valentin 	if (ret)
566eb982001SEduardo Valentin 		return ret;
567eb982001SEduardo Valentin 
568eb982001SEduardo Valentin 	bgp->regval[id].data = data;
569eb982001SEduardo Valentin 
570eb982001SEduardo Valentin 	return 0;
571eb982001SEduardo Valentin }
572eb982001SEduardo Valentin 
573eb982001SEduardo Valentin /**
574eb982001SEduardo Valentin  * ti_bandgap_get_sensor_data() - helper function to get thermal
575eb982001SEduardo Valentin  * framework related data.
576eb982001SEduardo Valentin  * @bgp: pointer to bandgap instance
577eb982001SEduardo Valentin  * @id: sensor id
578eb982001SEduardo Valentin  *
579eb982001SEduardo Valentin  * Return: data stored by set function with sensor id on success or NULL
580eb982001SEduardo Valentin  */
ti_bandgap_get_sensor_data(struct ti_bandgap * bgp,int id)581eb982001SEduardo Valentin void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
582eb982001SEduardo Valentin {
583eb982001SEduardo Valentin 	int ret = ti_bandgap_validate(bgp, id);
584eb982001SEduardo Valentin 	if (ret)
585eb982001SEduardo Valentin 		return ERR_PTR(ret);
586eb982001SEduardo Valentin 
587eb982001SEduardo Valentin 	return bgp->regval[id].data;
588eb982001SEduardo Valentin }
589eb982001SEduardo Valentin 
590eb982001SEduardo Valentin /***   Helper functions used during device initialization   ***/
591eb982001SEduardo Valentin 
592eb982001SEduardo Valentin /**
593eb982001SEduardo Valentin  * ti_bandgap_force_single_read() - executes 1 single ADC conversion
594eb982001SEduardo Valentin  * @bgp: pointer to struct ti_bandgap
595eb982001SEduardo Valentin  * @id: sensor id which it is desired to read 1 temperature
596eb982001SEduardo Valentin  *
597eb982001SEduardo Valentin  * Used to initialize the conversion state machine and set it to a valid
598eb982001SEduardo Valentin  * state. Called during device initialization and context restore events.
599eb982001SEduardo Valentin  *
600eb982001SEduardo Valentin  * Return: 0
601eb982001SEduardo Valentin  */
602eb982001SEduardo Valentin static int
ti_bandgap_force_single_read(struct ti_bandgap * bgp,int id)603eb982001SEduardo Valentin ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
604eb982001SEduardo Valentin {
605b57b4b4dSTony Lindgren 	struct temp_sensor_registers *tsr = bgp->conf->sensors[id].registers;
606514cbabbSTony Lindgren 	void __iomem *temp_sensor_ctrl = bgp->base + tsr->temp_sensor_ctrl;
607514cbabbSTony Lindgren 	int error;
608514cbabbSTony Lindgren 	u32 val;
609eb982001SEduardo Valentin 
610735c3535STony Lindgren 	/* Select continuous or single conversion mode */
611735c3535STony Lindgren 	if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) {
612735c3535STony Lindgren 		if (TI_BANDGAP_HAS(bgp, CONT_MODE_ONLY))
613735c3535STony Lindgren 			RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 1);
614735c3535STony Lindgren 		else
615eb982001SEduardo Valentin 			RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
616735c3535STony Lindgren 	}
617eb982001SEduardo Valentin 
618b57b4b4dSTony Lindgren 	/* Set Start of Conversion if available */
619b57b4b4dSTony Lindgren 	if (tsr->bgap_soc_mask) {
620eb982001SEduardo Valentin 		RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
621eb982001SEduardo Valentin 
622a4296d19SPavel Machek 		/* Wait for EOCZ going up */
623514cbabbSTony Lindgren 		error = readl_poll_timeout_atomic(temp_sensor_ctrl, val,
624514cbabbSTony Lindgren 						  val & tsr->bgap_eocz_mask,
625514cbabbSTony Lindgren 						  1, 1000);
626514cbabbSTony Lindgren 		if (error)
627514cbabbSTony Lindgren 			dev_warn(bgp->dev, "eocz timed out waiting high\n");
628eb982001SEduardo Valentin 
629b57b4b4dSTony Lindgren 		/* Clear Start of Conversion if available */
630eb982001SEduardo Valentin 		RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
631b57b4b4dSTony Lindgren 	}
632eb982001SEduardo Valentin 
633b57b4b4dSTony Lindgren 	/* Wait for EOCZ going down, always needed even if no bgap_soc_mask */
634514cbabbSTony Lindgren 	error = readl_poll_timeout_atomic(temp_sensor_ctrl, val,
635514cbabbSTony Lindgren 					  !(val & tsr->bgap_eocz_mask),
636514cbabbSTony Lindgren 					  1, 1500);
637514cbabbSTony Lindgren 	if (error)
638514cbabbSTony Lindgren 		dev_warn(bgp->dev, "eocz timed out waiting low\n");
639a4296d19SPavel Machek 
640eb982001SEduardo Valentin 	return 0;
641eb982001SEduardo Valentin }
642eb982001SEduardo Valentin 
643eb982001SEduardo Valentin /**
6448b8656d6SMarkus Elfring  * ti_bandgap_set_continuous_mode() - One time enabling of continuous mode
645eb982001SEduardo Valentin  * @bgp: pointer to struct ti_bandgap
646eb982001SEduardo Valentin  *
647eb982001SEduardo Valentin  * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
648eb982001SEduardo Valentin  * be used for junction temperature monitoring, it is desirable that the
649eb982001SEduardo Valentin  * sensors are operational all the time, so that alerts are generated
650eb982001SEduardo Valentin  * properly.
651eb982001SEduardo Valentin  *
652eb982001SEduardo Valentin  * Return: 0
653eb982001SEduardo Valentin  */
ti_bandgap_set_continuous_mode(struct ti_bandgap * bgp)654eb982001SEduardo Valentin static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
655eb982001SEduardo Valentin {
656eb982001SEduardo Valentin 	int i;
657eb982001SEduardo Valentin 
658eb982001SEduardo Valentin 	for (i = 0; i < bgp->conf->sensor_count; i++) {
659eb982001SEduardo Valentin 		/* Perform a single read just before enabling continuous */
660eb982001SEduardo Valentin 		ti_bandgap_force_single_read(bgp, i);
661eb982001SEduardo Valentin 		RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
662eb982001SEduardo Valentin 	}
663eb982001SEduardo Valentin 
664eb982001SEduardo Valentin 	return 0;
665eb982001SEduardo Valentin }
666eb982001SEduardo Valentin 
667eb982001SEduardo Valentin /**
668eb982001SEduardo Valentin  * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
669eb982001SEduardo Valentin  * @bgp: pointer to struct ti_bandgap
670eb982001SEduardo Valentin  * @id: id of the individual sensor
671eb982001SEduardo Valentin  * @trend: Pointer to trend.
672eb982001SEduardo Valentin  *
673eb982001SEduardo Valentin  * This function needs to be called to fetch the temperature trend of a
674eb982001SEduardo Valentin  * Particular sensor. The function computes the difference in temperature
675eb982001SEduardo Valentin  * w.r.t time. For the bandgaps with built in history buffer the temperatures
676eb982001SEduardo Valentin  * are read from the buffer and for those without the Buffer -ENOTSUPP is
677eb982001SEduardo Valentin  * returned.
678eb982001SEduardo Valentin  *
679eb982001SEduardo Valentin  * Return: 0 if no error, else return corresponding error. If no
680eb982001SEduardo Valentin  *		error then the trend value is passed on to trend parameter
681eb982001SEduardo Valentin  */
ti_bandgap_get_trend(struct ti_bandgap * bgp,int id,int * trend)682eb982001SEduardo Valentin int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
683eb982001SEduardo Valentin {
684eb982001SEduardo Valentin 	struct temp_sensor_registers *tsr;
685eb982001SEduardo Valentin 	u32 temp1, temp2, reg1, reg2;
686eb982001SEduardo Valentin 	int t1, t2, interval, ret = 0;
687eb982001SEduardo Valentin 
688eb982001SEduardo Valentin 	ret = ti_bandgap_validate(bgp, id);
689eb982001SEduardo Valentin 	if (ret)
690eb982001SEduardo Valentin 		goto exit;
691eb982001SEduardo Valentin 
692eb982001SEduardo Valentin 	if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
693eb982001SEduardo Valentin 	    !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
694eb982001SEduardo Valentin 		ret = -ENOTSUPP;
695eb982001SEduardo Valentin 		goto exit;
696eb982001SEduardo Valentin 	}
697eb982001SEduardo Valentin 
698ba0049eaSEduardo Valentin 	spin_lock(&bgp->lock);
699ba0049eaSEduardo Valentin 
700eb982001SEduardo Valentin 	tsr = bgp->conf->sensors[id].registers;
701eb982001SEduardo Valentin 
702eb982001SEduardo Valentin 	/* Freeze and read the last 2 valid readings */
703ba0049eaSEduardo Valentin 	RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
704eb982001SEduardo Valentin 	reg1 = tsr->ctrl_dtemp_1;
705eb982001SEduardo Valentin 	reg2 = tsr->ctrl_dtemp_2;
706eb982001SEduardo Valentin 
707eb982001SEduardo Valentin 	/* read temperature from history buffer */
708eb982001SEduardo Valentin 	temp1 = ti_bandgap_readl(bgp, reg1);
709eb982001SEduardo Valentin 	temp1 &= tsr->bgap_dtemp_mask;
710eb982001SEduardo Valentin 
711eb982001SEduardo Valentin 	temp2 = ti_bandgap_readl(bgp, reg2);
712eb982001SEduardo Valentin 	temp2 &= tsr->bgap_dtemp_mask;
713eb982001SEduardo Valentin 
714eb982001SEduardo Valentin 	/* Convert from adc values to mCelsius temperature */
715eb982001SEduardo Valentin 	ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
716eb982001SEduardo Valentin 	if (ret)
717ba0049eaSEduardo Valentin 		goto unfreeze;
718eb982001SEduardo Valentin 
719eb982001SEduardo Valentin 	ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
720eb982001SEduardo Valentin 	if (ret)
721ba0049eaSEduardo Valentin 		goto unfreeze;
722eb982001SEduardo Valentin 
723eb982001SEduardo Valentin 	/* Fetch the update interval */
724eb982001SEduardo Valentin 	ret = ti_bandgap_read_update_interval(bgp, id, &interval);
725e838ff81SRanganath Krishnan 	if (ret)
726ba0049eaSEduardo Valentin 		goto unfreeze;
727eb982001SEduardo Valentin 
728e838ff81SRanganath Krishnan 	/* Set the interval to 1 ms if bandgap counter delay is not set */
729e838ff81SRanganath Krishnan 	if (interval == 0)
730e838ff81SRanganath Krishnan 		interval = 1;
731e838ff81SRanganath Krishnan 
732eb982001SEduardo Valentin 	*trend = (t1 - t2) / interval;
733eb982001SEduardo Valentin 
734eb982001SEduardo Valentin 	dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
735eb982001SEduardo Valentin 		t1, t2, *trend);
736eb982001SEduardo Valentin 
737ba0049eaSEduardo Valentin unfreeze:
738ba0049eaSEduardo Valentin 	RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
739ba0049eaSEduardo Valentin 	spin_unlock(&bgp->lock);
740eb982001SEduardo Valentin exit:
741eb982001SEduardo Valentin 	return ret;
742eb982001SEduardo Valentin }
743eb982001SEduardo Valentin 
744eb982001SEduardo Valentin /**
745eb982001SEduardo Valentin  * ti_bandgap_tshut_init() - setup and initialize tshut handling
746eb982001SEduardo Valentin  * @bgp: pointer to struct ti_bandgap
747eb982001SEduardo Valentin  * @pdev: pointer to device struct platform_device
748eb982001SEduardo Valentin  *
749eb982001SEduardo Valentin  * Call this function only in case the bandgap features HAS(TSHUT).
750eb982001SEduardo Valentin  * In this case, the driver needs to handle the TSHUT signal as an IRQ.
751eb982001SEduardo Valentin  * The IRQ is wired as a GPIO, and for this purpose, it is required
752eb982001SEduardo Valentin  * to specify which GPIO line is used. TSHUT IRQ is fired anytime
753eb982001SEduardo Valentin  * one of the bandgap sensors violates the TSHUT high/hot threshold.
754eb982001SEduardo Valentin  * And in that case, the system must go off.
755eb982001SEduardo Valentin  *
756eb982001SEduardo Valentin  * Return: 0 if no error, else error status
757eb982001SEduardo Valentin  */
ti_bandgap_tshut_init(struct ti_bandgap * bgp,struct platform_device * pdev)758eb982001SEduardo Valentin static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
759eb982001SEduardo Valentin 				 struct platform_device *pdev)
760eb982001SEduardo Valentin {
761eb982001SEduardo Valentin 	int status;
762eb982001SEduardo Valentin 
7637375f2acSLinus Walleij 	status = request_irq(gpiod_to_irq(bgp->tshut_gpiod),
7647375f2acSLinus Walleij 			     ti_bandgap_tshut_irq_handler,
765eb982001SEduardo Valentin 			     IRQF_TRIGGER_RISING, "tshut", NULL);
7667375f2acSLinus Walleij 	if (status)
767eb982001SEduardo Valentin 		dev_err(bgp->dev, "request irq failed for TSHUT");
768eb982001SEduardo Valentin 
769eb982001SEduardo Valentin 	return 0;
770eb982001SEduardo Valentin }
771eb982001SEduardo Valentin 
772eb982001SEduardo Valentin /**
7738d84733dSYang Li  * ti_bandgap_talert_init() - setup and initialize talert handling
774eb982001SEduardo Valentin  * @bgp: pointer to struct ti_bandgap
775eb982001SEduardo Valentin  * @pdev: pointer to device struct platform_device
776eb982001SEduardo Valentin  *
777eb982001SEduardo Valentin  * Call this function only in case the bandgap features HAS(TALERT).
778eb982001SEduardo Valentin  * In this case, the driver needs to handle the TALERT signals as an IRQs.
779eb982001SEduardo Valentin  * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
780eb982001SEduardo Valentin  * are violated. In these situation, the driver must reprogram the thresholds,
781eb982001SEduardo Valentin  * accordingly to specified policy.
782eb982001SEduardo Valentin  *
783eb982001SEduardo Valentin  * Return: 0 if no error, else return corresponding error.
784eb982001SEduardo Valentin  */
ti_bandgap_talert_init(struct ti_bandgap * bgp,struct platform_device * pdev)785eb982001SEduardo Valentin static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
786eb982001SEduardo Valentin 				  struct platform_device *pdev)
787eb982001SEduardo Valentin {
788eb982001SEduardo Valentin 	int ret;
789eb982001SEduardo Valentin 
790eb982001SEduardo Valentin 	bgp->irq = platform_get_irq(pdev, 0);
7918cb775bbSMarkus Elfring 	if (bgp->irq < 0)
792eb982001SEduardo Valentin 		return bgp->irq;
7938cb775bbSMarkus Elfring 
794eb982001SEduardo Valentin 	ret = request_threaded_irq(bgp->irq, NULL,
795eb982001SEduardo Valentin 				   ti_bandgap_talert_irq_handler,
796eb982001SEduardo Valentin 				   IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
797eb982001SEduardo Valentin 				   "talert", bgp);
798eb982001SEduardo Valentin 	if (ret) {
799eb982001SEduardo Valentin 		dev_err(&pdev->dev, "Request threaded irq failed.\n");
800eb982001SEduardo Valentin 		return ret;
801eb982001SEduardo Valentin 	}
802eb982001SEduardo Valentin 
803eb982001SEduardo Valentin 	return 0;
804eb982001SEduardo Valentin }
805eb982001SEduardo Valentin 
806eb982001SEduardo Valentin static const struct of_device_id of_ti_bandgap_match[];
807eb982001SEduardo Valentin /**
808eb982001SEduardo Valentin  * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
809eb982001SEduardo Valentin  * @pdev: pointer to device struct platform_device
810eb982001SEduardo Valentin  *
811eb982001SEduardo Valentin  * Used to read the device tree properties accordingly to the bandgap
812eb982001SEduardo Valentin  * matching version. Based on bandgap version and its capabilities it
813eb982001SEduardo Valentin  * will build a struct ti_bandgap out of the required DT entries.
814eb982001SEduardo Valentin  *
815eb982001SEduardo Valentin  * Return: valid bandgap structure if successful, else returns ERR_PTR
816eb982001SEduardo Valentin  * return value must be verified with IS_ERR.
817eb982001SEduardo Valentin  */
ti_bandgap_build(struct platform_device * pdev)818eb982001SEduardo Valentin static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
819eb982001SEduardo Valentin {
820eb982001SEduardo Valentin 	struct device_node *node = pdev->dev.of_node;
821eb982001SEduardo Valentin 	const struct of_device_id *of_id;
822eb982001SEduardo Valentin 	struct ti_bandgap *bgp;
823eb982001SEduardo Valentin 	struct resource *res;
824eb982001SEduardo Valentin 	int i;
825eb982001SEduardo Valentin 
826eb982001SEduardo Valentin 	/* just for the sake */
827eb982001SEduardo Valentin 	if (!node) {
828eb982001SEduardo Valentin 		dev_err(&pdev->dev, "no platform information available\n");
829eb982001SEduardo Valentin 		return ERR_PTR(-EINVAL);
830eb982001SEduardo Valentin 	}
831eb982001SEduardo Valentin 
832eb982001SEduardo Valentin 	bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
83357e52115SMarkus Elfring 	if (!bgp)
834eb982001SEduardo Valentin 		return ERR_PTR(-ENOMEM);
835eb982001SEduardo Valentin 
836eb982001SEduardo Valentin 	of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
837eb982001SEduardo Valentin 	if (of_id)
838eb982001SEduardo Valentin 		bgp->conf = of_id->data;
839eb982001SEduardo Valentin 
840eb982001SEduardo Valentin 	/* register shadow for context save and restore */
841748c23d8SMarkus Elfring 	bgp->regval = devm_kcalloc(&pdev->dev, bgp->conf->sensor_count,
842748c23d8SMarkus Elfring 				   sizeof(*bgp->regval), GFP_KERNEL);
84357e52115SMarkus Elfring 	if (!bgp->regval)
844eb982001SEduardo Valentin 		return ERR_PTR(-ENOMEM);
845eb982001SEduardo Valentin 
846eb982001SEduardo Valentin 	i = 0;
847eb982001SEduardo Valentin 	do {
848eb982001SEduardo Valentin 		void __iomem *chunk;
849eb982001SEduardo Valentin 
850eb982001SEduardo Valentin 		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
851eb982001SEduardo Valentin 		if (!res)
852eb982001SEduardo Valentin 			break;
853eb982001SEduardo Valentin 		chunk = devm_ioremap_resource(&pdev->dev, res);
854eb982001SEduardo Valentin 		if (i == 0)
855eb982001SEduardo Valentin 			bgp->base = chunk;
856eb982001SEduardo Valentin 		if (IS_ERR(chunk))
857eb982001SEduardo Valentin 			return ERR_CAST(chunk);
858eb982001SEduardo Valentin 
859eb982001SEduardo Valentin 		i++;
860eb982001SEduardo Valentin 	} while (res);
861eb982001SEduardo Valentin 
862eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, TSHUT)) {
8637375f2acSLinus Walleij 		bgp->tshut_gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_IN);
8647375f2acSLinus Walleij 		if (IS_ERR(bgp->tshut_gpiod)) {
8657375f2acSLinus Walleij 			dev_err(&pdev->dev, "invalid gpio for tshut\n");
8667375f2acSLinus Walleij 			return ERR_CAST(bgp->tshut_gpiod);
867eb982001SEduardo Valentin 		}
868eb982001SEduardo Valentin 	}
869eb982001SEduardo Valentin 
870eb982001SEduardo Valentin 	return bgp;
871eb982001SEduardo Valentin }
872eb982001SEduardo Valentin 
873b98467feSPeter Ujfalusi /*
874b98467feSPeter Ujfalusi  * List of SoCs on which the CPU PM notifier can cause erros on the DTEMP
875b98467feSPeter Ujfalusi  * readout.
876b98467feSPeter Ujfalusi  * Enabled notifier on these machines results in erroneous, random values which
877b98467feSPeter Ujfalusi  * could trigger unexpected thermal shutdown.
878b98467feSPeter Ujfalusi  */
879b98467feSPeter Ujfalusi static const struct soc_device_attribute soc_no_cpu_notifier[] = {
880b98467feSPeter Ujfalusi 	{ .machine = "OMAP4430" },
8813f9cb579SGeert Uytterhoeven 	{ /* sentinel */ }
882b98467feSPeter Ujfalusi };
883b98467feSPeter Ujfalusi 
884eb982001SEduardo Valentin /***   Device driver call backs   ***/
885eb982001SEduardo Valentin 
886eb982001SEduardo Valentin static
ti_bandgap_probe(struct platform_device * pdev)887eb982001SEduardo Valentin int ti_bandgap_probe(struct platform_device *pdev)
888eb982001SEduardo Valentin {
889eb982001SEduardo Valentin 	struct ti_bandgap *bgp;
89013369194SDan Carpenter 	int clk_rate, ret, i;
891eb982001SEduardo Valentin 
892eb982001SEduardo Valentin 	bgp = ti_bandgap_build(pdev);
8930c12b5acSEduardo Valentin 	if (IS_ERR(bgp)) {
894eb982001SEduardo Valentin 		dev_err(&pdev->dev, "failed to fetch platform data\n");
895eb982001SEduardo Valentin 		return PTR_ERR(bgp);
896eb982001SEduardo Valentin 	}
897eb982001SEduardo Valentin 	bgp->dev = &pdev->dev;
898eb982001SEduardo Valentin 
8999c5c87e5SPavel Machek 	if (TI_BANDGAP_HAS(bgp, UNRELIABLE))
9009c5c87e5SPavel Machek 		dev_warn(&pdev->dev,
9019c5c87e5SPavel Machek 			 "This OMAP thermal sensor is unreliable. You've been warned\n");
9029c5c87e5SPavel Machek 
903eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, TSHUT)) {
904eb982001SEduardo Valentin 		ret = ti_bandgap_tshut_init(bgp, pdev);
905eb982001SEduardo Valentin 		if (ret) {
906eb982001SEduardo Valentin 			dev_err(&pdev->dev,
907eb982001SEduardo Valentin 				"failed to initialize system tshut IRQ\n");
908eb982001SEduardo Valentin 			return ret;
909eb982001SEduardo Valentin 		}
910eb982001SEduardo Valentin 	}
911eb982001SEduardo Valentin 
912eb982001SEduardo Valentin 	bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
91313369194SDan Carpenter 	if (IS_ERR(bgp->fclock)) {
914eb982001SEduardo Valentin 		dev_err(&pdev->dev, "failed to request fclock reference\n");
9150c12b5acSEduardo Valentin 		ret = PTR_ERR(bgp->fclock);
916eb982001SEduardo Valentin 		goto free_irqs;
917eb982001SEduardo Valentin 	}
918eb982001SEduardo Valentin 
919eb982001SEduardo Valentin 	bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
92013369194SDan Carpenter 	if (IS_ERR(bgp->div_clk)) {
921e34238bfSPavel Machek 		dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n");
9220c12b5acSEduardo Valentin 		ret = PTR_ERR(bgp->div_clk);
923882f5815SLuis Henriques 		goto put_fclock;
924eb982001SEduardo Valentin 	}
925eb982001SEduardo Valentin 
926eb982001SEduardo Valentin 	for (i = 0; i < bgp->conf->sensor_count; i++) {
927eb982001SEduardo Valentin 		struct temp_sensor_registers *tsr;
928eb982001SEduardo Valentin 		u32 val;
929eb982001SEduardo Valentin 
930eb982001SEduardo Valentin 		tsr = bgp->conf->sensors[i].registers;
931eb982001SEduardo Valentin 		/*
932eb982001SEduardo Valentin 		 * check if the efuse has a non-zero value if not
933eb982001SEduardo Valentin 		 * it is an untrimmed sample and the temperatures
934eb982001SEduardo Valentin 		 * may not be accurate
935eb982001SEduardo Valentin 		 */
936eb982001SEduardo Valentin 		val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
93713369194SDan Carpenter 		if (!val)
938eb982001SEduardo Valentin 			dev_info(&pdev->dev,
939eb982001SEduardo Valentin 				 "Non-trimmed BGAP, Temp not accurate\n");
940eb982001SEduardo Valentin 	}
941eb982001SEduardo Valentin 
942eb982001SEduardo Valentin 	clk_rate = clk_round_rate(bgp->div_clk,
943eb982001SEduardo Valentin 				  bgp->conf->sensors[0].ts_data->max_freq);
944eb982001SEduardo Valentin 	if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
945c68789e5SPaul Walmsley 	    clk_rate <= 0) {
946eb982001SEduardo Valentin 		ret = -ENODEV;
947eb982001SEduardo Valentin 		dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
948eb982001SEduardo Valentin 		goto put_clks;
949eb982001SEduardo Valentin 	}
950eb982001SEduardo Valentin 
951eb982001SEduardo Valentin 	ret = clk_set_rate(bgp->div_clk, clk_rate);
952eb982001SEduardo Valentin 	if (ret)
953eb982001SEduardo Valentin 		dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
954eb982001SEduardo Valentin 
955eb982001SEduardo Valentin 	bgp->clk_rate = clk_rate;
956eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
957eb982001SEduardo Valentin 		clk_prepare_enable(bgp->fclock);
958eb982001SEduardo Valentin 
959eb982001SEduardo Valentin 
960eb982001SEduardo Valentin 	spin_lock_init(&bgp->lock);
961eb982001SEduardo Valentin 	bgp->dev = &pdev->dev;
962eb982001SEduardo Valentin 	platform_set_drvdata(pdev, bgp);
963eb982001SEduardo Valentin 
964eb982001SEduardo Valentin 	ti_bandgap_power(bgp, true);
965eb982001SEduardo Valentin 
966eb982001SEduardo Valentin 	/* Set default counter to 1 for now */
967eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, COUNTER))
968eb982001SEduardo Valentin 		for (i = 0; i < bgp->conf->sensor_count; i++)
969eb982001SEduardo Valentin 			RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
970eb982001SEduardo Valentin 
971eb982001SEduardo Valentin 	/* Set default thresholds for alert and shutdown */
972eb982001SEduardo Valentin 	for (i = 0; i < bgp->conf->sensor_count; i++) {
973eb982001SEduardo Valentin 		struct temp_sensor_data *ts_data;
974eb982001SEduardo Valentin 
975eb982001SEduardo Valentin 		ts_data = bgp->conf->sensors[i].ts_data;
976eb982001SEduardo Valentin 
977eb982001SEduardo Valentin 		if (TI_BANDGAP_HAS(bgp, TALERT)) {
978eb982001SEduardo Valentin 			/* Set initial Talert thresholds */
979eb982001SEduardo Valentin 			RMW_BITS(bgp, i, bgap_threshold,
980eb982001SEduardo Valentin 				 threshold_tcold_mask, ts_data->t_cold);
981eb982001SEduardo Valentin 			RMW_BITS(bgp, i, bgap_threshold,
982eb982001SEduardo Valentin 				 threshold_thot_mask, ts_data->t_hot);
983eb982001SEduardo Valentin 			/* Enable the alert events */
984eb982001SEduardo Valentin 			RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
985eb982001SEduardo Valentin 			RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
986eb982001SEduardo Valentin 		}
987eb982001SEduardo Valentin 
988eb982001SEduardo Valentin 		if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
989eb982001SEduardo Valentin 			/* Set initial Tshut thresholds */
990eb982001SEduardo Valentin 			RMW_BITS(bgp, i, tshut_threshold,
991eb982001SEduardo Valentin 				 tshut_hot_mask, ts_data->tshut_hot);
992eb982001SEduardo Valentin 			RMW_BITS(bgp, i, tshut_threshold,
993eb982001SEduardo Valentin 				 tshut_cold_mask, ts_data->tshut_cold);
994eb982001SEduardo Valentin 		}
995eb982001SEduardo Valentin 	}
996eb982001SEduardo Valentin 
997eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
998eb982001SEduardo Valentin 		ti_bandgap_set_continuous_mode(bgp);
999eb982001SEduardo Valentin 
1000eb982001SEduardo Valentin 	/* Set .250 seconds time as default counter */
1001eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, COUNTER))
1002eb982001SEduardo Valentin 		for (i = 0; i < bgp->conf->sensor_count; i++)
1003eb982001SEduardo Valentin 			RMW_BITS(bgp, i, bgap_counter, counter_mask,
1004eb982001SEduardo Valentin 				 bgp->clk_rate / 4);
1005eb982001SEduardo Valentin 
1006eb982001SEduardo Valentin 	/* Every thing is good? Then expose the sensors */
1007eb982001SEduardo Valentin 	for (i = 0; i < bgp->conf->sensor_count; i++) {
1008eb982001SEduardo Valentin 		char *domain;
1009eb982001SEduardo Valentin 
1010eb982001SEduardo Valentin 		if (bgp->conf->sensors[i].register_cooling) {
1011eb982001SEduardo Valentin 			ret = bgp->conf->sensors[i].register_cooling(bgp, i);
1012eb982001SEduardo Valentin 			if (ret)
1013eb982001SEduardo Valentin 				goto remove_sensors;
1014eb982001SEduardo Valentin 		}
1015eb982001SEduardo Valentin 
1016eb982001SEduardo Valentin 		if (bgp->conf->expose_sensor) {
1017eb982001SEduardo Valentin 			domain = bgp->conf->sensors[i].domain;
1018eb982001SEduardo Valentin 			ret = bgp->conf->expose_sensor(bgp, i, domain);
1019eb982001SEduardo Valentin 			if (ret)
1020eb982001SEduardo Valentin 				goto remove_last_cooling;
1021eb982001SEduardo Valentin 		}
1022eb982001SEduardo Valentin 	}
1023eb982001SEduardo Valentin 
1024eb982001SEduardo Valentin 	/*
1025eb982001SEduardo Valentin 	 * Enable the Interrupts once everything is set. Otherwise irq handler
1026eb982001SEduardo Valentin 	 * might be called as soon as it is enabled where as rest of framework
1027eb982001SEduardo Valentin 	 * is still getting initialised.
1028eb982001SEduardo Valentin 	 */
1029eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, TALERT)) {
1030eb982001SEduardo Valentin 		ret = ti_bandgap_talert_init(bgp, pdev);
1031eb982001SEduardo Valentin 		if (ret) {
1032eb982001SEduardo Valentin 			dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1033eb982001SEduardo Valentin 			i = bgp->conf->sensor_count;
1034eb982001SEduardo Valentin 			goto disable_clk;
1035eb982001SEduardo Valentin 		}
1036eb982001SEduardo Valentin 	}
1037eb982001SEduardo Valentin 
10385093402eSAdam Ford #ifdef CONFIG_PM_SLEEP
10395093402eSAdam Ford 	bgp->nb.notifier_call = bandgap_omap_cpu_notifier;
1040b98467feSPeter Ujfalusi 	if (!soc_device_match(soc_no_cpu_notifier))
10415093402eSAdam Ford 		cpu_pm_register_notifier(&bgp->nb);
10425093402eSAdam Ford #endif
10435093402eSAdam Ford 
1044eb982001SEduardo Valentin 	return 0;
1045eb982001SEduardo Valentin 
1046eb982001SEduardo Valentin remove_last_cooling:
1047eb982001SEduardo Valentin 	if (bgp->conf->sensors[i].unregister_cooling)
1048eb982001SEduardo Valentin 		bgp->conf->sensors[i].unregister_cooling(bgp, i);
1049eb982001SEduardo Valentin remove_sensors:
1050eb982001SEduardo Valentin 	for (i--; i >= 0; i--) {
1051eb982001SEduardo Valentin 		if (bgp->conf->sensors[i].unregister_cooling)
1052eb982001SEduardo Valentin 			bgp->conf->sensors[i].unregister_cooling(bgp, i);
1053eb982001SEduardo Valentin 		if (bgp->conf->remove_sensor)
1054eb982001SEduardo Valentin 			bgp->conf->remove_sensor(bgp, i);
1055eb982001SEduardo Valentin 	}
1056eb982001SEduardo Valentin 	ti_bandgap_power(bgp, false);
1057eb982001SEduardo Valentin disable_clk:
1058eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1059eb982001SEduardo Valentin 		clk_disable_unprepare(bgp->fclock);
1060eb982001SEduardo Valentin put_clks:
1061eb982001SEduardo Valentin 	clk_put(bgp->div_clk);
1062882f5815SLuis Henriques put_fclock:
1063882f5815SLuis Henriques 	clk_put(bgp->fclock);
1064eb982001SEduardo Valentin free_irqs:
10657375f2acSLinus Walleij 	if (TI_BANDGAP_HAS(bgp, TSHUT))
10667375f2acSLinus Walleij 		free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL);
1067eb982001SEduardo Valentin 
1068eb982001SEduardo Valentin 	return ret;
1069eb982001SEduardo Valentin }
1070eb982001SEduardo Valentin 
1071eb982001SEduardo Valentin static
ti_bandgap_remove(struct platform_device * pdev)1072eb982001SEduardo Valentin int ti_bandgap_remove(struct platform_device *pdev)
1073eb982001SEduardo Valentin {
1074eb982001SEduardo Valentin 	struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1075eb982001SEduardo Valentin 	int i;
1076eb982001SEduardo Valentin 
1077b98467feSPeter Ujfalusi 	if (!soc_device_match(soc_no_cpu_notifier))
10785093402eSAdam Ford 		cpu_pm_unregister_notifier(&bgp->nb);
10795093402eSAdam Ford 
10805093402eSAdam Ford 	/* Remove sensor interfaces */
1081eb982001SEduardo Valentin 	for (i = 0; i < bgp->conf->sensor_count; i++) {
1082eb982001SEduardo Valentin 		if (bgp->conf->sensors[i].unregister_cooling)
1083eb982001SEduardo Valentin 			bgp->conf->sensors[i].unregister_cooling(bgp, i);
1084eb982001SEduardo Valentin 
1085eb982001SEduardo Valentin 		if (bgp->conf->remove_sensor)
1086eb982001SEduardo Valentin 			bgp->conf->remove_sensor(bgp, i);
1087eb982001SEduardo Valentin 	}
1088eb982001SEduardo Valentin 
1089eb982001SEduardo Valentin 	ti_bandgap_power(bgp, false);
1090eb982001SEduardo Valentin 
1091eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1092eb982001SEduardo Valentin 		clk_disable_unprepare(bgp->fclock);
1093eb982001SEduardo Valentin 	clk_put(bgp->fclock);
1094eb982001SEduardo Valentin 	clk_put(bgp->div_clk);
1095eb982001SEduardo Valentin 
1096eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, TALERT))
1097eb982001SEduardo Valentin 		free_irq(bgp->irq, bgp);
1098eb982001SEduardo Valentin 
10997375f2acSLinus Walleij 	if (TI_BANDGAP_HAS(bgp, TSHUT))
11007375f2acSLinus Walleij 		free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL);
1101eb982001SEduardo Valentin 
1102eb982001SEduardo Valentin 	return 0;
1103eb982001SEduardo Valentin }
1104eb982001SEduardo Valentin 
11053992b62dSGrygorii Strashko #ifdef CONFIG_PM_SLEEP
ti_bandgap_save_ctxt(struct ti_bandgap * bgp)1106eb982001SEduardo Valentin static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1107eb982001SEduardo Valentin {
1108eb982001SEduardo Valentin 	int i;
1109eb982001SEduardo Valentin 
1110eb982001SEduardo Valentin 	for (i = 0; i < bgp->conf->sensor_count; i++) {
1111eb982001SEduardo Valentin 		struct temp_sensor_registers *tsr;
1112eb982001SEduardo Valentin 		struct temp_sensor_regval *rval;
1113eb982001SEduardo Valentin 
1114eb982001SEduardo Valentin 		rval = &bgp->regval[i];
1115eb982001SEduardo Valentin 		tsr = bgp->conf->sensors[i].registers;
1116eb982001SEduardo Valentin 
1117eb982001SEduardo Valentin 		if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1118eb982001SEduardo Valentin 			rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1119eb982001SEduardo Valentin 							tsr->bgap_mode_ctrl);
1120eb982001SEduardo Valentin 		if (TI_BANDGAP_HAS(bgp, COUNTER))
1121eb982001SEduardo Valentin 			rval->bg_counter = ti_bandgap_readl(bgp,
1122eb982001SEduardo Valentin 							tsr->bgap_counter);
1123eb982001SEduardo Valentin 		if (TI_BANDGAP_HAS(bgp, TALERT)) {
1124eb982001SEduardo Valentin 			rval->bg_threshold = ti_bandgap_readl(bgp,
1125eb982001SEduardo Valentin 							tsr->bgap_threshold);
1126eb982001SEduardo Valentin 			rval->bg_ctrl = ti_bandgap_readl(bgp,
1127eb982001SEduardo Valentin 						   tsr->bgap_mask_ctrl);
1128eb982001SEduardo Valentin 		}
1129eb982001SEduardo Valentin 
1130eb982001SEduardo Valentin 		if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1131eb982001SEduardo Valentin 			rval->tshut_threshold = ti_bandgap_readl(bgp,
1132eb982001SEduardo Valentin 						   tsr->tshut_threshold);
1133eb982001SEduardo Valentin 	}
1134eb982001SEduardo Valentin 
1135eb982001SEduardo Valentin 	return 0;
1136eb982001SEduardo Valentin }
1137eb982001SEduardo Valentin 
ti_bandgap_restore_ctxt(struct ti_bandgap * bgp)1138eb982001SEduardo Valentin static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1139eb982001SEduardo Valentin {
1140eb982001SEduardo Valentin 	int i;
1141eb982001SEduardo Valentin 
1142eb982001SEduardo Valentin 	for (i = 0; i < bgp->conf->sensor_count; i++) {
1143eb982001SEduardo Valentin 		struct temp_sensor_registers *tsr;
1144eb982001SEduardo Valentin 		struct temp_sensor_regval *rval;
1145eb982001SEduardo Valentin 
1146eb982001SEduardo Valentin 		rval = &bgp->regval[i];
1147eb982001SEduardo Valentin 		tsr = bgp->conf->sensors[i].registers;
1148eb982001SEduardo Valentin 
1149eb982001SEduardo Valentin 		if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1150eb982001SEduardo Valentin 			ti_bandgap_writel(bgp, rval->tshut_threshold,
1151eb982001SEduardo Valentin 					  tsr->tshut_threshold);
1152eb982001SEduardo Valentin 		/* Force immediate temperature measurement and update
1153eb982001SEduardo Valentin 		 * of the DTEMP field
1154eb982001SEduardo Valentin 		 */
1155eb982001SEduardo Valentin 		ti_bandgap_force_single_read(bgp, i);
1156eb982001SEduardo Valentin 
1157eb982001SEduardo Valentin 		if (TI_BANDGAP_HAS(bgp, COUNTER))
1158eb982001SEduardo Valentin 			ti_bandgap_writel(bgp, rval->bg_counter,
1159eb982001SEduardo Valentin 					  tsr->bgap_counter);
1160eb982001SEduardo Valentin 		if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1161eb982001SEduardo Valentin 			ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1162eb982001SEduardo Valentin 					  tsr->bgap_mode_ctrl);
1163eb982001SEduardo Valentin 		if (TI_BANDGAP_HAS(bgp, TALERT)) {
1164eb982001SEduardo Valentin 			ti_bandgap_writel(bgp, rval->bg_threshold,
1165eb982001SEduardo Valentin 					  tsr->bgap_threshold);
1166eb982001SEduardo Valentin 			ti_bandgap_writel(bgp, rval->bg_ctrl,
1167eb982001SEduardo Valentin 					  tsr->bgap_mask_ctrl);
1168eb982001SEduardo Valentin 		}
1169eb982001SEduardo Valentin 	}
1170eb982001SEduardo Valentin 
1171eb982001SEduardo Valentin 	return 0;
1172eb982001SEduardo Valentin }
1173eb982001SEduardo Valentin 
ti_bandgap_suspend(struct device * dev)1174eb982001SEduardo Valentin static int ti_bandgap_suspend(struct device *dev)
1175eb982001SEduardo Valentin {
1176eb982001SEduardo Valentin 	struct ti_bandgap *bgp = dev_get_drvdata(dev);
1177eb982001SEduardo Valentin 	int err;
1178eb982001SEduardo Valentin 
1179eb982001SEduardo Valentin 	err = ti_bandgap_save_ctxt(bgp);
1180eb982001SEduardo Valentin 	ti_bandgap_power(bgp, false);
1181eb982001SEduardo Valentin 
1182eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1183eb982001SEduardo Valentin 		clk_disable_unprepare(bgp->fclock);
1184eb982001SEduardo Valentin 
11855093402eSAdam Ford 	bgp->is_suspended = true;
11865093402eSAdam Ford 
1187eb982001SEduardo Valentin 	return err;
1188eb982001SEduardo Valentin }
1189eb982001SEduardo Valentin 
bandgap_omap_cpu_notifier(struct notifier_block * nb,unsigned long cmd,void * v)11905093402eSAdam Ford static int bandgap_omap_cpu_notifier(struct notifier_block *nb,
11915093402eSAdam Ford 				  unsigned long cmd, void *v)
11925093402eSAdam Ford {
11935093402eSAdam Ford 	struct ti_bandgap *bgp;
11945093402eSAdam Ford 
11955093402eSAdam Ford 	bgp = container_of(nb, struct ti_bandgap, nb);
11965093402eSAdam Ford 
11975093402eSAdam Ford 	spin_lock(&bgp->lock);
11985093402eSAdam Ford 	switch (cmd) {
11995093402eSAdam Ford 	case CPU_CLUSTER_PM_ENTER:
12005093402eSAdam Ford 		if (bgp->is_suspended)
12015093402eSAdam Ford 			break;
12025093402eSAdam Ford 		ti_bandgap_save_ctxt(bgp);
12035093402eSAdam Ford 		ti_bandgap_power(bgp, false);
12045093402eSAdam Ford 		if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
12055093402eSAdam Ford 			clk_disable(bgp->fclock);
12065093402eSAdam Ford 		break;
12075093402eSAdam Ford 	case CPU_CLUSTER_PM_ENTER_FAILED:
12085093402eSAdam Ford 	case CPU_CLUSTER_PM_EXIT:
12095093402eSAdam Ford 		if (bgp->is_suspended)
12105093402eSAdam Ford 			break;
12115093402eSAdam Ford 		if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
12125093402eSAdam Ford 			clk_enable(bgp->fclock);
12135093402eSAdam Ford 		ti_bandgap_power(bgp, true);
12145093402eSAdam Ford 		ti_bandgap_restore_ctxt(bgp);
12155093402eSAdam Ford 		break;
12165093402eSAdam Ford 	}
12175093402eSAdam Ford 	spin_unlock(&bgp->lock);
12185093402eSAdam Ford 
12195093402eSAdam Ford 	return NOTIFY_OK;
12205093402eSAdam Ford }
12215093402eSAdam Ford 
ti_bandgap_resume(struct device * dev)1222eb982001SEduardo Valentin static int ti_bandgap_resume(struct device *dev)
1223eb982001SEduardo Valentin {
1224eb982001SEduardo Valentin 	struct ti_bandgap *bgp = dev_get_drvdata(dev);
1225eb982001SEduardo Valentin 
1226eb982001SEduardo Valentin 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1227eb982001SEduardo Valentin 		clk_prepare_enable(bgp->fclock);
1228eb982001SEduardo Valentin 
1229eb982001SEduardo Valentin 	ti_bandgap_power(bgp, true);
12305093402eSAdam Ford 	bgp->is_suspended = false;
1231eb982001SEduardo Valentin 
1232eb982001SEduardo Valentin 	return ti_bandgap_restore_ctxt(bgp);
1233eb982001SEduardo Valentin }
12345204f8c0SJingoo Han static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
12355204f8c0SJingoo Han 			 ti_bandgap_resume);
1236eb982001SEduardo Valentin 
1237eb982001SEduardo Valentin #define DEV_PM_OPS	(&ti_bandgap_dev_pm_ops)
1238eb982001SEduardo Valentin #else
1239eb982001SEduardo Valentin #define DEV_PM_OPS	NULL
1240eb982001SEduardo Valentin #endif
1241eb982001SEduardo Valentin 
1242eb982001SEduardo Valentin static const struct of_device_id of_ti_bandgap_match[] = {
12439c5c87e5SPavel Machek #ifdef CONFIG_OMAP3_THERMAL
12449c5c87e5SPavel Machek 	{
12459c5c87e5SPavel Machek 		.compatible = "ti,omap34xx-bandgap",
12469c5c87e5SPavel Machek 		.data = (void *)&omap34xx_data,
12479c5c87e5SPavel Machek 	},
1248b840b6e6SEduardo Valentin 	{
1249b840b6e6SEduardo Valentin 		.compatible = "ti,omap36xx-bandgap",
1250b840b6e6SEduardo Valentin 		.data = (void *)&omap36xx_data,
1251b840b6e6SEduardo Valentin 	},
12529c5c87e5SPavel Machek #endif
1253eb982001SEduardo Valentin #ifdef CONFIG_OMAP4_THERMAL
1254eb982001SEduardo Valentin 	{
1255eb982001SEduardo Valentin 		.compatible = "ti,omap4430-bandgap",
1256eb982001SEduardo Valentin 		.data = (void *)&omap4430_data,
1257eb982001SEduardo Valentin 	},
1258eb982001SEduardo Valentin 	{
1259eb982001SEduardo Valentin 		.compatible = "ti,omap4460-bandgap",
1260eb982001SEduardo Valentin 		.data = (void *)&omap4460_data,
1261eb982001SEduardo Valentin 	},
1262eb982001SEduardo Valentin 	{
1263eb982001SEduardo Valentin 		.compatible = "ti,omap4470-bandgap",
1264eb982001SEduardo Valentin 		.data = (void *)&omap4470_data,
1265eb982001SEduardo Valentin 	},
1266eb982001SEduardo Valentin #endif
1267eb982001SEduardo Valentin #ifdef CONFIG_OMAP5_THERMAL
1268eb982001SEduardo Valentin 	{
1269eb982001SEduardo Valentin 		.compatible = "ti,omap5430-bandgap",
1270eb982001SEduardo Valentin 		.data = (void *)&omap5430_data,
1271eb982001SEduardo Valentin 	},
1272eb982001SEduardo Valentin #endif
127325870e62SEduardo Valentin #ifdef CONFIG_DRA752_THERMAL
127425870e62SEduardo Valentin 	{
127525870e62SEduardo Valentin 		.compatible = "ti,dra752-bandgap",
127625870e62SEduardo Valentin 		.data = (void *)&dra752_data,
127725870e62SEduardo Valentin 	},
127825870e62SEduardo Valentin #endif
1279eb982001SEduardo Valentin 	/* Sentinel */
1280eb982001SEduardo Valentin 	{ },
1281eb982001SEduardo Valentin };
1282eb982001SEduardo Valentin MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1283eb982001SEduardo Valentin 
1284eb982001SEduardo Valentin static struct platform_driver ti_bandgap_sensor_driver = {
1285eb982001SEduardo Valentin 	.probe = ti_bandgap_probe,
1286eb982001SEduardo Valentin 	.remove = ti_bandgap_remove,
1287eb982001SEduardo Valentin 	.driver = {
1288eb982001SEduardo Valentin 			.name = "ti-soc-thermal",
1289eb982001SEduardo Valentin 			.pm = DEV_PM_OPS,
1290eb982001SEduardo Valentin 			.of_match_table	= of_ti_bandgap_match,
1291eb982001SEduardo Valentin 	},
1292eb982001SEduardo Valentin };
1293eb982001SEduardo Valentin 
1294eb982001SEduardo Valentin module_platform_driver(ti_bandgap_sensor_driver);
1295eb982001SEduardo Valentin 
1296eb982001SEduardo Valentin MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1297eb982001SEduardo Valentin MODULE_LICENSE("GPL v2");
1298eb982001SEduardo Valentin MODULE_ALIAS("platform:ti-soc-thermal");
1299eb982001SEduardo Valentin MODULE_AUTHOR("Texas Instrument Inc.");
1300