Home | History | Annotate | Line # | Download | only in rockchip
      1  1.6  riastrad /* $NetBSD: rk_anxdp.c,v 1.6 2021/12/19 12:43:37 riastradh Exp $ */
      2  1.1  jakllsch 
      3  1.1  jakllsch /*-
      4  1.1  jakllsch  * Copyright (c) 2019 Jonathan A. Kollasch <jakllsch (at) kollasch.net>
      5  1.1  jakllsch  * All rights reserved.
      6  1.1  jakllsch  *
      7  1.1  jakllsch  * Redistribution and use in source and binary forms, with or without
      8  1.1  jakllsch  * modification, are permitted provided that the following conditions
      9  1.1  jakllsch  * are met:
     10  1.1  jakllsch  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jakllsch  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jakllsch  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jakllsch  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jakllsch  *    documentation and/or other materials provided with the distribution.
     15  1.1  jakllsch  *
     16  1.1  jakllsch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jakllsch  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jakllsch  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jakllsch  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jakllsch  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jakllsch  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jakllsch  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jakllsch  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jakllsch  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jakllsch  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jakllsch  * SUCH DAMAGE.
     27  1.1  jakllsch  */
     28  1.1  jakllsch 
     29  1.1  jakllsch #include <sys/cdefs.h>
     30  1.6  riastrad __KERNEL_RCSID(0, "$NetBSD: rk_anxdp.c,v 1.6 2021/12/19 12:43:37 riastradh Exp $");
     31  1.1  jakllsch 
     32  1.1  jakllsch #include <sys/param.h>
     33  1.1  jakllsch #include <sys/bus.h>
     34  1.1  jakllsch #include <sys/device.h>
     35  1.1  jakllsch #include <sys/intr.h>
     36  1.1  jakllsch #include <sys/systm.h>
     37  1.1  jakllsch #include <sys/kernel.h>
     38  1.1  jakllsch #include <sys/conf.h>
     39  1.1  jakllsch 
     40  1.5  riastrad #include <drm/drm_drv.h>
     41  1.1  jakllsch #include <drm/drm_crtc_helper.h>
     42  1.1  jakllsch 
     43  1.1  jakllsch #include <dev/fdt/fdtvar.h>
     44  1.1  jakllsch #include <dev/fdt/fdt_port.h>
     45  1.1  jakllsch #include <dev/fdt/syscon.h>
     46  1.1  jakllsch 
     47  1.1  jakllsch #include <dev/ic/anx_dp.h>
     48  1.1  jakllsch 
     49  1.1  jakllsch #define	RK3399_GRF_SOC_CON20		0x6250
     50  1.1  jakllsch #define  EDP_LCDC_SEL			__BIT(5)
     51  1.1  jakllsch 
     52  1.1  jakllsch enum {
     53  1.1  jakllsch 	ANXDP_PORT_INPUT = 0,
     54  1.1  jakllsch 	ANXDP_PORT_OUTPUT = 1,
     55  1.1  jakllsch };
     56  1.1  jakllsch 
     57  1.3   thorpej static const struct device_compatible_entry compat_data[] = {
     58  1.3   thorpej 	{ .compat = "rockchip,rk3399-edp" },
     59  1.3   thorpej 	DEVICE_COMPAT_EOL
     60  1.1  jakllsch };
     61  1.1  jakllsch 
     62  1.1  jakllsch struct rk_anxdp_softc {
     63  1.1  jakllsch 	struct anxdp_softc	sc_base;
     64  1.1  jakllsch 	int			sc_phandle;
     65  1.1  jakllsch 
     66  1.1  jakllsch 	struct fdt_device_ports	sc_ports;
     67  1.1  jakllsch 	struct drm_encoder	sc_encoder;
     68  1.1  jakllsch 	struct drm_display_mode	sc_curmode;
     69  1.1  jakllsch 	struct syscon		*sc_grf;
     70  1.1  jakllsch 
     71  1.1  jakllsch 	bool			sc_activated;
     72  1.1  jakllsch };
     73  1.1  jakllsch 
     74  1.1  jakllsch #define	to_rk_anxdp_softc(x)	container_of(x, struct rk_anxdp_softc, sc_base)
     75  1.1  jakllsch #define	to_rk_anxdp_encoder(x)	container_of(x, struct rk_anxdp_softc, sc_encoder)
     76  1.1  jakllsch 
     77  1.1  jakllsch static void
     78  1.1  jakllsch rk_anxdp_select_input(struct rk_anxdp_softc *sc, u_int crtc_index)
     79  1.1  jakllsch {
     80  1.1  jakllsch 	const uint32_t write_mask = EDP_LCDC_SEL << 16;
     81  1.1  jakllsch 	const uint32_t write_val = crtc_index == 0 ? EDP_LCDC_SEL : 0;
     82  1.1  jakllsch 
     83  1.1  jakllsch 	syscon_lock(sc->sc_grf);
     84  1.1  jakllsch 	syscon_write_4(sc->sc_grf, RK3399_GRF_SOC_CON20, write_mask | write_val);
     85  1.1  jakllsch 	syscon_unlock(sc->sc_grf);
     86  1.1  jakllsch }
     87  1.1  jakllsch 
     88  1.1  jakllsch static bool
     89  1.1  jakllsch rk_anxdp_encoder_mode_fixup(struct drm_encoder *encoder,
     90  1.1  jakllsch     const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
     91  1.1  jakllsch {
     92  1.1  jakllsch 	return true;
     93  1.1  jakllsch }
     94  1.1  jakllsch 
     95  1.1  jakllsch static void
     96  1.1  jakllsch rk_anxdp_encoder_mode_set(struct drm_encoder *encoder,
     97  1.1  jakllsch     struct drm_display_mode *mode, struct drm_display_mode *adjusted)
     98  1.1  jakllsch {
     99  1.1  jakllsch }
    100  1.1  jakllsch 
    101  1.1  jakllsch static void
    102  1.1  jakllsch rk_anxdp_encoder_enable(struct drm_encoder *encoder)
    103  1.1  jakllsch {
    104  1.1  jakllsch }
    105  1.1  jakllsch 
    106  1.1  jakllsch static void
    107  1.1  jakllsch rk_anxdp_encoder_disable(struct drm_encoder *encoder)
    108  1.1  jakllsch {
    109  1.1  jakllsch }
    110  1.1  jakllsch 
    111  1.1  jakllsch static void
    112  1.1  jakllsch rk_anxdp_encoder_prepare(struct drm_encoder *encoder)
    113  1.1  jakllsch {
    114  1.1  jakllsch 	struct rk_anxdp_softc * const sc = to_rk_anxdp_encoder(encoder);
    115  1.1  jakllsch 	const u_int crtc_index = drm_crtc_index(encoder->crtc);
    116  1.1  jakllsch 
    117  1.1  jakllsch 	rk_anxdp_select_input(sc, crtc_index);
    118  1.1  jakllsch }
    119  1.1  jakllsch 
    120  1.1  jakllsch static const struct drm_encoder_funcs rk_anxdp_encoder_funcs = {
    121  1.1  jakllsch 	.destroy = drm_encoder_cleanup,
    122  1.1  jakllsch };
    123  1.1  jakllsch 
    124  1.1  jakllsch static const struct drm_encoder_helper_funcs rk_anxdp_encoder_helper_funcs = {
    125  1.1  jakllsch 	.prepare = rk_anxdp_encoder_prepare,
    126  1.1  jakllsch 	.mode_fixup = rk_anxdp_encoder_mode_fixup,
    127  1.1  jakllsch 	.mode_set = rk_anxdp_encoder_mode_set,
    128  1.1  jakllsch 	.enable = rk_anxdp_encoder_enable,
    129  1.1  jakllsch 	.disable = rk_anxdp_encoder_disable,
    130  1.1  jakllsch };
    131  1.1  jakllsch 
    132  1.1  jakllsch static int
    133  1.1  jakllsch rk_anxdp_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
    134  1.1  jakllsch {
    135  1.1  jakllsch 	struct rk_anxdp_softc * const sc = device_private(dev);
    136  1.1  jakllsch 	struct fdt_endpoint *in_ep = fdt_endpoint_remote(ep);
    137  1.1  jakllsch 	struct fdt_endpoint *out_ep, *out_rep;
    138  1.1  jakllsch 	struct drm_crtc *crtc;
    139  1.1  jakllsch 	int error;
    140  1.1  jakllsch 
    141  1.1  jakllsch 	if (sc->sc_activated != false) {
    142  1.1  jakllsch 		return 0;
    143  1.1  jakllsch 	}
    144  1.1  jakllsch 
    145  1.1  jakllsch 	if (!activate)
    146  1.1  jakllsch 		return EINVAL;
    147  1.1  jakllsch 
    148  1.1  jakllsch 	if (fdt_endpoint_port_index(ep) != ANXDP_PORT_INPUT)
    149  1.1  jakllsch 		return EINVAL;
    150  1.1  jakllsch 
    151  1.1  jakllsch 	switch (fdt_endpoint_type(in_ep)) {
    152  1.1  jakllsch 	case EP_DRM_CRTC:
    153  1.1  jakllsch 		crtc = fdt_endpoint_get_data(in_ep);
    154  1.1  jakllsch 		break;
    155  1.1  jakllsch 	default:
    156  1.1  jakllsch 		return EINVAL;
    157  1.1  jakllsch 		break;
    158  1.1  jakllsch 	}
    159  1.1  jakllsch 
    160  1.4  jmcneill 	sc->sc_encoder.possible_crtcs = 0x2; /* VOPB only */
    161  1.1  jakllsch 	drm_encoder_init(crtc->dev, &sc->sc_encoder, &rk_anxdp_encoder_funcs,
    162  1.5  riastrad 	    DRM_MODE_ENCODER_TMDS, NULL);
    163  1.1  jakllsch 	drm_encoder_helper_add(&sc->sc_encoder, &rk_anxdp_encoder_helper_funcs);
    164  1.1  jakllsch 
    165  1.1  jakllsch 	out_ep = fdt_endpoint_get_from_index(&sc->sc_ports, ANXDP_PORT_OUTPUT, 0);
    166  1.1  jakllsch 	if (out_ep != NULL) {
    167  1.1  jakllsch 		out_rep = fdt_endpoint_remote(out_ep);
    168  1.1  jakllsch 		if (out_rep != NULL && fdt_endpoint_type(out_rep) == EP_DRM_PANEL)
    169  1.1  jakllsch 			sc->sc_base.sc_panel = fdt_endpoint_get_data(out_rep);
    170  1.1  jakllsch 	}
    171  1.1  jakllsch 
    172  1.1  jakllsch         sc->sc_base.sc_connector.base.connector_type = DRM_MODE_CONNECTOR_eDP;
    173  1.1  jakllsch 	error = anxdp_bind(&sc->sc_base, &sc->sc_encoder);
    174  1.1  jakllsch 	if (error != 0)
    175  1.1  jakllsch 		return error;
    176  1.1  jakllsch 	sc->sc_activated = true;
    177  1.1  jakllsch 
    178  1.1  jakllsch 	if (out_ep != NULL) {
    179  1.1  jakllsch 		/* Ignore downstream connectors, we have our own. */
    180  1.1  jakllsch 		if (out_rep != NULL && fdt_endpoint_type(out_rep) == EP_DRM_CONNECTOR)
    181  1.1  jakllsch 			return 0;
    182  1.1  jakllsch 		error = fdt_endpoint_activate(out_ep, activate);
    183  1.1  jakllsch 		if (error != 0)
    184  1.1  jakllsch 			return error;
    185  1.1  jakllsch 	}
    186  1.1  jakllsch 
    187  1.1  jakllsch 	return 0;
    188  1.1  jakllsch }
    189  1.1  jakllsch 
    190  1.1  jakllsch static void *
    191  1.1  jakllsch rk_anxdp_ep_get_data(device_t dev, struct fdt_endpoint *ep)
    192  1.1  jakllsch {
    193  1.1  jakllsch 	struct rk_anxdp_softc * const sc = device_private(dev);
    194  1.1  jakllsch 
    195  1.1  jakllsch 	return &sc->sc_encoder;
    196  1.1  jakllsch }
    197  1.1  jakllsch 
    198  1.1  jakllsch #if ANXDP_AUDIO
    199  1.1  jakllsch static audio_dai_tag_t
    200  1.1  jakllsch rk_anxdp_dai_get_tag(device_t dev, const void *data, size_t len)
    201  1.1  jakllsch {
    202  1.1  jakllsch 	struct rk_anxdp_softc * const sc = device_private(dev);
    203  1.1  jakllsch 
    204  1.1  jakllsch 	if (len != 4)
    205  1.1  jakllsch 		return NULL;
    206  1.1  jakllsch 
    207  1.1  jakllsch 	return &sc->sc_base.sc_dai;
    208  1.1  jakllsch }
    209  1.1  jakllsch 
    210  1.1  jakllsch static struct fdtbus_dai_controller_func rk_anxdp_dai_funcs = {
    211  1.1  jakllsch 	.get_tag = rk_anxdp_dai_get_tag
    212  1.1  jakllsch };
    213  1.1  jakllsch #endif
    214  1.1  jakllsch 
    215  1.1  jakllsch static int
    216  1.1  jakllsch rk_anxdp_match(device_t parent, cfdata_t cf, void *aux)
    217  1.1  jakllsch {
    218  1.1  jakllsch 	struct fdt_attach_args * const faa = aux;
    219  1.1  jakllsch 
    220  1.3   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    221  1.1  jakllsch }
    222  1.1  jakllsch 
    223  1.1  jakllsch static void
    224  1.1  jakllsch rk_anxdp_attach(device_t parent, device_t self, void *aux)
    225  1.1  jakllsch {
    226  1.1  jakllsch 	struct rk_anxdp_softc * const sc = device_private(self);
    227  1.1  jakllsch 	struct fdt_attach_args * const faa = aux;
    228  1.1  jakllsch 	const int phandle = faa->faa_phandle;
    229  1.1  jakllsch 	bus_addr_t addr;
    230  1.1  jakllsch 	bus_size_t size;
    231  1.1  jakllsch 
    232  1.1  jakllsch 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    233  1.1  jakllsch 		aprint_error(": couldn't get registers\n");
    234  1.1  jakllsch 		return;
    235  1.1  jakllsch 	}
    236  1.1  jakllsch 
    237  1.1  jakllsch 	/* Required */
    238  1.1  jakllsch 	if (fdtbus_clock_enable(phandle, "pclk", true) != 0) {
    239  1.1  jakllsch 		aprint_error(": couldn't enable pclk clock\n");
    240  1.1  jakllsch 		return;
    241  1.1  jakllsch 	}
    242  1.1  jakllsch 
    243  1.1  jakllsch 	/* Required */
    244  1.1  jakllsch 	if (fdtbus_clock_enable(phandle, "dp", true) != 0) {
    245  1.1  jakllsch 		aprint_error(": couldn't enable dp clock\n");
    246  1.1  jakllsch 		return;
    247  1.1  jakllsch 	}
    248  1.1  jakllsch 
    249  1.1  jakllsch 	/* Optional */
    250  1.1  jakllsch 	if (fdtbus_clock_enable(phandle, "grf", false) != 0) {
    251  1.1  jakllsch 		aprint_error(": couldn't enable grf clock\n");
    252  1.1  jakllsch 		return;
    253  1.1  jakllsch 	}
    254  1.1  jakllsch 
    255  1.1  jakllsch 	/* TODO: Optional phy */
    256  1.1  jakllsch 
    257  1.1  jakllsch 	sc->sc_base.sc_dev = self;
    258  1.1  jakllsch 	sc->sc_base.sc_bst = faa->faa_bst;
    259  1.1  jakllsch 	if (bus_space_map(sc->sc_base.sc_bst, addr, size, 0, &sc->sc_base.sc_bsh) != 0) {
    260  1.1  jakllsch 		aprint_error(": couldn't map registers\n");
    261  1.1  jakllsch 		return;
    262  1.1  jakllsch 	}
    263  1.1  jakllsch 	sc->sc_phandle = faa->faa_phandle;
    264  1.1  jakllsch 	sc->sc_grf = fdtbus_syscon_acquire(phandle, "rockchip,grf");
    265  1.1  jakllsch 	if (sc->sc_grf == NULL) {
    266  1.1  jakllsch 		aprint_error(": couldn't get grf syscon\n");
    267  1.1  jakllsch 		return;
    268  1.1  jakllsch 	}
    269  1.1  jakllsch 
    270  1.1  jakllsch 	aprint_naive("\n");
    271  1.1  jakllsch 	aprint_normal(": eDP TX\n");
    272  1.1  jakllsch 
    273  1.1  jakllsch 	sc->sc_base.sc_flags |= ANXDP_FLAG_ROCKCHIP;
    274  1.1  jakllsch 
    275  1.1  jakllsch 	if (anxdp_attach(&sc->sc_base) != 0) {
    276  1.1  jakllsch 		aprint_error_dev(self, "failed to attach driver\n");
    277  1.1  jakllsch 		return;
    278  1.1  jakllsch 	}
    279  1.1  jakllsch 
    280  1.1  jakllsch 	sc->sc_ports.dp_ep_activate = rk_anxdp_ep_activate;
    281  1.1  jakllsch 	sc->sc_ports.dp_ep_get_data = rk_anxdp_ep_get_data;
    282  1.1  jakllsch 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_ENCODER);
    283  1.1  jakllsch 
    284  1.1  jakllsch #if ANXDP_AUDIO
    285  1.1  jakllsch 	fdtbus_register_dai_controller(self, phandle, &rk_anxdp_dai_funcs);
    286  1.1  jakllsch #endif
    287  1.1  jakllsch }
    288  1.1  jakllsch 
    289  1.1  jakllsch CFATTACH_DECL_NEW(rk_anxdp, sizeof(struct rk_anxdp_softc),
    290  1.1  jakllsch 	rk_anxdp_match, rk_anxdp_attach, NULL, NULL);
    291