1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Driver for the Conexant CX25821 PCIe bridge
5 * Copyright (C) 2009 Conexant Systems Inc.
6 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
7 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
13 #include <linux/pci.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/delay.h>
17 #include <linux/sched.h>
18 #include <linux/kdev_t.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/videobuf2-v4l2.h>
24 #include <media/videobuf2-dma-sg.h>
26 #include "cx25821-reg.h"
27 #include "cx25821-medusa-reg.h"
28 #include "cx25821-sram.h"
29 #include "cx25821-audio.h"
31 #include <linux/version.h>
32 #include <linux/mutex.h>
35 #define NO_SYNC_LINE (-1U)
37 #define CX25821_MAXBOARDS 2
39 #define LINE_SIZE_D1 1440
41 /* Number of decoders and encoders */
42 #define MAX_DECODERS 8
43 #define MAX_ENCODERS 2
44 #define QUAD_DECODERS 4
45 #define MAX_CAMERAS 16
47 /* Max number of inputs by card */
48 #define MAX_CX25821_INPUT 8
49 #define RESOURCE_VIDEO0 1
50 #define RESOURCE_VIDEO1 2
51 #define RESOURCE_VIDEO2 4
52 #define RESOURCE_VIDEO3 8
53 #define RESOURCE_VIDEO4 16
54 #define RESOURCE_VIDEO5 32
55 #define RESOURCE_VIDEO6 64
56 #define RESOURCE_VIDEO7 128
57 #define RESOURCE_VIDEO8 256
58 #define RESOURCE_VIDEO9 512
59 #define RESOURCE_VIDEO10 1024
60 #define RESOURCE_VIDEO11 2048
62 #define BUFFER_TIMEOUT (HZ) /* 0.5 seconds */
64 #define UNKNOWN_BOARD 0
65 #define CX25821_BOARD 1
67 /* Currently supported by the driver */
68 #define CX25821_NORMS (\
69 V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_M_KR | \
70 V4L2_STD_PAL_BG | V4L2_STD_PAL_DK | V4L2_STD_PAL_I | \
71 V4L2_STD_PAL_M | V4L2_STD_PAL_N | V4L2_STD_PAL_H | \
74 #define CX25821_BOARD_CONEXANT_ATHENA10 1
75 #define MAX_VID_CHANNEL_NUM 12
78 * Maximum capture-only channels. This can go away once video/audio output
79 * is fully supported in this driver.
81 #define MAX_VID_CAP_CHANNEL_NUM 10
83 #define VID_CHANNEL_NUM 8
86 u32 fourcc
; /* v4l2 format id */
92 struct cx25821_tvnorm
{
99 enum cx25821_src_sel_type
{
100 CX25821_SRC_SEL_EXT_656_VIDEO
= 0,
101 CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
104 struct cx25821_riscmem
{
111 /* buffer for one video frame */
112 struct cx25821_buffer
{
113 /* common v4l buffer stuff -- must be first */
114 struct vb2_v4l2_buffer vb
;
115 struct list_head queue
;
117 /* cx25821 specific */
119 struct cx25821_riscmem risc
;
120 const struct cx25821_fmt
*fmt
;
124 CX25821_UNDEFINED
= 0,
129 struct cx25821_board
{
139 struct cx25821_dev
*dev
;
144 struct i2c_adapter i2c_adap
;
145 struct i2c_client i2c_client
;
148 /* cx25821 registers used for raw address */
157 struct cx25821_dmaqueue
{
158 struct list_head active
;
164 struct cx25821_channel
;
166 struct cx25821_video_out_data
{
167 struct cx25821_channel
*chan
;
176 unsigned int _risc_size
;
178 __le32
*_dma_virt_start_addr
;
179 __le32
*_dma_virt_addr
;
180 dma_addr_t _dma_phys_addr
;
181 dma_addr_t _dma_phys_start_addr
;
183 unsigned int _data_buf_size
;
184 __le32
*_data_buf_virt_addr
;
185 dma_addr_t _data_buf_phys_addr
;
187 u32 upstream_riscbuf_size
;
188 u32 upstream_databuf_size
;
193 wait_queue_head_t waitq
;
196 struct cx25821_channel
{
198 struct cx25821_dev
*dev
;
200 struct v4l2_ctrl_handler hdl
;
202 struct video_device vdev
;
203 struct cx25821_dmaqueue dma_vidq
;
204 struct vb2_queue vidq
;
206 const struct sram_channel
*sram_channels
;
208 const struct cx25821_fmt
*fmt
;
210 unsigned int width
, height
;
212 int use_cif_resolution
;
215 /* video output data for the video output channel */
216 struct cx25821_video_out_data
*out
;
222 struct v4l2_device v4l2_dev
;
226 unsigned char pci_rev
, pci_lat
;
227 int pci_bus
, pci_slot
;
233 /* used by cx25821-alsa */
234 struct snd_card
*card
;
238 /* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
239 struct cx25821_i2c i2c_bus
[3];
244 struct cx25821_channel channels
[MAX_VID_CHANNEL_NUM
];
253 unsigned short _max_num_decoders
;
255 /* Analog Audio Upstream */
256 int _audio_is_running
;
257 int _audiopixel_format
;
258 int _is_first_audio_frame
;
259 int _audiofile_status
;
260 int _audio_lines_count
;
261 int _audioframe_count
;
262 int _audio_upstream_channel
;
263 int _last_index_irq
; /* The last interrupt index processed. */
265 __le32
*_risc_audio_jmp_addr
;
266 __le32
*_risc_virt_start_addr
;
267 __le32
*_risc_virt_addr
;
268 dma_addr_t _risc_phys_addr
;
269 dma_addr_t _risc_phys_start_addr
;
271 unsigned int _audiorisc_size
;
272 unsigned int _audiodata_buf_size
;
273 __le32
*_audiodata_buf_virt_addr
;
274 dma_addr_t _audiodata_buf_phys_addr
;
275 char *_audiofilename
;
276 u32 audio_upstream_riscbuf_size
;
277 u32 audio_upstream_databuf_size
;
278 int _audioframe_index
;
279 struct work_struct _audio_work_entry
;
280 char *input_audiofilename
;
286 struct cx25821_video_out_data vid_out_data
[2];
289 static inline struct cx25821_dev
*get_cx25821(struct v4l2_device
*v4l2_dev
)
291 return container_of(v4l2_dev
, struct cx25821_dev
, v4l2_dev
);
294 extern struct cx25821_board cx25821_boards
[];
296 #define SRAM_CH00 0 /* Video A */
297 #define SRAM_CH01 1 /* Video B */
298 #define SRAM_CH02 2 /* Video C */
299 #define SRAM_CH03 3 /* Video D */
300 #define SRAM_CH04 4 /* Video E */
301 #define SRAM_CH05 5 /* Video F */
302 #define SRAM_CH06 6 /* Video G */
303 #define SRAM_CH07 7 /* Video H */
305 #define SRAM_CH08 8 /* Audio A */
306 #define SRAM_CH09 9 /* Video Upstream I */
307 #define SRAM_CH10 10 /* Video Upstream J */
308 #define SRAM_CH11 11 /* Audio Upstream AUD_CHANNEL_B */
310 #define VID_UPSTREAM_SRAM_CHANNEL_I SRAM_CH09
311 #define VID_UPSTREAM_SRAM_CHANNEL_J SRAM_CH10
312 #define AUDIO_UPSTREAM_SRAM_CHANNEL_B SRAM_CH11
314 struct sram_channel
{
337 /* For Upstream Video */
349 extern const struct sram_channel cx25821_sram_channels
[];
351 #define cx_read(reg) readl(dev->lmmio + ((reg)>>2))
352 #define cx_write(reg, value) writel((value), dev->lmmio + ((reg)>>2))
354 #define cx_andor(reg, mask, value) \
355 writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
356 ((value) & (mask)), dev->lmmio+((reg)>>2))
358 #define cx_set(reg, bit) cx_andor((reg), (bit), (bit))
359 #define cx_clear(reg, bit) cx_andor((reg), (bit), 0)
361 #define Set_GPIO_Bit(Bit) (1 << Bit)
362 #define Clear_GPIO_Bit(Bit) (~(1 << Bit))
364 #define CX25821_ERR(fmt, args...) \
365 pr_err("(%d): " fmt, dev->board, ##args)
366 #define CX25821_WARN(fmt, args...) \
367 pr_warn("(%d): " fmt, dev->board, ##args)
368 #define CX25821_INFO(fmt, args...) \
369 pr_info("(%d): " fmt, dev->board, ##args)
371 extern int cx25821_i2c_register(struct cx25821_i2c
*bus
);
372 extern int cx25821_i2c_read(struct cx25821_i2c
*bus
, u16 reg_addr
, int *value
);
373 extern int cx25821_i2c_write(struct cx25821_i2c
*bus
, u16 reg_addr
, int value
);
374 extern int cx25821_i2c_unregister(struct cx25821_i2c
*bus
);
375 extern void cx25821_gpio_init(struct cx25821_dev
*dev
);
376 extern void cx25821_set_gpiopin_direction(struct cx25821_dev
*dev
,
377 int pin_number
, int pin_logic_value
);
379 extern int medusa_video_init(struct cx25821_dev
*dev
);
380 extern int medusa_set_videostandard(struct cx25821_dev
*dev
);
381 extern void medusa_set_resolution(struct cx25821_dev
*dev
, int width
,
383 extern int medusa_set_brightness(struct cx25821_dev
*dev
, int brightness
,
385 extern int medusa_set_contrast(struct cx25821_dev
*dev
, int contrast
,
387 extern int medusa_set_hue(struct cx25821_dev
*dev
, int hue
, int decoder
);
388 extern int medusa_set_saturation(struct cx25821_dev
*dev
, int saturation
,
391 extern int cx25821_sram_channel_setup(struct cx25821_dev
*dev
,
392 const struct sram_channel
*ch
, unsigned int bpl
,
395 extern int cx25821_riscmem_alloc(struct pci_dev
*pci
,
396 struct cx25821_riscmem
*risc
,
398 extern int cx25821_risc_buffer(struct pci_dev
*pci
, struct cx25821_riscmem
*risc
,
399 struct scatterlist
*sglist
,
400 unsigned int top_offset
,
401 unsigned int bottom_offset
,
403 unsigned int padding
, unsigned int lines
);
404 extern int cx25821_risc_databuffer_audio(struct pci_dev
*pci
,
405 struct cx25821_riscmem
*risc
,
406 struct scatterlist
*sglist
,
408 unsigned int lines
, unsigned int lpi
);
409 extern void cx25821_free_buffer(struct cx25821_dev
*dev
,
410 struct cx25821_buffer
*buf
);
411 extern void cx25821_sram_channel_dump(struct cx25821_dev
*dev
,
412 const struct sram_channel
*ch
);
413 extern void cx25821_sram_channel_dump_audio(struct cx25821_dev
*dev
,
414 const struct sram_channel
*ch
);
416 extern struct cx25821_dev
*cx25821_dev_get(struct pci_dev
*pci
);
417 extern void cx25821_print_irqbits(char *name
, char *tag
, char **strings
,
418 int len
, u32 bits
, u32 mask
);
419 extern void cx25821_dev_unregister(struct cx25821_dev
*dev
);
420 extern int cx25821_sram_channel_setup_audio(struct cx25821_dev
*dev
,
421 const struct sram_channel
*ch
,
422 unsigned int bpl
, u32 risc
);
424 extern void cx25821_set_pixel_format(struct cx25821_dev
*dev
, int channel
,