Skip to content

Commit 335a124

Browse files
GuEe-GUIRbb666
authored andcommitted
[dm][clk] refactoring the CLK framework
The old CLK is can't link all hardware clock cell in system that the API of layout such as 'set_parent' can't work as expected. Some hareware clock cell need some flags to prevent some dangerous behaviors, eg: When a clock cell is link to the PMU, the SoC will power-down if the cell is disable. The new CLK can do it, and make the CLK drivers implemented easier from TRM/DataSheet. Signed-off-by: GuEe-GUI <[email protected]>
1 parent a036ddc commit 335a124

File tree

4 files changed

+1805
-633
lines changed

4 files changed

+1805
-633
lines changed

components/drivers/clk/SConscript

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from building import *
22

33
group = []
4-
objs = []
54

65
if not GetDepend(['RT_USING_CLK']):
76
Return('group')
@@ -15,12 +14,7 @@ src = ['clk.c']
1514
if GetDepend(['RT_USING_OFW']):
1615
src += ['clk-fixed-rate.c']
1716

18-
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
1917

20-
for d in list:
21-
path = os.path.join(cwd, d)
22-
if os.path.isfile(os.path.join(path, 'SConscript')):
23-
objs = objs + SConscript(os.path.join(d, 'SConscript'))
24-
objs = objs + group
18+
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
2519

26-
Return('objs')
20+
Return('group')

components/drivers/clk/clk-fixed-rate.c

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,72 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2022-11-26 GuEe-GUI first version
9+
* 2024-05-01 GuEe-GUI update for new clk
910
*/
1011

1112
#include <rtthread.h>
1213
#include <rtdevice.h>
1314

14-
#include <drivers/platform.h>
15-
16-
static rt_err_t fixed_clk_ofw_init(struct rt_platform_device *pdev, struct rt_clk_fixed_rate *clk_fixed)
15+
struct clk_fixed
1716
{
18-
rt_err_t err = RT_EOK;
19-
rt_uint32_t rate, accuracy;
20-
struct rt_ofw_node *np = pdev->parent.ofw_node;
21-
const char *clk_name = np->name;
22-
23-
if (!rt_ofw_prop_read_u32(np, "clock-frequency", &rate))
24-
{
25-
rt_ofw_prop_read_u32(np, "clock-accuracy", &accuracy);
26-
rt_ofw_prop_read_string(np, "clock-output-names", &clk_name);
17+
struct rt_clk_node parent;
2718

28-
clk_fixed->clk.name = clk_name;
29-
clk_fixed->clk.rate = rate;
30-
clk_fixed->clk.min_rate = rate;
31-
clk_fixed->clk.max_rate = rate;
32-
clk_fixed->fixed_rate = rate;
33-
clk_fixed->fixed_accuracy = accuracy;
19+
struct rt_clk_fixed_rate fcell;
20+
struct rt_clk_cell *cells[1];
21+
};
3422

35-
rt_ofw_data(np) = &clk_fixed->clk;
36-
}
37-
else
38-
{
39-
err = -RT_EIO;
40-
}
23+
static rt_ubase_t fixed_clk_recalc_rate(struct rt_clk_cell *cell, rt_ubase_t parent_rate)
24+
{
25+
struct rt_clk_fixed_rate *fr = rt_container_of(cell, struct rt_clk_fixed_rate, cell);
4126

42-
return err;
27+
return fr->fixed_rate;
4328
}
4429

30+
static struct rt_clk_ops fixed_clk_ops =
31+
{
32+
.recalc_rate = fixed_clk_recalc_rate,
33+
};
34+
4535
static rt_err_t fixed_clk_probe(struct rt_platform_device *pdev)
4636
{
47-
rt_err_t err = RT_EOK;
48-
struct rt_clk_fixed_rate *clk_fixed = rt_calloc(1, sizeof(*clk_fixed));
37+
rt_err_t err;
38+
rt_uint32_t val;
39+
struct rt_device *dev = &pdev->parent;
40+
struct clk_fixed *cf = rt_calloc(1, sizeof(*cf));
4941

50-
if (clk_fixed)
42+
if (!cf)
5143
{
52-
err = fixed_clk_ofw_init(pdev, clk_fixed);
53-
}
54-
else
55-
{
56-
err = -RT_ENOMEM;
44+
return -RT_ENOMEM;
5745
}
5846

59-
if (!err)
47+
if ((err = rt_dm_dev_prop_read_u32(dev, "clock-frequency", &val)))
6048
{
61-
err = rt_clk_register(&clk_fixed->clk, RT_NULL);
49+
goto _fail;
6250
}
51+
cf->fcell.fixed_rate = val;
52+
53+
val = 0;
54+
rt_dm_dev_prop_read_u32(dev, "clock-accuracy", &val);
55+
cf->fcell.fixed_accuracy = val;
6356

64-
if (err && clk_fixed)
57+
rt_dm_dev_prop_read_string(dev, "clock-output-names", &cf->fcell.cell.name);
58+
59+
cf->parent.dev = dev;
60+
cf->parent.cells_nr = 1;
61+
cf->parent.cells = cf->cells;
62+
cf->cells[0] = &cf->fcell.cell;
63+
cf->fcell.cell.ops = &fixed_clk_ops;
64+
65+
if ((err = rt_clk_register(&cf->parent)))
6566
{
66-
rt_free(clk_fixed);
67+
goto _fail;
6768
}
6869

70+
return RT_EOK;
71+
72+
_fail:
73+
rt_free(cf);
74+
6975
return err;
7076
}
7177

0 commit comments

Comments
 (0)