2  *  Driver for Microtune MT2266 "Direct conversion low power broadband tuner"
 
   4  *  Copyright (c) 2007 Olivier DANET <odanet@caramail.com>
 
   6  *  This program is free software; you can redistribute it and/or modify
 
   7  *  it under the terms of the GNU General Public License as published by
 
   8  *  the Free Software Foundation; either version 2 of the License, or
 
   9  *  (at your option) any later version.
 
  11  *  This program is distributed in the hope that it will be useful,
 
  12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  14  *  GNU General Public License for more details.
 
  17 #include <linux/module.h>
 
  18 #include <linux/delay.h>
 
  19 #include <linux/dvb/frontend.h>
 
  20 #include <linux/i2c.h>
 
  22 #include "dvb_frontend.h"
 
  25 #define I2C_ADDRESS 0x60
 
  27 #define REG_PART_REV   0
 
  30 #define REG_BANDWIDTH  8
 
  36         struct mt2266_config *cfg;
 
  37         struct i2c_adapter   *i2c;
 
  47 /* Here, frequencies are expressed in kiloHertz to avoid 32 bits overflows */
 
  50 module_param(debug, int, 0644);
 
  51 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
 
  53 #define dprintk(args...) do { if (debug) {printk(KERN_DEBUG "MT2266: " args); printk("\n"); }} while (0)
 
  55 // Reads a single register
 
  56 static int mt2266_readreg(struct mt2266_priv *priv, u8 reg, u8 *val)
 
  58         struct 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 },
 
  62         if (i2c_transfer(priv->i2c, msg, 2) != 2) {
 
  63                 printk(KERN_WARNING "MT2266 I2C read failed\n");
 
  69 // Writes a single register
 
  70 static int mt2266_writereg(struct mt2266_priv *priv, u8 reg, u8 val)
 
  72         u8 buf[2] = { reg, val };
 
  73         struct i2c_msg msg = {
 
  74                 .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = 2
 
  76         if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
 
  77                 printk(KERN_WARNING "MT2266 I2C write failed\n");
 
  83 // Writes a set of consecutive registers
 
  84 static int mt2266_writeregs(struct mt2266_priv *priv,u8 *buf, u8 len)
 
  86         struct i2c_msg msg = {
 
  87                 .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = len
 
  89         if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
 
  90                 printk(KERN_WARNING "MT2266 I2C write failed (len=%i)\n",(int)len);
 
  96 // Initialisation sequences
 
  97 static u8 mt2266_init1[] = { REG_TUNE, 0x00, 0x00, 0x28,
 
  98                                  0x00, 0x52, 0x99, 0x3f };
 
 100 static u8 mt2266_init2[] = {
 
 101     0x17, 0x6d, 0x71, 0x61, 0xc0, 0xbf, 0xff, 0xdc, 0x00, 0x0a, 0xd4,
 
 102     0x03, 0x64, 0x64, 0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14,
 
 103     0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x7f, 0x5e, 0x3f, 0xff, 0xff,
 
 104     0xff, 0x00, 0x77, 0x0f, 0x2d
 
 107 static u8 mt2266_init_8mhz[] = { REG_BANDWIDTH, 0x22, 0x22, 0x22, 0x22,
 
 108                                                 0x22, 0x22, 0x22, 0x22 };
 
 110 static u8 mt2266_init_7mhz[] = { REG_BANDWIDTH, 0x32, 0x32, 0x32, 0x32,
 
 111                                                 0x32, 0x32, 0x32, 0x32 };
 
 113 static u8 mt2266_init_6mhz[] = { REG_BANDWIDTH, 0xa7, 0xa7, 0xa7, 0xa7,
 
 114                                                 0xa7, 0xa7, 0xa7, 0xa7 };
 
 116 static u8 mt2266_uhf[] = { 0x1d, 0xdc, 0x00, 0x0a, 0xd4, 0x03, 0x64, 0x64,
 
 117                            0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14 };
 
 119 static u8 mt2266_vhf[] = { 0x1d, 0xfe, 0x00, 0x00, 0xb4, 0x03, 0xa5, 0xa5,
 
 120                            0xa5, 0xa5, 0x82, 0xaa, 0xf1, 0x17, 0x80, 0x1f };
 
 122 #define FREF 30000       // Quartz oscillator 30 MHz
 
 124 static int mt2266_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
 
 126         struct mt2266_priv *priv;
 
 135         priv = fe->tuner_priv;
 
 137         freq = params->frequency / 1000; // Hz -> kHz
 
 138         if (freq < 470000 && freq > 230000)
 
 139                 return -EINVAL; /* Gap between VHF and UHF bands */
 
 140         priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
 
 141         priv->frequency = freq * 1000;
 
 143         tune = 2 * freq * (8192/16) / (FREF/16);
 
 144         band = (freq < 300000) ? MT2266_VHF : MT2266_UHF;
 
 145         if (band == MT2266_VHF)
 
 148         switch (params->u.ofdm.bandwidth) {
 
 149         case BANDWIDTH_6_MHZ:
 
 150                 mt2266_writeregs(priv, mt2266_init_6mhz,
 
 151                                  sizeof(mt2266_init_6mhz));
 
 153         case BANDWIDTH_7_MHZ:
 
 154                 mt2266_writeregs(priv, mt2266_init_7mhz,
 
 155                                  sizeof(mt2266_init_7mhz));
 
 157         case BANDWIDTH_8_MHZ:
 
 159                 mt2266_writeregs(priv, mt2266_init_8mhz,
 
 160                                  sizeof(mt2266_init_8mhz));
 
 164         if (band == MT2266_VHF && priv->band == MT2266_UHF) {
 
 165                 dprintk("Switch from UHF to VHF");
 
 166                 mt2266_writereg(priv, 0x05, 0x04);
 
 167                 mt2266_writereg(priv, 0x19, 0x61);
 
 168                 mt2266_writeregs(priv, mt2266_vhf, sizeof(mt2266_vhf));
 
 169         } else if (band == MT2266_UHF && priv->band == MT2266_VHF) {
 
 170                 dprintk("Switch from VHF to UHF");
 
 171                 mt2266_writereg(priv, 0x05, 0x52);
 
 172                 mt2266_writereg(priv, 0x19, 0x61);
 
 173                 mt2266_writeregs(priv, mt2266_uhf, sizeof(mt2266_uhf));
 
 179         else if (freq <= 525000)
 
 181         else if (freq <= 550000)
 
 183         else if (freq <= 580000)
 
 185         else if (freq <= 605000)
 
 187         else if (freq <= 630000)
 
 189         else if (freq <= 655000)
 
 191         else if (freq <= 685000)
 
 193         else if (freq <= 710000)
 
 195         else if (freq <= 735000)
 
 197         else if (freq <= 765000)
 
 199         else if (freq <= 802000)
 
 201         else if (freq <= 840000)
 
 207         b[1] = (tune >> 8) & 0x1F;
 
 210         mt2266_writeregs(priv,b,4);
 
 212         dprintk("set_parms: tune=%d band=%d %s",
 
 213                 (int) tune, (int) lnaband,
 
 214                 (band == MT2266_UHF) ? "UHF" : "VHF");
 
 215         dprintk("set_parms: [1..3]: %2x %2x %2x",
 
 216                 (int) b[1], (int) b[2], (int)b[3]);
 
 218         if (band == MT2266_UHF) {
 
 220                 b[1] = (priv->band == MT2266_VHF) ? 0x52 : 0x62;
 
 222                 mt2266_writeregs(priv, b, 3);
 
 225         /* Wait for pll lock or timeout */
 
 228                 mt2266_readreg(priv,REG_LOCK,b);
 
 234         dprintk("Lock when i=%i",(int)i);
 
 236         if (band == MT2266_UHF && priv->band == MT2266_VHF)
 
 237                 mt2266_writereg(priv, 0x05, 0x62);
 
 244 static void mt2266_calibrate(struct mt2266_priv *priv)
 
 246         mt2266_writereg(priv, 0x11, 0x03);
 
 247         mt2266_writereg(priv, 0x11, 0x01);
 
 248         mt2266_writeregs(priv, mt2266_init1, sizeof(mt2266_init1));
 
 249         mt2266_writeregs(priv, mt2266_init2, sizeof(mt2266_init2));
 
 250         mt2266_writereg(priv, 0x33, 0x5e);
 
 251         mt2266_writereg(priv, 0x10, 0x10);
 
 252         mt2266_writereg(priv, 0x10, 0x00);
 
 253         mt2266_writeregs(priv, mt2266_init_8mhz, sizeof(mt2266_init_8mhz));
 
 255         mt2266_writereg(priv, 0x17, 0x6d);
 
 256         mt2266_writereg(priv, 0x1c, 0x00);
 
 258         mt2266_writereg(priv, 0x17, 0x6d);
 
 259         mt2266_writereg(priv, 0x1c, 0xff);
 
 262 static int mt2266_get_frequency(struct dvb_frontend *fe, u32 *frequency)
 
 264         struct mt2266_priv *priv = fe->tuner_priv;
 
 265         *frequency = priv->frequency;
 
 269 static int mt2266_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
 
 271         struct mt2266_priv *priv = fe->tuner_priv;
 
 272         *bandwidth = priv->bandwidth;
 
 276 static int mt2266_init(struct dvb_frontend *fe)
 
 279         struct mt2266_priv *priv = fe->tuner_priv;
 
 280         ret = mt2266_writereg(priv, 0x17, 0x6d);
 
 283         ret = mt2266_writereg(priv, 0x1c, 0xff);
 
 289 static int mt2266_sleep(struct dvb_frontend *fe)
 
 291         struct mt2266_priv *priv = fe->tuner_priv;
 
 292         mt2266_writereg(priv, 0x17, 0x6d);
 
 293         mt2266_writereg(priv, 0x1c, 0x00);
 
 297 static int mt2266_release(struct dvb_frontend *fe)
 
 299         kfree(fe->tuner_priv);
 
 300         fe->tuner_priv = NULL;
 
 304 static const struct dvb_tuner_ops mt2266_tuner_ops = {
 
 306                 .name           = "Microtune MT2266",
 
 307                 .frequency_min  = 174000000,
 
 308                 .frequency_max  = 862000000,
 
 309                 .frequency_step =     50000,
 
 311         .release       = mt2266_release,
 
 313         .sleep         = mt2266_sleep,
 
 314         .set_params    = mt2266_set_params,
 
 315         .get_frequency = mt2266_get_frequency,
 
 316         .get_bandwidth = mt2266_get_bandwidth
 
 319 struct dvb_frontend * mt2266_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2266_config *cfg)
 
 321         struct mt2266_priv *priv = NULL;
 
 324         priv = kzalloc(sizeof(struct mt2266_priv), GFP_KERNEL);
 
 330         priv->band     = MT2266_UHF;
 
 332         if (mt2266_readreg(priv, 0, &id)) {
 
 336         if (id != PART_REV) {
 
 340         printk(KERN_INFO "MT2266: successfully identified\n");
 
 341         memcpy(&fe->ops.tuner_ops, &mt2266_tuner_ops, sizeof(struct dvb_tuner_ops));
 
 343         fe->tuner_priv = priv;
 
 344         mt2266_calibrate(priv);
 
 347 EXPORT_SYMBOL(mt2266_attach);
 
 349 MODULE_AUTHOR("Olivier DANET");
 
 350 MODULE_DESCRIPTION("Microtune MT2266 silicon tuner driver");
 
 351 MODULE_LICENSE("GPL");