2 * Copyright (c) 2014-2016, Fuzhou Rockchip Electronics Co., Ltd
3 * Caesar Wang <wxt@rock-chips.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
19 #include <linux/module.h>
21 #include <linux/of_address.h>
22 #include <linux/of_irq.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/reset.h>
26 #include <linux/thermal.h>
27 #include <linux/mfd/syscon.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. rk3288_code_table)
62 * ADC_INCREMENT: the adc value is incremental.(e.g. rk3368_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
;
90 * struct rockchip_tsadc_chip - hold the private data of tsadc chip
91 * @chn_id[SOC_MAX_SENSORS]: the sensor id of chip correspond to the channel
92 * @chn_num: the channel number of tsadc chip
93 * @tshut_temp: the hardware-controlled shutdown temperature value
94 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
95 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
96 * @initialize: SoC special initialize tsadc controller method
97 * @irq_ack: clear the interrupt
98 * @get_temp: get the temperature
99 * @set_alarm_temp: set the high temperature interrupt
100 * @set_tshut_temp: set the hardware-controlled shutdown temperature
101 * @set_tshut_mode: set the hardware-controlled shutdown mode
102 * @table: the chip-specific conversion table
104 struct rockchip_tsadc_chip
{
105 /* The sensor id of chip correspond to the ADC channel */
106 int chn_id
[SOC_MAX_SENSORS
];
109 /* The hardware-controlled tshut property */
111 enum tshut_mode tshut_mode
;
112 enum tshut_polarity tshut_polarity
;
114 /* Chip-wide methods */
115 void (*initialize
)(struct regmap
*grf
,
116 void __iomem
*reg
, enum tshut_polarity p
);
117 void (*irq_ack
)(void __iomem
*reg
);
118 void (*control
)(void __iomem
*reg
, bool on
);
120 /* Per-sensor methods */
121 int (*get_temp
)(struct chip_tsadc_table table
,
122 int chn
, void __iomem
*reg
, int *temp
);
123 void (*set_alarm_temp
)(struct chip_tsadc_table table
,
124 int chn
, void __iomem
*reg
, int temp
);
125 void (*set_tshut_temp
)(struct chip_tsadc_table table
,
126 int chn
, void __iomem
*reg
, int temp
);
127 void (*set_tshut_mode
)(int chn
, void __iomem
*reg
, enum tshut_mode m
);
129 /* Per-table methods */
130 struct chip_tsadc_table table
;
134 * struct rockchip_thermal_sensor - hold the information of thermal sensor
135 * @thermal: pointer to the platform/configuration data
136 * @tzd: pointer to a thermal zone
137 * @id: identifier of the thermal sensor
139 struct rockchip_thermal_sensor
{
140 struct rockchip_thermal_data
*thermal
;
141 struct thermal_zone_device
*tzd
;
146 * struct rockchip_thermal_data - hold the private data of thermal driver
147 * @chip: pointer to the platform/configuration data
148 * @pdev: platform device of thermal
149 * @reset: the reset controller of tsadc
150 * @sensors[SOC_MAX_SENSORS]: the thermal sensor
151 * @clk: the controller clock is divided by the exteral 24MHz
152 * @pclk: the advanced peripherals bus clock
153 * @grf: the general register file will be used to do static set by software
154 * @regs: the base address of tsadc controller
155 * @tshut_temp: the hardware-controlled shutdown temperature value
156 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
157 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
159 struct rockchip_thermal_data
{
160 const struct rockchip_tsadc_chip
*chip
;
161 struct platform_device
*pdev
;
162 struct reset_control
*reset
;
164 struct rockchip_thermal_sensor sensors
[SOC_MAX_SENSORS
];
173 enum tshut_mode tshut_mode
;
174 enum tshut_polarity tshut_polarity
;
178 * TSADC Sensor Register description:
180 * TSADCV2_* are used for RK3288 SoCs, the other chips can reuse it.
181 * TSADCV3_* are used for newer SoCs than RK3288. (e.g: RK3228, RK3399)
184 #define TSADCV2_USER_CON 0x00
185 #define TSADCV2_AUTO_CON 0x04
186 #define TSADCV2_INT_EN 0x08
187 #define TSADCV2_INT_PD 0x0c
188 #define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04)
189 #define TSADCV2_COMP_INT(chn) (0x30 + (chn) * 0x04)
190 #define TSADCV2_COMP_SHUT(chn) (0x40 + (chn) * 0x04)
191 #define TSADCV2_HIGHT_INT_DEBOUNCE 0x60
192 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE 0x64
193 #define TSADCV2_AUTO_PERIOD 0x68
194 #define TSADCV2_AUTO_PERIOD_HT 0x6c
196 #define TSADCV2_AUTO_EN BIT(0)
197 #define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn))
198 #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8)
200 #define TSADCV3_AUTO_Q_SEL_EN BIT(1)
202 #define TSADCV2_INT_SRC_EN(chn) BIT(chn)
203 #define TSADCV2_SHUT_2GPIO_SRC_EN(chn) BIT(4 + (chn))
204 #define TSADCV2_SHUT_2CRU_SRC_EN(chn) BIT(8 + (chn))
206 #define TSADCV2_INT_PD_CLEAR_MASK ~BIT(8)
207 #define TSADCV3_INT_PD_CLEAR_MASK ~BIT(16)
209 #define TSADCV2_DATA_MASK 0xfff
210 #define TSADCV3_DATA_MASK 0x3ff
212 #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT 4
213 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT 4
214 #define TSADCV2_AUTO_PERIOD_TIME 250 /* 250ms */
215 #define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* 50ms */
216 #define TSADCV3_AUTO_PERIOD_TIME 1875 /* 2.5ms */
217 #define TSADCV3_AUTO_PERIOD_HT_TIME 1875 /* 2.5ms */
219 #define TSADCV2_USER_INTER_PD_SOC 0x340 /* 13 clocks */
221 #define GRF_SARADC_TESTBIT 0x0e644
222 #define GRF_TSADC_TESTBIT_L 0x0e648
223 #define GRF_TSADC_TESTBIT_H 0x0e64c
225 #define GRF_SARADC_TESTBIT_ON (0x10001 << 2)
226 #define GRF_TSADC_TESTBIT_H_ON (0x10001 << 2)
227 #define GRF_TSADC_VCM_EN_L (0x10001 << 7)
228 #define GRF_TSADC_VCM_EN_H (0x10001 << 7)
231 * struct tsadc_table - code to temperature conversion table
232 * @code: the value of adc channel
233 * @temp: the temperature
235 * code to temperature mapping of the temperature sensor is a piece wise linear
236 * curve.Any temperature, code faling between to 2 give temperatures can be
237 * linearly interpolated.
238 * Code to Temperature mapping should be updated based on manufacturer results.
245 static const struct tsadc_table rk3228_code_table
[] = {
281 {TSADCV2_DATA_MASK
, 125000},
284 static const struct tsadc_table rk3288_code_table
[] = {
285 {TSADCV2_DATA_MASK
, -40000},
322 static const struct tsadc_table rk3368_code_table
[] = {
358 {TSADCV3_DATA_MASK
, 125000},
361 static const struct tsadc_table rk3399_code_table
[] = {
397 {TSADCV3_DATA_MASK
, 125000},
400 static u32
rk_tsadcv2_temp_to_code(struct chip_tsadc_table table
,
407 high
= table
.length
- 1;
408 mid
= (high
+ low
) / 2;
410 /* Return mask code data when the temp is over table range */
411 if (temp
< table
.id
[low
].temp
|| temp
> table
.id
[high
].temp
) {
412 error
= table
.data_mask
;
416 while (low
<= high
) {
417 if (temp
== table
.id
[mid
].temp
)
418 return table
.id
[mid
].code
;
419 else if (temp
< table
.id
[mid
].temp
)
423 mid
= (low
+ high
) / 2;
427 pr_err("Invalid the conversion, error=%d\n", error
);
431 static int rk_tsadcv2_code_to_temp(struct chip_tsadc_table table
, u32 code
,
434 unsigned int low
= 1;
435 unsigned int high
= table
.length
- 1;
436 unsigned int mid
= (low
+ high
) / 2;
440 WARN_ON(table
.length
< 2);
442 switch (table
.mode
) {
444 code
&= table
.data_mask
;
445 if (code
< table
.id
[high
].code
)
446 return -EAGAIN
; /* Incorrect reading */
448 while (low
<= high
) {
449 if (code
>= table
.id
[mid
].code
&&
450 code
< table
.id
[mid
- 1].code
)
452 else if (code
< table
.id
[mid
].code
)
457 mid
= (low
+ high
) / 2;
461 code
&= table
.data_mask
;
462 if (code
< table
.id
[low
].code
)
463 return -EAGAIN
; /* Incorrect reading */
465 while (low
<= high
) {
466 if (code
<= table
.id
[mid
].code
&&
467 code
> table
.id
[mid
- 1].code
)
469 else if (code
> table
.id
[mid
].code
)
474 mid
= (low
+ high
) / 2;
478 pr_err("Invalid the conversion table\n");
482 * The 5C granularity provided by the table is too much. Let's
483 * assume that the relationship between sensor readings and
484 * temperature between 2 table entries is linear and interpolate
485 * to produce less granular result.
487 num
= table
.id
[mid
].temp
- table
.id
[mid
- 1].temp
;
488 num
*= abs(table
.id
[mid
- 1].code
- code
);
489 denom
= abs(table
.id
[mid
- 1].code
- table
.id
[mid
].code
);
490 *temp
= table
.id
[mid
- 1].temp
+ (num
/ denom
);
496 * rk_tsadcv2_initialize - initialize TASDC Controller.
498 * (1) Set TSADC_V2_AUTO_PERIOD:
499 * Configure the interleave between every two accessing of
500 * TSADC in normal operation.
502 * (2) Set TSADCV2_AUTO_PERIOD_HT:
503 * Configure the interleave between every two accessing of
504 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
506 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
507 * If the temperature is higher than COMP_INT or COMP_SHUT for
508 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
510 static void rk_tsadcv2_initialize(struct regmap
*grf
, void __iomem
*regs
,
511 enum tshut_polarity tshut_polarity
)
513 if (tshut_polarity
== TSHUT_HIGH_ACTIVE
)
514 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH
,
515 regs
+ TSADCV2_AUTO_CON
);
517 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH
,
518 regs
+ TSADCV2_AUTO_CON
);
520 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME
, regs
+ TSADCV2_AUTO_PERIOD
);
521 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT
,
522 regs
+ TSADCV2_HIGHT_INT_DEBOUNCE
);
523 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME
,
524 regs
+ TSADCV2_AUTO_PERIOD_HT
);
525 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT
,
526 regs
+ TSADCV2_HIGHT_TSHUT_DEBOUNCE
);
529 pr_warn("%s: Missing rockchip,grf property\n", __func__
);
535 * rk_tsadcv3_initialize - initialize TASDC Controller.
537 * (1) The tsadc control power sequence.
539 * (2) Set TSADC_V2_AUTO_PERIOD:
540 * Configure the interleave between every two accessing of
541 * TSADC in normal operation.
543 * (2) Set TSADCV2_AUTO_PERIOD_HT:
544 * Configure the interleave between every two accessing of
545 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
547 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
548 * If the temperature is higher than COMP_INT or COMP_SHUT for
549 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
551 static void rk_tsadcv3_initialize(struct regmap
*grf
, void __iomem
*regs
,
552 enum tshut_polarity tshut_polarity
)
554 /* The tsadc control power sequence */
556 /* Set interleave value to workround ic time sync issue */
557 writel_relaxed(TSADCV2_USER_INTER_PD_SOC
, regs
+
560 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME
,
561 regs
+ TSADCV2_AUTO_PERIOD
);
562 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT
,
563 regs
+ TSADCV2_HIGHT_INT_DEBOUNCE
);
564 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME
,
565 regs
+ TSADCV2_AUTO_PERIOD_HT
);
566 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT
,
567 regs
+ TSADCV2_HIGHT_TSHUT_DEBOUNCE
);
570 /* Enable the voltage common mode feature */
571 regmap_write(grf
, GRF_TSADC_TESTBIT_L
, GRF_TSADC_VCM_EN_L
);
572 regmap_write(grf
, GRF_TSADC_TESTBIT_H
, GRF_TSADC_VCM_EN_H
);
574 usleep_range(15, 100); /* The spec note says at least 15 us */
575 regmap_write(grf
, GRF_SARADC_TESTBIT
, GRF_SARADC_TESTBIT_ON
);
576 regmap_write(grf
, GRF_TSADC_TESTBIT_H
, GRF_TSADC_TESTBIT_H_ON
);
577 usleep_range(90, 200); /* The spec note says at least 90 us */
579 writel_relaxed(TSADCV3_AUTO_PERIOD_TIME
,
580 regs
+ TSADCV2_AUTO_PERIOD
);
581 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT
,
582 regs
+ TSADCV2_HIGHT_INT_DEBOUNCE
);
583 writel_relaxed(TSADCV3_AUTO_PERIOD_HT_TIME
,
584 regs
+ TSADCV2_AUTO_PERIOD_HT
);
585 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT
,
586 regs
+ TSADCV2_HIGHT_TSHUT_DEBOUNCE
);
589 if (tshut_polarity
== TSHUT_HIGH_ACTIVE
)
590 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH
,
591 regs
+ TSADCV2_AUTO_CON
);
593 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH
,
594 regs
+ TSADCV2_AUTO_CON
);
597 static void rk_tsadcv2_irq_ack(void __iomem
*regs
)
601 val
= readl_relaxed(regs
+ TSADCV2_INT_PD
);
602 writel_relaxed(val
& TSADCV2_INT_PD_CLEAR_MASK
, regs
+ TSADCV2_INT_PD
);
605 static void rk_tsadcv3_irq_ack(void __iomem
*regs
)
609 val
= readl_relaxed(regs
+ TSADCV2_INT_PD
);
610 writel_relaxed(val
& TSADCV3_INT_PD_CLEAR_MASK
, regs
+ TSADCV2_INT_PD
);
613 static void rk_tsadcv2_control(void __iomem
*regs
, bool enable
)
617 val
= readl_relaxed(regs
+ TSADCV2_AUTO_CON
);
619 val
|= TSADCV2_AUTO_EN
;
621 val
&= ~TSADCV2_AUTO_EN
;
623 writel_relaxed(val
, regs
+ TSADCV2_AUTO_CON
);
627 * rk_tsadcv3_control - the tsadc controller is enabled or disabled.
629 * NOTE: TSADC controller works at auto mode, and some SoCs need set the
630 * tsadc_q_sel bit on TSADCV2_AUTO_CON[1]. The (1024 - tsadc_q) as output
631 * adc value if setting this bit to enable.
633 static void rk_tsadcv3_control(void __iomem
*regs
, bool enable
)
637 val
= readl_relaxed(regs
+ TSADCV2_AUTO_CON
);
639 val
|= TSADCV2_AUTO_EN
| TSADCV3_AUTO_Q_SEL_EN
;
641 val
&= ~TSADCV2_AUTO_EN
;
643 writel_relaxed(val
, regs
+ TSADCV2_AUTO_CON
);
646 static int rk_tsadcv2_get_temp(struct chip_tsadc_table table
,
647 int chn
, void __iomem
*regs
, int *temp
)
651 val
= readl_relaxed(regs
+ TSADCV2_DATA(chn
));
653 return rk_tsadcv2_code_to_temp(table
, val
, temp
);
656 static void rk_tsadcv2_alarm_temp(struct chip_tsadc_table table
,
657 int chn
, void __iomem
*regs
, int temp
)
659 u32 alarm_value
, int_en
;
661 /* Make sure the value is valid */
662 alarm_value
= rk_tsadcv2_temp_to_code(table
, temp
);
663 if (alarm_value
== table
.data_mask
)
666 writel_relaxed(alarm_value
& table
.data_mask
,
667 regs
+ TSADCV2_COMP_INT(chn
));
669 int_en
= readl_relaxed(regs
+ TSADCV2_INT_EN
);
670 int_en
|= TSADCV2_INT_SRC_EN(chn
);
671 writel_relaxed(int_en
, regs
+ TSADCV2_INT_EN
);
674 static void rk_tsadcv2_tshut_temp(struct chip_tsadc_table table
,
675 int chn
, void __iomem
*regs
, int temp
)
677 u32 tshut_value
, val
;
679 /* Make sure the value is valid */
680 tshut_value
= rk_tsadcv2_temp_to_code(table
, temp
);
681 if (tshut_value
== table
.data_mask
)
684 writel_relaxed(tshut_value
, regs
+ TSADCV2_COMP_SHUT(chn
));
686 /* TSHUT will be valid */
687 val
= readl_relaxed(regs
+ TSADCV2_AUTO_CON
);
688 writel_relaxed(val
| TSADCV2_AUTO_SRC_EN(chn
), regs
+ TSADCV2_AUTO_CON
);
691 static void rk_tsadcv2_tshut_mode(int chn
, void __iomem
*regs
,
692 enum tshut_mode mode
)
696 val
= readl_relaxed(regs
+ TSADCV2_INT_EN
);
697 if (mode
== TSHUT_MODE_GPIO
) {
698 val
&= ~TSADCV2_SHUT_2CRU_SRC_EN(chn
);
699 val
|= TSADCV2_SHUT_2GPIO_SRC_EN(chn
);
701 val
&= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn
);
702 val
|= TSADCV2_SHUT_2CRU_SRC_EN(chn
);
705 writel_relaxed(val
, regs
+ TSADCV2_INT_EN
);
708 static const struct rockchip_tsadc_chip rk3228_tsadc_data
= {
709 .chn_id
[SENSOR_CPU
] = 0, /* cpu sensor is channel 0 */
710 .chn_num
= 1, /* one channel for tsadc */
712 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
713 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
716 .initialize
= rk_tsadcv2_initialize
,
717 .irq_ack
= rk_tsadcv3_irq_ack
,
718 .control
= rk_tsadcv3_control
,
719 .get_temp
= rk_tsadcv2_get_temp
,
720 .set_alarm_temp
= rk_tsadcv2_alarm_temp
,
721 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
722 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
725 .id
= rk3228_code_table
,
726 .length
= ARRAY_SIZE(rk3228_code_table
),
727 .data_mask
= TSADCV3_DATA_MASK
,
728 .mode
= ADC_INCREMENT
,
732 static const struct rockchip_tsadc_chip rk3288_tsadc_data
= {
733 .chn_id
[SENSOR_CPU
] = 1, /* cpu sensor is channel 1 */
734 .chn_id
[SENSOR_GPU
] = 2, /* gpu sensor is channel 2 */
735 .chn_num
= 2, /* two channels for tsadc */
737 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
738 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
741 .initialize
= rk_tsadcv2_initialize
,
742 .irq_ack
= rk_tsadcv2_irq_ack
,
743 .control
= rk_tsadcv2_control
,
744 .get_temp
= rk_tsadcv2_get_temp
,
745 .set_alarm_temp
= rk_tsadcv2_alarm_temp
,
746 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
747 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
750 .id
= rk3288_code_table
,
751 .length
= ARRAY_SIZE(rk3288_code_table
),
752 .data_mask
= TSADCV2_DATA_MASK
,
753 .mode
= ADC_DECREMENT
,
757 static const struct rockchip_tsadc_chip rk3366_tsadc_data
= {
758 .chn_id
[SENSOR_CPU
] = 0, /* cpu sensor is channel 0 */
759 .chn_id
[SENSOR_GPU
] = 1, /* gpu sensor is channel 1 */
760 .chn_num
= 2, /* two channels for tsadc */
762 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
763 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
766 .initialize
= rk_tsadcv3_initialize
,
767 .irq_ack
= rk_tsadcv3_irq_ack
,
768 .control
= rk_tsadcv3_control
,
769 .get_temp
= rk_tsadcv2_get_temp
,
770 .set_alarm_temp
= rk_tsadcv2_alarm_temp
,
771 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
772 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
775 .id
= rk3228_code_table
,
776 .length
= ARRAY_SIZE(rk3228_code_table
),
777 .data_mask
= TSADCV3_DATA_MASK
,
778 .mode
= ADC_INCREMENT
,
782 static const struct rockchip_tsadc_chip rk3368_tsadc_data
= {
783 .chn_id
[SENSOR_CPU
] = 0, /* cpu sensor is channel 0 */
784 .chn_id
[SENSOR_GPU
] = 1, /* gpu sensor is channel 1 */
785 .chn_num
= 2, /* two channels for tsadc */
787 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
788 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
791 .initialize
= rk_tsadcv2_initialize
,
792 .irq_ack
= rk_tsadcv2_irq_ack
,
793 .control
= rk_tsadcv2_control
,
794 .get_temp
= rk_tsadcv2_get_temp
,
795 .set_alarm_temp
= rk_tsadcv2_alarm_temp
,
796 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
797 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
800 .id
= rk3368_code_table
,
801 .length
= ARRAY_SIZE(rk3368_code_table
),
802 .data_mask
= TSADCV3_DATA_MASK
,
803 .mode
= ADC_INCREMENT
,
807 static const struct rockchip_tsadc_chip rk3399_tsadc_data
= {
808 .chn_id
[SENSOR_CPU
] = 0, /* cpu sensor is channel 0 */
809 .chn_id
[SENSOR_GPU
] = 1, /* gpu sensor is channel 1 */
810 .chn_num
= 2, /* two channels for tsadc */
812 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
813 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
816 .initialize
= rk_tsadcv3_initialize
,
817 .irq_ack
= rk_tsadcv3_irq_ack
,
818 .control
= rk_tsadcv3_control
,
819 .get_temp
= rk_tsadcv2_get_temp
,
820 .set_alarm_temp
= rk_tsadcv2_alarm_temp
,
821 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
822 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
825 .id
= rk3399_code_table
,
826 .length
= ARRAY_SIZE(rk3399_code_table
),
827 .data_mask
= TSADCV3_DATA_MASK
,
828 .mode
= ADC_INCREMENT
,
832 static const struct of_device_id of_rockchip_thermal_match
[] = {
834 .compatible
= "rockchip,rk3228-tsadc",
835 .data
= (void *)&rk3228_tsadc_data
,
838 .compatible
= "rockchip,rk3288-tsadc",
839 .data
= (void *)&rk3288_tsadc_data
,
842 .compatible
= "rockchip,rk3366-tsadc",
843 .data
= (void *)&rk3366_tsadc_data
,
846 .compatible
= "rockchip,rk3368-tsadc",
847 .data
= (void *)&rk3368_tsadc_data
,
850 .compatible
= "rockchip,rk3399-tsadc",
851 .data
= (void *)&rk3399_tsadc_data
,
855 MODULE_DEVICE_TABLE(of
, of_rockchip_thermal_match
);
858 rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor
*sensor
, bool on
)
860 struct thermal_zone_device
*tzd
= sensor
->tzd
;
862 tzd
->ops
->set_mode(tzd
,
863 on
? THERMAL_DEVICE_ENABLED
: THERMAL_DEVICE_DISABLED
);
866 static irqreturn_t
rockchip_thermal_alarm_irq_thread(int irq
, void *dev
)
868 struct rockchip_thermal_data
*thermal
= dev
;
871 dev_dbg(&thermal
->pdev
->dev
, "thermal alarm\n");
873 thermal
->chip
->irq_ack(thermal
->regs
);
875 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++)
876 thermal_zone_device_update(thermal
->sensors
[i
].tzd
,
877 THERMAL_EVENT_UNSPECIFIED
);
882 static int rockchip_thermal_set_trips(void *_sensor
, int low
, int high
)
884 struct rockchip_thermal_sensor
*sensor
= _sensor
;
885 struct rockchip_thermal_data
*thermal
= sensor
->thermal
;
886 const struct rockchip_tsadc_chip
*tsadc
= thermal
->chip
;
888 dev_dbg(&thermal
->pdev
->dev
, "%s: sensor %d: low: %d, high %d\n",
889 __func__
, sensor
->id
, low
, high
);
891 tsadc
->set_alarm_temp(tsadc
->table
,
892 sensor
->id
, thermal
->regs
, high
);
897 static int rockchip_thermal_get_temp(void *_sensor
, int *out_temp
)
899 struct rockchip_thermal_sensor
*sensor
= _sensor
;
900 struct rockchip_thermal_data
*thermal
= sensor
->thermal
;
901 const struct rockchip_tsadc_chip
*tsadc
= sensor
->thermal
->chip
;
904 retval
= tsadc
->get_temp(tsadc
->table
,
905 sensor
->id
, thermal
->regs
, out_temp
);
906 dev_dbg(&thermal
->pdev
->dev
, "sensor %d - temp: %d, retval: %d\n",
907 sensor
->id
, *out_temp
, retval
);
912 static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops
= {
913 .get_temp
= rockchip_thermal_get_temp
,
914 .set_trips
= rockchip_thermal_set_trips
,
917 static int rockchip_configure_from_dt(struct device
*dev
,
918 struct device_node
*np
,
919 struct rockchip_thermal_data
*thermal
)
921 u32 shut_temp
, tshut_mode
, tshut_polarity
;
923 if (of_property_read_u32(np
, "rockchip,hw-tshut-temp", &shut_temp
)) {
925 "Missing tshut temp property, using default %d\n",
926 thermal
->chip
->tshut_temp
);
927 thermal
->tshut_temp
= thermal
->chip
->tshut_temp
;
929 if (shut_temp
> INT_MAX
) {
930 dev_err(dev
, "Invalid tshut temperature specified: %d\n",
934 thermal
->tshut_temp
= shut_temp
;
937 if (of_property_read_u32(np
, "rockchip,hw-tshut-mode", &tshut_mode
)) {
939 "Missing tshut mode property, using default (%s)\n",
940 thermal
->chip
->tshut_mode
== TSHUT_MODE_GPIO
?
942 thermal
->tshut_mode
= thermal
->chip
->tshut_mode
;
944 thermal
->tshut_mode
= tshut_mode
;
947 if (thermal
->tshut_mode
> 1) {
948 dev_err(dev
, "Invalid tshut mode specified: %d\n",
949 thermal
->tshut_mode
);
953 if (of_property_read_u32(np
, "rockchip,hw-tshut-polarity",
956 "Missing tshut-polarity property, using default (%s)\n",
957 thermal
->chip
->tshut_polarity
== TSHUT_LOW_ACTIVE
?
959 thermal
->tshut_polarity
= thermal
->chip
->tshut_polarity
;
961 thermal
->tshut_polarity
= tshut_polarity
;
964 if (thermal
->tshut_polarity
> 1) {
965 dev_err(dev
, "Invalid tshut-polarity specified: %d\n",
966 thermal
->tshut_polarity
);
970 /* The tsadc wont to handle the error in here since some SoCs didn't
971 * need this property.
973 thermal
->grf
= syscon_regmap_lookup_by_phandle(np
, "rockchip,grf");
979 rockchip_thermal_register_sensor(struct platform_device
*pdev
,
980 struct rockchip_thermal_data
*thermal
,
981 struct rockchip_thermal_sensor
*sensor
,
984 const struct rockchip_tsadc_chip
*tsadc
= thermal
->chip
;
987 tsadc
->set_tshut_mode(id
, thermal
->regs
, thermal
->tshut_mode
);
988 tsadc
->set_tshut_temp(tsadc
->table
, id
, thermal
->regs
,
989 thermal
->tshut_temp
);
991 sensor
->thermal
= thermal
;
993 sensor
->tzd
= devm_thermal_zone_of_sensor_register(&pdev
->dev
, id
,
994 sensor
, &rockchip_of_thermal_ops
);
995 if (IS_ERR(sensor
->tzd
)) {
996 error
= PTR_ERR(sensor
->tzd
);
997 dev_err(&pdev
->dev
, "failed to register sensor %d: %d\n",
1006 * Reset TSADC Controller, reset all tsadc registers.
1008 static void rockchip_thermal_reset_controller(struct reset_control
*reset
)
1010 reset_control_assert(reset
);
1011 usleep_range(10, 20);
1012 reset_control_deassert(reset
);
1015 static int rockchip_thermal_probe(struct platform_device
*pdev
)
1017 struct device_node
*np
= pdev
->dev
.of_node
;
1018 struct rockchip_thermal_data
*thermal
;
1019 const struct of_device_id
*match
;
1020 struct resource
*res
;
1025 match
= of_match_node(of_rockchip_thermal_match
, np
);
1029 irq
= platform_get_irq(pdev
, 0);
1031 dev_err(&pdev
->dev
, "no irq resource?\n");
1035 thermal
= devm_kzalloc(&pdev
->dev
, sizeof(struct rockchip_thermal_data
),
1040 thermal
->pdev
= pdev
;
1042 thermal
->chip
= (const struct rockchip_tsadc_chip
*)match
->data
;
1046 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1047 thermal
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
1048 if (IS_ERR(thermal
->regs
))
1049 return PTR_ERR(thermal
->regs
);
1051 thermal
->reset
= devm_reset_control_get(&pdev
->dev
, "tsadc-apb");
1052 if (IS_ERR(thermal
->reset
)) {
1053 error
= PTR_ERR(thermal
->reset
);
1054 dev_err(&pdev
->dev
, "failed to get tsadc reset: %d\n", error
);
1058 thermal
->clk
= devm_clk_get(&pdev
->dev
, "tsadc");
1059 if (IS_ERR(thermal
->clk
)) {
1060 error
= PTR_ERR(thermal
->clk
);
1061 dev_err(&pdev
->dev
, "failed to get tsadc clock: %d\n", error
);
1065 thermal
->pclk
= devm_clk_get(&pdev
->dev
, "apb_pclk");
1066 if (IS_ERR(thermal
->pclk
)) {
1067 error
= PTR_ERR(thermal
->pclk
);
1068 dev_err(&pdev
->dev
, "failed to get apb_pclk clock: %d\n",
1073 error
= clk_prepare_enable(thermal
->clk
);
1075 dev_err(&pdev
->dev
, "failed to enable converter clock: %d\n",
1080 error
= clk_prepare_enable(thermal
->pclk
);
1082 dev_err(&pdev
->dev
, "failed to enable pclk: %d\n", error
);
1083 goto err_disable_clk
;
1086 rockchip_thermal_reset_controller(thermal
->reset
);
1088 error
= rockchip_configure_from_dt(&pdev
->dev
, np
, thermal
);
1090 dev_err(&pdev
->dev
, "failed to parse device tree data: %d\n",
1092 goto err_disable_pclk
;
1095 thermal
->chip
->initialize(thermal
->grf
, thermal
->regs
,
1096 thermal
->tshut_polarity
);
1098 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++) {
1099 error
= rockchip_thermal_register_sensor(pdev
, thermal
,
1100 &thermal
->sensors
[i
],
1101 thermal
->chip
->chn_id
[i
]);
1104 "failed to register sensor[%d] : error = %d\n",
1106 goto err_disable_pclk
;
1110 error
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
1111 &rockchip_thermal_alarm_irq_thread
,
1113 "rockchip_thermal", thermal
);
1116 "failed to request tsadc irq: %d\n", error
);
1117 goto err_disable_pclk
;
1120 thermal
->chip
->control(thermal
->regs
, true);
1122 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++)
1123 rockchip_thermal_toggle_sensor(&thermal
->sensors
[i
], true);
1125 platform_set_drvdata(pdev
, thermal
);
1130 clk_disable_unprepare(thermal
->pclk
);
1132 clk_disable_unprepare(thermal
->clk
);
1137 static int rockchip_thermal_remove(struct platform_device
*pdev
)
1139 struct rockchip_thermal_data
*thermal
= platform_get_drvdata(pdev
);
1142 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++) {
1143 struct rockchip_thermal_sensor
*sensor
= &thermal
->sensors
[i
];
1145 rockchip_thermal_toggle_sensor(sensor
, false);
1148 thermal
->chip
->control(thermal
->regs
, false);
1150 clk_disable_unprepare(thermal
->pclk
);
1151 clk_disable_unprepare(thermal
->clk
);
1156 static int __maybe_unused
rockchip_thermal_suspend(struct device
*dev
)
1158 struct platform_device
*pdev
= to_platform_device(dev
);
1159 struct rockchip_thermal_data
*thermal
= platform_get_drvdata(pdev
);
1162 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++)
1163 rockchip_thermal_toggle_sensor(&thermal
->sensors
[i
], false);
1165 thermal
->chip
->control(thermal
->regs
, false);
1167 clk_disable(thermal
->pclk
);
1168 clk_disable(thermal
->clk
);
1170 pinctrl_pm_select_sleep_state(dev
);
1175 static int __maybe_unused
rockchip_thermal_resume(struct device
*dev
)
1177 struct platform_device
*pdev
= to_platform_device(dev
);
1178 struct rockchip_thermal_data
*thermal
= platform_get_drvdata(pdev
);
1182 error
= clk_enable(thermal
->clk
);
1186 error
= clk_enable(thermal
->pclk
);
1188 clk_disable(thermal
->clk
);
1192 rockchip_thermal_reset_controller(thermal
->reset
);
1194 thermal
->chip
->initialize(thermal
->grf
, thermal
->regs
,
1195 thermal
->tshut_polarity
);
1197 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++) {
1198 int id
= thermal
->sensors
[i
].id
;
1200 thermal
->chip
->set_tshut_mode(id
, thermal
->regs
,
1201 thermal
->tshut_mode
);
1202 thermal
->chip
->set_tshut_temp(thermal
->chip
->table
,
1204 thermal
->tshut_temp
);
1207 thermal
->chip
->control(thermal
->regs
, true);
1209 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++)
1210 rockchip_thermal_toggle_sensor(&thermal
->sensors
[i
], true);
1212 pinctrl_pm_select_default_state(dev
);
1217 static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops
,
1218 rockchip_thermal_suspend
, rockchip_thermal_resume
);
1220 static struct platform_driver rockchip_thermal_driver
= {
1222 .name
= "rockchip-thermal",
1223 .pm
= &rockchip_thermal_pm_ops
,
1224 .of_match_table
= of_rockchip_thermal_match
,
1226 .probe
= rockchip_thermal_probe
,
1227 .remove
= rockchip_thermal_remove
,
1230 module_platform_driver(rockchip_thermal_driver
);
1232 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
1233 MODULE_AUTHOR("Rockchip, Inc.");
1234 MODULE_LICENSE("GPL v2");
1235 MODULE_ALIAS("platform:rockchip-thermal");