1 /* $OpenBSD: rkanxdp.c,v 1.4 2020/06/08 04:47:58 jsg Exp $ */ 2 /* $NetBSD: rk_anxdp.c,v 1.2 2020/01/04 12:08:32 jmcneill Exp $ */ 3 /*- 4 * Copyright (c) 2019 Jonathan A. Kollasch <jakllsch@kollasch.net> 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/param.h> 30 #include <sys/device.h> 31 #include <sys/systm.h> 32 33 #include <machine/bus.h> 34 #include <machine/fdt.h> 35 36 #include <dev/ofw/openfirm.h> 37 #include <dev/ofw/ofw_clock.h> 38 #include <dev/ofw/ofw_gpio.h> 39 #include <dev/ofw/ofw_misc.h> 40 #include <dev/ofw/ofw_pinctrl.h> 41 #include <dev/ofw/fdt.h> 42 43 #include <drm/drm_crtc_helper.h> 44 45 #include <dev/ic/anxdp.h> 46 47 #define RK3399_GRF_SOC_CON20 0x6250 48 #define EDP_LCDC_SEL (1 << 5) 49 50 enum { 51 ANXDP_PORT_INPUT = 0, 52 ANXDP_PORT_OUTPUT = 1, 53 }; 54 55 struct rkanxdp_softc { 56 struct anxdp_softc sc_base; 57 58 struct drm_encoder sc_encoder; 59 struct drm_display_mode sc_curmode; 60 struct regmap *sc_grf; 61 62 int sc_activated; 63 64 struct device_ports sc_ports; 65 }; 66 67 #define to_rkanxdp_softc(x) container_of(x, struct rkanxdp_softc, sc_base) 68 #define to_rkanxdp_encoder(x) container_of(x, struct rkanxdp_softc, sc_encoder) 69 70 int rkanxdp_match(struct device *, void *, void *); 71 void rkanxdp_attach(struct device *, struct device *, void *); 72 73 void rkanxdp_select_input(struct rkanxdp_softc *, u_int); 74 void rkanxdp_encoder_enable(struct drm_encoder *); 75 void rkanxdp_encoder_dpms(struct drm_encoder *, int); 76 77 int rkanxdp_ep_activate(void *, struct endpoint *, void *); 78 void *rkanxdp_ep_get_cookie(void *, struct endpoint *); 79 80 struct cfattach rkanxdp_ca = { 81 sizeof (struct rkanxdp_softc), rkanxdp_match, rkanxdp_attach 82 }; 83 84 struct cfdriver rkanxdp_cd = { 85 NULL, "rkanxdp", DV_DULL 86 }; 87 88 int 89 rkanxdp_match(struct device *parent, void *match, void *aux) 90 { 91 struct fdt_attach_args *faa = aux; 92 93 return OF_is_compatible(faa->fa_node, "rockchip,rk3399-edp"); 94 } 95 96 void 97 rkanxdp_attach(struct device *parent, struct device *self, void *aux) 98 { 99 struct rkanxdp_softc *sc = (struct rkanxdp_softc *)self; 100 struct fdt_attach_args *faa = aux; 101 uint32_t grf; 102 103 if (faa->fa_nreg < 1) { 104 printf(": no registers\n"); 105 return; 106 } 107 108 pinctrl_byname(faa->fa_node, "default"); 109 110 reset_deassert(faa->fa_node, "dp"); 111 112 clock_enable(faa->fa_node, "pclk"); 113 clock_enable(faa->fa_node, "dp"); 114 clock_enable(faa->fa_node, "grf"); 115 116 sc->sc_base.sc_iot = faa->fa_iot; 117 if (bus_space_map(sc->sc_base.sc_iot, faa->fa_reg[0].addr, 118 faa->fa_reg[0].size, 0, &sc->sc_base.sc_ioh)) { 119 printf(": can't map registers\n"); 120 return; 121 } 122 123 grf = OF_getpropint(faa->fa_node, "rockchip,grf", 0); 124 sc->sc_grf = regmap_byphandle(grf); 125 if (sc->sc_grf == NULL) { 126 printf(": can't get grf\n"); 127 return; 128 } 129 130 printf(": eDP TX\n"); 131 132 sc->sc_base.sc_flags |= ANXDP_FLAG_ROCKCHIP; 133 134 if (anxdp_attach(&sc->sc_base) != 0) { 135 printf("%s: failed to attach driver\n", 136 sc->sc_base.sc_dev.dv_xname); 137 return; 138 } 139 140 sc->sc_ports.dp_node = faa->fa_node; 141 sc->sc_ports.dp_cookie = sc; 142 sc->sc_ports.dp_ep_activate = rkanxdp_ep_activate; 143 sc->sc_ports.dp_ep_get_cookie = rkanxdp_ep_get_cookie; 144 device_ports_register(&sc->sc_ports, EP_DRM_ENCODER); 145 } 146 147 void 148 rkanxdp_select_input(struct rkanxdp_softc *sc, u_int crtc_index) 149 { 150 uint32_t write_mask = EDP_LCDC_SEL << 16; 151 uint32_t write_val = crtc_index == 0 ? EDP_LCDC_SEL : 0; 152 153 regmap_write_4(sc->sc_grf, RK3399_GRF_SOC_CON20, write_mask | write_val); 154 } 155 156 void 157 rkanxdp_encoder_enable(struct drm_encoder *encoder) 158 { 159 struct rkanxdp_softc *sc = to_rkanxdp_encoder(encoder); 160 u_int crtc_index = drm_crtc_index(encoder->crtc); 161 162 rkanxdp_select_input(sc, crtc_index); 163 } 164 165 void 166 rkanxdp_encoder_dpms(struct drm_encoder *encoder, int mode) 167 { 168 struct rkanxdp_softc *sc = to_rkanxdp_encoder(encoder); 169 170 anxdp_dpms(&sc->sc_base, mode); 171 } 172 173 struct drm_encoder_funcs rkanxdp_encoder_funcs = { 174 .destroy = drm_encoder_cleanup, 175 }; 176 177 struct drm_encoder_helper_funcs rkanxdp_encoder_helper_funcs = { 178 .enable = rkanxdp_encoder_enable, 179 .dpms = rkanxdp_encoder_dpms, 180 }; 181 182 int 183 rkanxdp_ep_activate(void *cookie, struct endpoint *ep, void *arg) 184 { 185 struct rkanxdp_softc *sc = cookie; 186 struct drm_crtc *crtc = NULL; 187 struct endpoint *rep; 188 int error; 189 190 if (sc->sc_activated) 191 return 0; 192 193 if (ep->ep_port->dp_reg != ANXDP_PORT_INPUT) 194 return EINVAL; 195 196 rep = endpoint_remote(ep); 197 if (rep && rep->ep_type == EP_DRM_CRTC) 198 crtc = endpoint_get_cookie(rep); 199 if (crtc == NULL) 200 return EINVAL; 201 202 sc->sc_encoder.possible_crtcs = 0x3; /* XXX */ 203 drm_encoder_init(crtc->dev, &sc->sc_encoder, &rkanxdp_encoder_funcs, 204 DRM_MODE_ENCODER_TMDS, NULL); 205 drm_encoder_helper_add(&sc->sc_encoder, &rkanxdp_encoder_helper_funcs); 206 207 ep = endpoint_byreg(&sc->sc_ports, ANXDP_PORT_OUTPUT, 0); 208 if (ep) { 209 rep = endpoint_remote(ep); 210 if (rep && rep->ep_type == EP_DRM_PANEL) 211 sc->sc_base.sc_panel = endpoint_get_cookie(rep); 212 } 213 214 sc->sc_base.sc_connector.base.connector_type = DRM_MODE_CONNECTOR_eDP; 215 error = anxdp_bind(&sc->sc_base, &sc->sc_encoder); 216 if (error != 0) 217 return error; 218 219 sc->sc_activated = 1; 220 return 0; 221 } 222 223 void * 224 rkanxdp_ep_get_cookie(void *cookie, struct endpoint *ep) 225 { 226 struct rkanxdp_softc *sc = cookie; 227 return &sc->sc_encoder; 228 } 229