Linux Audio

Check our new training course

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