1/*
2 *  Driver for the Conexant CX25821 PCIe bridge
3 *
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
7 *
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.
12 *
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
16 *
17 *  GNU General Public License for more details.
18 *
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.
22 */
23
24#ifndef CX25821_H_
25#define CX25821_H_
26
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>
33
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>
38
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"
44
45#include <linux/version.h>
46#include <linux/mutex.h>
47
48#define CX25821_VERSION_CODE KERNEL_VERSION(0, 0, 106)
49
50#define UNSET (-1U)
51#define NO_SYNC_LINE (-1U)
52
53#define CX25821_MAXBOARDS 2
54
55#define LINE_SIZE_D1    1440
56
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
62
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
77
78#define BUFFER_TIMEOUT     (HZ)	/* 0.5 seconds */
79
80#define UNKNOWN_BOARD        0
81#define CX25821_BOARD        1
82
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    | \
88	V4L2_STD_PAL_Nc)
89
90#define CX25821_BOARD_CONEXANT_ATHENA10 1
91#define MAX_VID_CHANNEL_NUM     12
92#define VID_CHANNEL_NUM 8
93
94struct cx25821_fmt {
95	char *name;
96	u32 fourcc;		/* v4l2 format id */
97	int depth;
98	int flags;
99	u32 cxformat;
100};
101
102struct cx25821_tvnorm {
103	char *name;
104	v4l2_std_id id;
105	u32 cxiformat;
106	u32 cxoformat;
107};
108
109enum cx25821_src_sel_type {
110	CX25821_SRC_SEL_EXT_656_VIDEO = 0,
111	CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
112};
113
114/* buffer for one video frame */
115struct cx25821_buffer {
116	/* common v4l buffer stuff -- must be first */
117	struct videobuf_buffer vb;
118
119	/* cx25821 specific */
120	unsigned int bpl;
121	struct btcx_riscmem risc;
122	const struct cx25821_fmt *fmt;
123	u32 count;
124};
125
126enum port {
127	CX25821_UNDEFINED = 0,
128	CX25821_RAW,
129	CX25821_264
130};
131
132struct cx25821_board {
133	const char *name;
134	enum port porta;
135	enum port portb;
136	enum port portc;
137
138	u32 clk_freq;
139};
140
141struct cx25821_i2c {
142	struct cx25821_dev *dev;
143
144	int nr;
145
146	/* i2c i/o */
147	struct i2c_adapter i2c_adap;
148	struct i2c_client i2c_client;
149	u32 i2c_rc;
150
151	/* cx25821 registers used for raw addess */
152	u32 i2c_period;
153	u32 reg_ctrl;
154	u32 reg_stat;
155	u32 reg_addr;
156	u32 reg_rdata;
157	u32 reg_wdata;
158};
159
160struct cx25821_dmaqueue {
161	struct list_head active;
162	struct list_head queued;
163	struct timer_list timeout;
164	struct btcx_riscmem stopper;
165	u32 count;
166};
167
168struct cx25821_data {
169	struct cx25821_dev *dev;
170	const struct sram_channel *channel;
171};
172
173struct cx25821_dev;
174
175struct cx25821_channel;
176
177struct cx25821_video_out_data {
178	struct cx25821_channel *chan;
179	int _line_size;
180	int _prog_cnt;
181	int _pixel_format;
182	int _is_first_frame;
183	int _is_running;
184	int _file_status;
185	int _lines_count;
186	int _frame_count;
187	unsigned int _risc_size;
188
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;
193
194	unsigned int _data_buf_size;
195	__le32 *_data_buf_virt_addr;
196	dma_addr_t _data_buf_phys_addr;
197
198	u32 upstream_riscbuf_size;
199	u32 upstream_databuf_size;
200	int is_60hz;
201	int _frame_index;
202	int cur_frame_index;
203	int curpos;
204	wait_queue_head_t waitq;
205};
206
207struct cx25821_channel {
208	unsigned id;
209	struct cx25821_dev *dev;
210	struct v4l2_fh *streaming_fh;
211
212	struct v4l2_ctrl_handler hdl;
213	struct cx25821_data timeout_data;
214
215	struct video_device vdev;
216	struct cx25821_dmaqueue dma_vidq;
217	struct videobuf_queue vidq;
218
219	const struct sram_channel *sram_channels;
220
221	const struct cx25821_fmt *fmt;
222	unsigned int width, height;
223	int pixel_formats;
224	int use_cif_resolution;
225	int cif_width;
226
227	/* video output data for the video output channel */
228	struct cx25821_video_out_data *out;
229};
230
231struct snd_card;
232
233struct cx25821_dev {
234	struct v4l2_device v4l2_dev;
235
236	/* pci stuff */
237	struct pci_dev *pci;
238	unsigned char pci_rev, pci_lat;
239	int pci_bus, pci_slot;
240	u32 base_io_addr;
241	u32 __iomem *lmmio;
242	u8 __iomem *bmmio;
243	int pci_irqmask;
244	int hwrevision;
245	/* used by cx25821-alsa */
246	struct snd_card *card;
247
248	u32 clk_freq;
249
250	/* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
251	struct cx25821_i2c i2c_bus[3];
252
253	int nr;
254	struct mutex lock;
255
256	struct cx25821_channel channels[MAX_VID_CHANNEL_NUM];
257
258	/* board details */
259	unsigned int board;
260	char name[32];
261
262	/* Analog video */
263	unsigned int input;
264	v4l2_std_id tvnorm;
265	unsigned short _max_num_decoders;
266
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. */
276
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;
282
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;
294
295	/* V4l */
296	spinlock_t slock;
297
298	/* Video Upstream */
299	struct cx25821_video_out_data vid_out_data[2];
300};
301
302static inline struct cx25821_dev *get_cx25821(struct v4l2_device *v4l2_dev)
303{
304	return container_of(v4l2_dev, struct cx25821_dev, v4l2_dev);
305}
306
307extern struct cx25821_board cx25821_boards[];
308
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 */
317
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 */
322
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
326
327struct sram_channel {
328	char *name;
329	u32 i;
330	u32 cmds_start;
331	u32 ctrl_start;
332	u32 cdt;
333	u32 fifo_start;
334	u32 fifo_size;
335	u32 ptr1_reg;
336	u32 ptr2_reg;
337	u32 cnt1_reg;
338	u32 cnt2_reg;
339	u32 int_msk;
340	u32 int_stat;
341	u32 int_mstat;
342	u32 dma_ctl;
343	u32 gpcnt_ctl;
344	u32 gpcnt;
345	u32 aud_length;
346	u32 aud_cfg;
347	u32 fld_aud_fifo_en;
348	u32 fld_aud_risc_en;
349
350	/* For Upstream Video */
351	u32 vid_fmt_ctl;
352	u32 vid_active_ctl1;
353	u32 vid_active_ctl2;
354	u32 vid_cdt_size;
355
356	u32 vip_ctl;
357	u32 pix_frmt;
358	u32 jumponly;
359	u32 irq_bit;
360};
361
362extern const struct sram_channel cx25821_sram_channels[];
363
364#define cx_read(reg)             readl(dev->lmmio + ((reg)>>2))
365#define cx_write(reg, value)     writel((value), dev->lmmio + ((reg)>>2))
366
367#define cx_andor(reg, mask, value) \
368	writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
369	((value) & (mask)), dev->lmmio+((reg)>>2))
370
371#define cx_set(reg, bit)          cx_andor((reg), (bit), (bit))
372#define cx_clear(reg, bit)        cx_andor((reg), (bit), 0)
373
374#define Set_GPIO_Bit(Bit)                       (1 << Bit)
375#define Clear_GPIO_Bit(Bit)                     (~(1 << Bit))
376
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)
383
384extern int cx25821_i2c_register(struct cx25821_i2c *bus);
385extern int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value);
386extern int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value);
387extern int cx25821_i2c_unregister(struct cx25821_i2c *bus);
388extern void cx25821_gpio_init(struct cx25821_dev *dev);
389extern void cx25821_set_gpiopin_direction(struct cx25821_dev *dev,
390					  int pin_number, int pin_logic_value);
391
392extern int medusa_video_init(struct cx25821_dev *dev);
393extern int medusa_set_videostandard(struct cx25821_dev *dev);
394extern void medusa_set_resolution(struct cx25821_dev *dev, int width,
395				  int decoder_select);
396extern int medusa_set_brightness(struct cx25821_dev *dev, int brightness,
397				 int decoder);
398extern int medusa_set_contrast(struct cx25821_dev *dev, int contrast,
399			       int decoder);
400extern int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder);
401extern int medusa_set_saturation(struct cx25821_dev *dev, int saturation,
402				 int decoder);
403
404extern int cx25821_sram_channel_setup(struct cx25821_dev *dev,
405				      const struct sram_channel *ch, unsigned int bpl,
406				      u32 risc);
407
408extern 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,
412			       unsigned int bpl,
413			       unsigned int padding, unsigned int lines);
414extern int cx25821_risc_databuffer_audio(struct pci_dev *pci,
415					 struct btcx_riscmem *risc,
416					 struct scatterlist *sglist,
417					 unsigned int bpl,
418					 unsigned int lines, unsigned int lpi);
419extern void cx25821_free_buffer(struct videobuf_queue *q,
420				struct cx25821_buffer *buf);
421extern int cx25821_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
422				u32 reg, u32 mask, u32 value);
423extern void cx25821_sram_channel_dump(struct cx25821_dev *dev,
424				      const struct sram_channel *ch);
425extern void cx25821_sram_channel_dump_audio(struct cx25821_dev *dev,
426					    const struct sram_channel *ch);
427
428extern struct cx25821_dev *cx25821_dev_get(struct pci_dev *pci);
429extern void cx25821_print_irqbits(char *name, char *tag, char **strings,
430				  int len, u32 bits, u32 mask);
431extern void cx25821_dev_unregister(struct cx25821_dev *dev);
432extern int cx25821_sram_channel_setup_audio(struct cx25821_dev *dev,
433					    const struct sram_channel *ch,
434					    unsigned int bpl, u32 risc);
435
436extern int cx25821_vidupstream_init(struct cx25821_channel *chan, int pixel_format);
437extern int cx25821_audio_upstream_init(struct cx25821_dev *dev,
438				       int channel_select);
439extern int cx25821_write_frame(struct cx25821_channel *chan,
440		const char __user *data, size_t count);
441extern void cx25821_free_mem_upstream(struct cx25821_channel *chan);
442extern void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev);
443extern void cx25821_stop_upstream_video(struct cx25821_channel *chan);
444extern void cx25821_stop_upstream_audio(struct cx25821_dev *dev);
445extern int cx25821_sram_channel_setup_upstream(struct cx25821_dev *dev,
446					       const struct sram_channel *ch,
447					       unsigned int bpl, u32 risc);
448extern void cx25821_set_pixel_format(struct cx25821_dev *dev, int channel,
449				     u32 format);
450
451#endif
452