Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> |
| 3 | * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org> |
| 4 | * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org> |
| 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 | * Adjustable divider clock implementation |
| 11 | */ |
| 12 | |
| 13 | #include <linux/clk-provider.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/string.h> |
James Hogan | 1a3cd18 | 2013-01-15 10:28:05 +0000 | [diff] [blame] | 19 | #include <linux/log2.h> |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | * DOC: basic adjustable divider clock that cannot gate |
| 23 | * |
| 24 | * Traits of this clock: |
| 25 | * prepare - clk_prepare only ensures that parents are prepared |
| 26 | * enable - clk_enable only ensures that parents are enabled |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 27 | * rate - rate is adjustable. clk->rate = ceiling(parent->rate / divisor) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 28 | * parent - fixed parent. No clk_set_parent support |
| 29 | */ |
| 30 | |
| 31 | #define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw) |
| 32 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 33 | #define div_mask(width) ((1 << (width)) - 1) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 34 | |
Stephen Boyd | fab88ca | 2015-11-30 17:31:38 -0800 | [diff] [blame] | 35 | static unsigned int _get_table_maxdiv(const struct clk_div_table *table, |
| 36 | u8 width) |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 37 | { |
Stephen Boyd | fab88ca | 2015-11-30 17:31:38 -0800 | [diff] [blame] | 38 | unsigned int maxdiv = 0, mask = div_mask(width); |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 39 | const struct clk_div_table *clkt; |
| 40 | |
| 41 | for (clkt = table; clkt->div; clkt++) |
Stephen Boyd | fab88ca | 2015-11-30 17:31:38 -0800 | [diff] [blame] | 42 | if (clkt->div > maxdiv && clkt->val <= mask) |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 43 | maxdiv = clkt->div; |
| 44 | return maxdiv; |
| 45 | } |
| 46 | |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 47 | static unsigned int _get_table_mindiv(const struct clk_div_table *table) |
| 48 | { |
| 49 | unsigned int mindiv = UINT_MAX; |
| 50 | const struct clk_div_table *clkt; |
| 51 | |
| 52 | for (clkt = table; clkt->div; clkt++) |
| 53 | if (clkt->div < mindiv) |
| 54 | mindiv = clkt->div; |
| 55 | return mindiv; |
| 56 | } |
| 57 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 58 | static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width, |
| 59 | unsigned long flags) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 60 | { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 61 | if (flags & CLK_DIVIDER_ONE_BASED) |
| 62 | return div_mask(width); |
| 63 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
| 64 | return 1 << div_mask(width); |
| 65 | if (table) |
Stephen Boyd | fab88ca | 2015-11-30 17:31:38 -0800 | [diff] [blame] | 66 | return _get_table_maxdiv(table, width); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 67 | return div_mask(width) + 1; |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 68 | } |
| 69 | |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 70 | static unsigned int _get_table_div(const struct clk_div_table *table, |
| 71 | unsigned int val) |
| 72 | { |
| 73 | const struct clk_div_table *clkt; |
| 74 | |
| 75 | for (clkt = table; clkt->div; clkt++) |
| 76 | if (clkt->val == val) |
| 77 | return clkt->div; |
| 78 | return 0; |
| 79 | } |
| 80 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 81 | static unsigned int _get_div(const struct clk_div_table *table, |
Jim Quinlan | afe76c8f | 2015-05-15 15:45:47 -0400 | [diff] [blame] | 82 | unsigned int val, unsigned long flags, u8 width) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 83 | { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 84 | if (flags & CLK_DIVIDER_ONE_BASED) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 85 | return val; |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 86 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 87 | return 1 << val; |
Jim Quinlan | afe76c8f | 2015-05-15 15:45:47 -0400 | [diff] [blame] | 88 | if (flags & CLK_DIVIDER_MAX_AT_ZERO) |
| 89 | return val ? val : div_mask(width) + 1; |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 90 | if (table) |
| 91 | return _get_table_div(table, val); |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 92 | return val + 1; |
| 93 | } |
| 94 | |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 95 | static unsigned int _get_table_val(const struct clk_div_table *table, |
| 96 | unsigned int div) |
| 97 | { |
| 98 | const struct clk_div_table *clkt; |
| 99 | |
| 100 | for (clkt = table; clkt->div; clkt++) |
| 101 | if (clkt->div == div) |
| 102 | return clkt->val; |
| 103 | return 0; |
| 104 | } |
| 105 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 106 | static unsigned int _get_val(const struct clk_div_table *table, |
Jim Quinlan | afe76c8f | 2015-05-15 15:45:47 -0400 | [diff] [blame] | 107 | unsigned int div, unsigned long flags, u8 width) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 108 | { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 109 | if (flags & CLK_DIVIDER_ONE_BASED) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 110 | return div; |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 111 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 112 | return __ffs(div); |
Jim Quinlan | afe76c8f | 2015-05-15 15:45:47 -0400 | [diff] [blame] | 113 | if (flags & CLK_DIVIDER_MAX_AT_ZERO) |
| 114 | return (div == div_mask(width) + 1) ? 0 : div; |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 115 | if (table) |
| 116 | return _get_table_val(table, div); |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 117 | return div - 1; |
| 118 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 119 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 120 | unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate, |
| 121 | unsigned int val, |
| 122 | const struct clk_div_table *table, |
| 123 | unsigned long flags) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 124 | { |
Jim Quinlan | afe76c8f | 2015-05-15 15:45:47 -0400 | [diff] [blame] | 125 | struct clk_divider *divider = to_clk_divider(hw); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 126 | unsigned int div; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 127 | |
Jim Quinlan | afe76c8f | 2015-05-15 15:45:47 -0400 | [diff] [blame] | 128 | div = _get_div(table, val, flags, divider->width); |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 129 | if (!div) { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 130 | WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO), |
Soren Brinkmann | 056b2053 | 2013-04-02 15:36:56 -0700 | [diff] [blame] | 131 | "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n", |
Stephen Boyd | 2f508a9 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 132 | clk_hw_get_name(hw)); |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 133 | return parent_rate; |
| 134 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 135 | |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 136 | return DIV_ROUND_UP_ULL((u64)parent_rate, div); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 137 | } |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 138 | EXPORT_SYMBOL_GPL(divider_recalc_rate); |
| 139 | |
| 140 | static unsigned long clk_divider_recalc_rate(struct clk_hw *hw, |
| 141 | unsigned long parent_rate) |
| 142 | { |
| 143 | struct clk_divider *divider = to_clk_divider(hw); |
| 144 | unsigned int val; |
| 145 | |
| 146 | val = clk_readl(divider->reg) >> divider->shift; |
| 147 | val &= div_mask(divider->width); |
| 148 | |
| 149 | return divider_recalc_rate(hw, parent_rate, val, divider->table, |
| 150 | divider->flags); |
| 151 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 152 | |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 153 | static bool _is_valid_table_div(const struct clk_div_table *table, |
| 154 | unsigned int div) |
| 155 | { |
| 156 | const struct clk_div_table *clkt; |
| 157 | |
| 158 | for (clkt = table; clkt->div; clkt++) |
| 159 | if (clkt->div == div) |
| 160 | return true; |
| 161 | return false; |
| 162 | } |
| 163 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 164 | static bool _is_valid_div(const struct clk_div_table *table, unsigned int div, |
| 165 | unsigned long flags) |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 166 | { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 167 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
James Hogan | 1a3cd18 | 2013-01-15 10:28:05 +0000 | [diff] [blame] | 168 | return is_power_of_2(div); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 169 | if (table) |
| 170 | return _is_valid_table_div(table, div); |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 171 | return true; |
| 172 | } |
| 173 | |
Maxime COQUELIN | dd23c2c | 2014-01-29 17:24:06 +0100 | [diff] [blame] | 174 | static int _round_up_table(const struct clk_div_table *table, int div) |
| 175 | { |
| 176 | const struct clk_div_table *clkt; |
Maxime COQUELIN | fe52e75 | 2014-05-07 18:48:52 +0200 | [diff] [blame] | 177 | int up = INT_MAX; |
Maxime COQUELIN | dd23c2c | 2014-01-29 17:24:06 +0100 | [diff] [blame] | 178 | |
| 179 | for (clkt = table; clkt->div; clkt++) { |
| 180 | if (clkt->div == div) |
| 181 | return clkt->div; |
| 182 | else if (clkt->div < div) |
| 183 | continue; |
| 184 | |
| 185 | if ((clkt->div - div) < (up - div)) |
| 186 | up = clkt->div; |
| 187 | } |
| 188 | |
| 189 | return up; |
| 190 | } |
| 191 | |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 192 | static int _round_down_table(const struct clk_div_table *table, int div) |
| 193 | { |
| 194 | const struct clk_div_table *clkt; |
| 195 | int down = _get_table_mindiv(table); |
| 196 | |
| 197 | for (clkt = table; clkt->div; clkt++) { |
| 198 | if (clkt->div == div) |
| 199 | return clkt->div; |
| 200 | else if (clkt->div > div) |
| 201 | continue; |
| 202 | |
| 203 | if ((div - clkt->div) < (div - down)) |
| 204 | down = clkt->div; |
| 205 | } |
| 206 | |
| 207 | return down; |
| 208 | } |
| 209 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 210 | static int _div_round_up(const struct clk_div_table *table, |
| 211 | unsigned long parent_rate, unsigned long rate, |
| 212 | unsigned long flags) |
Maxime COQUELIN | dd23c2c | 2014-01-29 17:24:06 +0100 | [diff] [blame] | 213 | { |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 214 | int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); |
Maxime COQUELIN | dd23c2c | 2014-01-29 17:24:06 +0100 | [diff] [blame] | 215 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 216 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
Maxime COQUELIN | dd23c2c | 2014-01-29 17:24:06 +0100 | [diff] [blame] | 217 | div = __roundup_pow_of_two(div); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 218 | if (table) |
| 219 | div = _round_up_table(table, div); |
Maxime COQUELIN | dd23c2c | 2014-01-29 17:24:06 +0100 | [diff] [blame] | 220 | |
| 221 | return div; |
| 222 | } |
| 223 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 224 | static int _div_round_closest(const struct clk_div_table *table, |
| 225 | unsigned long parent_rate, unsigned long rate, |
| 226 | unsigned long flags) |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 227 | { |
Uwe Kleine-König | 9315514 | 2015-02-21 11:40:25 +0100 | [diff] [blame] | 228 | int up, down; |
Uwe Kleine-König | 26bac95 | 2015-02-21 11:40:24 +0100 | [diff] [blame] | 229 | unsigned long up_rate, down_rate; |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 230 | |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 231 | up = DIV_ROUND_UP_ULL((u64)parent_rate, rate); |
Uwe Kleine-König | 9315514 | 2015-02-21 11:40:25 +0100 | [diff] [blame] | 232 | down = parent_rate / rate; |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 233 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 234 | if (flags & CLK_DIVIDER_POWER_OF_TWO) { |
Uwe Kleine-König | 9315514 | 2015-02-21 11:40:25 +0100 | [diff] [blame] | 235 | up = __roundup_pow_of_two(up); |
| 236 | down = __rounddown_pow_of_two(down); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 237 | } else if (table) { |
Uwe Kleine-König | 9315514 | 2015-02-21 11:40:25 +0100 | [diff] [blame] | 238 | up = _round_up_table(table, up); |
| 239 | down = _round_down_table(table, down); |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 242 | up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up); |
| 243 | down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down); |
Uwe Kleine-König | 26bac95 | 2015-02-21 11:40:24 +0100 | [diff] [blame] | 244 | |
| 245 | return (rate - up_rate) <= (down_rate - rate) ? up : down; |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 248 | static int _div_round(const struct clk_div_table *table, |
| 249 | unsigned long parent_rate, unsigned long rate, |
| 250 | unsigned long flags) |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 251 | { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 252 | if (flags & CLK_DIVIDER_ROUND_CLOSEST) |
| 253 | return _div_round_closest(table, parent_rate, rate, flags); |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 254 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 255 | return _div_round_up(table, parent_rate, rate, flags); |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 258 | static bool _is_best_div(unsigned long rate, unsigned long now, |
| 259 | unsigned long best, 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) |
Maxime COQUELIN | 774b514 | 2014-01-29 17:24:07 +0100 | [diff] [blame] | 262 | return abs(rate - now) < abs(rate - best); |
| 263 | |
| 264 | return now <= rate && now > best; |
| 265 | } |
| 266 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 267 | static int _next_div(const struct clk_div_table *table, int div, |
| 268 | unsigned long flags) |
Maxime COQUELIN | 0e2de78 | 2014-01-29 17:24:08 +0100 | [diff] [blame] | 269 | { |
| 270 | div++; |
| 271 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 272 | if (flags & CLK_DIVIDER_POWER_OF_TWO) |
Maxime COQUELIN | 0e2de78 | 2014-01-29 17:24:08 +0100 | [diff] [blame] | 273 | return __roundup_pow_of_two(div); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 274 | if (table) |
| 275 | return _round_up_table(table, div); |
Maxime COQUELIN | 0e2de78 | 2014-01-29 17:24:08 +0100 | [diff] [blame] | 276 | |
| 277 | return div; |
| 278 | } |
| 279 | |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 280 | static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate, |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 281 | unsigned long *best_parent_rate, |
| 282 | const struct clk_div_table *table, u8 width, |
| 283 | unsigned long flags) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 284 | { |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 285 | int i, bestdiv = 0; |
| 286 | unsigned long parent_rate, best = 0, now, maxdiv; |
Shawn Guo | 081c902 | 2013-06-02 22:20:55 +0800 | [diff] [blame] | 287 | unsigned long parent_rate_saved = *best_parent_rate; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 288 | |
| 289 | if (!rate) |
| 290 | rate = 1; |
| 291 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 292 | maxdiv = _get_maxdiv(table, width, flags); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 293 | |
Stephen Boyd | 98d8a60 | 2015-06-29 16:56:30 -0700 | [diff] [blame] | 294 | if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) { |
Shawn Guo | 81536e0 | 2012-04-12 20:50:17 +0800 | [diff] [blame] | 295 | parent_rate = *best_parent_rate; |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 296 | bestdiv = _div_round(table, parent_rate, rate, flags); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 297 | bestdiv = bestdiv == 0 ? 1 : bestdiv; |
| 298 | bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv; |
| 299 | return bestdiv; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * The maximum divider we can use without overflowing |
| 304 | * unsigned long in rate * i below |
| 305 | */ |
| 306 | maxdiv = min(ULONG_MAX / rate, maxdiv); |
| 307 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 308 | for (i = 1; i <= maxdiv; i = _next_div(table, i, flags)) { |
| 309 | if (!_is_valid_div(table, i, flags)) |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 310 | continue; |
Shawn Guo | 081c902 | 2013-06-02 22:20:55 +0800 | [diff] [blame] | 311 | if (rate * i == parent_rate_saved) { |
| 312 | /* |
| 313 | * It's the most ideal case if the requested rate can be |
| 314 | * divided from parent clock without needing to change |
| 315 | * parent rate, so return the divider immediately. |
| 316 | */ |
| 317 | *best_parent_rate = parent_rate_saved; |
| 318 | return i; |
| 319 | } |
Stephen Boyd | 2f508a9 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 320 | parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw), |
Uwe Kleine-König | da32113 | 2015-02-21 11:40:23 +0100 | [diff] [blame] | 321 | rate * i); |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 322 | now = DIV_ROUND_UP_ULL((u64)parent_rate, i); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 323 | if (_is_best_div(rate, now, best, flags)) { |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 324 | bestdiv = i; |
| 325 | best = now; |
| 326 | *best_parent_rate = parent_rate; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | if (!bestdiv) { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 331 | bestdiv = _get_maxdiv(table, width, flags); |
Stephen Boyd | 2f508a9 | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 332 | *best_parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw), 1); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | return bestdiv; |
| 336 | } |
| 337 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 338 | long divider_round_rate(struct clk_hw *hw, unsigned long rate, |
| 339 | unsigned long *prate, const struct clk_div_table *table, |
| 340 | u8 width, unsigned long flags) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 341 | { |
| 342 | int div; |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 343 | |
| 344 | div = clk_divider_bestdiv(hw, rate, prate, table, width, flags); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 345 | |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 346 | return DIV_ROUND_UP_ULL((u64)*prate, div); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 347 | } |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 348 | EXPORT_SYMBOL_GPL(divider_round_rate); |
| 349 | |
| 350 | static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate, |
| 351 | unsigned long *prate) |
| 352 | { |
| 353 | struct clk_divider *divider = to_clk_divider(hw); |
| 354 | int bestdiv; |
| 355 | |
| 356 | /* if read only, just return current value */ |
| 357 | if (divider->flags & CLK_DIVIDER_READ_ONLY) { |
| 358 | bestdiv = readl(divider->reg) >> divider->shift; |
| 359 | bestdiv &= div_mask(divider->width); |
Jim Quinlan | afe76c8f | 2015-05-15 15:45:47 -0400 | [diff] [blame] | 360 | bestdiv = _get_div(divider->table, bestdiv, divider->flags, |
| 361 | divider->width); |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 362 | return DIV_ROUND_UP_ULL((u64)*prate, bestdiv); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | return divider_round_rate(hw, rate, prate, divider->table, |
| 366 | divider->width, divider->flags); |
| 367 | } |
| 368 | |
| 369 | int divider_get_val(unsigned long rate, unsigned long parent_rate, |
| 370 | const struct clk_div_table *table, u8 width, |
| 371 | unsigned long flags) |
| 372 | { |
| 373 | unsigned int div, value; |
| 374 | |
Brian Norris | 9556f9d | 2015-04-13 16:03:21 -0700 | [diff] [blame] | 375 | div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 376 | |
| 377 | if (!_is_valid_div(table, div, flags)) |
| 378 | return -EINVAL; |
| 379 | |
Jim Quinlan | afe76c8f | 2015-05-15 15:45:47 -0400 | [diff] [blame] | 380 | value = _get_val(table, div, flags, width); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 381 | |
| 382 | return min_t(unsigned int, value, div_mask(width)); |
| 383 | } |
| 384 | EXPORT_SYMBOL_GPL(divider_get_val); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 385 | |
Shawn Guo | 1c0035d | 2012-04-12 20:50:18 +0800 | [diff] [blame] | 386 | static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, |
| 387 | unsigned long parent_rate) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 388 | { |
| 389 | struct clk_divider *divider = to_clk_divider(hw); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 390 | unsigned int value; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 391 | unsigned long flags = 0; |
| 392 | u32 val; |
| 393 | |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 394 | value = divider_get_val(rate, parent_rate, divider->table, |
| 395 | divider->width, divider->flags); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 396 | |
| 397 | if (divider->lock) |
| 398 | spin_lock_irqsave(divider->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 399 | else |
| 400 | __acquire(divider->lock); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 401 | |
Haojian Zhuang | d57dfe7 | 2013-06-08 22:47:18 +0800 | [diff] [blame] | 402 | if (divider->flags & CLK_DIVIDER_HIWORD_MASK) { |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 403 | val = div_mask(divider->width) << (divider->shift + 16); |
Haojian Zhuang | d57dfe7 | 2013-06-08 22:47:18 +0800 | [diff] [blame] | 404 | } else { |
Gerhard Sittig | aa514ce | 2013-07-22 14:14:40 +0200 | [diff] [blame] | 405 | val = clk_readl(divider->reg); |
Stephen Boyd | bca9690 | 2015-01-19 18:05:29 -0800 | [diff] [blame] | 406 | val &= ~(div_mask(divider->width) << divider->shift); |
Haojian Zhuang | d57dfe7 | 2013-06-08 22:47:18 +0800 | [diff] [blame] | 407 | } |
Rajendra Nayak | 6d9252b | 2012-05-17 15:52:13 +0530 | [diff] [blame] | 408 | val |= value << divider->shift; |
Gerhard Sittig | aa514ce | 2013-07-22 14:14:40 +0200 | [diff] [blame] | 409 | clk_writel(val, divider->reg); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 410 | |
| 411 | if (divider->lock) |
| 412 | spin_unlock_irqrestore(divider->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 413 | else |
| 414 | __release(divider->lock); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 415 | |
| 416 | return 0; |
| 417 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 418 | |
Shawn Guo | 822c250 | 2012-03-27 15:23:22 +0800 | [diff] [blame] | 419 | const struct clk_ops clk_divider_ops = { |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 420 | .recalc_rate = clk_divider_recalc_rate, |
| 421 | .round_rate = clk_divider_round_rate, |
| 422 | .set_rate = clk_divider_set_rate, |
| 423 | }; |
| 424 | EXPORT_SYMBOL_GPL(clk_divider_ops); |
| 425 | |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 426 | static struct clk *_register_divider(struct device *dev, const char *name, |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 427 | const char *parent_name, unsigned long flags, |
| 428 | void __iomem *reg, u8 shift, u8 width, |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 429 | u8 clk_divider_flags, const struct clk_div_table *table, |
| 430 | spinlock_t *lock) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 431 | { |
| 432 | struct clk_divider *div; |
| 433 | struct clk *clk; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 434 | struct clk_init_data init; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 435 | |
Haojian Zhuang | d57dfe7 | 2013-06-08 22:47:18 +0800 | [diff] [blame] | 436 | if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) { |
| 437 | if (width + shift > 16) { |
| 438 | pr_warn("divider value exceeds LOWORD field\n"); |
| 439 | return ERR_PTR(-EINVAL); |
| 440 | } |
| 441 | } |
| 442 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 443 | /* allocate the divider */ |
Stephen Boyd | d122db7 | 2015-05-14 16:47:10 -0700 | [diff] [blame] | 444 | div = kzalloc(sizeof(*div), GFP_KERNEL); |
| 445 | if (!div) |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 446 | return ERR_PTR(-ENOMEM); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 447 | |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 448 | init.name = name; |
James Hogan | e6d5e7d | 2014-11-14 15:32:09 +0000 | [diff] [blame] | 449 | init.ops = &clk_divider_ops; |
Rajendra Nayak | f7d8caa | 2012-06-01 14:02:47 +0530 | [diff] [blame] | 450 | init.flags = flags | CLK_IS_BASIC; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 451 | init.parent_names = (parent_name ? &parent_name: NULL); |
| 452 | init.num_parents = (parent_name ? 1 : 0); |
| 453 | |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 454 | /* struct clk_divider assignments */ |
| 455 | div->reg = reg; |
| 456 | div->shift = shift; |
| 457 | div->width = width; |
| 458 | div->flags = clk_divider_flags; |
| 459 | div->lock = lock; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 460 | div->hw.init = &init; |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 461 | div->table = table; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 462 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 463 | /* register the clock */ |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 464 | clk = clk_register(dev, &div->hw); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 465 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 466 | if (IS_ERR(clk)) |
| 467 | kfree(div); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 468 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 469 | return clk; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 470 | } |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 471 | |
| 472 | /** |
| 473 | * clk_register_divider - register a divider clock with the clock framework |
| 474 | * @dev: device registering this clock |
| 475 | * @name: name of this clock |
| 476 | * @parent_name: name of clock's parent |
| 477 | * @flags: framework-specific flags |
| 478 | * @reg: register address to adjust divider |
| 479 | * @shift: number of bits to shift the bitfield |
| 480 | * @width: width of the bitfield |
| 481 | * @clk_divider_flags: divider-specific flags for this clock |
| 482 | * @lock: shared register lock for this clock |
| 483 | */ |
| 484 | struct clk *clk_register_divider(struct device *dev, const char *name, |
| 485 | const char *parent_name, unsigned long flags, |
| 486 | void __iomem *reg, u8 shift, u8 width, |
| 487 | u8 clk_divider_flags, spinlock_t *lock) |
| 488 | { |
| 489 | return _register_divider(dev, name, parent_name, flags, reg, shift, |
| 490 | width, clk_divider_flags, NULL, lock); |
| 491 | } |
Fabio Estevam | 4c5eeea | 2013-08-02 13:14:07 -0300 | [diff] [blame] | 492 | EXPORT_SYMBOL_GPL(clk_register_divider); |
Rajendra Nayak | 357c3f0 | 2012-06-29 19:06:32 +0530 | [diff] [blame] | 493 | |
| 494 | /** |
| 495 | * clk_register_divider_table - register a table based divider clock with |
| 496 | * the clock framework |
| 497 | * @dev: device registering this clock |
| 498 | * @name: name of this clock |
| 499 | * @parent_name: name of clock's parent |
| 500 | * @flags: framework-specific flags |
| 501 | * @reg: register address to adjust divider |
| 502 | * @shift: number of bits to shift the bitfield |
| 503 | * @width: width of the bitfield |
| 504 | * @clk_divider_flags: divider-specific flags for this clock |
| 505 | * @table: array of divider/value pairs ending with a div set to 0 |
| 506 | * @lock: shared register lock for this clock |
| 507 | */ |
| 508 | struct clk *clk_register_divider_table(struct device *dev, const char *name, |
| 509 | const char *parent_name, unsigned long flags, |
| 510 | void __iomem *reg, u8 shift, u8 width, |
| 511 | u8 clk_divider_flags, const struct clk_div_table *table, |
| 512 | spinlock_t *lock) |
| 513 | { |
| 514 | return _register_divider(dev, name, parent_name, flags, reg, shift, |
| 515 | width, clk_divider_flags, table, lock); |
| 516 | } |
Fabio Estevam | 4c5eeea | 2013-08-02 13:14:07 -0300 | [diff] [blame] | 517 | EXPORT_SYMBOL_GPL(clk_register_divider_table); |
Krzysztof Kozlowski | 4e3c021 | 2015-01-05 10:52:40 +0100 | [diff] [blame] | 518 | |
| 519 | void clk_unregister_divider(struct clk *clk) |
| 520 | { |
| 521 | struct clk_divider *div; |
| 522 | struct clk_hw *hw; |
| 523 | |
| 524 | hw = __clk_get_hw(clk); |
| 525 | if (!hw) |
| 526 | return; |
| 527 | |
| 528 | div = to_clk_divider(hw); |
| 529 | |
| 530 | clk_unregister(clk); |
| 531 | kfree(div); |
| 532 | } |
| 533 | EXPORT_SYMBOL_GPL(clk_unregister_divider); |