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/v4l2-ctrls.h>
37 #include <media/videobuf-dma-sg.h>
39 #include "btcx-risc.h"
40 #include "cx25821-reg.h"
41 #include "cx25821-medusa-reg.h"
42 #include "cx25821-sram.h"
43 #include "cx25821-audio.h"
45 #include <linux/version.h>
46 #include <linux/mutex.h>
48 #define CX25821_VERSION_CODE KERNEL_VERSION(0, 0, 106)
51 #define NO_SYNC_LINE (-1U)
53 #define CX25821_MAXBOARDS 2
55 #define LINE_SIZE_D1 1440
57 /* Number of decoders and encoders */
58 #define MAX_DECODERS 8
59 #define MAX_ENCODERS 2
60 #define QUAD_DECODERS 4
61 #define MAX_CAMERAS 16
63 /* Max number of inputs by card */
64 #define MAX_CX25821_INPUT 8
65 #define RESOURCE_VIDEO0 1
66 #define RESOURCE_VIDEO1 2
67 #define RESOURCE_VIDEO2 4
68 #define RESOURCE_VIDEO3 8
69 #define RESOURCE_VIDEO4 16
70 #define RESOURCE_VIDEO5 32
71 #define RESOURCE_VIDEO6 64
72 #define RESOURCE_VIDEO7 128
73 #define RESOURCE_VIDEO8 256
74 #define RESOURCE_VIDEO9 512
75 #define RESOURCE_VIDEO10 1024
76 #define RESOURCE_VIDEO11 2048
78 #define BUFFER_TIMEOUT (HZ) /* 0.5 seconds */
80 #define UNKNOWN_BOARD 0
81 #define CX25821_BOARD 1
83 /* Currently supported by the driver */
84 #define CX25821_NORMS (\
85 V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_M_KR | \
86 V4L2_STD_PAL_BG | V4L2_STD_PAL_DK | V4L2_STD_PAL_I | \
87 V4L2_STD_PAL_M | V4L2_STD_PAL_N | V4L2_STD_PAL_H | \
90 #define CX25821_BOARD_CONEXANT_ATHENA10 1
91 #define MAX_VID_CHANNEL_NUM 12
92 #define VID_CHANNEL_NUM 8
96 u32 fourcc
; /* v4l2 format id */
102 struct cx25821_tvnorm
{
109 enum cx25821_src_sel_type
{
110 CX25821_SRC_SEL_EXT_656_VIDEO
= 0,
111 CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
114 /* buffer for one video frame */
115 struct cx25821_buffer
{
116 /* common v4l buffer stuff -- must be first */
117 struct videobuf_buffer vb
;
119 /* cx25821 specific */
121 struct btcx_riscmem risc
;
122 const struct cx25821_fmt
*fmt
;
127 CX25821_UNDEFINED
= 0,
132 struct cx25821_board
{
142 struct cx25821_dev
*dev
;
147 struct i2c_adapter i2c_adap
;
148 struct i2c_client i2c_client
;
151 /* cx25821 registers used for raw addess */
160 struct cx25821_dmaqueue
{
161 struct list_head active
;
162 struct list_head queued
;
163 struct timer_list timeout
;
164 struct btcx_riscmem stopper
;
168 struct cx25821_data
{
169 struct cx25821_dev
*dev
;
170 const struct sram_channel
*channel
;
175 struct cx25821_channel
;
177 struct cx25821_video_out_data
{
178 struct cx25821_channel
*chan
;
187 unsigned int _risc_size
;
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
;
194 unsigned int _data_buf_size
;
195 __le32
*_data_buf_virt_addr
;
196 dma_addr_t _data_buf_phys_addr
;
198 u32 upstream_riscbuf_size
;
199 u32 upstream_databuf_size
;
204 wait_queue_head_t waitq
;
207 struct cx25821_channel
{
209 struct cx25821_dev
*dev
;
210 struct v4l2_fh
*streaming_fh
;
212 struct v4l2_ctrl_handler hdl
;
213 struct cx25821_data timeout_data
;
215 struct video_device vdev
;
216 struct cx25821_dmaqueue dma_vidq
;
217 struct videobuf_queue vidq
;
219 const struct sram_channel
*sram_channels
;
221 const struct cx25821_fmt
*fmt
;
222 unsigned int width
, height
;
224 int use_cif_resolution
;
227 /* video output data for the video output channel */
228 struct cx25821_video_out_data
*out
;
234 struct v4l2_device v4l2_dev
;
238 unsigned char pci_rev
, pci_lat
;
239 int pci_bus
, pci_slot
;
245 /* used by cx25821-alsa */
246 struct snd_card
*card
;
250 /* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
251 struct cx25821_i2c i2c_bus
[3];
256 struct cx25821_channel channels
[MAX_VID_CHANNEL_NUM
];
265 unsigned short _max_num_decoders
;
267 /* Analog Audio Upstream */
268 int _audio_is_running
;
269 int _audiopixel_format
;
270 int _is_first_audio_frame
;
271 int _audiofile_status
;
272 int _audio_lines_count
;
273 int _audioframe_count
;
274 int _audio_upstream_channel
;
275 int _last_index_irq
; /* The last interrupt index processed. */
277 __le32
*_risc_audio_jmp_addr
;
278 __le32
*_risc_virt_start_addr
;
279 __le32
*_risc_virt_addr
;
280 dma_addr_t _risc_phys_addr
;
281 dma_addr_t _risc_phys_start_addr
;
283 unsigned int _audiorisc_size
;
284 unsigned int _audiodata_buf_size
;
285 __le32
*_audiodata_buf_virt_addr
;
286 dma_addr_t _audiodata_buf_phys_addr
;
287 char *_audiofilename
;
288 u32 audio_upstream_riscbuf_size
;
289 u32 audio_upstream_databuf_size
;
290 int _audioframe_index
;
291 struct workqueue_struct
*_irq_audio_queues
;
292 struct work_struct _audio_work_entry
;
293 char *input_audiofilename
;
299 struct cx25821_video_out_data vid_out_data
[2];
302 static inline struct cx25821_dev
*get_cx25821(struct v4l2_device
*v4l2_dev
)
304 return container_of(v4l2_dev
, struct cx25821_dev
, v4l2_dev
);
307 extern struct cx25821_board cx25821_boards
[];
309 #define SRAM_CH00 0 /* Video A */
310 #define SRAM_CH01 1 /* Video B */
311 #define SRAM_CH02 2 /* Video C */
312 #define SRAM_CH03 3 /* Video D */
313 #define SRAM_CH04 4 /* Video E */
314 #define SRAM_CH05 5 /* Video F */
315 #define SRAM_CH06 6 /* Video G */
316 #define SRAM_CH07 7 /* Video H */
318 #define SRAM_CH08 8 /* Audio A */
319 #define SRAM_CH09 9 /* Video Upstream I */
320 #define SRAM_CH10 10 /* Video Upstream J */
321 #define SRAM_CH11 11 /* Audio Upstream AUD_CHANNEL_B */
323 #define VID_UPSTREAM_SRAM_CHANNEL_I SRAM_CH09
324 #define VID_UPSTREAM_SRAM_CHANNEL_J SRAM_CH10
325 #define AUDIO_UPSTREAM_SRAM_CHANNEL_B SRAM_CH11
327 struct sram_channel
{
350 /* For Upstream Video */
362 extern const struct sram_channel cx25821_sram_channels
[];
364 #define cx_read(reg) readl(dev->lmmio + ((reg)>>2))
365 #define cx_write(reg, value) writel((value), dev->lmmio + ((reg)>>2))
367 #define cx_andor(reg, mask, value) \
368 writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
369 ((value) & (mask)), dev->lmmio+((reg)>>2))
371 #define cx_set(reg, bit) cx_andor((reg), (bit), (bit))
372 #define cx_clear(reg, bit) cx_andor((reg), (bit), 0)
374 #define Set_GPIO_Bit(Bit) (1 << Bit)
375 #define Clear_GPIO_Bit(Bit) (~(1 << Bit))
377 #define CX25821_ERR(fmt, args...) \
378 pr_err("(%d): " fmt, dev->board, ##args)
379 #define CX25821_WARN(fmt, args...) \
380 pr_warn("(%d): " fmt, dev->board, ##args)
381 #define CX25821_INFO(fmt, args...) \
382 pr_info("(%d): " fmt, dev->board, ##args)
384 extern int cx25821_i2c_register(struct cx25821_i2c
*bus
);
385 extern int cx25821_i2c_read(struct cx25821_i2c
*bus
, u16 reg_addr
, int *value
);
386 extern int cx25821_i2c_write(struct cx25821_i2c
*bus
, u16 reg_addr
, int value
);
387 extern int cx25821_i2c_unregister(struct cx25821_i2c
*bus
);
388 extern void cx25821_gpio_init(struct cx25821_dev
*dev
);
389 extern void cx25821_set_gpiopin_direction(struct cx25821_dev
*dev
,
390 int pin_number
, int pin_logic_value
);
392 extern int medusa_video_init(struct cx25821_dev
*dev
);
393 extern int medusa_set_videostandard(struct cx25821_dev
*dev
);
394 extern void medusa_set_resolution(struct cx25821_dev
*dev
, int width
,
396 extern int medusa_set_brightness(struct cx25821_dev
*dev
, int brightness
,
398 extern int medusa_set_contrast(struct cx25821_dev
*dev
, int contrast
,
400 extern int medusa_set_hue(struct cx25821_dev
*dev
, int hue
, int decoder
);
401 extern int medusa_set_saturation(struct cx25821_dev
*dev
, int saturation
,
404 extern int cx25821_sram_channel_setup(struct cx25821_dev
*dev
,
405 const struct sram_channel
*ch
, unsigned int bpl
,
408 extern int cx25821_risc_buffer(struct pci_dev
*pci
, struct btcx_riscmem
*risc
,
409 struct scatterlist
*sglist
,
410 unsigned int top_offset
,
411 unsigned int bottom_offset
,
413 unsigned int padding
, unsigned int lines
);
414 extern int cx25821_risc_databuffer_audio(struct pci_dev
*pci
,
415 struct btcx_riscmem
*risc
,
416 struct scatterlist
*sglist
,
418 unsigned int lines
, unsigned int lpi
);
419 extern void cx25821_free_buffer(struct videobuf_queue
*q
,
420 struct cx25821_buffer
*buf
);
421 extern int cx25821_risc_stopper(struct pci_dev
*pci
, struct btcx_riscmem
*risc
,
422 u32 reg
, u32 mask
, u32 value
);
423 extern void cx25821_sram_channel_dump(struct cx25821_dev
*dev
,
424 const struct sram_channel
*ch
);
425 extern void cx25821_sram_channel_dump_audio(struct cx25821_dev
*dev
,
426 const struct sram_channel
*ch
);
428 extern struct cx25821_dev
*cx25821_dev_get(struct pci_dev
*pci
);
429 extern void cx25821_print_irqbits(char *name
, char *tag
, char **strings
,
430 int len
, u32 bits
, u32 mask
);
431 extern void cx25821_dev_unregister(struct cx25821_dev
*dev
);
432 extern int cx25821_sram_channel_setup_audio(struct cx25821_dev
*dev
,
433 const struct sram_channel
*ch
,
434 unsigned int bpl
, u32 risc
);
436 extern int cx25821_vidupstream_init(struct cx25821_channel
*chan
, int pixel_format
);
437 extern int cx25821_audio_upstream_init(struct cx25821_dev
*dev
,
439 extern int cx25821_write_frame(struct cx25821_channel
*chan
,
440 const char __user
*data
, size_t count
);
441 extern void cx25821_free_mem_upstream(struct cx25821_channel
*chan
);
442 extern void cx25821_free_mem_upstream_audio(struct cx25821_dev
*dev
);
443 extern void cx25821_stop_upstream_video(struct cx25821_channel
*chan
);
444 extern void cx25821_stop_upstream_audio(struct cx25821_dev
*dev
);
445 extern int cx25821_sram_channel_setup_upstream(struct cx25821_dev
*dev
,
446 const struct sram_channel
*ch
,
447 unsigned int bpl
, u32 risc
);
448 extern void cx25821_set_pixel_format(struct cx25821_dev
*dev
, int channel
,