1*c26e3874Sjmcneill /* $NetBSD: sunxi_ccu_mux.c,v 1.1 2021/11/07 17:13:12 jmcneill Exp $ */
2*c26e3874Sjmcneill
3*c26e3874Sjmcneill /*-
4*c26e3874Sjmcneill * Copyright (c) 2021 Jared McNeill <jmcneill@invisible.ca>
5*c26e3874Sjmcneill * All rights reserved.
6*c26e3874Sjmcneill *
7*c26e3874Sjmcneill * Redistribution and use in source and binary forms, with or without
8*c26e3874Sjmcneill * modification, are permitted provided that the following conditions
9*c26e3874Sjmcneill * are met:
10*c26e3874Sjmcneill * 1. Redistributions of source code must retain the above copyright
11*c26e3874Sjmcneill * notice, this list of conditions and the following disclaimer.
12*c26e3874Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
13*c26e3874Sjmcneill * notice, this list of conditions and the following disclaimer in the
14*c26e3874Sjmcneill * documentation and/or other materials provided with the distribution.
15*c26e3874Sjmcneill *
16*c26e3874Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*c26e3874Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*c26e3874Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*c26e3874Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*c26e3874Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*c26e3874Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22*c26e3874Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*c26e3874Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*c26e3874Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*c26e3874Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*c26e3874Sjmcneill * SUCH DAMAGE.
27*c26e3874Sjmcneill */
28*c26e3874Sjmcneill
29*c26e3874Sjmcneill #include <sys/cdefs.h>
30*c26e3874Sjmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_ccu_mux.c,v 1.1 2021/11/07 17:13:12 jmcneill Exp $");
31*c26e3874Sjmcneill
32*c26e3874Sjmcneill #include <sys/param.h>
33*c26e3874Sjmcneill #include <sys/bus.h>
34*c26e3874Sjmcneill
35*c26e3874Sjmcneill #include <dev/clk/clk_backend.h>
36*c26e3874Sjmcneill
37*c26e3874Sjmcneill #include <arm/sunxi/sunxi_ccu.h>
38*c26e3874Sjmcneill
39*c26e3874Sjmcneill int
sunxi_ccu_mux_set_parent(struct sunxi_ccu_softc * sc,struct sunxi_ccu_clk * clk,const char * name)40*c26e3874Sjmcneill sunxi_ccu_mux_set_parent(struct sunxi_ccu_softc *sc,
41*c26e3874Sjmcneill struct sunxi_ccu_clk *clk, const char *name)
42*c26e3874Sjmcneill {
43*c26e3874Sjmcneill struct sunxi_ccu_mux *mux = &clk->u.mux;
44*c26e3874Sjmcneill uint32_t val;
45*c26e3874Sjmcneill u_int index;
46*c26e3874Sjmcneill
47*c26e3874Sjmcneill KASSERT(clk->type == SUNXI_CCU_MUX);
48*c26e3874Sjmcneill
49*c26e3874Sjmcneill if (mux->sel == 0)
50*c26e3874Sjmcneill return ENODEV;
51*c26e3874Sjmcneill
52*c26e3874Sjmcneill for (index = 0; index < mux->nparents; index++) {
53*c26e3874Sjmcneill if (mux->parents[index] != NULL &&
54*c26e3874Sjmcneill strcmp(mux->parents[index], name) == 0)
55*c26e3874Sjmcneill break;
56*c26e3874Sjmcneill }
57*c26e3874Sjmcneill if (index == mux->nparents)
58*c26e3874Sjmcneill return EINVAL;
59*c26e3874Sjmcneill
60*c26e3874Sjmcneill val = CCU_READ(sc, mux->reg);
61*c26e3874Sjmcneill val &= ~mux->sel;
62*c26e3874Sjmcneill val |= __SHIFTIN(index, mux->sel);
63*c26e3874Sjmcneill CCU_WRITE(sc, mux->reg, val);
64*c26e3874Sjmcneill
65*c26e3874Sjmcneill return 0;
66*c26e3874Sjmcneill }
67*c26e3874Sjmcneill
68*c26e3874Sjmcneill const char *
sunxi_ccu_mux_get_parent(struct sunxi_ccu_softc * sc,struct sunxi_ccu_clk * clk)69*c26e3874Sjmcneill sunxi_ccu_mux_get_parent(struct sunxi_ccu_softc *sc,
70*c26e3874Sjmcneill struct sunxi_ccu_clk *clk)
71*c26e3874Sjmcneill {
72*c26e3874Sjmcneill struct sunxi_ccu_mux *mux = &clk->u.mux;
73*c26e3874Sjmcneill u_int index;
74*c26e3874Sjmcneill uint32_t val;
75*c26e3874Sjmcneill
76*c26e3874Sjmcneill KASSERT(clk->type == SUNXI_CCU_MUX);
77*c26e3874Sjmcneill
78*c26e3874Sjmcneill val = CCU_READ(sc, mux->reg);
79*c26e3874Sjmcneill index = __SHIFTOUT(val, mux->sel);
80*c26e3874Sjmcneill
81*c26e3874Sjmcneill return mux->parents[index];
82*c26e3874Sjmcneill }
83