1 /* $NetBSD: imx_ccm.c,v 1.2 2026/11/24 11:05:52 hkenken Exp $ */ 2 3 /*- 4 * Copyright (c) 2020 Jared McNeill <jmcneill (at) invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: imx_ccm.c,v 1.2 2026/11/24 11:05:52 hkenken Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/cpu.h> 35 #include <sys/device.h> 36 37 #include <dev/clk/clk_backend.h> 38 39 #include <arm/nxp/imx_ccm.h> 40 41 static struct clk * 42 imx_ccm_clock_get(void *priv, const char *name) 43 { 44 struct imx_ccm_softc * const sc = priv; 45 struct imx_ccm_clk *clk; 46 47 clk = imx_ccm_clock_find(sc, name); 48 if (clk == NULL) 49 return NULL; 50 51 return &clk->base; 52 } 53 54 static void 55 imx_ccm_clock_put(void *priv, struct clk *clk) 56 { 57 } 58 59 static u_int 60 imx_ccm_clock_get_rate(void *priv, struct clk *clkp) 61 { 62 struct imx_ccm_softc * const sc = priv; 63 struct imx_ccm_clk *clk = (struct imx_ccm_clk *)clkp; 64 struct clk *clkp_parent; 65 66 if (clk->get_rate) 67 return clk->get_rate(sc, clk); 68 69 clkp_parent = clk_get_parent(clkp); 70 if (clkp_parent == NULL) { 71 aprint_debug("%s: no parent for %s\n", __func__, clk->base.name); 72 return 0; 73 } 74 75 return clk_get_rate(clkp_parent); 76 } 77 78 static int 79 imx_ccm_clock_set_rate(void *priv, struct clk *clkp, u_int rate) 80 { 81 struct imx_ccm_softc * const sc = priv; 82 struct imx_ccm_clk *clk = (struct imx_ccm_clk *)clkp; 83 struct clk *clkp_parent; 84 85 if (clkp->flags & CLK_SET_RATE_PARENT) { 86 clkp_parent = clk_get_parent(clkp); 87 if (clkp_parent == NULL) { 88 aprint_error("%s: no parent for %s\n", __func__, clk->base.name); 89 return ENXIO; 90 } 91 return clk_set_rate(clkp_parent, rate); 92 } 93 94 if (clk->set_rate) 95 return clk->set_rate(sc, clk, rate); 96 97 return ENXIO; 98 } 99 100 static u_int 101 imx_ccm_clock_round_rate(void *priv, struct clk *clkp, u_int rate) 102 { 103 struct imx_ccm_softc * const sc = priv; 104 struct imx_ccm_clk *clk = (struct imx_ccm_clk *)clkp; 105 struct clk *clkp_parent; 106 107 if (clkp->flags & CLK_SET_RATE_PARENT) { 108 clkp_parent = clk_get_parent(clkp); 109 if (clkp_parent == NULL) { 110 aprint_error("%s: no parent for %s\n", __func__, clk->base.name); 111 return 0; 112 } 113 return clk_round_rate(clkp_parent, rate); 114 } 115 116 if (clk->round_rate) 117 return clk->round_rate(sc, clk, rate); 118 119 return 0; 120 } 121 122 static int 123 imx_ccm_clock_enable(void *priv, struct clk *clkp) 124 { 125 struct imx_ccm_softc * const sc = priv; 126 struct imx_ccm_clk *clk = (struct imx_ccm_clk *)clkp; 127 struct clk *clkp_parent; 128 int error = 0; 129 130 clkp_parent = clk_get_parent(clkp); 131 if (clkp_parent != NULL) { 132 error = clk_enable(clkp_parent); 133 if (error != 0) 134 return error; 135 } 136 137 if (clk->enable) 138 error = clk->enable(sc, clk, 1); 139 140 return error; 141 } 142 143 static int 144 imx_ccm_clock_disable(void *priv, struct clk *clkp) 145 { 146 struct imx_ccm_softc * const sc = priv; 147 struct imx_ccm_clk *clk = (struct imx_ccm_clk *)clkp; 148 int error = EINVAL; 149 150 if (clk->enable) 151 error = clk->enable(sc, clk, 0); 152 153 return error; 154 } 155 156 static int 157 imx_ccm_clock_set_parent(void *priv, struct clk *clkp, 158 struct clk *clkp_parent) 159 { 160 struct imx_ccm_softc * const sc = priv; 161 struct imx_ccm_clk *clk = (struct imx_ccm_clk *)clkp; 162 163 if (clk->set_parent == NULL) 164 return EINVAL; 165 166 return clk->set_parent(sc, clk, clkp_parent->name); 167 } 168 169 static struct clk * 170 imx_ccm_clock_get_parent(void *priv, struct clk *clkp) 171 { 172 struct imx_ccm_softc * const sc = priv; 173 struct imx_ccm_clk *clk = (struct imx_ccm_clk *)clkp; 174 struct imx_ccm_clk *clk_parent; 175 const char *parent; 176 177 if (clk->get_parent == NULL) 178 return NULL; 179 180 parent = clk->get_parent(sc, clk); 181 if (parent == NULL) 182 return NULL; 183 184 clk_parent = imx_ccm_clock_find(sc, parent); 185 if (clk_parent != NULL) 186 return &clk_parent->base; 187 188 if (sc->sc_clock_find_parent != NULL) 189 return sc->sc_clock_find_parent(sc, parent); 190 191 return NULL; 192 } 193 194 const struct clk_funcs imx_ccm_clock_funcs = { 195 .get = imx_ccm_clock_get, 196 .put = imx_ccm_clock_put, 197 .get_rate = imx_ccm_clock_get_rate, 198 .set_rate = imx_ccm_clock_set_rate, 199 .round_rate = imx_ccm_clock_round_rate, 200 .enable = imx_ccm_clock_enable, 201 .disable = imx_ccm_clock_disable, 202 .set_parent = imx_ccm_clock_set_parent, 203 .get_parent = imx_ccm_clock_get_parent, 204 }; 205 206 struct imx_ccm_clk * 207 imx_ccm_clock_find(struct imx_ccm_softc *sc, const char *name) 208 { 209 for (int i = 0; i < sc->sc_nclks; i++) { 210 if (sc->sc_clks[i].base.name == NULL) 211 continue; 212 if (strcmp(sc->sc_clks[i].base.name, name) == 0) 213 return &sc->sc_clks[i]; 214 } 215 216 return NULL; 217 } 218 219 int 220 imx_ccm_clock_attach(struct imx_ccm_softc *sc) 221 { 222 sc->sc_clkdom.name = device_xname(sc->sc_dev); 223 sc->sc_clkdom.funcs = &imx_ccm_clock_funcs; 224 sc->sc_clkdom.priv = sc; 225 for (int i = 0; i < sc->sc_nclks; i++) { 226 sc->sc_clks[i].base.domain = &sc->sc_clkdom; 227 clk_attach(&sc->sc_clks[i].base); 228 } 229 230 return 0; 231 } 232 233 void 234 imx_ccm_print(struct imx_ccm_softc *sc) 235 { 236 struct imx_ccm_clk *clk; 237 struct clk *clkp_parent; 238 const char *type; 239 int i; 240 241 for (i = 0; i < sc->sc_nclks; i++) { 242 clk = &sc->sc_clks[i]; 243 if (clk->type == IMX_CCM_UNKNOWN) 244 continue; 245 246 clkp_parent = clk_get_parent(&clk->base); 247 248 switch (clk->type) { 249 case IMX_CCM_EXTCLK: type = "extclk"; break; 250 case IMX_CCM_GATE: type = "gate"; break; 251 case IMX_CCM_COMPOSITE: type = "comp"; break; 252 case IMX_CCM_PLL: type = "pll"; break; 253 case IMX_CCM_FIXED: type = "fixed"; break; 254 case IMX_CCM_FIXED_FACTOR: type = "fixed-factor"; break; 255 case IMX_CCM_MUX: type = "mux"; break; 256 case IMX_CCM_DIV: type = "div"; break; 257 default: type = "???"; break; 258 } 259 260 aprint_debug_dev(sc->sc_dev, 261 "%3d %-14s %2s %-14s %-7s ", 262 clk->id, 263 clk->base.name, 264 clkp_parent ? "<-" : "", 265 clkp_parent ? clkp_parent->name : "", 266 type); 267 aprint_debug("%10d Hz\n", clk_get_rate(&clk->base)); 268 } 269 } 270