]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/media/platform/vsp1/vsp1_wpf.c
Merge tag 'pstore-v4.8' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
[linux.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_dl.h"
20 #include "vsp1_pipe.h"
21 #include "vsp1_rwpf.h"
22 #include "vsp1_video.h"
23
24 #define WPF_GEN2_MAX_WIDTH                      2048U
25 #define WPF_GEN2_MAX_HEIGHT                     2048U
26 #define WPF_GEN3_MAX_WIDTH                      8190U
27 #define WPF_GEN3_MAX_HEIGHT                     8190U
28
29 /* -----------------------------------------------------------------------------
30  * Device Access
31  */
32
33 static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf,
34                                   struct vsp1_dl_list *dl, u32 reg, u32 data)
35 {
36         vsp1_dl_list_write(dl, reg + wpf->entity.index * VI6_WPF_OFFSET, data);
37 }
38
39 /* -----------------------------------------------------------------------------
40  * V4L2 Subdevice Core Operations
41  */
42
43 static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
44 {
45         struct vsp1_rwpf *wpf = to_rwpf(subdev);
46         struct vsp1_device *vsp1 = wpf->entity.vsp1;
47
48         if (enable)
49                 return 0;
50
51         /* Write to registers directly when stopping the stream as there will be
52          * no pipeline run to apply the display list.
53          */
54         vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
55         vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET +
56                    VI6_WPF_SRCRPF, 0);
57
58         return 0;
59 }
60
61 /* -----------------------------------------------------------------------------
62  * V4L2 Subdevice Operations
63  */
64
65 static struct v4l2_subdev_video_ops wpf_video_ops = {
66         .s_stream = wpf_s_stream,
67 };
68
69 static struct v4l2_subdev_ops wpf_ops = {
70         .video  = &wpf_video_ops,
71         .pad    = &vsp1_rwpf_pad_ops,
72 };
73
74 /* -----------------------------------------------------------------------------
75  * VSP1 Entity Operations
76  */
77
78 static void vsp1_wpf_destroy(struct vsp1_entity *entity)
79 {
80         struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
81
82         vsp1_dlm_destroy(wpf->dlm);
83 }
84
85 static void wpf_set_memory(struct vsp1_entity *entity, struct vsp1_dl_list *dl)
86 {
87         struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
88
89         vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_Y, wpf->mem.addr[0]);
90         vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C0, wpf->mem.addr[1]);
91         vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C1, wpf->mem.addr[2]);
92 }
93
94 static void wpf_configure(struct vsp1_entity *entity,
95                           struct vsp1_pipeline *pipe,
96                           struct vsp1_dl_list *dl)
97 {
98         struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev);
99         struct vsp1_device *vsp1 = wpf->entity.vsp1;
100         const struct v4l2_mbus_framefmt *source_format;
101         const struct v4l2_mbus_framefmt *sink_format;
102         const struct v4l2_rect *crop;
103         unsigned int i;
104         u32 outfmt = 0;
105         u32 srcrpf = 0;
106
107         /* Cropping */
108         crop = vsp1_rwpf_get_crop(wpf, wpf->entity.config);
109
110         vsp1_wpf_write(wpf, dl, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
111                        (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) |
112                        (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT));
113         vsp1_wpf_write(wpf, dl, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
114                        (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) |
115                        (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT));
116
117         /* Format */
118         sink_format = vsp1_entity_get_pad_format(&wpf->entity,
119                                                  wpf->entity.config,
120                                                  RWPF_PAD_SINK);
121         source_format = vsp1_entity_get_pad_format(&wpf->entity,
122                                                    wpf->entity.config,
123                                                    RWPF_PAD_SOURCE);
124
125         if (!pipe->lif) {
126                 const struct v4l2_pix_format_mplane *format = &wpf->format;
127                 const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
128
129                 outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
130
131                 if (fmtinfo->alpha)
132                         outfmt |= VI6_WPF_OUTFMT_PXA;
133                 if (fmtinfo->swap_yc)
134                         outfmt |= VI6_WPF_OUTFMT_SPYCS;
135                 if (fmtinfo->swap_uv)
136                         outfmt |= VI6_WPF_OUTFMT_SPUVS;
137
138                 /* Destination stride and byte swapping. */
139                 vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_Y,
140                                format->plane_fmt[0].bytesperline);
141                 if (format->num_planes > 1)
142                         vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_C,
143                                        format->plane_fmt[1].bytesperline);
144
145                 vsp1_wpf_write(wpf, dl, VI6_WPF_DSWAP, fmtinfo->swap);
146         }
147
148         if (sink_format->code != source_format->code)
149                 outfmt |= VI6_WPF_OUTFMT_CSC;
150
151         outfmt |= wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT;
152         vsp1_wpf_write(wpf, dl, VI6_WPF_OUTFMT, outfmt);
153
154         vsp1_dl_list_write(dl, VI6_DPR_WPF_FPORCH(wpf->entity.index),
155                            VI6_DPR_WPF_FPORCH_FP_WPFN);
156
157         vsp1_dl_list_write(dl, VI6_WPF_WRBCK_CTRL, 0);
158
159         /* Sources. If the pipeline has a single input and BRU is not used,
160          * configure it as the master layer. Otherwise configure all
161          * inputs as sub-layers and select the virtual RPF as the master
162          * layer.
163          */
164         for (i = 0; i < vsp1->info->rpf_count; ++i) {
165                 struct vsp1_rwpf *input = pipe->inputs[i];
166
167                 if (!input)
168                         continue;
169
170                 srcrpf |= (!pipe->bru && pipe->num_inputs == 1)
171                         ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
172                         : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
173         }
174
175         if (pipe->bru || pipe->num_inputs > 1)
176                 srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
177
178         vsp1_wpf_write(wpf, dl, VI6_WPF_SRCRPF, srcrpf);
179
180         /* Enable interrupts */
181         vsp1_dl_list_write(dl, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
182         vsp1_dl_list_write(dl, VI6_WPF_IRQ_ENB(wpf->entity.index),
183                            VI6_WFP_IRQ_ENB_FREE);
184 }
185
186 static const struct vsp1_entity_operations wpf_entity_ops = {
187         .destroy = vsp1_wpf_destroy,
188         .set_memory = wpf_set_memory,
189         .configure = wpf_configure,
190 };
191
192 /* -----------------------------------------------------------------------------
193  * Initialization and Cleanup
194  */
195
196 struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
197 {
198         struct vsp1_rwpf *wpf;
199         char name[6];
200         int ret;
201
202         wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
203         if (wpf == NULL)
204                 return ERR_PTR(-ENOMEM);
205
206         if (vsp1->info->gen == 2) {
207                 wpf->max_width = WPF_GEN2_MAX_WIDTH;
208                 wpf->max_height = WPF_GEN2_MAX_HEIGHT;
209         } else {
210                 wpf->max_width = WPF_GEN3_MAX_WIDTH;
211                 wpf->max_height = WPF_GEN3_MAX_HEIGHT;
212         }
213
214         wpf->entity.ops = &wpf_entity_ops;
215         wpf->entity.type = VSP1_ENTITY_WPF;
216         wpf->entity.index = index;
217
218         sprintf(name, "wpf.%u", index);
219         ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &wpf_ops);
220         if (ret < 0)
221                 return ERR_PTR(ret);
222
223         /* Initialize the display list manager. */
224         wpf->dlm = vsp1_dlm_create(vsp1, index, 4);
225         if (!wpf->dlm) {
226                 ret = -ENOMEM;
227                 goto error;
228         }
229
230         /* Initialize the control handler. */
231         ret = vsp1_rwpf_init_ctrls(wpf);
232         if (ret < 0) {
233                 dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
234                         index);
235                 goto error;
236         }
237
238         return wpf;
239
240 error:
241         vsp1_entity_destroy(&wpf->entity);
242         return ERR_PTR(ret);
243 }