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