1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 STV6110(A) Silicon tuner driver 4 5 Copyright (C) Manu Abraham <abraham.manu@gmail.com> 6 7 Copyright (C) ST Microelectronics 8 9 */ 10 11 #include <linux/init.h> 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/slab.h> 15 #include <linux/string.h> 16 17 #include <media/dvb_frontend.h> 18 19 #include "stv6110x_reg.h" 20 #include "stv6110x.h" 21 #include "stv6110x_priv.h" 22 23 /* Max transfer size done by I2C transfer functions */ 24 #define MAX_XFER_SIZE 64 25 26 static unsigned int verbose; 27 module_param(verbose, int, 0644); 28 MODULE_PARM_DESC(verbose, "Set Verbosity level"); 29 30 static int stv6110x_read_reg(struct stv6110x_state *stv6110x, u8 reg, u8 *data) 31 { 32 int ret; 33 const struct stv6110x_config *config = stv6110x->config; 34 u8 b0[] = { reg }; 35 u8 b1[] = { 0 }; 36 struct i2c_msg msg[] = { 37 { .addr = config->addr, .flags = 0, .buf = b0, .len = 1 }, 38 { .addr = config->addr, .flags = I2C_M_RD, .buf = b1, .len = 1 } 39 }; 40 41 ret = i2c_transfer(stv6110x->i2c, msg, 2); 42 if (ret != 2) { 43 dprintk(FE_ERROR, 1, "I/O Error"); 44 return -EREMOTEIO; 45 } 46 *data = b1[0]; 47 48 return 0; 49 } 50 51 static int stv6110x_write_regs(struct stv6110x_state *stv6110x, int start, u8 data[], int len) 52 { 53 int ret; 54 const struct stv6110x_config *config = stv6110x->config; 55 u8 buf[MAX_XFER_SIZE]; 56 57 struct i2c_msg msg = { 58 .addr = config->addr, 59 .flags = 0, 60 .buf = buf, 61 .len = len + 1 62 }; 63 64 if (1 + len > sizeof(buf)) { 65 printk(KERN_WARNING 66 "%s: i2c wr: len=%d is too big!\n", 67 KBUILD_MODNAME, len); 68 return -EINVAL; 69 } 70 71 if (start + len > 8) 72 return -EINVAL; 73 74 buf[0] = start; 75 memcpy(&buf[1], data, len); 76 77 ret = i2c_transfer(stv6110x->i2c, &msg, 1); 78 if (ret != 1) { 79 dprintk(FE_ERROR, 1, "I/O Error"); 80 return -EREMOTEIO; 81 } 82 83 return 0; 84 } 85 86 static int stv6110x_write_reg(struct stv6110x_state *stv6110x, u8 reg, u8 data) 87 { 88 u8 tmp = data; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */ 89 90 return stv6110x_write_regs(stv6110x, reg, &tmp, 1); 91 } 92 93 static int stv6110x_init(struct dvb_frontend *fe) 94 { 95 struct stv6110x_state *stv6110x = fe->tuner_priv; 96 int ret; 97 98 ret = stv6110x_write_regs(stv6110x, 0, stv6110x->regs, 99 ARRAY_SIZE(stv6110x->regs)); 100 if (ret < 0) { 101 dprintk(FE_ERROR, 1, "Initialization failed"); 102 return -1; 103 } 104 105 return 0; 106 } 107 108 static int stv6110x_set_frequency(struct dvb_frontend *fe, u32 frequency) 109 { 110 struct stv6110x_state *stv6110x = fe->tuner_priv; 111 u32 rDiv, divider; 112 s32 pVal, pCalc, rDivOpt = 0, pCalcOpt = 1000; 113 u8 i; 114 115 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_K, (REFCLOCK_MHz - 16)); 116 117 if (frequency <= 1023000) { 118 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 1); 119 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0); 120 pVal = 40; 121 } else if (frequency <= 1300000) { 122 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 1); 123 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1); 124 pVal = 40; 125 } else if (frequency <= 2046000) { 126 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 0); 127 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0); 128 pVal = 20; 129 } else { 130 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 0); 131 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1); 132 pVal = 20; 133 } 134 135 for (rDiv = 0; rDiv <= 3; rDiv++) { 136 pCalc = (REFCLOCK_kHz / 100) / R_DIV(rDiv); 137 138 if ((abs((s32)(pCalc - pVal))) < (abs((s32)(pCalcOpt - pVal)))) 139 rDivOpt = rDiv; 140 141 pCalcOpt = (REFCLOCK_kHz / 100) / R_DIV(rDivOpt); 142 } 143 144 divider = (frequency * R_DIV(rDivOpt) * pVal) / REFCLOCK_kHz; 145 divider = (divider + 5) / 10; 146 147 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_R_DIV, rDivOpt); 148 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_N_DIV_11_8, MSB(divider)); 149 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG0], TNG0_N_DIV_7_0, LSB(divider)); 150 151 /* VCO Auto calibration */ 152 STV6110x_SETFIELD(stv6110x->regs[STV6110x_STAT1], STAT1_CALVCO_STRT, 1); 153 154 stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x->regs[STV6110x_CTRL1]); 155 stv6110x_write_reg(stv6110x, STV6110x_TNG1, stv6110x->regs[STV6110x_TNG1]); 156 stv6110x_write_reg(stv6110x, STV6110x_TNG0, stv6110x->regs[STV6110x_TNG0]); 157 stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x->regs[STV6110x_STAT1]); 158 159 for (i = 0; i < TRIALS; i++) { 160 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]); 161 if (!STV6110x_GETFIELD(STAT1_CALVCO_STRT, stv6110x->regs[STV6110x_STAT1])) 162 break; 163 msleep(1); 164 } 165 166 return 0; 167 } 168 169 static int stv6110x_get_frequency(struct dvb_frontend *fe, u32 *frequency) 170 { 171 struct stv6110x_state *stv6110x = fe->tuner_priv; 172 173 stv6110x_read_reg(stv6110x, STV6110x_TNG1, &stv6110x->regs[STV6110x_TNG1]); 174 stv6110x_read_reg(stv6110x, STV6110x_TNG0, &stv6110x->regs[STV6110x_TNG0]); 175 176 *frequency = (MAKEWORD16(STV6110x_GETFIELD(TNG1_N_DIV_11_8, stv6110x->regs[STV6110x_TNG1]), 177 STV6110x_GETFIELD(TNG0_N_DIV_7_0, stv6110x->regs[STV6110x_TNG0]))) * REFCLOCK_kHz; 178 179 *frequency /= (1 << (STV6110x_GETFIELD(TNG1_R_DIV, stv6110x->regs[STV6110x_TNG1]) + 180 STV6110x_GETFIELD(TNG1_DIV4SEL, stv6110x->regs[STV6110x_TNG1]))); 181 182 *frequency >>= 2; 183 184 return 0; 185 } 186 187 static int stv6110x_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth) 188 { 189 struct stv6110x_state *stv6110x = fe->tuner_priv; 190 u32 halfbw; 191 u8 i; 192 193 halfbw = bandwidth >> 1; 194 195 if (halfbw > 36000000) 196 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, 31); /* LPF */ 197 else if (halfbw < 5000000) 198 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, 0); /* LPF */ 199 else 200 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, ((halfbw / 1000000) - 5)); /* LPF */ 201 202 203 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x0); /* cal. clk activated */ 204 STV6110x_SETFIELD(stv6110x->regs[STV6110x_STAT1], STAT1_CALRC_STRT, 0x1); /* LPF auto cal */ 205 206 stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x->regs[STV6110x_CTRL3]); 207 stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x->regs[STV6110x_STAT1]); 208 209 for (i = 0; i < TRIALS; i++) { 210 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]); 211 if (!STV6110x_GETFIELD(STAT1_CALRC_STRT, stv6110x->regs[STV6110x_STAT1])) 212 break; 213 msleep(1); 214 } 215 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x1); /* cal. done */ 216 stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x->regs[STV6110x_CTRL3]); 217 218 return 0; 219 } 220 221 static int stv6110x_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth) 222 { 223 struct stv6110x_state *stv6110x = fe->tuner_priv; 224 225 stv6110x_read_reg(stv6110x, STV6110x_CTRL3, &stv6110x->regs[STV6110x_CTRL3]); 226 *bandwidth = (STV6110x_GETFIELD(CTRL3_CF, stv6110x->regs[STV6110x_CTRL3]) + 5) * 2000000; 227 228 return 0; 229 } 230 231 static int stv6110x_set_refclock(struct dvb_frontend *fe, u32 refclock) 232 { 233 struct stv6110x_state *stv6110x = fe->tuner_priv; 234 235 /* setup divider */ 236 switch (refclock) { 237 default: 238 case 1: 239 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 0); 240 break; 241 case 2: 242 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 1); 243 break; 244 case 4: 245 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 2); 246 break; 247 case 8: 248 case 0: 249 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 3); 250 break; 251 } 252 stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x->regs[STV6110x_CTRL2]); 253 254 return 0; 255 } 256 257 static int stv6110x_get_bbgain(struct dvb_frontend *fe, u32 *gain) 258 { 259 struct stv6110x_state *stv6110x = fe->tuner_priv; 260 261 stv6110x_read_reg(stv6110x, STV6110x_CTRL2, &stv6110x->regs[STV6110x_CTRL2]); 262 *gain = 2 * STV6110x_GETFIELD(CTRL2_BBGAIN, stv6110x->regs[STV6110x_CTRL2]); 263 264 return 0; 265 } 266 267 static int stv6110x_set_bbgain(struct dvb_frontend *fe, u32 gain) 268 { 269 struct stv6110x_state *stv6110x = fe->tuner_priv; 270 271 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_BBGAIN, gain / 2); 272 stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x->regs[STV6110x_CTRL2]); 273 274 return 0; 275 } 276 277 static int stv6110x_set_mode(struct dvb_frontend *fe, enum tuner_mode mode) 278 { 279 struct stv6110x_state *stv6110x = fe->tuner_priv; 280 int ret; 281 282 switch (mode) { 283 case TUNER_SLEEP: 284 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_SYN, 0); 285 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_RX, 0); 286 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_LPT, 0); 287 break; 288 289 case TUNER_WAKE: 290 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_SYN, 1); 291 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_RX, 1); 292 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_LPT, 1); 293 break; 294 } 295 296 ret = stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x->regs[STV6110x_CTRL1]); 297 if (ret < 0) { 298 dprintk(FE_ERROR, 1, "I/O Error"); 299 return -EIO; 300 } 301 302 return 0; 303 } 304 305 static int stv6110x_sleep(struct dvb_frontend *fe) 306 { 307 if (fe->tuner_priv) 308 return stv6110x_set_mode(fe, TUNER_SLEEP); 309 310 return 0; 311 } 312 313 static int stv6110x_get_status(struct dvb_frontend *fe, u32 *status) 314 { 315 struct stv6110x_state *stv6110x = fe->tuner_priv; 316 317 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]); 318 319 if (STV6110x_GETFIELD(STAT1_LOCK, stv6110x->regs[STV6110x_STAT1])) 320 *status = TUNER_PHASELOCKED; 321 else 322 *status = 0; 323 324 return 0; 325 } 326 327 328 static void stv6110x_release(struct dvb_frontend *fe) 329 { 330 struct stv6110x_state *stv6110x = fe->tuner_priv; 331 332 fe->tuner_priv = NULL; 333 kfree(stv6110x); 334 } 335 336 static void st6110x_init_regs(struct stv6110x_state *stv6110x) 337 { 338 u8 default_regs[] = {0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e}; 339 340 memcpy(stv6110x->regs, default_regs, 8); 341 } 342 343 static void stv6110x_setup_divider(struct stv6110x_state *stv6110x) 344 { 345 switch (stv6110x->config->clk_div) { 346 default: 347 case 1: 348 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], 349 CTRL2_CO_DIV, 350 0); 351 break; 352 case 2: 353 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], 354 CTRL2_CO_DIV, 355 1); 356 break; 357 case 4: 358 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], 359 CTRL2_CO_DIV, 360 2); 361 break; 362 case 8: 363 case 0: 364 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], 365 CTRL2_CO_DIV, 366 3); 367 break; 368 } 369 } 370 371 static const struct dvb_tuner_ops stv6110x_ops = { 372 .info = { 373 .name = "STV6110(A) Silicon Tuner", 374 .frequency_min_hz = 950 * MHz, 375 .frequency_max_hz = 2150 * MHz, 376 }, 377 .release = stv6110x_release 378 }; 379 380 static struct stv6110x_devctl stv6110x_ctl = { 381 .tuner_init = stv6110x_init, 382 .tuner_sleep = stv6110x_sleep, 383 .tuner_set_mode = stv6110x_set_mode, 384 .tuner_set_frequency = stv6110x_set_frequency, 385 .tuner_get_frequency = stv6110x_get_frequency, 386 .tuner_set_bandwidth = stv6110x_set_bandwidth, 387 .tuner_get_bandwidth = stv6110x_get_bandwidth, 388 .tuner_set_bbgain = stv6110x_set_bbgain, 389 .tuner_get_bbgain = stv6110x_get_bbgain, 390 .tuner_set_refclk = stv6110x_set_refclock, 391 .tuner_get_status = stv6110x_get_status, 392 }; 393 394 static void stv6110x_set_frontend_opts(struct stv6110x_state *stv6110x) 395 { 396 stv6110x->frontend->tuner_priv = stv6110x; 397 stv6110x->frontend->ops.tuner_ops = stv6110x_ops; 398 } 399 400 static struct stv6110x_devctl *stv6110x_get_devctl(struct i2c_client *client) 401 { 402 struct stv6110x_state *stv6110x = i2c_get_clientdata(client); 403 404 dev_dbg(&client->dev, "\n"); 405 406 return stv6110x->devctl; 407 } 408 409 static int stv6110x_probe(struct i2c_client *client) 410 { 411 struct stv6110x_config *config = client->dev.platform_data; 412 413 struct stv6110x_state *stv6110x; 414 415 stv6110x = kzalloc(sizeof(*stv6110x), GFP_KERNEL); 416 if (!stv6110x) 417 return -ENOMEM; 418 419 stv6110x->frontend = config->frontend; 420 stv6110x->i2c = client->adapter; 421 stv6110x->config = config; 422 stv6110x->devctl = &stv6110x_ctl; 423 424 st6110x_init_regs(stv6110x); 425 stv6110x_setup_divider(stv6110x); 426 stv6110x_set_frontend_opts(stv6110x); 427 428 dev_info(&stv6110x->i2c->dev, "Probed STV6110x\n"); 429 430 i2c_set_clientdata(client, stv6110x); 431 432 /* setup callbacks */ 433 config->get_devctl = stv6110x_get_devctl; 434 435 return 0; 436 } 437 438 static void stv6110x_remove(struct i2c_client *client) 439 { 440 struct stv6110x_state *stv6110x = i2c_get_clientdata(client); 441 442 stv6110x_release(stv6110x->frontend); 443 } 444 445 const struct stv6110x_devctl *stv6110x_attach(struct dvb_frontend *fe, 446 const struct stv6110x_config *config, 447 struct i2c_adapter *i2c) 448 { 449 struct stv6110x_state *stv6110x; 450 451 stv6110x = kzalloc(sizeof(*stv6110x), GFP_KERNEL); 452 if (!stv6110x) 453 return NULL; 454 455 stv6110x->frontend = fe; 456 stv6110x->i2c = i2c; 457 stv6110x->config = config; 458 stv6110x->devctl = &stv6110x_ctl; 459 460 st6110x_init_regs(stv6110x); 461 stv6110x_setup_divider(stv6110x); 462 stv6110x_set_frontend_opts(stv6110x); 463 464 fe->tuner_priv = stv6110x; 465 fe->ops.tuner_ops = stv6110x_ops; 466 467 dev_info(&stv6110x->i2c->dev, "Attaching STV6110x\n"); 468 return stv6110x->devctl; 469 } 470 EXPORT_SYMBOL(stv6110x_attach); 471 472 static const struct i2c_device_id stv6110x_id_table[] = { 473 {"stv6110x", 0}, 474 {} 475 }; 476 MODULE_DEVICE_TABLE(i2c, stv6110x_id_table); 477 478 static struct i2c_driver stv6110x_driver = { 479 .driver = { 480 .name = "stv6110x", 481 .suppress_bind_attrs = true, 482 }, 483 .probe_new = stv6110x_probe, 484 .remove = stv6110x_remove, 485 .id_table = stv6110x_id_table, 486 }; 487 488 module_i2c_driver(stv6110x_driver); 489 490 MODULE_AUTHOR("Manu Abraham"); 491 MODULE_DESCRIPTION("STV6110x Silicon tuner"); 492 MODULE_LICENSE("GPL"); 493