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 27*9556f9daSBrian 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 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, 81afe76c8fSJim Quinlan unsigned int val, unsigned long flags, u8 width) 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; 87afe76c8fSJim Quinlan if (flags & CLK_DIVIDER_MAX_AT_ZERO) 88afe76c8fSJim Quinlan return val ? val : div_mask(width) + 1; 89bca9690bSStephen Boyd if (table) 90bca9690bSStephen Boyd return _get_table_div(table, val); 916d9252bdSRajendra Nayak return val + 1; 926d9252bdSRajendra Nayak } 936d9252bdSRajendra Nayak 94357c3f0aSRajendra Nayak static unsigned int _get_table_val(const struct clk_div_table *table, 95357c3f0aSRajendra Nayak unsigned int div) 96357c3f0aSRajendra Nayak { 97357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 98357c3f0aSRajendra Nayak 99357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 100357c3f0aSRajendra Nayak if (clkt->div == div) 101357c3f0aSRajendra Nayak return clkt->val; 102357c3f0aSRajendra Nayak return 0; 103357c3f0aSRajendra Nayak } 104357c3f0aSRajendra Nayak 105bca9690bSStephen Boyd static unsigned int _get_val(const struct clk_div_table *table, 106afe76c8fSJim Quinlan unsigned int div, unsigned long flags, u8 width) 1076d9252bdSRajendra Nayak { 108bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ONE_BASED) 1096d9252bdSRajendra Nayak return div; 110bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 1116d9252bdSRajendra Nayak return __ffs(div); 112afe76c8fSJim Quinlan if (flags & CLK_DIVIDER_MAX_AT_ZERO) 113afe76c8fSJim Quinlan return (div == div_mask(width) + 1) ? 0 : div; 114bca9690bSStephen Boyd if (table) 115bca9690bSStephen Boyd return _get_table_val(table, div); 1166d9252bdSRajendra Nayak return div - 1; 1176d9252bdSRajendra Nayak } 1189d9f78edSMike Turquette 119bca9690bSStephen Boyd unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate, 120bca9690bSStephen Boyd unsigned int val, 121bca9690bSStephen Boyd const struct clk_div_table *table, 122bca9690bSStephen Boyd unsigned long flags) 1239d9f78edSMike Turquette { 124afe76c8fSJim Quinlan struct clk_divider *divider = to_clk_divider(hw); 125bca9690bSStephen Boyd unsigned int div; 1269d9f78edSMike Turquette 127afe76c8fSJim Quinlan div = _get_div(table, val, flags, divider->width); 1286d9252bdSRajendra Nayak if (!div) { 129bca9690bSStephen Boyd WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO), 130056b2053SSoren Brinkmann "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n", 1312f508a95SStephen Boyd clk_hw_get_name(hw)); 1326d9252bdSRajendra Nayak return parent_rate; 1336d9252bdSRajendra Nayak } 1349d9f78edSMike Turquette 135*9556f9daSBrian Norris return DIV_ROUND_UP_ULL((u64)parent_rate, div); 1369d9f78edSMike Turquette } 137bca9690bSStephen Boyd EXPORT_SYMBOL_GPL(divider_recalc_rate); 138bca9690bSStephen Boyd 139bca9690bSStephen Boyd static unsigned long clk_divider_recalc_rate(struct clk_hw *hw, 140bca9690bSStephen Boyd unsigned long parent_rate) 141bca9690bSStephen Boyd { 142bca9690bSStephen Boyd struct clk_divider *divider = to_clk_divider(hw); 143bca9690bSStephen Boyd unsigned int val; 144bca9690bSStephen Boyd 145bca9690bSStephen Boyd val = clk_readl(divider->reg) >> divider->shift; 146bca9690bSStephen Boyd val &= div_mask(divider->width); 147bca9690bSStephen Boyd 148bca9690bSStephen Boyd return divider_recalc_rate(hw, parent_rate, val, divider->table, 149bca9690bSStephen Boyd divider->flags); 150bca9690bSStephen Boyd } 1519d9f78edSMike Turquette 152357c3f0aSRajendra Nayak static bool _is_valid_table_div(const struct clk_div_table *table, 153357c3f0aSRajendra Nayak unsigned int div) 154357c3f0aSRajendra Nayak { 155357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 156357c3f0aSRajendra Nayak 157357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 158357c3f0aSRajendra Nayak if (clkt->div == div) 159357c3f0aSRajendra Nayak return true; 160357c3f0aSRajendra Nayak return false; 161357c3f0aSRajendra Nayak } 162357c3f0aSRajendra Nayak 163bca9690bSStephen Boyd static bool _is_valid_div(const struct clk_div_table *table, unsigned int div, 164bca9690bSStephen Boyd unsigned long flags) 165357c3f0aSRajendra Nayak { 166bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 1671a3cd184SJames Hogan return is_power_of_2(div); 168bca9690bSStephen Boyd if (table) 169bca9690bSStephen Boyd return _is_valid_table_div(table, div); 170357c3f0aSRajendra Nayak return true; 171357c3f0aSRajendra Nayak } 172357c3f0aSRajendra Nayak 173dd23c2cdSMaxime COQUELIN static int _round_up_table(const struct clk_div_table *table, int div) 174dd23c2cdSMaxime COQUELIN { 175dd23c2cdSMaxime COQUELIN const struct clk_div_table *clkt; 176fe52e750SMaxime COQUELIN int up = INT_MAX; 177dd23c2cdSMaxime COQUELIN 178dd23c2cdSMaxime COQUELIN for (clkt = table; clkt->div; clkt++) { 179dd23c2cdSMaxime COQUELIN if (clkt->div == div) 180dd23c2cdSMaxime COQUELIN return clkt->div; 181dd23c2cdSMaxime COQUELIN else if (clkt->div < div) 182dd23c2cdSMaxime COQUELIN continue; 183dd23c2cdSMaxime COQUELIN 184dd23c2cdSMaxime COQUELIN if ((clkt->div - div) < (up - div)) 185dd23c2cdSMaxime COQUELIN up = clkt->div; 186dd23c2cdSMaxime COQUELIN } 187dd23c2cdSMaxime COQUELIN 188dd23c2cdSMaxime COQUELIN return up; 189dd23c2cdSMaxime COQUELIN } 190dd23c2cdSMaxime COQUELIN 191774b5143SMaxime COQUELIN static int _round_down_table(const struct clk_div_table *table, int div) 192774b5143SMaxime COQUELIN { 193774b5143SMaxime COQUELIN const struct clk_div_table *clkt; 194774b5143SMaxime COQUELIN int down = _get_table_mindiv(table); 195774b5143SMaxime COQUELIN 196774b5143SMaxime COQUELIN for (clkt = table; clkt->div; clkt++) { 197774b5143SMaxime COQUELIN if (clkt->div == div) 198774b5143SMaxime COQUELIN return clkt->div; 199774b5143SMaxime COQUELIN else if (clkt->div > div) 200774b5143SMaxime COQUELIN continue; 201774b5143SMaxime COQUELIN 202774b5143SMaxime COQUELIN if ((div - clkt->div) < (div - down)) 203774b5143SMaxime COQUELIN down = clkt->div; 204774b5143SMaxime COQUELIN } 205774b5143SMaxime COQUELIN 206774b5143SMaxime COQUELIN return down; 207774b5143SMaxime COQUELIN } 208774b5143SMaxime COQUELIN 209bca9690bSStephen Boyd static int _div_round_up(const struct clk_div_table *table, 210bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 211bca9690bSStephen Boyd unsigned long flags) 212dd23c2cdSMaxime COQUELIN { 213*9556f9daSBrian Norris int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 214dd23c2cdSMaxime COQUELIN 215bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 216dd23c2cdSMaxime COQUELIN div = __roundup_pow_of_two(div); 217bca9690bSStephen Boyd if (table) 218bca9690bSStephen Boyd div = _round_up_table(table, div); 219dd23c2cdSMaxime COQUELIN 220dd23c2cdSMaxime COQUELIN return div; 221dd23c2cdSMaxime COQUELIN } 222dd23c2cdSMaxime COQUELIN 223bca9690bSStephen Boyd static int _div_round_closest(const struct clk_div_table *table, 224bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 225bca9690bSStephen Boyd unsigned long flags) 226774b5143SMaxime COQUELIN { 22793155142SUwe Kleine-König int up, down; 22826bac95aSUwe Kleine-König unsigned long up_rate, down_rate; 229774b5143SMaxime COQUELIN 230*9556f9daSBrian Norris up = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 23193155142SUwe Kleine-König down = parent_rate / rate; 232774b5143SMaxime COQUELIN 233bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) { 23493155142SUwe Kleine-König up = __roundup_pow_of_two(up); 23593155142SUwe Kleine-König down = __rounddown_pow_of_two(down); 236bca9690bSStephen Boyd } else if (table) { 23793155142SUwe Kleine-König up = _round_up_table(table, up); 23893155142SUwe Kleine-König down = _round_down_table(table, down); 239774b5143SMaxime COQUELIN } 240774b5143SMaxime COQUELIN 241*9556f9daSBrian Norris up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up); 242*9556f9daSBrian Norris down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down); 24326bac95aSUwe Kleine-König 24426bac95aSUwe Kleine-König return (rate - up_rate) <= (down_rate - rate) ? up : down; 245774b5143SMaxime COQUELIN } 246774b5143SMaxime COQUELIN 247bca9690bSStephen Boyd static int _div_round(const struct clk_div_table *table, 248bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 249bca9690bSStephen Boyd unsigned long flags) 250774b5143SMaxime COQUELIN { 251bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ROUND_CLOSEST) 252bca9690bSStephen Boyd return _div_round_closest(table, parent_rate, rate, flags); 253774b5143SMaxime COQUELIN 254bca9690bSStephen Boyd return _div_round_up(table, parent_rate, rate, flags); 255774b5143SMaxime COQUELIN } 256774b5143SMaxime COQUELIN 257bca9690bSStephen Boyd static bool _is_best_div(unsigned long rate, unsigned long now, 258bca9690bSStephen Boyd unsigned long best, unsigned long flags) 259774b5143SMaxime COQUELIN { 260bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ROUND_CLOSEST) 261774b5143SMaxime COQUELIN return abs(rate - now) < abs(rate - best); 262774b5143SMaxime COQUELIN 263774b5143SMaxime COQUELIN return now <= rate && now > best; 264774b5143SMaxime COQUELIN } 265774b5143SMaxime COQUELIN 266bca9690bSStephen Boyd static int _next_div(const struct clk_div_table *table, int div, 267bca9690bSStephen Boyd unsigned long flags) 2680e2de78eSMaxime COQUELIN { 2690e2de78eSMaxime COQUELIN div++; 2700e2de78eSMaxime COQUELIN 271bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 2720e2de78eSMaxime COQUELIN return __roundup_pow_of_two(div); 273bca9690bSStephen Boyd if (table) 274bca9690bSStephen Boyd return _round_up_table(table, div); 2750e2de78eSMaxime COQUELIN 2760e2de78eSMaxime COQUELIN return div; 2770e2de78eSMaxime COQUELIN } 2780e2de78eSMaxime COQUELIN 2799d9f78edSMike Turquette static int clk_divider_bestdiv(struct clk_hw *hw, 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 307bca9690bSStephen Boyd for (i = 1; i <= maxdiv; i = _next_div(table, i, flags)) { 308bca9690bSStephen Boyd if (!_is_valid_div(table, i, flags)) 3096d9252bdSRajendra Nayak continue; 310081c9025SShawn Guo if (rate * i == parent_rate_saved) { 311081c9025SShawn Guo /* 312081c9025SShawn Guo * It's the most ideal case if the requested rate can be 313081c9025SShawn Guo * divided from parent clock without needing to change 314081c9025SShawn Guo * parent rate, so return the divider immediately. 315081c9025SShawn Guo */ 316081c9025SShawn Guo *best_parent_rate = parent_rate_saved; 317081c9025SShawn Guo return i; 318081c9025SShawn Guo } 3192f508a95SStephen Boyd parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw), 320da321133SUwe Kleine-König rate * i); 321*9556f9daSBrian Norris now = DIV_ROUND_UP_ULL((u64)parent_rate, i); 322bca9690bSStephen Boyd if (_is_best_div(rate, now, best, flags)) { 3239d9f78edSMike Turquette bestdiv = i; 3249d9f78edSMike Turquette best = now; 3259d9f78edSMike Turquette *best_parent_rate = parent_rate; 3269d9f78edSMike Turquette } 3279d9f78edSMike Turquette } 3289d9f78edSMike Turquette 3299d9f78edSMike Turquette if (!bestdiv) { 330bca9690bSStephen Boyd bestdiv = _get_maxdiv(table, width, flags); 3312f508a95SStephen Boyd *best_parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw), 1); 3329d9f78edSMike Turquette } 3339d9f78edSMike Turquette 3349d9f78edSMike Turquette return bestdiv; 3359d9f78edSMike Turquette } 3369d9f78edSMike Turquette 337bca9690bSStephen Boyd long divider_round_rate(struct clk_hw *hw, unsigned long rate, 338bca9690bSStephen Boyd unsigned long *prate, const struct clk_div_table *table, 339bca9690bSStephen Boyd u8 width, unsigned long flags) 3409d9f78edSMike Turquette { 3419d9f78edSMike Turquette int div; 342bca9690bSStephen Boyd 343bca9690bSStephen Boyd div = clk_divider_bestdiv(hw, rate, prate, table, width, flags); 3449d9f78edSMike Turquette 345*9556f9daSBrian Norris return DIV_ROUND_UP_ULL((u64)*prate, div); 3469d9f78edSMike Turquette } 347bca9690bSStephen Boyd EXPORT_SYMBOL_GPL(divider_round_rate); 348bca9690bSStephen Boyd 349bca9690bSStephen Boyd static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate, 350bca9690bSStephen Boyd unsigned long *prate) 351bca9690bSStephen Boyd { 352bca9690bSStephen Boyd struct clk_divider *divider = to_clk_divider(hw); 353bca9690bSStephen Boyd int bestdiv; 354bca9690bSStephen Boyd 355bca9690bSStephen Boyd /* if read only, just return current value */ 356bca9690bSStephen Boyd if (divider->flags & CLK_DIVIDER_READ_ONLY) { 357bca9690bSStephen Boyd bestdiv = readl(divider->reg) >> divider->shift; 358bca9690bSStephen Boyd bestdiv &= div_mask(divider->width); 359afe76c8fSJim Quinlan bestdiv = _get_div(divider->table, bestdiv, divider->flags, 360afe76c8fSJim Quinlan divider->width); 361*9556f9daSBrian Norris return DIV_ROUND_UP_ULL((u64)*prate, bestdiv); 362bca9690bSStephen Boyd } 363bca9690bSStephen Boyd 364bca9690bSStephen Boyd return divider_round_rate(hw, rate, prate, divider->table, 365bca9690bSStephen Boyd divider->width, divider->flags); 366bca9690bSStephen Boyd } 367bca9690bSStephen Boyd 368bca9690bSStephen Boyd int divider_get_val(unsigned long rate, unsigned long parent_rate, 369bca9690bSStephen Boyd const struct clk_div_table *table, u8 width, 370bca9690bSStephen Boyd unsigned long flags) 371bca9690bSStephen Boyd { 372bca9690bSStephen Boyd unsigned int div, value; 373bca9690bSStephen Boyd 374*9556f9daSBrian Norris div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 375bca9690bSStephen Boyd 376bca9690bSStephen Boyd if (!_is_valid_div(table, div, flags)) 377bca9690bSStephen Boyd return -EINVAL; 378bca9690bSStephen Boyd 379afe76c8fSJim Quinlan value = _get_val(table, div, flags, width); 380bca9690bSStephen Boyd 381bca9690bSStephen Boyd return min_t(unsigned int, value, div_mask(width)); 382bca9690bSStephen Boyd } 383bca9690bSStephen Boyd EXPORT_SYMBOL_GPL(divider_get_val); 3849d9f78edSMike Turquette 3851c0035d7SShawn Guo static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, 3861c0035d7SShawn Guo unsigned long parent_rate) 3879d9f78edSMike Turquette { 3889d9f78edSMike Turquette struct clk_divider *divider = to_clk_divider(hw); 389bca9690bSStephen Boyd unsigned int value; 3909d9f78edSMike Turquette unsigned long flags = 0; 3919d9f78edSMike Turquette u32 val; 3929d9f78edSMike Turquette 393bca9690bSStephen Boyd value = divider_get_val(rate, parent_rate, divider->table, 394bca9690bSStephen Boyd divider->width, divider->flags); 3959d9f78edSMike Turquette 3969d9f78edSMike Turquette if (divider->lock) 3979d9f78edSMike Turquette spin_lock_irqsave(divider->lock, flags); 398661e2180SStephen Boyd else 399661e2180SStephen Boyd __acquire(divider->lock); 4009d9f78edSMike Turquette 401d57dfe75SHaojian Zhuang if (divider->flags & CLK_DIVIDER_HIWORD_MASK) { 402bca9690bSStephen Boyd val = div_mask(divider->width) << (divider->shift + 16); 403d57dfe75SHaojian Zhuang } else { 404aa514ce3SGerhard Sittig val = clk_readl(divider->reg); 405bca9690bSStephen Boyd val &= ~(div_mask(divider->width) << divider->shift); 406d57dfe75SHaojian Zhuang } 4076d9252bdSRajendra Nayak val |= value << divider->shift; 408aa514ce3SGerhard Sittig clk_writel(val, divider->reg); 4099d9f78edSMike Turquette 4109d9f78edSMike Turquette if (divider->lock) 4119d9f78edSMike Turquette spin_unlock_irqrestore(divider->lock, flags); 412661e2180SStephen Boyd else 413661e2180SStephen Boyd __release(divider->lock); 4149d9f78edSMike Turquette 4159d9f78edSMike Turquette return 0; 4169d9f78edSMike Turquette } 4179d9f78edSMike Turquette 418822c250eSShawn Guo const struct clk_ops clk_divider_ops = { 4199d9f78edSMike Turquette .recalc_rate = clk_divider_recalc_rate, 4209d9f78edSMike Turquette .round_rate = clk_divider_round_rate, 4219d9f78edSMike Turquette .set_rate = clk_divider_set_rate, 4229d9f78edSMike Turquette }; 4239d9f78edSMike Turquette EXPORT_SYMBOL_GPL(clk_divider_ops); 4249d9f78edSMike Turquette 425357c3f0aSRajendra Nayak static struct clk *_register_divider(struct device *dev, const char *name, 4269d9f78edSMike Turquette const char *parent_name, unsigned long flags, 4279d9f78edSMike Turquette void __iomem *reg, u8 shift, u8 width, 428357c3f0aSRajendra Nayak u8 clk_divider_flags, const struct clk_div_table *table, 429357c3f0aSRajendra Nayak spinlock_t *lock) 4309d9f78edSMike Turquette { 4319d9f78edSMike Turquette struct clk_divider *div; 4329d9f78edSMike Turquette struct clk *clk; 4330197b3eaSSaravana Kannan struct clk_init_data init; 4349d9f78edSMike Turquette 435d57dfe75SHaojian Zhuang if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) { 436d57dfe75SHaojian Zhuang if (width + shift > 16) { 437d57dfe75SHaojian Zhuang pr_warn("divider value exceeds LOWORD field\n"); 438d57dfe75SHaojian Zhuang return ERR_PTR(-EINVAL); 439d57dfe75SHaojian Zhuang } 440d57dfe75SHaojian Zhuang } 441d57dfe75SHaojian Zhuang 44227d54591SMike Turquette /* allocate the divider */ 443d122db7eSStephen Boyd div = kzalloc(sizeof(*div), GFP_KERNEL); 444d122db7eSStephen Boyd if (!div) 44527d54591SMike Turquette return ERR_PTR(-ENOMEM); 4469d9f78edSMike Turquette 4470197b3eaSSaravana Kannan init.name = name; 4480197b3eaSSaravana Kannan init.ops = &clk_divider_ops; 449f7d8caadSRajendra Nayak init.flags = flags | CLK_IS_BASIC; 4500197b3eaSSaravana Kannan init.parent_names = (parent_name ? &parent_name: NULL); 4510197b3eaSSaravana Kannan init.num_parents = (parent_name ? 1 : 0); 4520197b3eaSSaravana Kannan 4539d9f78edSMike Turquette /* struct clk_divider assignments */ 4549d9f78edSMike Turquette div->reg = reg; 4559d9f78edSMike Turquette div->shift = shift; 4569d9f78edSMike Turquette div->width = width; 4579d9f78edSMike Turquette div->flags = clk_divider_flags; 4589d9f78edSMike Turquette div->lock = lock; 4590197b3eaSSaravana Kannan div->hw.init = &init; 460357c3f0aSRajendra Nayak div->table = table; 4619d9f78edSMike Turquette 46227d54591SMike Turquette /* register the clock */ 4630197b3eaSSaravana Kannan clk = clk_register(dev, &div->hw); 4649d9f78edSMike Turquette 46527d54591SMike Turquette if (IS_ERR(clk)) 4669d9f78edSMike Turquette kfree(div); 4679d9f78edSMike Turquette 46827d54591SMike Turquette return clk; 4699d9f78edSMike Turquette } 470357c3f0aSRajendra Nayak 471357c3f0aSRajendra Nayak /** 472357c3f0aSRajendra Nayak * clk_register_divider - register a divider clock with the clock framework 473357c3f0aSRajendra Nayak * @dev: device registering this clock 474357c3f0aSRajendra Nayak * @name: name of this clock 475357c3f0aSRajendra Nayak * @parent_name: name of clock's parent 476357c3f0aSRajendra Nayak * @flags: framework-specific flags 477357c3f0aSRajendra Nayak * @reg: register address to adjust divider 478357c3f0aSRajendra Nayak * @shift: number of bits to shift the bitfield 479357c3f0aSRajendra Nayak * @width: width of the bitfield 480357c3f0aSRajendra Nayak * @clk_divider_flags: divider-specific flags for this clock 481357c3f0aSRajendra Nayak * @lock: shared register lock for this clock 482357c3f0aSRajendra Nayak */ 483357c3f0aSRajendra Nayak struct clk *clk_register_divider(struct device *dev, const char *name, 484357c3f0aSRajendra Nayak const char *parent_name, unsigned long flags, 485357c3f0aSRajendra Nayak void __iomem *reg, u8 shift, u8 width, 486357c3f0aSRajendra Nayak u8 clk_divider_flags, spinlock_t *lock) 487357c3f0aSRajendra Nayak { 488357c3f0aSRajendra Nayak return _register_divider(dev, name, parent_name, flags, reg, shift, 489357c3f0aSRajendra Nayak width, clk_divider_flags, NULL, lock); 490357c3f0aSRajendra Nayak } 4914c5eeea9SFabio Estevam EXPORT_SYMBOL_GPL(clk_register_divider); 492357c3f0aSRajendra Nayak 493357c3f0aSRajendra Nayak /** 494357c3f0aSRajendra Nayak * clk_register_divider_table - register a table based divider clock with 495357c3f0aSRajendra Nayak * the clock framework 496357c3f0aSRajendra Nayak * @dev: device registering this clock 497357c3f0aSRajendra Nayak * @name: name of this clock 498357c3f0aSRajendra Nayak * @parent_name: name of clock's parent 499357c3f0aSRajendra Nayak * @flags: framework-specific flags 500357c3f0aSRajendra Nayak * @reg: register address to adjust divider 501357c3f0aSRajendra Nayak * @shift: number of bits to shift the bitfield 502357c3f0aSRajendra Nayak * @width: width of the bitfield 503357c3f0aSRajendra Nayak * @clk_divider_flags: divider-specific flags for this clock 504357c3f0aSRajendra Nayak * @table: array of divider/value pairs ending with a div set to 0 505357c3f0aSRajendra Nayak * @lock: shared register lock for this clock 506357c3f0aSRajendra Nayak */ 507357c3f0aSRajendra Nayak struct clk *clk_register_divider_table(struct device *dev, const char *name, 508357c3f0aSRajendra Nayak const char *parent_name, unsigned long flags, 509357c3f0aSRajendra Nayak void __iomem *reg, u8 shift, u8 width, 510357c3f0aSRajendra Nayak u8 clk_divider_flags, const struct clk_div_table *table, 511357c3f0aSRajendra Nayak spinlock_t *lock) 512357c3f0aSRajendra Nayak { 513357c3f0aSRajendra Nayak return _register_divider(dev, name, parent_name, flags, reg, shift, 514357c3f0aSRajendra Nayak width, clk_divider_flags, table, lock); 515357c3f0aSRajendra Nayak } 5164c5eeea9SFabio Estevam EXPORT_SYMBOL_GPL(clk_register_divider_table); 5174e3c021fSKrzysztof Kozlowski 5184e3c021fSKrzysztof Kozlowski void clk_unregister_divider(struct clk *clk) 5194e3c021fSKrzysztof Kozlowski { 5204e3c021fSKrzysztof Kozlowski struct clk_divider *div; 5214e3c021fSKrzysztof Kozlowski struct clk_hw *hw; 5224e3c021fSKrzysztof Kozlowski 5234e3c021fSKrzysztof Kozlowski hw = __clk_get_hw(clk); 5244e3c021fSKrzysztof Kozlowski if (!hw) 5254e3c021fSKrzysztof Kozlowski return; 5264e3c021fSKrzysztof Kozlowski 5274e3c021fSKrzysztof Kozlowski div = to_clk_divider(hw); 5284e3c021fSKrzysztof Kozlowski 5294e3c021fSKrzysztof Kozlowski clk_unregister(clk); 5304e3c021fSKrzysztof Kozlowski kfree(div); 5314e3c021fSKrzysztof Kozlowski } 5324e3c021fSKrzysztof Kozlowski EXPORT_SYMBOL_GPL(clk_unregister_divider); 533