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 system has two Temperature Sensors.
44 * sensor0 is for CPU, and sensor1 is for GPU.
52 * The conversion table has the adc value and temperature.
53 * ADC_DECREMENT: the adc value is of diminishing.(e.g. rk3288_code_table)
54 * ADC_INCREMENT: the adc value is incremental.(e.g. rk3368_code_table)
61 #include "thermal_hwmon.h"
64 * The max sensors is two in rockchip SoCs.
65 * Two sensors: CPU and GPU sensor.
67 #define SOC_MAX_SENSORS 2
70 * struct chip_tsadc_table - hold information about chip-specific differences
71 * @id: conversion table
72 * @length: size of conversion table
73 * @data_mask: mask to apply on data inputs
74 * @mode: sort mode of this adc variant (incrementing or decrementing)
76 struct chip_tsadc_table {
77 const struct tsadc_table *id;
80 enum adc_sort_mode mode;
84 * struct rockchip_tsadc_chip - hold the private data of tsadc chip
85 * @chn_id: array of sensor ids of chip corresponding to the channel
86 * @chn_num: the channel number of tsadc chip
87 * @tshut_temp: the hardware-controlled shutdown temperature value
88 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
89 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
90 * @initialize: SoC special initialize tsadc controller method
91 * @irq_ack: clear the interrupt
92 * @control: enable/disable method for the tsadc controller
93 * @get_temp: get the temperature
94 * @set_alarm_temp: set the high temperature interrupt
95 * @set_tshut_temp: set the hardware-controlled shutdown temperature
96 * @set_tshut_mode: set the hardware-controlled shutdown mode
97 * @table: the chip-specific conversion table
99 struct rockchip_tsadc_chip {
100 /* The sensor id of chip correspond to the ADC channel */
101 int chn_id[SOC_MAX_SENSORS];
104 /* The hardware-controlled tshut property */
106 enum tshut_mode tshut_mode;
107 enum tshut_polarity tshut_polarity;
109 /* Chip-wide methods */
110 void (*initialize)(struct regmap *grf,
111 void __iomem *reg, enum tshut_polarity p);
112 void (*irq_ack)(void __iomem *reg);
113 void (*control)(void __iomem *reg, bool on);
115 /* Per-sensor methods */
116 int (*get_temp)(const struct chip_tsadc_table *table,
117 int chn, void __iomem *reg, int *temp);
118 int (*set_alarm_temp)(const struct chip_tsadc_table *table,
119 int chn, void __iomem *reg, int temp);
120 int (*set_tshut_temp)(const struct chip_tsadc_table *table,
121 int chn, void __iomem *reg, int temp);
122 void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
124 /* Per-table methods */
125 struct chip_tsadc_table table;
129 * struct rockchip_thermal_sensor - hold the information of thermal sensor
130 * @thermal: pointer to the platform/configuration data
131 * @tzd: pointer to a thermal zone
132 * @id: identifier of the thermal sensor
134 struct rockchip_thermal_sensor {
135 struct rockchip_thermal_data *thermal;
136 struct thermal_zone_device *tzd;
141 * struct rockchip_thermal_data - hold the private data of thermal driver
142 * @chip: pointer to the platform/configuration data
143 * @pdev: platform device of thermal
144 * @reset: the reset controller of tsadc
145 * @sensors: array of thermal sensors
146 * @clk: the controller clock is divided by the exteral 24MHz
147 * @pclk: the advanced peripherals bus clock
148 * @grf: the general register file will be used to do static set by software
149 * @regs: the base address of tsadc controller
150 * @tshut_temp: the hardware-controlled shutdown temperature value
151 * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
152 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
154 struct rockchip_thermal_data {
155 const struct rockchip_tsadc_chip *chip;
156 struct platform_device *pdev;
157 struct reset_control *reset;
159 struct rockchip_thermal_sensor sensors[SOC_MAX_SENSORS];
168 enum tshut_mode tshut_mode;
169 enum tshut_polarity tshut_polarity;
173 * TSADC Sensor Register description:
175 * TSADCV2_* are used for RK3288 SoCs, the other chips can reuse it.
176 * TSADCV3_* are used for newer SoCs than RK3288. (e.g: RK3228, RK3399)
179 #define TSADCV2_USER_CON 0x00
180 #define TSADCV2_AUTO_CON 0x04
181 #define TSADCV2_INT_EN 0x08
182 #define TSADCV2_INT_PD 0x0c
183 #define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04)
184 #define TSADCV2_COMP_INT(chn) (0x30 + (chn) * 0x04)
185 #define TSADCV2_COMP_SHUT(chn) (0x40 + (chn) * 0x04)
186 #define TSADCV2_HIGHT_INT_DEBOUNCE 0x60
187 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE 0x64
188 #define TSADCV2_AUTO_PERIOD 0x68
189 #define TSADCV2_AUTO_PERIOD_HT 0x6c
191 #define TSADCV2_AUTO_EN BIT(0)
192 #define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn))
193 #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8)
195 #define TSADCV3_AUTO_Q_SEL_EN BIT(1)
197 #define TSADCV2_INT_SRC_EN(chn) BIT(chn)
198 #define TSADCV2_SHUT_2GPIO_SRC_EN(chn) BIT(4 + (chn))
199 #define TSADCV2_SHUT_2CRU_SRC_EN(chn) BIT(8 + (chn))
201 #define TSADCV2_INT_PD_CLEAR_MASK ~BIT(8)
202 #define TSADCV3_INT_PD_CLEAR_MASK ~BIT(16)
204 #define TSADCV2_DATA_MASK 0xfff
205 #define TSADCV3_DATA_MASK 0x3ff
207 #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT 4
208 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT 4
209 #define TSADCV2_AUTO_PERIOD_TIME 250 /* 250ms */
210 #define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* 50ms */
211 #define TSADCV3_AUTO_PERIOD_TIME 1875 /* 2.5ms */
212 #define TSADCV3_AUTO_PERIOD_HT_TIME 1875 /* 2.5ms */
214 #define TSADCV5_AUTO_PERIOD_TIME 1622 /* 2.5ms */
215 #define TSADCV5_AUTO_PERIOD_HT_TIME 1622 /* 2.5ms */
217 #define TSADCV2_USER_INTER_PD_SOC 0x340 /* 13 clocks */
218 #define TSADCV5_USER_INTER_PD_SOC 0xfc0 /* 97us, at least 90us */
220 #define GRF_SARADC_TESTBIT 0x0e644
221 #define GRF_TSADC_TESTBIT_L 0x0e648
222 #define GRF_TSADC_TESTBIT_H 0x0e64c
224 #define PX30_GRF_SOC_CON2 0x0408
226 #define RK3568_GRF_TSADC_CON 0x0600
227 #define RK3568_GRF_TSADC_ANA_REG0 (0x10001 << 0)
228 #define RK3568_GRF_TSADC_ANA_REG1 (0x10001 << 1)
229 #define RK3568_GRF_TSADC_ANA_REG2 (0x10001 << 2)
230 #define RK3568_GRF_TSADC_TSEN (0x10001 << 8)
232 #define GRF_SARADC_TESTBIT_ON (0x10001 << 2)
233 #define GRF_TSADC_TESTBIT_H_ON (0x10001 << 2)
234 #define GRF_TSADC_VCM_EN_L (0x10001 << 7)
235 #define GRF_TSADC_VCM_EN_H (0x10001 << 7)
237 #define GRF_CON_TSADC_CH_INV (0x10001 << 1)
240 * struct tsadc_table - code to temperature conversion table
241 * @code: the value of adc channel
242 * @temp: the temperature
244 * code to temperature mapping of the temperature sensor is a piece wise linear
245 * curve.Any temperature, code faling between to 2 give temperatures can be
246 * linearly interpolated.
247 * Code to Temperature mapping should be updated based on manufacturer results.
254 static const struct tsadc_table rv1108_table[] = {
290 {TSADCV2_DATA_MASK, 125000},
293 static const struct tsadc_table rk3228_code_table[] = {
329 {TSADCV2_DATA_MASK, 125000},
332 static const struct tsadc_table rk3288_code_table[] = {
333 {TSADCV2_DATA_MASK, -40000},
371 static const struct tsadc_table rk3328_code_table[] = {
406 {TSADCV2_DATA_MASK, 125000},
409 static const struct tsadc_table rk3368_code_table[] = {
445 {TSADCV3_DATA_MASK, 125000},
448 static const struct tsadc_table rk3399_code_table[] = {
484 {TSADCV3_DATA_MASK, 125000},
487 static const struct tsadc_table rk3568_code_table[] = {
523 {TSADCV2_DATA_MASK, 125000},
526 static u32 rk_tsadcv2_temp_to_code(const struct chip_tsadc_table *table,
532 u32 error = table->data_mask;
535 high = (table->length - 1) - 1; /* ignore the last check for table */
536 mid = (high + low) / 2;
538 /* Return mask code data when the temp is over table range */
539 if (temp < table->id[low].temp || temp > table->id[high].temp)
542 while (low <= high) {
543 if (temp == table->id[mid].temp)
544 return table->id[mid].code;
545 else if (temp < table->id[mid].temp)
549 mid = (low + high) / 2;
553 * The conversion code granularity provided by the table. Let's
554 * assume that the relationship between temperature and
555 * analog value between 2 table entries is linear and interpolate
556 * to produce less granular result.
558 num = abs(table->id[mid + 1].code - table->id[mid].code);
559 num *= temp - table->id[mid].temp;
560 denom = table->id[mid + 1].temp - table->id[mid].temp;
562 switch (table->mode) {
564 return table->id[mid].code - (num / denom);
566 return table->id[mid].code + (num / denom);
568 pr_err("%s: unknown table mode: %d\n", __func__, table->mode);
573 pr_err("%s: invalid temperature, temp=%d error=%d\n",
574 __func__, temp, error);
578 static int rk_tsadcv2_code_to_temp(const struct chip_tsadc_table *table,
581 unsigned int low = 1;
582 unsigned int high = table->length - 1;
583 unsigned int mid = (low + high) / 2;
587 WARN_ON(table->length < 2);
589 switch (table->mode) {
591 code &= table->data_mask;
592 if (code <= table->id[high].code)
593 return -EAGAIN; /* Incorrect reading */
595 while (low <= high) {
596 if (code >= table->id[mid].code &&
597 code < table->id[mid - 1].code)
599 else if (code < table->id[mid].code)
604 mid = (low + high) / 2;
608 code &= table->data_mask;
609 if (code < table->id[low].code)
610 return -EAGAIN; /* Incorrect reading */
612 while (low <= high) {
613 if (code <= table->id[mid].code &&
614 code > table->id[mid - 1].code)
616 else if (code > table->id[mid].code)
621 mid = (low + high) / 2;
625 pr_err("%s: unknown table mode: %d\n", __func__, table->mode);
630 * The 5C granularity provided by the table is too much. Let's
631 * assume that the relationship between sensor readings and
632 * temperature between 2 table entries is linear and interpolate
633 * to produce less granular result.
635 num = table->id[mid].temp - table->id[mid - 1].temp;
636 num *= abs(table->id[mid - 1].code - code);
637 denom = abs(table->id[mid - 1].code - table->id[mid].code);
638 *temp = table->id[mid - 1].temp + (num / denom);
644 * rk_tsadcv2_initialize - initialize TASDC Controller.
645 * @grf: the general register file will be used to do static set by software
646 * @regs: the base address of tsadc controller
647 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
649 * (1) Set TSADC_V2_AUTO_PERIOD:
650 * Configure the interleave between every two accessing of
651 * TSADC in normal operation.
653 * (2) Set TSADCV2_AUTO_PERIOD_HT:
654 * Configure the interleave between every two accessing of
655 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
657 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
658 * If the temperature is higher than COMP_INT or COMP_SHUT for
659 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
661 static void rk_tsadcv2_initialize(struct regmap *grf, void __iomem *regs,
662 enum tshut_polarity tshut_polarity)
664 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
665 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
666 regs + TSADCV2_AUTO_CON);
668 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
669 regs + TSADCV2_AUTO_CON);
671 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
672 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
673 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
674 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
675 regs + TSADCV2_AUTO_PERIOD_HT);
676 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
677 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
681 * rk_tsadcv3_initialize - initialize TASDC Controller.
682 * @grf: the general register file will be used to do static set by software
683 * @regs: the base address of tsadc controller
684 * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
686 * (1) The tsadc control power sequence.
688 * (2) Set TSADC_V2_AUTO_PERIOD:
689 * Configure the interleave between every two accessing of
690 * TSADC in normal operation.
692 * (2) Set TSADCV2_AUTO_PERIOD_HT:
693 * Configure the interleave between every two accessing of
694 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
696 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
697 * If the temperature is higher than COMP_INT or COMP_SHUT for
698 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
700 static void rk_tsadcv3_initialize(struct regmap *grf, void __iomem *regs,
701 enum tshut_polarity tshut_polarity)
703 /* The tsadc control power sequence */
705 /* Set interleave value to workround ic time sync issue */
706 writel_relaxed(TSADCV2_USER_INTER_PD_SOC, regs +
709 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME,
710 regs + TSADCV2_AUTO_PERIOD);
711 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
712 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
713 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
714 regs + TSADCV2_AUTO_PERIOD_HT);
715 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
716 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
719 /* Enable the voltage common mode feature */
720 regmap_write(grf, GRF_TSADC_TESTBIT_L, GRF_TSADC_VCM_EN_L);
721 regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_VCM_EN_H);
723 usleep_range(15, 100); /* The spec note says at least 15 us */
724 regmap_write(grf, GRF_SARADC_TESTBIT, GRF_SARADC_TESTBIT_ON);
725 regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_TESTBIT_H_ON);
726 usleep_range(90, 200); /* The spec note says at least 90 us */
728 writel_relaxed(TSADCV3_AUTO_PERIOD_TIME,
729 regs + TSADCV2_AUTO_PERIOD);
730 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
731 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
732 writel_relaxed(TSADCV3_AUTO_PERIOD_HT_TIME,
733 regs + TSADCV2_AUTO_PERIOD_HT);
734 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
735 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
738 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
739 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
740 regs + TSADCV2_AUTO_CON);
742 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
743 regs + TSADCV2_AUTO_CON);
746 static void rk_tsadcv4_initialize(struct regmap *grf, void __iomem *regs,
747 enum tshut_polarity tshut_polarity)
749 rk_tsadcv2_initialize(grf, regs, tshut_polarity);
750 regmap_write(grf, PX30_GRF_SOC_CON2, GRF_CON_TSADC_CH_INV);
753 static void rk_tsadcv7_initialize(struct regmap *grf, void __iomem *regs,
754 enum tshut_polarity tshut_polarity)
756 writel_relaxed(TSADCV5_USER_INTER_PD_SOC, regs + TSADCV2_USER_CON);
757 writel_relaxed(TSADCV5_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
758 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
759 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
760 writel_relaxed(TSADCV5_AUTO_PERIOD_HT_TIME,
761 regs + TSADCV2_AUTO_PERIOD_HT);
762 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
763 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
765 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
766 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
767 regs + TSADCV2_AUTO_CON);
769 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
770 regs + TSADCV2_AUTO_CON);
773 * The general register file will is optional
774 * and might not be available.
777 regmap_write(grf, RK3568_GRF_TSADC_CON, RK3568_GRF_TSADC_TSEN);
779 * RK3568 TRM, section 18.5. requires a delay no less
780 * than 10us between the rising edge of tsadc_tsen_en
781 * and the rising edge of tsadc_ana_reg_0/1/2.
784 regmap_write(grf, RK3568_GRF_TSADC_CON, RK3568_GRF_TSADC_ANA_REG0);
785 regmap_write(grf, RK3568_GRF_TSADC_CON, RK3568_GRF_TSADC_ANA_REG1);
786 regmap_write(grf, RK3568_GRF_TSADC_CON, RK3568_GRF_TSADC_ANA_REG2);
789 * RK3568 TRM, section 18.5. requires a delay no less
790 * than 90us after the rising edge of tsadc_ana_reg_0/1/2.
792 usleep_range(100, 200);
796 static void rk_tsadcv2_irq_ack(void __iomem *regs)
800 val = readl_relaxed(regs + TSADCV2_INT_PD);
801 writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
804 static void rk_tsadcv3_irq_ack(void __iomem *regs)
808 val = readl_relaxed(regs + TSADCV2_INT_PD);
809 writel_relaxed(val & TSADCV3_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
812 static void rk_tsadcv2_control(void __iomem *regs, bool enable)
816 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
818 val |= TSADCV2_AUTO_EN;
820 val &= ~TSADCV2_AUTO_EN;
822 writel_relaxed(val, regs + TSADCV2_AUTO_CON);
826 * rk_tsadcv3_control - the tsadc controller is enabled or disabled.
827 * @regs: the base address of tsadc controller
828 * @enable: boolean flag to enable the controller
830 * NOTE: TSADC controller works at auto mode, and some SoCs need set the
831 * tsadc_q_sel bit on TSADCV2_AUTO_CON[1]. The (1024 - tsadc_q) as output
832 * adc value if setting this bit to enable.
834 static void rk_tsadcv3_control(void __iomem *regs, bool enable)
838 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
840 val |= TSADCV2_AUTO_EN | TSADCV3_AUTO_Q_SEL_EN;
842 val &= ~TSADCV2_AUTO_EN;
844 writel_relaxed(val, regs + TSADCV2_AUTO_CON);
847 static int rk_tsadcv2_get_temp(const struct chip_tsadc_table *table,
848 int chn, void __iomem *regs, int *temp)
852 val = readl_relaxed(regs + TSADCV2_DATA(chn));
854 return rk_tsadcv2_code_to_temp(table, val, temp);
857 static int rk_tsadcv2_alarm_temp(const struct chip_tsadc_table *table,
858 int chn, void __iomem *regs, int temp)
864 * In some cases, some sensors didn't need the trip points, the
865 * set_trips will pass {-INT_MAX, INT_MAX} to trigger tsadc alarm
866 * in the end, ignore this case and disable the high temperature
869 if (temp == INT_MAX) {
870 int_clr = readl_relaxed(regs + TSADCV2_INT_EN);
871 int_clr &= ~TSADCV2_INT_SRC_EN(chn);
872 writel_relaxed(int_clr, regs + TSADCV2_INT_EN);
876 /* Make sure the value is valid */
877 alarm_value = rk_tsadcv2_temp_to_code(table, temp);
878 if (alarm_value == table->data_mask)
881 writel_relaxed(alarm_value & table->data_mask,
882 regs + TSADCV2_COMP_INT(chn));
884 int_en = readl_relaxed(regs + TSADCV2_INT_EN);
885 int_en |= TSADCV2_INT_SRC_EN(chn);
886 writel_relaxed(int_en, regs + TSADCV2_INT_EN);
891 static int rk_tsadcv2_tshut_temp(const struct chip_tsadc_table *table,
892 int chn, void __iomem *regs, int temp)
894 u32 tshut_value, val;
896 /* Make sure the value is valid */
897 tshut_value = rk_tsadcv2_temp_to_code(table, temp);
898 if (tshut_value == table->data_mask)
901 writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
903 /* TSHUT will be valid */
904 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
905 writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
910 static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
911 enum tshut_mode mode)
915 val = readl_relaxed(regs + TSADCV2_INT_EN);
916 if (mode == TSHUT_MODE_GPIO) {
917 val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
918 val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
920 val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
921 val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
924 writel_relaxed(val, regs + TSADCV2_INT_EN);
927 static const struct rockchip_tsadc_chip px30_tsadc_data = {
928 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
929 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
930 .chn_num = 2, /* 2 channels for tsadc */
932 .tshut_mode = TSHUT_MODE_CRU, /* default TSHUT via CRU */
935 .initialize = rk_tsadcv4_initialize,
936 .irq_ack = rk_tsadcv3_irq_ack,
937 .control = rk_tsadcv3_control,
938 .get_temp = rk_tsadcv2_get_temp,
939 .set_alarm_temp = rk_tsadcv2_alarm_temp,
940 .set_tshut_temp = rk_tsadcv2_tshut_temp,
941 .set_tshut_mode = rk_tsadcv2_tshut_mode,
944 .id = rk3328_code_table,
945 .length = ARRAY_SIZE(rk3328_code_table),
946 .data_mask = TSADCV2_DATA_MASK,
947 .mode = ADC_INCREMENT,
951 static const struct rockchip_tsadc_chip rv1108_tsadc_data = {
952 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
953 .chn_num = 1, /* one channel for tsadc */
955 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
956 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
959 .initialize = rk_tsadcv2_initialize,
960 .irq_ack = rk_tsadcv3_irq_ack,
961 .control = rk_tsadcv3_control,
962 .get_temp = rk_tsadcv2_get_temp,
963 .set_alarm_temp = rk_tsadcv2_alarm_temp,
964 .set_tshut_temp = rk_tsadcv2_tshut_temp,
965 .set_tshut_mode = rk_tsadcv2_tshut_mode,
969 .length = ARRAY_SIZE(rv1108_table),
970 .data_mask = TSADCV2_DATA_MASK,
971 .mode = ADC_INCREMENT,
975 static const struct rockchip_tsadc_chip rk3228_tsadc_data = {
976 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
977 .chn_num = 1, /* one channel for tsadc */
979 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
980 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
983 .initialize = rk_tsadcv2_initialize,
984 .irq_ack = rk_tsadcv3_irq_ack,
985 .control = rk_tsadcv3_control,
986 .get_temp = rk_tsadcv2_get_temp,
987 .set_alarm_temp = rk_tsadcv2_alarm_temp,
988 .set_tshut_temp = rk_tsadcv2_tshut_temp,
989 .set_tshut_mode = rk_tsadcv2_tshut_mode,
992 .id = rk3228_code_table,
993 .length = ARRAY_SIZE(rk3228_code_table),
994 .data_mask = TSADCV3_DATA_MASK,
995 .mode = ADC_INCREMENT,
999 static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
1000 .chn_id[SENSOR_CPU] = 1, /* cpu sensor is channel 1 */
1001 .chn_id[SENSOR_GPU] = 2, /* gpu sensor is channel 2 */
1002 .chn_num = 2, /* two channels for tsadc */
1004 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
1005 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1006 .tshut_temp = 95000,
1008 .initialize = rk_tsadcv2_initialize,
1009 .irq_ack = rk_tsadcv2_irq_ack,
1010 .control = rk_tsadcv2_control,
1011 .get_temp = rk_tsadcv2_get_temp,
1012 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1013 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1014 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1017 .id = rk3288_code_table,
1018 .length = ARRAY_SIZE(rk3288_code_table),
1019 .data_mask = TSADCV2_DATA_MASK,
1020 .mode = ADC_DECREMENT,
1024 static const struct rockchip_tsadc_chip rk3328_tsadc_data = {
1025 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
1026 .chn_num = 1, /* one channels for tsadc */
1028 .tshut_mode = TSHUT_MODE_CRU, /* default TSHUT via CRU */
1029 .tshut_temp = 95000,
1031 .initialize = rk_tsadcv2_initialize,
1032 .irq_ack = rk_tsadcv3_irq_ack,
1033 .control = rk_tsadcv3_control,
1034 .get_temp = rk_tsadcv2_get_temp,
1035 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1036 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1037 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1040 .id = rk3328_code_table,
1041 .length = ARRAY_SIZE(rk3328_code_table),
1042 .data_mask = TSADCV2_DATA_MASK,
1043 .mode = ADC_INCREMENT,
1047 static const struct rockchip_tsadc_chip rk3366_tsadc_data = {
1048 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
1049 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
1050 .chn_num = 2, /* two channels for tsadc */
1052 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
1053 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1054 .tshut_temp = 95000,
1056 .initialize = rk_tsadcv3_initialize,
1057 .irq_ack = rk_tsadcv3_irq_ack,
1058 .control = rk_tsadcv3_control,
1059 .get_temp = rk_tsadcv2_get_temp,
1060 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1061 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1062 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1065 .id = rk3228_code_table,
1066 .length = ARRAY_SIZE(rk3228_code_table),
1067 .data_mask = TSADCV3_DATA_MASK,
1068 .mode = ADC_INCREMENT,
1072 static const struct rockchip_tsadc_chip rk3368_tsadc_data = {
1073 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
1074 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
1075 .chn_num = 2, /* two channels for tsadc */
1077 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
1078 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1079 .tshut_temp = 95000,
1081 .initialize = rk_tsadcv2_initialize,
1082 .irq_ack = rk_tsadcv2_irq_ack,
1083 .control = rk_tsadcv2_control,
1084 .get_temp = rk_tsadcv2_get_temp,
1085 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1086 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1087 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1090 .id = rk3368_code_table,
1091 .length = ARRAY_SIZE(rk3368_code_table),
1092 .data_mask = TSADCV3_DATA_MASK,
1093 .mode = ADC_INCREMENT,
1097 static const struct rockchip_tsadc_chip rk3399_tsadc_data = {
1098 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
1099 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
1100 .chn_num = 2, /* two channels for tsadc */
1102 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
1103 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1104 .tshut_temp = 95000,
1106 .initialize = rk_tsadcv3_initialize,
1107 .irq_ack = rk_tsadcv3_irq_ack,
1108 .control = rk_tsadcv3_control,
1109 .get_temp = rk_tsadcv2_get_temp,
1110 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1111 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1112 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1115 .id = rk3399_code_table,
1116 .length = ARRAY_SIZE(rk3399_code_table),
1117 .data_mask = TSADCV3_DATA_MASK,
1118 .mode = ADC_INCREMENT,
1122 static const struct rockchip_tsadc_chip rk3568_tsadc_data = {
1123 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
1124 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
1125 .chn_num = 2, /* two channels for tsadc */
1127 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
1128 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
1129 .tshut_temp = 95000,
1131 .initialize = rk_tsadcv7_initialize,
1132 .irq_ack = rk_tsadcv3_irq_ack,
1133 .control = rk_tsadcv3_control,
1134 .get_temp = rk_tsadcv2_get_temp,
1135 .set_alarm_temp = rk_tsadcv2_alarm_temp,
1136 .set_tshut_temp = rk_tsadcv2_tshut_temp,
1137 .set_tshut_mode = rk_tsadcv2_tshut_mode,
1140 .id = rk3568_code_table,
1141 .length = ARRAY_SIZE(rk3568_code_table),
1142 .data_mask = TSADCV2_DATA_MASK,
1143 .mode = ADC_INCREMENT,
1147 static const struct of_device_id of_rockchip_thermal_match[] = {
1148 { .compatible = "rockchip,px30-tsadc",
1149 .data = (void *)&px30_tsadc_data,
1152 .compatible = "rockchip,rv1108-tsadc",
1153 .data = (void *)&rv1108_tsadc_data,
1156 .compatible = "rockchip,rk3228-tsadc",
1157 .data = (void *)&rk3228_tsadc_data,
1160 .compatible = "rockchip,rk3288-tsadc",
1161 .data = (void *)&rk3288_tsadc_data,
1164 .compatible = "rockchip,rk3328-tsadc",
1165 .data = (void *)&rk3328_tsadc_data,
1168 .compatible = "rockchip,rk3366-tsadc",
1169 .data = (void *)&rk3366_tsadc_data,
1172 .compatible = "rockchip,rk3368-tsadc",
1173 .data = (void *)&rk3368_tsadc_data,
1176 .compatible = "rockchip,rk3399-tsadc",
1177 .data = (void *)&rk3399_tsadc_data,
1180 .compatible = "rockchip,rk3568-tsadc",
1181 .data = (void *)&rk3568_tsadc_data,
1185 MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
1188 rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
1190 struct thermal_zone_device *tzd = sensor->tzd;
1193 thermal_zone_device_enable(tzd);
1195 thermal_zone_device_disable(tzd);
1198 static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
1200 struct rockchip_thermal_data *thermal = dev;
1203 dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
1205 thermal->chip->irq_ack(thermal->regs);
1207 for (i = 0; i < thermal->chip->chn_num; i++)
1208 thermal_zone_device_update(thermal->sensors[i].tzd,
1209 THERMAL_EVENT_UNSPECIFIED);
1214 static int rockchip_thermal_set_trips(void *_sensor, int low, int high)
1216 struct rockchip_thermal_sensor *sensor = _sensor;
1217 struct rockchip_thermal_data *thermal = sensor->thermal;
1218 const struct rockchip_tsadc_chip *tsadc = thermal->chip;
1220 dev_dbg(&thermal->pdev->dev, "%s: sensor %d: low: %d, high %d\n",
1221 __func__, sensor->id, low, high);
1223 return tsadc->set_alarm_temp(&tsadc->table,
1224 sensor->id, thermal->regs, high);
1227 static int rockchip_thermal_get_temp(void *_sensor, int *out_temp)
1229 struct rockchip_thermal_sensor *sensor = _sensor;
1230 struct rockchip_thermal_data *thermal = sensor->thermal;
1231 const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
1234 retval = tsadc->get_temp(&tsadc->table,
1235 sensor->id, thermal->regs, out_temp);
1236 dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n",
1237 sensor->id, *out_temp, retval);
1242 static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = {
1243 .get_temp = rockchip_thermal_get_temp,
1244 .set_trips = rockchip_thermal_set_trips,
1247 static int rockchip_configure_from_dt(struct device *dev,
1248 struct device_node *np,
1249 struct rockchip_thermal_data *thermal)
1251 u32 shut_temp, tshut_mode, tshut_polarity;
1253 if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
1255 "Missing tshut temp property, using default %d\n",
1256 thermal->chip->tshut_temp);
1257 thermal->tshut_temp = thermal->chip->tshut_temp;
1259 if (shut_temp > INT_MAX) {
1260 dev_err(dev, "Invalid tshut temperature specified: %d\n",
1264 thermal->tshut_temp = shut_temp;
1267 if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
1269 "Missing tshut mode property, using default (%s)\n",
1270 thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
1272 thermal->tshut_mode = thermal->chip->tshut_mode;
1274 thermal->tshut_mode = tshut_mode;
1277 if (thermal->tshut_mode > 1) {
1278 dev_err(dev, "Invalid tshut mode specified: %d\n",
1279 thermal->tshut_mode);
1283 if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
1286 "Missing tshut-polarity property, using default (%s)\n",
1287 thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
1289 thermal->tshut_polarity = thermal->chip->tshut_polarity;
1291 thermal->tshut_polarity = tshut_polarity;
1294 if (thermal->tshut_polarity > 1) {
1295 dev_err(dev, "Invalid tshut-polarity specified: %d\n",
1296 thermal->tshut_polarity);
1300 /* The tsadc wont to handle the error in here since some SoCs didn't
1301 * need this property.
1303 thermal->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
1304 if (IS_ERR(thermal->grf))
1305 dev_warn(dev, "Missing rockchip,grf property\n");
1311 rockchip_thermal_register_sensor(struct platform_device *pdev,
1312 struct rockchip_thermal_data *thermal,
1313 struct rockchip_thermal_sensor *sensor,
1316 const struct rockchip_tsadc_chip *tsadc = thermal->chip;
1319 tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
1321 error = tsadc->set_tshut_temp(&tsadc->table, id, thermal->regs,
1322 thermal->tshut_temp);
1324 dev_err(&pdev->dev, "%s: invalid tshut=%d, error=%d\n",
1325 __func__, thermal->tshut_temp, error);
1327 sensor->thermal = thermal;
1329 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, id,
1330 sensor, &rockchip_of_thermal_ops);
1331 if (IS_ERR(sensor->tzd)) {
1332 error = PTR_ERR(sensor->tzd);
1333 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
1342 * Reset TSADC Controller, reset all tsadc registers.
1343 * @reset: the reset controller of tsadc
1345 static void rockchip_thermal_reset_controller(struct reset_control *reset)
1347 reset_control_assert(reset);
1348 usleep_range(10, 20);
1349 reset_control_deassert(reset);
1352 static int rockchip_thermal_probe(struct platform_device *pdev)
1354 struct device_node *np = pdev->dev.of_node;
1355 struct rockchip_thermal_data *thermal;
1356 const struct of_device_id *match;
1357 struct resource *res;
1362 match = of_match_node(of_rockchip_thermal_match, np);
1366 irq = platform_get_irq(pdev, 0);
1370 thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
1375 thermal->pdev = pdev;
1377 thermal->chip = (const struct rockchip_tsadc_chip *)match->data;
1381 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1382 thermal->regs = devm_ioremap_resource(&pdev->dev, res);
1383 if (IS_ERR(thermal->regs))
1384 return PTR_ERR(thermal->regs);
1386 thermal->reset = devm_reset_control_array_get(&pdev->dev, false, false);
1387 if (IS_ERR(thermal->reset)) {
1388 error = PTR_ERR(thermal->reset);
1389 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
1393 thermal->clk = devm_clk_get(&pdev->dev, "tsadc");
1394 if (IS_ERR(thermal->clk)) {
1395 error = PTR_ERR(thermal->clk);
1396 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
1400 thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
1401 if (IS_ERR(thermal->pclk)) {
1402 error = PTR_ERR(thermal->pclk);
1403 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
1408 error = clk_prepare_enable(thermal->clk);
1410 dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
1415 error = clk_prepare_enable(thermal->pclk);
1417 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
1418 goto err_disable_clk;
1421 rockchip_thermal_reset_controller(thermal->reset);
1423 error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
1425 dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
1427 goto err_disable_pclk;
1430 thermal->chip->initialize(thermal->grf, thermal->regs,
1431 thermal->tshut_polarity);
1433 for (i = 0; i < thermal->chip->chn_num; i++) {
1434 error = rockchip_thermal_register_sensor(pdev, thermal,
1435 &thermal->sensors[i],
1436 thermal->chip->chn_id[i]);
1439 "failed to register sensor[%d] : error = %d\n",
1441 goto err_disable_pclk;
1445 error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1446 &rockchip_thermal_alarm_irq_thread,
1448 "rockchip_thermal", thermal);
1451 "failed to request tsadc irq: %d\n", error);
1452 goto err_disable_pclk;
1455 thermal->chip->control(thermal->regs, true);
1457 for (i = 0; i < thermal->chip->chn_num; i++) {
1458 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
1459 thermal->sensors[i].tzd->tzp->no_hwmon = false;
1460 error = thermal_add_hwmon_sysfs(thermal->sensors[i].tzd);
1462 dev_warn(&pdev->dev,
1463 "failed to register sensor %d with hwmon: %d\n",
1467 platform_set_drvdata(pdev, thermal);
1472 clk_disable_unprepare(thermal->pclk);
1474 clk_disable_unprepare(thermal->clk);
1479 static int rockchip_thermal_remove(struct platform_device *pdev)
1481 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
1484 for (i = 0; i < thermal->chip->chn_num; i++) {
1485 struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
1487 thermal_remove_hwmon_sysfs(sensor->tzd);
1488 rockchip_thermal_toggle_sensor(sensor, false);
1491 thermal->chip->control(thermal->regs, false);
1493 clk_disable_unprepare(thermal->pclk);
1494 clk_disable_unprepare(thermal->clk);
1499 static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
1501 struct rockchip_thermal_data *thermal = dev_get_drvdata(dev);
1504 for (i = 0; i < thermal->chip->chn_num; i++)
1505 rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
1507 thermal->chip->control(thermal->regs, false);
1509 clk_disable(thermal->pclk);
1510 clk_disable(thermal->clk);
1512 pinctrl_pm_select_sleep_state(dev);
1517 static int __maybe_unused rockchip_thermal_resume(struct device *dev)
1519 struct rockchip_thermal_data *thermal = dev_get_drvdata(dev);
1523 error = clk_enable(thermal->clk);
1527 error = clk_enable(thermal->pclk);
1529 clk_disable(thermal->clk);
1533 rockchip_thermal_reset_controller(thermal->reset);
1535 thermal->chip->initialize(thermal->grf, thermal->regs,
1536 thermal->tshut_polarity);
1538 for (i = 0; i < thermal->chip->chn_num; i++) {
1539 int id = thermal->sensors[i].id;
1541 thermal->chip->set_tshut_mode(id, thermal->regs,
1542 thermal->tshut_mode);
1544 error = thermal->chip->set_tshut_temp(&thermal->chip->table,
1546 thermal->tshut_temp);
1548 dev_err(dev, "%s: invalid tshut=%d, error=%d\n",
1549 __func__, thermal->tshut_temp, error);
1552 thermal->chip->control(thermal->regs, true);
1554 for (i = 0; i < thermal->chip->chn_num; i++)
1555 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
1557 pinctrl_pm_select_default_state(dev);
1562 static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
1563 rockchip_thermal_suspend, rockchip_thermal_resume);
1565 static struct platform_driver rockchip_thermal_driver = {
1567 .name = "rockchip-thermal",
1568 .pm = &rockchip_thermal_pm_ops,
1569 .of_match_table = of_rockchip_thermal_match,
1571 .probe = rockchip_thermal_probe,
1572 .remove = rockchip_thermal_remove,
1575 module_platform_driver(rockchip_thermal_driver);
1577 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
1578 MODULE_AUTHOR("Rockchip, Inc.");
1579 MODULE_LICENSE("GPL v2");
1580 MODULE_ALIAS("platform:rockchip-thermal");