2 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
4 * Copyright (c) 2015, Fuzhou Rockchip Electronics Co., Ltd
5 * Caesar Wang <wxt@rock-chips.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/interrupt.h>
21 #include <linux/module.h>
23 #include <linux/of_address.h>
24 #include <linux/of_irq.h>
25 #include <linux/platform_device.h>
26 #include <linux/reset.h>
27 #include <linux/thermal.h>
28 #include <linux/pinctrl/consumer.h>
31 * If the temperature over a period of time High,
32 * the resulting TSHUT gave CRU module,let it reset the entire chip,
33 * or via GPIO give PMIC.
41 * the system Temperature Sensors tshut(tshut) polarity
42 * the bit 8 is tshut polarity.
43 * 0: low active, 1: high active
51 * The system has two Temperature Sensors.
52 * sensor0 is for CPU, and sensor1 is for GPU.
60 * The conversion table has the adc value and temperature.
61 * ADC_DECREMENT is the adc value decremnet.(e.g. v2_code_table)
62 * ADC_INCREMNET is the adc value incremnet.(e.g. v3_code_table)
70 * The max sensors is two in rockchip SoCs.
71 * Two sensors: CPU and GPU sensor.
73 #define SOC_MAX_SENSORS 2
75 struct chip_tsadc_table {
76 const struct tsadc_table *id;
78 /* the array table size*/
81 /* that analogic mask data */
84 /* the sort mode is adc value that increment or decrement in table */
85 enum adc_sort_mode mode;
88 struct rockchip_tsadc_chip {
89 /* The sensor id of chip correspond to the ADC channel */
90 int chn_id[SOC_MAX_SENSORS];
93 /* The hardware-controlled tshut property */
95 enum tshut_mode tshut_mode;
96 enum tshut_polarity tshut_polarity;
98 /* Chip-wide methods */
99 void (*initialize)(void __iomem *reg, enum tshut_polarity p);
100 void (*irq_ack)(void __iomem *reg);
101 void (*control)(void __iomem *reg, bool on);
103 /* Per-sensor methods */
104 int (*get_temp)(struct chip_tsadc_table table,
105 int chn, void __iomem *reg, int *temp);
106 void (*set_tshut_temp)(struct chip_tsadc_table table,
107 int chn, void __iomem *reg, int temp);
108 void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
110 /* Per-table methods */
111 struct chip_tsadc_table table;
114 struct rockchip_thermal_sensor {
115 struct rockchip_thermal_data *thermal;
116 struct thermal_zone_device *tzd;
120 struct rockchip_thermal_data {
121 const struct rockchip_tsadc_chip *chip;
122 struct platform_device *pdev;
123 struct reset_control *reset;
125 struct rockchip_thermal_sensor sensors[SOC_MAX_SENSORS];
133 enum tshut_mode tshut_mode;
134 enum tshut_polarity tshut_polarity;
137 /* TSADC Sensor info define: */
138 #define TSADCV2_AUTO_CON 0x04
139 #define TSADCV2_INT_EN 0x08
140 #define TSADCV2_INT_PD 0x0c
141 #define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04)
142 #define TSADCV2_COMP_SHUT(chn) (0x40 + (chn) * 0x04)
143 #define TSADCV2_HIGHT_INT_DEBOUNCE 0x60
144 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE 0x64
145 #define TSADCV2_AUTO_PERIOD 0x68
146 #define TSADCV2_AUTO_PERIOD_HT 0x6c
148 #define TSADCV2_AUTO_EN BIT(0)
149 #define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn))
150 #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8)
152 #define TSADCV2_INT_SRC_EN(chn) BIT(chn)
153 #define TSADCV2_SHUT_2GPIO_SRC_EN(chn) BIT(4 + (chn))
154 #define TSADCV2_SHUT_2CRU_SRC_EN(chn) BIT(8 + (chn))
156 #define TSADCV2_INT_PD_CLEAR_MASK ~BIT(8)
158 #define TSADCV2_DATA_MASK 0xfff
159 #define TSADCV3_DATA_MASK 0x3ff
161 #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT 4
162 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT 4
163 #define TSADCV2_AUTO_PERIOD_TIME 250 /* msec */
164 #define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* msec */
171 static const struct tsadc_table v2_code_table[] = {
172 {TSADCV2_DATA_MASK, -40000},
209 static const struct tsadc_table v3_code_table[] = {
245 {TSADCV3_DATA_MASK, 125000},
248 static u32 rk_tsadcv2_temp_to_code(struct chip_tsadc_table table,
254 high = table.length - 1;
255 mid = (high + low) / 2;
257 if (temp < table.id[low].temp || temp > table.id[high].temp)
260 while (low <= high) {
261 if (temp == table.id[mid].temp)
262 return table.id[mid].code;
263 else if (temp < table.id[mid].temp)
267 mid = (low + high) / 2;
273 static int rk_tsadcv2_code_to_temp(struct chip_tsadc_table table, u32 code,
276 unsigned int low = 1;
277 unsigned int high = table.length - 1;
278 unsigned int mid = (low + high) / 2;
282 WARN_ON(table.length < 2);
284 switch (table.mode) {
286 code &= table.data_mask;
287 if (code < table.id[high].code)
288 return -EAGAIN; /* Incorrect reading */
290 while (low <= high) {
291 if (code >= table.id[mid].code &&
292 code < table.id[mid - 1].code)
294 else if (code < table.id[mid].code)
299 mid = (low + high) / 2;
303 code &= table.data_mask;
304 if (code < table.id[low].code)
305 return -EAGAIN; /* Incorrect reading */
307 while (low <= high) {
308 if (code >= table.id[mid - 1].code &&
309 code < table.id[mid].code)
311 else if (code > table.id[mid].code)
316 mid = (low + high) / 2;
320 pr_err("Invalid the conversion table\n");
324 * The 5C granularity provided by the table is too much. Let's
325 * assume that the relationship between sensor readings and
326 * temperature between 2 table entries is linear and interpolate
327 * to produce less granular result.
329 num = table.id[mid].temp - v2_code_table[mid - 1].temp;
330 num *= abs(table.id[mid - 1].code - code);
331 denom = abs(table.id[mid - 1].code - table.id[mid].code);
332 *temp = table.id[mid - 1].temp + (num / denom);
338 * rk_tsadcv2_initialize - initialize TASDC Controller.
340 * (1) Set TSADC_V2_AUTO_PERIOD:
341 * Configure the interleave between every two accessing of
342 * TSADC in normal operation.
344 * (2) Set TSADCV2_AUTO_PERIOD_HT:
345 * Configure the interleave between every two accessing of
346 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
348 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
349 * If the temperature is higher than COMP_INT or COMP_SHUT for
350 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
352 static void rk_tsadcv2_initialize(void __iomem *regs,
353 enum tshut_polarity tshut_polarity)
355 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
356 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
357 regs + TSADCV2_AUTO_CON);
359 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
360 regs + TSADCV2_AUTO_CON);
362 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
363 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
364 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
365 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
366 regs + TSADCV2_AUTO_PERIOD_HT);
367 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
368 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
371 static void rk_tsadcv2_irq_ack(void __iomem *regs)
375 val = readl_relaxed(regs + TSADCV2_INT_PD);
376 writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
379 static void rk_tsadcv2_control(void __iomem *regs, bool enable)
383 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
385 val |= TSADCV2_AUTO_EN;
387 val &= ~TSADCV2_AUTO_EN;
389 writel_relaxed(val, regs + TSADCV2_AUTO_CON);
392 static int rk_tsadcv2_get_temp(struct chip_tsadc_table table,
393 int chn, void __iomem *regs, int *temp)
397 val = readl_relaxed(regs + TSADCV2_DATA(chn));
399 return rk_tsadcv2_code_to_temp(table, val, temp);
402 static void rk_tsadcv2_tshut_temp(struct chip_tsadc_table table,
403 int chn, void __iomem *regs, int temp)
405 u32 tshut_value, val;
407 tshut_value = rk_tsadcv2_temp_to_code(table, temp);
408 writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
410 /* TSHUT will be valid */
411 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
412 writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
415 static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
416 enum tshut_mode mode)
420 val = readl_relaxed(regs + TSADCV2_INT_EN);
421 if (mode == TSHUT_MODE_GPIO) {
422 val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
423 val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
425 val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
426 val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
429 writel_relaxed(val, regs + TSADCV2_INT_EN);
432 static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
433 .chn_id[SENSOR_CPU] = 1, /* cpu sensor is channel 1 */
434 .chn_id[SENSOR_GPU] = 2, /* gpu sensor is channel 2 */
435 .chn_num = 2, /* two channels for tsadc */
437 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
438 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
441 .initialize = rk_tsadcv2_initialize,
442 .irq_ack = rk_tsadcv2_irq_ack,
443 .control = rk_tsadcv2_control,
444 .get_temp = rk_tsadcv2_get_temp,
445 .set_tshut_temp = rk_tsadcv2_tshut_temp,
446 .set_tshut_mode = rk_tsadcv2_tshut_mode,
450 .length = ARRAY_SIZE(v2_code_table),
451 .data_mask = TSADCV2_DATA_MASK,
452 .mode = ADC_DECREMENT,
456 static const struct rockchip_tsadc_chip rk3368_tsadc_data = {
457 .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
458 .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
459 .chn_num = 2, /* two channels for tsadc */
461 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
462 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
465 .initialize = rk_tsadcv2_initialize,
466 .irq_ack = rk_tsadcv2_irq_ack,
467 .control = rk_tsadcv2_control,
468 .get_temp = rk_tsadcv2_get_temp,
469 .set_tshut_temp = rk_tsadcv2_tshut_temp,
470 .set_tshut_mode = rk_tsadcv2_tshut_mode,
474 .length = ARRAY_SIZE(v3_code_table),
475 .data_mask = TSADCV3_DATA_MASK,
476 .mode = ADC_INCREMENT,
480 static const struct of_device_id of_rockchip_thermal_match[] = {
482 .compatible = "rockchip,rk3288-tsadc",
483 .data = (void *)&rk3288_tsadc_data,
486 .compatible = "rockchip,rk3368-tsadc",
487 .data = (void *)&rk3368_tsadc_data,
491 MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
494 rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
496 struct thermal_zone_device *tzd = sensor->tzd;
498 tzd->ops->set_mode(tzd,
499 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
502 static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
504 struct rockchip_thermal_data *thermal = dev;
507 dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
509 thermal->chip->irq_ack(thermal->regs);
511 for (i = 0; i < thermal->chip->chn_num; i++)
512 thermal_zone_device_update(thermal->sensors[i].tzd);
517 static int rockchip_thermal_get_temp(void *_sensor, int *out_temp)
519 struct rockchip_thermal_sensor *sensor = _sensor;
520 struct rockchip_thermal_data *thermal = sensor->thermal;
521 const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
524 retval = tsadc->get_temp(tsadc->table,
525 sensor->id, thermal->regs, out_temp);
526 dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n",
527 sensor->id, *out_temp, retval);
532 static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = {
533 .get_temp = rockchip_thermal_get_temp,
536 static int rockchip_configure_from_dt(struct device *dev,
537 struct device_node *np,
538 struct rockchip_thermal_data *thermal)
540 u32 shut_temp, tshut_mode, tshut_polarity;
542 if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
544 "Missing tshut temp property, using default %d\n",
545 thermal->chip->tshut_temp);
546 thermal->tshut_temp = thermal->chip->tshut_temp;
548 if (shut_temp > INT_MAX) {
549 dev_err(dev, "Invalid tshut temperature specified: %d\n",
553 thermal->tshut_temp = shut_temp;
556 if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
558 "Missing tshut mode property, using default (%s)\n",
559 thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
561 thermal->tshut_mode = thermal->chip->tshut_mode;
563 thermal->tshut_mode = tshut_mode;
566 if (thermal->tshut_mode > 1) {
567 dev_err(dev, "Invalid tshut mode specified: %d\n",
568 thermal->tshut_mode);
572 if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
575 "Missing tshut-polarity property, using default (%s)\n",
576 thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
578 thermal->tshut_polarity = thermal->chip->tshut_polarity;
580 thermal->tshut_polarity = tshut_polarity;
583 if (thermal->tshut_polarity > 1) {
584 dev_err(dev, "Invalid tshut-polarity specified: %d\n",
585 thermal->tshut_polarity);
593 rockchip_thermal_register_sensor(struct platform_device *pdev,
594 struct rockchip_thermal_data *thermal,
595 struct rockchip_thermal_sensor *sensor,
598 const struct rockchip_tsadc_chip *tsadc = thermal->chip;
601 tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
602 tsadc->set_tshut_temp(tsadc->table, id, thermal->regs,
603 thermal->tshut_temp);
605 sensor->thermal = thermal;
607 sensor->tzd = thermal_zone_of_sensor_register(&pdev->dev, id, sensor,
608 &rockchip_of_thermal_ops);
609 if (IS_ERR(sensor->tzd)) {
610 error = PTR_ERR(sensor->tzd);
611 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
620 * Reset TSADC Controller, reset all tsadc registers.
622 static void rockchip_thermal_reset_controller(struct reset_control *reset)
624 reset_control_assert(reset);
625 usleep_range(10, 20);
626 reset_control_deassert(reset);
629 static int rockchip_thermal_probe(struct platform_device *pdev)
631 struct device_node *np = pdev->dev.of_node;
632 struct rockchip_thermal_data *thermal;
633 const struct of_device_id *match;
634 struct resource *res;
639 match = of_match_node(of_rockchip_thermal_match, np);
643 irq = platform_get_irq(pdev, 0);
645 dev_err(&pdev->dev, "no irq resource?\n");
649 thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
654 thermal->pdev = pdev;
656 thermal->chip = (const struct rockchip_tsadc_chip *)match->data;
660 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
661 thermal->regs = devm_ioremap_resource(&pdev->dev, res);
662 if (IS_ERR(thermal->regs))
663 return PTR_ERR(thermal->regs);
665 thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
666 if (IS_ERR(thermal->reset)) {
667 error = PTR_ERR(thermal->reset);
668 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
672 thermal->clk = devm_clk_get(&pdev->dev, "tsadc");
673 if (IS_ERR(thermal->clk)) {
674 error = PTR_ERR(thermal->clk);
675 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
679 thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
680 if (IS_ERR(thermal->pclk)) {
681 error = PTR_ERR(thermal->pclk);
682 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
687 error = clk_prepare_enable(thermal->clk);
689 dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
694 error = clk_prepare_enable(thermal->pclk);
696 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
697 goto err_disable_clk;
700 rockchip_thermal_reset_controller(thermal->reset);
702 error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
704 dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
706 goto err_disable_pclk;
709 thermal->chip->initialize(thermal->regs, thermal->tshut_polarity);
711 for (i = 0; i < thermal->chip->chn_num; i++) {
712 error = rockchip_thermal_register_sensor(pdev, thermal,
713 &thermal->sensors[i],
714 thermal->chip->chn_id[i]);
717 "failed to register sensor[%d] : error = %d\n",
719 for (j = 0; j < i; j++)
720 thermal_zone_of_sensor_unregister(&pdev->dev,
721 thermal->sensors[j].tzd);
722 goto err_disable_pclk;
726 error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
727 &rockchip_thermal_alarm_irq_thread,
729 "rockchip_thermal", thermal);
732 "failed to request tsadc irq: %d\n", error);
733 goto err_unregister_sensor;
736 thermal->chip->control(thermal->regs, true);
738 for (i = 0; i < thermal->chip->chn_num; i++)
739 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
741 platform_set_drvdata(pdev, thermal);
745 err_unregister_sensor:
747 thermal_zone_of_sensor_unregister(&pdev->dev,
748 thermal->sensors[i].tzd);
751 clk_disable_unprepare(thermal->pclk);
753 clk_disable_unprepare(thermal->clk);
758 static int rockchip_thermal_remove(struct platform_device *pdev)
760 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
763 for (i = 0; i < thermal->chip->chn_num; i++) {
764 struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
766 rockchip_thermal_toggle_sensor(sensor, false);
767 thermal_zone_of_sensor_unregister(&pdev->dev, sensor->tzd);
770 thermal->chip->control(thermal->regs, false);
772 clk_disable_unprepare(thermal->pclk);
773 clk_disable_unprepare(thermal->clk);
778 static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
780 struct platform_device *pdev = to_platform_device(dev);
781 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
784 for (i = 0; i < thermal->chip->chn_num; i++)
785 rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
787 thermal->chip->control(thermal->regs, false);
789 clk_disable(thermal->pclk);
790 clk_disable(thermal->clk);
792 pinctrl_pm_select_sleep_state(dev);
797 static int __maybe_unused rockchip_thermal_resume(struct device *dev)
799 struct platform_device *pdev = to_platform_device(dev);
800 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
804 error = clk_enable(thermal->clk);
808 error = clk_enable(thermal->pclk);
812 rockchip_thermal_reset_controller(thermal->reset);
814 thermal->chip->initialize(thermal->regs, thermal->tshut_polarity);
816 for (i = 0; i < thermal->chip->chn_num; i++) {
817 int id = thermal->sensors[i].id;
819 thermal->chip->set_tshut_mode(id, thermal->regs,
820 thermal->tshut_mode);
821 thermal->chip->set_tshut_temp(thermal->chip->table,
823 thermal->tshut_temp);
826 thermal->chip->control(thermal->regs, true);
828 for (i = 0; i < thermal->chip->chn_num; i++)
829 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
831 pinctrl_pm_select_default_state(dev);
836 static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
837 rockchip_thermal_suspend, rockchip_thermal_resume);
839 static struct platform_driver rockchip_thermal_driver = {
841 .name = "rockchip-thermal",
842 .pm = &rockchip_thermal_pm_ops,
843 .of_match_table = of_rockchip_thermal_match,
845 .probe = rockchip_thermal_probe,
846 .remove = rockchip_thermal_remove,
849 module_platform_driver(rockchip_thermal_driver);
851 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
852 MODULE_AUTHOR("Rockchip, Inc.");
853 MODULE_LICENSE("GPL v2");
854 MODULE_ALIAS("platform:rockchip-thermal");