1a42b7eb5SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2212afb97SMichael Zoran /*
3212afb97SMichael Zoran  * Broadcom BM2835 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 #define PREVIEW_LAYER      2
34212afb97SMichael Zoran 
35212afb97SMichael Zoran extern int bcm2835_v4l2_debug;
36212afb97SMichael Zoran 
37212afb97SMichael Zoran struct bm2835_mmal_dev {
38212afb97SMichael Zoran 	/* v4l2 devices */
39212afb97SMichael Zoran 	struct v4l2_device     v4l2_dev;
40212afb97SMichael Zoran 	struct video_device    vdev;
41212afb97SMichael Zoran 	struct mutex           mutex;
42212afb97SMichael Zoran 
43212afb97SMichael Zoran 	/* controls */
44212afb97SMichael Zoran 	struct v4l2_ctrl_handler  ctrl_handler;
45212afb97SMichael Zoran 	struct v4l2_ctrl          *ctrls[V4L2_CTRL_COUNT];
46212afb97SMichael Zoran 	enum v4l2_scene_mode	  scene_mode;
47212afb97SMichael Zoran 	struct mmal_colourfx      colourfx;
48212afb97SMichael Zoran 	int                       hflip;
49212afb97SMichael Zoran 	int                       vflip;
50212afb97SMichael Zoran 	int			  red_gain;
51212afb97SMichael Zoran 	int			  blue_gain;
52212afb97SMichael Zoran 	enum mmal_parameter_exposuremode exposure_mode_user;
53212afb97SMichael Zoran 	enum v4l2_exposure_auto_type exposure_mode_v4l2_user;
54212afb97SMichael Zoran 	/* active exposure mode may differ if selected via a scene mode */
55212afb97SMichael Zoran 	enum mmal_parameter_exposuremode exposure_mode_active;
56212afb97SMichael Zoran 	enum mmal_parameter_exposuremeteringmode metering_mode;
57212afb97SMichael Zoran 	unsigned int		  manual_shutter_speed;
58212afb97SMichael Zoran 	bool			  exp_auto_priority;
59212afb97SMichael Zoran 	bool manual_iso_enabled;
6016255382SNarcisa Ana Maria Vasile 	u32 iso;
61212afb97SMichael Zoran 
62212afb97SMichael Zoran 	/* allocated mmal instance and components */
63212afb97SMichael Zoran 	struct vchiq_mmal_instance   *instance;
643485507fSDave Stevenson 	struct vchiq_mmal_component  *component[COMP_COUNT];
65212afb97SMichael Zoran 	int camera_use_count;
66212afb97SMichael Zoran 
67212afb97SMichael Zoran 	struct v4l2_window overlay;
68212afb97SMichael Zoran 
69212afb97SMichael Zoran 	struct {
70212afb97SMichael Zoran 		unsigned int     width;  /* width */
71212afb97SMichael Zoran 		unsigned int     height;  /* height */
72212afb97SMichael Zoran 		unsigned int     stride;  /* stride */
73212afb97SMichael Zoran 		unsigned int     buffersize; /* buffer size with padding */
74212afb97SMichael Zoran 		struct mmal_fmt  *fmt;
75212afb97SMichael Zoran 		struct v4l2_fract timeperframe;
76212afb97SMichael Zoran 
77212afb97SMichael Zoran 		/* H264 encode bitrate */
78212afb97SMichael Zoran 		int         encode_bitrate;
79212afb97SMichael Zoran 		/* H264 bitrate mode. CBR/VBR */
80212afb97SMichael Zoran 		int         encode_bitrate_mode;
81212afb97SMichael Zoran 		/* H264 profile */
82212afb97SMichael Zoran 		enum v4l2_mpeg_video_h264_profile enc_profile;
83212afb97SMichael Zoran 		/* H264 level */
84212afb97SMichael Zoran 		enum v4l2_mpeg_video_h264_level enc_level;
85212afb97SMichael Zoran 		/* JPEG Q-factor */
86212afb97SMichael Zoran 		int         q_factor;
87212afb97SMichael Zoran 
88212afb97SMichael Zoran 		struct vb2_queue	vb_vidq;
89212afb97SMichael Zoran 
90212afb97SMichael Zoran 		/* VC start timestamp for streaming */
91212afb97SMichael Zoran 		s64         vc_start_timestamp;
92212afb97SMichael Zoran 		/* Kernel start timestamp for streaming */
936cf83f2aSArnd Bergmann 		ktime_t kernel_start_ts;
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 
113212afb97SMichael Zoran int bm2835_mmal_init_controls(
114212afb97SMichael Zoran 			struct bm2835_mmal_dev *dev,
115212afb97SMichael Zoran 			struct v4l2_ctrl_handler *hdl);
116212afb97SMichael Zoran 
117212afb97SMichael Zoran int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev);
118212afb97SMichael Zoran int set_framerate_params(struct bm2835_mmal_dev *dev);
119212afb97SMichael Zoran 
120212afb97SMichael Zoran /* Debug helpers */
121212afb97SMichael Zoran 
122212afb97SMichael Zoran #define v4l2_dump_pix_format(level, debug, dev, pix_fmt, desc)	\
123212afb97SMichael Zoran {	\
124212afb97SMichael Zoran 	v4l2_dbg(level, debug, dev,	\
125212afb97SMichael Zoran "%s: w %u h %u field %u pfmt 0x%x bpl %u sz_img %u colorspace 0x%x priv %u\n", \
1262367eb3fSMichael Zoran 		desc,	\
127212afb97SMichael Zoran 		(pix_fmt)->width, (pix_fmt)->height, (pix_fmt)->field,	\
128212afb97SMichael Zoran 		(pix_fmt)->pixelformat, (pix_fmt)->bytesperline,	\
129212afb97SMichael Zoran 		(pix_fmt)->sizeimage, (pix_fmt)->colorspace, (pix_fmt)->priv); \
130212afb97SMichael Zoran }
131212afb97SMichael Zoran #define v4l2_dump_win_format(level, debug, dev, win_fmt, desc)	\
132212afb97SMichael Zoran {	\
133212afb97SMichael Zoran 	v4l2_dbg(level, debug, dev,	\
134212afb97SMichael Zoran "%s: w %u h %u l %u t %u  field %u chromakey %06X clip %p " \
135212afb97SMichael Zoran "clipcount %u bitmap %p\n", \
1362367eb3fSMichael Zoran 		desc,	\
137212afb97SMichael Zoran 		(win_fmt)->w.width, (win_fmt)->w.height, \
138212afb97SMichael Zoran 		(win_fmt)->w.left, (win_fmt)->w.top, \
139212afb97SMichael Zoran 		(win_fmt)->field,	\
140212afb97SMichael Zoran 		(win_fmt)->chromakey,	\
141212afb97SMichael Zoran 		(win_fmt)->clips, (win_fmt)->clipcount,	\
142212afb97SMichael Zoran 		(win_fmt)->bitmap); \
143212afb97SMichael Zoran }
144