summaryrefslogtreecommitdiff
path: root/components/drivers/clk/clk-fixed-rate.c
blob: 0912e86dd6b4ae34ff932fb3c09637f5768e05e7 (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
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-11-26     GuEe-GUI     first version
 */

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

#include <drivers/platform.h>

static rt_err_t fixed_clk_ofw_init(struct rt_platform_device *pdev, struct rt_clk_fixed_rate *clk_fixed)
{
    rt_err_t err = RT_EOK;
    rt_uint32_t rate, accuracy;
    struct rt_ofw_node *np = pdev->parent.ofw_node;
    const char *clk_name = np->name;

    if (!rt_ofw_prop_read_u32(np, "clock-frequency", &rate))
    {
        rt_ofw_prop_read_u32(np, "clock-accuracy", &accuracy);
        rt_ofw_prop_read_string(np, "clock-output-names", &clk_name);

        clk_fixed->clk.name = clk_name;
        clk_fixed->clk.rate = rate;
        clk_fixed->clk.min_rate = rate;
        clk_fixed->clk.max_rate = rate;
        clk_fixed->fixed_rate = rate;
        clk_fixed->fixed_accuracy = accuracy;

        rt_ofw_data(np) = &clk_fixed->clk;
    }
    else
    {
        err = -RT_EIO;
    }

    return err;
}

static rt_err_t fixed_clk_probe(struct rt_platform_device *pdev)
{
    rt_err_t err = RT_EOK;
    struct rt_clk_fixed_rate *clk_fixed = rt_calloc(1, sizeof(*clk_fixed));

    if (clk_fixed)
    {
        err = fixed_clk_ofw_init(pdev, clk_fixed);
    }
    else
    {
        err = -RT_ENOMEM;
    }

    if (!err)
    {
        err = rt_clk_register(&clk_fixed->clk, RT_NULL);
    }

    if (err && clk_fixed)
    {
        rt_free(clk_fixed);
    }

    return err;
}

static const struct rt_ofw_node_id fixed_clk_ofw_ids[] =
{
    { .compatible = "fixed-clock" },
    { /* sentinel */ }
};

static struct rt_platform_driver fixed_clk_driver =
{
    .name = "clk-fixed-rate",
    .ids = fixed_clk_ofw_ids,

    .probe = fixed_clk_probe,
};

static int fixed_clk_drv_register(void)
{
    rt_platform_driver_register(&fixed_clk_driver);

    return 0;
}
INIT_SUBSYS_EXPORT(fixed_clk_drv_register);