ec100.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * E3C EC100 demodulator driver
  3. *
  4. * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  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. */
  21. #include "dvb_frontend.h"
  22. #include "ec100_priv.h"
  23. #include "ec100.h"
  24. int ec100_debug;
  25. module_param_named(debug, ec100_debug, int, 0644);
  26. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  27. struct ec100_state {
  28. struct i2c_adapter *i2c;
  29. struct dvb_frontend frontend;
  30. struct ec100_config config;
  31. u16 ber;
  32. };
  33. /* write single register */
  34. static int ec100_write_reg(struct ec100_state *state, u8 reg, u8 val)
  35. {
  36. u8 buf[2] = {reg, val};
  37. struct i2c_msg msg = {
  38. .addr = state->config.demod_address,
  39. .flags = 0,
  40. .len = 2,
  41. .buf = buf};
  42. if (i2c_transfer(state->i2c, &msg, 1) != 1) {
  43. warn("I2C write failed reg:%02x", reg);
  44. return -EREMOTEIO;
  45. }
  46. return 0;
  47. }
  48. /* read single register */
  49. static int ec100_read_reg(struct ec100_state *state, u8 reg, u8 *val)
  50. {
  51. struct i2c_msg msg[2] = {
  52. {
  53. .addr = state->config.demod_address,
  54. .flags = 0,
  55. .len = 1,
  56. .buf = &reg
  57. }, {
  58. .addr = state->config.demod_address,
  59. .flags = I2C_M_RD,
  60. .len = 1,
  61. .buf = val
  62. }
  63. };
  64. if (i2c_transfer(state->i2c, msg, 2) != 2) {
  65. warn("I2C read failed reg:%02x", reg);
  66. return -EREMOTEIO;
  67. }
  68. return 0;
  69. }
  70. static int ec100_set_frontend(struct dvb_frontend *fe,
  71. struct dvb_frontend_parameters *params)
  72. {
  73. struct ec100_state *state = fe->demodulator_priv;
  74. int ret;
  75. u8 tmp, tmp2;
  76. deb_info("%s: freq:%d bw:%d\n", __func__, params->frequency,
  77. params->u.ofdm.bandwidth);
  78. /* program tuner */
  79. if (fe->ops.tuner_ops.set_params)
  80. fe->ops.tuner_ops.set_params(fe, params);
  81. ret = ec100_write_reg(state, 0x04, 0x06);
  82. if (ret)
  83. goto error;
  84. ret = ec100_write_reg(state, 0x67, 0x58);
  85. if (ret)
  86. goto error;
  87. ret = ec100_write_reg(state, 0x05, 0x18);
  88. if (ret)
  89. goto error;
  90. /* reg/bw | 6 | 7 | 8
  91. -------+------+------+------
  92. A 0x1b | 0xa1 | 0xe7 | 0x2c
  93. A 0x1c | 0x55 | 0x63 | 0x72
  94. -------+------+------+------
  95. B 0x1b | 0xb7 | 0x00 | 0x49
  96. B 0x1c | 0x55 | 0x64 | 0x72 */
  97. switch (params->u.ofdm.bandwidth) {
  98. case BANDWIDTH_6_MHZ:
  99. tmp = 0xb7;
  100. tmp2 = 0x55;
  101. break;
  102. case BANDWIDTH_7_MHZ:
  103. tmp = 0x00;
  104. tmp2 = 0x64;
  105. break;
  106. case BANDWIDTH_8_MHZ:
  107. default:
  108. tmp = 0x49;
  109. tmp2 = 0x72;
  110. }
  111. ret = ec100_write_reg(state, 0x1b, tmp);
  112. if (ret)
  113. goto error;
  114. ret = ec100_write_reg(state, 0x1c, tmp2);
  115. if (ret)
  116. goto error;
  117. ret = ec100_write_reg(state, 0x0c, 0xbb); /* if freq */
  118. if (ret)
  119. goto error;
  120. ret = ec100_write_reg(state, 0x0d, 0x31); /* if freq */
  121. if (ret)
  122. goto error;
  123. ret = ec100_write_reg(state, 0x08, 0x24);
  124. if (ret)
  125. goto error;
  126. ret = ec100_write_reg(state, 0x00, 0x00); /* go */
  127. if (ret)
  128. goto error;
  129. ret = ec100_write_reg(state, 0x00, 0x20); /* go */
  130. if (ret)
  131. goto error;
  132. return ret;
  133. error:
  134. deb_info("%s: failed:%d\n", __func__, ret);
  135. return ret;
  136. }
  137. static int ec100_get_tune_settings(struct dvb_frontend *fe,
  138. struct dvb_frontend_tune_settings *fesettings)
  139. {
  140. fesettings->min_delay_ms = 300;
  141. fesettings->step_size = 0;
  142. fesettings->max_drift = 0;
  143. return 0;
  144. }
  145. static int ec100_read_status(struct dvb_frontend *fe, fe_status_t *status)
  146. {
  147. struct ec100_state *state = fe->demodulator_priv;
  148. int ret;
  149. u8 tmp;
  150. *status = 0;
  151. ret = ec100_read_reg(state, 0x42, &tmp);
  152. if (ret)
  153. goto error;
  154. if (tmp & 0x80) {
  155. /* bit7 set - have lock */
  156. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
  157. FE_HAS_SYNC | FE_HAS_LOCK;
  158. } else {
  159. ret = ec100_read_reg(state, 0x01, &tmp);
  160. if (ret)
  161. goto error;
  162. if (tmp & 0x10) {
  163. /* bit4 set - have signal */
  164. *status |= FE_HAS_SIGNAL;
  165. if (!(tmp & 0x01)) {
  166. /* bit0 clear - have ~valid signal */
  167. *status |= FE_HAS_CARRIER | FE_HAS_VITERBI;
  168. }
  169. }
  170. }
  171. return ret;
  172. error:
  173. deb_info("%s: failed:%d\n", __func__, ret);
  174. return ret;
  175. }
  176. static int ec100_read_ber(struct dvb_frontend *fe, u32 *ber)
  177. {
  178. struct ec100_state *state = fe->demodulator_priv;
  179. int ret;
  180. u8 tmp, tmp2;
  181. u16 ber2;
  182. *ber = 0;
  183. ret = ec100_read_reg(state, 0x65, &tmp);
  184. if (ret)
  185. goto error;
  186. ret = ec100_read_reg(state, 0x66, &tmp2);
  187. if (ret)
  188. goto error;
  189. ber2 = (tmp2 << 8) | tmp;
  190. /* if counter overflow or clear */
  191. if (ber2 < state->ber)
  192. *ber = ber2;
  193. else
  194. *ber = ber2 - state->ber;
  195. state->ber = ber2;
  196. return ret;
  197. error:
  198. deb_info("%s: failed:%d\n", __func__, ret);
  199. return ret;
  200. }
  201. static int ec100_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  202. {
  203. struct ec100_state *state = fe->demodulator_priv;
  204. int ret;
  205. u8 tmp;
  206. ret = ec100_read_reg(state, 0x24, &tmp);
  207. if (ret) {
  208. *strength = 0;
  209. goto error;
  210. }
  211. *strength = ((tmp << 8) | tmp);
  212. return ret;
  213. error:
  214. deb_info("%s: failed:%d\n", __func__, ret);
  215. return ret;
  216. }
  217. static int ec100_read_snr(struct dvb_frontend *fe, u16 *snr)
  218. {
  219. *snr = 0;
  220. return 0;
  221. }
  222. static int ec100_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  223. {
  224. *ucblocks = 0;
  225. return 0;
  226. }
  227. static void ec100_release(struct dvb_frontend *fe)
  228. {
  229. struct ec100_state *state = fe->demodulator_priv;
  230. kfree(state);
  231. }
  232. static struct dvb_frontend_ops ec100_ops;
  233. struct dvb_frontend *ec100_attach(const struct ec100_config *config,
  234. struct i2c_adapter *i2c)
  235. {
  236. int ret;
  237. struct ec100_state *state = NULL;
  238. u8 tmp;
  239. /* allocate memory for the internal state */
  240. state = kzalloc(sizeof(struct ec100_state), GFP_KERNEL);
  241. if (state == NULL)
  242. goto error;
  243. /* setup the state */
  244. state->i2c = i2c;
  245. memcpy(&state->config, config, sizeof(struct ec100_config));
  246. /* check if the demod is there */
  247. ret = ec100_read_reg(state, 0x33, &tmp);
  248. if (ret || tmp != 0x0b)
  249. goto error;
  250. /* create dvb_frontend */
  251. memcpy(&state->frontend.ops, &ec100_ops,
  252. sizeof(struct dvb_frontend_ops));
  253. state->frontend.demodulator_priv = state;
  254. return &state->frontend;
  255. error:
  256. kfree(state);
  257. return NULL;
  258. }
  259. EXPORT_SYMBOL(ec100_attach);
  260. static struct dvb_frontend_ops ec100_ops = {
  261. .info = {
  262. .name = "E3C EC100 DVB-T",
  263. .type = FE_OFDM,
  264. .caps =
  265. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  266. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  267. FE_CAN_QPSK | FE_CAN_QAM_16 |
  268. FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  269. FE_CAN_TRANSMISSION_MODE_AUTO |
  270. FE_CAN_GUARD_INTERVAL_AUTO |
  271. FE_CAN_HIERARCHY_AUTO |
  272. FE_CAN_MUTE_TS
  273. },
  274. .release = ec100_release,
  275. .set_frontend = ec100_set_frontend,
  276. .get_tune_settings = ec100_get_tune_settings,
  277. .read_status = ec100_read_status,
  278. .read_ber = ec100_read_ber,
  279. .read_signal_strength = ec100_read_signal_strength,
  280. .read_snr = ec100_read_snr,
  281. .read_ucblocks = ec100_read_ucblocks,
  282. };
  283. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  284. MODULE_DESCRIPTION("E3C EC100 DVB-T demodulator driver");
  285. MODULE_LICENSE("GPL");