]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/media/platform/vimc/vimc-scaler.c
media: vimc: move duplicated IS_SRC and IS_SINK to common header
[linux.git] / drivers / media / platform / vimc / vimc-scaler.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * vimc-scaler.c Virtual Media Controller Driver
4  *
5  * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
6  */
7
8 #include <linux/moduleparam.h>
9 #include <linux/vmalloc.h>
10 #include <linux/v4l2-mediabus.h>
11 #include <media/v4l2-subdev.h>
12
13 #include "vimc-common.h"
14
15 static unsigned int sca_mult = 3;
16 module_param(sca_mult, uint, 0000);
17 MODULE_PARM_DESC(sca_mult, " the image size multiplier");
18
19 #define MAX_ZOOM        8
20
21 struct vimc_sca_device {
22         struct vimc_ent_device ved;
23         struct v4l2_subdev sd;
24         struct device *dev;
25         /* NOTE: the source fmt is the same as the sink
26          * with the width and hight multiplied by mult
27          */
28         struct v4l2_mbus_framefmt sink_fmt;
29         /* Values calculated when the stream starts */
30         u8 *src_frame;
31         unsigned int src_line_size;
32         unsigned int bpp;
33 };
34
35 static const struct v4l2_mbus_framefmt sink_fmt_default = {
36         .width = 640,
37         .height = 480,
38         .code = MEDIA_BUS_FMT_RGB888_1X24,
39         .field = V4L2_FIELD_NONE,
40         .colorspace = V4L2_COLORSPACE_DEFAULT,
41 };
42
43 static int vimc_sca_init_cfg(struct v4l2_subdev *sd,
44                              struct v4l2_subdev_pad_config *cfg)
45 {
46         struct v4l2_mbus_framefmt *mf;
47         unsigned int i;
48
49         mf = v4l2_subdev_get_try_format(sd, cfg, 0);
50         *mf = sink_fmt_default;
51
52         for (i = 1; i < sd->entity.num_pads; i++) {
53                 mf = v4l2_subdev_get_try_format(sd, cfg, i);
54                 *mf = sink_fmt_default;
55                 mf->width = mf->width * sca_mult;
56                 mf->height = mf->height * sca_mult;
57         }
58
59         return 0;
60 }
61
62 static int vimc_sca_enum_mbus_code(struct v4l2_subdev *sd,
63                                    struct v4l2_subdev_pad_config *cfg,
64                                    struct v4l2_subdev_mbus_code_enum *code)
65 {
66         const struct vimc_pix_map *vpix = vimc_pix_map_by_index(code->index);
67
68         /* We don't support bayer format */
69         if (!vpix || vpix->bayer)
70                 return -EINVAL;
71
72         code->code = vpix->code;
73
74         return 0;
75 }
76
77 static int vimc_sca_enum_frame_size(struct v4l2_subdev *sd,
78                                     struct v4l2_subdev_pad_config *cfg,
79                                     struct v4l2_subdev_frame_size_enum *fse)
80 {
81         const struct vimc_pix_map *vpix;
82
83         if (fse->index)
84                 return -EINVAL;
85
86         /* Only accept code in the pix map table in non bayer format */
87         vpix = vimc_pix_map_by_code(fse->code);
88         if (!vpix || vpix->bayer)
89                 return -EINVAL;
90
91         fse->min_width = VIMC_FRAME_MIN_WIDTH;
92         fse->min_height = VIMC_FRAME_MIN_HEIGHT;
93
94         if (VIMC_IS_SINK(fse->pad)) {
95                 fse->max_width = VIMC_FRAME_MAX_WIDTH;
96                 fse->max_height = VIMC_FRAME_MAX_HEIGHT;
97         } else {
98                 fse->max_width = VIMC_FRAME_MAX_WIDTH * MAX_ZOOM;
99                 fse->max_height = VIMC_FRAME_MAX_HEIGHT * MAX_ZOOM;
100         }
101
102         return 0;
103 }
104
105 static int vimc_sca_get_fmt(struct v4l2_subdev *sd,
106                             struct v4l2_subdev_pad_config *cfg,
107                             struct v4l2_subdev_format *format)
108 {
109         struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
110
111         /* Get the current sink format */
112         format->format = (format->which == V4L2_SUBDEV_FORMAT_TRY) ?
113                          *v4l2_subdev_get_try_format(sd, cfg, 0) :
114                          vsca->sink_fmt;
115
116         /* Scale the frame size for the source pad */
117         if (VIMC_IS_SRC(format->pad)) {
118                 format->format.width = vsca->sink_fmt.width * sca_mult;
119                 format->format.height = vsca->sink_fmt.height * sca_mult;
120         }
121
122         return 0;
123 }
124
125 static void vimc_sca_adjust_sink_fmt(struct v4l2_mbus_framefmt *fmt)
126 {
127         const struct vimc_pix_map *vpix;
128
129         /* Only accept code in the pix map table in non bayer format */
130         vpix = vimc_pix_map_by_code(fmt->code);
131         if (!vpix || vpix->bayer)
132                 fmt->code = sink_fmt_default.code;
133
134         fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
135                              VIMC_FRAME_MAX_WIDTH) & ~1;
136         fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
137                               VIMC_FRAME_MAX_HEIGHT) & ~1;
138
139         if (fmt->field == V4L2_FIELD_ANY)
140                 fmt->field = sink_fmt_default.field;
141
142         vimc_colorimetry_clamp(fmt);
143 }
144
145 static int vimc_sca_set_fmt(struct v4l2_subdev *sd,
146                             struct v4l2_subdev_pad_config *cfg,
147                             struct v4l2_subdev_format *fmt)
148 {
149         struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
150         struct v4l2_mbus_framefmt *sink_fmt;
151
152         if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
153                 /* Do not change the format while stream is on */
154                 if (vsca->src_frame)
155                         return -EBUSY;
156
157                 sink_fmt = &vsca->sink_fmt;
158         } else {
159                 sink_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
160         }
161
162         /*
163          * Do not change the format of the source pad,
164          * it is propagated from the sink
165          */
166         if (VIMC_IS_SRC(fmt->pad)) {
167                 fmt->format = *sink_fmt;
168                 fmt->format.width = sink_fmt->width * sca_mult;
169                 fmt->format.height = sink_fmt->height * sca_mult;
170         } else {
171                 /* Set the new format in the sink pad */
172                 vimc_sca_adjust_sink_fmt(&fmt->format);
173
174                 dev_dbg(vsca->dev, "%s: sink format update: "
175                         "old:%dx%d (0x%x, %d, %d, %d, %d) "
176                         "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsca->sd.name,
177                         /* old */
178                         sink_fmt->width, sink_fmt->height, sink_fmt->code,
179                         sink_fmt->colorspace, sink_fmt->quantization,
180                         sink_fmt->xfer_func, sink_fmt->ycbcr_enc,
181                         /* new */
182                         fmt->format.width, fmt->format.height, fmt->format.code,
183                         fmt->format.colorspace, fmt->format.quantization,
184                         fmt->format.xfer_func, fmt->format.ycbcr_enc);
185
186                 *sink_fmt = fmt->format;
187         }
188
189         return 0;
190 }
191
192 static const struct v4l2_subdev_pad_ops vimc_sca_pad_ops = {
193         .init_cfg               = vimc_sca_init_cfg,
194         .enum_mbus_code         = vimc_sca_enum_mbus_code,
195         .enum_frame_size        = vimc_sca_enum_frame_size,
196         .get_fmt                = vimc_sca_get_fmt,
197         .set_fmt                = vimc_sca_set_fmt,
198 };
199
200 static int vimc_sca_s_stream(struct v4l2_subdev *sd, int enable)
201 {
202         struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
203
204         if (enable) {
205                 const struct vimc_pix_map *vpix;
206                 unsigned int frame_size;
207
208                 if (vsca->src_frame)
209                         return 0;
210
211                 /* Save the bytes per pixel of the sink */
212                 vpix = vimc_pix_map_by_code(vsca->sink_fmt.code);
213                 vsca->bpp = vpix->bpp;
214
215                 /* Calculate the width in bytes of the src frame */
216                 vsca->src_line_size = vsca->sink_fmt.width *
217                                       sca_mult * vsca->bpp;
218
219                 /* Calculate the frame size of the source pad */
220                 frame_size = vsca->src_line_size * vsca->sink_fmt.height *
221                              sca_mult;
222
223                 /* Allocate the frame buffer. Use vmalloc to be able to
224                  * allocate a large amount of memory
225                  */
226                 vsca->src_frame = vmalloc(frame_size);
227                 if (!vsca->src_frame)
228                         return -ENOMEM;
229
230         } else {
231                 if (!vsca->src_frame)
232                         return 0;
233
234                 vfree(vsca->src_frame);
235                 vsca->src_frame = NULL;
236         }
237
238         return 0;
239 }
240
241 static const struct v4l2_subdev_video_ops vimc_sca_video_ops = {
242         .s_stream = vimc_sca_s_stream,
243 };
244
245 static const struct v4l2_subdev_ops vimc_sca_ops = {
246         .pad = &vimc_sca_pad_ops,
247         .video = &vimc_sca_video_ops,
248 };
249
250 static void vimc_sca_fill_pix(u8 *const ptr,
251                               const u8 *const pixel,
252                               const unsigned int bpp)
253 {
254         unsigned int i;
255
256         /* copy the pixel to the pointer */
257         for (i = 0; i < bpp; i++)
258                 ptr[i] = pixel[i];
259 }
260
261 static void vimc_sca_scale_pix(const struct vimc_sca_device *const vsca,
262                                const unsigned int lin, const unsigned int col,
263                                const u8 *const sink_frame)
264 {
265         unsigned int i, j, index;
266         const u8 *pixel;
267
268         /* Point to the pixel value in position (lin, col) in the sink frame */
269         index = VIMC_FRAME_INDEX(lin, col,
270                                  vsca->sink_fmt.width,
271                                  vsca->bpp);
272         pixel = &sink_frame[index];
273
274         dev_dbg(vsca->dev,
275                 "sca: %s: --- scale_pix sink pos %dx%d, index %d ---\n",
276                 vsca->sd.name, lin, col, index);
277
278         /* point to the place we are going to put the first pixel
279          * in the scaled src frame
280          */
281         index = VIMC_FRAME_INDEX(lin * sca_mult, col * sca_mult,
282                                  vsca->sink_fmt.width * sca_mult, vsca->bpp);
283
284         dev_dbg(vsca->dev, "sca: %s: scale_pix src pos %dx%d, index %d\n",
285                 vsca->sd.name, lin * sca_mult, col * sca_mult, index);
286
287         /* Repeat this pixel mult times */
288         for (i = 0; i < sca_mult; i++) {
289                 /* Iterate through each beginning of a
290                  * pixel repetition in a line
291                  */
292                 for (j = 0; j < sca_mult * vsca->bpp; j += vsca->bpp) {
293                         dev_dbg(vsca->dev,
294                                 "sca: %s: sca: scale_pix src pos %d\n",
295                                 vsca->sd.name, index + j);
296
297                         /* copy the pixel to the position index + j */
298                         vimc_sca_fill_pix(&vsca->src_frame[index + j],
299                                           pixel, vsca->bpp);
300                 }
301
302                 /* move the index to the next line */
303                 index += vsca->src_line_size;
304         }
305 }
306
307 static void vimc_sca_fill_src_frame(const struct vimc_sca_device *const vsca,
308                                     const u8 *const sink_frame)
309 {
310         unsigned int i, j;
311
312         /* Scale each pixel from the original sink frame */
313         /* TODO: implement scale down, only scale up is supported for now */
314         for (i = 0; i < vsca->sink_fmt.height; i++)
315                 for (j = 0; j < vsca->sink_fmt.width; j++)
316                         vimc_sca_scale_pix(vsca, i, j, sink_frame);
317 }
318
319 static void *vimc_sca_process_frame(struct vimc_ent_device *ved,
320                                     const void *sink_frame)
321 {
322         struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
323                                                     ved);
324
325         /* If the stream in this node is not active, just return */
326         if (!vsca->src_frame)
327                 return ERR_PTR(-EINVAL);
328
329         vimc_sca_fill_src_frame(vsca, sink_frame);
330
331         return vsca->src_frame;
332 };
333
334 static void vimc_sca_release(struct v4l2_subdev *sd)
335 {
336         struct vimc_sca_device *vsca =
337                                 container_of(sd, struct vimc_sca_device, sd);
338
339         vimc_pads_cleanup(vsca->ved.pads);
340         kfree(vsca);
341 }
342
343 static const struct v4l2_subdev_internal_ops vimc_sca_int_ops = {
344         .release = vimc_sca_release,
345 };
346
347 void vimc_sca_rm(struct vimc_device *vimc, struct vimc_ent_device *ved)
348 {
349         struct vimc_sca_device *vsca;
350
351         vsca = container_of(ved, struct vimc_sca_device, ved);
352         vimc_ent_sd_unregister(ved, &vsca->sd);
353 }
354
355 struct vimc_ent_device *vimc_sca_add(struct vimc_device *vimc,
356                                      const char *vcfg_name)
357 {
358         struct v4l2_device *v4l2_dev = &vimc->v4l2_dev;
359         struct vimc_sca_device *vsca;
360         int ret;
361
362         /* Allocate the vsca struct */
363         vsca = kzalloc(sizeof(*vsca), GFP_KERNEL);
364         if (!vsca)
365                 return NULL;
366
367         /* Initialize ved and sd */
368         ret = vimc_ent_sd_register(&vsca->ved, &vsca->sd, v4l2_dev,
369                                    vcfg_name,
370                                    MEDIA_ENT_F_PROC_VIDEO_SCALER, 2,
371                                    (const unsigned long[2]) {MEDIA_PAD_FL_SINK,
372                                    MEDIA_PAD_FL_SOURCE},
373                                    &vimc_sca_int_ops, &vimc_sca_ops);
374         if (ret) {
375                 kfree(vsca);
376                 return NULL;
377         }
378
379         vsca->ved.process_frame = vimc_sca_process_frame;
380         vsca->dev = &vimc->pdev.dev;
381
382         /* Initialize the frame format */
383         vsca->sink_fmt = sink_fmt_default;
384
385         return &vsca->ved;
386 }