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