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>
25 #include <linux/pinctrl/consumer.h>
28 * If the temperature over a period of time High,
29 * the resulting TSHUT gave CRU module,let it reset the entire chip,
30 * or via GPIO give PMIC.
38 * the system Temperature Sensors tshut(tshut) polarity
39 * the bit 8 is tshut polarity.
40 * 0: low active, 1: high active
48 * The system has three Temperature Sensors. channel 0 is reserved,
49 * channel 1 is for CPU, and channel 2 is for GPU.
56 struct rockchip_tsadc_chip {
57 /* The hardware-controlled tshut property */
59 enum tshut_mode tshut_mode;
60 enum tshut_polarity tshut_polarity;
62 /* Chip-wide methods */
63 void (*initialize)(void __iomem *reg, enum tshut_polarity p);
64 void (*irq_ack)(void __iomem *reg);
65 void (*control)(void __iomem *reg, bool on);
67 /* Per-sensor methods */
68 int (*get_temp)(int chn, void __iomem *reg, int *temp);
69 void (*set_tshut_temp)(int chn, void __iomem *reg, long temp);
70 void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
73 struct rockchip_thermal_sensor {
74 struct rockchip_thermal_data *thermal;
75 struct thermal_zone_device *tzd;
79 #define NUM_SENSORS 2 /* Ignore unused sensor 0 */
81 struct rockchip_thermal_data {
82 const struct rockchip_tsadc_chip *chip;
83 struct platform_device *pdev;
84 struct reset_control *reset;
86 struct rockchip_thermal_sensor sensors[NUM_SENSORS];
94 enum tshut_mode tshut_mode;
95 enum tshut_polarity tshut_polarity;
98 /* TSADC V2 Sensor info define: */
99 #define TSADCV2_AUTO_CON 0x04
100 #define TSADCV2_INT_EN 0x08
101 #define TSADCV2_INT_PD 0x0c
102 #define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04)
103 #define TSADCV2_COMP_SHUT(chn) (0x40 + (chn) * 0x04)
104 #define TSADCV2_HIGHT_INT_DEBOUNCE 0x60
105 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE 0x64
106 #define TSADCV2_AUTO_PERIOD 0x68
107 #define TSADCV2_AUTO_PERIOD_HT 0x6c
109 #define TSADCV2_AUTO_EN BIT(0)
110 #define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn))
111 #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8)
113 #define TSADCV2_INT_SRC_EN(chn) BIT(chn)
114 #define TSADCV2_SHUT_2GPIO_SRC_EN(chn) BIT(4 + (chn))
115 #define TSADCV2_SHUT_2CRU_SRC_EN(chn) BIT(8 + (chn))
117 #define TSADCV2_INT_PD_CLEAR_MASK ~BIT(8)
119 #define TSADCV2_DATA_MASK 0xfff
120 #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT 4
121 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT 4
122 #define TSADCV2_AUTO_PERIOD_TIME 250 /* msec */
123 #define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* msec */
130 static const struct tsadc_table v2_code_table[] = {
131 {TSADCV2_DATA_MASK, -40000},
168 static u32 rk_tsadcv2_temp_to_code(long temp)
173 high = ARRAY_SIZE(v2_code_table) - 1;
174 mid = (high + low) / 2;
176 if (temp < v2_code_table[low].temp || temp > v2_code_table[high].temp)
179 while (low <= high) {
180 if (temp == v2_code_table[mid].temp)
181 return v2_code_table[mid].code;
182 else if (temp < v2_code_table[mid].temp)
186 mid = (low + high) / 2;
192 static int rk_tsadcv2_code_to_temp(u32 code, int *temp)
194 unsigned int low = 1;
195 unsigned int high = ARRAY_SIZE(v2_code_table) - 1;
196 unsigned int mid = (low + high) / 2;
200 BUILD_BUG_ON(ARRAY_SIZE(v2_code_table) < 2);
202 code &= TSADCV2_DATA_MASK;
203 if (code < v2_code_table[high].code)
204 return -EAGAIN; /* Incorrect reading */
206 while (low <= high) {
207 if (code >= v2_code_table[mid].code &&
208 code < v2_code_table[mid - 1].code)
210 else if (code < v2_code_table[mid].code)
214 mid = (low + high) / 2;
218 * The 5C granularity provided by the table is too much. Let's
219 * assume that the relationship between sensor readings and
220 * temperature between 2 table entries is linear and interpolate
221 * to produce less granular result.
223 num = v2_code_table[mid].temp - v2_code_table[mid - 1].temp;
224 num *= v2_code_table[mid - 1].code - code;
225 denom = v2_code_table[mid - 1].code - v2_code_table[mid].code;
226 *temp = v2_code_table[mid - 1].temp + (num / denom);
232 * rk_tsadcv2_initialize - initialize TASDC Controller
233 * (1) Set TSADCV2_AUTO_PERIOD, configure the interleave between
234 * every two accessing of TSADC in normal operation.
235 * (2) Set TSADCV2_AUTO_PERIOD_HT, configure the interleave between
236 * every two accessing of TSADC after the temperature is higher
237 * than COM_SHUT or COM_INT.
238 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE,
239 * if the temperature is higher than COMP_INT or COMP_SHUT for
240 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
242 static void rk_tsadcv2_initialize(void __iomem *regs,
243 enum tshut_polarity tshut_polarity)
245 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
246 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
247 regs + TSADCV2_AUTO_CON);
249 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
250 regs + TSADCV2_AUTO_CON);
252 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
253 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
254 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
255 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
256 regs + TSADCV2_AUTO_PERIOD_HT);
257 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
258 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
261 static void rk_tsadcv2_irq_ack(void __iomem *regs)
265 val = readl_relaxed(regs + TSADCV2_INT_PD);
266 writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
269 static void rk_tsadcv2_control(void __iomem *regs, bool enable)
273 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
275 val |= TSADCV2_AUTO_EN;
277 val &= ~TSADCV2_AUTO_EN;
279 writel_relaxed(val, regs + TSADCV2_AUTO_CON);
282 static int rk_tsadcv2_get_temp(int chn, void __iomem *regs, int *temp)
286 val = readl_relaxed(regs + TSADCV2_DATA(chn));
288 return rk_tsadcv2_code_to_temp(val, temp);
291 static void rk_tsadcv2_tshut_temp(int chn, void __iomem *regs, long temp)
293 u32 tshut_value, val;
295 tshut_value = rk_tsadcv2_temp_to_code(temp);
296 writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
298 /* TSHUT will be valid */
299 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
300 writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
303 static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
304 enum tshut_mode mode)
308 val = readl_relaxed(regs + TSADCV2_INT_EN);
309 if (mode == TSHUT_MODE_GPIO) {
310 val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
311 val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
313 val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
314 val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
317 writel_relaxed(val, regs + TSADCV2_INT_EN);
320 static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
321 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
322 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
325 .initialize = rk_tsadcv2_initialize,
326 .irq_ack = rk_tsadcv2_irq_ack,
327 .control = rk_tsadcv2_control,
328 .get_temp = rk_tsadcv2_get_temp,
329 .set_tshut_temp = rk_tsadcv2_tshut_temp,
330 .set_tshut_mode = rk_tsadcv2_tshut_mode,
333 static const struct of_device_id of_rockchip_thermal_match[] = {
335 .compatible = "rockchip,rk3288-tsadc",
336 .data = (void *)&rk3288_tsadc_data,
340 MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
343 rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
345 struct thermal_zone_device *tzd = sensor->tzd;
347 tzd->ops->set_mode(tzd,
348 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
351 static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
353 struct rockchip_thermal_data *thermal = dev;
356 dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
358 thermal->chip->irq_ack(thermal->regs);
360 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
361 thermal_zone_device_update(thermal->sensors[i].tzd);
366 static int rockchip_thermal_get_temp(void *_sensor, int *out_temp)
368 struct rockchip_thermal_sensor *sensor = _sensor;
369 struct rockchip_thermal_data *thermal = sensor->thermal;
370 const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
373 retval = tsadc->get_temp(sensor->id, thermal->regs, out_temp);
374 dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n",
375 sensor->id, *out_temp, retval);
380 static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = {
381 .get_temp = rockchip_thermal_get_temp,
384 static int rockchip_configure_from_dt(struct device *dev,
385 struct device_node *np,
386 struct rockchip_thermal_data *thermal)
388 u32 shut_temp, tshut_mode, tshut_polarity;
390 if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
392 "Missing tshut temp property, using default %ld\n",
393 thermal->chip->tshut_temp);
394 thermal->tshut_temp = thermal->chip->tshut_temp;
396 thermal->tshut_temp = shut_temp;
399 if (thermal->tshut_temp > INT_MAX) {
400 dev_err(dev, "Invalid tshut temperature specified: %ld\n",
401 thermal->tshut_temp);
405 if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
407 "Missing tshut mode property, using default (%s)\n",
408 thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
410 thermal->tshut_mode = thermal->chip->tshut_mode;
412 thermal->tshut_mode = tshut_mode;
415 if (thermal->tshut_mode > 1) {
416 dev_err(dev, "Invalid tshut mode specified: %d\n",
417 thermal->tshut_mode);
421 if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
424 "Missing tshut-polarity property, using default (%s)\n",
425 thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
427 thermal->tshut_polarity = thermal->chip->tshut_polarity;
429 thermal->tshut_polarity = tshut_polarity;
432 if (thermal->tshut_polarity > 1) {
433 dev_err(dev, "Invalid tshut-polarity specified: %d\n",
434 thermal->tshut_polarity);
442 rockchip_thermal_register_sensor(struct platform_device *pdev,
443 struct rockchip_thermal_data *thermal,
444 struct rockchip_thermal_sensor *sensor,
447 const struct rockchip_tsadc_chip *tsadc = thermal->chip;
450 tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
451 tsadc->set_tshut_temp(id, thermal->regs, thermal->tshut_temp);
453 sensor->thermal = thermal;
455 sensor->tzd = thermal_zone_of_sensor_register(&pdev->dev, id, sensor,
456 &rockchip_of_thermal_ops);
457 if (IS_ERR(sensor->tzd)) {
458 error = PTR_ERR(sensor->tzd);
459 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
468 * Reset TSADC Controller, reset all tsadc registers.
470 static void rockchip_thermal_reset_controller(struct reset_control *reset)
472 reset_control_assert(reset);
473 usleep_range(10, 20);
474 reset_control_deassert(reset);
477 static int rockchip_thermal_probe(struct platform_device *pdev)
479 struct device_node *np = pdev->dev.of_node;
480 struct rockchip_thermal_data *thermal;
481 const struct of_device_id *match;
482 struct resource *res;
487 match = of_match_node(of_rockchip_thermal_match, np);
491 irq = platform_get_irq(pdev, 0);
493 dev_err(&pdev->dev, "no irq resource?\n");
497 thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
502 thermal->pdev = pdev;
504 thermal->chip = (const struct rockchip_tsadc_chip *)match->data;
508 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
509 thermal->regs = devm_ioremap_resource(&pdev->dev, res);
510 if (IS_ERR(thermal->regs))
511 return PTR_ERR(thermal->regs);
513 thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
514 if (IS_ERR(thermal->reset)) {
515 error = PTR_ERR(thermal->reset);
516 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
520 thermal->clk = devm_clk_get(&pdev->dev, "tsadc");
521 if (IS_ERR(thermal->clk)) {
522 error = PTR_ERR(thermal->clk);
523 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
527 thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
528 if (IS_ERR(thermal->pclk)) {
529 error = PTR_ERR(thermal->pclk);
530 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
535 error = clk_prepare_enable(thermal->clk);
537 dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
542 error = clk_prepare_enable(thermal->pclk);
544 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
545 goto err_disable_clk;
548 rockchip_thermal_reset_controller(thermal->reset);
550 error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
552 dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
554 goto err_disable_pclk;
557 thermal->chip->initialize(thermal->regs, thermal->tshut_polarity);
559 error = rockchip_thermal_register_sensor(pdev, thermal,
560 &thermal->sensors[0],
564 "failed to register CPU thermal sensor: %d\n", error);
565 goto err_disable_pclk;
568 error = rockchip_thermal_register_sensor(pdev, thermal,
569 &thermal->sensors[1],
573 "failed to register GPU thermal sensor: %d\n", error);
574 goto err_unregister_cpu_sensor;
577 error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
578 &rockchip_thermal_alarm_irq_thread,
580 "rockchip_thermal", thermal);
583 "failed to request tsadc irq: %d\n", error);
584 goto err_unregister_gpu_sensor;
587 thermal->chip->control(thermal->regs, true);
589 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
590 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
592 platform_set_drvdata(pdev, thermal);
596 err_unregister_gpu_sensor:
597 thermal_zone_of_sensor_unregister(&pdev->dev, thermal->sensors[1].tzd);
598 err_unregister_cpu_sensor:
599 thermal_zone_of_sensor_unregister(&pdev->dev, thermal->sensors[0].tzd);
601 clk_disable_unprepare(thermal->pclk);
603 clk_disable_unprepare(thermal->clk);
608 static int rockchip_thermal_remove(struct platform_device *pdev)
610 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
613 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++) {
614 struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
616 rockchip_thermal_toggle_sensor(sensor, false);
617 thermal_zone_of_sensor_unregister(&pdev->dev, sensor->tzd);
620 thermal->chip->control(thermal->regs, false);
622 clk_disable_unprepare(thermal->pclk);
623 clk_disable_unprepare(thermal->clk);
628 static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
630 struct platform_device *pdev = to_platform_device(dev);
631 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
634 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
635 rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
637 thermal->chip->control(thermal->regs, false);
639 clk_disable(thermal->pclk);
640 clk_disable(thermal->clk);
642 pinctrl_pm_select_sleep_state(dev);
647 static int __maybe_unused rockchip_thermal_resume(struct device *dev)
649 struct platform_device *pdev = to_platform_device(dev);
650 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
654 error = clk_enable(thermal->clk);
658 error = clk_enable(thermal->pclk);
662 rockchip_thermal_reset_controller(thermal->reset);
664 thermal->chip->initialize(thermal->regs, thermal->tshut_polarity);
666 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++) {
667 enum sensor_id id = thermal->sensors[i].id;
669 thermal->chip->set_tshut_mode(id, thermal->regs,
670 thermal->tshut_mode);
671 thermal->chip->set_tshut_temp(id, thermal->regs,
672 thermal->tshut_temp);
675 thermal->chip->control(thermal->regs, true);
677 for (i = 0; i < ARRAY_SIZE(thermal->sensors); i++)
678 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
680 pinctrl_pm_select_default_state(dev);
685 static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
686 rockchip_thermal_suspend, rockchip_thermal_resume);
688 static struct platform_driver rockchip_thermal_driver = {
690 .name = "rockchip-thermal",
691 .pm = &rockchip_thermal_pm_ops,
692 .of_match_table = of_rockchip_thermal_match,
694 .probe = rockchip_thermal_probe,
695 .remove = rockchip_thermal_remove,
698 module_platform_driver(rockchip_thermal_driver);
700 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
701 MODULE_AUTHOR("Rockchip, Inc.");
702 MODULE_LICENSE("GPL v2");
703 MODULE_ALIAS("platform:rockchip-thermal");