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