xref: /freebsd/sys/dev/hdmi/dwc_hdmi_fdt.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
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 __FBSDID("$FreeBSD$");
30 
31 /*
32  * HDMI core module
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.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/extres/clk/clk.h>
48 
49 #include <dev/videomode/videomode.h>
50 #include <dev/videomode/edidvar.h>
51 
52 #include <dev/hdmi/dwc_hdmi.h>
53 
54 #include "hdmi_if.h"
55 
56 struct dwc_hdmi_fdt_softc {
57 	struct dwc_hdmi_softc	base;
58 	clk_t			clk_hdmi;
59 	clk_t			clk_ahb;
60 	phandle_t		i2c_xref;
61 };
62 
63 static struct ofw_compat_data compat_data[] = {
64 	{ "synopsys,dwc-hdmi",	1 },
65 	{ NULL,	            	0 }
66 };
67 
68 static device_t
69 dwc_hdmi_fdt_get_i2c_dev(device_t dev)
70 {
71 	struct dwc_hdmi_fdt_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 dwc_hdmi_fdt_detach(device_t dev)
83 {
84 	struct dwc_hdmi_fdt_softc *sc;
85 
86 	sc = device_get_softc(dev);
87 
88 	if (sc->clk_ahb != NULL)
89 		clk_release(sc->clk_ahb);
90 	if (sc->clk_hdmi != NULL)
91 		clk_release(sc->clk_hdmi);
92 
93 	if (sc->base.sc_mem_res != NULL)
94 		bus_release_resource(dev, SYS_RES_MEMORY,
95 		    sc->base.sc_mem_rid, sc->base.sc_mem_res);
96 
97 	return (0);
98 }
99 
100 static int
101 dwc_hdmi_fdt_attach(device_t dev)
102 {
103 	struct dwc_hdmi_fdt_softc *sc;
104 	phandle_t node, i2c_xref;
105 	uint32_t freq;
106 	int err;
107 
108 	sc = device_get_softc(dev);
109 	sc->base.sc_dev = dev;
110 	sc->base.sc_get_i2c_dev = dwc_hdmi_fdt_get_i2c_dev;
111 	err = 0;
112 
113 	/* Allocate memory resources. */
114 	sc->base.sc_mem_rid = 0;
115 	sc->base.sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
116 	    &sc->base.sc_mem_rid, RF_ACTIVE);
117 	if (sc->base.sc_mem_res == NULL) {
118 		device_printf(dev, "Cannot allocate memory resources\n");
119 		err = ENXIO;
120 		goto out;
121 	}
122 
123 	node = ofw_bus_get_node(dev);
124 	if (OF_getencprop(node, "ddc", &i2c_xref, sizeof(i2c_xref)) == -1)
125 		sc->i2c_xref = 0;
126 	else
127 		sc->i2c_xref = i2c_xref;
128 
129 	if (OF_getencprop(node, "reg-shift", &sc->base.sc_reg_shift,
130 	    sizeof(sc->base.sc_reg_shift)) <= 0)
131 		sc->base.sc_reg_shift = 0;
132 
133 	if (clk_get_by_ofw_name(dev, 0, "hdmi", &sc->clk_hdmi) != 0 ||
134 	    clk_get_by_ofw_name(dev, 0, "ahb", &sc->clk_ahb) != 0) {
135 		device_printf(dev, "Cannot get clocks\n");
136 		err = ENXIO;
137 		goto out;
138 	}
139 	if (OF_getencprop(node, "clock-frequency", &freq, sizeof(freq)) > 0) {
140 		err = clk_set_freq(sc->clk_hdmi, freq, CLK_SET_ROUND_DOWN);
141 		if (err != 0) {
142 			device_printf(dev,
143 			    "Cannot set HDMI clock frequency to %u Hz\n", freq);
144 			goto out;
145 		}
146 	} else
147 		device_printf(dev, "HDMI clock frequency not specified\n");
148 	if (clk_enable(sc->clk_hdmi) != 0) {
149 		device_printf(dev, "Cannot enable HDMI clock\n");
150 		err = ENXIO;
151 		goto out;
152 	}
153 	if (clk_enable(sc->clk_ahb) != 0) {
154 		device_printf(dev, "Cannot enable AHB clock\n");
155 		err = ENXIO;
156 		goto out;
157 	}
158 
159 	return (dwc_hdmi_init(dev));
160 
161 out:
162 
163 	dwc_hdmi_fdt_detach(dev);
164 
165 	return (err);
166 }
167 
168 static int
169 dwc_hdmi_fdt_probe(device_t dev)
170 {
171 
172 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
173 		return (ENXIO);
174 
175 	device_set_desc(dev, "Synopsys DesignWare HDMI Controller");
176 
177 	return (BUS_PROBE_DEFAULT);
178 }
179 
180 static device_method_t dwc_hdmi_fdt_methods[] = {
181 	/* Device interface */
182 	DEVMETHOD(device_probe,  dwc_hdmi_fdt_probe),
183 	DEVMETHOD(device_attach, dwc_hdmi_fdt_attach),
184 	DEVMETHOD(device_detach, dwc_hdmi_fdt_detach),
185 
186 	/* HDMI methods */
187 	DEVMETHOD(hdmi_get_edid,	dwc_hdmi_get_edid),
188 	DEVMETHOD(hdmi_set_videomode,	dwc_hdmi_set_videomode),
189 
190 	DEVMETHOD_END
191 };
192 
193 static driver_t dwc_hdmi_fdt_driver = {
194 	"dwc_hdmi",
195 	dwc_hdmi_fdt_methods,
196 	sizeof(struct dwc_hdmi_fdt_softc)
197 };
198 
199 static devclass_t dwc_hdmi_fdt_devclass;
200 
201 DRIVER_MODULE(dwc_hdmi_fdt, simplebus, dwc_hdmi_fdt_driver,
202     dwc_hdmi_fdt_devclass, 0, 0);
203