xref: /linux/drivers/media/pci/cx25821/cx25821.h (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  Driver for the Conexant CX25821 PCIe bridge
4  *
5  *  Copyright (C) 2009 Conexant Systems Inc.
6  *  Authors  <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
7  *  Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
8  */
9 
10 #ifndef CX25821_H_
11 #define CX25821_H_
12 
13 #include <linux/pci.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/delay.h>
17 #include <linux/sched.h>
18 #include <linux/kdev_t.h>
19 
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/videobuf2-v4l2.h>
24 #include <media/videobuf2-dma-sg.h>
25 
26 #include "cx25821-reg.h"
27 #include "cx25821-medusa-reg.h"
28 #include "cx25821-sram.h"
29 #include "cx25821-audio.h"
30 
31 #include <linux/mutex.h>
32 
33 #define UNSET (-1U)
34 #define NO_SYNC_LINE (-1U)
35 
36 #define CX25821_MAXBOARDS 2
37 
38 #define LINE_SIZE_D1    1440
39 
40 /* Number of decoders and encoders */
41 #define MAX_DECODERS            8
42 #define MAX_ENCODERS            2
43 #define QUAD_DECODERS           4
44 #define MAX_CAMERAS             16
45 
46 /* Max number of inputs by card */
47 #define MAX_CX25821_INPUT     8
48 #define RESOURCE_VIDEO0       1
49 #define RESOURCE_VIDEO1       2
50 #define RESOURCE_VIDEO2       4
51 #define RESOURCE_VIDEO3       8
52 #define RESOURCE_VIDEO4       16
53 #define RESOURCE_VIDEO5       32
54 #define RESOURCE_VIDEO6       64
55 #define RESOURCE_VIDEO7       128
56 #define RESOURCE_VIDEO8       256
57 #define RESOURCE_VIDEO9       512
58 #define RESOURCE_VIDEO10      1024
59 #define RESOURCE_VIDEO11      2048
60 
61 #define BUFFER_TIMEOUT     (HZ)	/* 0.5 seconds */
62 
63 #define UNKNOWN_BOARD        0
64 #define CX25821_BOARD        1
65 
66 /* Currently supported by the driver */
67 #define CX25821_NORMS (\
68 	V4L2_STD_NTSC_M |  V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_M_KR | \
69 	V4L2_STD_PAL_BG |  V4L2_STD_PAL_DK    |  V4L2_STD_PAL_I    | \
70 	V4L2_STD_PAL_M  |  V4L2_STD_PAL_N     |  V4L2_STD_PAL_H    | \
71 	V4L2_STD_PAL_Nc)
72 
73 #define CX25821_BOARD_CONEXANT_ATHENA10 1
74 #define MAX_VID_CHANNEL_NUM     12
75 
76 /*
77  * Maximum capture-only channels. This can go away once video/audio output
78  * is fully supported in this driver.
79  */
80 #define MAX_VID_CAP_CHANNEL_NUM     10
81 
82 #define VID_CHANNEL_NUM 8
83 
84 struct cx25821_fmt {
85 	u32 fourcc;		/* v4l2 format id */
86 	int depth;
87 	int flags;
88 	u32 cxformat;
89 };
90 
91 struct cx25821_tvnorm {
92 	char *name;
93 	v4l2_std_id id;
94 	u32 cxiformat;
95 	u32 cxoformat;
96 };
97 
98 enum cx25821_src_sel_type {
99 	CX25821_SRC_SEL_EXT_656_VIDEO = 0,
100 	CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
101 };
102 
103 struct cx25821_riscmem {
104 	unsigned int   size;
105 	__le32         *cpu;
106 	__le32         *jmp;
107 	dma_addr_t     dma;
108 };
109 
110 /* buffer for one video frame */
111 struct cx25821_buffer {
112 	/* common v4l buffer stuff -- must be first */
113 	struct vb2_v4l2_buffer vb;
114 	struct list_head queue;
115 
116 	/* cx25821 specific */
117 	unsigned int bpl;
118 	struct cx25821_riscmem risc;
119 	const struct cx25821_fmt *fmt;
120 };
121 
122 enum port {
123 	CX25821_UNDEFINED = 0,
124 	CX25821_RAW,
125 	CX25821_264
126 };
127 
128 struct cx25821_board {
129 	const char *name;
130 	enum port porta;
131 	enum port portb;
132 	enum port portc;
133 
134 	u32 clk_freq;
135 };
136 
137 struct cx25821_i2c {
138 	struct cx25821_dev *dev;
139 
140 	int nr;
141 
142 	/* i2c i/o */
143 	struct i2c_adapter i2c_adap;
144 	struct i2c_client i2c_client;
145 	u32 i2c_rc;
146 
147 	/* cx25821 registers used for raw address */
148 	u32 i2c_period;
149 	u32 reg_ctrl;
150 	u32 reg_stat;
151 	u32 reg_addr;
152 	u32 reg_rdata;
153 	u32 reg_wdata;
154 };
155 
156 struct cx25821_dmaqueue {
157 	struct list_head active;
158 	u32 count;
159 };
160 
161 struct cx25821_dev;
162 
163 struct cx25821_channel;
164 
165 struct cx25821_video_out_data {
166 	struct cx25821_channel *chan;
167 	int _line_size;
168 	int _prog_cnt;
169 	int _pixel_format;
170 	int _is_first_frame;
171 	int _is_running;
172 	int _file_status;
173 	int _lines_count;
174 	int _frame_count;
175 	unsigned int _risc_size;
176 
177 	__le32 *_dma_virt_start_addr;
178 	__le32 *_dma_virt_addr;
179 	dma_addr_t _dma_phys_addr;
180 	dma_addr_t _dma_phys_start_addr;
181 
182 	unsigned int _data_buf_size;
183 	__le32 *_data_buf_virt_addr;
184 	dma_addr_t _data_buf_phys_addr;
185 
186 	u32 upstream_riscbuf_size;
187 	u32 upstream_databuf_size;
188 	int is_60hz;
189 	int _frame_index;
190 	int cur_frame_index;
191 	int curpos;
192 	wait_queue_head_t waitq;
193 };
194 
195 struct cx25821_channel {
196 	unsigned id;
197 	struct cx25821_dev *dev;
198 
199 	struct v4l2_ctrl_handler hdl;
200 
201 	struct video_device vdev;
202 	struct cx25821_dmaqueue dma_vidq;
203 	struct vb2_queue vidq;
204 
205 	const struct sram_channel *sram_channels;
206 
207 	const struct cx25821_fmt *fmt;
208 	unsigned field;
209 	unsigned int width, height;
210 	int pixel_formats;
211 	int use_cif_resolution;
212 	int cif_width;
213 
214 	/* video output data for the video output channel */
215 	struct cx25821_video_out_data *out;
216 };
217 
218 struct snd_card;
219 
220 struct cx25821_dev {
221 	struct v4l2_device v4l2_dev;
222 
223 	/* pci stuff */
224 	struct pci_dev *pci;
225 	unsigned char pci_rev, pci_lat;
226 	int pci_bus, pci_slot;
227 	u32 base_io_addr;
228 	u32 __iomem *lmmio;
229 	u8 __iomem *bmmio;
230 	int pci_irqmask;
231 	int hwrevision;
232 	/* used by cx25821-alsa */
233 	struct snd_card *card;
234 
235 	u32 clk_freq;
236 
237 	/* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
238 	struct cx25821_i2c i2c_bus[3];
239 
240 	int nr;
241 	struct mutex lock;
242 
243 	struct cx25821_channel channels[MAX_VID_CHANNEL_NUM];
244 
245 	/* board details */
246 	unsigned int board;
247 	char name[32];
248 
249 	/* Analog video */
250 	unsigned int input;
251 	v4l2_std_id tvnorm;
252 	unsigned short _max_num_decoders;
253 
254 	/* Analog Audio Upstream */
255 	int _audio_is_running;
256 	int _audiopixel_format;
257 	int _is_first_audio_frame;
258 	int _audiofile_status;
259 	int _audio_lines_count;
260 	int _audioframe_count;
261 	int _audio_upstream_channel;
262 	int _last_index_irq;    /* The last interrupt index processed. */
263 
264 	__le32 *_risc_audio_jmp_addr;
265 	__le32 *_risc_virt_start_addr;
266 	__le32 *_risc_virt_addr;
267 	dma_addr_t _risc_phys_addr;
268 	dma_addr_t _risc_phys_start_addr;
269 
270 	unsigned int _audiorisc_size;
271 	unsigned int _audiodata_buf_size;
272 	__le32 *_audiodata_buf_virt_addr;
273 	dma_addr_t _audiodata_buf_phys_addr;
274 	char *_audiofilename;
275 	u32 audio_upstream_riscbuf_size;
276 	u32 audio_upstream_databuf_size;
277 	int _audioframe_index;
278 	struct work_struct _audio_work_entry;
279 	char *input_audiofilename;
280 
281 	/* V4l */
282 	spinlock_t slock;
283 
284 	/* Video Upstream */
285 	struct cx25821_video_out_data vid_out_data[2];
286 };
287 
288 static inline struct cx25821_dev *get_cx25821(struct v4l2_device *v4l2_dev)
289 {
290 	return container_of(v4l2_dev, struct cx25821_dev, v4l2_dev);
291 }
292 
293 extern struct cx25821_board cx25821_boards[];
294 
295 #define SRAM_CH00  0		/* Video A */
296 #define SRAM_CH01  1		/* Video B */
297 #define SRAM_CH02  2		/* Video C */
298 #define SRAM_CH03  3		/* Video D */
299 #define SRAM_CH04  4		/* Video E */
300 #define SRAM_CH05  5		/* Video F */
301 #define SRAM_CH06  6		/* Video G */
302 #define SRAM_CH07  7		/* Video H */
303 
304 #define SRAM_CH08  8		/* Audio A */
305 #define SRAM_CH09  9		/* Video Upstream I */
306 #define SRAM_CH10  10		/* Video Upstream J */
307 #define SRAM_CH11  11		/* Audio Upstream AUD_CHANNEL_B */
308 
309 #define VID_UPSTREAM_SRAM_CHANNEL_I     SRAM_CH09
310 #define VID_UPSTREAM_SRAM_CHANNEL_J     SRAM_CH10
311 #define AUDIO_UPSTREAM_SRAM_CHANNEL_B   SRAM_CH11
312 
313 struct sram_channel {
314 	char *name;
315 	u32 i;
316 	u32 cmds_start;
317 	u32 ctrl_start;
318 	u32 cdt;
319 	u32 fifo_start;
320 	u32 fifo_size;
321 	u32 ptr1_reg;
322 	u32 ptr2_reg;
323 	u32 cnt1_reg;
324 	u32 cnt2_reg;
325 	u32 int_msk;
326 	u32 int_stat;
327 	u32 int_mstat;
328 	u32 dma_ctl;
329 	u32 gpcnt_ctl;
330 	u32 gpcnt;
331 	u32 aud_length;
332 	u32 aud_cfg;
333 	u32 fld_aud_fifo_en;
334 	u32 fld_aud_risc_en;
335 
336 	/* For Upstream Video */
337 	u32 vid_fmt_ctl;
338 	u32 vid_active_ctl1;
339 	u32 vid_active_ctl2;
340 	u32 vid_cdt_size;
341 
342 	u32 vip_ctl;
343 	u32 pix_frmt;
344 	u32 jumponly;
345 	u32 irq_bit;
346 };
347 
348 extern const struct sram_channel cx25821_sram_channels[];
349 
350 #define cx_read(reg)             readl(dev->lmmio + ((reg)>>2))
351 #define cx_write(reg, value)     writel((value), dev->lmmio + ((reg)>>2))
352 
353 #define cx_andor(reg, mask, value) \
354 	writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
355 	((value) & (mask)), dev->lmmio+((reg)>>2))
356 
357 #define cx_set(reg, bit)          cx_andor((reg), (bit), (bit))
358 #define cx_clear(reg, bit)        cx_andor((reg), (bit), 0)
359 
360 #define Set_GPIO_Bit(Bit)                       (1 << Bit)
361 #define Clear_GPIO_Bit(Bit)                     (~(1 << Bit))
362 
363 #define CX25821_ERR(fmt, args...)			\
364 	pr_err("(%d): " fmt, dev->board, ##args)
365 #define CX25821_WARN(fmt, args...)			\
366 	pr_warn("(%d): " fmt, dev->board, ##args)
367 #define CX25821_INFO(fmt, args...)			\
368 	pr_info("(%d): " fmt, dev->board, ##args)
369 
370 extern int cx25821_i2c_register(struct cx25821_i2c *bus);
371 extern int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value);
372 extern int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value);
373 extern int cx25821_i2c_unregister(struct cx25821_i2c *bus);
374 extern void cx25821_gpio_init(struct cx25821_dev *dev);
375 extern void cx25821_set_gpiopin_direction(struct cx25821_dev *dev,
376 					  int pin_number, int pin_logic_value);
377 
378 extern int medusa_video_init(struct cx25821_dev *dev);
379 extern int medusa_set_videostandard(struct cx25821_dev *dev);
380 extern void medusa_set_resolution(struct cx25821_dev *dev, int width,
381 				  int decoder_select);
382 extern int medusa_set_brightness(struct cx25821_dev *dev, int brightness,
383 				 int decoder);
384 extern int medusa_set_contrast(struct cx25821_dev *dev, int contrast,
385 			       int decoder);
386 extern int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder);
387 extern int medusa_set_saturation(struct cx25821_dev *dev, int saturation,
388 				 int decoder);
389 
390 extern int cx25821_sram_channel_setup(struct cx25821_dev *dev,
391 				      const struct sram_channel *ch, unsigned int bpl,
392 				      u32 risc);
393 
394 extern int cx25821_riscmem_alloc(struct pci_dev *pci,
395 				 struct cx25821_riscmem *risc,
396 				 unsigned int size);
397 extern int cx25821_risc_buffer(struct pci_dev *pci, struct cx25821_riscmem *risc,
398 			       struct scatterlist *sglist,
399 			       unsigned int top_offset,
400 			       unsigned int bottom_offset,
401 			       unsigned int bpl,
402 			       unsigned int padding, unsigned int lines);
403 extern int cx25821_risc_databuffer_audio(struct pci_dev *pci,
404 					 struct cx25821_riscmem *risc,
405 					 struct scatterlist *sglist,
406 					 unsigned int bpl,
407 					 unsigned int lines, unsigned int lpi);
408 extern void cx25821_free_buffer(struct cx25821_dev *dev,
409 				struct cx25821_buffer *buf);
410 extern void cx25821_sram_channel_dump(struct cx25821_dev *dev,
411 				      const struct sram_channel *ch);
412 extern void cx25821_sram_channel_dump_audio(struct cx25821_dev *dev,
413 					    const struct sram_channel *ch);
414 
415 extern struct cx25821_dev *cx25821_dev_get(struct pci_dev *pci);
416 extern void cx25821_print_irqbits(char *name, char *tag, char **strings,
417 				  int len, u32 bits, u32 mask);
418 extern void cx25821_dev_unregister(struct cx25821_dev *dev);
419 extern int cx25821_sram_channel_setup_audio(struct cx25821_dev *dev,
420 					    const struct sram_channel *ch,
421 					    unsigned int bpl, u32 risc);
422 
423 extern void cx25821_set_pixel_format(struct cx25821_dev *dev, int channel,
424 				     u32 format);
425 
426 #endif
427