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