1163152cbSTony Lindgren /* 2163152cbSTony Lindgren * This program is free software; you can redistribute it and/or 3163152cbSTony Lindgren * modify it under the terms of the GNU General Public License as 4163152cbSTony Lindgren * published by the Free Software Foundation version 2. 5163152cbSTony Lindgren * 6163152cbSTony Lindgren * This program is distributed "as is" WITHOUT ANY WARRANTY of any 7163152cbSTony Lindgren * kind, whether express or implied; without even the implied warranty 8163152cbSTony Lindgren * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9163152cbSTony Lindgren * GNU General Public License for more details. 10163152cbSTony Lindgren */ 11163152cbSTony Lindgren 121b29e601SStephen Boyd #include <linux/clk.h> 13163152cbSTony Lindgren #include <linux/clk-provider.h> 14163152cbSTony Lindgren #include <linux/delay.h> 15163152cbSTony Lindgren #include <linux/err.h> 1662e59c4eSStephen Boyd #include <linux/io.h> 17cafeb002STony Lindgren #include <linux/math64.h> 18163152cbSTony Lindgren #include <linux/of.h> 19163152cbSTony Lindgren #include <linux/of_address.h> 20163152cbSTony Lindgren #include <linux/clk/ti.h> 21163152cbSTony Lindgren 22163152cbSTony Lindgren /* FAPLL Control Register PLL_CTRL */ 239089848dSTony Lindgren #define FAPLL_MAIN_MULT_N_SHIFT 16 249089848dSTony Lindgren #define FAPLL_MAIN_DIV_P_SHIFT 8 25163152cbSTony Lindgren #define FAPLL_MAIN_LOCK BIT(7) 26163152cbSTony Lindgren #define FAPLL_MAIN_PLLEN BIT(3) 27163152cbSTony Lindgren #define FAPLL_MAIN_BP BIT(2) 28163152cbSTony Lindgren #define FAPLL_MAIN_LOC_CTL BIT(0) 29163152cbSTony Lindgren 309089848dSTony Lindgren #define FAPLL_MAIN_MAX_MULT_N 0xffff 319089848dSTony Lindgren #define FAPLL_MAIN_MAX_DIV_P 0xff 329089848dSTony Lindgren #define FAPLL_MAIN_CLEAR_MASK \ 339089848dSTony Lindgren ((FAPLL_MAIN_MAX_MULT_N << FAPLL_MAIN_MULT_N_SHIFT) | \ 349089848dSTony Lindgren (FAPLL_MAIN_DIV_P_SHIFT << FAPLL_MAIN_DIV_P_SHIFT) | \ 359089848dSTony Lindgren FAPLL_MAIN_LOC_CTL) 369089848dSTony Lindgren 37163152cbSTony Lindgren /* FAPLL powerdown register PWD */ 38163152cbSTony Lindgren #define FAPLL_PWD_OFFSET 4 39163152cbSTony Lindgren 40163152cbSTony Lindgren #define MAX_FAPLL_OUTPUTS 7 41163152cbSTony Lindgren #define FAPLL_MAX_RETRIES 1000 42163152cbSTony Lindgren 43163152cbSTony Lindgren #define to_fapll(_hw) container_of(_hw, struct fapll_data, hw) 44163152cbSTony Lindgren #define to_synth(_hw) container_of(_hw, struct fapll_synth, hw) 45163152cbSTony Lindgren 46163152cbSTony Lindgren /* The bypass bit is inverted on the ddr_pll.. */ 47163152cbSTony Lindgren #define fapll_is_ddr_pll(va) (((u32)(va) & 0xffff) == 0x0440) 48163152cbSTony Lindgren 49163152cbSTony Lindgren /* 50163152cbSTony Lindgren * The audio_pll_clk1 input is hard wired to the 27MHz bypass clock, 51163152cbSTony Lindgren * and the audio_pll_clk1 synthesizer is hardwared to 32KiHz output. 52163152cbSTony Lindgren */ 53163152cbSTony Lindgren #define is_ddr_pll_clk1(va) (((u32)(va) & 0xffff) == 0x044c) 54163152cbSTony Lindgren #define is_audio_pll_clk1(va) (((u32)(va) & 0xffff) == 0x04a8) 55163152cbSTony Lindgren 56163152cbSTony Lindgren /* Synthesizer divider register */ 57163152cbSTony Lindgren #define SYNTH_LDMDIV1 BIT(8) 58163152cbSTony Lindgren 59163152cbSTony Lindgren /* Synthesizer frequency register */ 60163152cbSTony Lindgren #define SYNTH_LDFREQ BIT(31) 61163152cbSTony Lindgren 62cafeb002STony Lindgren #define SYNTH_PHASE_K 8 63cafeb002STony Lindgren #define SYNTH_MAX_INT_DIV 0xf 6433ca29c9STony Lindgren #define SYNTH_MAX_DIV_M 0xff 6533ca29c9STony Lindgren 66163152cbSTony Lindgren struct fapll_data { 67163152cbSTony Lindgren struct clk_hw hw; 68163152cbSTony Lindgren void __iomem *base; 69163152cbSTony Lindgren const char *name; 70163152cbSTony Lindgren struct clk *clk_ref; 71163152cbSTony Lindgren struct clk *clk_bypass; 72163152cbSTony Lindgren struct clk_onecell_data outputs; 73163152cbSTony Lindgren bool bypass_bit_inverted; 74163152cbSTony Lindgren }; 75163152cbSTony Lindgren 76163152cbSTony Lindgren struct fapll_synth { 77163152cbSTony Lindgren struct clk_hw hw; 78163152cbSTony Lindgren struct fapll_data *fd; 79163152cbSTony Lindgren int index; 80163152cbSTony Lindgren void __iomem *freq; 81163152cbSTony Lindgren void __iomem *div; 82163152cbSTony Lindgren const char *name; 83163152cbSTony Lindgren struct clk *clk_pll; 84163152cbSTony Lindgren }; 85163152cbSTony Lindgren 86163152cbSTony Lindgren static bool ti_fapll_clock_is_bypass(struct fapll_data *fd) 87163152cbSTony Lindgren { 88163152cbSTony Lindgren u32 v = readl_relaxed(fd->base); 89163152cbSTony Lindgren 90163152cbSTony Lindgren if (fd->bypass_bit_inverted) 91163152cbSTony Lindgren return !(v & FAPLL_MAIN_BP); 92163152cbSTony Lindgren else 93163152cbSTony Lindgren return !!(v & FAPLL_MAIN_BP); 94163152cbSTony Lindgren } 95163152cbSTony Lindgren 969089848dSTony Lindgren static void ti_fapll_set_bypass(struct fapll_data *fd) 979089848dSTony Lindgren { 989089848dSTony Lindgren u32 v = readl_relaxed(fd->base); 999089848dSTony Lindgren 1009089848dSTony Lindgren if (fd->bypass_bit_inverted) 1019089848dSTony Lindgren v &= ~FAPLL_MAIN_BP; 1029089848dSTony Lindgren else 1039089848dSTony Lindgren v |= FAPLL_MAIN_BP; 1049089848dSTony Lindgren writel_relaxed(v, fd->base); 1059089848dSTony Lindgren } 1069089848dSTony Lindgren 1079089848dSTony Lindgren static void ti_fapll_clear_bypass(struct fapll_data *fd) 1089089848dSTony Lindgren { 1099089848dSTony Lindgren u32 v = readl_relaxed(fd->base); 1109089848dSTony Lindgren 1119089848dSTony Lindgren if (fd->bypass_bit_inverted) 1129089848dSTony Lindgren v |= FAPLL_MAIN_BP; 1139089848dSTony Lindgren else 1149089848dSTony Lindgren v &= ~FAPLL_MAIN_BP; 1159089848dSTony Lindgren writel_relaxed(v, fd->base); 1169089848dSTony Lindgren } 1179089848dSTony Lindgren 1189089848dSTony Lindgren static int ti_fapll_wait_lock(struct fapll_data *fd) 1199089848dSTony Lindgren { 1209089848dSTony Lindgren int retries = FAPLL_MAX_RETRIES; 1219089848dSTony Lindgren u32 v; 1229089848dSTony Lindgren 1239089848dSTony Lindgren while ((v = readl_relaxed(fd->base))) { 1249089848dSTony Lindgren if (v & FAPLL_MAIN_LOCK) 1259089848dSTony Lindgren return 0; 1269089848dSTony Lindgren 1279089848dSTony Lindgren if (retries-- <= 0) 1289089848dSTony Lindgren break; 1299089848dSTony Lindgren 1309089848dSTony Lindgren udelay(1); 1319089848dSTony Lindgren } 1329089848dSTony Lindgren 1339089848dSTony Lindgren pr_err("%s failed to lock\n", fd->name); 1349089848dSTony Lindgren 1359089848dSTony Lindgren return -ETIMEDOUT; 1369089848dSTony Lindgren } 1379089848dSTony Lindgren 138163152cbSTony Lindgren static int ti_fapll_enable(struct clk_hw *hw) 139163152cbSTony Lindgren { 140163152cbSTony Lindgren struct fapll_data *fd = to_fapll(hw); 141163152cbSTony Lindgren u32 v = readl_relaxed(fd->base); 142163152cbSTony Lindgren 14303208cc6STony Lindgren v |= FAPLL_MAIN_PLLEN; 144163152cbSTony Lindgren writel_relaxed(v, fd->base); 1459089848dSTony Lindgren ti_fapll_wait_lock(fd); 146163152cbSTony Lindgren 147163152cbSTony Lindgren return 0; 148163152cbSTony Lindgren } 149163152cbSTony Lindgren 150163152cbSTony Lindgren static void ti_fapll_disable(struct clk_hw *hw) 151163152cbSTony Lindgren { 152163152cbSTony Lindgren struct fapll_data *fd = to_fapll(hw); 153163152cbSTony Lindgren u32 v = readl_relaxed(fd->base); 154163152cbSTony Lindgren 15503208cc6STony Lindgren v &= ~FAPLL_MAIN_PLLEN; 156163152cbSTony Lindgren writel_relaxed(v, fd->base); 157163152cbSTony Lindgren } 158163152cbSTony Lindgren 159163152cbSTony Lindgren static int ti_fapll_is_enabled(struct clk_hw *hw) 160163152cbSTony Lindgren { 161163152cbSTony Lindgren struct fapll_data *fd = to_fapll(hw); 162163152cbSTony Lindgren u32 v = readl_relaxed(fd->base); 163163152cbSTony Lindgren 16403208cc6STony Lindgren return v & FAPLL_MAIN_PLLEN; 165163152cbSTony Lindgren } 166163152cbSTony Lindgren 167163152cbSTony Lindgren static unsigned long ti_fapll_recalc_rate(struct clk_hw *hw, 168163152cbSTony Lindgren unsigned long parent_rate) 169163152cbSTony Lindgren { 170163152cbSTony Lindgren struct fapll_data *fd = to_fapll(hw); 171163152cbSTony Lindgren u32 fapll_n, fapll_p, v; 172c51185b4SNicolas Pitre u64 rate; 173163152cbSTony Lindgren 174163152cbSTony Lindgren if (ti_fapll_clock_is_bypass(fd)) 175163152cbSTony Lindgren return parent_rate; 176163152cbSTony Lindgren 177163152cbSTony Lindgren rate = parent_rate; 178163152cbSTony Lindgren 179163152cbSTony Lindgren /* PLL pre-divider is P and multiplier is N */ 180163152cbSTony Lindgren v = readl_relaxed(fd->base); 181163152cbSTony Lindgren fapll_p = (v >> 8) & 0xff; 182163152cbSTony Lindgren if (fapll_p) 183163152cbSTony Lindgren do_div(rate, fapll_p); 184163152cbSTony Lindgren fapll_n = v >> 16; 185163152cbSTony Lindgren if (fapll_n) 186163152cbSTony Lindgren rate *= fapll_n; 187163152cbSTony Lindgren 188163152cbSTony Lindgren return rate; 189163152cbSTony Lindgren } 190163152cbSTony Lindgren 191163152cbSTony Lindgren static u8 ti_fapll_get_parent(struct clk_hw *hw) 192163152cbSTony Lindgren { 193163152cbSTony Lindgren struct fapll_data *fd = to_fapll(hw); 194163152cbSTony Lindgren 195163152cbSTony Lindgren if (ti_fapll_clock_is_bypass(fd)) 196163152cbSTony Lindgren return 1; 197163152cbSTony Lindgren 198163152cbSTony Lindgren return 0; 199163152cbSTony Lindgren } 200163152cbSTony Lindgren 2019089848dSTony Lindgren static int ti_fapll_set_div_mult(unsigned long rate, 2029089848dSTony Lindgren unsigned long parent_rate, 2039089848dSTony Lindgren u32 *pre_div_p, u32 *mult_n) 2049089848dSTony Lindgren { 2059089848dSTony Lindgren /* 2069089848dSTony Lindgren * So far no luck getting decent clock with PLL divider, 2079089848dSTony Lindgren * PLL does not seem to lock and the signal does not look 2089089848dSTony Lindgren * right. It seems the divider can only be used together 2099089848dSTony Lindgren * with the multiplier? 2109089848dSTony Lindgren */ 2119089848dSTony Lindgren if (rate < parent_rate) { 2129089848dSTony Lindgren pr_warn("FAPLL main divider rates unsupported\n"); 2139089848dSTony Lindgren return -EINVAL; 2149089848dSTony Lindgren } 2159089848dSTony Lindgren 2169089848dSTony Lindgren *mult_n = rate / parent_rate; 2179089848dSTony Lindgren if (*mult_n > FAPLL_MAIN_MAX_MULT_N) 2189089848dSTony Lindgren return -EINVAL; 2199089848dSTony Lindgren *pre_div_p = 1; 2209089848dSTony Lindgren 2219089848dSTony Lindgren return 0; 2229089848dSTony Lindgren } 2239089848dSTony Lindgren 2249089848dSTony Lindgren static long ti_fapll_round_rate(struct clk_hw *hw, unsigned long rate, 2259089848dSTony Lindgren unsigned long *parent_rate) 2269089848dSTony Lindgren { 2279089848dSTony Lindgren u32 pre_div_p, mult_n; 2289089848dSTony Lindgren int error; 2299089848dSTony Lindgren 2309089848dSTony Lindgren if (!rate) 2319089848dSTony Lindgren return -EINVAL; 2329089848dSTony Lindgren 2339089848dSTony Lindgren error = ti_fapll_set_div_mult(rate, *parent_rate, 2349089848dSTony Lindgren &pre_div_p, &mult_n); 2359089848dSTony Lindgren if (error) 2369089848dSTony Lindgren return error; 2379089848dSTony Lindgren 2389089848dSTony Lindgren rate = *parent_rate / pre_div_p; 2399089848dSTony Lindgren rate *= mult_n; 2409089848dSTony Lindgren 2419089848dSTony Lindgren return rate; 2429089848dSTony Lindgren } 2439089848dSTony Lindgren 2449089848dSTony Lindgren static int ti_fapll_set_rate(struct clk_hw *hw, unsigned long rate, 2459089848dSTony Lindgren unsigned long parent_rate) 2469089848dSTony Lindgren { 2479089848dSTony Lindgren struct fapll_data *fd = to_fapll(hw); 2489089848dSTony Lindgren u32 pre_div_p, mult_n, v; 2499089848dSTony Lindgren int error; 2509089848dSTony Lindgren 2519089848dSTony Lindgren if (!rate) 2529089848dSTony Lindgren return -EINVAL; 2539089848dSTony Lindgren 2549089848dSTony Lindgren error = ti_fapll_set_div_mult(rate, parent_rate, 2559089848dSTony Lindgren &pre_div_p, &mult_n); 2569089848dSTony Lindgren if (error) 2579089848dSTony Lindgren return error; 2589089848dSTony Lindgren 2599089848dSTony Lindgren ti_fapll_set_bypass(fd); 2609089848dSTony Lindgren v = readl_relaxed(fd->base); 2619089848dSTony Lindgren v &= ~FAPLL_MAIN_CLEAR_MASK; 2629089848dSTony Lindgren v |= pre_div_p << FAPLL_MAIN_DIV_P_SHIFT; 2639089848dSTony Lindgren v |= mult_n << FAPLL_MAIN_MULT_N_SHIFT; 2649089848dSTony Lindgren writel_relaxed(v, fd->base); 2659089848dSTony Lindgren if (ti_fapll_is_enabled(hw)) 2669089848dSTony Lindgren ti_fapll_wait_lock(fd); 2679089848dSTony Lindgren ti_fapll_clear_bypass(fd); 2689089848dSTony Lindgren 2699089848dSTony Lindgren return 0; 2709089848dSTony Lindgren } 2719089848dSTony Lindgren 2727cc566a8SBhumika Goyal static const struct clk_ops ti_fapll_ops = { 273163152cbSTony Lindgren .enable = ti_fapll_enable, 274163152cbSTony Lindgren .disable = ti_fapll_disable, 275163152cbSTony Lindgren .is_enabled = ti_fapll_is_enabled, 276163152cbSTony Lindgren .recalc_rate = ti_fapll_recalc_rate, 277163152cbSTony Lindgren .get_parent = ti_fapll_get_parent, 2789089848dSTony Lindgren .round_rate = ti_fapll_round_rate, 2799089848dSTony Lindgren .set_rate = ti_fapll_set_rate, 280163152cbSTony Lindgren }; 281163152cbSTony Lindgren 282163152cbSTony Lindgren static int ti_fapll_synth_enable(struct clk_hw *hw) 283163152cbSTony Lindgren { 284163152cbSTony Lindgren struct fapll_synth *synth = to_synth(hw); 285163152cbSTony Lindgren u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET); 286163152cbSTony Lindgren 287163152cbSTony Lindgren v &= ~(1 << synth->index); 288163152cbSTony Lindgren writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET); 289163152cbSTony Lindgren 290163152cbSTony Lindgren return 0; 291163152cbSTony Lindgren } 292163152cbSTony Lindgren 293163152cbSTony Lindgren static void ti_fapll_synth_disable(struct clk_hw *hw) 294163152cbSTony Lindgren { 295163152cbSTony Lindgren struct fapll_synth *synth = to_synth(hw); 296163152cbSTony Lindgren u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET); 297163152cbSTony Lindgren 298163152cbSTony Lindgren v |= 1 << synth->index; 299163152cbSTony Lindgren writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET); 300163152cbSTony Lindgren } 301163152cbSTony Lindgren 302163152cbSTony Lindgren static int ti_fapll_synth_is_enabled(struct clk_hw *hw) 303163152cbSTony Lindgren { 304163152cbSTony Lindgren struct fapll_synth *synth = to_synth(hw); 305163152cbSTony Lindgren u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET); 306163152cbSTony Lindgren 307163152cbSTony Lindgren return !(v & (1 << synth->index)); 308163152cbSTony Lindgren } 309163152cbSTony Lindgren 310163152cbSTony Lindgren /* 311163152cbSTony Lindgren * See dm816x TRM chapter 1.10.3 Flying Adder PLL fore more info 312163152cbSTony Lindgren */ 313163152cbSTony Lindgren static unsigned long ti_fapll_synth_recalc_rate(struct clk_hw *hw, 314163152cbSTony Lindgren unsigned long parent_rate) 315163152cbSTony Lindgren { 316163152cbSTony Lindgren struct fapll_synth *synth = to_synth(hw); 317163152cbSTony Lindgren u32 synth_div_m; 318c51185b4SNicolas Pitre u64 rate; 319163152cbSTony Lindgren 320163152cbSTony Lindgren /* The audio_pll_clk1 is hardwired to produce 32.768KiHz clock */ 321163152cbSTony Lindgren if (!synth->div) 322163152cbSTony Lindgren return 32768; 323163152cbSTony Lindgren 324163152cbSTony Lindgren /* 325163152cbSTony Lindgren * PLL in bypass sets the synths in bypass mode too. The PLL rate 326163152cbSTony Lindgren * can be also be set to 27MHz, so we can't use parent_rate to 327163152cbSTony Lindgren * check for bypass mode. 328163152cbSTony Lindgren */ 329163152cbSTony Lindgren if (ti_fapll_clock_is_bypass(synth->fd)) 330163152cbSTony Lindgren return parent_rate; 331163152cbSTony Lindgren 332163152cbSTony Lindgren rate = parent_rate; 333163152cbSTony Lindgren 334163152cbSTony Lindgren /* 335163152cbSTony Lindgren * Synth frequency integer and fractional divider. 336163152cbSTony Lindgren * Note that the phase output K is 8, so the result needs 337cafeb002STony Lindgren * to be multiplied by SYNTH_PHASE_K. 338163152cbSTony Lindgren */ 339163152cbSTony Lindgren if (synth->freq) { 340163152cbSTony Lindgren u32 v, synth_int_div, synth_frac_div, synth_div_freq; 341163152cbSTony Lindgren 342163152cbSTony Lindgren v = readl_relaxed(synth->freq); 343163152cbSTony Lindgren synth_int_div = (v >> 24) & 0xf; 344163152cbSTony Lindgren synth_frac_div = v & 0xffffff; 345163152cbSTony Lindgren synth_div_freq = (synth_int_div * 10000000) + synth_frac_div; 346163152cbSTony Lindgren rate *= 10000000; 347163152cbSTony Lindgren do_div(rate, synth_div_freq); 348cafeb002STony Lindgren rate *= SYNTH_PHASE_K; 349163152cbSTony Lindgren } 350163152cbSTony Lindgren 35133ca29c9STony Lindgren /* Synth post-divider M */ 35233ca29c9STony Lindgren synth_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M; 353163152cbSTony Lindgren 35433ca29c9STony Lindgren return DIV_ROUND_UP_ULL(rate, synth_div_m); 355163152cbSTony Lindgren } 356163152cbSTony Lindgren 357cafeb002STony Lindgren static unsigned long ti_fapll_synth_get_frac_rate(struct clk_hw *hw, 358cafeb002STony Lindgren unsigned long parent_rate) 359cafeb002STony Lindgren { 360cafeb002STony Lindgren struct fapll_synth *synth = to_synth(hw); 361cafeb002STony Lindgren unsigned long current_rate, frac_rate; 362cafeb002STony Lindgren u32 post_div_m; 363cafeb002STony Lindgren 364cafeb002STony Lindgren current_rate = ti_fapll_synth_recalc_rate(hw, parent_rate); 365cafeb002STony Lindgren post_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M; 366cafeb002STony Lindgren frac_rate = current_rate * post_div_m; 367cafeb002STony Lindgren 368cafeb002STony Lindgren return frac_rate; 369cafeb002STony Lindgren } 370cafeb002STony Lindgren 371cafeb002STony Lindgren static u32 ti_fapll_synth_set_frac_rate(struct fapll_synth *synth, 372cafeb002STony Lindgren unsigned long rate, 373cafeb002STony Lindgren unsigned long parent_rate) 374cafeb002STony Lindgren { 375cafeb002STony Lindgren u32 post_div_m, synth_int_div = 0, synth_frac_div = 0, v; 376cafeb002STony Lindgren 377cafeb002STony Lindgren post_div_m = DIV_ROUND_UP_ULL((u64)parent_rate * SYNTH_PHASE_K, rate); 378cafeb002STony Lindgren post_div_m = post_div_m / SYNTH_MAX_INT_DIV; 379cafeb002STony Lindgren if (post_div_m > SYNTH_MAX_DIV_M) 380cafeb002STony Lindgren return -EINVAL; 381cafeb002STony Lindgren if (!post_div_m) 382cafeb002STony Lindgren post_div_m = 1; 383cafeb002STony Lindgren 384cafeb002STony Lindgren for (; post_div_m < SYNTH_MAX_DIV_M; post_div_m++) { 385cafeb002STony Lindgren synth_int_div = DIV_ROUND_UP_ULL((u64)parent_rate * 386cafeb002STony Lindgren SYNTH_PHASE_K * 387cafeb002STony Lindgren 10000000, 388cafeb002STony Lindgren rate * post_div_m); 389cafeb002STony Lindgren synth_frac_div = synth_int_div % 10000000; 390cafeb002STony Lindgren synth_int_div /= 10000000; 391cafeb002STony Lindgren 392cafeb002STony Lindgren if (synth_int_div <= SYNTH_MAX_INT_DIV) 393cafeb002STony Lindgren break; 394cafeb002STony Lindgren } 395cafeb002STony Lindgren 396cafeb002STony Lindgren if (synth_int_div > SYNTH_MAX_INT_DIV) 397cafeb002STony Lindgren return -EINVAL; 398cafeb002STony Lindgren 399cafeb002STony Lindgren v = readl_relaxed(synth->freq); 400cafeb002STony Lindgren v &= ~0x1fffffff; 401cafeb002STony Lindgren v |= (synth_int_div & SYNTH_MAX_INT_DIV) << 24; 402cafeb002STony Lindgren v |= (synth_frac_div & 0xffffff); 403cafeb002STony Lindgren v |= SYNTH_LDFREQ; 404cafeb002STony Lindgren writel_relaxed(v, synth->freq); 405cafeb002STony Lindgren 406cafeb002STony Lindgren return post_div_m; 407cafeb002STony Lindgren } 408cafeb002STony Lindgren 409cafeb002STony Lindgren static long ti_fapll_synth_round_rate(struct clk_hw *hw, unsigned long rate, 410cafeb002STony Lindgren unsigned long *parent_rate) 411cafeb002STony Lindgren { 412cafeb002STony Lindgren struct fapll_synth *synth = to_synth(hw); 413cafeb002STony Lindgren struct fapll_data *fd = synth->fd; 414cafeb002STony Lindgren unsigned long r; 415cafeb002STony Lindgren 416cafeb002STony Lindgren if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate) 417cafeb002STony Lindgren return -EINVAL; 418cafeb002STony Lindgren 419cafeb002STony Lindgren /* Only post divider m available with no fractional divider? */ 420cafeb002STony Lindgren if (!synth->freq) { 421cafeb002STony Lindgren unsigned long frac_rate; 422cafeb002STony Lindgren u32 synth_post_div_m; 423cafeb002STony Lindgren 424cafeb002STony Lindgren frac_rate = ti_fapll_synth_get_frac_rate(hw, *parent_rate); 425cafeb002STony Lindgren synth_post_div_m = DIV_ROUND_UP(frac_rate, rate); 426cafeb002STony Lindgren r = DIV_ROUND_UP(frac_rate, synth_post_div_m); 427cafeb002STony Lindgren goto out; 428cafeb002STony Lindgren } 429cafeb002STony Lindgren 430cafeb002STony Lindgren r = *parent_rate * SYNTH_PHASE_K; 431cafeb002STony Lindgren if (rate > r) 432cafeb002STony Lindgren goto out; 433cafeb002STony Lindgren 434cafeb002STony Lindgren r = DIV_ROUND_UP_ULL(r, SYNTH_MAX_INT_DIV * SYNTH_MAX_DIV_M); 435cafeb002STony Lindgren if (rate < r) 436cafeb002STony Lindgren goto out; 437cafeb002STony Lindgren 438cafeb002STony Lindgren r = rate; 439cafeb002STony Lindgren out: 440cafeb002STony Lindgren return r; 441cafeb002STony Lindgren } 442cafeb002STony Lindgren 443cafeb002STony Lindgren static int ti_fapll_synth_set_rate(struct clk_hw *hw, unsigned long rate, 444cafeb002STony Lindgren unsigned long parent_rate) 445cafeb002STony Lindgren { 446cafeb002STony Lindgren struct fapll_synth *synth = to_synth(hw); 447cafeb002STony Lindgren struct fapll_data *fd = synth->fd; 448cafeb002STony Lindgren unsigned long frac_rate, post_rate = 0; 449cafeb002STony Lindgren u32 post_div_m = 0, v; 450cafeb002STony Lindgren 451cafeb002STony Lindgren if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate) 452cafeb002STony Lindgren return -EINVAL; 453cafeb002STony Lindgren 454cafeb002STony Lindgren /* Produce the rate with just post divider M? */ 455cafeb002STony Lindgren frac_rate = ti_fapll_synth_get_frac_rate(hw, parent_rate); 456cafeb002STony Lindgren if (frac_rate < rate) { 457cafeb002STony Lindgren if (!synth->freq) 458cafeb002STony Lindgren return -EINVAL; 459cafeb002STony Lindgren } else { 460cafeb002STony Lindgren post_div_m = DIV_ROUND_UP(frac_rate, rate); 461cafeb002STony Lindgren if (post_div_m && (post_div_m <= SYNTH_MAX_DIV_M)) 462cafeb002STony Lindgren post_rate = DIV_ROUND_UP(frac_rate, post_div_m); 463cafeb002STony Lindgren if (!synth->freq && !post_rate) 464cafeb002STony Lindgren return -EINVAL; 465cafeb002STony Lindgren } 466cafeb002STony Lindgren 467cafeb002STony Lindgren /* Need to recalculate the fractional divider? */ 468cafeb002STony Lindgren if ((post_rate != rate) && synth->freq) 469cafeb002STony Lindgren post_div_m = ti_fapll_synth_set_frac_rate(synth, 470cafeb002STony Lindgren rate, 471cafeb002STony Lindgren parent_rate); 472cafeb002STony Lindgren 473cafeb002STony Lindgren v = readl_relaxed(synth->div); 474cafeb002STony Lindgren v &= ~SYNTH_MAX_DIV_M; 475cafeb002STony Lindgren v |= post_div_m; 476cafeb002STony Lindgren v |= SYNTH_LDMDIV1; 477cafeb002STony Lindgren writel_relaxed(v, synth->div); 478cafeb002STony Lindgren 479cafeb002STony Lindgren return 0; 480163152cbSTony Lindgren } 481163152cbSTony Lindgren 4827cc566a8SBhumika Goyal static const struct clk_ops ti_fapll_synt_ops = { 483163152cbSTony Lindgren .enable = ti_fapll_synth_enable, 484163152cbSTony Lindgren .disable = ti_fapll_synth_disable, 485163152cbSTony Lindgren .is_enabled = ti_fapll_synth_is_enabled, 486163152cbSTony Lindgren .recalc_rate = ti_fapll_synth_recalc_rate, 487cafeb002STony Lindgren .round_rate = ti_fapll_synth_round_rate, 488cafeb002STony Lindgren .set_rate = ti_fapll_synth_set_rate, 489163152cbSTony Lindgren }; 490163152cbSTony Lindgren 491163152cbSTony Lindgren static struct clk * __init ti_fapll_synth_setup(struct fapll_data *fd, 492163152cbSTony Lindgren void __iomem *freq, 493163152cbSTony Lindgren void __iomem *div, 494163152cbSTony Lindgren int index, 495163152cbSTony Lindgren const char *name, 496163152cbSTony Lindgren const char *parent, 497163152cbSTony Lindgren struct clk *pll_clk) 498163152cbSTony Lindgren { 499163152cbSTony Lindgren struct clk_init_data *init; 500163152cbSTony Lindgren struct fapll_synth *synth; 501*8c6239f6SZhang Qilong struct clk *clk = ERR_PTR(-ENOMEM); 502163152cbSTony Lindgren 503163152cbSTony Lindgren init = kzalloc(sizeof(*init), GFP_KERNEL); 504163152cbSTony Lindgren if (!init) 505163152cbSTony Lindgren return ERR_PTR(-ENOMEM); 506163152cbSTony Lindgren 507163152cbSTony Lindgren init->ops = &ti_fapll_synt_ops; 508163152cbSTony Lindgren init->name = name; 509163152cbSTony Lindgren init->parent_names = &parent; 510163152cbSTony Lindgren init->num_parents = 1; 511163152cbSTony Lindgren 512163152cbSTony Lindgren synth = kzalloc(sizeof(*synth), GFP_KERNEL); 513163152cbSTony Lindgren if (!synth) 514163152cbSTony Lindgren goto free; 515163152cbSTony Lindgren 516163152cbSTony Lindgren synth->fd = fd; 517163152cbSTony Lindgren synth->index = index; 518163152cbSTony Lindgren synth->freq = freq; 519163152cbSTony Lindgren synth->div = div; 520163152cbSTony Lindgren synth->name = name; 521163152cbSTony Lindgren synth->hw.init = init; 522163152cbSTony Lindgren synth->clk_pll = pll_clk; 523163152cbSTony Lindgren 524*8c6239f6SZhang Qilong clk = clk_register(NULL, &synth->hw); 525*8c6239f6SZhang Qilong if (IS_ERR(clk)) { 526*8c6239f6SZhang Qilong pr_err("failed to register clock\n"); 527*8c6239f6SZhang Qilong goto free; 528*8c6239f6SZhang Qilong } 529*8c6239f6SZhang Qilong 530*8c6239f6SZhang Qilong return clk; 531163152cbSTony Lindgren 532163152cbSTony Lindgren free: 533163152cbSTony Lindgren kfree(synth); 534163152cbSTony Lindgren kfree(init); 535163152cbSTony Lindgren 536*8c6239f6SZhang Qilong return clk; 537163152cbSTony Lindgren } 538163152cbSTony Lindgren 539163152cbSTony Lindgren static void __init ti_fapll_setup(struct device_node *node) 540163152cbSTony Lindgren { 541163152cbSTony Lindgren struct fapll_data *fd; 542163152cbSTony Lindgren struct clk_init_data *init = NULL; 543163152cbSTony Lindgren const char *parent_name[2]; 544163152cbSTony Lindgren struct clk *pll_clk; 545163152cbSTony Lindgren int i; 546163152cbSTony Lindgren 547163152cbSTony Lindgren fd = kzalloc(sizeof(*fd), GFP_KERNEL); 548163152cbSTony Lindgren if (!fd) 549163152cbSTony Lindgren return; 550163152cbSTony Lindgren 551163152cbSTony Lindgren fd->outputs.clks = kzalloc(sizeof(struct clk *) * 552163152cbSTony Lindgren MAX_FAPLL_OUTPUTS + 1, 553163152cbSTony Lindgren GFP_KERNEL); 554163152cbSTony Lindgren if (!fd->outputs.clks) 555163152cbSTony Lindgren goto free; 556163152cbSTony Lindgren 557163152cbSTony Lindgren init = kzalloc(sizeof(*init), GFP_KERNEL); 558163152cbSTony Lindgren if (!init) 559163152cbSTony Lindgren goto free; 560163152cbSTony Lindgren 561163152cbSTony Lindgren init->ops = &ti_fapll_ops; 562163152cbSTony Lindgren init->name = node->name; 563163152cbSTony Lindgren 564163152cbSTony Lindgren init->num_parents = of_clk_get_parent_count(node); 565163152cbSTony Lindgren if (init->num_parents != 2) { 566e665f029SRob Herring pr_err("%pOFn must have two parents\n", node); 567163152cbSTony Lindgren goto free; 568163152cbSTony Lindgren } 569163152cbSTony Lindgren 5709da9e761SDinh Nguyen of_clk_parent_fill(node, parent_name, 2); 571163152cbSTony Lindgren init->parent_names = parent_name; 572163152cbSTony Lindgren 573163152cbSTony Lindgren fd->clk_ref = of_clk_get(node, 0); 574163152cbSTony Lindgren if (IS_ERR(fd->clk_ref)) { 575e665f029SRob Herring pr_err("%pOFn could not get clk_ref\n", node); 576163152cbSTony Lindgren goto free; 577163152cbSTony Lindgren } 578163152cbSTony Lindgren 579163152cbSTony Lindgren fd->clk_bypass = of_clk_get(node, 1); 580163152cbSTony Lindgren if (IS_ERR(fd->clk_bypass)) { 581e665f029SRob Herring pr_err("%pOFn could not get clk_bypass\n", node); 582163152cbSTony Lindgren goto free; 583163152cbSTony Lindgren } 584163152cbSTony Lindgren 585163152cbSTony Lindgren fd->base = of_iomap(node, 0); 586163152cbSTony Lindgren if (!fd->base) { 587e665f029SRob Herring pr_err("%pOFn could not get IO base\n", node); 588163152cbSTony Lindgren goto free; 589163152cbSTony Lindgren } 590163152cbSTony Lindgren 591163152cbSTony Lindgren if (fapll_is_ddr_pll(fd->base)) 592163152cbSTony Lindgren fd->bypass_bit_inverted = true; 593163152cbSTony Lindgren 594163152cbSTony Lindgren fd->name = node->name; 595163152cbSTony Lindgren fd->hw.init = init; 596163152cbSTony Lindgren 597163152cbSTony Lindgren /* Register the parent PLL */ 598163152cbSTony Lindgren pll_clk = clk_register(NULL, &fd->hw); 599163152cbSTony Lindgren if (IS_ERR(pll_clk)) 600163152cbSTony Lindgren goto unmap; 601163152cbSTony Lindgren 602163152cbSTony Lindgren fd->outputs.clks[0] = pll_clk; 603163152cbSTony Lindgren fd->outputs.clk_num++; 604163152cbSTony Lindgren 605163152cbSTony Lindgren /* 606163152cbSTony Lindgren * Set up the child synthesizers starting at index 1 as the 607163152cbSTony Lindgren * PLL output is at index 0. We need to check the clock-indices 608163152cbSTony Lindgren * for numbering in case there are holes in the synth mapping, 609163152cbSTony Lindgren * and then probe the synth register to see if it has a FREQ 610163152cbSTony Lindgren * register available. 611163152cbSTony Lindgren */ 612163152cbSTony Lindgren for (i = 0; i < MAX_FAPLL_OUTPUTS; i++) { 613163152cbSTony Lindgren const char *output_name; 614163152cbSTony Lindgren void __iomem *freq, *div; 615163152cbSTony Lindgren struct clk *synth_clk; 616163152cbSTony Lindgren int output_instance; 617163152cbSTony Lindgren u32 v; 618163152cbSTony Lindgren 619163152cbSTony Lindgren if (of_property_read_string_index(node, "clock-output-names", 620163152cbSTony Lindgren i, &output_name)) 621163152cbSTony Lindgren continue; 622163152cbSTony Lindgren 623163152cbSTony Lindgren if (of_property_read_u32_index(node, "clock-indices", i, 624163152cbSTony Lindgren &output_instance)) 625163152cbSTony Lindgren output_instance = i; 626163152cbSTony Lindgren 627163152cbSTony Lindgren freq = fd->base + (output_instance * 8); 628163152cbSTony Lindgren div = freq + 4; 629163152cbSTony Lindgren 630163152cbSTony Lindgren /* Check for hardwired audio_pll_clk1 */ 631163152cbSTony Lindgren if (is_audio_pll_clk1(freq)) { 632412d6b47SStephen Boyd freq = NULL; 633412d6b47SStephen Boyd div = NULL; 634163152cbSTony Lindgren } else { 635163152cbSTony Lindgren /* Does the synthesizer have a FREQ register? */ 636163152cbSTony Lindgren v = readl_relaxed(freq); 637163152cbSTony Lindgren if (!v) 638412d6b47SStephen Boyd freq = NULL; 639163152cbSTony Lindgren } 640163152cbSTony Lindgren synth_clk = ti_fapll_synth_setup(fd, freq, div, output_instance, 641163152cbSTony Lindgren output_name, node->name, 642163152cbSTony Lindgren pll_clk); 643163152cbSTony Lindgren if (IS_ERR(synth_clk)) 644163152cbSTony Lindgren continue; 645163152cbSTony Lindgren 646163152cbSTony Lindgren fd->outputs.clks[output_instance] = synth_clk; 647163152cbSTony Lindgren fd->outputs.clk_num++; 648163152cbSTony Lindgren 649163152cbSTony Lindgren clk_register_clkdev(synth_clk, output_name, NULL); 650163152cbSTony Lindgren } 651163152cbSTony Lindgren 652163152cbSTony Lindgren /* Register the child synthesizers as the FAPLL outputs */ 653163152cbSTony Lindgren of_clk_add_provider(node, of_clk_src_onecell_get, &fd->outputs); 654163152cbSTony Lindgren /* Add clock alias for the outputs */ 655163152cbSTony Lindgren 656163152cbSTony Lindgren kfree(init); 657163152cbSTony Lindgren 658163152cbSTony Lindgren return; 659163152cbSTony Lindgren 660163152cbSTony Lindgren unmap: 661163152cbSTony Lindgren iounmap(fd->base); 662163152cbSTony Lindgren free: 663163152cbSTony Lindgren if (fd->clk_bypass) 664163152cbSTony Lindgren clk_put(fd->clk_bypass); 665163152cbSTony Lindgren if (fd->clk_ref) 666163152cbSTony Lindgren clk_put(fd->clk_ref); 667163152cbSTony Lindgren kfree(fd->outputs.clks); 668163152cbSTony Lindgren kfree(fd); 669163152cbSTony Lindgren kfree(init); 670163152cbSTony Lindgren } 671163152cbSTony Lindgren 672163152cbSTony Lindgren CLK_OF_DECLARE(ti_fapll_clock, "ti,dm816-fapll-clock", ti_fapll_setup); 673