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/videobuf2-v4l2.h>
38 #include <media/videobuf2-dma-sg.h>
39 
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 
93 /*
94  * Maximum capture-only channels. This can go away once video/audio output
95  * is fully supported in this driver.
96  */
97 #define MAX_VID_CAP_CHANNEL_NUM     10
98 
99 #define VID_CHANNEL_NUM 8
100 
101 struct cx25821_fmt {
102 	char *name;
103 	u32 fourcc;		/* v4l2 format id */
104 	int depth;
105 	int flags;
106 	u32 cxformat;
107 };
108 
109 struct cx25821_tvnorm {
110 	char *name;
111 	v4l2_std_id id;
112 	u32 cxiformat;
113 	u32 cxoformat;
114 };
115 
116 enum cx25821_src_sel_type {
117 	CX25821_SRC_SEL_EXT_656_VIDEO = 0,
118 	CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
119 };
120 
121 struct cx25821_riscmem {
122 	unsigned int   size;
123 	__le32         *cpu;
124 	__le32         *jmp;
125 	dma_addr_t     dma;
126 };
127 
128 /* buffer for one video frame */
129 struct cx25821_buffer {
130 	/* common v4l buffer stuff -- must be first */
131 	struct vb2_v4l2_buffer vb;
132 	struct list_head queue;
133 
134 	/* cx25821 specific */
135 	unsigned int bpl;
136 	struct cx25821_riscmem risc;
137 	const struct cx25821_fmt *fmt;
138 };
139 
140 enum port {
141 	CX25821_UNDEFINED = 0,
142 	CX25821_RAW,
143 	CX25821_264
144 };
145 
146 struct cx25821_board {
147 	const char *name;
148 	enum port porta;
149 	enum port portb;
150 	enum port portc;
151 
152 	u32 clk_freq;
153 };
154 
155 struct cx25821_i2c {
156 	struct cx25821_dev *dev;
157 
158 	int nr;
159 
160 	/* i2c i/o */
161 	struct i2c_adapter i2c_adap;
162 	struct i2c_client i2c_client;
163 	u32 i2c_rc;
164 
165 	/* cx25821 registers used for raw addess */
166 	u32 i2c_period;
167 	u32 reg_ctrl;
168 	u32 reg_stat;
169 	u32 reg_addr;
170 	u32 reg_rdata;
171 	u32 reg_wdata;
172 };
173 
174 struct cx25821_dmaqueue {
175 	struct list_head active;
176 	u32 count;
177 };
178 
179 struct cx25821_dev;
180 
181 struct cx25821_channel;
182 
183 struct cx25821_video_out_data {
184 	struct cx25821_channel *chan;
185 	int _line_size;
186 	int _prog_cnt;
187 	int _pixel_format;
188 	int _is_first_frame;
189 	int _is_running;
190 	int _file_status;
191 	int _lines_count;
192 	int _frame_count;
193 	unsigned int _risc_size;
194 
195 	__le32 *_dma_virt_start_addr;
196 	__le32 *_dma_virt_addr;
197 	dma_addr_t _dma_phys_addr;
198 	dma_addr_t _dma_phys_start_addr;
199 
200 	unsigned int _data_buf_size;
201 	__le32 *_data_buf_virt_addr;
202 	dma_addr_t _data_buf_phys_addr;
203 
204 	u32 upstream_riscbuf_size;
205 	u32 upstream_databuf_size;
206 	int is_60hz;
207 	int _frame_index;
208 	int cur_frame_index;
209 	int curpos;
210 	wait_queue_head_t waitq;
211 };
212 
213 struct cx25821_channel {
214 	unsigned id;
215 	struct cx25821_dev *dev;
216 
217 	struct v4l2_ctrl_handler hdl;
218 
219 	struct video_device vdev;
220 	struct cx25821_dmaqueue dma_vidq;
221 	struct vb2_queue vidq;
222 
223 	const struct sram_channel *sram_channels;
224 
225 	const struct cx25821_fmt *fmt;
226 	unsigned field;
227 	unsigned int width, height;
228 	int pixel_formats;
229 	int use_cif_resolution;
230 	int cif_width;
231 
232 	/* video output data for the video output channel */
233 	struct cx25821_video_out_data *out;
234 };
235 
236 struct snd_card;
237 
238 struct cx25821_dev {
239 	struct v4l2_device v4l2_dev;
240 
241 	/* pci stuff */
242 	struct pci_dev *pci;
243 	unsigned char pci_rev, pci_lat;
244 	int pci_bus, pci_slot;
245 	u32 base_io_addr;
246 	u32 __iomem *lmmio;
247 	u8 __iomem *bmmio;
248 	int pci_irqmask;
249 	int hwrevision;
250 	/* used by cx25821-alsa */
251 	struct snd_card *card;
252 	void *alloc_ctx;
253 
254 	u32 clk_freq;
255 
256 	/* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
257 	struct cx25821_i2c i2c_bus[3];
258 
259 	int nr;
260 	struct mutex lock;
261 
262 	struct cx25821_channel channels[MAX_VID_CHANNEL_NUM];
263 
264 	/* board details */
265 	unsigned int board;
266 	char name[32];
267 
268 	/* Analog video */
269 	unsigned int input;
270 	v4l2_std_id tvnorm;
271 	unsigned short _max_num_decoders;
272 
273 	/* Analog Audio Upstream */
274 	int _audio_is_running;
275 	int _audiopixel_format;
276 	int _is_first_audio_frame;
277 	int _audiofile_status;
278 	int _audio_lines_count;
279 	int _audioframe_count;
280 	int _audio_upstream_channel;
281 	int _last_index_irq;    /* The last interrupt index processed. */
282 
283 	__le32 *_risc_audio_jmp_addr;
284 	__le32 *_risc_virt_start_addr;
285 	__le32 *_risc_virt_addr;
286 	dma_addr_t _risc_phys_addr;
287 	dma_addr_t _risc_phys_start_addr;
288 
289 	unsigned int _audiorisc_size;
290 	unsigned int _audiodata_buf_size;
291 	__le32 *_audiodata_buf_virt_addr;
292 	dma_addr_t _audiodata_buf_phys_addr;
293 	char *_audiofilename;
294 	u32 audio_upstream_riscbuf_size;
295 	u32 audio_upstream_databuf_size;
296 	int _audioframe_index;
297 	struct workqueue_struct *_irq_audio_queues;
298 	struct work_struct _audio_work_entry;
299 	char *input_audiofilename;
300 
301 	/* V4l */
302 	spinlock_t slock;
303 
304 	/* Video Upstream */
305 	struct cx25821_video_out_data vid_out_data[2];
306 };
307 
308 static inline struct cx25821_dev *get_cx25821(struct v4l2_device *v4l2_dev)
309 {
310 	return container_of(v4l2_dev, struct cx25821_dev, v4l2_dev);
311 }
312 
313 extern struct cx25821_board cx25821_boards[];
314 
315 #define SRAM_CH00  0		/* Video A */
316 #define SRAM_CH01  1		/* Video B */
317 #define SRAM_CH02  2		/* Video C */
318 #define SRAM_CH03  3		/* Video D */
319 #define SRAM_CH04  4		/* Video E */
320 #define SRAM_CH05  5		/* Video F */
321 #define SRAM_CH06  6		/* Video G */
322 #define SRAM_CH07  7		/* Video H */
323 
324 #define SRAM_CH08  8		/* Audio A */
325 #define SRAM_CH09  9		/* Video Upstream I */
326 #define SRAM_CH10  10		/* Video Upstream J */
327 #define SRAM_CH11  11		/* Audio Upstream AUD_CHANNEL_B */
328 
329 #define VID_UPSTREAM_SRAM_CHANNEL_I     SRAM_CH09
330 #define VID_UPSTREAM_SRAM_CHANNEL_J     SRAM_CH10
331 #define AUDIO_UPSTREAM_SRAM_CHANNEL_B   SRAM_CH11
332 
333 struct sram_channel {
334 	char *name;
335 	u32 i;
336 	u32 cmds_start;
337 	u32 ctrl_start;
338 	u32 cdt;
339 	u32 fifo_start;
340 	u32 fifo_size;
341 	u32 ptr1_reg;
342 	u32 ptr2_reg;
343 	u32 cnt1_reg;
344 	u32 cnt2_reg;
345 	u32 int_msk;
346 	u32 int_stat;
347 	u32 int_mstat;
348 	u32 dma_ctl;
349 	u32 gpcnt_ctl;
350 	u32 gpcnt;
351 	u32 aud_length;
352 	u32 aud_cfg;
353 	u32 fld_aud_fifo_en;
354 	u32 fld_aud_risc_en;
355 
356 	/* For Upstream Video */
357 	u32 vid_fmt_ctl;
358 	u32 vid_active_ctl1;
359 	u32 vid_active_ctl2;
360 	u32 vid_cdt_size;
361 
362 	u32 vip_ctl;
363 	u32 pix_frmt;
364 	u32 jumponly;
365 	u32 irq_bit;
366 };
367 
368 extern const struct sram_channel cx25821_sram_channels[];
369 
370 #define cx_read(reg)             readl(dev->lmmio + ((reg)>>2))
371 #define cx_write(reg, value)     writel((value), dev->lmmio + ((reg)>>2))
372 
373 #define cx_andor(reg, mask, value) \
374 	writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
375 	((value) & (mask)), dev->lmmio+((reg)>>2))
376 
377 #define cx_set(reg, bit)          cx_andor((reg), (bit), (bit))
378 #define cx_clear(reg, bit)        cx_andor((reg), (bit), 0)
379 
380 #define Set_GPIO_Bit(Bit)                       (1 << Bit)
381 #define Clear_GPIO_Bit(Bit)                     (~(1 << Bit))
382 
383 #define CX25821_ERR(fmt, args...)			\
384 	pr_err("(%d): " fmt, dev->board, ##args)
385 #define CX25821_WARN(fmt, args...)			\
386 	pr_warn("(%d): " fmt, dev->board, ##args)
387 #define CX25821_INFO(fmt, args...)			\
388 	pr_info("(%d): " fmt, dev->board, ##args)
389 
390 extern int cx25821_i2c_register(struct cx25821_i2c *bus);
391 extern int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value);
392 extern int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value);
393 extern int cx25821_i2c_unregister(struct cx25821_i2c *bus);
394 extern void cx25821_gpio_init(struct cx25821_dev *dev);
395 extern void cx25821_set_gpiopin_direction(struct cx25821_dev *dev,
396 					  int pin_number, int pin_logic_value);
397 
398 extern int medusa_video_init(struct cx25821_dev *dev);
399 extern int medusa_set_videostandard(struct cx25821_dev *dev);
400 extern void medusa_set_resolution(struct cx25821_dev *dev, int width,
401 				  int decoder_select);
402 extern int medusa_set_brightness(struct cx25821_dev *dev, int brightness,
403 				 int decoder);
404 extern int medusa_set_contrast(struct cx25821_dev *dev, int contrast,
405 			       int decoder);
406 extern int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder);
407 extern int medusa_set_saturation(struct cx25821_dev *dev, int saturation,
408 				 int decoder);
409 
410 extern int cx25821_sram_channel_setup(struct cx25821_dev *dev,
411 				      const struct sram_channel *ch, unsigned int bpl,
412 				      u32 risc);
413 
414 extern int cx25821_riscmem_alloc(struct pci_dev *pci,
415 				 struct cx25821_riscmem *risc,
416 				 unsigned int size);
417 extern int cx25821_risc_buffer(struct pci_dev *pci, struct cx25821_riscmem *risc,
418 			       struct scatterlist *sglist,
419 			       unsigned int top_offset,
420 			       unsigned int bottom_offset,
421 			       unsigned int bpl,
422 			       unsigned int padding, unsigned int lines);
423 extern int cx25821_risc_databuffer_audio(struct pci_dev *pci,
424 					 struct cx25821_riscmem *risc,
425 					 struct scatterlist *sglist,
426 					 unsigned int bpl,
427 					 unsigned int lines, unsigned int lpi);
428 extern void cx25821_free_buffer(struct cx25821_dev *dev,
429 				struct cx25821_buffer *buf);
430 extern void cx25821_sram_channel_dump(struct cx25821_dev *dev,
431 				      const struct sram_channel *ch);
432 extern void cx25821_sram_channel_dump_audio(struct cx25821_dev *dev,
433 					    const struct sram_channel *ch);
434 
435 extern struct cx25821_dev *cx25821_dev_get(struct pci_dev *pci);
436 extern void cx25821_print_irqbits(char *name, char *tag, char **strings,
437 				  int len, u32 bits, u32 mask);
438 extern void cx25821_dev_unregister(struct cx25821_dev *dev);
439 extern int cx25821_sram_channel_setup_audio(struct cx25821_dev *dev,
440 					    const struct sram_channel *ch,
441 					    unsigned int bpl, u32 risc);
442 
443 extern int cx25821_vidupstream_init(struct cx25821_channel *chan, int pixel_format);
444 extern int cx25821_audio_upstream_init(struct cx25821_dev *dev,
445 				       int channel_select);
446 extern int cx25821_write_frame(struct cx25821_channel *chan,
447 		const char __user *data, size_t count);
448 extern void cx25821_free_mem_upstream(struct cx25821_channel *chan);
449 extern void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev);
450 extern void cx25821_stop_upstream_video(struct cx25821_channel *chan);
451 extern void cx25821_stop_upstream_audio(struct cx25821_dev *dev);
452 extern int cx25821_sram_channel_setup_upstream(struct cx25821_dev *dev,
453 					       const struct sram_channel *ch,
454 					       unsigned int bpl, u32 risc);
455 extern void cx25821_set_pixel_format(struct cx25821_dev *dev, int channel,
456 				     u32 format);
457 
458 #endif
459