Linux Audio

Check our new training course

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