1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/component.h>
19#include <linux/module.h>
20#include <linux/mod_devicetable.h>
21#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
43
44
45 struct v4l2_mbus_framefmt sink_fmt;
46
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
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
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
129 format->format = (format->which == V4L2_SUBDEV_FORMAT_TRY) ?
130 *v4l2_subdev_get_try_format(sd, cfg, 0) :
131 vsca->sink_fmt;
132
133
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
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
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
181
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
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
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
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);
220 int ret;
221
222 if (enable) {
223 const struct vimc_pix_map *vpix;
224 unsigned int frame_size;
225
226 if (vsca->src_frame)
227 return 0;
228
229
230 vpix = vimc_pix_map_by_code(vsca->sink_fmt.code);
231 vsca->bpp = vpix->bpp;
232
233
234 vsca->src_line_size = vsca->sink_fmt.width *
235 sca_mult * vsca->bpp;
236
237
238 frame_size = vsca->src_line_size * vsca->sink_fmt.height *
239 sca_mult;
240
241
242
243
244 vsca->src_frame = vmalloc(frame_size);
245 if (!vsca->src_frame)
246 return -ENOMEM;
247
248
249 ret = vimc_pipeline_s_stream(&vsca->sd.entity, 1);
250 if (ret) {
251 vfree(vsca->src_frame);
252 vsca->src_frame = NULL;
253 return ret;
254 }
255 } else {
256 if (!vsca->src_frame)
257 return 0;
258
259
260 ret = vimc_pipeline_s_stream(&vsca->sd.entity, 0);
261 if (ret)
262 return ret;
263
264 vfree(vsca->src_frame);
265 vsca->src_frame = NULL;
266 }
267
268 return 0;
269}
270
271static const struct v4l2_subdev_video_ops vimc_sca_video_ops = {
272 .s_stream = vimc_sca_s_stream,
273};
274
275static const struct v4l2_subdev_ops vimc_sca_ops = {
276 .pad = &vimc_sca_pad_ops,
277 .video = &vimc_sca_video_ops,
278};
279
280static void vimc_sca_fill_pix(u8 *const ptr,
281 const u8 *const pixel,
282 const unsigned int bpp)
283{
284 unsigned int i;
285
286
287 for (i = 0; i < bpp; i++)
288 ptr[i] = pixel[i];
289}
290
291static void vimc_sca_scale_pix(const struct vimc_sca_device *const vsca,
292 const unsigned int lin, const unsigned int col,
293 const u8 *const sink_frame)
294{
295 unsigned int i, j, index;
296 const u8 *pixel;
297
298
299 index = VIMC_FRAME_INDEX(lin, col,
300 vsca->sink_fmt.width,
301 vsca->bpp);
302 pixel = &sink_frame[index];
303
304 dev_dbg(vsca->dev,
305 "sca: %s: --- scale_pix sink pos %dx%d, index %d ---\n",
306 vsca->sd.name, lin, col, index);
307
308
309
310
311 index = VIMC_FRAME_INDEX(lin * sca_mult, col * sca_mult,
312 vsca->sink_fmt.width * sca_mult, vsca->bpp);
313
314 dev_dbg(vsca->dev, "sca: %s: scale_pix src pos %dx%d, index %d\n",
315 vsca->sd.name, lin * sca_mult, col * sca_mult, index);
316
317
318 for (i = 0; i < sca_mult; i++) {
319
320
321
322 for (j = 0; j < sca_mult * vsca->bpp; j += vsca->bpp) {
323 dev_dbg(vsca->dev,
324 "sca: %s: sca: scale_pix src pos %d\n",
325 vsca->sd.name, index + j);
326
327
328 vimc_sca_fill_pix(&vsca->src_frame[index + j],
329 pixel, vsca->bpp);
330 }
331
332
333 index += vsca->src_line_size;
334 }
335}
336
337static void vimc_sca_fill_src_frame(const struct vimc_sca_device *const vsca,
338 const u8 *const sink_frame)
339{
340 unsigned int i, j;
341
342
343
344 for (i = 0; i < vsca->sink_fmt.height; i++)
345 for (j = 0; j < vsca->sink_fmt.width; j++)
346 vimc_sca_scale_pix(vsca, i, j, sink_frame);
347}
348
349static void vimc_sca_process_frame(struct vimc_ent_device *ved,
350 struct media_pad *sink,
351 const void *sink_frame)
352{
353 struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
354 ved);
355 unsigned int i;
356
357
358 if (!vsca->src_frame)
359 return;
360
361 vimc_sca_fill_src_frame(vsca, sink_frame);
362
363
364 for (i = 1; i < vsca->sd.entity.num_pads; i++) {
365 struct media_pad *pad = &vsca->sd.entity.pads[i];
366
367 vimc_propagate_frame(pad, vsca->src_frame);
368 }
369};
370
371static void vimc_sca_comp_unbind(struct device *comp, struct device *master,
372 void *master_data)
373{
374 struct vimc_ent_device *ved = dev_get_drvdata(comp);
375 struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
376 ved);
377
378 vimc_ent_sd_unregister(ved, &vsca->sd);
379 kfree(vsca);
380}
381
382
383static int vimc_sca_comp_bind(struct device *comp, struct device *master,
384 void *master_data)
385{
386 struct v4l2_device *v4l2_dev = master_data;
387 struct vimc_platform_data *pdata = comp->platform_data;
388 struct vimc_sca_device *vsca;
389 int ret;
390
391
392 vsca = kzalloc(sizeof(*vsca), GFP_KERNEL);
393 if (!vsca)
394 return -ENOMEM;
395
396
397 ret = vimc_ent_sd_register(&vsca->ved, &vsca->sd, v4l2_dev,
398 pdata->entity_name,
399 MEDIA_ENT_F_PROC_VIDEO_SCALER, 2,
400 (const unsigned long[2]) {MEDIA_PAD_FL_SINK,
401 MEDIA_PAD_FL_SOURCE},
402 &vimc_sca_ops);
403 if (ret) {
404 kfree(vsca);
405 return ret;
406 }
407
408 vsca->ved.process_frame = vimc_sca_process_frame;
409 dev_set_drvdata(comp, &vsca->ved);
410 vsca->dev = comp;
411
412
413 vsca->sink_fmt = sink_fmt_default;
414
415 return 0;
416}
417
418static const struct component_ops vimc_sca_comp_ops = {
419 .bind = vimc_sca_comp_bind,
420 .unbind = vimc_sca_comp_unbind,
421};
422
423static int vimc_sca_probe(struct platform_device *pdev)
424{
425 return component_add(&pdev->dev, &vimc_sca_comp_ops);
426}
427
428static int vimc_sca_remove(struct platform_device *pdev)
429{
430 component_del(&pdev->dev, &vimc_sca_comp_ops);
431
432 return 0;
433}
434
435static const struct platform_device_id vimc_sca_driver_ids[] = {
436 {
437 .name = VIMC_SCA_DRV_NAME,
438 },
439 { }
440};
441
442static struct platform_driver vimc_sca_pdrv = {
443 .probe = vimc_sca_probe,
444 .remove = vimc_sca_remove,
445 .id_table = vimc_sca_driver_ids,
446 .driver = {
447 .name = VIMC_SCA_DRV_NAME,
448 },
449};
450
451module_platform_driver(vimc_sca_pdrv);
452
453MODULE_DEVICE_TABLE(platform, vimc_sca_driver_ids);
454
455MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Scaler");
456MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
457MODULE_LICENSE("GPL");
458