Path: blob/master/drivers/media/common/tuners/mt2266.c
15112 views
/*1* Driver for Microtune MT2266 "Direct conversion low power broadband tuner"2*3* Copyright (c) 2007 Olivier DANET <[email protected]>4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License as published by7* the Free Software Foundation; either version 2 of the License, or8* (at your option) any later version.9*10* This program is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13* GNU General Public License for more details.14*/1516#include <linux/module.h>17#include <linux/delay.h>18#include <linux/dvb/frontend.h>19#include <linux/i2c.h>20#include <linux/slab.h>2122#include "dvb_frontend.h"23#include "mt2266.h"2425#define I2C_ADDRESS 0x602627#define REG_PART_REV 028#define REG_TUNE 129#define REG_BAND 630#define REG_BANDWIDTH 831#define REG_LOCK 0x123233#define PART_REV 0x853435struct mt2266_priv {36struct mt2266_config *cfg;37struct i2c_adapter *i2c;3839u32 frequency;40u32 bandwidth;41u8 band;42};4344#define MT2266_VHF 145#define MT2266_UHF 04647/* Here, frequencies are expressed in kiloHertz to avoid 32 bits overflows */4849static int debug;50module_param(debug, int, 0644);51MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");5253#define dprintk(args...) do { if (debug) {printk(KERN_DEBUG "MT2266: " args); printk("\n"); }} while (0)5455// Reads a single register56static int mt2266_readreg(struct mt2266_priv *priv, u8 reg, u8 *val)57{58struct i2c_msg msg[2] = {59{ .addr = priv->cfg->i2c_address, .flags = 0, .buf = ®, .len = 1 },60{ .addr = priv->cfg->i2c_address, .flags = I2C_M_RD, .buf = val, .len = 1 },61};62if (i2c_transfer(priv->i2c, msg, 2) != 2) {63printk(KERN_WARNING "MT2266 I2C read failed\n");64return -EREMOTEIO;65}66return 0;67}6869// Writes a single register70static int mt2266_writereg(struct mt2266_priv *priv, u8 reg, u8 val)71{72u8 buf[2] = { reg, val };73struct i2c_msg msg = {74.addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = 275};76if (i2c_transfer(priv->i2c, &msg, 1) != 1) {77printk(KERN_WARNING "MT2266 I2C write failed\n");78return -EREMOTEIO;79}80return 0;81}8283// Writes a set of consecutive registers84static int mt2266_writeregs(struct mt2266_priv *priv,u8 *buf, u8 len)85{86struct i2c_msg msg = {87.addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = len88};89if (i2c_transfer(priv->i2c, &msg, 1) != 1) {90printk(KERN_WARNING "MT2266 I2C write failed (len=%i)\n",(int)len);91return -EREMOTEIO;92}93return 0;94}9596// Initialisation sequences97static u8 mt2266_init1[] = { REG_TUNE, 0x00, 0x00, 0x28,980x00, 0x52, 0x99, 0x3f };99100static u8 mt2266_init2[] = {1010x17, 0x6d, 0x71, 0x61, 0xc0, 0xbf, 0xff, 0xdc, 0x00, 0x0a, 0xd4,1020x03, 0x64, 0x64, 0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14,1030x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x7f, 0x5e, 0x3f, 0xff, 0xff,1040xff, 0x00, 0x77, 0x0f, 0x2d105};106107static u8 mt2266_init_8mhz[] = { REG_BANDWIDTH, 0x22, 0x22, 0x22, 0x22,1080x22, 0x22, 0x22, 0x22 };109110static u8 mt2266_init_7mhz[] = { REG_BANDWIDTH, 0x32, 0x32, 0x32, 0x32,1110x32, 0x32, 0x32, 0x32 };112113static u8 mt2266_init_6mhz[] = { REG_BANDWIDTH, 0xa7, 0xa7, 0xa7, 0xa7,1140xa7, 0xa7, 0xa7, 0xa7 };115116static u8 mt2266_uhf[] = { 0x1d, 0xdc, 0x00, 0x0a, 0xd4, 0x03, 0x64, 0x64,1170x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14 };118119static u8 mt2266_vhf[] = { 0x1d, 0xfe, 0x00, 0x00, 0xb4, 0x03, 0xa5, 0xa5,1200xa5, 0xa5, 0x82, 0xaa, 0xf1, 0x17, 0x80, 0x1f };121122#define FREF 30000 // Quartz oscillator 30 MHz123124static int mt2266_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)125{126struct mt2266_priv *priv;127int ret=0;128u32 freq;129u32 tune;130u8 lnaband;131u8 b[10];132int i;133u8 band;134135priv = fe->tuner_priv;136137freq = params->frequency / 1000; // Hz -> kHz138if (freq < 470000 && freq > 230000)139return -EINVAL; /* Gap between VHF and UHF bands */140priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;141priv->frequency = freq * 1000;142143tune = 2 * freq * (8192/16) / (FREF/16);144band = (freq < 300000) ? MT2266_VHF : MT2266_UHF;145if (band == MT2266_VHF)146tune *= 2;147148switch (params->u.ofdm.bandwidth) {149case BANDWIDTH_6_MHZ:150mt2266_writeregs(priv, mt2266_init_6mhz,151sizeof(mt2266_init_6mhz));152break;153case BANDWIDTH_7_MHZ:154mt2266_writeregs(priv, mt2266_init_7mhz,155sizeof(mt2266_init_7mhz));156break;157case BANDWIDTH_8_MHZ:158default:159mt2266_writeregs(priv, mt2266_init_8mhz,160sizeof(mt2266_init_8mhz));161break;162}163164if (band == MT2266_VHF && priv->band == MT2266_UHF) {165dprintk("Switch from UHF to VHF");166mt2266_writereg(priv, 0x05, 0x04);167mt2266_writereg(priv, 0x19, 0x61);168mt2266_writeregs(priv, mt2266_vhf, sizeof(mt2266_vhf));169} else if (band == MT2266_UHF && priv->band == MT2266_VHF) {170dprintk("Switch from VHF to UHF");171mt2266_writereg(priv, 0x05, 0x52);172mt2266_writereg(priv, 0x19, 0x61);173mt2266_writeregs(priv, mt2266_uhf, sizeof(mt2266_uhf));174}175msleep(10);176177if (freq <= 495000)178lnaband = 0xEE;179else if (freq <= 525000)180lnaband = 0xDD;181else if (freq <= 550000)182lnaband = 0xCC;183else if (freq <= 580000)184lnaband = 0xBB;185else if (freq <= 605000)186lnaband = 0xAA;187else if (freq <= 630000)188lnaband = 0x99;189else if (freq <= 655000)190lnaband = 0x88;191else if (freq <= 685000)192lnaband = 0x77;193else if (freq <= 710000)194lnaband = 0x66;195else if (freq <= 735000)196lnaband = 0x55;197else if (freq <= 765000)198lnaband = 0x44;199else if (freq <= 802000)200lnaband = 0x33;201else if (freq <= 840000)202lnaband = 0x22;203else204lnaband = 0x11;205206b[0] = REG_TUNE;207b[1] = (tune >> 8) & 0x1F;208b[2] = tune & 0xFF;209b[3] = tune >> 13;210mt2266_writeregs(priv,b,4);211212dprintk("set_parms: tune=%d band=%d %s",213(int) tune, (int) lnaband,214(band == MT2266_UHF) ? "UHF" : "VHF");215dprintk("set_parms: [1..3]: %2x %2x %2x",216(int) b[1], (int) b[2], (int)b[3]);217218if (band == MT2266_UHF) {219b[0] = 0x05;220b[1] = (priv->band == MT2266_VHF) ? 0x52 : 0x62;221b[2] = lnaband;222mt2266_writeregs(priv, b, 3);223}224225/* Wait for pll lock or timeout */226i = 0;227do {228mt2266_readreg(priv,REG_LOCK,b);229if (b[0] & 0x40)230break;231msleep(10);232i++;233} while (i<10);234dprintk("Lock when i=%i",(int)i);235236if (band == MT2266_UHF && priv->band == MT2266_VHF)237mt2266_writereg(priv, 0x05, 0x62);238239priv->band = band;240241return ret;242}243244static void mt2266_calibrate(struct mt2266_priv *priv)245{246mt2266_writereg(priv, 0x11, 0x03);247mt2266_writereg(priv, 0x11, 0x01);248mt2266_writeregs(priv, mt2266_init1, sizeof(mt2266_init1));249mt2266_writeregs(priv, mt2266_init2, sizeof(mt2266_init2));250mt2266_writereg(priv, 0x33, 0x5e);251mt2266_writereg(priv, 0x10, 0x10);252mt2266_writereg(priv, 0x10, 0x00);253mt2266_writeregs(priv, mt2266_init_8mhz, sizeof(mt2266_init_8mhz));254msleep(25);255mt2266_writereg(priv, 0x17, 0x6d);256mt2266_writereg(priv, 0x1c, 0x00);257msleep(75);258mt2266_writereg(priv, 0x17, 0x6d);259mt2266_writereg(priv, 0x1c, 0xff);260}261262static int mt2266_get_frequency(struct dvb_frontend *fe, u32 *frequency)263{264struct mt2266_priv *priv = fe->tuner_priv;265*frequency = priv->frequency;266return 0;267}268269static int mt2266_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)270{271struct mt2266_priv *priv = fe->tuner_priv;272*bandwidth = priv->bandwidth;273return 0;274}275276static int mt2266_init(struct dvb_frontend *fe)277{278int ret;279struct mt2266_priv *priv = fe->tuner_priv;280ret = mt2266_writereg(priv, 0x17, 0x6d);281if (ret < 0)282return ret;283ret = mt2266_writereg(priv, 0x1c, 0xff);284if (ret < 0)285return ret;286return 0;287}288289static int mt2266_sleep(struct dvb_frontend *fe)290{291struct mt2266_priv *priv = fe->tuner_priv;292mt2266_writereg(priv, 0x17, 0x6d);293mt2266_writereg(priv, 0x1c, 0x00);294return 0;295}296297static int mt2266_release(struct dvb_frontend *fe)298{299kfree(fe->tuner_priv);300fe->tuner_priv = NULL;301return 0;302}303304static const struct dvb_tuner_ops mt2266_tuner_ops = {305.info = {306.name = "Microtune MT2266",307.frequency_min = 174000000,308.frequency_max = 862000000,309.frequency_step = 50000,310},311.release = mt2266_release,312.init = mt2266_init,313.sleep = mt2266_sleep,314.set_params = mt2266_set_params,315.get_frequency = mt2266_get_frequency,316.get_bandwidth = mt2266_get_bandwidth317};318319struct dvb_frontend * mt2266_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2266_config *cfg)320{321struct mt2266_priv *priv = NULL;322u8 id = 0;323324priv = kzalloc(sizeof(struct mt2266_priv), GFP_KERNEL);325if (priv == NULL)326return NULL;327328priv->cfg = cfg;329priv->i2c = i2c;330priv->band = MT2266_UHF;331332if (mt2266_readreg(priv, 0, &id)) {333kfree(priv);334return NULL;335}336if (id != PART_REV) {337kfree(priv);338return NULL;339}340printk(KERN_INFO "MT2266: successfully identified\n");341memcpy(&fe->ops.tuner_ops, &mt2266_tuner_ops, sizeof(struct dvb_tuner_ops));342343fe->tuner_priv = priv;344mt2266_calibrate(priv);345return fe;346}347EXPORT_SYMBOL(mt2266_attach);348349MODULE_AUTHOR("Olivier DANET");350MODULE_DESCRIPTION("Microtune MT2266 silicon tuner driver");351MODULE_LICENSE("GPL");352353354