e4000.c 9.5 KB

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