19d9f78edSMike Turquette /* 29d9f78edSMike Turquette * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> 39d9f78edSMike Turquette * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org> 49d9f78edSMike Turquette * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org> 59d9f78edSMike Turquette * 69d9f78edSMike Turquette * This program is free software; you can redistribute it and/or modify 79d9f78edSMike Turquette * it under the terms of the GNU General Public License version 2 as 89d9f78edSMike Turquette * published by the Free Software Foundation. 99d9f78edSMike Turquette * 109d9f78edSMike Turquette * Adjustable divider clock implementation 119d9f78edSMike Turquette */ 129d9f78edSMike Turquette 139d9f78edSMike Turquette #include <linux/clk-provider.h> 149d9f78edSMike Turquette #include <linux/module.h> 159d9f78edSMike Turquette #include <linux/slab.h> 169d9f78edSMike Turquette #include <linux/io.h> 179d9f78edSMike Turquette #include <linux/err.h> 189d9f78edSMike Turquette #include <linux/string.h> 191a3cd184SJames Hogan #include <linux/log2.h> 209d9f78edSMike Turquette 219d9f78edSMike Turquette /* 229d9f78edSMike Turquette * DOC: basic adjustable divider clock that cannot gate 239d9f78edSMike Turquette * 249d9f78edSMike Turquette * Traits of this clock: 259d9f78edSMike Turquette * prepare - clk_prepare only ensures that parents are prepared 269d9f78edSMike Turquette * enable - clk_enable only ensures that parents are enabled 279556f9daSBrian Norris * rate - rate is adjustable. clk->rate = ceiling(parent->rate / divisor) 289d9f78edSMike Turquette * parent - fixed parent. No clk_set_parent support 299d9f78edSMike Turquette */ 309d9f78edSMike Turquette 31bca9690bSStephen Boyd #define div_mask(width) ((1 << (width)) - 1) 326d9252bdSRajendra Nayak 33fab88ca7SStephen Boyd static unsigned int _get_table_maxdiv(const struct clk_div_table *table, 34fab88ca7SStephen Boyd u8 width) 35357c3f0aSRajendra Nayak { 36fab88ca7SStephen Boyd unsigned int maxdiv = 0, mask = div_mask(width); 37357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 38357c3f0aSRajendra Nayak 39357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 40fab88ca7SStephen Boyd if (clkt->div > maxdiv && clkt->val <= mask) 41357c3f0aSRajendra Nayak maxdiv = clkt->div; 42357c3f0aSRajendra Nayak return maxdiv; 43357c3f0aSRajendra Nayak } 44357c3f0aSRajendra Nayak 45774b5143SMaxime COQUELIN static unsigned int _get_table_mindiv(const struct clk_div_table *table) 46774b5143SMaxime COQUELIN { 47774b5143SMaxime COQUELIN unsigned int mindiv = UINT_MAX; 48774b5143SMaxime COQUELIN const struct clk_div_table *clkt; 49774b5143SMaxime COQUELIN 50774b5143SMaxime COQUELIN for (clkt = table; clkt->div; clkt++) 51774b5143SMaxime COQUELIN if (clkt->div < mindiv) 52774b5143SMaxime COQUELIN mindiv = clkt->div; 53774b5143SMaxime COQUELIN return mindiv; 54774b5143SMaxime COQUELIN } 55774b5143SMaxime COQUELIN 56bca9690bSStephen Boyd static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width, 57bca9690bSStephen Boyd unsigned long flags) 586d9252bdSRajendra Nayak { 59bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ONE_BASED) 60bca9690bSStephen Boyd return div_mask(width); 61bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 62bca9690bSStephen Boyd return 1 << div_mask(width); 63bca9690bSStephen Boyd if (table) 64fab88ca7SStephen Boyd return _get_table_maxdiv(table, width); 65bca9690bSStephen Boyd return div_mask(width) + 1; 666d9252bdSRajendra Nayak } 676d9252bdSRajendra Nayak 68357c3f0aSRajendra Nayak static unsigned int _get_table_div(const struct clk_div_table *table, 69357c3f0aSRajendra Nayak unsigned int val) 70357c3f0aSRajendra Nayak { 71357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 72357c3f0aSRajendra Nayak 73357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 74357c3f0aSRajendra Nayak if (clkt->val == val) 75357c3f0aSRajendra Nayak return clkt->div; 76357c3f0aSRajendra Nayak return 0; 77357c3f0aSRajendra Nayak } 78357c3f0aSRajendra Nayak 79bca9690bSStephen Boyd static unsigned int _get_div(const struct clk_div_table *table, 80afe76c8fSJim Quinlan unsigned int val, unsigned long flags, u8 width) 816d9252bdSRajendra Nayak { 82bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ONE_BASED) 836d9252bdSRajendra Nayak return val; 84bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 856d9252bdSRajendra Nayak return 1 << val; 86afe76c8fSJim Quinlan if (flags & CLK_DIVIDER_MAX_AT_ZERO) 87afe76c8fSJim Quinlan return val ? val : div_mask(width) + 1; 88bca9690bSStephen Boyd if (table) 89bca9690bSStephen Boyd return _get_table_div(table, val); 906d9252bdSRajendra Nayak return val + 1; 916d9252bdSRajendra Nayak } 926d9252bdSRajendra Nayak 93357c3f0aSRajendra Nayak static unsigned int _get_table_val(const struct clk_div_table *table, 94357c3f0aSRajendra Nayak unsigned int div) 95357c3f0aSRajendra Nayak { 96357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 97357c3f0aSRajendra Nayak 98357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 99357c3f0aSRajendra Nayak if (clkt->div == div) 100357c3f0aSRajendra Nayak return clkt->val; 101357c3f0aSRajendra Nayak return 0; 102357c3f0aSRajendra Nayak } 103357c3f0aSRajendra Nayak 104bca9690bSStephen Boyd static unsigned int _get_val(const struct clk_div_table *table, 105afe76c8fSJim Quinlan unsigned int div, unsigned long flags, u8 width) 1066d9252bdSRajendra Nayak { 107bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ONE_BASED) 1086d9252bdSRajendra Nayak return div; 109bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 1106d9252bdSRajendra Nayak return __ffs(div); 111afe76c8fSJim Quinlan if (flags & CLK_DIVIDER_MAX_AT_ZERO) 112afe76c8fSJim Quinlan return (div == div_mask(width) + 1) ? 0 : div; 113bca9690bSStephen Boyd if (table) 114bca9690bSStephen Boyd return _get_table_val(table, div); 1156d9252bdSRajendra Nayak return div - 1; 1166d9252bdSRajendra Nayak } 1179d9f78edSMike Turquette 118bca9690bSStephen Boyd unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate, 119bca9690bSStephen Boyd unsigned int val, 120bca9690bSStephen Boyd const struct clk_div_table *table, 121bca9690bSStephen Boyd unsigned long flags) 1229d9f78edSMike Turquette { 123afe76c8fSJim Quinlan struct clk_divider *divider = to_clk_divider(hw); 124bca9690bSStephen Boyd unsigned int div; 1259d9f78edSMike Turquette 126afe76c8fSJim Quinlan div = _get_div(table, val, flags, divider->width); 1276d9252bdSRajendra Nayak if (!div) { 128bca9690bSStephen Boyd WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO), 129056b2053SSoren Brinkmann "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n", 1302f508a95SStephen Boyd clk_hw_get_name(hw)); 1316d9252bdSRajendra Nayak return parent_rate; 1326d9252bdSRajendra Nayak } 1339d9f78edSMike Turquette 1349556f9daSBrian Norris return DIV_ROUND_UP_ULL((u64)parent_rate, div); 1359d9f78edSMike Turquette } 136bca9690bSStephen Boyd EXPORT_SYMBOL_GPL(divider_recalc_rate); 137bca9690bSStephen Boyd 138bca9690bSStephen Boyd static unsigned long clk_divider_recalc_rate(struct clk_hw *hw, 139bca9690bSStephen Boyd unsigned long parent_rate) 140bca9690bSStephen Boyd { 141bca9690bSStephen Boyd struct clk_divider *divider = to_clk_divider(hw); 142bca9690bSStephen Boyd unsigned int val; 143bca9690bSStephen Boyd 144bca9690bSStephen Boyd val = clk_readl(divider->reg) >> divider->shift; 145bca9690bSStephen Boyd val &= div_mask(divider->width); 146bca9690bSStephen Boyd 147bca9690bSStephen Boyd return divider_recalc_rate(hw, parent_rate, val, divider->table, 148bca9690bSStephen Boyd divider->flags); 149bca9690bSStephen Boyd } 1509d9f78edSMike Turquette 151357c3f0aSRajendra Nayak static bool _is_valid_table_div(const struct clk_div_table *table, 152357c3f0aSRajendra Nayak unsigned int div) 153357c3f0aSRajendra Nayak { 154357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 155357c3f0aSRajendra Nayak 156357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 157357c3f0aSRajendra Nayak if (clkt->div == div) 158357c3f0aSRajendra Nayak return true; 159357c3f0aSRajendra Nayak return false; 160357c3f0aSRajendra Nayak } 161357c3f0aSRajendra Nayak 162bca9690bSStephen Boyd static bool _is_valid_div(const struct clk_div_table *table, unsigned int div, 163bca9690bSStephen Boyd unsigned long flags) 164357c3f0aSRajendra Nayak { 165bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 1661a3cd184SJames Hogan return is_power_of_2(div); 167bca9690bSStephen Boyd if (table) 168bca9690bSStephen Boyd return _is_valid_table_div(table, div); 169357c3f0aSRajendra Nayak return true; 170357c3f0aSRajendra Nayak } 171357c3f0aSRajendra Nayak 172dd23c2cdSMaxime COQUELIN static int _round_up_table(const struct clk_div_table *table, int div) 173dd23c2cdSMaxime COQUELIN { 174dd23c2cdSMaxime COQUELIN const struct clk_div_table *clkt; 175fe52e750SMaxime COQUELIN int up = INT_MAX; 176dd23c2cdSMaxime COQUELIN 177dd23c2cdSMaxime COQUELIN for (clkt = table; clkt->div; clkt++) { 178dd23c2cdSMaxime COQUELIN if (clkt->div == div) 179dd23c2cdSMaxime COQUELIN return clkt->div; 180dd23c2cdSMaxime COQUELIN else if (clkt->div < div) 181dd23c2cdSMaxime COQUELIN continue; 182dd23c2cdSMaxime COQUELIN 183dd23c2cdSMaxime COQUELIN if ((clkt->div - div) < (up - div)) 184dd23c2cdSMaxime COQUELIN up = clkt->div; 185dd23c2cdSMaxime COQUELIN } 186dd23c2cdSMaxime COQUELIN 187dd23c2cdSMaxime COQUELIN return up; 188dd23c2cdSMaxime COQUELIN } 189dd23c2cdSMaxime COQUELIN 190774b5143SMaxime COQUELIN static int _round_down_table(const struct clk_div_table *table, int div) 191774b5143SMaxime COQUELIN { 192774b5143SMaxime COQUELIN const struct clk_div_table *clkt; 193774b5143SMaxime COQUELIN int down = _get_table_mindiv(table); 194774b5143SMaxime COQUELIN 195774b5143SMaxime COQUELIN for (clkt = table; clkt->div; clkt++) { 196774b5143SMaxime COQUELIN if (clkt->div == div) 197774b5143SMaxime COQUELIN return clkt->div; 198774b5143SMaxime COQUELIN else if (clkt->div > div) 199774b5143SMaxime COQUELIN continue; 200774b5143SMaxime COQUELIN 201774b5143SMaxime COQUELIN if ((div - clkt->div) < (div - down)) 202774b5143SMaxime COQUELIN down = clkt->div; 203774b5143SMaxime COQUELIN } 204774b5143SMaxime COQUELIN 205774b5143SMaxime COQUELIN return down; 206774b5143SMaxime COQUELIN } 207774b5143SMaxime COQUELIN 208bca9690bSStephen Boyd static int _div_round_up(const struct clk_div_table *table, 209bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 210bca9690bSStephen Boyd unsigned long flags) 211dd23c2cdSMaxime COQUELIN { 2129556f9daSBrian Norris int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 213dd23c2cdSMaxime COQUELIN 214bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 215dd23c2cdSMaxime COQUELIN div = __roundup_pow_of_two(div); 216bca9690bSStephen Boyd if (table) 217bca9690bSStephen Boyd div = _round_up_table(table, div); 218dd23c2cdSMaxime COQUELIN 219dd23c2cdSMaxime COQUELIN return div; 220dd23c2cdSMaxime COQUELIN } 221dd23c2cdSMaxime COQUELIN 222bca9690bSStephen Boyd static int _div_round_closest(const struct clk_div_table *table, 223bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 224bca9690bSStephen Boyd unsigned long flags) 225774b5143SMaxime COQUELIN { 22693155142SUwe Kleine-König int up, down; 22726bac95aSUwe Kleine-König unsigned long up_rate, down_rate; 228774b5143SMaxime COQUELIN 2299556f9daSBrian Norris up = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 23093155142SUwe Kleine-König down = parent_rate / rate; 231774b5143SMaxime COQUELIN 232bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) { 23393155142SUwe Kleine-König up = __roundup_pow_of_two(up); 23493155142SUwe Kleine-König down = __rounddown_pow_of_two(down); 235bca9690bSStephen Boyd } else if (table) { 23693155142SUwe Kleine-König up = _round_up_table(table, up); 23793155142SUwe Kleine-König down = _round_down_table(table, down); 238774b5143SMaxime COQUELIN } 239774b5143SMaxime COQUELIN 2409556f9daSBrian Norris up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up); 2419556f9daSBrian Norris down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down); 24226bac95aSUwe Kleine-König 24326bac95aSUwe Kleine-König return (rate - up_rate) <= (down_rate - rate) ? up : down; 244774b5143SMaxime COQUELIN } 245774b5143SMaxime COQUELIN 246bca9690bSStephen Boyd static int _div_round(const struct clk_div_table *table, 247bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 248bca9690bSStephen Boyd unsigned long flags) 249774b5143SMaxime COQUELIN { 250bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ROUND_CLOSEST) 251bca9690bSStephen Boyd return _div_round_closest(table, parent_rate, rate, flags); 252774b5143SMaxime COQUELIN 253bca9690bSStephen Boyd return _div_round_up(table, parent_rate, rate, flags); 254774b5143SMaxime COQUELIN } 255774b5143SMaxime COQUELIN 256bca9690bSStephen Boyd static bool _is_best_div(unsigned long rate, unsigned long now, 257bca9690bSStephen Boyd unsigned long best, unsigned long flags) 258774b5143SMaxime COQUELIN { 259bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ROUND_CLOSEST) 260774b5143SMaxime COQUELIN return abs(rate - now) < abs(rate - best); 261774b5143SMaxime COQUELIN 262774b5143SMaxime COQUELIN return now <= rate && now > best; 263774b5143SMaxime COQUELIN } 264774b5143SMaxime COQUELIN 265bca9690bSStephen Boyd static int _next_div(const struct clk_div_table *table, int div, 266bca9690bSStephen Boyd unsigned long flags) 2670e2de78eSMaxime COQUELIN { 2680e2de78eSMaxime COQUELIN div++; 2690e2de78eSMaxime COQUELIN 270bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 2710e2de78eSMaxime COQUELIN return __roundup_pow_of_two(div); 272bca9690bSStephen Boyd if (table) 273bca9690bSStephen Boyd return _round_up_table(table, div); 2740e2de78eSMaxime COQUELIN 2750e2de78eSMaxime COQUELIN return div; 2760e2de78eSMaxime COQUELIN } 2770e2de78eSMaxime COQUELIN 278*22833a91SMaxime Ripard static int clk_divider_bestdiv(struct clk_hw *hw, struct clk_hw *parent, 279*22833a91SMaxime Ripard unsigned long rate, 280bca9690bSStephen Boyd unsigned long *best_parent_rate, 281bca9690bSStephen Boyd const struct clk_div_table *table, u8 width, 282bca9690bSStephen Boyd unsigned long flags) 2839d9f78edSMike Turquette { 2849d9f78edSMike Turquette int i, bestdiv = 0; 2859d9f78edSMike Turquette unsigned long parent_rate, best = 0, now, maxdiv; 286081c9025SShawn Guo unsigned long parent_rate_saved = *best_parent_rate; 2879d9f78edSMike Turquette 2889d9f78edSMike Turquette if (!rate) 2899d9f78edSMike Turquette rate = 1; 2909d9f78edSMike Turquette 291bca9690bSStephen Boyd maxdiv = _get_maxdiv(table, width, flags); 2929d9f78edSMike Turquette 29398d8a60eSStephen Boyd if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) { 29481536e07SShawn Guo parent_rate = *best_parent_rate; 295bca9690bSStephen Boyd bestdiv = _div_round(table, parent_rate, rate, flags); 2969d9f78edSMike Turquette bestdiv = bestdiv == 0 ? 1 : bestdiv; 2979d9f78edSMike Turquette bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv; 2989d9f78edSMike Turquette return bestdiv; 2999d9f78edSMike Turquette } 3009d9f78edSMike Turquette 3019d9f78edSMike Turquette /* 3029d9f78edSMike Turquette * The maximum divider we can use without overflowing 3039d9f78edSMike Turquette * unsigned long in rate * i below 3049d9f78edSMike Turquette */ 3059d9f78edSMike Turquette maxdiv = min(ULONG_MAX / rate, maxdiv); 3069d9f78edSMike Turquette 307653d1452SMasahiro Yamada for (i = _next_div(table, 0, flags); i <= maxdiv; 308653d1452SMasahiro Yamada i = _next_div(table, i, flags)) { 309081c9025SShawn Guo if (rate * i == parent_rate_saved) { 310081c9025SShawn Guo /* 311081c9025SShawn Guo * It's the most ideal case if the requested rate can be 312081c9025SShawn Guo * divided from parent clock without needing to change 313081c9025SShawn Guo * parent rate, so return the divider immediately. 314081c9025SShawn Guo */ 315081c9025SShawn Guo *best_parent_rate = parent_rate_saved; 316081c9025SShawn Guo return i; 317081c9025SShawn Guo } 318*22833a91SMaxime Ripard parent_rate = clk_hw_round_rate(parent, rate * i); 3199556f9daSBrian Norris now = DIV_ROUND_UP_ULL((u64)parent_rate, i); 320bca9690bSStephen Boyd if (_is_best_div(rate, now, best, flags)) { 3219d9f78edSMike Turquette bestdiv = i; 3229d9f78edSMike Turquette best = now; 3239d9f78edSMike Turquette *best_parent_rate = parent_rate; 3249d9f78edSMike Turquette } 3259d9f78edSMike Turquette } 3269d9f78edSMike Turquette 3279d9f78edSMike Turquette if (!bestdiv) { 328bca9690bSStephen Boyd bestdiv = _get_maxdiv(table, width, flags); 329*22833a91SMaxime Ripard *best_parent_rate = clk_hw_round_rate(parent, 1); 3309d9f78edSMike Turquette } 3319d9f78edSMike Turquette 3329d9f78edSMike Turquette return bestdiv; 3339d9f78edSMike Turquette } 3349d9f78edSMike Turquette 335*22833a91SMaxime Ripard long divider_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent, 336*22833a91SMaxime Ripard unsigned long rate, unsigned long *prate, 337*22833a91SMaxime Ripard const struct clk_div_table *table, 338bca9690bSStephen Boyd u8 width, unsigned long flags) 3399d9f78edSMike Turquette { 3409d9f78edSMike Turquette int div; 341bca9690bSStephen Boyd 342*22833a91SMaxime Ripard div = clk_divider_bestdiv(hw, parent, rate, prate, table, width, flags); 3439d9f78edSMike Turquette 3449556f9daSBrian Norris return DIV_ROUND_UP_ULL((u64)*prate, div); 3459d9f78edSMike Turquette } 346*22833a91SMaxime Ripard EXPORT_SYMBOL_GPL(divider_round_rate_parent); 347bca9690bSStephen Boyd 348bca9690bSStephen Boyd static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate, 349bca9690bSStephen Boyd unsigned long *prate) 350bca9690bSStephen Boyd { 351bca9690bSStephen Boyd struct clk_divider *divider = to_clk_divider(hw); 352bca9690bSStephen Boyd int bestdiv; 353bca9690bSStephen Boyd 354bca9690bSStephen Boyd /* if read only, just return current value */ 355bca9690bSStephen Boyd if (divider->flags & CLK_DIVIDER_READ_ONLY) { 3562cf9a578SGeert Uytterhoeven bestdiv = clk_readl(divider->reg) >> divider->shift; 357bca9690bSStephen Boyd bestdiv &= div_mask(divider->width); 358afe76c8fSJim Quinlan bestdiv = _get_div(divider->table, bestdiv, divider->flags, 359afe76c8fSJim Quinlan divider->width); 3609556f9daSBrian Norris return DIV_ROUND_UP_ULL((u64)*prate, bestdiv); 361bca9690bSStephen Boyd } 362bca9690bSStephen Boyd 363bca9690bSStephen Boyd return divider_round_rate(hw, rate, prate, divider->table, 364bca9690bSStephen Boyd divider->width, divider->flags); 365bca9690bSStephen Boyd } 366bca9690bSStephen Boyd 367bca9690bSStephen Boyd int divider_get_val(unsigned long rate, unsigned long parent_rate, 368bca9690bSStephen Boyd const struct clk_div_table *table, u8 width, 369bca9690bSStephen Boyd unsigned long flags) 370bca9690bSStephen Boyd { 371bca9690bSStephen Boyd unsigned int div, value; 372bca9690bSStephen Boyd 3739556f9daSBrian Norris div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 374bca9690bSStephen Boyd 375bca9690bSStephen Boyd if (!_is_valid_div(table, div, flags)) 376bca9690bSStephen Boyd return -EINVAL; 377bca9690bSStephen Boyd 378afe76c8fSJim Quinlan value = _get_val(table, div, flags, width); 379bca9690bSStephen Boyd 380bca9690bSStephen Boyd return min_t(unsigned int, value, div_mask(width)); 381bca9690bSStephen Boyd } 382bca9690bSStephen Boyd EXPORT_SYMBOL_GPL(divider_get_val); 3839d9f78edSMike Turquette 3841c0035d7SShawn Guo static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, 3851c0035d7SShawn Guo unsigned long parent_rate) 3869d9f78edSMike Turquette { 3879d9f78edSMike Turquette struct clk_divider *divider = to_clk_divider(hw); 388bca9690bSStephen Boyd unsigned int value; 3899d9f78edSMike Turquette unsigned long flags = 0; 3909d9f78edSMike Turquette u32 val; 3919d9f78edSMike Turquette 392bca9690bSStephen Boyd value = divider_get_val(rate, parent_rate, divider->table, 393bca9690bSStephen Boyd divider->width, divider->flags); 3949d9f78edSMike Turquette 3959d9f78edSMike Turquette if (divider->lock) 3969d9f78edSMike Turquette spin_lock_irqsave(divider->lock, flags); 397661e2180SStephen Boyd else 398661e2180SStephen Boyd __acquire(divider->lock); 3999d9f78edSMike Turquette 400d57dfe75SHaojian Zhuang if (divider->flags & CLK_DIVIDER_HIWORD_MASK) { 401bca9690bSStephen Boyd val = div_mask(divider->width) << (divider->shift + 16); 402d57dfe75SHaojian Zhuang } else { 403aa514ce3SGerhard Sittig val = clk_readl(divider->reg); 404bca9690bSStephen Boyd val &= ~(div_mask(divider->width) << divider->shift); 405d57dfe75SHaojian Zhuang } 4066d9252bdSRajendra Nayak val |= value << divider->shift; 407aa514ce3SGerhard Sittig clk_writel(val, divider->reg); 4089d9f78edSMike Turquette 4099d9f78edSMike Turquette if (divider->lock) 4109d9f78edSMike Turquette spin_unlock_irqrestore(divider->lock, flags); 411661e2180SStephen Boyd else 412661e2180SStephen Boyd __release(divider->lock); 4139d9f78edSMike Turquette 4149d9f78edSMike Turquette return 0; 4159d9f78edSMike Turquette } 4169d9f78edSMike Turquette 417822c250eSShawn Guo const struct clk_ops clk_divider_ops = { 4189d9f78edSMike Turquette .recalc_rate = clk_divider_recalc_rate, 4199d9f78edSMike Turquette .round_rate = clk_divider_round_rate, 4209d9f78edSMike Turquette .set_rate = clk_divider_set_rate, 4219d9f78edSMike Turquette }; 4229d9f78edSMike Turquette EXPORT_SYMBOL_GPL(clk_divider_ops); 4239d9f78edSMike Turquette 42450359819SHeiko Stuebner const struct clk_ops clk_divider_ro_ops = { 42550359819SHeiko Stuebner .recalc_rate = clk_divider_recalc_rate, 42650359819SHeiko Stuebner .round_rate = clk_divider_round_rate, 42750359819SHeiko Stuebner }; 42850359819SHeiko Stuebner EXPORT_SYMBOL_GPL(clk_divider_ro_ops); 42950359819SHeiko Stuebner 430eb7d264fSStephen Boyd static struct clk_hw *_register_divider(struct device *dev, const char *name, 4319d9f78edSMike Turquette const char *parent_name, unsigned long flags, 4329d9f78edSMike Turquette void __iomem *reg, u8 shift, u8 width, 433357c3f0aSRajendra Nayak u8 clk_divider_flags, const struct clk_div_table *table, 434357c3f0aSRajendra Nayak spinlock_t *lock) 4359d9f78edSMike Turquette { 4369d9f78edSMike Turquette struct clk_divider *div; 437eb7d264fSStephen Boyd struct clk_hw *hw; 4380197b3eaSSaravana Kannan struct clk_init_data init; 439eb7d264fSStephen Boyd int ret; 4409d9f78edSMike Turquette 441d57dfe75SHaojian Zhuang if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) { 442d57dfe75SHaojian Zhuang if (width + shift > 16) { 443d57dfe75SHaojian Zhuang pr_warn("divider value exceeds LOWORD field\n"); 444d57dfe75SHaojian Zhuang return ERR_PTR(-EINVAL); 445d57dfe75SHaojian Zhuang } 446d57dfe75SHaojian Zhuang } 447d57dfe75SHaojian Zhuang 44827d54591SMike Turquette /* allocate the divider */ 449d122db7eSStephen Boyd div = kzalloc(sizeof(*div), GFP_KERNEL); 450d122db7eSStephen Boyd if (!div) 45127d54591SMike Turquette return ERR_PTR(-ENOMEM); 4529d9f78edSMike Turquette 4530197b3eaSSaravana Kannan init.name = name; 45450359819SHeiko Stuebner if (clk_divider_flags & CLK_DIVIDER_READ_ONLY) 45550359819SHeiko Stuebner init.ops = &clk_divider_ro_ops; 45650359819SHeiko Stuebner else 4570197b3eaSSaravana Kannan init.ops = &clk_divider_ops; 458f7d8caadSRajendra Nayak init.flags = flags | CLK_IS_BASIC; 4590197b3eaSSaravana Kannan init.parent_names = (parent_name ? &parent_name: NULL); 4600197b3eaSSaravana Kannan init.num_parents = (parent_name ? 1 : 0); 4610197b3eaSSaravana Kannan 4629d9f78edSMike Turquette /* struct clk_divider assignments */ 4639d9f78edSMike Turquette div->reg = reg; 4649d9f78edSMike Turquette div->shift = shift; 4659d9f78edSMike Turquette div->width = width; 4669d9f78edSMike Turquette div->flags = clk_divider_flags; 4679d9f78edSMike Turquette div->lock = lock; 4680197b3eaSSaravana Kannan div->hw.init = &init; 469357c3f0aSRajendra Nayak div->table = table; 4709d9f78edSMike Turquette 47127d54591SMike Turquette /* register the clock */ 472eb7d264fSStephen Boyd hw = &div->hw; 473eb7d264fSStephen Boyd ret = clk_hw_register(dev, hw); 474eb7d264fSStephen Boyd if (ret) { 4759d9f78edSMike Turquette kfree(div); 476eb7d264fSStephen Boyd hw = ERR_PTR(ret); 477eb7d264fSStephen Boyd } 4789d9f78edSMike Turquette 479eb7d264fSStephen Boyd return hw; 4809d9f78edSMike Turquette } 481357c3f0aSRajendra Nayak 482357c3f0aSRajendra Nayak /** 483357c3f0aSRajendra Nayak * clk_register_divider - register a divider clock with the clock framework 484357c3f0aSRajendra Nayak * @dev: device registering this clock 485357c3f0aSRajendra Nayak * @name: name of this clock 486357c3f0aSRajendra Nayak * @parent_name: name of clock's parent 487357c3f0aSRajendra Nayak * @flags: framework-specific flags 488357c3f0aSRajendra Nayak * @reg: register address to adjust divider 489357c3f0aSRajendra Nayak * @shift: number of bits to shift the bitfield 490357c3f0aSRajendra Nayak * @width: width of the bitfield 491357c3f0aSRajendra Nayak * @clk_divider_flags: divider-specific flags for this clock 492357c3f0aSRajendra Nayak * @lock: shared register lock for this clock 493357c3f0aSRajendra Nayak */ 494357c3f0aSRajendra Nayak struct clk *clk_register_divider(struct device *dev, const char *name, 495357c3f0aSRajendra Nayak const char *parent_name, unsigned long flags, 496357c3f0aSRajendra Nayak void __iomem *reg, u8 shift, u8 width, 497357c3f0aSRajendra Nayak u8 clk_divider_flags, spinlock_t *lock) 498357c3f0aSRajendra Nayak { 499eb7d264fSStephen Boyd struct clk_hw *hw; 500eb7d264fSStephen Boyd 501eb7d264fSStephen Boyd hw = _register_divider(dev, name, parent_name, flags, reg, shift, 502eb7d264fSStephen Boyd width, clk_divider_flags, NULL, lock); 503eb7d264fSStephen Boyd if (IS_ERR(hw)) 504eb7d264fSStephen Boyd return ERR_CAST(hw); 505eb7d264fSStephen Boyd return hw->clk; 506eb7d264fSStephen Boyd } 507eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_register_divider); 508eb7d264fSStephen Boyd 509eb7d264fSStephen Boyd /** 510eb7d264fSStephen Boyd * clk_hw_register_divider - register a divider clock with the clock framework 511eb7d264fSStephen Boyd * @dev: device registering this clock 512eb7d264fSStephen Boyd * @name: name of this clock 513eb7d264fSStephen Boyd * @parent_name: name of clock's parent 514eb7d264fSStephen Boyd * @flags: framework-specific flags 515eb7d264fSStephen Boyd * @reg: register address to adjust divider 516eb7d264fSStephen Boyd * @shift: number of bits to shift the bitfield 517eb7d264fSStephen Boyd * @width: width of the bitfield 518eb7d264fSStephen Boyd * @clk_divider_flags: divider-specific flags for this clock 519eb7d264fSStephen Boyd * @lock: shared register lock for this clock 520eb7d264fSStephen Boyd */ 521eb7d264fSStephen Boyd struct clk_hw *clk_hw_register_divider(struct device *dev, const char *name, 522eb7d264fSStephen Boyd const char *parent_name, unsigned long flags, 523eb7d264fSStephen Boyd void __iomem *reg, u8 shift, u8 width, 524eb7d264fSStephen Boyd u8 clk_divider_flags, spinlock_t *lock) 525eb7d264fSStephen Boyd { 526357c3f0aSRajendra Nayak return _register_divider(dev, name, parent_name, flags, reg, shift, 527357c3f0aSRajendra Nayak width, clk_divider_flags, NULL, lock); 528357c3f0aSRajendra Nayak } 529eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_hw_register_divider); 530357c3f0aSRajendra Nayak 531357c3f0aSRajendra Nayak /** 532357c3f0aSRajendra Nayak * clk_register_divider_table - register a table based divider clock with 533357c3f0aSRajendra Nayak * the clock framework 534357c3f0aSRajendra Nayak * @dev: device registering this clock 535357c3f0aSRajendra Nayak * @name: name of this clock 536357c3f0aSRajendra Nayak * @parent_name: name of clock's parent 537357c3f0aSRajendra Nayak * @flags: framework-specific flags 538357c3f0aSRajendra Nayak * @reg: register address to adjust divider 539357c3f0aSRajendra Nayak * @shift: number of bits to shift the bitfield 540357c3f0aSRajendra Nayak * @width: width of the bitfield 541357c3f0aSRajendra Nayak * @clk_divider_flags: divider-specific flags for this clock 542357c3f0aSRajendra Nayak * @table: array of divider/value pairs ending with a div set to 0 543357c3f0aSRajendra Nayak * @lock: shared register lock for this clock 544357c3f0aSRajendra Nayak */ 545357c3f0aSRajendra Nayak struct clk *clk_register_divider_table(struct device *dev, const char *name, 546357c3f0aSRajendra Nayak const char *parent_name, unsigned long flags, 547357c3f0aSRajendra Nayak void __iomem *reg, u8 shift, u8 width, 548357c3f0aSRajendra Nayak u8 clk_divider_flags, const struct clk_div_table *table, 549357c3f0aSRajendra Nayak spinlock_t *lock) 550357c3f0aSRajendra Nayak { 551eb7d264fSStephen Boyd struct clk_hw *hw; 552eb7d264fSStephen Boyd 553eb7d264fSStephen Boyd hw = _register_divider(dev, name, parent_name, flags, reg, shift, 554eb7d264fSStephen Boyd width, clk_divider_flags, table, lock); 555eb7d264fSStephen Boyd if (IS_ERR(hw)) 556eb7d264fSStephen Boyd return ERR_CAST(hw); 557eb7d264fSStephen Boyd return hw->clk; 558eb7d264fSStephen Boyd } 559eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_register_divider_table); 560eb7d264fSStephen Boyd 561eb7d264fSStephen Boyd /** 562eb7d264fSStephen Boyd * clk_hw_register_divider_table - register a table based divider clock with 563eb7d264fSStephen Boyd * the clock framework 564eb7d264fSStephen Boyd * @dev: device registering this clock 565eb7d264fSStephen Boyd * @name: name of this clock 566eb7d264fSStephen Boyd * @parent_name: name of clock's parent 567eb7d264fSStephen Boyd * @flags: framework-specific flags 568eb7d264fSStephen Boyd * @reg: register address to adjust divider 569eb7d264fSStephen Boyd * @shift: number of bits to shift the bitfield 570eb7d264fSStephen Boyd * @width: width of the bitfield 571eb7d264fSStephen Boyd * @clk_divider_flags: divider-specific flags for this clock 572eb7d264fSStephen Boyd * @table: array of divider/value pairs ending with a div set to 0 573eb7d264fSStephen Boyd * @lock: shared register lock for this clock 574eb7d264fSStephen Boyd */ 575eb7d264fSStephen Boyd struct clk_hw *clk_hw_register_divider_table(struct device *dev, 576eb7d264fSStephen Boyd const char *name, const char *parent_name, unsigned long flags, 577eb7d264fSStephen Boyd void __iomem *reg, u8 shift, u8 width, 578eb7d264fSStephen Boyd u8 clk_divider_flags, const struct clk_div_table *table, 579eb7d264fSStephen Boyd spinlock_t *lock) 580eb7d264fSStephen Boyd { 581357c3f0aSRajendra Nayak return _register_divider(dev, name, parent_name, flags, reg, shift, 582357c3f0aSRajendra Nayak width, clk_divider_flags, table, lock); 583357c3f0aSRajendra Nayak } 584eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_hw_register_divider_table); 5854e3c021fSKrzysztof Kozlowski 5864e3c021fSKrzysztof Kozlowski void clk_unregister_divider(struct clk *clk) 5874e3c021fSKrzysztof Kozlowski { 5884e3c021fSKrzysztof Kozlowski struct clk_divider *div; 5894e3c021fSKrzysztof Kozlowski struct clk_hw *hw; 5904e3c021fSKrzysztof Kozlowski 5914e3c021fSKrzysztof Kozlowski hw = __clk_get_hw(clk); 5924e3c021fSKrzysztof Kozlowski if (!hw) 5934e3c021fSKrzysztof Kozlowski return; 5944e3c021fSKrzysztof Kozlowski 5954e3c021fSKrzysztof Kozlowski div = to_clk_divider(hw); 5964e3c021fSKrzysztof Kozlowski 5974e3c021fSKrzysztof Kozlowski clk_unregister(clk); 5984e3c021fSKrzysztof Kozlowski kfree(div); 5994e3c021fSKrzysztof Kozlowski } 6004e3c021fSKrzysztof Kozlowski EXPORT_SYMBOL_GPL(clk_unregister_divider); 601eb7d264fSStephen Boyd 602eb7d264fSStephen Boyd /** 603eb7d264fSStephen Boyd * clk_hw_unregister_divider - unregister a clk divider 604eb7d264fSStephen Boyd * @hw: hardware-specific clock data to unregister 605eb7d264fSStephen Boyd */ 606eb7d264fSStephen Boyd void clk_hw_unregister_divider(struct clk_hw *hw) 607eb7d264fSStephen Boyd { 608eb7d264fSStephen Boyd struct clk_divider *div; 609eb7d264fSStephen Boyd 610eb7d264fSStephen Boyd div = to_clk_divider(hw); 611eb7d264fSStephen Boyd 612eb7d264fSStephen Boyd clk_hw_unregister(hw); 613eb7d264fSStephen Boyd kfree(div); 614eb7d264fSStephen Boyd } 615eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_hw_unregister_divider); 616