| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Samsung Electronics Co., Ltd. |
| 3 | * Copyright (c) 2013 Linaro Ltd. |
| 4 | * Author: Thomas Abraham <thomas.ab@samsung.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This file includes utility functions to register clocks to common |
| 11 | * clock framework for Samsung platforms. |
| 12 | */ |
| 13 | |
| Stephen Boyd | 6f1ed07 | 2015-06-19 15:00:46 -0700 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | #include <linux/clkdev.h> |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/clk-provider.h> |
| Stephen Boyd | 62e59c4 | 2019-04-18 15:20:22 -0700 | [diff] [blame] | 18 | #include <linux/io.h> |
| Pankaj Dubey | 8b2f636 | 2014-09-29 13:17:48 +0530 | [diff] [blame] | 19 | #include <linux/of_address.h> |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 20 | #include <linux/syscore_ops.h> |
| Pankaj Dubey | 8b2f636 | 2014-09-29 13:17:48 +0530 | [diff] [blame] | 21 | |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 22 | #include "clk.h" |
| 23 | |
| Naveen Krishna Ch | 16a9013 | 2014-09-22 10:17:02 +0530 | [diff] [blame] | 24 | static LIST_HEAD(clock_reg_cache_list); |
| 25 | |
| Tomasz Figa | 3ccefbd | 2014-02-14 08:16:00 +0900 | [diff] [blame] | 26 | void samsung_clk_save(void __iomem *base, |
| 27 | struct samsung_clk_reg_dump *rd, |
| 28 | unsigned int num_regs) |
| 29 | { |
| 30 | for (; num_regs > 0; --num_regs, ++rd) |
| 31 | rd->value = readl(base + rd->offset); |
| 32 | } |
| 33 | |
| 34 | void samsung_clk_restore(void __iomem *base, |
| 35 | const struct samsung_clk_reg_dump *rd, |
| 36 | unsigned int num_regs) |
| 37 | { |
| 38 | for (; num_regs > 0; --num_regs, ++rd) |
| 39 | writel(rd->value, base + rd->offset); |
| 40 | } |
| 41 | |
| Tomasz Figa | c3b6c1d | 2014-02-14 08:16:00 +0900 | [diff] [blame] | 42 | struct samsung_clk_reg_dump *samsung_clk_alloc_reg_dump( |
| 43 | const unsigned long *rdump, |
| 44 | unsigned long nr_rdump) |
| Tomasz Figa | 3ccefbd | 2014-02-14 08:16:00 +0900 | [diff] [blame] | 45 | { |
| 46 | struct samsung_clk_reg_dump *rd; |
| 47 | unsigned int i; |
| 48 | |
| 49 | rd = kcalloc(nr_rdump, sizeof(*rd), GFP_KERNEL); |
| 50 | if (!rd) |
| 51 | return NULL; |
| 52 | |
| 53 | for (i = 0; i < nr_rdump; ++i) |
| 54 | rd[i].offset = rdump[i]; |
| 55 | |
| 56 | return rd; |
| 57 | } |
| 58 | |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 59 | /* setup the essentials required to support clock lookup using ccf */ |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 60 | struct samsung_clk_provider *__init samsung_clk_init(struct device_node *np, |
| 61 | void __iomem *base, unsigned long nr_clks) |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 62 | { |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 63 | struct samsung_clk_provider *ctx; |
| Tomasz Figa | 91a1263 | 2014-02-06 19:33:11 +0100 | [diff] [blame] | 64 | int i; |
| 65 | |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 66 | ctx = kzalloc(sizeof(struct samsung_clk_provider) + |
| 67 | sizeof(*ctx->clk_data.hws) * nr_clks, GFP_KERNEL); |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 68 | if (!ctx) |
| 69 | panic("could not allocate clock provider context.\n"); |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 70 | |
| Tomasz Figa | 91a1263 | 2014-02-06 19:33:11 +0100 | [diff] [blame] | 71 | for (i = 0; i < nr_clks; ++i) |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 72 | ctx->clk_data.hws[i] = ERR_PTR(-ENOENT); |
| Tomasz Figa | 91a1263 | 2014-02-06 19:33:11 +0100 | [diff] [blame] | 73 | |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 74 | ctx->reg_base = base; |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 75 | ctx->clk_data.num = nr_clks; |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 76 | spin_lock_init(&ctx->lock); |
| Heiko Stuebner | 6e92bf5a | 2013-03-18 13:43:52 +0900 | [diff] [blame] | 77 | |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 78 | return ctx; |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 79 | } |
| 80 | |
| Sylwester Nawrocki | d5e136a | 2014-06-18 17:46:52 +0200 | [diff] [blame] | 81 | void __init samsung_clk_of_add_provider(struct device_node *np, |
| 82 | struct samsung_clk_provider *ctx) |
| 83 | { |
| 84 | if (np) { |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 85 | if (of_clk_add_hw_provider(np, of_clk_hw_onecell_get, |
| Sylwester Nawrocki | d5e136a | 2014-06-18 17:46:52 +0200 | [diff] [blame] | 86 | &ctx->clk_data)) |
| 87 | panic("could not register clk provider\n"); |
| 88 | } |
| 89 | } |
| 90 | |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 91 | /* add a clock instance to the clock lookup table used for dt based lookup */ |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 92 | void samsung_clk_add_lookup(struct samsung_clk_provider *ctx, |
| 93 | struct clk_hw *clk_hw, unsigned int id) |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 94 | { |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 95 | if (id) |
| 96 | ctx->clk_data.hws[id] = clk_hw; |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 97 | } |
| 98 | |
| Heiko Stuebner | 5e2e019 | 2013-03-18 13:43:56 +0900 | [diff] [blame] | 99 | /* register a list of aliases */ |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 100 | void __init samsung_clk_register_alias(struct samsung_clk_provider *ctx, |
| Uwe Kleine-König | 4a1caed | 2015-05-28 10:45:51 +0200 | [diff] [blame] | 101 | const struct samsung_clock_alias *list, |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 102 | unsigned int nr_clk) |
| Heiko Stuebner | 5e2e019 | 2013-03-18 13:43:56 +0900 | [diff] [blame] | 103 | { |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 104 | struct clk_hw *clk_hw; |
| Heiko Stuebner | 5e2e019 | 2013-03-18 13:43:56 +0900 | [diff] [blame] | 105 | unsigned int idx, ret; |
| 106 | |
| Heiko Stuebner | 5e2e019 | 2013-03-18 13:43:56 +0900 | [diff] [blame] | 107 | for (idx = 0; idx < nr_clk; idx++, list++) { |
| 108 | if (!list->id) { |
| 109 | pr_err("%s: clock id missing for index %d\n", __func__, |
| 110 | idx); |
| 111 | continue; |
| 112 | } |
| 113 | |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 114 | clk_hw = ctx->clk_data.hws[list->id]; |
| 115 | if (!clk_hw) { |
| Heiko Stuebner | 5e2e019 | 2013-03-18 13:43:56 +0900 | [diff] [blame] | 116 | pr_err("%s: failed to find clock %d\n", __func__, |
| 117 | list->id); |
| 118 | continue; |
| 119 | } |
| 120 | |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 121 | ret = clk_hw_register_clkdev(clk_hw, list->alias, |
| 122 | list->dev_name); |
| Heiko Stuebner | 5e2e019 | 2013-03-18 13:43:56 +0900 | [diff] [blame] | 123 | if (ret) |
| 124 | pr_err("%s: failed to register lookup %s\n", |
| 125 | __func__, list->alias); |
| 126 | } |
| 127 | } |
| 128 | |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 129 | /* register a list of fixed clocks */ |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 130 | void __init samsung_clk_register_fixed_rate(struct samsung_clk_provider *ctx, |
| Uwe Kleine-König | 4a1caed | 2015-05-28 10:45:51 +0200 | [diff] [blame] | 131 | const struct samsung_fixed_rate_clock *list, |
| 132 | unsigned int nr_clk) |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 133 | { |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 134 | struct clk_hw *clk_hw; |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 135 | unsigned int idx, ret; |
| 136 | |
| 137 | for (idx = 0; idx < nr_clk; idx++, list++) { |
| Marek Szyprowski | d2f18d7 | 2017-08-21 10:05:00 +0200 | [diff] [blame] | 138 | clk_hw = clk_hw_register_fixed_rate(ctx->dev, list->name, |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 139 | list->parent_name, list->flags, list->fixed_rate); |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 140 | if (IS_ERR(clk_hw)) { |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 141 | pr_err("%s: failed to register clock %s\n", __func__, |
| 142 | list->name); |
| 143 | continue; |
| 144 | } |
| 145 | |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 146 | samsung_clk_add_lookup(ctx, clk_hw, list->id); |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * Unconditionally add a clock lookup for the fixed rate clocks. |
| 150 | * There are not many of these on any of Samsung platforms. |
| 151 | */ |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 152 | ret = clk_hw_register_clkdev(clk_hw, list->name, NULL); |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 153 | if (ret) |
| 154 | pr_err("%s: failed to register clock lookup for %s", |
| 155 | __func__, list->name); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /* register a list of fixed factor clocks */ |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 160 | void __init samsung_clk_register_fixed_factor(struct samsung_clk_provider *ctx, |
| Uwe Kleine-König | 4a1caed | 2015-05-28 10:45:51 +0200 | [diff] [blame] | 161 | const struct samsung_fixed_factor_clock *list, unsigned int nr_clk) |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 162 | { |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 163 | struct clk_hw *clk_hw; |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 164 | unsigned int idx; |
| 165 | |
| 166 | for (idx = 0; idx < nr_clk; idx++, list++) { |
| Marek Szyprowski | d2f18d7 | 2017-08-21 10:05:00 +0200 | [diff] [blame] | 167 | clk_hw = clk_hw_register_fixed_factor(ctx->dev, list->name, |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 168 | list->parent_name, list->flags, list->mult, list->div); |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 169 | if (IS_ERR(clk_hw)) { |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 170 | pr_err("%s: failed to register clock %s\n", __func__, |
| 171 | list->name); |
| 172 | continue; |
| 173 | } |
| 174 | |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 175 | samsung_clk_add_lookup(ctx, clk_hw, list->id); |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | /* register a list of mux clocks */ |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 180 | void __init samsung_clk_register_mux(struct samsung_clk_provider *ctx, |
| Uwe Kleine-König | 4a1caed | 2015-05-28 10:45:51 +0200 | [diff] [blame] | 181 | const struct samsung_mux_clock *list, |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 182 | unsigned int nr_clk) |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 183 | { |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 184 | struct clk_hw *clk_hw; |
| Marek Szyprowski | a4f21e9 | 2017-10-03 12:00:16 +0200 | [diff] [blame] | 185 | unsigned int idx; |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 186 | |
| 187 | for (idx = 0; idx < nr_clk; idx++, list++) { |
| Marek Szyprowski | d2f18d7 | 2017-08-21 10:05:00 +0200 | [diff] [blame] | 188 | clk_hw = clk_hw_register_mux(ctx->dev, list->name, |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 189 | list->parent_names, list->num_parents, list->flags, |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 190 | ctx->reg_base + list->offset, |
| 191 | list->shift, list->width, list->mux_flags, &ctx->lock); |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 192 | if (IS_ERR(clk_hw)) { |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 193 | pr_err("%s: failed to register clock %s\n", __func__, |
| 194 | list->name); |
| 195 | continue; |
| 196 | } |
| 197 | |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 198 | samsung_clk_add_lookup(ctx, clk_hw, list->id); |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | /* register a list of div clocks */ |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 203 | void __init samsung_clk_register_div(struct samsung_clk_provider *ctx, |
| Uwe Kleine-König | 4a1caed | 2015-05-28 10:45:51 +0200 | [diff] [blame] | 204 | const struct samsung_div_clock *list, |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 205 | unsigned int nr_clk) |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 206 | { |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 207 | struct clk_hw *clk_hw; |
| Marek Szyprowski | a4f21e9 | 2017-10-03 12:00:16 +0200 | [diff] [blame] | 208 | unsigned int idx; |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 209 | |
| 210 | for (idx = 0; idx < nr_clk; idx++, list++) { |
| Heiko Stuebner | 798ed61 | 2013-03-18 13:43:52 +0900 | [diff] [blame] | 211 | if (list->table) |
| Marek Szyprowski | d2f18d7 | 2017-08-21 10:05:00 +0200 | [diff] [blame] | 212 | clk_hw = clk_hw_register_divider_table(ctx->dev, |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 213 | list->name, list->parent_name, list->flags, |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 214 | ctx->reg_base + list->offset, |
| 215 | list->shift, list->width, list->div_flags, |
| 216 | list->table, &ctx->lock); |
| Heiko Stuebner | 798ed61 | 2013-03-18 13:43:52 +0900 | [diff] [blame] | 217 | else |
| Marek Szyprowski | d2f18d7 | 2017-08-21 10:05:00 +0200 | [diff] [blame] | 218 | clk_hw = clk_hw_register_divider(ctx->dev, list->name, |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 219 | list->parent_name, list->flags, |
| 220 | ctx->reg_base + list->offset, list->shift, |
| 221 | list->width, list->div_flags, &ctx->lock); |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 222 | if (IS_ERR(clk_hw)) { |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 223 | pr_err("%s: failed to register clock %s\n", __func__, |
| 224 | list->name); |
| 225 | continue; |
| 226 | } |
| 227 | |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 228 | samsung_clk_add_lookup(ctx, clk_hw, list->id); |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | /* register a list of gate clocks */ |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 233 | void __init samsung_clk_register_gate(struct samsung_clk_provider *ctx, |
| Uwe Kleine-König | 4a1caed | 2015-05-28 10:45:51 +0200 | [diff] [blame] | 234 | const struct samsung_gate_clock *list, |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 235 | unsigned int nr_clk) |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 236 | { |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 237 | struct clk_hw *clk_hw; |
| Marek Szyprowski | a4f21e9 | 2017-10-03 12:00:16 +0200 | [diff] [blame] | 238 | unsigned int idx; |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 239 | |
| 240 | for (idx = 0; idx < nr_clk; idx++, list++) { |
| Marek Szyprowski | d2f18d7 | 2017-08-21 10:05:00 +0200 | [diff] [blame] | 241 | clk_hw = clk_hw_register_gate(ctx->dev, list->name, list->parent_name, |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 242 | list->flags, ctx->reg_base + list->offset, |
| 243 | list->bit_idx, list->gate_flags, &ctx->lock); |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 244 | if (IS_ERR(clk_hw)) { |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 245 | pr_err("%s: failed to register clock %s\n", __func__, |
| 246 | list->name); |
| 247 | continue; |
| 248 | } |
| 249 | |
| Marek Szyprowski | ecb1f1f | 2017-04-24 08:42:20 +0200 | [diff] [blame] | 250 | samsung_clk_add_lookup(ctx, clk_hw, list->id); |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * obtain the clock speed of all external fixed clock sources from device |
| 256 | * tree and register it |
| 257 | */ |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 258 | void __init samsung_clk_of_register_fixed_ext(struct samsung_clk_provider *ctx, |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 259 | struct samsung_fixed_rate_clock *fixed_rate_clk, |
| 260 | unsigned int nr_fixed_rate_clk, |
| Krzysztof Kozlowski | 305cfab | 2014-06-26 14:00:06 +0200 | [diff] [blame] | 261 | const struct of_device_id *clk_matches) |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 262 | { |
| 263 | const struct of_device_id *match; |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 264 | struct device_node *clk_np; |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 265 | u32 freq; |
| 266 | |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 267 | for_each_matching_node_and_match(clk_np, clk_matches, &match) { |
| 268 | if (of_property_read_u32(clk_np, "clock-frequency", &freq)) |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 269 | continue; |
| Pankaj Dubey | 42fb57c | 2014-02-26 11:42:41 +0900 | [diff] [blame] | 270 | fixed_rate_clk[(unsigned long)match->data].fixed_rate = freq; |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 271 | } |
| Rahul Sharma | 976face | 2014-03-12 20:26:44 +0530 | [diff] [blame] | 272 | samsung_clk_register_fixed_rate(ctx, fixed_rate_clk, nr_fixed_rate_clk); |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 273 | } |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 274 | |
| 275 | /* utility function to get the rate of a specified clock */ |
| 276 | unsigned long _get_rate(const char *clk_name) |
| 277 | { |
| 278 | struct clk *clk; |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 279 | |
| Tomasz Figa | 3a64789 | 2013-08-26 19:09:00 +0200 | [diff] [blame] | 280 | clk = __clk_lookup(clk_name); |
| 281 | if (!clk) { |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 282 | pr_err("%s: could not find clock %s\n", __func__, clk_name); |
| 283 | return 0; |
| 284 | } |
| Tomasz Figa | 3a64789 | 2013-08-26 19:09:00 +0200 | [diff] [blame] | 285 | |
| 286 | return clk_get_rate(clk); |
| Thomas Abraham | 721c42a | 2013-03-09 17:02:44 +0900 | [diff] [blame] | 287 | } |
| Naveen Krishna Ch | 16a9013 | 2014-09-22 10:17:02 +0530 | [diff] [blame] | 288 | |
| 289 | #ifdef CONFIG_PM_SLEEP |
| 290 | static int samsung_clk_suspend(void) |
| 291 | { |
| 292 | struct samsung_clock_reg_cache *reg_cache; |
| 293 | |
| Marek Szyprowski | 8bf27ea | 2018-09-06 17:55:30 +0200 | [diff] [blame] | 294 | list_for_each_entry(reg_cache, &clock_reg_cache_list, node) { |
| Naveen Krishna Ch | 16a9013 | 2014-09-22 10:17:02 +0530 | [diff] [blame] | 295 | samsung_clk_save(reg_cache->reg_base, reg_cache->rdump, |
| 296 | reg_cache->rd_num); |
| Marek Szyprowski | 8bf27ea | 2018-09-06 17:55:30 +0200 | [diff] [blame] | 297 | samsung_clk_restore(reg_cache->reg_base, reg_cache->rsuspend, |
| 298 | reg_cache->rsuspend_num); |
| 299 | } |
| Naveen Krishna Ch | 16a9013 | 2014-09-22 10:17:02 +0530 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static void samsung_clk_resume(void) |
| 304 | { |
| 305 | struct samsung_clock_reg_cache *reg_cache; |
| 306 | |
| 307 | list_for_each_entry(reg_cache, &clock_reg_cache_list, node) |
| 308 | samsung_clk_restore(reg_cache->reg_base, reg_cache->rdump, |
| 309 | reg_cache->rd_num); |
| 310 | } |
| 311 | |
| 312 | static struct syscore_ops samsung_clk_syscore_ops = { |
| 313 | .suspend = samsung_clk_suspend, |
| 314 | .resume = samsung_clk_resume, |
| 315 | }; |
| 316 | |
| Marek Szyprowski | 8bf27ea | 2018-09-06 17:55:30 +0200 | [diff] [blame] | 317 | void samsung_clk_extended_sleep_init(void __iomem *reg_base, |
| Bartlomiej Zolnierkiewicz | 0c0cd59 | 2016-05-24 15:19:15 +0200 | [diff] [blame] | 318 | const unsigned long *rdump, |
| Marek Szyprowski | 8bf27ea | 2018-09-06 17:55:30 +0200 | [diff] [blame] | 319 | unsigned long nr_rdump, |
| 320 | const struct samsung_clk_reg_dump *rsuspend, |
| 321 | unsigned long nr_rsuspend) |
| Naveen Krishna Ch | 16a9013 | 2014-09-22 10:17:02 +0530 | [diff] [blame] | 322 | { |
| 323 | struct samsung_clock_reg_cache *reg_cache; |
| 324 | |
| 325 | reg_cache = kzalloc(sizeof(struct samsung_clock_reg_cache), |
| 326 | GFP_KERNEL); |
| 327 | if (!reg_cache) |
| 328 | panic("could not allocate register reg_cache.\n"); |
| 329 | reg_cache->rdump = samsung_clk_alloc_reg_dump(rdump, nr_rdump); |
| 330 | |
| 331 | if (!reg_cache->rdump) |
| 332 | panic("could not allocate register dump storage.\n"); |
| 333 | |
| 334 | if (list_empty(&clock_reg_cache_list)) |
| 335 | register_syscore_ops(&samsung_clk_syscore_ops); |
| 336 | |
| 337 | reg_cache->reg_base = reg_base; |
| 338 | reg_cache->rd_num = nr_rdump; |
| Marek Szyprowski | 8bf27ea | 2018-09-06 17:55:30 +0200 | [diff] [blame] | 339 | reg_cache->rsuspend = rsuspend; |
| 340 | reg_cache->rsuspend_num = nr_rsuspend; |
| Naveen Krishna Ch | 16a9013 | 2014-09-22 10:17:02 +0530 | [diff] [blame] | 341 | list_add_tail(®_cache->node, &clock_reg_cache_list); |
| 342 | } |
| Naveen Krishna Ch | 16a9013 | 2014-09-22 10:17:02 +0530 | [diff] [blame] | 343 | #endif |
| 344 | |
| 345 | /* |
| 346 | * Common function which registers plls, muxes, dividers and gates |
| 347 | * for each CMU. It also add CMU register list to register cache. |
| 348 | */ |
| Chanwoo Choi | 151d4d3 | 2014-12-23 16:40:21 +0900 | [diff] [blame] | 349 | struct samsung_clk_provider * __init samsung_cmu_register_one( |
| 350 | struct device_node *np, |
| Krzysztof Kozlowski | 9f92c0b | 2016-05-11 14:01:57 +0200 | [diff] [blame] | 351 | const struct samsung_cmu_info *cmu) |
| Naveen Krishna Ch | 16a9013 | 2014-09-22 10:17:02 +0530 | [diff] [blame] | 352 | { |
| 353 | void __iomem *reg_base; |
| 354 | struct samsung_clk_provider *ctx; |
| 355 | |
| 356 | reg_base = of_iomap(np, 0); |
| Chanwoo Choi | 151d4d3 | 2014-12-23 16:40:21 +0900 | [diff] [blame] | 357 | if (!reg_base) { |
| Naveen Krishna Ch | 16a9013 | 2014-09-22 10:17:02 +0530 | [diff] [blame] | 358 | panic("%s: failed to map registers\n", __func__); |
| Chanwoo Choi | 151d4d3 | 2014-12-23 16:40:21 +0900 | [diff] [blame] | 359 | return NULL; |
| 360 | } |
| Naveen Krishna Ch | 16a9013 | 2014-09-22 10:17:02 +0530 | [diff] [blame] | 361 | |
| 362 | ctx = samsung_clk_init(np, reg_base, cmu->nr_clk_ids); |
| Chanwoo Choi | 151d4d3 | 2014-12-23 16:40:21 +0900 | [diff] [blame] | 363 | if (!ctx) { |
| Shailendra Verma | c306317 | 2015-05-21 23:26:03 +0530 | [diff] [blame] | 364 | panic("%s: unable to allocate ctx\n", __func__); |
| Chanwoo Choi | 151d4d3 | 2014-12-23 16:40:21 +0900 | [diff] [blame] | 365 | return ctx; |
| 366 | } |
| Naveen Krishna Ch | 16a9013 | 2014-09-22 10:17:02 +0530 | [diff] [blame] | 367 | |
| 368 | if (cmu->pll_clks) |
| 369 | samsung_clk_register_pll(ctx, cmu->pll_clks, cmu->nr_pll_clks, |
| 370 | reg_base); |
| 371 | if (cmu->mux_clks) |
| 372 | samsung_clk_register_mux(ctx, cmu->mux_clks, |
| 373 | cmu->nr_mux_clks); |
| 374 | if (cmu->div_clks) |
| 375 | samsung_clk_register_div(ctx, cmu->div_clks, cmu->nr_div_clks); |
| 376 | if (cmu->gate_clks) |
| 377 | samsung_clk_register_gate(ctx, cmu->gate_clks, |
| 378 | cmu->nr_gate_clks); |
| 379 | if (cmu->fixed_clks) |
| 380 | samsung_clk_register_fixed_rate(ctx, cmu->fixed_clks, |
| 381 | cmu->nr_fixed_clks); |
| Naveen Krishna Ch | 0e5af27 | 2014-09-22 10:17:03 +0530 | [diff] [blame] | 382 | if (cmu->fixed_factor_clks) |
| 383 | samsung_clk_register_fixed_factor(ctx, cmu->fixed_factor_clks, |
| 384 | cmu->nr_fixed_factor_clks); |
| Naveen Krishna Ch | 16a9013 | 2014-09-22 10:17:02 +0530 | [diff] [blame] | 385 | if (cmu->clk_regs) |
| Marek Szyprowski | 8bf27ea | 2018-09-06 17:55:30 +0200 | [diff] [blame] | 386 | samsung_clk_extended_sleep_init(reg_base, |
| 387 | cmu->clk_regs, cmu->nr_clk_regs, |
| 388 | cmu->suspend_regs, cmu->nr_suspend_regs); |
| Naveen Krishna Ch | 16a9013 | 2014-09-22 10:17:02 +0530 | [diff] [blame] | 389 | |
| 390 | samsung_clk_of_add_provider(np, ctx); |
| Chanwoo Choi | 151d4d3 | 2014-12-23 16:40:21 +0900 | [diff] [blame] | 391 | |
| 392 | return ctx; |
| Naveen Krishna Ch | 16a9013 | 2014-09-22 10:17:02 +0530 | [diff] [blame] | 393 | } |