]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/thermal/imx_thermal.c
KARO: cleanup after merge of Freescale 3.10.17 stuff
[karo-tx-linux.git] / drivers / thermal / imx_thermal.c
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
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/device_cooling.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/kernel.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
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>
28
29 #define REG_SET         0x4
30 #define REG_CLR         0x8
31 #define REG_TOG         0xc
32
33 #define MISC0                           0x0150
34 #define MISC0_REFTOP_SELBIASOFF         (1 << 3)
35
36 #define TEMPSENSE0                      0x0180
37 #define TEMPSENSE0_ALARM_VALUE_SHIFT    20
38 #define TEMPSENSE0_ALARM_VALUE_MASK     (0xfff << TEMPSENSE0_ALARM_VALUE_SHIFT)
39 #define TEMPSENSE0_TEMP_CNT_SHIFT       8
40 #define TEMPSENSE0_TEMP_CNT_MASK        (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
41 #define TEMPSENSE0_FINISHED             (1 << 2)
42 #define TEMPSENSE0_MEASURE_TEMP         (1 << 1)
43 #define TEMPSENSE0_POWER_DOWN           (1 << 0)
44
45 #define TEMPSENSE1                      0x0190
46 #define TEMPSENSE1_MEASURE_FREQ         0xffff
47
48 #define OCOTP_ANA1                      0x04e0
49
50 /* The driver supports 1 passive trip point and 1 critical trip point */
51 enum imx_thermal_trip {
52         IMX_TRIP_PASSIVE,
53         IMX_TRIP_CRITICAL,
54         IMX_TRIP_NUM,
55 };
56
57 /*
58  * It defines the temperature in millicelsius for passive trip point
59  * that will trigger cooling action when crossed.
60  */
61 #define IMX_TEMP_PASSIVE                85000
62 #define IMX_TEMP_PASSIVE_COOL_DELTA     10000
63
64 /*
65  * The maximum die temperature on imx parts is 105C, let's give some cushion
66  * for noise and possible temperature rise between measurements.
67  */
68 #define IMX_TEMP_CRITICAL               100000
69
70 #define IMX_POLLING_DELAY               2000 /* millisecond */
71 #define IMX_PASSIVE_DELAY               1000
72
73 #define FACTOR0                         10000000
74 #define FACTOR1                         15976
75 #define FACTOR2                         4297157
76
77 struct imx_thermal_data {
78         struct thermal_zone_device *tz;
79         struct thermal_cooling_device *cdev[2];
80         enum thermal_device_mode mode;
81         struct regmap *tempmon;
82         u32 c1, c2; /* See formula in imx_get_sensor_data() */
83         unsigned long temp_passive;
84         unsigned long temp_critical;
85         unsigned long alarm_temp;
86         unsigned long last_temp;
87         bool irq_enabled;
88         int irq;
89         struct clk *thermal_clk;
90 };
91
92 static void imx_set_alarm_temp(struct imx_thermal_data *data,
93                                signed long alarm_temp)
94 {
95         struct regmap *map = data->tempmon;
96         int alarm_value;
97
98         data->alarm_temp = alarm_temp;
99         alarm_value = (data->c2 - alarm_temp) / data->c1;
100         regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_ALARM_VALUE_MASK);
101         regmap_write(map, TEMPSENSE0 + REG_SET, alarm_value <<
102                         TEMPSENSE0_ALARM_VALUE_SHIFT);
103 }
104
105 static int imx_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
106 {
107         struct imx_thermal_data *data = tz->devdata;
108         struct regmap *map = data->tempmon;
109         unsigned int n_meas;
110         bool wait;
111         u32 val;
112
113         if (data->mode == THERMAL_DEVICE_ENABLED) {
114                 /* Check if a measurement is currently in progress */
115                 regmap_read(map, TEMPSENSE0, &val);
116                 wait = !(val & TEMPSENSE0_FINISHED);
117         } else {
118                 /*
119                  * Every time we measure the temperature, we will power on the
120                  * temperature sensor, enable measurements, take a reading,
121                  * disable measurements, power off the temperature sensor.
122                  */
123                 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
124                 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
125
126                 wait = true;
127         }
128
129         /*
130          * According to the temp sensor designers, it may require up to ~17us
131          * to complete a measurement.
132          */
133         if (wait)
134                 usleep_range(20, 50);
135
136         regmap_read(map, TEMPSENSE0, &val);
137
138         if (data->mode != THERMAL_DEVICE_ENABLED) {
139                 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
140                 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
141         }
142
143         if ((val & TEMPSENSE0_FINISHED) == 0) {
144                 dev_dbg(&tz->device, "temp measurement never finished\n");
145                 return -EAGAIN;
146         }
147
148         n_meas = (val & TEMPSENSE0_TEMP_CNT_MASK) >> TEMPSENSE0_TEMP_CNT_SHIFT;
149
150         /* See imx_get_sensor_data() for formula derivation */
151         *temp = data->c2 - n_meas * data->c1;
152
153         /* Update alarm value to next higher trip point */
154         if (data->alarm_temp == data->temp_passive && *temp >= data->temp_passive)
155                 imx_set_alarm_temp(data, data->temp_critical);
156         if (data->alarm_temp == data->temp_critical && *temp < data->temp_passive) {
157                 imx_set_alarm_temp(data, data->temp_passive);
158                 dev_dbg(&tz->device, "thermal alarm off: T < %lu\n",
159                         data->alarm_temp / 1000);
160         }
161
162         if (*temp != data->last_temp) {
163                 dev_dbg(&tz->device, "millicelsius: %ld\n", *temp);
164                 data->last_temp = *temp;
165         }
166
167         /* Reenable alarm IRQ if temperature below alarm temperature */
168         if (!data->irq_enabled && *temp < data->alarm_temp) {
169                 data->irq_enabled = true;
170                 enable_irq(data->irq);
171         }
172
173         return 0;
174 }
175
176 static int imx_get_mode(struct thermal_zone_device *tz,
177                         enum thermal_device_mode *mode)
178 {
179         struct imx_thermal_data *data = tz->devdata;
180
181         *mode = data->mode;
182
183         return 0;
184 }
185
186 static int imx_set_mode(struct thermal_zone_device *tz,
187                         enum thermal_device_mode mode)
188 {
189         struct imx_thermal_data *data = tz->devdata;
190         struct regmap *map = data->tempmon;
191
192         if (mode == THERMAL_DEVICE_ENABLED) {
193                 tz->polling_delay = IMX_POLLING_DELAY;
194                 tz->passive_delay = IMX_PASSIVE_DELAY;
195
196                 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
197                 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
198
199                 if (!data->irq_enabled) {
200                         data->irq_enabled = true;
201                         enable_irq(data->irq);
202                 }
203         } else {
204                 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
205                 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
206
207                 tz->polling_delay = 0;
208                 tz->passive_delay = 0;
209
210                 if (data->irq_enabled) {
211                         disable_irq(data->irq);
212                         data->irq_enabled = false;
213                 }
214         }
215
216         data->mode = mode;
217         thermal_zone_device_update(tz);
218
219         return 0;
220 }
221
222 static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
223                              enum thermal_trip_type *type)
224 {
225         *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
226                                              THERMAL_TRIP_CRITICAL;
227         return 0;
228 }
229
230 static int imx_get_crit_temp(struct thermal_zone_device *tz,
231                              unsigned long *temp)
232 {
233         struct imx_thermal_data *data = tz->devdata;
234
235         *temp = data->temp_critical;
236         return 0;
237 }
238
239 static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
240                              unsigned long *temp)
241 {
242         struct imx_thermal_data *data = tz->devdata;
243
244         *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
245                                              data->temp_critical;
246         return 0;
247 }
248
249 static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
250                              unsigned long temp)
251 {
252         struct imx_thermal_data *data = tz->devdata;
253
254         if (trip == IMX_TRIP_CRITICAL)
255                 return -EPERM;
256
257         if (temp > IMX_TEMP_PASSIVE)
258                 return -EINVAL;
259
260         data->temp_passive = temp;
261
262         imx_set_alarm_temp(data, temp);
263
264         return 0;
265 }
266
267 static int imx_bind(struct thermal_zone_device *tz,
268                     struct thermal_cooling_device *cdev)
269 {
270         int ret;
271
272         ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
273                                                THERMAL_NO_LIMIT,
274                                                THERMAL_NO_LIMIT);
275         if (ret) {
276                 dev_err(&tz->device,
277                         "binding 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 int imx_unbind(struct thermal_zone_device *tz,
286                       struct thermal_cooling_device *cdev)
287 {
288         int ret;
289
290         ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
291         if (ret) {
292                 dev_err(&tz->device,
293                         "unbinding zone %s with cdev %s failed:%d\n",
294                         tz->type, cdev->type, ret);
295                 return ret;
296         }
297
298         return 0;
299 }
300
301 static int imx_get_trend(struct thermal_zone_device *tz,
302                 int trip, enum thermal_trend *trend)
303 {
304         int ret;
305         unsigned long trip_temp;
306
307         ret = imx_get_trip_temp(tz, trip, &trip_temp);
308         if (ret < 0)
309                 return ret;
310
311         if (tz->temperature >= (trip_temp - IMX_TEMP_PASSIVE_COOL_DELTA))
312                 *trend = THERMAL_TREND_RAISE_FULL;
313         else
314                 *trend = THERMAL_TREND_DROP_FULL;
315
316         return 0;
317 }
318
319 static struct thermal_zone_device_ops imx_tz_ops = {
320         .bind = imx_bind,
321         .unbind = imx_unbind,
322         .get_temp = imx_get_temp,
323         .get_mode = imx_get_mode,
324         .set_mode = imx_set_mode,
325         .get_trip_type = imx_get_trip_type,
326         .get_trip_temp = imx_get_trip_temp,
327         .get_crit_temp = imx_get_crit_temp,
328         .get_trend = imx_get_trend,
329         .set_trip_temp = imx_set_trip_temp,
330 };
331
332 static int imx_get_sensor_data(struct platform_device *pdev)
333 {
334         struct imx_thermal_data *data = platform_get_drvdata(pdev);
335         struct regmap *map;
336         int t1, t2, n1, n2;
337         int ret;
338         u32 val;
339         u64 temp64;
340
341         map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
342                                               "fsl,tempmon-data");
343         if (IS_ERR(map)) {
344                 ret = PTR_ERR(map);
345                 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
346                 return ret;
347         }
348
349         ret = regmap_read(map, OCOTP_ANA1, &val);
350         if (ret) {
351                 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
352                 return ret;
353         }
354
355         if (val == 0 || val == ~0) {
356                 dev_err(&pdev->dev, "invalid sensor calibration data\n");
357                 return -EINVAL;
358         }
359
360         /*
361          * Sensor data layout:
362          *   [31:20] - sensor value @ 25C
363          *    [19:8] - sensor value of hot
364          *     [7:0] - hot temperature value
365          * Use universal formula now and only need sensor value @ 25C
366          * slope = 0.4297157 - (0.0015976 * 25C fuse)
367          */
368         n1 = val >> 20;
369         n2 = (val & 0xfff00) >> 8;
370         t2 = val & 0xff;
371         t1 = 25; /* t1 always 25C */
372
373         /*
374          * Derived from linear interpolation:
375          * slope = 0.4297157 - (0.0015976 * 25C fuse)
376          * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
377          * (Nmeas - n1) / (Tmeas - t1) = slope
378          * We want to reduce this down to the minimum computation necessary
379          * for each temperature read.  Also, we want Tmeas in millicelsius
380          * and we don't want to lose precision from integer division. So...
381          * Tmeas = (Nmeas - n1) / slope + t1
382          * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
383          * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
384          * Let constant c1 = (-1000 / slope)
385          * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
386          * Let constant c2 = n1 *c1 + 1000 * t1
387          * milli_Tmeas = c2 - Nmeas * c1
388          */
389         temp64 = FACTOR0;
390         temp64 *= 1000;
391         do_div(temp64, FACTOR1 * n1 - FACTOR2);
392         data->c1 = temp64;
393         data->c2 = n1 * data->c1 + 1000 * t1;
394
395         /*
396          * Set the default passive cooling trip point to 20 Â°C below the
397          * maximum die temperature. Can be changed from userspace.
398          */
399         data->temp_passive = 1000 * (t2 - 20);
400
401         /*
402          * The maximum die temperature is t2, let's give 5 Â°C cushion
403          * for noise and possible temperature rise between measurements.
404          */
405         data->temp_critical = 1000 * (t2 - 5);
406
407         return 0;
408 }
409
410 static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
411 {
412         struct imx_thermal_data *data = dev;
413
414         disable_irq_nosync(irq);
415         data->irq_enabled = false;
416
417         return IRQ_WAKE_THREAD;
418 }
419
420 static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
421 {
422         struct imx_thermal_data *data = dev;
423
424         dev_dbg(&data->tz->device, "THERMAL ALARM: T > %lu\n",
425                 data->alarm_temp / 1000);
426
427         thermal_zone_device_update(data->tz);
428
429         return IRQ_HANDLED;
430 }
431
432 static int imx_thermal_probe(struct platform_device *pdev)
433 {
434         struct imx_thermal_data *data;
435         struct cpumask clip_cpus;
436         struct regmap *map;
437         int measure_freq;
438         int ret;
439
440         data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
441         if (!data)
442                 return -ENOMEM;
443
444         map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
445         if (IS_ERR(map)) {
446                 ret = PTR_ERR(map);
447                 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
448                 return ret;
449         }
450         data->tempmon = map;
451
452         data->irq = platform_get_irq(pdev, 0);
453         if (data->irq < 0)
454                 return data->irq;
455
456         ret = devm_request_threaded_irq(&pdev->dev, data->irq,
457                         imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
458                         0, "imx_thermal", data);
459         if (ret < 0) {
460                 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
461                 return ret;
462         }
463
464         platform_set_drvdata(pdev, data);
465
466         ret = imx_get_sensor_data(pdev);
467         if (ret) {
468                 dev_err(&pdev->dev, "failed to get sensor data\n");
469                 return ret;
470         }
471
472         /* Make sure sensor is in known good state for measurements */
473         regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
474         regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
475         regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
476         regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
477         regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
478
479         cpumask_set_cpu(0, &clip_cpus);
480         data->cdev[0] = cpufreq_cooling_register(&clip_cpus);
481         if (IS_ERR(data->cdev[0])) {
482                 ret = PTR_ERR(data->cdev[0]);
483                 dev_err(&pdev->dev,
484                         "failed to register cpufreq cooling device: %d\n", ret);
485                 return ret;
486         }
487
488         data->cdev[1] = devfreq_cooling_register();
489         if (IS_ERR(data->cdev[1])) {
490                 ret = PTR_ERR(data->cdev[1]);
491                 dev_err(&pdev->dev,
492                         "failed to register devfreq cooling device: %d\n", ret);
493                 return ret;
494         }
495
496         data->temp_passive = IMX_TEMP_PASSIVE;
497         data->temp_critical = IMX_TEMP_CRITICAL;
498         data->tz = thermal_zone_device_register("imx_thermal_zone",
499                                                 IMX_TRIP_NUM,
500                                                 BIT(IMX_TRIP_PASSIVE), data,
501                                                 &imx_tz_ops, NULL,
502                                                 IMX_PASSIVE_DELAY,
503                                                 IMX_POLLING_DELAY);
504         if (IS_ERR(data->tz)) {
505                 ret = PTR_ERR(data->tz);
506                 dev_err(&pdev->dev,
507                         "failed to register thermal zone device %d\n", ret);
508                 cpufreq_cooling_unregister(data->cdev[0]);
509                 devfreq_cooling_unregister(data->cdev[1]);
510                 return ret;
511         }
512
513         data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
514         if (IS_ERR(data->thermal_clk)) {
515                 dev_warn(&pdev->dev, "failed to get thermal clk!\n");
516         } else {
517                 /*
518                  * Thermal sensor needs clk on to get correct value, normally
519                  * we should enable its clk before taking measurement and disable
520                  * clk after measurement is done, but if alarm function is enabled,
521                  * hardware will auto measure the temperature periodically, so we
522                  * need to keep the clk always on for alarm function.
523                  */
524                 ret = clk_prepare_enable(data->thermal_clk);
525                 if (ret)
526                         dev_warn(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
527         }
528
529         /* Enable measurements at ~ 10 Hz */
530         regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
531         measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
532         regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
533         imx_set_alarm_temp(data, data->temp_passive);
534         regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
535         regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
536
537         data->irq_enabled = true;
538         data->mode = THERMAL_DEVICE_ENABLED;
539
540         return 0;
541 }
542
543 static int imx_thermal_remove(struct platform_device *pdev)
544 {
545         struct imx_thermal_data *data = platform_get_drvdata(pdev);
546         struct regmap *map = data->tempmon;
547
548         /* Disable measurements */
549         regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
550         if (!IS_ERR(data->thermal_clk))
551                 clk_disable_unprepare(data->thermal_clk);
552
553         thermal_zone_device_unregister(data->tz);
554         cpufreq_cooling_unregister(data->cdev[0]);
555         devfreq_cooling_unregister(data->cdev[1]);
556
557         return 0;
558 }
559
560 #ifdef CONFIG_PM_SLEEP
561 static int imx_thermal_suspend(struct device *dev)
562 {
563         struct imx_thermal_data *data = dev_get_drvdata(dev);
564         struct regmap *map = data->tempmon;
565
566         /*
567          * Need to disable thermal sensor, otherwise, when thermal core
568          * try to get temperature before thermal sensor resume, a wrong
569          * temperature will be read as the thermal sensor is powered
570          * down.
571          */
572         regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
573         regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
574         data->mode = THERMAL_DEVICE_DISABLED;
575
576         return 0;
577 }
578
579 static int imx_thermal_resume(struct device *dev)
580 {
581         struct imx_thermal_data *data = dev_get_drvdata(dev);
582         struct regmap *map = data->tempmon;
583
584         /* Enabled thermal sensor after resume */
585         regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
586         regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
587         data->mode = THERMAL_DEVICE_ENABLED;
588
589         return 0;
590 }
591 #endif
592
593 static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
594                          imx_thermal_suspend, imx_thermal_resume);
595
596 static const struct of_device_id of_imx_thermal_match[] = {
597         { .compatible = "fsl,imx6q-tempmon", },
598         { /* end */ }
599 };
600 MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
601
602 static struct platform_driver imx_thermal = {
603         .driver = {
604                 .name   = "imx_thermal",
605                 .owner  = THIS_MODULE,
606                 .pm     = &imx_thermal_pm_ops,
607                 .of_match_table = of_imx_thermal_match,
608         },
609         .probe          = imx_thermal_probe,
610         .remove         = imx_thermal_remove,
611 };
612 module_platform_driver(imx_thermal);
613
614 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
615 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
616 MODULE_LICENSE("GPL v2");
617 MODULE_ALIAS("platform:imx-thermal");