177f22241SEmmanuel Vadot /*-
277f22241SEmmanuel Vadot  * SPDX-License-Identifier: BSD-2-Clause
377f22241SEmmanuel Vadot  *
477f22241SEmmanuel Vadot  * Copyright (c) 2018 Emmanuel Vadot <manu@freebsd.org>
577f22241SEmmanuel Vadot  *
677f22241SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
777f22241SEmmanuel Vadot  * modification, are permitted provided that the following conditions
877f22241SEmmanuel Vadot  * are met:
977f22241SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
1077f22241SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
1177f22241SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
1277f22241SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
1377f22241SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
1477f22241SEmmanuel Vadot  *
1577f22241SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1677f22241SEmmanuel Vadot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1777f22241SEmmanuel Vadot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1877f22241SEmmanuel Vadot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1977f22241SEmmanuel Vadot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2077f22241SEmmanuel Vadot  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2177f22241SEmmanuel Vadot  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2277f22241SEmmanuel Vadot  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2377f22241SEmmanuel Vadot  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2477f22241SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2577f22241SEmmanuel Vadot  * SUCH DAMAGE.
2677f22241SEmmanuel Vadot  */
2777f22241SEmmanuel Vadot 
2877f22241SEmmanuel Vadot #include <sys/param.h>
2977f22241SEmmanuel Vadot #include <sys/systm.h>
3077f22241SEmmanuel Vadot #include <sys/bus.h>
3177f22241SEmmanuel Vadot 
32be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
3362e8ccc3SEmmanuel Vadot #include <dev/syscon/syscon.h>
3477f22241SEmmanuel Vadot 
3577f22241SEmmanuel Vadot #include <dev/clk/rockchip/rk_clk_composite.h>
3677f22241SEmmanuel Vadot 
3777f22241SEmmanuel Vadot #include "clkdev_if.h"
3877f22241SEmmanuel Vadot #include "syscon_if.h"
3977f22241SEmmanuel Vadot 
4077f22241SEmmanuel Vadot struct rk_clk_composite_sc {
4177f22241SEmmanuel Vadot 	uint32_t	muxdiv_offset;
4277f22241SEmmanuel Vadot 	uint32_t	mux_shift;
4377f22241SEmmanuel Vadot 	uint32_t	mux_width;
4477f22241SEmmanuel Vadot 	uint32_t	mux_mask;
4577f22241SEmmanuel Vadot 
4677f22241SEmmanuel Vadot 	uint32_t	div_shift;
4777f22241SEmmanuel Vadot 	uint32_t	div_width;
4877f22241SEmmanuel Vadot 	uint32_t	div_mask;
4977f22241SEmmanuel Vadot 
5077f22241SEmmanuel Vadot 	uint32_t	flags;
5177f22241SEmmanuel Vadot 
5277f22241SEmmanuel Vadot 	struct syscon	*grf;
5377f22241SEmmanuel Vadot };
5477f22241SEmmanuel Vadot 
5577f22241SEmmanuel Vadot #define	WRITE4(_clk, off, val)						\
5677f22241SEmmanuel Vadot 	rk_clk_composite_write_4(_clk, off, val)
5777f22241SEmmanuel Vadot #define	READ4(_clk, off, val)						\
5877f22241SEmmanuel Vadot 	rk_clk_composite_read_4(_clk, off, val)
5977f22241SEmmanuel Vadot #define	DEVICE_LOCK(_clk)						\
6077f22241SEmmanuel Vadot 	CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
6177f22241SEmmanuel Vadot #define	DEVICE_UNLOCK(_clk)						\
6277f22241SEmmanuel Vadot 	CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
6377f22241SEmmanuel Vadot 
6477f22241SEmmanuel Vadot #define	RK_CLK_COMPOSITE_MASK_SHIFT	16
6577f22241SEmmanuel Vadot 
6677f22241SEmmanuel Vadot #if 0
6777f22241SEmmanuel Vadot #define	dprintf(format, arg...)						\
6877f22241SEmmanuel Vadot 	printf("%s:(%s)" format, __func__, clknode_get_name(clk), arg)
6977f22241SEmmanuel Vadot #else
7077f22241SEmmanuel Vadot #define	dprintf(format, arg...)
7177f22241SEmmanuel Vadot #endif
7277f22241SEmmanuel Vadot 
7377f22241SEmmanuel Vadot static void
rk_clk_composite_read_4(struct clknode * clk,bus_addr_t addr,uint32_t * val)7477f22241SEmmanuel Vadot rk_clk_composite_read_4(struct clknode *clk, bus_addr_t addr, uint32_t *val)
7577f22241SEmmanuel Vadot {
7677f22241SEmmanuel Vadot 	struct rk_clk_composite_sc *sc;
7777f22241SEmmanuel Vadot 
7877f22241SEmmanuel Vadot 	sc = clknode_get_softc(clk);
7977f22241SEmmanuel Vadot 	if (sc->grf)
8077f22241SEmmanuel Vadot 		*val = SYSCON_READ_4(sc->grf, addr);
8177f22241SEmmanuel Vadot 	else
8277f22241SEmmanuel Vadot 		CLKDEV_READ_4(clknode_get_device(clk), addr, val);
8377f22241SEmmanuel Vadot }
8477f22241SEmmanuel Vadot 
8577f22241SEmmanuel Vadot static void
rk_clk_composite_write_4(struct clknode * clk,bus_addr_t addr,uint32_t val)8677f22241SEmmanuel Vadot rk_clk_composite_write_4(struct clknode *clk, bus_addr_t addr, uint32_t val)
8777f22241SEmmanuel Vadot {
8877f22241SEmmanuel Vadot 	struct rk_clk_composite_sc *sc;
8977f22241SEmmanuel Vadot 
9077f22241SEmmanuel Vadot 	sc = clknode_get_softc(clk);
9177f22241SEmmanuel Vadot 	if (sc->grf)
9277f22241SEmmanuel Vadot 		SYSCON_WRITE_4(sc->grf, addr, val | (0xffff << 16));
9377f22241SEmmanuel Vadot 	else
9477f22241SEmmanuel Vadot 		CLKDEV_WRITE_4(clknode_get_device(clk), addr, val);
9577f22241SEmmanuel Vadot }
9677f22241SEmmanuel Vadot 
9777f22241SEmmanuel Vadot static struct syscon *
rk_clk_composite_get_grf(struct clknode * clk)9877f22241SEmmanuel Vadot rk_clk_composite_get_grf(struct clknode *clk)
9977f22241SEmmanuel Vadot {
10077f22241SEmmanuel Vadot 	device_t dev;
10177f22241SEmmanuel Vadot 	phandle_t node;
10277f22241SEmmanuel Vadot 	struct syscon *grf;
10377f22241SEmmanuel Vadot 
10477f22241SEmmanuel Vadot 	grf = NULL;
10577f22241SEmmanuel Vadot 	dev = clknode_get_device(clk);
10677f22241SEmmanuel Vadot 	node = ofw_bus_get_node(dev);
10777f22241SEmmanuel Vadot 	if (OF_hasprop(node, "rockchip,grf") &&
10877f22241SEmmanuel Vadot 	    syscon_get_by_ofw_property(dev, node,
10977f22241SEmmanuel Vadot 	    "rockchip,grf", &grf) != 0) {
11077f22241SEmmanuel Vadot 		return (NULL);
11177f22241SEmmanuel Vadot         }
11277f22241SEmmanuel Vadot 
11377f22241SEmmanuel Vadot 	return (grf);
11477f22241SEmmanuel Vadot }
11577f22241SEmmanuel Vadot 
11677f22241SEmmanuel Vadot static int
rk_clk_composite_init(struct clknode * clk,device_t dev)11777f22241SEmmanuel Vadot rk_clk_composite_init(struct clknode *clk, device_t dev)
11877f22241SEmmanuel Vadot {
11977f22241SEmmanuel Vadot 	struct rk_clk_composite_sc *sc;
12077f22241SEmmanuel Vadot 	uint32_t val, idx;
12177f22241SEmmanuel Vadot 
12277f22241SEmmanuel Vadot 	sc = clknode_get_softc(clk);
12377f22241SEmmanuel Vadot 	if ((sc->flags & RK_CLK_COMPOSITE_GRF) != 0) {
12477f22241SEmmanuel Vadot 		sc->grf = rk_clk_composite_get_grf(clk);
12577f22241SEmmanuel Vadot 		if (sc->grf == NULL)
12677f22241SEmmanuel Vadot 			panic("clock %s has GRF flag set but no syscon is available",
12777f22241SEmmanuel Vadot 			    clknode_get_name(clk));
12877f22241SEmmanuel Vadot 	}
12977f22241SEmmanuel Vadot 
13077f22241SEmmanuel Vadot 	idx = 0;
13177f22241SEmmanuel Vadot 	if ((sc->flags & RK_CLK_COMPOSITE_HAVE_MUX) != 0) {
13277f22241SEmmanuel Vadot 		DEVICE_LOCK(clk);
13377f22241SEmmanuel Vadot 		READ4(clk, sc->muxdiv_offset, &val);
13477f22241SEmmanuel Vadot 		DEVICE_UNLOCK(clk);
13577f22241SEmmanuel Vadot 
13677f22241SEmmanuel Vadot 		idx = (val & sc->mux_mask) >> sc->mux_shift;
13777f22241SEmmanuel Vadot 	}
13877f22241SEmmanuel Vadot 
13977f22241SEmmanuel Vadot 	clknode_init_parent_idx(clk, idx);
14077f22241SEmmanuel Vadot 
14177f22241SEmmanuel Vadot 	return (0);
14277f22241SEmmanuel Vadot }
14377f22241SEmmanuel Vadot 
14477f22241SEmmanuel Vadot static int
rk_clk_composite_set_mux(struct clknode * clk,int index)14577f22241SEmmanuel Vadot rk_clk_composite_set_mux(struct clknode *clk, int index)
14677f22241SEmmanuel Vadot {
14777f22241SEmmanuel Vadot 	struct rk_clk_composite_sc *sc;
14877f22241SEmmanuel Vadot 	uint32_t val = 0;
14977f22241SEmmanuel Vadot 
15077f22241SEmmanuel Vadot 	sc = clknode_get_softc(clk);
15177f22241SEmmanuel Vadot 
15277f22241SEmmanuel Vadot 	if ((sc->flags & RK_CLK_COMPOSITE_HAVE_MUX) == 0)
15377f22241SEmmanuel Vadot 		return (0);
15477f22241SEmmanuel Vadot 
15577f22241SEmmanuel Vadot 	dprintf("Set mux to %d\n", index);
15677f22241SEmmanuel Vadot 	DEVICE_LOCK(clk);
15777f22241SEmmanuel Vadot 	val |= (index << sc->mux_shift);
15877f22241SEmmanuel Vadot 	val |= sc->mux_mask << RK_CLK_COMPOSITE_MASK_SHIFT;
15977f22241SEmmanuel Vadot 	dprintf("Write: muxdiv_offset=%x, val=%x\n", sc->muxdiv_offset, val);
16077f22241SEmmanuel Vadot 	WRITE4(clk, sc->muxdiv_offset, val);
16177f22241SEmmanuel Vadot 	DEVICE_UNLOCK(clk);
16277f22241SEmmanuel Vadot 
16377f22241SEmmanuel Vadot 	return (0);
16477f22241SEmmanuel Vadot }
16577f22241SEmmanuel Vadot 
16677f22241SEmmanuel Vadot static int
rk_clk_composite_recalc(struct clknode * clk,uint64_t * freq)16777f22241SEmmanuel Vadot rk_clk_composite_recalc(struct clknode *clk, uint64_t *freq)
16877f22241SEmmanuel Vadot {
16977f22241SEmmanuel Vadot 	struct rk_clk_composite_sc *sc;
17077f22241SEmmanuel Vadot 	uint32_t reg, div;
17177f22241SEmmanuel Vadot 
17277f22241SEmmanuel Vadot 	sc = clknode_get_softc(clk);
17377f22241SEmmanuel Vadot 
17477f22241SEmmanuel Vadot 	DEVICE_LOCK(clk);
17577f22241SEmmanuel Vadot 
17677f22241SEmmanuel Vadot 	READ4(clk, sc->muxdiv_offset, &reg);
17777f22241SEmmanuel Vadot 	dprintf("Read: muxdiv_offset=%x, val=%x\n", sc->muxdiv_offset, reg);
17877f22241SEmmanuel Vadot 
17977f22241SEmmanuel Vadot 	DEVICE_UNLOCK(clk);
18077f22241SEmmanuel Vadot 
18177f22241SEmmanuel Vadot 	div = ((reg & sc->div_mask) >> sc->div_shift);
18277f22241SEmmanuel Vadot 	if (sc->flags & RK_CLK_COMPOSITE_DIV_EXP)
18377f22241SEmmanuel Vadot 		div = 1 << div;
18477f22241SEmmanuel Vadot 	else
18577f22241SEmmanuel Vadot 		div += 1;
18677f22241SEmmanuel Vadot 	dprintf("parent_freq=%ju, div=%u\n", *freq, div);
18777f22241SEmmanuel Vadot 	*freq = *freq / div;
18877f22241SEmmanuel Vadot 	dprintf("Final freq=%ju\n", *freq);
18977f22241SEmmanuel Vadot 	return (0);
19077f22241SEmmanuel Vadot }
19177f22241SEmmanuel Vadot 
19277f22241SEmmanuel Vadot static uint32_t
rk_clk_composite_find_best(struct rk_clk_composite_sc * sc,uint64_t fparent,uint64_t freq,uint32_t * reg)19377f22241SEmmanuel Vadot rk_clk_composite_find_best(struct rk_clk_composite_sc *sc, uint64_t fparent,
19477f22241SEmmanuel Vadot     uint64_t freq, uint32_t *reg)
19577f22241SEmmanuel Vadot {
19677f22241SEmmanuel Vadot 	uint64_t best, cur;
19777f22241SEmmanuel Vadot 	uint32_t best_div, best_div_reg;
19877f22241SEmmanuel Vadot 	uint32_t div, div_reg;
19977f22241SEmmanuel Vadot 
20077f22241SEmmanuel Vadot 	best = 0;
20177f22241SEmmanuel Vadot 	best_div = 0;
20277f22241SEmmanuel Vadot 	best_div_reg = 0;
20377f22241SEmmanuel Vadot 
20477f22241SEmmanuel Vadot 	for (div_reg = 0;  div_reg <= ((sc->div_mask >> sc->div_shift) + 1);
20577f22241SEmmanuel Vadot 	    div_reg++) {
20677f22241SEmmanuel Vadot 		if (sc->flags == RK_CLK_COMPOSITE_DIV_EXP)
20777f22241SEmmanuel Vadot 			div = 1 << div_reg;
20877f22241SEmmanuel Vadot 		else
20977f22241SEmmanuel Vadot 			div = div_reg + 1;
21077f22241SEmmanuel Vadot 		cur = fparent / div;
21177f22241SEmmanuel Vadot 		if ((freq - cur) < (freq - best)) {
21277f22241SEmmanuel Vadot 			best = cur;
21377f22241SEmmanuel Vadot 			best_div = div;
21477f22241SEmmanuel Vadot 			best_div_reg = div_reg;
21577f22241SEmmanuel Vadot 			break;
21677f22241SEmmanuel Vadot 		}
21777f22241SEmmanuel Vadot 	}
21877f22241SEmmanuel Vadot 	*reg = best_div_reg;
21977f22241SEmmanuel Vadot 	return (best_div);
22077f22241SEmmanuel Vadot }
22177f22241SEmmanuel Vadot 
22277f22241SEmmanuel Vadot static int
rk_clk_composite_set_freq(struct clknode * clk,uint64_t fparent,uint64_t * fout,int flags,int * stop)22377f22241SEmmanuel Vadot rk_clk_composite_set_freq(struct clknode *clk, uint64_t fparent, uint64_t *fout,
22477f22241SEmmanuel Vadot     int flags, int *stop)
22577f22241SEmmanuel Vadot {
22677f22241SEmmanuel Vadot 	struct rk_clk_composite_sc *sc;
22777f22241SEmmanuel Vadot 	struct clknode *p_clk;
22877f22241SEmmanuel Vadot 	const char **p_names;
22977f22241SEmmanuel Vadot 	uint64_t best, cur;
23077f22241SEmmanuel Vadot 	uint32_t div, div_reg, best_div, best_div_reg, val;
23177f22241SEmmanuel Vadot 	int p_idx, best_parent;
23277f22241SEmmanuel Vadot 
23377f22241SEmmanuel Vadot 	sc = clknode_get_softc(clk);
23477f22241SEmmanuel Vadot 	dprintf("Finding best parent/div for target freq of %ju\n", *fout);
23577f22241SEmmanuel Vadot 	p_names = clknode_get_parent_names(clk);
23677f22241SEmmanuel Vadot 	for (best_div = 0, best = 0, p_idx = 0;
23777f22241SEmmanuel Vadot 	     p_idx != clknode_get_parents_num(clk); p_idx++) {
23877f22241SEmmanuel Vadot 		p_clk = clknode_find_by_name(p_names[p_idx]);
23977f22241SEmmanuel Vadot 		clknode_get_freq(p_clk, &fparent);
24077f22241SEmmanuel Vadot 		dprintf("Testing with parent %s (%d) at freq %ju\n",
24177f22241SEmmanuel Vadot 		    clknode_get_name(p_clk), p_idx, fparent);
24277f22241SEmmanuel Vadot 		div = rk_clk_composite_find_best(sc, fparent, *fout, &div_reg);
24377f22241SEmmanuel Vadot 		cur = fparent / div;
24477f22241SEmmanuel Vadot 		if ((*fout - cur) < (*fout - best)) {
24577f22241SEmmanuel Vadot 			best = cur;
24677f22241SEmmanuel Vadot 			best_div = div;
24777f22241SEmmanuel Vadot 			best_div_reg = div_reg;
24877f22241SEmmanuel Vadot 			best_parent = p_idx;
24977f22241SEmmanuel Vadot 			dprintf("Best parent so far %s (%d) with best freq at "
25077f22241SEmmanuel Vadot 			    "%ju\n", clknode_get_name(p_clk), p_idx, best);
25177f22241SEmmanuel Vadot 		}
25277f22241SEmmanuel Vadot 	}
25377f22241SEmmanuel Vadot 
25477f22241SEmmanuel Vadot 	*stop = 1;
25577f22241SEmmanuel Vadot 	if (best_div == 0)
25677f22241SEmmanuel Vadot 		return (ERANGE);
25777f22241SEmmanuel Vadot 
25877f22241SEmmanuel Vadot 	if ((best < *fout) && ((flags & CLK_SET_ROUND_DOWN) == 0))
25977f22241SEmmanuel Vadot 		return (ERANGE);
26077f22241SEmmanuel Vadot 
26177f22241SEmmanuel Vadot 	if ((best > *fout) && ((flags & CLK_SET_ROUND_UP) == 0)) {
26277f22241SEmmanuel Vadot 		return (ERANGE);
26377f22241SEmmanuel Vadot 	}
26477f22241SEmmanuel Vadot 
26577f22241SEmmanuel Vadot 	if ((flags & CLK_SET_DRYRUN) != 0) {
26677f22241SEmmanuel Vadot 		*fout = best;
26777f22241SEmmanuel Vadot 		return (0);
26877f22241SEmmanuel Vadot 	}
26977f22241SEmmanuel Vadot 
27077f22241SEmmanuel Vadot 	p_idx = clknode_get_parent_idx(clk);
27177f22241SEmmanuel Vadot 	if (p_idx != best_parent) {
27277f22241SEmmanuel Vadot 		dprintf("Switching parent index from %d to %d\n", p_idx,
27377f22241SEmmanuel Vadot 		    best_parent);
27477f22241SEmmanuel Vadot 		clknode_set_parent_by_idx(clk, best_parent);
27577f22241SEmmanuel Vadot 	}
27677f22241SEmmanuel Vadot 
27777f22241SEmmanuel Vadot 	dprintf("Setting divider to %d (reg: %d)\n", best_div, best_div_reg);
27877f22241SEmmanuel Vadot 	dprintf(" div_mask: 0x%X, div_shift: %d\n", sc->div_mask,
27977f22241SEmmanuel Vadot 	    sc->div_shift);
28077f22241SEmmanuel Vadot 
28177f22241SEmmanuel Vadot 	DEVICE_LOCK(clk);
28277f22241SEmmanuel Vadot 	val = best_div_reg << sc->div_shift;
28377f22241SEmmanuel Vadot 	val |= sc->div_mask << RK_CLK_COMPOSITE_MASK_SHIFT;
28477f22241SEmmanuel Vadot 	dprintf("Write: muxdiv_offset=%x, val=%x\n", sc->muxdiv_offset, val);
28577f22241SEmmanuel Vadot 	WRITE4(clk, sc->muxdiv_offset, val);
28677f22241SEmmanuel Vadot 	DEVICE_UNLOCK(clk);
28777f22241SEmmanuel Vadot 
28877f22241SEmmanuel Vadot 	*fout = best;
28977f22241SEmmanuel Vadot 	return (0);
29077f22241SEmmanuel Vadot }
29177f22241SEmmanuel Vadot 
29277f22241SEmmanuel Vadot static clknode_method_t rk_clk_composite_clknode_methods[] = {
29377f22241SEmmanuel Vadot 	/* Device interface */
29477f22241SEmmanuel Vadot 	CLKNODEMETHOD(clknode_init,		rk_clk_composite_init),
29577f22241SEmmanuel Vadot 	CLKNODEMETHOD(clknode_set_mux,		rk_clk_composite_set_mux),
29677f22241SEmmanuel Vadot 	CLKNODEMETHOD(clknode_recalc_freq,	rk_clk_composite_recalc),
29777f22241SEmmanuel Vadot 	CLKNODEMETHOD(clknode_set_freq,		rk_clk_composite_set_freq),
29877f22241SEmmanuel Vadot 	CLKNODEMETHOD_END
29977f22241SEmmanuel Vadot };
30077f22241SEmmanuel Vadot 
30177f22241SEmmanuel Vadot DEFINE_CLASS_1(rk_clk_composite_clknode, rk_clk_composite_clknode_class,
30277f22241SEmmanuel Vadot     rk_clk_composite_clknode_methods, sizeof(struct rk_clk_composite_sc),
30377f22241SEmmanuel Vadot     clknode_class);
30477f22241SEmmanuel Vadot 
30577f22241SEmmanuel Vadot int
rk_clk_composite_register(struct clkdom * clkdom,struct rk_clk_composite_def * clkdef)30677f22241SEmmanuel Vadot rk_clk_composite_register(struct clkdom *clkdom,
30777f22241SEmmanuel Vadot     struct rk_clk_composite_def *clkdef)
30877f22241SEmmanuel Vadot {
30977f22241SEmmanuel Vadot 	struct clknode *clk;
31077f22241SEmmanuel Vadot 	struct rk_clk_composite_sc *sc;
31177f22241SEmmanuel Vadot 
31277f22241SEmmanuel Vadot 	clk = clknode_create(clkdom, &rk_clk_composite_clknode_class,
31377f22241SEmmanuel Vadot 	    &clkdef->clkdef);
31477f22241SEmmanuel Vadot 	if (clk == NULL)
31577f22241SEmmanuel Vadot 		return (1);
31677f22241SEmmanuel Vadot 
31777f22241SEmmanuel Vadot 	sc = clknode_get_softc(clk);
31877f22241SEmmanuel Vadot 
31977f22241SEmmanuel Vadot 	sc->muxdiv_offset = clkdef->muxdiv_offset;
32077f22241SEmmanuel Vadot 
32177f22241SEmmanuel Vadot 	sc->mux_shift = clkdef->mux_shift;
32277f22241SEmmanuel Vadot 	sc->mux_width = clkdef->mux_width;
32377f22241SEmmanuel Vadot 	sc->mux_mask = ((1 << clkdef->mux_width) - 1) << sc->mux_shift;
32477f22241SEmmanuel Vadot 
32577f22241SEmmanuel Vadot 	sc->div_shift = clkdef->div_shift;
32677f22241SEmmanuel Vadot 	sc->div_width = clkdef->div_width;
32777f22241SEmmanuel Vadot 	sc->div_mask = ((1 << clkdef->div_width) - 1) << sc->div_shift;
32877f22241SEmmanuel Vadot 
32977f22241SEmmanuel Vadot 	sc->flags = clkdef->flags;
33077f22241SEmmanuel Vadot 
33177f22241SEmmanuel Vadot 	clknode_register(clkdom, clk);
33277f22241SEmmanuel Vadot 
33377f22241SEmmanuel Vadot 	return (0);
33477f22241SEmmanuel Vadot }
335