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/i2c-algo-bit.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include <linux/sched.h>
33 #include <linux/kdev_t.h>
34 
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-device.h>
37 #include <media/tuner.h>
38 #include <media/tveeprom.h>
39 #include <media/videobuf-dma-sg.h>
40 #include <media/videobuf-dvb.h>
41 
42 #include "btcx-risc.h"
43 #include "cx25821-reg.h"
44 #include "cx25821-medusa-reg.h"
45 #include "cx25821-sram.h"
46 #include "cx25821-audio.h"
47 #include "media/cx2341x.h"
48 
49 #include <linux/version.h>
50 #include <linux/mutex.h>
51 
52 #define CX25821_VERSION_CODE KERNEL_VERSION(0, 0, 106)
53 
54 #define UNSET (-1U)
55 #define NO_SYNC_LINE (-1U)
56 
57 #define CX25821_MAXBOARDS 2
58 
59 #define TRUE    1
60 #define FALSE   0
61 #define LINE_SIZE_D1    1440
62 
63 /* Number of decoders and encoders */
64 #define MAX_DECODERS            8
65 #define MAX_ENCODERS            2
66 #define QUAD_DECODERS           4
67 #define MAX_CAMERAS             16
68 
69 /* Max number of inputs by card */
70 #define MAX_CX25821_INPUT 8
71 #define INPUT(nr) (&cx25821_boards[dev->board].input[nr])
72 #define RESOURCE_VIDEO0       1
73 #define RESOURCE_VIDEO1       2
74 #define RESOURCE_VIDEO2       4
75 #define RESOURCE_VIDEO3       8
76 #define RESOURCE_VIDEO4       16
77 #define RESOURCE_VIDEO5       32
78 #define RESOURCE_VIDEO6       64
79 #define RESOURCE_VIDEO7       128
80 #define RESOURCE_VIDEO8       256
81 #define RESOURCE_VIDEO9       512
82 #define RESOURCE_VIDEO10      1024
83 #define RESOURCE_VIDEO11      2048
84 #define RESOURCE_VIDEO_IOCTL  4096
85 
86 #define BUFFER_TIMEOUT     (HZ)	/* 0.5 seconds */
87 
88 #define UNKNOWN_BOARD       0
89 #define CX25821_BOARD        1
90 
91 /* Currently supported by the driver */
92 #define CX25821_NORMS (\
93 	V4L2_STD_NTSC_M |  V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_M_KR | \
94 	V4L2_STD_PAL_BG |  V4L2_STD_PAL_DK    |  V4L2_STD_PAL_I    | \
95 	V4L2_STD_PAL_M  |  V4L2_STD_PAL_N     |  V4L2_STD_PAL_H    | \
96 	V4L2_STD_PAL_Nc)
97 
98 #define CX25821_BOARD_CONEXANT_ATHENA10 1
99 #define MAX_VID_CHANNEL_NUM     12
100 #define VID_CHANNEL_NUM 8
101 
102 struct cx25821_fmt {
103 	char *name;
104 	u32 fourcc;		/* v4l2 format id */
105 	int depth;
106 	int flags;
107 	u32 cxformat;
108 };
109 
110 struct cx25821_ctrl {
111 	struct v4l2_queryctrl v;
112 	u32 off;
113 	u32 reg;
114 	u32 mask;
115 	u32 shift;
116 };
117 
118 struct cx25821_tvnorm {
119 	char *name;
120 	v4l2_std_id id;
121 	u32 cxiformat;
122 	u32 cxoformat;
123 };
124 
125 struct cx25821_fh {
126 	struct cx25821_dev *dev;
127 	enum v4l2_buf_type type;
128 	int radio;
129 	u32 resources;
130 
131 	enum v4l2_priority prio;
132 
133 	/* video overlay */
134 	struct v4l2_window win;
135 	struct v4l2_clip *clips;
136 	unsigned int nclips;
137 
138 	/* video capture */
139 	struct cx25821_fmt *fmt;
140 	unsigned int width, height;
141 	int channel_id;
142 
143 	/* vbi capture */
144 	struct videobuf_queue vidq;
145 	struct videobuf_queue vbiq;
146 
147 	/* H264 Encoder specifics ONLY */
148 	struct videobuf_queue mpegq;
149 	atomic_t v4l_reading;
150 };
151 
152 enum cx25821_itype {
153 	CX25821_VMUX_COMPOSITE = 1,
154 	CX25821_VMUX_SVIDEO,
155 	CX25821_VMUX_DEBUG,
156 	CX25821_RADIO,
157 };
158 
159 enum cx25821_src_sel_type {
160 	CX25821_SRC_SEL_EXT_656_VIDEO = 0,
161 	CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
162 };
163 
164 /* buffer for one video frame */
165 struct cx25821_buffer {
166 	/* common v4l buffer stuff -- must be first */
167 	struct videobuf_buffer vb;
168 
169 	/* cx25821 specific */
170 	unsigned int bpl;
171 	struct btcx_riscmem risc;
172 	struct cx25821_fmt *fmt;
173 	u32 count;
174 };
175 
176 struct cx25821_input {
177 	enum cx25821_itype type;
178 	unsigned int vmux;
179 	u32 gpio0, gpio1, gpio2, gpio3;
180 };
181 
182 typedef enum {
183 	CX25821_UNDEFINED = 0,
184 	CX25821_RAW,
185 	CX25821_264
186 } port_t;
187 
188 struct cx25821_board {
189 	char *name;
190 	port_t porta, portb, portc;
191 	unsigned int tuner_type;
192 	unsigned int radio_type;
193 	unsigned char tuner_addr;
194 	unsigned char radio_addr;
195 
196 	u32 clk_freq;
197 	struct cx25821_input input[2];
198 };
199 
200 struct cx25821_subid {
201 	u16 subvendor;
202 	u16 subdevice;
203 	u32 card;
204 };
205 
206 struct cx25821_i2c {
207 	struct cx25821_dev *dev;
208 
209 	int nr;
210 
211 	/* i2c i/o */
212 	struct i2c_adapter i2c_adap;
213 	struct i2c_algo_bit_data i2c_algo;
214 	struct i2c_client i2c_client;
215 	u32 i2c_rc;
216 
217 	/* cx25821 registers used for raw addess */
218 	u32 i2c_period;
219 	u32 reg_ctrl;
220 	u32 reg_stat;
221 	u32 reg_addr;
222 	u32 reg_rdata;
223 	u32 reg_wdata;
224 };
225 
226 struct cx25821_dmaqueue {
227 	struct list_head active;
228 	struct list_head queued;
229 	struct timer_list timeout;
230 	struct btcx_riscmem stopper;
231 	u32 count;
232 };
233 
234 struct cx25821_data {
235 	struct cx25821_dev *dev;
236 	struct sram_channel *channel;
237 };
238 
239 struct cx25821_channel {
240 	struct v4l2_prio_state prio;
241 
242 	int ctl_bright;
243 	int ctl_contrast;
244 	int ctl_hue;
245 	int ctl_saturation;
246 	struct cx25821_data timeout_data;
247 
248 	struct video_device *video_dev;
249 	struct cx25821_dmaqueue vidq;
250 
251 	struct sram_channel *sram_channels;
252 
253 	struct mutex lock;
254 	int resources;
255 
256 	int pixel_formats;
257 	int use_cif_resolution;
258 	int cif_width;
259 };
260 
261 struct cx25821_dev {
262 	struct list_head devlist;
263 	atomic_t refcount;
264 	struct v4l2_device v4l2_dev;
265 
266 	/* pci stuff */
267 	struct pci_dev *pci;
268 	unsigned char pci_rev, pci_lat;
269 	int pci_bus, pci_slot;
270 	u32 base_io_addr;
271 	u32 __iomem *lmmio;
272 	u8 __iomem *bmmio;
273 	int pci_irqmask;
274 	int hwrevision;
275 
276 	u32 clk_freq;
277 
278 	/* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
279 	struct cx25821_i2c i2c_bus[3];
280 
281 	int nr;
282 	struct mutex lock;
283 
284 	struct cx25821_channel channels[MAX_VID_CHANNEL_NUM];
285 
286 	/* board details */
287 	unsigned int board;
288 	char name[32];
289 
290 	/* Analog video */
291 	u32 resources;
292 	unsigned int input;
293 	u32 tvaudio;
294 	v4l2_std_id tvnorm;
295 	unsigned int tuner_type;
296 	unsigned char tuner_addr;
297 	unsigned int radio_type;
298 	unsigned char radio_addr;
299 	unsigned int has_radio;
300 	unsigned int videc_type;
301 	unsigned char videc_addr;
302 	unsigned short _max_num_decoders;
303 
304 	/* Analog Audio Upstream */
305 	int _audio_is_running;
306 	int _audiopixel_format;
307 	int _is_first_audio_frame;
308 	int _audiofile_status;
309 	int _audio_lines_count;
310 	int _audioframe_count;
311 	int _audio_upstream_channel_select;
312 	int _last_index_irq;    /* The last interrupt index processed. */
313 
314 	__le32 *_risc_audio_jmp_addr;
315 	__le32 *_risc_virt_start_addr;
316 	__le32 *_risc_virt_addr;
317 	dma_addr_t _risc_phys_addr;
318 	dma_addr_t _risc_phys_start_addr;
319 
320 	unsigned int _audiorisc_size;
321 	unsigned int _audiodata_buf_size;
322 	__le32 *_audiodata_buf_virt_addr;
323 	dma_addr_t _audiodata_buf_phys_addr;
324 	char *_audiofilename;
325 
326 	/* V4l */
327 	u32 freq;
328 	struct video_device *vbi_dev;
329 	struct video_device *radio_dev;
330 	struct video_device *ioctl_dev;
331 
332 	spinlock_t slock;
333 
334 	/* Video Upstream */
335 	int _line_size;
336 	int _prog_cnt;
337 	int _pixel_format;
338 	int _is_first_frame;
339 	int _is_running;
340 	int _file_status;
341 	int _lines_count;
342 	int _frame_count;
343 	int _channel_upstream_select;
344 	unsigned int _risc_size;
345 
346 	__le32 *_dma_virt_start_addr;
347 	__le32 *_dma_virt_addr;
348 	dma_addr_t _dma_phys_addr;
349 	dma_addr_t _dma_phys_start_addr;
350 
351 	unsigned int _data_buf_size;
352 	__le32 *_data_buf_virt_addr;
353 	dma_addr_t _data_buf_phys_addr;
354 	char *_filename;
355 	char *_defaultname;
356 
357 	int _line_size_ch2;
358 	int _prog_cnt_ch2;
359 	int _pixel_format_ch2;
360 	int _is_first_frame_ch2;
361 	int _is_running_ch2;
362 	int _file_status_ch2;
363 	int _lines_count_ch2;
364 	int _frame_count_ch2;
365 	int _channel2_upstream_select;
366 	unsigned int _risc_size_ch2;
367 
368 	__le32 *_dma_virt_start_addr_ch2;
369 	__le32 *_dma_virt_addr_ch2;
370 	dma_addr_t _dma_phys_addr_ch2;
371 	dma_addr_t _dma_phys_start_addr_ch2;
372 
373 	unsigned int _data_buf_size_ch2;
374 	__le32 *_data_buf_virt_addr_ch2;
375 	dma_addr_t _data_buf_phys_addr_ch2;
376 	char *_filename_ch2;
377 	char *_defaultname_ch2;
378 
379 	/* MPEG Encoder ONLY settings */
380 	u32 cx23417_mailbox;
381 	struct cx2341x_mpeg_params mpeg_params;
382 	struct video_device *v4l_device;
383 	atomic_t v4l_reader_count;
384 	struct cx25821_tvnorm encodernorm;
385 
386 	u32 upstream_riscbuf_size;
387 	u32 upstream_databuf_size;
388 	u32 upstream_riscbuf_size_ch2;
389 	u32 upstream_databuf_size_ch2;
390 	u32 audio_upstream_riscbuf_size;
391 	u32 audio_upstream_databuf_size;
392 	int _isNTSC;
393 	int _frame_index;
394 	int _audioframe_index;
395 	struct workqueue_struct *_irq_queues;
396 	struct work_struct _irq_work_entry;
397 	struct workqueue_struct *_irq_queues_ch2;
398 	struct work_struct _irq_work_entry_ch2;
399 	struct workqueue_struct *_irq_audio_queues;
400 	struct work_struct _audio_work_entry;
401 	char *input_filename;
402 	char *input_filename_ch2;
403 	int _frame_index_ch2;
404 	int _isNTSC_ch2;
405 	char *vid_stdname_ch2;
406 	int pixel_format_ch2;
407 	int channel_select_ch2;
408 	int command_ch2;
409 	char *input_audiofilename;
410 	char *vid_stdname;
411 	int pixel_format;
412 	int channel_select;
413 	int command;
414 	int channel_opened;
415 };
416 
417 struct upstream_user_struct {
418 	char *input_filename;
419 	char *vid_stdname;
420 	int pixel_format;
421 	int channel_select;
422 	int command;
423 };
424 
425 struct downstream_user_struct {
426 	char *vid_stdname;
427 	int pixel_format;
428 	int cif_resolution_enable;
429 	int cif_width;
430 	int decoder_select;
431 	int command;
432 	int reg_address;
433 	int reg_data;
434 };
435 
436 extern struct upstream_user_struct *up_data;
437 
get_cx25821(struct v4l2_device * v4l2_dev)438 static inline struct cx25821_dev *get_cx25821(struct v4l2_device *v4l2_dev)
439 {
440 	return container_of(v4l2_dev, struct cx25821_dev, v4l2_dev);
441 }
442 
443 #define cx25821_call_all(dev, o, f, args...) \
444 	v4l2_device_call_all(&dev->v4l2_dev, 0, o, f, ##args)
445 
446 extern struct list_head cx25821_devlist;
447 extern struct mutex cx25821_devlist_mutex;
448 
449 extern struct cx25821_board cx25821_boards[];
450 extern struct cx25821_subid cx25821_subids[];
451 
452 #define SRAM_CH00  0		/* Video A */
453 #define SRAM_CH01  1		/* Video B */
454 #define SRAM_CH02  2		/* Video C */
455 #define SRAM_CH03  3		/* Video D */
456 #define SRAM_CH04  4		/* Video E */
457 #define SRAM_CH05  5		/* Video F */
458 #define SRAM_CH06  6		/* Video G */
459 #define SRAM_CH07  7		/* Video H */
460 
461 #define SRAM_CH08  8		/* Audio A */
462 #define SRAM_CH09  9		/* Video Upstream I */
463 #define SRAM_CH10  10		/* Video Upstream J */
464 #define SRAM_CH11  11		/* Audio Upstream AUD_CHANNEL_B */
465 
466 #define VID_UPSTREAM_SRAM_CHANNEL_I     SRAM_CH09
467 #define VID_UPSTREAM_SRAM_CHANNEL_J     SRAM_CH10
468 #define AUDIO_UPSTREAM_SRAM_CHANNEL_B   SRAM_CH11
469 #define VIDEO_IOCTL_CH  11
470 
471 struct sram_channel {
472 	char *name;
473 	u32 i;
474 	u32 cmds_start;
475 	u32 ctrl_start;
476 	u32 cdt;
477 	u32 fifo_start;
478 	u32 fifo_size;
479 	u32 ptr1_reg;
480 	u32 ptr2_reg;
481 	u32 cnt1_reg;
482 	u32 cnt2_reg;
483 	u32 int_msk;
484 	u32 int_stat;
485 	u32 int_mstat;
486 	u32 dma_ctl;
487 	u32 gpcnt_ctl;
488 	u32 gpcnt;
489 	u32 aud_length;
490 	u32 aud_cfg;
491 	u32 fld_aud_fifo_en;
492 	u32 fld_aud_risc_en;
493 
494 	/* For Upstream Video */
495 	u32 vid_fmt_ctl;
496 	u32 vid_active_ctl1;
497 	u32 vid_active_ctl2;
498 	u32 vid_cdt_size;
499 
500 	u32 vip_ctl;
501 	u32 pix_frmt;
502 	u32 jumponly;
503 	u32 irq_bit;
504 };
505 extern struct sram_channel cx25821_sram_channels[];
506 
507 #define STATUS_SUCCESS         0
508 #define STATUS_UNSUCCESSFUL    -1
509 
510 #define cx_read(reg)             readl(dev->lmmio + ((reg)>>2))
511 #define cx_write(reg, value)     writel((value), dev->lmmio + ((reg)>>2))
512 
513 #define cx_andor(reg, mask, value) \
514 	writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
515 	((value) & (mask)), dev->lmmio+((reg)>>2))
516 
517 #define cx_set(reg, bit)          cx_andor((reg), (bit), (bit))
518 #define cx_clear(reg, bit)        cx_andor((reg), (bit), 0)
519 
520 #define Set_GPIO_Bit(Bit)                       (1 << Bit)
521 #define Clear_GPIO_Bit(Bit)                     (~(1 << Bit))
522 
523 #define CX25821_ERR(fmt, args...)			\
524 	pr_err("(%d): " fmt, dev->board, ##args)
525 #define CX25821_WARN(fmt, args...)			\
526 	pr_warn("(%d): " fmt, dev->board, ##args)
527 #define CX25821_INFO(fmt, args...)			\
528 	pr_info("(%d): " fmt, dev->board, ##args)
529 
530 extern int cx25821_i2c_register(struct cx25821_i2c *bus);
531 extern void cx25821_card_setup(struct cx25821_dev *dev);
532 extern int cx25821_ir_init(struct cx25821_dev *dev);
533 extern int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value);
534 extern int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value);
535 extern int cx25821_i2c_unregister(struct cx25821_i2c *bus);
536 extern void cx25821_gpio_init(struct cx25821_dev *dev);
537 extern void cx25821_set_gpiopin_direction(struct cx25821_dev *dev,
538 					  int pin_number, int pin_logic_value);
539 
540 extern int medusa_video_init(struct cx25821_dev *dev);
541 extern int medusa_set_videostandard(struct cx25821_dev *dev);
542 extern void medusa_set_resolution(struct cx25821_dev *dev, int width,
543 				  int decoder_select);
544 extern int medusa_set_brightness(struct cx25821_dev *dev, int brightness,
545 				 int decoder);
546 extern int medusa_set_contrast(struct cx25821_dev *dev, int contrast,
547 			       int decoder);
548 extern int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder);
549 extern int medusa_set_saturation(struct cx25821_dev *dev, int saturation,
550 				 int decoder);
551 
552 extern int cx25821_sram_channel_setup(struct cx25821_dev *dev,
553 				      struct sram_channel *ch, unsigned int bpl,
554 				      u32 risc);
555 
556 extern int cx25821_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
557 			       struct scatterlist *sglist,
558 			       unsigned int top_offset,
559 			       unsigned int bottom_offset,
560 			       unsigned int bpl,
561 			       unsigned int padding, unsigned int lines);
562 extern int cx25821_risc_databuffer_audio(struct pci_dev *pci,
563 					 struct btcx_riscmem *risc,
564 					 struct scatterlist *sglist,
565 					 unsigned int bpl,
566 					 unsigned int lines, unsigned int lpi);
567 extern void cx25821_free_buffer(struct videobuf_queue *q,
568 				struct cx25821_buffer *buf);
569 extern int cx25821_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
570 				u32 reg, u32 mask, u32 value);
571 extern void cx25821_sram_channel_dump(struct cx25821_dev *dev,
572 				      struct sram_channel *ch);
573 extern void cx25821_sram_channel_dump_audio(struct cx25821_dev *dev,
574 					    struct sram_channel *ch);
575 
576 extern struct cx25821_dev *cx25821_dev_get(struct pci_dev *pci);
577 extern void cx25821_print_irqbits(char *name, char *tag, char **strings,
578 				  int len, u32 bits, u32 mask);
579 extern void cx25821_dev_unregister(struct cx25821_dev *dev);
580 extern int cx25821_sram_channel_setup_audio(struct cx25821_dev *dev,
581 					    struct sram_channel *ch,
582 					    unsigned int bpl, u32 risc);
583 
584 extern int cx25821_vidupstream_init_ch1(struct cx25821_dev *dev,
585 					int channel_select, int pixel_format);
586 extern int cx25821_vidupstream_init_ch2(struct cx25821_dev *dev,
587 					int channel_select, int pixel_format);
588 extern int cx25821_audio_upstream_init(struct cx25821_dev *dev,
589 				       int channel_select);
590 extern void cx25821_free_mem_upstream_ch1(struct cx25821_dev *dev);
591 extern void cx25821_free_mem_upstream_ch2(struct cx25821_dev *dev);
592 extern void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev);
593 extern void cx25821_start_upstream_video_ch1(struct cx25821_dev *dev,
594 					     struct upstream_user_struct
595 					     *up_data);
596 extern void cx25821_start_upstream_video_ch2(struct cx25821_dev *dev,
597 					     struct upstream_user_struct
598 					     *up_data);
599 extern void cx25821_start_upstream_audio(struct cx25821_dev *dev,
600 					 struct upstream_user_struct *up_data);
601 extern void cx25821_stop_upstream_video_ch1(struct cx25821_dev *dev);
602 extern void cx25821_stop_upstream_video_ch2(struct cx25821_dev *dev);
603 extern void cx25821_stop_upstream_audio(struct cx25821_dev *dev);
604 extern int cx25821_sram_channel_setup_upstream(struct cx25821_dev *dev,
605 					       struct sram_channel *ch,
606 					       unsigned int bpl, u32 risc);
607 extern void cx25821_set_pixel_format(struct cx25821_dev *dev, int channel,
608 				     u32 format);
609 extern void cx25821_videoioctl_unregister(struct cx25821_dev *dev);
610 extern struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
611 					      struct pci_dev *pci,
612 					      struct video_device *template,
613 					      char *type);
614 #endif
615