1
2
3
4
5
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
28static const u32 vimc_sca_supported_pixfmt[] = {
29 V4L2_PIX_FMT_BGR24,
30 V4L2_PIX_FMT_RGB24,
31 V4L2_PIX_FMT_ARGB32,
32};
33
34struct vimc_sca_device {
35 struct vimc_ent_device ved;
36 struct v4l2_subdev sd;
37 struct device *dev;
38
39
40
41 struct v4l2_mbus_framefmt sink_fmt;
42
43 u8 *src_frame;
44 unsigned int src_line_size;
45 unsigned int bpp;
46};
47
48static const struct v4l2_mbus_framefmt sink_fmt_default = {
49 .width = 640,
50 .height = 480,
51 .code = MEDIA_BUS_FMT_RGB888_1X24,
52 .field = V4L2_FIELD_NONE,
53 .colorspace = V4L2_COLORSPACE_DEFAULT,
54};
55
56static bool vimc_sca_is_pixfmt_supported(u32 pixelformat)
57{
58 unsigned int i;
59
60 for (i = 0; i < ARRAY_SIZE(vimc_sca_supported_pixfmt); i++)
61 if (vimc_sca_supported_pixfmt[i] == pixelformat)
62 return true;
63 return false;
64}
65
66static int vimc_sca_init_cfg(struct v4l2_subdev *sd,
67 struct v4l2_subdev_pad_config *cfg)
68{
69 struct v4l2_mbus_framefmt *mf;
70 unsigned int i;
71
72 mf = v4l2_subdev_get_try_format(sd, cfg, 0);
73 *mf = sink_fmt_default;
74
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;
80 }
81
82 return 0;
83}
84
85static 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)
88{
89 if (fse->index)
90 return -EINVAL;
91
92 fse->min_width = VIMC_FRAME_MIN_WIDTH;
93 fse->min_height = VIMC_FRAME_MIN_HEIGHT;
94
95 if (IS_SINK(fse->pad)) {
96 fse->max_width = VIMC_FRAME_MAX_WIDTH;
97 fse->max_height = VIMC_FRAME_MAX_HEIGHT;
98 } else {
99 fse->max_width = VIMC_FRAME_MAX_WIDTH * MAX_ZOOM;
100 fse->max_height = VIMC_FRAME_MAX_HEIGHT * MAX_ZOOM;
101 }
102
103 return 0;
104}
105
106static int vimc_sca_get_fmt(struct v4l2_subdev *sd,
107 struct v4l2_subdev_pad_config *cfg,
108 struct v4l2_subdev_format *format)
109{
110 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
111
112
113 format->format = (format->which == V4L2_SUBDEV_FORMAT_TRY) ?
114 *v4l2_subdev_get_try_format(sd, cfg, 0) :
115 vsca->sink_fmt;
116
117
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;
121 }
122
123 return 0;
124}
125
126static void vimc_sca_adjust_sink_fmt(struct v4l2_mbus_framefmt *fmt)
127{
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;
132
133 if (fmt->field == V4L2_FIELD_ANY)
134 fmt->field = sink_fmt_default.field;
135
136 vimc_colorimetry_clamp(fmt);
137}
138
139static int vimc_sca_set_fmt(struct v4l2_subdev *sd,
140 struct v4l2_subdev_pad_config *cfg,
141 struct v4l2_subdev_format *fmt)
142{
143 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
144 struct v4l2_mbus_framefmt *sink_fmt;
145
146 if (!vimc_mbus_code_supported(fmt->format.code))
147 fmt->format.code = sink_fmt_default.code;
148
149 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
150
151 if (vsca->ved.stream)
152 return -EBUSY;
153
154 sink_fmt = &vsca->sink_fmt;
155 } else {
156 sink_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
157 }
158
159
160
161
162
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;
167 } else {
168
169 vimc_sca_adjust_sink_fmt(&fmt->format);
170
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,
174
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,
178
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);
182
183 *sink_fmt = fmt->format;
184 }
185
186 return 0;
187}
188
189static 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,
195};
196
197static int vimc_sca_s_stream(struct v4l2_subdev *sd, int enable)
198{
199 struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
200
201 if (enable) {
202 u32 pixelformat = vsca->ved.stream->producer_pixfmt;
203 const struct v4l2_format_info *pix_info;
204 unsigned int frame_size;
205
206 if (!vimc_sca_is_pixfmt_supported(pixelformat)) {
207 dev_err(vsca->dev, "pixfmt (0x%08x) is not supported\n",
208 pixelformat);
209 return -EINVAL;
210 }
211
212
213 pix_info = v4l2_format_info(pixelformat);
214 vsca->bpp = pix_info->bpp[0];
215
216
217 vsca->src_line_size = vsca->sink_fmt.width *
218 sca_mult * vsca->bpp;
219
220
221 frame_size = vsca->src_line_size * vsca->sink_fmt.height *
222 sca_mult;
223
224
225
226
227 vsca->src_frame = vmalloc(frame_size);
228 if (!vsca->src_frame)
229 return -ENOMEM;
230
231 } else {
232 if (!vsca->src_frame)
233 return 0;
234
235 vfree(vsca->src_frame);
236 vsca->src_frame = NULL;
237 }
238
239 return 0;
240}
241
242static const struct v4l2_subdev_video_ops vimc_sca_video_ops = {
243 .s_stream = vimc_sca_s_stream,
244};
245
246static const struct v4l2_subdev_ops vimc_sca_ops = {
247 .pad = &vimc_sca_pad_ops,
248 .video = &vimc_sca_video_ops,
249};
250
251static void vimc_sca_fill_pix(u8 *const ptr,
252 const u8 *const pixel,
253 const unsigned int bpp)
254{
255 unsigned int i;
256
257
258 for (i = 0; i < bpp; i++)
259 ptr[i] = pixel[i];
260}
261
262static void vimc_sca_scale_pix(const struct vimc_sca_device *const vsca,
263 const unsigned int lin, const unsigned int col,
264 const u8 *const sink_frame)
265{
266 unsigned int i, j, index;
267 const u8 *pixel;
268
269
270 index = VIMC_FRAME_INDEX(lin, col,
271 vsca->sink_fmt.width,
272 vsca->bpp);
273 pixel = &sink_frame[index];
274
275 dev_dbg(vsca->dev,
276 "sca: %s: --- scale_pix sink pos %dx%d, index %d ---\n",
277 vsca->sd.name, lin, col, index);
278
279
280
281
282 index = VIMC_FRAME_INDEX(lin * sca_mult, col * sca_mult,
283 vsca->sink_fmt.width * sca_mult, vsca->bpp);
284
285 dev_dbg(vsca->dev, "sca: %s: scale_pix src pos %dx%d, index %d\n",
286 vsca->sd.name, lin * sca_mult, col * sca_mult, index);
287
288
289 for (i = 0; i < sca_mult; i++) {
290
291
292
293 for (j = 0; j < sca_mult * vsca->bpp; j += vsca->bpp) {
294 dev_dbg(vsca->dev,
295 "sca: %s: sca: scale_pix src pos %d\n",
296 vsca->sd.name, index + j);
297
298
299 vimc_sca_fill_pix(&vsca->src_frame[index + j],
300 pixel, vsca->bpp);
301 }
302
303
304 index += vsca->src_line_size;
305 }
306}
307
308static void vimc_sca_fill_src_frame(const struct vimc_sca_device *const vsca,
309 const u8 *const sink_frame)
310{
311 unsigned int i, j;
312
313
314
315 for (i = 0; i < vsca->sink_fmt.height; i++)
316 for (j = 0; j < vsca->sink_fmt.width; j++)
317 vimc_sca_scale_pix(vsca, i, j, sink_frame);
318}
319
320static void *vimc_sca_process_frame(struct vimc_ent_device *ved,
321 const void *sink_frame)
322{
323 struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
324 ved);
325
326
327 if (!ved->stream)
328 return ERR_PTR(-EINVAL);
329
330 vimc_sca_fill_src_frame(vsca, sink_frame);
331
332 return vsca->src_frame;
333};
334
335static void vimc_sca_release(struct v4l2_subdev *sd)
336{
337 struct vimc_sca_device *vsca =
338 container_of(sd, struct vimc_sca_device, sd);
339
340 kfree(vsca);
341}
342
343static const struct v4l2_subdev_internal_ops vimc_sca_int_ops = {
344 .release = vimc_sca_release,
345};
346
347static void vimc_sca_comp_unbind(struct device *comp, struct device *master,
348 void *master_data)
349{
350 struct vimc_ent_device *ved = dev_get_drvdata(comp);
351 struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
352 ved);
353
354 vimc_ent_sd_unregister(ved, &vsca->sd);
355}
356
357
358static int vimc_sca_comp_bind(struct device *comp, struct device *master,
359 void *master_data)
360{
361 struct v4l2_device *v4l2_dev = master_data;
362 struct vimc_platform_data *pdata = comp->platform_data;
363 struct vimc_sca_device *vsca;
364 int ret;
365
366
367 vsca = kzalloc(sizeof(*vsca), GFP_KERNEL);
368 if (!vsca)
369 return -ENOMEM;
370
371
372 ret = vimc_ent_sd_register(&vsca->ved, &vsca->sd, v4l2_dev,
373 pdata->entity_name,
374 MEDIA_ENT_F_PROC_VIDEO_SCALER, 2,
375 (const unsigned long[2]) {MEDIA_PAD_FL_SINK,
376 MEDIA_PAD_FL_SOURCE},
377 &vimc_sca_int_ops, &vimc_sca_ops);
378 if (ret) {
379 kfree(vsca);
380 return ret;
381 }
382
383 vsca->ved.process_frame = vimc_sca_process_frame;
384 dev_set_drvdata(comp, &vsca->ved);
385 vsca->dev = comp;
386
387
388 vsca->sink_fmt = sink_fmt_default;
389
390 return 0;
391}
392
393static const struct component_ops vimc_sca_comp_ops = {
394 .bind = vimc_sca_comp_bind,
395 .unbind = vimc_sca_comp_unbind,
396};
397
398static int vimc_sca_probe(struct platform_device *pdev)
399{
400 return component_add(&pdev->dev, &vimc_sca_comp_ops);
401}
402
403static int vimc_sca_remove(struct platform_device *pdev)
404{
405 component_del(&pdev->dev, &vimc_sca_comp_ops);
406
407 return 0;
408}
409
410static const struct platform_device_id vimc_sca_driver_ids[] = {
411 {
412 .name = VIMC_SCA_DRV_NAME,
413 },
414 { }
415};
416
417static struct platform_driver vimc_sca_pdrv = {
418 .probe = vimc_sca_probe,
419 .remove = vimc_sca_remove,
420 .id_table = vimc_sca_driver_ids,
421 .driver = {
422 .name = VIMC_SCA_DRV_NAME,
423 },
424};
425
426module_platform_driver(vimc_sca_pdrv);
427
428MODULE_DEVICE_TABLE(platform, vimc_sca_driver_ids);
429
430MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Scaler");
431MODULE_AUTHOR("Helen Mae Koike Fornazier <helen.fornazier@gmail.com>");
432MODULE_LICENSE("GPL");
433