blob: b1f82d64253e1077b86a2d20954330f436ef09bc [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>
Viresh Kumar4d753aa2017-04-25 15:57:14 +05306#include <linux/cpufreq.h>
Shawn Guoca3de462013-06-24 14:30:44 +08007#include <linux/cpu_cooling.h>
Shawn Guoca3de462013-06-24 14:30:44 +08008#include <linux/delay.h>
9#include <linux/device.h>
10#include <linux/init.h>
Philipp Zabel37713a12013-08-01 18:33:12 +020011#include <linux/interrupt.h>
Shawn Guoca3de462013-06-24 14:30:44 +080012#include <linux/io.h>
13#include <linux/kernel.h>
14#include <linux/mfd/syscon.h>
15#include <linux/module.h>
16#include <linux/of.h>
Anson Huang3c94f172014-08-06 15:12:09 +080017#include <linux/of_device.h>
Shawn Guoca3de462013-06-24 14:30:44 +080018#include <linux/platform_device.h>
19#include <linux/regmap.h>
20#include <linux/slab.h>
21#include <linux/thermal.h>
22#include <linux/types.h>
Leonard Crestezae621552017-07-14 17:11:08 +030023#include <linux/nvmem-consumer.h>
Shawn Guoca3de462013-06-24 14:30:44 +080024
25#define REG_SET 0x4
26#define REG_CLR 0x8
27#define REG_TOG 0xc
28
Anson Huangf085f672018-03-02 09:59:30 +080029/* i.MX6 specific */
30#define IMX6_MISC0 0x0150
31#define IMX6_MISC0_REFTOP_SELBIASOFF (1 << 3)
32#define IMX6_MISC1 0x0160
33#define IMX6_MISC1_IRQ_TEMPHIGH (1 << 29)
Anson Huang3c94f172014-08-06 15:12:09 +080034/* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
Anson Huangf085f672018-03-02 09:59:30 +080035#define IMX6_MISC1_IRQ_TEMPLOW (1 << 28)
36#define IMX6_MISC1_IRQ_TEMPPANIC (1 << 27)
Shawn Guoca3de462013-06-24 14:30:44 +080037
Anson Huangf085f672018-03-02 09:59:30 +080038#define IMX6_TEMPSENSE0 0x0180
39#define IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT 20
40#define IMX6_TEMPSENSE0_ALARM_VALUE_MASK (0xfff << 20)
41#define IMX6_TEMPSENSE0_TEMP_CNT_SHIFT 8
42#define IMX6_TEMPSENSE0_TEMP_CNT_MASK (0xfff << 8)
43#define IMX6_TEMPSENSE0_FINISHED (1 << 2)
44#define IMX6_TEMPSENSE0_MEASURE_TEMP (1 << 1)
45#define IMX6_TEMPSENSE0_POWER_DOWN (1 << 0)
Shawn Guoca3de462013-06-24 14:30:44 +080046
Anson Huangf085f672018-03-02 09:59:30 +080047#define IMX6_TEMPSENSE1 0x0190
48#define IMX6_TEMPSENSE1_MEASURE_FREQ 0xffff
49#define IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT 0
Shawn Guoca3de462013-06-24 14:30:44 +080050
Tim Harveya2291ba2015-11-19 06:49:40 -080051#define OCOTP_MEM0 0x0480
Shawn Guoca3de462013-06-24 14:30:44 +080052#define OCOTP_ANA1 0x04e0
53
Anson Huangf085f672018-03-02 09:59:30 +080054/* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
55#define IMX6_TEMPSENSE2 0x0290
56#define IMX6_TEMPSENSE2_LOW_VALUE_SHIFT 0
57#define IMX6_TEMPSENSE2_LOW_VALUE_MASK 0xfff
58#define IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT 16
59#define IMX6_TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
60
61/* i.MX7 specific */
62#define IMX7_ANADIG_DIGPROG 0x800
63#define IMX7_TEMPSENSE0 0x300
64#define IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT 18
65#define IMX7_TEMPSENSE0_PANIC_ALARM_MASK (0x1ff << 18)
66#define IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT 9
67#define IMX7_TEMPSENSE0_HIGH_ALARM_MASK (0x1ff << 9)
68#define IMX7_TEMPSENSE0_LOW_ALARM_SHIFT 0
69#define IMX7_TEMPSENSE0_LOW_ALARM_MASK 0x1ff
70
71#define IMX7_TEMPSENSE1 0x310
72#define IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT 16
73#define IMX7_TEMPSENSE1_MEASURE_FREQ_MASK (0xffff << 16)
74#define IMX7_TEMPSENSE1_FINISHED (1 << 11)
75#define IMX7_TEMPSENSE1_MEASURE_TEMP (1 << 10)
76#define IMX7_TEMPSENSE1_POWER_DOWN (1 << 9)
77#define IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT 0
78#define IMX7_TEMPSENSE1_TEMP_VALUE_MASK 0x1ff
79
Shawn Guoca3de462013-06-24 14:30:44 +080080/* The driver supports 1 passive trip point and 1 critical trip point */
81enum imx_thermal_trip {
82 IMX_TRIP_PASSIVE,
83 IMX_TRIP_CRITICAL,
84 IMX_TRIP_NUM,
85};
86
Shawn Guoca3de462013-06-24 14:30:44 +080087#define IMX_POLLING_DELAY 2000 /* millisecond */
88#define IMX_PASSIVE_DELAY 1000
89
Anson Huang3c94f172014-08-06 15:12:09 +080090#define TEMPMON_IMX6Q 1
91#define TEMPMON_IMX6SX 2
Anson Huangf085f672018-03-02 09:59:30 +080092#define TEMPMON_IMX7D 3
Anson Huang3c94f172014-08-06 15:12:09 +080093
94struct thermal_soc_data {
95 u32 version;
Anson Huangf085f672018-03-02 09:59:30 +080096
97 u32 sensor_ctrl;
98 u32 power_down_mask;
99 u32 measure_temp_mask;
100
101 u32 measure_freq_ctrl;
102 u32 measure_freq_mask;
103 u32 measure_freq_shift;
104
105 u32 temp_data;
106 u32 temp_value_mask;
107 u32 temp_value_shift;
108 u32 temp_valid_mask;
109
110 u32 panic_alarm_ctrl;
111 u32 panic_alarm_mask;
112 u32 panic_alarm_shift;
113
114 u32 high_alarm_ctrl;
115 u32 high_alarm_mask;
116 u32 high_alarm_shift;
117
118 u32 low_alarm_ctrl;
119 u32 low_alarm_mask;
120 u32 low_alarm_shift;
Anson Huang3c94f172014-08-06 15:12:09 +0800121};
122
123static struct thermal_soc_data thermal_imx6q_data = {
124 .version = TEMPMON_IMX6Q,
Anson Huangf085f672018-03-02 09:59:30 +0800125
126 .sensor_ctrl = IMX6_TEMPSENSE0,
127 .power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
128 .measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
129
130 .measure_freq_ctrl = IMX6_TEMPSENSE1,
131 .measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
132 .measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
133
134 .temp_data = IMX6_TEMPSENSE0,
135 .temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
136 .temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
137 .temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
138
139 .high_alarm_ctrl = IMX6_TEMPSENSE0,
140 .high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
141 .high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
Anson Huang3c94f172014-08-06 15:12:09 +0800142};
143
144static struct thermal_soc_data thermal_imx6sx_data = {
145 .version = TEMPMON_IMX6SX,
Anson Huangf085f672018-03-02 09:59:30 +0800146
147 .sensor_ctrl = IMX6_TEMPSENSE0,
148 .power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
149 .measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
150
151 .measure_freq_ctrl = IMX6_TEMPSENSE1,
152 .measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
153 .measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
154
155 .temp_data = IMX6_TEMPSENSE0,
156 .temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
157 .temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
158 .temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
159
160 .high_alarm_ctrl = IMX6_TEMPSENSE0,
161 .high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
162 .high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
163
164 .panic_alarm_ctrl = IMX6_TEMPSENSE2,
165 .panic_alarm_mask = IMX6_TEMPSENSE2_PANIC_VALUE_MASK,
166 .panic_alarm_shift = IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT,
167
168 .low_alarm_ctrl = IMX6_TEMPSENSE2,
169 .low_alarm_mask = IMX6_TEMPSENSE2_LOW_VALUE_MASK,
170 .low_alarm_shift = IMX6_TEMPSENSE2_LOW_VALUE_SHIFT,
171};
172
173static struct thermal_soc_data thermal_imx7d_data = {
174 .version = TEMPMON_IMX7D,
175
176 .sensor_ctrl = IMX7_TEMPSENSE1,
177 .power_down_mask = IMX7_TEMPSENSE1_POWER_DOWN,
178 .measure_temp_mask = IMX7_TEMPSENSE1_MEASURE_TEMP,
179
180 .measure_freq_ctrl = IMX7_TEMPSENSE1,
181 .measure_freq_shift = IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT,
182 .measure_freq_mask = IMX7_TEMPSENSE1_MEASURE_FREQ_MASK,
183
184 .temp_data = IMX7_TEMPSENSE1,
185 .temp_value_mask = IMX7_TEMPSENSE1_TEMP_VALUE_MASK,
186 .temp_value_shift = IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT,
187 .temp_valid_mask = IMX7_TEMPSENSE1_FINISHED,
188
189 .panic_alarm_ctrl = IMX7_TEMPSENSE1,
190 .panic_alarm_mask = IMX7_TEMPSENSE0_PANIC_ALARM_MASK,
191 .panic_alarm_shift = IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT,
192
193 .high_alarm_ctrl = IMX7_TEMPSENSE0,
194 .high_alarm_mask = IMX7_TEMPSENSE0_HIGH_ALARM_MASK,
195 .high_alarm_shift = IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT,
196
197 .low_alarm_ctrl = IMX7_TEMPSENSE0,
198 .low_alarm_mask = IMX7_TEMPSENSE0_LOW_ALARM_MASK,
199 .low_alarm_shift = IMX7_TEMPSENSE0_LOW_ALARM_SHIFT,
Anson Huang3c94f172014-08-06 15:12:09 +0800200};
201
Shawn Guoca3de462013-06-24 14:30:44 +0800202struct imx_thermal_data {
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530203 struct cpufreq_policy *policy;
Shawn Guoca3de462013-06-24 14:30:44 +0800204 struct thermal_zone_device *tz;
205 struct thermal_cooling_device *cdev;
206 enum thermal_device_mode mode;
207 struct regmap *tempmon;
Leonard Crestezae621552017-07-14 17:11:08 +0300208 u32 c1, c2; /* See formula in imx_init_calib() */
Sascha Hauer17e83512015-07-24 08:12:54 +0200209 int temp_passive;
210 int temp_critical;
Tim Harveya2291ba2015-11-19 06:49:40 -0800211 int temp_max;
Sascha Hauer17e83512015-07-24 08:12:54 +0200212 int alarm_temp;
213 int last_temp;
Philipp Zabel37713a12013-08-01 18:33:12 +0200214 bool irq_enabled;
215 int irq;
Anson Huang329fe7b2013-12-23 15:49:22 -0500216 struct clk *thermal_clk;
Anson Huang3c94f172014-08-06 15:12:09 +0800217 const struct thermal_soc_data *socdata;
Tim Harveya2291ba2015-11-19 06:49:40 -0800218 const char *temp_grade;
Shawn Guoca3de462013-06-24 14:30:44 +0800219};
220
Anson Huang3c94f172014-08-06 15:12:09 +0800221static void imx_set_panic_temp(struct imx_thermal_data *data,
Sascha Hauer17e83512015-07-24 08:12:54 +0200222 int panic_temp)
Anson Huang3c94f172014-08-06 15:12:09 +0800223{
Anson Huangf085f672018-03-02 09:59:30 +0800224 const struct thermal_soc_data *soc_data = data->socdata;
Anson Huang3c94f172014-08-06 15:12:09 +0800225 struct regmap *map = data->tempmon;
226 int critical_value;
227
228 critical_value = (data->c2 - panic_temp) / data->c1;
Anson Huangf085f672018-03-02 09:59:30 +0800229
230 regmap_write(map, soc_data->panic_alarm_ctrl + REG_CLR,
231 soc_data->panic_alarm_mask);
232 regmap_write(map, soc_data->panic_alarm_ctrl + REG_SET,
233 critical_value << soc_data->panic_alarm_shift);
Anson Huang3c94f172014-08-06 15:12:09 +0800234}
235
Philipp Zabel37713a12013-08-01 18:33:12 +0200236static void imx_set_alarm_temp(struct imx_thermal_data *data,
Sascha Hauer17e83512015-07-24 08:12:54 +0200237 int alarm_temp)
Philipp Zabel37713a12013-08-01 18:33:12 +0200238{
239 struct regmap *map = data->tempmon;
Anson Huangf085f672018-03-02 09:59:30 +0800240 const struct thermal_soc_data *soc_data = data->socdata;
Philipp Zabel37713a12013-08-01 18:33:12 +0200241 int alarm_value;
242
243 data->alarm_temp = alarm_temp;
Anson Huangf085f672018-03-02 09:59:30 +0800244
245 if (data->socdata->version == TEMPMON_IMX7D)
246 alarm_value = alarm_temp / 1000 + data->c1 - 25;
247 else
248 alarm_value = (data->c2 - alarm_temp) / data->c1;
249
250 regmap_write(map, soc_data->high_alarm_ctrl + REG_CLR,
251 soc_data->high_alarm_mask);
252 regmap_write(map, soc_data->high_alarm_ctrl + REG_SET,
253 alarm_value << soc_data->high_alarm_shift);
Philipp Zabel37713a12013-08-01 18:33:12 +0200254}
255
Sascha Hauer17e83512015-07-24 08:12:54 +0200256static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800257{
258 struct imx_thermal_data *data = tz->devdata;
Anson Huangf085f672018-03-02 09:59:30 +0800259 const struct thermal_soc_data *soc_data = data->socdata;
Shawn Guoca3de462013-06-24 14:30:44 +0800260 struct regmap *map = data->tempmon;
Shawn Guoca3de462013-06-24 14:30:44 +0800261 unsigned int n_meas;
Philipp Zabel37713a12013-08-01 18:33:12 +0200262 bool wait;
Shawn Guoca3de462013-06-24 14:30:44 +0800263 u32 val;
264
Philipp Zabel37713a12013-08-01 18:33:12 +0200265 if (data->mode == THERMAL_DEVICE_ENABLED) {
266 /* Check if a measurement is currently in progress */
Anson Huangf085f672018-03-02 09:59:30 +0800267 regmap_read(map, soc_data->temp_data, &val);
268 wait = !(val & soc_data->temp_valid_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200269 } else {
270 /*
271 * Every time we measure the temperature, we will power on the
272 * temperature sensor, enable measurements, take a reading,
273 * disable measurements, power off the temperature sensor.
274 */
Anson Huangf085f672018-03-02 09:59:30 +0800275 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
276 soc_data->power_down_mask);
277 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
278 soc_data->measure_temp_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200279
280 wait = true;
281 }
Shawn Guoca3de462013-06-24 14:30:44 +0800282
283 /*
284 * According to the temp sensor designers, it may require up to ~17us
285 * to complete a measurement.
286 */
Philipp Zabel37713a12013-08-01 18:33:12 +0200287 if (wait)
288 usleep_range(20, 50);
Shawn Guoca3de462013-06-24 14:30:44 +0800289
Anson Huangf085f672018-03-02 09:59:30 +0800290 regmap_read(map, soc_data->temp_data, &val);
Philipp Zabel37713a12013-08-01 18:33:12 +0200291
292 if (data->mode != THERMAL_DEVICE_ENABLED) {
Anson Huangf085f672018-03-02 09:59:30 +0800293 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
294 soc_data->measure_temp_mask);
295 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
296 soc_data->power_down_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200297 }
Shawn Guoca3de462013-06-24 14:30:44 +0800298
Anson Huangf085f672018-03-02 09:59:30 +0800299 if ((val & soc_data->temp_valid_mask) == 0) {
Shawn Guoca3de462013-06-24 14:30:44 +0800300 dev_dbg(&tz->device, "temp measurement never finished\n");
301 return -EAGAIN;
302 }
303
Anson Huangf085f672018-03-02 09:59:30 +0800304 n_meas = (val & soc_data->temp_value_mask)
305 >> soc_data->temp_value_shift;
Shawn Guoca3de462013-06-24 14:30:44 +0800306
Leonard Crestezae621552017-07-14 17:11:08 +0300307 /* See imx_init_calib() for formula derivation */
Anson Huangf085f672018-03-02 09:59:30 +0800308 if (data->socdata->version == TEMPMON_IMX7D)
309 *temp = (n_meas - data->c1 + 25) * 1000;
310 else
311 *temp = data->c2 - n_meas * data->c1;
Shawn Guoca3de462013-06-24 14:30:44 +0800312
Anson Huang3c94f172014-08-06 15:12:09 +0800313 /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
314 if (data->socdata->version == TEMPMON_IMX6Q) {
315 if (data->alarm_temp == data->temp_passive &&
316 *temp >= data->temp_passive)
317 imx_set_alarm_temp(data, data->temp_critical);
318 if (data->alarm_temp == data->temp_critical &&
319 *temp < data->temp_passive) {
320 imx_set_alarm_temp(data, data->temp_passive);
Sascha Hauer17e83512015-07-24 08:12:54 +0200321 dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
Anson Huang3c94f172014-08-06 15:12:09 +0800322 data->alarm_temp / 1000);
323 }
Philipp Zabel37713a12013-08-01 18:33:12 +0200324 }
325
326 if (*temp != data->last_temp) {
Sascha Hauer17e83512015-07-24 08:12:54 +0200327 dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
Philipp Zabel37713a12013-08-01 18:33:12 +0200328 data->last_temp = *temp;
329 }
330
331 /* Reenable alarm IRQ if temperature below alarm temperature */
332 if (!data->irq_enabled && *temp < data->alarm_temp) {
333 data->irq_enabled = true;
334 enable_irq(data->irq);
Shawn Guoca3de462013-06-24 14:30:44 +0800335 }
336
337 return 0;
338}
339
340static int imx_get_mode(struct thermal_zone_device *tz,
341 enum thermal_device_mode *mode)
342{
343 struct imx_thermal_data *data = tz->devdata;
344
345 *mode = data->mode;
346
347 return 0;
348}
349
350static int imx_set_mode(struct thermal_zone_device *tz,
351 enum thermal_device_mode mode)
352{
353 struct imx_thermal_data *data = tz->devdata;
Philipp Zabel37713a12013-08-01 18:33:12 +0200354 struct regmap *map = data->tempmon;
Anson Huangf085f672018-03-02 09:59:30 +0800355 const struct thermal_soc_data *soc_data = data->socdata;
Shawn Guoca3de462013-06-24 14:30:44 +0800356
357 if (mode == THERMAL_DEVICE_ENABLED) {
358 tz->polling_delay = IMX_POLLING_DELAY;
359 tz->passive_delay = IMX_PASSIVE_DELAY;
Philipp Zabel37713a12013-08-01 18:33:12 +0200360
Anson Huangf085f672018-03-02 09:59:30 +0800361 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
362 soc_data->power_down_mask);
363 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
364 soc_data->measure_temp_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200365
366 if (!data->irq_enabled) {
367 data->irq_enabled = true;
368 enable_irq(data->irq);
369 }
Shawn Guoca3de462013-06-24 14:30:44 +0800370 } else {
Anson Huangf085f672018-03-02 09:59:30 +0800371 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
372 soc_data->measure_temp_mask);
373 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
374 soc_data->power_down_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200375
Shawn Guoca3de462013-06-24 14:30:44 +0800376 tz->polling_delay = 0;
377 tz->passive_delay = 0;
Philipp Zabel37713a12013-08-01 18:33:12 +0200378
379 if (data->irq_enabled) {
380 disable_irq(data->irq);
381 data->irq_enabled = false;
382 }
Shawn Guoca3de462013-06-24 14:30:44 +0800383 }
384
385 data->mode = mode;
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700386 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Shawn Guoca3de462013-06-24 14:30:44 +0800387
388 return 0;
389}
390
391static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
392 enum thermal_trip_type *type)
393{
394 *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
395 THERMAL_TRIP_CRITICAL;
396 return 0;
397}
398
Sascha Hauer17e83512015-07-24 08:12:54 +0200399static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800400{
Philipp Zabel017e5142013-08-01 18:33:11 +0200401 struct imx_thermal_data *data = tz->devdata;
402
403 *temp = data->temp_critical;
Shawn Guoca3de462013-06-24 14:30:44 +0800404 return 0;
405}
406
407static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200408 int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800409{
Philipp Zabel017e5142013-08-01 18:33:11 +0200410 struct imx_thermal_data *data = tz->devdata;
411
412 *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
413 data->temp_critical;
414 return 0;
415}
416
417static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200418 int temp)
Philipp Zabel017e5142013-08-01 18:33:11 +0200419{
420 struct imx_thermal_data *data = tz->devdata;
421
Tim Harveya2291ba2015-11-19 06:49:40 -0800422 /* do not allow changing critical threshold */
Philipp Zabel017e5142013-08-01 18:33:11 +0200423 if (trip == IMX_TRIP_CRITICAL)
424 return -EPERM;
425
Tim Harveya2291ba2015-11-19 06:49:40 -0800426 /* do not allow passive to be set higher than critical */
427 if (temp < 0 || temp > data->temp_critical)
Philipp Zabel017e5142013-08-01 18:33:11 +0200428 return -EINVAL;
429
430 data->temp_passive = temp;
431
Philipp Zabel37713a12013-08-01 18:33:12 +0200432 imx_set_alarm_temp(data, temp);
433
Shawn Guoca3de462013-06-24 14:30:44 +0800434 return 0;
435}
436
437static int imx_bind(struct thermal_zone_device *tz,
438 struct thermal_cooling_device *cdev)
439{
440 int ret;
441
442 ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
443 THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000444 THERMAL_NO_LIMIT,
445 THERMAL_WEIGHT_DEFAULT);
Shawn Guoca3de462013-06-24 14:30:44 +0800446 if (ret) {
447 dev_err(&tz->device,
448 "binding zone %s with cdev %s failed:%d\n",
449 tz->type, cdev->type, ret);
450 return ret;
451 }
452
453 return 0;
454}
455
456static int imx_unbind(struct thermal_zone_device *tz,
457 struct thermal_cooling_device *cdev)
458{
459 int ret;
460
461 ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
462 if (ret) {
463 dev_err(&tz->device,
464 "unbinding zone %s with cdev %s failed:%d\n",
465 tz->type, cdev->type, ret);
466 return ret;
467 }
468
469 return 0;
470}
471
Eduardo Valentincbb07bb2014-01-06 08:54:34 -0400472static struct thermal_zone_device_ops imx_tz_ops = {
Shawn Guoca3de462013-06-24 14:30:44 +0800473 .bind = imx_bind,
474 .unbind = imx_unbind,
475 .get_temp = imx_get_temp,
476 .get_mode = imx_get_mode,
477 .set_mode = imx_set_mode,
478 .get_trip_type = imx_get_trip_type,
479 .get_trip_temp = imx_get_trip_temp,
480 .get_crit_temp = imx_get_crit_temp,
Philipp Zabel017e5142013-08-01 18:33:11 +0200481 .set_trip_temp = imx_set_trip_temp,
Shawn Guoca3de462013-06-24 14:30:44 +0800482};
483
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100484static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
Shawn Guoca3de462013-06-24 14:30:44 +0800485{
486 struct imx_thermal_data *data = platform_get_drvdata(pdev);
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100487 int n1;
Anson Huang749e8be2014-02-12 18:06:35 +0800488 u64 temp64;
Shawn Guoca3de462013-06-24 14:30:44 +0800489
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100490 if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
Shawn Guoca3de462013-06-24 14:30:44 +0800491 dev_err(&pdev->dev, "invalid sensor calibration data\n");
492 return -EINVAL;
493 }
494
495 /*
Anson Huangf085f672018-03-02 09:59:30 +0800496 * On i.MX7D, we only use the calibration data at 25C to get the temp,
497 * Tmeas = ( Nmeas - n1) + 25; n1 is the fuse value for 25C.
498 */
499 if (data->socdata->version == TEMPMON_IMX7D) {
500 data->c1 = (ocotp_ana1 >> 9) & 0x1ff;
501 return 0;
502 }
503
504 /*
Uwe Kleine-Königc5bbdb42017-11-30 10:17:36 +0100505 * The sensor is calibrated at 25 °C (aka T1) and the value measured
506 * (aka N1) at this temperature is provided in bits [31:20] in the
507 * i.MX's OCOTP value ANA1.
508 * To find the actual temperature T, the following formula has to be used
509 * when reading value n from the sensor:
510 *
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100511 * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C
512 * = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C
513 * = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C
Uwe Kleine-Königc5bbdb42017-11-30 10:17:36 +0100514 * = c2 - c1 * N
515 *
516 * with
517 *
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100518 * T1' = 28.580661 °C
519 * c1 = 1 / (0.0015423 * N1 - 0.4297157) °C
520 * c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C
521 * = T1' + N1 * c1
Shawn Guoca3de462013-06-24 14:30:44 +0800522 */
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100523 n1 = ocotp_ana1 >> 20;
Shawn Guoca3de462013-06-24 14:30:44 +0800524
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100525 temp64 = 10000000; /* use 10^7 as fixed point constant for values in formula */
Uwe Kleine-Königc5bbdb42017-11-30 10:17:36 +0100526 temp64 *= 1000; /* to get result in °mC */
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100527 do_div(temp64, 15423 * n1 - 4148468);
Anson Huang749e8be2014-02-12 18:06:35 +0800528 data->c1 = temp64;
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100529 data->c2 = n1 * data->c1 + 28581;
Shawn Guoca3de462013-06-24 14:30:44 +0800530
Leonard Crestezae621552017-07-14 17:11:08 +0300531 return 0;
532}
533
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100534static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
Leonard Crestezae621552017-07-14 17:11:08 +0300535{
536 struct imx_thermal_data *data = platform_get_drvdata(pdev);
Tim Harveya2291ba2015-11-19 06:49:40 -0800537
538 /* The maximum die temp is specified by the Temperature Grade */
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100539 switch ((ocotp_mem0 >> 6) & 0x3) {
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100540 case 0: /* Commercial (0 to 95 °C) */
Tim Harveya2291ba2015-11-19 06:49:40 -0800541 data->temp_grade = "Commercial";
542 data->temp_max = 95000;
543 break;
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100544 case 1: /* Extended Commercial (-20 °C to 105 °C) */
Tim Harveya2291ba2015-11-19 06:49:40 -0800545 data->temp_grade = "Extended Commercial";
546 data->temp_max = 105000;
547 break;
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100548 case 2: /* Industrial (-40 °C to 105 °C) */
Tim Harveya2291ba2015-11-19 06:49:40 -0800549 data->temp_grade = "Industrial";
550 data->temp_max = 105000;
551 break;
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100552 case 3: /* Automotive (-40 °C to 125 °C) */
Tim Harveya2291ba2015-11-19 06:49:40 -0800553 data->temp_grade = "Automotive";
554 data->temp_max = 125000;
555 break;
556 }
Philipp Zabel017e5142013-08-01 18:33:11 +0200557
558 /*
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100559 * Set the critical trip point at 5 °C under max
560 * Set the passive trip point at 10 °C under max (changeable via sysfs)
Philipp Zabel017e5142013-08-01 18:33:11 +0200561 */
Tim Harveya2291ba2015-11-19 06:49:40 -0800562 data->temp_critical = data->temp_max - (1000 * 5);
563 data->temp_passive = data->temp_max - (1000 * 10);
Leonard Crestezae621552017-07-14 17:11:08 +0300564}
565
566static int imx_init_from_tempmon_data(struct platform_device *pdev)
567{
568 struct regmap *map;
569 int ret;
570 u32 val;
571
572 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
573 "fsl,tempmon-data");
574 if (IS_ERR(map)) {
575 ret = PTR_ERR(map);
576 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
577 return ret;
578 }
579
580 ret = regmap_read(map, OCOTP_ANA1, &val);
581 if (ret) {
582 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
583 return ret;
584 }
585 ret = imx_init_calib(pdev, val);
586 if (ret)
587 return ret;
588
589 ret = regmap_read(map, OCOTP_MEM0, &val);
590 if (ret) {
591 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
592 return ret;
593 }
594 imx_init_temp_grade(pdev, val);
595
596 return 0;
597}
598
599static int imx_init_from_nvmem_cells(struct platform_device *pdev)
600{
601 int ret;
602 u32 val;
603
604 ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
605 if (ret)
606 return ret;
Jean-Christophe Duboisbe926ce2018-07-01 00:10:50 +0200607
608 ret = imx_init_calib(pdev, val);
609 if (ret)
610 return ret;
Leonard Crestezae621552017-07-14 17:11:08 +0300611
612 ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
613 if (ret)
614 return ret;
615 imx_init_temp_grade(pdev, val);
Philipp Zabel017e5142013-08-01 18:33:11 +0200616
Shawn Guoca3de462013-06-24 14:30:44 +0800617 return 0;
618}
619
Philipp Zabel37713a12013-08-01 18:33:12 +0200620static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
621{
622 struct imx_thermal_data *data = dev;
623
624 disable_irq_nosync(irq);
625 data->irq_enabled = false;
626
627 return IRQ_WAKE_THREAD;
628}
629
630static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
631{
632 struct imx_thermal_data *data = dev;
633
Sascha Hauer17e83512015-07-24 08:12:54 +0200634 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
Philipp Zabel37713a12013-08-01 18:33:12 +0200635 data->alarm_temp / 1000);
636
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700637 thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
Philipp Zabel37713a12013-08-01 18:33:12 +0200638
639 return IRQ_HANDLED;
640}
641
Anson Huang3c94f172014-08-06 15:12:09 +0800642static const struct of_device_id of_imx_thermal_match[] = {
643 { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
644 { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
Anson Huangf085f672018-03-02 09:59:30 +0800645 { .compatible = "fsl,imx7d-tempmon", .data = &thermal_imx7d_data, },
Anson Huang3c94f172014-08-06 15:12:09 +0800646 { /* end */ }
647};
648MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
649
Shawn Guoca3de462013-06-24 14:30:44 +0800650static int imx_thermal_probe(struct platform_device *pdev)
651{
652 struct imx_thermal_data *data;
Shawn Guoca3de462013-06-24 14:30:44 +0800653 struct regmap *map;
Philipp Zabel37713a12013-08-01 18:33:12 +0200654 int measure_freq;
Shawn Guoca3de462013-06-24 14:30:44 +0800655 int ret;
656
657 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
658 if (!data)
659 return -ENOMEM;
660
661 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
662 if (IS_ERR(map)) {
663 ret = PTR_ERR(map);
664 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
665 return ret;
666 }
667 data->tempmon = map;
668
Corentin LABBE829bc78a2016-08-16 10:51:38 +0200669 data->socdata = of_device_get_match_data(&pdev->dev);
Shailendra Verma8b051ec2017-01-30 10:34:58 +0530670 if (!data->socdata) {
671 dev_err(&pdev->dev, "no device match found\n");
672 return -ENODEV;
673 }
Anson Huang3c94f172014-08-06 15:12:09 +0800674
675 /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
676 if (data->socdata->version == TEMPMON_IMX6SX) {
Anson Huangf085f672018-03-02 09:59:30 +0800677 regmap_write(map, IMX6_MISC1 + REG_CLR,
678 IMX6_MISC1_IRQ_TEMPHIGH | IMX6_MISC1_IRQ_TEMPLOW
679 | IMX6_MISC1_IRQ_TEMPPANIC);
Anson Huang3c94f172014-08-06 15:12:09 +0800680 /*
681 * reset value of LOW ALARM is incorrect, set it to lowest
682 * value to avoid false trigger of low alarm.
683 */
Anson Huangf085f672018-03-02 09:59:30 +0800684 regmap_write(map, data->socdata->low_alarm_ctrl + REG_SET,
685 data->socdata->low_alarm_mask);
Anson Huang3c94f172014-08-06 15:12:09 +0800686 }
687
Philipp Zabel37713a12013-08-01 18:33:12 +0200688 data->irq = platform_get_irq(pdev, 0);
689 if (data->irq < 0)
690 return data->irq;
691
Shawn Guoca3de462013-06-24 14:30:44 +0800692 platform_set_drvdata(pdev, data);
693
Leonard Crestezae621552017-07-14 17:11:08 +0300694 if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
695 ret = imx_init_from_nvmem_cells(pdev);
696 if (ret == -EPROBE_DEFER)
697 return ret;
698 if (ret) {
699 dev_err(&pdev->dev, "failed to init from nvmem: %d\n",
700 ret);
701 return ret;
702 }
703 } else {
704 ret = imx_init_from_tempmon_data(pdev);
705 if (ret) {
706 dev_err(&pdev->dev, "failed to init from from fsl,tempmon-data\n");
707 return ret;
708 }
Shawn Guoca3de462013-06-24 14:30:44 +0800709 }
710
711 /* Make sure sensor is in known good state for measurements */
Anson Huangf085f672018-03-02 09:59:30 +0800712 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
713 data->socdata->power_down_mask);
714 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
715 data->socdata->measure_temp_mask);
716 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
717 data->socdata->measure_freq_mask);
718 if (data->socdata->version != TEMPMON_IMX7D)
719 regmap_write(map, IMX6_MISC0 + REG_SET,
720 IMX6_MISC0_REFTOP_SELBIASOFF);
721 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
722 data->socdata->power_down_mask);
Shawn Guoca3de462013-06-24 14:30:44 +0800723
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530724 data->policy = cpufreq_cpu_get(0);
725 if (!data->policy) {
726 pr_debug("%s: CPUFreq policy not found\n", __func__);
727 return -EPROBE_DEFER;
728 }
729
730 data->cdev = cpufreq_cooling_register(data->policy);
Shawn Guoca3de462013-06-24 14:30:44 +0800731 if (IS_ERR(data->cdev)) {
732 ret = PTR_ERR(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530733 dev_err(&pdev->dev,
734 "failed to register cpufreq cooling device: %d\n", ret);
735 cpufreq_cpu_put(data->policy);
Shawn Guoca3de462013-06-24 14:30:44 +0800736 return ret;
737 }
738
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100739 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
740 if (IS_ERR(data->thermal_clk)) {
741 ret = PTR_ERR(data->thermal_clk);
742 if (ret != -EPROBE_DEFER)
743 dev_err(&pdev->dev,
744 "failed to get thermal clk: %d\n", ret);
745 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530746 cpufreq_cpu_put(data->policy);
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100747 return ret;
748 }
749
750 /*
751 * Thermal sensor needs clk on to get correct value, normally
752 * we should enable its clk before taking measurement and disable
753 * clk after measurement is done, but if alarm function is enabled,
754 * hardware will auto measure the temperature periodically, so we
755 * need to keep the clk always on for alarm function.
756 */
757 ret = clk_prepare_enable(data->thermal_clk);
758 if (ret) {
759 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
760 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530761 cpufreq_cpu_put(data->policy);
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100762 return ret;
763 }
764
Shawn Guoca3de462013-06-24 14:30:44 +0800765 data->tz = thermal_zone_device_register("imx_thermal_zone",
Philipp Zabel017e5142013-08-01 18:33:11 +0200766 IMX_TRIP_NUM,
767 BIT(IMX_TRIP_PASSIVE), data,
Shawn Guoca3de462013-06-24 14:30:44 +0800768 &imx_tz_ops, NULL,
769 IMX_PASSIVE_DELAY,
770 IMX_POLLING_DELAY);
771 if (IS_ERR(data->tz)) {
772 ret = PTR_ERR(data->tz);
773 dev_err(&pdev->dev,
774 "failed to register thermal zone device %d\n", ret);
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100775 clk_disable_unprepare(data->thermal_clk);
Shawn Guoca3de462013-06-24 14:30:44 +0800776 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530777 cpufreq_cpu_put(data->policy);
Shawn Guoca3de462013-06-24 14:30:44 +0800778 return ret;
779 }
780
Tim Harveya2291ba2015-11-19 06:49:40 -0800781 dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
782 " critical:%dC passive:%dC\n", data->temp_grade,
783 data->temp_max / 1000, data->temp_critical / 1000,
784 data->temp_passive / 1000);
785
Philipp Zabel37713a12013-08-01 18:33:12 +0200786 /* Enable measurements at ~ 10 Hz */
Anson Huangf085f672018-03-02 09:59:30 +0800787 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
788 data->socdata->measure_freq_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200789 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
Anson Huangf085f672018-03-02 09:59:30 +0800790 regmap_write(map, data->socdata->measure_freq_ctrl + REG_SET,
791 measure_freq << data->socdata->measure_freq_shift);
Philipp Zabel37713a12013-08-01 18:33:12 +0200792 imx_set_alarm_temp(data, data->temp_passive);
Anson Huang3c94f172014-08-06 15:12:09 +0800793
794 if (data->socdata->version == TEMPMON_IMX6SX)
795 imx_set_panic_temp(data, data->temp_critical);
796
Anson Huangf085f672018-03-02 09:59:30 +0800797 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
798 data->socdata->power_down_mask);
799 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
800 data->socdata->measure_temp_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200801
Mikhail Lappocf1ba1d2018-02-02 16:17:46 -0200802 data->irq_enabled = true;
803 data->mode = THERMAL_DEVICE_ENABLED;
804
Bai Ping84866ee2015-09-14 19:09:51 +0800805 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
806 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
807 0, "imx_thermal", data);
808 if (ret < 0) {
809 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
810 clk_disable_unprepare(data->thermal_clk);
811 thermal_zone_device_unregister(data->tz);
812 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530813 cpufreq_cpu_put(data->policy);
Bai Ping84866ee2015-09-14 19:09:51 +0800814 return ret;
815 }
816
Shawn Guoca3de462013-06-24 14:30:44 +0800817 return 0;
818}
819
820static int imx_thermal_remove(struct platform_device *pdev)
821{
822 struct imx_thermal_data *data = platform_get_drvdata(pdev);
Philipp Zabel37713a12013-08-01 18:33:12 +0200823 struct regmap *map = data->tempmon;
824
825 /* Disable measurements */
Anson Huangf085f672018-03-02 09:59:30 +0800826 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
827 data->socdata->power_down_mask);
Anson Huang329fe7b2013-12-23 15:49:22 -0500828 if (!IS_ERR(data->thermal_clk))
829 clk_disable_unprepare(data->thermal_clk);
Shawn Guoca3de462013-06-24 14:30:44 +0800830
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);
Shawn Guoca3de462013-06-24 14:30:44 +0800834
835 return 0;
836}
837
838#ifdef CONFIG_PM_SLEEP
839static int imx_thermal_suspend(struct device *dev)
840{
841 struct imx_thermal_data *data = dev_get_drvdata(dev);
842 struct regmap *map = data->tempmon;
Shawn Guoca3de462013-06-24 14:30:44 +0800843
Anson Huangb46cce52013-12-24 09:43:24 -0500844 /*
845 * Need to disable thermal sensor, otherwise, when thermal core
846 * try to get temperature before thermal sensor resume, a wrong
847 * temperature will be read as the thermal sensor is powered
848 * down.
849 */
Anson Huangf085f672018-03-02 09:59:30 +0800850 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
851 data->socdata->measure_temp_mask);
852 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
853 data->socdata->power_down_mask);
Anson Huangb46cce52013-12-24 09:43:24 -0500854 data->mode = THERMAL_DEVICE_DISABLED;
Anson Huangd26eef82015-01-06 18:50:22 +0800855 clk_disable_unprepare(data->thermal_clk);
Shawn Guoca3de462013-06-24 14:30:44 +0800856
857 return 0;
858}
859
860static int imx_thermal_resume(struct device *dev)
861{
Anson Huangb46cce52013-12-24 09:43:24 -0500862 struct imx_thermal_data *data = dev_get_drvdata(dev);
863 struct regmap *map = data->tempmon;
Arvind Yadave3bdc8d2017-06-06 15:12:37 +0530864 int ret;
Anson Huangb46cce52013-12-24 09:43:24 -0500865
Arvind Yadave3bdc8d2017-06-06 15:12:37 +0530866 ret = clk_prepare_enable(data->thermal_clk);
867 if (ret)
868 return ret;
Anson Huangb46cce52013-12-24 09:43:24 -0500869 /* Enabled thermal sensor after resume */
Anson Huangf085f672018-03-02 09:59:30 +0800870 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
871 data->socdata->power_down_mask);
872 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
873 data->socdata->measure_temp_mask);
Anson Huangb46cce52013-12-24 09:43:24 -0500874 data->mode = THERMAL_DEVICE_ENABLED;
875
Shawn Guoca3de462013-06-24 14:30:44 +0800876 return 0;
877}
878#endif
879
880static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
881 imx_thermal_suspend, imx_thermal_resume);
882
Shawn Guoca3de462013-06-24 14:30:44 +0800883static struct platform_driver imx_thermal = {
884 .driver = {
885 .name = "imx_thermal",
Shawn Guoca3de462013-06-24 14:30:44 +0800886 .pm = &imx_thermal_pm_ops,
887 .of_match_table = of_imx_thermal_match,
888 },
889 .probe = imx_thermal_probe,
890 .remove = imx_thermal_remove,
891};
892module_platform_driver(imx_thermal);
893
894MODULE_AUTHOR("Freescale Semiconductor, Inc.");
895MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
896MODULE_LICENSE("GPL v2");
897MODULE_ALIAS("platform:imx-thermal");