Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2//
  3// Copyright 2013 Freescale Semiconductor, Inc.
  4
  5#include <linux/clk.h>
  6#include <linux/cpufreq.h>
  7#include <linux/cpu_cooling.h>
  8#include <linux/delay.h>
  9#include <linux/interrupt.h>
 10#include <linux/io.h>
 11#include <linux/mfd/syscon.h>
 12#include <linux/module.h>
 13#include <linux/of.h>
 14#include <linux/of_device.h>
 15#include <linux/regmap.h>
 16#include <linux/thermal.h>
 17#include <linux/nvmem-consumer.h>
 
 18
 19#define REG_SET		0x4
 20#define REG_CLR		0x8
 21#define REG_TOG		0xc
 22
 23/* 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)
 28/* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
 29#define IMX6_MISC1_IRQ_TEMPLOW			(1 << 28)
 30#define IMX6_MISC1_IRQ_TEMPPANIC		(1 << 27)
 31
 32#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)
 40
 41#define IMX6_TEMPSENSE1				0x0190
 42#define IMX6_TEMPSENSE1_MEASURE_FREQ		0xffff
 43#define IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT	0
 44
 45#define OCOTP_MEM0			0x0480
 46#define OCOTP_ANA1			0x04e0
 47
 48/* 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
 74/* 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
 81#define IMX_POLLING_DELAY		2000 /* millisecond */
 82#define IMX_PASSIVE_DELAY		1000
 83
 84#define TEMPMON_IMX6Q			1
 85#define TEMPMON_IMX6SX			2
 86#define TEMPMON_IMX7D			3
 87
 88struct thermal_soc_data {
 89	u32 version;
 90
 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;
115};
116
117static struct thermal_soc_data thermal_imx6q_data = {
118	.version = TEMPMON_IMX6Q,
119
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,
136};
137
138static struct thermal_soc_data thermal_imx6sx_data = {
139	.version = TEMPMON_IMX6SX,
140
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,
194};
195
196struct imx_thermal_data {
 
197	struct cpufreq_policy *policy;
198	struct thermal_zone_device *tz;
199	struct thermal_cooling_device *cdev;
200	struct regmap *tempmon;
201	u32 c1, c2; /* See formula in imx_init_calib() */
202	int temp_passive;
203	int temp_critical;
204	int temp_max;
205	int alarm_temp;
206	int last_temp;
207	bool irq_enabled;
208	int irq;
209	struct clk *thermal_clk;
210	const struct thermal_soc_data *socdata;
211	const char *temp_grade;
212};
213
214static void imx_set_panic_temp(struct imx_thermal_data *data,
215			       int panic_temp)
216{
217	const struct thermal_soc_data *soc_data = data->socdata;
218	struct regmap *map = data->tempmon;
219	int critical_value;
220
221	critical_value = (data->c2 - panic_temp) / data->c1;
222
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);
227}
228
229static void imx_set_alarm_temp(struct imx_thermal_data *data,
230			       int alarm_temp)
231{
232	struct regmap *map = data->tempmon;
233	const struct thermal_soc_data *soc_data = data->socdata;
234	int alarm_value;
235
236	data->alarm_temp = alarm_temp;
237
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);
247}
248
249static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
250{
251	struct imx_thermal_data *data = tz->devdata;
252	const struct thermal_soc_data *soc_data = data->socdata;
253	struct regmap *map = data->tempmon;
254	unsigned int n_meas;
255	bool wait, run_measurement;
256	u32 val;
 
257
258	run_measurement = !data->irq_enabled;
259	if (!run_measurement) {
260		/* Check if a measurement is currently in progress */
261		regmap_read(map, soc_data->temp_data, &val);
262		wait = !(val & soc_data->temp_valid_mask);
263	} else {
264		/*
265		 * Every time we measure the temperature, we will power on the
266		 * temperature sensor, enable measurements, take a reading,
267		 * disable measurements, power off the temperature sensor.
268		 */
269		regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
270			    soc_data->power_down_mask);
271		regmap_write(map, soc_data->sensor_ctrl + REG_SET,
272			    soc_data->measure_temp_mask);
273
274		wait = true;
275	}
276
277	/*
278	 * According to the temp sensor designers, it may require up to ~17us
279	 * to complete a measurement.
280	 */
281	if (wait)
282		usleep_range(20, 50);
283
284	regmap_read(map, soc_data->temp_data, &val);
285
286	if (run_measurement) {
287		regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
288			     soc_data->measure_temp_mask);
289		regmap_write(map, soc_data->sensor_ctrl + REG_SET,
290			     soc_data->power_down_mask);
291	}
292
293	if ((val & soc_data->temp_valid_mask) == 0) {
294		dev_dbg(&tz->device, "temp measurement never finished\n");
295		return -EAGAIN;
296	}
297
298	n_meas = (val & soc_data->temp_value_mask)
299		>> soc_data->temp_value_shift;
300
301	/* See imx_init_calib() for formula derivation */
302	if (data->socdata->version == TEMPMON_IMX7D)
303		*temp = (n_meas - data->c1 + 25) * 1000;
304	else
305		*temp = data->c2 - n_meas * data->c1;
306
307	/* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
308	if (data->socdata->version == TEMPMON_IMX6Q) {
309		if (data->alarm_temp == data->temp_passive &&
310			*temp >= data->temp_passive)
311			imx_set_alarm_temp(data, data->temp_critical);
312		if (data->alarm_temp == data->temp_critical &&
313			*temp < data->temp_passive) {
314			imx_set_alarm_temp(data, data->temp_passive);
315			dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
316				data->alarm_temp / 1000);
317		}
318	}
319
320	if (*temp != data->last_temp) {
321		dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
322		data->last_temp = *temp;
323	}
324
325	/* Reenable alarm IRQ if temperature below alarm temperature */
326	if (!data->irq_enabled && *temp < data->alarm_temp) {
327		data->irq_enabled = true;
328		enable_irq(data->irq);
329	}
330
 
 
331	return 0;
332}
333
334static int imx_change_mode(struct thermal_zone_device *tz,
335			   enum thermal_device_mode mode)
336{
337	struct imx_thermal_data *data = tz->devdata;
338	struct regmap *map = data->tempmon;
339	const struct thermal_soc_data *soc_data = data->socdata;
340
341	if (mode == THERMAL_DEVICE_ENABLED) {
342		regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
343			     soc_data->power_down_mask);
344		regmap_write(map, soc_data->sensor_ctrl + REG_SET,
345			     soc_data->measure_temp_mask);
346
347		if (!data->irq_enabled) {
348			data->irq_enabled = true;
349			enable_irq(data->irq);
350		}
351	} else {
352		regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
353			     soc_data->measure_temp_mask);
354		regmap_write(map, soc_data->sensor_ctrl + REG_SET,
355			     soc_data->power_down_mask);
356
357		if (data->irq_enabled) {
358			disable_irq(data->irq);
359			data->irq_enabled = false;
360		}
361	}
362
363	return 0;
364}
365
366static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
367			     enum thermal_trip_type *type)
368{
369	*type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
370					     THERMAL_TRIP_CRITICAL;
371	return 0;
372}
373
374static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
375{
376	struct imx_thermal_data *data = tz->devdata;
377
378	*temp = data->temp_critical;
379	return 0;
380}
381
382static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
383			     int *temp)
384{
385	struct imx_thermal_data *data = tz->devdata;
386
387	*temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
388					     data->temp_critical;
389	return 0;
390}
391
392static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
393			     int temp)
394{
395	struct imx_thermal_data *data = tz->devdata;
 
 
 
 
 
396
397	/* do not allow changing critical threshold */
398	if (trip == IMX_TRIP_CRITICAL)
399		return -EPERM;
400
401	/* do not allow passive to be set higher than critical */
402	if (temp < 0 || temp > data->temp_critical)
403		return -EINVAL;
404
405	data->temp_passive = temp;
406
407	imx_set_alarm_temp(data, temp);
408
 
 
409	return 0;
410}
411
412static int imx_bind(struct thermal_zone_device *tz,
413		    struct thermal_cooling_device *cdev)
414{
415	int ret;
416
417	ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
418					       THERMAL_NO_LIMIT,
419					       THERMAL_NO_LIMIT,
420					       THERMAL_WEIGHT_DEFAULT);
421	if (ret) {
422		dev_err(&tz->device,
423			"binding zone %s with cdev %s failed:%d\n",
424			tz->type, cdev->type, ret);
425		return ret;
426	}
427
428	return 0;
429}
430
431static int imx_unbind(struct thermal_zone_device *tz,
432		      struct thermal_cooling_device *cdev)
433{
434	int ret;
435
436	ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
437	if (ret) {
438		dev_err(&tz->device,
439			"unbinding zone %s with cdev %s failed:%d\n",
440			tz->type, cdev->type, ret);
441		return ret;
442	}
443
444	return 0;
445}
446
447static struct thermal_zone_device_ops imx_tz_ops = {
448	.bind = imx_bind,
449	.unbind = imx_unbind,
450	.get_temp = imx_get_temp,
451	.change_mode = imx_change_mode,
452	.get_trip_type = imx_get_trip_type,
453	.get_trip_temp = imx_get_trip_temp,
454	.get_crit_temp = imx_get_crit_temp,
455	.set_trip_temp = imx_set_trip_temp,
456};
457
458static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
459{
460	struct imx_thermal_data *data = platform_get_drvdata(pdev);
461	int n1;
462	u64 temp64;
463
464	if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
465		dev_err(&pdev->dev, "invalid sensor calibration data\n");
466		return -EINVAL;
467	}
468
469	/*
470	 * On i.MX7D, we only use the calibration data at 25C to get the temp,
471	 * Tmeas = ( Nmeas - n1) + 25; n1 is the fuse value for 25C.
472	 */
473	if (data->socdata->version == TEMPMON_IMX7D) {
474		data->c1 = (ocotp_ana1 >> 9) & 0x1ff;
475		return 0;
476	}
477
478	/*
479	 * The sensor is calibrated at 25 °C (aka T1) and the value measured
480	 * (aka N1) at this temperature is provided in bits [31:20] in the
481	 * i.MX's OCOTP value ANA1.
482	 * To find the actual temperature T, the following formula has to be used
483	 * when reading value n from the sensor:
484	 *
485	 * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C
486	 *   = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C
487	 *   = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C
488	 *   = c2 - c1 * N
489	 *
490	 * with
491	 *
492	 *  T1' = 28.580661 °C
493	 *   c1 = 1 / (0.0015423 * N1 - 0.4297157) °C
494	 *   c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C
495	 *      = T1' + N1 * c1
496	 */
497	n1 = ocotp_ana1 >> 20;
498
499	temp64 = 10000000; /* use 10^7 as fixed point constant for values in formula */
500	temp64 *= 1000; /* to get result in °mC */
501	do_div(temp64, 15423 * n1 - 4148468);
502	data->c1 = temp64;
503	data->c2 = n1 * data->c1 + 28581;
504
505	return 0;
506}
507
508static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
509{
510	struct imx_thermal_data *data = platform_get_drvdata(pdev);
511
512	/* The maximum die temp is specified by the Temperature Grade */
513	switch ((ocotp_mem0 >> 6) & 0x3) {
514	case 0: /* Commercial (0 to 95 °C) */
515		data->temp_grade = "Commercial";
516		data->temp_max = 95000;
517		break;
518	case 1: /* Extended Commercial (-20 °C to 105 °C) */
519		data->temp_grade = "Extended Commercial";
520		data->temp_max = 105000;
521		break;
522	case 2: /* Industrial (-40 °C to 105 °C) */
523		data->temp_grade = "Industrial";
524		data->temp_max = 105000;
525		break;
526	case 3: /* Automotive (-40 °C to 125 °C) */
527		data->temp_grade = "Automotive";
528		data->temp_max = 125000;
529		break;
530	}
531
532	/*
533	 * Set the critical trip point at 5 °C under max
534	 * Set the passive trip point at 10 °C under max (changeable via sysfs)
535	 */
536	data->temp_critical = data->temp_max - (1000 * 5);
537	data->temp_passive = data->temp_max - (1000 * 10);
538}
539
540static int imx_init_from_tempmon_data(struct platform_device *pdev)
541{
542	struct regmap *map;
543	int ret;
544	u32 val;
545
546	map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
547					      "fsl,tempmon-data");
548	if (IS_ERR(map)) {
549		ret = PTR_ERR(map);
550		dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
551		return ret;
552	}
553
554	ret = regmap_read(map, OCOTP_ANA1, &val);
555	if (ret) {
556		dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
557		return ret;
558	}
559	ret = imx_init_calib(pdev, val);
560	if (ret)
561		return ret;
562
563	ret = regmap_read(map, OCOTP_MEM0, &val);
564	if (ret) {
565		dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
566		return ret;
567	}
568	imx_init_temp_grade(pdev, val);
569
570	return 0;
571}
572
573static int imx_init_from_nvmem_cells(struct platform_device *pdev)
574{
575	int ret;
576	u32 val;
577
578	ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
579	if (ret)
580		return ret;
581
582	ret = imx_init_calib(pdev, val);
583	if (ret)
584		return ret;
585
586	ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
587	if (ret)
588		return ret;
589	imx_init_temp_grade(pdev, val);
590
591	return 0;
592}
593
594static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
595{
596	struct imx_thermal_data *data = dev;
597
598	disable_irq_nosync(irq);
599	data->irq_enabled = false;
600
601	return IRQ_WAKE_THREAD;
602}
603
604static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
605{
606	struct imx_thermal_data *data = dev;
607
608	dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
609		data->alarm_temp / 1000);
610
611	thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
612
613	return IRQ_HANDLED;
614}
615
616static const struct of_device_id of_imx_thermal_match[] = {
617	{ .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
618	{ .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
619	{ .compatible = "fsl,imx7d-tempmon", .data = &thermal_imx7d_data, },
620	{ /* end */ }
621};
622MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
623
624#ifdef CONFIG_CPU_FREQ
625/*
626 * Create cooling device in case no #cooling-cells property is available in
627 * CPU node
628 */
629static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
630{
631	struct device_node *np;
632	int ret = 0;
633
634	data->policy = cpufreq_cpu_get(0);
635	if (!data->policy) {
636		pr_debug("%s: CPUFreq policy not found\n", __func__);
637		return -EPROBE_DEFER;
638	}
639
640	np = of_get_cpu_node(data->policy->cpu, NULL);
641
642	if (!np || !of_find_property(np, "#cooling-cells", NULL)) {
643		data->cdev = cpufreq_cooling_register(data->policy);
644		if (IS_ERR(data->cdev)) {
645			ret = PTR_ERR(data->cdev);
646			cpufreq_cpu_put(data->policy);
647		}
648	}
649
650	of_node_put(np);
651
652	return ret;
653}
654
655static void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data)
656{
657	cpufreq_cooling_unregister(data->cdev);
658	cpufreq_cpu_put(data->policy);
659}
660
661#else
662
663static inline int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
664{
665	return 0;
666}
667
668static inline void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data)
669{
670}
671#endif
672
673static int imx_thermal_probe(struct platform_device *pdev)
674{
675	struct imx_thermal_data *data;
676	struct regmap *map;
677	int measure_freq;
678	int ret;
679
680	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
681	if (!data)
682		return -ENOMEM;
683
 
 
684	map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
685	if (IS_ERR(map)) {
686		ret = PTR_ERR(map);
687		dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
688		return ret;
689	}
690	data->tempmon = map;
691
692	data->socdata = of_device_get_match_data(&pdev->dev);
693	if (!data->socdata) {
694		dev_err(&pdev->dev, "no device match found\n");
695		return -ENODEV;
696	}
697
698	/* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
699	if (data->socdata->version == TEMPMON_IMX6SX) {
700		regmap_write(map, IMX6_MISC1 + REG_CLR,
701			IMX6_MISC1_IRQ_TEMPHIGH | IMX6_MISC1_IRQ_TEMPLOW
702			| IMX6_MISC1_IRQ_TEMPPANIC);
703		/*
704		 * reset value of LOW ALARM is incorrect, set it to lowest
705		 * value to avoid false trigger of low alarm.
706		 */
707		regmap_write(map, data->socdata->low_alarm_ctrl + REG_SET,
708			     data->socdata->low_alarm_mask);
709	}
710
711	data->irq = platform_get_irq(pdev, 0);
712	if (data->irq < 0)
713		return data->irq;
714
715	platform_set_drvdata(pdev, data);
716
717	if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
718		ret = imx_init_from_nvmem_cells(pdev);
719		if (ret) {
720			if (ret == -EPROBE_DEFER)
721				return ret;
722
723			dev_err(&pdev->dev, "failed to init from nvmem: %d\n",
724				ret);
725			return ret;
726		}
727	} else {
728		ret = imx_init_from_tempmon_data(pdev);
729		if (ret) {
730			dev_err(&pdev->dev, "failed to init from fsl,tempmon-data\n");
731			return ret;
732		}
733	}
734
735	/* Make sure sensor is in known good state for measurements */
736	regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
737		     data->socdata->power_down_mask);
738	regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
739		     data->socdata->measure_temp_mask);
740	regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
741		     data->socdata->measure_freq_mask);
742	if (data->socdata->version != TEMPMON_IMX7D)
743		regmap_write(map, IMX6_MISC0 + REG_SET,
744			IMX6_MISC0_REFTOP_SELBIASOFF);
745	regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
746		     data->socdata->power_down_mask);
747
748	ret = imx_thermal_register_legacy_cooling(data);
749	if (ret) {
750		if (ret == -EPROBE_DEFER)
751			return ret;
752
753		dev_err(&pdev->dev,
754			"failed to register cpufreq cooling device: %d\n", ret);
755		return ret;
756	}
757
758	data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
759	if (IS_ERR(data->thermal_clk)) {
760		ret = PTR_ERR(data->thermal_clk);
761		if (ret != -EPROBE_DEFER)
762			dev_err(&pdev->dev,
763				"failed to get thermal clk: %d\n", ret);
764		goto legacy_cleanup;
765	}
766
767	/*
768	 * Thermal sensor needs clk on to get correct value, normally
769	 * we should enable its clk before taking measurement and disable
770	 * clk after measurement is done, but if alarm function is enabled,
771	 * hardware will auto measure the temperature periodically, so we
772	 * need to keep the clk always on for alarm function.
773	 */
774	ret = clk_prepare_enable(data->thermal_clk);
775	if (ret) {
776		dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
777		goto legacy_cleanup;
778	}
779
780	data->tz = thermal_zone_device_register("imx_thermal_zone",
781						IMX_TRIP_NUM,
782						BIT(IMX_TRIP_PASSIVE), data,
783						&imx_tz_ops, NULL,
784						IMX_PASSIVE_DELAY,
785						IMX_POLLING_DELAY);
786	if (IS_ERR(data->tz)) {
787		ret = PTR_ERR(data->tz);
788		dev_err(&pdev->dev,
789			"failed to register thermal zone device %d\n", ret);
790		goto clk_disable;
791	}
792
793	dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
794		 " critical:%dC passive:%dC\n", data->temp_grade,
795		 data->temp_max / 1000, data->temp_critical / 1000,
796		 data->temp_passive / 1000);
797
798	/* Enable measurements at ~ 10 Hz */
799	regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
800		     data->socdata->measure_freq_mask);
801	measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
802	regmap_write(map, data->socdata->measure_freq_ctrl + REG_SET,
803		     measure_freq << data->socdata->measure_freq_shift);
804	imx_set_alarm_temp(data, data->temp_passive);
805
806	if (data->socdata->version == TEMPMON_IMX6SX)
807		imx_set_panic_temp(data, data->temp_critical);
808
809	regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
810		     data->socdata->power_down_mask);
811	regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
812		     data->socdata->measure_temp_mask);
 
 
 
 
 
 
 
 
 
 
813
814	data->irq_enabled = true;
815	ret = thermal_zone_device_enable(data->tz);
816	if (ret)
817		goto thermal_zone_unregister;
818
819	ret = devm_request_threaded_irq(&pdev->dev, data->irq,
820			imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
821			0, "imx_thermal", data);
822	if (ret < 0) {
823		dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
824		goto thermal_zone_unregister;
825	}
826
 
 
827	return 0;
828
829thermal_zone_unregister:
830	thermal_zone_device_unregister(data->tz);
 
 
 
