1 // SPDX-License-Identifier: GPL-2.0
3 * Broadcom BM2835 V4L2 driver
5 * Copyright © 2013 Raspberry Pi (Trading) Ltd.
7 * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
8 * Dave Stevenson <dsteve@broadcom.com>
9 * Simon Mellor <simellor@broadcom.com>
10 * Luke Diamand <luked@broadcom.com>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <media/videobuf2-vmalloc.h>
18 #include <media/videobuf2-dma-contig.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-ioctl.h>
21 #include <media/v4l2-ctrls.h>
22 #include <media/v4l2-fh.h>
23 #include <media/v4l2-event.h>
24 #include <media/v4l2-common.h>
25 #include <linux/delay.h>
26 #include <linux/platform_device.h>
28 #include "mmal-common.h"
29 #include "mmal-encodings.h"
30 #include "mmal-vchiq.h"
32 #include "mmal-parameters.h"
33 #include "bcm2835-camera.h"
35 #define BM2835_MMAL_VERSION "0.0.2"
36 #define BM2835_MMAL_MODULE_NAME "bcm2835-v4l2"
39 #define MIN_BUFFER_SIZE (80 * 1024)
41 #define MAX_VIDEO_MODE_WIDTH 1280
42 #define MAX_VIDEO_MODE_HEIGHT 720
44 #define MAX_BCM2835_CAMERAS 2
46 MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
47 MODULE_AUTHOR("Vincent Sanders");
48 MODULE_LICENSE("GPL");
49 MODULE_VERSION(BM2835_MMAL_VERSION);
50 MODULE_ALIAS("platform:bcm2835-camera");
52 int bcm2835_v4l2_debug;
53 module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
54 MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
57 static int video_nr[] = {[0 ... (MAX_BCM2835_CAMERAS - 1)] = UNSET };
58 module_param_array(video_nr, int, NULL, 0644);
59 MODULE_PARM_DESC(video_nr, "videoX start numbers, -1 is autodetect");
61 static int max_video_width = MAX_VIDEO_MODE_WIDTH;
62 static int max_video_height = MAX_VIDEO_MODE_HEIGHT;
63 module_param(max_video_width, int, 0644);
64 MODULE_PARM_DESC(max_video_width, "Threshold for video mode");
65 module_param(max_video_height, int, 0644);
66 MODULE_PARM_DESC(max_video_height, "Threshold for video mode");
68 /* global device data array */
69 static struct bm2835_mmal_dev *gdev[MAX_BCM2835_CAMERAS];
74 /* timeperframe: min/max and default */
75 static const struct v4l2_fract
76 tpf_min = {.numerator = 1, .denominator = FPS_MAX},
77 tpf_max = {.numerator = 1, .denominator = FPS_MIN},
78 tpf_default = {.numerator = 1000, .denominator = 30000};
81 static struct mmal_fmt formats[] = {
83 .name = "4:2:0, planar, YUV",
84 .fourcc = V4L2_PIX_FMT_YUV420,
86 .mmal = MMAL_ENCODING_I420,
88 .mmal_component = MMAL_COMPONENT_CAMERA,
92 .name = "4:2:2, packed, YUYV",
93 .fourcc = V4L2_PIX_FMT_YUYV,
95 .mmal = MMAL_ENCODING_YUYV,
97 .mmal_component = MMAL_COMPONENT_CAMERA,
101 .name = "RGB24 (LE)",
102 .fourcc = V4L2_PIX_FMT_RGB24,
104 .mmal = MMAL_ENCODING_RGB24,
106 .mmal_component = MMAL_COMPONENT_CAMERA,
111 .fourcc = V4L2_PIX_FMT_JPEG,
112 .flags = V4L2_FMT_FLAG_COMPRESSED,
113 .mmal = MMAL_ENCODING_JPEG,
115 .mmal_component = MMAL_COMPONENT_IMAGE_ENCODE,
120 .fourcc = V4L2_PIX_FMT_H264,
121 .flags = V4L2_FMT_FLAG_COMPRESSED,
122 .mmal = MMAL_ENCODING_H264,
124 .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
129 .fourcc = V4L2_PIX_FMT_MJPEG,
130 .flags = V4L2_FMT_FLAG_COMPRESSED,
131 .mmal = MMAL_ENCODING_MJPEG,
133 .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
137 .name = "4:2:2, packed, YVYU",
138 .fourcc = V4L2_PIX_FMT_YVYU,
140 .mmal = MMAL_ENCODING_YVYU,
142 .mmal_component = MMAL_COMPONENT_CAMERA,
146 .name = "4:2:2, packed, VYUY",
147 .fourcc = V4L2_PIX_FMT_VYUY,
149 .mmal = MMAL_ENCODING_VYUY,
151 .mmal_component = MMAL_COMPONENT_CAMERA,
155 .name = "4:2:2, packed, UYVY",
156 .fourcc = V4L2_PIX_FMT_UYVY,
158 .mmal = MMAL_ENCODING_UYVY,
160 .mmal_component = MMAL_COMPONENT_CAMERA,
164 .name = "4:2:0, planar, NV12",
165 .fourcc = V4L2_PIX_FMT_NV12,
167 .mmal = MMAL_ENCODING_NV12,
169 .mmal_component = MMAL_COMPONENT_CAMERA,
173 .name = "RGB24 (BE)",
174 .fourcc = V4L2_PIX_FMT_BGR24,
176 .mmal = MMAL_ENCODING_BGR24,
178 .mmal_component = MMAL_COMPONENT_CAMERA,
182 .name = "4:2:0, planar, YVU",
183 .fourcc = V4L2_PIX_FMT_YVU420,
185 .mmal = MMAL_ENCODING_YV12,
187 .mmal_component = MMAL_COMPONENT_CAMERA,
191 .name = "4:2:0, planar, NV21",
192 .fourcc = V4L2_PIX_FMT_NV21,
194 .mmal = MMAL_ENCODING_NV21,
196 .mmal_component = MMAL_COMPONENT_CAMERA,
200 .name = "RGB32 (BE)",
201 .fourcc = V4L2_PIX_FMT_BGR32,
203 .mmal = MMAL_ENCODING_BGRA,
205 .mmal_component = MMAL_COMPONENT_CAMERA,
211 static struct mmal_fmt *get_format(struct v4l2_format *f)
213 struct mmal_fmt *fmt;
216 for (k = 0; k < ARRAY_SIZE(formats); k++) {
218 if (fmt->fourcc == f->fmt.pix.pixelformat)
225 /* ------------------------------------------------------------------
226 * Videobuf queue operations
227 * ------------------------------------------------------------------
230 static int queue_setup(struct vb2_queue *vq,
231 unsigned int *nbuffers, unsigned int *nplanes,
232 unsigned int sizes[], struct device *alloc_ctxs[])
234 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
237 /* refuse queue setup if port is not configured */
238 if (!dev->capture.port) {
239 v4l2_err(&dev->v4l2_dev,
240 "%s: capture port not configured\n", __func__);
244 size = dev->capture.port->current_buffer.size;
246 v4l2_err(&dev->v4l2_dev,
247 "%s: capture port buffer size is zero\n", __func__);
251 if (*nbuffers < dev->capture.port->minimum_buffer.num)
252 *nbuffers = dev->capture.port->minimum_buffer.num;
254 dev->capture.port->current_buffer.num = *nbuffers;
261 * videobuf2-vmalloc allocator is context-less so no need to set
265 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
271 static int buffer_init(struct vb2_buffer *vb)
273 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
274 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
275 struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
277 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
279 buf->buffer = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
280 buf->buffer_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
282 return mmal_vchi_buffer_init(dev->instance, buf);
285 static int buffer_prepare(struct vb2_buffer *vb)
287 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
290 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
293 if (!dev->capture.port || !dev->capture.fmt)
296 size = dev->capture.stride * dev->capture.height;
297 if (vb2_plane_size(vb, 0) < size) {
298 v4l2_err(&dev->v4l2_dev,
299 "%s data will not fit into plane (%lu < %lu)\n",
300 __func__, vb2_plane_size(vb, 0), size);
307 static void buffer_cleanup(struct vb2_buffer *vb)
309 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
310 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
311 struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
313 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p, vb %p\n",
315 mmal_vchi_buffer_cleanup(buf);
318 static inline bool is_capturing(struct bm2835_mmal_dev *dev)
320 return dev->capture.camera_port ==
322 component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
325 static void buffer_cb(struct vchiq_mmal_instance *instance,
326 struct vchiq_mmal_port *port,
328 struct mmal_buffer *buf,
329 unsigned long length, u32 mmal_flags, s64 dts, s64 pts)
331 struct bm2835_mmal_dev *dev = port->cb_ctx;
333 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
334 "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
335 __func__, status, buf, length, mmal_flags, pts);
338 /* error in transfer */
340 /* there was a buffer with the error so return it */
341 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
344 } else if (length == 0) {
347 /* this should only ever happen if the port is
348 * disabled and there are buffers still queued
350 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
351 pr_debug("Empty buffer");
352 } else if (dev->capture.frame_count) {
353 /* grab another frame */
354 if (is_capturing(dev)) {
355 pr_debug("Grab another frame");
356 vchiq_mmal_port_parameter_set(
358 dev->capture.camera_port,
359 MMAL_PARAMETER_CAPTURE,
360 &dev->capture.frame_count,
361 sizeof(dev->capture.frame_count));
364 /* signal frame completion */
365 complete(&dev->capture.frame_cmplt);
368 if (dev->capture.frame_count) {
369 if (dev->capture.vc_start_timestamp != -1 &&
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();
386 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, length);
387 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
389 if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
391 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
392 "Grab another frame as buffer has EOS");
393 vchiq_mmal_port_parameter_set(
395 dev->capture.camera_port,
396 MMAL_PARAMETER_CAPTURE,
397 &dev->capture.frame_count,
398 sizeof(dev->capture.frame_count));
401 /* signal frame completion */
402 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
403 complete(&dev->capture.frame_cmplt);
408 static int enable_camera(struct bm2835_mmal_dev *dev)
412 if (!dev->camera_use_count) {
413 ret = vchiq_mmal_port_parameter_set(
415 &dev->component[MMAL_COMPONENT_CAMERA]->control,
416 MMAL_PARAMETER_CAMERA_NUM, &dev->camera_num,
417 sizeof(dev->camera_num));
419 v4l2_err(&dev->v4l2_dev,
420 "Failed setting camera num, ret %d\n", ret);
424 ret = vchiq_mmal_component_enable(
426 dev->component[MMAL_COMPONENT_CAMERA]);
428 v4l2_err(&dev->v4l2_dev,
429 "Failed enabling camera, ret %d\n", ret);
433 dev->camera_use_count++;
434 v4l2_dbg(1, bcm2835_v4l2_debug,
435 &dev->v4l2_dev, "enabled camera (refcount %d)\n",
436 dev->camera_use_count);
440 static int disable_camera(struct bm2835_mmal_dev *dev)
444 if (!dev->camera_use_count) {
445 v4l2_err(&dev->v4l2_dev,
446 "Disabled the camera when already disabled\n");
449 dev->camera_use_count--;
450 if (!dev->camera_use_count) {
451 unsigned int i = 0xFFFFFFFF;
453 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
454 "Disabling camera\n");
456 vchiq_mmal_component_disable(
458 dev->component[MMAL_COMPONENT_CAMERA]);
460 v4l2_err(&dev->v4l2_dev,
461 "Failed disabling camera, ret %d\n", ret);
464 vchiq_mmal_port_parameter_set(
466 &dev->component[MMAL_COMPONENT_CAMERA]->control,
467 MMAL_PARAMETER_CAMERA_NUM, &i,
470 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
471 "Camera refcount now %d\n", dev->camera_use_count);
475 static void buffer_queue(struct vb2_buffer *vb)
477 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
478 struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
479 struct mmal_buffer *buf = container_of(vb2, struct mmal_buffer, vb);
482 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
483 "%s: dev:%p buf:%p, idx %u\n",
484 __func__, dev, buf, vb2->vb2_buf.index);
486 ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
488 v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
492 static int start_streaming(struct vb2_queue *vq, unsigned int count)
494 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
498 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
501 /* ensure a format has actually been set */
502 if (!dev->capture.port)
505 if (enable_camera(dev) < 0) {
506 v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
510 /*init_completion(&dev->capture.frame_cmplt); */
512 /* enable frame capture */
513 dev->capture.frame_count = 1;
515 /* if the preview is not already running, wait for a few frames for AGC
518 if (!dev->component[MMAL_COMPONENT_PREVIEW]->enabled)
521 /* enable the connection from camera to encoder (if applicable) */
522 if (dev->capture.camera_port != dev->capture.port
523 && dev->capture.camera_port) {
524 ret = vchiq_mmal_port_enable(dev->instance,
525 dev->capture.camera_port, NULL);
527 v4l2_err(&dev->v4l2_dev,
528 "Failed to enable encode tunnel - error %d\n",
534 /* Get VC timestamp at this point in time */
535 parameter_size = sizeof(dev->capture.vc_start_timestamp);
536 if (vchiq_mmal_port_parameter_get(dev->instance,
537 dev->capture.camera_port,
538 MMAL_PARAMETER_SYSTEM_TIME,
539 &dev->capture.vc_start_timestamp,
541 v4l2_err(&dev->v4l2_dev,
542 "Failed to get VC start time - update your VC f/w\n");
544 /* Flag to indicate just to rely on kernel timestamps */
545 dev->capture.vc_start_timestamp = -1;
547 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
548 "Start time %lld size %d\n",
549 dev->capture.vc_start_timestamp, parameter_size);
551 dev->capture.kernel_start_ts = ktime_get();
553 /* enable the camera port */
554 dev->capture.port->cb_ctx = dev;
556 vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
558 v4l2_err(&dev->v4l2_dev,
559 "Failed to enable capture port - error %d. Disabling camera port again\n",
562 vchiq_mmal_port_disable(dev->instance,
563 dev->capture.camera_port);
564 if (disable_camera(dev) < 0) {
565 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
571 /* capture the first frame */
572 vchiq_mmal_port_parameter_set(dev->instance,
573 dev->capture.camera_port,
574 MMAL_PARAMETER_CAPTURE,
575 &dev->capture.frame_count,
576 sizeof(dev->capture.frame_count));
580 /* abort streaming and wait for last buffer */
581 static void stop_streaming(struct vb2_queue *vq)
584 unsigned long timeout;
585 struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
587 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
590 init_completion(&dev->capture.frame_cmplt);
591 dev->capture.frame_count = 0;
593 /* ensure a format has actually been set */
594 if (!dev->capture.port) {
595 v4l2_err(&dev->v4l2_dev,
596 "no capture port - stream not started?\n");
600 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
602 /* stop capturing frames */
603 vchiq_mmal_port_parameter_set(dev->instance,
604 dev->capture.camera_port,
605 MMAL_PARAMETER_CAPTURE,
606 &dev->capture.frame_count,
607 sizeof(dev->capture.frame_count));
609 /* wait for last frame to complete */
610 timeout = wait_for_completion_timeout(&dev->capture.frame_cmplt, HZ);
612 v4l2_err(&dev->v4l2_dev,
613 "timed out waiting for frame completion\n");
615 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
616 "disabling connection\n");
618 /* disable the connection from camera to encoder */
619 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
620 if (!ret && dev->capture.camera_port != dev->capture.port) {
621 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
623 ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
624 } else if (dev->capture.camera_port != dev->capture.port) {
625 v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
629 if (disable_camera(dev) < 0)
630 v4l2_err(&dev->v4l2_dev, "Failed to disable camera\n");
633 static const struct vb2_ops bm2835_mmal_video_qops = {
634 .queue_setup = queue_setup,
635 .buf_init = buffer_init,
636 .buf_prepare = buffer_prepare,
637 .buf_cleanup = buffer_cleanup,
638 .buf_queue = buffer_queue,
639 .start_streaming = start_streaming,
640 .stop_streaming = stop_streaming,
641 .wait_prepare = vb2_ops_wait_prepare,
642 .wait_finish = vb2_ops_wait_finish,
645 /* ------------------------------------------------------------------
647 * ------------------------------------------------------------------
650 static int set_overlay_params(struct bm2835_mmal_dev *dev,
651 struct vchiq_mmal_port *port)
653 struct mmal_parameter_displayregion prev_config = {
654 .set = MMAL_DISPLAY_SET_LAYER |
655 MMAL_DISPLAY_SET_ALPHA |
656 MMAL_DISPLAY_SET_DEST_RECT |
657 MMAL_DISPLAY_SET_FULLSCREEN,
658 .layer = PREVIEW_LAYER,
659 .alpha = dev->overlay.global_alpha,
662 .x = dev->overlay.w.left,
663 .y = dev->overlay.w.top,
664 .width = dev->overlay.w.width,
665 .height = dev->overlay.w.height,
668 return vchiq_mmal_port_parameter_set(dev->instance, port,
669 MMAL_PARAMETER_DISPLAYREGION,
670 &prev_config, sizeof(prev_config));
674 static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
675 struct v4l2_fmtdesc *f)
677 struct mmal_fmt *fmt;
679 if (f->index >= ARRAY_SIZE(formats))
682 fmt = &formats[f->index];
684 strlcpy((char *)f->description, fmt->name, sizeof(f->description));
685 f->pixelformat = fmt->fourcc;
686 f->flags = fmt->flags;
691 static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
692 struct v4l2_format *f)
694 struct bm2835_mmal_dev *dev = video_drvdata(file);
696 f->fmt.win = dev->overlay;
701 static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
702 struct v4l2_format *f)
704 struct bm2835_mmal_dev *dev = video_drvdata(file);
706 f->fmt.win.field = V4L2_FIELD_NONE;
707 f->fmt.win.chromakey = 0;
708 f->fmt.win.clips = NULL;
709 f->fmt.win.clipcount = 0;
710 f->fmt.win.bitmap = NULL;
712 v4l_bound_align_image(&f->fmt.win.w.width, MIN_WIDTH, dev->max_width, 1,
713 &f->fmt.win.w.height, MIN_HEIGHT, dev->max_height,
715 v4l_bound_align_image(&f->fmt.win.w.left, MIN_WIDTH, dev->max_width, 1,
716 &f->fmt.win.w.top, MIN_HEIGHT, dev->max_height,
719 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
720 "Overlay: Now w/h %dx%d l/t %dx%d\n",
721 f->fmt.win.w.width, f->fmt.win.w.height,
722 f->fmt.win.w.left, f->fmt.win.w.top);
724 v4l2_dump_win_format(1,
732 static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
733 struct v4l2_format *f)
735 struct bm2835_mmal_dev *dev = video_drvdata(file);
737 vidioc_try_fmt_vid_overlay(file, priv, f);
739 dev->overlay = f->fmt.win;
740 if (dev->component[MMAL_COMPONENT_PREVIEW]->enabled) {
741 set_overlay_params(dev,
742 &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
748 static int vidioc_overlay(struct file *file, void *f, unsigned int on)
751 struct bm2835_mmal_dev *dev = video_drvdata(file);
752 struct vchiq_mmal_port *src;
753 struct vchiq_mmal_port *dst;
755 if ((on && dev->component[MMAL_COMPONENT_PREVIEW]->enabled) ||
756 (!on && !dev->component[MMAL_COMPONENT_PREVIEW]->enabled))
757 return 0; /* already in requested state */
760 &dev->component[MMAL_COMPONENT_CAMERA]->
761 output[MMAL_CAMERA_PORT_PREVIEW];
764 /* disconnect preview ports and disable component */
765 ret = vchiq_mmal_port_disable(dev->instance, src);
768 vchiq_mmal_port_connect_tunnel(dev->instance, src,
771 ret = vchiq_mmal_component_disable(
773 dev->component[MMAL_COMPONENT_PREVIEW]);
779 /* set preview port format and connect it to output */
780 dst = &dev->component[MMAL_COMPONENT_PREVIEW]->input[0];
782 ret = vchiq_mmal_port_set_format(dev->instance, src);
786 ret = set_overlay_params(dev, dst);
790 if (enable_camera(dev) < 0)
793 ret = vchiq_mmal_component_enable(
795 dev->component[MMAL_COMPONENT_PREVIEW]);
799 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
801 ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
803 ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
808 static int vidioc_g_fbuf(struct file *file, void *fh,
809 struct v4l2_framebuffer *a)
811 /* The video overlay must stay within the framebuffer and can't be
812 * positioned independently.
814 struct bm2835_mmal_dev *dev = video_drvdata(file);
815 struct vchiq_mmal_port *preview_port =
816 &dev->component[MMAL_COMPONENT_CAMERA]->
817 output[MMAL_CAMERA_PORT_PREVIEW];
819 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
820 V4L2_FBUF_CAP_GLOBAL_ALPHA;
821 a->flags = V4L2_FBUF_FLAG_OVERLAY;
822 a->fmt.width = preview_port->es.video.width;
823 a->fmt.height = preview_port->es.video.height;
824 a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
825 a->fmt.bytesperline = preview_port->es.video.width;
826 a->fmt.sizeimage = (preview_port->es.video.width *
827 preview_port->es.video.height * 3) >> 1;
828 a->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
834 static int vidioc_enum_input(struct file *file, void *priv,
835 struct v4l2_input *inp)
837 /* only a single camera input */
841 inp->type = V4L2_INPUT_TYPE_CAMERA;
842 sprintf((char *)inp->name, "Camera %u", inp->index);
846 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
852 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
861 static int vidioc_querycap(struct file *file, void *priv,
862 struct v4l2_capability *cap)
864 struct bm2835_mmal_dev *dev = video_drvdata(file);
868 vchiq_mmal_version(dev->instance, &major, &minor);
870 strcpy((char *)cap->driver, "bm2835 mmal");
871 snprintf((char *)cap->card, sizeof(cap->card), "mmal service %d.%d",
874 snprintf((char *)cap->bus_info, sizeof(cap->bus_info),
875 "platform:%s", dev->v4l2_dev.name);
876 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
877 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
878 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
883 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
884 struct v4l2_fmtdesc *f)
886 struct mmal_fmt *fmt;
888 if (f->index >= ARRAY_SIZE(formats))
891 fmt = &formats[f->index];
893 strlcpy((char *)f->description, fmt->name, sizeof(f->description));
894 f->pixelformat = fmt->fourcc;
895 f->flags = fmt->flags;
900 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
901 struct v4l2_format *f)
903 struct bm2835_mmal_dev *dev = video_drvdata(file);
905 f->fmt.pix.width = dev->capture.width;
906 f->fmt.pix.height = dev->capture.height;
907 f->fmt.pix.field = V4L2_FIELD_NONE;
908 f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
909 f->fmt.pix.bytesperline = dev->capture.stride;
910 f->fmt.pix.sizeimage = dev->capture.buffersize;
912 if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
913 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
914 else if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG)
915 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
917 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
920 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
925 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
926 struct v4l2_format *f)
928 struct bm2835_mmal_dev *dev = video_drvdata(file);
929 struct mmal_fmt *mfmt;
931 mfmt = get_format(f);
933 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
934 "Fourcc format (0x%08x) unknown.\n",
935 f->fmt.pix.pixelformat);
936 f->fmt.pix.pixelformat = formats[0].fourcc;
937 mfmt = get_format(f);
940 f->fmt.pix.field = V4L2_FIELD_NONE;
942 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
943 "Clipping/aligning %dx%d format %08X\n",
944 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
946 v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, dev->max_width, 1,
947 &f->fmt.pix.height, MIN_HEIGHT, dev->max_height,
949 f->fmt.pix.bytesperline = f->fmt.pix.width * mfmt->ybbp;
950 if (!mfmt->remove_padding) {
951 int align_mask = ((32 * mfmt->depth) >> 3) - 1;
952 /* GPU isn't removing padding, so stride is aligned to 32 */
953 f->fmt.pix.bytesperline =
954 (f->fmt.pix.bytesperline + align_mask) & ~align_mask;
955 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
956 "Not removing padding, so bytes/line = %d, "
958 f->fmt.pix.bytesperline, align_mask);
961 /* Image buffer has to be padded to allow for alignment, even though
962 * we sometimes then remove that padding before delivering the buffer.
964 f->fmt.pix.sizeimage = ((f->fmt.pix.height + 15) & ~15) *
965 (((f->fmt.pix.width + 31) & ~31) * mfmt->depth) >> 3;
967 if ((mfmt->flags & V4L2_FMT_FLAG_COMPRESSED) &&
968 f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
969 f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
971 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24)
972 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
973 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
974 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
976 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
979 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
980 "Now %dx%d format %08X\n",
981 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
983 v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
988 static int mmal_setup_components(struct bm2835_mmal_dev *dev,
989 struct v4l2_format *f)
992 struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
993 struct vchiq_mmal_component *encode_component = NULL;
994 struct mmal_fmt *mfmt = get_format(f);
1000 if (dev->capture.encode_component) {
1001 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1002 "vid_cap - disconnect previous tunnel\n");
1004 /* Disconnect any previous connection */
1005 vchiq_mmal_port_connect_tunnel(dev->instance,
1006 dev->capture.camera_port, NULL);
1007 dev->capture.camera_port = NULL;
1008 ret = vchiq_mmal_component_disable(dev->instance,
1012 v4l2_err(&dev->v4l2_dev,
1013 "Failed to disable encode component %d\n",
1016 dev->capture.encode_component = NULL;
1018 /* format dependent port setup */
1019 switch (mfmt->mmal_component) {
1020 case MMAL_COMPONENT_CAMERA:
1021 /* Make a further decision on port based on resolution */
1022 if (f->fmt.pix.width <= max_video_width
1023 && f->fmt.pix.height <= max_video_height)
1024 camera_port = port =
1025 &dev->component[MMAL_COMPONENT_CAMERA]->
1026 output[MMAL_CAMERA_PORT_VIDEO];
1028 camera_port = port =
1029 &dev->component[MMAL_COMPONENT_CAMERA]->
1030 output[MMAL_CAMERA_PORT_CAPTURE];
1032 case MMAL_COMPONENT_IMAGE_ENCODE:
1033 encode_component = dev->component[MMAL_COMPONENT_IMAGE_ENCODE];
1034 port = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
1036 &dev->component[MMAL_COMPONENT_CAMERA]->
1037 output[MMAL_CAMERA_PORT_CAPTURE];
1039 case MMAL_COMPONENT_VIDEO_ENCODE:
1040 encode_component = dev->component[MMAL_COMPONENT_VIDEO_ENCODE];
1041 port = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
1043 &dev->component[MMAL_COMPONENT_CAMERA]->
1044 output[MMAL_CAMERA_PORT_VIDEO];
1053 if (encode_component)
1054 camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
1056 camera_port->format.encoding = mfmt->mmal;
1058 if (dev->rgb_bgr_swapped) {
1059 if (camera_port->format.encoding == MMAL_ENCODING_RGB24)
1060 camera_port->format.encoding = MMAL_ENCODING_BGR24;
1061 else if (camera_port->format.encoding == MMAL_ENCODING_BGR24)
1062 camera_port->format.encoding = MMAL_ENCODING_RGB24;
1065 remove_padding = mfmt->remove_padding;
1066 vchiq_mmal_port_parameter_set(dev->instance,
1068 MMAL_PARAMETER_NO_IMAGE_PADDING,
1069 &remove_padding, sizeof(remove_padding));
1071 camera_port->format.encoding_variant = 0;
1072 camera_port->es.video.width = f->fmt.pix.width;
1073 camera_port->es.video.height = f->fmt.pix.height;
1074 camera_port->es.video.crop.x = 0;
1075 camera_port->es.video.crop.y = 0;
1076 camera_port->es.video.crop.width = f->fmt.pix.width;
1077 camera_port->es.video.crop.height = f->fmt.pix.height;
1078 camera_port->es.video.frame_rate.num = 0;
1079 camera_port->es.video.frame_rate.den = 1;
1080 camera_port->es.video.color_space = MMAL_COLOR_SPACE_JPEG_JFIF;
1082 ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
1086 &dev->component[MMAL_COMPONENT_CAMERA]->
1087 output[MMAL_CAMERA_PORT_VIDEO]) {
1088 bool overlay_enabled =
1089 !!dev->component[MMAL_COMPONENT_PREVIEW]->enabled;
1090 struct vchiq_mmal_port *preview_port =
1091 &dev->component[MMAL_COMPONENT_CAMERA]->
1092 output[MMAL_CAMERA_PORT_PREVIEW];
1093 /* Preview and encode ports need to match on resolution */
1094 if (overlay_enabled) {
1095 /* Need to disable the overlay before we can update
1099 vchiq_mmal_port_disable(dev->instance,
1103 vchiq_mmal_port_connect_tunnel(
1108 preview_port->es.video.width = f->fmt.pix.width;
1109 preview_port->es.video.height = f->fmt.pix.height;
1110 preview_port->es.video.crop.x = 0;
1111 preview_port->es.video.crop.y = 0;
1112 preview_port->es.video.crop.width = f->fmt.pix.width;
1113 preview_port->es.video.crop.height = f->fmt.pix.height;
1114 preview_port->es.video.frame_rate.num =
1115 dev->capture.timeperframe.denominator;
1116 preview_port->es.video.frame_rate.den =
1117 dev->capture.timeperframe.numerator;
1118 ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
1119 if (overlay_enabled) {
1120 ret = vchiq_mmal_port_connect_tunnel(
1123 &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
1125 ret = vchiq_mmal_port_enable(dev->instance,
1132 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1133 "%s failed to set format %dx%d %08X\n", __func__,
1134 f->fmt.pix.width, f->fmt.pix.height,
1135 f->fmt.pix.pixelformat);
1136 /* ensure capture is not going to be tried */
1137 dev->capture.port = NULL;
1139 if (encode_component) {
1140 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1141 "vid_cap - set up encode comp\n");
1143 /* configure buffering */
1144 camera_port->current_buffer.size =
1145 camera_port->recommended_buffer.size;
1146 camera_port->current_buffer.num =
1147 camera_port->recommended_buffer.num;
1150 vchiq_mmal_port_connect_tunnel(
1153 &encode_component->input[0]);
1155 v4l2_dbg(1, bcm2835_v4l2_debug,
1157 "%s failed to create connection\n",
1159 /* ensure capture is not going to be tried */
1160 dev->capture.port = NULL;
1162 port->es.video.width = f->fmt.pix.width;
1163 port->es.video.height = f->fmt.pix.height;
1164 port->es.video.crop.x = 0;
1165 port->es.video.crop.y = 0;
1166 port->es.video.crop.width = f->fmt.pix.width;
1167 port->es.video.crop.height = f->fmt.pix.height;
1168 port->es.video.frame_rate.num =
1169 dev->capture.timeperframe.denominator;
1170 port->es.video.frame_rate.den =
1171 dev->capture.timeperframe.numerator;
1173 port->format.encoding = mfmt->mmal;
1174 port->format.encoding_variant = 0;
1175 /* Set any encoding specific parameters */
1176 switch (mfmt->mmal_component) {
1177 case MMAL_COMPONENT_VIDEO_ENCODE:
1178 port->format.bitrate =
1179 dev->capture.encode_bitrate;
1181 case MMAL_COMPONENT_IMAGE_ENCODE:
1182 /* Could set EXIF parameters here */
1187 ret = vchiq_mmal_port_set_format(dev->instance,
1190 v4l2_dbg(1, bcm2835_v4l2_debug,
1192 "%s failed to set format %dx%d fmt %08X\n",
1196 f->fmt.pix.pixelformat
1201 ret = vchiq_mmal_component_enable(
1205 v4l2_dbg(1, bcm2835_v4l2_debug,
1207 "%s Failed to enable encode components\n",
1212 /* configure buffering */
1213 port->current_buffer.num = 1;
1214 port->current_buffer.size =
1215 f->fmt.pix.sizeimage;
1216 if (port->format.encoding ==
1217 MMAL_ENCODING_JPEG) {
1218 v4l2_dbg(1, bcm2835_v4l2_debug,
1220 "JPG - buf size now %d was %d\n",
1221 f->fmt.pix.sizeimage,
1222 port->current_buffer.size);
1223 port->current_buffer.size =
1224 (f->fmt.pix.sizeimage <
1227 : f->fmt.pix.sizeimage;
1229 v4l2_dbg(1, bcm2835_v4l2_debug,
1231 "vid_cap - cur_buf.size set to %d\n",
1232 f->fmt.pix.sizeimage);
1233 port->current_buffer.alignment = 0;
1236 /* configure buffering */
1237 camera_port->current_buffer.num = 1;
1238 camera_port->current_buffer.size = f->fmt.pix.sizeimage;
1239 camera_port->current_buffer.alignment = 0;
1243 dev->capture.fmt = mfmt;
1244 dev->capture.stride = f->fmt.pix.bytesperline;
1245 dev->capture.width = camera_port->es.video.crop.width;
1246 dev->capture.height = camera_port->es.video.crop.height;
1247 dev->capture.buffersize = port->current_buffer.size;
1249 /* select port for capture */
1250 dev->capture.port = port;
1251 dev->capture.camera_port = camera_port;
1252 dev->capture.encode_component = encode_component;
1253 v4l2_dbg(1, bcm2835_v4l2_debug,
1255 "Set dev->capture.fmt %08X, %dx%d, stride %d, size %d",
1256 port->format.encoding,
1257 dev->capture.width, dev->capture.height,
1258 dev->capture.stride, dev->capture.buffersize);
1262 /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
1266 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1267 struct v4l2_format *f)
1270 struct bm2835_mmal_dev *dev = video_drvdata(file);
1271 struct mmal_fmt *mfmt;
1273 /* try the format to set valid parameters */
1274 ret = vidioc_try_fmt_vid_cap(file, priv, f);
1276 v4l2_err(&dev->v4l2_dev,
1277 "vid_cap - vidioc_try_fmt_vid_cap failed\n");
1281 /* if a capture is running refuse to set format */
1282 if (vb2_is_busy(&dev->capture.vb_vidq)) {
1283 v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
1287 /* If the format is unsupported v4l2 says we should switch to
1288 * a supported one and not return an error.
1290 mfmt = get_format(f);
1292 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1293 "Fourcc format (0x%08x) unknown.\n",
1294 f->fmt.pix.pixelformat);
1295 f->fmt.pix.pixelformat = formats[0].fourcc;
1296 mfmt = get_format(f);
1299 ret = mmal_setup_components(dev, f);
1301 v4l2_err(&dev->v4l2_dev,
1302 "%s: failed to setup mmal components: %d\n",
1310 static int vidioc_enum_framesizes(struct file *file, void *fh,
1311 struct v4l2_frmsizeenum *fsize)
1313 struct bm2835_mmal_dev *dev = video_drvdata(file);
1314 static const struct v4l2_frmsize_stepwise sizes = {
1322 for (i = 0; i < ARRAY_SIZE(formats); i++)
1323 if (formats[i].fourcc == fsize->pixel_format)
1325 if (i == ARRAY_SIZE(formats))
1327 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1328 fsize->stepwise = sizes;
1329 fsize->stepwise.max_width = dev->max_width;
1330 fsize->stepwise.max_height = dev->max_height;
1334 /* timeperframe is arbitrary and continuous */
1335 static int vidioc_enum_frameintervals(struct file *file, void *priv,
1336 struct v4l2_frmivalenum *fival)
1338 struct bm2835_mmal_dev *dev = video_drvdata(file);
1344 for (i = 0; i < ARRAY_SIZE(formats); i++)
1345 if (formats[i].fourcc == fival->pixel_format)
1347 if (i == ARRAY_SIZE(formats))
1350 /* regarding width & height - we support any within range */
1351 if (fival->width < MIN_WIDTH || fival->width > dev->max_width ||
1352 fival->height < MIN_HEIGHT || fival->height > dev->max_height)
1355 fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1357 /* fill in stepwise (step=1.0 is required by V4L2 spec) */
1358 fival->stepwise.min = tpf_min;
1359 fival->stepwise.max = tpf_max;
1360 fival->stepwise.step = (struct v4l2_fract) {1, 1};
1365 static int vidioc_g_parm(struct file *file, void *priv,
1366 struct v4l2_streamparm *parm)
1368 struct bm2835_mmal_dev *dev = video_drvdata(file);
1370 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1373 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1374 parm->parm.capture.timeperframe = dev->capture.timeperframe;
1375 parm->parm.capture.readbuffers = 1;
1379 #define FRACT_CMP(a, OP, b) \
1380 ((u64)(a).numerator * (b).denominator OP \
1381 (u64)(b).numerator * (a).denominator)
1383 static int vidioc_s_parm(struct file *file, void *priv,
1384 struct v4l2_streamparm *parm)
1386 struct bm2835_mmal_dev *dev = video_drvdata(file);
1387 struct v4l2_fract tpf;
1389 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1392 tpf = parm->parm.capture.timeperframe;
1394 /* tpf: {*, 0} resets timing; clip to [min, max]*/
1395 tpf = tpf.denominator ? tpf : tpf_default;
1396 tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1397 tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1399 dev->capture.timeperframe = tpf;
1400 parm->parm.capture.timeperframe = tpf;
1401 parm->parm.capture.readbuffers = 1;
1402 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1404 set_framerate_params(dev);
1409 static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
1411 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
1412 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
1413 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
1414 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
1415 .vidioc_overlay = vidioc_overlay,
1416 .vidioc_g_fbuf = vidioc_g_fbuf,
1419 .vidioc_enum_input = vidioc_enum_input,
1420 .vidioc_g_input = vidioc_g_input,
1421 .vidioc_s_input = vidioc_s_input,
1424 .vidioc_querycap = vidioc_querycap,
1425 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1426 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1427 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1428 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1430 /* buffer management */
1431 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1432 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1433 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1434 .vidioc_querybuf = vb2_ioctl_querybuf,
1435 .vidioc_qbuf = vb2_ioctl_qbuf,
1436 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1437 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1438 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1439 .vidioc_g_parm = vidioc_g_parm,
1440 .vidioc_s_parm = vidioc_s_parm,
1441 .vidioc_streamon = vb2_ioctl_streamon,
1442 .vidioc_streamoff = vb2_ioctl_streamoff,
1444 .vidioc_log_status = v4l2_ctrl_log_status,
1445 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1446 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1449 /* ------------------------------------------------------------------
1450 * Driver init/finalise
1451 * ------------------------------------------------------------------
1454 static const struct v4l2_file_operations camera0_fops = {
1455 .owner = THIS_MODULE,
1456 .open = v4l2_fh_open,
1457 .release = vb2_fop_release,
1458 .read = vb2_fop_read,
1459 .poll = vb2_fop_poll,
1460 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1461 .mmap = vb2_fop_mmap,
1464 static const struct video_device vdev_template = {
1466 .fops = &camera0_fops,
1467 .ioctl_ops = &camera0_ioctl_ops,
1468 .release = video_device_release_empty,
1471 /* Returns the number of cameras, and also the max resolution supported
1474 static int get_num_cameras(struct vchiq_mmal_instance *instance,
1475 unsigned int resolutions[][2], int num_resolutions)
1478 struct vchiq_mmal_component *cam_info_component;
1479 struct mmal_parameter_camera_info_t cam_info = {0};
1480 u32 param_size = sizeof(cam_info);
1483 /* create a camera_info component */
1484 ret = vchiq_mmal_component_init(instance, "camera_info",
1485 &cam_info_component);
1487 /* Unusual failure - let's guess one camera. */
1490 if (vchiq_mmal_port_parameter_get(instance,
1491 &cam_info_component->control,
1492 MMAL_PARAMETER_CAMERA_INFO,
1495 pr_info("Failed to get camera info\n");
1498 i < min_t(unsigned int, cam_info.num_cameras, num_resolutions);
1500 resolutions[i][0] = cam_info.cameras[i].max_width;
1501 resolutions[i][1] = cam_info.cameras[i].max_height;
1504 vchiq_mmal_component_finalise(instance,
1505 cam_info_component);
1507 return cam_info.num_cameras;
1510 static int set_camera_parameters(struct vchiq_mmal_instance *instance,
1511 struct vchiq_mmal_component *camera,
1512 struct bm2835_mmal_dev *dev)
1515 struct mmal_parameter_camera_config cam_config = {
1516 .max_stills_w = dev->max_width,
1517 .max_stills_h = dev->max_height,
1519 .one_shot_stills = 1,
1520 .max_preview_video_w = (max_video_width > 1920) ?
1521 max_video_width : 1920,
1522 .max_preview_video_h = (max_video_height > 1088) ?
1523 max_video_height : 1088,
1524 .num_preview_video_frames = 3,
1525 .stills_capture_circular_buffer_height = 0,
1526 .fast_preview_resume = 0,
1527 .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
1530 ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
1531 MMAL_PARAMETER_CAMERA_CONFIG,
1532 &cam_config, sizeof(cam_config));
1536 #define MAX_SUPPORTED_ENCODINGS 20
1538 /* MMAL instance and component init */
1539 static int mmal_init(struct bm2835_mmal_dev *dev)
1542 struct mmal_es_format_local *format;
1543 u32 supported_encodings[MAX_SUPPORTED_ENCODINGS];
1545 struct vchiq_mmal_component *camera;
1547 ret = vchiq_mmal_init(&dev->instance);
1551 /* get the camera component ready */
1552 ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
1553 &dev->component[MMAL_COMPONENT_CAMERA]);
1557 camera = dev->component[MMAL_COMPONENT_CAMERA];
1558 if (camera->outputs < MMAL_CAMERA_PORT_COUNT) {
1563 ret = set_camera_parameters(dev->instance,
1569 /* There was an error in the firmware that meant the camera component
1570 * produced BGR instead of RGB.
1571 * This is now fixed, but in order to support the old firmwares, we
1574 dev->rgb_bgr_swapped = true;
1575 param_size = sizeof(supported_encodings);
1576 ret = vchiq_mmal_port_parameter_get(dev->instance,
1577 &camera->output[MMAL_CAMERA_PORT_CAPTURE],
1578 MMAL_PARAMETER_SUPPORTED_ENCODINGS,
1579 &supported_encodings,
1584 for (i = 0; i < param_size / sizeof(u32); i++) {
1585 if (supported_encodings[i] == MMAL_ENCODING_BGR24) {
1586 /* Found BGR24 first - old firmware. */
1589 if (supported_encodings[i] == MMAL_ENCODING_RGB24) {
1590 /* Found RGB24 first
1591 * new firmware, so use RGB24.
1593 dev->rgb_bgr_swapped = false;
1598 format = &camera->output[MMAL_CAMERA_PORT_PREVIEW].format;
1600 format->encoding = MMAL_ENCODING_OPAQUE;
1601 format->encoding_variant = MMAL_ENCODING_I420;
1603 format->es->video.width = 1024;
1604 format->es->video.height = 768;
1605 format->es->video.crop.x = 0;
1606 format->es->video.crop.y = 0;
1607 format->es->video.crop.width = 1024;
1608 format->es->video.crop.height = 768;
1609 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1610 format->es->video.frame_rate.den = 1;
1612 format = &camera->output[MMAL_CAMERA_PORT_VIDEO].format;
1614 format->encoding = MMAL_ENCODING_OPAQUE;
1615 format->encoding_variant = MMAL_ENCODING_I420;
1617 format->es->video.width = 1024;
1618 format->es->video.height = 768;
1619 format->es->video.crop.x = 0;
1620 format->es->video.crop.y = 0;
1621 format->es->video.crop.width = 1024;
1622 format->es->video.crop.height = 768;
1623 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1624 format->es->video.frame_rate.den = 1;
1626 format = &camera->output[MMAL_CAMERA_PORT_CAPTURE].format;
1628 format->encoding = MMAL_ENCODING_OPAQUE;
1630 format->es->video.width = 2592;
1631 format->es->video.height = 1944;
1632 format->es->video.crop.x = 0;
1633 format->es->video.crop.y = 0;
1634 format->es->video.crop.width = 2592;
1635 format->es->video.crop.height = 1944;
1636 format->es->video.frame_rate.num = 0; /* Rely on fps_range */
1637 format->es->video.frame_rate.den = 1;
1639 dev->capture.width = format->es->video.width;
1640 dev->capture.height = format->es->video.height;
1641 dev->capture.fmt = &formats[0];
1642 dev->capture.encode_component = NULL;
1643 dev->capture.timeperframe = tpf_default;
1644 dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
1645 dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
1647 /* get the preview component ready */
1648 ret = vchiq_mmal_component_init(
1649 dev->instance, "ril.video_render",
1650 &dev->component[MMAL_COMPONENT_PREVIEW]);
1654 if (dev->component[MMAL_COMPONENT_PREVIEW]->inputs < 1) {
1656 pr_debug("too few input ports %d needed %d\n",
1657 dev->component[MMAL_COMPONENT_PREVIEW]->inputs, 1);
1661 /* get the image encoder component ready */
1662 ret = vchiq_mmal_component_init(
1663 dev->instance, "ril.image_encode",
1664 &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1668 if (dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs < 1) {
1670 v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
1671 dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs,
1673 goto unreg_image_encoder;
1676 /* get the video encoder component ready */
1677 ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
1679 component[MMAL_COMPONENT_VIDEO_ENCODE]);
1681 goto unreg_image_encoder;
1683 if (dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs < 1) {
1685 v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
1686 dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs,
1688 goto unreg_vid_encoder;
1692 struct vchiq_mmal_port *encoder_port =
1693 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
1694 encoder_port->format.encoding = MMAL_ENCODING_H264;
1695 ret = vchiq_mmal_port_set_format(dev->instance,
1700 unsigned int enable = 1;
1702 vchiq_mmal_port_parameter_set(
1704 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
1705 MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
1706 &enable, sizeof(enable));
1708 vchiq_mmal_port_parameter_set(dev->instance,
1709 &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
1710 MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
1714 ret = bm2835_mmal_set_all_camera_controls(dev);
1716 goto unreg_vid_encoder;
1721 pr_err("Cleanup: Destroy video encoder\n");
1722 vchiq_mmal_component_finalise(
1724 dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
1726 unreg_image_encoder:
1727 pr_err("Cleanup: Destroy image encoder\n");
1728 vchiq_mmal_component_finalise(
1730 dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
1733 pr_err("Cleanup: Destroy video render\n");
1734 vchiq_mmal_component_finalise(dev->instance,
1735 dev->component[MMAL_COMPONENT_PREVIEW]);
1738 pr_err("Cleanup: Destroy camera\n");
1739 vchiq_mmal_component_finalise(dev->instance,
1740 dev->component[MMAL_COMPONENT_CAMERA]);
1743 vchiq_mmal_finalise(dev->instance);
1747 static int bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
1748 struct video_device *vfd)
1752 *vfd = vdev_template;
1754 vfd->v4l2_dev = &dev->v4l2_dev;
1756 vfd->lock = &dev->mutex;
1758 vfd->queue = &dev->capture.vb_vidq;
1760 /* video device needs to be able to access instance data */
1761 video_set_drvdata(vfd, dev);
1763 ret = video_register_device(vfd,
1765 video_nr[dev->camera_num]);
1769 v4l2_info(vfd->v4l2_dev,
1770 "V4L2 device registered as %s - stills mode > %dx%d\n",
1771 video_device_node_name(vfd),
1772 max_video_width, max_video_height);
1777 static void bcm2835_cleanup_instance(struct bm2835_mmal_dev *dev)
1782 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1783 video_device_node_name(&dev->vdev));
1785 video_unregister_device(&dev->vdev);
1787 if (dev->capture.encode_component) {
1788 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
1789 "mmal_exit - disconnect tunnel\n");
1790 vchiq_mmal_port_connect_tunnel(dev->instance,
1791 dev->capture.camera_port, NULL);
1792 vchiq_mmal_component_disable(dev->instance,
1793 dev->capture.encode_component);
1795 vchiq_mmal_component_disable(dev->instance,
1796 dev->component[MMAL_COMPONENT_CAMERA]);
1798 vchiq_mmal_component_finalise(dev->instance,
1800 component[MMAL_COMPONENT_VIDEO_ENCODE]);
1802 vchiq_mmal_component_finalise(dev->instance,
1804 component[MMAL_COMPONENT_IMAGE_ENCODE]);
1806 vchiq_mmal_component_finalise(dev->instance,
1807 dev->component[MMAL_COMPONENT_PREVIEW]);
1809 vchiq_mmal_component_finalise(dev->instance,
1810 dev->component[MMAL_COMPONENT_CAMERA]);
1812 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1814 v4l2_device_unregister(&dev->v4l2_dev);
1819 static struct v4l2_format default_v4l2_format = {
1820 .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
1821 .fmt.pix.width = 1024,
1822 .fmt.pix.bytesperline = 0,
1823 .fmt.pix.height = 768,
1824 .fmt.pix.sizeimage = 1024 * 768,
1827 static int bcm2835_mmal_probe(struct platform_device *pdev)
1830 struct bm2835_mmal_dev *dev;
1831 struct vb2_queue *q;
1833 unsigned int num_cameras;
1834 struct vchiq_mmal_instance *instance;
1835 unsigned int resolutions[MAX_BCM2835_CAMERAS][2];
1838 ret = vchiq_mmal_init(&instance);
1842 num_cameras = get_num_cameras(instance,
1844 MAX_BCM2835_CAMERAS);
1845 if (num_cameras > MAX_BCM2835_CAMERAS)
1846 num_cameras = MAX_BCM2835_CAMERAS;
1848 for (camera = 0; camera < num_cameras; camera++) {
1849 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1855 /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
1856 mutex_init(&dev->mutex);
1857 dev->camera_num = camera;
1858 dev->max_width = resolutions[camera][0];
1859 dev->max_height = resolutions[camera][1];
1861 /* setup device defaults */
1862 dev->overlay.w.left = 150;
1863 dev->overlay.w.top = 50;
1864 dev->overlay.w.width = 1024;
1865 dev->overlay.w.height = 768;
1866 dev->overlay.clipcount = 0;
1867 dev->overlay.field = V4L2_FIELD_NONE;
1868 dev->overlay.global_alpha = 255;
1870 dev->capture.fmt = &formats[3]; /* JPEG */
1872 /* v4l device registration */
1873 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1874 "%s", BM2835_MMAL_MODULE_NAME);
1875 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1879 /* setup v4l controls */
1880 ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
1883 dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
1886 dev->instance = instance;
1887 ret = mmal_init(dev);
1891 /* initialize queue */
1892 q = &dev->capture.vb_vidq;
1893 memset(q, 0, sizeof(*q));
1894 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1895 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1897 q->buf_struct_size = sizeof(struct mmal_buffer);
1898 q->ops = &bm2835_mmal_video_qops;
1899 q->mem_ops = &vb2_vmalloc_memops;
1900 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1901 q->lock = &dev->mutex;
1902 ret = vb2_queue_init(q);
1906 /* initialise video devices */
1907 ret = bm2835_mmal_init_device(dev, &dev->vdev);
1911 /* Really want to call vidioc_s_fmt_vid_cap with the default
1912 * format, but currently the APIs don't join up.
1914 ret = mmal_setup_components(dev, &default_v4l2_format);
1916 v4l2_err(&dev->v4l2_dev,
1917 "%s: could not setup components\n", __func__);
1921 v4l2_info(&dev->v4l2_dev,
1922 "Broadcom 2835 MMAL video capture ver %s loaded.\n",
1923 BM2835_MMAL_VERSION);
1930 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1931 v4l2_device_unregister(&dev->v4l2_dev);
1937 for (i = 0; i < camera; i++) {
1938 bcm2835_cleanup_instance(gdev[i]);
1941 pr_info("%s: error %d while loading driver\n",
1942 BM2835_MMAL_MODULE_NAME, ret);
1947 static int bcm2835_mmal_remove(struct platform_device *pdev)
1950 struct vchiq_mmal_instance *instance = gdev[0]->instance;
1952 for (camera = 0; camera < MAX_BCM2835_CAMERAS; camera++) {
1953 bcm2835_cleanup_instance(gdev[camera]);
1954 gdev[camera] = NULL;
1956 vchiq_mmal_finalise(instance);
1961 static struct platform_driver bcm2835_camera_driver = {
1962 .probe = bcm2835_mmal_probe,
1963 .remove = bcm2835_mmal_remove,
1965 .name = "bcm2835-camera",
1969 module_platform_driver(bcm2835_camera_driver)