1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 Traphandler
4 * Copyright (C) 2014 Free Electrons
5 * Copyright (C) 2014 Atmel
7 * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
8 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
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_probe_helper.h>
26 #include <drm/drm_vblank.h>
28 #include "atmel_hlcdc_dc.h"
30 #define ATMEL_HLCDC_LAYER_IRQS_OFFSET 8
32 static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9n12_layers[] = {
35 .formats = &atmel_hlcdc_plane_rgb_formats,
38 .type = ATMEL_HLCDC_BASE_LAYER,
49 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_at91sam9n12 = {
57 .conflicting_output_formats = true,
58 .nlayers = ARRAY_SIZE(atmel_hlcdc_at91sam9n12_layers),
59 .layers = atmel_hlcdc_at91sam9n12_layers,
62 static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9x5_layers[] = {
65 .formats = &atmel_hlcdc_plane_rgb_formats,
68 .type = ATMEL_HLCDC_BASE_LAYER,
81 .formats = &atmel_hlcdc_plane_rgb_formats,
84 .type = ATMEL_HLCDC_OVERLAY_LAYER,
99 .name = "high-end-overlay",
100 .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
101 .regs_offset = 0x280,
103 .type = ATMEL_HLCDC_OVERLAY_LAYER,
113 .chroma_key_mask = 11,
114 .general_config = 12,
118 .clut_offset = 0x1000,
122 .formats = &atmel_hlcdc_plane_rgb_formats,
123 .regs_offset = 0x340,
125 .type = ATMEL_HLCDC_CURSOR_LAYER,
135 .chroma_key_mask = 8,
138 .clut_offset = 0x1400,
142 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_at91sam9x5 = {
150 .conflicting_output_formats = true,
151 .nlayers = ARRAY_SIZE(atmel_hlcdc_at91sam9x5_layers),
152 .layers = atmel_hlcdc_at91sam9x5_layers,
155 static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d3_layers[] = {
158 .formats = &atmel_hlcdc_plane_rgb_formats,
161 .type = ATMEL_HLCDC_BASE_LAYER,
170 .clut_offset = 0x600,
174 .formats = &atmel_hlcdc_plane_rgb_formats,
175 .regs_offset = 0x140,
177 .type = ATMEL_HLCDC_OVERLAY_LAYER,
186 .chroma_key_mask = 8,
189 .clut_offset = 0xa00,
193 .formats = &atmel_hlcdc_plane_rgb_formats,
194 .regs_offset = 0x240,
196 .type = ATMEL_HLCDC_OVERLAY_LAYER,
205 .chroma_key_mask = 8,
208 .clut_offset = 0xe00,
211 .name = "high-end-overlay",
212 .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
213 .regs_offset = 0x340,
215 .type = ATMEL_HLCDC_OVERLAY_LAYER,
225 .chroma_key_mask = 11,
226 .general_config = 12,
234 .clut_offset = 0x1200,
238 .formats = &atmel_hlcdc_plane_rgb_formats,
239 .regs_offset = 0x440,
241 .type = ATMEL_HLCDC_CURSOR_LAYER,
252 .chroma_key_mask = 8,
256 .clut_offset = 0x1600,
260 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d3 = {
268 .conflicting_output_formats = true,
269 .nlayers = ARRAY_SIZE(atmel_hlcdc_sama5d3_layers),
270 .layers = atmel_hlcdc_sama5d3_layers,
273 static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d4_layers[] = {
276 .formats = &atmel_hlcdc_plane_rgb_formats,
279 .type = ATMEL_HLCDC_BASE_LAYER,
288 .clut_offset = 0x600,
292 .formats = &atmel_hlcdc_plane_rgb_formats,
293 .regs_offset = 0x140,
295 .type = ATMEL_HLCDC_OVERLAY_LAYER,
304 .chroma_key_mask = 8,
307 .clut_offset = 0xa00,
311 .formats = &atmel_hlcdc_plane_rgb_formats,
312 .regs_offset = 0x240,
314 .type = ATMEL_HLCDC_OVERLAY_LAYER,
323 .chroma_key_mask = 8,
326 .clut_offset = 0xe00,
329 .name = "high-end-overlay",
330 .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
331 .regs_offset = 0x340,
333 .type = ATMEL_HLCDC_OVERLAY_LAYER,
343 .chroma_key_mask = 11,
344 .general_config = 12,
352 .clut_offset = 0x1200,
356 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d4 = {
364 .nlayers = ARRAY_SIZE(atmel_hlcdc_sama5d4_layers),
365 .layers = atmel_hlcdc_sama5d4_layers,
368 static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sam9x60_layers[] = {
371 .formats = &atmel_hlcdc_plane_rgb_formats,
374 .type = ATMEL_HLCDC_BASE_LAYER,
383 .clut_offset = 0x600,
387 .formats = &atmel_hlcdc_plane_rgb_formats,
388 .regs_offset = 0x160,
390 .type = ATMEL_HLCDC_OVERLAY_LAYER,
399 .chroma_key_mask = 8,
402 .clut_offset = 0xa00,
406 .formats = &atmel_hlcdc_plane_rgb_formats,
407 .regs_offset = 0x260,
409 .type = ATMEL_HLCDC_OVERLAY_LAYER,
418 .chroma_key_mask = 8,
421 .clut_offset = 0xe00,
424 .name = "high-end-overlay",
425 .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
426 .regs_offset = 0x360,
428 .type = ATMEL_HLCDC_OVERLAY_LAYER,
438 .chroma_key_mask = 11,
439 .general_config = 12,
447 .clut_offset = 0x1200,
451 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sam9x60 = {
459 .fixed_clksrc = true,
460 .nlayers = ARRAY_SIZE(atmel_hlcdc_sam9x60_layers),
461 .layers = atmel_hlcdc_sam9x60_layers,
464 static const struct of_device_id atmel_hlcdc_of_match[] = {
466 .compatible = "atmel,at91sam9n12-hlcdc",
467 .data = &atmel_hlcdc_dc_at91sam9n12,
470 .compatible = "atmel,at91sam9x5-hlcdc",
471 .data = &atmel_hlcdc_dc_at91sam9x5,
474 .compatible = "atmel,sama5d2-hlcdc",
475 .data = &atmel_hlcdc_dc_sama5d4,
478 .compatible = "atmel,sama5d3-hlcdc",
479 .data = &atmel_hlcdc_dc_sama5d3,
482 .compatible = "atmel,sama5d4-hlcdc",
483 .data = &atmel_hlcdc_dc_sama5d4,
486 .compatible = "microchip,sam9x60-hlcdc",
487 .data = &atmel_hlcdc_dc_sam9x60,
491 MODULE_DEVICE_TABLE(of, atmel_hlcdc_of_match);
494 atmel_hlcdc_dc_mode_valid(struct atmel_hlcdc_dc *dc,
495 const struct drm_display_mode *mode)
497 int vfront_porch = mode->vsync_start - mode->vdisplay;
498 int vback_porch = mode->vtotal - mode->vsync_end;
499 int vsync_len = mode->vsync_end - mode->vsync_start;
500 int hfront_porch = mode->hsync_start - mode->hdisplay;
501 int hback_porch = mode->htotal - mode->hsync_end;
502 int hsync_len = mode->hsync_end - mode->hsync_start;
504 if (hsync_len > dc->desc->max_spw + 1 || hsync_len < 1)
507 if (vsync_len > dc->desc->max_spw + 1 || vsync_len < 1)
510 if (hfront_porch > dc->desc->max_hpw + 1 || hfront_porch < 1 ||
511 hback_porch > dc->desc->max_hpw + 1 || hback_porch < 1 ||
513 return MODE_H_ILLEGAL;
515 if (vfront_porch > dc->desc->max_vpw + 1 || vfront_porch < 1 ||
516 vback_porch > dc->desc->max_vpw || vback_porch < 0 ||
518 return MODE_V_ILLEGAL;
523 static void atmel_hlcdc_layer_irq(struct atmel_hlcdc_layer *layer)
528 if (layer->desc->type == ATMEL_HLCDC_BASE_LAYER ||
529 layer->desc->type == ATMEL_HLCDC_OVERLAY_LAYER ||
530 layer->desc->type == ATMEL_HLCDC_CURSOR_LAYER)
531 atmel_hlcdc_plane_irq(atmel_hlcdc_layer_to_plane(layer));
534 static irqreturn_t atmel_hlcdc_dc_irq_handler(int irq, void *data)
536 struct drm_device *dev = data;
537 struct atmel_hlcdc_dc *dc = dev->dev_private;
538 unsigned long status;
539 unsigned int imr, isr;
542 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_IMR, &imr);
543 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
548 if (status & ATMEL_HLCDC_SOF)
549 atmel_hlcdc_crtc_irq(dc->crtc);
551 for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
552 if (ATMEL_HLCDC_LAYER_STATUS(i) & status)
553 atmel_hlcdc_layer_irq(dc->layers[i]);
559 static void atmel_hlcdc_dc_irq_postinstall(struct drm_device *dev)
561 struct atmel_hlcdc_dc *dc = dev->dev_private;
562 unsigned int cfg = 0;
565 /* Enable interrupts on activated layers */
566 for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
568 cfg |= ATMEL_HLCDC_LAYER_STATUS(i);
571 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, cfg);
574 static void atmel_hlcdc_dc_irq_disable(struct drm_device *dev)
576 struct atmel_hlcdc_dc *dc = dev->dev_private;
579 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, 0xffffffff);
580 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
583 static int atmel_hlcdc_dc_irq_install(struct drm_device *dev, unsigned int irq)
587 atmel_hlcdc_dc_irq_disable(dev);
589 ret = devm_request_irq(dev->dev, irq, atmel_hlcdc_dc_irq_handler, 0,
590 dev->driver->name, dev);
594 atmel_hlcdc_dc_irq_postinstall(dev);
599 static void atmel_hlcdc_dc_irq_uninstall(struct drm_device *dev)
601 atmel_hlcdc_dc_irq_disable(dev);
604 static const struct drm_mode_config_funcs mode_config_funcs = {
605 .fb_create = drm_gem_fb_create,
606 .atomic_check = drm_atomic_helper_check,
607 .atomic_commit = drm_atomic_helper_commit,
610 static int atmel_hlcdc_dc_modeset_init(struct drm_device *dev)
612 struct atmel_hlcdc_dc *dc = dev->dev_private;
615 drm_mode_config_init(dev);
617 ret = atmel_hlcdc_create_outputs(dev);
619 dev_err(dev->dev, "failed to create HLCDC outputs: %d\n", ret);
623 ret = atmel_hlcdc_create_planes(dev);
625 dev_err(dev->dev, "failed to create planes: %d\n", ret);
629 ret = atmel_hlcdc_crtc_create(dev);
631 dev_err(dev->dev, "failed to create crtc\n");
635 dev->mode_config.min_width = dc->desc->min_width;
636 dev->mode_config.min_height = dc->desc->min_height;
637 dev->mode_config.max_width = dc->desc->max_width;
638 dev->mode_config.max_height = dc->desc->max_height;
639 dev->mode_config.funcs = &mode_config_funcs;
640 dev->mode_config.async_page_flip = true;
645 static int atmel_hlcdc_dc_load(struct drm_device *dev)
647 struct platform_device *pdev = to_platform_device(dev->dev);
648 const struct of_device_id *match;
649 struct atmel_hlcdc_dc *dc;
652 match = of_match_node(atmel_hlcdc_of_match, dev->dev->parent->of_node);
654 dev_err(&pdev->dev, "invalid compatible string\n");
659 dev_err(&pdev->dev, "invalid hlcdc description\n");
663 dc = devm_kzalloc(dev->dev, sizeof(*dc), GFP_KERNEL);
667 dc->desc = match->data;
668 dc->hlcdc = dev_get_drvdata(dev->dev->parent);
669 dev->dev_private = dc;
671 ret = clk_prepare_enable(dc->hlcdc->periph_clk);
673 dev_err(dev->dev, "failed to enable periph_clk\n");
677 pm_runtime_enable(dev->dev);
679 ret = drm_vblank_init(dev, 1);
681 dev_err(dev->dev, "failed to initialize vblank\n");
682 goto err_periph_clk_disable;
685 ret = atmel_hlcdc_dc_modeset_init(dev);
687 dev_err(dev->dev, "failed to initialize mode setting\n");
688 goto err_periph_clk_disable;
691 drm_mode_config_reset(dev);
693 pm_runtime_get_sync(dev->dev);
694 ret = atmel_hlcdc_dc_irq_install(dev, dc->hlcdc->irq);
695 pm_runtime_put_sync(dev->dev);
697 dev_err(dev->dev, "failed to install IRQ handler\n");
698 goto err_periph_clk_disable;
701 platform_set_drvdata(pdev, dev);
703 drm_kms_helper_poll_init(dev);
707 err_periph_clk_disable:
708 pm_runtime_disable(dev->dev);
709 clk_disable_unprepare(dc->hlcdc->periph_clk);
714 static void atmel_hlcdc_dc_unload(struct drm_device *dev)
716 struct atmel_hlcdc_dc *dc = dev->dev_private;
718 drm_kms_helper_poll_fini(dev);
719 drm_atomic_helper_shutdown(dev);
720 drm_mode_config_cleanup(dev);
722 pm_runtime_get_sync(dev->dev);
723 atmel_hlcdc_dc_irq_uninstall(dev);
724 pm_runtime_put_sync(dev->dev);
726 dev->dev_private = NULL;
728 pm_runtime_disable(dev->dev);
729 clk_disable_unprepare(dc->hlcdc->periph_clk);
732 DEFINE_DRM_GEM_CMA_FOPS(fops);
734 static const struct drm_driver atmel_hlcdc_dc_driver = {
735 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
736 DRM_GEM_CMA_DRIVER_OPS,
738 .name = "atmel-hlcdc",
739 .desc = "Atmel HLCD Controller DRM",
745 static int atmel_hlcdc_dc_drm_probe(struct platform_device *pdev)
747 struct drm_device *ddev;
750 ddev = drm_dev_alloc(&atmel_hlcdc_dc_driver, &pdev->dev);
752 return PTR_ERR(ddev);
754 ret = atmel_hlcdc_dc_load(ddev);
758 ret = drm_dev_register(ddev, 0);
762 drm_fbdev_generic_setup(ddev, 24);
767 atmel_hlcdc_dc_unload(ddev);
775 static int atmel_hlcdc_dc_drm_remove(struct platform_device *pdev)
777 struct drm_device *ddev = platform_get_drvdata(pdev);
779 drm_dev_unregister(ddev);
780 atmel_hlcdc_dc_unload(ddev);
786 #ifdef CONFIG_PM_SLEEP
787 static int atmel_hlcdc_dc_drm_suspend(struct device *dev)
789 struct drm_device *drm_dev = dev_get_drvdata(dev);
790 struct atmel_hlcdc_dc *dc = drm_dev->dev_private;
791 struct regmap *regmap = dc->hlcdc->regmap;
792 struct drm_atomic_state *state;
794 state = drm_atomic_helper_suspend(drm_dev);
796 return PTR_ERR(state);
798 dc->suspend.state = state;
800 regmap_read(regmap, ATMEL_HLCDC_IMR, &dc->suspend.imr);
801 regmap_write(regmap, ATMEL_HLCDC_IDR, dc->suspend.imr);
802 clk_disable_unprepare(dc->hlcdc->periph_clk);
807 static int atmel_hlcdc_dc_drm_resume(struct device *dev)
809 struct drm_device *drm_dev = dev_get_drvdata(dev);
810 struct atmel_hlcdc_dc *dc = drm_dev->dev_private;
812 clk_prepare_enable(dc->hlcdc->periph_clk);
813 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, dc->suspend.imr);
815 return drm_atomic_helper_resume(drm_dev, dc->suspend.state);
819 static SIMPLE_DEV_PM_OPS(atmel_hlcdc_dc_drm_pm_ops,
820 atmel_hlcdc_dc_drm_suspend, atmel_hlcdc_dc_drm_resume);
822 static const struct of_device_id atmel_hlcdc_dc_of_match[] = {
823 { .compatible = "atmel,hlcdc-display-controller" },
827 static struct platform_driver atmel_hlcdc_dc_platform_driver = {
828 .probe = atmel_hlcdc_dc_drm_probe,
829 .remove = atmel_hlcdc_dc_drm_remove,
831 .name = "atmel-hlcdc-display-controller",
832 .pm = &atmel_hlcdc_dc_drm_pm_ops,
833 .of_match_table = atmel_hlcdc_dc_of_match,
836 module_platform_driver(atmel_hlcdc_dc_platform_driver);
838 MODULE_AUTHOR("Jean-Jacques Hiblot <jjhiblot@traphandler.com>");
839 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
840 MODULE_DESCRIPTION("Atmel HLCDC Display Controller DRM Driver");
841 MODULE_LICENSE("GPL");
842 MODULE_ALIAS("platform:atmel-hlcdc-dc");