1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) STMicroelectronics SA 2015
4 * Authors: Hugues Fruchet <hugues.fruchet@st.com>
5 * Jean-Christophe Trotin <jean-christophe.trotin@st.com>
6 * for STMicroelectronics.
7 */
8
9 #include <linux/clk.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/slab.h>
14
15 #include <media/v4l2-ioctl.h>
16 #include <media/v4l2-event.h>
17 #include <media/videobuf2-dma-contig.h>
18
19 #include "delta.h"
20 #include "delta-debug.h"
21 #include "delta-ipc.h"
22
23 #define DELTA_NAME "st-delta"
24
25 #define DELTA_PREFIX "[---:----]"
26
27 #define to_ctx(__fh) container_of(__fh, struct delta_ctx, fh)
28 #define to_au(__vbuf) container_of(__vbuf, struct delta_au, vbuf)
29 #define to_frame(__vbuf) container_of(__vbuf, struct delta_frame, vbuf)
30
31 #define call_dec_op(dec, op, args...)\
32 ((dec && (dec)->op) ? (dec)->op(args) : 0)
33
34 /* registry of available decoders */
35 static const struct delta_dec *delta_decoders[] = {
36 #ifdef CONFIG_VIDEO_STI_DELTA_MJPEG
37 &mjpegdec,
38 #endif
39 };
40
frame_size(u32 w,u32 h,u32 fmt)41 static inline int frame_size(u32 w, u32 h, u32 fmt)
42 {
43 switch (fmt) {
44 case V4L2_PIX_FMT_NV12:
45 return (w * h * 3) / 2;
46 default:
47 return 0;
48 }
49 }
50
frame_stride(u32 w,u32 fmt)51 static inline int frame_stride(u32 w, u32 fmt)
52 {
53 switch (fmt) {
54 case V4L2_PIX_FMT_NV12:
55 return w;
56 default:
57 return 0;
58 }
59 }
60
dump_au(struct delta_ctx * ctx,struct delta_au * au)61 static void dump_au(struct delta_ctx *ctx, struct delta_au *au)
62 {
63 struct delta_dev *delta = ctx->dev;
64 u32 size = 10; /* dump first & last 10 bytes */
65 u8 *data = (u8 *)(au->vaddr);
66
67 if (au->size <= (size * 2))
68 dev_dbg(delta->dev, "%s dump au[%d] dts=%lld size=%d data=%*ph\n",
69 ctx->name, au->vbuf.vb2_buf.index, au->dts, au->size,
70 au->size, data);
71 else
72 dev_dbg(delta->dev, "%s dump au[%d] dts=%lld size=%d data=%*ph..%*ph\n",
73 ctx->name, au->vbuf.vb2_buf.index, au->dts, au->size,
74 size, data, size, data + au->size - size);
75 }
76
dump_frame(struct delta_ctx * ctx,struct delta_frame * frame)77 static void dump_frame(struct delta_ctx *ctx, struct delta_frame *frame)
78 {
79 struct delta_dev *delta = ctx->dev;
80 u32 size = 10; /* dump first 10 bytes */
81 u8 *data = (u8 *)(frame->vaddr);
82
83 dev_dbg(delta->dev, "%s dump frame[%d] dts=%lld type=%s field=%s data=%*ph\n",
84 ctx->name, frame->index, frame->dts,
85 frame_type_str(frame->flags),
86 frame_field_str(frame->field),
87 size, data);
88 }
89
delta_au_done(struct delta_ctx * ctx,struct delta_au * au,int err)90 static void delta_au_done(struct delta_ctx *ctx, struct delta_au *au, int err)
91 {
92 struct vb2_v4l2_buffer *vbuf;
93
94 vbuf = &au->vbuf;
95 vbuf->sequence = ctx->au_num++;
96 v4l2_m2m_buf_done(vbuf, err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
97 }
98
delta_frame_done(struct delta_ctx * ctx,struct delta_frame * frame,int err)99 static void delta_frame_done(struct delta_ctx *ctx, struct delta_frame *frame,
100 int err)
101 {
102 struct vb2_v4l2_buffer *vbuf;
103
104 dump_frame(ctx, frame);
105
106 /* decoded frame is now output to user */
107 frame->state |= DELTA_FRAME_OUT;
108
109 vbuf = &frame->vbuf;
110 vbuf->sequence = ctx->frame_num++;
111 v4l2_m2m_buf_done(vbuf, err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
112
113 if (frame->info.size) /* ignore EOS */
114 ctx->output_frames++;
115 }
116
requeue_free_frames(struct delta_ctx * ctx)117 static void requeue_free_frames(struct delta_ctx *ctx)
118 {
119 struct vb2_v4l2_buffer *vbuf;
120 struct delta_frame *frame;
121 unsigned int i;
122
123 /* requeue all free frames */
124 for (i = 0; i < ctx->nb_of_frames; i++) {
125 frame = ctx->frames[i];
126 if (frame->state == DELTA_FRAME_FREE) {
127 vbuf = &frame->vbuf;
128 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
129 frame->state = DELTA_FRAME_M2M;
130 }
131 }
132 }
133
delta_recycle(struct delta_ctx * ctx,struct delta_frame * frame)134 static int delta_recycle(struct delta_ctx *ctx, struct delta_frame *frame)
135 {
136 const struct delta_dec *dec = ctx->dec;
137
138 /* recycle frame on decoder side */
139 call_dec_op(dec, recycle, ctx, frame);
140
141 /* this frame is no more output */
142 frame->state &= ~DELTA_FRAME_OUT;
143
144 /* requeue free frame */
145 if (frame->state == DELTA_FRAME_FREE) {
146 struct vb2_v4l2_buffer *vbuf = &frame->vbuf;
147
148 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
149 frame->state = DELTA_FRAME_M2M;
150 }
151
152 /* reset other frame fields */
153 frame->flags = 0;
154 frame->dts = 0;
155
156 return 0;
157 }
158
delta_push_dts(struct delta_ctx * ctx,u64 val)159 static void delta_push_dts(struct delta_ctx *ctx, u64 val)
160 {
161 struct delta_dts *dts;
162
163 dts = kzalloc(sizeof(*dts), GFP_KERNEL);
164 if (!dts)
165 return;
166
167 INIT_LIST_HEAD(&dts->list);
168
169 /*
170 * protected by global lock acquired
171 * by V4L2 when calling delta_vb2_au_queue
172 */
173 dts->val = val;
174 list_add_tail(&dts->list, &ctx->dts);
175 }
176
delta_pop_dts(struct delta_ctx * ctx,u64 * val)177 static void delta_pop_dts(struct delta_ctx *ctx, u64 *val)
178 {
179 struct delta_dev *delta = ctx->dev;
180 struct delta_dts *dts;
181
182 /*
183 * protected by global lock acquired
184 * by V4L2 when calling delta_vb2_au_queue
185 */
186 if (list_empty(&ctx->dts)) {
187 dev_warn(delta->dev, "%s no dts to pop ... output dts = 0\n",
188 ctx->name);
189 *val = 0;
190 return;
191 }
192
193 dts = list_first_entry(&ctx->dts, struct delta_dts, list);
194 list_del(&dts->list);
195
196 *val = dts->val;
197
198 kfree(dts);
199 }
200
delta_flush_dts(struct delta_ctx * ctx)201 static void delta_flush_dts(struct delta_ctx *ctx)
202 {
203 struct delta_dts *dts;
204 struct delta_dts *next;
205
206 /*
207 * protected by global lock acquired
208 * by V4L2 when calling delta_vb2_au_queue
209 */
210
211 /* free all pending dts */
212 list_for_each_entry_safe(dts, next, &ctx->dts, list)
213 kfree(dts);
214
215 /* reset list */
216 INIT_LIST_HEAD(&ctx->dts);
217 }
218
frame_alignment(u32 fmt)219 static inline int frame_alignment(u32 fmt)
220 {
221 switch (fmt) {
222 case V4L2_PIX_FMT_NV12:
223 case V4L2_PIX_FMT_NV21:
224 /* multiple of 2 */
225 return 2;
226 default:
227 return 1;
228 }
229 }
230
estimated_au_size(u32 w,u32 h)231 static inline int estimated_au_size(u32 w, u32 h)
232 {
233 /*
234 * for a MJPEG stream encoded from YUV422 pixel format,
235 * assuming a compression ratio of 2, the maximum size
236 * of an access unit is (width x height x 2) / 2,
237 * so (width x height)
238 */
239 return (w * h);
240 }
241
set_default_params(struct delta_ctx * ctx)242 static void set_default_params(struct delta_ctx *ctx)
243 {
244 struct delta_frameinfo *frameinfo = &ctx->frameinfo;
245 struct delta_streaminfo *streaminfo = &ctx->streaminfo;
246
247 memset(frameinfo, 0, sizeof(*frameinfo));
248 frameinfo->pixelformat = V4L2_PIX_FMT_NV12;
249 frameinfo->width = DELTA_DEFAULT_WIDTH;
250 frameinfo->height = DELTA_DEFAULT_HEIGHT;
251 frameinfo->aligned_width = ALIGN(frameinfo->width,
252 DELTA_WIDTH_ALIGNMENT);
253 frameinfo->aligned_height = ALIGN(frameinfo->height,
254 DELTA_HEIGHT_ALIGNMENT);
255 frameinfo->size = frame_size(frameinfo->aligned_width,
256 frameinfo->aligned_height,
257 frameinfo->pixelformat);
258 frameinfo->field = V4L2_FIELD_NONE;
259 frameinfo->colorspace = V4L2_COLORSPACE_REC709;
260 frameinfo->xfer_func = V4L2_XFER_FUNC_DEFAULT;
261 frameinfo->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
262 frameinfo->quantization = V4L2_QUANTIZATION_DEFAULT;
263
264 memset(streaminfo, 0, sizeof(*streaminfo));
265 streaminfo->streamformat = DELTA_DEFAULT_STREAMFORMAT;
266 streaminfo->width = DELTA_DEFAULT_WIDTH;
267 streaminfo->height = DELTA_DEFAULT_HEIGHT;
268 streaminfo->field = V4L2_FIELD_NONE;
269 streaminfo->colorspace = V4L2_COLORSPACE_REC709;
270 streaminfo->xfer_func = V4L2_XFER_FUNC_DEFAULT;
271 streaminfo->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
272 streaminfo->quantization = V4L2_QUANTIZATION_DEFAULT;
273
274 ctx->max_au_size = estimated_au_size(streaminfo->width,
275 streaminfo->height);
276 }
277
delta_find_decoder(struct delta_ctx * ctx,u32 streamformat,u32 pixelformat)278 static const struct delta_dec *delta_find_decoder(struct delta_ctx *ctx,
279 u32 streamformat,
280 u32 pixelformat)
281 {
282 struct delta_dev *delta = ctx->dev;
283 const struct delta_dec *dec;
284 unsigned int i;
285
286 for (i = 0; i < delta->nb_of_decoders; i++) {
287 dec = delta->decoders[i];
288 if ((dec->pixelformat == pixelformat) &&
289 (dec->streamformat == streamformat))
290 return dec;
291 }
292
293 return NULL;
294 }
295
register_format(u32 format,u32 formats[],u32 * nb_of_formats)296 static void register_format(u32 format, u32 formats[], u32 *nb_of_formats)
297 {
298 u32 i;
299
300 for (i = 0; i < *nb_of_formats; i++) {
301 if (format == formats[i])
302 return;
303 }
304
305 formats[(*nb_of_formats)++] = format;
306 }
307
register_formats(struct delta_dev * delta)308 static void register_formats(struct delta_dev *delta)
309 {
310 unsigned int i;
311
312 for (i = 0; i < delta->nb_of_decoders; i++) {
313 register_format(delta->decoders[i]->pixelformat,
314 delta->pixelformats,
315 &delta->nb_of_pixelformats);
316
317 register_format(delta->decoders[i]->streamformat,
318 delta->streamformats,
319 &delta->nb_of_streamformats);
320 }
321 }
322
register_decoders(struct delta_dev * delta)323 static void register_decoders(struct delta_dev *delta)
324 {
325 unsigned int i;
326
327 for (i = 0; i < ARRAY_SIZE(delta_decoders); i++) {
328 if (delta->nb_of_decoders >= DELTA_MAX_DECODERS) {
329 dev_dbg(delta->dev,
330 "%s failed to register %s decoder (%d maximum reached)\n",
331 DELTA_PREFIX, delta_decoders[i]->name,
332 DELTA_MAX_DECODERS);
333 return;
334 }
335
336 delta->decoders[delta->nb_of_decoders++] = delta_decoders[i];
337 dev_info(delta->dev, "%s %s decoder registered\n",
338 DELTA_PREFIX, delta_decoders[i]->name);
339 }
340 }
341
delta_open_decoder(struct delta_ctx * ctx,u32 streamformat,u32 pixelformat,const struct delta_dec ** pdec)342 static int delta_open_decoder(struct delta_ctx *ctx, u32 streamformat,
343 u32 pixelformat, const struct delta_dec **pdec)
344 {
345 struct delta_dev *delta = ctx->dev;
346 const struct delta_dec *dec;
347 int ret;
348
349 dec = delta_find_decoder(ctx, streamformat, ctx->frameinfo.pixelformat);
350 if (!dec) {
351 dev_err(delta->dev, "%s no decoder found matching %4.4s => %4.4s\n",
352 ctx->name, (char *)&streamformat, (char *)&pixelformat);
353 return -EINVAL;
354 }
355
356 dev_dbg(delta->dev, "%s one decoder matching %4.4s => %4.4s\n",
357 ctx->name, (char *)&streamformat, (char *)&pixelformat);
358
359 /* update instance name */
360 snprintf(ctx->name, sizeof(ctx->name), "[%3d:%4.4s]",
361 delta->instance_id, (char *)&streamformat);
362
363 /* open decoder instance */
364 ret = call_dec_op(dec, open, ctx);
365 if (ret) {
366 dev_err(delta->dev, "%s failed to open decoder instance (%d)\n",
367 ctx->name, ret);
368 return ret;
369 }
370
371 dev_dbg(delta->dev, "%s %s decoder opened\n", ctx->name, dec->name);
372
373 *pdec = dec;
374
375 return ret;
376 }
377
378 /*
379 * V4L2 ioctl operations
380 */
381
delta_querycap(struct file * file,void * priv,struct v4l2_capability * cap)382 static int delta_querycap(struct file *file, void *priv,
383 struct v4l2_capability *cap)
384 {
385 struct delta_ctx *ctx = to_ctx(file->private_data);
386 struct delta_dev *delta = ctx->dev;
387
388 strscpy(cap->driver, DELTA_NAME, sizeof(cap->driver));
389 strscpy(cap->card, delta->vdev->name, sizeof(cap->card));
390 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
391 delta->pdev->name);
392
393 return 0;
394 }
395
delta_enum_fmt_stream(struct file * file,void * priv,struct v4l2_fmtdesc * f)396 static int delta_enum_fmt_stream(struct file *file, void *priv,
397 struct v4l2_fmtdesc *f)
398 {
399 struct delta_ctx *ctx = to_ctx(file->private_data);
400 struct delta_dev *delta = ctx->dev;
401
402 if (unlikely(f->index >= delta->nb_of_streamformats))
403 return -EINVAL;
404
405 f->pixelformat = delta->streamformats[f->index];
406
407 return 0;
408 }
409
delta_enum_fmt_frame(struct file * file,void * priv,struct v4l2_fmtdesc * f)410 static int delta_enum_fmt_frame(struct file *file, void *priv,
411 struct v4l2_fmtdesc *f)
412 {
413 struct delta_ctx *ctx = to_ctx(file->private_data);
414 struct delta_dev *delta = ctx->dev;
415
416 if (unlikely(f->index >= delta->nb_of_pixelformats))
417 return -EINVAL;
418
419 f->pixelformat = delta->pixelformats[f->index];
420
421 return 0;
422 }
423
delta_g_fmt_stream(struct file * file,void * fh,struct v4l2_format * f)424 static int delta_g_fmt_stream(struct file *file, void *fh,
425 struct v4l2_format *f)
426 {
427 struct delta_ctx *ctx = to_ctx(file->private_data);
428 struct delta_dev *delta = ctx->dev;
429 struct v4l2_pix_format *pix = &f->fmt.pix;
430 struct delta_streaminfo *streaminfo = &ctx->streaminfo;
431 unsigned char str[100] = "";
432
433 if (!(ctx->flags & DELTA_FLAG_STREAMINFO))
434 dev_dbg(delta->dev,
435 "%s V4L2 GET_FMT (OUTPUT): no stream information available, default to %s\n",
436 ctx->name,
437 delta_streaminfo_str(streaminfo, str, sizeof(str)));
438
439 pix->pixelformat = streaminfo->streamformat;
440 pix->width = streaminfo->width;
441 pix->height = streaminfo->height;
442 pix->field = streaminfo->field;
443 pix->bytesperline = 0;
444 pix->sizeimage = ctx->max_au_size;
445 pix->colorspace = streaminfo->colorspace;
446 pix->xfer_func = streaminfo->xfer_func;
447 pix->ycbcr_enc = streaminfo->ycbcr_enc;
448 pix->quantization = streaminfo->quantization;
449
450 return 0;
451 }
452
delta_g_fmt_frame(struct file * file,void * fh,struct v4l2_format * f)453 static int delta_g_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
454 {
455 struct delta_ctx *ctx = to_ctx(file->private_data);
456 struct delta_dev *delta = ctx->dev;
457 struct v4l2_pix_format *pix = &f->fmt.pix;
458 struct delta_frameinfo *frameinfo = &ctx->frameinfo;
459 struct delta_streaminfo *streaminfo = &ctx->streaminfo;
460 unsigned char str[100] = "";
461
462 if (!(ctx->flags & DELTA_FLAG_FRAMEINFO))
463 dev_dbg(delta->dev,
464 "%s V4L2 GET_FMT (CAPTURE): no frame information available, default to %s\n",
465 ctx->name,
466 delta_frameinfo_str(frameinfo, str, sizeof(str)));
467
468 pix->pixelformat = frameinfo->pixelformat;
469 pix->width = frameinfo->aligned_width;
470 pix->height = frameinfo->aligned_height;
471 pix->field = frameinfo->field;
472 pix->bytesperline = frame_stride(frameinfo->aligned_width,
473 frameinfo->pixelformat);
474 pix->sizeimage = frameinfo->size;
475
476 if (ctx->flags & DELTA_FLAG_STREAMINFO) {
477 /* align colorspace & friends on stream ones if any set */
478 frameinfo->colorspace = streaminfo->colorspace;
479 frameinfo->xfer_func = streaminfo->xfer_func;
480 frameinfo->ycbcr_enc = streaminfo->ycbcr_enc;
481 frameinfo->quantization = streaminfo->quantization;
482 }
483 pix->colorspace = frameinfo->colorspace;
484 pix->xfer_func = frameinfo->xfer_func;
485 pix->ycbcr_enc = frameinfo->ycbcr_enc;
486 pix->quantization = frameinfo->quantization;
487
488 return 0;
489 }
490
delta_try_fmt_stream(struct file * file,void * priv,struct v4l2_format * f)491 static int delta_try_fmt_stream(struct file *file, void *priv,
492 struct v4l2_format *f)
493 {
494 struct delta_ctx *ctx = to_ctx(file->private_data);
495 struct delta_dev *delta = ctx->dev;
496 struct v4l2_pix_format *pix = &f->fmt.pix;
497 u32 streamformat = pix->pixelformat;
498 const struct delta_dec *dec;
499 u32 width, height;
500 u32 au_size;
501
502 dec = delta_find_decoder(ctx, streamformat, ctx->frameinfo.pixelformat);
503 if (!dec) {
504 dev_dbg(delta->dev,
505 "%s V4L2 TRY_FMT (OUTPUT): unsupported format %4.4s\n",
506 ctx->name, (char *)&pix->pixelformat);
507 return -EINVAL;
508 }
509
510 /* adjust width & height */
511 width = pix->width;
512 height = pix->height;
513 v4l_bound_align_image
514 (&pix->width,
515 DELTA_MIN_WIDTH,
516 dec->max_width ? dec->max_width : DELTA_MAX_WIDTH,
517 0,
518 &pix->height,
519 DELTA_MIN_HEIGHT,
520 dec->max_height ? dec->max_height : DELTA_MAX_HEIGHT,
521 0, 0);
522
523 if ((pix->width != width) || (pix->height != height))
524 dev_dbg(delta->dev,
525 "%s V4L2 TRY_FMT (OUTPUT): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
526 ctx->name, width, height,
527 pix->width, pix->height);
528
529 au_size = estimated_au_size(pix->width, pix->height);
530 if (pix->sizeimage < au_size) {
531 dev_dbg(delta->dev,
532 "%s V4L2 TRY_FMT (OUTPUT): size updated %d -> %d to fit estimated size\n",
533 ctx->name, pix->sizeimage, au_size);
534 pix->sizeimage = au_size;
535 }
536
537 pix->bytesperline = 0;
538
539 if (pix->field == V4L2_FIELD_ANY)
540 pix->field = V4L2_FIELD_NONE;
541
542 return 0;
543 }
544
delta_try_fmt_frame(struct file * file,void * priv,struct v4l2_format * f)545 static int delta_try_fmt_frame(struct file *file, void *priv,
546 struct v4l2_format *f)
547 {
548 struct delta_ctx *ctx = to_ctx(file->private_data);
549 struct delta_dev *delta = ctx->dev;
550 struct v4l2_pix_format *pix = &f->fmt.pix;
551 u32 pixelformat = pix->pixelformat;
552 const struct delta_dec *dec;
553 u32 width, height;
554
555 dec = delta_find_decoder(ctx, ctx->streaminfo.streamformat,
556 pixelformat);
557 if (!dec) {
558 dev_dbg(delta->dev,
559 "%s V4L2 TRY_FMT (CAPTURE): unsupported format %4.4s\n",
560 ctx->name, (char *)&pixelformat);
561 return -EINVAL;
562 }
563
564 /* adjust width & height */
565 width = pix->width;
566 height = pix->height;
567 v4l_bound_align_image(&pix->width,
568 DELTA_MIN_WIDTH, DELTA_MAX_WIDTH,
569 frame_alignment(pixelformat) - 1,
570 &pix->height,
571 DELTA_MIN_HEIGHT, DELTA_MAX_HEIGHT,
572 frame_alignment(pixelformat) - 1, 0);
573
574 if ((pix->width != width) || (pix->height != height))
575 dev_dbg(delta->dev,
576 "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
577 ctx->name, width, height, pix->width, pix->height);
578
579 /* default decoder alignment constraint */
580 width = ALIGN(pix->width, DELTA_WIDTH_ALIGNMENT);
581 height = ALIGN(pix->height, DELTA_HEIGHT_ALIGNMENT);
582 if ((pix->width != width) || (pix->height != height))
583 dev_dbg(delta->dev,
584 "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit decoder alignment\n",
585 ctx->name, width, height, pix->width, pix->height);
586
587 if (!pix->colorspace) {
588 pix->colorspace = V4L2_COLORSPACE_REC709;
589 pix->xfer_func = V4L2_XFER_FUNC_DEFAULT;
590 pix->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
591 pix->quantization = V4L2_QUANTIZATION_DEFAULT;
592 }
593
594 pix->width = width;
595 pix->height = height;
596 pix->bytesperline = frame_stride(pix->width, pixelformat);
597 pix->sizeimage = frame_size(pix->width, pix->height, pixelformat);
598
599 if (pix->field == V4L2_FIELD_ANY)
600 pix->field = V4L2_FIELD_NONE;
601
602 return 0;
603 }
604
delta_s_fmt_stream(struct file * file,void * fh,struct v4l2_format * f)605 static int delta_s_fmt_stream(struct file *file, void *fh,
606 struct v4l2_format *f)
607 {
608 struct delta_ctx *ctx = to_ctx(file->private_data);
609 struct delta_dev *delta = ctx->dev;
610 struct vb2_queue *vq;
611 struct v4l2_pix_format *pix = &f->fmt.pix;
612 int ret;
613
614 ret = delta_try_fmt_stream(file, fh, f);
615 if (ret) {
616 dev_dbg(delta->dev,
617 "%s V4L2 S_FMT (OUTPUT): unsupported format %4.4s\n",
618 ctx->name, (char *)&pix->pixelformat);
619 return ret;
620 }
621
622 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
623 if (vb2_is_streaming(vq)) {
624 dev_dbg(delta->dev, "%s V4L2 S_FMT (OUTPUT): queue busy\n",
625 ctx->name);
626 return -EBUSY;
627 }
628
629 ctx->max_au_size = pix->sizeimage;
630 ctx->streaminfo.width = pix->width;
631 ctx->streaminfo.height = pix->height;
632 ctx->streaminfo.streamformat = pix->pixelformat;
633 ctx->streaminfo.colorspace = pix->colorspace;
634 ctx->streaminfo.xfer_func = pix->xfer_func;
635 ctx->streaminfo.ycbcr_enc = pix->ycbcr_enc;
636 ctx->streaminfo.quantization = pix->quantization;
637 ctx->flags |= DELTA_FLAG_STREAMINFO;
638
639 return 0;
640 }
641
delta_s_fmt_frame(struct file * file,void * fh,struct v4l2_format * f)642 static int delta_s_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
643 {
644 struct delta_ctx *ctx = to_ctx(file->private_data);
645 struct delta_dev *delta = ctx->dev;
646 const struct delta_dec *dec = ctx->dec;
647 struct v4l2_pix_format *pix = &f->fmt.pix;
648 struct delta_frameinfo frameinfo;
649 unsigned char str[100] = "";
650 struct vb2_queue *vq;
651 int ret;
652
653 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
654 if (vb2_is_streaming(vq)) {
655 dev_dbg(delta->dev, "%s V4L2 S_FMT (CAPTURE): queue busy\n",
656 ctx->name);
657 return -EBUSY;
658 }
659
660 if (ctx->state < DELTA_STATE_READY) {
661 /*
662 * decoder not yet opened and valid stream header not found,
663 * could not negotiate format with decoder, check at least
664 * pixel format & negotiate resolution boundaries
665 * and alignment...
666 */
667 ret = delta_try_fmt_frame(file, fh, f);
668 if (ret) {
669 dev_dbg(delta->dev,
670 "%s V4L2 S_FMT (CAPTURE): unsupported format %4.4s\n",
671 ctx->name, (char *)&pix->pixelformat);
672 return ret;
673 }
674
675 return 0;
676 }
677
678 /* set frame information to decoder */
679 memset(&frameinfo, 0, sizeof(frameinfo));
680 frameinfo.pixelformat = pix->pixelformat;
681 frameinfo.width = pix->width;
682 frameinfo.height = pix->height;
683 frameinfo.aligned_width = pix->width;
684 frameinfo.aligned_height = pix->height;
685 frameinfo.size = pix->sizeimage;
686 frameinfo.field = pix->field;
687 frameinfo.colorspace = pix->colorspace;
688 frameinfo.xfer_func = pix->xfer_func;
689 frameinfo.ycbcr_enc = pix->ycbcr_enc;
690 frameinfo.quantization = pix->quantization;
691 ret = call_dec_op(dec, set_frameinfo, ctx, &frameinfo);
692 if (ret)
693 return ret;
694
695 /* then get what decoder can really do */
696 ret = call_dec_op(dec, get_frameinfo, ctx, &frameinfo);
697 if (ret)
698 return ret;
699
700 ctx->flags |= DELTA_FLAG_FRAMEINFO;
701 ctx->frameinfo = frameinfo;
702 dev_dbg(delta->dev,
703 "%s V4L2 SET_FMT (CAPTURE): frameinfo updated to %s\n",
704 ctx->name,
705 delta_frameinfo_str(&frameinfo, str, sizeof(str)));
706
707 pix->pixelformat = frameinfo.pixelformat;
708 pix->width = frameinfo.aligned_width;
709 pix->height = frameinfo.aligned_height;
710 pix->bytesperline = frame_stride(pix->width, pix->pixelformat);
711 pix->sizeimage = frameinfo.size;
712 pix->field = frameinfo.field;
713 pix->colorspace = frameinfo.colorspace;
714 pix->xfer_func = frameinfo.xfer_func;
715 pix->ycbcr_enc = frameinfo.ycbcr_enc;
716 pix->quantization = frameinfo.quantization;
717
718 return 0;
719 }
720
delta_g_selection(struct file * file,void * fh,struct v4l2_selection * s)721 static int delta_g_selection(struct file *file, void *fh,
722 struct v4l2_selection *s)
723 {
724 struct delta_ctx *ctx = to_ctx(fh);
725 struct delta_frameinfo *frameinfo = &ctx->frameinfo;
726 struct v4l2_rect crop;
727
728 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
729 return -EINVAL;
730
731 if ((ctx->flags & DELTA_FLAG_FRAMEINFO) &&
732 (frameinfo->flags & DELTA_FRAMEINFO_FLAG_CROP)) {
733 crop = frameinfo->crop;
734 } else {
735 /* default to video dimensions */
736 crop.left = 0;
737 crop.top = 0;
738 crop.width = frameinfo->width;
739 crop.height = frameinfo->height;
740 }
741
742 switch (s->target) {
743 case V4L2_SEL_TGT_COMPOSE:
744 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
745 /* visible area inside video */
746 s->r = crop;
747 break;
748 case V4L2_SEL_TGT_COMPOSE_PADDED:
749 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
750 /* up to aligned dimensions */
751 s->r.left = 0;
752 s->r.top = 0;
753 s->r.width = frameinfo->aligned_width;
754 s->r.height = frameinfo->aligned_height;
755 break;
756 default:
757 return -EINVAL;
758 }
759
760 return 0;
761 }
762
delta_complete_eos(struct delta_ctx * ctx,struct delta_frame * frame)763 static void delta_complete_eos(struct delta_ctx *ctx,
764 struct delta_frame *frame)
765 {
766 struct delta_dev *delta = ctx->dev;
767 const struct v4l2_event ev = {.type = V4L2_EVENT_EOS};
768
769 /*
770 * Send EOS to user:
771 * - by returning an empty frame flagged to V4L2_BUF_FLAG_LAST
772 * - and then send EOS event
773 */
774
775 /* empty frame */
776 frame->info.size = 0;
777
778 /* set the last buffer flag */
779 frame->flags |= V4L2_BUF_FLAG_LAST;
780
781 /* release frame to user */
782 delta_frame_done(ctx, frame, 0);
783
784 /* send EOS event */
785 v4l2_event_queue_fh(&ctx->fh, &ev);
786
787 dev_dbg(delta->dev, "%s EOS completed\n", ctx->name);
788 }
789
delta_try_decoder_cmd(struct file * file,void * fh,struct v4l2_decoder_cmd * cmd)790 static int delta_try_decoder_cmd(struct file *file, void *fh,
791 struct v4l2_decoder_cmd *cmd)
792 {
793 if (cmd->cmd != V4L2_DEC_CMD_STOP)
794 return -EINVAL;
795
796 if (cmd->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
797 return -EINVAL;
798
799 if (!(cmd->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) &&
800 (cmd->stop.pts != 0))
801 return -EINVAL;
802
803 return 0;
804 }
805
delta_decoder_stop_cmd(struct delta_ctx * ctx,void * fh)806 static int delta_decoder_stop_cmd(struct delta_ctx *ctx, void *fh)
807 {
808 const struct delta_dec *dec = ctx->dec;
809 struct delta_dev *delta = ctx->dev;
810 struct delta_frame *frame = NULL;
811 int ret = 0;
812
813 dev_dbg(delta->dev, "%s EOS received\n", ctx->name);
814
815 if (ctx->state != DELTA_STATE_READY)
816 return 0;
817
818 /* drain the decoder */
819 call_dec_op(dec, drain, ctx);
820
821 /* release to user drained frames */
822 while (1) {
823 frame = NULL;
824 ret = call_dec_op(dec, get_frame, ctx, &frame);
825 if (ret == -ENODATA) {
826 /* no more decoded frames */
827 break;
828 }
829 if (frame) {
830 dev_dbg(delta->dev, "%s drain frame[%d]\n",
831 ctx->name, frame->index);
832
833 /* pop timestamp and mark frame with it */
834 delta_pop_dts(ctx, &frame->dts);
835
836 /* release decoded frame to user */
837 delta_frame_done(ctx, frame, 0);
838 }
839 }
840
841 /* try to complete EOS */
842 ret = delta_get_free_frame(ctx, &frame);
843 if (ret)
844 goto delay_eos;
845
846 /* new frame available, EOS can now be completed */
847 delta_complete_eos(ctx, frame);
848
849 ctx->state = DELTA_STATE_EOS;
850
851 return 0;
852
853 delay_eos:
854 /*
855 * EOS completion from driver is delayed because
856 * we don't have a free empty frame available.
857 * EOS completion is so delayed till next frame_queue() call
858 * to be sure to have a free empty frame available.
859 */
860 ctx->state = DELTA_STATE_WF_EOS;
861 dev_dbg(delta->dev, "%s EOS delayed\n", ctx->name);
862
863 return 0;
864 }
865
delta_decoder_cmd(struct file * file,void * fh,struct v4l2_decoder_cmd * cmd)866 static int delta_decoder_cmd(struct file *file, void *fh,
867 struct v4l2_decoder_cmd *cmd)
868 {
869 struct delta_ctx *ctx = to_ctx(fh);
870 int ret = 0;
871
872 ret = delta_try_decoder_cmd(file, fh, cmd);
873 if (ret)
874 return ret;
875
876 return delta_decoder_stop_cmd(ctx, fh);
877 }
878
delta_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)879 static int delta_subscribe_event(struct v4l2_fh *fh,
880 const struct v4l2_event_subscription *sub)
881 {
882 switch (sub->type) {
883 case V4L2_EVENT_EOS:
884 return v4l2_event_subscribe(fh, sub, 2, NULL);
885 default:
886 return -EINVAL;
887 }
888
889 return 0;
890 }
891
892 /* v4l2 ioctl ops */
893 static const struct v4l2_ioctl_ops delta_ioctl_ops = {
894 .vidioc_querycap = delta_querycap,
895 .vidioc_enum_fmt_vid_cap = delta_enum_fmt_frame,
896 .vidioc_g_fmt_vid_cap = delta_g_fmt_frame,
897 .vidioc_try_fmt_vid_cap = delta_try_fmt_frame,
898 .vidioc_s_fmt_vid_cap = delta_s_fmt_frame,
899 .vidioc_enum_fmt_vid_out = delta_enum_fmt_stream,
900 .vidioc_g_fmt_vid_out = delta_g_fmt_stream,
901 .vidioc_try_fmt_vid_out = delta_try_fmt_stream,
902 .vidioc_s_fmt_vid_out = delta_s_fmt_stream,
903 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
904 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
905 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
906 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
907 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
908 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
909 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
910 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
911 .vidioc_g_selection = delta_g_selection,
912 .vidioc_try_decoder_cmd = delta_try_decoder_cmd,
913 .vidioc_decoder_cmd = delta_decoder_cmd,
914 .vidioc_subscribe_event = delta_subscribe_event,
915 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
916 };
917
918 /*
919 * mem-to-mem operations
920 */
921
delta_run_work(struct work_struct * work)922 static void delta_run_work(struct work_struct *work)
923 {
924 struct delta_ctx *ctx = container_of(work, struct delta_ctx, run_work);
925 struct delta_dev *delta = ctx->dev;
926 const struct delta_dec *dec = ctx->dec;
927 struct delta_au *au;
928 struct delta_frame *frame = NULL;
929 int ret = 0;
930 bool discard = false;
931 struct vb2_v4l2_buffer *vbuf;
932
933 if (!dec) {
934 dev_err(delta->dev, "%s no decoder opened yet\n", ctx->name);
935 return;
936 }
937
938 /* protect instance against reentrancy */
939 mutex_lock(&ctx->lock);
940
941 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
942 if (!vbuf) {
943 dev_err(delta->dev, "%s no buffer to decode\n", ctx->name);
944 mutex_unlock(&ctx->lock);
945 return;
946 }
947 au = to_au(vbuf);
948 au->size = vb2_get_plane_payload(&vbuf->vb2_buf, 0);
949 au->dts = vbuf->vb2_buf.timestamp;
950
951 /* dump access unit */
952 dump_au(ctx, au);
953
954 /* enable the hardware */
955 if (!dec->pm) {
956 ret = delta_get_sync(ctx);
957 if (ret)
958 goto err;
959 }
960
961 /* decode this access unit */
962 ret = call_dec_op(dec, decode, ctx, au);
963
964 /*
965 * if the (-ENODATA) value is returned, it refers to the interlaced
966 * stream case for which 2 access units are needed to get 1 frame.
967 * So, this returned value doesn't mean that the decoding fails, but
968 * indicates that the timestamp information of the access unit shall
969 * not be taken into account, and that the V4L2 buffer associated with
970 * the access unit shall be flagged with V4L2_BUF_FLAG_ERROR to inform
971 * the user of this situation
972 */
973 if (ret == -ENODATA) {
974 discard = true;
975 } else if (ret) {
976 dev_err(delta->dev, "%s decoding failed (%d)\n",
977 ctx->name, ret);
978
979 /* disable the hardware */
980 if (!dec->pm)
981 delta_put_autosuspend(ctx);
982
983 goto err;
984 }
985
986 /* disable the hardware */
987 if (!dec->pm)
988 delta_put_autosuspend(ctx);
989
990 /* push au timestamp in FIFO */
991 if (!discard)
992 delta_push_dts(ctx, au->dts);
993
994 /* get available decoded frames */
995 while (1) {
996 ret = call_dec_op(dec, get_frame, ctx, &frame);
997 if (ret == -ENODATA) {
998 /* no more decoded frames */
999 goto out;
1000 }
1001 if (ret) {
1002 dev_err(delta->dev, "%s cannot get decoded frame (%d)\n",
1003 ctx->name, ret);
1004 goto out;
1005 }
1006 if (!frame) {
1007 dev_err(delta->dev,
1008 "%s NULL decoded frame\n",
1009 ctx->name);
1010 ret = -EIO;
1011 goto out;
1012 }
1013
1014 /* pop timestamp and mark frame with it */
1015 delta_pop_dts(ctx, &frame->dts);
1016
1017 /* release decoded frame to user */
1018 delta_frame_done(ctx, frame, 0);
1019 }
1020
1021 out:
1022 requeue_free_frames(ctx);
1023 delta_au_done(ctx, au, (discard ? -ENODATA : 0));
1024 mutex_unlock(&ctx->lock);
1025 v4l2_m2m_job_finish(delta->m2m_dev, ctx->fh.m2m_ctx);
1026 return;
1027
1028 err:
1029 requeue_free_frames(ctx);
1030 delta_au_done(ctx, au, ret);
1031 mutex_unlock(&ctx->lock);
1032 v4l2_m2m_job_finish(delta->m2m_dev, ctx->fh.m2m_ctx);
1033 }
1034
delta_device_run(void * priv)1035 static void delta_device_run(void *priv)
1036 {
1037 struct delta_ctx *ctx = priv;
1038 struct delta_dev *delta = ctx->dev;
1039
1040 queue_work(delta->work_queue, &ctx->run_work);
1041 }
1042
delta_job_abort(void * priv)1043 static void delta_job_abort(void *priv)
1044 {
1045 struct delta_ctx *ctx = priv;
1046 struct delta_dev *delta = ctx->dev;
1047
1048 dev_dbg(delta->dev, "%s aborting job\n", ctx->name);
1049
1050 ctx->aborting = true;
1051 }
1052
delta_job_ready(void * priv)1053 static int delta_job_ready(void *priv)
1054 {
1055 struct delta_ctx *ctx = priv;
1056 struct delta_dev *delta = ctx->dev;
1057 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1058
1059 if (!src_bufs) {
1060 dev_dbg(delta->dev, "%s not ready: not enough video buffers.\n",
1061 ctx->name);
1062 return 0;
1063 }
1064
1065 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1066 dev_dbg(delta->dev, "%s not ready: not enough video capture buffers.\n",
1067 ctx->name);
1068 return 0;
1069 }
1070
1071 if (ctx->aborting) {
1072 dev_dbg(delta->dev, "%s job not ready: aborting\n", ctx->name);
1073 return 0;
1074 }
1075
1076 dev_dbg(delta->dev, "%s job ready\n", ctx->name);
1077
1078 return 1;
1079 }
1080
1081 /* mem-to-mem ops */
1082 static const struct v4l2_m2m_ops delta_m2m_ops = {
1083 .device_run = delta_device_run,
1084 .job_ready = delta_job_ready,
1085 .job_abort = delta_job_abort,
1086 };
1087
1088 /*
1089 * VB2 queue operations
1090 */
1091
delta_vb2_au_queue_setup(struct vb2_queue * vq,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])1092 static int delta_vb2_au_queue_setup(struct vb2_queue *vq,
1093 unsigned int *num_buffers,
1094 unsigned int *num_planes,
1095 unsigned int sizes[],
1096 struct device *alloc_devs[])
1097 {
1098 struct delta_ctx *ctx = vb2_get_drv_priv(vq);
1099 unsigned int size = ctx->max_au_size;
1100
1101 if (*num_planes)
1102 return sizes[0] < size ? -EINVAL : 0;
1103
1104 *num_planes = 1;
1105 if (*num_buffers < 1)
1106 *num_buffers = 1;
1107 if (*num_buffers > DELTA_MAX_AUS)
1108 *num_buffers = DELTA_MAX_AUS;
1109
1110 sizes[0] = size;
1111
1112 return 0;
1113 }
1114
delta_vb2_au_prepare(struct vb2_buffer * vb)1115 static int delta_vb2_au_prepare(struct vb2_buffer *vb)
1116 {
1117 struct vb2_queue *q = vb->vb2_queue;
1118 struct delta_ctx *ctx = vb2_get_drv_priv(q);
1119 struct delta_dev *delta = ctx->dev;
1120 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1121 struct delta_au *au = to_au(vbuf);
1122
1123 if (!au->prepared) {
1124 /* get memory addresses */
1125 au->vaddr = vb2_plane_vaddr(&au->vbuf.vb2_buf, 0);
1126 au->paddr = vb2_dma_contig_plane_dma_addr
1127 (&au->vbuf.vb2_buf, 0);
1128 au->prepared = true;
1129 dev_dbg(delta->dev, "%s au[%d] prepared; virt=0x%p, phy=0x%pad\n",
1130 ctx->name, vb->index, au->vaddr, &au->paddr);
1131 }
1132
1133 if (vbuf->field == V4L2_FIELD_ANY)
1134 vbuf->field = V4L2_FIELD_NONE;
1135
1136 return 0;
1137 }
1138
delta_setup_frame(struct delta_ctx * ctx,struct delta_frame * frame)1139 static int delta_setup_frame(struct delta_ctx *ctx,
1140 struct delta_frame *frame)
1141 {
1142 struct delta_dev *delta = ctx->dev;
1143 const struct delta_dec *dec = ctx->dec;
1144
1145 if (frame->index >= DELTA_MAX_FRAMES) {
1146 dev_err(delta->dev,
1147 "%s frame index=%d exceeds output frame count (%d)\n",
1148 ctx->name, frame->index, DELTA_MAX_FRAMES);
1149 return -EINVAL;
1150 }
1151
1152 if (ctx->nb_of_frames >= DELTA_MAX_FRAMES) {
1153 dev_err(delta->dev,
1154 "%s number of frames exceeds output frame count (%d > %d)\n",
1155 ctx->name, ctx->nb_of_frames, DELTA_MAX_FRAMES);
1156 return -EINVAL;
1157 }
1158
1159 if (frame->index != ctx->nb_of_frames) {
1160 dev_warn(delta->dev,
1161 "%s frame index discontinuity detected, expected %d, got %d\n",
1162 ctx->name, ctx->nb_of_frames, frame->index);
1163 }
1164
1165 frame->state = DELTA_FRAME_FREE;
1166 ctx->frames[ctx->nb_of_frames] = frame;
1167 ctx->nb_of_frames++;
1168
1169 /* setup frame on decoder side */
1170 return call_dec_op(dec, setup_frame, ctx, frame);
1171 }
1172
1173 /*
1174 * default implementation of get_frameinfo decoder ops
1175 * matching frame information from stream information
1176 * & with default pixel format & default alignment.
1177 */
delta_get_frameinfo_default(struct delta_ctx * ctx,struct delta_frameinfo * frameinfo)1178 int delta_get_frameinfo_default(struct delta_ctx *ctx,
1179 struct delta_frameinfo *frameinfo)
1180 {
1181 struct delta_streaminfo *streaminfo = &ctx->streaminfo;
1182
1183 memset(frameinfo, 0, sizeof(*frameinfo));
1184 frameinfo->pixelformat = V4L2_PIX_FMT_NV12;
1185 frameinfo->width = streaminfo->width;
1186 frameinfo->height = streaminfo->height;
1187 frameinfo->aligned_width = ALIGN(streaminfo->width,
1188 DELTA_WIDTH_ALIGNMENT);
1189 frameinfo->aligned_height = ALIGN(streaminfo->height,
1190 DELTA_HEIGHT_ALIGNMENT);
1191 frameinfo->size = frame_size(frameinfo->aligned_width,
1192 frameinfo->aligned_height,
1193 frameinfo->pixelformat);
1194 if (streaminfo->flags & DELTA_STREAMINFO_FLAG_CROP) {
1195 frameinfo->flags |= DELTA_FRAMEINFO_FLAG_CROP;
1196 frameinfo->crop = streaminfo->crop;
1197 }
1198 if (streaminfo->flags & DELTA_STREAMINFO_FLAG_PIXELASPECT) {
1199 frameinfo->flags |= DELTA_FRAMEINFO_FLAG_PIXELASPECT;
1200 frameinfo->pixelaspect = streaminfo->pixelaspect;
1201 }
1202 frameinfo->field = streaminfo->field;
1203
1204 return 0;
1205 }
1206
1207 /*
1208 * default implementation of recycle decoder ops
1209 * consisting to relax the "decoded" frame state
1210 */
delta_recycle_default(struct delta_ctx * pctx,struct delta_frame * frame)1211 int delta_recycle_default(struct delta_ctx *pctx,
1212 struct delta_frame *frame)
1213 {
1214 frame->state &= ~DELTA_FRAME_DEC;
1215
1216 return 0;
1217 }
1218
dump_frames_status(struct delta_ctx * ctx)1219 static void dump_frames_status(struct delta_ctx *ctx)
1220 {
1221 struct delta_dev *delta = ctx->dev;
1222 unsigned int i;
1223 struct delta_frame *frame;
1224 unsigned char str[100] = "";
1225
1226 dev_info(delta->dev,
1227 "%s dumping frames status...\n", ctx->name);
1228
1229 for (i = 0; i < ctx->nb_of_frames; i++) {
1230 frame = ctx->frames[i];
1231 dev_info(delta->dev,
1232 "%s frame[%d] %s\n",
1233 ctx->name, frame->index,
1234 frame_state_str(frame->state,
1235 str, sizeof(str)));
1236 }
1237 }
1238
delta_get_free_frame(struct delta_ctx * ctx,struct delta_frame ** pframe)1239 int delta_get_free_frame(struct delta_ctx *ctx,
1240 struct delta_frame **pframe)
1241 {
1242 struct delta_dev *delta = ctx->dev;
1243 struct vb2_v4l2_buffer *vbuf;
1244 struct delta_frame *frame;
1245
1246 *pframe = NULL;
1247
1248 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1249 if (!vbuf) {
1250 dev_err(delta->dev, "%s no frame available",
1251 ctx->name);
1252 return -EIO;
1253 }
1254
1255 frame = to_frame(vbuf);
1256 frame->state &= ~DELTA_FRAME_M2M;
1257 if (frame->state != DELTA_FRAME_FREE) {
1258 dev_err(delta->dev,
1259 "%s frame[%d] is not free\n",
1260 ctx->name, frame->index);
1261 dump_frames_status(ctx);
1262 return -ENODATA;
1263 }
1264
1265 dev_dbg(delta->dev,
1266 "%s get free frame[%d]\n", ctx->name, frame->index);
1267
1268 *pframe = frame;
1269 return 0;
1270 }
1271
delta_get_sync(struct delta_ctx * ctx)1272 int delta_get_sync(struct delta_ctx *ctx)
1273 {
1274 struct delta_dev *delta = ctx->dev;
1275 int ret = 0;
1276
1277 /* enable the hardware */
1278 ret = pm_runtime_get_sync(delta->dev);
1279 if (ret < 0) {
1280 dev_err(delta->dev, "%s pm_runtime_get_sync failed (%d)\n",
1281 __func__, ret);
1282 return ret;
1283 }
1284
1285 return 0;
1286 }
1287
delta_put_autosuspend(struct delta_ctx * ctx)1288 void delta_put_autosuspend(struct delta_ctx *ctx)
1289 {
1290 struct delta_dev *delta = ctx->dev;
1291
1292 pm_runtime_put_autosuspend(delta->dev);
1293 }
1294
delta_vb2_au_queue(struct vb2_buffer * vb)1295 static void delta_vb2_au_queue(struct vb2_buffer *vb)
1296 {
1297 struct vb2_queue *q = vb->vb2_queue;
1298 struct delta_ctx *ctx = vb2_get_drv_priv(q);
1299 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1300
1301 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1302 }
1303
delta_vb2_au_start_streaming(struct vb2_queue * q,unsigned int count)1304 static int delta_vb2_au_start_streaming(struct vb2_queue *q,
1305 unsigned int count)
1306 {
1307 struct delta_ctx *ctx = vb2_get_drv_priv(q);
1308 struct delta_dev *delta = ctx->dev;
1309 const struct delta_dec *dec = ctx->dec;
1310 struct delta_au *au;
1311 int ret = 0;
1312 struct vb2_v4l2_buffer *vbuf = NULL;
1313 struct delta_streaminfo *streaminfo = &ctx->streaminfo;
1314 struct delta_frameinfo *frameinfo = &ctx->frameinfo;
1315 unsigned char str1[100] = "";
1316 unsigned char str2[100] = "";
1317
1318 if ((ctx->state != DELTA_STATE_WF_FORMAT) &&
1319 (ctx->state != DELTA_STATE_WF_STREAMINFO))
1320 return 0;
1321
1322 if (ctx->state == DELTA_STATE_WF_FORMAT) {
1323 /* open decoder if not yet done */
1324 ret = delta_open_decoder(ctx,
1325 ctx->streaminfo.streamformat,
1326 ctx->frameinfo.pixelformat, &dec);
1327 if (ret)
1328 goto err;
1329 ctx->dec = dec;
1330 ctx->state = DELTA_STATE_WF_STREAMINFO;
1331 }
1332
1333 /*
1334 * first buffer should contain stream header,
1335 * decode it to get the infos related to stream
1336 * such as width, height, dpb, ...
1337 */
1338 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1339 if (!vbuf) {
1340 dev_err(delta->dev, "%s failed to start streaming, no stream header buffer enqueued\n",
1341 ctx->name);
1342 ret = -EINVAL;
1343 goto err;
1344 }
1345 au = to_au(vbuf);
1346 au->size = vb2_get_plane_payload(&vbuf->vb2_buf, 0);
1347 au->dts = vbuf->vb2_buf.timestamp;
1348
1349 delta_push_dts(ctx, au->dts);
1350
1351 /* dump access unit */
1352 dump_au(ctx, au);
1353
1354 /* decode this access unit */
1355 ret = call_dec_op(dec, decode, ctx, au);
1356 if (ret) {
1357 dev_err(delta->dev, "%s failed to start streaming, header decoding failed (%d)\n",
1358 ctx->name, ret);
1359 goto err;
1360 }
1361
1362 ret = call_dec_op(dec, get_streaminfo, ctx, streaminfo);
1363 if (ret) {
1364 dev_dbg_ratelimited(delta->dev,
1365 "%s failed to start streaming, valid stream header not yet decoded\n",
1366 ctx->name);
1367 goto err;
1368 }
1369 ctx->flags |= DELTA_FLAG_STREAMINFO;
1370
1371 ret = call_dec_op(dec, get_frameinfo, ctx, frameinfo);
1372 if (ret)
1373 goto err;
1374 ctx->flags |= DELTA_FLAG_FRAMEINFO;
1375
1376 ctx->state = DELTA_STATE_READY;
1377
1378 dev_dbg(delta->dev, "%s %s => %s\n", ctx->name,
1379 delta_streaminfo_str(streaminfo, str1, sizeof(str1)),
1380 delta_frameinfo_str(frameinfo, str2, sizeof(str2)));
1381
1382 delta_au_done(ctx, au, ret);
1383 return 0;
1384
1385 err:
1386 /*
1387 * return all buffers to vb2 in QUEUED state.
1388 * This will give ownership back to userspace
1389 */
1390 if (vbuf)
1391 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
1392
1393 while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1394 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
1395 return ret;
1396 }
1397
delta_vb2_au_stop_streaming(struct vb2_queue * q)1398 static void delta_vb2_au_stop_streaming(struct vb2_queue *q)
1399 {
1400 struct delta_ctx *ctx = vb2_get_drv_priv(q);
1401 struct vb2_v4l2_buffer *vbuf;
1402
1403 delta_flush_dts(ctx);
1404
1405 /* return all buffers to vb2 in ERROR state */
1406 while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1407 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1408
1409 ctx->au_num = 0;
1410
1411 ctx->aborting = false;
1412 }
1413
delta_vb2_frame_queue_setup(struct vb2_queue * vq,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])1414 static int delta_vb2_frame_queue_setup(struct vb2_queue *vq,
1415 unsigned int *num_buffers,
1416 unsigned int *num_planes,
1417 unsigned int sizes[],
1418 struct device *alloc_devs[])
1419 {
1420 struct delta_ctx *ctx = vb2_get_drv_priv(vq);
1421 struct delta_dev *delta = ctx->dev;
1422 struct delta_streaminfo *streaminfo = &ctx->streaminfo;
1423 struct delta_frameinfo *frameinfo = &ctx->frameinfo;
1424 unsigned int size = frameinfo->size;
1425
1426 /*
1427 * the number of output buffers needed for decoding =
1428 * user need (*num_buffers given, usually for display pipeline) +
1429 * stream need (streaminfo->dpb) +
1430 * decoding peak smoothing (depends on DELTA IP perf)
1431 */
1432 if (*num_buffers < DELTA_MIN_FRAME_USER) {
1433 dev_dbg(delta->dev,
1434 "%s num_buffers too low (%d), increasing to %d\n",
1435 ctx->name, *num_buffers, DELTA_MIN_FRAME_USER);
1436 *num_buffers = DELTA_MIN_FRAME_USER;
1437 }
1438
1439 *num_buffers += streaminfo->dpb + DELTA_PEAK_FRAME_SMOOTHING;
1440
1441 if (*num_buffers > DELTA_MAX_FRAMES) {
1442 dev_dbg(delta->dev,
1443 "%s output frame count too high (%d), cut to %d\n",
1444 ctx->name, *num_buffers, DELTA_MAX_FRAMES);
1445 *num_buffers = DELTA_MAX_FRAMES;
1446 }
1447
1448 if (*num_planes)
1449 return sizes[0] < size ? -EINVAL : 0;
1450
1451 /* single plane for Y and CbCr */
1452 *num_planes = 1;
1453
1454 sizes[0] = size;
1455
1456 ctx->nb_of_frames = 0;
1457
1458 return 0;
1459 }
1460
delta_vb2_frame_prepare(struct vb2_buffer * vb)1461 static int delta_vb2_frame_prepare(struct vb2_buffer *vb)
1462 {
1463 struct vb2_queue *q = vb->vb2_queue;
1464 struct delta_ctx *ctx = vb2_get_drv_priv(q);
1465 struct delta_dev *delta = ctx->dev;
1466 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1467 struct delta_frame *frame = to_frame(vbuf);
1468 int ret = 0;
1469
1470 if (!frame->prepared) {
1471 frame->index = vbuf->vb2_buf.index;
1472 frame->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
1473 frame->paddr = vb2_dma_contig_plane_dma_addr(&vbuf->vb2_buf, 0);
1474 frame->info = ctx->frameinfo;
1475
1476 ret = delta_setup_frame(ctx, frame);
1477 if (ret) {
1478 dev_err(delta->dev,
1479 "%s setup_frame() failed (%d)\n",
1480 ctx->name, ret);
1481 return ret;
1482 }
1483 frame->prepared = true;
1484 dev_dbg(delta->dev,
1485 "%s frame[%d] prepared; virt=0x%p, phy=0x%pad\n",
1486 ctx->name, vb->index, frame->vaddr,
1487 &frame->paddr);
1488 }
1489
1490 frame->flags = vbuf->flags;
1491
1492 return 0;
1493 }
1494
delta_vb2_frame_finish(struct vb2_buffer * vb)1495 static void delta_vb2_frame_finish(struct vb2_buffer *vb)
1496 {
1497 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1498 struct delta_frame *frame = to_frame(vbuf);
1499
1500 /* update V4L2 fields for user */
1501 vb2_set_plane_payload(&vbuf->vb2_buf, 0, frame->info.size);
1502 vb->timestamp = frame->dts;
1503 vbuf->field = frame->field;
1504 vbuf->flags = frame->flags;
1505 }
1506
delta_vb2_frame_queue(struct vb2_buffer * vb)1507 static void delta_vb2_frame_queue(struct vb2_buffer *vb)
1508 {
1509 struct vb2_queue *q = vb->vb2_queue;
1510 struct delta_ctx *ctx = vb2_get_drv_priv(q);
1511 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1512 struct delta_frame *frame = to_frame(vbuf);
1513
1514 if (ctx->state == DELTA_STATE_WF_EOS) {
1515 /* new frame available, EOS can now be completed */
1516 delta_complete_eos(ctx, frame);
1517
1518 ctx->state = DELTA_STATE_EOS;
1519
1520 /* return, no need to recycle this buffer to decoder */
1521 return;
1522 }
1523
1524 /* recycle this frame */
1525 delta_recycle(ctx, frame);
1526 }
1527
delta_vb2_frame_stop_streaming(struct vb2_queue * q)1528 static void delta_vb2_frame_stop_streaming(struct vb2_queue *q)
1529 {
1530 struct delta_ctx *ctx = vb2_get_drv_priv(q);
1531 struct vb2_v4l2_buffer *vbuf;
1532 struct delta_frame *frame;
1533 const struct delta_dec *dec = ctx->dec;
1534 unsigned int i;
1535
1536 delta_flush_dts(ctx);
1537
1538 call_dec_op(dec, flush, ctx);
1539
1540 /*
1541 * return all buffers to vb2 in ERROR state
1542 * & reset each frame state to OUT
1543 */
1544 for (i = 0; i < ctx->nb_of_frames; i++) {
1545 frame = ctx->frames[i];
1546 if (!(frame->state & DELTA_FRAME_OUT)) {
1547 vbuf = &frame->vbuf;
1548 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1549 }
1550 frame->state = DELTA_FRAME_OUT;
1551 }
1552
1553 ctx->frame_num = 0;
1554
1555 ctx->aborting = false;
1556 }
1557
1558 /* VB2 queue ops */
1559 static const struct vb2_ops delta_vb2_au_ops = {
1560 .queue_setup = delta_vb2_au_queue_setup,
1561 .buf_prepare = delta_vb2_au_prepare,
1562 .buf_queue = delta_vb2_au_queue,
1563 .wait_prepare = vb2_ops_wait_prepare,
1564 .wait_finish = vb2_ops_wait_finish,
1565 .start_streaming = delta_vb2_au_start_streaming,
1566 .stop_streaming = delta_vb2_au_stop_streaming,
1567 };
1568
1569 static const struct vb2_ops delta_vb2_frame_ops = {
1570 .queue_setup = delta_vb2_frame_queue_setup,
1571 .buf_prepare = delta_vb2_frame_prepare,
1572 .buf_finish = delta_vb2_frame_finish,
1573 .buf_queue = delta_vb2_frame_queue,
1574 .wait_prepare = vb2_ops_wait_prepare,
1575 .wait_finish = vb2_ops_wait_finish,
1576 .stop_streaming = delta_vb2_frame_stop_streaming,
1577 };
1578
1579 /*
1580 * V4L2 file operations
1581 */
1582
queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)1583 static int queue_init(void *priv,
1584 struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
1585 {
1586 struct vb2_queue *q;
1587 struct delta_ctx *ctx = priv;
1588 struct delta_dev *delta = ctx->dev;
1589 int ret;
1590
1591 /* setup vb2 queue for stream input */
1592 q = src_vq;
1593 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1594 q->io_modes = VB2_MMAP | VB2_DMABUF;
1595 q->drv_priv = ctx;
1596 /* overload vb2 buf with private au struct */
1597 q->buf_struct_size = sizeof(struct delta_au);
1598 q->ops = &delta_vb2_au_ops;
1599 q->mem_ops = &vb2_dma_contig_memops;
1600 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1601 q->lock = &delta->lock;
1602 q->dev = delta->dev;
1603
1604 ret = vb2_queue_init(q);
1605 if (ret)
1606 return ret;
1607
1608 /* setup vb2 queue for frame output */
1609 q = dst_vq;
1610 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1611 q->io_modes = VB2_MMAP | VB2_DMABUF;
1612 q->drv_priv = ctx;
1613 /* overload vb2 buf with private frame struct */
1614 q->buf_struct_size = sizeof(struct delta_frame)
1615 + DELTA_MAX_FRAME_PRIV_SIZE;
1616 q->ops = &delta_vb2_frame_ops;
1617 q->mem_ops = &vb2_dma_contig_memops;
1618 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1619 q->lock = &delta->lock;
1620 q->dev = delta->dev;
1621
1622 return vb2_queue_init(q);
1623 }
1624
delta_open(struct file * file)1625 static int delta_open(struct file *file)
1626 {
1627 struct delta_dev *delta = video_drvdata(file);
1628 struct delta_ctx *ctx = NULL;
1629 int ret = 0;
1630
1631 mutex_lock(&delta->lock);
1632
1633 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1634 if (!ctx) {
1635 ret = -ENOMEM;
1636 goto err;
1637 }
1638 ctx->dev = delta;
1639
1640 v4l2_fh_init(&ctx->fh, video_devdata(file));
1641 file->private_data = &ctx->fh;
1642 v4l2_fh_add(&ctx->fh);
1643
1644 INIT_WORK(&ctx->run_work, delta_run_work);
1645 mutex_init(&ctx->lock);
1646
1647 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(delta->m2m_dev, ctx,
1648 queue_init);
1649 if (IS_ERR(ctx->fh.m2m_ctx)) {
1650 ret = PTR_ERR(ctx->fh.m2m_ctx);
1651 dev_err(delta->dev, "%s failed to initialize m2m context (%d)\n",
1652 DELTA_PREFIX, ret);
1653 goto err_fh_del;
1654 }
1655
1656 /*
1657 * wait stream format to determine which
1658 * decoder to open
1659 */
1660 ctx->state = DELTA_STATE_WF_FORMAT;
1661
1662 INIT_LIST_HEAD(&ctx->dts);
1663
1664 /* set the instance name */
1665 delta->instance_id++;
1666 snprintf(ctx->name, sizeof(ctx->name), "[%3d:----]",
1667 delta->instance_id);
1668
1669 /* default parameters for frame and stream */
1670 set_default_params(ctx);
1671
1672 /* enable ST231 clocks */
1673 if (delta->clk_st231)
1674 if (clk_prepare_enable(delta->clk_st231))
1675 dev_warn(delta->dev, "failed to enable st231 clk\n");
1676
1677 /* enable FLASH_PROMIP clock */
1678 if (delta->clk_flash_promip)
1679 if (clk_prepare_enable(delta->clk_flash_promip))
1680 dev_warn(delta->dev, "failed to enable delta promip clk\n");
1681
1682 mutex_unlock(&delta->lock);
1683
1684 dev_dbg(delta->dev, "%s decoder instance created\n", ctx->name);
1685
1686 return 0;
1687
1688 err_fh_del:
1689 v4l2_fh_del(&ctx->fh);
1690 v4l2_fh_exit(&ctx->fh);
1691 kfree(ctx);
1692 err:
1693 mutex_unlock(&delta->lock);
1694
1695 return ret;
1696 }
1697
delta_release(struct file * file)1698 static int delta_release(struct file *file)
1699 {
1700 struct delta_ctx *ctx = to_ctx(file->private_data);
1701 struct delta_dev *delta = ctx->dev;
1702 const struct delta_dec *dec = ctx->dec;
1703
1704 mutex_lock(&delta->lock);
1705
1706 /* close decoder */
1707 call_dec_op(dec, close, ctx);
1708
1709 /*
1710 * trace a summary of instance
1711 * before closing (debug purpose)
1712 */
1713 delta_trace_summary(ctx);
1714
1715 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1716
1717 v4l2_fh_del(&ctx->fh);
1718 v4l2_fh_exit(&ctx->fh);
1719
1720 /* disable ST231 clocks */
1721 if (delta->clk_st231)
1722 clk_disable_unprepare(delta->clk_st231);
1723
1724 /* disable FLASH_PROMIP clock */
1725 if (delta->clk_flash_promip)
1726 clk_disable_unprepare(delta->clk_flash_promip);
1727
1728 dev_dbg(delta->dev, "%s decoder instance released\n", ctx->name);
1729
1730 kfree(ctx);
1731
1732 mutex_unlock(&delta->lock);
1733 return 0;
1734 }
1735
1736 /* V4L2 file ops */
1737 static const struct v4l2_file_operations delta_fops = {
1738 .owner = THIS_MODULE,
1739 .open = delta_open,
1740 .release = delta_release,
1741 .unlocked_ioctl = video_ioctl2,
1742 .mmap = v4l2_m2m_fop_mmap,
1743 .poll = v4l2_m2m_fop_poll,
1744 };
1745
1746 /*
1747 * Platform device operations
1748 */
1749
delta_register_device(struct delta_dev * delta)1750 static int delta_register_device(struct delta_dev *delta)
1751 {
1752 int ret;
1753 struct video_device *vdev;
1754
1755 if (!delta)
1756 return -ENODEV;
1757
1758 delta->m2m_dev = v4l2_m2m_init(&delta_m2m_ops);
1759 if (IS_ERR(delta->m2m_dev)) {
1760 dev_err(delta->dev, "%s failed to initialize v4l2-m2m device\n",
1761 DELTA_PREFIX);
1762 ret = PTR_ERR(delta->m2m_dev);
1763 goto err;
1764 }
1765
1766 vdev = video_device_alloc();
1767 if (!vdev) {
1768 dev_err(delta->dev, "%s failed to allocate video device\n",
1769 DELTA_PREFIX);
1770 ret = -ENOMEM;
1771 goto err_m2m_release;
1772 }
1773
1774 vdev->fops = &delta_fops;
1775 vdev->ioctl_ops = &delta_ioctl_ops;
1776 vdev->release = video_device_release;
1777 vdev->lock = &delta->lock;
1778 vdev->vfl_dir = VFL_DIR_M2M;
1779 vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M;
1780 vdev->v4l2_dev = &delta->v4l2_dev;
1781 snprintf(vdev->name, sizeof(vdev->name), "%s-%s",
1782 DELTA_NAME, DELTA_FW_VERSION);
1783
1784 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1785 if (ret) {
1786 dev_err(delta->dev, "%s failed to register video device\n",
1787 DELTA_PREFIX);
1788 goto err_vdev_release;
1789 }
1790
1791 delta->vdev = vdev;
1792 video_set_drvdata(vdev, delta);
1793 return 0;
1794
1795 err_vdev_release:
1796 video_device_release(vdev);
1797 err_m2m_release:
1798 v4l2_m2m_release(delta->m2m_dev);
1799 err:
1800 return ret;
1801 }
1802
delta_unregister_device(struct delta_dev * delta)1803 static void delta_unregister_device(struct delta_dev *delta)
1804 {
1805 if (!delta)
1806 return;
1807
1808 if (delta->m2m_dev)
1809 v4l2_m2m_release(delta->m2m_dev);
1810
1811 video_unregister_device(delta->vdev);
1812 }
1813
delta_probe(struct platform_device * pdev)1814 static int delta_probe(struct platform_device *pdev)
1815 {
1816 struct delta_dev *delta;
1817 struct device *dev = &pdev->dev;
1818 int ret;
1819
1820 delta = devm_kzalloc(dev, sizeof(*delta), GFP_KERNEL);
1821 if (!delta) {
1822 ret = -ENOMEM;
1823 goto err;
1824 }
1825
1826 delta->dev = dev;
1827 delta->pdev = pdev;
1828 platform_set_drvdata(pdev, delta);
1829
1830 mutex_init(&delta->lock);
1831
1832 /* get clock resources */
1833 delta->clk_delta = devm_clk_get(dev, "delta");
1834 if (IS_ERR(delta->clk_delta)) {
1835 dev_dbg(dev, "%s can't get delta clock\n", DELTA_PREFIX);
1836 delta->clk_delta = NULL;
1837 }
1838
1839 delta->clk_st231 = devm_clk_get(dev, "delta-st231");
1840 if (IS_ERR(delta->clk_st231)) {
1841 dev_dbg(dev, "%s can't get delta-st231 clock\n", DELTA_PREFIX);
1842 delta->clk_st231 = NULL;
1843 }
1844
1845 delta->clk_flash_promip = devm_clk_get(dev, "delta-flash-promip");
1846 if (IS_ERR(delta->clk_flash_promip)) {
1847 dev_dbg(dev, "%s can't get delta-flash-promip clock\n",
1848 DELTA_PREFIX);
1849 delta->clk_flash_promip = NULL;
1850 }
1851
1852 /* init pm_runtime used for power management */
1853 pm_runtime_set_autosuspend_delay(dev, DELTA_HW_AUTOSUSPEND_DELAY_MS);
1854 pm_runtime_use_autosuspend(dev);
1855 pm_runtime_set_suspended(dev);
1856 pm_runtime_enable(dev);
1857
1858 /* init firmware ipc channel */
1859 ret = delta_ipc_init(delta);
1860 if (ret) {
1861 dev_err(delta->dev, "%s failed to initialize firmware ipc channel\n",
1862 DELTA_PREFIX);
1863 goto err;
1864 }
1865
1866 /* register all available decoders */
1867 register_decoders(delta);
1868
1869 /* register all supported formats */
1870 register_formats(delta);
1871
1872 /* register on V4L2 */
1873 ret = v4l2_device_register(dev, &delta->v4l2_dev);
1874 if (ret) {
1875 dev_err(delta->dev, "%s failed to register V4L2 device\n",
1876 DELTA_PREFIX);
1877 goto err;
1878 }
1879
1880 delta->work_queue = create_workqueue(DELTA_NAME);
1881 if (!delta->work_queue) {
1882 dev_err(delta->dev, "%s failed to allocate work queue\n",
1883 DELTA_PREFIX);
1884 ret = -ENOMEM;
1885 goto err_v4l2;
1886 }
1887
1888 /* register device */
1889 ret = delta_register_device(delta);
1890 if (ret)
1891 goto err_work_queue;
1892
1893 dev_info(dev, "%s %s registered as /dev/video%d\n",
1894 DELTA_PREFIX, delta->vdev->name, delta->vdev->num);
1895
1896 return 0;
1897
1898 err_work_queue:
1899 destroy_workqueue(delta->work_queue);
1900 err_v4l2:
1901 v4l2_device_unregister(&delta->v4l2_dev);
1902 err:
1903 return ret;
1904 }
1905
delta_remove(struct platform_device * pdev)1906 static int delta_remove(struct platform_device *pdev)
1907 {
1908 struct delta_dev *delta = platform_get_drvdata(pdev);
1909
1910 delta_ipc_exit(delta);
1911
1912 delta_unregister_device(delta);
1913
1914 destroy_workqueue(delta->work_queue);
1915
1916 pm_runtime_put_autosuspend(delta->dev);
1917 pm_runtime_disable(delta->dev);
1918
1919 v4l2_device_unregister(&delta->v4l2_dev);
1920
1921 return 0;
1922 }
1923
delta_runtime_suspend(struct device * dev)1924 static int delta_runtime_suspend(struct device *dev)
1925 {
1926 struct delta_dev *delta = dev_get_drvdata(dev);
1927
1928 if (delta->clk_delta)
1929 clk_disable_unprepare(delta->clk_delta);
1930
1931 return 0;
1932 }
1933
delta_runtime_resume(struct device * dev)1934 static int delta_runtime_resume(struct device *dev)
1935 {
1936 struct delta_dev *delta = dev_get_drvdata(dev);
1937
1938 if (delta->clk_delta)
1939 if (clk_prepare_enable(delta->clk_delta))
1940 dev_warn(dev, "failed to prepare/enable delta clk\n");
1941
1942 return 0;
1943 }
1944
1945 /* PM ops */
1946 static const struct dev_pm_ops delta_pm_ops = {
1947 .runtime_suspend = delta_runtime_suspend,
1948 .runtime_resume = delta_runtime_resume,
1949 };
1950
1951 static const struct of_device_id delta_match_types[] = {
1952 {
1953 .compatible = "st,st-delta",
1954 },
1955 {
1956 /* end node */
1957 }
1958 };
1959
1960 MODULE_DEVICE_TABLE(of, delta_match_types);
1961
1962 static struct platform_driver delta_driver = {
1963 .probe = delta_probe,
1964 .remove = delta_remove,
1965 .driver = {
1966 .name = DELTA_NAME,
1967 .of_match_table = delta_match_types,
1968 .pm = &delta_pm_ops},
1969 };
1970
1971 module_platform_driver(delta_driver);
1972
1973 MODULE_LICENSE("GPL");
1974 MODULE_AUTHOR("Hugues Fruchet <hugues.fruchet@st.com>");
1975 MODULE_DESCRIPTION("STMicroelectronics DELTA video decoder V4L2 driver");
1976