drm/rcar: gem: dumb: pitch is an output
[platform/adaptation/renesas_rcar/renesas_kernel.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_crtc.h>
16 #include <drm/drm_crtc_helper.h>
17 #include <drm/drm_fb_cma_helper.h>
18 #include <drm/drm_gem_cma_helper.h>
19
20 #include <linux/of_graph.h>
21
22 #include "rcar_du_crtc.h"
23 #include "rcar_du_drv.h"
24 #include "rcar_du_encoder.h"
25 #include "rcar_du_kms.h"
26 #include "rcar_du_lvdsenc.h"
27 #include "rcar_du_regs.h"
28
29 /* -----------------------------------------------------------------------------
30  * Format helpers
31  */
32
33 static const struct rcar_du_format_info rcar_du_format_infos[] = {
34         {
35                 .fourcc = DRM_FORMAT_RGB565,
36                 .bpp = 16,
37                 .planes = 1,
38                 .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
39                 .edf = PnDDCR4_EDF_NONE,
40         }, {
41                 .fourcc = DRM_FORMAT_ARGB1555,
42                 .bpp = 16,
43                 .planes = 1,
44                 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
45                 .edf = PnDDCR4_EDF_NONE,
46         }, {
47                 .fourcc = DRM_FORMAT_XRGB1555,
48                 .bpp = 16,
49                 .planes = 1,
50                 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
51                 .edf = PnDDCR4_EDF_NONE,
52         }, {
53                 .fourcc = DRM_FORMAT_XRGB8888,
54                 .bpp = 32,
55                 .planes = 1,
56                 .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
57                 .edf = PnDDCR4_EDF_RGB888,
58         }, {
59                 .fourcc = DRM_FORMAT_ARGB8888,
60                 .bpp = 32,
61                 .planes = 1,
62                 .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_16BPP,
63                 .edf = PnDDCR4_EDF_ARGB8888,
64         }, {
65                 .fourcc = DRM_FORMAT_UYVY,
66                 .bpp = 16,
67                 .planes = 1,
68                 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
69                 .edf = PnDDCR4_EDF_NONE,
70         }, {
71                 .fourcc = DRM_FORMAT_YUYV,
72                 .bpp = 16,
73                 .planes = 1,
74                 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
75                 .edf = PnDDCR4_EDF_NONE,
76         }, {
77                 .fourcc = DRM_FORMAT_NV12,
78                 .bpp = 12,
79                 .planes = 2,
80                 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
81                 .edf = PnDDCR4_EDF_NONE,
82         }, {
83                 .fourcc = DRM_FORMAT_NV21,
84                 .bpp = 12,
85                 .planes = 2,
86                 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
87                 .edf = PnDDCR4_EDF_NONE,
88         }, {
89                 /* In YUV 4:2:2, only NV16 is supported (NV61 isn't) */
90                 .fourcc = DRM_FORMAT_NV16,
91                 .bpp = 16,
92                 .planes = 2,
93                 .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
94                 .edf = PnDDCR4_EDF_NONE,
95         },
96 };
97
98 const struct rcar_du_format_info *rcar_du_format_info(u32 fourcc)
99 {
100         unsigned int i;
101
102         for (i = 0; i < ARRAY_SIZE(rcar_du_format_infos); ++i) {
103                 if (rcar_du_format_infos[i].fourcc == fourcc)
104                         return &rcar_du_format_infos[i];
105         }
106
107         return NULL;
108 }
109
110 /* -----------------------------------------------------------------------------
111  * Frame buffer
112  */
113
114 int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev,
115                         struct drm_mode_create_dumb *args)
116 {
117         struct rcar_du_device *rcdu = dev->dev_private;
118         unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
119         unsigned int align;
120
121         /* The R8A7779 DU requires a 16 pixels pitch alignment as documented,
122          * but the R8A7790 DU seems to require a 128 bytes pitch alignment.
123          */
124         if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
125                 align = 128;
126         else
127                 align = 16 * args->bpp / 8;
128
129         args->pitch = roundup(min_pitch, align);
130
131         return drm_gem_cma_dumb_create(file, dev, args);
132 }
133
134 static struct drm_framebuffer *
135 rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
136                   struct drm_mode_fb_cmd2 *mode_cmd)
137 {
138         struct rcar_du_device *rcdu = dev->dev_private;
139         const struct rcar_du_format_info *format;
140         unsigned int max_pitch;
141         unsigned int align;
142         unsigned int bpp;
143
144         format = rcar_du_format_info(mode_cmd->pixel_format);
145         if (format == NULL) {
146                 dev_dbg(dev->dev, "unsupported pixel format %08x\n",
147                         mode_cmd->pixel_format);
148                 return ERR_PTR(-EINVAL);
149         }
150
151         /*
152          * The pitch and alignment constraints are expressed in pixels on the
153          * hardware side and in bytes in the DRM API.
154          */
155         bpp = format->planes == 2 ? 1 : format->bpp / 8;
156         max_pitch =  4096 * bpp;
157
158         if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
159                 align = 128;
160         else
161                 align = 16 * bpp;
162
163         if (mode_cmd->pitches[0] & (align - 1) ||
164             mode_cmd->pitches[0] >= max_pitch) {
165                 dev_dbg(dev->dev, "invalid pitch value %u\n",
166                         mode_cmd->pitches[0]);
167                 return ERR_PTR(-EINVAL);
168         }
169
170         if (format->planes == 2) {
171                 if (mode_cmd->pitches[1] != mode_cmd->pitches[0]) {
172                         dev_dbg(dev->dev,
173                                 "luma and chroma pitches do not match\n");
174                         return ERR_PTR(-EINVAL);
175                 }
176         }
177
178         return drm_fb_cma_create(dev, file_priv, mode_cmd);
179 }
180
181 static void rcar_du_output_poll_changed(struct drm_device *dev)
182 {
183         struct rcar_du_device *rcdu = dev->dev_private;
184
185         drm_fbdev_cma_hotplug_event(rcdu->fbdev);
186 }
187
188 static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
189         .fb_create = rcar_du_fb_create,
190         .output_poll_changed = rcar_du_output_poll_changed,
191 };
192
193 static int rcar_du_encoders_init_pdata(struct rcar_du_device *rcdu)
194 {
195         unsigned int num_encoders = 0;
196         unsigned int i;
197         int ret;
198
199         for (i = 0; i < rcdu->pdata->num_encoders; ++i) {
200                 const struct rcar_du_encoder_data *pdata =
201                         &rcdu->pdata->encoders[i];
202                 const struct rcar_du_output_routing *route =
203                         &rcdu->info->routes[pdata->output];
204
205                 if (pdata->type == RCAR_DU_ENCODER_UNUSED)
206                         continue;
207
208                 if (pdata->output >= RCAR_DU_OUTPUT_MAX ||
209                     route->possible_crtcs == 0) {
210                         dev_warn(rcdu->dev,
211                                  "encoder %u references unexisting output %u, skipping\n",
212                                  i, pdata->output);
213                         continue;
214                 }
215
216                 ret = rcar_du_encoder_init(rcdu, pdata->type, pdata->output,
217                                            pdata, NULL);
218                 if (ret < 0)
219                         return ret;
220
221                 num_encoders++;
222         }
223
224         return num_encoders;
225 }
226
227 static int rcar_du_encoders_init_dt_one(struct rcar_du_device *rcdu,
228                                         enum rcar_du_output output,
229                                         struct of_endpoint *ep)
230 {
231         static const struct {
232                 const char *compatible;
233                 enum rcar_du_encoder_type type;
234         } encoders[] = {
235                 { "adi,adv7123", RCAR_DU_ENCODER_VGA },
236                 { "thine,thc63lvdm83d", RCAR_DU_ENCODER_LVDS },
237         };
238
239         enum rcar_du_encoder_type enc_type = RCAR_DU_ENCODER_NONE;
240         struct device_node *connector = NULL;
241         struct device_node *encoder = NULL;
242         struct device_node *prev = NULL;
243         struct device_node *entity_ep_node;
244         struct device_node *entity;
245         int ret;
246
247         /*
248          * Locate the connected entity and infer its type from the number of
249          * endpoints.
250          */
251         entity = of_graph_get_remote_port_parent(ep->local_node);
252         if (!entity) {
253                 dev_dbg(rcdu->dev, "unconnected endpoint %s, skipping\n",
254                         ep->local_node->full_name);
255                 return 0;
256         }
257
258         entity_ep_node = of_parse_phandle(ep->local_node, "remote-endpoint", 0);
259
260         while (1) {
261                 struct device_node *ep_node;
262
263                 ep_node = of_graph_get_next_endpoint(entity, prev);
264                 of_node_put(prev);
265                 prev = ep_node;
266
267                 if (!ep_node)
268                         break;
269
270                 if (ep_node == entity_ep_node)
271                         continue;
272
273                 /*
274                  * We've found one endpoint other than the input, this must
275                  * be an encoder. Locate the connector.
276                  */
277                 encoder = entity;
278                 connector = of_graph_get_remote_port_parent(ep_node);
279                 of_node_put(ep_node);
280
281                 if (!connector) {
282                         dev_warn(rcdu->dev,
283                                  "no connector for encoder %s, skipping\n",
284                                  encoder->full_name);
285                         of_node_put(entity_ep_node);
286                         of_node_put(encoder);
287                         return 0;
288                 }
289
290                 break;
291         }
292
293         of_node_put(entity_ep_node);
294
295         if (encoder) {
296                 /*
297                  * If an encoder has been found, get its type based on its
298                  * compatible string.
299                  */
300                 unsigned int i;
301
302                 for (i = 0; i < ARRAY_SIZE(encoders); ++i) {
303                         if (of_device_is_compatible(encoder,
304                                                     encoders[i].compatible)) {
305                                 enc_type = encoders[i].type;
306                                 break;
307                         }
308                 }
309
310                 if (i == ARRAY_SIZE(encoders)) {
311                         dev_warn(rcdu->dev,
312                                  "unknown encoder type for %s, skipping\n",
313                                  encoder->full_name);
314                         of_node_put(encoder);
315                         of_node_put(connector);
316                         return 0;
317                 }
318         } else {
319                 /*
320                  * If no encoder has been found the entity must be the
321                  * connector.
322                  */
323                 connector = entity;
324         }
325
326         ret = rcar_du_encoder_init(rcdu, enc_type, output, NULL, connector);
327         of_node_put(encoder);
328         of_node_put(connector);
329
330         return ret < 0 ? ret : 1;
331 }
332
333 static int rcar_du_encoders_init_dt(struct rcar_du_device *rcdu)
334 {
335         struct device_node *np = rcdu->dev->of_node;
336         struct device_node *prev = NULL;
337         unsigned int num_encoders = 0;
338
339         /*
340          * Iterate over the endpoints and create one encoder for each output
341          * pipeline.
342          */
343         while (1) {
344                 struct device_node *ep_node;
345                 enum rcar_du_output output;
346                 struct of_endpoint ep;
347                 unsigned int i;
348                 int ret;
349
350                 ep_node = of_graph_get_next_endpoint(np, prev);
351                 of_node_put(prev);
352                 prev = ep_node;
353
354                 if (ep_node == NULL)
355                         break;
356
357                 ret = of_graph_parse_endpoint(ep_node, &ep);
358                 if (ret < 0) {
359                         of_node_put(ep_node);
360                         return ret;
361                 }
362
363                 /* Find the output route corresponding to the port number. */
364                 for (i = 0; i < RCAR_DU_OUTPUT_MAX; ++i) {
365                         if (rcdu->info->routes[i].possible_crtcs &&
366                             rcdu->info->routes[i].port == ep.port) {
367                                 output = i;
368                                 break;
369                         }
370                 }
371
372                 if (i == RCAR_DU_OUTPUT_MAX) {
373                         dev_warn(rcdu->dev,
374                                  "port %u references unexisting output, skipping\n",
375                                  ep.port);
376                         continue;
377                 }
378
379                 /* Process the output pipeline. */
380                 ret = rcar_du_encoders_init_dt_one(rcdu, output, &ep);
381                 if (ret < 0) {
382                         of_node_put(ep_node);
383                         return ret;
384                 }
385
386                 num_encoders += ret;
387         }
388
389         return num_encoders;
390 }
391
392 int rcar_du_modeset_init(struct rcar_du_device *rcdu)
393 {
394         static const unsigned int mmio_offsets[] = {
395                 DU0_REG_OFFSET, DU2_REG_OFFSET
396         };
397
398         struct drm_device *dev = rcdu->ddev;
399         struct drm_encoder *encoder;
400         struct drm_fbdev_cma *fbdev;
401         unsigned int num_encoders;
402         unsigned int num_groups;
403         unsigned int i;
404         int ret;
405
406         drm_mode_config_init(dev);
407
408         dev->mode_config.min_width = 0;
409         dev->mode_config.min_height = 0;
410         dev->mode_config.max_width = 4095;
411         dev->mode_config.max_height = 2047;
412         dev->mode_config.funcs = &rcar_du_mode_config_funcs;
413
414         rcdu->num_crtcs = rcdu->info->num_crtcs;
415
416         /* Initialize the groups. */
417         num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
418
419         for (i = 0; i < num_groups; ++i) {
420                 struct rcar_du_group *rgrp = &rcdu->groups[i];
421
422                 rgrp->dev = rcdu;
423                 rgrp->mmio_offset = mmio_offsets[i];
424                 rgrp->index = i;
425
426                 ret = rcar_du_planes_init(rgrp);
427                 if (ret < 0)
428                         return ret;
429         }
430
431         /* Create the CRTCs. */
432         for (i = 0; i < rcdu->num_crtcs; ++i) {
433                 struct rcar_du_group *rgrp = &rcdu->groups[i / 2];
434
435                 ret = rcar_du_crtc_create(rgrp, i);
436                 if (ret < 0)
437                         return ret;
438         }
439
440         /* Initialize the encoders. */
441         ret = rcar_du_lvdsenc_init(rcdu);
442         if (ret < 0)
443                 return ret;
444
445         if (rcdu->pdata)
446                 ret = rcar_du_encoders_init_pdata(rcdu);
447         else
448                 ret = rcar_du_encoders_init_dt(rcdu);
449
450         if (ret < 0)
451                 return ret;
452
453         num_encoders = ret;
454
455         /* Set the possible CRTCs and possible clones. There's always at least
456          * one way for all encoders to clone each other, set all bits in the
457          * possible clones field.
458          */
459         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
460                 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
461                 const struct rcar_du_output_routing *route =
462                         &rcdu->info->routes[renc->output];
463
464                 encoder->possible_crtcs = route->possible_crtcs;
465                 encoder->possible_clones = (1 << num_encoders) - 1;
466         }
467
468         /* Now that the CRTCs have been initialized register the planes. */
469         for (i = 0; i < num_groups; ++i) {
470                 ret = rcar_du_planes_register(&rcdu->groups[i]);
471                 if (ret < 0)
472                         return ret;
473         }
474
475         drm_kms_helper_poll_init(dev);
476
477         drm_helper_disable_unused_functions(dev);
478
479         fbdev = drm_fbdev_cma_init(dev, 32, dev->mode_config.num_crtc,
480                                    dev->mode_config.num_connector);
481         if (IS_ERR(fbdev))
482                 return PTR_ERR(fbdev);
483
484 #ifndef CONFIG_FRAMEBUFFER_CONSOLE
485         drm_fbdev_cma_restore_mode(fbdev);
486 #endif
487
488         rcdu->fbdev = fbdev;
489
490         return 0;
491 }