1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Broadcom BCM2835 V4L2 driver
4  *
5  * Copyright © 2013 Raspberry Pi (Trading) Ltd.
6  *
7  * Authors: Vincent Sanders @ Collabora
8  *          Dave Stevenson @ Broadcom
9  *		(now dave.stevenson@raspberrypi.org)
10  *          Simon Mellor @ Broadcom
11  *          Luke Diamand @ Broadcom
12  *
13  * core driver device
14  */
15 
16 #define V4L2_CTRL_COUNT 29 /* number of v4l controls */
17 
18 enum {
19 	COMP_CAMERA = 0,
20 	COMP_PREVIEW,
21 	COMP_IMAGE_ENCODE,
22 	COMP_VIDEO_ENCODE,
23 	COMP_COUNT
24 };
25 
26 enum {
27 	CAM_PORT_PREVIEW = 0,
28 	CAM_PORT_VIDEO,
29 	CAM_PORT_CAPTURE,
30 	CAM_PORT_COUNT
31 };
32 
33 extern int bcm2835_v4l2_debug;
34 
35 struct bcm2835_mmal_dev {
36 	/* v4l2 devices */
37 	struct v4l2_device v4l2_dev;
38 	struct video_device vdev;
39 	struct mutex mutex;
40 
41 	/* controls */
42 	struct v4l2_ctrl_handler ctrl_handler;
43 	struct v4l2_ctrl *ctrls[V4L2_CTRL_COUNT];
44 	enum v4l2_scene_mode scene_mode;
45 	struct mmal_colourfx colourfx;
46 	int hflip;
47 	int vflip;
48 	int red_gain;
49 	int blue_gain;
50 	enum mmal_parameter_exposuremode exposure_mode_user;
51 	enum v4l2_exposure_auto_type exposure_mode_v4l2_user;
52 	/* active exposure mode may differ if selected via a scene mode */
53 	enum mmal_parameter_exposuremode exposure_mode_active;
54 	enum mmal_parameter_exposuremeteringmode metering_mode;
55 	unsigned int manual_shutter_speed;
56 	bool exp_auto_priority;
57 	bool manual_iso_enabled;
58 	u32 iso;
59 
60 	/* allocated mmal instance and components */
61 	struct vchiq_mmal_instance *instance;
62 	struct vchiq_mmal_component *component[COMP_COUNT];
63 	int camera_use_count;
64 
65 	struct v4l2_window overlay;
66 
67 	struct {
68 		unsigned int width;  /* width */
69 		unsigned int height;  /* height */
70 		unsigned int stride;  /* stride */
71 		unsigned int buffersize; /* buffer size with padding */
72 		struct mmal_fmt *fmt;
73 		struct v4l2_fract timeperframe;
74 
75 		/* H264 encode bitrate */
76 		int encode_bitrate;
77 		/* H264 bitrate mode. CBR/VBR */
78 		int encode_bitrate_mode;
79 		/* H264 profile */
80 		enum v4l2_mpeg_video_h264_profile enc_profile;
81 		/* H264 level */
82 		enum v4l2_mpeg_video_h264_level enc_level;
83 		/* JPEG Q-factor */
84 		int q_factor;
85 
86 		struct vb2_queue vb_vidq;
87 
88 		/* VC start timestamp for streaming */
89 		s64 vc_start_timestamp;
90 		/* Kernel start timestamp for streaming */
91 		ktime_t kernel_start_ts;
92 		/* Sequence number of last buffer */
93 		u32 sequence;
94 
95 		struct vchiq_mmal_port *port; /* port being used for capture */
96 		/* camera port being used for capture */
97 		struct vchiq_mmal_port *camera_port;
98 		/* component being used for encode */
99 		struct vchiq_mmal_component *encode_component;
100 		/* number of frames remaining which driver should capture */
101 		unsigned int frame_count;
102 		/* last frame completion */
103 		struct completion frame_cmplt;
104 
105 	} capture;
106 
107 	unsigned int camera_num;
108 	unsigned int max_width;
109 	unsigned int max_height;
110 	unsigned int rgb_bgr_swapped;
111 };
112 
113 int bcm2835_mmal_init_controls(struct bcm2835_mmal_dev *dev, struct v4l2_ctrl_handler *hdl);
114 
115 int bcm2835_mmal_set_all_camera_controls(struct bcm2835_mmal_dev *dev);
116 int set_framerate_params(struct bcm2835_mmal_dev *dev);
117 
118 /* Debug helpers */
119 
120 #define v4l2_dump_pix_format(level, debug, dev, pix_fmt, desc)	\
121 {	\
122 	v4l2_dbg(level, debug, dev,	\
123 "%s: w %u h %u field %u pfmt 0x%x bpl %u sz_img %u colorspace 0x%x priv %u\n", \
124 		desc,	\
125 		(pix_fmt)->width, (pix_fmt)->height, (pix_fmt)->field,	\
126 		(pix_fmt)->pixelformat, (pix_fmt)->bytesperline,	\
127 		(pix_fmt)->sizeimage, (pix_fmt)->colorspace, (pix_fmt)->priv); \
128 }
129 
130 #define v4l2_dump_win_format(level, debug, dev, win_fmt, desc)	\
131 {	\
132 	v4l2_dbg(level, debug, dev,	\
133 "%s: w %u h %u l %u t %u  field %u chromakey %06X clip %p " \
134 "clipcount %u bitmap %p\n", \
135 		desc,	\
136 		(win_fmt)->w.width, (win_fmt)->w.height, \
137 		(win_fmt)->w.left, (win_fmt)->w.top, \
138 		(win_fmt)->field,	\
139 		(win_fmt)->chromakey,	\
140 		(win_fmt)->clips, (win_fmt)->clipcount,	\
141 		(win_fmt)->bitmap); \
142 }
143