xref: /freebsd/sys/dev/qcom_clk/qcom_clk_fdiv.c (revision be82b3a0bf72ed3b5f01ac9fcd8dcd3802e3c742)
1e34a491bSAdrian Chadd /*-
2e34a491bSAdrian Chadd  * Copyright (c) 2021 Adrian Chadd <adrian@FreeBSD.org>.
3e34a491bSAdrian Chadd  *
4e34a491bSAdrian Chadd  * Redistribution and use in source and binary forms, with or without
5e34a491bSAdrian Chadd  * modification, are permitted provided that the following conditions
6e34a491bSAdrian Chadd  * are met:
7e34a491bSAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
8e34a491bSAdrian Chadd  *    notice, this list of conditions and the following disclaimer.
9e34a491bSAdrian Chadd  * 2. Redistributions in binary form must reproduce the above copyright
10e34a491bSAdrian Chadd  *    notice, this list of conditions and the following disclaimer in the
11e34a491bSAdrian Chadd  *    documentation and/or other materials provided with the distribution.
12e34a491bSAdrian Chadd  *
13e34a491bSAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14e34a491bSAdrian Chadd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15e34a491bSAdrian Chadd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16e34a491bSAdrian Chadd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17e34a491bSAdrian Chadd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18e34a491bSAdrian Chadd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19e34a491bSAdrian Chadd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20e34a491bSAdrian Chadd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21e34a491bSAdrian Chadd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22e34a491bSAdrian Chadd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23e34a491bSAdrian Chadd  * SUCH DAMAGE.
24e34a491bSAdrian Chadd  */
25e34a491bSAdrian Chadd 
26e34a491bSAdrian Chadd #include <sys/param.h>
27e34a491bSAdrian Chadd #include <sys/systm.h>
28e34a491bSAdrian Chadd #include <sys/bus.h>
29e34a491bSAdrian Chadd #include <sys/lock.h>
30e34a491bSAdrian Chadd #include <sys/mutex.h>
31e34a491bSAdrian Chadd #include <sys/rman.h>
32e34a491bSAdrian Chadd #include <machine/bus.h>
33e34a491bSAdrian Chadd 
34*be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
35*be82b3a0SEmmanuel Vadot #include <dev/clk/clk_div.h>
36*be82b3a0SEmmanuel Vadot #include <dev/clk/clk_fixed.h>
37*be82b3a0SEmmanuel Vadot #include <dev/clk/clk_mux.h>
38e34a491bSAdrian Chadd 
39e34a491bSAdrian Chadd #include "qcom_clk_fdiv.h"
40e34a491bSAdrian Chadd 
41e34a491bSAdrian Chadd #include "clkdev_if.h"
42e34a491bSAdrian Chadd 
43e34a491bSAdrian Chadd /*
44e34a491bSAdrian Chadd  * This is a fixed divisor node.  It represents some divisor
45e34a491bSAdrian Chadd  * that is setup by the boot environment and we don't have
46e34a491bSAdrian Chadd  * any need for the driver to go and fiddle with.
47e34a491bSAdrian Chadd  *
48e34a491bSAdrian Chadd  * It likely should just live in the extres/clk code.
49e34a491bSAdrian Chadd  */
50e34a491bSAdrian Chadd 
51e34a491bSAdrian Chadd struct qcom_clk_fdiv_sc {
52e34a491bSAdrian Chadd 	struct clknode	*clknode;
53e34a491bSAdrian Chadd 	uint32_t divisor;
54e34a491bSAdrian Chadd };
55e34a491bSAdrian Chadd 
56e34a491bSAdrian Chadd static int
qcom_clk_fdiv_recalc(struct clknode * clk,uint64_t * freq)57e34a491bSAdrian Chadd qcom_clk_fdiv_recalc(struct clknode *clk, uint64_t *freq)
58e34a491bSAdrian Chadd {
59e34a491bSAdrian Chadd 	struct qcom_clk_fdiv_sc *sc;
60e34a491bSAdrian Chadd 
61e34a491bSAdrian Chadd 	sc = clknode_get_softc(clk);
62e34a491bSAdrian Chadd 
63e34a491bSAdrian Chadd 	if (freq == NULL || *freq == 0) {
64e34a491bSAdrian Chadd 		printf("%s: called; NULL or 0 frequency\n", __func__);
65e34a491bSAdrian Chadd 		return (ENXIO);
66e34a491bSAdrian Chadd 	}
67e34a491bSAdrian Chadd 
68e34a491bSAdrian Chadd 	*freq = *freq / sc->divisor;
69e34a491bSAdrian Chadd 	return (0);
70e34a491bSAdrian Chadd }
71e34a491bSAdrian Chadd 
72e34a491bSAdrian Chadd static int
qcom_clk_fdiv_init(struct clknode * clk,device_t dev)73e34a491bSAdrian Chadd qcom_clk_fdiv_init(struct clknode *clk, device_t dev)
74e34a491bSAdrian Chadd {
75e34a491bSAdrian Chadd 	/*
76e34a491bSAdrian Chadd 	 * There's only a single parent here for an fixed divisor,
77e34a491bSAdrian Chadd 	 * so just set it to 0; the caller doesn't need to supply it.
78e34a491bSAdrian Chadd 	 */
79e34a491bSAdrian Chadd 	clknode_init_parent_idx(clk, 0);
80e34a491bSAdrian Chadd 
81e34a491bSAdrian Chadd 	return(0);
82e34a491bSAdrian Chadd }
83e34a491bSAdrian Chadd 
84e34a491bSAdrian Chadd static clknode_method_t qcom_clk_fdiv_methods[] = {
85e34a491bSAdrian Chadd 	/* Device interface */
86e34a491bSAdrian Chadd 	CLKNODEMETHOD(clknode_init,		qcom_clk_fdiv_init),
87e34a491bSAdrian Chadd 	CLKNODEMETHOD(clknode_recalc_freq,	qcom_clk_fdiv_recalc),
88e34a491bSAdrian Chadd 	CLKNODEMETHOD_END
89e34a491bSAdrian Chadd };
90e34a491bSAdrian Chadd 
91e34a491bSAdrian Chadd DEFINE_CLASS_1(qcom_clk_fepll, qcom_clk_fdiv_class, qcom_clk_fdiv_methods,
92e34a491bSAdrian Chadd    sizeof(struct qcom_clk_fdiv_sc), clknode_class);
93e34a491bSAdrian Chadd 
94e34a491bSAdrian Chadd int
qcom_clk_fdiv_register(struct clkdom * clkdom,struct qcom_clk_fdiv_def * clkdef)95e34a491bSAdrian Chadd qcom_clk_fdiv_register(struct clkdom *clkdom, struct qcom_clk_fdiv_def *clkdef)
96e34a491bSAdrian Chadd {
97e34a491bSAdrian Chadd 	struct clknode *clk;
98e34a491bSAdrian Chadd 	struct qcom_clk_fdiv_sc *sc;
99e34a491bSAdrian Chadd 
100e34a491bSAdrian Chadd 	clk = clknode_create(clkdom, &qcom_clk_fdiv_class, &clkdef->clkdef);
101e34a491bSAdrian Chadd 	if (clk == NULL)
102e34a491bSAdrian Chadd 		return (1);
103e34a491bSAdrian Chadd 
104e34a491bSAdrian Chadd 	sc = clknode_get_softc(clk);
105e34a491bSAdrian Chadd 	sc->clknode = clk;
106e34a491bSAdrian Chadd 
107e34a491bSAdrian Chadd 	sc->divisor = clkdef->divisor;
108e34a491bSAdrian Chadd 
109e34a491bSAdrian Chadd 	clknode_register(clkdom, clk);
110e34a491bSAdrian Chadd 
111e34a491bSAdrian Chadd 	return (0);
112e34a491bSAdrian Chadd }
113