2 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
4 * Copyright (c) 2015, Fuzhou Rockchip Electronics Co., Ltd
5 * Caesar Wang <wxt@rock-chips.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/interrupt.h>
21 #include <linux/module.h>
23 #include <linux/of_address.h>
24 #include <linux/of_irq.h>
25 #include <linux/platform_device.h>
26 #include <linux/reset.h>
27 #include <linux/thermal.h>
28 #include <linux/pinctrl/consumer.h>
31 * If the temperature over a period of time High,
32 * the resulting TSHUT gave CRU module,let it reset the entire chip,
33 * or via GPIO give PMIC.
41 * The system Temperature Sensors tshut(tshut) polarity
42 * the bit 8 is tshut polarity.
43 * 0: low active, 1: high active
51 * The system has two Temperature Sensors.
52 * sensor0 is for CPU, and sensor1 is for GPU.
60 * The conversion table has the adc value and temperature.
61 * ADC_DECREMENT: the adc value is of diminishing.(e.g. v2_code_table)
62 * ADC_INCREMENT: the adc value is incremental.(e.g. v3_code_table)
70 * The max sensors is two in rockchip SoCs.
71 * Two sensors: CPU and GPU sensor.
73 #define SOC_MAX_SENSORS 2
76 * struct chip_tsadc_table: hold information about chip-specific differences
77 * @id: conversion table
78 * @length: size of conversion table
79 * @data_mask: mask to apply on data inputs
80 * @mode: sort mode of this adc variant (incrementing or decrementing)
82 struct chip_tsadc_table
{
83 const struct tsadc_table
*id
;
86 enum adc_sort_mode mode
;
89 struct rockchip_tsadc_chip
{
90 /* The sensor id of chip correspond to the ADC channel */
91 int chn_id
[SOC_MAX_SENSORS
];
94 /* The hardware-controlled tshut property */
96 enum tshut_mode tshut_mode
;
97 enum tshut_polarity tshut_polarity
;
99 /* Chip-wide methods */
100 void (*initialize
)(void __iomem
*reg
, enum tshut_polarity p
);
101 void (*irq_ack
)(void __iomem
*reg
);
102 void (*control
)(void __iomem
*reg
, bool on
);
104 /* Per-sensor methods */
105 int (*get_temp
)(struct chip_tsadc_table table
,
106 int chn
, void __iomem
*reg
, int *temp
);
107 void (*set_tshut_temp
)(struct chip_tsadc_table table
,
108 int chn
, void __iomem
*reg
, int temp
);
109 void (*set_tshut_mode
)(int chn
, void __iomem
*reg
, enum tshut_mode m
);
111 /* Per-table methods */
112 struct chip_tsadc_table table
;
115 struct rockchip_thermal_sensor
{
116 struct rockchip_thermal_data
*thermal
;
117 struct thermal_zone_device
*tzd
;
121 struct rockchip_thermal_data
{
122 const struct rockchip_tsadc_chip
*chip
;
123 struct platform_device
*pdev
;
124 struct reset_control
*reset
;
126 struct rockchip_thermal_sensor sensors
[SOC_MAX_SENSORS
];
134 enum tshut_mode tshut_mode
;
135 enum tshut_polarity tshut_polarity
;
138 /* TSADC Sensor info define: */
139 #define TSADCV2_AUTO_CON 0x04
140 #define TSADCV2_INT_EN 0x08
141 #define TSADCV2_INT_PD 0x0c
142 #define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04)
143 #define TSADCV2_COMP_SHUT(chn) (0x40 + (chn) * 0x04)
144 #define TSADCV2_HIGHT_INT_DEBOUNCE 0x60
145 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE 0x64
146 #define TSADCV2_AUTO_PERIOD 0x68
147 #define TSADCV2_AUTO_PERIOD_HT 0x6c
149 #define TSADCV2_AUTO_EN BIT(0)
150 #define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn))
151 #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8)
153 #define TSADCV2_INT_SRC_EN(chn) BIT(chn)
154 #define TSADCV2_SHUT_2GPIO_SRC_EN(chn) BIT(4 + (chn))
155 #define TSADCV2_SHUT_2CRU_SRC_EN(chn) BIT(8 + (chn))
157 #define TSADCV1_INT_PD_CLEAR_MASK ~BIT(16)
158 #define TSADCV2_INT_PD_CLEAR_MASK ~BIT(8)
160 #define TSADCV2_DATA_MASK 0xfff
161 #define TSADCV3_DATA_MASK 0x3ff
163 #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT 4
164 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT 4
165 #define TSADCV2_AUTO_PERIOD_TIME 250 /* msec */
166 #define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* msec */
175 * Code to Temperature mapping of the Temperature sensor is a piece wise linear
176 * curve.Any temperature, code faling between to 2 give temperatures can be
177 * linearly interpolated.
178 * Code to Temperature mapping should be updated based on sillcon results.
180 static const struct tsadc_table v1_code_table
[] = {
181 {TSADCV3_DATA_MASK
, -40000},
218 static const struct tsadc_table v2_code_table
[] = {
219 {TSADCV2_DATA_MASK
, -40000},
256 static const struct tsadc_table v3_code_table
[] = {
292 {TSADCV3_DATA_MASK
, 125000},
295 static u32
rk_tsadcv2_temp_to_code(struct chip_tsadc_table table
,
301 high
= table
.length
- 1;
302 mid
= (high
+ low
) / 2;
304 if (temp
< table
.id
[low
].temp
|| temp
> table
.id
[high
].temp
)
307 while (low
<= high
) {
308 if (temp
== table
.id
[mid
].temp
)
309 return table
.id
[mid
].code
;
310 else if (temp
< table
.id
[mid
].temp
)
314 mid
= (low
+ high
) / 2;
320 static int rk_tsadcv2_code_to_temp(struct chip_tsadc_table table
, u32 code
,
323 unsigned int low
= 1;
324 unsigned int high
= table
.length
- 1;
325 unsigned int mid
= (low
+ high
) / 2;
329 WARN_ON(table
.length
< 2);
331 switch (table
.mode
) {
333 code
&= table
.data_mask
;
334 if (code
< table
.id
[high
].code
)
335 return -EAGAIN
; /* Incorrect reading */
337 while (low
<= high
) {
338 if (code
>= table
.id
[mid
].code
&&
339 code
< table
.id
[mid
- 1].code
)
341 else if (code
< table
.id
[mid
].code
)
346 mid
= (low
+ high
) / 2;
350 code
&= table
.data_mask
;
351 if (code
< table
.id
[low
].code
)
352 return -EAGAIN
; /* Incorrect reading */
354 while (low
<= high
) {
355 if (code
>= table
.id
[mid
- 1].code
&&
356 code
< table
.id
[mid
].code
)
358 else if (code
> table
.id
[mid
].code
)
363 mid
= (low
+ high
) / 2;
367 pr_err("Invalid the conversion table\n");
371 * The 5C granularity provided by the table is too much. Let's
372 * assume that the relationship between sensor readings and
373 * temperature between 2 table entries is linear and interpolate
374 * to produce less granular result.
376 num
= table
.id
[mid
].temp
- v2_code_table
[mid
- 1].temp
;
377 num
*= abs(table
.id
[mid
- 1].code
- code
);
378 denom
= abs(table
.id
[mid
- 1].code
- table
.id
[mid
].code
);
379 *temp
= table
.id
[mid
- 1].temp
+ (num
/ denom
);
385 * rk_tsadcv2_initialize - initialize TASDC Controller.
387 * (1) Set TSADC_V2_AUTO_PERIOD:
388 * Configure the interleave between every two accessing of
389 * TSADC in normal operation.
391 * (2) Set TSADCV2_AUTO_PERIOD_HT:
392 * Configure the interleave between every two accessing of
393 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
395 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
396 * If the temperature is higher than COMP_INT or COMP_SHUT for
397 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
399 static void rk_tsadcv2_initialize(void __iomem
*regs
,
400 enum tshut_polarity tshut_polarity
)
402 if (tshut_polarity
== TSHUT_HIGH_ACTIVE
)
403 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH
,
404 regs
+ TSADCV2_AUTO_CON
);
406 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH
,
407 regs
+ TSADCV2_AUTO_CON
);
409 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME
, regs
+ TSADCV2_AUTO_PERIOD
);
410 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT
,
411 regs
+ TSADCV2_HIGHT_INT_DEBOUNCE
);
412 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME
,
413 regs
+ TSADCV2_AUTO_PERIOD_HT
);
414 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT
,
415 regs
+ TSADCV2_HIGHT_TSHUT_DEBOUNCE
);
418 static void rk_tsadcv1_irq_ack(void __iomem
*regs
)
422 val
= readl_relaxed(regs
+ TSADCV2_INT_PD
);
423 writel_relaxed(val
& TSADCV1_INT_PD_CLEAR_MASK
, regs
+ TSADCV2_INT_PD
);
426 static void rk_tsadcv2_irq_ack(void __iomem
*regs
)
430 val
= readl_relaxed(regs
+ TSADCV2_INT_PD
);
431 writel_relaxed(val
& TSADCV2_INT_PD_CLEAR_MASK
, regs
+ TSADCV2_INT_PD
);
434 static void rk_tsadcv2_control(void __iomem
*regs
, bool enable
)
438 val
= readl_relaxed(regs
+ TSADCV2_AUTO_CON
);
440 val
|= TSADCV2_AUTO_EN
;
442 val
&= ~TSADCV2_AUTO_EN
;
444 writel_relaxed(val
, regs
+ TSADCV2_AUTO_CON
);
447 static int rk_tsadcv2_get_temp(struct chip_tsadc_table table
,
448 int chn
, void __iomem
*regs
, int *temp
)
452 val
= readl_relaxed(regs
+ TSADCV2_DATA(chn
));
454 return rk_tsadcv2_code_to_temp(table
, val
, temp
);
457 static void rk_tsadcv2_tshut_temp(struct chip_tsadc_table table
,
458 int chn
, void __iomem
*regs
, int temp
)
460 u32 tshut_value
, val
;
462 tshut_value
= rk_tsadcv2_temp_to_code(table
, temp
);
463 writel_relaxed(tshut_value
, regs
+ TSADCV2_COMP_SHUT(chn
));
465 /* TSHUT will be valid */
466 val
= readl_relaxed(regs
+ TSADCV2_AUTO_CON
);
467 writel_relaxed(val
| TSADCV2_AUTO_SRC_EN(chn
), regs
+ TSADCV2_AUTO_CON
);
470 static void rk_tsadcv2_tshut_mode(int chn
, void __iomem
*regs
,
471 enum tshut_mode mode
)
475 val
= readl_relaxed(regs
+ TSADCV2_INT_EN
);
476 if (mode
== TSHUT_MODE_GPIO
) {
477 val
&= ~TSADCV2_SHUT_2CRU_SRC_EN(chn
);
478 val
|= TSADCV2_SHUT_2GPIO_SRC_EN(chn
);
480 val
&= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn
);
481 val
|= TSADCV2_SHUT_2CRU_SRC_EN(chn
);
484 writel_relaxed(val
, regs
+ TSADCV2_INT_EN
);
487 static const struct rockchip_tsadc_chip rk3228_tsadc_data
= {
488 .chn_id
[SENSOR_CPU
] = 0, /* cpu sensor is channel 0 */
489 .chn_num
= 1, /* one channel for tsadc */
491 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
492 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
495 .initialize
= rk_tsadcv2_initialize
,
496 .irq_ack
= rk_tsadcv1_irq_ack
,
497 .control
= rk_tsadcv2_control
,
498 .get_temp
= rk_tsadcv2_get_temp
,
499 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
500 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
504 .length
= ARRAY_SIZE(v1_code_table
),
505 .data_mask
= TSADCV3_DATA_MASK
,
506 .mode
= ADC_DECREMENT
,
510 static const struct rockchip_tsadc_chip rk3288_tsadc_data
= {
511 .chn_id
[SENSOR_CPU
] = 1, /* cpu sensor is channel 1 */
512 .chn_id
[SENSOR_GPU
] = 2, /* gpu sensor is channel 2 */
513 .chn_num
= 2, /* two channels for tsadc */
515 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
516 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
519 .initialize
= rk_tsadcv2_initialize
,
520 .irq_ack
= rk_tsadcv2_irq_ack
,
521 .control
= rk_tsadcv2_control
,
522 .get_temp
= rk_tsadcv2_get_temp
,
523 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
524 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
528 .length
= ARRAY_SIZE(v2_code_table
),
529 .data_mask
= TSADCV2_DATA_MASK
,
530 .mode
= ADC_DECREMENT
,
534 static const struct rockchip_tsadc_chip rk3368_tsadc_data
= {
535 .chn_id
[SENSOR_CPU
] = 0, /* cpu sensor is channel 0 */
536 .chn_id
[SENSOR_GPU
] = 1, /* gpu sensor is channel 1 */
537 .chn_num
= 2, /* two channels for tsadc */
539 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
540 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
543 .initialize
= rk_tsadcv2_initialize
,
544 .irq_ack
= rk_tsadcv2_irq_ack
,
545 .control
= rk_tsadcv2_control
,
546 .get_temp
= rk_tsadcv2_get_temp
,
547 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
548 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
552 .length
= ARRAY_SIZE(v3_code_table
),
553 .data_mask
= TSADCV3_DATA_MASK
,
554 .mode
= ADC_INCREMENT
,
558 static const struct of_device_id of_rockchip_thermal_match
[] = {
560 .compatible
= "rockchip,rk3228-tsadc",
561 .data
= (void *)&rk3228_tsadc_data
,
564 .compatible
= "rockchip,rk3288-tsadc",
565 .data
= (void *)&rk3288_tsadc_data
,
568 .compatible
= "rockchip,rk3368-tsadc",
569 .data
= (void *)&rk3368_tsadc_data
,
573 MODULE_DEVICE_TABLE(of
, of_rockchip_thermal_match
);
576 rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor
*sensor
, bool on
)
578 struct thermal_zone_device
*tzd
= sensor
->tzd
;
580 tzd
->ops
->set_mode(tzd
,
581 on
? THERMAL_DEVICE_ENABLED
: THERMAL_DEVICE_DISABLED
);
584 static irqreturn_t
rockchip_thermal_alarm_irq_thread(int irq
, void *dev
)
586 struct rockchip_thermal_data
*thermal
= dev
;
589 dev_dbg(&thermal
->pdev
->dev
, "thermal alarm\n");
591 thermal
->chip
->irq_ack(thermal
->regs
);
593 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++)
594 thermal_zone_device_update(thermal
->sensors
[i
].tzd
);
599 static int rockchip_thermal_get_temp(void *_sensor
, int *out_temp
)
601 struct rockchip_thermal_sensor
*sensor
= _sensor
;
602 struct rockchip_thermal_data
*thermal
= sensor
->thermal
;
603 const struct rockchip_tsadc_chip
*tsadc
= sensor
->thermal
->chip
;
606 retval
= tsadc
->get_temp(tsadc
->table
,
607 sensor
->id
, thermal
->regs
, out_temp
);
608 dev_dbg(&thermal
->pdev
->dev
, "sensor %d - temp: %d, retval: %d\n",
609 sensor
->id
, *out_temp
, retval
);
614 static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops
= {
615 .get_temp
= rockchip_thermal_get_temp
,
618 static int rockchip_configure_from_dt(struct device
*dev
,
619 struct device_node
*np
,
620 struct rockchip_thermal_data
*thermal
)
622 u32 shut_temp
, tshut_mode
, tshut_polarity
;
624 if (of_property_read_u32(np
, "rockchip,hw-tshut-temp", &shut_temp
)) {
626 "Missing tshut temp property, using default %d\n",
627 thermal
->chip
->tshut_temp
);
628 thermal
->tshut_temp
= thermal
->chip
->tshut_temp
;
630 thermal
->tshut_temp
= shut_temp
;
633 if (thermal
->tshut_temp
> INT_MAX
) {
634 dev_err(dev
, "Invalid tshut temperature specified: %d\n",
635 thermal
->tshut_temp
);
639 if (of_property_read_u32(np
, "rockchip,hw-tshut-mode", &tshut_mode
)) {
641 "Missing tshut mode property, using default (%s)\n",
642 thermal
->chip
->tshut_mode
== TSHUT_MODE_GPIO
?
644 thermal
->tshut_mode
= thermal
->chip
->tshut_mode
;
646 thermal
->tshut_mode
= tshut_mode
;
649 if (thermal
->tshut_mode
> 1) {
650 dev_err(dev
, "Invalid tshut mode specified: %d\n",
651 thermal
->tshut_mode
);
655 if (of_property_read_u32(np
, "rockchip,hw-tshut-polarity",
658 "Missing tshut-polarity property, using default (%s)\n",
659 thermal
->chip
->tshut_polarity
== TSHUT_LOW_ACTIVE
?
661 thermal
->tshut_polarity
= thermal
->chip
->tshut_polarity
;
663 thermal
->tshut_polarity
= tshut_polarity
;
666 if (thermal
->tshut_polarity
> 1) {
667 dev_err(dev
, "Invalid tshut-polarity specified: %d\n",
668 thermal
->tshut_polarity
);
676 rockchip_thermal_register_sensor(struct platform_device
*pdev
,
677 struct rockchip_thermal_data
*thermal
,
678 struct rockchip_thermal_sensor
*sensor
,
681 const struct rockchip_tsadc_chip
*tsadc
= thermal
->chip
;
684 tsadc
->set_tshut_mode(id
, thermal
->regs
, thermal
->tshut_mode
);
685 tsadc
->set_tshut_temp(tsadc
->table
, id
, thermal
->regs
,
686 thermal
->tshut_temp
);
688 sensor
->thermal
= thermal
;
690 sensor
->tzd
= thermal_zone_of_sensor_register(&pdev
->dev
, id
, sensor
,
691 &rockchip_of_thermal_ops
);
692 if (IS_ERR(sensor
->tzd
)) {
693 error
= PTR_ERR(sensor
->tzd
);
694 dev_err(&pdev
->dev
, "failed to register sensor %d: %d\n",
703 * Reset TSADC Controller, reset all tsadc registers.
705 static void rockchip_thermal_reset_controller(struct reset_control
*reset
)
707 reset_control_assert(reset
);
708 usleep_range(10, 20);
709 reset_control_deassert(reset
);
712 static int rockchip_thermal_probe(struct platform_device
*pdev
)
714 struct device_node
*np
= pdev
->dev
.of_node
;
715 struct rockchip_thermal_data
*thermal
;
716 const struct of_device_id
*match
;
717 struct resource
*res
;
722 match
= of_match_node(of_rockchip_thermal_match
, np
);
726 irq
= platform_get_irq(pdev
, 0);
728 dev_err(&pdev
->dev
, "no irq resource?\n");
732 thermal
= devm_kzalloc(&pdev
->dev
, sizeof(struct rockchip_thermal_data
),
737 thermal
->pdev
= pdev
;
739 thermal
->chip
= (const struct rockchip_tsadc_chip
*)match
->data
;
743 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
744 thermal
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
745 if (IS_ERR(thermal
->regs
))
746 return PTR_ERR(thermal
->regs
);
748 thermal
->reset
= devm_reset_control_get(&pdev
->dev
, "tsadc-apb");
749 if (IS_ERR(thermal
->reset
)) {
750 error
= PTR_ERR(thermal
->reset
);
751 dev_err(&pdev
->dev
, "failed to get tsadc reset: %d\n", error
);
755 thermal
->clk
= devm_clk_get(&pdev
->dev
, "tsadc");
756 if (IS_ERR(thermal
->clk
)) {
757 error
= PTR_ERR(thermal
->clk
);
758 dev_err(&pdev
->dev
, "failed to get tsadc clock: %d\n", error
);
762 thermal
->pclk
= devm_clk_get(&pdev
->dev
, "apb_pclk");
763 if (IS_ERR(thermal
->pclk
)) {
764 error
= PTR_ERR(thermal
->pclk
);
765 dev_err(&pdev
->dev
, "failed to get apb_pclk clock: %d\n",
770 error
= clk_prepare_enable(thermal
->clk
);
772 dev_err(&pdev
->dev
, "failed to enable converter clock: %d\n",
777 error
= clk_prepare_enable(thermal
->pclk
);
779 dev_err(&pdev
->dev
, "failed to enable pclk: %d\n", error
);
780 goto err_disable_clk
;
783 rockchip_thermal_reset_controller(thermal
->reset
);
785 error
= rockchip_configure_from_dt(&pdev
->dev
, np
, thermal
);
787 dev_err(&pdev
->dev
, "failed to parse device tree data: %d\n",
789 goto err_disable_pclk
;
792 thermal
->chip
->initialize(thermal
->regs
, thermal
->tshut_polarity
);
794 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++) {
795 error
= rockchip_thermal_register_sensor(pdev
, thermal
,
796 &thermal
->sensors
[i
],
797 thermal
->chip
->chn_id
[i
]);
800 "failed to register sensor[%d] : error = %d\n",
802 for (j
= 0; j
< i
; j
++)
803 thermal_zone_of_sensor_unregister(&pdev
->dev
,
804 thermal
->sensors
[j
].tzd
);
805 goto err_disable_pclk
;
809 error
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
810 &rockchip_thermal_alarm_irq_thread
,
812 "rockchip_thermal", thermal
);
815 "failed to request tsadc irq: %d\n", error
);
816 goto err_unregister_sensor
;
819 thermal
->chip
->control(thermal
->regs
, true);
821 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++)
822 rockchip_thermal_toggle_sensor(&thermal
->sensors
[i
], true);
824 platform_set_drvdata(pdev
, thermal
);
828 err_unregister_sensor
:
830 thermal_zone_of_sensor_unregister(&pdev
->dev
,
831 thermal
->sensors
[i
].tzd
);
834 clk_disable_unprepare(thermal
->pclk
);
836 clk_disable_unprepare(thermal
->clk
);
841 static int rockchip_thermal_remove(struct platform_device
*pdev
)
843 struct rockchip_thermal_data
*thermal
= platform_get_drvdata(pdev
);
846 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++) {
847 struct rockchip_thermal_sensor
*sensor
= &thermal
->sensors
[i
];
849 rockchip_thermal_toggle_sensor(sensor
, false);
850 thermal_zone_of_sensor_unregister(&pdev
->dev
, sensor
->tzd
);
853 thermal
->chip
->control(thermal
->regs
, false);
855 clk_disable_unprepare(thermal
->pclk
);
856 clk_disable_unprepare(thermal
->clk
);
861 static int __maybe_unused
rockchip_thermal_suspend(struct device
*dev
)
863 struct platform_device
*pdev
= to_platform_device(dev
);
864 struct rockchip_thermal_data
*thermal
= platform_get_drvdata(pdev
);
867 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++)
868 rockchip_thermal_toggle_sensor(&thermal
->sensors
[i
], false);
870 thermal
->chip
->control(thermal
->regs
, false);
872 clk_disable(thermal
->pclk
);
873 clk_disable(thermal
->clk
);
875 pinctrl_pm_select_sleep_state(dev
);
880 static int __maybe_unused
rockchip_thermal_resume(struct device
*dev
)
882 struct platform_device
*pdev
= to_platform_device(dev
);
883 struct rockchip_thermal_data
*thermal
= platform_get_drvdata(pdev
);
887 error
= clk_enable(thermal
->clk
);
891 error
= clk_enable(thermal
->pclk
);
895 rockchip_thermal_reset_controller(thermal
->reset
);
897 thermal
->chip
->initialize(thermal
->regs
, thermal
->tshut_polarity
);
899 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++) {
900 int id
= thermal
->sensors
[i
].id
;
902 thermal
->chip
->set_tshut_mode(id
, thermal
->regs
,
903 thermal
->tshut_mode
);
904 thermal
->chip
->set_tshut_temp(thermal
->chip
->table
,
906 thermal
->tshut_temp
);
909 thermal
->chip
->control(thermal
->regs
, true);
911 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++)
912 rockchip_thermal_toggle_sensor(&thermal
->sensors
[i
], true);
914 pinctrl_pm_select_default_state(dev
);
919 static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops
,
920 rockchip_thermal_suspend
, rockchip_thermal_resume
);
922 static struct platform_driver rockchip_thermal_driver
= {
924 .name
= "rockchip-thermal",
925 .pm
= &rockchip_thermal_pm_ops
,
926 .of_match_table
= of_rockchip_thermal_match
,
928 .probe
= rockchip_thermal_probe
,
929 .remove
= rockchip_thermal_remove
,
932 module_platform_driver(rockchip_thermal_driver
);
934 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
935 MODULE_AUTHOR("Rockchip, Inc.");
936 MODULE_LICENSE("GPL v2");
937 MODULE_ALIAS("platform:rockchip-thermal");