2 * Copyright (C) 2014 Traphandler
3 * Copyright (C) 2014 Free Electrons
4 * Copyright (C) 2014 Atmel
6 * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
7 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/clk.h>
23 #include <linux/irq.h>
24 #include <linux/irqchip.h>
25 #include <linux/module.h>
26 #include <linux/pm_runtime.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
,
48 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_at91sam9n12
= {
53 .nlayers
= ARRAY_SIZE(atmel_hlcdc_at91sam9n12_layers
),
54 .layers
= atmel_hlcdc_at91sam9n12_layers
,
57 static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9x5_layers
[] = {
60 .formats
= &atmel_hlcdc_plane_rgb_formats
,
63 .type
= ATMEL_HLCDC_BASE_LAYER
,
75 .formats
= &atmel_hlcdc_plane_rgb_formats
,
78 .type
= ATMEL_HLCDC_OVERLAY_LAYER
,
92 .name
= "high-end-overlay",
93 .formats
= &atmel_hlcdc_plane_rgb_and_yuv_formats
,
96 .type
= ATMEL_HLCDC_OVERLAY_LAYER
,
106 .chroma_key_mask
= 11,
107 .general_config
= 12,
113 .formats
= &atmel_hlcdc_plane_rgb_formats
,
114 .regs_offset
= 0x340,
116 .type
= ATMEL_HLCDC_CURSOR_LAYER
,
126 .chroma_key_mask
= 8,
132 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_at91sam9x5
= {
137 .nlayers
= ARRAY_SIZE(atmel_hlcdc_at91sam9x5_layers
),
138 .layers
= atmel_hlcdc_at91sam9x5_layers
,
141 static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d3_layers
[] = {
144 .formats
= &atmel_hlcdc_plane_rgb_formats
,
147 .type
= ATMEL_HLCDC_BASE_LAYER
,
159 .formats
= &atmel_hlcdc_plane_rgb_formats
,
160 .regs_offset
= 0x140,
162 .type
= ATMEL_HLCDC_OVERLAY_LAYER
,
171 .chroma_key_mask
= 8,
177 .formats
= &atmel_hlcdc_plane_rgb_formats
,
178 .regs_offset
= 0x240,
180 .type
= ATMEL_HLCDC_OVERLAY_LAYER
,
189 .chroma_key_mask
= 8,
194 .name
= "high-end-overlay",
195 .formats
= &atmel_hlcdc_plane_rgb_and_yuv_formats
,
196 .regs_offset
= 0x340,
198 .type
= ATMEL_HLCDC_OVERLAY_LAYER
,
208 .chroma_key_mask
= 11,
209 .general_config
= 12,
215 .formats
= &atmel_hlcdc_plane_rgb_formats
,
216 .regs_offset
= 0x440,
218 .type
= ATMEL_HLCDC_CURSOR_LAYER
,
229 .chroma_key_mask
= 8,
235 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d3
= {
240 .nlayers
= ARRAY_SIZE(atmel_hlcdc_sama5d3_layers
),
241 .layers
= atmel_hlcdc_sama5d3_layers
,
244 static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d4_layers
[] = {
247 .formats
= &atmel_hlcdc_plane_rgb_formats
,
250 .type
= ATMEL_HLCDC_BASE_LAYER
,
262 .formats
= &atmel_hlcdc_plane_rgb_formats
,
263 .regs_offset
= 0x140,
265 .type
= ATMEL_HLCDC_OVERLAY_LAYER
,
274 .chroma_key_mask
= 8,
280 .formats
= &atmel_hlcdc_plane_rgb_formats
,
281 .regs_offset
= 0x240,
283 .type
= ATMEL_HLCDC_OVERLAY_LAYER
,
292 .chroma_key_mask
= 8,
297 .name
= "high-end-overlay",
298 .formats
= &atmel_hlcdc_plane_rgb_and_yuv_formats
,
299 .regs_offset
= 0x340,
301 .type
= ATMEL_HLCDC_OVERLAY_LAYER
,
311 .chroma_key_mask
= 11,
312 .general_config
= 12,
318 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d4
= {
323 .nlayers
= ARRAY_SIZE(atmel_hlcdc_sama5d4_layers
),
324 .layers
= atmel_hlcdc_sama5d4_layers
,
326 static const struct of_device_id atmel_hlcdc_of_match
[] = {
328 .compatible
= "atmel,at91sam9n12-hlcdc",
329 .data
= &atmel_hlcdc_dc_at91sam9n12
,
332 .compatible
= "atmel,at91sam9x5-hlcdc",
333 .data
= &atmel_hlcdc_dc_at91sam9x5
,
336 .compatible
= "atmel,sama5d2-hlcdc",
337 .data
= &atmel_hlcdc_dc_sama5d4
,
340 .compatible
= "atmel,sama5d3-hlcdc",
341 .data
= &atmel_hlcdc_dc_sama5d3
,
344 .compatible
= "atmel,sama5d4-hlcdc",
345 .data
= &atmel_hlcdc_dc_sama5d4
,
349 MODULE_DEVICE_TABLE(of
, atmel_hlcdc_of_match
);
351 int atmel_hlcdc_dc_mode_valid(struct atmel_hlcdc_dc
*dc
,
352 struct drm_display_mode
*mode
)
354 int vfront_porch
= mode
->vsync_start
- mode
->vdisplay
;
355 int vback_porch
= mode
->vtotal
- mode
->vsync_end
;
356 int vsync_len
= mode
->vsync_end
- mode
->vsync_start
;
357 int hfront_porch
= mode
->hsync_start
- mode
->hdisplay
;
358 int hback_porch
= mode
->htotal
- mode
->hsync_end
;
359 int hsync_len
= mode
->hsync_end
- mode
->hsync_start
;
361 if (hsync_len
> 0x40 || hsync_len
< 1)
364 if (vsync_len
> 0x40 || vsync_len
< 1)
367 if (hfront_porch
> 0x200 || hfront_porch
< 1 ||
368 hback_porch
> 0x200 || hback_porch
< 1 ||
370 return MODE_H_ILLEGAL
;
372 if (vfront_porch
> 0x40 || vfront_porch
< 1 ||
373 vback_porch
> 0x40 || vback_porch
< 0 ||
375 return MODE_V_ILLEGAL
;
380 static irqreturn_t
atmel_hlcdc_dc_irq_handler(int irq
, void *data
)
382 struct drm_device
*dev
= data
;
383 struct atmel_hlcdc_dc
*dc
= dev
->dev_private
;
384 unsigned long status
;
385 unsigned int imr
, isr
;
388 regmap_read(dc
->hlcdc
->regmap
, ATMEL_HLCDC_IMR
, &imr
);
389 regmap_read(dc
->hlcdc
->regmap
, ATMEL_HLCDC_ISR
, &isr
);
394 if (status
& ATMEL_HLCDC_SOF
)
395 atmel_hlcdc_crtc_irq(dc
->crtc
);
397 for (i
= 0; i
< ATMEL_HLCDC_MAX_LAYERS
; i
++) {
398 struct atmel_hlcdc_layer
*layer
= dc
->layers
[i
];
400 if (!(ATMEL_HLCDC_LAYER_STATUS(i
) & status
) || !layer
)
403 atmel_hlcdc_layer_irq(layer
);
409 static struct drm_framebuffer
*atmel_hlcdc_fb_create(struct drm_device
*dev
,
410 struct drm_file
*file_priv
, const struct drm_mode_fb_cmd2
*mode_cmd
)
412 return drm_fb_cma_create(dev
, file_priv
, mode_cmd
);
415 static void atmel_hlcdc_fb_output_poll_changed(struct drm_device
*dev
)
417 struct atmel_hlcdc_dc
*dc
= dev
->dev_private
;
420 drm_fbdev_cma_hotplug_event(dc
->fbdev
);
422 dc
->fbdev
= drm_fbdev_cma_init(dev
, 24,
423 dev
->mode_config
.num_crtc
,
424 dev
->mode_config
.num_connector
);
425 if (IS_ERR(dc
->fbdev
))
430 static const struct drm_mode_config_funcs mode_config_funcs
= {
431 .fb_create
= atmel_hlcdc_fb_create
,
432 .output_poll_changed
= atmel_hlcdc_fb_output_poll_changed
,
433 .atomic_check
= drm_atomic_helper_check
,
434 .atomic_commit
= drm_atomic_helper_commit
,
437 static int atmel_hlcdc_dc_modeset_init(struct drm_device
*dev
)
439 struct atmel_hlcdc_dc
*dc
= dev
->dev_private
;
440 struct atmel_hlcdc_planes
*planes
;
444 drm_mode_config_init(dev
);
446 ret
= atmel_hlcdc_create_outputs(dev
);
448 dev_err(dev
->dev
, "failed to create panel: %d\n", ret
);
452 planes
= atmel_hlcdc_create_planes(dev
);
453 if (IS_ERR(planes
)) {
454 dev_err(dev
->dev
, "failed to create planes\n");
455 return PTR_ERR(planes
);
460 dc
->layers
[planes
->primary
->layer
.desc
->id
] =
461 &planes
->primary
->layer
;
464 dc
->layers
[planes
->cursor
->layer
.desc
->id
] =
465 &planes
->cursor
->layer
;
467 for (i
= 0; i
< planes
->noverlays
; i
++)
468 dc
->layers
[planes
->overlays
[i
]->layer
.desc
->id
] =
469 &planes
->overlays
[i
]->layer
;
471 ret
= atmel_hlcdc_crtc_create(dev
);
473 dev_err(dev
->dev
, "failed to create crtc\n");
477 dev
->mode_config
.min_width
= dc
->desc
->min_width
;
478 dev
->mode_config
.min_height
= dc
->desc
->min_height
;
479 dev
->mode_config
.max_width
= dc
->desc
->max_width
;
480 dev
->mode_config
.max_height
= dc
->desc
->max_height
;
481 dev
->mode_config
.funcs
= &mode_config_funcs
;
486 static int atmel_hlcdc_dc_load(struct drm_device
*dev
)
488 struct platform_device
*pdev
= to_platform_device(dev
->dev
);
489 const struct of_device_id
*match
;
490 struct atmel_hlcdc_dc
*dc
;
493 match
= of_match_node(atmel_hlcdc_of_match
, dev
->dev
->parent
->of_node
);
495 dev_err(&pdev
->dev
, "invalid compatible string\n");
500 dev_err(&pdev
->dev
, "invalid hlcdc description\n");
504 dc
= devm_kzalloc(dev
->dev
, sizeof(*dc
), GFP_KERNEL
);
508 dc
->wq
= alloc_ordered_workqueue("atmel-hlcdc-dc", 0);
512 dc
->desc
= match
->data
;
513 dc
->hlcdc
= dev_get_drvdata(dev
->dev
->parent
);
514 dev
->dev_private
= dc
;
516 ret
= clk_prepare_enable(dc
->hlcdc
->periph_clk
);
518 dev_err(dev
->dev
, "failed to enable periph_clk\n");
522 pm_runtime_enable(dev
->dev
);
524 ret
= drm_vblank_init(dev
, 1);
526 dev_err(dev
->dev
, "failed to initialize vblank\n");
527 goto err_periph_clk_disable
;
530 ret
= atmel_hlcdc_dc_modeset_init(dev
);
532 dev_err(dev
->dev
, "failed to initialize mode setting\n");
533 goto err_periph_clk_disable
;
536 drm_mode_config_reset(dev
);
538 pm_runtime_get_sync(dev
->dev
);
539 ret
= drm_irq_install(dev
, dc
->hlcdc
->irq
);
540 pm_runtime_put_sync(dev
->dev
);
542 dev_err(dev
->dev
, "failed to install IRQ handler\n");
543 goto err_periph_clk_disable
;
546 platform_set_drvdata(pdev
, dev
);
548 drm_kms_helper_poll_init(dev
);
550 /* force connectors detection */
551 drm_helper_hpd_irq_event(dev
);
555 err_periph_clk_disable
:
556 pm_runtime_disable(dev
->dev
);
557 clk_disable_unprepare(dc
->hlcdc
->periph_clk
);
560 destroy_workqueue(dc
->wq
);
565 static void atmel_hlcdc_dc_unload(struct drm_device
*dev
)
567 struct atmel_hlcdc_dc
*dc
= dev
->dev_private
;
570 drm_fbdev_cma_fini(dc
->fbdev
);
571 flush_workqueue(dc
->wq
);
572 drm_kms_helper_poll_fini(dev
);
573 drm_mode_config_cleanup(dev
);
574 drm_vblank_cleanup(dev
);
576 pm_runtime_get_sync(dev
->dev
);
577 drm_irq_uninstall(dev
);
578 pm_runtime_put_sync(dev
->dev
);
580 dev
->dev_private
= NULL
;
582 pm_runtime_disable(dev
->dev
);
583 clk_disable_unprepare(dc
->hlcdc
->periph_clk
);
584 destroy_workqueue(dc
->wq
);
587 static int atmel_hlcdc_dc_connector_plug_all(struct drm_device
*dev
)
589 struct drm_connector
*connector
, *failed
;
592 mutex_lock(&dev
->mode_config
.mutex
);
593 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
594 ret
= drm_connector_register(connector
);
600 mutex_unlock(&dev
->mode_config
.mutex
);
604 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
605 if (failed
== connector
)
608 drm_connector_unregister(connector
);
610 mutex_unlock(&dev
->mode_config
.mutex
);
615 static void atmel_hlcdc_dc_connector_unplug_all(struct drm_device
*dev
)
617 mutex_lock(&dev
->mode_config
.mutex
);
618 drm_connector_unplug_all(dev
);
619 mutex_unlock(&dev
->mode_config
.mutex
);
622 static void atmel_hlcdc_dc_lastclose(struct drm_device
*dev
)
624 struct atmel_hlcdc_dc
*dc
= dev
->dev_private
;
626 drm_fbdev_cma_restore_mode(dc
->fbdev
);
629 static int atmel_hlcdc_dc_irq_postinstall(struct drm_device
*dev
)
631 struct atmel_hlcdc_dc
*dc
= dev
->dev_private
;
632 unsigned int cfg
= 0;
635 /* Enable interrupts on activated layers */
636 for (i
= 0; i
< ATMEL_HLCDC_MAX_LAYERS
; i
++) {
638 cfg
|= ATMEL_HLCDC_LAYER_STATUS(i
);
641 regmap_write(dc
->hlcdc
->regmap
, ATMEL_HLCDC_IER
, cfg
);
646 static void atmel_hlcdc_dc_irq_uninstall(struct drm_device
*dev
)
648 struct atmel_hlcdc_dc
*dc
= dev
->dev_private
;
651 regmap_write(dc
->hlcdc
->regmap
, ATMEL_HLCDC_IDR
, 0xffffffff);
652 regmap_read(dc
->hlcdc
->regmap
, ATMEL_HLCDC_ISR
, &isr
);
655 static int atmel_hlcdc_dc_enable_vblank(struct drm_device
*dev
,
658 struct atmel_hlcdc_dc
*dc
= dev
->dev_private
;
660 /* Enable SOF (Start Of Frame) interrupt for vblank counting */
661 regmap_write(dc
->hlcdc
->regmap
, ATMEL_HLCDC_IER
, ATMEL_HLCDC_SOF
);
666 static void atmel_hlcdc_dc_disable_vblank(struct drm_device
*dev
,
669 struct atmel_hlcdc_dc
*dc
= dev
->dev_private
;
671 regmap_write(dc
->hlcdc
->regmap
, ATMEL_HLCDC_IDR
, ATMEL_HLCDC_SOF
);
674 static const struct file_operations fops
= {
675 .owner
= THIS_MODULE
,
677 .release
= drm_release
,
678 .unlocked_ioctl
= drm_ioctl
,
680 .compat_ioctl
= drm_compat_ioctl
,
685 .mmap
= drm_gem_cma_mmap
,
688 static struct drm_driver atmel_hlcdc_dc_driver
= {
689 .driver_features
= DRIVER_HAVE_IRQ
| DRIVER_GEM
|
690 DRIVER_MODESET
| DRIVER_PRIME
|
692 .lastclose
= atmel_hlcdc_dc_lastclose
,
693 .irq_handler
= atmel_hlcdc_dc_irq_handler
,
694 .irq_preinstall
= atmel_hlcdc_dc_irq_uninstall
,
695 .irq_postinstall
= atmel_hlcdc_dc_irq_postinstall
,
696 .irq_uninstall
= atmel_hlcdc_dc_irq_uninstall
,
697 .get_vblank_counter
= drm_vblank_no_hw_counter
,
698 .enable_vblank
= atmel_hlcdc_dc_enable_vblank
,
699 .disable_vblank
= atmel_hlcdc_dc_disable_vblank
,
700 .gem_free_object
= drm_gem_cma_free_object
,
701 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
702 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
703 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
704 .gem_prime_import
= drm_gem_prime_import
,
705 .gem_prime_export
= drm_gem_prime_export
,
706 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
707 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
708 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
709 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
710 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
711 .dumb_create
= drm_gem_cma_dumb_create
,
712 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
713 .dumb_destroy
= drm_gem_dumb_destroy
,
715 .name
= "atmel-hlcdc",
716 .desc
= "Atmel HLCD Controller DRM",
722 static int atmel_hlcdc_dc_drm_probe(struct platform_device
*pdev
)
724 struct drm_device
*ddev
;
727 ddev
= drm_dev_alloc(&atmel_hlcdc_dc_driver
, &pdev
->dev
);
731 ret
= atmel_hlcdc_dc_load(ddev
);
735 ret
= drm_dev_register(ddev
, 0);
739 ret
= atmel_hlcdc_dc_connector_plug_all(ddev
);
746 drm_dev_unregister(ddev
);
749 atmel_hlcdc_dc_unload(ddev
);
757 static int atmel_hlcdc_dc_drm_remove(struct platform_device
*pdev
)
759 struct drm_device
*ddev
= platform_get_drvdata(pdev
);
761 atmel_hlcdc_dc_connector_unplug_all(ddev
);
762 drm_dev_unregister(ddev
);
763 atmel_hlcdc_dc_unload(ddev
);
769 #ifdef CONFIG_PM_SLEEP
770 static int atmel_hlcdc_dc_drm_suspend(struct device
*dev
)
772 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
773 struct drm_crtc
*crtc
;
775 if (pm_runtime_suspended(dev
))
778 drm_modeset_lock_all(drm_dev
);
779 list_for_each_entry(crtc
, &drm_dev
->mode_config
.crtc_list
, head
)
780 atmel_hlcdc_crtc_suspend(crtc
);
781 drm_modeset_unlock_all(drm_dev
);
785 static int atmel_hlcdc_dc_drm_resume(struct device
*dev
)
787 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
788 struct drm_crtc
*crtc
;
790 if (pm_runtime_suspended(dev
))
793 drm_modeset_lock_all(drm_dev
);
794 list_for_each_entry(crtc
, &drm_dev
->mode_config
.crtc_list
, head
)
795 atmel_hlcdc_crtc_resume(crtc
);
796 drm_modeset_unlock_all(drm_dev
);
801 static SIMPLE_DEV_PM_OPS(atmel_hlcdc_dc_drm_pm_ops
,
802 atmel_hlcdc_dc_drm_suspend
, atmel_hlcdc_dc_drm_resume
);
804 static const struct of_device_id atmel_hlcdc_dc_of_match
[] = {
805 { .compatible
= "atmel,hlcdc-display-controller" },
809 static struct platform_driver atmel_hlcdc_dc_platform_driver
= {
810 .probe
= atmel_hlcdc_dc_drm_probe
,
811 .remove
= atmel_hlcdc_dc_drm_remove
,
813 .name
= "atmel-hlcdc-display-controller",
814 .pm
= &atmel_hlcdc_dc_drm_pm_ops
,
815 .of_match_table
= atmel_hlcdc_dc_of_match
,
818 module_platform_driver(atmel_hlcdc_dc_platform_driver
);
820 MODULE_AUTHOR("Jean-Jacques Hiblot <jjhiblot@traphandler.com>");
821 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
822 MODULE_DESCRIPTION("Atmel HLCDC Display Controller DRM Driver");
823 MODULE_LICENSE("GPL");
824 MODULE_ALIAS("platform:atmel-hlcdc-dc");