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