drm: rcar-du: Add procedure of memory width update
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / gpu / drm / rcar-du / rcar_du_plane.c
1 /*
2  * rcar_du_plane.c  --  R-Car Display Unit Planes
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 "rcar_du_drv.h"
21 #include "rcar_du_kms.h"
22 #include "rcar_du_plane.h"
23 #include "rcar_du_regs.h"
24
25 #define RCAR_DU_COLORKEY_NONE           (0 << 24)
26 #define RCAR_DU_COLORKEY_SOURCE         (1 << 24)
27 #define RCAR_DU_COLORKEY_MASK           (1 << 24)
28
29 struct rcar_du_kms_plane {
30         struct drm_plane plane;
31         struct rcar_du_plane *hwplane;
32 };
33
34 static inline struct rcar_du_plane *to_rcar_plane(struct drm_plane *plane)
35 {
36         return container_of(plane, struct rcar_du_kms_plane, plane)->hwplane;
37 }
38
39 static u32 rcar_du_plane_read(struct rcar_du_group *rgrp,
40                               unsigned int index, u32 reg)
41 {
42         return rcar_du_read(rgrp->dev,
43                             rgrp->mmio_offset + index * PLANE_OFF + reg);
44 }
45
46 static void rcar_du_plane_write(struct rcar_du_group *rgrp,
47                                 unsigned int index, u32 reg, u32 data)
48 {
49         rcar_du_write(rgrp->dev, rgrp->mmio_offset + index * PLANE_OFF + reg,
50                       data);
51 }
52
53 int rcar_du_plane_reserve(struct rcar_du_plane *plane,
54                           const struct rcar_du_format_info *format)
55 {
56         struct rcar_du_group *rgrp = plane->group;
57         unsigned int i;
58         int ret = -EBUSY;
59
60         mutex_lock(&rgrp->planes.lock);
61
62         for (i = 0; i < ARRAY_SIZE(rgrp->planes.planes); ++i) {
63                 if (!(rgrp->planes.free & (1 << i)))
64                         continue;
65
66                 if (format->planes == 1 ||
67                     rgrp->planes.free & (1 << ((i + 1) % 8)))
68                         break;
69         }
70
71         if (i == ARRAY_SIZE(rgrp->planes.planes))
72                 goto done;
73
74         rgrp->planes.free &= ~(1 << i);
75         if (format->planes == 2)
76                 rgrp->planes.free &= ~(1 << ((i + 1) % 8));
77
78         plane->hwindex = i;
79
80         ret = 0;
81
82 done:
83         mutex_unlock(&rgrp->planes.lock);
84         return ret;
85 }
86
87 void rcar_du_plane_release(struct rcar_du_plane *plane)
88 {
89         struct rcar_du_group *rgrp = plane->group;
90
91         if (plane->hwindex == -1)
92                 return;
93
94         mutex_lock(&rgrp->planes.lock);
95         rgrp->planes.free |= 1 << plane->hwindex;
96         if (plane->format->planes == 2)
97                 rgrp->planes.free |= 1 << ((plane->hwindex + 1) % 8);
98         mutex_unlock(&rgrp->planes.lock);
99
100         plane->hwindex = -1;
101 }
102
103 void rcar_du_plane_update_base(struct rcar_du_plane *plane)
104 {
105         struct rcar_du_group *rgrp = plane->group;
106         unsigned int index = plane->hwindex;
107         u32 mwr;
108
109         /* Memory pitch (expressed in pixels) */
110         if (plane->format->planes == 2)
111                 mwr = plane->pitch;
112         else
113                 mwr = plane->pitch * 8 / plane->format->bpp;
114
115         if ((plane->interlace_flag) && (plane->format->bpp == 32))
116                 rcar_du_plane_write(rgrp, index, PnMWR, mwr * 2);
117         else
118                 rcar_du_plane_write(rgrp, index, PnMWR, mwr);
119
120         /* The Y position is expressed in raster line units and must be doubled
121          * for 32bpp formats, according to the R8A7790 datasheet. No mention of
122          * doubling the Y position is found in the R8A7779 datasheet, but the
123          * rule seems to apply there as well.
124          *
125          * Similarly, for the second plane, NV12 and NV21 formats seem to
126          * require a halved Y position value.
127          */
128         rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x);
129         if ((!plane->interlace_flag) && (plane->format->bpp == 32))
130                 rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y * 2);
131         else
132                 rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y);
133         rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[0]);
134
135         if (plane->format->planes == 2) {
136                 index = (index + 1) % 8;
137
138                 rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x);
139                 rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y *
140                                     (plane->format->bpp == 16 ? 2 : 1) / 2);
141                 rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[1]);
142         }
143 }
144
145 void rcar_du_plane_compute_base(struct rcar_du_plane *plane,
146                                 struct drm_framebuffer *fb)
147 {
148         struct drm_gem_cma_object *gem;
149
150         plane->pitch = fb->pitches[0];
151
152         gem = drm_fb_cma_get_gem_obj(fb, 0);
153         plane->dma[0] = gem->paddr + fb->offsets[0];
154
155         if (plane->format->planes == 2) {
156                 gem = drm_fb_cma_get_gem_obj(fb, 1);
157                 plane->dma[1] = gem->paddr + fb->offsets[1];
158         }
159 }
160
161 static void rcar_du_plane_setup_mode(struct rcar_du_plane *plane,
162                                      unsigned int index)
163 {
164         struct rcar_du_group *rgrp = plane->group;
165         u32 colorkey;
166         u32 pnmr;
167
168         /* The PnALPHAR register controls alpha-blending in 16bpp formats
169          * (ARGB1555 and XRGB1555).
170          *
171          * For ARGB, set the alpha value to 0, and enable alpha-blending when
172          * the A bit is 0. This maps A=0 to alpha=0 and A=1 to alpha=255.
173          *
174          * For XRGB, set the alpha value to the plane-wide alpha value and
175          * enable alpha-blending regardless of the X bit value.
176          */
177         if (plane->format->fourcc != DRM_FORMAT_XRGB1555)
178                 rcar_du_plane_write(rgrp, index, PnALPHAR, PnALPHAR_ABIT_0);
179         else
180                 rcar_du_plane_write(rgrp, index, PnALPHAR,
181                                     PnALPHAR_ABIT_X | plane->alpha);
182
183         pnmr = PnMR_BM_MD | plane->format->pnmr;
184
185         /* Disable color keying when requested. YUV formats have the
186          * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying
187          * automatically.
188          */
189         if ((plane->colorkey & RCAR_DU_COLORKEY_MASK) == RCAR_DU_COLORKEY_NONE)
190                 pnmr |= PnMR_SPIM_TP_OFF;
191
192         /* For packed YUV formats we need to select the U/V order. */
193         if (plane->format->fourcc == DRM_FORMAT_YUYV)
194                 pnmr |= PnMR_YCDF_YUYV;
195
196         rcar_du_plane_write(rgrp, index, PnMR, pnmr);
197
198         switch (plane->format->fourcc) {
199         case DRM_FORMAT_RGB565:
200                 colorkey = ((plane->colorkey & 0xf80000) >> 8)
201                          | ((plane->colorkey & 0x00fc00) >> 5)
202                          | ((plane->colorkey & 0x0000f8) >> 3);
203                 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
204                 break;
205
206         case DRM_FORMAT_ARGB1555:
207         case DRM_FORMAT_XRGB1555:
208                 colorkey = ((plane->colorkey & 0xf80000) >> 9)
209                          | ((plane->colorkey & 0x00f800) >> 6)
210                          | ((plane->colorkey & 0x0000f8) >> 3);
211                 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
212                 break;
213
214         case DRM_FORMAT_XRGB8888:
215         case DRM_FORMAT_ARGB8888:
216                 rcar_du_plane_write(rgrp, index, PnTC3R,
217                                     PnTC3R_CODE | (plane->colorkey & 0xffffff));
218                 break;
219         }
220 }
221
222 static void __rcar_du_plane_setup(struct rcar_du_plane *plane,
223                                   unsigned int index)
224 {
225         struct rcar_du_group *rgrp = plane->group;
226         u32 ddcr2 = PnDDCR2_CODE;
227         u32 ddcr4;
228         u32 mwr;
229
230         /* Data format
231          *
232          * The data format is selected by the DDDF field in PnMR and the EDF
233          * field in DDCR4.
234          */
235         ddcr4 = rcar_du_plane_read(rgrp, index, PnDDCR4);
236         ddcr4 &= ~PnDDCR4_EDF_MASK;
237         ddcr4 |= plane->format->edf | PnDDCR4_CODE;
238
239         rcar_du_plane_setup_mode(plane, index);
240
241         if (plane->format->planes == 2) {
242                 if (plane->hwindex != index) {
243                         if (plane->format->fourcc == DRM_FORMAT_NV12 ||
244                             plane->format->fourcc == DRM_FORMAT_NV21)
245                                 ddcr2 |= PnDDCR2_Y420;
246
247                         if (plane->format->fourcc == DRM_FORMAT_NV21)
248                                 ddcr2 |= PnDDCR2_NV21;
249
250                         ddcr2 |= PnDDCR2_DIVU;
251                 } else {
252                         ddcr2 |= PnDDCR2_DIVY;
253                 }
254         }
255
256         rcar_du_plane_write(rgrp, index, PnDDCR2, ddcr2);
257         rcar_du_plane_write(rgrp, index, PnDDCR4, ddcr4);
258
259         /* Memory pitch (expressed in pixels) */
260         if (plane->format->planes == 2)
261                 mwr = plane->pitch;
262         else
263                 mwr = plane->pitch * 8 / plane->format->bpp;
264
265         if ((plane->interlace_flag) && (plane->format->bpp == 32))
266                 rcar_du_plane_write(rgrp, index, PnMWR, mwr * 2);
267         else
268                 rcar_du_plane_write(rgrp, index, PnMWR, mwr);
269
270         /* Destination position and size */
271         rcar_du_plane_write(rgrp, index, PnDSXR, plane->width);
272         rcar_du_plane_write(rgrp, index, PnDSYR, plane->height);
273         rcar_du_plane_write(rgrp, index, PnDPXR, plane->dst_x);
274         rcar_du_plane_write(rgrp, index, PnDPYR, plane->dst_y);
275
276         /* Wrap-around and blinking, disabled */
277         rcar_du_plane_write(rgrp, index, PnWASPR, 0);
278         rcar_du_plane_write(rgrp, index, PnWAMWR, 4095);
279         rcar_du_plane_write(rgrp, index, PnBTR, 0);
280         rcar_du_plane_write(rgrp, index, PnMLR, 0);
281 }
282
283 void rcar_du_plane_setup(struct rcar_du_plane *plane)
284 {
285         __rcar_du_plane_setup(plane, plane->hwindex);
286         if (plane->format->planes == 2)
287                 __rcar_du_plane_setup(plane, (plane->hwindex + 1) % 8);
288
289         rcar_du_plane_update_base(plane);
290 }
291
292 static int
293 rcar_du_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
294                        struct drm_framebuffer *fb, int crtc_x, int crtc_y,
295                        unsigned int crtc_w, unsigned int crtc_h,
296                        uint32_t src_x, uint32_t src_y,
297                        uint32_t src_w, uint32_t src_h)
298 {
299         struct rcar_du_plane *rplane = to_rcar_plane(plane);
300         struct rcar_du_device *rcdu = rplane->group->dev;
301         const struct rcar_du_format_info *format;
302         unsigned int nplanes;
303         int ret;
304
305         format = rcar_du_format_info(fb->pixel_format);
306         if (format == NULL) {
307                 dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__,
308                         fb->pixel_format);
309                 return -EINVAL;
310         }
311
312         if (src_w >> 16 != crtc_w || src_h >> 16 != crtc_h) {
313                 dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__);
314                 return -EINVAL;
315         }
316
317         nplanes = rplane->format ? rplane->format->planes : 0;
318
319         /* Reallocate hardware planes if the number of required planes has
320          * changed.
321          */
322         if (format->planes != nplanes) {
323                 rcar_du_plane_release(rplane);
324                 ret = rcar_du_plane_reserve(rplane, format);
325                 if (ret < 0)
326                         return ret;
327         }
328
329         rplane->crtc = crtc;
330         rplane->format = format;
331
332         rplane->src_x = src_x >> 16;
333         rplane->src_y = src_y >> 16;
334         rplane->dst_x = crtc_x;
335         rplane->dst_y = crtc_y;
336         rplane->width = crtc_w;
337         rplane->height = crtc_h;
338
339         rcar_du_plane_compute_base(rplane, fb);
340
341         if (crtc->mode.flags & DRM_MODE_FLAG_INTERLACE)
342                 rplane->interlace_flag = true;
343         else
344                 rplane->interlace_flag = false;
345         rcar_du_plane_setup(rplane);
346
347         mutex_lock(&rplane->group->planes.lock);
348         rplane->enabled = true;
349         rcar_du_crtc_update_planes(rplane->crtc);
350         mutex_unlock(&rplane->group->planes.lock);
351
352         return 0;
353 }
354
355 static int rcar_du_plane_disable(struct drm_plane *plane)
356 {
357         struct rcar_du_plane *rplane = to_rcar_plane(plane);
358
359         if (!rplane->enabled)
360                 return 0;
361
362         mutex_lock(&rplane->group->planes.lock);
363         rplane->enabled = false;
364         rcar_du_crtc_update_planes(rplane->crtc);
365         mutex_unlock(&rplane->group->planes.lock);
366
367         rcar_du_plane_release(rplane);
368
369         rplane->crtc = NULL;
370         rplane->format = NULL;
371
372         return 0;
373 }
374
375 /* Both the .set_property and the .update_plane operations are called with the
376  * mode_config lock held. There is this no need to explicitly protect access to
377  * the alpha and colorkey fields and the mode register.
378  */
379 static void rcar_du_plane_set_alpha(struct rcar_du_plane *plane, u32 alpha)
380 {
381         if (plane->alpha == alpha)
382                 return;
383
384         plane->alpha = alpha;
385         if (!plane->enabled || plane->format->fourcc != DRM_FORMAT_XRGB1555)
386                 return;
387
388         rcar_du_plane_setup_mode(plane, plane->hwindex);
389 }
390
391 static void rcar_du_plane_set_colorkey(struct rcar_du_plane *plane,
392                                        u32 colorkey)
393 {
394         if (plane->colorkey == colorkey)
395                 return;
396
397         plane->colorkey = colorkey;
398         if (!plane->enabled)
399                 return;
400
401         rcar_du_plane_setup_mode(plane, plane->hwindex);
402 }
403
404 static void rcar_du_plane_set_zpos(struct rcar_du_plane *plane,
405                                    unsigned int zpos)
406 {
407         mutex_lock(&plane->group->planes.lock);
408         if (plane->zpos == zpos)
409                 goto done;
410
411         plane->zpos = zpos;
412         if (!plane->enabled)
413                 goto done;
414
415         rcar_du_crtc_update_planes(plane->crtc);
416
417 done:
418         mutex_unlock(&plane->group->planes.lock);
419 }
420
421 static int rcar_du_plane_set_property(struct drm_plane *plane,
422                                       struct drm_property *property,
423                                       uint64_t value)
424 {
425         struct rcar_du_plane *rplane = to_rcar_plane(plane);
426         struct rcar_du_group *rgrp = rplane->group;
427
428         if (property == rgrp->planes.alpha)
429                 rcar_du_plane_set_alpha(rplane, value);
430         else if (property == rgrp->planes.colorkey)
431                 rcar_du_plane_set_colorkey(rplane, value);
432         else if (property == rgrp->planes.zpos)
433                 rcar_du_plane_set_zpos(rplane, value);
434         else
435                 return -EINVAL;
436
437         return 0;
438 }
439
440 static const struct drm_plane_funcs rcar_du_plane_funcs = {
441         .update_plane = rcar_du_plane_update,
442         .disable_plane = rcar_du_plane_disable,
443         .set_property = rcar_du_plane_set_property,
444         .destroy = drm_plane_cleanup,
445 };
446
447 static const uint32_t formats[] = {
448         DRM_FORMAT_RGB565,
449         DRM_FORMAT_ARGB1555,
450         DRM_FORMAT_XRGB1555,
451         DRM_FORMAT_XRGB8888,
452         DRM_FORMAT_ARGB8888,
453         DRM_FORMAT_UYVY,
454         DRM_FORMAT_YUYV,
455         DRM_FORMAT_NV12,
456         DRM_FORMAT_NV21,
457         DRM_FORMAT_NV16,
458 };
459
460 int rcar_du_planes_init(struct rcar_du_group *rgrp)
461 {
462         struct rcar_du_planes *planes = &rgrp->planes;
463         struct rcar_du_device *rcdu = rgrp->dev;
464         unsigned int i;
465
466         mutex_init(&planes->lock);
467         planes->free = 0xff;
468
469         planes->alpha =
470                 drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
471         if (planes->alpha == NULL)
472                 return -ENOMEM;
473
474         /* The color key is expressed as an RGB888 triplet stored in a 32-bit
475          * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
476          * or enable source color keying (1).
477          */
478         planes->colorkey =
479                 drm_property_create_range(rcdu->ddev, 0, "colorkey",
480                                           0, 0x01ffffff);
481         if (planes->colorkey == NULL)
482                 return -ENOMEM;
483
484         planes->zpos =
485                 drm_property_create_range(rcdu->ddev, 0, "zpos", 1, 7);
486         if (planes->zpos == NULL)
487                 return -ENOMEM;
488
489         for (i = 0; i < ARRAY_SIZE(planes->planes); ++i) {
490                 struct rcar_du_plane *plane = &planes->planes[i];
491
492                 plane->group = rgrp;
493                 plane->hwindex = -1;
494                 plane->alpha = 255;
495                 plane->colorkey = RCAR_DU_COLORKEY_NONE;
496                 plane->zpos = 0;
497         }
498
499         return 0;
500 }
501
502 int rcar_du_planes_register(struct rcar_du_group *rgrp)
503 {
504         struct rcar_du_planes *planes = &rgrp->planes;
505         struct rcar_du_device *rcdu = rgrp->dev;
506         unsigned int crtcs;
507         unsigned int i;
508         int ret;
509
510         crtcs = ((1 << rcdu->num_crtcs) - 1) & (3 << (2 * rgrp->index));
511
512         for (i = 0; i < RCAR_DU_NUM_KMS_PLANES; ++i) {
513                 struct rcar_du_kms_plane *plane;
514
515                 plane = devm_kzalloc(rcdu->dev, sizeof(*plane), GFP_KERNEL);
516                 if (plane == NULL)
517                         return -ENOMEM;
518
519                 plane->hwplane = &planes->planes[i + 2];
520                 plane->hwplane->zpos = 1;
521                 plane->hwplane->interlace_flag = false;
522
523                 ret = drm_plane_init(rcdu->ddev, &plane->plane, crtcs,
524                                      &rcar_du_plane_funcs, formats,
525                                      ARRAY_SIZE(formats), false);
526                 if (ret < 0)
527                         return ret;
528
529                 drm_object_attach_property(&plane->plane.base,
530                                            planes->alpha, 255);
531                 drm_object_attach_property(&plane->plane.base,
532                                            planes->colorkey,
533                                            RCAR_DU_COLORKEY_NONE);
534                 drm_object_attach_property(&plane->plane.base,
535                                            planes->zpos, 1);
536         }
537
538         return 0;
539 }