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/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>
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>
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"
49 #include <linux/version.h>
50 #include <linux/mutex.h>
52 #define CX25821_VERSION_CODE KERNEL_VERSION(0, 0, 106)
55 #define NO_SYNC_LINE (-1U)
57 #define CX25821_MAXBOARDS 2
61 #define LINE_SIZE_D1 1440
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
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
86 #define BUFFER_TIMEOUT (HZ) /* 0.5 seconds */
88 #define UNKNOWN_BOARD 0
89 #define CX25821_BOARD 1
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 | \
98 #define CX25821_BOARD_CONEXANT_ATHENA10 1
99 #define MAX_VID_CHANNEL_NUM 12
100 #define VID_CHANNEL_NUM 8
104 u32 fourcc
; /* v4l2 format id */
110 struct cx25821_ctrl
{
111 struct v4l2_queryctrl v
;
118 struct cx25821_tvnorm
{
126 struct cx25821_dev
*dev
;
127 enum v4l2_buf_type type
;
131 enum v4l2_priority prio
;
134 struct v4l2_window win
;
135 struct v4l2_clip
*clips
;
139 struct cx25821_fmt
*fmt
;
140 unsigned int width
, height
;
144 struct videobuf_queue vidq
;
145 struct videobuf_queue vbiq
;
147 /* H264 Encoder specifics ONLY */
148 struct videobuf_queue mpegq
;
149 atomic_t v4l_reading
;
153 CX25821_VMUX_COMPOSITE
= 1,
159 enum cx25821_src_sel_type
{
160 CX25821_SRC_SEL_EXT_656_VIDEO
= 0,
161 CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
164 /* buffer for one video frame */
165 struct cx25821_buffer
{
166 /* common v4l buffer stuff -- must be first */
167 struct videobuf_buffer vb
;
169 /* cx25821 specific */
171 struct btcx_riscmem risc
;
172 struct cx25821_fmt
*fmt
;
176 struct cx25821_input
{
177 enum cx25821_itype type
;
179 u32 gpio0
, gpio1
, gpio2
, gpio3
;
183 CX25821_UNDEFINED
= 0,
188 struct cx25821_board
{
193 unsigned int tuner_type
;
194 unsigned int radio_type
;
195 unsigned char tuner_addr
;
196 unsigned char radio_addr
;
199 struct cx25821_input input
[2];
202 struct cx25821_subid
{
209 struct cx25821_dev
*dev
;
214 struct i2c_adapter i2c_adap
;
215 struct i2c_algo_bit_data i2c_algo
;
216 struct i2c_client i2c_client
;
219 /* cx25821 registers used for raw addess */
228 struct cx25821_dmaqueue
{
229 struct list_head active
;
230 struct list_head queued
;
231 struct timer_list timeout
;
232 struct btcx_riscmem stopper
;
236 struct cx25821_data
{
237 struct cx25821_dev
*dev
;
238 struct sram_channel
*channel
;
241 struct cx25821_channel
{
242 struct v4l2_prio_state prio
;
248 struct cx25821_data timeout_data
;
250 struct video_device
*video_dev
;
251 struct cx25821_dmaqueue vidq
;
253 struct sram_channel
*sram_channels
;
259 int use_cif_resolution
;
264 struct list_head devlist
;
266 struct v4l2_device v4l2_dev
;
270 unsigned char pci_rev
, pci_lat
;
271 int pci_bus
, pci_slot
;
280 /* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
281 struct cx25821_i2c i2c_bus
[3];
286 struct cx25821_channel channels
[MAX_VID_CHANNEL_NUM
];
297 unsigned int tuner_type
;
298 unsigned char tuner_addr
;
299 unsigned int radio_type
;
300 unsigned char radio_addr
;
301 unsigned int has_radio
;
302 unsigned int videc_type
;
303 unsigned char videc_addr
;
304 unsigned short _max_num_decoders
;
306 /* Analog Audio Upstream */
307 int _audio_is_running
;
308 int _audiopixel_format
;
309 int _is_first_audio_frame
;
310 int _audiofile_status
;
311 int _audio_lines_count
;
312 int _audioframe_count
;
313 int _audio_upstream_channel
;
314 int _last_index_irq
; /* The last interrupt index processed. */
316 __le32
*_risc_audio_jmp_addr
;
317 __le32
*_risc_virt_start_addr
;
318 __le32
*_risc_virt_addr
;
319 dma_addr_t _risc_phys_addr
;
320 dma_addr_t _risc_phys_start_addr
;
322 unsigned int _audiorisc_size
;
323 unsigned int _audiodata_buf_size
;
324 __le32
*_audiodata_buf_virt_addr
;
325 dma_addr_t _audiodata_buf_phys_addr
;
326 char *_audiofilename
;
330 struct video_device
*vbi_dev
;
331 struct video_device
*radio_dev
;
332 struct video_device
*ioctl_dev
;
345 int _channel_upstream_select
;
346 unsigned int _risc_size
;
348 __le32
*_dma_virt_start_addr
;
349 __le32
*_dma_virt_addr
;
350 dma_addr_t _dma_phys_addr
;
351 dma_addr_t _dma_phys_start_addr
;
353 unsigned int _data_buf_size
;
354 __le32
*_data_buf_virt_addr
;
355 dma_addr_t _data_buf_phys_addr
;
361 int _pixel_format_ch2
;
362 int _is_first_frame_ch2
;
364 int _file_status_ch2
;
365 int _lines_count_ch2
;
366 int _frame_count_ch2
;
367 int _channel2_upstream_select
;
368 unsigned int _risc_size_ch2
;
370 __le32
*_dma_virt_start_addr_ch2
;
371 __le32
*_dma_virt_addr_ch2
;
372 dma_addr_t _dma_phys_addr_ch2
;
373 dma_addr_t _dma_phys_start_addr_ch2
;
375 unsigned int _data_buf_size_ch2
;
376 __le32
*_data_buf_virt_addr_ch2
;
377 dma_addr_t _data_buf_phys_addr_ch2
;
379 char *_defaultname_ch2
;
381 /* MPEG Encoder ONLY settings */
383 struct cx2341x_mpeg_params mpeg_params
;
384 struct video_device
*v4l_device
;
385 atomic_t v4l_reader_count
;
386 struct cx25821_tvnorm encodernorm
;
388 u32 upstream_riscbuf_size
;
389 u32 upstream_databuf_size
;
390 u32 upstream_riscbuf_size_ch2
;
391 u32 upstream_databuf_size_ch2
;
392 u32 audio_upstream_riscbuf_size
;
393 u32 audio_upstream_databuf_size
;
396 int _audioframe_index
;
397 struct workqueue_struct
*_irq_queues
;
398 struct work_struct _irq_work_entry
;
399 struct workqueue_struct
*_irq_queues_ch2
;
400 struct work_struct _irq_work_entry_ch2
;
401 struct workqueue_struct
*_irq_audio_queues
;
402 struct work_struct _audio_work_entry
;
403 char *input_filename
;
404 char *input_filename_ch2
;
405 int _frame_index_ch2
;
407 char *vid_stdname_ch2
;
408 int pixel_format_ch2
;
409 int channel_select_ch2
;
411 char *input_audiofilename
;
419 struct upstream_user_struct
{
420 char *input_filename
;
427 struct downstream_user_struct
{
430 int cif_resolution_enable
;
438 extern struct upstream_user_struct
*up_data
;
440 static inline struct cx25821_dev
*get_cx25821(struct v4l2_device
*v4l2_dev
)
442 return container_of(v4l2_dev
, struct cx25821_dev
, v4l2_dev
);
445 #define cx25821_call_all(dev, o, f, args...) \
446 v4l2_device_call_all(&dev->v4l2_dev, 0, o, f, ##args)
448 extern struct list_head cx25821_devlist
;
449 extern struct mutex cx25821_devlist_mutex
;
451 extern struct cx25821_board cx25821_boards
[];
452 extern struct cx25821_subid cx25821_subids
[];
454 #define SRAM_CH00 0 /* Video A */
455 #define SRAM_CH01 1 /* Video B */
456 #define SRAM_CH02 2 /* Video C */
457 #define SRAM_CH03 3 /* Video D */
458 #define SRAM_CH04 4 /* Video E */
459 #define SRAM_CH05 5 /* Video F */
460 #define SRAM_CH06 6 /* Video G */
461 #define SRAM_CH07 7 /* Video H */
463 #define SRAM_CH08 8 /* Audio A */
464 #define SRAM_CH09 9 /* Video Upstream I */
465 #define SRAM_CH10 10 /* Video Upstream J */
466 #define SRAM_CH11 11 /* Audio Upstream AUD_CHANNEL_B */
468 #define VID_UPSTREAM_SRAM_CHANNEL_I SRAM_CH09
469 #define VID_UPSTREAM_SRAM_CHANNEL_J SRAM_CH10
470 #define AUDIO_UPSTREAM_SRAM_CHANNEL_B SRAM_CH11
471 #define VIDEO_IOCTL_CH 11
473 struct sram_channel
{
496 /* For Upstream Video */
507 extern struct sram_channel cx25821_sram_channels
[];
509 #define STATUS_SUCCESS 0
510 #define STATUS_UNSUCCESSFUL -1
512 #define cx_read(reg) readl(dev->lmmio + ((reg)>>2))
513 #define cx_write(reg, value) writel((value), dev->lmmio + ((reg)>>2))
515 #define cx_andor(reg, mask, value) \
516 writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
517 ((value) & (mask)), dev->lmmio+((reg)>>2))
519 #define cx_set(reg, bit) cx_andor((reg), (bit), (bit))
520 #define cx_clear(reg, bit) cx_andor((reg), (bit), 0)
522 #define Set_GPIO_Bit(Bit) (1 << Bit)
523 #define Clear_GPIO_Bit(Bit) (~(1 << Bit))
525 #define CX25821_ERR(fmt, args...) \
526 pr_err("(%d): " fmt, dev->board, ##args)
527 #define CX25821_WARN(fmt, args...) \
528 pr_warn("(%d): " fmt, dev->board, ##args)
529 #define CX25821_INFO(fmt, args...) \
530 pr_info("(%d): " fmt, dev->board, ##args)
532 extern int cx25821_i2c_register(struct cx25821_i2c
*bus
);
533 extern void cx25821_card_setup(struct cx25821_dev
*dev
);
534 extern int cx25821_ir_init(struct cx25821_dev
*dev
);
535 extern int cx25821_i2c_read(struct cx25821_i2c
*bus
, u16 reg_addr
, int *value
);
536 extern int cx25821_i2c_write(struct cx25821_i2c
*bus
, u16 reg_addr
, int value
);
537 extern int cx25821_i2c_unregister(struct cx25821_i2c
*bus
);
538 extern void cx25821_gpio_init(struct cx25821_dev
*dev
);
539 extern void cx25821_set_gpiopin_direction(struct cx25821_dev
*dev
,
540 int pin_number
, int pin_logic_value
);
542 extern int medusa_video_init(struct cx25821_dev
*dev
);
543 extern int medusa_set_videostandard(struct cx25821_dev
*dev
);
544 extern void medusa_set_resolution(struct cx25821_dev
*dev
, int width
,
546 extern int medusa_set_brightness(struct cx25821_dev
*dev
, int brightness
,
548 extern int medusa_set_contrast(struct cx25821_dev
*dev
, int contrast
,
550 extern int medusa_set_hue(struct cx25821_dev
*dev
, int hue
, int decoder
);
551 extern int medusa_set_saturation(struct cx25821_dev
*dev
, int saturation
,
554 extern int cx25821_sram_channel_setup(struct cx25821_dev
*dev
,
555 struct sram_channel
*ch
, unsigned int bpl
,
558 extern int cx25821_risc_buffer(struct pci_dev
*pci
, struct btcx_riscmem
*risc
,
559 struct scatterlist
*sglist
,
560 unsigned int top_offset
,
561 unsigned int bottom_offset
,
563 unsigned int padding
, unsigned int lines
);
564 extern int cx25821_risc_databuffer_audio(struct pci_dev
*pci
,
565 struct btcx_riscmem
*risc
,
566 struct scatterlist
*sglist
,
568 unsigned int lines
, unsigned int lpi
);
569 extern void cx25821_free_buffer(struct videobuf_queue
*q
,
570 struct cx25821_buffer
*buf
);
571 extern int cx25821_risc_stopper(struct pci_dev
*pci
, struct btcx_riscmem
*risc
,
572 u32 reg
, u32 mask
, u32 value
);
573 extern void cx25821_sram_channel_dump(struct cx25821_dev
*dev
,
574 struct sram_channel
*ch
);
575 extern void cx25821_sram_channel_dump_audio(struct cx25821_dev
*dev
,
576 struct sram_channel
*ch
);
578 extern struct cx25821_dev
*cx25821_dev_get(struct pci_dev
*pci
);
579 extern void cx25821_print_irqbits(char *name
, char *tag
, char **strings
,
580 int len
, u32 bits
, u32 mask
);
581 extern void cx25821_dev_unregister(struct cx25821_dev
*dev
);
582 extern int cx25821_sram_channel_setup_audio(struct cx25821_dev
*dev
,
583 struct sram_channel
*ch
,
584 unsigned int bpl
, u32 risc
);
586 extern int cx25821_vidupstream_init_ch1(struct cx25821_dev
*dev
,
587 int channel_select
, int pixel_format
);
588 extern int cx25821_vidupstream_init_ch2(struct cx25821_dev
*dev
,
589 int channel_select
, int pixel_format
);
590 extern int cx25821_audio_upstream_init(struct cx25821_dev
*dev
,
592 extern void cx25821_free_mem_upstream_ch1(struct cx25821_dev
*dev
);
593 extern void cx25821_free_mem_upstream_ch2(struct cx25821_dev
*dev
);
594 extern void cx25821_free_mem_upstream_audio(struct cx25821_dev
*dev
);
595 extern void cx25821_start_upstream_video_ch1(struct cx25821_dev
*dev
,
596 struct upstream_user_struct
598 extern void cx25821_start_upstream_video_ch2(struct cx25821_dev
*dev
,
599 struct upstream_user_struct
601 extern void cx25821_start_upstream_audio(struct cx25821_dev
*dev
,
602 struct upstream_user_struct
*up_data
);
603 extern void cx25821_stop_upstream_video_ch1(struct cx25821_dev
*dev
);
604 extern void cx25821_stop_upstream_video_ch2(struct cx25821_dev
*dev
);
605 extern void cx25821_stop_upstream_audio(struct cx25821_dev
*dev
);
606 extern int cx25821_sram_channel_setup_upstream(struct cx25821_dev
*dev
,
607 struct sram_channel
*ch
,
608 unsigned int bpl
, u32 risc
);
609 extern void cx25821_set_pixel_format(struct cx25821_dev
*dev
, int channel
,
611 extern void cx25821_videoioctl_unregister(struct cx25821_dev
*dev
);
612 extern struct video_device
*cx25821_vdev_init(struct cx25821_dev
*dev
,
614 struct video_device
*template,