kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Hisilicon thermal sensor driver |
| 3 | * |
| 4 | * Copyright (c) 2014-2015 Hisilicon Limited. |
| 5 | * Copyright (c) 2014-2015 Linaro Limited. |
| 6 | * |
| 7 | * Xinwei Kong <kong.kongxinwei@hisilicon.com> |
| 8 | * Leo Yan <leo.yan@linaro.org> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 15 | * kind, whether express or implied; without even the implied warranty |
| 16 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/cpufreq.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/io.h> |
| 26 | |
| 27 | #include "thermal_core.h" |
| 28 | |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 29 | #define TEMP0_LAG (0x0) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 30 | #define TEMP0_TH (0x4) |
| 31 | #define TEMP0_RST_TH (0x8) |
| 32 | #define TEMP0_CFG (0xC) |
Daniel Lezcano | b424315 | 2017-10-19 19:05:50 +0200 | [diff] [blame] | 33 | #define TEMP0_CFG_SS_MSK (0xF000) |
| 34 | #define TEMP0_CFG_HDAK_MSK (0x30) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 35 | #define TEMP0_EN (0x10) |
| 36 | #define TEMP0_INT_EN (0x14) |
| 37 | #define TEMP0_INT_CLR (0x18) |
| 38 | #define TEMP0_RST_MSK (0x1C) |
| 39 | #define TEMP0_VALUE (0x28) |
| 40 | |
Daniel Lezcano | 48880b9 | 2017-10-19 19:05:46 +0200 | [diff] [blame] | 41 | #define HISI_TEMP_BASE (-60000) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 42 | #define HISI_TEMP_RESET (100000) |
Kevin Wangtao | e42bbe1 | 2017-10-19 19:05:57 +0200 | [diff] [blame] | 43 | #define HISI_TEMP_STEP (785) |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 44 | #define HISI_TEMP_LAG (3500) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 45 | |
| 46 | #define HISI_MAX_SENSORS 4 |
Daniel Lezcano | ff4ec29 | 2017-10-19 19:05:44 +0200 | [diff] [blame] | 47 | #define HISI_DEFAULT_SENSOR 2 |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 48 | |
| 49 | struct hisi_thermal_sensor { |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 50 | struct thermal_zone_device *tzd; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 51 | uint32_t id; |
| 52 | uint32_t thres_temp; |
| 53 | }; |
| 54 | |
| 55 | struct hisi_thermal_data { |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 56 | struct platform_device *pdev; |
| 57 | struct clk *clk; |
Daniel Lezcano | 609f26d | 2017-10-19 19:05:52 +0200 | [diff] [blame] | 58 | struct hisi_thermal_sensor sensor; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 59 | void __iomem *regs; |
Daniel Lezcano | 609f26d | 2017-10-19 19:05:52 +0200 | [diff] [blame] | 60 | int irq; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 61 | }; |
| 62 | |
Daniel Lezcano | 48880b9 | 2017-10-19 19:05:46 +0200 | [diff] [blame] | 63 | /* |
| 64 | * The temperature computation on the tsensor is as follow: |
| 65 | * Unit: millidegree Celsius |
Kevin Wangtao | e42bbe1 | 2017-10-19 19:05:57 +0200 | [diff] [blame] | 66 | * Step: 200/255 (0.7843) |
Daniel Lezcano | 48880b9 | 2017-10-19 19:05:46 +0200 | [diff] [blame] | 67 | * Temperature base: -60°C |
| 68 | * |
Kevin Wangtao | e42bbe1 | 2017-10-19 19:05:57 +0200 | [diff] [blame] | 69 | * The register is programmed in temperature steps, every step is 785 |
Daniel Lezcano | 48880b9 | 2017-10-19 19:05:46 +0200 | [diff] [blame] | 70 | * millidegree and begins at -60 000 m°C |
| 71 | * |
| 72 | * The temperature from the steps: |
| 73 | * |
Kevin Wangtao | e42bbe1 | 2017-10-19 19:05:57 +0200 | [diff] [blame] | 74 | * Temp = TempBase + (steps x 785) |
Daniel Lezcano | 48880b9 | 2017-10-19 19:05:46 +0200 | [diff] [blame] | 75 | * |
| 76 | * and the steps from the temperature: |
| 77 | * |
Kevin Wangtao | e42bbe1 | 2017-10-19 19:05:57 +0200 | [diff] [blame] | 78 | * steps = (Temp - TempBase) / 785 |
Daniel Lezcano | 48880b9 | 2017-10-19 19:05:46 +0200 | [diff] [blame] | 79 | * |
| 80 | */ |
| 81 | static inline int hisi_thermal_step_to_temp(int step) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 82 | { |
Daniel Lezcano | 48880b9 | 2017-10-19 19:05:46 +0200 | [diff] [blame] | 83 | return HISI_TEMP_BASE + (step * HISI_TEMP_STEP); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 84 | } |
| 85 | |
Daniel Lezcano | bc02ef6 | 2017-10-19 19:05:53 +0200 | [diff] [blame] | 86 | static inline int hisi_thermal_temp_to_step(int temp) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 87 | { |
Kevin Wangtao | e42bbe1 | 2017-10-19 19:05:57 +0200 | [diff] [blame] | 88 | return DIV_ROUND_UP(temp - HISI_TEMP_BASE, HISI_TEMP_STEP); |
Daniel Lezcano | db2b033 | 2017-10-19 19:05:47 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 91 | /* |
| 92 | * The lag register contains 5 bits encoding the temperature in steps. |
| 93 | * |
| 94 | * Each time the temperature crosses the threshold boundary, an |
| 95 | * interrupt is raised. It could be when the temperature is going |
| 96 | * above the threshold or below. However, if the temperature is |
| 97 | * fluctuating around this value due to the load, we can receive |
| 98 | * several interrupts which may not desired. |
| 99 | * |
| 100 | * We can setup a temperature representing the delta between the |
| 101 | * threshold and the current temperature when the temperature is |
| 102 | * decreasing. |
| 103 | * |
| 104 | * For instance: the lag register is 5°C, the threshold is 65°C, when |
| 105 | * the temperature reaches 65°C an interrupt is raised and when the |
| 106 | * temperature decrease to 65°C - 5°C another interrupt is raised. |
| 107 | * |
| 108 | * A very short lag can lead to an interrupt storm, a long lag |
| 109 | * increase the latency to react to the temperature changes. In our |
| 110 | * case, that is not really a problem as we are polling the |
| 111 | * temperature. |
| 112 | * |
| 113 | * [0:4] : lag register |
| 114 | * |
| 115 | * The temperature is coded in steps, cf. HISI_TEMP_STEP. |
| 116 | * |
| 117 | * Min : 0x00 : 0.0 °C |
| 118 | * Max : 0x1F : 24.3 °C |
| 119 | * |
| 120 | * The 'value' parameter is in milliCelsius. |
| 121 | */ |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 122 | static inline void hisi_thermal_set_lag(void __iomem *addr, int value) |
| 123 | { |
Kevin Wangtao | e42bbe1 | 2017-10-19 19:05:57 +0200 | [diff] [blame] | 124 | writel(DIV_ROUND_UP(value, HISI_TEMP_STEP) & 0x1F, addr + TEMP0_LAG); |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static inline void hisi_thermal_alarm_clear(void __iomem *addr, int value) |
| 128 | { |
| 129 | writel(value, addr + TEMP0_INT_CLR); |
| 130 | } |
| 131 | |
| 132 | static inline void hisi_thermal_alarm_enable(void __iomem *addr, int value) |
| 133 | { |
| 134 | writel(value, addr + TEMP0_INT_EN); |
| 135 | } |
| 136 | |
| 137 | static inline void hisi_thermal_alarm_set(void __iomem *addr, int temp) |
| 138 | { |
| 139 | writel(hisi_thermal_temp_to_step(temp) | 0x0FFFFFF00, addr + TEMP0_TH); |
| 140 | } |
| 141 | |
| 142 | static inline void hisi_thermal_reset_set(void __iomem *addr, int temp) |
| 143 | { |
| 144 | writel(hisi_thermal_temp_to_step(temp), addr + TEMP0_RST_TH); |
| 145 | } |
| 146 | |
| 147 | static inline void hisi_thermal_reset_enable(void __iomem *addr, int value) |
| 148 | { |
| 149 | writel(value, addr + TEMP0_RST_MSK); |
| 150 | } |
| 151 | |
| 152 | static inline void hisi_thermal_enable(void __iomem *addr, int value) |
| 153 | { |
| 154 | writel(value, addr + TEMP0_EN); |
| 155 | } |
| 156 | |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 157 | static inline int hisi_thermal_get_temperature(void __iomem *addr) |
| 158 | { |
| 159 | return hisi_thermal_step_to_temp(readl(addr + TEMP0_VALUE)); |
| 160 | } |
| 161 | |
Daniel Lezcano | b424315 | 2017-10-19 19:05:50 +0200 | [diff] [blame] | 162 | /* |
| 163 | * Temperature configuration register - Sensor selection |
| 164 | * |
| 165 | * Bits [19:12] |
| 166 | * |
| 167 | * 0x0: local sensor (default) |
| 168 | * 0x1: remote sensor 1 (ACPU cluster 1) |
| 169 | * 0x2: remote sensor 2 (ACPU cluster 0) |
| 170 | * 0x3: remote sensor 3 (G3D) |
| 171 | */ |
| 172 | static inline void hisi_thermal_sensor_select(void __iomem *addr, int sensor) |
| 173 | { |
| 174 | writel((readl(addr + TEMP0_CFG) & ~TEMP0_CFG_SS_MSK) | |
| 175 | (sensor << 12), addr + TEMP0_CFG); |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Temperature configuration register - Hdak conversion polling interval |
| 180 | * |
| 181 | * Bits [5:4] |
| 182 | * |
| 183 | * 0x0 : 0.768 ms |
| 184 | * 0x1 : 6.144 ms |
| 185 | * 0x2 : 49.152 ms |
| 186 | * 0x3 : 393.216 ms |
| 187 | */ |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 188 | static inline void hisi_thermal_hdak_set(void __iomem *addr, int value) |
| 189 | { |
Daniel Lezcano | b424315 | 2017-10-19 19:05:50 +0200 | [diff] [blame] | 190 | writel((readl(addr + TEMP0_CFG) & ~TEMP0_CFG_HDAK_MSK) | |
| 191 | (value << 4), addr + TEMP0_CFG); |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 192 | } |
| 193 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 194 | static void hisi_thermal_disable_sensor(struct hisi_thermal_data *data) |
| 195 | { |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 196 | /* disable sensor module */ |
Daniel Lezcano | 1e11b01 | 2017-10-19 19:05:49 +0200 | [diff] [blame] | 197 | hisi_thermal_enable(data->regs, 0); |
| 198 | hisi_thermal_alarm_enable(data->regs, 0); |
| 199 | hisi_thermal_reset_enable(data->regs, 0); |
Kevin Wangtao | 943c0f6 | 2017-10-19 19:05:56 +0200 | [diff] [blame] | 200 | |
| 201 | clk_disable_unprepare(data->clk); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 202 | } |
| 203 | |
Kevin Wangtao | a0678da8 | 2017-10-22 10:54:32 +0200 | [diff] [blame^] | 204 | static int hisi_thermal_setup(struct hisi_thermal_data *data) |
| 205 | { |
| 206 | struct hisi_thermal_sensor *sensor = &data->sensor; |
| 207 | int ret; |
| 208 | |
| 209 | /* enable clock for tsensor */ |
| 210 | ret = clk_prepare_enable(data->clk); |
| 211 | if (ret) |
| 212 | return ret; |
| 213 | |
| 214 | /* disable module firstly */ |
| 215 | hisi_thermal_reset_enable(data->regs, 0); |
| 216 | hisi_thermal_enable(data->regs, 0); |
| 217 | |
| 218 | /* select sensor id */ |
| 219 | hisi_thermal_sensor_select(data->regs, sensor->id); |
| 220 | |
| 221 | /* setting the hdak time */ |
| 222 | hisi_thermal_hdak_set(data->regs, 0); |
| 223 | |
| 224 | /* setting lag value between current temp and the threshold */ |
| 225 | hisi_thermal_set_lag(data->regs, HISI_TEMP_LAG); |
| 226 | |
| 227 | /* enable for interrupt */ |
| 228 | hisi_thermal_alarm_set(data->regs, sensor->thres_temp); |
| 229 | |
| 230 | hisi_thermal_reset_set(data->regs, HISI_TEMP_RESET); |
| 231 | |
| 232 | /* enable module */ |
| 233 | hisi_thermal_reset_enable(data->regs, 1); |
| 234 | hisi_thermal_enable(data->regs, 1); |
| 235 | |
| 236 | hisi_thermal_alarm_clear(data->regs, 0); |
| 237 | hisi_thermal_alarm_enable(data->regs, 1); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Daniel Lezcano | 81d7cb7 | 2017-10-19 19:05:54 +0200 | [diff] [blame] | 242 | static int hisi_thermal_get_temp(void *__data, int *temp) |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 243 | { |
Daniel Lezcano | 81d7cb7 | 2017-10-19 19:05:54 +0200 | [diff] [blame] | 244 | struct hisi_thermal_data *data = __data; |
| 245 | struct hisi_thermal_sensor *sensor = &data->sensor; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 246 | |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 247 | *temp = hisi_thermal_get_temperature(data->regs); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 248 | |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 249 | dev_dbg(&data->pdev->dev, "id=%d, temp=%d, thres=%d\n", |
| 250 | sensor->id, *temp, sensor->thres_temp); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Julia Lawall | 3fe156f | 2017-08-08 17:08:55 +0200 | [diff] [blame] | 255 | static const struct thermal_zone_of_device_ops hisi_of_thermal_ops = { |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 256 | .get_temp = hisi_thermal_get_temp, |
| 257 | }; |
| 258 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 259 | static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev) |
| 260 | { |
| 261 | struct hisi_thermal_data *data = dev; |
Daniel Lezcano | 609f26d | 2017-10-19 19:05:52 +0200 | [diff] [blame] | 262 | struct hisi_thermal_sensor *sensor = &data->sensor; |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 263 | int temp; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 264 | |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 265 | hisi_thermal_alarm_clear(data->regs, 1); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 266 | |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 267 | temp = hisi_thermal_get_temperature(data->regs); |
| 268 | |
| 269 | if (temp >= sensor->thres_temp) { |
| 270 | dev_crit(&data->pdev->dev, "THERMAL ALARM: %d > %d\n", |
| 271 | temp, sensor->thres_temp); |
| 272 | |
Daniel Lezcano | 609f26d | 2017-10-19 19:05:52 +0200 | [diff] [blame] | 273 | thermal_zone_device_update(data->sensor.tzd, |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 274 | THERMAL_EVENT_UNSPECIFIED); |
| 275 | |
| 276 | } else if (temp < sensor->thres_temp) { |
| 277 | dev_crit(&data->pdev->dev, "THERMAL ALARM stopped: %d < %d\n", |
| 278 | temp, sensor->thres_temp); |
| 279 | } |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 280 | |
| 281 | return IRQ_HANDLED; |
| 282 | } |
| 283 | |
| 284 | static int hisi_thermal_register_sensor(struct platform_device *pdev, |
| 285 | struct hisi_thermal_data *data, |
| 286 | struct hisi_thermal_sensor *sensor, |
| 287 | int index) |
| 288 | { |
| 289 | int ret, i; |
| 290 | const struct thermal_trip *trip; |
| 291 | |
| 292 | sensor->id = index; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 293 | |
Eduardo Valentin | 44a520d | 2016-03-09 13:07:13 -0800 | [diff] [blame] | 294 | sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, |
Daniel Lezcano | 81d7cb7 | 2017-10-19 19:05:54 +0200 | [diff] [blame] | 295 | sensor->id, data, |
| 296 | &hisi_of_thermal_ops); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 297 | if (IS_ERR(sensor->tzd)) { |
| 298 | ret = PTR_ERR(sensor->tzd); |
Leo Yan | 439dc96 | 2016-03-29 19:27:12 +0800 | [diff] [blame] | 299 | sensor->tzd = NULL; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 300 | dev_err(&pdev->dev, "failed to register sensor id %d: %d\n", |
| 301 | sensor->id, ret); |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | trip = of_thermal_get_trip_points(sensor->tzd); |
| 306 | |
| 307 | for (i = 0; i < of_thermal_get_ntrips(sensor->tzd); i++) { |
| 308 | if (trip[i].type == THERMAL_TRIP_PASSIVE) { |
Kevin Wangtao | e42bbe1 | 2017-10-19 19:05:57 +0200 | [diff] [blame] | 309 | sensor->thres_temp = trip[i].temperature; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 310 | break; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static const struct of_device_id of_hisi_thermal_match[] = { |
| 318 | { .compatible = "hisilicon,tsensor" }, |
| 319 | { /* end */ } |
| 320 | }; |
| 321 | MODULE_DEVICE_TABLE(of, of_hisi_thermal_match); |
| 322 | |
| 323 | static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor, |
| 324 | bool on) |
| 325 | { |
| 326 | struct thermal_zone_device *tzd = sensor->tzd; |
| 327 | |
| 328 | tzd->ops->set_mode(tzd, |
| 329 | on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED); |
| 330 | } |
| 331 | |
| 332 | static int hisi_thermal_probe(struct platform_device *pdev) |
| 333 | { |
| 334 | struct hisi_thermal_data *data; |
| 335 | struct resource *res; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 336 | int ret; |
| 337 | |
| 338 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 339 | if (!data) |
| 340 | return -ENOMEM; |
| 341 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 342 | data->pdev = pdev; |
| 343 | |
| 344 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 345 | data->regs = devm_ioremap_resource(&pdev->dev, res); |
| 346 | if (IS_ERR(data->regs)) { |
| 347 | dev_err(&pdev->dev, "failed to get io address\n"); |
| 348 | return PTR_ERR(data->regs); |
| 349 | } |
| 350 | |
| 351 | data->irq = platform_get_irq(pdev, 0); |
| 352 | if (data->irq < 0) |
| 353 | return data->irq; |
| 354 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 355 | platform_set_drvdata(pdev, data); |
| 356 | |
| 357 | data->clk = devm_clk_get(&pdev->dev, "thermal_clk"); |
| 358 | if (IS_ERR(data->clk)) { |
| 359 | ret = PTR_ERR(data->clk); |
| 360 | if (ret != -EPROBE_DEFER) |
| 361 | dev_err(&pdev->dev, |
| 362 | "failed to get thermal clk: %d\n", ret); |
| 363 | return ret; |
| 364 | } |
| 365 | |
Daniel Lezcano | ff4ec29 | 2017-10-19 19:05:44 +0200 | [diff] [blame] | 366 | ret = hisi_thermal_register_sensor(pdev, data, |
Daniel Lezcano | 609f26d | 2017-10-19 19:05:52 +0200 | [diff] [blame] | 367 | &data->sensor, |
Daniel Lezcano | ff4ec29 | 2017-10-19 19:05:44 +0200 | [diff] [blame] | 368 | HISI_DEFAULT_SENSOR); |
| 369 | if (ret) { |
| 370 | dev_err(&pdev->dev, "failed to register thermal sensor: %d\n", |
| 371 | ret); |
| 372 | return ret; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 373 | } |
| 374 | |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 375 | ret = hisi_thermal_setup(data); |
| 376 | if (ret) { |
| 377 | dev_err(&pdev->dev, "Failed to setup the sensor: %d\n", ret); |
| 378 | return ret; |
| 379 | } |
Daniel Lezcano | ff4ec29 | 2017-10-19 19:05:44 +0200 | [diff] [blame] | 380 | |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 381 | ret = devm_request_threaded_irq(&pdev->dev, data->irq, NULL, |
Daniel Lezcano | 2cb4de7 | 2017-10-19 19:05:45 +0200 | [diff] [blame] | 382 | hisi_thermal_alarm_irq_thread, |
Daniel Lezcano | 10d7e9a | 2017-10-19 19:05:51 +0200 | [diff] [blame] | 383 | IRQF_ONESHOT, "hisi_thermal", data); |
Daniel Lezcano | 2cb4de7 | 2017-10-19 19:05:45 +0200 | [diff] [blame] | 384 | if (ret < 0) { |
| 385 | dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret); |
| 386 | return ret; |
| 387 | } |
| 388 | |
Daniel Lezcano | 609f26d | 2017-10-19 19:05:52 +0200 | [diff] [blame] | 389 | hisi_thermal_toggle_sensor(&data->sensor, true); |
Daniel Lezcano | c176b10 | 2017-10-19 19:05:43 +0200 | [diff] [blame] | 390 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 391 | return 0; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static int hisi_thermal_remove(struct platform_device *pdev) |
| 395 | { |
| 396 | struct hisi_thermal_data *data = platform_get_drvdata(pdev); |
Daniel Lezcano | 609f26d | 2017-10-19 19:05:52 +0200 | [diff] [blame] | 397 | struct hisi_thermal_sensor *sensor = &data->sensor; |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 398 | |
Daniel Lezcano | ff4ec29 | 2017-10-19 19:05:44 +0200 | [diff] [blame] | 399 | hisi_thermal_toggle_sensor(sensor, false); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 400 | hisi_thermal_disable_sensor(data); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | #ifdef CONFIG_PM_SLEEP |
| 406 | static int hisi_thermal_suspend(struct device *dev) |
| 407 | { |
| 408 | struct hisi_thermal_data *data = dev_get_drvdata(dev); |
| 409 | |
| 410 | hisi_thermal_disable_sensor(data); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 411 | |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static int hisi_thermal_resume(struct device *dev) |
| 416 | { |
| 417 | struct hisi_thermal_data *data = dev_get_drvdata(dev); |
| 418 | |
Kevin Wangtao | 943c0f6 | 2017-10-19 19:05:56 +0200 | [diff] [blame] | 419 | return hisi_thermal_setup(data); |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 420 | } |
| 421 | #endif |
| 422 | |
| 423 | static SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops, |
| 424 | hisi_thermal_suspend, hisi_thermal_resume); |
| 425 | |
| 426 | static struct platform_driver hisi_thermal_driver = { |
| 427 | .driver = { |
| 428 | .name = "hisi_thermal", |
kongxinwei | 9a5238a | 2015-05-20 19:16:37 +0800 | [diff] [blame] | 429 | .pm = &hisi_thermal_pm_ops, |
| 430 | .of_match_table = of_hisi_thermal_match, |
| 431 | }, |
| 432 | .probe = hisi_thermal_probe, |
| 433 | .remove = hisi_thermal_remove, |
| 434 | }; |
| 435 | |
| 436 | module_platform_driver(hisi_thermal_driver); |
| 437 | |
| 438 | MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>"); |
| 439 | MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>"); |
| 440 | MODULE_DESCRIPTION("Hisilicon thermal driver"); |
| 441 | MODULE_LICENSE("GPL v2"); |