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