tua9001.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Infineon TUA 9001 silicon tuner driver
  3. *
  4. * Copyright (C) 2009 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 "tua9001.h"
  21. #include "tua9001_priv.h"
  22. /* write register */
  23. static int tua9001_wr_reg(struct tua9001_priv *priv, u8 reg, u16 val)
  24. {
  25. int ret;
  26. u8 buf[3] = { reg, (val >> 8) & 0xff, (val >> 0) & 0xff };
  27. struct i2c_msg msg[1] = {
  28. {
  29. .addr = priv->cfg->i2c_addr,
  30. .flags = 0,
  31. .len = sizeof(buf),
  32. .buf = buf,
  33. }
  34. };
  35. ret = i2c_transfer(priv->i2c, msg, 1);
  36. if (ret == 1) {
  37. ret = 0;
  38. } else {
  39. printk(KERN_WARNING "%s: I2C wr failed=%d reg=%02x\n",
  40. __func__, ret, reg);
  41. ret = -EREMOTEIO;
  42. }
  43. return ret;
  44. }
  45. static int tua9001_release(struct dvb_frontend *fe)
  46. {
  47. struct tua9001_priv *priv = fe->tuner_priv;
  48. int ret = 0;
  49. if (fe->callback)
  50. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  51. TUA9001_CMD_CEN, 0);
  52. kfree(fe->tuner_priv);
  53. fe->tuner_priv = NULL;
  54. return ret;
  55. }
  56. static int tua9001_init(struct dvb_frontend *fe)
  57. {
  58. struct tua9001_priv *priv = fe->tuner_priv;
  59. int ret = 0;
  60. u8 i;
  61. struct reg_val data[] = {
  62. { 0x1e, 0x6512 },
  63. { 0x25, 0xb888 },
  64. { 0x39, 0x5460 },
  65. { 0x3b, 0x00c0 },
  66. { 0x3a, 0xf000 },
  67. { 0x08, 0x0000 },
  68. { 0x32, 0x0030 },
  69. { 0x41, 0x703a },
  70. { 0x40, 0x1c78 },
  71. { 0x2c, 0x1c00 },
  72. { 0x36, 0xc013 },
  73. { 0x37, 0x6f18 },
  74. { 0x27, 0x0008 },
  75. { 0x2a, 0x0001 },
  76. { 0x34, 0x0a40 },
  77. };
  78. if (fe->callback) {
  79. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  80. TUA9001_CMD_RESETN, 0);
  81. if (ret < 0)
  82. goto err;
  83. }
  84. if (fe->ops.i2c_gate_ctrl)
  85. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c-gate */
  86. for (i = 0; i < ARRAY_SIZE(data); i++) {
  87. ret = tua9001_wr_reg(priv, data[i].reg, data[i].val);
  88. if (ret < 0)
  89. goto err_i2c_gate_ctrl;
  90. }
  91. err_i2c_gate_ctrl:
  92. if (fe->ops.i2c_gate_ctrl)
  93. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c-gate */
  94. err:
  95. if (ret < 0)
  96. pr_debug("%s: failed=%d\n", __func__, ret);
  97. return ret;
  98. }
  99. static int tua9001_sleep(struct dvb_frontend *fe)
  100. {
  101. struct tua9001_priv *priv = fe->tuner_priv;
  102. int ret = 0;
  103. if (fe->callback)
  104. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  105. TUA9001_CMD_RESETN, 1);
  106. if (ret < 0)
  107. pr_debug("%s: failed=%d\n", __func__, ret);
  108. return ret;
  109. }
  110. static int tua9001_set_params(struct dvb_frontend *fe)
  111. {
  112. struct tua9001_priv *priv = fe->tuner_priv;
  113. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  114. int ret, i;
  115. u16 val;
  116. u32 frequency;
  117. struct reg_val data[2];
  118. pr_debug("%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
  119. __func__, c->delivery_system, c->frequency,
  120. c->bandwidth_hz);
  121. switch (c->delivery_system) {
  122. case SYS_DVBT:
  123. switch (c->bandwidth_hz) {
  124. case 8000000:
  125. val = 0x0000;
  126. break;
  127. case 7000000:
  128. val = 0x1000;
  129. break;
  130. case 6000000:
  131. val = 0x2000;
  132. break;
  133. case 5000000:
  134. val = 0x3000;
  135. break;
  136. default:
  137. ret = -EINVAL;
  138. goto err;
  139. }
  140. break;
  141. default:
  142. ret = -EINVAL;
  143. goto err;
  144. }
  145. data[0].reg = 0x04;
  146. data[0].val = val;
  147. frequency = (c->frequency - 150000000);
  148. frequency /= 100;
  149. frequency *= 48;
  150. frequency /= 10000;
  151. data[1].reg = 0x1f;
  152. data[1].val = frequency;
  153. if (fe->ops.i2c_gate_ctrl)
  154. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c-gate */
  155. if (fe->callback) {
  156. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  157. TUA9001_CMD_RXEN, 0);
  158. if (ret < 0)
  159. goto err_i2c_gate_ctrl;
  160. }
  161. for (i = 0; i < ARRAY_SIZE(data); i++) {
  162. ret = tua9001_wr_reg(priv, data[i].reg, data[i].val);
  163. if (ret < 0)
  164. goto err_i2c_gate_ctrl;
  165. }
  166. if (fe->callback) {
  167. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  168. TUA9001_CMD_RXEN, 1);
  169. if (ret < 0)
  170. goto err_i2c_gate_ctrl;
  171. }
  172. err_i2c_gate_ctrl:
  173. if (fe->ops.i2c_gate_ctrl)
  174. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c-gate */
  175. err:
  176. if (ret < 0)
  177. pr_debug("%s: failed=%d\n", __func__, ret);
  178. return ret;
  179. }
  180. static int tua9001_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  181. {
  182. *frequency = 0; /* Zero-IF */
  183. return 0;
  184. }
  185. static const struct dvb_tuner_ops tua9001_tuner_ops = {
  186. .info = {
  187. .name = "Infineon TUA 9001",
  188. .frequency_min = 170000000,
  189. .frequency_max = 862000000,
  190. .frequency_step = 0,
  191. },
  192. .release = tua9001_release,
  193. .init = tua9001_init,
  194. .sleep = tua9001_sleep,
  195. .set_params = tua9001_set_params,
  196. .get_if_frequency = tua9001_get_if_frequency,
  197. };
  198. struct dvb_frontend *tua9001_attach(struct dvb_frontend *fe,
  199. struct i2c_adapter *i2c, struct tua9001_config *cfg)
  200. {
  201. struct tua9001_priv *priv = NULL;
  202. int ret;
  203. priv = kzalloc(sizeof(struct tua9001_priv), GFP_KERNEL);
  204. if (priv == NULL)
  205. return NULL;
  206. priv->cfg = cfg;
  207. priv->i2c = i2c;
  208. if (fe->callback) {
  209. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  210. TUA9001_CMD_CEN, 1);
  211. if (ret < 0)
  212. goto err;
  213. }
  214. printk(KERN_INFO "Infineon TUA 9001 successfully attached.");
  215. memcpy(&fe->ops.tuner_ops, &tua9001_tuner_ops,
  216. sizeof(struct dvb_tuner_ops));
  217. fe->tuner_priv = priv;
  218. return fe;
  219. err:
  220. kfree(priv);
  221. return NULL;
  222. }
  223. EXPORT_SYMBOL(tua9001_attach);
  224. MODULE_DESCRIPTION("Infineon TUA 9001 silicon tuner driver");
  225. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  226. MODULE_LICENSE("GPL");