1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * (C) COPYRIGHT 2012-2013 ARM Limited. All rights reserved. 4 * 5 * Parts of this file were based on sources as follows: 6 * 7 * Copyright (c) 2006-2008 Intel Corporation 8 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> 9 * Copyright (C) 2011 Texas Instruments 10 */ 11 12 /** 13 * DOC: ARM PrimeCell PL110 and PL111 CLCD Driver 14 * 15 * The PL110/PL111 is a simple LCD controller that can support TFT 16 * and STN displays. This driver exposes a standard KMS interface 17 * for them. 18 * 19 * The driver currently doesn't expose the cursor. The DRM API for 20 * cursors requires support for 64x64 ARGB8888 cursor images, while 21 * the hardware can only support 64x64 monochrome with masking 22 * cursors. While one could imagine trying to hack something together 23 * to look at the ARGB8888 and program reasonable in monochrome, we 24 * just don't expose the cursor at all instead, and leave cursor 25 * support to the application software cursor layer. 26 * 27 * TODO: 28 * 29 * - Fix race between setting plane base address and getting IRQ for 30 * vsync firing the pageflip completion. 31 * 32 * - Read back hardware state at boot to skip reprogramming the 33 * hardware when doing a no-op modeset. 34 * 35 * - Use the CLKSEL bit to support switching between the two external 36 * clock parents. 37 */ 38 39 #include <linux/amba/bus.h> 40 #include <linux/dma-buf.h> 41 #include <linux/module.h> 42 #include <linux/of.h> 43 #include <linux/of_graph.h> 44 #include <linux/of_reserved_mem.h> 45 #include <linux/shmem_fs.h> 46 #include <linux/slab.h> 47 48 #include <drm/clients/drm_client_setup.h> 49 #include <drm/drm_atomic_helper.h> 50 #include <drm/drm_bridge.h> 51 #include <drm/drm_drv.h> 52 #include <drm/drm_fbdev_dma.h> 53 #include <drm/drm_fourcc.h> 54 #include <drm/drm_gem_dma_helper.h> 55 #include <drm/drm_gem_framebuffer_helper.h> 56 #include <drm/drm_of.h> 57 #include <drm/drm_panel.h> 58 #include <drm/drm_print.h> 59 #include <drm/drm_probe_helper.h> 60 #include <drm/drm_vblank.h> 61 62 #include "pl111_drm.h" 63 #include "pl111_versatile.h" 64 #include "pl111_nomadik.h" 65 66 #define DRIVER_DESC "DRM module for PL111" 67 68 static const struct drm_mode_config_funcs mode_config_funcs = { 69 .fb_create = drm_gem_fb_create, 70 .atomic_check = drm_atomic_helper_check, 71 .atomic_commit = drm_atomic_helper_commit, 72 }; 73 74 static int pl111_modeset_init(struct drm_device *dev) 75 { 76 struct drm_mode_config *mode_config; 77 struct pl111_drm_dev_private *priv = dev->dev_private; 78 struct device_node *np = dev->dev->of_node; 79 struct device_node *remote; 80 struct drm_panel *panel = NULL; 81 struct drm_bridge *bridge = NULL; 82 bool defer = false; 83 int ret; 84 int i; 85 86 ret = drmm_mode_config_init(dev); 87 if (ret) 88 return ret; 89 90 mode_config = &dev->mode_config; 91 mode_config->funcs = &mode_config_funcs; 92 mode_config->min_width = 1; 93 mode_config->max_width = 1024; 94 mode_config->min_height = 1; 95 mode_config->max_height = 768; 96 97 i = 0; 98 for_each_endpoint_of_node(np, remote) { 99 struct drm_panel *tmp_panel; 100 struct drm_bridge *tmp_bridge; 101 102 drm_dbg(dev, "checking endpoint %d\n", i); 103 104 ret = drm_of_find_panel_or_bridge(dev->dev->of_node, 105 0, i, 106 &tmp_panel, 107 &tmp_bridge); 108 if (ret) { 109 if (ret == -EPROBE_DEFER) { 110 /* 111 * Something deferred, but that is often just 112 * another way of saying -ENODEV, but let's 113 * cast a vote for later deferral. 114 */ 115 defer = true; 116 } else if (ret != -ENODEV) { 117 /* Continue, maybe something else is working */ 118 drm_err(dev, 119 "endpoint %d returns %d\n", i, ret); 120 } 121 } 122 123 if (tmp_panel) { 124 drm_info(dev, 125 "found panel on endpoint %d\n", i); 126 panel = tmp_panel; 127 } 128 if (tmp_bridge) { 129 drm_info(dev, 130 "found bridge on endpoint %d\n", i); 131 bridge = tmp_bridge; 132 } 133 134 i++; 135 } 136 137 /* 138 * If we can't find neither panel nor bridge on any of the 139 * endpoints, and any of them retured -EPROBE_DEFER, then 140 * let's defer this driver too. 141 */ 142 if ((!panel && !bridge) && defer) 143 return -EPROBE_DEFER; 144 145 if (panel) { 146 bridge = drm_panel_bridge_add_typed(panel, 147 DRM_MODE_CONNECTOR_Unknown); 148 drm_panel_put(panel); 149 if (IS_ERR(bridge)) { 150 ret = PTR_ERR(bridge); 151 goto finish; 152 } 153 } else if (bridge) { 154 drm_info(dev, "Using non-panel bridge\n"); 155 } else { 156 drm_err(dev, "No bridge, exiting\n"); 157 return -ENODEV; 158 } 159 160 priv->bridge = bridge; 161 if (panel) { 162 priv->panel = panel; 163 priv->connector = drm_panel_bridge_connector(bridge); 164 } 165 166 ret = pl111_display_init(dev); 167 if (ret != 0) { 168 drm_err(dev, "Failed to init display\n"); 169 goto out_bridge; 170 } 171 172 ret = drm_simple_display_pipe_attach_bridge(&priv->pipe, 173 bridge); 174 if (ret) 175 return ret; 176 177 if (!priv->variant->broken_vblank) { 178 ret = drm_vblank_init(dev, 1); 179 if (ret != 0) { 180 drm_err(dev, "Failed to init vblank\n"); 181 goto out_bridge; 182 } 183 } 184 185 drm_mode_config_reset(dev); 186 187 drm_kms_helper_poll_init(dev); 188 189 goto finish; 190 191 out_bridge: 192 if (panel) 193 drm_panel_bridge_remove(bridge); 194 finish: 195 return ret; 196 } 197 198 static struct drm_gem_object * 199 pl111_gem_import_sg_table(struct drm_device *dev, 200 struct dma_buf_attachment *attach, 201 struct sg_table *sgt) 202 { 203 struct pl111_drm_dev_private *priv = dev->dev_private; 204 205 /* 206 * When using device-specific reserved memory we can't import 207 * DMA buffers: those are passed by reference in any global 208 * memory and we can only handle a specific range of memory. 209 */ 210 if (priv->use_device_memory) 211 return ERR_PTR(-EINVAL); 212 213 return drm_gem_dma_prime_import_sg_table(dev, attach, sgt); 214 } 215 216 DEFINE_DRM_GEM_DMA_FOPS(drm_fops); 217 218 static const struct drm_driver pl111_drm_driver = { 219 .driver_features = 220 DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC, 221 .ioctls = NULL, 222 .fops = &drm_fops, 223 .name = "pl111", 224 .desc = DRIVER_DESC, 225 .major = 1, 226 .minor = 0, 227 .patchlevel = 0, 228 .dumb_create = drm_gem_dma_dumb_create, 229 .gem_prime_import_sg_table = pl111_gem_import_sg_table, 230 DRM_FBDEV_DMA_DRIVER_OPS, 231 232 #if defined(CONFIG_DEBUG_FS) 233 .debugfs_init = pl111_debugfs_init, 234 #endif 235 }; 236 237 static int pl111_amba_probe(struct amba_device *amba_dev, 238 const struct amba_id *id) 239 { 240 struct device *dev = &amba_dev->dev; 241 struct pl111_drm_dev_private *priv; 242 const struct pl111_variant_data *variant = id->data; 243 struct drm_device *drm; 244 int ret; 245 246 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 247 if (!priv) 248 return -ENOMEM; 249 250 drm = drm_dev_alloc(&pl111_drm_driver, dev); 251 if (IS_ERR(drm)) 252 return PTR_ERR(drm); 253 amba_set_drvdata(amba_dev, drm); 254 priv->drm = drm; 255 drm->dev_private = priv; 256 priv->variant = variant; 257 258 ret = of_reserved_mem_device_init(dev); 259 if (!ret) { 260 drm_info(drm, "using device-specific reserved memory\n"); 261 priv->use_device_memory = true; 262 } 263 264 if (of_property_read_u32(dev->of_node, "max-memory-bandwidth", 265 &priv->memory_bw)) { 266 drm_info(drm, "no max memory bandwidth specified, assume unlimited\n"); 267 priv->memory_bw = 0; 268 } 269 270 /* The two main variants swap this register */ 271 if (variant->is_pl110 || variant->is_lcdc) { 272 priv->ienb = CLCD_PL110_IENB; 273 priv->ctrl = CLCD_PL110_CNTL; 274 } else { 275 priv->ienb = CLCD_PL111_IENB; 276 priv->ctrl = CLCD_PL111_CNTL; 277 } 278 279 priv->regs = devm_ioremap_resource(dev, &amba_dev->res); 280 if (IS_ERR(priv->regs)) { 281 drm_err(drm, "%s failed mmio\n", __func__); 282 ret = PTR_ERR(priv->regs); 283 goto dev_put; 284 } 285 286 /* This may override some variant settings */ 287 ret = pl111_versatile_init(drm, priv); 288 if (ret) 289 goto dev_put; 290 291 pl111_nomadik_init(drm); 292 293 /* turn off interrupts before requesting the irq */ 294 writel(0, priv->regs + priv->ienb); 295 296 ret = devm_request_irq(dev, amba_dev->irq[0], pl111_irq, 0, 297 variant->name, priv); 298 if (ret != 0) { 299 drm_err(drm, "%s failed irq %d\n", __func__, ret); 300 goto dev_put; 301 } 302 303 ret = pl111_modeset_init(drm); 304 if (ret != 0) 305 goto dev_put; 306 307 ret = drm_dev_register(drm, 0); 308 if (ret < 0) 309 goto dev_put; 310 311 drm_client_setup_with_color_mode(drm, priv->variant->fb_depth); 312 313 return 0; 314 315 dev_put: 316 drm_dev_put(drm); 317 of_reserved_mem_device_release(dev); 318 319 return ret; 320 } 321 322 static void pl111_amba_remove(struct amba_device *amba_dev) 323 { 324 struct device *dev = &amba_dev->dev; 325 struct drm_device *drm = amba_get_drvdata(amba_dev); 326 struct pl111_drm_dev_private *priv = drm->dev_private; 327 328 drm_dev_unregister(drm); 329 drm_atomic_helper_shutdown(drm); 330 if (priv->panel) 331 drm_panel_bridge_remove(priv->bridge); 332 drm_dev_put(drm); 333 of_reserved_mem_device_release(dev); 334 } 335 336 static void pl111_amba_shutdown(struct amba_device *amba_dev) 337 { 338 drm_atomic_helper_shutdown(amba_get_drvdata(amba_dev)); 339 } 340 341 /* 342 * This early variant lacks the 565 and 444 pixel formats. 343 */ 344 static const u32 pl110_pixel_formats[] = { 345 DRM_FORMAT_ABGR8888, 346 DRM_FORMAT_XBGR8888, 347 DRM_FORMAT_ARGB8888, 348 DRM_FORMAT_XRGB8888, 349 DRM_FORMAT_ABGR1555, 350 DRM_FORMAT_XBGR1555, 351 DRM_FORMAT_ARGB1555, 352 DRM_FORMAT_XRGB1555, 353 }; 354 355 static const struct pl111_variant_data pl110_variant = { 356 .name = "PL110", 357 .is_pl110 = true, 358 .formats = pl110_pixel_formats, 359 .nformats = ARRAY_SIZE(pl110_pixel_formats), 360 .fb_depth = 16, 361 }; 362 363 /* RealView, Versatile Express etc use this modern variant */ 364 static const u32 pl111_pixel_formats[] = { 365 DRM_FORMAT_ABGR8888, 366 DRM_FORMAT_XBGR8888, 367 DRM_FORMAT_ARGB8888, 368 DRM_FORMAT_XRGB8888, 369 DRM_FORMAT_BGR565, 370 DRM_FORMAT_RGB565, 371 DRM_FORMAT_ABGR1555, 372 DRM_FORMAT_XBGR1555, 373 DRM_FORMAT_ARGB1555, 374 DRM_FORMAT_XRGB1555, 375 DRM_FORMAT_ABGR4444, 376 DRM_FORMAT_XBGR4444, 377 DRM_FORMAT_ARGB4444, 378 DRM_FORMAT_XRGB4444, 379 }; 380 381 static const struct pl111_variant_data pl111_variant = { 382 .name = "PL111", 383 .formats = pl111_pixel_formats, 384 .nformats = ARRAY_SIZE(pl111_pixel_formats), 385 .fb_depth = 32, 386 }; 387 388 static const u32 pl110_nomadik_pixel_formats[] = { 389 DRM_FORMAT_RGB888, 390 DRM_FORMAT_BGR888, 391 DRM_FORMAT_ABGR8888, 392 DRM_FORMAT_XBGR8888, 393 DRM_FORMAT_ARGB8888, 394 DRM_FORMAT_XRGB8888, 395 DRM_FORMAT_BGR565, 396 DRM_FORMAT_RGB565, 397 DRM_FORMAT_ABGR1555, 398 DRM_FORMAT_XBGR1555, 399 DRM_FORMAT_ARGB1555, 400 DRM_FORMAT_XRGB1555, 401 DRM_FORMAT_ABGR4444, 402 DRM_FORMAT_XBGR4444, 403 DRM_FORMAT_ARGB4444, 404 DRM_FORMAT_XRGB4444, 405 }; 406 407 static const struct pl111_variant_data pl110_nomadik_variant = { 408 .name = "LCDC (PL110 Nomadik)", 409 .formats = pl110_nomadik_pixel_formats, 410 .nformats = ARRAY_SIZE(pl110_nomadik_pixel_formats), 411 .is_lcdc = true, 412 .st_bitmux_control = true, 413 .broken_vblank = true, 414 .fb_depth = 16, 415 }; 416 417 static const struct amba_id pl111_id_table[] = { 418 { 419 .id = 0x00041110, 420 .mask = 0x000fffff, 421 .data = (void *)&pl110_variant, 422 }, 423 { 424 .id = 0x00180110, 425 .mask = 0x00fffffe, 426 .data = (void *)&pl110_nomadik_variant, 427 }, 428 { 429 .id = 0x00041111, 430 .mask = 0x000fffff, 431 .data = (void *)&pl111_variant, 432 }, 433 {0, 0}, 434 }; 435 MODULE_DEVICE_TABLE(amba, pl111_id_table); 436 437 static struct amba_driver pl111_amba_driver __maybe_unused = { 438 .drv = { 439 .name = "drm-clcd-pl111", 440 }, 441 .probe = pl111_amba_probe, 442 .remove = pl111_amba_remove, 443 .shutdown = pl111_amba_shutdown, 444 .id_table = pl111_id_table, 445 }; 446 447 #ifdef CONFIG_ARM_AMBA 448 module_amba_driver(pl111_amba_driver); 449 #endif 450 451 MODULE_DESCRIPTION(DRIVER_DESC); 452 MODULE_AUTHOR("ARM Ltd."); 453 MODULE_LICENSE("GPL"); 454