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