blob: c3d98642a4aa692010b43a6b6b3d9c934c0735e4 [file] [log] [blame]
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03001/*
2 * vsp1_rpf.c -- R-Car VSP1 Read 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 RPF_MAX_WIDTH 8190
23#define RPF_MAX_HEIGHT 8190
24
25/* -----------------------------------------------------------------------------
26 * Device Access
27 */
28
29static inline u32 vsp1_rpf_read(struct vsp1_rwpf *rpf, u32 reg)
30{
31 return vsp1_read(rpf->entity.vsp1,
32 reg + rpf->entity.index * VI6_RPF_OFFSET);
33}
34
35static inline void vsp1_rpf_write(struct vsp1_rwpf *rpf, u32 reg, u32 data)
36{
37 vsp1_write(rpf->entity.vsp1,
38 reg + rpf->entity.index * VI6_RPF_OFFSET, data);
39}
40
41/* -----------------------------------------------------------------------------
42 * V4L2 Subdevice Core Operations
43 */
44
45static int rpf_s_stream(struct v4l2_subdev *subdev, int enable)
46{
47 struct vsp1_rwpf *rpf = to_rwpf(subdev);
48 const struct vsp1_format_info *fmtinfo = rpf->video.fmtinfo;
49 const struct v4l2_pix_format_mplane *format = &rpf->video.format;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030050 const struct v4l2_rect *crop = &rpf->crop;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030051 u32 pstride;
52 u32 infmt;
53
54 if (!enable)
55 return 0;
56
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030057 /* Source size, stride and crop offsets.
58 *
59 * The crop offsets correspond to the location of the crop rectangle top
60 * left corner in the plane buffer. Only two offsets are needed, as
61 * planes 2 and 3 always have identical strides.
62 */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030063 vsp1_rpf_write(rpf, VI6_RPF_SRC_BSIZE,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030064 (crop->width << VI6_RPF_SRC_BSIZE_BHSIZE_SHIFT) |
65 (crop->height << VI6_RPF_SRC_BSIZE_BVSIZE_SHIFT));
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030066 vsp1_rpf_write(rpf, VI6_RPF_SRC_ESIZE,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030067 (crop->width << VI6_RPF_SRC_ESIZE_EHSIZE_SHIFT) |
68 (crop->height << VI6_RPF_SRC_ESIZE_EVSIZE_SHIFT));
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030069
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030070 rpf->offsets[0] = crop->top * format->plane_fmt[0].bytesperline
71 + crop->left * fmtinfo->bpp[0] / 8;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030072 pstride = format->plane_fmt[0].bytesperline
73 << VI6_RPF_SRCM_PSTRIDE_Y_SHIFT;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030074 if (format->num_planes > 1) {
75 rpf->offsets[1] = crop->top * format->plane_fmt[1].bytesperline
76 + crop->left * fmtinfo->bpp[1] / 8;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030077 pstride |= format->plane_fmt[1].bytesperline
78 << VI6_RPF_SRCM_PSTRIDE_C_SHIFT;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030079 }
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030080
81 vsp1_rpf_write(rpf, VI6_RPF_SRCM_PSTRIDE, pstride);
82
83 /* Format */
84 infmt = VI6_RPF_INFMT_CIPM
85 | (fmtinfo->hwfmt << VI6_RPF_INFMT_RDFMT_SHIFT);
86
87 if (fmtinfo->swap_yc)
88 infmt |= VI6_RPF_INFMT_SPYCS;
89 if (fmtinfo->swap_uv)
90 infmt |= VI6_RPF_INFMT_SPUVS;
91
92 if (rpf->entity.formats[RWPF_PAD_SINK].code !=
93 rpf->entity.formats[RWPF_PAD_SOURCE].code)
94 infmt |= VI6_RPF_INFMT_CSC;
95
96 vsp1_rpf_write(rpf, VI6_RPF_INFMT, infmt);
97 vsp1_rpf_write(rpf, VI6_RPF_DSWAP, fmtinfo->swap);
98
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030099 /* Output location */
100 vsp1_rpf_write(rpf, VI6_RPF_LOC,
101 (rpf->location.left << VI6_RPF_LOC_HCOORD_SHIFT) |
102 (rpf->location.top << VI6_RPF_LOC_VCOORD_SHIFT));
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300103
104 /* Disable alpha, mask and color key. Set the alpha channel to a fixed
105 * value of 255.
106 */
107 vsp1_rpf_write(rpf, VI6_RPF_ALPH_SEL, VI6_RPF_ALPH_SEL_ASEL_FIXED);
108 vsp1_rpf_write(rpf, VI6_RPF_VRTCOL_SET,
109 255 << VI6_RPF_VRTCOL_SET_LAYA_SHIFT);
110 vsp1_rpf_write(rpf, VI6_RPF_MSK_CTRL, 0);
111 vsp1_rpf_write(rpf, VI6_RPF_CKEY_CTRL, 0);
112
113 return 0;
114}
115
116/* -----------------------------------------------------------------------------
117 * V4L2 Subdevice Operations
118 */
119
120static struct v4l2_subdev_video_ops rpf_video_ops = {
121 .s_stream = rpf_s_stream,
122};
123
124static struct v4l2_subdev_pad_ops rpf_pad_ops = {
125 .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
126 .enum_frame_size = vsp1_rwpf_enum_frame_size,
127 .get_fmt = vsp1_rwpf_get_format,
128 .set_fmt = vsp1_rwpf_set_format,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300129 .get_selection = vsp1_rwpf_get_selection,
130 .set_selection = vsp1_rwpf_set_selection,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300131};
132
133static struct v4l2_subdev_ops rpf_ops = {
134 .video = &rpf_video_ops,
135 .pad = &rpf_pad_ops,
136};
137
138/* -----------------------------------------------------------------------------
139 * Video Device Operations
140 */
141
142static void rpf_vdev_queue(struct vsp1_video *video,
143 struct vsp1_video_buffer *buf)
144{
145 struct vsp1_rwpf *rpf = container_of(video, struct vsp1_rwpf, video);
146
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300147 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_Y,
148 buf->addr[0] + rpf->offsets[0]);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300149 if (buf->buf.num_planes > 1)
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300150 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C0,
151 buf->addr[1] + rpf->offsets[1]);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300152 if (buf->buf.num_planes > 2)
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300153 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C1,
154 buf->addr[2] + rpf->offsets[1]);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300155}
156
157static const struct vsp1_video_operations rpf_vdev_ops = {
158 .queue = rpf_vdev_queue,
159};
160
161/* -----------------------------------------------------------------------------
162 * Initialization and Cleanup
163 */
164
165struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
166{
167 struct v4l2_subdev *subdev;
168 struct vsp1_video *video;
169 struct vsp1_rwpf *rpf;
170 int ret;
171
172 rpf = devm_kzalloc(vsp1->dev, sizeof(*rpf), GFP_KERNEL);
173 if (rpf == NULL)
174 return ERR_PTR(-ENOMEM);
175
176 rpf->max_width = RPF_MAX_WIDTH;
177 rpf->max_height = RPF_MAX_HEIGHT;
178
179 rpf->entity.type = VSP1_ENTITY_RPF;
180 rpf->entity.index = index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300181
182 ret = vsp1_entity_init(vsp1, &rpf->entity, 2);
183 if (ret < 0)
184 return ERR_PTR(ret);
185
186 /* Initialize the V4L2 subdev. */
187 subdev = &rpf->entity.subdev;
188 v4l2_subdev_init(subdev, &rpf_ops);
189
190 subdev->entity.ops = &vsp1_media_ops;
191 subdev->internal_ops = &vsp1_subdev_internal_ops;
192 snprintf(subdev->name, sizeof(subdev->name), "%s rpf.%u",
193 dev_name(vsp1->dev), index);
194 v4l2_set_subdevdata(subdev, rpf);
195 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
196
197 vsp1_entity_init_formats(subdev, NULL);
198
199 /* Initialize the video device. */
200 video = &rpf->video;
201
202 video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
203 video->vsp1 = vsp1;
204 video->ops = &rpf_vdev_ops;
205
206 ret = vsp1_video_init(video, &rpf->entity);
207 if (ret < 0)
208 goto error_video;
209
210 /* Connect the video device to the RPF. */
211 ret = media_entity_create_link(&rpf->video.video.entity, 0,
212 &rpf->entity.subdev.entity,
213 RWPF_PAD_SINK,
214 MEDIA_LNK_FL_ENABLED |
215 MEDIA_LNK_FL_IMMUTABLE);
216 if (ret < 0)
217 goto error_link;
218
219 return rpf;
220
221error_link:
222 vsp1_video_cleanup(video);
223error_video:
224 media_entity_cleanup(&rpf->entity.subdev.entity);
225 return ERR_PTR(ret);
226}