blob: fdee5a891e4074681871b38aee21c85bbcd85923 [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/* -----------------------------------------------------------------------------
Laurent Pinchart894dde52016-05-26 05:14:22 -030040 * Controls
41 */
42
43enum wpf_flip_ctrl {
44 WPF_CTRL_VFLIP = 0,
45 WPF_CTRL_HFLIP = 1,
46 WPF_CTRL_MAX,
47};
48
49static int vsp1_wpf_s_ctrl(struct v4l2_ctrl *ctrl)
50{
51 struct vsp1_rwpf *wpf =
52 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
53 unsigned int i;
54 u32 flip = 0;
55
56 switch (ctrl->id) {
57 case V4L2_CID_HFLIP:
58 case V4L2_CID_VFLIP:
59 for (i = 0; i < WPF_CTRL_MAX; ++i) {
60 if (wpf->flip.ctrls[i])
61 flip |= wpf->flip.ctrls[i]->val ? BIT(i) : 0;
62 }
63
64 spin_lock_irq(&wpf->flip.lock);
65 wpf->flip.pending = flip;
66 spin_unlock_irq(&wpf->flip.lock);
67 break;
68
69 default:
70 return -EINVAL;
71 }
72
73 return 0;
74}
75
76static const struct v4l2_ctrl_ops vsp1_wpf_ctrl_ops = {
77 .s_ctrl = vsp1_wpf_s_ctrl,
78};
79
80static int wpf_init_controls(struct vsp1_rwpf *wpf)
81{
82 struct vsp1_device *vsp1 = wpf->entity.vsp1;
83 unsigned int num_flip_ctrls;
84
85 spin_lock_init(&wpf->flip.lock);
86
87 if (wpf->entity.index != 0) {
88 /* Only WPF0 supports flipping. */
89 num_flip_ctrls = 0;
90 } else if (vsp1->info->features & VSP1_HAS_WPF_HFLIP) {
91 /* When horizontal flip is supported the WPF implements two
92 * controls (horizontal flip and vertical flip).
93 */
94 num_flip_ctrls = 2;
95 } else if (vsp1->info->features & VSP1_HAS_WPF_VFLIP) {
96 /* When only vertical flip is supported the WPF implements a
97 * single control (vertical flip).
98 */
99 num_flip_ctrls = 1;
100 } else {
101 /* Otherwise flipping is not supported. */
102 num_flip_ctrls = 0;
103 }
104
105 vsp1_rwpf_init_ctrls(wpf, num_flip_ctrls);
106
107 if (num_flip_ctrls >= 1) {
108 wpf->flip.ctrls[WPF_CTRL_VFLIP] =
109 v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops,
110 V4L2_CID_VFLIP, 0, 1, 1, 0);
111 }
112
113 if (num_flip_ctrls == 2) {
114 wpf->flip.ctrls[WPF_CTRL_HFLIP] =
115 v4l2_ctrl_new_std(&wpf->ctrls, &vsp1_wpf_ctrl_ops,
116 V4L2_CID_HFLIP, 0, 1, 1, 0);
117
118 v4l2_ctrl_cluster(2, wpf->flip.ctrls);
119 }
120
121 if (wpf->ctrls.error) {
122 dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
123 wpf->entity.index);
124 return wpf->ctrls.error;
125 }
126
127 return 0;
128}
129
130/* -----------------------------------------------------------------------------
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300131 * V4L2 Subdevice Core Operations
132 */
133
134static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
135{
136 struct vsp1_rwpf *wpf = to_rwpf(subdev);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300137 struct vsp1_device *vsp1 = wpf->entity.vsp1;
Laurent Pinchart7578c202014-05-21 19:00:05 -0300138
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200139 if (enable)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300140 return 0;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300141
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200142 /* Write to registers directly when stopping the stream as there will be
143 * no pipeline run to apply the display list.
Laurent Pinchart629bb6d2013-07-10 18:03:46 -0300144 */
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200145 vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
146 vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET +
147 VI6_WPF_SRCRPF, 0);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300148
149 return 0;
150}
151
152/* -----------------------------------------------------------------------------
153 * V4L2 Subdevice Operations
154 */
155
Laurent Pincharteb9163d2016-06-17 21:11:26 -0300156static const struct v4l2_subdev_video_ops wpf_video_ops = {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300157 .s_stream = wpf_s_stream,
158};
159
Laurent Pincharteb9163d2016-06-17 21:11:26 -0300160static const struct v4l2_subdev_ops wpf_ops = {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300161 .video = &wpf_video_ops,
Laurent Pinchartc6c8efb2015-11-22 13:37:45 -0200162 .pad = &vsp1_rwpf_pad_ops,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300163};
164
165/* -----------------------------------------------------------------------------
Laurent Pinchart52434532015-11-17 12:23:23 -0200166 * VSP1 Entity Operations
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300167 */
168
Laurent Pinchart52434532015-11-17 12:23:23 -0200169static void vsp1_wpf_destroy(struct vsp1_entity *entity)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300170{
Laurent Pinchart52434532015-11-17 12:23:23 -0200171 struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
172
173 vsp1_dlm_destroy(wpf->dlm);
174}
175
Laurent Pinchart83dd0192016-01-14 14:17:32 -0200176static void wpf_configure(struct vsp1_entity *entity,
177 struct vsp1_pipeline *pipe,
Laurent Pinchartd21fbbb2016-09-11 19:39:30 -0300178 struct vsp1_dl_list *dl,
179 enum vsp1_entity_params params)
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200180{
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200181 struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev);
182 struct vsp1_device *vsp1 = wpf->entity.vsp1;
183 const struct v4l2_mbus_framefmt *source_format;
184 const struct v4l2_mbus_framefmt *sink_format;
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200185 unsigned int i;
186 u32 outfmt = 0;
187 u32 srcrpf = 0;
188
Laurent Pinchartd21fbbb2016-09-11 19:39:30 -0300189 if (params == VSP1_ENTITY_PARAMS_RUNTIME) {
Laurent Pinchart894dde52016-05-26 05:14:22 -0300190 const unsigned int mask = BIT(WPF_CTRL_VFLIP)
191 | BIT(WPF_CTRL_HFLIP);
192
193 spin_lock(&wpf->flip.lock);
194 wpf->flip.active = (wpf->flip.active & ~mask)
195 | (wpf->flip.pending & mask);
196 spin_unlock(&wpf->flip.lock);
197
198 outfmt = (wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT) | wpf->outfmt;
199
200 if (wpf->flip.active & BIT(WPF_CTRL_VFLIP))
201 outfmt |= VI6_WPF_OUTFMT_FLP;
202 if (wpf->flip.active & BIT(WPF_CTRL_HFLIP))
203 outfmt |= VI6_WPF_OUTFMT_HFLP;
204
205 vsp1_wpf_write(wpf, dl, VI6_WPF_OUTFMT, outfmt);
Laurent Pinchartfc845e52016-06-11 04:07:56 -0300206 return;
Laurent Pinchartd05a3312016-06-20 05:04:38 -0300207 }
Laurent Pinchartfc845e52016-06-11 04:07:56 -0300208
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200209 sink_format = vsp1_entity_get_pad_format(&wpf->entity,
210 wpf->entity.config,
211 RWPF_PAD_SINK);
212 source_format = vsp1_entity_get_pad_format(&wpf->entity,
213 wpf->entity.config,
214 RWPF_PAD_SOURCE);
215
Laurent Pinchart8ddf3782016-09-12 09:50:13 -0300216 if (params == VSP1_ENTITY_PARAMS_PARTITION) {
217 const struct v4l2_pix_format_mplane *format = &wpf->format;
218 struct vsp1_rwpf_memory mem = wpf->mem;
219 unsigned int flip = wpf->flip.active;
220 unsigned int width = source_format->width;
221 unsigned int height = source_format->height;
222 unsigned int offset;
Laurent Pinchartb61bead2016-09-11 22:41:06 -0300223
Laurent Pinchart8ddf3782016-09-12 09:50:13 -0300224 /* Cropping. The partition algorithm can split the image into
225 * multiple slices.
226 */
Kieran Binghamfc6e5142016-09-11 23:26:35 -0300227 if (pipe->partitions > 1)
228 width = pipe->partition.width;
229
Laurent Pinchart8ddf3782016-09-12 09:50:13 -0300230 vsp1_wpf_write(wpf, dl, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
231 (0 << VI6_WPF_SZCLIP_OFST_SHIFT) |
232 (width << VI6_WPF_SZCLIP_SIZE_SHIFT));
233 vsp1_wpf_write(wpf, dl, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
234 (0 << VI6_WPF_SZCLIP_OFST_SHIFT) |
235 (height << VI6_WPF_SZCLIP_SIZE_SHIFT));
236
237 if (pipe->lif)
238 return;
239
240 /* Update the memory offsets based on flipping configuration.
241 * The destination addresses point to the locations where the
242 * VSP starts writing to memory, which can be different corners
Kieran Binghamfc6e5142016-09-11 23:26:35 -0300243 * of the image depending on vertical flipping.
Laurent Pinchart8ddf3782016-09-12 09:50:13 -0300244 */
Kieran Binghamfc6e5142016-09-11 23:26:35 -0300245 if (pipe->partitions > 1) {
246 const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
247
248 /* Horizontal flipping is handled through a line buffer
249 * and doesn't modify the start address, but still needs
250 * to be handled when image partitioning is in effect to
251 * order the partitions correctly.
252 */
253 if (flip & BIT(WPF_CTRL_HFLIP))
254 offset = format->width - pipe->partition.left
255 - pipe->partition.width;
256 else
257 offset = pipe->partition.left;
258
259 mem.addr[0] += offset * fmtinfo->bpp[0] / 8;
260 if (format->num_planes > 1) {
261 mem.addr[1] += offset / fmtinfo->hsub
262 * fmtinfo->bpp[1] / 8;
263 mem.addr[2] += offset / fmtinfo->hsub
264 * fmtinfo->bpp[2] / 8;
265 }
266 }
267
Laurent Pinchart8ddf3782016-09-12 09:50:13 -0300268 if (flip & BIT(WPF_CTRL_VFLIP)) {
269 mem.addr[0] += (format->height - 1)
270 * format->plane_fmt[0].bytesperline;
271
272 if (format->num_planes > 1) {
273 offset = (format->height / wpf->fmtinfo->vsub - 1)
274 * format->plane_fmt[1].bytesperline;
275 mem.addr[1] += offset;
276 mem.addr[2] += offset;
277 }
278 }
279
280 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_Y, mem.addr[0]);
281 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C0, mem.addr[1]);
282 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C1, mem.addr[2]);
283 return;
284 }
285
286 /* Format */
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200287 if (!pipe->lif) {
288 const struct v4l2_pix_format_mplane *format = &wpf->format;
289 const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
290
291 outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
292
293 if (fmtinfo->alpha)
294 outfmt |= VI6_WPF_OUTFMT_PXA;
295 if (fmtinfo->swap_yc)
296 outfmt |= VI6_WPF_OUTFMT_SPYCS;
297 if (fmtinfo->swap_uv)
298 outfmt |= VI6_WPF_OUTFMT_SPUVS;
299
300 /* Destination stride and byte swapping. */
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200301 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_Y,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200302 format->plane_fmt[0].bytesperline);
303 if (format->num_planes > 1)
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200304 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_C,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200305 format->plane_fmt[1].bytesperline);
306
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200307 vsp1_wpf_write(wpf, dl, VI6_WPF_DSWAP, fmtinfo->swap);
Laurent Pinchart894dde52016-05-26 05:14:22 -0300308
309 if (vsp1->info->features & VSP1_HAS_WPF_HFLIP &&
310 wpf->entity.index == 0)
311 vsp1_wpf_write(wpf, dl, VI6_WPF_ROT_CTRL,
312 VI6_WPF_ROT_CTRL_LN16 |
313 (256 << VI6_WPF_ROT_CTRL_LMEM_WD_SHIFT));
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200314 }
315
316 if (sink_format->code != source_format->code)
317 outfmt |= VI6_WPF_OUTFMT_CSC;
318
Laurent Pinchartd05a3312016-06-20 05:04:38 -0300319 wpf->outfmt = outfmt;
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200320
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200321 vsp1_dl_list_write(dl, VI6_DPR_WPF_FPORCH(wpf->entity.index),
322 VI6_DPR_WPF_FPORCH_FP_WPFN);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200323
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200324 vsp1_dl_list_write(dl, VI6_WPF_WRBCK_CTRL, 0);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200325
326 /* Sources. If the pipeline has a single input and BRU is not used,
327 * configure it as the master layer. Otherwise configure all
328 * inputs as sub-layers and select the virtual RPF as the master
329 * layer.
330 */
331 for (i = 0; i < vsp1->info->rpf_count; ++i) {
332 struct vsp1_rwpf *input = pipe->inputs[i];
333
334 if (!input)
335 continue;
336
337 srcrpf |= (!pipe->bru && pipe->num_inputs == 1)
338 ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
339 : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
340 }
341
342 if (pipe->bru || pipe->num_inputs > 1)
343 srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
344
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200345 vsp1_wpf_write(wpf, dl, VI6_WPF_SRCRPF, srcrpf);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200346
347 /* Enable interrupts */
Laurent Pinchart5e8dbbf2015-11-22 20:29:25 -0200348 vsp1_dl_list_write(dl, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
349 vsp1_dl_list_write(dl, VI6_WPF_IRQ_ENB(wpf->entity.index),
Kieran Bingham4c4b57b2016-09-06 06:55:02 -0300350 VI6_WFP_IRQ_ENB_DFEE);
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200351}
352
Laurent Pinchart52434532015-11-17 12:23:23 -0200353static const struct vsp1_entity_operations wpf_entity_ops = {
354 .destroy = vsp1_wpf_destroy,
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200355 .configure = wpf_configure,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300356};
357
358/* -----------------------------------------------------------------------------
359 * Initialization and Cleanup
360 */
361
362struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
363{
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300364 struct vsp1_rwpf *wpf;
Laurent Pinchart823329d2015-11-15 19:42:01 -0200365 char name[6];
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300366 int ret;
367
368 wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
369 if (wpf == NULL)
370 return ERR_PTR(-ENOMEM);
371
Laurent Pinchart0d268dc2016-03-25 06:51:06 -0300372 if (vsp1->info->gen == 2) {
373 wpf->max_width = WPF_GEN2_MAX_WIDTH;
374 wpf->max_height = WPF_GEN2_MAX_HEIGHT;
375 } else {
376 wpf->max_width = WPF_GEN3_MAX_WIDTH;
377 wpf->max_height = WPF_GEN3_MAX_HEIGHT;
378 }
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300379
Laurent Pinchart52434532015-11-17 12:23:23 -0200380 wpf->entity.ops = &wpf_entity_ops;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300381 wpf->entity.type = VSP1_ENTITY_WPF;
382 wpf->entity.index = index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300383
Laurent Pinchart823329d2015-11-15 19:42:01 -0200384 sprintf(name, "wpf.%u", index);
Laurent Pinchart6a8e07b2016-02-15 22:10:26 -0200385 ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &wpf_ops,
386 MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300387 if (ret < 0)
388 return ERR_PTR(ret);
389
Laurent Pinchart351bbf92015-11-01 15:18:56 -0200390 /* Initialize the display list manager. */
Kieran Bingham76e48892016-07-12 13:49:46 -0300391 wpf->dlm = vsp1_dlm_create(vsp1, index, 64);
Laurent Pinchart351bbf92015-11-01 15:18:56 -0200392 if (!wpf->dlm) {
393 ret = -ENOMEM;
394 goto error;
Laurent Pinchartef9621b2015-11-14 22:27:52 -0200395 }
396
Laurent Pinchart7578c202014-05-21 19:00:05 -0300397 /* Initialize the control handler. */
Laurent Pinchart894dde52016-05-26 05:14:22 -0300398 ret = wpf_init_controls(wpf);
Laurent Pinchartbd2fdd52015-11-01 12:19:42 -0200399 if (ret < 0) {
Laurent Pinchart7578c202014-05-21 19:00:05 -0300400 dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
401 index);
Laurent Pinchart7578c202014-05-21 19:00:05 -0300402 goto error;
403 }
404
Laurent Pinchartd05a3312016-06-20 05:04:38 -0300405 v4l2_ctrl_handler_setup(&wpf->ctrls);
406
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300407 return wpf;
408
Laurent Pinchart1499be62014-05-28 12:49:13 -0300409error:
410 vsp1_entity_destroy(&wpf->entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300411 return ERR_PTR(ret);
412}