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