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 int bcm2835_v4l2_debug;
48 module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
49 MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
52 static int video_nr[] = {[0 ... (MAX_BCM2835_CAMERAS - 1)] = UNSET };
53 module_param_array(video_nr, int, NULL, 0644);
54 MODULE_PARM_DESC(video_nr, "videoX start numbers, -1 is autodetect");
56 static int max_video_width = MAX_VIDEO_MODE_WIDTH;
57 static int max_video_height = MAX_VIDEO_MODE_HEIGHT;
58 module_param(max_video_width, int, 0644);
59 MODULE_PARM_DESC(max_video_width, "Threshold for video mode");
60 module_param(max_video_height, int, 0644);
61 MODULE_PARM_DESC(max_video_height, "Threshold for video mode");
63 /* camera instance counter */
64 static atomic_t camera_instance = ATOMIC_INIT(0);
66 /* global device data array */
67 static struct bm2835_mmal_dev *gdev[MAX_BCM2835_CAMERAS];
72 /* timeperframe: min/max and default */
73 static const struct v4l2_fract
74 tpf_min = {.numerator = 1, .denominator = FPS_MAX},
75 tpf_max = {.numerator = 1, .denominator = FPS_MIN},
76 tpf_default = {.numerator = 1000, .denominator = 30000};
79 static struct mmal_fmt formats[] = {
81 .fourcc = V4L2_PIX_FMT_YUV420,
82 .mmal = MMAL_ENCODING_I420,
84 .mmal_component = COMP_CAMERA,
88 .fourcc = V4L2_PIX_FMT_YUYV,
89 .mmal = MMAL_ENCODING_YUYV,
91 .mmal_component = COMP_CAMERA,
95 .fourcc = V4L2_PIX_FMT_RGB24,
96 .mmal = MMAL_ENCODING_RGB24,
98 .mmal_component = COMP_CAMERA,
102 .fourcc = V4L2_PIX_FMT_JPEG,
103 .flags = V4L2_FMT_FLAG_COMPRESSED,
104 .mmal = MMAL_ENCODING_JPEG,
106 .mmal_component = COMP_IMAGE_ENCODE,
110 .fourcc = V4L2_PIX_FMT_H264,
111 .flags = V4L2_FMT_FLAG_COMPRESSED,
112 .mmal = MMAL_ENCODING_H264,
114 .mmal_component = COMP_VIDEO_ENCODE,
118 .fourcc = V4L2_PIX_FMT_MJPEG,
119 .flags = V4L2_FMT_FLAG_COMPRESSED,
120 .mmal = MMAL_ENCODING_MJPEG,
122 .mmal_component = COMP_VIDEO_ENCODE,
126 .fourcc = V4L2_PIX_FMT_YVYU,
127 .mmal = MMAL_ENCODING_YVYU,
129 .mmal_component = COMP_CAMERA,
133 .fourcc = V4L2_PIX_FMT_VYUY,
134 .mmal = MMAL_ENCODING_VYUY,
136 .mmal_component = COMP_CAMERA,
140 .fourcc = V4L2_PIX_FMT_UYVY,
141 .mmal = MMAL_ENCODING_UYVY,
143 .mmal_component = COMP_CAMERA,
147 .fourcc = V4L2_PIX_FMT_NV12,
148 .mmal = MMAL_ENCODING_NV12,
150 .mmal_component = COMP_CAMERA,
154 .fourcc = V4L2_PIX_FMT_BGR24,
155 .mmal = MMAL_ENCODING_BGR24,
157 .mmal_component = COMP_CAMERA,
161 .fourcc = V4L2_PIX_FMT_YVU420,
162 .mmal = MMAL_ENCODING_YV12,
164 .mmal_component = COMP_CAMERA,
168 .fourcc = V4L2_PIX_FMT_NV21,
169 .mmal = MMAL_ENCODING_NV21,
171 .mmal_component = COMP_CAMERA,
175 .fourcc = V4L2_PIX_FMT_BGR32,
176 .mmal = MMAL_ENCODING_BGRA,
178 .mmal_component = COMP_CAMERA,
184 static struct mmal_fmt *get_format(struct v4l2_format *f)
186 struct mmal_fmt *fmt;
189 for (k = 0; k < ARRAY_SIZE(formats); k++) {
191 if (fmt->fourcc == f->fmt.pix.pixelformat)
198 /* ------------------------------------------------------------------
199 * Videobuf queue operations
200 * ------------------------------------------------------------------
203 static int queue_setup(struct vb2_queue *vq,
204 unsigned int *nbuffers, unsigned int *nplanes,
205 unsigned int sizes[], struct device *alloc_ctxs[])
207 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
210 /* refuse queue setup if port is not configured */
211 if (!dev->capture.port) {
212 v4l2_err(&dev->v4l2_dev,
213 "%s: capture port not configured\n", __func__);
217 /* Handle CREATE_BUFS situation - *nplanes != 0 */
220 sizes[0] < dev->capture.port->current_buffer.size) {
221 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
222 "%s: dev:%p Invalid buffer request from CREATE_BUFS, size %u < %u, nplanes %u != 1\n",
223 __func__, dev, sizes[0],
224 dev->capture.port->current_buffer.size,
232 /* Handle REQBUFS situation */
233 size = dev->capture.port->current_buffer.size;
235 v4l2_err(&dev->v4l2_dev,
236 "%s: capture port buffer size is zero\n", __func__);
240 if (*nbuffers < dev->capture.port->minimum_buffer.num)
241 *nbuffers = dev->capture.port->minimum_buffer.num;
243 dev->capture.port->current_buffer.num = *nbuffers;
250 * videobuf2-vmalloc allocator is context-less so no need to set
254 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
260 static int buffer_init(struct vb2_buffer *vb)
262 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
263 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
264 struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
266 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
268 buf->buffer = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
269 buf->buffer_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
271 return mmal_vchi_buffer_init(dev->instance, buf);
274 static int buffer_prepare(struct vb2_buffer *vb)
276 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
279 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
282 if (!dev->capture.port || !dev->capture.fmt)
285 size = dev->capture.stride * dev->capture.height;
286 if (vb2_plane_size(vb, 0) < size) {
287 v4l2_err(&dev->v4l2_dev,
288 "%s data will not fit into plane (%lu < %lu)\n",
289 __func__, vb2_plane_size(vb, 0), size);
296 static void buffer_cleanup(struct vb2_buffer *vb)
298 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
299 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
300 struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
302 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
304 mmal_vchi_buffer_cleanup(buf);
307 static inline bool is_capturing(struct bm2835_mmal_dev *dev)
309 return dev->capture.camera_port ==
310 &dev->component[COMP_CAMERA]->output[CAM_PORT_CAPTURE];
313 static void buffer_cb(struct vchiq_mmal_instance *instance,
314 struct vchiq_mmal_port *port,
316 struct mmal_buffer *buf,
317 unsigned long length, u32 mmal_flags, s64 dts, s64 pts)
319 struct bm2835_mmal_dev *dev = port->cb_ctx;
321 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
322 "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
323 __func__, status, buf, length, mmal_flags, pts);
326 /* error in transfer */
328 /* there was a buffer with the error so return it */
329 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
336 if (dev->capture.frame_count) {
337 /* empty buffer whilst capturing - expected to be an
338 * EOS, so grab another frame
340 if (is_capturing(dev)) {
341 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
342 "Grab another frame");
343 vchiq_mmal_port_parameter_set(
345 dev->capture.camera_port,
346 MMAL_PARAMETER_CAPTURE,
347 &dev->capture.frame_count,
348 sizeof(dev->capture.frame_count));
350 if (vchiq_mmal_submit_buffer(instance, port, buf))
351 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
352 "Failed to return EOS buffer");
354 /* stopping streaming.
355 * return buffer, and signal frame completion
357 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
358 complete(&dev->capture.frame_cmplt);
363 if (!dev->capture.frame_count) {
364 /* signal frame completion */
365 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
366 complete(&dev->capture.frame_cmplt);
370 if (dev->capture.vc_start_timestamp != -1 && pts) {
372 s64 runtime_us = pts -
373 dev->capture.vc_start_timestamp;
374 timestamp = ktime_add_us(dev->capture.kernel_start_ts,
376 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
377 "Convert start time %llu and %llu with offset %llu to %llu\n",
378 ktime_to_ns(dev->capture.kernel_start_ts),
379 dev->capture.vc_start_timestamp, pts,
380 ktime_to_ns(timestamp));
381 buf->vb.vb2_buf.timestamp = ktime_to_ns(timestamp);
383 buf->vb.vb2_buf.timestamp = ktime_get_ns();
385 buf->vb.sequence = dev->capture.sequence++;
386 buf->vb.field = V4L2_FIELD_NONE;
388 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, length);
389 if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_KEYFRAME)
390 buf->vb.flags |= V4L2_BUF_FLAG_KEYFRAME;
392 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
394 if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
396 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
397 "Grab another frame as buffer has EOS");
398 vchiq_mmal_port_parameter_set(
400 dev->capture.camera_port,
401 MMAL_PARAMETER_CAPTURE,
402 &dev->capture.frame_count,
403 sizeof(dev->capture.frame_count));
407 static int enable_camera(struct bm2835_mmal_dev *dev)
411 if (!dev->camera_use_count) {
412 ret = vchiq_mmal_port_parameter_set(
414 &dev->component[COMP_CAMERA]->control,
415 MMAL_PARAMETER_CAMERA_NUM, &dev->camera_num,
416 sizeof(dev->camera_num));
418 v4l2_err(&dev->v4l2_dev,
419 "Failed setting camera num, ret %d\n", ret);
423 ret = vchiq_mmal_component_enable(
425 dev->component[COMP_CAMERA]);
427 v4l2_err(&dev->v4l2_dev,
428 "Failed enabling camera, ret %d\n", ret);
432 dev->camera_use_count++;
433 v4l2_dbg(1, bcm2835_v4l2_debug,
434 &dev->v4l2_dev, "enabled camera (refcount %d)\n",
435 dev->camera_use_count);
439 static int disable_camera(struct bm2835_mmal_dev *dev)
443 if (!dev->camera_use_count) {
444 v4l2_err(&dev->v4l2_dev,
445 "Disabled the camera when already disabled\n");
448 dev->camera_use_count--;
449 if (!dev->camera_use_count) {
450 unsigned int i = 0xFFFFFFFF;
452 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
453 "Disabling camera\n");
455 vchiq_mmal_component_disable(
457 dev->component[COMP_CAMERA]);
459 v4l2_err(&dev->v4l2_dev,
460 "Failed disabling camera, ret %d\n", ret);
463 vchiq_mmal_port_parameter_set(
465 &dev->component[COMP_CAMERA]->control,
466 MMAL_PARAMETER_CAMERA_NUM, &i,
469 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
470 "Camera refcount now %d\n", dev->camera_use_count);
474 static void buffer_queue(struct vb2_buffer *vb)
476 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
477 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
478 struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
481 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
482 "%s: dev:%p buf:%p, idx %u\n",
483 __func__, dev, buf, vb2->vb2_buf.index);
485 ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
487 v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
491 static int start_streaming(struct vb2_queue *vq, unsigned int count)
493 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
497 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
500 /* ensure a format has actually been set */
501 if (!dev->capture.port)
504 if (enable_camera(dev) < 0) {
505 v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
509 /*init_completion(&dev->capture.frame_cmplt); */
511 /* enable frame capture */
512 dev->capture.frame_count = 1;
514 /* reset sequence number */
515 dev->capture.sequence = 0;
517 /* if the preview is not already running, wait for a few frames for AGC
520 if (!dev->component[COMP_PREVIEW]->enabled)
523 /* enable the connection from camera to encoder (if applicable) */
524 if (dev->capture.camera_port != dev->capture.port &&
525 dev->capture.camera_port) {
526 ret = vchiq_mmal_port_enable(dev->instance,
527 dev->capture.camera_port, NULL);
529 v4l2_err(&dev->v4l2_dev,
530 "Failed to enable encode tunnel - error %d\n",
536 /* Get VC timestamp at this point in time */
537 parameter_size = sizeof(dev->capture.vc_start_timestamp);
538 if (vchiq_mmal_port_parameter_get(dev->instance,
539 dev->capture.camera_port,
540 MMAL_PARAMETER_SYSTEM_TIME,
541 &dev->capture.vc_start_timestamp,
543 v4l2_err(&dev->v4l2_dev,
544 "Failed to get VC start time - update your VC f/w\n");
546 /* Flag to indicate just to rely on kernel timestamps */
547 dev->capture.vc_start_timestamp = -1;
549 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
550 "Start time %lld size %d\n",
551 dev->capture.vc_start_timestamp, parameter_size);
554 dev->capture.kernel_start_ts = ktime_get();
556 /* enable the camera port */
557 dev->capture.port->cb_ctx = dev;
559 vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
561 v4l2_err(&dev->v4l2_dev,
562 "Failed to enable capture port - error %d. Disabling camera port again\n",
565 vchiq_mmal_port_disable(dev->instance,
566 dev->capture.camera_port);
567 if (disable_camera(dev) < 0) {
568 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
574 /* capture the first frame */
575 vchiq_mmal_port_parameter_set(dev->instance,
576 dev->capture.camera_port,
577 MMAL_PARAMETER_CAPTURE,
578 &dev->capture.frame_count,
579 sizeof(dev->capture.frame_count));
583 /* abort streaming and wait for last buffer */
584 static void stop_streaming(struct vb2_queue *vq)
587 unsigned long timeout;
588 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
589 struct vchiq_mmal_port *port = dev->capture.port;
591 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
594 init_completion(&dev->capture.frame_cmplt);
595 dev->capture.frame_count = 0;
597 /* ensure a format has actually been set */
598 if (!dev->capture.port) {
599 v4l2_err(&dev->v4l2_dev,
600 "no capture port - stream not started?\n");
604 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
606 /* stop capturing frames */
607 vchiq_mmal_port_parameter_set(dev->instance,
608 dev->capture.camera_port,
609 MMAL_PARAMETER_CAPTURE,
610 &dev->capture.frame_count,
611 sizeof(dev->capture.frame_count));
613 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
614 "disabling connection\n");
616 /* disable the connection from camera to encoder */
617 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
618 if (!ret && dev->capture.camera_port != dev->capture.port) {
619 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
621 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
622 } else if (dev->capture.camera_port != dev->capture.port) {
623 v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
627 /* wait for all buffers to be returned */
628 while (atomic_read(&port->buffers_with_vpu)) {
629 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
630 "%s: Waiting for buffers to be returned - %d outstanding\n",
631 __func__, atomic_read(&port->buffers_with_vpu));
632 timeout = wait_for_completion_timeout(&dev->capture.frame_cmplt,
635 v4l2_err(&dev->v4l2_dev, "%s: Timeout waiting for buffers to be returned - %d outstanding\n",
637 atomic_read(&port->buffers_with_vpu));
642 if (disable_camera(dev) < 0)
643 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
646 static const struct vb2_ops bm2835_mmal_video_qops = {
647 .queue_setup = queue_setup,
648 .buf_init = buffer_init,
649 .buf_prepare = buffer_prepare,
650 .buf_cleanup = buffer_cleanup,
651 .buf_queue = buffer_queue,
652 .start_streaming = start_streaming,
653 .stop_streaming = stop_streaming,
654 .wait_prepare = vb2_ops_wait_prepare,
655 .wait_finish = vb2_ops_wait_finish,
658 /* ------------------------------------------------------------------
660 * ------------------------------------------------------------------
663 static int set_overlay_params(struct bm2835_mmal_dev *dev,
664 struct vchiq_mmal_port *port)
666 struct mmal_parameter_displayregion prev_config = {
667 .set = MMAL_DISPLAY_SET_LAYER |
668 MMAL_DISPLAY_SET_ALPHA |
669 MMAL_DISPLAY_SET_DEST_RECT |
670 MMAL_DISPLAY_SET_FULLSCREEN,
671 .layer = PREVIEW_LAYER,
672 .alpha = dev->overlay.global_alpha,
675 .x = dev->overlay.w.left,
676 .y = dev->overlay.w.top,
677 .width = dev->overlay.w.width,
678 .height = dev->overlay.w.height,
681 return vchiq_mmal_port_parameter_set(dev->instance, port,
682 MMAL_PARAMETER_DISPLAYREGION,
683 &prev_config, sizeof(prev_config));
687 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
688 struct v4l2_fmtdesc *f)
690 struct mmal_fmt *fmt;
692 if (f->index >= ARRAY_SIZE(formats))
695 fmt = &formats[f->index];
697 f->pixelformat = fmt->fourcc;
702 static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
703 struct v4l2_format *f)
705 struct bm2835_mmal_dev *dev = video_drvdata(file);
707 f->fmt.win = dev->overlay;
712 static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
713 struct v4l2_format *f)
715 struct bm2835_mmal_dev *dev = video_drvdata(file);
717 f->fmt.win.field = V4L2_FIELD_NONE;
718 f->fmt.win.chromakey = 0;
719 f->fmt.win.clips = NULL;
720 f->fmt.win.clipcount = 0;
721 f->fmt.win.bitmap = NULL;
723 v4l_bound_align_image(&f->fmt.win.w.width, MIN_WIDTH, dev->max_width, 1,
724 &f->fmt.win.w.height, MIN_HEIGHT, dev->max_height,
726 v4l_bound_align_image(&f->fmt.win.w.left, MIN_WIDTH, dev->max_width, 1,
727 &f->fmt.win.w.top, MIN_HEIGHT, dev->max_height,
730 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
731 "Overlay: Now w/h %dx%d l/t %dx%d\n",
732 f->fmt.win.w.width, f->fmt.win.w.height,
733 f->fmt.win.w.left, f->fmt.win.w.top);
735 v4l2_dump_win_format(1,
743 static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
744 struct v4l2_format *f)
746 struct bm2835_mmal_dev *dev = video_drvdata(file);
748 vidioc_try_fmt_vid_overlay(file, priv, f);
750 dev->overlay = f->fmt.win;
751 if (dev->component[COMP_PREVIEW]->enabled) {
752 set_overlay_params(dev,
753 &dev->component[COMP_PREVIEW]->input[0]);
759 static int vidioc_overlay(struct file *file, void *f, unsigned int on)
762 struct bm2835_mmal_dev *dev = video_drvdata(file);
763 struct vchiq_mmal_port *src;
764 struct vchiq_mmal_port *dst;
766 if ((on && dev->component[COMP_PREVIEW]->enabled) ||
767 (!on && !dev->component[COMP_PREVIEW]->enabled))
768 return 0; /* already in requested state */
771 &dev->component[COMP_CAMERA]->output[CAM_PORT_PREVIEW];
774 /* disconnect preview ports and disable component */
775 ret = vchiq_mmal_port_disable(dev->instance, src);
778 vchiq_mmal_port_connect_tunnel(dev->instance, src,
781 ret = vchiq_mmal_component_disable(
783 dev->component[COMP_PREVIEW]);
789 /* set preview port format and connect it to output */
790 dst = &dev->component[COMP_PREVIEW]->input[0];
792 ret = vchiq_mmal_port_set_format(dev->instance, src);
796 ret = set_overlay_params(dev, dst);
800 if (enable_camera(dev) < 0)
803 ret = vchiq_mmal_component_enable(
805 dev->component[COMP_PREVIEW]);
809 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
811 ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
815 return vchiq_mmal_port_enable(dev->instance, src, NULL);
818 static int vidioc_g_fbuf(struct file *file, void *fh,
819 struct v4l2_framebuffer *a)
821 /* The video overlay must stay within the framebuffer and can't be
822 * positioned independently.
824 struct bm2835_mmal_dev *dev = video_drvdata(file);
825 struct vchiq_mmal_port *preview_port =
826 &dev->component[COMP_CAMERA]->output[CAM_PORT_PREVIEW];
828 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
829 V4L2_FBUF_CAP_GLOBAL_ALPHA;
830 a->flags = V4L2_FBUF_FLAG_OVERLAY;
831 a->fmt.width = preview_port->es.video.width;
832 a->fmt.height = preview_port->es.video.height;
833 a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
834 a->fmt.bytesperline = preview_port->es.video.width;
835 a->fmt.sizeimage = (preview_port->es.video.width *
836 preview_port->es.video.height * 3) >> 1;
837 a->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
843 static int vidioc_enum_input(struct file *file, void *priv,
844 struct v4l2_input *inp)
846 /* only a single camera input */
850 inp->type = V4L2_INPUT_TYPE_CAMERA;
851 sprintf((char *)inp->name, "Camera %u", inp->index);
855 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
861 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
870 static int vidioc_querycap(struct file *file, void *priv,
871 struct v4l2_capability *cap)
873 struct bm2835_mmal_dev *dev = video_drvdata(file);
877 vchiq_mmal_version(dev->instance, &major, &minor);
879 strcpy((char *)cap->driver, "bm2835 mmal");
880 snprintf((char *)cap->card, sizeof(cap->card), "mmal service %d.%d",
883 snprintf((char *)cap->bus_info, sizeof(cap->bus_info),
884 "platform:%s", dev->v4l2_dev.name);
888 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
889 struct v4l2_fmtdesc *f)
891 struct mmal_fmt *fmt;
893 if (f->index >= ARRAY_SIZE(formats))
896 fmt = &formats[f->index];
898 f->pixelformat = fmt->fourcc;
903 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
904 struct v4l2_format *f)
906 struct bm2835_mmal_dev *dev = video_drvdata(file);
908 f->fmt.pix.width = dev->capture.width;
909 f->fmt.pix.height = dev->capture.height;
910 f->fmt.pix.field = V4L2_FIELD_NONE;
911 f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
912 f->fmt.pix.bytesperline = dev->capture.stride;
913 f->fmt.pix.sizeimage = dev->capture.buffersize;
915 if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
916 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
917 else if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG)
918 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
920 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
923 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
928 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
929 struct v4l2_format *f)
931 struct bm2835_mmal_dev *dev = video_drvdata(file);
932 struct mmal_fmt *mfmt;
934 mfmt = get_format(f);
936 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
937 "Fourcc format (0x%08x) unknown.\n",
938 f->fmt.pix.pixelformat);
939 f->fmt.pix.pixelformat = formats[0].fourcc;
940 mfmt = get_format(f);
943 f->fmt.pix.field = V4L2_FIELD_NONE;
945 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
946 "Clipping/aligning %dx%d format %08X\n",
947 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
949 v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, dev->max_width, 1,
950 &f->fmt.pix.height, MIN_HEIGHT, dev->max_height,
952 f->fmt.pix.bytesperline = f->fmt.pix.width * mfmt->ybbp;
953 if (!mfmt->remove_padding) {
954 if (mfmt->depth == 24) {
956 * 24bpp is a pain as we can't use simple masking.
957 * Min stride is width aligned to 16, times 24bpp.
959 f->fmt.pix.bytesperline =
960 ((f->fmt.pix.width + 15) & ~15) * 3;
963 * GPU isn't removing padding, so stride is aligned to
966 int align_mask = ((32 * mfmt->depth) >> 3) - 1;
968 f->fmt.pix.bytesperline =
969 (f->fmt.pix.bytesperline + align_mask) &
972 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
973 "Not removing padding, so bytes/line = %d\n",
974 f->fmt.pix.bytesperline);
977 /* Image buffer has to be padded to allow for alignment, even though
978 * we sometimes then remove that padding before delivering the buffer.
980 f->fmt.pix.sizeimage = ((f->fmt.pix.height + 15) & ~15) *
981 (((f->fmt.pix.width + 31) & ~31) * mfmt->depth) >> 3;
983 if ((mfmt->flags & V4L2_FMT_FLAG_COMPRESSED) &&
984 f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
985 f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
987 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24)
988 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
989 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
990 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
992 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
995 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
996 "Now %dx%d format %08X\n",
997 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
999 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
1004 static int mmal_setup_components(struct bm2835_mmal_dev *dev,
1005 struct v4l2_format *f)
1008 struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
1009 struct vchiq_mmal_component *encode_component = NULL;
1010 struct mmal_fmt *mfmt = get_format(f);
1016 if (dev->capture.encode_component) {
1017 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1018 "vid_cap - disconnect previous tunnel\n");
1020 /* Disconnect any previous connection */
1021 vchiq_mmal_port_connect_tunnel(dev->instance,
1022 dev->capture.camera_port, NULL);
1023 dev->capture.camera_port = NULL;
1024 ret = vchiq_mmal_component_disable(dev->instance,
1025 dev->capture.encode_component);
1027 v4l2_err(&dev->v4l2_dev,
1028 "Failed to disable encode component %d\n",
1031 dev->capture.encode_component = NULL;
1033 /* format dependent port setup */
1034 switch (mfmt->mmal_component) {
1036 /* Make a further decision on port based on resolution */
1037 if (f->fmt.pix.width <= max_video_width &&
1038 f->fmt.pix.height <= max_video_height)
1040 &dev->component[COMP_CAMERA]->output[CAM_PORT_VIDEO];
1043 &dev->component[COMP_CAMERA]->output[CAM_PORT_CAPTURE];
1046 case COMP_IMAGE_ENCODE:
1047 encode_component = dev->component[COMP_IMAGE_ENCODE];
1048 port = &dev->component[COMP_IMAGE_ENCODE]->output[0];
1050 &dev->component[COMP_CAMERA]->output[CAM_PORT_CAPTURE];
1052 case COMP_VIDEO_ENCODE:
1053 encode_component = dev->component[COMP_VIDEO_ENCODE];
1054 port = &dev->component[COMP_VIDEO_ENCODE]->output[0];
1056 &dev->component[COMP_CAMERA]->output[CAM_PORT_VIDEO];
1065 if (encode_component)
1066 camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
1068 camera_port->format.encoding = mfmt->mmal;
1070 if (dev->rgb_bgr_swapped) {
1071 if (camera_port->format.encoding == MMAL_ENCODING_RGB24)
1072 camera_port->format.encoding = MMAL_ENCODING_BGR24;
1073 else if (camera_port->format.encoding == MMAL_ENCODING_BGR24)
1074 camera_port->format.encoding = MMAL_ENCODING_RGB24;
1077 remove_padding = mfmt->remove_padding;
1078 vchiq_mmal_port_parameter_set(dev->instance,
1080 MMAL_PARAMETER_NO_IMAGE_PADDING,
1081 &remove_padding, sizeof(remove_padding));
1083 camera_port->format.encoding_variant = 0;
1084 camera_port->es.video.width = f->fmt.pix.width;
1085 camera_port->es.video.height = f->fmt.pix.height;
1086 camera_port->es.video.crop.x = 0;
1087 camera_port->es.video.crop.y = 0;
1088 camera_port->es.video.crop.width = f->fmt.pix.width;
1089 camera_port->es.video.crop.height = f->fmt.pix.height;
1090 camera_port->es.video.frame_rate.num = 0;
1091 camera_port->es.video.frame_rate.den = 1;
1092 camera_port->es.video.color_space = MMAL_COLOR_SPACE_JPEG_JFIF;
1094 ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
1098 &dev->component[COMP_CAMERA]->output[CAM_PORT_VIDEO]) {
1099 bool overlay_enabled =
1100 !!dev->component[COMP_PREVIEW]->enabled;
1101 struct vchiq_mmal_port *preview_port =
1102 &dev->component[COMP_CAMERA]->output[CAM_PORT_PREVIEW];
1103 /* Preview and encode ports need to match on resolution */
1104 if (overlay_enabled) {
1105 /* Need to disable the overlay before we can update
1109 vchiq_mmal_port_disable(dev->instance,
1113 vchiq_mmal_port_connect_tunnel(
1118 preview_port->es.video.width = f->fmt.pix.width;
1119 preview_port->es.video.height = f->fmt.pix.height;
1120 preview_port->es.video.crop.x = 0;
1121 preview_port->es.video.crop.y = 0;
1122 preview_port->es.video.crop.width = f->fmt.pix.width;
1123 preview_port->es.video.crop.height = f->fmt.pix.height;
1124 preview_port->es.video.frame_rate.num =
1125 dev->capture.timeperframe.denominator;
1126 preview_port->es.video.frame_rate.den =
1127 dev->capture.timeperframe.numerator;
1128 ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
1129 if (overlay_enabled) {
1130 ret = vchiq_mmal_port_connect_tunnel(
1133 &dev->component[COMP_PREVIEW]->input[0]);
1135 ret = vchiq_mmal_port_enable(dev->instance,
1142 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1143 "%s failed to set format %dx%d %08X\n", __func__,
1144 f->fmt.pix.width, f->fmt.pix.height,
1145 f->fmt.pix.pixelformat);
1146 /* ensure capture is not going to be tried */
1147 dev->capture.port = NULL;
1149 if (encode_component) {
1150 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1151 "vid_cap - set up encode comp\n");
1153 /* configure buffering */
1154 camera_port->current_buffer.size =
1155 camera_port->recommended_buffer.size;
1156 camera_port->current_buffer.num =
1157 camera_port->recommended_buffer.num;
1160 vchiq_mmal_port_connect_tunnel(
1163 &encode_component->input[0]);
1165 v4l2_dbg(1, bcm2835_v4l2_debug,
1167 "%s failed to create connection\n",
1169 /* ensure capture is not going to be tried */
1170 dev->capture.port = NULL;
1172 port->es.video.width = f->fmt.pix.width;
1173 port->es.video.height = f->fmt.pix.height;
1174 port->es.video.crop.x = 0;
1175 port->es.video.crop.y = 0;
1176 port->es.video.crop.width = f->fmt.pix.width;
1177 port->es.video.crop.height = f->fmt.pix.height;
1178 port->es.video.frame_rate.num =
1179 dev->capture.timeperframe.denominator;
1180 port->es.video.frame_rate.den =
1181 dev->capture.timeperframe.numerator;
1183 port->format.encoding = mfmt->mmal;
1184 port->format.encoding_variant = 0;
1185 /* Set any encoding specific parameters */
1186 switch (mfmt->mmal_component) {
1187 case COMP_VIDEO_ENCODE:
1188 port->format.bitrate =
1189 dev->capture.encode_bitrate;
1191 case COMP_IMAGE_ENCODE:
1192 /* Could set EXIF parameters here */
1197 ret = vchiq_mmal_port_set_format(dev->instance,
1200 v4l2_dbg(1, bcm2835_v4l2_debug,
1202 "%s failed to set format %dx%d fmt %08X\n",
1206 f->fmt.pix.pixelformat
1211 ret = vchiq_mmal_component_enable(
1215 v4l2_dbg(1, bcm2835_v4l2_debug,
1217 "%s Failed to enable encode components\n",
1222 /* configure buffering */
1223 port->current_buffer.num = 1;
1224 port->current_buffer.size =
1225 f->fmt.pix.sizeimage;
1226 if (port->format.encoding ==
1227 MMAL_ENCODING_JPEG) {
1228 v4l2_dbg(1, bcm2835_v4l2_debug,
1230 "JPG - buf size now %d was %d\n",
1231 f->fmt.pix.sizeimage,
1232 port->current_buffer.size);
1233 port->current_buffer.size =
1234 (f->fmt.pix.sizeimage <
1236 (100 << 10) : f->fmt.pix.sizeimage;
1238 v4l2_dbg(1, bcm2835_v4l2_debug,
1240 "vid_cap - cur_buf.size set to %d\n",
1241 f->fmt.pix.sizeimage);
1242 port->current_buffer.alignment = 0;
1245 /* configure buffering */
1246 camera_port->current_buffer.num = 1;
1247 camera_port->current_buffer.size = f->fmt.pix.sizeimage;
1248 camera_port->current_buffer.alignment = 0;
1252 dev->capture.fmt = mfmt;
1253 dev->capture.stride = f->fmt.pix.bytesperline;
1254 dev->capture.width = camera_port->es.video.crop.width;
1255 dev->capture.height = camera_port->es.video.crop.height;
1256 dev->capture.buffersize = port->current_buffer.size;
1258 /* select port for capture */
1259 dev->capture.port = port;
1260 dev->capture.camera_port = camera_port;
1261 dev->capture.encode_component = encode_component;
1262 v4l2_dbg(1, bcm2835_v4l2_debug,
1264 "Set dev->capture.fmt %08X, %dx%d, stride %d, size %d",
1265 port->format.encoding,
1266 dev->capture.width, dev->capture.height,
1267 dev->capture.stride, dev->capture.buffersize);
1271 /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
1275 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1276 struct v4l2_format *f)
1279 struct bm2835_mmal_dev *dev = video_drvdata(file);
1280 struct mmal_fmt *mfmt;
1282 /* try the format to set valid parameters */
1283 ret = vidioc_try_fmt_vid_cap(file, priv, f);
1285 v4l2_err(&dev->v4l2_dev,
1286 "vid_cap - vidioc_try_fmt_vid_cap failed\n");
1290 /* if a capture is running refuse to set format */
1291 if (vb2_is_busy(&dev->capture.vb_vidq)) {
1292 v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
1296 /* If the format is unsupported v4l2 says we should switch to
1297 * a supported one and not return an error.
1299 mfmt = get_format(f);
1301 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1302 "Fourcc format (0x%08x) unknown.\n",
1303 f->fmt.pix.pixelformat);
1304 f->fmt.pix.pixelformat = formats[0].fourcc;
1305 mfmt = get_format(f);
1308 ret = mmal_setup_components(dev, f);
1310 v4l2_err(&dev->v4l2_dev,
1311 "%s: failed to setup mmal components: %d\n",
1319 static int vidioc_enum_framesizes(struct file *file, void *fh,
1320 struct v4l2_frmsizeenum *fsize)
1322 struct bm2835_mmal_dev *dev = video_drvdata(file);
1323 static const struct v4l2_frmsize_stepwise sizes = {
1331 for (i = 0; i < ARRAY_SIZE(formats); i++)
1332 if (formats[i].fourcc == fsize->pixel_format)
1334 if (i == ARRAY_SIZE(formats))
1336 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1337 fsize->stepwise = sizes;
1338 fsize->stepwise.max_width = dev->max_width;
1339 fsize->stepwise.max_height = dev->max_height;
1343 /* timeperframe is arbitrary and continuous */
1344 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1345 struct v4l2_frmivalenum *fival)
1347 struct bm2835_mmal_dev *dev = video_drvdata(file);
1353 for (i = 0; i < ARRAY_SIZE(formats); i++)
1354 if (formats[i].fourcc == fival->pixel_format)
1356 if (i == ARRAY_SIZE(formats))
1359 /* regarding width & height - we support any within range */
1360 if (fival->width < MIN_WIDTH || fival->width > dev->max_width ||
1361 fival->height < MIN_HEIGHT || fival->height > dev->max_height)
1364 fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1366 /* fill in stepwise (step=1.0 is required by V4L2 spec) */
1367 fival->stepwise.min = tpf_min;
1368 fival->stepwise.max = tpf_max;
1369 fival->stepwise.step = (struct v4l2_fract) {1, 1};
1374 static int vidioc_g_parm(struct file *file, void *priv,
1375 struct v4l2_streamparm *parm)
1377 struct bm2835_mmal_dev *dev = video_drvdata(file);
1379 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1382 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1383 parm->parm.capture.timeperframe = dev->capture.timeperframe;
1384 parm->parm.capture.readbuffers = 1;
1388 static int vidioc_s_parm(struct file *file, void *priv,
1389 struct v4l2_streamparm *parm)
1391 struct bm2835_mmal_dev *dev = video_drvdata(file);
1392 struct v4l2_fract tpf;
1394 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1397 tpf = parm->parm.capture.timeperframe;
1399 /* tpf: {*, 0} resets timing; clip to [min, max]*/
1400 tpf = tpf.denominator ? tpf : tpf_default;
1401 tpf = V4L2_FRACT_COMPARE(tpf, <, tpf_min) ? tpf_min : tpf;
1402 tpf = V4L2_FRACT_COMPARE(tpf, >, tpf_max) ? tpf_max : tpf;
1404 dev->capture.timeperframe = tpf;
1405 parm->parm.capture.timeperframe = tpf;
1406 parm->parm.capture.readbuffers = 1;
1407 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1409 set_framerate_params(dev);
1414 static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
1416 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1417 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1418 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1419 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1420 .vidioc_overlay = vidioc_overlay,
1421 .vidioc_g_fbuf = vidioc_g_fbuf,
1424 .vidioc_enum_input = vidioc_enum_input,
1425 .vidioc_g_input = vidioc_g_input,
1426 .vidioc_s_input = vidioc_s_input,
1429 .vidioc_querycap = vidioc_querycap,
1430 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1431 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1432 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1433 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1435 /* buffer management */
1436 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1437 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1438 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1439 .vidioc_querybuf = vb2_ioctl_querybuf,
1440 .vidioc_qbuf = vb2_ioctl_qbuf,
1441 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1442 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1443 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1444 .vidioc_g_parm = vidioc_g_parm,
1445 .vidioc_s_parm = vidioc_s_parm,
1446 .vidioc_streamon = vb2_ioctl_streamon,
1447 .vidioc_streamoff = vb2_ioctl_streamoff,
1449 .vidioc_log_status = v4l2_ctrl_log_status,
1450 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1451 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1454 /* ------------------------------------------------------------------
1455 * Driver init/finalise
1456 * ------------------------------------------------------------------
1459 static const struct v4l2_file_operations camera0_fops = {
1460 .owner = THIS_MODULE,
1461 .open = v4l2_fh_open,
1462 .release = vb2_fop_release,
1463 .read = vb2_fop_read,
1464 .poll = vb2_fop_poll,
1465 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1466 .mmap = vb2_fop_mmap,
1469 static const struct video_device vdev_template = {
1471 .fops = &camera0_fops,
1472 .ioctl_ops = &camera0_ioctl_ops,
1473 .release = video_device_release_empty,
1474 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
1475 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE,
1478 /* Returns the number of cameras, and also the max resolution supported
1481 static int get_num_cameras(struct vchiq_mmal_instance *instance,
1482 unsigned int resolutions[][2], int num_resolutions)
1485 struct vchiq_mmal_component *cam_info_component;
1486 struct mmal_parameter_camera_info_t cam_info = {0};
1487 u32 param_size = sizeof(cam_info);
1490 /* create a camera_info component */
1491 ret = vchiq_mmal_component_init(instance, "camera_info",
1492 &cam_info_component);
1494 /* Unusual failure - let's guess one camera. */
1497 if (vchiq_mmal_port_parameter_get(instance,
1498 &cam_info_component->control,
1499 MMAL_PARAMETER_CAMERA_INFO,
1502 pr_info("Failed to get camera info\n");
1505 i < min_t(unsigned int, cam_info.num_cameras, num_resolutions);
1507 resolutions[i][0] = cam_info.cameras[i].max_width;
1508 resolutions[i][1] = cam_info.cameras[i].max_height;
1511 vchiq_mmal_component_finalise(instance,
1512 cam_info_component);
1514 return cam_info.num_cameras;
1517 static int set_camera_parameters(struct vchiq_mmal_instance *instance,
1518 struct vchiq_mmal_component *camera,
1519 struct bm2835_mmal_dev *dev)
1521 struct mmal_parameter_camera_config cam_config = {
1522 .max_stills_w = dev->max_width,
1523 .max_stills_h = dev->max_height,
1525 .one_shot_stills = 1,
1526 .max_preview_video_w = (max_video_width > 1920) ?
1527 max_video_width : 1920,
1528 .max_preview_video_h = (max_video_height > 1088) ?
1529 max_video_height : 1088,
1530 .num_preview_video_frames = 3,
1531 .stills_capture_circular_buffer_height = 0,
1532 .fast_preview_resume = 0,
1533 .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
1536 return vchiq_mmal_port_parameter_set(instance, &camera->control,
1537 MMAL_PARAMETER_CAMERA_CONFIG,
1538 &cam_config, sizeof(cam_config));
1541 #define MAX_SUPPORTED_ENCODINGS 20
1543 /* MMAL instance and component init */
1544 static int mmal_init(struct bm2835_mmal_dev *dev)
1547 struct mmal_es_format_local *format;
1548 u32 supported_encodings[MAX_SUPPORTED_ENCODINGS];
1550 struct vchiq_mmal_component *camera;
1552 ret = vchiq_mmal_init(&dev->instance);
1554 v4l2_err(&dev->v4l2_dev, "%s: vchiq mmal init failed %d\n",
1559 /* get the camera component ready */
1560 ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
1561 &dev->component[COMP_CAMERA]);
1565 camera = dev->component[COMP_CAMERA];
1566 if (camera->outputs < CAM_PORT_COUNT) {
1567 v4l2_err(&dev->v4l2_dev, "%s: too few camera outputs %d needed %d\n",
1568 __func__, camera->outputs, CAM_PORT_COUNT);
1573 ret = set_camera_parameters(dev->instance,
1577 v4l2_err(&dev->v4l2_dev, "%s: unable to set camera parameters: %d\n",
1582 /* There was an error in the firmware that meant the camera component
1583 * produced BGR instead of RGB.
1584 * This is now fixed, but in order to support the old firmwares, we
1587 dev->rgb_bgr_swapped = true;
1588 param_size = sizeof(supported_encodings);
1589 ret = vchiq_mmal_port_parameter_get(dev->instance,
1590 &camera->output[CAM_PORT_CAPTURE],
1591 MMAL_PARAMETER_SUPPORTED_ENCODINGS,
1592 &supported_encodings,
1597 for (i = 0; i < param_size / sizeof(u32); i++) {
1598 if (supported_encodings[i] == MMAL_ENCODING_BGR24) {
1599 /* Found BGR24 first - old firmware. */
1602 if (supported_encodings[i] == MMAL_ENCODING_RGB24) {
1603 /* Found RGB24 first
1604 * new firmware, so use RGB24.
1606 dev->rgb_bgr_swapped = false;
1611 format = &camera->output[CAM_PORT_PREVIEW].format;
1613 format->encoding = MMAL_ENCODING_OPAQUE;
1614 format->encoding_variant = MMAL_ENCODING_I420;
1616 format->es->video.width = 1024;
1617 format->es->video.height = 768;
1618 format->es->video.crop.x = 0;
1619 format->es->video.crop.y = 0;
1620 format->es->video.crop.width = 1024;
1621 format->es->video.crop.height = 768;
1622 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1623 format->es->video.frame_rate.den = 1;
1625 format = &camera->output[CAM_PORT_VIDEO].format;
1627 format->encoding = MMAL_ENCODING_OPAQUE;
1628 format->encoding_variant = MMAL_ENCODING_I420;
1630 format->es->video.width = 1024;
1631 format->es->video.height = 768;
1632 format->es->video.crop.x = 0;
1633 format->es->video.crop.y = 0;
1634 format->es->video.crop.width = 1024;
1635 format->es->video.crop.height = 768;
1636 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1637 format->es->video.frame_rate.den = 1;
1639 format = &camera->output[CAM_PORT_CAPTURE].format;
1641 format->encoding = MMAL_ENCODING_OPAQUE;
1643 format->es->video.width = 2592;
1644 format->es->video.height = 1944;
1645 format->es->video.crop.x = 0;
1646 format->es->video.crop.y = 0;
1647 format->es->video.crop.width = 2592;
1648 format->es->video.crop.height = 1944;
1649 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1650 format->es->video.frame_rate.den = 1;
1652 dev->capture.width = format->es->video.width;
1653 dev->capture.height = format->es->video.height;
1654 dev->capture.fmt = &formats[0];
1655 dev->capture.encode_component = NULL;
1656 dev->capture.timeperframe = tpf_default;
1657 dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
1658 dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
1660 /* get the preview component ready */
1661 ret = vchiq_mmal_component_init(
1662 dev->instance, "ril.video_render",
1663 &dev->component[COMP_PREVIEW]);
1667 if (dev->component[COMP_PREVIEW]->inputs < 1) {
1669 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1670 __func__, dev->component[COMP_PREVIEW]->inputs, 1);
1674 /* get the image encoder component ready */
1675 ret = vchiq_mmal_component_init(
1676 dev->instance, "ril.image_encode",
1677 &dev->component[COMP_IMAGE_ENCODE]);
1681 if (dev->component[COMP_IMAGE_ENCODE]->inputs < 1) {
1683 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1684 __func__, dev->component[COMP_IMAGE_ENCODE]->inputs,
1686 goto unreg_image_encoder;
1689 /* get the video encoder component ready */
1690 ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
1691 &dev->component[COMP_VIDEO_ENCODE]);
1693 goto unreg_image_encoder;
1695 if (dev->component[COMP_VIDEO_ENCODE]->inputs < 1) {
1697 v4l2_err(&dev->v4l2_dev, "%s: too few input ports %d needed %d\n",
1698 __func__, dev->component[COMP_VIDEO_ENCODE]->inputs,
1700 goto unreg_vid_encoder;
1704 struct vchiq_mmal_port *encoder_port =
1705 &dev->component[COMP_VIDEO_ENCODE]->output[0];
1706 encoder_port->format.encoding = MMAL_ENCODING_H264;
1707 ret = vchiq_mmal_port_set_format(dev->instance,
1712 unsigned int enable = 1;
1714 vchiq_mmal_port_parameter_set(
1716 &dev->component[COMP_VIDEO_ENCODE]->control,
1717 MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
1718 &enable, sizeof(enable));
1720 vchiq_mmal_port_parameter_set(dev->instance,
1721 &dev->component[COMP_VIDEO_ENCODE]->control,
1722 MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
1726 ret = bm2835_mmal_set_all_camera_controls(dev);
1728 v4l2_err(&dev->v4l2_dev, "%s: failed to set all camera controls: %d\n",
1730 goto unreg_vid_encoder;
1736 pr_err("Cleanup: Destroy video encoder\n");
1737 vchiq_mmal_component_finalise(
1739 dev->component[COMP_VIDEO_ENCODE]);
1741 unreg_image_encoder:
1742 pr_err("Cleanup: Destroy image encoder\n");
1743 vchiq_mmal_component_finalise(
1745 dev->component[COMP_IMAGE_ENCODE]);
1748 pr_err("Cleanup: Destroy video render\n");
1749 vchiq_mmal_component_finalise(dev->instance,
1750 dev->component[COMP_PREVIEW]);
1753 pr_err("Cleanup: Destroy camera\n");
1754 vchiq_mmal_component_finalise(dev->instance,
1755 dev->component[COMP_CAMERA]);
1758 vchiq_mmal_finalise(dev->instance);
1762 static int bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
1763 struct video_device *vfd)
1767 *vfd = vdev_template;
1769 vfd->v4l2_dev = &dev->v4l2_dev;
1771 vfd->lock = &dev->mutex;
1773 vfd->queue = &dev->capture.vb_vidq;
1775 /* video device needs to be able to access instance data */
1776 video_set_drvdata(vfd, dev);
1778 ret = video_register_device(vfd,
1780 video_nr[dev->camera_num]);
1784 v4l2_info(vfd->v4l2_dev,
1785 "V4L2 device registered as %s - stills mode > %dx%d\n",
1786 video_device_node_name(vfd),
1787 max_video_width, max_video_height);
1792 static void bcm2835_cleanup_instance(struct bm2835_mmal_dev *dev)
1797 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1798 video_device_node_name(&dev->vdev));
1800 video_unregister_device(&dev->vdev);
1802 if (dev->capture.encode_component) {
1803 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1804 "mmal_exit - disconnect tunnel\n");
1805 vchiq_mmal_port_connect_tunnel(dev->instance,
1806 dev->capture.camera_port, NULL);
1807 vchiq_mmal_component_disable(dev->instance,
1808 dev->capture.encode_component);
1810 vchiq_mmal_component_disable(dev->instance,
1811 dev->component[COMP_CAMERA]);
1813 vchiq_mmal_component_finalise(dev->instance,
1814 dev->component[COMP_VIDEO_ENCODE]);
1816 vchiq_mmal_component_finalise(dev->instance,
1817 dev->component[COMP_IMAGE_ENCODE]);
1819 vchiq_mmal_component_finalise(dev->instance,
1820 dev->component[COMP_PREVIEW]);
1822 vchiq_mmal_component_finalise(dev->instance,
1823 dev->component[COMP_CAMERA]);
1825 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1827 v4l2_device_unregister(&dev->v4l2_dev);
1832 static struct v4l2_format default_v4l2_format = {
1833 .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
1834 .fmt.pix.width = 1024,
1835 .fmt.pix.bytesperline = 0,
1836 .fmt.pix.height = 768,
1837 .fmt.pix.sizeimage = 1024 * 768,
1840 static int bcm2835_mmal_probe(struct platform_device *pdev)
1843 struct bm2835_mmal_dev *dev;
1844 struct vb2_queue *q;
1846 unsigned int num_cameras;
1847 struct vchiq_mmal_instance *instance;
1848 unsigned int resolutions[MAX_BCM2835_CAMERAS][2];
1851 ret = vchiq_mmal_init(&instance);
1855 num_cameras = get_num_cameras(instance,
1857 MAX_BCM2835_CAMERAS);
1859 if (num_cameras < 1) {
1864 if (num_cameras > MAX_BCM2835_CAMERAS)
1865 num_cameras = MAX_BCM2835_CAMERAS;
1867 for (camera = 0; camera < num_cameras; camera++) {
1868 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1874 /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
1875 mutex_init(&dev->mutex);
1876 dev->max_width = resolutions[camera][0];
1877 dev->max_height = resolutions[camera][1];
1879 /* setup device defaults */
1880 dev->overlay.w.left = 150;
1881 dev->overlay.w.top = 50;
1882 dev->overlay.w.width = 1024;
1883 dev->overlay.w.height = 768;
1884 dev->overlay.clipcount = 0;
1885 dev->overlay.field = V4L2_FIELD_NONE;
1886 dev->overlay.global_alpha = 255;
1888 dev->capture.fmt = &formats[3]; /* JPEG */
1890 /* v4l device registration */
1891 dev->camera_num = v4l2_device_set_name(&dev->v4l2_dev,
1892 BM2835_MMAL_MODULE_NAME,
1894 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1896 dev_err(&pdev->dev, "%s: could not register V4L2 device: %d\n",
1901 /* setup v4l controls */
1902 ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
1904 v4l2_err(&dev->v4l2_dev, "%s: could not init controls: %d\n",
1908 dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
1911 dev->instance = instance;
1912 ret = mmal_init(dev);
1914 v4l2_err(&dev->v4l2_dev, "%s: mmal init failed: %d\n",
1918 /* initialize queue */
1919 q = &dev->capture.vb_vidq;
1920 memset(q, 0, sizeof(*q));
1921 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1922 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1924 q->buf_struct_size = sizeof(struct mmal_buffer);
1925 q->ops = &bm2835_mmal_video_qops;
1926 q->mem_ops = &vb2_vmalloc_memops;
1927 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1928 q->lock = &dev->mutex;
1929 ret = vb2_queue_init(q);
1933 /* initialise video devices */
1934 ret = bm2835_mmal_init_device(dev, &dev->vdev);
1936 v4l2_err(&dev->v4l2_dev, "%s: could not init device: %d\n",
1941 /* Really want to call vidioc_s_fmt_vid_cap with the default
1942 * format, but currently the APIs don't join up.
1944 ret = mmal_setup_components(dev, &default_v4l2_format);
1946 v4l2_err(&dev->v4l2_dev, "%s: could not setup components: %d\n",
1951 v4l2_info(&dev->v4l2_dev,
1952 "Broadcom 2835 MMAL video capture ver %s loaded.\n",
1953 BM2835_MMAL_VERSION);
1960 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1961 v4l2_device_unregister(&dev->v4l2_dev);
1967 for (i = 0; i < camera; i++) {
1968 bcm2835_cleanup_instance(gdev[i]);
1973 vchiq_mmal_finalise(instance);
1978 static int bcm2835_mmal_remove(struct platform_device *pdev)
1981 struct vchiq_mmal_instance *instance = gdev[0]->instance;
1983 for (camera = 0; camera < MAX_BCM2835_CAMERAS; camera++) {
1984 bcm2835_cleanup_instance(gdev[camera]);
1985 gdev[camera] = NULL;
1987 vchiq_mmal_finalise(instance);
1992 static struct platform_driver bcm2835_camera_driver = {
1993 .probe = bcm2835_mmal_probe,
1994 .remove = bcm2835_mmal_remove,
1996 .name = "bcm2835-camera",
2000 module_platform_driver(bcm2835_camera_driver)
2002 MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
2003 MODULE_AUTHOR("Vincent Sanders");
2004 MODULE_LICENSE("GPL");
2005 MODULE_VERSION(BM2835_MMAL_VERSION);
2006 MODULE_ALIAS("platform:bcm2835-camera");