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