Path: blob/master/drivers/media/video/cx18/cx18-dvb.c
17683 views
/*1* cx18 functions for DVB support2*3* Copyright (c) 2008 Steven Toth <[email protected]>4* Copyright (C) 2008 Andy Walls <[email protected]>5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License as published by8* the Free Software Foundation; either version 2 of the License, or9* (at your option) any later version.10*11* This program is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14*15* GNU General Public License for more details.16*17* You should have received a copy of the GNU General Public License18* along with this program; if not, write to the Free Software19* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.20*/2122#include "cx18-version.h"23#include "cx18-dvb.h"24#include "cx18-io.h"25#include "cx18-queue.h"26#include "cx18-streams.h"27#include "cx18-cards.h"28#include "cx18-gpio.h"29#include "s5h1409.h"30#include "mxl5005s.h"31#include "s5h1411.h"32#include "tda18271.h"33#include "zl10353.h"3435#include <linux/firmware.h>36#include "mt352.h"37#include "mt352_priv.h"38#include "tuner-xc2028.h"3940DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);4142#define CX18_REG_DMUX_NUM_PORT_0_CONTROL 0xd5a00043#define CX18_CLOCK_ENABLE2 0xc7102444#define CX18_DMUX_CLK_MASK 0x00804546/*47* CX18_CARD_HVR_1600_ESMT48* CX18_CARD_HVR_1600_SAMSUNG49*/5051static struct mxl5005s_config hauppauge_hvr1600_tuner = {52.i2c_address = 0xC6 >> 1,53.if_freq = IF_FREQ_5380000HZ,54.xtal_freq = CRYSTAL_FREQ_16000000HZ,55.agc_mode = MXL_SINGLE_AGC,56.tracking_filter = MXL_TF_C_H,57.rssi_enable = MXL_RSSI_ENABLE,58.cap_select = MXL_CAP_SEL_ENABLE,59.div_out = MXL_DIV_OUT_4,60.clock_out = MXL_CLOCK_OUT_DISABLE,61.output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,62.top = MXL5005S_TOP_25P2,63.mod_mode = MXL_DIGITAL_MODE,64.if_mode = MXL_ZERO_IF,65.qam_gain = 0x02,66.AgcMasterByte = 0x00,67};6869static struct s5h1409_config hauppauge_hvr1600_config = {70.demod_address = 0x32 >> 1,71.output_mode = S5H1409_SERIAL_OUTPUT,72.gpio = S5H1409_GPIO_ON,73.qam_if = 44000,74.inversion = S5H1409_INVERSION_OFF,75.status_mode = S5H1409_DEMODLOCKING,76.mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,77.hvr1600_opt = S5H1409_HVR1600_OPTIMIZE78};7980/*81* CX18_CARD_HVR_1600_S5H141182*/83static struct s5h1411_config hcw_s5h1411_config = {84.output_mode = S5H1411_SERIAL_OUTPUT,85.gpio = S5H1411_GPIO_OFF,86.vsb_if = S5H1411_IF_44000,87.qam_if = S5H1411_IF_4000,88.inversion = S5H1411_INVERSION_ON,89.status_mode = S5H1411_DEMODLOCKING,90.mpeg_timing = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,91};9293static struct tda18271_std_map hauppauge_tda18271_std_map = {94.atsc_6 = { .if_freq = 5380, .agc_mode = 3, .std = 3,95.if_lvl = 6, .rfagc_top = 0x37 },96.qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 0,97.if_lvl = 6, .rfagc_top = 0x37 },98};99100static struct tda18271_config hauppauge_tda18271_config = {101.std_map = &hauppauge_tda18271_std_map,102.gate = TDA18271_GATE_DIGITAL,103.output_opt = TDA18271_OUTPUT_LT_OFF,104};105106/*107* CX18_CARD_LEADTEK_DVR3100H108*/109/* Information/confirmation of proper config values provided by Terry Wu */110static struct zl10353_config leadtek_dvr3100h_demod = {111.demod_address = 0x1e >> 1, /* Datasheet suggested straps */112.if2 = 45600, /* 4.560 MHz IF from the XC3028 */113.parallel_ts = 1, /* Not a serial TS */114.no_tuner = 1, /* XC3028 is not behind the gate */115.disable_i2c_gate_ctrl = 1, /* Disable the I2C gate */116};117118/*119* CX18_CARD_YUAN_MPC718120*/121/*122* Due to123*124* 1. an absence of information on how to prgram the MT352125* 2. the Linux mt352 module pushing MT352 initialzation off onto us here126*127* We have to use an init sequence that *you* must extract from the Windows128* driver (yuanrap.sys) and which we load as a firmware.129*130* If someone can provide me with a Zarlink MT352 (Intel CE6352?) Design Manual131* with chip programming details, then I can remove this annoyance.132*/133static int yuan_mpc718_mt352_reqfw(struct cx18_stream *stream,134const struct firmware **fw)135{136struct cx18 *cx = stream->cx;137const char *fn = "dvb-cx18-mpc718-mt352.fw";138int ret;139140ret = request_firmware(fw, fn, &cx->pci_dev->dev);141if (ret)142CX18_ERR("Unable to open firmware file %s\n", fn);143else {144size_t sz = (*fw)->size;145if (sz < 2 || sz > 64 || (sz % 2) != 0) {146CX18_ERR("Firmware %s has a bad size: %lu bytes\n",147fn, (unsigned long) sz);148ret = -EILSEQ;149release_firmware(*fw);150*fw = NULL;151}152}153154if (ret) {155CX18_ERR("The MPC718 board variant with the MT352 DVB-T"156"demodualtor will not work without it\n");157CX18_ERR("Run 'linux/Documentation/dvb/get_dvb_firmware "158"mpc718' if you need the firmware\n");159}160return ret;161}162163static int yuan_mpc718_mt352_init(struct dvb_frontend *fe)164{165struct cx18_dvb *dvb = container_of(fe->dvb,166struct cx18_dvb, dvb_adapter);167struct cx18_stream *stream = dvb->stream;168const struct firmware *fw = NULL;169int ret;170int i;171u8 buf[3];172173ret = yuan_mpc718_mt352_reqfw(stream, &fw);174if (ret)175return ret;176177/* Loop through all the register-value pairs in the firmware file */178for (i = 0; i < fw->size; i += 2) {179buf[0] = fw->data[i];180/* Intercept a few registers we want to set ourselves */181switch (buf[0]) {182case TRL_NOMINAL_RATE_0:183/* Set our custom OFDM bandwidth in the case below */184break;185case TRL_NOMINAL_RATE_1:186/* 6 MHz: 64/7 * 6/8 / 20.48 * 2^16 = 0x55b6.db6 */187/* 7 MHz: 64/7 * 7/8 / 20.48 * 2^16 = 0x6400 */188/* 8 MHz: 64/7 * 8/8 / 20.48 * 2^16 = 0x7249.249 */189buf[1] = 0x72;190buf[2] = 0x49;191mt352_write(fe, buf, 3);192break;193case INPUT_FREQ_0:194/* Set our custom IF in the case below */195break;196case INPUT_FREQ_1:197/* 4.56 MHz IF: (20.48 - 4.56)/20.48 * 2^14 = 0x31c0 */198buf[1] = 0x31;199buf[2] = 0xc0;200mt352_write(fe, buf, 3);201break;202default:203/* Pass through the register-value pair from the fw */204buf[1] = fw->data[i+1];205mt352_write(fe, buf, 2);206break;207}208}209210buf[0] = (u8) TUNER_GO;211buf[1] = 0x01; /* Go */212mt352_write(fe, buf, 2);213release_firmware(fw);214return 0;215}216217static struct mt352_config yuan_mpc718_mt352_demod = {218.demod_address = 0x1e >> 1,219.adc_clock = 20480, /* 20.480 MHz */220.if2 = 4560, /* 4.560 MHz */221.no_tuner = 1, /* XC3028 is not behind the gate */222.demod_init = yuan_mpc718_mt352_init,223};224225static struct zl10353_config yuan_mpc718_zl10353_demod = {226.demod_address = 0x1e >> 1, /* Datasheet suggested straps */227.if2 = 45600, /* 4.560 MHz IF from the XC3028 */228.parallel_ts = 1, /* Not a serial TS */229.no_tuner = 1, /* XC3028 is not behind the gate */230.disable_i2c_gate_ctrl = 1, /* Disable the I2C gate */231};232233static struct zl10353_config gotview_dvd3_zl10353_demod = {234.demod_address = 0x1e >> 1, /* Datasheet suggested straps */235.if2 = 45600, /* 4.560 MHz IF from the XC3028 */236.parallel_ts = 1, /* Not a serial TS */237.no_tuner = 1, /* XC3028 is not behind the gate */238.disable_i2c_gate_ctrl = 1, /* Disable the I2C gate */239};240241static int dvb_register(struct cx18_stream *stream);242243/* Kernel DVB framework calls this when the feed needs to start.244* The CX18 framework should enable the transport DMA handling245* and queue processing.246*/247static int cx18_dvb_start_feed(struct dvb_demux_feed *feed)248{249struct dvb_demux *demux = feed->demux;250struct cx18_stream *stream = (struct cx18_stream *) demux->priv;251struct cx18 *cx;252int ret;253u32 v;254255if (!stream)256return -EINVAL;257258cx = stream->cx;259CX18_DEBUG_INFO("Start feed: pid = 0x%x index = %d\n",260feed->pid, feed->index);261262mutex_lock(&cx->serialize_lock);263ret = cx18_init_on_first_open(cx);264mutex_unlock(&cx->serialize_lock);265if (ret) {266CX18_ERR("Failed to initialize firmware starting DVB feed\n");267return ret;268}269ret = -EINVAL;270271switch (cx->card->type) {272case CX18_CARD_HVR_1600_ESMT:273case CX18_CARD_HVR_1600_SAMSUNG:274case CX18_CARD_HVR_1600_S5H1411:275v = cx18_read_reg(cx, CX18_REG_DMUX_NUM_PORT_0_CONTROL);276v |= 0x00400000; /* Serial Mode */277v |= 0x00002000; /* Data Length - Byte */278v |= 0x00010000; /* Error - Polarity */279v |= 0x00020000; /* Error - Passthru */280v |= 0x000c0000; /* Error - Ignore */281cx18_write_reg(cx, v, CX18_REG_DMUX_NUM_PORT_0_CONTROL);282break;283284case CX18_CARD_LEADTEK_DVR3100H:285case CX18_CARD_YUAN_MPC718:286case CX18_CARD_GOTVIEW_PCI_DVD3:287default:288/* Assumption - Parallel transport - Signalling289* undefined or default.290*/291break;292}293294if (!demux->dmx.frontend)295return -EINVAL;296297mutex_lock(&stream->dvb->feedlock);298if (stream->dvb->feeding++ == 0) {299CX18_DEBUG_INFO("Starting Transport DMA\n");300mutex_lock(&cx->serialize_lock);301set_bit(CX18_F_S_STREAMING, &stream->s_flags);302ret = cx18_start_v4l2_encode_stream(stream);303if (ret < 0) {304CX18_DEBUG_INFO("Failed to start Transport DMA\n");305stream->dvb->feeding--;306if (stream->dvb->feeding == 0)307clear_bit(CX18_F_S_STREAMING, &stream->s_flags);308}309mutex_unlock(&cx->serialize_lock);310} else311ret = 0;312mutex_unlock(&stream->dvb->feedlock);313314return ret;315}316317/* Kernel DVB framework calls this when the feed needs to stop. */318static int cx18_dvb_stop_feed(struct dvb_demux_feed *feed)319{320struct dvb_demux *demux = feed->demux;321struct cx18_stream *stream = (struct cx18_stream *)demux->priv;322struct cx18 *cx;323int ret = -EINVAL;324325if (stream) {326cx = stream->cx;327CX18_DEBUG_INFO("Stop feed: pid = 0x%x index = %d\n",328feed->pid, feed->index);329330mutex_lock(&stream->dvb->feedlock);331if (--stream->dvb->feeding == 0) {332CX18_DEBUG_INFO("Stopping Transport DMA\n");333mutex_lock(&cx->serialize_lock);334ret = cx18_stop_v4l2_encode_stream(stream, 0);335mutex_unlock(&cx->serialize_lock);336} else337ret = 0;338mutex_unlock(&stream->dvb->feedlock);339}340341return ret;342}343344int cx18_dvb_register(struct cx18_stream *stream)345{346struct cx18 *cx = stream->cx;347struct cx18_dvb *dvb = stream->dvb;348struct dvb_adapter *dvb_adapter;349struct dvb_demux *dvbdemux;350struct dmx_demux *dmx;351int ret;352353if (!dvb)354return -EINVAL;355356dvb->enabled = 0;357dvb->stream = stream;358359ret = dvb_register_adapter(&dvb->dvb_adapter,360CX18_DRIVER_NAME,361THIS_MODULE, &cx->pci_dev->dev, adapter_nr);362if (ret < 0)363goto err_out;364365dvb_adapter = &dvb->dvb_adapter;366367dvbdemux = &dvb->demux;368369dvbdemux->priv = (void *)stream;370371dvbdemux->filternum = 256;372dvbdemux->feednum = 256;373dvbdemux->start_feed = cx18_dvb_start_feed;374dvbdemux->stop_feed = cx18_dvb_stop_feed;375dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |376DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);377ret = dvb_dmx_init(dvbdemux);378if (ret < 0)379goto err_dvb_unregister_adapter;380381dmx = &dvbdemux->dmx;382383dvb->hw_frontend.source = DMX_FRONTEND_0;384dvb->mem_frontend.source = DMX_MEMORY_FE;385dvb->dmxdev.filternum = 256;386dvb->dmxdev.demux = dmx;387388ret = dvb_dmxdev_init(&dvb->dmxdev, dvb_adapter);389if (ret < 0)390goto err_dvb_dmx_release;391392ret = dmx->add_frontend(dmx, &dvb->hw_frontend);393if (ret < 0)394goto err_dvb_dmxdev_release;395396ret = dmx->add_frontend(dmx, &dvb->mem_frontend);397if (ret < 0)398goto err_remove_hw_frontend;399400ret = dmx->connect_frontend(dmx, &dvb->hw_frontend);401if (ret < 0)402goto err_remove_mem_frontend;403404ret = dvb_register(stream);405if (ret < 0)406goto err_disconnect_frontend;407408dvb_net_init(dvb_adapter, &dvb->dvbnet, dmx);409410CX18_INFO("DVB Frontend registered\n");411CX18_INFO("Registered DVB adapter%d for %s (%d x %d.%02d kB)\n",412stream->dvb->dvb_adapter.num, stream->name,413stream->buffers, stream->buf_size/1024,414(stream->buf_size * 100 / 1024) % 100);415416mutex_init(&dvb->feedlock);417dvb->enabled = 1;418return ret;419420err_disconnect_frontend:421dmx->disconnect_frontend(dmx);422err_remove_mem_frontend:423dmx->remove_frontend(dmx, &dvb->mem_frontend);424err_remove_hw_frontend:425dmx->remove_frontend(dmx, &dvb->hw_frontend);426err_dvb_dmxdev_release:427dvb_dmxdev_release(&dvb->dmxdev);428err_dvb_dmx_release:429dvb_dmx_release(dvbdemux);430err_dvb_unregister_adapter:431dvb_unregister_adapter(dvb_adapter);432err_out:433return ret;434}435436void cx18_dvb_unregister(struct cx18_stream *stream)437{438struct cx18 *cx = stream->cx;439struct cx18_dvb *dvb = stream->dvb;440struct dvb_adapter *dvb_adapter;441struct dvb_demux *dvbdemux;442struct dmx_demux *dmx;443444CX18_INFO("unregister DVB\n");445446if (dvb == NULL || !dvb->enabled)447return;448449dvb_adapter = &dvb->dvb_adapter;450dvbdemux = &dvb->demux;451dmx = &dvbdemux->dmx;452453dmx->close(dmx);454dvb_net_release(&dvb->dvbnet);455dmx->remove_frontend(dmx, &dvb->mem_frontend);456dmx->remove_frontend(dmx, &dvb->hw_frontend);457dvb_dmxdev_release(&dvb->dmxdev);458dvb_dmx_release(dvbdemux);459dvb_unregister_frontend(dvb->fe);460dvb_frontend_detach(dvb->fe);461dvb_unregister_adapter(dvb_adapter);462}463464/* All the DVB attach calls go here, this function get's modified465* for each new card. cx18_dvb_start_feed() will also need changes.466*/467static int dvb_register(struct cx18_stream *stream)468{469struct cx18_dvb *dvb = stream->dvb;470struct cx18 *cx = stream->cx;471int ret = 0;472473switch (cx->card->type) {474case CX18_CARD_HVR_1600_ESMT:475case CX18_CARD_HVR_1600_SAMSUNG:476dvb->fe = dvb_attach(s5h1409_attach,477&hauppauge_hvr1600_config,478&cx->i2c_adap[0]);479if (dvb->fe != NULL) {480dvb_attach(mxl5005s_attach, dvb->fe,481&cx->i2c_adap[0],482&hauppauge_hvr1600_tuner);483ret = 0;484}485break;486case CX18_CARD_HVR_1600_S5H1411:487dvb->fe = dvb_attach(s5h1411_attach,488&hcw_s5h1411_config,489&cx->i2c_adap[0]);490if (dvb->fe != NULL)491dvb_attach(tda18271_attach, dvb->fe,4920x60, &cx->i2c_adap[0],493&hauppauge_tda18271_config);494break;495case CX18_CARD_LEADTEK_DVR3100H:496dvb->fe = dvb_attach(zl10353_attach,497&leadtek_dvr3100h_demod,498&cx->i2c_adap[1]);499if (dvb->fe != NULL) {500struct dvb_frontend *fe;501struct xc2028_config cfg = {502.i2c_adap = &cx->i2c_adap[1],503.i2c_addr = 0xc2 >> 1,504.ctrl = NULL,505};506static struct xc2028_ctrl ctrl = {507.fname = XC2028_DEFAULT_FIRMWARE,508.max_len = 64,509.demod = XC3028_FE_ZARLINK456,510.type = XC2028_AUTO,511};512513fe = dvb_attach(xc2028_attach, dvb->fe, &cfg);514if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)515fe->ops.tuner_ops.set_config(fe, &ctrl);516}517break;518case CX18_CARD_YUAN_MPC718:519/*520* TODO521* Apparently, these cards also could instead have a522* DiBcom demod supported by one of the db7000 drivers523*/524dvb->fe = dvb_attach(mt352_attach,525&yuan_mpc718_mt352_demod,526&cx->i2c_adap[1]);527if (dvb->fe == NULL)528dvb->fe = dvb_attach(zl10353_attach,529&yuan_mpc718_zl10353_demod,530&cx->i2c_adap[1]);531if (dvb->fe != NULL) {532struct dvb_frontend *fe;533struct xc2028_config cfg = {534.i2c_adap = &cx->i2c_adap[1],535.i2c_addr = 0xc2 >> 1,536.ctrl = NULL,537};538static struct xc2028_ctrl ctrl = {539.fname = XC2028_DEFAULT_FIRMWARE,540.max_len = 64,541.demod = XC3028_FE_ZARLINK456,542.type = XC2028_AUTO,543};544545fe = dvb_attach(xc2028_attach, dvb->fe, &cfg);546if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)547fe->ops.tuner_ops.set_config(fe, &ctrl);548}549break;550case CX18_CARD_GOTVIEW_PCI_DVD3:551dvb->fe = dvb_attach(zl10353_attach,552&gotview_dvd3_zl10353_demod,553&cx->i2c_adap[1]);554if (dvb->fe != NULL) {555struct dvb_frontend *fe;556struct xc2028_config cfg = {557.i2c_adap = &cx->i2c_adap[1],558.i2c_addr = 0xc2 >> 1,559.ctrl = NULL,560};561static struct xc2028_ctrl ctrl = {562.fname = XC2028_DEFAULT_FIRMWARE,563.max_len = 64,564.demod = XC3028_FE_ZARLINK456,565.type = XC2028_AUTO,566};567568fe = dvb_attach(xc2028_attach, dvb->fe, &cfg);569if (fe != NULL && fe->ops.tuner_ops.set_config != NULL)570fe->ops.tuner_ops.set_config(fe, &ctrl);571}572break;573default:574/* No Digital Tv Support */575break;576}577578if (dvb->fe == NULL) {579CX18_ERR("frontend initialization failed\n");580return -1;581}582583dvb->fe->callback = cx18_reset_tuner_gpio;584585ret = dvb_register_frontend(&dvb->dvb_adapter, dvb->fe);586if (ret < 0) {587if (dvb->fe->ops.release)588dvb->fe->ops.release(dvb->fe);589return ret;590}591592/*593* The firmware seems to enable the TS DMUX clock594* under various circumstances. However, since we know we595* might use it, let's just turn it on ourselves here.596*/597cx18_write_reg_expect(cx,598(CX18_DMUX_CLK_MASK << 16) | CX18_DMUX_CLK_MASK,599CX18_CLOCK_ENABLE2,600CX18_DMUX_CLK_MASK,601(CX18_DMUX_CLK_MASK << 16) | CX18_DMUX_CLK_MASK);602603return ret;604}605606607