tda8261.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. TDA8261 8PSK/QPSK tuner driver
  4. Copyright (C) Manu Abraham (abraham.manu@gmail.com)
  5. */
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <media/dvb_frontend.h>
  11. #include "tda8261.h"
  12. struct tda8261_state {
  13. struct dvb_frontend *fe;
  14. struct i2c_adapter *i2c;
  15. const struct tda8261_config *config;
  16. /* state cache */
  17. u32 frequency;
  18. u32 bandwidth;
  19. };
  20. static int tda8261_read(struct tda8261_state *state, u8 *buf)
  21. {
  22. const struct tda8261_config *config = state->config;
  23. int err = 0;
  24. struct i2c_msg msg = { .addr = config->addr, .flags = I2C_M_RD,.buf = buf, .len = 1 };
  25. if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1)
  26. pr_err("%s: read error, err=%d\n", __func__, err);
  27. return err;
  28. }
  29. static int tda8261_write(struct tda8261_state *state, u8 *buf)
  30. {
  31. const struct tda8261_config *config = state->config;
  32. int err = 0;
  33. struct i2c_msg msg = { .addr = config->addr, .flags = 0, .buf = buf, .len = 4 };
  34. if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1)
  35. pr_err("%s: write error, err=%d\n", __func__, err);
  36. return err;
  37. }
  38. static int tda8261_get_status(struct dvb_frontend *fe, u32 *status)
  39. {
  40. struct tda8261_state *state = fe->tuner_priv;
  41. u8 result = 0;
  42. int err = 0;
  43. *status = 0;
  44. if ((err = tda8261_read(state, &result)) < 0) {
  45. pr_err("%s: I/O Error\n", __func__);
  46. return err;
  47. }
  48. if ((result >> 6) & 0x01) {
  49. pr_debug("%s: Tuner Phase Locked\n", __func__);
  50. *status = 1;
  51. }
  52. return err;
  53. }
  54. static const u32 div_tab[] = { 2000, 1000, 500, 250, 125 }; /* kHz */
  55. static const u8 ref_div[] = { 0x00, 0x01, 0x02, 0x05, 0x07 };
  56. static int tda8261_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  57. {
  58. struct tda8261_state *state = fe->tuner_priv;
  59. *frequency = state->frequency;
  60. return 0;
  61. }
  62. static int tda8261_set_params(struct dvb_frontend *fe)
  63. {
  64. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  65. struct tda8261_state *state = fe->tuner_priv;
  66. const struct tda8261_config *config = state->config;
  67. u32 frequency, N, status = 0;
  68. u8 buf[4];
  69. int err = 0;
  70. /*
  71. * N = Max VCO Frequency / Channel Spacing
  72. * Max VCO Frequency = VCO frequency + (channel spacing - 1)
  73. * (to account for half channel spacing on either side)
  74. */
  75. frequency = c->frequency;
  76. if ((frequency < 950000) || (frequency > 2150000)) {
  77. pr_warn("%s: Frequency beyond limits, frequency=%d\n",
  78. __func__, frequency);
  79. return -EINVAL;
  80. }
  81. N = (frequency + (div_tab[config->step_size] - 1)) / div_tab[config->step_size];
  82. pr_debug("%s: Step size=%d, Divider=%d, PG=0x%02x (%d)\n",
  83. __func__, config->step_size, div_tab[config->step_size], N, N);
  84. buf[0] = (N >> 8) & 0xff;
  85. buf[1] = N & 0xff;
  86. buf[2] = (0x01 << 7) | ((ref_div[config->step_size] & 0x07) << 1);
  87. if (frequency < 1450000)
  88. buf[3] = 0x00;
  89. else if (frequency < 2000000)
  90. buf[3] = 0x40;
  91. else if (frequency < 2150000)
  92. buf[3] = 0x80;
  93. /* Set params */
  94. err = tda8261_write(state, buf);
  95. if (err < 0) {
  96. pr_err("%s: I/O Error\n", __func__);
  97. return err;
  98. }
  99. /* sleep for some time */
  100. pr_debug("%s: Waiting to Phase LOCK\n", __func__);
  101. msleep(20);
  102. /* check status */
  103. if ((err = tda8261_get_status(fe, &status)) < 0) {
  104. pr_err("%s: I/O Error\n", __func__);
  105. return err;
  106. }
  107. if (status == 1) {
  108. pr_debug("%s: Tuner Phase locked: status=%d\n", __func__,
  109. status);
  110. state->frequency = frequency; /* cache successful state */
  111. } else {
  112. pr_debug("%s: No Phase lock: status=%d\n", __func__, status);
  113. }
  114. return 0;
  115. }
  116. static void tda8261_release(struct dvb_frontend *fe)
  117. {
  118. struct tda8261_state *state = fe->tuner_priv;
  119. fe->tuner_priv = NULL;
  120. kfree(state);
  121. }
  122. static const struct dvb_tuner_ops tda8261_ops = {
  123. .info = {
  124. .name = "TDA8261",
  125. .frequency_min_hz = 950 * MHz,
  126. .frequency_max_hz = 2150 * MHz,
  127. },
  128. .set_params = tda8261_set_params,
  129. .get_frequency = tda8261_get_frequency,
  130. .get_status = tda8261_get_status,
  131. .release = tda8261_release
  132. };
  133. struct dvb_frontend *tda8261_attach(struct dvb_frontend *fe,
  134. const struct tda8261_config *config,
  135. struct i2c_adapter *i2c)
  136. {
  137. struct tda8261_state *state = NULL;
  138. if ((state = kzalloc(sizeof (struct tda8261_state), GFP_KERNEL)) == NULL)
  139. goto exit;
  140. state->config = config;
  141. state->i2c = i2c;
  142. state->fe = fe;
  143. fe->tuner_priv = state;
  144. fe->ops.tuner_ops = tda8261_ops;
  145. fe->ops.tuner_ops.info.frequency_step_hz = div_tab[config->step_size] * kHz;
  146. pr_info("%s: Attaching TDA8261 8PSK/QPSK tuner\n", __func__);
  147. return fe;
  148. exit:
  149. kfree(state);
  150. return NULL;
  151. }
  152. EXPORT_SYMBOL(tda8261_attach);
  153. MODULE_AUTHOR("Manu Abraham");
  154. MODULE_DESCRIPTION("TDA8261 8PSK/QPSK Tuner");
  155. MODULE_LICENSE("GPL");