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