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