tda18212.c 7.4 KB

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