Merge drm-fixes into drm-next.
[deliverable/linux.git] / drivers / media / platform / vsp1 / vsp1_rpf.c
CommitLineData
26e0ca22
LP
1/*
2 * vsp1_rpf.c -- R-Car VSP1 Read Pixel Formatter
3 *
8a1edc55 4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
26e0ca22
LP
5 *
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 RPF_MAX_WIDTH 8190
23#define RPF_MAX_HEIGHT 8190
24
25/* -----------------------------------------------------------------------------
26 * Device Access
27 */
28
26e0ca22
LP
29static inline void vsp1_rpf_write(struct vsp1_rwpf *rpf, u32 reg, u32 data)
30{
1517b039
TS
31 vsp1_mod_write(&rpf->entity, reg + rpf->entity.index * VI6_RPF_OFFSET,
32 data);
26e0ca22
LP
33}
34
7578c204
LP
35/* -----------------------------------------------------------------------------
36 * Controls
37 */
38
39static int rpf_s_ctrl(struct v4l2_ctrl *ctrl)
40{
41 struct vsp1_rwpf *rpf =
42 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
bdc2df62 43 struct vsp1_pipeline *pipe;
7578c204
LP
44
45 if (!vsp1_entity_is_streaming(&rpf->entity))
46 return 0;
47
48 switch (ctrl->id) {
49 case V4L2_CID_ALPHA_COMPONENT:
50 vsp1_rpf_write(rpf, VI6_RPF_VRTCOL_SET,
51 ctrl->val << VI6_RPF_VRTCOL_SET_LAYA_SHIFT);
bdc2df62
LP
52
53 pipe = to_vsp1_pipeline(&rpf->entity.subdev.entity);
54 vsp1_pipeline_propagate_alpha(pipe, &rpf->entity, ctrl->val);
7578c204
LP
55 break;
56 }
57
58 return 0;
59}
60
61static const struct v4l2_ctrl_ops rpf_ctrl_ops = {
62 .s_ctrl = rpf_s_ctrl,
63};
64
26e0ca22
LP
65/* -----------------------------------------------------------------------------
66 * V4L2 Subdevice Core Operations
67 */
68
69static int rpf_s_stream(struct v4l2_subdev *subdev, int enable)
70{
3dbb6100 71 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&subdev->entity);
26e0ca22 72 struct vsp1_rwpf *rpf = to_rwpf(subdev);
3dbb6100 73 struct vsp1_device *vsp1 = rpf->entity.vsp1;
86960eec
LP
74 const struct vsp1_format_info *fmtinfo = rpf->fmtinfo;
75 const struct v4l2_pix_format_mplane *format = &rpf->format;
e5ad37b6 76 const struct v4l2_rect *crop = &rpf->crop;
26e0ca22
LP
77 u32 pstride;
78 u32 infmt;
7578c204
LP
79 int ret;
80
81 ret = vsp1_entity_set_streaming(&rpf->entity, enable);
82 if (ret < 0)
83 return ret;
26e0ca22
LP
84
85 if (!enable)
86 return 0;
87
e5ad37b6
LP
88 /* Source size, stride and crop offsets.
89 *
90 * The crop offsets correspond to the location of the crop rectangle top
91 * left corner in the plane buffer. Only two offsets are needed, as
92 * planes 2 and 3 always have identical strides.
93 */
26e0ca22 94 vsp1_rpf_write(rpf, VI6_RPF_SRC_BSIZE,
e5ad37b6
LP
95 (crop->width << VI6_RPF_SRC_BSIZE_BHSIZE_SHIFT) |
96 (crop->height << VI6_RPF_SRC_BSIZE_BVSIZE_SHIFT));
26e0ca22 97 vsp1_rpf_write(rpf, VI6_RPF_SRC_ESIZE,
e5ad37b6
LP
98 (crop->width << VI6_RPF_SRC_ESIZE_EHSIZE_SHIFT) |
99 (crop->height << VI6_RPF_SRC_ESIZE_EVSIZE_SHIFT));
26e0ca22 100
e5ad37b6
LP
101 rpf->offsets[0] = crop->top * format->plane_fmt[0].bytesperline
102 + crop->left * fmtinfo->bpp[0] / 8;
26e0ca22
LP
103 pstride = format->plane_fmt[0].bytesperline
104 << VI6_RPF_SRCM_PSTRIDE_Y_SHIFT;
857161fc
TH
105
106 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_Y,
107 rpf->buf_addr[0] + rpf->offsets[0]);
108
e5ad37b6
LP
109 if (format->num_planes > 1) {
110 rpf->offsets[1] = crop->top * format->plane_fmt[1].bytesperline
111 + crop->left * fmtinfo->bpp[1] / 8;
26e0ca22
LP
112 pstride |= format->plane_fmt[1].bytesperline
113 << VI6_RPF_SRCM_PSTRIDE_C_SHIFT;
857161fc
TH
114
115 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C0,
116 rpf->buf_addr[1] + rpf->offsets[1]);
117
118 if (format->num_planes > 2)
119 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C1,
120 rpf->buf_addr[2] + rpf->offsets[1]);
e5ad37b6 121 }
26e0ca22
LP
122
123 vsp1_rpf_write(rpf, VI6_RPF_SRCM_PSTRIDE, pstride);
124
125 /* Format */
126 infmt = VI6_RPF_INFMT_CIPM
127 | (fmtinfo->hwfmt << VI6_RPF_INFMT_RDFMT_SHIFT);
128
129 if (fmtinfo->swap_yc)
130 infmt |= VI6_RPF_INFMT_SPYCS;
131 if (fmtinfo->swap_uv)
132 infmt |= VI6_RPF_INFMT_SPUVS;
133
134 if (rpf->entity.formats[RWPF_PAD_SINK].code !=
135 rpf->entity.formats[RWPF_PAD_SOURCE].code)
136 infmt |= VI6_RPF_INFMT_CSC;
137
138 vsp1_rpf_write(rpf, VI6_RPF_INFMT, infmt);
139 vsp1_rpf_write(rpf, VI6_RPF_DSWAP, fmtinfo->swap);
140
629bb6d4
LP
141 /* Output location */
142 vsp1_rpf_write(rpf, VI6_RPF_LOC,
143 (rpf->location.left << VI6_RPF_LOC_HCOORD_SHIFT) |
144 (rpf->location.top << VI6_RPF_LOC_VCOORD_SHIFT));
26e0ca22 145
7578c204
LP
146 /* Use the alpha channel (extended to 8 bits) when available or an
147 * alpha value set through the V4L2_CID_ALPHA_COMPONENT control
148 * otherwise. Disable color keying.
26e0ca22 149 */
7a52b6de
LP
150 vsp1_rpf_write(rpf, VI6_RPF_ALPH_SEL, VI6_RPF_ALPH_SEL_AEXT_EXT |
151 (fmtinfo->alpha ? VI6_RPF_ALPH_SEL_ASEL_PACKED
152 : VI6_RPF_ALPH_SEL_ASEL_FIXED));
3dbb6100 153
5aa2eb3c 154 if (vsp1->info->uapi)
3dbb6100
LP
155 mutex_lock(rpf->ctrls.lock);
156 vsp1_rpf_write(rpf, VI6_RPF_VRTCOL_SET,
157 rpf->alpha->cur.val << VI6_RPF_VRTCOL_SET_LAYA_SHIFT);
158 vsp1_pipeline_propagate_alpha(pipe, &rpf->entity, rpf->alpha->cur.val);
5aa2eb3c 159 if (vsp1->info->uapi)
3dbb6100
LP
160 mutex_unlock(rpf->ctrls.lock);
161
26e0ca22
LP
162 vsp1_rpf_write(rpf, VI6_RPF_MSK_CTRL, 0);
163 vsp1_rpf_write(rpf, VI6_RPF_CKEY_CTRL, 0);
164
165 return 0;
166}
167
168/* -----------------------------------------------------------------------------
169 * V4L2 Subdevice Operations
170 */
171
172static struct v4l2_subdev_video_ops rpf_video_ops = {
173 .s_stream = rpf_s_stream,
174};
175
176static struct v4l2_subdev_pad_ops rpf_pad_ops = {
177 .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
178 .enum_frame_size = vsp1_rwpf_enum_frame_size,
179 .get_fmt = vsp1_rwpf_get_format,
180 .set_fmt = vsp1_rwpf_set_format,
e5ad37b6
LP
181 .get_selection = vsp1_rwpf_get_selection,
182 .set_selection = vsp1_rwpf_set_selection,
26e0ca22
LP
183};
184
185static struct v4l2_subdev_ops rpf_ops = {
186 .video = &rpf_video_ops,
187 .pad = &rpf_pad_ops,
188};
189
190/* -----------------------------------------------------------------------------
191 * Video Device Operations
192 */
193
b58faa95 194static void rpf_set_memory(struct vsp1_rwpf *rpf, struct vsp1_rwpf_memory *mem)
26e0ca22 195{
857161fc
TH
196 unsigned int i;
197
198 for (i = 0; i < 3; ++i)
b58faa95 199 rpf->buf_addr[i] = mem->addr[i];
857161fc
TH
200
201 if (!vsp1_entity_is_streaming(&rpf->entity))
202 return;
26e0ca22 203
e5ad37b6 204 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_Y,
b58faa95
LP
205 mem->addr[0] + rpf->offsets[0]);
206 if (mem->num_planes > 1)
e5ad37b6 207 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C0,
b58faa95
LP
208 mem->addr[1] + rpf->offsets[1]);
209 if (mem->num_planes > 2)
e5ad37b6 210 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C1,
b58faa95 211 mem->addr[2] + rpf->offsets[1]);
26e0ca22
LP
212}
213
b6af10cd 214static const struct vsp1_rwpf_operations rpf_vdev_ops = {
b58faa95 215 .set_memory = rpf_set_memory,
26e0ca22
LP
216};
217
218/* -----------------------------------------------------------------------------
219 * Initialization and Cleanup
220 */
221
222struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
223{
224 struct v4l2_subdev *subdev;
26e0ca22
LP
225 struct vsp1_rwpf *rpf;
226 int ret;
227
228 rpf = devm_kzalloc(vsp1->dev, sizeof(*rpf), GFP_KERNEL);
229 if (rpf == NULL)
230 return ERR_PTR(-ENOMEM);
231
b6af10cd
LP
232 rpf->ops = &rpf_vdev_ops;
233
26e0ca22
LP
234 rpf->max_width = RPF_MAX_WIDTH;
235 rpf->max_height = RPF_MAX_HEIGHT;
236
237 rpf->entity.type = VSP1_ENTITY_RPF;
238 rpf->entity.index = index;
26e0ca22
LP
239
240 ret = vsp1_entity_init(vsp1, &rpf->entity, 2);
241 if (ret < 0)
242 return ERR_PTR(ret);
243
244 /* Initialize the V4L2 subdev. */
245 subdev = &rpf->entity.subdev;
246 v4l2_subdev_init(subdev, &rpf_ops);
247
babca007 248 subdev->entity.ops = &vsp1->media_ops;
26e0ca22
LP
249 subdev->internal_ops = &vsp1_subdev_internal_ops;
250 snprintf(subdev->name, sizeof(subdev->name), "%s rpf.%u",
251 dev_name(vsp1->dev), index);
252 v4l2_set_subdevdata(subdev, rpf);
253 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
254
255 vsp1_entity_init_formats(subdev, NULL);
256
7578c204
LP
257 /* Initialize the control handler. */
258 v4l2_ctrl_handler_init(&rpf->ctrls, 1);
3dbb6100
LP
259 rpf->alpha = v4l2_ctrl_new_std(&rpf->ctrls, &rpf_ctrl_ops,
260 V4L2_CID_ALPHA_COMPONENT,
261 0, 255, 1, 255);
7578c204
LP
262
263 rpf->entity.subdev.ctrl_handler = &rpf->ctrls;
264
265 if (rpf->ctrls.error) {
266 dev_err(vsp1->dev, "rpf%u: failed to initialize controls\n",
267 index);
268 ret = rpf->ctrls.error;
269 goto error;
270 }
271
26e0ca22
LP
272 return rpf;
273
1499be67
LP
274error:
275 vsp1_entity_destroy(&rpf->entity);
26e0ca22
LP
276 return ERR_PTR(ret);
277}
This page took 0.143008 seconds and 5 git commands to generate.