cx22700.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. Conexant cx22700 DVB OFDM demodulator driver
  3. Copyright (C) 2001-2002 Convergence Integrated Media GmbH
  4. Holger Waechtler <holger@convergence.de>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/string.h>
  22. #include <linux/slab.h>
  23. #include "dvb_frontend.h"
  24. #include "cx22700.h"
  25. struct cx22700_state {
  26. struct i2c_adapter* i2c;
  27. struct dvb_frontend_ops ops;
  28. const struct cx22700_config* config;
  29. struct dvb_frontend frontend;
  30. };
  31. static int debug;
  32. #define dprintk(args...) \
  33. do { \
  34. if (debug) printk(KERN_DEBUG "cx22700: " args); \
  35. } while (0)
  36. static u8 init_tab [] = {
  37. 0x04, 0x10,
  38. 0x05, 0x09,
  39. 0x06, 0x00,
  40. 0x08, 0x04,
  41. 0x09, 0x00,
  42. 0x0a, 0x01,
  43. 0x15, 0x40,
  44. 0x16, 0x10,
  45. 0x17, 0x87,
  46. 0x18, 0x17,
  47. 0x1a, 0x10,
  48. 0x25, 0x04,
  49. 0x2e, 0x00,
  50. 0x39, 0x00,
  51. 0x3a, 0x04,
  52. 0x45, 0x08,
  53. 0x46, 0x02,
  54. 0x47, 0x05,
  55. };
  56. static int cx22700_writereg (struct cx22700_state* state, u8 reg, u8 data)
  57. {
  58. int ret;
  59. u8 buf [] = { reg, data };
  60. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
  61. dprintk ("%s\n", __FUNCTION__);
  62. ret = i2c_transfer (state->i2c, &msg, 1);
  63. if (ret != 1)
  64. printk("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
  65. __FUNCTION__, reg, data, ret);
  66. return (ret != 1) ? -1 : 0;
  67. }
  68. static int cx22700_readreg (struct cx22700_state* state, u8 reg)
  69. {
  70. int ret;
  71. u8 b0 [] = { reg };
  72. u8 b1 [] = { 0 };
  73. struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
  74. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
  75. dprintk ("%s\n", __FUNCTION__);
  76. ret = i2c_transfer (state->i2c, msg, 2);
  77. if (ret != 2) return -EIO;
  78. return b1[0];
  79. }
  80. static int cx22700_set_inversion (struct cx22700_state* state, int inversion)
  81. {
  82. u8 val;
  83. dprintk ("%s\n", __FUNCTION__);
  84. switch (inversion) {
  85. case INVERSION_AUTO:
  86. return -EOPNOTSUPP;
  87. case INVERSION_ON:
  88. val = cx22700_readreg (state, 0x09);
  89. return cx22700_writereg (state, 0x09, val | 0x01);
  90. case INVERSION_OFF:
  91. val = cx22700_readreg (state, 0x09);
  92. return cx22700_writereg (state, 0x09, val & 0xfe);
  93. default:
  94. return -EINVAL;
  95. }
  96. }
  97. static int cx22700_set_tps (struct cx22700_state *state, struct dvb_ofdm_parameters *p)
  98. {
  99. static const u8 qam_tab [4] = { 0, 1, 0, 2 };
  100. static const u8 fec_tab [6] = { 0, 1, 2, 0, 3, 4 };
  101. u8 val;
  102. dprintk ("%s\n", __FUNCTION__);
  103. if (p->code_rate_HP < FEC_1_2 || p->code_rate_HP > FEC_7_8)
  104. return -EINVAL;
  105. if (p->code_rate_LP < FEC_1_2 || p->code_rate_LP > FEC_7_8)
  106. if (p->code_rate_HP == FEC_4_5 || p->code_rate_LP == FEC_4_5)
  107. return -EINVAL;
  108. if (p->guard_interval < GUARD_INTERVAL_1_32 ||
  109. p->guard_interval > GUARD_INTERVAL_1_4)
  110. return -EINVAL;
  111. if (p->transmission_mode != TRANSMISSION_MODE_2K &&
  112. p->transmission_mode != TRANSMISSION_MODE_8K)
  113. return -EINVAL;
  114. if (p->constellation != QPSK &&
  115. p->constellation != QAM_16 &&
  116. p->constellation != QAM_64)
  117. return -EINVAL;
  118. if (p->hierarchy_information < HIERARCHY_NONE ||
  119. p->hierarchy_information > HIERARCHY_4)
  120. return -EINVAL;
  121. if (p->bandwidth < BANDWIDTH_8_MHZ && p->bandwidth > BANDWIDTH_6_MHZ)
  122. return -EINVAL;
  123. if (p->bandwidth == BANDWIDTH_7_MHZ)
  124. cx22700_writereg (state, 0x09, cx22700_readreg (state, 0x09 | 0x10));
  125. else
  126. cx22700_writereg (state, 0x09, cx22700_readreg (state, 0x09 & ~0x10));
  127. val = qam_tab[p->constellation - QPSK];
  128. val |= p->hierarchy_information - HIERARCHY_NONE;
  129. cx22700_writereg (state, 0x04, val);
  130. val = fec_tab[p->code_rate_HP - FEC_1_2] << 3;
  131. val |= fec_tab[p->code_rate_LP - FEC_1_2];
  132. cx22700_writereg (state, 0x05, val);
  133. val = (p->guard_interval - GUARD_INTERVAL_1_32) << 2;
  134. val |= p->transmission_mode - TRANSMISSION_MODE_2K;
  135. cx22700_writereg (state, 0x06, val);
  136. cx22700_writereg (state, 0x08, 0x04 | 0x02); /* use user tps parameters */
  137. cx22700_writereg (state, 0x08, 0x04); /* restart aquisition */
  138. return 0;
  139. }
  140. static int cx22700_get_tps (struct cx22700_state* state, struct dvb_ofdm_parameters *p)
  141. {
  142. static const fe_modulation_t qam_tab [3] = { QPSK, QAM_16, QAM_64 };
  143. static const fe_code_rate_t fec_tab [5] = { FEC_1_2, FEC_2_3, FEC_3_4,
  144. FEC_5_6, FEC_7_8 };
  145. u8 val;
  146. dprintk ("%s\n", __FUNCTION__);
  147. if (!(cx22700_readreg(state, 0x07) & 0x20)) /* tps valid? */
  148. return -EAGAIN;
  149. val = cx22700_readreg (state, 0x01);
  150. if ((val & 0x7) > 4)
  151. p->hierarchy_information = HIERARCHY_AUTO;
  152. else
  153. p->hierarchy_information = HIERARCHY_NONE + (val & 0x7);
  154. if (((val >> 3) & 0x3) > 2)
  155. p->constellation = QAM_AUTO;
  156. else
  157. p->constellation = qam_tab[(val >> 3) & 0x3];
  158. val = cx22700_readreg (state, 0x02);
  159. if (((val >> 3) & 0x07) > 4)
  160. p->code_rate_HP = FEC_AUTO;
  161. else
  162. p->code_rate_HP = fec_tab[(val >> 3) & 0x07];
  163. if ((val & 0x07) > 4)
  164. p->code_rate_LP = FEC_AUTO;
  165. else
  166. p->code_rate_LP = fec_tab[val & 0x07];
  167. val = cx22700_readreg (state, 0x03);
  168. p->guard_interval = GUARD_INTERVAL_1_32 + ((val >> 6) & 0x3);
  169. p->transmission_mode = TRANSMISSION_MODE_2K + ((val >> 5) & 0x1);
  170. return 0;
  171. }
  172. static int cx22700_init (struct dvb_frontend* fe)
  173. { struct cx22700_state* state = fe->demodulator_priv;
  174. int i;
  175. dprintk("cx22700_init: init chip\n");
  176. cx22700_writereg (state, 0x00, 0x02); /* soft reset */
  177. cx22700_writereg (state, 0x00, 0x00);
  178. msleep(10);
  179. for (i=0; i<sizeof(init_tab); i+=2)
  180. cx22700_writereg (state, init_tab[i], init_tab[i+1]);
  181. cx22700_writereg (state, 0x00, 0x01);
  182. if (state->config->pll_init) {
  183. cx22700_writereg (state, 0x0a, 0x00); /* open i2c bus switch */
  184. state->config->pll_init(fe);
  185. cx22700_writereg (state, 0x0a, 0x01); /* close i2c bus switch */
  186. }
  187. return 0;
  188. }
  189. static int cx22700_read_status(struct dvb_frontend* fe, fe_status_t* status)
  190. {
  191. struct cx22700_state* state = fe->demodulator_priv;
  192. u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
  193. | (cx22700_readreg (state, 0x0e) << 1);
  194. u8 sync = cx22700_readreg (state, 0x07);
  195. *status = 0;
  196. if (rs_ber < 0xff00)
  197. *status |= FE_HAS_SIGNAL;
  198. if (sync & 0x20)
  199. *status |= FE_HAS_CARRIER;
  200. if (sync & 0x10)
  201. *status |= FE_HAS_VITERBI;
  202. if (sync & 0x10)
  203. *status |= FE_HAS_SYNC;
  204. if (*status == 0x0f)
  205. *status |= FE_HAS_LOCK;
  206. return 0;
  207. }
  208. static int cx22700_read_ber(struct dvb_frontend* fe, u32* ber)
  209. {
  210. struct cx22700_state* state = fe->demodulator_priv;
  211. *ber = cx22700_readreg (state, 0x0c) & 0x7f;
  212. cx22700_writereg (state, 0x0c, 0x00);
  213. return 0;
  214. }
  215. static int cx22700_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
  216. {
  217. struct cx22700_state* state = fe->demodulator_priv;
  218. u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
  219. | (cx22700_readreg (state, 0x0e) << 1);
  220. *signal_strength = ~rs_ber;
  221. return 0;
  222. }
  223. static int cx22700_read_snr(struct dvb_frontend* fe, u16* snr)
  224. {
  225. struct cx22700_state* state = fe->demodulator_priv;
  226. u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
  227. | (cx22700_readreg (state, 0x0e) << 1);
  228. *snr = ~rs_ber;
  229. return 0;
  230. }
  231. static int cx22700_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  232. {
  233. struct cx22700_state* state = fe->demodulator_priv;
  234. *ucblocks = cx22700_readreg (state, 0x0f);
  235. cx22700_writereg (state, 0x0f, 0x00);
  236. return 0;
  237. }
  238. static int cx22700_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  239. {
  240. struct cx22700_state* state = fe->demodulator_priv;
  241. cx22700_writereg (state, 0x00, 0x02); /* XXX CHECKME: soft reset*/
  242. cx22700_writereg (state, 0x00, 0x00);
  243. cx22700_writereg (state, 0x0a, 0x00); /* open i2c bus switch */
  244. state->config->pll_set(fe, p);
  245. cx22700_writereg (state, 0x0a, 0x01); /* close i2c bus switch */
  246. cx22700_set_inversion (state, p->inversion);
  247. cx22700_set_tps (state, &p->u.ofdm);
  248. cx22700_writereg (state, 0x37, 0x01); /* PAL loop filter off */
  249. cx22700_writereg (state, 0x00, 0x01); /* restart acquire */
  250. return 0;
  251. }
  252. static int cx22700_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  253. {
  254. struct cx22700_state* state = fe->demodulator_priv;
  255. u8 reg09 = cx22700_readreg (state, 0x09);
  256. p->inversion = reg09 & 0x1 ? INVERSION_ON : INVERSION_OFF;
  257. return cx22700_get_tps (state, &p->u.ofdm);
  258. }
  259. static int cx22700_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  260. {
  261. fesettings->min_delay_ms = 150;
  262. fesettings->step_size = 166667;
  263. fesettings->max_drift = 166667*2;
  264. return 0;
  265. }
  266. static void cx22700_release(struct dvb_frontend* fe)
  267. {
  268. struct cx22700_state* state = fe->demodulator_priv;
  269. kfree(state);
  270. }
  271. static struct dvb_frontend_ops cx22700_ops;
  272. struct dvb_frontend* cx22700_attach(const struct cx22700_config* config,
  273. struct i2c_adapter* i2c)
  274. {
  275. struct cx22700_state* state = NULL;
  276. /* allocate memory for the internal state */
  277. state = kmalloc(sizeof(struct cx22700_state), GFP_KERNEL);
  278. if (state == NULL) goto error;
  279. /* setup the state */
  280. state->config = config;
  281. state->i2c = i2c;
  282. memcpy(&state->ops, &cx22700_ops, sizeof(struct dvb_frontend_ops));
  283. /* check if the demod is there */
  284. if (cx22700_readreg(state, 0x07) < 0) goto error;
  285. /* create dvb_frontend */
  286. state->frontend.ops = &state->ops;
  287. state->frontend.demodulator_priv = state;
  288. return &state->frontend;
  289. error:
  290. kfree(state);
  291. return NULL;
  292. }
  293. static struct dvb_frontend_ops cx22700_ops = {
  294. .info = {
  295. .name = "Conexant CX22700 DVB-T",
  296. .type = FE_OFDM,
  297. .frequency_min = 470000000,
  298. .frequency_max = 860000000,
  299. .frequency_stepsize = 166667,
  300. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  301. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  302. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  303. FE_CAN_RECOVER
  304. },
  305. .release = cx22700_release,
  306. .init = cx22700_init,
  307. .set_frontend = cx22700_set_frontend,
  308. .get_frontend = cx22700_get_frontend,
  309. .get_tune_settings = cx22700_get_tune_settings,
  310. .read_status = cx22700_read_status,
  311. .read_ber = cx22700_read_ber,
  312. .read_signal_strength = cx22700_read_signal_strength,
  313. .read_snr = cx22700_read_snr,
  314. .read_ucblocks = cx22700_read_ucblocks,
  315. };
  316. module_param(debug, int, 0644);
  317. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  318. MODULE_DESCRIPTION("Conexant CX22700 DVB-T Demodulator driver");
  319. MODULE_AUTHOR("Holger Waechtler");
  320. MODULE_LICENSE("GPL");
  321. EXPORT_SYMBOL(cx22700_attach);