blob: 21b8c4c4da3cb8f1966f9ef0524c54814158e80a [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>
Leonard Crestezae621552017-07-14 17:11:08 +030028#include <linux/nvmem-consumer.h>
Shawn Guoca3de462013-06-24 14:30:44 +080029
30#define REG_SET 0x4
31#define REG_CLR 0x8
32#define REG_TOG 0xc
33
34#define MISC0 0x0150
35#define MISC0_REFTOP_SELBIASOFF (1 << 3)
Anson Huang3c94f172014-08-06 15:12:09 +080036#define MISC1 0x0160
37#define MISC1_IRQ_TEMPHIGH (1 << 29)
38/* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
39#define MISC1_IRQ_TEMPLOW (1 << 28)
40#define MISC1_IRQ_TEMPPANIC (1 << 27)
Shawn Guoca3de462013-06-24 14:30:44 +080041
42#define TEMPSENSE0 0x0180
Philipp Zabel37713a12013-08-01 18:33:12 +020043#define TEMPSENSE0_ALARM_VALUE_SHIFT 20
44#define TEMPSENSE0_ALARM_VALUE_MASK (0xfff << TEMPSENSE0_ALARM_VALUE_SHIFT)
Shawn Guoca3de462013-06-24 14:30:44 +080045#define TEMPSENSE0_TEMP_CNT_SHIFT 8
46#define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
47#define TEMPSENSE0_FINISHED (1 << 2)
48#define TEMPSENSE0_MEASURE_TEMP (1 << 1)
49#define TEMPSENSE0_POWER_DOWN (1 << 0)
50
51#define TEMPSENSE1 0x0190
52#define TEMPSENSE1_MEASURE_FREQ 0xffff
Anson Huang3c94f172014-08-06 15:12:09 +080053/* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
54#define TEMPSENSE2 0x0290
55#define TEMPSENSE2_LOW_VALUE_SHIFT 0
56#define TEMPSENSE2_LOW_VALUE_MASK 0xfff
57#define TEMPSENSE2_PANIC_VALUE_SHIFT 16
58#define TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
Shawn Guoca3de462013-06-24 14:30:44 +080059
Tim Harveya2291ba2015-11-19 06:49:40 -080060#define OCOTP_MEM0 0x0480
Shawn Guoca3de462013-06-24 14:30:44 +080061#define OCOTP_ANA1 0x04e0
62
63/* The driver supports 1 passive trip point and 1 critical trip point */
64enum imx_thermal_trip {
65 IMX_TRIP_PASSIVE,
66 IMX_TRIP_CRITICAL,
67 IMX_TRIP_NUM,
68};
69
Shawn Guoca3de462013-06-24 14:30:44 +080070#define IMX_POLLING_DELAY 2000 /* millisecond */
71#define IMX_PASSIVE_DELAY 1000
72
Anson Huang749e8be2014-02-12 18:06:35 +080073#define FACTOR0 10000000
74#define FACTOR1 15976
75#define FACTOR2 4297157
76
Anson Huang3c94f172014-08-06 15:12:09 +080077#define TEMPMON_IMX6Q 1
78#define TEMPMON_IMX6SX 2
79
80struct thermal_soc_data {
81 u32 version;
82};
83
84static struct thermal_soc_data thermal_imx6q_data = {
85 .version = TEMPMON_IMX6Q,
86};
87
88static struct thermal_soc_data thermal_imx6sx_data = {
89 .version = TEMPMON_IMX6SX,
90};
91
Shawn Guoca3de462013-06-24 14:30:44 +080092struct imx_thermal_data {
Viresh Kumar4d753aa2017-04-25 15:57:14 +053093 struct cpufreq_policy *policy;
Shawn Guoca3de462013-06-24 14:30:44 +080094 struct thermal_zone_device *tz;
95 struct thermal_cooling_device *cdev;
96 enum thermal_device_mode mode;
97 struct regmap *tempmon;
Leonard Crestezae621552017-07-14 17:11:08 +030098 u32 c1, c2; /* See formula in imx_init_calib() */
Sascha Hauer17e83512015-07-24 08:12:54 +020099 int temp_passive;
100 int temp_critical;
Tim Harveya2291ba2015-11-19 06:49:40 -0800101 int temp_max;
Sascha Hauer17e83512015-07-24 08:12:54 +0200102 int alarm_temp;
103 int last_temp;
Philipp Zabel37713a12013-08-01 18:33:12 +0200104 bool irq_enabled;
105 int irq;
Anson Huang329fe7b2013-12-23 15:49:22 -0500106 struct clk *thermal_clk;
Anson Huang3c94f172014-08-06 15:12:09 +0800107 const struct thermal_soc_data *socdata;
Tim Harveya2291ba2015-11-19 06:49:40 -0800108 const char *temp_grade;
Shawn Guoca3de462013-06-24 14:30:44 +0800109};
110
Anson Huang3c94f172014-08-06 15:12:09 +0800111static void imx_set_panic_temp(struct imx_thermal_data *data,
Sascha Hauer17e83512015-07-24 08:12:54 +0200112 int panic_temp)
Anson Huang3c94f172014-08-06 15:12:09 +0800113{
114 struct regmap *map = data->tempmon;
115 int critical_value;
116
117 critical_value = (data->c2 - panic_temp) / data->c1;
118 regmap_write(map, TEMPSENSE2 + REG_CLR, TEMPSENSE2_PANIC_VALUE_MASK);
119 regmap_write(map, TEMPSENSE2 + REG_SET, critical_value <<
120 TEMPSENSE2_PANIC_VALUE_SHIFT);
121}
122
Philipp Zabel37713a12013-08-01 18:33:12 +0200123static void imx_set_alarm_temp(struct imx_thermal_data *data,
Sascha Hauer17e83512015-07-24 08:12:54 +0200124 int alarm_temp)
Philipp Zabel37713a12013-08-01 18:33:12 +0200125{
126 struct regmap *map = data->tempmon;
127 int alarm_value;
128
129 data->alarm_temp = alarm_temp;
Anson Huang749e8be2014-02-12 18:06:35 +0800130 alarm_value = (data->c2 - alarm_temp) / data->c1;
Philipp Zabel37713a12013-08-01 18:33:12 +0200131 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_ALARM_VALUE_MASK);
132 regmap_write(map, TEMPSENSE0 + REG_SET, alarm_value <<
133 TEMPSENSE0_ALARM_VALUE_SHIFT);
134}
135
Sascha Hauer17e83512015-07-24 08:12:54 +0200136static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800137{
138 struct imx_thermal_data *data = tz->devdata;
139 struct regmap *map = data->tempmon;
Shawn Guoca3de462013-06-24 14:30:44 +0800140 unsigned int n_meas;
Philipp Zabel37713a12013-08-01 18:33:12 +0200141 bool wait;
Shawn Guoca3de462013-06-24 14:30:44 +0800142 u32 val;
143
Philipp Zabel37713a12013-08-01 18:33:12 +0200144 if (data->mode == THERMAL_DEVICE_ENABLED) {
145 /* Check if a measurement is currently in progress */
146 regmap_read(map, TEMPSENSE0, &val);
147 wait = !(val & TEMPSENSE0_FINISHED);
148 } else {
149 /*
150 * Every time we measure the temperature, we will power on the
151 * temperature sensor, enable measurements, take a reading,
152 * disable measurements, power off the temperature sensor.
153 */
154 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
155 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
156
157 wait = true;
158 }
Shawn Guoca3de462013-06-24 14:30:44 +0800159
160 /*
161 * According to the temp sensor designers, it may require up to ~17us
162 * to complete a measurement.
163 */
Philipp Zabel37713a12013-08-01 18:33:12 +0200164 if (wait)
165 usleep_range(20, 50);
Shawn Guoca3de462013-06-24 14:30:44 +0800166
167 regmap_read(map, TEMPSENSE0, &val);
Philipp Zabel37713a12013-08-01 18:33:12 +0200168
169 if (data->mode != THERMAL_DEVICE_ENABLED) {
170 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
171 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
172 }
Shawn Guoca3de462013-06-24 14:30:44 +0800173
174 if ((val & TEMPSENSE0_FINISHED) == 0) {
175 dev_dbg(&tz->device, "temp measurement never finished\n");
176 return -EAGAIN;
177 }
178
179 n_meas = (val & TEMPSENSE0_TEMP_CNT_MASK) >> TEMPSENSE0_TEMP_CNT_SHIFT;
180
Leonard Crestezae621552017-07-14 17:11:08 +0300181 /* See imx_init_calib() for formula derivation */
Anson Huang749e8be2014-02-12 18:06:35 +0800182 *temp = data->c2 - n_meas * data->c1;
Shawn Guoca3de462013-06-24 14:30:44 +0800183
Anson Huang3c94f172014-08-06 15:12:09 +0800184 /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
185 if (data->socdata->version == TEMPMON_IMX6Q) {
186 if (data->alarm_temp == data->temp_passive &&
187 *temp >= data->temp_passive)
188 imx_set_alarm_temp(data, data->temp_critical);
189 if (data->alarm_temp == data->temp_critical &&
190 *temp < data->temp_passive) {
191 imx_set_alarm_temp(data, data->temp_passive);
Sascha Hauer17e83512015-07-24 08:12:54 +0200192 dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
Anson Huang3c94f172014-08-06 15:12:09 +0800193 data->alarm_temp / 1000);
194 }
Philipp Zabel37713a12013-08-01 18:33:12 +0200195 }
196
197 if (*temp != data->last_temp) {
Sascha Hauer17e83512015-07-24 08:12:54 +0200198 dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
Philipp Zabel37713a12013-08-01 18:33:12 +0200199 data->last_temp = *temp;
200 }
201
202 /* Reenable alarm IRQ if temperature below alarm temperature */
203 if (!data->irq_enabled && *temp < data->alarm_temp) {
204 data->irq_enabled = true;
205 enable_irq(data->irq);
Shawn Guoca3de462013-06-24 14:30:44 +0800206 }
207
208 return 0;
209}
210
211static int imx_get_mode(struct thermal_zone_device *tz,
212 enum thermal_device_mode *mode)
213{
214 struct imx_thermal_data *data = tz->devdata;
215
216 *mode = data->mode;
217
218 return 0;
219}
220
221static int imx_set_mode(struct thermal_zone_device *tz,
222 enum thermal_device_mode mode)
223{
224 struct imx_thermal_data *data = tz->devdata;
Philipp Zabel37713a12013-08-01 18:33:12 +0200225 struct regmap *map = data->tempmon;
Shawn Guoca3de462013-06-24 14:30:44 +0800226
227 if (mode == THERMAL_DEVICE_ENABLED) {
228 tz->polling_delay = IMX_POLLING_DELAY;
229 tz->passive_delay = IMX_PASSIVE_DELAY;
Philipp Zabel37713a12013-08-01 18:33:12 +0200230
231 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
232 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
233
234 if (!data->irq_enabled) {
235 data->irq_enabled = true;
236 enable_irq(data->irq);
237 }
Shawn Guoca3de462013-06-24 14:30:44 +0800238 } else {
Philipp Zabel37713a12013-08-01 18:33:12 +0200239 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
240 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
241
Shawn Guoca3de462013-06-24 14:30:44 +0800242 tz->polling_delay = 0;
243 tz->passive_delay = 0;
Philipp Zabel37713a12013-08-01 18:33:12 +0200244
245 if (data->irq_enabled) {
246 disable_irq(data->irq);
247 data->irq_enabled = false;
248 }
Shawn Guoca3de462013-06-24 14:30:44 +0800249 }
250
251 data->mode = mode;
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700252 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Shawn Guoca3de462013-06-24 14:30:44 +0800253
254 return 0;
255}
256
257static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
258 enum thermal_trip_type *type)
259{
260 *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
261 THERMAL_TRIP_CRITICAL;
262 return 0;
263}
264
Sascha Hauer17e83512015-07-24 08:12:54 +0200265static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800266{
Philipp Zabel017e5142013-08-01 18:33:11 +0200267 struct imx_thermal_data *data = tz->devdata;
268
269 *temp = data->temp_critical;
Shawn Guoca3de462013-06-24 14:30:44 +0800270 return 0;
271}
272
273static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200274 int *temp)
Shawn Guoca3de462013-06-24 14:30:44 +0800275{
Philipp Zabel017e5142013-08-01 18:33:11 +0200276 struct imx_thermal_data *data = tz->devdata;
277
278 *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
279 data->temp_critical;
280 return 0;
281}
282
283static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200284 int temp)
Philipp Zabel017e5142013-08-01 18:33:11 +0200285{
286 struct imx_thermal_data *data = tz->devdata;
287
Tim Harveya2291ba2015-11-19 06:49:40 -0800288 /* do not allow changing critical threshold */
Philipp Zabel017e5142013-08-01 18:33:11 +0200289 if (trip == IMX_TRIP_CRITICAL)
290 return -EPERM;
291
Tim Harveya2291ba2015-11-19 06:49:40 -0800292 /* do not allow passive to be set higher than critical */
293 if (temp < 0 || temp > data->temp_critical)
Philipp Zabel017e5142013-08-01 18:33:11 +0200294 return -EINVAL;
295
296 data->temp_passive = temp;
297
Philipp Zabel37713a12013-08-01 18:33:12 +0200298 imx_set_alarm_temp(data, temp);
299
Shawn Guoca3de462013-06-24 14:30:44 +0800300 return 0;
301}
302
303static int imx_bind(struct thermal_zone_device *tz,
304 struct thermal_cooling_device *cdev)
305{
306 int ret;
307
308 ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
309 THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000310 THERMAL_NO_LIMIT,
311 THERMAL_WEIGHT_DEFAULT);
Shawn Guoca3de462013-06-24 14:30:44 +0800312 if (ret) {
313 dev_err(&tz->device,
314 "binding zone %s with cdev %s failed:%d\n",
315 tz->type, cdev->type, ret);
316 return ret;
317 }
318
319 return 0;
320}
321
322static int imx_unbind(struct thermal_zone_device *tz,
323 struct thermal_cooling_device *cdev)
324{
325 int ret;
326
327 ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
328 if (ret) {
329 dev_err(&tz->device,
330 "unbinding zone %s with cdev %s failed:%d\n",
331 tz->type, cdev->type, ret);
332 return ret;
333 }
334
335 return 0;
336}
337
Eduardo Valentincbb07bb2014-01-06 08:54:34 -0400338static struct thermal_zone_device_ops imx_tz_ops = {
Shawn Guoca3de462013-06-24 14:30:44 +0800339 .bind = imx_bind,
340 .unbind = imx_unbind,
341 .get_temp = imx_get_temp,
342 .get_mode = imx_get_mode,
343 .set_mode = imx_set_mode,
344 .get_trip_type = imx_get_trip_type,
345 .get_trip_temp = imx_get_trip_temp,
346 .get_crit_temp = imx_get_crit_temp,
Philipp Zabel017e5142013-08-01 18:33:11 +0200347 .set_trip_temp = imx_set_trip_temp,
Shawn Guoca3de462013-06-24 14:30:44 +0800348};
349
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100350static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
Shawn Guoca3de462013-06-24 14:30:44 +0800351{
352 struct imx_thermal_data *data = platform_get_drvdata(pdev);
Anson Huangd0f9d642014-06-20 15:03:06 +0800353 int t1, n1;
Anson Huang749e8be2014-02-12 18:06:35 +0800354 u64 temp64;
Shawn Guoca3de462013-06-24 14:30:44 +0800355
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100356 if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
Shawn Guoca3de462013-06-24 14:30:44 +0800357 dev_err(&pdev->dev, "invalid sensor calibration data\n");
358 return -EINVAL;
359 }
360
361 /*
362 * Sensor data layout:
363 * [31:20] - sensor value @ 25C
Anson Huang749e8be2014-02-12 18:06:35 +0800364 * Use universal formula now and only need sensor value @ 25C
365 * slope = 0.4297157 - (0.0015976 * 25C fuse)
Shawn Guoca3de462013-06-24 14:30:44 +0800366 */
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100367 n1 = ocotp_ana1 >> 20;
Shawn Guoca3de462013-06-24 14:30:44 +0800368 t1 = 25; /* t1 always 25C */
369
370 /*
Anson Huang749e8be2014-02-12 18:06:35 +0800371 * Derived from linear interpolation:
372 * slope = 0.4297157 - (0.0015976 * 25C fuse)
373 * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
374 * (Nmeas - n1) / (Tmeas - t1) = slope
Shawn Guoca3de462013-06-24 14:30:44 +0800375 * We want to reduce this down to the minimum computation necessary
376 * for each temperature read. Also, we want Tmeas in millicelsius
377 * and we don't want to lose precision from integer division. So...
Anson Huang749e8be2014-02-12 18:06:35 +0800378 * Tmeas = (Nmeas - n1) / slope + t1
379 * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
380 * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
381 * Let constant c1 = (-1000 / slope)
382 * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
383 * Let constant c2 = n1 *c1 + 1000 * t1
384 * milli_Tmeas = c2 - Nmeas * c1
Shawn Guoca3de462013-06-24 14:30:44 +0800385 */
Anson Huang749e8be2014-02-12 18:06:35 +0800386 temp64 = FACTOR0;
387 temp64 *= 1000;
388 do_div(temp64, FACTOR1 * n1 - FACTOR2);
389 data->c1 = temp64;
390 data->c2 = n1 * data->c1 + 1000 * t1;
Shawn Guoca3de462013-06-24 14:30:44 +0800391
Leonard Crestezae621552017-07-14 17:11:08 +0300392 return 0;
393}
394
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100395static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
Leonard Crestezae621552017-07-14 17:11:08 +0300396{
397 struct imx_thermal_data *data = platform_get_drvdata(pdev);
Tim Harveya2291ba2015-11-19 06:49:40 -0800398
399 /* The maximum die temp is specified by the Temperature Grade */
Uwe Kleine-Könige4bb2242017-11-30 10:17:35 +0100400 switch ((ocotp_mem0 >> 6) & 0x3) {
Tim Harveya2291ba2015-11-19 06:49:40 -0800401 case 0: /* Commercial (0 to 95C) */
402 data->temp_grade = "Commercial";
403 data->temp_max = 95000;
404 break;
405 case 1: /* Extended Commercial (-20 to 105C) */
406 data->temp_grade = "Extended Commercial";
407 data->temp_max = 105000;
408 break;
409 case 2: /* Industrial (-40 to 105C) */
410 data->temp_grade = "Industrial";
411 data->temp_max = 105000;
412 break;
413 case 3: /* Automotive (-40 to 125C) */
414 data->temp_grade = "Automotive";
415 data->temp_max = 125000;
416 break;
417 }
Philipp Zabel017e5142013-08-01 18:33:11 +0200418
419 /*
Tim Harveya2291ba2015-11-19 06:49:40 -0800420 * Set the critical trip point at 5C under max
421 * Set the passive trip point at 10C under max (can change via sysfs)
Philipp Zabel017e5142013-08-01 18:33:11 +0200422 */
Tim Harveya2291ba2015-11-19 06:49:40 -0800423 data->temp_critical = data->temp_max - (1000 * 5);
424 data->temp_passive = data->temp_max - (1000 * 10);
Leonard Crestezae621552017-07-14 17:11:08 +0300425}
426
427static int imx_init_from_tempmon_data(struct platform_device *pdev)
428{
429 struct regmap *map;
430 int ret;
431 u32 val;
432
433 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
434 "fsl,tempmon-data");
435 if (IS_ERR(map)) {
436 ret = PTR_ERR(map);
437 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
438 return ret;
439 }
440
441 ret = regmap_read(map, OCOTP_ANA1, &val);
442 if (ret) {
443 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
444 return ret;
445 }
446 ret = imx_init_calib(pdev, val);
447 if (ret)
448 return ret;
449
450 ret = regmap_read(map, OCOTP_MEM0, &val);
451 if (ret) {
452 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
453 return ret;
454 }
455 imx_init_temp_grade(pdev, val);
456
457 return 0;
458}
459
460static int imx_init_from_nvmem_cells(struct platform_device *pdev)
461{
462 int ret;
463 u32 val;
464
465 ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
466 if (ret)
467 return ret;
468 imx_init_calib(pdev, val);
469
470 ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
471 if (ret)
472 return ret;
473 imx_init_temp_grade(pdev, val);
Philipp Zabel017e5142013-08-01 18:33:11 +0200474
Shawn Guoca3de462013-06-24 14:30:44 +0800475 return 0;
476}
477
Philipp Zabel37713a12013-08-01 18:33:12 +0200478static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
479{
480 struct imx_thermal_data *data = dev;
481
482 disable_irq_nosync(irq);
483 data->irq_enabled = false;
484
485 return IRQ_WAKE_THREAD;
486}
487
488static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
489{
490 struct imx_thermal_data *data = dev;
491
Sascha Hauer17e83512015-07-24 08:12:54 +0200492 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
Philipp Zabel37713a12013-08-01 18:33:12 +0200493 data->alarm_temp / 1000);
494
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700495 thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
Philipp Zabel37713a12013-08-01 18:33:12 +0200496
497 return IRQ_HANDLED;
498}
499
Anson Huang3c94f172014-08-06 15:12:09 +0800500static const struct of_device_id of_imx_thermal_match[] = {
501 { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
502 { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
503 { /* end */ }
504};
505MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
506
Shawn Guoca3de462013-06-24 14:30:44 +0800507static int imx_thermal_probe(struct platform_device *pdev)
508{
509 struct imx_thermal_data *data;
Shawn Guoca3de462013-06-24 14:30:44 +0800510 struct regmap *map;
Philipp Zabel37713a12013-08-01 18:33:12 +0200511 int measure_freq;
Shawn Guoca3de462013-06-24 14:30:44 +0800512 int ret;
513
514 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
515 if (!data)
516 return -ENOMEM;
517
518 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
519 if (IS_ERR(map)) {
520 ret = PTR_ERR(map);
521 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
522 return ret;
523 }
524 data->tempmon = map;
525
Corentin LABBE829bc78a2016-08-16 10:51:38 +0200526 data->socdata = of_device_get_match_data(&pdev->dev);
Shailendra Verma8b051ec2017-01-30 10:34:58 +0530527 if (!data->socdata) {
528 dev_err(&pdev->dev, "no device match found\n");
529 return -ENODEV;
530 }
Anson Huang3c94f172014-08-06 15:12:09 +0800531
532 /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
533 if (data->socdata->version == TEMPMON_IMX6SX) {
534 regmap_write(map, MISC1 + REG_CLR, MISC1_IRQ_TEMPHIGH |
535 MISC1_IRQ_TEMPLOW | MISC1_IRQ_TEMPPANIC);
536 /*
537 * reset value of LOW ALARM is incorrect, set it to lowest
538 * value to avoid false trigger of low alarm.
539 */
540 regmap_write(map, TEMPSENSE2 + REG_SET,
541 TEMPSENSE2_LOW_VALUE_MASK);
542 }
543
Philipp Zabel37713a12013-08-01 18:33:12 +0200544 data->irq = platform_get_irq(pdev, 0);
545 if (data->irq < 0)
546 return data->irq;
547
Shawn Guoca3de462013-06-24 14:30:44 +0800548 platform_set_drvdata(pdev, data);
549
Leonard Crestezae621552017-07-14 17:11:08 +0300550 if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
551 ret = imx_init_from_nvmem_cells(pdev);
552 if (ret == -EPROBE_DEFER)
553 return ret;
554 if (ret) {
555 dev_err(&pdev->dev, "failed to init from nvmem: %d\n",
556 ret);
557 return ret;
558 }
559 } else {
560 ret = imx_init_from_tempmon_data(pdev);
561 if (ret) {
562 dev_err(&pdev->dev, "failed to init from from fsl,tempmon-data\n");
563 return ret;
564 }
Shawn Guoca3de462013-06-24 14:30:44 +0800565 }
566
567 /* Make sure sensor is in known good state for measurements */
568 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
569 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
570 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
571 regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
572 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
573
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530574 data->policy = cpufreq_cpu_get(0);
575 if (!data->policy) {
576 pr_debug("%s: CPUFreq policy not found\n", __func__);
577 return -EPROBE_DEFER;
578 }
579
580 data->cdev = cpufreq_cooling_register(data->policy);
Shawn Guoca3de462013-06-24 14:30:44 +0800581 if (IS_ERR(data->cdev)) {
582 ret = PTR_ERR(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530583 dev_err(&pdev->dev,
584 "failed to register cpufreq cooling device: %d\n", ret);
585 cpufreq_cpu_put(data->policy);
Shawn Guoca3de462013-06-24 14:30:44 +0800586 return ret;
587 }
588
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100589 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
590 if (IS_ERR(data->thermal_clk)) {
591 ret = PTR_ERR(data->thermal_clk);
592 if (ret != -EPROBE_DEFER)
593 dev_err(&pdev->dev,
594 "failed to get thermal clk: %d\n", ret);
595 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530596 cpufreq_cpu_put(data->policy);
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100597 return ret;
598 }
599
600 /*
601 * Thermal sensor needs clk on to get correct value, normally
602 * we should enable its clk before taking measurement and disable
603 * clk after measurement is done, but if alarm function is enabled,
604 * hardware will auto measure the temperature periodically, so we
605 * need to keep the clk always on for alarm function.
606 */
607 ret = clk_prepare_enable(data->thermal_clk);
608 if (ret) {
609 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
610 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530611 cpufreq_cpu_put(data->policy);
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100612 return ret;
613 }
614
Shawn Guoca3de462013-06-24 14:30:44 +0800615 data->tz = thermal_zone_device_register("imx_thermal_zone",
Philipp Zabel017e5142013-08-01 18:33:11 +0200616 IMX_TRIP_NUM,
617 BIT(IMX_TRIP_PASSIVE), data,
Shawn Guoca3de462013-06-24 14:30:44 +0800618 &imx_tz_ops, NULL,
619 IMX_PASSIVE_DELAY,
620 IMX_POLLING_DELAY);
621 if (IS_ERR(data->tz)) {
622 ret = PTR_ERR(data->tz);
623 dev_err(&pdev->dev,
624 "failed to register thermal zone device %d\n", ret);
Heiner Kallweit90a21ff2014-11-08 20:35:54 +0100625 clk_disable_unprepare(data->thermal_clk);
Shawn Guoca3de462013-06-24 14:30:44 +0800626 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530627 cpufreq_cpu_put(data->policy);
Shawn Guoca3de462013-06-24 14:30:44 +0800628 return ret;
629 }
630
Tim Harveya2291ba2015-11-19 06:49:40 -0800631 dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
632 " critical:%dC passive:%dC\n", data->temp_grade,
633 data->temp_max / 1000, data->temp_critical / 1000,
634 data->temp_passive / 1000);
635
Philipp Zabel37713a12013-08-01 18:33:12 +0200636 /* Enable measurements at ~ 10 Hz */
637 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
638 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
639 regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
640 imx_set_alarm_temp(data, data->temp_passive);
Anson Huang3c94f172014-08-06 15:12:09 +0800641
642 if (data->socdata->version == TEMPMON_IMX6SX)
643 imx_set_panic_temp(data, data->temp_critical);
644
Philipp Zabel37713a12013-08-01 18:33:12 +0200645 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
646 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
647
Bai Ping84866ee2015-09-14 19:09:51 +0800648 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
649 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
650 0, "imx_thermal", data);
651 if (ret < 0) {
652 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
653 clk_disable_unprepare(data->thermal_clk);
654 thermal_zone_device_unregister(data->tz);
655 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530656 cpufreq_cpu_put(data->policy);
Bai Ping84866ee2015-09-14 19:09:51 +0800657 return ret;
658 }
659
Philipp Zabel37713a12013-08-01 18:33:12 +0200660 data->irq_enabled = true;
Shawn Guoca3de462013-06-24 14:30:44 +0800661 data->mode = THERMAL_DEVICE_ENABLED;
662
663 return 0;
664}
665
666static int imx_thermal_remove(struct platform_device *pdev)
667{
668 struct imx_thermal_data *data = platform_get_drvdata(pdev);
Philipp Zabel37713a12013-08-01 18:33:12 +0200669 struct regmap *map = data->tempmon;
670
671 /* Disable measurements */
672 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
Anson Huang329fe7b2013-12-23 15:49:22 -0500673 if (!IS_ERR(data->thermal_clk))
674 clk_disable_unprepare(data->thermal_clk);
Shawn Guoca3de462013-06-24 14:30:44 +0800675
676 thermal_zone_device_unregister(data->tz);
677 cpufreq_cooling_unregister(data->cdev);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530678 cpufreq_cpu_put(data->policy);
Shawn Guoca3de462013-06-24 14:30:44 +0800679
680 return 0;
681}
682
683#ifdef CONFIG_PM_SLEEP
684static int imx_thermal_suspend(struct device *dev)
685{
686 struct imx_thermal_data *data = dev_get_drvdata(dev);
687 struct regmap *map = data->tempmon;
Shawn Guoca3de462013-06-24 14:30:44 +0800688
Anson Huangb46cce52013-12-24 09:43:24 -0500689 /*
690 * Need to disable thermal sensor, otherwise, when thermal core
691 * try to get temperature before thermal sensor resume, a wrong
692 * temperature will be read as the thermal sensor is powered
693 * down.
694 */
695 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
696 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
697 data->mode = THERMAL_DEVICE_DISABLED;
Anson Huangd26eef82015-01-06 18:50:22 +0800698 clk_disable_unprepare(data->thermal_clk);
Shawn Guoca3de462013-06-24 14:30:44 +0800699
700 return 0;
701}
702
703static int imx_thermal_resume(struct device *dev)
704{
Anson Huangb46cce52013-12-24 09:43:24 -0500705 struct imx_thermal_data *data = dev_get_drvdata(dev);
706 struct regmap *map = data->tempmon;
Arvind Yadave3bdc8d2017-06-06 15:12:37 +0530707 int ret;
Anson Huangb46cce52013-12-24 09:43:24 -0500708
Arvind Yadave3bdc8d2017-06-06 15:12:37 +0530709 ret = clk_prepare_enable(data->thermal_clk);
710 if (ret)
711 return ret;
Anson Huangb46cce52013-12-24 09:43:24 -0500712 /* Enabled thermal sensor after resume */
713 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
714 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
715 data->mode = THERMAL_DEVICE_ENABLED;
716
Shawn Guoca3de462013-06-24 14:30:44 +0800717 return 0;
718}
719#endif
720
721static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
722 imx_thermal_suspend, imx_thermal_resume);
723
Shawn Guoca3de462013-06-24 14:30:44 +0800724static struct platform_driver imx_thermal = {
725 .driver = {
726 .name = "imx_thermal",
Shawn Guoca3de462013-06-24 14:30:44 +0800727 .pm = &imx_thermal_pm_ops,
728 .of_match_table = of_imx_thermal_match,
729 },
730 .probe = imx_thermal_probe,
731 .remove = imx_thermal_remove,
732};
733module_platform_driver(imx_thermal);
734
735MODULE_AUTHOR("Freescale Semiconductor, Inc.");
736MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
737MODULE_LICENSE("GPL v2");
738MODULE_ALIAS("platform:imx-thermal");