cx22702.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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@linuxtv.org>
  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;
  37. module_param(debug, int, 0644);
  38. MODULE_PARM_DESC(debug, "Enable verbose debug messages");
  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 = {
  73. .addr = state->config->demod_address, .flags = 0,
  74. .buf = buf, .len = 2 };
  75. ret = i2c_transfer(state->i2c, &msg, 1);
  76. if (unlikely(ret != 1)) {
  77. printk(KERN_ERR
  78. "%s: error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
  79. __func__, reg, data, ret);
  80. return -1;
  81. }
  82. return 0;
  83. }
  84. static u8 cx22702_readreg(struct cx22702_state *state, u8 reg)
  85. {
  86. int ret;
  87. u8 data;
  88. struct i2c_msg msg[] = {
  89. { .addr = state->config->demod_address, .flags = 0,
  90. .buf = &reg, .len = 1 },
  91. { .addr = state->config->demod_address, .flags = I2C_M_RD,
  92. .buf = &data, .len = 1 } };
  93. ret = i2c_transfer(state->i2c, msg, 2);
  94. if (unlikely(ret != 2)) {
  95. printk(KERN_ERR "%s: error (reg == 0x%02x, ret == %i)\n",
  96. __func__, reg, ret);
  97. return 0;
  98. }
  99. return data;
  100. }
  101. static int cx22702_set_inversion(struct cx22702_state *state, int inversion)
  102. {
  103. u8 val;
  104. val = cx22702_readreg(state, 0x0C);
  105. switch (inversion) {
  106. case INVERSION_AUTO:
  107. return -EOPNOTSUPP;
  108. case INVERSION_ON:
  109. val |= 0x01;
  110. break;
  111. case INVERSION_OFF:
  112. val &= 0xfe;
  113. break;
  114. default:
  115. return -EINVAL;
  116. }
  117. return cx22702_writereg(state, 0x0C, val);
  118. }
  119. /* Retrieve the demod settings */
  120. static int cx22702_get_tps(struct cx22702_state *state,
  121. struct dvb_ofdm_parameters *p)
  122. {
  123. u8 val;
  124. /* Make sure the TPS regs are valid */
  125. if (!(cx22702_readreg(state, 0x0A) & 0x20))
  126. return -EAGAIN;
  127. val = cx22702_readreg(state, 0x01);
  128. switch ((val & 0x18) >> 3) {
  129. case 0:
  130. p->constellation = QPSK;
  131. break;
  132. case 1:
  133. p->constellation = QAM_16;
  134. break;
  135. case 2:
  136. p->constellation = QAM_64;
  137. break;
  138. }
  139. switch (val & 0x07) {
  140. case 0:
  141. p->hierarchy_information = HIERARCHY_NONE;
  142. break;
  143. case 1:
  144. p->hierarchy_information = HIERARCHY_1;
  145. break;
  146. case 2:
  147. p->hierarchy_information = HIERARCHY_2;
  148. break;
  149. case 3:
  150. p->hierarchy_information = HIERARCHY_4;
  151. break;
  152. }
  153. val = cx22702_readreg(state, 0x02);
  154. switch ((val & 0x38) >> 3) {
  155. case 0:
  156. p->code_rate_HP = FEC_1_2;
  157. break;
  158. case 1:
  159. p->code_rate_HP = FEC_2_3;
  160. break;
  161. case 2:
  162. p->code_rate_HP = FEC_3_4;
  163. break;
  164. case 3:
  165. p->code_rate_HP = FEC_5_6;
  166. break;
  167. case 4:
  168. p->code_rate_HP = FEC_7_8;
  169. break;
  170. }
  171. switch (val & 0x07) {
  172. case 0:
  173. p->code_rate_LP = FEC_1_2;
  174. break;
  175. case 1:
  176. p->code_rate_LP = FEC_2_3;
  177. break;
  178. case 2:
  179. p->code_rate_LP = FEC_3_4;
  180. break;
  181. case 3:
  182. p->code_rate_LP = FEC_5_6;
  183. break;
  184. case 4:
  185. p->code_rate_LP = FEC_7_8;
  186. break;
  187. }
  188. val = cx22702_readreg(state, 0x03);
  189. switch ((val & 0x0c) >> 2) {
  190. case 0:
  191. p->guard_interval = GUARD_INTERVAL_1_32;
  192. break;
  193. case 1:
  194. p->guard_interval = GUARD_INTERVAL_1_16;
  195. break;
  196. case 2:
  197. p->guard_interval = GUARD_INTERVAL_1_8;
  198. break;
  199. case 3:
  200. p->guard_interval = GUARD_INTERVAL_1_4;
  201. break;
  202. }
  203. switch (val & 0x03) {
  204. case 0:
  205. p->transmission_mode = TRANSMISSION_MODE_2K;
  206. break;
  207. case 1:
  208. p->transmission_mode = TRANSMISSION_MODE_8K;
  209. break;
  210. }
  211. return 0;
  212. }
  213. static int cx22702_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  214. {
  215. struct cx22702_state *state = fe->demodulator_priv;
  216. u8 val;
  217. dprintk("%s(%d)\n", __func__, enable);
  218. val = cx22702_readreg(state, 0x0D);
  219. if (enable)
  220. val &= 0xfe;
  221. else
  222. val |= 0x01;
  223. return cx22702_writereg(state, 0x0D, val);
  224. }
  225. /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
  226. static int cx22702_set_tps(struct dvb_frontend *fe,
  227. struct dvb_frontend_parameters *p)
  228. {
  229. u8 val;
  230. struct cx22702_state *state = fe->demodulator_priv;
  231. if (fe->ops.tuner_ops.set_params) {
  232. fe->ops.tuner_ops.set_params(fe, p);
  233. if (fe->ops.i2c_gate_ctrl)
  234. fe->ops.i2c_gate_ctrl(fe, 0);
  235. }
  236. /* set inversion */
  237. cx22702_set_inversion(state, p->inversion);
  238. /* set bandwidth */
  239. val = cx22702_readreg(state, 0x0C) & 0xcf;
  240. switch (p->u.ofdm.bandwidth) {
  241. case BANDWIDTH_6_MHZ:
  242. val |= 0x20;
  243. break;
  244. case BANDWIDTH_7_MHZ:
  245. val |= 0x10;
  246. break;
  247. case BANDWIDTH_8_MHZ:
  248. break;
  249. default:
  250. dprintk("%s: invalid bandwidth\n", __func__);
  251. return -EINVAL;
  252. }
  253. cx22702_writereg(state, 0x0C, val);
  254. p->u.ofdm.code_rate_LP = FEC_AUTO; /* temp hack as manual not working */
  255. /* use auto configuration? */
  256. if ((p->u.ofdm.hierarchy_information == HIERARCHY_AUTO) ||
  257. (p->u.ofdm.constellation == QAM_AUTO) ||
  258. (p->u.ofdm.code_rate_HP == FEC_AUTO) ||
  259. (p->u.ofdm.code_rate_LP == FEC_AUTO) ||
  260. (p->u.ofdm.guard_interval == GUARD_INTERVAL_AUTO) ||
  261. (p->u.ofdm.transmission_mode == TRANSMISSION_MODE_AUTO)) {
  262. /* TPS Source - use hardware driven values */
  263. cx22702_writereg(state, 0x06, 0x10);
  264. cx22702_writereg(state, 0x07, 0x9);
  265. cx22702_writereg(state, 0x08, 0xC1);
  266. cx22702_writereg(state, 0x0B, cx22702_readreg(state, 0x0B)
  267. & 0xfc);
  268. cx22702_writereg(state, 0x0C,
  269. (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40);
  270. cx22702_writereg(state, 0x00, 0x01); /* Begin aquisition */
  271. dprintk("%s: Autodetecting\n", __func__);
  272. return 0;
  273. }
  274. /* manually programmed values */
  275. val = 0;
  276. switch (p->u.ofdm.constellation) {
  277. case QPSK:
  278. val = (val & 0xe7);
  279. break;
  280. case QAM_16:
  281. val = (val & 0xe7) | 0x08;
  282. break;
  283. case QAM_64:
  284. val = (val & 0xe7) | 0x10;
  285. break;
  286. default:
  287. dprintk("%s: invalid constellation\n", __func__);
  288. return -EINVAL;
  289. }
  290. switch (p->u.ofdm.hierarchy_information) {
  291. case HIERARCHY_NONE:
  292. val = (val & 0xf8);
  293. break;
  294. case HIERARCHY_1:
  295. val = (val & 0xf8) | 1;
  296. break;
  297. case HIERARCHY_2:
  298. val = (val & 0xf8) | 2;
  299. break;
  300. case HIERARCHY_4:
  301. val = (val & 0xf8) | 3;
  302. break;
  303. default:
  304. dprintk("%s: invalid hierarchy\n", __func__);
  305. return -EINVAL;
  306. }
  307. cx22702_writereg(state, 0x06, val);
  308. val = 0;
  309. switch (p->u.ofdm.code_rate_HP) {
  310. case FEC_NONE:
  311. case FEC_1_2:
  312. val = (val & 0xc7);
  313. break;
  314. case FEC_2_3:
  315. val = (val & 0xc7) | 0x08;
  316. break;
  317. case FEC_3_4:
  318. val = (val & 0xc7) | 0x10;
  319. break;
  320. case FEC_5_6:
  321. val = (val & 0xc7) | 0x18;
  322. break;
  323. case FEC_7_8:
  324. val = (val & 0xc7) | 0x20;
  325. break;
  326. default:
  327. dprintk("%s: invalid code_rate_HP\n", __func__);
  328. return -EINVAL;
  329. }
  330. switch (p->u.ofdm.code_rate_LP) {
  331. case FEC_NONE:
  332. case FEC_1_2:
  333. val = (val & 0xf8);
  334. break;
  335. case FEC_2_3:
  336. val = (val & 0xf8) | 1;
  337. break;
  338. case FEC_3_4:
  339. val = (val & 0xf8) | 2;
  340. break;
  341. case FEC_5_6:
  342. val = (val & 0xf8) | 3;
  343. break;
  344. case FEC_7_8:
  345. val = (val & 0xf8) | 4;
  346. break;
  347. default:
  348. dprintk("%s: invalid code_rate_LP\n", __func__);
  349. return -EINVAL;
  350. }
  351. cx22702_writereg(state, 0x07, val);
  352. val = 0;
  353. switch (p->u.ofdm.guard_interval) {
  354. case GUARD_INTERVAL_1_32:
  355. val = (val & 0xf3);
  356. break;
  357. case GUARD_INTERVAL_1_16:
  358. val = (val & 0xf3) | 0x04;
  359. break;
  360. case GUARD_INTERVAL_1_8:
  361. val = (val & 0xf3) | 0x08;
  362. break;
  363. case GUARD_INTERVAL_1_4:
  364. val = (val & 0xf3) | 0x0c;
  365. break;
  366. default:
  367. dprintk("%s: invalid guard_interval\n", __func__);
  368. return -EINVAL;
  369. }
  370. switch (p->u.ofdm.transmission_mode) {
  371. case TRANSMISSION_MODE_2K:
  372. val = (val & 0xfc);
  373. break;
  374. case TRANSMISSION_MODE_8K:
  375. val = (val & 0xfc) | 1;
  376. break;
  377. default:
  378. dprintk("%s: invalid transmission_mode\n", __func__);
  379. return -EINVAL;
  380. }
  381. cx22702_writereg(state, 0x08, val);
  382. cx22702_writereg(state, 0x0B,
  383. (cx22702_readreg(state, 0x0B) & 0xfc) | 0x02);
  384. cx22702_writereg(state, 0x0C,
  385. (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40);
  386. /* Begin channel aquisition */
  387. cx22702_writereg(state, 0x00, 0x01);
  388. return 0;
  389. }
  390. /* Reset the demod hardware and reset all of the configuration registers
  391. to a default state. */
  392. static int cx22702_init(struct dvb_frontend *fe)
  393. {
  394. int i;
  395. struct cx22702_state *state = fe->demodulator_priv;
  396. cx22702_writereg(state, 0x00, 0x02);
  397. msleep(10);
  398. for (i = 0; i < ARRAY_SIZE(init_tab); i += 2)
  399. cx22702_writereg(state, init_tab[i], init_tab[i + 1]);
  400. cx22702_writereg(state, 0xf8, (state->config->output_mode << 1)
  401. & 0x02);
  402. cx22702_i2c_gate_ctrl(fe, 0);
  403. return 0;
  404. }
  405. static int cx22702_read_status(struct dvb_frontend *fe, fe_status_t *status)
  406. {
  407. struct cx22702_state *state = fe->demodulator_priv;
  408. u8 reg0A;
  409. u8 reg23;
  410. *status = 0;
  411. reg0A = cx22702_readreg(state, 0x0A);
  412. reg23 = cx22702_readreg(state, 0x23);
  413. dprintk("%s: status demod=0x%02x agc=0x%02x\n"
  414. , __func__, reg0A, reg23);
  415. if (reg0A & 0x10) {
  416. *status |= FE_HAS_LOCK;
  417. *status |= FE_HAS_VITERBI;
  418. *status |= FE_HAS_SYNC;
  419. }
  420. if (reg0A & 0x20)
  421. *status |= FE_HAS_CARRIER;
  422. if (reg23 < 0xf0)
  423. *status |= FE_HAS_SIGNAL;
  424. return 0;
  425. }
  426. static int cx22702_read_ber(struct dvb_frontend *fe, u32 *ber)
  427. {
  428. struct cx22702_state *state = fe->demodulator_priv;
  429. if (cx22702_readreg(state, 0xE4) & 0x02) {
  430. /* Realtime statistics */
  431. *ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7
  432. | (cx22702_readreg(state, 0xDF) & 0x7F);
  433. } else {
  434. /* Averagtine statistics */
  435. *ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7
  436. | cx22702_readreg(state, 0xDF);
  437. }
  438. return 0;
  439. }
  440. static int cx22702_read_signal_strength(struct dvb_frontend *fe,
  441. u16 *signal_strength)
  442. {
  443. struct cx22702_state *state = fe->demodulator_priv;
  444. u16 rs_ber;
  445. rs_ber = cx22702_readreg(state, 0x23);
  446. *signal_strength = (rs_ber << 8) | rs_ber;
  447. return 0;
  448. }
  449. static int cx22702_read_snr(struct dvb_frontend *fe, u16 *snr)
  450. {
  451. struct cx22702_state *state = fe->demodulator_priv;
  452. u16 rs_ber;
  453. if (cx22702_readreg(state, 0xE4) & 0x02) {
  454. /* Realtime statistics */
  455. rs_ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7
  456. | (cx22702_readreg(state, 0xDF) & 0x7F);
  457. } else {
  458. /* Averagine statistics */
  459. rs_ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 8
  460. | cx22702_readreg(state, 0xDF);
  461. }
  462. *snr = ~rs_ber;
  463. return 0;
  464. }
  465. static int cx22702_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  466. {
  467. struct cx22702_state *state = fe->demodulator_priv;
  468. u8 _ucblocks;
  469. /* RS Uncorrectable Packet Count then reset */
  470. _ucblocks = cx22702_readreg(state, 0xE3);
  471. if (state->prevUCBlocks < _ucblocks)
  472. *ucblocks = (_ucblocks - state->prevUCBlocks);
  473. else
  474. *ucblocks = state->prevUCBlocks - _ucblocks;
  475. state->prevUCBlocks = _ucblocks;
  476. return 0;
  477. }
  478. static int cx22702_get_frontend(struct dvb_frontend *fe,
  479. struct dvb_frontend_parameters *p)
  480. {
  481. struct cx22702_state *state = fe->demodulator_priv;
  482. u8 reg0C = cx22702_readreg(state, 0x0C);
  483. p->inversion = reg0C & 0x1 ? INVERSION_ON : INVERSION_OFF;
  484. return cx22702_get_tps(state, &p->u.ofdm);
  485. }
  486. static int cx22702_get_tune_settings(struct dvb_frontend *fe,
  487. struct dvb_frontend_tune_settings *tune)
  488. {
  489. tune->min_delay_ms = 1000;
  490. return 0;
  491. }
  492. static void cx22702_release(struct dvb_frontend *fe)
  493. {
  494. struct cx22702_state *state = fe->demodulator_priv;
  495. kfree(state);
  496. }
  497. static struct dvb_frontend_ops cx22702_ops;
  498. struct dvb_frontend *cx22702_attach(const struct cx22702_config *config,
  499. struct i2c_adapter *i2c)
  500. {
  501. struct cx22702_state *state = NULL;
  502. /* allocate memory for the internal state */
  503. state = kzalloc(sizeof(struct cx22702_state), GFP_KERNEL);
  504. if (state == NULL)
  505. goto error;
  506. /* setup the state */
  507. state->config = config;
  508. state->i2c = i2c;
  509. /* check if the demod is there */
  510. if (cx22702_readreg(state, 0x1f) != 0x3)
  511. goto error;
  512. /* create dvb_frontend */
  513. memcpy(&state->frontend.ops, &cx22702_ops,
  514. sizeof(struct dvb_frontend_ops));
  515. state->frontend.demodulator_priv = state;
  516. return &state->frontend;
  517. error:
  518. kfree(state);
  519. return NULL;
  520. }
  521. EXPORT_SYMBOL(cx22702_attach);
  522. static struct dvb_frontend_ops cx22702_ops = {
  523. .info = {
  524. .name = "Conexant CX22702 DVB-T",
  525. .type = FE_OFDM,
  526. .frequency_min = 177000000,
  527. .frequency_max = 858000000,
  528. .frequency_stepsize = 166666,
  529. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  530. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  531. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  532. FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
  533. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER
  534. },
  535. .release = cx22702_release,
  536. .init = cx22702_init,
  537. .i2c_gate_ctrl = cx22702_i2c_gate_ctrl,
  538. .set_frontend = cx22702_set_tps,
  539. .get_frontend = cx22702_get_frontend,
  540. .get_tune_settings = cx22702_get_tune_settings,
  541. .read_status = cx22702_read_status,
  542. .read_ber = cx22702_read_ber,
  543. .read_signal_strength = cx22702_read_signal_strength,
  544. .read_snr = cx22702_read_snr,
  545. .read_ucblocks = cx22702_read_ucblocks,
  546. };
  547. MODULE_DESCRIPTION("Conexant CX22702 DVB-T Demodulator driver");
  548. MODULE_AUTHOR("Steven Toth");
  549. MODULE_LICENSE("GPL");