[media] saa7164: Adding support for HVR2200 card id 0x8953
[linux-2.6/libata-dev.git] / drivers / media / video / saa7164 / saa7164.h
blob742b34103b5d8fb244fd779949c7b56deda52ec2
1 /*
2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 Driver architecture
24 *******************
26 saa7164_core.c/buffer.c/cards.c/i2c.c/dvb.c
27 | : Standard Linux driver framework for creating
28 | : exposing and managing interfaces to the rest
29 | : of the kernel or userland. Also uses _fw.c to load
30 | : firmware direct into the PCIe bus, bypassing layers.
32 saa7164_api..() : Translate kernel specific functions/features
33 | : into command buffers.
35 saa7164_cmd..() : Manages the flow of command packets on/off,
36 | : the bus. Deal with bus errors, timeouts etc.
38 saa7164_bus..() : Manage a read/write memory ring buffer in the
39 | : PCIe Address space.
41 | saa7164_fw...() : Load any frimware
42 | | : direct into the device
43 V V
44 <- ----------------- PCIe address space -------------------- ->
47 #include <linux/pci.h>
48 #include <linux/i2c.h>
49 #include <linux/i2c-algo-bit.h>
50 #include <linux/kdev_t.h>
51 #include <linux/mutex.h>
52 #include <linux/crc32.h>
53 #include <linux/kthread.h>
54 #include <linux/freezer.h>
56 #include <media/tuner.h>
57 #include <media/tveeprom.h>
58 #include <media/videobuf-dma-sg.h>
59 #include <media/videobuf-dvb.h>
60 #include <dvb_demux.h>
61 #include <dvb_frontend.h>
62 #include <dvb_net.h>
63 #include <dvbdev.h>
64 #include <dmxdev.h>
65 #include <media/v4l2-common.h>
66 #include <media/v4l2-ioctl.h>
67 #include <media/v4l2-chip-ident.h>
69 #include "saa7164-reg.h"
70 #include "saa7164-types.h"
72 #define SAA7164_MAXBOARDS 8
74 #define UNSET (-1U)
75 #define SAA7164_BOARD_NOAUTO UNSET
76 #define SAA7164_BOARD_UNKNOWN 0
77 #define SAA7164_BOARD_UNKNOWN_REV2 1
78 #define SAA7164_BOARD_UNKNOWN_REV3 2
79 #define SAA7164_BOARD_HAUPPAUGE_HVR2250 3
80 #define SAA7164_BOARD_HAUPPAUGE_HVR2200 4
81 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_2 5
82 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_3 6
83 #define SAA7164_BOARD_HAUPPAUGE_HVR2250_2 7
84 #define SAA7164_BOARD_HAUPPAUGE_HVR2250_3 8
85 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_4 9
86 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_5 10
88 #define SAA7164_MAX_UNITS 8
89 #define SAA7164_TS_NUMBER_OF_LINES 312
90 #define SAA7164_PS_NUMBER_OF_LINES 256
91 #define SAA7164_PT_ENTRIES 16 /* (312 * 188) / 4096 */
92 #define SAA7164_MAX_ENCODER_BUFFERS 64 /* max 5secs of latency at 6Mbps */
93 #define SAA7164_MAX_VBI_BUFFERS 64
95 /* Port related defines */
96 #define SAA7164_PORT_TS1 (0)
97 #define SAA7164_PORT_TS2 (SAA7164_PORT_TS1 + 1)
98 #define SAA7164_PORT_ENC1 (SAA7164_PORT_TS2 + 1)
99 #define SAA7164_PORT_ENC2 (SAA7164_PORT_ENC1 + 1)
100 #define SAA7164_PORT_VBI1 (SAA7164_PORT_ENC2 + 1)
101 #define SAA7164_PORT_VBI2 (SAA7164_PORT_VBI1 + 1)
102 #define SAA7164_MAX_PORTS (SAA7164_PORT_VBI2 + 1)
104 #define DBGLVL_FW 4
105 #define DBGLVL_DVB 8
106 #define DBGLVL_I2C 16
107 #define DBGLVL_API 32
108 #define DBGLVL_CMD 64
109 #define DBGLVL_BUS 128
110 #define DBGLVL_IRQ 256
111 #define DBGLVL_BUF 512
112 #define DBGLVL_ENC 1024
113 #define DBGLVL_VBI 2048
114 #define DBGLVL_THR 4096
115 #define DBGLVL_CPU 8192
117 #define SAA7164_NORMS \
118 (V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_443)
120 enum port_t {
121 SAA7164_MPEG_UNDEFINED = 0,
122 SAA7164_MPEG_DVB,
123 SAA7164_MPEG_ENCODER,
124 SAA7164_MPEG_VBI,
127 enum saa7164_i2c_bus_nr {
128 SAA7164_I2C_BUS_0 = 0,
129 SAA7164_I2C_BUS_1,
130 SAA7164_I2C_BUS_2,
133 enum saa7164_buffer_flags {
134 SAA7164_BUFFER_UNDEFINED = 0,
135 SAA7164_BUFFER_FREE,
136 SAA7164_BUFFER_BUSY,
137 SAA7164_BUFFER_FULL
140 enum saa7164_unit_type {
141 SAA7164_UNIT_UNDEFINED = 0,
142 SAA7164_UNIT_DIGITAL_DEMODULATOR,
143 SAA7164_UNIT_ANALOG_DEMODULATOR,
144 SAA7164_UNIT_TUNER,
145 SAA7164_UNIT_EEPROM,
146 SAA7164_UNIT_ZILOG_IRBLASTER,
147 SAA7164_UNIT_ENCODER,
150 /* The PCIe bridge doesn't grant direct access to i2c.
151 * Instead, you address i2c devices using a uniqely
152 * allocated 'unitid' value via a messaging API. This
153 * is a problem. The kernel and existing demod/tuner
154 * drivers expect to talk 'i2c', so we have to maintain
155 * a translation layer, and a series of functions to
156 * convert i2c bus + device address into a unit id.
158 struct saa7164_unit {
159 enum saa7164_unit_type type;
160 u8 id;
161 char *name;
162 enum saa7164_i2c_bus_nr i2c_bus_nr;
163 u8 i2c_bus_addr;
164 u8 i2c_reg_len;
167 struct saa7164_board {
168 char *name;
169 enum port_t porta, portb, portc,
170 portd, porte, portf;
171 enum {
172 SAA7164_CHIP_UNDEFINED = 0,
173 SAA7164_CHIP_REV2,
174 SAA7164_CHIP_REV3,
175 } chiprev;
176 struct saa7164_unit unit[SAA7164_MAX_UNITS];
179 struct saa7164_subid {
180 u16 subvendor;
181 u16 subdevice;
182 u32 card;
185 struct saa7164_encoder_fh {
186 struct saa7164_port *port;
187 atomic_t v4l_reading;
190 struct saa7164_vbi_fh {
191 struct saa7164_port *port;
192 atomic_t v4l_reading;
195 struct saa7164_histogram_bucket {
196 u32 val;
197 u32 count;
198 u64 update_time;
201 struct saa7164_histogram {
202 char name[32];
203 struct saa7164_histogram_bucket counter1[64];
206 struct saa7164_user_buffer {
207 struct list_head list;
209 /* Attributes */
210 u8 *data;
211 u32 pos;
212 u32 actual_size;
214 u32 crc;
217 struct saa7164_fw_status {
219 /* RISC Core details */
220 u32 status;
221 u32 mode;
222 u32 spec;
223 u32 inst;
224 u32 cpuload;
225 u32 remainheap;
227 /* Firmware version */
228 u32 version;
229 u32 major;
230 u32 sub;
231 u32 rel;
232 u32 buildnr;
235 struct saa7164_dvb {
236 struct mutex lock;
237 struct dvb_adapter adapter;
238 struct dvb_frontend *frontend;
239 struct dvb_demux demux;
240 struct dmxdev dmxdev;
241 struct dmx_frontend fe_hw;
242 struct dmx_frontend fe_mem;
243 struct dvb_net net;
244 int feeding;
247 struct saa7164_i2c {
248 struct saa7164_dev *dev;
250 enum saa7164_i2c_bus_nr nr;
252 /* I2C I/O */
253 struct i2c_adapter i2c_adap;
254 struct i2c_algo_bit_data i2c_algo;
255 struct i2c_client i2c_client;
256 u32 i2c_rc;
259 struct saa7164_ctrl {
260 struct v4l2_queryctrl v;
263 struct saa7164_tvnorm {
264 char *name;
265 v4l2_std_id id;
268 struct saa7164_encoder_params {
269 struct saa7164_tvnorm encodernorm;
270 u32 height;
271 u32 width;
272 u32 is_50hz;
273 u32 bitrate; /* bps */
274 u32 bitrate_peak; /* bps */
275 u32 bitrate_mode;
276 u32 stream_type; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
278 u32 audio_sampling_freq;
279 u32 ctl_mute;
280 u32 ctl_aspect;
281 u32 refdist;
282 u32 gop_size;
285 struct saa7164_vbi_params {
286 struct saa7164_tvnorm encodernorm;
287 u32 height;
288 u32 width;
289 u32 is_50hz;
290 u32 bitrate; /* bps */
291 u32 bitrate_peak; /* bps */
292 u32 bitrate_mode;
293 u32 stream_type; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
295 u32 audio_sampling_freq;
296 u32 ctl_mute;
297 u32 ctl_aspect;
298 u32 refdist;
299 u32 gop_size;
302 struct saa7164_port;
304 struct saa7164_buffer {
305 struct list_head list;
307 /* Note of which h/w buffer list index position we occupy */
308 int idx;
310 struct saa7164_port *port;
312 /* Hardware Specific */
313 /* PCI Memory allocations */
314 enum saa7164_buffer_flags flags; /* Free, Busy, Full */
316 /* A block of page align PCI memory */
317 u32 pci_size; /* PCI allocation size in bytes */
318 u64 __iomem *cpu; /* Virtual address */
319 dma_addr_t dma; /* Physical address */
320 u32 crc; /* Checksum for the entire buffer data */
322 /* A page table that splits the block into a number of entries */
323 u32 pt_size; /* PCI allocation size in bytes */
324 u64 __iomem *pt_cpu; /* Virtual address */
325 dma_addr_t pt_dma; /* Physical address */
327 /* Encoder fops */
328 u32 pos;
329 u32 actual_size;
332 struct saa7164_port {
334 struct saa7164_dev *dev;
335 enum port_t type;
336 int nr;
338 /* --- Generic port attributes --- */
340 /* HW stream parameters */
341 struct tmHWStreamParameters hw_streamingparams;
343 /* DMA configuration values, is seeded during initialization */
344 struct tmComResDMATermDescrHeader hwcfg;
346 /* hardware specific registers */
347 u32 bufcounter;
348 u32 pitch;
349 u32 bufsize;
350 u32 bufoffset;
351 u32 bufptr32l;
352 u32 bufptr32h;
353 u64 bufptr64;
355 u32 numpte; /* Number of entries in array, only valid in head */
357 struct mutex dmaqueue_lock;
358 struct saa7164_buffer dmaqueue;
360 u64 last_irq_msecs, last_svc_msecs;
361 u64 last_irq_msecs_diff, last_svc_msecs_diff;
362 u32 last_svc_wp;
363 u32 last_svc_rp;
364 u64 last_irq_svc_msecs_diff;
365 u64 last_read_msecs, last_read_msecs_diff;
366 u64 last_poll_msecs, last_poll_msecs_diff;
368 struct saa7164_histogram irq_interval;
369 struct saa7164_histogram svc_interval;
370 struct saa7164_histogram irq_svc_interval;
371 struct saa7164_histogram read_interval;
372 struct saa7164_histogram poll_interval;
374 /* --- DVB Transport Specific --- */
375 struct saa7164_dvb dvb;
377 /* --- Encoder/V4L related attributes --- */
378 /* Encoder */
379 /* Defaults established in saa7164-encoder.c */
380 struct saa7164_tvnorm encodernorm;
381 u32 height;
382 u32 width;
383 u32 freq;
384 u32 ts_packet_size;
385 u32 ts_packet_count;
386 u8 mux_input;
387 u8 encoder_profile;
388 u8 video_format;
389 u8 audio_format;
390 u8 video_resolution;
391 u16 ctl_brightness;
392 u16 ctl_contrast;
393 u16 ctl_hue;
394 u16 ctl_saturation;
395 u16 ctl_sharpness;
396 s8 ctl_volume;
398 struct tmComResAFeatureDescrHeader audfeat;
399 struct tmComResEncoderDescrHeader encunit;
400 struct tmComResProcDescrHeader vidproc;
401 struct tmComResExtDevDescrHeader ifunit;
402 struct tmComResTunerDescrHeader tunerunit;
404 struct work_struct workenc;
406 /* V4L Encoder Video */
407 struct saa7164_encoder_params encoder_params;
408 struct video_device *v4l_device;
409 atomic_t v4l_reader_count;
411 struct saa7164_buffer list_buf_used;
412 struct saa7164_buffer list_buf_free;
413 wait_queue_head_t wait_read;
415 /* V4L VBI */
416 struct tmComResVBIFormatDescrHeader vbi_fmt_ntsc;
417 struct saa7164_vbi_params vbi_params;
419 /* Debug */
420 u32 sync_errors;
421 u32 v_cc_errors;
422 u32 a_cc_errors;
423 u8 last_v_cc;
424 u8 last_a_cc;
425 u32 done_first_interrupt;
428 struct saa7164_dev {
429 struct list_head devlist;
430 atomic_t refcount;
432 /* pci stuff */
433 struct pci_dev *pci;
434 unsigned char pci_rev, pci_lat;
435 int pci_bus, pci_slot;
436 u32 __iomem *lmmio;
437 u8 __iomem *bmmio;
438 u32 __iomem *lmmio2;
439 u8 __iomem *bmmio2;
440 int pci_irqmask;
442 /* board details */
443 int nr;
444 int hwrevision;
445 u32 board;
446 char name[16];
448 /* firmware status */
449 struct saa7164_fw_status fw_status;
450 u32 firmwareloaded;
452 struct tmComResHWDescr hwdesc;
453 struct tmComResInterfaceDescr intfdesc;
454 struct tmComResBusDescr busdesc;
456 struct tmComResBusInfo bus;
458 /* Interrupt status and ack registers */
459 u32 int_status;
460 u32 int_ack;
462 struct cmd cmds[SAA_CMD_MAX_MSG_UNITS];
463 struct mutex lock;
465 /* I2c related */
466 struct saa7164_i2c i2c_bus[3];
468 /* Transport related */
469 struct saa7164_port ports[SAA7164_MAX_PORTS];
471 /* Deferred command/api interrupts handling */
472 struct work_struct workcmd;
474 /* A kernel thread to monitor the firmware log, used
475 * only in debug mode.
477 struct task_struct *kthread;
481 extern struct list_head saa7164_devlist;
482 extern unsigned int waitsecs;
483 extern unsigned int encoder_buffers;
484 extern unsigned int vbi_buffers;
486 /* ----------------------------------------------------------- */
487 /* saa7164-core.c */
488 void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr);
489 void saa7164_dumphex16(struct saa7164_dev *dev, u8 *buf, int len);
490 void saa7164_getfirmwarestatus(struct saa7164_dev *dev);
491 u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev);
492 void saa7164_histogram_update(struct saa7164_histogram *hg, u32 val);
494 /* ----------------------------------------------------------- */
495 /* saa7164-fw.c */
496 int saa7164_downloadfirmware(struct saa7164_dev *dev);
498 /* ----------------------------------------------------------- */
499 /* saa7164-i2c.c */
500 extern int saa7164_i2c_register(struct saa7164_i2c *bus);
501 extern int saa7164_i2c_unregister(struct saa7164_i2c *bus);
502 extern void saa7164_call_i2c_clients(struct saa7164_i2c *bus,
503 unsigned int cmd, void *arg);
505 /* ----------------------------------------------------------- */
506 /* saa7164-bus.c */
507 int saa7164_bus_setup(struct saa7164_dev *dev);
508 void saa7164_bus_dump(struct saa7164_dev *dev);
509 int saa7164_bus_set(struct saa7164_dev *dev, struct tmComResInfo* msg,
510 void *buf);
511 int saa7164_bus_get(struct saa7164_dev *dev, struct tmComResInfo* msg,
512 void *buf, int peekonly);
514 /* ----------------------------------------------------------- */
515 /* saa7164-cmd.c */
516 int saa7164_cmd_send(struct saa7164_dev *dev,
517 u8 id, enum tmComResCmd command, u16 controlselector,
518 u16 size, void *buf);
519 void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno);
520 int saa7164_irq_dequeue(struct saa7164_dev *dev);
522 /* ----------------------------------------------------------- */
523 /* saa7164-api.c */
524 int saa7164_api_get_fw_version(struct saa7164_dev *dev, u32 *version);
525 int saa7164_api_enum_subdevs(struct saa7164_dev *dev);
526 int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg,
527 u32 datalen, u8 *data);
528 int saa7164_api_i2c_write(struct saa7164_i2c *bus, u8 addr,
529 u32 datalen, u8 *data);
530 int saa7164_api_dif_write(struct saa7164_i2c *bus, u8 addr,
531 u32 datalen, u8 *data);
532 int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen);
533 int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin);
534 int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin);
535 int saa7164_api_transition_port(struct saa7164_port *port, u8 mode);
536 int saa7164_api_initialize_dif(struct saa7164_port *port);
537 int saa7164_api_configure_dif(struct saa7164_port *port, u32 std);
538 int saa7164_api_set_encoder(struct saa7164_port *port);
539 int saa7164_api_get_encoder(struct saa7164_port *port);
540 int saa7164_api_set_aspect_ratio(struct saa7164_port *port);
541 int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl);
542 int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl);
543 int saa7164_api_set_videomux(struct saa7164_port *port);
544 int saa7164_api_audio_mute(struct saa7164_port *port, int mute);
545 int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level);
546 int saa7164_api_set_audio_std(struct saa7164_port *port);
547 int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect);
548 int saa7164_api_get_videomux(struct saa7164_port *port);
549 int saa7164_api_set_vbi_format(struct saa7164_port *port);
550 int saa7164_api_set_debug(struct saa7164_dev *dev, u8 level);
551 int saa7164_api_collect_debug(struct saa7164_dev *dev);
552 int saa7164_api_get_load_info(struct saa7164_dev *dev,
553 struct tmFwInfoStruct *i);
555 /* ----------------------------------------------------------- */
556 /* saa7164-cards.c */
557 extern struct saa7164_board saa7164_boards[];
558 extern const unsigned int saa7164_bcount;
560 extern struct saa7164_subid saa7164_subids[];
561 extern const unsigned int saa7164_idcount;
563 extern void saa7164_card_list(struct saa7164_dev *dev);
564 extern void saa7164_gpio_setup(struct saa7164_dev *dev);
565 extern void saa7164_card_setup(struct saa7164_dev *dev);
567 extern int saa7164_i2caddr_to_reglen(struct saa7164_i2c *bus, int addr);
568 extern int saa7164_i2caddr_to_unitid(struct saa7164_i2c *bus, int addr);
569 extern char *saa7164_unitid_name(struct saa7164_dev *dev, u8 unitid);
571 /* ----------------------------------------------------------- */
572 /* saa7164-dvb.c */
573 extern int saa7164_dvb_register(struct saa7164_port *port);
574 extern int saa7164_dvb_unregister(struct saa7164_port *port);
576 /* ----------------------------------------------------------- */
577 /* saa7164-buffer.c */
578 extern struct saa7164_buffer *saa7164_buffer_alloc(
579 struct saa7164_port *port, u32 len);
580 extern int saa7164_buffer_dealloc(struct saa7164_buffer *buf);
581 extern void saa7164_buffer_display(struct saa7164_buffer *buf);
582 extern int saa7164_buffer_activate(struct saa7164_buffer *buf, int i);
583 extern int saa7164_buffer_cfg_port(struct saa7164_port *port);
584 extern struct saa7164_user_buffer *saa7164_buffer_alloc_user(
585 struct saa7164_dev *dev, u32 len);
586 extern void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf);
587 extern int saa7164_buffer_zero_offsets(struct saa7164_port *port, int i);
589 /* ----------------------------------------------------------- */
590 /* saa7164-encoder.c */
591 int saa7164_encoder_register(struct saa7164_port *port);
592 void saa7164_encoder_unregister(struct saa7164_port *port);
594 /* ----------------------------------------------------------- */
595 /* saa7164-vbi.c */
596 int saa7164_vbi_register(struct saa7164_port *port);
597 void saa7164_vbi_unregister(struct saa7164_port *port);
599 /* ----------------------------------------------------------- */
601 extern unsigned int crc_checking;
603 extern unsigned int saa_debug;
604 #define dprintk(level, fmt, arg...)\
605 do { if (saa_debug & level)\
606 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
607 } while (0)
609 #define log_warn(fmt, arg...)\
610 do { \
611 printk(KERN_WARNING "%s: " fmt, dev->name, ## arg);\
612 } while (0)
614 #define log_err(fmt, arg...)\
615 do { \
616 printk(KERN_ERROR "%s: " fmt, dev->name, ## arg);\
617 } while (0)
619 #define saa7164_readl(reg) readl(dev->lmmio + ((reg) >> 2))
620 #define saa7164_writel(reg, value) writel((value), dev->lmmio + ((reg) >> 2))
622 #define saa7164_readb(reg) readl(dev->bmmio + (reg))
623 #define saa7164_writeb(reg, value) writel((value), dev->bmmio + (reg))