thermal: mediatek: add support for MT7622 SoC
[linux-block.git] / drivers / thermal / imx_thermal.c
CommitLineData
ca3de46b
SG
1/*
2 * Copyright 2013 Freescale Semiconductor, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 */
9
329fe7b1 10#include <linux/clk.h>
4d753aa7 11#include <linux/cpufreq.h>
ca3de46b 12#include <linux/cpu_cooling.h>
ca3de46b
SG
13#include <linux/delay.h>
14#include <linux/device.h>
15#include <linux/init.h>
37713a1e 16#include <linux/interrupt.h>
ca3de46b
SG
17#include <linux/io.h>
18#include <linux/kernel.h>
19#include <linux/mfd/syscon.h>
20#include <linux/module.h>
21#include <linux/of.h>
3c94f17e 22#include <linux/of_device.h>
ca3de46b
SG
23#include <linux/platform_device.h>
24#include <linux/regmap.h>
25#include <linux/slab.h>
26#include <linux/thermal.h>
27#include <linux/types.h>
ae621557 28#include <linux/nvmem-consumer.h>
ca3de46b
SG
29
30#define REG_SET 0x4
31#define REG_CLR 0x8
32#define REG_TOG 0xc
33
34#define MISC0 0x0150
35#define MISC0_REFTOP_SELBIASOFF (1 << 3)
3c94f17e
AH
36#define MISC1 0x0160
37#define MISC1_IRQ_TEMPHIGH (1 << 29)
38/* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
39#define MISC1_IRQ_TEMPLOW (1 << 28)
40#define MISC1_IRQ_TEMPPANIC (1 << 27)
ca3de46b
SG
41
42#define TEMPSENSE0 0x0180
37713a1e
PZ
43#define TEMPSENSE0_ALARM_VALUE_SHIFT 20
44#define TEMPSENSE0_ALARM_VALUE_MASK (0xfff << TEMPSENSE0_ALARM_VALUE_SHIFT)
ca3de46b
SG
45#define TEMPSENSE0_TEMP_CNT_SHIFT 8
46#define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
47#define TEMPSENSE0_FINISHED (1 << 2)
48#define TEMPSENSE0_MEASURE_TEMP (1 << 1)
49#define TEMPSENSE0_POWER_DOWN (1 << 0)
50
51#define TEMPSENSE1 0x0190
52#define TEMPSENSE1_MEASURE_FREQ 0xffff
3c94f17e
AH
53/* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
54#define TEMPSENSE2 0x0290
55#define TEMPSENSE2_LOW_VALUE_SHIFT 0
56#define TEMPSENSE2_LOW_VALUE_MASK 0xfff
57#define TEMPSENSE2_PANIC_VALUE_SHIFT 16
58#define TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
ca3de46b 59
a2291bad 60#define OCOTP_MEM0 0x0480
ca3de46b
SG
61#define OCOTP_ANA1 0x04e0
62
63/* The driver supports 1 passive trip point and 1 critical trip point */
64enum imx_thermal_trip {
65 IMX_TRIP_PASSIVE,
66 IMX_TRIP_CRITICAL,
67 IMX_TRIP_NUM,
68};
69
ca3de46b
SG
70#define IMX_POLLING_DELAY 2000 /* millisecond */
71#define IMX_PASSIVE_DELAY 1000
72
3c94f17e
AH
73#define TEMPMON_IMX6Q 1
74#define TEMPMON_IMX6SX 2
75
76struct thermal_soc_data {
77 u32 version;
78};
79
80static struct thermal_soc_data thermal_imx6q_data = {
81 .version = TEMPMON_IMX6Q,
82};
83
84static struct thermal_soc_data thermal_imx6sx_data = {
85 .version = TEMPMON_IMX6SX,
86};
87
ca3de46b 88struct imx_thermal_data {
4d753aa7 89 struct cpufreq_policy *policy;
ca3de46b
SG
90 struct thermal_zone_device *tz;
91 struct thermal_cooling_device *cdev;
92 enum thermal_device_mode mode;
93 struct regmap *tempmon;
ae621557 94 u32 c1, c2; /* See formula in imx_init_calib() */
17e8351a
SH
95 int temp_passive;
96 int temp_critical;
a2291bad 97 int temp_max;
17e8351a
SH
98 int alarm_temp;
99 int last_temp;
37713a1e
PZ
100 bool irq_enabled;
101 int irq;
329fe7b1 102 struct clk *thermal_clk;
3c94f17e 103 const struct thermal_soc_data *socdata;
a2291bad 104 const char *temp_grade;
ca3de46b
SG
105};
106
3c94f17e 107static void imx_set_panic_temp(struct imx_thermal_data *data,
17e8351a 108 int panic_temp)
3c94f17e
AH
109{
110 struct regmap *map = data->tempmon;
111 int critical_value;
112
113 critical_value = (data->c2 - panic_temp) / data->c1;
114 regmap_write(map, TEMPSENSE2 + REG_CLR, TEMPSENSE2_PANIC_VALUE_MASK);
115 regmap_write(map, TEMPSENSE2 + REG_SET, critical_value <<
116 TEMPSENSE2_PANIC_VALUE_SHIFT);
117}
118
37713a1e 119static void imx_set_alarm_temp(struct imx_thermal_data *data,
17e8351a 120 int alarm_temp)
37713a1e
PZ
121{
122 struct regmap *map = data->tempmon;
123 int alarm_value;
124
125 data->alarm_temp = alarm_temp;
749e8be7 126 alarm_value = (data->c2 - alarm_temp) / data->c1;
37713a1e
PZ
127 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_ALARM_VALUE_MASK);
128 regmap_write(map, TEMPSENSE0 + REG_SET, alarm_value <<
129 TEMPSENSE0_ALARM_VALUE_SHIFT);
130}
131
17e8351a 132static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
ca3de46b
SG
133{
134 struct imx_thermal_data *data = tz->devdata;
135 struct regmap *map = data->tempmon;
ca3de46b 136 unsigned int n_meas;
37713a1e 137 bool wait;
ca3de46b
SG
138 u32 val;
139
37713a1e
PZ
140 if (data->mode == THERMAL_DEVICE_ENABLED) {
141 /* Check if a measurement is currently in progress */
142 regmap_read(map, TEMPSENSE0, &val);
143 wait = !(val & TEMPSENSE0_FINISHED);
144 } else {
145 /*
146 * Every time we measure the temperature, we will power on the
147 * temperature sensor, enable measurements, take a reading,
148 * disable measurements, power off the temperature sensor.
149 */
150 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
151 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
152
153 wait = true;
154 }
ca3de46b
SG
155
156 /*
157 * According to the temp sensor designers, it may require up to ~17us
158 * to complete a measurement.
159 */
37713a1e
PZ
160 if (wait)
161 usleep_range(20, 50);
ca3de46b
SG
162
163 regmap_read(map, TEMPSENSE0, &val);
37713a1e
PZ
164
165 if (data->mode != THERMAL_DEVICE_ENABLED) {
166 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
167 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
168 }
ca3de46b
SG
169
170 if ((val & TEMPSENSE0_FINISHED) == 0) {
171 dev_dbg(&tz->device, "temp measurement never finished\n");
172 return -EAGAIN;
173 }
174
175 n_meas = (val & TEMPSENSE0_TEMP_CNT_MASK) >> TEMPSENSE0_TEMP_CNT_SHIFT;
176
ae621557 177 /* See imx_init_calib() for formula derivation */
749e8be7 178 *temp = data->c2 - n_meas * data->c1;
ca3de46b 179
3c94f17e
AH
180 /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
181 if (data->socdata->version == TEMPMON_IMX6Q) {
182 if (data->alarm_temp == data->temp_passive &&
183 *temp >= data->temp_passive)
184 imx_set_alarm_temp(data, data->temp_critical);
185 if (data->alarm_temp == data->temp_critical &&
186 *temp < data->temp_passive) {
187 imx_set_alarm_temp(data, data->temp_passive);
17e8351a 188 dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
3c94f17e
AH
189 data->alarm_temp / 1000);
190 }
37713a1e
PZ
191 }
192
193 if (*temp != data->last_temp) {
17e8351a 194 dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
37713a1e
PZ
195 data->last_temp = *temp;
196 }
197
198 /* Reenable alarm IRQ if temperature below alarm temperature */
199 if (!data->irq_enabled && *temp < data->alarm_temp) {
200 data->irq_enabled = true;
201 enable_irq(data->irq);
ca3de46b
SG
202 }
203
204 return 0;
205}
206
207static int imx_get_mode(struct thermal_zone_device *tz,
208 enum thermal_device_mode *mode)
209{
210 struct imx_thermal_data *data = tz->devdata;
211
212 *mode = data->mode;
213
214 return 0;
215}
216
217static int imx_set_mode(struct thermal_zone_device *tz,
218 enum thermal_device_mode mode)
219{
220 struct imx_thermal_data *data = tz->devdata;
37713a1e 221 struct regmap *map = data->tempmon;
ca3de46b
SG
222
223 if (mode == THERMAL_DEVICE_ENABLED) {
224 tz->polling_delay = IMX_POLLING_DELAY;
225 tz->passive_delay = IMX_PASSIVE_DELAY;
37713a1e
PZ
226
227 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
228 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
229
230 if (!data->irq_enabled) {
231 data->irq_enabled = true;
232 enable_irq(data->irq);
233 }
ca3de46b 234 } else {
37713a1e
PZ
235 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
236 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
237
ca3de46b
SG
238 tz->polling_delay = 0;
239 tz->passive_delay = 0;
37713a1e
PZ
240
241 if (data->irq_enabled) {
242 disable_irq(data->irq);
243 data->irq_enabled = false;
244 }
ca3de46b
SG
245 }
246
247 data->mode = mode;
0e70f466 248 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
ca3de46b
SG
249
250 return 0;
251}
252
253static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
254 enum thermal_trip_type *type)
255{
256 *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
257 THERMAL_TRIP_CRITICAL;
258 return 0;
259}
260
17e8351a 261static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
ca3de46b 262{
017e5142
PZ
263 struct imx_thermal_data *data = tz->devdata;
264
265 *temp = data->temp_critical;
ca3de46b
SG
266 return 0;
267}
268
269static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
17e8351a 270 int *temp)
ca3de46b 271{
017e5142
PZ
272 struct imx_thermal_data *data = tz->devdata;
273
274 *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
275 data->temp_critical;
276 return 0;
277}
278
279static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
17e8351a 280 int temp)
017e5142
PZ
281{
282 struct imx_thermal_data *data = tz->devdata;
283
a2291bad 284 /* do not allow changing critical threshold */
017e5142
PZ
285 if (trip == IMX_TRIP_CRITICAL)
286 return -EPERM;
287
a2291bad
TH
288 /* do not allow passive to be set higher than critical */
289 if (temp < 0 || temp > data->temp_critical)
017e5142
PZ
290 return -EINVAL;
291
292 data->temp_passive = temp;
293
37713a1e
PZ
294 imx_set_alarm_temp(data, temp);
295
ca3de46b
SG
296 return 0;
297}
298
299static int imx_bind(struct thermal_zone_device *tz,
300 struct thermal_cooling_device *cdev)
301{
302 int ret;
303
304 ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
305 THERMAL_NO_LIMIT,
6cd9e9f6
KS
306 THERMAL_NO_LIMIT,
307 THERMAL_WEIGHT_DEFAULT);
ca3de46b
SG
308 if (ret) {
309 dev_err(&tz->device,
310 "binding zone %s with cdev %s failed:%d\n",
311 tz->type, cdev->type, ret);
312 return ret;
313 }
314
315 return 0;
316}
317
318static int imx_unbind(struct thermal_zone_device *tz,
319 struct thermal_cooling_device *cdev)
320{
321 int ret;
322
323 ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
324 if (ret) {
325 dev_err(&tz->device,
326 "unbinding zone %s with cdev %s failed:%d\n",
327 tz->type, cdev->type, ret);
328 return ret;
329 }
330
331 return 0;
332}
333
cbb07bb3 334static struct thermal_zone_device_ops imx_tz_ops = {
ca3de46b
SG
335 .bind = imx_bind,
336 .unbind = imx_unbind,
337 .get_temp = imx_get_temp,
338 .get_mode = imx_get_mode,
339 .set_mode = imx_set_mode,
340 .get_trip_type = imx_get_trip_type,
341 .get_trip_temp = imx_get_trip_temp,
342 .get_crit_temp = imx_get_crit_temp,
017e5142 343 .set_trip_temp = imx_set_trip_temp,
ca3de46b
SG
344};
345
e4bb2240 346static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
ca3de46b
SG
347{
348 struct imx_thermal_data *data = platform_get_drvdata(pdev);
4e5f61ca 349 int n1;
749e8be7 350 u64 temp64;
ca3de46b 351
e4bb2240 352 if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
ca3de46b
SG
353 dev_err(&pdev->dev, "invalid sensor calibration data\n");
354 return -EINVAL;
355 }
356
357 /*
c5bbdb4b
UKK
358 * The sensor is calibrated at 25 °C (aka T1) and the value measured
359 * (aka N1) at this temperature is provided in bits [31:20] in the
360 * i.MX's OCOTP value ANA1.
361 * To find the actual temperature T, the following formula has to be used
362 * when reading value n from the sensor:
363 *
4e5f61ca
UKK
364 * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C
365 * = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C
366 * = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C
c5bbdb4b
UKK
367 * = c2 - c1 * N
368 *
369 * with
370 *
4e5f61ca
UKK
371 * T1' = 28.580661 °C
372 * c1 = 1 / (0.0015423 * N1 - 0.4297157) °C
373 * c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C
374 * = T1' + N1 * c1
ca3de46b 375 */
e4bb2240 376 n1 = ocotp_ana1 >> 20;
ca3de46b 377
4e5f61ca 378 temp64 = 10000000; /* use 10^7 as fixed point constant for values in formula */
c5bbdb4b 379 temp64 *= 1000; /* to get result in °mC */
4e5f61ca 380 do_div(temp64, 15423 * n1 - 4148468);
749e8be7 381 data->c1 = temp64;
4e5f61ca 382 data->c2 = n1 * data->c1 + 28581;
ca3de46b 383
ae621557
LC
384 return 0;
385}
386
e4bb2240 387static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
ae621557
LC
388{
389 struct imx_thermal_data *data = platform_get_drvdata(pdev);
a2291bad
TH
390
391 /* The maximum die temp is specified by the Temperature Grade */
e4bb2240 392 switch ((ocotp_mem0 >> 6) & 0x3) {
339d7492 393 case 0: /* Commercial (0 to 95 °C) */
a2291bad
TH
394 data->temp_grade = "Commercial";
395 data->temp_max = 95000;
396 break;
339d7492 397 case 1: /* Extended Commercial (-20 °C to 105 °C) */
a2291bad
TH
398 data->temp_grade = "Extended Commercial";
399 data->temp_max = 105000;
400 break;
339d7492 401 case 2: /* Industrial (-40 °C to 105 °C) */
a2291bad
TH
402 data->temp_grade = "Industrial";
403 data->temp_max = 105000;
404 break;
339d7492 405 case 3: /* Automotive (-40 °C to 125 °C) */
a2291bad
TH
406 data->temp_grade = "Automotive";
407 data->temp_max = 125000;
408 break;
409 }
017e5142
PZ
410
411 /*
339d7492
UKK
412 * Set the critical trip point at 5 °C under max
413 * Set the passive trip point at 10 °C under max (changeable via sysfs)
017e5142 414 */
a2291bad
TH
415 data->temp_critical = data->temp_max - (1000 * 5);
416 data->temp_passive = data->temp_max - (1000 * 10);
ae621557
LC
417}
418
419static int imx_init_from_tempmon_data(struct platform_device *pdev)
420{
421 struct regmap *map;
422 int ret;
423 u32 val;
424
425 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
426 "fsl,tempmon-data");
427 if (IS_ERR(map)) {
428 ret = PTR_ERR(map);
429 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
430 return ret;
431 }
432
433 ret = regmap_read(map, OCOTP_ANA1, &val);
434 if (ret) {
435 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
436 return ret;
437 }
438 ret = imx_init_calib(pdev, val);
439 if (ret)
440 return ret;
441
442 ret = regmap_read(map, OCOTP_MEM0, &val);
443 if (ret) {
444 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
445 return ret;
446 }
447 imx_init_temp_grade(pdev, val);
448
449 return 0;
450}
451
452static int imx_init_from_nvmem_cells(struct platform_device *pdev)
453{
454 int ret;
455 u32 val;
456
457 ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
458 if (ret)
459 return ret;
460 imx_init_calib(pdev, val);
461
462 ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
463 if (ret)
464 return ret;
465 imx_init_temp_grade(pdev, val);
017e5142 466
ca3de46b
SG
467 return 0;
468}
469
37713a1e
PZ
470static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
471{
472 struct imx_thermal_data *data = dev;
473
474 disable_irq_nosync(irq);
475 data->irq_enabled = false;
476
477 return IRQ_WAKE_THREAD;
478}
479
480static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
481{
482 struct imx_thermal_data *data = dev;
483
17e8351a 484 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
37713a1e
PZ
485 data->alarm_temp / 1000);
486
0e70f466 487 thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
37713a1e
PZ
488
489 return IRQ_HANDLED;
490}
491
3c94f17e
AH
492static const struct of_device_id of_imx_thermal_match[] = {
493 { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
494 { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
495 { /* end */ }
496};
497MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
498
ca3de46b
SG
499static int imx_thermal_probe(struct platform_device *pdev)
500{
501 struct imx_thermal_data *data;
ca3de46b 502 struct regmap *map;
37713a1e 503 int measure_freq;
ca3de46b
SG
504 int ret;
505
506 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
507 if (!data)
508 return -ENOMEM;
509
510 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
511 if (IS_ERR(map)) {
512 ret = PTR_ERR(map);
513 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
514 return ret;
515 }
516 data->tempmon = map;
517
829bc78a 518 data->socdata = of_device_get_match_data(&pdev->dev);
8b051ec3
SV
519 if (!data->socdata) {
520 dev_err(&pdev->dev, "no device match found\n");
521 return -ENODEV;
522 }
3c94f17e
AH
523
524 /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
525 if (data->socdata->version == TEMPMON_IMX6SX) {
526 regmap_write(map, MISC1 + REG_CLR, MISC1_IRQ_TEMPHIGH |
527 MISC1_IRQ_TEMPLOW | MISC1_IRQ_TEMPPANIC);
528 /*
529 * reset value of LOW ALARM is incorrect, set it to lowest
530 * value to avoid false trigger of low alarm.
531 */
532 regmap_write(map, TEMPSENSE2 + REG_SET,
533 TEMPSENSE2_LOW_VALUE_MASK);
534 }
535
37713a1e
PZ
536 data->irq = platform_get_irq(pdev, 0);
537 if (data->irq < 0)
538 return data->irq;
539
ca3de46b
SG
540 platform_set_drvdata(pdev, data);
541
ae621557
LC
542 if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
543 ret = imx_init_from_nvmem_cells(pdev);
544 if (ret == -EPROBE_DEFER)
545 return ret;
546 if (ret) {
547 dev_err(&pdev->dev, "failed to init from nvmem: %d\n",
548 ret);
549 return ret;
550 }
551 } else {
552 ret = imx_init_from_tempmon_data(pdev);
553 if (ret) {
554 dev_err(&pdev->dev, "failed to init from from fsl,tempmon-data\n");
555 return ret;
556 }
ca3de46b
SG
557 }
558
559 /* Make sure sensor is in known good state for measurements */
560 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
561 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
562 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
563 regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
564 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
565
4d753aa7
VK
566 data->policy = cpufreq_cpu_get(0);
567 if (!data->policy) {
568 pr_debug("%s: CPUFreq policy not found\n", __func__);
569 return -EPROBE_DEFER;
570 }
571
572 data->cdev = cpufreq_cooling_register(data->policy);
ca3de46b
SG
573 if (IS_ERR(data->cdev)) {
574 ret = PTR_ERR(data->cdev);
4d753aa7
VK
575 dev_err(&pdev->dev,
576 "failed to register cpufreq cooling device: %d\n", ret);
577 cpufreq_cpu_put(data->policy);
ca3de46b
SG
578 return ret;
579 }
580
90a21ff5
HK
581 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
582 if (IS_ERR(data->thermal_clk)) {
583 ret = PTR_ERR(data->thermal_clk);
584 if (ret != -EPROBE_DEFER)
585 dev_err(&pdev->dev,
586 "failed to get thermal clk: %d\n", ret);
587 cpufreq_cooling_unregister(data->cdev);
4d753aa7 588 cpufreq_cpu_put(data->policy);
90a21ff5
HK
589 return ret;
590 }
591
592 /*
593 * Thermal sensor needs clk on to get correct value, normally
594 * we should enable its clk before taking measurement and disable
595 * clk after measurement is done, but if alarm function is enabled,
596 * hardware will auto measure the temperature periodically, so we
597 * need to keep the clk always on for alarm function.
598 */
599 ret = clk_prepare_enable(data->thermal_clk);
600 if (ret) {
601 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
602 cpufreq_cooling_unregister(data->cdev);
4d753aa7 603 cpufreq_cpu_put(data->policy);
90a21ff5
HK
604 return ret;
605 }
606
ca3de46b 607 data->tz = thermal_zone_device_register("imx_thermal_zone",
017e5142
PZ
608 IMX_TRIP_NUM,
609 BIT(IMX_TRIP_PASSIVE), data,
ca3de46b
SG
610 &imx_tz_ops, NULL,
611 IMX_PASSIVE_DELAY,
612 IMX_POLLING_DELAY);
613 if (IS_ERR(data->tz)) {
614 ret = PTR_ERR(data->tz);
615 dev_err(&pdev->dev,
616 "failed to register thermal zone device %d\n", ret);
90a21ff5 617 clk_disable_unprepare(data->thermal_clk);
ca3de46b 618 cpufreq_cooling_unregister(data->cdev);
4d753aa7 619 cpufreq_cpu_put(data->policy);
ca3de46b
SG
620 return ret;
621 }
622
a2291bad
TH
623 dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
624 " critical:%dC passive:%dC\n", data->temp_grade,
625 data->temp_max / 1000, data->temp_critical / 1000,
626 data->temp_passive / 1000);
627
37713a1e
PZ
628 /* Enable measurements at ~ 10 Hz */
629 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
630 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
631 regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
632 imx_set_alarm_temp(data, data->temp_passive);
3c94f17e
AH
633
634 if (data->socdata->version == TEMPMON_IMX6SX)
635 imx_set_panic_temp(data, data->temp_critical);
636
37713a1e
PZ
637 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
638 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
639
cf1ba1d7
ML
640 data->irq_enabled = true;
641 data->mode = THERMAL_DEVICE_ENABLED;
642
84866ee5
BP
643 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
644 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
645 0, "imx_thermal", data);
646 if (ret < 0) {
647 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
648 clk_disable_unprepare(data->thermal_clk);
649 thermal_zone_device_unregister(data->tz);
650 cpufreq_cooling_unregister(data->cdev);
4d753aa7 651 cpufreq_cpu_put(data->policy);
84866ee5
BP
652 return ret;
653 }
654
ca3de46b
SG
655 return 0;
656}
657
658static int imx_thermal_remove(struct platform_device *pdev)
659{
660 struct imx_thermal_data *data = platform_get_drvdata(pdev);
37713a1e
PZ
661 struct regmap *map = data->tempmon;
662
663 /* Disable measurements */
664 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
329fe7b1
AH
665 if (!IS_ERR(data->thermal_clk))
666 clk_disable_unprepare(data->thermal_clk);
ca3de46b
SG
667
668 thermal_zone_device_unregister(data->tz);
669 cpufreq_cooling_unregister(data->cdev);
4d753aa7 670 cpufreq_cpu_put(data->policy);
ca3de46b
SG
671
672 return 0;
673}
674
675#ifdef CONFIG_PM_SLEEP
676static int imx_thermal_suspend(struct device *dev)
677{
678 struct imx_thermal_data *data = dev_get_drvdata(dev);
679 struct regmap *map = data->tempmon;
ca3de46b 680
b46cce59
AH
681 /*
682 * Need to disable thermal sensor, otherwise, when thermal core
683 * try to get temperature before thermal sensor resume, a wrong
684 * temperature will be read as the thermal sensor is powered
685 * down.
686 */
687 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
688 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
689 data->mode = THERMAL_DEVICE_DISABLED;
d26eef8b 690 clk_disable_unprepare(data->thermal_clk);
ca3de46b
SG
691
692 return 0;
693}
694
695static int imx_thermal_resume(struct device *dev)
696{
b46cce59
AH
697 struct imx_thermal_data *data = dev_get_drvdata(dev);
698 struct regmap *map = data->tempmon;
e3bdc8d7 699 int ret;
b46cce59 700
e3bdc8d7
AY
701 ret = clk_prepare_enable(data->thermal_clk);
702 if (ret)
703 return ret;
b46cce59
AH
704 /* Enabled thermal sensor after resume */
705 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
706 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
707 data->mode = THERMAL_DEVICE_ENABLED;
708
ca3de46b
SG
709 return 0;
710}
711#endif
712
713static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
714 imx_thermal_suspend, imx_thermal_resume);
715
ca3de46b
SG
716static struct platform_driver imx_thermal = {
717 .driver = {
718 .name = "imx_thermal",
ca3de46b
SG
719 .pm = &imx_thermal_pm_ops,
720 .of_match_table = of_imx_thermal_match,
721 },
722 .probe = imx_thermal_probe,
723 .remove = imx_thermal_remove,
724};
725module_platform_driver(imx_thermal);
726
727MODULE_AUTHOR("Freescale Semiconductor, Inc.");
728MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
729MODULE_LICENSE("GPL v2");
730MODULE_ALIAS("platform:imx-thermal");