tda826x.c 4.3 KB

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