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
)(const struct chip_tsadc_table
*table
,
122 int chn
, void __iomem
*reg
, int *temp
);
123 int (*set_alarm_temp
)(const struct chip_tsadc_table
*table
,
124 int chn
, void __iomem
*reg
, int temp
);
125 int (*set_tshut_temp
)(const 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 rv1108_table
[] = {
281 {TSADCV2_DATA_MASK
, 125000},
284 static const struct tsadc_table rk3228_code_table
[] = {
320 {TSADCV2_DATA_MASK
, 125000},
323 static const struct tsadc_table rk3288_code_table
[] = {
324 {TSADCV2_DATA_MASK
, -40000},
362 static const struct tsadc_table rk3328_code_table
[] = {
397 {TSADCV2_DATA_MASK
, 125000},
400 static const struct tsadc_table rk3368_code_table
[] = {
436 {TSADCV3_DATA_MASK
, 125000},
439 static const struct tsadc_table rk3399_code_table
[] = {
475 {TSADCV3_DATA_MASK
, 125000},
478 static u32
rk_tsadcv2_temp_to_code(const struct chip_tsadc_table
*table
,
484 u32 error
= table
->data_mask
;
487 high
= (table
->length
- 1) - 1; /* ignore the last check for table */
488 mid
= (high
+ low
) / 2;
490 /* Return mask code data when the temp is over table range */
491 if (temp
< table
->id
[low
].temp
|| temp
> table
->id
[high
].temp
)
494 while (low
<= high
) {
495 if (temp
== table
->id
[mid
].temp
)
496 return table
->id
[mid
].code
;
497 else if (temp
< table
->id
[mid
].temp
)
501 mid
= (low
+ high
) / 2;
505 * The conversion code granularity provided by the table. Let's
506 * assume that the relationship between temperature and
507 * analog value between 2 table entries is linear and interpolate
508 * to produce less granular result.
510 num
= abs(table
->id
[mid
+ 1].code
- table
->id
[mid
].code
);
511 num
*= temp
- table
->id
[mid
].temp
;
512 denom
= table
->id
[mid
+ 1].temp
- table
->id
[mid
].temp
;
514 switch (table
->mode
) {
516 return table
->id
[mid
].code
- (num
/ denom
);
518 return table
->id
[mid
].code
+ (num
/ denom
);
520 pr_err("%s: unknown table mode: %d\n", __func__
, table
->mode
);
525 pr_err("%s: invalid temperature, temp=%d error=%d\n",
526 __func__
, temp
, error
);
530 static int rk_tsadcv2_code_to_temp(const struct chip_tsadc_table
*table
,
533 unsigned int low
= 1;
534 unsigned int high
= table
->length
- 1;
535 unsigned int mid
= (low
+ high
) / 2;
539 WARN_ON(table
->length
< 2);
541 switch (table
->mode
) {
543 code
&= table
->data_mask
;
544 if (code
<= table
->id
[high
].code
)
545 return -EAGAIN
; /* Incorrect reading */
547 while (low
<= high
) {
548 if (code
>= table
->id
[mid
].code
&&
549 code
< table
->id
[mid
- 1].code
)
551 else if (code
< table
->id
[mid
].code
)
556 mid
= (low
+ high
) / 2;
560 code
&= table
->data_mask
;
561 if (code
< table
->id
[low
].code
)
562 return -EAGAIN
; /* Incorrect reading */
564 while (low
<= high
) {
565 if (code
<= table
->id
[mid
].code
&&
566 code
> table
->id
[mid
- 1].code
)
568 else if (code
> table
->id
[mid
].code
)
573 mid
= (low
+ high
) / 2;
577 pr_err("%s: unknown table mode: %d\n", __func__
, table
->mode
);
582 * The 5C granularity provided by the table is too much. Let's
583 * assume that the relationship between sensor readings and
584 * temperature between 2 table entries is linear and interpolate
585 * to produce less granular result.
587 num
= table
->id
[mid
].temp
- table
->id
[mid
- 1].temp
;
588 num
*= abs(table
->id
[mid
- 1].code
- code
);
589 denom
= abs(table
->id
[mid
- 1].code
- table
->id
[mid
].code
);
590 *temp
= table
->id
[mid
- 1].temp
+ (num
/ denom
);
596 * rk_tsadcv2_initialize - initialize TASDC Controller.
598 * (1) Set TSADC_V2_AUTO_PERIOD:
599 * Configure the interleave between every two accessing of
600 * TSADC in normal operation.
602 * (2) Set TSADCV2_AUTO_PERIOD_HT:
603 * Configure the interleave between every two accessing of
604 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
606 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
607 * If the temperature is higher than COMP_INT or COMP_SHUT for
608 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
610 static void rk_tsadcv2_initialize(struct regmap
*grf
, void __iomem
*regs
,
611 enum tshut_polarity tshut_polarity
)
613 if (tshut_polarity
== TSHUT_HIGH_ACTIVE
)
614 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH
,
615 regs
+ TSADCV2_AUTO_CON
);
617 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH
,
618 regs
+ TSADCV2_AUTO_CON
);
620 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME
, regs
+ TSADCV2_AUTO_PERIOD
);
621 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT
,
622 regs
+ TSADCV2_HIGHT_INT_DEBOUNCE
);
623 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME
,
624 regs
+ TSADCV2_AUTO_PERIOD_HT
);
625 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT
,
626 regs
+ TSADCV2_HIGHT_TSHUT_DEBOUNCE
);
630 * rk_tsadcv3_initialize - initialize TASDC Controller.
632 * (1) The tsadc control power sequence.
634 * (2) Set TSADC_V2_AUTO_PERIOD:
635 * Configure the interleave between every two accessing of
636 * TSADC in normal operation.
638 * (2) Set TSADCV2_AUTO_PERIOD_HT:
639 * Configure the interleave between every two accessing of
640 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
642 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
643 * If the temperature is higher than COMP_INT or COMP_SHUT for
644 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
646 static void rk_tsadcv3_initialize(struct regmap
*grf
, void __iomem
*regs
,
647 enum tshut_polarity tshut_polarity
)
649 /* The tsadc control power sequence */
651 /* Set interleave value to workround ic time sync issue */
652 writel_relaxed(TSADCV2_USER_INTER_PD_SOC
, regs
+
655 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME
,
656 regs
+ TSADCV2_AUTO_PERIOD
);
657 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT
,
658 regs
+ TSADCV2_HIGHT_INT_DEBOUNCE
);
659 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME
,
660 regs
+ TSADCV2_AUTO_PERIOD_HT
);
661 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT
,
662 regs
+ TSADCV2_HIGHT_TSHUT_DEBOUNCE
);
665 /* Enable the voltage common mode feature */
666 regmap_write(grf
, GRF_TSADC_TESTBIT_L
, GRF_TSADC_VCM_EN_L
);
667 regmap_write(grf
, GRF_TSADC_TESTBIT_H
, GRF_TSADC_VCM_EN_H
);
669 usleep_range(15, 100); /* The spec note says at least 15 us */
670 regmap_write(grf
, GRF_SARADC_TESTBIT
, GRF_SARADC_TESTBIT_ON
);
671 regmap_write(grf
, GRF_TSADC_TESTBIT_H
, GRF_TSADC_TESTBIT_H_ON
);
672 usleep_range(90, 200); /* The spec note says at least 90 us */
674 writel_relaxed(TSADCV3_AUTO_PERIOD_TIME
,
675 regs
+ TSADCV2_AUTO_PERIOD
);
676 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT
,
677 regs
+ TSADCV2_HIGHT_INT_DEBOUNCE
);
678 writel_relaxed(TSADCV3_AUTO_PERIOD_HT_TIME
,
679 regs
+ TSADCV2_AUTO_PERIOD_HT
);
680 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT
,
681 regs
+ TSADCV2_HIGHT_TSHUT_DEBOUNCE
);
684 if (tshut_polarity
== TSHUT_HIGH_ACTIVE
)
685 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH
,
686 regs
+ TSADCV2_AUTO_CON
);
688 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH
,
689 regs
+ TSADCV2_AUTO_CON
);
692 static void rk_tsadcv2_irq_ack(void __iomem
*regs
)
696 val
= readl_relaxed(regs
+ TSADCV2_INT_PD
);
697 writel_relaxed(val
& TSADCV2_INT_PD_CLEAR_MASK
, regs
+ TSADCV2_INT_PD
);
700 static void rk_tsadcv3_irq_ack(void __iomem
*regs
)
704 val
= readl_relaxed(regs
+ TSADCV2_INT_PD
);
705 writel_relaxed(val
& TSADCV3_INT_PD_CLEAR_MASK
, regs
+ TSADCV2_INT_PD
);
708 static void rk_tsadcv2_control(void __iomem
*regs
, bool enable
)
712 val
= readl_relaxed(regs
+ TSADCV2_AUTO_CON
);
714 val
|= TSADCV2_AUTO_EN
;
716 val
&= ~TSADCV2_AUTO_EN
;
718 writel_relaxed(val
, regs
+ TSADCV2_AUTO_CON
);
722 * rk_tsadcv3_control - the tsadc controller is enabled or disabled.
724 * NOTE: TSADC controller works at auto mode, and some SoCs need set the
725 * tsadc_q_sel bit on TSADCV2_AUTO_CON[1]. The (1024 - tsadc_q) as output
726 * adc value if setting this bit to enable.
728 static void rk_tsadcv3_control(void __iomem
*regs
, bool enable
)
732 val
= readl_relaxed(regs
+ TSADCV2_AUTO_CON
);
734 val
|= TSADCV2_AUTO_EN
| TSADCV3_AUTO_Q_SEL_EN
;
736 val
&= ~TSADCV2_AUTO_EN
;
738 writel_relaxed(val
, regs
+ TSADCV2_AUTO_CON
);
741 static int rk_tsadcv2_get_temp(const struct chip_tsadc_table
*table
,
742 int chn
, void __iomem
*regs
, int *temp
)
746 val
= readl_relaxed(regs
+ TSADCV2_DATA(chn
));
748 return rk_tsadcv2_code_to_temp(table
, val
, temp
);
751 static int rk_tsadcv2_alarm_temp(const struct chip_tsadc_table
*table
,
752 int chn
, void __iomem
*regs
, int temp
)
758 * In some cases, some sensors didn't need the trip points, the
759 * set_trips will pass {-INT_MAX, INT_MAX} to trigger tsadc alarm
760 * in the end, ignore this case and disable the high temperature
763 if (temp
== INT_MAX
) {
764 int_clr
= readl_relaxed(regs
+ TSADCV2_INT_EN
);
765 int_clr
&= ~TSADCV2_INT_SRC_EN(chn
);
766 writel_relaxed(int_clr
, regs
+ TSADCV2_INT_EN
);
770 /* Make sure the value is valid */
771 alarm_value
= rk_tsadcv2_temp_to_code(table
, temp
);
772 if (alarm_value
== table
->data_mask
)
775 writel_relaxed(alarm_value
& table
->data_mask
,
776 regs
+ TSADCV2_COMP_INT(chn
));
778 int_en
= readl_relaxed(regs
+ TSADCV2_INT_EN
);
779 int_en
|= TSADCV2_INT_SRC_EN(chn
);
780 writel_relaxed(int_en
, regs
+ TSADCV2_INT_EN
);
785 static int rk_tsadcv2_tshut_temp(const struct chip_tsadc_table
*table
,
786 int chn
, void __iomem
*regs
, int temp
)
788 u32 tshut_value
, val
;
790 /* Make sure the value is valid */
791 tshut_value
= rk_tsadcv2_temp_to_code(table
, temp
);
792 if (tshut_value
== table
->data_mask
)
795 writel_relaxed(tshut_value
, regs
+ TSADCV2_COMP_SHUT(chn
));
797 /* TSHUT will be valid */
798 val
= readl_relaxed(regs
+ TSADCV2_AUTO_CON
);
799 writel_relaxed(val
| TSADCV2_AUTO_SRC_EN(chn
), regs
+ TSADCV2_AUTO_CON
);
804 static void rk_tsadcv2_tshut_mode(int chn
, void __iomem
*regs
,
805 enum tshut_mode mode
)
809 val
= readl_relaxed(regs
+ TSADCV2_INT_EN
);
810 if (mode
== TSHUT_MODE_GPIO
) {
811 val
&= ~TSADCV2_SHUT_2CRU_SRC_EN(chn
);
812 val
|= TSADCV2_SHUT_2GPIO_SRC_EN(chn
);
814 val
&= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn
);
815 val
|= TSADCV2_SHUT_2CRU_SRC_EN(chn
);
818 writel_relaxed(val
, regs
+ TSADCV2_INT_EN
);
821 static const struct rockchip_tsadc_chip rv1108_tsadc_data
= {
822 .chn_id
[SENSOR_CPU
] = 0, /* cpu sensor is channel 0 */
823 .chn_num
= 1, /* one channel for tsadc */
825 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
826 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
829 .initialize
= rk_tsadcv2_initialize
,
830 .irq_ack
= rk_tsadcv3_irq_ack
,
831 .control
= rk_tsadcv3_control
,
832 .get_temp
= rk_tsadcv2_get_temp
,
833 .set_alarm_temp
= rk_tsadcv2_alarm_temp
,
834 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
835 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
839 .length
= ARRAY_SIZE(rv1108_table
),
840 .data_mask
= TSADCV2_DATA_MASK
,
841 .mode
= ADC_INCREMENT
,
845 static const struct rockchip_tsadc_chip rk3228_tsadc_data
= {
846 .chn_id
[SENSOR_CPU
] = 0, /* cpu sensor is channel 0 */
847 .chn_num
= 1, /* one channel for tsadc */
849 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
850 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
853 .initialize
= rk_tsadcv2_initialize
,
854 .irq_ack
= rk_tsadcv3_irq_ack
,
855 .control
= rk_tsadcv3_control
,
856 .get_temp
= rk_tsadcv2_get_temp
,
857 .set_alarm_temp
= rk_tsadcv2_alarm_temp
,
858 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
859 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
862 .id
= rk3228_code_table
,
863 .length
= ARRAY_SIZE(rk3228_code_table
),
864 .data_mask
= TSADCV3_DATA_MASK
,
865 .mode
= ADC_INCREMENT
,
869 static const struct rockchip_tsadc_chip rk3288_tsadc_data
= {
870 .chn_id
[SENSOR_CPU
] = 1, /* cpu sensor is channel 1 */
871 .chn_id
[SENSOR_GPU
] = 2, /* gpu sensor is channel 2 */
872 .chn_num
= 2, /* two channels for tsadc */
874 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
875 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
878 .initialize
= rk_tsadcv2_initialize
,
879 .irq_ack
= rk_tsadcv2_irq_ack
,
880 .control
= rk_tsadcv2_control
,
881 .get_temp
= rk_tsadcv2_get_temp
,
882 .set_alarm_temp
= rk_tsadcv2_alarm_temp
,
883 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
884 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
887 .id
= rk3288_code_table
,
888 .length
= ARRAY_SIZE(rk3288_code_table
),
889 .data_mask
= TSADCV2_DATA_MASK
,
890 .mode
= ADC_DECREMENT
,
894 static const struct rockchip_tsadc_chip rk3328_tsadc_data
= {
895 .chn_id
[SENSOR_CPU
] = 0, /* cpu sensor is channel 0 */
896 .chn_num
= 1, /* one channels for tsadc */
898 .tshut_mode
= TSHUT_MODE_CRU
, /* default TSHUT via CRU */
901 .initialize
= rk_tsadcv2_initialize
,
902 .irq_ack
= rk_tsadcv3_irq_ack
,
903 .control
= rk_tsadcv3_control
,
904 .get_temp
= rk_tsadcv2_get_temp
,
905 .set_alarm_temp
= rk_tsadcv2_alarm_temp
,
906 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
907 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
910 .id
= rk3328_code_table
,
911 .length
= ARRAY_SIZE(rk3328_code_table
),
912 .data_mask
= TSADCV2_DATA_MASK
,
913 .mode
= ADC_INCREMENT
,
917 static const struct rockchip_tsadc_chip rk3366_tsadc_data
= {
918 .chn_id
[SENSOR_CPU
] = 0, /* cpu sensor is channel 0 */
919 .chn_id
[SENSOR_GPU
] = 1, /* gpu sensor is channel 1 */
920 .chn_num
= 2, /* two channels for tsadc */
922 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
923 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
926 .initialize
= rk_tsadcv3_initialize
,
927 .irq_ack
= rk_tsadcv3_irq_ack
,
928 .control
= rk_tsadcv3_control
,
929 .get_temp
= rk_tsadcv2_get_temp
,
930 .set_alarm_temp
= rk_tsadcv2_alarm_temp
,
931 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
932 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
935 .id
= rk3228_code_table
,
936 .length
= ARRAY_SIZE(rk3228_code_table
),
937 .data_mask
= TSADCV3_DATA_MASK
,
938 .mode
= ADC_INCREMENT
,
942 static const struct rockchip_tsadc_chip rk3368_tsadc_data
= {
943 .chn_id
[SENSOR_CPU
] = 0, /* cpu sensor is channel 0 */
944 .chn_id
[SENSOR_GPU
] = 1, /* gpu sensor is channel 1 */
945 .chn_num
= 2, /* two channels for tsadc */
947 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
948 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
951 .initialize
= rk_tsadcv2_initialize
,
952 .irq_ack
= rk_tsadcv2_irq_ack
,
953 .control
= rk_tsadcv2_control
,
954 .get_temp
= rk_tsadcv2_get_temp
,
955 .set_alarm_temp
= rk_tsadcv2_alarm_temp
,
956 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
957 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
960 .id
= rk3368_code_table
,
961 .length
= ARRAY_SIZE(rk3368_code_table
),
962 .data_mask
= TSADCV3_DATA_MASK
,
963 .mode
= ADC_INCREMENT
,
967 static const struct rockchip_tsadc_chip rk3399_tsadc_data
= {
968 .chn_id
[SENSOR_CPU
] = 0, /* cpu sensor is channel 0 */
969 .chn_id
[SENSOR_GPU
] = 1, /* gpu sensor is channel 1 */
970 .chn_num
= 2, /* two channels for tsadc */
972 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
973 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
976 .initialize
= rk_tsadcv3_initialize
,
977 .irq_ack
= rk_tsadcv3_irq_ack
,
978 .control
= rk_tsadcv3_control
,
979 .get_temp
= rk_tsadcv2_get_temp
,
980 .set_alarm_temp
= rk_tsadcv2_alarm_temp
,
981 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
982 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
985 .id
= rk3399_code_table
,
986 .length
= ARRAY_SIZE(rk3399_code_table
),
987 .data_mask
= TSADCV3_DATA_MASK
,
988 .mode
= ADC_INCREMENT
,
992 static const struct of_device_id of_rockchip_thermal_match
[] = {
994 .compatible
= "rockchip,rv1108-tsadc",
995 .data
= (void *)&rv1108_tsadc_data
,
998 .compatible
= "rockchip,rk3228-tsadc",
999 .data
= (void *)&rk3228_tsadc_data
,
1002 .compatible
= "rockchip,rk3288-tsadc",
1003 .data
= (void *)&rk3288_tsadc_data
,
1006 .compatible
= "rockchip,rk3328-tsadc",
1007 .data
= (void *)&rk3328_tsadc_data
,
1010 .compatible
= "rockchip,rk3366-tsadc",
1011 .data
= (void *)&rk3366_tsadc_data
,
1014 .compatible
= "rockchip,rk3368-tsadc",
1015 .data
= (void *)&rk3368_tsadc_data
,
1018 .compatible
= "rockchip,rk3399-tsadc",
1019 .data
= (void *)&rk3399_tsadc_data
,
1023 MODULE_DEVICE_TABLE(of
, of_rockchip_thermal_match
);
1026 rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor
*sensor
, bool on
)
1028 struct thermal_zone_device
*tzd
= sensor
->tzd
;
1030 tzd
->ops
->set_mode(tzd
,
1031 on
? THERMAL_DEVICE_ENABLED
: THERMAL_DEVICE_DISABLED
);
1034 static irqreturn_t
rockchip_thermal_alarm_irq_thread(int irq
, void *dev
)
1036 struct rockchip_thermal_data
*thermal
= dev
;
1039 dev_dbg(&thermal
->pdev
->dev
, "thermal alarm\n");
1041 thermal
->chip
->irq_ack(thermal
->regs
);
1043 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++)
1044 thermal_zone_device_update(thermal
->sensors
[i
].tzd
,
1045 THERMAL_EVENT_UNSPECIFIED
);
1050 static int rockchip_thermal_set_trips(void *_sensor
, int low
, int high
)
1052 struct rockchip_thermal_sensor
*sensor
= _sensor
;
1053 struct rockchip_thermal_data
*thermal
= sensor
->thermal
;
1054 const struct rockchip_tsadc_chip
*tsadc
= thermal
->chip
;
1056 dev_dbg(&thermal
->pdev
->dev
, "%s: sensor %d: low: %d, high %d\n",
1057 __func__
, sensor
->id
, low
, high
);
1059 return tsadc
->set_alarm_temp(&tsadc
->table
,
1060 sensor
->id
, thermal
->regs
, high
);
1063 static int rockchip_thermal_get_temp(void *_sensor
, int *out_temp
)
1065 struct rockchip_thermal_sensor
*sensor
= _sensor
;
1066 struct rockchip_thermal_data
*thermal
= sensor
->thermal
;
1067 const struct rockchip_tsadc_chip
*tsadc
= sensor
->thermal
->chip
;
1070 retval
= tsadc
->get_temp(&tsadc
->table
,
1071 sensor
->id
, thermal
->regs
, out_temp
);
1072 dev_dbg(&thermal
->pdev
->dev
, "sensor %d - temp: %d, retval: %d\n",
1073 sensor
->id
, *out_temp
, retval
);
1078 static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops
= {
1079 .get_temp
= rockchip_thermal_get_temp
,
1080 .set_trips
= rockchip_thermal_set_trips
,
1083 static int rockchip_configure_from_dt(struct device
*dev
,
1084 struct device_node
*np
,
1085 struct rockchip_thermal_data
*thermal
)
1087 u32 shut_temp
, tshut_mode
, tshut_polarity
;
1089 if (of_property_read_u32(np
, "rockchip,hw-tshut-temp", &shut_temp
)) {
1091 "Missing tshut temp property, using default %d\n",
1092 thermal
->chip
->tshut_temp
);
1093 thermal
->tshut_temp
= thermal
->chip
->tshut_temp
;
1095 if (shut_temp
> INT_MAX
) {
1096 dev_err(dev
, "Invalid tshut temperature specified: %d\n",
1100 thermal
->tshut_temp
= shut_temp
;
1103 if (of_property_read_u32(np
, "rockchip,hw-tshut-mode", &tshut_mode
)) {
1105 "Missing tshut mode property, using default (%s)\n",
1106 thermal
->chip
->tshut_mode
== TSHUT_MODE_GPIO
?
1108 thermal
->tshut_mode
= thermal
->chip
->tshut_mode
;
1110 thermal
->tshut_mode
= tshut_mode
;
1113 if (thermal
->tshut_mode
> 1) {
1114 dev_err(dev
, "Invalid tshut mode specified: %d\n",
1115 thermal
->tshut_mode
);
1119 if (of_property_read_u32(np
, "rockchip,hw-tshut-polarity",
1122 "Missing tshut-polarity property, using default (%s)\n",
1123 thermal
->chip
->tshut_polarity
== TSHUT_LOW_ACTIVE
?
1125 thermal
->tshut_polarity
= thermal
->chip
->tshut_polarity
;
1127 thermal
->tshut_polarity
= tshut_polarity
;
1130 if (thermal
->tshut_polarity
> 1) {
1131 dev_err(dev
, "Invalid tshut-polarity specified: %d\n",
1132 thermal
->tshut_polarity
);
1136 /* The tsadc wont to handle the error in here since some SoCs didn't
1137 * need this property.
1139 thermal
->grf
= syscon_regmap_lookup_by_phandle(np
, "rockchip,grf");
1140 if (IS_ERR(thermal
->grf
))
1141 dev_warn(dev
, "Missing rockchip,grf property\n");
1147 rockchip_thermal_register_sensor(struct platform_device
*pdev
,
1148 struct rockchip_thermal_data
*thermal
,
1149 struct rockchip_thermal_sensor
*sensor
,
1152 const struct rockchip_tsadc_chip
*tsadc
= thermal
->chip
;
1155 tsadc
->set_tshut_mode(id
, thermal
->regs
, thermal
->tshut_mode
);
1157 error
= tsadc
->set_tshut_temp(&tsadc
->table
, id
, thermal
->regs
,
1158 thermal
->tshut_temp
);
1160 dev_err(&pdev
->dev
, "%s: invalid tshut=%d, error=%d\n",
1161 __func__
, thermal
->tshut_temp
, error
);
1163 sensor
->thermal
= thermal
;
1165 sensor
->tzd
= devm_thermal_zone_of_sensor_register(&pdev
->dev
, id
,
1166 sensor
, &rockchip_of_thermal_ops
);
1167 if (IS_ERR(sensor
->tzd
)) {
1168 error
= PTR_ERR(sensor
->tzd
);
1169 dev_err(&pdev
->dev
, "failed to register sensor %d: %d\n",
1178 * Reset TSADC Controller, reset all tsadc registers.
1180 static void rockchip_thermal_reset_controller(struct reset_control
*reset
)
1182 reset_control_assert(reset
);
1183 usleep_range(10, 20);
1184 reset_control_deassert(reset
);
1187 static int rockchip_thermal_probe(struct platform_device
*pdev
)
1189 struct device_node
*np
= pdev
->dev
.of_node
;
1190 struct rockchip_thermal_data
*thermal
;
1191 const struct of_device_id
*match
;
1192 struct resource
*res
;
1197 match
= of_match_node(of_rockchip_thermal_match
, np
);
1201 irq
= platform_get_irq(pdev
, 0);
1203 dev_err(&pdev
->dev
, "no irq resource?\n");
1207 thermal
= devm_kzalloc(&pdev
->dev
, sizeof(struct rockchip_thermal_data
),
1212 thermal
->pdev
= pdev
;
1214 thermal
->chip
= (const struct rockchip_tsadc_chip
*)match
->data
;
1218 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1219 thermal
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
1220 if (IS_ERR(thermal
->regs
))
1221 return PTR_ERR(thermal
->regs
);
1223 thermal
->reset
= devm_reset_control_get(&pdev
->dev
, "tsadc-apb");
1224 if (IS_ERR(thermal
->reset
)) {
1225 error
= PTR_ERR(thermal
->reset
);
1226 dev_err(&pdev
->dev
, "failed to get tsadc reset: %d\n", error
);
1230 thermal
->clk
= devm_clk_get(&pdev
->dev
, "tsadc");
1231 if (IS_ERR(thermal
->clk
)) {
1232 error
= PTR_ERR(thermal
->clk
);
1233 dev_err(&pdev
->dev
, "failed to get tsadc clock: %d\n", error
);
1237 thermal
->pclk
= devm_clk_get(&pdev
->dev
, "apb_pclk");
1238 if (IS_ERR(thermal
->pclk
)) {
1239 error
= PTR_ERR(thermal
->pclk
);
1240 dev_err(&pdev
->dev
, "failed to get apb_pclk clock: %d\n",
1245 error
= clk_prepare_enable(thermal
->clk
);
1247 dev_err(&pdev
->dev
, "failed to enable converter clock: %d\n",
1252 error
= clk_prepare_enable(thermal
->pclk
);
1254 dev_err(&pdev
->dev
, "failed to enable pclk: %d\n", error
);
1255 goto err_disable_clk
;
1258 rockchip_thermal_reset_controller(thermal
->reset
);
1260 error
= rockchip_configure_from_dt(&pdev
->dev
, np
, thermal
);
1262 dev_err(&pdev
->dev
, "failed to parse device tree data: %d\n",
1264 goto err_disable_pclk
;
1267 thermal
->chip
->initialize(thermal
->grf
, thermal
->regs
,
1268 thermal
->tshut_polarity
);
1270 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++) {
1271 error
= rockchip_thermal_register_sensor(pdev
, thermal
,
1272 &thermal
->sensors
[i
],
1273 thermal
->chip
->chn_id
[i
]);
1276 "failed to register sensor[%d] : error = %d\n",
1278 goto err_disable_pclk
;
1282 error
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
1283 &rockchip_thermal_alarm_irq_thread
,
1285 "rockchip_thermal", thermal
);
1288 "failed to request tsadc irq: %d\n", error
);
1289 goto err_disable_pclk
;
1292 thermal
->chip
->control(thermal
->regs
, true);
1294 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++)
1295 rockchip_thermal_toggle_sensor(&thermal
->sensors
[i
], true);
1297 platform_set_drvdata(pdev
, thermal
);
1302 clk_disable_unprepare(thermal
->pclk
);
1304 clk_disable_unprepare(thermal
->clk
);
1309 static int rockchip_thermal_remove(struct platform_device
*pdev
)
1311 struct rockchip_thermal_data
*thermal
= platform_get_drvdata(pdev
);
1314 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++) {
1315 struct rockchip_thermal_sensor
*sensor
= &thermal
->sensors
[i
];
1317 rockchip_thermal_toggle_sensor(sensor
, false);
1320 thermal
->chip
->control(thermal
->regs
, false);
1322 clk_disable_unprepare(thermal
->pclk
);
1323 clk_disable_unprepare(thermal
->clk
);
1328 static int __maybe_unused
rockchip_thermal_suspend(struct device
*dev
)
1330 struct platform_device
*pdev
= to_platform_device(dev
);
1331 struct rockchip_thermal_data
*thermal
= platform_get_drvdata(pdev
);
1334 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++)
1335 rockchip_thermal_toggle_sensor(&thermal
->sensors
[i
], false);
1337 thermal
->chip
->control(thermal
->regs
, false);
1339 clk_disable(thermal
->pclk
);
1340 clk_disable(thermal
->clk
);
1342 pinctrl_pm_select_sleep_state(dev
);
1347 static int __maybe_unused
rockchip_thermal_resume(struct device
*dev
)
1349 struct platform_device
*pdev
= to_platform_device(dev
);
1350 struct rockchip_thermal_data
*thermal
= platform_get_drvdata(pdev
);
1354 error
= clk_enable(thermal
->clk
);
1358 error
= clk_enable(thermal
->pclk
);
1360 clk_disable(thermal
->clk
);
1364 rockchip_thermal_reset_controller(thermal
->reset
);
1366 thermal
->chip
->initialize(thermal
->grf
, thermal
->regs
,
1367 thermal
->tshut_polarity
);
1369 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++) {
1370 int id
= thermal
->sensors
[i
].id
;
1372 thermal
->chip
->set_tshut_mode(id
, thermal
->regs
,
1373 thermal
->tshut_mode
);
1375 error
= thermal
->chip
->set_tshut_temp(&thermal
->chip
->table
,
1377 thermal
->tshut_temp
);
1379 dev_err(&pdev
->dev
, "%s: invalid tshut=%d, error=%d\n",
1380 __func__
, thermal
->tshut_temp
, error
);
1383 thermal
->chip
->control(thermal
->regs
, true);
1385 for (i
= 0; i
< thermal
->chip
->chn_num
; i
++)
1386 rockchip_thermal_toggle_sensor(&thermal
->sensors
[i
], true);
1388 pinctrl_pm_select_default_state(dev
);
1393 static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops
,
1394 rockchip_thermal_suspend
, rockchip_thermal_resume
);
1396 static struct platform_driver rockchip_thermal_driver
= {
1398 .name
= "rockchip-thermal",
1399 .pm
= &rockchip_thermal_pm_ops
,
1400 .of_match_table
= of_rockchip_thermal_match
,
1402 .probe
= rockchip_thermal_probe
,
1403 .remove
= rockchip_thermal_remove
,
1406 module_platform_driver(rockchip_thermal_driver
);
1408 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
1409 MODULE_AUTHOR("Rockchip, Inc.");
1410 MODULE_LICENSE("GPL v2");
1411 MODULE_ALIAS("platform:rockchip-thermal");