tda18212.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * NXP TDA18212HN silicon tuner driver
  3. *
  4. * Copyright (C) 2011 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 along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "tda18212.h"
  21. struct tda18212_priv {
  22. struct tda18212_config *cfg;
  23. struct i2c_adapter *i2c;
  24. u32 if_frequency;
  25. };
  26. /* write multiple registers */
  27. static int tda18212_wr_regs(struct tda18212_priv *priv, u8 reg, u8 *val,
  28. int len)
  29. {
  30. int ret;
  31. u8 buf[len+1];
  32. struct i2c_msg msg[1] = {
  33. {
  34. .addr = priv->cfg->i2c_address,
  35. .flags = 0,
  36. .len = sizeof(buf),
  37. .buf = buf,
  38. }
  39. };
  40. buf[0] = reg;
  41. memcpy(&buf[1], val, len);
  42. ret = i2c_transfer(priv->i2c, msg, 1);
  43. if (ret == 1) {
  44. ret = 0;
  45. } else {
  46. dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%02x " \
  47. "len=%d\n", KBUILD_MODNAME, ret, reg, len);
  48. ret = -EREMOTEIO;
  49. }
  50. return ret;
  51. }
  52. /* read multiple registers */
  53. static int tda18212_rd_regs(struct tda18212_priv *priv, u8 reg, u8 *val,
  54. int len)
  55. {
  56. int ret;
  57. u8 buf[len];
  58. struct i2c_msg msg[2] = {
  59. {
  60. .addr = priv->cfg->i2c_address,
  61. .flags = 0,
  62. .len = 1,
  63. .buf = &reg,
  64. }, {
  65. .addr = priv->cfg->i2c_address,
  66. .flags = I2C_M_RD,
  67. .len = sizeof(buf),
  68. .buf = buf,
  69. }
  70. };
  71. ret = i2c_transfer(priv->i2c, msg, 2);
  72. if (ret == 2) {
  73. memcpy(val, buf, len);
  74. ret = 0;
  75. } else {
  76. dev_warn(&priv->i2c->dev, "%s: i2c rd failed=%d reg=%02x " \
  77. "len=%d\n", KBUILD_MODNAME, ret, reg, len);
  78. ret = -EREMOTEIO;
  79. }
  80. return ret;
  81. }
  82. /* write single register */
  83. static int tda18212_wr_reg(struct tda18212_priv *priv, u8 reg, u8 val)
  84. {
  85. return tda18212_wr_regs(priv, reg, &val, 1);
  86. }
  87. /* read single register */
  88. static int tda18212_rd_reg(struct tda18212_priv *priv, u8 reg, u8 *val)
  89. {
  90. return tda18212_rd_regs(priv, reg, val, 1);
  91. }
  92. #if 0 /* keep, useful when developing driver */
  93. static void tda18212_dump_regs(struct tda18212_priv *priv)
  94. {
  95. int i;
  96. u8 buf[256];
  97. #define TDA18212_RD_LEN 32
  98. for (i = 0; i < sizeof(buf); i += TDA18212_RD_LEN)
  99. tda18212_rd_regs(priv, i, &buf[i], TDA18212_RD_LEN);
  100. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 32, 1, buf,
  101. sizeof(buf), true);
  102. return;
  103. }
  104. #endif
  105. static int tda18212_set_params(struct dvb_frontend *fe)
  106. {
  107. struct tda18212_priv *priv = fe->tuner_priv;
  108. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  109. int ret, i;
  110. u32 if_khz;
  111. u8 buf[9];
  112. #define DVBT_6 0
  113. #define DVBT_7 1
  114. #define DVBT_8 2
  115. #define DVBT2_6 3
  116. #define DVBT2_7 4
  117. #define DVBT2_8 5
  118. #define DVBC_6 6
  119. #define DVBC_8 7
  120. static const u8 bw_params[][3] = {
  121. /* reg: 0f 13 23 */
  122. [DVBT_6] = { 0xb3, 0x20, 0x03 },
  123. [DVBT_7] = { 0xb3, 0x31, 0x01 },
  124. [DVBT_8] = { 0xb3, 0x22, 0x01 },
  125. [DVBT2_6] = { 0xbc, 0x20, 0x03 },
  126. [DVBT2_7] = { 0xbc, 0x72, 0x03 },
  127. [DVBT2_8] = { 0xbc, 0x22, 0x01 },
  128. [DVBC_6] = { 0x92, 0x50, 0x03 },
  129. [DVBC_8] = { 0x92, 0x53, 0x03 },
  130. };
  131. dev_dbg(&priv->i2c->dev,
  132. "%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
  133. __func__, c->delivery_system, c->frequency,
  134. c->bandwidth_hz);
  135. if (fe->ops.i2c_gate_ctrl)
  136. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  137. switch (c->delivery_system) {
  138. case SYS_DVBT:
  139. switch (c->bandwidth_hz) {
  140. case 6000000:
  141. if_khz = priv->cfg->if_dvbt_6;
  142. i = DVBT_6;
  143. break;
  144. case 7000000:
  145. if_khz = priv->cfg->if_dvbt_7;
  146. i = DVBT_7;
  147. break;
  148. case 8000000:
  149. if_khz = priv->cfg->if_dvbt_8;
  150. i = DVBT_8;
  151. break;
  152. default:
  153. ret = -EINVAL;
  154. goto error;
  155. }
  156. break;
  157. case SYS_DVBT2:
  158. switch (c->bandwidth_hz) {
  159. case 6000000:
  160. if_khz = priv->cfg->if_dvbt2_6;
  161. i = DVBT2_6;
  162. break;
  163. case 7000000:
  164. if_khz = priv->cfg->if_dvbt2_7;
  165. i = DVBT2_7;
  166. break;
  167. case 8000000:
  168. if_khz = priv->cfg->if_dvbt2_8;
  169. i = DVBT2_8;
  170. break;
  171. default:
  172. ret = -EINVAL;
  173. goto error;
  174. }
  175. break;
  176. case SYS_DVBC_ANNEX_A:
  177. case SYS_DVBC_ANNEX_C:
  178. if_khz = priv->cfg->if_dvbc;
  179. i = DVBC_8;
  180. break;
  181. default:
  182. ret = -EINVAL;
  183. goto error;
  184. }
  185. ret = tda18212_wr_reg(priv, 0x23, bw_params[i][2]);
  186. if (ret)
  187. goto error;
  188. ret = tda18212_wr_reg(priv, 0x06, 0x00);
  189. if (ret)
  190. goto error;
  191. ret = tda18212_wr_reg(priv, 0x0f, bw_params[i][0]);
  192. if (ret)
  193. goto error;
  194. buf[0] = 0x02;
  195. buf[1] = bw_params[i][1];
  196. buf[2] = 0x03; /* default value */
  197. buf[3] = DIV_ROUND_CLOSEST(if_khz, 50);
  198. buf[4] = ((c->frequency / 1000) >> 16) & 0xff;
  199. buf[5] = ((c->frequency / 1000) >> 8) & 0xff;
  200. buf[6] = ((c->frequency / 1000) >> 0) & 0xff;
  201. buf[7] = 0xc1;
  202. buf[8] = 0x01;
  203. ret = tda18212_wr_regs(priv, 0x12, buf, sizeof(buf));
  204. if (ret)
  205. goto error;
  206. /* actual IF rounded as it is on register */
  207. priv->if_frequency = buf[3] * 50 * 1000;
  208. exit:
  209. if (fe->ops.i2c_gate_ctrl)
  210. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  211. return ret;
  212. error:
  213. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  214. goto exit;
  215. }
  216. static int tda18212_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  217. {
  218. struct tda18212_priv *priv = fe->tuner_priv;
  219. *frequency = priv->if_frequency;
  220. return 0;
  221. }
  222. static int tda18212_release(struct dvb_frontend *fe)
  223. {
  224. kfree(fe->tuner_priv);
  225. fe->tuner_priv = NULL;
  226. return 0;
  227. }
  228. static const struct dvb_tuner_ops tda18212_tuner_ops = {
  229. .info = {
  230. .name = "NXP TDA18212",
  231. .frequency_min = 48000000,
  232. .frequency_max = 864000000,
  233. .frequency_step = 1000,
  234. },
  235. .release = tda18212_release,
  236. .set_params = tda18212_set_params,
  237. .get_if_frequency = tda18212_get_if_frequency,
  238. };
  239. struct dvb_frontend *tda18212_attach(struct dvb_frontend *fe,
  240. struct i2c_adapter *i2c, struct tda18212_config *cfg)
  241. {
  242. struct tda18212_priv *priv = NULL;
  243. int ret;
  244. u8 val;
  245. priv = kzalloc(sizeof(struct tda18212_priv), GFP_KERNEL);
  246. if (priv == NULL)
  247. return NULL;
  248. priv->cfg = cfg;
  249. priv->i2c = i2c;
  250. fe->tuner_priv = priv;
  251. if (fe->ops.i2c_gate_ctrl)
  252. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  253. /* check if the tuner is there */
  254. ret = tda18212_rd_reg(priv, 0x00, &val);
  255. if (fe->ops.i2c_gate_ctrl)
  256. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  257. if (!ret)
  258. dev_dbg(&priv->i2c->dev, "%s: chip id=%02x\n", __func__, val);
  259. if (ret || val != 0xc7) {
  260. kfree(priv);
  261. return NULL;
  262. }
  263. dev_info(&priv->i2c->dev,
  264. "%s: NXP TDA18212HN successfully identified\n",
  265. KBUILD_MODNAME);
  266. memcpy(&fe->ops.tuner_ops, &tda18212_tuner_ops,
  267. sizeof(struct dvb_tuner_ops));
  268. return fe;
  269. }
  270. EXPORT_SYMBOL(tda18212_attach);
  271. MODULE_DESCRIPTION("NXP TDA18212HN silicon tuner driver");
  272. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  273. MODULE_LICENSE("GPL");