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 <vincent.sanders@collabora.co.uk>
8  *          Dave Stevenson <dsteve@broadcom.com>
9  *          Simon Mellor <simellor@broadcom.com>
10  *          Luke Diamand <luked@broadcom.com>
11  *
12  * core driver device
13  */
14 
15 #define V4L2_CTRL_COUNT 29 /* number of v4l controls */
16 
17 enum {
18 	MMAL_COMPONENT_CAMERA = 0,
19 	MMAL_COMPONENT_PREVIEW,
20 	MMAL_COMPONENT_IMAGE_ENCODE,
21 	MMAL_COMPONENT_VIDEO_ENCODE,
22 	MMAL_COMPONENT_COUNT
23 };
24 
25 enum {
26 	MMAL_CAMERA_PORT_PREVIEW = 0,
27 	MMAL_CAMERA_PORT_VIDEO,
28 	MMAL_CAMERA_PORT_CAPTURE,
29 	MMAL_CAMERA_PORT_COUNT
30 };
31 
32 #define PREVIEW_LAYER      2
33 
34 extern int bcm2835_v4l2_debug;
35 
36 struct bm2835_mmal_dev {
37 	/* v4l2 devices */
38 	struct v4l2_device     v4l2_dev;
39 	struct video_device    vdev;
40 	struct mutex           mutex;
41 
42 	/* controls */
43 	struct v4l2_ctrl_handler  ctrl_handler;
44 	struct v4l2_ctrl          *ctrls[V4L2_CTRL_COUNT];
45 	enum v4l2_scene_mode	  scene_mode;
46 	struct mmal_colourfx      colourfx;
47 	int                       hflip;
48 	int                       vflip;
49 	int			  red_gain;
50 	int			  blue_gain;
51 	enum mmal_parameter_exposuremode exposure_mode_user;
52 	enum v4l2_exposure_auto_type exposure_mode_v4l2_user;
53 	/* active exposure mode may differ if selected via a scene mode */
54 	enum mmal_parameter_exposuremode exposure_mode_active;
55 	enum mmal_parameter_exposuremeteringmode metering_mode;
56 	unsigned int		  manual_shutter_speed;
57 	bool			  exp_auto_priority;
58 	bool manual_iso_enabled;
59 	u32 iso;
60 
61 	/* allocated mmal instance and components */
62 	struct vchiq_mmal_instance   *instance;
63 	struct vchiq_mmal_component  *component[MMAL_COMPONENT_COUNT];
64 	int camera_use_count;
65 
66 	struct v4l2_window overlay;
67 
68 	struct {
69 		unsigned int     width;  /* width */
70 		unsigned int     height;  /* height */
71 		unsigned int     stride;  /* stride */
72 		unsigned int     buffersize; /* buffer size with padding */
73 		struct mmal_fmt  *fmt;
74 		struct v4l2_fract timeperframe;
75 
76 		/* H264 encode bitrate */
77 		int         encode_bitrate;
78 		/* H264 bitrate mode. CBR/VBR */
79 		int         encode_bitrate_mode;
80 		/* H264 profile */
81 		enum v4l2_mpeg_video_h264_profile enc_profile;
82 		/* H264 level */
83 		enum v4l2_mpeg_video_h264_level enc_level;
84 		/* JPEG Q-factor */
85 		int         q_factor;
86 
87 		struct vb2_queue	vb_vidq;
88 
89 		/* VC start timestamp for streaming */
90 		s64         vc_start_timestamp;
91 		/* Kernel start timestamp for streaming */
92 		ktime_t kernel_start_ts;
93 
94 		struct vchiq_mmal_port  *port; /* port being used for capture */
95 		/* camera port being used for capture */
96 		struct vchiq_mmal_port  *camera_port;
97 		/* component being used for encode */
98 		struct vchiq_mmal_component *encode_component;
99 		/* number of frames remaining which driver should capture */
100 		unsigned int  frame_count;
101 		/* last frame completion */
102 		struct completion  frame_cmplt;
103 
104 	} capture;
105 
106 	unsigned int camera_num;
107 	unsigned int max_width;
108 	unsigned int max_height;
109 	unsigned int rgb_bgr_swapped;
110 };
111 
112 int bm2835_mmal_init_controls(
113 			struct bm2835_mmal_dev *dev,
114 			struct v4l2_ctrl_handler *hdl);
115 
116 int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev);
117 int set_framerate_params(struct bm2835_mmal_dev *dev);
118 
119 /* Debug helpers */
120 
121 #define v4l2_dump_pix_format(level, debug, dev, pix_fmt, desc)	\
122 {	\
123 	v4l2_dbg(level, debug, dev,	\
124 "%s: w %u h %u field %u pfmt 0x%x bpl %u sz_img %u colorspace 0x%x priv %u\n", \
125 		desc,	\
126 		(pix_fmt)->width, (pix_fmt)->height, (pix_fmt)->field,	\
127 		(pix_fmt)->pixelformat, (pix_fmt)->bytesperline,	\
128 		(pix_fmt)->sizeimage, (pix_fmt)->colorspace, (pix_fmt)->priv); \
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