rk_anxdp.c revision 1.1
1/* $NetBSD: rk_anxdp.c,v 1.1 2019/12/19 00:25:59 jakllsch Exp $ */ 2 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/cdefs.h> 30__KERNEL_RCSID(0, "$NetBSD: rk_anxdp.c,v 1.1 2019/12/19 00:25:59 jakllsch Exp $"); 31 32#include <sys/param.h> 33#include <sys/bus.h> 34#include <sys/device.h> 35#include <sys/intr.h> 36#include <sys/systm.h> 37#include <sys/kernel.h> 38#include <sys/conf.h> 39 40#include <drm/drmP.h> 41#include <drm/drm_crtc_helper.h> 42 43#include <dev/fdt/fdtvar.h> 44#include <dev/fdt/fdt_port.h> 45#include <dev/fdt/syscon.h> 46 47#include <dev/ic/anx_dp.h> 48 49#define RK3399_GRF_SOC_CON20 0x6250 50#define EDP_LCDC_SEL __BIT(5) 51 52enum { 53 ANXDP_PORT_INPUT = 0, 54 ANXDP_PORT_OUTPUT = 1, 55}; 56 57static const char * const compatible[] = { 58 "rockchip,rk3399-edp", 59 NULL 60}; 61 62struct rk_anxdp_softc { 63 struct anxdp_softc sc_base; 64 int sc_phandle; 65 66 struct fdt_device_ports sc_ports; 67 struct drm_encoder sc_encoder; 68 struct drm_display_mode sc_curmode; 69 struct syscon *sc_grf; 70 71 bool sc_activated; 72}; 73 74#define to_rk_anxdp_softc(x) container_of(x, struct rk_anxdp_softc, sc_base) 75#define to_rk_anxdp_encoder(x) container_of(x, struct rk_anxdp_softc, sc_encoder) 76 77static void 78rk_anxdp_select_input(struct rk_anxdp_softc *sc, u_int crtc_index) 79{ 80 const uint32_t write_mask = EDP_LCDC_SEL << 16; 81 const uint32_t write_val = crtc_index == 0 ? EDP_LCDC_SEL : 0; 82 83 syscon_lock(sc->sc_grf); 84 syscon_write_4(sc->sc_grf, RK3399_GRF_SOC_CON20, write_mask | write_val); 85 syscon_unlock(sc->sc_grf); 86} 87 88static bool 89rk_anxdp_encoder_mode_fixup(struct drm_encoder *encoder, 90 const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode) 91{ 92 return true; 93} 94 95static void 96rk_anxdp_encoder_mode_set(struct drm_encoder *encoder, 97 struct drm_display_mode *mode, struct drm_display_mode *adjusted) 98{ 99} 100 101static void 102rk_anxdp_encoder_enable(struct drm_encoder *encoder) 103{ 104} 105 106static void 107rk_anxdp_encoder_disable(struct drm_encoder *encoder) 108{ 109} 110 111static void 112rk_anxdp_encoder_prepare(struct drm_encoder *encoder) 113{ 114 struct rk_anxdp_softc * const sc = to_rk_anxdp_encoder(encoder); 115 const u_int crtc_index = drm_crtc_index(encoder->crtc); 116 117 rk_anxdp_select_input(sc, crtc_index); 118} 119 120static void 121rk_anxdp_encoder_commit(struct drm_encoder *encoder) 122{ 123} 124 125static const struct drm_encoder_funcs rk_anxdp_encoder_funcs = { 126 .destroy = drm_encoder_cleanup, 127}; 128 129static const struct drm_encoder_helper_funcs rk_anxdp_encoder_helper_funcs = { 130 .prepare = rk_anxdp_encoder_prepare, 131 .mode_fixup = rk_anxdp_encoder_mode_fixup, 132 .mode_set = rk_anxdp_encoder_mode_set, 133 .enable = rk_anxdp_encoder_enable, 134 .disable = rk_anxdp_encoder_disable, 135 .commit = rk_anxdp_encoder_commit, 136}; 137 138static int 139rk_anxdp_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate) 140{ 141 struct rk_anxdp_softc * const sc = device_private(dev); 142 struct fdt_endpoint *in_ep = fdt_endpoint_remote(ep); 143 struct fdt_endpoint *out_ep, *out_rep; 144 struct drm_crtc *crtc; 145 int error; 146 147 if (sc->sc_activated != false) { 148 return 0; 149 } 150 151 if (!activate) 152 return EINVAL; 153 154 if (fdt_endpoint_port_index(ep) != ANXDP_PORT_INPUT) 155 return EINVAL; 156 157 switch (fdt_endpoint_type(in_ep)) { 158 case EP_DRM_CRTC: 159 crtc = fdt_endpoint_get_data(in_ep); 160 break; 161 default: 162 return EINVAL; 163 break; 164 } 165 166 sc->sc_encoder.possible_crtcs = 0x3; /* XXX */ 167 drm_encoder_init(crtc->dev, &sc->sc_encoder, &rk_anxdp_encoder_funcs, 168 DRM_MODE_ENCODER_TMDS); 169 drm_encoder_helper_add(&sc->sc_encoder, &rk_anxdp_encoder_helper_funcs); 170 171 out_ep = fdt_endpoint_get_from_index(&sc->sc_ports, ANXDP_PORT_OUTPUT, 0); 172 if (out_ep != NULL) { 173 out_rep = fdt_endpoint_remote(out_ep); 174 if (out_rep != NULL && fdt_endpoint_type(out_rep) == EP_DRM_PANEL) 175 sc->sc_base.sc_panel = fdt_endpoint_get_data(out_rep); 176 } 177 178 sc->sc_base.sc_connector.base.connector_type = DRM_MODE_CONNECTOR_eDP; 179 error = anxdp_bind(&sc->sc_base, &sc->sc_encoder); 180 if (error != 0) 181 return error; 182 sc->sc_activated = true; 183 184 if (out_ep != NULL) { 185 /* Ignore downstream connectors, we have our own. */ 186 if (out_rep != NULL && fdt_endpoint_type(out_rep) == EP_DRM_CONNECTOR) 187 return 0; 188 error = fdt_endpoint_activate(out_ep, activate); 189 if (error != 0) 190 return error; 191 } 192 193 return 0; 194} 195 196static void * 197rk_anxdp_ep_get_data(device_t dev, struct fdt_endpoint *ep) 198{ 199 struct rk_anxdp_softc * const sc = device_private(dev); 200 201 return &sc->sc_encoder; 202} 203 204#if ANXDP_AUDIO 205static audio_dai_tag_t 206rk_anxdp_dai_get_tag(device_t dev, const void *data, size_t len) 207{ 208 struct rk_anxdp_softc * const sc = device_private(dev); 209 210 if (len != 4) 211 return NULL; 212 213 return &sc->sc_base.sc_dai; 214} 215 216static struct fdtbus_dai_controller_func rk_anxdp_dai_funcs = { 217 .get_tag = rk_anxdp_dai_get_tag 218}; 219#endif 220 221static int 222rk_anxdp_match(device_t parent, cfdata_t cf, void *aux) 223{ 224 struct fdt_attach_args * const faa = aux; 225 226 return of_match_compatible(faa->faa_phandle, compatible); 227} 228 229static void 230rk_anxdp_attach(device_t parent, device_t self, void *aux) 231{ 232 struct rk_anxdp_softc * const sc = device_private(self); 233 struct fdt_attach_args * const faa = aux; 234 const int phandle = faa->faa_phandle; 235 bus_addr_t addr; 236 bus_size_t size; 237 238 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 239 aprint_error(": couldn't get registers\n"); 240 return; 241 } 242 243 /* Required */ 244 if (fdtbus_clock_enable(phandle, "pclk", true) != 0) { 245 aprint_error(": couldn't enable pclk clock\n"); 246 return; 247 } 248 249 /* Required */ 250 if (fdtbus_clock_enable(phandle, "dp", true) != 0) { 251 aprint_error(": couldn't enable dp clock\n"); 252 return; 253 } 254 255 /* Optional */ 256 if (fdtbus_clock_enable(phandle, "grf", false) != 0) { 257 aprint_error(": couldn't enable grf clock\n"); 258 return; 259 } 260 261 /* TODO: Optional phy */ 262 263 sc->sc_base.sc_dev = self; 264 sc->sc_base.sc_bst = faa->faa_bst; 265 if (bus_space_map(sc->sc_base.sc_bst, addr, size, 0, &sc->sc_base.sc_bsh) != 0) { 266 aprint_error(": couldn't map registers\n"); 267 return; 268 } 269 sc->sc_phandle = faa->faa_phandle; 270 sc->sc_grf = fdtbus_syscon_acquire(phandle, "rockchip,grf"); 271 if (sc->sc_grf == NULL) { 272 aprint_error(": couldn't get grf syscon\n"); 273 return; 274 } 275 276 aprint_naive("\n"); 277 aprint_normal(": eDP TX\n"); 278 279 sc->sc_base.sc_flags |= ANXDP_FLAG_ROCKCHIP; 280 281 if (anxdp_attach(&sc->sc_base) != 0) { 282 aprint_error_dev(self, "failed to attach driver\n"); 283 return; 284 } 285 286 sc->sc_ports.dp_ep_activate = rk_anxdp_ep_activate; 287 sc->sc_ports.dp_ep_get_data = rk_anxdp_ep_get_data; 288 fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_ENCODER); 289 290#if ANXDP_AUDIO 291 fdtbus_register_dai_controller(self, phandle, &rk_anxdp_dai_funcs); 292#endif 293} 294 295CFATTACH_DECL_NEW(rk_anxdp, sizeof(struct rk_anxdp_softc), 296 rk_anxdp_match, rk_anxdp_attach, NULL, NULL); 297