xref: /linux/drivers/clk/analogbits/wrpll-cln28hpc.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
17b9487a9SPaul Walmsley // SPDX-License-Identifier: GPL-2.0
27b9487a9SPaul Walmsley /*
37b9487a9SPaul Walmsley  * Copyright (C) 2018-2019 SiFive, Inc.
47b9487a9SPaul Walmsley  * Wesley Terpstra
57b9487a9SPaul Walmsley  * Paul Walmsley
67b9487a9SPaul Walmsley  *
77b9487a9SPaul Walmsley  * This library supports configuration parsing and reprogramming of
87b9487a9SPaul Walmsley  * the CLN28HPC variant of the Analog Bits Wide Range PLL.  The
97b9487a9SPaul Walmsley  * intention is for this library to be reusable for any device that
107b9487a9SPaul Walmsley  * integrates this PLL; thus the register structure and programming
117b9487a9SPaul Walmsley  * details are expected to be provided by a separate IP block driver.
127b9487a9SPaul Walmsley  *
137b9487a9SPaul Walmsley  * The bulk of this code is primarily useful for clock configurations
147b9487a9SPaul Walmsley  * that must operate at arbitrary rates, as opposed to clock configurations
157b9487a9SPaul Walmsley  * that are restricted by software or manufacturer guidance to a small,
167b9487a9SPaul Walmsley  * pre-determined set of performance points.
177b9487a9SPaul Walmsley  *
187b9487a9SPaul Walmsley  * References:
197b9487a9SPaul Walmsley  * - Analog Bits "Wide Range PLL Datasheet", version 2015.10.01
207b9487a9SPaul Walmsley  * - SiFive FU540-C000 Manual v1p0, Chapter 7 "Clocking and Reset"
217b9487a9SPaul Walmsley  *   https://static.dev.sifive.com/FU540-C000-v1.0.pdf
227b9487a9SPaul Walmsley  */
237b9487a9SPaul Walmsley 
247b9487a9SPaul Walmsley #include <linux/bug.h>
257b9487a9SPaul Walmsley #include <linux/err.h>
26f39650deSAndy Shevchenko #include <linux/limits.h>
277b9487a9SPaul Walmsley #include <linux/log2.h>
287b9487a9SPaul Walmsley #include <linux/math64.h>
29f39650deSAndy Shevchenko #include <linux/math.h>
30f39650deSAndy Shevchenko #include <linux/minmax.h>
31*ee58d6a1SSamuel Holland #include <linux/module.h>
32f39650deSAndy Shevchenko 
337b9487a9SPaul Walmsley #include <linux/clk/analogbits-wrpll-cln28hpc.h>
347b9487a9SPaul Walmsley 
357b9487a9SPaul Walmsley /* MIN_INPUT_FREQ: minimum input clock frequency, in Hz (Fref_min) */
367b9487a9SPaul Walmsley #define MIN_INPUT_FREQ			7000000
377b9487a9SPaul Walmsley 
387b9487a9SPaul Walmsley /* MAX_INPUT_FREQ: maximum input clock frequency, in Hz (Fref_max) */
397b9487a9SPaul Walmsley #define MAX_INPUT_FREQ			600000000
407b9487a9SPaul Walmsley 
417b9487a9SPaul Walmsley /* MIN_POST_DIVIDE_REF_FREQ: minimum post-divider reference frequency, in Hz */
427b9487a9SPaul Walmsley #define MIN_POST_DIVR_FREQ		7000000
437b9487a9SPaul Walmsley 
447b9487a9SPaul Walmsley /* MAX_POST_DIVIDE_REF_FREQ: maximum post-divider reference frequency, in Hz */
457b9487a9SPaul Walmsley #define MAX_POST_DIVR_FREQ		200000000
467b9487a9SPaul Walmsley 
477b9487a9SPaul Walmsley /* MIN_VCO_FREQ: minimum VCO frequency, in Hz (Fvco_min) */
487b9487a9SPaul Walmsley #define MIN_VCO_FREQ			2400000000UL
497b9487a9SPaul Walmsley 
507b9487a9SPaul Walmsley /* MAX_VCO_FREQ: maximum VCO frequency, in Hz (Fvco_max) */
517b9487a9SPaul Walmsley #define MAX_VCO_FREQ			4800000000ULL
527b9487a9SPaul Walmsley 
537b9487a9SPaul Walmsley /* MAX_DIVQ_DIVISOR: maximum output divisor.  Selected by DIVQ = 6 */
547b9487a9SPaul Walmsley #define MAX_DIVQ_DIVISOR		64
557b9487a9SPaul Walmsley 
567b9487a9SPaul Walmsley /* MAX_DIVR_DIVISOR: maximum reference divisor.  Selected by DIVR = 63 */
577b9487a9SPaul Walmsley #define MAX_DIVR_DIVISOR		64
587b9487a9SPaul Walmsley 
597b9487a9SPaul Walmsley /* MAX_LOCK_US: maximum PLL lock time, in microseconds (tLOCK_max) */
607b9487a9SPaul Walmsley #define MAX_LOCK_US			70
617b9487a9SPaul Walmsley 
627b9487a9SPaul Walmsley /*
637b9487a9SPaul Walmsley  * ROUND_SHIFT: number of bits to shift to avoid precision loss in the rounding
647b9487a9SPaul Walmsley  *              algorithm
657b9487a9SPaul Walmsley  */
667b9487a9SPaul Walmsley #define ROUND_SHIFT			20
677b9487a9SPaul Walmsley 
687b9487a9SPaul Walmsley /*
697b9487a9SPaul Walmsley  * Private functions
707b9487a9SPaul Walmsley  */
717b9487a9SPaul Walmsley 
727b9487a9SPaul Walmsley /**
737b9487a9SPaul Walmsley  * __wrpll_calc_filter_range() - determine PLL loop filter bandwidth
747b9487a9SPaul Walmsley  * @post_divr_freq: input clock rate after the R divider
757b9487a9SPaul Walmsley  *
767b9487a9SPaul Walmsley  * Select the value to be presented to the PLL RANGE input signals, based
777b9487a9SPaul Walmsley  * on the input clock frequency after the post-R-divider @post_divr_freq.
787b9487a9SPaul Walmsley  * This code follows the recommendations in the PLL datasheet for filter
797b9487a9SPaul Walmsley  * range selection.
807b9487a9SPaul Walmsley  *
817b9487a9SPaul Walmsley  * Return: The RANGE value to be presented to the PLL configuration inputs,
827b9487a9SPaul Walmsley  *         or a negative return code upon error.
837b9487a9SPaul Walmsley  */
__wrpll_calc_filter_range(unsigned long post_divr_freq)847b9487a9SPaul Walmsley static int __wrpll_calc_filter_range(unsigned long post_divr_freq)
857b9487a9SPaul Walmsley {
867b9487a9SPaul Walmsley 	if (post_divr_freq < MIN_POST_DIVR_FREQ ||
877b9487a9SPaul Walmsley 	    post_divr_freq > MAX_POST_DIVR_FREQ) {
887b9487a9SPaul Walmsley 		WARN(1, "%s: post-divider reference freq out of range: %lu",
897b9487a9SPaul Walmsley 		     __func__, post_divr_freq);
907b9487a9SPaul Walmsley 		return -ERANGE;
917b9487a9SPaul Walmsley 	}
927b9487a9SPaul Walmsley 
937b9487a9SPaul Walmsley 	switch (post_divr_freq) {
947b9487a9SPaul Walmsley 	case 0 ... 10999999:
957b9487a9SPaul Walmsley 		return 1;
967b9487a9SPaul Walmsley 	case 11000000 ... 17999999:
977b9487a9SPaul Walmsley 		return 2;
987b9487a9SPaul Walmsley 	case 18000000 ... 29999999:
997b9487a9SPaul Walmsley 		return 3;
1007b9487a9SPaul Walmsley 	case 30000000 ... 49999999:
1017b9487a9SPaul Walmsley 		return 4;
1027b9487a9SPaul Walmsley 	case 50000000 ... 79999999:
1037b9487a9SPaul Walmsley 		return 5;
1047b9487a9SPaul Walmsley 	case 80000000 ... 129999999:
1057b9487a9SPaul Walmsley 		return 6;
1067b9487a9SPaul Walmsley 	}
1077b9487a9SPaul Walmsley 
1087b9487a9SPaul Walmsley 	return 7;
1097b9487a9SPaul Walmsley }
1107b9487a9SPaul Walmsley 
1117b9487a9SPaul Walmsley /**
1127b9487a9SPaul Walmsley  * __wrpll_calc_fbdiv() - return feedback fixed divide value
1137b9487a9SPaul Walmsley  * @c: ptr to a struct wrpll_cfg record to read from
1147b9487a9SPaul Walmsley  *
1157b9487a9SPaul Walmsley  * The internal feedback path includes a fixed by-two divider; the
1167b9487a9SPaul Walmsley  * external feedback path does not.  Return the appropriate divider
1177b9487a9SPaul Walmsley  * value (2 or 1) depending on whether internal or external feedback
1187b9487a9SPaul Walmsley  * is enabled.  This code doesn't test for invalid configurations
1197b9487a9SPaul Walmsley  * (e.g. both or neither of WRPLL_FLAGS_*_FEEDBACK are set); it relies
1207b9487a9SPaul Walmsley  * on the caller to do so.
1217b9487a9SPaul Walmsley  *
1227b9487a9SPaul Walmsley  * Context: Any context.  Caller must protect the memory pointed to by
1237b9487a9SPaul Walmsley  *          @c from simultaneous modification.
1247b9487a9SPaul Walmsley  *
1257b9487a9SPaul Walmsley  * Return: 2 if internal feedback is enabled or 1 if external feedback
1267b9487a9SPaul Walmsley  *         is enabled.
1277b9487a9SPaul Walmsley  */
__wrpll_calc_fbdiv(const struct wrpll_cfg * c)1287b9487a9SPaul Walmsley static u8 __wrpll_calc_fbdiv(const struct wrpll_cfg *c)
1297b9487a9SPaul Walmsley {
1307b9487a9SPaul Walmsley 	return (c->flags & WRPLL_FLAGS_INT_FEEDBACK_MASK) ? 2 : 1;
1317b9487a9SPaul Walmsley }
1327b9487a9SPaul Walmsley 
1337b9487a9SPaul Walmsley /**
1347b9487a9SPaul Walmsley  * __wrpll_calc_divq() - determine DIVQ based on target PLL output clock rate
1357b9487a9SPaul Walmsley  * @target_rate: target PLL output clock rate
1367b9487a9SPaul Walmsley  * @vco_rate: pointer to a u64 to store the computed VCO rate into
1377b9487a9SPaul Walmsley  *
1387b9487a9SPaul Walmsley  * Determine a reasonable value for the PLL Q post-divider, based on the
1397b9487a9SPaul Walmsley  * target output rate @target_rate for the PLL.  Along with returning the
1407b9487a9SPaul Walmsley  * computed Q divider value as the return value, this function stores the
1417b9487a9SPaul Walmsley  * desired target VCO rate into the variable pointed to by @vco_rate.
1427b9487a9SPaul Walmsley  *
1437b9487a9SPaul Walmsley  * Context: Any context.  Caller must protect the memory pointed to by
1447b9487a9SPaul Walmsley  *          @vco_rate from simultaneous access or modification.
1457b9487a9SPaul Walmsley  *
1467b9487a9SPaul Walmsley  * Return: a positive integer DIVQ value to be programmed into the hardware
1477b9487a9SPaul Walmsley  *         upon success, or 0 upon error (since 0 is an invalid DIVQ value)
1487b9487a9SPaul Walmsley  */
__wrpll_calc_divq(u32 target_rate,u64 * vco_rate)1497b9487a9SPaul Walmsley static u8 __wrpll_calc_divq(u32 target_rate, u64 *vco_rate)
1507b9487a9SPaul Walmsley {
1517b9487a9SPaul Walmsley 	u64 s;
1527b9487a9SPaul Walmsley 	u8 divq = 0;
1537b9487a9SPaul Walmsley 
1547b9487a9SPaul Walmsley 	if (!vco_rate) {
1557b9487a9SPaul Walmsley 		WARN_ON(1);
1567b9487a9SPaul Walmsley 		goto wcd_out;
1577b9487a9SPaul Walmsley 	}
1587b9487a9SPaul Walmsley 
1597b9487a9SPaul Walmsley 	s = div_u64(MAX_VCO_FREQ, target_rate);
1607b9487a9SPaul Walmsley 	if (s <= 1) {
1617b9487a9SPaul Walmsley 		divq = 1;
1627b9487a9SPaul Walmsley 		*vco_rate = MAX_VCO_FREQ;
1637b9487a9SPaul Walmsley 	} else if (s > MAX_DIVQ_DIVISOR) {
1647b9487a9SPaul Walmsley 		divq = ilog2(MAX_DIVQ_DIVISOR);
1657b9487a9SPaul Walmsley 		*vco_rate = MIN_VCO_FREQ;
1667b9487a9SPaul Walmsley 	} else {
1677b9487a9SPaul Walmsley 		divq = ilog2(s);
1687b9487a9SPaul Walmsley 		*vco_rate = (u64)target_rate << divq;
1697b9487a9SPaul Walmsley 	}
1707b9487a9SPaul Walmsley 
1717b9487a9SPaul Walmsley wcd_out:
1727b9487a9SPaul Walmsley 	return divq;
1737b9487a9SPaul Walmsley }
1747b9487a9SPaul Walmsley 
1757b9487a9SPaul Walmsley /**
1767b9487a9SPaul Walmsley  * __wrpll_update_parent_rate() - update PLL data when parent rate changes
1777b9487a9SPaul Walmsley  * @c: ptr to a struct wrpll_cfg record to write PLL data to
1787b9487a9SPaul Walmsley  * @parent_rate: PLL input refclk rate (pre-R-divider)
1797b9487a9SPaul Walmsley  *
1807b9487a9SPaul Walmsley  * Pre-compute some data used by the PLL configuration algorithm when
1817b9487a9SPaul Walmsley  * the PLL's reference clock rate changes.  The intention is to avoid
1827b9487a9SPaul Walmsley  * computation when the parent rate remains constant - expected to be
1837b9487a9SPaul Walmsley  * the common case.
1847b9487a9SPaul Walmsley  *
1857b9487a9SPaul Walmsley  * Returns: 0 upon success or -ERANGE if the reference clock rate is
1867b9487a9SPaul Walmsley  * out of range.
1877b9487a9SPaul Walmsley  */
__wrpll_update_parent_rate(struct wrpll_cfg * c,unsigned long parent_rate)1887b9487a9SPaul Walmsley static int __wrpll_update_parent_rate(struct wrpll_cfg *c,
1897b9487a9SPaul Walmsley 				      unsigned long parent_rate)
1907b9487a9SPaul Walmsley {
1917b9487a9SPaul Walmsley 	u8 max_r_for_parent;
1927b9487a9SPaul Walmsley 
1937b9487a9SPaul Walmsley 	if (parent_rate > MAX_INPUT_FREQ || parent_rate < MIN_POST_DIVR_FREQ)
1947b9487a9SPaul Walmsley 		return -ERANGE;
1957b9487a9SPaul Walmsley 
1967b9487a9SPaul Walmsley 	c->parent_rate = parent_rate;
1977b9487a9SPaul Walmsley 	max_r_for_parent = div_u64(parent_rate, MIN_POST_DIVR_FREQ);
1987b9487a9SPaul Walmsley 	c->max_r = min_t(u8, MAX_DIVR_DIVISOR, max_r_for_parent);
1997b9487a9SPaul Walmsley 
2007b9487a9SPaul Walmsley 	c->init_r = DIV_ROUND_UP_ULL(parent_rate, MAX_POST_DIVR_FREQ);
2017b9487a9SPaul Walmsley 
2027b9487a9SPaul Walmsley 	return 0;
2037b9487a9SPaul Walmsley }
2047b9487a9SPaul Walmsley 
2057b9487a9SPaul Walmsley /**
20606abc753SYang Yingliang  * wrpll_configure_for_rate() - compute PLL configuration for a target rate
2077b9487a9SPaul Walmsley  * @c: ptr to a struct wrpll_cfg record to write into
2087b9487a9SPaul Walmsley  * @target_rate: target PLL output clock rate (post-Q-divider)
2097b9487a9SPaul Walmsley  * @parent_rate: PLL input refclk rate (pre-R-divider)
2107b9487a9SPaul Walmsley  *
2117b9487a9SPaul Walmsley  * Compute the appropriate PLL signal configuration values and store
2127b9487a9SPaul Walmsley  * in PLL context @c.  PLL reprogramming is not glitchless, so the
2137b9487a9SPaul Walmsley  * caller should switch any downstream logic to a different clock
2147b9487a9SPaul Walmsley  * source or clock-gate it before presenting these values to the PLL
2157b9487a9SPaul Walmsley  * configuration signals.
2167b9487a9SPaul Walmsley  *
2177b9487a9SPaul Walmsley  * The caller must pass this function a pre-initialized struct
2187b9487a9SPaul Walmsley  * wrpll_cfg record: either initialized to zero (with the
2197b9487a9SPaul Walmsley  * exception of the .name and .flags fields) or read from the PLL.
2207b9487a9SPaul Walmsley  *
2217b9487a9SPaul Walmsley  * Context: Any context.  Caller must protect the memory pointed to by @c
2227b9487a9SPaul Walmsley  *          from simultaneous access or modification.
2237b9487a9SPaul Walmsley  *
2247b9487a9SPaul Walmsley  * Return: 0 upon success; anything else upon failure.
2257b9487a9SPaul Walmsley  */
wrpll_configure_for_rate(struct wrpll_cfg * c,u32 target_rate,unsigned long parent_rate)2267b9487a9SPaul Walmsley int wrpll_configure_for_rate(struct wrpll_cfg *c, u32 target_rate,
2277b9487a9SPaul Walmsley 			     unsigned long parent_rate)
2287b9487a9SPaul Walmsley {
2297b9487a9SPaul Walmsley 	unsigned long ratio;
2307b9487a9SPaul Walmsley 	u64 target_vco_rate, delta, best_delta, f_pre_div, vco, vco_pre;
2317b9487a9SPaul Walmsley 	u32 best_f, f, post_divr_freq;
2327b9487a9SPaul Walmsley 	u8 fbdiv, divq, best_r, r;
2337b9487a9SPaul Walmsley 	int range;
2347b9487a9SPaul Walmsley 
2357b9487a9SPaul Walmsley 	if (c->flags == 0) {
2367b9487a9SPaul Walmsley 		WARN(1, "%s called with uninitialized PLL config", __func__);
2377b9487a9SPaul Walmsley 		return -EINVAL;
2387b9487a9SPaul Walmsley 	}
2397b9487a9SPaul Walmsley 
2407b9487a9SPaul Walmsley 	/* Initialize rounding data if it hasn't been initialized already */
2417b9487a9SPaul Walmsley 	if (parent_rate != c->parent_rate) {
2427b9487a9SPaul Walmsley 		if (__wrpll_update_parent_rate(c, parent_rate)) {
2437b9487a9SPaul Walmsley 			pr_err("%s: PLL input rate is out of range\n",
2447b9487a9SPaul Walmsley 			       __func__);
2457b9487a9SPaul Walmsley 			return -ERANGE;
2467b9487a9SPaul Walmsley 		}
2477b9487a9SPaul Walmsley 	}
2487b9487a9SPaul Walmsley 
2497b9487a9SPaul Walmsley 	c->flags &= ~WRPLL_FLAGS_RESET_MASK;
2507b9487a9SPaul Walmsley 
2517b9487a9SPaul Walmsley 	/* Put the PLL into bypass if the user requests the parent clock rate */
2527b9487a9SPaul Walmsley 	if (target_rate == parent_rate) {
2537b9487a9SPaul Walmsley 		c->flags |= WRPLL_FLAGS_BYPASS_MASK;
2547b9487a9SPaul Walmsley 		return 0;
2557b9487a9SPaul Walmsley 	}
2567b9487a9SPaul Walmsley 
2577b9487a9SPaul Walmsley 	c->flags &= ~WRPLL_FLAGS_BYPASS_MASK;
2587b9487a9SPaul Walmsley 
2597b9487a9SPaul Walmsley 	/* Calculate the Q shift and target VCO rate */
2607b9487a9SPaul Walmsley 	divq = __wrpll_calc_divq(target_rate, &target_vco_rate);
2617b9487a9SPaul Walmsley 	if (!divq)
2627b9487a9SPaul Walmsley 		return -1;
2637b9487a9SPaul Walmsley 	c->divq = divq;
2647b9487a9SPaul Walmsley 
2657b9487a9SPaul Walmsley 	/* Precalculate the pre-Q divider target ratio */
2667b9487a9SPaul Walmsley 	ratio = div64_u64((target_vco_rate << ROUND_SHIFT), parent_rate);
2677b9487a9SPaul Walmsley 
2687b9487a9SPaul Walmsley 	fbdiv = __wrpll_calc_fbdiv(c);
2697b9487a9SPaul Walmsley 	best_r = 0;
2707b9487a9SPaul Walmsley 	best_f = 0;
2717b9487a9SPaul Walmsley 	best_delta = MAX_VCO_FREQ;
2727b9487a9SPaul Walmsley 
2737b9487a9SPaul Walmsley 	/*
2747b9487a9SPaul Walmsley 	 * Consider all values for R which land within
2757b9487a9SPaul Walmsley 	 * [MIN_POST_DIVR_FREQ, MAX_POST_DIVR_FREQ]; prefer smaller R
2767b9487a9SPaul Walmsley 	 */
2777b9487a9SPaul Walmsley 	for (r = c->init_r; r <= c->max_r; ++r) {
2787b9487a9SPaul Walmsley 		f_pre_div = ratio * r;
2797b9487a9SPaul Walmsley 		f = (f_pre_div + (1 << ROUND_SHIFT)) >> ROUND_SHIFT;
2807b9487a9SPaul Walmsley 		f >>= (fbdiv - 1);
2817b9487a9SPaul Walmsley 
2827b9487a9SPaul Walmsley 		post_divr_freq = div_u64(parent_rate, r);
2837b9487a9SPaul Walmsley 		vco_pre = fbdiv * post_divr_freq;
2847b9487a9SPaul Walmsley 		vco = vco_pre * f;
2857b9487a9SPaul Walmsley 
2867b9487a9SPaul Walmsley 		/* Ensure rounding didn't take us out of range */
2877b9487a9SPaul Walmsley 		if (vco > target_vco_rate) {
2887b9487a9SPaul Walmsley 			--f;
2897b9487a9SPaul Walmsley 			vco = vco_pre * f;
2907b9487a9SPaul Walmsley 		} else if (vco < MIN_VCO_FREQ) {
2917b9487a9SPaul Walmsley 			++f;
2927b9487a9SPaul Walmsley 			vco = vco_pre * f;
2937b9487a9SPaul Walmsley 		}
2947b9487a9SPaul Walmsley 
2957b9487a9SPaul Walmsley 		delta = abs(target_rate - vco);
2967b9487a9SPaul Walmsley 		if (delta < best_delta) {
2977b9487a9SPaul Walmsley 			best_delta = delta;
2987b9487a9SPaul Walmsley 			best_r = r;
2997b9487a9SPaul Walmsley 			best_f = f;
3007b9487a9SPaul Walmsley 		}
3017b9487a9SPaul Walmsley 	}
3027b9487a9SPaul Walmsley 
3037b9487a9SPaul Walmsley 	c->divr = best_r - 1;
3047b9487a9SPaul Walmsley 	c->divf = best_f - 1;
3057b9487a9SPaul Walmsley 
3067b9487a9SPaul Walmsley 	post_divr_freq = div_u64(parent_rate, best_r);
3077b9487a9SPaul Walmsley 
3087b9487a9SPaul Walmsley 	/* Pick the best PLL jitter filter */
3097b9487a9SPaul Walmsley 	range = __wrpll_calc_filter_range(post_divr_freq);
3107b9487a9SPaul Walmsley 	if (range < 0)
3117b9487a9SPaul Walmsley 		return range;
3127b9487a9SPaul Walmsley 	c->range = range;
3137b9487a9SPaul Walmsley 
3147b9487a9SPaul Walmsley 	return 0;
3157b9487a9SPaul Walmsley }
316*ee58d6a1SSamuel Holland EXPORT_SYMBOL_GPL(wrpll_configure_for_rate);
3177b9487a9SPaul Walmsley 
3187b9487a9SPaul Walmsley /**
3197b9487a9SPaul Walmsley  * wrpll_calc_output_rate() - calculate the PLL's target output rate
3207b9487a9SPaul Walmsley  * @c: ptr to a struct wrpll_cfg record to read from
3217b9487a9SPaul Walmsley  * @parent_rate: PLL refclk rate
3227b9487a9SPaul Walmsley  *
3237b9487a9SPaul Walmsley  * Given a pointer to the PLL's current input configuration @c and the
3247b9487a9SPaul Walmsley  * PLL's input reference clock rate @parent_rate (before the R
3257b9487a9SPaul Walmsley  * pre-divider), calculate the PLL's output clock rate (after the Q
3267b9487a9SPaul Walmsley  * post-divider).
3277b9487a9SPaul Walmsley  *
3287b9487a9SPaul Walmsley  * Context: Any context.  Caller must protect the memory pointed to by @c
3297b9487a9SPaul Walmsley  *          from simultaneous modification.
3307b9487a9SPaul Walmsley  *
3317b9487a9SPaul Walmsley  * Return: the PLL's output clock rate, in Hz.  The return value from
3327b9487a9SPaul Walmsley  *         this function is intended to be convenient to pass directly
3337b9487a9SPaul Walmsley  *         to the Linux clock framework; thus there is no explicit
3347b9487a9SPaul Walmsley  *         error return value.
3357b9487a9SPaul Walmsley  */
wrpll_calc_output_rate(const struct wrpll_cfg * c,unsigned long parent_rate)3367b9487a9SPaul Walmsley unsigned long wrpll_calc_output_rate(const struct wrpll_cfg *c,
3377b9487a9SPaul Walmsley 				     unsigned long parent_rate)
3387b9487a9SPaul Walmsley {
3397b9487a9SPaul Walmsley 	u8 fbdiv;
3407b9487a9SPaul Walmsley 	u64 n;
3417b9487a9SPaul Walmsley 
3427b9487a9SPaul Walmsley 	if (c->flags & WRPLL_FLAGS_EXT_FEEDBACK_MASK) {
3437b9487a9SPaul Walmsley 		WARN(1, "external feedback mode not yet supported");
3447b9487a9SPaul Walmsley 		return ULONG_MAX;
3457b9487a9SPaul Walmsley 	}
3467b9487a9SPaul Walmsley 
3477b9487a9SPaul Walmsley 	fbdiv = __wrpll_calc_fbdiv(c);
3487b9487a9SPaul Walmsley 	n = parent_rate * fbdiv * (c->divf + 1);
3497b9487a9SPaul Walmsley 	n = div_u64(n, c->divr + 1);
3507b9487a9SPaul Walmsley 	n >>= c->divq;
3517b9487a9SPaul Walmsley 
3527b9487a9SPaul Walmsley 	return n;
3537b9487a9SPaul Walmsley }
354*ee58d6a1SSamuel Holland EXPORT_SYMBOL_GPL(wrpll_calc_output_rate);
3557b9487a9SPaul Walmsley 
3567b9487a9SPaul Walmsley /**
3577b9487a9SPaul Walmsley  * wrpll_calc_max_lock_us() - return the time for the PLL to lock
3587b9487a9SPaul Walmsley  * @c: ptr to a struct wrpll_cfg record to read from
3597b9487a9SPaul Walmsley  *
3607b9487a9SPaul Walmsley  * Return the minimum amount of time (in microseconds) that the caller
3617b9487a9SPaul Walmsley  * must wait after reprogramming the PLL to ensure that it is locked
3627b9487a9SPaul Walmsley  * to the input frequency and stable.  This is likely to depend on the DIVR
3637b9487a9SPaul Walmsley  * value; this is under discussion with the manufacturer.
3647b9487a9SPaul Walmsley  *
3657b9487a9SPaul Walmsley  * Return: the minimum amount of time the caller must wait for the PLL
3667b9487a9SPaul Walmsley  *         to lock (in microseconds)
3677b9487a9SPaul Walmsley  */
wrpll_calc_max_lock_us(const struct wrpll_cfg * c)3687b9487a9SPaul Walmsley unsigned int wrpll_calc_max_lock_us(const struct wrpll_cfg *c)
3697b9487a9SPaul Walmsley {
3707b9487a9SPaul Walmsley 	return MAX_LOCK_US;
3717b9487a9SPaul Walmsley }
372*ee58d6a1SSamuel Holland EXPORT_SYMBOL_GPL(wrpll_calc_max_lock_us);
373*ee58d6a1SSamuel Holland 
374*ee58d6a1SSamuel Holland MODULE_AUTHOR("Paul Walmsley <paul.walmsley@sifive.com>");
375*ee58d6a1SSamuel Holland MODULE_DESCRIPTION("Analog Bits Wide-Range PLL library");
376*ee58d6a1SSamuel Holland MODULE_LICENSE("GPL");
377