2 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
18 #include <linux/module.h>
20 #include <linux/of_address.h>
21 #include <linux/of_irq.h>
22 #include <linux/platform_device.h>
23 #include <linux/reset.h>
24 #include <linux/thermal.h>
27 * If the temperature over a period of time High,
28 * the resulting TSHUT gave CRU module,let it reset the entire chip,
29 * or via GPIO give PMIC.
37 * the system Temperature Sensors tshut(tshut) polarity
38 * the bit 8 is tshut polarity.
39 * 0: low active, 1: high active
47 * The system has three Temperature Sensors. channel 0 is reserved,
48 * channel 1 is for CPU, and channel 2 is for GPU.
55 struct rockchip_tsadc_chip {
56 /* The hardware-controlled tshut property */
58 enum tshut_mode tshut_mode;
59 enum tshut_polarity tshut_polarity;
61 /* Chip-wide methods */
62 void (*initialize)(void __iomem *reg, enum tshut_polarity p);
63 void (*irq_ack)(void __iomem *reg);
64 void (*control)(void __iomem *reg, bool on);
66 /* Per-sensor methods */
67 int (*get_temp)(int chn, void __iomem *reg, int *temp);
68 void (*set_tshut_temp)(int chn, void __iomem *reg, long temp);
69 void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
72 struct rockchip_thermal_sensor {
73 struct rockchip_thermal_data *thermal;
74 struct thermal_zone_device *tzd;
78 #define NUM_SENSORS 2 /* Ignore unused sensor 0 */
80 struct rockchip_thermal_data {
81 const struct rockchip_tsadc_chip *chip;
82 struct platform_device *pdev;
83 struct reset_control *reset;
85 struct rockchip_thermal_sensor sensors[NUM_SENSORS];
93 enum tshut_mode tshut_mode;
94 enum tshut_polarity tshut_polarity;
97 /* TSADC V2 Sensor info define: */
98 #define TSADCV2_AUTO_CON 0x04
99 #define TSADCV2_INT_EN 0x08
100 #define TSADCV2_INT_PD 0x0c
101 #define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04)
102 #define TSADCV2_COMP_SHUT(chn) (0x40 + (chn) * 0x04)
103 #define TSADCV2_HIGHT_INT_DEBOUNCE 0x60
104 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE 0x64
105 #define TSADCV2_AUTO_PERIOD 0x68
106 #define TSADCV2_AUTO_PERIOD_HT 0x6c
108 #define TSADCV2_AUTO_EN BIT(0)
109 #define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn))
110 #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8)
112 #define TSADCV2_INT_SRC_EN(chn) BIT(chn)
113 #define TSADCV2_SHUT_2GPIO_SRC_EN(chn) BIT(4 + (chn))
114 #define TSADCV2_SHUT_2CRU_SRC_EN(chn) BIT(8 + (chn))
116 #define TSADCV2_INT_PD_CLEAR_MASK ~BIT(8)
118 #define TSADCV2_DATA_MASK 0xfff
119 #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT 4
120 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT 4
121 #define TSADCV2_AUTO_PERIOD_TIME 250 /* msec */
122 #define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* msec */
129 static const struct tsadc_table v2_code_table[] = {
130 {TSADCV2_DATA_MASK, -40000},
167 static u32 rk_tsadcv2_temp_to_code(long temp)
172 high = ARRAY_SIZE(v2_code_table) - 1;
173 mid = (high + low) / 2;
175 if (temp < v2_code_table[low].temp || temp > v2_code_table[high].temp)
178 while (low <= high) {
179 if (temp == v2_code_table[mid].temp)
180 return v2_code_table[mid].code;
181 else if (temp < v2_code_table[mid].temp)
185 mid = (low + high) / 2;
191 static int rk_tsadcv2_code_to_temp(u32 code, int *temp)
193 unsigned int low = 1;
194 unsigned int high = ARRAY_SIZE(v2_code_table) - 1;
195 unsigned int mid = (low + high) / 2;
199 BUILD_BUG_ON(ARRAY_SIZE(v2_code_table) < 2);
201 code &= TSADCV2_DATA_MASK;
202 if (code < v2_code_table[high].code)
203 return -EAGAIN; /* Incorrect reading */
205 while (low <= high) {
206 if (code >= v2_code_table[mid].code &&
207 code < v2_code_table[mid - 1].code)
209 else if (code < v2_code_table[mid].code)
213 mid = (low + high) / 2;
217 * The 5C granularity provided by the table is too much. Let's
218 * assume that the relationship between sensor readings and
219 * temperature between 2 table entries is linear and interpolate
220 * to produce less granular result.
222 num = v2_code_table[mid].temp - v2_code_table[mid - 1].temp;
223 num *= v2_code_table[mid - 1].code - code;
224 denom = v2_code_table[mid - 1].code - v2_code_table[mid].code;
225 *temp = v2_code_table[mid - 1].temp + (num / denom);
231 * rk_tsadcv2_initialize - initialize TASDC Controller
232 * (1) Set TSADCV2_AUTO_PERIOD, configure the interleave between
233 * every two accessing of TSADC in normal operation.
234 * (2) Set TSADCV2_AUTO_PERIOD_HT, configure the interleave between
235 * every two accessing of TSADC after the temperature is higher
236 * than COM_SHUT or COM_INT.
237 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE,
238 * if the temperature is higher than COMP_INT or COMP_SHUT for
239 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
241 static void rk_tsadcv2_initialize(void __iomem *regs,
242 enum tshut_polarity tshut_polarity)
244 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
245 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
246 regs + TSADCV2_AUTO_CON);
248 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
249 regs + TSADCV2_AUTO_CON);
251 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
252 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
253 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
254 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
255 regs + TSADCV2_AUTO_PERIOD_HT);
256 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
257 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
260 static void rk_tsadcv2_irq_ack(void __iomem *regs)
264 val = readl_relaxed(regs + TSADCV2_INT_PD);
265 writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
268 static void rk_tsadcv2_control(void __iomem *regs, bool enable)
272 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
274 val |= TSADCV2_AUTO_EN;
276 val &= ~TSADCV2_AUTO_EN;
278 writel_relaxed(val, regs + TSADCV2_AUTO_CON);
281 static int rk_tsadcv2_get_temp(int chn, void __iomem *regs, int *temp)
285 val = readl_relaxed(regs + TSADCV2_DATA(chn));
287 return rk_tsadcv2_code_to_temp(val, temp);
290 static void rk_tsadcv2_tshut_temp(int chn, void __iomem *regs, long temp)
292 u32 tshut_value, val;
294 tshut_value = rk_tsadcv2_temp_to_code(temp);
295 writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
297 /* TSHUT will be valid */
298 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
299 writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
302 static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
303 enum tshut_mode mode)
307 val = readl_relaxed(regs + TSADCV2_INT_EN);
308 if (mode == TSHUT_MODE_GPIO) {
309 val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
310 val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
312 val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
313 val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
316 writel_relaxed(val, regs + TSADCV2_INT_EN);
319 static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
320 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
321 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
324 .initialize = rk_tsadcv2_initialize,
325 .irq_ack = rk_tsadcv2_irq_ack,
326 .control = rk_tsadcv2_control,
327 .get_temp = rk_tsadcv2_get_temp,
328 .set_tshut_temp = rk_tsadcv2_tshut_temp,
329 .set_tshut_mode = rk_tsadcv2_tshut_mode,
332 static const struct of_device_id of_rockchip_thermal_match[] = {
334 .compatible = "rockchip,rk3288-tsadc",
335 .data = (void *)&rk3288_tsadc_data,
339 MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
342 rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
344 struct thermal_zone_device *tzd = sensor->tzd;
346 tzd->ops->set_mode(tzd,
347 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
350 static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
352 struct rockchip_thermal_data *thermal = dev;
355 dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
357 thermal->chip->irq_ack(thermal->regs);
359 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
360 thermal_zone_device_update(thermal->sensors[i].tzd);
365 static int rockchip_thermal_get_temp(void *_sensor, int *out_temp)
367 struct rockchip_thermal_sensor *sensor = _sensor;
368 struct rockchip_thermal_data *thermal = sensor->thermal;
369 const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
372 retval = tsadc->get_temp(sensor->id, thermal->regs, out_temp);
373 dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n",
374 sensor->id, *out_temp, retval);
379 static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = {
380 .get_temp = rockchip_thermal_get_temp,
383 static int rockchip_configure_from_dt(struct device *dev,
384 struct device_node *np,
385 struct rockchip_thermal_data *thermal)
387 u32 shut_temp, tshut_mode, tshut_polarity;
389 if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
391 "Missing tshut temp property, using default %ld\n",
392 thermal->chip->tshut_temp);
393 thermal->tshut_temp = thermal->chip->tshut_temp;
395 thermal->tshut_temp = shut_temp;
398 if (thermal->tshut_temp > INT_MAX) {
399 dev_err(dev, "Invalid tshut temperature specified: %ld\n",
400 thermal->tshut_temp);
404 if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
406 "Missing tshut mode property, using default (%s)\n",
407 thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
409 thermal->tshut_mode = thermal->chip->tshut_mode;
411 thermal->tshut_mode = tshut_mode;
414 if (thermal->tshut_mode > 1) {
415 dev_err(dev, "Invalid tshut mode specified: %d\n",
416 thermal->tshut_mode);
420 if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
423 "Missing tshut-polarity property, using default (%s)\n",
424 thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
426 thermal->tshut_polarity = thermal->chip->tshut_polarity;
428 thermal->tshut_polarity = tshut_polarity;
431 if (thermal->tshut_polarity > 1) {
432 dev_err(dev, "Invalid tshut-polarity specified: %d\n",
433 thermal->tshut_polarity);
441 rockchip_thermal_register_sensor(struct platform_device *pdev,
442 struct rockchip_thermal_data *thermal,
443 struct rockchip_thermal_sensor *sensor,
446 const struct rockchip_tsadc_chip *tsadc = thermal->chip;
449 tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
450 tsadc->set_tshut_temp(id, thermal->regs, thermal->tshut_temp);
452 sensor->thermal = thermal;
454 sensor->tzd = thermal_zone_of_sensor_register(&pdev->dev, id, sensor,
455 &rockchip_of_thermal_ops);
456 if (IS_ERR(sensor->tzd)) {
457 error = PTR_ERR(sensor->tzd);
458 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
467 * Reset TSADC Controller, reset all tsadc registers.
469 static void rockchip_thermal_reset_controller(struct reset_control *reset)
471 reset_control_assert(reset);
472 usleep_range(10, 20);
473 reset_control_deassert(reset);
476 static int rockchip_thermal_probe(struct platform_device *pdev)
478 struct device_node *np = pdev->dev.of_node;
479 struct rockchip_thermal_data *thermal;
480 const struct of_device_id *match;
481 struct resource *res;
486 match = of_match_node(of_rockchip_thermal_match, np);
490 irq = platform_get_irq(pdev, 0);
492 dev_err(&pdev->dev, "no irq resource?\n");
496 thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
501 thermal->pdev = pdev;
503 thermal->chip = (const struct rockchip_tsadc_chip *)match->data;
507 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
508 thermal->regs = devm_ioremap_resource(&pdev->dev, res);
509 if (IS_ERR(thermal->regs))
510 return PTR_ERR(thermal->regs);
512 thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
513 if (IS_ERR(thermal->reset)) {
514 error = PTR_ERR(thermal->reset);
515 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
519 thermal->clk = devm_clk_get(&pdev->dev, "tsadc");
520 if (IS_ERR(thermal->clk)) {
521 error = PTR_ERR(thermal->clk);
522 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
526 thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
527 if (IS_ERR(thermal->pclk)) {
528 error = PTR_ERR(thermal->pclk);
529 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
534 error = clk_prepare_enable(thermal->clk);
536 dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
541 error = clk_prepare_enable(thermal->pclk);
543 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
544 goto err_disable_clk;
547 rockchip_thermal_reset_controller(thermal->reset);
549 error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
551 dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
553 goto err_disable_pclk;
556 thermal->chip->initialize(thermal->regs, thermal->tshut_polarity);
558 error = rockchip_thermal_register_sensor(pdev, thermal,
559 &thermal->sensors[0],
563 "failed to register CPU thermal sensor: %d\n", error);
564 goto err_disable_pclk;
567 error = rockchip_thermal_register_sensor(pdev, thermal,
568 &thermal->sensors[1],
572 "failed to register GPU thermal sensor: %d\n", error);
573 goto err_unregister_cpu_sensor;
576 error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
577 &rockchip_thermal_alarm_irq_thread,
579 "rockchip_thermal", thermal);
582 "failed to request tsadc irq: %d\n", error);
583 goto err_unregister_gpu_sensor;
586 thermal->chip->control(thermal->regs, true);
588 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
589 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
591 platform_set_drvdata(pdev, thermal);
595 err_unregister_gpu_sensor:
596 thermal_zone_of_sensor_unregister(&pdev->dev, thermal->sensors[1].tzd);
597 err_unregister_cpu_sensor:
598 thermal_zone_of_sensor_unregister(&pdev->dev, thermal->sensors[0].tzd);
600 clk_disable_unprepare(thermal->pclk);
602 clk_disable_unprepare(thermal->clk);
607 static int rockchip_thermal_remove(struct platform_device *pdev)
609 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
612 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++) {
613 struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
615 rockchip_thermal_toggle_sensor(sensor, false);
616 thermal_zone_of_sensor_unregister(&pdev->dev, sensor->tzd);
619 thermal->chip->control(thermal->regs, false);
621 clk_disable_unprepare(thermal->pclk);
622 clk_disable_unprepare(thermal->clk);
627 static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
629 struct platform_device *pdev = to_platform_device(dev);
630 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
633 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
634 rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
636 thermal->chip->control(thermal->regs, false);
638 clk_disable(thermal->pclk);
639 clk_disable(thermal->clk);
644 static int __maybe_unused rockchip_thermal_resume(struct device *dev)
646 struct platform_device *pdev = to_platform_device(dev);
647 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
651 error = clk_enable(thermal->clk);
655 error = clk_enable(thermal->pclk);
659 rockchip_thermal_reset_controller(thermal->reset);
661 thermal->chip->initialize(thermal->regs, thermal->tshut_polarity);
663 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++) {
664 enum sensor_id id = thermal->sensors[i].id;
666 thermal->chip->set_tshut_mode(id, thermal->regs,
667 thermal->tshut_mode);
668 thermal->chip->set_tshut_temp(id, thermal->regs,
669 thermal->tshut_temp);
672 thermal->chip->control(thermal->regs, true);
674 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
675 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
680 static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
681 rockchip_thermal_suspend, rockchip_thermal_resume);
683 static struct platform_driver rockchip_thermal_driver = {
685 .name = "rockchip-thermal",
686 .pm = &rockchip_thermal_pm_ops,
687 .of_match_table = of_rockchip_thermal_match,
689 .probe = rockchip_thermal_probe,
690 .remove = rockchip_thermal_remove,
693 module_platform_driver(rockchip_thermal_driver);
695 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
696 MODULE_AUTHOR("Rockchip, Inc.");
697 MODULE_LICENSE("GPL v2");
698 MODULE_ALIAS("platform:rockchip-thermal");