Commit | Line | Data |
---|---|---|
ca3de46b SG |
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 | ||
329fe7b1 | 10 | #include <linux/clk.h> |
ca3de46b | 11 | #include <linux/cpu_cooling.h> |
ca3de46b SG |
12 | #include <linux/delay.h> |
13 | #include <linux/device.h> | |
14 | #include <linux/init.h> | |
37713a1e | 15 | #include <linux/interrupt.h> |
ca3de46b SG |
16 | #include <linux/io.h> |
17 | #include <linux/kernel.h> | |
18 | #include <linux/mfd/syscon.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/of.h> | |
3c94f17e | 21 | #include <linux/of_device.h> |
ca3de46b SG |
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) | |
3c94f17e AH |
34 | #define MISC1 0x0160 |
35 | #define MISC1_IRQ_TEMPHIGH (1 << 29) | |
36 | /* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */ | |
37 | #define MISC1_IRQ_TEMPLOW (1 << 28) | |
38 | #define MISC1_IRQ_TEMPPANIC (1 << 27) | |
ca3de46b SG |
39 | |
40 | #define TEMPSENSE0 0x0180 | |
37713a1e PZ |
41 | #define TEMPSENSE0_ALARM_VALUE_SHIFT 20 |
42 | #define TEMPSENSE0_ALARM_VALUE_MASK (0xfff << TEMPSENSE0_ALARM_VALUE_SHIFT) | |
ca3de46b SG |
43 | #define TEMPSENSE0_TEMP_CNT_SHIFT 8 |
44 | #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT) | |
45 | #define TEMPSENSE0_FINISHED (1 << 2) | |
46 | #define TEMPSENSE0_MEASURE_TEMP (1 << 1) | |
47 | #define TEMPSENSE0_POWER_DOWN (1 << 0) | |
48 | ||
49 | #define TEMPSENSE1 0x0190 | |
50 | #define TEMPSENSE1_MEASURE_FREQ 0xffff | |
3c94f17e AH |
51 | /* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */ |
52 | #define TEMPSENSE2 0x0290 | |
53 | #define TEMPSENSE2_LOW_VALUE_SHIFT 0 | |
54 | #define TEMPSENSE2_LOW_VALUE_MASK 0xfff | |
55 | #define TEMPSENSE2_PANIC_VALUE_SHIFT 16 | |
56 | #define TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000 | |
ca3de46b SG |
57 | |
58 | #define OCOTP_ANA1 0x04e0 | |
59 | ||
60 | /* The driver supports 1 passive trip point and 1 critical trip point */ | |
61 | enum imx_thermal_trip { | |
62 | IMX_TRIP_PASSIVE, | |
63 | IMX_TRIP_CRITICAL, | |
64 | IMX_TRIP_NUM, | |
65 | }; | |
66 | ||
67 | /* | |
68 | * It defines the temperature in millicelsius for passive trip point | |
69 | * that will trigger cooling action when crossed. | |
70 | */ | |
71 | #define IMX_TEMP_PASSIVE 85000 | |
72 | ||
ca3de46b SG |
73 | #define IMX_POLLING_DELAY 2000 /* millisecond */ |
74 | #define IMX_PASSIVE_DELAY 1000 | |
75 | ||
749e8be7 AH |
76 | #define FACTOR0 10000000 |
77 | #define FACTOR1 15976 | |
78 | #define FACTOR2 4297157 | |
79 | ||
3c94f17e AH |
80 | #define TEMPMON_IMX6Q 1 |
81 | #define TEMPMON_IMX6SX 2 | |
82 | ||
83 | struct thermal_soc_data { | |
84 | u32 version; | |
85 | }; | |
86 | ||
87 | static struct thermal_soc_data thermal_imx6q_data = { | |
88 | .version = TEMPMON_IMX6Q, | |
89 | }; | |
90 | ||
91 | static struct thermal_soc_data thermal_imx6sx_data = { | |
92 | .version = TEMPMON_IMX6SX, | |
93 | }; | |
94 | ||
ca3de46b SG |
95 | struct imx_thermal_data { |
96 | struct thermal_zone_device *tz; | |
97 | struct thermal_cooling_device *cdev; | |
98 | enum thermal_device_mode mode; | |
99 | struct regmap *tempmon; | |
749e8be7 | 100 | u32 c1, c2; /* See formula in imx_get_sensor_data() */ |
017e5142 PZ |
101 | unsigned long temp_passive; |
102 | unsigned long temp_critical; | |
37713a1e PZ |
103 | unsigned long alarm_temp; |
104 | unsigned long last_temp; | |
105 | bool irq_enabled; | |
106 | int irq; | |
329fe7b1 | 107 | struct clk *thermal_clk; |
3c94f17e | 108 | const struct thermal_soc_data *socdata; |
ca3de46b SG |
109 | }; |
110 | ||
3c94f17e AH |
111 | static void imx_set_panic_temp(struct imx_thermal_data *data, |
112 | signed long panic_temp) | |
113 | { | |
114 | struct regmap *map = data->tempmon; | |
115 | int critical_value; | |
116 | ||
117 | critical_value = (data->c2 - panic_temp) / data->c1; | |
118 | regmap_write(map, TEMPSENSE2 + REG_CLR, TEMPSENSE2_PANIC_VALUE_MASK); | |
119 | regmap_write(map, TEMPSENSE2 + REG_SET, critical_value << | |
120 | TEMPSENSE2_PANIC_VALUE_SHIFT); | |
121 | } | |
122 | ||
37713a1e PZ |
123 | static void imx_set_alarm_temp(struct imx_thermal_data *data, |
124 | signed long alarm_temp) | |
125 | { | |
126 | struct regmap *map = data->tempmon; | |
127 | int alarm_value; | |
128 | ||
129 | data->alarm_temp = alarm_temp; | |
749e8be7 | 130 | alarm_value = (data->c2 - alarm_temp) / data->c1; |
37713a1e PZ |
131 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_ALARM_VALUE_MASK); |
132 | regmap_write(map, TEMPSENSE0 + REG_SET, alarm_value << | |
133 | TEMPSENSE0_ALARM_VALUE_SHIFT); | |
134 | } | |
135 | ||
ca3de46b SG |
136 | static int imx_get_temp(struct thermal_zone_device *tz, unsigned long *temp) |
137 | { | |
138 | struct imx_thermal_data *data = tz->devdata; | |
139 | struct regmap *map = data->tempmon; | |
ca3de46b | 140 | unsigned int n_meas; |
37713a1e | 141 | bool wait; |
ca3de46b SG |
142 | u32 val; |
143 | ||
37713a1e PZ |
144 | if (data->mode == THERMAL_DEVICE_ENABLED) { |
145 | /* Check if a measurement is currently in progress */ | |
146 | regmap_read(map, TEMPSENSE0, &val); | |
147 | wait = !(val & TEMPSENSE0_FINISHED); | |
148 | } else { | |
149 | /* | |
150 | * Every time we measure the temperature, we will power on the | |
151 | * temperature sensor, enable measurements, take a reading, | |
152 | * disable measurements, power off the temperature sensor. | |
153 | */ | |
154 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN); | |
155 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP); | |
156 | ||
157 | wait = true; | |
158 | } | |
ca3de46b SG |
159 | |
160 | /* | |
161 | * According to the temp sensor designers, it may require up to ~17us | |
162 | * to complete a measurement. | |
163 | */ | |
37713a1e PZ |
164 | if (wait) |
165 | usleep_range(20, 50); | |
ca3de46b SG |
166 | |
167 | regmap_read(map, TEMPSENSE0, &val); | |
37713a1e PZ |
168 | |
169 | if (data->mode != THERMAL_DEVICE_ENABLED) { | |
170 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP); | |
171 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN); | |
172 | } | |
ca3de46b SG |
173 | |
174 | if ((val & TEMPSENSE0_FINISHED) == 0) { | |
175 | dev_dbg(&tz->device, "temp measurement never finished\n"); | |
176 | return -EAGAIN; | |
177 | } | |
178 | ||
179 | n_meas = (val & TEMPSENSE0_TEMP_CNT_MASK) >> TEMPSENSE0_TEMP_CNT_SHIFT; | |
180 | ||
181 | /* See imx_get_sensor_data() for formula derivation */ | |
749e8be7 | 182 | *temp = data->c2 - n_meas * data->c1; |
ca3de46b | 183 | |
3c94f17e AH |
184 | /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */ |
185 | if (data->socdata->version == TEMPMON_IMX6Q) { | |
186 | if (data->alarm_temp == data->temp_passive && | |
187 | *temp >= data->temp_passive) | |
188 | imx_set_alarm_temp(data, data->temp_critical); | |
189 | if (data->alarm_temp == data->temp_critical && | |
190 | *temp < data->temp_passive) { | |
191 | imx_set_alarm_temp(data, data->temp_passive); | |
192 | dev_dbg(&tz->device, "thermal alarm off: T < %lu\n", | |
193 | data->alarm_temp / 1000); | |
194 | } | |
37713a1e PZ |
195 | } |
196 | ||
197 | if (*temp != data->last_temp) { | |
ca3de46b | 198 | dev_dbg(&tz->device, "millicelsius: %ld\n", *temp); |
37713a1e PZ |
199 | data->last_temp = *temp; |
200 | } | |
201 | ||
202 | /* Reenable alarm IRQ if temperature below alarm temperature */ | |
203 | if (!data->irq_enabled && *temp < data->alarm_temp) { | |
204 | data->irq_enabled = true; | |
205 | enable_irq(data->irq); | |
ca3de46b SG |
206 | } |
207 | ||
208 | return 0; | |
209 | } | |
210 | ||
211 | static int imx_get_mode(struct thermal_zone_device *tz, | |
212 | enum thermal_device_mode *mode) | |
213 | { | |
214 | struct imx_thermal_data *data = tz->devdata; | |
215 | ||
216 | *mode = data->mode; | |
217 | ||
218 | return 0; | |
219 | } | |
220 | ||
221 | static int imx_set_mode(struct thermal_zone_device *tz, | |
222 | enum thermal_device_mode mode) | |
223 | { | |
224 | struct imx_thermal_data *data = tz->devdata; | |
37713a1e | 225 | struct regmap *map = data->tempmon; |
ca3de46b SG |
226 | |
227 | if (mode == THERMAL_DEVICE_ENABLED) { | |
228 | tz->polling_delay = IMX_POLLING_DELAY; | |
229 | tz->passive_delay = IMX_PASSIVE_DELAY; | |
37713a1e PZ |
230 | |
231 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN); | |
232 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP); | |
233 | ||
234 | if (!data->irq_enabled) { | |
235 | data->irq_enabled = true; | |
236 | enable_irq(data->irq); | |
237 | } | |
ca3de46b | 238 | } else { |
37713a1e PZ |
239 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP); |
240 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN); | |
241 | ||
ca3de46b SG |
242 | tz->polling_delay = 0; |
243 | tz->passive_delay = 0; | |
37713a1e PZ |
244 | |
245 | if (data->irq_enabled) { | |
246 | disable_irq(data->irq); | |
247 | data->irq_enabled = false; | |
248 | } | |
ca3de46b SG |
249 | } |
250 | ||
251 | data->mode = mode; | |
252 | thermal_zone_device_update(tz); | |
253 | ||
254 | return 0; | |
255 | } | |
256 | ||
257 | static int imx_get_trip_type(struct thermal_zone_device *tz, int trip, | |
258 | enum thermal_trip_type *type) | |
259 | { | |
260 | *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE : | |
261 | THERMAL_TRIP_CRITICAL; | |
262 | return 0; | |
263 | } | |
264 | ||
265 | static int imx_get_crit_temp(struct thermal_zone_device *tz, | |
266 | unsigned long *temp) | |
267 | { | |
017e5142 PZ |
268 | struct imx_thermal_data *data = tz->devdata; |
269 | ||
270 | *temp = data->temp_critical; | |
ca3de46b SG |
271 | return 0; |
272 | } | |
273 | ||
274 | static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip, | |
275 | unsigned long *temp) | |
276 | { | |
017e5142 PZ |
277 | struct imx_thermal_data *data = tz->devdata; |
278 | ||
279 | *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive : | |
280 | data->temp_critical; | |
281 | return 0; | |
282 | } | |
283 | ||
284 | static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip, | |
285 | unsigned long temp) | |
286 | { | |
287 | struct imx_thermal_data *data = tz->devdata; | |
288 | ||
289 | if (trip == IMX_TRIP_CRITICAL) | |
290 | return -EPERM; | |
291 | ||
292 | if (temp > IMX_TEMP_PASSIVE) | |
293 | return -EINVAL; | |
294 | ||
295 | data->temp_passive = temp; | |
296 | ||
37713a1e PZ |
297 | imx_set_alarm_temp(data, temp); |
298 | ||
ca3de46b SG |
299 | return 0; |
300 | } | |
301 | ||
302 | static int imx_bind(struct thermal_zone_device *tz, | |
303 | struct thermal_cooling_device *cdev) | |
304 | { | |
305 | int ret; | |
306 | ||
307 | ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev, | |
308 | THERMAL_NO_LIMIT, | |
6cd9e9f6 KS |
309 | THERMAL_NO_LIMIT, |
310 | THERMAL_WEIGHT_DEFAULT); | |
ca3de46b SG |
311 | if (ret) { |
312 | dev_err(&tz->device, | |
313 | "binding zone %s with cdev %s failed:%d\n", | |
314 | tz->type, cdev->type, ret); | |
315 | return ret; | |
316 | } | |
317 | ||
318 | return 0; | |
319 | } | |
320 | ||
321 | static int imx_unbind(struct thermal_zone_device *tz, | |
322 | struct thermal_cooling_device *cdev) | |
323 | { | |
324 | int ret; | |
325 | ||
326 | ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev); | |
327 | if (ret) { | |
328 | dev_err(&tz->device, | |
329 | "unbinding zone %s with cdev %s failed:%d\n", | |
330 | tz->type, cdev->type, ret); | |
331 | return ret; | |
332 | } | |
333 | ||
334 | return 0; | |
335 | } | |
336 | ||
cbb07bb3 | 337 | static struct thermal_zone_device_ops imx_tz_ops = { |
ca3de46b SG |
338 | .bind = imx_bind, |
339 | .unbind = imx_unbind, | |
340 | .get_temp = imx_get_temp, | |
341 | .get_mode = imx_get_mode, | |
342 | .set_mode = imx_set_mode, | |
343 | .get_trip_type = imx_get_trip_type, | |
344 | .get_trip_temp = imx_get_trip_temp, | |
345 | .get_crit_temp = imx_get_crit_temp, | |
017e5142 | 346 | .set_trip_temp = imx_set_trip_temp, |
ca3de46b SG |
347 | }; |
348 | ||
349 | static int imx_get_sensor_data(struct platform_device *pdev) | |
350 | { | |
351 | struct imx_thermal_data *data = platform_get_drvdata(pdev); | |
352 | struct regmap *map; | |
d0f9d64a | 353 | int t1, n1; |
ca3de46b SG |
354 | int ret; |
355 | u32 val; | |
749e8be7 | 356 | u64 temp64; |
ca3de46b SG |
357 | |
358 | map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, | |
359 | "fsl,tempmon-data"); | |
360 | if (IS_ERR(map)) { | |
361 | ret = PTR_ERR(map); | |
362 | dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret); | |
363 | return ret; | |
364 | } | |
365 | ||
366 | ret = regmap_read(map, OCOTP_ANA1, &val); | |
367 | if (ret) { | |
368 | dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret); | |
369 | return ret; | |
370 | } | |
371 | ||
372 | if (val == 0 || val == ~0) { | |
373 | dev_err(&pdev->dev, "invalid sensor calibration data\n"); | |
374 | return -EINVAL; | |
375 | } | |
376 | ||
377 | /* | |
378 | * Sensor data layout: | |
379 | * [31:20] - sensor value @ 25C | |
749e8be7 AH |
380 | * Use universal formula now and only need sensor value @ 25C |
381 | * slope = 0.4297157 - (0.0015976 * 25C fuse) | |
ca3de46b SG |
382 | */ |
383 | n1 = val >> 20; | |
ca3de46b SG |
384 | t1 = 25; /* t1 always 25C */ |
385 | ||
386 | /* | |
749e8be7 AH |
387 | * Derived from linear interpolation: |
388 | * slope = 0.4297157 - (0.0015976 * 25C fuse) | |
389 | * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0 | |
390 | * (Nmeas - n1) / (Tmeas - t1) = slope | |
ca3de46b SG |
391 | * We want to reduce this down to the minimum computation necessary |
392 | * for each temperature read. Also, we want Tmeas in millicelsius | |
393 | * and we don't want to lose precision from integer division. So... | |
749e8be7 AH |
394 | * Tmeas = (Nmeas - n1) / slope + t1 |
395 | * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1 | |
396 | * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1 | |
397 | * Let constant c1 = (-1000 / slope) | |
398 | * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1 | |
399 | * Let constant c2 = n1 *c1 + 1000 * t1 | |
400 | * milli_Tmeas = c2 - Nmeas * c1 | |
ca3de46b | 401 | */ |
749e8be7 AH |
402 | temp64 = FACTOR0; |
403 | temp64 *= 1000; | |
404 | do_div(temp64, FACTOR1 * n1 - FACTOR2); | |
405 | data->c1 = temp64; | |
406 | data->c2 = n1 * data->c1 + 1000 * t1; | |
ca3de46b | 407 | |
017e5142 | 408 | /* |
d0f9d64a AH |
409 | * Set the default passive cooling trip point, |
410 | * can be changed from userspace. | |
017e5142 | 411 | */ |
d0f9d64a | 412 | data->temp_passive = IMX_TEMP_PASSIVE; |
017e5142 PZ |
413 | |
414 | /* | |
d0f9d64a AH |
415 | * The maximum die temperature set to 20 C higher than |
416 | * IMX_TEMP_PASSIVE. | |
017e5142 | 417 | */ |
d0f9d64a | 418 | data->temp_critical = 1000 * 20 + data->temp_passive; |
017e5142 | 419 | |
ca3de46b SG |
420 | return 0; |
421 | } | |
422 | ||
37713a1e PZ |
423 | static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev) |
424 | { | |
425 | struct imx_thermal_data *data = dev; | |
426 | ||
427 | disable_irq_nosync(irq); | |
428 | data->irq_enabled = false; | |
429 | ||
430 | return IRQ_WAKE_THREAD; | |
431 | } | |
432 | ||
433 | static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev) | |
434 | { | |
435 | struct imx_thermal_data *data = dev; | |
436 | ||
437 | dev_dbg(&data->tz->device, "THERMAL ALARM: T > %lu\n", | |
438 | data->alarm_temp / 1000); | |
439 | ||
440 | thermal_zone_device_update(data->tz); | |
441 | ||
442 | return IRQ_HANDLED; | |
443 | } | |
444 | ||
3c94f17e AH |
445 | static const struct of_device_id of_imx_thermal_match[] = { |
446 | { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, }, | |
447 | { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, }, | |
448 | { /* end */ } | |
449 | }; | |
450 | MODULE_DEVICE_TABLE(of, of_imx_thermal_match); | |
451 | ||
ca3de46b SG |
452 | static int imx_thermal_probe(struct platform_device *pdev) |
453 | { | |
3c94f17e AH |
454 | const struct of_device_id *of_id = |
455 | of_match_device(of_imx_thermal_match, &pdev->dev); | |
ca3de46b | 456 | struct imx_thermal_data *data; |
ca3de46b | 457 | struct regmap *map; |
37713a1e | 458 | int measure_freq; |
ca3de46b SG |
459 | int ret; |
460 | ||
461 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); | |
462 | if (!data) | |
463 | return -ENOMEM; | |
464 | ||
465 | map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon"); | |
466 | if (IS_ERR(map)) { | |
467 | ret = PTR_ERR(map); | |
468 | dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret); | |
469 | return ret; | |
470 | } | |
471 | data->tempmon = map; | |
472 | ||
3c94f17e AH |
473 | data->socdata = of_id->data; |
474 | ||
475 | /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */ | |
476 | if (data->socdata->version == TEMPMON_IMX6SX) { | |
477 | regmap_write(map, MISC1 + REG_CLR, MISC1_IRQ_TEMPHIGH | | |
478 | MISC1_IRQ_TEMPLOW | MISC1_IRQ_TEMPPANIC); | |
479 | /* | |
480 | * reset value of LOW ALARM is incorrect, set it to lowest | |
481 | * value to avoid false trigger of low alarm. | |
482 | */ | |
483 | regmap_write(map, TEMPSENSE2 + REG_SET, | |
484 | TEMPSENSE2_LOW_VALUE_MASK); | |
485 | } | |
486 | ||
37713a1e PZ |
487 | data->irq = platform_get_irq(pdev, 0); |
488 | if (data->irq < 0) | |
489 | return data->irq; | |
490 | ||
491 | ret = devm_request_threaded_irq(&pdev->dev, data->irq, | |
492 | imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread, | |
493 | 0, "imx_thermal", data); | |
494 | if (ret < 0) { | |
495 | dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret); | |
496 | return ret; | |
497 | } | |
498 | ||
ca3de46b SG |
499 | platform_set_drvdata(pdev, data); |
500 | ||
501 | ret = imx_get_sensor_data(pdev); | |
502 | if (ret) { | |
503 | dev_err(&pdev->dev, "failed to get sensor data\n"); | |
504 | return ret; | |
505 | } | |
506 | ||
507 | /* Make sure sensor is in known good state for measurements */ | |
508 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN); | |
509 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP); | |
510 | regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ); | |
511 | regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF); | |
512 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN); | |
513 | ||
bec85d2e | 514 | data->cdev = cpufreq_cooling_register(cpu_present_mask); |
ca3de46b SG |
515 | if (IS_ERR(data->cdev)) { |
516 | ret = PTR_ERR(data->cdev); | |
412aff94 FE |
517 | if (ret != -EPROBE_DEFER) |
518 | dev_err(&pdev->dev, | |
519 | "failed to register cpufreq cooling device: %d\n", | |
520 | ret); | |
ca3de46b SG |
521 | return ret; |
522 | } | |
523 | ||
90a21ff5 HK |
524 | data->thermal_clk = devm_clk_get(&pdev->dev, NULL); |
525 | if (IS_ERR(data->thermal_clk)) { | |
526 | ret = PTR_ERR(data->thermal_clk); | |
527 | if (ret != -EPROBE_DEFER) | |
528 | dev_err(&pdev->dev, | |
529 | "failed to get thermal clk: %d\n", ret); | |
530 | cpufreq_cooling_unregister(data->cdev); | |
531 | return ret; | |
532 | } | |
533 | ||
534 | /* | |
535 | * Thermal sensor needs clk on to get correct value, normally | |
536 | * we should enable its clk before taking measurement and disable | |
537 | * clk after measurement is done, but if alarm function is enabled, | |
538 | * hardware will auto measure the temperature periodically, so we | |
539 | * need to keep the clk always on for alarm function. | |
540 | */ | |
541 | ret = clk_prepare_enable(data->thermal_clk); | |
542 | if (ret) { | |
543 | dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret); | |
544 | cpufreq_cooling_unregister(data->cdev); | |
545 | return ret; | |
546 | } | |
547 | ||
ca3de46b | 548 | data->tz = thermal_zone_device_register("imx_thermal_zone", |
017e5142 PZ |
549 | IMX_TRIP_NUM, |
550 | BIT(IMX_TRIP_PASSIVE), data, | |
ca3de46b SG |
551 | &imx_tz_ops, NULL, |
552 | IMX_PASSIVE_DELAY, | |
553 | IMX_POLLING_DELAY); | |
554 | if (IS_ERR(data->tz)) { | |
555 | ret = PTR_ERR(data->tz); | |
556 | dev_err(&pdev->dev, | |
557 | "failed to register thermal zone device %d\n", ret); | |
90a21ff5 | 558 | clk_disable_unprepare(data->thermal_clk); |
ca3de46b SG |
559 | cpufreq_cooling_unregister(data->cdev); |
560 | return ret; | |
561 | } | |
562 | ||
37713a1e PZ |
563 | /* Enable measurements at ~ 10 Hz */ |
564 | regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ); | |
565 | measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */ | |
566 | regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq); | |
567 | imx_set_alarm_temp(data, data->temp_passive); | |
3c94f17e AH |
568 | |
569 | if (data->socdata->version == TEMPMON_IMX6SX) | |
570 | imx_set_panic_temp(data, data->temp_critical); | |
571 | ||
37713a1e PZ |
572 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN); |
573 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP); | |
574 | ||
575 | data->irq_enabled = true; | |
ca3de46b SG |
576 | data->mode = THERMAL_DEVICE_ENABLED; |
577 | ||
578 | return 0; | |
579 | } | |
580 | ||
581 | static int imx_thermal_remove(struct platform_device *pdev) | |
582 | { | |
583 | struct imx_thermal_data *data = platform_get_drvdata(pdev); | |
37713a1e PZ |
584 | struct regmap *map = data->tempmon; |
585 | ||
586 | /* Disable measurements */ | |
587 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN); | |
329fe7b1 AH |
588 | if (!IS_ERR(data->thermal_clk)) |
589 | clk_disable_unprepare(data->thermal_clk); | |
ca3de46b SG |
590 | |
591 | thermal_zone_device_unregister(data->tz); | |
592 | cpufreq_cooling_unregister(data->cdev); | |
593 | ||
594 | return 0; | |
595 | } | |
596 | ||
597 | #ifdef CONFIG_PM_SLEEP | |
598 | static int imx_thermal_suspend(struct device *dev) | |
599 | { | |
600 | struct imx_thermal_data *data = dev_get_drvdata(dev); | |
601 | struct regmap *map = data->tempmon; | |
ca3de46b | 602 | |
b46cce59 AH |
603 | /* |
604 | * Need to disable thermal sensor, otherwise, when thermal core | |
605 | * try to get temperature before thermal sensor resume, a wrong | |
606 | * temperature will be read as the thermal sensor is powered | |
607 | * down. | |
608 | */ | |
609 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP); | |
610 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN); | |
611 | data->mode = THERMAL_DEVICE_DISABLED; | |
d26eef8b | 612 | clk_disable_unprepare(data->thermal_clk); |
ca3de46b SG |
613 | |
614 | return 0; | |
615 | } | |
616 | ||
617 | static int imx_thermal_resume(struct device *dev) | |
618 | { | |
b46cce59 AH |
619 | struct imx_thermal_data *data = dev_get_drvdata(dev); |
620 | struct regmap *map = data->tempmon; | |
621 | ||
d26eef8b | 622 | clk_prepare_enable(data->thermal_clk); |
b46cce59 AH |
623 | /* Enabled thermal sensor after resume */ |
624 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN); | |
625 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP); | |
626 | data->mode = THERMAL_DEVICE_ENABLED; | |
627 | ||
ca3de46b SG |
628 | return 0; |
629 | } | |
630 | #endif | |
631 | ||
632 | static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops, | |
633 | imx_thermal_suspend, imx_thermal_resume); | |
634 | ||
ca3de46b SG |
635 | static struct platform_driver imx_thermal = { |
636 | .driver = { | |
637 | .name = "imx_thermal", | |
ca3de46b SG |
638 | .pm = &imx_thermal_pm_ops, |
639 | .of_match_table = of_imx_thermal_match, | |
640 | }, | |
641 | .probe = imx_thermal_probe, | |
642 | .remove = imx_thermal_remove, | |
643 | }; | |
644 | module_platform_driver(imx_thermal); | |
645 | ||
646 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); | |
647 | MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs"); | |
648 | MODULE_LICENSE("GPL v2"); | |
649 | MODULE_ALIAS("platform:imx-thermal"); |