831clk_disable:
832	clk_disable_unprepare(data->thermal_clk);
833legacy_cleanup:
834	imx_thermal_unregister_legacy_cooling(data);
835
836	return ret;
837}
838
839static int imx_thermal_remove(struct platform_device *pdev)
840{
841	struct imx_thermal_data *data = platform_get_drvdata(pdev);
842	struct regmap *map = data->tempmon;
843
844	/* Disable measurements */
845	regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
846		     data->socdata->power_down_mask);
847	if (!IS_ERR(data->thermal_clk))
848		clk_disable_unprepare(data->thermal_clk);
849
850	thermal_zone_device_unregister(data->tz);
851	imx_thermal_unregister_legacy_cooling(data);
852
853	return 0;
854}
855
856static int __maybe_unused imx_thermal_suspend(struct device *dev)
857{
858	struct imx_thermal_data *data = dev_get_drvdata(dev);
859	int ret;
860
861	/*
862	 * Need to disable thermal sensor, otherwise, when thermal core
863	 * try to get temperature before thermal sensor resume, a wrong
864	 * temperature will be read as the thermal sensor is powered
865	 * down. This is done in change_mode() operation called from
866	 * thermal_zone_device_disable()
867	 */
868	ret = thermal_zone_device_disable(data->tz);
869	if (ret)
870		return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
871	clk_disable_unprepare(data->thermal_clk);
872
873	return 0;
874}
875
876static int __maybe_unused imx_thermal_resume(struct device *dev)
877{
878	struct imx_thermal_data *data = dev_get_drvdata(dev);
 
 
879	int ret;
880
881	ret = clk_prepare_enable(data->thermal_clk);
882	if (ret)
883		return ret;
884	/* Enabled thermal sensor after resume */
885	ret = thermal_zone_device_enable(data->tz);
 
886	if (ret)
887		return ret;
888
 
 
 
 
 
 
 
 
 
 
 
889	return 0;
890}
891
892static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
893			 imx_thermal_suspend, imx_thermal_resume);
 
 
 
