v4l: vsp1: Add alpha channel support to the memory ports
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / media / platform / vsp1 / vsp1_wpf.c
1 /*
2  * vsp1_wpf.c  --  R-Car VSP1 Write 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 WPF_MAX_WIDTH                           2048
23 #define WPF_MAX_HEIGHT                          2048
24
25 /* -----------------------------------------------------------------------------
26  * Device Access
27  */
28
29 static 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
35 static 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
45 static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
46 {
47         struct vsp1_pipeline *pipe = to_vsp1_pipeline(&subdev->entity);
48         struct vsp1_rwpf *wpf = to_rwpf(subdev);
49         struct vsp1_device *vsp1 = wpf->entity.vsp1;
50         const struct v4l2_rect *crop = &wpf->crop;
51         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                 vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, 0);
58                 return 0;
59         }
60
61         /* Sources. If the pipeline has a single input configure it as the
62          * master layer. Otherwise configure all inputs as sub-layers and
63          * select the virtual RPF as the master layer.
64          */
65         for (i = 0; i < pipe->num_inputs; ++i) {
66                 struct vsp1_rwpf *input = pipe->inputs[i];
67
68                 srcrpf |= pipe->num_inputs == 1
69                         ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
70                         : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
71         }
72
73         if (pipe->num_inputs > 1)
74                 srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
75
76         vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf);
77
78         /* Destination stride. */
79         if (!pipe->lif) {
80                 struct v4l2_pix_format_mplane *format = &wpf->video.format;
81
82                 vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_Y,
83                                format->plane_fmt[0].bytesperline);
84                 if (format->num_planes > 1)
85                         vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_C,
86                                        format->plane_fmt[1].bytesperline);
87         }
88
89         vsp1_wpf_write(wpf, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
90                        (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) |
91                        (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT));
92         vsp1_wpf_write(wpf, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
93                        (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) |
94                        (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT));
95
96         /* Format */
97         if (!pipe->lif) {
98                 const struct vsp1_format_info *fmtinfo = wpf->video.fmtinfo;
99
100                 outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
101
102                 if (fmtinfo->alpha)
103                         outfmt |= VI6_WPF_OUTFMT_PXA;
104                 if (fmtinfo->swap_yc)
105                         outfmt |= VI6_WPF_OUTFMT_SPYCS;
106                 if (fmtinfo->swap_uv)
107                         outfmt |= VI6_WPF_OUTFMT_SPUVS;
108
109                 vsp1_wpf_write(wpf, VI6_WPF_DSWAP, fmtinfo->swap);
110         }
111
112         if (wpf->entity.formats[RWPF_PAD_SINK].code !=
113             wpf->entity.formats[RWPF_PAD_SOURCE].code)
114                 outfmt |= VI6_WPF_OUTFMT_CSC;
115
116         vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, outfmt);
117
118         vsp1_write(vsp1, VI6_DPR_WPF_FPORCH(wpf->entity.index),
119                    VI6_DPR_WPF_FPORCH_FP_WPFN);
120
121         vsp1_write(vsp1, VI6_WPF_WRBCK_CTRL, 0);
122
123         /* Enable interrupts */
124         vsp1_write(vsp1, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
125         vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index),
126                    VI6_WFP_IRQ_ENB_FREE);
127
128         return 0;
129 }
130
131 /* -----------------------------------------------------------------------------
132  * V4L2 Subdevice Operations
133  */
134
135 static struct v4l2_subdev_video_ops wpf_video_ops = {
136         .s_stream = wpf_s_stream,
137 };
138
139 static struct v4l2_subdev_pad_ops wpf_pad_ops = {
140         .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
141         .enum_frame_size = vsp1_rwpf_enum_frame_size,
142         .get_fmt = vsp1_rwpf_get_format,
143         .set_fmt = vsp1_rwpf_set_format,
144         .get_selection = vsp1_rwpf_get_selection,
145         .set_selection = vsp1_rwpf_set_selection,
146 };
147
148 static struct v4l2_subdev_ops wpf_ops = {
149         .video  = &wpf_video_ops,
150         .pad    = &wpf_pad_ops,
151 };
152
153 /* -----------------------------------------------------------------------------
154  * Video Device Operations
155  */
156
157 static void wpf_vdev_queue(struct vsp1_video *video,
158                            struct vsp1_video_buffer *buf)
159 {
160         struct vsp1_rwpf *wpf = container_of(video, struct vsp1_rwpf, video);
161
162         vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_Y, buf->addr[0]);
163         if (buf->buf.num_planes > 1)
164                 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C0, buf->addr[1]);
165         if (buf->buf.num_planes > 2)
166                 vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C1, buf->addr[2]);
167 }
168
169 static const struct vsp1_video_operations wpf_vdev_ops = {
170         .queue = wpf_vdev_queue,
171 };
172
173 /* -----------------------------------------------------------------------------
174  * Initialization and Cleanup
175  */
176
177 struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
178 {
179         struct v4l2_subdev *subdev;
180         struct vsp1_video *video;
181         struct vsp1_rwpf *wpf;
182         unsigned int flags;
183         int ret;
184
185         wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
186         if (wpf == NULL)
187                 return ERR_PTR(-ENOMEM);
188
189         wpf->max_width = WPF_MAX_WIDTH;
190         wpf->max_height = WPF_MAX_HEIGHT;
191
192         wpf->entity.type = VSP1_ENTITY_WPF;
193         wpf->entity.index = index;
194
195         ret = vsp1_entity_init(vsp1, &wpf->entity, 2);
196         if (ret < 0)
197                 return ERR_PTR(ret);
198
199         /* Initialize the V4L2 subdev. */
200         subdev = &wpf->entity.subdev;
201         v4l2_subdev_init(subdev, &wpf_ops);
202
203         subdev->entity.ops = &vsp1_media_ops;
204         subdev->internal_ops = &vsp1_subdev_internal_ops;
205         snprintf(subdev->name, sizeof(subdev->name), "%s wpf.%u",
206                  dev_name(vsp1->dev), index);
207         v4l2_set_subdevdata(subdev, wpf);
208         subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
209
210         vsp1_entity_init_formats(subdev, NULL);
211
212         /* Initialize the video device. */
213         video = &wpf->video;
214
215         video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
216         video->vsp1 = vsp1;
217         video->ops = &wpf_vdev_ops;
218
219         ret = vsp1_video_init(video, &wpf->entity);
220         if (ret < 0)
221                 goto error;
222
223         wpf->entity.video = video;
224
225         /* Connect the video device to the WPF. All connections are immutable
226          * except for the WPF0 source link if a LIF is present.
227          */
228         flags = MEDIA_LNK_FL_ENABLED;
229         if (!(vsp1->pdata->features & VSP1_HAS_LIF) || index != 0)
230                 flags |= MEDIA_LNK_FL_IMMUTABLE;
231
232         ret = media_entity_create_link(&wpf->entity.subdev.entity,
233                                        RWPF_PAD_SOURCE,
234                                        &wpf->video.video.entity, 0, flags);
235         if (ret < 0)
236                 goto error;
237
238         wpf->entity.sink = &wpf->video.video.entity;
239
240         return wpf;
241
242 error:
243         vsp1_entity_destroy(&wpf->entity);
244         return ERR_PTR(ret);
245 }