1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * vimc-scaler.c Virtual Media Controller Driver
5 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
8 #include <linux/component.h>
9 #include <linux/module.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/platform_device.h>
12 #include <linux/vmalloc.h>
13 #include <linux/v4l2-mediabus.h>
14 #include <media/v4l2-subdev.h>
16 #include "vimc-common.h"
18 #define VIMC_SCA_DRV_NAME "vimc-scaler"
20 static unsigned int sca_mult = 3;
21 module_param(sca_mult, uint, 0000);
22 MODULE_PARM_DESC(sca_mult, " the image size multiplier");
24 #define IS_SINK(pad) (!pad)
25 #define IS_SRC(pad) (pad)
28 static const u32 vimc_sca_supported_pixfmt[] = {
34 struct vimc_sca_device {
35 struct vimc_ent_device ved;
36 struct v4l2_subdev sd;
38 /* NOTE: the source fmt is the same as the sink
39 * with the width and hight multiplied by mult
41 struct v4l2_mbus_framefmt sink_fmt;
42 /* Values calculated when the stream starts */
44 unsigned int src_line_size;
48 static const struct v4l2_mbus_framefmt sink_fmt_default = {
51 .code = MEDIA_BUS_FMT_RGB888_1X24,
52 .field = V4L2_FIELD_NONE,
53 .colorspace = V4L2_COLORSPACE_DEFAULT,
56 static bool vimc_sca_is_pixfmt_supported(u32 pixelformat)
60 for (i = 0; i < ARRAY_SIZE(vimc_sca_supported_pixfmt); i++)
61 if (vimc_sca_supported_pixfmt[i] == pixelformat)
66 static int vimc_sca_init_cfg(struct v4l2_subdev *sd,
67 struct v4l2_subdev_pad_config *cfg)
69 struct v4l2_mbus_framefmt *mf;
72 mf = v4l2_subdev_get_try_format(sd, cfg, 0);
73 *mf = sink_fmt_default;
75 for (i = 1; i < sd->entity.num_pads; i++) {
76 mf = v4l2_subdev_get_try_format(sd, cfg, i);
77 *mf = sink_fmt_default;
78 mf->width = mf->width * sca_mult;
79 mf->height = mf->height * sca_mult;
85 static int vimc_sca_enum_frame_size(struct v4l2_subdev *sd,
86 struct v4l2_subdev_pad_config *cfg,
87 struct v4l2_subdev_frame_size_enum *fse)
92 fse->min_width = VIMC_FRAME_MIN_WIDTH;
93 fse->min_height = VIMC_FRAME_MIN_HEIGHT;
95 if (IS_SINK(fse->pad)) {
96 fse->max_width = VIMC_FRAME_MAX_WIDTH;
97 fse->max_height = VIMC_FRAME_MAX_HEIGHT;
99 fse->max_width = VIMC_FRAME_MAX_WIDTH * MAX_ZOOM;
100 fse->max_height = VIMC_FRAME_MAX_HEIGHT * MAX_ZOOM;
106 static int vimc_sca_get_fmt(struct v4l2_subdev *sd,
107 struct v4l2_subdev_pad_config *cfg,
108 struct v4l2_subdev_format *format)
110 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
112 /* Get the current sink format */
113 format->format = (format->which == V4L2_SUBDEV_FORMAT_TRY) ?
114 *v4l2_subdev_get_try_format(sd, cfg, 0) :
117 /* Scale the frame size for the source pad */
118 if (IS_SRC(format->pad)) {
119 format->format.width = vsca->sink_fmt.width * sca_mult;
120 format->format.height = vsca->sink_fmt.height * sca_mult;
126 static void vimc_sca_adjust_sink_fmt(struct v4l2_mbus_framefmt *fmt)
128 fmt->width = clamp_t(u32, fmt->width, VIMC_FRAME_MIN_WIDTH,
129 VIMC_FRAME_MAX_WIDTH) & ~1;
130 fmt->height = clamp_t(u32, fmt->height, VIMC_FRAME_MIN_HEIGHT,
131 VIMC_FRAME_MAX_HEIGHT) & ~1;
133 if (fmt->field == V4L2_FIELD_ANY)
134 fmt->field = sink_fmt_default.field;
136 vimc_colorimetry_clamp(fmt);
139 static int vimc_sca_set_fmt(struct v4l2_subdev *sd,
140 struct v4l2_subdev_pad_config *cfg,
141 struct v4l2_subdev_format *fmt)
143 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
144 struct v4l2_mbus_framefmt *sink_fmt;
146 if (!vimc_mbus_code_supported(fmt->format.code))
147 fmt->format.code = sink_fmt_default.code;
149 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
150 /* Do not change the format while stream is on */
154 sink_fmt = &vsca->sink_fmt;
156 sink_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
160 * Do not change the format of the source pad,
161 * it is propagated from the sink
163 if (IS_SRC(fmt->pad)) {
164 fmt->format = *sink_fmt;
165 fmt->format.width = sink_fmt->width * sca_mult;
166 fmt->format.height = sink_fmt->height * sca_mult;
168 /* Set the new format in the sink pad */
169 vimc_sca_adjust_sink_fmt(&fmt->format);
171 dev_dbg(vsca->dev, "%s: sink format update: "
172 "old:%dx%d (0x%x, %d, %d, %d, %d) "
173 "new:%dx%d (0x%x, %d, %d, %d, %d)\n", vsca->sd.name,
175 sink_fmt->width, sink_fmt->height, sink_fmt->code,
176 sink_fmt->colorspace, sink_fmt->quantization,
177 sink_fmt->xfer_func, sink_fmt->ycbcr_enc,
179 fmt->format.width, fmt->format.height, fmt->format.code,
180 fmt->format.colorspace, fmt->format.quantization,
181 fmt->format.xfer_func, fmt->format.ycbcr_enc);
183 *sink_fmt = fmt->format;
189 static const struct v4l2_subdev_pad_ops vimc_sca_pad_ops = {
190 .init_cfg = vimc_sca_init_cfg,
191 .enum_mbus_code = vimc_enum_mbus_code,
192 .enum_frame_size = vimc_sca_enum_frame_size,
193 .get_fmt = vimc_sca_get_fmt,
194 .set_fmt = vimc_sca_set_fmt,
197 static int vimc_sca_s_stream(struct v4l2_subdev *sd, int enable)
199 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
202 u32 pixelformat = vsca->ved.stream->producer_pixfmt;
203 const struct v4l2_format_info *pix_info;
204 unsigned int frame_size;
209 if (!vimc_sca_is_pixfmt_supported(pixelformat)) {
210 dev_err(vsca->dev, "pixfmt (0x%08x) is not supported\n",
215 /* Save the bytes per pixel of the sink */
216 pix_info = v4l2_format_info(pixelformat);
217 vsca->bpp = pix_info->bpp[0];
219 /* Calculate the width in bytes of the src frame */
220 vsca->src_line_size = vsca->sink_fmt.width *
221 sca_mult * vsca->bpp;
223 /* Calculate the frame size of the source pad */
224 frame_size = vsca->src_line_size * vsca->sink_fmt.height *
227 /* Allocate the frame buffer. Use vmalloc to be able to
228 * allocate a large amount of memory
230 vsca->src_frame = vmalloc(frame_size);
231 if (!vsca->src_frame)
235 if (!vsca->src_frame)
238 vfree(vsca->src_frame);
239 vsca->src_frame = NULL;
245 static const struct v4l2_subdev_video_ops vimc_sca_video_ops = {
246 .s_stream = vimc_sca_s_stream,
249 static const struct v4l2_subdev_ops vimc_sca_ops = {
250 .pad = &vimc_sca_pad_ops,
251 .video = &vimc_sca_video_ops,
254 static void vimc_sca_fill_pix(u8 *const ptr,
255 const u8 *const pixel,
256 const unsigned int bpp)
260 /* copy the pixel to the pointer */
261 for (i = 0; i < bpp; i++)
265 static void vimc_sca_scale_pix(const struct vimc_sca_device *const vsca,
266 const unsigned int lin, const unsigned int col,
267 const u8 *const sink_frame)
269 unsigned int i, j, index;
272 /* Point to the pixel value in position (lin, col) in the sink frame */
273 index = VIMC_FRAME_INDEX(lin, col,
274 vsca->sink_fmt.width,
276 pixel = &sink_frame[index];
279 "sca: %s: --- scale_pix sink pos %dx%d, index %d ---\n",
280 vsca->sd.name, lin, col, index);
282 /* point to the place we are going to put the first pixel
283 * in the scaled src frame
285 index = VIMC_FRAME_INDEX(lin * sca_mult, col * sca_mult,
286 vsca->sink_fmt.width * sca_mult, vsca->bpp);
288 dev_dbg(vsca->dev, "sca: %s: scale_pix src pos %dx%d, index %d\n",
289 vsca->sd.name, lin * sca_mult, col * sca_mult, index);
291 /* Repeat this pixel mult times */
292 for (i = 0; i < sca_mult; i++) {
293 /* Iterate through each beginning of a
294 * pixel repetition in a line
296 for (j = 0; j < sca_mult * vsca->bpp; j += vsca->bpp) {
298 "sca: %s: sca: scale_pix src pos %d\n",
299 vsca->sd.name, index + j);
301 /* copy the pixel to the position index + j */
302 vimc_sca_fill_pix(&vsca->src_frame[index + j],
306 /* move the index to the next line */
307 index += vsca->src_line_size;
311 static void vimc_sca_fill_src_frame(const struct vimc_sca_device *const vsca,
312 const u8 *const sink_frame)
316 /* Scale each pixel from the original sink frame */
317 /* TODO: implement scale down, only scale up is supported for now */
318 for (i = 0; i < vsca->sink_fmt.height; i++)
319 for (j = 0; j < vsca->sink_fmt.width; j++)
320 vimc_sca_scale_pix(vsca, i, j, sink_frame);
323 static void *vimc_sca_process_frame(struct vimc_ent_device *ved,
324 const void *sink_frame)
326 struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
329 /* If the stream in this node is not active, just return */
330 if (!vsca->src_frame)
331 return ERR_PTR(-EINVAL);
333 vimc_sca_fill_src_frame(vsca, sink_frame);
335 return vsca->src_frame;
338 static void vimc_sca_release(struct v4l2_subdev *sd)
340 struct vimc_sca_device *vsca =
341 container_of(sd, struct vimc_sca_device, sd);
346 static const struct v4l2_subdev_internal_ops vimc_sca_int_ops = {
347 .release = vimc_sca_release,
350 static void vimc_sca_comp_unbind(struct device *comp, struct device *master,
353 struct vimc_ent_device *ved = dev_get_drvdata(comp);
354 struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
357 vimc_ent_sd_unregister(ved, &vsca->sd);
361 static int vimc_sca_comp_bind(struct device *comp, struct device *master,
364 struct v4l2_device *v4l2_dev = master_data;
365 struct vimc_platform_data *pdata = comp->platform_data;
366 struct vimc_sca_device *vsca;
369 /* Allocate the vsca struct */
370 vsca = kzalloc(sizeof(*vsca), GFP_KERNEL);
374 /* Initialize ved and sd */
375 ret = vimc_ent_sd_register(&vsca->ved, &vsca->sd, v4l2_dev,
377 MEDIA_ENT_F_PROC_VIDEO_SCALER, 2,
378 (const unsigned long[2]) {MEDIA_PAD_FL_SINK,
379 MEDIA_PAD_FL_SOURCE},
380 &vimc_sca_int_ops, &vimc_sca_ops);
386 vsca->ved.process_frame = vimc_sca_process_frame;
387 dev_set_drvdata(comp, &vsca->ved);
390 /* Initialize the frame format */
391 vsca->sink_fmt = sink_fmt_default;
396 static const struct component_ops vimc_sca_comp_ops = {
397 .bind = vimc_sca_comp_bind,
398 .unbind = vimc_sca_comp_unbind,
401 static int vimc_sca_probe(struct platform_device *pdev)
403 return component_add(&pdev->dev, &vimc_sca_comp_ops);
406 static int vimc_sca_remove(struct platform_device *pdev)
408 component_del(&pdev->dev, &vimc_sca_comp_ops);
413 static const struct platform_device_id vimc_sca_driver_ids[] = {
415 .name = VIMC_SCA_DRV_NAME,
420 static struct platform_driver vimc_sca_pdrv = {
421 .probe = vimc_sca_probe,
422 .remove = vimc_sca_remove,
423 .id_table = vimc_sca_driver_ids,
425 .name = VIMC_SCA_DRV_NAME,
429 module_platform_driver(vimc_sca_pdrv);
431 MODULE_DEVICE_TABLE(platform, vimc_sca_driver_ids);
433 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Scaler");
434 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
435 MODULE_LICENSE("GPL");