]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/rcar-du/rcar_du_kms.c
ASoC: rt5663: rename rt5668 as rt5663 v2
[linux.git] / drivers / gpu / drm / rcar-du / rcar_du_kms.c
1 /*
2  * rcar_du_kms.c  --  R-Car Display Unit Mode Setting
3  *
4  * Copyright (C) 2013-2015 Renesas Electronics Corporation
5  *
6  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <drm/drmP.h>
15 #include <drm/drm_atomic.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_crtc_helper.h>
19 #include <drm/drm_fb_cma_helper.h>
20 #include <drm/drm_gem_cma_helper.h>
21
22 #include <linux/of_graph.h>
23 #include <linux/wait.h>
24
25 #include "rcar_du_crtc.h"
26 #include "rcar_du_drv.h"
27 #include "rcar_du_encoder.h"
28 #include "rcar_du_kms.h"
29 #include "rcar_du_lvdsenc.h"
30 #include "rcar_du_regs.h"
31 #include "rcar_du_vsp.h"
32
33 /* -----------------------------------------------------------------------------
34  * Format helpers
35  */
36
37 static const struct rcar_du_format_info rcar_du_format_infos[] = {
38         {
39                 .fourcc = DRM_FORMAT_RGB565,
40                 .bpp = 16,
41                 .planes = 1,
42                 .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
43                 .edf = PnDDCR4_EDF_NONE,
44         }, {
45                 .fourcc = DRM_FORMAT_ARGB1555,
46                 .bpp = 16,
47                 .planes = 1,
48                 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
49                 .edf = PnDDCR4_EDF_NONE,
50         }, {
51                 .fourcc = DRM_FORMAT_XRGB1555,
52                 .bpp = 16,
53                 .planes = 1,
54                 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
55                 .edf = PnDDCR4_EDF_NONE,
56         }, {
57                 .fourcc = DRM_FORMAT_XRGB8888,
58                 .bpp = 32,
59                 .planes = 1,
60                 .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
61                 .edf = PnDDCR4_EDF_RGB888,
62         }, {
63                 .fourcc = DRM_FORMAT_ARGB8888,
64                 .bpp = 32,
65                 .planes = 1,
66                 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_16BPP,
67                 .edf = PnDDCR4_EDF_ARGB8888,
68         }, {
69                 .fourcc = DRM_FORMAT_UYVY,
70                 .bpp = 16,
71                 .planes = 1,
72                 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
73                 .edf = PnDDCR4_EDF_NONE,
74         }, {
75                 .fourcc = DRM_FORMAT_YUYV,
76                 .bpp = 16,
77                 .planes = 1,
78                 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
79                 .edf = PnDDCR4_EDF_NONE,
80         }, {
81                 .fourcc = DRM_FORMAT_NV12,
82                 .bpp = 12,
83                 .planes = 2,
84                 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
85                 .edf = PnDDCR4_EDF_NONE,
86         }, {
87                 .fourcc = DRM_FORMAT_NV21,
88                 .bpp = 12,
89                 .planes = 2,
90                 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
91                 .edf = PnDDCR4_EDF_NONE,
92         }, {
93                 .fourcc = DRM_FORMAT_NV16,
94                 .bpp = 16,
95                 .planes = 2,
96                 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
97                 .edf = PnDDCR4_EDF_NONE,
98         },
99         /* The following formats are not supported on Gen2 and thus have no
100          * associated .pnmr or .edf settings.
101          */
102         {
103                 .fourcc = DRM_FORMAT_NV61,
104                 .bpp = 16,
105                 .planes = 2,
106         }, {
107                 .fourcc = DRM_FORMAT_YUV420,
108                 .bpp = 12,
109                 .planes = 3,
110         }, {
111                 .fourcc = DRM_FORMAT_YVU420,
112                 .bpp = 12,
113                 .planes = 3,
114         }, {
115                 .fourcc = DRM_FORMAT_YUV422,
116                 .bpp = 16,
117                 .planes = 3,
118         }, {
119                 .fourcc = DRM_FORMAT_YVU422,
120                 .bpp = 16,
121                 .planes = 3,
122         }, {
123                 .fourcc = DRM_FORMAT_YUV444,
124                 .bpp = 24,
125                 .planes = 3,
126         }, {
127                 .fourcc = DRM_FORMAT_YVU444,
128                 .bpp = 24,
129                 .planes = 3,
130         },
131 };
132
133 const struct rcar_du_format_info *rcar_du_format_info(u32 fourcc)
134 {
135         unsigned int i;
136
137         for (i = 0; i < ARRAY_SIZE(rcar_du_format_infos); ++i) {
138                 if (rcar_du_format_infos[i].fourcc == fourcc)
139                         return &rcar_du_format_infos[i];
140         }
141
142         return NULL;
143 }
144
145 /* -----------------------------------------------------------------------------
146  * Frame buffer
147  */
148
149 int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev,
150                         struct drm_mode_create_dumb *args)
151 {
152         struct rcar_du_device *rcdu = dev->dev_private;
153         unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
154         unsigned int align;
155
156         /* The R8A7779 DU requires a 16 pixels pitch alignment as documented,
157          * but the R8A7790 DU seems to require a 128 bytes pitch alignment.
158          */
159         if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
160                 align = 128;
161         else
162                 align = 16 * args->bpp / 8;
163
164         args->pitch = roundup(min_pitch, align);
165
166         return drm_gem_cma_dumb_create_internal(file, dev, args);
167 }
168
169 static struct drm_framebuffer *
170 rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
171                   const struct drm_mode_fb_cmd2 *mode_cmd)
172 {
173         struct rcar_du_device *rcdu = dev->dev_private;
174         const struct rcar_du_format_info *format;
175         unsigned int max_pitch;
176         unsigned int align;
177         unsigned int bpp;
178         unsigned int i;
179
180         format = rcar_du_format_info(mode_cmd->pixel_format);
181         if (format == NULL) {
182                 dev_dbg(dev->dev, "unsupported pixel format %08x\n",
183                         mode_cmd->pixel_format);
184                 return ERR_PTR(-EINVAL);
185         }
186
187         /*
188          * The pitch and alignment constraints are expressed in pixels on the
189          * hardware side and in bytes in the DRM API.
190          */
191         bpp = format->planes == 1 ? format->bpp / 8 : 1;
192         max_pitch =  4096 * bpp;
193
194         if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
195                 align = 128;
196         else
197                 align = 16 * bpp;
198
199         if (mode_cmd->pitches[0] & (align - 1) ||
200             mode_cmd->pitches[0] >= max_pitch) {
201                 dev_dbg(dev->dev, "invalid pitch value %u\n",
202                         mode_cmd->pitches[0]);
203                 return ERR_PTR(-EINVAL);
204         }
205
206         for (i = 1; i < format->planes; ++i) {
207                 if (mode_cmd->pitches[i] != mode_cmd->pitches[0]) {
208                         dev_dbg(dev->dev,
209                                 "luma and chroma pitches do not match\n");
210                         return ERR_PTR(-EINVAL);
211                 }
212         }
213
214         return drm_fb_cma_create(dev, file_priv, mode_cmd);
215 }
216
217 static void rcar_du_output_poll_changed(struct drm_device *dev)
218 {
219         struct rcar_du_device *rcdu = dev->dev_private;
220
221         drm_fbdev_cma_hotplug_event(rcdu->fbdev);
222 }
223
224 /* -----------------------------------------------------------------------------
225  * Atomic Check and Update
226  */
227
228 static int rcar_du_atomic_check(struct drm_device *dev,
229                                 struct drm_atomic_state *state)
230 {
231         struct rcar_du_device *rcdu = dev->dev_private;
232         int ret;
233
234         ret = drm_atomic_helper_check(dev, state);
235         if (ret < 0)
236                 return ret;
237
238         if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE))
239                 return 0;
240
241         return rcar_du_atomic_check_planes(dev, state);
242 }
243
244 struct rcar_du_commit {
245         struct work_struct work;
246         struct drm_device *dev;
247         struct drm_atomic_state *state;
248         u32 crtcs;
249 };
250
251 static void rcar_du_atomic_complete(struct rcar_du_commit *commit)
252 {
253         struct drm_device *dev = commit->dev;
254         struct rcar_du_device *rcdu = dev->dev_private;
255         struct drm_atomic_state *old_state = commit->state;
256
257         /* Apply the atomic update. */
258         drm_atomic_helper_commit_modeset_disables(dev, old_state);
259         drm_atomic_helper_commit_modeset_enables(dev, old_state);
260         drm_atomic_helper_commit_planes(dev, old_state,
261                                         DRM_PLANE_COMMIT_ACTIVE_ONLY);
262
263         drm_atomic_helper_wait_for_vblanks(dev, old_state);
264
265         drm_atomic_helper_cleanup_planes(dev, old_state);
266
267         drm_atomic_state_free(old_state);
268
269         /* Complete the commit, wake up any waiter. */
270         spin_lock(&rcdu->commit.wait.lock);
271         rcdu->commit.pending &= ~commit->crtcs;
272         wake_up_all_locked(&rcdu->commit.wait);
273         spin_unlock(&rcdu->commit.wait.lock);
274
275         kfree(commit);
276 }
277
278 static void rcar_du_atomic_work(struct work_struct *work)
279 {
280         struct rcar_du_commit *commit =
281                 container_of(work, struct rcar_du_commit, work);
282
283         rcar_du_atomic_complete(commit);
284 }
285
286 static int rcar_du_atomic_commit(struct drm_device *dev,
287                                  struct drm_atomic_state *state,
288                                  bool nonblock)
289 {
290         struct rcar_du_device *rcdu = dev->dev_private;
291         struct rcar_du_commit *commit;
292         struct drm_crtc *crtc;
293         struct drm_crtc_state *crtc_state;
294         unsigned int i;
295         int ret;
296
297         ret = drm_atomic_helper_prepare_planes(dev, state);
298         if (ret)
299                 return ret;
300
301         /* Allocate the commit object. */
302         commit = kzalloc(sizeof(*commit), GFP_KERNEL);
303         if (commit == NULL) {
304                 ret = -ENOMEM;
305                 goto error;
306         }
307
308         INIT_WORK(&commit->work, rcar_du_atomic_work);
309         commit->dev = dev;
310         commit->state = state;
311
312         /* Wait until all affected CRTCs have completed previous commits and
313          * mark them as pending.
314          */
315         for_each_crtc_in_state(state, crtc, crtc_state, i)
316                 commit->crtcs |= drm_crtc_mask(crtc);
317
318         spin_lock(&rcdu->commit.wait.lock);
319         ret = wait_event_interruptible_locked(rcdu->commit.wait,
320                         !(rcdu->commit.pending & commit->crtcs));
321         if (ret == 0)
322                 rcdu->commit.pending |= commit->crtcs;
323         spin_unlock(&rcdu->commit.wait.lock);
324
325         if (ret) {
326                 kfree(commit);
327                 goto error;
328         }
329
330         /* Swap the state, this is the point of no return. */
331         drm_atomic_helper_swap_state(state, true);
332
333         if (nonblock)
334                 schedule_work(&commit->work);
335         else
336                 rcar_du_atomic_complete(commit);
337
338         return 0;
339
340 error:
341         drm_atomic_helper_cleanup_planes(dev, state);
342         return ret;
343 }
344
345 /* -----------------------------------------------------------------------------
346  * Initialization
347  */
348
349 static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
350         .fb_create = rcar_du_fb_create,
351         .output_poll_changed = rcar_du_output_poll_changed,
352         .atomic_check = rcar_du_atomic_check,
353         .atomic_commit = rcar_du_atomic_commit,
354 };
355
356 static int rcar_du_encoders_init_one(struct rcar_du_device *rcdu,
357                                      enum rcar_du_output output,
358                                      struct of_endpoint *ep)
359 {
360         static const struct {
361                 const char *compatible;
362                 enum rcar_du_encoder_type type;
363         } encoders[] = {
364                 { "adi,adv7123", RCAR_DU_ENCODER_VGA },
365                 { "adi,adv7511w", RCAR_DU_ENCODER_HDMI },
366                 { "thine,thc63lvdm83d", RCAR_DU_ENCODER_LVDS },
367         };
368
369         enum rcar_du_encoder_type enc_type = RCAR_DU_ENCODER_NONE;
370         struct device_node *connector = NULL;
371         struct device_node *encoder = NULL;
372         struct device_node *ep_node = NULL;
373         struct device_node *entity_ep_node;
374         struct device_node *entity;
375         int ret;
376
377         /*
378          * Locate the connected entity and infer its type from the number of
379          * endpoints.
380          */
381         entity = of_graph_get_remote_port_parent(ep->local_node);
382         if (!entity) {
383                 dev_dbg(rcdu->dev, "unconnected endpoint %s, skipping\n",
384                         ep->local_node->full_name);
385                 return -ENODEV;
386         }
387
388         entity_ep_node = of_parse_phandle(ep->local_node, "remote-endpoint", 0);
389
390         for_each_endpoint_of_node(entity, ep_node) {
391                 if (ep_node == entity_ep_node)
392                         continue;
393
394                 /*
395                  * We've found one endpoint other than the input, this must
396                  * be an encoder. Locate the connector.
397                  */
398                 encoder = entity;
399                 connector = of_graph_get_remote_port_parent(ep_node);
400                 of_node_put(ep_node);
401
402                 if (!connector) {
403                         dev_warn(rcdu->dev,
404                                  "no connector for encoder %s, skipping\n",
405                                  encoder->full_name);
406                         of_node_put(entity_ep_node);
407                         of_node_put(encoder);
408                         return -ENODEV;
409                 }
410
411                 break;
412         }
413
414         of_node_put(entity_ep_node);
415
416         if (encoder) {
417                 /*
418                  * If an encoder has been found, get its type based on its
419                  * compatible string.
420                  */
421                 unsigned int i;
422
423                 for (i = 0; i < ARRAY_SIZE(encoders); ++i) {
424                         if (of_device_is_compatible(encoder,
425                                                     encoders[i].compatible)) {
426                                 enc_type = encoders[i].type;
427                                 break;
428                         }
429                 }
430
431                 if (i == ARRAY_SIZE(encoders)) {
432                         dev_warn(rcdu->dev,
433                                  "unknown encoder type for %s, skipping\n",
434                                  encoder->full_name);
435                         of_node_put(encoder);
436                         of_node_put(connector);
437                         return -EINVAL;
438                 }
439         } else {
440                 /*
441                  * If no encoder has been found the entity must be the
442                  * connector.
443                  */
444                 connector = entity;
445         }
446
447         ret = rcar_du_encoder_init(rcdu, enc_type, output, encoder, connector);
448         of_node_put(encoder);
449         of_node_put(connector);
450
451         if (ret && ret != -EPROBE_DEFER)
452                 dev_warn(rcdu->dev,
453                          "failed to initialize encoder %s (%d), skipping\n",
454                          encoder->full_name, ret);
455
456         return ret;
457 }
458
459 static int rcar_du_encoders_init(struct rcar_du_device *rcdu)
460 {
461         struct device_node *np = rcdu->dev->of_node;
462         struct device_node *ep_node;
463         unsigned int num_encoders = 0;
464
465         /*
466          * Iterate over the endpoints and create one encoder for each output
467          * pipeline.
468          */
469         for_each_endpoint_of_node(np, ep_node) {
470                 enum rcar_du_output output;
471                 struct of_endpoint ep;
472                 unsigned int i;
473                 int ret;
474
475                 ret = of_graph_parse_endpoint(ep_node, &ep);
476                 if (ret < 0) {
477                         of_node_put(ep_node);
478                         return ret;
479                 }
480
481                 /* Find the output route corresponding to the port number. */
482                 for (i = 0; i < RCAR_DU_OUTPUT_MAX; ++i) {
483                         if (rcdu->info->routes[i].possible_crtcs &&
484                             rcdu->info->routes[i].port == ep.port) {
485                                 output = i;
486                                 break;
487                         }
488                 }
489
490                 if (i == RCAR_DU_OUTPUT_MAX) {
491                         dev_warn(rcdu->dev,
492                                  "port %u references unexisting output, skipping\n",
493                                  ep.port);
494                         continue;
495                 }
496
497                 /* Process the output pipeline. */
498                 ret = rcar_du_encoders_init_one(rcdu, output, &ep);
499                 if (ret < 0) {
500                         if (ret == -EPROBE_DEFER) {
501                                 of_node_put(ep_node);
502                                 return ret;
503                         }
504
505                         continue;
506                 }
507
508                 num_encoders++;
509         }
510
511         return num_encoders;
512 }
513
514 static int rcar_du_properties_init(struct rcar_du_device *rcdu)
515 {
516         rcdu->props.alpha =
517                 drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
518         if (rcdu->props.alpha == NULL)
519                 return -ENOMEM;
520
521         /* The color key is expressed as an RGB888 triplet stored in a 32-bit
522          * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
523          * or enable source color keying (1).
524          */
525         rcdu->props.colorkey =
526                 drm_property_create_range(rcdu->ddev, 0, "colorkey",
527                                           0, 0x01ffffff);
528         if (rcdu->props.colorkey == NULL)
529                 return -ENOMEM;
530
531         return 0;
532 }
533
534 int rcar_du_modeset_init(struct rcar_du_device *rcdu)
535 {
536         static const unsigned int mmio_offsets[] = {
537                 DU0_REG_OFFSET, DU2_REG_OFFSET
538         };
539
540         struct drm_device *dev = rcdu->ddev;
541         struct drm_encoder *encoder;
542         struct drm_fbdev_cma *fbdev;
543         unsigned int num_encoders;
544         unsigned int num_groups;
545         unsigned int i;
546         int ret;
547
548         drm_mode_config_init(dev);
549
550         dev->mode_config.min_width = 0;
551         dev->mode_config.min_height = 0;
552         dev->mode_config.max_width = 4095;
553         dev->mode_config.max_height = 2047;
554         dev->mode_config.funcs = &rcar_du_mode_config_funcs;
555
556         rcdu->num_crtcs = rcdu->info->num_crtcs;
557
558         ret = rcar_du_properties_init(rcdu);
559         if (ret < 0)
560                 return ret;
561
562         /* Initialize the groups. */
563         num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
564
565         for (i = 0; i < num_groups; ++i) {
566                 struct rcar_du_group *rgrp = &rcdu->groups[i];
567
568                 mutex_init(&rgrp->lock);
569
570                 rgrp->dev = rcdu;
571                 rgrp->mmio_offset = mmio_offsets[i];
572                 rgrp->index = i;
573                 rgrp->num_crtcs = min(rcdu->num_crtcs - 2 * i, 2U);
574
575                 /* If we have more than one CRTCs in this group pre-associate
576                  * the low-order planes with CRTC 0 and the high-order planes
577                  * with CRTC 1 to minimize flicker occurring when the
578                  * association is changed.
579                  */
580                 rgrp->dptsr_planes = rgrp->num_crtcs > 1
581                                    ? (rcdu->info->gen >= 3 ? 0x04 : 0xf0)
582                                    : 0;
583
584                 if (!rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
585                         ret = rcar_du_planes_init(rgrp);
586                         if (ret < 0)
587                                 return ret;
588                 }
589         }
590
591         /* Initialize the compositors. */
592         if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
593                 for (i = 0; i < rcdu->num_crtcs; ++i) {
594                         struct rcar_du_vsp *vsp = &rcdu->vsps[i];
595
596                         vsp->index = i;
597                         vsp->dev = rcdu;
598                         rcdu->crtcs[i].vsp = vsp;
599
600                         ret = rcar_du_vsp_init(vsp);
601                         if (ret < 0)
602                                 return ret;
603                 }
604         }
605
606         /* Create the CRTCs. */
607         for (i = 0; i < rcdu->num_crtcs; ++i) {
608                 struct rcar_du_group *rgrp = &rcdu->groups[i / 2];
609
610                 ret = rcar_du_crtc_create(rgrp, i);
611                 if (ret < 0)
612                         return ret;
613         }
614
615         /* Initialize the encoders. */
616         ret = rcar_du_lvdsenc_init(rcdu);
617         if (ret < 0)
618                 return ret;
619
620         ret = rcar_du_encoders_init(rcdu);
621         if (ret < 0)
622                 return ret;
623
624         if (ret == 0) {
625                 dev_err(rcdu->dev, "error: no encoder could be initialized\n");
626                 return -EINVAL;
627         }
628
629         num_encoders = ret;
630
631         /* Set the possible CRTCs and possible clones. There's always at least
632          * one way for all encoders to clone each other, set all bits in the
633          * possible clones field.
634          */
635         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
636                 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
637                 const struct rcar_du_output_routing *route =
638                         &rcdu->info->routes[renc->output];
639
640                 encoder->possible_crtcs = route->possible_crtcs;
641                 encoder->possible_clones = (1 << num_encoders) - 1;
642         }
643
644         drm_mode_config_reset(dev);
645
646         drm_kms_helper_poll_init(dev);
647
648         if (dev->mode_config.num_connector) {
649                 fbdev = drm_fbdev_cma_init(dev, 32, dev->mode_config.num_crtc,
650                                            dev->mode_config.num_connector);
651                 if (IS_ERR(fbdev))
652                         return PTR_ERR(fbdev);
653
654                 rcdu->fbdev = fbdev;
655         } else {
656                 dev_info(rcdu->dev,
657                          "no connector found, disabling fbdev emulation\n");
658         }
659
660         return 0;
661 }