2 * Copyright (c) 2015 Oleksandr Tymoshenko <gonzo@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/eventhandler.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
45 #include <machine/bus.h>
47 #include <dev/videomode/videomode.h>
48 #include <dev/videomode/edidvar.h>
50 #include <arm/freescale/imx/imx_ccmvar.h>
51 #include <arm/freescale/imx/imx_iomuxvar.h>
52 #include <arm/freescale/imx/imx_iomuxreg.h>
54 #include <dev/hdmi/dwc_hdmi.h>
58 struct imx_hdmi_softc {
59 struct dwc_hdmi_softc base;
61 eventhandler_tag eh_tag;
64 static struct ofw_compat_data compat_data[] = {
65 {"fsl,imx6dl-hdmi", 1},
66 {"fsl,imx6q-hdmi", 1},
71 imx_hdmi_get_i2c_dev(device_t dev)
73 struct imx_hdmi_softc *sc;
75 sc = device_get_softc(dev);
77 if (sc->i2c_xref == 0)
80 return (OF_device_from_xref(sc->i2c_xref));
84 * Deferred HDMI init. dwc_hdmi_init() does i2c transfers for DDC/EDID. The imx
85 * i2c devices also use a config_intrhook function to finish their init, because
86 * they require interrupts to perform transfers. There is no way to control
87 * whether the i2c or our hdmi intrhook function runs first. If we go first we
88 * have to continue waiting until after the i2c driver is ready to do transfers
89 * and has registered its phandle.
91 * This function is used as both a config_intrhook function and after that as an
92 * eventhandler callback function (if necessary), to see if our i2c device is
93 * ready yet. When it is, continue with hdmi init. When first called as an
94 * intrhook function the i2c devices might be ready, in which case we never
95 * register as an eventhandler at all. Otherwise we register to see newbus
96 * attach events, and as each device attaches we check to see whether it was the
97 * i2c device we care about. Once we have our i2c device we unregister from
98 * seeing further attach events.
101 imx_hdmi_init(void *dev)
103 struct imx_hdmi_softc *sc;
105 sc = device_get_softc((device_t)dev);
107 if (OF_device_from_xref(sc->i2c_xref) != NULL) {
108 if (sc->eh_tag != NULL) {
109 EVENTHANDLER_DEREGISTER_NOWAIT(device_attach,
117 device_printf((device_t)dev, "Waiting for DDC i2c device\n");
119 if (sc->eh_tag == NULL) {
120 sc->eh_tag = EVENTHANDLER_REGISTER(device_attach,
121 imx_hdmi_init, dev, EVENTHANDLER_PRI_ANY);
126 imx_hdmi_detach(device_t dev)
128 struct imx_hdmi_softc *sc;
130 sc = device_get_softc(dev);
132 if (sc->base.sc_mem_res != NULL)
133 bus_release_resource(dev, SYS_RES_MEMORY,
134 sc->base.sc_mem_rid, sc->base.sc_mem_res);
140 imx_hdmi_attach(device_t dev)
142 struct imx_hdmi_softc *sc;
145 phandle_t node, i2c_xref;
147 sc = device_get_softc(dev);
148 sc->base.sc_dev = dev;
149 sc->base.sc_get_i2c_dev = imx_hdmi_get_i2c_dev;
152 /* Allocate memory resources. */
153 sc->base.sc_mem_rid = 0;
154 sc->base.sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
155 &sc->base.sc_mem_rid, RF_ACTIVE);
156 if (sc->base.sc_mem_res == NULL) {
157 device_printf(dev, "Cannot allocate memory resources\n");
162 node = ofw_bus_get_node(dev);
163 if (OF_getencprop(node, "ddc-i2c-bus", &i2c_xref, sizeof(i2c_xref)) == -1)
166 sc->i2c_xref = i2c_xref;
168 imx_ccm_hdmi_enable();
170 gpr3 = imx_iomux_gpr_get(IOMUXC_GPR3);
171 gpr3 &= ~(IOMUXC_GPR3_HDMI_MASK);
172 gpr3 |= IOMUXC_GPR3_HDMI_IPU1_DI0;
173 imx_iomux_gpr_set(IOMUXC_GPR3, gpr3);
175 /* Further HDMI init requires interrupts for i2c transfers. */
176 config_intrhook_oneshot(imx_hdmi_init, dev);
180 imx_hdmi_detach(dev);
186 imx_hdmi_probe(device_t dev)
189 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
192 device_set_desc(dev, "Freescale i.MX6 HDMI core");
194 return (BUS_PROBE_DEFAULT);
197 static device_method_t imx_hdmi_methods[] = {
198 /* Device interface */
199 DEVMETHOD(device_probe, imx_hdmi_probe),
200 DEVMETHOD(device_attach, imx_hdmi_attach),
201 DEVMETHOD(device_detach, imx_hdmi_detach),
204 DEVMETHOD(hdmi_get_edid, dwc_hdmi_get_edid),
205 DEVMETHOD(hdmi_set_videomode, dwc_hdmi_set_videomode),
210 static driver_t imx_hdmi_driver = {
213 sizeof(struct imx_hdmi_softc)
216 static devclass_t imx_hdmi_devclass;
218 DRIVER_MODULE(hdmi, simplebus, imx_hdmi_driver, imx_hdmi_devclass, 0, 0);