1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __SAA7146_VV__
3 #define __SAA7146_VV__
4 
5 #include <media/v4l2-common.h>
6 #include <media/v4l2-ioctl.h>
7 #include <media/v4l2-fh.h>
8 #include <media/drv-intf/saa7146.h>
9 #include <media/videobuf-dma-sg.h>
10 
11 #define MAX_SAA7146_CAPTURE_BUFFERS	32	/* arbitrary */
12 #define BUFFER_TIMEOUT     (HZ/2)  /* 0.5 seconds */
13 
14 #define WRITE_RPS0(x) do { \
15 	dev->d_rps0.cpu_addr[ count++ ] = cpu_to_le32(x); \
16 	} while (0);
17 
18 #define WRITE_RPS1(x) do { \
19 	dev->d_rps1.cpu_addr[ count++ ] = cpu_to_le32(x); \
20 	} while (0);
21 
22 struct	saa7146_video_dma {
23 	u32 base_odd;
24 	u32 base_even;
25 	u32 prot_addr;
26 	u32 pitch;
27 	u32 base_page;
28 	u32 num_line_byte;
29 };
30 
31 #define FORMAT_BYTE_SWAP	0x1
32 #define FORMAT_IS_PLANAR	0x2
33 
34 struct saa7146_format {
35 	char	*name;
36 	u32	pixelformat;
37 	u32	trans;
38 	u8	depth;
39 	u8	flags;
40 	u8	swap;
41 };
42 
43 struct saa7146_standard
44 {
45 	char          *name;
46 	v4l2_std_id   id;
47 
48 	int v_offset;	/* number of lines of vertical offset before processing */
49 	int v_field;	/* number of lines in a field for HPS to process */
50 
51 	int h_offset;	/* horizontal offset of processing window */
52 	int h_pixels;	/* number of horizontal pixels to process */
53 
54 	int v_max_out;
55 	int h_max_out;
56 };
57 
58 /* buffer for one video/vbi frame */
59 struct saa7146_buf {
60 	/* common v4l buffer stuff -- must be first */
61 	struct videobuf_buffer vb;
62 
63 	/* saa7146 specific */
64 	struct v4l2_pix_format  *fmt;
65 	int (*activate)(struct saa7146_dev *dev,
66 			struct saa7146_buf *buf,
67 			struct saa7146_buf *next);
68 
69 	/* page tables */
70 	struct saa7146_pgtable  pt[3];
71 };
72 
73 struct saa7146_dmaqueue {
74 	struct saa7146_dev	*dev;
75 	struct saa7146_buf	*curr;
76 	struct list_head	queue;
77 	struct timer_list	timeout;
78 };
79 
80 struct saa7146_overlay {
81 	struct saa7146_fh	*fh;
82 	struct v4l2_window	win;
83 	struct v4l2_clip	clips[16];
84 	int			nclips;
85 };
86 
87 /* per open data */
88 struct saa7146_fh {
89 	/* Must be the first field! */
90 	struct v4l2_fh		fh;
91 	struct saa7146_dev	*dev;
92 
93 	/* video capture */
94 	struct videobuf_queue	video_q;
95 
96 	/* vbi capture */
97 	struct videobuf_queue	vbi_q;
98 
99 	unsigned int resources;	/* resource management for device open */
100 };
101 
102 #define STATUS_OVERLAY	0x01
103 #define STATUS_CAPTURE	0x02
104 
105 struct saa7146_vv
106 {
107 	/* vbi capture */
108 	struct saa7146_dmaqueue		vbi_dmaq;
109 	struct v4l2_vbi_format		vbi_fmt;
110 	struct timer_list		vbi_read_timeout;
111 	struct file			*vbi_read_timeout_file;
112 	/* vbi workaround interrupt queue */
113 	wait_queue_head_t		vbi_wq;
114 	int				vbi_fieldcount;
115 	struct saa7146_fh		*vbi_streaming;
116 
117 	int				video_status;
118 	struct saa7146_fh		*video_fh;
119 
120 	/* video overlay */
121 	struct saa7146_overlay		ov;
122 	struct v4l2_framebuffer		ov_fb;
123 	struct saa7146_format		*ov_fmt;
124 	struct saa7146_fh		*ov_suspend;
125 
126 	/* video capture */
127 	struct saa7146_dmaqueue		video_dmaq;
128 	struct v4l2_pix_format		video_fmt;
129 	enum v4l2_field			last_field;
130 
131 	/* common: fixme? shouldn't this be in saa7146_fh?
132 	   (this leads to a more complicated question: shall the driver
133 	   store the different settings (for example S_INPUT) for every open
134 	   and restore it appropriately, or should all settings be common for
135 	   all opens? currently, we do the latter, like all other
136 	   drivers do... */
137 	struct saa7146_standard	*standard;
138 
139 	int	vflip;
140 	int	hflip;
141 	int	current_hps_source;
142 	int	current_hps_sync;
143 
144 	struct saa7146_dma	d_clipping;	/* pointer to clipping memory */
145 
146 	unsigned int resources;	/* resource management for device */
147 };
148 
149 /* flags */
150 #define SAA7146_USE_PORT_B_FOR_VBI	0x2     /* use input port b for vbi hardware bug workaround */
151 
152 struct saa7146_ext_vv
153 {
154 	/* information about the video capabilities of the device */
155 	int	inputs;
156 	int	audios;
157 	u32	capabilities;
158 	int	flags;
159 
160 	/* additionally supported transmission standards */
161 	struct saa7146_standard *stds;
162 	int num_stds;
163 	int (*std_callback)(struct saa7146_dev*, struct saa7146_standard *);
164 
165 	/* the extension can override this */
166 	struct v4l2_ioctl_ops vid_ops;
167 	struct v4l2_ioctl_ops vbi_ops;
168 	/* pointer to the saa7146 core ops */
169 	const struct v4l2_ioctl_ops *core_ops;
170 
171 	struct v4l2_file_operations vbi_fops;
172 };
173 
174 struct saa7146_use_ops  {
175 	void (*init)(struct saa7146_dev *, struct saa7146_vv *);
176 	int(*open)(struct saa7146_dev *, struct file *);
177 	void (*release)(struct saa7146_dev *, struct file *);
178 	void (*irq_done)(struct saa7146_dev *, unsigned long status);
179 	ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
180 };
181 
182 /* from saa7146_fops.c */
183 int saa7146_register_device(struct video_device *vid, struct saa7146_dev *dev, char *name, int type);
184 int saa7146_unregister_device(struct video_device *vid, struct saa7146_dev *dev);
185 void saa7146_buffer_finish(struct saa7146_dev *dev, struct saa7146_dmaqueue *q, int state);
186 void saa7146_buffer_next(struct saa7146_dev *dev, struct saa7146_dmaqueue *q,int vbi);
187 int saa7146_buffer_queue(struct saa7146_dev *dev, struct saa7146_dmaqueue *q, struct saa7146_buf *buf);
188 void saa7146_buffer_timeout(struct timer_list *t);
189 void saa7146_dma_free(struct saa7146_dev* dev,struct videobuf_queue *q,
190 						struct saa7146_buf *buf);
191 
192 int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv);
193 int saa7146_vv_release(struct saa7146_dev* dev);
194 
195 /* from saa7146_hlp.c */
196 int saa7146_enable_overlay(struct saa7146_fh *fh);
197 void saa7146_disable_overlay(struct saa7146_fh *fh);
198 
199 void saa7146_set_capture(struct saa7146_dev *dev, struct saa7146_buf *buf, struct saa7146_buf *next);
200 void saa7146_write_out_dma(struct saa7146_dev* dev, int which, struct saa7146_video_dma* vdma) ;
201 void saa7146_set_hps_source_and_sync(struct saa7146_dev *saa, int source, int sync);
202 void saa7146_set_gpio(struct saa7146_dev *saa, u8 pin, u8 data);
203 
204 /* from saa7146_video.c */
205 extern const struct v4l2_ioctl_ops saa7146_video_ioctl_ops;
206 extern const struct v4l2_ioctl_ops saa7146_vbi_ioctl_ops;
207 extern const struct saa7146_use_ops saa7146_video_uops;
208 int saa7146_start_preview(struct saa7146_fh *fh);
209 int saa7146_stop_preview(struct saa7146_fh *fh);
210 long saa7146_video_do_ioctl(struct file *file, unsigned int cmd, void *arg);
211 int saa7146_s_ctrl(struct v4l2_ctrl *ctrl);
212 
213 /* from saa7146_vbi.c */
214 extern const struct saa7146_use_ops saa7146_vbi_uops;
215 
216 /* resource management functions */
217 int saa7146_res_get(struct saa7146_fh *fh, unsigned int bit);
218 void saa7146_res_free(struct saa7146_fh *fh, unsigned int bits);
219 
220 #define RESOURCE_DMA1_HPS	0x1
221 #define RESOURCE_DMA2_CLP	0x2
222 #define RESOURCE_DMA3_BRS	0x4
223 
224 /* saa7146 source inputs */
225 #define SAA7146_HPS_SOURCE_PORT_A	0x00
226 #define SAA7146_HPS_SOURCE_PORT_B	0x01
227 #define SAA7146_HPS_SOURCE_YPB_CPA	0x02
228 #define SAA7146_HPS_SOURCE_YPA_CPB	0x03
229 
230 /* sync inputs */
231 #define SAA7146_HPS_SYNC_PORT_A		0x00
232 #define SAA7146_HPS_SYNC_PORT_B		0x01
233 
234 /* some memory sizes */
235 /* max. 16 clipping rectangles */
236 #define SAA7146_CLIPPING_MEM	(16 * 4 * sizeof(u32))
237 
238 /* some defines for the various clipping-modes */
239 #define SAA7146_CLIPPING_RECT		0x4
240 #define SAA7146_CLIPPING_RECT_INVERTED	0x5
241 #define SAA7146_CLIPPING_MASK		0x6
242 #define SAA7146_CLIPPING_MASK_INVERTED	0x7
243 
244 /* output formats: each entry holds four information */
245 #define RGB08_COMPOSED	0x0217 /* composed is used in the sense of "not-planar" */
246 /* this means: planar?=0, yuv2rgb-conversation-mode=2, dither=yes(=1), format-mode = 7 */
247 #define RGB15_COMPOSED	0x0213
248 #define RGB16_COMPOSED	0x0210
249 #define RGB24_COMPOSED	0x0201
250 #define RGB32_COMPOSED	0x0202
251 
252 #define Y8			0x0006
253 #define YUV411_COMPOSED		0x0003
254 #define YUV422_COMPOSED		0x0000
255 /* this means: planar?=1, yuv2rgb-conversion-mode=0, dither=no(=0), format-mode = b */
256 #define YUV411_DECOMPOSED	0x100b
257 #define YUV422_DECOMPOSED	0x1009
258 #define YUV420_DECOMPOSED	0x100a
259 
260 #define IS_PLANAR(x) (x & 0xf000)
261 
262 /* misc defines */
263 #define SAA7146_NO_SWAP		(0x0)
264 #define SAA7146_TWO_BYTE_SWAP	(0x1)
265 #define SAA7146_FOUR_BYTE_SWAP	(0x2)
266 
267 #endif
268