blob: 6e057762c9339e26db23e57034efd8e31c7b80bd [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"
19#include "vsp1_rwpf.h"
20#include "vsp1_video.h"
21
22#define WPF_MAX_WIDTH 2048
23#define WPF_MAX_HEIGHT 2048
24
25/* -----------------------------------------------------------------------------
26 * Device Access
27 */
28
29static inline u32 vsp1_wpf_read(struct vsp1_rwpf *wpf, u32 reg)
30{
31 return vsp1_read(wpf->entity.vsp1,
32 reg + wpf->entity.index * VI6_WPF_OFFSET);
33}
34
35static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf, u32 reg, u32 data)
36{
37 vsp1_write(wpf->entity.vsp1,
38 reg + wpf->entity.index * VI6_WPF_OFFSET, data);
39}
40
41/* -----------------------------------------------------------------------------
Laurent Pinchart7578c202014-05-21 19:00:05 -030042 * Controls
43 */
44
45static int wpf_s_ctrl(struct v4l2_ctrl *ctrl)
46{
47 struct vsp1_rwpf *wpf =
48 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
49 u32 value;
50
51 if (!vsp1_entity_is_streaming(&wpf->entity))
52 return 0;
53
54 switch (ctrl->id) {
55 case V4L2_CID_ALPHA_COMPONENT:
56 value = vsp1_wpf_read(wpf, VI6_WPF_OUTFMT);
57 value &= ~VI6_WPF_OUTFMT_PDV_MASK;
58 value |= ctrl->val << VI6_WPF_OUTFMT_PDV_SHIFT;
59 vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, value);
60 break;
61 }
62
63 return 0;
64}
65
66static const struct v4l2_ctrl_ops wpf_ctrl_ops = {
67 .s_ctrl = wpf_s_ctrl,
68};
69
70/* -----------------------------------------------------------------------------
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030071 * V4L2 Subdevice Core Operations
72 */
73
74static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
75{
Laurent Pinchart5aeb01ad2014-05-27 20:35:36 -030076 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&subdev->entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030077 struct vsp1_rwpf *wpf = to_rwpf(subdev);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030078 struct vsp1_device *vsp1 = wpf->entity.vsp1;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030079 const struct v4l2_rect *crop = &wpf->crop;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030080 unsigned int i;
81 u32 srcrpf = 0;
82 u32 outfmt = 0;
Laurent Pinchart7578c202014-05-21 19:00:05 -030083 int ret;
84
85 ret = vsp1_entity_set_streaming(&wpf->entity, enable);
86 if (ret < 0)
87 return ret;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030088
89 if (!enable) {
90 vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
Laurent Pinchartd6c71e82014-05-28 13:10:33 -030091 vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, 0);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030092 return 0;
93 }
94
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030095 /* Sources. If the pipeline has a single input configure it as the
96 * master layer. Otherwise configure all inputs as sub-layers and
97 * select the virtual RPF as the master layer.
98 */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030099 for (i = 0; i < pipe->num_inputs; ++i) {
100 struct vsp1_rwpf *input = pipe->inputs[i];
101
Laurent Pinchart629bb6d2013-07-10 18:03:46 -0300102 srcrpf |= pipe->num_inputs == 1
103 ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
104 : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300105 }
106
Laurent Pinchart629bb6d2013-07-10 18:03:46 -0300107 if (pipe->num_inputs > 1)
108 srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
109
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300110 vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf);
111
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300112 /* Destination stride. */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300113 if (!pipe->lif) {
114 struct v4l2_pix_format_mplane *format = &wpf->video.format;
115
116 vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_Y,
117 format->plane_fmt[0].bytesperline);
118 if (format->num_planes > 1)
119 vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_C,
120 format->plane_fmt[1].bytesperline);
121 }
122
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300123 vsp1_wpf_write(wpf, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
124 (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) |
125 (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT));
126 vsp1_wpf_write(wpf, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
127 (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) |
128 (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT));
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300129
130 /* Format */
131 if (!pipe->lif) {
132 const struct vsp1_format_info *fmtinfo = wpf->video.fmtinfo;
133
134 outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
135
Laurent Pinchart7a52b6d2014-05-26 20:12:53 -0300136 if (fmtinfo->alpha)
137 outfmt |= VI6_WPF_OUTFMT_PXA;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300138 if (fmtinfo->swap_yc)
139 outfmt |= VI6_WPF_OUTFMT_SPYCS;
140 if (fmtinfo->swap_uv)
141 outfmt |= VI6_WPF_OUTFMT_SPUVS;
142
143 vsp1_wpf_write(wpf, VI6_WPF_DSWAP, fmtinfo->swap);
144 }
145
146 if (wpf->entity.formats[RWPF_PAD_SINK].code !=
147 wpf->entity.formats[RWPF_PAD_SOURCE].code)
148 outfmt |= VI6_WPF_OUTFMT_CSC;
149
Laurent Pinchart7578c202014-05-21 19:00:05 -0300150 /* Take the control handler lock to ensure that the PDV value won't be
151 * changed behind our back by a set control operation.
152 */
153 mutex_lock(wpf->ctrls.lock);
154 outfmt |= vsp1_wpf_read(wpf, VI6_WPF_OUTFMT) & VI6_WPF_OUTFMT_PDV_MASK;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300155 vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, outfmt);
Laurent Pinchart7578c202014-05-21 19:00:05 -0300156 mutex_unlock(wpf->ctrls.lock);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300157
158 vsp1_write(vsp1, VI6_DPR_WPF_FPORCH(wpf->entity.index),
159 VI6_DPR_WPF_FPORCH_FP_WPFN);
160
161 vsp1_write(vsp1, VI6_WPF_WRBCK_CTRL, 0);
162
163 /* Enable interrupts */
164 vsp1_write(vsp1, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
165 vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index),
166 VI6_WFP_IRQ_ENB_FREE);
167
168 return 0;
169}
170
171/* -----------------------------------------------------------------------------
172 * V4L2 Subdevice Operations
173 */
174
175static struct v4l2_subdev_video_ops wpf_video_ops = {
176 .s_stream = wpf_s_stream,
177};
178
179static struct v4l2_subdev_pad_ops wpf_pad_ops = {
180 .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
181 .enum_frame_size = vsp1_rwpf_enum_frame_size,
182 .get_fmt = vsp1_rwpf_get_format,
183 .set_fmt = vsp1_rwpf_set_format,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300184 .get_selection = vsp1_rwpf_get_selection,
185 .set_selection = vsp1_rwpf_set_selection,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300186};
187
188static struct v4l2_subdev_ops wpf_ops = {
189 .video = &wpf_video_ops,
190 .pad = &wpf_pad_ops,
191};
192
193/* -----------------------------------------------------------------------------
194 * Video Device Operations
195 */
196
197static void wpf_vdev_queue(struct vsp1_video *video,
198 struct vsp1_video_buffer *buf)
199{
200 struct vsp1_rwpf *wpf = container_of(video, struct vsp1_rwpf, video);
201
202 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_Y, buf->addr[0]);
203 if (buf->buf.num_planes > 1)
204 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C0, buf->addr[1]);
205 if (buf->buf.num_planes > 2)
206 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C1, buf->addr[2]);
207}
208
209static const struct vsp1_video_operations wpf_vdev_ops = {
210 .queue = wpf_vdev_queue,
211};
212
213/* -----------------------------------------------------------------------------
214 * Initialization and Cleanup
215 */
216
217struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
218{
219 struct v4l2_subdev *subdev;
220 struct vsp1_video *video;
221 struct vsp1_rwpf *wpf;
222 unsigned int flags;
223 int ret;
224
225 wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
226 if (wpf == NULL)
227 return ERR_PTR(-ENOMEM);
228
229 wpf->max_width = WPF_MAX_WIDTH;
230 wpf->max_height = WPF_MAX_HEIGHT;
231
232 wpf->entity.type = VSP1_ENTITY_WPF;
233 wpf->entity.index = index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300234
235 ret = vsp1_entity_init(vsp1, &wpf->entity, 2);
236 if (ret < 0)
237 return ERR_PTR(ret);
238
239 /* Initialize the V4L2 subdev. */
240 subdev = &wpf->entity.subdev;
241 v4l2_subdev_init(subdev, &wpf_ops);
242
243 subdev->entity.ops = &vsp1_media_ops;
244 subdev->internal_ops = &vsp1_subdev_internal_ops;
245 snprintf(subdev->name, sizeof(subdev->name), "%s wpf.%u",
246 dev_name(vsp1->dev), index);
247 v4l2_set_subdevdata(subdev, wpf);
248 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
249
250 vsp1_entity_init_formats(subdev, NULL);
251
Laurent Pinchart7578c202014-05-21 19:00:05 -0300252 /* Initialize the control handler. */
253 v4l2_ctrl_handler_init(&wpf->ctrls, 1);
254 v4l2_ctrl_new_std(&wpf->ctrls, &wpf_ctrl_ops, V4L2_CID_ALPHA_COMPONENT,
255 0, 255, 1, 255);
256
257 wpf->entity.subdev.ctrl_handler = &wpf->ctrls;
258
259 if (wpf->ctrls.error) {
260 dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
261 index);
262 ret = wpf->ctrls.error;
263 goto error;
264 }
265
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300266 /* Initialize the video device. */
267 video = &wpf->video;
268
269 video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
270 video->vsp1 = vsp1;
271 video->ops = &wpf_vdev_ops;
272
273 ret = vsp1_video_init(video, &wpf->entity);
274 if (ret < 0)
Laurent Pinchart1499be62014-05-28 12:49:13 -0300275 goto error;
276
277 wpf->entity.video = video;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300278
279 /* Connect the video device to the WPF. All connections are immutable
280 * except for the WPF0 source link if a LIF is present.
281 */
282 flags = MEDIA_LNK_FL_ENABLED;
283 if (!(vsp1->pdata->features & VSP1_HAS_LIF) || index != 0)
284 flags |= MEDIA_LNK_FL_IMMUTABLE;
285
286 ret = media_entity_create_link(&wpf->entity.subdev.entity,
287 RWPF_PAD_SOURCE,
288 &wpf->video.video.entity, 0, flags);
289 if (ret < 0)
Laurent Pinchart1499be62014-05-28 12:49:13 -0300290 goto error;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300291
292 wpf->entity.sink = &wpf->video.video.entity;
293
294 return wpf;
295
Laurent Pinchart1499be62014-05-28 12:49:13 -0300296error:
297 vsp1_entity_destroy(&wpf->entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300298 return ERR_PTR(ret);
299}