blob: d0edcde721bd7443cc759085bd2de1c923413801 [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
Takanari Hayama5d0beee2014-11-26 22:25:02 -030095 /* Sources. If the pipeline has a single input and BRU is not used,
96 * configure it as the master layer. Otherwise configure all
97 * inputs as sub-layers and select the virtual RPF as the master
98 * layer.
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030099 */
Laurent Pinchart96bfa6a2015-08-05 16:40:31 -0300100 for (i = 0; i < vsp1->pdata.rpf_count; ++i) {
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300101 struct vsp1_rwpf *input = pipe->inputs[i];
102
Laurent Pinchart96bfa6a2015-08-05 16:40:31 -0300103 if (!input)
104 continue;
105
Takanari Hayama5d0beee2014-11-26 22:25:02 -0300106 srcrpf |= (!pipe->bru && pipe->num_inputs == 1)
Laurent Pinchart629bb6d2013-07-10 18:03:46 -0300107 ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
108 : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300109 }
110
Takanari Hayama5d0beee2014-11-26 22:25:02 -0300111 if (pipe->bru || pipe->num_inputs > 1)
Laurent Pinchart629bb6d2013-07-10 18:03:46 -0300112 srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
113
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300114 vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf);
115
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300116 /* Destination stride. */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300117 if (!pipe->lif) {
Laurent Pinchart86960ee2015-07-28 14:00:43 -0300118 struct v4l2_pix_format_mplane *format = &wpf->format;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300119
120 vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_Y,
121 format->plane_fmt[0].bytesperline);
122 if (format->num_planes > 1)
123 vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_C,
124 format->plane_fmt[1].bytesperline);
125 }
126
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300127 vsp1_wpf_write(wpf, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
128 (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) |
129 (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT));
130 vsp1_wpf_write(wpf, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
131 (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) |
132 (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT));
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300133
134 /* Format */
135 if (!pipe->lif) {
Laurent Pinchart86960ee2015-07-28 14:00:43 -0300136 const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300137
138 outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
139
Laurent Pinchart7a52b6d2014-05-26 20:12:53 -0300140 if (fmtinfo->alpha)
141 outfmt |= VI6_WPF_OUTFMT_PXA;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300142 if (fmtinfo->swap_yc)
143 outfmt |= VI6_WPF_OUTFMT_SPYCS;
144 if (fmtinfo->swap_uv)
145 outfmt |= VI6_WPF_OUTFMT_SPUVS;
146
147 vsp1_wpf_write(wpf, VI6_WPF_DSWAP, fmtinfo->swap);
148 }
149
150 if (wpf->entity.formats[RWPF_PAD_SINK].code !=
151 wpf->entity.formats[RWPF_PAD_SOURCE].code)
152 outfmt |= VI6_WPF_OUTFMT_CSC;
153
Laurent Pinchart7578c202014-05-21 19:00:05 -0300154 /* Take the control handler lock to ensure that the PDV value won't be
155 * changed behind our back by a set control operation.
156 */
Laurent Pinchartf2ed4592015-07-28 16:16:05 -0300157 if (vsp1->pdata.uapi)
158 mutex_lock(wpf->ctrls.lock);
Laurent Pinchart7578c202014-05-21 19:00:05 -0300159 outfmt |= vsp1_wpf_read(wpf, VI6_WPF_OUTFMT) & VI6_WPF_OUTFMT_PDV_MASK;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300160 vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, outfmt);
Laurent Pinchartf2ed4592015-07-28 16:16:05 -0300161 if (vsp1->pdata.uapi)
162 mutex_unlock(wpf->ctrls.lock);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300163
164 vsp1_write(vsp1, VI6_DPR_WPF_FPORCH(wpf->entity.index),
165 VI6_DPR_WPF_FPORCH_FP_WPFN);
166
167 vsp1_write(vsp1, VI6_WPF_WRBCK_CTRL, 0);
168
169 /* Enable interrupts */
170 vsp1_write(vsp1, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
171 vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index),
172 VI6_WFP_IRQ_ENB_FREE);
173
174 return 0;
175}
176
177/* -----------------------------------------------------------------------------
178 * V4L2 Subdevice Operations
179 */
180
181static struct v4l2_subdev_video_ops wpf_video_ops = {
182 .s_stream = wpf_s_stream,
183};
184
185static struct v4l2_subdev_pad_ops wpf_pad_ops = {
186 .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
187 .enum_frame_size = vsp1_rwpf_enum_frame_size,
188 .get_fmt = vsp1_rwpf_get_format,
189 .set_fmt = vsp1_rwpf_set_format,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300190 .get_selection = vsp1_rwpf_get_selection,
191 .set_selection = vsp1_rwpf_set_selection,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300192};
193
194static struct v4l2_subdev_ops wpf_ops = {
195 .video = &wpf_video_ops,
196 .pad = &wpf_pad_ops,
197};
198
199/* -----------------------------------------------------------------------------
200 * Video Device Operations
201 */
202
Laurent Pinchartb58faa92015-07-28 16:04:47 -0300203static void wpf_set_memory(struct vsp1_rwpf *wpf, struct vsp1_rwpf_memory *mem)
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300204{
Laurent Pinchartb58faa92015-07-28 16:04:47 -0300205 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_Y, mem->addr[0]);
206 if (mem->num_planes > 1)
207 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C0, mem->addr[1]);
208 if (mem->num_planes > 2)
209 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C1, mem->addr[2]);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300210}
211
Laurent Pinchartb6af10c2015-07-28 14:05:56 -0300212static const struct vsp1_rwpf_operations wpf_vdev_ops = {
Laurent Pinchartb58faa92015-07-28 16:04:47 -0300213 .set_memory = wpf_set_memory,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300214};
215
216/* -----------------------------------------------------------------------------
217 * Initialization and Cleanup
218 */
219
220struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
221{
222 struct v4l2_subdev *subdev;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300223 struct vsp1_rwpf *wpf;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300224 int ret;
225
226 wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
227 if (wpf == NULL)
228 return ERR_PTR(-ENOMEM);
229
Laurent Pinchartb6af10c2015-07-28 14:05:56 -0300230 wpf->ops = &wpf_vdev_ops;
231
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300232 wpf->max_width = WPF_MAX_WIDTH;
233 wpf->max_height = WPF_MAX_HEIGHT;
234
235 wpf->entity.type = VSP1_ENTITY_WPF;
236 wpf->entity.index = index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300237
238 ret = vsp1_entity_init(vsp1, &wpf->entity, 2);
239 if (ret < 0)
240 return ERR_PTR(ret);
241
242 /* Initialize the V4L2 subdev. */
243 subdev = &wpf->entity.subdev;
244 v4l2_subdev_init(subdev, &wpf_ops);
245
246 subdev->entity.ops = &vsp1_media_ops;
247 subdev->internal_ops = &vsp1_subdev_internal_ops;
248 snprintf(subdev->name, sizeof(subdev->name), "%s wpf.%u",
249 dev_name(vsp1->dev), index);
250 v4l2_set_subdevdata(subdev, wpf);
251 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
252
253 vsp1_entity_init_formats(subdev, NULL);
254
Laurent Pinchart7578c202014-05-21 19:00:05 -0300255 /* Initialize the control handler. */
256 v4l2_ctrl_handler_init(&wpf->ctrls, 1);
257 v4l2_ctrl_new_std(&wpf->ctrls, &wpf_ctrl_ops, V4L2_CID_ALPHA_COMPONENT,
258 0, 255, 1, 255);
259
260 wpf->entity.subdev.ctrl_handler = &wpf->ctrls;
261
262 if (wpf->ctrls.error) {
263 dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
264 index);
265 ret = wpf->ctrls.error;
266 goto error;
267 }
268
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300269 return wpf;
270
Laurent Pinchart1499be62014-05-28 12:49:13 -0300271error:
272 vsp1_entity_destroy(&wpf->entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300273 return ERR_PTR(ret);
274}