fixedfactorclock.c revision 1.4 1 1.4 thorpej /* $NetBSD: fixedfactorclock.c,v 1.4 2021/01/27 03:10:21 thorpej Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.4 thorpej __KERNEL_RCSID(0, "$NetBSD: fixedfactorclock.c,v 1.4 2021/01/27 03:10:21 thorpej Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/systm.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/kmem.h>
36 1.1 jmcneill #include <sys/bus.h>
37 1.1 jmcneill
38 1.1 jmcneill #include <dev/clk/clk_backend.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <dev/fdt/fdtvar.h>
41 1.1 jmcneill
42 1.1 jmcneill static int fixedfactorclock_match(device_t, cfdata_t, void *);
43 1.1 jmcneill static void fixedfactorclock_attach(device_t, device_t, void *);
44 1.1 jmcneill
45 1.3 aymeric static struct clk *fixedfactorclock_decode(device_t, int, const void *, size_t);
46 1.1 jmcneill
47 1.1 jmcneill static const struct fdtbus_clock_controller_func fixedfactorclock_fdt_funcs = {
48 1.1 jmcneill .decode = fixedfactorclock_decode
49 1.1 jmcneill };
50 1.1 jmcneill
51 1.1 jmcneill static struct clk *fixedfactorclock_get(void *, const char *);
52 1.1 jmcneill static void fixedfactorclock_put(void *, struct clk *);
53 1.1 jmcneill static u_int fixedfactorclock_get_rate(void *, struct clk *);
54 1.1 jmcneill
55 1.1 jmcneill static const struct clk_funcs fixedfactorclock_clk_funcs = {
56 1.1 jmcneill .get = fixedfactorclock_get,
57 1.1 jmcneill .put = fixedfactorclock_put,
58 1.1 jmcneill .get_rate = fixedfactorclock_get_rate,
59 1.1 jmcneill };
60 1.1 jmcneill
61 1.1 jmcneill struct fixedfactorclock_clk {
62 1.1 jmcneill struct clk base;
63 1.1 jmcneill
64 1.1 jmcneill u_int div;
65 1.1 jmcneill u_int mult;
66 1.1 jmcneill };
67 1.1 jmcneill
68 1.1 jmcneill struct fixedfactorclock_softc {
69 1.1 jmcneill device_t sc_dev;
70 1.1 jmcneill int sc_phandle;
71 1.1 jmcneill
72 1.1 jmcneill struct clk_domain sc_clkdom;
73 1.1 jmcneill struct fixedfactorclock_clk sc_clk;
74 1.1 jmcneill };
75 1.1 jmcneill
76 1.1 jmcneill CFATTACH_DECL_NEW(ffclock, sizeof(struct fixedfactorclock_softc),
77 1.1 jmcneill fixedfactorclock_match, fixedfactorclock_attach, NULL, NULL);
78 1.1 jmcneill
79 1.4 thorpej static const struct device_compatible_entry compat_data[] = {
80 1.4 thorpej { .compat = "fixed-factor-clock" },
81 1.4 thorpej DEVICE_COMPAT_EOL
82 1.4 thorpej };
83 1.4 thorpej
84 1.1 jmcneill static int
85 1.1 jmcneill fixedfactorclock_match(device_t parent, cfdata_t cf, void *aux)
86 1.1 jmcneill {
87 1.1 jmcneill const struct fdt_attach_args *faa = aux;
88 1.1 jmcneill
89 1.4 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
90 1.1 jmcneill }
91 1.1 jmcneill
92 1.1 jmcneill static void
93 1.1 jmcneill fixedfactorclock_attach(device_t parent, device_t self, void *aux)
94 1.1 jmcneill {
95 1.1 jmcneill struct fixedfactorclock_softc * const sc = device_private(self);
96 1.1 jmcneill const struct fdt_attach_args *faa = aux;
97 1.1 jmcneill const int phandle = faa->faa_phandle;
98 1.1 jmcneill const char *name;
99 1.1 jmcneill
100 1.1 jmcneill sc->sc_dev = self;
101 1.1 jmcneill sc->sc_phandle = phandle;
102 1.2 jmcneill sc->sc_clkdom.name = device_xname(self);
103 1.1 jmcneill sc->sc_clkdom.funcs = &fixedfactorclock_clk_funcs;
104 1.1 jmcneill sc->sc_clkdom.priv = sc;
105 1.1 jmcneill
106 1.1 jmcneill of_getprop_uint32(phandle, "clock-div", &sc->sc_clk.div);
107 1.1 jmcneill of_getprop_uint32(phandle, "clock-mult", &sc->sc_clk.mult);
108 1.1 jmcneill
109 1.1 jmcneill if (sc->sc_clk.div == 0 || sc->sc_clk.mult == 0) {
110 1.1 jmcneill aprint_error(": invalid properties\n");
111 1.1 jmcneill return;
112 1.1 jmcneill }
113 1.1 jmcneill
114 1.1 jmcneill name = fdtbus_get_string(phandle, "clock-output-names");
115 1.1 jmcneill if (name == NULL)
116 1.1 jmcneill name = faa->faa_name;
117 1.1 jmcneill
118 1.1 jmcneill sc->sc_clk.base.domain = &sc->sc_clkdom;
119 1.1 jmcneill sc->sc_clk.base.name = name;
120 1.2 jmcneill clk_attach(&sc->sc_clk.base);
121 1.1 jmcneill
122 1.1 jmcneill aprint_naive("\n");
123 1.1 jmcneill aprint_normal(": x%u /%u fixed-factor clock\n",
124 1.1 jmcneill sc->sc_clk.mult, sc->sc_clk.div);
125 1.1 jmcneill
126 1.1 jmcneill fdtbus_register_clock_controller(self, phandle,
127 1.1 jmcneill &fixedfactorclock_fdt_funcs);
128 1.1 jmcneill }
129 1.1 jmcneill
130 1.1 jmcneill static struct clk *
131 1.3 aymeric fixedfactorclock_decode(device_t dev, int cc_phandle, const void *data,
132 1.3 aymeric size_t len)
133 1.1 jmcneill {
134 1.1 jmcneill struct fixedfactorclock_softc * const sc = device_private(dev);
135 1.1 jmcneill
136 1.1 jmcneill /* #clock-cells for a fixed factor clock is always 0 */
137 1.1 jmcneill if (len != 0)
138 1.1 jmcneill return NULL;
139 1.1 jmcneill
140 1.1 jmcneill return &sc->sc_clk.base;
141 1.1 jmcneill }
142 1.1 jmcneill
143 1.1 jmcneill static struct clk *
144 1.1 jmcneill fixedfactorclock_get(void *priv, const char *name)
145 1.1 jmcneill {
146 1.1 jmcneill struct fixedfactorclock_softc * const sc = priv;
147 1.1 jmcneill
148 1.1 jmcneill if (strcmp(name, sc->sc_clk.base.name) != 0)
149 1.1 jmcneill return NULL;
150 1.1 jmcneill
151 1.1 jmcneill return &sc->sc_clk.base;
152 1.1 jmcneill }
153 1.1 jmcneill
154 1.1 jmcneill static void
155 1.1 jmcneill fixedfactorclock_put(void *priv, struct clk *clk)
156 1.1 jmcneill {
157 1.1 jmcneill }
158 1.1 jmcneill
159 1.1 jmcneill static u_int
160 1.1 jmcneill fixedfactorclock_get_rate(void *priv, struct clk *clk)
161 1.1 jmcneill {
162 1.1 jmcneill struct fixedfactorclock_softc * const sc = priv;
163 1.1 jmcneill struct fixedfactorclock_clk *fclk = (struct fixedfactorclock_clk *)clk;
164 1.1 jmcneill struct clk *clkp_parent;
165 1.1 jmcneill
166 1.1 jmcneill clkp_parent = fdtbus_clock_get_index(sc->sc_phandle, 0);
167 1.1 jmcneill if (clkp_parent == NULL)
168 1.1 jmcneill return 0;
169 1.1 jmcneill
170 1.1 jmcneill return (clk_get_rate(clkp_parent) * fclk->mult) / fclk->div;
171 1.1 jmcneill }
172