1 /* $NetBSD: imx6_dwhdmi.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $ */ 2 /*- 3 * Copyright (c) 2020 Genetec Corporation. All rights reserved. 4 * Written by Hashimoto Kenichi for Genetec Corporation. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: imx6_dwhdmi.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $"); 30 31 #include <sys/param.h> 32 #include <sys/bus.h> 33 #include <sys/device.h> 34 #include <sys/intr.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/conf.h> 38 39 #include <drm/drmP.h> 40 #include <drm/drm_crtc_helper.h> 41 42 #include <dev/fdt/fdtvar.h> 43 #include <dev/fdt/fdt_port.h> 44 #include <dev/fdt/syscon.h> 45 46 #include <dev/ic/dw_hdmi.h> 47 48 static const struct dwhdmi_mpll_config imx6_dwhdmi_mpll_config[] = { 49 { 45250, 0x01e0, 0x0000, 0x091c }, 50 { 92500, 0x0072, 0x0001, 0x06dc }, 51 { 148500, 0x0051, 0x0002, 0x091c }, 52 { 216000, 0x00a0, 0x000a, 0x06dc }, 53 { 0, 0x0000, 0x0000, 0x0000 }, 54 }; 55 56 static const struct dwhdmi_phy_config imx6_dwhdmi_phy_config[] = { 57 { 216000, 0x800d, 0x000a, 0x01ad }, 58 { 0, 0x0000, 0x0000, 0x0000 } 59 }; 60 61 enum { 62 DWHDMI_PORT_INPUT = 0, 63 DWHDMI_PORT_OUTPUT = 1, 64 }; 65 66 static const struct device_compatible_entry compat_data[] = { 67 { .compat = "fsl,imx6dl-hdmi" }, 68 { .compat = "fsl,imx6q-hdmi" }, 69 DEVICE_COMPAT_EOL 70 }; 71 72 struct imx6_dwhdmi_softc { 73 struct dwhdmi_softc sc_base; 74 int sc_phandle; 75 76 struct fdt_device_ports sc_ports; 77 struct drm_display_mode sc_curmode; 78 struct drm_encoder sc_encoder; 79 80 bool sc_activated; 81 }; 82 83 #define to_imx6_dwhdmi_softc(x) container_of(x, struct imx6_dwhdmi_softc, sc_base) 84 #define to_imx6_dwhdmi_encoder(x) container_of(x, struct imx6_dwhdmi_softc, sc_encoder) 85 86 static bool 87 imx6_dwhdmi_encoder_mode_fixup(struct drm_encoder *encoder, 88 const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode) 89 { 90 return true; 91 } 92 93 static void 94 imx6_dwhdmi_encoder_mode_set(struct drm_encoder *encoder, 95 struct drm_display_mode *mode, struct drm_display_mode *adjusted) 96 { 97 } 98 99 static void 100 imx6_dwhdmi_encoder_enable(struct drm_encoder *encoder) 101 { 102 } 103 104 static void 105 imx6_dwhdmi_encoder_disable(struct drm_encoder *encoder) 106 { 107 } 108 109 static void 110 imx6_dwhdmi_encoder_prepare(struct drm_encoder *encoder) 111 { 112 } 113 114 static void 115 imx6_dwhdmi_encoder_commit(struct drm_encoder *encoder) 116 { 117 } 118 119 static const struct drm_encoder_funcs imx6_dwhdmi_encoder_funcs = { 120 .destroy = drm_encoder_cleanup, 121 }; 122 123 static const struct drm_encoder_helper_funcs imx6_dwhdmi_encoder_helper_funcs = { 124 .prepare = imx6_dwhdmi_encoder_prepare, 125 .mode_fixup = imx6_dwhdmi_encoder_mode_fixup, 126 .mode_set = imx6_dwhdmi_encoder_mode_set, 127 .enable = imx6_dwhdmi_encoder_enable, 128 .disable = imx6_dwhdmi_encoder_disable, 129 .commit = imx6_dwhdmi_encoder_commit, 130 }; 131 132 static int 133 imx6_dwhdmi_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate) 134 { 135 struct imx6_dwhdmi_softc * const sc = device_private(dev); 136 struct fdt_endpoint *in_ep = fdt_endpoint_remote(ep); 137 struct fdt_endpoint *out_ep, *out_rep; 138 struct drm_crtc *crtc; 139 int error; 140 141 if (sc->sc_activated != false) { 142 return 0; 143 } 144 145 if (!activate) 146 return EINVAL; 147 148 if (fdt_endpoint_port_index(ep) != DWHDMI_PORT_INPUT) 149 return EINVAL; 150 151 switch (fdt_endpoint_type(in_ep)) { 152 case EP_DRM_CRTC: 153 crtc = fdt_endpoint_get_data(in_ep); 154 break; 155 default: 156 crtc = NULL; 157 break; 158 } 159 160 if (crtc == NULL) 161 return EINVAL; 162 163 sc->sc_encoder.possible_crtcs = 3; // 1U << drm_crtc_index(crtc); /* XXX */ 164 drm_encoder_init(crtc->dev, &sc->sc_encoder, &imx6_dwhdmi_encoder_funcs, 165 DRM_MODE_ENCODER_TMDS); 166 drm_encoder_helper_add(&sc->sc_encoder, &imx6_dwhdmi_encoder_helper_funcs); 167 168 sc->sc_base.sc_connector.base.connector_type = DRM_MODE_CONNECTOR_HDMIA; 169 error = dwhdmi_bind(&sc->sc_base, &sc->sc_encoder); 170 if (error != 0) 171 return error; 172 sc->sc_activated = true; 173 174 out_ep = fdt_endpoint_get_from_index(&sc->sc_ports, DWHDMI_PORT_OUTPUT, 0); 175 if (out_ep != NULL) { 176 /* Ignore downstream connectors, we have our own. */ 177 out_rep = fdt_endpoint_remote(out_ep); 178 if (out_rep != NULL && fdt_endpoint_type(out_rep) == EP_DRM_CONNECTOR) 179 return 0; 180 181 error = fdt_endpoint_activate(out_ep, activate); 182 if (error != 0) 183 return error; 184 } 185 186 return 0; 187 } 188 189 static void * 190 imx6_dwhdmi_ep_get_data(device_t dev, struct fdt_endpoint *ep) 191 { 192 struct imx6_dwhdmi_softc * const sc = device_private(dev); 193 194 return &sc->sc_encoder; 195 } 196 197 static audio_dai_tag_t 198 imx6_dwhdmi_dai_get_tag(device_t dev, const void *data, size_t len) 199 { 200 struct imx6_dwhdmi_softc * const sc = device_private(dev); 201 202 if (len != 4) 203 return NULL; 204 205 return &sc->sc_base.sc_dai; 206 } 207 208 static struct fdtbus_dai_controller_func imx6_dwhdmi_dai_funcs = { 209 .get_tag = imx6_dwhdmi_dai_get_tag 210 }; 211 212 static int 213 imx6_dwhdmi_match(device_t parent, cfdata_t cf, void *aux) 214 { 215 struct fdt_attach_args * const faa = aux; 216 217 return of_compatible_match(faa->faa_phandle, compat_data); 218 } 219 220 static void 221 imx6_dwhdmi_attach(device_t parent, device_t self, void *aux) 222 { 223 struct imx6_dwhdmi_softc * const sc = device_private(self); 224 struct fdt_attach_args * const faa = aux; 225 const int phandle = faa->faa_phandle; 226 bus_addr_t addr; 227 bus_size_t size; 228 229 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 230 aprint_error(": couldn't get registers\n"); 231 return; 232 } 233 234 /* Required */ 235 if (fdtbus_clock_enable(phandle, "iahb", true) != 0) { 236 aprint_error(": couldn't enable iahb clock\n"); 237 return; 238 } 239 240 /* Required */ 241 if (fdtbus_clock_enable(phandle, "isfr", true) != 0) { 242 aprint_error(": couldn't enable isfr clock\n"); 243 return; 244 } 245 246 sc->sc_base.sc_dev = self; 247 sc->sc_base.sc_reg_width = 1; 248 sc->sc_base.sc_bst = faa->faa_bst; 249 if (bus_space_map(sc->sc_base.sc_bst, addr, size, 0, &sc->sc_base.sc_bsh) != 0) { 250 aprint_error(": couldn't map registers\n"); 251 return; 252 } 253 sc->sc_phandle = faa->faa_phandle; 254 255 aprint_naive("\n"); 256 aprint_normal(": HDMI TX\n"); 257 258 sc->sc_base.sc_ic = fdtbus_i2c_acquire(phandle, "ddc-i2c-bus"); 259 if (of_hasprop(phandle, "ddc-i2c-bus") && sc->sc_base.sc_ic == NULL) { 260 aprint_error_dev(self, "couldn't find external I2C master\n"); 261 return; 262 } 263 264 sc->sc_base.sc_flags |= DWHDMI_USE_INTERNAL_PHY; 265 sc->sc_base.sc_detect = dwhdmi_phy_detect; 266 sc->sc_base.sc_enable = dwhdmi_phy_enable; 267 sc->sc_base.sc_disable = dwhdmi_phy_disable; 268 sc->sc_base.sc_mode_set = dwhdmi_phy_mode_set; 269 sc->sc_base.sc_mpll_config = imx6_dwhdmi_mpll_config; 270 sc->sc_base.sc_phy_config = imx6_dwhdmi_phy_config; 271 272 if (dwhdmi_attach(&sc->sc_base) != 0) { 273 aprint_error_dev(self, "failed to attach driver\n"); 274 return; 275 } 276 277 sc->sc_ports.dp_ep_activate = imx6_dwhdmi_ep_activate; 278 sc->sc_ports.dp_ep_get_data = imx6_dwhdmi_ep_get_data; 279 fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_ENCODER); 280 281 fdtbus_register_dai_controller(self, phandle, &imx6_dwhdmi_dai_funcs); 282 } 283 284 CFATTACH_DECL_NEW(imx6_dwhdmi, sizeof(struct imx6_dwhdmi_softc), 285 imx6_dwhdmi_match, imx6_dwhdmi_attach, NULL, NULL); 286