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/moduleparam.h>
9 #include <linux/vmalloc.h>
10 #include <linux/v4l2-mediabus.h>
11 #include <media/v4l2-subdev.h>
13 #include "vimc-common.h"
15 static unsigned int sca_mult = 3;
16 module_param(sca_mult, uint, 0000);
17 MODULE_PARM_DESC(sca_mult, " the image size multiplier");
21 struct vimc_sca_device {
22 struct vimc_ent_device ved;
23 struct v4l2_subdev sd;
25 /* NOTE: the source fmt is the same as the sink
26 * with the width and hight multiplied by mult
28 struct v4l2_mbus_framefmt sink_fmt;
29 /* Values calculated when the stream starts */
31 unsigned int src_line_size;
35 static const struct v4l2_mbus_framefmt sink_fmt_default = {
38 .code = MEDIA_BUS_FMT_RGB888_1X24,
39 .field = V4L2_FIELD_NONE,
40 .colorspace = V4L2_COLORSPACE_DEFAULT,
43 static int vimc_sca_init_cfg(struct v4l2_subdev *sd,
44 struct v4l2_subdev_pad_config *cfg)
46 struct v4l2_mbus_framefmt *mf;
49 mf = v4l2_subdev_get_try_format(sd, cfg, 0);
50 *mf = sink_fmt_default;
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;
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)
66 const struct vimc_pix_map *vpix = vimc_pix_map_by_index(code->index);
68 /* We don't support bayer format */
69 if (!vpix || vpix->bayer)
72 code->code = vpix->code;
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)
81 const struct vimc_pix_map *vpix;
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)
91 fse->min_width = VIMC_FRAME_MIN_WIDTH;
92 fse->min_height = VIMC_FRAME_MIN_HEIGHT;
94 if (VIMC_IS_SINK(fse->pad)) {
95 fse->max_width = VIMC_FRAME_MAX_WIDTH;
96 fse->max_height = VIMC_FRAME_MAX_HEIGHT;
98 fse->max_width = VIMC_FRAME_MAX_WIDTH * MAX_ZOOM;
99 fse->max_height = VIMC_FRAME_MAX_HEIGHT * MAX_ZOOM;
105 static int vimc_sca_get_fmt(struct v4l2_subdev *sd,
106 struct v4l2_subdev_pad_config *cfg,
107 struct v4l2_subdev_format *format)
109 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
111 /* Get the current sink format */
112 format->format = (format->which == V4L2_SUBDEV_FORMAT_TRY) ?
113 *v4l2_subdev_get_try_format(sd, cfg, 0) :
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;
125 static void vimc_sca_adjust_sink_fmt(struct v4l2_mbus_framefmt *fmt)
127 const struct vimc_pix_map *vpix;
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;
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;
139 if (fmt->field == V4L2_FIELD_ANY)
140 fmt->field = sink_fmt_default.field;
142 vimc_colorimetry_clamp(fmt);
145 static int vimc_sca_set_fmt(struct v4l2_subdev *sd,
146 struct v4l2_subdev_pad_config *cfg,
147 struct v4l2_subdev_format *fmt)
149 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
150 struct v4l2_mbus_framefmt *sink_fmt;
152 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
153 /* Do not change the format while stream is on */
157 sink_fmt = &vsca->sink_fmt;
159 sink_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
163 * Do not change the format of the source pad,
164 * it is propagated from the sink
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;
171 /* Set the new format in the sink pad */
172 vimc_sca_adjust_sink_fmt(&fmt->format);
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,
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,
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);
186 *sink_fmt = fmt->format;
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,
200 static int vimc_sca_s_stream(struct v4l2_subdev *sd, int enable)
202 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
205 const struct vimc_pix_map *vpix;
206 unsigned int frame_size;
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;
215 /* Calculate the width in bytes of the src frame */
216 vsca->src_line_size = vsca->sink_fmt.width *
217 sca_mult * vsca->bpp;
219 /* Calculate the frame size of the source pad */
220 frame_size = vsca->src_line_size * vsca->sink_fmt.height *
223 /* Allocate the frame buffer. Use vmalloc to be able to
224 * allocate a large amount of memory
226 vsca->src_frame = vmalloc(frame_size);
227 if (!vsca->src_frame)
231 if (!vsca->src_frame)
234 vfree(vsca->src_frame);
235 vsca->src_frame = NULL;
241 static const struct v4l2_subdev_video_ops vimc_sca_video_ops = {
242 .s_stream = vimc_sca_s_stream,
245 static const struct v4l2_subdev_ops vimc_sca_ops = {
246 .pad = &vimc_sca_pad_ops,
247 .video = &vimc_sca_video_ops,
250 static void vimc_sca_fill_pix(u8 *const ptr,
251 const u8 *const pixel,
252 const unsigned int bpp)
256 /* copy the pixel to the pointer */
257 for (i = 0; i < bpp; i++)
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)
265 unsigned int i, j, index;
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,
272 pixel = &sink_frame[index];
275 "sca: %s: --- scale_pix sink pos %dx%d, index %d ---\n",
276 vsca->sd.name, lin, col, index);
278 /* point to the place we are going to put the first pixel
279 * in the scaled src frame
281 index = VIMC_FRAME_INDEX(lin * sca_mult, col * sca_mult,
282 vsca->sink_fmt.width * sca_mult, vsca->bpp);
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);
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
292 for (j = 0; j < sca_mult * vsca->bpp; j += vsca->bpp) {
294 "sca: %s: sca: scale_pix src pos %d\n",
295 vsca->sd.name, index + j);
297 /* copy the pixel to the position index + j */
298 vimc_sca_fill_pix(&vsca->src_frame[index + j],
302 /* move the index to the next line */
303 index += vsca->src_line_size;
307 static void vimc_sca_fill_src_frame(const struct vimc_sca_device *const vsca,
308 const u8 *const sink_frame)
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);
319 static void *vimc_sca_process_frame(struct vimc_ent_device *ved,
320 const void *sink_frame)
322 struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
325 /* If the stream in this node is not active, just return */
326 if (!vsca->src_frame)
327 return ERR_PTR(-EINVAL);
329 vimc_sca_fill_src_frame(vsca, sink_frame);
331 return vsca->src_frame;
334 static void vimc_sca_release(struct v4l2_subdev *sd)
336 struct vimc_sca_device *vsca =
337 container_of(sd, struct vimc_sca_device, sd);
339 vimc_pads_cleanup(vsca->ved.pads);
343 static const struct v4l2_subdev_internal_ops vimc_sca_int_ops = {
344 .release = vimc_sca_release,
347 void vimc_sca_rm(struct vimc_device *vimc, struct vimc_ent_device *ved)
349 struct vimc_sca_device *vsca;
351 vsca = container_of(ved, struct vimc_sca_device, ved);
352 vimc_ent_sd_unregister(ved, &vsca->sd);
355 struct vimc_ent_device *vimc_sca_add(struct vimc_device *vimc,
356 const char *vcfg_name)
358 struct v4l2_device *v4l2_dev = &vimc->v4l2_dev;
359 struct vimc_sca_device *vsca;
362 /* Allocate the vsca struct */
363 vsca = kzalloc(sizeof(*vsca), GFP_KERNEL);
367 /* Initialize ved and sd */
368 ret = vimc_ent_sd_register(&vsca->ved, &vsca->sd, v4l2_dev,
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);
379 vsca->ved.process_frame = vimc_sca_process_frame;
380 vsca->dev = &vimc->pdev.dev;
382 /* Initialize the frame format */
383 vsca->sink_fmt = sink_fmt_default;