xref: /freebsd/sys/dev/hdmi/dwc_hdmi_fdt.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*-
2  * Copyright (c) 2015 Oleksandr Tymoshenko <gonzo@freebsd.org>
3  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 /*
30  * HDMI core module
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.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/extres/clk/clk.h>
46 
47 #include <dev/videomode/videomode.h>
48 #include <dev/videomode/edidvar.h>
49 
50 #include <dev/hdmi/dwc_hdmi.h>
51 
52 #include "crtc_if.h"
53 
54 struct dwc_hdmi_fdt_softc {
55 	struct dwc_hdmi_softc	base;
56 	clk_t			clk_hdmi;
57 	clk_t			clk_ahb;
58 	phandle_t		i2c_xref;
59 };
60 
61 static struct ofw_compat_data compat_data[] = {
62 	{ "synopsys,dwc-hdmi",	1 },
63 	{ NULL,	            	0 }
64 };
65 
66 static device_t
67 dwc_hdmi_fdt_get_i2c_dev(device_t dev)
68 {
69 	struct dwc_hdmi_fdt_softc *sc;
70 
71 	sc = device_get_softc(dev);
72 
73 	if (sc->i2c_xref == 0)
74 		return (NULL);
75 
76 	return (OF_device_from_xref(sc->i2c_xref));
77 }
78 
79 static int
80 dwc_hdmi_fdt_detach(device_t dev)
81 {
82 	struct dwc_hdmi_fdt_softc *sc;
83 
84 	sc = device_get_softc(dev);
85 
86 	if (sc->clk_ahb != NULL)
87 		clk_release(sc->clk_ahb);
88 	if (sc->clk_hdmi != NULL)
89 		clk_release(sc->clk_hdmi);
90 
91 	if (sc->base.sc_mem_res != NULL)
92 		bus_release_resource(dev, SYS_RES_MEMORY,
93 		    sc->base.sc_mem_rid, sc->base.sc_mem_res);
94 
95 	return (0);
96 }
97 
98 static int
99 dwc_hdmi_fdt_attach(device_t dev)
100 {
101 	struct dwc_hdmi_fdt_softc *sc;
102 	phandle_t node, i2c_xref;
103 	uint32_t freq;
104 	int err;
105 
106 	sc = device_get_softc(dev);
107 	sc->base.sc_dev = dev;
108 	sc->base.sc_get_i2c_dev = dwc_hdmi_fdt_get_i2c_dev;
109 	err = 0;
110 
111 	/* Allocate memory resources. */
112 	sc->base.sc_mem_rid = 0;
113 	sc->base.sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
114 	    &sc->base.sc_mem_rid, RF_ACTIVE);
115 	if (sc->base.sc_mem_res == NULL) {
116 		device_printf(dev, "Cannot allocate memory resources\n");
117 		err = ENXIO;
118 		goto out;
119 	}
120 
121 	node = ofw_bus_get_node(dev);
122 	if (OF_getencprop(node, "ddc", &i2c_xref, sizeof(i2c_xref)) == -1)
123 		sc->i2c_xref = 0;
124 	else
125 		sc->i2c_xref = i2c_xref;
126 
127 	if (OF_getencprop(node, "reg-shift", &sc->base.sc_reg_shift,
128 	    sizeof(sc->base.sc_reg_shift)) <= 0)
129 		sc->base.sc_reg_shift = 0;
130 
131 	if (clk_get_by_ofw_name(dev, 0, "hdmi", &sc->clk_hdmi) != 0 ||
132 	    clk_get_by_ofw_name(dev, 0, "ahb", &sc->clk_ahb) != 0) {
133 		device_printf(dev, "Cannot get clocks\n");
134 		err = ENXIO;
135 		goto out;
136 	}
137 	if (OF_getencprop(node, "clock-frequency", &freq, sizeof(freq)) > 0) {
138 		err = clk_set_freq(sc->clk_hdmi, freq, CLK_SET_ROUND_DOWN);
139 		if (err != 0) {
140 			device_printf(dev,
141 			    "Cannot set HDMI clock frequency to %u Hz\n", freq);
142 			goto out;
143 		}
144 	} else
145 		device_printf(dev, "HDMI clock frequency not specified\n");
146 	if (clk_enable(sc->clk_hdmi) != 0) {
147 		device_printf(dev, "Cannot enable HDMI clock\n");
148 		err = ENXIO;
149 		goto out;
150 	}
151 	if (clk_enable(sc->clk_ahb) != 0) {
152 		device_printf(dev, "Cannot enable AHB clock\n");
153 		err = ENXIO;
154 		goto out;
155 	}
156 
157 	return (dwc_hdmi_init(dev));
158 
159 out:
160 
161 	dwc_hdmi_fdt_detach(dev);
162 
163 	return (err);
164 }
165 
166 static int
167 dwc_hdmi_fdt_probe(device_t dev)
168 {
169 
170 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
171 		return (ENXIO);
172 
173 	device_set_desc(dev, "Synopsys DesignWare HDMI Controller");
174 
175 	return (BUS_PROBE_DEFAULT);
176 }
177 
178 static device_method_t dwc_hdmi_fdt_methods[] = {
179 	/* Device interface */
180 	DEVMETHOD(device_probe,  dwc_hdmi_fdt_probe),
181 	DEVMETHOD(device_attach, dwc_hdmi_fdt_attach),
182 	DEVMETHOD(device_detach, dwc_hdmi_fdt_detach),
183 
184 	/* HDMI methods */
185 	DEVMETHOD(hdmi_get_edid,	dwc_hdmi_get_edid),
186 	DEVMETHOD(hdmi_set_videomode,	dwc_hdmi_set_videomode),
187 
188 	DEVMETHOD_END
189 };
190 
191 static driver_t dwc_hdmi_fdt_driver = {
192 	"dwc_hdmi",
193 	dwc_hdmi_fdt_methods,
194 	sizeof(struct dwc_hdmi_fdt_softc)
195 };
196 
197 DRIVER_MODULE(dwc_hdmi_fdt, simplebus, dwc_hdmi_fdt_driver, 0, 0);
198