2 * Copyright 2013 Freescale Semiconductor, Inc.
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.
10 #include <linux/cpu_cooling.h>
11 #include <linux/cpufreq.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <linux/thermal.h>
24 #include <linux/types.h>
31 #define MISC0_REFTOP_SELBIASOFF (1 << 3)
33 #define TEMPSENSE0 0x0180
34 #define TEMPSENSE0_TEMP_CNT_SHIFT 8
35 #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
36 #define TEMPSENSE0_FINISHED (1 << 2)
37 #define TEMPSENSE0_MEASURE_TEMP (1 << 1)
38 #define TEMPSENSE0_POWER_DOWN (1 << 0)
40 #define TEMPSENSE1 0x0190
41 #define TEMPSENSE1_MEASURE_FREQ 0xffff
43 #define OCOTP_ANA1 0x04e0
45 /* The driver supports 1 passive trip point and 1 critical trip point */
46 enum imx_thermal_trip {
53 * It defines the temperature in millicelsius for passive trip point
54 * that will trigger cooling action when crossed.
56 #define IMX_TEMP_PASSIVE 85000
59 * The maximum die temperature on imx parts is 105C, let's give some cushion
60 * for noise and possible temperature rise between measurements.
62 #define IMX_TEMP_CRITICAL 100000
64 #define IMX_POLLING_DELAY 2000 /* millisecond */
65 #define IMX_PASSIVE_DELAY 1000
67 struct imx_thermal_data {
68 struct thermal_zone_device *tz;
69 struct thermal_cooling_device *cdev;
70 enum thermal_device_mode mode;
71 struct regmap *tempmon;
72 int c1, c2; /* See formula in imx_get_sensor_data() */
75 static int imx_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
77 struct imx_thermal_data *data = tz->devdata;
78 struct regmap *map = data->tempmon;
79 static unsigned long last_temp;
84 * Every time we measure the temperature, we will power on the
85 * temperature sensor, enable measurements, take a reading,
86 * disable measurements, power off the temperature sensor.
88 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
89 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
92 * According to the temp sensor designers, it may require up to ~17us
93 * to complete a measurement.
97 regmap_read(map, TEMPSENSE0, &val);
98 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
99 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
101 if ((val & TEMPSENSE0_FINISHED) == 0) {
102 dev_dbg(&tz->device, "temp measurement never finished\n");
106 n_meas = (val & TEMPSENSE0_TEMP_CNT_MASK) >> TEMPSENSE0_TEMP_CNT_SHIFT;
108 /* See imx_get_sensor_data() for formula derivation */
109 *temp = data->c2 + data->c1 * n_meas;
111 if (*temp != last_temp) {
112 dev_dbg(&tz->device, "millicelsius: %ld\n", *temp);
119 static int imx_get_mode(struct thermal_zone_device *tz,
120 enum thermal_device_mode *mode)
122 struct imx_thermal_data *data = tz->devdata;
129 static int imx_set_mode(struct thermal_zone_device *tz,
130 enum thermal_device_mode mode)
132 struct imx_thermal_data *data = tz->devdata;
134 if (mode == THERMAL_DEVICE_ENABLED) {
135 tz->polling_delay = IMX_POLLING_DELAY;
136 tz->passive_delay = IMX_PASSIVE_DELAY;
138 tz->polling_delay = 0;
139 tz->passive_delay = 0;
143 thermal_zone_device_update(tz);
148 static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
149 enum thermal_trip_type *type)
151 *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
152 THERMAL_TRIP_CRITICAL;
156 static int imx_get_crit_temp(struct thermal_zone_device *tz,
159 *temp = IMX_TEMP_CRITICAL;
163 static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
166 *temp = (trip == IMX_TRIP_PASSIVE) ? IMX_TEMP_PASSIVE :
171 static int imx_bind(struct thermal_zone_device *tz,
172 struct thermal_cooling_device *cdev)
176 ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
181 "binding zone %s with cdev %s failed:%d\n",
182 tz->type, cdev->type, ret);
189 static int imx_unbind(struct thermal_zone_device *tz,
190 struct thermal_cooling_device *cdev)
194 ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
197 "unbinding zone %s with cdev %s failed:%d\n",
198 tz->type, cdev->type, ret);
205 static const struct thermal_zone_device_ops imx_tz_ops = {
207 .unbind = imx_unbind,
208 .get_temp = imx_get_temp,
209 .get_mode = imx_get_mode,
210 .set_mode = imx_set_mode,
211 .get_trip_type = imx_get_trip_type,
212 .get_trip_temp = imx_get_trip_temp,
213 .get_crit_temp = imx_get_crit_temp,
216 static int imx_get_sensor_data(struct platform_device *pdev)
218 struct imx_thermal_data *data = platform_get_drvdata(pdev);
224 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
228 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
232 ret = regmap_read(map, OCOTP_ANA1, &val);
234 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
238 if (val == 0 || val == ~0) {
239 dev_err(&pdev->dev, "invalid sensor calibration data\n");
244 * Sensor data layout:
245 * [31:20] - sensor value @ 25C
246 * [19:8] - sensor value of hot
247 * [7:0] - hot temperature value
250 n2 = (val & 0xfff00) >> 8;
252 t1 = 25; /* t1 always 25C */
255 * Derived from linear interpolation,
256 * Tmeas = T2 + (Nmeas - N2) * (T1 - T2) / (N1 - N2)
257 * We want to reduce this down to the minimum computation necessary
258 * for each temperature read. Also, we want Tmeas in millicelsius
259 * and we don't want to lose precision from integer division. So...
260 * milli_Tmeas = 1000 * T2 + 1000 * (Nmeas - N2) * (T1 - T2) / (N1 - N2)
261 * Let constant c1 = 1000 * (T1 - T2) / (N1 - N2)
262 * milli_Tmeas = (1000 * T2) + c1 * (Nmeas - N2)
263 * milli_Tmeas = (1000 * T2) + (c1 * Nmeas) - (c1 * N2)
264 * Let constant c2 = (1000 * T2) - (c1 * N2)
265 * milli_Tmeas = c2 + (c1 * Nmeas)
267 data->c1 = 1000 * (t1 - t2) / (n1 - n2);
268 data->c2 = 1000 * t2 - data->c1 * n2;
273 static int imx_thermal_probe(struct platform_device *pdev)
275 struct imx_thermal_data *data;
276 struct cpumask clip_cpus;
280 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
284 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
287 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
292 platform_set_drvdata(pdev, data);
294 ret = imx_get_sensor_data(pdev);
296 dev_err(&pdev->dev, "failed to get sensor data\n");
300 /* Make sure sensor is in known good state for measurements */
301 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
302 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
303 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
304 regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
305 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
307 cpumask_set_cpu(0, &clip_cpus);
308 data->cdev = cpufreq_cooling_register(&clip_cpus);
309 if (IS_ERR(data->cdev)) {
310 ret = PTR_ERR(data->cdev);
312 "failed to register cpufreq cooling device: %d\n", ret);
316 data->tz = thermal_zone_device_register("imx_thermal_zone",
317 IMX_TRIP_NUM, 0, data,
321 if (IS_ERR(data->tz)) {
322 ret = PTR_ERR(data->tz);
324 "failed to register thermal zone device %d\n", ret);
325 cpufreq_cooling_unregister(data->cdev);
329 data->mode = THERMAL_DEVICE_ENABLED;
334 static int imx_thermal_remove(struct platform_device *pdev)
336 struct imx_thermal_data *data = platform_get_drvdata(pdev);
338 thermal_zone_device_unregister(data->tz);
339 cpufreq_cooling_unregister(data->cdev);
344 #ifdef CONFIG_PM_SLEEP
345 static int imx_thermal_suspend(struct device *dev)
347 struct imx_thermal_data *data = dev_get_drvdata(dev);
348 struct regmap *map = data->tempmon;
351 regmap_read(map, TEMPSENSE0, &val);
352 if ((val & TEMPSENSE0_POWER_DOWN) == 0) {
354 * If a measurement is taking place, wait for a long enough
355 * time for it to finish, and then check again. If it still
356 * does not finish, something must go wrong.
359 regmap_read(map, TEMPSENSE0, &val);
360 if ((val & TEMPSENSE0_POWER_DOWN) == 0)
367 static int imx_thermal_resume(struct device *dev)
369 /* Nothing to do for now */
374 static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
375 imx_thermal_suspend, imx_thermal_resume);
377 static const struct of_device_id of_imx_thermal_match[] = {
378 { .compatible = "fsl,imx6q-tempmon", },
382 static struct platform_driver imx_thermal = {
384 .name = "imx_thermal",
385 .owner = THIS_MODULE,
386 .pm = &imx_thermal_pm_ops,
387 .of_match_table = of_imx_thermal_match,
389 .probe = imx_thermal_probe,
390 .remove = imx_thermal_remove,
392 module_platform_driver(imx_thermal);
394 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
395 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
396 MODULE_LICENSE("GPL v2");
397 MODULE_ALIAS("platform:imx-thermal");