xref: /freebsd/sys/arm/freescale/imx/imx6_hdmi.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1 /*-
2  * Copyright (c) 2015 Oleksandr Tymoshenko <gonzo@freebsd.org>
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 #include <sys/cdefs.h>
28 /*
29  * HDMI core module
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/rman.h>
39 
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/ofw_bus_subr.h>
42 
43 #include <machine/bus.h>
44 
45 #include <dev/videomode/videomode.h>
46 #include <dev/videomode/edidvar.h>
47 
48 #include <arm/freescale/imx/imx_ccmvar.h>
49 #include <arm/freescale/imx/imx_iomuxvar.h>
50 #include <arm/freescale/imx/imx_iomuxreg.h>
51 
52 #include <dev/hdmi/dwc_hdmi.h>
53 
54 #include "crtc_if.h"
55 
56 struct imx_hdmi_softc {
57 	struct dwc_hdmi_softc	base;
58 	phandle_t		i2c_xref;
59 	eventhandler_tag	eh_tag;
60 };
61 
62 static struct ofw_compat_data compat_data[] = {
63 	{"fsl,imx6dl-hdmi", 1},
64 	{"fsl,imx6q-hdmi",  1},
65 	{NULL,	            0}
66 };
67 
68 static device_t
imx_hdmi_get_i2c_dev(device_t dev)69 imx_hdmi_get_i2c_dev(device_t dev)
70 {
71 	struct imx_hdmi_softc *sc;
72 
73 	sc = device_get_softc(dev);
74 
75 	if (sc->i2c_xref == 0)
76 		return (NULL);
77 
78 	return (OF_device_from_xref(sc->i2c_xref));
79 }
80 
81 /*
82  * Deferred HDMI init.  dwc_hdmi_init() does i2c transfers for DDC/EDID. The imx
83  * i2c devices also use a config_intrhook function to finish their init, because
84  * they require interrupts to perform transfers.  There is no way to control
85  * whether the i2c or our hdmi intrhook function runs first.  If we go first we
86  * have to continue waiting until after the i2c driver is ready to do transfers
87  * and has registered its phandle.
88  *
89  * This function is used as both a config_intrhook function and after that as an
90  * eventhandler callback function (if necessary), to see if our i2c device is
91  * ready yet.  When it is, continue with hdmi init.  When first called as an
92  * intrhook function the i2c devices might be ready, in which case we never
93  * register as an eventhandler at all.  Otherwise we register to see newbus
94  * attach events, and as each device attaches we check to see whether it was the
95  * i2c device we care about.  Once we have our i2c device we unregister from
96  * seeing further attach events.
97  */
98 static void
imx_hdmi_init(void * dev)99 imx_hdmi_init(void *dev)
100 {
101 	struct imx_hdmi_softc *sc;
102 
103 	sc = device_get_softc((device_t)dev);
104 
105 	if (OF_device_from_xref(sc->i2c_xref) != NULL) {
106 		if (sc->eh_tag != NULL) {
107 			EVENTHANDLER_DEREGISTER_NOWAIT(device_attach,
108 			    sc->eh_tag);
109 		}
110 		dwc_hdmi_init(dev);
111 		return;
112 	}
113 
114 	if (bootverbose)
115 		device_printf((device_t)dev, "Waiting for DDC i2c device\n");
116 
117 	if (sc->eh_tag == NULL) {
118 		sc->eh_tag = EVENTHANDLER_REGISTER(device_attach,
119 		    imx_hdmi_init, dev, EVENTHANDLER_PRI_ANY);
120 	}
121 }
122 
123 static int
imx_hdmi_detach(device_t dev)124 imx_hdmi_detach(device_t dev)
125 {
126 	struct imx_hdmi_softc *sc;
127 
128 	sc = device_get_softc(dev);
129 
130 	if (sc->base.sc_mem_res != NULL)
131 		bus_release_resource(dev, SYS_RES_MEMORY,
132 		    sc->base.sc_mem_rid, sc->base.sc_mem_res);
133 
134 	return (0);
135 }
136 
137 static int
imx_hdmi_attach(device_t dev)138 imx_hdmi_attach(device_t dev)
139 {
140 	struct imx_hdmi_softc *sc;
141 	int err;
142 	uint32_t gpr3;
143 	phandle_t node, i2c_xref;
144 
145 	sc = device_get_softc(dev);
146 	sc->base.sc_dev = dev;
147 	sc->base.sc_get_i2c_dev = imx_hdmi_get_i2c_dev;
148 	err = 0;
149 
150 	/* Allocate memory resources. */
151 	sc->base.sc_mem_rid = 0;
152 	sc->base.sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
153 	    &sc->base.sc_mem_rid, RF_ACTIVE);
154 	if (sc->base.sc_mem_res == NULL) {
155 		device_printf(dev, "Cannot allocate memory resources\n");
156 		err = ENXIO;
157 		goto out;
158 	}
159 
160 	node = ofw_bus_get_node(dev);
161 	if (OF_getencprop(node, "ddc-i2c-bus", &i2c_xref, sizeof(i2c_xref)) == -1)
162 		sc->i2c_xref = 0;
163 	else
164 		sc->i2c_xref = i2c_xref;
165 
166 	imx_ccm_hdmi_enable();
167 
168 	gpr3 = imx_iomux_gpr_get(IOMUXC_GPR3);
169 	gpr3 &= ~(IOMUXC_GPR3_HDMI_MASK);
170 	gpr3 |= IOMUXC_GPR3_HDMI_IPU1_DI0;
171 	imx_iomux_gpr_set(IOMUXC_GPR3, gpr3);
172 
173 	/* Further HDMI init requires interrupts for i2c transfers. */
174 	config_intrhook_oneshot(imx_hdmi_init, dev);
175 	return (0);
176 
177 out:
178 	imx_hdmi_detach(dev);
179 
180 	return (err);
181 }
182 
183 static int
imx_hdmi_probe(device_t dev)184 imx_hdmi_probe(device_t dev)
185 {
186 
187 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
188 		return (ENXIO);
189 
190 	device_set_desc(dev, "Freescale i.MX6 HDMI core");
191 
192 	return (BUS_PROBE_DEFAULT);
193 }
194 
195 static device_method_t imx_hdmi_methods[] = {
196 	/* Device interface */
197 	DEVMETHOD(device_probe,  imx_hdmi_probe),
198 	DEVMETHOD(device_attach, imx_hdmi_attach),
199 	DEVMETHOD(device_detach, imx_hdmi_detach),
200 
201 	/* CRTC methods */
202 	DEVMETHOD(crtc_get_edid,	dwc_hdmi_get_edid),
203 	DEVMETHOD(crtc_set_videomode,	dwc_hdmi_set_videomode),
204 
205 	DEVMETHOD_END
206 };
207 
208 static driver_t imx_hdmi_driver = {
209 	"hdmi",
210 	imx_hdmi_methods,
211 	sizeof(struct imx_hdmi_softc)
212 };
213 
214 DRIVER_MODULE(hdmi, simplebus, imx_hdmi_driver, 0, 0);
215