Linux-libre 4.14.14-gnu
[librecmc/linux-libre.git] / drivers / media / platform / vsp1 / vsp1_rwpf.c
1 /*
2  * vsp1_rwpf.c  --  R-Car VSP1 Read and Write Pixel Formatters
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 <media/v4l2-subdev.h>
15
16 #include "vsp1.h"
17 #include "vsp1_rwpf.h"
18 #include "vsp1_video.h"
19
20 #define RWPF_MIN_WIDTH                          1
21 #define RWPF_MIN_HEIGHT                         1
22
23 struct v4l2_rect *vsp1_rwpf_get_crop(struct vsp1_rwpf *rwpf,
24                                      struct v4l2_subdev_pad_config *config)
25 {
26         return v4l2_subdev_get_try_crop(&rwpf->entity.subdev, config,
27                                         RWPF_PAD_SINK);
28 }
29
30 /* -----------------------------------------------------------------------------
31  * V4L2 Subdevice Pad Operations
32  */
33
34 static int vsp1_rwpf_enum_mbus_code(struct v4l2_subdev *subdev,
35                                     struct v4l2_subdev_pad_config *cfg,
36                                     struct v4l2_subdev_mbus_code_enum *code)
37 {
38         static const unsigned int codes[] = {
39                 MEDIA_BUS_FMT_ARGB8888_1X32,
40                 MEDIA_BUS_FMT_AHSV8888_1X32,
41                 MEDIA_BUS_FMT_AYUV8_1X32,
42         };
43
44         if (code->index >= ARRAY_SIZE(codes))
45                 return -EINVAL;
46
47         code->code = codes[code->index];
48
49         return 0;
50 }
51
52 static int vsp1_rwpf_enum_frame_size(struct v4l2_subdev *subdev,
53                                      struct v4l2_subdev_pad_config *cfg,
54                                      struct v4l2_subdev_frame_size_enum *fse)
55 {
56         struct vsp1_rwpf *rwpf = to_rwpf(subdev);
57
58         return vsp1_subdev_enum_frame_size(subdev, cfg, fse, RWPF_MIN_WIDTH,
59                                            RWPF_MIN_HEIGHT, rwpf->max_width,
60                                            rwpf->max_height);
61 }
62
63 static int vsp1_rwpf_set_format(struct v4l2_subdev *subdev,
64                                 struct v4l2_subdev_pad_config *cfg,
65                                 struct v4l2_subdev_format *fmt)
66 {
67         struct vsp1_rwpf *rwpf = to_rwpf(subdev);
68         struct v4l2_subdev_pad_config *config;
69         struct v4l2_mbus_framefmt *format;
70         int ret = 0;
71
72         mutex_lock(&rwpf->entity.lock);
73
74         config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, fmt->which);
75         if (!config) {
76                 ret = -EINVAL;
77                 goto done;
78         }
79
80         /* Default to YUV if the requested format is not supported. */
81         if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
82             fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
83             fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
84                 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
85
86         format = vsp1_entity_get_pad_format(&rwpf->entity, config, fmt->pad);
87
88         if (fmt->pad == RWPF_PAD_SOURCE) {
89                 /*
90                  * The RWPF performs format conversion but can't scale, only the
91                  * format code can be changed on the source pad.
92                  */
93                 format->code = fmt->format.code;
94                 fmt->format = *format;
95                 goto done;
96         }
97
98         format->code = fmt->format.code;
99         format->width = clamp_t(unsigned int, fmt->format.width,
100                                 RWPF_MIN_WIDTH, rwpf->max_width);
101         format->height = clamp_t(unsigned int, fmt->format.height,
102                                  RWPF_MIN_HEIGHT, rwpf->max_height);
103         format->field = V4L2_FIELD_NONE;
104         format->colorspace = V4L2_COLORSPACE_SRGB;
105
106         fmt->format = *format;
107
108         if (rwpf->entity.type == VSP1_ENTITY_RPF) {
109                 struct v4l2_rect *crop;
110
111                 /* Update the sink crop rectangle. */
112                 crop = vsp1_rwpf_get_crop(rwpf, config);
113                 crop->left = 0;
114                 crop->top = 0;
115                 crop->width = fmt->format.width;
116                 crop->height = fmt->format.height;
117         }
118
119         /* Propagate the format to the source pad. */
120         format = vsp1_entity_get_pad_format(&rwpf->entity, config,
121                                             RWPF_PAD_SOURCE);
122         *format = fmt->format;
123
124         if (rwpf->flip.rotate) {
125                 format->width = fmt->format.height;
126                 format->height = fmt->format.width;
127         }
128
129 done:
130         mutex_unlock(&rwpf->entity.lock);
131         return ret;
132 }
133
134 static int vsp1_rwpf_get_selection(struct v4l2_subdev *subdev,
135                                    struct v4l2_subdev_pad_config *cfg,
136                                    struct v4l2_subdev_selection *sel)
137 {
138         struct vsp1_rwpf *rwpf = to_rwpf(subdev);
139         struct v4l2_subdev_pad_config *config;
140         struct v4l2_mbus_framefmt *format;
141         int ret = 0;
142
143         /*
144          * Cropping is only supported on the RPF and is implemented on the sink
145          * pad.
146          */
147         if (rwpf->entity.type == VSP1_ENTITY_WPF || sel->pad != RWPF_PAD_SINK)
148                 return -EINVAL;
149
150         mutex_lock(&rwpf->entity.lock);
151
152         config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
153         if (!config) {
154                 ret = -EINVAL;
155                 goto done;
156         }
157
158         switch (sel->target) {
159         case V4L2_SEL_TGT_CROP:
160                 sel->r = *vsp1_rwpf_get_crop(rwpf, config);
161                 break;
162
163         case V4L2_SEL_TGT_CROP_BOUNDS:
164                 format = vsp1_entity_get_pad_format(&rwpf->entity, config,
165                                                     RWPF_PAD_SINK);
166                 sel->r.left = 0;
167                 sel->r.top = 0;
168                 sel->r.width = format->width;
169                 sel->r.height = format->height;
170                 break;
171
172         default:
173                 ret = -EINVAL;
174                 break;
175         }
176
177 done:
178         mutex_unlock(&rwpf->entity.lock);
179         return ret;
180 }
181
182 static int vsp1_rwpf_set_selection(struct v4l2_subdev *subdev,
183                                    struct v4l2_subdev_pad_config *cfg,
184                                    struct v4l2_subdev_selection *sel)
185 {
186         struct vsp1_rwpf *rwpf = to_rwpf(subdev);
187         struct v4l2_subdev_pad_config *config;
188         struct v4l2_mbus_framefmt *format;
189         struct v4l2_rect *crop;
190         int ret = 0;
191
192         /*
193          * Cropping is only supported on the RPF and is implemented on the sink
194          * pad.
195          */
196         if (rwpf->entity.type == VSP1_ENTITY_WPF || sel->pad != RWPF_PAD_SINK)
197                 return -EINVAL;
198
199         if (sel->target != V4L2_SEL_TGT_CROP)
200                 return -EINVAL;
201
202         mutex_lock(&rwpf->entity.lock);
203
204         config = vsp1_entity_get_pad_config(&rwpf->entity, cfg, sel->which);
205         if (!config) {
206                 ret = -EINVAL;
207                 goto done;
208         }
209
210         /* Make sure the crop rectangle is entirely contained in the image. */
211         format = vsp1_entity_get_pad_format(&rwpf->entity, config,
212                                             RWPF_PAD_SINK);
213
214         /*
215          * Restrict the crop rectangle coordinates to multiples of 2 to avoid
216          * shifting the color plane.
217          */
218         if (format->code == MEDIA_BUS_FMT_AYUV8_1X32) {
219                 sel->r.left = ALIGN(sel->r.left, 2);
220                 sel->r.top = ALIGN(sel->r.top, 2);
221                 sel->r.width = round_down(sel->r.width, 2);
222                 sel->r.height = round_down(sel->r.height, 2);
223         }
224
225         sel->r.left = min_t(unsigned int, sel->r.left, format->width - 2);
226         sel->r.top = min_t(unsigned int, sel->r.top, format->height - 2);
227         sel->r.width = min_t(unsigned int, sel->r.width,
228                              format->width - sel->r.left);
229         sel->r.height = min_t(unsigned int, sel->r.height,
230                               format->height - sel->r.top);
231
232         crop = vsp1_rwpf_get_crop(rwpf, config);
233         *crop = sel->r;
234
235         /* Propagate the format to the source pad. */
236         format = vsp1_entity_get_pad_format(&rwpf->entity, config,
237                                             RWPF_PAD_SOURCE);
238         format->width = crop->width;
239         format->height = crop->height;
240
241 done:
242         mutex_unlock(&rwpf->entity.lock);
243         return ret;
244 }
245
246 const struct v4l2_subdev_pad_ops vsp1_rwpf_pad_ops = {
247         .init_cfg = vsp1_entity_init_cfg,
248         .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
249         .enum_frame_size = vsp1_rwpf_enum_frame_size,
250         .get_fmt = vsp1_subdev_get_pad_format,
251         .set_fmt = vsp1_rwpf_set_format,
252         .get_selection = vsp1_rwpf_get_selection,
253         .set_selection = vsp1_rwpf_set_selection,
254 };
255
256 /* -----------------------------------------------------------------------------
257  * Controls
258  */
259
260 static int vsp1_rwpf_s_ctrl(struct v4l2_ctrl *ctrl)
261 {
262         struct vsp1_rwpf *rwpf =
263                 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
264
265         switch (ctrl->id) {
266         case V4L2_CID_ALPHA_COMPONENT:
267                 rwpf->alpha = ctrl->val;
268                 break;
269         }
270
271         return 0;
272 }
273
274 static const struct v4l2_ctrl_ops vsp1_rwpf_ctrl_ops = {
275         .s_ctrl = vsp1_rwpf_s_ctrl,
276 };
277
278 int vsp1_rwpf_init_ctrls(struct vsp1_rwpf *rwpf, unsigned int ncontrols)
279 {
280         v4l2_ctrl_handler_init(&rwpf->ctrls, ncontrols + 1);
281         v4l2_ctrl_new_std(&rwpf->ctrls, &vsp1_rwpf_ctrl_ops,
282                           V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
283
284         rwpf->entity.subdev.ctrl_handler = &rwpf->ctrls;
285
286         return rwpf->ctrls.error;
287 }