1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TI Bandgap temperature sensor driver
4  *
5  * Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/
6  * Author: J Keerthy <j-keerthy@ti.com>
7  * Author: Moiz Sonasath <m-sonasath@ti.com>
8  * Couple of fixes, DT and MFD adaptation:
9  *   Eduardo Valentin <eduardo.valentin@ti.com>
10  */
11 
12 #include <linux/module.h>
13 #include <linux/export.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/interrupt.h>
17 #include <linux/clk.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/platform_device.h>
20 #include <linux/err.h>
21 #include <linux/types.h>
22 #include <linux/spinlock.h>
23 #include <linux/reboot.h>
24 #include <linux/of_device.h>
25 #include <linux/of_platform.h>
26 #include <linux/of_irq.h>
27 #include <linux/io.h>
28 
29 #include "ti-bandgap.h"
30 
31 static int ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id);
32 
33 /***   Helper functions to access registers and their bitfields   ***/
34 
35 /**
36  * ti_bandgap_readl() - simple read helper function
37  * @bgp: pointer to ti_bandgap structure
38  * @reg: desired register (offset) to be read
39  *
40  * Helper function to read bandgap registers. It uses the io remapped area.
41  * Return: the register value.
42  */
43 static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
44 {
45 	return readl(bgp->base + reg);
46 }
47 
48 /**
49  * ti_bandgap_writel() - simple write helper function
50  * @bgp: pointer to ti_bandgap structure
51  * @val: desired register value to be written
52  * @reg: desired register (offset) to be written
53  *
54  * Helper function to write bandgap registers. It uses the io remapped area.
55  */
56 static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
57 {
58 	writel(val, bgp->base + reg);
59 }
60 
61 /**
62  * DOC: macro to update bits.
63  *
64  * RMW_BITS() - used to read, modify and update bandgap bitfields.
65  *            The value passed will be shifted.
66  */
67 #define RMW_BITS(bgp, id, reg, mask, val)			\
68 do {								\
69 	struct temp_sensor_registers *t;			\
70 	u32 r;							\
71 								\
72 	t = bgp->conf->sensors[(id)].registers;		\
73 	r = ti_bandgap_readl(bgp, t->reg);			\
74 	r &= ~t->mask;						\
75 	r |= (val) << __ffs(t->mask);				\
76 	ti_bandgap_writel(bgp, r, t->reg);			\
77 } while (0)
78 
79 /***   Basic helper functions   ***/
80 
81 /**
82  * ti_bandgap_power() - controls the power state of a bandgap device
83  * @bgp: pointer to ti_bandgap structure
84  * @on: desired power state (1 - on, 0 - off)
85  *
86  * Used to power on/off a bandgap device instance. Only used on those
87  * that features tempsoff bit.
88  *
89  * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
90  */
91 static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
92 {
93 	int i;
94 
95 	if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH))
96 		return -ENOTSUPP;
97 
98 	for (i = 0; i < bgp->conf->sensor_count; i++)
99 		/* active on 0 */
100 		RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
101 	return 0;
102 }
103 
104 /**
105  * ti_errata814_bandgap_read_temp() - helper function to read dra7 sensor temperature
106  * @bgp: pointer to ti_bandgap structure
107  * @reg: desired register (offset) to be read
108  *
109  * Function to read dra7 bandgap sensor temperature. This is done separately
110  * so as to workaround the errata "Bandgap Temperature read Dtemp can be
111  * corrupted" - Errata ID: i814".
112  * Read accesses to registers listed below can be corrupted due to incorrect
113  * resynchronization between clock domains.
114  * Read access to registers below can be corrupted :
115  * CTRL_CORE_DTEMP_MPU/GPU/CORE/DSPEVE/IVA_n (n = 0 to 4)
116  * CTRL_CORE_TEMP_SENSOR_MPU/GPU/CORE/DSPEVE/IVA_n
117  *
118  * Return: the register value.
119  */
120 static u32 ti_errata814_bandgap_read_temp(struct ti_bandgap *bgp,  u32 reg)
121 {
122 	u32 val1, val2;
123 
124 	val1 = ti_bandgap_readl(bgp, reg);
125 	val2 = ti_bandgap_readl(bgp, reg);
126 
127 	/* If both times we read the same value then that is right */
128 	if (val1 == val2)
129 		return val1;
130 
131 	/* if val1 and val2 are different read it third time */
132 	return ti_bandgap_readl(bgp, reg);
133 }
134 
135 /**
136  * ti_bandgap_read_temp() - helper function to read sensor temperature
137  * @bgp: pointer to ti_bandgap structure
138  * @id: bandgap sensor id
139  *
140  * Function to concentrate the steps to read sensor temperature register.
141  * This function is desired because, depending on bandgap device version,
142  * it might be needed to freeze the bandgap state machine, before fetching
143  * the register value.
144  *
145  * Return: temperature in ADC values.
146  */
147 static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
148 {
149 	struct temp_sensor_registers *tsr;
150 	u32 temp, reg;
151 
152 	tsr = bgp->conf->sensors[id].registers;
153 	reg = tsr->temp_sensor_ctrl;
154 
155 	if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
156 		RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
157 		/*
158 		 * In case we cannot read from cur_dtemp / dtemp_0,
159 		 * then we read from the last valid temp read
160 		 */
161 		reg = tsr->ctrl_dtemp_1;
162 	}
163 
164 	/* read temperature */
165 	if (TI_BANDGAP_HAS(bgp, ERRATA_814))
166 		temp = ti_errata814_bandgap_read_temp(bgp, reg);
167 	else
168 		temp = ti_bandgap_readl(bgp, reg);
169 
170 	temp &= tsr->bgap_dtemp_mask;
171 
172 	if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
173 		RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
174 
175 	return temp;
176 }
177 
178 /***   IRQ handlers   ***/
179 
180 /**
181  * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
182  * @irq: IRQ number
183  * @data: private data (struct ti_bandgap *)
184  *
185  * This is the Talert handler. Use it only if bandgap device features
186  * HAS(TALERT). This handler goes over all sensors and checks their
187  * conditions and acts accordingly. In case there are events pending,
188  * it will reset the event mask to wait for the opposite event (next event).
189  * Every time there is a new event, it will be reported to thermal layer.
190  *
191  * Return: IRQ_HANDLED
192  */
193 static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
194 {
195 	struct ti_bandgap *bgp = data;
196 	struct temp_sensor_registers *tsr;
197 	u32 t_hot = 0, t_cold = 0, ctrl;
198 	int i;
199 
200 	spin_lock(&bgp->lock);
201 	for (i = 0; i < bgp->conf->sensor_count; i++) {
202 		tsr = bgp->conf->sensors[i].registers;
203 		ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
204 
205 		/* Read the status of t_hot */
206 		t_hot = ctrl & tsr->status_hot_mask;
207 
208 		/* Read the status of t_cold */
209 		t_cold = ctrl & tsr->status_cold_mask;
210 
211 		if (!t_cold && !t_hot)
212 			continue;
213 
214 		ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
215 		/*
216 		 * One TALERT interrupt: Two sources
217 		 * If the interrupt is due to t_hot then mask t_hot and
218 		 * and unmask t_cold else mask t_cold and unmask t_hot
219 		 */
220 		if (t_hot) {
221 			ctrl &= ~tsr->mask_hot_mask;
222 			ctrl |= tsr->mask_cold_mask;
223 		} else if (t_cold) {
224 			ctrl &= ~tsr->mask_cold_mask;
225 			ctrl |= tsr->mask_hot_mask;
226 		}
227 
228 		ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
229 
230 		dev_dbg(bgp->dev,
231 			"%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
232 			__func__, bgp->conf->sensors[i].domain,
233 			t_hot, t_cold);
234 
235 		/* report temperature to whom may concern */
236 		if (bgp->conf->report_temperature)
237 			bgp->conf->report_temperature(bgp, i);
238 	}
239 	spin_unlock(&bgp->lock);
240 
241 	return IRQ_HANDLED;
242 }
243 
244 /**
245  * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
246  * @irq: IRQ number
247  * @data: private data (unused)
248  *
249  * This is the Tshut handler. Use it only if bandgap device features
250  * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
251  * the system.
252  *
253  * Return: IRQ_HANDLED
254  */
255 static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
256 {
257 	pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
258 		 __func__);
259 
260 	orderly_poweroff(true);
261 
262 	return IRQ_HANDLED;
263 }
264 
265 /***   Helper functions which manipulate conversion ADC <-> mi Celsius   ***/
266 
267 /**
268  * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
269  * @bgp: struct ti_bandgap pointer
270  * @adc_val: value in ADC representation
271  * @t: address where to write the resulting temperature in mCelsius
272  *
273  * Simple conversion from ADC representation to mCelsius. In case the ADC value
274  * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
275  * The conversion table is indexed by the ADC values.
276  *
277  * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
278  * argument is out of the ADC conv table range.
279  */
280 static
281 int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
282 {
283 	const struct ti_bandgap_data *conf = bgp->conf;
284 
285 	/* look up for temperature in the table and return the temperature */
286 	if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val)
287 		return -ERANGE;
288 
289 	*t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
290 	return 0;
291 }
292 
293 /**
294  * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
295  * @bgp: struct ti_bandgap pointer
296  * @id: bandgap sensor id
297  *
298  * Checks if the bandgap pointer is valid and if the sensor id is also
299  * applicable.
300  *
301  * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
302  * @id cannot index @bgp sensors.
303  */
304 static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
305 {
306 	if (!bgp || IS_ERR(bgp)) {
307 		pr_err("%s: invalid bandgap pointer\n", __func__);
308 		return -EINVAL;
309 	}
310 
311 	if ((id < 0) || (id >= bgp->conf->sensor_count)) {
312 		dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
313 			__func__, id);
314 		return -ERANGE;
315 	}
316 
317 	return 0;
318 }
319 
320 /**
321  * ti_bandgap_read_counter() - read the sensor counter
322  * @bgp: pointer to bandgap instance
323  * @id: sensor id
324  * @interval: resulting update interval in miliseconds
325  */
326 static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
327 				    int *interval)
328 {
329 	struct temp_sensor_registers *tsr;
330 	int time;
331 
332 	tsr = bgp->conf->sensors[id].registers;
333 	time = ti_bandgap_readl(bgp, tsr->bgap_counter);
334 	time = (time & tsr->counter_mask) >>
335 					__ffs(tsr->counter_mask);
336 	time = time * 1000 / bgp->clk_rate;
337 	*interval = time;
338 }
339 
340 /**
341  * ti_bandgap_read_counter_delay() - read the sensor counter delay
342  * @bgp: pointer to bandgap instance
343  * @id: sensor id
344  * @interval: resulting update interval in miliseconds
345  */
346 static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
347 					  int *interval)
348 {
349 	struct temp_sensor_registers *tsr;
350 	int reg_val;
351 
352 	tsr = bgp->conf->sensors[id].registers;
353 
354 	reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
355 	reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
356 				__ffs(tsr->mask_counter_delay_mask);
357 	switch (reg_val) {
358 	case 0:
359 		*interval = 0;
360 		break;
361 	case 1:
362 		*interval = 1;
363 		break;
364 	case 2:
365 		*interval = 10;
366 		break;
367 	case 3:
368 		*interval = 100;
369 		break;
370 	case 4:
371 		*interval = 250;
372 		break;
373 	case 5:
374 		*interval = 500;
375 		break;
376 	default:
377 		dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
378 			 reg_val);
379 	}
380 }
381 
382 /**
383  * ti_bandgap_read_update_interval() - read the sensor update interval
384  * @bgp: pointer to bandgap instance
385  * @id: sensor id
386  * @interval: resulting update interval in miliseconds
387  *
388  * Return: 0 on success or the proper error code
389  */
390 int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
391 				    int *interval)
392 {
393 	int ret = 0;
394 
395 	ret = ti_bandgap_validate(bgp, id);
396 	if (ret)
397 		goto exit;
398 
399 	if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
400 	    !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
401 		ret = -ENOTSUPP;
402 		goto exit;
403 	}
404 
405 	if (TI_BANDGAP_HAS(bgp, COUNTER)) {
406 		ti_bandgap_read_counter(bgp, id, interval);
407 		goto exit;
408 	}
409 
410 	ti_bandgap_read_counter_delay(bgp, id, interval);
411 exit:
412 	return ret;
413 }
414 
415 /**
416  * ti_bandgap_write_counter_delay() - set the counter_delay
417  * @bgp: pointer to bandgap instance
418  * @id: sensor id
419  * @interval: desired update interval in miliseconds
420  *
421  * Return: 0 on success or the proper error code
422  */
423 static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
424 					  u32 interval)
425 {
426 	int rval;
427 
428 	switch (interval) {
429 	case 0: /* Immediate conversion */
430 		rval = 0x0;
431 		break;
432 	case 1: /* Conversion after ever 1ms */
433 		rval = 0x1;
434 		break;
435 	case 10: /* Conversion after ever 10ms */
436 		rval = 0x2;
437 		break;
438 	case 100: /* Conversion after ever 100ms */
439 		rval = 0x3;
440 		break;
441 	case 250: /* Conversion after ever 250ms */
442 		rval = 0x4;
443 		break;
444 	case 500: /* Conversion after ever 500ms */
445 		rval = 0x5;
446 		break;
447 	default:
448 		dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
449 		return -EINVAL;
450 	}
451 
452 	spin_lock(&bgp->lock);
453 	RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
454 	spin_unlock(&bgp->lock);
455 
456 	return 0;
457 }
458 
459 /**
460  * ti_bandgap_write_counter() - set the bandgap sensor counter
461  * @bgp: pointer to bandgap instance
462  * @id: sensor id
463  * @interval: desired update interval in miliseconds
464  */
465 static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
466 				     u32 interval)
467 {
468 	interval = interval * bgp->clk_rate / 1000;
469 	spin_lock(&bgp->lock);
470 	RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
471 	spin_unlock(&bgp->lock);
472 }
473 
474 /**
475  * ti_bandgap_write_update_interval() - set the update interval
476  * @bgp: pointer to bandgap instance
477  * @id: sensor id
478  * @interval: desired update interval in miliseconds
479  *
480  * Return: 0 on success or the proper error code
481  */
482 int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
483 				     int id, u32 interval)
484 {
485 	int ret = ti_bandgap_validate(bgp, id);
486 	if (ret)
487 		goto exit;
488 
489 	if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
490 	    !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
491 		ret = -ENOTSUPP;
492 		goto exit;
493 	}
494 
495 	if (TI_BANDGAP_HAS(bgp, COUNTER)) {
496 		ti_bandgap_write_counter(bgp, id, interval);
497 		goto exit;
498 	}
499 
500 	ret = ti_bandgap_write_counter_delay(bgp, id, interval);
501 exit:
502 	return ret;
503 }
504 
505 /**
506  * ti_bandgap_read_temperature() - report current temperature
507  * @bgp: pointer to bandgap instance
508  * @id: sensor id
509  * @temperature: resulting temperature
510  *
511  * Return: 0 on success or the proper error code
512  */
513 int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
514 				int *temperature)
515 {
516 	u32 temp;
517 	int ret;
518 
519 	ret = ti_bandgap_validate(bgp, id);
520 	if (ret)
521 		return ret;
522 
523 	if (!TI_BANDGAP_HAS(bgp, MODE_CONFIG)) {
524 		ret = ti_bandgap_force_single_read(bgp, id);
525 		if (ret)
526 			return ret;
527 	}
528 
529 	spin_lock(&bgp->lock);
530 	temp = ti_bandgap_read_temp(bgp, id);
531 	spin_unlock(&bgp->lock);
532 
533 	ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
534 	if (ret)
535 		return -EIO;
536 
537 	*temperature = temp;
538 
539 	return 0;
540 }
541 
542 /**
543  * ti_bandgap_set_sensor_data() - helper function to store thermal
544  * framework related data.
545  * @bgp: pointer to bandgap instance
546  * @id: sensor id
547  * @data: thermal framework related data to be stored
548  *
549  * Return: 0 on success or the proper error code
550  */
551 int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
552 {
553 	int ret = ti_bandgap_validate(bgp, id);
554 	if (ret)
555 		return ret;
556 
557 	bgp->regval[id].data = data;
558 
559 	return 0;
560 }
561 
562 /**
563  * ti_bandgap_get_sensor_data() - helper function to get thermal
564  * framework related data.
565  * @bgp: pointer to bandgap instance
566  * @id: sensor id
567  *
568  * Return: data stored by set function with sensor id on success or NULL
569  */
570 void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
571 {
572 	int ret = ti_bandgap_validate(bgp, id);
573 	if (ret)
574 		return ERR_PTR(ret);
575 
576 	return bgp->regval[id].data;
577 }
578 
579 /***   Helper functions used during device initialization   ***/
580 
581 /**
582  * ti_bandgap_force_single_read() - executes 1 single ADC conversion
583  * @bgp: pointer to struct ti_bandgap
584  * @id: sensor id which it is desired to read 1 temperature
585  *
586  * Used to initialize the conversion state machine and set it to a valid
587  * state. Called during device initialization and context restore events.
588  *
589  * Return: 0
590  */
591 static int
592 ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
593 {
594 	u32 counter = 1000;
595 	struct temp_sensor_registers *tsr;
596 
597 	/* Select single conversion mode */
598 	if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
599 		RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
600 
601 	/* Start of Conversion = 1 */
602 	RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
603 
604 	/* Wait for EOCZ going up */
605 	tsr = bgp->conf->sensors[id].registers;
606 
607 	while (--counter) {
608 		if (ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) &
609 		    tsr->bgap_eocz_mask)
610 			break;
611 	}
612 
613 	/* Start of Conversion = 0 */
614 	RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
615 
616 	/* Wait for EOCZ going down */
617 	counter = 1000;
618 	while (--counter) {
619 		if (!(ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) &
620 		      tsr->bgap_eocz_mask))
621 			break;
622 	}
623 
624 	return 0;
625 }
626 
627 /**
628  * ti_bandgap_set_continuous_mode() - One time enabling of continuous mode
629  * @bgp: pointer to struct ti_bandgap
630  *
631  * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
632  * be used for junction temperature monitoring, it is desirable that the
633  * sensors are operational all the time, so that alerts are generated
634  * properly.
635  *
636  * Return: 0
637  */
638 static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
639 {
640 	int i;
641 
642 	for (i = 0; i < bgp->conf->sensor_count; i++) {
643 		/* Perform a single read just before enabling continuous */
644 		ti_bandgap_force_single_read(bgp, i);
645 		RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
646 	}
647 
648 	return 0;
649 }
650 
651 /**
652  * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
653  * @bgp: pointer to struct ti_bandgap
654  * @id: id of the individual sensor
655  * @trend: Pointer to trend.
656  *
657  * This function needs to be called to fetch the temperature trend of a
658  * Particular sensor. The function computes the difference in temperature
659  * w.r.t time. For the bandgaps with built in history buffer the temperatures
660  * are read from the buffer and for those without the Buffer -ENOTSUPP is
661  * returned.
662  *
663  * Return: 0 if no error, else return corresponding error. If no
664  *		error then the trend value is passed on to trend parameter
665  */
666 int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
667 {
668 	struct temp_sensor_registers *tsr;
669 	u32 temp1, temp2, reg1, reg2;
670 	int t1, t2, interval, ret = 0;
671 
672 	ret = ti_bandgap_validate(bgp, id);
673 	if (ret)
674 		goto exit;
675 
676 	if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
677 	    !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
678 		ret = -ENOTSUPP;
679 		goto exit;
680 	}
681 
682 	spin_lock(&bgp->lock);
683 
684 	tsr = bgp->conf->sensors[id].registers;
685 
686 	/* Freeze and read the last 2 valid readings */
687 	RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
688 	reg1 = tsr->ctrl_dtemp_1;
689 	reg2 = tsr->ctrl_dtemp_2;
690 
691 	/* read temperature from history buffer */
692 	temp1 = ti_bandgap_readl(bgp, reg1);
693 	temp1 &= tsr->bgap_dtemp_mask;
694 
695 	temp2 = ti_bandgap_readl(bgp, reg2);
696 	temp2 &= tsr->bgap_dtemp_mask;
697 
698 	/* Convert from adc values to mCelsius temperature */
699 	ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
700 	if (ret)
701 		goto unfreeze;
702 
703 	ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
704 	if (ret)
705 		goto unfreeze;
706 
707 	/* Fetch the update interval */
708 	ret = ti_bandgap_read_update_interval(bgp, id, &interval);
709 	if (ret)
710 		goto unfreeze;
711 
712 	/* Set the interval to 1 ms if bandgap counter delay is not set */
713 	if (interval == 0)
714 		interval = 1;
715 
716 	*trend = (t1 - t2) / interval;
717 
718 	dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
719 		t1, t2, *trend);
720 
721 unfreeze:
722 	RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
723 	spin_unlock(&bgp->lock);
724 exit:
725 	return ret;
726 }
727 
728 /**
729  * ti_bandgap_tshut_init() - setup and initialize tshut handling
730  * @bgp: pointer to struct ti_bandgap
731  * @pdev: pointer to device struct platform_device
732  *
733  * Call this function only in case the bandgap features HAS(TSHUT).
734  * In this case, the driver needs to handle the TSHUT signal as an IRQ.
735  * The IRQ is wired as a GPIO, and for this purpose, it is required
736  * to specify which GPIO line is used. TSHUT IRQ is fired anytime
737  * one of the bandgap sensors violates the TSHUT high/hot threshold.
738  * And in that case, the system must go off.
739  *
740  * Return: 0 if no error, else error status
741  */
742 static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
743 				 struct platform_device *pdev)
744 {
745 	int status;
746 
747 	status = request_irq(gpiod_to_irq(bgp->tshut_gpiod),
748 			     ti_bandgap_tshut_irq_handler,
749 			     IRQF_TRIGGER_RISING, "tshut", NULL);
750 	if (status)
751 		dev_err(bgp->dev, "request irq failed for TSHUT");
752 
753 	return 0;
754 }
755 
756 /**
757  * ti_bandgap_alert_init() - setup and initialize talert handling
758  * @bgp: pointer to struct ti_bandgap
759  * @pdev: pointer to device struct platform_device
760  *
761  * Call this function only in case the bandgap features HAS(TALERT).
762  * In this case, the driver needs to handle the TALERT signals as an IRQs.
763  * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
764  * are violated. In these situation, the driver must reprogram the thresholds,
765  * accordingly to specified policy.
766  *
767  * Return: 0 if no error, else return corresponding error.
768  */
769 static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
770 				  struct platform_device *pdev)
771 {
772 	int ret;
773 
774 	bgp->irq = platform_get_irq(pdev, 0);
775 	if (bgp->irq < 0)
776 		return bgp->irq;
777 
778 	ret = request_threaded_irq(bgp->irq, NULL,
779 				   ti_bandgap_talert_irq_handler,
780 				   IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
781 				   "talert", bgp);
782 	if (ret) {
783 		dev_err(&pdev->dev, "Request threaded irq failed.\n");
784 		return ret;
785 	}
786 
787 	return 0;
788 }
789 
790 static const struct of_device_id of_ti_bandgap_match[];
791 /**
792  * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
793  * @pdev: pointer to device struct platform_device
794  *
795  * Used to read the device tree properties accordingly to the bandgap
796  * matching version. Based on bandgap version and its capabilities it
797  * will build a struct ti_bandgap out of the required DT entries.
798  *
799  * Return: valid bandgap structure if successful, else returns ERR_PTR
800  * return value must be verified with IS_ERR.
801  */
802 static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
803 {
804 	struct device_node *node = pdev->dev.of_node;
805 	const struct of_device_id *of_id;
806 	struct ti_bandgap *bgp;
807 	struct resource *res;
808 	int i;
809 
810 	/* just for the sake */
811 	if (!node) {
812 		dev_err(&pdev->dev, "no platform information available\n");
813 		return ERR_PTR(-EINVAL);
814 	}
815 
816 	bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
817 	if (!bgp)
818 		return ERR_PTR(-ENOMEM);
819 
820 	of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
821 	if (of_id)
822 		bgp->conf = of_id->data;
823 
824 	/* register shadow for context save and restore */
825 	bgp->regval = devm_kcalloc(&pdev->dev, bgp->conf->sensor_count,
826 				   sizeof(*bgp->regval), GFP_KERNEL);
827 	if (!bgp->regval)
828 		return ERR_PTR(-ENOMEM);
829 
830 	i = 0;
831 	do {
832 		void __iomem *chunk;
833 
834 		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
835 		if (!res)
836 			break;
837 		chunk = devm_ioremap_resource(&pdev->dev, res);
838 		if (i == 0)
839 			bgp->base = chunk;
840 		if (IS_ERR(chunk))
841 			return ERR_CAST(chunk);
842 
843 		i++;
844 	} while (res);
845 
846 	if (TI_BANDGAP_HAS(bgp, TSHUT)) {
847 		bgp->tshut_gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_IN);
848 		if (IS_ERR(bgp->tshut_gpiod)) {
849 			dev_err(&pdev->dev, "invalid gpio for tshut\n");
850 			return ERR_CAST(bgp->tshut_gpiod);
851 		}
852 	}
853 
854 	return bgp;
855 }
856 
857 /***   Device driver call backs   ***/
858 
859 static
860 int ti_bandgap_probe(struct platform_device *pdev)
861 {
862 	struct ti_bandgap *bgp;
863 	int clk_rate, ret, i;
864 
865 	bgp = ti_bandgap_build(pdev);
866 	if (IS_ERR(bgp)) {
867 		dev_err(&pdev->dev, "failed to fetch platform data\n");
868 		return PTR_ERR(bgp);
869 	}
870 	bgp->dev = &pdev->dev;
871 
872 	if (TI_BANDGAP_HAS(bgp, UNRELIABLE))
873 		dev_warn(&pdev->dev,
874 			 "This OMAP thermal sensor is unreliable. You've been warned\n");
875 
876 	if (TI_BANDGAP_HAS(bgp, TSHUT)) {
877 		ret = ti_bandgap_tshut_init(bgp, pdev);
878 		if (ret) {
879 			dev_err(&pdev->dev,
880 				"failed to initialize system tshut IRQ\n");
881 			return ret;
882 		}
883 	}
884 
885 	bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
886 	if (IS_ERR(bgp->fclock)) {
887 		dev_err(&pdev->dev, "failed to request fclock reference\n");
888 		ret = PTR_ERR(bgp->fclock);
889 		goto free_irqs;
890 	}
891 
892 	bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
893 	if (IS_ERR(bgp->div_clk)) {
894 		dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n");
895 		ret = PTR_ERR(bgp->div_clk);
896 		goto put_fclock;
897 	}
898 
899 	for (i = 0; i < bgp->conf->sensor_count; i++) {
900 		struct temp_sensor_registers *tsr;
901 		u32 val;
902 
903 		tsr = bgp->conf->sensors[i].registers;
904 		/*
905 		 * check if the efuse has a non-zero value if not
906 		 * it is an untrimmed sample and the temperatures
907 		 * may not be accurate
908 		 */
909 		val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
910 		if (!val)
911 			dev_info(&pdev->dev,
912 				 "Non-trimmed BGAP, Temp not accurate\n");
913 	}
914 
915 	clk_rate = clk_round_rate(bgp->div_clk,
916 				  bgp->conf->sensors[0].ts_data->max_freq);
917 	if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
918 	    clk_rate <= 0) {
919 		ret = -ENODEV;
920 		dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
921 		goto put_clks;
922 	}
923 
924 	ret = clk_set_rate(bgp->div_clk, clk_rate);
925 	if (ret)
926 		dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
927 
928 	bgp->clk_rate = clk_rate;
929 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
930 		clk_prepare_enable(bgp->fclock);
931 
932 
933 	spin_lock_init(&bgp->lock);
934 	bgp->dev = &pdev->dev;
935 	platform_set_drvdata(pdev, bgp);
936 
937 	ti_bandgap_power(bgp, true);
938 
939 	/* Set default counter to 1 for now */
940 	if (TI_BANDGAP_HAS(bgp, COUNTER))
941 		for (i = 0; i < bgp->conf->sensor_count; i++)
942 			RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
943 
944 	/* Set default thresholds for alert and shutdown */
945 	for (i = 0; i < bgp->conf->sensor_count; i++) {
946 		struct temp_sensor_data *ts_data;
947 
948 		ts_data = bgp->conf->sensors[i].ts_data;
949 
950 		if (TI_BANDGAP_HAS(bgp, TALERT)) {
951 			/* Set initial Talert thresholds */
952 			RMW_BITS(bgp, i, bgap_threshold,
953 				 threshold_tcold_mask, ts_data->t_cold);
954 			RMW_BITS(bgp, i, bgap_threshold,
955 				 threshold_thot_mask, ts_data->t_hot);
956 			/* Enable the alert events */
957 			RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
958 			RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
959 		}
960 
961 		if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
962 			/* Set initial Tshut thresholds */
963 			RMW_BITS(bgp, i, tshut_threshold,
964 				 tshut_hot_mask, ts_data->tshut_hot);
965 			RMW_BITS(bgp, i, tshut_threshold,
966 				 tshut_cold_mask, ts_data->tshut_cold);
967 		}
968 	}
969 
970 	if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
971 		ti_bandgap_set_continuous_mode(bgp);
972 
973 	/* Set .250 seconds time as default counter */
974 	if (TI_BANDGAP_HAS(bgp, COUNTER))
975 		for (i = 0; i < bgp->conf->sensor_count; i++)
976 			RMW_BITS(bgp, i, bgap_counter, counter_mask,
977 				 bgp->clk_rate / 4);
978 
979 	/* Every thing is good? Then expose the sensors */
980 	for (i = 0; i < bgp->conf->sensor_count; i++) {
981 		char *domain;
982 
983 		if (bgp->conf->sensors[i].register_cooling) {
984 			ret = bgp->conf->sensors[i].register_cooling(bgp, i);
985 			if (ret)
986 				goto remove_sensors;
987 		}
988 
989 		if (bgp->conf->expose_sensor) {
990 			domain = bgp->conf->sensors[i].domain;
991 			ret = bgp->conf->expose_sensor(bgp, i, domain);
992 			if (ret)
993 				goto remove_last_cooling;
994 		}
995 	}
996 
997 	/*
998 	 * Enable the Interrupts once everything is set. Otherwise irq handler
999 	 * might be called as soon as it is enabled where as rest of framework
1000 	 * is still getting initialised.
1001 	 */
1002 	if (TI_BANDGAP_HAS(bgp, TALERT)) {
1003 		ret = ti_bandgap_talert_init(bgp, pdev);
1004 		if (ret) {
1005 			dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1006 			i = bgp->conf->sensor_count;
1007 			goto disable_clk;
1008 		}
1009 	}
1010 
1011 	return 0;
1012 
1013 remove_last_cooling:
1014 	if (bgp->conf->sensors[i].unregister_cooling)
1015 		bgp->conf->sensors[i].unregister_cooling(bgp, i);
1016 remove_sensors:
1017 	for (i--; i >= 0; i--) {
1018 		if (bgp->conf->sensors[i].unregister_cooling)
1019 			bgp->conf->sensors[i].unregister_cooling(bgp, i);
1020 		if (bgp->conf->remove_sensor)
1021 			bgp->conf->remove_sensor(bgp, i);
1022 	}
1023 	ti_bandgap_power(bgp, false);
1024 disable_clk:
1025 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1026 		clk_disable_unprepare(bgp->fclock);
1027 put_clks:
1028 	clk_put(bgp->div_clk);
1029 put_fclock:
1030 	clk_put(bgp->fclock);
1031 free_irqs:
1032 	if (TI_BANDGAP_HAS(bgp, TSHUT))
1033 		free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL);
1034 
1035 	return ret;
1036 }
1037 
1038 static
1039 int ti_bandgap_remove(struct platform_device *pdev)
1040 {
1041 	struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1042 	int i;
1043 
1044 	/* First thing is to remove sensor interfaces */
1045 	for (i = 0; i < bgp->conf->sensor_count; i++) {
1046 		if (bgp->conf->sensors[i].unregister_cooling)
1047 			bgp->conf->sensors[i].unregister_cooling(bgp, i);
1048 
1049 		if (bgp->conf->remove_sensor)
1050 			bgp->conf->remove_sensor(bgp, i);
1051 	}
1052 
1053 	ti_bandgap_power(bgp, false);
1054 
1055 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1056 		clk_disable_unprepare(bgp->fclock);
1057 	clk_put(bgp->fclock);
1058 	clk_put(bgp->div_clk);
1059 
1060 	if (TI_BANDGAP_HAS(bgp, TALERT))
1061 		free_irq(bgp->irq, bgp);
1062 
1063 	if (TI_BANDGAP_HAS(bgp, TSHUT))
1064 		free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL);
1065 
1066 	return 0;
1067 }
1068 
1069 #ifdef CONFIG_PM_SLEEP
1070 static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1071 {
1072 	int i;
1073 
1074 	for (i = 0; i < bgp->conf->sensor_count; i++) {
1075 		struct temp_sensor_registers *tsr;
1076 		struct temp_sensor_regval *rval;
1077 
1078 		rval = &bgp->regval[i];
1079 		tsr = bgp->conf->sensors[i].registers;
1080 
1081 		if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1082 			rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1083 							tsr->bgap_mode_ctrl);
1084 		if (TI_BANDGAP_HAS(bgp, COUNTER))
1085 			rval->bg_counter = ti_bandgap_readl(bgp,
1086 							tsr->bgap_counter);
1087 		if (TI_BANDGAP_HAS(bgp, TALERT)) {
1088 			rval->bg_threshold = ti_bandgap_readl(bgp,
1089 							tsr->bgap_threshold);
1090 			rval->bg_ctrl = ti_bandgap_readl(bgp,
1091 						   tsr->bgap_mask_ctrl);
1092 		}
1093 
1094 		if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1095 			rval->tshut_threshold = ti_bandgap_readl(bgp,
1096 						   tsr->tshut_threshold);
1097 	}
1098 
1099 	return 0;
1100 }
1101 
1102 static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1103 {
1104 	int i;
1105 
1106 	for (i = 0; i < bgp->conf->sensor_count; i++) {
1107 		struct temp_sensor_registers *tsr;
1108 		struct temp_sensor_regval *rval;
1109 		u32 val = 0;
1110 
1111 		rval = &bgp->regval[i];
1112 		tsr = bgp->conf->sensors[i].registers;
1113 
1114 		if (TI_BANDGAP_HAS(bgp, COUNTER))
1115 			val = ti_bandgap_readl(bgp, tsr->bgap_counter);
1116 
1117 		if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1118 			ti_bandgap_writel(bgp, rval->tshut_threshold,
1119 					  tsr->tshut_threshold);
1120 		/* Force immediate temperature measurement and update
1121 		 * of the DTEMP field
1122 		 */
1123 		ti_bandgap_force_single_read(bgp, i);
1124 
1125 		if (TI_BANDGAP_HAS(bgp, COUNTER))
1126 			ti_bandgap_writel(bgp, rval->bg_counter,
1127 					  tsr->bgap_counter);
1128 		if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1129 			ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1130 					  tsr->bgap_mode_ctrl);
1131 		if (TI_BANDGAP_HAS(bgp, TALERT)) {
1132 			ti_bandgap_writel(bgp, rval->bg_threshold,
1133 					  tsr->bgap_threshold);
1134 			ti_bandgap_writel(bgp, rval->bg_ctrl,
1135 					  tsr->bgap_mask_ctrl);
1136 		}
1137 	}
1138 
1139 	return 0;
1140 }
1141 
1142 static int ti_bandgap_suspend(struct device *dev)
1143 {
1144 	struct ti_bandgap *bgp = dev_get_drvdata(dev);
1145 	int err;
1146 
1147 	err = ti_bandgap_save_ctxt(bgp);
1148 	ti_bandgap_power(bgp, false);
1149 
1150 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1151 		clk_disable_unprepare(bgp->fclock);
1152 
1153 	return err;
1154 }
1155 
1156 static int ti_bandgap_resume(struct device *dev)
1157 {
1158 	struct ti_bandgap *bgp = dev_get_drvdata(dev);
1159 
1160 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1161 		clk_prepare_enable(bgp->fclock);
1162 
1163 	ti_bandgap_power(bgp, true);
1164 
1165 	return ti_bandgap_restore_ctxt(bgp);
1166 }
1167 static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
1168 			 ti_bandgap_resume);
1169 
1170 #define DEV_PM_OPS	(&ti_bandgap_dev_pm_ops)
1171 #else
1172 #define DEV_PM_OPS	NULL
1173 #endif
1174 
1175 static const struct of_device_id of_ti_bandgap_match[] = {
1176 #ifdef CONFIG_OMAP3_THERMAL
1177 	{
1178 		.compatible = "ti,omap34xx-bandgap",
1179 		.data = (void *)&omap34xx_data,
1180 	},
1181 	{
1182 		.compatible = "ti,omap36xx-bandgap",
1183 		.data = (void *)&omap36xx_data,
1184 	},
1185 #endif
1186 #ifdef CONFIG_OMAP4_THERMAL
1187 	{
1188 		.compatible = "ti,omap4430-bandgap",
1189 		.data = (void *)&omap4430_data,
1190 	},
1191 	{
1192 		.compatible = "ti,omap4460-bandgap",
1193 		.data = (void *)&omap4460_data,
1194 	},
1195 	{
1196 		.compatible = "ti,omap4470-bandgap",
1197 		.data = (void *)&omap4470_data,
1198 	},
1199 #endif
1200 #ifdef CONFIG_OMAP5_THERMAL
1201 	{
1202 		.compatible = "ti,omap5430-bandgap",
1203 		.data = (void *)&omap5430_data,
1204 	},
1205 #endif
1206 #ifdef CONFIG_DRA752_THERMAL
1207 	{
1208 		.compatible = "ti,dra752-bandgap",
1209 		.data = (void *)&dra752_data,
1210 	},
1211 #endif
1212 	/* Sentinel */
1213 	{ },
1214 };
1215 MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1216 
1217 static struct platform_driver ti_bandgap_sensor_driver = {
1218 	.probe = ti_bandgap_probe,
1219 	.remove = ti_bandgap_remove,
1220 	.driver = {
1221 			.name = "ti-soc-thermal",
1222 			.pm = DEV_PM_OPS,
1223 			.of_match_table	= of_ti_bandgap_match,
1224 	},
1225 };
1226 
1227 module_platform_driver(ti_bandgap_sensor_driver);
1228 
1229 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1230 MODULE_LICENSE("GPL v2");
1231 MODULE_ALIAS("platform:ti-soc-thermal");
1232 MODULE_AUTHOR("Texas Instrument Inc.");
1233