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 const struct dvb_tuner_ops stv6110x_ops = { 337 .info = { 338 .name = "STV6110(A) Silicon Tuner", 339 .frequency_min_hz = 950 * MHz, 340 .frequency_max_hz = 2150 * MHz, 341 }, 342 .release = stv6110x_release 343 }; 344 345 static const struct stv6110x_devctl stv6110x_ctl = { 346 .tuner_init = stv6110x_init, 347 .tuner_sleep = stv6110x_sleep, 348 .tuner_set_mode = stv6110x_set_mode, 349 .tuner_set_frequency = stv6110x_set_frequency, 350 .tuner_get_frequency = stv6110x_get_frequency, 351 .tuner_set_bandwidth = stv6110x_set_bandwidth, 352 .tuner_get_bandwidth = stv6110x_get_bandwidth, 353 .tuner_set_bbgain = stv6110x_set_bbgain, 354 .tuner_get_bbgain = stv6110x_get_bbgain, 355 .tuner_set_refclk = stv6110x_set_refclock, 356 .tuner_get_status = stv6110x_get_status, 357 }; 358 359 const struct stv6110x_devctl *stv6110x_attach(struct dvb_frontend *fe, 360 const struct stv6110x_config *config, 361 struct i2c_adapter *i2c) 362 { 363 struct stv6110x_state *stv6110x; 364 u8 default_regs[] = {0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e}; 365 366 stv6110x = kzalloc(sizeof (struct stv6110x_state), GFP_KERNEL); 367 if (!stv6110x) 368 return NULL; 369 370 stv6110x->i2c = i2c; 371 stv6110x->config = config; 372 stv6110x->devctl = &stv6110x_ctl; 373 memcpy(stv6110x->regs, default_regs, 8); 374 375 /* setup divider */ 376 switch (stv6110x->config->clk_div) { 377 default: 378 case 1: 379 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 0); 380 break; 381 case 2: 382 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 1); 383 break; 384 case 4: 385 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 2); 386 break; 387 case 8: 388 case 0: 389 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 3); 390 break; 391 } 392 393 fe->tuner_priv = stv6110x; 394 fe->ops.tuner_ops = stv6110x_ops; 395 396 printk(KERN_INFO "%s: Attaching STV6110x\n", __func__); 397 return stv6110x->devctl; 398 } 399 EXPORT_SYMBOL(stv6110x_attach); 400 401 MODULE_AUTHOR("Manu Abraham"); 402 MODULE_DESCRIPTION("STV6110x Silicon tuner"); 403 MODULE_LICENSE("GPL"); 404