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.
23 #include <linux/pci.h>
24 #include <linux/i2c.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/kdev_t.h>
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-ctrls.h>
33 #include <media/videobuf2-v4l2.h>
34 #include <media/videobuf2-dma-sg.h>
36 #include "cx25821-reg.h"
37 #include "cx25821-medusa-reg.h"
38 #include "cx25821-sram.h"
39 #include "cx25821-audio.h"
41 #include <linux/version.h>
42 #include <linux/mutex.h>
45 #define NO_SYNC_LINE (-1U)
47 #define CX25821_MAXBOARDS 2
49 #define LINE_SIZE_D1 1440
51 /* Number of decoders and encoders */
52 #define MAX_DECODERS 8
53 #define MAX_ENCODERS 2
54 #define QUAD_DECODERS 4
55 #define MAX_CAMERAS 16
57 /* Max number of inputs by card */
58 #define MAX_CX25821_INPUT 8
59 #define RESOURCE_VIDEO0 1
60 #define RESOURCE_VIDEO1 2
61 #define RESOURCE_VIDEO2 4
62 #define RESOURCE_VIDEO3 8
63 #define RESOURCE_VIDEO4 16
64 #define RESOURCE_VIDEO5 32
65 #define RESOURCE_VIDEO6 64
66 #define RESOURCE_VIDEO7 128
67 #define RESOURCE_VIDEO8 256
68 #define RESOURCE_VIDEO9 512
69 #define RESOURCE_VIDEO10 1024
70 #define RESOURCE_VIDEO11 2048
72 #define BUFFER_TIMEOUT (HZ) /* 0.5 seconds */
74 #define UNKNOWN_BOARD 0
75 #define CX25821_BOARD 1
77 /* Currently supported by the driver */
78 #define CX25821_NORMS (\
79 V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_M_KR | \
80 V4L2_STD_PAL_BG | V4L2_STD_PAL_DK | V4L2_STD_PAL_I | \
81 V4L2_STD_PAL_M | V4L2_STD_PAL_N | V4L2_STD_PAL_H | \
84 #define CX25821_BOARD_CONEXANT_ATHENA10 1
85 #define MAX_VID_CHANNEL_NUM 12
88 * Maximum capture-only channels. This can go away once video/audio output
89 * is fully supported in this driver.
91 #define MAX_VID_CAP_CHANNEL_NUM 10
93 #define VID_CHANNEL_NUM 8
97 u32 fourcc
; /* v4l2 format id */
103 struct cx25821_tvnorm
{
110 enum cx25821_src_sel_type
{
111 CX25821_SRC_SEL_EXT_656_VIDEO
= 0,
112 CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
115 struct cx25821_riscmem
{
122 /* buffer for one video frame */
123 struct cx25821_buffer
{
124 /* common v4l buffer stuff -- must be first */
125 struct vb2_v4l2_buffer vb
;
126 struct list_head queue
;
128 /* cx25821 specific */
130 struct cx25821_riscmem risc
;
131 const struct cx25821_fmt
*fmt
;
135 CX25821_UNDEFINED
= 0,
140 struct cx25821_board
{
150 struct cx25821_dev
*dev
;
155 struct i2c_adapter i2c_adap
;
156 struct i2c_client i2c_client
;
159 /* cx25821 registers used for raw addess */
168 struct cx25821_dmaqueue
{
169 struct list_head active
;
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
;
211 struct v4l2_ctrl_handler hdl
;
213 struct video_device vdev
;
214 struct cx25821_dmaqueue dma_vidq
;
215 struct vb2_queue vidq
;
217 const struct sram_channel
*sram_channels
;
219 const struct cx25821_fmt
*fmt
;
221 unsigned int width
, height
;
223 int use_cif_resolution
;
226 /* video output data for the video output channel */
227 struct cx25821_video_out_data
*out
;
233 struct v4l2_device v4l2_dev
;
237 unsigned char pci_rev
, pci_lat
;
238 int pci_bus
, pci_slot
;
244 /* used by cx25821-alsa */
245 struct snd_card
*card
;
249 /* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
250 struct cx25821_i2c i2c_bus
[3];
255 struct cx25821_channel channels
[MAX_VID_CHANNEL_NUM
];
264 unsigned short _max_num_decoders
;
266 /* Analog Audio Upstream */
267 int _audio_is_running
;
268 int _audiopixel_format
;
269 int _is_first_audio_frame
;
270 int _audiofile_status
;
271 int _audio_lines_count
;
272 int _audioframe_count
;
273 int _audio_upstream_channel
;
274 int _last_index_irq
; /* The last interrupt index processed. */
276 __le32
*_risc_audio_jmp_addr
;
277 __le32
*_risc_virt_start_addr
;
278 __le32
*_risc_virt_addr
;
279 dma_addr_t _risc_phys_addr
;
280 dma_addr_t _risc_phys_start_addr
;
282 unsigned int _audiorisc_size
;
283 unsigned int _audiodata_buf_size
;
284 __le32
*_audiodata_buf_virt_addr
;
285 dma_addr_t _audiodata_buf_phys_addr
;
286 char *_audiofilename
;
287 u32 audio_upstream_riscbuf_size
;
288 u32 audio_upstream_databuf_size
;
289 int _audioframe_index
;
290 struct work_struct _audio_work_entry
;
291 char *input_audiofilename
;
297 struct cx25821_video_out_data vid_out_data
[2];
300 static inline struct cx25821_dev
*get_cx25821(struct v4l2_device
*v4l2_dev
)
302 return container_of(v4l2_dev
, struct cx25821_dev
, v4l2_dev
);
305 extern struct cx25821_board cx25821_boards
[];
307 #define SRAM_CH00 0 /* Video A */
308 #define SRAM_CH01 1 /* Video B */
309 #define SRAM_CH02 2 /* Video C */
310 #define SRAM_CH03 3 /* Video D */
311 #define SRAM_CH04 4 /* Video E */
312 #define SRAM_CH05 5 /* Video F */
313 #define SRAM_CH06 6 /* Video G */
314 #define SRAM_CH07 7 /* Video H */
316 #define SRAM_CH08 8 /* Audio A */
317 #define SRAM_CH09 9 /* Video Upstream I */
318 #define SRAM_CH10 10 /* Video Upstream J */
319 #define SRAM_CH11 11 /* Audio Upstream AUD_CHANNEL_B */
321 #define VID_UPSTREAM_SRAM_CHANNEL_I SRAM_CH09
322 #define VID_UPSTREAM_SRAM_CHANNEL_J SRAM_CH10
323 #define AUDIO_UPSTREAM_SRAM_CHANNEL_B SRAM_CH11
325 struct sram_channel
{
348 /* For Upstream Video */
360 extern const struct sram_channel cx25821_sram_channels
[];
362 #define cx_read(reg) readl(dev->lmmio + ((reg)>>2))
363 #define cx_write(reg, value) writel((value), dev->lmmio + ((reg)>>2))
365 #define cx_andor(reg, mask, value) \
366 writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
367 ((value) & (mask)), dev->lmmio+((reg)>>2))
369 #define cx_set(reg, bit) cx_andor((reg), (bit), (bit))
370 #define cx_clear(reg, bit) cx_andor((reg), (bit), 0)
372 #define Set_GPIO_Bit(Bit) (1 << Bit)
373 #define Clear_GPIO_Bit(Bit) (~(1 << Bit))
375 #define CX25821_ERR(fmt, args...) \
376 pr_err("(%d): " fmt, dev->board, ##args)
377 #define CX25821_WARN(fmt, args...) \
378 pr_warn("(%d): " fmt, dev->board, ##args)
379 #define CX25821_INFO(fmt, args...) \
380 pr_info("(%d): " fmt, dev->board, ##args)
382 extern int cx25821_i2c_register(struct cx25821_i2c
*bus
);
383 extern int cx25821_i2c_read(struct cx25821_i2c
*bus
, u16 reg_addr
, int *value
);
384 extern int cx25821_i2c_write(struct cx25821_i2c
*bus
, u16 reg_addr
, int value
);
385 extern int cx25821_i2c_unregister(struct cx25821_i2c
*bus
);
386 extern void cx25821_gpio_init(struct cx25821_dev
*dev
);
387 extern void cx25821_set_gpiopin_direction(struct cx25821_dev
*dev
,
388 int pin_number
, int pin_logic_value
);
390 extern int medusa_video_init(struct cx25821_dev
*dev
);
391 extern int medusa_set_videostandard(struct cx25821_dev
*dev
);
392 extern void medusa_set_resolution(struct cx25821_dev
*dev
, int width
,
394 extern int medusa_set_brightness(struct cx25821_dev
*dev
, int brightness
,
396 extern int medusa_set_contrast(struct cx25821_dev
*dev
, int contrast
,
398 extern int medusa_set_hue(struct cx25821_dev
*dev
, int hue
, int decoder
);
399 extern int medusa_set_saturation(struct cx25821_dev
*dev
, int saturation
,
402 extern int cx25821_sram_channel_setup(struct cx25821_dev
*dev
,
403 const struct sram_channel
*ch
, unsigned int bpl
,
406 extern int cx25821_riscmem_alloc(struct pci_dev
*pci
,
407 struct cx25821_riscmem
*risc
,
409 extern int cx25821_risc_buffer(struct pci_dev
*pci
, struct cx25821_riscmem
*risc
,
410 struct scatterlist
*sglist
,
411 unsigned int top_offset
,
412 unsigned int bottom_offset
,
414 unsigned int padding
, unsigned int lines
);
415 extern int cx25821_risc_databuffer_audio(struct pci_dev
*pci
,
416 struct cx25821_riscmem
*risc
,
417 struct scatterlist
*sglist
,
419 unsigned int lines
, unsigned int lpi
);
420 extern void cx25821_free_buffer(struct cx25821_dev
*dev
,
421 struct cx25821_buffer
*buf
);
422 extern void cx25821_sram_channel_dump(struct cx25821_dev
*dev
,
423 const struct sram_channel
*ch
);
424 extern void cx25821_sram_channel_dump_audio(struct cx25821_dev
*dev
,
425 const struct sram_channel
*ch
);
427 extern struct cx25821_dev
*cx25821_dev_get(struct pci_dev
*pci
);
428 extern void cx25821_print_irqbits(char *name
, char *tag
, char **strings
,
429 int len
, u32 bits
, u32 mask
);
430 extern void cx25821_dev_unregister(struct cx25821_dev
*dev
);
431 extern int cx25821_sram_channel_setup_audio(struct cx25821_dev
*dev
,
432 const struct sram_channel
*ch
,
433 unsigned int bpl
, u32 risc
);
435 extern int cx25821_vidupstream_init(struct cx25821_channel
*chan
, int pixel_format
);
436 extern int cx25821_audio_upstream_init(struct cx25821_dev
*dev
,
438 extern int cx25821_write_frame(struct cx25821_channel
*chan
,
439 const char __user
*data
, size_t count
);
440 extern void cx25821_free_mem_upstream(struct cx25821_channel
*chan
);
441 extern void cx25821_free_mem_upstream_audio(struct cx25821_dev
*dev
);
442 extern void cx25821_stop_upstream_video(struct cx25821_channel
*chan
);
443 extern void cx25821_stop_upstream_audio(struct cx25821_dev
*dev
);
444 extern int cx25821_sram_channel_setup_upstream(struct cx25821_dev
*dev
,
445 const struct sram_channel
*ch
,
446 unsigned int bpl
, u32 risc
);
447 extern void cx25821_set_pixel_format(struct cx25821_dev
*dev
, int channel
,