blob: c5a2a42c923c00bc1c3bfdc723df92a9efe8f222 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamadab21e20b2016-01-19 13:55:28 +09002/*
3 * Copyright (C) 2016 Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadab21e20b2016-01-19 13:55:28 +09004 */
5
6#include <common.h>
Stephen Warren135aa952016-06-17 09:44:00 -06007#include <clk-uclass.h>
Simon Glass9d922452017-05-17 17:18:03 -06008#include <dm.h>
Simon Glass0fd3d912020-12-22 19:30:28 -07009#include <dm/device-internal.h>
Peng Fan4f305bf2019-07-31 07:01:39 +000010#include <linux/clk-provider.h>
Masahiro Yamadab21e20b2016-01-19 13:55:28 +090011
Tero Kristofc960cb2021-06-11 11:45:06 +030012#define UBOOT_DM_CLK_FIXED_RATE "fixed_rate_clock"
13#define UBOOT_DM_CLK_FIXED_RATE_RAW "fixed_rate_raw_clock"
14
Stephen Warren135aa952016-06-17 09:44:00 -060015static ulong clk_fixed_rate_get_rate(struct clk *clk)
Masahiro Yamadab21e20b2016-01-19 13:55:28 +090016{
Stephen Warren135aa952016-06-17 09:44:00 -060017 return to_clk_fixed_rate(clk->dev)->fixed_rate;
Masahiro Yamadab21e20b2016-01-19 13:55:28 +090018}
19
Chunfeng Yun6bf6d812020-01-09 11:35:08 +080020/* avoid clk_enable() return -ENOSYS */
21static int dummy_enable(struct clk *clk)
22{
23 return 0;
24}
25
Masahiro Yamadab21e20b2016-01-19 13:55:28 +090026const struct clk_ops clk_fixed_rate_ops = {
27 .get_rate = clk_fixed_rate_get_rate,
Chunfeng Yun6bf6d812020-01-09 11:35:08 +080028 .enable = dummy_enable,
Samuel Holland17864402021-10-12 19:40:29 -050029 .disable = dummy_enable,
Masahiro Yamadab21e20b2016-01-19 13:55:28 +090030};
31
Simon Glass4ddc91b2021-03-15 17:25:23 +130032void clk_fixed_rate_ofdata_to_plat_(struct udevice *dev,
33 struct clk_fixed_rate *plat)
Masahiro Yamadab21e20b2016-01-19 13:55:28 +090034{
Simon Glass4ddc91b2021-03-15 17:25:23 +130035 struct clk *clk = &plat->clk;
Simon Glassdcfc42b2021-08-07 07:24:06 -060036 if (CONFIG_IS_ENABLED(OF_REAL))
37 plat->fixed_rate = dev_read_u32_default(dev, "clock-frequency",
38 0);
39
Lukasz Majewski36bac0a2019-06-24 15:50:40 +020040 /* Make fixed rate clock accessible from higher level struct clk */
Simon Glass0fd3d912020-12-22 19:30:28 -070041 /* FIXME: This is not allowed */
42 dev_set_uclass_priv(dev, clk);
Simon Glass4ddc91b2021-03-15 17:25:23 +130043
Lukasz Majewski36bac0a2019-06-24 15:50:40 +020044 clk->dev = dev;
Peng Fane6849e22019-08-21 13:35:03 +000045 clk->enable_count = 0;
Simon Glass4ddc91b2021-03-15 17:25:23 +130046}
47
Tero Kristofc960cb2021-06-11 11:45:06 +030048static ulong clk_fixed_rate_raw_get_rate(struct clk *clk)
49{
50 return container_of(clk, struct clk_fixed_rate, clk)->fixed_rate;
51}
52
53const struct clk_ops clk_fixed_rate_raw_ops = {
54 .get_rate = clk_fixed_rate_raw_get_rate,
55};
56
Simon Glass4ddc91b2021-03-15 17:25:23 +130057static int clk_fixed_rate_of_to_plat(struct udevice *dev)
58{
59 clk_fixed_rate_ofdata_to_plat_(dev, to_clk_fixed_rate(dev));
Masahiro Yamadab21e20b2016-01-19 13:55:28 +090060
61 return 0;
62}
63
Tero Kristofc960cb2021-06-11 11:45:06 +030064#if CONFIG_IS_ENABLED(CLK_CCF)
65struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
66 ulong rate)
67{
68 struct clk *clk;
69 struct clk_fixed_rate *fixed;
70 int ret;
71
72 fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
73 if (!fixed)
74 return ERR_PTR(-ENOMEM);
75
76 fixed->fixed_rate = rate;
77
78 clk = &fixed->clk;
79
80 ret = clk_register(clk, UBOOT_DM_CLK_FIXED_RATE_RAW, name, NULL);
81 if (ret) {
82 kfree(fixed);
83 return ERR_PTR(ret);
84 }
85
86 return clk;
87}
88#endif
89
Masahiro Yamadab21e20b2016-01-19 13:55:28 +090090static const struct udevice_id clk_fixed_rate_match[] = {
91 {
92 .compatible = "fixed-clock",
93 },
94 { /* sentinel */ }
95};
96
Simon Glass88280522020-10-03 11:31:32 -060097U_BOOT_DRIVER(fixed_clock) = {
98 .name = "fixed_clock",
Masahiro Yamadab21e20b2016-01-19 13:55:28 +090099 .id = UCLASS_CLK,
100 .of_match = clk_fixed_rate_match,
Simon Glassd1998a92020-12-03 16:55:21 -0700101 .of_to_plat = clk_fixed_rate_of_to_plat,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700102 .plat_auto = sizeof(struct clk_fixed_rate),
Masahiro Yamadab21e20b2016-01-19 13:55:28 +0900103 .ops = &clk_fixed_rate_ops,
Michal Simek4ab38172020-09-16 13:20:55 +0200104 .flags = DM_FLAG_PRE_RELOC,
Masahiro Yamadab21e20b2016-01-19 13:55:28 +0900105};
Tero Kristofc960cb2021-06-11 11:45:06 +0300106
107U_BOOT_DRIVER(clk_fixed_rate_raw) = {
108 .name = UBOOT_DM_CLK_FIXED_RATE_RAW,
109 .id = UCLASS_CLK,
110 .ops = &clk_fixed_rate_raw_ops,
111 .flags = DM_FLAG_PRE_RELOC,
112};