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 "dvb_frontend.h"
  20. #include "tda8261.h"
  21. struct tda8261_state {
  22. struct dvb_frontend *fe;
  23. struct i2c_adapter *i2c;
  24. const struct tda8261_config *config;
  25. /* state cache */
  26. u32 frequency;
  27. u32 bandwidth;
  28. };
  29. static int tda8261_read(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 = I2C_M_RD,.buf = buf, .len = 2 };
  34. if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1)
  35. printk("%s: read error, err=%d\n", __func__, err);
  36. return err;
  37. }
  38. static int tda8261_write(struct tda8261_state *state, u8 *buf)
  39. {
  40. const struct tda8261_config *config = state->config;
  41. int err = 0;
  42. struct i2c_msg msg = { .addr = config->addr, .flags = 0, .buf = buf, .len = 4 };
  43. if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1)
  44. printk("%s: write error, err=%d\n", __func__, err);
  45. return err;
  46. }
  47. static int tda8261_get_status(struct dvb_frontend *fe, u32 *status)
  48. {
  49. struct tda8261_state *state = fe->tuner_priv;
  50. u8 result = 0;
  51. int err = 0;
  52. *status = 0;
  53. if ((err = tda8261_read(state, &result)) < 0) {
  54. printk("%s: I/O Error\n", __func__);
  55. return err;
  56. }
  57. if ((result >> 6) & 0x01) {
  58. printk("%s: Tuner Phase Locked\n", __func__);
  59. *status = 1;
  60. }
  61. return err;
  62. }
  63. static const u32 div_tab[] = { 2000, 1000, 500, 250, 125 }; /* kHz */
  64. static const u8 ref_div[] = { 0x00, 0x01, 0x02, 0x05, 0x07 };
  65. static int tda8261_get_state(struct dvb_frontend *fe,
  66. enum tuner_param param,
  67. struct tuner_state *tstate)
  68. {
  69. struct tda8261_state *state = fe->tuner_priv;
  70. int err = 0;
  71. switch (param) {
  72. case DVBFE_TUNER_FREQUENCY:
  73. tstate->frequency = state->frequency;
  74. break;
  75. case DVBFE_TUNER_BANDWIDTH:
  76. tstate->bandwidth = 40000000; /* FIXME! need to calculate Bandwidth */
  77. break;
  78. default:
  79. printk("%s: Unknown parameter (param=%d)\n", __func__, param);
  80. err = -EINVAL;
  81. break;
  82. }
  83. return err;
  84. }
  85. static int tda8261_set_state(struct dvb_frontend *fe,
  86. enum tuner_param param,
  87. struct tuner_state *tstate)
  88. {
  89. struct tda8261_state *state = fe->tuner_priv;
  90. const struct tda8261_config *config = state->config;
  91. u32 frequency, N, status = 0;
  92. u8 buf[4];
  93. int err = 0;
  94. if (param & DVBFE_TUNER_FREQUENCY) {
  95. /**
  96. * N = Max VCO Frequency / Channel Spacing
  97. * Max VCO Frequency = VCO frequency + (channel spacing - 1)
  98. * (to account for half channel spacing on either side)
  99. */
  100. frequency = tstate->frequency;
  101. if ((frequency < 950000) || (frequency > 2150000)) {
  102. printk("%s: Frequency beyond limits, frequency=%d\n", __func__, frequency);
  103. return -EINVAL;
  104. }
  105. N = (frequency + (div_tab[config->step_size] - 1)) / div_tab[config->step_size];
  106. printk("%s: Step size=%d, Divider=%d, PG=0x%02x (%d)\n",
  107. __func__, config->step_size, div_tab[config->step_size], N, N);
  108. buf[0] = (N >> 8) & 0xff;
  109. buf[1] = N & 0xff;
  110. buf[2] = (0x01 << 7) | ((ref_div[config->step_size] & 0x07) << 1);
  111. if (frequency < 1450000)
  112. buf[3] = 0x00;
  113. if (frequency < 2000000)
  114. buf[3] = 0x40;
  115. if (frequency < 2150000)
  116. buf[3] = 0x80;
  117. /* Set params */
  118. if ((err = tda8261_write(state, buf)) < 0) {
  119. printk("%s: I/O Error\n", __func__);
  120. return err;
  121. }
  122. /* sleep for some time */
  123. printk("%s: Waiting to Phase LOCK\n", __func__);
  124. msleep(20);
  125. /* check status */
  126. if ((err = tda8261_get_status(fe, &status)) < 0) {
  127. printk("%s: I/O Error\n", __func__);
  128. return err;
  129. }
  130. if (status == 1) {
  131. printk("%s: Tuner Phase locked: status=%d\n", __func__, status);
  132. state->frequency = frequency; /* cache successful state */
  133. } else {
  134. printk("%s: No Phase lock: status=%d\n", __func__, status);
  135. }
  136. } else {
  137. printk("%s: Unknown parameter (param=%d)\n", __func__, param);
  138. return -EINVAL;
  139. }
  140. return 0;
  141. }
  142. static int tda8261_release(struct dvb_frontend *fe)
  143. {
  144. struct tda8261_state *state = fe->tuner_priv;
  145. fe->tuner_priv = NULL;
  146. kfree(state);
  147. return 0;
  148. }
  149. static struct dvb_tuner_ops tda8261_ops = {
  150. .info = {
  151. .name = "TDA8261",
  152. // .tuner_name = NULL,
  153. .frequency_min = 950000,
  154. .frequency_max = 2150000,
  155. .frequency_step = 0
  156. },
  157. .set_state = tda8261_set_state,
  158. .get_state = tda8261_get_state,
  159. .get_status = tda8261_get_status,
  160. .release = tda8261_release
  161. };
  162. struct dvb_frontend *tda8261_attach(struct dvb_frontend *fe,
  163. const struct tda8261_config *config,
  164. struct i2c_adapter *i2c)
  165. {
  166. struct tda8261_state *state = NULL;
  167. if ((state = kzalloc(sizeof (struct tda8261_state), GFP_KERNEL)) == NULL)
  168. goto exit;
  169. state->config = config;
  170. state->i2c = i2c;
  171. state->fe = fe;
  172. fe->tuner_priv = state;
  173. fe->ops.tuner_ops = tda8261_ops;
  174. fe->ops.tuner_ops.info.frequency_step = div_tab[config->step_size];
  175. // fe->ops.tuner_ops.tuner_name = &config->buf;
  176. // printk("%s: Attaching %s TDA8261 8PSK/QPSK tuner\n",
  177. // __func__, fe->ops.tuner_ops.tuner_name);
  178. printk("%s: Attaching TDA8261 8PSK/QPSK tuner\n", __func__);
  179. return fe;
  180. exit:
  181. kfree(state);
  182. return NULL;
  183. }
  184. EXPORT_SYMBOL(tda8261_attach);
  185. MODULE_PARM_DESC(verbose, "Set verbosity level");
  186. MODULE_AUTHOR("Manu Abraham");
  187. MODULE_DESCRIPTION("TDA8261 8PSK/QPSK Tuner");
  188. MODULE_LICENSE("GPL");