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