| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1 | /* |
| 2 | * cx18 functions for DVB support |
| 3 | * |
| Steven Toth | 6d89761 | 2008-09-03 17:12:12 -0300 | [diff] [blame] | 4 | * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org> |
| Andy Walls | 1ed9dcc | 2008-11-22 01:37:34 -0300 | [diff] [blame] | 5 | * Copyright (C) 2008 Andy Walls <awalls@radix.net> |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 6 | * |
| 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. |
| 11 | * |
| 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 |
| 15 | * |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 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. |
| 21 | */ |
| 22 | |
| 23 | #include "cx18-version.h" |
| 24 | #include "cx18-dvb.h" |
| Andy Walls | b152642 | 2008-08-30 16:03:44 -0300 | [diff] [blame] | 25 | #include "cx18-io.h" |
| Andy Walls | 21a278b | 2009-04-15 20:45:10 -0300 | [diff] [blame] | 26 | #include "cx18-queue.h" |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 27 | #include "cx18-streams.h" |
| 28 | #include "cx18-cards.h" |
| Andy Walls | 8bb09db | 2009-06-09 20:47:21 -0300 | [diff] [blame^] | 29 | #include "cx18-gpio.h" |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 30 | #include "s5h1409.h" |
| Steven Toth | 6712947 | 2008-05-01 07:23:23 -0300 | [diff] [blame] | 31 | #include "mxl5005s.h" |
| Andy Walls | 8bb09db | 2009-06-09 20:47:21 -0300 | [diff] [blame^] | 32 | #include "zl10353.h" |
| 33 | #include "tuner-xc2028.h" |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 34 | |
| 35 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 36 | |
| 37 | #define CX18_REG_DMUX_NUM_PORT_0_CONTROL 0xd5a000 |
| Andy Walls | 8bb09db | 2009-06-09 20:47:21 -0300 | [diff] [blame^] | 38 | #define CX18_CLOCK_ENABLE2 0xc71024 |
| 39 | #define CX18_DMUX_CLK_MASK 0x0080 |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 40 | |
| Steven Toth | 6712947 | 2008-05-01 07:23:23 -0300 | [diff] [blame] | 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, |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | static struct s5h1409_config hauppauge_hvr1600_config = { |
| 59 | .demod_address = 0x32 >> 1, |
| 60 | .output_mode = S5H1409_SERIAL_OUTPUT, |
| 61 | .gpio = S5H1409_GPIO_ON, |
| 62 | .qam_if = 44000, |
| 63 | .inversion = S5H1409_INVERSION_OFF, |
| 64 | .status_mode = S5H1409_DEMODLOCKING, |
| 65 | .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK |
| Andy Walls | 8bb09db | 2009-06-09 20:47:21 -0300 | [diff] [blame^] | 66 | }; |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 67 | |
| Andy Walls | 8bb09db | 2009-06-09 20:47:21 -0300 | [diff] [blame^] | 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 */ |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 75 | }; |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 76 | |
| 77 | static int dvb_register(struct cx18_stream *stream); |
| 78 | |
| 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. |
| 82 | */ |
| 83 | static int cx18_dvb_start_feed(struct dvb_demux_feed *feed) |
| 84 | { |
| 85 | struct dvb_demux *demux = feed->demux; |
| 86 | struct cx18_stream *stream = (struct cx18_stream *) demux->priv; |
| 87 | struct cx18 *cx = stream->cx; |
| Andy Walls | 08cf7b2 | 2008-06-22 00:04:21 -0300 | [diff] [blame] | 88 | int ret; |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 89 | u32 v; |
| 90 | |
| 91 | CX18_DEBUG_INFO("Start feed: pid = 0x%x index = %d\n", |
| 92 | feed->pid, feed->index); |
| Andy Walls | 08cf7b2 | 2008-06-22 00:04:21 -0300 | [diff] [blame] | 93 | |
| 94 | mutex_lock(&cx->serialize_lock); |
| 95 | ret = cx18_init_on_first_open(cx); |
| 96 | mutex_unlock(&cx->serialize_lock); |
| 97 | if (ret) { |
| 98 | CX18_ERR("Failed to initialize firmware starting DVB feed\n"); |
| 99 | return ret; |
| 100 | } |
| 101 | ret = -EINVAL; |
| 102 | |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 103 | switch (cx->card->type) { |
| 104 | case CX18_CARD_HVR_1600_ESMT: |
| 105 | case CX18_CARD_HVR_1600_SAMSUNG: |
| Andy Walls | b152642 | 2008-08-30 16:03:44 -0300 | [diff] [blame] | 106 | v = cx18_read_reg(cx, CX18_REG_DMUX_NUM_PORT_0_CONTROL); |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 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 */ |
| Andy Walls | b152642 | 2008-08-30 16:03:44 -0300 | [diff] [blame] | 112 | cx18_write_reg(cx, v, CX18_REG_DMUX_NUM_PORT_0_CONTROL); |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 113 | break; |
| 114 | |
| Andy Walls | 8bb09db | 2009-06-09 20:47:21 -0300 | [diff] [blame^] | 115 | case CX18_CARD_LEADTEK_DVR3100H: |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 116 | default: |
| 117 | /* Assumption - Parallel transport - Signalling |
| 118 | * undefined or default. |
| 119 | */ |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | if (!demux->dmx.frontend) |
| 124 | return -EINVAL; |
| 125 | |
| Andy Walls | f68d0cf | 2008-11-05 21:19:15 -0300 | [diff] [blame] | 126 | if (!stream) |
| 127 | return -EINVAL; |
| 128 | |
| 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); |
| 134 | if (ret < 0) { |
| 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); |
| 139 | } |
| 140 | } else |
| 141 | ret = 0; |
| 142 | mutex_unlock(&stream->dvb.feedlock); |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | /* Kernel DVB framework calls this when the feed needs to stop. */ |
| 148 | static int cx18_dvb_stop_feed(struct dvb_demux_feed *feed) |
| 149 | { |
| 150 | struct dvb_demux *demux = feed->demux; |
| 151 | struct cx18_stream *stream = (struct cx18_stream *)demux->priv; |
| 152 | struct cx18 *cx = stream->cx; |
| 153 | int ret = -EINVAL; |
| 154 | |
| 155 | CX18_DEBUG_INFO("Stop feed: pid = 0x%x index = %d\n", |
| 156 | feed->pid, feed->index); |
| 157 | |
| 158 | if (stream) { |
| 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); |
| 163 | } else |
| 164 | ret = 0; |
| 165 | mutex_unlock(&stream->dvb.feedlock); |
| 166 | } |
| 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | int cx18_dvb_register(struct cx18_stream *stream) |
| 172 | { |
| 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; |
| 178 | int ret; |
| 179 | |
| 180 | if (!dvb) |
| 181 | return -EINVAL; |
| 182 | |
| 183 | ret = dvb_register_adapter(&dvb->dvb_adapter, |
| 184 | CX18_DRIVER_NAME, |
| Andy Walls | 3d05913 | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 185 | THIS_MODULE, &cx->pci_dev->dev, adapter_nr); |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 186 | if (ret < 0) |
| 187 | goto err_out; |
| 188 | |
| 189 | dvb_adapter = &dvb->dvb_adapter; |
| 190 | |
| 191 | dvbdemux = &dvb->demux; |
| 192 | |
| 193 | dvbdemux->priv = (void *)stream; |
| 194 | |
| 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); |
| 202 | if (ret < 0) |
| 203 | goto err_dvb_unregister_adapter; |
| 204 | |
| 205 | dmx = &dvbdemux->dmx; |
| 206 | |
| 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; |
| 211 | |
| 212 | ret = dvb_dmxdev_init(&dvb->dmxdev, dvb_adapter); |
| 213 | if (ret < 0) |
| 214 | goto err_dvb_dmx_release; |
| 215 | |
| 216 | ret = dmx->add_frontend(dmx, &dvb->hw_frontend); |
| 217 | if (ret < 0) |
| 218 | goto err_dvb_dmxdev_release; |
| 219 | |
| 220 | ret = dmx->add_frontend(dmx, &dvb->mem_frontend); |
| 221 | if (ret < 0) |
| 222 | goto err_remove_hw_frontend; |
| 223 | |
| 224 | ret = dmx->connect_frontend(dmx, &dvb->hw_frontend); |
| 225 | if (ret < 0) |
| 226 | goto err_remove_mem_frontend; |
| 227 | |
| 228 | ret = dvb_register(stream); |
| 229 | if (ret < 0) |
| 230 | goto err_disconnect_frontend; |
| 231 | |
| 232 | dvb_net_init(dvb_adapter, &dvb->dvbnet, dmx); |
| 233 | |
| 234 | CX18_INFO("DVB Frontend registered\n"); |
| Andy Walls | 6ecd86d | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 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); |
| 238 | |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 239 | mutex_init(&dvb->feedlock); |
| 240 | dvb->enabled = 1; |
| 241 | return ret; |
| 242 | |
| 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); |
| 251 | err_dvb_dmx_release: |
| 252 | dvb_dmx_release(dvbdemux); |
| 253 | err_dvb_unregister_adapter: |
| 254 | dvb_unregister_adapter(dvb_adapter); |
| 255 | err_out: |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | void cx18_dvb_unregister(struct cx18_stream *stream) |
| 260 | { |
| 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; |
| 266 | |
| 267 | CX18_INFO("unregister DVB\n"); |
| 268 | |
| 269 | dvb_adapter = &dvb->dvb_adapter; |
| 270 | dvbdemux = &dvb->demux; |
| 271 | dmx = &dvbdemux->dmx; |
| 272 | |
| 273 | dmx->close(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); |
| 282 | } |
| 283 | |
| 284 | /* All the DVB attach calls go here, this function get's modified |
| Andy Walls | 8bb09db | 2009-06-09 20:47:21 -0300 | [diff] [blame^] | 285 | * for each new card. cx18_dvb_start_feed() will also need changes. |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 286 | */ |
| 287 | static int dvb_register(struct cx18_stream *stream) |
| 288 | { |
| 289 | struct cx18_dvb *dvb = &stream->dvb; |
| 290 | struct cx18 *cx = stream->cx; |
| 291 | int ret = 0; |
| 292 | |
| 293 | switch (cx->card->type) { |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 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, |
| 298 | &cx->i2c_adap[0]); |
| 299 | if (dvb->fe != NULL) { |
| Steven Toth | 6712947 | 2008-05-01 07:23:23 -0300 | [diff] [blame] | 300 | dvb_attach(mxl5005s_attach, dvb->fe, |
| 301 | &cx->i2c_adap[0], |
| 302 | &hauppauge_hvr1600_tuner); |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 303 | ret = 0; |
| 304 | } |
| 305 | break; |
| Andy Walls | 8bb09db | 2009-06-09 20:47:21 -0300 | [diff] [blame^] | 306 | case CX18_CARD_LEADTEK_DVR3100H: |
| 307 | dvb->fe = dvb_attach(zl10353_attach, |
| 308 | &leadtek_dvr3100h_demod, |
| 309 | &cx->i2c_adap[1]); |
| 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, |
| 315 | .ctrl = NULL, |
| 316 | }; |
| 317 | static struct xc2028_ctrl ctrl = { |
| 318 | .fname = XC2028_DEFAULT_FIRMWARE, |
| 319 | .max_len = 64, |
| 320 | .demod = XC3028_FE_ZARLINK456, |
| 321 | .type = XC2028_AUTO, |
| 322 | }; |
| 323 | |
| 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); |
| 327 | } |
| 328 | break; |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 329 | default: |
| 330 | /* No Digital Tv Support */ |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | if (dvb->fe == NULL) { |
| 335 | CX18_ERR("frontend initialization failed\n"); |
| 336 | return -1; |
| 337 | } |
| 338 | |
| Andy Walls | 8bb09db | 2009-06-09 20:47:21 -0300 | [diff] [blame^] | 339 | dvb->fe->callback = cx18_reset_tuner_gpio; |
| 340 | |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 341 | ret = dvb_register_frontend(&dvb->dvb_adapter, dvb->fe); |
| 342 | if (ret < 0) { |
| 343 | if (dvb->fe->ops.release) |
| 344 | dvb->fe->ops.release(dvb->fe); |
| 345 | return ret; |
| 346 | } |
| 347 | |
| Andy Walls | 8bb09db | 2009-06-09 20:47:21 -0300 | [diff] [blame^] | 348 | /* |
| 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. |
| 352 | */ |
| 353 | cx18_write_reg_expect(cx, |
| 354 | (CX18_DMUX_CLK_MASK << 16) | CX18_DMUX_CLK_MASK, |
| 355 | CX18_CLOCK_ENABLE2, |
| 356 | CX18_DMUX_CLK_MASK, |
| 357 | (CX18_DMUX_CLK_MASK << 16) | CX18_DMUX_CLK_MASK); |
| 358 | |
| Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 359 | return ret; |
| 360 | } |