OMAP3 SRF: Generic shared resource f/w
[linux-ginger.git] / drivers / media / video / saa7164 / saa7164-dvb.c
blob6a2d847d6a881e441c8aaa5a63e2f4b4d6ca5207
1 /*
2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2009 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.
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.
22 #include "saa7164.h"
24 #include "tda10048.h"
25 #include "tda18271.h"
26 #include "s5h1411.h"
28 #define DRIVER_NAME "saa7164"
30 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
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,
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,
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 },
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,
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 .rf_cal_on_startup = 1
74 static struct s5h1411_config hauppauge_s5h1411_config = {
75 .output_mode = S5H1411_SERIAL_OUTPUT,
76 .gpio = S5H1411_GPIO_ON,
77 .qam_if = S5H1411_IF_4000,
78 .vsb_if = S5H1411_IF_3250,
79 .inversion = S5H1411_INVERSION_ON,
80 .status_mode = S5H1411_DEMODLOCKING,
81 .mpeg_timing = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
84 static int saa7164_dvb_stop_tsport(struct saa7164_tsport *port)
86 struct saa7164_dev *dev = port->dev;
87 int ret;
89 ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
90 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
91 printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
92 __func__, ret);
93 ret = -EIO;
94 } else {
95 dprintk(DBGLVL_DVB, "%s() Stopped\n", __func__);
96 ret = 0;
99 return ret;
102 static int saa7164_dvb_acquire_tsport(struct saa7164_tsport *port)
104 struct saa7164_dev *dev = port->dev;
105 int ret;
107 ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
108 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
109 printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
110 __func__, ret);
111 ret = -EIO;
112 } else {
113 dprintk(DBGLVL_DVB, "%s() Acquired\n", __func__);
114 ret = 0;
117 return ret;
120 static int saa7164_dvb_pause_tsport(struct saa7164_tsport *port)
122 struct saa7164_dev *dev = port->dev;
123 int ret;
125 ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
126 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
127 printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
128 __func__, ret);
129 ret = -EIO;
130 } else {
131 dprintk(DBGLVL_DVB, "%s() Paused\n", __func__);
132 ret = 0;
135 return ret;
138 /* Firmware is very windows centric, meaning you have to transition
139 * the part through AVStream / KS Windows stages, forwards or backwards.
140 * States are: stopped, acquired (h/w), paused, started.
142 static int saa7164_dvb_stop_streaming(struct saa7164_tsport *port)
144 struct saa7164_dev *dev = port->dev;
145 int ret;
147 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
149 ret = saa7164_dvb_pause_tsport(port);
150 ret = saa7164_dvb_acquire_tsport(port);
151 ret = saa7164_dvb_stop_tsport(port);
153 return ret;
156 static int saa7164_dvb_cfg_tsport(struct saa7164_tsport *port)
158 tmHWStreamParameters_t *params = &port->hw_streamingparams;
159 struct saa7164_dev *dev = port->dev;
160 struct saa7164_buffer *buf;
161 struct list_head *c, *n;
162 int i = 0;
164 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
166 saa7164_writel(port->pitch, params->pitch);
167 saa7164_writel(port->bufsize, params->pitch * params->numberoflines);
169 dprintk(DBGLVL_DVB, " configured:\n");
170 dprintk(DBGLVL_DVB, " lmmio 0x%p\n", dev->lmmio);
171 dprintk(DBGLVL_DVB, " bufcounter 0x%x = 0x%x\n", port->bufcounter,
172 saa7164_readl(port->bufcounter));
174 dprintk(DBGLVL_DVB, " pitch 0x%x = %d\n", port->pitch,
175 saa7164_readl(port->pitch));
177 dprintk(DBGLVL_DVB, " bufsize 0x%x = %d\n", port->bufsize,
178 saa7164_readl(port->bufsize));
180 dprintk(DBGLVL_DVB, " buffercount = %d\n", port->hwcfg.buffercount);
181 dprintk(DBGLVL_DVB, " bufoffset = 0x%x\n", port->bufoffset);
182 dprintk(DBGLVL_DVB, " bufptr32h = 0x%x\n", port->bufptr32h);
183 dprintk(DBGLVL_DVB, " bufptr32l = 0x%x\n", port->bufptr32l);
185 /* Poke the buffers and offsets into PCI space */
186 mutex_lock(&port->dmaqueue_lock);
187 list_for_each_safe(c, n, &port->dmaqueue.list) {
188 buf = list_entry(c, struct saa7164_buffer, list);
190 /* TODO: Review this in light of 32v64 assignments */
191 saa7164_writel(port->bufoffset + (sizeof(u32) * i), 0);
192 saa7164_writel(port->bufptr32h + ((sizeof(u32) * 2) * i),
193 buf->pt_dma);
194 saa7164_writel(port->bufptr32l + ((sizeof(u32) * 2) * i), 0);
196 dprintk(DBGLVL_DVB,
197 " buf[%d] offset 0x%llx (0x%x) "
198 "buf 0x%llx/%llx (0x%x/%x)\n",
200 (u64)port->bufoffset + (i * sizeof(u32)),
201 saa7164_readl(port->bufoffset + (sizeof(u32) * i)),
202 (u64)port->bufptr32h + ((sizeof(u32) * 2) * i),
203 (u64)port->bufptr32l + ((sizeof(u32) * 2) * i),
204 saa7164_readl(port->bufptr32h + ((sizeof(u32) * i)
205 * 2)),
206 saa7164_readl(port->bufptr32l + ((sizeof(u32) * i)
207 * 2)));
209 if (i++ > port->hwcfg.buffercount)
210 BUG();
213 mutex_unlock(&port->dmaqueue_lock);
215 return 0;
218 static int saa7164_dvb_start_tsport(struct saa7164_tsport *port)
220 struct saa7164_dev *dev = port->dev;
221 int ret = 0, result;
223 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
225 saa7164_dvb_cfg_tsport(port);
227 /* Acquire the hardware */
228 result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
229 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
230 printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
231 __func__, result);
233 /* Stop the hardware, regardless */
234 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
235 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
236 printk(KERN_ERR "%s() acquire/forced stop transition "
237 "failed, res = 0x%x\n", __func__, result);
239 ret = -EIO;
240 goto out;
241 } else
242 dprintk(DBGLVL_DVB, "%s() Acquired\n", __func__);
244 /* Pause the hardware */
245 result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
246 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
247 printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
248 __func__, result);
250 /* Stop the hardware, regardless */
251 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
252 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
253 printk(KERN_ERR "%s() pause/forced stop transition "
254 "failed, res = 0x%x\n", __func__, result);
257 ret = -EIO;
258 goto out;
259 } else
260 dprintk(DBGLVL_DVB, "%s() Paused\n", __func__);
262 /* Start the hardware */
263 result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
264 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
265 printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
266 __func__, result);
268 /* Stop the hardware, regardless */
269 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
270 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
271 printk(KERN_ERR "%s() run/forced stop transition "
272 "failed, res = 0x%x\n", __func__, result);
275 ret = -EIO;
276 } else
277 dprintk(DBGLVL_DVB, "%s() Running\n", __func__);
279 out:
280 return ret;
283 static int saa7164_dvb_start_feed(struct dvb_demux_feed *feed)
285 struct dvb_demux *demux = feed->demux;
286 struct saa7164_tsport *port = (struct saa7164_tsport *) demux->priv;
287 struct saa7164_dvb *dvb = &port->dvb;
288 struct saa7164_dev *dev = port->dev;
289 int ret = 0;
291 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
293 if (!demux->dmx.frontend)
294 return -EINVAL;
296 if (dvb) {
297 mutex_lock(&dvb->lock);
298 if (dvb->feeding++ == 0) {
299 /* Start transport */
300 ret = saa7164_dvb_start_tsport(port);
302 mutex_unlock(&dvb->lock);
303 dprintk(DBGLVL_DVB, "%s(port=%d) now feeding = %d\n",
304 __func__, port->nr, dvb->feeding);
307 return ret;
310 static int saa7164_dvb_stop_feed(struct dvb_demux_feed *feed)
312 struct dvb_demux *demux = feed->demux;
313 struct saa7164_tsport *port = (struct saa7164_tsport *) demux->priv;
314 struct saa7164_dvb *dvb = &port->dvb;
315 struct saa7164_dev *dev = port->dev;
316 int ret = 0;
318 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
320 if (dvb) {
321 mutex_lock(&dvb->lock);
322 if (--dvb->feeding == 0) {
323 /* Stop transport */
324 ret = saa7164_dvb_stop_streaming(port);
326 mutex_unlock(&dvb->lock);
327 dprintk(DBGLVL_DVB, "%s(port=%d) now feeding = %d\n",
328 __func__, port->nr, dvb->feeding);
331 return ret;
334 static int dvb_register(struct saa7164_tsport *port)
336 struct saa7164_dvb *dvb = &port->dvb;
337 struct saa7164_dev *dev = port->dev;
338 struct saa7164_buffer *buf;
339 int result, i;
341 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
343 /* Sanity check that the PCI configuration space is active */
344 if (port->hwcfg.BARLocation == 0) {
345 result = -ENOMEM;
346 printk(KERN_ERR "%s: dvb_register_adapter failed "
347 "(errno = %d), NO PCI configuration\n",
348 DRIVER_NAME, result);
349 goto fail_adapter;
352 /* Init and establish defaults */
353 port->hw_streamingparams.bitspersample = 8;
354 port->hw_streamingparams.samplesperline = 188;
355 port->hw_streamingparams.numberoflines =
356 (SAA7164_TS_NUMBER_OF_LINES * 188) / 188;
358 port->hw_streamingparams.pitch = 188;
359 port->hw_streamingparams.linethreshold = 0;
360 port->hw_streamingparams.pagetablelistvirt = 0;
361 port->hw_streamingparams.pagetablelistphys = 0;
362 port->hw_streamingparams.numpagetables = 2 +
363 ((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
365 port->hw_streamingparams.numpagetableentries = port->hwcfg.buffercount;
367 /* Allocate the PCI resources */
368 for (i = 0; i < port->hwcfg.buffercount; i++) {
369 buf = saa7164_buffer_alloc(port,
370 port->hw_streamingparams.numberoflines *
371 port->hw_streamingparams.pitch);
373 if (!buf) {
374 result = -ENOMEM;
375 printk(KERN_ERR "%s: dvb_register_adapter failed "
376 "(errno = %d), unable to allocate buffers\n",
377 DRIVER_NAME, result);
378 goto fail_adapter;
380 buf->nr = i;
382 mutex_lock(&port->dmaqueue_lock);
383 list_add_tail(&buf->list, &port->dmaqueue.list);
384 mutex_unlock(&port->dmaqueue_lock);
387 /* register adapter */
388 result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE,
389 &dev->pci->dev, adapter_nr);
390 if (result < 0) {
391 printk(KERN_ERR "%s: dvb_register_adapter failed "
392 "(errno = %d)\n", DRIVER_NAME, result);
393 goto fail_adapter;
395 dvb->adapter.priv = port;
397 /* register frontend */
398 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
399 if (result < 0) {
400 printk(KERN_ERR "%s: dvb_register_frontend failed "
401 "(errno = %d)\n", DRIVER_NAME, result);
402 goto fail_frontend;
405 /* register demux stuff */
406 dvb->demux.dmx.capabilities =
407 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
408 DMX_MEMORY_BASED_FILTERING;
409 dvb->demux.priv = port;
410 dvb->demux.filternum = 256;
411 dvb->demux.feednum = 256;
412 dvb->demux.start_feed = saa7164_dvb_start_feed;
413 dvb->demux.stop_feed = saa7164_dvb_stop_feed;
414 result = dvb_dmx_init(&dvb->demux);
415 if (result < 0) {
416 printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
417 DRIVER_NAME, result);
418 goto fail_dmx;
421 dvb->dmxdev.filternum = 256;
422 dvb->dmxdev.demux = &dvb->demux.dmx;
423 dvb->dmxdev.capabilities = 0;
424 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
425 if (result < 0) {
426 printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
427 DRIVER_NAME, result);
428 goto fail_dmxdev;
431 dvb->fe_hw.source = DMX_FRONTEND_0;
432 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
433 if (result < 0) {
434 printk(KERN_ERR "%s: add_frontend failed "
435 "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result);
436 goto fail_fe_hw;
439 dvb->fe_mem.source = DMX_MEMORY_FE;
440 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
441 if (result < 0) {
442 printk(KERN_ERR "%s: add_frontend failed "
443 "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result);
444 goto fail_fe_mem;
447 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
448 if (result < 0) {
449 printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
450 DRIVER_NAME, result);
451 goto fail_fe_conn;
454 /* register network adapter */
455 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
456 return 0;
458 fail_fe_conn:
459 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
460 fail_fe_mem:
461 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
462 fail_fe_hw:
463 dvb_dmxdev_release(&dvb->dmxdev);
464 fail_dmxdev:
465 dvb_dmx_release(&dvb->demux);
466 fail_dmx:
467 dvb_unregister_frontend(dvb->frontend);
468 fail_frontend:
469 dvb_frontend_detach(dvb->frontend);
470 dvb_unregister_adapter(&dvb->adapter);
471 fail_adapter:
472 return result;
475 int saa7164_dvb_unregister(struct saa7164_tsport *port)
477 struct saa7164_dvb *dvb = &port->dvb;
478 struct saa7164_dev *dev = port->dev;
479 struct saa7164_buffer *b;
480 struct list_head *c, *n;
482 dprintk(DBGLVL_DVB, "%s()\n", __func__);
484 /* Remove any allocated buffers */
485 mutex_lock(&port->dmaqueue_lock);
486 list_for_each_safe(c, n, &port->dmaqueue.list) {
487 b = list_entry(c, struct saa7164_buffer, list);
488 list_del(c);
489 saa7164_buffer_dealloc(port, b);
491 mutex_unlock(&port->dmaqueue_lock);
493 if (dvb->frontend == NULL)
494 return 0;
496 dvb_net_release(&dvb->net);
497 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
498 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
499 dvb_dmxdev_release(&dvb->dmxdev);
500 dvb_dmx_release(&dvb->demux);
501 dvb_unregister_frontend(dvb->frontend);
502 dvb_frontend_detach(dvb->frontend);
503 dvb_unregister_adapter(&dvb->adapter);
504 return 0;
507 /* All the DVB attach calls go here, this function get's modified
508 * for each new card.
510 int saa7164_dvb_register(struct saa7164_tsport *port)
512 struct saa7164_dev *dev = port->dev;
513 struct saa7164_dvb *dvb = &port->dvb;
514 struct saa7164_i2c *i2c_bus = NULL;
515 int ret;
517 dprintk(DBGLVL_DVB, "%s()\n", __func__);
519 /* init frontend */
520 switch (dev->board) {
521 case SAA7164_BOARD_HAUPPAUGE_HVR2200:
522 case SAA7164_BOARD_HAUPPAUGE_HVR2200_2:
523 case SAA7164_BOARD_HAUPPAUGE_HVR2200_3:
524 i2c_bus = &dev->i2c_bus[port->nr + 1];
525 switch (port->nr) {
526 case 0:
527 port->dvb.frontend = dvb_attach(tda10048_attach,
528 &hauppauge_hvr2200_1_config,
529 &i2c_bus->i2c_adap);
531 if (port->dvb.frontend != NULL) {
532 /* TODO: addr is in the card struct */
533 dvb_attach(tda18271_attach, port->dvb.frontend,
534 0xc0 >> 1, &i2c_bus->i2c_adap,
535 &hauppauge_hvr22x0_tuner_config);
538 break;
539 case 1:
540 port->dvb.frontend = dvb_attach(tda10048_attach,
541 &hauppauge_hvr2200_2_config,
542 &i2c_bus->i2c_adap);
544 if (port->dvb.frontend != NULL) {
545 /* TODO: addr is in the card struct */
546 dvb_attach(tda18271_attach, port->dvb.frontend,
547 0xc0 >> 1, &i2c_bus->i2c_adap,
548 &hauppauge_hvr22x0s_tuner_config);
551 break;
553 break;
554 case SAA7164_BOARD_HAUPPAUGE_HVR2250:
555 case SAA7164_BOARD_HAUPPAUGE_HVR2250_2:
556 case SAA7164_BOARD_HAUPPAUGE_HVR2250_3:
557 i2c_bus = &dev->i2c_bus[port->nr + 1];
559 port->dvb.frontend = dvb_attach(s5h1411_attach,
560 &hauppauge_s5h1411_config,
561 &i2c_bus->i2c_adap);
563 if (port->dvb.frontend != NULL) {
564 if (port->nr == 0) {
565 /* Master TDA18271 */
566 /* TODO: addr is in the card struct */
567 dvb_attach(tda18271_attach, port->dvb.frontend,
568 0xc0 >> 1, &i2c_bus->i2c_adap,
569 &hauppauge_hvr22x0_tuner_config);
570 } else {
571 /* Slave TDA18271 */
572 dvb_attach(tda18271_attach, port->dvb.frontend,
573 0xc0 >> 1, &i2c_bus->i2c_adap,
574 &hauppauge_hvr22x0s_tuner_config);
578 break;
579 default:
580 printk(KERN_ERR "%s: The frontend isn't supported\n",
581 dev->name);
582 break;
584 if (NULL == dvb->frontend) {
585 printk(KERN_ERR "%s() Frontend initialization failed\n",
586 __func__);
587 return -1;
590 /* Put the analog decoder in standby to keep it quiet */
592 /* register everything */
593 ret = dvb_register(port);
594 if (ret < 0) {
595 if (dvb->frontend->ops.release)
596 dvb->frontend->ops.release(dvb->frontend);
597 return ret;
600 return 0;