#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/malloc.h>
#include <sys/endian.h>
#include <sys/rman.h>
#include <sys/timeet.h>
#include <sys/timetc.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <machine/bus.h>
#include <machine/cpu.h>
#include <machine/intr.h>
#define READ4(_sc, _reg) \
bus_space_read_4(_sc->bst, _sc->bsh, _reg)
#define WRITE4(_sc, _reg, _val) \
bus_space_write_4(_sc->bst, _sc->bsh, _reg, _val)
#define AUDMUX_PTCR(n) (0x8 * (n - 1))
#define PTCR_TFS_DIR (1 << 31)
#define PTCR_TFSEL_S 27
#define PTCR_TFSEL_M 0xf
#define PTCR_TCLKDIR (1 << 26)
#define PTCR_TCSEL_S 22
#define PTCR_TCSEL_M 0xf
#define PTCR_RFS_DIR (1 << 21)
#define PTCR_SYN (1 << 11)
#define AUDMUX_PDCR(n) (0x8 * (n - 1) + 0x4)
#define PDCR_RXDSEL_S 13
#define PDCR_RXDSEL_M 0x3
#define PDCR_RXDSEL_PORT(n) (n - 1)
struct audmux_softc {
struct resource *res[1];
bus_space_tag_t bst;
bus_space_handle_t bsh;
void *ih;
};
static struct resource_spec audmux_spec[] = {
{ SYS_RES_MEMORY, 0, RF_ACTIVE },
{ -1, 0 }
};
static int
audmux_probe(device_t dev)
{
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (!ofw_bus_is_compatible(dev, "fsl,imx6q-audmux"))
return (ENXIO);
device_set_desc(dev, "i.MX6 Digital Audio Multiplexer");
return (BUS_PROBE_DEFAULT);
}
static int
audmux_configure(struct audmux_softc *sc,
int ssi_port, int audmux_port)
{
uint32_t reg;
reg = (PTCR_TFS_DIR | PTCR_TCLKDIR | PTCR_SYN);
WRITE4(sc, AUDMUX_PTCR(audmux_port), reg);
reg = (PDCR_RXDSEL_PORT(ssi_port) << PDCR_RXDSEL_S);
WRITE4(sc, AUDMUX_PDCR(audmux_port), reg);
return (0);
}
static int
audmux_attach(device_t dev)
{
struct audmux_softc *sc;
sc = device_get_softc(dev);
if (bus_alloc_resources(dev, audmux_spec, sc->res)) {
device_printf(dev, "could not allocate resources\n");
return (ENXIO);
}
sc->bst = rman_get_bustag(sc->res[0]);
sc->bsh = rman_get_bushandle(sc->res[0]);
audmux_configure(sc, 1, 5);
return (0);
};
static device_method_t audmux_methods[] = {
DEVMETHOD(device_probe, audmux_probe),
DEVMETHOD(device_attach, audmux_attach),
{ 0, 0 }
};
static driver_t audmux_driver = {
"audmux",
audmux_methods,
sizeof(struct audmux_softc),
};
DRIVER_MODULE(audmux, simplebus, audmux_driver, 0, 0);