tda18218.c 8.7 KB

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