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.
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
;
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",
95 dprintk(DBGLVL_DVB
, "%s() Stopped\n", __func__
);
102 static int saa7164_dvb_acquire_tsport(struct saa7164_tsport
*port
)
104 struct saa7164_dev
*dev
= port
->dev
;
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",
113 dprintk(DBGLVL_DVB
, "%s() Acquired\n", __func__
);
120 static int saa7164_dvb_pause_tsport(struct saa7164_tsport
*port
)
122 struct saa7164_dev
*dev
= port
->dev
;
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",
131 dprintk(DBGLVL_DVB
, "%s() Paused\n", __func__
);
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
;
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
);
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
;
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
),
194 saa7164_writel(port
->bufptr32l
+ ((sizeof(u32
) * 2) * i
), 0);
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
)
206 saa7164_readl(port
->bufptr32l
+ ((sizeof(u32
) * i
)
209 if (i
++ > port
->hwcfg
.buffercount
)
213 mutex_unlock(&port
->dmaqueue_lock
);
218 static int saa7164_dvb_start_tsport(struct saa7164_tsport
*port
)
220 struct saa7164_dev
*dev
= port
->dev
;
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",
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
);
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",
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
);
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",
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
);
277 dprintk(DBGLVL_DVB
, "%s() Running\n", __func__
);
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
;
291 dprintk(DBGLVL_DVB
, "%s(port=%d)\n", __func__
, port
->nr
);
293 if (!demux
->dmx
.frontend
)
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
);
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
;
318 dprintk(DBGLVL_DVB
, "%s(port=%d)\n", __func__
, port
->nr
);
321 mutex_lock(&dvb
->lock
);
322 if (--dvb
->feeding
== 0) {
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
);
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
;
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) {
346 printk(KERN_ERR
"%s: dvb_register_adapter failed "
347 "(errno = %d), NO PCI configuration\n",
348 DRIVER_NAME
, result
);
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
);
375 printk(KERN_ERR
"%s: dvb_register_adapter failed "
376 "(errno = %d), unable to allocate buffers\n",
377 DRIVER_NAME
, result
);
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
);
391 printk(KERN_ERR
"%s: dvb_register_adapter failed "
392 "(errno = %d)\n", DRIVER_NAME
, result
);
395 dvb
->adapter
.priv
= port
;
397 /* register frontend */
398 result
= dvb_register_frontend(&dvb
->adapter
, dvb
->frontend
);
400 printk(KERN_ERR
"%s: dvb_register_frontend failed "
401 "(errno = %d)\n", DRIVER_NAME
, result
);
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
);
416 printk(KERN_ERR
"%s: dvb_dmx_init failed (errno = %d)\n",
417 DRIVER_NAME
, result
);
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
);
426 printk(KERN_ERR
"%s: dvb_dmxdev_init failed (errno = %d)\n",
427 DRIVER_NAME
, result
);
431 dvb
->fe_hw
.source
= DMX_FRONTEND_0
;
432 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
434 printk(KERN_ERR
"%s: add_frontend failed "
435 "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME
, result
);
439 dvb
->fe_mem
.source
= DMX_MEMORY_FE
;
440 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
442 printk(KERN_ERR
"%s: add_frontend failed "
443 "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME
, result
);
447 result
= dvb
->demux
.dmx
.connect_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
449 printk(KERN_ERR
"%s: connect_frontend failed (errno = %d)\n",
450 DRIVER_NAME
, result
);
454 /* register network adapter */
455 dvb_net_init(&dvb
->adapter
, &dvb
->net
, &dvb
->demux
.dmx
);
459 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
461 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
463 dvb_dmxdev_release(&dvb
->dmxdev
);
465 dvb_dmx_release(&dvb
->demux
);
467 dvb_unregister_frontend(dvb
->frontend
);
469 dvb_frontend_detach(dvb
->frontend
);
470 dvb_unregister_adapter(&dvb
->adapter
);
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
);
489 saa7164_buffer_dealloc(port
, b
);
491 mutex_unlock(&port
->dmaqueue_lock
);
493 if (dvb
->frontend
== NULL
)
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
);
507 /* All the DVB attach calls go here, this function get's modified
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
;
517 dprintk(DBGLVL_DVB
, "%s()\n", __func__
);
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];
527 port
->dvb
.frontend
= dvb_attach(tda10048_attach
,
528 &hauppauge_hvr2200_1_config
,
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
);
540 port
->dvb
.frontend
= dvb_attach(tda10048_attach
,
541 &hauppauge_hvr2200_2_config
,
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
);
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
,
563 if (port
->dvb
.frontend
!= NULL
) {
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
);
572 dvb_attach(tda18271_attach
, port
->dvb
.frontend
,
573 0xc0 >> 1, &i2c_bus
->i2c_adap
,
574 &hauppauge_hvr22x0s_tuner_config
);
580 printk(KERN_ERR
"%s: The frontend isn't supported\n",
584 if (NULL
== dvb
->frontend
) {
585 printk(KERN_ERR
"%s() Frontend initialization failed\n",
590 /* Put the analog decoder in standby to keep it quiet */
592 /* register everything */
593 ret
= dvb_register(port
);
595 if (dvb
->frontend
->ops
.release
)
596 dvb
->frontend
->ops
.release(dvb
->frontend
);