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
)
196 unsigned int low
= 0;
197 unsigned int high
= ARRAY_SIZE(v2_code_table
) - 1;
198 unsigned int mid
= (low
+ high
) / 2;
202 /* Invalid code, return -EAGAIN */
203 if (code
> TSADCV2_DATA_MASK
)
206 while (low
<= high
&& mid
) {
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 return v2_code_table
[mid
- 1].temp
+ (num
/ denom
);
230 * rk_tsadcv2_initialize - initialize TASDC Controller
231 * (1) Set TSADCV2_AUTO_PERIOD, configure the interleave between
232 * every two accessing of TSADC in normal operation.
233 * (2) Set TSADCV2_AUTO_PERIOD_HT, configure the interleave between
234 * every two accessing of TSADC after the temperature is higher
235 * than COM_SHUT or COM_INT.
236 * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE,
237 * if the temperature is higher than COMP_INT or COMP_SHUT for
238 * "debounce" times, TSADC controller will generate interrupt or TSHUT.
240 static void rk_tsadcv2_initialize(void __iomem
*regs
,
241 enum tshut_polarity tshut_polarity
)
243 if (tshut_polarity
== TSHUT_HIGH_ACTIVE
)
244 writel_relaxed(0 | (TSADCV2_AUTO_TSHUT_POLARITY_HIGH
),
245 regs
+ TSADCV2_AUTO_CON
);
247 writel_relaxed(0 | (TSADCV2_AUTO_TSHUT_POLARITY_LOW
),
248 regs
+ TSADCV2_AUTO_CON
);
250 writel_relaxed(TSADCV2_AUTO_PERIOD_TIME
, regs
+ TSADCV2_AUTO_PERIOD
);
251 writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT
,
252 regs
+ TSADCV2_HIGHT_INT_DEBOUNCE
);
253 writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME
,
254 regs
+ TSADCV2_AUTO_PERIOD_HT
);
255 writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT
,
256 regs
+ TSADCV2_HIGHT_TSHUT_DEBOUNCE
);
259 static void rk_tsadcv2_irq_ack(void __iomem
*regs
)
263 val
= readl_relaxed(regs
+ TSADCV2_INT_PD
);
264 writel_relaxed(val
& TSADCV2_INT_PD_CLEAR
, regs
+ TSADCV2_INT_PD
);
267 static void rk_tsadcv2_control(void __iomem
*regs
, bool enable
)
271 val
= readl_relaxed(regs
+ TSADCV2_AUTO_CON
);
273 val
|= TSADCV2_AUTO_EN
;
275 val
&= ~TSADCV2_AUTO_EN
;
277 writel_relaxed(val
, regs
+ TSADCV2_AUTO_CON
);
280 static int rk_tsadcv2_get_temp(int chn
, void __iomem
*regs
, long *temp
)
284 /* the A/D value of the channel last conversion need some time */
285 val
= readl_relaxed(regs
+ TSADCV2_DATA(chn
));
289 *temp
= rk_tsadcv2_code_to_temp(val
);
294 static void rk_tsadcv2_tshut_temp(int chn
, void __iomem
*regs
, long temp
)
296 u32 tshut_value
, val
;
298 tshut_value
= rk_tsadcv2_temp_to_code(temp
);
299 writel_relaxed(tshut_value
, regs
+ TSADCV2_COMP_SHUT(chn
));
301 /* TSHUT will be valid */
302 val
= readl_relaxed(regs
+ TSADCV2_AUTO_CON
);
303 writel_relaxed(val
| TSADCV2_AUTO_SRC_EN(chn
), regs
+ TSADCV2_AUTO_CON
);
306 static void rk_tsadcv2_tshut_mode(int chn
, void __iomem
*regs
,
307 enum tshut_mode mode
)
311 val
= readl_relaxed(regs
+ TSADCV2_INT_EN
);
312 if (mode
== TSHUT_MODE_GPIO
) {
313 val
&= ~TSADCV2_SHUT_2CRU_SRC_EN(chn
);
314 val
|= TSADCV2_SHUT_2GPIO_SRC_EN(chn
);
316 val
&= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn
);
317 val
|= TSADCV2_SHUT_2CRU_SRC_EN(chn
);
320 writel_relaxed(val
, regs
+ TSADCV2_INT_EN
);
323 static const struct rockchip_tsadc_chip rk3288_tsadc_data
= {
324 .tshut_mode
= TSHUT_MODE_GPIO
, /* default TSHUT via GPIO give PMIC */
325 .tshut_polarity
= TSHUT_LOW_ACTIVE
, /* default TSHUT LOW ACTIVE */
328 .initialize
= rk_tsadcv2_initialize
,
329 .irq_ack
= rk_tsadcv2_irq_ack
,
330 .control
= rk_tsadcv2_control
,
331 .get_temp
= rk_tsadcv2_get_temp
,
332 .set_tshut_temp
= rk_tsadcv2_tshut_temp
,
333 .set_tshut_mode
= rk_tsadcv2_tshut_mode
,
336 static const struct of_device_id of_rockchip_thermal_match
[] = {
338 .compatible
= "rockchip,rk3288-tsadc",
339 .data
= (void *)&rk3288_tsadc_data
,
343 MODULE_DEVICE_TABLE(of
, of_rockchip_thermal_match
);
346 rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor
*sensor
, bool on
)
348 struct thermal_zone_device
*tzd
= sensor
->tzd
;
350 tzd
->ops
->set_mode(tzd
,
351 on
? THERMAL_DEVICE_ENABLED
: THERMAL_DEVICE_DISABLED
);
354 static irqreturn_t
rockchip_thermal_alarm_irq_thread(int irq
, void *dev
)
356 struct rockchip_thermal_data
*thermal
= dev
;
359 dev_dbg(&thermal
->pdev
->dev
, "thermal alarm\n");
361 thermal
->chip
->irq_ack(thermal
->regs
);
363 for (i
= 0; i
< ARRAY_SIZE(thermal
->sensors
); i
++)
364 thermal_zone_device_update(thermal
->sensors
[i
].tzd
);
369 static int rockchip_thermal_get_temp(void *_sensor
, long *out_temp
)
371 struct rockchip_thermal_sensor
*sensor
= _sensor
;
372 struct rockchip_thermal_data
*thermal
= sensor
->thermal
;
373 const struct rockchip_tsadc_chip
*tsadc
= sensor
->thermal
->chip
;
376 retval
= tsadc
->get_temp(sensor
->id
, thermal
->regs
, out_temp
);
377 dev_dbg(&thermal
->pdev
->dev
, "sensor %d - temp: %ld, retval: %d\n",
378 sensor
->id
, *out_temp
, retval
);
383 static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops
= {
384 .get_temp
= rockchip_thermal_get_temp
,
387 static int rockchip_configure_from_dt(struct device
*dev
,
388 struct device_node
*np
,
389 struct rockchip_thermal_data
*thermal
)
391 u32 shut_temp
, tshut_mode
, tshut_polarity
;
393 if (of_property_read_u32(np
, "rockchip,hw-tshut-temp", &shut_temp
)) {
395 "Missing tshut temp property, using default %ld\n",
396 thermal
->chip
->tshut_temp
);
397 thermal
->tshut_temp
= thermal
->chip
->tshut_temp
;
399 thermal
->tshut_temp
= shut_temp
;
402 if (thermal
->tshut_temp
> INT_MAX
) {
403 dev_err(dev
, "Invalid tshut temperature specified: %ld\n",
404 thermal
->tshut_temp
);
408 if (of_property_read_u32(np
, "rockchip,hw-tshut-mode", &tshut_mode
)) {
410 "Missing tshut mode property, using default (%s)\n",
411 thermal
->chip
->tshut_mode
== TSHUT_MODE_GPIO
?
413 thermal
->tshut_mode
= thermal
->chip
->tshut_mode
;
415 thermal
->tshut_mode
= tshut_mode
;
418 if (thermal
->tshut_mode
> 1) {
419 dev_err(dev
, "Invalid tshut mode specified: %d\n",
420 thermal
->tshut_mode
);
424 if (of_property_read_u32(np
, "rockchip,hw-tshut-polarity",
427 "Missing tshut-polarity property, using default (%s)\n",
428 thermal
->chip
->tshut_polarity
== TSHUT_LOW_ACTIVE
?
430 thermal
->tshut_polarity
= thermal
->chip
->tshut_polarity
;
432 thermal
->tshut_polarity
= tshut_polarity
;
435 if (thermal
->tshut_polarity
> 1) {
436 dev_err(dev
, "Invalid tshut-polarity specified: %d\n",
437 thermal
->tshut_polarity
);
445 rockchip_thermal_register_sensor(struct platform_device
*pdev
,
446 struct rockchip_thermal_data
*thermal
,
447 struct rockchip_thermal_sensor
*sensor
,
450 const struct rockchip_tsadc_chip
*tsadc
= thermal
->chip
;
453 tsadc
->set_tshut_mode(id
, thermal
->regs
, thermal
->tshut_mode
);
454 tsadc
->set_tshut_temp(id
, thermal
->regs
, thermal
->tshut_temp
);
456 sensor
->thermal
= thermal
;
458 sensor
->tzd
= thermal_zone_of_sensor_register(&pdev
->dev
, id
, sensor
,
459 &rockchip_of_thermal_ops
);
460 if (IS_ERR(sensor
->tzd
)) {
461 error
= PTR_ERR(sensor
->tzd
);
462 dev_err(&pdev
->dev
, "failed to register sensor %d: %d\n",
471 * Reset TSADC Controller, reset all tsadc registers.
473 static void rockchip_thermal_reset_controller(struct reset_control
*reset
)
475 reset_control_assert(reset
);
476 usleep_range(10, 20);
477 reset_control_deassert(reset
);
480 static int rockchip_thermal_probe(struct platform_device
*pdev
)
482 struct device_node
*np
= pdev
->dev
.of_node
;
483 struct rockchip_thermal_data
*thermal
;
484 const struct of_device_id
*match
;
485 struct resource
*res
;
490 match
= of_match_node(of_rockchip_thermal_match
, np
);
494 irq
= platform_get_irq(pdev
, 0);
496 dev_err(&pdev
->dev
, "no irq resource?\n");
500 thermal
= devm_kzalloc(&pdev
->dev
, sizeof(struct rockchip_thermal_data
),
505 thermal
->pdev
= pdev
;
507 thermal
->chip
= (const struct rockchip_tsadc_chip
*)match
->data
;
511 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
512 thermal
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
513 if (IS_ERR(thermal
->regs
))
514 return PTR_ERR(thermal
->regs
);
516 thermal
->reset
= devm_reset_control_get(&pdev
->dev
, "tsadc-apb");
517 if (IS_ERR(thermal
->reset
)) {
518 error
= PTR_ERR(thermal
->reset
);
519 dev_err(&pdev
->dev
, "failed to get tsadc reset: %d\n", error
);
523 thermal
->clk
= devm_clk_get(&pdev
->dev
, "tsadc");
524 if (IS_ERR(thermal
->clk
)) {
525 error
= PTR_ERR(thermal
->clk
);
526 dev_err(&pdev
->dev
, "failed to get tsadc clock: %d\n", error
);
530 thermal
->pclk
= devm_clk_get(&pdev
->dev
, "apb_pclk");
531 if (IS_ERR(thermal
->pclk
)) {
532 error
= PTR_ERR(thermal
->pclk
);
533 dev_err(&pdev
->dev
, "failed to get apb_pclk clock: %d\n",
538 error
= clk_prepare_enable(thermal
->clk
);
540 dev_err(&pdev
->dev
, "failed to enable converter clock: %d\n",
545 error
= clk_prepare_enable(thermal
->pclk
);
547 dev_err(&pdev
->dev
, "failed to enable pclk: %d\n", error
);
548 goto err_disable_clk
;
551 rockchip_thermal_reset_controller(thermal
->reset
);
553 error
= rockchip_configure_from_dt(&pdev
->dev
, np
, thermal
);
555 dev_err(&pdev
->dev
, "failed to parse device tree data: %d\n",
557 goto err_disable_pclk
;
560 thermal
->chip
->initialize(thermal
->regs
, thermal
->tshut_polarity
);
562 error
= rockchip_thermal_register_sensor(pdev
, thermal
,
563 &thermal
->sensors
[0],
567 "failed to register CPU thermal sensor: %d\n", error
);
568 goto err_disable_pclk
;
571 error
= rockchip_thermal_register_sensor(pdev
, thermal
,
572 &thermal
->sensors
[1],
576 "failed to register GPU thermal sensor: %d\n", error
);
577 goto err_unregister_cpu_sensor
;
580 error
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
581 &rockchip_thermal_alarm_irq_thread
,
583 "rockchip_thermal", thermal
);
586 "failed to request tsadc irq: %d\n", error
);
587 goto err_unregister_gpu_sensor
;
590 thermal
->chip
->control(thermal
->regs
, true);
592 for (i
= 0; i
< ARRAY_SIZE(thermal
->sensors
); i
++)
593 rockchip_thermal_toggle_sensor(&thermal
->sensors
[i
], true);
595 platform_set_drvdata(pdev
, thermal
);
599 err_unregister_gpu_sensor
:
600 thermal_zone_of_sensor_unregister(&pdev
->dev
, thermal
->sensors
[1].tzd
);
601 err_unregister_cpu_sensor
:
602 thermal_zone_of_sensor_unregister(&pdev
->dev
, thermal
->sensors
[0].tzd
);
604 clk_disable_unprepare(thermal
->pclk
);
606 clk_disable_unprepare(thermal
->clk
);
611 static int rockchip_thermal_remove(struct platform_device
*pdev
)
613 struct rockchip_thermal_data
*thermal
= platform_get_drvdata(pdev
);
616 for (i
= 0; i
< ARRAY_SIZE(thermal
->sensors
); i
++) {
617 struct rockchip_thermal_sensor
*sensor
= &thermal
->sensors
[i
];
619 rockchip_thermal_toggle_sensor(sensor
, false);
620 thermal_zone_of_sensor_unregister(&pdev
->dev
, sensor
->tzd
);
623 thermal
->chip
->control(thermal
->regs
, false);
625 clk_disable_unprepare(thermal
->pclk
);
626 clk_disable_unprepare(thermal
->clk
);
631 static int __maybe_unused
rockchip_thermal_suspend(struct device
*dev
)
633 struct platform_device
*pdev
= to_platform_device(dev
);
634 struct rockchip_thermal_data
*thermal
= platform_get_drvdata(pdev
);
637 for (i
= 0; i
< ARRAY_SIZE(thermal
->sensors
); i
++)
638 rockchip_thermal_toggle_sensor(&thermal
->sensors
[i
], false);
640 thermal
->chip
->control(thermal
->regs
, false);
642 clk_disable(thermal
->pclk
);
643 clk_disable(thermal
->clk
);
648 static int __maybe_unused
rockchip_thermal_resume(struct device
*dev
)
650 struct platform_device
*pdev
= to_platform_device(dev
);
651 struct rockchip_thermal_data
*thermal
= platform_get_drvdata(pdev
);
655 error
= clk_enable(thermal
->clk
);
659 error
= clk_enable(thermal
->pclk
);
663 rockchip_thermal_reset_controller(thermal
->reset
);
665 thermal
->chip
->initialize(thermal
->regs
, thermal
->tshut_polarity
);
667 for (i
= 0; i
< ARRAY_SIZE(thermal
->sensors
); i
++) {
668 enum sensor_id id
= thermal
->sensors
[i
].id
;
670 thermal
->chip
->set_tshut_mode(id
, thermal
->regs
,
671 thermal
->tshut_mode
);
672 thermal
->chip
->set_tshut_temp(id
, thermal
->regs
,
673 thermal
->tshut_temp
);
676 thermal
->chip
->control(thermal
->regs
, true);
678 for (i
= 0; i
< ARRAY_SIZE(thermal
->sensors
); i
++)
679 rockchip_thermal_toggle_sensor(&thermal
->sensors
[i
], true);
684 static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops
,
685 rockchip_thermal_suspend
, rockchip_thermal_resume
);
687 static struct platform_driver rockchip_thermal_driver
= {
689 .name
= "rockchip-thermal",
690 .pm
= &rockchip_thermal_pm_ops
,
691 .of_match_table
= of_rockchip_thermal_match
,
693 .probe
= rockchip_thermal_probe
,
694 .remove
= rockchip_thermal_remove
,
697 module_platform_driver(rockchip_thermal_driver
);
699 MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
700 MODULE_AUTHOR("Rockchip, Inc.");
701 MODULE_LICENSE("GPL v2");
702 MODULE_ALIAS("platform:rockchip-thermal");