]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/platform/vsp1/vsp1_rpf.c
[media] v4l: vsp1: Add alpha channel support to the memory ports
[mirror_ubuntu-bionic-kernel.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
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;
e5ad37b6 50 const struct v4l2_rect *crop = &rpf->crop;
26e0ca22
LP
51 u32 pstride;
52 u32 infmt;
53
54 if (!enable)
55 return 0;
56
e5ad37b6
LP
57 /* 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 */
26e0ca22 63 vsp1_rpf_write(rpf, VI6_RPF_SRC_BSIZE,
e5ad37b6
LP
64 (crop->width << VI6_RPF_SRC_BSIZE_BHSIZE_SHIFT) |
65 (crop->height << VI6_RPF_SRC_BSIZE_BVSIZE_SHIFT));
26e0ca22 66 vsp1_rpf_write(rpf, VI6_RPF_SRC_ESIZE,
e5ad37b6
LP
67 (crop->width << VI6_RPF_SRC_ESIZE_EHSIZE_SHIFT) |
68 (crop->height << VI6_RPF_SRC_ESIZE_EVSIZE_SHIFT));
26e0ca22 69
e5ad37b6
LP
70 rpf->offsets[0] = crop->top * format->plane_fmt[0].bytesperline
71 + crop->left * fmtinfo->bpp[0] / 8;
26e0ca22
LP
72 pstride = format->plane_fmt[0].bytesperline
73 << VI6_RPF_SRCM_PSTRIDE_Y_SHIFT;
e5ad37b6
LP
74 if (format->num_planes > 1) {
75 rpf->offsets[1] = crop->top * format->plane_fmt[1].bytesperline
76 + crop->left * fmtinfo->bpp[1] / 8;
26e0ca22
LP
77 pstride |= format->plane_fmt[1].bytesperline
78 << VI6_RPF_SRCM_PSTRIDE_C_SHIFT;
e5ad37b6 79 }
26e0ca22
LP
80
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
629bb6d4
LP
99 /* 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));
26e0ca22 103
7a52b6de
LP
104 /* Use the alpha channel (extended to 8 bits) when available or a
105 * hardcoded 255 value otherwise. Disable color keying.
26e0ca22 106 */
7a52b6de
LP
107 vsp1_rpf_write(rpf, VI6_RPF_ALPH_SEL, VI6_RPF_ALPH_SEL_AEXT_EXT |
108 (fmtinfo->alpha ? VI6_RPF_ALPH_SEL_ASEL_PACKED
109 : VI6_RPF_ALPH_SEL_ASEL_FIXED));
26e0ca22
LP
110 vsp1_rpf_write(rpf, VI6_RPF_VRTCOL_SET,
111 255 << VI6_RPF_VRTCOL_SET_LAYA_SHIFT);
112 vsp1_rpf_write(rpf, VI6_RPF_MSK_CTRL, 0);
113 vsp1_rpf_write(rpf, VI6_RPF_CKEY_CTRL, 0);
114
115 return 0;
116}
117
118/* -----------------------------------------------------------------------------
119 * V4L2 Subdevice Operations
120 */
121
122static struct v4l2_subdev_video_ops rpf_video_ops = {
123 .s_stream = rpf_s_stream,
124};
125
126static struct v4l2_subdev_pad_ops rpf_pad_ops = {
127 .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
128 .enum_frame_size = vsp1_rwpf_enum_frame_size,
129 .get_fmt = vsp1_rwpf_get_format,
130 .set_fmt = vsp1_rwpf_set_format,
e5ad37b6
LP
131 .get_selection = vsp1_rwpf_get_selection,
132 .set_selection = vsp1_rwpf_set_selection,
26e0ca22
LP
133};
134
135static struct v4l2_subdev_ops rpf_ops = {
136 .video = &rpf_video_ops,
137 .pad = &rpf_pad_ops,
138};
139
140/* -----------------------------------------------------------------------------
141 * Video Device Operations
142 */
143
144static void rpf_vdev_queue(struct vsp1_video *video,
145 struct vsp1_video_buffer *buf)
146{
147 struct vsp1_rwpf *rpf = container_of(video, struct vsp1_rwpf, video);
148
e5ad37b6
LP
149 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_Y,
150 buf->addr[0] + rpf->offsets[0]);
26e0ca22 151 if (buf->buf.num_planes > 1)
e5ad37b6
LP
152 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C0,
153 buf->addr[1] + rpf->offsets[1]);
26e0ca22 154 if (buf->buf.num_planes > 2)
e5ad37b6
LP
155 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C1,
156 buf->addr[2] + rpf->offsets[1]);
26e0ca22
LP
157}
158
159static const struct vsp1_video_operations rpf_vdev_ops = {
160 .queue = rpf_vdev_queue,
161};
162
163/* -----------------------------------------------------------------------------
164 * Initialization and Cleanup
165 */
166
167struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
168{
169 struct v4l2_subdev *subdev;
170 struct vsp1_video *video;
171 struct vsp1_rwpf *rpf;
172 int ret;
173
174 rpf = devm_kzalloc(vsp1->dev, sizeof(*rpf), GFP_KERNEL);
175 if (rpf == NULL)
176 return ERR_PTR(-ENOMEM);
177
178 rpf->max_width = RPF_MAX_WIDTH;
179 rpf->max_height = RPF_MAX_HEIGHT;
180
181 rpf->entity.type = VSP1_ENTITY_RPF;
182 rpf->entity.index = index;
26e0ca22
LP
183
184 ret = vsp1_entity_init(vsp1, &rpf->entity, 2);
185 if (ret < 0)
186 return ERR_PTR(ret);
187
188 /* Initialize the V4L2 subdev. */
189 subdev = &rpf->entity.subdev;
190 v4l2_subdev_init(subdev, &rpf_ops);
191
192 subdev->entity.ops = &vsp1_media_ops;
193 subdev->internal_ops = &vsp1_subdev_internal_ops;
194 snprintf(subdev->name, sizeof(subdev->name), "%s rpf.%u",
195 dev_name(vsp1->dev), index);
196 v4l2_set_subdevdata(subdev, rpf);
197 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
198
199 vsp1_entity_init_formats(subdev, NULL);
200
201 /* Initialize the video device. */
202 video = &rpf->video;
203
204 video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
205 video->vsp1 = vsp1;
206 video->ops = &rpf_vdev_ops;
207
208 ret = vsp1_video_init(video, &rpf->entity);
209 if (ret < 0)
1499be67
LP
210 goto error;
211
212 rpf->entity.video = video;
26e0ca22
LP
213
214 /* Connect the video device to the RPF. */
215 ret = media_entity_create_link(&rpf->video.video.entity, 0,
216 &rpf->entity.subdev.entity,
217 RWPF_PAD_SINK,
218 MEDIA_LNK_FL_ENABLED |
219 MEDIA_LNK_FL_IMMUTABLE);
220 if (ret < 0)
1499be67 221 goto error;
26e0ca22
LP
222
223 return rpf;
224
1499be67
LP
225error:
226 vsp1_entity_destroy(&rpf->entity);
26e0ca22
LP
227 return ERR_PTR(ret);
228}