tda18212.c 7.0 KB

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