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