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, long *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_DISABLE ~BIT(0)
110 #define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn))
111 #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8)
112 #define TSADCV2_AUTO_TSHUT_POLARITY_LOW ~BIT(8)
114 #define TSADCV2_INT_SRC_EN(chn) BIT(chn)
115 #define TSADCV2_SHUT_2GPIO_SRC_EN(chn) BIT(4 + (chn))
116 #define TSADCV2_SHUT_2CRU_SRC_EN(chn) BIT(8 + (chn))
118 #define TSADCV2_INT_PD_CLEAR ~BIT(8)
120 #define TSADCV2_DATA_MASK 0xfff
121 #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT 4
122 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT 4
123 #define TSADCV2_AUTO_PERIOD_TIME 250 /* msec */
124 #define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* msec */
131 static const struct tsadc_table v2_code_table[] = {
132 {TSADCV2_DATA_MASK, -40000},
170 static u32 rk_tsadcv2_temp_to_code(long temp)
175 high = ARRAY_SIZE(v2_code_table) - 1;
176 mid = (high + low) / 2;
178 if (temp < v2_code_table[low].temp || temp > v2_code_table[high].temp)
181 while (low <= high) {
182 if (temp == v2_code_table[mid].temp)
183 return v2_code_table[mid].code;
184 else if (temp < v2_code_table[mid].temp)
188 mid = (low + high) / 2;
194 static long rk_tsadcv2_code_to_temp(u32 code)
199 high = ARRAY_SIZE(v2_code_table) - 1;
200 mid = (high + low) / 2;
202 if (code > v2_code_table[low].code || code < v2_code_table[high].code)
203 return 125000; /* No code available, return max temperature */
205 while (low <= high) {
206 if (code >= v2_code_table[mid].code && code <
207 v2_code_table[mid - 1].code)
208 return v2_code_table[mid].temp;
209 else if (code < v2_code_table[mid].code)
213 mid = (low + high) / 2;
220 * rk_tsadcv2_initialize - initialize TASDC Controller
221 * (1) Set TSADCV2_AUTO_PERIOD, configure the interleave between
222 * every two accessing of TSADC in normal operation.
223 * (2) Set TSADCV2_AUTO_PERIOD_HT, configure the interleave between
224 * every two accessing of TSADC after the temperature is higher
225 * than COM_SHUT or COM_INT.
226 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE,
227 * if the temperature is higher than COMP_INT or COMP_SHUT for
228 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
230 static void rk_tsadcv2_initialize(void __iomem *regs,
231 enum tshut_polarity tshut_polarity)
233 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
234 writel_relaxed(0 | (TSADCV2_AUTO_TSHUT_POLARITY_HIGH),
235 regs + TSADCV2_AUTO_CON);
237 writel_relaxed(0 | (TSADCV2_AUTO_TSHUT_POLARITY_LOW),
238 regs + TSADCV2_AUTO_CON);
240 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
241 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
242 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
243 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
244 regs + TSADCV2_AUTO_PERIOD_HT);
245 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
246 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
249 static void rk_tsadcv2_irq_ack(void __iomem *regs)
253 val = readl_relaxed(regs + TSADCV2_INT_PD);
254 writel_relaxed(val & TSADCV2_INT_PD_CLEAR, regs + TSADCV2_INT_PD);
257 static void rk_tsadcv2_control(void __iomem *regs, bool enable)
261 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
263 val |= TSADCV2_AUTO_EN;
265 val &= ~TSADCV2_AUTO_EN;
267 writel_relaxed(val, regs + TSADCV2_AUTO_CON);
270 static int rk_tsadcv2_get_temp(int chn, void __iomem *regs, long *temp)
274 /* the A/D value of the channel last conversion need some time */
275 val = readl_relaxed(regs + TSADCV2_DATA(chn));
279 *temp = rk_tsadcv2_code_to_temp(val);
284 static void rk_tsadcv2_tshut_temp(int chn, void __iomem *regs, long temp)
286 u32 tshut_value, val;
288 tshut_value = rk_tsadcv2_temp_to_code(temp);
289 writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
291 /* TSHUT will be valid */
292 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
293 writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
296 static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
297 enum tshut_mode mode)
301 val = readl_relaxed(regs + TSADCV2_INT_EN);
302 if (mode == TSHUT_MODE_GPIO) {
303 val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
304 val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
306 val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
307 val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
310 writel_relaxed(val, regs + TSADCV2_INT_EN);
313 static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
314 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
315 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
318 .initialize = rk_tsadcv2_initialize,
319 .irq_ack = rk_tsadcv2_irq_ack,
320 .control = rk_tsadcv2_control,
321 .get_temp = rk_tsadcv2_get_temp,
322 .set_tshut_temp = rk_tsadcv2_tshut_temp,
323 .set_tshut_mode = rk_tsadcv2_tshut_mode,
326 static const struct of_device_id of_rockchip_thermal_match[] = {
328 .compatible = "rockchip,rk3288-tsadc",
329 .data = (void *)&rk3288_tsadc_data,
333 MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
336 rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
338 struct thermal_zone_device *tzd = sensor->tzd;
340 tzd->ops->set_mode(tzd,
341 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
344 static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
346 struct rockchip_thermal_data *thermal = dev;
349 dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
351 thermal->chip->irq_ack(thermal->regs);
353 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
354 thermal_zone_device_update(thermal->sensors[i].tzd);
359 static int rockchip_thermal_get_temp(void *_sensor, long *out_temp)
361 struct rockchip_thermal_sensor *sensor = _sensor;
362 struct rockchip_thermal_data *thermal = sensor->thermal;
363 const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
366 retval = tsadc->get_temp(sensor->id, thermal->regs, out_temp);
367 dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %ld, retval: %d\n",
368 sensor->id, *out_temp, retval);
373 static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = {
374 .get_temp = rockchip_thermal_get_temp,
377 static int rockchip_configure_from_dt(struct device *dev,
378 struct device_node *np,
379 struct rockchip_thermal_data *thermal)
381 u32 shut_temp, tshut_mode, tshut_polarity;
383 if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
385 "Missing tshut temp property, using default %ld\n",
386 thermal->chip->tshut_temp);
387 thermal->tshut_temp = thermal->chip->tshut_temp;
389 thermal->tshut_temp = shut_temp;
392 if (thermal->tshut_temp > INT_MAX) {
393 dev_err(dev, "Invalid tshut temperature specified: %ld\n",
394 thermal->tshut_temp);
398 if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
400 "Missing tshut mode property, using default (%s)\n",
401 thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
403 thermal->tshut_mode = thermal->chip->tshut_mode;
405 thermal->tshut_mode = tshut_mode;
408 if (thermal->tshut_mode > 1) {
409 dev_err(dev, "Invalid tshut mode specified: %d\n",
410 thermal->tshut_mode);
414 if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
417 "Missing tshut-polarity property, using default (%s)\n",
418 thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
420 thermal->tshut_polarity = thermal->chip->tshut_polarity;
422 thermal->tshut_polarity = tshut_polarity;
425 if (thermal->tshut_polarity > 1) {
426 dev_err(dev, "Invalid tshut-polarity specified: %d\n",
427 thermal->tshut_polarity);
435 rockchip_thermal_register_sensor(struct platform_device *pdev,
436 struct rockchip_thermal_data *thermal,
437 struct rockchip_thermal_sensor *sensor,
440 const struct rockchip_tsadc_chip *tsadc = thermal->chip;
443 tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
444 tsadc->set_tshut_temp(id, thermal->regs, thermal->tshut_temp);
446 sensor->thermal = thermal;
448 sensor->tzd = thermal_zone_of_sensor_register(&pdev->dev, id, sensor,
449 &rockchip_of_thermal_ops);
450 if (IS_ERR(sensor->tzd)) {
451 error = PTR_ERR(sensor->tzd);
452 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
461 * Reset TSADC Controller, reset all tsadc registers.
463 static void rockchip_thermal_reset_controller(struct reset_control *reset)
465 reset_control_assert(reset);
466 usleep_range(10, 20);
467 reset_control_deassert(reset);
470 static int rockchip_thermal_probe(struct platform_device *pdev)
472 struct device_node *np = pdev->dev.of_node;
473 struct rockchip_thermal_data *thermal;
474 const struct of_device_id *match;
475 struct resource *res;
480 match = of_match_node(of_rockchip_thermal_match, np);
484 irq = platform_get_irq(pdev, 0);
486 dev_err(&pdev->dev, "no irq resource?\n");
490 thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
495 thermal->pdev = pdev;
497 thermal->chip = (const struct rockchip_tsadc_chip *)match->data;
501 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
502 thermal->regs = devm_ioremap_resource(&pdev->dev, res);
503 if (IS_ERR(thermal->regs))
504 return PTR_ERR(thermal->regs);
506 thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
507 if (IS_ERR(thermal->reset)) {
508 error = PTR_ERR(thermal->reset);
509 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
513 thermal->clk = devm_clk_get(&pdev->dev, "tsadc");
514 if (IS_ERR(thermal->clk)) {
515 error = PTR_ERR(thermal->clk);
516 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
520 thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
521 if (IS_ERR(thermal->pclk)) {
522 error = PTR_ERR(thermal->clk);
523 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
528 error = clk_prepare_enable(thermal->clk);
530 dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
535 error = clk_prepare_enable(thermal->pclk);
537 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
538 goto err_disable_clk;
541 rockchip_thermal_reset_controller(thermal->reset);
543 error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
545 dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
547 goto err_disable_pclk;
550 thermal->chip->initialize(thermal->regs, thermal->tshut_polarity);
552 error = rockchip_thermal_register_sensor(pdev, thermal,
553 &thermal->sensors[0],
557 "failed to register CPU thermal sensor: %d\n", error);
558 goto err_disable_pclk;
561 error = rockchip_thermal_register_sensor(pdev, thermal,
562 &thermal->sensors[1],
566 "failed to register GPU thermal sensor: %d\n", error);
567 goto err_unregister_cpu_sensor;
570 error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
571 &rockchip_thermal_alarm_irq_thread,
573 "rockchip_thermal", thermal);
576 "failed to request tsadc irq: %d\n", error);
577 goto err_unregister_gpu_sensor;
580 thermal->chip->control(thermal->regs, true);
582 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
583 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
585 platform_set_drvdata(pdev, thermal);
589 err_unregister_gpu_sensor:
590 thermal_zone_of_sensor_unregister(&pdev->dev, thermal->sensors[1].tzd);
591 err_unregister_cpu_sensor:
592 thermal_zone_of_sensor_unregister(&pdev->dev, thermal->sensors[0].tzd);
594 clk_disable_unprepare(thermal->pclk);
596 clk_disable_unprepare(thermal->clk);
601 static int rockchip_thermal_remove(struct platform_device *pdev)
603 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
606 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++) {
607 struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
609 rockchip_thermal_toggle_sensor(sensor, false);
610 thermal_zone_of_sensor_unregister(&pdev->dev, sensor->tzd);
613 thermal->chip->control(thermal->regs, false);
615 clk_disable_unprepare(thermal->pclk);
616 clk_disable_unprepare(thermal->clk);
621 static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
623 struct platform_device *pdev = to_platform_device(dev);
624 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
627 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
628 rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
630 thermal->chip->control(thermal->regs, false);
632 clk_disable(thermal->pclk);
633 clk_disable(thermal->clk);
638 static int __maybe_unused rockchip_thermal_resume(struct device *dev)
640 struct platform_device *pdev = to_platform_device(dev);
641 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
645 error = clk_enable(thermal->clk);
649 error = clk_enable(thermal->pclk);
653 rockchip_thermal_reset_controller(thermal->reset);
655 thermal->chip->initialize(thermal->regs, thermal->tshut_polarity);
657 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++) {
658 enum sensor_id id = thermal->sensors[i].id;
660 thermal->chip->set_tshut_mode(id, thermal->regs,
661 thermal->tshut_mode);
662 thermal->chip->set_tshut_temp(id, thermal->regs,
663 thermal->tshut_temp);
666 thermal->chip->control(thermal->regs, true);
668 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
669 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
674 static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
675 rockchip_thermal_suspend, rockchip_thermal_resume);
677 static struct platform_driver rockchip_thermal_driver = {
679 .name = "rockchip-thermal",
680 .pm = &rockchip_thermal_pm_ops,
681 .of_match_table = of_rockchip_thermal_match,
683 .probe = rockchip_thermal_probe,
684 .remove = rockchip_thermal_remove,
687 module_platform_driver(rockchip_thermal_driver);
689 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
690 MODULE_AUTHOR("Rockchip, Inc.");
691 MODULE_LICENSE("GPL v2");
692 MODULE_ALIAS("platform:rockchip-thermal");