imx6: backport thermal driver
[openwrt/svn-archive/archive.git] / target / linux / imx6 / patches-3.10 / 0015-thermal-add-imx-thermal-driver-support.patch
1 From ca3de46b50809000b5ba708634e26ad979a4a63a Mon Sep 17 00:00:00 2001
2 From: Shawn Guo <shawn.guo@linaro.org>
3 Date: Mon, 24 Jun 2013 14:30:44 +0800
4 Subject: [PATCH] thermal: add imx thermal driver support
5
6 This is based on the initial imx thermal work done by
7 Rob Lee <rob.lee@linaro.org> (Not sure if the email address is still
8 valid). Since he is no longer interested in the work and I have
9 rewritten a significant amount of the code, I just took the authorship
10 over from him.
11
12 It adds the imx thermal support using Temperature Monitor (TEMPMON)
13 block found on some Freescale i.MX SoCs. The driver uses syscon regmap
14 interface to access TEMPMON control registers and calibration data, and
15 supports cpufreq as the cooling device.
16
17 Signed-off-by: Shawn Guo <shawn.guo@linaro.org>
18 Signed-off-by: Eduardo Valentin <eduardo.valentin@ti.com>
19 ---
20 .../devicetree/bindings/thermal/imx-thermal.txt | 17 +
21 drivers/thermal/Kconfig | 11 +
22 drivers/thermal/Makefile | 1 +
23 drivers/thermal/imx_thermal.c | 397 +++++++++++++++++++++
24 4 files changed, 426 insertions(+)
25 create mode 100644 Documentation/devicetree/bindings/thermal/imx-thermal.txt
26 create mode 100644 drivers/thermal/imx_thermal.c
27
28 diff --git a/Documentation/devicetree/bindings/thermal/imx-thermal.txt b/Documentation/devicetree/bindings/thermal/imx-thermal.txt
29 new file mode 100644
30 index 0000000..541c25e
31 --- /dev/null
32 +++ b/Documentation/devicetree/bindings/thermal/imx-thermal.txt
33 @@ -0,0 +1,17 @@
34 +* Temperature Monitor (TEMPMON) on Freescale i.MX SoCs
35 +
36 +Required properties:
37 +- compatible : "fsl,imx6q-thermal"
38 +- fsl,tempmon : phandle pointer to system controller that contains TEMPMON
39 + control registers, e.g. ANATOP on imx6q.
40 +- fsl,tempmon-data : phandle pointer to fuse controller that contains TEMPMON
41 + calibration data, e.g. OCOTP on imx6q. The details about calibration data
42 + can be found in SoC Reference Manual.
43 +
44 +Example:
45 +
46 +tempmon {
47 + compatible = "fsl,imx6q-tempmon";
48 + fsl,tempmon = <&anatop>;
49 + fsl,tempmon-data = <&ocotp>;
50 +};
51 diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
52 index e988c81..69eed55 100644
53 --- a/drivers/thermal/Kconfig
54 +++ b/drivers/thermal/Kconfig
55 @@ -91,6 +91,17 @@ config THERMAL_EMULATION
56 because userland can easily disable the thermal policy by simply
57 flooding this sysfs node with low temperature values.
58
59 +config IMX_THERMAL
60 + tristate "Temperature sensor driver for Freescale i.MX SoCs"
61 + depends on CPU_THERMAL
62 + depends on MFD_SYSCON
63 + depends on OF
64 + help
65 + Support for Temperature Monitor (TEMPMON) found on Freescale i.MX SoCs.
66 + It supports one critical trip point and one passive trip point. The
67 + cpufreq is used as the cooling device to throttle CPUs when the
68 + passive trip is crossed.
69 +
70 config SPEAR_THERMAL
71 bool "SPEAr thermal sensor driver"
72 depends on PLAT_SPEAR
73 diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
74 index 67184a2..dff19c6 100644
75 --- a/drivers/thermal/Makefile
76 +++ b/drivers/thermal/Makefile
77 @@ -21,6 +21,7 @@ obj-$(CONFIG_EXYNOS_THERMAL) += exynos_thermal.o
78 obj-$(CONFIG_DOVE_THERMAL) += dove_thermal.o
79 obj-$(CONFIG_DB8500_THERMAL) += db8500_thermal.o
80 obj-$(CONFIG_ARMADA_THERMAL) += armada_thermal.o
81 +obj-$(CONFIG_IMX_THERMAL) += imx_thermal.o
82 obj-$(CONFIG_DB8500_CPUFREQ_COOLING) += db8500_cpufreq_cooling.o
83 obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
84 obj-$(CONFIG_X86_PKG_TEMP_THERMAL) += x86_pkg_temp_thermal.o
85 diff --git a/drivers/thermal/imx_thermal.c b/drivers/thermal/imx_thermal.c
86 new file mode 100644
87 index 0000000..d16c33c
88 --- /dev/null
89 +++ b/drivers/thermal/imx_thermal.c
90 @@ -0,0 +1,397 @@
91 +/*
92 + * Copyright 2013 Freescale Semiconductor, Inc.
93 + *
94 + * This program is free software; you can redistribute it and/or modify
95 + * it under the terms of the GNU General Public License version 2 as
96 + * published by the Free Software Foundation.
97 + *
98 + */
99 +
100 +#include <linux/cpu_cooling.h>
101 +#include <linux/cpufreq.h>
102 +#include <linux/delay.h>
103 +#include <linux/device.h>
104 +#include <linux/init.h>
105 +#include <linux/io.h>
106 +#include <linux/kernel.h>
107 +#include <linux/mfd/syscon.h>
108 +#include <linux/module.h>
109 +#include <linux/of.h>
110 +#include <linux/platform_device.h>
111 +#include <linux/regmap.h>
112 +#include <linux/slab.h>
113 +#include <linux/thermal.h>
114 +#include <linux/types.h>
115 +
116 +#define REG_SET 0x4
117 +#define REG_CLR 0x8
118 +#define REG_TOG 0xc
119 +
120 +#define MISC0 0x0150
121 +#define MISC0_REFTOP_SELBIASOFF (1 << 3)
122 +
123 +#define TEMPSENSE0 0x0180
124 +#define TEMPSENSE0_TEMP_CNT_SHIFT 8
125 +#define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
126 +#define TEMPSENSE0_FINISHED (1 << 2)
127 +#define TEMPSENSE0_MEASURE_TEMP (1 << 1)
128 +#define TEMPSENSE0_POWER_DOWN (1 << 0)
129 +
130 +#define TEMPSENSE1 0x0190
131 +#define TEMPSENSE1_MEASURE_FREQ 0xffff
132 +
133 +#define OCOTP_ANA1 0x04e0
134 +
135 +/* The driver supports 1 passive trip point and 1 critical trip point */
136 +enum imx_thermal_trip {
137 + IMX_TRIP_PASSIVE,
138 + IMX_TRIP_CRITICAL,
139 + IMX_TRIP_NUM,
140 +};
141 +
142 +/*
143 + * It defines the temperature in millicelsius for passive trip point
144 + * that will trigger cooling action when crossed.
145 + */
146 +#define IMX_TEMP_PASSIVE 85000
147 +
148 +/*
149 + * The maximum die temperature on imx parts is 105C, let's give some cushion
150 + * for noise and possible temperature rise between measurements.
151 + */
152 +#define IMX_TEMP_CRITICAL 100000
153 +
154 +#define IMX_POLLING_DELAY 2000 /* millisecond */
155 +#define IMX_PASSIVE_DELAY 1000
156 +
157 +struct imx_thermal_data {
158 + struct thermal_zone_device *tz;
159 + struct thermal_cooling_device *cdev;
160 + enum thermal_device_mode mode;
161 + struct regmap *tempmon;
162 + int c1, c2; /* See formula in imx_get_sensor_data() */
163 +};
164 +
165 +static int imx_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
166 +{
167 + struct imx_thermal_data *data = tz->devdata;
168 + struct regmap *map = data->tempmon;
169 + static unsigned long last_temp;
170 + unsigned int n_meas;
171 + u32 val;
172 +
173 + /*
174 + * Every time we measure the temperature, we will power on the
175 + * temperature sensor, enable measurements, take a reading,
176 + * disable measurements, power off the temperature sensor.
177 + */
178 + regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
179 + regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
180 +
181 + /*
182 + * According to the temp sensor designers, it may require up to ~17us
183 + * to complete a measurement.
184 + */
185 + usleep_range(20, 50);
186 +
187 + regmap_read(map, TEMPSENSE0, &val);
188 + regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
189 + regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
190 +
191 + if ((val & TEMPSENSE0_FINISHED) == 0) {
192 + dev_dbg(&tz->device, "temp measurement never finished\n");
193 + return -EAGAIN;
194 + }
195 +
196 + n_meas = (val & TEMPSENSE0_TEMP_CNT_MASK) >> TEMPSENSE0_TEMP_CNT_SHIFT;
197 +
198 + /* See imx_get_sensor_data() for formula derivation */
199 + *temp = data->c2 + data->c1 * n_meas;
200 +
201 + if (*temp != last_temp) {
202 + dev_dbg(&tz->device, "millicelsius: %ld\n", *temp);
203 + last_temp = *temp;
204 + }
205 +
206 + return 0;
207 +}
208 +
209 +static int imx_get_mode(struct thermal_zone_device *tz,
210 + enum thermal_device_mode *mode)
211 +{
212 + struct imx_thermal_data *data = tz->devdata;
213 +
214 + *mode = data->mode;
215 +
216 + return 0;
217 +}
218 +
219 +static int imx_set_mode(struct thermal_zone_device *tz,
220 + enum thermal_device_mode mode)
221 +{
222 + struct imx_thermal_data *data = tz->devdata;
223 +
224 + if (mode == THERMAL_DEVICE_ENABLED) {
225 + tz->polling_delay = IMX_POLLING_DELAY;
226 + tz->passive_delay = IMX_PASSIVE_DELAY;
227 + } else {
228 + tz->polling_delay = 0;
229 + tz->passive_delay = 0;
230 + }
231 +
232 + data->mode = mode;
233 + thermal_zone_device_update(tz);
234 +
235 + return 0;
236 +}
237 +
238 +static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
239 + enum thermal_trip_type *type)
240 +{
241 + *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
242 + THERMAL_TRIP_CRITICAL;
243 + return 0;
244 +}
245 +
246 +static int imx_get_crit_temp(struct thermal_zone_device *tz,
247 + unsigned long *temp)
248 +{
249 + *temp = IMX_TEMP_CRITICAL;
250 + return 0;
251 +}
252 +
253 +static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
254 + unsigned long *temp)
255 +{
256 + *temp = (trip == IMX_TRIP_PASSIVE) ? IMX_TEMP_PASSIVE :
257 + IMX_TEMP_CRITICAL;
258 + return 0;
259 +}
260 +
261 +static int imx_bind(struct thermal_zone_device *tz,
262 + struct thermal_cooling_device *cdev)
263 +{
264 + int ret;
265 +
266 + ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
267 + THERMAL_NO_LIMIT,
268 + THERMAL_NO_LIMIT);
269 + if (ret) {
270 + dev_err(&tz->device,
271 + "binding zone %s with cdev %s failed:%d\n",
272 + tz->type, cdev->type, ret);
273 + return ret;
274 + }
275 +
276 + return 0;
277 +}
278 +
279 +static int imx_unbind(struct thermal_zone_device *tz,
280 + struct thermal_cooling_device *cdev)
281 +{
282 + int ret;
283 +
284 + ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
285 + if (ret) {
286 + dev_err(&tz->device,
287 + "unbinding zone %s with cdev %s failed:%d\n",
288 + tz->type, cdev->type, ret);
289 + return ret;
290 + }
291 +
292 + return 0;
293 +}
294 +
295 +static const struct thermal_zone_device_ops imx_tz_ops = {
296 + .bind = imx_bind,
297 + .unbind = imx_unbind,
298 + .get_temp = imx_get_temp,
299 + .get_mode = imx_get_mode,
300 + .set_mode = imx_set_mode,
301 + .get_trip_type = imx_get_trip_type,
302 + .get_trip_temp = imx_get_trip_temp,
303 + .get_crit_temp = imx_get_crit_temp,
304 +};
305 +
306 +static int imx_get_sensor_data(struct platform_device *pdev)
307 +{
308 + struct imx_thermal_data *data = platform_get_drvdata(pdev);
309 + struct regmap *map;
310 + int t1, t2, n1, n2;
311 + int ret;
312 + u32 val;
313 +
314 + map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
315 + "fsl,tempmon-data");
316 + if (IS_ERR(map)) {
317 + ret = PTR_ERR(map);
318 + dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
319 + return ret;
320 + }
321 +
322 + ret = regmap_read(map, OCOTP_ANA1, &val);
323 + if (ret) {
324 + dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
325 + return ret;
326 + }
327 +
328 + if (val == 0 || val == ~0) {
329 + dev_err(&pdev->dev, "invalid sensor calibration data\n");
330 + return -EINVAL;
331 + }
332 +
333 + /*
334 + * Sensor data layout:
335 + * [31:20] - sensor value @ 25C
336 + * [19:8] - sensor value of hot
337 + * [7:0] - hot temperature value
338 + */
339 + n1 = val >> 20;
340 + n2 = (val & 0xfff00) >> 8;
341 + t2 = val & 0xff;
342 + t1 = 25; /* t1 always 25C */
343 +
344 + /*
345 + * Derived from linear interpolation,
346 + * Tmeas = T2 + (Nmeas - N2) * (T1 - T2) / (N1 - N2)
347 + * We want to reduce this down to the minimum computation necessary
348 + * for each temperature read. Also, we want Tmeas in millicelsius
349 + * and we don't want to lose precision from integer division. So...
350 + * milli_Tmeas = 1000 * T2 + 1000 * (Nmeas - N2) * (T1 - T2) / (N1 - N2)
351 + * Let constant c1 = 1000 * (T1 - T2) / (N1 - N2)
352 + * milli_Tmeas = (1000 * T2) + c1 * (Nmeas - N2)
353 + * milli_Tmeas = (1000 * T2) + (c1 * Nmeas) - (c1 * N2)
354 + * Let constant c2 = (1000 * T2) - (c1 * N2)
355 + * milli_Tmeas = c2 + (c1 * Nmeas)
356 + */
357 + data->c1 = 1000 * (t1 - t2) / (n1 - n2);
358 + data->c2 = 1000 * t2 - data->c1 * n2;
359 +
360 + return 0;
361 +}
362 +
363 +static int imx_thermal_probe(struct platform_device *pdev)
364 +{
365 + struct imx_thermal_data *data;
366 + struct cpumask clip_cpus;
367 + struct regmap *map;
368 + int ret;
369 +
370 + data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
371 + if (!data)
372 + return -ENOMEM;
373 +
374 + map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
375 + if (IS_ERR(map)) {
376 + ret = PTR_ERR(map);
377 + dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
378 + return ret;
379 + }
380 + data->tempmon = map;
381 +
382 + platform_set_drvdata(pdev, data);
383 +
384 + ret = imx_get_sensor_data(pdev);
385 + if (ret) {
386 + dev_err(&pdev->dev, "failed to get sensor data\n");
387 + return ret;
388 + }
389 +
390 + /* Make sure sensor is in known good state for measurements */
391 + regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
392 + regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
393 + regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
394 + regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
395 + regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
396 +
397 + cpumask_set_cpu(0, &clip_cpus);
398 + data->cdev = cpufreq_cooling_register(&clip_cpus);
399 + if (IS_ERR(data->cdev)) {
400 + ret = PTR_ERR(data->cdev);
401 + dev_err(&pdev->dev,
402 + "failed to register cpufreq cooling device: %d\n", ret);
403 + return ret;
404 + }
405 +
406 + data->tz = thermal_zone_device_register("imx_thermal_zone",
407 + IMX_TRIP_NUM, 0, data,
408 + &imx_tz_ops, NULL,
409 + IMX_PASSIVE_DELAY,
410 + IMX_POLLING_DELAY);
411 + if (IS_ERR(data->tz)) {
412 + ret = PTR_ERR(data->tz);
413 + dev_err(&pdev->dev,
414 + "failed to register thermal zone device %d\n", ret);
415 + cpufreq_cooling_unregister(data->cdev);
416 + return ret;
417 + }
418 +
419 + data->mode = THERMAL_DEVICE_ENABLED;
420 +
421 + return 0;
422 +}
423 +
424 +static int imx_thermal_remove(struct platform_device *pdev)
425 +{
426 + struct imx_thermal_data *data = platform_get_drvdata(pdev);
427 +
428 + thermal_zone_device_unregister(data->tz);
429 + cpufreq_cooling_unregister(data->cdev);
430 +
431 + return 0;
432 +}
433 +
434 +#ifdef CONFIG_PM_SLEEP
435 +static int imx_thermal_suspend(struct device *dev)
436 +{
437 + struct imx_thermal_data *data = dev_get_drvdata(dev);
438 + struct regmap *map = data->tempmon;
439 + u32 val;
440 +
441 + regmap_read(map, TEMPSENSE0, &val);
442 + if ((val & TEMPSENSE0_POWER_DOWN) == 0) {
443 + /*
444 + * If a measurement is taking place, wait for a long enough
445 + * time for it to finish, and then check again. If it still
446 + * does not finish, something must go wrong.
447 + */
448 + udelay(50);
449 + regmap_read(map, TEMPSENSE0, &val);
450 + if ((val & TEMPSENSE0_POWER_DOWN) == 0)
451 + return -ETIMEDOUT;
452 + }
453 +
454 + return 0;
455 +}
456 +
457 +static int imx_thermal_resume(struct device *dev)
458 +{
459 + /* Nothing to do for now */
460 + return 0;
461 +}
462 +#endif
463 +
464 +static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
465 + imx_thermal_suspend, imx_thermal_resume);
466 +
467 +static const struct of_device_id of_imx_thermal_match[] = {
468 + { .compatible = "fsl,imx6q-tempmon", },
469 + { /* end */ }
470 +};
471 +
472 +static struct platform_driver imx_thermal = {
473 + .driver = {
474 + .name = "imx_thermal",
475 + .owner = THIS_MODULE,
476 + .pm = &imx_thermal_pm_ops,
477 + .of_match_table = of_imx_thermal_match,
478 + },
479 + .probe = imx_thermal_probe,
480 + .remove = imx_thermal_remove,
481 +};
482 +module_platform_driver(imx_thermal);
483 +
484 +MODULE_AUTHOR("Freescale Semiconductor, Inc.");
485 +MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
486 +MODULE_LICENSE("GPL v2");
487 +MODULE_ALIAS("platform:imx-thermal");
488 --
489 1.8.4
490