1e1bd55e5SStephen Boyd // SPDX-License-Identifier: GPL-2.0 29d9f78edSMike Turquette /* 39d9f78edSMike Turquette * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> 49d9f78edSMike Turquette * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org> 59d9f78edSMike Turquette * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org> 69d9f78edSMike Turquette * 79d9f78edSMike Turquette * Adjustable divider clock implementation 89d9f78edSMike Turquette */ 99d9f78edSMike Turquette 109d9f78edSMike Turquette #include <linux/clk-provider.h> 119d9f78edSMike Turquette #include <linux/module.h> 129d9f78edSMike Turquette #include <linux/slab.h> 139d9f78edSMike Turquette #include <linux/io.h> 149d9f78edSMike Turquette #include <linux/err.h> 159d9f78edSMike Turquette #include <linux/string.h> 161a3cd184SJames Hogan #include <linux/log2.h> 179d9f78edSMike Turquette 189d9f78edSMike Turquette /* 199d9f78edSMike Turquette * DOC: basic adjustable divider clock that cannot gate 209d9f78edSMike Turquette * 219d9f78edSMike Turquette * Traits of this clock: 229d9f78edSMike Turquette * prepare - clk_prepare only ensures that parents are prepared 239d9f78edSMike Turquette * enable - clk_enable only ensures that parents are enabled 249556f9daSBrian Norris * rate - rate is adjustable. clk->rate = ceiling(parent->rate / divisor) 259d9f78edSMike Turquette * parent - fixed parent. No clk_set_parent support 269d9f78edSMike Turquette */ 279d9f78edSMike Turquette 28434d69faSJonas Gorski static inline u32 clk_div_readl(struct clk_divider *divider) 29434d69faSJonas Gorski { 30434d69faSJonas Gorski if (divider->flags & CLK_DIVIDER_BIG_ENDIAN) 31434d69faSJonas Gorski return ioread32be(divider->reg); 32434d69faSJonas Gorski 33*5834fd75SJonas Gorski return readl(divider->reg); 34434d69faSJonas Gorski } 35434d69faSJonas Gorski 36434d69faSJonas Gorski static inline void clk_div_writel(struct clk_divider *divider, u32 val) 37434d69faSJonas Gorski { 38434d69faSJonas Gorski if (divider->flags & CLK_DIVIDER_BIG_ENDIAN) 39434d69faSJonas Gorski iowrite32be(val, divider->reg); 40434d69faSJonas Gorski else 41*5834fd75SJonas Gorski writel(val, divider->reg); 42434d69faSJonas Gorski } 43434d69faSJonas Gorski 44fab88ca7SStephen Boyd static unsigned int _get_table_maxdiv(const struct clk_div_table *table, 45fab88ca7SStephen Boyd u8 width) 46357c3f0aSRajendra Nayak { 47e6d3cc7bSJerome Brunet unsigned int maxdiv = 0, mask = clk_div_mask(width); 48357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 49357c3f0aSRajendra Nayak 50357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 51fab88ca7SStephen Boyd if (clkt->div > maxdiv && clkt->val <= mask) 52357c3f0aSRajendra Nayak maxdiv = clkt->div; 53357c3f0aSRajendra Nayak return maxdiv; 54357c3f0aSRajendra Nayak } 55357c3f0aSRajendra Nayak 56774b5143SMaxime COQUELIN static unsigned int _get_table_mindiv(const struct clk_div_table *table) 57774b5143SMaxime COQUELIN { 58774b5143SMaxime COQUELIN unsigned int mindiv = UINT_MAX; 59774b5143SMaxime COQUELIN const struct clk_div_table *clkt; 60774b5143SMaxime COQUELIN 61774b5143SMaxime COQUELIN for (clkt = table; clkt->div; clkt++) 62774b5143SMaxime COQUELIN if (clkt->div < mindiv) 63774b5143SMaxime COQUELIN mindiv = clkt->div; 64774b5143SMaxime COQUELIN return mindiv; 65774b5143SMaxime COQUELIN } 66774b5143SMaxime COQUELIN 67bca9690bSStephen Boyd static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width, 68bca9690bSStephen Boyd unsigned long flags) 696d9252bdSRajendra Nayak { 70bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ONE_BASED) 71e6d3cc7bSJerome Brunet return clk_div_mask(width); 72bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 73e6d3cc7bSJerome Brunet return 1 << clk_div_mask(width); 74bca9690bSStephen Boyd if (table) 75fab88ca7SStephen Boyd return _get_table_maxdiv(table, width); 76e6d3cc7bSJerome Brunet return clk_div_mask(width) + 1; 776d9252bdSRajendra Nayak } 786d9252bdSRajendra Nayak 79357c3f0aSRajendra Nayak static unsigned int _get_table_div(const struct clk_div_table *table, 80357c3f0aSRajendra Nayak unsigned int val) 81357c3f0aSRajendra Nayak { 82357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 83357c3f0aSRajendra Nayak 84357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 85357c3f0aSRajendra Nayak if (clkt->val == val) 86357c3f0aSRajendra Nayak return clkt->div; 87357c3f0aSRajendra Nayak return 0; 88357c3f0aSRajendra Nayak } 89357c3f0aSRajendra Nayak 90bca9690bSStephen Boyd static unsigned int _get_div(const struct clk_div_table *table, 91afe76c8fSJim Quinlan unsigned int val, unsigned long flags, u8 width) 926d9252bdSRajendra Nayak { 93bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ONE_BASED) 946d9252bdSRajendra Nayak return val; 95bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 966d9252bdSRajendra Nayak return 1 << val; 97afe76c8fSJim Quinlan if (flags & CLK_DIVIDER_MAX_AT_ZERO) 98e6d3cc7bSJerome Brunet return val ? val : clk_div_mask(width) + 1; 99bca9690bSStephen Boyd if (table) 100bca9690bSStephen Boyd return _get_table_div(table, val); 1016d9252bdSRajendra Nayak return val + 1; 1026d9252bdSRajendra Nayak } 1036d9252bdSRajendra Nayak 104357c3f0aSRajendra Nayak static unsigned int _get_table_val(const struct clk_div_table *table, 105357c3f0aSRajendra Nayak unsigned int div) 106357c3f0aSRajendra Nayak { 107357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 108357c3f0aSRajendra Nayak 109357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 110357c3f0aSRajendra Nayak if (clkt->div == div) 111357c3f0aSRajendra Nayak return clkt->val; 112357c3f0aSRajendra Nayak return 0; 113357c3f0aSRajendra Nayak } 114357c3f0aSRajendra Nayak 115bca9690bSStephen Boyd static unsigned int _get_val(const struct clk_div_table *table, 116afe76c8fSJim Quinlan unsigned int div, unsigned long flags, u8 width) 1176d9252bdSRajendra Nayak { 118bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ONE_BASED) 1196d9252bdSRajendra Nayak return div; 120bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 1216d9252bdSRajendra Nayak return __ffs(div); 122afe76c8fSJim Quinlan if (flags & CLK_DIVIDER_MAX_AT_ZERO) 123e6d3cc7bSJerome Brunet return (div == clk_div_mask(width) + 1) ? 0 : div; 124bca9690bSStephen Boyd if (table) 125bca9690bSStephen Boyd return _get_table_val(table, div); 1266d9252bdSRajendra Nayak return div - 1; 1276d9252bdSRajendra Nayak } 1289d9f78edSMike Turquette 129bca9690bSStephen Boyd unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate, 130bca9690bSStephen Boyd unsigned int val, 131bca9690bSStephen Boyd const struct clk_div_table *table, 13212a26c29SJerome Brunet unsigned long flags, unsigned long width) 1339d9f78edSMike Turquette { 134bca9690bSStephen Boyd unsigned int div; 1359d9f78edSMike Turquette 13612a26c29SJerome Brunet div = _get_div(table, val, flags, width); 1376d9252bdSRajendra Nayak if (!div) { 138bca9690bSStephen Boyd WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO), 139056b2053SSoren Brinkmann "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n", 1402f508a95SStephen Boyd clk_hw_get_name(hw)); 1416d9252bdSRajendra Nayak return parent_rate; 1426d9252bdSRajendra Nayak } 1439d9f78edSMike Turquette 1449556f9daSBrian Norris return DIV_ROUND_UP_ULL((u64)parent_rate, div); 1459d9f78edSMike Turquette } 146bca9690bSStephen Boyd EXPORT_SYMBOL_GPL(divider_recalc_rate); 147bca9690bSStephen Boyd 148bca9690bSStephen Boyd static unsigned long clk_divider_recalc_rate(struct clk_hw *hw, 149bca9690bSStephen Boyd unsigned long parent_rate) 150bca9690bSStephen Boyd { 151bca9690bSStephen Boyd struct clk_divider *divider = to_clk_divider(hw); 152bca9690bSStephen Boyd unsigned int val; 153bca9690bSStephen Boyd 154434d69faSJonas Gorski val = clk_div_readl(divider) >> divider->shift; 155e6d3cc7bSJerome Brunet val &= clk_div_mask(divider->width); 156bca9690bSStephen Boyd 157bca9690bSStephen Boyd return divider_recalc_rate(hw, parent_rate, val, divider->table, 15812a26c29SJerome Brunet divider->flags, divider->width); 159bca9690bSStephen Boyd } 1609d9f78edSMike Turquette 161357c3f0aSRajendra Nayak static bool _is_valid_table_div(const struct clk_div_table *table, 162357c3f0aSRajendra Nayak unsigned int div) 163357c3f0aSRajendra Nayak { 164357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 165357c3f0aSRajendra Nayak 166357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 167357c3f0aSRajendra Nayak if (clkt->div == div) 168357c3f0aSRajendra Nayak return true; 169357c3f0aSRajendra Nayak return false; 170357c3f0aSRajendra Nayak } 171357c3f0aSRajendra Nayak 172bca9690bSStephen Boyd static bool _is_valid_div(const struct clk_div_table *table, unsigned int div, 173bca9690bSStephen Boyd unsigned long flags) 174357c3f0aSRajendra Nayak { 175bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 1761a3cd184SJames Hogan return is_power_of_2(div); 177bca9690bSStephen Boyd if (table) 178bca9690bSStephen Boyd return _is_valid_table_div(table, div); 179357c3f0aSRajendra Nayak return true; 180357c3f0aSRajendra Nayak } 181357c3f0aSRajendra Nayak 182dd23c2cdSMaxime COQUELIN static int _round_up_table(const struct clk_div_table *table, int div) 183dd23c2cdSMaxime COQUELIN { 184dd23c2cdSMaxime COQUELIN const struct clk_div_table *clkt; 185fe52e750SMaxime COQUELIN int up = INT_MAX; 186dd23c2cdSMaxime COQUELIN 187dd23c2cdSMaxime COQUELIN for (clkt = table; clkt->div; clkt++) { 188dd23c2cdSMaxime COQUELIN if (clkt->div == div) 189dd23c2cdSMaxime COQUELIN return clkt->div; 190dd23c2cdSMaxime COQUELIN else if (clkt->div < div) 191dd23c2cdSMaxime COQUELIN continue; 192dd23c2cdSMaxime COQUELIN 193dd23c2cdSMaxime COQUELIN if ((clkt->div - div) < (up - div)) 194dd23c2cdSMaxime COQUELIN up = clkt->div; 195dd23c2cdSMaxime COQUELIN } 196dd23c2cdSMaxime COQUELIN 197dd23c2cdSMaxime COQUELIN return up; 198dd23c2cdSMaxime COQUELIN } 199dd23c2cdSMaxime COQUELIN 200774b5143SMaxime COQUELIN static int _round_down_table(const struct clk_div_table *table, int div) 201774b5143SMaxime COQUELIN { 202774b5143SMaxime COQUELIN const struct clk_div_table *clkt; 203774b5143SMaxime COQUELIN int down = _get_table_mindiv(table); 204774b5143SMaxime COQUELIN 205774b5143SMaxime COQUELIN for (clkt = table; clkt->div; clkt++) { 206774b5143SMaxime COQUELIN if (clkt->div == div) 207774b5143SMaxime COQUELIN return clkt->div; 208774b5143SMaxime COQUELIN else if (clkt->div > div) 209774b5143SMaxime COQUELIN continue; 210774b5143SMaxime COQUELIN 211774b5143SMaxime COQUELIN if ((div - clkt->div) < (div - down)) 212774b5143SMaxime COQUELIN down = clkt->div; 213774b5143SMaxime COQUELIN } 214774b5143SMaxime COQUELIN 215774b5143SMaxime COQUELIN return down; 216774b5143SMaxime COQUELIN } 217774b5143SMaxime COQUELIN 218bca9690bSStephen Boyd static int _div_round_up(const struct clk_div_table *table, 219bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 220bca9690bSStephen Boyd unsigned long flags) 221dd23c2cdSMaxime COQUELIN { 2229556f9daSBrian Norris int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 223dd23c2cdSMaxime COQUELIN 224bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 225dd23c2cdSMaxime COQUELIN div = __roundup_pow_of_two(div); 226bca9690bSStephen Boyd if (table) 227bca9690bSStephen Boyd div = _round_up_table(table, div); 228dd23c2cdSMaxime COQUELIN 229dd23c2cdSMaxime COQUELIN return div; 230dd23c2cdSMaxime COQUELIN } 231dd23c2cdSMaxime COQUELIN 232bca9690bSStephen Boyd static int _div_round_closest(const struct clk_div_table *table, 233bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 234bca9690bSStephen Boyd unsigned long flags) 235774b5143SMaxime COQUELIN { 23693155142SUwe Kleine-König int up, down; 23726bac95aSUwe Kleine-König unsigned long up_rate, down_rate; 238774b5143SMaxime COQUELIN 2399556f9daSBrian Norris up = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 24093155142SUwe Kleine-König down = parent_rate / rate; 241774b5143SMaxime COQUELIN 242bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) { 24393155142SUwe Kleine-König up = __roundup_pow_of_two(up); 24493155142SUwe Kleine-König down = __rounddown_pow_of_two(down); 245bca9690bSStephen Boyd } else if (table) { 24693155142SUwe Kleine-König up = _round_up_table(table, up); 24793155142SUwe Kleine-König down = _round_down_table(table, down); 248774b5143SMaxime COQUELIN } 249774b5143SMaxime COQUELIN 2509556f9daSBrian Norris up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up); 2519556f9daSBrian Norris down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down); 25226bac95aSUwe Kleine-König 25326bac95aSUwe Kleine-König return (rate - up_rate) <= (down_rate - rate) ? up : down; 254774b5143SMaxime COQUELIN } 255774b5143SMaxime COQUELIN 256bca9690bSStephen Boyd static int _div_round(const struct clk_div_table *table, 257bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 258bca9690bSStephen Boyd unsigned long flags) 259774b5143SMaxime COQUELIN { 260bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ROUND_CLOSEST) 261bca9690bSStephen Boyd return _div_round_closest(table, parent_rate, rate, flags); 262774b5143SMaxime COQUELIN 263bca9690bSStephen Boyd return _div_round_up(table, parent_rate, rate, flags); 264774b5143SMaxime COQUELIN } 265774b5143SMaxime COQUELIN 266bca9690bSStephen Boyd static bool _is_best_div(unsigned long rate, unsigned long now, 267bca9690bSStephen Boyd unsigned long best, unsigned long flags) 268774b5143SMaxime COQUELIN { 269bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ROUND_CLOSEST) 270774b5143SMaxime COQUELIN return abs(rate - now) < abs(rate - best); 271774b5143SMaxime COQUELIN 272774b5143SMaxime COQUELIN return now <= rate && now > best; 273774b5143SMaxime COQUELIN } 274774b5143SMaxime COQUELIN 275bca9690bSStephen Boyd static int _next_div(const struct clk_div_table *table, int div, 276bca9690bSStephen Boyd unsigned long flags) 2770e2de78eSMaxime COQUELIN { 2780e2de78eSMaxime COQUELIN div++; 2790e2de78eSMaxime COQUELIN 280bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 2810e2de78eSMaxime COQUELIN return __roundup_pow_of_two(div); 282bca9690bSStephen Boyd if (table) 283bca9690bSStephen Boyd return _round_up_table(table, div); 2840e2de78eSMaxime COQUELIN 2850e2de78eSMaxime COQUELIN return div; 2860e2de78eSMaxime COQUELIN } 2870e2de78eSMaxime COQUELIN 28822833a91SMaxime Ripard static int clk_divider_bestdiv(struct clk_hw *hw, struct clk_hw *parent, 28922833a91SMaxime Ripard unsigned long rate, 290bca9690bSStephen Boyd unsigned long *best_parent_rate, 291bca9690bSStephen Boyd const struct clk_div_table *table, u8 width, 292bca9690bSStephen Boyd unsigned long flags) 2939d9f78edSMike Turquette { 2949d9f78edSMike Turquette int i, bestdiv = 0; 2959d9f78edSMike Turquette unsigned long parent_rate, best = 0, now, maxdiv; 296081c9025SShawn Guo unsigned long parent_rate_saved = *best_parent_rate; 2979d9f78edSMike Turquette 2989d9f78edSMike Turquette if (!rate) 2999d9f78edSMike Turquette rate = 1; 3009d9f78edSMike Turquette 301bca9690bSStephen Boyd maxdiv = _get_maxdiv(table, width, flags); 3029d9f78edSMike Turquette 30398d8a60eSStephen Boyd if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) { 30481536e07SShawn Guo parent_rate = *best_parent_rate; 305bca9690bSStephen Boyd bestdiv = _div_round(table, parent_rate, rate, flags); 3069d9f78edSMike Turquette bestdiv = bestdiv == 0 ? 1 : bestdiv; 3079d9f78edSMike Turquette bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv; 3089d9f78edSMike Turquette return bestdiv; 3099d9f78edSMike Turquette } 3109d9f78edSMike Turquette 3119d9f78edSMike Turquette /* 3129d9f78edSMike Turquette * The maximum divider we can use without overflowing 3139d9f78edSMike Turquette * unsigned long in rate * i below 3149d9f78edSMike Turquette */ 3159d9f78edSMike Turquette maxdiv = min(ULONG_MAX / rate, maxdiv); 3169d9f78edSMike Turquette 317653d1452SMasahiro Yamada for (i = _next_div(table, 0, flags); i <= maxdiv; 318653d1452SMasahiro Yamada i = _next_div(table, i, flags)) { 319081c9025SShawn Guo if (rate * i == parent_rate_saved) { 320081c9025SShawn Guo /* 321081c9025SShawn Guo * It's the most ideal case if the requested rate can be 322081c9025SShawn Guo * divided from parent clock without needing to change 323081c9025SShawn Guo * parent rate, so return the divider immediately. 324081c9025SShawn Guo */ 325081c9025SShawn Guo *best_parent_rate = parent_rate_saved; 326081c9025SShawn Guo return i; 327081c9025SShawn Guo } 32822833a91SMaxime Ripard parent_rate = clk_hw_round_rate(parent, rate * i); 3299556f9daSBrian Norris now = DIV_ROUND_UP_ULL((u64)parent_rate, i); 330bca9690bSStephen Boyd if (_is_best_div(rate, now, best, flags)) { 3319d9f78edSMike Turquette bestdiv = i; 3329d9f78edSMike Turquette best = now; 3339d9f78edSMike Turquette *best_parent_rate = parent_rate; 3349d9f78edSMike Turquette } 3359d9f78edSMike Turquette } 3369d9f78edSMike Turquette 3379d9f78edSMike Turquette if (!bestdiv) { 338bca9690bSStephen Boyd bestdiv = _get_maxdiv(table, width, flags); 33922833a91SMaxime Ripard *best_parent_rate = clk_hw_round_rate(parent, 1); 3409d9f78edSMike Turquette } 3419d9f78edSMike Turquette 3429d9f78edSMike Turquette return bestdiv; 3439d9f78edSMike Turquette } 3449d9f78edSMike Turquette 34522833a91SMaxime Ripard long divider_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent, 34622833a91SMaxime Ripard unsigned long rate, unsigned long *prate, 34722833a91SMaxime Ripard const struct clk_div_table *table, 348bca9690bSStephen Boyd u8 width, unsigned long flags) 3499d9f78edSMike Turquette { 3509d9f78edSMike Turquette int div; 351bca9690bSStephen Boyd 35222833a91SMaxime Ripard div = clk_divider_bestdiv(hw, parent, rate, prate, table, width, flags); 3539d9f78edSMike Turquette 3549556f9daSBrian Norris return DIV_ROUND_UP_ULL((u64)*prate, div); 3559d9f78edSMike Turquette } 35622833a91SMaxime Ripard EXPORT_SYMBOL_GPL(divider_round_rate_parent); 357bca9690bSStephen Boyd 358b15ee490SJerome Brunet long divider_ro_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent, 359b15ee490SJerome Brunet unsigned long rate, unsigned long *prate, 360b15ee490SJerome Brunet const struct clk_div_table *table, u8 width, 361b15ee490SJerome Brunet unsigned long flags, unsigned int val) 362b15ee490SJerome Brunet { 363b15ee490SJerome Brunet int div; 364b15ee490SJerome Brunet 365b15ee490SJerome Brunet div = _get_div(table, val, flags, width); 366b15ee490SJerome Brunet 367b15ee490SJerome Brunet /* Even a read-only clock can propagate a rate change */ 368b15ee490SJerome Brunet if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) { 369b15ee490SJerome Brunet if (!parent) 370b15ee490SJerome Brunet return -EINVAL; 371b15ee490SJerome Brunet 372b15ee490SJerome Brunet *prate = clk_hw_round_rate(parent, rate * div); 373b15ee490SJerome Brunet } 374b15ee490SJerome Brunet 375b15ee490SJerome Brunet return DIV_ROUND_UP_ULL((u64)*prate, div); 376b15ee490SJerome Brunet } 377b15ee490SJerome Brunet EXPORT_SYMBOL_GPL(divider_ro_round_rate_parent); 378b15ee490SJerome Brunet 379b15ee490SJerome Brunet 380bca9690bSStephen Boyd static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate, 381bca9690bSStephen Boyd unsigned long *prate) 382bca9690bSStephen Boyd { 383bca9690bSStephen Boyd struct clk_divider *divider = to_clk_divider(hw); 384bca9690bSStephen Boyd 385bca9690bSStephen Boyd /* if read only, just return current value */ 386bca9690bSStephen Boyd if (divider->flags & CLK_DIVIDER_READ_ONLY) { 387b15ee490SJerome Brunet u32 val; 388b15ee490SJerome Brunet 389434d69faSJonas Gorski val = clk_div_readl(divider) >> divider->shift; 390b15ee490SJerome Brunet val &= clk_div_mask(divider->width); 391b15ee490SJerome Brunet 392b15ee490SJerome Brunet return divider_ro_round_rate(hw, rate, prate, divider->table, 393b15ee490SJerome Brunet divider->width, divider->flags, 394b15ee490SJerome Brunet val); 395bca9690bSStephen Boyd } 396bca9690bSStephen Boyd 397bca9690bSStephen Boyd return divider_round_rate(hw, rate, prate, divider->table, 398bca9690bSStephen Boyd divider->width, divider->flags); 399bca9690bSStephen Boyd } 400bca9690bSStephen Boyd 401bca9690bSStephen Boyd int divider_get_val(unsigned long rate, unsigned long parent_rate, 402bca9690bSStephen Boyd const struct clk_div_table *table, u8 width, 403bca9690bSStephen Boyd unsigned long flags) 404bca9690bSStephen Boyd { 405bca9690bSStephen Boyd unsigned int div, value; 406bca9690bSStephen Boyd 4079556f9daSBrian Norris div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 408bca9690bSStephen Boyd 409bca9690bSStephen Boyd if (!_is_valid_div(table, div, flags)) 410bca9690bSStephen Boyd return -EINVAL; 411bca9690bSStephen Boyd 412afe76c8fSJim Quinlan value = _get_val(table, div, flags, width); 413bca9690bSStephen Boyd 414e6d3cc7bSJerome Brunet return min_t(unsigned int, value, clk_div_mask(width)); 415bca9690bSStephen Boyd } 416bca9690bSStephen Boyd EXPORT_SYMBOL_GPL(divider_get_val); 4179d9f78edSMike Turquette 4181c0035d7SShawn Guo static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, 4191c0035d7SShawn Guo unsigned long parent_rate) 4209d9f78edSMike Turquette { 4219d9f78edSMike Turquette struct clk_divider *divider = to_clk_divider(hw); 4222316a7a3SAlex Frid int value; 4239d9f78edSMike Turquette unsigned long flags = 0; 4249d9f78edSMike Turquette u32 val; 4259d9f78edSMike Turquette 426bca9690bSStephen Boyd value = divider_get_val(rate, parent_rate, divider->table, 427bca9690bSStephen Boyd divider->width, divider->flags); 4282316a7a3SAlex Frid if (value < 0) 4292316a7a3SAlex Frid return value; 4309d9f78edSMike Turquette 4319d9f78edSMike Turquette if (divider->lock) 4329d9f78edSMike Turquette spin_lock_irqsave(divider->lock, flags); 433661e2180SStephen Boyd else 434661e2180SStephen Boyd __acquire(divider->lock); 4359d9f78edSMike Turquette 436d57dfe75SHaojian Zhuang if (divider->flags & CLK_DIVIDER_HIWORD_MASK) { 437e6d3cc7bSJerome Brunet val = clk_div_mask(divider->width) << (divider->shift + 16); 438d57dfe75SHaojian Zhuang } else { 439434d69faSJonas Gorski val = clk_div_readl(divider); 440e6d3cc7bSJerome Brunet val &= ~(clk_div_mask(divider->width) << divider->shift); 441d57dfe75SHaojian Zhuang } 4422316a7a3SAlex Frid val |= (u32)value << divider->shift; 443434d69faSJonas Gorski clk_div_writel(divider, val); 4449d9f78edSMike Turquette 4459d9f78edSMike Turquette if (divider->lock) 4469d9f78edSMike Turquette spin_unlock_irqrestore(divider->lock, flags); 447661e2180SStephen Boyd else 448661e2180SStephen Boyd __release(divider->lock); 4499d9f78edSMike Turquette 4509d9f78edSMike Turquette return 0; 4519d9f78edSMike Turquette } 4529d9f78edSMike Turquette 453822c250eSShawn Guo const struct clk_ops clk_divider_ops = { 4549d9f78edSMike Turquette .recalc_rate = clk_divider_recalc_rate, 4559d9f78edSMike Turquette .round_rate = clk_divider_round_rate, 4569d9f78edSMike Turquette .set_rate = clk_divider_set_rate, 4579d9f78edSMike Turquette }; 4589d9f78edSMike Turquette EXPORT_SYMBOL_GPL(clk_divider_ops); 4599d9f78edSMike Turquette 46050359819SHeiko Stuebner const struct clk_ops clk_divider_ro_ops = { 46150359819SHeiko Stuebner .recalc_rate = clk_divider_recalc_rate, 46250359819SHeiko Stuebner .round_rate = clk_divider_round_rate, 46350359819SHeiko Stuebner }; 46450359819SHeiko Stuebner EXPORT_SYMBOL_GPL(clk_divider_ro_ops); 46550359819SHeiko Stuebner 466eb7d264fSStephen Boyd static struct clk_hw *_register_divider(struct device *dev, const char *name, 4679d9f78edSMike Turquette const char *parent_name, unsigned long flags, 4689d9f78edSMike Turquette void __iomem *reg, u8 shift, u8 width, 469357c3f0aSRajendra Nayak u8 clk_divider_flags, const struct clk_div_table *table, 470357c3f0aSRajendra Nayak spinlock_t *lock) 4719d9f78edSMike Turquette { 4729d9f78edSMike Turquette struct clk_divider *div; 473eb7d264fSStephen Boyd struct clk_hw *hw; 4740197b3eaSSaravana Kannan struct clk_init_data init; 475eb7d264fSStephen Boyd int ret; 4769d9f78edSMike Turquette 477d57dfe75SHaojian Zhuang if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) { 478d57dfe75SHaojian Zhuang if (width + shift > 16) { 479d57dfe75SHaojian Zhuang pr_warn("divider value exceeds LOWORD field\n"); 480d57dfe75SHaojian Zhuang return ERR_PTR(-EINVAL); 481d57dfe75SHaojian Zhuang } 482d57dfe75SHaojian Zhuang } 483d57dfe75SHaojian Zhuang 48427d54591SMike Turquette /* allocate the divider */ 485d122db7eSStephen Boyd div = kzalloc(sizeof(*div), GFP_KERNEL); 486d122db7eSStephen Boyd if (!div) 48727d54591SMike Turquette return ERR_PTR(-ENOMEM); 4889d9f78edSMike Turquette 4890197b3eaSSaravana Kannan init.name = name; 49050359819SHeiko Stuebner if (clk_divider_flags & CLK_DIVIDER_READ_ONLY) 49150359819SHeiko Stuebner init.ops = &clk_divider_ro_ops; 49250359819SHeiko Stuebner else 4930197b3eaSSaravana Kannan init.ops = &clk_divider_ops; 494f7d8caadSRajendra Nayak init.flags = flags | CLK_IS_BASIC; 4950197b3eaSSaravana Kannan init.parent_names = (parent_name ? &parent_name: NULL); 4960197b3eaSSaravana Kannan init.num_parents = (parent_name ? 1 : 0); 4970197b3eaSSaravana Kannan 4989d9f78edSMike Turquette /* struct clk_divider assignments */ 4999d9f78edSMike Turquette div->reg = reg; 5009d9f78edSMike Turquette div->shift = shift; 5019d9f78edSMike Turquette div->width = width; 5029d9f78edSMike Turquette div->flags = clk_divider_flags; 5039d9f78edSMike Turquette div->lock = lock; 5040197b3eaSSaravana Kannan div->hw.init = &init; 505357c3f0aSRajendra Nayak div->table = table; 5069d9f78edSMike Turquette 50727d54591SMike Turquette /* register the clock */ 508eb7d264fSStephen Boyd hw = &div->hw; 509eb7d264fSStephen Boyd ret = clk_hw_register(dev, hw); 510eb7d264fSStephen Boyd if (ret) { 5119d9f78edSMike Turquette kfree(div); 512eb7d264fSStephen Boyd hw = ERR_PTR(ret); 513eb7d264fSStephen Boyd } 5149d9f78edSMike Turquette 515eb7d264fSStephen Boyd return hw; 5169d9f78edSMike Turquette } 517357c3f0aSRajendra Nayak 518357c3f0aSRajendra Nayak /** 519357c3f0aSRajendra Nayak * clk_register_divider - register a divider clock with the clock framework 520357c3f0aSRajendra Nayak * @dev: device registering this clock 521357c3f0aSRajendra Nayak * @name: name of this clock 522357c3f0aSRajendra Nayak * @parent_name: name of clock's parent 523357c3f0aSRajendra Nayak * @flags: framework-specific flags 524357c3f0aSRajendra Nayak * @reg: register address to adjust divider 525357c3f0aSRajendra Nayak * @shift: number of bits to shift the bitfield 526357c3f0aSRajendra Nayak * @width: width of the bitfield 527357c3f0aSRajendra Nayak * @clk_divider_flags: divider-specific flags for this clock 528357c3f0aSRajendra Nayak * @lock: shared register lock for this clock 529357c3f0aSRajendra Nayak */ 530357c3f0aSRajendra Nayak struct clk *clk_register_divider(struct device *dev, const char *name, 531357c3f0aSRajendra Nayak const char *parent_name, unsigned long flags, 532357c3f0aSRajendra Nayak void __iomem *reg, u8 shift, u8 width, 533357c3f0aSRajendra Nayak u8 clk_divider_flags, spinlock_t *lock) 534357c3f0aSRajendra Nayak { 535eb7d264fSStephen Boyd struct clk_hw *hw; 536eb7d264fSStephen Boyd 537eb7d264fSStephen Boyd hw = _register_divider(dev, name, parent_name, flags, reg, shift, 538eb7d264fSStephen Boyd width, clk_divider_flags, NULL, lock); 539eb7d264fSStephen Boyd if (IS_ERR(hw)) 540eb7d264fSStephen Boyd return ERR_CAST(hw); 541eb7d264fSStephen Boyd return hw->clk; 542eb7d264fSStephen Boyd } 543eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_register_divider); 544eb7d264fSStephen Boyd 545eb7d264fSStephen Boyd /** 546eb7d264fSStephen Boyd * clk_hw_register_divider - register a divider clock with the clock framework 547eb7d264fSStephen Boyd * @dev: device registering this clock 548eb7d264fSStephen Boyd * @name: name of this clock 549eb7d264fSStephen Boyd * @parent_name: name of clock's parent 550eb7d264fSStephen Boyd * @flags: framework-specific flags 551eb7d264fSStephen Boyd * @reg: register address to adjust divider 552eb7d264fSStephen Boyd * @shift: number of bits to shift the bitfield 553eb7d264fSStephen Boyd * @width: width of the bitfield 554eb7d264fSStephen Boyd * @clk_divider_flags: divider-specific flags for this clock 555eb7d264fSStephen Boyd * @lock: shared register lock for this clock 556eb7d264fSStephen Boyd */ 557eb7d264fSStephen Boyd struct clk_hw *clk_hw_register_divider(struct device *dev, const char *name, 558eb7d264fSStephen Boyd const char *parent_name, unsigned long flags, 559eb7d264fSStephen Boyd void __iomem *reg, u8 shift, u8 width, 560eb7d264fSStephen Boyd u8 clk_divider_flags, spinlock_t *lock) 561eb7d264fSStephen Boyd { 562357c3f0aSRajendra Nayak return _register_divider(dev, name, parent_name, flags, reg, shift, 563357c3f0aSRajendra Nayak width, clk_divider_flags, NULL, lock); 564357c3f0aSRajendra Nayak } 565eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_hw_register_divider); 566357c3f0aSRajendra Nayak 567357c3f0aSRajendra Nayak /** 568357c3f0aSRajendra Nayak * clk_register_divider_table - register a table based divider clock with 569357c3f0aSRajendra Nayak * the clock framework 570357c3f0aSRajendra Nayak * @dev: device registering this clock 571357c3f0aSRajendra Nayak * @name: name of this clock 572357c3f0aSRajendra Nayak * @parent_name: name of clock's parent 573357c3f0aSRajendra Nayak * @flags: framework-specific flags 574357c3f0aSRajendra Nayak * @reg: register address to adjust divider 575357c3f0aSRajendra Nayak * @shift: number of bits to shift the bitfield 576357c3f0aSRajendra Nayak * @width: width of the bitfield 577357c3f0aSRajendra Nayak * @clk_divider_flags: divider-specific flags for this clock 578357c3f0aSRajendra Nayak * @table: array of divider/value pairs ending with a div set to 0 579357c3f0aSRajendra Nayak * @lock: shared register lock for this clock 580357c3f0aSRajendra Nayak */ 581357c3f0aSRajendra Nayak struct clk *clk_register_divider_table(struct device *dev, const char *name, 582357c3f0aSRajendra Nayak const char *parent_name, unsigned long flags, 583357c3f0aSRajendra Nayak void __iomem *reg, u8 shift, u8 width, 584357c3f0aSRajendra Nayak u8 clk_divider_flags, const struct clk_div_table *table, 585357c3f0aSRajendra Nayak spinlock_t *lock) 586357c3f0aSRajendra Nayak { 587eb7d264fSStephen Boyd struct clk_hw *hw; 588eb7d264fSStephen Boyd 589eb7d264fSStephen Boyd hw = _register_divider(dev, name, parent_name, flags, reg, shift, 590eb7d264fSStephen Boyd width, clk_divider_flags, table, lock); 591eb7d264fSStephen Boyd if (IS_ERR(hw)) 592eb7d264fSStephen Boyd return ERR_CAST(hw); 593eb7d264fSStephen Boyd return hw->clk; 594eb7d264fSStephen Boyd } 595eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_register_divider_table); 596eb7d264fSStephen Boyd 597eb7d264fSStephen Boyd /** 598eb7d264fSStephen Boyd * clk_hw_register_divider_table - register a table based divider clock with 599eb7d264fSStephen Boyd * the clock framework 600eb7d264fSStephen Boyd * @dev: device registering this clock 601eb7d264fSStephen Boyd * @name: name of this clock 602eb7d264fSStephen Boyd * @parent_name: name of clock's parent 603eb7d264fSStephen Boyd * @flags: framework-specific flags 604eb7d264fSStephen Boyd * @reg: register address to adjust divider 605eb7d264fSStephen Boyd * @shift: number of bits to shift the bitfield 606eb7d264fSStephen Boyd * @width: width of the bitfield 607eb7d264fSStephen Boyd * @clk_divider_flags: divider-specific flags for this clock 608eb7d264fSStephen Boyd * @table: array of divider/value pairs ending with a div set to 0 609eb7d264fSStephen Boyd * @lock: shared register lock for this clock 610eb7d264fSStephen Boyd */ 611eb7d264fSStephen Boyd struct clk_hw *clk_hw_register_divider_table(struct device *dev, 612eb7d264fSStephen Boyd const char *name, const char *parent_name, unsigned long flags, 613eb7d264fSStephen Boyd void __iomem *reg, u8 shift, u8 width, 614eb7d264fSStephen Boyd u8 clk_divider_flags, const struct clk_div_table *table, 615eb7d264fSStephen Boyd spinlock_t *lock) 616eb7d264fSStephen Boyd { 617357c3f0aSRajendra Nayak return _register_divider(dev, name, parent_name, flags, reg, shift, 618357c3f0aSRajendra Nayak width, clk_divider_flags, table, lock); 619357c3f0aSRajendra Nayak } 620eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_hw_register_divider_table); 6214e3c021fSKrzysztof Kozlowski 6224e3c021fSKrzysztof Kozlowski void clk_unregister_divider(struct clk *clk) 6234e3c021fSKrzysztof Kozlowski { 6244e3c021fSKrzysztof Kozlowski struct clk_divider *div; 6254e3c021fSKrzysztof Kozlowski struct clk_hw *hw; 6264e3c021fSKrzysztof Kozlowski 6274e3c021fSKrzysztof Kozlowski hw = __clk_get_hw(clk); 6284e3c021fSKrzysztof Kozlowski if (!hw) 6294e3c021fSKrzysztof Kozlowski return; 6304e3c021fSKrzysztof Kozlowski 6314e3c021fSKrzysztof Kozlowski div = to_clk_divider(hw); 6324e3c021fSKrzysztof Kozlowski 6334e3c021fSKrzysztof Kozlowski clk_unregister(clk); 6344e3c021fSKrzysztof Kozlowski kfree(div); 6354e3c021fSKrzysztof Kozlowski } 6364e3c021fSKrzysztof Kozlowski EXPORT_SYMBOL_GPL(clk_unregister_divider); 637eb7d264fSStephen Boyd 638eb7d264fSStephen Boyd /** 639eb7d264fSStephen Boyd * clk_hw_unregister_divider - unregister a clk divider 640eb7d264fSStephen Boyd * @hw: hardware-specific clock data to unregister 641eb7d264fSStephen Boyd */ 642eb7d264fSStephen Boyd void clk_hw_unregister_divider(struct clk_hw *hw) 643eb7d264fSStephen Boyd { 644eb7d264fSStephen Boyd struct clk_divider *div; 645eb7d264fSStephen Boyd 646eb7d264fSStephen Boyd div = to_clk_divider(hw); 647eb7d264fSStephen Boyd 648eb7d264fSStephen Boyd clk_hw_unregister(hw); 649eb7d264fSStephen Boyd kfree(div); 650eb7d264fSStephen Boyd } 651eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_hw_unregister_divider); 652