cx22702.c 14 KB

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