summaryrefslogtreecommitdiff
path: root/components/drivers/dvfs/dvfs_governor.c
blob: e0bf4946a0c5fb1148fe2e0a606ad0a37d8c1c23 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * 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.governor"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

static RT_DEFINE_SPINLOCK(dvfs_governor_nodes_lock);
static rt_list_t dvfs_governor_nodes = RT_LIST_OBJECT_INIT(dvfs_governor_nodes);

static struct rt_dvfs_governor *dvfs_governor_find(rt_uint32_t governor, const char *name)
{
    struct rt_dvfs_governor *gov;

    rt_list_for_each_entry(gov, &dvfs_governor_nodes, list)
    {
        if (governor != RT_UINT32_MAX && gov->type == governor)
        {
            return gov;
        }
        else if (name && !rt_strncmp(name, gov->name, sizeof(gov->name) - 1))
        {
            return gov;
        }
    }

    return RT_NULL;
}

rt_err_t rt_dvfs_governor_register(struct rt_dvfs_governor *gov)
{
    rt_err_t err = RT_EOK;

    if (!gov)
    {
        return -RT_EINVAL;
    }

    RT_ASSERT(gov->set_frequency != RT_NULL);

    rt_spin_lock(&dvfs_governor_nodes_lock);

    if (dvfs_governor_find(gov->type, gov->name))
    {
        LOG_E("Governor %s[%u] is exists", gov->name, gov->type);
        err = -RT_EFULL;

        goto _out_lock;
    }

    rt_list_init(&gov->list);
    rt_ref_init(&gov->ref);
    rt_list_insert_before(&dvfs_governor_nodes, &gov->list);

_out_lock:
    rt_spin_unlock(&dvfs_governor_nodes_lock);

    return err;
}

rt_err_t rt_dvfs_governor_unregister(struct rt_dvfs_governor *gov)
{
    rt_err_t err = RT_EOK;

    if (!gov)
    {
        return -RT_EINVAL;
    }

    rt_spin_lock(&dvfs_governor_nodes_lock);

    if (rt_ref_read(&gov->ref) > 1)
    {
        err = -RT_EBUSY;
        goto _out_lock;
    }

    rt_list_remove(&gov->list);

_out_lock:
    rt_spin_unlock(&dvfs_governor_nodes_lock);

    return err;
}

struct rt_dvfs_governor *rt_dvfs_governor_get(rt_uint32_t governor)
{
    struct rt_dvfs_governor *gov;

    rt_spin_lock(&dvfs_governor_nodes_lock);

    if ((gov = dvfs_governor_find(governor, RT_NULL)))
    {
        rt_ref_get(&gov->ref);
    }

    rt_spin_unlock(&dvfs_governor_nodes_lock);

    return gov;
}

struct rt_dvfs_governor *rt_dvfs_governor_get_by_name(const char *name)
{
    struct rt_dvfs_governor *gov;

    rt_spin_lock(&dvfs_governor_nodes_lock);

    if ((gov = dvfs_governor_find(RT_UINT32_MAX, name)))
    {
        rt_ref_get(&gov->ref);
    }

    rt_spin_unlock(&dvfs_governor_nodes_lock);

    return gov;
}

static void dvfs_governor_release(struct rt_ref *ref)
{
}

void rt_dvfs_governor_put(struct rt_dvfs_governor *gov)
{
    if (!gov)
    {
        return;
    }

    rt_ref_put(&gov->ref, dvfs_governor_release);
}