Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 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> |
Neil Armstrong | a41e82e | 2017-04-04 14:15:23 +0200 | [diff] [blame] | 27 | #include <linux/component.h> |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 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_fb_cma_helper.h> |
| 38 | #include <drm/drm_rect.h> |
| 39 | #include <drm/drm_fb_helper.h> |
| 40 | |
| 41 | #include "meson_drv.h" |
| 42 | #include "meson_plane.h" |
| 43 | #include "meson_crtc.h" |
| 44 | #include "meson_venc_cvbs.h" |
| 45 | |
| 46 | #include "meson_vpp.h" |
| 47 | #include "meson_viu.h" |
| 48 | #include "meson_venc.h" |
| 49 | #include "meson_canvas.h" |
| 50 | #include "meson_registers.h" |
| 51 | |
| 52 | #define DRIVER_NAME "meson" |
| 53 | #define DRIVER_DESC "Amlogic Meson DRM driver" |
| 54 | |
Neil Armstrong | 2021d5b | 2017-04-04 14:15:29 +0200 | [diff] [blame^] | 55 | /** |
| 56 | * DOC: Video Processing Unit |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 57 | * |
| 58 | * VPU Handles the Global Video Processing, it includes management of the |
| 59 | * clocks gates, blocks reset lines and power domains. |
| 60 | * |
| 61 | * What is missing : |
Neil Armstrong | 2021d5b | 2017-04-04 14:15:29 +0200 | [diff] [blame^] | 62 | * |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 63 | * - Full reset of entire video processing HW blocks |
| 64 | * - Scaling and setup of the VPU clock |
| 65 | * - Bus clock gates |
| 66 | * - Powering up video processing HW blocks |
| 67 | * - Powering Up HDMI controller and PHY |
| 68 | */ |
| 69 | |
| 70 | static void meson_fb_output_poll_changed(struct drm_device *dev) |
| 71 | { |
| 72 | struct meson_drm *priv = dev->dev_private; |
| 73 | |
| 74 | drm_fbdev_cma_hotplug_event(priv->fbdev); |
| 75 | } |
| 76 | |
| 77 | static const struct drm_mode_config_funcs meson_mode_config_funcs = { |
| 78 | .output_poll_changed = meson_fb_output_poll_changed, |
| 79 | .atomic_check = drm_atomic_helper_check, |
| 80 | .atomic_commit = drm_atomic_helper_commit, |
| 81 | .fb_create = drm_fb_cma_create, |
| 82 | }; |
| 83 | |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 84 | static irqreturn_t meson_irq(int irq, void *arg) |
| 85 | { |
| 86 | struct drm_device *dev = arg; |
| 87 | struct meson_drm *priv = dev->dev_private; |
| 88 | |
| 89 | (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG)); |
| 90 | |
| 91 | meson_crtc_irq(priv); |
| 92 | |
| 93 | return IRQ_HANDLED; |
| 94 | } |
| 95 | |
Daniel Vetter | d55f7e5 | 2017-03-08 15:12:56 +0100 | [diff] [blame] | 96 | DEFINE_DRM_GEM_CMA_FOPS(fops); |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 97 | |
| 98 | static struct drm_driver meson_driver = { |
| 99 | .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | |
| 100 | DRIVER_MODESET | DRIVER_PRIME | |
| 101 | DRIVER_ATOMIC, |
| 102 | |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 103 | /* IRQ */ |
| 104 | .irq_handler = meson_irq, |
| 105 | |
| 106 | /* PRIME Ops */ |
| 107 | .prime_handle_to_fd = drm_gem_prime_handle_to_fd, |
| 108 | .prime_fd_to_handle = drm_gem_prime_fd_to_handle, |
| 109 | .gem_prime_import = drm_gem_prime_import, |
| 110 | .gem_prime_export = drm_gem_prime_export, |
| 111 | .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, |
| 112 | .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, |
| 113 | .gem_prime_vmap = drm_gem_cma_prime_vmap, |
| 114 | .gem_prime_vunmap = drm_gem_cma_prime_vunmap, |
| 115 | .gem_prime_mmap = drm_gem_cma_prime_mmap, |
| 116 | |
| 117 | /* GEM Ops */ |
| 118 | .dumb_create = drm_gem_cma_dumb_create, |
| 119 | .dumb_destroy = drm_gem_dumb_destroy, |
| 120 | .dumb_map_offset = drm_gem_cma_dumb_map_offset, |
| 121 | .gem_free_object_unlocked = drm_gem_cma_free_object, |
| 122 | .gem_vm_ops = &drm_gem_cma_vm_ops, |
| 123 | |
| 124 | /* Misc */ |
| 125 | .fops = &fops, |
| 126 | .name = DRIVER_NAME, |
| 127 | .desc = DRIVER_DESC, |
| 128 | .date = "20161109", |
| 129 | .major = 1, |
| 130 | .minor = 0, |
| 131 | }; |
| 132 | |
| 133 | static bool meson_vpu_has_available_connectors(struct device *dev) |
| 134 | { |
| 135 | struct device_node *ep, *remote; |
| 136 | |
| 137 | /* Parses each endpoint and check if remote exists */ |
| 138 | for_each_endpoint_of_node(dev->of_node, ep) { |
| 139 | /* If the endpoint node exists, consider it enabled */ |
| 140 | remote = of_graph_get_remote_port(ep); |
| 141 | if (remote) |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | static struct regmap_config meson_regmap_config = { |
| 149 | .reg_bits = 32, |
| 150 | .val_bits = 32, |
| 151 | .reg_stride = 4, |
| 152 | .max_register = 0x1000, |
| 153 | }; |
| 154 | |
Neil Armstrong | a41e82e | 2017-04-04 14:15:23 +0200 | [diff] [blame] | 155 | static int meson_drv_bind(struct device *dev) |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 156 | { |
Neil Armstrong | a41e82e | 2017-04-04 14:15:23 +0200 | [diff] [blame] | 157 | struct platform_device *pdev = to_platform_device(dev); |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 158 | struct meson_drm *priv; |
| 159 | struct drm_device *drm; |
| 160 | struct resource *res; |
| 161 | void __iomem *regs; |
| 162 | int ret; |
| 163 | |
| 164 | /* Checks if an output connector is available */ |
| 165 | if (!meson_vpu_has_available_connectors(dev)) { |
| 166 | dev_err(dev, "No output connector available\n"); |
| 167 | return -ENODEV; |
| 168 | } |
| 169 | |
| 170 | drm = drm_dev_alloc(&meson_driver, dev); |
| 171 | if (IS_ERR(drm)) |
| 172 | return PTR_ERR(drm); |
| 173 | |
| 174 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 175 | if (!priv) { |
| 176 | ret = -ENOMEM; |
| 177 | goto free_drm; |
| 178 | } |
| 179 | drm->dev_private = priv; |
| 180 | priv->drm = drm; |
| 181 | priv->dev = dev; |
| 182 | |
| 183 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vpu"); |
| 184 | regs = devm_ioremap_resource(dev, res); |
| 185 | if (IS_ERR(regs)) |
| 186 | return PTR_ERR(regs); |
| 187 | |
| 188 | priv->io_base = regs; |
| 189 | |
| 190 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi"); |
| 191 | /* Simply ioremap since it may be a shared register zone */ |
| 192 | regs = devm_ioremap(dev, res->start, resource_size(res)); |
| 193 | if (!regs) |
| 194 | return -EADDRNOTAVAIL; |
| 195 | |
| 196 | priv->hhi = devm_regmap_init_mmio(dev, regs, |
| 197 | &meson_regmap_config); |
| 198 | if (IS_ERR(priv->hhi)) { |
| 199 | dev_err(&pdev->dev, "Couldn't create the HHI regmap\n"); |
| 200 | return PTR_ERR(priv->hhi); |
| 201 | } |
| 202 | |
| 203 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmc"); |
| 204 | /* Simply ioremap since it may be a shared register zone */ |
| 205 | regs = devm_ioremap(dev, res->start, resource_size(res)); |
| 206 | if (!regs) |
| 207 | return -EADDRNOTAVAIL; |
| 208 | |
| 209 | priv->dmc = devm_regmap_init_mmio(dev, regs, |
| 210 | &meson_regmap_config); |
| 211 | if (IS_ERR(priv->dmc)) { |
| 212 | dev_err(&pdev->dev, "Couldn't create the DMC regmap\n"); |
| 213 | return PTR_ERR(priv->dmc); |
| 214 | } |
| 215 | |
| 216 | priv->vsync_irq = platform_get_irq(pdev, 0); |
| 217 | |
| 218 | drm_vblank_init(drm, 1); |
| 219 | drm_mode_config_init(drm); |
Neil Armstrong | a41e82e | 2017-04-04 14:15:23 +0200 | [diff] [blame] | 220 | drm->mode_config.max_width = 3840; |
| 221 | drm->mode_config.max_height = 2160; |
| 222 | drm->mode_config.funcs = &meson_mode_config_funcs; |
| 223 | |
| 224 | /* Hardware Initialization */ |
| 225 | |
| 226 | meson_venc_init(priv); |
| 227 | meson_vpp_init(priv); |
| 228 | meson_viu_init(priv); |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 229 | |
| 230 | /* Encoder Initialization */ |
| 231 | |
| 232 | ret = meson_venc_cvbs_create(priv); |
| 233 | if (ret) |
| 234 | goto free_drm; |
| 235 | |
Neil Armstrong | a41e82e | 2017-04-04 14:15:23 +0200 | [diff] [blame] | 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 | } |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 241 | |
| 242 | ret = meson_plane_create(priv); |
| 243 | if (ret) |
| 244 | goto free_drm; |
| 245 | |
| 246 | ret = meson_crtc_create(priv); |
| 247 | if (ret) |
| 248 | goto free_drm; |
| 249 | |
| 250 | ret = drm_irq_install(drm, priv->vsync_irq); |
| 251 | if (ret) |
| 252 | goto free_drm; |
| 253 | |
| 254 | drm_mode_config_reset(drm); |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 255 | |
| 256 | priv->fbdev = drm_fbdev_cma_init(drm, 32, |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 257 | drm->mode_config.num_connector); |
| 258 | if (IS_ERR(priv->fbdev)) { |
| 259 | ret = PTR_ERR(priv->fbdev); |
| 260 | goto free_drm; |
| 261 | } |
| 262 | |
| 263 | drm_kms_helper_poll_init(drm); |
| 264 | |
| 265 | platform_set_drvdata(pdev, priv); |
| 266 | |
| 267 | ret = drm_dev_register(drm, 0); |
| 268 | if (ret) |
| 269 | goto free_drm; |
| 270 | |
| 271 | return 0; |
| 272 | |
| 273 | free_drm: |
| 274 | drm_dev_unref(drm); |
| 275 | |
| 276 | return ret; |
| 277 | } |
| 278 | |
Neil Armstrong | a41e82e | 2017-04-04 14:15:23 +0200 | [diff] [blame] | 279 | static void meson_drv_unbind(struct device *dev) |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 280 | { |
Neil Armstrong | a41e82e | 2017-04-04 14:15:23 +0200 | [diff] [blame] | 281 | struct drm_device *drm = dev_get_drvdata(dev); |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 282 | struct meson_drm *priv = drm->dev_private; |
| 283 | |
| 284 | drm_dev_unregister(drm); |
| 285 | drm_kms_helper_poll_fini(drm); |
| 286 | drm_fbdev_cma_fini(priv->fbdev); |
| 287 | drm_mode_config_cleanup(drm); |
| 288 | drm_vblank_cleanup(drm); |
| 289 | drm_dev_unref(drm); |
| 290 | |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Neil Armstrong | a41e82e | 2017-04-04 14:15:23 +0200 | [diff] [blame] | 293 | static const struct component_master_ops meson_drv_master_ops = { |
| 294 | .bind = meson_drv_bind, |
| 295 | .unbind = meson_drv_unbind, |
| 296 | }; |
| 297 | |
| 298 | static int compare_of(struct device *dev, void *data) |
| 299 | { |
| 300 | DRM_DEBUG_DRIVER("Comparing of node %s with %s\n", |
| 301 | of_node_full_name(dev->of_node), |
| 302 | of_node_full_name(data)); |
| 303 | |
| 304 | return dev->of_node == data; |
| 305 | } |
| 306 | |
| 307 | /* Possible connectors nodes to ignore */ |
| 308 | static const struct of_device_id connectors_match[] = { |
| 309 | { .compatible = "composite-video-connector" }, |
| 310 | { .compatible = "svideo-connector" }, |
| 311 | { .compatible = "hdmi-connector" }, |
| 312 | { .compatible = "dvi-connector" }, |
| 313 | {} |
| 314 | }; |
| 315 | |
| 316 | static int meson_probe_remote(struct platform_device *pdev, |
| 317 | struct component_match **match, |
| 318 | struct device_node *parent, |
| 319 | struct device_node *remote) |
| 320 | { |
| 321 | struct device_node *ep, *remote_node; |
| 322 | int count = 1; |
| 323 | |
| 324 | /* If node is a connector, return and do not add to match table */ |
| 325 | if (of_match_node(connectors_match, remote)) |
| 326 | return 1; |
| 327 | |
| 328 | component_match_add(&pdev->dev, match, compare_of, remote); |
| 329 | |
| 330 | for_each_endpoint_of_node(remote, ep) { |
| 331 | remote_node = of_graph_get_remote_port_parent(ep); |
| 332 | if (!remote_node || |
| 333 | remote_node == parent || /* Ignore parent endpoint */ |
| 334 | !of_device_is_available(remote_node)) |
| 335 | continue; |
| 336 | |
| 337 | count += meson_probe_remote(pdev, match, remote, remote_node); |
| 338 | |
| 339 | of_node_put(remote_node); |
| 340 | } |
| 341 | |
| 342 | return count; |
| 343 | } |
| 344 | |
| 345 | static int meson_drv_probe(struct platform_device *pdev) |
| 346 | { |
| 347 | struct component_match *match = NULL; |
| 348 | struct device_node *np = pdev->dev.of_node; |
| 349 | struct device_node *ep, *remote; |
| 350 | int count = 0; |
| 351 | |
| 352 | for_each_endpoint_of_node(np, ep) { |
| 353 | remote = of_graph_get_remote_port_parent(ep); |
| 354 | if (!remote || !of_device_is_available(remote)) |
| 355 | continue; |
| 356 | |
| 357 | count += meson_probe_remote(pdev, &match, np, remote); |
| 358 | } |
| 359 | |
| 360 | /* If some endpoints were found, initialize the nodes */ |
| 361 | if (count) { |
| 362 | dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count); |
| 363 | |
| 364 | return component_master_add_with_match(&pdev->dev, |
| 365 | &meson_drv_master_ops, |
| 366 | match); |
| 367 | } |
| 368 | |
| 369 | /* If no output endpoints were available, simply bail out */ |
| 370 | return 0; |
| 371 | }; |
| 372 | |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 373 | static const struct of_device_id dt_match[] = { |
| 374 | { .compatible = "amlogic,meson-gxbb-vpu" }, |
| 375 | { .compatible = "amlogic,meson-gxl-vpu" }, |
| 376 | { .compatible = "amlogic,meson-gxm-vpu" }, |
| 377 | {} |
| 378 | }; |
| 379 | MODULE_DEVICE_TABLE(of, dt_match); |
| 380 | |
| 381 | static struct platform_driver meson_drm_platform_driver = { |
| 382 | .probe = meson_drv_probe, |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 383 | .driver = { |
Neil Armstrong | 8aaacbc | 2017-02-02 10:47:44 +0100 | [diff] [blame] | 384 | .name = "meson-drm", |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 385 | .of_match_table = dt_match, |
| 386 | }, |
| 387 | }; |
| 388 | |
| 389 | module_platform_driver(meson_drm_platform_driver); |
| 390 | |
| 391 | MODULE_AUTHOR("Jasper St. Pierre <jstpierre@mecheye.net>"); |
| 392 | MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>"); |
| 393 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 394 | MODULE_LICENSE("GPL"); |