xref: /freebsd/sys/arm/freescale/imx/imx6_audmux.c (revision 64de80195bba295c961a4cdf96dbe0e4979bdf2a)
1 /*-
2  * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * i.MX6 Digital Audio Multiplexer (AUDMUX)
29  * Chapter 16, i.MX 6Dual/6Quad Applications Processor Reference Manual,
30  * Rev. 1, 04/2013
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/malloc.h>
42 #include <sys/endian.h>
43 #include <sys/rman.h>
44 #include <sys/timeet.h>
45 #include <sys/timetc.h>
46 
47 #include <dev/fdt/fdt_common.h>
48 #include <dev/ofw/openfirm.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 
52 #include <machine/bus.h>
53 #include <machine/fdt.h>
54 #include <machine/cpu.h>
55 #include <machine/intr.h>
56 
57 #define	READ4(_sc, _reg)	\
58 	bus_space_read_4(_sc->bst, _sc->bsh, _reg)
59 #define	WRITE4(_sc, _reg, _val)	\
60 	bus_space_write_4(_sc->bst, _sc->bsh, _reg, _val)
61 
62 #define	AUDMUX_PTCR(n)	(0x8 * (n - 1))	/* Port Timing Control Register */
63 #define	 PTCR_TFS_DIR	(1 << 31)	/* Transmit Frame Sync Direction Control */
64 #define	 PTCR_TFSEL_S	27		/* Transmit Frame Sync Select */
65 #define	 PTCR_TFSEL_M	0xf
66 #define	 PTCR_TCLKDIR	(1 << 26)	/* Transmit Clock Direction Control */
67 #define	 PTCR_TCSEL_S	22		/* Transmit Clock Select. */
68 #define	 PTCR_TCSEL_M	0xf
69 #define	 PTCR_RFS_DIR	(1 << 21)	/* Receive Frame Sync Direction Control */
70 #define	 PTCR_SYN	(1 << 11)
71 #define	AUDMUX_PDCR(n)	(0x8 * (n - 1) + 0x4)	/* Port Data Control Reg */
72 #define	 PDCR_RXDSEL_S		13	/* Receive Data Select */
73 #define	 PDCR_RXDSEL_M		0x3
74 #define	 PDCR_RXDSEL_PORT(n)	(n - 1)
75 
76 struct audmux_softc {
77 	struct resource		*res[1];
78 	bus_space_tag_t		bst;
79 	bus_space_handle_t	bsh;
80 	void			*ih;
81 };
82 
83 static struct resource_spec audmux_spec[] = {
84 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
85 	{ -1, 0 }
86 };
87 
88 static int
89 audmux_probe(device_t dev)
90 {
91 
92 	if (!ofw_bus_status_okay(dev))
93 		return (ENXIO);
94 
95 	if (!ofw_bus_is_compatible(dev, "fsl,imx6q-audmux"))
96 		return (ENXIO);
97 
98 	device_set_desc(dev, "i.MX6 Digital Audio Multiplexer");
99 	return (BUS_PROBE_DEFAULT);
100 }
101 
102 static int
103 audmux_configure(struct audmux_softc *sc,
104 	int ssi_port, int audmux_port)
105 {
106 	uint32_t reg;
107 
108 	/* Direction: output */
109 	reg = (PTCR_TFS_DIR | PTCR_TCLKDIR | PTCR_SYN);
110 	WRITE4(sc, AUDMUX_PTCR(audmux_port), reg);
111 
112 	/* Select source */
113 	reg = (PDCR_RXDSEL_PORT(ssi_port) << PDCR_RXDSEL_S);
114 	WRITE4(sc, AUDMUX_PDCR(audmux_port), reg);
115 
116 	return (0);
117 }
118 
119 static int
120 audmux_attach(device_t dev)
121 {
122 	struct audmux_softc *sc;
123 
124 	sc = device_get_softc(dev);
125 
126 	if (bus_alloc_resources(dev, audmux_spec, sc->res)) {
127 		device_printf(dev, "could not allocate resources\n");
128 		return (ENXIO);
129 	}
130 
131 	/* Memory interface */
132 	sc->bst = rman_get_bustag(sc->res[0]);
133 	sc->bsh = rman_get_bushandle(sc->res[0]);
134 
135 	/*
136 	 * Direct SSI1 output to AUDMUX5 pins.
137 	 * TODO: dehardcore this.
138 	 */
139 	audmux_configure(sc, 1, 5);
140 
141 	return (0);
142 };
143 
144 static device_method_t audmux_methods[] = {
145 	/* Device interface */
146 	DEVMETHOD(device_probe,		audmux_probe),
147 	DEVMETHOD(device_attach,	audmux_attach),
148 	{ 0, 0 }
149 };
150 
151 static driver_t audmux_driver = {
152 	"audmux",
153 	audmux_methods,
154 	sizeof(struct audmux_softc),
155 };
156 
157 static devclass_t audmux_devclass;
158 
159 DRIVER_MODULE(audmux, simplebus, audmux_driver, audmux_devclass, 0, 0);
160