2 * cx18 functions for DVB support
4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
5 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "cx18-version.h"
26 #include "cx18-queue.h"
27 #include "cx18-streams.h"
28 #include "cx18-cards.h"
29 #include "cx18-gpio.h"
34 #include <linux/firmware.h>
36 #include "mt352_priv.h"
37 #include "tuner-xc2028.h"
39 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
41 #define CX18_REG_DMUX_NUM_PORT_0_CONTROL 0xd5a000
42 #define CX18_CLOCK_ENABLE2 0xc71024
43 #define CX18_DMUX_CLK_MASK 0x0080
46 * CX18_CARD_HVR_1600_ESMT
47 * CX18_CARD_HVR_1600_SAMSUNG
50 static struct mxl5005s_config hauppauge_hvr1600_tuner
= {
51 .i2c_address
= 0xC6 >> 1,
52 .if_freq
= IF_FREQ_5380000HZ
,
53 .xtal_freq
= CRYSTAL_FREQ_16000000HZ
,
54 .agc_mode
= MXL_SINGLE_AGC
,
55 .tracking_filter
= MXL_TF_C_H
,
56 .rssi_enable
= MXL_RSSI_ENABLE
,
57 .cap_select
= MXL_CAP_SEL_ENABLE
,
58 .div_out
= MXL_DIV_OUT_4
,
59 .clock_out
= MXL_CLOCK_OUT_DISABLE
,
60 .output_load
= MXL5005S_IF_OUTPUT_LOAD_200_OHM
,
61 .top
= MXL5005S_TOP_25P2
,
62 .mod_mode
= MXL_DIGITAL_MODE
,
63 .if_mode
= MXL_ZERO_IF
,
65 .AgcMasterByte
= 0x00,
68 static struct s5h1409_config hauppauge_hvr1600_config
= {
69 .demod_address
= 0x32 >> 1,
70 .output_mode
= S5H1409_SERIAL_OUTPUT
,
71 .gpio
= S5H1409_GPIO_ON
,
73 .inversion
= S5H1409_INVERSION_OFF
,
74 .status_mode
= S5H1409_DEMODLOCKING
,
75 .mpeg_timing
= S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
,
76 .hvr1600_opt
= S5H1409_HVR1600_OPTIMIZE
80 * CX18_CARD_LEADTEK_DVR3100H
82 /* Information/confirmation of proper config values provided by Terry Wu */
83 static struct zl10353_config leadtek_dvr3100h_demod
= {
84 .demod_address
= 0x1e >> 1, /* Datasheet suggested straps */
85 .if2
= 45600, /* 4.560 MHz IF from the XC3028 */
86 .parallel_ts
= 1, /* Not a serial TS */
87 .no_tuner
= 1, /* XC3028 is not behind the gate */
88 .disable_i2c_gate_ctrl
= 1, /* Disable the I2C gate */
92 * CX18_CARD_YUAN_MPC718
97 * 1. an absence of information on how to prgram the MT352
98 * 2. the Linux mt352 module pushing MT352 initialzation off onto us here
100 * We have to use an init sequence that *you* must extract from the Windows
101 * driver (yuanrap.sys) and which we load as a firmware.
103 * If someone can provide me with a Zarlink MT352 (Intel CE6352?) Design Manual
104 * with chip programming details, then I can remove this annoyance.
106 static int yuan_mpc718_mt352_reqfw(struct cx18_stream
*stream
,
107 const struct firmware
**fw
)
109 struct cx18
*cx
= stream
->cx
;
110 const char *fn
= "dvb-cx18-mpc718-mt352.fw";
113 ret
= request_firmware(fw
, fn
, &cx
->pci_dev
->dev
);
115 CX18_ERR("Unable to open firmware file %s\n", fn
);
117 size_t sz
= (*fw
)->size
;
118 if (sz
< 2 || sz
> 64 || (sz
% 2) != 0) {
119 CX18_ERR("Firmware %s has a bad size: %lu bytes\n",
120 fn
, (unsigned long) sz
);
122 release_firmware(*fw
);
128 CX18_ERR("The MPC718 board variant with the MT352 DVB-T"
129 "demodualtor will not work without it\n");
130 CX18_ERR("Run 'linux/Documentation/dvb/get_dvb_firmware "
131 "mpc718' if you need the firmware\n");
136 static int yuan_mpc718_mt352_init(struct dvb_frontend
*fe
)
138 struct cx18_dvb
*dvb
= container_of(fe
->dvb
,
139 struct cx18_dvb
, dvb_adapter
);
140 struct cx18_stream
*stream
= container_of(dvb
, struct cx18_stream
, dvb
);
141 const struct firmware
*fw
= NULL
;
146 ret
= yuan_mpc718_mt352_reqfw(stream
, &fw
);
150 /* Loop through all the register-value pairs in the firmware file */
151 for (i
= 0; i
< fw
->size
; i
+= 2) {
152 buf
[0] = fw
->data
[i
];
153 /* Intercept a few registers we want to set ourselves */
155 case TRL_NOMINAL_RATE_0
:
156 /* Set our custom OFDM bandwidth in the case below */
158 case TRL_NOMINAL_RATE_1
:
159 /* 6 MHz: 64/7 * 6/8 / 20.48 * 2^16 = 0x55b6.db6 */
160 /* 7 MHz: 64/7 * 7/8 / 20.48 * 2^16 = 0x6400 */
161 /* 8 MHz: 64/7 * 8/8 / 20.48 * 2^16 = 0x7249.249 */
164 mt352_write(fe
, buf
, 3);
167 /* Set our custom IF in the case below */
170 /* 4.56 MHz IF: (20.48 - 4.56)/20.48 * 2^14 = 0x31c0 */
173 mt352_write(fe
, buf
, 3);
176 /* Pass through the register-value pair from the fw */
177 buf
[1] = fw
->data
[i
+1];
178 mt352_write(fe
, buf
, 2);
183 buf
[0] = (u8
) TUNER_GO
;
184 buf
[1] = 0x01; /* Go */
185 mt352_write(fe
, buf
, 2);
186 release_firmware(fw
);
190 static struct mt352_config yuan_mpc718_mt352_demod
= {
191 .demod_address
= 0x1e >> 1,
192 .adc_clock
= 20480, /* 20.480 MHz */
193 .if2
= 4560, /* 4.560 MHz */
194 .no_tuner
= 1, /* XC3028 is not behind the gate */
195 .demod_init
= yuan_mpc718_mt352_init
,
198 static struct zl10353_config yuan_mpc718_zl10353_demod
= {
199 .demod_address
= 0x1e >> 1, /* Datasheet suggested straps */
200 .if2
= 45600, /* 4.560 MHz IF from the XC3028 */
201 .parallel_ts
= 1, /* Not a serial TS */
202 .no_tuner
= 1, /* XC3028 is not behind the gate */
203 .disable_i2c_gate_ctrl
= 1, /* Disable the I2C gate */
206 static int dvb_register(struct cx18_stream
*stream
);
208 /* Kernel DVB framework calls this when the feed needs to start.
209 * The CX18 framework should enable the transport DMA handling
210 * and queue processing.
212 static int cx18_dvb_start_feed(struct dvb_demux_feed
*feed
)
214 struct dvb_demux
*demux
= feed
->demux
;
215 struct cx18_stream
*stream
= (struct cx18_stream
*) demux
->priv
;
224 CX18_DEBUG_INFO("Start feed: pid = 0x%x index = %d\n",
225 feed
->pid
, feed
->index
);
227 mutex_lock(&cx
->serialize_lock
);
228 ret
= cx18_init_on_first_open(cx
);
229 mutex_unlock(&cx
->serialize_lock
);
231 CX18_ERR("Failed to initialize firmware starting DVB feed\n");
236 switch (cx
->card
->type
) {
237 case CX18_CARD_HVR_1600_ESMT
:
238 case CX18_CARD_HVR_1600_SAMSUNG
:
239 v
= cx18_read_reg(cx
, CX18_REG_DMUX_NUM_PORT_0_CONTROL
);
240 v
|= 0x00400000; /* Serial Mode */
241 v
|= 0x00002000; /* Data Length - Byte */
242 v
|= 0x00010000; /* Error - Polarity */
243 v
|= 0x00020000; /* Error - Passthru */
244 v
|= 0x000c0000; /* Error - Ignore */
245 cx18_write_reg(cx
, v
, CX18_REG_DMUX_NUM_PORT_0_CONTROL
);
248 case CX18_CARD_LEADTEK_DVR3100H
:
249 case CX18_CARD_YUAN_MPC718
:
251 /* Assumption - Parallel transport - Signalling
252 * undefined or default.
257 if (!demux
->dmx
.frontend
)
260 mutex_lock(&stream
->dvb
.feedlock
);
261 if (stream
->dvb
.feeding
++ == 0) {
262 CX18_DEBUG_INFO("Starting Transport DMA\n");
263 mutex_lock(&cx
->serialize_lock
);
264 set_bit(CX18_F_S_STREAMING
, &stream
->s_flags
);
265 ret
= cx18_start_v4l2_encode_stream(stream
);
267 CX18_DEBUG_INFO("Failed to start Transport DMA\n");
268 stream
->dvb
.feeding
--;
269 if (stream
->dvb
.feeding
== 0)
270 clear_bit(CX18_F_S_STREAMING
, &stream
->s_flags
);
272 mutex_unlock(&cx
->serialize_lock
);
275 mutex_unlock(&stream
->dvb
.feedlock
);
280 /* Kernel DVB framework calls this when the feed needs to stop. */
281 static int cx18_dvb_stop_feed(struct dvb_demux_feed
*feed
)
283 struct dvb_demux
*demux
= feed
->demux
;
284 struct cx18_stream
*stream
= (struct cx18_stream
*)demux
->priv
;
290 CX18_DEBUG_INFO("Stop feed: pid = 0x%x index = %d\n",
291 feed
->pid
, feed
->index
);
293 mutex_lock(&stream
->dvb
.feedlock
);
294 if (--stream
->dvb
.feeding
== 0) {
295 CX18_DEBUG_INFO("Stopping Transport DMA\n");
296 mutex_lock(&cx
->serialize_lock
);
297 ret
= cx18_stop_v4l2_encode_stream(stream
, 0);
298 mutex_unlock(&cx
->serialize_lock
);
301 mutex_unlock(&stream
->dvb
.feedlock
);
307 int cx18_dvb_register(struct cx18_stream
*stream
)
309 struct cx18
*cx
= stream
->cx
;
310 struct cx18_dvb
*dvb
= &stream
->dvb
;
311 struct dvb_adapter
*dvb_adapter
;
312 struct dvb_demux
*dvbdemux
;
313 struct dmx_demux
*dmx
;
319 ret
= dvb_register_adapter(&dvb
->dvb_adapter
,
321 THIS_MODULE
, &cx
->pci_dev
->dev
, adapter_nr
);
325 dvb_adapter
= &dvb
->dvb_adapter
;
327 dvbdemux
= &dvb
->demux
;
329 dvbdemux
->priv
= (void *)stream
;
331 dvbdemux
->filternum
= 256;
332 dvbdemux
->feednum
= 256;
333 dvbdemux
->start_feed
= cx18_dvb_start_feed
;
334 dvbdemux
->stop_feed
= cx18_dvb_stop_feed
;
335 dvbdemux
->dmx
.capabilities
= (DMX_TS_FILTERING
|
336 DMX_SECTION_FILTERING
| DMX_MEMORY_BASED_FILTERING
);
337 ret
= dvb_dmx_init(dvbdemux
);
339 goto err_dvb_unregister_adapter
;
341 dmx
= &dvbdemux
->dmx
;
343 dvb
->hw_frontend
.source
= DMX_FRONTEND_0
;
344 dvb
->mem_frontend
.source
= DMX_MEMORY_FE
;
345 dvb
->dmxdev
.filternum
= 256;
346 dvb
->dmxdev
.demux
= dmx
;
348 ret
= dvb_dmxdev_init(&dvb
->dmxdev
, dvb_adapter
);
350 goto err_dvb_dmx_release
;
352 ret
= dmx
->add_frontend(dmx
, &dvb
->hw_frontend
);
354 goto err_dvb_dmxdev_release
;
356 ret
= dmx
->add_frontend(dmx
, &dvb
->mem_frontend
);
358 goto err_remove_hw_frontend
;
360 ret
= dmx
->connect_frontend(dmx
, &dvb
->hw_frontend
);
362 goto err_remove_mem_frontend
;
364 ret
= dvb_register(stream
);
366 goto err_disconnect_frontend
;
368 dvb_net_init(dvb_adapter
, &dvb
->dvbnet
, dmx
);
370 CX18_INFO("DVB Frontend registered\n");
371 CX18_INFO("Registered DVB adapter%d for %s (%d x %d.%02d kB)\n",
372 stream
->dvb
.dvb_adapter
.num
, stream
->name
,
373 stream
->buffers
, stream
->buf_size
/1024,
374 (stream
->buf_size
* 100 / 1024) % 100);
376 mutex_init(&dvb
->feedlock
);
380 err_disconnect_frontend
:
381 dmx
->disconnect_frontend(dmx
);
382 err_remove_mem_frontend
:
383 dmx
->remove_frontend(dmx
, &dvb
->mem_frontend
);
384 err_remove_hw_frontend
:
385 dmx
->remove_frontend(dmx
, &dvb
->hw_frontend
);
386 err_dvb_dmxdev_release
:
387 dvb_dmxdev_release(&dvb
->dmxdev
);
389 dvb_dmx_release(dvbdemux
);
390 err_dvb_unregister_adapter
:
391 dvb_unregister_adapter(dvb_adapter
);
396 void cx18_dvb_unregister(struct cx18_stream
*stream
)
398 struct cx18
*cx
= stream
->cx
;
399 struct cx18_dvb
*dvb
= &stream
->dvb
;
400 struct dvb_adapter
*dvb_adapter
;
401 struct dvb_demux
*dvbdemux
;
402 struct dmx_demux
*dmx
;
404 CX18_INFO("unregister DVB\n");
406 dvb_adapter
= &dvb
->dvb_adapter
;
407 dvbdemux
= &dvb
->demux
;
408 dmx
= &dvbdemux
->dmx
;
411 dvb_net_release(&dvb
->dvbnet
);
412 dmx
->remove_frontend(dmx
, &dvb
->mem_frontend
);
413 dmx
->remove_frontend(dmx
, &dvb
->hw_frontend
);
414 dvb_dmxdev_release(&dvb
->dmxdev
);
415 dvb_dmx_release(dvbdemux
);
416 dvb_unregister_frontend(dvb
->fe
);
417 dvb_frontend_detach(dvb
->fe
);
418 dvb_unregister_adapter(dvb_adapter
);
421 /* All the DVB attach calls go here, this function get's modified
422 * for each new card. cx18_dvb_start_feed() will also need changes.
424 static int dvb_register(struct cx18_stream
*stream
)
426 struct cx18_dvb
*dvb
= &stream
->dvb
;
427 struct cx18
*cx
= stream
->cx
;
430 switch (cx
->card
->type
) {
431 case CX18_CARD_HVR_1600_ESMT
:
432 case CX18_CARD_HVR_1600_SAMSUNG
:
433 dvb
->fe
= dvb_attach(s5h1409_attach
,
434 &hauppauge_hvr1600_config
,
436 if (dvb
->fe
!= NULL
) {
437 dvb_attach(mxl5005s_attach
, dvb
->fe
,
439 &hauppauge_hvr1600_tuner
);
443 case CX18_CARD_LEADTEK_DVR3100H
:
444 dvb
->fe
= dvb_attach(zl10353_attach
,
445 &leadtek_dvr3100h_demod
,
447 if (dvb
->fe
!= NULL
) {
448 struct dvb_frontend
*fe
;
449 struct xc2028_config cfg
= {
450 .i2c_adap
= &cx
->i2c_adap
[1],
451 .i2c_addr
= 0xc2 >> 1,
454 static struct xc2028_ctrl ctrl
= {
455 .fname
= XC2028_DEFAULT_FIRMWARE
,
457 .demod
= XC3028_FE_ZARLINK456
,
461 fe
= dvb_attach(xc2028_attach
, dvb
->fe
, &cfg
);
462 if (fe
!= NULL
&& fe
->ops
.tuner_ops
.set_config
!= NULL
)
463 fe
->ops
.tuner_ops
.set_config(fe
, &ctrl
);
466 case CX18_CARD_YUAN_MPC718
:
469 * Apparently, these cards also could instead have a
470 * DiBcom demod supported by one of the db7000 drivers
472 dvb
->fe
= dvb_attach(mt352_attach
,
473 &yuan_mpc718_mt352_demod
,
476 dvb
->fe
= dvb_attach(zl10353_attach
,
477 &yuan_mpc718_zl10353_demod
,
479 if (dvb
->fe
!= NULL
) {
480 struct dvb_frontend
*fe
;
481 struct xc2028_config cfg
= {
482 .i2c_adap
= &cx
->i2c_adap
[1],
483 .i2c_addr
= 0xc2 >> 1,
486 static struct xc2028_ctrl ctrl
= {
487 .fname
= XC2028_DEFAULT_FIRMWARE
,
489 .demod
= XC3028_FE_ZARLINK456
,
493 fe
= dvb_attach(xc2028_attach
, dvb
->fe
, &cfg
);
494 if (fe
!= NULL
&& fe
->ops
.tuner_ops
.set_config
!= NULL
)
495 fe
->ops
.tuner_ops
.set_config(fe
, &ctrl
);
499 /* No Digital Tv Support */
503 if (dvb
->fe
== NULL
) {
504 CX18_ERR("frontend initialization failed\n");
508 dvb
->fe
->callback
= cx18_reset_tuner_gpio
;
510 ret
= dvb_register_frontend(&dvb
->dvb_adapter
, dvb
->fe
);
512 if (dvb
->fe
->ops
.release
)
513 dvb
->fe
->ops
.release(dvb
->fe
);
518 * The firmware seems to enable the TS DMUX clock
519 * under various circumstances. However, since we know we
520 * might use it, let's just turn it on ourselves here.
522 cx18_write_reg_expect(cx
,
523 (CX18_DMUX_CLK_MASK
<< 16) | CX18_DMUX_CLK_MASK
,
526 (CX18_DMUX_CLK_MASK
<< 16) | CX18_DMUX_CLK_MASK
);