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