1 /* 2 * Copyright (C) 2016 BayLibre, SAS 3 * Author: Neil Armstrong <narmstrong@baylibre.com> 4 * Copyright (C) 2014 Endless Mobile 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of the 9 * License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see <http://www.gnu.org/licenses/>. 18 * 19 * Written by: 20 * Jasper St. Pierre <jstpierre@mecheye.net> 21 */ 22 23 #include <linux/kernel.h> 24 #include <linux/module.h> 25 #include <linux/mutex.h> 26 #include <linux/platform_device.h> 27 #include <linux/component.h> 28 #include <linux/of_graph.h> 29 30 #include <drm/drmP.h> 31 #include <drm/drm_atomic.h> 32 #include <drm/drm_atomic_helper.h> 33 #include <drm/drm_fb_cma_helper.h> 34 #include <drm/drm_fb_helper.h> 35 #include <drm/drm_flip_work.h> 36 #include <drm/drm_gem_cma_helper.h> 37 #include <drm/drm_gem_framebuffer_helper.h> 38 #include <drm/drm_plane_helper.h> 39 #include <drm/drm_probe_helper.h> 40 #include <drm/drm_rect.h> 41 42 #include "meson_drv.h" 43 #include "meson_plane.h" 44 #include "meson_overlay.h" 45 #include "meson_crtc.h" 46 #include "meson_venc_cvbs.h" 47 48 #include "meson_vpp.h" 49 #include "meson_viu.h" 50 #include "meson_venc.h" 51 #include "meson_registers.h" 52 53 #define DRIVER_NAME "meson" 54 #define DRIVER_DESC "Amlogic Meson DRM driver" 55 56 /** 57 * DOC: Video Processing Unit 58 * 59 * VPU Handles the Global Video Processing, it includes management of the 60 * clocks gates, blocks reset lines and power domains. 61 * 62 * What is missing : 63 * 64 * - Full reset of entire video processing HW blocks 65 * - Scaling and setup of the VPU clock 66 * - Bus clock gates 67 * - Powering up video processing HW blocks 68 * - Powering Up HDMI controller and PHY 69 */ 70 71 static const struct drm_mode_config_funcs meson_mode_config_funcs = { 72 .atomic_check = drm_atomic_helper_check, 73 .atomic_commit = drm_atomic_helper_commit, 74 .fb_create = drm_gem_fb_create, 75 }; 76 77 static const struct drm_mode_config_helper_funcs meson_mode_config_helpers = { 78 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm, 79 }; 80 81 static irqreturn_t meson_irq(int irq, void *arg) 82 { 83 struct drm_device *dev = arg; 84 struct meson_drm *priv = dev->dev_private; 85 86 (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG)); 87 88 meson_crtc_irq(priv); 89 90 return IRQ_HANDLED; 91 } 92 93 DEFINE_DRM_GEM_CMA_FOPS(fops); 94 95 static struct drm_driver meson_driver = { 96 .driver_features = DRIVER_GEM | 97 DRIVER_MODESET | DRIVER_PRIME | 98 DRIVER_ATOMIC, 99 100 /* IRQ */ 101 .irq_handler = meson_irq, 102 103 /* PRIME Ops */ 104 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 105 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 106 .gem_prime_import = drm_gem_prime_import, 107 .gem_prime_export = drm_gem_prime_export, 108 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, 109 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, 110 .gem_prime_vmap = drm_gem_cma_prime_vmap, 111 .gem_prime_vunmap = drm_gem_cma_prime_vunmap, 112 .gem_prime_mmap = drm_gem_cma_prime_mmap, 113 114 /* GEM Ops */ 115 .dumb_create = drm_gem_cma_dumb_create, 116 .gem_free_object_unlocked = drm_gem_cma_free_object, 117 .gem_vm_ops = &drm_gem_cma_vm_ops, 118 119 /* Misc */ 120 .fops = &fops, 121 .name = DRIVER_NAME, 122 .desc = DRIVER_DESC, 123 .date = "20161109", 124 .major = 1, 125 .minor = 0, 126 }; 127 128 static bool meson_vpu_has_available_connectors(struct device *dev) 129 { 130 struct device_node *ep, *remote; 131 132 /* Parses each endpoint and check if remote exists */ 133 for_each_endpoint_of_node(dev->of_node, ep) { 134 /* If the endpoint node exists, consider it enabled */ 135 remote = of_graph_get_remote_port(ep); 136 if (remote) 137 return true; 138 } 139 140 return false; 141 } 142 143 static struct regmap_config meson_regmap_config = { 144 .reg_bits = 32, 145 .val_bits = 32, 146 .reg_stride = 4, 147 .max_register = 0x1000, 148 }; 149 150 static void meson_vpu_init(struct meson_drm *priv) 151 { 152 writel_relaxed(0x210000, priv->io_base + _REG(VPU_RDARB_MODE_L1C1)); 153 writel_relaxed(0x10000, priv->io_base + _REG(VPU_RDARB_MODE_L1C2)); 154 writel_relaxed(0x900000, priv->io_base + _REG(VPU_RDARB_MODE_L2C1)); 155 writel_relaxed(0x20000, priv->io_base + _REG(VPU_WRARB_MODE_L2C1)); 156 } 157 158 static void meson_remove_framebuffers(void) 159 { 160 struct apertures_struct *ap; 161 162 ap = alloc_apertures(1); 163 if (!ap) 164 return; 165 166 /* The framebuffer can be located anywhere in RAM */ 167 ap->ranges[0].base = 0; 168 ap->ranges[0].size = ~0; 169 170 drm_fb_helper_remove_conflicting_framebuffers(ap, "meson-drm-fb", 171 false); 172 kfree(ap); 173 } 174 175 static int meson_drv_bind_master(struct device *dev, bool has_components) 176 { 177 struct platform_device *pdev = to_platform_device(dev); 178 struct meson_drm *priv; 179 struct drm_device *drm; 180 struct resource *res; 181 void __iomem *regs; 182 int ret; 183 184 /* Checks if an output connector is available */ 185 if (!meson_vpu_has_available_connectors(dev)) { 186 dev_err(dev, "No output connector available\n"); 187 return -ENODEV; 188 } 189 190 drm = drm_dev_alloc(&meson_driver, dev); 191 if (IS_ERR(drm)) 192 return PTR_ERR(drm); 193 194 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 195 if (!priv) { 196 ret = -ENOMEM; 197 goto free_drm; 198 } 199 drm->dev_private = priv; 200 priv->drm = drm; 201 priv->dev = dev; 202 203 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vpu"); 204 regs = devm_ioremap_resource(dev, res); 205 if (IS_ERR(regs)) { 206 ret = PTR_ERR(regs); 207 goto free_drm; 208 } 209 210 priv->io_base = regs; 211 212 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi"); 213 if (!res) { 214 ret = -EINVAL; 215 goto free_drm; 216 } 217 /* Simply ioremap since it may be a shared register zone */ 218 regs = devm_ioremap(dev, res->start, resource_size(res)); 219 if (!regs) { 220 ret = -EADDRNOTAVAIL; 221 goto free_drm; 222 } 223 224 priv->hhi = devm_regmap_init_mmio(dev, regs, 225 &meson_regmap_config); 226 if (IS_ERR(priv->hhi)) { 227 dev_err(&pdev->dev, "Couldn't create the HHI regmap\n"); 228 ret = PTR_ERR(priv->hhi); 229 goto free_drm; 230 } 231 232 priv->canvas = meson_canvas_get(dev); 233 if (IS_ERR(priv->canvas)) { 234 ret = PTR_ERR(priv->canvas); 235 goto free_drm; 236 } 237 238 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_osd1); 239 if (ret) 240 goto free_drm; 241 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_0); 242 if (ret) { 243 meson_canvas_free(priv->canvas, priv->canvas_id_osd1); 244 goto free_drm; 245 } 246 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_1); 247 if (ret) { 248 meson_canvas_free(priv->canvas, priv->canvas_id_osd1); 249 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_0); 250 goto free_drm; 251 } 252 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_2); 253 if (ret) { 254 meson_canvas_free(priv->canvas, priv->canvas_id_osd1); 255 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_0); 256 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_1); 257 goto free_drm; 258 } 259 260 priv->vsync_irq = platform_get_irq(pdev, 0); 261 262 ret = drm_vblank_init(drm, 1); 263 if (ret) 264 goto free_drm; 265 266 /* Remove early framebuffers (ie. simplefb) */ 267 meson_remove_framebuffers(); 268 269 drm_mode_config_init(drm); 270 drm->mode_config.max_width = 3840; 271 drm->mode_config.max_height = 2160; 272 drm->mode_config.funcs = &meson_mode_config_funcs; 273 drm->mode_config.helper_private = &meson_mode_config_helpers; 274 275 /* Hardware Initialization */ 276 277 meson_vpu_init(priv); 278 meson_venc_init(priv); 279 meson_vpp_init(priv); 280 meson_viu_init(priv); 281 282 /* Encoder Initialization */ 283 284 ret = meson_venc_cvbs_create(priv); 285 if (ret) 286 goto free_drm; 287 288 if (has_components) { 289 ret = component_bind_all(drm->dev, drm); 290 if (ret) { 291 dev_err(drm->dev, "Couldn't bind all components\n"); 292 goto free_drm; 293 } 294 } 295 296 ret = meson_plane_create(priv); 297 if (ret) 298 goto free_drm; 299 300 ret = meson_overlay_create(priv); 301 if (ret) 302 goto free_drm; 303 304 ret = meson_crtc_create(priv); 305 if (ret) 306 goto free_drm; 307 308 ret = drm_irq_install(drm, priv->vsync_irq); 309 if (ret) 310 goto free_drm; 311 312 drm_mode_config_reset(drm); 313 314 drm_kms_helper_poll_init(drm); 315 316 platform_set_drvdata(pdev, priv); 317 318 ret = drm_dev_register(drm, 0); 319 if (ret) 320 goto free_drm; 321 322 drm_fbdev_generic_setup(drm, 32); 323 324 return 0; 325 326 free_drm: 327 drm_dev_put(drm); 328 329 return ret; 330 } 331 332 static int meson_drv_bind(struct device *dev) 333 { 334 return meson_drv_bind_master(dev, true); 335 } 336 337 static void meson_drv_unbind(struct device *dev) 338 { 339 struct drm_device *drm = dev_get_drvdata(dev); 340 struct meson_drm *priv = drm->dev_private; 341 342 if (priv->canvas) { 343 meson_canvas_free(priv->canvas, priv->canvas_id_osd1); 344 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_0); 345 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_1); 346 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_2); 347 } 348 349 drm_dev_unregister(drm); 350 drm_kms_helper_poll_fini(drm); 351 drm_mode_config_cleanup(drm); 352 drm_dev_put(drm); 353 354 } 355 356 static const struct component_master_ops meson_drv_master_ops = { 357 .bind = meson_drv_bind, 358 .unbind = meson_drv_unbind, 359 }; 360 361 static int compare_of(struct device *dev, void *data) 362 { 363 DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n", 364 dev->of_node, data); 365 366 return dev->of_node == data; 367 } 368 369 /* Possible connectors nodes to ignore */ 370 static const struct of_device_id connectors_match[] = { 371 { .compatible = "composite-video-connector" }, 372 { .compatible = "svideo-connector" }, 373 { .compatible = "hdmi-connector" }, 374 { .compatible = "dvi-connector" }, 375 {} 376 }; 377 378 static int meson_probe_remote(struct platform_device *pdev, 379 struct component_match **match, 380 struct device_node *parent, 381 struct device_node *remote) 382 { 383 struct device_node *ep, *remote_node; 384 int count = 1; 385 386 /* If node is a connector, return and do not add to match table */ 387 if (of_match_node(connectors_match, remote)) 388 return 1; 389 390 component_match_add(&pdev->dev, match, compare_of, remote); 391 392 for_each_endpoint_of_node(remote, ep) { 393 remote_node = of_graph_get_remote_port_parent(ep); 394 if (!remote_node || 395 remote_node == parent || /* Ignore parent endpoint */ 396 !of_device_is_available(remote_node)) { 397 of_node_put(remote_node); 398 continue; 399 } 400 401 count += meson_probe_remote(pdev, match, remote, remote_node); 402 403 of_node_put(remote_node); 404 } 405 406 return count; 407 } 408 409 static int meson_drv_probe(struct platform_device *pdev) 410 { 411 struct component_match *match = NULL; 412 struct device_node *np = pdev->dev.of_node; 413 struct device_node *ep, *remote; 414 int count = 0; 415 416 for_each_endpoint_of_node(np, ep) { 417 remote = of_graph_get_remote_port_parent(ep); 418 if (!remote || !of_device_is_available(remote)) { 419 of_node_put(remote); 420 continue; 421 } 422 423 count += meson_probe_remote(pdev, &match, np, remote); 424 of_node_put(remote); 425 } 426 427 if (count && !match) 428 return meson_drv_bind_master(&pdev->dev, false); 429 430 /* If some endpoints were found, initialize the nodes */ 431 if (count) { 432 dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count); 433 434 return component_master_add_with_match(&pdev->dev, 435 &meson_drv_master_ops, 436 match); 437 } 438 439 /* If no output endpoints were available, simply bail out */ 440 return 0; 441 }; 442 443 static const struct of_device_id dt_match[] = { 444 { .compatible = "amlogic,meson-gxbb-vpu" }, 445 { .compatible = "amlogic,meson-gxl-vpu" }, 446 { .compatible = "amlogic,meson-gxm-vpu" }, 447 { .compatible = "amlogic,meson-g12a-vpu" }, 448 {} 449 }; 450 MODULE_DEVICE_TABLE(of, dt_match); 451 452 static struct platform_driver meson_drm_platform_driver = { 453 .probe = meson_drv_probe, 454 .driver = { 455 .name = "meson-drm", 456 .of_match_table = dt_match, 457 }, 458 }; 459 460 module_platform_driver(meson_drm_platform_driver); 461 462 MODULE_AUTHOR("Jasper St. Pierre <jstpierre@mecheye.net>"); 463 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>"); 464 MODULE_DESCRIPTION(DRIVER_DESC); 465 MODULE_LICENSE("GPL"); 466