blob: 325a9b2dcfb07dea945f2b6b33a7ee8743644008 [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 Glass589d9152016-07-04 11:58:03 -060035#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb95c7b92021-03-15 17:25:23 +130036 plat->fixed_rate = dev_read_u32_default(dev, "clock-frequency", 0);
Simon Glass589d9152016-07-04 11:58:03 -060037#endif
Lukasz Majewski9ded469272019-06-24 15:50:40 +020038 /* Make fixed rate clock accessible from higher level struct clk */
Simon Glass95588622020-12-22 19:30:28 -070039 /* FIXME: This is not allowed */
40 dev_set_uclass_priv(dev, clk);
Simon Glassb95c7b92021-03-15 17:25:23 +130041
Lukasz Majewski9ded469272019-06-24 15:50:40 +020042 clk->dev = dev;
Peng Fan30a6ebc2019-08-21 13:35:03 +000043 clk->enable_count = 0;
Simon Glassb95c7b92021-03-15 17:25:23 +130044}
45
Tero Kristode4ef9b2021-06-11 11:45:06 +030046static ulong clk_fixed_rate_raw_get_rate(struct clk *clk)
47{
48 return container_of(clk, struct clk_fixed_rate, clk)->fixed_rate;
49}
50
51const struct clk_ops clk_fixed_rate_raw_ops = {
52 .get_rate = clk_fixed_rate_raw_get_rate,
53};
54
Simon Glassb95c7b92021-03-15 17:25:23 +130055static int clk_fixed_rate_of_to_plat(struct udevice *dev)
56{
57 clk_fixed_rate_ofdata_to_plat_(dev, to_clk_fixed_rate(dev));
Masahiro Yamada31adfc22016-01-19 13:55:28 +090058
59 return 0;
60}
Tero Kristode4ef9b2021-06-11 11:45:06 +030061
62#if CONFIG_IS_ENABLED(CLK_CCF)
63struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
64 ulong rate)
65{
66 struct clk *clk;
67 struct clk_fixed_rate *fixed;
68 int ret;
69
70 fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
71 if (!fixed)
72 return ERR_PTR(-ENOMEM);
73
74 fixed->fixed_rate = rate;
75
76 clk = &fixed->clk;
77
78 ret = clk_register(clk, UBOOT_DM_CLK_FIXED_RATE_RAW, name, NULL);
79 if (ret) {
80 kfree(fixed);
81 return ERR_PTR(ret);
82 }
83
84 return clk;
85}
86#endif
Masahiro Yamada31adfc22016-01-19 13:55:28 +090087
88static const struct udevice_id clk_fixed_rate_match[] = {
89 {
90 .compatible = "fixed-clock",
91 },
92 { /* sentinel */ }
93};
94
Simon Glass6b927b12020-10-03 11:31:32 -060095U_BOOT_DRIVER(fixed_clock) = {
96 .name = "fixed_clock",
Masahiro Yamada31adfc22016-01-19 13:55:28 +090097 .id = UCLASS_CLK,
98 .of_match = clk_fixed_rate_match,
Simon Glassaad29ae2020-12-03 16:55:21 -070099 .of_to_plat = clk_fixed_rate_of_to_plat,
Simon Glass71fa5b42020-12-03 16:55:18 -0700100 .plat_auto = sizeof(struct clk_fixed_rate),
Masahiro Yamada31adfc22016-01-19 13:55:28 +0900101 .ops = &clk_fixed_rate_ops,
Michal Simek34f67812020-09-16 13:20:55 +0200102 .flags = DM_FLAG_PRE_RELOC,
Masahiro Yamada31adfc22016-01-19 13:55:28 +0900103};
Tero Kristode4ef9b2021-06-11 11:45:06 +0300104
105U_BOOT_DRIVER(clk_fixed_rate_raw) = {
106 .name = UBOOT_DM_CLK_FIXED_RATE_RAW,
107 .id = UCLASS_CLK,
108 .ops = &clk_fixed_rate_raw_ops,
109 .flags = DM_FLAG_PRE_RELOC,
110};