Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | b21e20b | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | b21e20b | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | 560e1e0 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_CLK |
| 7 | |
Masahiro Yamada | b21e20b | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 8 | #include <common.h> |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 9 | #include <clk-uclass.h> |
Simon Glass | 9d92245 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Patrick Delaunay | 560e1e0 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 12 | #include <dm/device-internal.h> |
Peng Fan | 4f305bf | 2019-07-31 07:01:39 +0000 | [diff] [blame] | 13 | #include <linux/clk-provider.h> |
Masahiro Yamada | b21e20b | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 14 | |
Tero Kristo | fc960cb | 2021-06-11 11:45:06 +0300 | [diff] [blame] | 15 | #define UBOOT_DM_CLK_FIXED_RATE "fixed_rate_clock" |
| 16 | #define UBOOT_DM_CLK_FIXED_RATE_RAW "fixed_rate_raw_clock" |
| 17 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 18 | static ulong clk_fixed_rate_get_rate(struct clk *clk) |
Masahiro Yamada | b21e20b | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 19 | { |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 20 | return to_clk_fixed_rate(clk->dev)->fixed_rate; |
Masahiro Yamada | b21e20b | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 21 | } |
| 22 | |
Chunfeng Yun | 6bf6d81 | 2020-01-09 11:35:08 +0800 | [diff] [blame] | 23 | /* avoid clk_enable() return -ENOSYS */ |
| 24 | static int dummy_enable(struct clk *clk) |
| 25 | { |
| 26 | return 0; |
| 27 | } |
| 28 | |
Masahiro Yamada | b21e20b | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 29 | const struct clk_ops clk_fixed_rate_ops = { |
| 30 | .get_rate = clk_fixed_rate_get_rate, |
Chunfeng Yun | 6bf6d81 | 2020-01-09 11:35:08 +0800 | [diff] [blame] | 31 | .enable = dummy_enable, |
Samuel Holland | 1786440 | 2021-10-12 19:40:29 -0500 | [diff] [blame] | 32 | .disable = dummy_enable, |
Masahiro Yamada | b21e20b | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 33 | }; |
| 34 | |
Simon Glass | 4ddc91b | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 35 | void clk_fixed_rate_ofdata_to_plat_(struct udevice *dev, |
| 36 | struct clk_fixed_rate *plat) |
Masahiro Yamada | b21e20b | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 37 | { |
Simon Glass | 4ddc91b | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 38 | struct clk *clk = &plat->clk; |
Simon Glass | dcfc42b | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 39 | if (CONFIG_IS_ENABLED(OF_REAL)) |
| 40 | plat->fixed_rate = dev_read_u32_default(dev, "clock-frequency", |
| 41 | 0); |
| 42 | |
Lukasz Majewski | 36bac0a | 2019-06-24 15:50:40 +0200 | [diff] [blame] | 43 | /* Make fixed rate clock accessible from higher level struct clk */ |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 44 | /* FIXME: This is not allowed */ |
| 45 | dev_set_uclass_priv(dev, clk); |
Simon Glass | 4ddc91b | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 46 | |
Lukasz Majewski | 36bac0a | 2019-06-24 15:50:40 +0200 | [diff] [blame] | 47 | clk->dev = dev; |
Peng Fan | e6849e2 | 2019-08-21 13:35:03 +0000 | [diff] [blame] | 48 | clk->enable_count = 0; |
Simon Glass | 4ddc91b | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 49 | } |
| 50 | |
Tero Kristo | fc960cb | 2021-06-11 11:45:06 +0300 | [diff] [blame] | 51 | static ulong clk_fixed_rate_raw_get_rate(struct clk *clk) |
| 52 | { |
| 53 | return container_of(clk, struct clk_fixed_rate, clk)->fixed_rate; |
| 54 | } |
| 55 | |
| 56 | const struct clk_ops clk_fixed_rate_raw_ops = { |
| 57 | .get_rate = clk_fixed_rate_raw_get_rate, |
| 58 | }; |
| 59 | |
Simon Glass | 4ddc91b | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 60 | static int clk_fixed_rate_of_to_plat(struct udevice *dev) |
| 61 | { |
| 62 | clk_fixed_rate_ofdata_to_plat_(dev, to_clk_fixed_rate(dev)); |
Masahiro Yamada | b21e20b | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Tero Kristo | fc960cb | 2021-06-11 11:45:06 +0300 | [diff] [blame] | 67 | #if CONFIG_IS_ENABLED(CLK_CCF) |
| 68 | struct clk *clk_register_fixed_rate(struct device *dev, const char *name, |
| 69 | ulong rate) |
| 70 | { |
| 71 | struct clk *clk; |
| 72 | struct clk_fixed_rate *fixed; |
| 73 | int ret; |
| 74 | |
| 75 | fixed = kzalloc(sizeof(*fixed), GFP_KERNEL); |
| 76 | if (!fixed) |
| 77 | return ERR_PTR(-ENOMEM); |
| 78 | |
| 79 | fixed->fixed_rate = rate; |
| 80 | |
| 81 | clk = &fixed->clk; |
| 82 | |
| 83 | ret = clk_register(clk, UBOOT_DM_CLK_FIXED_RATE_RAW, name, NULL); |
| 84 | if (ret) { |
| 85 | kfree(fixed); |
| 86 | return ERR_PTR(ret); |
| 87 | } |
| 88 | |
| 89 | return clk; |
| 90 | } |
| 91 | #endif |
| 92 | |
Masahiro Yamada | b21e20b | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 93 | static const struct udevice_id clk_fixed_rate_match[] = { |
| 94 | { |
| 95 | .compatible = "fixed-clock", |
| 96 | }, |
| 97 | { /* sentinel */ } |
| 98 | }; |
| 99 | |
Simon Glass | 8828052 | 2020-10-03 11:31:32 -0600 | [diff] [blame] | 100 | U_BOOT_DRIVER(fixed_clock) = { |
| 101 | .name = "fixed_clock", |
Masahiro Yamada | b21e20b | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 102 | .id = UCLASS_CLK, |
| 103 | .of_match = clk_fixed_rate_match, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 104 | .of_to_plat = clk_fixed_rate_of_to_plat, |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 105 | .plat_auto = sizeof(struct clk_fixed_rate), |
Masahiro Yamada | b21e20b | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 106 | .ops = &clk_fixed_rate_ops, |
Michal Simek | 4ab3817 | 2020-09-16 13:20:55 +0200 | [diff] [blame] | 107 | .flags = DM_FLAG_PRE_RELOC, |
Masahiro Yamada | b21e20b | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 108 | }; |
Tero Kristo | fc960cb | 2021-06-11 11:45:06 +0300 | [diff] [blame] | 109 | |
| 110 | U_BOOT_DRIVER(clk_fixed_rate_raw) = { |
| 111 | .name = UBOOT_DM_CLK_FIXED_RATE_RAW, |
| 112 | .id = UCLASS_CLK, |
| 113 | .ops = &clk_fixed_rate_raw_ops, |
| 114 | .flags = DM_FLAG_PRE_RELOC, |
| 115 | }; |