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