cx22700.c 10 KB

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