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