Stephen Boyd | e1bd55e | 2018-12-11 09:57:48 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> |
| 4 | * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org> |
| 5 | * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org> |
| 6 | * |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 7 | * Adjustable divider clock implementation |
| 8 | */ |
| 9 | |
| 10 | #include <linux/clk-provider.h> |
Michael Walle | 2679269 | 2020-11-08 19:51:09 +0100 | [diff] [blame] | 11 | #include <linux/device.h> |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/string.h> |
James Hogan | 1a3cd18 | 2013-01-15 10:28:05 +0000 | [diff] [blame] | 17 | #include <linux/log2.h> |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * DOC: basic adjustable divider clock that cannot gate |
| 21 | * |
| 22 | * Traits of this clock: |
| 23 | * prepare - clk_prepare only ensures that parents are prepared |
| 24 | * enable - clk_enable only ensures that parents are enabled |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 25 | * rate - rate is adjustable. clk->rate = ceiling(parent->rate / divisor) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 26 | * parent - fixed parent. No clk_set_parent support |
| 27 | */ |
| 28 | |
Jonas Gorski | 434d69f | 2019-04-18 13:12:04 +0200 | [diff] [blame] | 29 | static inline u32 clk_div_readl(struct clk_divider *divider) |
| 30 | { |
| 31 | if (divider->flags & CLK_DIVIDER_BIG_ENDIAN) |
| 32 | return ioread32be(divider->reg); |
| 33 | |
Jonas Gorski | 5834fd7 | 2019-04-18 13:12:11 +0200 | [diff] [blame] | 34 | return readl(divider->reg); |
Jonas Gorski | 434d69f | 2019-04-18 13:12:04 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static inline void clk_div_writel(struct clk_divider *divider, u32 val) |
| 38 | { |
| 39 | if (divider->flags & CLK_DIVIDER_BIG_ENDIAN) |
| 40 | iowrite32be(val, divider->reg); |
| 41 | else |
Jonas Gorski | 5834fd7 | 2019-04-18 13:12:11 +0200 | [diff] [blame] | 42 | writel(val, divider->reg); |
Jonas Gorski | 434d69f | 2019-04-18 13:12:04 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Stephen Boyd | fab88ca | 2015-11-30 17:31:38 -0800 | [diff] [blame] | 45 | static unsigned int _get_table_maxdiv(const struct clk_div_table *table, |
| 46 | u8 width) |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 47 | { |
Jerome Brunet | e6d3cc7 | 2018-02-14 14:43:33 +0100 | [diff] [blame] | 48 | unsigned int maxdiv = 0, mask = clk_div_mask(width); |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 49 | const struct clk_div_table *clkt; |
| 50 | |
| 51 | for (clkt = table; clkt->div; clkt++) |
Stephen Boyd | fab88ca | 2015-11-30 17:31:38 -0800 | [diff] [blame] | 52 | if (clkt->div > maxdiv && clkt->val <= mask) |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 53 | maxdiv = clkt->div; |
| 54 | return maxdiv; |
| 55 | } |
| 56 | |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 57 | static unsigned int _get_table_mindiv(const struct clk_div_table *table) |
| 58 | { |
| 59 | unsigned int mindiv = UINT_MAX; |
| 60 | const struct clk_div_table *clkt; |
| 61 | |
| 62 | for (clkt = table; clkt->div; clkt++) |
| 63 | if (clkt->div < mindiv) |
| 64 | mindiv = clkt->div; |
| 65 | return mindiv; |
| 66 | } |
| 67 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 68 | static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width, |
| 69 | unsigned long flags) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 70 | { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 71 | if (flags & CLK_DIVIDER_ONE_BASED) |
Jerome Brunet | e6d3cc7 | 2018-02-14 14:43:33 +0100 | [diff] [blame] | 72 | return clk_div_mask(width); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 73 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
Jerome Brunet | e6d3cc7 | 2018-02-14 14:43:33 +0100 | [diff] [blame] | 74 | return 1 << clk_div_mask(width); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 75 | if (table) |
Stephen Boyd | fab88ca | 2015-11-30 17:31:38 -0800 | [diff] [blame] | 76 | return _get_table_maxdiv(table, width); |
Jerome Brunet | e6d3cc7 | 2018-02-14 14:43:33 +0100 | [diff] [blame] | 77 | return clk_div_mask(width) + 1; |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 78 | } |
| 79 | |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 80 | static unsigned int _get_table_div(const struct clk_div_table *table, |
| 81 | unsigned int val) |
| 82 | { |
| 83 | const struct clk_div_table *clkt; |
| 84 | |
| 85 | for (clkt = table; clkt->div; clkt++) |
| 86 | if (clkt->val == val) |
| 87 | return clkt->div; |
| 88 | return 0; |
| 89 | } |
| 90 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 91 | static unsigned int _get_div(const struct clk_div_table *table, |
Jim Quinlan | afe76c8f | 2015-05-15 15:45:47 -0400 | [diff] [blame] | 92 | unsigned int val, unsigned long flags, u8 width) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 93 | { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 94 | if (flags & CLK_DIVIDER_ONE_BASED) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 95 | return val; |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 96 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 97 | return 1 << val; |
Jim Quinlan | afe76c8f | 2015-05-15 15:45:47 -0400 | [diff] [blame] | 98 | if (flags & CLK_DIVIDER_MAX_AT_ZERO) |
Jerome Brunet | e6d3cc7 | 2018-02-14 14:43:33 +0100 | [diff] [blame] | 99 | return val ? val : clk_div_mask(width) + 1; |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 100 | if (table) |
| 101 | return _get_table_div(table, val); |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 102 | return val + 1; |
| 103 | } |
| 104 | |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 105 | static unsigned int _get_table_val(const struct clk_div_table *table, |
| 106 | unsigned int div) |
| 107 | { |
| 108 | const struct clk_div_table *clkt; |
| 109 | |
| 110 | for (clkt = table; clkt->div; clkt++) |
| 111 | if (clkt->div == div) |
| 112 | return clkt->val; |
| 113 | return 0; |
| 114 | } |
| 115 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 116 | static unsigned int _get_val(const struct clk_div_table *table, |
Jim Quinlan | afe76c8f | 2015-05-15 15:45:47 -0400 | [diff] [blame] | 117 | unsigned int div, unsigned long flags, u8 width) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 118 | { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 119 | if (flags & CLK_DIVIDER_ONE_BASED) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 120 | return div; |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 121 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 122 | return __ffs(div); |
Jim Quinlan | afe76c8f | 2015-05-15 15:45:47 -0400 | [diff] [blame] | 123 | if (flags & CLK_DIVIDER_MAX_AT_ZERO) |
Jerome Brunet | e6d3cc7 | 2018-02-14 14:43:33 +0100 | [diff] [blame] | 124 | return (div == clk_div_mask(width) + 1) ? 0 : div; |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 125 | if (table) |
| 126 | return _get_table_val(table, div); |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 127 | return div - 1; |
| 128 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 129 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 130 | unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate, |
| 131 | unsigned int val, |
| 132 | const struct clk_div_table *table, |
Jerome Brunet | 12a26c2 | 2017-12-21 17:30:54 +0100 | [diff] [blame] | 133 | unsigned long flags, unsigned long width) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 134 | { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 135 | unsigned int div; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 136 | |
Jerome Brunet | 12a26c2 | 2017-12-21 17:30:54 +0100 | [diff] [blame] | 137 | div = _get_div(table, val, flags, width); |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 138 | if (!div) { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 139 | WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO), |
Soren Brinkmann | 056b2053 | 2013-04-02 15:36:56 -0700 | [diff] [blame] | 140 | "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n", |
Stephen Boyd | 2f508a9 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 141 | clk_hw_get_name(hw)); |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 142 | return parent_rate; |
| 143 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 144 | |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 145 | return DIV_ROUND_UP_ULL((u64)parent_rate, div); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 146 | } |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 147 | EXPORT_SYMBOL_GPL(divider_recalc_rate); |
| 148 | |
| 149 | static unsigned long clk_divider_recalc_rate(struct clk_hw *hw, |
| 150 | unsigned long parent_rate) |
| 151 | { |
| 152 | struct clk_divider *divider = to_clk_divider(hw); |
| 153 | unsigned int val; |
| 154 | |
Jonas Gorski | 434d69f | 2019-04-18 13:12:04 +0200 | [diff] [blame] | 155 | val = clk_div_readl(divider) >> divider->shift; |
Jerome Brunet | e6d3cc7 | 2018-02-14 14:43:33 +0100 | [diff] [blame] | 156 | val &= clk_div_mask(divider->width); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 157 | |
| 158 | return divider_recalc_rate(hw, parent_rate, val, divider->table, |
Jerome Brunet | 12a26c2 | 2017-12-21 17:30:54 +0100 | [diff] [blame] | 159 | divider->flags, divider->width); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 160 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 161 | |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 162 | static bool _is_valid_table_div(const struct clk_div_table *table, |
| 163 | unsigned int div) |
| 164 | { |
| 165 | const struct clk_div_table *clkt; |
| 166 | |
| 167 | for (clkt = table; clkt->div; clkt++) |
| 168 | if (clkt->div == div) |
| 169 | return true; |
| 170 | return false; |
| 171 | } |
| 172 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 173 | static bool _is_valid_div(const struct clk_div_table *table, unsigned int div, |
| 174 | unsigned long flags) |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 175 | { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 176 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
James Hogan | 1a3cd18 | 2013-01-15 10:28:05 +0000 | [diff] [blame] | 177 | return is_power_of_2(div); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 178 | if (table) |
| 179 | return _is_valid_table_div(table, div); |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 180 | return true; |
| 181 | } |
| 182 | |
Maxime COQUELIN | dd23c2c | 2014-01-29 17:24:06 +0100 | [diff] [blame] | 183 | static int _round_up_table(const struct clk_div_table *table, int div) |
| 184 | { |
| 185 | const struct clk_div_table *clkt; |
Maxime COQUELIN | fe52e75 | 2014-05-07 18:48:52 +0200 | [diff] [blame] | 186 | int up = INT_MAX; |
Maxime COQUELIN | dd23c2c | 2014-01-29 17:24:06 +0100 | [diff] [blame] | 187 | |
| 188 | for (clkt = table; clkt->div; clkt++) { |
| 189 | if (clkt->div == div) |
| 190 | return clkt->div; |
| 191 | else if (clkt->div < div) |
| 192 | continue; |
| 193 | |
| 194 | if ((clkt->div - div) < (up - div)) |
| 195 | up = clkt->div; |
| 196 | } |
| 197 | |
| 198 | return up; |
| 199 | } |
| 200 | |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 201 | static int _round_down_table(const struct clk_div_table *table, int div) |
| 202 | { |
| 203 | const struct clk_div_table *clkt; |
| 204 | int down = _get_table_mindiv(table); |
| 205 | |
| 206 | for (clkt = table; clkt->div; clkt++) { |
| 207 | if (clkt->div == div) |
| 208 | return clkt->div; |
| 209 | else if (clkt->div > div) |
| 210 | continue; |
| 211 | |
| 212 | if ((div - clkt->div) < (div - down)) |
| 213 | down = clkt->div; |
| 214 | } |
| 215 | |
| 216 | return down; |
| 217 | } |
| 218 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 219 | static int _div_round_up(const struct clk_div_table *table, |
| 220 | unsigned long parent_rate, unsigned long rate, |
| 221 | unsigned long flags) |
Maxime COQUELIN | dd23c2c | 2014-01-29 17:24:06 +0100 | [diff] [blame] | 222 | { |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 223 | int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); |
Maxime COQUELIN | dd23c2c | 2014-01-29 17:24:06 +0100 | [diff] [blame] | 224 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 225 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
Maxime COQUELIN | dd23c2c | 2014-01-29 17:24:06 +0100 | [diff] [blame] | 226 | div = __roundup_pow_of_two(div); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 227 | if (table) |
| 228 | div = _round_up_table(table, div); |
Maxime COQUELIN | dd23c2c | 2014-01-29 17:24:06 +0100 | [diff] [blame] | 229 | |
| 230 | return div; |
| 231 | } |
| 232 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 233 | static int _div_round_closest(const struct clk_div_table *table, |
| 234 | unsigned long parent_rate, unsigned long rate, |
| 235 | unsigned long flags) |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 236 | { |
Uwe Kleine-König | 9315514 | 2015-02-21 11:40:25 +0100 | [diff] [blame] | 237 | int up, down; |
Uwe Kleine-König | 26bac95 | 2015-02-21 11:40:24 +0100 | [diff] [blame] | 238 | unsigned long up_rate, down_rate; |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 239 | |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 240 | up = DIV_ROUND_UP_ULL((u64)parent_rate, rate); |
Uwe Kleine-König | 9315514 | 2015-02-21 11:40:25 +0100 | [diff] [blame] | 241 | down = parent_rate / rate; |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 242 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 243 | if (flags & CLK_DIVIDER_POWER_OF_TWO) { |
Uwe Kleine-König | 9315514 | 2015-02-21 11:40:25 +0100 | [diff] [blame] | 244 | up = __roundup_pow_of_two(up); |
| 245 | down = __rounddown_pow_of_two(down); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 246 | } else if (table) { |
Uwe Kleine-König | 9315514 | 2015-02-21 11:40:25 +0100 | [diff] [blame] | 247 | up = _round_up_table(table, up); |
| 248 | down = _round_down_table(table, down); |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 251 | up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up); |
| 252 | down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down); |
Uwe Kleine-König | 26bac95 | 2015-02-21 11:40:24 +0100 | [diff] [blame] | 253 | |
| 254 | return (rate - up_rate) <= (down_rate - rate) ? up : down; |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 257 | static int _div_round(const struct clk_div_table *table, |
| 258 | unsigned long parent_rate, unsigned long rate, |
| 259 | unsigned long flags) |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 260 | { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 261 | if (flags & CLK_DIVIDER_ROUND_CLOSEST) |
| 262 | return _div_round_closest(table, parent_rate, rate, flags); |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 263 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 264 | return _div_round_up(table, parent_rate, rate, flags); |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 267 | static bool _is_best_div(unsigned long rate, unsigned long now, |
| 268 | unsigned long best, unsigned long flags) |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 269 | { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 270 | if (flags & CLK_DIVIDER_ROUND_CLOSEST) |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 271 | return abs(rate - now) < abs(rate - best); |
| 272 | |
| 273 | return now <= rate && now > best; |
| 274 | } |
| 275 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 276 | static int _next_div(const struct clk_div_table *table, int div, |
| 277 | unsigned long flags) |
Maxime COQUELIN | 0e2de78 | 2014-01-29 17:24:08 +0100 | [diff] [blame] | 278 | { |
| 279 | div++; |
| 280 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 281 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
Maxime COQUELIN | 0e2de78 | 2014-01-29 17:24:08 +0100 | [diff] [blame] | 282 | return __roundup_pow_of_two(div); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 283 | if (table) |
| 284 | return _round_up_table(table, div); |
Maxime COQUELIN | 0e2de78 | 2014-01-29 17:24:08 +0100 | [diff] [blame] | 285 | |
| 286 | return div; |
| 287 | } |
| 288 | |
Maxime Ripard | 22833a9 | 2017-05-17 09:40:30 +0200 | [diff] [blame] | 289 | static int clk_divider_bestdiv(struct clk_hw *hw, struct clk_hw *parent, |
| 290 | unsigned long rate, |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 291 | unsigned long *best_parent_rate, |
| 292 | const struct clk_div_table *table, u8 width, |
| 293 | unsigned long flags) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 294 | { |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 295 | int i, bestdiv = 0; |
| 296 | unsigned long parent_rate, best = 0, now, maxdiv; |
Shawn Guo | 081c902 | 2013-06-02 22:20:55 +0800 | [diff] [blame] | 297 | unsigned long parent_rate_saved = *best_parent_rate; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 298 | |
| 299 | if (!rate) |
| 300 | rate = 1; |
| 301 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 302 | maxdiv = _get_maxdiv(table, width, flags); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 303 | |
Stephen Boyd | 98d8a60 | 2015-06-29 16:56:30 -0700 | [diff] [blame] | 304 | if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) { |
Shawn Guo | 81536e0 | 2012-04-12 20:50:17 +0800 | [diff] [blame] | 305 | parent_rate = *best_parent_rate; |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 306 | bestdiv = _div_round(table, parent_rate, rate, flags); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 307 | bestdiv = bestdiv == 0 ? 1 : bestdiv; |
| 308 | bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv; |
| 309 | return bestdiv; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * The maximum divider we can use without overflowing |
| 314 | * unsigned long in rate * i below |
| 315 | */ |
| 316 | maxdiv = min(ULONG_MAX / rate, maxdiv); |
| 317 | |
Masahiro Yamada | 653d145 | 2016-01-05 12:43:41 +0900 | [diff] [blame] | 318 | for (i = _next_div(table, 0, flags); i <= maxdiv; |
| 319 | i = _next_div(table, i, flags)) { |
Shawn Guo | 081c902 | 2013-06-02 22:20:55 +0800 | [diff] [blame] | 320 | if (rate * i == parent_rate_saved) { |
| 321 | /* |
| 322 | * It's the most ideal case if the requested rate can be |
| 323 | * divided from parent clock without needing to change |
| 324 | * parent rate, so return the divider immediately. |
| 325 | */ |
| 326 | *best_parent_rate = parent_rate_saved; |
| 327 | return i; |
| 328 | } |
Maxime Ripard | 22833a9 | 2017-05-17 09:40:30 +0200 | [diff] [blame] | 329 | parent_rate = clk_hw_round_rate(parent, rate * i); |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 330 | now = DIV_ROUND_UP_ULL((u64)parent_rate, i); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 331 | if (_is_best_div(rate, now, best, flags)) { |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 332 | bestdiv = i; |
| 333 | best = now; |
| 334 | *best_parent_rate = parent_rate; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | if (!bestdiv) { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 339 | bestdiv = _get_maxdiv(table, width, flags); |
Maxime Ripard | 22833a9 | 2017-05-17 09:40:30 +0200 | [diff] [blame] | 340 | *best_parent_rate = clk_hw_round_rate(parent, 1); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | return bestdiv; |
| 344 | } |
| 345 | |
Martin Blumenstingl | bbd7a6c | 2021-06-28 00:39:57 +0200 | [diff] [blame] | 346 | int divider_determine_rate(struct clk_hw *hw, struct clk_rate_request *req, |
| 347 | const struct clk_div_table *table, u8 width, |
| 348 | unsigned long flags) |
| 349 | { |
| 350 | int div; |
| 351 | |
| 352 | div = clk_divider_bestdiv(hw, req->best_parent_hw, req->rate, |
| 353 | &req->best_parent_rate, table, width, flags); |
| 354 | |
| 355 | req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div); |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | EXPORT_SYMBOL_GPL(divider_determine_rate); |
| 360 | |
| 361 | int divider_ro_determine_rate(struct clk_hw *hw, struct clk_rate_request *req, |
| 362 | const struct clk_div_table *table, u8 width, |
| 363 | unsigned long flags, unsigned int val) |
| 364 | { |
| 365 | int div; |
| 366 | |
| 367 | div = _get_div(table, val, flags, width); |
| 368 | |
| 369 | /* Even a read-only clock can propagate a rate change */ |
| 370 | if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) { |
| 371 | if (!req->best_parent_hw) |
| 372 | return -EINVAL; |
| 373 | |
| 374 | req->best_parent_rate = clk_hw_round_rate(req->best_parent_hw, |
| 375 | req->rate * div); |
| 376 | } |
| 377 | |
| 378 | req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div); |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | EXPORT_SYMBOL_GPL(divider_ro_determine_rate); |
| 383 | |
Maxime Ripard | 22833a9 | 2017-05-17 09:40:30 +0200 | [diff] [blame] | 384 | long divider_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent, |
| 385 | unsigned long rate, unsigned long *prate, |
| 386 | const struct clk_div_table *table, |
| 387 | u8 width, unsigned long flags) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 388 | { |
Martin Blumenstingl | bbd7a6c | 2021-06-28 00:39:57 +0200 | [diff] [blame] | 389 | struct clk_rate_request req = { |
| 390 | .rate = rate, |
| 391 | .best_parent_rate = *prate, |
| 392 | .best_parent_hw = parent, |
| 393 | }; |
| 394 | int ret; |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 395 | |
Martin Blumenstingl | bbd7a6c | 2021-06-28 00:39:57 +0200 | [diff] [blame] | 396 | ret = divider_determine_rate(hw, &req, table, width, flags); |
| 397 | if (ret) |
| 398 | return ret; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 399 | |
Martin Blumenstingl | bbd7a6c | 2021-06-28 00:39:57 +0200 | [diff] [blame] | 400 | *prate = req.best_parent_rate; |
| 401 | |
| 402 | return req.rate; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 403 | } |
Maxime Ripard | 22833a9 | 2017-05-17 09:40:30 +0200 | [diff] [blame] | 404 | EXPORT_SYMBOL_GPL(divider_round_rate_parent); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 405 | |
Jerome Brunet | b15ee49 | 2018-02-14 14:43:39 +0100 | [diff] [blame] | 406 | long divider_ro_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent, |
| 407 | unsigned long rate, unsigned long *prate, |
| 408 | const struct clk_div_table *table, u8 width, |
| 409 | unsigned long flags, unsigned int val) |
| 410 | { |
Martin Blumenstingl | bbd7a6c | 2021-06-28 00:39:57 +0200 | [diff] [blame] | 411 | struct clk_rate_request req = { |
| 412 | .rate = rate, |
| 413 | .best_parent_rate = *prate, |
| 414 | .best_parent_hw = parent, |
| 415 | }; |
| 416 | int ret; |
Jerome Brunet | b15ee49 | 2018-02-14 14:43:39 +0100 | [diff] [blame] | 417 | |
Martin Blumenstingl | bbd7a6c | 2021-06-28 00:39:57 +0200 | [diff] [blame] | 418 | ret = divider_ro_determine_rate(hw, &req, table, width, flags, val); |
| 419 | if (ret) |
| 420 | return ret; |
Jerome Brunet | b15ee49 | 2018-02-14 14:43:39 +0100 | [diff] [blame] | 421 | |
Martin Blumenstingl | bbd7a6c | 2021-06-28 00:39:57 +0200 | [diff] [blame] | 422 | *prate = req.best_parent_rate; |
Jerome Brunet | b15ee49 | 2018-02-14 14:43:39 +0100 | [diff] [blame] | 423 | |
Martin Blumenstingl | bbd7a6c | 2021-06-28 00:39:57 +0200 | [diff] [blame] | 424 | return req.rate; |
Jerome Brunet | b15ee49 | 2018-02-14 14:43:39 +0100 | [diff] [blame] | 425 | } |
| 426 | EXPORT_SYMBOL_GPL(divider_ro_round_rate_parent); |
| 427 | |
Stephen Boyd | 783d08b | 2021-07-01 18:10:58 -0700 | [diff] [blame] | 428 | static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate, |
| 429 | unsigned long *prate) |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 430 | { |
| 431 | struct clk_divider *divider = to_clk_divider(hw); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 432 | |
| 433 | /* if read only, just return current value */ |
| 434 | if (divider->flags & CLK_DIVIDER_READ_ONLY) { |
Jerome Brunet | b15ee49 | 2018-02-14 14:43:39 +0100 | [diff] [blame] | 435 | u32 val; |
| 436 | |
Jonas Gorski | 434d69f | 2019-04-18 13:12:04 +0200 | [diff] [blame] | 437 | val = clk_div_readl(divider) >> divider->shift; |
Jerome Brunet | b15ee49 | 2018-02-14 14:43:39 +0100 | [diff] [blame] | 438 | val &= clk_div_mask(divider->width); |
| 439 | |
Stephen Boyd | 783d08b | 2021-07-01 18:10:58 -0700 | [diff] [blame] | 440 | return divider_ro_round_rate(hw, rate, prate, divider->table, |
| 441 | divider->width, divider->flags, |
| 442 | val); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Stephen Boyd | 783d08b | 2021-07-01 18:10:58 -0700 | [diff] [blame] | 445 | return divider_round_rate(hw, rate, prate, divider->table, |
| 446 | divider->width, divider->flags); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Martin Blumenstingl | 69a00fb | 2021-07-03 00:51:40 +0200 | [diff] [blame] | 449 | static int clk_divider_determine_rate(struct clk_hw *hw, |
| 450 | struct clk_rate_request *req) |
| 451 | { |
| 452 | struct clk_divider *divider = to_clk_divider(hw); |
| 453 | |
| 454 | /* if read only, just return current value */ |
| 455 | if (divider->flags & CLK_DIVIDER_READ_ONLY) { |
| 456 | u32 val; |
| 457 | |
| 458 | val = clk_div_readl(divider) >> divider->shift; |
| 459 | val &= clk_div_mask(divider->width); |
| 460 | |
| 461 | return divider_ro_determine_rate(hw, req, divider->table, |
| 462 | divider->width, |
| 463 | divider->flags, val); |
| 464 | } |
| 465 | |
| 466 | return divider_determine_rate(hw, req, divider->table, divider->width, |
| 467 | divider->flags); |
| 468 | } |
| 469 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 470 | int divider_get_val(unsigned long rate, unsigned long parent_rate, |
| 471 | const struct clk_div_table *table, u8 width, |
| 472 | unsigned long flags) |
| 473 | { |
| 474 | unsigned int div, value; |
| 475 | |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 476 | div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 477 | |
| 478 | if (!_is_valid_div(table, div, flags)) |
| 479 | return -EINVAL; |
| 480 | |
Jim Quinlan | afe76c8f | 2015-05-15 15:45:47 -0400 | [diff] [blame] | 481 | value = _get_val(table, div, flags, width); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 482 | |
Jerome Brunet | e6d3cc7 | 2018-02-14 14:43:33 +0100 | [diff] [blame] | 483 | return min_t(unsigned int, value, clk_div_mask(width)); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 484 | } |
| 485 | EXPORT_SYMBOL_GPL(divider_get_val); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 486 | |
Shawn Guo | 1c0035d | 2012-04-12 20:50:18 +0800 | [diff] [blame] | 487 | static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, |
| 488 | unsigned long parent_rate) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 489 | { |
| 490 | struct clk_divider *divider = to_clk_divider(hw); |
Alex Frid | 2316a7a | 2017-07-25 13:18:40 +0300 | [diff] [blame] | 491 | int value; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 492 | unsigned long flags = 0; |
| 493 | u32 val; |
| 494 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 495 | value = divider_get_val(rate, parent_rate, divider->table, |
| 496 | divider->width, divider->flags); |
Alex Frid | 2316a7a | 2017-07-25 13:18:40 +0300 | [diff] [blame] | 497 | if (value < 0) |
| 498 | return value; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 499 | |
| 500 | if (divider->lock) |
| 501 | spin_lock_irqsave(divider->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 502 | else |
| 503 | __acquire(divider->lock); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 504 | |
Haojian Zhuang | d57dfe7 | 2013-06-08 22:47:18 +0800 | [diff] [blame] | 505 | if (divider->flags & CLK_DIVIDER_HIWORD_MASK) { |
Jerome Brunet | e6d3cc7 | 2018-02-14 14:43:33 +0100 | [diff] [blame] | 506 | val = clk_div_mask(divider->width) << (divider->shift + 16); |
Haojian Zhuang | d57dfe7 | 2013-06-08 22:47:18 +0800 | [diff] [blame] | 507 | } else { |
Jonas Gorski | 434d69f | 2019-04-18 13:12:04 +0200 | [diff] [blame] | 508 | val = clk_div_readl(divider); |
Jerome Brunet | e6d3cc7 | 2018-02-14 14:43:33 +0100 | [diff] [blame] | 509 | val &= ~(clk_div_mask(divider->width) << divider->shift); |
Haojian Zhuang | d57dfe7 | 2013-06-08 22:47:18 +0800 | [diff] [blame] | 510 | } |
Alex Frid | 2316a7a | 2017-07-25 13:18:40 +0300 | [diff] [blame] | 511 | val |= (u32)value << divider->shift; |
Jonas Gorski | 434d69f | 2019-04-18 13:12:04 +0200 | [diff] [blame] | 512 | clk_div_writel(divider, val); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 513 | |
| 514 | if (divider->lock) |
| 515 | spin_unlock_irqrestore(divider->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 516 | else |
| 517 | __release(divider->lock); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 518 | |
| 519 | return 0; |
| 520 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 521 | |
Shawn Guo | 822c250 | 2012-03-27 15:23:22 +0800 | [diff] [blame] | 522 | const struct clk_ops clk_divider_ops = { |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 523 | .recalc_rate = clk_divider_recalc_rate, |
Stephen Boyd | 783d08b | 2021-07-01 18:10:58 -0700 | [diff] [blame] | 524 | .round_rate = clk_divider_round_rate, |
Martin Blumenstingl | 69a00fb | 2021-07-03 00:51:40 +0200 | [diff] [blame] | 525 | .determine_rate = clk_divider_determine_rate, |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 526 | .set_rate = clk_divider_set_rate, |
| 527 | }; |
| 528 | EXPORT_SYMBOL_GPL(clk_divider_ops); |
| 529 | |
Heiko Stuebner | 5035981 | 2016-01-21 21:53:09 +0100 | [diff] [blame] | 530 | const struct clk_ops clk_divider_ro_ops = { |
| 531 | .recalc_rate = clk_divider_recalc_rate, |
Stephen Boyd | 783d08b | 2021-07-01 18:10:58 -0700 | [diff] [blame] | 532 | .round_rate = clk_divider_round_rate, |
Martin Blumenstingl | 69a00fb | 2021-07-03 00:51:40 +0200 | [diff] [blame] | 533 | .determine_rate = clk_divider_determine_rate, |
Heiko Stuebner | 5035981 | 2016-01-21 21:53:09 +0100 | [diff] [blame] | 534 | }; |
| 535 | EXPORT_SYMBOL_GPL(clk_divider_ro_ops); |
| 536 | |
Stephen Boyd | ff25881 | 2019-08-30 08:09:23 -0700 | [diff] [blame] | 537 | struct clk_hw *__clk_hw_register_divider(struct device *dev, |
| 538 | struct device_node *np, const char *name, |
| 539 | const char *parent_name, const struct clk_hw *parent_hw, |
| 540 | const struct clk_parent_data *parent_data, unsigned long flags, |
| 541 | void __iomem *reg, u8 shift, u8 width, u8 clk_divider_flags, |
| 542 | const struct clk_div_table *table, spinlock_t *lock) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 543 | { |
| 544 | struct clk_divider *div; |
Stephen Boyd | eb7d264 | 2016-02-06 23:26:37 -0800 | [diff] [blame] | 545 | struct clk_hw *hw; |
Manivannan Sadhasivam | cc819cf | 2019-11-15 21:58:55 +0530 | [diff] [blame] | 546 | struct clk_init_data init = {}; |
Stephen Boyd | eb7d264 | 2016-02-06 23:26:37 -0800 | [diff] [blame] | 547 | int ret; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 548 | |
Haojian Zhuang | d57dfe7 | 2013-06-08 22:47:18 +0800 | [diff] [blame] | 549 | if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) { |
| 550 | if (width + shift > 16) { |
| 551 | pr_warn("divider value exceeds LOWORD field\n"); |
| 552 | return ERR_PTR(-EINVAL); |
| 553 | } |
| 554 | } |
| 555 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 556 | /* allocate the divider */ |
Stephen Boyd | d122db7 | 2015-05-14 16:47:10 -0700 | [diff] [blame] | 557 | div = kzalloc(sizeof(*div), GFP_KERNEL); |
| 558 | if (!div) |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 559 | return ERR_PTR(-ENOMEM); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 560 | |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 561 | init.name = name; |
Heiko Stuebner | 5035981 | 2016-01-21 21:53:09 +0100 | [diff] [blame] | 562 | if (clk_divider_flags & CLK_DIVIDER_READ_ONLY) |
| 563 | init.ops = &clk_divider_ro_ops; |
| 564 | else |
| 565 | init.ops = &clk_divider_ops; |
Stephen Boyd | 90b6c5c | 2019-04-25 10:57:37 -0700 | [diff] [blame] | 566 | init.flags = flags; |
Michael Tretter | 0225dae | 2021-01-21 08:16:46 +0100 | [diff] [blame] | 567 | init.parent_names = parent_name ? &parent_name : NULL; |
| 568 | init.parent_hws = parent_hw ? &parent_hw : NULL; |
| 569 | init.parent_data = parent_data; |
| 570 | if (parent_name || parent_hw || parent_data) |
| 571 | init.num_parents = 1; |
| 572 | else |
| 573 | init.num_parents = 0; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 574 | |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 575 | /* struct clk_divider assignments */ |
| 576 | div->reg = reg; |
| 577 | div->shift = shift; |
| 578 | div->width = width; |
| 579 | div->flags = clk_divider_flags; |
| 580 | div->lock = lock; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 581 | div->hw.init = &init; |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 582 | div->table = table; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 583 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 584 | /* register the clock */ |
Stephen Boyd | eb7d264 | 2016-02-06 23:26:37 -0800 | [diff] [blame] | 585 | hw = &div->hw; |
| 586 | ret = clk_hw_register(dev, hw); |
| 587 | if (ret) { |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 588 | kfree(div); |
Stephen Boyd | eb7d264 | 2016-02-06 23:26:37 -0800 | [diff] [blame] | 589 | hw = ERR_PTR(ret); |
| 590 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 591 | |
Stephen Boyd | eb7d264 | 2016-02-06 23:26:37 -0800 | [diff] [blame] | 592 | return hw; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 593 | } |
Stephen Boyd | ff25881 | 2019-08-30 08:09:23 -0700 | [diff] [blame] | 594 | EXPORT_SYMBOL_GPL(__clk_hw_register_divider); |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 595 | |
| 596 | /** |
| 597 | * clk_register_divider_table - register a table based divider clock with |
| 598 | * the clock framework |
| 599 | * @dev: device registering this clock |
| 600 | * @name: name of this clock |
| 601 | * @parent_name: name of clock's parent |
| 602 | * @flags: framework-specific flags |
| 603 | * @reg: register address to adjust divider |
| 604 | * @shift: number of bits to shift the bitfield |
| 605 | * @width: width of the bitfield |
| 606 | * @clk_divider_flags: divider-specific flags for this clock |
| 607 | * @table: array of divider/value pairs ending with a div set to 0 |
| 608 | * @lock: shared register lock for this clock |
| 609 | */ |
| 610 | struct clk *clk_register_divider_table(struct device *dev, const char *name, |
| 611 | const char *parent_name, unsigned long flags, |
| 612 | void __iomem *reg, u8 shift, u8 width, |
| 613 | u8 clk_divider_flags, const struct clk_div_table *table, |
| 614 | spinlock_t *lock) |
| 615 | { |
Stephen Boyd | eb7d264 | 2016-02-06 23:26:37 -0800 | [diff] [blame] | 616 | struct clk_hw *hw; |
| 617 | |
Stephen Boyd | ff25881 | 2019-08-30 08:09:23 -0700 | [diff] [blame] | 618 | hw = __clk_hw_register_divider(dev, NULL, name, parent_name, NULL, |
| 619 | NULL, flags, reg, shift, width, clk_divider_flags, |
| 620 | table, lock); |
Stephen Boyd | eb7d264 | 2016-02-06 23:26:37 -0800 | [diff] [blame] | 621 | if (IS_ERR(hw)) |
| 622 | return ERR_CAST(hw); |
| 623 | return hw->clk; |
| 624 | } |
| 625 | EXPORT_SYMBOL_GPL(clk_register_divider_table); |
| 626 | |
Krzysztof Kozlowski | 4e3c021 | 2015-01-05 10:52:40 +0100 | [diff] [blame] | 627 | void clk_unregister_divider(struct clk *clk) |
| 628 | { |
| 629 | struct clk_divider *div; |
| 630 | struct clk_hw *hw; |
| 631 | |
| 632 | hw = __clk_get_hw(clk); |
| 633 | if (!hw) |
| 634 | return; |
| 635 | |
| 636 | div = to_clk_divider(hw); |
| 637 | |
| 638 | clk_unregister(clk); |
| 639 | kfree(div); |
| 640 | } |
| 641 | EXPORT_SYMBOL_GPL(clk_unregister_divider); |
Stephen Boyd | eb7d264 | 2016-02-06 23:26:37 -0800 | [diff] [blame] | 642 | |
| 643 | /** |
| 644 | * clk_hw_unregister_divider - unregister a clk divider |
| 645 | * @hw: hardware-specific clock data to unregister |
| 646 | */ |
| 647 | void clk_hw_unregister_divider(struct clk_hw *hw) |
| 648 | { |
| 649 | struct clk_divider *div; |
| 650 | |
| 651 | div = to_clk_divider(hw); |
| 652 | |
| 653 | clk_hw_unregister(hw); |
| 654 | kfree(div); |
| 655 | } |
| 656 | EXPORT_SYMBOL_GPL(clk_hw_unregister_divider); |
Michael Walle | 2679269 | 2020-11-08 19:51:09 +0100 | [diff] [blame] | 657 | |
| 658 | static void devm_clk_hw_release_divider(struct device *dev, void *res) |
| 659 | { |
| 660 | clk_hw_unregister_divider(*(struct clk_hw **)res); |
| 661 | } |
| 662 | |
| 663 | struct clk_hw *__devm_clk_hw_register_divider(struct device *dev, |
| 664 | struct device_node *np, const char *name, |
| 665 | const char *parent_name, const struct clk_hw *parent_hw, |
| 666 | const struct clk_parent_data *parent_data, unsigned long flags, |
| 667 | void __iomem *reg, u8 shift, u8 width, u8 clk_divider_flags, |
| 668 | const struct clk_div_table *table, spinlock_t *lock) |
| 669 | { |
| 670 | struct clk_hw **ptr, *hw; |
| 671 | |
| 672 | ptr = devres_alloc(devm_clk_hw_release_divider, sizeof(*ptr), GFP_KERNEL); |
| 673 | if (!ptr) |
| 674 | return ERR_PTR(-ENOMEM); |
| 675 | |
| 676 | hw = __clk_hw_register_divider(dev, np, name, parent_name, parent_hw, |
| 677 | parent_data, flags, reg, shift, width, |
| 678 | clk_divider_flags, table, lock); |
| 679 | |
| 680 | if (!IS_ERR(hw)) { |
| 681 | *ptr = hw; |
| 682 | devres_add(dev, ptr); |
| 683 | } else { |
| 684 | devres_free(ptr); |
| 685 | } |
| 686 | |
| 687 | return hw; |
| 688 | } |
| 689 | EXPORT_SYMBOL_GPL(__devm_clk_hw_register_divider); |