xref: /freebsd/sys/arm/freescale/imx/imx6_hdmi.c (revision 792bbaba989533a1fc93823df1720c8c4aaf0442)
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/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/rman.h>
40 
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 
44 #include <machine/bus.h>
45 
46 #include <dev/videomode/videomode.h>
47 #include <dev/videomode/edidvar.h>
48 
49 #include <arm/freescale/imx/imx_ccmvar.h>
50 #include <arm/freescale/imx/imx_iomuxvar.h>
51 #include <arm/freescale/imx/imx_iomuxreg.h>
52 
53 #include <dev/hdmi/dwc_hdmi.h>
54 
55 #include "hdmi_if.h"
56 
57 struct imx_hdmi_softc {
58 	struct dwc_hdmi_softc	base;
59 	phandle_t		i2c_xref;
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
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 static int
82 imx_hdmi_detach(device_t dev)
83 {
84 	struct imx_hdmi_softc *sc;
85 
86 	sc = device_get_softc(dev);
87 
88 	if (sc->base.sc_mem_res != NULL)
89 		bus_release_resource(dev, SYS_RES_MEMORY,
90 		    sc->base.sc_mem_rid, sc->base.sc_mem_res);
91 
92 	return (0);
93 }
94 
95 static int
96 imx_hdmi_attach(device_t dev)
97 {
98 	struct imx_hdmi_softc *sc;
99 	int err;
100 	uint32_t gpr3;
101 	phandle_t node, i2c_xref;
102 
103 	sc = device_get_softc(dev);
104 	sc->base.sc_dev = dev;
105 	sc->base.sc_get_i2c_dev = imx_hdmi_get_i2c_dev;
106 	err = 0;
107 
108 	/* Allocate memory resources. */
109 	sc->base.sc_mem_rid = 0;
110 	sc->base.sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
111 	    &sc->base.sc_mem_rid, RF_ACTIVE);
112 	if (sc->base.sc_mem_res == NULL) {
113 		device_printf(dev, "Cannot allocate memory resources\n");
114 		err = ENXIO;
115 		goto out;
116 	}
117 
118 	node = ofw_bus_get_node(dev);
119 	if (OF_getencprop(node, "ddc-i2c-bus", &i2c_xref, sizeof(i2c_xref)) == -1)
120 		sc->i2c_xref = 0;
121 	else
122 		sc->i2c_xref = i2c_xref;
123 
124 	imx_ccm_hdmi_enable();
125 
126 	gpr3 = imx_iomux_gpr_get(IOMUXC_GPR3);
127 	gpr3 &= ~(IOMUXC_GPR3_HDMI_MASK);
128 	gpr3 |= IOMUXC_GPR3_HDMI_IPU1_DI0;
129 	imx_iomux_gpr_set(IOMUXC_GPR3, gpr3);
130 
131 	return (dwc_hdmi_init(dev));
132 
133 out:
134 	imx_hdmi_detach(dev);
135 
136 	return (err);
137 }
138 
139 static int
140 imx_hdmi_probe(device_t dev)
141 {
142 
143 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
144 		return (ENXIO);
145 
146 	device_set_desc(dev, "Freescale i.MX6 HDMI core");
147 
148 	return (BUS_PROBE_DEFAULT);
149 }
150 
151 static device_method_t imx_hdmi_methods[] = {
152 	/* Device interface */
153 	DEVMETHOD(device_probe,  imx_hdmi_probe),
154 	DEVMETHOD(device_attach, imx_hdmi_attach),
155 	DEVMETHOD(device_detach, imx_hdmi_detach),
156 
157 	/* HDMI methods */
158 	DEVMETHOD(hdmi_get_edid,	dwc_hdmi_get_edid),
159 	DEVMETHOD(hdmi_set_videomode,	dwc_hdmi_set_videomode),
160 
161 	DEVMETHOD_END
162 };
163 
164 static driver_t imx_hdmi_driver = {
165 	"hdmi",
166 	imx_hdmi_methods,
167 	sizeof(struct imx_hdmi_softc)
168 };
169 
170 static devclass_t imx_hdmi_devclass;
171 
172 DRIVER_MODULE(hdmi, simplebus, imx_hdmi_driver, imx_hdmi_devclass, 0, 0);
173