]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/media/platform/vimc/vimc-scaler.c
Merge tag 'for-linus-5.2-rc1' of ssh://gitolite.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / platform / vimc / vimc-scaler.c
1 /*
2 * vimc-scaler.c Virtual Media Controller Driver
3 *
4 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18 #include <linux/component.h>
19 #include <linux/module.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/platform_device.h>
22 #include <linux/vmalloc.h>
23 #include <linux/v4l2-mediabus.h>
24 #include <media/v4l2-subdev.h>
25
26 #include "vimc-common.h"
27
28 #define VIMC_SCA_DRV_NAME "vimc-scaler"
29
30 static unsigned int sca_mult = 3;
31 module_param(sca_mult, uint, 0000);
32 MODULE_PARM_DESC(sca_mult, " the image size multiplier");
33
34 #define IS_SINK(pad) (!pad)
35 #define IS_SRC(pad) (pad)
36 #define MAX_ZOOM 8
37
38 static const u32 vimc_sca_supported_pixfmt[] = {
39 V4L2_PIX_FMT_BGR24,
40 V4L2_PIX_FMT_RGB24,
41 V4L2_PIX_FMT_ARGB32,
42 };
43
44 struct vimc_sca_device {
45 struct vimc_ent_device ved;
46 struct v4l2_subdev sd;
47 struct device *dev;
48 /* NOTE: the source fmt is the same as the sink
49 * with the width and hight multiplied by mult
50 */
51 struct v4l2_mbus_framefmt sink_fmt;
52 /* Values calculated when the stream starts */
53 u8 *src_frame;
54 unsigned int src_line_size;
55 unsigned int bpp;
56 };
57
58 static const struct v4l2_mbus_framefmt sink_fmt_default = {
59 .width = 640,
60 .height = 480,
61 .code = MEDIA_BUS_FMT_RGB888_1X24,
62 .field = V4L2_FIELD_NONE,
63 .colorspace = V4L2_COLORSPACE_DEFAULT,
64 };
65
66 static bool vimc_sca_is_pixfmt_supported(u32 pixelformat)
67 {
68 unsigned int i;
69
70 for (i = 0; i < ARRAY_SIZE(vimc_sca_supported_pixfmt); i++)
71 if (vimc_sca_supported_pixfmt[i] == pixelformat)
72 return true;
73 return false;
74 }
75
76 static int vimc_sca_init_cfg(struct v4l2_subdev *sd,
77 struct v4l2_subdev_pad_config *cfg)
78 {
79 struct v4l2_mbus_framefmt *mf;
80 unsigned int i;
81
82 mf = v4l2_subdev_get_try_format(sd, cfg, 0);
83 *mf = sink_fmt_default;
84
85 for (i = 1; i < sd->entity.num_pads; i++) {
86 mf = v4l2_subdev_get_try_format(sd, cfg, i);
87 *mf = sink_fmt_default;
88 mf->width = mf->width * sca_mult;
89 mf->height = mf->height * sca_mult;
90 }
91
92 return 0;
93 }
94
95 static int vimc_sca_enum_frame_size(struct v4l2_subdev *sd,
96 struct v4l2_subdev_pad_config *cfg,
97 struct v4l2_subdev_frame_size_enum *fse)
98 {
99 if (fse->index)
100 return -EINVAL;
101
102 fse->min_width = VIMC_FRAME_MIN_WIDTH;
103 fse->min_height = VIMC_FRAME_MIN_HEIGHT;
104
105 if (IS_SINK(fse->pad)) {
106 fse->max_width = VIMC_FRAME_MAX_WIDTH;
107 fse->max_height = VIMC_FRAME_MAX_HEIGHT;
108 } else {
109 fse->max_width = VIMC_FRAME_MAX_WIDTH * MAX_ZOOM;
110 fse->max_height = VIMC_FRAME_MAX_HEIGHT * MAX_ZOOM;
111 }
112
113 return 0;
114 }
115
116 static int vimc_sca_get_fmt(struct v4l2_subdev *sd,
117 struct v4l2_subdev_pad_config *cfg,
118 struct v4l2_subdev_format *format)
119 {
120 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
121
122 /* Get the current sink format */
123 format->format = (format->which == V4L2_SUBDEV_FORMAT_TRY) ?
124 *v4l2_subdev_get_try_format(sd, cfg, 0) :
125 vsca->sink_fmt;
126
127 /* Scale the frame size for the source pad */
128 if (IS_SRC(format->pad)) {
129 format->format.width = vsca->sink_fmt.width * sca_mult;
130 format->format.height = vsca->sink_fmt.height * sca_mult;
131 }
132
133 return 0;
134 }
135
136 static void vimc_sca_adjust_sink_fmt(struct v4l2_mbus_framefmt *fmt)
137 {
138 fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
139 VIMC_FRAME_MAX_WIDTH) & ~1;
140 fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
141 VIMC_FRAME_MAX_HEIGHT) & ~1;
142
143 if (fmt->field == V4L2_FIELD_ANY)
144 fmt->field = sink_fmt_default.field;
145
146 vimc_colorimetry_clamp(fmt);
147 }
148
149 static int vimc_sca_set_fmt(struct v4l2_subdev *sd,
150 struct v4l2_subdev_pad_config *cfg,
151 struct v4l2_subdev_format *fmt)
152 {
153 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
154 struct v4l2_mbus_framefmt *sink_fmt;
155
156 if (!vimc_mbus_code_supported(fmt->format.code))
157 fmt->format.code = sink_fmt_default.code;
158
159 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
160 /* Do not change the format while stream is on */
161 if (vsca->src_frame)
162 return -EBUSY;
163
164 sink_fmt = &vsca->sink_fmt;
165 } else {
166 sink_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
167 }
168
169 /*
170 * Do not change the format of the source pad,
171 * it is propagated from the sink
172 */
173 if (IS_SRC(fmt->pad)) {
174 fmt->format = *sink_fmt;
175 fmt->format.width = sink_fmt->width * sca_mult;
176 fmt->format.height = sink_fmt->height * sca_mult;
177 } else {
178 /* Set the new format in the sink pad */
179 vimc_sca_adjust_sink_fmt(&fmt->format);
180
181 dev_dbg(vsca->dev, "%s: sink format update: "
182 "old:%dx%d (0x%x, %d, %d, %d, %d) "
183 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsca->sd.name,
184 /* old */
185 sink_fmt->width, sink_fmt->height, sink_fmt->code,
186 sink_fmt->colorspace, sink_fmt->quantization,
187 sink_fmt->xfer_func, sink_fmt->ycbcr_enc,
188 /* new */
189 fmt->format.width, fmt->format.height, fmt->format.code,
190 fmt->format.colorspace, fmt->format.quantization,
191 fmt->format.xfer_func, fmt->format.ycbcr_enc);
192
193 *sink_fmt = fmt->format;
194 }
195
196 return 0;
197 }
198
199 static const struct v4l2_subdev_pad_ops vimc_sca_pad_ops = {
200 .init_cfg = vimc_sca_init_cfg,
201 .enum_mbus_code = vimc_enum_mbus_code,
202 .enum_frame_size = vimc_sca_enum_frame_size,
203 .get_fmt = vimc_sca_get_fmt,
204 .set_fmt = vimc_sca_set_fmt,
205 };
206
207 static int vimc_sca_s_stream(struct v4l2_subdev *sd, int enable)
208 {
209 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
210
211 if (enable) {
212 u32 pixelformat = vsca->ved.stream->producer_pixfmt;
213 const struct v4l2_format_info *pix_info;
214 unsigned int frame_size;
215
216 if (vsca->src_frame)
217 return 0;
218
219 if (!vimc_sca_is_pixfmt_supported(pixelformat)) {
220 dev_err(vsca->dev, "pixfmt (0x%08x) is not supported\n",
221 pixelformat);
222 return -EINVAL;
223 }
224
225 /* Save the bytes per pixel of the sink */
226 pix_info = v4l2_format_info(pixelformat);
227 vsca->bpp = pix_info->bpp[0];
228
229 /* Calculate the width in bytes of the src frame */
230 vsca->src_line_size = vsca->sink_fmt.width *
231 sca_mult * vsca->bpp;
232
233 /* Calculate the frame size of the source pad */
234 frame_size = vsca->src_line_size * vsca->sink_fmt.height *
235 sca_mult;
236
237 /* Allocate the frame buffer. Use vmalloc to be able to
238 * allocate a large amount of memory
239 */
240 vsca->src_frame = vmalloc(frame_size);
241 if (!vsca->src_frame)
242 return -ENOMEM;
243
244 } else {
245 if (!vsca->src_frame)
246 return 0;
247
248 vfree(vsca->src_frame);
249 vsca->src_frame = NULL;
250 }
251
252 return 0;
253 }
254
255 static const struct v4l2_subdev_video_ops vimc_sca_video_ops = {
256 .s_stream = vimc_sca_s_stream,
257 };
258
259 static const struct v4l2_subdev_ops vimc_sca_ops = {
260 .pad = &vimc_sca_pad_ops,
261 .video = &vimc_sca_video_ops,
262 };
263
264 static void vimc_sca_fill_pix(u8 *const ptr,
265 const u8 *const pixel,
266 const unsigned int bpp)
267 {
268 unsigned int i;
269
270 /* copy the pixel to the pointer */
271 for (i = 0; i < bpp; i++)
272 ptr[i] = pixel[i];
273 }
274
275 static void vimc_sca_scale_pix(const struct vimc_sca_device *const vsca,
276 const unsigned int lin, const unsigned int col,
277 const u8 *const sink_frame)
278 {
279 unsigned int i, j, index;
280 const u8 *pixel;
281
282 /* Point to the pixel value in position (lin, col) in the sink frame */
283 index = VIMC_FRAME_INDEX(lin, col,
284 vsca->sink_fmt.width,
285 vsca->bpp);
286 pixel = &sink_frame[index];
287
288 dev_dbg(vsca->dev,
289 "sca: %s: --- scale_pix sink pos %dx%d, index %d ---\n",
290 vsca->sd.name, lin, col, index);
291
292 /* point to the place we are going to put the first pixel
293 * in the scaled src frame
294 */
295 index = VIMC_FRAME_INDEX(lin * sca_mult, col * sca_mult,
296 vsca->sink_fmt.width * sca_mult, vsca->bpp);
297
298 dev_dbg(vsca->dev, "sca: %s: scale_pix src pos %dx%d, index %d\n",
299 vsca->sd.name, lin * sca_mult, col * sca_mult, index);
300
301 /* Repeat this pixel mult times */
302 for (i = 0; i < sca_mult; i++) {
303 /* Iterate through each beginning of a
304 * pixel repetition in a line
305 */
306 for (j = 0; j < sca_mult * vsca->bpp; j += vsca->bpp) {
307 dev_dbg(vsca->dev,
308 "sca: %s: sca: scale_pix src pos %d\n",
309 vsca->sd.name, index + j);
310
311 /* copy the pixel to the position index + j */
312 vimc_sca_fill_pix(&vsca->src_frame[index + j],
313 pixel, vsca->bpp);
314 }
315
316 /* move the index to the next line */
317 index += vsca->src_line_size;
318 }
319 }
320
321 static void vimc_sca_fill_src_frame(const struct vimc_sca_device *const vsca,
322 const u8 *const sink_frame)
323 {
324 unsigned int i, j;
325
326 /* Scale each pixel from the original sink frame */
327 /* TODO: implement scale down, only scale up is supported for now */
328 for (i = 0; i < vsca->sink_fmt.height; i++)
329 for (j = 0; j < vsca->sink_fmt.width; j++)
330 vimc_sca_scale_pix(vsca, i, j, sink_frame);
331 }
332
333 static void *vimc_sca_process_frame(struct vimc_ent_device *ved,
334 const void *sink_frame)
335 {
336 struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
337 ved);
338
339 /* If the stream in this node is not active, just return */
340 if (!vsca->src_frame)
341 return ERR_PTR(-EINVAL);
342
343 vimc_sca_fill_src_frame(vsca, sink_frame);
344
345 return vsca->src_frame;
346 };
347
348 static void vimc_sca_release(struct v4l2_subdev *sd)
349 {
350 struct vimc_sca_device *vsca =
351 container_of(sd, struct vimc_sca_device, sd);
352
353 kfree(vsca);
354 }
355
356 static const struct v4l2_subdev_internal_ops vimc_sca_int_ops = {
357 .release = vimc_sca_release,
358 };
359
360 static void vimc_sca_comp_unbind(struct device *comp, struct device *master,
361 void *master_data)
362 {
363 struct vimc_ent_device *ved = dev_get_drvdata(comp);
364 struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
365 ved);
366
367 vimc_ent_sd_unregister(ved, &vsca->sd);
368 }
369
370
371 static int vimc_sca_comp_bind(struct device *comp, struct device *master,
372 void *master_data)
373 {
374 struct v4l2_device *v4l2_dev = master_data;
375 struct vimc_platform_data *pdata = comp->platform_data;
376 struct vimc_sca_device *vsca;
377 int ret;
378
379 /* Allocate the vsca struct */
380 vsca = kzalloc(sizeof(*vsca), GFP_KERNEL);
381 if (!vsca)
382 return -ENOMEM;
383
384 /* Initialize ved and sd */
385 ret = vimc_ent_sd_register(&vsca->ved, &vsca->sd, v4l2_dev,
386 pdata->entity_name,
387 MEDIA_ENT_F_PROC_VIDEO_SCALER, 2,
388 (const unsigned long[2]) {MEDIA_PAD_FL_SINK,
389 MEDIA_PAD_FL_SOURCE},
390 &vimc_sca_int_ops, &vimc_sca_ops);
391 if (ret) {
392 kfree(vsca);
393 return ret;
394 }
395
396 vsca->ved.process_frame = vimc_sca_process_frame;
397 dev_set_drvdata(comp, &vsca->ved);
398 vsca->dev = comp;
399
400 /* Initialize the frame format */
401 vsca->sink_fmt = sink_fmt_default;
402
403 return 0;
404 }
405
406 static const struct component_ops vimc_sca_comp_ops = {
407 .bind = vimc_sca_comp_bind,
408 .unbind = vimc_sca_comp_unbind,
409 };
410
411 static int vimc_sca_probe(struct platform_device *pdev)
412 {
413 return component_add(&pdev->dev, &vimc_sca_comp_ops);
414 }
415
416 static int vimc_sca_remove(struct platform_device *pdev)
417 {
418 component_del(&pdev->dev, &vimc_sca_comp_ops);
419
420 return 0;
421 }
422
423 static const struct platform_device_id vimc_sca_driver_ids[] = {
424 {
425 .name = VIMC_SCA_DRV_NAME,
426 },
427 { }
428 };
429
430 static struct platform_driver vimc_sca_pdrv = {
431 .probe = vimc_sca_probe,
432 .remove = vimc_sca_remove,
433 .id_table = vimc_sca_driver_ids,
434 .driver = {
435 .name = VIMC_SCA_DRV_NAME,
436 },
437 };
438
439 module_platform_driver(vimc_sca_pdrv);
440
441 MODULE_DEVICE_TABLE(platform, vimc_sca_driver_ids);
442
443 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Scaler");
444 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
445 MODULE_LICENSE("GPL");