tda826x.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/dvb/frontend.h>
  19. #include <asm/types.h>
  20. #include "tda826x.h"
  21. static int debug;
  22. #define dprintk(args...) \
  23. do { \
  24. if (debug) printk(KERN_DEBUG "tda826x: " args); \
  25. } while (0)
  26. struct tda826x_priv {
  27. /* i2c details */
  28. int i2c_address;
  29. struct i2c_adapter *i2c;
  30. u8 has_loopthrough:1;
  31. u32 frequency;
  32. };
  33. static int tda826x_release(struct dvb_frontend *fe)
  34. {
  35. kfree(fe->tuner_priv);
  36. fe->tuner_priv = NULL;
  37. return 0;
  38. }
  39. static int tda826x_sleep(struct dvb_frontend *fe)
  40. {
  41. struct tda826x_priv *priv = fe->tuner_priv;
  42. int ret;
  43. u8 buf [] = { 0x00, 0x8d };
  44. struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 2 };
  45. dprintk("%s:\n", __func__);
  46. if (!priv->has_loopthrough)
  47. buf[1] = 0xad;
  48. if (fe->ops.i2c_gate_ctrl)
  49. fe->ops.i2c_gate_ctrl(fe, 1);
  50. if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
  51. dprintk("%s: i2c error\n", __func__);
  52. }
  53. if (fe->ops.i2c_gate_ctrl)
  54. fe->ops.i2c_gate_ctrl(fe, 0);
  55. return (ret == 1) ? 0 : ret;
  56. }
  57. static int tda826x_set_params(struct dvb_frontend *fe)
  58. {
  59. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  60. struct tda826x_priv *priv = fe->tuner_priv;
  61. int ret;
  62. u32 div;
  63. u32 ksyms;
  64. u32 bandwidth;
  65. u8 buf [11];
  66. struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 11 };
  67. dprintk("%s:\n", __func__);
  68. div = (p->frequency + (1000-1)) / 1000;
  69. /* BW = ((1 + RO) * SR/2 + 5) * 1.3 [SR in MSPS, BW in MHz] */
  70. /* with R0 = 0.35 and some transformations: */
  71. ksyms = p->symbol_rate / 1000;
  72. bandwidth = (878 * ksyms + 6500000) / 1000000 + 1;
  73. if (bandwidth < 5)
  74. bandwidth = 5;
  75. else if (bandwidth > 36)
  76. bandwidth = 36;
  77. buf[0] = 0x00; // subaddress
  78. buf[1] = 0x09; // powerdown RSSI + the magic value 1
  79. if (!priv->has_loopthrough)
  80. buf[1] |= 0x20; // power down loopthrough if not needed
  81. buf[2] = (1<<5) | 0x0b; // 1Mhz + 0.45 VCO
  82. buf[3] = div >> 7;
  83. buf[4] = div << 1;
  84. buf[5] = ((bandwidth - 5) << 3) | 7; /* baseband cut-off */
  85. buf[6] = 0xfe; // baseband gain 9 db + no RF attenuation
  86. buf[7] = 0x83; // charge pumps at high, tests off
  87. buf[8] = 0x80; // recommended value 4 for AMPVCO + disable ports.
  88. buf[9] = 0x1a; // normal caltime + recommended values for SELTH + SELVTL
  89. buf[10] = 0xd4; // recommended value 13 for BBIAS + unknown bit set on
  90. if (fe->ops.i2c_gate_ctrl)
  91. fe->ops.i2c_gate_ctrl(fe, 1);
  92. if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
  93. dprintk("%s: i2c error\n", __func__);
  94. }
  95. if (fe->ops.i2c_gate_ctrl)
  96. fe->ops.i2c_gate_ctrl(fe, 0);
  97. priv->frequency = div * 1000;
  98. return (ret == 1) ? 0 : ret;
  99. }
  100. static int tda826x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  101. {
  102. struct tda826x_priv *priv = fe->tuner_priv;
  103. *frequency = priv->frequency;
  104. return 0;
  105. }
  106. static struct dvb_tuner_ops tda826x_tuner_ops = {
  107. .info = {
  108. .name = "Philips TDA826X",
  109. .frequency_min = 950000,
  110. .frequency_max = 2175000
  111. },
  112. .release = tda826x_release,
  113. .sleep = tda826x_sleep,
  114. .set_params = tda826x_set_params,
  115. .get_frequency = tda826x_get_frequency,
  116. };
  117. struct dvb_frontend *tda826x_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c, int has_loopthrough)
  118. {
  119. struct tda826x_priv *priv = NULL;
  120. u8 b1 [] = { 0, 0 };
  121. struct i2c_msg msg[2] = {
  122. { .addr = addr, .flags = 0, .buf = NULL, .len = 0 },
  123. { .addr = addr, .flags = I2C_M_RD, .buf = b1, .len = 2 }
  124. };
  125. int ret;
  126. dprintk("%s:\n", __func__);
  127. if (fe->ops.i2c_gate_ctrl)
  128. fe->ops.i2c_gate_ctrl(fe, 1);
  129. ret = i2c_transfer (i2c, msg, 2);
  130. if (fe->ops.i2c_gate_ctrl)
  131. fe->ops.i2c_gate_ctrl(fe, 0);
  132. if (ret != 2)
  133. return NULL;
  134. if (!(b1[1] & 0x80))
  135. return NULL;
  136. priv = kzalloc(sizeof(struct tda826x_priv), GFP_KERNEL);
  137. if (priv == NULL)
  138. return NULL;
  139. priv->i2c_address = addr;
  140. priv->i2c = i2c;
  141. priv->has_loopthrough = has_loopthrough;
  142. memcpy(&fe->ops.tuner_ops, &tda826x_tuner_ops, sizeof(struct dvb_tuner_ops));
  143. fe->tuner_priv = priv;
  144. return fe;
  145. }
  146. EXPORT_SYMBOL(tda826x_attach);
  147. module_param(debug, int, 0644);
  148. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  149. MODULE_DESCRIPTION("DVB TDA826x driver");
  150. MODULE_AUTHOR("Andrew de Quincey");
  151. MODULE_LICENSE("GPL");