Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / pci / saa7164 / saa7164.h
blobf3358f43195f9df0ac0bf093ebf0221d4a7a8394
1 /*
2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010-2015 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.
19 Driver architecture
20 *******************
22 saa7164_core.c/buffer.c/cards.c/i2c.c/dvb.c
23 | : Standard Linux driver framework for creating
24 | : exposing and managing interfaces to the rest
25 | : of the kernel or userland. Also uses _fw.c to load
26 | : firmware direct into the PCIe bus, bypassing layers.
28 saa7164_api..() : Translate kernel specific functions/features
29 | : into command buffers.
31 saa7164_cmd..() : Manages the flow of command packets on/off,
32 | : the bus. Deal with bus errors, timeouts etc.
34 saa7164_bus..() : Manage a read/write memory ring buffer in the
35 | : PCIe Address space.
37 | saa7164_fw...() : Load any frimware
38 | | : direct into the device
39 V V
40 <- ----------------- PCIe address space -------------------- ->
43 #include <linux/pci.h>
44 #include <linux/i2c.h>
45 #include <linux/kdev_t.h>
46 #include <linux/mutex.h>
47 #include <linux/crc32.h>
48 #include <linux/kthread.h>
49 #include <linux/freezer.h>
51 #include <media/tuner.h>
52 #include <media/tveeprom.h>
53 #include <media/dvb_demux.h>
54 #include <media/dvb_frontend.h>
55 #include <media/dvb_net.h>
56 #include <media/dvbdev.h>
57 #include <media/dmxdev.h>
58 #include <media/v4l2-common.h>
59 #include <media/v4l2-ioctl.h>
60 #include <media/v4l2-device.h>
61 #include <media/v4l2-ctrls.h>
62 #include <media/v4l2-event.h>
64 #include "saa7164-reg.h"
65 #include "saa7164-types.h"
67 #define SAA7164_MAXBOARDS 8
69 #define UNSET (-1U)
70 #define SAA7164_BOARD_NOAUTO UNSET
71 #define SAA7164_BOARD_UNKNOWN 0
72 #define SAA7164_BOARD_UNKNOWN_REV2 1
73 #define SAA7164_BOARD_UNKNOWN_REV3 2
74 #define SAA7164_BOARD_HAUPPAUGE_HVR2250 3
75 #define SAA7164_BOARD_HAUPPAUGE_HVR2200 4
76 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_2 5
77 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_3 6
78 #define SAA7164_BOARD_HAUPPAUGE_HVR2250_2 7
79 #define SAA7164_BOARD_HAUPPAUGE_HVR2250_3 8
80 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_4 9
81 #define SAA7164_BOARD_HAUPPAUGE_HVR2200_5 10
82 #define SAA7164_BOARD_HAUPPAUGE_HVR2255proto 11
83 #define SAA7164_BOARD_HAUPPAUGE_HVR2255 12
84 #define SAA7164_BOARD_HAUPPAUGE_HVR2205 13
86 #define SAA7164_MAX_UNITS 8
87 #define SAA7164_TS_NUMBER_OF_LINES 312
88 #define SAA7164_PS_NUMBER_OF_LINES 256
89 #define SAA7164_PT_ENTRIES 16 /* (312 * 188) / 4096 */
90 #define SAA7164_MAX_ENCODER_BUFFERS 64 /* max 5secs of latency at 6Mbps */
91 #define SAA7164_MAX_VBI_BUFFERS 64
93 /* Port related defines */
94 #define SAA7164_PORT_TS1 (0)
95 #define SAA7164_PORT_TS2 (SAA7164_PORT_TS1 + 1)
96 #define SAA7164_PORT_ENC1 (SAA7164_PORT_TS2 + 1)
97 #define SAA7164_PORT_ENC2 (SAA7164_PORT_ENC1 + 1)
98 #define SAA7164_PORT_VBI1 (SAA7164_PORT_ENC2 + 1)
99 #define SAA7164_PORT_VBI2 (SAA7164_PORT_VBI1 + 1)
100 #define SAA7164_MAX_PORTS (SAA7164_PORT_VBI2 + 1)
102 #define DBGLVL_FW 4
103 #define DBGLVL_DVB 8
104 #define DBGLVL_I2C 16
105 #define DBGLVL_API 32
106 #define DBGLVL_CMD 64
107 #define DBGLVL_BUS 128
108 #define DBGLVL_IRQ 256
109 #define DBGLVL_BUF 512
110 #define DBGLVL_ENC 1024
111 #define DBGLVL_VBI 2048
112 #define DBGLVL_THR 4096
113 #define DBGLVL_CPU 8192
115 #define SAA7164_NORMS \
116 (V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP)
118 /* TV frequency range copied from tuner-core.c */
119 #define SAA7164_TV_MIN_FREQ (44U * 16U)
120 #define SAA7164_TV_MAX_FREQ (958U * 16U)
122 enum port_t {
123 SAA7164_MPEG_UNDEFINED = 0,
124 SAA7164_MPEG_DVB,
125 SAA7164_MPEG_ENCODER,
126 SAA7164_MPEG_VBI,
129 enum saa7164_i2c_bus_nr {
130 SAA7164_I2C_BUS_0 = 0,
131 SAA7164_I2C_BUS_1,
132 SAA7164_I2C_BUS_2,
135 enum saa7164_buffer_flags {
136 SAA7164_BUFFER_UNDEFINED = 0,
137 SAA7164_BUFFER_FREE,
138 SAA7164_BUFFER_BUSY,
139 SAA7164_BUFFER_FULL
142 enum saa7164_unit_type {
143 SAA7164_UNIT_UNDEFINED = 0,
144 SAA7164_UNIT_DIGITAL_DEMODULATOR,
145 SAA7164_UNIT_ANALOG_DEMODULATOR,
146 SAA7164_UNIT_TUNER,
147 SAA7164_UNIT_EEPROM,
148 SAA7164_UNIT_ZILOG_IRBLASTER,
149 SAA7164_UNIT_ENCODER,
152 /* The PCIe bridge doesn't grant direct access to i2c.
153 * Instead, you address i2c devices using a uniqely
154 * allocated 'unitid' value via a messaging API. This
155 * is a problem. The kernel and existing demod/tuner
156 * drivers expect to talk 'i2c', so we have to maintain
157 * a translation layer, and a series of functions to
158 * convert i2c bus + device address into a unit id.
160 struct saa7164_unit {
161 enum saa7164_unit_type type;
162 u8 id;
163 char *name;
164 enum saa7164_i2c_bus_nr i2c_bus_nr;
165 u8 i2c_bus_addr;
166 u8 i2c_reg_len;
169 struct saa7164_board {
170 char *name;
171 enum port_t porta, portb, portc,
172 portd, porte, portf;
173 enum {
174 SAA7164_CHIP_UNDEFINED = 0,
175 SAA7164_CHIP_REV2,
176 SAA7164_CHIP_REV3,
177 } chiprev;
178 struct saa7164_unit unit[SAA7164_MAX_UNITS];
181 struct saa7164_subid {
182 u16 subvendor;
183 u16 subdevice;
184 u32 card;
187 struct saa7164_encoder_fh {
188 struct v4l2_fh fh;
189 struct saa7164_port *port;
190 atomic_t v4l_reading;
193 struct saa7164_vbi_fh {
194 struct v4l2_fh fh;
195 struct saa7164_port *port;
196 atomic_t v4l_reading;
199 struct saa7164_histogram_bucket {
200 u32 val;
201 u32 count;
202 u64 update_time;
205 struct saa7164_histogram {
206 char name[32];
207 struct saa7164_histogram_bucket counter1[64];
210 struct saa7164_user_buffer {
211 struct list_head list;
213 /* Attributes */
214 u8 *data;
215 u32 pos;
216 u32 actual_size;
218 u32 crc;
221 struct saa7164_fw_status {
223 /* RISC Core details */
224 u32 status;
225 u32 mode;
226 u32 spec;
227 u32 inst;
228 u32 cpuload;
229 u32 remainheap;
231 /* Firmware version */
232 u32 version;
233 u32 major;
234 u32 sub;
235 u32 rel;
236 u32 buildnr;
239 struct saa7164_dvb {
240 struct mutex lock;
241 struct dvb_adapter adapter;
242 struct dvb_frontend *frontend;
243 struct dvb_demux demux;
244 struct dmxdev dmxdev;
245 struct dmx_frontend fe_hw;
246 struct dmx_frontend fe_mem;
247 struct dvb_net net;
248 int feeding;
251 struct saa7164_i2c {
252 struct saa7164_dev *dev;
254 enum saa7164_i2c_bus_nr nr;
256 /* I2C I/O */
257 struct i2c_adapter i2c_adap;
258 struct i2c_client i2c_client;
259 u32 i2c_rc;
262 struct saa7164_tvnorm {
263 char *name;
264 v4l2_std_id id;
267 struct saa7164_encoder_params {
268 struct saa7164_tvnorm encodernorm;
269 u32 height;
270 u32 width;
271 u32 is_50hz;
272 u32 bitrate; /* bps */
273 u32 bitrate_peak; /* bps */
274 u32 bitrate_mode;
275 u32 stream_type; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
277 u32 audio_sampling_freq;
278 u32 ctl_mute;
279 u32 ctl_aspect;
280 u32 refdist;
281 u32 gop_size;
284 struct saa7164_vbi_params {
285 struct saa7164_tvnorm encodernorm;
286 u32 height;
287 u32 width;
288 u32 is_50hz;
289 u32 bitrate; /* bps */
290 u32 bitrate_peak; /* bps */
291 u32 bitrate_mode;
292 u32 stream_type; /* V4L2_MPEG_STREAM_TYPE_MPEG2_TS */
294 u32 audio_sampling_freq;
295 u32 ctl_mute;
296 u32 ctl_aspect;
297 u32 refdist;
298 u32 gop_size;
301 struct saa7164_port;
303 struct saa7164_buffer {
304 struct list_head list;
306 /* Note of which h/w buffer list index position we occupy */
307 int idx;
309 struct saa7164_port *port;
311 /* Hardware Specific */
312 /* PCI Memory allocations */
313 enum saa7164_buffer_flags flags; /* Free, Busy, Full */
315 /* A block of page align PCI memory */
316 u32 pci_size; /* PCI allocation size in bytes */
317 u64 *cpu; /* Virtual address */
318 dma_addr_t dma; /* Physical address */
319 u32 crc; /* Checksum for the entire buffer data */
321 /* A page table that splits the block into a number of entries */
322 u32 pt_size; /* PCI allocation size in bytes */
323 u64 *pt_cpu; /* Virtual address */
324 dma_addr_t pt_dma; /* Physical address */
326 /* Encoder fops */
327 u32 pos;
328 u32 actual_size;
331 struct saa7164_port {
333 struct saa7164_dev *dev;
334 enum port_t type;
335 int nr;
337 /* --- Generic port attributes --- */
339 /* HW stream parameters */
340 struct tmHWStreamParameters hw_streamingparams;
342 /* DMA configuration values, is seeded during initialization */
343 struct tmComResDMATermDescrHeader hwcfg;
345 /* hardware specific registers */
346 u32 bufcounter;
347 u32 pitch;
348 u32 bufsize;
349 u32 bufoffset;
350 u32 bufptr32l;
351 u32 bufptr32h;
352 u64 bufptr64;
354 u32 numpte; /* Number of entries in array, only valid in head */
356 struct mutex dmaqueue_lock;
357 struct saa7164_buffer dmaqueue;
359 u64 last_irq_msecs, last_svc_msecs;
360 u64 last_irq_msecs_diff, last_svc_msecs_diff;
361 u32 last_svc_wp;
362 u32 last_svc_rp;
363 u64 last_irq_svc_msecs_diff;
364 u64 last_read_msecs, last_read_msecs_diff;
365 u64 last_poll_msecs, last_poll_msecs_diff;
367 struct saa7164_histogram irq_interval;
368 struct saa7164_histogram svc_interval;
369 struct saa7164_histogram irq_svc_interval;
370 struct saa7164_histogram read_interval;
371 struct saa7164_histogram poll_interval;
373 /* --- DVB Transport Specific --- */
374 struct saa7164_dvb dvb;
375 struct i2c_client *i2c_client_demod;
376 struct i2c_client *i2c_client_tuner;
378 /* --- Encoder/V4L related attributes --- */
379 /* Encoder */
380 /* Defaults established in saa7164-encoder.c */
381 struct saa7164_tvnorm encodernorm;
382 struct v4l2_ctrl_handler ctrl_handler;
383 v4l2_std_id std;
384 u32 height;
385 u32 width;
386 u32 freq;
387 u8 mux_input;
388 u8 encoder_profile;
389 u8 video_format;
390 u8 audio_format;
391 u8 video_resolution;
392 u16 ctl_brightness;
393 u16 ctl_contrast;
394 u16 ctl_hue;
395 u16 ctl_saturation;
396 u16 ctl_sharpness;
397 s8 ctl_volume;
399 struct tmComResAFeatureDescrHeader audfeat;
400 struct tmComResEncoderDescrHeader encunit;
401 struct tmComResProcDescrHeader vidproc;
402 struct tmComResExtDevDescrHeader ifunit;
403 struct tmComResTunerDescrHeader tunerunit;
405 struct work_struct workenc;
407 /* V4L Encoder Video */
408 struct saa7164_encoder_params encoder_params;
409 struct video_device *v4l_device;
410 atomic_t v4l_reader_count;
412 struct saa7164_buffer list_buf_used;
413 struct saa7164_buffer list_buf_free;
414 wait_queue_head_t wait_read;
416 /* V4L VBI */
417 struct tmComResVBIFormatDescrHeader vbi_fmt_ntsc;
418 struct saa7164_vbi_params vbi_params;
419 struct saa7164_port *enc_port;
421 /* Debug */
422 u32 sync_errors;
423 u32 v_cc_errors;
424 u32 a_cc_errors;
425 u8 last_v_cc;
426 u8 last_a_cc;
427 u32 done_first_interrupt;
430 struct saa7164_dev {
431 struct list_head devlist;
432 atomic_t refcount;
434 struct v4l2_device v4l2_dev;
436 /* pci stuff */
437 struct pci_dev *pci;
438 unsigned char pci_rev, pci_lat;
439 int pci_bus, pci_slot;
440 u32 __iomem *lmmio;
441 u8 __iomem *bmmio;
442 u32 __iomem *lmmio2;
443 u8 __iomem *bmmio2;
444 int pci_irqmask;
446 /* board details */
447 int nr;
448 int hwrevision;
449 u32 board;
450 char name[16];
452 /* firmware status */
453 struct saa7164_fw_status fw_status;
454 u32 firmwareloaded;
456 struct tmComResHWDescr hwdesc;
457 struct tmComResInterfaceDescr intfdesc;
458 struct tmComResBusDescr busdesc;
460 struct tmComResBusInfo bus;
462 /* Interrupt status and ack registers */
463 u32 int_status;
464 u32 int_ack;
465 bool msi;
467 struct cmd cmds[SAA_CMD_MAX_MSG_UNITS];
468 struct mutex lock;
470 /* I2c related */
471 struct saa7164_i2c i2c_bus[3];
473 /* Transport related */
474 struct saa7164_port ports[SAA7164_MAX_PORTS];
476 /* Deferred command/api interrupts handling */
477 struct work_struct workcmd;
479 /* A kernel thread to monitor the firmware log, used
480 * only in debug mode.
482 struct task_struct *kthread;
486 extern struct list_head saa7164_devlist;
487 extern unsigned int waitsecs;
488 extern unsigned int encoder_buffers;
489 extern unsigned int vbi_buffers;
491 /* ----------------------------------------------------------- */
492 /* saa7164-core.c */
493 void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr);
494 void saa7164_getfirmwarestatus(struct saa7164_dev *dev);
495 u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev);
496 void saa7164_histogram_update(struct saa7164_histogram *hg, u32 val);
498 /* ----------------------------------------------------------- */
499 /* saa7164-fw.c */
500 int saa7164_downloadfirmware(struct saa7164_dev *dev);
502 /* ----------------------------------------------------------- */
503 /* saa7164-i2c.c */
504 extern int saa7164_i2c_register(struct saa7164_i2c *bus);
505 extern int saa7164_i2c_unregister(struct saa7164_i2c *bus);
506 extern void saa7164_call_i2c_clients(struct saa7164_i2c *bus,
507 unsigned int cmd, void *arg);
509 /* ----------------------------------------------------------- */
510 /* saa7164-bus.c */
511 int saa7164_bus_setup(struct saa7164_dev *dev);
512 void saa7164_bus_dump(struct saa7164_dev *dev);
513 int saa7164_bus_set(struct saa7164_dev *dev, struct tmComResInfo* msg,
514 void *buf);
515 int saa7164_bus_get(struct saa7164_dev *dev, struct tmComResInfo* msg,
516 void *buf, int peekonly);
518 /* ----------------------------------------------------------- */
519 /* saa7164-cmd.c */
520 int saa7164_cmd_send(struct saa7164_dev *dev,
521 u8 id, enum tmComResCmd command, u16 controlselector,
522 u16 size, void *buf);
523 void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno);
524 int saa7164_irq_dequeue(struct saa7164_dev *dev);
526 /* ----------------------------------------------------------- */
527 /* saa7164-api.c */
528 int saa7164_api_get_fw_version(struct saa7164_dev *dev, u32 *version);
529 int saa7164_api_enum_subdevs(struct saa7164_dev *dev);
530 int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg,
531 u32 datalen, u8 *data);
532 int saa7164_api_i2c_write(struct saa7164_i2c *bus, u8 addr,
533 u32 datalen, u8 *data);
534 int saa7164_api_dif_write(struct saa7164_i2c *bus, u8 addr,
535 u32 datalen, u8 *data);
536 int saa7164_api_read_eeprom(struct saa7164_dev *dev, u8 *buf, int buflen);
537 int saa7164_api_set_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin);
538 int saa7164_api_clear_gpiobit(struct saa7164_dev *dev, u8 unitid, u8 pin);
539 int saa7164_api_transition_port(struct saa7164_port *port, u8 mode);
540 int saa7164_api_initialize_dif(struct saa7164_port *port);
541 int saa7164_api_configure_dif(struct saa7164_port *port, u32 std);
542 int saa7164_api_set_encoder(struct saa7164_port *port);
543 int saa7164_api_get_encoder(struct saa7164_port *port);
544 int saa7164_api_set_aspect_ratio(struct saa7164_port *port);
545 int saa7164_api_set_usercontrol(struct saa7164_port *port, u8 ctl);
546 int saa7164_api_get_usercontrol(struct saa7164_port *port, u8 ctl);
547 int saa7164_api_set_videomux(struct saa7164_port *port);
548 int saa7164_api_audio_mute(struct saa7164_port *port, int mute);
549 int saa7164_api_set_audio_volume(struct saa7164_port *port, s8 level);
550 int saa7164_api_set_audio_std(struct saa7164_port *port);
551 int saa7164_api_set_audio_detection(struct saa7164_port *port, int autodetect);
552 int saa7164_api_get_videomux(struct saa7164_port *port);
553 int saa7164_api_set_vbi_format(struct saa7164_port *port);
554 int saa7164_api_set_debug(struct saa7164_dev *dev, u8 level);
555 int saa7164_api_collect_debug(struct saa7164_dev *dev);
556 int saa7164_api_get_load_info(struct saa7164_dev *dev,
557 struct tmFwInfoStruct *i);
559 /* ----------------------------------------------------------- */
560 /* saa7164-cards.c */
561 extern struct saa7164_board saa7164_boards[];
562 extern const unsigned int saa7164_bcount;
564 extern struct saa7164_subid saa7164_subids[];
565 extern const unsigned int saa7164_idcount;
567 extern void saa7164_card_list(struct saa7164_dev *dev);
568 extern void saa7164_gpio_setup(struct saa7164_dev *dev);
569 extern void saa7164_card_setup(struct saa7164_dev *dev);
571 extern int saa7164_i2caddr_to_reglen(struct saa7164_i2c *bus, int addr);
572 extern int saa7164_i2caddr_to_unitid(struct saa7164_i2c *bus, int addr);
573 extern char *saa7164_unitid_name(struct saa7164_dev *dev, u8 unitid);
575 /* ----------------------------------------------------------- */
576 /* saa7164-dvb.c */
577 extern int saa7164_dvb_register(struct saa7164_port *port);
578 extern int saa7164_dvb_unregister(struct saa7164_port *port);
580 /* ----------------------------------------------------------- */
581 /* saa7164-buffer.c */
582 extern struct saa7164_buffer *saa7164_buffer_alloc(
583 struct saa7164_port *port, u32 len);
584 extern int saa7164_buffer_dealloc(struct saa7164_buffer *buf);
585 extern void saa7164_buffer_display(struct saa7164_buffer *buf);
586 extern int saa7164_buffer_activate(struct saa7164_buffer *buf, int i);
587 extern int saa7164_buffer_cfg_port(struct saa7164_port *port);
588 extern struct saa7164_user_buffer *saa7164_buffer_alloc_user(
589 struct saa7164_dev *dev, u32 len);
590 extern void saa7164_buffer_dealloc_user(struct saa7164_user_buffer *buf);
591 extern int saa7164_buffer_zero_offsets(struct saa7164_port *port, int i);
593 /* ----------------------------------------------------------- */
594 /* saa7164-encoder.c */
595 int saa7164_s_std(struct saa7164_port *port, v4l2_std_id id);
596 int saa7164_g_std(struct saa7164_port *port, v4l2_std_id *id);
597 int saa7164_enum_input(struct file *file, void *priv, struct v4l2_input *i);
598 int saa7164_g_input(struct saa7164_port *port, unsigned int *i);
599 int saa7164_s_input(struct saa7164_port *port, unsigned int i);
600 int saa7164_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t);
601 int saa7164_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t);
602 int saa7164_g_frequency(struct saa7164_port *port, struct v4l2_frequency *f);
603 int saa7164_s_frequency(struct saa7164_port *port,
604 const struct v4l2_frequency *f);
605 int saa7164_encoder_register(struct saa7164_port *port);
606 void saa7164_encoder_unregister(struct saa7164_port *port);
608 /* ----------------------------------------------------------- */
609 /* saa7164-vbi.c */
610 int saa7164_vbi_register(struct saa7164_port *port);
611 void saa7164_vbi_unregister(struct saa7164_port *port);
613 /* ----------------------------------------------------------- */
615 extern unsigned int crc_checking;
617 extern unsigned int saa_debug;
618 #define dprintk(level, fmt, arg...)\
619 do { if (saa_debug & level)\
620 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
621 } while (0)
623 #define log_warn(fmt, arg...)\
624 do { \
625 printk(KERN_WARNING "%s: " fmt, dev->name, ## arg);\
626 } while (0)
628 #define saa7164_readl(reg) readl(dev->lmmio + ((reg) >> 2))
629 #define saa7164_writel(reg, value) writel((value), dev->lmmio + ((reg) >> 2))
631 #define saa7164_readb(reg) readl(dev->bmmio + (reg))
632 #define saa7164_writeb(reg, value) writel((value), dev->bmmio + (reg))