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,
135 static const struct of_device_id atmel_hlcdc_of_match[] = {
137 .compatible = "atmel,sama5d3-hlcdc",
138 .data = &atmel_hlcdc_dc_sama5d3,
143 int atmel_hlcdc_dc_mode_valid(struct atmel_hlcdc_dc *dc,
144 struct drm_display_mode *mode)
146 int vfront_porch = mode->vsync_start - mode->vdisplay;
147 int vback_porch = mode->vtotal - mode->vsync_end;
148 int vsync_len = mode->vsync_end - mode->vsync_start;
149 int hfront_porch = mode->hsync_start - mode->hdisplay;
150 int hback_porch = mode->htotal - mode->hsync_end;
151 int hsync_len = mode->hsync_end - mode->hsync_start;
153 if (hsync_len > 0x40 || hsync_len < 1)
156 if (vsync_len > 0x40 || vsync_len < 1)
159 if (hfront_porch > 0x200 || hfront_porch < 1 ||
160 hback_porch > 0x200 || hback_porch < 1 ||
162 return MODE_H_ILLEGAL;
164 if (vfront_porch > 0x40 || vfront_porch < 1 ||
165 vback_porch > 0x40 || vback_porch < 0 ||
167 return MODE_V_ILLEGAL;
172 static irqreturn_t atmel_hlcdc_dc_irq_handler(int irq, void *data)
174 struct drm_device *dev = data;
175 struct atmel_hlcdc_dc *dc = dev->dev_private;
176 unsigned long status;
177 unsigned int imr, isr;
180 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_IMR, &imr);
181 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
186 if (status & ATMEL_HLCDC_SOF)
187 atmel_hlcdc_crtc_irq(dc->crtc);
189 for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
190 struct atmel_hlcdc_layer *layer = dc->layers[i];
192 if (!(ATMEL_HLCDC_LAYER_STATUS(i) & status) || !layer)
195 atmel_hlcdc_layer_irq(layer);
201 static struct drm_framebuffer *atmel_hlcdc_fb_create(struct drm_device *dev,
202 struct drm_file *file_priv, struct drm_mode_fb_cmd2 *mode_cmd)
204 return drm_fb_cma_create(dev, file_priv, mode_cmd);
207 static void atmel_hlcdc_fb_output_poll_changed(struct drm_device *dev)
209 struct atmel_hlcdc_dc *dc = dev->dev_private;
212 drm_fbdev_cma_hotplug_event(dc->fbdev);
214 dc->fbdev = drm_fbdev_cma_init(dev, 24,
215 dev->mode_config.num_crtc,
216 dev->mode_config.num_connector);
217 if (IS_ERR(dc->fbdev))
222 static const struct drm_mode_config_funcs mode_config_funcs = {
223 .fb_create = atmel_hlcdc_fb_create,
224 .output_poll_changed = atmel_hlcdc_fb_output_poll_changed,
227 static int atmel_hlcdc_dc_modeset_init(struct drm_device *dev)
229 struct atmel_hlcdc_dc *dc = dev->dev_private;
230 struct atmel_hlcdc_planes *planes;
234 drm_mode_config_init(dev);
236 ret = atmel_hlcdc_create_outputs(dev);
238 dev_err(dev->dev, "failed to create panel: %d\n", ret);
242 planes = atmel_hlcdc_create_planes(dev);
243 if (IS_ERR(planes)) {
244 dev_err(dev->dev, "failed to create planes\n");
245 return PTR_ERR(planes);
250 dc->layers[planes->primary->layer.desc->id] =
251 &planes->primary->layer;
254 dc->layers[planes->cursor->layer.desc->id] =
255 &planes->cursor->layer;
257 for (i = 0; i < planes->noverlays; i++)
258 dc->layers[planes->overlays[i]->layer.desc->id] =
259 &planes->overlays[i]->layer;
261 ret = atmel_hlcdc_crtc_create(dev);
263 dev_err(dev->dev, "failed to create crtc\n");
267 dev->mode_config.min_width = dc->desc->min_width;
268 dev->mode_config.min_height = dc->desc->min_height;
269 dev->mode_config.max_width = dc->desc->max_width;
270 dev->mode_config.max_height = dc->desc->max_height;
271 dev->mode_config.funcs = &mode_config_funcs;
276 static int atmel_hlcdc_dc_load(struct drm_device *dev)
278 struct platform_device *pdev = to_platform_device(dev->dev);
279 const struct of_device_id *match;
280 struct atmel_hlcdc_dc *dc;
283 match = of_match_node(atmel_hlcdc_of_match, dev->dev->parent->of_node);
285 dev_err(&pdev->dev, "invalid compatible string\n");
290 dev_err(&pdev->dev, "invalid hlcdc description\n");
294 dc = devm_kzalloc(dev->dev, sizeof(*dc), GFP_KERNEL);
298 dc->wq = alloc_ordered_workqueue("atmel-hlcdc-dc", 0);
302 dc->desc = match->data;
303 dc->hlcdc = dev_get_drvdata(dev->dev->parent);
304 dev->dev_private = dc;
306 ret = clk_prepare_enable(dc->hlcdc->periph_clk);
308 dev_err(dev->dev, "failed to enable periph_clk\n");
312 pm_runtime_enable(dev->dev);
314 pm_runtime_put_sync(dev->dev);
316 ret = atmel_hlcdc_dc_modeset_init(dev);
318 dev_err(dev->dev, "failed to initialize mode setting\n");
319 goto err_periph_clk_disable;
322 ret = drm_vblank_init(dev, 1);
324 dev_err(dev->dev, "failed to initialize vblank\n");
325 goto err_periph_clk_disable;
328 pm_runtime_get_sync(dev->dev);
329 ret = drm_irq_install(dev, dc->hlcdc->irq);
330 pm_runtime_put_sync(dev->dev);
332 dev_err(dev->dev, "failed to install IRQ handler\n");
333 goto err_periph_clk_disable;
336 platform_set_drvdata(pdev, dev);
338 drm_kms_helper_poll_init(dev);
340 /* force connectors detection */
341 drm_helper_hpd_irq_event(dev);
345 err_periph_clk_disable:
346 pm_runtime_disable(dev->dev);
347 clk_disable_unprepare(dc->hlcdc->periph_clk);
350 destroy_workqueue(dc->wq);
355 static void atmel_hlcdc_dc_unload(struct drm_device *dev)
357 struct atmel_hlcdc_dc *dc = dev->dev_private;
360 drm_fbdev_cma_fini(dc->fbdev);
361 flush_workqueue(dc->wq);
362 drm_kms_helper_poll_fini(dev);
363 drm_mode_config_cleanup(dev);
364 drm_vblank_cleanup(dev);
366 pm_runtime_get_sync(dev->dev);
367 drm_irq_uninstall(dev);
368 pm_runtime_put_sync(dev->dev);
370 dev->dev_private = NULL;
372 pm_runtime_disable(dev->dev);
373 clk_disable_unprepare(dc->hlcdc->periph_clk);
374 destroy_workqueue(dc->wq);
377 static int atmel_hlcdc_dc_connector_plug_all(struct drm_device *dev)
379 struct drm_connector *connector, *failed;
382 mutex_lock(&dev->mode_config.mutex);
383 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
384 ret = drm_connector_register(connector);
390 mutex_unlock(&dev->mode_config.mutex);
394 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
395 if (failed == connector)
398 drm_connector_unregister(connector);
400 mutex_unlock(&dev->mode_config.mutex);
405 static void atmel_hlcdc_dc_connector_unplug_all(struct drm_device *dev)
407 mutex_lock(&dev->mode_config.mutex);
408 drm_connector_unplug_all(dev);
409 mutex_unlock(&dev->mode_config.mutex);
412 static void atmel_hlcdc_dc_preclose(struct drm_device *dev,
413 struct drm_file *file)
415 struct drm_crtc *crtc;
417 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
418 atmel_hlcdc_crtc_cancel_page_flip(crtc, file);
421 static void atmel_hlcdc_dc_lastclose(struct drm_device *dev)
423 struct atmel_hlcdc_dc *dc = dev->dev_private;
425 drm_fbdev_cma_restore_mode(dc->fbdev);
428 static int atmel_hlcdc_dc_irq_postinstall(struct drm_device *dev)
430 struct atmel_hlcdc_dc *dc = dev->dev_private;
431 unsigned int cfg = 0;
434 /* Enable interrupts on activated layers */
435 for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
437 cfg |= ATMEL_HLCDC_LAYER_STATUS(i);
440 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, cfg);
445 static void atmel_hlcdc_dc_irq_uninstall(struct drm_device *dev)
447 struct atmel_hlcdc_dc *dc = dev->dev_private;
450 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, 0xffffffff);
451 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
454 static int atmel_hlcdc_dc_enable_vblank(struct drm_device *dev, int crtc)
456 struct atmel_hlcdc_dc *dc = dev->dev_private;
458 /* Enable SOF (Start Of Frame) interrupt for vblank counting */
459 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, ATMEL_HLCDC_SOF);
464 static void atmel_hlcdc_dc_disable_vblank(struct drm_device *dev, int crtc)
466 struct atmel_hlcdc_dc *dc = dev->dev_private;
468 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, ATMEL_HLCDC_SOF);
471 static const struct file_operations fops = {
472 .owner = THIS_MODULE,
474 .release = drm_release,
475 .unlocked_ioctl = drm_ioctl,
477 .compat_ioctl = drm_compat_ioctl,
482 .mmap = drm_gem_cma_mmap,
485 static struct drm_driver atmel_hlcdc_dc_driver = {
486 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET,
487 .preclose = atmel_hlcdc_dc_preclose,
488 .lastclose = atmel_hlcdc_dc_lastclose,
489 .irq_handler = atmel_hlcdc_dc_irq_handler,
490 .irq_preinstall = atmel_hlcdc_dc_irq_uninstall,
491 .irq_postinstall = atmel_hlcdc_dc_irq_postinstall,
492 .irq_uninstall = atmel_hlcdc_dc_irq_uninstall,
493 .get_vblank_counter = drm_vblank_count,
494 .enable_vblank = atmel_hlcdc_dc_enable_vblank,
495 .disable_vblank = atmel_hlcdc_dc_disable_vblank,
496 .gem_free_object = drm_gem_cma_free_object,
497 .gem_vm_ops = &drm_gem_cma_vm_ops,
498 .dumb_create = drm_gem_cma_dumb_create,
499 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
500 .dumb_destroy = drm_gem_dumb_destroy,
502 .name = "atmel-hlcdc",
503 .desc = "Atmel HLCD Controller DRM",
509 static int atmel_hlcdc_dc_drm_probe(struct platform_device *pdev)
511 struct drm_device *ddev;
514 ddev = drm_dev_alloc(&atmel_hlcdc_dc_driver, &pdev->dev);
518 ret = drm_dev_set_unique(ddev, dev_name(ddev->dev));
522 ret = atmel_hlcdc_dc_load(ddev);
526 ret = drm_dev_register(ddev, 0);
530 ret = atmel_hlcdc_dc_connector_plug_all(ddev);
537 drm_dev_unregister(ddev);
540 atmel_hlcdc_dc_unload(ddev);
548 static int atmel_hlcdc_dc_drm_remove(struct platform_device *pdev)
550 struct drm_device *ddev = platform_get_drvdata(pdev);
552 atmel_hlcdc_dc_connector_unplug_all(ddev);
553 drm_dev_unregister(ddev);
554 atmel_hlcdc_dc_unload(ddev);
560 static const struct of_device_id atmel_hlcdc_dc_of_match[] = {
561 { .compatible = "atmel,hlcdc-display-controller" },
565 static struct platform_driver atmel_hlcdc_dc_platform_driver = {
566 .probe = atmel_hlcdc_dc_drm_probe,
567 .remove = atmel_hlcdc_dc_drm_remove,
569 .name = "atmel-hlcdc-display-controller",
570 .of_match_table = atmel_hlcdc_dc_of_match,
573 module_platform_driver(atmel_hlcdc_dc_platform_driver);
575 MODULE_AUTHOR("Jean-Jacques Hiblot <jjhiblot@traphandler.com>");
576 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
577 MODULE_DESCRIPTION("Atmel HLCDC Display Controller DRM Driver");
578 MODULE_LICENSE("GPL");
579 MODULE_ALIAS("platform:atmel-hlcdc-dc");