1 /* 2 * Driver for the NXP SAA7164 PCIe bridge 3 * 4 * Copyright (c) 2010 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 #include "saa7164.h" 23 24 #include "tda10048.h" 25 #include "tda18271.h" 26 #include "s5h1411.h" 27 28 #define DRIVER_NAME "saa7164" 29 30 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 31 32 /* addr is in the card struct, get it from there */ 33 static struct tda10048_config hauppauge_hvr2200_1_config = { 34 .demod_address = 0x10 >> 1, 35 .output_mode = TDA10048_SERIAL_OUTPUT, 36 .fwbulkwritelen = TDA10048_BULKWRITE_200, 37 .inversion = TDA10048_INVERSION_ON, 38 .dtv6_if_freq_khz = TDA10048_IF_3300, 39 .dtv7_if_freq_khz = TDA10048_IF_3500, 40 .dtv8_if_freq_khz = TDA10048_IF_4000, 41 .clk_freq_khz = TDA10048_CLK_16000, 42 }; 43 static struct tda10048_config hauppauge_hvr2200_2_config = { 44 .demod_address = 0x12 >> 1, 45 .output_mode = TDA10048_SERIAL_OUTPUT, 46 .fwbulkwritelen = TDA10048_BULKWRITE_200, 47 .inversion = TDA10048_INVERSION_ON, 48 .dtv6_if_freq_khz = TDA10048_IF_3300, 49 .dtv7_if_freq_khz = TDA10048_IF_3500, 50 .dtv8_if_freq_khz = TDA10048_IF_4000, 51 .clk_freq_khz = TDA10048_CLK_16000, 52 }; 53 54 static struct tda18271_std_map hauppauge_tda18271_std_map = { 55 .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 3, 56 .if_lvl = 6, .rfagc_top = 0x37 }, 57 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 0, 58 .if_lvl = 6, .rfagc_top = 0x37 }, 59 }; 60 61 static struct tda18271_config hauppauge_hvr22x0_tuner_config = { 62 .std_map = &hauppauge_tda18271_std_map, 63 .gate = TDA18271_GATE_ANALOG, 64 .role = TDA18271_MASTER, 65 }; 66 67 static struct tda18271_config hauppauge_hvr22x0s_tuner_config = { 68 .std_map = &hauppauge_tda18271_std_map, 69 .gate = TDA18271_GATE_ANALOG, 70 .role = TDA18271_SLAVE, 71 .output_opt = TDA18271_OUTPUT_LT_OFF, 72 .rf_cal_on_startup = 1 73 }; 74 75 static struct s5h1411_config hauppauge_s5h1411_config = { 76 .output_mode = S5H1411_SERIAL_OUTPUT, 77 .gpio = S5H1411_GPIO_ON, 78 .qam_if = S5H1411_IF_4000, 79 .vsb_if = S5H1411_IF_3250, 80 .inversion = S5H1411_INVERSION_ON, 81 .status_mode = S5H1411_DEMODLOCKING, 82 .mpeg_timing = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK, 83 }; 84 85 static int saa7164_dvb_stop_port(struct saa7164_port *port) 86 { 87 struct saa7164_dev *dev = port->dev; 88 int ret; 89 90 ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); 91 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) { 92 printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n", 93 __func__, ret); 94 ret = -EIO; 95 } else { 96 dprintk(DBGLVL_DVB, "%s() Stopped\n", __func__); 97 ret = 0; 98 } 99 100 return ret; 101 } 102 103 static int saa7164_dvb_acquire_port(struct saa7164_port *port) 104 { 105 struct saa7164_dev *dev = port->dev; 106 int ret; 107 108 ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE); 109 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) { 110 printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n", 111 __func__, ret); 112 ret = -EIO; 113 } else { 114 dprintk(DBGLVL_DVB, "%s() Acquired\n", __func__); 115 ret = 0; 116 } 117 118 return ret; 119 } 120 121 static int saa7164_dvb_pause_port(struct saa7164_port *port) 122 { 123 struct saa7164_dev *dev = port->dev; 124 int ret; 125 126 ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE); 127 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) { 128 printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n", 129 __func__, ret); 130 ret = -EIO; 131 } else { 132 dprintk(DBGLVL_DVB, "%s() Paused\n", __func__); 133 ret = 0; 134 } 135 136 return ret; 137 } 138 139 /* Firmware is very windows centric, meaning you have to transition 140 * the part through AVStream / KS Windows stages, forwards or backwards. 141 * States are: stopped, acquired (h/w), paused, started. 142 */ 143 static int saa7164_dvb_stop_streaming(struct saa7164_port *port) 144 { 145 struct saa7164_dev *dev = port->dev; 146 struct saa7164_buffer *buf; 147 struct list_head *p, *q; 148 int ret; 149 150 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr); 151 152 ret = saa7164_dvb_pause_port(port); 153 ret = saa7164_dvb_acquire_port(port); 154 ret = saa7164_dvb_stop_port(port); 155 156 /* Mark the hardware buffers as free */ 157 mutex_lock(&port->dmaqueue_lock); 158 list_for_each_safe(p, q, &port->dmaqueue.list) { 159 buf = list_entry(p, struct saa7164_buffer, list); 160 buf->flags = SAA7164_BUFFER_FREE; 161 } 162 mutex_unlock(&port->dmaqueue_lock); 163 164 return ret; 165 } 166 167 static int saa7164_dvb_start_port(struct saa7164_port *port) 168 { 169 struct saa7164_dev *dev = port->dev; 170 int ret = 0, result; 171 172 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr); 173 174 saa7164_buffer_cfg_port(port); 175 176 /* Acquire the hardware */ 177 result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE); 178 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { 179 printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n", 180 __func__, result); 181 182 /* Stop the hardware, regardless */ 183 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); 184 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { 185 printk(KERN_ERR "%s() acquire/forced stop transition " 186 "failed, res = 0x%x\n", __func__, result); 187 } 188 ret = -EIO; 189 goto out; 190 } else 191 dprintk(DBGLVL_DVB, "%s() Acquired\n", __func__); 192 193 /* Pause the hardware */ 194 result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE); 195 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { 196 printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n", 197 __func__, result); 198 199 /* Stop the hardware, regardless */ 200 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); 201 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { 202 printk(KERN_ERR "%s() pause/forced stop transition " 203 "failed, res = 0x%x\n", __func__, result); 204 } 205 206 ret = -EIO; 207 goto out; 208 } else 209 dprintk(DBGLVL_DVB, "%s() Paused\n", __func__); 210 211 /* Start the hardware */ 212 result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN); 213 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { 214 printk(KERN_ERR "%s() run transition failed, result = 0x%x\n", 215 __func__, result); 216 217 /* Stop the hardware, regardless */ 218 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP); 219 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) { 220 printk(KERN_ERR "%s() run/forced stop transition " 221 "failed, res = 0x%x\n", __func__, result); 222 } 223 224 ret = -EIO; 225 } else 226 dprintk(DBGLVL_DVB, "%s() Running\n", __func__); 227 228 out: 229 return ret; 230 } 231 232 static int saa7164_dvb_start_feed(struct dvb_demux_feed *feed) 233 { 234 struct dvb_demux *demux = feed->demux; 235 struct saa7164_port *port = (struct saa7164_port *) demux->priv; 236 struct saa7164_dvb *dvb = &port->dvb; 237 struct saa7164_dev *dev = port->dev; 238 int ret = 0; 239 240 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr); 241 242 if (!demux->dmx.frontend) 243 return -EINVAL; 244 245 if (dvb) { 246 mutex_lock(&dvb->lock); 247 if (dvb->feeding++ == 0) { 248 /* Start transport */ 249 ret = saa7164_dvb_start_port(port); 250 } 251 mutex_unlock(&dvb->lock); 252 dprintk(DBGLVL_DVB, "%s(port=%d) now feeding = %d\n", 253 __func__, port->nr, dvb->feeding); 254 } 255 256 return ret; 257 } 258 259 static int saa7164_dvb_stop_feed(struct dvb_demux_feed *feed) 260 { 261 struct dvb_demux *demux = feed->demux; 262 struct saa7164_port *port = (struct saa7164_port *) demux->priv; 263 struct saa7164_dvb *dvb = &port->dvb; 264 struct saa7164_dev *dev = port->dev; 265 int ret = 0; 266 267 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr); 268 269 if (dvb) { 270 mutex_lock(&dvb->lock); 271 if (--dvb->feeding == 0) { 272 /* Stop transport */ 273 ret = saa7164_dvb_stop_streaming(port); 274 } 275 mutex_unlock(&dvb->lock); 276 dprintk(DBGLVL_DVB, "%s(port=%d) now feeding = %d\n", 277 __func__, port->nr, dvb->feeding); 278 } 279 280 return ret; 281 } 282 283 static int dvb_register(struct saa7164_port *port) 284 { 285 struct saa7164_dvb *dvb = &port->dvb; 286 struct saa7164_dev *dev = port->dev; 287 struct saa7164_buffer *buf; 288 int result, i; 289 290 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr); 291 292 if (port->type != SAA7164_MPEG_DVB) 293 BUG(); 294 295 /* Sanity check that the PCI configuration space is active */ 296 if (port->hwcfg.BARLocation == 0) { 297 result = -ENOMEM; 298 printk(KERN_ERR "%s: dvb_register_adapter failed " 299 "(errno = %d), NO PCI configuration\n", 300 DRIVER_NAME, result); 301 goto fail_adapter; 302 } 303 304 /* Init and establish defaults */ 305 port->hw_streamingparams.bitspersample = 8; 306 port->hw_streamingparams.samplesperline = 188; 307 port->hw_streamingparams.numberoflines = 308 (SAA7164_TS_NUMBER_OF_LINES * 188) / 188; 309 310 port->hw_streamingparams.pitch = 188; 311 port->hw_streamingparams.linethreshold = 0; 312 port->hw_streamingparams.pagetablelistvirt = NULL; 313 port->hw_streamingparams.pagetablelistphys = NULL; 314 port->hw_streamingparams.numpagetables = 2 + 315 ((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE); 316 317 port->hw_streamingparams.numpagetableentries = port->hwcfg.buffercount; 318 319 /* Allocate the PCI resources */ 320 for (i = 0; i < port->hwcfg.buffercount; i++) { 321 buf = saa7164_buffer_alloc(port, 322 port->hw_streamingparams.numberoflines * 323 port->hw_streamingparams.pitch); 324 325 if (!buf) { 326 result = -ENOMEM; 327 printk(KERN_ERR "%s: dvb_register_adapter failed " 328 "(errno = %d), unable to allocate buffers\n", 329 DRIVER_NAME, result); 330 goto fail_adapter; 331 } 332 333 mutex_lock(&port->dmaqueue_lock); 334 list_add_tail(&buf->list, &port->dmaqueue.list); 335 mutex_unlock(&port->dmaqueue_lock); 336 } 337 338 /* register adapter */ 339 result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE, 340 &dev->pci->dev, adapter_nr); 341 if (result < 0) { 342 printk(KERN_ERR "%s: dvb_register_adapter failed " 343 "(errno = %d)\n", DRIVER_NAME, result); 344 goto fail_adapter; 345 } 346 dvb->adapter.priv = port; 347 348 /* register frontend */ 349 result = dvb_register_frontend(&dvb->adapter, dvb->frontend); 350 if (result < 0) { 351 printk(KERN_ERR "%s: dvb_register_frontend failed " 352 "(errno = %d)\n", DRIVER_NAME, result); 353 goto fail_frontend; 354 } 355 356 /* register demux stuff */ 357 dvb->demux.dmx.capabilities = 358 DMX_TS_FILTERING | DMX_SECTION_FILTERING | 359 DMX_MEMORY_BASED_FILTERING; 360 dvb->demux.priv = port; 361 dvb->demux.filternum = 256; 362 dvb->demux.feednum = 256; 363 dvb->demux.start_feed = saa7164_dvb_start_feed; 364 dvb->demux.stop_feed = saa7164_dvb_stop_feed; 365 result = dvb_dmx_init(&dvb->demux); 366 if (result < 0) { 367 printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n", 368 DRIVER_NAME, result); 369 goto fail_dmx; 370 } 371 372 dvb->dmxdev.filternum = 256; 373 dvb->dmxdev.demux = &dvb->demux.dmx; 374 dvb->dmxdev.capabilities = 0; 375 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter); 376 if (result < 0) { 377 printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n", 378 DRIVER_NAME, result); 379 goto fail_dmxdev; 380 } 381 382 dvb->fe_hw.source = DMX_FRONTEND_0; 383 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw); 384 if (result < 0) { 385 printk(KERN_ERR "%s: add_frontend failed " 386 "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result); 387 goto fail_fe_hw; 388 } 389 390 dvb->fe_mem.source = DMX_MEMORY_FE; 391 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem); 392 if (result < 0) { 393 printk(KERN_ERR "%s: add_frontend failed " 394 "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result); 395 goto fail_fe_mem; 396 } 397 398 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw); 399 if (result < 0) { 400 printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n", 401 DRIVER_NAME, result); 402 goto fail_fe_conn; 403 } 404 405 /* register network adapter */ 406 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx); 407 return 0; 408 409 fail_fe_conn: 410 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem); 411 fail_fe_mem: 412 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw); 413 fail_fe_hw: 414 dvb_dmxdev_release(&dvb->dmxdev); 415 fail_dmxdev: 416 dvb_dmx_release(&dvb->demux); 417 fail_dmx: 418 dvb_unregister_frontend(dvb->frontend); 419 fail_frontend: 420 dvb_frontend_detach(dvb->frontend); 421 dvb_unregister_adapter(&dvb->adapter); 422 fail_adapter: 423 return result; 424 } 425 426 int saa7164_dvb_unregister(struct saa7164_port *port) 427 { 428 struct saa7164_dvb *dvb = &port->dvb; 429 struct saa7164_dev *dev = port->dev; 430 struct saa7164_buffer *b; 431 struct list_head *c, *n; 432 433 dprintk(DBGLVL_DVB, "%s()\n", __func__); 434 435 if (port->type != SAA7164_MPEG_DVB) 436 BUG(); 437 438 /* Remove any allocated buffers */ 439 mutex_lock(&port->dmaqueue_lock); 440 list_for_each_safe(c, n, &port->dmaqueue.list) { 441 b = list_entry(c, struct saa7164_buffer, list); 442 list_del(c); 443 saa7164_buffer_dealloc(b); 444 } 445 mutex_unlock(&port->dmaqueue_lock); 446 447 if (dvb->frontend == NULL) 448 return 0; 449 450 dvb_net_release(&dvb->net); 451 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem); 452 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw); 453 dvb_dmxdev_release(&dvb->dmxdev); 454 dvb_dmx_release(&dvb->demux); 455 dvb_unregister_frontend(dvb->frontend); 456 dvb_frontend_detach(dvb->frontend); 457 dvb_unregister_adapter(&dvb->adapter); 458 return 0; 459 } 460 461 /* All the DVB attach calls go here, this function get's modified 462 * for each new card. 463 */ 464 int saa7164_dvb_register(struct saa7164_port *port) 465 { 466 struct saa7164_dev *dev = port->dev; 467 struct saa7164_dvb *dvb = &port->dvb; 468 struct saa7164_i2c *i2c_bus = NULL; 469 int ret; 470 471 dprintk(DBGLVL_DVB, "%s()\n", __func__); 472 473 /* init frontend */ 474 switch (dev->board) { 475 case SAA7164_BOARD_HAUPPAUGE_HVR2200: 476 case SAA7164_BOARD_HAUPPAUGE_HVR2200_2: 477 case SAA7164_BOARD_HAUPPAUGE_HVR2200_3: 478 case SAA7164_BOARD_HAUPPAUGE_HVR2200_4: 479 case SAA7164_BOARD_HAUPPAUGE_HVR2200_5: 480 i2c_bus = &dev->i2c_bus[port->nr + 1]; 481 switch (port->nr) { 482 case 0: 483 port->dvb.frontend = dvb_attach(tda10048_attach, 484 &hauppauge_hvr2200_1_config, 485 &i2c_bus->i2c_adap); 486 487 if (port->dvb.frontend != NULL) { 488 /* TODO: addr is in the card struct */ 489 dvb_attach(tda18271_attach, port->dvb.frontend, 490 0xc0 >> 1, &i2c_bus->i2c_adap, 491 &hauppauge_hvr22x0_tuner_config); 492 } 493 494 break; 495 case 1: 496 port->dvb.frontend = dvb_attach(tda10048_attach, 497 &hauppauge_hvr2200_2_config, 498 &i2c_bus->i2c_adap); 499 500 if (port->dvb.frontend != NULL) { 501 /* TODO: addr is in the card struct */ 502 dvb_attach(tda18271_attach, port->dvb.frontend, 503 0xc0 >> 1, &i2c_bus->i2c_adap, 504 &hauppauge_hvr22x0s_tuner_config); 505 } 506 507 break; 508 } 509 break; 510 case SAA7164_BOARD_HAUPPAUGE_HVR2250: 511 case SAA7164_BOARD_HAUPPAUGE_HVR2250_2: 512 case SAA7164_BOARD_HAUPPAUGE_HVR2250_3: 513 i2c_bus = &dev->i2c_bus[port->nr + 1]; 514 515 port->dvb.frontend = dvb_attach(s5h1411_attach, 516 &hauppauge_s5h1411_config, 517 &i2c_bus->i2c_adap); 518 519 if (port->dvb.frontend != NULL) { 520 if (port->nr == 0) { 521 /* Master TDA18271 */ 522 /* TODO: addr is in the card struct */ 523 dvb_attach(tda18271_attach, port->dvb.frontend, 524 0xc0 >> 1, &i2c_bus->i2c_adap, 525 &hauppauge_hvr22x0_tuner_config); 526 } else { 527 /* Slave TDA18271 */ 528 dvb_attach(tda18271_attach, port->dvb.frontend, 529 0xc0 >> 1, &i2c_bus->i2c_adap, 530 &hauppauge_hvr22x0s_tuner_config); 531 } 532 } 533 534 break; 535 default: 536 printk(KERN_ERR "%s: The frontend isn't supported\n", 537 dev->name); 538 break; 539 } 540 if (NULL == dvb->frontend) { 541 printk(KERN_ERR "%s() Frontend initialization failed\n", 542 __func__); 543 return -1; 544 } 545 546 /* register everything */ 547 ret = dvb_register(port); 548 if (ret < 0) { 549 if (dvb->frontend->ops.release) 550 dvb->frontend->ops.release(dvb->frontend); 551 return ret; 552 } 553 554 return 0; 555 } 556 557