tda8261.c 5.2 KB

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