]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/platform/vsp1/vsp1_rpf.c
[media] v4l: vsp1: Implement and use the subdev pad::init_cfg configuration
[karo-tx-linux.git] / drivers / media / platform / vsp1 / vsp1_rpf.c
1 /*
2  * vsp1_rpf.c  --  R-Car VSP1 Read Pixel Formatter
3  *
4  * Copyright (C) 2013-2014 Renesas Electronics Corporation
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
29 static inline void vsp1_rpf_write(struct vsp1_rwpf *rpf, u32 reg, u32 data)
30 {
31         vsp1_mod_write(&rpf->entity, reg + rpf->entity.index * VI6_RPF_OFFSET,
32                        data);
33 }
34
35 /* -----------------------------------------------------------------------------
36  * V4L2 Subdevice Core Operations
37  */
38
39 static int rpf_s_stream(struct v4l2_subdev *subdev, int enable)
40 {
41         struct vsp1_pipeline *pipe = to_vsp1_pipeline(&subdev->entity);
42         struct vsp1_rwpf *rpf = to_rwpf(subdev);
43         const struct vsp1_format_info *fmtinfo = rpf->fmtinfo;
44         const struct v4l2_pix_format_mplane *format = &rpf->format;
45         const struct v4l2_rect *crop = &rpf->crop;
46         u32 pstride;
47         u32 infmt;
48
49         if (!enable)
50                 return 0;
51
52         /* Source size, stride and crop offsets.
53          *
54          * The crop offsets correspond to the location of the crop rectangle top
55          * left corner in the plane buffer. Only two offsets are needed, as
56          * planes 2 and 3 always have identical strides.
57          */
58         vsp1_rpf_write(rpf, VI6_RPF_SRC_BSIZE,
59                        (crop->width << VI6_RPF_SRC_BSIZE_BHSIZE_SHIFT) |
60                        (crop->height << VI6_RPF_SRC_BSIZE_BVSIZE_SHIFT));
61         vsp1_rpf_write(rpf, VI6_RPF_SRC_ESIZE,
62                        (crop->width << VI6_RPF_SRC_ESIZE_EHSIZE_SHIFT) |
63                        (crop->height << VI6_RPF_SRC_ESIZE_EVSIZE_SHIFT));
64
65         rpf->offsets[0] = crop->top * format->plane_fmt[0].bytesperline
66                         + crop->left * fmtinfo->bpp[0] / 8;
67         pstride = format->plane_fmt[0].bytesperline
68                 << VI6_RPF_SRCM_PSTRIDE_Y_SHIFT;
69
70         if (format->num_planes > 1) {
71                 rpf->offsets[1] = crop->top * format->plane_fmt[1].bytesperline
72                                 + crop->left * fmtinfo->bpp[1] / 8;
73                 pstride |= format->plane_fmt[1].bytesperline
74                         << VI6_RPF_SRCM_PSTRIDE_C_SHIFT;
75         } else {
76                 rpf->offsets[1] = 0;
77         }
78
79         vsp1_rpf_write(rpf, VI6_RPF_SRCM_PSTRIDE, pstride);
80
81         /* Format */
82         infmt = VI6_RPF_INFMT_CIPM
83               | (fmtinfo->hwfmt << VI6_RPF_INFMT_RDFMT_SHIFT);
84
85         if (fmtinfo->swap_yc)
86                 infmt |= VI6_RPF_INFMT_SPYCS;
87         if (fmtinfo->swap_uv)
88                 infmt |= VI6_RPF_INFMT_SPUVS;
89
90         if (rpf->entity.formats[RWPF_PAD_SINK].code !=
91             rpf->entity.formats[RWPF_PAD_SOURCE].code)
92                 infmt |= VI6_RPF_INFMT_CSC;
93
94         vsp1_rpf_write(rpf, VI6_RPF_INFMT, infmt);
95         vsp1_rpf_write(rpf, VI6_RPF_DSWAP, fmtinfo->swap);
96
97         /* Output location */
98         vsp1_rpf_write(rpf, VI6_RPF_LOC,
99                        (rpf->location.left << VI6_RPF_LOC_HCOORD_SHIFT) |
100                        (rpf->location.top << VI6_RPF_LOC_VCOORD_SHIFT));
101
102         /* Use the alpha channel (extended to 8 bits) when available or an
103          * alpha value set through the V4L2_CID_ALPHA_COMPONENT control
104          * otherwise. Disable color keying.
105          */
106         vsp1_rpf_write(rpf, VI6_RPF_ALPH_SEL, VI6_RPF_ALPH_SEL_AEXT_EXT |
107                        (fmtinfo->alpha ? VI6_RPF_ALPH_SEL_ASEL_PACKED
108                                        : VI6_RPF_ALPH_SEL_ASEL_FIXED));
109
110         vsp1_rpf_write(rpf, VI6_RPF_VRTCOL_SET,
111                        rpf->alpha << VI6_RPF_VRTCOL_SET_LAYA_SHIFT);
112
113         vsp1_pipeline_propagate_alpha(pipe, &rpf->entity, rpf->alpha);
114
115         vsp1_rpf_write(rpf, VI6_RPF_MSK_CTRL, 0);
116         vsp1_rpf_write(rpf, VI6_RPF_CKEY_CTRL, 0);
117
118         return 0;
119 }
120
121 /* -----------------------------------------------------------------------------
122  * V4L2 Subdevice Operations
123  */
124
125 static struct v4l2_subdev_video_ops rpf_video_ops = {
126         .s_stream = rpf_s_stream,
127 };
128
129 static struct v4l2_subdev_pad_ops rpf_pad_ops = {
130         .init_cfg = vsp1_entity_init_cfg,
131         .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
132         .enum_frame_size = vsp1_rwpf_enum_frame_size,
133         .get_fmt = vsp1_rwpf_get_format,
134         .set_fmt = vsp1_rwpf_set_format,
135         .get_selection = vsp1_rwpf_get_selection,
136         .set_selection = vsp1_rwpf_set_selection,
137 };
138
139 static struct v4l2_subdev_ops rpf_ops = {
140         .video  = &rpf_video_ops,
141         .pad    = &rpf_pad_ops,
142 };
143
144 /* -----------------------------------------------------------------------------
145  * VSP1 Entity Operations
146  */
147
148 static void rpf_set_memory(struct vsp1_entity *entity)
149 {
150         struct vsp1_rwpf *rpf = entity_to_rwpf(entity);
151
152         vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_Y,
153                        rpf->mem.addr[0] + rpf->offsets[0]);
154         vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C0,
155                        rpf->mem.addr[1] + rpf->offsets[1]);
156         vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C1,
157                        rpf->mem.addr[2] + rpf->offsets[1]);
158 }
159
160 static const struct vsp1_entity_operations rpf_entity_ops = {
161         .set_memory = rpf_set_memory,
162 };
163
164 /* -----------------------------------------------------------------------------
165  * Initialization and Cleanup
166  */
167
168 struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
169 {
170         struct vsp1_rwpf *rpf;
171         char name[6];
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.ops = &rpf_entity_ops;
182         rpf->entity.type = VSP1_ENTITY_RPF;
183         rpf->entity.index = index;
184
185         sprintf(name, "rpf.%u", index);
186         ret = vsp1_entity_init(vsp1, &rpf->entity, name, 2, &rpf_ops);
187         if (ret < 0)
188                 return ERR_PTR(ret);
189
190         /* Initialize the control handler. */
191         ret = vsp1_rwpf_init_ctrls(rpf);
192         if (ret < 0) {
193                 dev_err(vsp1->dev, "rpf%u: failed to initialize controls\n",
194                         index);
195                 goto error;
196         }
197
198         return rpf;
199
200 error:
201         vsp1_entity_destroy(&rpf->entity);
202         return ERR_PTR(ret);
203 }