PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / thermal / imx_thermal.c
blob45af765a3198c4b0e55e96c878d609f8b6b34d8e
1 /*
2 * Copyright 2013 Freescale Semiconductor, Inc.
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.
8 */
10 #include <linux/clk.h>
11 #include <linux/cpu_cooling.h>
12 #include <linux/cpufreq.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
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>
22 #include <linux/platform_device.h>
23 #include <linux/regmap.h>
24 #include <linux/slab.h>
25 #include <linux/thermal.h>
26 #include <linux/types.h>
28 #define REG_SET 0x4
29 #define REG_CLR 0x8
30 #define REG_TOG 0xc
32 #define MISC0 0x0150
33 #define MISC0_REFTOP_SELBIASOFF (1 << 3)
35 #define TEMPSENSE0 0x0180
36 #define TEMPSENSE0_ALARM_VALUE_SHIFT 20
37 #define TEMPSENSE0_ALARM_VALUE_MASK (0xfff << TEMPSENSE0_ALARM_VALUE_SHIFT)
38 #define TEMPSENSE0_TEMP_CNT_SHIFT 8
39 #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
40 #define TEMPSENSE0_FINISHED (1 << 2)
41 #define TEMPSENSE0_MEASURE_TEMP (1 << 1)
42 #define TEMPSENSE0_POWER_DOWN (1 << 0)
44 #define TEMPSENSE1 0x0190
45 #define TEMPSENSE1_MEASURE_FREQ 0xffff
47 #define OCOTP_ANA1 0x04e0
49 /* The driver supports 1 passive trip point and 1 critical trip point */
50 enum imx_thermal_trip {
51 IMX_TRIP_PASSIVE,
52 IMX_TRIP_CRITICAL,
53 IMX_TRIP_NUM,
57 * It defines the temperature in millicelsius for passive trip point
58 * that will trigger cooling action when crossed.
60 #define IMX_TEMP_PASSIVE 85000
62 #define IMX_POLLING_DELAY 2000 /* millisecond */
63 #define IMX_PASSIVE_DELAY 1000
65 struct imx_thermal_data {
66 struct thermal_zone_device *tz;
67 struct thermal_cooling_device *cdev;
68 enum thermal_device_mode mode;
69 struct regmap *tempmon;
70 int c1, c2; /* See formula in imx_get_sensor_data() */
71 unsigned long temp_passive;
72 unsigned long temp_critical;
73 unsigned long alarm_temp;
74 unsigned long last_temp;
75 bool irq_enabled;
76 int irq;
77 struct clk *thermal_clk;
80 static void imx_set_alarm_temp(struct imx_thermal_data *data,
81 signed long alarm_temp)
83 struct regmap *map = data->tempmon;
84 int alarm_value;
86 data->alarm_temp = alarm_temp;
87 alarm_value = (alarm_temp - data->c2) / data->c1;
88 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_ALARM_VALUE_MASK);
89 regmap_write(map, TEMPSENSE0 + REG_SET, alarm_value <<
90 TEMPSENSE0_ALARM_VALUE_SHIFT);
93 static int imx_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
95 struct imx_thermal_data *data = tz->devdata;
96 struct regmap *map = data->tempmon;
97 unsigned int n_meas;
98 bool wait;
99 u32 val;
101 if (data->mode == THERMAL_DEVICE_ENABLED) {
102 /* Check if a measurement is currently in progress */
103 regmap_read(map, TEMPSENSE0, &val);
104 wait = !(val & TEMPSENSE0_FINISHED);
105 } else {
107 * Every time we measure the temperature, we will power on the
108 * temperature sensor, enable measurements, take a reading,
109 * disable measurements, power off the temperature sensor.
111 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
112 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
114 wait = true;
118 * According to the temp sensor designers, it may require up to ~17us
119 * to complete a measurement.
121 if (wait)
122 usleep_range(20, 50);
124 regmap_read(map, TEMPSENSE0, &val);
126 if (data->mode != THERMAL_DEVICE_ENABLED) {
127 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
128 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
131 if ((val & TEMPSENSE0_FINISHED) == 0) {
132 dev_dbg(&tz->device, "temp measurement never finished\n");
133 return -EAGAIN;
136 n_meas = (val & TEMPSENSE0_TEMP_CNT_MASK) >> TEMPSENSE0_TEMP_CNT_SHIFT;
138 /* See imx_get_sensor_data() for formula derivation */
139 *temp = data->c2 + data->c1 * n_meas;
141 /* Update alarm value to next higher trip point */
142 if (data->alarm_temp == data->temp_passive && *temp >= data->temp_passive)
143 imx_set_alarm_temp(data, data->temp_critical);
144 if (data->alarm_temp == data->temp_critical && *temp < data->temp_passive) {
145 imx_set_alarm_temp(data, data->temp_passive);
146 dev_dbg(&tz->device, "thermal alarm off: T < %lu\n",
147 data->alarm_temp / 1000);
150 if (*temp != data->last_temp) {
151 dev_dbg(&tz->device, "millicelsius: %ld\n", *temp);
152 data->last_temp = *temp;
155 /* Reenable alarm IRQ if temperature below alarm temperature */
156 if (!data->irq_enabled && *temp < data->alarm_temp) {
157 data->irq_enabled = true;
158 enable_irq(data->irq);
161 return 0;
164 static int imx_get_mode(struct thermal_zone_device *tz,
165 enum thermal_device_mode *mode)
167 struct imx_thermal_data *data = tz->devdata;
169 *mode = data->mode;
171 return 0;
174 static int imx_set_mode(struct thermal_zone_device *tz,
175 enum thermal_device_mode mode)
177 struct imx_thermal_data *data = tz->devdata;
178 struct regmap *map = data->tempmon;
180 if (mode == THERMAL_DEVICE_ENABLED) {
181 tz->polling_delay = IMX_POLLING_DELAY;
182 tz->passive_delay = IMX_PASSIVE_DELAY;
184 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
185 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
187 if (!data->irq_enabled) {
188 data->irq_enabled = true;
189 enable_irq(data->irq);
191 } else {
192 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
193 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
195 tz->polling_delay = 0;
196 tz->passive_delay = 0;
198 if (data->irq_enabled) {
199 disable_irq(data->irq);
200 data->irq_enabled = false;
204 data->mode = mode;
205 thermal_zone_device_update(tz);
207 return 0;
210 static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
211 enum thermal_trip_type *type)
213 *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
214 THERMAL_TRIP_CRITICAL;
215 return 0;
218 static int imx_get_crit_temp(struct thermal_zone_device *tz,
219 unsigned long *temp)
221 struct imx_thermal_data *data = tz->devdata;
223 *temp = data->temp_critical;
224 return 0;
227 static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
228 unsigned long *temp)
230 struct imx_thermal_data *data = tz->devdata;
232 *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
233 data->temp_critical;
234 return 0;
237 static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
238 unsigned long temp)
240 struct imx_thermal_data *data = tz->devdata;
242 if (trip == IMX_TRIP_CRITICAL)
243 return -EPERM;
245 if (temp > IMX_TEMP_PASSIVE)
246 return -EINVAL;
248 data->temp_passive = temp;
250 imx_set_alarm_temp(data, temp);
252 return 0;
255 static int imx_bind(struct thermal_zone_device *tz,
256 struct thermal_cooling_device *cdev)
258 int ret;
260 ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
261 THERMAL_NO_LIMIT,
262 THERMAL_NO_LIMIT);
263 if (ret) {
264 dev_err(&tz->device,
265 "binding zone %s with cdev %s failed:%d\n",
266 tz->type, cdev->type, ret);
267 return ret;
270 return 0;
273 static int imx_unbind(struct thermal_zone_device *tz,
274 struct thermal_cooling_device *cdev)
276 int ret;
278 ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
279 if (ret) {
280 dev_err(&tz->device,
281 "unbinding zone %s with cdev %s failed:%d\n",
282 tz->type, cdev->type, ret);
283 return ret;
286 return 0;
289 static struct thermal_zone_device_ops imx_tz_ops = {
290 .bind = imx_bind,
291 .unbind = imx_unbind,
292 .get_temp = imx_get_temp,
293 .get_mode = imx_get_mode,
294 .set_mode = imx_set_mode,
295 .get_trip_type = imx_get_trip_type,
296 .get_trip_temp = imx_get_trip_temp,
297 .get_crit_temp = imx_get_crit_temp,
298 .set_trip_temp = imx_set_trip_temp,
301 static int imx_get_sensor_data(struct platform_device *pdev)
303 struct imx_thermal_data *data = platform_get_drvdata(pdev);
304 struct regmap *map;
305 int t1, t2, n1, n2;
306 int ret;
307 u32 val;
309 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
310 "fsl,tempmon-data");
311 if (IS_ERR(map)) {
312 ret = PTR_ERR(map);
313 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
314 return ret;
317 ret = regmap_read(map, OCOTP_ANA1, &val);
318 if (ret) {
319 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
320 return ret;
323 if (val == 0 || val == ~0) {
324 dev_err(&pdev->dev, "invalid sensor calibration data\n");
325 return -EINVAL;
329 * Sensor data layout:
330 * [31:20] - sensor value @ 25C
331 * [19:8] - sensor value of hot
332 * [7:0] - hot temperature value
334 n1 = val >> 20;
335 n2 = (val & 0xfff00) >> 8;
336 t2 = val & 0xff;
337 t1 = 25; /* t1 always 25C */
340 * Derived from linear interpolation,
341 * Tmeas = T2 + (Nmeas - N2) * (T1 - T2) / (N1 - N2)
342 * We want to reduce this down to the minimum computation necessary
343 * for each temperature read. Also, we want Tmeas in millicelsius
344 * and we don't want to lose precision from integer division. So...
345 * milli_Tmeas = 1000 * T2 + 1000 * (Nmeas - N2) * (T1 - T2) / (N1 - N2)
346 * Let constant c1 = 1000 * (T1 - T2) / (N1 - N2)
347 * milli_Tmeas = (1000 * T2) + c1 * (Nmeas - N2)
348 * milli_Tmeas = (1000 * T2) + (c1 * Nmeas) - (c1 * N2)
349 * Let constant c2 = (1000 * T2) - (c1 * N2)
350 * milli_Tmeas = c2 + (c1 * Nmeas)
352 data->c1 = 1000 * (t1 - t2) / (n1 - n2);
353 data->c2 = 1000 * t2 - data->c1 * n2;
356 * Set the default passive cooling trip point to 20 °C below the
357 * maximum die temperature. Can be changed from userspace.
359 data->temp_passive = 1000 * (t2 - 20);
362 * The maximum die temperature is t2, let's give 5 °C cushion
363 * for noise and possible temperature rise between measurements.
365 data->temp_critical = 1000 * (t2 - 5);
367 return 0;
370 static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
372 struct imx_thermal_data *data = dev;
374 disable_irq_nosync(irq);
375 data->irq_enabled = false;
377 return IRQ_WAKE_THREAD;
380 static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
382 struct imx_thermal_data *data = dev;
384 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %lu\n",
385 data->alarm_temp / 1000);
387 thermal_zone_device_update(data->tz);
389 return IRQ_HANDLED;
392 static int imx_thermal_probe(struct platform_device *pdev)
394 struct imx_thermal_data *data;
395 struct cpumask clip_cpus;
396 struct regmap *map;
397 int measure_freq;
398 int ret;
400 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
401 if (!data)
402 return -ENOMEM;
404 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
405 if (IS_ERR(map)) {
406 ret = PTR_ERR(map);
407 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
408 return ret;
410 data->tempmon = map;
412 data->irq = platform_get_irq(pdev, 0);
413 if (data->irq < 0)
414 return data->irq;
416 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
417 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
418 0, "imx_thermal", data);
419 if (ret < 0) {
420 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
421 return ret;
424 platform_set_drvdata(pdev, data);
426 ret = imx_get_sensor_data(pdev);
427 if (ret) {
428 dev_err(&pdev->dev, "failed to get sensor data\n");
429 return ret;
432 /* Make sure sensor is in known good state for measurements */
433 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
434 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
435 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
436 regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
437 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
439 cpumask_set_cpu(0, &clip_cpus);
440 data->cdev = cpufreq_cooling_register(&clip_cpus);
441 if (IS_ERR(data->cdev)) {
442 ret = PTR_ERR(data->cdev);
443 dev_err(&pdev->dev,
444 "failed to register cpufreq cooling device: %d\n", ret);
445 return ret;
448 data->tz = thermal_zone_device_register("imx_thermal_zone",
449 IMX_TRIP_NUM,
450 BIT(IMX_TRIP_PASSIVE), data,
451 &imx_tz_ops, NULL,
452 IMX_PASSIVE_DELAY,
453 IMX_POLLING_DELAY);
454 if (IS_ERR(data->tz)) {
455 ret = PTR_ERR(data->tz);
456 dev_err(&pdev->dev,
457 "failed to register thermal zone device %d\n", ret);
458 cpufreq_cooling_unregister(data->cdev);
459 return ret;
462 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
463 if (IS_ERR(data->thermal_clk)) {
464 dev_warn(&pdev->dev, "failed to get thermal clk!\n");
465 } else {
467 * Thermal sensor needs clk on to get correct value, normally
468 * we should enable its clk before taking measurement and disable
469 * clk after measurement is done, but if alarm function is enabled,
470 * hardware will auto measure the temperature periodically, so we
471 * need to keep the clk always on for alarm function.
473 ret = clk_prepare_enable(data->thermal_clk);
474 if (ret)
475 dev_warn(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
478 /* Enable measurements at ~ 10 Hz */
479 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
480 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
481 regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
482 imx_set_alarm_temp(data, data->temp_passive);
483 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
484 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
486 data->irq_enabled = true;
487 data->mode = THERMAL_DEVICE_ENABLED;
489 return 0;
492 static int imx_thermal_remove(struct platform_device *pdev)
494 struct imx_thermal_data *data = platform_get_drvdata(pdev);
495 struct regmap *map = data->tempmon;
497 /* Disable measurements */
498 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
499 if (!IS_ERR(data->thermal_clk))
500 clk_disable_unprepare(data->thermal_clk);
502 thermal_zone_device_unregister(data->tz);
503 cpufreq_cooling_unregister(data->cdev);
505 return 0;
508 #ifdef CONFIG_PM_SLEEP
509 static int imx_thermal_suspend(struct device *dev)
511 struct imx_thermal_data *data = dev_get_drvdata(dev);
512 struct regmap *map = data->tempmon;
515 * Need to disable thermal sensor, otherwise, when thermal core
516 * try to get temperature before thermal sensor resume, a wrong
517 * temperature will be read as the thermal sensor is powered
518 * down.
520 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
521 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
522 data->mode = THERMAL_DEVICE_DISABLED;
524 return 0;
527 static int imx_thermal_resume(struct device *dev)
529 struct imx_thermal_data *data = dev_get_drvdata(dev);
530 struct regmap *map = data->tempmon;
532 /* Enabled thermal sensor after resume */
533 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
534 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
535 data->mode = THERMAL_DEVICE_ENABLED;
537 return 0;
539 #endif
541 static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
542 imx_thermal_suspend, imx_thermal_resume);
544 static const struct of_device_id of_imx_thermal_match[] = {
545 { .compatible = "fsl,imx6q-tempmon", },
546 { /* end */ }
548 MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
550 static struct platform_driver imx_thermal = {
551 .driver = {
552 .name = "imx_thermal",
553 .owner = THIS_MODULE,
554 .pm = &imx_thermal_pm_ops,
555 .of_match_table = of_imx_thermal_match,
557 .probe = imx_thermal_probe,
558 .remove = imx_thermal_remove,
560 module_platform_driver(imx_thermal);
562 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
563 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
564 MODULE_LICENSE("GPL v2");
565 MODULE_ALIAS("platform:imx-thermal");