linux/drivers/media/dvb-frontends/zl10039.c
<<
>>
Prefs
   1/*
   2 *  Driver for Zarlink ZL10039 DVB-S tuner
   3 *
   4 *  Copyright 2007 Jan D. Louw <jd.louw@mweb.co.za>
   5 *
   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.
  10 *
  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 *
  15 *  GNU General Public License for more details.
  16 */
  17
  18#include <linux/module.h>
  19#include <linux/init.h>
  20#include <linux/string.h>
  21#include <linux/slab.h>
  22#include <linux/dvb/frontend.h>
  23
  24#include "dvb_frontend.h"
  25#include "zl10039.h"
  26
  27static int debug;
  28
  29/* Max transfer size done by I2C transfer functions */
  30#define MAX_XFER_SIZE  64
  31
  32#define dprintk(args...) \
  33        do { \
  34                if (debug) \
  35                        printk(KERN_DEBUG args); \
  36        } while (0)
  37
  38enum zl10039_model_id {
  39        ID_ZL10039 = 1
  40};
  41
  42struct zl10039_state {
  43        struct i2c_adapter *i2c;
  44        u8 i2c_addr;
  45        u8 id;
  46};
  47
  48enum zl10039_reg_addr {
  49        PLL0 = 0,
  50        PLL1,
  51        PLL2,
  52        PLL3,
  53        RFFE,
  54        BASE0,
  55        BASE1,
  56        BASE2,
  57        LO0,
  58        LO1,
  59        LO2,
  60        LO3,
  61        LO4,
  62        LO5,
  63        LO6,
  64        GENERAL
  65};
  66
  67static int zl10039_read(const struct zl10039_state *state,
  68                        const enum zl10039_reg_addr reg, u8 *buf,
  69                        const size_t count)
  70{
  71        u8 regbuf[] = { reg };
  72        struct i2c_msg msg[] = {
  73                {/* Write register address */
  74                        .addr = state->i2c_addr,
  75                        .flags = 0,
  76                        .buf = regbuf,
  77                        .len = 1,
  78                }, {/* Read count bytes */
  79                        .addr = state->i2c_addr,
  80                        .flags = I2C_M_RD,
  81                        .buf = buf,
  82                        .len = count,
  83                },
  84        };
  85
  86        dprintk("%s\n", __func__);
  87
  88        if (i2c_transfer(state->i2c, msg, 2) != 2) {
  89                dprintk("%s: i2c read error\n", __func__);
  90                return -EREMOTEIO;
  91        }
  92
  93        return 0; /* Success */
  94}
  95
  96static int zl10039_write(struct zl10039_state *state,
  97                        const enum zl10039_reg_addr reg, const u8 *src,
  98                        const size_t count)
  99{
 100        u8 buf[MAX_XFER_SIZE];
 101        struct i2c_msg msg = {
 102                .addr = state->i2c_addr,
 103                .flags = 0,
 104                .buf = buf,
 105                .len = count + 1,
 106        };
 107
 108        if (1 + count > sizeof(buf)) {
 109                printk(KERN_WARNING
 110                       "%s: i2c wr reg=%04x: len=%zu is too big!\n",
 111                       KBUILD_MODNAME, reg, count);
 112                return -EINVAL;
 113        }
 114
 115        dprintk("%s\n", __func__);
 116        /* Write register address and data in one go */
 117        buf[0] = reg;
 118        memcpy(&buf[1], src, count);
 119        if (i2c_transfer(state->i2c, &msg, 1) != 1) {
 120                dprintk("%s: i2c write error\n", __func__);
 121                return -EREMOTEIO;
 122        }
 123
 124        return 0; /* Success */
 125}
 126
 127static inline int zl10039_readreg(struct zl10039_state *state,
 128                                const enum zl10039_reg_addr reg, u8 *val)
 129{
 130        return zl10039_read(state, reg, val, 1);
 131}
 132
 133static inline int zl10039_writereg(struct zl10039_state *state,
 134                                const enum zl10039_reg_addr reg,
 135                                const u8 val)
 136{
 137        return zl10039_write(state, reg, &val, 1);
 138}
 139
 140static int zl10039_init(struct dvb_frontend *fe)
 141{
 142        struct zl10039_state *state = fe->tuner_priv;
 143        int ret;
 144
 145        dprintk("%s\n", __func__);
 146        if (fe->ops.i2c_gate_ctrl)
 147                fe->ops.i2c_gate_ctrl(fe, 1);
 148        /* Reset logic */
 149        ret = zl10039_writereg(state, GENERAL, 0x40);
 150        if (ret < 0) {
 151                dprintk("Note: i2c write error normal when resetting the tuner\n");
 152        }
 153        /* Wake up */
 154        ret = zl10039_writereg(state, GENERAL, 0x01);
 155        if (ret < 0) {
 156                dprintk("Tuner power up failed\n");
 157                return ret;
 158        }
 159        if (fe->ops.i2c_gate_ctrl)
 160                fe->ops.i2c_gate_ctrl(fe, 0);
 161
 162        return 0;
 163}
 164
 165static int zl10039_sleep(struct dvb_frontend *fe)
 166{
 167        struct zl10039_state *state = fe->tuner_priv;
 168        int ret;
 169
 170        dprintk("%s\n", __func__);
 171        if (fe->ops.i2c_gate_ctrl)
 172                fe->ops.i2c_gate_ctrl(fe, 1);
 173        ret = zl10039_writereg(state, GENERAL, 0x80);
 174        if (ret < 0) {
 175                dprintk("Tuner sleep failed\n");
 176                return ret;
 177        }
 178        if (fe->ops.i2c_gate_ctrl)
 179                fe->ops.i2c_gate_ctrl(fe, 0);
 180
 181        return 0;
 182}
 183
 184static int zl10039_set_params(struct dvb_frontend *fe)
 185{
 186        struct dtv_frontend_properties *c = &fe->dtv_property_cache;
 187        struct zl10039_state *state = fe->tuner_priv;
 188        u8 buf[6];
 189        u8 bf;
 190        u32 fbw;
 191        u32 div;
 192        int ret;
 193
 194        dprintk("%s\n", __func__);
 195        dprintk("Set frequency = %d, symbol rate = %d\n",
 196                        c->frequency, c->symbol_rate);
 197
 198        /* Assumed 10.111 MHz crystal oscillator */
 199        /* Cancelled num/den 80 to prevent overflow */
 200        div = (c->frequency * 1000) / 126387;
 201        fbw = (c->symbol_rate * 27) / 32000;
 202        /* Cancelled num/den 10 to prevent overflow */
 203        bf = ((fbw * 5088) / 1011100) - 1;
 204
 205        /*PLL divider*/
 206        buf[0] = (div >> 8) & 0x7f;
 207        buf[1] = (div >> 0) & 0xff;
 208        /*Reference divider*/
 209        /* Select reference ratio of 80 */
 210        buf[2] = 0x1D;
 211        /*PLL test modes*/
 212        buf[3] = 0x40;
 213        /*RF Control register*/
 214        buf[4] = 0x6E; /* Bypass enable */
 215        /*Baseband filter cutoff */
 216        buf[5] = bf;
 217
 218        /* Open i2c gate */
 219        if (fe->ops.i2c_gate_ctrl)
 220                fe->ops.i2c_gate_ctrl(fe, 1);
 221        /* BR = 10, Enable filter adjustment */
 222        ret = zl10039_writereg(state, BASE1, 0x0A);
 223        if (ret < 0)
 224                goto error;
 225        /* Write new config values */
 226        ret = zl10039_write(state, PLL0, buf, sizeof(buf));
 227        if (ret < 0)
 228                goto error;
 229        /* BR = 10, Disable filter adjustment */
 230        ret = zl10039_writereg(state, BASE1, 0x6A);
 231        if (ret < 0)
 232                goto error;
 233
 234        /* Close i2c gate */
 235        if (fe->ops.i2c_gate_ctrl)
 236                fe->ops.i2c_gate_ctrl(fe, 0);
 237        return 0;
 238error:
 239        dprintk("Error setting tuner\n");
 240        return ret;
 241}
 242
 243static void zl10039_release(struct dvb_frontend *fe)
 244{
 245        struct zl10039_state *state = fe->tuner_priv;
 246
 247        dprintk("%s\n", __func__);
 248        kfree(state);
 249        fe->tuner_priv = NULL;
 250}
 251
 252static const struct dvb_tuner_ops zl10039_ops = {
 253        .release = zl10039_release,
 254        .init = zl10039_init,
 255        .sleep = zl10039_sleep,
 256        .set_params = zl10039_set_params,
 257};
 258
 259struct dvb_frontend *zl10039_attach(struct dvb_frontend *fe,
 260                u8 i2c_addr, struct i2c_adapter *i2c)
 261{
 262        struct zl10039_state *state = NULL;
 263
 264        dprintk("%s\n", __func__);
 265        state = kmalloc(sizeof(struct zl10039_state), GFP_KERNEL);
 266        if (state == NULL)
 267                goto error;
 268
 269        state->i2c = i2c;
 270        state->i2c_addr = i2c_addr;
 271
 272        /* Open i2c gate */
 273        if (fe->ops.i2c_gate_ctrl)
 274                fe->ops.i2c_gate_ctrl(fe, 1);
 275        /* check if this is a valid tuner */
 276        if (zl10039_readreg(state, GENERAL, &state->id) < 0) {
 277                /* Close i2c gate */
 278                if (fe->ops.i2c_gate_ctrl)
 279                        fe->ops.i2c_gate_ctrl(fe, 0);
 280                goto error;
 281        }
 282        /* Close i2c gate */
 283        if (fe->ops.i2c_gate_ctrl)
 284                fe->ops.i2c_gate_ctrl(fe, 0);
 285
 286        state->id = state->id & 0x0f;
 287        switch (state->id) {
 288        case ID_ZL10039:
 289                strcpy(fe->ops.tuner_ops.info.name,
 290                        "Zarlink ZL10039 DVB-S tuner");
 291                break;
 292        default:
 293                dprintk("Chip ID=%x does not match a known type\n", state->id);
 294                goto error;
 295        }
 296
 297        memcpy(&fe->ops.tuner_ops, &zl10039_ops, sizeof(struct dvb_tuner_ops));
 298        fe->tuner_priv = state;
 299        dprintk("Tuner attached @ i2c address 0x%02x\n", i2c_addr);
 300        return fe;
 301error:
 302        kfree(state);
 303        return NULL;
 304}
 305EXPORT_SYMBOL(zl10039_attach);
 306
 307module_param(debug, int, 0644);
 308MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
 309MODULE_DESCRIPTION("Zarlink ZL10039 DVB-S tuner driver");
 310MODULE_AUTHOR("Jan D. Louw <jd.louw@mweb.co.za>");
 311MODULE_LICENSE("GPL");
 312