xref: /linux/drivers/thermal/rockchip_thermal.c (revision 9cfc5c90ad38c8fc11bfd39de42a107da00871ba)
1 /*
2  * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  */
13 
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/io.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_address.h>
21 #include <linux/of_irq.h>
22 #include <linux/platform_device.h>
23 #include <linux/reset.h>
24 #include <linux/thermal.h>
25 #include <linux/pinctrl/consumer.h>
26 
27 /**
28  * If the temperature over a period of time High,
29  * the resulting TSHUT gave CRU module,let it reset the entire chip,
30  * or via GPIO give PMIC.
31  */
32 enum tshut_mode {
33 	TSHUT_MODE_CRU = 0,
34 	TSHUT_MODE_GPIO,
35 };
36 
37 /**
38  * the system Temperature Sensors tshut(tshut) polarity
39  * the bit 8 is tshut polarity.
40  * 0: low active, 1: high active
41  */
42 enum tshut_polarity {
43 	TSHUT_LOW_ACTIVE = 0,
44 	TSHUT_HIGH_ACTIVE,
45 };
46 
47 /**
48  * The system has three Temperature Sensors.  channel 0 is reserved,
49  * channel 1 is for CPU, and channel 2 is for GPU.
50  */
51 enum sensor_id {
52 	SENSOR_CPU = 1,
53 	SENSOR_GPU,
54 };
55 
56 struct rockchip_tsadc_chip {
57 	/* The hardware-controlled tshut property */
58 	long tshut_temp;
59 	enum tshut_mode tshut_mode;
60 	enum tshut_polarity tshut_polarity;
61 
62 	/* Chip-wide methods */
63 	void (*initialize)(void __iomem *reg, enum tshut_polarity p);
64 	void (*irq_ack)(void __iomem *reg);
65 	void (*control)(void __iomem *reg, bool on);
66 
67 	/* Per-sensor methods */
68 	int (*get_temp)(int chn, void __iomem *reg, int *temp);
69 	void (*set_tshut_temp)(int chn, void __iomem *reg, long temp);
70 	void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
71 };
72 
73 struct rockchip_thermal_sensor {
74 	struct rockchip_thermal_data *thermal;
75 	struct thermal_zone_device *tzd;
76 	enum sensor_id id;
77 };
78 
79 #define NUM_SENSORS	2 /* Ignore unused sensor 0 */
80 
81 struct rockchip_thermal_data {
82 	const struct rockchip_tsadc_chip *chip;
83 	struct platform_device *pdev;
84 	struct reset_control *reset;
85 
86 	struct rockchip_thermal_sensor sensors[NUM_SENSORS];
87 
88 	struct clk *clk;
89 	struct clk *pclk;
90 
91 	void __iomem *regs;
92 
93 	long tshut_temp;
94 	enum tshut_mode tshut_mode;
95 	enum tshut_polarity tshut_polarity;
96 };
97 
98 /* TSADC V2 Sensor info define: */
99 #define TSADCV2_AUTO_CON			0x04
100 #define TSADCV2_INT_EN				0x08
101 #define TSADCV2_INT_PD				0x0c
102 #define TSADCV2_DATA(chn)			(0x20 + (chn) * 0x04)
103 #define TSADCV2_COMP_SHUT(chn)		        (0x40 + (chn) * 0x04)
104 #define TSADCV2_HIGHT_INT_DEBOUNCE		0x60
105 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE		0x64
106 #define TSADCV2_AUTO_PERIOD			0x68
107 #define TSADCV2_AUTO_PERIOD_HT			0x6c
108 
109 #define TSADCV2_AUTO_EN				BIT(0)
110 #define TSADCV2_AUTO_SRC_EN(chn)		BIT(4 + (chn))
111 #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH	BIT(8)
112 
113 #define TSADCV2_INT_SRC_EN(chn)			BIT(chn)
114 #define TSADCV2_SHUT_2GPIO_SRC_EN(chn)		BIT(4 + (chn))
115 #define TSADCV2_SHUT_2CRU_SRC_EN(chn)		BIT(8 + (chn))
116 
117 #define TSADCV2_INT_PD_CLEAR_MASK		~BIT(8)
118 
119 #define TSADCV2_DATA_MASK			0xfff
120 #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT	4
121 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT	4
122 #define TSADCV2_AUTO_PERIOD_TIME		250 /* msec */
123 #define TSADCV2_AUTO_PERIOD_HT_TIME		50  /* msec */
124 
125 struct tsadc_table {
126 	u32 code;
127 	long temp;
128 };
129 
130 static const struct tsadc_table v2_code_table[] = {
131 	{TSADCV2_DATA_MASK, -40000},
132 	{3800, -40000},
133 	{3792, -35000},
134 	{3783, -30000},
135 	{3774, -25000},
136 	{3765, -20000},
137 	{3756, -15000},
138 	{3747, -10000},
139 	{3737, -5000},
140 	{3728, 0},
141 	{3718, 5000},
142 	{3708, 10000},
143 	{3698, 15000},
144 	{3688, 20000},
145 	{3678, 25000},
146 	{3667, 30000},
147 	{3656, 35000},
148 	{3645, 40000},
149 	{3634, 45000},
150 	{3623, 50000},
151 	{3611, 55000},
152 	{3600, 60000},
153 	{3588, 65000},
154 	{3575, 70000},
155 	{3563, 75000},
156 	{3550, 80000},
157 	{3537, 85000},
158 	{3524, 90000},
159 	{3510, 95000},
160 	{3496, 100000},
161 	{3482, 105000},
162 	{3467, 110000},
163 	{3452, 115000},
164 	{3437, 120000},
165 	{3421, 125000},
166 };
167 
168 static u32 rk_tsadcv2_temp_to_code(long temp)
169 {
170 	int high, low, mid;
171 
172 	low = 0;
173 	high = ARRAY_SIZE(v2_code_table) - 1;
174 	mid = (high + low) / 2;
175 
176 	if (temp < v2_code_table[low].temp || temp > v2_code_table[high].temp)
177 		return 0;
178 
179 	while (low <= high) {
180 		if (temp == v2_code_table[mid].temp)
181 			return v2_code_table[mid].code;
182 		else if (temp < v2_code_table[mid].temp)
183 			high = mid - 1;
184 		else
185 			low = mid + 1;
186 		mid = (low + high) / 2;
187 	}
188 
189 	return 0;
190 }
191 
192 static int rk_tsadcv2_code_to_temp(u32 code, int *temp)
193 {
194 	unsigned int low = 1;
195 	unsigned int high = ARRAY_SIZE(v2_code_table) - 1;
196 	unsigned int mid = (low + high) / 2;
197 	unsigned int num;
198 	unsigned long denom;
199 
200 	BUILD_BUG_ON(ARRAY_SIZE(v2_code_table) < 2);
201 
202 	code &= TSADCV2_DATA_MASK;
203 	if (code < v2_code_table[high].code)
204 		return -EAGAIN;		/* Incorrect reading */
205 
206 	while (low <= high) {
207 		if (code >= v2_code_table[mid].code &&
208 		    code < v2_code_table[mid - 1].code)
209 			break;
210 		else if (code < v2_code_table[mid].code)
211 			low = mid + 1;
212 		else
213 			high = mid - 1;
214 		mid = (low + high) / 2;
215 	}
216 
217 	/*
218 	 * The 5C granularity provided by the table is too much. Let's
219 	 * assume that the relationship between sensor readings and
220 	 * temperature between 2 table entries is linear and interpolate
221 	 * to produce less granular result.
222 	 */
223 	num = v2_code_table[mid].temp - v2_code_table[mid - 1].temp;
224 	num *= v2_code_table[mid - 1].code - code;
225 	denom = v2_code_table[mid - 1].code - v2_code_table[mid].code;
226 	*temp = v2_code_table[mid - 1].temp + (num / denom);
227 
228 	return 0;
229 }
230 
231 /**
232  * rk_tsadcv2_initialize - initialize TASDC Controller
233  * (1) Set TSADCV2_AUTO_PERIOD, configure the interleave between
234  * every two accessing of TSADC in normal operation.
235  * (2) Set TSADCV2_AUTO_PERIOD_HT, configure the interleave between
236  * every two accessing of TSADC after the temperature is higher
237  * than COM_SHUT or COM_INT.
238  * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE,
239  * if the temperature is higher than COMP_INT or COMP_SHUT for
240  * "debounce" times, TSADC controller will generate interrupt or TSHUT.
241  */
242 static void rk_tsadcv2_initialize(void __iomem *regs,
243 				  enum tshut_polarity tshut_polarity)
244 {
245 	if (tshut_polarity == TSHUT_HIGH_ACTIVE)
246 		writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
247 			       regs + TSADCV2_AUTO_CON);
248 	else
249 		writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
250 			       regs + TSADCV2_AUTO_CON);
251 
252 	writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
253 	writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
254 		       regs + TSADCV2_HIGHT_INT_DEBOUNCE);
255 	writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
256 		       regs + TSADCV2_AUTO_PERIOD_HT);
257 	writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
258 		       regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
259 }
260 
261 static void rk_tsadcv2_irq_ack(void __iomem *regs)
262 {
263 	u32 val;
264 
265 	val = readl_relaxed(regs + TSADCV2_INT_PD);
266 	writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
267 }
268 
269 static void rk_tsadcv2_control(void __iomem *regs, bool enable)
270 {
271 	u32 val;
272 
273 	val = readl_relaxed(regs + TSADCV2_AUTO_CON);
274 	if (enable)
275 		val |= TSADCV2_AUTO_EN;
276 	else
277 		val &= ~TSADCV2_AUTO_EN;
278 
279 	writel_relaxed(val, regs + TSADCV2_AUTO_CON);
280 }
281 
282 static int rk_tsadcv2_get_temp(int chn, void __iomem *regs, int *temp)
283 {
284 	u32 val;
285 
286 	val = readl_relaxed(regs + TSADCV2_DATA(chn));
287 
288 	return rk_tsadcv2_code_to_temp(val, temp);
289 }
290 
291 static void rk_tsadcv2_tshut_temp(int chn, void __iomem *regs, long temp)
292 {
293 	u32 tshut_value, val;
294 
295 	tshut_value = rk_tsadcv2_temp_to_code(temp);
296 	writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
297 
298 	/* TSHUT will be valid */
299 	val = readl_relaxed(regs + TSADCV2_AUTO_CON);
300 	writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
301 }
302 
303 static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
304 				  enum tshut_mode mode)
305 {
306 	u32 val;
307 
308 	val = readl_relaxed(regs + TSADCV2_INT_EN);
309 	if (mode == TSHUT_MODE_GPIO) {
310 		val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
311 		val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
312 	} else {
313 		val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
314 		val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
315 	}
316 
317 	writel_relaxed(val, regs + TSADCV2_INT_EN);
318 }
319 
320 static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
321 	.tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
322 	.tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
323 	.tshut_temp = 95000,
324 
325 	.initialize = rk_tsadcv2_initialize,
326 	.irq_ack = rk_tsadcv2_irq_ack,
327 	.control = rk_tsadcv2_control,
328 	.get_temp = rk_tsadcv2_get_temp,
329 	.set_tshut_temp = rk_tsadcv2_tshut_temp,
330 	.set_tshut_mode = rk_tsadcv2_tshut_mode,
331 };
332 
333 static const struct of_device_id of_rockchip_thermal_match[] = {
334 	{
335 		.compatible = "rockchip,rk3288-tsadc",
336 		.data = (void *)&rk3288_tsadc_data,
337 	},
338 	{ /* end */ },
339 };
340 MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
341 
342 static void
343 rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
344 {
345 	struct thermal_zone_device *tzd = sensor->tzd;
346 
347 	tzd->ops->set_mode(tzd,
348 		on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
349 }
350 
351 static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
352 {
353 	struct rockchip_thermal_data *thermal = dev;
354 	int i;
355 
356 	dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
357 
358 	thermal->chip->irq_ack(thermal->regs);
359 
360 	for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
361 		thermal_zone_device_update(thermal->sensors[i].tzd);
362 
363 	return IRQ_HANDLED;
364 }
365 
366 static int rockchip_thermal_get_temp(void *_sensor, int *out_temp)
367 {
368 	struct rockchip_thermal_sensor *sensor = _sensor;
369 	struct rockchip_thermal_data *thermal = sensor->thermal;
370 	const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
371 	int retval;
372 
373 	retval = tsadc->get_temp(sensor->id, thermal->regs, out_temp);
374 	dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n",
375 		sensor->id, *out_temp, retval);
376 
377 	return retval;
378 }
379 
380 static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = {
381 	.get_temp = rockchip_thermal_get_temp,
382 };
383 
384 static int rockchip_configure_from_dt(struct device *dev,
385 				      struct device_node *np,
386 				      struct rockchip_thermal_data *thermal)
387 {
388 	u32 shut_temp, tshut_mode, tshut_polarity;
389 
390 	if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
391 		dev_warn(dev,
392 			 "Missing tshut temp property, using default %ld\n",
393 			 thermal->chip->tshut_temp);
394 		thermal->tshut_temp = thermal->chip->tshut_temp;
395 	} else {
396 		thermal->tshut_temp = shut_temp;
397 	}
398 
399 	if (thermal->tshut_temp > INT_MAX) {
400 		dev_err(dev, "Invalid tshut temperature specified: %ld\n",
401 			thermal->tshut_temp);
402 		return -ERANGE;
403 	}
404 
405 	if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
406 		dev_warn(dev,
407 			 "Missing tshut mode property, using default (%s)\n",
408 			 thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
409 				"gpio" : "cru");
410 		thermal->tshut_mode = thermal->chip->tshut_mode;
411 	} else {
412 		thermal->tshut_mode = tshut_mode;
413 	}
414 
415 	if (thermal->tshut_mode > 1) {
416 		dev_err(dev, "Invalid tshut mode specified: %d\n",
417 			thermal->tshut_mode);
418 		return -EINVAL;
419 	}
420 
421 	if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
422 				 &tshut_polarity)) {
423 		dev_warn(dev,
424 			 "Missing tshut-polarity property, using default (%s)\n",
425 			 thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
426 				"low" : "high");
427 		thermal->tshut_polarity = thermal->chip->tshut_polarity;
428 	} else {
429 		thermal->tshut_polarity = tshut_polarity;
430 	}
431 
432 	if (thermal->tshut_polarity > 1) {
433 		dev_err(dev, "Invalid tshut-polarity specified: %d\n",
434 			thermal->tshut_polarity);
435 		return -EINVAL;
436 	}
437 
438 	return 0;
439 }
440 
441 static int
442 rockchip_thermal_register_sensor(struct platform_device *pdev,
443 				 struct rockchip_thermal_data *thermal,
444 				 struct rockchip_thermal_sensor *sensor,
445 				 enum sensor_id id)
446 {
447 	const struct rockchip_tsadc_chip *tsadc = thermal->chip;
448 	int error;
449 
450 	tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
451 	tsadc->set_tshut_temp(id, thermal->regs, thermal->tshut_temp);
452 
453 	sensor->thermal = thermal;
454 	sensor->id = id;
455 	sensor->tzd = thermal_zone_of_sensor_register(&pdev->dev, id, sensor,
456 						      &rockchip_of_thermal_ops);
457 	if (IS_ERR(sensor->tzd)) {
458 		error = PTR_ERR(sensor->tzd);
459 		dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
460 			id, error);
461 		return error;
462 	}
463 
464 	return 0;
465 }
466 
467 /*
468  * Reset TSADC Controller, reset all tsadc registers.
469  */
470 static void rockchip_thermal_reset_controller(struct reset_control *reset)
471 {
472 	reset_control_assert(reset);
473 	usleep_range(10, 20);
474 	reset_control_deassert(reset);
475 }
476 
477 static int rockchip_thermal_probe(struct platform_device *pdev)
478 {
479 	struct device_node *np = pdev->dev.of_node;
480 	struct rockchip_thermal_data *thermal;
481 	const struct of_device_id *match;
482 	struct resource *res;
483 	int irq;
484 	int i;
485 	int error;
486 
487 	match = of_match_node(of_rockchip_thermal_match, np);
488 	if (!match)
489 		return -ENXIO;
490 
491 	irq = platform_get_irq(pdev, 0);
492 	if (irq < 0) {
493 		dev_err(&pdev->dev, "no irq resource?\n");
494 		return -EINVAL;
495 	}
496 
497 	thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
498 			       GFP_KERNEL);
499 	if (!thermal)
500 		return -ENOMEM;
501 
502 	thermal->pdev = pdev;
503 
504 	thermal->chip = (const struct rockchip_tsadc_chip *)match->data;
505 	if (!thermal->chip)
506 		return -EINVAL;
507 
508 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
509 	thermal->regs = devm_ioremap_resource(&pdev->dev, res);
510 	if (IS_ERR(thermal->regs))
511 		return PTR_ERR(thermal->regs);
512 
513 	thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
514 	if (IS_ERR(thermal->reset)) {
515 		error = PTR_ERR(thermal->reset);
516 		dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
517 		return error;
518 	}
519 
520 	thermal->clk = devm_clk_get(&pdev->dev, "tsadc");
521 	if (IS_ERR(thermal->clk)) {
522 		error = PTR_ERR(thermal->clk);
523 		dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
524 		return error;
525 	}
526 
527 	thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
528 	if (IS_ERR(thermal->pclk)) {
529 		error = PTR_ERR(thermal->pclk);
530 		dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
531 			error);
532 		return error;
533 	}
534 
535 	error = clk_prepare_enable(thermal->clk);
536 	if (error) {
537 		dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
538 			error);
539 		return error;
540 	}
541 
542 	error = clk_prepare_enable(thermal->pclk);
543 	if (error) {
544 		dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
545 		goto err_disable_clk;
546 	}
547 
548 	rockchip_thermal_reset_controller(thermal->reset);
549 
550 	error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
551 	if (error) {
552 		dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
553 			error);
554 		goto err_disable_pclk;
555 	}
556 
557 	thermal->chip->initialize(thermal->regs, thermal->tshut_polarity);
558 
559 	error = rockchip_thermal_register_sensor(pdev, thermal,
560 						 &thermal->sensors[0],
561 						 SENSOR_CPU);
562 	if (error) {
563 		dev_err(&pdev->dev,
564 			"failed to register CPU thermal sensor: %d\n", error);
565 		goto err_disable_pclk;
566 	}
567 
568 	error = rockchip_thermal_register_sensor(pdev, thermal,
569 						 &thermal->sensors[1],
570 						 SENSOR_GPU);
571 	if (error) {
572 		dev_err(&pdev->dev,
573 			"failed to register GPU thermal sensor: %d\n", error);
574 		goto err_unregister_cpu_sensor;
575 	}
576 
577 	error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
578 					  &rockchip_thermal_alarm_irq_thread,
579 					  IRQF_ONESHOT,
580 					  "rockchip_thermal", thermal);
581 	if (error) {
582 		dev_err(&pdev->dev,
583 			"failed to request tsadc irq: %d\n", error);
584 		goto err_unregister_gpu_sensor;
585 	}
586 
587 	thermal->chip->control(thermal->regs, true);
588 
589 	for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
590 		rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
591 
592 	platform_set_drvdata(pdev, thermal);
593 
594 	return 0;
595 
596 err_unregister_gpu_sensor:
597 	thermal_zone_of_sensor_unregister(&pdev->dev, thermal->sensors[1].tzd);
598 err_unregister_cpu_sensor:
599 	thermal_zone_of_sensor_unregister(&pdev->dev, thermal->sensors[0].tzd);
600 err_disable_pclk:
601 	clk_disable_unprepare(thermal->pclk);
602 err_disable_clk:
603 	clk_disable_unprepare(thermal->clk);
604 
605 	return error;
606 }
607 
608 static int rockchip_thermal_remove(struct platform_device *pdev)
609 {
610 	struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
611 	int i;
612 
613 	for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++) {
614 		struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
615 
616 		rockchip_thermal_toggle_sensor(sensor, false);
617 		thermal_zone_of_sensor_unregister(&pdev->dev, sensor->tzd);
618 	}
619 
620 	thermal->chip->control(thermal->regs, false);
621 
622 	clk_disable_unprepare(thermal->pclk);
623 	clk_disable_unprepare(thermal->clk);
624 
625 	return 0;
626 }
627 
628 static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
629 {
630 	struct platform_device *pdev = to_platform_device(dev);
631 	struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
632 	int i;
633 
634 	for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
635 		rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
636 
637 	thermal->chip->control(thermal->regs, false);
638 
639 	clk_disable(thermal->pclk);
640 	clk_disable(thermal->clk);
641 
642 	pinctrl_pm_select_sleep_state(dev);
643 
644 	return 0;
645 }
646 
647 static int __maybe_unused rockchip_thermal_resume(struct device *dev)
648 {
649 	struct platform_device *pdev = to_platform_device(dev);
650 	struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
651 	int i;
652 	int error;
653 
654 	error = clk_enable(thermal->clk);
655 	if (error)
656 		return error;
657 
658 	error = clk_enable(thermal->pclk);
659 	if (error)
660 		return error;
661 
662 	rockchip_thermal_reset_controller(thermal->reset);
663 
664 	thermal->chip->initialize(thermal->regs, thermal->tshut_polarity);
665 
666 	for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++) {
667 		enum sensor_id id = thermal->sensors[i].id;
668 
669 		thermal->chip->set_tshut_mode(id, thermal->regs,
670 					      thermal->tshut_mode);
671 		thermal->chip->set_tshut_temp(id, thermal->regs,
672 					      thermal->tshut_temp);
673 	}
674 
675 	thermal->chip->control(thermal->regs, true);
676 
677 	for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
678 		rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
679 
680 	pinctrl_pm_select_default_state(dev);
681 
682 	return 0;
683 }
684 
685 static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
686 			 rockchip_thermal_suspend, rockchip_thermal_resume);
687 
688 static struct platform_driver rockchip_thermal_driver = {
689 	.driver = {
690 		.name = "rockchip-thermal",
691 		.pm = &rockchip_thermal_pm_ops,
692 		.of_match_table = of_rockchip_thermal_match,
693 	},
694 	.probe = rockchip_thermal_probe,
695 	.remove = rockchip_thermal_remove,
696 };
697 
698 module_platform_driver(rockchip_thermal_driver);
699 
700 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
701 MODULE_AUTHOR("Rockchip, Inc.");
702 MODULE_LICENSE("GPL v2");
703 MODULE_ALIAS("platform:rockchip-thermal");
704