e4000.c 9.6 KB

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