1 // SPDX-License-Identifier: GPL-2.0
3 * Broadcom BM2835 V4L2 driver
5 * Copyright © 2013 Raspberry Pi (Trading) Ltd.
7 * Authors: Vincent Sanders @ Collabora
8 * Dave Stevenson @ Broadcom
9 * (now dave.stevenson@raspberrypi.org)
10 * Simon Mellor @ Broadcom
11 * Luke Diamand @ Broadcom
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <media/videobuf2-vmalloc.h>
19 #include <media/videobuf2-dma-contig.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-fh.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-common.h>
26 #include <linux/delay.h>
27 #include <linux/platform_device.h>
29 #include "mmal-common.h"
30 #include "mmal-encodings.h"
31 #include "mmal-vchiq.h"
33 #include "mmal-parameters.h"
34 #include "bcm2835-camera.h"
36 #define BM2835_MMAL_VERSION "0.0.2"
37 #define BM2835_MMAL_MODULE_NAME "bcm2835-v4l2"
40 #define MIN_BUFFER_SIZE (80 * 1024)
42 #define MAX_VIDEO_MODE_WIDTH 1280
43 #define MAX_VIDEO_MODE_HEIGHT 720
45 #define MAX_BCM2835_CAMERAS 2
47 MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
48 MODULE_AUTHOR("Vincent Sanders");
49 MODULE_LICENSE("GPL");
50 MODULE_VERSION(BM2835_MMAL_VERSION);
51 MODULE_ALIAS("platform:bcm2835-camera");
53 int bcm2835_v4l2_debug;
54 module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
55 MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
58 static int video_nr[] = {[0 ... (MAX_BCM2835_CAMERAS - 1)] = UNSET };
59 module_param_array(video_nr, int, NULL, 0644);
60 MODULE_PARM_DESC(video_nr, "videoX start numbers, -1 is autodetect");
62 static int max_video_width = MAX_VIDEO_MODE_WIDTH;
63 static int max_video_height = MAX_VIDEO_MODE_HEIGHT;
64 module_param(max_video_width, int, 0644);
65 MODULE_PARM_DESC(max_video_width, "Threshold for video mode");
66 module_param(max_video_height, int, 0644);
67 MODULE_PARM_DESC(max_video_height, "Threshold for video mode");
69 /* global device data array */
70 static struct bm2835_mmal_dev *gdev[MAX_BCM2835_CAMERAS];
75 /* timeperframe: min/max and default */
76 static const struct v4l2_fract
77 tpf_min = {.numerator = 1, .denominator = FPS_MAX},
78 tpf_max = {.numerator = 1, .denominator = FPS_MIN},
79 tpf_default = {.numerator = 1000, .denominator = 30000};
81 /* Container for MMAL and VB2 buffers*/
82 struct vb2_mmal_buffer {
83 struct vb2_v4l2_buffer vb;
84 struct mmal_buffer mmal;
88 static struct mmal_fmt formats[] = {
90 .name = "4:2:0, planar, YUV",
91 .fourcc = V4L2_PIX_FMT_YUV420,
93 .mmal = MMAL_ENCODING_I420,
95 .mmal_component = COMP_CAMERA,
99 .name = "4:2:2, packed, YUYV",
100 .fourcc = V4L2_PIX_FMT_YUYV,
102 .mmal = MMAL_ENCODING_YUYV,
104 .mmal_component = COMP_CAMERA,
108 .name = "RGB24 (LE)",
109 .fourcc = V4L2_PIX_FMT_RGB24,
111 .mmal = MMAL_ENCODING_RGB24,
113 .mmal_component = COMP_CAMERA,
118 .fourcc = V4L2_PIX_FMT_JPEG,
119 .flags = V4L2_FMT_FLAG_COMPRESSED,
120 .mmal = MMAL_ENCODING_JPEG,
122 .mmal_component = COMP_IMAGE_ENCODE,
127 .fourcc = V4L2_PIX_FMT_H264,
128 .flags = V4L2_FMT_FLAG_COMPRESSED,
129 .mmal = MMAL_ENCODING_H264,
131 .mmal_component = COMP_VIDEO_ENCODE,
136 .fourcc = V4L2_PIX_FMT_MJPEG,
137 .flags = V4L2_FMT_FLAG_COMPRESSED,
138 .mmal = MMAL_ENCODING_MJPEG,
140 .mmal_component = COMP_VIDEO_ENCODE,
144 .name = "4:2:2, packed, YVYU",
145 .fourcc = V4L2_PIX_FMT_YVYU,
147 .mmal = MMAL_ENCODING_YVYU,
149 .mmal_component = COMP_CAMERA,
153 .name = "4:2:2, packed, VYUY",
154 .fourcc = V4L2_PIX_FMT_VYUY,
156 .mmal = MMAL_ENCODING_VYUY,
158 .mmal_component = COMP_CAMERA,
162 .name = "4:2:2, packed, UYVY",
163 .fourcc = V4L2_PIX_FMT_UYVY,
165 .mmal = MMAL_ENCODING_UYVY,
167 .mmal_component = COMP_CAMERA,
171 .name = "4:2:0, planar, NV12",
172 .fourcc = V4L2_PIX_FMT_NV12,
174 .mmal = MMAL_ENCODING_NV12,
176 .mmal_component = COMP_CAMERA,
180 .name = "RGB24 (BE)",
181 .fourcc = V4L2_PIX_FMT_BGR24,
183 .mmal = MMAL_ENCODING_BGR24,
185 .mmal_component = COMP_CAMERA,
189 .name = "4:2:0, planar, YVU",
190 .fourcc = V4L2_PIX_FMT_YVU420,
192 .mmal = MMAL_ENCODING_YV12,
194 .mmal_component = COMP_CAMERA,
198 .name = "4:2:0, planar, NV21",
199 .fourcc = V4L2_PIX_FMT_NV21,
201 .mmal = MMAL_ENCODING_NV21,
203 .mmal_component = COMP_CAMERA,
207 .name = "RGB32 (BE)",
208 .fourcc = V4L2_PIX_FMT_BGR32,
210 .mmal = MMAL_ENCODING_BGRA,
212 .mmal_component = COMP_CAMERA,
218 static struct mmal_fmt *get_format(struct v4l2_format *f)
220 struct mmal_fmt *fmt;
223 for (k = 0; k < ARRAY_SIZE(formats); k++) {
225 if (fmt->fourcc == f->fmt.pix.pixelformat)
232 /* ------------------------------------------------------------------
233 * Videobuf queue operations
234 * ------------------------------------------------------------------
237 static int queue_setup(struct vb2_queue *vq,
238 unsigned int *nbuffers, unsigned int *nplanes,
239 unsigned int sizes[], struct device *alloc_ctxs[])
241 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
244 /* refuse queue setup if port is not configured */
245 if (!dev->capture.port) {
246 v4l2_err(&dev->v4l2_dev,
247 "%s: capture port not configured\n", __func__);
251 /* Handle CREATE_BUFS situation - *nplanes != 0 */
254 sizes[0] < dev->capture.port->current_buffer.size) {
255 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
256 "%s: dev:%p Invalid buffer request from CREATE_BUFS, size %u < %u, nplanes %u != 1\n",
257 __func__, dev, sizes[0],
258 dev->capture.port->current_buffer.size,
266 /* Handle REQBUFS situation */
267 size = dev->capture.port->current_buffer.size;
269 v4l2_err(&dev->v4l2_dev,
270 "%s: capture port buffer size is zero\n", __func__);
274 if (*nbuffers < dev->capture.port->minimum_buffer.num)
275 *nbuffers = dev->capture.port->minimum_buffer.num;
277 dev->capture.port->current_buffer.num = *nbuffers;
284 * videobuf2-vmalloc allocator is context-less so no need to set
288 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
294 static int buffer_init(struct vb2_buffer *vb)
296 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
297 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
298 struct vb2_mmal_buffer *buf =
299 container_of(vb2, struct vb2_mmal_buffer, vb);
301 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
303 buf->mmal.buffer = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
304 buf->mmal.buffer_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
306 return mmal_vchi_buffer_init(dev->instance, &buf->mmal);
309 static int buffer_prepare(struct vb2_buffer *vb)
311 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
314 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
317 if (!dev->capture.port || !dev->capture.fmt)
320 size = dev->capture.stride * dev->capture.height;
321 if (vb2_plane_size(vb, 0) < size) {
322 v4l2_err(&dev->v4l2_dev,
323 "%s data will not fit into plane (%lu < %lu)\n",
324 __func__, vb2_plane_size(vb, 0), size);
331 static void buffer_cleanup(struct vb2_buffer *vb)
333 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
334 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
335 struct vb2_mmal_buffer *buf =
336 container_of(vb2, struct vb2_mmal_buffer, vb);
338 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
341 mmal_vchi_buffer_cleanup(&buf->mmal);
344 static inline bool is_capturing(struct bm2835_mmal_dev *dev)
346 return dev->capture.camera_port ==
347 &dev->component[COMP_CAMERA]->output[CAM_PORT_CAPTURE];
350 static void buffer_cb(struct vchiq_mmal_instance *instance,
351 struct vchiq_mmal_port *port,
353 struct mmal_buffer *mmal_buf)
355 struct bm2835_mmal_dev *dev = port->cb_ctx;
356 struct vb2_mmal_buffer *buf =
357 container_of(mmal_buf, struct vb2_mmal_buffer, mmal);
359 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
360 "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
361 __func__, status, buf, mmal_buf->length, mmal_buf->mmal_flags,
365 /* error in transfer */
367 /* there was a buffer with the error so return it */
368 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
373 if (mmal_buf->length == 0) {
375 if (dev->capture.frame_count) {
376 /* empty buffer whilst capturing - expected to be an
377 * EOS, so grab another frame
379 if (is_capturing(dev)) {
380 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
381 "Grab another frame");
382 vchiq_mmal_port_parameter_set(
384 dev->capture.camera_port,
385 MMAL_PARAMETER_CAPTURE,
386 &dev->capture.frame_count,
387 sizeof(dev->capture.frame_count));
389 if (vchiq_mmal_submit_buffer(instance, port,
391 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
392 "Failed to return EOS buffer");
394 /* stopping streaming.
395 * return buffer, and signal frame completion
397 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
398 complete(&dev->capture.frame_cmplt);
403 if (!dev->capture.frame_count) {
404 /* signal frame completion */
405 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
406 complete(&dev->capture.frame_cmplt);
410 if (dev->capture.vc_start_timestamp == -1) {
412 * VPU doesn't support MMAL_PARAMETER_SYSTEM_TIME, rely on
413 * kernel time, and have no latency compensation.
415 buf->vb.vb2_buf.timestamp = ktime_get_ns();
416 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
417 "Buffer time set as current time - %lld",
418 buf->vb.vb2_buf.timestamp);
419 } else if (mmal_buf->pts != 0) {
421 s64 runtime_us = mmal_buf->pts -
422 dev->capture.vc_start_timestamp;
423 timestamp = ktime_add_us(dev->capture.kernel_start_ts,
425 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
426 "Convert start time %llu and %llu with offset %llu to %llu\n",
427 ktime_to_ns(dev->capture.kernel_start_ts),
428 dev->capture.vc_start_timestamp, mmal_buf->pts,
429 ktime_to_ns(timestamp));
430 if (timestamp < dev->capture.last_timestamp) {
431 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
432 "Negative delta - using last time\n");
433 timestamp = dev->capture.last_timestamp;
435 buf->vb.vb2_buf.timestamp = ktime_to_ns(timestamp);
437 if (dev->capture.last_timestamp) {
438 buf->vb.vb2_buf.timestamp = dev->capture.last_timestamp;
439 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
440 "Buffer time set as last timestamp - %lld",
441 buf->vb.vb2_buf.timestamp);
443 buf->vb.vb2_buf.timestamp =
444 ktime_to_ns(dev->capture.kernel_start_ts);
445 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
446 "Buffer time set as start timestamp - %lld",
447 buf->vb.vb2_buf.timestamp);
450 dev->capture.last_timestamp = buf->vb.vb2_buf.timestamp;
451 buf->vb.sequence = dev->capture.sequence++;
452 buf->vb.field = V4L2_FIELD_NONE;
454 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, mmal_buf->length);
455 if (mmal_buf->mmal_flags & MMAL_BUFFER_HEADER_FLAG_KEYFRAME)
456 buf->vb.flags |= V4L2_BUF_FLAG_KEYFRAME;
458 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
459 "Buffer has ts %llu", dev->capture.last_timestamp);
460 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
462 if (mmal_buf->mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
464 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
465 "Grab another frame as buffer has EOS");
466 vchiq_mmal_port_parameter_set(instance,
467 dev->capture.camera_port,
468 MMAL_PARAMETER_CAPTURE,
469 &dev->capture.frame_count,
470 sizeof(dev->capture.frame_count));
474 static int enable_camera(struct bm2835_mmal_dev *dev)
478 if (!dev->camera_use_count) {
479 ret = vchiq_mmal_port_parameter_set(
481 &dev->component[COMP_CAMERA]->control,
482 MMAL_PARAMETER_CAMERA_NUM, &dev->camera_num,
483 sizeof(dev->camera_num));
485 v4l2_err(&dev->v4l2_dev,
486 "Failed setting camera num, ret %d\n", ret);
490 ret = vchiq_mmal_component_enable(
492 dev->component[COMP_CAMERA]);
494 v4l2_err(&dev->v4l2_dev,
495 "Failed enabling camera, ret %d\n", ret);
499 dev->camera_use_count++;
500 v4l2_dbg(1, bcm2835_v4l2_debug,
501 &dev->v4l2_dev, "enabled camera (refcount %d)\n",
502 dev->camera_use_count);
506 static int disable_camera(struct bm2835_mmal_dev *dev)
510 if (!dev->camera_use_count) {
511 v4l2_err(&dev->v4l2_dev,
512 "Disabled the camera when already disabled\n");
515 dev->camera_use_count--;
516 if (!dev->camera_use_count) {
517 unsigned int i = 0xFFFFFFFF;
519 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
520 "Disabling camera\n");
522 vchiq_mmal_component_disable(
524 dev->component[COMP_CAMERA]);
526 v4l2_err(&dev->v4l2_dev,
527 "Failed disabling camera, ret %d\n", ret);
530 vchiq_mmal_port_parameter_set(
532 &dev->component[COMP_CAMERA]->control,
533 MMAL_PARAMETER_CAMERA_NUM, &i,
536 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
537 "Camera refcount now %d\n", dev->camera_use_count);
541 static void buffer_queue(struct vb2_buffer *vb)
543 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
544 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
545 struct vb2_mmal_buffer *buf =
546 container_of(vb2, struct vb2_mmal_buffer, vb);
549 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
550 "%s: dev:%p buf:%p, idx %u\n",
551 __func__, dev, buf, vb2->vb2_buf.index);
553 ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port,
556 v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
560 static int start_streaming(struct vb2_queue *vq, unsigned int count)
562 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
566 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
569 /* ensure a format has actually been set */
570 if (!dev->capture.port)
573 if (enable_camera(dev) < 0) {
574 v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
578 /*init_completion(&dev->capture.frame_cmplt); */
580 /* enable frame capture */
581 dev->capture.frame_count = 1;
583 /* reset sequence number */
584 dev->capture.sequence = 0;
586 /* if the preview is not already running, wait for a few frames for AGC
589 if (!dev->component[COMP_PREVIEW]->enabled)
592 /* enable the connection from camera to encoder (if applicable) */
593 if (dev->capture.camera_port != dev->capture.port &&
594 dev->capture.camera_port) {
595 ret = vchiq_mmal_port_enable(dev->instance,
596 dev->capture.camera_port, NULL);
598 v4l2_err(&dev->v4l2_dev,
599 "Failed to enable encode tunnel - error %d\n",
605 /* Get VC timestamp at this point in time */
606 parameter_size = sizeof(dev->capture.vc_start_timestamp);
607 if (vchiq_mmal_port_parameter_get(dev->instance,
608 dev->capture.camera_port,
609 MMAL_PARAMETER_SYSTEM_TIME,
610 &dev->capture.vc_start_timestamp,
612 v4l2_err(&dev->v4l2_dev,
613 "Failed to get VC start time - update your VC f/w\n");
615 /* Flag to indicate just to rely on kernel timestamps */
616 dev->capture.vc_start_timestamp = -1;
618 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
619 "Start time %lld size %d\n",
620 dev->capture.vc_start_timestamp, parameter_size);
623 dev->capture.kernel_start_ts = ktime_get();
624 dev->capture.last_timestamp = 0;
626 /* enable the camera port */
627 dev->capture.port->cb_ctx = dev;
629 vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
631 v4l2_err(&dev->v4l2_dev,
632 "Failed to enable capture port - error %d. Disabling camera port again\n",
635 vchiq_mmal_port_disable(dev->instance,
636 dev->capture.camera_port);
637 if (disable_camera(dev) < 0) {
638 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
644 /* capture the first frame */
645 vchiq_mmal_port_parameter_set(dev->instance,
646 dev->capture.camera_port,
647 MMAL_PARAMETER_CAPTURE,
648 &dev->capture.frame_count,
649 sizeof(dev->capture.frame_count));
653 /* abort streaming and wait for last buffer */
654 static void stop_streaming(struct vb2_queue *vq)
657 unsigned long timeout;
658 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
659 struct vchiq_mmal_port *port = dev->capture.port;
661 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
664 init_completion(&dev->capture.frame_cmplt);
665 dev->capture.frame_count = 0;
667 /* ensure a format has actually been set */
669 v4l2_err(&dev->v4l2_dev,
670 "no capture port - stream not started?\n");
674 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
676 /* stop capturing frames */
677 vchiq_mmal_port_parameter_set(dev->instance,
678 dev->capture.camera_port,
679 MMAL_PARAMETER_CAPTURE,
680 &dev->capture.frame_count,
681 sizeof(dev->capture.frame_count));
683 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
684 "disabling connection\n");
686 /* disable the connection from camera to encoder */
687 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
688 if (!ret && dev->capture.camera_port != port) {
689 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
691 ret = vchiq_mmal_port_disable(dev->instance, port);
692 } else if (dev->capture.camera_port != port) {
693 v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
697 /* wait for all buffers to be returned */
698 while (atomic_read(&port->buffers_with_vpu)) {
699 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
700 "%s: Waiting for buffers to be returned - %d outstanding\n",
701 __func__, atomic_read(&port->buffers_with_vpu));
702 timeout = wait_for_completion_timeout(&dev->capture.frame_cmplt,
705 v4l2_err(&dev->v4l2_dev, "%s: Timeout waiting for buffers to be returned - %d outstanding\n",
707 atomic_read(&port->buffers_with_vpu));
712 if (disable_camera(dev) < 0)
713 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
716 static const struct vb2_ops bm2835_mmal_video_qops = {
717 .queue_setup = queue_setup,
718 .buf_init = buffer_init,
719 .buf_prepare = buffer_prepare,
720 .buf_cleanup = buffer_cleanup,
721 .buf_queue = buffer_queue,
722 .start_streaming = start_streaming,
723 .stop_streaming = stop_streaming,
724 .wait_prepare = vb2_ops_wait_prepare,
725 .wait_finish = vb2_ops_wait_finish,
728 /* ------------------------------------------------------------------
730 * ------------------------------------------------------------------
733 static int set_overlay_params(struct bm2835_mmal_dev *dev,
734 struct vchiq_mmal_port *port)
736 struct mmal_parameter_displayregion prev_config = {
737 .set = MMAL_DISPLAY_SET_LAYER |
738 MMAL_DISPLAY_SET_ALPHA |
739 MMAL_DISPLAY_SET_DEST_RECT |
740 MMAL_DISPLAY_SET_FULLSCREEN,
741 .layer = PREVIEW_LAYER,
742 .alpha = dev->overlay.global_alpha,
745 .x = dev->overlay.w.left,
746 .y = dev->overlay.w.top,
747 .width = dev->overlay.w.width,
748 .height = dev->overlay.w.height,
751 return vchiq_mmal_port_parameter_set(dev->instance, port,
752 MMAL_PARAMETER_DISPLAYREGION,
753 &prev_config, sizeof(prev_config));
757 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
758 struct v4l2_fmtdesc *f)
760 struct mmal_fmt *fmt;
762 if (f->index >= ARRAY_SIZE(formats))
765 fmt = &formats[f->index];
767 strlcpy((char *)f->description, fmt->name, sizeof(f->description));
768 f->pixelformat = fmt->fourcc;
769 f->flags = fmt->flags;
774 static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
775 struct v4l2_format *f)
777 struct bm2835_mmal_dev *dev = video_drvdata(file);
779 f->fmt.win = dev->overlay;
784 static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
785 struct v4l2_format *f)
787 struct bm2835_mmal_dev *dev = video_drvdata(file);
789 f->fmt.win.field = V4L2_FIELD_NONE;
790 f->fmt.win.chromakey = 0;
791 f->fmt.win.clips = NULL;
792 f->fmt.win.clipcount = 0;
793 f->fmt.win.bitmap = NULL;
795 v4l_bound_align_image(&f->fmt.win.w.width, MIN_WIDTH, dev->max_width, 1,
796 &f->fmt.win.w.height, MIN_HEIGHT, dev->max_height,
798 v4l_bound_align_image(&f->fmt.win.w.left, MIN_WIDTH, dev->max_width, 1,
799 &f->fmt.win.w.top, MIN_HEIGHT, dev->max_height,
802 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
803 "Overlay: Now w/h %dx%d l/t %dx%d\n",
804 f->fmt.win.w.width, f->fmt.win.w.height,
805 f->fmt.win.w.left, f->fmt.win.w.top);
807 v4l2_dump_win_format(1,
815 static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
816 struct v4l2_format *f)
818 struct bm2835_mmal_dev *dev = video_drvdata(file);
820 vidioc_try_fmt_vid_overlay(file, priv, f);
822 dev->overlay = f->fmt.win;
823 if (dev->component[COMP_PREVIEW]->enabled) {
824 set_overlay_params(dev,
825 &dev->component[COMP_PREVIEW]->input[0]);
831 static int vidioc_overlay(struct file *file, void *f, unsigned int on)
834 struct bm2835_mmal_dev *dev = video_drvdata(file);
835 struct vchiq_mmal_port *src;
836 struct vchiq_mmal_port *dst;
838 if ((on && dev->component[COMP_PREVIEW]->enabled) ||
839 (!on && !dev->component[COMP_PREVIEW]->enabled))
840 return 0; /* already in requested state */
843 &dev->component[COMP_CAMERA]->output[CAM_PORT_PREVIEW];
846 /* disconnect preview ports and disable component */
847 ret = vchiq_mmal_port_disable(dev->instance, src);
850 vchiq_mmal_port_connect_tunnel(dev->instance, src,
853 ret = vchiq_mmal_component_disable(
855 dev->component[COMP_PREVIEW]);
861 /* set preview port format and connect it to output */
862 dst = &dev->component[COMP_PREVIEW]->input[0];
864 ret = vchiq_mmal_port_set_format(dev->instance, src);
868 ret = set_overlay_params(dev, dst);
872 if (enable_camera(dev) < 0) {
877 ret = vchiq_mmal_component_enable(
879 dev->component[COMP_PREVIEW]);
883 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
885 ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
887 ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
892 static int vidioc_g_fbuf(struct file *file, void *fh,
893 struct v4l2_framebuffer *a)
895 /* The video overlay must stay within the framebuffer and can't be
896 * positioned independently.
898 struct bm2835_mmal_dev *dev = video_drvdata(file);
899 struct vchiq_mmal_port *preview_port =
900 &dev->component[COMP_CAMERA]->output[CAM_PORT_PREVIEW];
902 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
903 V4L2_FBUF_CAP_GLOBAL_ALPHA;
904 a->flags = V4L2_FBUF_FLAG_OVERLAY;
905 a->fmt.width = preview_port->es.video.width;
906 a->fmt.height = preview_port->es.video.height;
907 a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
908 a->fmt.bytesperline = preview_port->es.video.width;
909 a->fmt.sizeimage = (preview_port->es.video.width *
910 preview_port->es.video.height * 3) >> 1;
911 a->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
917 static int vidioc_enum_input(struct file *file, void *priv,
918 struct v4l2_input *inp)
920 /* only a single camera input */
924 inp->type = V4L2_INPUT_TYPE_CAMERA;
925 sprintf((char *)inp->name, "Camera %u", inp->index);
929 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
935 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
944 static int vidioc_querycap(struct file *file, void *priv,
945 struct v4l2_capability *cap)
947 struct bm2835_mmal_dev *dev = video_drvdata(file);
951 vchiq_mmal_version(dev->instance, &major, &minor);
953 strcpy((char *)cap->driver, "bm2835 mmal");
954 snprintf((char *)cap->card, sizeof(cap->card), "mmal service %d.%d",
957 snprintf((char *)cap->bus_info, sizeof(cap->bus_info),
958 "platform:%s", dev->v4l2_dev.name);
959 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
960 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
961 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
966 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
967 struct v4l2_fmtdesc *f)
969 struct mmal_fmt *fmt;
971 if (f->index >= ARRAY_SIZE(formats))
974 fmt = &formats[f->index];
976 strlcpy((char *)f->description, fmt->name, sizeof(f->description));
977 f->pixelformat = fmt->fourcc;
978 f->flags = fmt->flags;
983 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
984 struct v4l2_format *f)
986 struct bm2835_mmal_dev *dev = video_drvdata(file);
988 f->fmt.pix.width = dev->capture.width;
989 f->fmt.pix.height = dev->capture.height;
990 f->fmt.pix.field = V4L2_FIELD_NONE;
991 f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
992 f->fmt.pix.bytesperline = dev->capture.stride;
993 f->fmt.pix.sizeimage = dev->capture.buffersize;
995 if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
996 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
997 else if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG)
998 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
1000 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1001 f->fmt.pix.priv = 0;
1003 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
1008 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1009 struct v4l2_format *f)
1011 struct bm2835_mmal_dev *dev = video_drvdata(file);
1012 struct mmal_fmt *mfmt;
1014 mfmt = get_format(f);
1016 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1017 "Fourcc format (0x%08x) unknown.\n",
1018 f->fmt.pix.pixelformat);
1019 f->fmt.pix.pixelformat = formats[0].fourcc;
1020 mfmt = get_format(f);
1023 f->fmt.pix.field = V4L2_FIELD_NONE;
1025 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1026 "Clipping/aligning %dx%d format %08X\n",
1027 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
1029 v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, dev->max_width, 1,
1030 &f->fmt.pix.height, MIN_HEIGHT, dev->max_height,
1032 f->fmt.pix.bytesperline = f->fmt.pix.width * mfmt->ybbp;
1033 if (!mfmt->remove_padding) {
1034 if (mfmt->depth == 24) {
1036 * 24bpp is a pain as we can't use simple masking.
1037 * Min stride is width aligned to 16, times 24bpp.
1039 f->fmt.pix.bytesperline =
1040 ((f->fmt.pix.width + 15) & ~15) * 3;
1043 * GPU isn't removing padding, so stride is aligned to
1046 int align_mask = ((32 * mfmt->depth) >> 3) - 1;
1048 f->fmt.pix.bytesperline =
1049 (f->fmt.pix.bytesperline + align_mask) &
1052 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1053 "Not removing padding, so bytes/line = %d\n",
1054 f->fmt.pix.bytesperline);
1057 /* Image buffer has to be padded to allow for alignment, even though
1058 * we sometimes then remove that padding before delivering the buffer.
1060 f->fmt.pix.sizeimage = ((f->fmt.pix.height + 15) & ~15) *
1061 (((f->fmt.pix.width + 31) & ~31) * mfmt->depth) >> 3;
1063 if ((mfmt->flags & V4L2_FMT_FLAG_COMPRESSED) &&
1064 f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
1065 f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
1067 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24)
1068 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1069 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
1070 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
1072 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1073 f->fmt.pix.priv = 0;
1075 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1076 "Now %dx%d format %08X\n",
1077 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
1079 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
1084 static int mmal_setup_components(struct bm2835_mmal_dev *dev,
1085 struct v4l2_format *f)
1088 struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
1089 struct vchiq_mmal_component *encode_component = NULL;
1090 struct mmal_fmt *mfmt = get_format(f);
1096 if (dev->capture.encode_component) {
1097 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1098 "vid_cap - disconnect previous tunnel\n");
1100 /* Disconnect any previous connection */
1101 vchiq_mmal_port_connect_tunnel(dev->instance,
1102 dev->capture.camera_port, NULL);
1103 dev->capture.camera_port = NULL;
1104 ret = vchiq_mmal_component_disable(dev->instance,
1105 dev->capture.encode_component);
1107 v4l2_err(&dev->v4l2_dev,
1108 "Failed to disable encode component %d\n",
1111 dev->capture.encode_component = NULL;
1113 /* format dependent port setup */
1114 switch (mfmt->mmal_component) {
1116 /* Make a further decision on port based on resolution */
1117 if (f->fmt.pix.width <= max_video_width &&
1118 f->fmt.pix.height <= max_video_height)
1120 &dev->component[COMP_CAMERA]->output[CAM_PORT_VIDEO];
1123 &dev->component[COMP_CAMERA]->output[CAM_PORT_CAPTURE];
1126 case COMP_IMAGE_ENCODE:
1127 encode_component = dev->component[COMP_IMAGE_ENCODE];
1128 port = &dev->component[COMP_IMAGE_ENCODE]->output[0];
1130 &dev->component[COMP_CAMERA]->output[CAM_PORT_CAPTURE];
1132 case COMP_VIDEO_ENCODE:
1133 encode_component = dev->component[COMP_VIDEO_ENCODE];
1134 port = &dev->component[COMP_VIDEO_ENCODE]->output[0];
1136 &dev->component[COMP_CAMERA]->output[CAM_PORT_VIDEO];
1145 if (encode_component)
1146 camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
1148 camera_port->format.encoding = mfmt->mmal;
1150 if (dev->rgb_bgr_swapped) {
1151 if (camera_port->format.encoding == MMAL_ENCODING_RGB24)
1152 camera_port->format.encoding = MMAL_ENCODING_BGR24;
1153 else if (camera_port->format.encoding == MMAL_ENCODING_BGR24)
1154 camera_port->format.encoding = MMAL_ENCODING_RGB24;
1157 remove_padding = mfmt->remove_padding;
1158 vchiq_mmal_port_parameter_set(dev->instance,
1160 MMAL_PARAMETER_NO_IMAGE_PADDING,
1161 &remove_padding, sizeof(remove_padding));
1163 camera_port->format.encoding_variant = 0;
1164 camera_port->es.video.width = f->fmt.pix.width;
1165 camera_port->es.video.height = f->fmt.pix.height;
1166 camera_port->es.video.crop.x = 0;
1167 camera_port->es.video.crop.y = 0;
1168 camera_port->es.video.crop.width = f->fmt.pix.width;
1169 camera_port->es.video.crop.height = f->fmt.pix.height;
1170 camera_port->es.video.frame_rate.num = 0;
1171 camera_port->es.video.frame_rate.den = 1;
1172 camera_port->es.video.color_space = MMAL_COLOR_SPACE_JPEG_JFIF;
1174 ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
1178 &dev->component[COMP_CAMERA]->output[CAM_PORT_VIDEO]) {
1179 bool overlay_enabled =
1180 !!dev->component[COMP_PREVIEW]->enabled;
1181 struct vchiq_mmal_port *preview_port =
1182 &dev->component[COMP_CAMERA]->output[CAM_PORT_PREVIEW];
1183 /* Preview and encode ports need to match on resolution */
1184 if (overlay_enabled) {
1185 /* Need to disable the overlay before we can update
1189 vchiq_mmal_port_disable(dev->instance,
1193 vchiq_mmal_port_connect_tunnel(
1198 preview_port->es.video.width = f->fmt.pix.width;
1199 preview_port->es.video.height = f->fmt.pix.height;
1200 preview_port->es.video.crop.x = 0;
1201 preview_port->es.video.crop.y = 0;
1202 preview_port->es.video.crop.width = f->fmt.pix.width;
1203 preview_port->es.video.crop.height = f->fmt.pix.height;
1204 preview_port->es.video.frame_rate.num =
1205 dev->capture.timeperframe.denominator;
1206 preview_port->es.video.frame_rate.den =
1207 dev->capture.timeperframe.numerator;
1208 ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
1209 if (overlay_enabled) {
1210 ret = vchiq_mmal_port_connect_tunnel(
1213 &dev->component[COMP_PREVIEW]->input[0]);
1215 ret = vchiq_mmal_port_enable(dev->instance,
1222 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1223 "%s failed to set format %dx%d %08X\n", __func__,
1224 f->fmt.pix.width, f->fmt.pix.height,
1225 f->fmt.pix.pixelformat);
1226 /* ensure capture is not going to be tried */
1227 dev->capture.port = NULL;
1229 if (encode_component) {
1230 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1231 "vid_cap - set up encode comp\n");
1233 /* configure buffering */
1234 camera_port->current_buffer.size =
1235 camera_port->recommended_buffer.size;
1236 camera_port->current_buffer.num =
1237 camera_port->recommended_buffer.num;
1240 vchiq_mmal_port_connect_tunnel(
1243 &encode_component->input[0]);
1245 v4l2_dbg(1, bcm2835_v4l2_debug,
1247 "%s failed to create connection\n",
1249 /* ensure capture is not going to be tried */
1250 dev->capture.port = NULL;
1252 port->es.video.width = f->fmt.pix.width;
1253 port->es.video.height = f->fmt.pix.height;
1254 port->es.video.crop.x = 0;
1255 port->es.video.crop.y = 0;
1256 port->es.video.crop.width = f->fmt.pix.width;
1257 port->es.video.crop.height = f->fmt.pix.height;
1258 port->es.video.frame_rate.num =
1259 dev->capture.timeperframe.denominator;
1260 port->es.video.frame_rate.den =
1261 dev->capture.timeperframe.numerator;
1263 port->format.encoding = mfmt->mmal;
1264 port->format.encoding_variant = 0;
1265 /* Set any encoding specific parameters */
1266 switch (mfmt->mmal_component) {
1267 case COMP_VIDEO_ENCODE:
1268 port->format.bitrate =
1269 dev->capture.encode_bitrate;
1271 case COMP_IMAGE_ENCODE:
1272 /* Could set EXIF parameters here */
1277 ret = vchiq_mmal_port_set_format(dev->instance,
1280 v4l2_dbg(1, bcm2835_v4l2_debug,
1282 "%s failed to set format %dx%d fmt %08X\n",
1286 f->fmt.pix.pixelformat
1291 ret = vchiq_mmal_component_enable(
1295 v4l2_dbg(1, bcm2835_v4l2_debug,
1297 "%s Failed to enable encode components\n",
1302 /* configure buffering */
1303 port->current_buffer.num = 1;
1304 port->current_buffer.size =
1305 f->fmt.pix.sizeimage;
1306 if (port->format.encoding ==
1307 MMAL_ENCODING_JPEG) {
1308 v4l2_dbg(1, bcm2835_v4l2_debug,
1310 "JPG - buf size now %d was %d\n",
1311 f->fmt.pix.sizeimage,
1312 port->current_buffer.size);
1313 port->current_buffer.size =
1314 (f->fmt.pix.sizeimage <
1316 (100 << 10) : f->fmt.pix.sizeimage;
1318 v4l2_dbg(1, bcm2835_v4l2_debug,
1320 "vid_cap - cur_buf.size set to %d\n",
1321 f->fmt.pix.sizeimage);
1322 port->current_buffer.alignment = 0;
1325 /* configure buffering */
1326 camera_port->current_buffer.num = 1;
1327 camera_port->current_buffer.size = f->fmt.pix.sizeimage;
1328 camera_port->current_buffer.alignment = 0;
1332 dev->capture.fmt = mfmt;
1333 dev->capture.stride = f->fmt.pix.bytesperline;
1334 dev->capture.width = camera_port->es.video.crop.width;
1335 dev->capture.height = camera_port->es.video.crop.height;
1336 dev->capture.buffersize = port->current_buffer.size;
1338 /* select port for capture */
1339 dev->capture.port = port;
1340 dev->capture.camera_port = camera_port;
1341 dev->capture.encode_component = encode_component;
1342 v4l2_dbg(1, bcm2835_v4l2_debug,
1344 "Set dev->capture.fmt %08X, %dx%d, stride %d, size %d",
1345 port->format.encoding,
1346 dev->capture.width, dev->capture.height,
1347 dev->capture.stride, dev->capture.buffersize);
1351 /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
1355 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1356 struct v4l2_format *f)
1359 struct bm2835_mmal_dev *dev = video_drvdata(file);
1360 struct mmal_fmt *mfmt;
1362 /* try the format to set valid parameters */
1363 ret = vidioc_try_fmt_vid_cap(file, priv, f);
1365 v4l2_err(&dev->v4l2_dev,
1366 "vid_cap - vidioc_try_fmt_vid_cap failed\n");
1370 /* if a capture is running refuse to set format */
1371 if (vb2_is_busy(&dev->capture.vb_vidq)) {
1372 v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
1376 /* If the format is unsupported v4l2 says we should switch to
1377 * a supported one and not return an error.
1379 mfmt = get_format(f);
1381 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1382 "Fourcc format (0x%08x) unknown.\n",
1383 f->fmt.pix.pixelformat);
1384 f->fmt.pix.pixelformat = formats[0].fourcc;
1385 mfmt = get_format(f);
1388 ret = mmal_setup_components(dev, f);
1390 v4l2_err(&dev->v4l2_dev,
1391 "%s: failed to setup mmal components: %d\n",
1399 static int vidioc_enum_framesizes(struct file *file, void *fh,
1400 struct v4l2_frmsizeenum *fsize)
1402 struct bm2835_mmal_dev *dev = video_drvdata(file);
1403 static const struct v4l2_frmsize_stepwise sizes = {
1411 for (i = 0; i < ARRAY_SIZE(formats); i++)
1412 if (formats[i].fourcc == fsize->pixel_format)
1414 if (i == ARRAY_SIZE(formats))
1416 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1417 fsize->stepwise = sizes;
1418 fsize->stepwise.max_width = dev->max_width;
1419 fsize->stepwise.max_height = dev->max_height;
1423 /* timeperframe is arbitrary and continuous */
1424 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1425 struct v4l2_frmivalenum *fival)
1427 struct bm2835_mmal_dev *dev = video_drvdata(file);
1433 for (i = 0; i < ARRAY_SIZE(formats); i++)
1434 if (formats[i].fourcc == fival->pixel_format)
1436 if (i == ARRAY_SIZE(formats))
1439 /* regarding width & height - we support any within range */
1440 if (fival->width < MIN_WIDTH || fival->width > dev->max_width ||
1441 fival->height < MIN_HEIGHT || fival->height > dev->max_height)
1444 fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1446 /* fill in stepwise (step=1.0 is required by V4L2 spec) */
1447 fival->stepwise.min = tpf_min;
1448 fival->stepwise.max = tpf_max;
1449 fival->stepwise.step = (struct v4l2_fract) {1, 1};
1454 static int vidioc_g_parm(struct file *file, void *priv,
1455 struct v4l2_streamparm *parm)
1457 struct bm2835_mmal_dev *dev = video_drvdata(file);
1459 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1462 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1463 parm->parm.capture.timeperframe = dev->capture.timeperframe;
1464 parm->parm.capture.readbuffers = 1;
1468 #define FRACT_CMP(a, OP, b) \
1469 ((u64)(a).numerator * (b).denominator OP \
1470 (u64)(b).numerator * (a).denominator)
1472 static int vidioc_s_parm(struct file *file, void *priv,
1473 struct v4l2_streamparm *parm)
1475 struct bm2835_mmal_dev *dev = video_drvdata(file);
1476 struct v4l2_fract tpf;
1478 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1481 tpf = parm->parm.capture.timeperframe;
1483 /* tpf: {*, 0} resets timing; clip to [min, max]*/
1484 tpf = tpf.denominator ? tpf : tpf_default;
1485 tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1486 tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1488 dev->capture.timeperframe = tpf;
1489 parm->parm.capture.timeperframe = tpf;
1490 parm->parm.capture.readbuffers = 1;
1491 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1493 set_framerate_params(dev);
1498 static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
1500 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1501 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1502 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1503 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1504 .vidioc_overlay = vidioc_overlay,
1505 .vidioc_g_fbuf = vidioc_g_fbuf,
1508 .vidioc_enum_input = vidioc_enum_input,
1509 .vidioc_g_input = vidioc_g_input,
1510 .vidioc_s_input = vidioc_s_input,
1513 .vidioc_querycap = vidioc_querycap,
1514 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1515 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1516 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1517 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1519 /* buffer management */
1520 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1521 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1522 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1523 .vidioc_querybuf = vb2_ioctl_querybuf,
1524 .vidioc_qbuf = vb2_ioctl_qbuf,
1525 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1526 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1527 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1528 .vidioc_g_parm = vidioc_g_parm,
1529 .vidioc_s_parm = vidioc_s_parm,
1530 .vidioc_streamon = vb2_ioctl_streamon,
1531 .vidioc_streamoff = vb2_ioctl_streamoff,
1533 .vidioc_log_status = v4l2_ctrl_log_status,
1534 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1535 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1538 /* ------------------------------------------------------------------
1539 * Driver init/finalise
1540 * ------------------------------------------------------------------
1543 static const struct v4l2_file_operations camera0_fops = {
1544 .owner = THIS_MODULE,
1545 .open = v4l2_fh_open,
1546 .release = vb2_fop_release,
1547 .read = vb2_fop_read,
1548 .poll = vb2_fop_poll,
1549 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1550 .mmap = vb2_fop_mmap,
1553 static const struct video_device vdev_template = {
1555 .fops = &camera0_fops,
1556 .ioctl_ops = &camera0_ioctl_ops,
1557 .release = video_device_release_empty,
1560 /* Returns the number of cameras, and also the max resolution supported
1563 static int get_num_cameras(struct vchiq_mmal_instance *instance,
1564 unsigned int resolutions[][2], int num_resolutions)
1567 struct vchiq_mmal_component *cam_info_component;
1568 struct mmal_parameter_camera_info_t cam_info = {0};
1569 u32 param_size = sizeof(cam_info);
1572 /* create a camera_info component */
1573 ret = vchiq_mmal_component_init(instance, "camera_info",
1574 &cam_info_component);
1576 /* Unusual failure - let's guess one camera. */
1579 if (vchiq_mmal_port_parameter_get(instance,
1580 &cam_info_component->control,
1581 MMAL_PARAMETER_CAMERA_INFO,
1584 pr_info("Failed to get camera info\n");
1587 i < min_t(unsigned int, cam_info.num_cameras, num_resolutions);
1589 resolutions[i][0] = cam_info.cameras[i].max_width;
1590 resolutions[i][1] = cam_info.cameras[i].max_height;
1593 vchiq_mmal_component_finalise(instance,
1594 cam_info_component);
1596 return cam_info.num_cameras;
1599 static int set_camera_parameters(struct vchiq_mmal_instance *instance,
1600 struct vchiq_mmal_component *camera,
1601 struct bm2835_mmal_dev *dev)
1604 struct mmal_parameter_camera_config cam_config = {
1605 .max_stills_w = dev->max_width,
1606 .max_stills_h = dev->max_height,
1608 .one_shot_stills = 1,
1609 .max_preview_video_w = (max_video_width > 1920) ?
1610 max_video_width : 1920,
1611 .max_preview_video_h = (max_video_height > 1088) ?
1612 max_video_height : 1088,
1613 .num_preview_video_frames = 3,
1614 .stills_capture_circular_buffer_height = 0,
1615 .fast_preview_resume = 0,
1616 .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
1619 ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
1620 MMAL_PARAMETER_CAMERA_CONFIG,
1621 &cam_config, sizeof(cam_config));
1625 #define MAX_SUPPORTED_ENCODINGS 20
1627 /* MMAL instance and component init */
1628 static int mmal_init(struct bm2835_mmal_dev *dev)
1631 struct mmal_es_format_local *format;
1632 u32 supported_encodings[MAX_SUPPORTED_ENCODINGS];
1634 struct vchiq_mmal_component *camera;
1636 ret = vchiq_mmal_init(&dev->instance);
1638 v4l2_err(&dev->v4l2_dev, "%s: vchiq mmal init failed %d\n",
1643 /* get the camera component ready */
1644 ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
1645 &dev->component[COMP_CAMERA]);
1649 camera = dev->component[COMP_CAMERA];
1650 if (camera->outputs < CAM_PORT_COUNT) {
1651 v4l2_err(&dev->v4l2_dev, "%s: too few camera outputs %d needed %d\n",
1652 __func__, camera->outputs, CAM_PORT_COUNT);
1657 ret = set_camera_parameters(dev->instance,
1661 v4l2_err(&dev->v4l2_dev, "%s: unable to set camera parameters: %d\n",
1666 /* There was an error in the firmware that meant the camera component
1667 * produced BGR instead of RGB.
1668 * This is now fixed, but in order to support the old firmwares, we
1671 dev->rgb_bgr_swapped = true;
1672 param_size = sizeof(supported_encodings);
1673 ret = vchiq_mmal_port_parameter_get(dev->instance,
1674 &camera->output[CAM_PORT_CAPTURE],
1675 MMAL_PARAMETER_SUPPORTED_ENCODINGS,
1676 &supported_encodings,
1681 for (i = 0; i < param_size / sizeof(u32); i++) {
1682 if (supported_encodings[i] == MMAL_ENCODING_BGR24) {
1683 /* Found BGR24 first - old firmware. */
1686 if (supported_encodings[i] == MMAL_ENCODING_RGB24) {
1687 /* Found RGB24 first
1688 * new firmware, so use RGB24.
1690 dev->rgb_bgr_swapped = false;
1695 format = &camera->output[CAM_PORT_PREVIEW].format;
1697 format->encoding = MMAL_ENCODING_OPAQUE;
1698 format->encoding_variant = MMAL_ENCODING_I420;
1700 format->es->video.width = 1024;
1701 format->es->video.height = 768;
1702 format->es->video.crop.x = 0;
1703 format->es->video.crop.y = 0;
1704 format->es->video.crop.width = 1024;
1705 format->es->video.crop.height = 768;
1706 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1707 format->es->video.frame_rate.den = 1;
1709 format = &camera->output[CAM_PORT_VIDEO].format;
1711 format->encoding = MMAL_ENCODING_OPAQUE;
1712 format->encoding_variant = MMAL_ENCODING_I420;
1714 format->es->video.width = 1024;
1715 format->es->video.height = 768;
1716 format->es->video.crop.x = 0;
1717 format->es->video.crop.y = 0;
1718 format->es->video.crop.width = 1024;
1719 format->es->video.crop.height = 768;
1720 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1721 format->es->video.frame_rate.den = 1;
1723 format = &camera->output[CAM_PORT_CAPTURE].format;
1725 format->encoding = MMAL_ENCODING_OPAQUE;
1727 format->es->video.width = 2592;
1728 format->es->video.height = 1944;
1729 format->es->video.crop.x = 0;
1730 format->es->video.crop.y = 0;
1731 format->es->video.crop.width = 2592;
1732 format->es->video.crop.height = 1944;
1733 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1734 format->es->video.frame_rate.den = 1;
1736 dev->capture.width = format->es->video.width;
1737 dev->capture.height = format->es->video.height;
1738 dev->capture.fmt = &formats[0];
1739 dev->capture.encode_component = NULL;
1740 dev->capture.timeperframe = tpf_default;
1741 dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
1742 dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
1744 /* get the preview component ready */
1745 ret = vchiq_mmal_component_init(
1746 dev->instance, "ril.video_render",
1747 &dev->component[COMP_PREVIEW]);
1751 if (dev->component[COMP_PREVIEW]->inputs < 1) {
1753 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1754 __func__, dev->component[COMP_PREVIEW]->inputs, 1);
1758 /* get the image encoder component ready */
1759 ret = vchiq_mmal_component_init(
1760 dev->instance, "ril.image_encode",
1761 &dev->component[COMP_IMAGE_ENCODE]);
1765 if (dev->component[COMP_IMAGE_ENCODE]->inputs < 1) {
1767 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1768 __func__, dev->component[COMP_IMAGE_ENCODE]->inputs,
1770 goto unreg_image_encoder;
1773 /* get the video encoder component ready */
1774 ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
1775 &dev->component[COMP_VIDEO_ENCODE]);
1777 goto unreg_image_encoder;
1779 if (dev->component[COMP_VIDEO_ENCODE]->inputs < 1) {
1781 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1782 __func__, dev->component[COMP_VIDEO_ENCODE]->inputs,
1784 goto unreg_vid_encoder;
1788 struct vchiq_mmal_port *encoder_port =
1789 &dev->component[COMP_VIDEO_ENCODE]->output[0];
1790 encoder_port->format.encoding = MMAL_ENCODING_H264;
1791 ret = vchiq_mmal_port_set_format(dev->instance,
1796 unsigned int enable = 1;
1798 vchiq_mmal_port_parameter_set(
1800 &dev->component[COMP_VIDEO_ENCODE]->control,
1801 MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
1802 &enable, sizeof(enable));
1804 vchiq_mmal_port_parameter_set(dev->instance,
1805 &dev->component[COMP_VIDEO_ENCODE]->control,
1806 MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
1810 ret = bm2835_mmal_set_all_camera_controls(dev);
1812 v4l2_err(&dev->v4l2_dev, "%s: failed to set all camera controls: %d\n",
1814 goto unreg_vid_encoder;
1820 pr_err("Cleanup: Destroy video encoder\n");
1821 vchiq_mmal_component_finalise(
1823 dev->component[COMP_VIDEO_ENCODE]);
1825 unreg_image_encoder:
1826 pr_err("Cleanup: Destroy image encoder\n");
1827 vchiq_mmal_component_finalise(
1829 dev->component[COMP_IMAGE_ENCODE]);
1832 pr_err("Cleanup: Destroy video render\n");
1833 vchiq_mmal_component_finalise(dev->instance,
1834 dev->component[COMP_PREVIEW]);
1837 pr_err("Cleanup: Destroy camera\n");
1838 vchiq_mmal_component_finalise(dev->instance,
1839 dev->component[COMP_CAMERA]);
1842 vchiq_mmal_finalise(dev->instance);
1846 static int bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
1847 struct video_device *vfd)
1851 *vfd = vdev_template;
1853 vfd->v4l2_dev = &dev->v4l2_dev;
1855 vfd->lock = &dev->mutex;
1857 vfd->queue = &dev->capture.vb_vidq;
1859 /* video device needs to be able to access instance data */
1860 video_set_drvdata(vfd, dev);
1862 ret = video_register_device(vfd,
1864 video_nr[dev->camera_num]);
1868 v4l2_info(vfd->v4l2_dev,
1869 "V4L2 device registered as %s - stills mode > %dx%d\n",
1870 video_device_node_name(vfd),
1871 max_video_width, max_video_height);
1876 static void bcm2835_cleanup_instance(struct bm2835_mmal_dev *dev)
1881 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1882 video_device_node_name(&dev->vdev));
1884 video_unregister_device(&dev->vdev);
1886 if (dev->capture.encode_component) {
1887 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1888 "mmal_exit - disconnect tunnel\n");
1889 vchiq_mmal_port_connect_tunnel(dev->instance,
1890 dev->capture.camera_port, NULL);
1891 vchiq_mmal_component_disable(dev->instance,
1892 dev->capture.encode_component);
1894 vchiq_mmal_component_disable(dev->instance,
1895 dev->component[COMP_CAMERA]);
1897 vchiq_mmal_component_finalise(dev->instance,
1898 dev->component[COMP_VIDEO_ENCODE]);
1900 vchiq_mmal_component_finalise(dev->instance,
1901 dev->component[COMP_IMAGE_ENCODE]);
1903 vchiq_mmal_component_finalise(dev->instance,
1904 dev->component[COMP_PREVIEW]);
1906 vchiq_mmal_component_finalise(dev->instance,
1907 dev->component[COMP_CAMERA]);
1909 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1911 v4l2_device_unregister(&dev->v4l2_dev);
1916 static struct v4l2_format default_v4l2_format = {
1917 .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
1918 .fmt.pix.width = 1024,
1919 .fmt.pix.bytesperline = 0,
1920 .fmt.pix.height = 768,
1921 .fmt.pix.sizeimage = 1024 * 768,
1924 static int bcm2835_mmal_probe(struct platform_device *pdev)
1927 struct bm2835_mmal_dev *dev;
1928 struct vb2_queue *q;
1930 unsigned int num_cameras;
1931 struct vchiq_mmal_instance *instance;
1932 unsigned int resolutions[MAX_BCM2835_CAMERAS][2];
1935 ret = vchiq_mmal_init(&instance);
1939 num_cameras = get_num_cameras(instance,
1941 MAX_BCM2835_CAMERAS);
1943 if (num_cameras < 1) {
1948 if (num_cameras > MAX_BCM2835_CAMERAS)
1949 num_cameras = MAX_BCM2835_CAMERAS;
1951 for (camera = 0; camera < num_cameras; camera++) {
1952 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1958 /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
1959 mutex_init(&dev->mutex);
1960 dev->camera_num = camera;
1961 dev->max_width = resolutions[camera][0];
1962 dev->max_height = resolutions[camera][1];
1964 /* setup device defaults */
1965 dev->overlay.w.left = 150;
1966 dev->overlay.w.top = 50;
1967 dev->overlay.w.width = 1024;
1968 dev->overlay.w.height = 768;
1969 dev->overlay.clipcount = 0;
1970 dev->overlay.field = V4L2_FIELD_NONE;
1971 dev->overlay.global_alpha = 255;
1973 dev->capture.fmt = &formats[3]; /* JPEG */
1975 /* v4l device registration */
1976 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1977 "%s", BM2835_MMAL_MODULE_NAME);
1978 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1980 dev_err(&pdev->dev, "%s: could not register V4L2 device: %d\n",
1985 /* setup v4l controls */
1986 ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
1988 v4l2_err(&dev->v4l2_dev, "%s: could not init controls: %d\n",
1992 dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
1995 dev->instance = instance;
1996 ret = mmal_init(dev);
1998 v4l2_err(&dev->v4l2_dev, "%s: mmal init failed: %d\n",
2002 /* initialize queue */
2003 q = &dev->capture.vb_vidq;
2004 memset(q, 0, sizeof(*q));
2005 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2006 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
2008 q->buf_struct_size = sizeof(struct vb2_mmal_buffer);
2009 q->ops = &bm2835_mmal_video_qops;
2010 q->mem_ops = &vb2_vmalloc_memops;
2011 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2012 q->lock = &dev->mutex;
2013 ret = vb2_queue_init(q);
2017 /* initialise video devices */
2018 ret = bm2835_mmal_init_device(dev, &dev->vdev);
2020 v4l2_err(&dev->v4l2_dev, "%s: could not init device: %d\n",
2025 /* Really want to call vidioc_s_fmt_vid_cap with the default
2026 * format, but currently the APIs don't join up.
2028 ret = mmal_setup_components(dev, &default_v4l2_format);
2030 v4l2_err(&dev->v4l2_dev, "%s: could not setup components: %d\n",
2035 v4l2_info(&dev->v4l2_dev,
2036 "Broadcom 2835 MMAL video capture ver %s loaded.\n",
2037 BM2835_MMAL_VERSION);
2044 v4l2_ctrl_handler_free(&dev->ctrl_handler);
2045 v4l2_device_unregister(&dev->v4l2_dev);
2051 for (i = 0; i < camera; i++) {
2052 bcm2835_cleanup_instance(gdev[i]);
2057 vchiq_mmal_finalise(instance);
2062 static int bcm2835_mmal_remove(struct platform_device *pdev)
2065 struct vchiq_mmal_instance *instance = gdev[0]->instance;
2067 for (camera = 0; camera < MAX_BCM2835_CAMERAS; camera++) {
2068 bcm2835_cleanup_instance(gdev[camera]);
2069 gdev[camera] = NULL;
2071 vchiq_mmal_finalise(instance);
2076 static struct platform_driver bcm2835_camera_driver = {
2077 .probe = bcm2835_mmal_probe,
2078 .remove = bcm2835_mmal_remove,
2080 .name = "bcm2835-camera",
2084 module_platform_driver(bcm2835_camera_driver)
2086 MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
2087 MODULE_AUTHOR("Vincent Sanders");
2088 MODULE_LICENSE("GPL");
2089 MODULE_VERSION(BM2835_MMAL_VERSION);
2090 MODULE_ALIAS("platform:bcm2835-camera");