tda8261.c 5.5 KB

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