1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2018-2021 Emmanuel Vadot <manu@FreeBSD.org>
5  * Copyright (c) 2021 Bjoern A. Zeeb <bz@FreeBSD.org>
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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, 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 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/systm.h>
35 #include <sys/clock.h>
36 
37 #include <dev/ofw/ofw_bus.h>
38 #include <dev/ofw/ofw_bus_subr.h>
39 
40 #include <dev/extres/clk/clk.h>
41 
42 #include <dev/iicbus/pmic/rockchip/rk8xx.h>
43 
44 /* Clock class and method */
45 struct rk8xx_clk_sc {
46 	device_t		base_dev;
47 };
48 
49 #define	CLK32OUT_REG		0x20
50 #define	CLK32OUT_CLKOUT2_EN	1
51 
52 static int
53 rk8xx_clk_set_gate_1(struct clknode *clk, bool enable)
54 {
55 	struct rk8xx_clk_sc *sc;
56 	uint8_t val;
57 
58 	sc = clknode_get_softc(clk);
59 
60 	rk8xx_read(sc->base_dev, CLK32OUT_REG, &val, sizeof(val));
61 	if (enable)
62 		val |= CLK32OUT_CLKOUT2_EN;
63 	else
64 		val &= ~CLK32OUT_CLKOUT2_EN;
65 	rk8xx_write(sc->base_dev, CLK32OUT_REG, &val, 1);
66 
67 	return (0);
68 }
69 
70 static int
71 rk8xx_clk_recalc(struct clknode *clk, uint64_t *freq)
72 {
73 
74 	*freq = 32768;
75 	return (0);
76 }
77 
78 static clknode_method_t rk8xx_clk_clknode_methods_0[] = {
79 	CLKNODEMETHOD(clknode_recalc_freq,	rk8xx_clk_recalc),
80 	CLKNODEMETHOD_END
81 };
82 
83 DEFINE_CLASS_1(rk8xx_clk_clknode_0, rk8xx_clk_clknode_class_0,
84     rk8xx_clk_clknode_methods_0, sizeof(struct rk8xx_clk_sc),
85     clknode_class);
86 
87 static clknode_method_t rk8xx_clk_clknode_methods_1[] = {
88 	CLKNODEMETHOD(clknode_set_gate,		rk8xx_clk_set_gate_1),
89 	CLKNODEMETHOD_END
90 };
91 
92 DEFINE_CLASS_1(rk8xx_clk_clknode_1, rk8xx_clk_clknode_class_1,
93     rk8xx_clk_clknode_methods_1, sizeof(struct rk8xx_clk_sc),
94     rk8xx_clk_clknode_class_0);
95 
96 int
97 rk8xx_attach_clocks(struct rk8xx_softc *sc)
98 {
99 	struct clkdom *clkdom;
100 	struct clknode_init_def clkidef;
101 	struct clknode *clk;
102 	struct rk8xx_clk_sc *clksc;
103 	const char **clknames;
104 	phandle_t node;
105 	int nclks, rv;
106 
107 	node = ofw_bus_get_node(sc->dev);
108 
109 	/* clock-output-names are optional. Could use them for clkidef.name. */
110 	nclks = ofw_bus_string_list_to_array(node, "clock-output-names",
111 	    &clknames);
112 
113 	clkdom = clkdom_create(sc->dev);
114 
115 	memset(&clkidef, 0, sizeof(clkidef));
116 	clkidef.id = 0;
117 	clkidef.name = (nclks = 2) ? clknames[0] : "clk32kout1";
118 	clk = clknode_create(clkdom, &rk8xx_clk_clknode_class_0, &clkidef);
119 	if (clk == NULL) {
120 		device_printf(sc->dev, "Cannot create '%s'.\n", clkidef.name);
121 		return (ENXIO);
122 	}
123 	clksc = clknode_get_softc(clk);
124 	clksc->base_dev = sc->dev;
125 	clknode_register(clkdom, clk);
126 
127 	memset(&clkidef, 0, sizeof(clkidef));
128 	clkidef.id = 1;
129 	clkidef.name = (nclks = 2) ? clknames[1] : "clk32kout2";
130 	clk = clknode_create(clkdom, &rk8xx_clk_clknode_class_1, &clkidef);
131 	if (clk == NULL) {
132 		device_printf(sc->dev, "Cannot create '%s'.\n", clkidef.name);
133 		return (ENXIO);
134 	}
135 	clksc = clknode_get_softc(clk);
136 	clksc->base_dev = sc->dev;
137 	clknode_register(clkdom, clk);
138 
139 	rv = clkdom_finit(clkdom);
140 	if (rv != 0) {
141 		device_printf(sc->dev, "Cannot finalize clkdom initialization: "
142 		    "%d\n", rv);
143 		return (ENXIO);
144 	}
145 
146 	if (bootverbose)
147 		clkdom_dump(clkdom);
148 
149 	return (0);
150 }
151