1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014-2016, Fuzhou Rockchip Electronics Co., Ltd
4 * Caesar Wang <wxt@rock-chips.com>
8 #include <linux/delay.h>
9 #include <linux/interrupt.h>
11 #include <linux/module.h>
13 #include <linux/of_address.h>
14 #include <linux/of_irq.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17 #include <linux/reset.h>
18 #include <linux/thermal.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/pinctrl/consumer.h>
23 * If the temperature over a period of time High,
24 * the resulting TSHUT gave CRU module,let it reset the entire chip,
25 * or via GPIO give PMIC.
33 * The system Temperature Sensors tshut(tshut) polarity
34 * the bit 8 is tshut polarity.
35 * 0: low active, 1: high active
43 * The conversion table has the adc value and temperature.
44 * ADC_DECREMENT: the adc value is of diminishing.(e.g. rk3288_code_table)
45 * ADC_INCREMENT: the adc value is incremental.(e.g. rk3368_code_table)
52 #include "thermal_hwmon.h"
55 * struct chip_tsadc_table - hold information about chip-specific differences
56 * @id: conversion table
57 * @length: size of conversion table
58 * @data_mask: mask to apply on data inputs
59 * @mode: sort mode of this adc variant (incrementing or decrementing)
61 struct chip_tsadc_table {
62 const struct tsadc_table *id;
65 enum adc_sort_mode mode;
69 * struct rockchip_tsadc_chip - hold the private data of tsadc chip
70 * @chn_offset: the channel offset of the first channel
71 * @chn_num: the channel number of tsadc chip
72 * @tshut_temp: the hardware-controlled shutdown temperature value
73 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
74 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
75 * @initialize: SoC special initialize tsadc controller method
76 * @irq_ack: clear the interrupt
77 * @control: enable/disable method for the tsadc controller
78 * @get_temp: get the temperature
79 * @set_alarm_temp: set the high temperature interrupt
80 * @set_tshut_temp: set the hardware-controlled shutdown temperature
81 * @set_tshut_mode: set the hardware-controlled shutdown mode
82 * @table: the chip-specific conversion table
84 struct rockchip_tsadc_chip {
85 /* The sensor id of chip correspond to the ADC channel */
89 /* The hardware-controlled tshut property */
91 enum tshut_mode tshut_mode;
92 enum tshut_polarity tshut_polarity;
94 /* Chip-wide methods */
95 void (*initialize)(struct regmap *grf,
96 void __iomem *reg, enum tshut_polarity p);
97 void (*irq_ack)(void __iomem *reg);
98 void (*control)(void __iomem *reg, bool on);
100 /* Per-sensor methods */
101 int (*get_temp)(const struct chip_tsadc_table *table,
102 int chn, void __iomem *reg, int *temp);
103 int (*set_alarm_temp)(const struct chip_tsadc_table *table,
104 int chn, void __iomem *reg, int temp);
105 int (*set_tshut_temp)(const struct chip_tsadc_table *table,
106 int chn, void __iomem *reg, int temp);
107 void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
109 /* Per-table methods */
110 struct chip_tsadc_table table;
114 * struct rockchip_thermal_sensor - hold the information of thermal sensor
115 * @thermal: pointer to the platform/configuration data
116 * @tzd: pointer to a thermal zone
117 * @id: identifier of the thermal sensor
119 struct rockchip_thermal_sensor {
120 struct rockchip_thermal_data *thermal;
121 struct thermal_zone_device *tzd;
126 * struct rockchip_thermal_data - hold the private data of thermal driver
127 * @chip: pointer to the platform/configuration data
128 * @pdev: platform device of thermal
129 * @reset: the reset controller of tsadc
130 * @sensors: array of thermal sensors
131 * @clk: the controller clock is divided by the exteral 24MHz
132 * @pclk: the advanced peripherals bus clock
133 * @grf: the general register file will be used to do static set by software
134 * @regs: the base address of tsadc controller
135 * @tshut_temp: the hardware-controlled shutdown temperature value
136 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
137 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
139 struct rockchip_thermal_data {
140 const struct rockchip_tsadc_chip *chip;
141 struct platform_device *pdev;
142 struct reset_control *reset;
144 struct rockchip_thermal_sensor *sensors;
153 enum tshut_mode tshut_mode;
154 enum tshut_polarity tshut_polarity;
158 * TSADC Sensor Register description:
160 * TSADCV2_* are used for RK3288 SoCs, the other chips can reuse it.
161 * TSADCV3_* are used for newer SoCs than RK3288. (e.g: RK3228, RK3399)
164 #define TSADCV2_USER_CON 0x00
165 #define TSADCV2_AUTO_CON 0x04
166 #define TSADCV2_INT_EN 0x08
167 #define TSADCV2_INT_PD 0x0c
168 #define TSADCV3_AUTO_SRC_CON 0x0c
169 #define TSADCV3_HT_INT_EN 0x14
170 #define TSADCV3_HSHUT_GPIO_INT_EN 0x18
171 #define TSADCV3_HSHUT_CRU_INT_EN 0x1c
172 #define TSADCV3_INT_PD 0x24
173 #define TSADCV3_HSHUT_PD 0x28
174 #define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04)
175 #define TSADCV2_COMP_INT(chn) (0x30 + (chn) * 0x04)
176 #define TSADCV2_COMP_SHUT(chn) (0x40 + (chn) * 0x04)
177 #define TSADCV3_DATA(chn) (0x2c + (chn) * 0x04)
178 #define TSADCV3_COMP_INT(chn) (0x6c + (chn) * 0x04)
179 #define TSADCV3_COMP_SHUT(chn) (0x10c + (chn) * 0x04)
180 #define TSADCV2_HIGHT_INT_DEBOUNCE 0x60
181 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE 0x64
182 #define TSADCV3_HIGHT_INT_DEBOUNCE 0x14c
183 #define TSADCV3_HIGHT_TSHUT_DEBOUNCE 0x150
184 #define TSADCV2_AUTO_PERIOD 0x68
185 #define TSADCV2_AUTO_PERIOD_HT 0x6c
186 #define TSADCV3_AUTO_PERIOD 0x154
187 #define TSADCV3_AUTO_PERIOD_HT 0x158
189 #define TSADCV2_AUTO_EN BIT(0)
190 #define TSADCV2_AUTO_EN_MASK BIT(16)
191 #define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn))
192 #define TSADCV3_AUTO_SRC_EN(chn) BIT(chn)
193 #define TSADCV3_AUTO_SRC_EN_MASK(chn) BIT(16 + chn)
194 #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8)
195 #define TSADCV2_AUTO_TSHUT_POLARITY_MASK BIT(24)
197 #define TSADCV3_AUTO_Q_SEL_EN BIT(1)
199 #define TSADCV2_INT_SRC_EN(chn) BIT(chn)
200 #define TSADCV2_INT_SRC_EN_MASK(chn) BIT(16 + (chn))
201 #define TSADCV2_SHUT_2GPIO_SRC_EN(chn) BIT(4 + (chn))
202 #define TSADCV2_SHUT_2CRU_SRC_EN(chn) BIT(8 + (chn))
204 #define TSADCV2_INT_PD_CLEAR_MASK ~BIT(8)
205 #define TSADCV3_INT_PD_CLEAR_MASK ~BIT(16)
206 #define TSADCV4_INT_PD_CLEAR_MASK 0xffffffff
208 #define TSADCV2_DATA_MASK 0xfff
209 #define TSADCV3_DATA_MASK 0x3ff
210 #define TSADCV4_DATA_MASK 0x1ff
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 TSADCV5_AUTO_PERIOD_TIME 1622 /* 2.5ms */
220 #define TSADCV5_AUTO_PERIOD_HT_TIME 1622 /* 2.5ms */
221 #define TSADCV6_AUTO_PERIOD_TIME 5000 /* 2.5ms */
222 #define TSADCV6_AUTO_PERIOD_HT_TIME 5000 /* 2.5ms */
224 #define TSADCV2_USER_INTER_PD_SOC 0x340 /* 13 clocks */
225 #define TSADCV5_USER_INTER_PD_SOC 0xfc0 /* 97us, at least 90us */
227 #define GRF_SARADC_TESTBIT 0x0e644
228 #define GRF_TSADC_TESTBIT_L 0x0e648
229 #define GRF_TSADC_TESTBIT_H 0x0e64c
231 #define PX30_GRF_SOC_CON2 0x0408
233 #define RK3568_GRF_TSADC_CON 0x0600
234 #define RK3568_GRF_TSADC_ANA_REG0 (0x10001 << 0)
235 #define RK3568_GRF_TSADC_ANA_REG1 (0x10001 << 1)
236 #define RK3568_GRF_TSADC_ANA_REG2 (0x10001 << 2)
237 #define RK3568_GRF_TSADC_TSEN (0x10001 << 8)
239 #define RK3588_GRF0_TSADC_CON 0x0100
241 #define RK3588_GRF0_TSADC_TRM (0xff0077 << 0)
242 #define RK3588_GRF0_TSADC_SHUT_2CRU (0x30003 << 10)
243 #define RK3588_GRF0_TSADC_SHUT_2GPIO (0x70007 << 12)
245 #define GRF_SARADC_TESTBIT_ON (0x10001 << 2)
246 #define GRF_TSADC_TESTBIT_H_ON (0x10001 << 2)
247 #define GRF_TSADC_VCM_EN_L (0x10001 << 7)
248 #define GRF_TSADC_VCM_EN_H (0x10001 << 7)
250 #define GRF_CON_TSADC_CH_INV (0x10001 << 1)
253 * struct tsadc_table - code to temperature conversion table
254 * @code: the value of adc channel
255 * @temp: the temperature
257 * code to temperature mapping of the temperature sensor is a piece wise linear
258 * curve.Any temperature, code faling between to 2 give temperatures can be
259 * linearly interpolated.
260 * Code to Temperature mapping should be updated based on manufacturer results.
267 static const struct tsadc_table rv1108_table[] = {
303 {TSADCV2_DATA_MASK, 125000},
306 static const struct tsadc_table rk3228_code_table[] = {
342 {TSADCV2_DATA_MASK, 125000},
345 static const struct tsadc_table rk3288_code_table[] = {
346 {TSADCV2_DATA_MASK, -40000},
384 static const struct tsadc_table rk3328_code_table[] = {
419 {TSADCV2_DATA_MASK, 125000},
422 static const struct tsadc_table rk3368_code_table[] = {
458 {TSADCV3_DATA_MASK, 125000},
461 static const struct tsadc_table rk3399_code_table[] = {
497 {TSADCV3_DATA_MASK, 125000},
500 static const struct tsadc_table rk3568_code_table[] = {
536 {TSADCV2_DATA_MASK, 125000},
539 static const struct tsadc_table rk3588_code_table[] = {
545 {TSADCV4_DATA_MASK, 125000},
548 static u32 rk_tsadcv2_temp_to_code(const struct chip_tsadc_table *table,
554 u32 error = table->data_mask;
557 high = (table->length - 1) - 1; /* ignore the last check for table */
558 mid = (high + low) / 2;
560 /* Return mask code data when the temp is over table range */
561 if (temp < table->id[low].temp || temp > table->id[high].temp)
564 while (low <= high) {
565 if (temp == table->id[mid].temp)
566 return table->id[mid].code;
567 else if (temp < table->id[mid].temp)
571 mid = (low + high) / 2;
575 * The conversion code granularity provided by the table. Let's
576 * assume that the relationship between temperature and
577 * analog value between 2 table entries is linear and interpolate
578 * to produce less granular result.
580 num = abs(table->id[mid + 1].code - table->id[mid].code);
581 num *= temp - table->id[mid].temp;
582 denom = table->id[mid + 1].temp - table->id[mid].temp;
584 switch (table->mode) {
586 return table->id[mid].code - (num / denom);
588 return table->id[mid].code + (num / denom);
590 pr_err("%s: unknown table mode: %d\n", __func__, table->mode);
595 pr_err("%s: invalid temperature, temp=%d error=%d\n",
596 __func__, temp, error);
600 static int rk_tsadcv2_code_to_temp(const struct chip_tsadc_table *table,
603 unsigned int low = 1;
604 unsigned int high = table->length - 1;
605 unsigned int mid = (low + high) / 2;
609 WARN_ON(table->length < 2);
611 switch (table->mode) {
613 code &= table->data_mask;
614 if (code <= table->id[high].code)
615 return -EAGAIN; /* Incorrect reading */
617 while (low <= high) {
618 if (code >= table->id[mid].code &&
619 code < table->id[mid - 1].code)
621 else if (code < table->id[mid].code)
626 mid = (low + high) / 2;
630 code &= table->data_mask;
631 if (code < table->id[low].code)
632 return -EAGAIN; /* Incorrect reading */
634 while (low <= high) {
635 if (code <= table->id[mid].code &&
636 code > table->id[mid - 1].code)
638 else if (code > table->id[mid].code)
643 mid = (low + high) / 2;
647 pr_err("%s: unknown table mode: %d\n", __func__, table->mode);
652 * The 5C granularity provided by the table is too much. Let's
653 * assume that the relationship between sensor readings and
654 * temperature between 2 table entries is linear and interpolate
655 * to produce less granular result.
657 num = table->id[mid].temp - table->id[mid - 1].temp;
658 num *= abs(table->id[mid - 1].code - code);
659 denom = abs(table->id[mid - 1].code - table->id[mid].code);
660 *temp = table->id[mid - 1].temp + (num / denom);
666 * rk_tsadcv2_initialize - initialize TASDC Controller.
667 * @grf: the general register file will be used to do static set by software
668 * @regs: the base address of tsadc controller
669 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
671 * (1) Set TSADC_V2_AUTO_PERIOD:
672 * Configure the interleave between every two accessing of
673 * TSADC in normal operation.
675 * (2) Set TSADCV2_AUTO_PERIOD_HT:
676 * Configure the interleave between every two accessing of
677 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
679 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
680 * If the temperature is higher than COMP_INT or COMP_SHUT for
681 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
683 static void rk_tsadcv2_initialize(struct regmap *grf, void __iomem *regs,
684 enum tshut_polarity tshut_polarity)
686 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
687 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
688 regs + TSADCV2_AUTO_CON);
690 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
691 regs + TSADCV2_AUTO_CON);
693 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
694 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
695 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
696 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
697 regs + TSADCV2_AUTO_PERIOD_HT);
698 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
699 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
703 * rk_tsadcv3_initialize - initialize TASDC Controller.
704 * @grf: the general register file will be used to do static set by software
705 * @regs: the base address of tsadc controller
706 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
708 * (1) The tsadc control power sequence.
710 * (2) Set TSADC_V2_AUTO_PERIOD:
711 * Configure the interleave between every two accessing of
712 * TSADC in normal operation.
714 * (2) Set TSADCV2_AUTO_PERIOD_HT:
715 * Configure the interleave between every two accessing of
716 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
718 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
719 * If the temperature is higher than COMP_INT or COMP_SHUT for
720 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
722 static void rk_tsadcv3_initialize(struct regmap *grf, void __iomem *regs,
723 enum tshut_polarity tshut_polarity)
725 /* The tsadc control power sequence */
727 /* Set interleave value to workround ic time sync issue */
728 writel_relaxed(TSADCV2_USER_INTER_PD_SOC, regs +
731 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME,
732 regs + TSADCV2_AUTO_PERIOD);
733 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
734 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
735 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
736 regs + TSADCV2_AUTO_PERIOD_HT);
737 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
738 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
741 /* Enable the voltage common mode feature */
742 regmap_write(grf, GRF_TSADC_TESTBIT_L, GRF_TSADC_VCM_EN_L);
743 regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_VCM_EN_H);
745 usleep_range(15, 100); /* The spec note says at least 15 us */
746 regmap_write(grf, GRF_SARADC_TESTBIT, GRF_SARADC_TESTBIT_ON);
747 regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_TESTBIT_H_ON);
748 usleep_range(90, 200); /* The spec note says at least 90 us */
750 writel_relaxed(TSADCV3_AUTO_PERIOD_TIME,
751 regs + TSADCV2_AUTO_PERIOD);
752 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
753 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
754 writel_relaxed(TSADCV3_AUTO_PERIOD_HT_TIME,
755 regs + TSADCV2_AUTO_PERIOD_HT);
756 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
757 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
760 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
761 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
762 regs + TSADCV2_AUTO_CON);
764 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
765 regs + TSADCV2_AUTO_CON);
768 static void rk_tsadcv4_initialize(struct regmap *grf, void __iomem *regs,
769 enum tshut_polarity tshut_polarity)
771 rk_tsadcv2_initialize(grf, regs, tshut_polarity);
772 regmap_write(grf, PX30_GRF_SOC_CON2, GRF_CON_TSADC_CH_INV);
775 static void rk_tsadcv7_initialize(struct regmap *grf, void __iomem *regs,
776 enum tshut_polarity tshut_polarity)
778 writel_relaxed(TSADCV5_USER_INTER_PD_SOC, regs + TSADCV2_USER_CON);
779 writel_relaxed(TSADCV5_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
780 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
781 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
782 writel_relaxed(TSADCV5_AUTO_PERIOD_HT_TIME,
783 regs + TSADCV2_AUTO_PERIOD_HT);
784 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
785 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
787 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
788 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
789 regs + TSADCV2_AUTO_CON);
791 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
792 regs + TSADCV2_AUTO_CON);
795 * The general register file will is optional
796 * and might not be available.
799 regmap_write(grf, RK3568_GRF_TSADC_CON, RK3568_GRF_TSADC_TSEN);
801 * RK3568 TRM, section 18.5. requires a delay no less
802 * than 10us between the rising edge of tsadc_tsen_en
803 * and the rising edge of tsadc_ana_reg_0/1/2.
806 regmap_write(grf, RK3568_GRF_TSADC_CON, RK3568_GRF_TSADC_ANA_REG0);
807 regmap_write(grf, RK3568_GRF_TSADC_CON, RK3568_GRF_TSADC_ANA_REG1);
808 regmap_write(grf, RK3568_GRF_TSADC_CON, RK3568_GRF_TSADC_ANA_REG2);
811 * RK3568 TRM, section 18.5. requires a delay no less
812 * than 90us after the rising edge of tsadc_ana_reg_0/1/2.
814 usleep_range(100, 200);
818 static void rk_tsadcv8_initialize(struct regmap *grf, void __iomem *regs,
819 enum tshut_polarity tshut_polarity)
821 writel_relaxed(TSADCV6_AUTO_PERIOD_TIME, regs + TSADCV3_AUTO_PERIOD);
822 writel_relaxed(TSADCV6_AUTO_PERIOD_HT_TIME,
823 regs + TSADCV3_AUTO_PERIOD_HT);
824 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
825 regs + TSADCV3_HIGHT_INT_DEBOUNCE);
826 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
827 regs + TSADCV3_HIGHT_TSHUT_DEBOUNCE);
828 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
829 writel_relaxed(TSADCV2_AUTO_TSHUT_POLARITY_HIGH |
830 TSADCV2_AUTO_TSHUT_POLARITY_MASK,
831 regs + TSADCV2_AUTO_CON);
833 writel_relaxed(TSADCV2_AUTO_TSHUT_POLARITY_MASK,
834 regs + TSADCV2_AUTO_CON);
837 static void rk_tsadcv2_irq_ack(void __iomem *regs)
841 val = readl_relaxed(regs + TSADCV2_INT_PD);
842 writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
845 static void rk_tsadcv3_irq_ack(void __iomem *regs)
849 val = readl_relaxed(regs + TSADCV2_INT_PD);
850 writel_relaxed(val & TSADCV3_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
853 static void rk_tsadcv4_irq_ack(void __iomem *regs)
857 val = readl_relaxed(regs + TSADCV3_INT_PD);
858 writel_relaxed(val & TSADCV4_INT_PD_CLEAR_MASK, regs + TSADCV3_INT_PD);
859 val = readl_relaxed(regs + TSADCV3_HSHUT_PD);
860 writel_relaxed(val & TSADCV3_INT_PD_CLEAR_MASK,
861 regs + TSADCV3_HSHUT_PD);
864 static void rk_tsadcv2_control(void __iomem *regs, bool enable)
868 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
870 val |= TSADCV2_AUTO_EN;
872 val &= ~TSADCV2_AUTO_EN;
874 writel_relaxed(val, regs + TSADCV2_AUTO_CON);
878 * rk_tsadcv3_control - the tsadc controller is enabled or disabled.
879 * @regs: the base address of tsadc controller
880 * @enable: boolean flag to enable the controller
882 * NOTE: TSADC controller works at auto mode, and some SoCs need set the
883 * tsadc_q_sel bit on TSADCV2_AUTO_CON[1]. The (1024 - tsadc_q) as output
884 * adc value if setting this bit to enable.
886 static void rk_tsadcv3_control(void __iomem *regs, bool enable)
890 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
892 val |= TSADCV2_AUTO_EN | TSADCV3_AUTO_Q_SEL_EN;
894 val &= ~TSADCV2_AUTO_EN;
896 writel_relaxed(val, regs + TSADCV2_AUTO_CON);
899 static void rk_tsadcv4_control(void __iomem *regs, bool enable)
904 val = TSADCV2_AUTO_EN | TSADCV2_AUTO_EN_MASK;
906 val = TSADCV2_AUTO_EN_MASK;
908 writel_relaxed(val, regs + TSADCV2_AUTO_CON);
911 static int rk_tsadcv2_get_temp(const struct chip_tsadc_table *table,
912 int chn, void __iomem *regs, int *temp)
916 val = readl_relaxed(regs + TSADCV2_DATA(chn));
918 return rk_tsadcv2_code_to_temp(table, val, temp);
921 static int rk_tsadcv4_get_temp(const struct chip_tsadc_table *table,
922 int chn, void __iomem *regs, int *temp)
926 val = readl_relaxed(regs + TSADCV3_DATA(chn));
928 return rk_tsadcv2_code_to_temp(table, val, temp);
931 static int rk_tsadcv2_alarm_temp(const struct chip_tsadc_table *table,
932 int chn, void __iomem *regs, int temp)
938 * In some cases, some sensors didn't need the trip points, the
939 * set_trips will pass {-INT_MAX, INT_MAX} to trigger tsadc alarm
940 * in the end, ignore this case and disable the high temperature
943 if (temp == INT_MAX) {
944 int_clr = readl_relaxed(regs + TSADCV2_INT_EN);
945 int_clr &= ~TSADCV2_INT_SRC_EN(chn);
946 writel_relaxed(int_clr, regs + TSADCV2_INT_EN);
950 /* Make sure the value is valid */
951 alarm_value = rk_tsadcv2_temp_to_code(table, temp);
952 if (alarm_value == table->data_mask)
955 writel_relaxed(alarm_value & table->data_mask,
956 regs + TSADCV2_COMP_INT(chn));
958 int_en = readl_relaxed(regs + TSADCV2_INT_EN);
959 int_en |= TSADCV2_INT_SRC_EN(chn);
960 writel_relaxed(int_en, regs + TSADCV2_INT_EN);
965 static int rk_tsadcv3_alarm_temp(const struct chip_tsadc_table *table,
966 int chn, void __iomem *regs, int temp)
971 * In some cases, some sensors didn't need the trip points, the
972 * set_trips will pass {-INT_MAX, INT_MAX} to trigger tsadc alarm
973 * in the end, ignore this case and disable the high temperature
976 if (temp == INT_MAX) {
977 writel_relaxed(TSADCV2_INT_SRC_EN_MASK(chn),
978 regs + TSADCV3_HT_INT_EN);
981 /* Make sure the value is valid */
982 alarm_value = rk_tsadcv2_temp_to_code(table, temp);
983 if (alarm_value == table->data_mask)
985 writel_relaxed(alarm_value & table->data_mask,
986 regs + TSADCV3_COMP_INT(chn));
987 writel_relaxed(TSADCV2_INT_SRC_EN(chn) | TSADCV2_INT_SRC_EN_MASK(chn),
988 regs + TSADCV3_HT_INT_EN);
992 static int rk_tsadcv2_tshut_temp(const struct chip_tsadc_table *table,
993 int chn, void __iomem *regs, int temp)
995 u32 tshut_value, val;
997 /* Make sure the value is valid */
998 tshut_value = rk_tsadcv2_temp_to_code(table, temp);
999 if (tshut_value == table->data_mask)
1002 writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
1004 /* TSHUT will be valid */
1005 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
1006 writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
1011 static int rk_tsadcv3_tshut_temp(const struct chip_tsadc_table *table,
1012 int chn, void __iomem *regs, int temp)
1016 /* Make sure the value is valid */
1017 tshut_value = rk_tsadcv2_temp_to_code(table, temp);
1018 if (tshut_value == table->data_mask)
1021 writel_relaxed(tshut_value, regs + TSADCV3_COMP_SHUT(chn));
1023 /* TSHUT will be valid */
1024 writel_relaxed(TSADCV3_AUTO_SRC_EN(chn) | TSADCV3_AUTO_SRC_EN_MASK(chn),
1025 regs + TSADCV3_AUTO_SRC_CON);
1030 static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
1031 enum tshut_mode mode)
1035 val = readl_relaxed(regs + TSADCV2_INT_EN);
1036 if (mode == TSHUT_MODE_GPIO) {
1037 val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
1038 val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
1040 val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
1041 val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
1044 writel_relaxed(val, regs + TSADCV2_INT_EN);
1047 static void rk_tsadcv3_tshut_mode(int chn, void __iomem *regs,
1048 enum tshut_mode mode)
1050 u32 val_gpio, val_cru;
1052 if (mode == TSHUT_MODE_GPIO) {
1053 val_gpio = TSADCV2_INT_SRC_EN(chn) | TSADCV2_INT_SRC_EN_MASK(chn);
1054 val_cru = TSADCV2_INT_SRC_EN_MASK(chn);
1056 val_cru = TSADCV2_INT_SRC_EN(chn) | TSADCV2_INT_SRC_EN_MASK(chn);
1057 val_gpio = TSADCV2_INT_SRC_EN_MASK(chn);
1059 writel_relaxed(val_gpio, regs + TSADCV3_HSHUT_GPIO_INT_EN);
1060 writel_relaxed(val_cru, regs + TSADCV3_HSHUT_CRU_INT_EN);
1063 static const struct rockchip_tsadc_chip px30_tsadc_data = {
1066 .chn_num = 2, /* 2 channels for tsadc */
1068 .tshut_mode = TSHUT_MODE_CRU, /* default TSHUT via CRU */
1069 .tshut_temp = 95000,
1071 .initialize = rk_tsadcv4_initialize,
1072 .irq_ack = rk_tsadcv3_irq_ack,
1073 .control = rk_tsadcv3_control,
1074 .get_temp = rk_tsadcv2_get_temp,
1075 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1076 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1077 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1080 .id = rk3328_code_table,
1081 .length = ARRAY_SIZE(rk3328_code_table),
1082 .data_mask = TSADCV2_DATA_MASK,
1083 .mode = ADC_INCREMENT,
1087 static const struct rockchip_tsadc_chip rv1108_tsadc_data = {
1090 .chn_num = 1, /* one channel for tsadc */
1092 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
1093 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1094 .tshut_temp = 95000,
1096 .initialize = rk_tsadcv2_initialize,
1097 .irq_ack = rk_tsadcv3_irq_ack,
1098 .control = rk_tsadcv3_control,
1099 .get_temp = rk_tsadcv2_get_temp,
1100 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1101 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1102 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1106 .length = ARRAY_SIZE(rv1108_table),
1107 .data_mask = TSADCV2_DATA_MASK,
1108 .mode = ADC_INCREMENT,
1112 static const struct rockchip_tsadc_chip rk3228_tsadc_data = {
1115 .chn_num = 1, /* one channel for tsadc */
1117 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
1118 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1119 .tshut_temp = 95000,
1121 .initialize = rk_tsadcv2_initialize,
1122 .irq_ack = rk_tsadcv3_irq_ack,
1123 .control = rk_tsadcv3_control,
1124 .get_temp = rk_tsadcv2_get_temp,
1125 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1126 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1127 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1130 .id = rk3228_code_table,
1131 .length = ARRAY_SIZE(rk3228_code_table),
1132 .data_mask = TSADCV3_DATA_MASK,
1133 .mode = ADC_INCREMENT,
1137 static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
1140 .chn_num = 2, /* two channels for tsadc */
1142 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
1143 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1144 .tshut_temp = 95000,
1146 .initialize = rk_tsadcv2_initialize,
1147 .irq_ack = rk_tsadcv2_irq_ack,
1148 .control = rk_tsadcv2_control,
1149 .get_temp = rk_tsadcv2_get_temp,
1150 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1151 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1152 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1155 .id = rk3288_code_table,
1156 .length = ARRAY_SIZE(rk3288_code_table),
1157 .data_mask = TSADCV2_DATA_MASK,
1158 .mode = ADC_DECREMENT,
1162 static const struct rockchip_tsadc_chip rk3328_tsadc_data = {
1165 .chn_num = 1, /* one channels for tsadc */
1167 .tshut_mode = TSHUT_MODE_CRU, /* default TSHUT via CRU */
1168 .tshut_temp = 95000,
1170 .initialize = rk_tsadcv2_initialize,
1171 .irq_ack = rk_tsadcv3_irq_ack,
1172 .control = rk_tsadcv3_control,
1173 .get_temp = rk_tsadcv2_get_temp,
1174 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1175 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1176 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1179 .id = rk3328_code_table,
1180 .length = ARRAY_SIZE(rk3328_code_table),
1181 .data_mask = TSADCV2_DATA_MASK,
1182 .mode = ADC_INCREMENT,
1186 static const struct rockchip_tsadc_chip rk3366_tsadc_data = {
1189 .chn_num = 2, /* two channels for tsadc */
1191 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
1192 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1193 .tshut_temp = 95000,
1195 .initialize = rk_tsadcv3_initialize,
1196 .irq_ack = rk_tsadcv3_irq_ack,
1197 .control = rk_tsadcv3_control,
1198 .get_temp = rk_tsadcv2_get_temp,
1199 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1200 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1201 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1204 .id = rk3228_code_table,
1205 .length = ARRAY_SIZE(rk3228_code_table),
1206 .data_mask = TSADCV3_DATA_MASK,
1207 .mode = ADC_INCREMENT,
1211 static const struct rockchip_tsadc_chip rk3368_tsadc_data = {
1214 .chn_num = 2, /* two channels for tsadc */
1216 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
1217 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1218 .tshut_temp = 95000,
1220 .initialize = rk_tsadcv2_initialize,
1221 .irq_ack = rk_tsadcv2_irq_ack,
1222 .control = rk_tsadcv2_control,
1223 .get_temp = rk_tsadcv2_get_temp,
1224 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1225 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1226 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1229 .id = rk3368_code_table,
1230 .length = ARRAY_SIZE(rk3368_code_table),
1231 .data_mask = TSADCV3_DATA_MASK,
1232 .mode = ADC_INCREMENT,
1236 static const struct rockchip_tsadc_chip rk3399_tsadc_data = {
1239 .chn_num = 2, /* two channels for tsadc */
1241 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
1242 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1243 .tshut_temp = 95000,
1245 .initialize = rk_tsadcv3_initialize,
1246 .irq_ack = rk_tsadcv3_irq_ack,
1247 .control = rk_tsadcv3_control,
1248 .get_temp = rk_tsadcv2_get_temp,
1249 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1250 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1251 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1254 .id = rk3399_code_table,
1255 .length = ARRAY_SIZE(rk3399_code_table),
1256 .data_mask = TSADCV3_DATA_MASK,
1257 .mode = ADC_INCREMENT,
1261 static const struct rockchip_tsadc_chip rk3568_tsadc_data = {
1264 .chn_num = 2, /* two channels for tsadc */
1266 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
1267 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1268 .tshut_temp = 95000,
1270 .initialize = rk_tsadcv7_initialize,
1271 .irq_ack = rk_tsadcv3_irq_ack,
1272 .control = rk_tsadcv3_control,
1273 .get_temp = rk_tsadcv2_get_temp,
1274 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1275 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1276 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1279 .id = rk3568_code_table,
1280 .length = ARRAY_SIZE(rk3568_code_table),
1281 .data_mask = TSADCV2_DATA_MASK,
1282 .mode = ADC_INCREMENT,
1286 static const struct rockchip_tsadc_chip rk3588_tsadc_data = {
1287 /* top, big_core0, big_core1, little_core, center, gpu, npu */
1289 .chn_num = 7, /* seven channels for tsadc */
1290 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
1291 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1292 .tshut_temp = 95000,
1293 .initialize = rk_tsadcv8_initialize,
1294 .irq_ack = rk_tsadcv4_irq_ack,
1295 .control = rk_tsadcv4_control,
1296 .get_temp = rk_tsadcv4_get_temp,
1297 .set_alarm_temp = rk_tsadcv3_alarm_temp,
1298 .set_tshut_temp = rk_tsadcv3_tshut_temp,
1299 .set_tshut_mode = rk_tsadcv3_tshut_mode,
1301 .id = rk3588_code_table,
1302 .length = ARRAY_SIZE(rk3588_code_table),
1303 .data_mask = TSADCV4_DATA_MASK,
1304 .mode = ADC_INCREMENT,
1308 static const struct of_device_id of_rockchip_thermal_match[] = {
1309 { .compatible = "rockchip,px30-tsadc",
1310 .data = (void *)&px30_tsadc_data,
1313 .compatible = "rockchip,rv1108-tsadc",
1314 .data = (void *)&rv1108_tsadc_data,
1317 .compatible = "rockchip,rk3228-tsadc",
1318 .data = (void *)&rk3228_tsadc_data,
1321 .compatible = "rockchip,rk3288-tsadc",
1322 .data = (void *)&rk3288_tsadc_data,
1325 .compatible = "rockchip,rk3328-tsadc",
1326 .data = (void *)&rk3328_tsadc_data,
1329 .compatible = "rockchip,rk3366-tsadc",
1330 .data = (void *)&rk3366_tsadc_data,
1333 .compatible = "rockchip,rk3368-tsadc",
1334 .data = (void *)&rk3368_tsadc_data,
1337 .compatible = "rockchip,rk3399-tsadc",
1338 .data = (void *)&rk3399_tsadc_data,
1341 .compatible = "rockchip,rk3568-tsadc",
1342 .data = (void *)&rk3568_tsadc_data,
1345 .compatible = "rockchip,rk3588-tsadc",
1346 .data = (void *)&rk3588_tsadc_data,
1350 MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
1353 rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
1355 struct thermal_zone_device *tzd = sensor->tzd;
1358 thermal_zone_device_enable(tzd);
1360 thermal_zone_device_disable(tzd);
1363 static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
1365 struct rockchip_thermal_data *thermal = dev;
1368 dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
1370 thermal->chip->irq_ack(thermal->regs);
1372 for (i = 0; i < thermal->chip->chn_num; i++)
1373 thermal_zone_device_update(thermal->sensors[i].tzd,
1374 THERMAL_EVENT_UNSPECIFIED);
1379 static int rockchip_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
1381 struct rockchip_thermal_sensor *sensor = thermal_zone_device_priv(tz);
1382 struct rockchip_thermal_data *thermal = sensor->thermal;
1383 const struct rockchip_tsadc_chip *tsadc = thermal->chip;
1385 dev_dbg(&thermal->pdev->dev, "%s: sensor %d: low: %d, high %d\n",
1386 __func__, sensor->id, low, high);
1388 return tsadc->set_alarm_temp(&tsadc->table,
1389 sensor->id, thermal->regs, high);
1392 static int rockchip_thermal_get_temp(struct thermal_zone_device *tz, int *out_temp)
1394 struct rockchip_thermal_sensor *sensor = thermal_zone_device_priv(tz);
1395 struct rockchip_thermal_data *thermal = sensor->thermal;
1396 const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
1399 retval = tsadc->get_temp(&tsadc->table,
1400 sensor->id, thermal->regs, out_temp);
1404 static const struct thermal_zone_device_ops rockchip_of_thermal_ops = {
1405 .get_temp = rockchip_thermal_get_temp,
1406 .set_trips = rockchip_thermal_set_trips,
1409 static int rockchip_configure_from_dt(struct device *dev,
1410 struct device_node *np,
1411 struct rockchip_thermal_data *thermal)
1413 u32 shut_temp, tshut_mode, tshut_polarity;
1415 if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
1417 "Missing tshut temp property, using default %d\n",
1418 thermal->chip->tshut_temp);
1419 thermal->tshut_temp = thermal->chip->tshut_temp;
1421 if (shut_temp > INT_MAX) {
1422 dev_err(dev, "Invalid tshut temperature specified: %d\n",
1426 thermal->tshut_temp = shut_temp;
1429 if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
1431 "Missing tshut mode property, using default (%s)\n",
1432 thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
1434 thermal->tshut_mode = thermal->chip->tshut_mode;
1436 thermal->tshut_mode = tshut_mode;
1439 if (thermal->tshut_mode > 1) {
1440 dev_err(dev, "Invalid tshut mode specified: %d\n",
1441 thermal->tshut_mode);
1445 if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
1448 "Missing tshut-polarity property, using default (%s)\n",
1449 thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
1451 thermal->tshut_polarity = thermal->chip->tshut_polarity;
1453 thermal->tshut_polarity = tshut_polarity;
1456 if (thermal->tshut_polarity > 1) {
1457 dev_err(dev, "Invalid tshut-polarity specified: %d\n",
1458 thermal->tshut_polarity);
1462 /* The tsadc wont to handle the error in here since some SoCs didn't
1463 * need this property.
1465 thermal->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
1466 if (IS_ERR(thermal->grf))
1467 dev_warn(dev, "Missing rockchip,grf property\n");
1473 rockchip_thermal_register_sensor(struct platform_device *pdev,
1474 struct rockchip_thermal_data *thermal,
1475 struct rockchip_thermal_sensor *sensor,
1478 const struct rockchip_tsadc_chip *tsadc = thermal->chip;
1481 tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
1483 error = tsadc->set_tshut_temp(&tsadc->table, id, thermal->regs,
1484 thermal->tshut_temp);
1486 dev_err(&pdev->dev, "%s: invalid tshut=%d, error=%d\n",
1487 __func__, thermal->tshut_temp, error);
1489 sensor->thermal = thermal;
1491 sensor->tzd = devm_thermal_of_zone_register(&pdev->dev, id, sensor,
1492 &rockchip_of_thermal_ops);
1493 if (IS_ERR(sensor->tzd)) {
1494 error = PTR_ERR(sensor->tzd);
1495 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
1504 * rockchip_thermal_reset_controller - Reset TSADC Controller, reset all tsadc registers.
1505 * @reset: the reset controller of tsadc
1507 static void rockchip_thermal_reset_controller(struct reset_control *reset)
1509 reset_control_assert(reset);
1510 usleep_range(10, 20);
1511 reset_control_deassert(reset);
1514 static int rockchip_thermal_probe(struct platform_device *pdev)
1516 struct device_node *np = pdev->dev.of_node;
1517 struct rockchip_thermal_data *thermal;
1522 irq = platform_get_irq(pdev, 0);
1526 thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
1531 thermal->pdev = pdev;
1533 thermal->chip = device_get_match_data(&pdev->dev);
1537 thermal->sensors = devm_kcalloc(&pdev->dev, thermal->chip->chn_num,
1538 sizeof(*thermal->sensors), GFP_KERNEL);
1539 if (!thermal->sensors)
1542 thermal->regs = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
1543 if (IS_ERR(thermal->regs))
1544 return PTR_ERR(thermal->regs);
1546 thermal->reset = devm_reset_control_array_get_exclusive(&pdev->dev);
1547 if (IS_ERR(thermal->reset))
1548 return dev_err_probe(&pdev->dev, PTR_ERR(thermal->reset),
1549 "failed to get tsadc reset.\n");
1551 thermal->clk = devm_clk_get_enabled(&pdev->dev, "tsadc");
1552 if (IS_ERR(thermal->clk))
1553 return dev_err_probe(&pdev->dev, PTR_ERR(thermal->clk),
1554 "failed to get tsadc clock.\n");
1556 thermal->pclk = devm_clk_get_enabled(&pdev->dev, "apb_pclk");
1557 if (IS_ERR(thermal->pclk))
1558 return dev_err_probe(&pdev->dev, PTR_ERR(thermal->pclk),
1559 "failed to get apb_pclk clock.\n");
1561 rockchip_thermal_reset_controller(thermal->reset);
1563 error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
1565 return dev_err_probe(&pdev->dev, error,
1566 "failed to parse device tree data\n");
1568 thermal->chip->initialize(thermal->grf, thermal->regs,
1569 thermal->tshut_polarity);
1571 for (i = 0; i < thermal->chip->chn_num; i++) {
1572 error = rockchip_thermal_register_sensor(pdev, thermal,
1573 &thermal->sensors[i],
1574 thermal->chip->chn_offset + i);
1576 return dev_err_probe(&pdev->dev, error,
1577 "failed to register sensor[%d].\n", i);
1580 error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1581 &rockchip_thermal_alarm_irq_thread,
1583 "rockchip_thermal", thermal);
1585 return dev_err_probe(&pdev->dev, error,
1586 "failed to request tsadc irq.\n");
1588 thermal->chip->control(thermal->regs, true);
1590 for (i = 0; i < thermal->chip->chn_num; i++) {
1591 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
1592 error = thermal_add_hwmon_sysfs(thermal->sensors[i].tzd);
1594 dev_warn(&pdev->dev,
1595 "failed to register sensor %d with hwmon: %d\n",
1599 platform_set_drvdata(pdev, thermal);
1604 static int rockchip_thermal_remove(struct platform_device *pdev)
1606 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
1609 for (i = 0; i < thermal->chip->chn_num; i++) {
1610 struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
1612 thermal_remove_hwmon_sysfs(sensor->tzd);
1613 rockchip_thermal_toggle_sensor(sensor, false);
1616 thermal->chip->control(thermal->regs, false);
1621 static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
1623 struct rockchip_thermal_data *thermal = dev_get_drvdata(dev);
1626 for (i = 0; i < thermal->chip->chn_num; i++)
1627 rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
1629 thermal->chip->control(thermal->regs, false);
1631 clk_disable(thermal->pclk);
1632 clk_disable(thermal->clk);
1634 pinctrl_pm_select_sleep_state(dev);
1639 static int __maybe_unused rockchip_thermal_resume(struct device *dev)
1641 struct rockchip_thermal_data *thermal = dev_get_drvdata(dev);
1645 error = clk_enable(thermal->clk);
1649 error = clk_enable(thermal->pclk);
1651 clk_disable(thermal->clk);
1655 rockchip_thermal_reset_controller(thermal->reset);
1657 thermal->chip->initialize(thermal->grf, thermal->regs,
1658 thermal->tshut_polarity);
1660 for (i = 0; i < thermal->chip->chn_num; i++) {
1661 int id = thermal->sensors[i].id;
1663 thermal->chip->set_tshut_mode(id, thermal->regs,
1664 thermal->tshut_mode);
1666 error = thermal->chip->set_tshut_temp(&thermal->chip->table,
1668 thermal->tshut_temp);
1670 dev_err(dev, "%s: invalid tshut=%d, error=%d\n",
1671 __func__, thermal->tshut_temp, error);
1674 thermal->chip->control(thermal->regs, true);
1676 for (i = 0; i < thermal->chip->chn_num; i++)
1677 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
1679 pinctrl_pm_select_default_state(dev);
1684 static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
1685 rockchip_thermal_suspend, rockchip_thermal_resume);
1687 static struct platform_driver rockchip_thermal_driver = {
1689 .name = "rockchip-thermal",
1690 .pm = &rockchip_thermal_pm_ops,
1691 .of_match_table = of_rockchip_thermal_match,
1693 .probe = rockchip_thermal_probe,
1694 .remove = rockchip_thermal_remove,
1697 module_platform_driver(rockchip_thermal_driver);
1699 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
1700 MODULE_AUTHOR("Rockchip, Inc.");
1701 MODULE_LICENSE("GPL v2");
1702 MODULE_ALIAS("platform:rockchip-thermal");