blob: 9700ae39feb7a2b76fcaf913c48a23f9b7451b6b [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>
Philipp Zabel37713a12013-08-01 18:33:12 +02009#include <linux/interrupt.h>
Shawn Guoca3de462013-06-24 14:30:44 +080010#include <linux/io.h>
Shawn Guoca3de462013-06-24 14:30:44 +080011#include <linux/mfd/syscon.h>
12#include <linux/module.h>
13#include <linux/of.h>
Anson Huang3c94f172014-08-06 15:12:09 +080014#include <linux/of_device.h>
Shawn Guoca3de462013-06-24 14:30:44 +080015#include <linux/regmap.h>
Shawn Guoca3de462013-06-24 14:30:44 +080016#include <linux/thermal.h>
Leonard Crestezae621552017-07-14 17:11:08 +030017#include <linux/nvmem-consumer.h>
Shawn Guoca3de462013-06-24 14:30:44 +080018
19#define REG_SET 0x4
20#define REG_CLR 0x8
21#define REG_TOG 0xc
22
Anson Huangf085f672018-03-02 09:59:30 +080023/* i.MX6 specific */
24#define IMX6_MISC0 0x0150
25#define IMX6_MISC0_REFTOP_SELBIASOFF (1 << 3)
26#define IMX6_MISC1 0x0160
27#define IMX6_MISC1_IRQ_TEMPHIGH (1 << 29)
Anson Huang3c94f172014-08-06 15:12:09 +080028/* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
Anson Huangf085f672018-03-02 09:59:30 +080029#define IMX6_MISC1_IRQ_TEMPLOW (1 << 28)
30#define IMX6_MISC1_IRQ_TEMPPANIC (1 << 27)
Shawn Guoca3de462013-06-24 14:30:44 +080031
Anson Huangf085f672018-03-02 09:59:30 +080032#define IMX6_TEMPSENSE0 0x0180
33#define IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT 20
34#define IMX6_TEMPSENSE0_ALARM_VALUE_MASK (0xfff << 20)
35#define IMX6_TEMPSENSE0_TEMP_CNT_SHIFT 8
36#define IMX6_TEMPSENSE0_TEMP_CNT_MASK (0xfff << 8)
37#define IMX6_TEMPSENSE0_FINISHED (1 << 2)
38#define IMX6_TEMPSENSE0_MEASURE_TEMP (1 << 1)
39#define IMX6_TEMPSENSE0_POWER_DOWN (1 << 0)
Shawn Guoca3de462013-06-24 14:30:44 +080040
Anson Huangf085f672018-03-02 09:59:30 +080041#define IMX6_TEMPSENSE1 0x0190
42#define IMX6_TEMPSENSE1_MEASURE_FREQ 0xffff
43#define IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT 0
Shawn Guoca3de462013-06-24 14:30:44 +080044
Tim Harveya2291ba2015-11-19 06:49:40 -080045#define OCOTP_MEM0 0x0480
Shawn Guoca3de462013-06-24 14:30:44 +080046#define OCOTP_ANA1 0x04e0
47
Anson Huangf085f672018-03-02 09:59:30 +080048/* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
49#define IMX6_TEMPSENSE2 0x0290
50#define IMX6_TEMPSENSE2_LOW_VALUE_SHIFT 0
51#define IMX6_TEMPSENSE2_LOW_VALUE_MASK 0xfff
52#define IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT 16
53#define IMX6_TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
54
55/* i.MX7 specific */
56#define IMX7_ANADIG_DIGPROG 0x800
57#define IMX7_TEMPSENSE0 0x300
58#define IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT 18
59#define IMX7_TEMPSENSE0_PANIC_ALARM_MASK (0x1ff << 18)
60#define IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT 9
61#define IMX7_TEMPSENSE0_HIGH_ALARM_MASK (0x1ff << 9)
62#define IMX7_TEMPSENSE0_LOW_ALARM_SHIFT 0
63#define IMX7_TEMPSENSE0_LOW_ALARM_MASK 0x1ff
64
65#define IMX7_TEMPSENSE1 0x310
66#define IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT 16
67#define IMX7_TEMPSENSE1_MEASURE_FREQ_MASK (0xffff << 16)
68#define IMX7_TEMPSENSE1_FINISHED (1 << 11)
69#define IMX7_TEMPSENSE1_MEASURE_TEMP (1 << 10)
70#define IMX7_TEMPSENSE1_POWER_DOWN (1 << 9)
71#define IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT 0
72#define IMX7_TEMPSENSE1_TEMP_VALUE_MASK 0x1ff
73
Shawn Guoca3de462013-06-24 14:30:44 +080074/* The driver supports 1 passive trip point and 1 critical trip point */
75enum imx_thermal_trip {
76 IMX_TRIP_PASSIVE,
77 IMX_TRIP_CRITICAL,
78 IMX_TRIP_NUM,
79};
80
Shawn Guoca3de462013-06-24 14:30:44 +080081#define IMX_POLLING_DELAY 2000 /* millisecond */
82#define IMX_PASSIVE_DELAY 1000
83
Anson Huang3c94f172014-08-06 15:12:09 +080084#define TEMPMON_IMX6Q 1
85#define TEMPMON_IMX6SX 2
Anson Huangf085f672018-03-02 09:59:30 +080086#define TEMPMON_IMX7D 3
Anson Huang3c94f172014-08-06 15:12:09 +080087
88struct thermal_soc_data {
89 u32 version;
Anson Huangf085f672018-03-02 09:59:30 +080090
91 u32 sensor_ctrl;
92 u32 power_down_mask;
93 u32 measure_temp_mask;
94
95 u32 measure_freq_ctrl;
96 u32 measure_freq_mask;
97 u32 measure_freq_shift;
98
99 u32 temp_data;
100 u32 temp_value_mask;
101 u32 temp_value_shift;
102 u32 temp_valid_mask;
103
104 u32 panic_alarm_ctrl;
105 u32 panic_alarm_mask;
106 u32 panic_alarm_shift;
107
108 u32 high_alarm_ctrl;
109 u32 high_alarm_mask;
110 u32 high_alarm_shift;
111
112 u32 low_alarm_ctrl;
113 u32 low_alarm_mask;
114 u32 low_alarm_shift;
Anson Huang3c94f172014-08-06 15:12:09 +0800115};
116
117static struct thermal_soc_data thermal_imx6q_data = {
118 .version = TEMPMON_IMX6Q,
Anson Huangf085f672018-03-02 09:59:30 +0800119
120 .sensor_ctrl = IMX6_TEMPSENSE0,
121 .power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
122 .measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
123
124 .measure_freq_ctrl = IMX6_TEMPSENSE1,
125 .measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
126 .measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
127
128 .temp_data = IMX6_TEMPSENSE0,
129 .temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
130 .temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
131 .temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
132
133 .high_alarm_ctrl = IMX6_TEMPSENSE0,
134 .high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
135 .high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
Anson Huang3c94f172014-08-06 15:12:09 +0800136};
137
138static struct thermal_soc_data thermal_imx6sx_data = {
139 .version = TEMPMON_IMX6SX,
Anson Huangf085f672018-03-02 09:59:30 +0800140
141 .sensor_ctrl = IMX6_TEMPSENSE0,
142 .power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
143 .measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
144
145 .measure_freq_ctrl = IMX6_TEMPSENSE1,
146 .measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
147 .measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
148
149 .temp_data = IMX6_TEMPSENSE0,
150 .temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
151 .temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
152 .temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
153
154 .high_alarm_ctrl = IMX6_TEMPSENSE0,
155 .high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
156 .high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
157
158 .panic_alarm_ctrl = IMX6_TEMPSENSE2,
159 .panic_alarm_mask = IMX6_TEMPSENSE2_PANIC_VALUE_MASK,
160 .panic_alarm_shift = IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT,
161
162 .low_alarm_ctrl = IMX6_TEMPSENSE2,
163 .low_alarm_mask = IMX6_TEMPSENSE2_LOW_VALUE_MASK,
164 .low_alarm_shift = IMX6_TEMPSENSE2_LOW_VALUE_SHIFT,
165};
166
167static struct thermal_soc_data thermal_imx7d_data = {
168 .version = TEMPMON_IMX7D,
169
170 .sensor_ctrl = IMX7_TEMPSENSE1,
171 .power_down_mask = IMX7_TEMPSENSE1_POWER_DOWN,
172 .measure_temp_mask = IMX7_TEMPSENSE1_MEASURE_TEMP,
173
174 .measure_freq_ctrl = IMX7_TEMPSENSE1,
175 .measure_freq_shift = IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT,
176 .measure_freq_mask = IMX7_TEMPSENSE1_MEASURE_FREQ_MASK,
177
178 .temp_data = IMX7_TEMPSENSE1,
179 .temp_value_mask = IMX7_TEMPSENSE1_TEMP_VALUE_MASK,
180 .temp_value_shift = IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT,
181 .temp_valid_mask = IMX7_TEMPSENSE1_FINISHED,
182
183 .panic_alarm_ctrl = IMX7_TEMPSENSE1,
184 .panic_alarm_mask = IMX7_TEMPSENSE0_PANIC_ALARM_MASK,
185 .panic_alarm_shift = IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT,
186
187 .high_alarm_ctrl = IMX7_TEMPSENSE0,
188 .high_alarm_mask = IMX7_TEMPSENSE0_HIGH_ALARM_MASK,
189 .high_alarm_shift = IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT,
190
191 .low_alarm_ctrl = IMX7_TEMPSENSE0,
192 .low_alarm_mask = IMX7_TEMPSENSE0_LOW_ALARM_MASK,
193 .low_alarm_shift = IMX7_TEMPSENSE0_LOW_ALARM_SHIFT,
Anson Huang3c94f172014-08-06 15:12:09 +0800194};
195
Shawn Guoca3de462013-06-24 14:30:44 +0800196struct imx_thermal_data {
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530197 struct cpufreq_policy *policy;
Shawn Guoca3de462013-06-24 14:30:44 +0800198 struct thermal_zone_device *tz;
199 struct thermal_cooling_device *cdev;
Shawn Guoca3de462013-06-24 14:30:44 +0800200 struct regmap *tempmon;
Leonard Crestezae621552017-07-14 17:11:08 +0300201 u32 c1, c2; /* See formula in imx_init_calib() */
Sascha Hauer17e83512015-07-24 08:12:54 +0200202 int temp_passive;
203 int temp_critical;
Tim Harveya2291ba2015-11-19 06:49:40 -0800204 int temp_max;
Sascha Hauer17e83512015-07-24 08:12:54 +0200205 int alarm_temp;
206 int last_temp;
Philipp Zabel37713a12013-08-01 18:33:12 +0200207 bool irq_enabled;
208 int irq;
Anson Huang329fe7b2013-12-23 15:49:22 -0500209 struct clk *thermal_clk;
Anson Huang3c94f172014-08-06 15:12:09 +0800210 const struct thermal_soc_data *socdata;
Tim Harveya2291ba2015-11-19 06:49:40 -0800211 const char *temp_grade;
Shawn Guoca3de462013-06-24 14:30:44 +0800212};
213
Anson Huang3c94f172014-08-06 15:12:09 +0800214static void imx_set_panic_temp(struct imx_thermal_data *data,
Sascha Hauer17e83512015-07-24 08:12:54 +0200215 int panic_temp)
Anson Huang3c94f172014-08-06 15:12:09 +0800216{
Anson Huangf085f672018-03-02 09:59:30 +0800217 const struct thermal_soc_data *soc_data = data->socdata;
Anson Huang3c94f172014-08-06 15:12:09 +0800218 struct regmap *map = data->tempmon;
219 int critical_value;
220
221 critical_value = (data->c2 - panic_temp) / data->c1;
Anson Huangf085f672018-03-02 09:59:30 +0800222
223 regmap_write(map, soc_data->panic_alarm_ctrl + REG_CLR,
224 soc_data->panic_alarm_mask);
225 regmap_write(map, soc_data->panic_alarm_ctrl + REG_SET,
226 critical_value << soc_data->panic_alarm_shift);
Anson Huang3c94f172014-08-06 15:12:09 +0800227}
228
Philipp Zabel37713a12013-08-01 18:33:12 +0200229static void imx_set_alarm_temp(struct imx_thermal_data *data,
Sascha Hauer17e83512015-07-24 08:12:54 +0200230 int alarm_temp)
Philipp Zabel37713a12013-08-01 18:33:12 +0200231{
232 struct regmap *map = data->tempmon;
Anson Huangf085f672018-03-02 09:59:30 +0800233 const struct thermal_soc_data *soc_data = data->socdata;
Philipp Zabel37713a12013-08-01 18:33:12 +0200234 int alarm_value;
235
236 data->alarm_temp = alarm_temp;
Anson Huangf085f672018-03-02 09:59:30 +0800237
238 if (data->socdata->version == TEMPMON_IMX7D)
239 alarm_value = alarm_temp / 1000 + data->c1 - 25;
240 else
241 alarm_value = (data->c2 - alarm_temp) / data->c1;
242
243 regmap_write(map, soc_data->high_alarm_ctrl + REG_CLR,
244 soc_data->high_alarm_mask);
245 regmap_write(map, soc_data->high_alarm_ctrl + REG_SET,
246 alarm_value << soc_data->high_alarm_shift);
Philipp Zabel37713a12013-08-01 18:33:12 +0200247}
248
Sascha Hauer17e83512015-07-24 08:12:54 +0200249static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800250{
251 struct imx_thermal_data *data = tz->devdata;
Anson Huangf085f672018-03-02 09:59:30 +0800252 const struct thermal_soc_data *soc_data = data->socdata;
Shawn Guoca3de462013-06-24 14:30:44 +0800253 struct regmap *map = data->tempmon;
Shawn Guoca3de462013-06-24 14:30:44 +0800254 unsigned int n_meas;
Philipp Zabel37713a12013-08-01 18:33:12 +0200255 bool wait;
Shawn Guoca3de462013-06-24 14:30:44 +0800256 u32 val;
257
Andrzej Pietrasiewicz7f4957b2020-06-29 14:29:21 +0200258 if (thermal_zone_device_is_enabled(tz)) {
Philipp Zabel37713a12013-08-01 18:33:12 +0200259 /* Check if a measurement is currently in progress */
Anson Huangf085f672018-03-02 09:59:30 +0800260 regmap_read(map, soc_data->temp_data, &val);
261 wait = !(val & soc_data->temp_valid_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200262 } else {
263 /*
264 * Every time we measure the temperature, we will power on the
265 * temperature sensor, enable measurements, take a reading,
266 * disable measurements, power off the temperature sensor.
267 */
Anson Huangf085f672018-03-02 09:59:30 +0800268 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
269 soc_data->power_down_mask);
270 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
271 soc_data->measure_temp_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200272
273 wait = true;
274 }
Shawn Guoca3de462013-06-24 14:30:44 +0800275
276 /*
277 * According to the temp sensor designers, it may require up to ~17us
278 * to complete a measurement.
279 */
Philipp Zabel37713a12013-08-01 18:33:12 +0200280 if (wait)
281 usleep_range(20, 50);
Shawn Guoca3de462013-06-24 14:30:44 +0800282
Anson Huangf085f672018-03-02 09:59:30 +0800283 regmap_read(map, soc_data->temp_data, &val);
Philipp Zabel37713a12013-08-01 18:33:12 +0200284
Andrzej Pietrasiewicz7f4957b2020-06-29 14:29:21 +0200285 if (!thermal_zone_device_is_enabled(tz)) {
Anson Huangf085f672018-03-02 09:59:30 +0800286 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
287 soc_data->measure_temp_mask);
288 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
289 soc_data->power_down_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200290 }
Shawn Guoca3de462013-06-24 14:30:44 +0800291
Anson Huangf085f672018-03-02 09:59:30 +0800292 if ((val & soc_data->temp_valid_mask) == 0) {
Shawn Guoca3de462013-06-24 14:30:44 +0800293 dev_dbg(&tz->device, "temp measurement never finished\n");
294 return -EAGAIN;
295 }
296
Anson Huangf085f672018-03-02 09:59:30 +0800297 n_meas = (val & soc_data->temp_value_mask)
298 >> soc_data->temp_value_shift;
Shawn Guoca3de462013-06-24 14:30:44 +0800299
Leonard Crestezae621552017-07-14 17:11:08 +0300300 /* See imx_init_calib() for formula derivation */
Anson Huangf085f672018-03-02 09:59:30 +0800301 if (data->socdata->version == TEMPMON_IMX7D)
302 *temp = (n_meas - data->c1 + 25) * 1000;
303 else
304 *temp = data->c2 - n_meas * data->c1;
Shawn Guoca3de462013-06-24 14:30:44 +0800305
Anson Huang3c94f172014-08-06 15:12:09 +0800306 /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
307 if (data->socdata->version == TEMPMON_IMX6Q) {
308 if (data->alarm_temp == data->temp_passive &&
309 *temp >= data->temp_passive)
310 imx_set_alarm_temp(data, data->temp_critical);
311 if (data->alarm_temp == data->temp_critical &&
312 *temp < data->temp_passive) {
313 imx_set_alarm_temp(data, data->temp_passive);
Sascha Hauer17e83512015-07-24 08:12:54 +0200314 dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
Anson Huang3c94f172014-08-06 15:12:09 +0800315 data->alarm_temp / 1000);
316 }
Philipp Zabel37713a12013-08-01 18:33:12 +0200317 }
318
319 if (*temp != data->last_temp) {
Sascha Hauer17e83512015-07-24 08:12:54 +0200320 dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
Philipp Zabel37713a12013-08-01 18:33:12 +0200321 data->last_temp = *temp;
322 }
323
324 /* Reenable alarm IRQ if temperature below alarm temperature */
325 if (!data->irq_enabled && *temp < data->alarm_temp) {
326 data->irq_enabled = true;
327 enable_irq(data->irq);
Shawn Guoca3de462013-06-24 14:30:44 +0800328 }
329
330 return 0;
331}
332
Andrzej Pietrasiewiczf5e50bf42020-06-29 14:29:25 +0200333static int imx_change_mode(struct thermal_zone_device *tz,
334 enum thermal_device_mode mode)
Shawn Guoca3de462013-06-24 14:30:44 +0800335{
336 struct imx_thermal_data *data = tz->devdata;
Philipp Zabel37713a12013-08-01 18:33:12 +0200337 struct regmap *map = data->tempmon;
Anson Huangf085f672018-03-02 09:59:30 +0800338 const struct thermal_soc_data *soc_data = data->socdata;
Shawn Guoca3de462013-06-24 14:30:44 +0800339
340 if (mode == THERMAL_DEVICE_ENABLED) {
Anson Huangf085f672018-03-02 09:59:30 +0800341 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
342 soc_data->power_down_mask);
343 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
344 soc_data->measure_temp_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200345
346 if (!data->irq_enabled) {
347 data->irq_enabled = true;
348 enable_irq(data->irq);
349 }
Shawn Guoca3de462013-06-24 14:30:44 +0800350 } else {
Anson Huangf085f672018-03-02 09:59:30 +0800351 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
352 soc_data->measure_temp_mask);
353 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
354 soc_data->power_down_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200355
Philipp Zabel37713a12013-08-01 18:33:12 +0200356 if (data->irq_enabled) {
357 disable_irq(data->irq);
358 data->irq_enabled = false;
359 }
Shawn Guoca3de462013-06-24 14:30:44 +0800360 }
361
Shawn Guoca3de462013-06-24 14:30:44 +0800362 return 0;
363}
364
365static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
366 enum thermal_trip_type *type)
367{
368 *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
369 THERMAL_TRIP_CRITICAL;
370 return 0;
371}
372
Sascha Hauer17e83512015-07-24 08:12:54 +0200373static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800374{
Philipp Zabel017e5142013-08-01 18:33:11 +0200375 struct imx_thermal_data *data = tz->devdata;
376
377 *temp = data->temp_critical;
Shawn Guoca3de462013-06-24 14:30:44 +0800378 return 0;
379}
380
381static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200382 int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800383{
Philipp Zabel017e5142013-08-01 18:33:11 +0200384 struct imx_thermal_data *data = tz->devdata;
385
386 *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
387 data->temp_critical;
388 return 0;
389}
390
391static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200392 int temp)
Philipp Zabel017e5142013-08-01 18:33:11 +0200393{
394 struct imx_thermal_data *data = tz->devdata;
395
Tim Harveya2291ba2015-11-19 06:49:40 -0800396 /* do not allow changing critical threshold */
Philipp Zabel017e5142013-08-01 18:33:11 +0200397 if (trip == IMX_TRIP_CRITICAL)
398 return -EPERM;
399
Tim Harveya2291ba2015-11-19 06:49:40 -0800400 /* do not allow passive to be set higher than critical */
401 if (temp < 0 || temp > data->temp_critical)
Philipp Zabel017e5142013-08-01 18:33:11 +0200402 return -EINVAL;
403
404 data->temp_passive = temp;
405
Philipp Zabel37713a12013-08-01 18:33:12 +0200406 imx_set_alarm_temp(data, temp);
407
Shawn Guoca3de462013-06-24 14:30:44 +0800408 return 0;
409}
410
411static int imx_bind(struct thermal_zone_device *tz,
412 struct thermal_cooling_device *cdev)
413{
414 int ret;
415
416 ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
417 THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000418 THERMAL_NO_LIMIT,
419 THERMAL_WEIGHT_DEFAULT);
Shawn Guoca3de462013-06-24 14:30:44 +0800420 if (ret) {
421 dev_err(&tz->device,
422 "binding zone %s with cdev %s failed:%d\n",
423 tz->type, cdev->type, ret);
424 return ret;
425 }
426
427 return 0;
428}
429
430static int imx_unbind(struct thermal_zone_device *tz,
431 struct thermal_cooling_device *cdev)
432{
433 int ret;
434
435 ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
436 if (ret) {
437 dev_err(&tz->device,
438 "unbinding zone %s with cdev %s failed:%d\n",
439 tz->type, cdev->type, ret);
440 return ret;
441 }
442
443 return 0;
444}
445
Eduardo Valentincbb07bb2014-01-06 08:54:34 -0400446static struct thermal_zone_device_ops imx_tz_ops = {
Shawn Guoca3de462013-06-24 14:30:44 +0800447 .bind = imx_bind,
448 .unbind = imx_unbind,
449 .get_temp = imx_get_temp,
Andrzej Pietrasiewiczf5e50bf42020-06-29 14:29:25 +0200450 .change_mode = imx_change_mode,
Shawn Guoca3de462013-06-24 14:30:44 +0800451 .get_trip_type = imx_get_trip_type,
452 .get_trip_temp = imx_get_trip_temp,
453 .get_crit_temp = imx_get_crit_temp,
Philipp Zabel017e5142013-08-01 18:33:11 +0200454 .set_trip_temp = imx_set_trip_temp,
Shawn Guoca3de462013-06-24 14:30:44 +0800455};
456
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100457static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
Shawn Guoca3de462013-06-24 14:30:44 +0800458{
459 struct imx_thermal_data *data = platform_get_drvdata(pdev);
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100460 int n1;
Anson Huang749e8be2014-02-12 18:06:35 +0800461 u64 temp64;
Shawn Guoca3de462013-06-24 14:30:44 +0800462
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100463 if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
Shawn Guoca3de462013-06-24 14:30:44 +0800464 dev_err(&pdev->dev, "invalid sensor calibration data\n");
465 return -EINVAL;
466 }
467
468 /*
Anson Huangf085f672018-03-02 09:59:30 +0800469 * On i.MX7D, we only use the calibration data at 25C to get the temp,
470 * Tmeas = ( Nmeas - n1) + 25; n1 is the fuse value for 25C.
471 */
472 if (data->socdata->version == TEMPMON_IMX7D) {
473 data->c1 = (ocotp_ana1 >> 9) & 0x1ff;
474 return 0;
475 }
476
477 /*
Uwe Kleine-Königc5bbdb42017-11-30 10:17:36 +0100478 * The sensor is calibrated at 25 °C (aka T1) and the value measured
479 * (aka N1) at this temperature is provided in bits [31:20] in the
480 * i.MX's OCOTP value ANA1.
481 * To find the actual temperature T, the following formula has to be used
482 * when reading value n from the sensor:
483 *
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100484 * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C
485 * = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C
486 * = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C
Uwe Kleine-Königc5bbdb42017-11-30 10:17:36 +0100487 * = c2 - c1 * N
488 *
489 * with
490 *
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100491 * T1' = 28.580661 °C
492 * c1 = 1 / (0.0015423 * N1 - 0.4297157) °C
493 * c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C
494 * = T1' + N1 * c1
Shawn Guoca3de462013-06-24 14:30:44 +0800495 */
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100496 n1 = ocotp_ana1 >> 20;
Shawn Guoca3de462013-06-24 14:30:44 +0800497
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100498 temp64 = 10000000; /* use 10^7 as fixed point constant for values in formula */
Uwe Kleine-Königc5bbdb42017-11-30 10:17:36 +0100499 temp64 *= 1000; /* to get result in °mC */
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100500 do_div(temp64, 15423 * n1 - 4148468);
Anson Huang749e8be2014-02-12 18:06:35 +0800501 data->c1 = temp64;
Uwe Kleine-König4e5f61c2017-11-30 10:17:38 +0100502 data->c2 = n1 * data->c1 + 28581;
Shawn Guoca3de462013-06-24 14:30:44 +0800503
Leonard Crestezae621552017-07-14 17:11:08 +0300504 return 0;
505}
506
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100507static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
Leonard Crestezae621552017-07-14 17:11:08 +0300508{
509 struct imx_thermal_data *data = platform_get_drvdata(pdev);
Tim Harveya2291ba2015-11-19 06:49:40 -0800510
511 /* The maximum die temp is specified by the Temperature Grade */
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100512 switch ((ocotp_mem0 >> 6) & 0x3) {
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100513 case 0: /* Commercial (0 to 95 °C) */
Tim Harveya2291ba2015-11-19 06:49:40 -0800514 data->temp_grade = "Commercial";
515 data->temp_max = 95000;
516 break;
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100517 case 1: /* Extended Commercial (-20 °C to 105 °C) */
Tim Harveya2291ba2015-11-19 06:49:40 -0800518 data->temp_grade = "Extended Commercial";
519 data->temp_max = 105000;
520 break;
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100521 case 2: /* Industrial (-40 °C to 105 °C) */
Tim Harveya2291ba2015-11-19 06:49:40 -0800522 data->temp_grade = "Industrial";
523 data->temp_max = 105000;
524 break;
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100525 case 3: /* Automotive (-40 °C to 125 °C) */
Tim Harveya2291ba2015-11-19 06:49:40 -0800526 data->temp_grade = "Automotive";
527 data->temp_max = 125000;
528 break;
529 }
Philipp Zabel017e5142013-08-01 18:33:11 +0200530
531 /*
Uwe Kleine-König339d7492017-11-30 10:17:37 +0100532 * Set the critical trip point at 5 °C under max
533 * Set the passive trip point at 10 °C under max (changeable via sysfs)
Philipp Zabel017e5142013-08-01 18:33:11 +0200534 */
Tim Harveya2291ba2015-11-19 06:49:40 -0800535 data->temp_critical = data->temp_max - (1000 * 5);
536 data->temp_passive = data->temp_max - (1000 * 10);
Leonard Crestezae621552017-07-14 17:11:08 +0300537}
538
539static int imx_init_from_tempmon_data(struct platform_device *pdev)
540{
541 struct regmap *map;
542 int ret;
543 u32 val;
544
545 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
546 "fsl,tempmon-data");
547 if (IS_ERR(map)) {
548 ret = PTR_ERR(map);
549 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
550 return ret;
551 }
552
553 ret = regmap_read(map, OCOTP_ANA1, &val);
554 if (ret) {
555 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
556 return ret;
557 }
558 ret = imx_init_calib(pdev, val);
559 if (ret)
560 return ret;
561
562 ret = regmap_read(map, OCOTP_MEM0, &val);
563 if (ret) {
564 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
565 return ret;
566 }
567 imx_init_temp_grade(pdev, val);
568
569 return 0;
570}
571
572static int imx_init_from_nvmem_cells(struct platform_device *pdev)
573{
574 int ret;
575 u32 val;
576
577 ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
578 if (ret)
579 return ret;
Jean-Christophe Duboisbe926ce2018-07-01 00:10:50 +0200580
581 ret = imx_init_calib(pdev, val);
582 if (ret)
583 return ret;
Leonard Crestezae621552017-07-14 17:11:08 +0300584
585 ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
586 if (ret)
587 return ret;
588 imx_init_temp_grade(pdev, val);
Philipp Zabel017e5142013-08-01 18:33:11 +0200589
Shawn Guoca3de462013-06-24 14:30:44 +0800590 return 0;
591}
592
Philipp Zabel37713a12013-08-01 18:33:12 +0200593static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
594{
595 struct imx_thermal_data *data = dev;
596
597 disable_irq_nosync(irq);
598 data->irq_enabled = false;
599
600 return IRQ_WAKE_THREAD;
601}
602
603static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
604{
605 struct imx_thermal_data *data = dev;
606
Sascha Hauer17e83512015-07-24 08:12:54 +0200607 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
Philipp Zabel37713a12013-08-01 18:33:12 +0200608 data->alarm_temp / 1000);
609
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700610 thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
Philipp Zabel37713a12013-08-01 18:33:12 +0200611
612 return IRQ_HANDLED;
613}
614
Anson Huang3c94f172014-08-06 15:12:09 +0800615static const struct of_device_id of_imx_thermal_match[] = {
616 { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
617 { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
Anson Huangf085f672018-03-02 09:59:30 +0800618 { .compatible = "fsl,imx7d-tempmon", .data = &thermal_imx7d_data, },
Anson Huang3c94f172014-08-06 15:12:09 +0800619 { /* end */ }
620};
621MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
622
Anson Huangc589c562018-11-21 05:49:36 +0000623#ifdef CONFIG_CPU_FREQ
Bastian Stendera1d00152018-06-08 11:06:39 +0200624/*
625 * Create cooling device in case no #cooling-cells property is available in
626 * CPU node
627 */
628static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
629{
Anson Huangc589c562018-11-21 05:49:36 +0000630 struct device_node *np;
Bastian Stendera1d00152018-06-08 11:06:39 +0200631 int ret;
632
Anson Huangc589c562018-11-21 05:49:36 +0000633 data->policy = cpufreq_cpu_get(0);
634 if (!data->policy) {
635 pr_debug("%s: CPUFreq policy not found\n", __func__);
636 return -EPROBE_DEFER;
637 }
638
639 np = of_get_cpu_node(data->policy->cpu, NULL);
640
Bastian Stendera1d00152018-06-08 11:06:39 +0200641 if (!np || !of_find_property(np, "#cooling-cells", NULL)) {
642 data->cdev = cpufreq_cooling_register(data->policy);
643 if (IS_ERR(data->cdev)) {
644 ret = PTR_ERR(data->cdev);
645 cpufreq_cpu_put(data->policy);
646 return ret;
647 }
648 }
649
650 return 0;
651}
652
Anson Huangc589c562018-11-21 05:49:36 +0000653static void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data)
654{
655 cpufreq_cooling_unregister(data->cdev);
656 cpufreq_cpu_put(data->policy);
657}
658
659#else
660
661static inline int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
662{
663 return 0;
664}
665
666static inline void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data)
667{
668}
669#endif
670
Shawn Guoca3de462013-06-24 14:30:44 +0800671static int imx_thermal_probe(struct platform_device *pdev)
672{
673 struct imx_thermal_data *data;
Shawn Guoca3de462013-06-24 14:30:44 +0800674 struct regmap *map;
Philipp Zabel37713a12013-08-01 18:33:12 +0200675 int measure_freq;
Shawn Guoca3de462013-06-24 14:30:44 +0800676 int ret;
677
678 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
679 if (!data)
680 return -ENOMEM;
681
682 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
683 if (IS_ERR(map)) {
684 ret = PTR_ERR(map);
685 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
686 return ret;
687 }
688 data->tempmon = map;
689
Corentin LABBE829bc78a2016-08-16 10:51:38 +0200690 data->socdata = of_device_get_match_data(&pdev->dev);
Shailendra Verma8b051ec2017-01-30 10:34:58 +0530691 if (!data->socdata) {
692 dev_err(&pdev->dev, "no device match found\n");
693 return -ENODEV;
694 }
Anson Huang3c94f172014-08-06 15:12:09 +0800695
696 /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
697 if (data->socdata->version == TEMPMON_IMX6SX) {
Anson Huangf085f672018-03-02 09:59:30 +0800698 regmap_write(map, IMX6_MISC1 + REG_CLR,
699 IMX6_MISC1_IRQ_TEMPHIGH | IMX6_MISC1_IRQ_TEMPLOW
700 | IMX6_MISC1_IRQ_TEMPPANIC);
Anson Huang3c94f172014-08-06 15:12:09 +0800701 /*
702 * reset value of LOW ALARM is incorrect, set it to lowest
703 * value to avoid false trigger of low alarm.
704 */
Anson Huangf085f672018-03-02 09:59:30 +0800705 regmap_write(map, data->socdata->low_alarm_ctrl + REG_SET,
706 data->socdata->low_alarm_mask);
Anson Huang3c94f172014-08-06 15:12:09 +0800707 }
708
Philipp Zabel37713a12013-08-01 18:33:12 +0200709 data->irq = platform_get_irq(pdev, 0);
710 if (data->irq < 0)
711 return data->irq;
712
Shawn Guoca3de462013-06-24 14:30:44 +0800713 platform_set_drvdata(pdev, data);
714
Leonard Crestezae621552017-07-14 17:11:08 +0300715 if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
716 ret = imx_init_from_nvmem_cells(pdev);
Leonard Crestezae621552017-07-14 17:11:08 +0300717 if (ret) {
Anson Huang925b3832018-11-21 05:49:40 +0000718 if (ret == -EPROBE_DEFER)
719 return ret;
720
Leonard Crestezae621552017-07-14 17:11:08 +0300721 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) {
Anson Huang337a4ae2018-09-13 17:13:05 +0800728 dev_err(&pdev->dev, "failed to init from fsl,tempmon-data\n");
Leonard Crestezae621552017-07-14 17:11:08 +0300729 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
Bastian Stendera1d00152018-06-08 11:06:39 +0200746 ret = imx_thermal_register_legacy_cooling(data);
747 if (ret) {
Anson Huangc589c562018-11-21 05:49:36 +0000748 if (ret == -EPROBE_DEFER)
749 return ret;
750
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530751 dev_err(&pdev->dev,
752 "failed to register cpufreq cooling device: %d\n", ret);
Shawn Guoca3de462013-06-24 14:30:44 +0800753 return ret;
754 }
755
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100756 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
757 if (IS_ERR(data->thermal_clk)) {
758 ret = PTR_ERR(data->thermal_clk);
759 if (ret != -EPROBE_DEFER)
760 dev_err(&pdev->dev,
761 "failed to get thermal clk: %d\n", ret);
Anson Huangc589c562018-11-21 05:49:36 +0000762 goto legacy_cleanup;
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100763 }
764
765 /*
766 * Thermal sensor needs clk on to get correct value, normally
767 * we should enable its clk before taking measurement and disable
768 * clk after measurement is done, but if alarm function is enabled,
769 * hardware will auto measure the temperature periodically, so we
770 * need to keep the clk always on for alarm function.
771 */
772 ret = clk_prepare_enable(data->thermal_clk);
773 if (ret) {
774 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
Anson Huangc589c562018-11-21 05:49:36 +0000775 goto legacy_cleanup;
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100776 }
777
Shawn Guoca3de462013-06-24 14:30:44 +0800778 data->tz = thermal_zone_device_register("imx_thermal_zone",
Philipp Zabel017e5142013-08-01 18:33:11 +0200779 IMX_TRIP_NUM,
780 BIT(IMX_TRIP_PASSIVE), data,
Shawn Guoca3de462013-06-24 14:30:44 +0800781 &imx_tz_ops, NULL,
782 IMX_PASSIVE_DELAY,
783 IMX_POLLING_DELAY);
784 if (IS_ERR(data->tz)) {
785 ret = PTR_ERR(data->tz);
786 dev_err(&pdev->dev,
787 "failed to register thermal zone device %d\n", ret);
Anson Huangb6ad3982018-09-13 17:13:06 +0800788 goto clk_disable;
Shawn Guoca3de462013-06-24 14:30:44 +0800789 }
790
Tim Harveya2291ba2015-11-19 06:49:40 -0800791 dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
792 " critical:%dC passive:%dC\n", data->temp_grade,
793 data->temp_max / 1000, data->temp_critical / 1000,
794 data->temp_passive / 1000);
795
Philipp Zabel37713a12013-08-01 18:33:12 +0200796 /* Enable measurements at ~ 10 Hz */
Anson Huangf085f672018-03-02 09:59:30 +0800797 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
798 data->socdata->measure_freq_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200799 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
Anson Huangf085f672018-03-02 09:59:30 +0800800 regmap_write(map, data->socdata->measure_freq_ctrl + REG_SET,
801 measure_freq << data->socdata->measure_freq_shift);
Philipp Zabel37713a12013-08-01 18:33:12 +0200802 imx_set_alarm_temp(data, data->temp_passive);
Anson Huang3c94f172014-08-06 15:12:09 +0800803
804 if (data->socdata->version == TEMPMON_IMX6SX)
805 imx_set_panic_temp(data, data->temp_critical);
806
Anson Huangf085f672018-03-02 09:59:30 +0800807 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
808 data->socdata->power_down_mask);
809 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
810 data->socdata->measure_temp_mask);
Philipp Zabel37713a12013-08-01 18:33:12 +0200811
Mikhail Lappocf1ba1d2018-02-02 16:17:46 -0200812 data->irq_enabled = true;
Andrzej Pietrasiewicz7f4957b2020-06-29 14:29:21 +0200813 ret = thermal_zone_device_enable(data->tz);
814 if (ret)
815 goto thermal_zone_unregister;
Mikhail Lappocf1ba1d2018-02-02 16:17:46 -0200816
Bai Ping84866ee2015-09-14 19:09:51 +0800817 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
818 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
819 0, "imx_thermal", data);
820 if (ret < 0) {
821 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
Anson Huangb6ad3982018-09-13 17:13:06 +0800822 goto thermal_zone_unregister;
Bai Ping84866ee2015-09-14 19:09:51 +0800823 }
824
Shawn Guoca3de462013-06-24 14:30:44 +0800825 return 0;
Anson Huangb6ad3982018-09-13 17:13:06 +0800826
827thermal_zone_unregister:
828 thermal_zone_device_unregister(data->tz);
829clk_disable:
830 clk_disable_unprepare(data->thermal_clk);
Anson Huangc589c562018-11-21 05:49:36 +0000831legacy_cleanup:
832 imx_thermal_unregister_legacy_cooling(data);
Anson Huangb6ad3982018-09-13 17:13:06 +0800833
834 return ret;
Shawn Guoca3de462013-06-24 14:30:44 +0800835}
836
837static int imx_thermal_remove(struct platform_device *pdev)
838{
839 struct imx_thermal_data *data = platform_get_drvdata(pdev);
Philipp Zabel37713a12013-08-01 18:33:12 +0200840 struct regmap *map = data->tempmon;
841
842 /* Disable measurements */
Anson Huangf085f672018-03-02 09:59:30 +0800843 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
844 data->socdata->power_down_mask);
Anson Huang329fe7b2013-12-23 15:49:22 -0500845 if (!IS_ERR(data->thermal_clk))
846 clk_disable_unprepare(data->thermal_clk);
Shawn Guoca3de462013-06-24 14:30:44 +0800847
848 thermal_zone_device_unregister(data->tz);
Anson Huang9db11012020-03-13 16:28:14 +0800849 imx_thermal_unregister_legacy_cooling(data);
Shawn Guoca3de462013-06-24 14:30:44 +0800850
851 return 0;
852}
853
Anson Huangb0095142020-03-02 16:54:26 +0800854static int __maybe_unused imx_thermal_suspend(struct device *dev)
Shawn Guoca3de462013-06-24 14:30:44 +0800855{
856 struct imx_thermal_data *data = dev_get_drvdata(dev);
Andrzej Pietrasiewicz7f4957b2020-06-29 14:29:21 +0200857 int ret;
Shawn Guoca3de462013-06-24 14:30:44 +0800858
Anson Huangb46cce52013-12-24 09:43:24 -0500859 /*
860 * Need to disable thermal sensor, otherwise, when thermal core
861 * try to get temperature before thermal sensor resume, a wrong
862 * temperature will be read as the thermal sensor is powered
Andrzej Pietrasiewiczf5e50bf42020-06-29 14:29:25 +0200863 * down. This is done in change_mode() operation called from
Andrzej Pietrasiewicz7f4957b2020-06-29 14:29:21 +0200864 * thermal_zone_device_disable()
Anson Huangb46cce52013-12-24 09:43:24 -0500865 */
Andrzej Pietrasiewicz7f4957b2020-06-29 14:29:21 +0200866 ret = thermal_zone_device_disable(data->tz);
867 if (ret)
868 return ret;
Anson Huangd26eef82015-01-06 18:50:22 +0800869 clk_disable_unprepare(data->thermal_clk);
Shawn Guoca3de462013-06-24 14:30:44 +0800870
871 return 0;
872}
873
Anson Huangb0095142020-03-02 16:54:26 +0800874static int __maybe_unused imx_thermal_resume(struct device *dev)
Shawn Guoca3de462013-06-24 14:30:44 +0800875{
Anson Huangb46cce52013-12-24 09:43:24 -0500876 struct imx_thermal_data *data = dev_get_drvdata(dev);
Arvind Yadave3bdc8d2017-06-06 15:12:37 +0530877 int ret;
Anson Huangb46cce52013-12-24 09:43:24 -0500878
Arvind Yadave3bdc8d2017-06-06 15:12:37 +0530879 ret = clk_prepare_enable(data->thermal_clk);
880 if (ret)
881 return ret;
Anson Huangb46cce52013-12-24 09:43:24 -0500882 /* Enabled thermal sensor after resume */
Andrzej Pietrasiewicz7f4957b2020-06-29 14:29:21 +0200883 ret = thermal_zone_device_enable(data->tz);
884 if (ret)
885 return ret;
Anson Huangb46cce52013-12-24 09:43:24 -0500886
Shawn Guoca3de462013-06-24 14:30:44 +0800887 return 0;
888}
Shawn Guoca3de462013-06-24 14:30:44 +0800889
890static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
891 imx_thermal_suspend, imx_thermal_resume);
892
Shawn Guoca3de462013-06-24 14:30:44 +0800893static struct platform_driver imx_thermal = {
894 .driver = {
895 .name = "imx_thermal",
Shawn Guoca3de462013-06-24 14:30:44 +0800896 .pm = &imx_thermal_pm_ops,
897 .of_match_table = of_imx_thermal_match,
898 },
899 .probe = imx_thermal_probe,
900 .remove = imx_thermal_remove,
901};
902module_platform_driver(imx_thermal);
903
904MODULE_AUTHOR("Freescale Semiconductor, Inc.");
905MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
906MODULE_LICENSE("GPL v2");
907MODULE_ALIAS("platform:imx-thermal");