lgdt330x.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. fe_modulation_t 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. switch (state->config->demod_chip) {
  263. case LGDT3302:
  264. err = i2c_read_demod_bytes(state, LGDT3302_PACKET_ERR_COUNTER1,
  265. buf, sizeof(buf));
  266. break;
  267. case LGDT3303:
  268. err = i2c_read_demod_bytes(state, LGDT3303_PACKET_ERR_COUNTER1,
  269. buf, sizeof(buf));
  270. break;
  271. default:
  272. printk(KERN_WARNING
  273. "Only LGDT3302 and LGDT3303 are supported chips.\n");
  274. err = -ENODEV;
  275. }
  276. *ucblocks = (buf[0] << 8) | buf[1];
  277. return 0;
  278. }
  279. static int lgdt330x_set_parameters(struct dvb_frontend* fe,
  280. struct dvb_frontend_parameters *param)
  281. {
  282. /*
  283. * Array of byte pairs <address, value>
  284. * to initialize 8VSB for lgdt3303 chip 50 MHz IF
  285. */
  286. static u8 lgdt3303_8vsb_44_data[] = {
  287. 0x04, 0x00,
  288. 0x0d, 0x40,
  289. 0x0e, 0x87,
  290. 0x0f, 0x8e,
  291. 0x10, 0x01,
  292. 0x47, 0x8b };
  293. /*
  294. * Array of byte pairs <address, value>
  295. * to initialize QAM for lgdt3303 chip
  296. */
  297. static u8 lgdt3303_qam_data[] = {
  298. 0x04, 0x00,
  299. 0x0d, 0x00,
  300. 0x0e, 0x00,
  301. 0x0f, 0x00,
  302. 0x10, 0x00,
  303. 0x51, 0x63,
  304. 0x47, 0x66,
  305. 0x48, 0x66,
  306. 0x4d, 0x1a,
  307. 0x49, 0x08,
  308. 0x4a, 0x9b };
  309. struct lgdt330x_state* state = fe->demodulator_priv;
  310. static u8 top_ctrl_cfg[] = { TOP_CONTROL, 0x03 };
  311. int err;
  312. /* Change only if we are actually changing the modulation */
  313. if (state->current_modulation != param->u.vsb.modulation) {
  314. switch(param->u.vsb.modulation) {
  315. case VSB_8:
  316. dprintk("%s: VSB_8 MODE\n", __func__);
  317. /* Select VSB mode */
  318. top_ctrl_cfg[1] = 0x03;
  319. /* Select ANT connector if supported by card */
  320. if (state->config->pll_rf_set)
  321. state->config->pll_rf_set(fe, 1);
  322. if (state->config->demod_chip == LGDT3303) {
  323. err = i2c_write_demod_bytes(state, lgdt3303_8vsb_44_data,
  324. sizeof(lgdt3303_8vsb_44_data));
  325. }
  326. break;
  327. case QAM_64:
  328. dprintk("%s: QAM_64 MODE\n", __func__);
  329. /* Select QAM_64 mode */
  330. top_ctrl_cfg[1] = 0x00;
  331. /* Select CABLE connector if supported by card */
  332. if (state->config->pll_rf_set)
  333. state->config->pll_rf_set(fe, 0);
  334. if (state->config->demod_chip == LGDT3303) {
  335. err = i2c_write_demod_bytes(state, lgdt3303_qam_data,
  336. sizeof(lgdt3303_qam_data));
  337. }
  338. break;
  339. case QAM_256:
  340. dprintk("%s: QAM_256 MODE\n", __func__);
  341. /* Select QAM_256 mode */
  342. top_ctrl_cfg[1] = 0x01;
  343. /* Select CABLE connector if supported by card */
  344. if (state->config->pll_rf_set)
  345. state->config->pll_rf_set(fe, 0);
  346. if (state->config->demod_chip == LGDT3303) {
  347. err = i2c_write_demod_bytes(state, lgdt3303_qam_data,
  348. sizeof(lgdt3303_qam_data));
  349. }
  350. break;
  351. default:
  352. printk(KERN_WARNING "lgdt330x: %s: Modulation type(%d) UNSUPPORTED\n", __func__, param->u.vsb.modulation);
  353. return -1;
  354. }
  355. /*
  356. * select serial or parallel MPEG harware interface
  357. * Serial: 0x04 for LGDT3302 or 0x40 for LGDT3303
  358. * Parallel: 0x00
  359. */
  360. top_ctrl_cfg[1] |= state->config->serial_mpeg;
  361. /* Select the requested mode */
  362. i2c_write_demod_bytes(state, top_ctrl_cfg,
  363. sizeof(top_ctrl_cfg));
  364. if (state->config->set_ts_params)
  365. state->config->set_ts_params(fe, 0);
  366. state->current_modulation = param->u.vsb.modulation;
  367. }
  368. /* Tune to the specified frequency */
  369. if (fe->ops.tuner_ops.set_params) {
  370. fe->ops.tuner_ops.set_params(fe, param);
  371. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  372. }
  373. /* Keep track of the new frequency */
  374. /* FIXME this is the wrong way to do this... */
  375. /* The tuner is shared with the video4linux analog API */
  376. state->current_frequency = param->frequency;
  377. lgdt330x_SwReset(state);
  378. return 0;
  379. }
  380. static int lgdt330x_get_frontend(struct dvb_frontend* fe,
  381. struct dvb_frontend_parameters* param)
  382. {
  383. struct lgdt330x_state *state = fe->demodulator_priv;
  384. param->frequency = state->current_frequency;
  385. return 0;
  386. }
  387. static int lgdt3302_read_status(struct dvb_frontend* fe, fe_status_t* status)
  388. {
  389. struct lgdt330x_state* state = fe->demodulator_priv;
  390. u8 buf[3];
  391. *status = 0; /* Reset status result */
  392. /* AGC status register */
  393. i2c_read_demod_bytes(state, AGC_STATUS, buf, 1);
  394. dprintk("%s: AGC_STATUS = 0x%02x\n", __func__, buf[0]);
  395. if ((buf[0] & 0x0c) == 0x8){
  396. /* Test signal does not exist flag */
  397. /* as well as the AGC lock flag. */
  398. *status |= FE_HAS_SIGNAL;
  399. }
  400. /*
  401. * You must set the Mask bits to 1 in the IRQ_MASK in order
  402. * to see that status bit in the IRQ_STATUS register.
  403. * This is done in SwReset();
  404. */
  405. /* signal status */
  406. i2c_read_demod_bytes(state, TOP_CONTROL, buf, sizeof(buf));
  407. dprintk("%s: TOP_CONTROL = 0x%02x, IRO_MASK = 0x%02x, IRQ_STATUS = 0x%02x\n", __func__, buf[0], buf[1], buf[2]);
  408. /* sync status */
  409. if ((buf[2] & 0x03) == 0x01) {
  410. *status |= FE_HAS_SYNC;
  411. }
  412. /* FEC error status */
  413. if ((buf[2] & 0x0c) == 0x08) {
  414. *status |= FE_HAS_LOCK;
  415. *status |= FE_HAS_VITERBI;
  416. }
  417. /* Carrier Recovery Lock Status Register */
  418. i2c_read_demod_bytes(state, CARRIER_LOCK, buf, 1);
  419. dprintk("%s: CARRIER_LOCK = 0x%02x\n", __func__, buf[0]);
  420. switch (state->current_modulation) {
  421. case QAM_256:
  422. case QAM_64:
  423. /* Need to understand why there are 3 lock levels here */
  424. if ((buf[0] & 0x07) == 0x07)
  425. *status |= FE_HAS_CARRIER;
  426. break;
  427. case VSB_8:
  428. if ((buf[0] & 0x80) == 0x80)
  429. *status |= FE_HAS_CARRIER;
  430. break;
  431. default:
  432. printk(KERN_WARNING "lgdt330x: %s: Modulation set to unsupported value\n", __func__);
  433. }
  434. return 0;
  435. }
  436. static int lgdt3303_read_status(struct dvb_frontend* fe, fe_status_t* status)
  437. {
  438. struct lgdt330x_state* state = fe->demodulator_priv;
  439. int err;
  440. u8 buf[3];
  441. *status = 0; /* Reset status result */
  442. /* lgdt3303 AGC status register */
  443. err = i2c_read_demod_bytes(state, 0x58, buf, 1);
  444. if (err < 0)
  445. return err;
  446. dprintk("%s: AGC_STATUS = 0x%02x\n", __func__, buf[0]);
  447. if ((buf[0] & 0x21) == 0x01){
  448. /* Test input signal does not exist flag */
  449. /* as well as the AGC lock flag. */
  450. *status |= FE_HAS_SIGNAL;
  451. }
  452. /* Carrier Recovery Lock Status Register */
  453. i2c_read_demod_bytes(state, CARRIER_LOCK, buf, 1);
  454. dprintk("%s: CARRIER_LOCK = 0x%02x\n", __func__, buf[0]);
  455. switch (state->current_modulation) {
  456. case QAM_256:
  457. case QAM_64:
  458. /* Need to understand why there are 3 lock levels here */
  459. if ((buf[0] & 0x07) == 0x07)
  460. *status |= FE_HAS_CARRIER;
  461. else
  462. break;
  463. i2c_read_demod_bytes(state, 0x8a, buf, 1);
  464. if ((buf[0] & 0x04) == 0x04)
  465. *status |= FE_HAS_SYNC;
  466. if ((buf[0] & 0x01) == 0x01)
  467. *status |= FE_HAS_LOCK;
  468. if ((buf[0] & 0x08) == 0x08)
  469. *status |= FE_HAS_VITERBI;
  470. break;
  471. case VSB_8:
  472. if ((buf[0] & 0x80) == 0x80)
  473. *status |= FE_HAS_CARRIER;
  474. else
  475. break;
  476. i2c_read_demod_bytes(state, 0x38, buf, 1);
  477. if ((buf[0] & 0x02) == 0x00)
  478. *status |= FE_HAS_SYNC;
  479. if ((buf[0] & 0x01) == 0x01) {
  480. *status |= FE_HAS_LOCK;
  481. *status |= FE_HAS_VITERBI;
  482. }
  483. break;
  484. default:
  485. printk(KERN_WARNING "lgdt330x: %s: Modulation set to unsupported value\n", __func__);
  486. }
  487. return 0;
  488. }
  489. /* Calculate SNR estimation (scaled by 2^24)
  490. 8-VSB SNR equations from LGDT3302 and LGDT3303 datasheets, QAM
  491. equations from LGDT3303 datasheet. VSB is the same between the '02
  492. and '03, so maybe QAM is too? Perhaps someone with a newer datasheet
  493. that has QAM information could verify?
  494. For 8-VSB: (two ways, take your pick)
  495. LGDT3302:
  496. SNR_EQ = 10 * log10(25 * 24^2 / EQ_MSE)
  497. LGDT3303:
  498. SNR_EQ = 10 * log10(25 * 32^2 / EQ_MSE)
  499. LGDT3302 & LGDT3303:
  500. SNR_PT = 10 * log10(25 * 32^2 / PT_MSE) (we use this one)
  501. For 64-QAM:
  502. SNR = 10 * log10( 688128 / MSEQAM)
  503. For 256-QAM:
  504. SNR = 10 * log10( 696320 / MSEQAM)
  505. We re-write the snr equation as:
  506. SNR * 2^24 = 10*(c - intlog10(MSE))
  507. Where for 256-QAM, c = log10(696320) * 2^24, and so on. */
  508. static u32 calculate_snr(u32 mse, u32 c)
  509. {
  510. if (mse == 0) /* No signal */
  511. return 0;
  512. mse = intlog10(mse);
  513. if (mse > c) {
  514. /* Negative SNR, which is possible, but realisticly the
  515. demod will lose lock before the signal gets this bad. The
  516. API only allows for unsigned values, so just return 0 */
  517. return 0;
  518. }
  519. return 10*(c - mse);
  520. }
  521. static int lgdt3302_read_snr(struct dvb_frontend* fe, u16* snr)
  522. {
  523. struct lgdt330x_state* state = (struct lgdt330x_state*) fe->demodulator_priv;
  524. u8 buf[5]; /* read data buffer */
  525. u32 noise; /* noise value */
  526. u32 c; /* per-modulation SNR calculation constant */
  527. switch(state->current_modulation) {
  528. case VSB_8:
  529. i2c_read_demod_bytes(state, LGDT3302_EQPH_ERR0, buf, 5);
  530. #ifdef USE_EQMSE
  531. /* Use Equalizer Mean-Square Error Register */
  532. /* SNR for ranges from -15.61 to +41.58 */
  533. noise = ((buf[0] & 7) << 16) | (buf[1] << 8) | buf[2];
  534. c = 69765745; /* log10(25*24^2)*2^24 */
  535. #else
  536. /* Use Phase Tracker Mean-Square Error Register */
  537. /* SNR for ranges from -13.11 to +44.08 */
  538. noise = ((buf[0] & 7<<3) << 13) | (buf[3] << 8) | buf[4];
  539. c = 73957994; /* log10(25*32^2)*2^24 */
  540. #endif
  541. break;
  542. case QAM_64:
  543. case QAM_256:
  544. i2c_read_demod_bytes(state, CARRIER_MSEQAM1, buf, 2);
  545. noise = ((buf[0] & 3) << 8) | buf[1];
  546. c = state->current_modulation == QAM_64 ? 97939837 : 98026066;
  547. /* log10(688128)*2^24 and log10(696320)*2^24 */
  548. break;
  549. default:
  550. printk(KERN_ERR "lgdt330x: %s: Modulation set to unsupported value\n",
  551. __func__);
  552. return -EREMOTEIO; /* return -EDRIVER_IS_GIBBERED; */
  553. }
  554. state->snr = calculate_snr(noise, c);
  555. *snr = (state->snr) >> 16; /* Convert from 8.24 fixed-point to 8.8 */
  556. dprintk("%s: noise = 0x%08x, snr = %d.%02d dB\n", __func__, noise,
  557. state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
  558. return 0;
  559. }
  560. static int lgdt3303_read_snr(struct dvb_frontend* fe, u16* snr)
  561. {
  562. struct lgdt330x_state* state = (struct lgdt330x_state*) fe->demodulator_priv;
  563. u8 buf[5]; /* read data buffer */
  564. u32 noise; /* noise value */
  565. u32 c; /* per-modulation SNR calculation constant */
  566. switch(state->current_modulation) {
  567. case VSB_8:
  568. i2c_read_demod_bytes(state, LGDT3303_EQPH_ERR0, buf, 5);
  569. #ifdef USE_EQMSE
  570. /* Use Equalizer Mean-Square Error Register */
  571. /* SNR for ranges from -16.12 to +44.08 */
  572. noise = ((buf[0] & 0x78) << 13) | (buf[1] << 8) | buf[2];
  573. c = 73957994; /* log10(25*32^2)*2^24 */
  574. #else
  575. /* Use Phase Tracker Mean-Square Error Register */
  576. /* SNR for ranges from -13.11 to +44.08 */
  577. noise = ((buf[0] & 7) << 16) | (buf[3] << 8) | buf[4];
  578. c = 73957994; /* log10(25*32^2)*2^24 */
  579. #endif
  580. break;
  581. case QAM_64:
  582. case QAM_256:
  583. i2c_read_demod_bytes(state, CARRIER_MSEQAM1, buf, 2);
  584. noise = (buf[0] << 8) | buf[1];
  585. c = state->current_modulation == QAM_64 ? 97939837 : 98026066;
  586. /* log10(688128)*2^24 and log10(696320)*2^24 */
  587. break;
  588. default:
  589. printk(KERN_ERR "lgdt330x: %s: Modulation set to unsupported value\n",
  590. __func__);
  591. return -EREMOTEIO; /* return -EDRIVER_IS_GIBBERED; */
  592. }
  593. state->snr = calculate_snr(noise, c);
  594. *snr = (state->snr) >> 16; /* Convert from 8.24 fixed-point to 8.8 */
  595. dprintk("%s: noise = 0x%08x, snr = %d.%02d dB\n", __func__, noise,
  596. state->snr >> 24, (((state->snr >> 8) & 0xffff) * 100) >> 16);
  597. return 0;
  598. }
  599. static int lgdt330x_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  600. {
  601. /* Calculate Strength from SNR up to 35dB */
  602. /* Even though the SNR can go higher than 35dB, there is some comfort */
  603. /* factor in having a range of strong signals that can show at 100% */
  604. struct lgdt330x_state* state = (struct lgdt330x_state*) fe->demodulator_priv;
  605. u16 snr;
  606. int ret;
  607. ret = fe->ops.read_snr(fe, &snr);
  608. if (ret != 0)
  609. return ret;
  610. /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
  611. /* scale the range 0 - 35*2^24 into 0 - 65535 */
  612. if (state->snr >= 8960 * 0x10000)
  613. *strength = 0xffff;
  614. else
  615. *strength = state->snr / 8960;
  616. return 0;
  617. }
  618. static int lgdt330x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fe_tune_settings)
  619. {
  620. /* I have no idea about this - it may not be needed */
  621. fe_tune_settings->min_delay_ms = 500;
  622. fe_tune_settings->step_size = 0;
  623. fe_tune_settings->max_drift = 0;
  624. return 0;
  625. }
  626. static void lgdt330x_release(struct dvb_frontend* fe)
  627. {
  628. struct lgdt330x_state* state = (struct lgdt330x_state*) fe->demodulator_priv;
  629. kfree(state);
  630. }
  631. static struct dvb_frontend_ops lgdt3302_ops;
  632. static struct dvb_frontend_ops lgdt3303_ops;
  633. struct dvb_frontend* lgdt330x_attach(const struct lgdt330x_config* config,
  634. struct i2c_adapter* i2c)
  635. {
  636. struct lgdt330x_state* state = NULL;
  637. u8 buf[1];
  638. /* Allocate memory for the internal state */
  639. state = kzalloc(sizeof(struct lgdt330x_state), GFP_KERNEL);
  640. if (state == NULL)
  641. goto error;
  642. /* Setup the state */
  643. state->config = config;
  644. state->i2c = i2c;
  645. /* Create dvb_frontend */
  646. switch (config->demod_chip) {
  647. case LGDT3302:
  648. memcpy(&state->frontend.ops, &lgdt3302_ops, sizeof(struct dvb_frontend_ops));
  649. break;
  650. case LGDT3303:
  651. memcpy(&state->frontend.ops, &lgdt3303_ops, sizeof(struct dvb_frontend_ops));
  652. break;
  653. default:
  654. goto error;
  655. }
  656. state->frontend.demodulator_priv = state;
  657. /* Verify communication with demod chip */
  658. if (i2c_read_demod_bytes(state, 2, buf, 1))
  659. goto error;
  660. state->current_frequency = -1;
  661. state->current_modulation = -1;
  662. return &state->frontend;
  663. error:
  664. kfree(state);
  665. dprintk("%s: ERROR\n",__func__);
  666. return NULL;
  667. }
  668. static struct dvb_frontend_ops lgdt3302_ops = {
  669. .info = {
  670. .name= "LG Electronics LGDT3302 VSB/QAM Frontend",
  671. .type = FE_ATSC,
  672. .frequency_min= 54000000,
  673. .frequency_max= 858000000,
  674. .frequency_stepsize= 62500,
  675. .symbol_rate_min = 5056941, /* QAM 64 */
  676. .symbol_rate_max = 10762000, /* VSB 8 */
  677. .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
  678. },
  679. .init = lgdt330x_init,
  680. .set_frontend = lgdt330x_set_parameters,
  681. .get_frontend = lgdt330x_get_frontend,
  682. .get_tune_settings = lgdt330x_get_tune_settings,
  683. .read_status = lgdt3302_read_status,
  684. .read_ber = lgdt330x_read_ber,
  685. .read_signal_strength = lgdt330x_read_signal_strength,
  686. .read_snr = lgdt3302_read_snr,
  687. .read_ucblocks = lgdt330x_read_ucblocks,
  688. .release = lgdt330x_release,
  689. };
  690. static struct dvb_frontend_ops lgdt3303_ops = {
  691. .info = {
  692. .name= "LG Electronics LGDT3303 VSB/QAM Frontend",
  693. .type = FE_ATSC,
  694. .frequency_min= 54000000,
  695. .frequency_max= 858000000,
  696. .frequency_stepsize= 62500,
  697. .symbol_rate_min = 5056941, /* QAM 64 */
  698. .symbol_rate_max = 10762000, /* VSB 8 */
  699. .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
  700. },
  701. .init = lgdt330x_init,
  702. .set_frontend = lgdt330x_set_parameters,
  703. .get_frontend = lgdt330x_get_frontend,
  704. .get_tune_settings = lgdt330x_get_tune_settings,
  705. .read_status = lgdt3303_read_status,
  706. .read_ber = lgdt330x_read_ber,
  707. .read_signal_strength = lgdt330x_read_signal_strength,
  708. .read_snr = lgdt3303_read_snr,
  709. .read_ucblocks = lgdt330x_read_ucblocks,
  710. .release = lgdt330x_release,
  711. };
  712. MODULE_DESCRIPTION("LGDT330X (ATSC 8VSB & ITU-T J.83 AnnexB 64/256 QAM) Demodulator Driver");
  713. MODULE_AUTHOR("Wilson Michaels");
  714. MODULE_LICENSE("GPL");
  715. EXPORT_SYMBOL(lgdt330x_attach);
  716. /*
  717. * Local variables:
  718. * c-basic-offset: 8
  719. * End:
  720. */