1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * zoran - Iomega Buz driver
4 *
5 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
6 *
7 * based on
8 *
9 * zoran.0.0.3 Copyright (C) 1998 Dave Perks <dperks@ibm.net>
10 *
11 * and
12 *
13 * bttv - Bt848 frame grabber driver
14 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
15 * & Marcus Metzler (mocm@thp.uni-koeln.de)
16 */
17
18 #ifndef _BUZ_H_
19 #define _BUZ_H_
20
21 #include <linux/debugfs.h>
22 #include <linux/pci.h>
23 #include <linux/i2c-algo-bit.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/videobuf2-core.h>
27 #include <media/videobuf2-v4l2.h>
28 #include <media/videobuf2-dma-contig.h>
29
30 #define ZR_NORM_PAL 0
31 #define ZR_NORM_NTSC 1
32 #define ZR_NORM_SECAM 2
33
34 struct zr_buffer {
35 /* common v4l buffer stuff -- must be first */
36 struct vb2_v4l2_buffer vbuf;
37 struct list_head queue;
38 };
39
vb2_to_zr_buffer(struct vb2_buffer * vb)40 static inline struct zr_buffer *vb2_to_zr_buffer(struct vb2_buffer *vb)
41 {
42 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
43
44 return container_of(vbuf, struct zr_buffer, vbuf);
45 }
46
47 #define ZORAN_NAME "ZORAN" /* name of the device */
48
49 #define ZR_DEVNAME(zr) ((zr)->name)
50
51 #define BUZ_MAX_WIDTH (zr->timing->wa)
52 #define BUZ_MAX_HEIGHT (zr->timing->ha)
53 #define BUZ_MIN_WIDTH 32 /* never display less than 32 pixels */
54 #define BUZ_MIN_HEIGHT 24 /* never display less than 24 rows */
55
56 #define BUZ_NUM_STAT_COM 4
57 #define BUZ_MASK_STAT_COM 3
58
59 #define BUZ_MAX_INPUT 16
60
61 #include "zr36057.h"
62
63 enum card_type {
64 UNKNOWN = -1,
65
66 /* Pinnacle/Miro */
67 DC10_OLD, /* DC30 like */
68 DC10_NEW, /* DC10_PLUS like */
69 DC10_PLUS,
70 DC30,
71 DC30_PLUS,
72
73 /* Linux Media Labs */
74 LML33,
75 LML33R10,
76
77 /* Iomega */
78 BUZ,
79
80 /* AverMedia */
81 AVS6EYES,
82
83 /* total number of cards */
84 NUM_CARDS
85 };
86
87 enum zoran_codec_mode {
88 BUZ_MODE_IDLE, /* nothing going on */
89 BUZ_MODE_MOTION_COMPRESS, /* grabbing frames */
90 BUZ_MODE_MOTION_DECOMPRESS, /* playing frames */
91 BUZ_MODE_STILL_COMPRESS, /* still frame conversion */
92 BUZ_MODE_STILL_DECOMPRESS /* still frame conversion */
93 };
94
95 enum zoran_map_mode {
96 ZORAN_MAP_MODE_NONE,
97 ZORAN_MAP_MODE_RAW,
98 ZORAN_MAP_MODE_JPG_REC,
99 ZORAN_MAP_MODE_JPG_PLAY,
100 };
101
102 enum gpio_type {
103 ZR_GPIO_JPEG_SLEEP = 0,
104 ZR_GPIO_JPEG_RESET,
105 ZR_GPIO_JPEG_FRAME,
106 ZR_GPIO_VID_DIR,
107 ZR_GPIO_VID_EN,
108 ZR_GPIO_VID_RESET,
109 ZR_GPIO_CLK_SEL1,
110 ZR_GPIO_CLK_SEL2,
111 ZR_GPIO_MAX,
112 };
113
114 enum gpcs_type {
115 GPCS_JPEG_RESET = 0,
116 GPCS_JPEG_START,
117 GPCS_MAX,
118 };
119
120 struct zoran_format {
121 char *name;
122 __u32 fourcc;
123 int colorspace;
124 int depth;
125 __u32 flags;
126 __u32 vfespfr;
127 };
128
129 /* flags */
130 #define ZORAN_FORMAT_COMPRESSED BIT(0)
131 #define ZORAN_FORMAT_OVERLAY BIT(1)
132 #define ZORAN_FORMAT_CAPTURE BIT(2)
133 #define ZORAN_FORMAT_PLAYBACK BIT(3)
134
135 /* v4l-capture settings */
136 struct zoran_v4l_settings {
137 int width, height, bytesperline; /* capture size */
138 const struct zoran_format *format; /* capture format */
139 };
140
141 /* jpg-capture/-playback settings */
142 struct zoran_jpg_settings {
143 /* this bit is used to set everything to default */
144 int decimation;
145 /* capture decimation settings (tmp_dcm=1 means both fields) */
146 int hor_dcm, ver_dcm, tmp_dcm;
147 /* field-settings (odd_even=1 (+tmp_dcm=1) means top-field-first) */
148 int field_per_buff, odd_even;
149 /* crop settings (subframe capture) */
150 int img_x, img_y, img_width, img_height;
151 /* JPEG-specific capture settings */
152 struct v4l2_jpegcompression jpg_comp;
153 };
154
155 struct zoran;
156
157 /* zoran_fh contains per-open() settings */
158 struct zoran_fh {
159 struct v4l2_fh fh;
160 struct zoran *zr;
161 };
162
163 struct card_info {
164 enum card_type type;
165 char name[32];
166 const char *i2c_decoder; /* i2c decoder device */
167 const unsigned short *addrs_decoder;
168 const char *i2c_encoder; /* i2c encoder device */
169 const unsigned short *addrs_encoder;
170 u16 video_vfe, video_codec; /* videocodec types */
171 u16 audio_chip; /* audio type */
172
173 int inputs; /* number of video inputs */
174 struct input {
175 int muxsel;
176 char name[32];
177 } input[BUZ_MAX_INPUT];
178
179 v4l2_std_id norms;
180 const struct tvnorm *tvn[3]; /* supported TV norms */
181
182 u32 jpeg_int; /* JPEG interrupt */
183 u32 vsync_int; /* VSYNC interrupt */
184 s8 gpio[ZR_GPIO_MAX];
185 u8 gpcs[GPCS_MAX];
186
187 struct vfe_polarity vfe_pol;
188 u8 gpio_pol[ZR_GPIO_MAX];
189
190 /* is the /GWS line connected? */
191 u8 gws_not_connected;
192
193 /* avs6eyes mux setting */
194 u8 input_mux;
195
196 void (*init)(struct zoran *zr);
197 };
198
199 struct zoran {
200 struct v4l2_device v4l2_dev;
201 struct v4l2_ctrl_handler hdl;
202 struct video_device *video_dev;
203 struct vb2_queue vq;
204
205 struct i2c_adapter i2c_adapter; /* */
206 struct i2c_algo_bit_data i2c_algo; /* */
207 u32 i2cbr;
208
209 struct v4l2_subdev *decoder; /* video decoder sub-device */
210 struct v4l2_subdev *encoder; /* video encoder sub-device */
211
212 struct videocodec *codec; /* video codec */
213 struct videocodec *vfe; /* video front end */
214
215 struct mutex lock; /* file ops serialize lock */
216
217 u8 initialized; /* flag if zoran has been correctly initialized */
218 struct card_info card;
219 const struct tvnorm *timing;
220
221 unsigned short id; /* number of this device */
222 char name[40]; /* name of this device */
223 struct pci_dev *pci_dev; /* PCI device */
224 unsigned char revision; /* revision of zr36057 */
225 unsigned char __iomem *zr36057_mem;/* pointer to mapped IO memory */
226
227 spinlock_t spinlock; /* Spinlock */
228
229 /* Video for Linux parameters */
230 int input; /* card's norm and input */
231 v4l2_std_id norm;
232
233 /* Current buffer params */
234 unsigned int buffer_size;
235
236 struct zoran_v4l_settings v4l_settings; /* structure with a lot of things to play with */
237
238 /* Buz MJPEG parameters */
239 enum zoran_codec_mode codec_mode; /* status of codec */
240 struct zoran_jpg_settings jpg_settings; /* structure with a lot of things to play with */
241
242 /* grab queue counts/indices, mask with BUZ_MASK_STAT_COM before using as index */
243 /* (dma_head - dma_tail) is number active in DMA, must be <= BUZ_NUM_STAT_COM */
244 /* (value & BUZ_MASK_STAT_COM) corresponds to index in stat_com table */
245 unsigned long jpg_que_head; /* Index where to put next buffer which is queued */
246 unsigned long jpg_dma_head; /* Index of next buffer which goes into stat_com */
247 unsigned long jpg_dma_tail; /* Index of last buffer in stat_com */
248 unsigned long jpg_que_tail; /* Index of last buffer in queue */
249 unsigned long jpg_seq_num; /* count of frames since grab/play started */
250 unsigned long jpg_err_seq; /* last seq_num before error */
251 unsigned long jpg_err_shift;
252 unsigned long jpg_queued_num; /* count of frames queued since grab/play started */
253 unsigned long vbseq;
254
255 /* zr36057's code buffer table */
256 /* stat_com[i] is indexed by dma_head/tail & BUZ_MASK_STAT_COM */
257 __le32 *stat_com;
258
259 /* Additional stuff for testing */
260 unsigned int ghost_int;
261 int intr_counter_GIRQ1;
262 int intr_counter_GIRQ0;
263 int intr_counter_cod_rep_irq;
264 int intr_counter_jpeg_rep_irq;
265 int field_counter;
266 int irq1_in;
267 int irq1_out;
268 int jpeg_in;
269 int jpeg_out;
270 int JPEG_0;
271 int JPEG_1;
272 int end_event_missed;
273 int jpeg_missed;
274 int jpeg_error;
275 int num_errors;
276 int jpeg_max_missed;
277 int jpeg_min_missed;
278 unsigned int prepared;
279 unsigned int queued;
280
281 u32 last_isr;
282 unsigned long frame_num;
283 int running;
284 int buf_in_reserve;
285
286 dma_addr_t p_sc;
287 __le32 *stat_comb;
288 dma_addr_t p_scb;
289 enum zoran_map_mode map_mode;
290 struct list_head queued_bufs;
291 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
292 struct zr_buffer *inuse[BUZ_NUM_STAT_COM * 2];
293 struct dentry *dbgfs_dir;
294 };
295
to_zoran(struct v4l2_device * v4l2_dev)296 static inline struct zoran *to_zoran(struct v4l2_device *v4l2_dev)
297 {
298 return container_of(v4l2_dev, struct zoran, v4l2_dev);
299 }
300
301 /*
302 * There was something called _ALPHA_BUZ that used the PCI address instead of
303 * the kernel iomapped address for btread/btwrite.
304 */
305 #define btwrite(dat, adr) writel((dat), zr->zr36057_mem + (adr))
306 #define btread(adr) readl(zr->zr36057_mem + (adr))
307
308 #define btand(dat, adr) btwrite((dat) & btread(adr), (adr))
309 #define btor(dat, adr) btwrite((dat) | btread(adr), (adr))
310 #define btaor(dat, mask, adr) btwrite((dat) | ((mask) & btread(adr)), (adr))
311
312 #endif
313
314 /*
315 * Debugging macros
316 */
317 #define zrdev_dbg(zr, format, args...) \
318 pci_dbg((zr)->pci_dev, format, ##args) \
319
320 #define zrdev_err(zr, format, args...) \
321 pci_err((zr)->pci_dev, format, ##args) \
322
323 #define zrdev_info(zr, format, args...) \
324 pci_info((zr)->pci_dev, format, ##args) \
325
326 int zoran_queue_init(struct zoran *zr, struct vb2_queue *vq, int dir);
327 void zoran_queue_exit(struct zoran *zr);
328 int zr_set_buf(struct zoran *zr);
329