cx22700.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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/string.h>
  21. #include <linux/slab.h>
  22. #include "dvb_frontend.h"
  23. #include "cx22700.h"
  24. struct cx22700_state {
  25. struct i2c_adapter* i2c;
  26. const struct cx22700_config* config;
  27. struct dvb_frontend frontend;
  28. };
  29. static int debug;
  30. #define dprintk(args...) \
  31. do { \
  32. if (debug) printk(KERN_DEBUG "cx22700: " args); \
  33. } while (0)
  34. static u8 init_tab [] = {
  35. 0x04, 0x10,
  36. 0x05, 0x09,
  37. 0x06, 0x00,
  38. 0x08, 0x04,
  39. 0x09, 0x00,
  40. 0x0a, 0x01,
  41. 0x15, 0x40,
  42. 0x16, 0x10,
  43. 0x17, 0x87,
  44. 0x18, 0x17,
  45. 0x1a, 0x10,
  46. 0x25, 0x04,
  47. 0x2e, 0x00,
  48. 0x39, 0x00,
  49. 0x3a, 0x04,
  50. 0x45, 0x08,
  51. 0x46, 0x02,
  52. 0x47, 0x05,
  53. };
  54. static int cx22700_writereg (struct cx22700_state* state, u8 reg, u8 data)
  55. {
  56. int ret;
  57. u8 buf [] = { reg, data };
  58. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
  59. dprintk ("%s\n", __func__);
  60. ret = i2c_transfer (state->i2c, &msg, 1);
  61. if (ret != 1)
  62. printk("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
  63. __func__, reg, data, ret);
  64. return (ret != 1) ? -1 : 0;
  65. }
  66. static int cx22700_readreg (struct cx22700_state* state, u8 reg)
  67. {
  68. int ret;
  69. u8 b0 [] = { reg };
  70. u8 b1 [] = { 0 };
  71. struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
  72. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
  73. dprintk ("%s\n", __func__);
  74. ret = i2c_transfer (state->i2c, msg, 2);
  75. if (ret != 2) return -EIO;
  76. return b1[0];
  77. }
  78. static int cx22700_set_inversion (struct cx22700_state* state, int inversion)
  79. {
  80. u8 val;
  81. dprintk ("%s\n", __func__);
  82. switch (inversion) {
  83. case INVERSION_AUTO:
  84. return -EOPNOTSUPP;
  85. case INVERSION_ON:
  86. val = cx22700_readreg (state, 0x09);
  87. return cx22700_writereg (state, 0x09, val | 0x01);
  88. case INVERSION_OFF:
  89. val = cx22700_readreg (state, 0x09);
  90. return cx22700_writereg (state, 0x09, val & 0xfe);
  91. default:
  92. return -EINVAL;
  93. }
  94. }
  95. static int cx22700_set_tps(struct cx22700_state *state,
  96. struct dtv_frontend_properties *p)
  97. {
  98. static const u8 qam_tab [4] = { 0, 1, 0, 2 };
  99. static const u8 fec_tab [6] = { 0, 1, 2, 0, 3, 4 };
  100. u8 val;
  101. dprintk ("%s\n", __func__);
  102. if (p->code_rate_HP < FEC_1_2 || p->code_rate_HP > FEC_7_8)
  103. return -EINVAL;
  104. if (p->code_rate_LP < FEC_1_2 || p->code_rate_LP > FEC_7_8)
  105. return -EINVAL;
  106. if (p->code_rate_HP == FEC_4_5 || p->code_rate_LP == FEC_4_5)
  107. return -EINVAL;
  108. if ((int)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->modulation != QPSK &&
  115. p->modulation != QAM_16 &&
  116. p->modulation != QAM_64)
  117. return -EINVAL;
  118. if ((int)p->hierarchy < HIERARCHY_NONE ||
  119. p->hierarchy > HIERARCHY_4)
  120. return -EINVAL;
  121. if (p->bandwidth_hz > 8000000 || p->bandwidth_hz < 6000000)
  122. return -EINVAL;
  123. if (p->bandwidth_hz == 7000000)
  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->modulation - QPSK];
  128. val |= p->hierarchy - HIERARCHY_NONE;
  129. cx22700_writereg (state, 0x04, val);
  130. if (p->code_rate_HP - FEC_1_2 >= sizeof(fec_tab) ||
  131. p->code_rate_LP - FEC_1_2 >= sizeof(fec_tab))
  132. return -EINVAL;
  133. val = fec_tab[p->code_rate_HP - FEC_1_2] << 3;
  134. val |= fec_tab[p->code_rate_LP - FEC_1_2];
  135. cx22700_writereg (state, 0x05, val);
  136. val = (p->guard_interval - GUARD_INTERVAL_1_32) << 2;
  137. val |= p->transmission_mode - TRANSMISSION_MODE_2K;
  138. cx22700_writereg (state, 0x06, val);
  139. cx22700_writereg (state, 0x08, 0x04 | 0x02); /* use user tps parameters */
  140. cx22700_writereg (state, 0x08, 0x04); /* restart acquisition */
  141. return 0;
  142. }
  143. static int cx22700_get_tps(struct cx22700_state *state,
  144. struct dtv_frontend_properties *p)
  145. {
  146. static const enum fe_modulation qam_tab[3] = { QPSK, QAM_16, QAM_64 };
  147. static const enum fe_code_rate fec_tab[5] = {
  148. FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_7_8
  149. };
  150. u8 val;
  151. dprintk ("%s\n", __func__);
  152. if (!(cx22700_readreg(state, 0x07) & 0x20)) /* tps valid? */
  153. return -EAGAIN;
  154. val = cx22700_readreg (state, 0x01);
  155. if ((val & 0x7) > 4)
  156. p->hierarchy = HIERARCHY_AUTO;
  157. else
  158. p->hierarchy = HIERARCHY_NONE + (val & 0x7);
  159. if (((val >> 3) & 0x3) > 2)
  160. p->modulation = QAM_AUTO;
  161. else
  162. p->modulation = qam_tab[(val >> 3) & 0x3];
  163. val = cx22700_readreg (state, 0x02);
  164. if (((val >> 3) & 0x07) > 4)
  165. p->code_rate_HP = FEC_AUTO;
  166. else
  167. p->code_rate_HP = fec_tab[(val >> 3) & 0x07];
  168. if ((val & 0x07) > 4)
  169. p->code_rate_LP = FEC_AUTO;
  170. else
  171. p->code_rate_LP = fec_tab[val & 0x07];
  172. val = cx22700_readreg (state, 0x03);
  173. p->guard_interval = GUARD_INTERVAL_1_32 + ((val >> 6) & 0x3);
  174. p->transmission_mode = TRANSMISSION_MODE_2K + ((val >> 5) & 0x1);
  175. return 0;
  176. }
  177. static int cx22700_init (struct dvb_frontend* fe)
  178. { struct cx22700_state* state = fe->demodulator_priv;
  179. int i;
  180. dprintk("cx22700_init: init chip\n");
  181. cx22700_writereg (state, 0x00, 0x02); /* soft reset */
  182. cx22700_writereg (state, 0x00, 0x00);
  183. msleep(10);
  184. for (i=0; i<sizeof(init_tab); i+=2)
  185. cx22700_writereg (state, init_tab[i], init_tab[i+1]);
  186. cx22700_writereg (state, 0x00, 0x01);
  187. return 0;
  188. }
  189. static int cx22700_read_status(struct dvb_frontend *fe, enum fe_status *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)
  239. {
  240. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  241. struct cx22700_state* state = fe->demodulator_priv;
  242. cx22700_writereg (state, 0x00, 0x02); /* XXX CHECKME: soft reset*/
  243. cx22700_writereg (state, 0x00, 0x00);
  244. if (fe->ops.tuner_ops.set_params) {
  245. fe->ops.tuner_ops.set_params(fe);
  246. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  247. }
  248. cx22700_set_inversion(state, c->inversion);
  249. cx22700_set_tps(state, c);
  250. cx22700_writereg (state, 0x37, 0x01); /* PAL loop filter off */
  251. cx22700_writereg (state, 0x00, 0x01); /* restart acquire */
  252. return 0;
  253. }
  254. static int cx22700_get_frontend(struct dvb_frontend *fe,
  255. struct dtv_frontend_properties *c)
  256. {
  257. struct cx22700_state* state = fe->demodulator_priv;
  258. u8 reg09 = cx22700_readreg (state, 0x09);
  259. c->inversion = reg09 & 0x1 ? INVERSION_ON : INVERSION_OFF;
  260. return cx22700_get_tps(state, c);
  261. }
  262. static int cx22700_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  263. {
  264. struct cx22700_state* state = fe->demodulator_priv;
  265. if (enable) {
  266. return cx22700_writereg(state, 0x0a, 0x00);
  267. } else {
  268. return cx22700_writereg(state, 0x0a, 0x01);
  269. }
  270. }
  271. static int cx22700_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  272. {
  273. fesettings->min_delay_ms = 150;
  274. fesettings->step_size = 166667;
  275. fesettings->max_drift = 166667*2;
  276. return 0;
  277. }
  278. static void cx22700_release(struct dvb_frontend* fe)
  279. {
  280. struct cx22700_state* state = fe->demodulator_priv;
  281. kfree(state);
  282. }
  283. static struct dvb_frontend_ops cx22700_ops;
  284. struct dvb_frontend* cx22700_attach(const struct cx22700_config* config,
  285. struct i2c_adapter* i2c)
  286. {
  287. struct cx22700_state* state = NULL;
  288. /* allocate memory for the internal state */
  289. state = kzalloc(sizeof(struct cx22700_state), GFP_KERNEL);
  290. if (state == NULL) goto error;
  291. /* setup the state */
  292. state->config = config;
  293. state->i2c = i2c;
  294. /* check if the demod is there */
  295. if (cx22700_readreg(state, 0x07) < 0) goto error;
  296. /* create dvb_frontend */
  297. memcpy(&state->frontend.ops, &cx22700_ops, sizeof(struct dvb_frontend_ops));
  298. state->frontend.demodulator_priv = state;
  299. return &state->frontend;
  300. error:
  301. kfree(state);
  302. return NULL;
  303. }
  304. static struct dvb_frontend_ops cx22700_ops = {
  305. .delsys = { SYS_DVBT },
  306. .info = {
  307. .name = "Conexant CX22700 DVB-T",
  308. .frequency_min = 470000000,
  309. .frequency_max = 860000000,
  310. .frequency_stepsize = 166667,
  311. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  312. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  313. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  314. FE_CAN_RECOVER
  315. },
  316. .release = cx22700_release,
  317. .init = cx22700_init,
  318. .i2c_gate_ctrl = cx22700_i2c_gate_ctrl,
  319. .set_frontend = cx22700_set_frontend,
  320. .get_frontend = cx22700_get_frontend,
  321. .get_tune_settings = cx22700_get_tune_settings,
  322. .read_status = cx22700_read_status,
  323. .read_ber = cx22700_read_ber,
  324. .read_signal_strength = cx22700_read_signal_strength,
  325. .read_snr = cx22700_read_snr,
  326. .read_ucblocks = cx22700_read_ucblocks,
  327. };
  328. module_param(debug, int, 0644);
  329. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  330. MODULE_DESCRIPTION("Conexant CX22700 DVB-T Demodulator driver");
  331. MODULE_AUTHOR("Holger Waechtler");
  332. MODULE_LICENSE("GPL");
  333. EXPORT_SYMBOL(cx22700_attach);