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 31fab88ca7SStephen Boyd static unsigned int _get_table_maxdiv(const struct clk_div_table *table, 32fab88ca7SStephen Boyd u8 width) 33357c3f0aSRajendra Nayak { 34e6d3cc7bSJerome Brunet unsigned int maxdiv = 0, mask = clk_div_mask(width); 35357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 36357c3f0aSRajendra Nayak 37357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 38fab88ca7SStephen Boyd if (clkt->div > maxdiv && clkt->val <= mask) 39357c3f0aSRajendra Nayak maxdiv = clkt->div; 40357c3f0aSRajendra Nayak return maxdiv; 41357c3f0aSRajendra Nayak } 42357c3f0aSRajendra Nayak 43774b5143SMaxime COQUELIN static unsigned int _get_table_mindiv(const struct clk_div_table *table) 44774b5143SMaxime COQUELIN { 45774b5143SMaxime COQUELIN unsigned int mindiv = UINT_MAX; 46774b5143SMaxime COQUELIN const struct clk_div_table *clkt; 47774b5143SMaxime COQUELIN 48774b5143SMaxime COQUELIN for (clkt = table; clkt->div; clkt++) 49774b5143SMaxime COQUELIN if (clkt->div < mindiv) 50774b5143SMaxime COQUELIN mindiv = clkt->div; 51774b5143SMaxime COQUELIN return mindiv; 52774b5143SMaxime COQUELIN } 53774b5143SMaxime COQUELIN 54bca9690bSStephen Boyd static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width, 55bca9690bSStephen Boyd unsigned long flags) 566d9252bdSRajendra Nayak { 57bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ONE_BASED) 58e6d3cc7bSJerome Brunet return clk_div_mask(width); 59bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 60e6d3cc7bSJerome Brunet return 1 << clk_div_mask(width); 61bca9690bSStephen Boyd if (table) 62fab88ca7SStephen Boyd return _get_table_maxdiv(table, width); 63e6d3cc7bSJerome Brunet return clk_div_mask(width) + 1; 646d9252bdSRajendra Nayak } 656d9252bdSRajendra Nayak 66357c3f0aSRajendra Nayak static unsigned int _get_table_div(const struct clk_div_table *table, 67357c3f0aSRajendra Nayak unsigned int val) 68357c3f0aSRajendra Nayak { 69357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 70357c3f0aSRajendra Nayak 71357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 72357c3f0aSRajendra Nayak if (clkt->val == val) 73357c3f0aSRajendra Nayak return clkt->div; 74357c3f0aSRajendra Nayak return 0; 75357c3f0aSRajendra Nayak } 76357c3f0aSRajendra Nayak 77bca9690bSStephen Boyd static unsigned int _get_div(const struct clk_div_table *table, 78afe76c8fSJim Quinlan unsigned int val, unsigned long flags, u8 width) 796d9252bdSRajendra Nayak { 80bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ONE_BASED) 816d9252bdSRajendra Nayak return val; 82bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 836d9252bdSRajendra Nayak return 1 << val; 84afe76c8fSJim Quinlan if (flags & CLK_DIVIDER_MAX_AT_ZERO) 85e6d3cc7bSJerome Brunet return val ? val : clk_div_mask(width) + 1; 86bca9690bSStephen Boyd if (table) 87bca9690bSStephen Boyd return _get_table_div(table, val); 886d9252bdSRajendra Nayak return val + 1; 896d9252bdSRajendra Nayak } 906d9252bdSRajendra Nayak 91357c3f0aSRajendra Nayak static unsigned int _get_table_val(const struct clk_div_table *table, 92357c3f0aSRajendra Nayak unsigned int div) 93357c3f0aSRajendra Nayak { 94357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 95357c3f0aSRajendra Nayak 96357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 97357c3f0aSRajendra Nayak if (clkt->div == div) 98357c3f0aSRajendra Nayak return clkt->val; 99357c3f0aSRajendra Nayak return 0; 100357c3f0aSRajendra Nayak } 101357c3f0aSRajendra Nayak 102bca9690bSStephen Boyd static unsigned int _get_val(const struct clk_div_table *table, 103afe76c8fSJim Quinlan unsigned int div, unsigned long flags, u8 width) 1046d9252bdSRajendra Nayak { 105bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ONE_BASED) 1066d9252bdSRajendra Nayak return div; 107bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 1086d9252bdSRajendra Nayak return __ffs(div); 109afe76c8fSJim Quinlan if (flags & CLK_DIVIDER_MAX_AT_ZERO) 110e6d3cc7bSJerome Brunet return (div == clk_div_mask(width) + 1) ? 0 : div; 111bca9690bSStephen Boyd if (table) 112bca9690bSStephen Boyd return _get_table_val(table, div); 1136d9252bdSRajendra Nayak return div - 1; 1146d9252bdSRajendra Nayak } 1159d9f78edSMike Turquette 116bca9690bSStephen Boyd unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate, 117bca9690bSStephen Boyd unsigned int val, 118bca9690bSStephen Boyd const struct clk_div_table *table, 11912a26c29SJerome Brunet unsigned long flags, unsigned long width) 1209d9f78edSMike Turquette { 121bca9690bSStephen Boyd unsigned int div; 1229d9f78edSMike Turquette 12312a26c29SJerome Brunet div = _get_div(table, val, flags, width); 1246d9252bdSRajendra Nayak if (!div) { 125bca9690bSStephen Boyd WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO), 126056b2053SSoren Brinkmann "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n", 1272f508a95SStephen Boyd clk_hw_get_name(hw)); 1286d9252bdSRajendra Nayak return parent_rate; 1296d9252bdSRajendra Nayak } 1309d9f78edSMike Turquette 1319556f9daSBrian Norris return DIV_ROUND_UP_ULL((u64)parent_rate, div); 1329d9f78edSMike Turquette } 133bca9690bSStephen Boyd EXPORT_SYMBOL_GPL(divider_recalc_rate); 134bca9690bSStephen Boyd 135bca9690bSStephen Boyd static unsigned long clk_divider_recalc_rate(struct clk_hw *hw, 136bca9690bSStephen Boyd unsigned long parent_rate) 137bca9690bSStephen Boyd { 138bca9690bSStephen Boyd struct clk_divider *divider = to_clk_divider(hw); 139bca9690bSStephen Boyd unsigned int val; 140bca9690bSStephen Boyd 141bca9690bSStephen Boyd val = clk_readl(divider->reg) >> divider->shift; 142e6d3cc7bSJerome Brunet val &= clk_div_mask(divider->width); 143bca9690bSStephen Boyd 144bca9690bSStephen Boyd return divider_recalc_rate(hw, parent_rate, val, divider->table, 14512a26c29SJerome Brunet divider->flags, divider->width); 146bca9690bSStephen Boyd } 1479d9f78edSMike Turquette 148357c3f0aSRajendra Nayak static bool _is_valid_table_div(const struct clk_div_table *table, 149357c3f0aSRajendra Nayak unsigned int div) 150357c3f0aSRajendra Nayak { 151357c3f0aSRajendra Nayak const struct clk_div_table *clkt; 152357c3f0aSRajendra Nayak 153357c3f0aSRajendra Nayak for (clkt = table; clkt->div; clkt++) 154357c3f0aSRajendra Nayak if (clkt->div == div) 155357c3f0aSRajendra Nayak return true; 156357c3f0aSRajendra Nayak return false; 157357c3f0aSRajendra Nayak } 158357c3f0aSRajendra Nayak 159bca9690bSStephen Boyd static bool _is_valid_div(const struct clk_div_table *table, unsigned int div, 160bca9690bSStephen Boyd unsigned long flags) 161357c3f0aSRajendra Nayak { 162bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 1631a3cd184SJames Hogan return is_power_of_2(div); 164bca9690bSStephen Boyd if (table) 165bca9690bSStephen Boyd return _is_valid_table_div(table, div); 166357c3f0aSRajendra Nayak return true; 167357c3f0aSRajendra Nayak } 168357c3f0aSRajendra Nayak 169dd23c2cdSMaxime COQUELIN static int _round_up_table(const struct clk_div_table *table, int div) 170dd23c2cdSMaxime COQUELIN { 171dd23c2cdSMaxime COQUELIN const struct clk_div_table *clkt; 172fe52e750SMaxime COQUELIN int up = INT_MAX; 173dd23c2cdSMaxime COQUELIN 174dd23c2cdSMaxime COQUELIN for (clkt = table; clkt->div; clkt++) { 175dd23c2cdSMaxime COQUELIN if (clkt->div == div) 176dd23c2cdSMaxime COQUELIN return clkt->div; 177dd23c2cdSMaxime COQUELIN else if (clkt->div < div) 178dd23c2cdSMaxime COQUELIN continue; 179dd23c2cdSMaxime COQUELIN 180dd23c2cdSMaxime COQUELIN if ((clkt->div - div) < (up - div)) 181dd23c2cdSMaxime COQUELIN up = clkt->div; 182dd23c2cdSMaxime COQUELIN } 183dd23c2cdSMaxime COQUELIN 184dd23c2cdSMaxime COQUELIN return up; 185dd23c2cdSMaxime COQUELIN } 186dd23c2cdSMaxime COQUELIN 187774b5143SMaxime COQUELIN static int _round_down_table(const struct clk_div_table *table, int div) 188774b5143SMaxime COQUELIN { 189774b5143SMaxime COQUELIN const struct clk_div_table *clkt; 190774b5143SMaxime COQUELIN int down = _get_table_mindiv(table); 191774b5143SMaxime COQUELIN 192774b5143SMaxime COQUELIN for (clkt = table; clkt->div; clkt++) { 193774b5143SMaxime COQUELIN if (clkt->div == div) 194774b5143SMaxime COQUELIN return clkt->div; 195774b5143SMaxime COQUELIN else if (clkt->div > div) 196774b5143SMaxime COQUELIN continue; 197774b5143SMaxime COQUELIN 198774b5143SMaxime COQUELIN if ((div - clkt->div) < (div - down)) 199774b5143SMaxime COQUELIN down = clkt->div; 200774b5143SMaxime COQUELIN } 201774b5143SMaxime COQUELIN 202774b5143SMaxime COQUELIN return down; 203774b5143SMaxime COQUELIN } 204774b5143SMaxime COQUELIN 205bca9690bSStephen Boyd static int _div_round_up(const struct clk_div_table *table, 206bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 207bca9690bSStephen Boyd unsigned long flags) 208dd23c2cdSMaxime COQUELIN { 2099556f9daSBrian Norris int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 210dd23c2cdSMaxime COQUELIN 211bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 212dd23c2cdSMaxime COQUELIN div = __roundup_pow_of_two(div); 213bca9690bSStephen Boyd if (table) 214bca9690bSStephen Boyd div = _round_up_table(table, div); 215dd23c2cdSMaxime COQUELIN 216dd23c2cdSMaxime COQUELIN return div; 217dd23c2cdSMaxime COQUELIN } 218dd23c2cdSMaxime COQUELIN 219bca9690bSStephen Boyd static int _div_round_closest(const struct clk_div_table *table, 220bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 221bca9690bSStephen Boyd unsigned long flags) 222774b5143SMaxime COQUELIN { 22393155142SUwe Kleine-König int up, down; 22426bac95aSUwe Kleine-König unsigned long up_rate, down_rate; 225774b5143SMaxime COQUELIN 2269556f9daSBrian Norris up = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 22793155142SUwe Kleine-König down = parent_rate / rate; 228774b5143SMaxime COQUELIN 229bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) { 23093155142SUwe Kleine-König up = __roundup_pow_of_two(up); 23193155142SUwe Kleine-König down = __rounddown_pow_of_two(down); 232bca9690bSStephen Boyd } else if (table) { 23393155142SUwe Kleine-König up = _round_up_table(table, up); 23493155142SUwe Kleine-König down = _round_down_table(table, down); 235774b5143SMaxime COQUELIN } 236774b5143SMaxime COQUELIN 2379556f9daSBrian Norris up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up); 2389556f9daSBrian Norris down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down); 23926bac95aSUwe Kleine-König 24026bac95aSUwe Kleine-König return (rate - up_rate) <= (down_rate - rate) ? up : down; 241774b5143SMaxime COQUELIN } 242774b5143SMaxime COQUELIN 243bca9690bSStephen Boyd static int _div_round(const struct clk_div_table *table, 244bca9690bSStephen Boyd unsigned long parent_rate, unsigned long rate, 245bca9690bSStephen Boyd unsigned long flags) 246774b5143SMaxime COQUELIN { 247bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ROUND_CLOSEST) 248bca9690bSStephen Boyd return _div_round_closest(table, parent_rate, rate, flags); 249774b5143SMaxime COQUELIN 250bca9690bSStephen Boyd return _div_round_up(table, parent_rate, rate, flags); 251774b5143SMaxime COQUELIN } 252774b5143SMaxime COQUELIN 253bca9690bSStephen Boyd static bool _is_best_div(unsigned long rate, unsigned long now, 254bca9690bSStephen Boyd unsigned long best, unsigned long flags) 255774b5143SMaxime COQUELIN { 256bca9690bSStephen Boyd if (flags & CLK_DIVIDER_ROUND_CLOSEST) 257774b5143SMaxime COQUELIN return abs(rate - now) < abs(rate - best); 258774b5143SMaxime COQUELIN 259774b5143SMaxime COQUELIN return now <= rate && now > best; 260774b5143SMaxime COQUELIN } 261774b5143SMaxime COQUELIN 262bca9690bSStephen Boyd static int _next_div(const struct clk_div_table *table, int div, 263bca9690bSStephen Boyd unsigned long flags) 2640e2de78eSMaxime COQUELIN { 2650e2de78eSMaxime COQUELIN div++; 2660e2de78eSMaxime COQUELIN 267bca9690bSStephen Boyd if (flags & CLK_DIVIDER_POWER_OF_TWO) 2680e2de78eSMaxime COQUELIN return __roundup_pow_of_two(div); 269bca9690bSStephen Boyd if (table) 270bca9690bSStephen Boyd return _round_up_table(table, div); 2710e2de78eSMaxime COQUELIN 2720e2de78eSMaxime COQUELIN return div; 2730e2de78eSMaxime COQUELIN } 2740e2de78eSMaxime COQUELIN 27522833a91SMaxime Ripard static int clk_divider_bestdiv(struct clk_hw *hw, struct clk_hw *parent, 27622833a91SMaxime Ripard unsigned long rate, 277bca9690bSStephen Boyd unsigned long *best_parent_rate, 278bca9690bSStephen Boyd const struct clk_div_table *table, u8 width, 279bca9690bSStephen Boyd unsigned long flags) 2809d9f78edSMike Turquette { 2819d9f78edSMike Turquette int i, bestdiv = 0; 2829d9f78edSMike Turquette unsigned long parent_rate, best = 0, now, maxdiv; 283081c9025SShawn Guo unsigned long parent_rate_saved = *best_parent_rate; 2849d9f78edSMike Turquette 2859d9f78edSMike Turquette if (!rate) 2869d9f78edSMike Turquette rate = 1; 2879d9f78edSMike Turquette 288bca9690bSStephen Boyd maxdiv = _get_maxdiv(table, width, flags); 2899d9f78edSMike Turquette 29098d8a60eSStephen Boyd if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) { 29181536e07SShawn Guo parent_rate = *best_parent_rate; 292bca9690bSStephen Boyd bestdiv = _div_round(table, parent_rate, rate, flags); 2939d9f78edSMike Turquette bestdiv = bestdiv == 0 ? 1 : bestdiv; 2949d9f78edSMike Turquette bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv; 2959d9f78edSMike Turquette return bestdiv; 2969d9f78edSMike Turquette } 2979d9f78edSMike Turquette 2989d9f78edSMike Turquette /* 2999d9f78edSMike Turquette * The maximum divider we can use without overflowing 3009d9f78edSMike Turquette * unsigned long in rate * i below 3019d9f78edSMike Turquette */ 3029d9f78edSMike Turquette maxdiv = min(ULONG_MAX / rate, maxdiv); 3039d9f78edSMike Turquette 304653d1452SMasahiro Yamada for (i = _next_div(table, 0, flags); i <= maxdiv; 305653d1452SMasahiro Yamada i = _next_div(table, i, flags)) { 306081c9025SShawn Guo if (rate * i == parent_rate_saved) { 307081c9025SShawn Guo /* 308081c9025SShawn Guo * It's the most ideal case if the requested rate can be 309081c9025SShawn Guo * divided from parent clock without needing to change 310081c9025SShawn Guo * parent rate, so return the divider immediately. 311081c9025SShawn Guo */ 312081c9025SShawn Guo *best_parent_rate = parent_rate_saved; 313081c9025SShawn Guo return i; 314081c9025SShawn Guo } 31522833a91SMaxime Ripard parent_rate = clk_hw_round_rate(parent, rate * i); 3169556f9daSBrian Norris now = DIV_ROUND_UP_ULL((u64)parent_rate, i); 317bca9690bSStephen Boyd if (_is_best_div(rate, now, best, flags)) { 3189d9f78edSMike Turquette bestdiv = i; 3199d9f78edSMike Turquette best = now; 3209d9f78edSMike Turquette *best_parent_rate = parent_rate; 3219d9f78edSMike Turquette } 3229d9f78edSMike Turquette } 3239d9f78edSMike Turquette 3249d9f78edSMike Turquette if (!bestdiv) { 325bca9690bSStephen Boyd bestdiv = _get_maxdiv(table, width, flags); 32622833a91SMaxime Ripard *best_parent_rate = clk_hw_round_rate(parent, 1); 3279d9f78edSMike Turquette } 3289d9f78edSMike Turquette 3299d9f78edSMike Turquette return bestdiv; 3309d9f78edSMike Turquette } 3319d9f78edSMike Turquette 33222833a91SMaxime Ripard long divider_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent, 33322833a91SMaxime Ripard unsigned long rate, unsigned long *prate, 33422833a91SMaxime Ripard const struct clk_div_table *table, 335bca9690bSStephen Boyd u8 width, unsigned long flags) 3369d9f78edSMike Turquette { 3379d9f78edSMike Turquette int div; 338bca9690bSStephen Boyd 33922833a91SMaxime Ripard div = clk_divider_bestdiv(hw, parent, rate, prate, table, width, flags); 3409d9f78edSMike Turquette 3419556f9daSBrian Norris return DIV_ROUND_UP_ULL((u64)*prate, div); 3429d9f78edSMike Turquette } 34322833a91SMaxime Ripard EXPORT_SYMBOL_GPL(divider_round_rate_parent); 344bca9690bSStephen Boyd 345*b15ee490SJerome Brunet long divider_ro_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent, 346*b15ee490SJerome Brunet unsigned long rate, unsigned long *prate, 347*b15ee490SJerome Brunet const struct clk_div_table *table, u8 width, 348*b15ee490SJerome Brunet unsigned long flags, unsigned int val) 349*b15ee490SJerome Brunet { 350*b15ee490SJerome Brunet int div; 351*b15ee490SJerome Brunet 352*b15ee490SJerome Brunet div = _get_div(table, val, flags, width); 353*b15ee490SJerome Brunet 354*b15ee490SJerome Brunet /* Even a read-only clock can propagate a rate change */ 355*b15ee490SJerome Brunet if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) { 356*b15ee490SJerome Brunet if (!parent) 357*b15ee490SJerome Brunet return -EINVAL; 358*b15ee490SJerome Brunet 359*b15ee490SJerome Brunet *prate = clk_hw_round_rate(parent, rate * div); 360*b15ee490SJerome Brunet } 361*b15ee490SJerome Brunet 362*b15ee490SJerome Brunet return DIV_ROUND_UP_ULL((u64)*prate, div); 363*b15ee490SJerome Brunet } 364*b15ee490SJerome Brunet EXPORT_SYMBOL_GPL(divider_ro_round_rate_parent); 365*b15ee490SJerome Brunet 366*b15ee490SJerome Brunet 367bca9690bSStephen Boyd static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate, 368bca9690bSStephen Boyd unsigned long *prate) 369bca9690bSStephen Boyd { 370bca9690bSStephen Boyd struct clk_divider *divider = to_clk_divider(hw); 371bca9690bSStephen Boyd 372bca9690bSStephen Boyd /* if read only, just return current value */ 373bca9690bSStephen Boyd if (divider->flags & CLK_DIVIDER_READ_ONLY) { 374*b15ee490SJerome Brunet u32 val; 375*b15ee490SJerome Brunet 376*b15ee490SJerome Brunet val = clk_readl(divider->reg) >> divider->shift; 377*b15ee490SJerome Brunet val &= clk_div_mask(divider->width); 378*b15ee490SJerome Brunet 379*b15ee490SJerome Brunet return divider_ro_round_rate(hw, rate, prate, divider->table, 380*b15ee490SJerome Brunet divider->width, divider->flags, 381*b15ee490SJerome Brunet val); 382bca9690bSStephen Boyd } 383bca9690bSStephen Boyd 384bca9690bSStephen Boyd return divider_round_rate(hw, rate, prate, divider->table, 385bca9690bSStephen Boyd divider->width, divider->flags); 386bca9690bSStephen Boyd } 387bca9690bSStephen Boyd 388bca9690bSStephen Boyd int divider_get_val(unsigned long rate, unsigned long parent_rate, 389bca9690bSStephen Boyd const struct clk_div_table *table, u8 width, 390bca9690bSStephen Boyd unsigned long flags) 391bca9690bSStephen Boyd { 392bca9690bSStephen Boyd unsigned int div, value; 393bca9690bSStephen Boyd 3949556f9daSBrian Norris div = DIV_ROUND_UP_ULL((u64)parent_rate, rate); 395bca9690bSStephen Boyd 396bca9690bSStephen Boyd if (!_is_valid_div(table, div, flags)) 397bca9690bSStephen Boyd return -EINVAL; 398bca9690bSStephen Boyd 399afe76c8fSJim Quinlan value = _get_val(table, div, flags, width); 400bca9690bSStephen Boyd 401e6d3cc7bSJerome Brunet return min_t(unsigned int, value, clk_div_mask(width)); 402bca9690bSStephen Boyd } 403bca9690bSStephen Boyd EXPORT_SYMBOL_GPL(divider_get_val); 4049d9f78edSMike Turquette 4051c0035d7SShawn Guo static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, 4061c0035d7SShawn Guo unsigned long parent_rate) 4079d9f78edSMike Turquette { 4089d9f78edSMike Turquette struct clk_divider *divider = to_clk_divider(hw); 4092316a7a3SAlex Frid int value; 4109d9f78edSMike Turquette unsigned long flags = 0; 4119d9f78edSMike Turquette u32 val; 4129d9f78edSMike Turquette 413bca9690bSStephen Boyd value = divider_get_val(rate, parent_rate, divider->table, 414bca9690bSStephen Boyd divider->width, divider->flags); 4152316a7a3SAlex Frid if (value < 0) 4162316a7a3SAlex Frid return value; 4179d9f78edSMike Turquette 4189d9f78edSMike Turquette if (divider->lock) 4199d9f78edSMike Turquette spin_lock_irqsave(divider->lock, flags); 420661e2180SStephen Boyd else 421661e2180SStephen Boyd __acquire(divider->lock); 4229d9f78edSMike Turquette 423d57dfe75SHaojian Zhuang if (divider->flags & CLK_DIVIDER_HIWORD_MASK) { 424e6d3cc7bSJerome Brunet val = clk_div_mask(divider->width) << (divider->shift + 16); 425d57dfe75SHaojian Zhuang } else { 426aa514ce3SGerhard Sittig val = clk_readl(divider->reg); 427e6d3cc7bSJerome Brunet val &= ~(clk_div_mask(divider->width) << divider->shift); 428d57dfe75SHaojian Zhuang } 4292316a7a3SAlex Frid val |= (u32)value << divider->shift; 430aa514ce3SGerhard Sittig clk_writel(val, divider->reg); 4319d9f78edSMike Turquette 4329d9f78edSMike Turquette if (divider->lock) 4339d9f78edSMike Turquette spin_unlock_irqrestore(divider->lock, flags); 434661e2180SStephen Boyd else 435661e2180SStephen Boyd __release(divider->lock); 4369d9f78edSMike Turquette 4379d9f78edSMike Turquette return 0; 4389d9f78edSMike Turquette } 4399d9f78edSMike Turquette 440822c250eSShawn Guo const struct clk_ops clk_divider_ops = { 4419d9f78edSMike Turquette .recalc_rate = clk_divider_recalc_rate, 4429d9f78edSMike Turquette .round_rate = clk_divider_round_rate, 4439d9f78edSMike Turquette .set_rate = clk_divider_set_rate, 4449d9f78edSMike Turquette }; 4459d9f78edSMike Turquette EXPORT_SYMBOL_GPL(clk_divider_ops); 4469d9f78edSMike Turquette 44750359819SHeiko Stuebner const struct clk_ops clk_divider_ro_ops = { 44850359819SHeiko Stuebner .recalc_rate = clk_divider_recalc_rate, 44950359819SHeiko Stuebner .round_rate = clk_divider_round_rate, 45050359819SHeiko Stuebner }; 45150359819SHeiko Stuebner EXPORT_SYMBOL_GPL(clk_divider_ro_ops); 45250359819SHeiko Stuebner 453eb7d264fSStephen Boyd static struct clk_hw *_register_divider(struct device *dev, const char *name, 4549d9f78edSMike Turquette const char *parent_name, unsigned long flags, 4559d9f78edSMike Turquette void __iomem *reg, u8 shift, u8 width, 456357c3f0aSRajendra Nayak u8 clk_divider_flags, const struct clk_div_table *table, 457357c3f0aSRajendra Nayak spinlock_t *lock) 4589d9f78edSMike Turquette { 4599d9f78edSMike Turquette struct clk_divider *div; 460eb7d264fSStephen Boyd struct clk_hw *hw; 4610197b3eaSSaravana Kannan struct clk_init_data init; 462eb7d264fSStephen Boyd int ret; 4639d9f78edSMike Turquette 464d57dfe75SHaojian Zhuang if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) { 465d57dfe75SHaojian Zhuang if (width + shift > 16) { 466d57dfe75SHaojian Zhuang pr_warn("divider value exceeds LOWORD field\n"); 467d57dfe75SHaojian Zhuang return ERR_PTR(-EINVAL); 468d57dfe75SHaojian Zhuang } 469d57dfe75SHaojian Zhuang } 470d57dfe75SHaojian Zhuang 47127d54591SMike Turquette /* allocate the divider */ 472d122db7eSStephen Boyd div = kzalloc(sizeof(*div), GFP_KERNEL); 473d122db7eSStephen Boyd if (!div) 47427d54591SMike Turquette return ERR_PTR(-ENOMEM); 4759d9f78edSMike Turquette 4760197b3eaSSaravana Kannan init.name = name; 47750359819SHeiko Stuebner if (clk_divider_flags & CLK_DIVIDER_READ_ONLY) 47850359819SHeiko Stuebner init.ops = &clk_divider_ro_ops; 47950359819SHeiko Stuebner else 4800197b3eaSSaravana Kannan init.ops = &clk_divider_ops; 481f7d8caadSRajendra Nayak init.flags = flags | CLK_IS_BASIC; 4820197b3eaSSaravana Kannan init.parent_names = (parent_name ? &parent_name: NULL); 4830197b3eaSSaravana Kannan init.num_parents = (parent_name ? 1 : 0); 4840197b3eaSSaravana Kannan 4859d9f78edSMike Turquette /* struct clk_divider assignments */ 4869d9f78edSMike Turquette div->reg = reg; 4879d9f78edSMike Turquette div->shift = shift; 4889d9f78edSMike Turquette div->width = width; 4899d9f78edSMike Turquette div->flags = clk_divider_flags; 4909d9f78edSMike Turquette div->lock = lock; 4910197b3eaSSaravana Kannan div->hw.init = &init; 492357c3f0aSRajendra Nayak div->table = table; 4939d9f78edSMike Turquette 49427d54591SMike Turquette /* register the clock */ 495eb7d264fSStephen Boyd hw = &div->hw; 496eb7d264fSStephen Boyd ret = clk_hw_register(dev, hw); 497eb7d264fSStephen Boyd if (ret) { 4989d9f78edSMike Turquette kfree(div); 499eb7d264fSStephen Boyd hw = ERR_PTR(ret); 500eb7d264fSStephen Boyd } 5019d9f78edSMike Turquette 502eb7d264fSStephen Boyd return hw; 5039d9f78edSMike Turquette } 504357c3f0aSRajendra Nayak 505357c3f0aSRajendra Nayak /** 506357c3f0aSRajendra Nayak * clk_register_divider - register a divider clock with the clock framework 507357c3f0aSRajendra Nayak * @dev: device registering this clock 508357c3f0aSRajendra Nayak * @name: name of this clock 509357c3f0aSRajendra Nayak * @parent_name: name of clock's parent 510357c3f0aSRajendra Nayak * @flags: framework-specific flags 511357c3f0aSRajendra Nayak * @reg: register address to adjust divider 512357c3f0aSRajendra Nayak * @shift: number of bits to shift the bitfield 513357c3f0aSRajendra Nayak * @width: width of the bitfield 514357c3f0aSRajendra Nayak * @clk_divider_flags: divider-specific flags for this clock 515357c3f0aSRajendra Nayak * @lock: shared register lock for this clock 516357c3f0aSRajendra Nayak */ 517357c3f0aSRajendra Nayak struct clk *clk_register_divider(struct device *dev, const char *name, 518357c3f0aSRajendra Nayak const char *parent_name, unsigned long flags, 519357c3f0aSRajendra Nayak void __iomem *reg, u8 shift, u8 width, 520357c3f0aSRajendra Nayak u8 clk_divider_flags, spinlock_t *lock) 521357c3f0aSRajendra Nayak { 522eb7d264fSStephen Boyd struct clk_hw *hw; 523eb7d264fSStephen Boyd 524eb7d264fSStephen Boyd hw = _register_divider(dev, name, parent_name, flags, reg, shift, 525eb7d264fSStephen Boyd width, clk_divider_flags, NULL, lock); 526eb7d264fSStephen Boyd if (IS_ERR(hw)) 527eb7d264fSStephen Boyd return ERR_CAST(hw); 528eb7d264fSStephen Boyd return hw->clk; 529eb7d264fSStephen Boyd } 530eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_register_divider); 531eb7d264fSStephen Boyd 532eb7d264fSStephen Boyd /** 533eb7d264fSStephen Boyd * clk_hw_register_divider - register a divider clock with the clock framework 534eb7d264fSStephen Boyd * @dev: device registering this clock 535eb7d264fSStephen Boyd * @name: name of this clock 536eb7d264fSStephen Boyd * @parent_name: name of clock's parent 537eb7d264fSStephen Boyd * @flags: framework-specific flags 538eb7d264fSStephen Boyd * @reg: register address to adjust divider 539eb7d264fSStephen Boyd * @shift: number of bits to shift the bitfield 540eb7d264fSStephen Boyd * @width: width of the bitfield 541eb7d264fSStephen Boyd * @clk_divider_flags: divider-specific flags for this clock 542eb7d264fSStephen Boyd * @lock: shared register lock for this clock 543eb7d264fSStephen Boyd */ 544eb7d264fSStephen Boyd struct clk_hw *clk_hw_register_divider(struct device *dev, const char *name, 545eb7d264fSStephen Boyd const char *parent_name, unsigned long flags, 546eb7d264fSStephen Boyd void __iomem *reg, u8 shift, u8 width, 547eb7d264fSStephen Boyd u8 clk_divider_flags, spinlock_t *lock) 548eb7d264fSStephen Boyd { 549357c3f0aSRajendra Nayak return _register_divider(dev, name, parent_name, flags, reg, shift, 550357c3f0aSRajendra Nayak width, clk_divider_flags, NULL, lock); 551357c3f0aSRajendra Nayak } 552eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_hw_register_divider); 553357c3f0aSRajendra Nayak 554357c3f0aSRajendra Nayak /** 555357c3f0aSRajendra Nayak * clk_register_divider_table - register a table based divider clock with 556357c3f0aSRajendra Nayak * the clock framework 557357c3f0aSRajendra Nayak * @dev: device registering this clock 558357c3f0aSRajendra Nayak * @name: name of this clock 559357c3f0aSRajendra Nayak * @parent_name: name of clock's parent 560357c3f0aSRajendra Nayak * @flags: framework-specific flags 561357c3f0aSRajendra Nayak * @reg: register address to adjust divider 562357c3f0aSRajendra Nayak * @shift: number of bits to shift the bitfield 563357c3f0aSRajendra Nayak * @width: width of the bitfield 564357c3f0aSRajendra Nayak * @clk_divider_flags: divider-specific flags for this clock 565357c3f0aSRajendra Nayak * @table: array of divider/value pairs ending with a div set to 0 566357c3f0aSRajendra Nayak * @lock: shared register lock for this clock 567357c3f0aSRajendra Nayak */ 568357c3f0aSRajendra Nayak struct clk *clk_register_divider_table(struct device *dev, const char *name, 569357c3f0aSRajendra Nayak const char *parent_name, unsigned long flags, 570357c3f0aSRajendra Nayak void __iomem *reg, u8 shift, u8 width, 571357c3f0aSRajendra Nayak u8 clk_divider_flags, const struct clk_div_table *table, 572357c3f0aSRajendra Nayak spinlock_t *lock) 573357c3f0aSRajendra Nayak { 574eb7d264fSStephen Boyd struct clk_hw *hw; 575eb7d264fSStephen Boyd 576eb7d264fSStephen Boyd hw = _register_divider(dev, name, parent_name, flags, reg, shift, 577eb7d264fSStephen Boyd width, clk_divider_flags, table, lock); 578eb7d264fSStephen Boyd if (IS_ERR(hw)) 579eb7d264fSStephen Boyd return ERR_CAST(hw); 580eb7d264fSStephen Boyd return hw->clk; 581eb7d264fSStephen Boyd } 582eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_register_divider_table); 583eb7d264fSStephen Boyd 584eb7d264fSStephen Boyd /** 585eb7d264fSStephen Boyd * clk_hw_register_divider_table - register a table based divider clock with 586eb7d264fSStephen Boyd * the clock framework 587eb7d264fSStephen Boyd * @dev: device registering this clock 588eb7d264fSStephen Boyd * @name: name of this clock 589eb7d264fSStephen Boyd * @parent_name: name of clock's parent 590eb7d264fSStephen Boyd * @flags: framework-specific flags 591eb7d264fSStephen Boyd * @reg: register address to adjust divider 592eb7d264fSStephen Boyd * @shift: number of bits to shift the bitfield 593eb7d264fSStephen Boyd * @width: width of the bitfield 594eb7d264fSStephen Boyd * @clk_divider_flags: divider-specific flags for this clock 595eb7d264fSStephen Boyd * @table: array of divider/value pairs ending with a div set to 0 596eb7d264fSStephen Boyd * @lock: shared register lock for this clock 597eb7d264fSStephen Boyd */ 598eb7d264fSStephen Boyd struct clk_hw *clk_hw_register_divider_table(struct device *dev, 599eb7d264fSStephen Boyd const char *name, const char *parent_name, unsigned long flags, 600eb7d264fSStephen Boyd void __iomem *reg, u8 shift, u8 width, 601eb7d264fSStephen Boyd u8 clk_divider_flags, const struct clk_div_table *table, 602eb7d264fSStephen Boyd spinlock_t *lock) 603eb7d264fSStephen Boyd { 604357c3f0aSRajendra Nayak return _register_divider(dev, name, parent_name, flags, reg, shift, 605357c3f0aSRajendra Nayak width, clk_divider_flags, table, lock); 606357c3f0aSRajendra Nayak } 607eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_hw_register_divider_table); 6084e3c021fSKrzysztof Kozlowski 6094e3c021fSKrzysztof Kozlowski void clk_unregister_divider(struct clk *clk) 6104e3c021fSKrzysztof Kozlowski { 6114e3c021fSKrzysztof Kozlowski struct clk_divider *div; 6124e3c021fSKrzysztof Kozlowski struct clk_hw *hw; 6134e3c021fSKrzysztof Kozlowski 6144e3c021fSKrzysztof Kozlowski hw = __clk_get_hw(clk); 6154e3c021fSKrzysztof Kozlowski if (!hw) 6164e3c021fSKrzysztof Kozlowski return; 6174e3c021fSKrzysztof Kozlowski 6184e3c021fSKrzysztof Kozlowski div = to_clk_divider(hw); 6194e3c021fSKrzysztof Kozlowski 6204e3c021fSKrzysztof Kozlowski clk_unregister(clk); 6214e3c021fSKrzysztof Kozlowski kfree(div); 6224e3c021fSKrzysztof Kozlowski } 6234e3c021fSKrzysztof Kozlowski EXPORT_SYMBOL_GPL(clk_unregister_divider); 624eb7d264fSStephen Boyd 625eb7d264fSStephen Boyd /** 626eb7d264fSStephen Boyd * clk_hw_unregister_divider - unregister a clk divider 627eb7d264fSStephen Boyd * @hw: hardware-specific clock data to unregister 628eb7d264fSStephen Boyd */ 629eb7d264fSStephen Boyd void clk_hw_unregister_divider(struct clk_hw *hw) 630eb7d264fSStephen Boyd { 631eb7d264fSStephen Boyd struct clk_divider *div; 632eb7d264fSStephen Boyd 633eb7d264fSStephen Boyd div = to_clk_divider(hw); 634eb7d264fSStephen Boyd 635eb7d264fSStephen Boyd clk_hw_unregister(hw); 636eb7d264fSStephen Boyd kfree(div); 637eb7d264fSStephen Boyd } 638eb7d264fSStephen Boyd EXPORT_SYMBOL_GPL(clk_hw_unregister_divider); 639