tda826x.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Driver for Philips tda8262/tda8263 DVBS Silicon tuners
  3. (c) 2006 Andrew de Quincey
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/dvb/frontend.h>
  18. #include <asm/types.h>
  19. #include "tda826x.h"
  20. static int debug = 0;
  21. #define dprintk(args...) \
  22. do { \
  23. if (debug) printk(KERN_DEBUG "tda826x: " args); \
  24. } while (0)
  25. struct tda826x_priv {
  26. /* i2c details */
  27. int i2c_address;
  28. struct i2c_adapter *i2c;
  29. u8 has_loopthrough:1;
  30. u32 frequency;
  31. };
  32. static int tda826x_release(struct dvb_frontend *fe)
  33. {
  34. kfree(fe->tuner_priv);
  35. fe->tuner_priv = NULL;
  36. return 0;
  37. }
  38. static int tda826x_sleep(struct dvb_frontend *fe)
  39. {
  40. struct tda826x_priv *priv = fe->tuner_priv;
  41. int ret;
  42. u8 buf [] = { 0x00, 0x8d };
  43. struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 2 };
  44. dprintk("%s:\n", __FUNCTION__);
  45. if (!priv->has_loopthrough)
  46. buf[1] = 0xad;
  47. if (fe->ops.i2c_gate_ctrl)
  48. fe->ops.i2c_gate_ctrl(fe, 1);
  49. if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
  50. dprintk("%s: i2c error\n", __FUNCTION__);
  51. }
  52. if (fe->ops.i2c_gate_ctrl)
  53. fe->ops.i2c_gate_ctrl(fe, 0);
  54. return (ret == 1) ? 0 : ret;
  55. }
  56. static int tda826x_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
  57. {
  58. struct tda826x_priv *priv = fe->tuner_priv;
  59. int ret;
  60. u32 div;
  61. u8 buf [11];
  62. struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 11 };
  63. dprintk("%s:\n", __FUNCTION__);
  64. div = (params->frequency + (1000-1)) / 1000;
  65. buf[0] = 0x00; // subaddress
  66. buf[1] = 0x09; // powerdown RSSI + the magic value 1
  67. if (!priv->has_loopthrough)
  68. buf[1] |= 0x20; // power down loopthrough if not needed
  69. buf[2] = (1<<5) | 0x0b; // 1Mhz + 0.45 VCO
  70. buf[3] = div >> 7;
  71. buf[4] = div << 1;
  72. buf[5] = 0xff; // basedband filter to max
  73. buf[6] = 0xfe; // gains at max + no RF attenuation
  74. buf[7] = 0x83; // charge pumps at high, tests off
  75. buf[8] = 0x80; // recommended value 4 for AMPVCO + disable ports.
  76. buf[9] = 0x1a; // normal caltime + recommended values for SELTH + SELVTL
  77. buf[10] = 0xd4; // recommended value 13 for BBIAS + unknown bit set on
  78. if (fe->ops.i2c_gate_ctrl)
  79. fe->ops.i2c_gate_ctrl(fe, 1);
  80. if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
  81. dprintk("%s: i2c error\n", __FUNCTION__);
  82. }
  83. if (fe->ops.i2c_gate_ctrl)
  84. fe->ops.i2c_gate_ctrl(fe, 0);
  85. priv->frequency = div * 1000;
  86. return (ret == 1) ? 0 : ret;
  87. }
  88. static int tda826x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  89. {
  90. struct tda826x_priv *priv = fe->tuner_priv;
  91. *frequency = priv->frequency;
  92. return 0;
  93. }
  94. static struct dvb_tuner_ops tda826x_tuner_ops = {
  95. .info = {
  96. .name = "Philips TDA826X",
  97. .frequency_min = 950000,
  98. .frequency_max = 2175000
  99. },
  100. .release = tda826x_release,
  101. .sleep = tda826x_sleep,
  102. .set_params = tda826x_set_params,
  103. .get_frequency = tda826x_get_frequency,
  104. };
  105. struct dvb_frontend *tda826x_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c, int has_loopthrough)
  106. {
  107. struct tda826x_priv *priv = NULL;
  108. u8 b1 [] = { 0, 0 };
  109. struct i2c_msg msg[2] = {
  110. { .addr = addr, .flags = 0, .buf = NULL, .len = 0 },
  111. { .addr = addr, .flags = I2C_M_RD, .buf = b1, .len = 2 }
  112. };
  113. int ret;
  114. dprintk("%s:\n", __FUNCTION__);
  115. if (fe->ops.i2c_gate_ctrl)
  116. fe->ops.i2c_gate_ctrl(fe, 1);
  117. ret = i2c_transfer (i2c, msg, 2);
  118. if (fe->ops.i2c_gate_ctrl)
  119. fe->ops.i2c_gate_ctrl(fe, 0);
  120. if (ret != 2)
  121. return NULL;
  122. if (!(b1[1] & 0x80))
  123. return NULL;
  124. priv = kzalloc(sizeof(struct tda826x_priv), GFP_KERNEL);
  125. if (priv == NULL)
  126. return NULL;
  127. priv->i2c_address = addr;
  128. priv->i2c = i2c;
  129. priv->has_loopthrough = has_loopthrough;
  130. memcpy(&fe->ops.tuner_ops, &tda826x_tuner_ops, sizeof(struct dvb_tuner_ops));
  131. fe->tuner_priv = priv;
  132. return fe;
  133. }
  134. EXPORT_SYMBOL(tda826x_attach);
  135. module_param(debug, int, 0644);
  136. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  137. MODULE_DESCRIPTION("DVB TDA826x driver");
  138. MODULE_AUTHOR("Andrew de Quincey");
  139. MODULE_LICENSE("GPL");