lgdt330x.c 22 KB

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