tda18218.c 8.0 KB

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