1a42b7eb5SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2212afb97SMichael Zoran /*
3*b33721baSGaston Gonzalez  * Broadcom BCM2835 V4L2 driver
4212afb97SMichael Zoran  *
5212afb97SMichael Zoran  * Copyright © 2013 Raspberry Pi (Trading) Ltd.
6212afb97SMichael Zoran  *
7be2f87c3SDave Stevenson  * Authors: Vincent Sanders @ Collabora
8be2f87c3SDave Stevenson  *          Dave Stevenson @ Broadcom
9be2f87c3SDave Stevenson  *		(now dave.stevenson@raspberrypi.org)
10be2f87c3SDave Stevenson  *          Simon Mellor @ Broadcom
11be2f87c3SDave Stevenson  *          Luke Diamand @ Broadcom
12212afb97SMichael Zoran  *
13212afb97SMichael Zoran  * core driver device
14212afb97SMichael Zoran  */
15212afb97SMichael Zoran 
16212afb97SMichael Zoran #define V4L2_CTRL_COUNT 29 /* number of v4l controls */
17212afb97SMichael Zoran 
18212afb97SMichael Zoran enum {
193485507fSDave Stevenson 	COMP_CAMERA = 0,
203485507fSDave Stevenson 	COMP_PREVIEW,
213485507fSDave Stevenson 	COMP_IMAGE_ENCODE,
223485507fSDave Stevenson 	COMP_VIDEO_ENCODE,
233485507fSDave Stevenson 	COMP_COUNT
24212afb97SMichael Zoran };
25212afb97SMichael Zoran 
26212afb97SMichael Zoran enum {
273485507fSDave Stevenson 	CAM_PORT_PREVIEW = 0,
283485507fSDave Stevenson 	CAM_PORT_VIDEO,
293485507fSDave Stevenson 	CAM_PORT_CAPTURE,
303485507fSDave Stevenson 	CAM_PORT_COUNT
31212afb97SMichael Zoran };
32212afb97SMichael Zoran 
33212afb97SMichael Zoran extern int bcm2835_v4l2_debug;
34212afb97SMichael Zoran 
35710ec044SGaston Gonzalez struct bcm2835_mmal_dev {
36212afb97SMichael Zoran 	/* v4l2 devices */
37212afb97SMichael Zoran 	struct v4l2_device v4l2_dev;
38212afb97SMichael Zoran 	struct video_device vdev;
39212afb97SMichael Zoran 	struct mutex mutex;
40212afb97SMichael Zoran 
41212afb97SMichael Zoran 	/* controls */
42212afb97SMichael Zoran 	struct v4l2_ctrl_handler ctrl_handler;
43212afb97SMichael Zoran 	struct v4l2_ctrl *ctrls[V4L2_CTRL_COUNT];
44212afb97SMichael Zoran 	enum v4l2_scene_mode scene_mode;
45212afb97SMichael Zoran 	struct mmal_colourfx colourfx;
46212afb97SMichael Zoran 	int hflip;
47212afb97SMichael Zoran 	int vflip;
48212afb97SMichael Zoran 	int red_gain;
49212afb97SMichael Zoran 	int blue_gain;
50212afb97SMichael Zoran 	enum mmal_parameter_exposuremode exposure_mode_user;
51212afb97SMichael Zoran 	enum v4l2_exposure_auto_type exposure_mode_v4l2_user;
52212afb97SMichael Zoran 	/* active exposure mode may differ if selected via a scene mode */
53212afb97SMichael Zoran 	enum mmal_parameter_exposuremode exposure_mode_active;
54212afb97SMichael Zoran 	enum mmal_parameter_exposuremeteringmode metering_mode;
55212afb97SMichael Zoran 	unsigned int manual_shutter_speed;
56212afb97SMichael Zoran 	bool exp_auto_priority;
57212afb97SMichael Zoran 	bool manual_iso_enabled;
5816255382SNarcisa Ana Maria Vasile 	u32 iso;
59212afb97SMichael Zoran 
60212afb97SMichael Zoran 	/* allocated mmal instance and components */
61212afb97SMichael Zoran 	struct vchiq_mmal_instance *instance;
623485507fSDave Stevenson 	struct vchiq_mmal_component *component[COMP_COUNT];
63212afb97SMichael Zoran 	int camera_use_count;
64212afb97SMichael Zoran 
65212afb97SMichael Zoran 	struct v4l2_window overlay;
66212afb97SMichael Zoran 
67212afb97SMichael Zoran 	struct {
68212afb97SMichael Zoran 		unsigned int width;  /* width */
69212afb97SMichael Zoran 		unsigned int height;  /* height */
70212afb97SMichael Zoran 		unsigned int stride;  /* stride */
71212afb97SMichael Zoran 		unsigned int buffersize; /* buffer size with padding */
72212afb97SMichael Zoran 		struct mmal_fmt *fmt;
73212afb97SMichael Zoran 		struct v4l2_fract timeperframe;
74212afb97SMichael Zoran 
75212afb97SMichael Zoran 		/* H264 encode bitrate */
76212afb97SMichael Zoran 		int encode_bitrate;
77212afb97SMichael Zoran 		/* H264 bitrate mode. CBR/VBR */
78212afb97SMichael Zoran 		int encode_bitrate_mode;
79212afb97SMichael Zoran 		/* H264 profile */
80212afb97SMichael Zoran 		enum v4l2_mpeg_video_h264_profile enc_profile;
81212afb97SMichael Zoran 		/* H264 level */
82212afb97SMichael Zoran 		enum v4l2_mpeg_video_h264_level enc_level;
83212afb97SMichael Zoran 		/* JPEG Q-factor */
84212afb97SMichael Zoran 		int q_factor;
85212afb97SMichael Zoran 
86212afb97SMichael Zoran 		struct vb2_queue vb_vidq;
87212afb97SMichael Zoran 
88212afb97SMichael Zoran 		/* VC start timestamp for streaming */
89212afb97SMichael Zoran 		s64 vc_start_timestamp;
90212afb97SMichael Zoran 		/* Kernel start timestamp for streaming */
916cf83f2aSArnd Bergmann 		ktime_t kernel_start_ts;
92abb4718cSDave Stevenson 		/* Sequence number of last buffer */
93abb4718cSDave Stevenson 		u32 sequence;
94212afb97SMichael Zoran 
95212afb97SMichael Zoran 		struct vchiq_mmal_port *port; /* port being used for capture */
96212afb97SMichael Zoran 		/* camera port being used for capture */
97212afb97SMichael Zoran 		struct vchiq_mmal_port *camera_port;
98212afb97SMichael Zoran 		/* component being used for encode */
99212afb97SMichael Zoran 		struct vchiq_mmal_component *encode_component;
100212afb97SMichael Zoran 		/* number of frames remaining which driver should capture */
101212afb97SMichael Zoran 		unsigned int frame_count;
102212afb97SMichael Zoran 		/* last frame completion */
103212afb97SMichael Zoran 		struct completion frame_cmplt;
104212afb97SMichael Zoran 
105212afb97SMichael Zoran 	} capture;
106212afb97SMichael Zoran 
107212afb97SMichael Zoran 	unsigned int camera_num;
108212afb97SMichael Zoran 	unsigned int max_width;
109212afb97SMichael Zoran 	unsigned int max_height;
110212afb97SMichael Zoran 	unsigned int rgb_bgr_swapped;
111212afb97SMichael Zoran };
112212afb97SMichael Zoran 
113d6776424SGaston Gonzalez int bcm2835_mmal_init_controls(struct bcm2835_mmal_dev *dev, struct v4l2_ctrl_handler *hdl);
114212afb97SMichael Zoran 
115d6776424SGaston Gonzalez int bcm2835_mmal_set_all_camera_controls(struct bcm2835_mmal_dev *dev);
116710ec044SGaston Gonzalez int set_framerate_params(struct bcm2835_mmal_dev *dev);
117212afb97SMichael Zoran 
118212afb97SMichael Zoran /* Debug helpers */
119212afb97SMichael Zoran 
120212afb97SMichael Zoran #define v4l2_dump_pix_format(level, debug, dev, pix_fmt, desc)	\
121212afb97SMichael Zoran {	\
122212afb97SMichael Zoran 	v4l2_dbg(level, debug, dev,	\
123212afb97SMichael Zoran "%s: w %u h %u field %u pfmt 0x%x bpl %u sz_img %u colorspace 0x%x priv %u\n", \
1242367eb3fSMichael Zoran 		desc,	\
125212afb97SMichael Zoran 		(pix_fmt)->width, (pix_fmt)->height, (pix_fmt)->field,	\
126212afb97SMichael Zoran 		(pix_fmt)->pixelformat, (pix_fmt)->bytesperline,	\
127212afb97SMichael Zoran 		(pix_fmt)->sizeimage, (pix_fmt)->colorspace, (pix_fmt)->priv); \
128212afb97SMichael Zoran }
129fe78fd7bSDave Stevenson 
130212afb97SMichael Zoran #define v4l2_dump_win_format(level, debug, dev, win_fmt, desc)	\
131212afb97SMichael Zoran {	\
132212afb97SMichael Zoran 	v4l2_dbg(level, debug, dev,	\
133212afb97SMichael Zoran "%s: w %u h %u l %u t %u  field %u chromakey %06X clip %p " \
134212afb97SMichael Zoran "clipcount %u bitmap %p\n", \
1352367eb3fSMichael Zoran 		desc,	\
136212afb97SMichael Zoran 		(win_fmt)->w.width, (win_fmt)->w.height, \
137212afb97SMichael Zoran 		(win_fmt)->w.left, (win_fmt)->w.top, \
138212afb97SMichael Zoran 		(win_fmt)->field,	\
139212afb97SMichael Zoran 		(win_fmt)->chromakey,	\
140212afb97SMichael Zoran 		(win_fmt)->clips, (win_fmt)->clipcount,	\
141212afb97SMichael Zoran 		(win_fmt)->bitmap); \
142212afb97SMichael Zoran }
143