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 		dev_err(&pdev->dev, "get_irq failed\n");
777 		return bgp->irq;
778 	}
779 	ret = request_threaded_irq(bgp->irq, NULL,
780 				   ti_bandgap_talert_irq_handler,
781 				   IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
782 				   "talert", bgp);
783 	if (ret) {
784 		dev_err(&pdev->dev, "Request threaded irq failed.\n");
785 		return ret;
786 	}
787 
788 	return 0;
789 }
790 
791 static const struct of_device_id of_ti_bandgap_match[];
792 /**
793  * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
794  * @pdev: pointer to device struct platform_device
795  *
796  * Used to read the device tree properties accordingly to the bandgap
797  * matching version. Based on bandgap version and its capabilities it
798  * will build a struct ti_bandgap out of the required DT entries.
799  *
800  * Return: valid bandgap structure if successful, else returns ERR_PTR
801  * return value must be verified with IS_ERR.
802  */
803 static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
804 {
805 	struct device_node *node = pdev->dev.of_node;
806 	const struct of_device_id *of_id;
807 	struct ti_bandgap *bgp;
808 	struct resource *res;
809 	int i;
810 
811 	/* just for the sake */
812 	if (!node) {
813 		dev_err(&pdev->dev, "no platform information available\n");
814 		return ERR_PTR(-EINVAL);
815 	}
816 
817 	bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
818 	if (!bgp)
819 		return ERR_PTR(-ENOMEM);
820 
821 	of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
822 	if (of_id)
823 		bgp->conf = of_id->data;
824 
825 	/* register shadow for context save and restore */
826 	bgp->regval = devm_kcalloc(&pdev->dev, bgp->conf->sensor_count,
827 				   sizeof(*bgp->regval), GFP_KERNEL);
828 	if (!bgp->regval)
829 		return ERR_PTR(-ENOMEM);
830 
831 	i = 0;
832 	do {
833 		void __iomem *chunk;
834 
835 		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
836 		if (!res)
837 			break;
838 		chunk = devm_ioremap_resource(&pdev->dev, res);
839 		if (i == 0)
840 			bgp->base = chunk;
841 		if (IS_ERR(chunk))
842 			return ERR_CAST(chunk);
843 
844 		i++;
845 	} while (res);
846 
847 	if (TI_BANDGAP_HAS(bgp, TSHUT)) {
848 		bgp->tshut_gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_IN);
849 		if (IS_ERR(bgp->tshut_gpiod)) {
850 			dev_err(&pdev->dev, "invalid gpio for tshut\n");
851 			return ERR_CAST(bgp->tshut_gpiod);
852 		}
853 	}
854 
855 	return bgp;
856 }
857 
858 /***   Device driver call backs   ***/
859 
860 static
861 int ti_bandgap_probe(struct platform_device *pdev)
862 {
863 	struct ti_bandgap *bgp;
864 	int clk_rate, ret, i;
865 
866 	bgp = ti_bandgap_build(pdev);
867 	if (IS_ERR(bgp)) {
868 		dev_err(&pdev->dev, "failed to fetch platform data\n");
869 		return PTR_ERR(bgp);
870 	}
871 	bgp->dev = &pdev->dev;
872 
873 	if (TI_BANDGAP_HAS(bgp, UNRELIABLE))
874 		dev_warn(&pdev->dev,
875 			 "This OMAP thermal sensor is unreliable. You've been warned\n");
876 
877 	if (TI_BANDGAP_HAS(bgp, TSHUT)) {
878 		ret = ti_bandgap_tshut_init(bgp, pdev);
879 		if (ret) {
880 			dev_err(&pdev->dev,
881 				"failed to initialize system tshut IRQ\n");
882 			return ret;
883 		}
884 	}
885 
886 	bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
887 	if (IS_ERR(bgp->fclock)) {
888 		dev_err(&pdev->dev, "failed to request fclock reference\n");
889 		ret = PTR_ERR(bgp->fclock);
890 		goto free_irqs;
891 	}
892 
893 	bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
894 	if (IS_ERR(bgp->div_clk)) {
895 		dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n");
896 		ret = PTR_ERR(bgp->div_clk);
897 		goto put_fclock;
898 	}
899 
900 	for (i = 0; i < bgp->conf->sensor_count; i++) {
901 		struct temp_sensor_registers *tsr;
902 		u32 val;
903 
904 		tsr = bgp->conf->sensors[i].registers;
905 		/*
906 		 * check if the efuse has a non-zero value if not
907 		 * it is an untrimmed sample and the temperatures
908 		 * may not be accurate
909 		 */
910 		val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
911 		if (!val)
912 			dev_info(&pdev->dev,
913 				 "Non-trimmed BGAP, Temp not accurate\n");
914 	}
915 
916 	clk_rate = clk_round_rate(bgp->div_clk,
917 				  bgp->conf->sensors[0].ts_data->max_freq);
918 	if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
919 	    clk_rate <= 0) {
920 		ret = -ENODEV;
921 		dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
922 		goto put_clks;
923 	}
924 
925 	ret = clk_set_rate(bgp->div_clk, clk_rate);
926 	if (ret)
927 		dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
928 
929 	bgp->clk_rate = clk_rate;
930 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
931 		clk_prepare_enable(bgp->fclock);
932 
933 
934 	spin_lock_init(&bgp->lock);
935 	bgp->dev = &pdev->dev;
936 	platform_set_drvdata(pdev, bgp);
937 
938 	ti_bandgap_power(bgp, true);
939 
940 	/* Set default counter to 1 for now */
941 	if (TI_BANDGAP_HAS(bgp, COUNTER))
942 		for (i = 0; i < bgp->conf->sensor_count; i++)
943 			RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
944 
945 	/* Set default thresholds for alert and shutdown */
946 	for (i = 0; i < bgp->conf->sensor_count; i++) {
947 		struct temp_sensor_data *ts_data;
948 
949 		ts_data = bgp->conf->sensors[i].ts_data;
950 
951 		if (TI_BANDGAP_HAS(bgp, TALERT)) {
952 			/* Set initial Talert thresholds */
953 			RMW_BITS(bgp, i, bgap_threshold,
954 				 threshold_tcold_mask, ts_data->t_cold);
955 			RMW_BITS(bgp, i, bgap_threshold,
956 				 threshold_thot_mask, ts_data->t_hot);
957 			/* Enable the alert events */
958 			RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
959 			RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
960 		}
961 
962 		if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
963 			/* Set initial Tshut thresholds */
964 			RMW_BITS(bgp, i, tshut_threshold,
965 				 tshut_hot_mask, ts_data->tshut_hot);
966 			RMW_BITS(bgp, i, tshut_threshold,
967 				 tshut_cold_mask, ts_data->tshut_cold);
968 		}
969 	}
970 
971 	if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
972 		ti_bandgap_set_continuous_mode(bgp);
973 
974 	/* Set .250 seconds time as default counter */
975 	if (TI_BANDGAP_HAS(bgp, COUNTER))
976 		for (i = 0; i < bgp->conf->sensor_count; i++)
977 			RMW_BITS(bgp, i, bgap_counter, counter_mask,
978 				 bgp->clk_rate / 4);
979 
980 	/* Every thing is good? Then expose the sensors */
981 	for (i = 0; i < bgp->conf->sensor_count; i++) {
982 		char *domain;
983 
984 		if (bgp->conf->sensors[i].register_cooling) {
985 			ret = bgp->conf->sensors[i].register_cooling(bgp, i);
986 			if (ret)
987 				goto remove_sensors;
988 		}
989 
990 		if (bgp->conf->expose_sensor) {
991 			domain = bgp->conf->sensors[i].domain;
992 			ret = bgp->conf->expose_sensor(bgp, i, domain);
993 			if (ret)
994 				goto remove_last_cooling;
995 		}
996 	}
997 
998 	/*
999 	 * Enable the Interrupts once everything is set. Otherwise irq handler
1000 	 * might be called as soon as it is enabled where as rest of framework
1001 	 * is still getting initialised.
1002 	 */
1003 	if (TI_BANDGAP_HAS(bgp, TALERT)) {
1004 		ret = ti_bandgap_talert_init(bgp, pdev);
1005 		if (ret) {
1006 			dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
1007 			i = bgp->conf->sensor_count;
1008 			goto disable_clk;
1009 		}
1010 	}
1011 
1012 	return 0;
1013 
1014 remove_last_cooling:
1015 	if (bgp->conf->sensors[i].unregister_cooling)
1016 		bgp->conf->sensors[i].unregister_cooling(bgp, i);
1017 remove_sensors:
1018 	for (i--; i >= 0; i--) {
1019 		if (bgp->conf->sensors[i].unregister_cooling)
1020 			bgp->conf->sensors[i].unregister_cooling(bgp, i);
1021 		if (bgp->conf->remove_sensor)
1022 			bgp->conf->remove_sensor(bgp, i);
1023 	}
1024 	ti_bandgap_power(bgp, false);
1025 disable_clk:
1026 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1027 		clk_disable_unprepare(bgp->fclock);
1028 put_clks:
1029 	clk_put(bgp->div_clk);
1030 put_fclock:
1031 	clk_put(bgp->fclock);
1032 free_irqs:
1033 	if (TI_BANDGAP_HAS(bgp, TSHUT))
1034 		free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL);
1035 
1036 	return ret;
1037 }
1038 
1039 static
1040 int ti_bandgap_remove(struct platform_device *pdev)
1041 {
1042 	struct ti_bandgap *bgp = platform_get_drvdata(pdev);
1043 	int i;
1044 
1045 	/* First thing is to remove sensor interfaces */
1046 	for (i = 0; i < bgp->conf->sensor_count; i++) {
1047 		if (bgp->conf->sensors[i].unregister_cooling)
1048 			bgp->conf->sensors[i].unregister_cooling(bgp, i);
1049 
1050 		if (bgp->conf->remove_sensor)
1051 			bgp->conf->remove_sensor(bgp, i);
1052 	}
1053 
1054 	ti_bandgap_power(bgp, false);
1055 
1056 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1057 		clk_disable_unprepare(bgp->fclock);
1058 	clk_put(bgp->fclock);
1059 	clk_put(bgp->div_clk);
1060 
1061 	if (TI_BANDGAP_HAS(bgp, TALERT))
1062 		free_irq(bgp->irq, bgp);
1063 
1064 	if (TI_BANDGAP_HAS(bgp, TSHUT))
1065 		free_irq(gpiod_to_irq(bgp->tshut_gpiod), NULL);
1066 
1067 	return 0;
1068 }
1069 
1070 #ifdef CONFIG_PM_SLEEP
1071 static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
1072 {
1073 	int i;
1074 
1075 	for (i = 0; i < bgp->conf->sensor_count; i++) {
1076 		struct temp_sensor_registers *tsr;
1077 		struct temp_sensor_regval *rval;
1078 
1079 		rval = &bgp->regval[i];
1080 		tsr = bgp->conf->sensors[i].registers;
1081 
1082 		if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1083 			rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
1084 							tsr->bgap_mode_ctrl);
1085 		if (TI_BANDGAP_HAS(bgp, COUNTER))
1086 			rval->bg_counter = ti_bandgap_readl(bgp,
1087 							tsr->bgap_counter);
1088 		if (TI_BANDGAP_HAS(bgp, TALERT)) {
1089 			rval->bg_threshold = ti_bandgap_readl(bgp,
1090 							tsr->bgap_threshold);
1091 			rval->bg_ctrl = ti_bandgap_readl(bgp,
1092 						   tsr->bgap_mask_ctrl);
1093 		}
1094 
1095 		if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1096 			rval->tshut_threshold = ti_bandgap_readl(bgp,
1097 						   tsr->tshut_threshold);
1098 	}
1099 
1100 	return 0;
1101 }
1102 
1103 static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
1104 {
1105 	int i;
1106 
1107 	for (i = 0; i < bgp->conf->sensor_count; i++) {
1108 		struct temp_sensor_registers *tsr;
1109 		struct temp_sensor_regval *rval;
1110 		u32 val = 0;
1111 
1112 		rval = &bgp->regval[i];
1113 		tsr = bgp->conf->sensors[i].registers;
1114 
1115 		if (TI_BANDGAP_HAS(bgp, COUNTER))
1116 			val = ti_bandgap_readl(bgp, tsr->bgap_counter);
1117 
1118 		if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1119 			ti_bandgap_writel(bgp, rval->tshut_threshold,
1120 					  tsr->tshut_threshold);
1121 		/* Force immediate temperature measurement and update
1122 		 * of the DTEMP field
1123 		 */
1124 		ti_bandgap_force_single_read(bgp, i);
1125 
1126 		if (TI_BANDGAP_HAS(bgp, COUNTER))
1127 			ti_bandgap_writel(bgp, rval->bg_counter,
1128 					  tsr->bgap_counter);
1129 		if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1130 			ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
1131 					  tsr->bgap_mode_ctrl);
1132 		if (TI_BANDGAP_HAS(bgp, TALERT)) {
1133 			ti_bandgap_writel(bgp, rval->bg_threshold,
1134 					  tsr->bgap_threshold);
1135 			ti_bandgap_writel(bgp, rval->bg_ctrl,
1136 					  tsr->bgap_mask_ctrl);
1137 		}
1138 	}
1139 
1140 	return 0;
1141 }
1142 
1143 static int ti_bandgap_suspend(struct device *dev)
1144 {
1145 	struct ti_bandgap *bgp = dev_get_drvdata(dev);
1146 	int err;
1147 
1148 	err = ti_bandgap_save_ctxt(bgp);
1149 	ti_bandgap_power(bgp, false);
1150 
1151 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1152 		clk_disable_unprepare(bgp->fclock);
1153 
1154 	return err;
1155 }
1156 
1157 static int ti_bandgap_resume(struct device *dev)
1158 {
1159 	struct ti_bandgap *bgp = dev_get_drvdata(dev);
1160 
1161 	if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
1162 		clk_prepare_enable(bgp->fclock);
1163 
1164 	ti_bandgap_power(bgp, true);
1165 
1166 	return ti_bandgap_restore_ctxt(bgp);
1167 }
1168 static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
1169 			 ti_bandgap_resume);
1170 
1171 #define DEV_PM_OPS	(&ti_bandgap_dev_pm_ops)
1172 #else
1173 #define DEV_PM_OPS	NULL
1174 #endif
1175 
1176 static const struct of_device_id of_ti_bandgap_match[] = {
1177 #ifdef CONFIG_OMAP3_THERMAL
1178 	{
1179 		.compatible = "ti,omap34xx-bandgap",
1180 		.data = (void *)&omap34xx_data,
1181 	},
1182 	{
1183 		.compatible = "ti,omap36xx-bandgap",
1184 		.data = (void *)&omap36xx_data,
1185 	},
1186 #endif
1187 #ifdef CONFIG_OMAP4_THERMAL
1188 	{
1189 		.compatible = "ti,omap4430-bandgap",
1190 		.data = (void *)&omap4430_data,
1191 	},
1192 	{
1193 		.compatible = "ti,omap4460-bandgap",
1194 		.data = (void *)&omap4460_data,
1195 	},
1196 	{
1197 		.compatible = "ti,omap4470-bandgap",
1198 		.data = (void *)&omap4470_data,
1199 	},
1200 #endif
1201 #ifdef CONFIG_OMAP5_THERMAL
1202 	{
1203 		.compatible = "ti,omap5430-bandgap",
1204 		.data = (void *)&omap5430_data,
1205 	},
1206 #endif
1207 #ifdef CONFIG_DRA752_THERMAL
1208 	{
1209 		.compatible = "ti,dra752-bandgap",
1210 		.data = (void *)&dra752_data,
1211 	},
1212 #endif
1213 	/* Sentinel */
1214 	{ },
1215 };
1216 MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
1217 
1218 static struct platform_driver ti_bandgap_sensor_driver = {
1219 	.probe = ti_bandgap_probe,
1220 	.remove = ti_bandgap_remove,
1221 	.driver = {
1222 			.name = "ti-soc-thermal",
1223 			.pm = DEV_PM_OPS,
1224 			.of_match_table	= of_ti_bandgap_match,
1225 	},
1226 };
1227 
1228 module_platform_driver(ti_bandgap_sensor_driver);
1229 
1230 MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1231 MODULE_LICENSE("GPL v2");
1232 MODULE_ALIAS("platform:ti-soc-thermal");
1233 MODULE_AUTHOR("Texas Instrument Inc.");
1234