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