/*-1* Copyright (c) 2015 Oleksandr Tymoshenko <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/cdefs.h>27/*28* HDMI core module29*/3031#include <sys/param.h>32#include <sys/systm.h>33#include <sys/eventhandler.h>34#include <sys/kernel.h>35#include <sys/module.h>36#include <sys/bus.h>37#include <sys/rman.h>3839#include <dev/ofw/ofw_bus.h>40#include <dev/ofw/ofw_bus_subr.h>4142#include <machine/bus.h>4344#include <dev/videomode/videomode.h>45#include <dev/videomode/edidvar.h>4647#include <arm/freescale/imx/imx_ccmvar.h>48#include <arm/freescale/imx/imx_iomuxvar.h>49#include <arm/freescale/imx/imx_iomuxreg.h>5051#include <dev/hdmi/dwc_hdmi.h>5253#include "crtc_if.h"5455struct imx_hdmi_softc {56struct dwc_hdmi_softc base;57phandle_t i2c_xref;58eventhandler_tag eh_tag;59};6061static struct ofw_compat_data compat_data[] = {62{"fsl,imx6dl-hdmi", 1},63{"fsl,imx6q-hdmi", 1},64{NULL, 0}65};6667static device_t68imx_hdmi_get_i2c_dev(device_t dev)69{70struct imx_hdmi_softc *sc;7172sc = device_get_softc(dev);7374if (sc->i2c_xref == 0)75return (NULL);7677return (OF_device_from_xref(sc->i2c_xref));78}7980/*81* Deferred HDMI init. dwc_hdmi_init() does i2c transfers for DDC/EDID. The imx82* i2c devices also use a config_intrhook function to finish their init, because83* they require interrupts to perform transfers. There is no way to control84* whether the i2c or our hdmi intrhook function runs first. If we go first we85* have to continue waiting until after the i2c driver is ready to do transfers86* and has registered its phandle.87*88* This function is used as both a config_intrhook function and after that as an89* eventhandler callback function (if necessary), to see if our i2c device is90* ready yet. When it is, continue with hdmi init. When first called as an91* intrhook function the i2c devices might be ready, in which case we never92* register as an eventhandler at all. Otherwise we register to see newbus93* attach events, and as each device attaches we check to see whether it was the94* i2c device we care about. Once we have our i2c device we unregister from95* seeing further attach events.96*/97static void98imx_hdmi_init(void *dev)99{100struct imx_hdmi_softc *sc;101102sc = device_get_softc((device_t)dev);103104if (OF_device_from_xref(sc->i2c_xref) != NULL) {105if (sc->eh_tag != NULL) {106EVENTHANDLER_DEREGISTER_NOWAIT(device_attach,107sc->eh_tag);108}109dwc_hdmi_init(dev);110return;111}112113if (bootverbose)114device_printf((device_t)dev, "Waiting for DDC i2c device\n");115116if (sc->eh_tag == NULL) {117sc->eh_tag = EVENTHANDLER_REGISTER(device_attach,118imx_hdmi_init, dev, EVENTHANDLER_PRI_ANY);119}120}121122static int123imx_hdmi_detach(device_t dev)124{125struct imx_hdmi_softc *sc;126127sc = device_get_softc(dev);128129if (sc->base.sc_mem_res != NULL)130bus_release_resource(dev, SYS_RES_MEMORY,131sc->base.sc_mem_rid, sc->base.sc_mem_res);132133return (0);134}135136static int137imx_hdmi_attach(device_t dev)138{139struct imx_hdmi_softc *sc;140int err;141uint32_t gpr3;142phandle_t node, i2c_xref;143144sc = device_get_softc(dev);145sc->base.sc_dev = dev;146sc->base.sc_get_i2c_dev = imx_hdmi_get_i2c_dev;147err = 0;148149/* Allocate memory resources. */150sc->base.sc_mem_rid = 0;151sc->base.sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,152&sc->base.sc_mem_rid, RF_ACTIVE);153if (sc->base.sc_mem_res == NULL) {154device_printf(dev, "Cannot allocate memory resources\n");155err = ENXIO;156goto out;157}158159node = ofw_bus_get_node(dev);160if (OF_getencprop(node, "ddc-i2c-bus", &i2c_xref, sizeof(i2c_xref)) == -1)161sc->i2c_xref = 0;162else163sc->i2c_xref = i2c_xref;164165imx_ccm_hdmi_enable();166167gpr3 = imx_iomux_gpr_get(IOMUXC_GPR3);168gpr3 &= ~(IOMUXC_GPR3_HDMI_MASK);169gpr3 |= IOMUXC_GPR3_HDMI_IPU1_DI0;170imx_iomux_gpr_set(IOMUXC_GPR3, gpr3);171172/* Further HDMI init requires interrupts for i2c transfers. */173config_intrhook_oneshot(imx_hdmi_init, dev);174return (0);175176out:177imx_hdmi_detach(dev);178179return (err);180}181182static int183imx_hdmi_probe(device_t dev)184{185186if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)187return (ENXIO);188189device_set_desc(dev, "Freescale i.MX6 HDMI core");190191return (BUS_PROBE_DEFAULT);192}193194static device_method_t imx_hdmi_methods[] = {195/* Device interface */196DEVMETHOD(device_probe, imx_hdmi_probe),197DEVMETHOD(device_attach, imx_hdmi_attach),198DEVMETHOD(device_detach, imx_hdmi_detach),199200/* CRTC methods */201DEVMETHOD(crtc_get_edid, dwc_hdmi_get_edid),202DEVMETHOD(crtc_set_videomode, dwc_hdmi_set_videomode),203204DEVMETHOD_END205};206207static driver_t imx_hdmi_driver = {208"hdmi",209imx_hdmi_methods,210sizeof(struct imx_hdmi_softc)211};212213DRIVER_MODULE(hdmi, simplebus, imx_hdmi_driver, 0, 0);214215216