tda8261.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "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_state(struct dvb_frontend *fe,
  67. enum tuner_param param,
  68. struct tuner_state *tstate)
  69. {
  70. struct tda8261_state *state = fe->tuner_priv;
  71. int err = 0;
  72. switch (param) {
  73. case DVBFE_TUNER_FREQUENCY:
  74. tstate->frequency = state->frequency;
  75. break;
  76. case DVBFE_TUNER_BANDWIDTH:
  77. tstate->bandwidth = 40000000; /* FIXME! need to calculate Bandwidth */
  78. break;
  79. default:
  80. pr_err("%s: Unknown parameter (param=%d)\n", __func__, param);
  81. err = -EINVAL;
  82. break;
  83. }
  84. return err;
  85. }
  86. static int tda8261_set_state(struct dvb_frontend *fe,
  87. enum tuner_param param,
  88. struct tuner_state *tstate)
  89. {
  90. struct tda8261_state *state = fe->tuner_priv;
  91. const struct tda8261_config *config = state->config;
  92. u32 frequency, N, status = 0;
  93. u8 buf[4];
  94. int err = 0;
  95. if (param & DVBFE_TUNER_FREQUENCY) {
  96. /**
  97. * N = Max VCO Frequency / Channel Spacing
  98. * Max VCO Frequency = VCO frequency + (channel spacing - 1)
  99. * (to account for half channel spacing on either side)
  100. */
  101. frequency = tstate->frequency;
  102. if ((frequency < 950000) || (frequency > 2150000)) {
  103. pr_warn("%s: Frequency beyond limits, frequency=%d\n", __func__, frequency);
  104. return -EINVAL;
  105. }
  106. N = (frequency + (div_tab[config->step_size] - 1)) / div_tab[config->step_size];
  107. pr_debug("%s: Step size=%d, Divider=%d, PG=0x%02x (%d)\n",
  108. __func__, config->step_size, div_tab[config->step_size], N, N);
  109. buf[0] = (N >> 8) & 0xff;
  110. buf[1] = N & 0xff;
  111. buf[2] = (0x01 << 7) | ((ref_div[config->step_size] & 0x07) << 1);
  112. if (frequency < 1450000)
  113. buf[3] = 0x00;
  114. else if (frequency < 2000000)
  115. buf[3] = 0x40;
  116. else if (frequency < 2150000)
  117. buf[3] = 0x80;
  118. /* Set params */
  119. if ((err = tda8261_write(state, buf)) < 0) {
  120. pr_err("%s: I/O Error\n", __func__);
  121. return err;
  122. }
  123. /* sleep for some time */
  124. pr_debug("%s: Waiting to Phase LOCK\n", __func__);
  125. msleep(20);
  126. /* check status */
  127. if ((err = tda8261_get_status(fe, &status)) < 0) {
  128. pr_err("%s: I/O Error\n", __func__);
  129. return err;
  130. }
  131. if (status == 1) {
  132. pr_debug("%s: Tuner Phase locked: status=%d\n", __func__, status);
  133. state->frequency = frequency; /* cache successful state */
  134. } else {
  135. pr_debug("%s: No Phase lock: status=%d\n", __func__, status);
  136. }
  137. } else {
  138. pr_err("%s: Unknown parameter (param=%d)\n", __func__, param);
  139. return -EINVAL;
  140. }
  141. return 0;
  142. }
  143. static int tda8261_release(struct dvb_frontend *fe)
  144. {
  145. struct tda8261_state *state = fe->tuner_priv;
  146. fe->tuner_priv = NULL;
  147. kfree(state);
  148. return 0;
  149. }
  150. static struct dvb_tuner_ops tda8261_ops = {
  151. .info = {
  152. .name = "TDA8261",
  153. // .tuner_name = NULL,
  154. .frequency_min = 950000,
  155. .frequency_max = 2150000,
  156. .frequency_step = 0
  157. },
  158. .set_state = tda8261_set_state,
  159. .get_state = tda8261_get_state,
  160. .get_status = tda8261_get_status,
  161. .release = tda8261_release
  162. };
  163. struct dvb_frontend *tda8261_attach(struct dvb_frontend *fe,
  164. const struct tda8261_config *config,
  165. struct i2c_adapter *i2c)
  166. {
  167. struct tda8261_state *state = NULL;
  168. if ((state = kzalloc(sizeof (struct tda8261_state), GFP_KERNEL)) == NULL)
  169. goto exit;
  170. state->config = config;
  171. state->i2c = i2c;
  172. state->fe = fe;
  173. fe->tuner_priv = state;
  174. fe->ops.tuner_ops = tda8261_ops;
  175. fe->ops.tuner_ops.info.frequency_step = div_tab[config->step_size];
  176. // fe->ops.tuner_ops.tuner_name = &config->buf;
  177. // printk("%s: Attaching %s TDA8261 8PSK/QPSK tuner\n",
  178. // __func__, fe->ops.tuner_ops.tuner_name);
  179. pr_info("%s: Attaching TDA8261 8PSK/QPSK tuner\n", __func__);
  180. return fe;
  181. exit:
  182. kfree(state);
  183. return NULL;
  184. }
  185. EXPORT_SYMBOL(tda8261_attach);
  186. MODULE_AUTHOR("Manu Abraham");
  187. MODULE_DESCRIPTION("TDA8261 8PSK/QPSK Tuner");
  188. MODULE_LICENSE("GPL");