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