cx22702.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. Conexant 22702 DVB OFDM demodulator driver
  3. based on:
  4. Alps TDMB7 DVB OFDM demodulator driver
  5. Copyright (C) 2001-2002 Convergence Integrated Media GmbH
  6. Holger Waechtler <holger@convergence.de>
  7. Copyright (C) 2004 Steven Toth <stoth@hauppauge.com>
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  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. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/string.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include "dvb_frontend.h"
  27. #include "dvb-pll.h"
  28. #include "cx22702.h"
  29. struct cx22702_state {
  30. struct i2c_adapter* i2c;
  31. struct dvb_frontend_ops ops;
  32. /* configuration settings */
  33. const struct cx22702_config* config;
  34. struct dvb_frontend frontend;
  35. /* previous uncorrected block counter */
  36. u8 prevUCBlocks;
  37. };
  38. static int debug = 0;
  39. #define dprintk if (debug) printk
  40. /* Register values to initialise the demod */
  41. static u8 init_tab [] = {
  42. 0x00, 0x00, /* Stop aquisition */
  43. 0x0B, 0x06,
  44. 0x09, 0x01,
  45. 0x0D, 0x41,
  46. 0x16, 0x32,
  47. 0x20, 0x0A,
  48. 0x21, 0x17,
  49. 0x24, 0x3e,
  50. 0x26, 0xff,
  51. 0x27, 0x10,
  52. 0x28, 0x00,
  53. 0x29, 0x00,
  54. 0x2a, 0x10,
  55. 0x2b, 0x00,
  56. 0x2c, 0x10,
  57. 0x2d, 0x00,
  58. 0x48, 0xd4,
  59. 0x49, 0x56,
  60. 0x6b, 0x1e,
  61. 0xc8, 0x02,
  62. 0xf9, 0x00,
  63. 0xfa, 0x00,
  64. 0xfb, 0x00,
  65. 0xfc, 0x00,
  66. 0xfd, 0x00,
  67. };
  68. static int cx22702_writereg (struct cx22702_state* state, u8 reg, u8 data)
  69. {
  70. int ret;
  71. u8 buf [] = { reg, data };
  72. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
  73. ret = i2c_transfer(state->i2c, &msg, 1);
  74. if (ret != 1)
  75. printk("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
  76. __FUNCTION__, reg, data, ret);
  77. return (ret != 1) ? -1 : 0;
  78. }
  79. static u8 cx22702_readreg (struct cx22702_state* state, u8 reg)
  80. {
  81. int ret;
  82. u8 b0 [] = { reg };
  83. u8 b1 [] = { 0 };
  84. struct i2c_msg msg [] = {
  85. { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
  86. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
  87. ret = i2c_transfer(state->i2c, msg, 2);
  88. if (ret != 2)
  89. printk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
  90. return b1[0];
  91. }
  92. static int cx22702_set_inversion (struct cx22702_state *state, int inversion)
  93. {
  94. u8 val;
  95. switch (inversion) {
  96. case INVERSION_AUTO:
  97. return -EOPNOTSUPP;
  98. case INVERSION_ON:
  99. val = cx22702_readreg (state, 0x0C);
  100. return cx22702_writereg (state, 0x0C, val | 0x01);
  101. case INVERSION_OFF:
  102. val = cx22702_readreg (state, 0x0C);
  103. return cx22702_writereg (state, 0x0C, val & 0xfe);
  104. default:
  105. return -EINVAL;
  106. }
  107. }
  108. /* Retrieve the demod settings */
  109. static int cx22702_get_tps (struct cx22702_state *state, struct dvb_ofdm_parameters *p)
  110. {
  111. u8 val;
  112. /* Make sure the TPS regs are valid */
  113. if (!(cx22702_readreg(state, 0x0A) & 0x20))
  114. return -EAGAIN;
  115. val = cx22702_readreg (state, 0x01);
  116. switch( (val&0x18)>>3) {
  117. case 0: p->constellation = QPSK; break;
  118. case 1: p->constellation = QAM_16; break;
  119. case 2: p->constellation = QAM_64; break;
  120. }
  121. switch( val&0x07 ) {
  122. case 0: p->hierarchy_information = HIERARCHY_NONE; break;
  123. case 1: p->hierarchy_information = HIERARCHY_1; break;
  124. case 2: p->hierarchy_information = HIERARCHY_2; break;
  125. case 3: p->hierarchy_information = HIERARCHY_4; break;
  126. }
  127. val = cx22702_readreg (state, 0x02);
  128. switch( (val&0x38)>>3 ) {
  129. case 0: p->code_rate_HP = FEC_1_2; break;
  130. case 1: p->code_rate_HP = FEC_2_3; break;
  131. case 2: p->code_rate_HP = FEC_3_4; break;
  132. case 3: p->code_rate_HP = FEC_5_6; break;
  133. case 4: p->code_rate_HP = FEC_7_8; break;
  134. }
  135. switch( val&0x07 ) {
  136. case 0: p->code_rate_LP = FEC_1_2; break;
  137. case 1: p->code_rate_LP = FEC_2_3; break;
  138. case 2: p->code_rate_LP = FEC_3_4; break;
  139. case 3: p->code_rate_LP = FEC_5_6; break;
  140. case 4: p->code_rate_LP = FEC_7_8; break;
  141. }
  142. val = cx22702_readreg (state, 0x03);
  143. switch( (val&0x0c)>>2 ) {
  144. case 0: p->guard_interval = GUARD_INTERVAL_1_32; break;
  145. case 1: p->guard_interval = GUARD_INTERVAL_1_16; break;
  146. case 2: p->guard_interval = GUARD_INTERVAL_1_8; break;
  147. case 3: p->guard_interval = GUARD_INTERVAL_1_4; break;
  148. }
  149. switch( val&0x03 ) {
  150. case 0: p->transmission_mode = TRANSMISSION_MODE_2K; break;
  151. case 1: p->transmission_mode = TRANSMISSION_MODE_8K; break;
  152. }
  153. return 0;
  154. }
  155. static int cx22702_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  156. {
  157. struct cx22702_state* state = fe->demodulator_priv;
  158. dprintk ("%s(%d)\n", __FUNCTION__, enable);
  159. if (enable)
  160. return cx22702_writereg (state, 0x0D, cx22702_readreg(state, 0x0D) & 0xfe);
  161. else
  162. return cx22702_writereg (state, 0x0D, cx22702_readreg(state, 0x0D) | 1);
  163. }
  164. /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
  165. static int cx22702_set_tps (struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  166. {
  167. u8 val;
  168. struct cx22702_state* state = fe->demodulator_priv;
  169. /* set PLL */
  170. cx22702_i2c_gate_ctrl(fe, 1);
  171. if (state->config->pll_set) {
  172. state->config->pll_set(fe, p);
  173. } else if (state->config->pll_desc) {
  174. u8 pllbuf[4];
  175. struct i2c_msg msg = { .addr = state->config->pll_address,
  176. .buf = pllbuf, .len = 4 };
  177. dvb_pll_configure(state->config->pll_desc, pllbuf,
  178. p->frequency,
  179. p->u.ofdm.bandwidth);
  180. i2c_transfer(state->i2c, &msg, 1);
  181. } else {
  182. BUG();
  183. }
  184. cx22702_i2c_gate_ctrl(fe, 0);
  185. /* set inversion */
  186. cx22702_set_inversion (state, p->inversion);
  187. /* set bandwidth */
  188. switch(p->u.ofdm.bandwidth) {
  189. case BANDWIDTH_6_MHZ:
  190. cx22702_writereg(state, 0x0C, (cx22702_readreg(state, 0x0C) & 0xcf) | 0x20 );
  191. break;
  192. case BANDWIDTH_7_MHZ:
  193. cx22702_writereg(state, 0x0C, (cx22702_readreg(state, 0x0C) & 0xcf) | 0x10 );
  194. break;
  195. case BANDWIDTH_8_MHZ:
  196. cx22702_writereg(state, 0x0C, cx22702_readreg(state, 0x0C) &0xcf );
  197. break;
  198. default:
  199. dprintk ("%s: invalid bandwidth\n",__FUNCTION__);
  200. return -EINVAL;
  201. }
  202. p->u.ofdm.code_rate_LP = FEC_AUTO; //temp hack as manual not working
  203. /* use auto configuration? */
  204. if((p->u.ofdm.hierarchy_information==HIERARCHY_AUTO) ||
  205. (p->u.ofdm.constellation==QAM_AUTO) ||
  206. (p->u.ofdm.code_rate_HP==FEC_AUTO) ||
  207. (p->u.ofdm.code_rate_LP==FEC_AUTO) ||
  208. (p->u.ofdm.guard_interval==GUARD_INTERVAL_AUTO) ||
  209. (p->u.ofdm.transmission_mode==TRANSMISSION_MODE_AUTO) ) {
  210. /* TPS Source - use hardware driven values */
  211. cx22702_writereg(state, 0x06, 0x10);
  212. cx22702_writereg(state, 0x07, 0x9);
  213. cx22702_writereg(state, 0x08, 0xC1);
  214. cx22702_writereg(state, 0x0B, cx22702_readreg(state, 0x0B) & 0xfc );
  215. cx22702_writereg(state, 0x0C, (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40 );
  216. cx22702_writereg(state, 0x00, 0x01); /* Begin aquisition */
  217. dprintk("%s: Autodetecting\n",__FUNCTION__);
  218. return 0;
  219. }
  220. /* manually programmed values */
  221. val=0;
  222. switch(p->u.ofdm.constellation) {
  223. case QPSK: val = (val&0xe7); break;
  224. case QAM_16: val = (val&0xe7)|0x08; break;
  225. case QAM_64: val = (val&0xe7)|0x10; break;
  226. default:
  227. dprintk ("%s: invalid constellation\n",__FUNCTION__);
  228. return -EINVAL;
  229. }
  230. switch(p->u.ofdm.hierarchy_information) {
  231. case HIERARCHY_NONE: val = (val&0xf8); break;
  232. case HIERARCHY_1: val = (val&0xf8)|1; break;
  233. case HIERARCHY_2: val = (val&0xf8)|2; break;
  234. case HIERARCHY_4: val = (val&0xf8)|3; break;
  235. default:
  236. dprintk ("%s: invalid hierarchy\n",__FUNCTION__);
  237. return -EINVAL;
  238. }
  239. cx22702_writereg (state, 0x06, val);
  240. val=0;
  241. switch(p->u.ofdm.code_rate_HP) {
  242. case FEC_NONE:
  243. case FEC_1_2: val = (val&0xc7); break;
  244. case FEC_2_3: val = (val&0xc7)|0x08; break;
  245. case FEC_3_4: val = (val&0xc7)|0x10; break;
  246. case FEC_5_6: val = (val&0xc7)|0x18; break;
  247. case FEC_7_8: val = (val&0xc7)|0x20; break;
  248. default:
  249. dprintk ("%s: invalid code_rate_HP\n",__FUNCTION__);
  250. return -EINVAL;
  251. }
  252. switch(p->u.ofdm.code_rate_LP) {
  253. case FEC_NONE:
  254. case FEC_1_2: val = (val&0xf8); break;
  255. case FEC_2_3: val = (val&0xf8)|1; break;
  256. case FEC_3_4: val = (val&0xf8)|2; break;
  257. case FEC_5_6: val = (val&0xf8)|3; break;
  258. case FEC_7_8: val = (val&0xf8)|4; break;
  259. default:
  260. dprintk ("%s: invalid code_rate_LP\n",__FUNCTION__);
  261. return -EINVAL;
  262. }
  263. cx22702_writereg (state, 0x07, val);
  264. val=0;
  265. switch(p->u.ofdm.guard_interval) {
  266. case GUARD_INTERVAL_1_32: val = (val&0xf3); break;
  267. case GUARD_INTERVAL_1_16: val = (val&0xf3)|0x04; break;
  268. case GUARD_INTERVAL_1_8: val = (val&0xf3)|0x08; break;
  269. case GUARD_INTERVAL_1_4: val = (val&0xf3)|0x0c; break;
  270. default:
  271. dprintk ("%s: invalid guard_interval\n",__FUNCTION__);
  272. return -EINVAL;
  273. }
  274. switch(p->u.ofdm.transmission_mode) {
  275. case TRANSMISSION_MODE_2K: val = (val&0xfc); break;
  276. case TRANSMISSION_MODE_8K: val = (val&0xfc)|1; break;
  277. default:
  278. dprintk ("%s: invalid transmission_mode\n",__FUNCTION__);
  279. return -EINVAL;
  280. }
  281. cx22702_writereg(state, 0x08, val);
  282. cx22702_writereg(state, 0x0B, (cx22702_readreg(state, 0x0B) & 0xfc) | 0x02 );
  283. cx22702_writereg(state, 0x0C, (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40 );
  284. /* Begin channel aquisition */
  285. cx22702_writereg(state, 0x00, 0x01);
  286. return 0;
  287. }
  288. /* Reset the demod hardware and reset all of the configuration registers
  289. to a default state. */
  290. static int cx22702_init (struct dvb_frontend* fe)
  291. {
  292. int i;
  293. struct cx22702_state* state = fe->demodulator_priv;
  294. cx22702_writereg (state, 0x00, 0x02);
  295. msleep(10);
  296. for (i=0; i<sizeof(init_tab); i+=2)
  297. cx22702_writereg (state, init_tab[i], init_tab[i+1]);
  298. cx22702_writereg (state, 0xf8, (state->config->output_mode << 1) & 0x02);
  299. /* init PLL */
  300. if (state->config->pll_init)
  301. state->config->pll_init(fe);
  302. cx22702_i2c_gate_ctrl(fe, 0);
  303. return 0;
  304. }
  305. static int cx22702_read_status(struct dvb_frontend* fe, fe_status_t* status)
  306. {
  307. struct cx22702_state* state = fe->demodulator_priv;
  308. u8 reg0A;
  309. u8 reg23;
  310. *status = 0;
  311. reg0A = cx22702_readreg (state, 0x0A);
  312. reg23 = cx22702_readreg (state, 0x23);
  313. dprintk ("%s: status demod=0x%02x agc=0x%02x\n"
  314. ,__FUNCTION__,reg0A,reg23);
  315. if(reg0A & 0x10) {
  316. *status |= FE_HAS_LOCK;
  317. *status |= FE_HAS_VITERBI;
  318. *status |= FE_HAS_SYNC;
  319. }
  320. if(reg0A & 0x20)
  321. *status |= FE_HAS_CARRIER;
  322. if(reg23 < 0xf0)
  323. *status |= FE_HAS_SIGNAL;
  324. return 0;
  325. }
  326. static int cx22702_read_ber(struct dvb_frontend* fe, u32* ber)
  327. {
  328. struct cx22702_state* state = fe->demodulator_priv;
  329. if(cx22702_readreg (state, 0xE4) & 0x02) {
  330. /* Realtime statistics */
  331. *ber = (cx22702_readreg (state, 0xDE) & 0x7F) << 7
  332. | (cx22702_readreg (state, 0xDF)&0x7F);
  333. } else {
  334. /* Averagtine statistics */
  335. *ber = (cx22702_readreg (state, 0xDE) & 0x7F) << 7
  336. | cx22702_readreg (state, 0xDF);
  337. }
  338. return 0;
  339. }
  340. static int cx22702_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
  341. {
  342. struct cx22702_state* state = fe->demodulator_priv;
  343. *signal_strength = cx22702_readreg (state, 0x23);
  344. return 0;
  345. }
  346. static int cx22702_read_snr(struct dvb_frontend* fe, u16* snr)
  347. {
  348. struct cx22702_state* state = fe->demodulator_priv;
  349. u16 rs_ber=0;
  350. if(cx22702_readreg (state, 0xE4) & 0x02) {
  351. /* Realtime statistics */
  352. rs_ber = (cx22702_readreg (state, 0xDE) & 0x7F) << 7
  353. | (cx22702_readreg (state, 0xDF)& 0x7F);
  354. } else {
  355. /* Averagine statistics */
  356. rs_ber = (cx22702_readreg (state, 0xDE) & 0x7F) << 8
  357. | cx22702_readreg (state, 0xDF);
  358. }
  359. *snr = ~rs_ber;
  360. return 0;
  361. }
  362. static int cx22702_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  363. {
  364. struct cx22702_state* state = fe->demodulator_priv;
  365. u8 _ucblocks;
  366. /* RS Uncorrectable Packet Count then reset */
  367. _ucblocks = cx22702_readreg (state, 0xE3);
  368. if (state->prevUCBlocks < _ucblocks)
  369. *ucblocks = (_ucblocks - state->prevUCBlocks);
  370. else
  371. *ucblocks = state->prevUCBlocks - _ucblocks;
  372. state->prevUCBlocks = _ucblocks;
  373. return 0;
  374. }
  375. static int cx22702_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  376. {
  377. struct cx22702_state* state = fe->demodulator_priv;
  378. u8 reg0C = cx22702_readreg (state, 0x0C);
  379. p->inversion = reg0C & 0x1 ? INVERSION_ON : INVERSION_OFF;
  380. return cx22702_get_tps (state, &p->u.ofdm);
  381. }
  382. static int cx22702_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  383. {
  384. tune->min_delay_ms = 1000;
  385. return 0;
  386. }
  387. static void cx22702_release(struct dvb_frontend* fe)
  388. {
  389. struct cx22702_state* state = fe->demodulator_priv;
  390. kfree(state);
  391. }
  392. static struct dvb_frontend_ops cx22702_ops;
  393. struct dvb_frontend* cx22702_attach(const struct cx22702_config* config,
  394. struct i2c_adapter* i2c)
  395. {
  396. struct cx22702_state* state = NULL;
  397. /* allocate memory for the internal state */
  398. state = kmalloc(sizeof(struct cx22702_state), GFP_KERNEL);
  399. if (state == NULL)
  400. goto error;
  401. /* setup the state */
  402. state->config = config;
  403. state->i2c = i2c;
  404. memcpy(&state->ops, &cx22702_ops, sizeof(struct dvb_frontend_ops));
  405. state->prevUCBlocks = 0;
  406. /* check if the demod is there */
  407. if (cx22702_readreg(state, 0x1f) != 0x3)
  408. goto error;
  409. /* create dvb_frontend */
  410. state->frontend.ops = &state->ops;
  411. state->frontend.demodulator_priv = state;
  412. return &state->frontend;
  413. error:
  414. kfree(state);
  415. return NULL;
  416. }
  417. static struct dvb_frontend_ops cx22702_ops = {
  418. .info = {
  419. .name = "Conexant CX22702 DVB-T",
  420. .type = FE_OFDM,
  421. .frequency_min = 177000000,
  422. .frequency_max = 858000000,
  423. .frequency_stepsize = 166666,
  424. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  425. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  426. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  427. FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
  428. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER
  429. },
  430. .release = cx22702_release,
  431. .init = cx22702_init,
  432. .set_frontend = cx22702_set_tps,
  433. .get_frontend = cx22702_get_frontend,
  434. .get_tune_settings = cx22702_get_tune_settings,
  435. .read_status = cx22702_read_status,
  436. .read_ber = cx22702_read_ber,
  437. .read_signal_strength = cx22702_read_signal_strength,
  438. .read_snr = cx22702_read_snr,
  439. .read_ucblocks = cx22702_read_ucblocks,
  440. .i2c_gate_ctrl = cx22702_i2c_gate_ctrl,
  441. };
  442. module_param(debug, int, 0644);
  443. MODULE_PARM_DESC(debug, "Enable verbose debug messages");
  444. MODULE_DESCRIPTION("Conexant CX22702 DVB-T Demodulator driver");
  445. MODULE_AUTHOR("Steven Toth");
  446. MODULE_LICENSE("GPL");
  447. EXPORT_SYMBOL(cx22702_attach);