blob: d1da05cc18a5b1eaeaf962cbc0c9c1e16388bdac [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
Patrick Delaunay8767e792021-11-19 15:12:07 +01006#define LOG_CATEGORY UCLASS_CLK
7
Stephen Warrena9622432016-06-17 09:44:00 -06008#include <clk-uclass.h>
Simon Glass11c89f32017-05-17 17:18:03 -06009#include <dm.h>
Patrick Delaunay8767e792021-11-19 15:12:07 +010010#include <log.h>
Simon Glass95588622020-12-22 19:30:28 -070011#include <dm/device-internal.h>
Peng Fanec424a72019-07-31 07:01:39 +000012#include <linux/clk-provider.h>
Masahiro Yamada31adfc22016-01-19 13:55:28 +090013
Tero Kristode4ef9b2021-06-11 11:45:06 +030014#define UBOOT_DM_CLK_FIXED_RATE "fixed_rate_clock"
15#define UBOOT_DM_CLK_FIXED_RATE_RAW "fixed_rate_raw_clock"
16
Stephen Warrena9622432016-06-17 09:44:00 -060017static ulong clk_fixed_rate_get_rate(struct clk *clk)
Masahiro Yamada31adfc22016-01-19 13:55:28 +090018{
Stephen Warrena9622432016-06-17 09:44:00 -060019 return to_clk_fixed_rate(clk->dev)->fixed_rate;
Masahiro Yamada31adfc22016-01-19 13:55:28 +090020}
21
developercdb0efd2020-01-09 11:35:08 +080022/* avoid clk_enable() return -ENOSYS */
23static int dummy_enable(struct clk *clk)
24{
25 return 0;
26}
27
Masahiro Yamada31adfc22016-01-19 13:55:28 +090028const struct clk_ops clk_fixed_rate_ops = {
29 .get_rate = clk_fixed_rate_get_rate,
developercdb0efd2020-01-09 11:35:08 +080030 .enable = dummy_enable,
Samuel Hollandd3196cb2021-10-12 19:40:29 -050031 .disable = dummy_enable,
Masahiro Yamada31adfc22016-01-19 13:55:28 +090032};
33
Simon Glassb95c7b92021-03-15 17:25:23 +130034void clk_fixed_rate_ofdata_to_plat_(struct udevice *dev,
35 struct clk_fixed_rate *plat)
Masahiro Yamada31adfc22016-01-19 13:55:28 +090036{
Simon Glassb95c7b92021-03-15 17:25:23 +130037 struct clk *clk = &plat->clk;
Simon Glass6d70ba02021-08-07 07:24:06 -060038 if (CONFIG_IS_ENABLED(OF_REAL))
39 plat->fixed_rate = dev_read_u32_default(dev, "clock-frequency",
40 0);
41
Lukasz Majewski9ded469272019-06-24 15:50:40 +020042 /* Make fixed rate clock accessible from higher level struct clk */
Simon Glass95588622020-12-22 19:30:28 -070043 /* FIXME: This is not allowed */
44 dev_set_uclass_priv(dev, clk);
Simon Glassb95c7b92021-03-15 17:25:23 +130045
Lukasz Majewski9ded469272019-06-24 15:50:40 +020046 clk->dev = dev;
Peng Fan30a6ebc2019-08-21 13:35:03 +000047 clk->enable_count = 0;
Simon Glassb95c7b92021-03-15 17:25:23 +130048}
49
Tero Kristode4ef9b2021-06-11 11:45:06 +030050static ulong clk_fixed_rate_raw_get_rate(struct clk *clk)
51{
52 return container_of(clk, struct clk_fixed_rate, clk)->fixed_rate;
53}
54
55const struct clk_ops clk_fixed_rate_raw_ops = {
56 .get_rate = clk_fixed_rate_raw_get_rate,
57};
58
Simon Glassb95c7b92021-03-15 17:25:23 +130059static int clk_fixed_rate_of_to_plat(struct udevice *dev)
60{
61 clk_fixed_rate_ofdata_to_plat_(dev, to_clk_fixed_rate(dev));
Masahiro Yamada31adfc22016-01-19 13:55:28 +090062
63 return 0;
64}
Tero Kristode4ef9b2021-06-11 11:45:06 +030065
66#if CONFIG_IS_ENABLED(CLK_CCF)
67struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
68 ulong rate)
69{
70 struct clk *clk;
71 struct clk_fixed_rate *fixed;
72 int ret;
73
74 fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
75 if (!fixed)
76 return ERR_PTR(-ENOMEM);
77
78 fixed->fixed_rate = rate;
79
80 clk = &fixed->clk;
81
82 ret = clk_register(clk, UBOOT_DM_CLK_FIXED_RATE_RAW, name, NULL);
83 if (ret) {
84 kfree(fixed);
85 return ERR_PTR(ret);
86 }
87
88 return clk;
89}
90#endif
Masahiro Yamada31adfc22016-01-19 13:55:28 +090091
92static const struct udevice_id clk_fixed_rate_match[] = {
93 {
94 .compatible = "fixed-clock",
95 },
96 { /* sentinel */ }
97};
98
Simon Glass6b927b12020-10-03 11:31:32 -060099U_BOOT_DRIVER(fixed_clock) = {
100 .name = "fixed_clock",
Masahiro Yamada31adfc22016-01-19 13:55:28 +0900101 .id = UCLASS_CLK,
102 .of_match = clk_fixed_rate_match,
Simon Glassaad29ae2020-12-03 16:55:21 -0700103 .of_to_plat = clk_fixed_rate_of_to_plat,
Simon Glass71fa5b42020-12-03 16:55:18 -0700104 .plat_auto = sizeof(struct clk_fixed_rate),
Masahiro Yamada31adfc22016-01-19 13:55:28 +0900105 .ops = &clk_fixed_rate_ops,
Michal Simek34f67812020-09-16 13:20:55 +0200106 .flags = DM_FLAG_PRE_RELOC,
Masahiro Yamada31adfc22016-01-19 13:55:28 +0900107};
Tero Kristode4ef9b2021-06-11 11:45:06 +0300108
109U_BOOT_DRIVER(clk_fixed_rate_raw) = {
110 .name = UBOOT_DM_CLK_FIXED_RATE_RAW,
111 .id = UCLASS_CLK,
112 .ops = &clk_fixed_rate_raw_ops,
113 .flags = DM_FLAG_PRE_RELOC,
114};