blob: 4798b4b1fd77f309e19cad3f627b728858b22b35 [file] [log] [blame]
Shawn Guoca3de462013-06-24 14:30:44 +08001/*
2 * Copyright 2013 Freescale Semiconductor, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 */
9
Anson Huang329fe7b2013-12-23 15:49:22 -050010#include <linux/clk.h>
Viresh Kumar4d753aa2017-04-25 15:57:14 +053011#include <linux/cpufreq.h>
Shawn Guoca3de462013-06-24 14:30:44 +080012#include <linux/cpu_cooling.h>
Shawn Guoca3de462013-06-24 14:30:44 +080013#include <linux/delay.h>
14#include <linux/device.h>
15#include <linux/init.h>
Philipp Zabel37713a12013-08-01 18:33:12 +020016#include <linux/interrupt.h>
Shawn Guoca3de462013-06-24 14:30:44 +080017#include <linux/io.h>
18#include <linux/kernel.h>
19#include <linux/mfd/syscon.h>
20#include <linux/module.h>
21#include <linux/of.h>
Anson Huang3c94f172014-08-06 15:12:09 +080022#include <linux/of_device.h>
Shawn Guoca3de462013-06-24 14:30:44 +080023#include <linux/platform_device.h>
24#include <linux/regmap.h>
25#include <linux/slab.h>
26#include <linux/thermal.h>
27#include <linux/types.h>
28
29#define REG_SET 0x4
30#define REG_CLR 0x8
31#define REG_TOG 0xc
32
33#define MISC0 0x0150
34#define MISC0_REFTOP_SELBIASOFF (1 << 3)
Anson Huang3c94f172014-08-06 15:12:09 +080035#define MISC1 0x0160
36#define MISC1_IRQ_TEMPHIGH (1 << 29)
37/* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
38#define MISC1_IRQ_TEMPLOW (1 << 28)
39#define MISC1_IRQ_TEMPPANIC (1 << 27)
Shawn Guoca3de462013-06-24 14:30:44 +080040
41#define TEMPSENSE0 0x0180
Philipp Zabel37713a12013-08-01 18:33:12 +020042#define TEMPSENSE0_ALARM_VALUE_SHIFT 20
43#define TEMPSENSE0_ALARM_VALUE_MASK (0xfff << TEMPSENSE0_ALARM_VALUE_SHIFT)
Shawn Guoca3de462013-06-24 14:30:44 +080044#define TEMPSENSE0_TEMP_CNT_SHIFT 8
45#define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
46#define TEMPSENSE0_FINISHED (1 << 2)
47#define TEMPSENSE0_MEASURE_TEMP (1 << 1)
48#define TEMPSENSE0_POWER_DOWN (1 << 0)
49
50#define TEMPSENSE1 0x0190
51#define TEMPSENSE1_MEASURE_FREQ 0xffff
Anson Huang3c94f172014-08-06 15:12:09 +080052/* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
53#define TEMPSENSE2 0x0290
54#define TEMPSENSE2_LOW_VALUE_SHIFT 0
55#define TEMPSENSE2_LOW_VALUE_MASK 0xfff
56#define TEMPSENSE2_PANIC_VALUE_SHIFT 16
57#define TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
Shawn Guoca3de462013-06-24 14:30:44 +080058
Tim Harveya2291ba2015-11-19 06:49:40 -080059#define OCOTP_MEM0 0x0480
Shawn Guoca3de462013-06-24 14:30:44 +080060#define OCOTP_ANA1 0x04e0
61
62/* The driver supports 1 passive trip point and 1 critical trip point */
63enum imx_thermal_trip {
64 IMX_TRIP_PASSIVE,
65 IMX_TRIP_CRITICAL,
66 IMX_TRIP_NUM,
67};
68
Shawn Guoca3de462013-06-24 14:30:44 +080069#define IMX_POLLING_DELAY 2000 /* millisecond */
70#define IMX_PASSIVE_DELAY 1000
71
Anson Huang749e8be2014-02-12 18:06:35 +080072#define FACTOR0 10000000
73#define FACTOR1 15976
74#define FACTOR2 4297157
75
Anson Huang3c94f172014-08-06 15:12:09 +080076#define TEMPMON_IMX6Q 1
77#define TEMPMON_IMX6SX 2
78
79struct thermal_soc_data {
80 u32 version;
81};
82
83static struct thermal_soc_data thermal_imx6q_data = {
84 .version = TEMPMON_IMX6Q,
85};
86
87static struct thermal_soc_data thermal_imx6sx_data = {
88 .version = TEMPMON_IMX6SX,
89};
90
Shawn Guoca3de462013-06-24 14:30:44 +080091struct imx_thermal_data {
Viresh Kumar4d753aa2017-04-25 15:57:14 +053092 struct cpufreq_policy *policy;
Shawn Guoca3de462013-06-24 14:30:44 +080093 struct thermal_zone_device *tz;
94 struct thermal_cooling_device *cdev;
95 enum thermal_device_mode mode;
96 struct regmap *tempmon;
Anson Huang749e8be2014-02-12 18:06:35 +080097 u32 c1, c2; /* See formula in imx_get_sensor_data() */
Sascha Hauer17e83512015-07-24 08:12:54 +020098 int temp_passive;
99 int temp_critical;
Tim Harveya2291ba2015-11-19 06:49:40 -0800100 int temp_max;
Sascha Hauer17e83512015-07-24 08:12:54 +0200101 int alarm_temp;
102 int last_temp;
Philipp Zabel37713a12013-08-01 18:33:12 +0200103 bool irq_enabled;
104 int irq;
Anson Huang329fe7b2013-12-23 15:49:22 -0500105 struct clk *thermal_clk;
Anson Huang3c94f172014-08-06 15:12:09 +0800106 const struct thermal_soc_data *socdata;
Tim Harveya2291ba2015-11-19 06:49:40 -0800107 const char *temp_grade;
Shawn Guoca3de462013-06-24 14:30:44 +0800108};
109
Anson Huang3c94f172014-08-06 15:12:09 +0800110static void imx_set_panic_temp(struct imx_thermal_data *data,
Sascha Hauer17e83512015-07-24 08:12:54 +0200111 int panic_temp)
Anson Huang3c94f172014-08-06 15:12:09 +0800112{
113 struct regmap *map = data->tempmon;
114 int critical_value;
115
116 critical_value = (data->c2 - panic_temp) / data->c1;
117 regmap_write(map, TEMPSENSE2 + REG_CLR, TEMPSENSE2_PANIC_VALUE_MASK);
118 regmap_write(map, TEMPSENSE2 + REG_SET, critical_value <<
119 TEMPSENSE2_PANIC_VALUE_SHIFT);
120}
121
Philipp Zabel37713a12013-08-01 18:33:12 +0200122static void imx_set_alarm_temp(struct imx_thermal_data *data,
Sascha Hauer17e83512015-07-24 08:12:54 +0200123 int alarm_temp)
Philipp Zabel37713a12013-08-01 18:33:12 +0200124{
125 struct regmap *map = data->tempmon;
126 int alarm_value;
127
128 data->alarm_temp = alarm_temp;
Anson Huang749e8be2014-02-12 18:06:35 +0800129 alarm_value = (data->c2 - alarm_temp) / data->c1;
Philipp Zabel37713a12013-08-01 18:33:12 +0200130 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_ALARM_VALUE_MASK);
131 regmap_write(map, TEMPSENSE0 + REG_SET, alarm_value <<
132 TEMPSENSE0_ALARM_VALUE_SHIFT);
133}
134
Sascha Hauer17e83512015-07-24 08:12:54 +0200135static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800136{
137 struct imx_thermal_data *data = tz->devdata;
138 struct regmap *map = data->tempmon;
Shawn Guoca3de462013-06-24 14:30:44 +0800139 unsigned int n_meas;
Philipp Zabel37713a12013-08-01 18:33:12 +0200140 bool wait;
Shawn Guoca3de462013-06-24 14:30:44 +0800141 u32 val;
142
Philipp Zabel37713a12013-08-01 18:33:12 +0200143 if (data->mode == THERMAL_DEVICE_ENABLED) {
144 /* Check if a measurement is currently in progress */
145 regmap_read(map, TEMPSENSE0, &val);
146 wait = !(val & TEMPSENSE0_FINISHED);
147 } else {
148 /*
149 * Every time we measure the temperature, we will power on the
150 * temperature sensor, enable measurements, take a reading,
151 * disable measurements, power off the temperature sensor.
152 */
153 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
154 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
155
156 wait = true;
157 }
Shawn Guoca3de462013-06-24 14:30:44 +0800158
159 /*
160 * According to the temp sensor designers, it may require up to ~17us
161 * to complete a measurement.
162 */
Philipp Zabel37713a12013-08-01 18:33:12 +0200163 if (wait)
164 usleep_range(20, 50);
Shawn Guoca3de462013-06-24 14:30:44 +0800165
166 regmap_read(map, TEMPSENSE0, &val);
Philipp Zabel37713a12013-08-01 18:33:12 +0200167
168 if (data->mode != THERMAL_DEVICE_ENABLED) {
169 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
170 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
171 }
Shawn Guoca3de462013-06-24 14:30:44 +0800172
173 if ((val & TEMPSENSE0_FINISHED) == 0) {
174 dev_dbg(&tz->device, "temp measurement never finished\n");
175 return -EAGAIN;
176 }
177
178 n_meas = (val & TEMPSENSE0_TEMP_CNT_MASK) >> TEMPSENSE0_TEMP_CNT_SHIFT;
179
180 /* See imx_get_sensor_data() for formula derivation */
Anson Huang749e8be2014-02-12 18:06:35 +0800181 *temp = data->c2 - n_meas * data->c1;
Shawn Guoca3de462013-06-24 14:30:44 +0800182
Anson Huang3c94f172014-08-06 15:12:09 +0800183 /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
184 if (data->socdata->version == TEMPMON_IMX6Q) {
185 if (data->alarm_temp == data->temp_passive &&
186 *temp >= data->temp_passive)
187 imx_set_alarm_temp(data, data->temp_critical);
188 if (data->alarm_temp == data->temp_critical &&
189 *temp < data->temp_passive) {
190 imx_set_alarm_temp(data, data->temp_passive);
Sascha Hauer17e83512015-07-24 08:12:54 +0200191 dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
Anson Huang3c94f172014-08-06 15:12:09 +0800192 data->alarm_temp / 1000);
193 }
Philipp Zabel37713a12013-08-01 18:33:12 +0200194 }
195
196 if (*temp != data->last_temp) {
Sascha Hauer17e83512015-07-24 08:12:54 +0200197 dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
Philipp Zabel37713a12013-08-01 18:33:12 +0200198 data->last_temp = *temp;
199 }
200
201 /* Reenable alarm IRQ if temperature below alarm temperature */
202 if (!data->irq_enabled && *temp < data->alarm_temp) {
203 data->irq_enabled = true;
204 enable_irq(data->irq);
Shawn Guoca3de462013-06-24 14:30:44 +0800205 }
206
207 return 0;
208}
209
210static int imx_get_mode(struct thermal_zone_device *tz,
211 enum thermal_device_mode *mode)
212{
213 struct imx_thermal_data *data = tz->devdata;
214
215 *mode = data->mode;
216
217 return 0;
218}
219
220static int imx_set_mode(struct thermal_zone_device *tz,
221 enum thermal_device_mode mode)
222{
223 struct imx_thermal_data *data = tz->devdata;
Philipp Zabel37713a12013-08-01 18:33:12 +0200224 struct regmap *map = data->tempmon;
Shawn Guoca3de462013-06-24 14:30:44 +0800225
226 if (mode == THERMAL_DEVICE_ENABLED) {
227 tz->polling_delay = IMX_POLLING_DELAY;
228 tz->passive_delay = IMX_PASSIVE_DELAY;
Philipp Zabel37713a12013-08-01 18:33:12 +0200229
230 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
231 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
232
233 if (!data->irq_enabled) {
234 data->irq_enabled = true;
235 enable_irq(data->irq);
236 }
Shawn Guoca3de462013-06-24 14:30:44 +0800237 } else {
Philipp Zabel37713a12013-08-01 18:33:12 +0200238 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
239 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
240
Shawn Guoca3de462013-06-24 14:30:44 +0800241 tz->polling_delay = 0;
242 tz->passive_delay = 0;
Philipp Zabel37713a12013-08-01 18:33:12 +0200243
244 if (data->irq_enabled) {
245 disable_irq(data->irq);
246 data->irq_enabled = false;
247 }
Shawn Guoca3de462013-06-24 14:30:44 +0800248 }
249
250 data->mode = mode;
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700251 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Shawn Guoca3de462013-06-24 14:30:44 +0800252
253 return 0;
254}
255
256static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
257 enum thermal_trip_type *type)
258{
259 *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
260 THERMAL_TRIP_CRITICAL;
261 return 0;
262}
263
Sascha Hauer17e83512015-07-24 08:12:54 +0200264static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800265{
Philipp Zabel017e5142013-08-01 18:33:11 +0200266 struct imx_thermal_data *data = tz->devdata;
267
268 *temp = data->temp_critical;
Shawn Guoca3de462013-06-24 14:30:44 +0800269 return 0;
270}
271
272static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200273 int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800274{
Philipp Zabel017e5142013-08-01 18:33:11 +0200275 struct imx_thermal_data *data = tz->devdata;
276
277 *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
278 data->temp_critical;
279 return 0;
280}
281
282static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200283 int temp)
Philipp Zabel017e5142013-08-01 18:33:11 +0200284{
285 struct imx_thermal_data *data = tz->devdata;
286
Tim Harveya2291ba2015-11-19 06:49:40 -0800287 /* do not allow changing critical threshold */
Philipp Zabel017e5142013-08-01 18:33:11 +0200288 if (trip == IMX_TRIP_CRITICAL)
289 return -EPERM;
290
Tim Harveya2291ba2015-11-19 06:49:40 -0800291 /* do not allow passive to be set higher than critical */
292 if (temp < 0 || temp > data->temp_critical)
Philipp Zabel017e5142013-08-01 18:33:11 +0200293 return -EINVAL;
294
295 data->temp_passive = temp;
296
Philipp Zabel37713a12013-08-01 18:33:12 +0200297 imx_set_alarm_temp(data, temp);
298
Shawn Guoca3de462013-06-24 14:30:44 +0800299 return 0;
300}
301
302static int imx_bind(struct thermal_zone_device *tz,
303 struct thermal_cooling_device *cdev)
304{
305 int ret;
306
307 ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
308 THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000309 THERMAL_NO_LIMIT,
310 THERMAL_WEIGHT_DEFAULT);
Shawn Guoca3de462013-06-24 14:30:44 +0800311 if (ret) {
312 dev_err(&tz->device,
313 "binding zone %s with cdev %s failed:%d\n",
314 tz->type, cdev->type, ret);
315 return ret;
316 }
317
318 return 0;
319}
320
321static int imx_unbind(struct thermal_zone_device *tz,
322 struct thermal_cooling_device *cdev)
323{
324 int ret;
325
326 ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
327 if (ret) {
328 dev_err(&tz->device,
329 "unbinding zone %s with cdev %s failed:%d\n",
330 tz->type, cdev->type, ret);
331 return ret;
332 }
333
334 return 0;
335}
336
Eduardo Valentincbb07bb2014-01-06 08:54:34 -0400337static struct thermal_zone_device_ops imx_tz_ops = {
Shawn Guoca3de462013-06-24 14:30:44 +0800338 .bind = imx_bind,
339 .unbind = imx_unbind,
340 .get_temp = imx_get_temp,
341 .get_mode = imx_get_mode,
342 .set_mode = imx_set_mode,
343 .get_trip_type = imx_get_trip_type,
344 .get_trip_temp = imx_get_trip_temp,
345 .get_crit_temp = imx_get_crit_temp,
Philipp Zabel017e5142013-08-01 18:33:11 +0200346 .set_trip_temp = imx_set_trip_temp,
Shawn Guoca3de462013-06-24 14:30:44 +0800347};
348
349static int imx_get_sensor_data(struct platform_device *pdev)
350{
351 struct imx_thermal_data *data = platform_get_drvdata(pdev);
352 struct regmap *map;
Anson Huangd0f9d642014-06-20 15:03:06 +0800353 int t1, n1;
Shawn Guoca3de462013-06-24 14:30:44 +0800354 int ret;
355 u32 val;
Anson Huang749e8be2014-02-12 18:06:35 +0800356 u64 temp64;
Shawn Guoca3de462013-06-24 14:30:44 +0800357
358 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
359 "fsl,tempmon-data");
360 if (IS_ERR(map)) {
361 ret = PTR_ERR(map);
362 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
363 return ret;
364 }
365
366 ret = regmap_read(map, OCOTP_ANA1, &val);
367 if (ret) {
368 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
369 return ret;
370 }
371
372 if (val == 0 || val == ~0) {
373 dev_err(&pdev->dev, "invalid sensor calibration data\n");
374 return -EINVAL;
375 }
376
377 /*
378 * Sensor data layout:
379 * [31:20] - sensor value @ 25C
Anson Huang749e8be2014-02-12 18:06:35 +0800380 * Use universal formula now and only need sensor value @ 25C
381 * slope = 0.4297157 - (0.0015976 * 25C fuse)
Shawn Guoca3de462013-06-24 14:30:44 +0800382 */
383 n1 = val >> 20;
Shawn Guoca3de462013-06-24 14:30:44 +0800384 t1 = 25; /* t1 always 25C */
385
386 /*
Anson Huang749e8be2014-02-12 18:06:35 +0800387 * Derived from linear interpolation:
388 * slope = 0.4297157 - (0.0015976 * 25C fuse)
389 * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
390 * (Nmeas - n1) / (Tmeas - t1) = slope
Shawn Guoca3de462013-06-24 14:30:44 +0800391 * We want to reduce this down to the minimum computation necessary
392 * for each temperature read. Also, we want Tmeas in millicelsius
393 * and we don't want to lose precision from integer division. So...
Anson Huang749e8be2014-02-12 18:06:35 +0800394 * Tmeas = (Nmeas - n1) / slope + t1
395 * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
396 * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
397 * Let constant c1 = (-1000 / slope)
398 * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
399 * Let constant c2 = n1 *c1 + 1000 * t1
400 * milli_Tmeas = c2 - Nmeas * c1
Shawn Guoca3de462013-06-24 14:30:44 +0800401 */
Anson Huang749e8be2014-02-12 18:06:35 +0800402 temp64 = FACTOR0;
403 temp64 *= 1000;
404 do_div(temp64, FACTOR1 * n1 - FACTOR2);
405 data->c1 = temp64;
406 data->c2 = n1 * data->c1 + 1000 * t1;
Shawn Guoca3de462013-06-24 14:30:44 +0800407
Tim Harveya2291ba2015-11-19 06:49:40 -0800408 /* use OTP for thermal grade */
409 ret = regmap_read(map, OCOTP_MEM0, &val);
410 if (ret) {
411 dev_err(&pdev->dev, "failed to read temp grade: %d\n", ret);
412 return ret;
413 }
414
415 /* The maximum die temp is specified by the Temperature Grade */
416 switch ((val >> 6) & 0x3) {
417 case 0: /* Commercial (0 to 95C) */
418 data->temp_grade = "Commercial";
419 data->temp_max = 95000;
420 break;
421 case 1: /* Extended Commercial (-20 to 105C) */
422 data->temp_grade = "Extended Commercial";
423 data->temp_max = 105000;
424 break;
425 case 2: /* Industrial (-40 to 105C) */
426 data->temp_grade = "Industrial";
427 data->temp_max = 105000;
428 break;
429 case 3: /* Automotive (-40 to 125C) */
430 data->temp_grade = "Automotive";
431 data->temp_max = 125000;
432 break;
433 }
Philipp Zabel017e5142013-08-01 18:33:11 +0200434
435 /*
Tim Harveya2291ba2015-11-19 06:49:40 -0800436 * Set the critical trip point at 5C under max
437 * Set the passive trip point at 10C under max (can change via sysfs)
Philipp Zabel017e5142013-08-01 18:33:11 +0200438 */
Tim Harveya2291ba2015-11-19 06:49:40 -0800439 data->temp_critical = data->temp_max - (1000 * 5);
440 data->temp_passive = data->temp_max - (1000 * 10);
Philipp Zabel017e5142013-08-01 18:33:11 +0200441
Shawn Guoca3de462013-06-24 14:30:44 +0800442 return 0;
443}
444
Philipp Zabel37713a12013-08-01 18:33:12 +0200445static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
446{
447 struct imx_thermal_data *data = dev;
448
449 disable_irq_nosync(irq);
450 data->irq_enabled = false;
451
452 return IRQ_WAKE_THREAD;
453}
454
455static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
456{
457 struct imx_thermal_data *data = dev;
458
Sascha Hauer17e83512015-07-24 08:12:54 +0200459 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
Philipp Zabel37713a12013-08-01 18:33:12 +0200460 data->alarm_temp / 1000);
461
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700462 thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
Philipp Zabel37713a12013-08-01 18:33:12 +0200463
464 return IRQ_HANDLED;
465}
466
Anson Huang3c94f172014-08-06 15:12:09 +0800467static const struct of_device_id of_imx_thermal_match[] = {
468 { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
469 { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
470 { /* end */ }
471};
472MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
473
Shawn Guoca3de462013-06-24 14:30:44 +0800474static int imx_thermal_probe(struct platform_device *pdev)
475{
476 struct imx_thermal_data *data;
Shawn Guoca3de462013-06-24 14:30:44 +0800477 struct regmap *map;
Philipp Zabel37713a12013-08-01 18:33:12 +0200478 int measure_freq;
Shawn Guoca3de462013-06-24 14:30:44 +0800479 int ret;
480
481 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
482 if (!data)
483 return -ENOMEM;
484
485 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
486 if (IS_ERR(map)) {
487 ret = PTR_ERR(map);
488 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
489 return ret;
490 }
491 data->tempmon = map;
492
Corentin LABBE829bc78a2016-08-16 10:51:38 +0200493 data->socdata = of_device_get_match_data(&pdev->dev);
Shailendra Verma8b051ec2017-01-30 10:34:58 +0530494 if (!data->socdata) {
495 dev_err(&pdev->dev, "no device match found\n");
496 return -ENODEV;
497 }
Anson Huang3c94f172014-08-06 15:12:09 +0800498
499 /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
500 if (data->socdata->version == TEMPMON_IMX6SX) {
501 regmap_write(map, MISC1 + REG_CLR, MISC1_IRQ_TEMPHIGH |
502 MISC1_IRQ_TEMPLOW | MISC1_IRQ_TEMPPANIC);
503 /*
504 * reset value of LOW ALARM is incorrect, set it to lowest
505 * value to avoid false trigger of low alarm.
506 */
507 regmap_write(map, TEMPSENSE2 + REG_SET,
508 TEMPSENSE2_LOW_VALUE_MASK);
509 }
510
Philipp Zabel37713a12013-08-01 18:33:12 +0200511 data->irq = platform_get_irq(pdev, 0);
512 if (data->irq < 0)
513 return data->irq;
514
Shawn Guoca3de462013-06-24 14:30:44 +0800515 platform_set_drvdata(pdev, data);
516
517 ret = imx_get_sensor_data(pdev);
518 if (ret) {
519 dev_err(&pdev->dev, "failed to get sensor data\n");
520 return ret;
521 }
522
523 /* Make sure sensor is in known good state for measurements */
524 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
525 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
526 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
527 regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
528 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
529
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530530 data->policy = cpufreq_cpu_get(0);
531 if (!data->policy) {
532 pr_debug("%s: CPUFreq policy not found\n", __func__);
533 return -EPROBE_DEFER;
534 }
535
536 data->cdev = cpufreq_cooling_register(data->policy);
Shawn Guoca3de462013-06-24 14:30:44 +0800537 if (IS_ERR(data->cdev)) {
538 ret = PTR_ERR(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530539 dev_err(&pdev->dev,
540 "failed to register cpufreq cooling device: %d\n", ret);
541 cpufreq_cpu_put(data->policy);
Shawn Guoca3de462013-06-24 14:30:44 +0800542 return ret;
543 }
544
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100545 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
546 if (IS_ERR(data->thermal_clk)) {
547 ret = PTR_ERR(data->thermal_clk);
548 if (ret != -EPROBE_DEFER)
549 dev_err(&pdev->dev,
550 "failed to get thermal clk: %d\n", ret);
551 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530552 cpufreq_cpu_put(data->policy);
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100553 return ret;
554 }
555
556 /*
557 * Thermal sensor needs clk on to get correct value, normally
558 * we should enable its clk before taking measurement and disable
559 * clk after measurement is done, but if alarm function is enabled,
560 * hardware will auto measure the temperature periodically, so we
561 * need to keep the clk always on for alarm function.
562 */
563 ret = clk_prepare_enable(data->thermal_clk);
564 if (ret) {
565 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
566 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530567 cpufreq_cpu_put(data->policy);
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100568 return ret;
569 }
570
Shawn Guoca3de462013-06-24 14:30:44 +0800571 data->tz = thermal_zone_device_register("imx_thermal_zone",
Philipp Zabel017e5142013-08-01 18:33:11 +0200572 IMX_TRIP_NUM,
573 BIT(IMX_TRIP_PASSIVE), data,
Shawn Guoca3de462013-06-24 14:30:44 +0800574 &imx_tz_ops, NULL,
575 IMX_PASSIVE_DELAY,
576 IMX_POLLING_DELAY);
577 if (IS_ERR(data->tz)) {
578 ret = PTR_ERR(data->tz);
579 dev_err(&pdev->dev,
580 "failed to register thermal zone device %d\n", ret);
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100581 clk_disable_unprepare(data->thermal_clk);
Shawn Guoca3de462013-06-24 14:30:44 +0800582 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530583 cpufreq_cpu_put(data->policy);
Shawn Guoca3de462013-06-24 14:30:44 +0800584 return ret;
585 }
586
Tim Harveya2291ba2015-11-19 06:49:40 -0800587 dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
588 " critical:%dC passive:%dC\n", data->temp_grade,
589 data->temp_max / 1000, data->temp_critical / 1000,
590 data->temp_passive / 1000);
591
Philipp Zabel37713a12013-08-01 18:33:12 +0200592 /* Enable measurements at ~ 10 Hz */
593 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
594 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
595 regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
596 imx_set_alarm_temp(data, data->temp_passive);
Anson Huang3c94f172014-08-06 15:12:09 +0800597
598 if (data->socdata->version == TEMPMON_IMX6SX)
599 imx_set_panic_temp(data, data->temp_critical);
600
Philipp Zabel37713a12013-08-01 18:33:12 +0200601 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
602 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
603
Bai Ping84866ee2015-09-14 19:09:51 +0800604 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
605 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
606 0, "imx_thermal", data);
607 if (ret < 0) {
608 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
609 clk_disable_unprepare(data->thermal_clk);
610 thermal_zone_device_unregister(data->tz);
611 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530612 cpufreq_cpu_put(data->policy);
Bai Ping84866ee2015-09-14 19:09:51 +0800613 return ret;
614 }
615
Philipp Zabel37713a12013-08-01 18:33:12 +0200616 data->irq_enabled = true;
Shawn Guoca3de462013-06-24 14:30:44 +0800617 data->mode = THERMAL_DEVICE_ENABLED;
618
619 return 0;
620}
621
622static int imx_thermal_remove(struct platform_device *pdev)
623{
624 struct imx_thermal_data *data = platform_get_drvdata(pdev);
Philipp Zabel37713a12013-08-01 18:33:12 +0200625 struct regmap *map = data->tempmon;
626
627 /* Disable measurements */
628 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
Anson Huang329fe7b2013-12-23 15:49:22 -0500629 if (!IS_ERR(data->thermal_clk))
630 clk_disable_unprepare(data->thermal_clk);
Shawn Guoca3de462013-06-24 14:30:44 +0800631
632 thermal_zone_device_unregister(data->tz);
633 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530634 cpufreq_cpu_put(data->policy);
Shawn Guoca3de462013-06-24 14:30:44 +0800635
636 return 0;
637}
638
639#ifdef CONFIG_PM_SLEEP
640static int imx_thermal_suspend(struct device *dev)
641{
642 struct imx_thermal_data *data = dev_get_drvdata(dev);
643 struct regmap *map = data->tempmon;
Shawn Guoca3de462013-06-24 14:30:44 +0800644
Anson Huangb46cce52013-12-24 09:43:24 -0500645 /*
646 * Need to disable thermal sensor, otherwise, when thermal core
647 * try to get temperature before thermal sensor resume, a wrong
648 * temperature will be read as the thermal sensor is powered
649 * down.
650 */
651 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
652 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
653 data->mode = THERMAL_DEVICE_DISABLED;
Anson Huangd26eef82015-01-06 18:50:22 +0800654 clk_disable_unprepare(data->thermal_clk);
Shawn Guoca3de462013-06-24 14:30:44 +0800655
656 return 0;
657}
658
659static int imx_thermal_resume(struct device *dev)
660{
Anson Huangb46cce52013-12-24 09:43:24 -0500661 struct imx_thermal_data *data = dev_get_drvdata(dev);
662 struct regmap *map = data->tempmon;
Arvind Yadave3bdc8d2017-06-06 15:12:37 +0530663 int ret;
Anson Huangb46cce52013-12-24 09:43:24 -0500664
Arvind Yadave3bdc8d2017-06-06 15:12:37 +0530665 ret = clk_prepare_enable(data->thermal_clk);
666 if (ret)
667 return ret;
Anson Huangb46cce52013-12-24 09:43:24 -0500668 /* Enabled thermal sensor after resume */
669 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
670 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
671 data->mode = THERMAL_DEVICE_ENABLED;
672
Shawn Guoca3de462013-06-24 14:30:44 +0800673 return 0;
674}
675#endif
676
677static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
678 imx_thermal_suspend, imx_thermal_resume);
679
Shawn Guoca3de462013-06-24 14:30:44 +0800680static struct platform_driver imx_thermal = {
681 .driver = {
682 .name = "imx_thermal",
Shawn Guoca3de462013-06-24 14:30:44 +0800683 .pm = &imx_thermal_pm_ops,
684 .of_match_table = of_imx_thermal_match,
685 },
686 .probe = imx_thermal_probe,
687 .remove = imx_thermal_remove,
688};
689module_platform_driver(imx_thermal);
690
691MODULE_AUTHOR("Freescale Semiconductor, Inc.");
692MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
693MODULE_LICENSE("GPL v2");
694MODULE_ALIAS("platform:imx-thermal");