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"
33 #include "tuner-xc2028.h"
35 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
37 #define CX18_REG_DMUX_NUM_PORT_0_CONTROL 0xd5a000
38 #define CX18_CLOCK_ENABLE2 0xc71024
39 #define CX18_DMUX_CLK_MASK 0x0080
41 static struct mxl5005s_config hauppauge_hvr1600_tuner = {
42 .i2c_address = 0xC6 >> 1,
43 .if_freq = IF_FREQ_5380000HZ,
44 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
45 .agc_mode = MXL_SINGLE_AGC,
46 .tracking_filter = MXL_TF_C_H,
47 .rssi_enable = MXL_RSSI_ENABLE,
48 .cap_select = MXL_CAP_SEL_ENABLE,
49 .div_out = MXL_DIV_OUT_4,
50 .clock_out = MXL_CLOCK_OUT_DISABLE,
51 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
52 .top = MXL5005S_TOP_25P2,
53 .mod_mode = MXL_DIGITAL_MODE,
54 .if_mode = MXL_ZERO_IF,
55 .AgcMasterByte = 0x00,
58 static struct s5h1409_config hauppauge_hvr1600_config = {
59 .demod_address = 0x32 >> 1,
60 .output_mode = S5H1409_SERIAL_OUTPUT,
61 .gpio = S5H1409_GPIO_ON,
63 .inversion = S5H1409_INVERSION_OFF,
64 .status_mode = S5H1409_DEMODLOCKING,
65 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
68 /* Information/confirmation of proper config values provided by Terry Wu */
69 static struct zl10353_config leadtek_dvr3100h_demod = {
70 .demod_address = 0x1e >> 1, /* Datasheet suggested straps */
71 .if2 = 45600, /* 4.560 MHz IF from the XC3028 */
72 .parallel_ts = 1, /* Not a serial TS */
73 .no_tuner = 1, /* XC3028 is not behind the gate */
74 .disable_i2c_gate_ctrl = 1, /* Disable the I2C gate */
77 static int dvb_register(struct cx18_stream *stream);
79 /* Kernel DVB framework calls this when the feed needs to start.
80 * The CX18 framework should enable the transport DMA handling
81 * and queue processing.
83 static int cx18_dvb_start_feed(struct dvb_demux_feed *feed)
85 struct dvb_demux *demux = feed->demux;
86 struct cx18_stream *stream = (struct cx18_stream *) demux->priv;
87 struct cx18 *cx = stream->cx;
91 CX18_DEBUG_INFO("Start feed: pid = 0x%x index = %d\n",
92 feed->pid, feed->index);
94 mutex_lock(&cx->serialize_lock);
95 ret = cx18_init_on_first_open(cx);
96 mutex_unlock(&cx->serialize_lock);
98 CX18_ERR("Failed to initialize firmware starting DVB feed\n");
103 switch (cx->card->type) {
104 case CX18_CARD_HVR_1600_ESMT:
105 case CX18_CARD_HVR_1600_SAMSUNG:
106 v = cx18_read_reg(cx, CX18_REG_DMUX_NUM_PORT_0_CONTROL);
107 v |= 0x00400000; /* Serial Mode */
108 v |= 0x00002000; /* Data Length - Byte */
109 v |= 0x00010000; /* Error - Polarity */
110 v |= 0x00020000; /* Error - Passthru */
111 v |= 0x000c0000; /* Error - Ignore */
112 cx18_write_reg(cx, v, CX18_REG_DMUX_NUM_PORT_0_CONTROL);
115 case CX18_CARD_LEADTEK_DVR3100H:
117 /* Assumption - Parallel transport - Signalling
118 * undefined or default.
123 if (!demux->dmx.frontend)
129 mutex_lock(&stream->dvb.feedlock);
130 if (stream->dvb.feeding++ == 0) {
131 CX18_DEBUG_INFO("Starting Transport DMA\n");
132 set_bit(CX18_F_S_STREAMING, &stream->s_flags);
133 ret = cx18_start_v4l2_encode_stream(stream);
135 CX18_DEBUG_INFO("Failed to start Transport DMA\n");
136 stream->dvb.feeding--;
137 if (stream->dvb.feeding == 0)
138 clear_bit(CX18_F_S_STREAMING, &stream->s_flags);
142 mutex_unlock(&stream->dvb.feedlock);
147 /* Kernel DVB framework calls this when the feed needs to stop. */
148 static int cx18_dvb_stop_feed(struct dvb_demux_feed *feed)
150 struct dvb_demux *demux = feed->demux;
151 struct cx18_stream *stream = (struct cx18_stream *)demux->priv;
152 struct cx18 *cx = stream->cx;
155 CX18_DEBUG_INFO("Stop feed: pid = 0x%x index = %d\n",
156 feed->pid, feed->index);
159 mutex_lock(&stream->dvb.feedlock);
160 if (--stream->dvb.feeding == 0) {
161 CX18_DEBUG_INFO("Stopping Transport DMA\n");
162 ret = cx18_stop_v4l2_encode_stream(stream, 0);
165 mutex_unlock(&stream->dvb.feedlock);
171 int cx18_dvb_register(struct cx18_stream *stream)
173 struct cx18 *cx = stream->cx;
174 struct cx18_dvb *dvb = &stream->dvb;
175 struct dvb_adapter *dvb_adapter;
176 struct dvb_demux *dvbdemux;
177 struct dmx_demux *dmx;
183 ret = dvb_register_adapter(&dvb->dvb_adapter,
185 THIS_MODULE, &cx->pci_dev->dev, adapter_nr);
189 dvb_adapter = &dvb->dvb_adapter;
191 dvbdemux = &dvb->demux;
193 dvbdemux->priv = (void *)stream;
195 dvbdemux->filternum = 256;
196 dvbdemux->feednum = 256;
197 dvbdemux->start_feed = cx18_dvb_start_feed;
198 dvbdemux->stop_feed = cx18_dvb_stop_feed;
199 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
200 DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);
201 ret = dvb_dmx_init(dvbdemux);
203 goto err_dvb_unregister_adapter;
205 dmx = &dvbdemux->dmx;
207 dvb->hw_frontend.source = DMX_FRONTEND_0;
208 dvb->mem_frontend.source = DMX_MEMORY_FE;
209 dvb->dmxdev.filternum = 256;
210 dvb->dmxdev.demux = dmx;
212 ret = dvb_dmxdev_init(&dvb->dmxdev, dvb_adapter);
214 goto err_dvb_dmx_release;
216 ret = dmx->add_frontend(dmx, &dvb->hw_frontend);
218 goto err_dvb_dmxdev_release;
220 ret = dmx->add_frontend(dmx, &dvb->mem_frontend);
222 goto err_remove_hw_frontend;
224 ret = dmx->connect_frontend(dmx, &dvb->hw_frontend);
226 goto err_remove_mem_frontend;
228 ret = dvb_register(stream);
230 goto err_disconnect_frontend;
232 dvb_net_init(dvb_adapter, &dvb->dvbnet, dmx);
234 CX18_INFO("DVB Frontend registered\n");
235 CX18_INFO("Registered DVB adapter%d for %s (%d x %d kB)\n",
236 stream->dvb.dvb_adapter.num, stream->name,
237 stream->buffers, stream->buf_size/1024);
239 mutex_init(&dvb->feedlock);
243 err_disconnect_frontend:
244 dmx->disconnect_frontend(dmx);
245 err_remove_mem_frontend:
246 dmx->remove_frontend(dmx, &dvb->mem_frontend);
247 err_remove_hw_frontend:
248 dmx->remove_frontend(dmx, &dvb->hw_frontend);
249 err_dvb_dmxdev_release:
250 dvb_dmxdev_release(&dvb->dmxdev);
252 dvb_dmx_release(dvbdemux);
253 err_dvb_unregister_adapter:
254 dvb_unregister_adapter(dvb_adapter);
259 void cx18_dvb_unregister(struct cx18_stream *stream)
261 struct cx18 *cx = stream->cx;
262 struct cx18_dvb *dvb = &stream->dvb;
263 struct dvb_adapter *dvb_adapter;
264 struct dvb_demux *dvbdemux;
265 struct dmx_demux *dmx;
267 CX18_INFO("unregister DVB\n");
269 dvb_adapter = &dvb->dvb_adapter;
270 dvbdemux = &dvb->demux;
271 dmx = &dvbdemux->dmx;
274 dvb_net_release(&dvb->dvbnet);
275 dmx->remove_frontend(dmx, &dvb->mem_frontend);
276 dmx->remove_frontend(dmx, &dvb->hw_frontend);
277 dvb_dmxdev_release(&dvb->dmxdev);
278 dvb_dmx_release(dvbdemux);
279 dvb_unregister_frontend(dvb->fe);
280 dvb_frontend_detach(dvb->fe);
281 dvb_unregister_adapter(dvb_adapter);
284 /* All the DVB attach calls go here, this function get's modified
285 * for each new card. cx18_dvb_start_feed() will also need changes.
287 static int dvb_register(struct cx18_stream *stream)
289 struct cx18_dvb *dvb = &stream->dvb;
290 struct cx18 *cx = stream->cx;
293 switch (cx->card->type) {
294 case CX18_CARD_HVR_1600_ESMT:
295 case CX18_CARD_HVR_1600_SAMSUNG:
296 dvb->fe = dvb_attach(s5h1409_attach,
297 &hauppauge_hvr1600_config,
299 if (dvb->fe != NULL) {
300 dvb_attach(mxl5005s_attach, dvb->fe,
302 &hauppauge_hvr1600_tuner);
306 case CX18_CARD_LEADTEK_DVR3100H:
307 dvb->fe = dvb_attach(zl10353_attach,
308 &leadtek_dvr3100h_demod,
310 if (dvb->fe != NULL) {
311 struct dvb_frontend *fe;
312 struct xc2028_config cfg = {
313 .i2c_adap = &cx->i2c_adap[1],
314 .i2c_addr = 0xc2 >> 1,
317 static struct xc2028_ctrl ctrl = {
318 .fname = XC2028_DEFAULT_FIRMWARE,
320 .demod = XC3028_FE_ZARLINK456,
324 fe = dvb_attach(xc2028_attach, dvb->fe, &cfg);
325 if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)
326 fe->ops.tuner_ops.set_config(fe, &ctrl);
330 /* No Digital Tv Support */
334 if (dvb->fe == NULL) {
335 CX18_ERR("frontend initialization failed\n");
339 dvb->fe->callback = cx18_reset_tuner_gpio;
341 ret = dvb_register_frontend(&dvb->dvb_adapter, dvb->fe);
343 if (dvb->fe->ops.release)
344 dvb->fe->ops.release(dvb->fe);
349 * The firmware seems to enable the TS DMUX clock
350 * under various circumstances. However, since we know we
351 * might use it, let's just turn it on ourselves here.
353 cx18_write_reg_expect(cx,
354 (CX18_DMUX_CLK_MASK << 16) | CX18_DMUX_CLK_MASK,
357 (CX18_DMUX_CLK_MASK << 16) | CX18_DMUX_CLK_MASK);