]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.c
drm: atmel-hlcdc: distinguish between sama5d3 and sama5d4
[karo-tx-linux.git] / drivers / gpu / drm / atmel-hlcdc / atmel_hlcdc_dc.c
1 /*
2  * Copyright (C) 2014 Traphandler
3  * Copyright (C) 2014 Free Electrons
4  * Copyright (C) 2014 Atmel
5  *
6  * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
7  * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
8  *
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.
12  *
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.
17  *
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/>.
20  */
21
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>
27
28 #include "atmel_hlcdc_dc.h"
29
30 #define ATMEL_HLCDC_LAYER_IRQS_OFFSET           8
31
32 static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d3_layers[] = {
33         {
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,
46                 },
47         },
48         {
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,
64                 },
65         },
66         {
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,
82                 },
83         },
84         {
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,
102                 },
103         },
104         {
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,
122                 },
123         },
124 };
125
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,
133         .max_sync_width = 64,
134         .max_hfp = 0x200,
135         .max_hbp = 0x200,
136         .max_vfp = 0x40,
137         .max_vbp = 0x3f,
138 };
139
140 static const struct atmel_hlcdc_dc_desc atmel_hlcdc_dc_sama5d4 = {
141         .min_width = 0,
142         .min_height = 0,
143         .max_width = 2048,
144         .max_height = 2048,
145         .nlayers = ARRAY_SIZE(atmel_hlcdc_sama5d3_layers),
146         .layers = atmel_hlcdc_sama5d3_layers,
147         .max_sync_width = 256,
148         .max_hfp = 0x100,
149         .max_hbp = 0x100,
150         .max_vfp = 0x100,
151         .max_vbp = 0xff,
152 };
153
154 static const struct of_device_id atmel_hlcdc_of_match[] = {
155         {
156                 .compatible = "atmel,sama5d3-hlcdc",
157                 .data = &atmel_hlcdc_dc_sama5d3,
158         },
159         {
160                 .compatible = "atmel,sama5d4-hlcdc",
161                 .data = &atmel_hlcdc_dc_sama5d4,
162         },
163         { /* sentinel */ }
164 };
165
166 int atmel_hlcdc_dc_mode_valid(struct atmel_hlcdc_dc *dc,
167                               struct drm_display_mode *mode)
168 {
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;
175
176         if (hsync_len > dc->desc->max_sync_width || hsync_len < 1)
177                 return MODE_HSYNC;
178
179         if (vsync_len > dc->desc->max_sync_width || vsync_len < 1)
180                 return MODE_VSYNC;
181
182         if (hfront_porch > dc->desc->max_hfp || hfront_porch < 1 ||
183             hback_porch > dc->desc->max_hbp || hback_porch < 1 ||
184             mode->hdisplay < 1)
185                 return MODE_H_ILLEGAL;
186
187         if (vfront_porch > dc->desc->max_vfp || vfront_porch < 1 ||
188             vback_porch > dc->desc->max_vbp || vback_porch < 0 ||
189             mode->vdisplay < 1)
190                 return MODE_V_ILLEGAL;
191
192         return MODE_OK;
193 }
194
195 static irqreturn_t atmel_hlcdc_dc_irq_handler(int irq, void *data)
196 {
197         struct drm_device *dev = data;
198         struct atmel_hlcdc_dc *dc = dev->dev_private;
199         unsigned long status;
200         unsigned int imr, isr;
201         int i;
202
203         regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_IMR, &imr);
204         regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
205         status = imr & isr;
206         if (!status)
207                 return IRQ_NONE;
208
209         if (status & ATMEL_HLCDC_SOF)
210                 atmel_hlcdc_crtc_irq(dc->crtc);
211
212         for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
213                 struct atmel_hlcdc_layer *layer = dc->layers[i];
214
215                 if (!(ATMEL_HLCDC_LAYER_STATUS(i) & status) || !layer)
216                         continue;
217
218                 atmel_hlcdc_layer_irq(layer);
219         }
220
221         return IRQ_HANDLED;
222 }
223
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)
226 {
227         return drm_fb_cma_create(dev, file_priv, mode_cmd);
228 }
229
230 static void atmel_hlcdc_fb_output_poll_changed(struct drm_device *dev)
231 {
232         struct atmel_hlcdc_dc *dc = dev->dev_private;
233
234         if (dc->fbdev) {
235                 drm_fbdev_cma_hotplug_event(dc->fbdev);
236         } else {
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))
241                         dc->fbdev = NULL;
242         }
243 }
244
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,
250 };
251
252 static int atmel_hlcdc_dc_modeset_init(struct drm_device *dev)
253 {
254         struct atmel_hlcdc_dc *dc = dev->dev_private;
255         struct atmel_hlcdc_planes *planes;
256         int ret;
257         int i;
258
259         drm_mode_config_init(dev);
260
261         ret = atmel_hlcdc_create_outputs(dev);
262         if (ret) {
263                 dev_err(dev->dev, "failed to create panel: %d\n", ret);
264                 return ret;
265         }
266
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);
271         }
272
273         dc->planes = planes;
274
275         dc->layers[planes->primary->layer.desc->id] =
276                                                 &planes->primary->layer;
277
278         if (planes->cursor)
279                 dc->layers[planes->cursor->layer.desc->id] =
280                                                         &planes->cursor->layer;
281
282         for (i = 0; i < planes->noverlays; i++)
283                 dc->layers[planes->overlays[i]->layer.desc->id] =
284                                                 &planes->overlays[i]->layer;
285
286         ret = atmel_hlcdc_crtc_create(dev);
287         if (ret) {
288                 dev_err(dev->dev, "failed to create crtc\n");
289                 return ret;
290         }
291
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;
297
298         return 0;
299 }
300
301 static int atmel_hlcdc_dc_load(struct drm_device *dev)
302 {
303         struct platform_device *pdev = to_platform_device(dev->dev);
304         const struct of_device_id *match;
305         struct atmel_hlcdc_dc *dc;
306         int ret;
307
308         match = of_match_node(atmel_hlcdc_of_match, dev->dev->parent->of_node);
309         if (!match) {
310                 dev_err(&pdev->dev, "invalid compatible string\n");
311                 return -ENODEV;
312         }
313
314         if (!match->data) {
315                 dev_err(&pdev->dev, "invalid hlcdc description\n");
316                 return -EINVAL;
317         }
318
319         dc = devm_kzalloc(dev->dev, sizeof(*dc), GFP_KERNEL);
320         if (!dc)
321                 return -ENOMEM;
322
323         dc->wq = alloc_ordered_workqueue("atmel-hlcdc-dc", 0);
324         if (!dc->wq)
325                 return -ENOMEM;
326
327         dc->desc = match->data;
328         dc->hlcdc = dev_get_drvdata(dev->dev->parent);
329         dev->dev_private = dc;
330
331         ret = clk_prepare_enable(dc->hlcdc->periph_clk);
332         if (ret) {
333                 dev_err(dev->dev, "failed to enable periph_clk\n");
334                 goto err_destroy_wq;
335         }
336
337         pm_runtime_enable(dev->dev);
338
339         ret = atmel_hlcdc_dc_modeset_init(dev);
340         if (ret < 0) {
341                 dev_err(dev->dev, "failed to initialize mode setting\n");
342                 goto err_periph_clk_disable;
343         }
344
345         drm_mode_config_reset(dev);
346
347         ret = drm_vblank_init(dev, 1);
348         if (ret < 0) {
349                 dev_err(dev->dev, "failed to initialize vblank\n");
350                 goto err_periph_clk_disable;
351         }
352
353         pm_runtime_get_sync(dev->dev);
354         ret = drm_irq_install(dev, dc->hlcdc->irq);
355         pm_runtime_put_sync(dev->dev);
356         if (ret < 0) {
357                 dev_err(dev->dev, "failed to install IRQ handler\n");
358                 goto err_periph_clk_disable;
359         }
360
361         platform_set_drvdata(pdev, dev);
362
363         drm_kms_helper_poll_init(dev);
364
365         /* force connectors detection */
366         drm_helper_hpd_irq_event(dev);
367
368         return 0;
369
370 err_periph_clk_disable:
371         pm_runtime_disable(dev->dev);
372         clk_disable_unprepare(dc->hlcdc->periph_clk);
373
374 err_destroy_wq:
375         destroy_workqueue(dc->wq);
376
377         return ret;
378 }
379
380 static void atmel_hlcdc_dc_unload(struct drm_device *dev)
381 {
382         struct atmel_hlcdc_dc *dc = dev->dev_private;
383
384         if (dc->fbdev)
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);
390
391         pm_runtime_get_sync(dev->dev);
392         drm_irq_uninstall(dev);
393         pm_runtime_put_sync(dev->dev);
394
395         dev->dev_private = NULL;
396
397         pm_runtime_disable(dev->dev);
398         clk_disable_unprepare(dc->hlcdc->periph_clk);
399         destroy_workqueue(dc->wq);
400 }
401
402 static int atmel_hlcdc_dc_connector_plug_all(struct drm_device *dev)
403 {
404         struct drm_connector *connector, *failed;
405         int ret;
406
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);
410                 if (ret) {
411                         failed = connector;
412                         goto err;
413                 }
414         }
415         mutex_unlock(&dev->mode_config.mutex);
416         return 0;
417
418 err:
419         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
420                 if (failed == connector)
421                         break;
422
423                 drm_connector_unregister(connector);
424         }
425         mutex_unlock(&dev->mode_config.mutex);
426
427         return ret;
428 }
429
430 static void atmel_hlcdc_dc_connector_unplug_all(struct drm_device *dev)
431 {
432         mutex_lock(&dev->mode_config.mutex);
433         drm_connector_unplug_all(dev);
434         mutex_unlock(&dev->mode_config.mutex);
435 }
436
437 static void atmel_hlcdc_dc_preclose(struct drm_device *dev,
438                                     struct drm_file *file)
439 {
440         struct drm_crtc *crtc;
441
442         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
443                 atmel_hlcdc_crtc_cancel_page_flip(crtc, file);
444 }
445
446 static void atmel_hlcdc_dc_lastclose(struct drm_device *dev)
447 {
448         struct atmel_hlcdc_dc *dc = dev->dev_private;
449
450         drm_fbdev_cma_restore_mode(dc->fbdev);
451 }
452
453 static int atmel_hlcdc_dc_irq_postinstall(struct drm_device *dev)
454 {
455         struct atmel_hlcdc_dc *dc = dev->dev_private;
456         unsigned int cfg = 0;
457         int i;
458
459         /* Enable interrupts on activated layers */
460         for (i = 0; i < ATMEL_HLCDC_MAX_LAYERS; i++) {
461                 if (dc->layers[i])
462                         cfg |= ATMEL_HLCDC_LAYER_STATUS(i);
463         }
464
465         regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, cfg);
466
467         return 0;
468 }
469
470 static void atmel_hlcdc_dc_irq_uninstall(struct drm_device *dev)
471 {
472         struct atmel_hlcdc_dc *dc = dev->dev_private;
473         unsigned int isr;
474
475         regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, 0xffffffff);
476         regmap_read(dc->hlcdc->regmap, ATMEL_HLCDC_ISR, &isr);
477 }
478
479 static int atmel_hlcdc_dc_enable_vblank(struct drm_device *dev, int crtc)
480 {
481         struct atmel_hlcdc_dc *dc = dev->dev_private;
482
483         /* Enable SOF (Start Of Frame) interrupt for vblank counting */
484         regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IER, ATMEL_HLCDC_SOF);
485
486         return 0;
487 }
488
489 static void atmel_hlcdc_dc_disable_vblank(struct drm_device *dev, int crtc)
490 {
491         struct atmel_hlcdc_dc *dc = dev->dev_private;
492
493         regmap_write(dc->hlcdc->regmap, ATMEL_HLCDC_IDR, ATMEL_HLCDC_SOF);
494 }
495
496 static const struct file_operations fops = {
497         .owner              = THIS_MODULE,
498         .open               = drm_open,
499         .release            = drm_release,
500         .unlocked_ioctl     = drm_ioctl,
501 #ifdef CONFIG_COMPAT
502         .compat_ioctl       = drm_compat_ioctl,
503 #endif
504         .poll               = drm_poll,
505         .read               = drm_read,
506         .llseek             = no_llseek,
507         .mmap               = drm_gem_cma_mmap,
508 };
509
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,
526         .fops = &fops,
527         .name = "atmel-hlcdc",
528         .desc = "Atmel HLCD Controller DRM",
529         .date = "20141504",
530         .major = 1,
531         .minor = 0,
532 };
533
534 static int atmel_hlcdc_dc_drm_probe(struct platform_device *pdev)
535 {
536         struct drm_device *ddev;
537         int ret;
538
539         ddev = drm_dev_alloc(&atmel_hlcdc_dc_driver, &pdev->dev);
540         if (!ddev)
541                 return -ENOMEM;
542
543         ret = drm_dev_set_unique(ddev, dev_name(ddev->dev));
544         if (ret)
545                 goto err_unref;
546
547         ret = atmel_hlcdc_dc_load(ddev);
548         if (ret)
549                 goto err_unref;
550
551         ret = drm_dev_register(ddev, 0);
552         if (ret)
553                 goto err_unload;
554
555         ret = atmel_hlcdc_dc_connector_plug_all(ddev);
556         if (ret)
557                 goto err_unregister;
558
559         return 0;
560
561 err_unregister:
562         drm_dev_unregister(ddev);
563
564 err_unload:
565         atmel_hlcdc_dc_unload(ddev);
566
567 err_unref:
568         drm_dev_unref(ddev);
569
570         return ret;
571 }
572
573 static int atmel_hlcdc_dc_drm_remove(struct platform_device *pdev)
574 {
575         struct drm_device *ddev = platform_get_drvdata(pdev);
576
577         atmel_hlcdc_dc_connector_unplug_all(ddev);
578         drm_dev_unregister(ddev);
579         atmel_hlcdc_dc_unload(ddev);
580         drm_dev_unref(ddev);
581
582         return 0;
583 }
584
585 #ifdef CONFIG_PM
586 static int atmel_hlcdc_dc_drm_suspend(struct device *dev)
587 {
588         struct drm_device *drm_dev = dev_get_drvdata(dev);
589         struct drm_crtc *crtc;
590
591         if (pm_runtime_suspended(dev))
592                 return 0;
593
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);
598         return 0;
599 }
600
601 static int atmel_hlcdc_dc_drm_resume(struct device *dev)
602 {
603         struct drm_device *drm_dev = dev_get_drvdata(dev);
604         struct drm_crtc *crtc;
605
606         if (pm_runtime_suspended(dev))
607                 return 0;
608
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);
613         return 0;
614 }
615 #endif
616
617 static SIMPLE_DEV_PM_OPS(atmel_hlcdc_dc_drm_pm_ops,
618                 atmel_hlcdc_dc_drm_suspend, atmel_hlcdc_dc_drm_resume);
619
620 static const struct of_device_id atmel_hlcdc_dc_of_match[] = {
621         { .compatible = "atmel,hlcdc-display-controller" },
622         { }
623 };
624
625 static struct platform_driver atmel_hlcdc_dc_platform_driver = {
626         .probe  = atmel_hlcdc_dc_drm_probe,
627         .remove = atmel_hlcdc_dc_drm_remove,
628         .driver = {
629                 .name   = "atmel-hlcdc-display-controller",
630                 .pm     = &atmel_hlcdc_dc_drm_pm_ops,
631                 .of_match_table = atmel_hlcdc_dc_of_match,
632         },
633 };
634 module_platform_driver(atmel_hlcdc_dc_platform_driver);
635
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");