Home | History | Annotate | Line # | Download | only in sunxi
      1 /* $NetBSD: sunxi_dwhdmi.c,v 1.11 2021/12/19 11:01:10 riastradh Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2019 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: sunxi_dwhdmi.c,v 1.11 2021/12/19 11:01:10 riastradh Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/conf.h>
     35 #include <sys/device.h>
     36 #include <sys/intr.h>
     37 #include <sys/kernel.h>
     38 #include <sys/systm.h>
     39 
     40 #include <dev/fdt/fdt_port.h>
     41 #include <dev/fdt/fdtvar.h>
     42 
     43 #include <dev/ic/dw_hdmi.h>
     44 
     45 #include <arm/sunxi/sunxi_hdmiphy.h>
     46 
     47 #include <drm/drm_drv.h>
     48 
     49 enum {
     50 	DWHDMI_PORT_INPUT = 0,
     51 	DWHDMI_PORT_OUTPUT = 1,
     52 };
     53 
     54 static const struct device_compatible_entry compat_data[] = {
     55 	{ .compat = "allwinner,sun8i-h3-dw-hdmi" },
     56 	{ .compat = "allwinner,sun50i-a64-dw-hdmi" },
     57 	DEVICE_COMPAT_EOL
     58 };
     59 
     60 struct sunxi_dwhdmi_softc {
     61 	struct dwhdmi_softc	sc_base;
     62 	int			sc_phandle;
     63 	struct fdtbus_phy	*sc_phy;
     64 	struct fdtbus_regulator	*sc_regulator;
     65 	struct clk		*sc_clk;
     66 
     67 	struct fdt_device_ports	sc_ports;
     68 	struct drm_display_mode	sc_curmode;
     69 };
     70 
     71 #define	to_sunxi_dwhdmi_softc(x)	container_of(x, struct sunxi_dwhdmi_softc, sc_base)
     72 
     73 static int
     74 sunxi_dwhdmi_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
     75 {
     76 	struct sunxi_dwhdmi_softc * const sc = device_private(dev);
     77 	struct fdt_endpoint *in_ep = fdt_endpoint_remote(ep);
     78 	struct fdt_endpoint *out_ep, *out_rep;
     79 	struct drm_encoder *encoder;
     80 	struct drm_bridge *bridge;
     81 	int error;
     82 
     83 	if (!activate)
     84 		return EINVAL;
     85 
     86 	if (fdt_endpoint_port_index(ep) != DWHDMI_PORT_INPUT)
     87 		return EINVAL;
     88 
     89 	switch (fdt_endpoint_type(in_ep)) {
     90 	case EP_DRM_ENCODER:
     91 		encoder = fdt_endpoint_get_data(in_ep);
     92 		break;
     93 	case EP_DRM_BRIDGE:
     94 		bridge = fdt_endpoint_get_data(in_ep);
     95 		encoder = bridge->encoder;
     96 		break;
     97 	default:
     98 		encoder = NULL;
     99 		break;
    100 	}
    101 
    102 	if (encoder == NULL)
    103 		return EINVAL;
    104 
    105 	if (sc->sc_regulator != NULL) {
    106 		error = fdtbus_regulator_enable(sc->sc_regulator);
    107 		if (error != 0) {
    108 			device_printf(dev, "couldn't enable supply\n");
    109 			return error;
    110 		}
    111 	}
    112 
    113 	error = dwhdmi_bind(&sc->sc_base, encoder);
    114 	if (error != 0)
    115 		return error;
    116 
    117 	out_ep = fdt_endpoint_get_from_index(&sc->sc_ports, DWHDMI_PORT_OUTPUT, 0);
    118 	if (out_ep != NULL) {
    119 		/* Ignore downstream connectors, we have our own. */
    120 		out_rep = fdt_endpoint_remote(out_ep);
    121 		if (out_rep != NULL && fdt_endpoint_type(out_rep) == EP_DRM_CONNECTOR)
    122 			return 0;
    123 
    124 		error = fdt_endpoint_activate(out_ep, activate);
    125 		if (error != 0)
    126 			return error;
    127 	}
    128 
    129 	return 0;
    130 }
    131 
    132 static void *
    133 sunxi_dwhdmi_ep_get_data(device_t dev, struct fdt_endpoint *ep)
    134 {
    135 	struct sunxi_dwhdmi_softc * const sc = device_private(dev);
    136 
    137 	return &sc->sc_base.sc_bridge;
    138 }
    139 
    140 static enum drm_connector_status
    141 sunxi_dwhdmi_detect(struct dwhdmi_softc *dsc, bool force)
    142 {
    143 	struct sunxi_dwhdmi_softc * const sc = to_sunxi_dwhdmi_softc(dsc);
    144 
    145 	KASSERT(sc->sc_phy != NULL);
    146 
    147 	if (sunxi_hdmiphy_detect(sc->sc_phy, force))
    148 		return connector_status_connected;
    149 	else
    150 		return connector_status_disconnected;
    151 }
    152 
    153 static void
    154 sunxi_dwhdmi_enable(struct dwhdmi_softc *dsc)
    155 {
    156 	struct sunxi_dwhdmi_softc * const sc = to_sunxi_dwhdmi_softc(dsc);
    157 	int error;
    158 
    159 	KASSERT(sc->sc_phy != NULL);
    160 
    161 	error = fdtbus_phy_enable(sc->sc_phy, true);
    162 	if (error != 0) {
    163 		device_printf(dsc->sc_dev, "failed to enable phy: %d\n", error);
    164 		return;
    165 	}
    166 
    167 	error = sunxi_hdmiphy_config(sc->sc_phy, &sc->sc_curmode);
    168 	if (error != 0)
    169 		device_printf(dsc->sc_dev, "failed to configure phy: %d\n", error);
    170 }
    171 
    172 static void
    173 sunxi_dwhdmi_disable(struct dwhdmi_softc *dsc)
    174 {
    175 	struct sunxi_dwhdmi_softc * const sc = to_sunxi_dwhdmi_softc(dsc);
    176 	int error;
    177 
    178 	KASSERT(sc->sc_phy != NULL);
    179 
    180 	error = fdtbus_phy_enable(sc->sc_phy, false);
    181 	if (error != 0)
    182 		device_printf(dsc->sc_dev, "failed to disable phy\n");
    183 }
    184 
    185 static void
    186 sunxi_dwhdmi_mode_set(struct dwhdmi_softc *dsc,
    187     const struct drm_display_mode *mode,
    188     const struct drm_display_mode *adjusted_mode)
    189 {
    190 	struct sunxi_dwhdmi_softc * const sc = to_sunxi_dwhdmi_softc(dsc);
    191 	int error;
    192 
    193 	if (sc->sc_clk != NULL) {
    194 		error = clk_set_rate(sc->sc_clk, adjusted_mode->clock * 1000);
    195 		if (error != 0)
    196 			device_printf(sc->sc_base.sc_dev,
    197 			    "couldn't set pixel clock to %u Hz: %d\n",
    198 			    adjusted_mode->clock * 1000, error);
    199 	}
    200 
    201 	sc->sc_curmode = *adjusted_mode;
    202 }
    203 
    204 static audio_dai_tag_t
    205 sunxi_dwhdmi_dai_get_tag(device_t dev, const void *data, size_t len)
    206 {
    207 	struct sunxi_dwhdmi_softc * const sc = device_private(dev);
    208 
    209 	if (len != 4)
    210 		return NULL;
    211 
    212 	return &sc->sc_base.sc_dai;
    213 }
    214 
    215 static struct fdtbus_dai_controller_func sunxi_dwhdmi_dai_funcs = {
    216 	.get_tag = sunxi_dwhdmi_dai_get_tag
    217 };
    218 
    219 static int
    220 sunxi_dwhdmi_match(device_t parent, cfdata_t cf, void *aux)
    221 {
    222 	struct fdt_attach_args * const faa = aux;
    223 
    224 	return of_compatible_match(faa->faa_phandle, compat_data);
    225 }
    226 
    227 static void
    228 sunxi_dwhdmi_attach(device_t parent, device_t self, void *aux)
    229 {
    230 	struct sunxi_dwhdmi_softc * const sc = device_private(self);
    231 	struct fdt_attach_args * const faa = aux;
    232 	prop_dictionary_t prop = device_properties(self);
    233 	const int phandle = faa->faa_phandle;
    234 	struct clk *clk_iahb, *clk_isfr, *clk_tmds;
    235 	struct fdtbus_reset *rst;
    236 	bool is_disabled;
    237 	bus_addr_t addr;
    238 	bus_size_t size;
    239 
    240 	if (prop_dictionary_get_bool(prop, "disabled", &is_disabled) && is_disabled) {
    241 		aprint_naive("\n");
    242 		aprint_normal(": HDMI TX (disabled)\n");
    243 		return;
    244 	}
    245 
    246 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    247 		aprint_error(": couldn't get registers\n");
    248 		return;
    249 	}
    250 
    251 	clk_iahb = fdtbus_clock_get(phandle, "iahb");
    252 	if (clk_iahb == NULL || clk_enable(clk_iahb) != 0) {
    253 		aprint_error(": couldn't enable iahb clock\n");
    254 		return;
    255 	}
    256 
    257 	clk_isfr = fdtbus_clock_get(phandle, "isfr");
    258 	if (clk_isfr == NULL || clk_enable(clk_isfr) != 0) {
    259 		aprint_error(": couldn't enable isfr clock\n");
    260 		return;
    261 	}
    262 
    263 	clk_tmds = fdtbus_clock_get(phandle, "tmds");
    264 	if (clk_tmds == NULL || clk_enable(clk_tmds) != 0) {
    265 		aprint_error(": couldn't enable tmds clock\n");
    266 		return;
    267 	}
    268 
    269 	sc->sc_base.sc_dev = self;
    270 	sc->sc_base.sc_reg_width = 1;
    271 	sc->sc_base.sc_bst = faa->faa_bst;
    272 	if (bus_space_map(sc->sc_base.sc_bst, addr, size, 0, &sc->sc_base.sc_bsh) != 0) {
    273 		aprint_error(": couldn't map registers\n");
    274 		return;
    275 	}
    276 	sc->sc_base.sc_detect = sunxi_dwhdmi_detect;
    277 	sc->sc_base.sc_enable = sunxi_dwhdmi_enable;
    278 	sc->sc_base.sc_disable = sunxi_dwhdmi_disable;
    279 	sc->sc_base.sc_mode_set = sunxi_dwhdmi_mode_set;
    280 	sc->sc_base.sc_scl_hcnt = 0xd8;
    281 	sc->sc_base.sc_scl_lcnt = 0xfe;
    282 	sc->sc_phandle = faa->faa_phandle;
    283 	sc->sc_clk = clk_tmds;
    284 
    285 	aprint_naive("\n");
    286 	aprint_normal(": HDMI TX\n");
    287 
    288 	sc->sc_regulator = fdtbus_regulator_acquire(sc->sc_phandle, "hvcc-supply");
    289 
    290 	sc->sc_phy = fdtbus_phy_get(sc->sc_phandle, "hdmi-phy");
    291 	if (sc->sc_phy == NULL)
    292 		sc->sc_phy = fdtbus_phy_get(sc->sc_phandle, "phy");
    293 	if (sc->sc_phy == NULL) {
    294 		device_printf(self, "couldn't find PHY\n");
    295 		return;
    296 	}
    297 
    298 	rst = fdtbus_reset_get(phandle, "ctrl");
    299 	if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
    300 		aprint_error_dev(self, "couldn't de-assert reset\n");
    301 		return;
    302 	}
    303 
    304 	sunxi_hdmiphy_init(sc->sc_phy);
    305 
    306 	if (dwhdmi_attach(&sc->sc_base) != 0) {
    307 		aprint_error_dev(self, "failed to attach driver\n");
    308 		return;
    309 	}
    310 
    311 	sc->sc_ports.dp_ep_activate = sunxi_dwhdmi_ep_activate;
    312 	sc->sc_ports.dp_ep_get_data = sunxi_dwhdmi_ep_get_data;
    313 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_BRIDGE);
    314 
    315 	fdtbus_register_dai_controller(self, phandle, &sunxi_dwhdmi_dai_funcs);
    316 }
    317 
    318 CFATTACH_DECL_NEW(sunxi_dwhdmi, sizeof(struct sunxi_dwhdmi_softc),
    319 	sunxi_dwhdmi_match, sunxi_dwhdmi_attach, NULL, NULL);
    320