blob: 9385bc703dcd54010aa8a53c3a589bbc95ed99ba [file] [log] [blame]
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03001/*
2 * vsp1_wpf.c -- R-Car VSP1 Write Pixel Formatter
3 *
Laurent Pinchart8a1edc52014-02-06 14:42:31 -03004 * Copyright (C) 2013-2014 Renesas Electronics Corporation
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03005 *
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 <linux/device.h>
15
16#include <media/v4l2-subdev.h>
17
18#include "vsp1.h"
Laurent Pinchartef9621b2015-11-14 22:27:52 -020019#include "vsp1_dl.h"
Laurent Pincharta0cdac52016-01-17 19:53:56 -020020#include "vsp1_pipe.h"
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030021#include "vsp1_rwpf.h"
22#include "vsp1_video.h"
23
Laurent Pinchart0d268dc2016-03-25 06:51:06 -030024#define WPF_GEN2_MAX_WIDTH 2048U
25#define WPF_GEN2_MAX_HEIGHT 2048U
26#define WPF_GEN3_MAX_WIDTH 8190U
27#define WPF_GEN3_MAX_HEIGHT 8190U
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030028
29/* -----------------------------------------------------------------------------
30 * Device Access
31 */
32
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020033static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf,
34 struct vsp1_dl_list *dl, u32 reg, u32 data)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030035{
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020036 vsp1_dl_list_write(dl, reg + wpf->entity.index * VI6_WPF_OFFSET, data);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030037}
38
39/* -----------------------------------------------------------------------------
40 * V4L2 Subdevice Core Operations
41 */
42
43static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
44{
45 struct vsp1_rwpf *wpf = to_rwpf(subdev);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030046 struct vsp1_device *vsp1 = wpf->entity.vsp1;
Laurent Pinchart7578c202014-05-21 19:00:05 -030047
Laurent Pinchart7b905f02015-11-17 13:10:26 -020048 if (enable)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030049 return 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030050
Laurent Pinchart7b905f02015-11-17 13:10:26 -020051 /* Write to registers directly when stopping the stream as there will be
52 * no pipeline run to apply the display list.
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030053 */
Laurent Pinchart7b905f02015-11-17 13:10:26 -020054 vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
55 vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET +
56 VI6_WPF_SRCRPF, 0);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030057
58 return 0;
59}
60
61/* -----------------------------------------------------------------------------
62 * V4L2 Subdevice Operations
63 */
64
Laurent Pincharteb9163d2016-06-17 21:11:26 -030065static const struct v4l2_subdev_video_ops wpf_video_ops = {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030066 .s_stream = wpf_s_stream,
67};
68
Laurent Pincharteb9163d2016-06-17 21:11:26 -030069static const struct v4l2_subdev_ops wpf_ops = {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030070 .video = &wpf_video_ops,
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -020071 .pad = &vsp1_rwpf_pad_ops,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030072};
73
74/* -----------------------------------------------------------------------------
Laurent Pinchart52434532015-11-17 12:23:23 -020075 * VSP1 Entity Operations
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030076 */
77
Laurent Pinchart52434532015-11-17 12:23:23 -020078static void vsp1_wpf_destroy(struct vsp1_entity *entity)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030079{
Laurent Pinchart52434532015-11-17 12:23:23 -020080 struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
81
82 vsp1_dlm_destroy(wpf->dlm);
83}
84
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020085static void wpf_set_memory(struct vsp1_entity *entity, struct vsp1_dl_list *dl)
Laurent Pinchart52434532015-11-17 12:23:23 -020086{
87 struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
88
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -020089 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_Y, wpf->mem.addr[0]);
90 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C0, wpf->mem.addr[1]);
91 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C1, wpf->mem.addr[2]);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030092}
93
Laurent Pinchart83dd0192016-01-14 14:17:32 -020094static void wpf_configure(struct vsp1_entity *entity,
95 struct vsp1_pipeline *pipe,
Laurent Pinchartfc845e52016-06-11 04:07:56 -030096 struct vsp1_dl_list *dl, bool full)
Laurent Pinchart7b905f02015-11-17 13:10:26 -020097{
Laurent Pinchart7b905f02015-11-17 13:10:26 -020098 struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev);
99 struct vsp1_device *vsp1 = wpf->entity.vsp1;
100 const struct v4l2_mbus_framefmt *source_format;
101 const struct v4l2_mbus_framefmt *sink_format;
102 const struct v4l2_rect *crop;
103 unsigned int i;
104 u32 outfmt = 0;
105 u32 srcrpf = 0;
106
Laurent Pinchartd05a3312016-06-20 05:04:38 -0300107 if (!full) {
108 vsp1_wpf_write(wpf, dl, VI6_WPF_OUTFMT, wpf->outfmt |
109 (wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT));
Laurent Pinchartfc845e52016-06-11 04:07:56 -0300110 return;
Laurent Pinchartd05a3312016-06-20 05:04:38 -0300111 }
Laurent Pinchartfc845e52016-06-11 04:07:56 -0300112
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200113 /* Cropping */
114 crop = vsp1_rwpf_get_crop(wpf, wpf->entity.config);
115
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200116 vsp1_wpf_write(wpf, dl, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200117 (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) |
118 (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT));
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200119 vsp1_wpf_write(wpf, dl, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200120 (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) |
121 (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT));
122
123 /* Format */
124 sink_format = vsp1_entity_get_pad_format(&wpf->entity,
125 wpf->entity.config,
126 RWPF_PAD_SINK);
127 source_format = vsp1_entity_get_pad_format(&wpf->entity,
128 wpf->entity.config,
129 RWPF_PAD_SOURCE);
130
131 if (!pipe->lif) {
132 const struct v4l2_pix_format_mplane *format = &wpf->format;
133 const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
134
135 outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
136
137 if (fmtinfo->alpha)
138 outfmt |= VI6_WPF_OUTFMT_PXA;
139 if (fmtinfo->swap_yc)
140 outfmt |= VI6_WPF_OUTFMT_SPYCS;
141 if (fmtinfo->swap_uv)
142 outfmt |= VI6_WPF_OUTFMT_SPUVS;
143
144 /* Destination stride and byte swapping. */
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200145 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_Y,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200146 format->plane_fmt[0].bytesperline);
147 if (format->num_planes > 1)
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200148 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_C,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200149 format->plane_fmt[1].bytesperline);
150
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200151 vsp1_wpf_write(wpf, dl, VI6_WPF_DSWAP, fmtinfo->swap);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200152 }
153
154 if (sink_format->code != source_format->code)
155 outfmt |= VI6_WPF_OUTFMT_CSC;
156
Laurent Pinchartd05a3312016-06-20 05:04:38 -0300157 wpf->outfmt = outfmt;
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200158
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200159 vsp1_dl_list_write(dl, VI6_DPR_WPF_FPORCH(wpf->entity.index),
160 VI6_DPR_WPF_FPORCH_FP_WPFN);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200161
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200162 vsp1_dl_list_write(dl, VI6_WPF_WRBCK_CTRL, 0);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200163
164 /* Sources. If the pipeline has a single input and BRU is not used,
165 * configure it as the master layer. Otherwise configure all
166 * inputs as sub-layers and select the virtual RPF as the master
167 * layer.
168 */
169 for (i = 0; i < vsp1->info->rpf_count; ++i) {
170 struct vsp1_rwpf *input = pipe->inputs[i];
171
172 if (!input)
173 continue;
174
175 srcrpf |= (!pipe->bru && pipe->num_inputs == 1)
176 ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
177 : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
178 }
179
180 if (pipe->bru || pipe->num_inputs > 1)
181 srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
182
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200183 vsp1_wpf_write(wpf, dl, VI6_WPF_SRCRPF, srcrpf);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200184
185 /* Enable interrupts */
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200186 vsp1_dl_list_write(dl, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
187 vsp1_dl_list_write(dl, VI6_WPF_IRQ_ENB(wpf->entity.index),
188 VI6_WFP_IRQ_ENB_FREE);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200189}
190
Laurent Pinchart52434532015-11-17 12:23:23 -0200191static const struct vsp1_entity_operations wpf_entity_ops = {
192 .destroy = vsp1_wpf_destroy,
Laurent Pinchartb58faa92015-07-28 16:04:47 -0300193 .set_memory = wpf_set_memory,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200194 .configure = wpf_configure,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300195};
196
197/* -----------------------------------------------------------------------------
198 * Initialization and Cleanup
199 */
200
201struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
202{
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300203 struct vsp1_rwpf *wpf;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200204 char name[6];
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300205 int ret;
206
207 wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
208 if (wpf == NULL)
209 return ERR_PTR(-ENOMEM);
210
Laurent Pinchart0d268dc2016-03-25 06:51:06 -0300211 if (vsp1->info->gen == 2) {
212 wpf->max_width = WPF_GEN2_MAX_WIDTH;
213 wpf->max_height = WPF_GEN2_MAX_HEIGHT;
214 } else {
215 wpf->max_width = WPF_GEN3_MAX_WIDTH;
216 wpf->max_height = WPF_GEN3_MAX_HEIGHT;
217 }
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300218
Laurent Pinchart52434532015-11-17 12:23:23 -0200219 wpf->entity.ops = &wpf_entity_ops;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300220 wpf->entity.type = VSP1_ENTITY_WPF;
221 wpf->entity.index = index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300222
Laurent Pinchart823329d2015-11-15 19:42:01 -0200223 sprintf(name, "wpf.%u", index);
Laurent Pinchart6a8e07b2016-02-15 22:10:26 -0200224 ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &wpf_ops,
225 MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300226 if (ret < 0)
227 return ERR_PTR(ret);
228
Laurent Pinchart351bbf92015-11-01 15:18:56 -0200229 /* Initialize the display list manager. */
230 wpf->dlm = vsp1_dlm_create(vsp1, index, 4);
231 if (!wpf->dlm) {
232 ret = -ENOMEM;
233 goto error;
Laurent Pinchartef9621b2015-11-14 22:27:52 -0200234 }
235
Laurent Pinchart7578c202014-05-21 19:00:05 -0300236 /* Initialize the control handler. */
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200237 ret = vsp1_rwpf_init_ctrls(wpf);
238 if (ret < 0) {
Laurent Pinchart7578c202014-05-21 19:00:05 -0300239 dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
240 index);
Laurent Pinchart7578c202014-05-21 19:00:05 -0300241 goto error;
242 }
243
Laurent Pinchartd05a3312016-06-20 05:04:38 -0300244 v4l2_ctrl_handler_setup(&wpf->ctrls);
245
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300246 return wpf;
247
Laurent Pinchart1499be62014-05-28 12:49:13 -0300248error:
249 vsp1_entity_destroy(&wpf->entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300250 return ERR_PTR(ret);
251}