xref: /linux/drivers/media/pci/tw68/tw68.h (revision b85d45947951d23cb22d90caecf4c1eb81342c96)
1 /*
2  *  tw68 driver common header file
3  *
4  *  Much of this code is derived from the cx88 and sa7134 drivers, which
5  *  were in turn derived from the bt87x driver.  The original work was by
6  *  Gerd Knorr; more recently the code was enhanced by Mauro Carvalho Chehab,
7  *  Hans Verkuil, Andy Walls and many others.  Their work is gratefully
8  *  acknowledged.  Full credit goes to them - any problems within this code
9  *  are mine.
10  *
11  *  Copyright (C) 2009  William M. Brack
12  *
13  *  Refactored and updated to the latest v4l core frameworks:
14  *
15  *  Copyright (C) 2014 Hans Verkuil <hverkuil@xs4all.nl>
16  *
17  *  This program is free software; you can redistribute it and/or modify
18  *  it under the terms of the GNU General Public License as published by
19  *  the Free Software Foundation; either version 2 of the License, or
20  *  (at your option) any later version.
21  *
22  *  This program is distributed in the hope that it will be useful,
23  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  *  GNU General Public License for more details.
26  */
27 
28 #include <linux/pci.h>
29 #include <linux/videodev2.h>
30 #include <linux/notifier.h>
31 #include <linux/delay.h>
32 #include <linux/mutex.h>
33 #include <linux/io.h>
34 
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-ioctl.h>
37 #include <media/v4l2-ctrls.h>
38 #include <media/v4l2-device.h>
39 #include <media/videobuf2-dma-sg.h>
40 
41 #include "tw68-reg.h"
42 
43 #define	UNSET	(-1U)
44 
45 #define TW68_NORMS ( \
46 	V4L2_STD_NTSC    | V4L2_STD_PAL       | V4L2_STD_SECAM    | \
47 	V4L2_STD_PAL_M   | V4L2_STD_PAL_Nc    | V4L2_STD_PAL_60)
48 
49 #define	TW68_VID_INTS	(TW68_FFERR | TW68_PABORT | TW68_DMAPERR | \
50 			 TW68_FFOF   | TW68_DMAPI)
51 /* TW6800 chips have trouble with these, so we don't set them for that chip */
52 #define	TW68_VID_INTSX	(TW68_FDMIS | TW68_HLOCK | TW68_VLOCK)
53 
54 #define	TW68_I2C_INTS	(TW68_SBERR | TW68_SBDONE | TW68_SBERR2  | \
55 			 TW68_SBDONE2)
56 
57 enum tw68_decoder_type {
58 	TW6800,
59 	TW6801,
60 	TW6804,
61 	TWXXXX,
62 };
63 
64 /* ----------------------------------------------------------- */
65 /* static data                                                 */
66 
67 struct tw68_tvnorm {
68 	char		*name;
69 	v4l2_std_id	id;
70 
71 	/* video decoder */
72 	u32	sync_control;
73 	u32	luma_control;
74 	u32	chroma_ctrl1;
75 	u32	chroma_gain;
76 	u32	chroma_ctrl2;
77 	u32	vgate_misc;
78 
79 	/* video scaler */
80 	u32	h_delay;
81 	u32	h_delay0;	/* for TW6800 */
82 	u32	h_start;
83 	u32	h_stop;
84 	u32	v_delay;
85 	u32	video_v_start;
86 	u32	video_v_stop;
87 	u32	vbi_v_start_0;
88 	u32	vbi_v_stop_0;
89 	u32	vbi_v_start_1;
90 
91 	/* Techwell specific */
92 	u32	format;
93 };
94 
95 struct tw68_format {
96 	char	*name;
97 	u32	fourcc;
98 	u32	depth;
99 	u32	twformat;
100 };
101 
102 /* ----------------------------------------------------------- */
103 /* card configuration					  */
104 
105 #define TW68_BOARD_NOAUTO		UNSET
106 #define TW68_BOARD_UNKNOWN		0
107 #define	TW68_BOARD_GENERIC_6802		1
108 
109 #define	TW68_MAXBOARDS			16
110 #define	TW68_INPUT_MAX			4
111 
112 /* ----------------------------------------------------------- */
113 /* device / file handle status                                 */
114 
115 #define	BUFFER_TIMEOUT	msecs_to_jiffies(500)	/* 0.5 seconds */
116 
117 struct tw68_dev;	/* forward delclaration */
118 
119 /* buffer for one video/vbi/ts frame */
120 struct tw68_buf {
121 	struct vb2_buffer vb;
122 	struct list_head list;
123 
124 	unsigned int   size;
125 	__le32         *cpu;
126 	__le32         *jmp;
127 	dma_addr_t     dma;
128 };
129 
130 struct tw68_fmt {
131 	char			*name;
132 	u32			fourcc;	/* v4l2 format id */
133 	int			depth;
134 	int			flags;
135 	u32			twformat;
136 };
137 
138 /* global device status */
139 struct tw68_dev {
140 	struct mutex		lock;
141 	spinlock_t		slock;
142 	u16			instance;
143 	struct v4l2_device	v4l2_dev;
144 
145 	/* various device info */
146 	enum tw68_decoder_type	vdecoder;
147 	struct video_device	vdev;
148 	struct v4l2_ctrl_handler hdl;
149 
150 	/* pci i/o */
151 	char			*name;
152 	struct pci_dev		*pci;
153 	unsigned char		pci_rev, pci_lat;
154 	u32			__iomem *lmmio;
155 	u8			__iomem *bmmio;
156 	u32			pci_irqmask;
157 	/* The irq mask to be used will depend upon the chip type */
158 	u32			board_virqmask;
159 
160 	/* video capture */
161 	const struct tw68_format *fmt;
162 	unsigned		width, height;
163 	unsigned		seqnr;
164 	unsigned		field;
165 	struct vb2_queue	vidq;
166 	struct list_head	active;
167 	void			*alloc_ctx;
168 
169 	/* various v4l controls */
170 	const struct tw68_tvnorm *tvnorm;	/* video */
171 
172 	int			input;
173 };
174 
175 /* ----------------------------------------------------------- */
176 
177 #define tw_readl(reg)		readl(dev->lmmio + ((reg) >> 2))
178 #define	tw_readb(reg)		readb(dev->bmmio + (reg))
179 #define tw_writel(reg, value)	writel((value), dev->lmmio + ((reg) >> 2))
180 #define	tw_writeb(reg, value)	writeb((value), dev->bmmio + (reg))
181 
182 #define tw_andorl(reg, mask, value) \
183 		writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
184 		((value) & (mask)), dev->lmmio+((reg)>>2))
185 #define	tw_andorb(reg, mask, value) \
186 		writeb((readb(dev->bmmio + (reg)) & ~(mask)) |\
187 		((value) & (mask)), dev->bmmio+(reg))
188 #define tw_setl(reg, bit)	tw_andorl((reg), (bit), (bit))
189 #define	tw_setb(reg, bit)	tw_andorb((reg), (bit), (bit))
190 #define	tw_clearl(reg, bit)	\
191 		writel((readl(dev->lmmio + ((reg) >> 2)) & ~(bit)), \
192 		dev->lmmio + ((reg) >> 2))
193 #define	tw_clearb(reg, bit)	\
194 		writeb((readb(dev->bmmio+(reg)) & ~(bit)), \
195 		dev->bmmio + (reg))
196 
197 #define tw_wait(us) { udelay(us); }
198 
199 /* ----------------------------------------------------------- */
200 /* tw68-video.c                                                */
201 
202 void tw68_set_tvnorm_hw(struct tw68_dev *dev);
203 
204 int tw68_video_init1(struct tw68_dev *dev);
205 int tw68_video_init2(struct tw68_dev *dev, int video_nr);
206 void tw68_irq_video_done(struct tw68_dev *dev, unsigned long status);
207 int tw68_video_start_dma(struct tw68_dev *dev, struct tw68_buf *buf);
208 
209 /* ----------------------------------------------------------- */
210 /* tw68-risc.c                                                 */
211 
212 int tw68_risc_buffer(struct pci_dev *pci, struct tw68_buf *buf,
213 	struct scatterlist *sglist, unsigned int top_offset,
214 	unsigned int bottom_offset, unsigned int bpl,
215 	unsigned int padding, unsigned int lines);
216