894
895static struct platform_driver imx_thermal = {
896	.driver = {
897		.name	= "imx_thermal",
898		.pm	= &imx_thermal_pm_ops,
899		.of_match_table = of_imx_thermal_match,
900	},
901	.probe		= imx_thermal_probe,
902	.remove		= imx_thermal_remove,
903};
904module_platform_driver(imx_thermal);
905
906MODULE_AUTHOR("Freescale Semiconductor, Inc.");
907MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
908MODULE_LICENSE("GPL v2");
909MODULE_ALIAS("platform:imx-thermal");
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2//
  3// Copyright 2013 Freescale Semiconductor, Inc.
  4
  5#include <linux/clk.h>
  6#include <linux/cpufreq.h>
  7#include <linux/cpu_cooling.h>
  8#include <linux/delay.h>
  9#include <linux/interrupt.h>
 10#include <linux/io.h>
 11#include <linux/mfd/syscon.h>
 12#include <linux/module.h>
 13#include <linux/of.h>
 14#include <linux/of_device.h>
 15#include <linux/regmap.h>
 16#include <linux/thermal.h>
 17#include <linux/nvmem-consumer.h>
 18#include <linux/pm_runtime.h>
 19
 20#define REG_SET		0x4
 21#define REG_CLR		0x8
 22#define REG_TOG		0xc
 23
 24/* i.MX6 specific */
 25#define IMX6_MISC0				0x0150
 26#define IMX6_MISC0_REFTOP_SELBIASOFF		(1 << 3)
 27#define IMX6_MISC1				0x0160
 28#define IMX6_MISC1_IRQ_TEMPHIGH			(1 << 29)
 29/* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
 30#define IMX6_MISC1_IRQ_TEMPLOW			(1 << 28)
 31#define IMX6_MISC1_IRQ_TEMPPANIC		(1 << 27)
 32
 33#define IMX6_TEMPSENSE0				0x0180
 34#define IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT	20
 35#define IMX6_TEMPSENSE0_ALARM_VALUE_MASK	(0xfff << 20)
 36#define IMX6_TEMPSENSE0_TEMP_CNT_SHIFT		8
 37#define IMX6_TEMPSENSE0_TEMP_CNT_MASK		(0xfff << 8)
 38#define IMX6_TEMPSENSE0_FINISHED		(1 << 2)
 39#define IMX6_TEMPSENSE0_MEASURE_TEMP		(1 << 1)
 40#define IMX6_TEMPSENSE0_POWER_DOWN		(1 << 0)
 41
 42#define IMX6_TEMPSENSE1				0x0190
 43#define IMX6_TEMPSENSE1_MEASURE_FREQ		0xffff
 44#define IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT	0
 45
 46#define OCOTP_MEM0			0x0480
 47#define OCOTP_ANA1			0x04e0
 48
 49/* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
 50#define IMX6_TEMPSENSE2				0x0290
 51#define IMX6_TEMPSENSE2_LOW_VALUE_SHIFT		0
 52#define IMX6_TEMPSENSE2_LOW_VALUE_MASK		0xfff
 53#define IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT	16
 54#define IMX6_TEMPSENSE2_PANIC_VALUE_MASK	0xfff0000
 55
 56/* i.MX7 specific */
 57#define IMX7_ANADIG_DIGPROG			0x800
 58#define IMX7_TEMPSENSE0				0x300
 59#define IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT	18
 60#define IMX7_TEMPSENSE0_PANIC_ALARM_MASK	(0x1ff << 18)
 61#define IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT	9
 62#define IMX7_TEMPSENSE0_HIGH_ALARM_MASK		(0x1ff << 9)
 63#define IMX7_TEMPSENSE0_LOW_ALARM_SHIFT		0
 64#define IMX7_TEMPSENSE0_LOW_ALARM_MASK		0x1ff
 65
 66#define IMX7_TEMPSENSE1				0x310
 67#define IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT	16
 68#define IMX7_TEMPSENSE1_MEASURE_FREQ_MASK	(0xffff << 16)
 69#define IMX7_TEMPSENSE1_FINISHED		(1 << 11)
 70#define IMX7_TEMPSENSE1_MEASURE_TEMP		(1 << 10)
 71#define IMX7_TEMPSENSE1_POWER_DOWN		(1 << 9)
 72#define IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT	0
 73#define IMX7_TEMPSENSE1_TEMP_VALUE_MASK		0x1ff
 74
 75/* The driver supports 1 passive trip point and 1 critical trip point */
 76enum imx_thermal_trip {
 77	IMX_TRIP_PASSIVE,
 78	IMX_TRIP_CRITICAL,
 79	IMX_TRIP_NUM,
 80};
 81
 82#define IMX_POLLING_DELAY		2000 /* millisecond */
 83#define IMX_PASSIVE_DELAY		1000
 84
 85#define TEMPMON_IMX6Q			1
 86#define TEMPMON_IMX6SX			2
 87#define TEMPMON_IMX7D			3
 88
 89struct thermal_soc_data {
 90	u32 version;
 91
 92	u32 sensor_ctrl;
 93	u32 power_down_mask;
 94	u32 measure_temp_mask;
 95
 96	u32 measure_freq_ctrl;
 97	u32 measure_freq_mask;
 98	u32 measure_freq_shift;
 99
100	u32 temp_data;
101	u32 temp_value_mask;
102	u32 temp_value_shift;
103	u32 temp_valid_mask;
104
105	u32 panic_alarm_ctrl;
106	u32 panic_alarm_mask;
107	u32 panic_alarm_shift;
108
109	u32 high_alarm_ctrl;
110	u32 high_alarm_mask;
111	u32 high_alarm_shift;
112
113	u32 low_alarm_ctrl;
114	u32 low_alarm_mask;
115	u32 low_alarm_shift;
116};
117
118static struct thermal_soc_data thermal_imx6q_data = {
119	.version = TEMPMON_IMX6Q,
120
121	.sensor_ctrl = IMX6_TEMPSENSE0,
122	.power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
123	.measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
124
125	.measure_freq_ctrl = IMX6_TEMPSENSE1,
126	.measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
127	.measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
128
129	.temp_data = IMX6_TEMPSENSE0,
130	.temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
131	.temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
132	.temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
133
134	.high_alarm_ctrl = IMX6_TEMPSENSE0,
135	.high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
136	.high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
137};
138
139static struct thermal_soc_data thermal_imx6sx_data = {
140	.version = TEMPMON_IMX6SX,
141
142	.sensor_ctrl = IMX6_TEMPSENSE0,
143	.power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
144	.measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
145
146	.measure_freq_ctrl = IMX6_TEMPSENSE1,
147	.measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
148	.measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
149
150	.temp_data = IMX6_TEMPSENSE0,
151	.temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
152	.temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
153	.temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
154
155	.high_alarm_ctrl = IMX6_TEMPSENSE0,
156	.high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
157	.high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
158
159	.panic_alarm_ctrl = IMX6_TEMPSENSE2,
160	.panic_alarm_mask = IMX6_TEMPSENSE2_PANIC_VALUE_MASK,
161	.panic_alarm_shift = IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT,
162
163	.low_alarm_ctrl = IMX6_TEMPSENSE2,
164	.low_alarm_mask = IMX6_TEMPSENSE2_LOW_VALUE_MASK,
165	.low_alarm_shift = IMX6_TEMPSENSE2_LOW_VALUE_SHIFT,
166};
167
168static struct thermal_soc_data thermal_imx7d_data = {
169	.version = TEMPMON_IMX7D,
170
171	.sensor_ctrl = IMX7_TEMPSENSE1,
172	.power_down_mask = IMX7_TEMPSENSE1_POWER_DOWN,
173	.measure_temp_mask = IMX7_TEMPSENSE1_MEASURE_TEMP,
174
175	.measure_freq_ctrl = IMX7_TEMPSENSE1,
176	.measure_freq_shift = IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT,
177	.measure_freq_mask = IMX7_TEMPSENSE1_MEASURE_FREQ_MASK,
178
179	.temp_data = IMX7_TEMPSENSE1,
180	.temp_value_mask = IMX7_TEMPSENSE1_TEMP_VALUE_MASK,
181	.temp_value_shift = IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT,
182	.temp_valid_mask = IMX7_TEMPSENSE1_FINISHED,
183
184	.panic_alarm_ctrl = IMX7_TEMPSENSE1,
185	.panic_alarm_mask = IMX7_TEMPSENSE0_PANIC_ALARM_MASK,
186	.panic_alarm_shift = IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT,
187
188	.high_alarm_ctrl = IMX7_TEMPSENSE0,
189	.high_alarm_mask = IMX7_TEMPSENSE0_HIGH_ALARM_MASK,
190	.high_alarm_shift = IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT,
191
192	.low_alarm_ctrl = IMX7_TEMPSENSE0,
193	.low_alarm_mask = IMX7_TEMPSENSE0_LOW_ALARM_MASK,
194	.low_alarm_shift = IMX7_TEMPSENSE0_LOW_ALARM_SHIFT,
195};
196
197struct imx_thermal_data {
198	struct device *dev;
199	struct cpufreq_policy *policy;
200	struct thermal_zone_device *tz;
201	struct thermal_cooling_device *cdev;
202	struct regmap *tempmon;
203	u32 c1, c2; /* See formula in imx_init_calib() */
204	int temp_passive;
205	int temp_critical;
206	int temp_max;
207	int alarm_temp;
208	int last_temp;
209	bool irq_enabled;
210	int irq;
211	struct clk *thermal_clk;
212	const struct thermal_soc_data *socdata;
213	const char *temp_grade;
214};
215
216static void imx_set_panic_temp(struct imx_thermal_data *data,
217			       int panic_temp)
218{
219	const struct thermal_soc_data *soc_data = data->socdata;
220	struct regmap *map = data->tempmon;
221	int critical_value;
222
223	critical_value = (data->c2 - panic_temp) / data->c1;
224
225	regmap_write(map, soc_data->panic_alarm_ctrl + REG_CLR,
226		     soc_data->panic_alarm_mask);
227	regmap_write(map, soc_data->panic_alarm_ctrl + REG_SET,
228		     critical_value << soc_data->panic_alarm_shift);
229}
230
231static void imx_set_alarm_temp(struct imx_thermal_data *data,
232			       int alarm_temp)
233{
234	struct regmap *map = data->tempmon;
235	const struct thermal_soc_data *soc_data = data->socdata;
236	int alarm_value;
237
238	data->alarm_temp = alarm_temp;
239
240	if (data->socdata->version == TEMPMON_IMX7D)
241		alarm_value = alarm_temp / 1000 + data->c1 - 25;
242	else
243		alarm_value = (data->c2 - alarm_temp) / data->c1;
244
245	regmap_write(map, soc_data->high_alarm_ctrl + REG_CLR,
246		     soc_data->high_alarm_mask);
247	regmap_write(map, soc_data->high_alarm_ctrl + REG_SET,
248		     alarm_value << soc_data->high_alarm_shift);
249}
250
251static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
252{
253	struct imx_thermal_data *data = tz->devdata;
254	const struct thermal_soc_data *soc_data = data->socdata;
255	struct regmap *map = data->tempmon;
256	unsigned int n_meas;
 
257	u32 val;
258	int ret;
259
260	ret = pm_runtime_resume_and_get(data->dev);
261	if (ret < 0)
262		return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
264	regmap_read(map, soc_data->temp_data, &val);
265
 
 
 
 
 
 
 
266	if ((val & soc_data->temp_valid_mask) == 0) {
267		dev_dbg(&tz->device, "temp measurement never finished\n");
268		return -EAGAIN;
269	}
270
271	n_meas = (val & soc_data->temp_value_mask)
272		>> soc_data->temp_value_shift;
273
274	/* See imx_init_calib() for formula derivation */
275	if (data->socdata->version == TEMPMON_IMX7D)
276		*temp = (n_meas - data->c1 + 25) * 1000;
277	else
278		*temp = data->c2 - n_meas * data->c1;
279
280	/* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
281	if (data->socdata->version == TEMPMON_IMX6Q) {
282		if (data->alarm_temp == data->temp_passive &&
283			*temp >= data->temp_passive)
284			imx_set_alarm_temp(data, data->temp_critical);
285		if (data->alarm_temp == data->temp_critical &&
286			*temp < data->temp_passive) {
287			imx_set_alarm_temp(data, data->temp_passive);
288			dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
289				data->alarm_temp / 1000);
290		}
291	}
292
293	if (*temp != data->last_temp) {
294		dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
295		data->last_temp = *temp;
296	}
297
298	/* Reenable alarm IRQ if temperature below alarm temperature */
299	if (!data->irq_enabled && *temp < data->alarm_temp) {
300		data->irq_enabled = true;
301		enable_irq(data->irq);
302	}
303
304	pm_runtime_put(data->dev);
305
306	return 0;
307}
308
309static int imx_change_mode(struct thermal_zone_device *tz,
310			   enum thermal_device_mode mode)
311{
312	struct imx_thermal_data *data = tz->devdata;
 
 
313
314	if (mode == THERMAL_DEVICE_ENABLED) {
315		pm_runtime_get(data->dev);
 
 
 
316
317		if (!data->irq_enabled) {
318			data->irq_enabled = true;
319			enable_irq(data->irq);
320		}
321	} else {
322		pm_runtime_put(data->dev);
 
 
 
323
324		if (data->irq_enabled) {
325			disable_irq(data->irq);
326			data->irq_enabled = false;
327		}
328	}
329
330	return 0;
331}
332
333static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
334			     enum thermal_trip_type *type)
335{
336	*type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
337					     THERMAL_TRIP_CRITICAL;
338	return 0;
339}
340
341static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
342{
343	struct imx_thermal_data *data = tz->devdata;
344
345	*temp = data->temp_critical;
346	return 0;
347}
348
349static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
350			     int *temp)
351{
352	struct imx_thermal_data *data = tz->devdata;
353
354	*temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
355					     data->temp_critical;
356	return 0;
357}
358
359static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
360			     int temp)
361{
362	struct imx_thermal_data *data = tz->devdata;
363	int ret;
364
365	ret = pm_runtime_resume_and_get(data->dev);
366	if (ret < 0)
367		return ret;
368
369	/* do not allow changing critical threshold */
370	if (trip == IMX_TRIP_CRITICAL)
371		return -EPERM;
372
373	/* do not allow passive to be set higher than critical */
374	if (temp < 0 || temp > data->temp_critical)
375		return -EINVAL;
376
377	data->temp_passive = temp;
378
379	imx_set_alarm_temp(data, temp);
380
381	pm_runtime_put(data->dev);
382
383	return 0;
384}
385
386static int imx_bind(struct thermal_zone_device *tz,
387		    struct thermal_cooling_device *cdev)
388{
389	int ret;
390
391	ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
392					       THERMAL_NO_LIMIT,
393					       THERMAL_NO_LIMIT,
394					       THERMAL_WEIGHT_DEFAULT);
395	if (ret) {
396		dev_err(&tz->device,
397			"binding zone %s with cdev %s failed:%d\n",
398			tz->type, cdev->type, ret);
399		return ret;
400	}
401
402	return 0;
403}
404
405static int imx_unbind(struct thermal_zone_device *tz,
406		      struct thermal_cooling_device *cdev)
407{
408	int ret;
409
410	ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
411	if (ret) {
412		dev_err(&tz->device,
413			"unbinding zone %s with cdev %s failed:%d\n",
414			tz->type, cdev->type, ret);
415		return ret;
416	}
417
418	return 0;
419}
420
421static struct thermal_zone_device_ops imx_tz_ops = {
422	.bind = imx_bind,
423	.unbind = imx_unbind,
424	.get_temp = imx_get_temp,
425	.change_mode = imx_change_mode,
426	.get_trip_type = imx_get_trip_type,
427	.get_trip_temp = imx_get_trip_temp,
428	.get_crit_temp = imx_get_crit_temp,
429	.set_trip_temp = imx_set_trip_temp,
430};
431
432static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
433{
434	struct imx_thermal_data *data = platform_get_drvdata(pdev);
435	int n1;
436	u64 temp64;
437
438	if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
439		dev_err(&pdev->dev, "invalid sensor calibration data\n");
440		return -EINVAL;
441	}
442
443	/*
444	 * On i.MX7D, we only use the calibration data at 25C to get the temp,
445	 * Tmeas = ( Nmeas - n1) + 25; n1 is the fuse value for 25C.
446	 */
447	if (data->socdata->version == TEMPMON_IMX7D) {
448		data->c1 = (ocotp_ana1 >> 9) & 0x1ff;
449		return 0;
450	}
451
452	/*
453	 * The sensor is calibrated at 25 °C (aka T1) and the value measured
454	 * (aka N1) at this temperature is provided in bits [31:20] in the
455	 * i.MX's OCOTP value ANA1.
456	 * To find the actual temperature T, the following formula has to be used
457	 * when reading value n from the sensor:
458	 *
459	 * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C
460	 *   = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C
461	 *   = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C
462	 *   = c2 - c1 * N
463	 *
464	 * with
465	 *
466	 *  T1' = 28.580661 °C
467	 *   c1 = 1 / (0.0015423 * N1 - 0.4297157) °C
468	 *   c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C
469	 *      = T1' + N1 * c1
470	 */
471	n1 = ocotp_ana1 >> 20;
472
473	temp64 = 10000000; /* use 10^7 as fixed point constant for values in formula */
474	temp64 *= 1000; /* to get result in °mC */
475	do_div(temp64, 15423 * n1 - 4148468);
476	data->c1 = temp64;
477	data->c2 = n1 * data->c1 + 28581;
478
479	return 0;
480}
481
482static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
483{
484	struct imx_thermal_data *data = platform_get_drvdata(pdev);
485
486	/* The maximum die temp is specified by the Temperature Grade */
487	switch ((ocotp_mem0 >> 6) & 0x3) {
488	case 0: /* Commercial (0 to 95 °C) */
489		data->temp_grade = "Commercial";
490		data->temp_max = 95000;
491		break;
492	case 1: /* Extended Commercial (-20 °C to 105 °C) */
493		data->temp_grade = "Extended Commercial";
494		data->temp_max = 105000;
495		break;
496	case 2: /* Industrial (-40 °C to 105 °C) */
497		data->temp_grade = "Industrial";
498		data->temp_max = 105000;
499		break;
500	case 3: /* Automotive (-40 °C to 125 °C) */
501		data->temp_grade = "Automotive";
502		data->temp_max = 125000;
503		break;
504	}
505
506	/*
507	 * Set the critical trip point at 5 °C under max
508	 * Set the passive trip point at 10 °C under max (changeable via sysfs)
509	 */
510	data->temp_critical = data->temp_max - (1000 * 5);
511	data->temp_passive = data->temp_max - (1000 * 10);
512}
513
514static int imx_init_from_tempmon_data(struct platform_device *pdev)
515{
516	struct regmap *map;
517	int ret;
518	u32 val;
519
520	map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
521					      "fsl,tempmon-data");
522	if (IS_ERR(map)) {
523		ret = PTR_ERR(map);
524		dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
525		return ret;
526	}
527
528	ret = regmap_read(map, OCOTP_ANA1, &val);
529	if (ret) {
530		dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
531		return ret;
532	}
533	ret = imx_init_calib(pdev, val);
534	if (ret)
535		return ret;
536
537	ret = regmap_read(map, OCOTP_MEM0, &val);
538	if (ret) {
539		dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
540		return ret;
541	}
542	imx_init_temp_grade(pdev, val);
543
544	return 0;
545}
546
547static int imx_init_from_nvmem_cells(struct platform_device *pdev)
548{
549	int ret;
550	u32 val;
551
552	ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
553	if (ret)
554		return ret;
555
556	ret = imx_init_calib(pdev, val);
557	if (ret)
558		return ret;
559
560	ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
561	if (ret)
562		return ret;
563	imx_init_temp_grade(pdev, val);
564
565	return 0;
566}
567
568static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
569{
570	struct imx_thermal_data *data = dev;
571
572	disable_irq_nosync(irq);
573	data->irq_enabled = false;
574
575	return IRQ_WAKE_THREAD;
576}
577
578static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
579{
580	struct imx_thermal_data *data = dev;
581
582	dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
583		data->alarm_temp / 1000);
584
585	thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
586
587	return IRQ_HANDLED;
588}
589
590static const struct of_device_id of_imx_thermal_match[] = {
591	{ .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
592	{ .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
593	{ .compatible = "fsl,imx7d-tempmon", .data = &thermal_imx7d_data, },
594	{ /* end */ }
595};
596MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
597
598#ifdef CONFIG_CPU_FREQ
599/*
600 * Create cooling device in case no #cooling-cells property is available in
601 * CPU node
602 */
603static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
604{
605	struct device_node *np;
606	int ret = 0;
607
608	data->policy = cpufreq_cpu_get(0);
609	if (!data->policy) {
610		pr_debug("%s: CPUFreq policy not found\n", __func__);
611		return -EPROBE_DEFER;
612	}
613
614	np = of_get_cpu_node(data->policy->cpu, NULL);
615
616	if (!np || !of_find_property(np, "#cooling-cells", NULL)) {
617		data->cdev = cpufreq_cooling_register(data->policy);
618		if (IS_ERR(data->cdev)) {
619			ret = PTR_ERR(data->cdev);
620			cpufreq_cpu_put(data->policy);
621		}
622	}
623
624	of_node_put(np);
625
626	return ret;
627}
628
629static void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data)
630{
631	cpufreq_cooling_unregister(data->cdev);
632	cpufreq_cpu_put(data->policy);
633}
634
635#else
636
637static inline int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
638{
639	return 0;
640}
641
642static inline void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data)
643{
644}
645#endif
646
647static int imx_thermal_probe(struct platform_device *pdev)
648{
649	struct imx_thermal_data *data;
650	struct regmap *map;
651	int measure_freq;
652	int ret;
653
654	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
655	if (!data)
656		return -ENOMEM;
657
658	data->dev = &pdev->dev;
659
660	map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
661	if (IS_ERR(map)) {
662		ret = PTR_ERR(map);
663		dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
664		return ret;
665	}
666	data->tempmon = map;
667
668	data->socdata = of_device_get_match_data(&pdev->dev);
669	if (!data->socdata) {
670		dev_err(&pdev->dev, "no device match found\n");
671		return -ENODEV;
672	}
673
674	/* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
675	if (data->socdata->version == TEMPMON_IMX6SX) {
676		regmap_write(map, IMX6_MISC1 + REG_CLR,
677			IMX6_MISC1_IRQ_TEMPHIGH | IMX6_MISC1_IRQ_TEMPLOW
678			| IMX6_MISC1_IRQ_TEMPPANIC);
679		/*
680		 * reset value of LOW ALARM is incorrect, set it to lowest
681		 * value to avoid false trigger of low alarm.
682		 */
683		regmap_write(map, data->socdata->low_alarm_ctrl + REG_SET,
684			     data->socdata->low_alarm_mask);
685	}
686
687	data->irq = platform_get_irq(pdev, 0);
688	if (data->irq < 0)
689		return data->irq;
690
691	platform_set_drvdata(pdev, data);
692
693	if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
694		ret = imx_init_from_nvmem_cells(pdev);
695		if (ret)
696			return dev_err_probe(&pdev->dev, ret,
697					     "failed to init from nvmem\n");
 
 
 
 
 
