linux/drivers/media/platform/vsp1/vsp1_wpf.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * vsp1_wpf.c  --  R-Car VSP1 Write Pixel Formatter
   4 *
   5 * Copyright (C) 2013-2014 Renesas Electronics Corporation
   6 *
   7 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
   8 */
   9
  10#include <linux/device.h>
  11
  12#include <media/v4l2-subdev.h>
  13
  14#include "vsp1.h"
  15#include "vsp1_dl.h"
  16#include "vsp1_pipe.h"
  17#include "vsp1_rwpf.h"
  18#include "vsp1_video.h"
  19
  20#define WPF_GEN2_MAX_WIDTH                      2048U
  21#define WPF_GEN2_MAX_HEIGHT                     2048U
  22#define WPF_GEN3_MAX_WIDTH                      8190U
  23#define WPF_GEN3_MAX_HEIGHT                     8190U
  24
  25/* -----------------------------------------------------------------------------
  26 * Device Access
  27 */
  28
  29static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf,
  30                                  struct vsp1_dl_body *dlb, u32 reg, u32 data)
  31{
  32        vsp1_dl_body_write(dlb, reg + wpf->entity.index * VI6_WPF_OFFSET, data);
  33}
  34
  35/* -----------------------------------------------------------------------------
  36 * Controls
  37 */
  38
  39enum wpf_flip_ctrl {
  40        WPF_CTRL_VFLIP = 0,
  41        WPF_CTRL_HFLIP = 1,
  42};
  43
  44static int vsp1_wpf_set_rotation(struct vsp1_rwpf *wpf, unsigned int rotation)
  45{
  46        struct vsp1_video *video = wpf->video;
  47        struct v4l2_mbus_framefmt *sink_format;
  48        struct v4l2_mbus_framefmt *source_format;
  49        bool rotate;
  50        int ret = 0;
  51
  52        /*
  53         * Only consider the 0°/180° from/to 90°/270° modifications, the rest
  54         * is taken care of by the flipping configuration.
  55         */
  56        rotate = rotation == 90 || rotation == 270;
  57        if (rotate == wpf->flip.rotate)
  58                return 0;
  59
  60        /* Changing rotation isn't allowed when buffers are allocated. */
  61        mutex_lock(&video->lock);
  62
  63        if (vb2_is_busy(&video->queue)) {
  64                ret = -EBUSY;
  65                goto done;
  66        }
  67
  68        sink_format = vsp1_entity_get_pad_format(&wpf->entity,
  69                                                 wpf->entity.config,
  70                                                 RWPF_PAD_SINK);
  71        source_format = vsp1_entity_get_pad_format(&wpf->entity,
  72                                                   wpf->entity.config,
  73                                                   RWPF_PAD_SOURCE);
  74
  75        mutex_lock(&wpf->entity.lock);
  76
  77        if (rotate) {
  78                source_format->width = sink_format->height;
  79                source_format->height = sink_format->width;
  80        } else {
  81                source_format->width = sink_format->width;
  82                source_format->height = sink_format->height;
  83        }
  84
  85        wpf->flip.rotate = rotate;
  86
  87        mutex_unlock(&wpf->entity.lock);
  88
  89done:
  90        mutex_unlock(&video->lock);
  91        return ret;
  92}
  93
  94static int vsp1_wpf_s_ctrl(struct v4l2_ctrl *ctrl)
  95{
  96        struct vsp1_rwpf *wpf =
  97                container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
  98        unsigned int rotation;
  99        u32 flip = 0;
 100        int ret;
 101
 102        /* Update the rotation. */
 103        rotation = wpf->flip.ctrls.rotate ? wpf->flip.ctrls.rotate->val : 0;
 104        ret = vsp1_wpf_set_rotation(wpf, rotation);
 105        if (ret < 0)
 106                return ret;
 107
 108        /*
 109         * Compute the flip value resulting from all three controls, with
 110         * rotation by 180° flipping the image in both directions. Store the
 111         * result in the pending flip field for the next frame that will be
 112         * processed.
 113         */
 114        if (wpf->flip.ctrls.vflip->val)
 115                flip |= BIT(WPF_CTRL_VFLIP);
 116
 117        if (wpf->flip.ctrls.hflip && wpf->flip.ctrls.hflip->val)
 118                flip |= BIT(WPF_CTRL_HFLIP);
 119
 120        if (rotation == 180 || rotation == 270)
 121                flip ^= BIT(WPF_CTRL_VFLIP) | BIT(WPF_CTRL_HFLIP);
 122
 123        spin_lock_irq(&wpf->flip.lock);
 124        wpf->flip.pending = flip;
 125        spin_unlock_irq(&wpf->flip.lock);
 126
 127        return 0;
 128}
 129
 130static const struct v4l2_ctrl_ops vsp1_wpf_ctrl_ops = {
 131        .s_ctrl = vsp1_wpf_s_ctrl,
 132};
 133
 134static int wpf_init_controls(struct vsp1_rwpf *wpf)
 135{
 136        struct vsp1_device *vsp1 = wpf->entity.vsp1;
 137        unsigned int num_flip_ctrls;
 138
 139        spin_lock_init(&wpf->flip.lock);
 140
 141        if (wpf->entity.index != 0) {
 142                /* Only WPF0 supports flipping. */
 143                num_flip_ctrls = 0;
 144        } else if (vsp1->info->features & VSP1_HAS_WPF_HFLIP) {
 145                /*
 146                 * When horizontal flip is supported the WPF implements three
 147                 * controls (horizontal flip, vertical flip and rotation).
 148                 */
 149                num_flip_ctrls = 3;
 150        } else if (vsp1->info->features & VSP1_HAS_WPF_VFLIP) {
 151                /*
 152                 * When only vertical flip is supported the WPF implements a
 153                 * single control (vertical flip).
 154                 */
 155                num_flip_ctrls = 1;
 156        } else {
 157                /* Otherwise flipping is not supported. */
 158                num_flip_ctrls = 0;
 159        }
 160
 161        vsp1_rwpf_init_ctrls(wpf, num_flip_ctrls);
 162
 163        if (num_flip_ctrls >= 1) {
 164                wpf->flip.ctrls.vflip =
 165                        v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops,
 166                                          V4L2_CID_VFLIP, 0, 1, 1, 0);
 167        }
 168
 169        if (num_flip_ctrls == 3) {
 170                wpf->flip.ctrls.hflip =
 171                        v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops,
 172                                          V4L2_CID_HFLIP, 0, 1, 1, 0);
 173                wpf->flip.ctrls.rotate =
 174                        v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops,
 175                                          V4L2_CID_ROTATE, 0, 270, 90, 0);
 176                v4l2_ctrl_cluster(3, &wpf->flip.ctrls.vflip);
 177        }
 178
 179        if (wpf->ctrls.error) {
 180                dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
 181                        wpf->entity.index);
 182                return wpf->ctrls.error;
 183        }
 184
 185        return 0;
 186}
 187
 188/* -----------------------------------------------------------------------------
 189 * V4L2 Subdevice Core Operations
 190 */
 191
 192static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
 193{
 194        struct vsp1_rwpf *wpf = to_rwpf(subdev);
 195        struct vsp1_device *vsp1 = wpf->entity.vsp1;
 196
 197        if (enable)
 198                return 0;
 199
 200        /*
 201         * Write to registers directly when stopping the stream as there will be
 202         * no pipeline run to apply the display list.
 203         */
 204        vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
 205        vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET +
 206                   VI6_WPF_SRCRPF, 0);
 207
 208        return 0;
 209}
 210
 211/* -----------------------------------------------------------------------------
 212 * V4L2 Subdevice Operations
 213 */
 214
 215static const struct v4l2_subdev_video_ops wpf_video_ops = {
 216        .s_stream = wpf_s_stream,
 217};
 218
 219static const struct v4l2_subdev_ops wpf_ops = {
 220        .video  = &wpf_video_ops,
 221        .pad    = &vsp1_rwpf_pad_ops,
 222};
 223
 224/* -----------------------------------------------------------------------------
 225 * VSP1 Entity Operations
 226 */
 227
 228static void vsp1_wpf_destroy(struct vsp1_entity *entity)
 229{
 230        struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
 231
 232        vsp1_dlm_destroy(wpf->dlm);
 233}
 234
 235static void wpf_configure_stream(struct vsp1_entity *entity,
 236                                 struct vsp1_pipeline *pipe,
 237                                 struct vsp1_dl_body *dlb)
 238{
 239        struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev);
 240        struct vsp1_device *vsp1 = wpf->entity.vsp1;
 241        const struct v4l2_mbus_framefmt *source_format;
 242        const struct v4l2_mbus_framefmt *sink_format;
 243        unsigned int i;
 244        u32 outfmt = 0;
 245        u32 srcrpf = 0;
 246
 247        sink_format = vsp1_entity_get_pad_format(&wpf->entity,
 248                                                 wpf->entity.config,
 249                                                 RWPF_PAD_SINK);
 250        source_format = vsp1_entity_get_pad_format(&wpf->entity,
 251                                                   wpf->entity.config,
 252                                                   RWPF_PAD_SOURCE);
 253        /* Format */
 254        if (!pipe->lif) {
 255                const struct v4l2_pix_format_mplane *format = &wpf->format;
 256                const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
 257
 258                outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
 259
 260                if (wpf->flip.rotate)
 261                        outfmt |= VI6_WPF_OUTFMT_ROT;
 262
 263                if (fmtinfo->alpha)
 264                        outfmt |= VI6_WPF_OUTFMT_PXA;
 265                if (fmtinfo->swap_yc)
 266                        outfmt |= VI6_WPF_OUTFMT_SPYCS;
 267                if (fmtinfo->swap_uv)
 268                        outfmt |= VI6_WPF_OUTFMT_SPUVS;
 269
 270                /* Destination stride and byte swapping. */
 271                vsp1_wpf_write(wpf, dlb, VI6_WPF_DSTM_STRIDE_Y,
 272                               format->plane_fmt[0].bytesperline);
 273                if (format->num_planes > 1)
 274                        vsp1_wpf_write(wpf, dlb, VI6_WPF_DSTM_STRIDE_C,
 275                                       format->plane_fmt[1].bytesperline);
 276
 277                vsp1_wpf_write(wpf, dlb, VI6_WPF_DSWAP, fmtinfo->swap);
 278
 279                if (vsp1->info->features & VSP1_HAS_WPF_HFLIP &&
 280                    wpf->entity.index == 0)
 281                        vsp1_wpf_write(wpf, dlb, VI6_WPF_ROT_CTRL,
 282                                       VI6_WPF_ROT_CTRL_LN16 |
 283                                       (256 << VI6_WPF_ROT_CTRL_LMEM_WD_SHIFT));
 284        }
 285
 286        if (sink_format->code != source_format->code)
 287                outfmt |= VI6_WPF_OUTFMT_CSC;
 288
 289        wpf->outfmt = outfmt;
 290
 291        vsp1_dl_body_write(dlb, VI6_DPR_WPF_FPORCH(wpf->entity.index),
 292                           VI6_DPR_WPF_FPORCH_FP_WPFN);
 293
 294        vsp1_dl_body_write(dlb, VI6_WPF_WRBCK_CTRL, 0);
 295
 296        /*
 297         * Sources. If the pipeline has a single input and BRx is not used,
 298         * configure it as the master layer. Otherwise configure all
 299         * inputs as sub-layers and select the virtual RPF as the master
 300         * layer.
 301         */
 302        for (i = 0; i < vsp1->info->rpf_count; ++i) {
 303                struct vsp1_rwpf *input = pipe->inputs[i];
 304
 305                if (!input)
 306                        continue;
 307
 308                srcrpf |= (!pipe->brx && pipe->num_inputs == 1)
 309                        ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
 310                        : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
 311        }
 312
 313        if (pipe->brx)
 314                srcrpf |= pipe->brx->type == VSP1_ENTITY_BRU
 315                        ? VI6_WPF_SRCRPF_VIRACT_MST
 316                        : VI6_WPF_SRCRPF_VIRACT2_MST;
 317
 318        vsp1_wpf_write(wpf, dlb, VI6_WPF_SRCRPF, srcrpf);
 319
 320        /* Enable interrupts */
 321        vsp1_dl_body_write(dlb, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
 322        vsp1_dl_body_write(dlb, VI6_WPF_IRQ_ENB(wpf->entity.index),
 323                           VI6_WFP_IRQ_ENB_DFEE);
 324}
 325
 326static void wpf_configure_frame(struct vsp1_entity *entity,
 327                                struct vsp1_pipeline *pipe,
 328                                struct vsp1_dl_list *dl,
 329                                struct vsp1_dl_body *dlb)
 330{
 331        const unsigned int mask = BIT(WPF_CTRL_VFLIP)
 332                                | BIT(WPF_CTRL_HFLIP);
 333        struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev);
 334        unsigned long flags;
 335        u32 outfmt;
 336
 337        spin_lock_irqsave(&wpf->flip.lock, flags);
 338        wpf->flip.active = (wpf->flip.active & ~mask)
 339                         | (wpf->flip.pending & mask);
 340        spin_unlock_irqrestore(&wpf->flip.lock, flags);
 341
 342        outfmt = (wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT) | wpf->outfmt;
 343
 344        if (wpf->flip.active & BIT(WPF_CTRL_VFLIP))
 345                outfmt |= VI6_WPF_OUTFMT_FLP;
 346        if (wpf->flip.active & BIT(WPF_CTRL_HFLIP))
 347                outfmt |= VI6_WPF_OUTFMT_HFLP;
 348
 349        vsp1_wpf_write(wpf, dlb, VI6_WPF_OUTFMT, outfmt);
 350}
 351
 352static void wpf_configure_partition(struct vsp1_entity *entity,
 353                                    struct vsp1_pipeline *pipe,
 354                                    struct vsp1_dl_list *dl,
 355                                    struct vsp1_dl_body *dlb)
 356{
 357        struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev);
 358        struct vsp1_device *vsp1 = wpf->entity.vsp1;
 359        struct vsp1_rwpf_memory mem = wpf->mem;
 360        const struct v4l2_mbus_framefmt *sink_format;
 361        const struct v4l2_pix_format_mplane *format = &wpf->format;
 362        const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
 363        unsigned int width;
 364        unsigned int height;
 365        unsigned int offset;
 366        unsigned int flip;
 367        unsigned int i;
 368
 369        sink_format = vsp1_entity_get_pad_format(&wpf->entity,
 370                                                 wpf->entity.config,
 371                                                 RWPF_PAD_SINK);
 372        width = sink_format->width;
 373        height = sink_format->height;
 374
 375        /*
 376         * Cropping. The partition algorithm can split the image into
 377         * multiple slices.
 378         */
 379        if (pipe->partitions > 1)
 380                width = pipe->partition->wpf.width;
 381
 382        vsp1_wpf_write(wpf, dlb, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
 383                       (0 << VI6_WPF_SZCLIP_OFST_SHIFT) |
 384                       (width << VI6_WPF_SZCLIP_SIZE_SHIFT));
 385        vsp1_wpf_write(wpf, dlb, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
 386                       (0 << VI6_WPF_SZCLIP_OFST_SHIFT) |
 387                       (height << VI6_WPF_SZCLIP_SIZE_SHIFT));
 388
 389        if (pipe->lif)
 390                return;
 391
 392        /*
 393         * Update the memory offsets based on flipping configuration.
 394         * The destination addresses point to the locations where the
 395         * VSP starts writing to memory, which can be any corner of the
 396         * image depending on the combination of flipping and rotation.
 397         */
 398
 399        /*
 400         * First take the partition left coordinate into account.
 401         * Compute the offset to order the partitions correctly on the
 402         * output based on whether flipping is enabled. Consider
 403         * horizontal flipping when rotation is disabled but vertical
 404         * flipping when rotation is enabled, as rotating the image
 405         * switches the horizontal and vertical directions. The offset
 406         * is applied horizontally or vertically accordingly.
 407         */
 408        flip = wpf->flip.active;
 409
 410        if (flip & BIT(WPF_CTRL_HFLIP) && !wpf->flip.rotate)
 411                offset = format->width - pipe->partition->wpf.left
 412                        - pipe->partition->wpf.width;
 413        else if (flip & BIT(WPF_CTRL_VFLIP) && wpf->flip.rotate)
 414                offset = format->height - pipe->partition->wpf.left
 415                        - pipe->partition->wpf.width;
 416        else
 417                offset = pipe->partition->wpf.left;
 418
 419        for (i = 0; i < format->num_planes; ++i) {
 420                unsigned int hsub = i > 0 ? fmtinfo->hsub : 1;
 421                unsigned int vsub = i > 0 ? fmtinfo->vsub : 1;
 422
 423                if (wpf->flip.rotate)
 424                        mem.addr[i] += offset / vsub
 425                                     * format->plane_fmt[i].bytesperline;
 426                else
 427                        mem.addr[i] += offset / hsub
 428                                     * fmtinfo->bpp[i] / 8;
 429        }
 430
 431        if (flip & BIT(WPF_CTRL_VFLIP)) {
 432                /*
 433                 * When rotating the output (after rotation) image
 434                 * height is equal to the partition width (before
 435                 * rotation). Otherwise it is equal to the output
 436                 * image height.
 437                 */
 438                if (wpf->flip.rotate)
 439                        height = pipe->partition->wpf.width;
 440                else
 441                        height = format->height;
 442
 443                mem.addr[0] += (height - 1)
 444                             * format->plane_fmt[0].bytesperline;
 445
 446                if (format->num_planes > 1) {
 447                        offset = (height / fmtinfo->vsub - 1)
 448                               * format->plane_fmt[1].bytesperline;
 449                        mem.addr[1] += offset;
 450                        mem.addr[2] += offset;
 451                }
 452        }
 453
 454        if (wpf->flip.rotate && !(flip & BIT(WPF_CTRL_HFLIP))) {
 455                unsigned int hoffset = max(0, (int)format->width - 16);
 456
 457                /*
 458                 * Compute the output coordinate. The partition
 459                 * horizontal (left) offset becomes a vertical offset.
 460                 */
 461                for (i = 0; i < format->num_planes; ++i) {
 462                        unsigned int hsub = i > 0 ? fmtinfo->hsub : 1;
 463
 464                        mem.addr[i] += hoffset / hsub
 465                                     * fmtinfo->bpp[i] / 8;
 466                }
 467        }
 468
 469        /*
 470         * On Gen3 hardware the SPUVS bit has no effect on 3-planar
 471         * formats. Swap the U and V planes manually in that case.
 472         */
 473        if (vsp1->info->gen == 3 && format->num_planes == 3 &&
 474            fmtinfo->swap_uv)
 475                swap(mem.addr[1], mem.addr[2]);
 476
 477        vsp1_wpf_write(wpf, dlb, VI6_WPF_DSTM_ADDR_Y, mem.addr[0]);
 478        vsp1_wpf_write(wpf, dlb, VI6_WPF_DSTM_ADDR_C0, mem.addr[1]);
 479        vsp1_wpf_write(wpf, dlb, VI6_WPF_DSTM_ADDR_C1, mem.addr[2]);
 480}
 481
 482static unsigned int wpf_max_width(struct vsp1_entity *entity,
 483                                  struct vsp1_pipeline *pipe)
 484{
 485        struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev);
 486
 487        return wpf->flip.rotate ? 256 : wpf->max_width;
 488}
 489
 490static void wpf_partition(struct vsp1_entity *entity,
 491                          struct vsp1_pipeline *pipe,
 492                          struct vsp1_partition *partition,
 493                          unsigned int partition_idx,
 494                          struct vsp1_partition_window *window)
 495{
 496        partition->wpf = *window;
 497}
 498
 499static const struct vsp1_entity_operations wpf_entity_ops = {
 500        .destroy = vsp1_wpf_destroy,
 501        .configure_stream = wpf_configure_stream,
 502        .configure_frame = wpf_configure_frame,
 503        .configure_partition = wpf_configure_partition,
 504        .max_width = wpf_max_width,
 505        .partition = wpf_partition,
 506};
 507
 508/* -----------------------------------------------------------------------------
 509 * Initialization and Cleanup
 510 */
 511
 512struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
 513{
 514        struct vsp1_rwpf *wpf;
 515        char name[6];
 516        int ret;
 517
 518        wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
 519        if (wpf == NULL)
 520                return ERR_PTR(-ENOMEM);
 521
 522        if (vsp1->info->gen == 2) {
 523                wpf->max_width = WPF_GEN2_MAX_WIDTH;
 524                wpf->max_height = WPF_GEN2_MAX_HEIGHT;
 525        } else {
 526                wpf->max_width = WPF_GEN3_MAX_WIDTH;
 527                wpf->max_height = WPF_GEN3_MAX_HEIGHT;
 528        }
 529
 530        wpf->entity.ops = &wpf_entity_ops;
 531        wpf->entity.type = VSP1_ENTITY_WPF;
 532        wpf->entity.index = index;
 533
 534        sprintf(name, "wpf.%u", index);
 535        ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &wpf_ops,
 536                               MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
 537        if (ret < 0)
 538                return ERR_PTR(ret);
 539
 540        /* Initialize the display list manager. */
 541        wpf->dlm = vsp1_dlm_create(vsp1, index, 64);
 542        if (!wpf->dlm) {
 543                ret = -ENOMEM;
 544                goto error;
 545        }
 546
 547        /* Initialize the control handler. */
 548        ret = wpf_init_controls(wpf);
 549        if (ret < 0) {
 550                dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
 551                        index);
 552                goto error;
 553        }
 554
 555        v4l2_ctrl_handler_setup(&wpf->ctrls);
 556
 557        return wpf;
 558
 559error:
 560        vsp1_entity_destroy(&wpf->entity);
 561        return ERR_PTR(ret);
 562}
 563