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_sama5d3_layers[] = {
35 .formats = &atmel_hlcdc_plane_rgb_formats,
38 .type = ATMEL_HLCDC_BASE_LAYER,
50 .formats = &atmel_hlcdc_plane_rgb_formats,
53 .type = ATMEL_HLCDC_OVERLAY_LAYER,
68 .formats = &atmel_hlcdc_plane_rgb_formats,
71 .type = ATMEL_HLCDC_OVERLAY_LAYER,
85 .name = "high-end-overlay",
86 .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
89 .type = ATMEL_HLCDC_OVERLAY_LAYER,
99 .chroma_key_mask = 11,
100 .general_config = 12,
106 .formats = &atmel_hlcdc_plane_rgb_formats,
107 .regs_offset = 0x440,
109 .type = ATMEL_HLCDC_CURSOR_LAYER,
120 .chroma_key_mask = 8,
126 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d3 = {
131 .nlayers = ARRAY_SIZE(atmel_hlcdc_sama5d3_layers),
132 .layers = atmel_hlcdc_sama5d3_layers,
133 .max_sync_width = 64,
140 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d4 = {
145 .nlayers = ARRAY_SIZE(atmel_hlcdc_sama5d3_layers),
146 .layers = atmel_hlcdc_sama5d3_layers,
147 .max_sync_width = 256,
154 static const struct of_device_id atmel_hlcdc_of_match[] = {
156 .compatible = "atmel,sama5d3-hlcdc",
157 .data = &atmel_hlcdc_dc_sama5d3,
160 .compatible = "atmel,sama5d4-hlcdc",
161 .data = &atmel_hlcdc_dc_sama5d4,
166 int atmel_hlcdc_dc_mode_valid(struct atmel_hlcdc_dc *dc,
167 struct drm_display_mode *mode)
169 int vfront_porch = mode->vsync_start - mode->vdisplay;
170 int vback_porch = mode->vtotal - mode->vsync_end;
171 int vsync_len = mode->vsync_end - mode->vsync_start;
172 int hfront_porch = mode->hsync_start - mode->hdisplay;
173 int hback_porch = mode->htotal - mode->hsync_end;
174 int hsync_len = mode->hsync_end - mode->hsync_start;
176 if (hsync_len > dc->desc->max_sync_width || hsync_len < 1)
179 if (vsync_len > dc->desc->max_sync_width || vsync_len < 1)
182 if (hfront_porch > dc->desc->max_hfp || hfront_porch < 1 ||
183 hback_porch > dc->desc->max_hbp || hback_porch < 1 ||
185 return MODE_H_ILLEGAL;
187 if (vfront_porch > dc->desc->max_vfp || vfront_porch < 1 ||
188 vback_porch > dc->desc->max_vbp || vback_porch < 0 ||
190 return MODE_V_ILLEGAL;
195 static irqreturn_t atmel_hlcdc_dc_irq_handler(int irq, void *data)
197 struct drm_device *dev = data;
198 struct atmel_hlcdc_dc *dc = dev->dev_private;
199 unsigned long status;
200 unsigned int imr, isr;
203 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_IMR, &imr);
204 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
209 if (status & ATMEL_HLCDC_SOF)
210 atmel_hlcdc_crtc_irq(dc->crtc);
212 for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
213 struct atmel_hlcdc_layer *layer = dc->layers[i];
215 if (!(ATMEL_HLCDC_LAYER_STATUS(i) & status) || !layer)
218 atmel_hlcdc_layer_irq(layer);
224 static struct drm_framebuffer *atmel_hlcdc_fb_create(struct drm_device *dev,
225 struct drm_file *file_priv, struct drm_mode_fb_cmd2 *mode_cmd)
227 return drm_fb_cma_create(dev, file_priv, mode_cmd);
230 static void atmel_hlcdc_fb_output_poll_changed(struct drm_device *dev)
232 struct atmel_hlcdc_dc *dc = dev->dev_private;
235 drm_fbdev_cma_hotplug_event(dc->fbdev);
237 dc->fbdev = drm_fbdev_cma_init(dev, 24,
238 dev->mode_config.num_crtc,
239 dev->mode_config.num_connector);
240 if (IS_ERR(dc->fbdev))
245 static const struct drm_mode_config_funcs mode_config_funcs = {
246 .fb_create = atmel_hlcdc_fb_create,
247 .output_poll_changed = atmel_hlcdc_fb_output_poll_changed,
248 .atomic_check = drm_atomic_helper_check,
249 .atomic_commit = drm_atomic_helper_commit,
252 static int atmel_hlcdc_dc_modeset_init(struct drm_device *dev)
254 struct atmel_hlcdc_dc *dc = dev->dev_private;
255 struct atmel_hlcdc_planes *planes;
259 drm_mode_config_init(dev);
261 ret = atmel_hlcdc_create_outputs(dev);
263 dev_err(dev->dev, "failed to create panel: %d\n", ret);
267 planes = atmel_hlcdc_create_planes(dev);
268 if (IS_ERR(planes)) {
269 dev_err(dev->dev, "failed to create planes\n");
270 return PTR_ERR(planes);
275 dc->layers[planes->primary->layer.desc->id] =
276 &planes->primary->layer;
279 dc->layers[planes->cursor->layer.desc->id] =
280 &planes->cursor->layer;
282 for (i = 0; i < planes->noverlays; i++)
283 dc->layers[planes->overlays[i]->layer.desc->id] =
284 &planes->overlays[i]->layer;
286 ret = atmel_hlcdc_crtc_create(dev);
288 dev_err(dev->dev, "failed to create crtc\n");
292 dev->mode_config.min_width = dc->desc->min_width;
293 dev->mode_config.min_height = dc->desc->min_height;
294 dev->mode_config.max_width = dc->desc->max_width;
295 dev->mode_config.max_height = dc->desc->max_height;
296 dev->mode_config.funcs = &mode_config_funcs;
301 static int atmel_hlcdc_dc_load(struct drm_device *dev)
303 struct platform_device *pdev = to_platform_device(dev->dev);
304 const struct of_device_id *match;
305 struct atmel_hlcdc_dc *dc;
308 match = of_match_node(atmel_hlcdc_of_match, dev->dev->parent->of_node);
310 dev_err(&pdev->dev, "invalid compatible string\n");
315 dev_err(&pdev->dev, "invalid hlcdc description\n");
319 dc = devm_kzalloc(dev->dev, sizeof(*dc), GFP_KERNEL);
323 dc->wq = alloc_ordered_workqueue("atmel-hlcdc-dc", 0);
327 dc->desc = match->data;
328 dc->hlcdc = dev_get_drvdata(dev->dev->parent);
329 dev->dev_private = dc;
331 ret = clk_prepare_enable(dc->hlcdc->periph_clk);
333 dev_err(dev->dev, "failed to enable periph_clk\n");
337 pm_runtime_enable(dev->dev);
339 ret = atmel_hlcdc_dc_modeset_init(dev);
341 dev_err(dev->dev, "failed to initialize mode setting\n");
342 goto err_periph_clk_disable;
345 drm_mode_config_reset(dev);
347 ret = drm_vblank_init(dev, 1);
349 dev_err(dev->dev, "failed to initialize vblank\n");
350 goto err_periph_clk_disable;
353 pm_runtime_get_sync(dev->dev);
354 ret = drm_irq_install(dev, dc->hlcdc->irq);
355 pm_runtime_put_sync(dev->dev);
357 dev_err(dev->dev, "failed to install IRQ handler\n");
358 goto err_periph_clk_disable;
361 platform_set_drvdata(pdev, dev);
363 drm_kms_helper_poll_init(dev);
365 /* force connectors detection */
366 drm_helper_hpd_irq_event(dev);
370 err_periph_clk_disable:
371 pm_runtime_disable(dev->dev);
372 clk_disable_unprepare(dc->hlcdc->periph_clk);
375 destroy_workqueue(dc->wq);
380 static void atmel_hlcdc_dc_unload(struct drm_device *dev)
382 struct atmel_hlcdc_dc *dc = dev->dev_private;
385 drm_fbdev_cma_fini(dc->fbdev);
386 flush_workqueue(dc->wq);
387 drm_kms_helper_poll_fini(dev);
388 drm_mode_config_cleanup(dev);
389 drm_vblank_cleanup(dev);
391 pm_runtime_get_sync(dev->dev);
392 drm_irq_uninstall(dev);
393 pm_runtime_put_sync(dev->dev);
395 dev->dev_private = NULL;
397 pm_runtime_disable(dev->dev);
398 clk_disable_unprepare(dc->hlcdc->periph_clk);
399 destroy_workqueue(dc->wq);
402 static int atmel_hlcdc_dc_connector_plug_all(struct drm_device *dev)
404 struct drm_connector *connector, *failed;
407 mutex_lock(&dev->mode_config.mutex);
408 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
409 ret = drm_connector_register(connector);
415 mutex_unlock(&dev->mode_config.mutex);
419 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
420 if (failed == connector)
423 drm_connector_unregister(connector);
425 mutex_unlock(&dev->mode_config.mutex);
430 static void atmel_hlcdc_dc_connector_unplug_all(struct drm_device *dev)
432 mutex_lock(&dev->mode_config.mutex);
433 drm_connector_unplug_all(dev);
434 mutex_unlock(&dev->mode_config.mutex);
437 static void atmel_hlcdc_dc_preclose(struct drm_device *dev,
438 struct drm_file *file)
440 struct drm_crtc *crtc;
442 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
443 atmel_hlcdc_crtc_cancel_page_flip(crtc, file);
446 static void atmel_hlcdc_dc_lastclose(struct drm_device *dev)
448 struct atmel_hlcdc_dc *dc = dev->dev_private;
450 drm_fbdev_cma_restore_mode(dc->fbdev);
453 static int atmel_hlcdc_dc_irq_postinstall(struct drm_device *dev)
455 struct atmel_hlcdc_dc *dc = dev->dev_private;
456 unsigned int cfg = 0;
459 /* Enable interrupts on activated layers */
460 for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
462 cfg |= ATMEL_HLCDC_LAYER_STATUS(i);
465 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, cfg);
470 static void atmel_hlcdc_dc_irq_uninstall(struct drm_device *dev)
472 struct atmel_hlcdc_dc *dc = dev->dev_private;
475 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, 0xffffffff);
476 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
479 static int atmel_hlcdc_dc_enable_vblank(struct drm_device *dev, int crtc)
481 struct atmel_hlcdc_dc *dc = dev->dev_private;
483 /* Enable SOF (Start Of Frame) interrupt for vblank counting */
484 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, ATMEL_HLCDC_SOF);
489 static void atmel_hlcdc_dc_disable_vblank(struct drm_device *dev, int crtc)
491 struct atmel_hlcdc_dc *dc = dev->dev_private;
493 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, ATMEL_HLCDC_SOF);
496 static const struct file_operations fops = {
497 .owner = THIS_MODULE,
499 .release = drm_release,
500 .unlocked_ioctl = drm_ioctl,
502 .compat_ioctl = drm_compat_ioctl,
507 .mmap = drm_gem_cma_mmap,
510 static struct drm_driver atmel_hlcdc_dc_driver = {
511 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET,
512 .preclose = atmel_hlcdc_dc_preclose,
513 .lastclose = atmel_hlcdc_dc_lastclose,
514 .irq_handler = atmel_hlcdc_dc_irq_handler,
515 .irq_preinstall = atmel_hlcdc_dc_irq_uninstall,
516 .irq_postinstall = atmel_hlcdc_dc_irq_postinstall,
517 .irq_uninstall = atmel_hlcdc_dc_irq_uninstall,
518 .get_vblank_counter = drm_vblank_count,
519 .enable_vblank = atmel_hlcdc_dc_enable_vblank,
520 .disable_vblank = atmel_hlcdc_dc_disable_vblank,
521 .gem_free_object = drm_gem_cma_free_object,
522 .gem_vm_ops = &drm_gem_cma_vm_ops,
523 .dumb_create = drm_gem_cma_dumb_create,
524 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
525 .dumb_destroy = drm_gem_dumb_destroy,
527 .name = "atmel-hlcdc",
528 .desc = "Atmel HLCD Controller DRM",
534 static int atmel_hlcdc_dc_drm_probe(struct platform_device *pdev)
536 struct drm_device *ddev;
539 ddev = drm_dev_alloc(&atmel_hlcdc_dc_driver, &pdev->dev);
543 ret = drm_dev_set_unique(ddev, dev_name(ddev->dev));
547 ret = atmel_hlcdc_dc_load(ddev);
551 ret = drm_dev_register(ddev, 0);
555 ret = atmel_hlcdc_dc_connector_plug_all(ddev);
562 drm_dev_unregister(ddev);
565 atmel_hlcdc_dc_unload(ddev);
573 static int atmel_hlcdc_dc_drm_remove(struct platform_device *pdev)
575 struct drm_device *ddev = platform_get_drvdata(pdev);
577 atmel_hlcdc_dc_connector_unplug_all(ddev);
578 drm_dev_unregister(ddev);
579 atmel_hlcdc_dc_unload(ddev);
586 static int atmel_hlcdc_dc_drm_suspend(struct device *dev)
588 struct drm_device *drm_dev = dev_get_drvdata(dev);
589 struct drm_crtc *crtc;
591 if (pm_runtime_suspended(dev))
594 drm_modeset_lock_all(drm_dev);
595 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head)
596 atmel_hlcdc_crtc_suspend(crtc);
597 drm_modeset_unlock_all(drm_dev);
601 static int atmel_hlcdc_dc_drm_resume(struct device *dev)
603 struct drm_device *drm_dev = dev_get_drvdata(dev);
604 struct drm_crtc *crtc;
606 if (pm_runtime_suspended(dev))
609 drm_modeset_lock_all(drm_dev);
610 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head)
611 atmel_hlcdc_crtc_resume(crtc);
612 drm_modeset_unlock_all(drm_dev);
617 static SIMPLE_DEV_PM_OPS(atmel_hlcdc_dc_drm_pm_ops,
618 atmel_hlcdc_dc_drm_suspend, atmel_hlcdc_dc_drm_resume);
620 static const struct of_device_id atmel_hlcdc_dc_of_match[] = {
621 { .compatible = "atmel,hlcdc-display-controller" },
625 static struct platform_driver atmel_hlcdc_dc_platform_driver = {
626 .probe = atmel_hlcdc_dc_drm_probe,
627 .remove = atmel_hlcdc_dc_drm_remove,
629 .name = "atmel-hlcdc-display-controller",
630 .pm = &atmel_hlcdc_dc_drm_pm_ops,
631 .of_match_table = atmel_hlcdc_dc_of_match,
634 module_platform_driver(atmel_hlcdc_dc_platform_driver);
636 MODULE_AUTHOR("Jean-Jacques Hiblot <jjhiblot@traphandler.com>");
637 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
638 MODULE_DESCRIPTION("Atmel HLCDC Display Controller DRM Driver");
639 MODULE_LICENSE("GPL");
640 MODULE_ALIAS("platform:atmel-hlcdc-dc");