e4000.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Elonics E4000 silicon tuner driver
  3. *
  4. * Copyright (C) 2012 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 along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "e4000_priv.h"
  21. /* write multiple registers */
  22. static int e4000_wr_regs(struct e4000_priv *priv, u8 reg, u8 *val, int len)
  23. {
  24. int ret;
  25. u8 buf[1 + len];
  26. struct i2c_msg msg[1] = {
  27. {
  28. .addr = priv->cfg->i2c_addr,
  29. .flags = 0,
  30. .len = sizeof(buf),
  31. .buf = buf,
  32. }
  33. };
  34. buf[0] = reg;
  35. memcpy(&buf[1], val, len);
  36. ret = i2c_transfer(priv->i2c, msg, 1);
  37. if (ret == 1) {
  38. ret = 0;
  39. } else {
  40. dev_warn(&priv->i2c->dev,
  41. "%s: i2c wr failed=%d reg=%02x len=%d\n",
  42. KBUILD_MODNAME, ret, reg, len);
  43. ret = -EREMOTEIO;
  44. }
  45. return ret;
  46. }
  47. /* read multiple registers */
  48. static int e4000_rd_regs(struct e4000_priv *priv, u8 reg, u8 *val, int len)
  49. {
  50. int ret;
  51. u8 buf[len];
  52. struct i2c_msg msg[2] = {
  53. {
  54. .addr = priv->cfg->i2c_addr,
  55. .flags = 0,
  56. .len = 1,
  57. .buf = &reg,
  58. }, {
  59. .addr = priv->cfg->i2c_addr,
  60. .flags = I2C_M_RD,
  61. .len = sizeof(buf),
  62. .buf = buf,
  63. }
  64. };
  65. ret = i2c_transfer(priv->i2c, msg, 2);
  66. if (ret == 2) {
  67. memcpy(val, buf, len);
  68. ret = 0;
  69. } else {
  70. dev_warn(&priv->i2c->dev,
  71. "%s: i2c rd failed=%d reg=%02x len=%d\n",
  72. KBUILD_MODNAME, ret, reg, len);
  73. ret = -EREMOTEIO;
  74. }
  75. return ret;
  76. }
  77. /* write single register */
  78. static int e4000_wr_reg(struct e4000_priv *priv, u8 reg, u8 val)
  79. {
  80. return e4000_wr_regs(priv, reg, &val, 1);
  81. }
  82. /* read single register */
  83. static int e4000_rd_reg(struct e4000_priv *priv, u8 reg, u8 *val)
  84. {
  85. return e4000_rd_regs(priv, reg, val, 1);
  86. }
  87. static int e4000_init(struct dvb_frontend *fe)
  88. {
  89. struct e4000_priv *priv = fe->tuner_priv;
  90. int ret;
  91. dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
  92. if (fe->ops.i2c_gate_ctrl)
  93. fe->ops.i2c_gate_ctrl(fe, 1);
  94. /* dummy I2C to ensure I2C wakes up */
  95. ret = e4000_wr_reg(priv, 0x02, 0x40);
  96. /* reset */
  97. ret = e4000_wr_reg(priv, 0x00, 0x01);
  98. if (ret < 0)
  99. goto err;
  100. /* disable output clock */
  101. ret = e4000_wr_reg(priv, 0x06, 0x00);
  102. if (ret < 0)
  103. goto err;
  104. ret = e4000_wr_reg(priv, 0x7a, 0x96);
  105. if (ret < 0)
  106. goto err;
  107. /* configure gains */
  108. ret = e4000_wr_regs(priv, 0x7e, "\x01\xfe", 2);
  109. if (ret < 0)
  110. goto err;
  111. ret = e4000_wr_reg(priv, 0x82, 0x00);
  112. if (ret < 0)
  113. goto err;
  114. ret = e4000_wr_reg(priv, 0x24, 0x05);
  115. if (ret < 0)
  116. goto err;
  117. ret = e4000_wr_regs(priv, 0x87, "\x20\x01", 2);
  118. if (ret < 0)
  119. goto err;
  120. ret = e4000_wr_regs(priv, 0x9f, "\x7f\x07", 2);
  121. if (ret < 0)
  122. goto err;
  123. /* DC offset control */
  124. ret = e4000_wr_reg(priv, 0x2d, 0x1f);
  125. if (ret < 0)
  126. goto err;
  127. ret = e4000_wr_regs(priv, 0x70, "\x01\x01", 2);
  128. if (ret < 0)
  129. goto err;
  130. /* gain control */
  131. ret = e4000_wr_reg(priv, 0x1a, 0x17);
  132. if (ret < 0)
  133. goto err;
  134. ret = e4000_wr_reg(priv, 0x1f, 0x1a);
  135. if (ret < 0)
  136. goto err;
  137. if (fe->ops.i2c_gate_ctrl)
  138. fe->ops.i2c_gate_ctrl(fe, 0);
  139. return 0;
  140. err:
  141. if (fe->ops.i2c_gate_ctrl)
  142. fe->ops.i2c_gate_ctrl(fe, 0);
  143. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  144. return ret;
  145. }
  146. static int e4000_sleep(struct dvb_frontend *fe)
  147. {
  148. struct e4000_priv *priv = fe->tuner_priv;
  149. int ret;
  150. dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
  151. if (fe->ops.i2c_gate_ctrl)
  152. fe->ops.i2c_gate_ctrl(fe, 1);
  153. ret = e4000_wr_reg(priv, 0x00, 0x00);
  154. if (ret < 0)
  155. goto err;
  156. if (fe->ops.i2c_gate_ctrl)
  157. fe->ops.i2c_gate_ctrl(fe, 0);
  158. return 0;
  159. err:
  160. if (fe->ops.i2c_gate_ctrl)
  161. fe->ops.i2c_gate_ctrl(fe, 0);
  162. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  163. return ret;
  164. }
  165. static int e4000_set_params(struct dvb_frontend *fe)
  166. {
  167. struct e4000_priv *priv = fe->tuner_priv;
  168. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  169. int ret, i, sigma_delta;
  170. unsigned int f_vco;
  171. u8 buf[5], i_data[4], q_data[4];
  172. dev_dbg(&priv->i2c->dev,
  173. "%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
  174. __func__, c->delivery_system, c->frequency,
  175. c->bandwidth_hz);
  176. if (fe->ops.i2c_gate_ctrl)
  177. fe->ops.i2c_gate_ctrl(fe, 1);
  178. /* gain control manual */
  179. ret = e4000_wr_reg(priv, 0x1a, 0x00);
  180. if (ret < 0)
  181. goto err;
  182. /* PLL */
  183. for (i = 0; i < ARRAY_SIZE(e4000_pll_lut); i++) {
  184. if (c->frequency <= e4000_pll_lut[i].freq)
  185. break;
  186. }
  187. if (i == ARRAY_SIZE(e4000_pll_lut))
  188. goto err;
  189. /*
  190. * Note: Currently f_vco overflows when c->frequency is 1 073 741 824 Hz
  191. * or more.
  192. */
  193. f_vco = c->frequency * e4000_pll_lut[i].mul;
  194. sigma_delta = 0x10000UL * (f_vco % priv->cfg->clock) / priv->cfg->clock;
  195. buf[0] = f_vco / priv->cfg->clock;
  196. buf[1] = (sigma_delta >> 0) & 0xff;
  197. buf[2] = (sigma_delta >> 8) & 0xff;
  198. buf[3] = 0x00;
  199. buf[4] = e4000_pll_lut[i].div;
  200. dev_dbg(&priv->i2c->dev, "%s: f_vco=%u pll div=%d sigma_delta=%04x\n",
  201. __func__, f_vco, buf[0], sigma_delta);
  202. ret = e4000_wr_regs(priv, 0x09, buf, 5);
  203. if (ret < 0)
  204. goto err;
  205. /* LNA filter (RF filter) */
  206. for (i = 0; i < ARRAY_SIZE(e400_lna_filter_lut); i++) {
  207. if (c->frequency <= e400_lna_filter_lut[i].freq)
  208. break;
  209. }
  210. if (i == ARRAY_SIZE(e400_lna_filter_lut))
  211. goto err;
  212. ret = e4000_wr_reg(priv, 0x10, e400_lna_filter_lut[i].val);
  213. if (ret < 0)
  214. goto err;
  215. /* IF filters */
  216. for (i = 0; i < ARRAY_SIZE(e4000_if_filter_lut); i++) {
  217. if (c->bandwidth_hz <= e4000_if_filter_lut[i].freq)
  218. break;
  219. }
  220. if (i == ARRAY_SIZE(e4000_if_filter_lut))
  221. goto err;
  222. buf[0] = e4000_if_filter_lut[i].reg11_val;
  223. buf[1] = e4000_if_filter_lut[i].reg12_val;
  224. ret = e4000_wr_regs(priv, 0x11, buf, 2);
  225. if (ret < 0)
  226. goto err;
  227. /* frequency band */
  228. for (i = 0; i < ARRAY_SIZE(e4000_band_lut); i++) {
  229. if (c->frequency <= e4000_band_lut[i].freq)
  230. break;
  231. }
  232. if (i == ARRAY_SIZE(e4000_band_lut))
  233. goto err;
  234. ret = e4000_wr_reg(priv, 0x07, e4000_band_lut[i].reg07_val);
  235. if (ret < 0)
  236. goto err;
  237. ret = e4000_wr_reg(priv, 0x78, e4000_band_lut[i].reg78_val);
  238. if (ret < 0)
  239. goto err;
  240. /* DC offset */
  241. for (i = 0; i < 4; i++) {
  242. if (i == 0)
  243. ret = e4000_wr_regs(priv, 0x15, "\x00\x7e\x24", 3);
  244. else if (i == 1)
  245. ret = e4000_wr_regs(priv, 0x15, "\x00\x7f", 2);
  246. else if (i == 2)
  247. ret = e4000_wr_regs(priv, 0x15, "\x01", 1);
  248. else
  249. ret = e4000_wr_regs(priv, 0x16, "\x7e", 1);
  250. if (ret < 0)
  251. goto err;
  252. ret = e4000_wr_reg(priv, 0x29, 0x01);
  253. if (ret < 0)
  254. goto err;
  255. ret = e4000_rd_regs(priv, 0x2a, buf, 3);
  256. if (ret < 0)
  257. goto err;
  258. i_data[i] = (((buf[2] >> 0) & 0x3) << 6) | (buf[0] & 0x3f);
  259. q_data[i] = (((buf[2] >> 4) & 0x3) << 6) | (buf[1] & 0x3f);
  260. }
  261. swap(q_data[2], q_data[3]);
  262. swap(i_data[2], i_data[3]);
  263. ret = e4000_wr_regs(priv, 0x50, q_data, 4);
  264. if (ret < 0)
  265. goto err;
  266. ret = e4000_wr_regs(priv, 0x60, i_data, 4);
  267. if (ret < 0)
  268. goto err;
  269. /* gain control auto */
  270. ret = e4000_wr_reg(priv, 0x1a, 0x17);
  271. if (ret < 0)
  272. goto err;
  273. if (fe->ops.i2c_gate_ctrl)
  274. fe->ops.i2c_gate_ctrl(fe, 0);
  275. return 0;
  276. err:
  277. if (fe->ops.i2c_gate_ctrl)
  278. fe->ops.i2c_gate_ctrl(fe, 0);
  279. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  280. return ret;
  281. }
  282. static int e4000_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  283. {
  284. struct e4000_priv *priv = fe->tuner_priv;
  285. dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
  286. *frequency = 0; /* Zero-IF */
  287. return 0;
  288. }
  289. static int e4000_release(struct dvb_frontend *fe)
  290. {
  291. struct e4000_priv *priv = fe->tuner_priv;
  292. dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
  293. kfree(fe->tuner_priv);
  294. return 0;
  295. }
  296. static const struct dvb_tuner_ops e4000_tuner_ops = {
  297. .info = {
  298. .name = "Elonics E4000",
  299. .frequency_min = 174000000,
  300. .frequency_max = 862000000,
  301. },
  302. .release = e4000_release,
  303. .init = e4000_init,
  304. .sleep = e4000_sleep,
  305. .set_params = e4000_set_params,
  306. .get_if_frequency = e4000_get_if_frequency,
  307. };
  308. struct dvb_frontend *e4000_attach(struct dvb_frontend *fe,
  309. struct i2c_adapter *i2c, const struct e4000_config *cfg)
  310. {
  311. struct e4000_priv *priv;
  312. int ret;
  313. u8 chip_id;
  314. if (fe->ops.i2c_gate_ctrl)
  315. fe->ops.i2c_gate_ctrl(fe, 1);
  316. priv = kzalloc(sizeof(struct e4000_priv), GFP_KERNEL);
  317. if (!priv) {
  318. ret = -ENOMEM;
  319. dev_err(&i2c->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
  320. goto err;
  321. }
  322. priv->cfg = cfg;
  323. priv->i2c = i2c;
  324. /* check if the tuner is there */
  325. ret = e4000_rd_reg(priv, 0x02, &chip_id);
  326. if (ret < 0)
  327. goto err;
  328. dev_dbg(&priv->i2c->dev, "%s: chip_id=%02x\n", __func__, chip_id);
  329. if (chip_id != 0x40)
  330. goto err;
  331. /* put sleep as chip seems to be in normal mode by default */
  332. ret = e4000_wr_reg(priv, 0x00, 0x00);
  333. if (ret < 0)
  334. goto err;
  335. dev_info(&priv->i2c->dev,
  336. "%s: Elonics E4000 successfully identified\n",
  337. KBUILD_MODNAME);
  338. fe->tuner_priv = priv;
  339. memcpy(&fe->ops.tuner_ops, &e4000_tuner_ops,
  340. sizeof(struct dvb_tuner_ops));
  341. if (fe->ops.i2c_gate_ctrl)
  342. fe->ops.i2c_gate_ctrl(fe, 0);
  343. return fe;
  344. err:
  345. if (fe->ops.i2c_gate_ctrl)
  346. fe->ops.i2c_gate_ctrl(fe, 0);
  347. dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
  348. kfree(priv);
  349. return NULL;
  350. }
  351. EXPORT_SYMBOL(e4000_attach);
  352. MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver");
  353. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  354. MODULE_LICENSE("GPL");