1/* $NetBSD: ti_comp_clock.c,v 1.1 2025/12/16 12:20:22 skrll Exp $ */ 2 3/*- 4 * Copyright (c) 2025 Rui-Xiang Guo 5 * Copyright (c) 2019 Jared McNeill <jmcneill@invisible.ca> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h> 31__KERNEL_RCSID(0, "$NetBSD: ti_comp_clock.c,v 1.1 2025/12/16 12:20:22 skrll Exp $"); 32 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/device.h> 36#include <sys/kmem.h> 37#include <sys/bus.h> 38 39#include <dev/clk/clk_backend.h> 40 41#include <dev/fdt/fdtvar.h> 42 43static int ti_comp_clock_match(device_t, cfdata_t, void *); 44static void ti_comp_clock_attach(device_t, device_t, void *); 45 46static struct clk *ti_comp_clock_decode(device_t, int, const void *, size_t); 47 48static const struct fdtbus_clock_controller_func ti_comp_clock_fdt_funcs = { 49 .decode = ti_comp_clock_decode 50}; 51 52static struct clk *ti_comp_clock_get(void *, const char *); 53static void ti_comp_clock_put(void *, struct clk *); 54static u_int ti_comp_clock_get_rate(void *, struct clk *); 55static int ti_comp_clock_enable(void *, struct clk *); 56static int ti_comp_clock_disable(void *, struct clk *); 57static struct clk *ti_comp_clock_get_parent(void *, struct clk *); 58static int ti_comp_clock_set_parent(void *, struct clk *, struct clk *); 59 60static const struct clk_funcs ti_comp_clock_clk_funcs = { 61 .get = ti_comp_clock_get, 62 .put = ti_comp_clock_put, 63 .get_rate = ti_comp_clock_get_rate, 64 .enable = ti_comp_clock_enable, 65 .disable = ti_comp_clock_disable, 66 .get_parent = ti_comp_clock_get_parent, 67 .set_parent = ti_comp_clock_set_parent, 68}; 69 70struct ti_comp_clock_softc { 71 device_t sc_dev; 72 int sc_phandle; 73 74 struct clk_domain sc_clkdom; 75 struct clk sc_clk; 76}; 77 78#define RD4(sc, reg) \ 79 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 80#define WR4(sc, reg, val) \ 81 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 82 83CFATTACH_DECL_NEW(ti_comp_clock, sizeof(struct ti_comp_clock_softc), 84 ti_comp_clock_match, ti_comp_clock_attach, NULL, NULL); 85 86static const struct device_compatible_entry compat_data[] = { 87 { .compat = "ti,composite-clock" }, 88 DEVICE_COMPAT_EOL 89}; 90 91static int 92ti_comp_clock_match(device_t parent, cfdata_t cf, void *aux) 93{ 94 const struct fdt_attach_args *faa = aux; 95 96 return of_compatible_match(faa->faa_phandle, compat_data); 97} 98 99static void 100ti_comp_clock_attach(device_t parent, device_t self, void *aux) 101{ 102 struct ti_comp_clock_softc * const sc = device_private(self); 103 const struct fdt_attach_args *faa = aux; 104 const int phandle = faa->faa_phandle; 105 106 sc->sc_dev = self; 107 sc->sc_phandle = phandle; 108 109 sc->sc_clkdom.name = device_xname(self); 110 sc->sc_clkdom.funcs = &ti_comp_clock_clk_funcs; 111 sc->sc_clkdom.priv = sc; 112 113 sc->sc_clk.domain = &sc->sc_clkdom; 114 sc->sc_clk.name = kmem_asprintf("%s", faa->faa_name); 115 clk_attach(&sc->sc_clk); 116 117 aprint_naive("\n"); 118 aprint_normal(": TI composite clock (%s)\n", sc->sc_clk.name); 119 120 fdtbus_register_clock_controller(self, phandle, &ti_comp_clock_fdt_funcs); 121} 122 123static struct clk * 124ti_comp_clock_decode(device_t dev, int cc_phandle, const void *data, 125 size_t len) 126{ 127 struct ti_comp_clock_softc * const sc = device_private(dev); 128 129 return &sc->sc_clk; 130} 131 132static struct clk * 133ti_comp_clock_get(void *priv, const char *name) 134{ 135 struct ti_comp_clock_softc * const sc = priv; 136 137 return &sc->sc_clk; 138} 139 140static void 141ti_comp_clock_put(void *priv, struct clk *clk) 142{ 143} 144 145static u_int 146ti_comp_clock_get_rate(void *priv, struct clk *clk) 147{ 148 struct clk *clk_parent = clk_get_parent(clk); 149 150 if (clk_parent == NULL) 151 return 0; 152 153 return clk_get_rate(clk_parent); 154} 155 156static int 157ti_comp_clock_enable(void *priv, struct clk *clk) 158{ 159 struct ti_comp_clock_softc * const sc = priv; 160 struct clk *clk_gate = fdtbus_clock_get_index(sc->sc_phandle, 0); 161 162 return clk_enable(clk_gate); 163} 164 165static int 166ti_comp_clock_disable(void *priv, struct clk *clk) 167{ 168 struct ti_comp_clock_softc * const sc = priv; 169 struct clk *clk_gate = fdtbus_clock_get_index(sc->sc_phandle, 0); 170 171 return clk_disable(clk_gate); 172} 173 174static struct clk * 175ti_comp_clock_get_parent(void *priv, struct clk *clk) 176{ 177 struct ti_comp_clock_softc * const sc = priv; 178 179 return fdtbus_clock_get_index(sc->sc_phandle, 1); 180} 181 182static int 183ti_comp_clock_set_parent(void *priv, struct clk *clk, struct clk *parent_clk) 184{ 185 return clk_set_parent(clk, parent_clk); 186} 187