tda18218.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. {
  99. struct tda18218_priv *priv = fe->tuner_priv;
  100. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  101. u32 bw = c->bandwidth_hz;
  102. int ret;
  103. u8 buf[3], i, BP_Filter, LP_Fc;
  104. u32 LO_Frac;
  105. /* TODO: find out correct AGC algorithm */
  106. u8 agc[][2] = {
  107. { R20_AGC11, 0x60 },
  108. { R23_AGC21, 0x02 },
  109. { R20_AGC11, 0xa0 },
  110. { R23_AGC21, 0x09 },
  111. { R20_AGC11, 0xe0 },
  112. { R23_AGC21, 0x0c },
  113. { R20_AGC11, 0x40 },
  114. { R23_AGC21, 0x01 },
  115. { R20_AGC11, 0x80 },
  116. { R23_AGC21, 0x08 },
  117. { R20_AGC11, 0xc0 },
  118. { R23_AGC21, 0x0b },
  119. { R24_AGC22, 0x1c },
  120. { R24_AGC22, 0x0c },
  121. };
  122. if (fe->ops.i2c_gate_ctrl)
  123. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  124. /* low-pass filter cut-off frequency */
  125. if (bw <= 6000000) {
  126. LP_Fc = 0;
  127. priv->if_frequency = 3000000;
  128. } else if (bw <= 7000000) {
  129. LP_Fc = 1;
  130. priv->if_frequency = 3500000;
  131. } else {
  132. LP_Fc = 2;
  133. priv->if_frequency = 4000000;
  134. }
  135. LO_Frac = c->frequency + priv->if_frequency;
  136. /* band-pass filter */
  137. if (LO_Frac < 188000000)
  138. BP_Filter = 3;
  139. else if (LO_Frac < 253000000)
  140. BP_Filter = 4;
  141. else if (LO_Frac < 343000000)
  142. BP_Filter = 5;
  143. else
  144. BP_Filter = 6;
  145. buf[0] = (priv->regs[R1A_IF1] & ~7) | BP_Filter; /* BP_Filter */
  146. buf[1] = (priv->regs[R1B_IF2] & ~3) | LP_Fc; /* LP_Fc */
  147. buf[2] = priv->regs[R1C_AGC2B];
  148. ret = tda18218_wr_regs(priv, R1A_IF1, buf, 3);
  149. if (ret)
  150. goto error;
  151. buf[0] = (LO_Frac / 1000) >> 12; /* LO_Frac_0 */
  152. buf[1] = (LO_Frac / 1000) >> 4; /* LO_Frac_1 */
  153. buf[2] = (LO_Frac / 1000) << 4 |
  154. (priv->regs[R0C_MD5] & 0x0f); /* LO_Frac_2 */
  155. ret = tda18218_wr_regs(priv, R0A_MD3, buf, 3);
  156. if (ret)
  157. goto error;
  158. buf[0] = priv->regs[R0F_MD8] | (1 << 6); /* Freq_prog_Start */
  159. ret = tda18218_wr_regs(priv, R0F_MD8, buf, 1);
  160. if (ret)
  161. goto error;
  162. buf[0] = priv->regs[R0F_MD8] & ~(1 << 6); /* Freq_prog_Start */
  163. ret = tda18218_wr_regs(priv, R0F_MD8, buf, 1);
  164. if (ret)
  165. goto error;
  166. /* trigger AGC */
  167. for (i = 0; i < ARRAY_SIZE(agc); i++) {
  168. ret = tda18218_wr_reg(priv, agc[i][0], agc[i][1]);
  169. if (ret)
  170. goto error;
  171. }
  172. error:
  173. if (fe->ops.i2c_gate_ctrl)
  174. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  175. if (ret)
  176. dbg("%s: failed ret:%d", __func__, ret);
  177. return ret;
  178. }
  179. static int tda18218_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  180. {
  181. struct tda18218_priv *priv = fe->tuner_priv;
  182. *frequency = priv->if_frequency;
  183. dbg("%s: if=%d", __func__, *frequency);
  184. return 0;
  185. }
  186. static int tda18218_sleep(struct dvb_frontend *fe)
  187. {
  188. struct tda18218_priv *priv = fe->tuner_priv;
  189. int ret;
  190. if (fe->ops.i2c_gate_ctrl)
  191. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  192. /* standby */
  193. ret = tda18218_wr_reg(priv, R17_PD1, priv->regs[R17_PD1] | (1 << 0));
  194. if (fe->ops.i2c_gate_ctrl)
  195. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  196. if (ret)
  197. dbg("%s: failed ret:%d", __func__, ret);
  198. return ret;
  199. }
  200. static int tda18218_init(struct dvb_frontend *fe)
  201. {
  202. struct tda18218_priv *priv = fe->tuner_priv;
  203. int ret;
  204. /* TODO: calibrations */
  205. if (fe->ops.i2c_gate_ctrl)
  206. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  207. ret = tda18218_wr_regs(priv, R00_ID, priv->regs, TDA18218_NUM_REGS);
  208. if (fe->ops.i2c_gate_ctrl)
  209. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  210. if (ret)
  211. dbg("%s: failed ret:%d", __func__, ret);
  212. return ret;
  213. }
  214. static int tda18218_release(struct dvb_frontend *fe)
  215. {
  216. kfree(fe->tuner_priv);
  217. fe->tuner_priv = NULL;
  218. return 0;
  219. }
  220. static const struct dvb_tuner_ops tda18218_tuner_ops = {
  221. .info = {
  222. .name = "NXP TDA18218",
  223. .frequency_min = 174000000,
  224. .frequency_max = 864000000,
  225. .frequency_step = 1000,
  226. },
  227. .release = tda18218_release,
  228. .init = tda18218_init,
  229. .sleep = tda18218_sleep,
  230. .set_params = tda18218_set_params,
  231. .get_if_frequency = tda18218_get_if_frequency,
  232. };
  233. struct dvb_frontend *tda18218_attach(struct dvb_frontend *fe,
  234. struct i2c_adapter *i2c, struct tda18218_config *cfg)
  235. {
  236. struct tda18218_priv *priv = NULL;
  237. u8 val;
  238. int ret;
  239. /* chip default registers values */
  240. static u8 def_regs[] = {
  241. 0xc0, 0x88, 0x00, 0x8e, 0x03, 0x00, 0x00, 0xd0, 0x00, 0x40,
  242. 0x00, 0x00, 0x07, 0xff, 0x84, 0x09, 0x00, 0x13, 0x00, 0x00,
  243. 0x01, 0x84, 0x09, 0xf0, 0x19, 0x0a, 0x8e, 0x69, 0x98, 0x01,
  244. 0x00, 0x58, 0x10, 0x40, 0x8c, 0x00, 0x0c, 0x48, 0x85, 0xc9,
  245. 0xa7, 0x00, 0x00, 0x00, 0x30, 0x81, 0x80, 0x00, 0x39, 0x00,
  246. 0x8a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf6, 0xf6
  247. };
  248. priv = kzalloc(sizeof(struct tda18218_priv), GFP_KERNEL);
  249. if (priv == NULL)
  250. return NULL;
  251. priv->cfg = cfg;
  252. priv->i2c = i2c;
  253. fe->tuner_priv = priv;
  254. if (fe->ops.i2c_gate_ctrl)
  255. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  256. /* check if the tuner is there */
  257. ret = tda18218_rd_reg(priv, R00_ID, &val);
  258. dbg("%s: ret:%d chip ID:%02x", __func__, ret, val);
  259. if (ret || val != def_regs[R00_ID]) {
  260. kfree(priv);
  261. return NULL;
  262. }
  263. info("NXP TDA18218HN successfully identified.");
  264. memcpy(&fe->ops.tuner_ops, &tda18218_tuner_ops,
  265. sizeof(struct dvb_tuner_ops));
  266. memcpy(priv->regs, def_regs, sizeof(def_regs));
  267. /* loop-through enabled chip default register values */
  268. if (priv->cfg->loop_through) {
  269. priv->regs[R17_PD1] = 0xb0;
  270. priv->regs[R18_PD2] = 0x59;
  271. }
  272. /* standby */
  273. ret = tda18218_wr_reg(priv, R17_PD1, priv->regs[R17_PD1] | (1 << 0));
  274. if (ret)
  275. dbg("%s: failed ret:%d", __func__, ret);
  276. if (fe->ops.i2c_gate_ctrl)
  277. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  278. return fe;
  279. }
  280. EXPORT_SYMBOL(tda18218_attach);
  281. MODULE_DESCRIPTION("NXP TDA18218HN silicon tuner driver");
  282. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  283. MODULE_LICENSE("GPL");