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