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