1818#include <linux/dma-fence.h>
1919#include <linux/moduleparam.h>
2020#include <linux/of_device.h>
21+ #include <linux/thermal.h>
2122
2223#include "etnaviv_cmdbuf.h"
2324#include "etnaviv_dump.h"
@@ -409,6 +410,17 @@ static void etnaviv_gpu_load_clock(struct etnaviv_gpu *gpu, u32 clock)
409410 gpu_write (gpu , VIVS_HI_CLOCK_CONTROL , clock );
410411}
411412
413+ static void etnaviv_gpu_update_clock (struct etnaviv_gpu * gpu )
414+ {
415+ unsigned int fscale = 1 << (6 - gpu -> freq_scale );
416+ u32 clock ;
417+
418+ clock = VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS |
419+ VIVS_HI_CLOCK_CONTROL_FSCALE_VAL (fscale );
420+
421+ etnaviv_gpu_load_clock (gpu , clock );
422+ }
423+
412424static int etnaviv_hw_reset (struct etnaviv_gpu * gpu )
413425{
414426 u32 control , idle ;
@@ -426,11 +438,10 @@ static int etnaviv_hw_reset(struct etnaviv_gpu *gpu)
426438 timeout = jiffies + msecs_to_jiffies (1000 );
427439
428440 while (time_is_after_jiffies (timeout )) {
429- control = VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS |
430- VIVS_HI_CLOCK_CONTROL_FSCALE_VAL (0x40 );
431-
432441 /* enable clock */
433- etnaviv_gpu_load_clock (gpu , control );
442+ etnaviv_gpu_update_clock (gpu );
443+
444+ control = gpu_read (gpu , VIVS_HI_CLOCK_CONTROL );
434445
435446 /* Wait for stable clock. Vivante's code waited for 1ms */
436447 usleep_range (1000 , 10000 );
@@ -490,11 +501,7 @@ static int etnaviv_hw_reset(struct etnaviv_gpu *gpu)
490501 }
491502
492503 /* We rely on the GPU running, so program the clock */
493- control = VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS |
494- VIVS_HI_CLOCK_CONTROL_FSCALE_VAL (0x40 );
495-
496- /* enable clock */
497- etnaviv_gpu_load_clock (gpu , control );
504+ etnaviv_gpu_update_clock (gpu );
498505
499506 return 0 ;
500507}
@@ -1532,17 +1539,13 @@ static int etnaviv_gpu_hw_suspend(struct etnaviv_gpu *gpu)
15321539#ifdef CONFIG_PM
15331540static int etnaviv_gpu_hw_resume (struct etnaviv_gpu * gpu )
15341541{
1535- u32 clock ;
15361542 int ret ;
15371543
15381544 ret = mutex_lock_killable (& gpu -> lock );
15391545 if (ret )
15401546 return ret ;
15411547
1542- clock = VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS |
1543- VIVS_HI_CLOCK_CONTROL_FSCALE_VAL (0x40 );
1544-
1545- etnaviv_gpu_load_clock (gpu , clock );
1548+ etnaviv_gpu_update_clock (gpu );
15461549 etnaviv_gpu_hw_init (gpu );
15471550
15481551 gpu -> switch_context = true;
@@ -1554,6 +1557,47 @@ static int etnaviv_gpu_hw_resume(struct etnaviv_gpu *gpu)
15541557}
15551558#endif
15561559
1560+ static int
1561+ etnaviv_gpu_cooling_get_max_state (struct thermal_cooling_device * cdev ,
1562+ unsigned long * state )
1563+ {
1564+ * state = 6 ;
1565+
1566+ return 0 ;
1567+ }
1568+
1569+ static int
1570+ etnaviv_gpu_cooling_get_cur_state (struct thermal_cooling_device * cdev ,
1571+ unsigned long * state )
1572+ {
1573+ struct etnaviv_gpu * gpu = cdev -> devdata ;
1574+
1575+ * state = gpu -> freq_scale ;
1576+
1577+ return 0 ;
1578+ }
1579+
1580+ static int
1581+ etnaviv_gpu_cooling_set_cur_state (struct thermal_cooling_device * cdev ,
1582+ unsigned long state )
1583+ {
1584+ struct etnaviv_gpu * gpu = cdev -> devdata ;
1585+
1586+ mutex_lock (& gpu -> lock );
1587+ gpu -> freq_scale = state ;
1588+ if (!pm_runtime_suspended (gpu -> dev ))
1589+ etnaviv_gpu_update_clock (gpu );
1590+ mutex_unlock (& gpu -> lock );
1591+
1592+ return 0 ;
1593+ }
1594+
1595+ static struct thermal_cooling_device_ops cooling_ops = {
1596+ .get_max_state = etnaviv_gpu_cooling_get_max_state ,
1597+ .get_cur_state = etnaviv_gpu_cooling_get_cur_state ,
1598+ .set_cur_state = etnaviv_gpu_cooling_set_cur_state ,
1599+ };
1600+
15571601static int etnaviv_gpu_bind (struct device * dev , struct device * master ,
15581602 void * data )
15591603{
@@ -1562,13 +1606,20 @@ static int etnaviv_gpu_bind(struct device *dev, struct device *master,
15621606 struct etnaviv_gpu * gpu = dev_get_drvdata (dev );
15631607 int ret ;
15641608
1609+ gpu -> cooling = thermal_of_cooling_device_register (dev -> of_node ,
1610+ (char * )dev_name (dev ), gpu , & cooling_ops );
1611+ if (IS_ERR (gpu -> cooling ))
1612+ return PTR_ERR (gpu -> cooling );
1613+
15651614#ifdef CONFIG_PM
15661615 ret = pm_runtime_get_sync (gpu -> dev );
15671616#else
15681617 ret = etnaviv_gpu_clk_enable (gpu );
15691618#endif
1570- if (ret < 0 )
1619+ if (ret < 0 ) {
1620+ thermal_cooling_device_unregister (gpu -> cooling );
15711621 return ret ;
1622+ }
15721623
15731624 gpu -> drm = drm ;
15741625 gpu -> fence_context = dma_fence_context_alloc (1 );
@@ -1622,6 +1673,9 @@ static void etnaviv_gpu_unbind(struct device *dev, struct device *master,
16221673 }
16231674
16241675 gpu -> drm = NULL ;
1676+
1677+ thermal_cooling_device_unregister (gpu -> cooling );
1678+ gpu -> cooling = NULL ;
16251679}
16261680
16271681static const struct component_ops gpu_ops = {
0 commit comments