1*8644267aSskrll /* $NetBSD: imx_ccm_extclk.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_extclk.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 #include <dev/fdt/fdtvar.h>
40*8644267aSskrll
41*8644267aSskrll int
imx_ccm_extclk_enable(struct imx_ccm_softc * sc,struct imx_ccm_clk * clk,int enable)42*8644267aSskrll imx_ccm_extclk_enable(struct imx_ccm_softc *sc, struct imx_ccm_clk *clk,
43*8644267aSskrll int enable)
44*8644267aSskrll {
45*8644267aSskrll struct clk *extclk;
46*8644267aSskrll
47*8644267aSskrll KASSERT(clk->type == IMX_CCM_EXTCLK);
48*8644267aSskrll
49*8644267aSskrll if ((extclk = fdtbus_clock_byname(clk->u.extclk)) == NULL)
50*8644267aSskrll return ENXIO;
51*8644267aSskrll
52*8644267aSskrll return clk_enable(extclk);
53*8644267aSskrll }
54*8644267aSskrll
55*8644267aSskrll u_int
imx_ccm_extclk_get_rate(struct imx_ccm_softc * sc,struct imx_ccm_clk * clk)56*8644267aSskrll imx_ccm_extclk_get_rate(struct imx_ccm_softc *sc,
57*8644267aSskrll struct imx_ccm_clk *clk)
58*8644267aSskrll {
59*8644267aSskrll struct clk *extclk;
60*8644267aSskrll
61*8644267aSskrll KASSERT(clk->type == IMX_CCM_EXTCLK);
62*8644267aSskrll
63*8644267aSskrll if ((extclk = fdtbus_clock_byname(clk->u.extclk)) == NULL)
64*8644267aSskrll return 0;
65*8644267aSskrll
66*8644267aSskrll return clk_get_rate(extclk);
67*8644267aSskrll }
68*8644267aSskrll
69*8644267aSskrll int
imx_ccm_extclk_set_rate(struct imx_ccm_softc * sc,struct imx_ccm_clk * clk,u_int rate)70*8644267aSskrll imx_ccm_extclk_set_rate(struct imx_ccm_softc *sc,
71*8644267aSskrll struct imx_ccm_clk *clk, u_int rate)
72*8644267aSskrll {
73*8644267aSskrll struct clk *extclk;
74*8644267aSskrll
75*8644267aSskrll KASSERT(clk->type == IMX_CCM_EXTCLK);
76*8644267aSskrll
77*8644267aSskrll if ((extclk = fdtbus_clock_byname(clk->u.extclk)) == NULL)
78*8644267aSskrll return ENXIO;
79*8644267aSskrll
80*8644267aSskrll return clk_set_rate(extclk, rate);
81*8644267aSskrll }
82*8644267aSskrll
83*8644267aSskrll const char *
imx_ccm_extclk_get_parent(struct imx_ccm_softc * sc,struct imx_ccm_clk * clk)84*8644267aSskrll imx_ccm_extclk_get_parent(struct imx_ccm_softc *sc,
85*8644267aSskrll struct imx_ccm_clk *clk)
86*8644267aSskrll {
87*8644267aSskrll struct clk *extclk, *clkp;
88*8644267aSskrll
89*8644267aSskrll KASSERT(clk->type == IMX_CCM_EXTCLK);
90*8644267aSskrll
91*8644267aSskrll if ((extclk = fdtbus_clock_byname(clk->u.extclk)) == NULL)
92*8644267aSskrll return NULL;
93*8644267aSskrll
94*8644267aSskrll clkp = clk_get_parent(extclk);
95*8644267aSskrll if (clkp == NULL)
96*8644267aSskrll return NULL;
97*8644267aSskrll
98*8644267aSskrll return clkp->name;
99*8644267aSskrll }
100