698	} else {
699		ret = imx_init_from_tempmon_data(pdev);
700		if (ret) {
701			dev_err(&pdev->dev, "failed to init from fsl,tempmon-data\n");
702			return ret;
703		}
704	}
705
706	/* Make sure sensor is in known good state for measurements */
707	regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
708		     data->socdata->power_down_mask);
709	regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
710		     data->socdata->measure_temp_mask);
711	regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
712		     data->socdata->measure_freq_mask);
713	if (data->socdata->version != TEMPMON_IMX7D)
714		regmap_write(map, IMX6_MISC0 + REG_SET,
715			IMX6_MISC0_REFTOP_SELBIASOFF);
716	regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
717		     data->socdata->power_down_mask);
718
719	ret = imx_thermal_register_legacy_cooling(data);
720	if (ret)
721		return dev_err_probe(&pdev->dev, ret,
722				     "failed to register cpufreq cooling device\n");
 
 
 
 
 
723
724	data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
725	if (IS_ERR(data->thermal_clk)) {
726		ret = PTR_ERR(data->thermal_clk);
727		if (ret != -EPROBE_DEFER)
728			dev_err(&pdev->dev,
729				"failed to get thermal clk: %d\n", ret);
730		goto legacy_cleanup;
731	}
732
733	/*
734	 * Thermal sensor needs clk on to get correct value, normally
735	 * we should enable its clk before taking measurement and disable
736	 * clk after measurement is done, but if alarm function is enabled,
737	 * hardware will auto measure the temperature periodically, so we
738	 * need to keep the clk always on for alarm function.
739	 */
740	ret = clk_prepare_enable(data->thermal_clk);
741	if (ret) {
742		dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
743		goto legacy_cleanup;
744	}
745
746	data->tz = thermal_zone_device_register("imx_thermal_zone",
747						IMX_TRIP_NUM,
748						BIT(IMX_TRIP_PASSIVE), data,
749						&imx_tz_ops, NULL,
750						IMX_PASSIVE_DELAY,
751						IMX_POLLING_DELAY);
752	if (IS_ERR(data->tz)) {
753		ret = PTR_ERR(data->tz);
754		dev_err(&pdev->dev,
755			"failed to register thermal zone device %d\n", ret);
756		goto clk_disable;
757	}
758
759	dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
760		 " critical:%dC passive:%dC\n", data->temp_grade,
761		 data->temp_max / 1000, data->temp_critical / 1000,
762		 data->temp_passive / 1000);
763
764	/* Enable measurements at ~ 10 Hz */
765	regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
766		     data->socdata->measure_freq_mask);
767	measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
768	regmap_write(map, data->socdata->measure_freq_ctrl + REG_SET,
769		     measure_freq << data->socdata->measure_freq_shift);
770	imx_set_alarm_temp(data, data->temp_passive);
771
772	if (data->socdata->version == TEMPMON_IMX6SX)
773		imx_set_panic_temp(data, data->temp_critical);
774
775	regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
776		     data->socdata->power_down_mask);
777	regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
778		     data->socdata->measure_temp_mask);
779	/* After power up, we need a delay before first access can be done. */
780	usleep_range(20, 50);
781
782	/* the core was configured and enabled just before */
783	pm_runtime_set_active(&pdev->dev);
784	pm_runtime_enable(data->dev);
785
786	ret = pm_runtime_resume_and_get(data->dev);
787	if (ret < 0)
788		goto disable_runtime_pm;
789
790	data->irq_enabled = true;
791	ret = thermal_zone_device_enable(data->tz);
792	if (ret)
793		goto thermal_zone_unregister;
794
795	ret = devm_request_threaded_irq(&pdev->dev, data->irq,
796			imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
797			0, "imx_thermal", data);
798	if (ret < 0) {
799		dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
800		goto thermal_zone_unregister;
801	}
802
803	pm_runtime_put(data->dev);
804
805	return 0;
806
807thermal_zone_unregister:
808	thermal_zone_device_unregister(data->tz);
809disable_runtime_pm:
810	pm_runtime_put_noidle(data->dev);
811	pm_runtime_disable(data->dev);
812clk_disable:
813	clk_disable_unprepare(data->thermal_clk);
814legacy_cleanup:
815	imx_thermal_unregister_legacy_cooling(data);
816
817	return ret;
818}
819
820static int imx_thermal_remove(struct platform_device *pdev)
821{
822	struct imx_thermal_data *data = platform_get_drvdata(pdev);
 
823
824	pm_runtime_put_noidle(data->dev);
825	pm_runtime_disable(data->dev);
 
 
 
826
827	thermal_zone_device_unregister(data->tz);
828	imx_thermal_unregister_legacy_cooling(data);
829
830	return 0;
831}
832
833static int __maybe_unused imx_thermal_suspend(struct device *dev)
834{
835	struct imx_thermal_data *data = dev_get_drvdata(dev);
836	int ret;
837
838	/*
839	 * Need to disable thermal sensor, otherwise, when thermal core
840	 * try to get temperature before thermal sensor resume, a wrong
841	 * temperature will be read as the thermal sensor is powered
842	 * down. This is done in change_mode() operation called from
843	 * thermal_zone_device_disable()
844	 */
845	ret = thermal_zone_device_disable(data->tz);
846	if (ret)
847		return ret;
848
849	return pm_runtime_force_suspend(data->dev);
850}
851
852static int __maybe_unused imx_thermal_resume(struct device *dev)
853{
854	struct imx_thermal_data *data = dev_get_drvdata(dev);
855	int ret;
856
857	ret = pm_runtime_force_resume(data->dev);
858	if (ret)
859		return ret;
860	/* Enabled thermal sensor after resume */
861	return thermal_zone_device_enable(data->tz);
862}
863
864static int __maybe_unused imx_thermal_runtime_suspend(struct device *dev)
865{
866	struct imx_thermal_data *data = dev_get_drvdata(dev);
867	const struct thermal_soc_data *socdata = data->socdata;
868	struct regmap *map = data->tempmon;
869	int ret;
870
871	ret = regmap_write(map, socdata->sensor_ctrl + REG_CLR,
872			   socdata->measure_temp_mask);
873	if (ret)
874		return ret;
875
876	ret = regmap_write(map, socdata->sensor_ctrl + REG_SET,
877			   socdata->power_down_mask);
878	if (ret)
879		return ret;
880
881	clk_disable_unprepare(data->thermal_clk);
882
883	return 0;
884}
885
886static int __maybe_unused imx_thermal_runtime_resume(struct device *dev)
887{
888	struct imx_thermal_data *data = dev_get_drvdata(dev);
889	const struct thermal_soc_data *socdata = data->socdata;
890	struct regmap *map = data->tempmon;
891	int ret;
892
893	ret = clk_prepare_enable(data->thermal_clk);
894	if (ret)
895		return ret;
896
897	ret = regmap_write(map, socdata->sensor_ctrl + REG_CLR,
898			   socdata->power_down_mask);
899	if (ret)
900		return ret;
901
902	ret = regmap_write(map, socdata->sensor_ctrl + REG_SET,
903			   socdata->measure_temp_mask);
904	if (ret)
905		return ret;
906
907	/*
908	 * According to the temp sensor designers, it may require up to ~17us
909	 * to complete a measurement.
910	 */
911	usleep_range(20, 50);
912
913	return 0;
914}
915
916static const struct dev_pm_ops imx_thermal_pm_ops = {
917	SET_SYSTEM_SLEEP_PM_OPS(imx_thermal_suspend, imx_thermal_resume)
918	SET_RUNTIME_PM_OPS(imx_thermal_runtime_suspend,
919			   imx_thermal_runtime_resume, NULL)
920};
921
922static struct platform_driver imx_thermal = {
923	.driver = {
924		.name	= "imx_thermal",
925		.pm	= &imx_thermal_pm_ops,
926		.of_match_table = of_imx_thermal_match,
927	},
928	.probe		= imx_thermal_probe,
929	.remove		= imx_thermal_remove,
930};
931module_platform_driver(imx_thermal);
932
933MODULE_AUTHOR("Freescale Semiconductor, Inc.");
934MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
935MODULE_LICENSE("GPL v2");
936MODULE_ALIAS("platform:imx-thermal");