stb6100.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. STB6100 Silicon Tuner
  4. Copyright (C) Manu Abraham (abraham.manu@gmail.com)
  5. Copyright (C) ST Microelectronics
  6. */
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/string.h>
  12. #include <media/dvb_frontend.h>
  13. #include "stb6100.h"
  14. static unsigned int verbose;
  15. module_param(verbose, int, 0644);
  16. /* Max transfer size done by I2C transfer functions */
  17. #define MAX_XFER_SIZE 64
  18. #define FE_ERROR 0
  19. #define FE_NOTICE 1
  20. #define FE_INFO 2
  21. #define FE_DEBUG 3
  22. #define dprintk(x, y, z, format, arg...) do { \
  23. if (z) { \
  24. if ((x > FE_ERROR) && (x > y)) \
  25. printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \
  26. else if ((x > FE_NOTICE) && (x > y)) \
  27. printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \
  28. else if ((x > FE_INFO) && (x > y)) \
  29. printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \
  30. else if ((x > FE_DEBUG) && (x > y)) \
  31. printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \
  32. } else { \
  33. if (x > y) \
  34. printk(format, ##arg); \
  35. } \
  36. } while (0)
  37. struct stb6100_lkup {
  38. u32 val_low;
  39. u32 val_high;
  40. u8 reg;
  41. };
  42. static void stb6100_release(struct dvb_frontend *fe);
  43. static const struct stb6100_lkup lkup[] = {
  44. { 0, 950000, 0x0a },
  45. { 950000, 1000000, 0x0a },
  46. { 1000000, 1075000, 0x0c },
  47. { 1075000, 1200000, 0x00 },
  48. { 1200000, 1300000, 0x01 },
  49. { 1300000, 1370000, 0x02 },
  50. { 1370000, 1470000, 0x04 },
  51. { 1470000, 1530000, 0x05 },
  52. { 1530000, 1650000, 0x06 },
  53. { 1650000, 1800000, 0x08 },
  54. { 1800000, 1950000, 0x0a },
  55. { 1950000, 2150000, 0x0c },
  56. { 2150000, 9999999, 0x0c },
  57. { 0, 0, 0x00 }
  58. };
  59. /* Register names for easy debugging. */
  60. static const char *stb6100_regnames[] = {
  61. [STB6100_LD] = "LD",
  62. [STB6100_VCO] = "VCO",
  63. [STB6100_NI] = "NI",
  64. [STB6100_NF_LSB] = "NF",
  65. [STB6100_K] = "K",
  66. [STB6100_G] = "G",
  67. [STB6100_F] = "F",
  68. [STB6100_DLB] = "DLB",
  69. [STB6100_TEST1] = "TEST1",
  70. [STB6100_FCCK] = "FCCK",
  71. [STB6100_LPEN] = "LPEN",
  72. [STB6100_TEST3] = "TEST3",
  73. };
  74. /* Template for normalisation, i.e. setting unused or undocumented
  75. * bits as required according to the documentation.
  76. */
  77. struct stb6100_regmask {
  78. u8 mask;
  79. u8 set;
  80. };
  81. static const struct stb6100_regmask stb6100_template[] = {
  82. [STB6100_LD] = { 0xff, 0x00 },
  83. [STB6100_VCO] = { 0xff, 0x00 },
  84. [STB6100_NI] = { 0xff, 0x00 },
  85. [STB6100_NF_LSB] = { 0xff, 0x00 },
  86. [STB6100_K] = { 0xc7, 0x38 },
  87. [STB6100_G] = { 0xef, 0x10 },
  88. [STB6100_F] = { 0x1f, 0xc0 },
  89. [STB6100_DLB] = { 0x38, 0xc4 },
  90. [STB6100_TEST1] = { 0x00, 0x8f },
  91. [STB6100_FCCK] = { 0x40, 0x0d },
  92. [STB6100_LPEN] = { 0xf0, 0x0b },
  93. [STB6100_TEST3] = { 0x00, 0xde },
  94. };
  95. /*
  96. * Currently unused. Some boards might need it in the future
  97. */
  98. static inline void stb6100_normalise_regs(u8 regs[])
  99. {
  100. int i;
  101. for (i = 0; i < STB6100_NUMREGS; i++)
  102. regs[i] = (regs[i] & stb6100_template[i].mask) | stb6100_template[i].set;
  103. }
  104. static int stb6100_read_regs(struct stb6100_state *state, u8 regs[])
  105. {
  106. int rc;
  107. struct i2c_msg msg = {
  108. .addr = state->config->tuner_address,
  109. .flags = I2C_M_RD,
  110. .buf = regs,
  111. .len = STB6100_NUMREGS
  112. };
  113. rc = i2c_transfer(state->i2c, &msg, 1);
  114. if (unlikely(rc != 1)) {
  115. dprintk(verbose, FE_ERROR, 1, "Read (0x%x) err, rc=[%d]",
  116. state->config->tuner_address, rc);
  117. return -EREMOTEIO;
  118. }
  119. if (unlikely(verbose > FE_DEBUG)) {
  120. int i;
  121. dprintk(verbose, FE_DEBUG, 1, " Read from 0x%02x", state->config->tuner_address);
  122. for (i = 0; i < STB6100_NUMREGS; i++)
  123. dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[i], regs[i]);
  124. }
  125. return 0;
  126. }
  127. static int stb6100_read_reg(struct stb6100_state *state, u8 reg)
  128. {
  129. u8 regs[STB6100_NUMREGS];
  130. struct i2c_msg msg = {
  131. .addr = state->config->tuner_address + reg,
  132. .flags = I2C_M_RD,
  133. .buf = regs,
  134. .len = 1
  135. };
  136. i2c_transfer(state->i2c, &msg, 1);
  137. if (unlikely(reg >= STB6100_NUMREGS)) {
  138. dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
  139. return -EINVAL;
  140. }
  141. if (unlikely(verbose > FE_DEBUG)) {
  142. dprintk(verbose, FE_DEBUG, 1, " Read from 0x%02x", state->config->tuner_address);
  143. dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[reg], regs[0]);
  144. }
  145. return (unsigned int)regs[0];
  146. }
  147. static int stb6100_write_reg_range(struct stb6100_state *state, u8 buf[], int start, int len)
  148. {
  149. int rc;
  150. u8 cmdbuf[MAX_XFER_SIZE];
  151. struct i2c_msg msg = {
  152. .addr = state->config->tuner_address,
  153. .flags = 0,
  154. .buf = cmdbuf,
  155. .len = len + 1
  156. };
  157. if (1 + len > sizeof(cmdbuf)) {
  158. printk(KERN_WARNING
  159. "%s: i2c wr: len=%d is too big!\n",
  160. KBUILD_MODNAME, len);
  161. return -EINVAL;
  162. }
  163. if (unlikely(start < 1 || start + len > STB6100_NUMREGS)) {
  164. dprintk(verbose, FE_ERROR, 1, "Invalid register range %d:%d",
  165. start, len);
  166. return -EINVAL;
  167. }
  168. memcpy(&cmdbuf[1], buf, len);
  169. cmdbuf[0] = start;
  170. if (unlikely(verbose > FE_DEBUG)) {
  171. int i;
  172. dprintk(verbose, FE_DEBUG, 1, " Write @ 0x%02x: [%d:%d]", state->config->tuner_address, start, len);
  173. for (i = 0; i < len; i++)
  174. dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[start + i], buf[i]);
  175. }
  176. rc = i2c_transfer(state->i2c, &msg, 1);
  177. if (unlikely(rc != 1)) {
  178. dprintk(verbose, FE_ERROR, 1, "(0x%x) write err [%d:%d], rc=[%d]",
  179. (unsigned int)state->config->tuner_address, start, len, rc);
  180. return -EREMOTEIO;
  181. }
  182. return 0;
  183. }
  184. static int stb6100_write_reg(struct stb6100_state *state, u8 reg, u8 data)
  185. {
  186. u8 tmp = data; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
  187. if (unlikely(reg >= STB6100_NUMREGS)) {
  188. dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
  189. return -EREMOTEIO;
  190. }
  191. tmp = (tmp & stb6100_template[reg].mask) | stb6100_template[reg].set;
  192. return stb6100_write_reg_range(state, &tmp, reg, 1);
  193. }
  194. static int stb6100_get_status(struct dvb_frontend *fe, u32 *status)
  195. {
  196. int rc;
  197. struct stb6100_state *state = fe->tuner_priv;
  198. rc = stb6100_read_reg(state, STB6100_LD);
  199. if (rc < 0) {
  200. dprintk(verbose, FE_ERROR, 1, "%s failed", __func__);
  201. return rc;
  202. }
  203. return (rc & STB6100_LD_LOCK) ? TUNER_STATUS_LOCKED : 0;
  204. }
  205. static int stb6100_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  206. {
  207. int rc;
  208. u8 f;
  209. u32 bw;
  210. struct stb6100_state *state = fe->tuner_priv;
  211. rc = stb6100_read_reg(state, STB6100_F);
  212. if (rc < 0)
  213. return rc;
  214. f = rc & STB6100_F_F;
  215. bw = (f + 5) * 2000; /* x2 for ZIF */
  216. *bandwidth = state->bandwidth = bw * 1000;
  217. dprintk(verbose, FE_DEBUG, 1, "bandwidth = %u Hz", state->bandwidth);
  218. return 0;
  219. }
  220. static int stb6100_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
  221. {
  222. u32 tmp;
  223. int rc;
  224. struct stb6100_state *state = fe->tuner_priv;
  225. dprintk(verbose, FE_DEBUG, 1, "set bandwidth to %u Hz", bandwidth);
  226. bandwidth /= 2; /* ZIF */
  227. if (bandwidth >= 36000000) /* F[4:0] BW/2 max =31+5=36 mhz for F=31 */
  228. tmp = 31;
  229. else if (bandwidth <= 5000000) /* bw/2 min = 5Mhz for F=0 */
  230. tmp = 0;
  231. else /* if 5 < bw/2 < 36 */
  232. tmp = (bandwidth + 500000) / 1000000 - 5;
  233. /* Turn on LPF bandwidth setting clock control,
  234. * set bandwidth, wait 10ms, turn off.
  235. */
  236. rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d | STB6100_FCCK_FCCK);
  237. if (rc < 0)
  238. return rc;
  239. rc = stb6100_write_reg(state, STB6100_F, 0xc0 | tmp);
  240. if (rc < 0)
  241. return rc;
  242. msleep(5); /* This is dangerous as another (related) thread may start */
  243. rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d);
  244. if (rc < 0)
  245. return rc;
  246. msleep(10); /* This is dangerous as another (related) thread may start */
  247. return 0;
  248. }
  249. static int stb6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  250. {
  251. int rc;
  252. u32 nint, nfrac, fvco;
  253. int psd2, odiv;
  254. struct stb6100_state *state = fe->tuner_priv;
  255. u8 regs[STB6100_NUMREGS];
  256. rc = stb6100_read_regs(state, regs);
  257. if (rc < 0)
  258. return rc;
  259. odiv = (regs[STB6100_VCO] & STB6100_VCO_ODIV) >> STB6100_VCO_ODIV_SHIFT;
  260. psd2 = (regs[STB6100_K] & STB6100_K_PSD2) >> STB6100_K_PSD2_SHIFT;
  261. nint = regs[STB6100_NI];
  262. nfrac = ((regs[STB6100_K] & STB6100_K_NF_MSB) << 8) | regs[STB6100_NF_LSB];
  263. fvco = (nfrac * state->reference >> (9 - psd2)) + (nint * state->reference << psd2);
  264. *frequency = state->frequency = fvco >> (odiv + 1);
  265. dprintk(verbose, FE_DEBUG, 1,
  266. "frequency = %u kHz, odiv = %u, psd2 = %u, fxtal = %u kHz, fvco = %u kHz, N(I) = %u, N(F) = %u",
  267. state->frequency, odiv, psd2, state->reference, fvco, nint, nfrac);
  268. return 0;
  269. }
  270. static int stb6100_set_frequency(struct dvb_frontend *fe, u32 frequency)
  271. {
  272. int rc;
  273. const struct stb6100_lkup *ptr;
  274. struct stb6100_state *state = fe->tuner_priv;
  275. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  276. u32 srate = 0, fvco, nint, nfrac;
  277. u8 regs[STB6100_NUMREGS];
  278. u8 g, psd2, odiv;
  279. dprintk(verbose, FE_DEBUG, 1, "Version 2010-8-14 13:51");
  280. if (fe->ops.get_frontend) {
  281. dprintk(verbose, FE_DEBUG, 1, "Get frontend parameters");
  282. fe->ops.get_frontend(fe, p);
  283. }
  284. srate = p->symbol_rate;
  285. /* Set up tuner cleanly, LPF calibration on */
  286. rc = stb6100_write_reg(state, STB6100_FCCK, 0x4d | STB6100_FCCK_FCCK);
  287. if (rc < 0)
  288. return rc; /* allow LPF calibration */
  289. /* PLL Loop disabled, bias on, VCO on, synth on */
  290. regs[STB6100_LPEN] = 0xeb;
  291. rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]);
  292. if (rc < 0)
  293. return rc;
  294. /* Program the registers with their data values */
  295. /* VCO divide ratio (LO divide ratio, VCO prescaler enable). */
  296. if (frequency <= 1075000)
  297. odiv = 1;
  298. else
  299. odiv = 0;
  300. /* VCO enabled, search clock off as per LL3.7, 3.4.1 */
  301. regs[STB6100_VCO] = 0xe0 | (odiv << STB6100_VCO_ODIV_SHIFT);
  302. /* OSM */
  303. for (ptr = lkup;
  304. (ptr->val_high != 0) && !CHKRANGE(frequency, ptr->val_low, ptr->val_high);
  305. ptr++);
  306. if (ptr->val_high == 0) {
  307. printk(KERN_ERR "%s: frequency out of range: %u kHz\n", __func__, frequency);
  308. return -EINVAL;
  309. }
  310. regs[STB6100_VCO] = (regs[STB6100_VCO] & ~STB6100_VCO_OSM) | ptr->reg;
  311. rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
  312. if (rc < 0)
  313. return rc;
  314. if ((frequency > 1075000) && (frequency <= 1325000))
  315. psd2 = 0;
  316. else
  317. psd2 = 1;
  318. /* F(VCO) = F(LO) * (ODIV == 0 ? 2 : 4) */
  319. fvco = frequency << (1 + odiv);
  320. /* N(I) = floor(f(VCO) / (f(XTAL) * (PSD2 ? 2 : 1))) */
  321. nint = fvco / (state->reference << psd2);
  322. /* N(F) = round(f(VCO) / f(XTAL) * (PSD2 ? 2 : 1) - N(I)) * 2 ^ 9 */
  323. nfrac = DIV_ROUND_CLOSEST((fvco - (nint * state->reference << psd2))
  324. << (9 - psd2), state->reference);
  325. /* NI */
  326. regs[STB6100_NI] = nint;
  327. rc = stb6100_write_reg(state, STB6100_NI, regs[STB6100_NI]);
  328. if (rc < 0)
  329. return rc;
  330. /* NF */
  331. regs[STB6100_NF_LSB] = nfrac;
  332. rc = stb6100_write_reg(state, STB6100_NF_LSB, regs[STB6100_NF_LSB]);
  333. if (rc < 0)
  334. return rc;
  335. /* K */
  336. regs[STB6100_K] = (0x38 & ~STB6100_K_PSD2) | (psd2 << STB6100_K_PSD2_SHIFT);
  337. regs[STB6100_K] = (regs[STB6100_K] & ~STB6100_K_NF_MSB) | ((nfrac >> 8) & STB6100_K_NF_MSB);
  338. rc = stb6100_write_reg(state, STB6100_K, regs[STB6100_K]);
  339. if (rc < 0)
  340. return rc;
  341. /* G Baseband gain. */
  342. if (srate >= 15000000)
  343. g = 9; /* +4 dB */
  344. else if (srate >= 5000000)
  345. g = 11; /* +8 dB */
  346. else
  347. g = 14; /* +14 dB */
  348. regs[STB6100_G] = (0x10 & ~STB6100_G_G) | g;
  349. regs[STB6100_G] &= ~STB6100_G_GCT; /* mask GCT */
  350. regs[STB6100_G] |= (1 << 5); /* 2Vp-p Mode */
  351. rc = stb6100_write_reg(state, STB6100_G, regs[STB6100_G]);
  352. if (rc < 0)
  353. return rc;
  354. /* F we don't write as it is set up in BW set */
  355. /* DLB set DC servo loop BW to 160Hz (LLA 3.8 / 2.1) */
  356. regs[STB6100_DLB] = 0xcc;
  357. rc = stb6100_write_reg(state, STB6100_DLB, regs[STB6100_DLB]);
  358. if (rc < 0)
  359. return rc;
  360. dprintk(verbose, FE_DEBUG, 1,
  361. "frequency = %u, srate = %u, g = %u, odiv = %u, psd2 = %u, fxtal = %u, osm = %u, fvco = %u, N(I) = %u, N(F) = %u",
  362. frequency, srate, (unsigned int)g, (unsigned int)odiv,
  363. (unsigned int)psd2, state->reference,
  364. ptr->reg, fvco, nint, nfrac);
  365. /* Set up the test registers */
  366. regs[STB6100_TEST1] = 0x8f;
  367. rc = stb6100_write_reg(state, STB6100_TEST1, regs[STB6100_TEST1]);
  368. if (rc < 0)
  369. return rc;
  370. regs[STB6100_TEST3] = 0xde;
  371. rc = stb6100_write_reg(state, STB6100_TEST3, regs[STB6100_TEST3]);
  372. if (rc < 0)
  373. return rc;
  374. /* Bring up tuner according to LLA 3.7 3.4.1, step 2 */
  375. regs[STB6100_LPEN] = 0xfb; /* PLL Loop enabled, bias on, VCO on, synth on */
  376. rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]);
  377. if (rc < 0)
  378. return rc;
  379. msleep(2);
  380. /* Bring up tuner according to LLA 3.7 3.4.1, step 3 */
  381. regs[STB6100_VCO] &= ~STB6100_VCO_OCK; /* VCO fast search */
  382. rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
  383. if (rc < 0)
  384. return rc;
  385. msleep(10); /* This is dangerous as another (related) thread may start */ /* wait for LO to lock */
  386. regs[STB6100_VCO] &= ~STB6100_VCO_OSCH; /* vco search disabled */
  387. regs[STB6100_VCO] |= STB6100_VCO_OCK; /* search clock off */
  388. rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
  389. if (rc < 0)
  390. return rc;
  391. rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d);
  392. if (rc < 0)
  393. return rc; /* Stop LPF calibration */
  394. msleep(10); /* This is dangerous as another (related) thread may start */
  395. /* wait for stabilisation, (should not be necessary) */
  396. return 0;
  397. }
  398. static int stb6100_sleep(struct dvb_frontend *fe)
  399. {
  400. /* TODO: power down */
  401. return 0;
  402. }
  403. static int stb6100_init(struct dvb_frontend *fe)
  404. {
  405. struct stb6100_state *state = fe->tuner_priv;
  406. int refclk = 27000000; /* Hz */
  407. /*
  408. * iqsense = 1
  409. * tunerstep = 125000
  410. */
  411. state->bandwidth = 36000000; /* Hz */
  412. state->reference = refclk / 1000; /* kHz */
  413. /* Set default bandwidth. Modified, PN 13-May-10 */
  414. return 0;
  415. }
  416. static int stb6100_set_params(struct dvb_frontend *fe)
  417. {
  418. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  419. if (c->frequency > 0)
  420. stb6100_set_frequency(fe, c->frequency);
  421. if (c->bandwidth_hz > 0)
  422. stb6100_set_bandwidth(fe, c->bandwidth_hz);
  423. return 0;
  424. }
  425. static const struct dvb_tuner_ops stb6100_ops = {
  426. .info = {
  427. .name = "STB6100 Silicon Tuner",
  428. .frequency_min_hz = 950 * MHz,
  429. .frequency_max_hz = 2150 * MHz,
  430. },
  431. .init = stb6100_init,
  432. .sleep = stb6100_sleep,
  433. .get_status = stb6100_get_status,
  434. .set_params = stb6100_set_params,
  435. .get_frequency = stb6100_get_frequency,
  436. .get_bandwidth = stb6100_get_bandwidth,
  437. .release = stb6100_release
  438. };
  439. struct dvb_frontend *stb6100_attach(struct dvb_frontend *fe,
  440. const struct stb6100_config *config,
  441. struct i2c_adapter *i2c)
  442. {
  443. struct stb6100_state *state = NULL;
  444. state = kzalloc(sizeof (struct stb6100_state), GFP_KERNEL);
  445. if (!state)
  446. return NULL;
  447. state->config = config;
  448. state->i2c = i2c;
  449. state->frontend = fe;
  450. state->reference = config->refclock / 1000; /* kHz */
  451. fe->tuner_priv = state;
  452. fe->ops.tuner_ops = stb6100_ops;
  453. printk("%s: Attaching STB6100 \n", __func__);
  454. return fe;
  455. }
  456. static void stb6100_release(struct dvb_frontend *fe)
  457. {
  458. struct stb6100_state *state = fe->tuner_priv;
  459. fe->tuner_priv = NULL;
  460. kfree(state);
  461. }
  462. EXPORT_SYMBOL(stb6100_attach);
  463. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  464. MODULE_AUTHOR("Manu Abraham");
  465. MODULE_DESCRIPTION("STB6100 Silicon tuner");
  466. MODULE_LICENSE("GPL");