stb6100.c 15 KB

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