1 /* 2 * tw68 driver common header file 3 * 4 * Much of this code is derived from the cx88 and sa7134 drivers, which 5 * were in turn derived from the bt87x driver. The original work was by 6 * Gerd Knorr; more recently the code was enhanced by Mauro Carvalho Chehab, 7 * Hans Verkuil, Andy Walls and many others. Their work is gratefully 8 * acknowledged. Full credit goes to them - any problems within this code 9 * are mine. 10 * 11 * Copyright (C) 2009 William M. Brack <wbrack@mmm.com.hk> 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License as published by 15 * the Free Software Foundation; either version 2 of the License, or 16 * (at your option) any later version. 17 * 18 * This program is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU General Public License for more details. 22 * 23 * You should have received a copy of the GNU General Public License 24 * along with this program; if not, write to the Free Software 25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 26 * 02111-1307 USA 27 */ 28 29 #include <linux/version.h> 30 #define TW68_VERSION_CODE KERNEL_VERSION(0, 0, 8) 31 32 #include <linux/pci.h> 33 #include <linux/i2c.h> 34 #include <linux/i2c-algo-bit.h> 35 #include <linux/videodev2.h> 36 #include <linux/kdev_t.h> 37 #include <linux/input.h> 38 #include <linux/notifier.h> 39 #include <linux/delay.h> 40 #include <linux/mutex.h> 41 42 #include <asm/io.h> 43 44 #include <media/v4l2-common.h> 45 #include <media/v4l2-ioctl.h> 46 #include <media/v4l2-device.h> 47 48 #include <media/tuner.h> 49 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) 50 # include <media/ir-common.h> 51 #endif 52 #include <media/ir-kbd-i2c.h> 53 #include <media/videobuf-dma-sg.h> 54 55 #include "btcx-risc.h" 56 #include "tw68-reg.h" 57 58 #define UNSET (-1U) 59 60 /* 61 * dprintk statement within the code use a 'level' argument. For 62 * our purposes, we use the following levels: 63 */ 64 #define DBG_UNEXPECTED (1 << 0) 65 #define DBG_UNUSUAL (1 << 1) 66 #define DBG_TESTING (1 << 2) 67 #define DBG_BUFF (1 << 3) 68 #define DBG_FLOW (1 << 15) 69 70 /* system vendor and device ID's */ 71 #define PCI_VENDOR_ID_TECHWELL 0x1797 72 #define PCI_DEVICE_ID_6800 0x6800 73 #define PCI_DEVICE_ID_6801 0x6801 74 #define PCI_DEVICE_ID_AUDIO2 0x6802 75 #define PCI_DEVICE_ID_TS3 0x6803 76 #define PCI_DEVICE_ID_6804 0x6804 77 #define PCI_DEVICE_ID_AUDIO5 0x6805 78 #define PCI_DEVICE_ID_TS6 0x6806 79 80 /* tw6816 based cards */ 81 #define PCI_DEVICE_ID_6816_1 0x6810 82 #define PCI_DEVICE_ID_6816_2 0x6811 83 #define PCI_DEVICE_ID_6816_3 0x6812 84 #define PCI_DEVICE_ID_6816_4 0x6813 85 86 /* subsystem vendor ID's */ 87 #define TW68_PCI_ID_TECHWELL 0x1797 88 89 #define TW68_NORMS (\ 90 V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM | \ 91 V4L2_STD_PAL_BG | V4L2_STD_PAL_DK | V4L2_STD_PAL_I | \ 92 V4L2_STD_PAL_M | V4L2_STD_PAL_Nc | V4L2_STD_PAL_60 | \ 93 V4L2_STD_525_60 | V4L2_STD_625_50 | \ 94 V4L2_STD_SECAM_L| V4L2_STD_SECAM_LC | V4L2_STD_SECAM_DK) 95 96 #define TW68_VID_INTS (TW68_FFERR | TW68_PABORT | TW68_DMAPERR | \ 97 TW68_FFOF | TW68_DMAPI) 98 /* TW6800 chips have trouble with these, so we don't set them for that chip */ 99 #define TW68_VID_INTSX (TW68_FDMIS | TW68_HLOCK | TW68_VLOCK) 100 101 #define TW68_I2C_INTS (TW68_SBERR | TW68_SBDONE | TW68_SBERR2 | \ 102 TW68_SBDONE2) 103 104 typedef enum { 105 TW6800, 106 TW6801, 107 TW6804, 108 TWXXXX, 109 } TW68_DECODER_TYPE; 110 /* ----------------------------------------------------------- */ 111 /* static data */ 112 113 struct tw68_tvnorm { 114 char *name; 115 v4l2_std_id id; 116 117 /* video decoder */ 118 u32 sync_control; 119 u32 luma_control; 120 u32 chroma_ctrl1; 121 u32 chroma_gain; 122 u32 chroma_ctrl2; 123 u32 vgate_misc; 124 125 /* video scaler */ 126 u32 h_delay; 127 u32 h_delay0; /* for TW6800 */ 128 u32 h_start; 129 u32 h_stop; 130 u32 v_delay; 131 u32 video_v_start; 132 u32 video_v_stop; 133 u32 vbi_v_start_0; 134 u32 vbi_v_stop_0; 135 u32 vbi_v_start_1; 136 137 /* Techwell specific */ 138 u32 format; 139 }; 140 141 struct tw68_format { 142 char *name; 143 u32 fourcc; 144 u32 depth; 145 u32 twformat; 146 }; 147 148 /* ----------------------------------------------------------- */ 149 /* card configuration */ 150 151 #define TW68_BOARD_NOAUTO UNSET 152 #define TW68_BOARD_UNKNOWN 0 153 #define TW68_BOARD_GENERIC_6802 1 154 155 #define TW68_MAXBOARDS 16 156 #define TW68_INPUT_MAX 8 157 158 /* ----------------------------------------------------------- */ 159 /* enums */ 160 161 enum tw68_mpeg_type { 162 TW68_MPEG_UNUSED, 163 TW68_MPEG_EMPRESS, 164 TW68_MPEG_DVB, 165 }; 166 167 enum tw68_audio_in { 168 TV = 1, 169 LINE1 = 2, 170 LINE2 = 3, 171 LINE2_LEFT, 172 }; 173 174 enum tw68_video_out { 175 CCIR656 = 1, 176 }; 177 178 /* Structs for card definition */ 179 struct tw68_input { 180 char *name; /* text description */ 181 unsigned int vmux; /* mux value */ 182 enum tw68_audio_in mux; 183 unsigned int gpio; 184 unsigned int tv:1; 185 }; 186 187 struct tw68_board { 188 char *name; 189 unsigned int audio_clock; 190 191 /* input switching */ 192 unsigned int gpiomask; 193 struct tw68_input inputs[TW68_INPUT_MAX]; 194 struct tw68_input radio; 195 struct tw68_input mute; 196 197 /* i2c chip info */ 198 unsigned int tuner_type; 199 unsigned int radio_type; 200 unsigned char tuner_addr; 201 unsigned char radio_addr; 202 203 unsigned int tda9887_conf; 204 unsigned int tuner_config; 205 206 enum tw68_video_out video_out; 207 enum tw68_mpeg_type mpeg; 208 unsigned int vid_port_opts; 209 }; 210 211 #define card_has_radio(dev) (NULL != tw68_boards[dev->board].radio.name) 212 #define card_has_mpeg(dev) (TW68_MPEG_UNUSED != \ 213 tw68_boards[dev->board].mpeg) 214 #define card_in(dev, n) (tw68_boards[dev->board].inputs[n]) 215 #define card(dev) (tw68_boards[dev->board]) 216 217 /* ----------------------------------------------------------- */ 218 /* device / file handle status */ 219 220 #define RESOURCE_VIDEO 1 221 #define RESOURCE_VBI 2 222 223 #define INTERLACE_AUTO 0 224 #define INTERLACE_ON 1 225 #define INTERLACE_OFF 2 226 227 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */ 228 229 struct tw68_dev; /* forward delclaration */ 230 231 /* tvaudio thread status */ 232 struct tw68_thread { 233 struct task_struct *thread; 234 unsigned int scan1; 235 unsigned int scan2; 236 unsigned int mode; 237 unsigned int stopped; 238 }; 239 240 /* buffer for one video/vbi/ts frame */ 241 struct tw68_buf { 242 /* common v4l buffer stuff -- must be first */ 243 struct videobuf_buffer vb; 244 245 /* tw68 specific */ 246 struct tw68_format *fmt; 247 struct tw68_input *input; 248 unsigned int top_seen; 249 int (*activate)(struct tw68_dev *dev, 250 struct tw68_buf *buf, 251 struct tw68_buf *next); 252 struct btcx_riscmem risc; 253 unsigned int bpl; 254 }; 255 256 struct tw68_dmaqueue { 257 struct tw68_dev *dev; 258 struct list_head active; 259 struct list_head queued; 260 struct timer_list timeout; 261 struct btcx_riscmem stopper; 262 int (*buf_compat)(struct tw68_buf *prev, 263 struct tw68_buf *buf); 264 int (*start_dma)(struct tw68_dev *dev, 265 struct tw68_dmaqueue *q, 266 struct tw68_buf *buf); 267 }; 268 269 /* video filehandle status */ 270 struct tw68_fh { 271 struct tw68_dev *dev; 272 unsigned int radio; 273 enum v4l2_buf_type type; 274 unsigned int resources; 275 enum v4l2_priority prio; 276 277 /* video capture */ 278 struct tw68_format *fmt; 279 unsigned int width, height; 280 struct videobuf_queue cap; /* also used for overlay */ 281 282 /* vbi capture */ 283 struct videobuf_queue vbi; 284 }; 285 286 /* dmasound dsp status */ 287 struct tw68_dmasound { 288 struct mutex lock; 289 int minor_mixer; 290 int minor_dsp; 291 unsigned int users_dsp; 292 293 /* mixer */ 294 enum tw68_audio_in input; 295 unsigned int count; 296 unsigned int line1; 297 unsigned int line2; 298 299 /* dsp */ 300 unsigned int afmt; 301 unsigned int rate; 302 unsigned int channels; 303 unsigned int recording_on; 304 unsigned int dma_running; 305 unsigned int blocks; 306 unsigned int blksize; 307 unsigned int bufsize; 308 struct videobuf_dmabuf dma; 309 unsigned int dma_blk; 310 unsigned int read_offset; 311 unsigned int read_count; 312 void *priv_data; 313 struct snd_pcm_substream *substream; 314 }; 315 316 struct tw68_fmt { 317 char *name; 318 u32 fourcc; /* v4l2 format id */ 319 int depth; 320 int flags; 321 u32 twformat; 322 }; 323 324 /* ts/mpeg status */ 325 struct tw68_ts { 326 /* TS capture */ 327 int nr_packets; 328 int nr_bufs; 329 }; 330 331 /* ts/mpeg ops */ 332 struct tw68_mpeg_ops { 333 enum tw68_mpeg_type type; 334 struct list_head next; 335 int (*init)(struct tw68_dev *dev); 336 int (*fini)(struct tw68_dev *dev); 337 void (*signal_change)(struct tw68_dev *dev); 338 }; 339 340 enum tw68_ts_status { 341 TW68_TS_STOPPED, 342 TW68_TS_BUFF_DONE, 343 TW68_TS_STARTED, 344 }; 345 346 /* global device status */ 347 struct tw68_dev { 348 struct list_head devlist; 349 struct mutex lock; 350 spinlock_t slock; 351 struct v4l2_prio_state prio; 352 struct v4l2_device v4l2_dev; 353 /* workstruct for loading modules */ 354 struct work_struct request_module_wk; 355 356 /* insmod option/autodetected */ 357 int autodetected; 358 359 /* various device info */ 360 TW68_DECODER_TYPE vdecoder; 361 unsigned int resources; 362 struct video_device *video_dev; 363 struct video_device *radio_dev; 364 struct video_device *vbi_dev; 365 struct tw68_dmasound dmasound; 366 367 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0) 368 /* infrared remote */ 369 int has_remote; 370 struct card_ir *remote; 371 #endif 372 373 /* pci i/o */ 374 char name[32]; 375 int nr; 376 struct pci_dev *pci; 377 unsigned char pci_rev, pci_lat; 378 u32 __iomem *lmmio; 379 u8 __iomem *bmmio; 380 u32 pci_irqmask; 381 /* The irq mask to be used will depend upon the chip type */ 382 u32 board_virqmask; 383 384 /* config info */ 385 unsigned int board; 386 unsigned int tuner_type; 387 unsigned int radio_type; 388 unsigned char tuner_addr; 389 unsigned char radio_addr; 390 391 unsigned int tda9887_conf; 392 unsigned int gpio_value; 393 394 /* i2c i/o */ 395 struct i2c_algo_bit_data i2c_algo; 396 struct i2c_adapter i2c_adap; 397 struct i2c_client i2c_client; 398 u32 i2c_state; 399 u32 i2c_done; 400 wait_queue_head_t i2c_queue; 401 int i2c_rc; 402 unsigned char eedata[256]; 403 404 /* video+ts+vbi capture */ 405 struct tw68_dmaqueue video_q; 406 struct tw68_dmaqueue vbi_q; 407 unsigned int video_fieldcount; 408 unsigned int vbi_fieldcount; 409 410 /* various v4l controls */ 411 struct tw68_tvnorm *tvnorm; /* video */ 412 struct tw68_tvaudio *tvaudio; 413 #if 0 414 unsigned int ctl_input; 415 int ctl_bright; 416 int ctl_contrast; 417 int ctl_hue; 418 int ctl_saturation; 419 int ctl_freq; 420 int ctl_mute; /* audio */ 421 int ctl_volume; 422 int ctl_invert; /* private */ 423 int ctl_mirror; 424 int ctl_y_odd; 425 int ctl_y_even; 426 int ctl_automute; 427 #endif 428 429 /* crop */ 430 struct v4l2_rect crop_bounds; 431 struct v4l2_rect crop_defrect; 432 struct v4l2_rect crop_current; 433 434 /* other global state info */ 435 unsigned int automute; 436 struct tw68_thread thread; 437 /* input is latest requested by app, hw_input is current hw setting */ 438 struct tw68_input *input; 439 struct tw68_input *hw_input; 440 unsigned int hw_mute; 441 int last_carrier; 442 int nosignal; 443 unsigned int insuspend; 444 445 /* TW68_MPEG_* */ 446 struct tw68_ts ts; 447 struct tw68_dmaqueue ts_q; 448 enum tw68_ts_status ts_state; 449 unsigned int buff_cnt; 450 struct tw68_mpeg_ops *mops; 451 452 void (*gate_ctrl)(struct tw68_dev *dev, int open); 453 }; 454 455 /* ----------------------------------------------------------- */ 456 457 #define tw_readl(reg) readl(dev->lmmio + ((reg) >> 2)) 458 #define tw_readb(reg) readb(dev->bmmio + (reg)) 459 #define tw_writel(reg, value) writel((value), dev->lmmio + ((reg) >> 2)) 460 #define tw_writeb(reg, value) writeb((value), dev->bmmio + (reg)) 461 462 #define tw_andorl(reg, mask, value) \ 463 writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\ 464 ((value) & (mask)), dev->lmmio+((reg)>>2)) 465 #define tw_andorb(reg, mask, value) \ 466 writeb((readb(dev->bmmio + (reg)) & ~(mask)) |\ 467 ((value) & (mask)), dev->bmmio+(reg)) 468 #define tw_setl(reg, bit) tw_andorl((reg), (bit), (bit)) 469 #define tw_setb(reg, bit) tw_andorb((reg), (bit), (bit)) 470 #define tw_clearl(reg, bit) \ 471 writel((readl(dev->lmmio + ((reg) >> 2)) & ~(bit)), \ 472 dev->lmmio + ((reg) >> 2)) 473 #define tw_clearb(reg, bit) \ 474 writeb((readb(dev->bmmio+(reg)) & ~(bit)), \ 475 dev->bmmio + (reg)) 476 #define tw_call_all(dev, o, f, args...) do { \ 477 if (dev->gate_ctrl) \ 478 dev->gate_ctrl(dev, 1); \ 479 v4l2_device_call_all(&(dev)->v4l2_dev, 0, o, f , ##args); \ 480 if (dev->gate_ctrl) \ 481 dev->gate_ctrl(dev, 0); \ 482 } while (0) 483 484 #define tw_wait(us) { udelay(us); } 485 486 static inline struct tw68_dev *to_tw68_dev(struct v4l2_device *v4l2_dev) 487 { 488 return container_of(v4l2_dev, struct tw68_dev, v4l2_dev); 489 } 490 491 /* ----------------------------------------------------------- */ 492 /* tw68-core.c */ 493 494 extern struct list_head tw68_devlist; 495 extern struct mutex tw68_devlist_lock; 496 extern unsigned int irq_debug; 497 498 int tw68_buffer_count(unsigned int size, unsigned int count); 499 void tw68_buffer_queue(struct tw68_dev *dev, struct tw68_dmaqueue *q, 500 struct tw68_buf *buf); 501 void tw68_buffer_timeout(unsigned long data); 502 int tw68_set_dmabits(struct tw68_dev *dev); 503 void tw68_dma_free(struct videobuf_queue *q, struct tw68_buf *buf); 504 void tw68_wakeup(struct tw68_dmaqueue *q, unsigned int *field_count); 505 int tw68_buffer_requeue(struct tw68_dev *dev, struct tw68_dmaqueue *q); 506 507 /* ----------------------------------------------------------- */ 508 /* tw68-cards.c */ 509 510 extern struct tw68_board tw68_boards[]; 511 extern const unsigned int tw68_bcount; 512 extern struct pci_device_id __devinitdata tw68_pci_tbl[]; 513 514 int tw68_board_init1(struct tw68_dev *dev); 515 int tw68_board_init2(struct tw68_dev *dev); 516 int tw68_tuner_callback(void *priv, int component, int command, int arg); 517 518 /* ----------------------------------------------------------- */ 519 /* tw68-i2c.c */ 520 521 int tw68_i2c_register(struct tw68_dev *dev); 522 int tw68_i2c_unregister(struct tw68_dev *dev); 523 void tw68_irq_i2c(struct tw68_dev *dev, int status); 524 525 /* ----------------------------------------------------------- */ 526 /* tw68-video.c */ 527 528 extern unsigned int video_debug; 529 extern struct video_device tw68_video_template; 530 extern struct video_device tw68_radio_template; 531 532 int tw68_videoport_init(struct tw68_dev *dev); 533 void tw68_set_tvnorm_hw(struct tw68_dev *dev); 534 535 int tw68_video_init1(struct tw68_dev *dev); 536 int tw68_video_init2(struct tw68_dev *dev); 537 void tw68_irq_video_signalchange(struct tw68_dev *dev); 538 void tw68_irq_video_done(struct tw68_dev *dev, unsigned long status); 539 540 /* ----------------------------------------------------------- */ 541 /* tw68-ts.c */ 542 543 int tw68_ts_init1(struct tw68_dev *dev); 544 int tw68_ts_fini(struct tw68_dev *dev); 545 void tw68_irq_ts_done(struct tw68_dev *dev, unsigned long status); 546 547 int tw68_ts_register(struct tw68_mpeg_ops *ops); 548 void tw68_ts_unregister(struct tw68_mpeg_ops *ops); 549 550 int tw68_ts_init_hw(struct tw68_dev *dev); 551 552 /* ----------------------------------------------------------- */ 553 /* tw68-vbi.c */ 554 555 extern struct videobuf_queue_ops tw68_vbi_qops; 556 extern struct video_device tw68_vbi_template; 557 558 int tw68_vbi_init1(struct tw68_dev *dev); 559 int tw68_vbi_fini(struct tw68_dev *dev); 560 void tw68_irq_vbi_done(struct tw68_dev *dev, unsigned long status); 561 562 /* ----------------------------------------------------------- */ 563 /* tw68-tvaudio.c */ 564 565 int tw68_tvaudio_rx2mode(u32 rx); 566 567 void tw68_tvaudio_setmute(struct tw68_dev *dev); 568 void tw68_tvaudio_setinput(struct tw68_dev *dev, 569 struct tw68_input *in); 570 void tw68_tvaudio_setvolume(struct tw68_dev *dev, int level); 571 int tw68_tvaudio_getstereo(struct tw68_dev *dev); 572 void tw68_tvaudio_init(struct tw68_dev *dev); 573 int tw68_tvaudio_init2(struct tw68_dev *dev); 574 int tw68_tvaudio_fini(struct tw68_dev *dev); 575 int tw68_tvaudio_do_scan(struct tw68_dev *dev); 576 int tw_dsp_writel(struct tw68_dev *dev, int reg, u32 value); 577 void tw68_enable_i2s(struct tw68_dev *dev); 578 579 /* ----------------------------------------------------------- */ 580 /* tw68-risc.c */ 581 582 int tw68_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc, 583 struct scatterlist *sglist, unsigned int top_offset, 584 unsigned int bottom_offset, unsigned int bpl, 585 unsigned int padding, unsigned int lines); 586 int tw68_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc); 587 int tw68_risc_overlay(struct tw68_fh *fh, struct btcx_riscmem *risc, 588 int field_type); 589