1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * i.MX IPUv3 IC PP mem2mem CSC/Scaler driver
4 *
5 * Copyright (C) 2011 Pengutronix, Sascha Hauer
6 * Copyright (C) 2018 Pengutronix, Philipp Zabel
7 */
8 #include <linux/module.h>
9 #include <linux/delay.h>
10 #include <linux/fs.h>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
13 #include <video/imx-ipu-v3.h>
14 #include <video/imx-ipu-image-convert.h>
15
16 #include <media/media-device.h>
17 #include <media/v4l2-ctrls.h>
18 #include <media/v4l2-event.h>
19 #include <media/v4l2-mem2mem.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/videobuf2-dma-contig.h>
23
24 #include "imx-media.h"
25
26 #define fh_to_ctx(__fh) container_of(__fh, struct ipu_csc_scaler_ctx, fh)
27
28 #define IMX_CSC_SCALER_NAME "imx-csc-scaler"
29
30 enum {
31 V4L2_M2M_SRC = 0,
32 V4L2_M2M_DST = 1,
33 };
34
35 struct ipu_csc_scaler_priv {
36 struct imx_media_video_dev vdev;
37
38 struct v4l2_m2m_dev *m2m_dev;
39 struct device *dev;
40
41 struct imx_media_dev *md;
42
43 struct mutex mutex; /* mem2mem device mutex */
44 };
45
46 #define vdev_to_priv(v) container_of(v, struct ipu_csc_scaler_priv, vdev)
47
48 /* Per-queue, driver-specific private data */
49 struct ipu_csc_scaler_q_data {
50 struct v4l2_pix_format cur_fmt;
51 struct v4l2_rect rect;
52 };
53
54 struct ipu_csc_scaler_ctx {
55 struct ipu_csc_scaler_priv *priv;
56
57 struct v4l2_fh fh;
58 struct ipu_csc_scaler_q_data q_data[2];
59 struct ipu_image_convert_ctx *icc;
60
61 struct v4l2_ctrl_handler ctrl_hdlr;
62 int rotate;
63 bool hflip;
64 bool vflip;
65 enum ipu_rotate_mode rot_mode;
66 unsigned int sequence;
67 };
68
get_q_data(struct ipu_csc_scaler_ctx * ctx,enum v4l2_buf_type type)69 static struct ipu_csc_scaler_q_data *get_q_data(struct ipu_csc_scaler_ctx *ctx,
70 enum v4l2_buf_type type)
71 {
72 if (V4L2_TYPE_IS_OUTPUT(type))
73 return &ctx->q_data[V4L2_M2M_SRC];
74 else
75 return &ctx->q_data[V4L2_M2M_DST];
76 }
77
78 /*
79 * mem2mem callbacks
80 */
81
job_abort(void * _ctx)82 static void job_abort(void *_ctx)
83 {
84 struct ipu_csc_scaler_ctx *ctx = _ctx;
85
86 if (ctx->icc)
87 ipu_image_convert_abort(ctx->icc);
88 }
89
ipu_ic_pp_complete(struct ipu_image_convert_run * run,void * _ctx)90 static void ipu_ic_pp_complete(struct ipu_image_convert_run *run, void *_ctx)
91 {
92 struct ipu_csc_scaler_ctx *ctx = _ctx;
93 struct ipu_csc_scaler_priv *priv = ctx->priv;
94 struct vb2_v4l2_buffer *src_buf, *dst_buf;
95
96 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
97 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
98
99 v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true);
100
101 src_buf->sequence = ctx->sequence++;
102 dst_buf->sequence = src_buf->sequence;
103
104 v4l2_m2m_buf_done(src_buf, run->status ? VB2_BUF_STATE_ERROR :
105 VB2_BUF_STATE_DONE);
106 v4l2_m2m_buf_done(dst_buf, run->status ? VB2_BUF_STATE_ERROR :
107 VB2_BUF_STATE_DONE);
108
109 v4l2_m2m_job_finish(priv->m2m_dev, ctx->fh.m2m_ctx);
110 kfree(run);
111 }
112
device_run(void * _ctx)113 static void device_run(void *_ctx)
114 {
115 struct ipu_csc_scaler_ctx *ctx = _ctx;
116 struct ipu_csc_scaler_priv *priv = ctx->priv;
117 struct vb2_v4l2_buffer *src_buf, *dst_buf;
118 struct ipu_image_convert_run *run;
119 int ret;
120
121 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
122 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
123
124 run = kzalloc(sizeof(*run), GFP_KERNEL);
125 if (!run)
126 goto err;
127
128 run->ctx = ctx->icc;
129 run->in_phys = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
130 run->out_phys = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
131
132 ret = ipu_image_convert_queue(run);
133 if (ret < 0) {
134 v4l2_err(ctx->priv->vdev.vfd->v4l2_dev,
135 "%s: failed to queue: %d\n", __func__, ret);
136 goto err;
137 }
138
139 return;
140
141 err:
142 v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
143 v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
144 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
145 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
146 v4l2_m2m_job_finish(priv->m2m_dev, ctx->fh.m2m_ctx);
147 }
148
149 /*
150 * Video ioctls
151 */
ipu_csc_scaler_querycap(struct file * file,void * priv,struct v4l2_capability * cap)152 static int ipu_csc_scaler_querycap(struct file *file, void *priv,
153 struct v4l2_capability *cap)
154 {
155 strscpy(cap->driver, IMX_CSC_SCALER_NAME, sizeof(cap->driver));
156 strscpy(cap->card, IMX_CSC_SCALER_NAME, sizeof(cap->card));
157 snprintf(cap->bus_info, sizeof(cap->bus_info),
158 "platform:%s", IMX_CSC_SCALER_NAME);
159
160 return 0;
161 }
162
ipu_csc_scaler_enum_fmt(struct file * file,void * fh,struct v4l2_fmtdesc * f)163 static int ipu_csc_scaler_enum_fmt(struct file *file, void *fh,
164 struct v4l2_fmtdesc *f)
165 {
166 u32 fourcc;
167 int ret;
168
169 ret = imx_media_enum_pixel_formats(&fourcc, f->index,
170 PIXFMT_SEL_YUV_RGB, 0);
171 if (ret)
172 return ret;
173
174 f->pixelformat = fourcc;
175
176 return 0;
177 }
178
ipu_csc_scaler_g_fmt(struct file * file,void * priv,struct v4l2_format * f)179 static int ipu_csc_scaler_g_fmt(struct file *file, void *priv,
180 struct v4l2_format *f)
181 {
182 struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
183 struct ipu_csc_scaler_q_data *q_data;
184
185 q_data = get_q_data(ctx, f->type);
186
187 f->fmt.pix = q_data->cur_fmt;
188
189 return 0;
190 }
191
ipu_csc_scaler_try_fmt(struct file * file,void * priv,struct v4l2_format * f)192 static int ipu_csc_scaler_try_fmt(struct file *file, void *priv,
193 struct v4l2_format *f)
194 {
195 struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
196 struct ipu_csc_scaler_q_data *q_data = get_q_data(ctx, f->type);
197 struct ipu_image test_in, test_out;
198 enum v4l2_field field;
199
200 field = f->fmt.pix.field;
201 if (field == V4L2_FIELD_ANY)
202 field = V4L2_FIELD_NONE;
203 else if (field != V4L2_FIELD_NONE)
204 return -EINVAL;
205
206 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
207 struct ipu_csc_scaler_q_data *q_data_in =
208 get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
209
210 test_out.pix = f->fmt.pix;
211 test_in.pix = q_data_in->cur_fmt;
212 } else {
213 struct ipu_csc_scaler_q_data *q_data_out =
214 get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
215
216 test_in.pix = f->fmt.pix;
217 test_out.pix = q_data_out->cur_fmt;
218 }
219
220 ipu_image_convert_adjust(&test_in, &test_out, ctx->rot_mode);
221
222 f->fmt.pix = (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
223 test_out.pix : test_in.pix;
224
225 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
226 f->fmt.pix.colorspace = q_data->cur_fmt.colorspace;
227 f->fmt.pix.ycbcr_enc = q_data->cur_fmt.ycbcr_enc;
228 f->fmt.pix.xfer_func = q_data->cur_fmt.xfer_func;
229 f->fmt.pix.quantization = q_data->cur_fmt.quantization;
230 } else if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT) {
231 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
232 f->fmt.pix.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
233 f->fmt.pix.xfer_func = V4L2_XFER_FUNC_DEFAULT;
234 f->fmt.pix.quantization = V4L2_QUANTIZATION_DEFAULT;
235 }
236
237 return 0;
238 }
239
ipu_csc_scaler_s_fmt(struct file * file,void * priv,struct v4l2_format * f)240 static int ipu_csc_scaler_s_fmt(struct file *file, void *priv,
241 struct v4l2_format *f)
242 {
243 struct ipu_csc_scaler_q_data *q_data;
244 struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
245 struct vb2_queue *vq;
246 int ret;
247
248 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
249 if (vb2_is_busy(vq)) {
250 v4l2_err(ctx->priv->vdev.vfd->v4l2_dev, "%s: queue busy\n",
251 __func__);
252 return -EBUSY;
253 }
254
255 q_data = get_q_data(ctx, f->type);
256
257 ret = ipu_csc_scaler_try_fmt(file, priv, f);
258 if (ret < 0)
259 return ret;
260
261 q_data->cur_fmt.width = f->fmt.pix.width;
262 q_data->cur_fmt.height = f->fmt.pix.height;
263 q_data->cur_fmt.pixelformat = f->fmt.pix.pixelformat;
264 q_data->cur_fmt.field = f->fmt.pix.field;
265 q_data->cur_fmt.bytesperline = f->fmt.pix.bytesperline;
266 q_data->cur_fmt.sizeimage = f->fmt.pix.sizeimage;
267
268 /* Reset cropping/composing rectangle */
269 q_data->rect.left = 0;
270 q_data->rect.top = 0;
271 q_data->rect.width = q_data->cur_fmt.width;
272 q_data->rect.height = q_data->cur_fmt.height;
273
274 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
275 /* Set colorimetry on the output queue */
276 q_data->cur_fmt.colorspace = f->fmt.pix.colorspace;
277 q_data->cur_fmt.ycbcr_enc = f->fmt.pix.ycbcr_enc;
278 q_data->cur_fmt.xfer_func = f->fmt.pix.xfer_func;
279 q_data->cur_fmt.quantization = f->fmt.pix.quantization;
280 /* Propagate colorimetry to the capture queue */
281 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
282 q_data->cur_fmt.colorspace = f->fmt.pix.colorspace;
283 q_data->cur_fmt.ycbcr_enc = f->fmt.pix.ycbcr_enc;
284 q_data->cur_fmt.xfer_func = f->fmt.pix.xfer_func;
285 q_data->cur_fmt.quantization = f->fmt.pix.quantization;
286 }
287
288 /*
289 * TODO: Setting colorimetry on the capture queue is currently not
290 * supported by the V4L2 API
291 */
292
293 return 0;
294 }
295
ipu_csc_scaler_g_selection(struct file * file,void * priv,struct v4l2_selection * s)296 static int ipu_csc_scaler_g_selection(struct file *file, void *priv,
297 struct v4l2_selection *s)
298 {
299 struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
300 struct ipu_csc_scaler_q_data *q_data;
301
302 switch (s->target) {
303 case V4L2_SEL_TGT_CROP:
304 case V4L2_SEL_TGT_CROP_DEFAULT:
305 case V4L2_SEL_TGT_CROP_BOUNDS:
306 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
307 return -EINVAL;
308 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
309 break;
310 case V4L2_SEL_TGT_COMPOSE:
311 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
312 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
313 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
314 return -EINVAL;
315 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
316 break;
317 default:
318 return -EINVAL;
319 }
320
321 if (s->target == V4L2_SEL_TGT_CROP ||
322 s->target == V4L2_SEL_TGT_COMPOSE) {
323 s->r = q_data->rect;
324 } else {
325 s->r.left = 0;
326 s->r.top = 0;
327 s->r.width = q_data->cur_fmt.width;
328 s->r.height = q_data->cur_fmt.height;
329 }
330
331 return 0;
332 }
333
ipu_csc_scaler_s_selection(struct file * file,void * priv,struct v4l2_selection * s)334 static int ipu_csc_scaler_s_selection(struct file *file, void *priv,
335 struct v4l2_selection *s)
336 {
337 struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(priv);
338 struct ipu_csc_scaler_q_data *q_data;
339
340 switch (s->target) {
341 case V4L2_SEL_TGT_CROP:
342 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
343 return -EINVAL;
344 break;
345 case V4L2_SEL_TGT_COMPOSE:
346 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
347 return -EINVAL;
348 break;
349 default:
350 return -EINVAL;
351 }
352
353 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
354 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
355 return -EINVAL;
356
357 q_data = get_q_data(ctx, s->type);
358
359 /* The input's frame width to the IC must be a multiple of 8 pixels
360 * When performing resizing the frame width must be multiple of burst
361 * size - 8 or 16 pixels as defined by CB#_BURST_16 parameter.
362 */
363 if (s->flags & V4L2_SEL_FLAG_GE)
364 s->r.width = round_up(s->r.width, 8);
365 if (s->flags & V4L2_SEL_FLAG_LE)
366 s->r.width = round_down(s->r.width, 8);
367 s->r.width = clamp_t(unsigned int, s->r.width, 8,
368 round_down(q_data->cur_fmt.width, 8));
369 s->r.height = clamp_t(unsigned int, s->r.height, 1,
370 q_data->cur_fmt.height);
371 s->r.left = clamp_t(unsigned int, s->r.left, 0,
372 q_data->cur_fmt.width - s->r.width);
373 s->r.top = clamp_t(unsigned int, s->r.top, 0,
374 q_data->cur_fmt.height - s->r.height);
375
376 /* V4L2_SEL_FLAG_KEEP_CONFIG is only valid for subdevices */
377 q_data->rect = s->r;
378
379 return 0;
380 }
381
382 static const struct v4l2_ioctl_ops ipu_csc_scaler_ioctl_ops = {
383 .vidioc_querycap = ipu_csc_scaler_querycap,
384
385 .vidioc_enum_fmt_vid_cap = ipu_csc_scaler_enum_fmt,
386 .vidioc_g_fmt_vid_cap = ipu_csc_scaler_g_fmt,
387 .vidioc_try_fmt_vid_cap = ipu_csc_scaler_try_fmt,
388 .vidioc_s_fmt_vid_cap = ipu_csc_scaler_s_fmt,
389
390 .vidioc_enum_fmt_vid_out = ipu_csc_scaler_enum_fmt,
391 .vidioc_g_fmt_vid_out = ipu_csc_scaler_g_fmt,
392 .vidioc_try_fmt_vid_out = ipu_csc_scaler_try_fmt,
393 .vidioc_s_fmt_vid_out = ipu_csc_scaler_s_fmt,
394
395 .vidioc_g_selection = ipu_csc_scaler_g_selection,
396 .vidioc_s_selection = ipu_csc_scaler_s_selection,
397
398 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
399 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
400
401 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
402 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
403 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
404 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
405 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
406
407 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
408 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
409
410 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
411 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
412 };
413
414 /*
415 * Queue operations
416 */
417
ipu_csc_scaler_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])418 static int ipu_csc_scaler_queue_setup(struct vb2_queue *vq,
419 unsigned int *nbuffers,
420 unsigned int *nplanes,
421 unsigned int sizes[],
422 struct device *alloc_devs[])
423 {
424 struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(vq);
425 struct ipu_csc_scaler_q_data *q_data;
426 unsigned int size, count = *nbuffers;
427
428 q_data = get_q_data(ctx, vq->type);
429
430 size = q_data->cur_fmt.sizeimage;
431
432 *nbuffers = count;
433
434 if (*nplanes)
435 return sizes[0] < size ? -EINVAL : 0;
436
437 *nplanes = 1;
438 sizes[0] = size;
439
440 dev_dbg(ctx->priv->dev, "get %d buffer(s) of size %d each.\n",
441 count, size);
442
443 return 0;
444 }
445
ipu_csc_scaler_buf_prepare(struct vb2_buffer * vb)446 static int ipu_csc_scaler_buf_prepare(struct vb2_buffer *vb)
447 {
448 struct vb2_queue *vq = vb->vb2_queue;
449 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
450 struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(vq);
451 struct ipu_csc_scaler_q_data *q_data;
452 unsigned long size;
453
454 dev_dbg(ctx->priv->dev, "type: %d\n", vq->type);
455
456 if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
457 if (vbuf->field == V4L2_FIELD_ANY)
458 vbuf->field = V4L2_FIELD_NONE;
459 if (vbuf->field != V4L2_FIELD_NONE) {
460 dev_dbg(ctx->priv->dev, "%s: field isn't supported\n",
461 __func__);
462 return -EINVAL;
463 }
464 }
465
466 q_data = get_q_data(ctx, vq->type);
467 size = q_data->cur_fmt.sizeimage;
468
469 if (vb2_plane_size(vb, 0) < size) {
470 dev_dbg(ctx->priv->dev,
471 "%s: data will not fit into plane (%lu < %lu)\n",
472 __func__, vb2_plane_size(vb, 0), size);
473 return -EINVAL;
474 }
475
476 vb2_set_plane_payload(vb, 0, size);
477
478 return 0;
479 }
480
ipu_csc_scaler_buf_queue(struct vb2_buffer * vb)481 static void ipu_csc_scaler_buf_queue(struct vb2_buffer *vb)
482 {
483 struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
484
485 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
486 }
487
ipu_image_from_q_data(struct ipu_image * im,struct ipu_csc_scaler_q_data * q_data)488 static void ipu_image_from_q_data(struct ipu_image *im,
489 struct ipu_csc_scaler_q_data *q_data)
490 {
491 struct v4l2_pix_format *fmt = &q_data->cur_fmt;
492
493 im->pix = *fmt;
494 if (fmt->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT)
495 im->pix.ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
496 if (fmt->quantization == V4L2_QUANTIZATION_DEFAULT)
497 im->pix.ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
498 im->rect = q_data->rect;
499 }
500
ipu_csc_scaler_start_streaming(struct vb2_queue * q,unsigned int count)501 static int ipu_csc_scaler_start_streaming(struct vb2_queue *q,
502 unsigned int count)
503 {
504 const enum ipu_ic_task ic_task = IC_TASK_POST_PROCESSOR;
505 struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(q);
506 struct ipu_csc_scaler_priv *priv = ctx->priv;
507 struct ipu_soc *ipu = priv->md->ipu[0];
508 struct ipu_csc_scaler_q_data *q_data;
509 struct vb2_queue *other_q;
510 struct ipu_image in, out;
511
512 other_q = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
513 (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
514 V4L2_BUF_TYPE_VIDEO_OUTPUT :
515 V4L2_BUF_TYPE_VIDEO_CAPTURE);
516 if (!vb2_is_streaming(other_q))
517 return 0;
518
519 if (ctx->icc) {
520 v4l2_warn(ctx->priv->vdev.vfd->v4l2_dev, "removing old ICC\n");
521 ipu_image_convert_unprepare(ctx->icc);
522 }
523
524 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
525 ipu_image_from_q_data(&in, q_data);
526
527 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
528 ipu_image_from_q_data(&out, q_data);
529
530 ctx->icc = ipu_image_convert_prepare(ipu, ic_task, &in, &out,
531 ctx->rot_mode,
532 ipu_ic_pp_complete, ctx);
533 if (IS_ERR(ctx->icc)) {
534 struct vb2_v4l2_buffer *buf;
535 int ret = PTR_ERR(ctx->icc);
536
537 ctx->icc = NULL;
538 v4l2_err(ctx->priv->vdev.vfd->v4l2_dev, "%s: error %d\n",
539 __func__, ret);
540 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
541 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
542 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
543 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
544 return ret;
545 }
546
547 return 0;
548 }
549
ipu_csc_scaler_stop_streaming(struct vb2_queue * q)550 static void ipu_csc_scaler_stop_streaming(struct vb2_queue *q)
551 {
552 struct ipu_csc_scaler_ctx *ctx = vb2_get_drv_priv(q);
553 struct vb2_v4l2_buffer *buf;
554
555 if (ctx->icc) {
556 ipu_image_convert_unprepare(ctx->icc);
557 ctx->icc = NULL;
558 }
559
560 ctx->sequence = 0;
561
562 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
563 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
564 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
565 } else {
566 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
567 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
568 }
569 }
570
571 static const struct vb2_ops ipu_csc_scaler_qops = {
572 .queue_setup = ipu_csc_scaler_queue_setup,
573 .buf_prepare = ipu_csc_scaler_buf_prepare,
574 .buf_queue = ipu_csc_scaler_buf_queue,
575 .start_streaming = ipu_csc_scaler_start_streaming,
576 .stop_streaming = ipu_csc_scaler_stop_streaming,
577 };
578
ipu_csc_scaler_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)579 static int ipu_csc_scaler_queue_init(void *priv, struct vb2_queue *src_vq,
580 struct vb2_queue *dst_vq)
581 {
582 struct ipu_csc_scaler_ctx *ctx = priv;
583 int ret;
584
585 memset(src_vq, 0, sizeof(*src_vq));
586 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
587 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
588 src_vq->drv_priv = ctx;
589 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
590 src_vq->ops = &ipu_csc_scaler_qops;
591 src_vq->mem_ops = &vb2_dma_contig_memops;
592 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
593 src_vq->lock = &ctx->priv->mutex;
594 src_vq->dev = ctx->priv->dev;
595
596 ret = vb2_queue_init(src_vq);
597 if (ret)
598 return ret;
599
600 memset(dst_vq, 0, sizeof(*dst_vq));
601 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
602 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
603 dst_vq->drv_priv = ctx;
604 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
605 dst_vq->ops = &ipu_csc_scaler_qops;
606 dst_vq->mem_ops = &vb2_dma_contig_memops;
607 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
608 dst_vq->lock = &ctx->priv->mutex;
609 dst_vq->dev = ctx->priv->dev;
610
611 return vb2_queue_init(dst_vq);
612 }
613
ipu_csc_scaler_s_ctrl(struct v4l2_ctrl * ctrl)614 static int ipu_csc_scaler_s_ctrl(struct v4l2_ctrl *ctrl)
615 {
616 struct ipu_csc_scaler_ctx *ctx = container_of(ctrl->handler,
617 struct ipu_csc_scaler_ctx,
618 ctrl_hdlr);
619 enum ipu_rotate_mode rot_mode;
620 int rotate;
621 bool hflip, vflip;
622 int ret = 0;
623
624 rotate = ctx->rotate;
625 hflip = ctx->hflip;
626 vflip = ctx->vflip;
627
628 switch (ctrl->id) {
629 case V4L2_CID_HFLIP:
630 hflip = ctrl->val;
631 break;
632 case V4L2_CID_VFLIP:
633 vflip = ctrl->val;
634 break;
635 case V4L2_CID_ROTATE:
636 rotate = ctrl->val;
637 break;
638 default:
639 return -EINVAL;
640 }
641
642 ret = ipu_degrees_to_rot_mode(&rot_mode, rotate, hflip, vflip);
643 if (ret)
644 return ret;
645
646 if (rot_mode != ctx->rot_mode) {
647 struct v4l2_pix_format *in_fmt, *out_fmt;
648 struct ipu_image test_in, test_out;
649
650 in_fmt = &ctx->q_data[V4L2_M2M_SRC].cur_fmt;
651 out_fmt = &ctx->q_data[V4L2_M2M_DST].cur_fmt;
652
653 test_in.pix = *in_fmt;
654 test_out.pix = *out_fmt;
655
656 if (ipu_rot_mode_is_irt(rot_mode) !=
657 ipu_rot_mode_is_irt(ctx->rot_mode)) {
658 /* Switch width & height to keep aspect ratio intact */
659 test_out.pix.width = out_fmt->height;
660 test_out.pix.height = out_fmt->width;
661 }
662
663 ipu_image_convert_adjust(&test_in, &test_out, ctx->rot_mode);
664
665 /* Check if output format needs to be changed */
666 if (test_in.pix.width != in_fmt->width ||
667 test_in.pix.height != in_fmt->height ||
668 test_in.pix.bytesperline != in_fmt->bytesperline ||
669 test_in.pix.sizeimage != in_fmt->sizeimage) {
670 struct vb2_queue *out_q;
671
672 out_q = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
673 V4L2_BUF_TYPE_VIDEO_OUTPUT);
674 if (vb2_is_busy(out_q))
675 return -EBUSY;
676 }
677
678 /* Check if capture format needs to be changed */
679 if (test_out.pix.width != out_fmt->width ||
680 test_out.pix.height != out_fmt->height ||
681 test_out.pix.bytesperline != out_fmt->bytesperline ||
682 test_out.pix.sizeimage != out_fmt->sizeimage) {
683 struct vb2_queue *cap_q;
684
685 cap_q = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
686 V4L2_BUF_TYPE_VIDEO_CAPTURE);
687 if (vb2_is_busy(cap_q))
688 return -EBUSY;
689 }
690
691 *in_fmt = test_in.pix;
692 *out_fmt = test_out.pix;
693
694 ctx->rot_mode = rot_mode;
695 ctx->rotate = rotate;
696 ctx->hflip = hflip;
697 ctx->vflip = vflip;
698 }
699
700 return 0;
701 }
702
703 static const struct v4l2_ctrl_ops ipu_csc_scaler_ctrl_ops = {
704 .s_ctrl = ipu_csc_scaler_s_ctrl,
705 };
706
ipu_csc_scaler_init_controls(struct ipu_csc_scaler_ctx * ctx)707 static int ipu_csc_scaler_init_controls(struct ipu_csc_scaler_ctx *ctx)
708 {
709 struct v4l2_ctrl_handler *hdlr = &ctx->ctrl_hdlr;
710
711 v4l2_ctrl_handler_init(hdlr, 3);
712
713 v4l2_ctrl_new_std(hdlr, &ipu_csc_scaler_ctrl_ops, V4L2_CID_HFLIP,
714 0, 1, 1, 0);
715 v4l2_ctrl_new_std(hdlr, &ipu_csc_scaler_ctrl_ops, V4L2_CID_VFLIP,
716 0, 1, 1, 0);
717 v4l2_ctrl_new_std(hdlr, &ipu_csc_scaler_ctrl_ops, V4L2_CID_ROTATE,
718 0, 270, 90, 0);
719
720 if (hdlr->error) {
721 v4l2_ctrl_handler_free(hdlr);
722 return hdlr->error;
723 }
724
725 v4l2_ctrl_handler_setup(hdlr);
726 return 0;
727 }
728
729 #define DEFAULT_WIDTH 720
730 #define DEFAULT_HEIGHT 576
731 static const struct ipu_csc_scaler_q_data ipu_csc_scaler_q_data_default = {
732 .cur_fmt = {
733 .width = DEFAULT_WIDTH,
734 .height = DEFAULT_HEIGHT,
735 .pixelformat = V4L2_PIX_FMT_YUV420,
736 .field = V4L2_FIELD_NONE,
737 .bytesperline = DEFAULT_WIDTH,
738 .sizeimage = DEFAULT_WIDTH * DEFAULT_HEIGHT * 3 / 2,
739 .colorspace = V4L2_COLORSPACE_SRGB,
740 },
741 .rect = {
742 .width = DEFAULT_WIDTH,
743 .height = DEFAULT_HEIGHT,
744 },
745 };
746
747 /*
748 * File operations
749 */
ipu_csc_scaler_open(struct file * file)750 static int ipu_csc_scaler_open(struct file *file)
751 {
752 struct ipu_csc_scaler_priv *priv = video_drvdata(file);
753 struct ipu_csc_scaler_ctx *ctx = NULL;
754 int ret;
755
756 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
757 if (!ctx)
758 return -ENOMEM;
759
760 ctx->rot_mode = IPU_ROTATE_NONE;
761
762 v4l2_fh_init(&ctx->fh, video_devdata(file));
763 file->private_data = &ctx->fh;
764 v4l2_fh_add(&ctx->fh);
765 ctx->priv = priv;
766
767 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(priv->m2m_dev, ctx,
768 &ipu_csc_scaler_queue_init);
769 if (IS_ERR(ctx->fh.m2m_ctx)) {
770 ret = PTR_ERR(ctx->fh.m2m_ctx);
771 goto err_ctx;
772 }
773
774 ret = ipu_csc_scaler_init_controls(ctx);
775 if (ret)
776 goto err_ctrls;
777
778 ctx->fh.ctrl_handler = &ctx->ctrl_hdlr;
779
780 ctx->q_data[V4L2_M2M_SRC] = ipu_csc_scaler_q_data_default;
781 ctx->q_data[V4L2_M2M_DST] = ipu_csc_scaler_q_data_default;
782
783 dev_dbg(priv->dev, "Created instance %p, m2m_ctx: %p\n", ctx,
784 ctx->fh.m2m_ctx);
785
786 return 0;
787
788 err_ctrls:
789 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
790 err_ctx:
791 v4l2_fh_del(&ctx->fh);
792 v4l2_fh_exit(&ctx->fh);
793 kfree(ctx);
794 return ret;
795 }
796
ipu_csc_scaler_release(struct file * file)797 static int ipu_csc_scaler_release(struct file *file)
798 {
799 struct ipu_csc_scaler_priv *priv = video_drvdata(file);
800 struct ipu_csc_scaler_ctx *ctx = fh_to_ctx(file->private_data);
801
802 dev_dbg(priv->dev, "Releasing instance %p\n", ctx);
803
804 v4l2_ctrl_handler_free(&ctx->ctrl_hdlr);
805 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
806 v4l2_fh_del(&ctx->fh);
807 v4l2_fh_exit(&ctx->fh);
808 kfree(ctx);
809
810 return 0;
811 }
812
813 static const struct v4l2_file_operations ipu_csc_scaler_fops = {
814 .owner = THIS_MODULE,
815 .open = ipu_csc_scaler_open,
816 .release = ipu_csc_scaler_release,
817 .poll = v4l2_m2m_fop_poll,
818 .unlocked_ioctl = video_ioctl2,
819 .mmap = v4l2_m2m_fop_mmap,
820 };
821
822 static const struct v4l2_m2m_ops m2m_ops = {
823 .device_run = device_run,
824 .job_abort = job_abort,
825 };
826
ipu_csc_scaler_video_device_release(struct video_device * vdev)827 static void ipu_csc_scaler_video_device_release(struct video_device *vdev)
828 {
829 struct ipu_csc_scaler_priv *priv = video_get_drvdata(vdev);
830
831 v4l2_m2m_release(priv->m2m_dev);
832 video_device_release(vdev);
833 kfree(priv);
834 }
835
836 static const struct video_device ipu_csc_scaler_videodev_template = {
837 .name = "ipu_ic_pp csc/scaler",
838 .fops = &ipu_csc_scaler_fops,
839 .ioctl_ops = &ipu_csc_scaler_ioctl_ops,
840 .minor = -1,
841 .release = ipu_csc_scaler_video_device_release,
842 .vfl_dir = VFL_DIR_M2M,
843 .device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
844 };
845
imx_media_csc_scaler_device_register(struct imx_media_video_dev * vdev)846 int imx_media_csc_scaler_device_register(struct imx_media_video_dev *vdev)
847 {
848 struct ipu_csc_scaler_priv *priv = vdev_to_priv(vdev);
849 struct video_device *vfd = vdev->vfd;
850 int ret;
851
852 vfd->v4l2_dev = &priv->md->v4l2_dev;
853
854 ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
855 if (ret) {
856 v4l2_err(vfd->v4l2_dev, "Failed to register video device\n");
857 return ret;
858 }
859
860 v4l2_info(vfd->v4l2_dev, "Registered %s as /dev/%s\n", vfd->name,
861 video_device_node_name(vfd));
862
863 return 0;
864 }
865
imx_media_csc_scaler_device_unregister(struct imx_media_video_dev * vdev)866 void imx_media_csc_scaler_device_unregister(struct imx_media_video_dev *vdev)
867 {
868 struct ipu_csc_scaler_priv *priv = vdev_to_priv(vdev);
869 struct video_device *vfd = priv->vdev.vfd;
870
871 video_unregister_device(vfd);
872 }
873
874 struct imx_media_video_dev *
imx_media_csc_scaler_device_init(struct imx_media_dev * md)875 imx_media_csc_scaler_device_init(struct imx_media_dev *md)
876 {
877 struct ipu_csc_scaler_priv *priv;
878 struct video_device *vfd;
879 int ret;
880
881 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
882 if (!priv)
883 return ERR_PTR(-ENOMEM);
884
885 priv->md = md;
886 priv->dev = md->md.dev;
887
888 mutex_init(&priv->mutex);
889
890 vfd = video_device_alloc();
891 if (!vfd) {
892 ret = -ENOMEM;
893 goto err_vfd;
894 }
895
896 *vfd = ipu_csc_scaler_videodev_template;
897 vfd->lock = &priv->mutex;
898 priv->vdev.vfd = vfd;
899
900 INIT_LIST_HEAD(&priv->vdev.list);
901
902 video_set_drvdata(vfd, priv);
903
904 priv->m2m_dev = v4l2_m2m_init(&m2m_ops);
905 if (IS_ERR(priv->m2m_dev)) {
906 ret = PTR_ERR(priv->m2m_dev);
907 v4l2_err(&md->v4l2_dev, "Failed to init mem2mem device: %d\n",
908 ret);
909 goto err_m2m;
910 }
911
912 return &priv->vdev;
913
914 err_m2m:
915 video_set_drvdata(vfd, NULL);
916 err_vfd:
917 kfree(priv);
918 return ERR_PTR(ret);
919 }
920
921 MODULE_DESCRIPTION("i.MX IPUv3 mem2mem scaler/CSC driver");
922 MODULE_AUTHOR("Sascha Hauer <[email protected]>");
923 MODULE_LICENSE("GPL");
924