Merge tag 'clk-for-linus-3.20' of git://git.linaro.org/people/mike.turquette/linux
[firefly-linux-kernel-4.4.55.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_internal(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_one(struct rcar_du_device *rcdu,
194                                      enum rcar_du_output output,
195                                      struct of_endpoint *ep)
196 {
197         static const struct {
198                 const char *compatible;
199                 enum rcar_du_encoder_type type;
200         } encoders[] = {
201                 { "adi,adv7123", RCAR_DU_ENCODER_VGA },
202                 { "adi,adv7511w", RCAR_DU_ENCODER_HDMI },
203                 { "thine,thc63lvdm83d", RCAR_DU_ENCODER_LVDS },
204         };
205
206         enum rcar_du_encoder_type enc_type = RCAR_DU_ENCODER_NONE;
207         struct device_node *connector = NULL;
208         struct device_node *encoder = NULL;
209         struct device_node *prev = NULL;
210         struct device_node *entity_ep_node;
211         struct device_node *entity;
212         int ret;
213
214         /*
215          * Locate the connected entity and infer its type from the number of
216          * endpoints.
217          */
218         entity = of_graph_get_remote_port_parent(ep->local_node);
219         if (!entity) {
220                 dev_dbg(rcdu->dev, "unconnected endpoint %s, skipping\n",
221                         ep->local_node->full_name);
222                 return 0;
223         }
224
225         entity_ep_node = of_parse_phandle(ep->local_node, "remote-endpoint", 0);
226
227         while (1) {
228                 struct device_node *ep_node;
229
230                 ep_node = of_graph_get_next_endpoint(entity, prev);
231                 of_node_put(prev);
232                 prev = ep_node;
233
234                 if (!ep_node)
235                         break;
236
237                 if (ep_node == entity_ep_node)
238                         continue;
239
240                 /*
241                  * We've found one endpoint other than the input, this must
242                  * be an encoder. Locate the connector.
243                  */
244                 encoder = entity;
245                 connector = of_graph_get_remote_port_parent(ep_node);
246                 of_node_put(ep_node);
247
248                 if (!connector) {
249                         dev_warn(rcdu->dev,
250                                  "no connector for encoder %s, skipping\n",
251                                  encoder->full_name);
252                         of_node_put(entity_ep_node);
253                         of_node_put(encoder);
254                         return 0;
255                 }
256
257                 break;
258         }
259
260         of_node_put(entity_ep_node);
261
262         if (encoder) {
263                 /*
264                  * If an encoder has been found, get its type based on its
265                  * compatible string.
266                  */
267                 unsigned int i;
268
269                 for (i = 0; i < ARRAY_SIZE(encoders); ++i) {
270                         if (of_device_is_compatible(encoder,
271                                                     encoders[i].compatible)) {
272                                 enc_type = encoders[i].type;
273                                 break;
274                         }
275                 }
276
277                 if (i == ARRAY_SIZE(encoders)) {
278                         dev_warn(rcdu->dev,
279                                  "unknown encoder type for %s, skipping\n",
280                                  encoder->full_name);
281                         of_node_put(encoder);
282                         of_node_put(connector);
283                         return 0;
284                 }
285         } else {
286                 /*
287                  * If no encoder has been found the entity must be the
288                  * connector.
289                  */
290                 connector = entity;
291         }
292
293         ret = rcar_du_encoder_init(rcdu, enc_type, output, encoder, connector);
294         of_node_put(encoder);
295         of_node_put(connector);
296
297         return ret < 0 ? ret : 1;
298 }
299
300 static int rcar_du_encoders_init(struct rcar_du_device *rcdu)
301 {
302         struct device_node *np = rcdu->dev->of_node;
303         struct device_node *prev = NULL;
304         unsigned int num_encoders = 0;
305
306         /*
307          * Iterate over the endpoints and create one encoder for each output
308          * pipeline.
309          */
310         while (1) {
311                 struct device_node *ep_node;
312                 enum rcar_du_output output;
313                 struct of_endpoint ep;
314                 unsigned int i;
315                 int ret;
316
317                 ep_node = of_graph_get_next_endpoint(np, prev);
318                 of_node_put(prev);
319                 prev = ep_node;
320
321                 if (ep_node == NULL)
322                         break;
323
324                 ret = of_graph_parse_endpoint(ep_node, &ep);
325                 if (ret < 0) {
326                         of_node_put(ep_node);
327                         return ret;
328                 }
329
330                 /* Find the output route corresponding to the port number. */
331                 for (i = 0; i < RCAR_DU_OUTPUT_MAX; ++i) {
332                         if (rcdu->info->routes[i].possible_crtcs &&
333                             rcdu->info->routes[i].port == ep.port) {
334                                 output = i;
335                                 break;
336                         }
337                 }
338
339                 if (i == RCAR_DU_OUTPUT_MAX) {
340                         dev_warn(rcdu->dev,
341                                  "port %u references unexisting output, skipping\n",
342                                  ep.port);
343                         continue;
344                 }
345
346                 /* Process the output pipeline. */
347                 ret = rcar_du_encoders_init_one(rcdu, output, &ep);
348                 if (ret < 0) {
349                         if (ret == -EPROBE_DEFER) {
350                                 of_node_put(ep_node);
351                                 return ret;
352                         }
353
354                         dev_info(rcdu->dev,
355                                  "encoder initialization failed, skipping\n");
356                         continue;
357                 }
358
359                 num_encoders += ret;
360         }
361
362         return num_encoders;
363 }
364
365 int rcar_du_modeset_init(struct rcar_du_device *rcdu)
366 {
367         static const unsigned int mmio_offsets[] = {
368                 DU0_REG_OFFSET, DU2_REG_OFFSET
369         };
370
371         struct drm_device *dev = rcdu->ddev;
372         struct drm_encoder *encoder;
373         struct drm_fbdev_cma *fbdev;
374         unsigned int num_encoders;
375         unsigned int num_groups;
376         unsigned int i;
377         int ret;
378
379         drm_mode_config_init(dev);
380
381         dev->mode_config.min_width = 0;
382         dev->mode_config.min_height = 0;
383         dev->mode_config.max_width = 4095;
384         dev->mode_config.max_height = 2047;
385         dev->mode_config.funcs = &rcar_du_mode_config_funcs;
386
387         rcdu->num_crtcs = rcdu->info->num_crtcs;
388
389         /* Initialize the groups. */
390         num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
391
392         for (i = 0; i < num_groups; ++i) {
393                 struct rcar_du_group *rgrp = &rcdu->groups[i];
394
395                 rgrp->dev = rcdu;
396                 rgrp->mmio_offset = mmio_offsets[i];
397                 rgrp->index = i;
398
399                 ret = rcar_du_planes_init(rgrp);
400                 if (ret < 0)
401                         return ret;
402         }
403
404         /* Create the CRTCs. */
405         for (i = 0; i < rcdu->num_crtcs; ++i) {
406                 struct rcar_du_group *rgrp = &rcdu->groups[i / 2];
407
408                 ret = rcar_du_crtc_create(rgrp, i);
409                 if (ret < 0)
410                         return ret;
411         }
412
413         /* Initialize the encoders. */
414         ret = rcar_du_lvdsenc_init(rcdu);
415         if (ret < 0)
416                 return ret;
417
418         ret = rcar_du_encoders_init(rcdu);
419         if (ret < 0)
420                 return ret;
421
422         if (ret == 0) {
423                 dev_err(rcdu->dev, "error: no encoder could be initialized\n");
424                 return -EINVAL;
425         }
426
427         num_encoders = ret;
428
429         /* Set the possible CRTCs and possible clones. There's always at least
430          * one way for all encoders to clone each other, set all bits in the
431          * possible clones field.
432          */
433         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
434                 struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
435                 const struct rcar_du_output_routing *route =
436                         &rcdu->info->routes[renc->output];
437
438                 encoder->possible_crtcs = route->possible_crtcs;
439                 encoder->possible_clones = (1 << num_encoders) - 1;
440         }
441
442         /* Now that the CRTCs have been initialized register the planes. */
443         for (i = 0; i < num_groups; ++i) {
444                 ret = rcar_du_planes_register(&rcdu->groups[i]);
445                 if (ret < 0)
446                         return ret;
447         }
448
449         drm_kms_helper_poll_init(dev);
450
451         drm_helper_disable_unused_functions(dev);
452
453         fbdev = drm_fbdev_cma_init(dev, 32, dev->mode_config.num_crtc,
454                                    dev->mode_config.num_connector);
455         if (IS_ERR(fbdev))
456                 return PTR_ERR(fbdev);
457
458 #ifndef CONFIG_FRAMEBUFFER_CONSOLE
459         drm_fbdev_cma_restore_mode(fbdev);
460 #endif
461
462         rcdu->fbdev = fbdev;
463
464         return 0;
465 }