1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 Traphandler
4 * Copyright (C) 2014 Free Electrons
5 * Copyright (C) 2014 Atmel
11 #include <linux/clk.h>
12 #include <linux/irq.h>
13 #include <linux/irqchip.h>
14 #include <linux/mfd/atmel-hlcdc.h>
15 #include <linux/module.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/platform_device.h>
19 #include <drm/drm_atomic.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_drv.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_gem_cma_helper.h>
24 #include <drm/drm_gem_framebuffer_helper.h>
25 #include <drm/drm_irq.h>
26 #include <drm/drm_probe_helper.h>
27 #include <drm/drm_vblank.h>
29 #include "atmel_hlcdc_dc.h"
31 #define ATMEL_HLCDC_LAYER_IRQS_OFFSET 8
33 static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9n12_layers[] = {
36 .formats = &atmel_hlcdc_plane_rgb_formats,
39 .type = ATMEL_HLCDC_BASE_LAYER,
50 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_at91sam9n12 = {
58 .conflicting_output_formats = true,
59 .nlayers = ARRAY_SIZE(atmel_hlcdc_at91sam9n12_layers),
60 .layers = atmel_hlcdc_at91sam9n12_layers,
63 static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9x5_layers[] = {
66 .formats = &atmel_hlcdc_plane_rgb_formats,
69 .type = ATMEL_HLCDC_BASE_LAYER,
82 .formats = &atmel_hlcdc_plane_rgb_formats,
85 .type = ATMEL_HLCDC_OVERLAY_LAYER,
100 .name = "high-end-overlay",
101 .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
102 .regs_offset = 0x280,
104 .type = ATMEL_HLCDC_OVERLAY_LAYER,
114 .chroma_key_mask = 11,
115 .general_config = 12,
119 .clut_offset = 0x1000,
123 .formats = &atmel_hlcdc_plane_rgb_formats,
124 .regs_offset = 0x340,
126 .type = ATMEL_HLCDC_CURSOR_LAYER,
136 .chroma_key_mask = 8,
139 .clut_offset = 0x1400,
143 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_at91sam9x5 = {
151 .conflicting_output_formats = true,
152 .nlayers = ARRAY_SIZE(atmel_hlcdc_at91sam9x5_layers),
153 .layers = atmel_hlcdc_at91sam9x5_layers,
156 static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d3_layers[] = {
159 .formats = &atmel_hlcdc_plane_rgb_formats,
162 .type = ATMEL_HLCDC_BASE_LAYER,
171 .clut_offset = 0x600,
175 .formats = &atmel_hlcdc_plane_rgb_formats,
176 .regs_offset = 0x140,
178 .type = ATMEL_HLCDC_OVERLAY_LAYER,
187 .chroma_key_mask = 8,
190 .clut_offset = 0xa00,
194 .formats = &atmel_hlcdc_plane_rgb_formats,
195 .regs_offset = 0x240,
197 .type = ATMEL_HLCDC_OVERLAY_LAYER,
206 .chroma_key_mask = 8,
209 .clut_offset = 0xe00,
212 .name = "high-end-overlay",
213 .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
214 .regs_offset = 0x340,
216 .type = ATMEL_HLCDC_OVERLAY_LAYER,
226 .chroma_key_mask = 11,
227 .general_config = 12,
235 .clut_offset = 0x1200,
239 .formats = &atmel_hlcdc_plane_rgb_formats,
240 .regs_offset = 0x440,
242 .type = ATMEL_HLCDC_CURSOR_LAYER,
253 .chroma_key_mask = 8,
257 .clut_offset = 0x1600,
261 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d3 = {
269 .conflicting_output_formats = true,
270 .nlayers = ARRAY_SIZE(atmel_hlcdc_sama5d3_layers),
271 .layers = atmel_hlcdc_sama5d3_layers,
274 static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d4_layers[] = {
277 .formats = &atmel_hlcdc_plane_rgb_formats,
280 .type = ATMEL_HLCDC_BASE_LAYER,
289 .clut_offset = 0x600,
293 .formats = &atmel_hlcdc_plane_rgb_formats,
294 .regs_offset = 0x140,
296 .type = ATMEL_HLCDC_OVERLAY_LAYER,
305 .chroma_key_mask = 8,
308 .clut_offset = 0xa00,
312 .formats = &atmel_hlcdc_plane_rgb_formats,
313 .regs_offset = 0x240,
315 .type = ATMEL_HLCDC_OVERLAY_LAYER,
324 .chroma_key_mask = 8,
327 .clut_offset = 0xe00,
330 .name = "high-end-overlay",
331 .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
332 .regs_offset = 0x340,
334 .type = ATMEL_HLCDC_OVERLAY_LAYER,
344 .chroma_key_mask = 11,
345 .general_config = 12,
353 .clut_offset = 0x1200,
357 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d4 = {
365 .nlayers = ARRAY_SIZE(atmel_hlcdc_sama5d4_layers),
366 .layers = atmel_hlcdc_sama5d4_layers,
369 static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sam9x60_layers[] = {
372 .formats = &atmel_hlcdc_plane_rgb_formats,
375 .type = ATMEL_HLCDC_BASE_LAYER,
384 .clut_offset = 0x600,
388 .formats = &atmel_hlcdc_plane_rgb_formats,
389 .regs_offset = 0x160,
391 .type = ATMEL_HLCDC_OVERLAY_LAYER,
400 .chroma_key_mask = 8,
403 .clut_offset = 0xa00,
407 .formats = &atmel_hlcdc_plane_rgb_formats,
408 .regs_offset = 0x260,
410 .type = ATMEL_HLCDC_OVERLAY_LAYER,
419 .chroma_key_mask = 8,
422 .clut_offset = 0xe00,
425 .name = "high-end-overlay",
426 .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
427 .regs_offset = 0x360,
429 .type = ATMEL_HLCDC_OVERLAY_LAYER,
439 .chroma_key_mask = 11,
440 .general_config = 12,
448 .clut_offset = 0x1200,
452 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sam9x60 = {
460 .fixed_clksrc = true,
461 .nlayers = ARRAY_SIZE(atmel_hlcdc_sam9x60_layers),
462 .layers = atmel_hlcdc_sam9x60_layers,
465 static const struct of_device_id atmel_hlcdc_of_match[] = {
467 .compatible = "atmel,at91sam9n12-hlcdc",
468 .data = &atmel_hlcdc_dc_at91sam9n12,
471 .compatible = "atmel,at91sam9x5-hlcdc",
472 .data = &atmel_hlcdc_dc_at91sam9x5,
475 .compatible = "atmel,sama5d2-hlcdc",
476 .data = &atmel_hlcdc_dc_sama5d4,
479 .compatible = "atmel,sama5d3-hlcdc",
480 .data = &atmel_hlcdc_dc_sama5d3,
483 .compatible = "atmel,sama5d4-hlcdc",
484 .data = &atmel_hlcdc_dc_sama5d4,
487 .compatible = "microchip,sam9x60-hlcdc",
488 .data = &atmel_hlcdc_dc_sam9x60,
492 MODULE_DEVICE_TABLE(of, atmel_hlcdc_of_match);
495 atmel_hlcdc_dc_mode_valid(struct atmel_hlcdc_dc *dc,
496 const struct drm_display_mode *mode)
498 int vfront_porch = mode->vsync_start - mode->vdisplay;
499 int vback_porch = mode->vtotal - mode->vsync_end;
500 int vsync_len = mode->vsync_end - mode->vsync_start;
501 int hfront_porch = mode->hsync_start - mode->hdisplay;
502 int hback_porch = mode->htotal - mode->hsync_end;
503 int hsync_len = mode->hsync_end - mode->hsync_start;
505 if (hsync_len > dc->desc->max_spw + 1 || hsync_len < 1)
508 if (vsync_len > dc->desc->max_spw + 1 || vsync_len < 1)
511 if (hfront_porch > dc->desc->max_hpw + 1 || hfront_porch < 1 ||
512 hback_porch > dc->desc->max_hpw + 1 || hback_porch < 1 ||
514 return MODE_H_ILLEGAL;
516 if (vfront_porch > dc->desc->max_vpw + 1 || vfront_porch < 1 ||
517 vback_porch > dc->desc->max_vpw || vback_porch < 0 ||
519 return MODE_V_ILLEGAL;
524 static void atmel_hlcdc_layer_irq(struct atmel_hlcdc_layer *layer)
529 if (layer->desc->type == ATMEL_HLCDC_BASE_LAYER ||
530 layer->desc->type == ATMEL_HLCDC_OVERLAY_LAYER ||
531 layer->desc->type == ATMEL_HLCDC_CURSOR_LAYER)
532 atmel_hlcdc_plane_irq(atmel_hlcdc_layer_to_plane(layer));
535 static irqreturn_t atmel_hlcdc_dc_irq_handler(int irq, void *data)
537 struct drm_device *dev = data;
538 struct atmel_hlcdc_dc *dc = dev->dev_private;
539 unsigned long status;
540 unsigned int imr, isr;
543 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_IMR, &imr);
544 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
549 if (status & ATMEL_HLCDC_SOF)
550 atmel_hlcdc_crtc_irq(dc->crtc);
552 for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
553 if (ATMEL_HLCDC_LAYER_STATUS(i) & status)
554 atmel_hlcdc_layer_irq(dc->layers[i]);
560 struct atmel_hlcdc_dc_commit {
561 struct work_struct work;
562 struct drm_device *dev;
563 struct drm_atomic_state *state;
567 atmel_hlcdc_dc_atomic_complete(struct atmel_hlcdc_dc_commit *commit)
569 struct drm_device *dev = commit->dev;
570 struct atmel_hlcdc_dc *dc = dev->dev_private;
571 struct drm_atomic_state *old_state = commit->state;
573 /* Apply the atomic update. */
574 drm_atomic_helper_commit_modeset_disables(dev, old_state);
575 drm_atomic_helper_commit_planes(dev, old_state, 0);
576 drm_atomic_helper_commit_modeset_enables(dev, old_state);
578 drm_atomic_helper_wait_for_vblanks(dev, old_state);
580 drm_atomic_helper_cleanup_planes(dev, old_state);
582 drm_atomic_state_put(old_state);
584 /* Complete the commit, wake up any waiter. */
585 spin_lock(&dc->commit.wait.lock);
586 dc->commit.pending = false;
587 wake_up_all_locked(&dc->commit.wait);
588 spin_unlock(&dc->commit.wait.lock);
593 static void atmel_hlcdc_dc_atomic_work(struct work_struct *work)
595 struct atmel_hlcdc_dc_commit *commit =
596 container_of(work, struct atmel_hlcdc_dc_commit, work);
598 atmel_hlcdc_dc_atomic_complete(commit);
601 static int atmel_hlcdc_dc_atomic_commit(struct drm_device *dev,
602 struct drm_atomic_state *state,
605 struct atmel_hlcdc_dc *dc = dev->dev_private;
606 struct atmel_hlcdc_dc_commit *commit;
609 ret = drm_atomic_helper_prepare_planes(dev, state);
613 /* Allocate the commit object. */
614 commit = kzalloc(sizeof(*commit), GFP_KERNEL);
620 INIT_WORK(&commit->work, atmel_hlcdc_dc_atomic_work);
622 commit->state = state;
624 spin_lock(&dc->commit.wait.lock);
625 ret = wait_event_interruptible_locked(dc->commit.wait,
626 !dc->commit.pending);
628 dc->commit.pending = true;
629 spin_unlock(&dc->commit.wait.lock);
634 /* We have our own synchronization through the commit lock. */
635 BUG_ON(drm_atomic_helper_swap_state(state, false) < 0);
637 /* Swap state succeeded, this is the point of no return. */
638 drm_atomic_state_get(state);
640 queue_work(dc->wq, &commit->work);
642 atmel_hlcdc_dc_atomic_complete(commit);
649 drm_atomic_helper_cleanup_planes(dev, state);
653 static const struct drm_mode_config_funcs mode_config_funcs = {
654 .fb_create = drm_gem_fb_create,
655 .atomic_check = drm_atomic_helper_check,
656 .atomic_commit = atmel_hlcdc_dc_atomic_commit,
659 static int atmel_hlcdc_dc_modeset_init(struct drm_device *dev)
661 struct atmel_hlcdc_dc *dc = dev->dev_private;
664 drm_mode_config_init(dev);
666 ret = atmel_hlcdc_create_outputs(dev);
668 dev_err(dev->dev, "failed to create HLCDC outputs: %d\n", ret);
672 ret = atmel_hlcdc_create_planes(dev);
674 dev_err(dev->dev, "failed to create planes: %d\n", ret);
678 ret = atmel_hlcdc_crtc_create(dev);
680 dev_err(dev->dev, "failed to create crtc\n");
684 dev->mode_config.min_width = dc->desc->min_width;
685 dev->mode_config.min_height = dc->desc->min_height;
686 dev->mode_config.max_width = dc->desc->max_width;
687 dev->mode_config.max_height = dc->desc->max_height;
688 dev->mode_config.funcs = &mode_config_funcs;
693 static int atmel_hlcdc_dc_load(struct drm_device *dev)
695 struct platform_device *pdev = to_platform_device(dev->dev);
696 const struct of_device_id *match;
697 struct atmel_hlcdc_dc *dc;
700 match = of_match_node(atmel_hlcdc_of_match, dev->dev->parent->of_node);
702 dev_err(&pdev->dev, "invalid compatible string\n");
707 dev_err(&pdev->dev, "invalid hlcdc description\n");
711 dc = devm_kzalloc(dev->dev, sizeof(*dc), GFP_KERNEL);
715 dc->wq = alloc_ordered_workqueue("atmel-hlcdc-dc", 0);
719 init_waitqueue_head(&dc->commit.wait);
720 dc->desc = match->data;
721 dc->hlcdc = dev_get_drvdata(dev->dev->parent);
722 dev->dev_private = dc;
724 ret = clk_prepare_enable(dc->hlcdc->periph_clk);
726 dev_err(dev->dev, "failed to enable periph_clk\n");
730 pm_runtime_enable(dev->dev);
732 ret = drm_vblank_init(dev, 1);
734 dev_err(dev->dev, "failed to initialize vblank\n");
735 goto err_periph_clk_disable;
738 ret = atmel_hlcdc_dc_modeset_init(dev);
740 dev_err(dev->dev, "failed to initialize mode setting\n");
741 goto err_periph_clk_disable;
744 drm_mode_config_reset(dev);
746 pm_runtime_get_sync(dev->dev);
747 ret = drm_irq_install(dev, dc->hlcdc->irq);
748 pm_runtime_put_sync(dev->dev);
750 dev_err(dev->dev, "failed to install IRQ handler\n");
751 goto err_periph_clk_disable;
754 platform_set_drvdata(pdev, dev);
756 drm_kms_helper_poll_init(dev);
760 err_periph_clk_disable:
761 pm_runtime_disable(dev->dev);
762 clk_disable_unprepare(dc->hlcdc->periph_clk);
765 destroy_workqueue(dc->wq);
770 static void atmel_hlcdc_dc_unload(struct drm_device *dev)
772 struct atmel_hlcdc_dc *dc = dev->dev_private;
774 flush_workqueue(dc->wq);
775 drm_kms_helper_poll_fini(dev);
776 drm_atomic_helper_shutdown(dev);
777 drm_mode_config_cleanup(dev);
779 pm_runtime_get_sync(dev->dev);
780 drm_irq_uninstall(dev);
781 pm_runtime_put_sync(dev->dev);
783 dev->dev_private = NULL;
785 pm_runtime_disable(dev->dev);
786 clk_disable_unprepare(dc->hlcdc->periph_clk);
787 destroy_workqueue(dc->wq);
790 static int atmel_hlcdc_dc_irq_postinstall(struct drm_device *dev)
792 struct atmel_hlcdc_dc *dc = dev->dev_private;
793 unsigned int cfg = 0;
796 /* Enable interrupts on activated layers */
797 for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
799 cfg |= ATMEL_HLCDC_LAYER_STATUS(i);
802 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, cfg);
807 static void atmel_hlcdc_dc_irq_uninstall(struct drm_device *dev)
809 struct atmel_hlcdc_dc *dc = dev->dev_private;
812 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, 0xffffffff);
813 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
816 DEFINE_DRM_GEM_CMA_FOPS(fops);
818 static struct drm_driver atmel_hlcdc_dc_driver = {
819 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
820 .irq_handler = atmel_hlcdc_dc_irq_handler,
821 .irq_preinstall = atmel_hlcdc_dc_irq_uninstall,
822 .irq_postinstall = atmel_hlcdc_dc_irq_postinstall,
823 .irq_uninstall = atmel_hlcdc_dc_irq_uninstall,
824 .gem_free_object_unlocked = drm_gem_cma_free_object,
825 .gem_vm_ops = &drm_gem_cma_vm_ops,
826 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
827 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
828 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
829 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
830 .gem_prime_vmap = drm_gem_cma_prime_vmap,
831 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
832 .gem_prime_mmap = drm_gem_cma_prime_mmap,
833 .dumb_create = drm_gem_cma_dumb_create,
835 .name = "atmel-hlcdc",
836 .desc = "Atmel HLCD Controller DRM",
842 static int atmel_hlcdc_dc_drm_probe(struct platform_device *pdev)
844 struct drm_device *ddev;
847 ddev = drm_dev_alloc(&atmel_hlcdc_dc_driver, &pdev->dev);
849 return PTR_ERR(ddev);
851 ret = atmel_hlcdc_dc_load(ddev);
855 ret = drm_dev_register(ddev, 0);
859 drm_fbdev_generic_setup(ddev, 24);
864 atmel_hlcdc_dc_unload(ddev);
872 static int atmel_hlcdc_dc_drm_remove(struct platform_device *pdev)
874 struct drm_device *ddev = platform_get_drvdata(pdev);
876 drm_dev_unregister(ddev);
877 atmel_hlcdc_dc_unload(ddev);
883 #ifdef CONFIG_PM_SLEEP
884 static int atmel_hlcdc_dc_drm_suspend(struct device *dev)
886 struct drm_device *drm_dev = dev_get_drvdata(dev);
887 struct atmel_hlcdc_dc *dc = drm_dev->dev_private;
888 struct regmap *regmap = dc->hlcdc->regmap;
889 struct drm_atomic_state *state;
891 state = drm_atomic_helper_suspend(drm_dev);
893 return PTR_ERR(state);
895 dc->suspend.state = state;
897 regmap_read(regmap, ATMEL_HLCDC_IMR, &dc->suspend.imr);
898 regmap_write(regmap, ATMEL_HLCDC_IDR, dc->suspend.imr);
899 clk_disable_unprepare(dc->hlcdc->periph_clk);
904 static int atmel_hlcdc_dc_drm_resume(struct device *dev)
906 struct drm_device *drm_dev = dev_get_drvdata(dev);
907 struct atmel_hlcdc_dc *dc = drm_dev->dev_private;
909 clk_prepare_enable(dc->hlcdc->periph_clk);
910 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, dc->suspend.imr);
912 return drm_atomic_helper_resume(drm_dev, dc->suspend.state);
916 static SIMPLE_DEV_PM_OPS(atmel_hlcdc_dc_drm_pm_ops,
917 atmel_hlcdc_dc_drm_suspend, atmel_hlcdc_dc_drm_resume);
919 static const struct of_device_id atmel_hlcdc_dc_of_match[] = {
920 { .compatible = "atmel,hlcdc-display-controller" },
924 static struct platform_driver atmel_hlcdc_dc_platform_driver = {
925 .probe = atmel_hlcdc_dc_drm_probe,
926 .remove = atmel_hlcdc_dc_drm_remove,
928 .name = "atmel-hlcdc-display-controller",
929 .pm = &atmel_hlcdc_dc_drm_pm_ops,
930 .of_match_table = atmel_hlcdc_dc_of_match,
933 module_platform_driver(atmel_hlcdc_dc_platform_driver);
937 MODULE_DESCRIPTION("Atmel HLCDC Display Controller DRM Driver");
938 MODULE_LICENSE("GPL");
939 MODULE_ALIAS("platform:atmel-hlcdc-dc");