1 /* 2 * stv6110.c 3 * 4 * Driver for ST STV6110 satellite tuner IC. 5 * 6 * Copyright (C) 2009 NetUP Inc. 7 * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 23 */ 24 25 #include <linux/slab.h> 26 #include <linux/module.h> 27 #include <linux/dvb/frontend.h> 28 29 #include <linux/types.h> 30 31 #include "stv6110.h" 32 33 /* Max transfer size done by I2C transfer functions */ 34 #define MAX_XFER_SIZE 64 35 36 static int debug; 37 38 struct stv6110_priv { 39 int i2c_address; 40 struct i2c_adapter *i2c; 41 42 u32 mclk; 43 u8 clk_div; 44 u8 gain; 45 u8 regs[8]; 46 }; 47 48 #define dprintk(args...) \ 49 do { \ 50 if (debug) \ 51 printk(KERN_DEBUG args); \ 52 } while (0) 53 54 static s32 abssub(s32 a, s32 b) 55 { 56 if (a > b) 57 return a - b; 58 else 59 return b - a; 60 }; 61 62 static int stv6110_release(struct dvb_frontend *fe) 63 { 64 kfree(fe->tuner_priv); 65 fe->tuner_priv = NULL; 66 return 0; 67 } 68 69 static int stv6110_write_regs(struct dvb_frontend *fe, u8 buf[], 70 int start, int len) 71 { 72 struct stv6110_priv *priv = fe->tuner_priv; 73 int rc; 74 u8 cmdbuf[MAX_XFER_SIZE]; 75 struct i2c_msg msg = { 76 .addr = priv->i2c_address, 77 .flags = 0, 78 .buf = cmdbuf, 79 .len = len + 1 80 }; 81 82 dprintk("%s\n", __func__); 83 84 if (1 + len > sizeof(cmdbuf)) { 85 printk(KERN_WARNING 86 "%s: i2c wr: len=%d is too big!\n", 87 KBUILD_MODNAME, len); 88 return -EINVAL; 89 } 90 91 if (start + len > 8) 92 return -EINVAL; 93 94 memcpy(&cmdbuf[1], buf, len); 95 cmdbuf[0] = start; 96 97 if (fe->ops.i2c_gate_ctrl) 98 fe->ops.i2c_gate_ctrl(fe, 1); 99 100 rc = i2c_transfer(priv->i2c, &msg, 1); 101 if (rc != 1) 102 dprintk("%s: i2c error\n", __func__); 103 104 if (fe->ops.i2c_gate_ctrl) 105 fe->ops.i2c_gate_ctrl(fe, 0); 106 107 return 0; 108 } 109 110 static int stv6110_read_regs(struct dvb_frontend *fe, u8 regs[], 111 int start, int len) 112 { 113 struct stv6110_priv *priv = fe->tuner_priv; 114 int rc; 115 u8 reg[] = { start }; 116 struct i2c_msg msg[] = { 117 { 118 .addr = priv->i2c_address, 119 .flags = 0, 120 .buf = reg, 121 .len = 1, 122 }, { 123 .addr = priv->i2c_address, 124 .flags = I2C_M_RD, 125 .buf = regs, 126 .len = len, 127 }, 128 }; 129 130 if (fe->ops.i2c_gate_ctrl) 131 fe->ops.i2c_gate_ctrl(fe, 1); 132 133 rc = i2c_transfer(priv->i2c, msg, 2); 134 if (rc != 2) 135 dprintk("%s: i2c error\n", __func__); 136 137 if (fe->ops.i2c_gate_ctrl) 138 fe->ops.i2c_gate_ctrl(fe, 0); 139 140 memcpy(&priv->regs[start], regs, len); 141 142 return 0; 143 } 144 145 static int stv6110_read_reg(struct dvb_frontend *fe, int start) 146 { 147 u8 buf[] = { 0 }; 148 stv6110_read_regs(fe, buf, start, 1); 149 150 return buf[0]; 151 } 152 153 static int stv6110_sleep(struct dvb_frontend *fe) 154 { 155 u8 reg[] = { 0 }; 156 stv6110_write_regs(fe, reg, 0, 1); 157 158 return 0; 159 } 160 161 static u32 carrier_width(u32 symbol_rate, fe_rolloff_t rolloff) 162 { 163 u32 rlf; 164 165 switch (rolloff) { 166 case ROLLOFF_20: 167 rlf = 20; 168 break; 169 case ROLLOFF_25: 170 rlf = 25; 171 break; 172 default: 173 rlf = 35; 174 break; 175 } 176 177 return symbol_rate + ((symbol_rate * rlf) / 100); 178 } 179 180 static int stv6110_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth) 181 { 182 struct stv6110_priv *priv = fe->tuner_priv; 183 u8 r8, ret = 0x04; 184 int i; 185 186 if ((bandwidth / 2) > 36000000) /*BW/2 max=31+5=36 mhz for r8=31*/ 187 r8 = 31; 188 else if ((bandwidth / 2) < 5000000) /* BW/2 min=5Mhz for F=0 */ 189 r8 = 0; 190 else /*if 5 < BW/2 < 36*/ 191 r8 = (bandwidth / 2) / 1000000 - 5; 192 193 /* ctrl3, RCCLKOFF = 0 Activate the calibration Clock */ 194 /* ctrl3, CF = r8 Set the LPF value */ 195 priv->regs[RSTV6110_CTRL3] &= ~((1 << 6) | 0x1f); 196 priv->regs[RSTV6110_CTRL3] |= (r8 & 0x1f); 197 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1); 198 /* stat1, CALRCSTRT = 1 Start LPF auto calibration*/ 199 priv->regs[RSTV6110_STAT1] |= 0x02; 200 stv6110_write_regs(fe, &priv->regs[RSTV6110_STAT1], RSTV6110_STAT1, 1); 201 202 i = 0; 203 /* Wait for CALRCSTRT == 0 */ 204 while ((i < 10) && (ret != 0)) { 205 ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x02); 206 mdelay(1); /* wait for LPF auto calibration */ 207 i++; 208 } 209 210 /* RCCLKOFF = 1 calibration done, desactivate the calibration Clock */ 211 priv->regs[RSTV6110_CTRL3] |= (1 << 6); 212 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1); 213 return 0; 214 } 215 216 static int stv6110_init(struct dvb_frontend *fe) 217 { 218 struct stv6110_priv *priv = fe->tuner_priv; 219 u8 buf0[] = { 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e }; 220 221 memcpy(priv->regs, buf0, 8); 222 /* K = (Reference / 1000000) - 16 */ 223 priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3); 224 priv->regs[RSTV6110_CTRL1] |= 225 ((((priv->mclk / 1000000) - 16) & 0x1f) << 3); 226 227 /* divisor value for the output clock */ 228 priv->regs[RSTV6110_CTRL2] &= ~0xc0; 229 priv->regs[RSTV6110_CTRL2] |= (priv->clk_div << 6); 230 231 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1], RSTV6110_CTRL1, 8); 232 msleep(1); 233 stv6110_set_bandwidth(fe, 72000000); 234 235 return 0; 236 } 237 238 static int stv6110_get_frequency(struct dvb_frontend *fe, u32 *frequency) 239 { 240 struct stv6110_priv *priv = fe->tuner_priv; 241 u32 nbsteps, divider, psd2, freq; 242 u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 243 244 stv6110_read_regs(fe, regs, 0, 8); 245 /*N*/ 246 divider = (priv->regs[RSTV6110_TUNING2] & 0x0f) << 8; 247 divider += priv->regs[RSTV6110_TUNING1]; 248 249 /*R*/ 250 nbsteps = (priv->regs[RSTV6110_TUNING2] >> 6) & 3; 251 /*p*/ 252 psd2 = (priv->regs[RSTV6110_TUNING2] >> 4) & 1; 253 254 freq = divider * (priv->mclk / 1000); 255 freq /= (1 << (nbsteps + psd2)); 256 freq /= 4; 257 258 *frequency = freq; 259 260 return 0; 261 } 262 263 static int stv6110_set_frequency(struct dvb_frontend *fe, u32 frequency) 264 { 265 struct stv6110_priv *priv = fe->tuner_priv; 266 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 267 u8 ret = 0x04; 268 u32 divider, ref, p, presc, i, result_freq, vco_freq; 269 s32 p_calc, p_calc_opt = 1000, r_div, r_div_opt = 0, p_val; 270 s32 srate; 271 272 dprintk("%s, freq=%d kHz, mclk=%d Hz\n", __func__, 273 frequency, priv->mclk); 274 275 /* K = (Reference / 1000000) - 16 */ 276 priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3); 277 priv->regs[RSTV6110_CTRL1] |= 278 ((((priv->mclk / 1000000) - 16) & 0x1f) << 3); 279 280 /* BB_GAIN = db/2 */ 281 if (fe->ops.set_property && fe->ops.get_property) { 282 srate = c->symbol_rate; 283 dprintk("%s: Get Frontend parameters: srate=%d\n", 284 __func__, srate); 285 } else 286 srate = 15000000; 287 288 priv->regs[RSTV6110_CTRL2] &= ~0x0f; 289 priv->regs[RSTV6110_CTRL2] |= (priv->gain & 0x0f); 290 291 if (frequency <= 1023000) { 292 p = 1; 293 presc = 0; 294 } else if (frequency <= 1300000) { 295 p = 1; 296 presc = 1; 297 } else if (frequency <= 2046000) { 298 p = 0; 299 presc = 0; 300 } else { 301 p = 0; 302 presc = 1; 303 } 304 /* DIV4SEL = p*/ 305 priv->regs[RSTV6110_TUNING2] &= ~(1 << 4); 306 priv->regs[RSTV6110_TUNING2] |= (p << 4); 307 308 /* PRESC32ON = presc */ 309 priv->regs[RSTV6110_TUNING2] &= ~(1 << 5); 310 priv->regs[RSTV6110_TUNING2] |= (presc << 5); 311 312 p_val = (int)(1 << (p + 1)) * 10;/* P = 2 or P = 4 */ 313 for (r_div = 0; r_div <= 3; r_div++) { 314 p_calc = (priv->mclk / 100000); 315 p_calc /= (1 << (r_div + 1)); 316 if ((abssub(p_calc, p_val)) < (abssub(p_calc_opt, p_val))) 317 r_div_opt = r_div; 318 319 p_calc_opt = (priv->mclk / 100000); 320 p_calc_opt /= (1 << (r_div_opt + 1)); 321 } 322 323 ref = priv->mclk / ((1 << (r_div_opt + 1)) * (1 << (p + 1))); 324 divider = (((frequency * 1000) + (ref >> 1)) / ref); 325 326 /* RDIV = r_div_opt */ 327 priv->regs[RSTV6110_TUNING2] &= ~(3 << 6); 328 priv->regs[RSTV6110_TUNING2] |= (((r_div_opt) & 3) << 6); 329 330 /* NDIV_MSB = MSB(divider) */ 331 priv->regs[RSTV6110_TUNING2] &= ~0x0f; 332 priv->regs[RSTV6110_TUNING2] |= (((divider) >> 8) & 0x0f); 333 334 /* NDIV_LSB, LSB(divider) */ 335 priv->regs[RSTV6110_TUNING1] = (divider & 0xff); 336 337 /* CALVCOSTRT = 1 VCO Auto Calibration */ 338 priv->regs[RSTV6110_STAT1] |= 0x04; 339 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1], 340 RSTV6110_CTRL1, 8); 341 342 i = 0; 343 /* Wait for CALVCOSTRT == 0 */ 344 while ((i < 10) && (ret != 0)) { 345 ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x04); 346 msleep(1); /* wait for VCO auto calibration */ 347 i++; 348 } 349 350 ret = stv6110_read_reg(fe, RSTV6110_STAT1); 351 stv6110_get_frequency(fe, &result_freq); 352 353 vco_freq = divider * ((priv->mclk / 1000) / ((1 << (r_div_opt + 1)))); 354 dprintk("%s, stat1=%x, lo_freq=%d kHz, vco_frec=%d kHz\n", __func__, 355 ret, result_freq, vco_freq); 356 357 return 0; 358 } 359 360 static int stv6110_set_params(struct dvb_frontend *fe) 361 { 362 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 363 u32 bandwidth = carrier_width(c->symbol_rate, c->rolloff); 364 365 stv6110_set_frequency(fe, c->frequency); 366 stv6110_set_bandwidth(fe, bandwidth); 367 368 return 0; 369 } 370 371 static int stv6110_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth) 372 { 373 struct stv6110_priv *priv = fe->tuner_priv; 374 u8 r8 = 0; 375 u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 376 stv6110_read_regs(fe, regs, 0, 8); 377 378 /* CF */ 379 r8 = priv->regs[RSTV6110_CTRL3] & 0x1f; 380 *bandwidth = (r8 + 5) * 2000000;/* x2 for ZIF tuner BW/2 = F+5 Mhz */ 381 382 return 0; 383 } 384 385 static struct dvb_tuner_ops stv6110_tuner_ops = { 386 .info = { 387 .name = "ST STV6110", 388 .frequency_min = 950000, 389 .frequency_max = 2150000, 390 .frequency_step = 1000, 391 }, 392 .init = stv6110_init, 393 .release = stv6110_release, 394 .sleep = stv6110_sleep, 395 .set_params = stv6110_set_params, 396 .get_frequency = stv6110_get_frequency, 397 .set_frequency = stv6110_set_frequency, 398 .get_bandwidth = stv6110_get_bandwidth, 399 .set_bandwidth = stv6110_set_bandwidth, 400 401 }; 402 403 struct dvb_frontend *stv6110_attach(struct dvb_frontend *fe, 404 const struct stv6110_config *config, 405 struct i2c_adapter *i2c) 406 { 407 struct stv6110_priv *priv = NULL; 408 u8 reg0[] = { 0x00, 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e }; 409 410 struct i2c_msg msg[] = { 411 { 412 .addr = config->i2c_address, 413 .flags = 0, 414 .buf = reg0, 415 .len = 9 416 } 417 }; 418 int ret; 419 420 /* divisor value for the output clock */ 421 reg0[2] &= ~0xc0; 422 reg0[2] |= (config->clk_div << 6); 423 424 if (fe->ops.i2c_gate_ctrl) 425 fe->ops.i2c_gate_ctrl(fe, 1); 426 427 ret = i2c_transfer(i2c, msg, 1); 428 429 if (fe->ops.i2c_gate_ctrl) 430 fe->ops.i2c_gate_ctrl(fe, 0); 431 432 if (ret != 1) 433 return NULL; 434 435 priv = kzalloc(sizeof(struct stv6110_priv), GFP_KERNEL); 436 if (priv == NULL) 437 return NULL; 438 439 priv->i2c_address = config->i2c_address; 440 priv->i2c = i2c; 441 priv->mclk = config->mclk; 442 priv->clk_div = config->clk_div; 443 priv->gain = config->gain; 444 445 memcpy(&priv->regs, ®0[1], 8); 446 447 memcpy(&fe->ops.tuner_ops, &stv6110_tuner_ops, 448 sizeof(struct dvb_tuner_ops)); 449 fe->tuner_priv = priv; 450 printk(KERN_INFO "STV6110 attached on addr=%x!\n", priv->i2c_address); 451 452 return fe; 453 } 454 EXPORT_SYMBOL(stv6110_attach); 455 456 module_param(debug, int, 0644); 457 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); 458 459 MODULE_DESCRIPTION("ST STV6110 driver"); 460 MODULE_AUTHOR("Igor M. Liplianin"); 461 MODULE_LICENSE("GPL"); 462