Linux 4.2.1
[linux/fpc-iii.git] / drivers / gpu / drm / atmel-hlcdc / atmel_hlcdc_dc.c
blobef6182bc8e5eef229dc990354f6d31c18915f3bc
1 /*
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
16 * more details.
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[] = {
34 .name = "base",
35 .formats = &atmel_hlcdc_plane_rgb_formats,
36 .regs_offset = 0x40,
37 .id = 0,
38 .type = ATMEL_HLCDC_BASE_LAYER,
39 .nconfigs = 7,
40 .layout = {
41 .xstride = { 2 },
42 .default_color = 3,
43 .general_config = 4,
44 .disc_pos = 5,
45 .disc_size = 6,
49 .name = "overlay1",
50 .formats = &atmel_hlcdc_plane_rgb_formats,
51 .regs_offset = 0x140,
52 .id = 1,
53 .type = ATMEL_HLCDC_OVERLAY_LAYER,
54 .nconfigs = 10,
55 .layout = {
56 .pos = 2,
57 .size = 3,
58 .xstride = { 4 },
59 .pstride = { 5 },
60 .default_color = 6,
61 .chroma_key = 7,
62 .chroma_key_mask = 8,
63 .general_config = 9,
67 .name = "overlay2",
68 .formats = &atmel_hlcdc_plane_rgb_formats,
69 .regs_offset = 0x240,
70 .id = 2,
71 .type = ATMEL_HLCDC_OVERLAY_LAYER,
72 .nconfigs = 10,
73 .layout = {
74 .pos = 2,
75 .size = 3,
76 .xstride = { 4 },
77 .pstride = { 5 },
78 .default_color = 6,
79 .chroma_key = 7,
80 .chroma_key_mask = 8,
81 .general_config = 9,
85 .name = "high-end-overlay",
86 .formats = &atmel_hlcdc_plane_rgb_and_yuv_formats,
87 .regs_offset = 0x340,
88 .id = 3,
89 .type = ATMEL_HLCDC_OVERLAY_LAYER,
90 .nconfigs = 42,
91 .layout = {
92 .pos = 2,
93 .size = 3,
94 .memsize = 4,
95 .xstride = { 5, 7 },
96 .pstride = { 6, 8 },
97 .default_color = 9,
98 .chroma_key = 10,
99 .chroma_key_mask = 11,
100 .general_config = 12,
101 .csc = 14,
105 .name = "cursor",
106 .formats = &atmel_hlcdc_plane_rgb_formats,
107 .regs_offset = 0x440,
108 .id = 4,
109 .type = ATMEL_HLCDC_CURSOR_LAYER,
110 .nconfigs = 10,
111 .max_width = 128,
112 .max_height = 128,
113 .layout = {
114 .pos = 2,
115 .size = 3,
116 .xstride = { 4 },
117 .pstride = { 5 },
118 .default_color = 6,
119 .chroma_key = 7,
120 .chroma_key_mask = 8,
121 .general_config = 9,
126 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d3 = {
127 .min_width = 0,
128 .min_height = 0,
129 .max_width = 2048,
130 .max_height = 2048,
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,
140 { /* sentinel */ },
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)
154 return MODE_HSYNC;
156 if (vsync_len > 0x40 || vsync_len < 1)
157 return MODE_VSYNC;
159 if (hfront_porch > 0x200 || hfront_porch < 1 ||
160 hback_porch > 0x200 || hback_porch < 1 ||
161 mode->hdisplay < 1)
162 return MODE_H_ILLEGAL;
164 if (vfront_porch > 0x40 || vfront_porch < 1 ||
165 vback_porch > 0x40 || vback_porch < 0 ||
166 mode->vdisplay < 1)
167 return MODE_V_ILLEGAL;
169 return MODE_OK;
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;
178 int i;
180 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_IMR, &imr);
181 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
182 status = imr & isr;
183 if (!status)
184 return IRQ_NONE;
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)
193 continue;
195 atmel_hlcdc_layer_irq(layer);
198 return IRQ_HANDLED;
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;
211 if (dc->fbdev) {
212 drm_fbdev_cma_hotplug_event(dc->fbdev);
213 } else {
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))
218 dc->fbdev = NULL;
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,
225 .atomic_check = drm_atomic_helper_check,
226 .atomic_commit = drm_atomic_helper_commit,
229 static int atmel_hlcdc_dc_modeset_init(struct drm_device *dev)
231 struct atmel_hlcdc_dc *dc = dev->dev_private;
232 struct atmel_hlcdc_planes *planes;
233 int ret;
234 int i;
236 drm_mode_config_init(dev);
238 ret = atmel_hlcdc_create_outputs(dev);
239 if (ret) {
240 dev_err(dev->dev, "failed to create panel: %d\n", ret);
241 return ret;
244 planes = atmel_hlcdc_create_planes(dev);
245 if (IS_ERR(planes)) {
246 dev_err(dev->dev, "failed to create planes\n");
247 return PTR_ERR(planes);
250 dc->planes = planes;
252 dc->layers[planes->primary->layer.desc->id] =
253 &planes->primary->layer;
255 if (planes->cursor)
256 dc->layers[planes->cursor->layer.desc->id] =
257 &planes->cursor->layer;
259 for (i = 0; i < planes->noverlays; i++)
260 dc->layers[planes->overlays[i]->layer.desc->id] =
261 &planes->overlays[i]->layer;
263 ret = atmel_hlcdc_crtc_create(dev);
264 if (ret) {
265 dev_err(dev->dev, "failed to create crtc\n");
266 return ret;
269 dev->mode_config.min_width = dc->desc->min_width;
270 dev->mode_config.min_height = dc->desc->min_height;
271 dev->mode_config.max_width = dc->desc->max_width;
272 dev->mode_config.max_height = dc->desc->max_height;
273 dev->mode_config.funcs = &mode_config_funcs;
275 return 0;
278 static int atmel_hlcdc_dc_load(struct drm_device *dev)
280 struct platform_device *pdev = to_platform_device(dev->dev);
281 const struct of_device_id *match;
282 struct atmel_hlcdc_dc *dc;
283 int ret;
285 match = of_match_node(atmel_hlcdc_of_match, dev->dev->parent->of_node);
286 if (!match) {
287 dev_err(&pdev->dev, "invalid compatible string\n");
288 return -ENODEV;
291 if (!match->data) {
292 dev_err(&pdev->dev, "invalid hlcdc description\n");
293 return -EINVAL;
296 dc = devm_kzalloc(dev->dev, sizeof(*dc), GFP_KERNEL);
297 if (!dc)
298 return -ENOMEM;
300 dc->wq = alloc_ordered_workqueue("atmel-hlcdc-dc", 0);
301 if (!dc->wq)
302 return -ENOMEM;
304 dc->desc = match->data;
305 dc->hlcdc = dev_get_drvdata(dev->dev->parent);
306 dev->dev_private = dc;
308 ret = clk_prepare_enable(dc->hlcdc->periph_clk);
309 if (ret) {
310 dev_err(dev->dev, "failed to enable periph_clk\n");
311 goto err_destroy_wq;
314 pm_runtime_enable(dev->dev);
316 ret = drm_vblank_init(dev, 1);
317 if (ret < 0) {
318 dev_err(dev->dev, "failed to initialize vblank\n");
319 goto err_periph_clk_disable;
322 ret = atmel_hlcdc_dc_modeset_init(dev);
323 if (ret < 0) {
324 dev_err(dev->dev, "failed to initialize mode setting\n");
325 goto err_periph_clk_disable;
328 drm_mode_config_reset(dev);
330 pm_runtime_get_sync(dev->dev);
331 ret = drm_irq_install(dev, dc->hlcdc->irq);
332 pm_runtime_put_sync(dev->dev);
333 if (ret < 0) {
334 dev_err(dev->dev, "failed to install IRQ handler\n");
335 goto err_periph_clk_disable;
338 platform_set_drvdata(pdev, dev);
340 drm_kms_helper_poll_init(dev);
342 /* force connectors detection */
343 drm_helper_hpd_irq_event(dev);
345 return 0;
347 err_periph_clk_disable:
348 pm_runtime_disable(dev->dev);
349 clk_disable_unprepare(dc->hlcdc->periph_clk);
351 err_destroy_wq:
352 destroy_workqueue(dc->wq);
354 return ret;
357 static void atmel_hlcdc_dc_unload(struct drm_device *dev)
359 struct atmel_hlcdc_dc *dc = dev->dev_private;
361 if (dc->fbdev)
362 drm_fbdev_cma_fini(dc->fbdev);
363 flush_workqueue(dc->wq);
364 drm_kms_helper_poll_fini(dev);
365 drm_mode_config_cleanup(dev);
366 drm_vblank_cleanup(dev);
368 pm_runtime_get_sync(dev->dev);
369 drm_irq_uninstall(dev);
370 pm_runtime_put_sync(dev->dev);
372 dev->dev_private = NULL;
374 pm_runtime_disable(dev->dev);
375 clk_disable_unprepare(dc->hlcdc->periph_clk);
376 destroy_workqueue(dc->wq);
379 static int atmel_hlcdc_dc_connector_plug_all(struct drm_device *dev)
381 struct drm_connector *connector, *failed;
382 int ret;
384 mutex_lock(&dev->mode_config.mutex);
385 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
386 ret = drm_connector_register(connector);
387 if (ret) {
388 failed = connector;
389 goto err;
392 mutex_unlock(&dev->mode_config.mutex);
393 return 0;
395 err:
396 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
397 if (failed == connector)
398 break;
400 drm_connector_unregister(connector);
402 mutex_unlock(&dev->mode_config.mutex);
404 return ret;
407 static void atmel_hlcdc_dc_connector_unplug_all(struct drm_device *dev)
409 mutex_lock(&dev->mode_config.mutex);
410 drm_connector_unplug_all(dev);
411 mutex_unlock(&dev->mode_config.mutex);
414 static void atmel_hlcdc_dc_preclose(struct drm_device *dev,
415 struct drm_file *file)
417 struct drm_crtc *crtc;
419 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
420 atmel_hlcdc_crtc_cancel_page_flip(crtc, file);
423 static void atmel_hlcdc_dc_lastclose(struct drm_device *dev)
425 struct atmel_hlcdc_dc *dc = dev->dev_private;
427 drm_fbdev_cma_restore_mode(dc->fbdev);
430 static int atmel_hlcdc_dc_irq_postinstall(struct drm_device *dev)
432 struct atmel_hlcdc_dc *dc = dev->dev_private;
433 unsigned int cfg = 0;
434 int i;
436 /* Enable interrupts on activated layers */
437 for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
438 if (dc->layers[i])
439 cfg |= ATMEL_HLCDC_LAYER_STATUS(i);
442 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, cfg);
444 return 0;
447 static void atmel_hlcdc_dc_irq_uninstall(struct drm_device *dev)
449 struct atmel_hlcdc_dc *dc = dev->dev_private;
450 unsigned int isr;
452 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, 0xffffffff);
453 regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
456 static int atmel_hlcdc_dc_enable_vblank(struct drm_device *dev, int crtc)
458 struct atmel_hlcdc_dc *dc = dev->dev_private;
460 /* Enable SOF (Start Of Frame) interrupt for vblank counting */
461 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, ATMEL_HLCDC_SOF);
463 return 0;
466 static void atmel_hlcdc_dc_disable_vblank(struct drm_device *dev, int crtc)
468 struct atmel_hlcdc_dc *dc = dev->dev_private;
470 regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, ATMEL_HLCDC_SOF);
473 static const struct file_operations fops = {
474 .owner = THIS_MODULE,
475 .open = drm_open,
476 .release = drm_release,
477 .unlocked_ioctl = drm_ioctl,
478 #ifdef CONFIG_COMPAT
479 .compat_ioctl = drm_compat_ioctl,
480 #endif
481 .poll = drm_poll,
482 .read = drm_read,
483 .llseek = no_llseek,
484 .mmap = drm_gem_cma_mmap,
487 static struct drm_driver atmel_hlcdc_dc_driver = {
488 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET,
489 .preclose = atmel_hlcdc_dc_preclose,
490 .lastclose = atmel_hlcdc_dc_lastclose,
491 .irq_handler = atmel_hlcdc_dc_irq_handler,
492 .irq_preinstall = atmel_hlcdc_dc_irq_uninstall,
493 .irq_postinstall = atmel_hlcdc_dc_irq_postinstall,
494 .irq_uninstall = atmel_hlcdc_dc_irq_uninstall,
495 .get_vblank_counter = drm_vblank_count,
496 .enable_vblank = atmel_hlcdc_dc_enable_vblank,
497 .disable_vblank = atmel_hlcdc_dc_disable_vblank,
498 .gem_free_object = drm_gem_cma_free_object,
499 .gem_vm_ops = &drm_gem_cma_vm_ops,
500 .dumb_create = drm_gem_cma_dumb_create,
501 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
502 .dumb_destroy = drm_gem_dumb_destroy,
503 .fops = &fops,
504 .name = "atmel-hlcdc",
505 .desc = "Atmel HLCD Controller DRM",
506 .date = "20141504",
507 .major = 1,
508 .minor = 0,
511 static int atmel_hlcdc_dc_drm_probe(struct platform_device *pdev)
513 struct drm_device *ddev;
514 int ret;
516 ddev = drm_dev_alloc(&atmel_hlcdc_dc_driver, &pdev->dev);
517 if (!ddev)
518 return -ENOMEM;
520 ret = drm_dev_set_unique(ddev, dev_name(ddev->dev));
521 if (ret)
522 goto err_unref;
524 ret = atmel_hlcdc_dc_load(ddev);
525 if (ret)
526 goto err_unref;
528 ret = drm_dev_register(ddev, 0);
529 if (ret)
530 goto err_unload;
532 ret = atmel_hlcdc_dc_connector_plug_all(ddev);
533 if (ret)
534 goto err_unregister;
536 return 0;
538 err_unregister:
539 drm_dev_unregister(ddev);
541 err_unload:
542 atmel_hlcdc_dc_unload(ddev);
544 err_unref:
545 drm_dev_unref(ddev);
547 return ret;
550 static int atmel_hlcdc_dc_drm_remove(struct platform_device *pdev)
552 struct drm_device *ddev = platform_get_drvdata(pdev);
554 atmel_hlcdc_dc_connector_unplug_all(ddev);
555 drm_dev_unregister(ddev);
556 atmel_hlcdc_dc_unload(ddev);
557 drm_dev_unref(ddev);
559 return 0;
562 #ifdef CONFIG_PM_SLEEP
563 static int atmel_hlcdc_dc_drm_suspend(struct device *dev)
565 struct drm_device *drm_dev = dev_get_drvdata(dev);
566 struct drm_crtc *crtc;
568 if (pm_runtime_suspended(dev))
569 return 0;
571 drm_modeset_lock_all(drm_dev);
572 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head)
573 atmel_hlcdc_crtc_suspend(crtc);
574 drm_modeset_unlock_all(drm_dev);
575 return 0;
578 static int atmel_hlcdc_dc_drm_resume(struct device *dev)
580 struct drm_device *drm_dev = dev_get_drvdata(dev);
581 struct drm_crtc *crtc;
583 if (pm_runtime_suspended(dev))
584 return 0;
586 drm_modeset_lock_all(drm_dev);
587 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head)
588 atmel_hlcdc_crtc_resume(crtc);
589 drm_modeset_unlock_all(drm_dev);
590 return 0;
592 #endif
594 static SIMPLE_DEV_PM_OPS(atmel_hlcdc_dc_drm_pm_ops,
595 atmel_hlcdc_dc_drm_suspend, atmel_hlcdc_dc_drm_resume);
597 static const struct of_device_id atmel_hlcdc_dc_of_match[] = {
598 { .compatible = "atmel,hlcdc-display-controller" },
599 { },
602 static struct platform_driver atmel_hlcdc_dc_platform_driver = {
603 .probe = atmel_hlcdc_dc_drm_probe,
604 .remove = atmel_hlcdc_dc_drm_remove,
605 .driver = {
606 .name = "atmel-hlcdc-display-controller",
607 .pm = &atmel_hlcdc_dc_drm_pm_ops,
608 .of_match_table = atmel_hlcdc_dc_of_match,
611 module_platform_driver(atmel_hlcdc_dc_platform_driver);
613 MODULE_AUTHOR("Jean-Jacques Hiblot <jjhiblot@traphandler.com>");
614 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
615 MODULE_DESCRIPTION("Atmel HLCDC Display Controller DRM Driver");
616 MODULE_LICENSE("GPL");
617 MODULE_ALIAS("platform:atmel-hlcdc-dc");