Thomas Gleixner | 2025cf9 | 2019-05-29 07:18:02 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 2 | /* |
Caesar Wang | 678065d | 2016-04-18 11:35:58 +0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Fuzhou Rockchip Electronics Co., Ltd |
Caesar Wang | 20f0af7 | 2015-11-09 12:48:59 +0800 | [diff] [blame] | 4 | * Caesar Wang <wxt@rock-chips.com> |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/clk.h> |
| 8 | #include <linux/delay.h> |
| 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/io.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/of.h> |
| 13 | #include <linux/of_address.h> |
| 14 | #include <linux/of_irq.h> |
| 15 | #include <linux/platform_device.h> |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 16 | #include <linux/regmap.h> |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 17 | #include <linux/reset.h> |
| 18 | #include <linux/thermal.h> |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 19 | #include <linux/mfd/syscon.h> |
Caesar Wang | c970872 | 2015-11-11 19:43:11 -0800 | [diff] [blame] | 20 | #include <linux/pinctrl/consumer.h> |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 21 | |
Amit Kucheria | 66ec4bf | 2019-11-20 21:15:16 +0530 | [diff] [blame] | 22 | /* |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 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. |
| 26 | */ |
| 27 | enum tshut_mode { |
| 28 | TSHUT_MODE_CRU = 0, |
| 29 | TSHUT_MODE_GPIO, |
| 30 | }; |
| 31 | |
Amit Kucheria | 66ec4bf | 2019-11-20 21:15:16 +0530 | [diff] [blame] | 32 | /* |
Caesar Wang | 13c1cfd | 2015-12-03 16:48:39 +0800 | [diff] [blame] | 33 | * The system Temperature Sensors tshut(tshut) polarity |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 34 | * the bit 8 is tshut polarity. |
| 35 | * 0: low active, 1: high active |
| 36 | */ |
| 37 | enum tshut_polarity { |
| 38 | TSHUT_LOW_ACTIVE = 0, |
| 39 | TSHUT_HIGH_ACTIVE, |
| 40 | }; |
| 41 | |
Amit Kucheria | 66ec4bf | 2019-11-20 21:15:16 +0530 | [diff] [blame] | 42 | /* |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 43 | * The system has two Temperature Sensors. |
| 44 | * sensor0 is for CPU, and sensor1 is for GPU. |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 45 | */ |
| 46 | enum sensor_id { |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 47 | SENSOR_CPU = 0, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 48 | SENSOR_GPU, |
| 49 | }; |
| 50 | |
Amit Kucheria | 66ec4bf | 2019-11-20 21:15:16 +0530 | [diff] [blame] | 51 | /* |
Caesar Wang | 13c1cfd | 2015-12-03 16:48:39 +0800 | [diff] [blame] | 52 | * The conversion table has the adc value and temperature. |
Caesar Wang | 952418a | 2016-02-15 15:33:30 +0800 | [diff] [blame] | 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) |
Caesar Wang | 13c1cfd | 2015-12-03 16:48:39 +0800 | [diff] [blame] | 55 | */ |
Caesar Wang | 020ba95 | 2015-11-09 12:48:57 +0800 | [diff] [blame] | 56 | enum adc_sort_mode { |
| 57 | ADC_DECREMENT = 0, |
| 58 | ADC_INCREMENT, |
| 59 | }; |
| 60 | |
Stefan Schaeckeler | d27970b | 2019-12-11 22:17:02 -0800 | [diff] [blame] | 61 | #include "thermal_hwmon.h" |
| 62 | |
Caesar Wang | 020ba95 | 2015-11-09 12:48:57 +0800 | [diff] [blame] | 63 | /** |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 64 | * The max sensors is two in rockchip SoCs. |
| 65 | * Two sensors: CPU and GPU sensor. |
| 66 | */ |
| 67 | #define SOC_MAX_SENSORS 2 |
| 68 | |
Caesar Wang | 13c1cfd | 2015-12-03 16:48:39 +0800 | [diff] [blame] | 69 | /** |
Caesar Wang | 678065d | 2016-04-18 11:35:58 +0800 | [diff] [blame] | 70 | * struct chip_tsadc_table - hold information about chip-specific differences |
Caesar Wang | 13c1cfd | 2015-12-03 16:48:39 +0800 | [diff] [blame] | 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) |
| 75 | */ |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 76 | struct chip_tsadc_table { |
| 77 | const struct tsadc_table *id; |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 78 | unsigned int length; |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 79 | u32 data_mask; |
Caesar Wang | 020ba95 | 2015-11-09 12:48:57 +0800 | [diff] [blame] | 80 | enum adc_sort_mode mode; |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 81 | }; |
| 82 | |
Caesar Wang | 678065d | 2016-04-18 11:35:58 +0800 | [diff] [blame] | 83 | /** |
| 84 | * struct rockchip_tsadc_chip - hold the private data of tsadc chip |
Amit Kucheria | 66ec4bf | 2019-11-20 21:15:16 +0530 | [diff] [blame] | 85 | * @chn_id: array of sensor ids of chip corresponding to the channel |
Caesar Wang | 678065d | 2016-04-18 11:35:58 +0800 | [diff] [blame] | 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 |
Amit Kucheria | 66ec4bf | 2019-11-20 21:15:16 +0530 | [diff] [blame] | 92 | * @control: enable/disable method for the tsadc controller |
Caesar Wang | 678065d | 2016-04-18 11:35:58 +0800 | [diff] [blame] | 93 | * @get_temp: get the temperature |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 94 | * @set_alarm_temp: set the high temperature interrupt |
Caesar Wang | 678065d | 2016-04-18 11:35:58 +0800 | [diff] [blame] | 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 |
| 98 | */ |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 99 | struct rockchip_tsadc_chip { |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 100 | /* The sensor id of chip correspond to the ADC channel */ |
| 101 | int chn_id[SOC_MAX_SENSORS]; |
| 102 | int chn_num; |
| 103 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 104 | /* The hardware-controlled tshut property */ |
Caesar Wang | 437df21 | 2015-11-09 12:48:58 +0800 | [diff] [blame] | 105 | int tshut_temp; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 106 | enum tshut_mode tshut_mode; |
| 107 | enum tshut_polarity tshut_polarity; |
| 108 | |
| 109 | /* Chip-wide methods */ |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 110 | void (*initialize)(struct regmap *grf, |
| 111 | void __iomem *reg, enum tshut_polarity p); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 112 | void (*irq_ack)(void __iomem *reg); |
| 113 | void (*control)(void __iomem *reg, bool on); |
| 114 | |
| 115 | /* Per-sensor methods */ |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 116 | int (*get_temp)(const struct chip_tsadc_table *table, |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 117 | int chn, void __iomem *reg, int *temp); |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 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); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 122 | void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m); |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 123 | |
| 124 | /* Per-table methods */ |
| 125 | struct chip_tsadc_table table; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 126 | }; |
| 127 | |
Caesar Wang | 678065d | 2016-04-18 11:35:58 +0800 | [diff] [blame] | 128 | /** |
| 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 |
| 133 | */ |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 134 | struct rockchip_thermal_sensor { |
| 135 | struct rockchip_thermal_data *thermal; |
| 136 | struct thermal_zone_device *tzd; |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 137 | int id; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 138 | }; |
| 139 | |
Caesar Wang | 678065d | 2016-04-18 11:35:58 +0800 | [diff] [blame] | 140 | /** |
| 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 |
Amit Kucheria | 66ec4bf | 2019-11-20 21:15:16 +0530 | [diff] [blame] | 145 | * @sensors: array of thermal sensors |
Caesar Wang | 678065d | 2016-04-18 11:35:58 +0800 | [diff] [blame] | 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) |
| 153 | */ |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 154 | struct rockchip_thermal_data { |
| 155 | const struct rockchip_tsadc_chip *chip; |
| 156 | struct platform_device *pdev; |
| 157 | struct reset_control *reset; |
| 158 | |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 159 | struct rockchip_thermal_sensor sensors[SOC_MAX_SENSORS]; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 160 | |
| 161 | struct clk *clk; |
| 162 | struct clk *pclk; |
| 163 | |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 164 | struct regmap *grf; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 165 | void __iomem *regs; |
| 166 | |
Caesar Wang | 437df21 | 2015-11-09 12:48:58 +0800 | [diff] [blame] | 167 | int tshut_temp; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 168 | enum tshut_mode tshut_mode; |
| 169 | enum tshut_polarity tshut_polarity; |
| 170 | }; |
| 171 | |
Caesar Wang | 952418a | 2016-02-15 15:33:30 +0800 | [diff] [blame] | 172 | /** |
| 173 | * TSADC Sensor Register description: |
| 174 | * |
| 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) |
| 177 | * |
| 178 | */ |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 179 | #define TSADCV2_USER_CON 0x00 |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 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) |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 184 | #define TSADCV2_COMP_INT(chn) (0x30 + (chn) * 0x04) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 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 |
| 190 | |
| 191 | #define TSADCV2_AUTO_EN BIT(0) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 192 | #define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn)) |
| 193 | #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8) |
Caesar Wang | 678065d | 2016-04-18 11:35:58 +0800 | [diff] [blame] | 194 | |
Caesar Wang | 7ea38c6 | 2016-02-15 15:33:31 +0800 | [diff] [blame] | 195 | #define TSADCV3_AUTO_Q_SEL_EN BIT(1) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 196 | |
| 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)) |
| 200 | |
Dmitry Torokhov | 452e01b | 2015-08-07 14:00:52 -0700 | [diff] [blame] | 201 | #define TSADCV2_INT_PD_CLEAR_MASK ~BIT(8) |
Caesar Wang | 952418a | 2016-02-15 15:33:30 +0800 | [diff] [blame] | 202 | #define TSADCV3_INT_PD_CLEAR_MASK ~BIT(16) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 203 | |
| 204 | #define TSADCV2_DATA_MASK 0xfff |
Caesar Wang | 20f0af7 | 2015-11-09 12:48:59 +0800 | [diff] [blame] | 205 | #define TSADCV3_DATA_MASK 0x3ff |
| 206 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 207 | #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT 4 |
| 208 | #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT 4 |
Caesar Wang | 4666787 | 2016-06-22 18:13:56 +0800 | [diff] [blame] | 209 | #define TSADCV2_AUTO_PERIOD_TIME 250 /* 250ms */ |
| 210 | #define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* 50ms */ |
Rocky Hao | 5ef62de | 2016-07-27 22:10:43 +0800 | [diff] [blame] | 211 | #define TSADCV3_AUTO_PERIOD_TIME 1875 /* 2.5ms */ |
| 212 | #define TSADCV3_AUTO_PERIOD_HT_TIME 1875 /* 2.5ms */ |
Caesar Wang | 4666787 | 2016-06-22 18:13:56 +0800 | [diff] [blame] | 213 | |
Finley Xiao | 16bee04 | 2021-05-06 14:55:14 -0300 | [diff] [blame] | 214 | #define TSADCV5_AUTO_PERIOD_TIME 1622 /* 2.5ms */ |
| 215 | #define TSADCV5_AUTO_PERIOD_HT_TIME 1622 /* 2.5ms */ |
| 216 | |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 217 | #define TSADCV2_USER_INTER_PD_SOC 0x340 /* 13 clocks */ |
Finley Xiao | 16bee04 | 2021-05-06 14:55:14 -0300 | [diff] [blame] | 218 | #define TSADCV5_USER_INTER_PD_SOC 0xfc0 /* 97us, at least 90us */ |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 219 | |
| 220 | #define GRF_SARADC_TESTBIT 0x0e644 |
| 221 | #define GRF_TSADC_TESTBIT_L 0x0e648 |
| 222 | #define GRF_TSADC_TESTBIT_H 0x0e64c |
| 223 | |
Elaine Zhang | ffd1b12 | 2019-04-30 18:09:46 +0800 | [diff] [blame] | 224 | #define PX30_GRF_SOC_CON2 0x0408 |
| 225 | |
Finley Xiao | 16bee04 | 2021-05-06 14:55:14 -0300 | [diff] [blame] | 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) |
| 231 | |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 232 | #define GRF_SARADC_TESTBIT_ON (0x10001 << 2) |
| 233 | #define GRF_TSADC_TESTBIT_H_ON (0x10001 << 2) |
Rocky Hao | 23f75e4 | 2016-07-27 22:10:42 +0800 | [diff] [blame] | 234 | #define GRF_TSADC_VCM_EN_L (0x10001 << 7) |
| 235 | #define GRF_TSADC_VCM_EN_H (0x10001 << 7) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 236 | |
Elaine Zhang | ffd1b12 | 2019-04-30 18:09:46 +0800 | [diff] [blame] | 237 | #define GRF_CON_TSADC_CH_INV (0x10001 << 1) |
| 238 | |
Caesar Wang | 678065d | 2016-04-18 11:35:58 +0800 | [diff] [blame] | 239 | /** |
| 240 | * struct tsadc_table - code to temperature conversion table |
| 241 | * @code: the value of adc channel |
| 242 | * @temp: the temperature |
| 243 | * Note: |
| 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. |
| 248 | */ |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 249 | struct tsadc_table { |
Dmitry Torokhov | d9a241c | 2015-08-07 13:59:23 -0700 | [diff] [blame] | 250 | u32 code; |
Caesar Wang | 437df21 | 2015-11-09 12:48:58 +0800 | [diff] [blame] | 251 | int temp; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 252 | }; |
| 253 | |
Rocky Hao | 4eca8ca | 2017-08-24 18:27:52 +0800 | [diff] [blame] | 254 | static const struct tsadc_table rv1108_table[] = { |
| 255 | {0, -40000}, |
| 256 | {374, -40000}, |
| 257 | {382, -35000}, |
| 258 | {389, -30000}, |
| 259 | {397, -25000}, |
| 260 | {405, -20000}, |
| 261 | {413, -15000}, |
| 262 | {421, -10000}, |
| 263 | {429, -5000}, |
| 264 | {436, 0}, |
| 265 | {444, 5000}, |
| 266 | {452, 10000}, |
| 267 | {460, 15000}, |
| 268 | {468, 20000}, |
| 269 | {476, 25000}, |
| 270 | {483, 30000}, |
| 271 | {491, 35000}, |
| 272 | {499, 40000}, |
| 273 | {507, 45000}, |
| 274 | {515, 50000}, |
| 275 | {523, 55000}, |
| 276 | {531, 60000}, |
| 277 | {539, 65000}, |
| 278 | {547, 70000}, |
| 279 | {555, 75000}, |
| 280 | {562, 80000}, |
| 281 | {570, 85000}, |
| 282 | {578, 90000}, |
| 283 | {586, 95000}, |
| 284 | {594, 100000}, |
| 285 | {602, 105000}, |
| 286 | {610, 110000}, |
| 287 | {618, 115000}, |
| 288 | {626, 120000}, |
| 289 | {634, 125000}, |
| 290 | {TSADCV2_DATA_MASK, 125000}, |
| 291 | }; |
| 292 | |
Caesar Wang | 952418a | 2016-02-15 15:33:30 +0800 | [diff] [blame] | 293 | static const struct tsadc_table rk3228_code_table[] = { |
Caesar Wang | 7ea38c6 | 2016-02-15 15:33:31 +0800 | [diff] [blame] | 294 | {0, -40000}, |
| 295 | {588, -40000}, |
| 296 | {593, -35000}, |
| 297 | {598, -30000}, |
| 298 | {603, -25000}, |
| 299 | {608, -20000}, |
| 300 | {613, -15000}, |
| 301 | {618, -10000}, |
| 302 | {623, -5000}, |
| 303 | {629, 0}, |
| 304 | {634, 5000}, |
| 305 | {639, 10000}, |
| 306 | {644, 15000}, |
| 307 | {649, 20000}, |
| 308 | {654, 25000}, |
| 309 | {660, 30000}, |
| 310 | {665, 35000}, |
| 311 | {670, 40000}, |
| 312 | {675, 45000}, |
| 313 | {681, 50000}, |
| 314 | {686, 55000}, |
| 315 | {691, 60000}, |
| 316 | {696, 65000}, |
| 317 | {702, 70000}, |
| 318 | {707, 75000}, |
| 319 | {712, 80000}, |
| 320 | {717, 85000}, |
| 321 | {723, 90000}, |
| 322 | {728, 95000}, |
| 323 | {733, 100000}, |
| 324 | {738, 105000}, |
| 325 | {744, 110000}, |
| 326 | {749, 115000}, |
| 327 | {754, 120000}, |
| 328 | {760, 125000}, |
| 329 | {TSADCV2_DATA_MASK, 125000}, |
Caesar Wang | 7b02a5e | 2015-12-03 16:48:42 +0800 | [diff] [blame] | 330 | }; |
| 331 | |
Caesar Wang | 952418a | 2016-02-15 15:33:30 +0800 | [diff] [blame] | 332 | static const struct tsadc_table rk3288_code_table[] = { |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 333 | {TSADCV2_DATA_MASK, -40000}, |
| 334 | {3800, -40000}, |
| 335 | {3792, -35000}, |
| 336 | {3783, -30000}, |
| 337 | {3774, -25000}, |
| 338 | {3765, -20000}, |
| 339 | {3756, -15000}, |
| 340 | {3747, -10000}, |
| 341 | {3737, -5000}, |
| 342 | {3728, 0}, |
| 343 | {3718, 5000}, |
| 344 | {3708, 10000}, |
| 345 | {3698, 15000}, |
| 346 | {3688, 20000}, |
| 347 | {3678, 25000}, |
| 348 | {3667, 30000}, |
| 349 | {3656, 35000}, |
| 350 | {3645, 40000}, |
| 351 | {3634, 45000}, |
| 352 | {3623, 50000}, |
| 353 | {3611, 55000}, |
| 354 | {3600, 60000}, |
| 355 | {3588, 65000}, |
| 356 | {3575, 70000}, |
| 357 | {3563, 75000}, |
| 358 | {3550, 80000}, |
| 359 | {3537, 85000}, |
| 360 | {3524, 90000}, |
| 361 | {3510, 95000}, |
| 362 | {3496, 100000}, |
| 363 | {3482, 105000}, |
| 364 | {3467, 110000}, |
| 365 | {3452, 115000}, |
| 366 | {3437, 120000}, |
| 367 | {3421, 125000}, |
Caesar Wang | cadf29d | 2016-12-12 19:05:34 +0800 | [diff] [blame] | 368 | {0, 125000}, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 369 | }; |
| 370 | |
Rocky Hao | eda519d | 2017-08-04 16:06:13 +0800 | [diff] [blame] | 371 | static const struct tsadc_table rk3328_code_table[] = { |
| 372 | {0, -40000}, |
| 373 | {296, -40000}, |
| 374 | {304, -35000}, |
| 375 | {313, -30000}, |
| 376 | {331, -20000}, |
| 377 | {340, -15000}, |
| 378 | {349, -10000}, |
| 379 | {359, -5000}, |
| 380 | {368, 0}, |
| 381 | {378, 5000}, |
| 382 | {388, 10000}, |
| 383 | {398, 15000}, |
| 384 | {408, 20000}, |
| 385 | {418, 25000}, |
| 386 | {429, 30000}, |
| 387 | {440, 35000}, |
| 388 | {451, 40000}, |
| 389 | {462, 45000}, |
| 390 | {473, 50000}, |
| 391 | {485, 55000}, |
| 392 | {496, 60000}, |
| 393 | {508, 65000}, |
| 394 | {521, 70000}, |
| 395 | {533, 75000}, |
| 396 | {546, 80000}, |
| 397 | {559, 85000}, |
| 398 | {572, 90000}, |
| 399 | {586, 95000}, |
| 400 | {600, 100000}, |
| 401 | {614, 105000}, |
| 402 | {629, 110000}, |
| 403 | {644, 115000}, |
| 404 | {659, 120000}, |
| 405 | {675, 125000}, |
| 406 | {TSADCV2_DATA_MASK, 125000}, |
| 407 | }; |
| 408 | |
Caesar Wang | 952418a | 2016-02-15 15:33:30 +0800 | [diff] [blame] | 409 | static const struct tsadc_table rk3368_code_table[] = { |
Caesar Wang | 20f0af7 | 2015-11-09 12:48:59 +0800 | [diff] [blame] | 410 | {0, -40000}, |
| 411 | {106, -40000}, |
| 412 | {108, -35000}, |
| 413 | {110, -30000}, |
| 414 | {112, -25000}, |
| 415 | {114, -20000}, |
| 416 | {116, -15000}, |
| 417 | {118, -10000}, |
| 418 | {120, -5000}, |
| 419 | {122, 0}, |
| 420 | {124, 5000}, |
| 421 | {126, 10000}, |
| 422 | {128, 15000}, |
| 423 | {130, 20000}, |
| 424 | {132, 25000}, |
| 425 | {134, 30000}, |
| 426 | {136, 35000}, |
| 427 | {138, 40000}, |
| 428 | {140, 45000}, |
| 429 | {142, 50000}, |
| 430 | {144, 55000}, |
| 431 | {146, 60000}, |
| 432 | {148, 65000}, |
| 433 | {150, 70000}, |
| 434 | {152, 75000}, |
| 435 | {154, 80000}, |
| 436 | {156, 85000}, |
| 437 | {158, 90000}, |
| 438 | {160, 95000}, |
| 439 | {162, 100000}, |
| 440 | {163, 105000}, |
| 441 | {165, 110000}, |
| 442 | {167, 115000}, |
| 443 | {169, 120000}, |
| 444 | {171, 125000}, |
| 445 | {TSADCV3_DATA_MASK, 125000}, |
| 446 | }; |
| 447 | |
Caesar Wang | 952418a | 2016-02-15 15:33:30 +0800 | [diff] [blame] | 448 | static const struct tsadc_table rk3399_code_table[] = { |
Caesar Wang | 7ea38c6 | 2016-02-15 15:33:31 +0800 | [diff] [blame] | 449 | {0, -40000}, |
Caesar Wang | f762a35 | 2016-04-18 11:35:55 +0800 | [diff] [blame] | 450 | {402, -40000}, |
| 451 | {410, -35000}, |
| 452 | {419, -30000}, |
| 453 | {427, -25000}, |
| 454 | {436, -20000}, |
| 455 | {444, -15000}, |
| 456 | {453, -10000}, |
| 457 | {461, -5000}, |
| 458 | {470, 0}, |
| 459 | {478, 5000}, |
| 460 | {487, 10000}, |
| 461 | {496, 15000}, |
| 462 | {504, 20000}, |
| 463 | {513, 25000}, |
| 464 | {521, 30000}, |
| 465 | {530, 35000}, |
| 466 | {538, 40000}, |
| 467 | {547, 45000}, |
| 468 | {555, 50000}, |
| 469 | {564, 55000}, |
| 470 | {573, 60000}, |
| 471 | {581, 65000}, |
| 472 | {590, 70000}, |
| 473 | {599, 75000}, |
| 474 | {607, 80000}, |
| 475 | {616, 85000}, |
| 476 | {624, 90000}, |
| 477 | {633, 95000}, |
| 478 | {642, 100000}, |
| 479 | {650, 105000}, |
| 480 | {659, 110000}, |
| 481 | {668, 115000}, |
| 482 | {677, 120000}, |
| 483 | {685, 125000}, |
Caesar Wang | 7ea38c6 | 2016-02-15 15:33:31 +0800 | [diff] [blame] | 484 | {TSADCV3_DATA_MASK, 125000}, |
Caesar Wang | b0d7033 | 2015-12-03 16:48:43 +0800 | [diff] [blame] | 485 | }; |
| 486 | |
Finley Xiao | 16bee04 | 2021-05-06 14:55:14 -0300 | [diff] [blame] | 487 | static const struct tsadc_table rk3568_code_table[] = { |
| 488 | {0, -40000}, |
| 489 | {1584, -40000}, |
| 490 | {1620, -35000}, |
| 491 | {1652, -30000}, |
| 492 | {1688, -25000}, |
| 493 | {1720, -20000}, |
| 494 | {1756, -15000}, |
| 495 | {1788, -10000}, |
| 496 | {1824, -5000}, |
| 497 | {1856, 0}, |
| 498 | {1892, 5000}, |
| 499 | {1924, 10000}, |
| 500 | {1956, 15000}, |
| 501 | {1992, 20000}, |
| 502 | {2024, 25000}, |
| 503 | {2060, 30000}, |
| 504 | {2092, 35000}, |
| 505 | {2128, 40000}, |
| 506 | {2160, 45000}, |
| 507 | {2196, 50000}, |
| 508 | {2228, 55000}, |
| 509 | {2264, 60000}, |
| 510 | {2300, 65000}, |
| 511 | {2332, 70000}, |
| 512 | {2368, 75000}, |
| 513 | {2400, 80000}, |
| 514 | {2436, 85000}, |
| 515 | {2468, 90000}, |
| 516 | {2500, 95000}, |
| 517 | {2536, 100000}, |
| 518 | {2572, 105000}, |
| 519 | {2604, 110000}, |
| 520 | {2636, 115000}, |
| 521 | {2672, 120000}, |
| 522 | {2704, 125000}, |
| 523 | {TSADCV2_DATA_MASK, 125000}, |
| 524 | }; |
| 525 | |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 526 | static u32 rk_tsadcv2_temp_to_code(const struct chip_tsadc_table *table, |
Caesar Wang | 437df21 | 2015-11-09 12:48:58 +0800 | [diff] [blame] | 527 | int temp) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 528 | { |
| 529 | int high, low, mid; |
Caesar Wang | cadf29d | 2016-12-12 19:05:34 +0800 | [diff] [blame] | 530 | unsigned long num; |
| 531 | unsigned int denom; |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 532 | u32 error = table->data_mask; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 533 | |
| 534 | low = 0; |
Caesar Wang | cadf29d | 2016-12-12 19:05:34 +0800 | [diff] [blame] | 535 | high = (table->length - 1) - 1; /* ignore the last check for table */ |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 536 | mid = (high + low) / 2; |
| 537 | |
Caesar Wang | 1f09ba8 | 2016-06-22 18:13:57 +0800 | [diff] [blame] | 538 | /* Return mask code data when the temp is over table range */ |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 539 | if (temp < table->id[low].temp || temp > table->id[high].temp) |
Caesar Wang | 1f09ba8 | 2016-06-22 18:13:57 +0800 | [diff] [blame] | 540 | goto exit; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 541 | |
| 542 | while (low <= high) { |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 543 | if (temp == table->id[mid].temp) |
| 544 | return table->id[mid].code; |
| 545 | else if (temp < table->id[mid].temp) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 546 | high = mid - 1; |
| 547 | else |
| 548 | low = mid + 1; |
| 549 | mid = (low + high) / 2; |
| 550 | } |
| 551 | |
Caesar Wang | cadf29d | 2016-12-12 19:05:34 +0800 | [diff] [blame] | 552 | /* |
| 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. |
| 557 | */ |
| 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; |
| 561 | |
| 562 | switch (table->mode) { |
| 563 | case ADC_DECREMENT: |
| 564 | return table->id[mid].code - (num / denom); |
| 565 | case ADC_INCREMENT: |
| 566 | return table->id[mid].code + (num / denom); |
| 567 | default: |
| 568 | pr_err("%s: unknown table mode: %d\n", __func__, table->mode); |
| 569 | return error; |
| 570 | } |
| 571 | |
Caesar Wang | 1f09ba8 | 2016-06-22 18:13:57 +0800 | [diff] [blame] | 572 | exit: |
Brian Norris | e6ed1b4 | 2016-12-12 19:05:31 +0800 | [diff] [blame] | 573 | pr_err("%s: invalid temperature, temp=%d error=%d\n", |
| 574 | __func__, temp, error); |
Caesar Wang | 1f09ba8 | 2016-06-22 18:13:57 +0800 | [diff] [blame] | 575 | return error; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 576 | } |
| 577 | |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 578 | static int rk_tsadcv2_code_to_temp(const struct chip_tsadc_table *table, |
| 579 | u32 code, int *temp) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 580 | { |
Dmitry Torokhov | d9a241c | 2015-08-07 13:59:23 -0700 | [diff] [blame] | 581 | unsigned int low = 1; |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 582 | unsigned int high = table->length - 1; |
Caesar Wang | 1e9a1aea | 2015-01-25 10:11:11 +0800 | [diff] [blame] | 583 | unsigned int mid = (low + high) / 2; |
| 584 | unsigned int num; |
| 585 | unsigned long denom; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 586 | |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 587 | WARN_ON(table->length < 2); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 588 | |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 589 | switch (table->mode) { |
Caesar Wang | 020ba95 | 2015-11-09 12:48:57 +0800 | [diff] [blame] | 590 | case ADC_DECREMENT: |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 591 | code &= table->data_mask; |
Caesar Wang | db83188 | 2017-01-05 12:37:55 +0800 | [diff] [blame] | 592 | if (code <= table->id[high].code) |
Caesar Wang | 020ba95 | 2015-11-09 12:48:57 +0800 | [diff] [blame] | 593 | return -EAGAIN; /* Incorrect reading */ |
Dmitry Torokhov | d9a241c | 2015-08-07 13:59:23 -0700 | [diff] [blame] | 594 | |
Caesar Wang | 020ba95 | 2015-11-09 12:48:57 +0800 | [diff] [blame] | 595 | while (low <= high) { |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 596 | if (code >= table->id[mid].code && |
| 597 | code < table->id[mid - 1].code) |
Caesar Wang | 020ba95 | 2015-11-09 12:48:57 +0800 | [diff] [blame] | 598 | break; |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 599 | else if (code < table->id[mid].code) |
Caesar Wang | 020ba95 | 2015-11-09 12:48:57 +0800 | [diff] [blame] | 600 | low = mid + 1; |
| 601 | else |
| 602 | high = mid - 1; |
| 603 | |
| 604 | mid = (low + high) / 2; |
| 605 | } |
| 606 | break; |
| 607 | case ADC_INCREMENT: |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 608 | code &= table->data_mask; |
| 609 | if (code < table->id[low].code) |
Caesar Wang | 020ba95 | 2015-11-09 12:48:57 +0800 | [diff] [blame] | 610 | return -EAGAIN; /* Incorrect reading */ |
| 611 | |
| 612 | while (low <= high) { |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 613 | if (code <= table->id[mid].code && |
| 614 | code > table->id[mid - 1].code) |
Caesar Wang | 020ba95 | 2015-11-09 12:48:57 +0800 | [diff] [blame] | 615 | break; |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 616 | else if (code > table->id[mid].code) |
Caesar Wang | 020ba95 | 2015-11-09 12:48:57 +0800 | [diff] [blame] | 617 | low = mid + 1; |
| 618 | else |
| 619 | high = mid - 1; |
| 620 | |
| 621 | mid = (low + high) / 2; |
| 622 | } |
| 623 | break; |
| 624 | default: |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 625 | pr_err("%s: unknown table mode: %d\n", __func__, table->mode); |
Brian Norris | e6ed1b4 | 2016-12-12 19:05:31 +0800 | [diff] [blame] | 626 | return -EINVAL; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 627 | } |
| 628 | |
Caesar Wang | 1e9a1aea | 2015-01-25 10:11:11 +0800 | [diff] [blame] | 629 | /* |
| 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. |
| 634 | */ |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 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); |
Dmitry Torokhov | d9a241c | 2015-08-07 13:59:23 -0700 | [diff] [blame] | 639 | |
| 640 | return 0; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /** |
Caesar Wang | 144c556 | 2015-11-05 13:17:59 +0800 | [diff] [blame] | 644 | * rk_tsadcv2_initialize - initialize TASDC Controller. |
Amit Kucheria | 66ec4bf | 2019-11-20 21:15:16 +0530 | [diff] [blame] | 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) |
Caesar Wang | 144c556 | 2015-11-05 13:17:59 +0800 | [diff] [blame] | 648 | * |
| 649 | * (1) Set TSADC_V2_AUTO_PERIOD: |
| 650 | * Configure the interleave between every two accessing of |
| 651 | * TSADC in normal operation. |
| 652 | * |
| 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. |
| 656 | * |
| 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. |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 660 | */ |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 661 | static void rk_tsadcv2_initialize(struct regmap *grf, void __iomem *regs, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 662 | enum tshut_polarity tshut_polarity) |
| 663 | { |
| 664 | if (tshut_polarity == TSHUT_HIGH_ACTIVE) |
Dmitry Torokhov | 452e01b | 2015-08-07 14:00:52 -0700 | [diff] [blame] | 665 | writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 666 | regs + TSADCV2_AUTO_CON); |
| 667 | else |
Dmitry Torokhov | 452e01b | 2015-08-07 14:00:52 -0700 | [diff] [blame] | 668 | writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 669 | regs + TSADCV2_AUTO_CON); |
| 670 | |
| 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); |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /** |
| 681 | * rk_tsadcv3_initialize - initialize TASDC Controller. |
Amit Kucheria | 66ec4bf | 2019-11-20 21:15:16 +0530 | [diff] [blame] | 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) |
Caesar Wang | 678065d | 2016-04-18 11:35:58 +0800 | [diff] [blame] | 685 | * |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 686 | * (1) The tsadc control power sequence. |
| 687 | * |
| 688 | * (2) Set TSADC_V2_AUTO_PERIOD: |
| 689 | * Configure the interleave between every two accessing of |
| 690 | * TSADC in normal operation. |
| 691 | * |
| 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. |
| 695 | * |
| 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. |
| 699 | */ |
| 700 | static void rk_tsadcv3_initialize(struct regmap *grf, void __iomem *regs, |
| 701 | enum tshut_polarity tshut_polarity) |
| 702 | { |
| 703 | /* The tsadc control power sequence */ |
| 704 | if (IS_ERR(grf)) { |
| 705 | /* Set interleave value to workround ic time sync issue */ |
| 706 | writel_relaxed(TSADCV2_USER_INTER_PD_SOC, regs + |
| 707 | TSADCV2_USER_CON); |
Caesar Wang | 4666787 | 2016-06-22 18:13:56 +0800 | [diff] [blame] | 708 | |
| 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); |
| 717 | |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 718 | } else { |
Rocky Hao | 23f75e4 | 2016-07-27 22:10:42 +0800 | [diff] [blame] | 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); |
| 722 | |
Caesar Wang | 2fe5c1b | 2016-05-03 10:23:50 +0800 | [diff] [blame] | 723 | usleep_range(15, 100); /* The spec note says at least 15 us */ |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 724 | regmap_write(grf, GRF_SARADC_TESTBIT, GRF_SARADC_TESTBIT_ON); |
| 725 | regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_TESTBIT_H_ON); |
Caesar Wang | 2fe5c1b | 2016-05-03 10:23:50 +0800 | [diff] [blame] | 726 | usleep_range(90, 200); /* The spec note says at least 90 us */ |
Caesar Wang | 4666787 | 2016-06-22 18:13:56 +0800 | [diff] [blame] | 727 | |
| 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); |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | if (tshut_polarity == TSHUT_HIGH_ACTIVE) |
| 739 | writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH, |
| 740 | regs + TSADCV2_AUTO_CON); |
| 741 | else |
| 742 | writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH, |
| 743 | regs + TSADCV2_AUTO_CON); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 744 | } |
| 745 | |
Elaine Zhang | ffd1b12 | 2019-04-30 18:09:46 +0800 | [diff] [blame] | 746 | static void rk_tsadcv4_initialize(struct regmap *grf, void __iomem *regs, |
| 747 | enum tshut_polarity tshut_polarity) |
| 748 | { |
| 749 | rk_tsadcv2_initialize(grf, regs, tshut_polarity); |
| 750 | regmap_write(grf, PX30_GRF_SOC_CON2, GRF_CON_TSADC_CH_INV); |
| 751 | } |
| 752 | |
Finley Xiao | 16bee04 | 2021-05-06 14:55:14 -0300 | [diff] [blame] | 753 | static void rk_tsadcv7_initialize(struct regmap *grf, void __iomem *regs, |
| 754 | enum tshut_polarity tshut_polarity) |
| 755 | { |
| 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); |
| 764 | |
| 765 | if (tshut_polarity == TSHUT_HIGH_ACTIVE) |
| 766 | writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH, |
| 767 | regs + TSADCV2_AUTO_CON); |
| 768 | else |
| 769 | writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH, |
| 770 | regs + TSADCV2_AUTO_CON); |
| 771 | |
| 772 | /* |
| 773 | * The general register file will is optional |
| 774 | * and might not be available. |
| 775 | */ |
| 776 | if (!IS_ERR(grf)) { |
| 777 | regmap_write(grf, RK3568_GRF_TSADC_CON, RK3568_GRF_TSADC_TSEN); |
| 778 | /* |
| 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. |
| 782 | */ |
| 783 | udelay(15); |
| 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); |
| 787 | |
| 788 | /* |
| 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. |
| 791 | */ |
| 792 | usleep_range(100, 200); |
| 793 | } |
| 794 | } |
| 795 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 796 | static void rk_tsadcv2_irq_ack(void __iomem *regs) |
| 797 | { |
| 798 | u32 val; |
| 799 | |
| 800 | val = readl_relaxed(regs + TSADCV2_INT_PD); |
Dmitry Torokhov | 452e01b | 2015-08-07 14:00:52 -0700 | [diff] [blame] | 801 | writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 802 | } |
| 803 | |
Caesar Wang | 952418a | 2016-02-15 15:33:30 +0800 | [diff] [blame] | 804 | static void rk_tsadcv3_irq_ack(void __iomem *regs) |
| 805 | { |
| 806 | u32 val; |
| 807 | |
| 808 | val = readl_relaxed(regs + TSADCV2_INT_PD); |
| 809 | writel_relaxed(val & TSADCV3_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD); |
| 810 | } |
| 811 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 812 | static void rk_tsadcv2_control(void __iomem *regs, bool enable) |
| 813 | { |
| 814 | u32 val; |
| 815 | |
| 816 | val = readl_relaxed(regs + TSADCV2_AUTO_CON); |
| 817 | if (enable) |
| 818 | val |= TSADCV2_AUTO_EN; |
| 819 | else |
| 820 | val &= ~TSADCV2_AUTO_EN; |
| 821 | |
| 822 | writel_relaxed(val, regs + TSADCV2_AUTO_CON); |
| 823 | } |
| 824 | |
Caesar Wang | 7ea38c6 | 2016-02-15 15:33:31 +0800 | [diff] [blame] | 825 | /** |
Caesar Wang | 678065d | 2016-04-18 11:35:58 +0800 | [diff] [blame] | 826 | * rk_tsadcv3_control - the tsadc controller is enabled or disabled. |
Amit Kucheria | 66ec4bf | 2019-11-20 21:15:16 +0530 | [diff] [blame] | 827 | * @regs: the base address of tsadc controller |
| 828 | * @enable: boolean flag to enable the controller |
Caesar Wang | 678065d | 2016-04-18 11:35:58 +0800 | [diff] [blame] | 829 | * |
| 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. |
Caesar Wang | 7ea38c6 | 2016-02-15 15:33:31 +0800 | [diff] [blame] | 833 | */ |
| 834 | static void rk_tsadcv3_control(void __iomem *regs, bool enable) |
| 835 | { |
| 836 | u32 val; |
| 837 | |
| 838 | val = readl_relaxed(regs + TSADCV2_AUTO_CON); |
| 839 | if (enable) |
| 840 | val |= TSADCV2_AUTO_EN | TSADCV3_AUTO_Q_SEL_EN; |
| 841 | else |
| 842 | val &= ~TSADCV2_AUTO_EN; |
| 843 | |
| 844 | writel_relaxed(val, regs + TSADCV2_AUTO_CON); |
| 845 | } |
| 846 | |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 847 | static int rk_tsadcv2_get_temp(const struct chip_tsadc_table *table, |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 848 | int chn, void __iomem *regs, int *temp) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 849 | { |
| 850 | u32 val; |
| 851 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 852 | val = readl_relaxed(regs + TSADCV2_DATA(chn)); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 853 | |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 854 | return rk_tsadcv2_code_to_temp(table, val, temp); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 855 | } |
| 856 | |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 857 | static int rk_tsadcv2_alarm_temp(const struct chip_tsadc_table *table, |
| 858 | int chn, void __iomem *regs, int temp) |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 859 | { |
Caesar Wang | 18591ad | 2016-12-12 19:05:35 +0800 | [diff] [blame] | 860 | u32 alarm_value; |
| 861 | u32 int_en, int_clr; |
| 862 | |
| 863 | /* |
| 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 |
| 867 | * interrupt. |
| 868 | */ |
| 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); |
| 873 | return 0; |
| 874 | } |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 875 | |
Caesar Wang | 1f09ba8 | 2016-06-22 18:13:57 +0800 | [diff] [blame] | 876 | /* Make sure the value is valid */ |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 877 | alarm_value = rk_tsadcv2_temp_to_code(table, temp); |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 878 | if (alarm_value == table->data_mask) |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 879 | return -ERANGE; |
Caesar Wang | 1f09ba8 | 2016-06-22 18:13:57 +0800 | [diff] [blame] | 880 | |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 881 | writel_relaxed(alarm_value & table->data_mask, |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 882 | regs + TSADCV2_COMP_INT(chn)); |
| 883 | |
| 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); |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 887 | |
| 888 | return 0; |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 889 | } |
| 890 | |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 891 | static int rk_tsadcv2_tshut_temp(const struct chip_tsadc_table *table, |
| 892 | int chn, void __iomem *regs, int temp) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 893 | { |
| 894 | u32 tshut_value, val; |
| 895 | |
Caesar Wang | 1f09ba8 | 2016-06-22 18:13:57 +0800 | [diff] [blame] | 896 | /* Make sure the value is valid */ |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 897 | tshut_value = rk_tsadcv2_temp_to_code(table, temp); |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 898 | if (tshut_value == table->data_mask) |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 899 | return -ERANGE; |
Caesar Wang | 1f09ba8 | 2016-06-22 18:13:57 +0800 | [diff] [blame] | 900 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 901 | writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn)); |
| 902 | |
| 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); |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 906 | |
| 907 | return 0; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs, |
| 911 | enum tshut_mode mode) |
| 912 | { |
| 913 | u32 val; |
| 914 | |
| 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); |
| 919 | } else { |
| 920 | val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn); |
| 921 | val |= TSADCV2_SHUT_2CRU_SRC_EN(chn); |
| 922 | } |
| 923 | |
| 924 | writel_relaxed(val, regs + TSADCV2_INT_EN); |
| 925 | } |
| 926 | |
Elaine Zhang | ffd1b12 | 2019-04-30 18:09:46 +0800 | [diff] [blame] | 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 */ |
| 931 | |
| 932 | .tshut_mode = TSHUT_MODE_CRU, /* default TSHUT via CRU */ |
| 933 | .tshut_temp = 95000, |
| 934 | |
| 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, |
| 942 | |
| 943 | .table = { |
| 944 | .id = rk3328_code_table, |
| 945 | .length = ARRAY_SIZE(rk3328_code_table), |
| 946 | .data_mask = TSADCV2_DATA_MASK, |
| 947 | .mode = ADC_INCREMENT, |
| 948 | }, |
| 949 | }; |
| 950 | |
Rocky Hao | 4eca8ca | 2017-08-24 18:27:52 +0800 | [diff] [blame] | 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 */ |
| 954 | |
| 955 | .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */ |
| 956 | .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */ |
| 957 | .tshut_temp = 95000, |
| 958 | |
| 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, |
| 966 | |
| 967 | .table = { |
| 968 | .id = rv1108_table, |
| 969 | .length = ARRAY_SIZE(rv1108_table), |
| 970 | .data_mask = TSADCV2_DATA_MASK, |
| 971 | .mode = ADC_INCREMENT, |
| 972 | }, |
| 973 | }; |
| 974 | |
Caesar Wang | 7b02a5e | 2015-12-03 16:48:42 +0800 | [diff] [blame] | 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 */ |
| 978 | |
| 979 | .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */ |
| 980 | .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */ |
| 981 | .tshut_temp = 95000, |
| 982 | |
| 983 | .initialize = rk_tsadcv2_initialize, |
Caesar Wang | 952418a | 2016-02-15 15:33:30 +0800 | [diff] [blame] | 984 | .irq_ack = rk_tsadcv3_irq_ack, |
Caesar Wang | 7ea38c6 | 2016-02-15 15:33:31 +0800 | [diff] [blame] | 985 | .control = rk_tsadcv3_control, |
Caesar Wang | 7b02a5e | 2015-12-03 16:48:42 +0800 | [diff] [blame] | 986 | .get_temp = rk_tsadcv2_get_temp, |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 987 | .set_alarm_temp = rk_tsadcv2_alarm_temp, |
Caesar Wang | 7b02a5e | 2015-12-03 16:48:42 +0800 | [diff] [blame] | 988 | .set_tshut_temp = rk_tsadcv2_tshut_temp, |
| 989 | .set_tshut_mode = rk_tsadcv2_tshut_mode, |
| 990 | |
| 991 | .table = { |
Caesar Wang | 952418a | 2016-02-15 15:33:30 +0800 | [diff] [blame] | 992 | .id = rk3228_code_table, |
| 993 | .length = ARRAY_SIZE(rk3228_code_table), |
Caesar Wang | 7b02a5e | 2015-12-03 16:48:42 +0800 | [diff] [blame] | 994 | .data_mask = TSADCV3_DATA_MASK, |
Caesar Wang | 7ea38c6 | 2016-02-15 15:33:31 +0800 | [diff] [blame] | 995 | .mode = ADC_INCREMENT, |
Caesar Wang | 7b02a5e | 2015-12-03 16:48:42 +0800 | [diff] [blame] | 996 | }, |
| 997 | }; |
| 998 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 999 | static const struct rockchip_tsadc_chip rk3288_tsadc_data = { |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 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 */ |
| 1003 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 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, |
| 1007 | |
| 1008 | .initialize = rk_tsadcv2_initialize, |
| 1009 | .irq_ack = rk_tsadcv2_irq_ack, |
| 1010 | .control = rk_tsadcv2_control, |
| 1011 | .get_temp = rk_tsadcv2_get_temp, |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 1012 | .set_alarm_temp = rk_tsadcv2_alarm_temp, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1013 | .set_tshut_temp = rk_tsadcv2_tshut_temp, |
| 1014 | .set_tshut_mode = rk_tsadcv2_tshut_mode, |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 1015 | |
| 1016 | .table = { |
Caesar Wang | 952418a | 2016-02-15 15:33:30 +0800 | [diff] [blame] | 1017 | .id = rk3288_code_table, |
| 1018 | .length = ARRAY_SIZE(rk3288_code_table), |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 1019 | .data_mask = TSADCV2_DATA_MASK, |
Caesar Wang | 020ba95 | 2015-11-09 12:48:57 +0800 | [diff] [blame] | 1020 | .mode = ADC_DECREMENT, |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 1021 | }, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1022 | }; |
| 1023 | |
Rocky Hao | eda519d | 2017-08-04 16:06:13 +0800 | [diff] [blame] | 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 */ |
| 1027 | |
| 1028 | .tshut_mode = TSHUT_MODE_CRU, /* default TSHUT via CRU */ |
| 1029 | .tshut_temp = 95000, |
| 1030 | |
| 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, |
| 1038 | |
| 1039 | .table = { |
| 1040 | .id = rk3328_code_table, |
| 1041 | .length = ARRAY_SIZE(rk3328_code_table), |
| 1042 | .data_mask = TSADCV2_DATA_MASK, |
| 1043 | .mode = ADC_INCREMENT, |
| 1044 | }, |
| 1045 | }; |
| 1046 | |
Elaine Zhang | 1cd60269 | 2016-04-18 11:35:57 +0800 | [diff] [blame] | 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 */ |
| 1051 | |
| 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, |
| 1055 | |
| 1056 | .initialize = rk_tsadcv3_initialize, |
| 1057 | .irq_ack = rk_tsadcv3_irq_ack, |
| 1058 | .control = rk_tsadcv3_control, |
| 1059 | .get_temp = rk_tsadcv2_get_temp, |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 1060 | .set_alarm_temp = rk_tsadcv2_alarm_temp, |
Elaine Zhang | 1cd60269 | 2016-04-18 11:35:57 +0800 | [diff] [blame] | 1061 | .set_tshut_temp = rk_tsadcv2_tshut_temp, |
| 1062 | .set_tshut_mode = rk_tsadcv2_tshut_mode, |
| 1063 | |
| 1064 | .table = { |
| 1065 | .id = rk3228_code_table, |
| 1066 | .length = ARRAY_SIZE(rk3228_code_table), |
| 1067 | .data_mask = TSADCV3_DATA_MASK, |
| 1068 | .mode = ADC_INCREMENT, |
| 1069 | }, |
| 1070 | }; |
| 1071 | |
Caesar Wang | 20f0af7 | 2015-11-09 12:48:59 +0800 | [diff] [blame] | 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 */ |
| 1076 | |
| 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, |
| 1080 | |
| 1081 | .initialize = rk_tsadcv2_initialize, |
| 1082 | .irq_ack = rk_tsadcv2_irq_ack, |
| 1083 | .control = rk_tsadcv2_control, |
| 1084 | .get_temp = rk_tsadcv2_get_temp, |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 1085 | .set_alarm_temp = rk_tsadcv2_alarm_temp, |
Caesar Wang | 20f0af7 | 2015-11-09 12:48:59 +0800 | [diff] [blame] | 1086 | .set_tshut_temp = rk_tsadcv2_tshut_temp, |
| 1087 | .set_tshut_mode = rk_tsadcv2_tshut_mode, |
| 1088 | |
| 1089 | .table = { |
Caesar Wang | 952418a | 2016-02-15 15:33:30 +0800 | [diff] [blame] | 1090 | .id = rk3368_code_table, |
| 1091 | .length = ARRAY_SIZE(rk3368_code_table), |
Caesar Wang | 20f0af7 | 2015-11-09 12:48:59 +0800 | [diff] [blame] | 1092 | .data_mask = TSADCV3_DATA_MASK, |
| 1093 | .mode = ADC_INCREMENT, |
| 1094 | }, |
| 1095 | }; |
| 1096 | |
Caesar Wang | b0d7033 | 2015-12-03 16:48:43 +0800 | [diff] [blame] | 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 */ |
| 1101 | |
| 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, |
| 1105 | |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 1106 | .initialize = rk_tsadcv3_initialize, |
Caesar Wang | 952418a | 2016-02-15 15:33:30 +0800 | [diff] [blame] | 1107 | .irq_ack = rk_tsadcv3_irq_ack, |
Caesar Wang | 7ea38c6 | 2016-02-15 15:33:31 +0800 | [diff] [blame] | 1108 | .control = rk_tsadcv3_control, |
Caesar Wang | b0d7033 | 2015-12-03 16:48:43 +0800 | [diff] [blame] | 1109 | .get_temp = rk_tsadcv2_get_temp, |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 1110 | .set_alarm_temp = rk_tsadcv2_alarm_temp, |
Caesar Wang | b0d7033 | 2015-12-03 16:48:43 +0800 | [diff] [blame] | 1111 | .set_tshut_temp = rk_tsadcv2_tshut_temp, |
| 1112 | .set_tshut_mode = rk_tsadcv2_tshut_mode, |
| 1113 | |
| 1114 | .table = { |
Caesar Wang | 952418a | 2016-02-15 15:33:30 +0800 | [diff] [blame] | 1115 | .id = rk3399_code_table, |
| 1116 | .length = ARRAY_SIZE(rk3399_code_table), |
Caesar Wang | b0d7033 | 2015-12-03 16:48:43 +0800 | [diff] [blame] | 1117 | .data_mask = TSADCV3_DATA_MASK, |
Caesar Wang | 7ea38c6 | 2016-02-15 15:33:31 +0800 | [diff] [blame] | 1118 | .mode = ADC_INCREMENT, |
Caesar Wang | b0d7033 | 2015-12-03 16:48:43 +0800 | [diff] [blame] | 1119 | }, |
| 1120 | }; |
| 1121 | |
Finley Xiao | 16bee04 | 2021-05-06 14:55:14 -0300 | [diff] [blame] | 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 */ |
| 1126 | |
| 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, |
| 1130 | |
| 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, |
| 1138 | |
| 1139 | .table = { |
| 1140 | .id = rk3568_code_table, |
| 1141 | .length = ARRAY_SIZE(rk3568_code_table), |
| 1142 | .data_mask = TSADCV2_DATA_MASK, |
| 1143 | .mode = ADC_INCREMENT, |
| 1144 | }, |
| 1145 | }; |
| 1146 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1147 | static const struct of_device_id of_rockchip_thermal_match[] = { |
Elaine Zhang | ffd1b12 | 2019-04-30 18:09:46 +0800 | [diff] [blame] | 1148 | { .compatible = "rockchip,px30-tsadc", |
| 1149 | .data = (void *)&px30_tsadc_data, |
| 1150 | }, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1151 | { |
Rocky Hao | 4eca8ca | 2017-08-24 18:27:52 +0800 | [diff] [blame] | 1152 | .compatible = "rockchip,rv1108-tsadc", |
| 1153 | .data = (void *)&rv1108_tsadc_data, |
| 1154 | }, |
| 1155 | { |
Caesar Wang | 7b02a5e | 2015-12-03 16:48:42 +0800 | [diff] [blame] | 1156 | .compatible = "rockchip,rk3228-tsadc", |
| 1157 | .data = (void *)&rk3228_tsadc_data, |
| 1158 | }, |
| 1159 | { |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1160 | .compatible = "rockchip,rk3288-tsadc", |
| 1161 | .data = (void *)&rk3288_tsadc_data, |
| 1162 | }, |
Caesar Wang | 20f0af7 | 2015-11-09 12:48:59 +0800 | [diff] [blame] | 1163 | { |
Rocky Hao | eda519d | 2017-08-04 16:06:13 +0800 | [diff] [blame] | 1164 | .compatible = "rockchip,rk3328-tsadc", |
| 1165 | .data = (void *)&rk3328_tsadc_data, |
| 1166 | }, |
| 1167 | { |
Elaine Zhang | 1cd60269 | 2016-04-18 11:35:57 +0800 | [diff] [blame] | 1168 | .compatible = "rockchip,rk3366-tsadc", |
| 1169 | .data = (void *)&rk3366_tsadc_data, |
| 1170 | }, |
| 1171 | { |
Caesar Wang | 20f0af7 | 2015-11-09 12:48:59 +0800 | [diff] [blame] | 1172 | .compatible = "rockchip,rk3368-tsadc", |
| 1173 | .data = (void *)&rk3368_tsadc_data, |
| 1174 | }, |
Caesar Wang | b0d7033 | 2015-12-03 16:48:43 +0800 | [diff] [blame] | 1175 | { |
| 1176 | .compatible = "rockchip,rk3399-tsadc", |
| 1177 | .data = (void *)&rk3399_tsadc_data, |
| 1178 | }, |
Finley Xiao | 16bee04 | 2021-05-06 14:55:14 -0300 | [diff] [blame] | 1179 | { |
| 1180 | .compatible = "rockchip,rk3568-tsadc", |
| 1181 | .data = (void *)&rk3568_tsadc_data, |
| 1182 | }, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1183 | { /* end */ }, |
| 1184 | }; |
| 1185 | MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match); |
| 1186 | |
| 1187 | static void |
| 1188 | rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on) |
| 1189 | { |
| 1190 | struct thermal_zone_device *tzd = sensor->tzd; |
| 1191 | |
Andrzej Pietrasiewicz | 7f4957b | 2020-06-29 14:29:21 +0200 | [diff] [blame] | 1192 | if (on) |
| 1193 | thermal_zone_device_enable(tzd); |
| 1194 | else |
| 1195 | thermal_zone_device_disable(tzd); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev) |
| 1199 | { |
| 1200 | struct rockchip_thermal_data *thermal = dev; |
| 1201 | int i; |
| 1202 | |
| 1203 | dev_dbg(&thermal->pdev->dev, "thermal alarm\n"); |
| 1204 | |
| 1205 | thermal->chip->irq_ack(thermal->regs); |
| 1206 | |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 1207 | for (i = 0; i < thermal->chip->chn_num; i++) |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 1208 | thermal_zone_device_update(thermal->sensors[i].tzd, |
| 1209 | THERMAL_EVENT_UNSPECIFIED); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1210 | |
| 1211 | return IRQ_HANDLED; |
| 1212 | } |
| 1213 | |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 1214 | static int rockchip_thermal_set_trips(void *_sensor, int low, int high) |
| 1215 | { |
| 1216 | struct rockchip_thermal_sensor *sensor = _sensor; |
| 1217 | struct rockchip_thermal_data *thermal = sensor->thermal; |
| 1218 | const struct rockchip_tsadc_chip *tsadc = thermal->chip; |
| 1219 | |
| 1220 | dev_dbg(&thermal->pdev->dev, "%s: sensor %d: low: %d, high %d\n", |
| 1221 | __func__, sensor->id, low, high); |
| 1222 | |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 1223 | return tsadc->set_alarm_temp(&tsadc->table, |
| 1224 | sensor->id, thermal->regs, high); |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 1225 | } |
| 1226 | |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 1227 | static int rockchip_thermal_get_temp(void *_sensor, int *out_temp) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1228 | { |
| 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; |
| 1232 | int retval; |
| 1233 | |
Brian Norris | cdd8b3f | 2016-12-12 19:05:32 +0800 | [diff] [blame] | 1234 | retval = tsadc->get_temp(&tsadc->table, |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 1235 | sensor->id, thermal->regs, out_temp); |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 1236 | dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n", |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1237 | sensor->id, *out_temp, retval); |
| 1238 | |
| 1239 | return retval; |
| 1240 | } |
| 1241 | |
| 1242 | static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = { |
| 1243 | .get_temp = rockchip_thermal_get_temp, |
Caesar Wang | 1484850 | 2016-06-22 16:42:05 +0800 | [diff] [blame] | 1244 | .set_trips = rockchip_thermal_set_trips, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1245 | }; |
| 1246 | |
| 1247 | static int rockchip_configure_from_dt(struct device *dev, |
| 1248 | struct device_node *np, |
| 1249 | struct rockchip_thermal_data *thermal) |
| 1250 | { |
| 1251 | u32 shut_temp, tshut_mode, tshut_polarity; |
| 1252 | |
| 1253 | if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) { |
| 1254 | dev_warn(dev, |
Caesar Wang | 437df21 | 2015-11-09 12:48:58 +0800 | [diff] [blame] | 1255 | "Missing tshut temp property, using default %d\n", |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1256 | thermal->chip->tshut_temp); |
| 1257 | thermal->tshut_temp = thermal->chip->tshut_temp; |
| 1258 | } else { |
Caesar Wang | 43b4eb9 | 2016-02-15 15:33:28 +0800 | [diff] [blame] | 1259 | if (shut_temp > INT_MAX) { |
| 1260 | dev_err(dev, "Invalid tshut temperature specified: %d\n", |
| 1261 | shut_temp); |
| 1262 | return -ERANGE; |
| 1263 | } |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1264 | thermal->tshut_temp = shut_temp; |
| 1265 | } |
| 1266 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1267 | if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) { |
| 1268 | dev_warn(dev, |
| 1269 | "Missing tshut mode property, using default (%s)\n", |
| 1270 | thermal->chip->tshut_mode == TSHUT_MODE_GPIO ? |
| 1271 | "gpio" : "cru"); |
| 1272 | thermal->tshut_mode = thermal->chip->tshut_mode; |
| 1273 | } else { |
| 1274 | thermal->tshut_mode = tshut_mode; |
| 1275 | } |
| 1276 | |
| 1277 | if (thermal->tshut_mode > 1) { |
| 1278 | dev_err(dev, "Invalid tshut mode specified: %d\n", |
| 1279 | thermal->tshut_mode); |
| 1280 | return -EINVAL; |
| 1281 | } |
| 1282 | |
| 1283 | if (of_property_read_u32(np, "rockchip,hw-tshut-polarity", |
| 1284 | &tshut_polarity)) { |
| 1285 | dev_warn(dev, |
| 1286 | "Missing tshut-polarity property, using default (%s)\n", |
| 1287 | thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ? |
| 1288 | "low" : "high"); |
| 1289 | thermal->tshut_polarity = thermal->chip->tshut_polarity; |
| 1290 | } else { |
| 1291 | thermal->tshut_polarity = tshut_polarity; |
| 1292 | } |
| 1293 | |
| 1294 | if (thermal->tshut_polarity > 1) { |
| 1295 | dev_err(dev, "Invalid tshut-polarity specified: %d\n", |
| 1296 | thermal->tshut_polarity); |
| 1297 | return -EINVAL; |
| 1298 | } |
| 1299 | |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 1300 | /* The tsadc wont to handle the error in here since some SoCs didn't |
| 1301 | * need this property. |
| 1302 | */ |
| 1303 | thermal->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf"); |
Shawn Lin | ce62aba | 2016-10-11 10:07:34 +0800 | [diff] [blame] | 1304 | if (IS_ERR(thermal->grf)) |
| 1305 | dev_warn(dev, "Missing rockchip,grf property\n"); |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 1306 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | static int |
| 1311 | rockchip_thermal_register_sensor(struct platform_device *pdev, |
| 1312 | struct rockchip_thermal_data *thermal, |
| 1313 | struct rockchip_thermal_sensor *sensor, |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 1314 | int id) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1315 | { |
| 1316 | const struct rockchip_tsadc_chip *tsadc = thermal->chip; |
| 1317 | int error; |
| 1318 | |
| 1319 | tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode); |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 1320 | |
| 1321 | error = tsadc->set_tshut_temp(&tsadc->table, id, thermal->regs, |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 1322 | thermal->tshut_temp); |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 1323 | if (error) |
| 1324 | dev_err(&pdev->dev, "%s: invalid tshut=%d, error=%d\n", |
| 1325 | __func__, thermal->tshut_temp, error); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1326 | |
| 1327 | sensor->thermal = thermal; |
| 1328 | sensor->id = id; |
Eduardo Valentin | 2633ad1 | 2016-03-09 13:10:28 -0800 | [diff] [blame] | 1329 | sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, id, |
| 1330 | sensor, &rockchip_of_thermal_ops); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1331 | if (IS_ERR(sensor->tzd)) { |
| 1332 | error = PTR_ERR(sensor->tzd); |
| 1333 | dev_err(&pdev->dev, "failed to register sensor %d: %d\n", |
| 1334 | id, error); |
| 1335 | return error; |
| 1336 | } |
| 1337 | |
| 1338 | return 0; |
| 1339 | } |
| 1340 | |
Caesar Wang | 13c1cfd | 2015-12-03 16:48:39 +0800 | [diff] [blame] | 1341 | /** |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1342 | * Reset TSADC Controller, reset all tsadc registers. |
Amit Kucheria | 66ec4bf | 2019-11-20 21:15:16 +0530 | [diff] [blame] | 1343 | * @reset: the reset controller of tsadc |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1344 | */ |
| 1345 | static void rockchip_thermal_reset_controller(struct reset_control *reset) |
| 1346 | { |
| 1347 | reset_control_assert(reset); |
| 1348 | usleep_range(10, 20); |
| 1349 | reset_control_deassert(reset); |
| 1350 | } |
| 1351 | |
| 1352 | static int rockchip_thermal_probe(struct platform_device *pdev) |
| 1353 | { |
| 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; |
| 1358 | int irq; |
Eduardo Valentin | 2633ad1 | 2016-03-09 13:10:28 -0800 | [diff] [blame] | 1359 | int i; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1360 | int error; |
| 1361 | |
| 1362 | match = of_match_node(of_rockchip_thermal_match, np); |
| 1363 | if (!match) |
| 1364 | return -ENXIO; |
| 1365 | |
| 1366 | irq = platform_get_irq(pdev, 0); |
Markus Elfring | 8cb775bb | 2020-04-05 18:35:16 +0200 | [diff] [blame] | 1367 | if (irq < 0) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1368 | return -EINVAL; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1369 | |
| 1370 | thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data), |
| 1371 | GFP_KERNEL); |
| 1372 | if (!thermal) |
| 1373 | return -ENOMEM; |
| 1374 | |
| 1375 | thermal->pdev = pdev; |
| 1376 | |
| 1377 | thermal->chip = (const struct rockchip_tsadc_chip *)match->data; |
| 1378 | if (!thermal->chip) |
| 1379 | return -EINVAL; |
| 1380 | |
| 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); |
| 1385 | |
Johan Jonker | 02832ed | 2021-09-30 13:05:16 +0200 | [diff] [blame] | 1386 | thermal->reset = devm_reset_control_array_get(&pdev->dev, false, false); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 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); |
| 1390 | return error; |
| 1391 | } |
| 1392 | |
| 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); |
| 1397 | return error; |
| 1398 | } |
| 1399 | |
| 1400 | thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk"); |
| 1401 | if (IS_ERR(thermal->pclk)) { |
Dan Carpenter | 0d0a2bf | 2015-04-21 12:34:10 +0300 | [diff] [blame] | 1402 | error = PTR_ERR(thermal->pclk); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1403 | dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n", |
| 1404 | error); |
| 1405 | return error; |
| 1406 | } |
| 1407 | |
| 1408 | error = clk_prepare_enable(thermal->clk); |
| 1409 | if (error) { |
| 1410 | dev_err(&pdev->dev, "failed to enable converter clock: %d\n", |
| 1411 | error); |
| 1412 | return error; |
| 1413 | } |
| 1414 | |
| 1415 | error = clk_prepare_enable(thermal->pclk); |
| 1416 | if (error) { |
| 1417 | dev_err(&pdev->dev, "failed to enable pclk: %d\n", error); |
| 1418 | goto err_disable_clk; |
| 1419 | } |
| 1420 | |
| 1421 | rockchip_thermal_reset_controller(thermal->reset); |
| 1422 | |
| 1423 | error = rockchip_configure_from_dt(&pdev->dev, np, thermal); |
| 1424 | if (error) { |
| 1425 | dev_err(&pdev->dev, "failed to parse device tree data: %d\n", |
| 1426 | error); |
| 1427 | goto err_disable_pclk; |
| 1428 | } |
| 1429 | |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 1430 | thermal->chip->initialize(thermal->grf, thermal->regs, |
| 1431 | thermal->tshut_polarity); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1432 | |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 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]); |
| 1437 | if (error) { |
| 1438 | dev_err(&pdev->dev, |
| 1439 | "failed to register sensor[%d] : error = %d\n", |
| 1440 | i, error); |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 1441 | goto err_disable_pclk; |
| 1442 | } |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | error = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
| 1446 | &rockchip_thermal_alarm_irq_thread, |
| 1447 | IRQF_ONESHOT, |
| 1448 | "rockchip_thermal", thermal); |
| 1449 | if (error) { |
| 1450 | dev_err(&pdev->dev, |
| 1451 | "failed to request tsadc irq: %d\n", error); |
Eduardo Valentin | 2633ad1 | 2016-03-09 13:10:28 -0800 | [diff] [blame] | 1452 | goto err_disable_pclk; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | thermal->chip->control(thermal->regs, true); |
| 1456 | |
Stefan Schaeckeler | d27970b | 2019-12-11 22:17:02 -0800 | [diff] [blame] | 1457 | for (i = 0; i < thermal->chip->chn_num; i++) { |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1458 | rockchip_thermal_toggle_sensor(&thermal->sensors[i], true); |
Stefan Schaeckeler | d27970b | 2019-12-11 22:17:02 -0800 | [diff] [blame] | 1459 | thermal->sensors[i].tzd->tzp->no_hwmon = false; |
| 1460 | error = thermal_add_hwmon_sysfs(thermal->sensors[i].tzd); |
| 1461 | if (error) |
| 1462 | dev_warn(&pdev->dev, |
| 1463 | "failed to register sensor %d with hwmon: %d\n", |
| 1464 | i, error); |
| 1465 | } |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1466 | |
| 1467 | platform_set_drvdata(pdev, thermal); |
| 1468 | |
| 1469 | return 0; |
| 1470 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1471 | err_disable_pclk: |
| 1472 | clk_disable_unprepare(thermal->pclk); |
| 1473 | err_disable_clk: |
| 1474 | clk_disable_unprepare(thermal->clk); |
| 1475 | |
| 1476 | return error; |
| 1477 | } |
| 1478 | |
| 1479 | static int rockchip_thermal_remove(struct platform_device *pdev) |
| 1480 | { |
| 1481 | struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev); |
| 1482 | int i; |
| 1483 | |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 1484 | for (i = 0; i < thermal->chip->chn_num; i++) { |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1485 | struct rockchip_thermal_sensor *sensor = &thermal->sensors[i]; |
| 1486 | |
Stefan Schaeckeler | d27970b | 2019-12-11 22:17:02 -0800 | [diff] [blame] | 1487 | thermal_remove_hwmon_sysfs(sensor->tzd); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1488 | rockchip_thermal_toggle_sensor(sensor, false); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | thermal->chip->control(thermal->regs, false); |
| 1492 | |
| 1493 | clk_disable_unprepare(thermal->pclk); |
| 1494 | clk_disable_unprepare(thermal->clk); |
| 1495 | |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
| 1499 | static int __maybe_unused rockchip_thermal_suspend(struct device *dev) |
| 1500 | { |
Wolfram Sang | 26d84c2 | 2018-10-21 22:00:49 +0200 | [diff] [blame] | 1501 | struct rockchip_thermal_data *thermal = dev_get_drvdata(dev); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1502 | int i; |
| 1503 | |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 1504 | for (i = 0; i < thermal->chip->chn_num; i++) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1505 | rockchip_thermal_toggle_sensor(&thermal->sensors[i], false); |
| 1506 | |
| 1507 | thermal->chip->control(thermal->regs, false); |
| 1508 | |
| 1509 | clk_disable(thermal->pclk); |
| 1510 | clk_disable(thermal->clk); |
Heiko Stuebner | 0f5ee06 | 2019-05-22 16:12:36 +0200 | [diff] [blame] | 1511 | |
| 1512 | pinctrl_pm_select_sleep_state(dev); |
Caesar Wang | 7e38a5b | 2015-10-23 19:25:27 +0800 | [diff] [blame] | 1513 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1514 | return 0; |
| 1515 | } |
| 1516 | |
| 1517 | static int __maybe_unused rockchip_thermal_resume(struct device *dev) |
| 1518 | { |
Wolfram Sang | 26d84c2 | 2018-10-21 22:00:49 +0200 | [diff] [blame] | 1519 | struct rockchip_thermal_data *thermal = dev_get_drvdata(dev); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1520 | int i; |
| 1521 | int error; |
| 1522 | |
| 1523 | error = clk_enable(thermal->clk); |
| 1524 | if (error) |
| 1525 | return error; |
| 1526 | |
| 1527 | error = clk_enable(thermal->pclk); |
Shawn Lin | ab5b52f | 2016-04-18 11:35:53 +0800 | [diff] [blame] | 1528 | if (error) { |
| 1529 | clk_disable(thermal->clk); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1530 | return error; |
Shawn Lin | ab5b52f | 2016-04-18 11:35:53 +0800 | [diff] [blame] | 1531 | } |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1532 | |
| 1533 | rockchip_thermal_reset_controller(thermal->reset); |
| 1534 | |
Caesar Wang | b948476 | 2016-04-18 11:35:56 +0800 | [diff] [blame] | 1535 | thermal->chip->initialize(thermal->grf, thermal->regs, |
| 1536 | thermal->tshut_polarity); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1537 | |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 1538 | for (i = 0; i < thermal->chip->chn_num; i++) { |
| 1539 | int id = thermal->sensors[i].id; |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1540 | |
| 1541 | thermal->chip->set_tshut_mode(id, thermal->regs, |
| 1542 | thermal->tshut_mode); |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 1543 | |
| 1544 | error = thermal->chip->set_tshut_temp(&thermal->chip->table, |
Caesar Wang | ce74110 | 2015-11-09 12:48:56 +0800 | [diff] [blame] | 1545 | id, thermal->regs, |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1546 | thermal->tshut_temp); |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 1547 | if (error) |
Wolfram Sang | 26d84c2 | 2018-10-21 22:00:49 +0200 | [diff] [blame] | 1548 | dev_err(dev, "%s: invalid tshut=%d, error=%d\n", |
Caesar Wang | d353049 | 2016-12-12 19:05:33 +0800 | [diff] [blame] | 1549 | __func__, thermal->tshut_temp, error); |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | thermal->chip->control(thermal->regs, true); |
| 1553 | |
Caesar Wang | 1d98b618 | 2015-11-05 13:17:58 +0800 | [diff] [blame] | 1554 | for (i = 0; i < thermal->chip->chn_num; i++) |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1555 | rockchip_thermal_toggle_sensor(&thermal->sensors[i], true); |
| 1556 | |
Heiko Stuebner | 0f5ee06 | 2019-05-22 16:12:36 +0200 | [diff] [blame] | 1557 | pinctrl_pm_select_default_state(dev); |
Caesar Wang | 7e38a5b | 2015-10-23 19:25:27 +0800 | [diff] [blame] | 1558 | |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1559 | return 0; |
| 1560 | } |
| 1561 | |
| 1562 | static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops, |
| 1563 | rockchip_thermal_suspend, rockchip_thermal_resume); |
| 1564 | |
| 1565 | static struct platform_driver rockchip_thermal_driver = { |
| 1566 | .driver = { |
| 1567 | .name = "rockchip-thermal", |
Caesar Wang | cbac8f63 | 2014-11-24 12:58:59 +0800 | [diff] [blame] | 1568 | .pm = &rockchip_thermal_pm_ops, |
| 1569 | .of_match_table = of_rockchip_thermal_match, |
| 1570 | }, |
| 1571 | .probe = rockchip_thermal_probe, |
| 1572 | .remove = rockchip_thermal_remove, |
| 1573 | }; |
| 1574 | |
| 1575 | module_platform_driver(rockchip_thermal_driver); |
| 1576 | |
| 1577 | MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver"); |
| 1578 | MODULE_AUTHOR("Rockchip, Inc."); |
| 1579 | MODULE_LICENSE("GPL v2"); |
| 1580 | MODULE_ALIAS("platform:rockchip-thermal"); |