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