cx22702.c 14 KB

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