lgdt330x.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*
  2. * Support for LGDT3302 and LGDT3303 - VSB/QAM
  3. *
  4. * Copyright (C) 2005 Wilson Michaels <wilsonmichaels@earthlink.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. */
  21. /*
  22. * NOTES ABOUT THIS DRIVER
  23. *
  24. * This Linux driver supports:
  25. * DViCO FusionHDTV 3 Gold-Q
  26. * DViCO FusionHDTV 3 Gold-T
  27. * DViCO FusionHDTV 5 Gold
  28. * DViCO FusionHDTV 5 Lite
  29. * DViCO FusionHDTV 5 USB Gold
  30. * Air2PC/AirStar 2 ATSC 3rd generation (HD5000)
  31. * pcHDTV HD5500
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/delay.h>
  38. #include <linux/string.h>
  39. #include <linux/slab.h>
  40. #include <asm/byteorder.h>
  41. #include "dvb_frontend.h"
  42. #include "dvb_math.h"
  43. #include "lgdt330x_priv.h"
  44. #include "lgdt330x.h"
  45. /* Use Equalizer Mean Squared Error instead of Phaser Tracker MSE */
  46. /* #define USE_EQMSE */
  47. static int debug;
  48. module_param(debug, int, 0644);
  49. MODULE_PARM_DESC(debug,"Turn on/off lgdt330x frontend debugging (default:off).");
  50. #define dprintk(args...) \
  51. do { \
  52. if (debug) printk(KERN_DEBUG "lgdt330x: " args); \
  53. } while (0)
  54. struct lgdt330x_state
  55. {
  56. struct i2c_adapter* i2c;
  57. /* Configuration settings */
  58. const struct lgdt330x_config* config;
  59. struct dvb_frontend frontend;
  60. /* Demodulator private data */
  61. enum fe_modulation current_modulation;
  62. u32 snr; /* Result of last SNR calculation */
  63. /* Tuner private data */
  64. u32 current_frequency;
  65. };
  66. static int i2c_write_demod_bytes (struct lgdt330x_state* state,
  67. u8 *buf, /* data bytes to send */
  68. int len /* number of bytes to send */ )
  69. {
  70. struct i2c_msg msg =
  71. { .addr = state->config->demod_address,
  72. .flags = 0,
  73. .buf = buf,
  74. .len = 2 };
  75. int i;
  76. int err;
  77. for (i=0; i<len-1; i+=2){
  78. if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
  79. printk(KERN_WARNING "lgdt330x: %s error (addr %02x <- %02x, err = %i)\n", __func__, msg.buf[0], msg.buf[1], err);
  80. if (err < 0)
  81. return err;
  82. else
  83. return -EREMOTEIO;
  84. }
  85. msg.buf += 2;
  86. }
  87. return 0;
  88. }
  89. /*
  90. * This routine writes the register (reg) to the demod bus
  91. * then reads the data returned for (len) bytes.
  92. */
  93. static int i2c_read_demod_bytes(struct lgdt330x_state *state,
  94. enum I2C_REG reg, u8 *buf, int len)
  95. {
  96. u8 wr [] = { reg };
  97. struct i2c_msg msg [] = {
  98. { .addr = state->config->demod_address,
  99. .flags = 0, .buf = wr, .len = 1 },
  100. { .addr = state->config->demod_address,
  101. .flags = I2C_M_RD, .buf = buf, .len = len },
  102. };
  103. int ret;
  104. ret = i2c_transfer(state->i2c, msg, 2);
  105. if (ret != 2) {
  106. printk(KERN_WARNING "lgdt330x: %s: addr 0x%02x select 0x%02x error (ret == %i)\n", __func__, state->config->demod_address, reg, ret);
  107. if (ret >= 0)
  108. ret = -EIO;
  109. } else {
  110. ret = 0;
  111. }
  112. return ret;
  113. }
  114. /* Software reset */
  115. static int lgdt3302_SwReset(struct lgdt330x_state* state)
  116. {
  117. u8 ret;
  118. u8 reset[] = {
  119. IRQ_MASK,
  120. 0x00 /* bit 6 is active low software reset
  121. * bits 5-0 are 1 to mask interrupts */
  122. };
  123. ret = i2c_write_demod_bytes(state,
  124. reset, sizeof(reset));
  125. if (ret == 0) {
  126. /* force reset high (inactive) and unmask interrupts */
  127. reset[1] = 0x7f;
  128. ret = i2c_write_demod_bytes(state,
  129. reset, sizeof(reset));
  130. }
  131. return ret;
  132. }
  133. static int lgdt3303_SwReset(struct lgdt330x_state* state)
  134. {
  135. u8 ret;
  136. u8 reset[] = {
  137. 0x02,
  138. 0x00 /* bit 0 is active low software reset */
  139. };
  140. ret = i2c_write_demod_bytes(state,
  141. reset, sizeof(reset));
  142. if (ret == 0) {
  143. /* force reset high (inactive) */
  144. reset[1] = 0x01;
  145. ret = i2c_write_demod_bytes(state,
  146. reset, sizeof(reset));
  147. }
  148. return ret;
  149. }
  150. static int lgdt330x_SwReset(struct lgdt330x_state* state)
  151. {
  152. switch (state->config->demod_chip) {
  153. case LGDT3302:
  154. return lgdt3302_SwReset(state);
  155. case LGDT3303:
  156. return lgdt3303_SwReset(state);
  157. default:
  158. return -ENODEV;
  159. }
  160. }
  161. static int lgdt330x_init(struct dvb_frontend* fe)
  162. {
  163. /* Hardware reset is done using gpio[0] of cx23880x chip.
  164. * I'd like to do it here, but don't know how to find chip address.
  165. * cx88-cards.c arranges for the reset bit to be inactive (high).
  166. * Maybe there needs to be a callable function in cx88-core or
  167. * the caller of this function needs to do it. */
  168. /*
  169. * Array of byte pairs <address, value>
  170. * to initialize each different chip
  171. */
  172. static u8 lgdt3302_init_data[] = {
  173. /* Use 50MHz parameter values from spec sheet since xtal is 50 */
  174. /* Change the value of NCOCTFV[25:0] of carrier
  175. recovery center frequency register */
  176. VSB_CARRIER_FREQ0, 0x00,
  177. VSB_CARRIER_FREQ1, 0x87,
  178. VSB_CARRIER_FREQ2, 0x8e,
  179. VSB_CARRIER_FREQ3, 0x01,
  180. /* Change the TPCLK pin polarity
  181. data is valid on falling clock */
  182. DEMUX_CONTROL, 0xfb,
  183. /* Change the value of IFBW[11:0] of
  184. AGC IF/RF loop filter bandwidth register */
  185. AGC_RF_BANDWIDTH0, 0x40,
  186. AGC_RF_BANDWIDTH1, 0x93,
  187. AGC_RF_BANDWIDTH2, 0x00,
  188. /* Change the value of bit 6, 'nINAGCBY' and
  189. 'NSSEL[1:0] of ACG function control register 2 */
  190. AGC_FUNC_CTRL2, 0xc6,
  191. /* Change the value of bit 6 'RFFIX'
  192. of AGC function control register 3 */
  193. AGC_FUNC_CTRL3, 0x40,
  194. /* Set the value of 'INLVTHD' register 0x2a/0x2c
  195. to 0x7fe */
  196. AGC_DELAY0, 0x07,
  197. AGC_DELAY2, 0xfe,
  198. /* Change the value of IAGCBW[15:8]
  199. of inner AGC loop filter bandwidth */
  200. AGC_LOOP_BANDWIDTH0, 0x08,
  201. AGC_LOOP_BANDWIDTH1, 0x9a
  202. };
  203. static u8 lgdt3303_init_data[] = {
  204. 0x4c, 0x14
  205. };
  206. static u8 flip_1_lgdt3303_init_data[] = {
  207. 0x4c, 0x14,
  208. 0x87, 0xf3
  209. };
  210. static u8 flip_2_lgdt3303_init_data[] = {
  211. 0x4c, 0x14,
  212. 0x87, 0xda
  213. };
  214. struct lgdt330x_state* state = fe->demodulator_priv;
  215. char *chip_name;
  216. int err;
  217. switch (state->config->demod_chip) {
  218. case LGDT3302:
  219. chip_name = "LGDT3302";
  220. err = i2c_write_demod_bytes(state, lgdt3302_init_data,
  221. sizeof(lgdt3302_init_data));
  222. break;
  223. case LGDT3303:
  224. chip_name = "LGDT3303";
  225. switch (state->config->clock_polarity_flip) {
  226. case 2:
  227. err = i2c_write_demod_bytes(state,
  228. flip_2_lgdt3303_init_data,
  229. sizeof(flip_2_lgdt3303_init_data));
  230. break;
  231. case 1:
  232. err = i2c_write_demod_bytes(state,
  233. flip_1_lgdt3303_init_data,
  234. sizeof(flip_1_lgdt3303_init_data));
  235. break;
  236. case 0:
  237. default:
  238. err = i2c_write_demod_bytes(state, lgdt3303_init_data,
  239. sizeof(lgdt3303_init_data));
  240. }
  241. break;
  242. default:
  243. chip_name = "undefined";
  244. printk (KERN_WARNING "Only LGDT3302 and LGDT3303 are supported chips.\n");
  245. err = -ENODEV;
  246. }
  247. dprintk("%s entered as %s\n", __func__, chip_name);
  248. if (err < 0)
  249. return err;
  250. return lgdt330x_SwReset(state);
  251. }
  252. static int lgdt330x_read_ber(struct dvb_frontend* fe, u32* ber)
  253. {
  254. *ber = 0; /* Not supplied by the demod chips */
  255. return 0;
  256. }
  257. static int lgdt330x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  258. {
  259. struct lgdt330x_state* state = fe->demodulator_priv;
  260. int err;
  261. u8 buf[2];
  262. *ucblocks = 0;
  263. switch (state->config->demod_chip) {
  264. case LGDT3302:
  265. err = i2c_read_demod_bytes(state, LGDT3302_PACKET_ERR_COUNTER1,
  266. buf, sizeof(buf));
  267. break;
  268. case LGDT3303:
  269. err = i2c_read_demod_bytes(state, LGDT3303_PACKET_ERR_COUNTER1,
  270. buf, sizeof(buf));
  271. break;
  272. default:
  273. printk(KERN_WARNING
  274. "Only LGDT3302 and LGDT3303 are supported chips.\n");
  275. err = -ENODEV;
  276. }
  277. if (err < 0)
  278. return err;
  279. *ucblocks = (buf[0] << 8) | buf[1];
  280. return 0;
  281. }
  282. static int lgdt330x_set_parameters(struct dvb_frontend *fe)
  283. {
  284. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  285. /*
  286. * Array of byte pairs <address, value>
  287. * to initialize 8VSB for lgdt3303 chip 50 MHz IF
  288. */
  289. static u8 lgdt3303_8vsb_44_data[] = {
  290. 0x04, 0x00,
  291. 0x0d, 0x40,
  292. 0x0e, 0x87,
  293. 0x0f, 0x8e,
  294. 0x10, 0x01,
  295. 0x47, 0x8b };
  296. /*
  297. * Array of byte pairs <address, value>
  298. * to initialize QAM for lgdt3303 chip
  299. */
  300. static u8 lgdt3303_qam_data[] = {
  301. 0x04, 0x00,
  302. 0x0d, 0x00,
  303. 0x0e, 0x00,
  304. 0x0f, 0x00,
  305. 0x10, 0x00,
  306. 0x51, 0x63,
  307. 0x47, 0x66,
  308. 0x48, 0x66,
  309. 0x4d, 0x1a,
  310. 0x49, 0x08,
  311. 0x4a, 0x9b };
  312. struct lgdt330x_state* state = fe->demodulator_priv;
  313. static u8 top_ctrl_cfg[] = { TOP_CONTROL, 0x03 };
  314. int err = 0;
  315. /* Change only if we are actually changing the modulation */
  316. if (state->current_modulation != p->modulation) {
  317. switch (p->modulation) {
  318. case VSB_8:
  319. dprintk("%s: VSB_8 MODE\n", __func__);
  320. /* Select VSB mode */
  321. top_ctrl_cfg[1] = 0x03;
  322. /* Select ANT connector if supported by card */
  323. if (state->config->pll_rf_set)
  324. state->config->pll_rf_set(fe, 1);
  325. if (state->config->demod_chip == LGDT3303) {
  326. err = i2c_write_demod_bytes(state, lgdt3303_8vsb_44_data,
  327. sizeof(lgdt3303_8vsb_44_data));
  328. }
  329. break;
  330. case QAM_64:
  331. dprintk("%s: QAM_64 MODE\n", __func__);
  332. /* Select QAM_64 mode */
  333. top_ctrl_cfg[1] = 0x00;
  334. /* Select CABLE connector if supported by card */
  335. if (state->config->pll_rf_set)
  336. state->config->pll_rf_set(fe, 0);
  337. if (state->config->demod_chip == LGDT3303) {
  338. err = i2c_write_demod_bytes(state, lgdt3303_qam_data,
  339. sizeof(lgdt3303_qam_data));
  340. }
  341. break;
  342. case QAM_256:
  343. dprintk("%s: QAM_256 MODE\n", __func__);
  344. /* Select QAM_256 mode */
  345. top_ctrl_cfg[1] = 0x01;
  346. /* Select CABLE connector if supported by card */
  347. if (state->config->pll_rf_set)
  348. state->config->pll_rf_set(fe, 0);
  349. if (state->config->demod_chip == LGDT3303) {
  350. err = i2c_write_demod_bytes(state, lgdt3303_qam_data,
  351. sizeof(lgdt3303_qam_data));
  352. }
  353. break;
  354. default:
  355. printk(KERN_WARNING "lgdt330x: %s: Modulation type(%d) UNSUPPORTED\n", __func__, p->modulation);
  356. return -1;
  357. }
  358. if (err < 0)
  359. printk(KERN_WARNING "lgdt330x: %s: error blasting "
  360. "bytes to lgdt3303 for modulation type(%d)\n",
  361. __func__, p->modulation);
  362. /*
  363. * select serial or parallel MPEG harware interface
  364. * Serial: 0x04 for LGDT3302 or 0x40 for LGDT3303
  365. * Parallel: 0x00
  366. */
  367. top_ctrl_cfg[1] |= state->config->serial_mpeg;
  368. /* Select the requested mode */
  369. i2c_write_demod_bytes(state, top_ctrl_cfg,
  370. sizeof(top_ctrl_cfg));
  371. if (state->config->set_ts_params)
  372. state->config->set_ts_params(fe, 0);
  373. state->current_modulation = p->modulation;
  374. }
  375. /* Tune to the specified frequency */
  376. if (fe->ops.tuner_ops.set_params) {
  377. fe->ops.tuner_ops.set_params(fe);
  378. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  379. }
  380. /* Keep track of the new frequency */
  381. /* FIXME this is the wrong way to do this... */
  382. /* The tuner is shared with the video4linux analog API */
  383. state->current_frequency = p->frequency;
  384. lgdt330x_SwReset(state);
  385. return 0;
  386. }
  387. static int lgdt330x_get_frontend(struct dvb_frontend *fe)
  388. {
  389. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  390. struct lgdt330x_state *state = fe->demodulator_priv;
  391. p->frequency = state->current_frequency;
  392. return 0;
  393. }
  394. static int lgdt3302_read_status(struct dvb_frontend *fe,
  395. enum fe_status *status)
  396. {
  397. struct lgdt330x_state* state = fe->demodulator_priv;
  398. u8 buf[3];
  399. *status = 0; /* Reset status result */
  400. /* AGC status register */
  401. i2c_read_demod_bytes(state, AGC_STATUS, buf, 1);
  402. dprintk("%s: AGC_STATUS = 0x%02x\n", __func__, buf[0]);
  403. if ((buf[0] & 0x0c) == 0x8){
  404. /* Test signal does not exist flag */
  405. /* as well as the AGC lock flag. */
  406. *status |= FE_HAS_SIGNAL;
  407. }
  408. /*
  409. * You must set the Mask bits to 1 in the IRQ_MASK in order
  410. * to see that status bit in the IRQ_STATUS register.
  411. * This is done in SwReset();
  412. */
  413. /* signal status */
  414. i2c_read_demod_bytes(state, TOP_CONTROL, buf, sizeof(buf));
  415. dprintk("%s: TOP_CONTROL = 0x%02x, IRO_MASK = 0x%02x, IRQ_STATUS = 0x%02x\n", __func__, buf[0], buf[1], buf[2]);
  416. /* sync status */
  417. if ((buf[2] & 0x03) == 0x01) {
  418. *status |= FE_HAS_SYNC;
  419. }
  420. /* FEC error status */
  421. if ((buf[2] & 0x0c) == 0x08) {
  422. *status |= FE_HAS_LOCK;
  423. *status |= FE_HAS_VITERBI;
  424. }
  425. /* Carrier Recovery Lock Status Register */
  426. i2c_read_demod_bytes(state, CARRIER_LOCK, buf, 1);
  427. dprintk("%s: CARRIER_LOCK = 0x%02x\n", __func__, buf[0]);
  428. switch (state->current_modulation) {
  429. case QAM_256:
  430. case QAM_64:
  431. /* Need to understand why there are 3 lock levels here */
  432. if ((buf[0] & 0x07) == 0x07)
  433. *status |= FE_HAS_CARRIER;
  434. break;
  435. case VSB_8:
  436. if ((buf[0] & 0x80) == 0x80)
  437. *status |= FE_HAS_CARRIER;
  438. break;
  439. default:
  440. printk(KERN_WARNING "lgdt330x: %s: Modulation set to unsupported value\n", __func__);
  441. }
  442. return 0;
  443. }
  444. static int lgdt3303_read_status(struct dvb_frontend *fe,
  445. enum fe_status *status)
  446. {
  447. struct lgdt330x_state* state = fe->demodulator_priv;
  448. int err;
  449. u8 buf[3];
  450. *status = 0; /* Reset status result */
  451. /* lgdt3303 AGC status register */
  452. err = i2c_read_demod_bytes(state, 0x58, buf, 1);
  453. if (err < 0)
  454. return err;
  455. dprintk("%s: AGC_STATUS = 0x%02x\n", __func__, buf[0]);
  456. if ((buf[0] & 0x21) == 0x01){
  457. /* Test input signal does not exist flag */
  458. /* as well as the AGC lock flag. */
  459. *status |= FE_HAS_SIGNAL;
  460. }
  461. /* Carrier Recovery Lock Status Register */
  462. i2c_read_demod_bytes(state, CARRIER_LOCK, buf, 1);
  463. dprintk("%s: CARRIER_LOCK = 0x%02x\n", __func__, buf[0]);
  464. switch (state->current_modulation) {
  465. case QAM_256:
  466. case QAM_64:
  467. /* Need to understand why there are 3 lock levels here */
  468. if ((buf[0] & 0x07) == 0x07)
  469. *status |= FE_HAS_CARRIER;
  470. else
  471. break;
  472. i2c_read_demod_bytes(state, 0x8a, buf, 1);
  473. if ((buf[0] & 0x04) == 0x04)
  474. *status |= FE_HAS_SYNC;
  475. if ((buf[0] & 0x01) == 0x01)
  476. *status |= FE_HAS_LOCK;
  477. if ((buf[0] & 0x08) == 0x08)
  478. *status |= FE_HAS_VITERBI;
  479. break;
  480. case VSB_8:
  481. if ((buf[0] & 0x80) == 0x80)
  482. *status |= FE_HAS_CARRIER;
  483. else
  484. break;
  485. i2c_read_demod_bytes(state, 0x38, buf, 1);
  486. if ((buf[0] & 0x02) == 0x00)
  487. *status |= FE_HAS_SYNC;
  488. if ((buf[0] & 0x01) == 0x01) {
  489. *status |= FE_HAS_LOCK;
  490. *status |= FE_HAS_VITERBI;
  491. }
  492. break;
  493. default:
  494. printk(KERN_WARNING "lgdt330x: %s: Modulation set to unsupported value\n", __func__);
  495. }
  496. return 0;
  497. }
  498. /* Calculate SNR estimation (scaled by 2^24)
  499. 8-VSB SNR equations from LGDT3302 and LGDT3303 datasheets, QAM
  500. equations from LGDT3303 datasheet. VSB is the same between the '02
  501. and '03, so maybe QAM is too? Perhaps someone with a newer datasheet
  502. that has QAM information could verify?
  503. For 8-VSB: (two ways, take your pick)
  504. LGDT3302:
  505. SNR_EQ = 10 * log10(25 * 24^2 / EQ_MSE)
  506. LGDT3303:
  507. SNR_EQ = 10 * log10(25 * 32^2 / EQ_MSE)
  508. LGDT3302 & LGDT3303:
  509. SNR_PT = 10 * log10(25 * 32^2 / PT_MSE) (we use this one)
  510. For 64-QAM:
  511. SNR = 10 * log10( 688128 / MSEQAM)
  512. For 256-QAM:
  513. SNR = 10 * log10( 696320 / MSEQAM)
  514. We re-write the snr equation as:
  515. SNR * 2^24 = 10*(c - intlog10(MSE))
  516. Where for 256-QAM, c = log10(696320) * 2^24, and so on. */
  517. static u32 calculate_snr(u32 mse, u32 c)
  518. {
  519. if (mse == 0) /* No signal */
  520. return 0;
  521. mse = intlog10(mse);
  522. if (mse > c) {
  523. /* Negative SNR, which is possible, but realisticly the
  524. demod will lose lock before the signal gets this bad. The
  525. API only allows for unsigned values, so just return 0 */
  526. return 0;
  527. }
  528. return 10*(c - mse);
  529. }
  530. static int lgdt3302_read_snr(struct dvb_frontend* fe, u16* snr)
  531. {
  532. struct lgdt330x_state* state = (struct lgdt330x_state*) fe->demodulator_priv;
  533. u8 buf[5]; /* read data buffer */
  534. u32 noise; /* noise value */
  535. u32 c; /* per-modulation SNR calculation constant */
  536. switch(state->current_modulation) {
  537. case VSB_8:
  538. i2c_read_demod_bytes(state, LGDT3302_EQPH_ERR0, buf, 5);
  539. #ifdef USE_EQMSE
  540. /* Use Equalizer Mean-Square Error Register */
  541. /* SNR for ranges from -15.61 to +41.58 */
  542. noise = ((buf[0] & 7) << 16) | (buf[1] << 8) | buf[2];
  543. c = 69765745; /* log10(25*24^2)*2^24 */
  544. #else
  545. /* Use Phase Tracker Mean-Square Error Register */
  546. /* SNR for ranges from -13.11 to +44.08 */
  547. noise = ((buf[0] & 7<<3) << 13) | (buf[3] << 8) | buf[4];
  548. c = 73957994; /* log10(25*32^2)*2^24 */
  549. #endif
  550. break;
  551. case QAM_64:
  552. case QAM_256:
  553. i2c_read_demod_bytes(state, CARRIER_MSEQAM1, buf, 2);
  554. noise = ((buf[0] & 3) << 8) | buf[1];
  555. c = state->current_modulation == QAM_64 ? 97939837 : 98026066;
  556. /* log10(688128)*2^24 and log10(696320)*2^24 */
  557. break;
  558. default:
  559. printk(KERN_ERR "lgdt330x: %s: Modulation set to unsupported value\n",
  560. __func__);
  561. return -EREMOTEIO; /* return -EDRIVER_IS_GIBBERED; */
  562. }
  563. state->snr = calculate_snr(noise, c);
  564. *snr = (state->snr) >> 16; /* Convert from 8.24 fixed-point to 8.8 */
  565. dprintk("%s: noise = 0x%08x, snr = %d.%02d dB\n", __func__, noise,
  566. state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
  567. return 0;
  568. }
  569. static int lgdt3303_read_snr(struct dvb_frontend* fe, u16* snr)
  570. {
  571. struct lgdt330x_state* state = (struct lgdt330x_state*) fe->demodulator_priv;
  572. u8 buf[5]; /* read data buffer */
  573. u32 noise; /* noise value */
  574. u32 c; /* per-modulation SNR calculation constant */
  575. switch(state->current_modulation) {
  576. case VSB_8:
  577. i2c_read_demod_bytes(state, LGDT3303_EQPH_ERR0, buf, 5);
  578. #ifdef USE_EQMSE
  579. /* Use Equalizer Mean-Square Error Register */
  580. /* SNR for ranges from -16.12 to +44.08 */
  581. noise = ((buf[0] & 0x78) << 13) | (buf[1] << 8) | buf[2];
  582. c = 73957994; /* log10(25*32^2)*2^24 */
  583. #else
  584. /* Use Phase Tracker Mean-Square Error Register */
  585. /* SNR for ranges from -13.11 to +44.08 */
  586. noise = ((buf[0] & 7) << 16) | (buf[3] << 8) | buf[4];
  587. c = 73957994; /* log10(25*32^2)*2^24 */
  588. #endif
  589. break;
  590. case QAM_64:
  591. case QAM_256:
  592. i2c_read_demod_bytes(state, CARRIER_MSEQAM1, buf, 2);
  593. noise = (buf[0] << 8) | buf[1];
  594. c = state->current_modulation == QAM_64 ? 97939837 : 98026066;
  595. /* log10(688128)*2^24 and log10(696320)*2^24 */
  596. break;
  597. default:
  598. printk(KERN_ERR "lgdt330x: %s: Modulation set to unsupported value\n",
  599. __func__);
  600. return -EREMOTEIO; /* return -EDRIVER_IS_GIBBERED; */
  601. }
  602. state->snr = calculate_snr(noise, c);
  603. *snr = (state->snr) >> 16; /* Convert from 8.24 fixed-point to 8.8 */
  604. dprintk("%s: noise = 0x%08x, snr = %d.%02d dB\n", __func__, noise,
  605. state->snr >> 24, (((state->snr >> 8) & 0xffff) * 100) >> 16);
  606. return 0;
  607. }
  608. static int lgdt330x_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  609. {
  610. /* Calculate Strength from SNR up to 35dB */
  611. /* Even though the SNR can go higher than 35dB, there is some comfort */
  612. /* factor in having a range of strong signals that can show at 100% */
  613. struct lgdt330x_state* state = (struct lgdt330x_state*) fe->demodulator_priv;
  614. u16 snr;
  615. int ret;
  616. ret = fe->ops.read_snr(fe, &snr);
  617. if (ret != 0)
  618. return ret;
  619. /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
  620. /* scale the range 0 - 35*2^24 into 0 - 65535 */
  621. if (state->snr >= 8960 * 0x10000)
  622. *strength = 0xffff;
  623. else
  624. *strength = state->snr / 8960;
  625. return 0;
  626. }
  627. static int lgdt330x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fe_tune_settings)
  628. {
  629. /* I have no idea about this - it may not be needed */
  630. fe_tune_settings->min_delay_ms = 500;
  631. fe_tune_settings->step_size = 0;
  632. fe_tune_settings->max_drift = 0;
  633. return 0;
  634. }
  635. static void lgdt330x_release(struct dvb_frontend* fe)
  636. {
  637. struct lgdt330x_state* state = (struct lgdt330x_state*) fe->demodulator_priv;
  638. kfree(state);
  639. }
  640. static struct dvb_frontend_ops lgdt3302_ops;
  641. static struct dvb_frontend_ops lgdt3303_ops;
  642. struct dvb_frontend* lgdt330x_attach(const struct lgdt330x_config* config,
  643. struct i2c_adapter* i2c)
  644. {
  645. struct lgdt330x_state* state = NULL;
  646. u8 buf[1];
  647. /* Allocate memory for the internal state */
  648. state = kzalloc(sizeof(struct lgdt330x_state), GFP_KERNEL);
  649. if (state == NULL)
  650. goto error;
  651. /* Setup the state */
  652. state->config = config;
  653. state->i2c = i2c;
  654. /* Create dvb_frontend */
  655. switch (config->demod_chip) {
  656. case LGDT3302:
  657. memcpy(&state->frontend.ops, &lgdt3302_ops, sizeof(struct dvb_frontend_ops));
  658. break;
  659. case LGDT3303:
  660. memcpy(&state->frontend.ops, &lgdt3303_ops, sizeof(struct dvb_frontend_ops));
  661. break;
  662. default:
  663. goto error;
  664. }
  665. state->frontend.demodulator_priv = state;
  666. /* Verify communication with demod chip */
  667. if (i2c_read_demod_bytes(state, 2, buf, 1))
  668. goto error;
  669. state->current_frequency = -1;
  670. state->current_modulation = -1;
  671. return &state->frontend;
  672. error:
  673. kfree(state);
  674. dprintk("%s: ERROR\n",__func__);
  675. return NULL;
  676. }
  677. static struct dvb_frontend_ops lgdt3302_ops = {
  678. .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
  679. .info = {
  680. .name= "LG Electronics LGDT3302 VSB/QAM Frontend",
  681. .frequency_min= 54000000,
  682. .frequency_max= 858000000,
  683. .frequency_stepsize= 62500,
  684. .symbol_rate_min = 5056941, /* QAM 64 */
  685. .symbol_rate_max = 10762000, /* VSB 8 */
  686. .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
  687. },
  688. .init = lgdt330x_init,
  689. .set_frontend = lgdt330x_set_parameters,
  690. .get_frontend = lgdt330x_get_frontend,
  691. .get_tune_settings = lgdt330x_get_tune_settings,
  692. .read_status = lgdt3302_read_status,
  693. .read_ber = lgdt330x_read_ber,
  694. .read_signal_strength = lgdt330x_read_signal_strength,
  695. .read_snr = lgdt3302_read_snr,
  696. .read_ucblocks = lgdt330x_read_ucblocks,
  697. .release = lgdt330x_release,
  698. };
  699. static struct dvb_frontend_ops lgdt3303_ops = {
  700. .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
  701. .info = {
  702. .name= "LG Electronics LGDT3303 VSB/QAM Frontend",
  703. .frequency_min= 54000000,
  704. .frequency_max= 858000000,
  705. .frequency_stepsize= 62500,
  706. .symbol_rate_min = 5056941, /* QAM 64 */
  707. .symbol_rate_max = 10762000, /* VSB 8 */
  708. .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
  709. },
  710. .init = lgdt330x_init,
  711. .set_frontend = lgdt330x_set_parameters,
  712. .get_frontend = lgdt330x_get_frontend,
  713. .get_tune_settings = lgdt330x_get_tune_settings,
  714. .read_status = lgdt3303_read_status,
  715. .read_ber = lgdt330x_read_ber,
  716. .read_signal_strength = lgdt330x_read_signal_strength,
  717. .read_snr = lgdt3303_read_snr,
  718. .read_ucblocks = lgdt330x_read_ucblocks,
  719. .release = lgdt330x_release,
  720. };
  721. MODULE_DESCRIPTION("LGDT330X (ATSC 8VSB & ITU-T J.83 AnnexB 64/256 QAM) Demodulator Driver");
  722. MODULE_AUTHOR("Wilson Michaels");
  723. MODULE_LICENSE("GPL");
  724. EXPORT_SYMBOL(lgdt330x_attach);