lgdt330x.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. *
  29. * TODO:
  30. * signal strength always returns 0.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/init.h>
  37. #include <linux/delay.h>
  38. #include <asm/byteorder.h>
  39. #include "dvb_frontend.h"
  40. #include "lgdt330x_priv.h"
  41. #include "lgdt330x.h"
  42. static int debug = 0;
  43. module_param(debug, int, 0644);
  44. MODULE_PARM_DESC(debug,"Turn on/off lgdt330x frontend debugging (default:off).");
  45. #define dprintk(args...) \
  46. do { \
  47. if (debug) printk(KERN_DEBUG "lgdt330x: " args); \
  48. } while (0)
  49. struct lgdt330x_state
  50. {
  51. struct i2c_adapter* i2c;
  52. struct dvb_frontend_ops ops;
  53. /* Configuration settings */
  54. const struct lgdt330x_config* config;
  55. struct dvb_frontend frontend;
  56. /* Demodulator private data */
  57. fe_modulation_t current_modulation;
  58. /* Tuner private data */
  59. u32 current_frequency;
  60. };
  61. static int i2c_write_demod_bytes (struct lgdt330x_state* state,
  62. u8 *buf, /* data bytes to send */
  63. int len /* number of bytes to send */ )
  64. {
  65. struct i2c_msg msg =
  66. { .addr = state->config->demod_address,
  67. .flags = 0,
  68. .buf = buf,
  69. .len = 2 };
  70. int i;
  71. int err;
  72. for (i=0; i<len-1; i+=2){
  73. if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
  74. printk(KERN_WARNING "lgdt330x: %s error (addr %02x <- %02x, err = %i)\n", __FUNCTION__, msg.buf[0], msg.buf[1], err);
  75. if (err < 0)
  76. return err;
  77. else
  78. return -EREMOTEIO;
  79. }
  80. msg.buf += 2;
  81. }
  82. return 0;
  83. }
  84. /*
  85. * This routine writes the register (reg) to the demod bus
  86. * then reads the data returned for (len) bytes.
  87. */
  88. static u8 i2c_read_demod_bytes (struct lgdt330x_state* state,
  89. enum I2C_REG reg, u8* buf, int len)
  90. {
  91. u8 wr [] = { reg };
  92. struct i2c_msg msg [] = {
  93. { .addr = state->config->demod_address,
  94. .flags = 0, .buf = wr, .len = 1 },
  95. { .addr = state->config->demod_address,
  96. .flags = I2C_M_RD, .buf = buf, .len = len },
  97. };
  98. int ret;
  99. ret = i2c_transfer(state->i2c, msg, 2);
  100. if (ret != 2) {
  101. printk(KERN_WARNING "lgdt330x: %s: addr 0x%02x select 0x%02x error (ret == %i)\n", __FUNCTION__, state->config->demod_address, reg, ret);
  102. } else {
  103. ret = 0;
  104. }
  105. return ret;
  106. }
  107. /* Software reset */
  108. static int lgdt3302_SwReset(struct lgdt330x_state* state)
  109. {
  110. u8 ret;
  111. u8 reset[] = {
  112. IRQ_MASK,
  113. 0x00 /* bit 6 is active low software reset
  114. * bits 5-0 are 1 to mask interrupts */
  115. };
  116. ret = i2c_write_demod_bytes(state,
  117. reset, sizeof(reset));
  118. if (ret == 0) {
  119. /* force reset high (inactive) and unmask interrupts */
  120. reset[1] = 0x7f;
  121. ret = i2c_write_demod_bytes(state,
  122. reset, sizeof(reset));
  123. }
  124. return ret;
  125. }
  126. static int lgdt3303_SwReset(struct lgdt330x_state* state)
  127. {
  128. u8 ret;
  129. u8 reset[] = {
  130. 0x02,
  131. 0x00 /* bit 0 is active low software reset */
  132. };
  133. ret = i2c_write_demod_bytes(state,
  134. reset, sizeof(reset));
  135. if (ret == 0) {
  136. /* force reset high (inactive) */
  137. reset[1] = 0x01;
  138. ret = i2c_write_demod_bytes(state,
  139. reset, sizeof(reset));
  140. }
  141. return ret;
  142. }
  143. static int lgdt330x_SwReset(struct lgdt330x_state* state)
  144. {
  145. switch (state->config->demod_chip) {
  146. case LGDT3302:
  147. return lgdt3302_SwReset(state);
  148. case LGDT3303:
  149. return lgdt3303_SwReset(state);
  150. default:
  151. return -ENODEV;
  152. }
  153. }
  154. static int lgdt330x_init(struct dvb_frontend* fe)
  155. {
  156. /* Hardware reset is done using gpio[0] of cx23880x chip.
  157. * I'd like to do it here, but don't know how to find chip address.
  158. * cx88-cards.c arranges for the reset bit to be inactive (high).
  159. * Maybe there needs to be a callable function in cx88-core or
  160. * the caller of this function needs to do it. */
  161. /*
  162. * Array of byte pairs <address, value>
  163. * to initialize each different chip
  164. */
  165. static u8 lgdt3302_init_data[] = {
  166. /* Use 50MHz parameter values from spec sheet since xtal is 50 */
  167. /* Change the value of NCOCTFV[25:0] of carrier
  168. recovery center frequency register */
  169. VSB_CARRIER_FREQ0, 0x00,
  170. VSB_CARRIER_FREQ1, 0x87,
  171. VSB_CARRIER_FREQ2, 0x8e,
  172. VSB_CARRIER_FREQ3, 0x01,
  173. /* Change the TPCLK pin polarity
  174. data is valid on falling clock */
  175. DEMUX_CONTROL, 0xfb,
  176. /* Change the value of IFBW[11:0] of
  177. AGC IF/RF loop filter bandwidth register */
  178. AGC_RF_BANDWIDTH0, 0x40,
  179. AGC_RF_BANDWIDTH1, 0x93,
  180. AGC_RF_BANDWIDTH2, 0x00,
  181. /* Change the value of bit 6, 'nINAGCBY' and
  182. 'NSSEL[1:0] of ACG function control register 2 */
  183. AGC_FUNC_CTRL2, 0xc6,
  184. /* Change the value of bit 6 'RFFIX'
  185. of AGC function control register 3 */
  186. AGC_FUNC_CTRL3, 0x40,
  187. /* Set the value of 'INLVTHD' register 0x2a/0x2c
  188. to 0x7fe */
  189. AGC_DELAY0, 0x07,
  190. AGC_DELAY2, 0xfe,
  191. /* Change the value of IAGCBW[15:8]
  192. of inner AGC loop filter bandwith */
  193. AGC_LOOP_BANDWIDTH0, 0x08,
  194. AGC_LOOP_BANDWIDTH1, 0x9a
  195. };
  196. static u8 lgdt3303_init_data[] = {
  197. 0x4c, 0x14
  198. };
  199. struct lgdt330x_state* state = fe->demodulator_priv;
  200. char *chip_name;
  201. int err;
  202. switch (state->config->demod_chip) {
  203. case LGDT3302:
  204. chip_name = "LGDT3302";
  205. err = i2c_write_demod_bytes(state, lgdt3302_init_data,
  206. sizeof(lgdt3302_init_data));
  207. break;
  208. case LGDT3303:
  209. chip_name = "LGDT3303";
  210. err = i2c_write_demod_bytes(state, lgdt3303_init_data,
  211. sizeof(lgdt3303_init_data));
  212. break;
  213. default:
  214. chip_name = "undefined";
  215. printk (KERN_WARNING "Only LGDT3302 and LGDT3303 are supported chips.\n");
  216. err = -ENODEV;
  217. }
  218. dprintk("%s entered as %s\n", __FUNCTION__, chip_name);
  219. if (err < 0)
  220. return err;
  221. return lgdt330x_SwReset(state);
  222. }
  223. static int lgdt330x_read_ber(struct dvb_frontend* fe, u32* ber)
  224. {
  225. *ber = 0; /* Not supplied by the demod chips */
  226. return 0;
  227. }
  228. static int lgdt330x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  229. {
  230. struct lgdt330x_state* state = fe->demodulator_priv;
  231. int err;
  232. u8 buf[2];
  233. switch (state->config->demod_chip) {
  234. case LGDT3302:
  235. err = i2c_read_demod_bytes(state, LGDT3302_PACKET_ERR_COUNTER1,
  236. buf, sizeof(buf));
  237. break;
  238. case LGDT3303:
  239. err = i2c_read_demod_bytes(state, LGDT3303_PACKET_ERR_COUNTER1,
  240. buf, sizeof(buf));
  241. break;
  242. default:
  243. printk(KERN_WARNING
  244. "Only LGDT3302 and LGDT3303 are supported chips.\n");
  245. err = -ENODEV;
  246. }
  247. *ucblocks = (buf[0] << 8) | buf[1];
  248. return 0;
  249. }
  250. static int lgdt330x_set_parameters(struct dvb_frontend* fe,
  251. struct dvb_frontend_parameters *param)
  252. {
  253. /*
  254. * Array of byte pairs <address, value>
  255. * to initialize 8VSB for lgdt3303 chip 50 MHz IF
  256. */
  257. static u8 lgdt3303_8vsb_44_data[] = {
  258. 0x04, 0x00,
  259. 0x0d, 0x40,
  260. 0x0e, 0x87,
  261. 0x0f, 0x8e,
  262. 0x10, 0x01,
  263. 0x47, 0x8b };
  264. /*
  265. * Array of byte pairs <address, value>
  266. * to initialize QAM for lgdt3303 chip
  267. */
  268. static u8 lgdt3303_qam_data[] = {
  269. 0x04, 0x00,
  270. 0x0d, 0x00,
  271. 0x0e, 0x00,
  272. 0x0f, 0x00,
  273. 0x10, 0x00,
  274. 0x51, 0x63,
  275. 0x47, 0x66,
  276. 0x48, 0x66,
  277. 0x4d, 0x1a,
  278. 0x49, 0x08,
  279. 0x4a, 0x9b };
  280. struct lgdt330x_state* state = fe->demodulator_priv;
  281. static u8 top_ctrl_cfg[] = { TOP_CONTROL, 0x03 };
  282. int err;
  283. /* Change only if we are actually changing the modulation */
  284. if (state->current_modulation != param->u.vsb.modulation) {
  285. switch(param->u.vsb.modulation) {
  286. case VSB_8:
  287. dprintk("%s: VSB_8 MODE\n", __FUNCTION__);
  288. /* Select VSB mode */
  289. top_ctrl_cfg[1] = 0x03;
  290. /* Select ANT connector if supported by card */
  291. if (state->config->pll_rf_set)
  292. state->config->pll_rf_set(fe, 1);
  293. if (state->config->demod_chip == LGDT3303) {
  294. err = i2c_write_demod_bytes(state, lgdt3303_8vsb_44_data,
  295. sizeof(lgdt3303_8vsb_44_data));
  296. }
  297. break;
  298. case QAM_64:
  299. dprintk("%s: QAM_64 MODE\n", __FUNCTION__);
  300. /* Select QAM_64 mode */
  301. top_ctrl_cfg[1] = 0x00;
  302. /* Select CABLE connector if supported by card */
  303. if (state->config->pll_rf_set)
  304. state->config->pll_rf_set(fe, 0);
  305. if (state->config->demod_chip == LGDT3303) {
  306. err = i2c_write_demod_bytes(state, lgdt3303_qam_data,
  307. sizeof(lgdt3303_qam_data));
  308. }
  309. break;
  310. case QAM_256:
  311. dprintk("%s: QAM_256 MODE\n", __FUNCTION__);
  312. /* Select QAM_256 mode */
  313. top_ctrl_cfg[1] = 0x01;
  314. /* Select CABLE connector if supported by card */
  315. if (state->config->pll_rf_set)
  316. state->config->pll_rf_set(fe, 0);
  317. if (state->config->demod_chip == LGDT3303) {
  318. err = i2c_write_demod_bytes(state, lgdt3303_qam_data,
  319. sizeof(lgdt3303_qam_data));
  320. }
  321. break;
  322. default:
  323. printk(KERN_WARNING "lgdt330x: %s: Modulation type(%d) UNSUPPORTED\n", __FUNCTION__, param->u.vsb.modulation);
  324. return -1;
  325. }
  326. /*
  327. * select serial or parallel MPEG harware interface
  328. * Serial: 0x04 for LGDT3302 or 0x40 for LGDT3303
  329. * Parallel: 0x00
  330. */
  331. top_ctrl_cfg[1] |= state->config->serial_mpeg;
  332. /* Select the requested mode */
  333. i2c_write_demod_bytes(state, top_ctrl_cfg,
  334. sizeof(top_ctrl_cfg));
  335. state->config->set_ts_params(fe, 0);
  336. state->current_modulation = param->u.vsb.modulation;
  337. }
  338. /* Change only if we are actually changing the channel */
  339. if (state->current_frequency != param->frequency) {
  340. /* Tune to the new frequency */
  341. state->config->pll_set(fe, param);
  342. /* Keep track of the new frequency */
  343. state->current_frequency = param->frequency;
  344. }
  345. lgdt330x_SwReset(state);
  346. return 0;
  347. }
  348. static int lgdt330x_get_frontend(struct dvb_frontend* fe,
  349. struct dvb_frontend_parameters* param)
  350. {
  351. struct lgdt330x_state *state = fe->demodulator_priv;
  352. param->frequency = state->current_frequency;
  353. return 0;
  354. }
  355. static int lgdt3302_read_status(struct dvb_frontend* fe, fe_status_t* status)
  356. {
  357. struct lgdt330x_state* state = fe->demodulator_priv;
  358. u8 buf[3];
  359. *status = 0; /* Reset status result */
  360. /* AGC status register */
  361. i2c_read_demod_bytes(state, AGC_STATUS, buf, 1);
  362. dprintk("%s: AGC_STATUS = 0x%02x\n", __FUNCTION__, buf[0]);
  363. if ((buf[0] & 0x0c) == 0x8){
  364. /* Test signal does not exist flag */
  365. /* as well as the AGC lock flag. */
  366. *status |= FE_HAS_SIGNAL;
  367. } else {
  368. /* Without a signal all other status bits are meaningless */
  369. return 0;
  370. }
  371. /*
  372. * You must set the Mask bits to 1 in the IRQ_MASK in order
  373. * to see that status bit in the IRQ_STATUS register.
  374. * This is done in SwReset();
  375. */
  376. /* signal status */
  377. i2c_read_demod_bytes(state, TOP_CONTROL, buf, sizeof(buf));
  378. dprintk("%s: TOP_CONTROL = 0x%02x, IRO_MASK = 0x%02x, IRQ_STATUS = 0x%02x\n", __FUNCTION__, buf[0], buf[1], buf[2]);
  379. /* sync status */
  380. if ((buf[2] & 0x03) == 0x01) {
  381. *status |= FE_HAS_SYNC;
  382. }
  383. /* FEC error status */
  384. if ((buf[2] & 0x0c) == 0x08) {
  385. *status |= FE_HAS_LOCK;
  386. *status |= FE_HAS_VITERBI;
  387. }
  388. /* Carrier Recovery Lock Status Register */
  389. i2c_read_demod_bytes(state, CARRIER_LOCK, buf, 1);
  390. dprintk("%s: CARRIER_LOCK = 0x%02x\n", __FUNCTION__, buf[0]);
  391. switch (state->current_modulation) {
  392. case QAM_256:
  393. case QAM_64:
  394. /* Need to undestand why there are 3 lock levels here */
  395. if ((buf[0] & 0x07) == 0x07)
  396. *status |= FE_HAS_CARRIER;
  397. break;
  398. case VSB_8:
  399. if ((buf[0] & 0x80) == 0x80)
  400. *status |= FE_HAS_CARRIER;
  401. break;
  402. default:
  403. printk("KERN_WARNING lgdt330x: %s: Modulation set to unsupported value\n", __FUNCTION__);
  404. }
  405. return 0;
  406. }
  407. static int lgdt3303_read_status(struct dvb_frontend* fe, fe_status_t* status)
  408. {
  409. struct lgdt330x_state* state = fe->demodulator_priv;
  410. int err;
  411. u8 buf[3];
  412. *status = 0; /* Reset status result */
  413. /* lgdt3303 AGC status register */
  414. err = i2c_read_demod_bytes(state, 0x58, buf, 1);
  415. if (err < 0)
  416. return err;
  417. dprintk("%s: AGC_STATUS = 0x%02x\n", __FUNCTION__, buf[0]);
  418. if ((buf[0] & 0x21) == 0x01){
  419. /* Test input signal does not exist flag */
  420. /* as well as the AGC lock flag. */
  421. *status |= FE_HAS_SIGNAL;
  422. } else {
  423. /* Without a signal all other status bits are meaningless */
  424. return 0;
  425. }
  426. /* Carrier Recovery Lock Status Register */
  427. i2c_read_demod_bytes(state, CARRIER_LOCK, buf, 1);
  428. dprintk("%s: CARRIER_LOCK = 0x%02x\n", __FUNCTION__, buf[0]);
  429. switch (state->current_modulation) {
  430. case QAM_256:
  431. case QAM_64:
  432. /* Need to undestand why there are 3 lock levels here */
  433. if ((buf[0] & 0x07) == 0x07)
  434. *status |= FE_HAS_CARRIER;
  435. else
  436. break;
  437. i2c_read_demod_bytes(state, 0x8a, buf, 1);
  438. if ((buf[0] & 0x04) == 0x04)
  439. *status |= FE_HAS_SYNC;
  440. if ((buf[0] & 0x01) == 0x01)
  441. *status |= FE_HAS_LOCK;
  442. if ((buf[0] & 0x08) == 0x08)
  443. *status |= FE_HAS_VITERBI;
  444. break;
  445. case VSB_8:
  446. if ((buf[0] & 0x80) == 0x80)
  447. *status |= FE_HAS_CARRIER;
  448. else
  449. break;
  450. i2c_read_demod_bytes(state, 0x38, buf, 1);
  451. if ((buf[0] & 0x02) == 0x00)
  452. *status |= FE_HAS_SYNC;
  453. if ((buf[0] & 0x01) == 0x01) {
  454. *status |= FE_HAS_LOCK;
  455. *status |= FE_HAS_VITERBI;
  456. }
  457. break;
  458. default:
  459. printk("KERN_WARNING lgdt330x: %s: Modulation set to unsupported value\n", __FUNCTION__);
  460. }
  461. return 0;
  462. }
  463. static int lgdt330x_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  464. {
  465. /* not directly available. */
  466. *strength = 0;
  467. return 0;
  468. }
  469. static int lgdt3302_read_snr(struct dvb_frontend* fe, u16* snr)
  470. {
  471. #ifdef SNR_IN_DB
  472. /*
  473. * Spec sheet shows formula for SNR_EQ = 10 log10(25 * 24**2 / noise)
  474. * and SNR_PH = 10 log10(25 * 32**2 / noise) for equalizer and phase tracker
  475. * respectively. The following tables are built on these formulas.
  476. * The usual definition is SNR = 20 log10(signal/noise)
  477. * If the specification is wrong the value retuned is 1/2 the actual SNR in db.
  478. *
  479. * This table is a an ordered list of noise values computed by the
  480. * formula from the spec sheet such that the index into the table
  481. * starting at 43 or 45 is the SNR value in db. There are duplicate noise
  482. * value entries at the beginning because the SNR varies more than
  483. * 1 db for a change of 1 digit in noise at very small values of noise.
  484. *
  485. * Examples from SNR_EQ table:
  486. * noise SNR
  487. * 0 43
  488. * 1 42
  489. * 2 39
  490. * 3 37
  491. * 4 36
  492. * 5 35
  493. * 6 34
  494. * 7 33
  495. * 8 33
  496. * 9 32
  497. * 10 32
  498. * 11 31
  499. * 12 31
  500. * 13 30
  501. */
  502. static const u32 SNR_EQ[] =
  503. { 1, 2, 2, 2, 3, 3, 4, 4, 5, 7,
  504. 9, 11, 13, 17, 21, 26, 33, 41, 52, 65,
  505. 81, 102, 129, 162, 204, 257, 323, 406, 511, 644,
  506. 810, 1020, 1284, 1616, 2035, 2561, 3224, 4059, 5110, 6433,
  507. 8098, 10195, 12835, 16158, 20341, 25608, 32238, 40585, 51094, 64323,
  508. 80978, 101945, 128341, 161571, 203406, 256073, 0x40000
  509. };
  510. static const u32 SNR_PH[] =
  511. { 1, 2, 2, 2, 3, 3, 4, 5, 6, 8,
  512. 10, 12, 15, 19, 23, 29, 37, 46, 58, 73,
  513. 91, 115, 144, 182, 229, 288, 362, 456, 574, 722,
  514. 909, 1144, 1440, 1813, 2282, 2873, 3617, 4553, 5732, 7216,
  515. 9084, 11436, 14396, 18124, 22817, 28724, 36161, 45524, 57312, 72151,
  516. 90833, 114351, 143960, 181235, 228161, 0x080000
  517. };
  518. static u8 buf[5];/* read data buffer */
  519. static u32 noise; /* noise value */
  520. static u32 snr_db; /* index into SNR_EQ[] */
  521. struct lgdt330x_state* state = (struct lgdt330x_state*) fe->demodulator_priv;
  522. /* read both equalizer and phase tracker noise data */
  523. i2c_read_demod_bytes(state, EQPH_ERR0, buf, sizeof(buf));
  524. if (state->current_modulation == VSB_8) {
  525. /* Equalizer Mean-Square Error Register for VSB */
  526. noise = ((buf[0] & 7) << 16) | (buf[1] << 8) | buf[2];
  527. /*
  528. * Look up noise value in table.
  529. * A better search algorithm could be used...
  530. * watch out there are duplicate entries.
  531. */
  532. for (snr_db = 0; snr_db < sizeof(SNR_EQ); snr_db++) {
  533. if (noise < SNR_EQ[snr_db]) {
  534. *snr = 43 - snr_db;
  535. break;
  536. }
  537. }
  538. } else {
  539. /* Phase Tracker Mean-Square Error Register for QAM */
  540. noise = ((buf[0] & 7<<3) << 13) | (buf[3] << 8) | buf[4];
  541. /* Look up noise value in table. */
  542. for (snr_db = 0; snr_db < sizeof(SNR_PH); snr_db++) {
  543. if (noise < SNR_PH[snr_db]) {
  544. *snr = 45 - snr_db;
  545. break;
  546. }
  547. }
  548. }
  549. #else
  550. /* Return the raw noise value */
  551. static u8 buf[5];/* read data buffer */
  552. static u32 noise; /* noise value */
  553. struct lgdt330x_state* state = (struct lgdt330x_state*) fe->demodulator_priv;
  554. /* read both equalizer and pase tracker noise data */
  555. i2c_read_demod_bytes(state, EQPH_ERR0, buf, sizeof(buf));
  556. if (state->current_modulation == VSB_8) {
  557. /* Phase Tracker Mean-Square Error Register for VSB */
  558. noise = ((buf[0] & 7<<3) << 13) | (buf[3] << 8) | buf[4];
  559. } else {
  560. /* Carrier Recovery Mean-Square Error for QAM */
  561. i2c_read_demod_bytes(state, 0x1a, buf, 2);
  562. noise = ((buf[0] & 3) << 8) | buf[1];
  563. }
  564. /* Small values for noise mean signal is better so invert noise */
  565. *snr = ~noise;
  566. #endif
  567. dprintk("%s: noise = 0x%05x, snr = %idb\n",__FUNCTION__, noise, *snr);
  568. return 0;
  569. }
  570. static int lgdt3303_read_snr(struct dvb_frontend* fe, u16* snr)
  571. {
  572. /* Return the raw noise value */
  573. static u8 buf[5];/* read data buffer */
  574. static u32 noise; /* noise value */
  575. struct lgdt330x_state* state = (struct lgdt330x_state*) fe->demodulator_priv;
  576. if (state->current_modulation == VSB_8) {
  577. /* Phase Tracker Mean-Square Error Register for VSB */
  578. noise = ((buf[0] & 7) << 16) | (buf[3] << 8) | buf[4];
  579. } else {
  580. /* Carrier Recovery Mean-Square Error for QAM */
  581. i2c_read_demod_bytes(state, 0x1a, buf, 2);
  582. noise = (buf[0] << 8) | buf[1];
  583. }
  584. /* Small values for noise mean signal is better so invert noise */
  585. *snr = ~noise;
  586. dprintk("%s: noise = 0x%05x, snr = %idb\n",__FUNCTION__, noise, *snr);
  587. return 0;
  588. }
  589. static int lgdt330x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fe_tune_settings)
  590. {
  591. /* I have no idea about this - it may not be needed */
  592. fe_tune_settings->min_delay_ms = 500;
  593. fe_tune_settings->step_size = 0;
  594. fe_tune_settings->max_drift = 0;
  595. return 0;
  596. }
  597. static void lgdt330x_release(struct dvb_frontend* fe)
  598. {
  599. struct lgdt330x_state* state = (struct lgdt330x_state*) fe->demodulator_priv;
  600. kfree(state);
  601. }
  602. static struct dvb_frontend_ops lgdt3302_ops;
  603. static struct dvb_frontend_ops lgdt3303_ops;
  604. struct dvb_frontend* lgdt330x_attach(const struct lgdt330x_config* config,
  605. struct i2c_adapter* i2c)
  606. {
  607. struct lgdt330x_state* state = NULL;
  608. u8 buf[1];
  609. /* Allocate memory for the internal state */
  610. state = (struct lgdt330x_state*) kmalloc(sizeof(struct lgdt330x_state), GFP_KERNEL);
  611. if (state == NULL)
  612. goto error;
  613. memset(state,0,sizeof(*state));
  614. /* Setup the state */
  615. state->config = config;
  616. state->i2c = i2c;
  617. switch (config->demod_chip) {
  618. case LGDT3302:
  619. memcpy(&state->ops, &lgdt3302_ops, sizeof(struct dvb_frontend_ops));
  620. break;
  621. case LGDT3303:
  622. memcpy(&state->ops, &lgdt3303_ops, sizeof(struct dvb_frontend_ops));
  623. break;
  624. default:
  625. goto error;
  626. }
  627. /* Verify communication with demod chip */
  628. if (i2c_read_demod_bytes(state, 2, buf, 1))
  629. goto error;
  630. state->current_frequency = -1;
  631. state->current_modulation = -1;
  632. /* Create dvb_frontend */
  633. state->frontend.ops = &state->ops;
  634. state->frontend.demodulator_priv = state;
  635. return &state->frontend;
  636. error:
  637. if (state)
  638. kfree(state);
  639. dprintk("%s: ERROR\n",__FUNCTION__);
  640. return NULL;
  641. }
  642. static struct dvb_frontend_ops lgdt3302_ops = {
  643. .info = {
  644. .name= "LG Electronics LGDT3302 VSB/QAM Frontend",
  645. .type = FE_ATSC,
  646. .frequency_min= 54000000,
  647. .frequency_max= 858000000,
  648. .frequency_stepsize= 62500,
  649. /* Symbol rate is for all VSB modes need to check QAM */
  650. .symbol_rate_min = 10762000,
  651. .symbol_rate_max = 10762000,
  652. .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
  653. },
  654. .init = lgdt330x_init,
  655. .set_frontend = lgdt330x_set_parameters,
  656. .get_frontend = lgdt330x_get_frontend,
  657. .get_tune_settings = lgdt330x_get_tune_settings,
  658. .read_status = lgdt3302_read_status,
  659. .read_ber = lgdt330x_read_ber,
  660. .read_signal_strength = lgdt330x_read_signal_strength,
  661. .read_snr = lgdt3302_read_snr,
  662. .read_ucblocks = lgdt330x_read_ucblocks,
  663. .release = lgdt330x_release,
  664. };
  665. static struct dvb_frontend_ops lgdt3303_ops = {
  666. .info = {
  667. .name= "LG Electronics LGDT3303 VSB/QAM Frontend",
  668. .type = FE_ATSC,
  669. .frequency_min= 54000000,
  670. .frequency_max= 858000000,
  671. .frequency_stepsize= 62500,
  672. /* Symbol rate is for all VSB modes need to check QAM */
  673. .symbol_rate_min = 10762000,
  674. .symbol_rate_max = 10762000,
  675. .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
  676. },
  677. .init = lgdt330x_init,
  678. .set_frontend = lgdt330x_set_parameters,
  679. .get_frontend = lgdt330x_get_frontend,
  680. .get_tune_settings = lgdt330x_get_tune_settings,
  681. .read_status = lgdt3303_read_status,
  682. .read_ber = lgdt330x_read_ber,
  683. .read_signal_strength = lgdt330x_read_signal_strength,
  684. .read_snr = lgdt3303_read_snr,
  685. .read_ucblocks = lgdt330x_read_ucblocks,
  686. .release = lgdt330x_release,
  687. };
  688. MODULE_DESCRIPTION("LGDT330X (ATSC 8VSB & ITU-T J.83 AnnexB 64/256 QAM) Demodulator Driver");
  689. MODULE_AUTHOR("Wilson Michaels");
  690. MODULE_LICENSE("GPL");
  691. EXPORT_SYMBOL(lgdt330x_attach);
  692. /*
  693. * Local variables:
  694. * c-basic-offset: 8
  695. * End:
  696. */