blob: 2028afa4ef7ad8249850d8662872dcbdbbd2d5d3 [file] [log] [blame]
Helen Fornazier6856ba72017-06-19 14:00:20 -03001/*
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>
Randy Dunlapac316722018-06-19 22:47:28 -070020#include <linux/mod_devicetable.h>
Helen Fornazier6856ba72017-06-19 14:00:20 -030021#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
30static unsigned int sca_mult = 3;
31module_param(sca_mult, uint, 0000);
32MODULE_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
38struct vimc_sca_device {
39 struct vimc_ent_device ved;
40 struct v4l2_subdev sd;
41 struct device *dev;
42 /* NOTE: the source fmt is the same as the sink
43 * with the width and hight multiplied by mult
44 */
45 struct v4l2_mbus_framefmt sink_fmt;
46 /* Values calculated when the stream starts */
47 u8 *src_frame;
48 unsigned int src_line_size;
49 unsigned int bpp;
50};
51
52static const struct v4l2_mbus_framefmt sink_fmt_default = {
53 .width = 640,
54 .height = 480,
55 .code = MEDIA_BUS_FMT_RGB888_1X24,
56 .field = V4L2_FIELD_NONE,
57 .colorspace = V4L2_COLORSPACE_DEFAULT,
58};
59
60static int vimc_sca_init_cfg(struct v4l2_subdev *sd,
61 struct v4l2_subdev_pad_config *cfg)
62{
63 struct v4l2_mbus_framefmt *mf;
64 unsigned int i;
65
66 mf = v4l2_subdev_get_try_format(sd, cfg, 0);
67 *mf = sink_fmt_default;
68
69 for (i = 1; i < sd->entity.num_pads; i++) {
70 mf = v4l2_subdev_get_try_format(sd, cfg, i);
71 *mf = sink_fmt_default;
72 mf->width = mf->width * sca_mult;
73 mf->height = mf->height * sca_mult;
74 }
75
76 return 0;
77}
78
79static int vimc_sca_enum_mbus_code(struct v4l2_subdev *sd,
80 struct v4l2_subdev_pad_config *cfg,
81 struct v4l2_subdev_mbus_code_enum *code)
82{
83 const struct vimc_pix_map *vpix = vimc_pix_map_by_index(code->index);
84
85 /* We don't support bayer format */
86 if (!vpix || vpix->bayer)
87 return -EINVAL;
88
89 code->code = vpix->code;
90
91 return 0;
92}
93
94static int vimc_sca_enum_frame_size(struct v4l2_subdev *sd,
95 struct v4l2_subdev_pad_config *cfg,
96 struct v4l2_subdev_frame_size_enum *fse)
97{
98 const struct vimc_pix_map *vpix;
99
100 if (fse->index)
101 return -EINVAL;
102
103 /* Only accept code in the pix map table in non bayer format */
104 vpix = vimc_pix_map_by_code(fse->code);
105 if (!vpix || vpix->bayer)
106 return -EINVAL;
107
108 fse->min_width = VIMC_FRAME_MIN_WIDTH;
109 fse->min_height = VIMC_FRAME_MIN_HEIGHT;
110
111 if (IS_SINK(fse->pad)) {
112 fse->max_width = VIMC_FRAME_MAX_WIDTH;
113 fse->max_height = VIMC_FRAME_MAX_HEIGHT;
114 } else {
115 fse->max_width = VIMC_FRAME_MAX_WIDTH * MAX_ZOOM;
116 fse->max_height = VIMC_FRAME_MAX_HEIGHT * MAX_ZOOM;
117 }
118
119 return 0;
120}
121
122static int vimc_sca_get_fmt(struct v4l2_subdev *sd,
123 struct v4l2_subdev_pad_config *cfg,
124 struct v4l2_subdev_format *format)
125{
126 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
127
128 /* Get the current sink format */
129 format->format = (format->which == V4L2_SUBDEV_FORMAT_TRY) ?
130 *v4l2_subdev_get_try_format(sd, cfg, 0) :
131 vsca->sink_fmt;
132
133 /* Scale the frame size for the source pad */
134 if (IS_SRC(format->pad)) {
135 format->format.width = vsca->sink_fmt.width * sca_mult;
136 format->format.height = vsca->sink_fmt.height * sca_mult;
137 }
138
139 return 0;
140}
141
142static void vimc_sca_adjust_sink_fmt(struct v4l2_mbus_framefmt *fmt)
143{
144 const struct vimc_pix_map *vpix;
145
146 /* Only accept code in the pix map table in non bayer format */
147 vpix = vimc_pix_map_by_code(fmt->code);
148 if (!vpix || vpix->bayer)
149 fmt->code = sink_fmt_default.code;
150
151 fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
152 VIMC_FRAME_MAX_WIDTH) & ~1;
153 fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
154 VIMC_FRAME_MAX_HEIGHT) & ~1;
155
156 if (fmt->field == V4L2_FIELD_ANY)
157 fmt->field = sink_fmt_default.field;
158
159 vimc_colorimetry_clamp(fmt);
160}
161
162static int vimc_sca_set_fmt(struct v4l2_subdev *sd,
163 struct v4l2_subdev_pad_config *cfg,
164 struct v4l2_subdev_format *fmt)
165{
166 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
167 struct v4l2_mbus_framefmt *sink_fmt;
168
169 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
170 /* Do not change the format while stream is on */
171 if (vsca->src_frame)
172 return -EBUSY;
173
174 sink_fmt = &vsca->sink_fmt;
175 } else {
176 sink_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
177 }
178
179 /*
180 * Do not change the format of the source pad,
181 * it is propagated from the sink
182 */
183 if (IS_SRC(fmt->pad)) {
184 fmt->format = *sink_fmt;
185 fmt->format.width = sink_fmt->width * sca_mult;
186 fmt->format.height = sink_fmt->height * sca_mult;
187 } else {
188 /* Set the new format in the sink pad */
189 vimc_sca_adjust_sink_fmt(&fmt->format);
190
191 dev_dbg(vsca->dev, "%s: sink format update: "
192 "old:%dx%d (0x%x, %d, %d, %d, %d) "
193 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsca->sd.name,
194 /* old */
195 sink_fmt->width, sink_fmt->height, sink_fmt->code,
196 sink_fmt->colorspace, sink_fmt->quantization,
197 sink_fmt->xfer_func, sink_fmt->ycbcr_enc,
198 /* new */
199 fmt->format.width, fmt->format.height, fmt->format.code,
200 fmt->format.colorspace, fmt->format.quantization,
201 fmt->format.xfer_func, fmt->format.ycbcr_enc);
202
203 *sink_fmt = fmt->format;
204 }
205
206 return 0;
207}
208
209static const struct v4l2_subdev_pad_ops vimc_sca_pad_ops = {
210 .init_cfg = vimc_sca_init_cfg,
211 .enum_mbus_code = vimc_sca_enum_mbus_code,
212 .enum_frame_size = vimc_sca_enum_frame_size,
213 .get_fmt = vimc_sca_get_fmt,
214 .set_fmt = vimc_sca_set_fmt,
215};
216
217static int vimc_sca_s_stream(struct v4l2_subdev *sd, int enable)
218{
219 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
Helen Fornazier6856ba72017-06-19 14:00:20 -0300220
221 if (enable) {
222 const struct vimc_pix_map *vpix;
223 unsigned int frame_size;
224
225 if (vsca->src_frame)
226 return 0;
227
228 /* Save the bytes per pixel of the sink */
229 vpix = vimc_pix_map_by_code(vsca->sink_fmt.code);
230 vsca->bpp = vpix->bpp;
231
232 /* Calculate the width in bytes of the src frame */
233 vsca->src_line_size = vsca->sink_fmt.width *
234 sca_mult * vsca->bpp;
235
236 /* Calculate the frame size of the source pad */
237 frame_size = vsca->src_line_size * vsca->sink_fmt.height *
238 sca_mult;
239
240 /* Allocate the frame buffer. Use vmalloc to be able to
241 * allocate a large amount of memory
242 */
243 vsca->src_frame = vmalloc(frame_size);
244 if (!vsca->src_frame)
245 return -ENOMEM;
246
Helen Fornazier6856ba72017-06-19 14:00:20 -0300247 } else {
248 if (!vsca->src_frame)
249 return 0;
250
Helen Fornazier6856ba72017-06-19 14:00:20 -0300251 vfree(vsca->src_frame);
252 vsca->src_frame = NULL;
253 }
254
255 return 0;
256}
257
Julia Lawallebf7a642017-08-08 06:58:28 -0400258static const struct v4l2_subdev_video_ops vimc_sca_video_ops = {
Helen Fornazier6856ba72017-06-19 14:00:20 -0300259 .s_stream = vimc_sca_s_stream,
260};
261
262static const struct v4l2_subdev_ops vimc_sca_ops = {
263 .pad = &vimc_sca_pad_ops,
264 .video = &vimc_sca_video_ops,
265};
266
267static void vimc_sca_fill_pix(u8 *const ptr,
268 const u8 *const pixel,
269 const unsigned int bpp)
270{
271 unsigned int i;
272
273 /* copy the pixel to the pointer */
274 for (i = 0; i < bpp; i++)
275 ptr[i] = pixel[i];
276}
277
278static void vimc_sca_scale_pix(const struct vimc_sca_device *const vsca,
279 const unsigned int lin, const unsigned int col,
280 const u8 *const sink_frame)
281{
282 unsigned int i, j, index;
283 const u8 *pixel;
284
285 /* Point to the pixel value in position (lin, col) in the sink frame */
286 index = VIMC_FRAME_INDEX(lin, col,
287 vsca->sink_fmt.width,
288 vsca->bpp);
289 pixel = &sink_frame[index];
290
291 dev_dbg(vsca->dev,
292 "sca: %s: --- scale_pix sink pos %dx%d, index %d ---\n",
293 vsca->sd.name, lin, col, index);
294
295 /* point to the place we are going to put the first pixel
296 * in the scaled src frame
297 */
298 index = VIMC_FRAME_INDEX(lin * sca_mult, col * sca_mult,
299 vsca->sink_fmt.width * sca_mult, vsca->bpp);
300
301 dev_dbg(vsca->dev, "sca: %s: scale_pix src pos %dx%d, index %d\n",
302 vsca->sd.name, lin * sca_mult, col * sca_mult, index);
303
304 /* Repeat this pixel mult times */
305 for (i = 0; i < sca_mult; i++) {
306 /* Iterate through each beginning of a
307 * pixel repetition in a line
308 */
309 for (j = 0; j < sca_mult * vsca->bpp; j += vsca->bpp) {
310 dev_dbg(vsca->dev,
311 "sca: %s: sca: scale_pix src pos %d\n",
312 vsca->sd.name, index + j);
313
314 /* copy the pixel to the position index + j */
315 vimc_sca_fill_pix(&vsca->src_frame[index + j],
316 pixel, vsca->bpp);
317 }
318
319 /* move the index to the next line */
320 index += vsca->src_line_size;
321 }
322}
323
324static void vimc_sca_fill_src_frame(const struct vimc_sca_device *const vsca,
325 const u8 *const sink_frame)
326{
327 unsigned int i, j;
328
329 /* Scale each pixel from the original sink frame */
330 /* TODO: implement scale down, only scale up is supported for now */
331 for (i = 0; i < vsca->sink_fmt.height; i++)
332 for (j = 0; j < vsca->sink_fmt.width; j++)
333 vimc_sca_scale_pix(vsca, i, j, sink_frame);
334}
335
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500336static void *vimc_sca_process_frame(struct vimc_ent_device *ved,
337 const void *sink_frame)
Helen Fornazier6856ba72017-06-19 14:00:20 -0300338{
339 struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
340 ved);
Helen Fornazier6856ba72017-06-19 14:00:20 -0300341
342 /* If the stream in this node is not active, just return */
343 if (!vsca->src_frame)
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500344 return ERR_PTR(-EINVAL);
Helen Fornazier6856ba72017-06-19 14:00:20 -0300345
346 vimc_sca_fill_src_frame(vsca, sink_frame);
347
Lucas A. M. Magalhãesadc589d2019-01-21 20:05:01 -0500348 return vsca->src_frame;
Helen Fornazier6856ba72017-06-19 14:00:20 -0300349};
350
Hans Verkuil2b177f22019-03-05 03:36:20 -0500351static void vimc_sca_release(struct v4l2_subdev *sd)
352{
353 struct vimc_sca_device *vsca =
354 container_of(sd, struct vimc_sca_device, sd);
355
356 kfree(vsca);
357}
358
359static const struct v4l2_subdev_internal_ops vimc_sca_int_ops = {
360 .release = vimc_sca_release,
361};
362
Helen Fornazier6856ba72017-06-19 14:00:20 -0300363static void vimc_sca_comp_unbind(struct device *comp, struct device *master,
364 void *master_data)
365{
366 struct vimc_ent_device *ved = dev_get_drvdata(comp);
367 struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
368 ved);
369
370 vimc_ent_sd_unregister(ved, &vsca->sd);
Helen Fornazier6856ba72017-06-19 14:00:20 -0300371}
372
373
374static int vimc_sca_comp_bind(struct device *comp, struct device *master,
375 void *master_data)
376{
377 struct v4l2_device *v4l2_dev = master_data;
378 struct vimc_platform_data *pdata = comp->platform_data;
379 struct vimc_sca_device *vsca;
380 int ret;
381
382 /* Allocate the vsca struct */
383 vsca = kzalloc(sizeof(*vsca), GFP_KERNEL);
384 if (!vsca)
385 return -ENOMEM;
386
387 /* Initialize ved and sd */
388 ret = vimc_ent_sd_register(&vsca->ved, &vsca->sd, v4l2_dev,
389 pdata->entity_name,
Hans Verkuil1ceda322018-02-07 12:06:30 -0500390 MEDIA_ENT_F_PROC_VIDEO_SCALER, 2,
Helen Fornazier6856ba72017-06-19 14:00:20 -0300391 (const unsigned long[2]) {MEDIA_PAD_FL_SINK,
392 MEDIA_PAD_FL_SOURCE},
Hans Verkuil2b177f22019-03-05 03:36:20 -0500393 &vimc_sca_int_ops, &vimc_sca_ops);
Helen Fornazier6856ba72017-06-19 14:00:20 -0300394 if (ret) {
395 kfree(vsca);
396 return ret;
397 }
398
399 vsca->ved.process_frame = vimc_sca_process_frame;
400 dev_set_drvdata(comp, &vsca->ved);
401 vsca->dev = comp;
402
403 /* Initialize the frame format */
404 vsca->sink_fmt = sink_fmt_default;
405
406 return 0;
407}
408
409static const struct component_ops vimc_sca_comp_ops = {
410 .bind = vimc_sca_comp_bind,
411 .unbind = vimc_sca_comp_unbind,
412};
413
414static int vimc_sca_probe(struct platform_device *pdev)
415{
416 return component_add(&pdev->dev, &vimc_sca_comp_ops);
417}
418
419static int vimc_sca_remove(struct platform_device *pdev)
420{
421 component_del(&pdev->dev, &vimc_sca_comp_ops);
422
423 return 0;
424}
425
Helen Fornazier6856ba72017-06-19 14:00:20 -0300426static const struct platform_device_id vimc_sca_driver_ids[] = {
427 {
428 .name = VIMC_SCA_DRV_NAME,
429 },
430 { }
431};
432
Javier Martinez Canillasbb3abbb2017-07-14 05:58:39 -0300433static struct platform_driver vimc_sca_pdrv = {
434 .probe = vimc_sca_probe,
435 .remove = vimc_sca_remove,
436 .id_table = vimc_sca_driver_ids,
437 .driver = {
438 .name = VIMC_SCA_DRV_NAME,
439 },
440};
441
Helen Fornazier6856ba72017-06-19 14:00:20 -0300442module_platform_driver(vimc_sca_pdrv);
443
444MODULE_DEVICE_TABLE(platform, vimc_sca_driver_ids);
445
446MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Scaler");
447MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
448MODULE_LICENSE("GPL");