PageRenderTime 171ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/media/common/tuners/tda18212.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 265 lines | 199 code | 41 blank | 25 comment | 22 complexity | 4b999a303052b9fd33a09dfebb33ed75 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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_priv.h"
  21. static int debug;
  22. module_param(debug, int, 0644);
  23. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  24. /* write multiple registers */
  25. static int tda18212_wr_regs(struct tda18212_priv *priv, u8 reg, u8 *val,
  26. int len)
  27. {
  28. int ret;
  29. u8 buf[len+1];
  30. struct i2c_msg msg[1] = {
  31. {
  32. .addr = priv->cfg->i2c_address,
  33. .flags = 0,
  34. .len = sizeof(buf),
  35. .buf = buf,
  36. }
  37. };
  38. buf[0] = reg;
  39. memcpy(&buf[1], val, len);
  40. ret = i2c_transfer(priv->i2c, msg, 1);
  41. if (ret == 1) {
  42. ret = 0;
  43. } else {
  44. warn("i2c wr failed ret:%d reg:%02x len:%d", ret, reg, len);
  45. ret = -EREMOTEIO;
  46. }
  47. return ret;
  48. }
  49. /* read multiple registers */
  50. static int tda18212_rd_regs(struct tda18212_priv *priv, u8 reg, u8 *val,
  51. int len)
  52. {
  53. int ret;
  54. u8 buf[len];
  55. struct i2c_msg msg[2] = {
  56. {
  57. .addr = priv->cfg->i2c_address,
  58. .flags = 0,
  59. .len = 1,
  60. .buf = &reg,
  61. }, {
  62. .addr = priv->cfg->i2c_address,
  63. .flags = I2C_M_RD,
  64. .len = sizeof(buf),
  65. .buf = buf,
  66. }
  67. };
  68. ret = i2c_transfer(priv->i2c, msg, 2);
  69. if (ret == 2) {
  70. memcpy(val, buf, len);
  71. ret = 0;
  72. } else {
  73. warn("i2c rd failed ret:%d reg:%02x len:%d", ret, reg, len);
  74. ret = -EREMOTEIO;
  75. }
  76. return ret;
  77. }
  78. /* write single register */
  79. static int tda18212_wr_reg(struct tda18212_priv *priv, u8 reg, u8 val)
  80. {
  81. return tda18212_wr_regs(priv, reg, &val, 1);
  82. }
  83. /* read single register */
  84. static int tda18212_rd_reg(struct tda18212_priv *priv, u8 reg, u8 *val)
  85. {
  86. return tda18212_rd_regs(priv, reg, val, 1);
  87. }
  88. #if 0 /* keep, useful when developing driver */
  89. static void tda18212_dump_regs(struct tda18212_priv *priv)
  90. {
  91. int i;
  92. u8 buf[256];
  93. #define TDA18212_RD_LEN 32
  94. for (i = 0; i < sizeof(buf); i += TDA18212_RD_LEN)
  95. tda18212_rd_regs(priv, i, &buf[i], TDA18212_RD_LEN);
  96. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 32, 1, buf,
  97. sizeof(buf), true);
  98. return;
  99. }
  100. #endif
  101. static int tda18212_set_params(struct dvb_frontend *fe,
  102. struct dvb_frontend_parameters *p)
  103. {
  104. struct tda18212_priv *priv = fe->tuner_priv;
  105. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  106. int ret, i;
  107. u32 if_khz;
  108. u8 buf[9];
  109. static const u8 bw_params[][3] = {
  110. /* 0f 13 23 */
  111. { 0xb3, 0x20, 0x03 }, /* DVB-T 6 MHz */
  112. { 0xb3, 0x31, 0x01 }, /* DVB-T 7 MHz */
  113. { 0xb3, 0x22, 0x01 }, /* DVB-T 8 MHz */
  114. { 0x92, 0x53, 0x03 }, /* DVB-C */
  115. };
  116. dbg("%s: delsys=%d RF=%d BW=%d", __func__,
  117. c->delivery_system, c->frequency, c->bandwidth_hz);
  118. if (fe->ops.i2c_gate_ctrl)
  119. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  120. switch (c->delivery_system) {
  121. case SYS_DVBT:
  122. switch (c->bandwidth_hz) {
  123. case 6000000:
  124. if_khz = priv->cfg->if_dvbt_6;
  125. i = 0;
  126. break;
  127. case 7000000:
  128. if_khz = priv->cfg->if_dvbt_7;
  129. i = 1;
  130. break;
  131. case 8000000:
  132. if_khz = priv->cfg->if_dvbt_8;
  133. i = 2;
  134. break;
  135. default:
  136. ret = -EINVAL;
  137. goto error;
  138. }
  139. break;
  140. case SYS_DVBC_ANNEX_AC:
  141. if_khz = priv->cfg->if_dvbc;
  142. i = 3;
  143. break;
  144. default:
  145. ret = -EINVAL;
  146. goto error;
  147. }
  148. ret = tda18212_wr_reg(priv, 0x23, bw_params[i][2]);
  149. if (ret)
  150. goto error;
  151. ret = tda18212_wr_reg(priv, 0x06, 0x00);
  152. if (ret)
  153. goto error;
  154. ret = tda18212_wr_reg(priv, 0x0f, bw_params[i][0]);
  155. if (ret)
  156. goto error;
  157. buf[0] = 0x02;
  158. buf[1] = bw_params[i][1];
  159. buf[2] = 0x03; /* default value */
  160. buf[3] = if_khz / 50;
  161. buf[4] = ((c->frequency / 1000) >> 16) & 0xff;
  162. buf[5] = ((c->frequency / 1000) >> 8) & 0xff;
  163. buf[6] = ((c->frequency / 1000) >> 0) & 0xff;
  164. buf[7] = 0xc1;
  165. buf[8] = 0x01;
  166. ret = tda18212_wr_regs(priv, 0x12, buf, sizeof(buf));
  167. if (ret)
  168. goto error;
  169. exit:
  170. if (fe->ops.i2c_gate_ctrl)
  171. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  172. return ret;
  173. error:
  174. dbg("%s: failed:%d", __func__, ret);
  175. goto exit;
  176. }
  177. static int tda18212_release(struct dvb_frontend *fe)
  178. {
  179. kfree(fe->tuner_priv);
  180. fe->tuner_priv = NULL;
  181. return 0;
  182. }
  183. static const struct dvb_tuner_ops tda18212_tuner_ops = {
  184. .info = {
  185. .name = "NXP TDA18212",
  186. .frequency_min = 48000000,
  187. .frequency_max = 864000000,
  188. .frequency_step = 1000,
  189. },
  190. .release = tda18212_release,
  191. .set_params = tda18212_set_params,
  192. };
  193. struct dvb_frontend *tda18212_attach(struct dvb_frontend *fe,
  194. struct i2c_adapter *i2c, struct tda18212_config *cfg)
  195. {
  196. struct tda18212_priv *priv = NULL;
  197. int ret;
  198. u8 val;
  199. priv = kzalloc(sizeof(struct tda18212_priv), GFP_KERNEL);
  200. if (priv == NULL)
  201. return NULL;
  202. priv->cfg = cfg;
  203. priv->i2c = i2c;
  204. fe->tuner_priv = priv;
  205. if (fe->ops.i2c_gate_ctrl)
  206. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  207. /* check if the tuner is there */
  208. ret = tda18212_rd_reg(priv, 0x00, &val);
  209. if (fe->ops.i2c_gate_ctrl)
  210. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  211. dbg("%s: ret:%d chip ID:%02x", __func__, ret, val);
  212. if (ret || val != 0xc7) {
  213. kfree(priv);
  214. return NULL;
  215. }
  216. info("NXP TDA18212HN successfully identified.");
  217. memcpy(&fe->ops.tuner_ops, &tda18212_tuner_ops,
  218. sizeof(struct dvb_tuner_ops));
  219. return fe;
  220. }
  221. EXPORT_SYMBOL(tda18212_attach);
  222. MODULE_DESCRIPTION("NXP TDA18212HN silicon tuner driver");
  223. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  224. MODULE_LICENSE("GPL");