tda18218.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * NXP TDA18218HN silicon tuner driver
  3. *
  4. * Copyright (C) 2010 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. #include "tda18218_priv.h"
  17. /* Max transfer size done by I2C transfer functions */
  18. #define MAX_XFER_SIZE 64
  19. /* write multiple registers */
  20. static int tda18218_wr_regs(struct tda18218_priv *priv, u8 reg, u8 *val, u8 len)
  21. {
  22. int ret = 0, len2, remaining;
  23. u8 buf[MAX_XFER_SIZE];
  24. struct i2c_msg msg[1] = {
  25. {
  26. .addr = priv->cfg->i2c_address,
  27. .flags = 0,
  28. .buf = buf,
  29. }
  30. };
  31. if (1 + len > sizeof(buf)) {
  32. dev_warn(&priv->i2c->dev,
  33. "%s: i2c wr reg=%04x: len=%d is too big!\n",
  34. KBUILD_MODNAME, reg, len);
  35. return -EINVAL;
  36. }
  37. for (remaining = len; remaining > 0;
  38. remaining -= (priv->cfg->i2c_wr_max - 1)) {
  39. len2 = remaining;
  40. if (len2 > (priv->cfg->i2c_wr_max - 1))
  41. len2 = (priv->cfg->i2c_wr_max - 1);
  42. msg[0].len = 1 + len2;
  43. buf[0] = reg + len - remaining;
  44. memcpy(&buf[1], &val[len - remaining], len2);
  45. ret = i2c_transfer(priv->i2c, msg, 1);
  46. if (ret != 1)
  47. break;
  48. }
  49. if (ret == 1) {
  50. ret = 0;
  51. } else {
  52. dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%02x " \
  53. "len=%d\n", KBUILD_MODNAME, ret, reg, len);
  54. ret = -EREMOTEIO;
  55. }
  56. return ret;
  57. }
  58. /* read multiple registers */
  59. static int tda18218_rd_regs(struct tda18218_priv *priv, u8 reg, u8 *val, u8 len)
  60. {
  61. int ret;
  62. u8 buf[MAX_XFER_SIZE]; /* we must start read always from reg 0x00 */
  63. struct i2c_msg msg[2] = {
  64. {
  65. .addr = priv->cfg->i2c_address,
  66. .flags = 0,
  67. .len = 1,
  68. .buf = "\x00",
  69. }, {
  70. .addr = priv->cfg->i2c_address,
  71. .flags = I2C_M_RD,
  72. .len = reg + len,
  73. .buf = buf,
  74. }
  75. };
  76. if (reg + len > sizeof(buf)) {
  77. dev_warn(&priv->i2c->dev,
  78. "%s: i2c wr reg=%04x: len=%d is too big!\n",
  79. KBUILD_MODNAME, reg, len);
  80. return -EINVAL;
  81. }
  82. ret = i2c_transfer(priv->i2c, msg, 2);
  83. if (ret == 2) {
  84. memcpy(val, &buf[reg], len);
  85. ret = 0;
  86. } else {
  87. dev_warn(&priv->i2c->dev, "%s: i2c rd failed=%d reg=%02x " \
  88. "len=%d\n", KBUILD_MODNAME, ret, reg, len);
  89. ret = -EREMOTEIO;
  90. }
  91. return ret;
  92. }
  93. /* write single register */
  94. static int tda18218_wr_reg(struct tda18218_priv *priv, u8 reg, u8 val)
  95. {
  96. return tda18218_wr_regs(priv, reg, &val, 1);
  97. }
  98. /* read single register */
  99. static int tda18218_rd_reg(struct tda18218_priv *priv, u8 reg, u8 *val)
  100. {
  101. return tda18218_rd_regs(priv, reg, val, 1);
  102. }
  103. static int tda18218_set_params(struct dvb_frontend *fe)
  104. {
  105. struct tda18218_priv *priv = fe->tuner_priv;
  106. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  107. u32 bw = c->bandwidth_hz;
  108. int ret;
  109. u8 buf[3], i, BP_Filter, LP_Fc;
  110. u32 LO_Frac;
  111. /* TODO: find out correct AGC algorithm */
  112. u8 agc[][2] = {
  113. { R20_AGC11, 0x60 },
  114. { R23_AGC21, 0x02 },
  115. { R20_AGC11, 0xa0 },
  116. { R23_AGC21, 0x09 },
  117. { R20_AGC11, 0xe0 },
  118. { R23_AGC21, 0x0c },
  119. { R20_AGC11, 0x40 },
  120. { R23_AGC21, 0x01 },
  121. { R20_AGC11, 0x80 },
  122. { R23_AGC21, 0x08 },
  123. { R20_AGC11, 0xc0 },
  124. { R23_AGC21, 0x0b },
  125. { R24_AGC22, 0x1c },
  126. { R24_AGC22, 0x0c },
  127. };
  128. if (fe->ops.i2c_gate_ctrl)
  129. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  130. /* low-pass filter cut-off frequency */
  131. if (bw <= 6000000) {
  132. LP_Fc = 0;
  133. priv->if_frequency = 3000000;
  134. } else if (bw <= 7000000) {
  135. LP_Fc = 1;
  136. priv->if_frequency = 3500000;
  137. } else {
  138. LP_Fc = 2;
  139. priv->if_frequency = 4000000;
  140. }
  141. LO_Frac = c->frequency + priv->if_frequency;
  142. /* band-pass filter */
  143. if (LO_Frac < 188000000)
  144. BP_Filter = 3;
  145. else if (LO_Frac < 253000000)
  146. BP_Filter = 4;
  147. else if (LO_Frac < 343000000)
  148. BP_Filter = 5;
  149. else
  150. BP_Filter = 6;
  151. buf[0] = (priv->regs[R1A_IF1] & ~7) | BP_Filter; /* BP_Filter */
  152. buf[1] = (priv->regs[R1B_IF2] & ~3) | LP_Fc; /* LP_Fc */
  153. buf[2] = priv->regs[R1C_AGC2B];
  154. ret = tda18218_wr_regs(priv, R1A_IF1, buf, 3);
  155. if (ret)
  156. goto error;
  157. buf[0] = (LO_Frac / 1000) >> 12; /* LO_Frac_0 */
  158. buf[1] = (LO_Frac / 1000) >> 4; /* LO_Frac_1 */
  159. buf[2] = (LO_Frac / 1000) << 4 |
  160. (priv->regs[R0C_MD5] & 0x0f); /* LO_Frac_2 */
  161. ret = tda18218_wr_regs(priv, R0A_MD3, buf, 3);
  162. if (ret)
  163. goto error;
  164. buf[0] = priv->regs[R0F_MD8] | (1 << 6); /* Freq_prog_Start */
  165. ret = tda18218_wr_regs(priv, R0F_MD8, buf, 1);
  166. if (ret)
  167. goto error;
  168. buf[0] = priv->regs[R0F_MD8] & ~(1 << 6); /* Freq_prog_Start */
  169. ret = tda18218_wr_regs(priv, R0F_MD8, buf, 1);
  170. if (ret)
  171. goto error;
  172. /* trigger AGC */
  173. for (i = 0; i < ARRAY_SIZE(agc); i++) {
  174. ret = tda18218_wr_reg(priv, agc[i][0], agc[i][1]);
  175. if (ret)
  176. goto error;
  177. }
  178. error:
  179. if (fe->ops.i2c_gate_ctrl)
  180. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  181. if (ret)
  182. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  183. return ret;
  184. }
  185. static int tda18218_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  186. {
  187. struct tda18218_priv *priv = fe->tuner_priv;
  188. *frequency = priv->if_frequency;
  189. dev_dbg(&priv->i2c->dev, "%s: if_frequency=%d\n", __func__, *frequency);
  190. return 0;
  191. }
  192. static int tda18218_sleep(struct dvb_frontend *fe)
  193. {
  194. struct tda18218_priv *priv = fe->tuner_priv;
  195. int ret;
  196. if (fe->ops.i2c_gate_ctrl)
  197. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  198. /* standby */
  199. ret = tda18218_wr_reg(priv, R17_PD1, priv->regs[R17_PD1] | (1 << 0));
  200. if (fe->ops.i2c_gate_ctrl)
  201. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  202. if (ret)
  203. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  204. return ret;
  205. }
  206. static int tda18218_init(struct dvb_frontend *fe)
  207. {
  208. struct tda18218_priv *priv = fe->tuner_priv;
  209. int ret;
  210. /* TODO: calibrations */
  211. if (fe->ops.i2c_gate_ctrl)
  212. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  213. ret = tda18218_wr_regs(priv, R00_ID, priv->regs, TDA18218_NUM_REGS);
  214. if (fe->ops.i2c_gate_ctrl)
  215. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  216. if (ret)
  217. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  218. return ret;
  219. }
  220. static void tda18218_release(struct dvb_frontend *fe)
  221. {
  222. kfree(fe->tuner_priv);
  223. fe->tuner_priv = NULL;
  224. }
  225. static const struct dvb_tuner_ops tda18218_tuner_ops = {
  226. .info = {
  227. .name = "NXP TDA18218",
  228. .frequency_min_hz = 174 * MHz,
  229. .frequency_max_hz = 864 * MHz,
  230. .frequency_step_hz = 1 * kHz,
  231. },
  232. .release = tda18218_release,
  233. .init = tda18218_init,
  234. .sleep = tda18218_sleep,
  235. .set_params = tda18218_set_params,
  236. .get_if_frequency = tda18218_get_if_frequency,
  237. };
  238. struct dvb_frontend *tda18218_attach(struct dvb_frontend *fe,
  239. struct i2c_adapter *i2c, struct tda18218_config *cfg)
  240. {
  241. struct tda18218_priv *priv = NULL;
  242. u8 val;
  243. int ret;
  244. /* chip default registers values */
  245. static u8 def_regs[] = {
  246. 0xc0, 0x88, 0x00, 0x8e, 0x03, 0x00, 0x00, 0xd0, 0x00, 0x40,
  247. 0x00, 0x00, 0x07, 0xff, 0x84, 0x09, 0x00, 0x13, 0x00, 0x00,
  248. 0x01, 0x84, 0x09, 0xf0, 0x19, 0x0a, 0x8e, 0x69, 0x98, 0x01,
  249. 0x00, 0x58, 0x10, 0x40, 0x8c, 0x00, 0x0c, 0x48, 0x85, 0xc9,
  250. 0xa7, 0x00, 0x00, 0x00, 0x30, 0x81, 0x80, 0x00, 0x39, 0x00,
  251. 0x8a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf6, 0xf6
  252. };
  253. priv = kzalloc(sizeof(struct tda18218_priv), GFP_KERNEL);
  254. if (priv == NULL)
  255. return NULL;
  256. priv->cfg = cfg;
  257. priv->i2c = i2c;
  258. fe->tuner_priv = priv;
  259. if (fe->ops.i2c_gate_ctrl)
  260. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  261. /* check if the tuner is there */
  262. ret = tda18218_rd_reg(priv, R00_ID, &val);
  263. if (!ret)
  264. dev_dbg(&priv->i2c->dev, "%s: chip id=%02x\n", __func__, val);
  265. if (ret || val != def_regs[R00_ID]) {
  266. kfree(priv);
  267. return NULL;
  268. }
  269. dev_info(&priv->i2c->dev,
  270. "%s: NXP TDA18218HN successfully identified\n",
  271. KBUILD_MODNAME);
  272. memcpy(&fe->ops.tuner_ops, &tda18218_tuner_ops,
  273. sizeof(struct dvb_tuner_ops));
  274. memcpy(priv->regs, def_regs, sizeof(def_regs));
  275. /* loop-through enabled chip default register values */
  276. if (priv->cfg->loop_through) {
  277. priv->regs[R17_PD1] = 0xb0;
  278. priv->regs[R18_PD2] = 0x59;
  279. }
  280. /* standby */
  281. ret = tda18218_wr_reg(priv, R17_PD1, priv->regs[R17_PD1] | (1 << 0));
  282. if (ret)
  283. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  284. if (fe->ops.i2c_gate_ctrl)
  285. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  286. return fe;
  287. }
  288. EXPORT_SYMBOL(tda18218_attach);
  289. MODULE_DESCRIPTION("NXP TDA18218HN silicon tuner driver");
  290. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  291. MODULE_LICENSE("GPL");