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