]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/media/pci/cx25821/cx25821.h
67b3c550e4540944ea4e5b72edd8847d0839f520
[karo-tx-linux.git] / drivers / media / pci / cx25821 / cx25821.h
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
94 struct cx25821_fmt {
95         char *name;
96         u32 fourcc;             /* v4l2 format id */
97         int depth;
98         int flags;
99         u32 cxformat;
100 };
101
102 struct cx25821_tvnorm {
103         char *name;
104         v4l2_std_id id;
105         u32 cxiformat;
106         u32 cxoformat;
107 };
108
109 enum 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 */
115 struct 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
126 enum port {
127         CX25821_UNDEFINED = 0,
128         CX25821_RAW,
129         CX25821_264
130 };
131
132 struct 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
141 struct 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
160 struct 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
168 struct cx25821_data {
169         struct cx25821_dev *dev;
170         const struct sram_channel *channel;
171 };
172
173 struct cx25821_dev;
174
175 struct cx25821_channel {
176         unsigned id;
177         struct cx25821_dev *dev;
178         struct v4l2_fh *streaming_fh;
179
180         struct v4l2_ctrl_handler hdl;
181         struct cx25821_data timeout_data;
182
183         struct video_device vdev;
184         struct cx25821_dmaqueue dma_vidq;
185         struct videobuf_queue vidq;
186
187         const struct sram_channel *sram_channels;
188
189         const struct cx25821_fmt *fmt;
190         unsigned int width, height;
191         int pixel_formats;
192         int use_cif_resolution;
193         int cif_width;
194 };
195
196 struct snd_card;
197
198 struct cx25821_dev {
199         struct v4l2_device v4l2_dev;
200
201         /* pci stuff */
202         struct pci_dev *pci;
203         unsigned char pci_rev, pci_lat;
204         int pci_bus, pci_slot;
205         u32 base_io_addr;
206         u32 __iomem *lmmio;
207         u8 __iomem *bmmio;
208         int pci_irqmask;
209         int hwrevision;
210         /* used by cx25821-alsa */
211         struct snd_card *card;
212
213         u32 clk_freq;
214
215         /* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
216         struct cx25821_i2c i2c_bus[3];
217
218         int nr;
219         struct mutex lock;
220
221         struct cx25821_channel channels[MAX_VID_CHANNEL_NUM];
222
223         /* board details */
224         unsigned int board;
225         char name[32];
226
227         /* Analog video */
228         unsigned int input;
229         v4l2_std_id tvnorm;
230         unsigned short _max_num_decoders;
231
232         /* Analog Audio Upstream */
233         int _audio_is_running;
234         int _audiopixel_format;
235         int _is_first_audio_frame;
236         int _audiofile_status;
237         int _audio_lines_count;
238         int _audioframe_count;
239         int _audio_upstream_channel;
240         int _last_index_irq;    /* The last interrupt index processed. */
241
242         __le32 *_risc_audio_jmp_addr;
243         __le32 *_risc_virt_start_addr;
244         __le32 *_risc_virt_addr;
245         dma_addr_t _risc_phys_addr;
246         dma_addr_t _risc_phys_start_addr;
247
248         unsigned int _audiorisc_size;
249         unsigned int _audiodata_buf_size;
250         __le32 *_audiodata_buf_virt_addr;
251         dma_addr_t _audiodata_buf_phys_addr;
252         char *_audiofilename;
253
254         /* V4l */
255         spinlock_t slock;
256
257         /* Video Upstream */
258         int _line_size;
259         int _prog_cnt;
260         int _pixel_format;
261         int _is_first_frame;
262         int _is_running;
263         int _file_status;
264         int _lines_count;
265         int _frame_count;
266         int _channel_upstream_select;
267         unsigned int _risc_size;
268
269         __le32 *_dma_virt_start_addr;
270         __le32 *_dma_virt_addr;
271         dma_addr_t _dma_phys_addr;
272         dma_addr_t _dma_phys_start_addr;
273
274         unsigned int _data_buf_size;
275         __le32 *_data_buf_virt_addr;
276         dma_addr_t _data_buf_phys_addr;
277         char *_filename;
278         char *_defaultname;
279
280         int _line_size_ch2;
281         int _prog_cnt_ch2;
282         int _pixel_format_ch2;
283         int _is_first_frame_ch2;
284         int _is_running_ch2;
285         int _file_status_ch2;
286         int _lines_count_ch2;
287         int _frame_count_ch2;
288         int _channel2_upstream_select;
289         unsigned int _risc_size_ch2;
290
291         __le32 *_dma_virt_start_addr_ch2;
292         __le32 *_dma_virt_addr_ch2;
293         dma_addr_t _dma_phys_addr_ch2;
294         dma_addr_t _dma_phys_start_addr_ch2;
295
296         unsigned int _data_buf_size_ch2;
297         __le32 *_data_buf_virt_addr_ch2;
298         dma_addr_t _data_buf_phys_addr_ch2;
299         char *_filename_ch2;
300         char *_defaultname_ch2;
301
302         u32 upstream_riscbuf_size;
303         u32 upstream_databuf_size;
304         u32 upstream_riscbuf_size_ch2;
305         u32 upstream_databuf_size_ch2;
306         u32 audio_upstream_riscbuf_size;
307         u32 audio_upstream_databuf_size;
308         int _isNTSC;
309         int _frame_index;
310         int _audioframe_index;
311         struct workqueue_struct *_irq_queues;
312         struct work_struct _irq_work_entry;
313         struct workqueue_struct *_irq_queues_ch2;
314         struct work_struct _irq_work_entry_ch2;
315         struct workqueue_struct *_irq_audio_queues;
316         struct work_struct _audio_work_entry;
317         char *input_filename;
318         char *input_filename_ch2;
319         int _frame_index_ch2;
320         int _isNTSC_ch2;
321         char *vid_stdname_ch2;
322         int pixel_format_ch2;
323         int channel_select_ch2;
324         int command_ch2;
325         char *input_audiofilename;
326         char *vid_stdname;
327         int pixel_format;
328         int channel_select;
329         int command;
330 };
331
332 struct upstream_user_struct {
333         char *input_filename;
334         char *vid_stdname;
335         int pixel_format;
336         int channel_select;
337         int command;
338 };
339
340 static inline struct cx25821_dev *get_cx25821(struct v4l2_device *v4l2_dev)
341 {
342         return container_of(v4l2_dev, struct cx25821_dev, v4l2_dev);
343 }
344
345 extern struct cx25821_board cx25821_boards[];
346
347 #define SRAM_CH00  0            /* Video A */
348 #define SRAM_CH01  1            /* Video B */
349 #define SRAM_CH02  2            /* Video C */
350 #define SRAM_CH03  3            /* Video D */
351 #define SRAM_CH04  4            /* Video E */
352 #define SRAM_CH05  5            /* Video F */
353 #define SRAM_CH06  6            /* Video G */
354 #define SRAM_CH07  7            /* Video H */
355
356 #define SRAM_CH08  8            /* Audio A */
357 #define SRAM_CH09  9            /* Video Upstream I */
358 #define SRAM_CH10  10           /* Video Upstream J */
359 #define SRAM_CH11  11           /* Audio Upstream AUD_CHANNEL_B */
360
361 #define VID_UPSTREAM_SRAM_CHANNEL_I     SRAM_CH09
362 #define VID_UPSTREAM_SRAM_CHANNEL_J     SRAM_CH10
363 #define AUDIO_UPSTREAM_SRAM_CHANNEL_B   SRAM_CH11
364
365 struct sram_channel {
366         char *name;
367         u32 i;
368         u32 cmds_start;
369         u32 ctrl_start;
370         u32 cdt;
371         u32 fifo_start;
372         u32 fifo_size;
373         u32 ptr1_reg;
374         u32 ptr2_reg;
375         u32 cnt1_reg;
376         u32 cnt2_reg;
377         u32 int_msk;
378         u32 int_stat;
379         u32 int_mstat;
380         u32 dma_ctl;
381         u32 gpcnt_ctl;
382         u32 gpcnt;
383         u32 aud_length;
384         u32 aud_cfg;
385         u32 fld_aud_fifo_en;
386         u32 fld_aud_risc_en;
387
388         /* For Upstream Video */
389         u32 vid_fmt_ctl;
390         u32 vid_active_ctl1;
391         u32 vid_active_ctl2;
392         u32 vid_cdt_size;
393
394         u32 vip_ctl;
395         u32 pix_frmt;
396         u32 jumponly;
397         u32 irq_bit;
398 };
399
400 extern const struct sram_channel cx25821_sram_channels[];
401
402 #define cx_read(reg)             readl(dev->lmmio + ((reg)>>2))
403 #define cx_write(reg, value)     writel((value), dev->lmmio + ((reg)>>2))
404
405 #define cx_andor(reg, mask, value) \
406         writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
407         ((value) & (mask)), dev->lmmio+((reg)>>2))
408
409 #define cx_set(reg, bit)          cx_andor((reg), (bit), (bit))
410 #define cx_clear(reg, bit)        cx_andor((reg), (bit), 0)
411
412 #define Set_GPIO_Bit(Bit)                       (1 << Bit)
413 #define Clear_GPIO_Bit(Bit)                     (~(1 << Bit))
414
415 #define CX25821_ERR(fmt, args...)                       \
416         pr_err("(%d): " fmt, dev->board, ##args)
417 #define CX25821_WARN(fmt, args...)                      \
418         pr_warn("(%d): " fmt, dev->board, ##args)
419 #define CX25821_INFO(fmt, args...)                      \
420         pr_info("(%d): " fmt, dev->board, ##args)
421
422 extern int cx25821_i2c_register(struct cx25821_i2c *bus);
423 extern int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value);
424 extern int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value);
425 extern int cx25821_i2c_unregister(struct cx25821_i2c *bus);
426 extern void cx25821_gpio_init(struct cx25821_dev *dev);
427 extern void cx25821_set_gpiopin_direction(struct cx25821_dev *dev,
428                                           int pin_number, int pin_logic_value);
429
430 extern int medusa_video_init(struct cx25821_dev *dev);
431 extern int medusa_set_videostandard(struct cx25821_dev *dev);
432 extern void medusa_set_resolution(struct cx25821_dev *dev, int width,
433                                   int decoder_select);
434 extern int medusa_set_brightness(struct cx25821_dev *dev, int brightness,
435                                  int decoder);
436 extern int medusa_set_contrast(struct cx25821_dev *dev, int contrast,
437                                int decoder);
438 extern int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder);
439 extern int medusa_set_saturation(struct cx25821_dev *dev, int saturation,
440                                  int decoder);
441
442 extern int cx25821_sram_channel_setup(struct cx25821_dev *dev,
443                                       const struct sram_channel *ch, unsigned int bpl,
444                                       u32 risc);
445
446 extern int cx25821_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
447                                struct scatterlist *sglist,
448                                unsigned int top_offset,
449                                unsigned int bottom_offset,
450                                unsigned int bpl,
451                                unsigned int padding, unsigned int lines);
452 extern int cx25821_risc_databuffer_audio(struct pci_dev *pci,
453                                          struct btcx_riscmem *risc,
454                                          struct scatterlist *sglist,
455                                          unsigned int bpl,
456                                          unsigned int lines, unsigned int lpi);
457 extern void cx25821_free_buffer(struct videobuf_queue *q,
458                                 struct cx25821_buffer *buf);
459 extern int cx25821_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
460                                 u32 reg, u32 mask, u32 value);
461 extern void cx25821_sram_channel_dump(struct cx25821_dev *dev,
462                                       const struct sram_channel *ch);
463 extern void cx25821_sram_channel_dump_audio(struct cx25821_dev *dev,
464                                             const struct sram_channel *ch);
465
466 extern struct cx25821_dev *cx25821_dev_get(struct pci_dev *pci);
467 extern void cx25821_print_irqbits(char *name, char *tag, char **strings,
468                                   int len, u32 bits, u32 mask);
469 extern void cx25821_dev_unregister(struct cx25821_dev *dev);
470 extern int cx25821_sram_channel_setup_audio(struct cx25821_dev *dev,
471                                             const struct sram_channel *ch,
472                                             unsigned int bpl, u32 risc);
473
474 extern int cx25821_vidupstream_init_ch1(struct cx25821_dev *dev,
475                                         int channel_select, int pixel_format);
476 extern int cx25821_vidupstream_init_ch2(struct cx25821_dev *dev,
477                                         int channel_select, int pixel_format);
478 extern int cx25821_audio_upstream_init(struct cx25821_dev *dev,
479                                        int channel_select);
480 extern void cx25821_free_mem_upstream_ch1(struct cx25821_dev *dev);
481 extern void cx25821_free_mem_upstream_ch2(struct cx25821_dev *dev);
482 extern void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev);
483 extern void cx25821_start_upstream_video_ch1(struct cx25821_dev *dev,
484                                              struct upstream_user_struct
485                                              *up_data);
486 extern void cx25821_start_upstream_video_ch2(struct cx25821_dev *dev,
487                                              struct upstream_user_struct
488                                              *up_data);
489 extern void cx25821_start_upstream_audio(struct cx25821_dev *dev,
490                                          struct upstream_user_struct *up_data);
491 extern void cx25821_stop_upstream_video_ch1(struct cx25821_dev *dev);
492 extern void cx25821_stop_upstream_video_ch2(struct cx25821_dev *dev);
493 extern void cx25821_stop_upstream_audio(struct cx25821_dev *dev);
494 extern int cx25821_sram_channel_setup_upstream(struct cx25821_dev *dev,
495                                                const struct sram_channel *ch,
496                                                unsigned int bpl, u32 risc);
497 extern void cx25821_set_pixel_format(struct cx25821_dev *dev, int channel,
498                                      u32 format);
499
500 #endif