2 * Driver for the Conexant CX25821 PCIe bridge
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
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.
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
17 * GNU General Public License for more details.
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.
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>
34 #include <media/v4l2-common.h>
35 #include <media/v4l2-device.h>
36 #include <media/videobuf-dma-sg.h>
38 #include "btcx-risc.h"
39 #include "cx25821-reg.h"
40 #include "cx25821-medusa-reg.h"
41 #include "cx25821-sram.h"
42 #include "cx25821-audio.h"
44 #include <linux/version.h>
45 #include <linux/mutex.h>
47 #define CX25821_VERSION_CODE KERNEL_VERSION(0, 0, 106)
50 #define NO_SYNC_LINE (-1U)
52 #define CX25821_MAXBOARDS 2
56 #define LINE_SIZE_D1 1440
58 /* Number of decoders and encoders */
59 #define MAX_DECODERS 8
60 #define MAX_ENCODERS 2
61 #define QUAD_DECODERS 4
62 #define MAX_CAMERAS 16
64 /* Max number of inputs by card */
65 #define MAX_CX25821_INPUT 8
66 #define INPUT(nr) (&cx25821_boards[dev->board].input[nr])
67 #define RESOURCE_VIDEO0 1
68 #define RESOURCE_VIDEO1 2
69 #define RESOURCE_VIDEO2 4
70 #define RESOURCE_VIDEO3 8
71 #define RESOURCE_VIDEO4 16
72 #define RESOURCE_VIDEO5 32
73 #define RESOURCE_VIDEO6 64
74 #define RESOURCE_VIDEO7 128
75 #define RESOURCE_VIDEO8 256
76 #define RESOURCE_VIDEO9 512
77 #define RESOURCE_VIDEO10 1024
78 #define RESOURCE_VIDEO11 2048
80 #define BUFFER_TIMEOUT (HZ) /* 0.5 seconds */
82 #define UNKNOWN_BOARD 0
83 #define CX25821_BOARD 1
85 /* Currently supported by the driver */
86 #define CX25821_NORMS (\
87 V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_M_KR | \
88 V4L2_STD_PAL_BG | V4L2_STD_PAL_DK | V4L2_STD_PAL_I | \
89 V4L2_STD_PAL_M | V4L2_STD_PAL_N | V4L2_STD_PAL_H | \
92 #define CX25821_BOARD_CONEXANT_ATHENA10 1
93 #define MAX_VID_CHANNEL_NUM 12
94 #define VID_CHANNEL_NUM 8
95 #define CX25821_NR_INPUT 2
99 u32 fourcc; /* v4l2 format id */
105 struct cx25821_ctrl {
106 struct v4l2_queryctrl v;
113 struct cx25821_tvnorm {
121 struct cx25821_dev *dev;
122 enum v4l2_buf_type type;
125 enum v4l2_priority prio;
128 const struct cx25821_fmt *fmt;
129 unsigned int width, height;
131 struct videobuf_queue vidq;
135 CX25821_VMUX_COMPOSITE = 1,
140 enum cx25821_src_sel_type {
141 CX25821_SRC_SEL_EXT_656_VIDEO = 0,
142 CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
145 /* buffer for one video frame */
146 struct cx25821_buffer {
147 /* common v4l buffer stuff -- must be first */
148 struct videobuf_buffer vb;
150 /* cx25821 specific */
152 struct btcx_riscmem risc;
153 const struct cx25821_fmt *fmt;
157 struct cx25821_input {
158 enum cx25821_itype type;
160 u32 gpio0, gpio1, gpio2, gpio3;
164 CX25821_UNDEFINED = 0,
169 struct cx25821_board {
176 struct cx25821_input input[CX25821_NR_INPUT];
180 struct cx25821_dev *dev;
185 struct i2c_adapter i2c_adap;
186 struct i2c_client i2c_client;
189 /* cx25821 registers used for raw addess */
198 struct cx25821_dmaqueue {
199 struct list_head active;
200 struct list_head queued;
201 struct timer_list timeout;
202 struct btcx_riscmem stopper;
206 struct cx25821_data {
207 struct cx25821_dev *dev;
208 const struct sram_channel *channel;
211 struct cx25821_channel {
212 struct v4l2_prio_state prio;
218 struct cx25821_data timeout_data;
220 struct video_device vdev;
221 struct cx25821_dmaqueue vidq;
223 const struct sram_channel *sram_channels;
229 int use_cif_resolution;
236 struct v4l2_device v4l2_dev;
240 unsigned char pci_rev, pci_lat;
241 int pci_bus, pci_slot;
247 /* used by cx25821-alsa */
248 struct snd_card *card;
252 /* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
253 struct cx25821_i2c i2c_bus[3];
258 struct cx25821_channel channels[MAX_VID_CHANNEL_NUM];
268 unsigned short _max_num_decoders;
270 /* Analog Audio Upstream */
271 int _audio_is_running;
272 int _audiopixel_format;
273 int _is_first_audio_frame;
274 int _audiofile_status;
275 int _audio_lines_count;
276 int _audioframe_count;
277 int _audio_upstream_channel;
278 int _last_index_irq; /* The last interrupt index processed. */
280 __le32 *_risc_audio_jmp_addr;
281 __le32 *_risc_virt_start_addr;
282 __le32 *_risc_virt_addr;
283 dma_addr_t _risc_phys_addr;
284 dma_addr_t _risc_phys_start_addr;
286 unsigned int _audiorisc_size;
287 unsigned int _audiodata_buf_size;
288 __le32 *_audiodata_buf_virt_addr;
289 dma_addr_t _audiodata_buf_phys_addr;
290 char *_audiofilename;
304 int _channel_upstream_select;
305 unsigned int _risc_size;
307 __le32 *_dma_virt_start_addr;
308 __le32 *_dma_virt_addr;
309 dma_addr_t _dma_phys_addr;
310 dma_addr_t _dma_phys_start_addr;
312 unsigned int _data_buf_size;
313 __le32 *_data_buf_virt_addr;
314 dma_addr_t _data_buf_phys_addr;
320 int _pixel_format_ch2;
321 int _is_first_frame_ch2;
323 int _file_status_ch2;
324 int _lines_count_ch2;
325 int _frame_count_ch2;
326 int _channel2_upstream_select;
327 unsigned int _risc_size_ch2;
329 __le32 *_dma_virt_start_addr_ch2;
330 __le32 *_dma_virt_addr_ch2;
331 dma_addr_t _dma_phys_addr_ch2;
332 dma_addr_t _dma_phys_start_addr_ch2;
334 unsigned int _data_buf_size_ch2;
335 __le32 *_data_buf_virt_addr_ch2;
336 dma_addr_t _data_buf_phys_addr_ch2;
338 char *_defaultname_ch2;
340 u32 upstream_riscbuf_size;
341 u32 upstream_databuf_size;
342 u32 upstream_riscbuf_size_ch2;
343 u32 upstream_databuf_size_ch2;
344 u32 audio_upstream_riscbuf_size;
345 u32 audio_upstream_databuf_size;
348 int _audioframe_index;
349 struct workqueue_struct *_irq_queues;
350 struct work_struct _irq_work_entry;
351 struct workqueue_struct *_irq_queues_ch2;
352 struct work_struct _irq_work_entry_ch2;
353 struct workqueue_struct *_irq_audio_queues;
354 struct work_struct _audio_work_entry;
355 char *input_filename;
356 char *input_filename_ch2;
357 int _frame_index_ch2;
359 char *vid_stdname_ch2;
360 int pixel_format_ch2;
361 int channel_select_ch2;
363 char *input_audiofilename;
371 struct upstream_user_struct {
372 char *input_filename;
379 struct downstream_user_struct {
382 int cif_resolution_enable;
390 extern struct upstream_user_struct *up_data;
392 static inline struct cx25821_dev *get_cx25821(struct v4l2_device *v4l2_dev)
394 return container_of(v4l2_dev, struct cx25821_dev, v4l2_dev);
397 #define cx25821_call_all(dev, o, f, args...) \
398 v4l2_device_call_all(&dev->v4l2_dev, 0, o, f, ##args)
400 extern struct cx25821_board cx25821_boards[];
402 #define SRAM_CH00 0 /* Video A */
403 #define SRAM_CH01 1 /* Video B */
404 #define SRAM_CH02 2 /* Video C */
405 #define SRAM_CH03 3 /* Video D */
406 #define SRAM_CH04 4 /* Video E */
407 #define SRAM_CH05 5 /* Video F */
408 #define SRAM_CH06 6 /* Video G */
409 #define SRAM_CH07 7 /* Video H */
411 #define SRAM_CH08 8 /* Audio A */
412 #define SRAM_CH09 9 /* Video Upstream I */
413 #define SRAM_CH10 10 /* Video Upstream J */
414 #define SRAM_CH11 11 /* Audio Upstream AUD_CHANNEL_B */
416 #define VID_UPSTREAM_SRAM_CHANNEL_I SRAM_CH09
417 #define VID_UPSTREAM_SRAM_CHANNEL_J SRAM_CH10
418 #define AUDIO_UPSTREAM_SRAM_CHANNEL_B SRAM_CH11
420 struct sram_channel {
443 /* For Upstream Video */
455 extern const struct sram_channel cx25821_sram_channels[];
457 #define STATUS_SUCCESS 0
458 #define STATUS_UNSUCCESSFUL -1
460 #define cx_read(reg) readl(dev->lmmio + ((reg)>>2))
461 #define cx_write(reg, value) writel((value), dev->lmmio + ((reg)>>2))
463 #define cx_andor(reg, mask, value) \
464 writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
465 ((value) & (mask)), dev->lmmio+((reg)>>2))
467 #define cx_set(reg, bit) cx_andor((reg), (bit), (bit))
468 #define cx_clear(reg, bit) cx_andor((reg), (bit), 0)
470 #define Set_GPIO_Bit(Bit) (1 << Bit)
471 #define Clear_GPIO_Bit(Bit) (~(1 << Bit))
473 #define CX25821_ERR(fmt, args...) \
474 pr_err("(%d): " fmt, dev->board, ##args)
475 #define CX25821_WARN(fmt, args...) \
476 pr_warn("(%d): " fmt, dev->board, ##args)
477 #define CX25821_INFO(fmt, args...) \
478 pr_info("(%d): " fmt, dev->board, ##args)
480 extern int cx25821_i2c_register(struct cx25821_i2c *bus);
481 extern int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value);
482 extern int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value);
483 extern int cx25821_i2c_unregister(struct cx25821_i2c *bus);
484 extern void cx25821_gpio_init(struct cx25821_dev *dev);
485 extern void cx25821_set_gpiopin_direction(struct cx25821_dev *dev,
486 int pin_number, int pin_logic_value);
488 extern int medusa_video_init(struct cx25821_dev *dev);
489 extern int medusa_set_videostandard(struct cx25821_dev *dev);
490 extern void medusa_set_resolution(struct cx25821_dev *dev, int width,
492 extern int medusa_set_brightness(struct cx25821_dev *dev, int brightness,
494 extern int medusa_set_contrast(struct cx25821_dev *dev, int contrast,
496 extern int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder);
497 extern int medusa_set_saturation(struct cx25821_dev *dev, int saturation,
500 extern int cx25821_sram_channel_setup(struct cx25821_dev *dev,
501 const struct sram_channel *ch, unsigned int bpl,
504 extern int cx25821_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
505 struct scatterlist *sglist,
506 unsigned int top_offset,
507 unsigned int bottom_offset,
509 unsigned int padding, unsigned int lines);
510 extern int cx25821_risc_databuffer_audio(struct pci_dev *pci,
511 struct btcx_riscmem *risc,
512 struct scatterlist *sglist,
514 unsigned int lines, unsigned int lpi);
515 extern void cx25821_free_buffer(struct videobuf_queue *q,
516 struct cx25821_buffer *buf);
517 extern int cx25821_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
518 u32 reg, u32 mask, u32 value);
519 extern void cx25821_sram_channel_dump(struct cx25821_dev *dev,
520 const struct sram_channel *ch);
521 extern void cx25821_sram_channel_dump_audio(struct cx25821_dev *dev,
522 const struct sram_channel *ch);
524 extern struct cx25821_dev *cx25821_dev_get(struct pci_dev *pci);
525 extern void cx25821_print_irqbits(char *name, char *tag, char **strings,
526 int len, u32 bits, u32 mask);
527 extern void cx25821_dev_unregister(struct cx25821_dev *dev);
528 extern int cx25821_sram_channel_setup_audio(struct cx25821_dev *dev,
529 const struct sram_channel *ch,
530 unsigned int bpl, u32 risc);
532 extern int cx25821_vidupstream_init_ch1(struct cx25821_dev *dev,
533 int channel_select, int pixel_format);
534 extern int cx25821_vidupstream_init_ch2(struct cx25821_dev *dev,
535 int channel_select, int pixel_format);
536 extern int cx25821_audio_upstream_init(struct cx25821_dev *dev,
538 extern void cx25821_free_mem_upstream_ch1(struct cx25821_dev *dev);
539 extern void cx25821_free_mem_upstream_ch2(struct cx25821_dev *dev);
540 extern void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev);
541 extern void cx25821_start_upstream_video_ch1(struct cx25821_dev *dev,
542 struct upstream_user_struct
544 extern void cx25821_start_upstream_video_ch2(struct cx25821_dev *dev,
545 struct upstream_user_struct
547 extern void cx25821_start_upstream_audio(struct cx25821_dev *dev,
548 struct upstream_user_struct *up_data);
549 extern void cx25821_stop_upstream_video_ch1(struct cx25821_dev *dev);
550 extern void cx25821_stop_upstream_video_ch2(struct cx25821_dev *dev);
551 extern void cx25821_stop_upstream_audio(struct cx25821_dev *dev);
552 extern int cx25821_sram_channel_setup_upstream(struct cx25821_dev *dev,
553 const struct sram_channel *ch,
554 unsigned int bpl, u32 risc);
555 extern void cx25821_set_pixel_format(struct cx25821_dev *dev, int channel,