Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Emilio López <emilio@elopez.com.ar> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * Adjustable factor-based clock implementation |
| 9 | */ |
| 10 | |
| 11 | #include <linux/clk-provider.h> |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 12 | #include <linux/delay.h> |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 13 | #include <linux/err.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/of_address.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/string.h> |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 19 | |
| 20 | #include "clk-factors.h" |
| 21 | |
| 22 | /* |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 23 | * DOC: basic adjustable factor-based clock |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 24 | * |
| 25 | * Traits of this clock: |
| 26 | * prepare - clk_prepare only ensures that parents are prepared |
| 27 | * enable - clk_enable only ensures that parents are enabled |
| 28 | * rate - rate is adjustable. |
| 29 | * clk->rate = (parent->rate * N * (K + 1) >> P) / (M + 1) |
| 30 | * parent - fixed parent. No clk_set_parent support |
| 31 | */ |
| 32 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 33 | #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw) |
| 34 | |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 35 | #define FACTORS_MAX_PARENTS 5 |
| 36 | |
Emilio López | c518e84 | 2013-09-20 22:03:10 -0300 | [diff] [blame] | 37 | #define SETMASK(len, pos) (((1U << (len)) - 1) << (pos)) |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 38 | #define CLRMASK(len, pos) (~(SETMASK(len, pos))) |
| 39 | #define FACTOR_GET(bit, len, reg) (((reg) & SETMASK(len, bit)) >> (bit)) |
| 40 | |
| 41 | #define FACTOR_SET(bit, len, reg, val) \ |
| 42 | (((reg) & CLRMASK(len, bit)) | (val << (bit))) |
| 43 | |
| 44 | static unsigned long clk_factors_recalc_rate(struct clk_hw *hw, |
| 45 | unsigned long parent_rate) |
| 46 | { |
| 47 | u8 n = 1, k = 0, p = 0, m = 0; |
| 48 | u32 reg; |
| 49 | unsigned long rate; |
| 50 | struct clk_factors *factors = to_clk_factors(hw); |
Chen-Yu Tsai | b3e919e | 2016-01-25 21:15:38 +0800 | [diff] [blame] | 51 | const struct clk_factors_config *config = factors->config; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 52 | |
| 53 | /* Fetch the register value */ |
| 54 | reg = readl(factors->reg); |
| 55 | |
| 56 | /* Get each individual factor if applicable */ |
| 57 | if (config->nwidth != SUNXI_FACTORS_NOT_APPLICABLE) |
| 58 | n = FACTOR_GET(config->nshift, config->nwidth, reg); |
| 59 | if (config->kwidth != SUNXI_FACTORS_NOT_APPLICABLE) |
| 60 | k = FACTOR_GET(config->kshift, config->kwidth, reg); |
| 61 | if (config->mwidth != SUNXI_FACTORS_NOT_APPLICABLE) |
| 62 | m = FACTOR_GET(config->mshift, config->mwidth, reg); |
| 63 | if (config->pwidth != SUNXI_FACTORS_NOT_APPLICABLE) |
| 64 | p = FACTOR_GET(config->pshift, config->pwidth, reg); |
| 65 | |
| 66 | /* Calculate the rate */ |
Chen-Yu Tsai | 9a5e6c7 | 2014-06-26 23:55:41 +0800 | [diff] [blame] | 67 | rate = (parent_rate * (n + config->n_start) * (k + 1) >> p) / (m + 1); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 68 | |
| 69 | return rate; |
| 70 | } |
| 71 | |
| 72 | static long clk_factors_round_rate(struct clk_hw *hw, unsigned long rate, |
| 73 | unsigned long *parent_rate) |
| 74 | { |
| 75 | struct clk_factors *factors = to_clk_factors(hw); |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame^] | 76 | struct factors_request req = { |
| 77 | .rate = rate, |
| 78 | .parent_rate = *parent_rate, |
| 79 | }; |
| 80 | |
| 81 | factors->get_factors(&req); |
| 82 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 83 | |
| 84 | return rate; |
| 85 | } |
| 86 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 87 | static int clk_factors_determine_rate(struct clk_hw *hw, |
| 88 | struct clk_rate_request *req) |
Emilio López | 862b728 | 2014-05-02 17:57:15 +0200 | [diff] [blame] | 89 | { |
Stephen Boyd | 1b14afa | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 90 | struct clk_hw *parent, *best_parent = NULL; |
Emilio López | 862b728 | 2014-05-02 17:57:15 +0200 | [diff] [blame] | 91 | int i, num_parents; |
| 92 | unsigned long parent_rate, best = 0, child_rate, best_child_rate = 0; |
| 93 | |
| 94 | /* find the parent that can help provide the fastest rate <= rate */ |
Stephen Boyd | 497295a | 2015-06-25 16:53:23 -0700 | [diff] [blame] | 95 | num_parents = clk_hw_get_num_parents(hw); |
Emilio López | 862b728 | 2014-05-02 17:57:15 +0200 | [diff] [blame] | 96 | for (i = 0; i < num_parents; i++) { |
Stephen Boyd | 1b14afa | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 97 | parent = clk_hw_get_parent_by_index(hw, i); |
Emilio López | 862b728 | 2014-05-02 17:57:15 +0200 | [diff] [blame] | 98 | if (!parent) |
| 99 | continue; |
Stephen Boyd | 98d8a60 | 2015-06-29 16:56:30 -0700 | [diff] [blame] | 100 | if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) |
Stephen Boyd | 1b14afa | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 101 | parent_rate = clk_hw_round_rate(parent, req->rate); |
Emilio López | 862b728 | 2014-05-02 17:57:15 +0200 | [diff] [blame] | 102 | else |
Stephen Boyd | 1b14afa | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 103 | parent_rate = clk_hw_get_rate(parent); |
Emilio López | 862b728 | 2014-05-02 17:57:15 +0200 | [diff] [blame] | 104 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 105 | child_rate = clk_factors_round_rate(hw, req->rate, |
| 106 | &parent_rate); |
Emilio López | 862b728 | 2014-05-02 17:57:15 +0200 | [diff] [blame] | 107 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 108 | if (child_rate <= req->rate && child_rate > best_child_rate) { |
Emilio López | 862b728 | 2014-05-02 17:57:15 +0200 | [diff] [blame] | 109 | best_parent = parent; |
| 110 | best = parent_rate; |
| 111 | best_child_rate = child_rate; |
| 112 | } |
| 113 | } |
| 114 | |
Boris Brezillon | 57d866e | 2015-07-09 22:39:38 +0200 | [diff] [blame] | 115 | if (!best_parent) |
| 116 | return -EINVAL; |
| 117 | |
Stephen Boyd | 1b14afa | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 118 | req->best_parent_hw = best_parent; |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 119 | req->best_parent_rate = best; |
| 120 | req->rate = best_child_rate; |
Emilio López | 862b728 | 2014-05-02 17:57:15 +0200 | [diff] [blame] | 121 | |
Boris Brezillon | 0817b62 | 2015-07-07 20:48:08 +0200 | [diff] [blame] | 122 | return 0; |
Emilio López | 862b728 | 2014-05-02 17:57:15 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 125 | static int clk_factors_set_rate(struct clk_hw *hw, unsigned long rate, |
| 126 | unsigned long parent_rate) |
| 127 | { |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame^] | 128 | struct factors_request req = { |
| 129 | .rate = rate, |
| 130 | .parent_rate = parent_rate, |
| 131 | }; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 132 | u32 reg; |
| 133 | struct clk_factors *factors = to_clk_factors(hw); |
Chen-Yu Tsai | b3e919e | 2016-01-25 21:15:38 +0800 | [diff] [blame] | 134 | const struct clk_factors_config *config = factors->config; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 135 | unsigned long flags = 0; |
| 136 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame^] | 137 | factors->get_factors(&req); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 138 | |
| 139 | if (factors->lock) |
| 140 | spin_lock_irqsave(factors->lock, flags); |
| 141 | |
| 142 | /* Fetch the register value */ |
| 143 | reg = readl(factors->reg); |
| 144 | |
| 145 | /* Set up the new factors - macros do not do anything if width is 0 */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame^] | 146 | reg = FACTOR_SET(config->nshift, config->nwidth, reg, req.n); |
| 147 | reg = FACTOR_SET(config->kshift, config->kwidth, reg, req.k); |
| 148 | reg = FACTOR_SET(config->mshift, config->mwidth, reg, req.m); |
| 149 | reg = FACTOR_SET(config->pshift, config->pwidth, reg, req.p); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 150 | |
| 151 | /* Apply them now */ |
| 152 | writel(reg, factors->reg); |
| 153 | |
| 154 | /* delay 500us so pll stabilizes */ |
| 155 | __delay((rate >> 20) * 500 / 2); |
| 156 | |
| 157 | if (factors->lock) |
| 158 | spin_unlock_irqrestore(factors->lock, flags); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 163 | static const struct clk_ops clk_factors_ops = { |
Emilio López | 862b728 | 2014-05-02 17:57:15 +0200 | [diff] [blame] | 164 | .determine_rate = clk_factors_determine_rate, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 165 | .recalc_rate = clk_factors_recalc_rate, |
| 166 | .round_rate = clk_factors_round_rate, |
| 167 | .set_rate = clk_factors_set_rate, |
| 168 | }; |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 169 | |
Hans de Goede | 7c74c22 | 2014-11-23 14:38:07 +0100 | [diff] [blame] | 170 | struct clk *sunxi_factors_register(struct device_node *node, |
| 171 | const struct factors_data *data, |
| 172 | spinlock_t *lock, |
| 173 | void __iomem *reg) |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 174 | { |
| 175 | struct clk *clk; |
| 176 | struct clk_factors *factors; |
| 177 | struct clk_gate *gate = NULL; |
| 178 | struct clk_mux *mux = NULL; |
| 179 | struct clk_hw *gate_hw = NULL; |
| 180 | struct clk_hw *mux_hw = NULL; |
| 181 | const char *clk_name = node->name; |
| 182 | const char *parents[FACTORS_MAX_PARENTS]; |
Chen-Yu Tsai | 78ca95c | 2016-01-25 21:15:39 +0800 | [diff] [blame] | 183 | int ret, i = 0; |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 184 | |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 185 | /* if we have a mux, we will have >1 parents */ |
Dinh Nguyen | 8a53fb2 | 2015-07-06 22:59:05 -0500 | [diff] [blame] | 186 | i = of_clk_parent_fill(node, parents, FACTORS_MAX_PARENTS); |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * some factor clocks, such as pll5 and pll6, may have multiple |
| 190 | * outputs, and have their name designated in factors_data |
| 191 | */ |
| 192 | if (data->name) |
| 193 | clk_name = data->name; |
| 194 | else |
| 195 | of_property_read_string(node, "clock-output-names", &clk_name); |
| 196 | |
| 197 | factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL); |
| 198 | if (!factors) |
Chen-Yu Tsai | 78ca95c | 2016-01-25 21:15:39 +0800 | [diff] [blame] | 199 | goto err_factors; |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 200 | |
| 201 | /* set up factors properties */ |
| 202 | factors->reg = reg; |
| 203 | factors->config = data->table; |
| 204 | factors->get_factors = data->getter; |
| 205 | factors->lock = lock; |
| 206 | |
| 207 | /* Add a gate if this factor clock can be gated */ |
| 208 | if (data->enable) { |
| 209 | gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL); |
Chen-Yu Tsai | 78ca95c | 2016-01-25 21:15:39 +0800 | [diff] [blame] | 210 | if (!gate) |
| 211 | goto err_gate; |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 212 | |
Chen-Yu Tsai | 4cbeaeb | 2016-01-25 21:15:40 +0800 | [diff] [blame] | 213 | factors->gate = gate; |
| 214 | |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 215 | /* set up gate properties */ |
| 216 | gate->reg = reg; |
| 217 | gate->bit_idx = data->enable; |
| 218 | gate->lock = factors->lock; |
| 219 | gate_hw = &gate->hw; |
| 220 | } |
| 221 | |
| 222 | /* Add a mux if this factor clock can be muxed */ |
| 223 | if (data->mux) { |
| 224 | mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL); |
Chen-Yu Tsai | 78ca95c | 2016-01-25 21:15:39 +0800 | [diff] [blame] | 225 | if (!mux) |
| 226 | goto err_mux; |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 227 | |
Chen-Yu Tsai | 4cbeaeb | 2016-01-25 21:15:40 +0800 | [diff] [blame] | 228 | factors->mux = mux; |
| 229 | |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 230 | /* set up gate properties */ |
| 231 | mux->reg = reg; |
| 232 | mux->shift = data->mux; |
Chen-Yu Tsai | e94f8cb3 | 2014-10-20 22:10:26 +0800 | [diff] [blame] | 233 | mux->mask = data->muxmask; |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 234 | mux->lock = factors->lock; |
| 235 | mux_hw = &mux->hw; |
| 236 | } |
| 237 | |
| 238 | clk = clk_register_composite(NULL, clk_name, |
| 239 | parents, i, |
| 240 | mux_hw, &clk_mux_ops, |
| 241 | &factors->hw, &clk_factors_ops, |
| 242 | gate_hw, &clk_gate_ops, 0); |
Chen-Yu Tsai | 78ca95c | 2016-01-25 21:15:39 +0800 | [diff] [blame] | 243 | if (IS_ERR(clk)) |
| 244 | goto err_register; |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 245 | |
Chen-Yu Tsai | 78ca95c | 2016-01-25 21:15:39 +0800 | [diff] [blame] | 246 | ret = of_clk_add_provider(node, of_clk_src_simple_get, clk); |
| 247 | if (ret) |
| 248 | goto err_provider; |
| 249 | |
| 250 | ret = clk_register_clkdev(clk, clk_name, NULL); |
| 251 | if (ret) |
| 252 | goto err_clkdev; |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 253 | |
| 254 | return clk; |
Chen-Yu Tsai | 78ca95c | 2016-01-25 21:15:39 +0800 | [diff] [blame] | 255 | |
| 256 | err_clkdev: |
| 257 | of_clk_del_provider(node); |
| 258 | err_provider: |
| 259 | /* TODO: The composite clock stuff will leak a bit here. */ |
| 260 | clk_unregister(clk); |
| 261 | err_register: |
| 262 | kfree(mux); |
| 263 | err_mux: |
| 264 | kfree(gate); |
| 265 | err_gate: |
| 266 | kfree(factors); |
| 267 | err_factors: |
| 268 | return NULL; |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 269 | } |
Chen-Yu Tsai | 4cbeaeb | 2016-01-25 21:15:40 +0800 | [diff] [blame] | 270 | |
| 271 | void sunxi_factors_unregister(struct device_node *node, struct clk *clk) |
| 272 | { |
| 273 | struct clk_hw *hw = __clk_get_hw(clk); |
| 274 | struct clk_factors *factors; |
| 275 | const char *name; |
| 276 | |
| 277 | if (!hw) |
| 278 | return; |
| 279 | |
| 280 | factors = to_clk_factors(hw); |
| 281 | name = clk_hw_get_name(hw); |
| 282 | |
| 283 | /* No unregister call for clkdev_* */ |
| 284 | of_clk_del_provider(node); |
| 285 | /* TODO: The composite clock stuff will leak a bit here. */ |
| 286 | clk_unregister(clk); |
| 287 | kfree(factors->mux); |
| 288 | kfree(factors->gate); |
| 289 | kfree(factors); |
| 290 | } |