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 | 589d915 | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 35 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | b95c7b9 | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 36 | plat->fixed_rate = dev_read_u32_default(dev, "clock-frequency", 0); |
Simon Glass | 589d915 | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 37 | #endif |
Lukasz Majewski | 9ded46927 | 2019-06-24 15:50:40 +0200 | [diff] [blame] | 38 | /* Make fixed rate clock accessible from higher level struct clk */ |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 39 | /* FIXME: This is not allowed */ |
| 40 | dev_set_uclass_priv(dev, clk); |
Simon Glass | b95c7b9 | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 41 | |
Lukasz Majewski | 9ded46927 | 2019-06-24 15:50:40 +0200 | [diff] [blame] | 42 | clk->dev = dev; |
Peng Fan | 30a6ebc | 2019-08-21 13:35:03 +0000 | [diff] [blame] | 43 | clk->enable_count = 0; |
Simon Glass | b95c7b9 | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 44 | } |
| 45 | |
Tero Kristo | de4ef9b | 2021-06-11 11:45:06 +0300 | [diff] [blame] | 46 | static 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 | |
| 51 | const struct clk_ops clk_fixed_rate_raw_ops = { |
| 52 | .get_rate = clk_fixed_rate_raw_get_rate, |
| 53 | }; |
| 54 | |
Simon Glass | b95c7b9 | 2021-03-15 17:25:23 +1300 | [diff] [blame] | 55 | static 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 Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 58 | |
| 59 | return 0; |
| 60 | } |
Tero Kristo | de4ef9b | 2021-06-11 11:45:06 +0300 | [diff] [blame] | 61 | |
| 62 | #if CONFIG_IS_ENABLED(CLK_CCF) |
| 63 | struct 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 Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 87 | |
| 88 | static const struct udevice_id clk_fixed_rate_match[] = { |
| 89 | { |
| 90 | .compatible = "fixed-clock", |
| 91 | }, |
| 92 | { /* sentinel */ } |
| 93 | }; |
| 94 | |
Simon Glass | 6b927b1 | 2020-10-03 11:31:32 -0600 | [diff] [blame] | 95 | U_BOOT_DRIVER(fixed_clock) = { |
| 96 | .name = "fixed_clock", |
Masahiro Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 97 | .id = UCLASS_CLK, |
| 98 | .of_match = clk_fixed_rate_match, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 99 | .of_to_plat = clk_fixed_rate_of_to_plat, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 100 | .plat_auto = sizeof(struct clk_fixed_rate), |
Masahiro Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 101 | .ops = &clk_fixed_rate_ops, |
Michal Simek | 34f6781 | 2020-09-16 13:20:55 +0200 | [diff] [blame] | 102 | .flags = DM_FLAG_PRE_RELOC, |
Masahiro Yamada | 31adfc2 | 2016-01-19 13:55:28 +0900 | [diff] [blame] | 103 | }; |
Tero Kristo | de4ef9b | 2021-06-11 11:45:06 +0300 | [diff] [blame] | 104 | |
| 105 | U_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 | }; |