1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2016 Marek Vasut <marex@denx.de> 4 * 5 * This code is based on drivers/video/fbdev/mxsfb.c : 6 * Copyright (C) 2010 Juergen Beisert, Pengutronix 7 * Copyright (C) 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved. 8 * Copyright (C) 2008 Embedded Alley Solutions, Inc All Rights Reserved. 9 */ 10 11 #include <linux/module.h> 12 #include <linux/spinlock.h> 13 #include <linux/clk.h> 14 #include <linux/component.h> 15 #include <linux/list.h> 16 #include <linux/of_device.h> 17 #include <linux/of_graph.h> 18 #include <linux/of_reserved_mem.h> 19 #include <linux/pm_runtime.h> 20 #include <linux/reservation.h> 21 22 #include <drm/drmP.h> 23 #include <drm/drm_atomic.h> 24 #include <drm/drm_atomic_helper.h> 25 #include <drm/drm_crtc.h> 26 #include <drm/drm_fb_helper.h> 27 #include <drm/drm_fb_cma_helper.h> 28 #include <drm/drm_gem_cma_helper.h> 29 #include <drm/drm_gem_framebuffer_helper.h> 30 #include <drm/drm_of.h> 31 #include <drm/drm_panel.h> 32 #include <drm/drm_probe_helper.h> 33 #include <drm/drm_simple_kms_helper.h> 34 35 #include "mxsfb_drv.h" 36 #include "mxsfb_regs.h" 37 38 enum mxsfb_devtype { 39 MXSFB_V3, 40 MXSFB_V4, 41 }; 42 43 static const struct mxsfb_devdata mxsfb_devdata[] = { 44 [MXSFB_V3] = { 45 .transfer_count = LCDC_V3_TRANSFER_COUNT, 46 .cur_buf = LCDC_V3_CUR_BUF, 47 .next_buf = LCDC_V3_NEXT_BUF, 48 .debug0 = LCDC_V3_DEBUG0, 49 .hs_wdth_mask = 0xff, 50 .hs_wdth_shift = 24, 51 .ipversion = 3, 52 }, 53 [MXSFB_V4] = { 54 .transfer_count = LCDC_V4_TRANSFER_COUNT, 55 .cur_buf = LCDC_V4_CUR_BUF, 56 .next_buf = LCDC_V4_NEXT_BUF, 57 .debug0 = LCDC_V4_DEBUG0, 58 .hs_wdth_mask = 0x3fff, 59 .hs_wdth_shift = 18, 60 .ipversion = 4, 61 }, 62 }; 63 64 static const uint32_t mxsfb_formats[] = { 65 DRM_FORMAT_XRGB8888, 66 DRM_FORMAT_RGB565 67 }; 68 69 static struct mxsfb_drm_private * 70 drm_pipe_to_mxsfb_drm_private(struct drm_simple_display_pipe *pipe) 71 { 72 return container_of(pipe, struct mxsfb_drm_private, pipe); 73 } 74 75 void mxsfb_enable_axi_clk(struct mxsfb_drm_private *mxsfb) 76 { 77 if (mxsfb->clk_axi) 78 clk_prepare_enable(mxsfb->clk_axi); 79 } 80 81 void mxsfb_disable_axi_clk(struct mxsfb_drm_private *mxsfb) 82 { 83 if (mxsfb->clk_axi) 84 clk_disable_unprepare(mxsfb->clk_axi); 85 } 86 87 static const struct drm_mode_config_funcs mxsfb_mode_config_funcs = { 88 .fb_create = drm_gem_fb_create, 89 .atomic_check = drm_atomic_helper_check, 90 .atomic_commit = drm_atomic_helper_commit, 91 }; 92 93 static const struct drm_mode_config_helper_funcs mxsfb_mode_config_helpers = { 94 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm, 95 }; 96 97 static void mxsfb_pipe_enable(struct drm_simple_display_pipe *pipe, 98 struct drm_crtc_state *crtc_state, 99 struct drm_plane_state *plane_state) 100 { 101 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe); 102 struct drm_device *drm = pipe->plane.dev; 103 104 pm_runtime_get_sync(drm->dev); 105 drm_panel_prepare(mxsfb->panel); 106 mxsfb_crtc_enable(mxsfb); 107 drm_panel_enable(mxsfb->panel); 108 } 109 110 static void mxsfb_pipe_disable(struct drm_simple_display_pipe *pipe) 111 { 112 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe); 113 struct drm_device *drm = pipe->plane.dev; 114 struct drm_crtc *crtc = &pipe->crtc; 115 struct drm_pending_vblank_event *event; 116 117 drm_panel_disable(mxsfb->panel); 118 mxsfb_crtc_disable(mxsfb); 119 drm_panel_unprepare(mxsfb->panel); 120 pm_runtime_put_sync(drm->dev); 121 122 spin_lock_irq(&drm->event_lock); 123 event = crtc->state->event; 124 if (event) { 125 crtc->state->event = NULL; 126 drm_crtc_send_vblank_event(crtc, event); 127 } 128 spin_unlock_irq(&drm->event_lock); 129 } 130 131 static void mxsfb_pipe_update(struct drm_simple_display_pipe *pipe, 132 struct drm_plane_state *plane_state) 133 { 134 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe); 135 136 mxsfb_plane_atomic_update(mxsfb, plane_state); 137 } 138 139 static int mxsfb_pipe_enable_vblank(struct drm_simple_display_pipe *pipe) 140 { 141 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe); 142 143 /* Clear and enable VBLANK IRQ */ 144 mxsfb_enable_axi_clk(mxsfb); 145 writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR); 146 writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_SET); 147 mxsfb_disable_axi_clk(mxsfb); 148 149 return 0; 150 } 151 152 static void mxsfb_pipe_disable_vblank(struct drm_simple_display_pipe *pipe) 153 { 154 struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe); 155 156 /* Disable and clear VBLANK IRQ */ 157 mxsfb_enable_axi_clk(mxsfb); 158 writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_CLR); 159 writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR); 160 mxsfb_disable_axi_clk(mxsfb); 161 } 162 163 static struct drm_simple_display_pipe_funcs mxsfb_funcs = { 164 .enable = mxsfb_pipe_enable, 165 .disable = mxsfb_pipe_disable, 166 .update = mxsfb_pipe_update, 167 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb, 168 .enable_vblank = mxsfb_pipe_enable_vblank, 169 .disable_vblank = mxsfb_pipe_disable_vblank, 170 }; 171 172 static int mxsfb_load(struct drm_device *drm, unsigned long flags) 173 { 174 struct platform_device *pdev = to_platform_device(drm->dev); 175 struct mxsfb_drm_private *mxsfb; 176 struct resource *res; 177 int ret; 178 179 mxsfb = devm_kzalloc(&pdev->dev, sizeof(*mxsfb), GFP_KERNEL); 180 if (!mxsfb) 181 return -ENOMEM; 182 183 drm->dev_private = mxsfb; 184 mxsfb->devdata = &mxsfb_devdata[pdev->id_entry->driver_data]; 185 186 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 187 mxsfb->base = devm_ioremap_resource(drm->dev, res); 188 if (IS_ERR(mxsfb->base)) 189 return PTR_ERR(mxsfb->base); 190 191 mxsfb->clk = devm_clk_get(drm->dev, NULL); 192 if (IS_ERR(mxsfb->clk)) 193 return PTR_ERR(mxsfb->clk); 194 195 mxsfb->clk_axi = devm_clk_get(drm->dev, "axi"); 196 if (IS_ERR(mxsfb->clk_axi)) 197 mxsfb->clk_axi = NULL; 198 199 mxsfb->clk_disp_axi = devm_clk_get(drm->dev, "disp_axi"); 200 if (IS_ERR(mxsfb->clk_disp_axi)) 201 mxsfb->clk_disp_axi = NULL; 202 203 ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32)); 204 if (ret) 205 return ret; 206 207 pm_runtime_enable(drm->dev); 208 209 ret = drm_vblank_init(drm, drm->mode_config.num_crtc); 210 if (ret < 0) { 211 dev_err(drm->dev, "Failed to initialise vblank\n"); 212 goto err_vblank; 213 } 214 215 /* Modeset init */ 216 drm_mode_config_init(drm); 217 218 ret = mxsfb_create_output(drm); 219 if (ret < 0) { 220 dev_err(drm->dev, "Failed to create outputs\n"); 221 goto err_vblank; 222 } 223 224 ret = drm_simple_display_pipe_init(drm, &mxsfb->pipe, &mxsfb_funcs, 225 mxsfb_formats, ARRAY_SIZE(mxsfb_formats), NULL, 226 &mxsfb->connector); 227 if (ret < 0) { 228 dev_err(drm->dev, "Cannot setup simple display pipe\n"); 229 goto err_vblank; 230 } 231 232 ret = drm_panel_attach(mxsfb->panel, &mxsfb->connector); 233 if (ret) { 234 dev_err(drm->dev, "Cannot connect panel\n"); 235 goto err_vblank; 236 } 237 238 drm->mode_config.min_width = MXSFB_MIN_XRES; 239 drm->mode_config.min_height = MXSFB_MIN_YRES; 240 drm->mode_config.max_width = MXSFB_MAX_XRES; 241 drm->mode_config.max_height = MXSFB_MAX_YRES; 242 drm->mode_config.funcs = &mxsfb_mode_config_funcs; 243 drm->mode_config.helper_private = &mxsfb_mode_config_helpers; 244 245 drm_mode_config_reset(drm); 246 247 pm_runtime_get_sync(drm->dev); 248 ret = drm_irq_install(drm, platform_get_irq(pdev, 0)); 249 pm_runtime_put_sync(drm->dev); 250 251 if (ret < 0) { 252 dev_err(drm->dev, "Failed to install IRQ handler\n"); 253 goto err_irq; 254 } 255 256 drm_kms_helper_poll_init(drm); 257 258 platform_set_drvdata(pdev, drm); 259 260 drm_helper_hpd_irq_event(drm); 261 262 return 0; 263 264 err_irq: 265 drm_panel_detach(mxsfb->panel); 266 err_vblank: 267 pm_runtime_disable(drm->dev); 268 269 return ret; 270 } 271 272 static void mxsfb_unload(struct drm_device *drm) 273 { 274 drm_kms_helper_poll_fini(drm); 275 drm_mode_config_cleanup(drm); 276 277 pm_runtime_get_sync(drm->dev); 278 drm_irq_uninstall(drm); 279 pm_runtime_put_sync(drm->dev); 280 281 drm->dev_private = NULL; 282 283 pm_runtime_disable(drm->dev); 284 } 285 286 static void mxsfb_irq_preinstall(struct drm_device *drm) 287 { 288 struct mxsfb_drm_private *mxsfb = drm->dev_private; 289 290 mxsfb_pipe_disable_vblank(&mxsfb->pipe); 291 } 292 293 static irqreturn_t mxsfb_irq_handler(int irq, void *data) 294 { 295 struct drm_device *drm = data; 296 struct mxsfb_drm_private *mxsfb = drm->dev_private; 297 u32 reg; 298 299 mxsfb_enable_axi_clk(mxsfb); 300 301 reg = readl(mxsfb->base + LCDC_CTRL1); 302 303 if (reg & CTRL1_CUR_FRAME_DONE_IRQ) 304 drm_crtc_handle_vblank(&mxsfb->pipe.crtc); 305 306 writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR); 307 308 mxsfb_disable_axi_clk(mxsfb); 309 310 return IRQ_HANDLED; 311 } 312 313 DEFINE_DRM_GEM_CMA_FOPS(fops); 314 315 static struct drm_driver mxsfb_driver = { 316 .driver_features = DRIVER_GEM | DRIVER_MODESET | 317 DRIVER_PRIME | DRIVER_ATOMIC, 318 .irq_handler = mxsfb_irq_handler, 319 .irq_preinstall = mxsfb_irq_preinstall, 320 .irq_uninstall = mxsfb_irq_preinstall, 321 .gem_free_object_unlocked = drm_gem_cma_free_object, 322 .gem_vm_ops = &drm_gem_cma_vm_ops, 323 .dumb_create = drm_gem_cma_dumb_create, 324 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 325 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 326 .gem_prime_export = drm_gem_prime_export, 327 .gem_prime_import = drm_gem_prime_import, 328 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, 329 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, 330 .gem_prime_vmap = drm_gem_cma_prime_vmap, 331 .gem_prime_vunmap = drm_gem_cma_prime_vunmap, 332 .gem_prime_mmap = drm_gem_cma_prime_mmap, 333 .fops = &fops, 334 .name = "mxsfb-drm", 335 .desc = "MXSFB Controller DRM", 336 .date = "20160824", 337 .major = 1, 338 .minor = 0, 339 }; 340 341 static const struct platform_device_id mxsfb_devtype[] = { 342 { .name = "imx23-fb", .driver_data = MXSFB_V3, }, 343 { .name = "imx28-fb", .driver_data = MXSFB_V4, }, 344 { .name = "imx6sx-fb", .driver_data = MXSFB_V4, }, 345 { /* sentinel */ } 346 }; 347 MODULE_DEVICE_TABLE(platform, mxsfb_devtype); 348 349 static const struct of_device_id mxsfb_dt_ids[] = { 350 { .compatible = "fsl,imx23-lcdif", .data = &mxsfb_devtype[0], }, 351 { .compatible = "fsl,imx28-lcdif", .data = &mxsfb_devtype[1], }, 352 { .compatible = "fsl,imx6sx-lcdif", .data = &mxsfb_devtype[2], }, 353 { /* sentinel */ } 354 }; 355 MODULE_DEVICE_TABLE(of, mxsfb_dt_ids); 356 357 static int mxsfb_probe(struct platform_device *pdev) 358 { 359 struct drm_device *drm; 360 const struct of_device_id *of_id = 361 of_match_device(mxsfb_dt_ids, &pdev->dev); 362 int ret; 363 364 if (!pdev->dev.of_node) 365 return -ENODEV; 366 367 if (of_id) 368 pdev->id_entry = of_id->data; 369 370 drm = drm_dev_alloc(&mxsfb_driver, &pdev->dev); 371 if (IS_ERR(drm)) 372 return PTR_ERR(drm); 373 374 ret = mxsfb_load(drm, 0); 375 if (ret) 376 goto err_free; 377 378 ret = drm_dev_register(drm, 0); 379 if (ret) 380 goto err_unload; 381 382 drm_fbdev_generic_setup(drm, 32); 383 384 return 0; 385 386 err_unload: 387 mxsfb_unload(drm); 388 err_free: 389 drm_dev_put(drm); 390 391 return ret; 392 } 393 394 static int mxsfb_remove(struct platform_device *pdev) 395 { 396 struct drm_device *drm = platform_get_drvdata(pdev); 397 398 drm_dev_unregister(drm); 399 mxsfb_unload(drm); 400 drm_dev_put(drm); 401 402 return 0; 403 } 404 405 #ifdef CONFIG_PM_SLEEP 406 static int mxsfb_suspend(struct device *dev) 407 { 408 struct drm_device *drm = dev_get_drvdata(dev); 409 410 return drm_mode_config_helper_suspend(drm); 411 } 412 413 static int mxsfb_resume(struct device *dev) 414 { 415 struct drm_device *drm = dev_get_drvdata(dev); 416 417 return drm_mode_config_helper_resume(drm); 418 } 419 #endif 420 421 static const struct dev_pm_ops mxsfb_pm_ops = { 422 SET_SYSTEM_SLEEP_PM_OPS(mxsfb_suspend, mxsfb_resume) 423 }; 424 425 static struct platform_driver mxsfb_platform_driver = { 426 .probe = mxsfb_probe, 427 .remove = mxsfb_remove, 428 .id_table = mxsfb_devtype, 429 .driver = { 430 .name = "mxsfb", 431 .of_match_table = mxsfb_dt_ids, 432 .pm = &mxsfb_pm_ops, 433 }, 434 }; 435 436 module_platform_driver(mxsfb_platform_driver); 437 438 MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); 439 MODULE_DESCRIPTION("Freescale MXS DRM/KMS driver"); 440 MODULE_LICENSE("GPL"); 441