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 
20 #ifndef CX25821_H_
21 #define CX25821_H_
22 
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>
29 
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>
35 
36 #include "cx25821-reg.h"
37 #include "cx25821-medusa-reg.h"
38 #include "cx25821-sram.h"
39 #include "cx25821-audio.h"
40 
41 #include <linux/version.h>
42 #include <linux/mutex.h>
43 
44 #define CX25821_VERSION_CODE KERNEL_VERSION(0, 0, 106)
45 
46 #define UNSET (-1U)
47 #define NO_SYNC_LINE (-1U)
48 
49 #define CX25821_MAXBOARDS 2
50 
51 #define LINE_SIZE_D1    1440
52 
53 /* Number of decoders and encoders */
54 #define MAX_DECODERS            8
55 #define MAX_ENCODERS            2
56 #define QUAD_DECODERS           4
57 #define MAX_CAMERAS             16
58 
59 /* Max number of inputs by card */
60 #define MAX_CX25821_INPUT     8
61 #define RESOURCE_VIDEO0       1
62 #define RESOURCE_VIDEO1       2
63 #define RESOURCE_VIDEO2       4
64 #define RESOURCE_VIDEO3       8
65 #define RESOURCE_VIDEO4       16
66 #define RESOURCE_VIDEO5       32
67 #define RESOURCE_VIDEO6       64
68 #define RESOURCE_VIDEO7       128
69 #define RESOURCE_VIDEO8       256
70 #define RESOURCE_VIDEO9       512
71 #define RESOURCE_VIDEO10      1024
72 #define RESOURCE_VIDEO11      2048
73 
74 #define BUFFER_TIMEOUT     (HZ)	/* 0.5 seconds */
75 
76 #define UNKNOWN_BOARD        0
77 #define CX25821_BOARD        1
78 
79 /* Currently supported by the driver */
80 #define CX25821_NORMS (\
81 	V4L2_STD_NTSC_M |  V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_M_KR | \
82 	V4L2_STD_PAL_BG |  V4L2_STD_PAL_DK    |  V4L2_STD_PAL_I    | \
83 	V4L2_STD_PAL_M  |  V4L2_STD_PAL_N     |  V4L2_STD_PAL_H    | \
84 	V4L2_STD_PAL_Nc)
85 
86 #define CX25821_BOARD_CONEXANT_ATHENA10 1
87 #define MAX_VID_CHANNEL_NUM     12
88 
89 /*
90  * Maximum capture-only channels. This can go away once video/audio output
91  * is fully supported in this driver.
92  */
93 #define MAX_VID_CAP_CHANNEL_NUM     10
94 
95 #define VID_CHANNEL_NUM 8
96 
97 struct cx25821_fmt {
98 	char *name;
99 	u32 fourcc;		/* v4l2 format id */
100 	int depth;
101 	int flags;
102 	u32 cxformat;
103 };
104 
105 struct cx25821_tvnorm {
106 	char *name;
107 	v4l2_std_id id;
108 	u32 cxiformat;
109 	u32 cxoformat;
110 };
111 
112 enum cx25821_src_sel_type {
113 	CX25821_SRC_SEL_EXT_656_VIDEO = 0,
114 	CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
115 };
116 
117 struct cx25821_riscmem {
118 	unsigned int   size;
119 	__le32         *cpu;
120 	__le32         *jmp;
121 	dma_addr_t     dma;
122 };
123 
124 /* buffer for one video frame */
125 struct cx25821_buffer {
126 	/* common v4l buffer stuff -- must be first */
127 	struct vb2_v4l2_buffer vb;
128 	struct list_head queue;
129 
130 	/* cx25821 specific */
131 	unsigned int bpl;
132 	struct cx25821_riscmem risc;
133 	const struct cx25821_fmt *fmt;
134 };
135 
136 enum port {
137 	CX25821_UNDEFINED = 0,
138 	CX25821_RAW,
139 	CX25821_264
140 };
141 
142 struct cx25821_board {
143 	const char *name;
144 	enum port porta;
145 	enum port portb;
146 	enum port portc;
147 
148 	u32 clk_freq;
149 };
150 
151 struct cx25821_i2c {
152 	struct cx25821_dev *dev;
153 
154 	int nr;
155 
156 	/* i2c i/o */
157 	struct i2c_adapter i2c_adap;
158 	struct i2c_client i2c_client;
159 	u32 i2c_rc;
160 
161 	/* cx25821 registers used for raw addess */
162 	u32 i2c_period;
163 	u32 reg_ctrl;
164 	u32 reg_stat;
165 	u32 reg_addr;
166 	u32 reg_rdata;
167 	u32 reg_wdata;
168 };
169 
170 struct cx25821_dmaqueue {
171 	struct list_head active;
172 	u32 count;
173 };
174 
175 struct cx25821_dev;
176 
177 struct cx25821_channel;
178 
179 struct cx25821_video_out_data {
180 	struct cx25821_channel *chan;
181 	int _line_size;
182 	int _prog_cnt;
183 	int _pixel_format;
184 	int _is_first_frame;
185 	int _is_running;
186 	int _file_status;
187 	int _lines_count;
188 	int _frame_count;
189 	unsigned int _risc_size;
190 
191 	__le32 *_dma_virt_start_addr;
192 	__le32 *_dma_virt_addr;
193 	dma_addr_t _dma_phys_addr;
194 	dma_addr_t _dma_phys_start_addr;
195 
196 	unsigned int _data_buf_size;
197 	__le32 *_data_buf_virt_addr;
198 	dma_addr_t _data_buf_phys_addr;
199 
200 	u32 upstream_riscbuf_size;
201 	u32 upstream_databuf_size;
202 	int is_60hz;
203 	int _frame_index;
204 	int cur_frame_index;
205 	int curpos;
206 	wait_queue_head_t waitq;
207 };
208 
209 struct cx25821_channel {
210 	unsigned id;
211 	struct cx25821_dev *dev;
212 
213 	struct v4l2_ctrl_handler hdl;
214 
215 	struct video_device vdev;
216 	struct cx25821_dmaqueue dma_vidq;
217 	struct vb2_queue vidq;
218 
219 	const struct sram_channel *sram_channels;
220 
221 	const struct cx25821_fmt *fmt;
222 	unsigned field;
223 	unsigned int width, height;
224 	int pixel_formats;
225 	int use_cif_resolution;
226 	int cif_width;
227 
228 	/* video output data for the video output channel */
229 	struct cx25821_video_out_data *out;
230 };
231 
232 struct snd_card;
233 
234 struct cx25821_dev {
235 	struct v4l2_device v4l2_dev;
236 
237 	/* pci stuff */
238 	struct pci_dev *pci;
239 	unsigned char pci_rev, pci_lat;
240 	int pci_bus, pci_slot;
241 	u32 base_io_addr;
242 	u32 __iomem *lmmio;
243 	u8 __iomem *bmmio;
244 	int pci_irqmask;
245 	int hwrevision;
246 	/* used by cx25821-alsa */
247 	struct snd_card *card;
248 
249 	u32 clk_freq;
250 
251 	/* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
252 	struct cx25821_i2c i2c_bus[3];
253 
254 	int nr;
255 	struct mutex lock;
256 
257 	struct cx25821_channel channels[MAX_VID_CHANNEL_NUM];
258 
259 	/* board details */
260 	unsigned int board;
261 	char name[32];
262 
263 	/* Analog video */
264 	unsigned int input;
265 	v4l2_std_id tvnorm;
266 	unsigned short _max_num_decoders;
267 
268 	/* Analog Audio Upstream */
269 	int _audio_is_running;
270 	int _audiopixel_format;
271 	int _is_first_audio_frame;
272 	int _audiofile_status;
273 	int _audio_lines_count;
274 	int _audioframe_count;
275 	int _audio_upstream_channel;
276 	int _last_index_irq;    /* The last interrupt index processed. */
277 
278 	__le32 *_risc_audio_jmp_addr;
279 	__le32 *_risc_virt_start_addr;
280 	__le32 *_risc_virt_addr;
281 	dma_addr_t _risc_phys_addr;
282 	dma_addr_t _risc_phys_start_addr;
283 
284 	unsigned int _audiorisc_size;
285 	unsigned int _audiodata_buf_size;
286 	__le32 *_audiodata_buf_virt_addr;
287 	dma_addr_t _audiodata_buf_phys_addr;
288 	char *_audiofilename;
289 	u32 audio_upstream_riscbuf_size;
290 	u32 audio_upstream_databuf_size;
291 	int _audioframe_index;
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 
302 static 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 
307 extern 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 
327 struct 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 
362 extern 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 
384 extern int cx25821_i2c_register(struct cx25821_i2c *bus);
385 extern int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value);
386 extern int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value);
387 extern int cx25821_i2c_unregister(struct cx25821_i2c *bus);
388 extern void cx25821_gpio_init(struct cx25821_dev *dev);
389 extern void cx25821_set_gpiopin_direction(struct cx25821_dev *dev,
390 					  int pin_number, int pin_logic_value);
391 
392 extern int medusa_video_init(struct cx25821_dev *dev);
393 extern int medusa_set_videostandard(struct cx25821_dev *dev);
394 extern void medusa_set_resolution(struct cx25821_dev *dev, int width,
395 				  int decoder_select);
396 extern int medusa_set_brightness(struct cx25821_dev *dev, int brightness,
397 				 int decoder);
398 extern int medusa_set_contrast(struct cx25821_dev *dev, int contrast,
399 			       int decoder);
400 extern int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder);
401 extern int medusa_set_saturation(struct cx25821_dev *dev, int saturation,
402 				 int decoder);
403 
404 extern int cx25821_sram_channel_setup(struct cx25821_dev *dev,
405 				      const struct sram_channel *ch, unsigned int bpl,
406 				      u32 risc);
407 
408 extern int cx25821_riscmem_alloc(struct pci_dev *pci,
409 				 struct cx25821_riscmem *risc,
410 				 unsigned int size);
411 extern int cx25821_risc_buffer(struct pci_dev *pci, struct cx25821_riscmem *risc,
412 			       struct scatterlist *sglist,
413 			       unsigned int top_offset,
414 			       unsigned int bottom_offset,
415 			       unsigned int bpl,
416 			       unsigned int padding, unsigned int lines);
417 extern int cx25821_risc_databuffer_audio(struct pci_dev *pci,
418 					 struct cx25821_riscmem *risc,
419 					 struct scatterlist *sglist,
420 					 unsigned int bpl,
421 					 unsigned int lines, unsigned int lpi);
422 extern void cx25821_free_buffer(struct cx25821_dev *dev,
423 				struct cx25821_buffer *buf);
424 extern void cx25821_sram_channel_dump(struct cx25821_dev *dev,
425 				      const struct sram_channel *ch);
426 extern void cx25821_sram_channel_dump_audio(struct cx25821_dev *dev,
427 					    const struct sram_channel *ch);
428 
429 extern struct cx25821_dev *cx25821_dev_get(struct pci_dev *pci);
430 extern void cx25821_print_irqbits(char *name, char *tag, char **strings,
431 				  int len, u32 bits, u32 mask);
432 extern void cx25821_dev_unregister(struct cx25821_dev *dev);
433 extern int cx25821_sram_channel_setup_audio(struct cx25821_dev *dev,
434 					    const struct sram_channel *ch,
435 					    unsigned int bpl, u32 risc);
436 
437 extern int cx25821_vidupstream_init(struct cx25821_channel *chan, int pixel_format);
438 extern int cx25821_audio_upstream_init(struct cx25821_dev *dev,
439 				       int channel_select);
440 extern int cx25821_write_frame(struct cx25821_channel *chan,
441 		const char __user *data, size_t count);
442 extern void cx25821_free_mem_upstream(struct cx25821_channel *chan);
443 extern void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev);
444 extern void cx25821_stop_upstream_video(struct cx25821_channel *chan);
445 extern void cx25821_stop_upstream_audio(struct cx25821_dev *dev);
446 extern int cx25821_sram_channel_setup_upstream(struct cx25821_dev *dev,
447 					       const struct sram_channel *ch,
448 					       unsigned int bpl, u32 risc);
449 extern void cx25821_set_pixel_format(struct cx25821_dev *dev, int channel,
450 				     u32 format);
451 
452 #endif
453