1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Broadcom BM2835 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 	MMAL_COMPONENT_CAMERA = 0,
20 	MMAL_COMPONENT_PREVIEW,
21 	MMAL_COMPONENT_IMAGE_ENCODE,
22 	MMAL_COMPONENT_VIDEO_ENCODE,
23 	MMAL_COMPONENT_COUNT
24 };
25 
26 enum {
27 	MMAL_CAMERA_PORT_PREVIEW = 0,
28 	MMAL_CAMERA_PORT_VIDEO,
29 	MMAL_CAMERA_PORT_CAPTURE,
30 	MMAL_CAMERA_PORT_COUNT
31 };
32 
33 #define PREVIEW_LAYER      2
34 
35 extern int bcm2835_v4l2_debug;
36 
37 struct bm2835_mmal_dev {
38 	/* v4l2 devices */
39 	struct v4l2_device     v4l2_dev;
40 	struct video_device    vdev;
41 	struct mutex           mutex;
42 
43 	/* controls */
44 	struct v4l2_ctrl_handler  ctrl_handler;
45 	struct v4l2_ctrl          *ctrls[V4L2_CTRL_COUNT];
46 	enum v4l2_scene_mode	  scene_mode;
47 	struct mmal_colourfx      colourfx;
48 	int                       hflip;
49 	int                       vflip;
50 	int			  red_gain;
51 	int			  blue_gain;
52 	enum mmal_parameter_exposuremode exposure_mode_user;
53 	enum v4l2_exposure_auto_type exposure_mode_v4l2_user;
54 	/* active exposure mode may differ if selected via a scene mode */
55 	enum mmal_parameter_exposuremode exposure_mode_active;
56 	enum mmal_parameter_exposuremeteringmode metering_mode;
57 	unsigned int		  manual_shutter_speed;
58 	bool			  exp_auto_priority;
59 	bool manual_iso_enabled;
60 	u32 iso;
61 
62 	/* allocated mmal instance and components */
63 	struct vchiq_mmal_instance   *instance;
64 	struct vchiq_mmal_component  *component[MMAL_COMPONENT_COUNT];
65 	int camera_use_count;
66 
67 	struct v4l2_window overlay;
68 
69 	struct {
70 		unsigned int     width;  /* width */
71 		unsigned int     height;  /* height */
72 		unsigned int     stride;  /* stride */
73 		unsigned int     buffersize; /* buffer size with padding */
74 		struct mmal_fmt  *fmt;
75 		struct v4l2_fract timeperframe;
76 
77 		/* H264 encode bitrate */
78 		int         encode_bitrate;
79 		/* H264 bitrate mode. CBR/VBR */
80 		int         encode_bitrate_mode;
81 		/* H264 profile */
82 		enum v4l2_mpeg_video_h264_profile enc_profile;
83 		/* H264 level */
84 		enum v4l2_mpeg_video_h264_level enc_level;
85 		/* JPEG Q-factor */
86 		int         q_factor;
87 
88 		struct vb2_queue	vb_vidq;
89 
90 		/* VC start timestamp for streaming */
91 		s64         vc_start_timestamp;
92 		/* Kernel start timestamp for streaming */
93 		ktime_t kernel_start_ts;
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 bm2835_mmal_init_controls(
114 			struct bm2835_mmal_dev *dev,
115 			struct v4l2_ctrl_handler *hdl);
116 
117 int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev);
118 int set_framerate_params(struct bm2835_mmal_dev *dev);
119 
120 /* Debug helpers */
121 
122 #define v4l2_dump_pix_format(level, debug, dev, pix_fmt, desc)	\
123 {	\
124 	v4l2_dbg(level, debug, dev,	\
125 "%s: w %u h %u field %u pfmt 0x%x bpl %u sz_img %u colorspace 0x%x priv %u\n", \
126 		desc,	\
127 		(pix_fmt)->width, (pix_fmt)->height, (pix_fmt)->field,	\
128 		(pix_fmt)->pixelformat, (pix_fmt)->bytesperline,	\
129 		(pix_fmt)->sizeimage, (pix_fmt)->colorspace, (pix_fmt)->priv); \
130 }
131 #define v4l2_dump_win_format(level, debug, dev, win_fmt, desc)	\
132 {	\
133 	v4l2_dbg(level, debug, dev,	\
134 "%s: w %u h %u l %u t %u  field %u chromakey %06X clip %p " \
135 "clipcount %u bitmap %p\n", \
136 		desc,	\
137 		(win_fmt)->w.width, (win_fmt)->w.height, \
138 		(win_fmt)->w.left, (win_fmt)->w.top, \
139 		(win_fmt)->field,	\
140 		(win_fmt)->chromakey,	\
141 		(win_fmt)->clips, (win_fmt)->clipcount,	\
142 		(win_fmt)->bitmap); \
143 }
144