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 22*9e56a7d4STony Lindgren #include "clock.h" 23*9e56a7d4STony Lindgren 24163152cbSTony Lindgren /* FAPLL Control Register PLL_CTRL */ 259089848dSTony Lindgren #define FAPLL_MAIN_MULT_N_SHIFT 16 269089848dSTony Lindgren #define FAPLL_MAIN_DIV_P_SHIFT 8 27163152cbSTony Lindgren #define FAPLL_MAIN_LOCK BIT(7) 28163152cbSTony Lindgren #define FAPLL_MAIN_PLLEN BIT(3) 29163152cbSTony Lindgren #define FAPLL_MAIN_BP BIT(2) 30163152cbSTony Lindgren #define FAPLL_MAIN_LOC_CTL BIT(0) 31163152cbSTony Lindgren 329089848dSTony Lindgren #define FAPLL_MAIN_MAX_MULT_N 0xffff 339089848dSTony Lindgren #define FAPLL_MAIN_MAX_DIV_P 0xff 349089848dSTony Lindgren #define FAPLL_MAIN_CLEAR_MASK \ 359089848dSTony Lindgren ((FAPLL_MAIN_MAX_MULT_N << FAPLL_MAIN_MULT_N_SHIFT) | \ 369089848dSTony Lindgren (FAPLL_MAIN_DIV_P_SHIFT << FAPLL_MAIN_DIV_P_SHIFT) | \ 379089848dSTony Lindgren FAPLL_MAIN_LOC_CTL) 389089848dSTony Lindgren 39163152cbSTony Lindgren /* FAPLL powerdown register PWD */ 40163152cbSTony Lindgren #define FAPLL_PWD_OFFSET 4 41163152cbSTony Lindgren 42163152cbSTony Lindgren #define MAX_FAPLL_OUTPUTS 7 43163152cbSTony Lindgren #define FAPLL_MAX_RETRIES 1000 44163152cbSTony Lindgren 45163152cbSTony Lindgren #define to_fapll(_hw) container_of(_hw, struct fapll_data, hw) 46163152cbSTony Lindgren #define to_synth(_hw) container_of(_hw, struct fapll_synth, hw) 47163152cbSTony Lindgren 48163152cbSTony Lindgren /* The bypass bit is inverted on the ddr_pll.. */ 49163152cbSTony Lindgren #define fapll_is_ddr_pll(va) (((u32)(va) & 0xffff) == 0x0440) 50163152cbSTony Lindgren 51163152cbSTony Lindgren /* 52163152cbSTony Lindgren * The audio_pll_clk1 input is hard wired to the 27MHz bypass clock, 53163152cbSTony Lindgren * and the audio_pll_clk1 synthesizer is hardwared to 32KiHz output. 54163152cbSTony Lindgren */ 55163152cbSTony Lindgren #define is_ddr_pll_clk1(va) (((u32)(va) & 0xffff) == 0x044c) 56163152cbSTony Lindgren #define is_audio_pll_clk1(va) (((u32)(va) & 0xffff) == 0x04a8) 57163152cbSTony Lindgren 58163152cbSTony Lindgren /* Synthesizer divider register */ 59163152cbSTony Lindgren #define SYNTH_LDMDIV1 BIT(8) 60163152cbSTony Lindgren 61163152cbSTony Lindgren /* Synthesizer frequency register */ 62163152cbSTony Lindgren #define SYNTH_LDFREQ BIT(31) 63163152cbSTony Lindgren 64cafeb002STony Lindgren #define SYNTH_PHASE_K 8 65cafeb002STony Lindgren #define SYNTH_MAX_INT_DIV 0xf 6633ca29c9STony Lindgren #define SYNTH_MAX_DIV_M 0xff 6733ca29c9STony Lindgren 68163152cbSTony Lindgren struct fapll_data { 69163152cbSTony Lindgren struct clk_hw hw; 70163152cbSTony Lindgren void __iomem *base; 71163152cbSTony Lindgren const char *name; 72163152cbSTony Lindgren struct clk *clk_ref; 73163152cbSTony Lindgren struct clk *clk_bypass; 74163152cbSTony Lindgren struct clk_onecell_data outputs; 75163152cbSTony Lindgren bool bypass_bit_inverted; 76163152cbSTony Lindgren }; 77163152cbSTony Lindgren 78163152cbSTony Lindgren struct fapll_synth { 79163152cbSTony Lindgren struct clk_hw hw; 80163152cbSTony Lindgren struct fapll_data *fd; 81163152cbSTony Lindgren int index; 82163152cbSTony Lindgren void __iomem *freq; 83163152cbSTony Lindgren void __iomem *div; 84163152cbSTony Lindgren const char *name; 85163152cbSTony Lindgren struct clk *clk_pll; 86163152cbSTony Lindgren }; 87163152cbSTony Lindgren 88163152cbSTony Lindgren static bool ti_fapll_clock_is_bypass(struct fapll_data *fd) 89163152cbSTony Lindgren { 90163152cbSTony Lindgren u32 v = readl_relaxed(fd->base); 91163152cbSTony Lindgren 92163152cbSTony Lindgren if (fd->bypass_bit_inverted) 93163152cbSTony Lindgren return !(v & FAPLL_MAIN_BP); 94163152cbSTony Lindgren else 95163152cbSTony Lindgren return !!(v & FAPLL_MAIN_BP); 96163152cbSTony Lindgren } 97163152cbSTony Lindgren 989089848dSTony Lindgren static void ti_fapll_set_bypass(struct fapll_data *fd) 999089848dSTony Lindgren { 1009089848dSTony Lindgren u32 v = readl_relaxed(fd->base); 1019089848dSTony Lindgren 1029089848dSTony Lindgren if (fd->bypass_bit_inverted) 1039089848dSTony Lindgren v &= ~FAPLL_MAIN_BP; 1049089848dSTony Lindgren else 1059089848dSTony Lindgren v |= FAPLL_MAIN_BP; 1069089848dSTony Lindgren writel_relaxed(v, fd->base); 1079089848dSTony Lindgren } 1089089848dSTony Lindgren 1099089848dSTony Lindgren static void ti_fapll_clear_bypass(struct fapll_data *fd) 1109089848dSTony Lindgren { 1119089848dSTony Lindgren u32 v = readl_relaxed(fd->base); 1129089848dSTony Lindgren 1139089848dSTony Lindgren if (fd->bypass_bit_inverted) 1149089848dSTony Lindgren v |= FAPLL_MAIN_BP; 1159089848dSTony Lindgren else 1169089848dSTony Lindgren v &= ~FAPLL_MAIN_BP; 1179089848dSTony Lindgren writel_relaxed(v, fd->base); 1189089848dSTony Lindgren } 1199089848dSTony Lindgren 1209089848dSTony Lindgren static int ti_fapll_wait_lock(struct fapll_data *fd) 1219089848dSTony Lindgren { 1229089848dSTony Lindgren int retries = FAPLL_MAX_RETRIES; 1239089848dSTony Lindgren u32 v; 1249089848dSTony Lindgren 1259089848dSTony Lindgren while ((v = readl_relaxed(fd->base))) { 1269089848dSTony Lindgren if (v & FAPLL_MAIN_LOCK) 1279089848dSTony Lindgren return 0; 1289089848dSTony Lindgren 1299089848dSTony Lindgren if (retries-- <= 0) 1309089848dSTony Lindgren break; 1319089848dSTony Lindgren 1329089848dSTony Lindgren udelay(1); 1339089848dSTony Lindgren } 1349089848dSTony Lindgren 1359089848dSTony Lindgren pr_err("%s failed to lock\n", fd->name); 1369089848dSTony Lindgren 1379089848dSTony Lindgren return -ETIMEDOUT; 1389089848dSTony Lindgren } 1399089848dSTony Lindgren 140163152cbSTony Lindgren static int ti_fapll_enable(struct clk_hw *hw) 141163152cbSTony Lindgren { 142163152cbSTony Lindgren struct fapll_data *fd = to_fapll(hw); 143163152cbSTony Lindgren u32 v = readl_relaxed(fd->base); 144163152cbSTony Lindgren 14503208cc6STony Lindgren v |= FAPLL_MAIN_PLLEN; 146163152cbSTony Lindgren writel_relaxed(v, fd->base); 1479089848dSTony Lindgren ti_fapll_wait_lock(fd); 148163152cbSTony Lindgren 149163152cbSTony Lindgren return 0; 150163152cbSTony Lindgren } 151163152cbSTony Lindgren 152163152cbSTony Lindgren static void ti_fapll_disable(struct clk_hw *hw) 153163152cbSTony Lindgren { 154163152cbSTony Lindgren struct fapll_data *fd = to_fapll(hw); 155163152cbSTony Lindgren u32 v = readl_relaxed(fd->base); 156163152cbSTony Lindgren 15703208cc6STony Lindgren v &= ~FAPLL_MAIN_PLLEN; 158163152cbSTony Lindgren writel_relaxed(v, fd->base); 159163152cbSTony Lindgren } 160163152cbSTony Lindgren 161163152cbSTony Lindgren static int ti_fapll_is_enabled(struct clk_hw *hw) 162163152cbSTony Lindgren { 163163152cbSTony Lindgren struct fapll_data *fd = to_fapll(hw); 164163152cbSTony Lindgren u32 v = readl_relaxed(fd->base); 165163152cbSTony Lindgren 16603208cc6STony Lindgren return v & FAPLL_MAIN_PLLEN; 167163152cbSTony Lindgren } 168163152cbSTony Lindgren 169163152cbSTony Lindgren static unsigned long ti_fapll_recalc_rate(struct clk_hw *hw, 170163152cbSTony Lindgren unsigned long parent_rate) 171163152cbSTony Lindgren { 172163152cbSTony Lindgren struct fapll_data *fd = to_fapll(hw); 173163152cbSTony Lindgren u32 fapll_n, fapll_p, v; 174c51185b4SNicolas Pitre u64 rate; 175163152cbSTony Lindgren 176163152cbSTony Lindgren if (ti_fapll_clock_is_bypass(fd)) 177163152cbSTony Lindgren return parent_rate; 178163152cbSTony Lindgren 179163152cbSTony Lindgren rate = parent_rate; 180163152cbSTony Lindgren 181163152cbSTony Lindgren /* PLL pre-divider is P and multiplier is N */ 182163152cbSTony Lindgren v = readl_relaxed(fd->base); 183163152cbSTony Lindgren fapll_p = (v >> 8) & 0xff; 184163152cbSTony Lindgren if (fapll_p) 185163152cbSTony Lindgren do_div(rate, fapll_p); 186163152cbSTony Lindgren fapll_n = v >> 16; 187163152cbSTony Lindgren if (fapll_n) 188163152cbSTony Lindgren rate *= fapll_n; 189163152cbSTony Lindgren 190163152cbSTony Lindgren return rate; 191163152cbSTony Lindgren } 192163152cbSTony Lindgren 193163152cbSTony Lindgren static u8 ti_fapll_get_parent(struct clk_hw *hw) 194163152cbSTony Lindgren { 195163152cbSTony Lindgren struct fapll_data *fd = to_fapll(hw); 196163152cbSTony Lindgren 197163152cbSTony Lindgren if (ti_fapll_clock_is_bypass(fd)) 198163152cbSTony Lindgren return 1; 199163152cbSTony Lindgren 200163152cbSTony Lindgren return 0; 201163152cbSTony Lindgren } 202163152cbSTony Lindgren 2039089848dSTony Lindgren static int ti_fapll_set_div_mult(unsigned long rate, 2049089848dSTony Lindgren unsigned long parent_rate, 2059089848dSTony Lindgren u32 *pre_div_p, u32 *mult_n) 2069089848dSTony Lindgren { 2079089848dSTony Lindgren /* 2089089848dSTony Lindgren * So far no luck getting decent clock with PLL divider, 2099089848dSTony Lindgren * PLL does not seem to lock and the signal does not look 2109089848dSTony Lindgren * right. It seems the divider can only be used together 2119089848dSTony Lindgren * with the multiplier? 2129089848dSTony Lindgren */ 2139089848dSTony Lindgren if (rate < parent_rate) { 2149089848dSTony Lindgren pr_warn("FAPLL main divider rates unsupported\n"); 2159089848dSTony Lindgren return -EINVAL; 2169089848dSTony Lindgren } 2179089848dSTony Lindgren 2189089848dSTony Lindgren *mult_n = rate / parent_rate; 2199089848dSTony Lindgren if (*mult_n > FAPLL_MAIN_MAX_MULT_N) 2209089848dSTony Lindgren return -EINVAL; 2219089848dSTony Lindgren *pre_div_p = 1; 2229089848dSTony Lindgren 2239089848dSTony Lindgren return 0; 2249089848dSTony Lindgren } 2259089848dSTony Lindgren 2269089848dSTony Lindgren static long ti_fapll_round_rate(struct clk_hw *hw, unsigned long rate, 2279089848dSTony Lindgren unsigned long *parent_rate) 2289089848dSTony Lindgren { 2299089848dSTony Lindgren u32 pre_div_p, mult_n; 2309089848dSTony Lindgren int error; 2319089848dSTony Lindgren 2329089848dSTony Lindgren if (!rate) 2339089848dSTony Lindgren return -EINVAL; 2349089848dSTony Lindgren 2359089848dSTony Lindgren error = ti_fapll_set_div_mult(rate, *parent_rate, 2369089848dSTony Lindgren &pre_div_p, &mult_n); 2379089848dSTony Lindgren if (error) 2389089848dSTony Lindgren return error; 2399089848dSTony Lindgren 2409089848dSTony Lindgren rate = *parent_rate / pre_div_p; 2419089848dSTony Lindgren rate *= mult_n; 2429089848dSTony Lindgren 2439089848dSTony Lindgren return rate; 2449089848dSTony Lindgren } 2459089848dSTony Lindgren 2469089848dSTony Lindgren static int ti_fapll_set_rate(struct clk_hw *hw, unsigned long rate, 2479089848dSTony Lindgren unsigned long parent_rate) 2489089848dSTony Lindgren { 2499089848dSTony Lindgren struct fapll_data *fd = to_fapll(hw); 2509089848dSTony Lindgren u32 pre_div_p, mult_n, v; 2519089848dSTony Lindgren int error; 2529089848dSTony Lindgren 2539089848dSTony Lindgren if (!rate) 2549089848dSTony Lindgren return -EINVAL; 2559089848dSTony Lindgren 2569089848dSTony Lindgren error = ti_fapll_set_div_mult(rate, parent_rate, 2579089848dSTony Lindgren &pre_div_p, &mult_n); 2589089848dSTony Lindgren if (error) 2599089848dSTony Lindgren return error; 2609089848dSTony Lindgren 2619089848dSTony Lindgren ti_fapll_set_bypass(fd); 2629089848dSTony Lindgren v = readl_relaxed(fd->base); 2639089848dSTony Lindgren v &= ~FAPLL_MAIN_CLEAR_MASK; 2649089848dSTony Lindgren v |= pre_div_p << FAPLL_MAIN_DIV_P_SHIFT; 2659089848dSTony Lindgren v |= mult_n << FAPLL_MAIN_MULT_N_SHIFT; 2669089848dSTony Lindgren writel_relaxed(v, fd->base); 2679089848dSTony Lindgren if (ti_fapll_is_enabled(hw)) 2689089848dSTony Lindgren ti_fapll_wait_lock(fd); 2699089848dSTony Lindgren ti_fapll_clear_bypass(fd); 2709089848dSTony Lindgren 2719089848dSTony Lindgren return 0; 2729089848dSTony Lindgren } 2739089848dSTony Lindgren 2747cc566a8SBhumika Goyal static const struct clk_ops ti_fapll_ops = { 275163152cbSTony Lindgren .enable = ti_fapll_enable, 276163152cbSTony Lindgren .disable = ti_fapll_disable, 277163152cbSTony Lindgren .is_enabled = ti_fapll_is_enabled, 278163152cbSTony Lindgren .recalc_rate = ti_fapll_recalc_rate, 279163152cbSTony Lindgren .get_parent = ti_fapll_get_parent, 2809089848dSTony Lindgren .round_rate = ti_fapll_round_rate, 2819089848dSTony Lindgren .set_rate = ti_fapll_set_rate, 282163152cbSTony Lindgren }; 283163152cbSTony Lindgren 284163152cbSTony Lindgren static int ti_fapll_synth_enable(struct clk_hw *hw) 285163152cbSTony Lindgren { 286163152cbSTony Lindgren struct fapll_synth *synth = to_synth(hw); 287163152cbSTony Lindgren u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET); 288163152cbSTony Lindgren 289163152cbSTony Lindgren v &= ~(1 << synth->index); 290163152cbSTony Lindgren writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET); 291163152cbSTony Lindgren 292163152cbSTony Lindgren return 0; 293163152cbSTony Lindgren } 294163152cbSTony Lindgren 295163152cbSTony Lindgren static void ti_fapll_synth_disable(struct clk_hw *hw) 296163152cbSTony Lindgren { 297163152cbSTony Lindgren struct fapll_synth *synth = to_synth(hw); 298163152cbSTony Lindgren u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET); 299163152cbSTony Lindgren 300163152cbSTony Lindgren v |= 1 << synth->index; 301163152cbSTony Lindgren writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET); 302163152cbSTony Lindgren } 303163152cbSTony Lindgren 304163152cbSTony Lindgren static int ti_fapll_synth_is_enabled(struct clk_hw *hw) 305163152cbSTony Lindgren { 306163152cbSTony Lindgren struct fapll_synth *synth = to_synth(hw); 307163152cbSTony Lindgren u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET); 308163152cbSTony Lindgren 309163152cbSTony Lindgren return !(v & (1 << synth->index)); 310163152cbSTony Lindgren } 311163152cbSTony Lindgren 312163152cbSTony Lindgren /* 313163152cbSTony Lindgren * See dm816x TRM chapter 1.10.3 Flying Adder PLL fore more info 314163152cbSTony Lindgren */ 315163152cbSTony Lindgren static unsigned long ti_fapll_synth_recalc_rate(struct clk_hw *hw, 316163152cbSTony Lindgren unsigned long parent_rate) 317163152cbSTony Lindgren { 318163152cbSTony Lindgren struct fapll_synth *synth = to_synth(hw); 319163152cbSTony Lindgren u32 synth_div_m; 320c51185b4SNicolas Pitre u64 rate; 321163152cbSTony Lindgren 322163152cbSTony Lindgren /* The audio_pll_clk1 is hardwired to produce 32.768KiHz clock */ 323163152cbSTony Lindgren if (!synth->div) 324163152cbSTony Lindgren return 32768; 325163152cbSTony Lindgren 326163152cbSTony Lindgren /* 327163152cbSTony Lindgren * PLL in bypass sets the synths in bypass mode too. The PLL rate 328163152cbSTony Lindgren * can be also be set to 27MHz, so we can't use parent_rate to 329163152cbSTony Lindgren * check for bypass mode. 330163152cbSTony Lindgren */ 331163152cbSTony Lindgren if (ti_fapll_clock_is_bypass(synth->fd)) 332163152cbSTony Lindgren return parent_rate; 333163152cbSTony Lindgren 334163152cbSTony Lindgren rate = parent_rate; 335163152cbSTony Lindgren 336163152cbSTony Lindgren /* 337163152cbSTony Lindgren * Synth frequency integer and fractional divider. 338163152cbSTony Lindgren * Note that the phase output K is 8, so the result needs 339cafeb002STony Lindgren * to be multiplied by SYNTH_PHASE_K. 340163152cbSTony Lindgren */ 341163152cbSTony Lindgren if (synth->freq) { 342163152cbSTony Lindgren u32 v, synth_int_div, synth_frac_div, synth_div_freq; 343163152cbSTony Lindgren 344163152cbSTony Lindgren v = readl_relaxed(synth->freq); 345163152cbSTony Lindgren synth_int_div = (v >> 24) & 0xf; 346163152cbSTony Lindgren synth_frac_div = v & 0xffffff; 347163152cbSTony Lindgren synth_div_freq = (synth_int_div * 10000000) + synth_frac_div; 348163152cbSTony Lindgren rate *= 10000000; 349163152cbSTony Lindgren do_div(rate, synth_div_freq); 350cafeb002STony Lindgren rate *= SYNTH_PHASE_K; 351163152cbSTony Lindgren } 352163152cbSTony Lindgren 35333ca29c9STony Lindgren /* Synth post-divider M */ 35433ca29c9STony Lindgren synth_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M; 355163152cbSTony Lindgren 35633ca29c9STony Lindgren return DIV_ROUND_UP_ULL(rate, synth_div_m); 357163152cbSTony Lindgren } 358163152cbSTony Lindgren 359cafeb002STony Lindgren static unsigned long ti_fapll_synth_get_frac_rate(struct clk_hw *hw, 360cafeb002STony Lindgren unsigned long parent_rate) 361cafeb002STony Lindgren { 362cafeb002STony Lindgren struct fapll_synth *synth = to_synth(hw); 363cafeb002STony Lindgren unsigned long current_rate, frac_rate; 364cafeb002STony Lindgren u32 post_div_m; 365cafeb002STony Lindgren 366cafeb002STony Lindgren current_rate = ti_fapll_synth_recalc_rate(hw, parent_rate); 367cafeb002STony Lindgren post_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M; 368cafeb002STony Lindgren frac_rate = current_rate * post_div_m; 369cafeb002STony Lindgren 370cafeb002STony Lindgren return frac_rate; 371cafeb002STony Lindgren } 372cafeb002STony Lindgren 373cafeb002STony Lindgren static u32 ti_fapll_synth_set_frac_rate(struct fapll_synth *synth, 374cafeb002STony Lindgren unsigned long rate, 375cafeb002STony Lindgren unsigned long parent_rate) 376cafeb002STony Lindgren { 377cafeb002STony Lindgren u32 post_div_m, synth_int_div = 0, synth_frac_div = 0, v; 378cafeb002STony Lindgren 379cafeb002STony Lindgren post_div_m = DIV_ROUND_UP_ULL((u64)parent_rate * SYNTH_PHASE_K, rate); 380cafeb002STony Lindgren post_div_m = post_div_m / SYNTH_MAX_INT_DIV; 381cafeb002STony Lindgren if (post_div_m > SYNTH_MAX_DIV_M) 382cafeb002STony Lindgren return -EINVAL; 383cafeb002STony Lindgren if (!post_div_m) 384cafeb002STony Lindgren post_div_m = 1; 385cafeb002STony Lindgren 386cafeb002STony Lindgren for (; post_div_m < SYNTH_MAX_DIV_M; post_div_m++) { 387cafeb002STony Lindgren synth_int_div = DIV_ROUND_UP_ULL((u64)parent_rate * 388cafeb002STony Lindgren SYNTH_PHASE_K * 389cafeb002STony Lindgren 10000000, 390cafeb002STony Lindgren rate * post_div_m); 391cafeb002STony Lindgren synth_frac_div = synth_int_div % 10000000; 392cafeb002STony Lindgren synth_int_div /= 10000000; 393cafeb002STony Lindgren 394cafeb002STony Lindgren if (synth_int_div <= SYNTH_MAX_INT_DIV) 395cafeb002STony Lindgren break; 396cafeb002STony Lindgren } 397cafeb002STony Lindgren 398cafeb002STony Lindgren if (synth_int_div > SYNTH_MAX_INT_DIV) 399cafeb002STony Lindgren return -EINVAL; 400cafeb002STony Lindgren 401cafeb002STony Lindgren v = readl_relaxed(synth->freq); 402cafeb002STony Lindgren v &= ~0x1fffffff; 403cafeb002STony Lindgren v |= (synth_int_div & SYNTH_MAX_INT_DIV) << 24; 404cafeb002STony Lindgren v |= (synth_frac_div & 0xffffff); 405cafeb002STony Lindgren v |= SYNTH_LDFREQ; 406cafeb002STony Lindgren writel_relaxed(v, synth->freq); 407cafeb002STony Lindgren 408cafeb002STony Lindgren return post_div_m; 409cafeb002STony Lindgren } 410cafeb002STony Lindgren 411cafeb002STony Lindgren static long ti_fapll_synth_round_rate(struct clk_hw *hw, unsigned long rate, 412cafeb002STony Lindgren unsigned long *parent_rate) 413cafeb002STony Lindgren { 414cafeb002STony Lindgren struct fapll_synth *synth = to_synth(hw); 415cafeb002STony Lindgren struct fapll_data *fd = synth->fd; 416cafeb002STony Lindgren unsigned long r; 417cafeb002STony Lindgren 418cafeb002STony Lindgren if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate) 419cafeb002STony Lindgren return -EINVAL; 420cafeb002STony Lindgren 421cafeb002STony Lindgren /* Only post divider m available with no fractional divider? */ 422cafeb002STony Lindgren if (!synth->freq) { 423cafeb002STony Lindgren unsigned long frac_rate; 424cafeb002STony Lindgren u32 synth_post_div_m; 425cafeb002STony Lindgren 426cafeb002STony Lindgren frac_rate = ti_fapll_synth_get_frac_rate(hw, *parent_rate); 427cafeb002STony Lindgren synth_post_div_m = DIV_ROUND_UP(frac_rate, rate); 428cafeb002STony Lindgren r = DIV_ROUND_UP(frac_rate, synth_post_div_m); 429cafeb002STony Lindgren goto out; 430cafeb002STony Lindgren } 431cafeb002STony Lindgren 432cafeb002STony Lindgren r = *parent_rate * SYNTH_PHASE_K; 433cafeb002STony Lindgren if (rate > r) 434cafeb002STony Lindgren goto out; 435cafeb002STony Lindgren 436cafeb002STony Lindgren r = DIV_ROUND_UP_ULL(r, SYNTH_MAX_INT_DIV * SYNTH_MAX_DIV_M); 437cafeb002STony Lindgren if (rate < r) 438cafeb002STony Lindgren goto out; 439cafeb002STony Lindgren 440cafeb002STony Lindgren r = rate; 441cafeb002STony Lindgren out: 442cafeb002STony Lindgren return r; 443cafeb002STony Lindgren } 444cafeb002STony Lindgren 445cafeb002STony Lindgren static int ti_fapll_synth_set_rate(struct clk_hw *hw, unsigned long rate, 446cafeb002STony Lindgren unsigned long parent_rate) 447cafeb002STony Lindgren { 448cafeb002STony Lindgren struct fapll_synth *synth = to_synth(hw); 449cafeb002STony Lindgren struct fapll_data *fd = synth->fd; 450cafeb002STony Lindgren unsigned long frac_rate, post_rate = 0; 451cafeb002STony Lindgren u32 post_div_m = 0, v; 452cafeb002STony Lindgren 453cafeb002STony Lindgren if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate) 454cafeb002STony Lindgren return -EINVAL; 455cafeb002STony Lindgren 456cafeb002STony Lindgren /* Produce the rate with just post divider M? */ 457cafeb002STony Lindgren frac_rate = ti_fapll_synth_get_frac_rate(hw, parent_rate); 458cafeb002STony Lindgren if (frac_rate < rate) { 459cafeb002STony Lindgren if (!synth->freq) 460cafeb002STony Lindgren return -EINVAL; 461cafeb002STony Lindgren } else { 462cafeb002STony Lindgren post_div_m = DIV_ROUND_UP(frac_rate, rate); 463cafeb002STony Lindgren if (post_div_m && (post_div_m <= SYNTH_MAX_DIV_M)) 464cafeb002STony Lindgren post_rate = DIV_ROUND_UP(frac_rate, post_div_m); 465cafeb002STony Lindgren if (!synth->freq && !post_rate) 466cafeb002STony Lindgren return -EINVAL; 467cafeb002STony Lindgren } 468cafeb002STony Lindgren 469cafeb002STony Lindgren /* Need to recalculate the fractional divider? */ 470cafeb002STony Lindgren if ((post_rate != rate) && synth->freq) 471cafeb002STony Lindgren post_div_m = ti_fapll_synth_set_frac_rate(synth, 472cafeb002STony Lindgren rate, 473cafeb002STony Lindgren parent_rate); 474cafeb002STony Lindgren 475cafeb002STony Lindgren v = readl_relaxed(synth->div); 476cafeb002STony Lindgren v &= ~SYNTH_MAX_DIV_M; 477cafeb002STony Lindgren v |= post_div_m; 478cafeb002STony Lindgren v |= SYNTH_LDMDIV1; 479cafeb002STony Lindgren writel_relaxed(v, synth->div); 480cafeb002STony Lindgren 481cafeb002STony Lindgren return 0; 482163152cbSTony Lindgren } 483163152cbSTony Lindgren 4847cc566a8SBhumika Goyal static const struct clk_ops ti_fapll_synt_ops = { 485163152cbSTony Lindgren .enable = ti_fapll_synth_enable, 486163152cbSTony Lindgren .disable = ti_fapll_synth_disable, 487163152cbSTony Lindgren .is_enabled = ti_fapll_synth_is_enabled, 488163152cbSTony Lindgren .recalc_rate = ti_fapll_synth_recalc_rate, 489cafeb002STony Lindgren .round_rate = ti_fapll_synth_round_rate, 490cafeb002STony Lindgren .set_rate = ti_fapll_synth_set_rate, 491163152cbSTony Lindgren }; 492163152cbSTony Lindgren 493163152cbSTony Lindgren static struct clk * __init ti_fapll_synth_setup(struct fapll_data *fd, 494163152cbSTony Lindgren void __iomem *freq, 495163152cbSTony Lindgren void __iomem *div, 496163152cbSTony Lindgren int index, 497163152cbSTony Lindgren const char *name, 498163152cbSTony Lindgren const char *parent, 499163152cbSTony Lindgren struct clk *pll_clk) 500163152cbSTony Lindgren { 501163152cbSTony Lindgren struct clk_init_data *init; 502163152cbSTony Lindgren struct fapll_synth *synth; 5038c6239f6SZhang Qilong struct clk *clk = ERR_PTR(-ENOMEM); 504163152cbSTony Lindgren 505163152cbSTony Lindgren init = kzalloc(sizeof(*init), GFP_KERNEL); 506163152cbSTony Lindgren if (!init) 507163152cbSTony Lindgren return ERR_PTR(-ENOMEM); 508163152cbSTony Lindgren 509163152cbSTony Lindgren init->ops = &ti_fapll_synt_ops; 510163152cbSTony Lindgren init->name = name; 511163152cbSTony Lindgren init->parent_names = &parent; 512163152cbSTony Lindgren init->num_parents = 1; 513163152cbSTony Lindgren 514163152cbSTony Lindgren synth = kzalloc(sizeof(*synth), GFP_KERNEL); 515163152cbSTony Lindgren if (!synth) 516163152cbSTony Lindgren goto free; 517163152cbSTony Lindgren 518163152cbSTony Lindgren synth->fd = fd; 519163152cbSTony Lindgren synth->index = index; 520163152cbSTony Lindgren synth->freq = freq; 521163152cbSTony Lindgren synth->div = div; 522163152cbSTony Lindgren synth->name = name; 523163152cbSTony Lindgren synth->hw.init = init; 524163152cbSTony Lindgren synth->clk_pll = pll_clk; 525163152cbSTony Lindgren 5268c6239f6SZhang Qilong clk = clk_register(NULL, &synth->hw); 5278c6239f6SZhang Qilong if (IS_ERR(clk)) { 5288c6239f6SZhang Qilong pr_err("failed to register clock\n"); 5298c6239f6SZhang Qilong goto free; 5308c6239f6SZhang Qilong } 5318c6239f6SZhang Qilong 5328c6239f6SZhang Qilong return clk; 533163152cbSTony Lindgren 534163152cbSTony Lindgren free: 535163152cbSTony Lindgren kfree(synth); 536163152cbSTony Lindgren kfree(init); 537163152cbSTony Lindgren 5388c6239f6SZhang Qilong return clk; 539163152cbSTony Lindgren } 540163152cbSTony Lindgren 541163152cbSTony Lindgren static void __init ti_fapll_setup(struct device_node *node) 542163152cbSTony Lindgren { 543163152cbSTony Lindgren struct fapll_data *fd; 544163152cbSTony Lindgren struct clk_init_data *init = NULL; 545163152cbSTony Lindgren const char *parent_name[2]; 546163152cbSTony Lindgren struct clk *pll_clk; 547*9e56a7d4STony Lindgren const char *name; 548163152cbSTony Lindgren int i; 549163152cbSTony Lindgren 550163152cbSTony Lindgren fd = kzalloc(sizeof(*fd), GFP_KERNEL); 551163152cbSTony Lindgren if (!fd) 552163152cbSTony Lindgren return; 553163152cbSTony Lindgren 554163152cbSTony Lindgren fd->outputs.clks = kzalloc(sizeof(struct clk *) * 555163152cbSTony Lindgren MAX_FAPLL_OUTPUTS + 1, 556163152cbSTony Lindgren GFP_KERNEL); 557163152cbSTony Lindgren if (!fd->outputs.clks) 558163152cbSTony Lindgren goto free; 559163152cbSTony Lindgren 560163152cbSTony Lindgren init = kzalloc(sizeof(*init), GFP_KERNEL); 561163152cbSTony Lindgren if (!init) 562163152cbSTony Lindgren goto free; 563163152cbSTony Lindgren 564163152cbSTony Lindgren init->ops = &ti_fapll_ops; 565*9e56a7d4STony Lindgren name = ti_dt_clk_name(node); 566*9e56a7d4STony Lindgren init->name = name; 567163152cbSTony Lindgren 568163152cbSTony Lindgren init->num_parents = of_clk_get_parent_count(node); 569163152cbSTony Lindgren if (init->num_parents != 2) { 570e665f029SRob Herring pr_err("%pOFn must have two parents\n", node); 571163152cbSTony Lindgren goto free; 572163152cbSTony Lindgren } 573163152cbSTony Lindgren 5749da9e761SDinh Nguyen of_clk_parent_fill(node, parent_name, 2); 575163152cbSTony Lindgren init->parent_names = parent_name; 576163152cbSTony Lindgren 577163152cbSTony Lindgren fd->clk_ref = of_clk_get(node, 0); 578163152cbSTony Lindgren if (IS_ERR(fd->clk_ref)) { 579e665f029SRob Herring pr_err("%pOFn could not get clk_ref\n", node); 580163152cbSTony Lindgren goto free; 581163152cbSTony Lindgren } 582163152cbSTony Lindgren 583163152cbSTony Lindgren fd->clk_bypass = of_clk_get(node, 1); 584163152cbSTony Lindgren if (IS_ERR(fd->clk_bypass)) { 585e665f029SRob Herring pr_err("%pOFn could not get clk_bypass\n", node); 586163152cbSTony Lindgren goto free; 587163152cbSTony Lindgren } 588163152cbSTony Lindgren 589163152cbSTony Lindgren fd->base = of_iomap(node, 0); 590163152cbSTony Lindgren if (!fd->base) { 591e665f029SRob Herring pr_err("%pOFn could not get IO base\n", node); 592163152cbSTony Lindgren goto free; 593163152cbSTony Lindgren } 594163152cbSTony Lindgren 595163152cbSTony Lindgren if (fapll_is_ddr_pll(fd->base)) 596163152cbSTony Lindgren fd->bypass_bit_inverted = true; 597163152cbSTony Lindgren 598*9e56a7d4STony Lindgren fd->name = name; 599163152cbSTony Lindgren fd->hw.init = init; 600163152cbSTony Lindgren 601163152cbSTony Lindgren /* Register the parent PLL */ 602163152cbSTony Lindgren pll_clk = clk_register(NULL, &fd->hw); 603163152cbSTony Lindgren if (IS_ERR(pll_clk)) 604163152cbSTony Lindgren goto unmap; 605163152cbSTony Lindgren 606163152cbSTony Lindgren fd->outputs.clks[0] = pll_clk; 607163152cbSTony Lindgren fd->outputs.clk_num++; 608163152cbSTony Lindgren 609163152cbSTony Lindgren /* 610163152cbSTony Lindgren * Set up the child synthesizers starting at index 1 as the 611163152cbSTony Lindgren * PLL output is at index 0. We need to check the clock-indices 612163152cbSTony Lindgren * for numbering in case there are holes in the synth mapping, 613163152cbSTony Lindgren * and then probe the synth register to see if it has a FREQ 614163152cbSTony Lindgren * register available. 615163152cbSTony Lindgren */ 616163152cbSTony Lindgren for (i = 0; i < MAX_FAPLL_OUTPUTS; i++) { 617163152cbSTony Lindgren const char *output_name; 618163152cbSTony Lindgren void __iomem *freq, *div; 619163152cbSTony Lindgren struct clk *synth_clk; 620163152cbSTony Lindgren int output_instance; 621163152cbSTony Lindgren u32 v; 622163152cbSTony Lindgren 623163152cbSTony Lindgren if (of_property_read_string_index(node, "clock-output-names", 624163152cbSTony Lindgren i, &output_name)) 625163152cbSTony Lindgren continue; 626163152cbSTony Lindgren 627163152cbSTony Lindgren if (of_property_read_u32_index(node, "clock-indices", i, 628163152cbSTony Lindgren &output_instance)) 629163152cbSTony Lindgren output_instance = i; 630163152cbSTony Lindgren 631163152cbSTony Lindgren freq = fd->base + (output_instance * 8); 632163152cbSTony Lindgren div = freq + 4; 633163152cbSTony Lindgren 634163152cbSTony Lindgren /* Check for hardwired audio_pll_clk1 */ 635163152cbSTony Lindgren if (is_audio_pll_clk1(freq)) { 636412d6b47SStephen Boyd freq = NULL; 637412d6b47SStephen Boyd div = NULL; 638163152cbSTony Lindgren } else { 639163152cbSTony Lindgren /* Does the synthesizer have a FREQ register? */ 640163152cbSTony Lindgren v = readl_relaxed(freq); 641163152cbSTony Lindgren if (!v) 642412d6b47SStephen Boyd freq = NULL; 643163152cbSTony Lindgren } 644163152cbSTony Lindgren synth_clk = ti_fapll_synth_setup(fd, freq, div, output_instance, 645*9e56a7d4STony Lindgren output_name, name, pll_clk); 646163152cbSTony Lindgren if (IS_ERR(synth_clk)) 647163152cbSTony Lindgren continue; 648163152cbSTony Lindgren 649163152cbSTony Lindgren fd->outputs.clks[output_instance] = synth_clk; 650163152cbSTony Lindgren fd->outputs.clk_num++; 651163152cbSTony Lindgren 652163152cbSTony Lindgren clk_register_clkdev(synth_clk, output_name, NULL); 653163152cbSTony Lindgren } 654163152cbSTony Lindgren 655163152cbSTony Lindgren /* Register the child synthesizers as the FAPLL outputs */ 656163152cbSTony Lindgren of_clk_add_provider(node, of_clk_src_onecell_get, &fd->outputs); 657163152cbSTony Lindgren /* Add clock alias for the outputs */ 658163152cbSTony Lindgren 659163152cbSTony Lindgren kfree(init); 660163152cbSTony Lindgren 661163152cbSTony Lindgren return; 662163152cbSTony Lindgren 663163152cbSTony Lindgren unmap: 664163152cbSTony Lindgren iounmap(fd->base); 665163152cbSTony Lindgren free: 666163152cbSTony Lindgren if (fd->clk_bypass) 667163152cbSTony Lindgren clk_put(fd->clk_bypass); 668163152cbSTony Lindgren if (fd->clk_ref) 669163152cbSTony Lindgren clk_put(fd->clk_ref); 670163152cbSTony Lindgren kfree(fd->outputs.clks); 671163152cbSTony Lindgren kfree(fd); 672163152cbSTony Lindgren kfree(init); 673163152cbSTony Lindgren } 674163152cbSTony Lindgren 675163152cbSTony Lindgren CLK_OF_DECLARE(ti_fapll_clock, "ti,dm816-fapll-clock", ti_fapll_setup); 676