blob: 591f09ca3affd8344849befe666877c43cf05759 [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/* -----------------------------------------------------------------------------
42 * V4L2 Subdevice Core Operations
43 */
44
45static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
46{
Laurent Pinchart5aeb01ad2014-05-27 20:35:36 -030047 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&subdev->entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030048 struct vsp1_rwpf *wpf = to_rwpf(subdev);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030049 struct vsp1_device *vsp1 = wpf->entity.vsp1;
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030050 const struct v4l2_rect *crop = &wpf->crop;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030051 unsigned int i;
52 u32 srcrpf = 0;
53 u32 outfmt = 0;
54
55 if (!enable) {
56 vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
57 return 0;
58 }
59
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030060 /* Sources. If the pipeline has a single input configure it as the
61 * master layer. Otherwise configure all inputs as sub-layers and
62 * select the virtual RPF as the master layer.
63 */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030064 for (i = 0; i < pipe->num_inputs; ++i) {
65 struct vsp1_rwpf *input = pipe->inputs[i];
66
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030067 srcrpf |= pipe->num_inputs == 1
68 ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
69 : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030070 }
71
Laurent Pinchart629bb6d2013-07-10 18:03:46 -030072 if (pipe->num_inputs > 1)
73 srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
74
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030075 vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf);
76
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030077 /* Destination stride. */
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030078 if (!pipe->lif) {
79 struct v4l2_pix_format_mplane *format = &wpf->video.format;
80
81 vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_Y,
82 format->plane_fmt[0].bytesperline);
83 if (format->num_planes > 1)
84 vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_C,
85 format->plane_fmt[1].bytesperline);
86 }
87
Laurent Pincharte5ad37b2013-08-24 20:49:58 -030088 vsp1_wpf_write(wpf, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
89 (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) |
90 (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT));
91 vsp1_wpf_write(wpf, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
92 (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) |
93 (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT));
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030094
95 /* Format */
96 if (!pipe->lif) {
97 const struct vsp1_format_info *fmtinfo = wpf->video.fmtinfo;
98
99 outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
100
101 if (fmtinfo->swap_yc)
102 outfmt |= VI6_WPF_OUTFMT_SPYCS;
103 if (fmtinfo->swap_uv)
104 outfmt |= VI6_WPF_OUTFMT_SPUVS;
105
106 vsp1_wpf_write(wpf, VI6_WPF_DSWAP, fmtinfo->swap);
107 }
108
109 if (wpf->entity.formats[RWPF_PAD_SINK].code !=
110 wpf->entity.formats[RWPF_PAD_SOURCE].code)
111 outfmt |= VI6_WPF_OUTFMT_CSC;
112
113 vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, outfmt);
114
115 vsp1_write(vsp1, VI6_DPR_WPF_FPORCH(wpf->entity.index),
116 VI6_DPR_WPF_FPORCH_FP_WPFN);
117
118 vsp1_write(vsp1, VI6_WPF_WRBCK_CTRL, 0);
119
120 /* Enable interrupts */
121 vsp1_write(vsp1, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
122 vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index),
123 VI6_WFP_IRQ_ENB_FREE);
124
125 return 0;
126}
127
128/* -----------------------------------------------------------------------------
129 * V4L2 Subdevice Operations
130 */
131
132static struct v4l2_subdev_video_ops wpf_video_ops = {
133 .s_stream = wpf_s_stream,
134};
135
136static struct v4l2_subdev_pad_ops wpf_pad_ops = {
137 .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
138 .enum_frame_size = vsp1_rwpf_enum_frame_size,
139 .get_fmt = vsp1_rwpf_get_format,
140 .set_fmt = vsp1_rwpf_set_format,
Laurent Pincharte5ad37b2013-08-24 20:49:58 -0300141 .get_selection = vsp1_rwpf_get_selection,
142 .set_selection = vsp1_rwpf_set_selection,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300143};
144
145static struct v4l2_subdev_ops wpf_ops = {
146 .video = &wpf_video_ops,
147 .pad = &wpf_pad_ops,
148};
149
150/* -----------------------------------------------------------------------------
151 * Video Device Operations
152 */
153
154static void wpf_vdev_queue(struct vsp1_video *video,
155 struct vsp1_video_buffer *buf)
156{
157 struct vsp1_rwpf *wpf = container_of(video, struct vsp1_rwpf, video);
158
159 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_Y, buf->addr[0]);
160 if (buf->buf.num_planes > 1)
161 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C0, buf->addr[1]);
162 if (buf->buf.num_planes > 2)
163 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C1, buf->addr[2]);
164}
165
166static const struct vsp1_video_operations wpf_vdev_ops = {
167 .queue = wpf_vdev_queue,
168};
169
170/* -----------------------------------------------------------------------------
171 * Initialization and Cleanup
172 */
173
174struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
175{
176 struct v4l2_subdev *subdev;
177 struct vsp1_video *video;
178 struct vsp1_rwpf *wpf;
179 unsigned int flags;
180 int ret;
181
182 wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
183 if (wpf == NULL)
184 return ERR_PTR(-ENOMEM);
185
186 wpf->max_width = WPF_MAX_WIDTH;
187 wpf->max_height = WPF_MAX_HEIGHT;
188
189 wpf->entity.type = VSP1_ENTITY_WPF;
190 wpf->entity.index = index;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300191
192 ret = vsp1_entity_init(vsp1, &wpf->entity, 2);
193 if (ret < 0)
194 return ERR_PTR(ret);
195
196 /* Initialize the V4L2 subdev. */
197 subdev = &wpf->entity.subdev;
198 v4l2_subdev_init(subdev, &wpf_ops);
199
200 subdev->entity.ops = &vsp1_media_ops;
201 subdev->internal_ops = &vsp1_subdev_internal_ops;
202 snprintf(subdev->name, sizeof(subdev->name), "%s wpf.%u",
203 dev_name(vsp1->dev), index);
204 v4l2_set_subdevdata(subdev, wpf);
205 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
206
207 vsp1_entity_init_formats(subdev, NULL);
208
209 /* Initialize the video device. */
210 video = &wpf->video;
211
212 video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
213 video->vsp1 = vsp1;
214 video->ops = &wpf_vdev_ops;
215
216 ret = vsp1_video_init(video, &wpf->entity);
217 if (ret < 0)
Laurent Pinchart1499be62014-05-28 12:49:13 -0300218 goto error;
219
220 wpf->entity.video = video;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300221
222 /* Connect the video device to the WPF. All connections are immutable
223 * except for the WPF0 source link if a LIF is present.
224 */
225 flags = MEDIA_LNK_FL_ENABLED;
226 if (!(vsp1->pdata->features & VSP1_HAS_LIF) || index != 0)
227 flags |= MEDIA_LNK_FL_IMMUTABLE;
228
229 ret = media_entity_create_link(&wpf->entity.subdev.entity,
230 RWPF_PAD_SOURCE,
231 &wpf->video.video.entity, 0, flags);
232 if (ret < 0)
Laurent Pinchart1499be62014-05-28 12:49:13 -0300233 goto error;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300234
235 wpf->entity.sink = &wpf->video.video.entity;
236
237 return wpf;
238
Laurent Pinchart1499be62014-05-28 12:49:13 -0300239error:
240 vsp1_entity_destroy(&wpf->entity);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300241 return ERR_PTR(ret);
242}