Thomas Gleixner | 5a72924 | 2022-06-07 16:11:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 2 | /* |
Hao Fang | 4481b39 | 2021-03-30 14:45:33 +0800 | [diff] [blame] | 3 | * HiSilicon thermal sensor driver |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 4 | * |
Hao Fang | 4481b39 | 2021-03-30 14:45:33 +0800 | [diff] [blame] | 5 | * Copyright (c) 2014-2015 HiSilicon Limited. |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 6 | * Copyright (c) 2014-2015 Linaro Limited. |
| 7 | * |
| 8 | * Xinwei Kong <kong.kongxinwei@hisilicon.com> |
| 9 | * Leo Yan <leo.yan@linaro.org> |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/cpufreq.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/module.h> |
Rob Herring | f6a756e | 2023-07-14 11:50:07 -0600 | [diff] [blame] | 16 | #include <linux/of.h> |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/io.h> |
Daniel Lezcano | 9272d2d4 | 2023-02-06 16:34:29 +0100 | [diff] [blame] | 19 | #include <linux/thermal.h> |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 20 | |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 21 | #define HI6220_TEMP0_LAG (0x0) |
| 22 | #define HI6220_TEMP0_TH (0x4) |
| 23 | #define HI6220_TEMP0_RST_TH (0x8) |
| 24 | #define HI6220_TEMP0_CFG (0xC) |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 25 | #define HI6220_TEMP0_CFG_SS_MSK (0xF000) |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 26 | #define HI6220_TEMP0_CFG_HDAK_MSK (0x30) |
| 27 | #define HI6220_TEMP0_EN (0x10) |
| 28 | #define HI6220_TEMP0_INT_EN (0x14) |
| 29 | #define HI6220_TEMP0_INT_CLR (0x18) |
| 30 | #define HI6220_TEMP0_RST_MSK (0x1C) |
| 31 | #define HI6220_TEMP0_VALUE (0x28) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 32 | |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 33 | #define HI3660_OFFSET(chan) ((chan) * 0x40) |
| 34 | #define HI3660_TEMP(chan) (HI3660_OFFSET(chan) + 0x1C) |
| 35 | #define HI3660_TH(chan) (HI3660_OFFSET(chan) + 0x20) |
| 36 | #define HI3660_LAG(chan) (HI3660_OFFSET(chan) + 0x28) |
| 37 | #define HI3660_INT_EN(chan) (HI3660_OFFSET(chan) + 0x2C) |
| 38 | #define HI3660_INT_CLR(chan) (HI3660_OFFSET(chan) + 0x30) |
| 39 | |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 40 | #define HI6220_TEMP_BASE (-60000) |
| 41 | #define HI6220_TEMP_RESET (100000) |
| 42 | #define HI6220_TEMP_STEP (785) |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 43 | #define HI6220_TEMP_LAG (3500) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 44 | |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 45 | #define HI3660_TEMP_BASE (-63780) |
| 46 | #define HI3660_TEMP_STEP (205) |
| 47 | #define HI3660_TEMP_LAG (4000) |
| 48 | |
Daniel Lezcano | a849eec | 2018-09-25 11:03:05 +0200 | [diff] [blame] | 49 | #define HI6220_CLUSTER0_SENSOR 2 |
Daniel Lezcano | ce8c070 | 2018-09-25 11:03:10 +0200 | [diff] [blame] | 50 | #define HI6220_CLUSTER1_SENSOR 1 |
| 51 | |
| 52 | #define HI3660_LITTLE_SENSOR 0 |
Daniel Lezcano | a849eec | 2018-09-25 11:03:05 +0200 | [diff] [blame] | 53 | #define HI3660_BIG_SENSOR 1 |
Daniel Lezcano | ce8c070 | 2018-09-25 11:03:10 +0200 | [diff] [blame] | 54 | #define HI3660_G3D_SENSOR 2 |
| 55 | #define HI3660_MODEM_SENSOR 3 |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 56 | |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 57 | struct hisi_thermal_data; |
| 58 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 59 | struct hisi_thermal_sensor { |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 60 | struct hisi_thermal_data *data; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 61 | struct thermal_zone_device *tzd; |
Daniel Lezcano | 2cffaef | 2018-09-25 11:03:07 +0200 | [diff] [blame] | 62 | const char *irq_name; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 63 | uint32_t id; |
| 64 | uint32_t thres_temp; |
| 65 | }; |
| 66 | |
Daniel Lezcano | c90aaec | 2018-09-25 11:02:59 +0200 | [diff] [blame] | 67 | struct hisi_thermal_ops { |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 68 | int (*get_temp)(struct hisi_thermal_sensor *sensor); |
| 69 | int (*enable_sensor)(struct hisi_thermal_sensor *sensor); |
| 70 | int (*disable_sensor)(struct hisi_thermal_sensor *sensor); |
| 71 | int (*irq_handler)(struct hisi_thermal_sensor *sensor); |
Daniel Lezcano | c90aaec | 2018-09-25 11:02:59 +0200 | [diff] [blame] | 72 | int (*probe)(struct hisi_thermal_data *data); |
| 73 | }; |
| 74 | |
| 75 | struct hisi_thermal_data { |
| 76 | const struct hisi_thermal_ops *ops; |
Daniel Lezcano | 8c0ffc8 | 2018-09-25 11:03:03 +0200 | [diff] [blame] | 77 | struct hisi_thermal_sensor *sensor; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 78 | struct platform_device *pdev; |
| 79 | struct clk *clk; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 80 | void __iomem *regs; |
Daniel Lezcano | 8c0ffc8 | 2018-09-25 11:03:03 +0200 | [diff] [blame] | 81 | int nr_sensors; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 82 | }; |
| 83 | |
Daniel Lezcano | 48880b9 | 2017-10-19 19:05:46 +0200 | [diff] [blame] | 84 | /* |
| 85 | * The temperature computation on the tsensor is as follow: |
| 86 | * Unit: millidegree Celsius |
Kevin Wangtao | e42bbe1 | 2017-10-19 19:05:57 +0200 | [diff] [blame] | 87 | * Step: 200/255 (0.7843) |
Daniel Lezcano | 48880b9 | 2017-10-19 19:05:46 +0200 | [diff] [blame] | 88 | * Temperature base: -60°C |
| 89 | * |
Kevin Wangtao | e42bbe1 | 2017-10-19 19:05:57 +0200 | [diff] [blame] | 90 | * The register is programmed in temperature steps, every step is 785 |
Daniel Lezcano | 48880b9 | 2017-10-19 19:05:46 +0200 | [diff] [blame] | 91 | * millidegree and begins at -60 000 m°C |
| 92 | * |
| 93 | * The temperature from the steps: |
| 94 | * |
Kevin Wangtao | e42bbe1 | 2017-10-19 19:05:57 +0200 | [diff] [blame] | 95 | * Temp = TempBase + (steps x 785) |
Daniel Lezcano | 48880b9 | 2017-10-19 19:05:46 +0200 | [diff] [blame] | 96 | * |
| 97 | * and the steps from the temperature: |
| 98 | * |
Kevin Wangtao | e42bbe1 | 2017-10-19 19:05:57 +0200 | [diff] [blame] | 99 | * steps = (Temp - TempBase) / 785 |
Daniel Lezcano | 48880b9 | 2017-10-19 19:05:46 +0200 | [diff] [blame] | 100 | * |
| 101 | */ |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 102 | static inline int hi6220_thermal_step_to_temp(int step) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 103 | { |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 104 | return HI6220_TEMP_BASE + (step * HI6220_TEMP_STEP); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 105 | } |
| 106 | |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 107 | static inline int hi6220_thermal_temp_to_step(int temp) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 108 | { |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 109 | return DIV_ROUND_UP(temp - HI6220_TEMP_BASE, HI6220_TEMP_STEP); |
Daniel Lezcano | db2b033 | 2017-10-19 19:05:47 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 112 | /* |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 113 | * for Hi3660, |
| 114 | * Step: 189/922 (0.205) |
| 115 | * Temperature base: -63.780°C |
| 116 | * |
| 117 | * The register is programmed in temperature steps, every step is 205 |
| 118 | * millidegree and begins at -63 780 m°C |
| 119 | */ |
| 120 | static inline int hi3660_thermal_step_to_temp(int step) |
| 121 | { |
| 122 | return HI3660_TEMP_BASE + step * HI3660_TEMP_STEP; |
| 123 | } |
| 124 | |
| 125 | static inline int hi3660_thermal_temp_to_step(int temp) |
| 126 | { |
| 127 | return DIV_ROUND_UP(temp - HI3660_TEMP_BASE, HI3660_TEMP_STEP); |
| 128 | } |
| 129 | |
| 130 | /* |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 131 | * The lag register contains 5 bits encoding the temperature in steps. |
| 132 | * |
| 133 | * Each time the temperature crosses the threshold boundary, an |
| 134 | * interrupt is raised. It could be when the temperature is going |
| 135 | * above the threshold or below. However, if the temperature is |
| 136 | * fluctuating around this value due to the load, we can receive |
| 137 | * several interrupts which may not desired. |
| 138 | * |
| 139 | * We can setup a temperature representing the delta between the |
| 140 | * threshold and the current temperature when the temperature is |
| 141 | * decreasing. |
| 142 | * |
| 143 | * For instance: the lag register is 5°C, the threshold is 65°C, when |
| 144 | * the temperature reaches 65°C an interrupt is raised and when the |
| 145 | * temperature decrease to 65°C - 5°C another interrupt is raised. |
| 146 | * |
| 147 | * A very short lag can lead to an interrupt storm, a long lag |
| 148 | * increase the latency to react to the temperature changes. In our |
| 149 | * case, that is not really a problem as we are polling the |
| 150 | * temperature. |
| 151 | * |
| 152 | * [0:4] : lag register |
| 153 | * |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 154 | * The temperature is coded in steps, cf. HI6220_TEMP_STEP. |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 155 | * |
| 156 | * Min : 0x00 : 0.0 °C |
| 157 | * Max : 0x1F : 24.3 °C |
| 158 | * |
| 159 | * The 'value' parameter is in milliCelsius. |
| 160 | */ |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 161 | static inline void hi6220_thermal_set_lag(void __iomem *addr, int value) |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 162 | { |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 163 | writel(DIV_ROUND_UP(value, HI6220_TEMP_STEP) & 0x1F, |
| 164 | addr + HI6220_TEMP0_LAG); |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 167 | static inline void hi6220_thermal_alarm_clear(void __iomem *addr, int value) |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 168 | { |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 169 | writel(value, addr + HI6220_TEMP0_INT_CLR); |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 172 | static inline void hi6220_thermal_alarm_enable(void __iomem *addr, int value) |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 173 | { |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 174 | writel(value, addr + HI6220_TEMP0_INT_EN); |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 177 | static inline void hi6220_thermal_alarm_set(void __iomem *addr, int temp) |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 178 | { |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 179 | writel(hi6220_thermal_temp_to_step(temp) | 0x0FFFFFF00, |
| 180 | addr + HI6220_TEMP0_TH); |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 183 | static inline void hi6220_thermal_reset_set(void __iomem *addr, int temp) |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 184 | { |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 185 | writel(hi6220_thermal_temp_to_step(temp), addr + HI6220_TEMP0_RST_TH); |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 188 | static inline void hi6220_thermal_reset_enable(void __iomem *addr, int value) |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 189 | { |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 190 | writel(value, addr + HI6220_TEMP0_RST_MSK); |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 193 | static inline void hi6220_thermal_enable(void __iomem *addr, int value) |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 194 | { |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 195 | writel(value, addr + HI6220_TEMP0_EN); |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 198 | static inline int hi6220_thermal_get_temperature(void __iomem *addr) |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 199 | { |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 200 | return hi6220_thermal_step_to_temp(readl(addr + HI6220_TEMP0_VALUE)); |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Daniel Lezcano | b424315 | 2017-10-19 19:05:50 +0200 | [diff] [blame] | 203 | /* |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 204 | * [0:6] lag register |
| 205 | * |
| 206 | * The temperature is coded in steps, cf. HI3660_TEMP_STEP. |
| 207 | * |
| 208 | * Min : 0x00 : 0.0 °C |
| 209 | * Max : 0x7F : 26.0 °C |
| 210 | * |
| 211 | */ |
| 212 | static inline void hi3660_thermal_set_lag(void __iomem *addr, |
| 213 | int id, int value) |
| 214 | { |
| 215 | writel(DIV_ROUND_UP(value, HI3660_TEMP_STEP) & 0x7F, |
| 216 | addr + HI3660_LAG(id)); |
| 217 | } |
| 218 | |
| 219 | static inline void hi3660_thermal_alarm_clear(void __iomem *addr, |
| 220 | int id, int value) |
| 221 | { |
| 222 | writel(value, addr + HI3660_INT_CLR(id)); |
| 223 | } |
| 224 | |
| 225 | static inline void hi3660_thermal_alarm_enable(void __iomem *addr, |
| 226 | int id, int value) |
| 227 | { |
| 228 | writel(value, addr + HI3660_INT_EN(id)); |
| 229 | } |
| 230 | |
| 231 | static inline void hi3660_thermal_alarm_set(void __iomem *addr, |
| 232 | int id, int value) |
| 233 | { |
| 234 | writel(value, addr + HI3660_TH(id)); |
| 235 | } |
| 236 | |
| 237 | static inline int hi3660_thermal_get_temperature(void __iomem *addr, int id) |
| 238 | { |
| 239 | return hi3660_thermal_step_to_temp(readl(addr + HI3660_TEMP(id))); |
| 240 | } |
| 241 | |
| 242 | /* |
Daniel Lezcano | b424315 | 2017-10-19 19:05:50 +0200 | [diff] [blame] | 243 | * Temperature configuration register - Sensor selection |
| 244 | * |
| 245 | * Bits [19:12] |
| 246 | * |
| 247 | * 0x0: local sensor (default) |
| 248 | * 0x1: remote sensor 1 (ACPU cluster 1) |
| 249 | * 0x2: remote sensor 2 (ACPU cluster 0) |
| 250 | * 0x3: remote sensor 3 (G3D) |
| 251 | */ |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 252 | static inline void hi6220_thermal_sensor_select(void __iomem *addr, int sensor) |
Daniel Lezcano | b424315 | 2017-10-19 19:05:50 +0200 | [diff] [blame] | 253 | { |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 254 | writel((readl(addr + HI6220_TEMP0_CFG) & ~HI6220_TEMP0_CFG_SS_MSK) | |
| 255 | (sensor << 12), addr + HI6220_TEMP0_CFG); |
Daniel Lezcano | b424315 | 2017-10-19 19:05:50 +0200 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Temperature configuration register - Hdak conversion polling interval |
| 260 | * |
| 261 | * Bits [5:4] |
| 262 | * |
| 263 | * 0x0 : 0.768 ms |
| 264 | * 0x1 : 6.144 ms |
| 265 | * 0x2 : 49.152 ms |
| 266 | * 0x3 : 393.216 ms |
| 267 | */ |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 268 | static inline void hi6220_thermal_hdak_set(void __iomem *addr, int value) |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 269 | { |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 270 | writel((readl(addr + HI6220_TEMP0_CFG) & ~HI6220_TEMP0_CFG_HDAK_MSK) | |
| 271 | (value << 4), addr + HI6220_TEMP0_CFG); |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 274 | static int hi6220_thermal_irq_handler(struct hisi_thermal_sensor *sensor) |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 275 | { |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 276 | struct hisi_thermal_data *data = sensor->data; |
| 277 | |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 278 | hi6220_thermal_alarm_clear(data->regs, 1); |
| 279 | return 0; |
| 280 | } |
| 281 | |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 282 | static int hi3660_thermal_irq_handler(struct hisi_thermal_sensor *sensor) |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 283 | { |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 284 | struct hisi_thermal_data *data = sensor->data; |
| 285 | |
| 286 | hi3660_thermal_alarm_clear(data->regs, sensor->id, 1); |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 290 | static int hi6220_thermal_get_temp(struct hisi_thermal_sensor *sensor) |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 291 | { |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 292 | struct hisi_thermal_data *data = sensor->data; |
| 293 | |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 294 | return hi6220_thermal_get_temperature(data->regs); |
| 295 | } |
| 296 | |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 297 | static int hi3660_thermal_get_temp(struct hisi_thermal_sensor *sensor) |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 298 | { |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 299 | struct hisi_thermal_data *data = sensor->data; |
| 300 | |
| 301 | return hi3660_thermal_get_temperature(data->regs, sensor->id); |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 304 | static int hi6220_thermal_disable_sensor(struct hisi_thermal_sensor *sensor) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 305 | { |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 306 | struct hisi_thermal_data *data = sensor->data; |
| 307 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 308 | /* disable sensor module */ |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 309 | hi6220_thermal_enable(data->regs, 0); |
| 310 | hi6220_thermal_alarm_enable(data->regs, 0); |
| 311 | hi6220_thermal_reset_enable(data->regs, 0); |
Kevin Wangtao | 943c0f6a | 2017-10-19 19:05:56 +0200 | [diff] [blame] | 312 | |
| 313 | clk_disable_unprepare(data->clk); |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 314 | |
| 315 | return 0; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 316 | } |
| 317 | |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 318 | static int hi3660_thermal_disable_sensor(struct hisi_thermal_sensor *sensor) |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 319 | { |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 320 | struct hisi_thermal_data *data = sensor->data; |
| 321 | |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 322 | /* disable sensor module */ |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 323 | hi3660_thermal_alarm_enable(data->regs, sensor->id, 0); |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 324 | return 0; |
| 325 | } |
| 326 | |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 327 | static int hi6220_thermal_enable_sensor(struct hisi_thermal_sensor *sensor) |
Kevin Wangtao | a0678da8 | 2017-10-22 10:54:32 +0200 | [diff] [blame] | 328 | { |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 329 | struct hisi_thermal_data *data = sensor->data; |
Kevin Wangtao | a0678da8 | 2017-10-22 10:54:32 +0200 | [diff] [blame] | 330 | int ret; |
| 331 | |
| 332 | /* enable clock for tsensor */ |
| 333 | ret = clk_prepare_enable(data->clk); |
| 334 | if (ret) |
| 335 | return ret; |
| 336 | |
| 337 | /* disable module firstly */ |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 338 | hi6220_thermal_reset_enable(data->regs, 0); |
| 339 | hi6220_thermal_enable(data->regs, 0); |
Kevin Wangtao | a0678da8 | 2017-10-22 10:54:32 +0200 | [diff] [blame] | 340 | |
| 341 | /* select sensor id */ |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 342 | hi6220_thermal_sensor_select(data->regs, sensor->id); |
Kevin Wangtao | a0678da8 | 2017-10-22 10:54:32 +0200 | [diff] [blame] | 343 | |
| 344 | /* setting the hdak time */ |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 345 | hi6220_thermal_hdak_set(data->regs, 0); |
Kevin Wangtao | a0678da8 | 2017-10-22 10:54:32 +0200 | [diff] [blame] | 346 | |
| 347 | /* setting lag value between current temp and the threshold */ |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 348 | hi6220_thermal_set_lag(data->regs, HI6220_TEMP_LAG); |
Kevin Wangtao | a0678da8 | 2017-10-22 10:54:32 +0200 | [diff] [blame] | 349 | |
| 350 | /* enable for interrupt */ |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 351 | hi6220_thermal_alarm_set(data->regs, sensor->thres_temp); |
Kevin Wangtao | a0678da8 | 2017-10-22 10:54:32 +0200 | [diff] [blame] | 352 | |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 353 | hi6220_thermal_reset_set(data->regs, HI6220_TEMP_RESET); |
Kevin Wangtao | a0678da8 | 2017-10-22 10:54:32 +0200 | [diff] [blame] | 354 | |
| 355 | /* enable module */ |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 356 | hi6220_thermal_reset_enable(data->regs, 1); |
| 357 | hi6220_thermal_enable(data->regs, 1); |
Kevin Wangtao | a0678da8 | 2017-10-22 10:54:32 +0200 | [diff] [blame] | 358 | |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 359 | hi6220_thermal_alarm_clear(data->regs, 0); |
| 360 | hi6220_thermal_alarm_enable(data->regs, 1); |
Kevin Wangtao | a0678da8 | 2017-10-22 10:54:32 +0200 | [diff] [blame] | 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 365 | static int hi3660_thermal_enable_sensor(struct hisi_thermal_sensor *sensor) |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 366 | { |
| 367 | unsigned int value; |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 368 | struct hisi_thermal_data *data = sensor->data; |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 369 | |
| 370 | /* disable interrupt */ |
| 371 | hi3660_thermal_alarm_enable(data->regs, sensor->id, 0); |
| 372 | |
| 373 | /* setting lag value between current temp and the threshold */ |
| 374 | hi3660_thermal_set_lag(data->regs, sensor->id, HI3660_TEMP_LAG); |
| 375 | |
| 376 | /* set interrupt threshold */ |
| 377 | value = hi3660_thermal_temp_to_step(sensor->thres_temp); |
| 378 | hi3660_thermal_alarm_set(data->regs, sensor->id, value); |
| 379 | |
| 380 | /* enable interrupt */ |
| 381 | hi3660_thermal_alarm_clear(data->regs, sensor->id, 1); |
| 382 | hi3660_thermal_alarm_enable(data->regs, sensor->id, 1); |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 387 | static int hi6220_thermal_probe(struct hisi_thermal_data *data) |
| 388 | { |
| 389 | struct platform_device *pdev = data->pdev; |
| 390 | struct device *dev = &pdev->dev; |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 391 | int ret; |
| 392 | |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 393 | data->clk = devm_clk_get(dev, "thermal_clk"); |
| 394 | if (IS_ERR(data->clk)) { |
| 395 | ret = PTR_ERR(data->clk); |
| 396 | if (ret != -EPROBE_DEFER) |
| 397 | dev_err(dev, "failed to get thermal clk: %d\n", ret); |
| 398 | return ret; |
| 399 | } |
| 400 | |
Daniel Lezcano | 8c0ffc8 | 2018-09-25 11:03:03 +0200 | [diff] [blame] | 401 | data->sensor = devm_kzalloc(dev, sizeof(*data->sensor), GFP_KERNEL); |
| 402 | if (!data->sensor) |
| 403 | return -ENOMEM; |
| 404 | |
Daniel Lezcano | a849eec | 2018-09-25 11:03:05 +0200 | [diff] [blame] | 405 | data->sensor[0].id = HI6220_CLUSTER0_SENSOR; |
Daniel Lezcano | 2cffaef | 2018-09-25 11:03:07 +0200 | [diff] [blame] | 406 | data->sensor[0].irq_name = "tsensor_intr"; |
Daniel Lezcano | 8c0ffc8 | 2018-09-25 11:03:03 +0200 | [diff] [blame] | 407 | data->sensor[0].data = data; |
| 408 | data->nr_sensors = 1; |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 413 | static int hi3660_thermal_probe(struct hisi_thermal_data *data) |
| 414 | { |
Daniel Lezcano | 8c0ffc8 | 2018-09-25 11:03:03 +0200 | [diff] [blame] | 415 | struct platform_device *pdev = data->pdev; |
| 416 | struct device *dev = &pdev->dev; |
| 417 | |
Daniel Lezcano | 7d3a2a2 | 2018-11-30 09:00:32 +0100 | [diff] [blame] | 418 | data->nr_sensors = 1; |
Daniel Lezcano | 8c6c368 | 2018-09-25 11:03:12 +0200 | [diff] [blame] | 419 | |
| 420 | data->sensor = devm_kzalloc(dev, sizeof(*data->sensor) * |
| 421 | data->nr_sensors, GFP_KERNEL); |
Daniel Lezcano | 8c0ffc8 | 2018-09-25 11:03:03 +0200 | [diff] [blame] | 422 | if (!data->sensor) |
| 423 | return -ENOMEM; |
| 424 | |
Daniel Lezcano | a849eec | 2018-09-25 11:03:05 +0200 | [diff] [blame] | 425 | data->sensor[0].id = HI3660_BIG_SENSOR; |
Daniel Lezcano | 2cffaef | 2018-09-25 11:03:07 +0200 | [diff] [blame] | 426 | data->sensor[0].irq_name = "tsensor_a73"; |
Daniel Lezcano | 8c0ffc8 | 2018-09-25 11:03:03 +0200 | [diff] [blame] | 427 | data->sensor[0].data = data; |
Daniel Lezcano | 8c6c368 | 2018-09-25 11:03:12 +0200 | [diff] [blame] | 428 | |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 429 | return 0; |
| 430 | } |
| 431 | |
Daniel Lezcano | 5ee7811 | 2022-08-05 00:43:39 +0200 | [diff] [blame] | 432 | static int hisi_thermal_get_temp(struct thermal_zone_device *tz, int *temp) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 433 | { |
Daniel Lezcano | 5f68d07 | 2023-03-01 21:14:30 +0100 | [diff] [blame] | 434 | struct hisi_thermal_sensor *sensor = thermal_zone_device_priv(tz); |
Daniel Lezcano | 49e778d | 2018-09-25 11:03:01 +0200 | [diff] [blame] | 435 | struct hisi_thermal_data *data = sensor->data; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 436 | |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 437 | *temp = data->ops->get_temp(sensor); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 438 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 439 | return 0; |
| 440 | } |
| 441 | |
Daniel Lezcano | 5ee7811 | 2022-08-05 00:43:39 +0200 | [diff] [blame] | 442 | static const struct thermal_zone_device_ops hisi_of_thermal_ops = { |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 443 | .get_temp = hisi_thermal_get_temp, |
| 444 | }; |
| 445 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 446 | static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev) |
| 447 | { |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 448 | struct hisi_thermal_sensor *sensor = dev; |
| 449 | struct hisi_thermal_data *data = sensor->data; |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 450 | int temp = 0; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 451 | |
Daniel Lezcano | 9c9ae8d | 2018-09-25 11:03:00 +0200 | [diff] [blame] | 452 | data->ops->irq_handler(sensor); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 453 | |
Daniel Lezcano | 5ee7811 | 2022-08-05 00:43:39 +0200 | [diff] [blame] | 454 | temp = data->ops->get_temp(sensor); |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 455 | |
| 456 | if (temp >= sensor->thres_temp) { |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 457 | dev_crit(&data->pdev->dev, |
| 458 | "sensor <%d> THERMAL ALARM: %d > %d\n", |
| 459 | sensor->id, temp, sensor->thres_temp); |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 460 | |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 461 | thermal_zone_device_update(sensor->tzd, |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 462 | THERMAL_EVENT_UNSPECIFIED); |
| 463 | |
Kevin Wangtao | 5ed82b7 | 2017-10-22 10:54:33 +0200 | [diff] [blame] | 464 | } else { |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 465 | dev_crit(&data->pdev->dev, |
| 466 | "sensor <%d> THERMAL ALARM stopped: %d < %d\n", |
| 467 | sensor->id, temp, sensor->thres_temp); |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 468 | } |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 469 | |
| 470 | return IRQ_HANDLED; |
| 471 | } |
| 472 | |
| 473 | static int hisi_thermal_register_sensor(struct platform_device *pdev, |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 474 | struct hisi_thermal_sensor *sensor) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 475 | { |
| 476 | int ret, i; |
Daniel Lezcano | 68a306c | 2022-10-03 11:25:45 +0200 | [diff] [blame] | 477 | struct thermal_trip trip; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 478 | |
Daniel Lezcano | 5ee7811 | 2022-08-05 00:43:39 +0200 | [diff] [blame] | 479 | sensor->tzd = devm_thermal_of_zone_register(&pdev->dev, |
| 480 | sensor->id, sensor, |
| 481 | &hisi_of_thermal_ops); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 482 | if (IS_ERR(sensor->tzd)) { |
| 483 | ret = PTR_ERR(sensor->tzd); |
Leo Yan | 439dc96 | 2016-03-29 19:27:12 +0800 | [diff] [blame] | 484 | sensor->tzd = NULL; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 485 | dev_err(&pdev->dev, "failed to register sensor id %d: %d\n", |
| 486 | sensor->id, ret); |
| 487 | return ret; |
| 488 | } |
| 489 | |
Daniel Lezcano | 68a306c | 2022-10-03 11:25:45 +0200 | [diff] [blame] | 490 | for (i = 0; i < thermal_zone_get_num_trips(sensor->tzd); i++) { |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 491 | |
Daniel Lezcano | 68a306c | 2022-10-03 11:25:45 +0200 | [diff] [blame] | 492 | thermal_zone_get_trip(sensor->tzd, i, &trip); |
| 493 | |
| 494 | if (trip.type == THERMAL_TRIP_PASSIVE) { |
| 495 | sensor->thres_temp = trip.temperature; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 496 | break; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
Daniel Lezcano | c90aaec | 2018-09-25 11:02:59 +0200 | [diff] [blame] | 503 | static const struct hisi_thermal_ops hi6220_ops = { |
| 504 | .get_temp = hi6220_thermal_get_temp, |
| 505 | .enable_sensor = hi6220_thermal_enable_sensor, |
| 506 | .disable_sensor = hi6220_thermal_disable_sensor, |
| 507 | .irq_handler = hi6220_thermal_irq_handler, |
| 508 | .probe = hi6220_thermal_probe, |
| 509 | }; |
| 510 | |
| 511 | static const struct hisi_thermal_ops hi3660_ops = { |
| 512 | .get_temp = hi3660_thermal_get_temp, |
| 513 | .enable_sensor = hi3660_thermal_enable_sensor, |
| 514 | .disable_sensor = hi3660_thermal_disable_sensor, |
| 515 | .irq_handler = hi3660_thermal_irq_handler, |
| 516 | .probe = hi3660_thermal_probe, |
| 517 | }; |
| 518 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 519 | static const struct of_device_id of_hisi_thermal_match[] = { |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 520 | { |
| 521 | .compatible = "hisilicon,tsensor", |
Daniel Lezcano | c90aaec | 2018-09-25 11:02:59 +0200 | [diff] [blame] | 522 | .data = &hi6220_ops, |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 523 | }, |
| 524 | { |
| 525 | .compatible = "hisilicon,hi3660-tsensor", |
Daniel Lezcano | c90aaec | 2018-09-25 11:02:59 +0200 | [diff] [blame] | 526 | .data = &hi3660_ops, |
Kevin Wangtao | 2bb60a8 | 2017-10-22 10:54:35 +0200 | [diff] [blame] | 527 | }, |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 528 | { /* end */ } |
| 529 | }; |
| 530 | MODULE_DEVICE_TABLE(of, of_hisi_thermal_match); |
| 531 | |
| 532 | static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor, |
| 533 | bool on) |
| 534 | { |
| 535 | struct thermal_zone_device *tzd = sensor->tzd; |
| 536 | |
Andrzej Pietrasiewicz | 7f4957b | 2020-06-29 14:29:21 +0200 | [diff] [blame] | 537 | if (on) |
| 538 | thermal_zone_device_enable(tzd); |
| 539 | else |
| 540 | thermal_zone_device_disable(tzd); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static int hisi_thermal_probe(struct platform_device *pdev) |
| 544 | { |
| 545 | struct hisi_thermal_data *data; |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 546 | struct device *dev = &pdev->dev; |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 547 | int i, ret; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 548 | |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 549 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 550 | if (!data) |
| 551 | return -ENOMEM; |
| 552 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 553 | data->pdev = pdev; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 554 | platform_set_drvdata(pdev, data); |
Daniel Lezcano | c90aaec | 2018-09-25 11:02:59 +0200 | [diff] [blame] | 555 | data->ops = of_device_get_match_data(dev); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 556 | |
Yang Li | e45c9a2 | 2023-03-08 14:27:19 +0800 | [diff] [blame] | 557 | data->regs = devm_platform_ioremap_resource(pdev, 0); |
Ye Bin | beaa410 | 2021-04-09 15:52:24 +0800 | [diff] [blame] | 558 | if (IS_ERR(data->regs)) |
Daniel Lezcano | 9bb4ec8 | 2018-09-25 11:03:02 +0200 | [diff] [blame] | 559 | return PTR_ERR(data->regs); |
Daniel Lezcano | 9bb4ec8 | 2018-09-25 11:03:02 +0200 | [diff] [blame] | 560 | |
Daniel Lezcano | c90aaec | 2018-09-25 11:02:59 +0200 | [diff] [blame] | 561 | ret = data->ops->probe(data); |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 562 | if (ret) |
| 563 | return ret; |
| 564 | |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 565 | for (i = 0; i < data->nr_sensors; i++) { |
| 566 | struct hisi_thermal_sensor *sensor = &data->sensor[i]; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 567 | |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 568 | ret = hisi_thermal_register_sensor(pdev, sensor); |
| 569 | if (ret) { |
| 570 | dev_err(dev, "failed to register thermal sensor: %d\n", |
| 571 | ret); |
| 572 | return ret; |
| 573 | } |
Daniel Lezcano | ff4ec29 | 2017-10-19 19:05:44 +0200 | [diff] [blame] | 574 | |
Daniel Lezcano | 5d7ab8f | 2018-11-30 09:00:31 +0100 | [diff] [blame] | 575 | ret = platform_get_irq(pdev, 0); |
Daniel Lezcano | a18e83e | 2018-09-25 11:03:09 +0200 | [diff] [blame] | 576 | if (ret < 0) |
| 577 | return ret; |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 578 | |
Daniel Lezcano | a18e83e | 2018-09-25 11:03:09 +0200 | [diff] [blame] | 579 | ret = devm_request_threaded_irq(dev, ret, NULL, |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 580 | hisi_thermal_alarm_irq_thread, |
Daniel Lezcano | 2cffaef | 2018-09-25 11:03:07 +0200 | [diff] [blame] | 581 | IRQF_ONESHOT, sensor->irq_name, |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 582 | sensor); |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 583 | if (ret < 0) { |
Daniel Lezcano | a18e83e | 2018-09-25 11:03:09 +0200 | [diff] [blame] | 584 | dev_err(dev, "Failed to request alarm irq: %d\n", ret); |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 585 | return ret; |
| 586 | } |
Daniel Lezcano | 2cb4de7 | 2017-10-19 19:05:45 +0200 | [diff] [blame] | 587 | |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 588 | ret = data->ops->enable_sensor(sensor); |
| 589 | if (ret) { |
| 590 | dev_err(dev, "Failed to setup the sensor: %d\n", ret); |
| 591 | return ret; |
| 592 | } |
| 593 | |
| 594 | hisi_thermal_toggle_sensor(sensor, true); |
| 595 | } |
Daniel Lezcano | c176b10 | 2017-10-19 19:05:43 +0200 | [diff] [blame] | 596 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 597 | return 0; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 598 | } |
| 599 | |
Uwe Kleine-König | 6abe2f0 | 2023-09-27 21:37:12 +0200 | [diff] [blame] | 600 | static void hisi_thermal_remove(struct platform_device *pdev) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 601 | { |
| 602 | struct hisi_thermal_data *data = platform_get_drvdata(pdev); |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 603 | int i; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 604 | |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 605 | for (i = 0; i < data->nr_sensors; i++) { |
| 606 | struct hisi_thermal_sensor *sensor = &data->sensor[i]; |
Kevin Wangtao | a160a46 | 2017-10-22 10:54:34 +0200 | [diff] [blame] | 607 | |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 608 | hisi_thermal_toggle_sensor(sensor, false); |
| 609 | data->ops->disable_sensor(sensor); |
| 610 | } |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 611 | } |
| 612 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 613 | static int hisi_thermal_suspend(struct device *dev) |
| 614 | { |
| 615 | struct hisi_thermal_data *data = dev_get_drvdata(dev); |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 616 | int i; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 617 | |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 618 | for (i = 0; i < data->nr_sensors; i++) |
| 619 | data->ops->disable_sensor(&data->sensor[i]); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 620 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static int hisi_thermal_resume(struct device *dev) |
| 625 | { |
| 626 | struct hisi_thermal_data *data = dev_get_drvdata(dev); |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 627 | int i, ret = 0; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 628 | |
Daniel Lezcano | 7edc5e4 | 2018-09-25 11:03:04 +0200 | [diff] [blame] | 629 | for (i = 0; i < data->nr_sensors; i++) |
| 630 | ret |= data->ops->enable_sensor(&data->sensor[i]); |
| 631 | |
| 632 | return ret; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 633 | } |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 634 | |
Hesham Almatary | 7bb732f | 2022-03-24 10:34:43 +0000 | [diff] [blame] | 635 | static DEFINE_SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops, |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 636 | hisi_thermal_suspend, hisi_thermal_resume); |
| 637 | |
| 638 | static struct platform_driver hisi_thermal_driver = { |
| 639 | .driver = { |
| 640 | .name = "hisi_thermal", |
Hesham Almatary | 7bb732f | 2022-03-24 10:34:43 +0000 | [diff] [blame] | 641 | .pm = pm_sleep_ptr(&hisi_thermal_pm_ops), |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 642 | .of_match_table = of_hisi_thermal_match, |
| 643 | }, |
| 644 | .probe = hisi_thermal_probe, |
Uwe Kleine-König | 6abe2f0 | 2023-09-27 21:37:12 +0200 | [diff] [blame] | 645 | .remove_new = hisi_thermal_remove, |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 646 | }; |
| 647 | |
| 648 | module_platform_driver(hisi_thermal_driver); |
| 649 | |
| 650 | MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>"); |
| 651 | MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>"); |
Hao Fang | 4481b39 | 2021-03-30 14:45:33 +0800 | [diff] [blame] | 652 | MODULE_DESCRIPTION("HiSilicon thermal driver"); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 653 | MODULE_LICENSE("GPL v2"); |