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 27b11d282dSTomi Valkeinen * rate - rate is adjustable. clk->rate = DIV_ROUND_UP(parent->rate / divisor) 289d9f78edSMike Turquette * parent - fixed parent. No clk_set_parent support 299d9f78edSMike Turquette */ 309d9f78edSMike Turquette 319d9f78edSMike Turquette #define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw) 329d9f78edSMike Turquette 33bca9690bSStephen Boyd #define div_mask(width) ((1 << (width)) - 1) 346d9252bdSRajendra Nayak 35357c3f0aSRajendra Nayak static unsigned int _get_table_maxdiv(const struct clk_div_table *table) 36357c3f0aSRajendra Nayak { 37357c3f0aSRajendra Nayak unsigned int maxdiv = 0; 38357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 39357c3f0aSRajendra Nayak 40357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 41357c3f0aSRajendra Nayak if (clkt->div > maxdiv) 42357c3f0aSRajendra Nayak maxdiv = clkt->div; 43357c3f0aSRajendra Nayak return maxdiv; 44357c3f0aSRajendra Nayak } 45357c3f0aSRajendra Nayak 46774b5143SMaxime COQUELIN static unsigned int _get_table_mindiv(const struct clk_div_table *table) 47774b5143SMaxime COQUELIN { 48774b5143SMaxime COQUELIN unsigned int mindiv = UINT_MAX; 49774b5143SMaxime COQUELIN const struct clk_div_table *clkt; 50774b5143SMaxime COQUELIN 51774b5143SMaxime COQUELIN for (clkt = table; clkt->div; clkt++) 52774b5143SMaxime COQUELIN if (clkt->div < mindiv) 53774b5143SMaxime COQUELIN mindiv = clkt->div; 54774b5143SMaxime COQUELIN return mindiv; 55774b5143SMaxime COQUELIN } 56774b5143SMaxime COQUELIN 57bca9690bSStephen Boyd static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width, 58bca9690bSStephen Boyd unsigned long flags) 596d9252bdSRajendra Nayak { 60bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ONE_BASED) 61bca9690bSStephen Boyd return div_mask(width); 62bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 63bca9690bSStephen Boyd return 1 << div_mask(width); 64bca9690bSStephen Boyd if (table) 65bca9690bSStephen Boyd return _get_table_maxdiv(table); 66bca9690bSStephen Boyd return div_mask(width) + 1; 676d9252bdSRajendra Nayak } 686d9252bdSRajendra Nayak 69357c3f0aSRajendra Nayak static unsigned int _get_table_div(const struct clk_div_table *table, 70357c3f0aSRajendra Nayak unsigned int val) 71357c3f0aSRajendra Nayak { 72357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 73357c3f0aSRajendra Nayak 74357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 75357c3f0aSRajendra Nayak if (clkt->val == val) 76357c3f0aSRajendra Nayak return clkt->div; 77357c3f0aSRajendra Nayak return 0; 78357c3f0aSRajendra Nayak } 79357c3f0aSRajendra Nayak 80bca9690bSStephen Boyd static unsigned int _get_div(const struct clk_div_table *table, 81bca9690bSStephen Boyd unsigned int val, unsigned long flags) 826d9252bdSRajendra Nayak { 83bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ONE_BASED) 846d9252bdSRajendra Nayak return val; 85bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 866d9252bdSRajendra Nayak return 1 << val; 87bca9690bSStephen Boyd if (table) 88bca9690bSStephen Boyd return _get_table_div(table, val); 896d9252bdSRajendra Nayak return val + 1; 906d9252bdSRajendra Nayak } 916d9252bdSRajendra Nayak 92357c3f0aSRajendra Nayak static unsigned int _get_table_val(const struct clk_div_table *table, 93357c3f0aSRajendra Nayak unsigned int div) 94357c3f0aSRajendra Nayak { 95357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 96357c3f0aSRajendra Nayak 97357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 98357c3f0aSRajendra Nayak if (clkt->div == div) 99357c3f0aSRajendra Nayak return clkt->val; 100357c3f0aSRajendra Nayak return 0; 101357c3f0aSRajendra Nayak } 102357c3f0aSRajendra Nayak 103bca9690bSStephen Boyd static unsigned int _get_val(const struct clk_div_table *table, 104bca9690bSStephen Boyd unsigned int div, unsigned long flags) 1056d9252bdSRajendra Nayak { 106bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ONE_BASED) 1076d9252bdSRajendra Nayak return div; 108bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 1096d9252bdSRajendra Nayak return __ffs(div); 110bca9690bSStephen Boyd if (table) 111bca9690bSStephen Boyd return _get_table_val(table, div); 1126d9252bdSRajendra Nayak return div - 1; 1136d9252bdSRajendra Nayak } 1149d9f78edSMike Turquette 115bca9690bSStephen Boyd unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate, 116bca9690bSStephen Boyd unsigned int val, 117bca9690bSStephen Boyd const struct clk_div_table *table, 118bca9690bSStephen Boyd unsigned long flags) 1199d9f78edSMike Turquette { 120bca9690bSStephen Boyd unsigned int div; 1219d9f78edSMike Turquette 122bca9690bSStephen Boyd div = _get_div(table, val, flags); 1236d9252bdSRajendra Nayak if (!div) { 124bca9690bSStephen Boyd WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO), 125056b2053SSoren Brinkmann "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n", 1266d9252bdSRajendra Nayak __clk_get_name(hw->clk)); 1276d9252bdSRajendra Nayak return parent_rate; 1286d9252bdSRajendra Nayak } 1299d9f78edSMike Turquette 130b11d282dSTomi Valkeinen return DIV_ROUND_UP(parent_rate, div); 1319d9f78edSMike Turquette } 132bca9690bSStephen Boyd EXPORT_SYMBOL_GPL(divider_recalc_rate); 133bca9690bSStephen Boyd 134bca9690bSStephen Boyd static unsigned long clk_divider_recalc_rate(struct clk_hw *hw, 135bca9690bSStephen Boyd unsigned long parent_rate) 136bca9690bSStephen Boyd { 137bca9690bSStephen Boyd struct clk_divider *divider = to_clk_divider(hw); 138bca9690bSStephen Boyd unsigned int val; 139bca9690bSStephen Boyd 140bca9690bSStephen Boyd val = clk_readl(divider->reg) >> divider->shift; 141bca9690bSStephen Boyd val &= div_mask(divider->width); 142bca9690bSStephen Boyd 143bca9690bSStephen Boyd return divider_recalc_rate(hw, parent_rate, val, divider->table, 144bca9690bSStephen Boyd divider->flags); 145bca9690bSStephen Boyd } 1469d9f78edSMike Turquette 147357c3f0aSRajendra Nayak static bool _is_valid_table_div(const struct clk_div_table *table, 148357c3f0aSRajendra Nayak unsigned int div) 149357c3f0aSRajendra Nayak { 150357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 151357c3f0aSRajendra Nayak 152357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 153357c3f0aSRajendra Nayak if (clkt->div == div) 154357c3f0aSRajendra Nayak return true; 155357c3f0aSRajendra Nayak return false; 156357c3f0aSRajendra Nayak } 157357c3f0aSRajendra Nayak 158bca9690bSStephen Boyd static bool _is_valid_div(const struct clk_div_table *table, unsigned int div, 159bca9690bSStephen Boyd unsigned long flags) 160357c3f0aSRajendra Nayak { 161bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 1621a3cd184SJames Hogan return is_power_of_2(div); 163bca9690bSStephen Boyd if (table) 164bca9690bSStephen Boyd return _is_valid_table_div(table, div); 165357c3f0aSRajendra Nayak return true; 166357c3f0aSRajendra Nayak } 167357c3f0aSRajendra Nayak 168dd23c2cdSMaxime COQUELIN static int _round_up_table(const struct clk_div_table *table, int div) 169dd23c2cdSMaxime COQUELIN { 170dd23c2cdSMaxime COQUELIN const struct clk_div_table *clkt; 171fe52e750SMaxime COQUELIN int up = INT_MAX; 172dd23c2cdSMaxime COQUELIN 173dd23c2cdSMaxime COQUELIN for (clkt = table; clkt->div; clkt++) { 174dd23c2cdSMaxime COQUELIN if (clkt->div == div) 175dd23c2cdSMaxime COQUELIN return clkt->div; 176dd23c2cdSMaxime COQUELIN else if (clkt->div < div) 177dd23c2cdSMaxime COQUELIN continue; 178dd23c2cdSMaxime COQUELIN 179dd23c2cdSMaxime COQUELIN if ((clkt->div - div) < (up - div)) 180dd23c2cdSMaxime COQUELIN up = clkt->div; 181dd23c2cdSMaxime COQUELIN } 182dd23c2cdSMaxime COQUELIN 183dd23c2cdSMaxime COQUELIN return up; 184dd23c2cdSMaxime COQUELIN } 185dd23c2cdSMaxime COQUELIN 186774b5143SMaxime COQUELIN static int _round_down_table(const struct clk_div_table *table, int div) 187774b5143SMaxime COQUELIN { 188774b5143SMaxime COQUELIN const struct clk_div_table *clkt; 189774b5143SMaxime COQUELIN int down = _get_table_mindiv(table); 190774b5143SMaxime COQUELIN 191774b5143SMaxime COQUELIN for (clkt = table; clkt->div; clkt++) { 192774b5143SMaxime COQUELIN if (clkt->div == div) 193774b5143SMaxime COQUELIN return clkt->div; 194774b5143SMaxime COQUELIN else if (clkt->div > div) 195774b5143SMaxime COQUELIN continue; 196774b5143SMaxime COQUELIN 197774b5143SMaxime COQUELIN if ((div - clkt->div) < (div - down)) 198774b5143SMaxime COQUELIN down = clkt->div; 199774b5143SMaxime COQUELIN } 200774b5143SMaxime COQUELIN 201774b5143SMaxime COQUELIN return down; 202774b5143SMaxime COQUELIN } 203774b5143SMaxime COQUELIN 204bca9690bSStephen Boyd static int _div_round_up(const struct clk_div_table *table, 205bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 206bca9690bSStephen Boyd unsigned long flags) 207dd23c2cdSMaxime COQUELIN { 208dd23c2cdSMaxime COQUELIN int div = DIV_ROUND_UP(parent_rate, rate); 209dd23c2cdSMaxime COQUELIN 210bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 211dd23c2cdSMaxime COQUELIN div = __roundup_pow_of_two(div); 212bca9690bSStephen Boyd if (table) 213bca9690bSStephen Boyd div = _round_up_table(table, div); 214dd23c2cdSMaxime COQUELIN 215dd23c2cdSMaxime COQUELIN return div; 216dd23c2cdSMaxime COQUELIN } 217dd23c2cdSMaxime COQUELIN 218bca9690bSStephen Boyd static int _div_round_closest(const struct clk_div_table *table, 219bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 220bca9690bSStephen Boyd unsigned long flags) 221774b5143SMaxime COQUELIN { 222774b5143SMaxime COQUELIN int up, down, div; 223774b5143SMaxime COQUELIN 224774b5143SMaxime COQUELIN up = down = div = DIV_ROUND_CLOSEST(parent_rate, rate); 225774b5143SMaxime COQUELIN 226bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) { 227774b5143SMaxime COQUELIN up = __roundup_pow_of_two(div); 228774b5143SMaxime COQUELIN down = __rounddown_pow_of_two(div); 229bca9690bSStephen Boyd } else if (table) { 230bca9690bSStephen Boyd up = _round_up_table(table, div); 231bca9690bSStephen Boyd down = _round_down_table(table, div); 232774b5143SMaxime COQUELIN } 233774b5143SMaxime COQUELIN 234774b5143SMaxime COQUELIN return (up - div) <= (div - down) ? up : down; 235774b5143SMaxime COQUELIN } 236774b5143SMaxime COQUELIN 237bca9690bSStephen Boyd static int _div_round(const struct clk_div_table *table, 238bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 239bca9690bSStephen Boyd unsigned long flags) 240774b5143SMaxime COQUELIN { 241bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ROUND_CLOSEST) 242bca9690bSStephen Boyd return _div_round_closest(table, parent_rate, rate, flags); 243774b5143SMaxime COQUELIN 244bca9690bSStephen Boyd return _div_round_up(table, parent_rate, rate, flags); 245774b5143SMaxime COQUELIN } 246774b5143SMaxime COQUELIN 247bca9690bSStephen Boyd static bool _is_best_div(unsigned long rate, unsigned long now, 248bca9690bSStephen Boyd unsigned long best, unsigned long flags) 249774b5143SMaxime COQUELIN { 250bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ROUND_CLOSEST) 251774b5143SMaxime COQUELIN return abs(rate - now) < abs(rate - best); 252774b5143SMaxime COQUELIN 253774b5143SMaxime COQUELIN return now <= rate && now > best; 254774b5143SMaxime COQUELIN } 255774b5143SMaxime COQUELIN 256bca9690bSStephen Boyd static int _next_div(const struct clk_div_table *table, int div, 257bca9690bSStephen Boyd unsigned long flags) 2580e2de78eSMaxime COQUELIN { 2590e2de78eSMaxime COQUELIN div++; 2600e2de78eSMaxime COQUELIN 261bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 2620e2de78eSMaxime COQUELIN return __roundup_pow_of_two(div); 263bca9690bSStephen Boyd if (table) 264bca9690bSStephen Boyd return _round_up_table(table, div); 2650e2de78eSMaxime COQUELIN 2660e2de78eSMaxime COQUELIN return div; 2670e2de78eSMaxime COQUELIN } 2680e2de78eSMaxime COQUELIN 2699d9f78edSMike Turquette static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate, 270bca9690bSStephen Boyd unsigned long *best_parent_rate, 271bca9690bSStephen Boyd const struct clk_div_table *table, u8 width, 272bca9690bSStephen Boyd unsigned long flags) 2739d9f78edSMike Turquette { 2749d9f78edSMike Turquette int i, bestdiv = 0; 2759d9f78edSMike Turquette unsigned long parent_rate, best = 0, now, maxdiv; 276081c9025SShawn Guo unsigned long parent_rate_saved = *best_parent_rate; 2779d9f78edSMike Turquette 2789d9f78edSMike Turquette if (!rate) 2799d9f78edSMike Turquette rate = 1; 2809d9f78edSMike Turquette 281bca9690bSStephen Boyd maxdiv = _get_maxdiv(table, width, flags); 2829d9f78edSMike Turquette 28381536e07SShawn Guo if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) { 28481536e07SShawn Guo parent_rate = *best_parent_rate; 285bca9690bSStephen Boyd bestdiv = _div_round(table, parent_rate, rate, flags); 2869d9f78edSMike Turquette bestdiv = bestdiv == 0 ? 1 : bestdiv; 2879d9f78edSMike Turquette bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv; 2889d9f78edSMike Turquette return bestdiv; 2899d9f78edSMike Turquette } 2909d9f78edSMike Turquette 2919d9f78edSMike Turquette /* 2929d9f78edSMike Turquette * The maximum divider we can use without overflowing 2939d9f78edSMike Turquette * unsigned long in rate * i below 2949d9f78edSMike Turquette */ 2959d9f78edSMike Turquette maxdiv = min(ULONG_MAX / rate, maxdiv); 2969d9f78edSMike Turquette 297bca9690bSStephen Boyd for (i = 1; i <= maxdiv; i = _next_div(table, i, flags)) { 298bca9690bSStephen Boyd if (!_is_valid_div(table, i, flags)) 2996d9252bdSRajendra Nayak continue; 300081c9025SShawn Guo if (rate * i == parent_rate_saved) { 301081c9025SShawn Guo /* 302081c9025SShawn Guo * It's the most ideal case if the requested rate can be 303081c9025SShawn Guo * divided from parent clock without needing to change 304081c9025SShawn Guo * parent rate, so return the divider immediately. 305081c9025SShawn Guo */ 306081c9025SShawn Guo *best_parent_rate = parent_rate_saved; 307081c9025SShawn Guo return i; 308081c9025SShawn Guo } 3099d9f78edSMike Turquette parent_rate = __clk_round_rate(__clk_get_parent(hw->clk), 310*da321133SUwe Kleine-König rate * i); 311b11d282dSTomi Valkeinen now = DIV_ROUND_UP(parent_rate, i); 312bca9690bSStephen Boyd if (_is_best_div(rate, now, best, flags)) { 3139d9f78edSMike Turquette bestdiv = i; 3149d9f78edSMike Turquette best = now; 3159d9f78edSMike Turquette *best_parent_rate = parent_rate; 3169d9f78edSMike Turquette } 3179d9f78edSMike Turquette } 3189d9f78edSMike Turquette 3199d9f78edSMike Turquette if (!bestdiv) { 320bca9690bSStephen Boyd bestdiv = _get_maxdiv(table, width, flags); 3219d9f78edSMike Turquette *best_parent_rate = __clk_round_rate(__clk_get_parent(hw->clk), 1); 3229d9f78edSMike Turquette } 3239d9f78edSMike Turquette 3249d9f78edSMike Turquette return bestdiv; 3259d9f78edSMike Turquette } 3269d9f78edSMike Turquette 327bca9690bSStephen Boyd long divider_round_rate(struct clk_hw *hw, unsigned long rate, 328bca9690bSStephen Boyd unsigned long *prate, const struct clk_div_table *table, 329bca9690bSStephen Boyd u8 width, unsigned long flags) 3309d9f78edSMike Turquette { 3319d9f78edSMike Turquette int div; 332bca9690bSStephen Boyd 333bca9690bSStephen Boyd div = clk_divider_bestdiv(hw, rate, prate, table, width, flags); 3349d9f78edSMike Turquette 335b11d282dSTomi Valkeinen return DIV_ROUND_UP(*prate, div); 3369d9f78edSMike Turquette } 337bca9690bSStephen Boyd EXPORT_SYMBOL_GPL(divider_round_rate); 338bca9690bSStephen Boyd 339bca9690bSStephen Boyd static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate, 340bca9690bSStephen Boyd unsigned long *prate) 341bca9690bSStephen Boyd { 342bca9690bSStephen Boyd struct clk_divider *divider = to_clk_divider(hw); 343bca9690bSStephen Boyd int bestdiv; 344bca9690bSStephen Boyd 345bca9690bSStephen Boyd /* if read only, just return current value */ 346bca9690bSStephen Boyd if (divider->flags & CLK_DIVIDER_READ_ONLY) { 347bca9690bSStephen Boyd bestdiv = readl(divider->reg) >> divider->shift; 348bca9690bSStephen Boyd bestdiv &= div_mask(divider->width); 349bca9690bSStephen Boyd bestdiv = _get_div(divider->table, bestdiv, divider->flags); 3502f7bf4afSHeiko Stübner return DIV_ROUND_UP(*prate, bestdiv); 351bca9690bSStephen Boyd } 352bca9690bSStephen Boyd 353bca9690bSStephen Boyd return divider_round_rate(hw, rate, prate, divider->table, 354bca9690bSStephen Boyd divider->width, divider->flags); 355bca9690bSStephen Boyd } 356bca9690bSStephen Boyd 357bca9690bSStephen Boyd int divider_get_val(unsigned long rate, unsigned long parent_rate, 358bca9690bSStephen Boyd const struct clk_div_table *table, u8 width, 359bca9690bSStephen Boyd unsigned long flags) 360bca9690bSStephen Boyd { 361bca9690bSStephen Boyd unsigned int div, value; 362bca9690bSStephen Boyd 363bca9690bSStephen Boyd div = DIV_ROUND_UP(parent_rate, rate); 364bca9690bSStephen Boyd 365bca9690bSStephen Boyd if (!_is_valid_div(table, div, flags)) 366bca9690bSStephen Boyd return -EINVAL; 367bca9690bSStephen Boyd 368bca9690bSStephen Boyd value = _get_val(table, div, flags); 369bca9690bSStephen Boyd 370bca9690bSStephen Boyd return min_t(unsigned int, value, div_mask(width)); 371bca9690bSStephen Boyd } 372bca9690bSStephen Boyd EXPORT_SYMBOL_GPL(divider_get_val); 3739d9f78edSMike Turquette 3741c0035d7SShawn Guo static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, 3751c0035d7SShawn Guo unsigned long parent_rate) 3769d9f78edSMike Turquette { 3779d9f78edSMike Turquette struct clk_divider *divider = to_clk_divider(hw); 378bca9690bSStephen Boyd unsigned int value; 3799d9f78edSMike Turquette unsigned long flags = 0; 3809d9f78edSMike Turquette u32 val; 3819d9f78edSMike Turquette 382bca9690bSStephen Boyd value = divider_get_val(rate, parent_rate, divider->table, 383bca9690bSStephen Boyd divider->width, divider->flags); 3849d9f78edSMike Turquette 3859d9f78edSMike Turquette if (divider->lock) 3869d9f78edSMike Turquette spin_lock_irqsave(divider->lock, flags); 3879d9f78edSMike Turquette 388d57dfe75SHaojian Zhuang if (divider->flags & CLK_DIVIDER_HIWORD_MASK) { 389bca9690bSStephen Boyd val = div_mask(divider->width) << (divider->shift + 16); 390d57dfe75SHaojian Zhuang } else { 391aa514ce3SGerhard Sittig val = clk_readl(divider->reg); 392bca9690bSStephen Boyd val &= ~(div_mask(divider->width) << divider->shift); 393d57dfe75SHaojian Zhuang } 3946d9252bdSRajendra Nayak val |= value << divider->shift; 395aa514ce3SGerhard Sittig clk_writel(val, divider->reg); 3969d9f78edSMike Turquette 3979d9f78edSMike Turquette if (divider->lock) 3989d9f78edSMike Turquette spin_unlock_irqrestore(divider->lock, flags); 3999d9f78edSMike Turquette 4009d9f78edSMike Turquette return 0; 4019d9f78edSMike Turquette } 4029d9f78edSMike Turquette 403822c250eSShawn Guo const struct clk_ops clk_divider_ops = { 4049d9f78edSMike Turquette .recalc_rate = clk_divider_recalc_rate, 4059d9f78edSMike Turquette .round_rate = clk_divider_round_rate, 4069d9f78edSMike Turquette .set_rate = clk_divider_set_rate, 4079d9f78edSMike Turquette }; 4089d9f78edSMike Turquette EXPORT_SYMBOL_GPL(clk_divider_ops); 4099d9f78edSMike Turquette 410357c3f0aSRajendra Nayak static struct clk *_register_divider(struct device *dev, const char *name, 4119d9f78edSMike Turquette const char *parent_name, unsigned long flags, 4129d9f78edSMike Turquette void __iomem *reg, u8 shift, u8 width, 413357c3f0aSRajendra Nayak u8 clk_divider_flags, const struct clk_div_table *table, 414357c3f0aSRajendra Nayak spinlock_t *lock) 4159d9f78edSMike Turquette { 4169d9f78edSMike Turquette struct clk_divider *div; 4179d9f78edSMike Turquette struct clk *clk; 4180197b3eaSSaravana Kannan struct clk_init_data init; 4199d9f78edSMike Turquette 420d57dfe75SHaojian Zhuang if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) { 421d57dfe75SHaojian Zhuang if (width + shift > 16) { 422d57dfe75SHaojian Zhuang pr_warn("divider value exceeds LOWORD field\n"); 423d57dfe75SHaojian Zhuang return ERR_PTR(-EINVAL); 424d57dfe75SHaojian Zhuang } 425d57dfe75SHaojian Zhuang } 426d57dfe75SHaojian Zhuang 42727d54591SMike Turquette /* allocate the divider */ 4289d9f78edSMike Turquette div = kzalloc(sizeof(struct clk_divider), GFP_KERNEL); 4299d9f78edSMike Turquette if (!div) { 4309d9f78edSMike Turquette pr_err("%s: could not allocate divider clk\n", __func__); 43127d54591SMike Turquette return ERR_PTR(-ENOMEM); 4329d9f78edSMike Turquette } 4339d9f78edSMike Turquette 4340197b3eaSSaravana Kannan init.name = name; 4350197b3eaSSaravana Kannan init.ops = &clk_divider_ops; 436f7d8caadSRajendra Nayak init.flags = flags | CLK_IS_BASIC; 4370197b3eaSSaravana Kannan init.parent_names = (parent_name ? &parent_name: NULL); 4380197b3eaSSaravana Kannan init.num_parents = (parent_name ? 1 : 0); 4390197b3eaSSaravana Kannan 4409d9f78edSMike Turquette /* struct clk_divider assignments */ 4419d9f78edSMike Turquette div->reg = reg; 4429d9f78edSMike Turquette div->shift = shift; 4439d9f78edSMike Turquette div->width = width; 4449d9f78edSMike Turquette div->flags = clk_divider_flags; 4459d9f78edSMike Turquette div->lock = lock; 4460197b3eaSSaravana Kannan div->hw.init = &init; 447357c3f0aSRajendra Nayak div->table = table; 4489d9f78edSMike Turquette 44927d54591SMike Turquette /* register the clock */ 4500197b3eaSSaravana Kannan clk = clk_register(dev, &div->hw); 4519d9f78edSMike Turquette 45227d54591SMike Turquette if (IS_ERR(clk)) 4539d9f78edSMike Turquette kfree(div); 4549d9f78edSMike Turquette 45527d54591SMike Turquette return clk; 4569d9f78edSMike Turquette } 457357c3f0aSRajendra Nayak 458357c3f0aSRajendra Nayak /** 459357c3f0aSRajendra Nayak * clk_register_divider - register a divider clock with the clock framework 460357c3f0aSRajendra Nayak * @dev: device registering this clock 461357c3f0aSRajendra Nayak * @name: name of this clock 462357c3f0aSRajendra Nayak * @parent_name: name of clock's parent 463357c3f0aSRajendra Nayak * @flags: framework-specific flags 464357c3f0aSRajendra Nayak * @reg: register address to adjust divider 465357c3f0aSRajendra Nayak * @shift: number of bits to shift the bitfield 466357c3f0aSRajendra Nayak * @width: width of the bitfield 467357c3f0aSRajendra Nayak * @clk_divider_flags: divider-specific flags for this clock 468357c3f0aSRajendra Nayak * @lock: shared register lock for this clock 469357c3f0aSRajendra Nayak */ 470357c3f0aSRajendra Nayak struct clk *clk_register_divider(struct device *dev, const char *name, 471357c3f0aSRajendra Nayak const char *parent_name, unsigned long flags, 472357c3f0aSRajendra Nayak void __iomem *reg, u8 shift, u8 width, 473357c3f0aSRajendra Nayak u8 clk_divider_flags, spinlock_t *lock) 474357c3f0aSRajendra Nayak { 475357c3f0aSRajendra Nayak return _register_divider(dev, name, parent_name, flags, reg, shift, 476357c3f0aSRajendra Nayak width, clk_divider_flags, NULL, lock); 477357c3f0aSRajendra Nayak } 4784c5eeea9SFabio Estevam EXPORT_SYMBOL_GPL(clk_register_divider); 479357c3f0aSRajendra Nayak 480357c3f0aSRajendra Nayak /** 481357c3f0aSRajendra Nayak * clk_register_divider_table - register a table based divider clock with 482357c3f0aSRajendra Nayak * the clock framework 483357c3f0aSRajendra Nayak * @dev: device registering this clock 484357c3f0aSRajendra Nayak * @name: name of this clock 485357c3f0aSRajendra Nayak * @parent_name: name of clock's parent 486357c3f0aSRajendra Nayak * @flags: framework-specific flags 487357c3f0aSRajendra Nayak * @reg: register address to adjust divider 488357c3f0aSRajendra Nayak * @shift: number of bits to shift the bitfield 489357c3f0aSRajendra Nayak * @width: width of the bitfield 490357c3f0aSRajendra Nayak * @clk_divider_flags: divider-specific flags for this clock 491357c3f0aSRajendra Nayak * @table: array of divider/value pairs ending with a div set to 0 492357c3f0aSRajendra Nayak * @lock: shared register lock for this clock 493357c3f0aSRajendra Nayak */ 494357c3f0aSRajendra Nayak struct clk *clk_register_divider_table(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, const struct clk_div_table *table, 498357c3f0aSRajendra Nayak spinlock_t *lock) 499357c3f0aSRajendra Nayak { 500357c3f0aSRajendra Nayak return _register_divider(dev, name, parent_name, flags, reg, shift, 501357c3f0aSRajendra Nayak width, clk_divider_flags, table, lock); 502357c3f0aSRajendra Nayak } 5034c5eeea9SFabio Estevam EXPORT_SYMBOL_GPL(clk_register_divider_table); 5044e3c021fSKrzysztof Kozlowski 5054e3c021fSKrzysztof Kozlowski void clk_unregister_divider(struct clk *clk) 5064e3c021fSKrzysztof Kozlowski { 5074e3c021fSKrzysztof Kozlowski struct clk_divider *div; 5084e3c021fSKrzysztof Kozlowski struct clk_hw *hw; 5094e3c021fSKrzysztof Kozlowski 5104e3c021fSKrzysztof Kozlowski hw = __clk_get_hw(clk); 5114e3c021fSKrzysztof Kozlowski if (!hw) 5124e3c021fSKrzysztof Kozlowski return; 5134e3c021fSKrzysztof Kozlowski 5144e3c021fSKrzysztof Kozlowski div = to_clk_divider(hw); 5154e3c021fSKrzysztof Kozlowski 5164e3c021fSKrzysztof Kozlowski clk_unregister(clk); 5174e3c021fSKrzysztof Kozlowski kfree(div); 5184e3c021fSKrzysztof Kozlowski } 5194e3c021fSKrzysztof Kozlowski EXPORT_SYMBOL_GPL(clk_unregister_divider); 520