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_flip_work.h> 34 #include <drm/drm_crtc_helper.h> 35 #include <drm/drm_plane_helper.h> 36 #include <drm/drm_gem_cma_helper.h> 37 #include <drm/drm_gem_framebuffer_helper.h> 38 #include <drm/drm_fb_cma_helper.h> 39 #include <drm/drm_rect.h> 40 #include <drm/drm_fb_helper.h> 41 42 #include "meson_drv.h" 43 #include "meson_plane.h" 44 #include "meson_crtc.h" 45 #include "meson_venc_cvbs.h" 46 47 #include "meson_vpp.h" 48 #include "meson_viu.h" 49 #include "meson_venc.h" 50 #include "meson_canvas.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 irqreturn_t meson_irq(int irq, void *arg) 78 { 79 struct drm_device *dev = arg; 80 struct meson_drm *priv = dev->dev_private; 81 82 (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG)); 83 84 meson_crtc_irq(priv); 85 86 return IRQ_HANDLED; 87 } 88 89 DEFINE_DRM_GEM_CMA_FOPS(fops); 90 91 static struct drm_driver meson_driver = { 92 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | 93 DRIVER_MODESET | DRIVER_PRIME | 94 DRIVER_ATOMIC, 95 96 /* IRQ */ 97 .irq_handler = meson_irq, 98 99 /* PRIME Ops */ 100 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 101 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 102 .gem_prime_import = drm_gem_prime_import, 103 .gem_prime_export = drm_gem_prime_export, 104 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, 105 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, 106 .gem_prime_vmap = drm_gem_cma_prime_vmap, 107 .gem_prime_vunmap = drm_gem_cma_prime_vunmap, 108 .gem_prime_mmap = drm_gem_cma_prime_mmap, 109 110 /* GEM Ops */ 111 .dumb_create = drm_gem_cma_dumb_create, 112 .gem_free_object_unlocked = drm_gem_cma_free_object, 113 .gem_vm_ops = &drm_gem_cma_vm_ops, 114 115 /* Misc */ 116 .fops = &fops, 117 .name = DRIVER_NAME, 118 .desc = DRIVER_DESC, 119 .date = "20161109", 120 .major = 1, 121 .minor = 0, 122 }; 123 124 static bool meson_vpu_has_available_connectors(struct device *dev) 125 { 126 struct device_node *ep, *remote; 127 128 /* Parses each endpoint and check if remote exists */ 129 for_each_endpoint_of_node(dev->of_node, ep) { 130 /* If the endpoint node exists, consider it enabled */ 131 remote = of_graph_get_remote_port(ep); 132 if (remote) 133 return true; 134 } 135 136 return false; 137 } 138 139 static struct regmap_config meson_regmap_config = { 140 .reg_bits = 32, 141 .val_bits = 32, 142 .reg_stride = 4, 143 .max_register = 0x1000, 144 }; 145 146 static void meson_vpu_init(struct meson_drm *priv) 147 { 148 writel_relaxed(0x210000, priv->io_base + _REG(VPU_RDARB_MODE_L1C1)); 149 writel_relaxed(0x10000, priv->io_base + _REG(VPU_RDARB_MODE_L1C2)); 150 writel_relaxed(0x900000, priv->io_base + _REG(VPU_RDARB_MODE_L2C1)); 151 writel_relaxed(0x20000, priv->io_base + _REG(VPU_WRARB_MODE_L2C1)); 152 } 153 154 static int meson_drv_bind_master(struct device *dev, bool has_components) 155 { 156 struct platform_device *pdev = to_platform_device(dev); 157 struct meson_drm *priv; 158 struct drm_device *drm; 159 struct resource *res; 160 void __iomem *regs; 161 int ret; 162 163 /* Checks if an output connector is available */ 164 if (!meson_vpu_has_available_connectors(dev)) { 165 dev_err(dev, "No output connector available\n"); 166 return -ENODEV; 167 } 168 169 drm = drm_dev_alloc(&meson_driver, dev); 170 if (IS_ERR(drm)) 171 return PTR_ERR(drm); 172 173 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 174 if (!priv) { 175 ret = -ENOMEM; 176 goto free_drm; 177 } 178 drm->dev_private = priv; 179 priv->drm = drm; 180 priv->dev = dev; 181 182 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vpu"); 183 regs = devm_ioremap_resource(dev, res); 184 if (IS_ERR(regs)) { 185 ret = PTR_ERR(regs); 186 goto free_drm; 187 } 188 189 priv->io_base = regs; 190 191 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi"); 192 if (!res) { 193 ret = -EINVAL; 194 goto free_drm; 195 } 196 /* Simply ioremap since it may be a shared register zone */ 197 regs = devm_ioremap(dev, res->start, resource_size(res)); 198 if (!regs) { 199 ret = -EADDRNOTAVAIL; 200 goto free_drm; 201 } 202 203 priv->hhi = devm_regmap_init_mmio(dev, regs, 204 &meson_regmap_config); 205 if (IS_ERR(priv->hhi)) { 206 dev_err(&pdev->dev, "Couldn't create the HHI regmap\n"); 207 ret = PTR_ERR(priv->hhi); 208 goto free_drm; 209 } 210 211 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmc"); 212 if (!res) { 213 ret = -EINVAL; 214 goto free_drm; 215 } 216 /* Simply ioremap since it may be a shared register zone */ 217 regs = devm_ioremap(dev, res->start, resource_size(res)); 218 if (!regs) { 219 ret = -EADDRNOTAVAIL; 220 goto free_drm; 221 } 222 223 priv->dmc = devm_regmap_init_mmio(dev, regs, 224 &meson_regmap_config); 225 if (IS_ERR(priv->dmc)) { 226 dev_err(&pdev->dev, "Couldn't create the DMC regmap\n"); 227 ret = PTR_ERR(priv->dmc); 228 goto free_drm; 229 } 230 231 priv->vsync_irq = platform_get_irq(pdev, 0); 232 233 ret = drm_vblank_init(drm, 1); 234 if (ret) 235 goto free_drm; 236 237 drm_mode_config_init(drm); 238 drm->mode_config.max_width = 3840; 239 drm->mode_config.max_height = 2160; 240 drm->mode_config.funcs = &meson_mode_config_funcs; 241 242 /* Hardware Initialization */ 243 244 meson_vpu_init(priv); 245 meson_venc_init(priv); 246 meson_vpp_init(priv); 247 meson_viu_init(priv); 248 249 /* Encoder Initialization */ 250 251 ret = meson_venc_cvbs_create(priv); 252 if (ret) 253 goto free_drm; 254 255 if (has_components) { 256 ret = component_bind_all(drm->dev, drm); 257 if (ret) { 258 dev_err(drm->dev, "Couldn't bind all components\n"); 259 goto free_drm; 260 } 261 } 262 263 ret = meson_plane_create(priv); 264 if (ret) 265 goto free_drm; 266 267 ret = meson_crtc_create(priv); 268 if (ret) 269 goto free_drm; 270 271 ret = drm_irq_install(drm, priv->vsync_irq); 272 if (ret) 273 goto free_drm; 274 275 drm_mode_config_reset(drm); 276 277 drm_kms_helper_poll_init(drm); 278 279 platform_set_drvdata(pdev, priv); 280 281 ret = drm_dev_register(drm, 0); 282 if (ret) 283 goto free_drm; 284 285 drm_fbdev_generic_setup(drm, 32); 286 287 return 0; 288 289 free_drm: 290 drm_dev_put(drm); 291 292 return ret; 293 } 294 295 static int meson_drv_bind(struct device *dev) 296 { 297 return meson_drv_bind_master(dev, true); 298 } 299 300 static void meson_drv_unbind(struct device *dev) 301 { 302 struct drm_device *drm = dev_get_drvdata(dev); 303 304 drm_dev_unregister(drm); 305 drm_kms_helper_poll_fini(drm); 306 drm_mode_config_cleanup(drm); 307 drm_dev_put(drm); 308 309 } 310 311 static const struct component_master_ops meson_drv_master_ops = { 312 .bind = meson_drv_bind, 313 .unbind = meson_drv_unbind, 314 }; 315 316 static int compare_of(struct device *dev, void *data) 317 { 318 DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n", 319 dev->of_node, data); 320 321 return dev->of_node == data; 322 } 323 324 /* Possible connectors nodes to ignore */ 325 static const struct of_device_id connectors_match[] = { 326 { .compatible = "composite-video-connector" }, 327 { .compatible = "svideo-connector" }, 328 { .compatible = "hdmi-connector" }, 329 { .compatible = "dvi-connector" }, 330 {} 331 }; 332 333 static int meson_probe_remote(struct platform_device *pdev, 334 struct component_match **match, 335 struct device_node *parent, 336 struct device_node *remote) 337 { 338 struct device_node *ep, *remote_node; 339 int count = 1; 340 341 /* If node is a connector, return and do not add to match table */ 342 if (of_match_node(connectors_match, remote)) 343 return 1; 344 345 component_match_add(&pdev->dev, match, compare_of, remote); 346 347 for_each_endpoint_of_node(remote, ep) { 348 remote_node = of_graph_get_remote_port_parent(ep); 349 if (!remote_node || 350 remote_node == parent || /* Ignore parent endpoint */ 351 !of_device_is_available(remote_node)) 352 continue; 353 354 count += meson_probe_remote(pdev, match, remote, remote_node); 355 356 of_node_put(remote_node); 357 } 358 359 return count; 360 } 361 362 static int meson_drv_probe(struct platform_device *pdev) 363 { 364 struct component_match *match = NULL; 365 struct device_node *np = pdev->dev.of_node; 366 struct device_node *ep, *remote; 367 int count = 0; 368 369 for_each_endpoint_of_node(np, ep) { 370 remote = of_graph_get_remote_port_parent(ep); 371 if (!remote || !of_device_is_available(remote)) 372 continue; 373 374 count += meson_probe_remote(pdev, &match, np, remote); 375 } 376 377 if (count && !match) 378 return meson_drv_bind_master(&pdev->dev, false); 379 380 /* If some endpoints were found, initialize the nodes */ 381 if (count) { 382 dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count); 383 384 return component_master_add_with_match(&pdev->dev, 385 &meson_drv_master_ops, 386 match); 387 } 388 389 /* If no output endpoints were available, simply bail out */ 390 return 0; 391 }; 392 393 static const struct of_device_id dt_match[] = { 394 { .compatible = "amlogic,meson-gxbb-vpu" }, 395 { .compatible = "amlogic,meson-gxl-vpu" }, 396 { .compatible = "amlogic,meson-gxm-vpu" }, 397 {} 398 }; 399 MODULE_DEVICE_TABLE(of, dt_match); 400 401 static struct platform_driver meson_drm_platform_driver = { 402 .probe = meson_drv_probe, 403 .driver = { 404 .name = "meson-drm", 405 .of_match_table = dt_match, 406 }, 407 }; 408 409 module_platform_driver(meson_drm_platform_driver); 410 411 MODULE_AUTHOR("Jasper St. Pierre <jstpierre@mecheye.net>"); 412 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>"); 413 MODULE_DESCRIPTION(DRIVER_DESC); 414 MODULE_LICENSE("GPL"); 415