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 of_device_id atmel_hlcdc_of_match[] = {
246 .compatible = "atmel,at91sam9n12-hlcdc",
247 .data = &atmel_hlcdc_dc_at91sam9n12,
250 .compatible = "atmel,at91sam9x5-hlcdc",
251 .data = &atmel_hlcdc_dc_at91sam9x5,
254 .compatible = "atmel,sama5d3-hlcdc",
255 .data = &atmel_hlcdc_dc_sama5d3,
260 int atmel_hlcdc_dc_mode_valid(struct atmel_hlcdc_dc *dc,
261 struct drm_display_mode *mode)
263 int vfront_porch = mode->vsync_start - mode->vdisplay;
264 int vback_porch = mode->vtotal - mode->vsync_end;
265 int vsync_len = mode->vsync_end - mode->vsync_start;
266 int hfront_porch = mode->hsync_start - mode->hdisplay;
267 int hback_porch = mode->htotal - mode->hsync_end;
268 int hsync_len = mode->hsync_end - mode->hsync_start;
270 if (hsync_len > 0x40 || hsync_len < 1)
273 if (vsync_len > 0x40 || vsync_len < 1)
276 if (hfront_porch > 0x200 || hfront_porch < 1 ||
277 hback_porch > 0x200 || hback_porch < 1 ||
279 return MODE_H_ILLEGAL;
281 if (vfront_porch > 0x40 || vfront_porch < 1 ||
282 vback_porch > 0x40 || vback_porch < 0 ||
284 return MODE_V_ILLEGAL;
289 static irqreturn_t atmel_hlcdc_dc_irq_handler(int irq, void *data)
291 struct drm_device *dev = data;
292 struct atmel_hlcdc_dc *dc = dev->dev_private;
293 unsigned long status;
294 unsigned int imr, isr;
297 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_IMR, &imr);
298 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
303 if (status & ATMEL_HLCDC_SOF)
304 atmel_hlcdc_crtc_irq(dc->crtc);
306 for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
307 struct atmel_hlcdc_layer *layer = dc->layers[i];
309 if (!(ATMEL_HLCDC_LAYER_STATUS(i) & status) || !layer)
312 atmel_hlcdc_layer_irq(layer);
318 static struct drm_framebuffer *atmel_hlcdc_fb_create(struct drm_device *dev,
319 struct drm_file *file_priv, struct drm_mode_fb_cmd2 *mode_cmd)
321 return drm_fb_cma_create(dev, file_priv, mode_cmd);
324 static void atmel_hlcdc_fb_output_poll_changed(struct drm_device *dev)
326 struct atmel_hlcdc_dc *dc = dev->dev_private;
329 drm_fbdev_cma_hotplug_event(dc->fbdev);
331 dc->fbdev = drm_fbdev_cma_init(dev, 24,
332 dev->mode_config.num_crtc,
333 dev->mode_config.num_connector);
334 if (IS_ERR(dc->fbdev))
339 static const struct drm_mode_config_funcs mode_config_funcs = {
340 .fb_create = atmel_hlcdc_fb_create,
341 .output_poll_changed = atmel_hlcdc_fb_output_poll_changed,
342 .atomic_check = drm_atomic_helper_check,
343 .atomic_commit = drm_atomic_helper_commit,
346 static int atmel_hlcdc_dc_modeset_init(struct drm_device *dev)
348 struct atmel_hlcdc_dc *dc = dev->dev_private;
349 struct atmel_hlcdc_planes *planes;
353 drm_mode_config_init(dev);
355 ret = atmel_hlcdc_create_outputs(dev);
357 dev_err(dev->dev, "failed to create panel: %d\n", ret);
361 planes = atmel_hlcdc_create_planes(dev);
362 if (IS_ERR(planes)) {
363 dev_err(dev->dev, "failed to create planes\n");
364 return PTR_ERR(planes);
369 dc->layers[planes->primary->layer.desc->id] =
370 &planes->primary->layer;
373 dc->layers[planes->cursor->layer.desc->id] =
374 &planes->cursor->layer;
376 for (i = 0; i < planes->noverlays; i++)
377 dc->layers[planes->overlays[i]->layer.desc->id] =
378 &planes->overlays[i]->layer;
380 ret = atmel_hlcdc_crtc_create(dev);
382 dev_err(dev->dev, "failed to create crtc\n");
386 dev->mode_config.min_width = dc->desc->min_width;
387 dev->mode_config.min_height = dc->desc->min_height;
388 dev->mode_config.max_width = dc->desc->max_width;
389 dev->mode_config.max_height = dc->desc->max_height;
390 dev->mode_config.funcs = &mode_config_funcs;
395 static int atmel_hlcdc_dc_load(struct drm_device *dev)
397 struct platform_device *pdev = to_platform_device(dev->dev);
398 const struct of_device_id *match;
399 struct atmel_hlcdc_dc *dc;
402 match = of_match_node(atmel_hlcdc_of_match, dev->dev->parent->of_node);
404 dev_err(&pdev->dev, "invalid compatible string\n");
409 dev_err(&pdev->dev, "invalid hlcdc description\n");
413 dc = devm_kzalloc(dev->dev, sizeof(*dc), GFP_KERNEL);
417 dc->wq = alloc_ordered_workqueue("atmel-hlcdc-dc", 0);
421 dc->desc = match->data;
422 dc->hlcdc = dev_get_drvdata(dev->dev->parent);
423 dev->dev_private = dc;
425 ret = clk_prepare_enable(dc->hlcdc->periph_clk);
427 dev_err(dev->dev, "failed to enable periph_clk\n");
431 pm_runtime_enable(dev->dev);
433 ret = drm_vblank_init(dev, 1);
435 dev_err(dev->dev, "failed to initialize vblank\n");
436 goto err_periph_clk_disable;
439 ret = atmel_hlcdc_dc_modeset_init(dev);
441 dev_err(dev->dev, "failed to initialize mode setting\n");
442 goto err_periph_clk_disable;
445 drm_mode_config_reset(dev);
447 pm_runtime_get_sync(dev->dev);
448 ret = drm_irq_install(dev, dc->hlcdc->irq);
449 pm_runtime_put_sync(dev->dev);
451 dev_err(dev->dev, "failed to install IRQ handler\n");
452 goto err_periph_clk_disable;
455 platform_set_drvdata(pdev, dev);
457 drm_kms_helper_poll_init(dev);
459 /* force connectors detection */
460 drm_helper_hpd_irq_event(dev);
464 err_periph_clk_disable:
465 pm_runtime_disable(dev->dev);
466 clk_disable_unprepare(dc->hlcdc->periph_clk);
469 destroy_workqueue(dc->wq);
474 static void atmel_hlcdc_dc_unload(struct drm_device *dev)
476 struct atmel_hlcdc_dc *dc = dev->dev_private;
479 drm_fbdev_cma_fini(dc->fbdev);
480 flush_workqueue(dc->wq);
481 drm_kms_helper_poll_fini(dev);
482 drm_mode_config_cleanup(dev);
483 drm_vblank_cleanup(dev);
485 pm_runtime_get_sync(dev->dev);
486 drm_irq_uninstall(dev);
487 pm_runtime_put_sync(dev->dev);
489 dev->dev_private = NULL;
491 pm_runtime_disable(dev->dev);
492 clk_disable_unprepare(dc->hlcdc->periph_clk);
493 destroy_workqueue(dc->wq);
496 static int atmel_hlcdc_dc_connector_plug_all(struct drm_device *dev)
498 struct drm_connector *connector, *failed;
501 mutex_lock(&dev->mode_config.mutex);
502 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
503 ret = drm_connector_register(connector);
509 mutex_unlock(&dev->mode_config.mutex);
513 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
514 if (failed == connector)
517 drm_connector_unregister(connector);
519 mutex_unlock(&dev->mode_config.mutex);
524 static void atmel_hlcdc_dc_connector_unplug_all(struct drm_device *dev)
526 mutex_lock(&dev->mode_config.mutex);
527 drm_connector_unplug_all(dev);
528 mutex_unlock(&dev->mode_config.mutex);
531 static void atmel_hlcdc_dc_preclose(struct drm_device *dev,
532 struct drm_file *file)
534 struct drm_crtc *crtc;
536 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
537 atmel_hlcdc_crtc_cancel_page_flip(crtc, file);
540 static void atmel_hlcdc_dc_lastclose(struct drm_device *dev)
542 struct atmel_hlcdc_dc *dc = dev->dev_private;
544 drm_fbdev_cma_restore_mode(dc->fbdev);
547 static int atmel_hlcdc_dc_irq_postinstall(struct drm_device *dev)
549 struct atmel_hlcdc_dc *dc = dev->dev_private;
550 unsigned int cfg = 0;
553 /* Enable interrupts on activated layers */
554 for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
556 cfg |= ATMEL_HLCDC_LAYER_STATUS(i);
559 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, cfg);
564 static void atmel_hlcdc_dc_irq_uninstall(struct drm_device *dev)
566 struct atmel_hlcdc_dc *dc = dev->dev_private;
569 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, 0xffffffff);
570 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
573 static int atmel_hlcdc_dc_enable_vblank(struct drm_device *dev, int crtc)
575 struct atmel_hlcdc_dc *dc = dev->dev_private;
577 /* Enable SOF (Start Of Frame) interrupt for vblank counting */
578 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, ATMEL_HLCDC_SOF);
583 static void atmel_hlcdc_dc_disable_vblank(struct drm_device *dev, int crtc)
585 struct atmel_hlcdc_dc *dc = dev->dev_private;
587 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, ATMEL_HLCDC_SOF);
590 static const struct file_operations fops = {
591 .owner = THIS_MODULE,
593 .release = drm_release,
594 .unlocked_ioctl = drm_ioctl,
596 .compat_ioctl = drm_compat_ioctl,
601 .mmap = drm_gem_cma_mmap,
604 static struct drm_driver atmel_hlcdc_dc_driver = {
605 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
606 DRIVER_MODESET | DRIVER_PRIME |
608 .preclose = atmel_hlcdc_dc_preclose,
609 .lastclose = atmel_hlcdc_dc_lastclose,
610 .irq_handler = atmel_hlcdc_dc_irq_handler,
611 .irq_preinstall = atmel_hlcdc_dc_irq_uninstall,
612 .irq_postinstall = atmel_hlcdc_dc_irq_postinstall,
613 .irq_uninstall = atmel_hlcdc_dc_irq_uninstall,
614 .get_vblank_counter = drm_vblank_count,
615 .enable_vblank = atmel_hlcdc_dc_enable_vblank,
616 .disable_vblank = atmel_hlcdc_dc_disable_vblank,
617 .gem_free_object = drm_gem_cma_free_object,
618 .gem_vm_ops = &drm_gem_cma_vm_ops,
619 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
620 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
621 .gem_prime_import = drm_gem_prime_import,
622 .gem_prime_export = drm_gem_prime_export,
623 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
624 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
625 .gem_prime_vmap = drm_gem_cma_prime_vmap,
626 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
627 .gem_prime_mmap = drm_gem_cma_prime_mmap,
628 .dumb_create = drm_gem_cma_dumb_create,
629 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
630 .dumb_destroy = drm_gem_dumb_destroy,
632 .name = "atmel-hlcdc",
633 .desc = "Atmel HLCD Controller DRM",
639 static int atmel_hlcdc_dc_drm_probe(struct platform_device *pdev)
641 struct drm_device *ddev;
644 ddev = drm_dev_alloc(&atmel_hlcdc_dc_driver, &pdev->dev);
648 ret = drm_dev_set_unique(ddev, dev_name(ddev->dev));
652 ret = atmel_hlcdc_dc_load(ddev);
656 ret = drm_dev_register(ddev, 0);
660 ret = atmel_hlcdc_dc_connector_plug_all(ddev);
667 drm_dev_unregister(ddev);
670 atmel_hlcdc_dc_unload(ddev);
678 static int atmel_hlcdc_dc_drm_remove(struct platform_device *pdev)
680 struct drm_device *ddev = platform_get_drvdata(pdev);
682 atmel_hlcdc_dc_connector_unplug_all(ddev);
683 drm_dev_unregister(ddev);
684 atmel_hlcdc_dc_unload(ddev);
691 static int atmel_hlcdc_dc_drm_suspend(struct device *dev)
693 struct drm_device *drm_dev = dev_get_drvdata(dev);
694 struct drm_crtc *crtc;
696 if (pm_runtime_suspended(dev))
699 drm_modeset_lock_all(drm_dev);
700 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head)
701 atmel_hlcdc_crtc_suspend(crtc);
702 drm_modeset_unlock_all(drm_dev);
706 static int atmel_hlcdc_dc_drm_resume(struct device *dev)
708 struct drm_device *drm_dev = dev_get_drvdata(dev);
709 struct drm_crtc *crtc;
711 if (pm_runtime_suspended(dev))
714 drm_modeset_lock_all(drm_dev);
715 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head)
716 atmel_hlcdc_crtc_resume(crtc);
717 drm_modeset_unlock_all(drm_dev);
722 static SIMPLE_DEV_PM_OPS(atmel_hlcdc_dc_drm_pm_ops,
723 atmel_hlcdc_dc_drm_suspend, atmel_hlcdc_dc_drm_resume);
725 static const struct of_device_id atmel_hlcdc_dc_of_match[] = {
726 { .compatible = "atmel,hlcdc-display-controller" },
730 static struct platform_driver atmel_hlcdc_dc_platform_driver = {
731 .probe = atmel_hlcdc_dc_drm_probe,
732 .remove = atmel_hlcdc_dc_drm_remove,
734 .name = "atmel-hlcdc-display-controller",
735 .pm = &atmel_hlcdc_dc_drm_pm_ops,
736 .of_match_table = atmel_hlcdc_dc_of_match,
739 module_platform_driver(atmel_hlcdc_dc_platform_driver);
741 MODULE_AUTHOR("Jean-Jacques Hiblot <jjhiblot@traphandler.com>");
742 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
743 MODULE_DESCRIPTION("Atmel HLCDC Display Controller DRM Driver");
744 MODULE_LICENSE("GPL");
745 MODULE_ALIAS("platform:atmel-hlcdc-dc");