l64781.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. driver for LSI L64781 COFDM demodulator
  3. Copyright (C) 2001 Holger Waechtler for Convergence Integrated Media GmbH
  4. Marko Kohtala <marko.kohtala@luukku.com>
  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 <linux/slab.h>
  22. #include "dvb_frontend.h"
  23. #include "l64781.h"
  24. struct l64781_state {
  25. struct i2c_adapter* i2c;
  26. const struct l64781_config* config;
  27. struct dvb_frontend frontend;
  28. /* private demodulator data */
  29. unsigned int first:1;
  30. };
  31. #define dprintk(args...) \
  32. do { \
  33. if (debug) printk(KERN_DEBUG "l64781: " args); \
  34. } while (0)
  35. static int debug;
  36. module_param(debug, int, 0644);
  37. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  38. static int l64781_writereg (struct l64781_state* state, u8 reg, u8 data)
  39. {
  40. int ret;
  41. u8 buf [] = { reg, data };
  42. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
  43. if ((ret = i2c_transfer(state->i2c, &msg, 1)) != 1)
  44. dprintk ("%s: write_reg error (reg == %02x) = %02x!\n",
  45. __func__, reg, ret);
  46. return (ret != 1) ? -1 : 0;
  47. }
  48. static int l64781_readreg (struct l64781_state* state, u8 reg)
  49. {
  50. int ret;
  51. u8 b0 [] = { reg };
  52. u8 b1 [] = { 0 };
  53. struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
  54. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
  55. ret = i2c_transfer(state->i2c, msg, 2);
  56. if (ret != 2) return ret;
  57. return b1[0];
  58. }
  59. static void apply_tps (struct l64781_state* state)
  60. {
  61. l64781_writereg (state, 0x2a, 0x00);
  62. l64781_writereg (state, 0x2a, 0x01);
  63. /* This here is a little bit questionable because it enables
  64. the automatic update of TPS registers. I think we'd need to
  65. handle the IRQ from FE to update some other registers as
  66. well, or at least implement some magic to tuning to correct
  67. to the TPS received from transmission. */
  68. l64781_writereg (state, 0x2a, 0x02);
  69. }
  70. static void reset_afc (struct l64781_state* state)
  71. {
  72. /* Set AFC stall for the AFC_INIT_FRQ setting, TIM_STALL for
  73. timing offset */
  74. l64781_writereg (state, 0x07, 0x9e); /* stall AFC */
  75. l64781_writereg (state, 0x08, 0); /* AFC INIT FREQ */
  76. l64781_writereg (state, 0x09, 0);
  77. l64781_writereg (state, 0x0a, 0);
  78. l64781_writereg (state, 0x07, 0x8e);
  79. l64781_writereg (state, 0x0e, 0); /* AGC gain to zero in beginning */
  80. l64781_writereg (state, 0x11, 0x80); /* stall TIM */
  81. l64781_writereg (state, 0x10, 0); /* TIM_OFFSET_LSB */
  82. l64781_writereg (state, 0x12, 0);
  83. l64781_writereg (state, 0x13, 0);
  84. l64781_writereg (state, 0x11, 0x00);
  85. }
  86. static int reset_and_configure (struct l64781_state* state)
  87. {
  88. u8 buf [] = { 0x06 };
  89. struct i2c_msg msg = { .addr = 0x00, .flags = 0, .buf = buf, .len = 1 };
  90. // NOTE: this is correct in writing to address 0x00
  91. return (i2c_transfer(state->i2c, &msg, 1) == 1) ? 0 : -ENODEV;
  92. }
  93. static int apply_frontend_param (struct dvb_frontend* fe, struct dvb_frontend_parameters *param)
  94. {
  95. struct l64781_state* state = fe->demodulator_priv;
  96. /* The coderates for FEC_NONE, FEC_4_5 and FEC_FEC_6_7 are arbitrary */
  97. static const u8 fec_tab[] = { 7, 0, 1, 2, 9, 3, 10, 4 };
  98. /* QPSK, QAM_16, QAM_64 */
  99. static const u8 qam_tab [] = { 2, 4, 0, 6 };
  100. static const u8 bw_tab [] = { 8, 7, 6 }; /* 8Mhz, 7MHz, 6MHz */
  101. static const u8 guard_tab [] = { 1, 2, 4, 8 };
  102. /* The Grundig 29504-401.04 Tuner comes with 18.432MHz crystal. */
  103. static const u32 ppm = 8000;
  104. struct dvb_ofdm_parameters *p = &param->u.ofdm;
  105. u32 ddfs_offset_fixed;
  106. /* u32 ddfs_offset_variable = 0x6000-((1000000UL+ppm)/ */
  107. /* bw_tab[p->bandWidth]<<10)/15625; */
  108. u32 init_freq;
  109. u32 spi_bias;
  110. u8 val0x04;
  111. u8 val0x05;
  112. u8 val0x06;
  113. int bw = p->bandwidth - BANDWIDTH_8_MHZ;
  114. if (fe->ops.tuner_ops.set_params) {
  115. fe->ops.tuner_ops.set_params(fe, param);
  116. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  117. }
  118. if (param->inversion != INVERSION_ON &&
  119. param->inversion != INVERSION_OFF)
  120. return -EINVAL;
  121. if (bw < 0 || bw > 2)
  122. return -EINVAL;
  123. if (p->code_rate_HP != FEC_1_2 && p->code_rate_HP != FEC_2_3 &&
  124. p->code_rate_HP != FEC_3_4 && p->code_rate_HP != FEC_5_6 &&
  125. p->code_rate_HP != FEC_7_8)
  126. return -EINVAL;
  127. if (p->hierarchy_information != HIERARCHY_NONE &&
  128. (p->code_rate_LP != FEC_1_2 && p->code_rate_LP != FEC_2_3 &&
  129. p->code_rate_LP != FEC_3_4 && p->code_rate_LP != FEC_5_6 &&
  130. p->code_rate_LP != FEC_7_8))
  131. return -EINVAL;
  132. if (p->constellation != QPSK && p->constellation != QAM_16 &&
  133. p->constellation != QAM_64)
  134. return -EINVAL;
  135. if (p->transmission_mode != TRANSMISSION_MODE_2K &&
  136. p->transmission_mode != TRANSMISSION_MODE_8K)
  137. return -EINVAL;
  138. if (p->guard_interval < GUARD_INTERVAL_1_32 ||
  139. p->guard_interval > GUARD_INTERVAL_1_4)
  140. return -EINVAL;
  141. if (p->hierarchy_information < HIERARCHY_NONE ||
  142. p->hierarchy_information > HIERARCHY_4)
  143. return -EINVAL;
  144. ddfs_offset_fixed = 0x4000-(ppm<<16)/bw_tab[p->bandwidth]/1000000;
  145. /* This works up to 20000 ppm, it overflows if too large ppm! */
  146. init_freq = (((8UL<<25) + (8UL<<19) / 25*ppm / (15625/25)) /
  147. bw_tab[p->bandwidth] & 0xFFFFFF);
  148. /* SPI bias calculation is slightly modified to fit in 32bit */
  149. /* will work for high ppm only... */
  150. spi_bias = 378 * (1 << 10);
  151. spi_bias *= 16;
  152. spi_bias *= bw_tab[p->bandwidth];
  153. spi_bias *= qam_tab[p->constellation];
  154. spi_bias /= p->code_rate_HP + 1;
  155. spi_bias /= (guard_tab[p->guard_interval] + 32);
  156. spi_bias *= 1000;
  157. spi_bias /= 1000 + ppm/1000;
  158. spi_bias *= p->code_rate_HP;
  159. val0x04 = (p->transmission_mode << 2) | p->guard_interval;
  160. val0x05 = fec_tab[p->code_rate_HP];
  161. if (p->hierarchy_information != HIERARCHY_NONE)
  162. val0x05 |= (p->code_rate_LP - FEC_1_2) << 3;
  163. val0x06 = (p->hierarchy_information << 2) | p->constellation;
  164. l64781_writereg (state, 0x04, val0x04);
  165. l64781_writereg (state, 0x05, val0x05);
  166. l64781_writereg (state, 0x06, val0x06);
  167. reset_afc (state);
  168. /* Technical manual section 2.6.1, TIM_IIR_GAIN optimal values */
  169. l64781_writereg (state, 0x15,
  170. p->transmission_mode == TRANSMISSION_MODE_2K ? 1 : 3);
  171. l64781_writereg (state, 0x16, init_freq & 0xff);
  172. l64781_writereg (state, 0x17, (init_freq >> 8) & 0xff);
  173. l64781_writereg (state, 0x18, (init_freq >> 16) & 0xff);
  174. l64781_writereg (state, 0x1b, spi_bias & 0xff);
  175. l64781_writereg (state, 0x1c, (spi_bias >> 8) & 0xff);
  176. l64781_writereg (state, 0x1d, ((spi_bias >> 16) & 0x7f) |
  177. (param->inversion == INVERSION_ON ? 0x80 : 0x00));
  178. l64781_writereg (state, 0x22, ddfs_offset_fixed & 0xff);
  179. l64781_writereg (state, 0x23, (ddfs_offset_fixed >> 8) & 0x3f);
  180. l64781_readreg (state, 0x00); /* clear interrupt registers... */
  181. l64781_readreg (state, 0x01); /* dto. */
  182. apply_tps (state);
  183. return 0;
  184. }
  185. static int get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters* param)
  186. {
  187. struct l64781_state* state = fe->demodulator_priv;
  188. int tmp;
  189. tmp = l64781_readreg(state, 0x04);
  190. switch(tmp & 3) {
  191. case 0:
  192. param->u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
  193. break;
  194. case 1:
  195. param->u.ofdm.guard_interval = GUARD_INTERVAL_1_16;
  196. break;
  197. case 2:
  198. param->u.ofdm.guard_interval = GUARD_INTERVAL_1_8;
  199. break;
  200. case 3:
  201. param->u.ofdm.guard_interval = GUARD_INTERVAL_1_4;
  202. break;
  203. }
  204. switch((tmp >> 2) & 3) {
  205. case 0:
  206. param->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K;
  207. break;
  208. case 1:
  209. param->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
  210. break;
  211. default:
  212. printk("Unexpected value for transmission_mode\n");
  213. }
  214. tmp = l64781_readreg(state, 0x05);
  215. switch(tmp & 7) {
  216. case 0:
  217. param->u.ofdm.code_rate_HP = FEC_1_2;
  218. break;
  219. case 1:
  220. param->u.ofdm.code_rate_HP = FEC_2_3;
  221. break;
  222. case 2:
  223. param->u.ofdm.code_rate_HP = FEC_3_4;
  224. break;
  225. case 3:
  226. param->u.ofdm.code_rate_HP = FEC_5_6;
  227. break;
  228. case 4:
  229. param->u.ofdm.code_rate_HP = FEC_7_8;
  230. break;
  231. default:
  232. printk("Unexpected value for code_rate_HP\n");
  233. }
  234. switch((tmp >> 3) & 7) {
  235. case 0:
  236. param->u.ofdm.code_rate_LP = FEC_1_2;
  237. break;
  238. case 1:
  239. param->u.ofdm.code_rate_LP = FEC_2_3;
  240. break;
  241. case 2:
  242. param->u.ofdm.code_rate_LP = FEC_3_4;
  243. break;
  244. case 3:
  245. param->u.ofdm.code_rate_LP = FEC_5_6;
  246. break;
  247. case 4:
  248. param->u.ofdm.code_rate_LP = FEC_7_8;
  249. break;
  250. default:
  251. printk("Unexpected value for code_rate_LP\n");
  252. }
  253. tmp = l64781_readreg(state, 0x06);
  254. switch(tmp & 3) {
  255. case 0:
  256. param->u.ofdm.constellation = QPSK;
  257. break;
  258. case 1:
  259. param->u.ofdm.constellation = QAM_16;
  260. break;
  261. case 2:
  262. param->u.ofdm.constellation = QAM_64;
  263. break;
  264. default:
  265. printk("Unexpected value for constellation\n");
  266. }
  267. switch((tmp >> 2) & 7) {
  268. case 0:
  269. param->u.ofdm.hierarchy_information = HIERARCHY_NONE;
  270. break;
  271. case 1:
  272. param->u.ofdm.hierarchy_information = HIERARCHY_1;
  273. break;
  274. case 2:
  275. param->u.ofdm.hierarchy_information = HIERARCHY_2;
  276. break;
  277. case 3:
  278. param->u.ofdm.hierarchy_information = HIERARCHY_4;
  279. break;
  280. default:
  281. printk("Unexpected value for hierarchy\n");
  282. }
  283. tmp = l64781_readreg (state, 0x1d);
  284. param->inversion = (tmp & 0x80) ? INVERSION_ON : INVERSION_OFF;
  285. tmp = (int) (l64781_readreg (state, 0x08) |
  286. (l64781_readreg (state, 0x09) << 8) |
  287. (l64781_readreg (state, 0x0a) << 16));
  288. param->frequency += tmp;
  289. return 0;
  290. }
  291. static int l64781_read_status(struct dvb_frontend* fe, fe_status_t* status)
  292. {
  293. struct l64781_state* state = fe->demodulator_priv;
  294. int sync = l64781_readreg (state, 0x32);
  295. int gain = l64781_readreg (state, 0x0e);
  296. l64781_readreg (state, 0x00); /* clear interrupt registers... */
  297. l64781_readreg (state, 0x01); /* dto. */
  298. *status = 0;
  299. if (gain > 5)
  300. *status |= FE_HAS_SIGNAL;
  301. if (sync & 0x02) /* VCXO locked, this criteria should be ok */
  302. *status |= FE_HAS_CARRIER;
  303. if (sync & 0x20)
  304. *status |= FE_HAS_VITERBI;
  305. if (sync & 0x40)
  306. *status |= FE_HAS_SYNC;
  307. if (sync == 0x7f)
  308. *status |= FE_HAS_LOCK;
  309. return 0;
  310. }
  311. static int l64781_read_ber(struct dvb_frontend* fe, u32* ber)
  312. {
  313. struct l64781_state* state = fe->demodulator_priv;
  314. /* XXX FIXME: set up counting period (reg 0x26...0x28)
  315. */
  316. *ber = l64781_readreg (state, 0x39)
  317. | (l64781_readreg (state, 0x3a) << 8);
  318. return 0;
  319. }
  320. static int l64781_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
  321. {
  322. struct l64781_state* state = fe->demodulator_priv;
  323. u8 gain = l64781_readreg (state, 0x0e);
  324. *signal_strength = (gain << 8) | gain;
  325. return 0;
  326. }
  327. static int l64781_read_snr(struct dvb_frontend* fe, u16* snr)
  328. {
  329. struct l64781_state* state = fe->demodulator_priv;
  330. u8 avg_quality = 0xff - l64781_readreg (state, 0x33);
  331. *snr = (avg_quality << 8) | avg_quality; /* not exact, but...*/
  332. return 0;
  333. }
  334. static int l64781_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  335. {
  336. struct l64781_state* state = fe->demodulator_priv;
  337. *ucblocks = l64781_readreg (state, 0x37)
  338. | (l64781_readreg (state, 0x38) << 8);
  339. return 0;
  340. }
  341. static int l64781_sleep(struct dvb_frontend* fe)
  342. {
  343. struct l64781_state* state = fe->demodulator_priv;
  344. /* Power down */
  345. return l64781_writereg (state, 0x3e, 0x5a);
  346. }
  347. static int l64781_init(struct dvb_frontend* fe)
  348. {
  349. struct l64781_state* state = fe->demodulator_priv;
  350. reset_and_configure (state);
  351. /* Power up */
  352. l64781_writereg (state, 0x3e, 0xa5);
  353. /* Reset hard */
  354. l64781_writereg (state, 0x2a, 0x04);
  355. l64781_writereg (state, 0x2a, 0x00);
  356. /* Set tuner specific things */
  357. /* AFC_POL, set also in reset_afc */
  358. l64781_writereg (state, 0x07, 0x8e);
  359. /* Use internal ADC */
  360. l64781_writereg (state, 0x0b, 0x81);
  361. /* AGC loop gain, and polarity is positive */
  362. l64781_writereg (state, 0x0c, 0x84);
  363. /* Internal ADC outputs two's complement */
  364. l64781_writereg (state, 0x0d, 0x8c);
  365. /* With ppm=8000, it seems the DTR_SENSITIVITY will result in
  366. value of 2 with all possible bandwidths and guard
  367. intervals, which is the initial value anyway. */
  368. /*l64781_writereg (state, 0x19, 0x92);*/
  369. /* Everything is two's complement, soft bit and CSI_OUT too */
  370. l64781_writereg (state, 0x1e, 0x09);
  371. /* delay a bit after first init attempt */
  372. if (state->first) {
  373. state->first = 0;
  374. msleep(200);
  375. }
  376. return 0;
  377. }
  378. static int l64781_get_tune_settings(struct dvb_frontend* fe,
  379. struct dvb_frontend_tune_settings* fesettings)
  380. {
  381. fesettings->min_delay_ms = 4000;
  382. fesettings->step_size = 0;
  383. fesettings->max_drift = 0;
  384. return 0;
  385. }
  386. static void l64781_release(struct dvb_frontend* fe)
  387. {
  388. struct l64781_state* state = fe->demodulator_priv;
  389. kfree(state);
  390. }
  391. static struct dvb_frontend_ops l64781_ops;
  392. struct dvb_frontend* l64781_attach(const struct l64781_config* config,
  393. struct i2c_adapter* i2c)
  394. {
  395. struct l64781_state* state = NULL;
  396. int reg0x3e = -1;
  397. u8 b0 [] = { 0x1a };
  398. u8 b1 [] = { 0x00 };
  399. struct i2c_msg msg [] = { { .addr = config->demod_address, .flags = 0, .buf = b0, .len = 1 },
  400. { .addr = config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
  401. /* allocate memory for the internal state */
  402. state = kzalloc(sizeof(struct l64781_state), GFP_KERNEL);
  403. if (state == NULL) goto error;
  404. /* setup the state */
  405. state->config = config;
  406. state->i2c = i2c;
  407. state->first = 1;
  408. /**
  409. * the L64781 won't show up before we send the reset_and_configure()
  410. * broadcast. If nothing responds there is no L64781 on the bus...
  411. */
  412. if (reset_and_configure(state) < 0) {
  413. dprintk("No response to reset and configure broadcast...\n");
  414. goto error;
  415. }
  416. /* The chip always responds to reads */
  417. if (i2c_transfer(state->i2c, msg, 2) != 2) {
  418. dprintk("No response to read on I2C bus\n");
  419. goto error;
  420. }
  421. /* Save current register contents for bailout */
  422. reg0x3e = l64781_readreg(state, 0x3e);
  423. /* Reading the POWER_DOWN register always returns 0 */
  424. if (reg0x3e != 0) {
  425. dprintk("Device doesn't look like L64781\n");
  426. goto error;
  427. }
  428. /* Turn the chip off */
  429. l64781_writereg (state, 0x3e, 0x5a);
  430. /* Responds to all reads with 0 */
  431. if (l64781_readreg(state, 0x1a) != 0) {
  432. dprintk("Read 1 returned unexpcted value\n");
  433. goto error;
  434. }
  435. /* Turn the chip on */
  436. l64781_writereg (state, 0x3e, 0xa5);
  437. /* Responds with register default value */
  438. if (l64781_readreg(state, 0x1a) != 0xa1) {
  439. dprintk("Read 2 returned unexpcted value\n");
  440. goto error;
  441. }
  442. /* create dvb_frontend */
  443. memcpy(&state->frontend.ops, &l64781_ops, sizeof(struct dvb_frontend_ops));
  444. state->frontend.demodulator_priv = state;
  445. return &state->frontend;
  446. error:
  447. if (reg0x3e >= 0)
  448. l64781_writereg (state, 0x3e, reg0x3e); /* restore reg 0x3e */
  449. kfree(state);
  450. return NULL;
  451. }
  452. static struct dvb_frontend_ops l64781_ops = {
  453. .info = {
  454. .name = "LSI L64781 DVB-T",
  455. .type = FE_OFDM,
  456. /* .frequency_min = ???,*/
  457. /* .frequency_max = ???,*/
  458. .frequency_stepsize = 166666,
  459. /* .frequency_tolerance = ???,*/
  460. /* .symbol_rate_tolerance = ???,*/
  461. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  462. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  463. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  464. FE_CAN_MUTE_TS
  465. },
  466. .release = l64781_release,
  467. .init = l64781_init,
  468. .sleep = l64781_sleep,
  469. .set_frontend = apply_frontend_param,
  470. .get_frontend = get_frontend,
  471. .get_tune_settings = l64781_get_tune_settings,
  472. .read_status = l64781_read_status,
  473. .read_ber = l64781_read_ber,
  474. .read_signal_strength = l64781_read_signal_strength,
  475. .read_snr = l64781_read_snr,
  476. .read_ucblocks = l64781_read_ucblocks,
  477. };
  478. MODULE_DESCRIPTION("LSI L64781 DVB-T Demodulator driver");
  479. MODULE_AUTHOR("Holger Waechtler, Marko Kohtala");
  480. MODULE_LICENSE("GPL");
  481. EXPORT_SYMBOL(l64781_attach);