18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or 38c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License as 48c2ecf20Sopenharmony_ci * published by the Free Software Foundation version 2. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * This program is distributed "as is" WITHOUT ANY WARRANTY of any 78c2ecf20Sopenharmony_ci * kind, whether express or implied; without even the implied warranty 88c2ecf20Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 98c2ecf20Sopenharmony_ci * GNU General Public License for more details. 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/clk.h> 138c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 148c2ecf20Sopenharmony_ci#include <linux/delay.h> 158c2ecf20Sopenharmony_ci#include <linux/err.h> 168c2ecf20Sopenharmony_ci#include <linux/io.h> 178c2ecf20Sopenharmony_ci#include <linux/math64.h> 188c2ecf20Sopenharmony_ci#include <linux/of.h> 198c2ecf20Sopenharmony_ci#include <linux/of_address.h> 208c2ecf20Sopenharmony_ci#include <linux/clk/ti.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include "clock.h" 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci/* FAPLL Control Register PLL_CTRL */ 258c2ecf20Sopenharmony_ci#define FAPLL_MAIN_MULT_N_SHIFT 16 268c2ecf20Sopenharmony_ci#define FAPLL_MAIN_DIV_P_SHIFT 8 278c2ecf20Sopenharmony_ci#define FAPLL_MAIN_LOCK BIT(7) 288c2ecf20Sopenharmony_ci#define FAPLL_MAIN_PLLEN BIT(3) 298c2ecf20Sopenharmony_ci#define FAPLL_MAIN_BP BIT(2) 308c2ecf20Sopenharmony_ci#define FAPLL_MAIN_LOC_CTL BIT(0) 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define FAPLL_MAIN_MAX_MULT_N 0xffff 338c2ecf20Sopenharmony_ci#define FAPLL_MAIN_MAX_DIV_P 0xff 348c2ecf20Sopenharmony_ci#define FAPLL_MAIN_CLEAR_MASK \ 358c2ecf20Sopenharmony_ci ((FAPLL_MAIN_MAX_MULT_N << FAPLL_MAIN_MULT_N_SHIFT) | \ 368c2ecf20Sopenharmony_ci (FAPLL_MAIN_DIV_P_SHIFT << FAPLL_MAIN_DIV_P_SHIFT) | \ 378c2ecf20Sopenharmony_ci FAPLL_MAIN_LOC_CTL) 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci/* FAPLL powerdown register PWD */ 408c2ecf20Sopenharmony_ci#define FAPLL_PWD_OFFSET 4 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci#define MAX_FAPLL_OUTPUTS 7 438c2ecf20Sopenharmony_ci#define FAPLL_MAX_RETRIES 1000 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci#define to_fapll(_hw) container_of(_hw, struct fapll_data, hw) 468c2ecf20Sopenharmony_ci#define to_synth(_hw) container_of(_hw, struct fapll_synth, hw) 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci/* The bypass bit is inverted on the ddr_pll.. */ 498c2ecf20Sopenharmony_ci#define fapll_is_ddr_pll(va) (((u32)(va) & 0xffff) == 0x0440) 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci/* 528c2ecf20Sopenharmony_ci * The audio_pll_clk1 input is hard wired to the 27MHz bypass clock, 538c2ecf20Sopenharmony_ci * and the audio_pll_clk1 synthesizer is hardwared to 32KiHz output. 548c2ecf20Sopenharmony_ci */ 558c2ecf20Sopenharmony_ci#define is_ddr_pll_clk1(va) (((u32)(va) & 0xffff) == 0x044c) 568c2ecf20Sopenharmony_ci#define is_audio_pll_clk1(va) (((u32)(va) & 0xffff) == 0x04a8) 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci/* Synthesizer divider register */ 598c2ecf20Sopenharmony_ci#define SYNTH_LDMDIV1 BIT(8) 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci/* Synthesizer frequency register */ 628c2ecf20Sopenharmony_ci#define SYNTH_LDFREQ BIT(31) 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci#define SYNTH_PHASE_K 8 658c2ecf20Sopenharmony_ci#define SYNTH_MAX_INT_DIV 0xf 668c2ecf20Sopenharmony_ci#define SYNTH_MAX_DIV_M 0xff 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistruct fapll_data { 698c2ecf20Sopenharmony_ci struct clk_hw hw; 708c2ecf20Sopenharmony_ci void __iomem *base; 718c2ecf20Sopenharmony_ci const char *name; 728c2ecf20Sopenharmony_ci struct clk *clk_ref; 738c2ecf20Sopenharmony_ci struct clk *clk_bypass; 748c2ecf20Sopenharmony_ci struct clk_onecell_data outputs; 758c2ecf20Sopenharmony_ci bool bypass_bit_inverted; 768c2ecf20Sopenharmony_ci}; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistruct fapll_synth { 798c2ecf20Sopenharmony_ci struct clk_hw hw; 808c2ecf20Sopenharmony_ci struct fapll_data *fd; 818c2ecf20Sopenharmony_ci int index; 828c2ecf20Sopenharmony_ci void __iomem *freq; 838c2ecf20Sopenharmony_ci void __iomem *div; 848c2ecf20Sopenharmony_ci const char *name; 858c2ecf20Sopenharmony_ci struct clk *clk_pll; 868c2ecf20Sopenharmony_ci}; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_cistatic bool ti_fapll_clock_is_bypass(struct fapll_data *fd) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci u32 v = readl_relaxed(fd->base); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci if (fd->bypass_bit_inverted) 938c2ecf20Sopenharmony_ci return !(v & FAPLL_MAIN_BP); 948c2ecf20Sopenharmony_ci else 958c2ecf20Sopenharmony_ci return !!(v & FAPLL_MAIN_BP); 968c2ecf20Sopenharmony_ci} 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cistatic void ti_fapll_set_bypass(struct fapll_data *fd) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci u32 v = readl_relaxed(fd->base); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci if (fd->bypass_bit_inverted) 1038c2ecf20Sopenharmony_ci v &= ~FAPLL_MAIN_BP; 1048c2ecf20Sopenharmony_ci else 1058c2ecf20Sopenharmony_ci v |= FAPLL_MAIN_BP; 1068c2ecf20Sopenharmony_ci writel_relaxed(v, fd->base); 1078c2ecf20Sopenharmony_ci} 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic void ti_fapll_clear_bypass(struct fapll_data *fd) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci u32 v = readl_relaxed(fd->base); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci if (fd->bypass_bit_inverted) 1148c2ecf20Sopenharmony_ci v |= FAPLL_MAIN_BP; 1158c2ecf20Sopenharmony_ci else 1168c2ecf20Sopenharmony_ci v &= ~FAPLL_MAIN_BP; 1178c2ecf20Sopenharmony_ci writel_relaxed(v, fd->base); 1188c2ecf20Sopenharmony_ci} 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_cistatic int ti_fapll_wait_lock(struct fapll_data *fd) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci int retries = FAPLL_MAX_RETRIES; 1238c2ecf20Sopenharmony_ci u32 v; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci while ((v = readl_relaxed(fd->base))) { 1268c2ecf20Sopenharmony_ci if (v & FAPLL_MAIN_LOCK) 1278c2ecf20Sopenharmony_ci return 0; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci if (retries-- <= 0) 1308c2ecf20Sopenharmony_ci break; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci udelay(1); 1338c2ecf20Sopenharmony_ci } 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci pr_err("%s failed to lock\n", fd->name); 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci return -ETIMEDOUT; 1388c2ecf20Sopenharmony_ci} 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cistatic int ti_fapll_enable(struct clk_hw *hw) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci struct fapll_data *fd = to_fapll(hw); 1438c2ecf20Sopenharmony_ci u32 v = readl_relaxed(fd->base); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci v |= FAPLL_MAIN_PLLEN; 1468c2ecf20Sopenharmony_ci writel_relaxed(v, fd->base); 1478c2ecf20Sopenharmony_ci ti_fapll_wait_lock(fd); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci return 0; 1508c2ecf20Sopenharmony_ci} 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistatic void ti_fapll_disable(struct clk_hw *hw) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci struct fapll_data *fd = to_fapll(hw); 1558c2ecf20Sopenharmony_ci u32 v = readl_relaxed(fd->base); 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci v &= ~FAPLL_MAIN_PLLEN; 1588c2ecf20Sopenharmony_ci writel_relaxed(v, fd->base); 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cistatic int ti_fapll_is_enabled(struct clk_hw *hw) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci struct fapll_data *fd = to_fapll(hw); 1648c2ecf20Sopenharmony_ci u32 v = readl_relaxed(fd->base); 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci return v & FAPLL_MAIN_PLLEN; 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_cistatic unsigned long ti_fapll_recalc_rate(struct clk_hw *hw, 1708c2ecf20Sopenharmony_ci unsigned long parent_rate) 1718c2ecf20Sopenharmony_ci{ 1728c2ecf20Sopenharmony_ci struct fapll_data *fd = to_fapll(hw); 1738c2ecf20Sopenharmony_ci u32 fapll_n, fapll_p, v; 1748c2ecf20Sopenharmony_ci u64 rate; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci if (ti_fapll_clock_is_bypass(fd)) 1778c2ecf20Sopenharmony_ci return parent_rate; 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci rate = parent_rate; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci /* PLL pre-divider is P and multiplier is N */ 1828c2ecf20Sopenharmony_ci v = readl_relaxed(fd->base); 1838c2ecf20Sopenharmony_ci fapll_p = (v >> 8) & 0xff; 1848c2ecf20Sopenharmony_ci if (fapll_p) 1858c2ecf20Sopenharmony_ci do_div(rate, fapll_p); 1868c2ecf20Sopenharmony_ci fapll_n = v >> 16; 1878c2ecf20Sopenharmony_ci if (fapll_n) 1888c2ecf20Sopenharmony_ci rate *= fapll_n; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci return rate; 1918c2ecf20Sopenharmony_ci} 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_cistatic u8 ti_fapll_get_parent(struct clk_hw *hw) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci struct fapll_data *fd = to_fapll(hw); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci if (ti_fapll_clock_is_bypass(fd)) 1988c2ecf20Sopenharmony_ci return 1; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci return 0; 2018c2ecf20Sopenharmony_ci} 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_cistatic int ti_fapll_set_div_mult(unsigned long rate, 2048c2ecf20Sopenharmony_ci unsigned long parent_rate, 2058c2ecf20Sopenharmony_ci u32 *pre_div_p, u32 *mult_n) 2068c2ecf20Sopenharmony_ci{ 2078c2ecf20Sopenharmony_ci /* 2088c2ecf20Sopenharmony_ci * So far no luck getting decent clock with PLL divider, 2098c2ecf20Sopenharmony_ci * PLL does not seem to lock and the signal does not look 2108c2ecf20Sopenharmony_ci * right. It seems the divider can only be used together 2118c2ecf20Sopenharmony_ci * with the multiplier? 2128c2ecf20Sopenharmony_ci */ 2138c2ecf20Sopenharmony_ci if (rate < parent_rate) { 2148c2ecf20Sopenharmony_ci pr_warn("FAPLL main divider rates unsupported\n"); 2158c2ecf20Sopenharmony_ci return -EINVAL; 2168c2ecf20Sopenharmony_ci } 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci *mult_n = rate / parent_rate; 2198c2ecf20Sopenharmony_ci if (*mult_n > FAPLL_MAIN_MAX_MULT_N) 2208c2ecf20Sopenharmony_ci return -EINVAL; 2218c2ecf20Sopenharmony_ci *pre_div_p = 1; 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci return 0; 2248c2ecf20Sopenharmony_ci} 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_cistatic long ti_fapll_round_rate(struct clk_hw *hw, unsigned long rate, 2278c2ecf20Sopenharmony_ci unsigned long *parent_rate) 2288c2ecf20Sopenharmony_ci{ 2298c2ecf20Sopenharmony_ci u32 pre_div_p, mult_n; 2308c2ecf20Sopenharmony_ci int error; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci if (!rate) 2338c2ecf20Sopenharmony_ci return -EINVAL; 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci error = ti_fapll_set_div_mult(rate, *parent_rate, 2368c2ecf20Sopenharmony_ci &pre_div_p, &mult_n); 2378c2ecf20Sopenharmony_ci if (error) 2388c2ecf20Sopenharmony_ci return error; 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci rate = *parent_rate / pre_div_p; 2418c2ecf20Sopenharmony_ci rate *= mult_n; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci return rate; 2448c2ecf20Sopenharmony_ci} 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_cistatic int ti_fapll_set_rate(struct clk_hw *hw, unsigned long rate, 2478c2ecf20Sopenharmony_ci unsigned long parent_rate) 2488c2ecf20Sopenharmony_ci{ 2498c2ecf20Sopenharmony_ci struct fapll_data *fd = to_fapll(hw); 2508c2ecf20Sopenharmony_ci u32 pre_div_p, mult_n, v; 2518c2ecf20Sopenharmony_ci int error; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci if (!rate) 2548c2ecf20Sopenharmony_ci return -EINVAL; 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci error = ti_fapll_set_div_mult(rate, parent_rate, 2578c2ecf20Sopenharmony_ci &pre_div_p, &mult_n); 2588c2ecf20Sopenharmony_ci if (error) 2598c2ecf20Sopenharmony_ci return error; 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci ti_fapll_set_bypass(fd); 2628c2ecf20Sopenharmony_ci v = readl_relaxed(fd->base); 2638c2ecf20Sopenharmony_ci v &= ~FAPLL_MAIN_CLEAR_MASK; 2648c2ecf20Sopenharmony_ci v |= pre_div_p << FAPLL_MAIN_DIV_P_SHIFT; 2658c2ecf20Sopenharmony_ci v |= mult_n << FAPLL_MAIN_MULT_N_SHIFT; 2668c2ecf20Sopenharmony_ci writel_relaxed(v, fd->base); 2678c2ecf20Sopenharmony_ci if (ti_fapll_is_enabled(hw)) 2688c2ecf20Sopenharmony_ci ti_fapll_wait_lock(fd); 2698c2ecf20Sopenharmony_ci ti_fapll_clear_bypass(fd); 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci return 0; 2728c2ecf20Sopenharmony_ci} 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_cistatic const struct clk_ops ti_fapll_ops = { 2758c2ecf20Sopenharmony_ci .enable = ti_fapll_enable, 2768c2ecf20Sopenharmony_ci .disable = ti_fapll_disable, 2778c2ecf20Sopenharmony_ci .is_enabled = ti_fapll_is_enabled, 2788c2ecf20Sopenharmony_ci .recalc_rate = ti_fapll_recalc_rate, 2798c2ecf20Sopenharmony_ci .get_parent = ti_fapll_get_parent, 2808c2ecf20Sopenharmony_ci .round_rate = ti_fapll_round_rate, 2818c2ecf20Sopenharmony_ci .set_rate = ti_fapll_set_rate, 2828c2ecf20Sopenharmony_ci}; 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_cistatic int ti_fapll_synth_enable(struct clk_hw *hw) 2858c2ecf20Sopenharmony_ci{ 2868c2ecf20Sopenharmony_ci struct fapll_synth *synth = to_synth(hw); 2878c2ecf20Sopenharmony_ci u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET); 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci v &= ~(1 << synth->index); 2908c2ecf20Sopenharmony_ci writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci return 0; 2938c2ecf20Sopenharmony_ci} 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_cistatic void ti_fapll_synth_disable(struct clk_hw *hw) 2968c2ecf20Sopenharmony_ci{ 2978c2ecf20Sopenharmony_ci struct fapll_synth *synth = to_synth(hw); 2988c2ecf20Sopenharmony_ci u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET); 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci v |= 1 << synth->index; 3018c2ecf20Sopenharmony_ci writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET); 3028c2ecf20Sopenharmony_ci} 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_cistatic int ti_fapll_synth_is_enabled(struct clk_hw *hw) 3058c2ecf20Sopenharmony_ci{ 3068c2ecf20Sopenharmony_ci struct fapll_synth *synth = to_synth(hw); 3078c2ecf20Sopenharmony_ci u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET); 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci return !(v & (1 << synth->index)); 3108c2ecf20Sopenharmony_ci} 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci/* 3138c2ecf20Sopenharmony_ci * See dm816x TRM chapter 1.10.3 Flying Adder PLL fore more info 3148c2ecf20Sopenharmony_ci */ 3158c2ecf20Sopenharmony_cistatic unsigned long ti_fapll_synth_recalc_rate(struct clk_hw *hw, 3168c2ecf20Sopenharmony_ci unsigned long parent_rate) 3178c2ecf20Sopenharmony_ci{ 3188c2ecf20Sopenharmony_ci struct fapll_synth *synth = to_synth(hw); 3198c2ecf20Sopenharmony_ci u32 synth_div_m; 3208c2ecf20Sopenharmony_ci u64 rate; 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci /* The audio_pll_clk1 is hardwired to produce 32.768KiHz clock */ 3238c2ecf20Sopenharmony_ci if (!synth->div) 3248c2ecf20Sopenharmony_ci return 32768; 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci /* 3278c2ecf20Sopenharmony_ci * PLL in bypass sets the synths in bypass mode too. The PLL rate 3288c2ecf20Sopenharmony_ci * can be also be set to 27MHz, so we can't use parent_rate to 3298c2ecf20Sopenharmony_ci * check for bypass mode. 3308c2ecf20Sopenharmony_ci */ 3318c2ecf20Sopenharmony_ci if (ti_fapll_clock_is_bypass(synth->fd)) 3328c2ecf20Sopenharmony_ci return parent_rate; 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci rate = parent_rate; 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci /* 3378c2ecf20Sopenharmony_ci * Synth frequency integer and fractional divider. 3388c2ecf20Sopenharmony_ci * Note that the phase output K is 8, so the result needs 3398c2ecf20Sopenharmony_ci * to be multiplied by SYNTH_PHASE_K. 3408c2ecf20Sopenharmony_ci */ 3418c2ecf20Sopenharmony_ci if (synth->freq) { 3428c2ecf20Sopenharmony_ci u32 v, synth_int_div, synth_frac_div, synth_div_freq; 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ci v = readl_relaxed(synth->freq); 3458c2ecf20Sopenharmony_ci synth_int_div = (v >> 24) & 0xf; 3468c2ecf20Sopenharmony_ci synth_frac_div = v & 0xffffff; 3478c2ecf20Sopenharmony_ci synth_div_freq = (synth_int_div * 10000000) + synth_frac_div; 3488c2ecf20Sopenharmony_ci rate *= 10000000; 3498c2ecf20Sopenharmony_ci do_div(rate, synth_div_freq); 3508c2ecf20Sopenharmony_ci rate *= SYNTH_PHASE_K; 3518c2ecf20Sopenharmony_ci } 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci /* Synth post-divider M */ 3548c2ecf20Sopenharmony_ci synth_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M; 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci return DIV_ROUND_UP_ULL(rate, synth_div_m); 3578c2ecf20Sopenharmony_ci} 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_cistatic unsigned long ti_fapll_synth_get_frac_rate(struct clk_hw *hw, 3608c2ecf20Sopenharmony_ci unsigned long parent_rate) 3618c2ecf20Sopenharmony_ci{ 3628c2ecf20Sopenharmony_ci struct fapll_synth *synth = to_synth(hw); 3638c2ecf20Sopenharmony_ci unsigned long current_rate, frac_rate; 3648c2ecf20Sopenharmony_ci u32 post_div_m; 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci current_rate = ti_fapll_synth_recalc_rate(hw, parent_rate); 3678c2ecf20Sopenharmony_ci post_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M; 3688c2ecf20Sopenharmony_ci frac_rate = current_rate * post_div_m; 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci return frac_rate; 3718c2ecf20Sopenharmony_ci} 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_cistatic u32 ti_fapll_synth_set_frac_rate(struct fapll_synth *synth, 3748c2ecf20Sopenharmony_ci unsigned long rate, 3758c2ecf20Sopenharmony_ci unsigned long parent_rate) 3768c2ecf20Sopenharmony_ci{ 3778c2ecf20Sopenharmony_ci u32 post_div_m, synth_int_div = 0, synth_frac_div = 0, v; 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci post_div_m = DIV_ROUND_UP_ULL((u64)parent_rate * SYNTH_PHASE_K, rate); 3808c2ecf20Sopenharmony_ci post_div_m = post_div_m / SYNTH_MAX_INT_DIV; 3818c2ecf20Sopenharmony_ci if (post_div_m > SYNTH_MAX_DIV_M) 3828c2ecf20Sopenharmony_ci return -EINVAL; 3838c2ecf20Sopenharmony_ci if (!post_div_m) 3848c2ecf20Sopenharmony_ci post_div_m = 1; 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci for (; post_div_m < SYNTH_MAX_DIV_M; post_div_m++) { 3878c2ecf20Sopenharmony_ci synth_int_div = DIV_ROUND_UP_ULL((u64)parent_rate * 3888c2ecf20Sopenharmony_ci SYNTH_PHASE_K * 3898c2ecf20Sopenharmony_ci 10000000, 3908c2ecf20Sopenharmony_ci rate * post_div_m); 3918c2ecf20Sopenharmony_ci synth_frac_div = synth_int_div % 10000000; 3928c2ecf20Sopenharmony_ci synth_int_div /= 10000000; 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci if (synth_int_div <= SYNTH_MAX_INT_DIV) 3958c2ecf20Sopenharmony_ci break; 3968c2ecf20Sopenharmony_ci } 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci if (synth_int_div > SYNTH_MAX_INT_DIV) 3998c2ecf20Sopenharmony_ci return -EINVAL; 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_ci v = readl_relaxed(synth->freq); 4028c2ecf20Sopenharmony_ci v &= ~0x1fffffff; 4038c2ecf20Sopenharmony_ci v |= (synth_int_div & SYNTH_MAX_INT_DIV) << 24; 4048c2ecf20Sopenharmony_ci v |= (synth_frac_div & 0xffffff); 4058c2ecf20Sopenharmony_ci v |= SYNTH_LDFREQ; 4068c2ecf20Sopenharmony_ci writel_relaxed(v, synth->freq); 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci return post_div_m; 4098c2ecf20Sopenharmony_ci} 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_cistatic long ti_fapll_synth_round_rate(struct clk_hw *hw, unsigned long rate, 4128c2ecf20Sopenharmony_ci unsigned long *parent_rate) 4138c2ecf20Sopenharmony_ci{ 4148c2ecf20Sopenharmony_ci struct fapll_synth *synth = to_synth(hw); 4158c2ecf20Sopenharmony_ci struct fapll_data *fd = synth->fd; 4168c2ecf20Sopenharmony_ci unsigned long r; 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ci if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate) 4198c2ecf20Sopenharmony_ci return -EINVAL; 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci /* Only post divider m available with no fractional divider? */ 4228c2ecf20Sopenharmony_ci if (!synth->freq) { 4238c2ecf20Sopenharmony_ci unsigned long frac_rate; 4248c2ecf20Sopenharmony_ci u32 synth_post_div_m; 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci frac_rate = ti_fapll_synth_get_frac_rate(hw, *parent_rate); 4278c2ecf20Sopenharmony_ci synth_post_div_m = DIV_ROUND_UP(frac_rate, rate); 4288c2ecf20Sopenharmony_ci r = DIV_ROUND_UP(frac_rate, synth_post_div_m); 4298c2ecf20Sopenharmony_ci goto out; 4308c2ecf20Sopenharmony_ci } 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci r = *parent_rate * SYNTH_PHASE_K; 4338c2ecf20Sopenharmony_ci if (rate > r) 4348c2ecf20Sopenharmony_ci goto out; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci r = DIV_ROUND_UP_ULL(r, SYNTH_MAX_INT_DIV * SYNTH_MAX_DIV_M); 4378c2ecf20Sopenharmony_ci if (rate < r) 4388c2ecf20Sopenharmony_ci goto out; 4398c2ecf20Sopenharmony_ci 4408c2ecf20Sopenharmony_ci r = rate; 4418c2ecf20Sopenharmony_ciout: 4428c2ecf20Sopenharmony_ci return r; 4438c2ecf20Sopenharmony_ci} 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_cistatic int ti_fapll_synth_set_rate(struct clk_hw *hw, unsigned long rate, 4468c2ecf20Sopenharmony_ci unsigned long parent_rate) 4478c2ecf20Sopenharmony_ci{ 4488c2ecf20Sopenharmony_ci struct fapll_synth *synth = to_synth(hw); 4498c2ecf20Sopenharmony_ci struct fapll_data *fd = synth->fd; 4508c2ecf20Sopenharmony_ci unsigned long frac_rate, post_rate = 0; 4518c2ecf20Sopenharmony_ci u32 post_div_m = 0, v; 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate) 4548c2ecf20Sopenharmony_ci return -EINVAL; 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci /* Produce the rate with just post divider M? */ 4578c2ecf20Sopenharmony_ci frac_rate = ti_fapll_synth_get_frac_rate(hw, parent_rate); 4588c2ecf20Sopenharmony_ci if (frac_rate < rate) { 4598c2ecf20Sopenharmony_ci if (!synth->freq) 4608c2ecf20Sopenharmony_ci return -EINVAL; 4618c2ecf20Sopenharmony_ci } else { 4628c2ecf20Sopenharmony_ci post_div_m = DIV_ROUND_UP(frac_rate, rate); 4638c2ecf20Sopenharmony_ci if (post_div_m && (post_div_m <= SYNTH_MAX_DIV_M)) 4648c2ecf20Sopenharmony_ci post_rate = DIV_ROUND_UP(frac_rate, post_div_m); 4658c2ecf20Sopenharmony_ci if (!synth->freq && !post_rate) 4668c2ecf20Sopenharmony_ci return -EINVAL; 4678c2ecf20Sopenharmony_ci } 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci /* Need to recalculate the fractional divider? */ 4708c2ecf20Sopenharmony_ci if ((post_rate != rate) && synth->freq) 4718c2ecf20Sopenharmony_ci post_div_m = ti_fapll_synth_set_frac_rate(synth, 4728c2ecf20Sopenharmony_ci rate, 4738c2ecf20Sopenharmony_ci parent_rate); 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci v = readl_relaxed(synth->div); 4768c2ecf20Sopenharmony_ci v &= ~SYNTH_MAX_DIV_M; 4778c2ecf20Sopenharmony_ci v |= post_div_m; 4788c2ecf20Sopenharmony_ci v |= SYNTH_LDMDIV1; 4798c2ecf20Sopenharmony_ci writel_relaxed(v, synth->div); 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci return 0; 4828c2ecf20Sopenharmony_ci} 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_cistatic const struct clk_ops ti_fapll_synt_ops = { 4858c2ecf20Sopenharmony_ci .enable = ti_fapll_synth_enable, 4868c2ecf20Sopenharmony_ci .disable = ti_fapll_synth_disable, 4878c2ecf20Sopenharmony_ci .is_enabled = ti_fapll_synth_is_enabled, 4888c2ecf20Sopenharmony_ci .recalc_rate = ti_fapll_synth_recalc_rate, 4898c2ecf20Sopenharmony_ci .round_rate = ti_fapll_synth_round_rate, 4908c2ecf20Sopenharmony_ci .set_rate = ti_fapll_synth_set_rate, 4918c2ecf20Sopenharmony_ci}; 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_cistatic struct clk * __init ti_fapll_synth_setup(struct fapll_data *fd, 4948c2ecf20Sopenharmony_ci void __iomem *freq, 4958c2ecf20Sopenharmony_ci void __iomem *div, 4968c2ecf20Sopenharmony_ci int index, 4978c2ecf20Sopenharmony_ci const char *name, 4988c2ecf20Sopenharmony_ci const char *parent, 4998c2ecf20Sopenharmony_ci struct clk *pll_clk) 5008c2ecf20Sopenharmony_ci{ 5018c2ecf20Sopenharmony_ci struct clk_init_data *init; 5028c2ecf20Sopenharmony_ci struct fapll_synth *synth; 5038c2ecf20Sopenharmony_ci struct clk *clk = ERR_PTR(-ENOMEM); 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci init = kzalloc(sizeof(*init), GFP_KERNEL); 5068c2ecf20Sopenharmony_ci if (!init) 5078c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ci init->ops = &ti_fapll_synt_ops; 5108c2ecf20Sopenharmony_ci init->name = name; 5118c2ecf20Sopenharmony_ci init->parent_names = &parent; 5128c2ecf20Sopenharmony_ci init->num_parents = 1; 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_ci synth = kzalloc(sizeof(*synth), GFP_KERNEL); 5158c2ecf20Sopenharmony_ci if (!synth) 5168c2ecf20Sopenharmony_ci goto free; 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci synth->fd = fd; 5198c2ecf20Sopenharmony_ci synth->index = index; 5208c2ecf20Sopenharmony_ci synth->freq = freq; 5218c2ecf20Sopenharmony_ci synth->div = div; 5228c2ecf20Sopenharmony_ci synth->name = name; 5238c2ecf20Sopenharmony_ci synth->hw.init = init; 5248c2ecf20Sopenharmony_ci synth->clk_pll = pll_clk; 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci clk = clk_register(NULL, &synth->hw); 5278c2ecf20Sopenharmony_ci if (IS_ERR(clk)) { 5288c2ecf20Sopenharmony_ci pr_err("failed to register clock\n"); 5298c2ecf20Sopenharmony_ci goto free; 5308c2ecf20Sopenharmony_ci } 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci return clk; 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_cifree: 5358c2ecf20Sopenharmony_ci kfree(synth); 5368c2ecf20Sopenharmony_ci kfree(init); 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci return clk; 5398c2ecf20Sopenharmony_ci} 5408c2ecf20Sopenharmony_ci 5418c2ecf20Sopenharmony_cistatic void __init ti_fapll_setup(struct device_node *node) 5428c2ecf20Sopenharmony_ci{ 5438c2ecf20Sopenharmony_ci struct fapll_data *fd; 5448c2ecf20Sopenharmony_ci struct clk_init_data *init = NULL; 5458c2ecf20Sopenharmony_ci const char *parent_name[2]; 5468c2ecf20Sopenharmony_ci struct clk *pll_clk; 5478c2ecf20Sopenharmony_ci const char *name; 5488c2ecf20Sopenharmony_ci int i; 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci fd = kzalloc(sizeof(*fd), GFP_KERNEL); 5518c2ecf20Sopenharmony_ci if (!fd) 5528c2ecf20Sopenharmony_ci return; 5538c2ecf20Sopenharmony_ci 5548c2ecf20Sopenharmony_ci fd->outputs.clks = kzalloc(sizeof(struct clk *) * 5558c2ecf20Sopenharmony_ci MAX_FAPLL_OUTPUTS + 1, 5568c2ecf20Sopenharmony_ci GFP_KERNEL); 5578c2ecf20Sopenharmony_ci if (!fd->outputs.clks) 5588c2ecf20Sopenharmony_ci goto free; 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_ci init = kzalloc(sizeof(*init), GFP_KERNEL); 5618c2ecf20Sopenharmony_ci if (!init) 5628c2ecf20Sopenharmony_ci goto free; 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ci init->ops = &ti_fapll_ops; 5658c2ecf20Sopenharmony_ci name = ti_dt_clk_name(node); 5668c2ecf20Sopenharmony_ci init->name = name; 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_ci init->num_parents = of_clk_get_parent_count(node); 5698c2ecf20Sopenharmony_ci if (init->num_parents != 2) { 5708c2ecf20Sopenharmony_ci pr_err("%pOFn must have two parents\n", node); 5718c2ecf20Sopenharmony_ci goto free; 5728c2ecf20Sopenharmony_ci } 5738c2ecf20Sopenharmony_ci 5748c2ecf20Sopenharmony_ci of_clk_parent_fill(node, parent_name, 2); 5758c2ecf20Sopenharmony_ci init->parent_names = parent_name; 5768c2ecf20Sopenharmony_ci 5778c2ecf20Sopenharmony_ci fd->clk_ref = of_clk_get(node, 0); 5788c2ecf20Sopenharmony_ci if (IS_ERR(fd->clk_ref)) { 5798c2ecf20Sopenharmony_ci pr_err("%pOFn could not get clk_ref\n", node); 5808c2ecf20Sopenharmony_ci goto free; 5818c2ecf20Sopenharmony_ci } 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_ci fd->clk_bypass = of_clk_get(node, 1); 5848c2ecf20Sopenharmony_ci if (IS_ERR(fd->clk_bypass)) { 5858c2ecf20Sopenharmony_ci pr_err("%pOFn could not get clk_bypass\n", node); 5868c2ecf20Sopenharmony_ci goto free; 5878c2ecf20Sopenharmony_ci } 5888c2ecf20Sopenharmony_ci 5898c2ecf20Sopenharmony_ci fd->base = of_iomap(node, 0); 5908c2ecf20Sopenharmony_ci if (!fd->base) { 5918c2ecf20Sopenharmony_ci pr_err("%pOFn could not get IO base\n", node); 5928c2ecf20Sopenharmony_ci goto free; 5938c2ecf20Sopenharmony_ci } 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci if (fapll_is_ddr_pll(fd->base)) 5968c2ecf20Sopenharmony_ci fd->bypass_bit_inverted = true; 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_ci fd->name = name; 5998c2ecf20Sopenharmony_ci fd->hw.init = init; 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_ci /* Register the parent PLL */ 6028c2ecf20Sopenharmony_ci pll_clk = clk_register(NULL, &fd->hw); 6038c2ecf20Sopenharmony_ci if (IS_ERR(pll_clk)) 6048c2ecf20Sopenharmony_ci goto unmap; 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_ci fd->outputs.clks[0] = pll_clk; 6078c2ecf20Sopenharmony_ci fd->outputs.clk_num++; 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci /* 6108c2ecf20Sopenharmony_ci * Set up the child synthesizers starting at index 1 as the 6118c2ecf20Sopenharmony_ci * PLL output is at index 0. We need to check the clock-indices 6128c2ecf20Sopenharmony_ci * for numbering in case there are holes in the synth mapping, 6138c2ecf20Sopenharmony_ci * and then probe the synth register to see if it has a FREQ 6148c2ecf20Sopenharmony_ci * register available. 6158c2ecf20Sopenharmony_ci */ 6168c2ecf20Sopenharmony_ci for (i = 0; i < MAX_FAPLL_OUTPUTS; i++) { 6178c2ecf20Sopenharmony_ci const char *output_name; 6188c2ecf20Sopenharmony_ci void __iomem *freq, *div; 6198c2ecf20Sopenharmony_ci struct clk *synth_clk; 6208c2ecf20Sopenharmony_ci int output_instance; 6218c2ecf20Sopenharmony_ci u32 v; 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci if (of_property_read_string_index(node, "clock-output-names", 6248c2ecf20Sopenharmony_ci i, &output_name)) 6258c2ecf20Sopenharmony_ci continue; 6268c2ecf20Sopenharmony_ci 6278c2ecf20Sopenharmony_ci if (of_property_read_u32_index(node, "clock-indices", i, 6288c2ecf20Sopenharmony_ci &output_instance)) 6298c2ecf20Sopenharmony_ci output_instance = i; 6308c2ecf20Sopenharmony_ci 6318c2ecf20Sopenharmony_ci freq = fd->base + (output_instance * 8); 6328c2ecf20Sopenharmony_ci div = freq + 4; 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_ci /* Check for hardwired audio_pll_clk1 */ 6358c2ecf20Sopenharmony_ci if (is_audio_pll_clk1(freq)) { 6368c2ecf20Sopenharmony_ci freq = NULL; 6378c2ecf20Sopenharmony_ci div = NULL; 6388c2ecf20Sopenharmony_ci } else { 6398c2ecf20Sopenharmony_ci /* Does the synthesizer have a FREQ register? */ 6408c2ecf20Sopenharmony_ci v = readl_relaxed(freq); 6418c2ecf20Sopenharmony_ci if (!v) 6428c2ecf20Sopenharmony_ci freq = NULL; 6438c2ecf20Sopenharmony_ci } 6448c2ecf20Sopenharmony_ci synth_clk = ti_fapll_synth_setup(fd, freq, div, output_instance, 6458c2ecf20Sopenharmony_ci output_name, name, pll_clk); 6468c2ecf20Sopenharmony_ci if (IS_ERR(synth_clk)) 6478c2ecf20Sopenharmony_ci continue; 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci fd->outputs.clks[output_instance] = synth_clk; 6508c2ecf20Sopenharmony_ci fd->outputs.clk_num++; 6518c2ecf20Sopenharmony_ci 6528c2ecf20Sopenharmony_ci clk_register_clkdev(synth_clk, output_name, NULL); 6538c2ecf20Sopenharmony_ci } 6548c2ecf20Sopenharmony_ci 6558c2ecf20Sopenharmony_ci /* Register the child synthesizers as the FAPLL outputs */ 6568c2ecf20Sopenharmony_ci of_clk_add_provider(node, of_clk_src_onecell_get, &fd->outputs); 6578c2ecf20Sopenharmony_ci /* Add clock alias for the outputs */ 6588c2ecf20Sopenharmony_ci 6598c2ecf20Sopenharmony_ci kfree(init); 6608c2ecf20Sopenharmony_ci 6618c2ecf20Sopenharmony_ci return; 6628c2ecf20Sopenharmony_ci 6638c2ecf20Sopenharmony_ciunmap: 6648c2ecf20Sopenharmony_ci iounmap(fd->base); 6658c2ecf20Sopenharmony_cifree: 6668c2ecf20Sopenharmony_ci if (fd->clk_bypass) 6678c2ecf20Sopenharmony_ci clk_put(fd->clk_bypass); 6688c2ecf20Sopenharmony_ci if (fd->clk_ref) 6698c2ecf20Sopenharmony_ci clk_put(fd->clk_ref); 6708c2ecf20Sopenharmony_ci kfree(fd->outputs.clks); 6718c2ecf20Sopenharmony_ci kfree(fd); 6728c2ecf20Sopenharmony_ci kfree(init); 6738c2ecf20Sopenharmony_ci} 6748c2ecf20Sopenharmony_ci 6758c2ecf20Sopenharmony_ciCLK_OF_DECLARE(ti_fapll_clock, "ti,dm816-fapll-clock", ti_fapll_setup); 676