tua9001.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. kfree(fe->tuner_priv);
  48. fe->tuner_priv = NULL;
  49. return 0;
  50. }
  51. static int tua9001_init(struct dvb_frontend *fe)
  52. {
  53. struct tua9001_priv *priv = fe->tuner_priv;
  54. int ret = 0;
  55. u8 i;
  56. struct reg_val data[] = {
  57. { 0x1e, 0x6512 },
  58. { 0x25, 0xb888 },
  59. { 0x39, 0x5460 },
  60. { 0x3b, 0x00c0 },
  61. { 0x3a, 0xf000 },
  62. { 0x08, 0x0000 },
  63. { 0x32, 0x0030 },
  64. { 0x41, 0x703a },
  65. { 0x40, 0x1c78 },
  66. { 0x2c, 0x1c00 },
  67. { 0x36, 0xc013 },
  68. { 0x37, 0x6f18 },
  69. { 0x27, 0x0008 },
  70. { 0x2a, 0x0001 },
  71. { 0x34, 0x0a40 },
  72. };
  73. if (fe->ops.i2c_gate_ctrl)
  74. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c-gate */
  75. for (i = 0; i < ARRAY_SIZE(data); i++) {
  76. ret = tua9001_wr_reg(priv, data[i].reg, data[i].val);
  77. if (ret)
  78. break;
  79. }
  80. if (fe->ops.i2c_gate_ctrl)
  81. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c-gate */
  82. if (ret < 0)
  83. pr_debug("%s: failed=%d\n", __func__, ret);
  84. return ret;
  85. }
  86. static int tua9001_set_params(struct dvb_frontend *fe)
  87. {
  88. struct tua9001_priv *priv = fe->tuner_priv;
  89. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  90. int ret, i;
  91. u16 val;
  92. u32 frequency;
  93. struct reg_val data[2];
  94. pr_debug("%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
  95. __func__, c->delivery_system, c->frequency,
  96. c->bandwidth_hz);
  97. switch (c->delivery_system) {
  98. case SYS_DVBT:
  99. switch (c->bandwidth_hz) {
  100. case 8000000:
  101. val = 0x0000;
  102. break;
  103. case 7000000:
  104. val = 0x1000;
  105. break;
  106. case 6000000:
  107. val = 0x2000;
  108. break;
  109. case 5000000:
  110. val = 0x3000;
  111. break;
  112. default:
  113. ret = -EINVAL;
  114. goto err;
  115. }
  116. break;
  117. default:
  118. ret = -EINVAL;
  119. goto err;
  120. }
  121. data[0].reg = 0x04;
  122. data[0].val = val;
  123. frequency = (c->frequency - 150000000);
  124. frequency /= 100;
  125. frequency *= 48;
  126. frequency /= 10000;
  127. data[1].reg = 0x1f;
  128. data[1].val = frequency;
  129. if (fe->ops.i2c_gate_ctrl)
  130. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c-gate */
  131. for (i = 0; i < ARRAY_SIZE(data); i++) {
  132. ret = tua9001_wr_reg(priv, data[i].reg, data[i].val);
  133. if (ret < 0)
  134. break;
  135. }
  136. if (fe->ops.i2c_gate_ctrl)
  137. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c-gate */
  138. err:
  139. if (ret < 0)
  140. pr_debug("%s: failed=%d\n", __func__, ret);
  141. return ret;
  142. }
  143. static int tua9001_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  144. {
  145. *frequency = 0; /* Zero-IF */
  146. return 0;
  147. }
  148. static const struct dvb_tuner_ops tua9001_tuner_ops = {
  149. .info = {
  150. .name = "Infineon TUA 9001",
  151. .frequency_min = 170000000,
  152. .frequency_max = 862000000,
  153. .frequency_step = 0,
  154. },
  155. .release = tua9001_release,
  156. .init = tua9001_init,
  157. .set_params = tua9001_set_params,
  158. .get_if_frequency = tua9001_get_if_frequency,
  159. };
  160. struct dvb_frontend *tua9001_attach(struct dvb_frontend *fe,
  161. struct i2c_adapter *i2c, struct tua9001_config *cfg)
  162. {
  163. struct tua9001_priv *priv = NULL;
  164. priv = kzalloc(sizeof(struct tua9001_priv), GFP_KERNEL);
  165. if (priv == NULL)
  166. return NULL;
  167. priv->cfg = cfg;
  168. priv->i2c = i2c;
  169. printk(KERN_INFO "Infineon TUA 9001 successfully attached.");
  170. memcpy(&fe->ops.tuner_ops, &tua9001_tuner_ops,
  171. sizeof(struct dvb_tuner_ops));
  172. fe->tuner_priv = priv;
  173. return fe;
  174. }
  175. EXPORT_SYMBOL(tua9001_attach);
  176. MODULE_DESCRIPTION("Infineon TUA 9001 silicon tuner driver");
  177. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  178. MODULE_LICENSE("GPL");