e4000.c 9.9 KB

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