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 <media/dvb_frontend.h> 25 #include "zl10039.h" 26 27 static 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 38 enum zl10039_model_id { 39 ID_ZL10039 = 1 40 }; 41 42 struct zl10039_state { 43 struct i2c_adapter *i2c; 44 u8 i2c_addr; 45 u8 id; 46 }; 47 48 enum 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 67 static 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 96 static 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 127 static 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 133 static inline int zl10039_writereg(struct zl10039_state *state, 134 const enum zl10039_reg_addr reg, 135 const u8 val) 136 { 137 const u8 tmp = val; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */ 138 139 return zl10039_write(state, reg, &tmp, 1); 140 } 141 142 static int zl10039_init(struct dvb_frontend *fe) 143 { 144 struct zl10039_state *state = fe->tuner_priv; 145 int ret; 146 147 dprintk("%s\n", __func__); 148 if (fe->ops.i2c_gate_ctrl) 149 fe->ops.i2c_gate_ctrl(fe, 1); 150 /* Reset logic */ 151 ret = zl10039_writereg(state, GENERAL, 0x40); 152 if (ret < 0) { 153 dprintk("Note: i2c write error normal when resetting the tuner\n"); 154 } 155 /* Wake up */ 156 ret = zl10039_writereg(state, GENERAL, 0x01); 157 if (ret < 0) { 158 dprintk("Tuner power up failed\n"); 159 return ret; 160 } 161 if (fe->ops.i2c_gate_ctrl) 162 fe->ops.i2c_gate_ctrl(fe, 0); 163 164 return 0; 165 } 166 167 static int zl10039_sleep(struct dvb_frontend *fe) 168 { 169 struct zl10039_state *state = fe->tuner_priv; 170 int ret; 171 172 dprintk("%s\n", __func__); 173 if (fe->ops.i2c_gate_ctrl) 174 fe->ops.i2c_gate_ctrl(fe, 1); 175 ret = zl10039_writereg(state, GENERAL, 0x80); 176 if (ret < 0) { 177 dprintk("Tuner sleep failed\n"); 178 return ret; 179 } 180 if (fe->ops.i2c_gate_ctrl) 181 fe->ops.i2c_gate_ctrl(fe, 0); 182 183 return 0; 184 } 185 186 static int zl10039_set_params(struct dvb_frontend *fe) 187 { 188 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 189 struct zl10039_state *state = fe->tuner_priv; 190 u8 buf[6]; 191 u8 bf; 192 u32 fbw; 193 u32 div; 194 int ret; 195 196 dprintk("%s\n", __func__); 197 dprintk("Set frequency = %d, symbol rate = %d\n", 198 c->frequency, c->symbol_rate); 199 200 /* Assumed 10.111 MHz crystal oscillator */ 201 /* Cancelled num/den 80 to prevent overflow */ 202 div = (c->frequency * 1000) / 126387; 203 fbw = (c->symbol_rate * 27) / 32000; 204 /* Cancelled num/den 10 to prevent overflow */ 205 bf = ((fbw * 5088) / 1011100) - 1; 206 207 /*PLL divider*/ 208 buf[0] = (div >> 8) & 0x7f; 209 buf[1] = (div >> 0) & 0xff; 210 /*Reference divider*/ 211 /* Select reference ratio of 80 */ 212 buf[2] = 0x1D; 213 /*PLL test modes*/ 214 buf[3] = 0x40; 215 /*RF Control register*/ 216 buf[4] = 0x6E; /* Bypass enable */ 217 /*Baseband filter cutoff */ 218 buf[5] = bf; 219 220 /* Open i2c gate */ 221 if (fe->ops.i2c_gate_ctrl) 222 fe->ops.i2c_gate_ctrl(fe, 1); 223 /* BR = 10, Enable filter adjustment */ 224 ret = zl10039_writereg(state, BASE1, 0x0A); 225 if (ret < 0) 226 goto error; 227 /* Write new config values */ 228 ret = zl10039_write(state, PLL0, buf, sizeof(buf)); 229 if (ret < 0) 230 goto error; 231 /* BR = 10, Disable filter adjustment */ 232 ret = zl10039_writereg(state, BASE1, 0x6A); 233 if (ret < 0) 234 goto error; 235 236 /* Close i2c gate */ 237 if (fe->ops.i2c_gate_ctrl) 238 fe->ops.i2c_gate_ctrl(fe, 0); 239 return 0; 240 error: 241 dprintk("Error setting tuner\n"); 242 return ret; 243 } 244 245 static void zl10039_release(struct dvb_frontend *fe) 246 { 247 struct zl10039_state *state = fe->tuner_priv; 248 249 dprintk("%s\n", __func__); 250 kfree(state); 251 fe->tuner_priv = NULL; 252 } 253 254 static const struct dvb_tuner_ops zl10039_ops = { 255 .release = zl10039_release, 256 .init = zl10039_init, 257 .sleep = zl10039_sleep, 258 .set_params = zl10039_set_params, 259 }; 260 261 struct dvb_frontend *zl10039_attach(struct dvb_frontend *fe, 262 u8 i2c_addr, struct i2c_adapter *i2c) 263 { 264 struct zl10039_state *state = NULL; 265 266 dprintk("%s\n", __func__); 267 state = kmalloc(sizeof(struct zl10039_state), GFP_KERNEL); 268 if (state == NULL) 269 goto error; 270 271 state->i2c = i2c; 272 state->i2c_addr = i2c_addr; 273 274 /* Open i2c gate */ 275 if (fe->ops.i2c_gate_ctrl) 276 fe->ops.i2c_gate_ctrl(fe, 1); 277 /* check if this is a valid tuner */ 278 if (zl10039_readreg(state, GENERAL, &state->id) < 0) { 279 /* Close i2c gate */ 280 if (fe->ops.i2c_gate_ctrl) 281 fe->ops.i2c_gate_ctrl(fe, 0); 282 goto error; 283 } 284 /* Close i2c gate */ 285 if (fe->ops.i2c_gate_ctrl) 286 fe->ops.i2c_gate_ctrl(fe, 0); 287 288 state->id = state->id & 0x0f; 289 switch (state->id) { 290 case ID_ZL10039: 291 strscpy(fe->ops.tuner_ops.info.name, 292 "Zarlink ZL10039 DVB-S tuner", 293 sizeof(fe->ops.tuner_ops.info.name)); 294 break; 295 default: 296 dprintk("Chip ID=%x does not match a known type\n", state->id); 297 goto error; 298 } 299 300 memcpy(&fe->ops.tuner_ops, &zl10039_ops, sizeof(struct dvb_tuner_ops)); 301 fe->tuner_priv = state; 302 dprintk("Tuner attached @ i2c address 0x%02x\n", i2c_addr); 303 return fe; 304 error: 305 kfree(state); 306 return NULL; 307 } 308 EXPORT_SYMBOL(zl10039_attach); 309 310 module_param(debug, int, 0644); 311 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); 312 MODULE_DESCRIPTION("Zarlink ZL10039 DVB-S tuner driver"); 313 MODULE_AUTHOR("Jan D. Louw <jd.louw@mweb.co.za>"); 314 MODULE_LICENSE("GPL"); 315