cx22700.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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, struct dvb_ofdm_parameters *p)
  96. {
  97. static const u8 qam_tab [4] = { 0, 1, 0, 2 };
  98. static const u8 fec_tab [6] = { 0, 1, 2, 0, 3, 4 };
  99. u8 val;
  100. dprintk ("%s\n", __func__);
  101. if (p->code_rate_HP < FEC_1_2 || p->code_rate_HP > FEC_7_8)
  102. return -EINVAL;
  103. if (p->code_rate_LP < FEC_1_2 || p->code_rate_LP > FEC_7_8)
  104. return -EINVAL;
  105. if (p->code_rate_HP == FEC_4_5 || p->code_rate_LP == FEC_4_5)
  106. return -EINVAL;
  107. if (p->guard_interval < GUARD_INTERVAL_1_32 ||
  108. p->guard_interval > GUARD_INTERVAL_1_4)
  109. return -EINVAL;
  110. if (p->transmission_mode != TRANSMISSION_MODE_2K &&
  111. p->transmission_mode != TRANSMISSION_MODE_8K)
  112. return -EINVAL;
  113. if (p->constellation != QPSK &&
  114. p->constellation != QAM_16 &&
  115. p->constellation != QAM_64)
  116. return -EINVAL;
  117. if (p->hierarchy_information < HIERARCHY_NONE ||
  118. p->hierarchy_information > HIERARCHY_4)
  119. return -EINVAL;
  120. if (p->bandwidth < BANDWIDTH_8_MHZ || p->bandwidth > BANDWIDTH_6_MHZ)
  121. return -EINVAL;
  122. if (p->bandwidth == BANDWIDTH_7_MHZ)
  123. cx22700_writereg (state, 0x09, cx22700_readreg (state, 0x09 | 0x10));
  124. else
  125. cx22700_writereg (state, 0x09, cx22700_readreg (state, 0x09 & ~0x10));
  126. val = qam_tab[p->constellation - QPSK];
  127. val |= p->hierarchy_information - HIERARCHY_NONE;
  128. cx22700_writereg (state, 0x04, val);
  129. val = fec_tab[p->code_rate_HP - FEC_1_2] << 3;
  130. val |= fec_tab[p->code_rate_LP - FEC_1_2];
  131. cx22700_writereg (state, 0x05, val);
  132. val = (p->guard_interval - GUARD_INTERVAL_1_32) << 2;
  133. val |= p->transmission_mode - TRANSMISSION_MODE_2K;
  134. cx22700_writereg (state, 0x06, val);
  135. cx22700_writereg (state, 0x08, 0x04 | 0x02); /* use user tps parameters */
  136. cx22700_writereg (state, 0x08, 0x04); /* restart acquisition */
  137. return 0;
  138. }
  139. static int cx22700_get_tps (struct cx22700_state* state, struct dvb_ofdm_parameters *p)
  140. {
  141. static const fe_modulation_t qam_tab [3] = { QPSK, QAM_16, QAM_64 };
  142. static const fe_code_rate_t fec_tab [5] = { FEC_1_2, FEC_2_3, FEC_3_4,
  143. FEC_5_6, FEC_7_8 };
  144. u8 val;
  145. dprintk ("%s\n", __func__);
  146. if (!(cx22700_readreg(state, 0x07) & 0x20)) /* tps valid? */
  147. return -EAGAIN;
  148. val = cx22700_readreg (state, 0x01);
  149. if ((val & 0x7) > 4)
  150. p->hierarchy_information = HIERARCHY_AUTO;
  151. else
  152. p->hierarchy_information = HIERARCHY_NONE + (val & 0x7);
  153. if (((val >> 3) & 0x3) > 2)
  154. p->constellation = QAM_AUTO;
  155. else
  156. p->constellation = qam_tab[(val >> 3) & 0x3];
  157. val = cx22700_readreg (state, 0x02);
  158. if (((val >> 3) & 0x07) > 4)
  159. p->code_rate_HP = FEC_AUTO;
  160. else
  161. p->code_rate_HP = fec_tab[(val >> 3) & 0x07];
  162. if ((val & 0x07) > 4)
  163. p->code_rate_LP = FEC_AUTO;
  164. else
  165. p->code_rate_LP = fec_tab[val & 0x07];
  166. val = cx22700_readreg (state, 0x03);
  167. p->guard_interval = GUARD_INTERVAL_1_32 + ((val >> 6) & 0x3);
  168. p->transmission_mode = TRANSMISSION_MODE_2K + ((val >> 5) & 0x1);
  169. return 0;
  170. }
  171. static int cx22700_init (struct dvb_frontend* fe)
  172. { struct cx22700_state* state = fe->demodulator_priv;
  173. int i;
  174. dprintk("cx22700_init: init chip\n");
  175. cx22700_writereg (state, 0x00, 0x02); /* soft reset */
  176. cx22700_writereg (state, 0x00, 0x00);
  177. msleep(10);
  178. for (i=0; i<sizeof(init_tab); i+=2)
  179. cx22700_writereg (state, init_tab[i], init_tab[i+1]);
  180. cx22700_writereg (state, 0x00, 0x01);
  181. return 0;
  182. }
  183. static int cx22700_read_status(struct dvb_frontend* fe, fe_status_t* status)
  184. {
  185. struct cx22700_state* state = fe->demodulator_priv;
  186. u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
  187. | (cx22700_readreg (state, 0x0e) << 1);
  188. u8 sync = cx22700_readreg (state, 0x07);
  189. *status = 0;
  190. if (rs_ber < 0xff00)
  191. *status |= FE_HAS_SIGNAL;
  192. if (sync & 0x20)
  193. *status |= FE_HAS_CARRIER;
  194. if (sync & 0x10)
  195. *status |= FE_HAS_VITERBI;
  196. if (sync & 0x10)
  197. *status |= FE_HAS_SYNC;
  198. if (*status == 0x0f)
  199. *status |= FE_HAS_LOCK;
  200. return 0;
  201. }
  202. static int cx22700_read_ber(struct dvb_frontend* fe, u32* ber)
  203. {
  204. struct cx22700_state* state = fe->demodulator_priv;
  205. *ber = cx22700_readreg (state, 0x0c) & 0x7f;
  206. cx22700_writereg (state, 0x0c, 0x00);
  207. return 0;
  208. }
  209. static int cx22700_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
  210. {
  211. struct cx22700_state* state = fe->demodulator_priv;
  212. u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
  213. | (cx22700_readreg (state, 0x0e) << 1);
  214. *signal_strength = ~rs_ber;
  215. return 0;
  216. }
  217. static int cx22700_read_snr(struct dvb_frontend* fe, u16* snr)
  218. {
  219. struct cx22700_state* state = fe->demodulator_priv;
  220. u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
  221. | (cx22700_readreg (state, 0x0e) << 1);
  222. *snr = ~rs_ber;
  223. return 0;
  224. }
  225. static int cx22700_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  226. {
  227. struct cx22700_state* state = fe->demodulator_priv;
  228. *ucblocks = cx22700_readreg (state, 0x0f);
  229. cx22700_writereg (state, 0x0f, 0x00);
  230. return 0;
  231. }
  232. static int cx22700_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  233. {
  234. struct cx22700_state* state = fe->demodulator_priv;
  235. cx22700_writereg (state, 0x00, 0x02); /* XXX CHECKME: soft reset*/
  236. cx22700_writereg (state, 0x00, 0x00);
  237. if (fe->ops.tuner_ops.set_params) {
  238. fe->ops.tuner_ops.set_params(fe, p);
  239. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  240. }
  241. cx22700_set_inversion (state, p->inversion);
  242. cx22700_set_tps (state, &p->u.ofdm);
  243. cx22700_writereg (state, 0x37, 0x01); /* PAL loop filter off */
  244. cx22700_writereg (state, 0x00, 0x01); /* restart acquire */
  245. return 0;
  246. }
  247. static int cx22700_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  248. {
  249. struct cx22700_state* state = fe->demodulator_priv;
  250. u8 reg09 = cx22700_readreg (state, 0x09);
  251. p->inversion = reg09 & 0x1 ? INVERSION_ON : INVERSION_OFF;
  252. return cx22700_get_tps (state, &p->u.ofdm);
  253. }
  254. static int cx22700_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  255. {
  256. struct cx22700_state* state = fe->demodulator_priv;
  257. if (enable) {
  258. return cx22700_writereg(state, 0x0a, 0x00);
  259. } else {
  260. return cx22700_writereg(state, 0x0a, 0x01);
  261. }
  262. }
  263. static int cx22700_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  264. {
  265. fesettings->min_delay_ms = 150;
  266. fesettings->step_size = 166667;
  267. fesettings->max_drift = 166667*2;
  268. return 0;
  269. }
  270. static void cx22700_release(struct dvb_frontend* fe)
  271. {
  272. struct cx22700_state* state = fe->demodulator_priv;
  273. kfree(state);
  274. }
  275. static struct dvb_frontend_ops cx22700_ops;
  276. struct dvb_frontend* cx22700_attach(const struct cx22700_config* config,
  277. struct i2c_adapter* i2c)
  278. {
  279. struct cx22700_state* state = NULL;
  280. /* allocate memory for the internal state */
  281. state = kzalloc(sizeof(struct cx22700_state), GFP_KERNEL);
  282. if (state == NULL) goto error;
  283. /* setup the state */
  284. state->config = config;
  285. state->i2c = i2c;
  286. /* check if the demod is there */
  287. if (cx22700_readreg(state, 0x07) < 0) goto error;
  288. /* create dvb_frontend */
  289. memcpy(&state->frontend.ops, &cx22700_ops, sizeof(struct dvb_frontend_ops));
  290. state->frontend.demodulator_priv = state;
  291. return &state->frontend;
  292. error:
  293. kfree(state);
  294. return NULL;
  295. }
  296. static struct dvb_frontend_ops cx22700_ops = {
  297. .info = {
  298. .name = "Conexant CX22700 DVB-T",
  299. .type = FE_OFDM,
  300. .frequency_min = 470000000,
  301. .frequency_max = 860000000,
  302. .frequency_stepsize = 166667,
  303. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  304. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  305. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  306. FE_CAN_RECOVER
  307. },
  308. .release = cx22700_release,
  309. .init = cx22700_init,
  310. .i2c_gate_ctrl = cx22700_i2c_gate_ctrl,
  311. .set_frontend = cx22700_set_frontend,
  312. .get_frontend = cx22700_get_frontend,
  313. .get_tune_settings = cx22700_get_tune_settings,
  314. .read_status = cx22700_read_status,
  315. .read_ber = cx22700_read_ber,
  316. .read_signal_strength = cx22700_read_signal_strength,
  317. .read_snr = cx22700_read_snr,
  318. .read_ucblocks = cx22700_read_ucblocks,
  319. };
  320. module_param(debug, int, 0644);
  321. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  322. MODULE_DESCRIPTION("Conexant CX22700 DVB-T Demodulator driver");
  323. MODULE_AUTHOR("Holger Waechtler");
  324. MODULE_LICENSE("GPL");
  325. EXPORT_SYMBOL(cx22700_attach);