1 /* $NetBSD: tegra_drm.c,v 1.11 2021/01/27 03:10:19 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca> 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: tegra_drm.c,v 1.11 2021/01/27 03:10:19 thorpej 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 <uvm/uvm_extern.h> 41 #include <uvm/uvm_device.h> 42 43 #include <drm/drmP.h> 44 45 #include <arm/nvidia/tegra_reg.h> 46 #include <arm/nvidia/tegra_var.h> 47 #include <arm/nvidia/tegra_drm.h> 48 49 #include <dev/fdt/fdtvar.h> 50 51 static int tegra_drm_match(device_t, cfdata_t, void *); 52 static void tegra_drm_attach(device_t, device_t, void *); 53 54 static int tegra_drm_set_busid(struct drm_device *, struct drm_master *); 55 56 static int tegra_drm_load(struct drm_device *, unsigned long); 57 static int tegra_drm_unload(struct drm_device *); 58 59 static struct drm_driver tegra_drm_driver = { 60 .driver_features = DRIVER_MODESET | DRIVER_GEM, 61 .dev_priv_size = 0, 62 .load = tegra_drm_load, 63 .unload = tegra_drm_unload, 64 65 .gem_free_object = drm_gem_cma_free_object, 66 .mmap_object = drm_gem_or_legacy_mmap_object, 67 .gem_uvm_ops = &drm_gem_cma_uvm_ops, 68 69 .dumb_create = drm_gem_cma_dumb_create, 70 .dumb_map_offset = drm_gem_cma_dumb_map_offset, 71 .dumb_destroy = drm_gem_dumb_destroy, 72 73 .get_vblank_counter = tegra_drm_get_vblank_counter, 74 .enable_vblank = tegra_drm_enable_vblank, 75 .disable_vblank = tegra_drm_disable_vblank, 76 77 .name = DRIVER_NAME, 78 .desc = DRIVER_DESC, 79 .date = DRIVER_DATE, 80 .major = DRIVER_MAJOR, 81 .minor = DRIVER_MINOR, 82 .patchlevel = DRIVER_PATCHLEVEL, 83 84 .set_busid = tegra_drm_set_busid, 85 }; 86 87 CFATTACH_DECL_NEW(tegra_drm, sizeof(struct tegra_drm_softc), 88 tegra_drm_match, tegra_drm_attach, NULL, NULL); 89 90 static const struct device_compatible_entry compat_data[] = { 91 { .compat = "nvidia,tegra124-host1x" }, 92 DEVICE_COMPAT_EOL 93 }; 94 95 static int 96 tegra_drm_match(device_t parent, cfdata_t cf, void *aux) 97 { 98 struct fdt_attach_args * const faa = aux; 99 100 return of_compatible_match(faa->faa_phandle, compat_data); 101 } 102 103 static const struct device_compatible_entry hdmi_compat[] = { 104 { .compat = "nvidia,tegra124-hdmi" }, 105 DEVICE_COMPAT_EOL 106 }; 107 108 static const struct device_compatible_entry dc_compat[] = { 109 { .compat = "nvidia,tegra124-dc" }, 110 DEVICE_COMPAT_EOL 111 }; 112 113 static void 114 tegra_drm_attach(device_t parent, device_t self, void *aux) 115 { 116 struct tegra_drm_softc * const sc = device_private(self); 117 struct fdt_attach_args * const faa = aux; 118 struct drm_driver * const driver = &tegra_drm_driver; 119 prop_dictionary_t prop = device_properties(self); 120 int error, node, hdmi_phandle, ddc_phandle; 121 static const char * const hdmi_supplies[] = { 122 "hdmi-supply", "pll-supply", "vdd-supply" 123 }; 124 struct fdtbus_regulator *reg; 125 u_int n, ndc; 126 127 sc->sc_dev = self; 128 sc->sc_dmat = faa->faa_dmat; 129 sc->sc_bst = faa->faa_bst; 130 sc->sc_phandle = faa->faa_phandle; 131 132 aprint_naive("\n"); 133 aprint_normal("\n"); 134 135 sc->sc_clk_host1x = fdtbus_clock_get_index(faa->faa_phandle, 0); 136 if (sc->sc_clk_host1x == NULL) { 137 aprint_error_dev(self, "couldn't get clock host1x\n"); 138 return; 139 } 140 sc->sc_rst_host1x = fdtbus_reset_get(faa->faa_phandle, "host1x"); 141 if (sc->sc_clk_host1x == NULL || sc->sc_rst_host1x == NULL) { 142 aprint_error_dev(self, "couldn't get reset host1x\n"); 143 return; 144 } 145 146 ndc = 0; 147 hdmi_phandle = -1; 148 for (node = OF_child(faa->faa_phandle); node; node = OF_peer(node)) { 149 if (of_compatible_match(node, hdmi_compat)) { 150 sc->sc_clk_hdmi = fdtbus_clock_get(node, "hdmi"); 151 sc->sc_clk_hdmi_parent = fdtbus_clock_get(node, 152 "parent"); 153 sc->sc_rst_hdmi = fdtbus_reset_get(node, "hdmi"); 154 hdmi_phandle = node; 155 } else if (of_compatible_match(node, dc_compat) && 156 ndc < __arraycount(sc->sc_clk_dc)) { 157 sc->sc_clk_dc[ndc] = fdtbus_clock_get(node, "dc"); 158 sc->sc_clk_dc_parent[ndc] = fdtbus_clock_get(node, 159 "parent"); 160 sc->sc_rst_dc[ndc] = fdtbus_reset_get(node, "dc"); 161 ++ndc; 162 } 163 } 164 if (hdmi_phandle >= 0) { 165 ddc_phandle = fdtbus_get_phandle(hdmi_phandle, 166 "nvidia,ddc-i2c-bus"); 167 if (ddc_phandle >= 0) { 168 sc->sc_ddc = fdtbus_get_i2c_tag(ddc_phandle); 169 } 170 171 sc->sc_pin_hpd = fdtbus_gpio_acquire(hdmi_phandle, 172 "nvidia,hpd-gpio", GPIO_PIN_INPUT); 173 174 for (n = 0; n < __arraycount(hdmi_supplies); n++) { 175 const char *supply = hdmi_supplies[n]; 176 reg = fdtbus_regulator_acquire(hdmi_phandle, supply); 177 if (reg == NULL) { 178 aprint_error_dev(self, "couldn't acquire %s\n", 179 supply); 180 continue; 181 } 182 if (fdtbus_regulator_enable(reg) != 0) { 183 aprint_error_dev(self, "couldn't enable %s\n", 184 supply); 185 } 186 fdtbus_regulator_release(reg); 187 } 188 } 189 190 fdtbus_reset_assert(sc->sc_rst_host1x); 191 error = clk_enable(sc->sc_clk_host1x); 192 if (error) { 193 aprint_error_dev(self, "couldn't enable clock host1x: %d\n", 194 error); 195 return; 196 } 197 fdtbus_reset_deassert(sc->sc_rst_host1x); 198 199 prop_dictionary_get_bool(prop, "force-dvi", &sc->sc_force_dvi); 200 201 sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev); 202 if (sc->sc_ddev == NULL) { 203 aprint_error_dev(self, "couldn't allocate DRM device\n"); 204 return; 205 } 206 sc->sc_ddev->dev_private = sc; 207 sc->sc_ddev->bst = sc->sc_bst; 208 sc->sc_ddev->bus_dmat = sc->sc_dmat; 209 sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat; 210 sc->sc_ddev->dmat_subregion_p = false; 211 212 error = -drm_dev_register(sc->sc_ddev, 0); 213 if (error) { 214 drm_dev_unref(sc->sc_ddev); 215 aprint_error_dev(self, "couldn't register DRM device: %d\n", 216 error); 217 return; 218 } 219 220 aprint_normal_dev(self, "initialized %s %d.%d.%d %s on minor %d\n", 221 driver->name, driver->major, driver->minor, driver->patchlevel, 222 driver->date, sc->sc_ddev->primary->index); 223 224 return; 225 } 226 227 static int 228 tegra_drm_set_busid(struct drm_device *ddev, struct drm_master *master) 229 { 230 const char *id = "platform:tegra:0"; 231 232 master->unique = kzalloc(strlen(id) + 1, GFP_KERNEL); 233 if (master->unique == NULL) 234 return -ENOMEM; 235 strcpy(master->unique, id); 236 master->unique_len = strlen(master->unique); 237 238 return 0; 239 } 240 241 242 static int 243 tegra_drm_load(struct drm_device *ddev, unsigned long flags) 244 { 245 int error; 246 247 error = tegra_drm_mode_init(ddev); 248 if (error) 249 goto drmerr; 250 251 error = tegra_drm_fb_init(ddev); 252 if (error) 253 goto drmerr; 254 255 return 0; 256 257 drmerr: 258 drm_mode_config_cleanup(ddev); 259 260 return error; 261 } 262 263 static int 264 tegra_drm_unload(struct drm_device *ddev) 265 { 266 drm_mode_config_cleanup(ddev); 267 268 return 0; 269 } 270