blob: e0dc4ab85f8153c5b9bd462796c7a100654dd891 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada31adfc22016-01-19 13:55:28 +09002/*
3 * Copyright (C) 2016 Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada31adfc22016-01-19 13:55:28 +09004 */
5
6#include <common.h>
Stephen Warrena9622432016-06-17 09:44:00 -06007#include <clk-uclass.h>
Simon Glass11c89f32017-05-17 17:18:03 -06008#include <dm.h>
Simon Glass95588622020-12-22 19:30:28 -07009#include <dm/device-internal.h>
Peng Fanec424a72019-07-31 07:01:39 +000010#include <linux/clk-provider.h>
Masahiro Yamada31adfc22016-01-19 13:55:28 +090011
Tero Kristode4ef9b2021-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 Warrena9622432016-06-17 09:44:00 -060015static ulong clk_fixed_rate_get_rate(struct clk *clk)
Masahiro Yamada31adfc22016-01-19 13:55:28 +090016{
Stephen Warrena9622432016-06-17 09:44:00 -060017 return to_clk_fixed_rate(clk->dev)->fixed_rate;
Masahiro Yamada31adfc22016-01-19 13:55:28 +090018}
19
developercdb0efd2020-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 Yamada31adfc22016-01-19 13:55:28 +090026const struct clk_ops clk_fixed_rate_ops = {
27 .get_rate = clk_fixed_rate_get_rate,
developercdb0efd2020-01-09 11:35:08 +080028 .enable = dummy_enable,
Masahiro Yamada31adfc22016-01-19 13:55:28 +090029};
30
Simon Glassb95c7b92021-03-15 17:25:23 +130031void clk_fixed_rate_ofdata_to_plat_(struct udevice *dev,
32 struct clk_fixed_rate *plat)
Masahiro Yamada31adfc22016-01-19 13:55:28 +090033{
Simon Glassb95c7b92021-03-15 17:25:23 +130034 struct clk *clk = &plat->clk;
Simon Glass6d70ba02021-08-07 07:24:06 -060035 if (CONFIG_IS_ENABLED(OF_REAL))
36 plat->fixed_rate = dev_read_u32_default(dev, "clock-frequency",
37 0);
38
Lukasz Majewski9ded469272019-06-24 15:50:40 +020039 /* Make fixed rate clock accessible from higher level struct clk */
Simon Glass95588622020-12-22 19:30:28 -070040 /* FIXME: This is not allowed */
41 dev_set_uclass_priv(dev, clk);
Simon Glassb95c7b92021-03-15 17:25:23 +130042
Lukasz Majewski9ded469272019-06-24 15:50:40 +020043 clk->dev = dev;
Peng Fan30a6ebc2019-08-21 13:35:03 +000044 clk->enable_count = 0;
Simon Glassb95c7b92021-03-15 17:25:23 +130045}
46
Tero Kristode4ef9b2021-06-11 11:45:06 +030047static ulong clk_fixed_rate_raw_get_rate(struct clk *clk)
48{
49 return container_of(clk, struct clk_fixed_rate, clk)->fixed_rate;
50}
51
52const struct clk_ops clk_fixed_rate_raw_ops = {
53 .get_rate = clk_fixed_rate_raw_get_rate,
54};
55
Simon Glassb95c7b92021-03-15 17:25:23 +130056static int clk_fixed_rate_of_to_plat(struct udevice *dev)
57{
58 clk_fixed_rate_ofdata_to_plat_(dev, to_clk_fixed_rate(dev));
Masahiro Yamada31adfc22016-01-19 13:55:28 +090059
60 return 0;
61}
Tero Kristode4ef9b2021-06-11 11:45:06 +030062
63#if CONFIG_IS_ENABLED(CLK_CCF)
64struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
65 ulong rate)
66{
67 struct clk *clk;
68 struct clk_fixed_rate *fixed;
69 int ret;
70
71 fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
72 if (!fixed)
73 return ERR_PTR(-ENOMEM);
74
75 fixed->fixed_rate = rate;
76
77 clk = &fixed->clk;
78
79 ret = clk_register(clk, UBOOT_DM_CLK_FIXED_RATE_RAW, name, NULL);
80 if (ret) {
81 kfree(fixed);
82 return ERR_PTR(ret);
83 }
84
85 return clk;
86}
87#endif
Masahiro Yamada31adfc22016-01-19 13:55:28 +090088
89static const struct udevice_id clk_fixed_rate_match[] = {
90 {
91 .compatible = "fixed-clock",
92 },
93 { /* sentinel */ }
94};
95
Simon Glass6b927b12020-10-03 11:31:32 -060096U_BOOT_DRIVER(fixed_clock) = {
97 .name = "fixed_clock",
Masahiro Yamada31adfc22016-01-19 13:55:28 +090098 .id = UCLASS_CLK,
99 .of_match = clk_fixed_rate_match,
Simon Glassaad29ae2020-12-03 16:55:21 -0700100 .of_to_plat = clk_fixed_rate_of_to_plat,
Simon Glass71fa5b42020-12-03 16:55:18 -0700101 .plat_auto = sizeof(struct clk_fixed_rate),
Masahiro Yamada31adfc22016-01-19 13:55:28 +0900102 .ops = &clk_fixed_rate_ops,
Michal Simek34f67812020-09-16 13:20:55 +0200103 .flags = DM_FLAG_PRE_RELOC,
Masahiro Yamada31adfc22016-01-19 13:55:28 +0900104};
Tero Kristode4ef9b2021-06-11 11:45:06 +0300105
106U_BOOT_DRIVER(clk_fixed_rate_raw) = {
107 .name = UBOOT_DM_CLK_FIXED_RATE_RAW,
108 .id = UCLASS_CLK,
109 .ops = &clk_fixed_rate_raw_ops,
110 .flags = DM_FLAG_PRE_RELOC,
111};