summaryrefslogtreecommitdiff
path: root/components/drivers/dvfs/dvfs_pm.c
blob: 15ae49fec2e5be714672d898ec78c8f45e27b35a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-11-21     GuEe-GUI     first version
 */

#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>

#define DBG_TAG "dvfs.pm"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

rt_err_t rt_dvfs_scaling_pm_suspend(struct rt_device_pm *device_pm, rt_uint8_t mode)
{
    struct rt_dvfs_scaling *dvfs = device_pm->device->dvfs_scaling;

    if (!dvfs)
    {
        return RT_EOK;
    }

    return rt_dvfs_scaling_suspend(dvfs);
}

rt_err_t rt_dvfs_scaling_pm_resume(struct rt_device_pm *device_pm, rt_uint8_t mode)
{
    struct rt_dvfs_scaling *dvfs = device_pm->device->dvfs_scaling;

    if (!dvfs)
    {
        return RT_EOK;
    }

    return rt_dvfs_scaling_resume(dvfs);
}

rt_err_t rt_dvfs_scaling_pm_frequency_change(struct rt_device_pm *device_pm, rt_uint8_t mode)
{
    rt_uint32_t governor_type = RT_DVFS_GOVERNOR_TYPE_ONDEMAND;
    struct rt_dvfs_scaling *dvfs = device_pm->device->dvfs_scaling;

    if (!dvfs)
    {
        return RT_EOK;
    }

    rt_dvfs_scaling_enter(dvfs);

    if (dvfs->gov)
    {
        governor_type = dvfs->gov->type;
    }

    rt_dvfs_scaling_leave(dvfs);

    if (governor_type == RT_DVFS_GOVERNOR_TYPE_FREEDOM)
    {
        switch (mode)
        {
        case PM_RUN_MODE_HIGH_SPEED:
            return rt_dvfs_scaling_set_frequency(dvfs, dvfs->max_freq);

        case PM_RUN_MODE_NORMAL_SPEED:
            /*
             * It's not a good idea, but the OPP doesn't seem to have defaults.
             */
            return rt_dvfs_scaling_set_frequency(dvfs, (dvfs->max_freq + dvfs->min_freq) / 2);

        case PM_RUN_MODE_MEDIUM_SPEED:
            return rt_dvfs_scaling_set_frequency(dvfs, dvfs->suspend_freq);

        case PM_RUN_MODE_LOW_SPEED:
            return rt_dvfs_scaling_set_frequency(dvfs, dvfs->min_freq);

        default:
            break;
        }
    }
    else
    {
        switch (mode)
        {
        case PM_RUN_MODE_HIGH_SPEED:
            return rt_dvfs_scaling_set_governor(dvfs, RT_DVFS_GOVERNOR_TYPE_PERFORMANCE);

        case PM_RUN_MODE_NORMAL_SPEED:
            return rt_dvfs_scaling_set_governor(dvfs, RT_DVFS_GOVERNOR_TYPE_ONDEMAND);

        case PM_RUN_MODE_MEDIUM_SPEED:
            return rt_dvfs_scaling_set_governor(dvfs, RT_DVFS_GOVERNOR_TYPE_CONSERVATIVE);

        case PM_RUN_MODE_LOW_SPEED:
            return rt_dvfs_scaling_set_governor(dvfs, RT_DVFS_GOVERNOR_TYPE_POWERSAVE);

        default:
            break;
        }
    }

    return -RT_EINVAL;
}