xref: /freebsd/sys/arm/freescale/imx/imx6_hdmi.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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 __FBSDID("$FreeBSD$");
29 
30 /*
31  * HDMI core module
32  */
33 
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>
39 #include <sys/bus.h>
40 #include <sys/rman.h>
41 
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 
45 #include <machine/bus.h>
46 
47 #include <dev/videomode/videomode.h>
48 #include <dev/videomode/edidvar.h>
49 
50 #include <arm/freescale/imx/imx_ccmvar.h>
51 #include <arm/freescale/imx/imx_iomuxvar.h>
52 #include <arm/freescale/imx/imx_iomuxreg.h>
53 
54 #include <dev/hdmi/dwc_hdmi.h>
55 
56 #include "hdmi_if.h"
57 
58 struct imx_hdmi_softc {
59 	struct dwc_hdmi_softc	base;
60 	phandle_t		i2c_xref;
61 	eventhandler_tag	eh_tag;
62 };
63 
64 static struct ofw_compat_data compat_data[] = {
65 	{"fsl,imx6dl-hdmi", 1},
66 	{"fsl,imx6q-hdmi",  1},
67 	{NULL,	            0}
68 };
69 
70 static device_t
71 imx_hdmi_get_i2c_dev(device_t dev)
72 {
73 	struct imx_hdmi_softc *sc;
74 
75 	sc = device_get_softc(dev);
76 
77 	if (sc->i2c_xref == 0)
78 		return (NULL);
79 
80 	return (OF_device_from_xref(sc->i2c_xref));
81 }
82 
83 /*
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.
90  *
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.
99  */
100 static void
101 imx_hdmi_init(void *dev)
102 {
103 	struct imx_hdmi_softc *sc;
104 
105 	sc = device_get_softc((device_t)dev);
106 
107 	if (OF_device_from_xref(sc->i2c_xref) != NULL) {
108 		if (sc->eh_tag != NULL) {
109 			EVENTHANDLER_DEREGISTER_NOWAIT(device_attach,
110 			    sc->eh_tag);
111 		}
112 		dwc_hdmi_init(dev);
113 		return;
114 	}
115 
116 	if (bootverbose)
117 		device_printf((device_t)dev, "Waiting for DDC i2c device\n");
118 
119 	if (sc->eh_tag == NULL) {
120 		sc->eh_tag = EVENTHANDLER_REGISTER(device_attach,
121 		    imx_hdmi_init, dev, EVENTHANDLER_PRI_ANY);
122 	}
123 }
124 
125 static int
126 imx_hdmi_detach(device_t dev)
127 {
128 	struct imx_hdmi_softc *sc;
129 
130 	sc = device_get_softc(dev);
131 
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);
135 
136 	return (0);
137 }
138 
139 static int
140 imx_hdmi_attach(device_t dev)
141 {
142 	struct imx_hdmi_softc *sc;
143 	int err;
144 	uint32_t gpr3;
145 	phandle_t node, i2c_xref;
146 
147 	sc = device_get_softc(dev);
148 	sc->base.sc_dev = dev;
149 	sc->base.sc_get_i2c_dev = imx_hdmi_get_i2c_dev;
150 	err = 0;
151 
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");
158 		err = ENXIO;
159 		goto out;
160 	}
161 
162 	node = ofw_bus_get_node(dev);
163 	if (OF_getencprop(node, "ddc-i2c-bus", &i2c_xref, sizeof(i2c_xref)) == -1)
164 		sc->i2c_xref = 0;
165 	else
166 		sc->i2c_xref = i2c_xref;
167 
168 	imx_ccm_hdmi_enable();
169 
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);
174 
175 	/* Further HDMI init requires interrupts for i2c transfers. */
176 	config_intrhook_oneshot(imx_hdmi_init, dev);
177 	return (0);
178 
179 out:
180 	imx_hdmi_detach(dev);
181 
182 	return (err);
183 }
184 
185 static int
186 imx_hdmi_probe(device_t dev)
187 {
188 
189 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
190 		return (ENXIO);
191 
192 	device_set_desc(dev, "Freescale i.MX6 HDMI core");
193 
194 	return (BUS_PROBE_DEFAULT);
195 }
196 
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),
202 
203 	/* HDMI methods */
204 	DEVMETHOD(hdmi_get_edid,	dwc_hdmi_get_edid),
205 	DEVMETHOD(hdmi_set_videomode,	dwc_hdmi_set_videomode),
206 
207 	DEVMETHOD_END
208 };
209 
210 static driver_t imx_hdmi_driver = {
211 	"hdmi",
212 	imx_hdmi_methods,
213 	sizeof(struct imx_hdmi_softc)
214 };
215 
216 static devclass_t imx_hdmi_devclass;
217 
218 DRIVER_MODULE(hdmi, simplebus, imx_hdmi_driver, imx_hdmi_devclass, 0, 0);
219