Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 7 | #include <clk-uclass.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 9 | #include <dm/device-internal.h> |
Peng Fan | ec424a7 | 2019-07-31 07:01:39 +0000 | [diff] [blame] | 10 | #include <linux/clk-provider.h> |
Masahiro Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 11 | |
Tero Kristo | de4ef9b | 2021-06-11 11:45:06 +0300 | [diff] [blame] | 12 | #define UBOOT_DM_CLK_FIXED_RATE "fixed_rate_clock" |
| 13 | #define UBOOT_DM_CLK_FIXED_RATE_RAW "fixed_rate_raw_clock" |
| 14 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 15 | static ulong clk_fixed_rate_get_rate(struct clk *clk) |
Masahiro Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 16 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 17 | return to_clk_fixed_rate(clk->dev)->fixed_rate; |
Masahiro Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 18 | } |
| 19 | |
developer | cdb0efd | 2020-01-09 11:35:08 +0800 | [diff] [blame] | 20 | /* avoid clk_enable() return -ENOSYS */ |
| 21 | static int dummy_enable(struct clk *clk) |
| 22 | { |
| 23 | return 0; |
| 24 | } |
| 25 | |
Masahiro Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 26 | const struct clk_ops clk_fixed_rate_ops = { |
| 27 | .get_rate = clk_fixed_rate_get_rate, |
developer | cdb0efd | 2020-01-09 11:35:08 +0800 | [diff] [blame] | 28 | .enable = dummy_enable, |
Masahiro Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 29 | }; |
| 30 | |
Simon Glass | b95c7b9 | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 31 | void clk_fixed_rate_ofdata_to_plat_(struct udevice *dev, |
| 32 | struct clk_fixed_rate *plat) |
Masahiro Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 33 | { |
Simon Glass | b95c7b9 | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 34 | struct clk *clk = &plat->clk; |
Simon Glass | 6d70ba0 | 2021-08-07 07:24:06 -0600 | [diff] [blame^] | 35 | if (CONFIG_IS_ENABLED(OF_REAL)) |
| 36 | plat->fixed_rate = dev_read_u32_default(dev, "clock-frequency", |
| 37 | 0); |
| 38 | |
Lukasz Majewski | 9ded46927 | 2019-06-24 15:50:40 +0200 | [diff] [blame] | 39 | /* Make fixed rate clock accessible from higher level struct clk */ |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 40 | /* FIXME: This is not allowed */ |
| 41 | dev_set_uclass_priv(dev, clk); |
Simon Glass | b95c7b9 | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 42 | |
Lukasz Majewski | 9ded46927 | 2019-06-24 15:50:40 +0200 | [diff] [blame] | 43 | clk->dev = dev; |
Peng Fan | 30a6ebc | 2019-08-21 13:35:03 +0000 | [diff] [blame] | 44 | clk->enable_count = 0; |
Simon Glass | b95c7b9 | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 45 | } |
| 46 | |
Tero Kristo | de4ef9b | 2021-06-11 11:45:06 +0300 | [diff] [blame] | 47 | static 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 | |
| 52 | const struct clk_ops clk_fixed_rate_raw_ops = { |
| 53 | .get_rate = clk_fixed_rate_raw_get_rate, |
| 54 | }; |
| 55 | |
Simon Glass | b95c7b9 | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 56 | static 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 Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 59 | |
| 60 | return 0; |
| 61 | } |
Tero Kristo | de4ef9b | 2021-06-11 11:45:06 +0300 | [diff] [blame] | 62 | |
| 63 | #if CONFIG_IS_ENABLED(CLK_CCF) |
| 64 | struct 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 Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 88 | |
| 89 | static const struct udevice_id clk_fixed_rate_match[] = { |
| 90 | { |
| 91 | .compatible = "fixed-clock", |
| 92 | }, |
| 93 | { /* sentinel */ } |
| 94 | }; |
| 95 | |
Simon Glass | 6b927b1 | 2020-10-03 11:31:32 -0600 | [diff] [blame] | 96 | U_BOOT_DRIVER(fixed_clock) = { |
| 97 | .name = "fixed_clock", |
Masahiro Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 98 | .id = UCLASS_CLK, |
| 99 | .of_match = clk_fixed_rate_match, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 100 | .of_to_plat = clk_fixed_rate_of_to_plat, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 101 | .plat_auto = sizeof(struct clk_fixed_rate), |
Masahiro Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 102 | .ops = &clk_fixed_rate_ops, |
Michal Simek | 34f6781 | 2020-09-16 13:20:55 +0200 | [diff] [blame] | 103 | .flags = DM_FLAG_PRE_RELOC, |
Masahiro Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 104 | }; |
Tero Kristo | de4ef9b | 2021-06-11 11:45:06 +0300 | [diff] [blame] | 105 | |
| 106 | U_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 | }; |