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 two Temperature Sensors.
49 * sensor0 is for CPU, and sensor1 is for GPU.
57 * The max sensors is two in rockchip SoCs.
58 * Two sensors: CPU and GPU sensor.
60 #define SOC_MAX_SENSORS 2
62 struct chip_tsadc_table {
63 const struct tsadc_table *id;
65 /* the array table size*/
68 /* that analogic mask data */
72 struct rockchip_tsadc_chip {
73 /* The sensor id of chip correspond to the ADC channel */
74 int chn_id[SOC_MAX_SENSORS];
77 /* The hardware-controlled tshut property */
79 enum tshut_mode tshut_mode;
80 enum tshut_polarity tshut_polarity;
82 /* Chip-wide methods */
83 void (*initialize)(void __iomem *reg, enum tshut_polarity p);
84 void (*irq_ack)(void __iomem *reg);
85 void (*control)(void __iomem *reg, bool on);
87 /* Per-sensor methods */
88 int (*get_temp)(struct chip_tsadc_table table,
89 int chn, void __iomem *reg, int *temp);
90 void (*set_tshut_temp)(struct chip_tsadc_table table,
91 int chn, void __iomem *reg, long temp);
92 void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
94 /* Per-table methods */
95 struct chip_tsadc_table table;
98 struct rockchip_thermal_sensor {
99 struct rockchip_thermal_data *thermal;
100 struct thermal_zone_device *tzd;
104 struct rockchip_thermal_data {
105 const struct rockchip_tsadc_chip *chip;
106 struct platform_device *pdev;
107 struct reset_control *reset;
109 struct rockchip_thermal_sensor sensors[SOC_MAX_SENSORS];
117 enum tshut_mode tshut_mode;
118 enum tshut_polarity tshut_polarity;
121 /* TSADC Sensor info define: */
122 #define TSADCV2_AUTO_CON 0x04
123 #define TSADCV2_INT_EN 0x08
124 #define TSADCV2_INT_PD 0x0c
125 #define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04)
126 #define TSADCV2_COMP_SHUT(chn) (0x40 + (chn) * 0x04)
127 #define TSADCV2_HIGHT_INT_DEBOUNCE 0x60
128 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE 0x64
129 #define TSADCV2_AUTO_PERIOD 0x68
130 #define TSADCV2_AUTO_PERIOD_HT 0x6c
132 #define TSADCV2_AUTO_EN BIT(0)
133 #define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn))
134 #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8)
136 #define TSADCV2_INT_SRC_EN(chn) BIT(chn)
137 #define TSADCV2_SHUT_2GPIO_SRC_EN(chn) BIT(4 + (chn))
138 #define TSADCV2_SHUT_2CRU_SRC_EN(chn) BIT(8 + (chn))
140 #define TSADCV2_INT_PD_CLEAR_MASK ~BIT(8)
142 #define TSADCV2_DATA_MASK 0xfff
143 #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT 4
144 #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT 4
145 #define TSADCV2_AUTO_PERIOD_TIME 250 /* msec */
146 #define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* msec */
153 static const struct tsadc_table v2_code_table[] = {
154 {TSADCV2_DATA_MASK, -40000},
191 static u32 rk_tsadcv2_temp_to_code(struct chip_tsadc_table table,
197 high = table.length - 1;
198 mid = (high + low) / 2;
200 if (temp < table.id[low].temp || temp > table.id[high].temp)
203 while (low <= high) {
204 if (temp == table.id[mid].temp)
205 return table.id[mid].code;
206 else if (temp < table.id[mid].temp)
210 mid = (low + high) / 2;
216 static int rk_tsadcv2_code_to_temp(struct chip_tsadc_table table, u32 code,
219 unsigned int low = 1;
220 unsigned int high = table.length - 1;
221 unsigned int mid = (low + high) / 2;
225 WARN_ON(table.length < 2);
227 code &= table.data_mask;
228 if (code < table.id[high].code)
229 return -EAGAIN; /* Incorrect reading */
231 while (low <= high) {
232 if (code >= table.id[mid].code &&
233 code < table.id[mid - 1].code)
235 else if (code < table.id[mid].code)
239 mid = (low + high) / 2;
243 * The 5C granularity provided by the table is too much. Let's
244 * assume that the relationship between sensor readings and
245 * temperature between 2 table entries is linear and interpolate
246 * to produce less granular result.
248 num = table.id[mid].temp - v2_code_table[mid - 1].temp;
249 num *= table.id[mid - 1].code - code;
250 denom = table.id[mid - 1].code - table.id[mid].code;
251 *temp = table.id[mid - 1].temp + (num / denom);
257 * rk_tsadcv2_initialize - initialize TASDC Controller.
259 * (1) Set TSADC_V2_AUTO_PERIOD:
260 * Configure the interleave between every two accessing of
261 * TSADC in normal operation.
263 * (2) Set TSADCV2_AUTO_PERIOD_HT:
264 * Configure the interleave between every two accessing of
265 * TSADC after the temperature is higher than COM_SHUT or COM_INT.
267 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
268 * If the temperature is higher than COMP_INT or COMP_SHUT for
269 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
271 static void rk_tsadcv2_initialize(void __iomem *regs,
272 enum tshut_polarity tshut_polarity)
274 if (tshut_polarity == TSHUT_HIGH_ACTIVE)
275 writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
276 regs + TSADCV2_AUTO_CON);
278 writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
279 regs + TSADCV2_AUTO_CON);
281 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
282 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
283 regs + TSADCV2_HIGHT_INT_DEBOUNCE);
284 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
285 regs + TSADCV2_AUTO_PERIOD_HT);
286 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
287 regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
290 static void rk_tsadcv2_irq_ack(void __iomem *regs)
294 val = readl_relaxed(regs + TSADCV2_INT_PD);
295 writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
298 static void rk_tsadcv2_control(void __iomem *regs, bool enable)
302 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
304 val |= TSADCV2_AUTO_EN;
306 val &= ~TSADCV2_AUTO_EN;
308 writel_relaxed(val, regs + TSADCV2_AUTO_CON);
311 static int rk_tsadcv2_get_temp(struct chip_tsadc_table table,
312 int chn, void __iomem *regs, int *temp)
316 val = readl_relaxed(regs + TSADCV2_DATA(chn));
318 return rk_tsadcv2_code_to_temp(table, val, temp);
321 static void rk_tsadcv2_tshut_temp(struct chip_tsadc_table table,
322 int chn, void __iomem *regs, long temp)
324 u32 tshut_value, val;
326 tshut_value = rk_tsadcv2_temp_to_code(table, temp);
327 writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
329 /* TSHUT will be valid */
330 val = readl_relaxed(regs + TSADCV2_AUTO_CON);
331 writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
334 static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
335 enum tshut_mode mode)
339 val = readl_relaxed(regs + TSADCV2_INT_EN);
340 if (mode == TSHUT_MODE_GPIO) {
341 val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
342 val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
344 val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
345 val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
348 writel_relaxed(val, regs + TSADCV2_INT_EN);
351 static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
352 .chn_id[SENSOR_CPU] = 1, /* cpu sensor is channel 1 */
353 .chn_id[SENSOR_GPU] = 2, /* gpu sensor is channel 2 */
354 .chn_num = 2, /* two channels for tsadc */
356 .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
357 .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
360 .initialize = rk_tsadcv2_initialize,
361 .irq_ack = rk_tsadcv2_irq_ack,
362 .control = rk_tsadcv2_control,
363 .get_temp = rk_tsadcv2_get_temp,
364 .set_tshut_temp = rk_tsadcv2_tshut_temp,
365 .set_tshut_mode = rk_tsadcv2_tshut_mode,
369 .length = ARRAY_SIZE(v2_code_table),
370 .data_mask = TSADCV2_DATA_MASK,
374 static const struct of_device_id of_rockchip_thermal_match[] = {
376 .compatible = "rockchip,rk3288-tsadc",
377 .data = (void *)&rk3288_tsadc_data,
381 MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
384 rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
386 struct thermal_zone_device *tzd = sensor->tzd;
388 tzd->ops->set_mode(tzd,
389 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
392 static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
394 struct rockchip_thermal_data *thermal = dev;
397 dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
399 thermal->chip->irq_ack(thermal->regs);
401 for (i = 0; i < thermal->chip->chn_num; i++)
402 thermal_zone_device_update(thermal->sensors[i].tzd);
407 static int rockchip_thermal_get_temp(void *_sensor, int *out_temp)
409 struct rockchip_thermal_sensor *sensor = _sensor;
410 struct rockchip_thermal_data *thermal = sensor->thermal;
411 const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
414 retval = tsadc->get_temp(tsadc->table,
415 sensor->id, thermal->regs, out_temp);
416 dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n",
417 sensor->id, *out_temp, retval);
422 static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = {
423 .get_temp = rockchip_thermal_get_temp,
426 static int rockchip_configure_from_dt(struct device *dev,
427 struct device_node *np,
428 struct rockchip_thermal_data *thermal)
430 u32 shut_temp, tshut_mode, tshut_polarity;
432 if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
434 "Missing tshut temp property, using default %ld\n",
435 thermal->chip->tshut_temp);
436 thermal->tshut_temp = thermal->chip->tshut_temp;
438 thermal->tshut_temp = shut_temp;
441 if (thermal->tshut_temp > INT_MAX) {
442 dev_err(dev, "Invalid tshut temperature specified: %ld\n",
443 thermal->tshut_temp);
447 if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
449 "Missing tshut mode property, using default (%s)\n",
450 thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
452 thermal->tshut_mode = thermal->chip->tshut_mode;
454 thermal->tshut_mode = tshut_mode;
457 if (thermal->tshut_mode > 1) {
458 dev_err(dev, "Invalid tshut mode specified: %d\n",
459 thermal->tshut_mode);
463 if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
466 "Missing tshut-polarity property, using default (%s)\n",
467 thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
469 thermal->tshut_polarity = thermal->chip->tshut_polarity;
471 thermal->tshut_polarity = tshut_polarity;
474 if (thermal->tshut_polarity > 1) {
475 dev_err(dev, "Invalid tshut-polarity specified: %d\n",
476 thermal->tshut_polarity);
484 rockchip_thermal_register_sensor(struct platform_device *pdev,
485 struct rockchip_thermal_data *thermal,
486 struct rockchip_thermal_sensor *sensor,
489 const struct rockchip_tsadc_chip *tsadc = thermal->chip;
492 tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
493 tsadc->set_tshut_temp(tsadc->table, id, thermal->regs,
494 thermal->tshut_temp);
496 sensor->thermal = thermal;
498 sensor->tzd = thermal_zone_of_sensor_register(&pdev->dev, id, sensor,
499 &rockchip_of_thermal_ops);
500 if (IS_ERR(sensor->tzd)) {
501 error = PTR_ERR(sensor->tzd);
502 dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
511 * Reset TSADC Controller, reset all tsadc registers.
513 static void rockchip_thermal_reset_controller(struct reset_control *reset)
515 reset_control_assert(reset);
516 usleep_range(10, 20);
517 reset_control_deassert(reset);
520 static int rockchip_thermal_probe(struct platform_device *pdev)
522 struct device_node *np = pdev->dev.of_node;
523 struct rockchip_thermal_data *thermal;
524 const struct of_device_id *match;
525 struct resource *res;
530 match = of_match_node(of_rockchip_thermal_match, np);
534 irq = platform_get_irq(pdev, 0);
536 dev_err(&pdev->dev, "no irq resource?\n");
540 thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
545 thermal->pdev = pdev;
547 thermal->chip = (const struct rockchip_tsadc_chip *)match->data;
551 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
552 thermal->regs = devm_ioremap_resource(&pdev->dev, res);
553 if (IS_ERR(thermal->regs))
554 return PTR_ERR(thermal->regs);
556 thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
557 if (IS_ERR(thermal->reset)) {
558 error = PTR_ERR(thermal->reset);
559 dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
563 thermal->clk = devm_clk_get(&pdev->dev, "tsadc");
564 if (IS_ERR(thermal->clk)) {
565 error = PTR_ERR(thermal->clk);
566 dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
570 thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
571 if (IS_ERR(thermal->pclk)) {
572 error = PTR_ERR(thermal->pclk);
573 dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
578 error = clk_prepare_enable(thermal->clk);
580 dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
585 error = clk_prepare_enable(thermal->pclk);
587 dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
588 goto err_disable_clk;
591 rockchip_thermal_reset_controller(thermal->reset);
593 error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
595 dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
597 goto err_disable_pclk;
600 thermal->chip->initialize(thermal->regs, thermal->tshut_polarity);
602 for (i = 0; i < thermal->chip->chn_num; i++) {
603 error = rockchip_thermal_register_sensor(pdev, thermal,
604 &thermal->sensors[i],
605 thermal->chip->chn_id[i]);
608 "failed to register sensor[%d] : error = %d\n",
610 for (j = 0; j < i; j++)
611 thermal_zone_of_sensor_unregister(&pdev->dev,
612 thermal->sensors[j].tzd);
613 goto err_disable_pclk;
617 error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
618 &rockchip_thermal_alarm_irq_thread,
620 "rockchip_thermal", thermal);
623 "failed to request tsadc irq: %d\n", error);
624 goto err_unregister_sensor;
627 thermal->chip->control(thermal->regs, true);
629 for (i = 0; i < thermal->chip->chn_num; i++)
630 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
632 platform_set_drvdata(pdev, thermal);
636 err_unregister_sensor:
638 thermal_zone_of_sensor_unregister(&pdev->dev,
639 thermal->sensors[i].tzd);
642 clk_disable_unprepare(thermal->pclk);
644 clk_disable_unprepare(thermal->clk);
649 static int rockchip_thermal_remove(struct platform_device *pdev)
651 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
654 for (i = 0; i < thermal->chip->chn_num; i++) {
655 struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
657 rockchip_thermal_toggle_sensor(sensor, false);
658 thermal_zone_of_sensor_unregister(&pdev->dev, sensor->tzd);
661 thermal->chip->control(thermal->regs, false);
663 clk_disable_unprepare(thermal->pclk);
664 clk_disable_unprepare(thermal->clk);
669 static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
671 struct platform_device *pdev = to_platform_device(dev);
672 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
675 for (i = 0; i < thermal->chip->chn_num; i++)
676 rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
678 thermal->chip->control(thermal->regs, false);
680 clk_disable(thermal->pclk);
681 clk_disable(thermal->clk);
683 pinctrl_pm_select_sleep_state(dev);
688 static int __maybe_unused rockchip_thermal_resume(struct device *dev)
690 struct platform_device *pdev = to_platform_device(dev);
691 struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
695 error = clk_enable(thermal->clk);
699 error = clk_enable(thermal->pclk);
703 rockchip_thermal_reset_controller(thermal->reset);
705 thermal->chip->initialize(thermal->regs, thermal->tshut_polarity);
707 for (i = 0; i < thermal->chip->chn_num; i++) {
708 int id = thermal->sensors[i].id;
710 thermal->chip->set_tshut_mode(id, thermal->regs,
711 thermal->tshut_mode);
712 thermal->chip->set_tshut_temp(thermal->chip->table,
714 thermal->tshut_temp);
717 thermal->chip->control(thermal->regs, true);
719 for (i = 0; i < thermal->chip->chn_num; i++)
720 rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
722 pinctrl_pm_select_default_state(dev);
727 static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
728 rockchip_thermal_suspend, rockchip_thermal_resume);
730 static struct platform_driver rockchip_thermal_driver = {
732 .name = "rockchip-thermal",
733 .pm = &rockchip_thermal_pm_ops,
734 .of_match_table = of_rockchip_thermal_match,
736 .probe = rockchip_thermal_probe,
737 .remove = rockchip_thermal_remove,
740 module_platform_driver(rockchip_thermal_driver);
742 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
743 MODULE_AUTHOR("Rockchip, Inc.");
744 MODULE_LICENSE("GPL v2");
745 MODULE_ALIAS("platform:rockchip-thermal");