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