ec100.c 7.3 KB

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