blob: aa452acb60b6c93f2ca0d8ace93aa7c4cc883d32 [file] [log] [blame]
Fabio Estevam45f8b0d2018-05-23 16:24:40 -03001// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright 2013 Freescale Semiconductor, Inc.
Shawn Guoca3de462013-06-24 14:30:44 +08004
Anson Huang329fe7b2013-12-23 15:49:22 -05005#include <linux/clk.h>
Bastian Stendera1d00152018-06-08 11:06:39 +02006#include <linux/cpu.h>
Viresh Kumar4d753aa2017-04-25 15:57:14 +05307#include <linux/cpufreq.h>
Shawn Guoca3de462013-06-24 14:30:44 +08008#include <linux/cpu_cooling.h>
Shawn Guoca3de462013-06-24 14:30:44 +08009#include <linux/delay.h>
10#include <linux/device.h>
11#include <linux/init.h>
Philipp Zabel37713a12013-08-01 18:33:12 +020012#include <linux/interrupt.h>
Shawn Guoca3de462013-06-24 14:30:44 +080013#include <linux/io.h>
14#include <linux/kernel.h>
15#include <linux/mfd/syscon.h>
16#include <linux/module.h>
17#include <linux/of.h>
Anson Huang3c94f172014-08-06 15:12:09 +080018#include <linux/of_device.h>
Shawn Guoca3de462013-06-24 14:30:44 +080019#include <linux/platform_device.h>
20#include <linux/regmap.h>
21#include <linux/slab.h>
22#include <linux/thermal.h>
23#include <linux/types.h>
Leonard Crestezae621552017-07-14 17:11:08 +030024#include <linux/nvmem-consumer.h>
Shawn Guoca3de462013-06-24 14:30:44 +080025
26#define REG_SET 0x4
27#define REG_CLR 0x8
28#define REG_TOG 0xc
29
Anson Huangf085f672018-03-02 09:59:30 +080030/* i.MX6 specific */
31#define IMX6_MISC0 0x0150
32#define IMX6_MISC0_REFTOP_SELBIASOFF (1 << 3)
33#define IMX6_MISC1 0x0160
34#define IMX6_MISC1_IRQ_TEMPHIGH (1 << 29)
Anson Huang3c94f172014-08-06 15:12:09 +080035/* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
Anson Huangf085f672018-03-02 09:59:30 +080036#define IMX6_MISC1_IRQ_TEMPLOW (1 << 28)
37#define IMX6_MISC1_IRQ_TEMPPANIC (1 << 27)
Shawn Guoca3de462013-06-24 14:30:44 +080038
Anson Huangf085f672018-03-02 09:59:30 +080039#define IMX6_TEMPSENSE0 0x0180
40#define IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT 20
41#define IMX6_TEMPSENSE0_ALARM_VALUE_MASK (0xfff << 20)
42#define IMX6_TEMPSENSE0_TEMP_CNT_SHIFT 8
43#define IMX6_TEMPSENSE0_TEMP_CNT_MASK (0xfff << 8)
44#define IMX6_TEMPSENSE0_FINISHED (1 << 2)
45#define IMX6_TEMPSENSE0_MEASURE_TEMP (1 << 1)
46#define IMX6_TEMPSENSE0_POWER_DOWN (1 << 0)
Shawn Guoca3de462013-06-24 14:30:44 +080047
Anson Huangf085f672018-03-02 09:59:30 +080048#define IMX6_TEMPSENSE1 0x0190
49#define IMX6_TEMPSENSE1_MEASURE_FREQ 0xffff
50#define IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT 0
Shawn Guoca3de462013-06-24 14:30:44 +080051
Tim Harveya2291ba2015-11-19 06:49:40 -080052#define OCOTP_MEM0 0x0480
Shawn Guoca3de462013-06-24 14:30:44 +080053#define OCOTP_ANA1 0x04e0
54
Anson Huangf085f672018-03-02 09:59:30 +080055/* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
56#define IMX6_TEMPSENSE2 0x0290
57#define IMX6_TEMPSENSE2_LOW_VALUE_SHIFT 0
58#define IMX6_TEMPSENSE2_LOW_VALUE_MASK 0xfff
59#define IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT 16
60#define IMX6_TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
61
62/* i.MX7 specific */
63#define IMX7_ANADIG_DIGPROG 0x800
64#define IMX7_TEMPSENSE0 0x300
65#define IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT 18
66#define IMX7_TEMPSENSE0_PANIC_ALARM_MASK (0x1ff << 18)
67#define IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT 9
68#define IMX7_TEMPSENSE0_HIGH_ALARM_MASK (0x1ff << 9)
69#define IMX7_TEMPSENSE0_LOW_ALARM_SHIFT 0
70#define IMX7_TEMPSENSE0_LOW_ALARM_MASK 0x1ff
71
72#define IMX7_TEMPSENSE1 0x310
73#define IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT 16
74#define IMX7_TEMPSENSE1_MEASURE_FREQ_MASK (0xffff << 16)
75#define IMX7_TEMPSENSE1_FINISHED (1 << 11)
76#define IMX7_TEMPSENSE1_MEASURE_TEMP (1 << 10)
77#define IMX7_TEMPSENSE1_POWER_DOWN (1 << 9)
78#define IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT 0
79#define IMX7_TEMPSENSE1_TEMP_VALUE_MASK 0x1ff
80
Shawn Guoca3de462013-06-24 14:30:44 +080081/* The driver supports 1 passive trip point and 1 critical trip point */
82enum imx_thermal_trip {
83 IMX_TRIP_PASSIVE,
84 IMX_TRIP_CRITICAL,
85 IMX_TRIP_NUM,
86};
87
Shawn Guoca3de462013-06-24 14:30:44 +080088#define IMX_POLLING_DELAY 2000 /* millisecond */
89#define IMX_PASSIVE_DELAY 1000
90
Anson Huang3c94f172014-08-06 15:12:09 +080091#define TEMPMON_IMX6Q 1
92#define TEMPMON_IMX6SX 2
Anson Huangf085f672018-03-02 09:59:30 +080093#define TEMPMON_IMX7D 3
Anson Huang3c94f172014-08-06 15:12:09 +080094
95struct thermal_soc_data {
96 u32 version;
Anson Huangf085f672018-03-02 09:59:30 +080097
98 u32 sensor_ctrl;
99 u32 power_down_mask;
100 u32 measure_temp_mask;
101
102 u32 measure_freq_ctrl;
103 u32 measure_freq_mask;
104 u32 measure_freq_shift;
105
106 u32 temp_data;
107 u32 temp_value_mask;
108 u32 temp_value_shift;
109 u32 temp_valid_mask;
110
111 u32 panic_alarm_ctrl;
112 u32 panic_alarm_mask;
113 u32 panic_alarm_shift;
114
115 u32 high_alarm_ctrl;
116 u32 high_alarm_mask;
117 u32 high_alarm_shift;
118
119 u32 low_alarm_ctrl;
120 u32 low_alarm_mask;
121 u32 low_alarm_shift;
Anson Huang3c94f172014-08-06 15:12:09 +0800122};
123
124static struct thermal_soc_data thermal_imx6q_data = {
125 .version = TEMPMON_IMX6Q,
Anson Huangf085f672018-03-02 09:59:30 +0800126
127 .sensor_ctrl = IMX6_TEMPSENSE0,
128 .power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
129 .measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
130
131 .measure_freq_ctrl = IMX6_TEMPSENSE1,
132 .measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
133 .measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
134
135 .temp_data = IMX6_TEMPSENSE0,
136 .temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
137 .temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
138 .temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
139
140 .high_alarm_ctrl = IMX6_TEMPSENSE0,
141 .high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
142 .high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
Anson Huang3c94f172014-08-06 15:12:09 +0800143};
144
145static struct thermal_soc_data thermal_imx6sx_data = {
146 .version = TEMPMON_IMX6SX,
Anson Huangf085f672018-03-02 09:59:30 +0800147
148 .sensor_ctrl = IMX6_TEMPSENSE0,
149 .power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
150 .measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
151
152 .measure_freq_ctrl = IMX6_TEMPSENSE1,
153 .measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
154 .measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
155
156 .temp_data = IMX6_TEMPSENSE0,
157 .temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
158 .temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
159 .temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
160
161 .high_alarm_ctrl = IMX6_TEMPSENSE0,
162 .high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
163 .high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
164
165 .panic_alarm_ctrl = IMX6_TEMPSENSE2,
166 .panic_alarm_mask = IMX6_TEMPSENSE2_PANIC_VALUE_MASK,
167 .panic_alarm_shift = IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT,
168
169 .low_alarm_ctrl = IMX6_TEMPSENSE2,
170 .low_alarm_mask = IMX6_TEMPSENSE2_LOW_VALUE_MASK,
171 .low_alarm_shift = IMX6_TEMPSENSE2_LOW_VALUE_SHIFT,
172};
173
174static struct thermal_soc_data thermal_imx7d_data = {
175 .version = TEMPMON_IMX7D,
176
177 .sensor_ctrl = IMX7_TEMPSENSE1,
178 .power_down_mask = IMX7_TEMPSENSE1_POWER_DOWN,
179 .measure_temp_mask = IMX7_TEMPSENSE1_MEASURE_TEMP,
180
181 .measure_freq_ctrl = IMX7_TEMPSENSE1,
182 .measure_freq_shift = IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT,
183 .measure_freq_mask = IMX7_TEMPSENSE1_MEASURE_FREQ_MASK,
184
185 .temp_data = IMX7_TEMPSENSE1,
186 .temp_value_mask = IMX7_TEMPSENSE1_TEMP_VALUE_MASK,
187 .temp_value_shift = IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT,
188 .temp_valid_mask = IMX7_TEMPSENSE1_FINISHED,
189
190 .panic_alarm_ctrl = IMX7_TEMPSENSE1,
191 .panic_alarm_mask = IMX7_TEMPSENSE0_PANIC_ALARM_MASK,
192 .panic_alarm_shift = IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT,
193
194 .high_alarm_ctrl = IMX7_TEMPSENSE0,
195 .high_alarm_mask = IMX7_TEMPSENSE0_HIGH_ALARM_MASK,
196 .high_alarm_shift = IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT,
197
198 .low_alarm_ctrl = IMX7_TEMPSENSE0,
199 .low_alarm_mask = IMX7_TEMPSENSE0_LOW_ALARM_MASK,
200 .low_alarm_shift = IMX7_TEMPSENSE0_LOW_ALARM_SHIFT,
Anson Huang3c94f172014-08-06 15:12:09 +0800201};
202
Shawn Guoca3de462013-06-24 14:30:44 +0800203struct imx_thermal_data {
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530204 struct cpufreq_policy *policy;
Shawn Guoca3de462013-06-24 14:30:44 +0800205 struct thermal_zone_device *tz;
206 struct thermal_cooling_device *cdev;
207 enum thermal_device_mode mode;
208 struct regmap *tempmon;
Leonard Crestezae621552017-07-14 17:11:08 +0300209 u32 c1, c2; /* See formula in imx_init_calib() */
Sascha Hauer17e83512015-07-24 08:12:54 +0200210 int temp_passive;
211 int temp_critical;
Tim Harveya2291ba2015-11-19 06:49:40 -0800212 int temp_max;
Sascha Hauer17e83512015-07-24 08:12:54 +0200213 int alarm_temp;
214 int last_temp;
Philipp Zabel37713a12013-08-01 18:33:12 +0200215 bool irq_enabled;
216 int irq;
Anson Huang329fe7b2013-12-23 15:49:22 -0500217 struct clk *thermal_clk;
Anson Huang3c94f172014-08-06 15:12:09 +0800218 const struct thermal_soc_data *socdata;
Tim Harveya2291ba2015-11-19 06:49:40 -0800219 const char *temp_grade;
Shawn Guoca3de462013-06-24 14:30:44 +0800220};
221
Anson Huang3c94f172014-08-06 15:12:09 +0800222static void imx_set_panic_temp(struct imx_thermal_data *data,
Sascha Hauer17e83512015-07-24 08:12:54 +0200223 int panic_temp)
Anson Huang3c94f172014-08-06 15:12:09 +0800224{
Anson Huangf085f672018-03-02 09:59:30 +0800225 const struct thermal_soc_data *soc_data = data->socdata;
Anson Huang3c94f172014-08-06 15:12:09 +0800226 struct regmap *map = data->tempmon;
227 int critical_value;
228
229 critical_value = (data->c2 - panic_temp) / data->c1;
Anson Huangf085f672018-03-02 09:59:30 +0800230
231 regmap_write(map, soc_data->panic_alarm_ctrl + REG_CLR,
232 soc_data->panic_alarm_mask);
233 regmap_write(map, soc_data->panic_alarm_ctrl + REG_SET,
234 critical_value << soc_data->panic_alarm_shift);
Anson Huang3c94f172014-08-06 15:12:09 +0800235}
236
Philipp Zabel37713a12013-08-01 18:33:12 +0200237static void imx_set_alarm_temp(struct imx_thermal_data *data,
Sascha Hauer17e83512015-07-24 08:12:54 +0200238 int alarm_temp)
Philipp Zabel37713a12013-08-01 18:33:12 +0200239{
240 struct regmap *map = data->tempmon;
Anson Huangf085f672018-03-02 09:59:30 +0800241 const struct thermal_soc_data *soc_data = data->socdata;
Philipp Zabel37713a12013-08-01 18:33:12 +0200242 int alarm_value;
243
244 data->alarm_temp = alarm_temp;
Anson Huangf085f672018-03-02 09:59:30 +0800245
246 if (data->socdata->version == TEMPMON_IMX7D)
247 alarm_value = alarm_temp / 1000 + data->c1 - 25;
248 else
249 alarm_value = (data->c2 - alarm_temp) / data->c1;
250
251 regmap_write(map, soc_data->high_alarm_ctrl + REG_CLR,
252 soc_data->high_alarm_mask);
253 regmap_write(map, soc_data->high_alarm_ctrl + REG_SET,
254 alarm_value << soc_data->high_alarm_shift);
Philipp Zabel37713a12013-08-01 18:33:12 +0200255}
256
Sascha Hauer17e83512015-07-24 08:12:54 +0200257static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800258{
259 struct imx_thermal_data *data = tz->devdata;
Anson Huangf085f672018-03-02 09:59:30 +0800260 const struct thermal_soc_data *soc_data = data->socdata;
Shawn Guoca3de462013-06-24 14:30:44 +0800261 struct regmap *map = data->tempmon;
Shawn Guoca3de462013-06-24 14:30:44 +0800262 unsigned int n_meas;
Philipp Zabel37713a12013-08-01 18:33:12 +0200263 bool wait;
Shawn Guoca3de462013-06-24 14:30:44 +0800264 u32 val;
265
Philipp Zabel37713a12013-08-01 18:33:12 +0200266 if (data->mode == THERMAL_DEVICE_ENABLED) {
267 /* Check if a measurement is currently in progress */
Anson Huangf085f672018-03-02 09:59:30 +0800268 regmap_read(map, soc_data->temp_data, &val);
269 wait = !(val & soc_data->temp_valid_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200270 } else {
271 /*
272 * Every time we measure the temperature, we will power on the
273 * temperature sensor, enable measurements, take a reading,
274 * disable measurements, power off the temperature sensor.
275 */
Anson Huangf085f672018-03-02 09:59:30 +0800276 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
277 soc_data->power_down_mask);
278 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
279 soc_data->measure_temp_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200280
281 wait = true;
282 }
Shawn Guoca3de462013-06-24 14:30:44 +0800283
284 /*
285 * According to the temp sensor designers, it may require up to ~17us
286 * to complete a measurement.
287 */
Philipp Zabel37713a12013-08-01 18:33:12 +0200288 if (wait)
289 usleep_range(20, 50);
Shawn Guoca3de462013-06-24 14:30:44 +0800290
Anson Huangf085f672018-03-02 09:59:30 +0800291 regmap_read(map, soc_data->temp_data, &val);
Philipp Zabel37713a12013-08-01 18:33:12 +0200292
293 if (data->mode != THERMAL_DEVICE_ENABLED) {
Anson Huangf085f672018-03-02 09:59:30 +0800294 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
295 soc_data->measure_temp_mask);
296 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
297 soc_data->power_down_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200298 }
Shawn Guoca3de462013-06-24 14:30:44 +0800299
Anson Huangf085f672018-03-02 09:59:30 +0800300 if ((val & soc_data->temp_valid_mask) == 0) {
Shawn Guoca3de462013-06-24 14:30:44 +0800301 dev_dbg(&tz->device, "temp measurement never finished\n");
302 return -EAGAIN;
303 }
304
Anson Huangf085f672018-03-02 09:59:30 +0800305 n_meas = (val & soc_data->temp_value_mask)
306 >> soc_data->temp_value_shift;
Shawn Guoca3de462013-06-24 14:30:44 +0800307
Leonard Crestezae621552017-07-14 17:11:08 +0300308 /* See imx_init_calib() for formula derivation */
Anson Huangf085f672018-03-02 09:59:30 +0800309 if (data->socdata->version == TEMPMON_IMX7D)
310 *temp = (n_meas - data->c1 + 25) * 1000;
311 else
312 *temp = data->c2 - n_meas * data->c1;
Shawn Guoca3de462013-06-24 14:30:44 +0800313
Anson Huang3c94f172014-08-06 15:12:09 +0800314 /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
315 if (data->socdata->version == TEMPMON_IMX6Q) {
316 if (data->alarm_temp == data->temp_passive &&
317 *temp >= data->temp_passive)
318 imx_set_alarm_temp(data, data->temp_critical);
319 if (data->alarm_temp == data->temp_critical &&
320 *temp < data->temp_passive) {
321 imx_set_alarm_temp(data, data->temp_passive);
Sascha Hauer17e83512015-07-24 08:12:54 +0200322 dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
Anson Huang3c94f172014-08-06 15:12:09 +0800323 data->alarm_temp / 1000);
324 }
Philipp Zabel37713a12013-08-01 18:33:12 +0200325 }
326
327 if (*temp != data->last_temp) {
Sascha Hauer17e83512015-07-24 08:12:54 +0200328 dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
Philipp Zabel37713a12013-08-01 18:33:12 +0200329 data->last_temp = *temp;
330 }
331
332 /* Reenable alarm IRQ if temperature below alarm temperature */
333 if (!data->irq_enabled && *temp < data->alarm_temp) {
334 data->irq_enabled = true;
335 enable_irq(data->irq);
Shawn Guoca3de462013-06-24 14:30:44 +0800336 }
337
338 return 0;
339}
340
341static int imx_get_mode(struct thermal_zone_device *tz,
342 enum thermal_device_mode *mode)
343{
344 struct imx_thermal_data *data = tz->devdata;
345
346 *mode = data->mode;
347
348 return 0;
349}
350
351static int imx_set_mode(struct thermal_zone_device *tz,
352 enum thermal_device_mode mode)
353{
354 struct imx_thermal_data *data = tz->devdata;
Philipp Zabel37713a12013-08-01 18:33:12 +0200355 struct regmap *map = data->tempmon;
Anson Huangf085f672018-03-02 09:59:30 +0800356 const struct thermal_soc_data *soc_data = data->socdata;
Shawn Guoca3de462013-06-24 14:30:44 +0800357
358 if (mode == THERMAL_DEVICE_ENABLED) {
359 tz->polling_delay = IMX_POLLING_DELAY;
360 tz->passive_delay = IMX_PASSIVE_DELAY;
Philipp Zabel37713a12013-08-01 18:33:12 +0200361
Anson Huangf085f672018-03-02 09:59:30 +0800362 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
363 soc_data->power_down_mask);
364 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
365 soc_data->measure_temp_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200366
367 if (!data->irq_enabled) {
368 data->irq_enabled = true;
369 enable_irq(data->irq);
370 }
Shawn Guoca3de462013-06-24 14:30:44 +0800371 } else {
Anson Huangf085f672018-03-02 09:59:30 +0800372 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
373 soc_data->measure_temp_mask);
374 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
375 soc_data->power_down_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200376
Shawn Guoca3de462013-06-24 14:30:44 +0800377 tz->polling_delay = 0;
378 tz->passive_delay = 0;
Philipp Zabel37713a12013-08-01 18:33:12 +0200379
380 if (data->irq_enabled) {
381 disable_irq(data->irq);
382 data->irq_enabled = false;
383 }
Shawn Guoca3de462013-06-24 14:30:44 +0800384 }
385
386 data->mode = mode;
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700387 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Shawn Guoca3de462013-06-24 14:30:44 +0800388
389 return 0;
390}
391
392static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
393 enum thermal_trip_type *type)
394{
395 *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
396 THERMAL_TRIP_CRITICAL;
397 return 0;
398}
399
Sascha Hauer17e83512015-07-24 08:12:54 +0200400static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800401{
Philipp Zabel017e5142013-08-01 18:33:11 +0200402 struct imx_thermal_data *data = tz->devdata;
403
404 *temp = data->temp_critical;
Shawn Guoca3de462013-06-24 14:30:44 +0800405 return 0;
406}
407
408static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200409 int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800410{
Philipp Zabel017e5142013-08-01 18:33:11 +0200411 struct imx_thermal_data *data = tz->devdata;
412
413 *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
414 data->temp_critical;
415 return 0;
416}
417
418static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200419 int temp)
Philipp Zabel017e5142013-08-01 18:33:11 +0200420{
421 struct imx_thermal_data *data = tz->devdata;
422
Tim Harveya2291ba2015-11-19 06:49:40 -0800423 /* do not allow changing critical threshold */
Philipp Zabel017e5142013-08-01 18:33:11 +0200424 if (trip == IMX_TRIP_CRITICAL)
425 return -EPERM;
426
Tim Harveya2291ba2015-11-19 06:49:40 -0800427 /* do not allow passive to be set higher than critical */
428 if (temp < 0 || temp > data->temp_critical)
Philipp Zabel017e5142013-08-01 18:33:11 +0200429 return -EINVAL;
430
431 data->temp_passive = temp;
432
Philipp Zabel37713a12013-08-01 18:33:12 +0200433 imx_set_alarm_temp(data, temp);
434
Shawn Guoca3de462013-06-24 14:30:44 +0800435 return 0;
436}
437
438static int imx_bind(struct thermal_zone_device *tz,
439 struct thermal_cooling_device *cdev)
440{
441 int ret;
442
443 ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
444 THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000445 THERMAL_NO_LIMIT,
446 THERMAL_WEIGHT_DEFAULT);
Shawn Guoca3de462013-06-24 14:30:44 +0800447 if (ret) {
448 dev_err(&tz->device,
449 "binding zone %s with cdev %s failed:%d\n",
450 tz->type, cdev->type, ret);
451 return ret;
452 }
453
454 return 0;
455}
456
457static int imx_unbind(struct thermal_zone_device *tz,
458 struct thermal_cooling_device *cdev)
459{
460 int ret;
461
462 ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
463 if (ret) {
464 dev_err(&tz->device,
465 "unbinding zone %s with cdev %s failed:%d\n",
466 tz->type, cdev->type, ret);
467 return ret;
468 }
469
470 return 0;
471}
472
Eduardo Valentincbb07bb2014-01-06 08:54:34 -0400473static struct thermal_zone_device_ops imx_tz_ops = {
Shawn Guoca3de462013-06-24 14:30:44 +0800474 .bind = imx_bind,
475 .unbind = imx_unbind,
476 .get_temp = imx_get_temp,
477 .get_mode = imx_get_mode,
478 .set_mode = imx_set_mode,
479 .get_trip_type = imx_get_trip_type,
480 .get_trip_temp = imx_get_trip_temp,
481 .get_crit_temp = imx_get_crit_temp,
Philipp Zabel017e5142013-08-01 18:33:11 +0200482 .set_trip_temp = imx_set_trip_temp,
Shawn Guoca3de462013-06-24 14:30:44 +0800483};
484
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100485static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
Shawn Guoca3de462013-06-24 14:30:44 +0800486{
487 struct imx_thermal_data *data = platform_get_drvdata(pdev);
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100488 int n1;
Anson Huang749e8be2014-02-12 18:06:35 +0800489 u64 temp64;
Shawn Guoca3de462013-06-24 14:30:44 +0800490
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100491 if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
Shawn Guoca3de462013-06-24 14:30:44 +0800492 dev_err(&pdev->dev, "invalid sensor calibration data\n");
493 return -EINVAL;
494 }
495
496 /*
Anson Huangf085f672018-03-02 09:59:30 +0800497 * On i.MX7D, we only use the calibration data at 25C to get the temp,
498 * Tmeas = ( Nmeas - n1) + 25; n1 is the fuse value for 25C.
499 */
500 if (data->socdata->version == TEMPMON_IMX7D) {
501 data->c1 = (ocotp_ana1 >> 9) & 0x1ff;
502 return 0;
503 }
504
505 /*
Uwe Kleine-Königc5bbdb42017-11-30 10:17:36 +0100506 * The sensor is calibrated at 25 °C (aka T1) and the value measured
507 * (aka N1) at this temperature is provided in bits [31:20] in the
508 * i.MX's OCOTP value ANA1.
509 * To find the actual temperature T, the following formula has to be used
510 * when reading value n from the sensor:
511 *
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100512 * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C
513 * = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C
514 * = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C
Uwe Kleine-Königc5bbdb42017-11-30 10:17:36 +0100515 * = c2 - c1 * N
516 *
517 * with
518 *
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100519 * T1' = 28.580661 °C
520 * c1 = 1 / (0.0015423 * N1 - 0.4297157) °C
521 * c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C
522 * = T1' + N1 * c1
Shawn Guoca3de462013-06-24 14:30:44 +0800523 */
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100524 n1 = ocotp_ana1 >> 20;
Shawn Guoca3de462013-06-24 14:30:44 +0800525
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100526 temp64 = 10000000; /* use 10^7 as fixed point constant for values in formula */
Uwe Kleine-Königc5bbdb42017-11-30 10:17:36 +0100527 temp64 *= 1000; /* to get result in °mC */
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100528 do_div(temp64, 15423 * n1 - 4148468);
Anson Huang749e8be2014-02-12 18:06:35 +0800529 data->c1 = temp64;
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100530 data->c2 = n1 * data->c1 + 28581;
Shawn Guoca3de462013-06-24 14:30:44 +0800531
Leonard Crestezae621552017-07-14 17:11:08 +0300532 return 0;
533}
534
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100535static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
Leonard Crestezae621552017-07-14 17:11:08 +0300536{
537 struct imx_thermal_data *data = platform_get_drvdata(pdev);
Tim Harveya2291ba2015-11-19 06:49:40 -0800538
539 /* The maximum die temp is specified by the Temperature Grade */
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100540 switch ((ocotp_mem0 >> 6) & 0x3) {
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100541 case 0: /* Commercial (0 to 95 °C) */
Tim Harveya2291ba2015-11-19 06:49:40 -0800542 data->temp_grade = "Commercial";
543 data->temp_max = 95000;
544 break;
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100545 case 1: /* Extended Commercial (-20 °C to 105 °C) */
Tim Harveya2291ba2015-11-19 06:49:40 -0800546 data->temp_grade = "Extended Commercial";
547 data->temp_max = 105000;
548 break;
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100549 case 2: /* Industrial (-40 °C to 105 °C) */
Tim Harveya2291ba2015-11-19 06:49:40 -0800550 data->temp_grade = "Industrial";
551 data->temp_max = 105000;
552 break;
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100553 case 3: /* Automotive (-40 °C to 125 °C) */
Tim Harveya2291ba2015-11-19 06:49:40 -0800554 data->temp_grade = "Automotive";
555 data->temp_max = 125000;
556 break;
557 }
Philipp Zabel017e5142013-08-01 18:33:11 +0200558
559 /*
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100560 * Set the critical trip point at 5 °C under max
561 * Set the passive trip point at 10 °C under max (changeable via sysfs)
Philipp Zabel017e5142013-08-01 18:33:11 +0200562 */
Tim Harveya2291ba2015-11-19 06:49:40 -0800563 data->temp_critical = data->temp_max - (1000 * 5);
564 data->temp_passive = data->temp_max - (1000 * 10);
Leonard Crestezae621552017-07-14 17:11:08 +0300565}
566
567static int imx_init_from_tempmon_data(struct platform_device *pdev)
568{
569 struct regmap *map;
570 int ret;
571 u32 val;
572
573 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
574 "fsl,tempmon-data");
575 if (IS_ERR(map)) {
576 ret = PTR_ERR(map);
577 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
578 return ret;
579 }
580
581 ret = regmap_read(map, OCOTP_ANA1, &val);
582 if (ret) {
583 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
584 return ret;
585 }
586 ret = imx_init_calib(pdev, val);
587 if (ret)
588 return ret;
589
590 ret = regmap_read(map, OCOTP_MEM0, &val);
591 if (ret) {
592 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
593 return ret;
594 }
595 imx_init_temp_grade(pdev, val);
596
597 return 0;
598}
599
600static int imx_init_from_nvmem_cells(struct platform_device *pdev)
601{
602 int ret;
603 u32 val;
604
605 ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
606 if (ret)
607 return ret;
Jean-Christophe Duboisbe926ce2018-07-01 00:10:50 +0200608
609 ret = imx_init_calib(pdev, val);
610 if (ret)
611 return ret;
Leonard Crestezae621552017-07-14 17:11:08 +0300612
613 ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
614 if (ret)
615 return ret;
616 imx_init_temp_grade(pdev, val);
Philipp Zabel017e5142013-08-01 18:33:11 +0200617
Shawn Guoca3de462013-06-24 14:30:44 +0800618 return 0;
619}
620
Philipp Zabel37713a12013-08-01 18:33:12 +0200621static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
622{
623 struct imx_thermal_data *data = dev;
624
625 disable_irq_nosync(irq);
626 data->irq_enabled = false;
627
628 return IRQ_WAKE_THREAD;
629}
630
631static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
632{
633 struct imx_thermal_data *data = dev;
634
Sascha Hauer17e83512015-07-24 08:12:54 +0200635 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
Philipp Zabel37713a12013-08-01 18:33:12 +0200636 data->alarm_temp / 1000);
637
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700638 thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
Philipp Zabel37713a12013-08-01 18:33:12 +0200639
640 return IRQ_HANDLED;
641}
642
Anson Huang3c94f172014-08-06 15:12:09 +0800643static const struct of_device_id of_imx_thermal_match[] = {
644 { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
645 { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
Anson Huangf085f672018-03-02 09:59:30 +0800646 { .compatible = "fsl,imx7d-tempmon", .data = &thermal_imx7d_data, },
Anson Huang3c94f172014-08-06 15:12:09 +0800647 { /* end */ }
648};
649MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
650
Bastian Stendera1d00152018-06-08 11:06:39 +0200651/*
652 * Create cooling device in case no #cooling-cells property is available in
653 * CPU node
654 */
655static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
656{
657 struct device_node *np = of_get_cpu_node(data->policy->cpu, NULL);
658 int ret;
659
660 if (!np || !of_find_property(np, "#cooling-cells", NULL)) {
661 data->cdev = cpufreq_cooling_register(data->policy);
662 if (IS_ERR(data->cdev)) {
663 ret = PTR_ERR(data->cdev);
664 cpufreq_cpu_put(data->policy);
665 return ret;
666 }
667 }
668
669 return 0;
670}
671
Shawn Guoca3de462013-06-24 14:30:44 +0800672static int imx_thermal_probe(struct platform_device *pdev)
673{
674 struct imx_thermal_data *data;
Shawn Guoca3de462013-06-24 14:30:44 +0800675 struct regmap *map;
Philipp Zabel37713a12013-08-01 18:33:12 +0200676 int measure_freq;
Shawn Guoca3de462013-06-24 14:30:44 +0800677 int ret;
678
679 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
680 if (!data)
681 return -ENOMEM;
682
683 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
684 if (IS_ERR(map)) {
685 ret = PTR_ERR(map);
686 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
687 return ret;
688 }
689 data->tempmon = map;
690
Corentin LABBE829bc78a2016-08-16 10:51:38 +0200691 data->socdata = of_device_get_match_data(&pdev->dev);
Shailendra Verma8b051ec2017-01-30 10:34:58 +0530692 if (!data->socdata) {
693 dev_err(&pdev->dev, "no device match found\n");
694 return -ENODEV;
695 }
Anson Huang3c94f172014-08-06 15:12:09 +0800696
697 /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
698 if (data->socdata->version == TEMPMON_IMX6SX) {
Anson Huangf085f672018-03-02 09:59:30 +0800699 regmap_write(map, IMX6_MISC1 + REG_CLR,
700 IMX6_MISC1_IRQ_TEMPHIGH | IMX6_MISC1_IRQ_TEMPLOW
701 | IMX6_MISC1_IRQ_TEMPPANIC);
Anson Huang3c94f172014-08-06 15:12:09 +0800702 /*
703 * reset value of LOW ALARM is incorrect, set it to lowest
704 * value to avoid false trigger of low alarm.
705 */
Anson Huangf085f672018-03-02 09:59:30 +0800706 regmap_write(map, data->socdata->low_alarm_ctrl + REG_SET,
707 data->socdata->low_alarm_mask);
Anson Huang3c94f172014-08-06 15:12:09 +0800708 }
709
Philipp Zabel37713a12013-08-01 18:33:12 +0200710 data->irq = platform_get_irq(pdev, 0);
711 if (data->irq < 0)
712 return data->irq;
713
Shawn Guoca3de462013-06-24 14:30:44 +0800714 platform_set_drvdata(pdev, data);
715
Leonard Crestezae621552017-07-14 17:11:08 +0300716 if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
717 ret = imx_init_from_nvmem_cells(pdev);
718 if (ret == -EPROBE_DEFER)
719 return ret;
720 if (ret) {
721 dev_err(&pdev->dev, "failed to init from nvmem: %d\n",
722 ret);
723 return ret;
724 }
725 } else {
726 ret = imx_init_from_tempmon_data(pdev);
727 if (ret) {
728 dev_err(&pdev->dev, "failed to init from from fsl,tempmon-data\n");
729 return ret;
730 }
Shawn Guoca3de462013-06-24 14:30:44 +0800731 }
732
733 /* Make sure sensor is in known good state for measurements */
Anson Huangf085f672018-03-02 09:59:30 +0800734 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
735 data->socdata->power_down_mask);
736 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
737 data->socdata->measure_temp_mask);
738 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
739 data->socdata->measure_freq_mask);
740 if (data->socdata->version != TEMPMON_IMX7D)
741 regmap_write(map, IMX6_MISC0 + REG_SET,
742 IMX6_MISC0_REFTOP_SELBIASOFF);
743 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
744 data->socdata->power_down_mask);
Shawn Guoca3de462013-06-24 14:30:44 +0800745
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530746 data->policy = cpufreq_cpu_get(0);
747 if (!data->policy) {
748 pr_debug("%s: CPUFreq policy not found\n", __func__);
749 return -EPROBE_DEFER;
750 }
751
Bastian Stendera1d00152018-06-08 11:06:39 +0200752 ret = imx_thermal_register_legacy_cooling(data);
753 if (ret) {
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530754 dev_err(&pdev->dev,
755 "failed to register cpufreq cooling device: %d\n", ret);
Shawn Guoca3de462013-06-24 14:30:44 +0800756 return ret;
757 }
758
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100759 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
760 if (IS_ERR(data->thermal_clk)) {
761 ret = PTR_ERR(data->thermal_clk);
762 if (ret != -EPROBE_DEFER)
763 dev_err(&pdev->dev,
764 "failed to get thermal clk: %d\n", ret);
765 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530766 cpufreq_cpu_put(data->policy);
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100767 return ret;
768 }
769
770 /*
771 * Thermal sensor needs clk on to get correct value, normally
772 * we should enable its clk before taking measurement and disable
773 * clk after measurement is done, but if alarm function is enabled,
774 * hardware will auto measure the temperature periodically, so we
775 * need to keep the clk always on for alarm function.
776 */
777 ret = clk_prepare_enable(data->thermal_clk);
778 if (ret) {
779 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
780 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530781 cpufreq_cpu_put(data->policy);
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100782 return ret;
783 }
784
Shawn Guoca3de462013-06-24 14:30:44 +0800785 data->tz = thermal_zone_device_register("imx_thermal_zone",
Philipp Zabel017e5142013-08-01 18:33:11 +0200786 IMX_TRIP_NUM,
787 BIT(IMX_TRIP_PASSIVE), data,
Shawn Guoca3de462013-06-24 14:30:44 +0800788 &imx_tz_ops, NULL,
789 IMX_PASSIVE_DELAY,
790 IMX_POLLING_DELAY);
791 if (IS_ERR(data->tz)) {
792 ret = PTR_ERR(data->tz);
793 dev_err(&pdev->dev,
794 "failed to register thermal zone device %d\n", ret);
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100795 clk_disable_unprepare(data->thermal_clk);
Shawn Guoca3de462013-06-24 14:30:44 +0800796 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530797 cpufreq_cpu_put(data->policy);
Shawn Guoca3de462013-06-24 14:30:44 +0800798 return ret;
799 }
800
Tim Harveya2291ba2015-11-19 06:49:40 -0800801 dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
802 " critical:%dC passive:%dC\n", data->temp_grade,
803 data->temp_max / 1000, data->temp_critical / 1000,
804 data->temp_passive / 1000);
805
Philipp Zabel37713a12013-08-01 18:33:12 +0200806 /* Enable measurements at ~ 10 Hz */
Anson Huangf085f672018-03-02 09:59:30 +0800807 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
808 data->socdata->measure_freq_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200809 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
Anson Huangf085f672018-03-02 09:59:30 +0800810 regmap_write(map, data->socdata->measure_freq_ctrl + REG_SET,
811 measure_freq << data->socdata->measure_freq_shift);
Philipp Zabel37713a12013-08-01 18:33:12 +0200812 imx_set_alarm_temp(data, data->temp_passive);
Anson Huang3c94f172014-08-06 15:12:09 +0800813
814 if (data->socdata->version == TEMPMON_IMX6SX)
815 imx_set_panic_temp(data, data->temp_critical);
816
Anson Huangf085f672018-03-02 09:59:30 +0800817 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
818 data->socdata->power_down_mask);
819 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
820 data->socdata->measure_temp_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200821
Mikhail Lappocf1ba1d2018-02-02 16:17:46 -0200822 data->irq_enabled = true;
823 data->mode = THERMAL_DEVICE_ENABLED;
824
Bai Ping84866ee2015-09-14 19:09:51 +0800825 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
826 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
827 0, "imx_thermal", data);
828 if (ret < 0) {
829 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
830 clk_disable_unprepare(data->thermal_clk);
831 thermal_zone_device_unregister(data->tz);
832 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530833 cpufreq_cpu_put(data->policy);
Bai Ping84866ee2015-09-14 19:09:51 +0800834 return ret;
835 }
836
Shawn Guoca3de462013-06-24 14:30:44 +0800837 return 0;
838}
839
840static int imx_thermal_remove(struct platform_device *pdev)
841{
842 struct imx_thermal_data *data = platform_get_drvdata(pdev);
Philipp Zabel37713a12013-08-01 18:33:12 +0200843 struct regmap *map = data->tempmon;
844
845 /* Disable measurements */
Anson Huangf085f672018-03-02 09:59:30 +0800846 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
847 data->socdata->power_down_mask);
Anson Huang329fe7b2013-12-23 15:49:22 -0500848 if (!IS_ERR(data->thermal_clk))
849 clk_disable_unprepare(data->thermal_clk);
Shawn Guoca3de462013-06-24 14:30:44 +0800850
851 thermal_zone_device_unregister(data->tz);
852 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530853 cpufreq_cpu_put(data->policy);
Shawn Guoca3de462013-06-24 14:30:44 +0800854
855 return 0;
856}
857
858#ifdef CONFIG_PM_SLEEP
859static int imx_thermal_suspend(struct device *dev)
860{
861 struct imx_thermal_data *data = dev_get_drvdata(dev);
862 struct regmap *map = data->tempmon;
Shawn Guoca3de462013-06-24 14:30:44 +0800863
Anson Huangb46cce52013-12-24 09:43:24 -0500864 /*
865 * Need to disable thermal sensor, otherwise, when thermal core
866 * try to get temperature before thermal sensor resume, a wrong
867 * temperature will be read as the thermal sensor is powered
868 * down.
869 */
Anson Huangf085f672018-03-02 09:59:30 +0800870 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
871 data->socdata->measure_temp_mask);
872 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
873 data->socdata->power_down_mask);
Anson Huangb46cce52013-12-24 09:43:24 -0500874 data->mode = THERMAL_DEVICE_DISABLED;
Anson Huangd26eef82015-01-06 18:50:22 +0800875 clk_disable_unprepare(data->thermal_clk);
Shawn Guoca3de462013-06-24 14:30:44 +0800876
877 return 0;
878}
879
880static int imx_thermal_resume(struct device *dev)
881{
Anson Huangb46cce52013-12-24 09:43:24 -0500882 struct imx_thermal_data *data = dev_get_drvdata(dev);
883 struct regmap *map = data->tempmon;
Arvind Yadave3bdc8d2017-06-06 15:12:37 +0530884 int ret;
Anson Huangb46cce52013-12-24 09:43:24 -0500885
Arvind Yadave3bdc8d2017-06-06 15:12:37 +0530886 ret = clk_prepare_enable(data->thermal_clk);
887 if (ret)
888 return ret;
Anson Huangb46cce52013-12-24 09:43:24 -0500889 /* Enabled thermal sensor after resume */
Anson Huangf085f672018-03-02 09:59:30 +0800890 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
891 data->socdata->power_down_mask);
892 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
893 data->socdata->measure_temp_mask);
Anson Huangb46cce52013-12-24 09:43:24 -0500894 data->mode = THERMAL_DEVICE_ENABLED;
895
Shawn Guoca3de462013-06-24 14:30:44 +0800896 return 0;
897}
898#endif
899
900static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
901 imx_thermal_suspend, imx_thermal_resume);
902
Shawn Guoca3de462013-06-24 14:30:44 +0800903static struct platform_driver imx_thermal = {
904 .driver = {
905 .name = "imx_thermal",
Shawn Guoca3de462013-06-24 14:30:44 +0800906 .pm = &imx_thermal_pm_ops,
907 .of_match_table = of_imx_thermal_match,
908 },
909 .probe = imx_thermal_probe,
910 .remove = imx_thermal_remove,
911};
912module_platform_driver(imx_thermal);
913
914MODULE_AUTHOR("Freescale Semiconductor, Inc.");
915MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
916MODULE_LICENSE("GPL v2");
917MODULE_ALIAS("platform:imx-thermal");