1 // SPDX-License-Identifier: GPL-2.0
3 * Broadcom BM2835 V4L2 driver
5 * Copyright © 2013 Raspberry Pi (Trading) Ltd.
7 * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
8 * Dave Stevenson <dsteve@broadcom.com>
9 * Simon Mellor <simellor@broadcom.com>
10 * Luke Diamand <luked@broadcom.com>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <media/videobuf2-vmalloc.h>
18 #include <media/videobuf2-dma-contig.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-ioctl.h>
21 #include <media/v4l2-ctrls.h>
22 #include <media/v4l2-fh.h>
23 #include <media/v4l2-event.h>
24 #include <media/v4l2-common.h>
25 #include <linux/delay.h>
26 #include <linux/platform_device.h>
28 #include "mmal-common.h"
29 #include "mmal-encodings.h"
30 #include "mmal-vchiq.h"
32 #include "mmal-parameters.h"
33 #include "bcm2835-camera.h"
35 #define BM2835_MMAL_VERSION "0.0.2"
36 #define BM2835_MMAL_MODULE_NAME "bcm2835-v4l2"
39 #define MIN_BUFFER_SIZE (80 * 1024)
41 #define MAX_VIDEO_MODE_WIDTH 1280
42 #define MAX_VIDEO_MODE_HEIGHT 720
44 #define MAX_BCM2835_CAMERAS 2
46 int bcm2835_v4l2_debug;
47 module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
48 MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
51 static int video_nr[] = {[0 ... (MAX_BCM2835_CAMERAS - 1)] = UNSET };
52 module_param_array(video_nr, int, NULL, 0644);
53 MODULE_PARM_DESC(video_nr, "videoX start numbers, -1 is autodetect");
55 static int max_video_width = MAX_VIDEO_MODE_WIDTH;
56 static int max_video_height = MAX_VIDEO_MODE_HEIGHT;
57 module_param(max_video_width, int, 0644);
58 MODULE_PARM_DESC(max_video_width, "Threshold for video mode");
59 module_param(max_video_height, int, 0644);
60 MODULE_PARM_DESC(max_video_height, "Threshold for video mode");
62 /* global device data array */
63 static struct bm2835_mmal_dev *gdev[MAX_BCM2835_CAMERAS];
68 /* timeperframe: min/max and default */
69 static const struct v4l2_fract
70 tpf_min = {.numerator = 1, .denominator = FPS_MAX},
71 tpf_max = {.numerator = 1, .denominator = FPS_MIN},
72 tpf_default = {.numerator = 1000, .denominator = 30000};
75 static struct mmal_fmt formats[] = {
77 .name = "4:2:0, planar, YUV",
78 .fourcc = V4L2_PIX_FMT_YUV420,
80 .mmal = MMAL_ENCODING_I420,
82 .mmal_component = MMAL_COMPONENT_CAMERA,
86 .name = "4:2:2, packed, YUYV",
87 .fourcc = V4L2_PIX_FMT_YUYV,
89 .mmal = MMAL_ENCODING_YUYV,
91 .mmal_component = MMAL_COMPONENT_CAMERA,
96 .fourcc = V4L2_PIX_FMT_RGB24,
98 .mmal = MMAL_ENCODING_RGB24,
100 .mmal_component = MMAL_COMPONENT_CAMERA,
105 .fourcc = V4L2_PIX_FMT_JPEG,
106 .flags = V4L2_FMT_FLAG_COMPRESSED,
107 .mmal = MMAL_ENCODING_JPEG,
109 .mmal_component = MMAL_COMPONENT_IMAGE_ENCODE,
114 .fourcc = V4L2_PIX_FMT_H264,
115 .flags = V4L2_FMT_FLAG_COMPRESSED,
116 .mmal = MMAL_ENCODING_H264,
118 .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
123 .fourcc = V4L2_PIX_FMT_MJPEG,
124 .flags = V4L2_FMT_FLAG_COMPRESSED,
125 .mmal = MMAL_ENCODING_MJPEG,
127 .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
131 .name = "4:2:2, packed, YVYU",
132 .fourcc = V4L2_PIX_FMT_YVYU,
134 .mmal = MMAL_ENCODING_YVYU,
136 .mmal_component = MMAL_COMPONENT_CAMERA,
140 .name = "4:2:2, packed, VYUY",
141 .fourcc = V4L2_PIX_FMT_VYUY,
143 .mmal = MMAL_ENCODING_VYUY,
145 .mmal_component = MMAL_COMPONENT_CAMERA,
149 .name = "4:2:2, packed, UYVY",
150 .fourcc = V4L2_PIX_FMT_UYVY,
152 .mmal = MMAL_ENCODING_UYVY,
154 .mmal_component = MMAL_COMPONENT_CAMERA,
158 .name = "4:2:0, planar, NV12",
159 .fourcc = V4L2_PIX_FMT_NV12,
161 .mmal = MMAL_ENCODING_NV12,
163 .mmal_component = MMAL_COMPONENT_CAMERA,
167 .name = "RGB24 (BE)",
168 .fourcc = V4L2_PIX_FMT_BGR24,
170 .mmal = MMAL_ENCODING_BGR24,
172 .mmal_component = MMAL_COMPONENT_CAMERA,
176 .name = "4:2:0, planar, YVU",
177 .fourcc = V4L2_PIX_FMT_YVU420,
179 .mmal = MMAL_ENCODING_YV12,
181 .mmal_component = MMAL_COMPONENT_CAMERA,
185 .name = "4:2:0, planar, NV21",
186 .fourcc = V4L2_PIX_FMT_NV21,
188 .mmal = MMAL_ENCODING_NV21,
190 .mmal_component = MMAL_COMPONENT_CAMERA,
194 .name = "RGB32 (BE)",
195 .fourcc = V4L2_PIX_FMT_BGR32,
197 .mmal = MMAL_ENCODING_BGRA,
199 .mmal_component = MMAL_COMPONENT_CAMERA,
205 static struct mmal_fmt *get_format(struct v4l2_format *f)
207 struct mmal_fmt *fmt;
210 for (k = 0; k < ARRAY_SIZE(formats); k++) {
212 if (fmt->fourcc == f->fmt.pix.pixelformat)
219 /* ------------------------------------------------------------------
220 * Videobuf queue operations
221 * ------------------------------------------------------------------
224 static int queue_setup(struct vb2_queue *vq,
225 unsigned int *nbuffers, unsigned int *nplanes,
226 unsigned int sizes[], struct device *alloc_ctxs[])
228 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
231 /* refuse queue setup if port is not configured */
232 if (!dev->capture.port) {
233 v4l2_err(&dev->v4l2_dev,
234 "%s: capture port not configured\n", __func__);
238 size = dev->capture.port->current_buffer.size;
240 v4l2_err(&dev->v4l2_dev,
241 "%s: capture port buffer size is zero\n", __func__);
245 if (*nbuffers < dev->capture.port->minimum_buffer.num)
246 *nbuffers = dev->capture.port->minimum_buffer.num;
248 dev->capture.port->current_buffer.num = *nbuffers;
255 * videobuf2-vmalloc allocator is context-less so no need to set
259 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
265 static int buffer_init(struct vb2_buffer *vb)
267 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
268 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
269 struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
271 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
273 buf->buffer = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
274 buf->buffer_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
276 return mmal_vchi_buffer_init(dev->instance, buf);
279 static int buffer_prepare(struct vb2_buffer *vb)
281 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
284 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
287 if (!dev->capture.port || !dev->capture.fmt)
290 size = dev->capture.stride * dev->capture.height;
291 if (vb2_plane_size(vb, 0) < size) {
292 v4l2_err(&dev->v4l2_dev,
293 "%s data will not fit into plane (%lu < %lu)\n",
294 __func__, vb2_plane_size(vb, 0), size);
301 static void buffer_cleanup(struct vb2_buffer *vb)
303 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
304 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
305 struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
307 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
309 mmal_vchi_buffer_cleanup(buf);
312 static inline bool is_capturing(struct bm2835_mmal_dev *dev)
314 return dev->capture.camera_port ==
316 component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
319 static void buffer_cb(struct vchiq_mmal_instance *instance,
320 struct vchiq_mmal_port *port,
322 struct mmal_buffer *buf,
323 unsigned long length, u32 mmal_flags, s64 dts, s64 pts)
325 struct bm2835_mmal_dev *dev = port->cb_ctx;
327 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
328 "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
329 __func__, status, buf, length, mmal_flags, pts);
332 /* error in transfer */
334 /* there was a buffer with the error so return it */
335 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
338 } else if (length == 0) {
341 /* this should only ever happen if the port is
342 * disabled and there are buffers still queued
344 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
345 pr_debug("Empty buffer");
346 } else if (dev->capture.frame_count) {
347 /* grab another frame */
348 if (is_capturing(dev)) {
349 pr_debug("Grab another frame");
350 vchiq_mmal_port_parameter_set(
352 dev->capture.camera_port,
353 MMAL_PARAMETER_CAPTURE,
354 &dev->capture.frame_count,
355 sizeof(dev->capture.frame_count));
358 /* signal frame completion */
359 complete(&dev->capture.frame_cmplt);
362 if (dev->capture.frame_count) {
363 if (dev->capture.vc_start_timestamp != -1 &&
366 s64 runtime_us = pts -
367 dev->capture.vc_start_timestamp;
368 timestamp = ktime_add_us(dev->capture.kernel_start_ts,
370 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
371 "Convert start time %llu and %llu with offset %llu to %llu\n",
372 ktime_to_ns(dev->capture.kernel_start_ts),
373 dev->capture.vc_start_timestamp, pts,
374 ktime_to_ns(timestamp));
375 buf->vb.vb2_buf.timestamp = ktime_to_ns(timestamp);
377 buf->vb.vb2_buf.timestamp = ktime_get_ns();
380 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, length);
381 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
383 if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
385 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
386 "Grab another frame as buffer has EOS");
387 vchiq_mmal_port_parameter_set(
389 dev->capture.camera_port,
390 MMAL_PARAMETER_CAPTURE,
391 &dev->capture.frame_count,
392 sizeof(dev->capture.frame_count));
395 /* signal frame completion */
396 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
397 complete(&dev->capture.frame_cmplt);
402 static int enable_camera(struct bm2835_mmal_dev *dev)
406 if (!dev->camera_use_count) {
407 ret = vchiq_mmal_port_parameter_set(
409 &dev->component[MMAL_COMPONENT_CAMERA]->control,
410 MMAL_PARAMETER_CAMERA_NUM, &dev->camera_num,
411 sizeof(dev->camera_num));
413 v4l2_err(&dev->v4l2_dev,
414 "Failed setting camera num, ret %d\n", ret);
418 ret = vchiq_mmal_component_enable(
420 dev->component[MMAL_COMPONENT_CAMERA]);
422 v4l2_err(&dev->v4l2_dev,
423 "Failed enabling camera, ret %d\n", ret);
427 dev->camera_use_count++;
428 v4l2_dbg(1, bcm2835_v4l2_debug,
429 &dev->v4l2_dev, "enabled camera (refcount %d)\n",
430 dev->camera_use_count);
434 static int disable_camera(struct bm2835_mmal_dev *dev)
438 if (!dev->camera_use_count) {
439 v4l2_err(&dev->v4l2_dev,
440 "Disabled the camera when already disabled\n");
443 dev->camera_use_count--;
444 if (!dev->camera_use_count) {
445 unsigned int i = 0xFFFFFFFF;
447 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
448 "Disabling camera\n");
450 vchiq_mmal_component_disable(
452 dev->component[MMAL_COMPONENT_CAMERA]);
454 v4l2_err(&dev->v4l2_dev,
455 "Failed disabling camera, ret %d\n", ret);
458 vchiq_mmal_port_parameter_set(
460 &dev->component[MMAL_COMPONENT_CAMERA]->control,
461 MMAL_PARAMETER_CAMERA_NUM, &i,
464 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
465 "Camera refcount now %d\n", dev->camera_use_count);
469 static void buffer_queue(struct vb2_buffer *vb)
471 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
472 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
473 struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
476 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
477 "%s: dev:%p buf:%p, idx %u\n",
478 __func__, dev, buf, vb2->vb2_buf.index);
480 ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
482 v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
486 static int start_streaming(struct vb2_queue *vq, unsigned int count)
488 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
492 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
495 /* ensure a format has actually been set */
496 if (!dev->capture.port)
499 if (enable_camera(dev) < 0) {
500 v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
504 /*init_completion(&dev->capture.frame_cmplt); */
506 /* enable frame capture */
507 dev->capture.frame_count = 1;
509 /* if the preview is not already running, wait for a few frames for AGC
512 if (!dev->component[MMAL_COMPONENT_PREVIEW]->enabled)
515 /* enable the connection from camera to encoder (if applicable) */
516 if (dev->capture.camera_port != dev->capture.port
517 && dev->capture.camera_port) {
518 ret = vchiq_mmal_port_enable(dev->instance,
519 dev->capture.camera_port, NULL);
521 v4l2_err(&dev->v4l2_dev,
522 "Failed to enable encode tunnel - error %d\n",
528 /* Get VC timestamp at this point in time */
529 parameter_size = sizeof(dev->capture.vc_start_timestamp);
530 if (vchiq_mmal_port_parameter_get(dev->instance,
531 dev->capture.camera_port,
532 MMAL_PARAMETER_SYSTEM_TIME,
533 &dev->capture.vc_start_timestamp,
535 v4l2_err(&dev->v4l2_dev,
536 "Failed to get VC start time - update your VC f/w\n");
538 /* Flag to indicate just to rely on kernel timestamps */
539 dev->capture.vc_start_timestamp = -1;
541 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
542 "Start time %lld size %d\n",
543 dev->capture.vc_start_timestamp, parameter_size);
545 dev->capture.kernel_start_ts = ktime_get();
547 /* enable the camera port */
548 dev->capture.port->cb_ctx = dev;
550 vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
552 v4l2_err(&dev->v4l2_dev,
553 "Failed to enable capture port - error %d. Disabling camera port again\n",
556 vchiq_mmal_port_disable(dev->instance,
557 dev->capture.camera_port);
558 if (disable_camera(dev) < 0) {
559 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
565 /* capture the first frame */
566 vchiq_mmal_port_parameter_set(dev->instance,
567 dev->capture.camera_port,
568 MMAL_PARAMETER_CAPTURE,
569 &dev->capture.frame_count,
570 sizeof(dev->capture.frame_count));
574 /* abort streaming and wait for last buffer */
575 static void stop_streaming(struct vb2_queue *vq)
578 unsigned long timeout;
579 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
581 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
584 init_completion(&dev->capture.frame_cmplt);
585 dev->capture.frame_count = 0;
587 /* ensure a format has actually been set */
588 if (!dev->capture.port) {
589 v4l2_err(&dev->v4l2_dev,
590 "no capture port - stream not started?\n");
594 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
596 /* stop capturing frames */
597 vchiq_mmal_port_parameter_set(dev->instance,
598 dev->capture.camera_port,
599 MMAL_PARAMETER_CAPTURE,
600 &dev->capture.frame_count,
601 sizeof(dev->capture.frame_count));
603 /* wait for last frame to complete */
604 timeout = wait_for_completion_timeout(&dev->capture.frame_cmplt, HZ);
606 v4l2_err(&dev->v4l2_dev,
607 "timed out waiting for frame completion\n");
609 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
610 "disabling connection\n");
612 /* disable the connection from camera to encoder */
613 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
614 if (!ret && dev->capture.camera_port != dev->capture.port) {
615 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
617 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
618 } else if (dev->capture.camera_port != dev->capture.port) {
619 v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
623 if (disable_camera(dev) < 0)
624 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
627 static const struct vb2_ops bm2835_mmal_video_qops = {
628 .queue_setup = queue_setup,
629 .buf_init = buffer_init,
630 .buf_prepare = buffer_prepare,
631 .buf_cleanup = buffer_cleanup,
632 .buf_queue = buffer_queue,
633 .start_streaming = start_streaming,
634 .stop_streaming = stop_streaming,
635 .wait_prepare = vb2_ops_wait_prepare,
636 .wait_finish = vb2_ops_wait_finish,
639 /* ------------------------------------------------------------------
641 * ------------------------------------------------------------------
644 static int set_overlay_params(struct bm2835_mmal_dev *dev,
645 struct vchiq_mmal_port *port)
647 struct mmal_parameter_displayregion prev_config = {
648 .set = MMAL_DISPLAY_SET_LAYER |
649 MMAL_DISPLAY_SET_ALPHA |
650 MMAL_DISPLAY_SET_DEST_RECT |
651 MMAL_DISPLAY_SET_FULLSCREEN,
652 .layer = PREVIEW_LAYER,
653 .alpha = dev->overlay.global_alpha,
656 .x = dev->overlay.w.left,
657 .y = dev->overlay.w.top,
658 .width = dev->overlay.w.width,
659 .height = dev->overlay.w.height,
662 return vchiq_mmal_port_parameter_set(dev->instance, port,
663 MMAL_PARAMETER_DISPLAYREGION,
664 &prev_config, sizeof(prev_config));
668 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
669 struct v4l2_fmtdesc *f)
671 struct mmal_fmt *fmt;
673 if (f->index >= ARRAY_SIZE(formats))
676 fmt = &formats[f->index];
678 strlcpy((char *)f->description, fmt->name, sizeof(f->description));
679 f->pixelformat = fmt->fourcc;
680 f->flags = fmt->flags;
685 static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
686 struct v4l2_format *f)
688 struct bm2835_mmal_dev *dev = video_drvdata(file);
690 f->fmt.win = dev->overlay;
695 static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
696 struct v4l2_format *f)
698 struct bm2835_mmal_dev *dev = video_drvdata(file);
700 f->fmt.win.field = V4L2_FIELD_NONE;
701 f->fmt.win.chromakey = 0;
702 f->fmt.win.clips = NULL;
703 f->fmt.win.clipcount = 0;
704 f->fmt.win.bitmap = NULL;
706 v4l_bound_align_image(&f->fmt.win.w.width, MIN_WIDTH, dev->max_width, 1,
707 &f->fmt.win.w.height, MIN_HEIGHT, dev->max_height,
709 v4l_bound_align_image(&f->fmt.win.w.left, MIN_WIDTH, dev->max_width, 1,
710 &f->fmt.win.w.top, MIN_HEIGHT, dev->max_height,
713 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
714 "Overlay: Now w/h %dx%d l/t %dx%d\n",
715 f->fmt.win.w.width, f->fmt.win.w.height,
716 f->fmt.win.w.left, f->fmt.win.w.top);
718 v4l2_dump_win_format(1,
726 static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
727 struct v4l2_format *f)
729 struct bm2835_mmal_dev *dev = video_drvdata(file);
731 vidioc_try_fmt_vid_overlay(file, priv, f);
733 dev->overlay = f->fmt.win;
734 if (dev->component[MMAL_COMPONENT_PREVIEW]->enabled) {
735 set_overlay_params(dev,
736 &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
742 static int vidioc_overlay(struct file *file, void *f, unsigned int on)
745 struct bm2835_mmal_dev *dev = video_drvdata(file);
746 struct vchiq_mmal_port *src;
747 struct vchiq_mmal_port *dst;
749 if ((on && dev->component[MMAL_COMPONENT_PREVIEW]->enabled) ||
750 (!on && !dev->component[MMAL_COMPONENT_PREVIEW]->enabled))
751 return 0; /* already in requested state */
754 &dev->component[MMAL_COMPONENT_CAMERA]->
755 output[MMAL_CAMERA_PORT_PREVIEW];
758 /* disconnect preview ports and disable component */
759 ret = vchiq_mmal_port_disable(dev->instance, src);
762 vchiq_mmal_port_connect_tunnel(dev->instance, src,
765 ret = vchiq_mmal_component_disable(
767 dev->component[MMAL_COMPONENT_PREVIEW]);
773 /* set preview port format and connect it to output */
774 dst = &dev->component[MMAL_COMPONENT_PREVIEW]->input[0];
776 ret = vchiq_mmal_port_set_format(dev->instance, src);
780 ret = set_overlay_params(dev, dst);
784 if (enable_camera(dev) < 0)
787 ret = vchiq_mmal_component_enable(
789 dev->component[MMAL_COMPONENT_PREVIEW]);
793 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
795 ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
797 ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
802 static int vidioc_g_fbuf(struct file *file, void *fh,
803 struct v4l2_framebuffer *a)
805 /* The video overlay must stay within the framebuffer and can't be
806 * positioned independently.
808 struct bm2835_mmal_dev *dev = video_drvdata(file);
809 struct vchiq_mmal_port *preview_port =
810 &dev->component[MMAL_COMPONENT_CAMERA]->
811 output[MMAL_CAMERA_PORT_PREVIEW];
813 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
814 V4L2_FBUF_CAP_GLOBAL_ALPHA;
815 a->flags = V4L2_FBUF_FLAG_OVERLAY;
816 a->fmt.width = preview_port->es.video.width;
817 a->fmt.height = preview_port->es.video.height;
818 a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
819 a->fmt.bytesperline = preview_port->es.video.width;
820 a->fmt.sizeimage = (preview_port->es.video.width *
821 preview_port->es.video.height * 3) >> 1;
822 a->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
828 static int vidioc_enum_input(struct file *file, void *priv,
829 struct v4l2_input *inp)
831 /* only a single camera input */
835 inp->type = V4L2_INPUT_TYPE_CAMERA;
836 sprintf((char *)inp->name, "Camera %u", inp->index);
840 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
846 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
855 static int vidioc_querycap(struct file *file, void *priv,
856 struct v4l2_capability *cap)
858 struct bm2835_mmal_dev *dev = video_drvdata(file);
862 vchiq_mmal_version(dev->instance, &major, &minor);
864 strcpy((char *)cap->driver, "bm2835 mmal");
865 snprintf((char *)cap->card, sizeof(cap->card), "mmal service %d.%d",
868 snprintf((char *)cap->bus_info, sizeof(cap->bus_info),
869 "platform:%s", dev->v4l2_dev.name);
870 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
871 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
872 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
877 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
878 struct v4l2_fmtdesc *f)
880 struct mmal_fmt *fmt;
882 if (f->index >= ARRAY_SIZE(formats))
885 fmt = &formats[f->index];
887 strlcpy((char *)f->description, fmt->name, sizeof(f->description));
888 f->pixelformat = fmt->fourcc;
889 f->flags = fmt->flags;
894 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
895 struct v4l2_format *f)
897 struct bm2835_mmal_dev *dev = video_drvdata(file);
899 f->fmt.pix.width = dev->capture.width;
900 f->fmt.pix.height = dev->capture.height;
901 f->fmt.pix.field = V4L2_FIELD_NONE;
902 f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
903 f->fmt.pix.bytesperline = dev->capture.stride;
904 f->fmt.pix.sizeimage = dev->capture.buffersize;
906 if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
907 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
908 else if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG)
909 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
911 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
914 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
919 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
920 struct v4l2_format *f)
922 struct bm2835_mmal_dev *dev = video_drvdata(file);
923 struct mmal_fmt *mfmt;
925 mfmt = get_format(f);
927 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
928 "Fourcc format (0x%08x) unknown.\n",
929 f->fmt.pix.pixelformat);
930 f->fmt.pix.pixelformat = formats[0].fourcc;
931 mfmt = get_format(f);
934 f->fmt.pix.field = V4L2_FIELD_NONE;
936 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
937 "Clipping/aligning %dx%d format %08X\n",
938 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
940 v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, dev->max_width, 1,
941 &f->fmt.pix.height, MIN_HEIGHT, dev->max_height,
943 f->fmt.pix.bytesperline = f->fmt.pix.width * mfmt->ybbp;
944 if (!mfmt->remove_padding) {
945 int align_mask = ((32 * mfmt->depth) >> 3) - 1;
946 /* GPU isn't removing padding, so stride is aligned to 32 */
947 f->fmt.pix.bytesperline =
948 (f->fmt.pix.bytesperline + align_mask) & ~align_mask;
949 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
950 "Not removing padding, so bytes/line = %d, "
952 f->fmt.pix.bytesperline, align_mask);
955 /* Image buffer has to be padded to allow for alignment, even though
956 * we sometimes then remove that padding before delivering the buffer.
958 f->fmt.pix.sizeimage = ((f->fmt.pix.height + 15) & ~15) *
959 (((f->fmt.pix.width + 31) & ~31) * mfmt->depth) >> 3;
961 if ((mfmt->flags & V4L2_FMT_FLAG_COMPRESSED) &&
962 f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
963 f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
965 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24)
966 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
967 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
968 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
970 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
973 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
974 "Now %dx%d format %08X\n",
975 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
977 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
982 static int mmal_setup_components(struct bm2835_mmal_dev *dev,
983 struct v4l2_format *f)
986 struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
987 struct vchiq_mmal_component *encode_component = NULL;
988 struct mmal_fmt *mfmt = get_format(f);
994 if (dev->capture.encode_component) {
995 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
996 "vid_cap - disconnect previous tunnel\n");
998 /* Disconnect any previous connection */
999 vchiq_mmal_port_connect_tunnel(dev->instance,
1000 dev->capture.camera_port, NULL);
1001 dev->capture.camera_port = NULL;
1002 ret = vchiq_mmal_component_disable(dev->instance,
1006 v4l2_err(&dev->v4l2_dev,
1007 "Failed to disable encode component %d\n",
1010 dev->capture.encode_component = NULL;
1012 /* format dependent port setup */
1013 switch (mfmt->mmal_component) {
1014 case MMAL_COMPONENT_CAMERA:
1015 /* Make a further decision on port based on resolution */
1016 if (f->fmt.pix.width <= max_video_width
1017 && f->fmt.pix.height <= max_video_height)
1018 camera_port = port =
1019 &dev->component[MMAL_COMPONENT_CAMERA]->
1020 output[MMAL_CAMERA_PORT_VIDEO];
1022 camera_port = port =
1023 &dev->component[MMAL_COMPONENT_CAMERA]->
1024 output[MMAL_CAMERA_PORT_CAPTURE];
1026 case MMAL_COMPONENT_IMAGE_ENCODE:
1027 encode_component = dev->component[MMAL_COMPONENT_IMAGE_ENCODE];
1028 port = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
1030 &dev->component[MMAL_COMPONENT_CAMERA]->
1031 output[MMAL_CAMERA_PORT_CAPTURE];
1033 case MMAL_COMPONENT_VIDEO_ENCODE:
1034 encode_component = dev->component[MMAL_COMPONENT_VIDEO_ENCODE];
1035 port = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
1037 &dev->component[MMAL_COMPONENT_CAMERA]->
1038 output[MMAL_CAMERA_PORT_VIDEO];
1047 if (encode_component)
1048 camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
1050 camera_port->format.encoding = mfmt->mmal;
1052 if (dev->rgb_bgr_swapped) {
1053 if (camera_port->format.encoding == MMAL_ENCODING_RGB24)
1054 camera_port->format.encoding = MMAL_ENCODING_BGR24;
1055 else if (camera_port->format.encoding == MMAL_ENCODING_BGR24)
1056 camera_port->format.encoding = MMAL_ENCODING_RGB24;
1059 remove_padding = mfmt->remove_padding;
1060 vchiq_mmal_port_parameter_set(dev->instance,
1062 MMAL_PARAMETER_NO_IMAGE_PADDING,
1063 &remove_padding, sizeof(remove_padding));
1065 camera_port->format.encoding_variant = 0;
1066 camera_port->es.video.width = f->fmt.pix.width;
1067 camera_port->es.video.height = f->fmt.pix.height;
1068 camera_port->es.video.crop.x = 0;
1069 camera_port->es.video.crop.y = 0;
1070 camera_port->es.video.crop.width = f->fmt.pix.width;
1071 camera_port->es.video.crop.height = f->fmt.pix.height;
1072 camera_port->es.video.frame_rate.num = 0;
1073 camera_port->es.video.frame_rate.den = 1;
1074 camera_port->es.video.color_space = MMAL_COLOR_SPACE_JPEG_JFIF;
1076 ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
1080 &dev->component[MMAL_COMPONENT_CAMERA]->
1081 output[MMAL_CAMERA_PORT_VIDEO]) {
1082 bool overlay_enabled =
1083 !!dev->component[MMAL_COMPONENT_PREVIEW]->enabled;
1084 struct vchiq_mmal_port *preview_port =
1085 &dev->component[MMAL_COMPONENT_CAMERA]->
1086 output[MMAL_CAMERA_PORT_PREVIEW];
1087 /* Preview and encode ports need to match on resolution */
1088 if (overlay_enabled) {
1089 /* Need to disable the overlay before we can update
1093 vchiq_mmal_port_disable(dev->instance,
1097 vchiq_mmal_port_connect_tunnel(
1102 preview_port->es.video.width = f->fmt.pix.width;
1103 preview_port->es.video.height = f->fmt.pix.height;
1104 preview_port->es.video.crop.x = 0;
1105 preview_port->es.video.crop.y = 0;
1106 preview_port->es.video.crop.width = f->fmt.pix.width;
1107 preview_port->es.video.crop.height = f->fmt.pix.height;
1108 preview_port->es.video.frame_rate.num =
1109 dev->capture.timeperframe.denominator;
1110 preview_port->es.video.frame_rate.den =
1111 dev->capture.timeperframe.numerator;
1112 ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
1113 if (overlay_enabled) {
1114 ret = vchiq_mmal_port_connect_tunnel(
1117 &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
1119 ret = vchiq_mmal_port_enable(dev->instance,
1126 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1127 "%s failed to set format %dx%d %08X\n", __func__,
1128 f->fmt.pix.width, f->fmt.pix.height,
1129 f->fmt.pix.pixelformat);
1130 /* ensure capture is not going to be tried */
1131 dev->capture.port = NULL;
1133 if (encode_component) {
1134 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1135 "vid_cap - set up encode comp\n");
1137 /* configure buffering */
1138 camera_port->current_buffer.size =
1139 camera_port->recommended_buffer.size;
1140 camera_port->current_buffer.num =
1141 camera_port->recommended_buffer.num;
1144 vchiq_mmal_port_connect_tunnel(
1147 &encode_component->input[0]);
1149 v4l2_dbg(1, bcm2835_v4l2_debug,
1151 "%s failed to create connection\n",
1153 /* ensure capture is not going to be tried */
1154 dev->capture.port = NULL;
1156 port->es.video.width = f->fmt.pix.width;
1157 port->es.video.height = f->fmt.pix.height;
1158 port->es.video.crop.x = 0;
1159 port->es.video.crop.y = 0;
1160 port->es.video.crop.width = f->fmt.pix.width;
1161 port->es.video.crop.height = f->fmt.pix.height;
1162 port->es.video.frame_rate.num =
1163 dev->capture.timeperframe.denominator;
1164 port->es.video.frame_rate.den =
1165 dev->capture.timeperframe.numerator;
1167 port->format.encoding = mfmt->mmal;
1168 port->format.encoding_variant = 0;
1169 /* Set any encoding specific parameters */
1170 switch (mfmt->mmal_component) {
1171 case MMAL_COMPONENT_VIDEO_ENCODE:
1172 port->format.bitrate =
1173 dev->capture.encode_bitrate;
1175 case MMAL_COMPONENT_IMAGE_ENCODE:
1176 /* Could set EXIF parameters here */
1181 ret = vchiq_mmal_port_set_format(dev->instance,
1184 v4l2_dbg(1, bcm2835_v4l2_debug,
1186 "%s failed to set format %dx%d fmt %08X\n",
1190 f->fmt.pix.pixelformat
1195 ret = vchiq_mmal_component_enable(
1199 v4l2_dbg(1, bcm2835_v4l2_debug,
1201 "%s Failed to enable encode components\n",
1206 /* configure buffering */
1207 port->current_buffer.num = 1;
1208 port->current_buffer.size =
1209 f->fmt.pix.sizeimage;
1210 if (port->format.encoding ==
1211 MMAL_ENCODING_JPEG) {
1212 v4l2_dbg(1, bcm2835_v4l2_debug,
1214 "JPG - buf size now %d was %d\n",
1215 f->fmt.pix.sizeimage,
1216 port->current_buffer.size);
1217 port->current_buffer.size =
1218 (f->fmt.pix.sizeimage <
1221 : f->fmt.pix.sizeimage;
1223 v4l2_dbg(1, bcm2835_v4l2_debug,
1225 "vid_cap - cur_buf.size set to %d\n",
1226 f->fmt.pix.sizeimage);
1227 port->current_buffer.alignment = 0;
1230 /* configure buffering */
1231 camera_port->current_buffer.num = 1;
1232 camera_port->current_buffer.size = f->fmt.pix.sizeimage;
1233 camera_port->current_buffer.alignment = 0;
1237 dev->capture.fmt = mfmt;
1238 dev->capture.stride = f->fmt.pix.bytesperline;
1239 dev->capture.width = camera_port->es.video.crop.width;
1240 dev->capture.height = camera_port->es.video.crop.height;
1241 dev->capture.buffersize = port->current_buffer.size;
1243 /* select port for capture */
1244 dev->capture.port = port;
1245 dev->capture.camera_port = camera_port;
1246 dev->capture.encode_component = encode_component;
1247 v4l2_dbg(1, bcm2835_v4l2_debug,
1249 "Set dev->capture.fmt %08X, %dx%d, stride %d, size %d",
1250 port->format.encoding,
1251 dev->capture.width, dev->capture.height,
1252 dev->capture.stride, dev->capture.buffersize);
1256 /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
1260 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1261 struct v4l2_format *f)
1264 struct bm2835_mmal_dev *dev = video_drvdata(file);
1265 struct mmal_fmt *mfmt;
1267 /* try the format to set valid parameters */
1268 ret = vidioc_try_fmt_vid_cap(file, priv, f);
1270 v4l2_err(&dev->v4l2_dev,
1271 "vid_cap - vidioc_try_fmt_vid_cap failed\n");
1275 /* if a capture is running refuse to set format */
1276 if (vb2_is_busy(&dev->capture.vb_vidq)) {
1277 v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
1281 /* If the format is unsupported v4l2 says we should switch to
1282 * a supported one and not return an error.
1284 mfmt = get_format(f);
1286 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1287 "Fourcc format (0x%08x) unknown.\n",
1288 f->fmt.pix.pixelformat);
1289 f->fmt.pix.pixelformat = formats[0].fourcc;
1290 mfmt = get_format(f);
1293 ret = mmal_setup_components(dev, f);
1295 v4l2_err(&dev->v4l2_dev,
1296 "%s: failed to setup mmal components: %d\n",
1304 static int vidioc_enum_framesizes(struct file *file, void *fh,
1305 struct v4l2_frmsizeenum *fsize)
1307 struct bm2835_mmal_dev *dev = video_drvdata(file);
1308 static const struct v4l2_frmsize_stepwise sizes = {
1316 for (i = 0; i < ARRAY_SIZE(formats); i++)
1317 if (formats[i].fourcc == fsize->pixel_format)
1319 if (i == ARRAY_SIZE(formats))
1321 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1322 fsize->stepwise = sizes;
1323 fsize->stepwise.max_width = dev->max_width;
1324 fsize->stepwise.max_height = dev->max_height;
1328 /* timeperframe is arbitrary and continuous */
1329 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1330 struct v4l2_frmivalenum *fival)
1332 struct bm2835_mmal_dev *dev = video_drvdata(file);
1338 for (i = 0; i < ARRAY_SIZE(formats); i++)
1339 if (formats[i].fourcc == fival->pixel_format)
1341 if (i == ARRAY_SIZE(formats))
1344 /* regarding width & height - we support any within range */
1345 if (fival->width < MIN_WIDTH || fival->width > dev->max_width ||
1346 fival->height < MIN_HEIGHT || fival->height > dev->max_height)
1349 fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1351 /* fill in stepwise (step=1.0 is required by V4L2 spec) */
1352 fival->stepwise.min = tpf_min;
1353 fival->stepwise.max = tpf_max;
1354 fival->stepwise.step = (struct v4l2_fract) {1, 1};
1359 static int vidioc_g_parm(struct file *file, void *priv,
1360 struct v4l2_streamparm *parm)
1362 struct bm2835_mmal_dev *dev = video_drvdata(file);
1364 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1367 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1368 parm->parm.capture.timeperframe = dev->capture.timeperframe;
1369 parm->parm.capture.readbuffers = 1;
1373 #define FRACT_CMP(a, OP, b) \
1374 ((u64)(a).numerator * (b).denominator OP \
1375 (u64)(b).numerator * (a).denominator)
1377 static int vidioc_s_parm(struct file *file, void *priv,
1378 struct v4l2_streamparm *parm)
1380 struct bm2835_mmal_dev *dev = video_drvdata(file);
1381 struct v4l2_fract tpf;
1383 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1386 tpf = parm->parm.capture.timeperframe;
1388 /* tpf: {*, 0} resets timing; clip to [min, max]*/
1389 tpf = tpf.denominator ? tpf : tpf_default;
1390 tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1391 tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1393 dev->capture.timeperframe = tpf;
1394 parm->parm.capture.timeperframe = tpf;
1395 parm->parm.capture.readbuffers = 1;
1396 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1398 set_framerate_params(dev);
1403 static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
1405 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1406 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1407 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1408 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1409 .vidioc_overlay = vidioc_overlay,
1410 .vidioc_g_fbuf = vidioc_g_fbuf,
1413 .vidioc_enum_input = vidioc_enum_input,
1414 .vidioc_g_input = vidioc_g_input,
1415 .vidioc_s_input = vidioc_s_input,
1418 .vidioc_querycap = vidioc_querycap,
1419 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1420 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1421 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1422 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1424 /* buffer management */
1425 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1426 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1427 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1428 .vidioc_querybuf = vb2_ioctl_querybuf,
1429 .vidioc_qbuf = vb2_ioctl_qbuf,
1430 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1431 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1432 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1433 .vidioc_g_parm = vidioc_g_parm,
1434 .vidioc_s_parm = vidioc_s_parm,
1435 .vidioc_streamon = vb2_ioctl_streamon,
1436 .vidioc_streamoff = vb2_ioctl_streamoff,
1438 .vidioc_log_status = v4l2_ctrl_log_status,
1439 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1440 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1443 /* ------------------------------------------------------------------
1444 * Driver init/finalise
1445 * ------------------------------------------------------------------
1448 static const struct v4l2_file_operations camera0_fops = {
1449 .owner = THIS_MODULE,
1450 .open = v4l2_fh_open,
1451 .release = vb2_fop_release,
1452 .read = vb2_fop_read,
1453 .poll = vb2_fop_poll,
1454 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1455 .mmap = vb2_fop_mmap,
1458 static const struct video_device vdev_template = {
1460 .fops = &camera0_fops,
1461 .ioctl_ops = &camera0_ioctl_ops,
1462 .release = video_device_release_empty,
1465 /* Returns the number of cameras, and also the max resolution supported
1468 static int get_num_cameras(struct vchiq_mmal_instance *instance,
1469 unsigned int resolutions[][2], int num_resolutions)
1472 struct vchiq_mmal_component *cam_info_component;
1473 struct mmal_parameter_camera_info_t cam_info = {0};
1474 u32 param_size = sizeof(cam_info);
1477 /* create a camera_info component */
1478 ret = vchiq_mmal_component_init(instance, "camera_info",
1479 &cam_info_component);
1481 /* Unusual failure - let's guess one camera. */
1484 if (vchiq_mmal_port_parameter_get(instance,
1485 &cam_info_component->control,
1486 MMAL_PARAMETER_CAMERA_INFO,
1489 pr_info("Failed to get camera info\n");
1492 i < min_t(unsigned int, cam_info.num_cameras, num_resolutions);
1494 resolutions[i][0] = cam_info.cameras[i].max_width;
1495 resolutions[i][1] = cam_info.cameras[i].max_height;
1498 vchiq_mmal_component_finalise(instance,
1499 cam_info_component);
1501 return cam_info.num_cameras;
1504 static int set_camera_parameters(struct vchiq_mmal_instance *instance,
1505 struct vchiq_mmal_component *camera,
1506 struct bm2835_mmal_dev *dev)
1509 struct mmal_parameter_camera_config cam_config = {
1510 .max_stills_w = dev->max_width,
1511 .max_stills_h = dev->max_height,
1513 .one_shot_stills = 1,
1514 .max_preview_video_w = (max_video_width > 1920) ?
1515 max_video_width : 1920,
1516 .max_preview_video_h = (max_video_height > 1088) ?
1517 max_video_height : 1088,
1518 .num_preview_video_frames = 3,
1519 .stills_capture_circular_buffer_height = 0,
1520 .fast_preview_resume = 0,
1521 .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
1524 ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
1525 MMAL_PARAMETER_CAMERA_CONFIG,
1526 &cam_config, sizeof(cam_config));
1530 #define MAX_SUPPORTED_ENCODINGS 20
1532 /* MMAL instance and component init */
1533 static int mmal_init(struct bm2835_mmal_dev *dev)
1536 struct mmal_es_format_local *format;
1537 u32 supported_encodings[MAX_SUPPORTED_ENCODINGS];
1539 struct vchiq_mmal_component *camera;
1541 ret = vchiq_mmal_init(&dev->instance);
1543 v4l2_err(&dev->v4l2_dev, "%s: vchiq mmal init failed %d\n",
1548 /* get the camera component ready */
1549 ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
1550 &dev->component[MMAL_COMPONENT_CAMERA]);
1554 camera = dev->component[MMAL_COMPONENT_CAMERA];
1555 if (camera->outputs < MMAL_CAMERA_PORT_COUNT) {
1556 v4l2_err(&dev->v4l2_dev, "%s: too few camera outputs %d needed %d\n",
1557 __func__, camera->outputs, MMAL_CAMERA_PORT_COUNT);
1562 ret = set_camera_parameters(dev->instance,
1566 v4l2_err(&dev->v4l2_dev, "%s: unable to set camera parameters: %d\n",
1571 /* There was an error in the firmware that meant the camera component
1572 * produced BGR instead of RGB.
1573 * This is now fixed, but in order to support the old firmwares, we
1576 dev->rgb_bgr_swapped = true;
1577 param_size = sizeof(supported_encodings);
1578 ret = vchiq_mmal_port_parameter_get(dev->instance,
1579 &camera->output[MMAL_CAMERA_PORT_CAPTURE],
1580 MMAL_PARAMETER_SUPPORTED_ENCODINGS,
1581 &supported_encodings,
1586 for (i = 0; i < param_size / sizeof(u32); i++) {
1587 if (supported_encodings[i] == MMAL_ENCODING_BGR24) {
1588 /* Found BGR24 first - old firmware. */
1591 if (supported_encodings[i] == MMAL_ENCODING_RGB24) {
1592 /* Found RGB24 first
1593 * new firmware, so use RGB24.
1595 dev->rgb_bgr_swapped = false;
1600 format = &camera->output[MMAL_CAMERA_PORT_PREVIEW].format;
1602 format->encoding = MMAL_ENCODING_OPAQUE;
1603 format->encoding_variant = MMAL_ENCODING_I420;
1605 format->es->video.width = 1024;
1606 format->es->video.height = 768;
1607 format->es->video.crop.x = 0;
1608 format->es->video.crop.y = 0;
1609 format->es->video.crop.width = 1024;
1610 format->es->video.crop.height = 768;
1611 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1612 format->es->video.frame_rate.den = 1;
1614 format = &camera->output[MMAL_CAMERA_PORT_VIDEO].format;
1616 format->encoding = MMAL_ENCODING_OPAQUE;
1617 format->encoding_variant = MMAL_ENCODING_I420;
1619 format->es->video.width = 1024;
1620 format->es->video.height = 768;
1621 format->es->video.crop.x = 0;
1622 format->es->video.crop.y = 0;
1623 format->es->video.crop.width = 1024;
1624 format->es->video.crop.height = 768;
1625 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1626 format->es->video.frame_rate.den = 1;
1628 format = &camera->output[MMAL_CAMERA_PORT_CAPTURE].format;
1630 format->encoding = MMAL_ENCODING_OPAQUE;
1632 format->es->video.width = 2592;
1633 format->es->video.height = 1944;
1634 format->es->video.crop.x = 0;
1635 format->es->video.crop.y = 0;
1636 format->es->video.crop.width = 2592;
1637 format->es->video.crop.height = 1944;
1638 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1639 format->es->video.frame_rate.den = 1;
1641 dev->capture.width = format->es->video.width;
1642 dev->capture.height = format->es->video.height;
1643 dev->capture.fmt = &formats[0];
1644 dev->capture.encode_component = NULL;
1645 dev->capture.timeperframe = tpf_default;
1646 dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
1647 dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
1649 /* get the preview component ready */
1650 ret = vchiq_mmal_component_init(
1651 dev->instance, "ril.video_render",
1652 &dev->component[MMAL_COMPONENT_PREVIEW]);
1656 if (dev->component[MMAL_COMPONENT_PREVIEW]->inputs < 1) {
1658 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1659 __func__, dev->component[MMAL_COMPONENT_PREVIEW]->inputs, 1);
1663 /* get the image encoder component ready */
1664 ret = vchiq_mmal_component_init(
1665 dev->instance, "ril.image_encode",
1666 &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1670 if (dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs < 1) {
1672 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1673 __func__, dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs,
1675 goto unreg_image_encoder;
1678 /* get the video encoder component ready */
1679 ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
1681 component[MMAL_COMPONENT_VIDEO_ENCODE]);
1683 goto unreg_image_encoder;
1685 if (dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs < 1) {
1687 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1688 __func__, dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs,
1690 goto unreg_vid_encoder;
1694 struct vchiq_mmal_port *encoder_port =
1695 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
1696 encoder_port->format.encoding = MMAL_ENCODING_H264;
1697 ret = vchiq_mmal_port_set_format(dev->instance,
1702 unsigned int enable = 1;
1704 vchiq_mmal_port_parameter_set(
1706 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
1707 MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
1708 &enable, sizeof(enable));
1710 vchiq_mmal_port_parameter_set(dev->instance,
1711 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
1712 MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
1716 ret = bm2835_mmal_set_all_camera_controls(dev);
1718 v4l2_err(&dev->v4l2_dev, "%s: failed to set all camera controls: %d\n",
1720 goto unreg_vid_encoder;
1726 pr_err("Cleanup: Destroy video encoder\n");
1727 vchiq_mmal_component_finalise(
1729 dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
1731 unreg_image_encoder:
1732 pr_err("Cleanup: Destroy image encoder\n");
1733 vchiq_mmal_component_finalise(
1735 dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1738 pr_err("Cleanup: Destroy video render\n");
1739 vchiq_mmal_component_finalise(dev->instance,
1740 dev->component[MMAL_COMPONENT_PREVIEW]);
1743 pr_err("Cleanup: Destroy camera\n");
1744 vchiq_mmal_component_finalise(dev->instance,
1745 dev->component[MMAL_COMPONENT_CAMERA]);
1748 vchiq_mmal_finalise(dev->instance);
1752 static int bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
1753 struct video_device *vfd)
1757 *vfd = vdev_template;
1759 vfd->v4l2_dev = &dev->v4l2_dev;
1761 vfd->lock = &dev->mutex;
1763 vfd->queue = &dev->capture.vb_vidq;
1765 /* video device needs to be able to access instance data */
1766 video_set_drvdata(vfd, dev);
1768 ret = video_register_device(vfd,
1770 video_nr[dev->camera_num]);
1774 v4l2_info(vfd->v4l2_dev,
1775 "V4L2 device registered as %s - stills mode > %dx%d\n",
1776 video_device_node_name(vfd),
1777 max_video_width, max_video_height);
1782 static void bcm2835_cleanup_instance(struct bm2835_mmal_dev *dev)
1787 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1788 video_device_node_name(&dev->vdev));
1790 video_unregister_device(&dev->vdev);
1792 if (dev->capture.encode_component) {
1793 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1794 "mmal_exit - disconnect tunnel\n");
1795 vchiq_mmal_port_connect_tunnel(dev->instance,
1796 dev->capture.camera_port, NULL);
1797 vchiq_mmal_component_disable(dev->instance,
1798 dev->capture.encode_component);
1800 vchiq_mmal_component_disable(dev->instance,
1801 dev->component[MMAL_COMPONENT_CAMERA]);
1803 vchiq_mmal_component_finalise(dev->instance,
1805 component[MMAL_COMPONENT_VIDEO_ENCODE]);
1807 vchiq_mmal_component_finalise(dev->instance,
1809 component[MMAL_COMPONENT_IMAGE_ENCODE]);
1811 vchiq_mmal_component_finalise(dev->instance,
1812 dev->component[MMAL_COMPONENT_PREVIEW]);
1814 vchiq_mmal_component_finalise(dev->instance,
1815 dev->component[MMAL_COMPONENT_CAMERA]);
1817 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1819 v4l2_device_unregister(&dev->v4l2_dev);
1824 static struct v4l2_format default_v4l2_format = {
1825 .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
1826 .fmt.pix.width = 1024,
1827 .fmt.pix.bytesperline = 0,
1828 .fmt.pix.height = 768,
1829 .fmt.pix.sizeimage = 1024 * 768,
1832 static int bcm2835_mmal_probe(struct platform_device *pdev)
1835 struct bm2835_mmal_dev *dev;
1836 struct vb2_queue *q;
1838 unsigned int num_cameras;
1839 struct vchiq_mmal_instance *instance;
1840 unsigned int resolutions[MAX_BCM2835_CAMERAS][2];
1843 ret = vchiq_mmal_init(&instance);
1847 num_cameras = get_num_cameras(instance,
1849 MAX_BCM2835_CAMERAS);
1851 if (num_cameras < 1) {
1856 if (num_cameras > MAX_BCM2835_CAMERAS)
1857 num_cameras = MAX_BCM2835_CAMERAS;
1859 for (camera = 0; camera < num_cameras; camera++) {
1860 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1866 /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
1867 mutex_init(&dev->mutex);
1868 dev->camera_num = camera;
1869 dev->max_width = resolutions[camera][0];
1870 dev->max_height = resolutions[camera][1];
1872 /* setup device defaults */
1873 dev->overlay.w.left = 150;
1874 dev->overlay.w.top = 50;
1875 dev->overlay.w.width = 1024;
1876 dev->overlay.w.height = 768;
1877 dev->overlay.clipcount = 0;
1878 dev->overlay.field = V4L2_FIELD_NONE;
1879 dev->overlay.global_alpha = 255;
1881 dev->capture.fmt = &formats[3]; /* JPEG */
1883 /* v4l device registration */
1884 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1885 "%s", BM2835_MMAL_MODULE_NAME);
1886 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1888 dev_err(&pdev->dev, "%s: could not register V4L2 device: %d\n",
1893 /* setup v4l controls */
1894 ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
1896 v4l2_err(&dev->v4l2_dev, "%s: could not init controls: %d\n",
1900 dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
1903 dev->instance = instance;
1904 ret = mmal_init(dev);
1906 v4l2_err(&dev->v4l2_dev, "%s: mmal init failed: %d\n",
1910 /* initialize queue */
1911 q = &dev->capture.vb_vidq;
1912 memset(q, 0, sizeof(*q));
1913 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1914 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1916 q->buf_struct_size = sizeof(struct mmal_buffer);
1917 q->ops = &bm2835_mmal_video_qops;
1918 q->mem_ops = &vb2_vmalloc_memops;
1919 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1920 q->lock = &dev->mutex;
1921 ret = vb2_queue_init(q);
1925 /* initialise video devices */
1926 ret = bm2835_mmal_init_device(dev, &dev->vdev);
1928 v4l2_err(&dev->v4l2_dev, "%s: could not init device: %d\n",
1933 /* Really want to call vidioc_s_fmt_vid_cap with the default
1934 * format, but currently the APIs don't join up.
1936 ret = mmal_setup_components(dev, &default_v4l2_format);
1938 v4l2_err(&dev->v4l2_dev, "%s: could not setup components: %d\n",
1943 v4l2_info(&dev->v4l2_dev,
1944 "Broadcom 2835 MMAL video capture ver %s loaded.\n",
1945 BM2835_MMAL_VERSION);
1952 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1953 v4l2_device_unregister(&dev->v4l2_dev);
1959 for (i = 0; i < camera; i++) {
1960 bcm2835_cleanup_instance(gdev[i]);
1965 vchiq_mmal_finalise(instance);
1970 static int bcm2835_mmal_remove(struct platform_device *pdev)
1973 struct vchiq_mmal_instance *instance = gdev[0]->instance;
1975 for (camera = 0; camera < MAX_BCM2835_CAMERAS; camera++) {
1976 bcm2835_cleanup_instance(gdev[camera]);
1977 gdev[camera] = NULL;
1979 vchiq_mmal_finalise(instance);
1984 static struct platform_driver bcm2835_camera_driver = {
1985 .probe = bcm2835_mmal_probe,
1986 .remove = bcm2835_mmal_remove,
1988 .name = "bcm2835-camera",
1992 module_platform_driver(bcm2835_camera_driver)
1994 MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
1995 MODULE_AUTHOR("Vincent Sanders");
1996 MODULE_LICENSE("GPL");
1997 MODULE_VERSION(BM2835_MMAL_VERSION);
1998 MODULE_ALIAS("platform:bcm2835-camera");