xref: /netbsd-src/sys/arch/arm/nxp/imx_ccm_fixed_factor.c (revision 8644267a503e0f573f2833152303dbf2ce4aca52)
1*8644267aSskrll /* $NetBSD: imx_ccm_fixed_factor.c,v 1.1 2020/12/23 14:42:38 skrll Exp $ */
2*8644267aSskrll 
3*8644267aSskrll /*-
4*8644267aSskrll  * Copyright (c) 2020 Jared McNeill <jmcneill@invisible.ca>
5*8644267aSskrll  * All rights reserved.
6*8644267aSskrll  *
7*8644267aSskrll  * Redistribution and use in source and binary forms, with or without
8*8644267aSskrll  * modification, are permitted provided that the following conditions
9*8644267aSskrll  * are met:
10*8644267aSskrll  * 1. Redistributions of source code must retain the above copyright
11*8644267aSskrll  *    notice, this list of conditions and the following disclaimer.
12*8644267aSskrll  * 2. Redistributions in binary form must reproduce the above copyright
13*8644267aSskrll  *    notice, this list of conditions and the following disclaimer in the
14*8644267aSskrll  *    documentation and/or other materials provided with the distribution.
15*8644267aSskrll  *
16*8644267aSskrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*8644267aSskrll  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*8644267aSskrll  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*8644267aSskrll  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8644267aSskrll  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8644267aSskrll  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22*8644267aSskrll  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8644267aSskrll  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8644267aSskrll  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8644267aSskrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8644267aSskrll  * SUCH DAMAGE.
27*8644267aSskrll  */
28*8644267aSskrll 
29*8644267aSskrll #include <sys/cdefs.h>
30*8644267aSskrll __KERNEL_RCSID(0, "$NetBSD: imx_ccm_fixed_factor.c,v 1.1 2020/12/23 14:42:38 skrll Exp $");
31*8644267aSskrll 
32*8644267aSskrll #include <sys/param.h>
33*8644267aSskrll #include <sys/bus.h>
34*8644267aSskrll 
35*8644267aSskrll #include <dev/clk/clk_backend.h>
36*8644267aSskrll 
37*8644267aSskrll #include <arm/nxp/imx_ccm.h>
38*8644267aSskrll 
39*8644267aSskrll static u_int
imx_ccm_fixed_factor_get_parent_rate(struct clk * clkp)40*8644267aSskrll imx_ccm_fixed_factor_get_parent_rate(struct clk *clkp)
41*8644267aSskrll {
42*8644267aSskrll 	struct clk *clkp_parent;
43*8644267aSskrll 
44*8644267aSskrll 	clkp_parent = clk_get_parent(clkp);
45*8644267aSskrll 	if (clkp_parent == NULL)
46*8644267aSskrll 		return 0;
47*8644267aSskrll 
48*8644267aSskrll 	return clk_get_rate(clkp_parent);
49*8644267aSskrll }
50*8644267aSskrll 
51*8644267aSskrll u_int
imx_ccm_fixed_factor_get_rate(struct imx_ccm_softc * sc,struct imx_ccm_clk * clk)52*8644267aSskrll imx_ccm_fixed_factor_get_rate(struct imx_ccm_softc *sc,
53*8644267aSskrll     struct imx_ccm_clk *clk)
54*8644267aSskrll {
55*8644267aSskrll 	struct imx_ccm_fixed_factor *fixed_factor = &clk->u.fixed_factor;
56*8644267aSskrll 	struct clk *clkp = &clk->base;
57*8644267aSskrll 
58*8644267aSskrll 	KASSERT(clk->type == IMX_CCM_FIXED_FACTOR);
59*8644267aSskrll 
60*8644267aSskrll 	const u_int p_rate = imx_ccm_fixed_factor_get_parent_rate(clkp);
61*8644267aSskrll 	if (p_rate == 0)
62*8644267aSskrll 		return 0;
63*8644267aSskrll 
64*8644267aSskrll 	return (u_int)(((uint64_t)p_rate * fixed_factor->mult) / fixed_factor->div);
65*8644267aSskrll }
66*8644267aSskrll 
67*8644267aSskrll static int
imx_ccm_fixed_factor_set_parent_rate(struct clk * clkp,u_int rate)68*8644267aSskrll imx_ccm_fixed_factor_set_parent_rate(struct clk *clkp, u_int rate)
69*8644267aSskrll {
70*8644267aSskrll 	struct clk *clkp_parent;
71*8644267aSskrll 
72*8644267aSskrll 	clkp_parent = clk_get_parent(clkp);
73*8644267aSskrll 	if (clkp_parent == NULL)
74*8644267aSskrll 		return ENXIO;
75*8644267aSskrll 
76*8644267aSskrll 	return clk_set_rate(clkp_parent, rate);
77*8644267aSskrll }
78*8644267aSskrll 
79*8644267aSskrll int
imx_ccm_fixed_factor_set_rate(struct imx_ccm_softc * sc,struct imx_ccm_clk * clk,u_int rate)80*8644267aSskrll imx_ccm_fixed_factor_set_rate(struct imx_ccm_softc *sc,
81*8644267aSskrll     struct imx_ccm_clk *clk, u_int rate)
82*8644267aSskrll {
83*8644267aSskrll 	struct imx_ccm_fixed_factor *fixed_factor = &clk->u.fixed_factor;
84*8644267aSskrll 	struct clk *clkp = &clk->base;
85*8644267aSskrll 
86*8644267aSskrll 	KASSERT(clk->type == IMX_CCM_FIXED_FACTOR);
87*8644267aSskrll 
88*8644267aSskrll 	rate *= fixed_factor->div;
89*8644267aSskrll 	rate /= fixed_factor->mult;
90*8644267aSskrll 
91*8644267aSskrll 	return imx_ccm_fixed_factor_set_parent_rate(clkp, rate);
92*8644267aSskrll }
93*8644267aSskrll 
94*8644267aSskrll const char *
imx_ccm_fixed_factor_get_parent(struct imx_ccm_softc * sc,struct imx_ccm_clk * clk)95*8644267aSskrll imx_ccm_fixed_factor_get_parent(struct imx_ccm_softc *sc,
96*8644267aSskrll     struct imx_ccm_clk *clk)
97*8644267aSskrll {
98*8644267aSskrll 	struct imx_ccm_fixed_factor *fixed_factor = &clk->u.fixed_factor;
99*8644267aSskrll 
100*8644267aSskrll 	KASSERT(clk->type == IMX_CCM_FIXED_FACTOR);
101*8644267aSskrll 
102*8644267aSskrll 	return fixed_factor->parent;
103*8644267aSskrll }
104