Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/sage/ceph...
[linux-drm-fsl-dcu.git] / drivers / staging / 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/i2c-algo-bit.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include <linux/sched.h>
33 #include <linux/kdev_t.h>
34 #include <linux/smp_lock.h>
35
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-device.h>
38 #include <media/tuner.h>
39 #include <media/tveeprom.h>
40 #include <media/videobuf-dma-sg.h>
41 #include <media/videobuf-dvb.h>
42
43 #include "btcx-risc.h"
44 #include "cx25821-reg.h"
45 #include "cx25821-medusa-reg.h"
46 #include "cx25821-sram.h"
47 #include "cx25821-audio.h"
48 #include "media/cx2341x.h"
49
50 #include <linux/version.h>
51 #include <linux/mutex.h>
52
53 #define CX25821_VERSION_CODE KERNEL_VERSION(0, 0, 106)
54
55 #define UNSET (-1U)
56 #define NO_SYNC_LINE (-1U)
57
58 #define CX25821_MAXBOARDS 2
59
60 #define TRUE    1
61 #define FALSE   0
62 #define LINE_SIZE_D1    1440
63
64 // Number of decoders and encoders
65 #define MAX_DECODERS            8
66 #define MAX_ENCODERS            2
67 #define QUAD_DECODERS           4
68 #define MAX_CAMERAS             16
69
70 /* Max number of inputs by card */
71 #define MAX_CX25821_INPUT 8
72 #define INPUT(nr) (&cx25821_boards[dev->board].input[nr])
73 #define RESOURCE_VIDEO0       1
74 #define RESOURCE_VIDEO1       2
75 #define RESOURCE_VIDEO2       4
76 #define RESOURCE_VIDEO3       8
77 #define RESOURCE_VIDEO4       16
78 #define RESOURCE_VIDEO5       32
79 #define RESOURCE_VIDEO6       64
80 #define RESOURCE_VIDEO7       128
81 #define RESOURCE_VIDEO8       256
82 #define RESOURCE_VIDEO9       512
83 #define RESOURCE_VIDEO10      1024
84 #define RESOURCE_VIDEO11      2048
85 #define RESOURCE_VIDEO_IOCTL  4096
86
87 #define BUFFER_TIMEOUT     (HZ) /* 0.5 seconds */
88
89 #define UNKNOWN_BOARD       0
90 #define CX25821_BOARD        1
91
92 /* Currently supported by the driver */
93 #define CX25821_NORMS (\
94     V4L2_STD_NTSC_M |  V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_M_KR | \
95     V4L2_STD_PAL_BG |  V4L2_STD_PAL_DK    |  V4L2_STD_PAL_I    | \
96     V4L2_STD_PAL_M  |  V4L2_STD_PAL_N     |  V4L2_STD_PAL_H    | \
97     V4L2_STD_PAL_Nc )
98
99 #define CX25821_BOARD_CONEXANT_ATHENA10 1
100 #define MAX_VID_CHANNEL_NUM     12
101 #define VID_CHANNEL_NUM 8
102
103 struct cx25821_fmt {
104         char *name;
105         u32 fourcc;             /* v4l2 format id */
106         int depth;
107         int flags;
108         u32 cxformat;
109 };
110
111 struct cx25821_ctrl {
112         struct v4l2_queryctrl v;
113         u32 off;
114         u32 reg;
115         u32 mask;
116         u32 shift;
117 };
118
119 struct cx25821_tvnorm {
120         char *name;
121         v4l2_std_id id;
122         u32 cxiformat;
123         u32 cxoformat;
124 };
125
126 struct cx25821_fh {
127         struct cx25821_dev *dev;
128         enum v4l2_buf_type type;
129         int radio;
130         u32 resources;
131
132         enum v4l2_priority prio;
133
134         /* video overlay */
135         struct v4l2_window win;
136         struct v4l2_clip *clips;
137         unsigned int nclips;
138
139         /* video capture */
140         struct cx25821_fmt *fmt;
141         unsigned int width, height;
142
143         /* vbi capture */
144         struct videobuf_queue vidq;
145         struct videobuf_queue vbiq;
146
147         /* H264 Encoder specifics ONLY */
148         struct videobuf_queue mpegq;
149         atomic_t v4l_reading;
150 };
151
152 enum cx25821_itype {
153         CX25821_VMUX_COMPOSITE = 1,
154         CX25821_VMUX_SVIDEO,
155         CX25821_VMUX_DEBUG,
156         CX25821_RADIO,
157 };
158
159 enum cx25821_src_sel_type {
160         CX25821_SRC_SEL_EXT_656_VIDEO = 0,
161         CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
162 };
163
164 /* buffer for one video frame */
165 struct cx25821_buffer {
166         /* common v4l buffer stuff -- must be first */
167         struct videobuf_buffer vb;
168
169         /* cx25821 specific */
170         unsigned int bpl;
171         struct btcx_riscmem risc;
172         struct cx25821_fmt *fmt;
173         u32 count;
174 };
175
176 struct cx25821_input {
177         enum cx25821_itype type;
178         unsigned int vmux;
179         u32 gpio0, gpio1, gpio2, gpio3;
180 };
181
182 typedef enum {
183         CX25821_UNDEFINED = 0,
184         CX25821_RAW,
185         CX25821_264
186 } port_t;
187
188 struct cx25821_board {
189         char *name;
190         port_t porta, portb, portc;
191         unsigned int tuner_type;
192         unsigned int radio_type;
193         unsigned char tuner_addr;
194         unsigned char radio_addr;
195
196         u32 clk_freq;
197         struct cx25821_input input[2];
198 };
199
200 struct cx25821_subid {
201         u16 subvendor;
202         u16 subdevice;
203         u32 card;
204 };
205
206 struct cx25821_i2c {
207         struct cx25821_dev *dev;
208
209         int nr;
210
211         /* i2c i/o */
212         struct i2c_adapter i2c_adap;
213         struct i2c_algo_bit_data i2c_algo;
214         struct i2c_client i2c_client;
215         u32 i2c_rc;
216
217         /* cx25821 registers used for raw addess */
218         u32 i2c_period;
219         u32 reg_ctrl;
220         u32 reg_stat;
221         u32 reg_addr;
222         u32 reg_rdata;
223         u32 reg_wdata;
224 };
225
226 struct cx25821_dmaqueue {
227         struct list_head active;
228         struct list_head queued;
229         struct timer_list timeout;
230         struct btcx_riscmem stopper;
231         u32 count;
232 };
233
234 struct cx25821_data {
235         struct cx25821_dev *dev;
236         struct sram_channel *channel;
237 };
238
239 struct cx25821_dev {
240         struct list_head devlist;
241         atomic_t refcount;
242         struct v4l2_device v4l2_dev;
243
244         struct v4l2_prio_state prio;
245
246         /* pci stuff */
247         struct pci_dev *pci;
248         unsigned char pci_rev, pci_lat;
249         int pci_bus, pci_slot;
250         u32 base_io_addr;
251         u32 __iomem *lmmio;
252         u8 __iomem *bmmio;
253         int pci_irqmask;
254         int hwrevision;
255
256         u32 clk_freq;
257
258         /* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
259         struct cx25821_i2c i2c_bus[3];
260
261         int nr;
262         struct mutex lock;
263
264         /* board details */
265         unsigned int board;
266         char name[32];
267
268         /* sram configuration */
269         struct sram_channel *sram_channels;
270
271         /* Analog video */
272         u32 resources;
273         unsigned int input;
274         u32 tvaudio;
275         v4l2_std_id tvnorm;
276         unsigned int tuner_type;
277         unsigned char tuner_addr;
278         unsigned int radio_type;
279         unsigned char radio_addr;
280         unsigned int has_radio;
281         unsigned int videc_type;
282         unsigned char videc_addr;
283         unsigned short _max_num_decoders;
284
285         int ctl_bright;
286         int ctl_contrast;
287         int ctl_hue;
288         int ctl_saturation;
289
290         struct cx25821_data timeout_data[MAX_VID_CHANNEL_NUM];
291
292         /* Analog Audio Upstream */
293         int _audio_is_running;
294         int _audiopixel_format;
295         int _is_first_audio_frame;
296         int _audiofile_status;
297         int _audio_lines_count;
298         int _audioframe_count;
299         int _audio_upstream_channel_select;
300         int _last_index_irq;    //The last interrupt index processed.
301
302         __le32 *_risc_audio_jmp_addr;
303         __le32 *_risc_virt_start_addr;
304         __le32 *_risc_virt_addr;
305         dma_addr_t _risc_phys_addr;
306         dma_addr_t _risc_phys_start_addr;
307
308         unsigned int _audiorisc_size;
309         unsigned int _audiodata_buf_size;
310         __le32 *_audiodata_buf_virt_addr;
311         dma_addr_t _audiodata_buf_phys_addr;
312         char *_audiofilename;
313
314         /* V4l */
315         u32 freq;
316         struct video_device *video_dev[MAX_VID_CHANNEL_NUM];
317         struct video_device *vbi_dev;
318         struct video_device *radio_dev;
319         struct video_device *ioctl_dev;
320
321         struct cx25821_dmaqueue vidq[MAX_VID_CHANNEL_NUM];
322         spinlock_t slock;
323
324         /* Video Upstream */
325         int _line_size;
326         int _prog_cnt;
327         int _pixel_format;
328         int _is_first_frame;
329         int _is_running;
330         int _file_status;
331         int _lines_count;
332         int _frame_count;
333         int _channel_upstream_select;
334         unsigned int _risc_size;
335
336         __le32 *_dma_virt_start_addr;
337         __le32 *_dma_virt_addr;
338         dma_addr_t _dma_phys_addr;
339         dma_addr_t _dma_phys_start_addr;
340
341         unsigned int _data_buf_size;
342         __le32 *_data_buf_virt_addr;
343         dma_addr_t _data_buf_phys_addr;
344         char *_filename;
345         char *_defaultname;
346
347         int _line_size_ch2;
348         int _prog_cnt_ch2;
349         int _pixel_format_ch2;
350         int _is_first_frame_ch2;
351         int _is_running_ch2;
352         int _file_status_ch2;
353         int _lines_count_ch2;
354         int _frame_count_ch2;
355         int _channel2_upstream_select;
356         unsigned int _risc_size_ch2;
357
358         __le32 *_dma_virt_start_addr_ch2;
359         __le32 *_dma_virt_addr_ch2;
360         dma_addr_t _dma_phys_addr_ch2;
361         dma_addr_t _dma_phys_start_addr_ch2;
362
363         unsigned int _data_buf_size_ch2;
364         __le32 *_data_buf_virt_addr_ch2;
365         dma_addr_t _data_buf_phys_addr_ch2;
366         char *_filename_ch2;
367         char *_defaultname_ch2;
368
369         /* MPEG Encoder ONLY settings */
370         u32 cx23417_mailbox;
371         struct cx2341x_mpeg_params mpeg_params;
372         struct video_device *v4l_device;
373         atomic_t v4l_reader_count;
374         struct cx25821_tvnorm encodernorm;
375
376         u32 upstream_riscbuf_size;
377         u32 upstream_databuf_size;
378         u32 upstream_riscbuf_size_ch2;
379         u32 upstream_databuf_size_ch2;
380         u32 audio_upstream_riscbuf_size;
381         u32 audio_upstream_databuf_size;
382         int _isNTSC;
383         int _frame_index;
384         int _audioframe_index;
385         struct workqueue_struct *_irq_queues;
386         struct work_struct _irq_work_entry;
387         struct workqueue_struct *_irq_queues_ch2;
388         struct work_struct _irq_work_entry_ch2;
389         struct workqueue_struct *_irq_audio_queues;
390         struct work_struct _audio_work_entry;
391         char *input_filename;
392         char *input_filename_ch2;
393         int _frame_index_ch2;
394         int _isNTSC_ch2;
395         char *vid_stdname_ch2;
396         int pixel_format_ch2;
397         int channel_select_ch2;
398         int command_ch2;
399         char *input_audiofilename;
400         char *vid_stdname;
401         int pixel_format;
402         int channel_select;
403         int command;
404         int pixel_formats[VID_CHANNEL_NUM];
405         int use_cif_resolution[VID_CHANNEL_NUM];
406         int cif_width[VID_CHANNEL_NUM];
407         int channel_opened;
408 };
409
410 struct upstream_user_struct {
411         char *input_filename;
412         char *vid_stdname;
413         int pixel_format;
414         int channel_select;
415         int command;
416 };
417
418 struct downstream_user_struct {
419         char *vid_stdname;
420         int pixel_format;
421         int cif_resolution_enable;
422         int cif_width;
423         int decoder_select;
424         int command;
425         int reg_address;
426         int reg_data;
427 };
428
429 extern struct upstream_user_struct *up_data;
430
431 static inline struct cx25821_dev *get_cx25821(struct v4l2_device *v4l2_dev)
432 {
433         return container_of(v4l2_dev, struct cx25821_dev, v4l2_dev);
434 }
435
436 #define cx25821_call_all(dev, o, f, args...) \
437     v4l2_device_call_all(&dev->v4l2_dev, 0, o, f, ##args)
438
439 extern struct list_head cx25821_devlist;
440 extern struct cx25821_board cx25821_boards[];
441 extern struct cx25821_subid cx25821_subids[];
442
443 #define SRAM_CH00  0            /* Video A */
444 #define SRAM_CH01  1            /* Video B */
445 #define SRAM_CH02  2            /* Video C */
446 #define SRAM_CH03  3            /* Video D */
447 #define SRAM_CH04  4            /* Video E */
448 #define SRAM_CH05  5            /* Video F */
449 #define SRAM_CH06  6            /* Video G */
450 #define SRAM_CH07  7            /* Video H */
451
452 #define SRAM_CH08  8            /* Audio A */
453 #define SRAM_CH09  9            /* Video Upstream I */
454 #define SRAM_CH10  10           /* Video Upstream J */
455 #define SRAM_CH11  11           /* Audio Upstream AUD_CHANNEL_B */
456
457 #define VID_UPSTREAM_SRAM_CHANNEL_I     SRAM_CH09
458 #define VID_UPSTREAM_SRAM_CHANNEL_J     SRAM_CH10
459 #define AUDIO_UPSTREAM_SRAM_CHANNEL_B   SRAM_CH11
460 #define VIDEO_IOCTL_CH  11
461
462 struct sram_channel {
463         char *name;
464         u32 i;
465         u32 cmds_start;
466         u32 ctrl_start;
467         u32 cdt;
468         u32 fifo_start;
469         u32 fifo_size;
470         u32 ptr1_reg;
471         u32 ptr2_reg;
472         u32 cnt1_reg;
473         u32 cnt2_reg;
474         u32 int_msk;
475         u32 int_stat;
476         u32 int_mstat;
477         u32 dma_ctl;
478         u32 gpcnt_ctl;
479         u32 gpcnt;
480         u32 aud_length;
481         u32 aud_cfg;
482         u32 fld_aud_fifo_en;
483         u32 fld_aud_risc_en;
484
485         //For Upstream Video
486         u32 vid_fmt_ctl;
487         u32 vid_active_ctl1;
488         u32 vid_active_ctl2;
489         u32 vid_cdt_size;
490
491         u32 vip_ctl;
492         u32 pix_frmt;
493         u32 jumponly;
494         u32 irq_bit;
495 };
496 extern struct sram_channel cx25821_sram_channels[];
497
498 #define STATUS_SUCCESS         0
499 #define STATUS_UNSUCCESSFUL    -1
500
501 #define cx_read(reg)             readl(dev->lmmio + ((reg)>>2))
502 #define cx_write(reg, value)     writel((value), dev->lmmio + ((reg)>>2))
503
504 #define cx_andor(reg, mask, value) \
505   writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
506   ((value) & (mask)), dev->lmmio+((reg)>>2))
507
508 #define cx_set(reg, bit)          cx_andor((reg), (bit), (bit))
509 #define cx_clear(reg, bit)        cx_andor((reg), (bit), 0)
510
511 #define Set_GPIO_Bit(Bit)                       (1 << Bit)
512 #define Clear_GPIO_Bit(Bit)                     (~(1 << Bit))
513
514 #define CX25821_ERR(fmt, args...)      printk(KERN_ERR  "cx25821(%d): " fmt, dev->board, ## args)
515 #define CX25821_WARN(fmt, args...)     printk(KERN_WARNING "cx25821(%d): " fmt, dev->board , ## args)
516 #define CX25821_INFO(fmt, args...)     printk(KERN_INFO "cx25821(%d): " fmt, dev->board , ## args)
517
518 extern int cx25821_i2c_register(struct cx25821_i2c *bus);
519 extern void cx25821_card_setup(struct cx25821_dev *dev);
520 extern int cx25821_ir_init(struct cx25821_dev *dev);
521 extern int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value);
522 extern int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value);
523 extern int cx25821_i2c_unregister(struct cx25821_i2c *bus);
524 extern void cx25821_gpio_init(struct cx25821_dev *dev);
525 extern void cx25821_set_gpiopin_direction(struct cx25821_dev *dev,
526                                           int pin_number, int pin_logic_value);
527
528 extern int medusa_video_init(struct cx25821_dev *dev);
529 extern int medusa_set_videostandard(struct cx25821_dev *dev);
530 extern void medusa_set_resolution(struct cx25821_dev *dev, int width,
531                                   int decoder_select);
532 extern int medusa_set_brightness(struct cx25821_dev *dev, int brightness,
533                                  int decoder);
534 extern int medusa_set_contrast(struct cx25821_dev *dev, int contrast,
535                                int decoder);
536 extern int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder);
537 extern int medusa_set_saturation(struct cx25821_dev *dev, int saturation,
538                                  int decoder);
539
540 extern int cx25821_sram_channel_setup(struct cx25821_dev *dev,
541                                       struct sram_channel *ch, unsigned int bpl,
542                                       u32 risc);
543
544 extern int cx25821_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
545                                struct scatterlist *sglist,
546                                unsigned int top_offset,
547                                unsigned int bottom_offset,
548                                unsigned int bpl,
549                                unsigned int padding, unsigned int lines);
550 extern int cx25821_risc_databuffer_audio(struct pci_dev *pci,
551                                          struct btcx_riscmem *risc,
552                                          struct scatterlist *sglist,
553                                          unsigned int bpl,
554                                          unsigned int lines, unsigned int lpi);
555 extern void cx25821_free_buffer(struct videobuf_queue *q,
556                                 struct cx25821_buffer *buf);
557 extern int cx25821_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
558                                 u32 reg, u32 mask, u32 value);
559 extern void cx25821_sram_channel_dump(struct cx25821_dev *dev,
560                                       struct sram_channel *ch);
561 extern void cx25821_sram_channel_dump_audio(struct cx25821_dev *dev,
562                                             struct sram_channel *ch);
563
564 extern struct cx25821_dev *cx25821_dev_get(struct pci_dev *pci);
565 extern void cx25821_print_irqbits(char *name, char *tag, char **strings,
566                                   int len, u32 bits, u32 mask);
567 extern void cx25821_dev_unregister(struct cx25821_dev *dev);
568 extern int cx25821_sram_channel_setup_audio(struct cx25821_dev *dev,
569                                             struct sram_channel *ch,
570                                             unsigned int bpl, u32 risc);
571
572 extern int cx25821_vidupstream_init_ch1(struct cx25821_dev *dev,
573                                         int channel_select, int pixel_format);
574 extern int cx25821_vidupstream_init_ch2(struct cx25821_dev *dev,
575                                         int channel_select, int pixel_format);
576 extern int cx25821_audio_upstream_init(struct cx25821_dev *dev,
577                                        int channel_select);
578 extern void cx25821_free_mem_upstream_ch1(struct cx25821_dev *dev);
579 extern void cx25821_free_mem_upstream_ch2(struct cx25821_dev *dev);
580 extern void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev);
581 extern void cx25821_start_upstream_video_ch1(struct cx25821_dev *dev,
582                                              struct upstream_user_struct
583                                              *up_data);
584 extern void cx25821_start_upstream_video_ch2(struct cx25821_dev *dev,
585                                              struct upstream_user_struct
586                                              *up_data);
587 extern void cx25821_start_upstream_audio(struct cx25821_dev *dev,
588                                          struct upstream_user_struct *up_data);
589 extern void cx25821_stop_upstream_video_ch1(struct cx25821_dev *dev);
590 extern void cx25821_stop_upstream_video_ch2(struct cx25821_dev *dev);
591 extern void cx25821_stop_upstream_audio(struct cx25821_dev *dev);
592 extern int cx25821_sram_channel_setup_upstream(struct cx25821_dev *dev,
593                                                struct sram_channel *ch,
594                                                unsigned int bpl, u32 risc);
595 extern void cx25821_set_pixel_format(struct cx25821_dev *dev, int channel,
596                                      u32 format);
597 extern void cx25821_videoioctl_unregister(struct cx25821_dev *dev);
598 extern struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
599                                               struct pci_dev *pci,
600                                               struct video_device *template,
601                                               char *type);
602 #endif