Path: blob/master/drivers/media/dvb/b2c2/flexcop-fe-tuner.c
15111 views
/*1* Linux driver for digital TV devices equipped with B2C2 FlexcopII(b)/III2* flexcop-fe-tuner.c - methods for frontend attachment and DiSEqC controlling3* see flexcop.c for copyright information4*/5#include <media/tuner.h>6#include "flexcop.h"7#include "mt312.h"8#include "stv0299.h"9#include "s5h1420.h"10#include "itd1000.h"11#include "cx24113.h"12#include "cx24123.h"13#include "isl6421.h"14#include "mt352.h"15#include "bcm3510.h"16#include "nxt200x.h"17#include "dvb-pll.h"18#include "lgdt330x.h"19#include "tuner-simple.h"20#include "stv0297.h"212223/* Can we use the specified front-end? Remember that if we are compiled24* into the kernel we can't call code that's in modules. */25#define FE_SUPPORTED(fe) (defined(CONFIG_DVB_##fe) || \26(defined(CONFIG_DVB_##fe##_MODULE) && defined(MODULE)))2728/* lnb control */29#if FE_SUPPORTED(MT312) || FE_SUPPORTED(STV0299)30static int flexcop_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)31{32struct flexcop_device *fc = fe->dvb->priv;33flexcop_ibi_value v;34deb_tuner("polarity/voltage = %u\n", voltage);3536v = fc->read_ibi_reg(fc, misc_204);37switch (voltage) {38case SEC_VOLTAGE_OFF:39v.misc_204.ACPI1_sig = 1;40break;41case SEC_VOLTAGE_13:42v.misc_204.ACPI1_sig = 0;43v.misc_204.LNB_L_H_sig = 0;44break;45case SEC_VOLTAGE_18:46v.misc_204.ACPI1_sig = 0;47v.misc_204.LNB_L_H_sig = 1;48break;49default:50err("unknown SEC_VOLTAGE value");51return -EINVAL;52}53return fc->write_ibi_reg(fc, misc_204, v);54}55#endif5657#if FE_SUPPORTED(S5H1420) || FE_SUPPORTED(STV0299) || FE_SUPPORTED(MT312)58static int flexcop_sleep(struct dvb_frontend* fe)59{60struct flexcop_device *fc = fe->dvb->priv;61if (fc->fe_sleep)62return fc->fe_sleep(fe);63return 0;64}65#endif6667/* SkyStar2 DVB-S rev 2.3 */68#if FE_SUPPORTED(MT312) && FE_SUPPORTED(PLL)69static int flexcop_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)70{71/* u16 wz_half_period_for_45_mhz[] = { 0x01ff, 0x0154, 0x00ff, 0x00cc }; */72struct flexcop_device *fc = fe->dvb->priv;73flexcop_ibi_value v;74u16 ax;75v.raw = 0;76deb_tuner("tone = %u\n",tone);7778switch (tone) {79case SEC_TONE_ON:80ax = 0x01ff;81break;82case SEC_TONE_OFF:83ax = 0;84break;85default:86err("unknown SEC_TONE value");87return -EINVAL;88}8990v.lnb_switch_freq_200.LNB_CTLPrescaler_sig = 1; /* divide by 2 */91v.lnb_switch_freq_200.LNB_CTLHighCount_sig = ax;92v.lnb_switch_freq_200.LNB_CTLLowCount_sig = ax == 0 ? 0x1ff : ax;93return fc->write_ibi_reg(fc,lnb_switch_freq_200,v);94}9596static void flexcop_diseqc_send_bit(struct dvb_frontend* fe, int data)97{98flexcop_set_tone(fe, SEC_TONE_ON);99udelay(data ? 500 : 1000);100flexcop_set_tone(fe, SEC_TONE_OFF);101udelay(data ? 1000 : 500);102}103104static void flexcop_diseqc_send_byte(struct dvb_frontend* fe, int data)105{106int i, par = 1, d;107for (i = 7; i >= 0; i--) {108d = (data >> i) & 1;109par ^= d;110flexcop_diseqc_send_bit(fe, d);111}112flexcop_diseqc_send_bit(fe, par);113}114115static int flexcop_send_diseqc_msg(struct dvb_frontend *fe,116int len, u8 *msg, unsigned long burst)117{118int i;119120flexcop_set_tone(fe, SEC_TONE_OFF);121mdelay(16);122123for (i = 0; i < len; i++)124flexcop_diseqc_send_byte(fe,msg[i]);125mdelay(16);126127if (burst != -1) {128if (burst)129flexcop_diseqc_send_byte(fe, 0xff);130else {131flexcop_set_tone(fe, SEC_TONE_ON);132mdelay(12);133udelay(500);134flexcop_set_tone(fe, SEC_TONE_OFF);135}136msleep(20);137}138return 0;139}140141static int flexcop_diseqc_send_master_cmd(struct dvb_frontend *fe,142struct dvb_diseqc_master_cmd *cmd)143{144return flexcop_send_diseqc_msg(fe, cmd->msg_len, cmd->msg, 0);145}146147static int flexcop_diseqc_send_burst(struct dvb_frontend *fe,148fe_sec_mini_cmd_t minicmd)149{150return flexcop_send_diseqc_msg(fe, 0, NULL, minicmd);151}152153static struct mt312_config skystar23_samsung_tbdu18132_config = {154.demod_address = 0x0e,155};156157static int skystar2_rev23_attach(struct flexcop_device *fc,158struct i2c_adapter *i2c)159{160struct dvb_frontend_ops *ops;161162fc->fe = dvb_attach(mt312_attach, &skystar23_samsung_tbdu18132_config, i2c);163if (!fc->fe)164return 0;165166if (!dvb_attach(dvb_pll_attach, fc->fe, 0x61, i2c,167DVB_PLL_SAMSUNG_TBDU18132))168return 0;169170ops = &fc->fe->ops;171ops->diseqc_send_master_cmd = flexcop_diseqc_send_master_cmd;172ops->diseqc_send_burst = flexcop_diseqc_send_burst;173ops->set_tone = flexcop_set_tone;174ops->set_voltage = flexcop_set_voltage;175fc->fe_sleep = ops->sleep;176ops->sleep = flexcop_sleep;177return 1;178}179#else180#define skystar2_rev23_attach NULL181#endif182183/* SkyStar2 DVB-S rev 2.6 */184#if FE_SUPPORTED(STV0299) && FE_SUPPORTED(PLL)185static int samsung_tbmu24112_set_symbol_rate(struct dvb_frontend *fe,186u32 srate, u32 ratio)187{188u8 aclk = 0;189u8 bclk = 0;190191if (srate < 1500000) {192aclk = 0xb7; bclk = 0x47;193} else if (srate < 3000000) {194aclk = 0xb7; bclk = 0x4b;195} else if (srate < 7000000) {196aclk = 0xb7; bclk = 0x4f;197} else if (srate < 14000000) {198aclk = 0xb7; bclk = 0x53;199} else if (srate < 30000000) {200aclk = 0xb6; bclk = 0x53;201} else if (srate < 45000000) {202aclk = 0xb4; bclk = 0x51;203}204205stv0299_writereg(fe, 0x13, aclk);206stv0299_writereg(fe, 0x14, bclk);207stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);208stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);209stv0299_writereg(fe, 0x21, ratio & 0xf0);210return 0;211}212213static u8 samsung_tbmu24112_inittab[] = {2140x01, 0x15,2150x02, 0x30,2160x03, 0x00,2170x04, 0x7D,2180x05, 0x35,2190x06, 0x02,2200x07, 0x00,2210x08, 0xC3,2220x0C, 0x00,2230x0D, 0x81,2240x0E, 0x23,2250x0F, 0x12,2260x10, 0x7E,2270x11, 0x84,2280x12, 0xB9,2290x13, 0x88,2300x14, 0x89,2310x15, 0xC9,2320x16, 0x00,2330x17, 0x5C,2340x18, 0x00,2350x19, 0x00,2360x1A, 0x00,2370x1C, 0x00,2380x1D, 0x00,2390x1E, 0x00,2400x1F, 0x3A,2410x20, 0x2E,2420x21, 0x80,2430x22, 0xFF,2440x23, 0xC1,2450x28, 0x00,2460x29, 0x1E,2470x2A, 0x14,2480x2B, 0x0F,2490x2C, 0x09,2500x2D, 0x05,2510x31, 0x1F,2520x32, 0x19,2530x33, 0xFE,2540x34, 0x93,2550xff, 0xff,256};257258static struct stv0299_config samsung_tbmu24112_config = {259.demod_address = 0x68,260.inittab = samsung_tbmu24112_inittab,261.mclk = 88000000UL,262.invert = 0,263.skip_reinit = 0,264.lock_output = STV0299_LOCKOUTPUT_LK,265.volt13_op0_op1 = STV0299_VOLT13_OP1,266.min_delay_ms = 100,267.set_symbol_rate = samsung_tbmu24112_set_symbol_rate,268};269270static int skystar2_rev26_attach(struct flexcop_device *fc,271struct i2c_adapter *i2c)272{273fc->fe = dvb_attach(stv0299_attach, &samsung_tbmu24112_config, i2c);274if (!fc->fe)275return 0;276277if (!dvb_attach(dvb_pll_attach, fc->fe, 0x61, i2c,278DVB_PLL_SAMSUNG_TBMU24112))279return 0;280281fc->fe->ops.set_voltage = flexcop_set_voltage;282fc->fe_sleep = fc->fe->ops.sleep;283fc->fe->ops.sleep = flexcop_sleep;284return 1;285286}287#else288#define skystar2_rev26_attach NULL289#endif290291/* SkyStar2 DVB-S rev 2.7 */292#if FE_SUPPORTED(S5H1420) && FE_SUPPORTED(ISL6421) && FE_SUPPORTED(TUNER_ITD1000)293static struct s5h1420_config skystar2_rev2_7_s5h1420_config = {294.demod_address = 0x53,295.invert = 1,296.repeated_start_workaround = 1,297.serial_mpeg = 1,298};299300static struct itd1000_config skystar2_rev2_7_itd1000_config = {301.i2c_address = 0x61,302};303304static int skystar2_rev27_attach(struct flexcop_device *fc,305struct i2c_adapter *i2c)306{307flexcop_ibi_value r108;308struct i2c_adapter *i2c_tuner;309310/* enable no_base_addr - no repeated start when reading */311fc->fc_i2c_adap[0].no_base_addr = 1;312fc->fe = dvb_attach(s5h1420_attach, &skystar2_rev2_7_s5h1420_config,313i2c);314if (!fc->fe)315goto fail;316317i2c_tuner = s5h1420_get_tuner_i2c_adapter(fc->fe);318if (!i2c_tuner)319goto fail;320321fc->fe_sleep = fc->fe->ops.sleep;322fc->fe->ops.sleep = flexcop_sleep;323324/* enable no_base_addr - no repeated start when reading */325fc->fc_i2c_adap[2].no_base_addr = 1;326if (!dvb_attach(isl6421_attach, fc->fe, &fc->fc_i2c_adap[2].i2c_adap,3270x08, 1, 1)) {328err("ISL6421 could NOT be attached");329goto fail_isl;330}331info("ISL6421 successfully attached");332333/* the ITD1000 requires a lower i2c clock - is it a problem ? */334r108.raw = 0x00000506;335fc->write_ibi_reg(fc, tw_sm_c_108, r108);336if (!dvb_attach(itd1000_attach, fc->fe, i2c_tuner,337&skystar2_rev2_7_itd1000_config)) {338err("ITD1000 could NOT be attached");339/* Should i2c clock be restored? */340goto fail_isl;341}342info("ITD1000 successfully attached");343344return 1;345346fail_isl:347fc->fc_i2c_adap[2].no_base_addr = 0;348fail:349/* for the next devices we need it again */350fc->fc_i2c_adap[0].no_base_addr = 0;351return 0;352}353#else354#define skystar2_rev27_attach NULL355#endif356357/* SkyStar2 rev 2.8 */358#if FE_SUPPORTED(CX24123) && FE_SUPPORTED(ISL6421) && FE_SUPPORTED(TUNER_CX24113)359static struct cx24123_config skystar2_rev2_8_cx24123_config = {360.demod_address = 0x55,361.dont_use_pll = 1,362.agc_callback = cx24113_agc_callback,363};364365static const struct cx24113_config skystar2_rev2_8_cx24113_config = {366.i2c_addr = 0x54,367.xtal_khz = 10111,368};369370static int skystar2_rev28_attach(struct flexcop_device *fc,371struct i2c_adapter *i2c)372{373struct i2c_adapter *i2c_tuner;374375fc->fe = dvb_attach(cx24123_attach, &skystar2_rev2_8_cx24123_config,376i2c);377if (!fc->fe)378return 0;379380i2c_tuner = cx24123_get_tuner_i2c_adapter(fc->fe);381if (!i2c_tuner)382return 0;383384if (!dvb_attach(cx24113_attach, fc->fe, &skystar2_rev2_8_cx24113_config,385i2c_tuner)) {386err("CX24113 could NOT be attached");387return 0;388}389info("CX24113 successfully attached");390391fc->fc_i2c_adap[2].no_base_addr = 1;392if (!dvb_attach(isl6421_attach, fc->fe, &fc->fc_i2c_adap[2].i2c_adap,3930x08, 0, 0)) {394err("ISL6421 could NOT be attached");395fc->fc_i2c_adap[2].no_base_addr = 0;396return 0;397}398info("ISL6421 successfully attached");399/* TODO on i2c_adap[1] addr 0x11 (EEPROM) there seems to be an400* IR-receiver (PIC16F818) - but the card has no input for that ??? */401return 1;402}403#else404#define skystar2_rev28_attach NULL405#endif406407/* AirStar DVB-T */408#if FE_SUPPORTED(MT352) && FE_SUPPORTED(PLL)409static int samsung_tdtc9251dh0_demod_init(struct dvb_frontend *fe)410{411static u8 mt352_clock_config[] = { 0x89, 0x18, 0x2d };412static u8 mt352_reset[] = { 0x50, 0x80 };413static u8 mt352_adc_ctl_1_cfg[] = { 0x8E, 0x40 };414static u8 mt352_agc_cfg[] = { 0x67, 0x28, 0xa1 };415static u8 mt352_capt_range_cfg[] = { 0x75, 0x32 };416417mt352_write(fe, mt352_clock_config, sizeof(mt352_clock_config));418udelay(2000);419mt352_write(fe, mt352_reset, sizeof(mt352_reset));420mt352_write(fe, mt352_adc_ctl_1_cfg, sizeof(mt352_adc_ctl_1_cfg));421mt352_write(fe, mt352_agc_cfg, sizeof(mt352_agc_cfg));422mt352_write(fe, mt352_capt_range_cfg, sizeof(mt352_capt_range_cfg));423return 0;424}425426static struct mt352_config samsung_tdtc9251dh0_config = {427.demod_address = 0x0f,428.demod_init = samsung_tdtc9251dh0_demod_init,429};430431static int airstar_dvbt_attach(struct flexcop_device *fc,432struct i2c_adapter *i2c)433{434fc->fe = dvb_attach(mt352_attach, &samsung_tdtc9251dh0_config, i2c);435if (!fc->fe)436return 0;437438return !!dvb_attach(dvb_pll_attach, fc->fe, 0x61, NULL,439DVB_PLL_SAMSUNG_TDTC9251DH0);440}441#else442#define airstar_dvbt_attach NULL443#endif444445/* AirStar ATSC 1st generation */446#if FE_SUPPORTED(BCM3510)447static int flexcop_fe_request_firmware(struct dvb_frontend *fe,448const struct firmware **fw, char* name)449{450struct flexcop_device *fc = fe->dvb->priv;451return request_firmware(fw, name, fc->dev);452}453454static struct bcm3510_config air2pc_atsc_first_gen_config = {455.demod_address = 0x0f,456.request_firmware = flexcop_fe_request_firmware,457};458459static int airstar_atsc1_attach(struct flexcop_device *fc,460struct i2c_adapter *i2c)461{462fc->fe = dvb_attach(bcm3510_attach, &air2pc_atsc_first_gen_config, i2c);463return fc->fe != NULL;464}465#else466#define airstar_atsc1_attach NULL467#endif468469/* AirStar ATSC 2nd generation */470#if FE_SUPPORTED(NXT200X) && FE_SUPPORTED(PLL)471static struct nxt200x_config samsung_tbmv_config = {472.demod_address = 0x0a,473};474475static int airstar_atsc2_attach(struct flexcop_device *fc,476struct i2c_adapter *i2c)477{478fc->fe = dvb_attach(nxt200x_attach, &samsung_tbmv_config, i2c);479if (!fc->fe)480return 0;481482return !!dvb_attach(dvb_pll_attach, fc->fe, 0x61, NULL,483DVB_PLL_SAMSUNG_TBMV);484}485#else486#define airstar_atsc2_attach NULL487#endif488489/* AirStar ATSC 3rd generation */490#if FE_SUPPORTED(LGDT330X)491static struct lgdt330x_config air2pc_atsc_hd5000_config = {492.demod_address = 0x59,493.demod_chip = LGDT3303,494.serial_mpeg = 0x04,495.clock_polarity_flip = 1,496};497498static int airstar_atsc3_attach(struct flexcop_device *fc,499struct i2c_adapter *i2c)500{501fc->fe = dvb_attach(lgdt330x_attach, &air2pc_atsc_hd5000_config, i2c);502if (!fc->fe)503return 0;504505return !!dvb_attach(simple_tuner_attach, fc->fe, i2c, 0x61,506TUNER_LG_TDVS_H06XF);507}508#else509#define airstar_atsc3_attach NULL510#endif511512/* CableStar2 DVB-C */513#if FE_SUPPORTED(STV0297) && FE_SUPPORTED(PLL)514static u8 alps_tdee4_stv0297_inittab[] = {5150x80, 0x01,5160x80, 0x00,5170x81, 0x01,5180x81, 0x00,5190x00, 0x48,5200x01, 0x58,5210x03, 0x00,5220x04, 0x00,5230x07, 0x00,5240x08, 0x00,5250x30, 0xff,5260x31, 0x9d,5270x32, 0xff,5280x33, 0x00,5290x34, 0x29,5300x35, 0x55,5310x36, 0x80,5320x37, 0x6e,5330x38, 0x9c,5340x40, 0x1a,5350x41, 0xfe,5360x42, 0x33,5370x43, 0x00,5380x44, 0xff,5390x45, 0x00,5400x46, 0x00,5410x49, 0x04,5420x4a, 0x51,5430x4b, 0xf8,5440x52, 0x30,5450x53, 0x06,5460x59, 0x06,5470x5a, 0x5e,5480x5b, 0x04,5490x61, 0x49,5500x62, 0x0a,5510x70, 0xff,5520x71, 0x04,5530x72, 0x00,5540x73, 0x00,5550x74, 0x0c,5560x80, 0x20,5570x81, 0x00,5580x82, 0x30,5590x83, 0x00,5600x84, 0x04,5610x85, 0x22,5620x86, 0x08,5630x87, 0x1b,5640x88, 0x00,5650x89, 0x00,5660x90, 0x00,5670x91, 0x04,5680xa0, 0x86,5690xa1, 0x00,5700xa2, 0x00,5710xb0, 0x91,5720xb1, 0x0b,5730xc0, 0x5b,5740xc1, 0x10,5750xc2, 0x12,5760xd0, 0x02,5770xd1, 0x00,5780xd2, 0x00,5790xd3, 0x00,5800xd4, 0x02,5810xd5, 0x00,5820xde, 0x00,5830xdf, 0x01,5840xff, 0xff,585};586587static struct stv0297_config alps_tdee4_stv0297_config = {588.demod_address = 0x1c,589.inittab = alps_tdee4_stv0297_inittab,590};591592static int cablestar2_attach(struct flexcop_device *fc,593struct i2c_adapter *i2c)594{595fc->fc_i2c_adap[0].no_base_addr = 1;596fc->fe = dvb_attach(stv0297_attach, &alps_tdee4_stv0297_config, i2c);597if (!fc->fe)598goto fail;599600/* This tuner doesn't use the stv0297's I2C gate, but instead the601* tuner is connected to a different flexcop I2C adapter. */602if (fc->fe->ops.i2c_gate_ctrl)603fc->fe->ops.i2c_gate_ctrl(fc->fe, 0);604fc->fe->ops.i2c_gate_ctrl = NULL;605606if (!dvb_attach(dvb_pll_attach, fc->fe, 0x61,607&fc->fc_i2c_adap[2].i2c_adap, DVB_PLL_TDEE4))608goto fail;609610return 1;611612fail:613/* Reset for next frontend to try */614fc->fc_i2c_adap[0].no_base_addr = 0;615return 0;616}617#else618#define cablestar2_attach NULL619#endif620621static struct {622flexcop_device_type_t type;623int (*attach)(struct flexcop_device *, struct i2c_adapter *);624} flexcop_frontends[] = {625{ FC_SKY_REV27, skystar2_rev27_attach },626{ FC_SKY_REV28, skystar2_rev28_attach },627{ FC_SKY_REV26, skystar2_rev26_attach },628{ FC_AIR_DVBT, airstar_dvbt_attach },629{ FC_AIR_ATSC2, airstar_atsc2_attach },630{ FC_AIR_ATSC3, airstar_atsc3_attach },631{ FC_AIR_ATSC1, airstar_atsc1_attach },632{ FC_CABLE, cablestar2_attach },633{ FC_SKY_REV23, skystar2_rev23_attach },634};635636/* try to figure out the frontend */637int flexcop_frontend_init(struct flexcop_device *fc)638{639int i;640for (i = 0; i < ARRAY_SIZE(flexcop_frontends); i++) {641if (!flexcop_frontends[i].attach)642continue;643/* type needs to be set before, because of some workarounds644* done based on the probed card type */645fc->dev_type = flexcop_frontends[i].type;646if (flexcop_frontends[i].attach(fc, &fc->fc_i2c_adap[0].i2c_adap))647goto fe_found;648/* Clean up partially attached frontend */649if (fc->fe) {650dvb_frontend_detach(fc->fe);651fc->fe = NULL;652}653}654fc->dev_type = FC_UNK;655err("no frontend driver found for this B2C2/FlexCop adapter");656return -ENODEV;657658fe_found:659info("found '%s' .", fc->fe->ops.info.name);660if (dvb_register_frontend(&fc->dvb_adapter, fc->fe)) {661err("frontend registration failed!");662dvb_frontend_detach(fc->fe);663fc->fe = NULL;664return -EINVAL;665}666fc->init_state |= FC_STATE_FE_INIT;667return 0;668}669670void flexcop_frontend_exit(struct flexcop_device *fc)671{672if (fc->init_state & FC_STATE_FE_INIT) {673dvb_unregister_frontend(fc->fe);674dvb_frontend_detach(fc->fe);675}676fc->init_state &= ~FC_STATE_FE_INIT;677}678679680