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/mutex.h>
34 #define NO_SYNC_LINE (-1U)
36 #define CX25821_MAXBOARDS 2
38 #define LINE_SIZE_D1 1440
40 /* Number of decoders and encoders */
41 #define MAX_DECODERS 8
42 #define MAX_ENCODERS 2
43 #define QUAD_DECODERS 4
44 #define MAX_CAMERAS 16
46 /* Max number of inputs by card */
47 #define MAX_CX25821_INPUT 8
48 #define RESOURCE_VIDEO0 1
49 #define RESOURCE_VIDEO1 2
50 #define RESOURCE_VIDEO2 4
51 #define RESOURCE_VIDEO3 8
52 #define RESOURCE_VIDEO4 16
53 #define RESOURCE_VIDEO5 32
54 #define RESOURCE_VIDEO6 64
55 #define RESOURCE_VIDEO7 128
56 #define RESOURCE_VIDEO8 256
57 #define RESOURCE_VIDEO9 512
58 #define RESOURCE_VIDEO10 1024
59 #define RESOURCE_VIDEO11 2048
61 #define BUFFER_TIMEOUT (HZ) /* 0.5 seconds */
63 #define UNKNOWN_BOARD 0
64 #define CX25821_BOARD 1
66 /* Currently supported by the driver */
67 #define CX25821_NORMS (\
68 V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_M_KR | \
69 V4L2_STD_PAL_BG | V4L2_STD_PAL_DK | V4L2_STD_PAL_I | \
70 V4L2_STD_PAL_M | V4L2_STD_PAL_N | V4L2_STD_PAL_H | \
73 #define CX25821_BOARD_CONEXANT_ATHENA10 1
74 #define MAX_VID_CHANNEL_NUM 12
77 * Maximum capture-only channels. This can go away once video/audio output
78 * is fully supported in this driver.
80 #define MAX_VID_CAP_CHANNEL_NUM 10
82 #define VID_CHANNEL_NUM 8
85 u32 fourcc
; /* v4l2 format id */
91 struct cx25821_tvnorm
{
98 enum cx25821_src_sel_type
{
99 CX25821_SRC_SEL_EXT_656_VIDEO
= 0,
100 CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
103 struct cx25821_riscmem
{
110 /* buffer for one video frame */
111 struct cx25821_buffer
{
112 /* common v4l buffer stuff -- must be first */
113 struct vb2_v4l2_buffer vb
;
114 struct list_head queue
;
116 /* cx25821 specific */
118 struct cx25821_riscmem risc
;
119 const struct cx25821_fmt
*fmt
;
123 CX25821_UNDEFINED
= 0,
128 struct cx25821_board
{
138 struct cx25821_dev
*dev
;
143 struct i2c_adapter i2c_adap
;
144 struct i2c_client i2c_client
;
147 /* cx25821 registers used for raw address */
156 struct cx25821_dmaqueue
{
157 struct list_head active
;
163 struct cx25821_channel
;
165 struct cx25821_video_out_data
{
166 struct cx25821_channel
*chan
;
175 unsigned int _risc_size
;
177 __le32
*_dma_virt_start_addr
;
178 __le32
*_dma_virt_addr
;
179 dma_addr_t _dma_phys_addr
;
180 dma_addr_t _dma_phys_start_addr
;
182 unsigned int _data_buf_size
;
183 __le32
*_data_buf_virt_addr
;
184 dma_addr_t _data_buf_phys_addr
;
186 u32 upstream_riscbuf_size
;
187 u32 upstream_databuf_size
;
192 wait_queue_head_t waitq
;
195 struct cx25821_channel
{
197 struct cx25821_dev
*dev
;
199 struct v4l2_ctrl_handler hdl
;
201 struct video_device vdev
;
202 struct cx25821_dmaqueue dma_vidq
;
203 struct vb2_queue vidq
;
205 const struct sram_channel
*sram_channels
;
207 const struct cx25821_fmt
*fmt
;
209 unsigned int width
, height
;
211 int use_cif_resolution
;
214 /* video output data for the video output channel */
215 struct cx25821_video_out_data
*out
;
221 struct v4l2_device v4l2_dev
;
225 unsigned char pci_rev
, pci_lat
;
226 int pci_bus
, pci_slot
;
232 /* used by cx25821-alsa */
233 struct snd_card
*card
;
237 /* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
238 struct cx25821_i2c i2c_bus
[3];
243 struct cx25821_channel channels
[MAX_VID_CHANNEL_NUM
];
252 unsigned short _max_num_decoders
;
254 /* Analog Audio Upstream */
255 int _audio_is_running
;
256 int _audiopixel_format
;
257 int _is_first_audio_frame
;
258 int _audiofile_status
;
259 int _audio_lines_count
;
260 int _audioframe_count
;
261 int _audio_upstream_channel
;
262 int _last_index_irq
; /* The last interrupt index processed. */
264 __le32
*_risc_audio_jmp_addr
;
265 __le32
*_risc_virt_start_addr
;
266 __le32
*_risc_virt_addr
;
267 dma_addr_t _risc_phys_addr
;
268 dma_addr_t _risc_phys_start_addr
;
270 unsigned int _audiorisc_size
;
271 unsigned int _audiodata_buf_size
;
272 __le32
*_audiodata_buf_virt_addr
;
273 dma_addr_t _audiodata_buf_phys_addr
;
274 char *_audiofilename
;
275 u32 audio_upstream_riscbuf_size
;
276 u32 audio_upstream_databuf_size
;
277 int _audioframe_index
;
278 struct work_struct _audio_work_entry
;
279 char *input_audiofilename
;
285 struct cx25821_video_out_data vid_out_data
[2];
288 static inline struct cx25821_dev
*get_cx25821(struct v4l2_device
*v4l2_dev
)
290 return container_of(v4l2_dev
, struct cx25821_dev
, v4l2_dev
);
293 extern struct cx25821_board cx25821_boards
[];
295 #define SRAM_CH00 0 /* Video A */
296 #define SRAM_CH01 1 /* Video B */
297 #define SRAM_CH02 2 /* Video C */
298 #define SRAM_CH03 3 /* Video D */
299 #define SRAM_CH04 4 /* Video E */
300 #define SRAM_CH05 5 /* Video F */
301 #define SRAM_CH06 6 /* Video G */
302 #define SRAM_CH07 7 /* Video H */
304 #define SRAM_CH08 8 /* Audio A */
305 #define SRAM_CH09 9 /* Video Upstream I */
306 #define SRAM_CH10 10 /* Video Upstream J */
307 #define SRAM_CH11 11 /* Audio Upstream AUD_CHANNEL_B */
309 #define VID_UPSTREAM_SRAM_CHANNEL_I SRAM_CH09
310 #define VID_UPSTREAM_SRAM_CHANNEL_J SRAM_CH10
311 #define AUDIO_UPSTREAM_SRAM_CHANNEL_B SRAM_CH11
313 struct sram_channel
{
336 /* For Upstream Video */
348 extern const struct sram_channel cx25821_sram_channels
[];
350 #define cx_read(reg) readl(dev->lmmio + ((reg)>>2))
351 #define cx_write(reg, value) writel((value), dev->lmmio + ((reg)>>2))
353 #define cx_andor(reg, mask, value) \
354 writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
355 ((value) & (mask)), dev->lmmio+((reg)>>2))
357 #define cx_set(reg, bit) cx_andor((reg), (bit), (bit))
358 #define cx_clear(reg, bit) cx_andor((reg), (bit), 0)
360 #define Set_GPIO_Bit(Bit) (1 << Bit)
361 #define Clear_GPIO_Bit(Bit) (~(1 << Bit))
363 #define CX25821_ERR(fmt, args...) \
364 pr_err("(%d): " fmt, dev->board, ##args)
365 #define CX25821_WARN(fmt, args...) \
366 pr_warn("(%d): " fmt, dev->board, ##args)
367 #define CX25821_INFO(fmt, args...) \
368 pr_info("(%d): " fmt, dev->board, ##args)
370 extern int cx25821_i2c_register(struct cx25821_i2c
*bus
);
371 extern int cx25821_i2c_read(struct cx25821_i2c
*bus
, u16 reg_addr
, int *value
);
372 extern int cx25821_i2c_write(struct cx25821_i2c
*bus
, u16 reg_addr
, int value
);
373 extern int cx25821_i2c_unregister(struct cx25821_i2c
*bus
);
374 extern void cx25821_gpio_init(struct cx25821_dev
*dev
);
375 extern void cx25821_set_gpiopin_direction(struct cx25821_dev
*dev
,
376 int pin_number
, int pin_logic_value
);
378 extern int medusa_video_init(struct cx25821_dev
*dev
);
379 extern int medusa_set_videostandard(struct cx25821_dev
*dev
);
380 extern void medusa_set_resolution(struct cx25821_dev
*dev
, int width
,
382 extern int medusa_set_brightness(struct cx25821_dev
*dev
, int brightness
,
384 extern int medusa_set_contrast(struct cx25821_dev
*dev
, int contrast
,
386 extern int medusa_set_hue(struct cx25821_dev
*dev
, int hue
, int decoder
);
387 extern int medusa_set_saturation(struct cx25821_dev
*dev
, int saturation
,
390 extern int cx25821_sram_channel_setup(struct cx25821_dev
*dev
,
391 const struct sram_channel
*ch
, unsigned int bpl
,
394 extern int cx25821_riscmem_alloc(struct pci_dev
*pci
,
395 struct cx25821_riscmem
*risc
,
397 extern int cx25821_risc_buffer(struct pci_dev
*pci
, struct cx25821_riscmem
*risc
,
398 struct scatterlist
*sglist
,
399 unsigned int top_offset
,
400 unsigned int bottom_offset
,
402 unsigned int padding
, unsigned int lines
);
403 extern int cx25821_risc_databuffer_audio(struct pci_dev
*pci
,
404 struct cx25821_riscmem
*risc
,
405 struct scatterlist
*sglist
,
407 unsigned int lines
, unsigned int lpi
);
408 extern void cx25821_free_buffer(struct cx25821_dev
*dev
,
409 struct cx25821_buffer
*buf
);
410 extern void cx25821_sram_channel_dump(struct cx25821_dev
*dev
,
411 const struct sram_channel
*ch
);
412 extern void cx25821_sram_channel_dump_audio(struct cx25821_dev
*dev
,
413 const struct sram_channel
*ch
);
415 extern struct cx25821_dev
*cx25821_dev_get(struct pci_dev
*pci
);
416 extern void cx25821_print_irqbits(char *name
, char *tag
, char **strings
,
417 int len
, u32 bits
, u32 mask
);
418 extern void cx25821_dev_unregister(struct cx25821_dev
*dev
);
419 extern int cx25821_sram_channel_setup_audio(struct cx25821_dev
*dev
,
420 const struct sram_channel
*ch
,
421 unsigned int bpl
, u32 risc
);
423 extern void cx25821_set_pixel_format(struct cx25821_dev
*dev
, int channel
,