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
29 static unsigned int sca_mult = 3;
30 module_param(sca_mult, uint, 0000);
31 MODULE_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
37 struct 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
51 static 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
vimc_sca_init_cfg(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg)59 static 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
vimc_sca_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)78 static 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
vimc_sca_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)93 static 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
vimc_sca_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)121 static 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
vimc_sca_adjust_sink_fmt(struct v4l2_mbus_framefmt * fmt)141 static 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
vimc_sca_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)161 static 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
208 static 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
vimc_sca_s_stream(struct v4l2_subdev * sd,int enable)216 static int vimc_sca_s_stream(struct v4l2_subdev *sd, int enable)
217 {
218 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
219
220 if (enable) {
221 const struct vimc_pix_map *vpix;
222 unsigned int frame_size;
223
224 if (vsca->src_frame)
225 return 0;
226
227 /* Save the bytes per pixel of the sink */
228 vpix = vimc_pix_map_by_code(vsca->sink_fmt.code);
229 vsca->bpp = vpix->bpp;
230
231 /* Calculate the width in bytes of the src frame */
232 vsca->src_line_size = vsca->sink_fmt.width *
233 sca_mult * vsca->bpp;
234
235 /* Calculate the frame size of the source pad */
236 frame_size = vsca->src_line_size * vsca->sink_fmt.height *
237 sca_mult;
238
239 /* Allocate the frame buffer. Use vmalloc to be able to
240 * allocate a large amount of memory
241 */
242 vsca->src_frame = vmalloc(frame_size);
243 if (!vsca->src_frame)
244 return -ENOMEM;
245
246 } else {
247 if (!vsca->src_frame)
248 return 0;
249
250 vfree(vsca->src_frame);
251 vsca->src_frame = NULL;
252 }
253
254 return 0;
255 }
256
257 static const struct v4l2_subdev_video_ops vimc_sca_video_ops = {
258 .s_stream = vimc_sca_s_stream,
259 };
260
261 static const struct v4l2_subdev_ops vimc_sca_ops = {
262 .pad = &vimc_sca_pad_ops,
263 .video = &vimc_sca_video_ops,
264 };
265
vimc_sca_fill_pix(u8 * const ptr,const u8 * const pixel,const unsigned int bpp)266 static void vimc_sca_fill_pix(u8 *const ptr,
267 const u8 *const pixel,
268 const unsigned int bpp)
269 {
270 unsigned int i;
271
272 /* copy the pixel to the pointer */
273 for (i = 0; i < bpp; i++)
274 ptr[i] = pixel[i];
275 }
276
vimc_sca_scale_pix(const struct vimc_sca_device * const vsca,const unsigned int lin,const unsigned int col,const u8 * const sink_frame)277 static void vimc_sca_scale_pix(const struct vimc_sca_device *const vsca,
278 const unsigned int lin, const unsigned int col,
279 const u8 *const sink_frame)
280 {
281 unsigned int i, j, index;
282 const u8 *pixel;
283
284 /* Point to the pixel value in position (lin, col) in the sink frame */
285 index = VIMC_FRAME_INDEX(lin, col,
286 vsca->sink_fmt.width,
287 vsca->bpp);
288 pixel = &sink_frame[index];
289
290 dev_dbg(vsca->dev,
291 "sca: %s: --- scale_pix sink pos %dx%d, index %d ---\n",
292 vsca->sd.name, lin, col, index);
293
294 /* point to the place we are going to put the first pixel
295 * in the scaled src frame
296 */
297 index = VIMC_FRAME_INDEX(lin * sca_mult, col * sca_mult,
298 vsca->sink_fmt.width * sca_mult, vsca->bpp);
299
300 dev_dbg(vsca->dev, "sca: %s: scale_pix src pos %dx%d, index %d\n",
301 vsca->sd.name, lin * sca_mult, col * sca_mult, index);
302
303 /* Repeat this pixel mult times */
304 for (i = 0; i < sca_mult; i++) {
305 /* Iterate through each beginning of a
306 * pixel repetition in a line
307 */
308 for (j = 0; j < sca_mult * vsca->bpp; j += vsca->bpp) {
309 dev_dbg(vsca->dev,
310 "sca: %s: sca: scale_pix src pos %d\n",
311 vsca->sd.name, index + j);
312
313 /* copy the pixel to the position index + j */
314 vimc_sca_fill_pix(&vsca->src_frame[index + j],
315 pixel, vsca->bpp);
316 }
317
318 /* move the index to the next line */
319 index += vsca->src_line_size;
320 }
321 }
322
vimc_sca_fill_src_frame(const struct vimc_sca_device * const vsca,const u8 * const sink_frame)323 static void vimc_sca_fill_src_frame(const struct vimc_sca_device *const vsca,
324 const u8 *const sink_frame)
325 {
326 unsigned int i, j;
327
328 /* Scale each pixel from the original sink frame */
329 /* TODO: implement scale down, only scale up is supported for now */
330 for (i = 0; i < vsca->sink_fmt.height; i++)
331 for (j = 0; j < vsca->sink_fmt.width; j++)
332 vimc_sca_scale_pix(vsca, i, j, sink_frame);
333 }
334
vimc_sca_process_frame(struct vimc_ent_device * ved,const void * sink_frame)335 static void *vimc_sca_process_frame(struct vimc_ent_device *ved,
336 const void *sink_frame)
337 {
338 struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
339 ved);
340
341 /* If the stream in this node is not active, just return */
342 if (!vsca->src_frame)
343 return ERR_PTR(-EINVAL);
344
345 vimc_sca_fill_src_frame(vsca, sink_frame);
346
347 return vsca->src_frame;
348 };
349
vimc_sca_comp_unbind(struct device * comp,struct device * master,void * master_data)350 static void vimc_sca_comp_unbind(struct device *comp, struct device *master,
351 void *master_data)
352 {
353 struct vimc_ent_device *ved = dev_get_drvdata(comp);
354 struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
355 ved);
356
357 vimc_ent_sd_unregister(ved, &vsca->sd);
358 kfree(vsca);
359 }
360
361
vimc_sca_comp_bind(struct device * comp,struct device * master,void * master_data)362 static int vimc_sca_comp_bind(struct device *comp, struct device *master,
363 void *master_data)
364 {
365 struct v4l2_device *v4l2_dev = master_data;
366 struct vimc_platform_data *pdata = comp->platform_data;
367 struct vimc_sca_device *vsca;
368 int ret;
369
370 /* Allocate the vsca struct */
371 vsca = kzalloc(sizeof(*vsca), GFP_KERNEL);
372 if (!vsca)
373 return -ENOMEM;
374
375 /* Initialize ved and sd */
376 ret = vimc_ent_sd_register(&vsca->ved, &vsca->sd, v4l2_dev,
377 pdata->entity_name,
378 MEDIA_ENT_F_ATV_DECODER, 2,
379 (const unsigned long[2]) {MEDIA_PAD_FL_SINK,
380 MEDIA_PAD_FL_SOURCE},
381 &vimc_sca_ops);
382 if (ret) {
383 kfree(vsca);
384 return ret;
385 }
386
387 vsca->ved.process_frame = vimc_sca_process_frame;
388 dev_set_drvdata(comp, &vsca->ved);
389 vsca->dev = comp;
390
391 /* Initialize the frame format */
392 vsca->sink_fmt = sink_fmt_default;
393
394 return 0;
395 }
396
397 static const struct component_ops vimc_sca_comp_ops = {
398 .bind = vimc_sca_comp_bind,
399 .unbind = vimc_sca_comp_unbind,
400 };
401
vimc_sca_probe(struct platform_device * pdev)402 static int vimc_sca_probe(struct platform_device *pdev)
403 {
404 return component_add(&pdev->dev, &vimc_sca_comp_ops);
405 }
406
vimc_sca_remove(struct platform_device * pdev)407 static int vimc_sca_remove(struct platform_device *pdev)
408 {
409 component_del(&pdev->dev, &vimc_sca_comp_ops);
410
411 return 0;
412 }
413
414 static const struct platform_device_id vimc_sca_driver_ids[] = {
415 {
416 .name = VIMC_SCA_DRV_NAME,
417 },
418 { }
419 };
420
421 static struct platform_driver vimc_sca_pdrv = {
422 .probe = vimc_sca_probe,
423 .remove = vimc_sca_remove,
424 .id_table = vimc_sca_driver_ids,
425 .driver = {
426 .name = VIMC_SCA_DRV_NAME,
427 },
428 };
429
430 module_platform_driver(vimc_sca_pdrv);
431
432 MODULE_DEVICE_TABLE(platform, vimc_sca_driver_ids);
433
434 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Scaler");
435 MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
436 MODULE_LICENSE("GPL");
437