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 ==
315 &dev->component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
318 static void buffer_cb(struct vchiq_mmal_instance *instance,
319 struct vchiq_mmal_port *port,
321 struct mmal_buffer *buf,
322 unsigned long length, u32 mmal_flags, s64 dts, s64 pts)
324 struct bm2835_mmal_dev *dev = port->cb_ctx;
326 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
327 "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
328 __func__, status, buf, length, mmal_flags, pts);
331 /* error in transfer */
333 /* there was a buffer with the error so return it */
334 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
337 } else if (length == 0) {
340 /* this should only ever happen if the port is
341 * disabled and there are buffers still queued
343 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
344 pr_debug("Empty buffer");
345 } else if (dev->capture.frame_count) {
346 /* grab another frame */
347 if (is_capturing(dev)) {
348 pr_debug("Grab another frame");
349 vchiq_mmal_port_parameter_set(
351 dev->capture.camera_port,
352 MMAL_PARAMETER_CAPTURE,
353 &dev->capture.frame_count,
354 sizeof(dev->capture.frame_count));
357 /* signal frame completion */
358 complete(&dev->capture.frame_cmplt);
361 if (dev->capture.frame_count) {
362 if (dev->capture.vc_start_timestamp != -1 && pts) {
364 s64 runtime_us = pts -
365 dev->capture.vc_start_timestamp;
366 timestamp = ktime_add_us(dev->capture.kernel_start_ts,
368 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
369 "Convert start time %llu and %llu with offset %llu to %llu\n",
370 ktime_to_ns(dev->capture.kernel_start_ts),
371 dev->capture.vc_start_timestamp, pts,
372 ktime_to_ns(timestamp));
373 buf->vb.vb2_buf.timestamp = ktime_to_ns(timestamp);
375 buf->vb.vb2_buf.timestamp = ktime_get_ns();
378 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, length);
379 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
381 if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
383 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
384 "Grab another frame as buffer has EOS");
385 vchiq_mmal_port_parameter_set(
387 dev->capture.camera_port,
388 MMAL_PARAMETER_CAPTURE,
389 &dev->capture.frame_count,
390 sizeof(dev->capture.frame_count));
393 /* signal frame completion */
394 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
395 complete(&dev->capture.frame_cmplt);
400 static int enable_camera(struct bm2835_mmal_dev *dev)
404 if (!dev->camera_use_count) {
405 ret = vchiq_mmal_port_parameter_set(
407 &dev->component[MMAL_COMPONENT_CAMERA]->control,
408 MMAL_PARAMETER_CAMERA_NUM, &dev->camera_num,
409 sizeof(dev->camera_num));
411 v4l2_err(&dev->v4l2_dev,
412 "Failed setting camera num, ret %d\n", ret);
416 ret = vchiq_mmal_component_enable(
418 dev->component[MMAL_COMPONENT_CAMERA]);
420 v4l2_err(&dev->v4l2_dev,
421 "Failed enabling camera, ret %d\n", ret);
425 dev->camera_use_count++;
426 v4l2_dbg(1, bcm2835_v4l2_debug,
427 &dev->v4l2_dev, "enabled camera (refcount %d)\n",
428 dev->camera_use_count);
432 static int disable_camera(struct bm2835_mmal_dev *dev)
436 if (!dev->camera_use_count) {
437 v4l2_err(&dev->v4l2_dev,
438 "Disabled the camera when already disabled\n");
441 dev->camera_use_count--;
442 if (!dev->camera_use_count) {
443 unsigned int i = 0xFFFFFFFF;
445 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
446 "Disabling camera\n");
448 vchiq_mmal_component_disable(
450 dev->component[MMAL_COMPONENT_CAMERA]);
452 v4l2_err(&dev->v4l2_dev,
453 "Failed disabling camera, ret %d\n", ret);
456 vchiq_mmal_port_parameter_set(
458 &dev->component[MMAL_COMPONENT_CAMERA]->control,
459 MMAL_PARAMETER_CAMERA_NUM, &i,
462 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
463 "Camera refcount now %d\n", dev->camera_use_count);
467 static void buffer_queue(struct vb2_buffer *vb)
469 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
470 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
471 struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
474 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
475 "%s: dev:%p buf:%p, idx %u\n",
476 __func__, dev, buf, vb2->vb2_buf.index);
478 ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
480 v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
484 static int start_streaming(struct vb2_queue *vq, unsigned int count)
486 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
490 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
493 /* ensure a format has actually been set */
494 if (!dev->capture.port)
497 if (enable_camera(dev) < 0) {
498 v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
502 /*init_completion(&dev->capture.frame_cmplt); */
504 /* enable frame capture */
505 dev->capture.frame_count = 1;
507 /* if the preview is not already running, wait for a few frames for AGC
510 if (!dev->component[MMAL_COMPONENT_PREVIEW]->enabled)
513 /* enable the connection from camera to encoder (if applicable) */
514 if (dev->capture.camera_port != dev->capture.port &&
515 dev->capture.camera_port) {
516 ret = vchiq_mmal_port_enable(dev->instance,
517 dev->capture.camera_port, NULL);
519 v4l2_err(&dev->v4l2_dev,
520 "Failed to enable encode tunnel - error %d\n",
526 /* Get VC timestamp at this point in time */
527 parameter_size = sizeof(dev->capture.vc_start_timestamp);
528 if (vchiq_mmal_port_parameter_get(dev->instance,
529 dev->capture.camera_port,
530 MMAL_PARAMETER_SYSTEM_TIME,
531 &dev->capture.vc_start_timestamp,
533 v4l2_err(&dev->v4l2_dev,
534 "Failed to get VC start time - update your VC f/w\n");
536 /* Flag to indicate just to rely on kernel timestamps */
537 dev->capture.vc_start_timestamp = -1;
539 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
540 "Start time %lld size %d\n",
541 dev->capture.vc_start_timestamp, parameter_size);
543 dev->capture.kernel_start_ts = ktime_get();
545 /* enable the camera port */
546 dev->capture.port->cb_ctx = dev;
548 vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
550 v4l2_err(&dev->v4l2_dev,
551 "Failed to enable capture port - error %d. Disabling camera port again\n",
554 vchiq_mmal_port_disable(dev->instance,
555 dev->capture.camera_port);
556 if (disable_camera(dev) < 0) {
557 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
563 /* capture the first frame */
564 vchiq_mmal_port_parameter_set(dev->instance,
565 dev->capture.camera_port,
566 MMAL_PARAMETER_CAPTURE,
567 &dev->capture.frame_count,
568 sizeof(dev->capture.frame_count));
572 /* abort streaming and wait for last buffer */
573 static void stop_streaming(struct vb2_queue *vq)
576 unsigned long timeout;
577 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
579 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
582 init_completion(&dev->capture.frame_cmplt);
583 dev->capture.frame_count = 0;
585 /* ensure a format has actually been set */
586 if (!dev->capture.port) {
587 v4l2_err(&dev->v4l2_dev,
588 "no capture port - stream not started?\n");
592 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
594 /* stop capturing frames */
595 vchiq_mmal_port_parameter_set(dev->instance,
596 dev->capture.camera_port,
597 MMAL_PARAMETER_CAPTURE,
598 &dev->capture.frame_count,
599 sizeof(dev->capture.frame_count));
601 /* wait for last frame to complete */
602 timeout = wait_for_completion_timeout(&dev->capture.frame_cmplt, HZ);
604 v4l2_err(&dev->v4l2_dev,
605 "timed out waiting for frame completion\n");
607 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
608 "disabling connection\n");
610 /* disable the connection from camera to encoder */
611 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
612 if (!ret && dev->capture.camera_port != dev->capture.port) {
613 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
615 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
616 } else if (dev->capture.camera_port != dev->capture.port) {
617 v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
621 if (disable_camera(dev) < 0)
622 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
625 static const struct vb2_ops bm2835_mmal_video_qops = {
626 .queue_setup = queue_setup,
627 .buf_init = buffer_init,
628 .buf_prepare = buffer_prepare,
629 .buf_cleanup = buffer_cleanup,
630 .buf_queue = buffer_queue,
631 .start_streaming = start_streaming,
632 .stop_streaming = stop_streaming,
633 .wait_prepare = vb2_ops_wait_prepare,
634 .wait_finish = vb2_ops_wait_finish,
637 /* ------------------------------------------------------------------
639 * ------------------------------------------------------------------
642 static int set_overlay_params(struct bm2835_mmal_dev *dev,
643 struct vchiq_mmal_port *port)
645 struct mmal_parameter_displayregion prev_config = {
646 .set = MMAL_DISPLAY_SET_LAYER |
647 MMAL_DISPLAY_SET_ALPHA |
648 MMAL_DISPLAY_SET_DEST_RECT |
649 MMAL_DISPLAY_SET_FULLSCREEN,
650 .layer = PREVIEW_LAYER,
651 .alpha = dev->overlay.global_alpha,
654 .x = dev->overlay.w.left,
655 .y = dev->overlay.w.top,
656 .width = dev->overlay.w.width,
657 .height = dev->overlay.w.height,
660 return vchiq_mmal_port_parameter_set(dev->instance, port,
661 MMAL_PARAMETER_DISPLAYREGION,
662 &prev_config, sizeof(prev_config));
666 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
667 struct v4l2_fmtdesc *f)
669 struct mmal_fmt *fmt;
671 if (f->index >= ARRAY_SIZE(formats))
674 fmt = &formats[f->index];
676 strlcpy((char *)f->description, fmt->name, sizeof(f->description));
677 f->pixelformat = fmt->fourcc;
678 f->flags = fmt->flags;
683 static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
684 struct v4l2_format *f)
686 struct bm2835_mmal_dev *dev = video_drvdata(file);
688 f->fmt.win = dev->overlay;
693 static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
694 struct v4l2_format *f)
696 struct bm2835_mmal_dev *dev = video_drvdata(file);
698 f->fmt.win.field = V4L2_FIELD_NONE;
699 f->fmt.win.chromakey = 0;
700 f->fmt.win.clips = NULL;
701 f->fmt.win.clipcount = 0;
702 f->fmt.win.bitmap = NULL;
704 v4l_bound_align_image(&f->fmt.win.w.width, MIN_WIDTH, dev->max_width, 1,
705 &f->fmt.win.w.height, MIN_HEIGHT, dev->max_height,
707 v4l_bound_align_image(&f->fmt.win.w.left, MIN_WIDTH, dev->max_width, 1,
708 &f->fmt.win.w.top, MIN_HEIGHT, dev->max_height,
711 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
712 "Overlay: Now w/h %dx%d l/t %dx%d\n",
713 f->fmt.win.w.width, f->fmt.win.w.height,
714 f->fmt.win.w.left, f->fmt.win.w.top);
716 v4l2_dump_win_format(1,
724 static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
725 struct v4l2_format *f)
727 struct bm2835_mmal_dev *dev = video_drvdata(file);
729 vidioc_try_fmt_vid_overlay(file, priv, f);
731 dev->overlay = f->fmt.win;
732 if (dev->component[MMAL_COMPONENT_PREVIEW]->enabled) {
733 set_overlay_params(dev,
734 &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
740 static int vidioc_overlay(struct file *file, void *f, unsigned int on)
743 struct bm2835_mmal_dev *dev = video_drvdata(file);
744 struct vchiq_mmal_port *src;
745 struct vchiq_mmal_port *dst;
747 if ((on && dev->component[MMAL_COMPONENT_PREVIEW]->enabled) ||
748 (!on && !dev->component[MMAL_COMPONENT_PREVIEW]->enabled))
749 return 0; /* already in requested state */
752 &dev->component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_PREVIEW];
755 /* disconnect preview ports and disable component */
756 ret = vchiq_mmal_port_disable(dev->instance, src);
759 vchiq_mmal_port_connect_tunnel(dev->instance, src,
762 ret = vchiq_mmal_component_disable(
764 dev->component[MMAL_COMPONENT_PREVIEW]);
770 /* set preview port format and connect it to output */
771 dst = &dev->component[MMAL_COMPONENT_PREVIEW]->input[0];
773 ret = vchiq_mmal_port_set_format(dev->instance, src);
777 ret = set_overlay_params(dev, dst);
781 if (enable_camera(dev) < 0)
784 ret = vchiq_mmal_component_enable(
786 dev->component[MMAL_COMPONENT_PREVIEW]);
790 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
792 ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
794 ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
799 static int vidioc_g_fbuf(struct file *file, void *fh,
800 struct v4l2_framebuffer *a)
802 /* The video overlay must stay within the framebuffer and can't be
803 * positioned independently.
805 struct bm2835_mmal_dev *dev = video_drvdata(file);
806 struct vchiq_mmal_port *preview_port =
807 &dev->component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_PREVIEW];
809 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
810 V4L2_FBUF_CAP_GLOBAL_ALPHA;
811 a->flags = V4L2_FBUF_FLAG_OVERLAY;
812 a->fmt.width = preview_port->es.video.width;
813 a->fmt.height = preview_port->es.video.height;
814 a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
815 a->fmt.bytesperline = preview_port->es.video.width;
816 a->fmt.sizeimage = (preview_port->es.video.width *
817 preview_port->es.video.height * 3) >> 1;
818 a->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
824 static int vidioc_enum_input(struct file *file, void *priv,
825 struct v4l2_input *inp)
827 /* only a single camera input */
831 inp->type = V4L2_INPUT_TYPE_CAMERA;
832 sprintf((char *)inp->name, "Camera %u", inp->index);
836 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
842 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
851 static int vidioc_querycap(struct file *file, void *priv,
852 struct v4l2_capability *cap)
854 struct bm2835_mmal_dev *dev = video_drvdata(file);
858 vchiq_mmal_version(dev->instance, &major, &minor);
860 strcpy((char *)cap->driver, "bm2835 mmal");
861 snprintf((char *)cap->card, sizeof(cap->card), "mmal service %d.%d",
864 snprintf((char *)cap->bus_info, sizeof(cap->bus_info),
865 "platform:%s", dev->v4l2_dev.name);
866 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
867 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
868 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
873 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
874 struct v4l2_fmtdesc *f)
876 struct mmal_fmt *fmt;
878 if (f->index >= ARRAY_SIZE(formats))
881 fmt = &formats[f->index];
883 strlcpy((char *)f->description, fmt->name, sizeof(f->description));
884 f->pixelformat = fmt->fourcc;
885 f->flags = fmt->flags;
890 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
891 struct v4l2_format *f)
893 struct bm2835_mmal_dev *dev = video_drvdata(file);
895 f->fmt.pix.width = dev->capture.width;
896 f->fmt.pix.height = dev->capture.height;
897 f->fmt.pix.field = V4L2_FIELD_NONE;
898 f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
899 f->fmt.pix.bytesperline = dev->capture.stride;
900 f->fmt.pix.sizeimage = dev->capture.buffersize;
902 if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
903 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
904 else if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG)
905 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
907 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
910 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
915 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
916 struct v4l2_format *f)
918 struct bm2835_mmal_dev *dev = video_drvdata(file);
919 struct mmal_fmt *mfmt;
921 mfmt = get_format(f);
923 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
924 "Fourcc format (0x%08x) unknown.\n",
925 f->fmt.pix.pixelformat);
926 f->fmt.pix.pixelformat = formats[0].fourcc;
927 mfmt = get_format(f);
930 f->fmt.pix.field = V4L2_FIELD_NONE;
932 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
933 "Clipping/aligning %dx%d format %08X\n",
934 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
936 v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, dev->max_width, 1,
937 &f->fmt.pix.height, MIN_HEIGHT, dev->max_height,
939 f->fmt.pix.bytesperline = f->fmt.pix.width * mfmt->ybbp;
940 if (!mfmt->remove_padding) {
941 int align_mask = ((32 * mfmt->depth) >> 3) - 1;
942 /* GPU isn't removing padding, so stride is aligned to 32 */
943 f->fmt.pix.bytesperline =
944 (f->fmt.pix.bytesperline + align_mask) & ~align_mask;
945 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
946 "Not removing padding, so bytes/line = %d, "
948 f->fmt.pix.bytesperline, align_mask);
951 /* Image buffer has to be padded to allow for alignment, even though
952 * we sometimes then remove that padding before delivering the buffer.
954 f->fmt.pix.sizeimage = ((f->fmt.pix.height + 15) & ~15) *
955 (((f->fmt.pix.width + 31) & ~31) * mfmt->depth) >> 3;
957 if ((mfmt->flags & V4L2_FMT_FLAG_COMPRESSED) &&
958 f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
959 f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
961 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24)
962 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
963 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
964 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
966 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
969 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
970 "Now %dx%d format %08X\n",
971 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
973 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
978 static int mmal_setup_components(struct bm2835_mmal_dev *dev,
979 struct v4l2_format *f)
982 struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
983 struct vchiq_mmal_component *encode_component = NULL;
984 struct mmal_fmt *mfmt = get_format(f);
990 if (dev->capture.encode_component) {
991 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
992 "vid_cap - disconnect previous tunnel\n");
994 /* Disconnect any previous connection */
995 vchiq_mmal_port_connect_tunnel(dev->instance,
996 dev->capture.camera_port, NULL);
997 dev->capture.camera_port = NULL;
998 ret = vchiq_mmal_component_disable(dev->instance,
999 dev->capture.encode_component);
1001 v4l2_err(&dev->v4l2_dev,
1002 "Failed to disable encode component %d\n",
1005 dev->capture.encode_component = NULL;
1007 /* format dependent port setup */
1008 switch (mfmt->mmal_component) {
1009 case MMAL_COMPONENT_CAMERA:
1010 /* Make a further decision on port based on resolution */
1011 if (f->fmt.pix.width <= max_video_width &&
1012 f->fmt.pix.height <= max_video_height)
1013 camera_port = port =
1014 &dev->component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_VIDEO];
1016 camera_port = port =
1017 &dev->component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
1019 case MMAL_COMPONENT_IMAGE_ENCODE:
1020 encode_component = dev->component[MMAL_COMPONENT_IMAGE_ENCODE];
1021 port = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
1023 &dev->component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
1025 case MMAL_COMPONENT_VIDEO_ENCODE:
1026 encode_component = dev->component[MMAL_COMPONENT_VIDEO_ENCODE];
1027 port = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
1029 &dev->component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_VIDEO];
1038 if (encode_component)
1039 camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
1041 camera_port->format.encoding = mfmt->mmal;
1043 if (dev->rgb_bgr_swapped) {
1044 if (camera_port->format.encoding == MMAL_ENCODING_RGB24)
1045 camera_port->format.encoding = MMAL_ENCODING_BGR24;
1046 else if (camera_port->format.encoding == MMAL_ENCODING_BGR24)
1047 camera_port->format.encoding = MMAL_ENCODING_RGB24;
1050 remove_padding = mfmt->remove_padding;
1051 vchiq_mmal_port_parameter_set(dev->instance,
1053 MMAL_PARAMETER_NO_IMAGE_PADDING,
1054 &remove_padding, sizeof(remove_padding));
1056 camera_port->format.encoding_variant = 0;
1057 camera_port->es.video.width = f->fmt.pix.width;
1058 camera_port->es.video.height = f->fmt.pix.height;
1059 camera_port->es.video.crop.x = 0;
1060 camera_port->es.video.crop.y = 0;
1061 camera_port->es.video.crop.width = f->fmt.pix.width;
1062 camera_port->es.video.crop.height = f->fmt.pix.height;
1063 camera_port->es.video.frame_rate.num = 0;
1064 camera_port->es.video.frame_rate.den = 1;
1065 camera_port->es.video.color_space = MMAL_COLOR_SPACE_JPEG_JFIF;
1067 ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
1071 &dev->component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_VIDEO]) {
1072 bool overlay_enabled =
1073 !!dev->component[MMAL_COMPONENT_PREVIEW]->enabled;
1074 struct vchiq_mmal_port *preview_port =
1075 &dev->component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_PREVIEW];
1076 /* Preview and encode ports need to match on resolution */
1077 if (overlay_enabled) {
1078 /* Need to disable the overlay before we can update
1082 vchiq_mmal_port_disable(dev->instance,
1086 vchiq_mmal_port_connect_tunnel(
1091 preview_port->es.video.width = f->fmt.pix.width;
1092 preview_port->es.video.height = f->fmt.pix.height;
1093 preview_port->es.video.crop.x = 0;
1094 preview_port->es.video.crop.y = 0;
1095 preview_port->es.video.crop.width = f->fmt.pix.width;
1096 preview_port->es.video.crop.height = f->fmt.pix.height;
1097 preview_port->es.video.frame_rate.num =
1098 dev->capture.timeperframe.denominator;
1099 preview_port->es.video.frame_rate.den =
1100 dev->capture.timeperframe.numerator;
1101 ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
1102 if (overlay_enabled) {
1103 ret = vchiq_mmal_port_connect_tunnel(
1106 &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
1108 ret = vchiq_mmal_port_enable(dev->instance,
1115 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1116 "%s failed to set format %dx%d %08X\n", __func__,
1117 f->fmt.pix.width, f->fmt.pix.height,
1118 f->fmt.pix.pixelformat);
1119 /* ensure capture is not going to be tried */
1120 dev->capture.port = NULL;
1122 if (encode_component) {
1123 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1124 "vid_cap - set up encode comp\n");
1126 /* configure buffering */
1127 camera_port->current_buffer.size =
1128 camera_port->recommended_buffer.size;
1129 camera_port->current_buffer.num =
1130 camera_port->recommended_buffer.num;
1133 vchiq_mmal_port_connect_tunnel(
1136 &encode_component->input[0]);
1138 v4l2_dbg(1, bcm2835_v4l2_debug,
1140 "%s failed to create connection\n",
1142 /* ensure capture is not going to be tried */
1143 dev->capture.port = NULL;
1145 port->es.video.width = f->fmt.pix.width;
1146 port->es.video.height = f->fmt.pix.height;
1147 port->es.video.crop.x = 0;
1148 port->es.video.crop.y = 0;
1149 port->es.video.crop.width = f->fmt.pix.width;
1150 port->es.video.crop.height = f->fmt.pix.height;
1151 port->es.video.frame_rate.num =
1152 dev->capture.timeperframe.denominator;
1153 port->es.video.frame_rate.den =
1154 dev->capture.timeperframe.numerator;
1156 port->format.encoding = mfmt->mmal;
1157 port->format.encoding_variant = 0;
1158 /* Set any encoding specific parameters */
1159 switch (mfmt->mmal_component) {
1160 case MMAL_COMPONENT_VIDEO_ENCODE:
1161 port->format.bitrate =
1162 dev->capture.encode_bitrate;
1164 case MMAL_COMPONENT_IMAGE_ENCODE:
1165 /* Could set EXIF parameters here */
1170 ret = vchiq_mmal_port_set_format(dev->instance,
1173 v4l2_dbg(1, bcm2835_v4l2_debug,
1175 "%s failed to set format %dx%d fmt %08X\n",
1179 f->fmt.pix.pixelformat
1184 ret = vchiq_mmal_component_enable(
1188 v4l2_dbg(1, bcm2835_v4l2_debug,
1190 "%s Failed to enable encode components\n",
1195 /* configure buffering */
1196 port->current_buffer.num = 1;
1197 port->current_buffer.size =
1198 f->fmt.pix.sizeimage;
1199 if (port->format.encoding ==
1200 MMAL_ENCODING_JPEG) {
1201 v4l2_dbg(1, bcm2835_v4l2_debug,
1203 "JPG - buf size now %d was %d\n",
1204 f->fmt.pix.sizeimage,
1205 port->current_buffer.size);
1206 port->current_buffer.size =
1207 (f->fmt.pix.sizeimage <
1210 : f->fmt.pix.sizeimage;
1212 v4l2_dbg(1, bcm2835_v4l2_debug,
1214 "vid_cap - cur_buf.size set to %d\n",
1215 f->fmt.pix.sizeimage);
1216 port->current_buffer.alignment = 0;
1219 /* configure buffering */
1220 camera_port->current_buffer.num = 1;
1221 camera_port->current_buffer.size = f->fmt.pix.sizeimage;
1222 camera_port->current_buffer.alignment = 0;
1226 dev->capture.fmt = mfmt;
1227 dev->capture.stride = f->fmt.pix.bytesperline;
1228 dev->capture.width = camera_port->es.video.crop.width;
1229 dev->capture.height = camera_port->es.video.crop.height;
1230 dev->capture.buffersize = port->current_buffer.size;
1232 /* select port for capture */
1233 dev->capture.port = port;
1234 dev->capture.camera_port = camera_port;
1235 dev->capture.encode_component = encode_component;
1236 v4l2_dbg(1, bcm2835_v4l2_debug,
1238 "Set dev->capture.fmt %08X, %dx%d, stride %d, size %d",
1239 port->format.encoding,
1240 dev->capture.width, dev->capture.height,
1241 dev->capture.stride, dev->capture.buffersize);
1245 /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
1249 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1250 struct v4l2_format *f)
1253 struct bm2835_mmal_dev *dev = video_drvdata(file);
1254 struct mmal_fmt *mfmt;
1256 /* try the format to set valid parameters */
1257 ret = vidioc_try_fmt_vid_cap(file, priv, f);
1259 v4l2_err(&dev->v4l2_dev,
1260 "vid_cap - vidioc_try_fmt_vid_cap failed\n");
1264 /* if a capture is running refuse to set format */
1265 if (vb2_is_busy(&dev->capture.vb_vidq)) {
1266 v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
1270 /* If the format is unsupported v4l2 says we should switch to
1271 * a supported one and not return an error.
1273 mfmt = get_format(f);
1275 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1276 "Fourcc format (0x%08x) unknown.\n",
1277 f->fmt.pix.pixelformat);
1278 f->fmt.pix.pixelformat = formats[0].fourcc;
1279 mfmt = get_format(f);
1282 ret = mmal_setup_components(dev, f);
1284 v4l2_err(&dev->v4l2_dev,
1285 "%s: failed to setup mmal components: %d\n",
1293 static int vidioc_enum_framesizes(struct file *file, void *fh,
1294 struct v4l2_frmsizeenum *fsize)
1296 struct bm2835_mmal_dev *dev = video_drvdata(file);
1297 static const struct v4l2_frmsize_stepwise sizes = {
1305 for (i = 0; i < ARRAY_SIZE(formats); i++)
1306 if (formats[i].fourcc == fsize->pixel_format)
1308 if (i == ARRAY_SIZE(formats))
1310 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1311 fsize->stepwise = sizes;
1312 fsize->stepwise.max_width = dev->max_width;
1313 fsize->stepwise.max_height = dev->max_height;
1317 /* timeperframe is arbitrary and continuous */
1318 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1319 struct v4l2_frmivalenum *fival)
1321 struct bm2835_mmal_dev *dev = video_drvdata(file);
1327 for (i = 0; i < ARRAY_SIZE(formats); i++)
1328 if (formats[i].fourcc == fival->pixel_format)
1330 if (i == ARRAY_SIZE(formats))
1333 /* regarding width & height - we support any within range */
1334 if (fival->width < MIN_WIDTH || fival->width > dev->max_width ||
1335 fival->height < MIN_HEIGHT || fival->height > dev->max_height)
1338 fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1340 /* fill in stepwise (step=1.0 is required by V4L2 spec) */
1341 fival->stepwise.min = tpf_min;
1342 fival->stepwise.max = tpf_max;
1343 fival->stepwise.step = (struct v4l2_fract) {1, 1};
1348 static int vidioc_g_parm(struct file *file, void *priv,
1349 struct v4l2_streamparm *parm)
1351 struct bm2835_mmal_dev *dev = video_drvdata(file);
1353 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1356 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1357 parm->parm.capture.timeperframe = dev->capture.timeperframe;
1358 parm->parm.capture.readbuffers = 1;
1362 static int vidioc_s_parm(struct file *file, void *priv,
1363 struct v4l2_streamparm *parm)
1365 struct bm2835_mmal_dev *dev = video_drvdata(file);
1366 struct v4l2_fract tpf;
1368 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1371 tpf = parm->parm.capture.timeperframe;
1373 /* tpf: {*, 0} resets timing; clip to [min, max]*/
1374 tpf = tpf.denominator ? tpf : tpf_default;
1375 tpf = V4L2_FRACT_COMPARE(tpf, <, tpf_min) ? tpf_min : tpf;
1376 tpf = V4L2_FRACT_COMPARE(tpf, >, tpf_max) ? tpf_max : tpf;
1378 dev->capture.timeperframe = tpf;
1379 parm->parm.capture.timeperframe = tpf;
1380 parm->parm.capture.readbuffers = 1;
1381 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1383 set_framerate_params(dev);
1388 static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
1390 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1391 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1392 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1393 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1394 .vidioc_overlay = vidioc_overlay,
1395 .vidioc_g_fbuf = vidioc_g_fbuf,
1398 .vidioc_enum_input = vidioc_enum_input,
1399 .vidioc_g_input = vidioc_g_input,
1400 .vidioc_s_input = vidioc_s_input,
1403 .vidioc_querycap = vidioc_querycap,
1404 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1405 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1406 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1407 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1409 /* buffer management */
1410 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1411 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1412 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1413 .vidioc_querybuf = vb2_ioctl_querybuf,
1414 .vidioc_qbuf = vb2_ioctl_qbuf,
1415 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1416 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1417 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1418 .vidioc_g_parm = vidioc_g_parm,
1419 .vidioc_s_parm = vidioc_s_parm,
1420 .vidioc_streamon = vb2_ioctl_streamon,
1421 .vidioc_streamoff = vb2_ioctl_streamoff,
1423 .vidioc_log_status = v4l2_ctrl_log_status,
1424 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1425 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1428 /* ------------------------------------------------------------------
1429 * Driver init/finalise
1430 * ------------------------------------------------------------------
1433 static const struct v4l2_file_operations camera0_fops = {
1434 .owner = THIS_MODULE,
1435 .open = v4l2_fh_open,
1436 .release = vb2_fop_release,
1437 .read = vb2_fop_read,
1438 .poll = vb2_fop_poll,
1439 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1440 .mmap = vb2_fop_mmap,
1443 static const struct video_device vdev_template = {
1445 .fops = &camera0_fops,
1446 .ioctl_ops = &camera0_ioctl_ops,
1447 .release = video_device_release_empty,
1450 /* Returns the number of cameras, and also the max resolution supported
1453 static int get_num_cameras(struct vchiq_mmal_instance *instance,
1454 unsigned int resolutions[][2], int num_resolutions)
1457 struct vchiq_mmal_component *cam_info_component;
1458 struct mmal_parameter_camera_info_t cam_info = {0};
1459 u32 param_size = sizeof(cam_info);
1462 /* create a camera_info component */
1463 ret = vchiq_mmal_component_init(instance, "camera_info",
1464 &cam_info_component);
1466 /* Unusual failure - let's guess one camera. */
1469 if (vchiq_mmal_port_parameter_get(instance,
1470 &cam_info_component->control,
1471 MMAL_PARAMETER_CAMERA_INFO,
1474 pr_info("Failed to get camera info\n");
1477 i < min_t(unsigned int, cam_info.num_cameras, num_resolutions);
1479 resolutions[i][0] = cam_info.cameras[i].max_width;
1480 resolutions[i][1] = cam_info.cameras[i].max_height;
1483 vchiq_mmal_component_finalise(instance,
1484 cam_info_component);
1486 return cam_info.num_cameras;
1489 static int set_camera_parameters(struct vchiq_mmal_instance *instance,
1490 struct vchiq_mmal_component *camera,
1491 struct bm2835_mmal_dev *dev)
1493 struct mmal_parameter_camera_config cam_config = {
1494 .max_stills_w = dev->max_width,
1495 .max_stills_h = dev->max_height,
1497 .one_shot_stills = 1,
1498 .max_preview_video_w = (max_video_width > 1920) ?
1499 max_video_width : 1920,
1500 .max_preview_video_h = (max_video_height > 1088) ?
1501 max_video_height : 1088,
1502 .num_preview_video_frames = 3,
1503 .stills_capture_circular_buffer_height = 0,
1504 .fast_preview_resume = 0,
1505 .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
1508 return vchiq_mmal_port_parameter_set(instance, &camera->control,
1509 MMAL_PARAMETER_CAMERA_CONFIG,
1510 &cam_config, sizeof(cam_config));
1513 #define MAX_SUPPORTED_ENCODINGS 20
1515 /* MMAL instance and component init */
1516 static int mmal_init(struct bm2835_mmal_dev *dev)
1519 struct mmal_es_format_local *format;
1520 u32 supported_encodings[MAX_SUPPORTED_ENCODINGS];
1522 struct vchiq_mmal_component *camera;
1524 ret = vchiq_mmal_init(&dev->instance);
1526 v4l2_err(&dev->v4l2_dev, "%s: vchiq mmal init failed %d\n",
1531 /* get the camera component ready */
1532 ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
1533 &dev->component[MMAL_COMPONENT_CAMERA]);
1537 camera = dev->component[MMAL_COMPONENT_CAMERA];
1538 if (camera->outputs < MMAL_CAMERA_PORT_COUNT) {
1539 v4l2_err(&dev->v4l2_dev, "%s: too few camera outputs %d needed %d\n",
1540 __func__, camera->outputs, MMAL_CAMERA_PORT_COUNT);
1545 ret = set_camera_parameters(dev->instance,
1549 v4l2_err(&dev->v4l2_dev, "%s: unable to set camera parameters: %d\n",
1554 /* There was an error in the firmware that meant the camera component
1555 * produced BGR instead of RGB.
1556 * This is now fixed, but in order to support the old firmwares, we
1559 dev->rgb_bgr_swapped = true;
1560 param_size = sizeof(supported_encodings);
1561 ret = vchiq_mmal_port_parameter_get(dev->instance,
1562 &camera->output[MMAL_CAMERA_PORT_CAPTURE],
1563 MMAL_PARAMETER_SUPPORTED_ENCODINGS,
1564 &supported_encodings,
1569 for (i = 0; i < param_size / sizeof(u32); i++) {
1570 if (supported_encodings[i] == MMAL_ENCODING_BGR24) {
1571 /* Found BGR24 first - old firmware. */
1574 if (supported_encodings[i] == MMAL_ENCODING_RGB24) {
1575 /* Found RGB24 first
1576 * new firmware, so use RGB24.
1578 dev->rgb_bgr_swapped = false;
1583 format = &camera->output[MMAL_CAMERA_PORT_PREVIEW].format;
1585 format->encoding = MMAL_ENCODING_OPAQUE;
1586 format->encoding_variant = MMAL_ENCODING_I420;
1588 format->es->video.width = 1024;
1589 format->es->video.height = 768;
1590 format->es->video.crop.x = 0;
1591 format->es->video.crop.y = 0;
1592 format->es->video.crop.width = 1024;
1593 format->es->video.crop.height = 768;
1594 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1595 format->es->video.frame_rate.den = 1;
1597 format = &camera->output[MMAL_CAMERA_PORT_VIDEO].format;
1599 format->encoding = MMAL_ENCODING_OPAQUE;
1600 format->encoding_variant = MMAL_ENCODING_I420;
1602 format->es->video.width = 1024;
1603 format->es->video.height = 768;
1604 format->es->video.crop.x = 0;
1605 format->es->video.crop.y = 0;
1606 format->es->video.crop.width = 1024;
1607 format->es->video.crop.height = 768;
1608 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1609 format->es->video.frame_rate.den = 1;
1611 format = &camera->output[MMAL_CAMERA_PORT_CAPTURE].format;
1613 format->encoding = MMAL_ENCODING_OPAQUE;
1615 format->es->video.width = 2592;
1616 format->es->video.height = 1944;
1617 format->es->video.crop.x = 0;
1618 format->es->video.crop.y = 0;
1619 format->es->video.crop.width = 2592;
1620 format->es->video.crop.height = 1944;
1621 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1622 format->es->video.frame_rate.den = 1;
1624 dev->capture.width = format->es->video.width;
1625 dev->capture.height = format->es->video.height;
1626 dev->capture.fmt = &formats[0];
1627 dev->capture.encode_component = NULL;
1628 dev->capture.timeperframe = tpf_default;
1629 dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
1630 dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
1632 /* get the preview component ready */
1633 ret = vchiq_mmal_component_init(
1634 dev->instance, "ril.video_render",
1635 &dev->component[MMAL_COMPONENT_PREVIEW]);
1639 if (dev->component[MMAL_COMPONENT_PREVIEW]->inputs < 1) {
1641 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1642 __func__, dev->component[MMAL_COMPONENT_PREVIEW]->inputs, 1);
1646 /* get the image encoder component ready */
1647 ret = vchiq_mmal_component_init(
1648 dev->instance, "ril.image_encode",
1649 &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1653 if (dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs < 1) {
1655 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1656 __func__, dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs,
1658 goto unreg_image_encoder;
1661 /* get the video encoder component ready */
1662 ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
1663 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
1665 goto unreg_image_encoder;
1667 if (dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs < 1) {
1669 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1670 __func__, dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs,
1672 goto unreg_vid_encoder;
1676 struct vchiq_mmal_port *encoder_port =
1677 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
1678 encoder_port->format.encoding = MMAL_ENCODING_H264;
1679 ret = vchiq_mmal_port_set_format(dev->instance,
1684 unsigned int enable = 1;
1686 vchiq_mmal_port_parameter_set(
1688 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
1689 MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
1690 &enable, sizeof(enable));
1692 vchiq_mmal_port_parameter_set(dev->instance,
1693 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
1694 MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
1698 ret = bm2835_mmal_set_all_camera_controls(dev);
1700 v4l2_err(&dev->v4l2_dev, "%s: failed to set all camera controls: %d\n",
1702 goto unreg_vid_encoder;
1708 pr_err("Cleanup: Destroy video encoder\n");
1709 vchiq_mmal_component_finalise(
1711 dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
1713 unreg_image_encoder:
1714 pr_err("Cleanup: Destroy image encoder\n");
1715 vchiq_mmal_component_finalise(
1717 dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1720 pr_err("Cleanup: Destroy video render\n");
1721 vchiq_mmal_component_finalise(dev->instance,
1722 dev->component[MMAL_COMPONENT_PREVIEW]);
1725 pr_err("Cleanup: Destroy camera\n");
1726 vchiq_mmal_component_finalise(dev->instance,
1727 dev->component[MMAL_COMPONENT_CAMERA]);
1730 vchiq_mmal_finalise(dev->instance);
1734 static int bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
1735 struct video_device *vfd)
1739 *vfd = vdev_template;
1741 vfd->v4l2_dev = &dev->v4l2_dev;
1743 vfd->lock = &dev->mutex;
1745 vfd->queue = &dev->capture.vb_vidq;
1747 /* video device needs to be able to access instance data */
1748 video_set_drvdata(vfd, dev);
1750 ret = video_register_device(vfd,
1752 video_nr[dev->camera_num]);
1756 v4l2_info(vfd->v4l2_dev,
1757 "V4L2 device registered as %s - stills mode > %dx%d\n",
1758 video_device_node_name(vfd),
1759 max_video_width, max_video_height);
1764 static void bcm2835_cleanup_instance(struct bm2835_mmal_dev *dev)
1769 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1770 video_device_node_name(&dev->vdev));
1772 video_unregister_device(&dev->vdev);
1774 if (dev->capture.encode_component) {
1775 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1776 "mmal_exit - disconnect tunnel\n");
1777 vchiq_mmal_port_connect_tunnel(dev->instance,
1778 dev->capture.camera_port, NULL);
1779 vchiq_mmal_component_disable(dev->instance,
1780 dev->capture.encode_component);
1782 vchiq_mmal_component_disable(dev->instance,
1783 dev->component[MMAL_COMPONENT_CAMERA]);
1785 vchiq_mmal_component_finalise(dev->instance,
1786 dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
1788 vchiq_mmal_component_finalise(dev->instance,
1789 dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1791 vchiq_mmal_component_finalise(dev->instance,
1792 dev->component[MMAL_COMPONENT_PREVIEW]);
1794 vchiq_mmal_component_finalise(dev->instance,
1795 dev->component[MMAL_COMPONENT_CAMERA]);
1797 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1799 v4l2_device_unregister(&dev->v4l2_dev);
1804 static struct v4l2_format default_v4l2_format = {
1805 .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
1806 .fmt.pix.width = 1024,
1807 .fmt.pix.bytesperline = 0,
1808 .fmt.pix.height = 768,
1809 .fmt.pix.sizeimage = 1024 * 768,
1812 static int bcm2835_mmal_probe(struct platform_device *pdev)
1815 struct bm2835_mmal_dev *dev;
1816 struct vb2_queue *q;
1818 unsigned int num_cameras;
1819 struct vchiq_mmal_instance *instance;
1820 unsigned int resolutions[MAX_BCM2835_CAMERAS][2];
1823 ret = vchiq_mmal_init(&instance);
1827 num_cameras = get_num_cameras(instance,
1829 MAX_BCM2835_CAMERAS);
1831 if (num_cameras < 1) {
1836 if (num_cameras > MAX_BCM2835_CAMERAS)
1837 num_cameras = MAX_BCM2835_CAMERAS;
1839 for (camera = 0; camera < num_cameras; camera++) {
1840 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1846 /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
1847 mutex_init(&dev->mutex);
1848 dev->camera_num = camera;
1849 dev->max_width = resolutions[camera][0];
1850 dev->max_height = resolutions[camera][1];
1852 /* setup device defaults */
1853 dev->overlay.w.left = 150;
1854 dev->overlay.w.top = 50;
1855 dev->overlay.w.width = 1024;
1856 dev->overlay.w.height = 768;
1857 dev->overlay.clipcount = 0;
1858 dev->overlay.field = V4L2_FIELD_NONE;
1859 dev->overlay.global_alpha = 255;
1861 dev->capture.fmt = &formats[3]; /* JPEG */
1863 /* v4l device registration */
1864 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1865 "%s", BM2835_MMAL_MODULE_NAME);
1866 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1868 dev_err(&pdev->dev, "%s: could not register V4L2 device: %d\n",
1873 /* setup v4l controls */
1874 ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
1876 v4l2_err(&dev->v4l2_dev, "%s: could not init controls: %d\n",
1880 dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
1883 dev->instance = instance;
1884 ret = mmal_init(dev);
1886 v4l2_err(&dev->v4l2_dev, "%s: mmal init failed: %d\n",
1890 /* initialize queue */
1891 q = &dev->capture.vb_vidq;
1892 memset(q, 0, sizeof(*q));
1893 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1894 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1896 q->buf_struct_size = sizeof(struct mmal_buffer);
1897 q->ops = &bm2835_mmal_video_qops;
1898 q->mem_ops = &vb2_vmalloc_memops;
1899 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1900 q->lock = &dev->mutex;
1901 ret = vb2_queue_init(q);
1905 /* initialise video devices */
1906 ret = bm2835_mmal_init_device(dev, &dev->vdev);
1908 v4l2_err(&dev->v4l2_dev, "%s: could not init device: %d\n",
1913 /* Really want to call vidioc_s_fmt_vid_cap with the default
1914 * format, but currently the APIs don't join up.
1916 ret = mmal_setup_components(dev, &default_v4l2_format);
1918 v4l2_err(&dev->v4l2_dev, "%s: could not setup components: %d\n",
1923 v4l2_info(&dev->v4l2_dev,
1924 "Broadcom 2835 MMAL video capture ver %s loaded.\n",
1925 BM2835_MMAL_VERSION);
1932 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1933 v4l2_device_unregister(&dev->v4l2_dev);
1939 for (i = 0; i < camera; i++) {
1940 bcm2835_cleanup_instance(gdev[i]);
1945 vchiq_mmal_finalise(instance);
1950 static int bcm2835_mmal_remove(struct platform_device *pdev)
1953 struct vchiq_mmal_instance *instance = gdev[0]->instance;
1955 for (camera = 0; camera < MAX_BCM2835_CAMERAS; camera++) {
1956 bcm2835_cleanup_instance(gdev[camera]);
1957 gdev[camera] = NULL;
1959 vchiq_mmal_finalise(instance);
1964 static struct platform_driver bcm2835_camera_driver = {
1965 .probe = bcm2835_mmal_probe,
1966 .remove = bcm2835_mmal_remove,
1968 .name = "bcm2835-camera",
1972 module_platform_driver(bcm2835_camera_driver)
1974 MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
1975 MODULE_AUTHOR("Vincent Sanders");
1976 MODULE_LICENSE("GPL");
1977 MODULE_VERSION(BM2835_MMAL_VERSION);
1978 MODULE_ALIAS("platform:bcm2835-camera");