tua6100.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Driver for Infineon tua6100 pll.
  3. *
  4. * (c) 2006 Andrew de Quincey
  5. *
  6. * Based on code found in budget-av.c, which has the following:
  7. * Compiled from various sources by Michael Hunold <michael@mihu.de>
  8. *
  9. * CI interface support (c) 2004 Olivier Gournet <ogournet@anevia.com> &
  10. * Andrew de Quincey <adq_dvb@lidskialf.net>
  11. *
  12. * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
  13. *
  14. * Copyright (C) 1999-2002 Ralph Metzler
  15. * & Marcus Metzler for convergence integrated media GmbH
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. */
  30. #include <linux/slab.h>
  31. #include <linux/module.h>
  32. #include <linux/dvb/frontend.h>
  33. #include <asm/types.h>
  34. #include "tua6100.h"
  35. struct tua6100_priv {
  36. /* i2c details */
  37. int i2c_address;
  38. struct i2c_adapter *i2c;
  39. u32 frequency;
  40. };
  41. static int tua6100_release(struct dvb_frontend *fe)
  42. {
  43. kfree(fe->tuner_priv);
  44. fe->tuner_priv = NULL;
  45. return 0;
  46. }
  47. static int tua6100_sleep(struct dvb_frontend *fe)
  48. {
  49. struct tua6100_priv *priv = fe->tuner_priv;
  50. int ret;
  51. u8 reg0[] = { 0x00, 0x00 };
  52. struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };
  53. if (fe->ops.i2c_gate_ctrl)
  54. fe->ops.i2c_gate_ctrl(fe, 1);
  55. if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
  56. printk("%s: i2c error\n", __func__);
  57. }
  58. if (fe->ops.i2c_gate_ctrl)
  59. fe->ops.i2c_gate_ctrl(fe, 0);
  60. return (ret == 1) ? 0 : ret;
  61. }
  62. static int tua6100_set_params(struct dvb_frontend *fe)
  63. {
  64. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  65. struct tua6100_priv *priv = fe->tuner_priv;
  66. u32 div;
  67. u32 prediv;
  68. u8 reg0[] = { 0x00, 0x00 };
  69. u8 reg1[] = { 0x01, 0x00, 0x00, 0x00 };
  70. u8 reg2[] = { 0x02, 0x00, 0x00 };
  71. struct i2c_msg msg0 = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };
  72. struct i2c_msg msg1 = { .addr = priv->i2c_address, .flags = 0, .buf = reg1, .len = 4 };
  73. struct i2c_msg msg2 = { .addr = priv->i2c_address, .flags = 0, .buf = reg2, .len = 3 };
  74. #define _R 4
  75. #define _P 32
  76. #define _ri 4000000
  77. // setup register 0
  78. if (c->frequency < 2000000)
  79. reg0[1] = 0x03;
  80. else
  81. reg0[1] = 0x07;
  82. // setup register 1
  83. if (c->frequency < 1630000)
  84. reg1[1] = 0x2c;
  85. else
  86. reg1[1] = 0x0c;
  87. if (_P == 64)
  88. reg1[1] |= 0x40;
  89. if (c->frequency >= 1525000)
  90. reg1[1] |= 0x80;
  91. // register 2
  92. reg2[1] = (_R >> 8) & 0x03;
  93. reg2[2] = _R;
  94. if (c->frequency < 1455000)
  95. reg2[1] |= 0x1c;
  96. else if (c->frequency < 1630000)
  97. reg2[1] |= 0x0c;
  98. else
  99. reg2[1] |= 0x1c;
  100. /*
  101. * The N divisor ratio (note: c->frequency is in kHz, but we
  102. * need it in Hz)
  103. */
  104. prediv = (c->frequency * _R) / (_ri / 1000);
  105. div = prediv / _P;
  106. reg1[1] |= (div >> 9) & 0x03;
  107. reg1[2] = div >> 1;
  108. reg1[3] = (div << 7);
  109. priv->frequency = ((div * _P) * (_ri / 1000)) / _R;
  110. // Finally, calculate and store the value for A
  111. reg1[3] |= (prediv - (div*_P)) & 0x7f;
  112. #undef _R
  113. #undef _P
  114. #undef _ri
  115. if (fe->ops.i2c_gate_ctrl)
  116. fe->ops.i2c_gate_ctrl(fe, 1);
  117. if (i2c_transfer(priv->i2c, &msg0, 1) != 1)
  118. return -EIO;
  119. if (fe->ops.i2c_gate_ctrl)
  120. fe->ops.i2c_gate_ctrl(fe, 1);
  121. if (i2c_transfer(priv->i2c, &msg2, 1) != 1)
  122. return -EIO;
  123. if (fe->ops.i2c_gate_ctrl)
  124. fe->ops.i2c_gate_ctrl(fe, 1);
  125. if (i2c_transfer(priv->i2c, &msg1, 1) != 1)
  126. return -EIO;
  127. if (fe->ops.i2c_gate_ctrl)
  128. fe->ops.i2c_gate_ctrl(fe, 0);
  129. return 0;
  130. }
  131. static int tua6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  132. {
  133. struct tua6100_priv *priv = fe->tuner_priv;
  134. *frequency = priv->frequency;
  135. return 0;
  136. }
  137. static struct dvb_tuner_ops tua6100_tuner_ops = {
  138. .info = {
  139. .name = "Infineon TUA6100",
  140. .frequency_min = 950000,
  141. .frequency_max = 2150000,
  142. .frequency_step = 1000,
  143. },
  144. .release = tua6100_release,
  145. .sleep = tua6100_sleep,
  146. .set_params = tua6100_set_params,
  147. .get_frequency = tua6100_get_frequency,
  148. };
  149. struct dvb_frontend *tua6100_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c)
  150. {
  151. struct tua6100_priv *priv = NULL;
  152. u8 b1 [] = { 0x80 };
  153. u8 b2 [] = { 0x00 };
  154. struct i2c_msg msg [] = { { .addr = addr, .flags = 0, .buf = b1, .len = 1 },
  155. { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 } };
  156. int ret;
  157. if (fe->ops.i2c_gate_ctrl)
  158. fe->ops.i2c_gate_ctrl(fe, 1);
  159. ret = i2c_transfer (i2c, msg, 2);
  160. if (fe->ops.i2c_gate_ctrl)
  161. fe->ops.i2c_gate_ctrl(fe, 0);
  162. if (ret != 2)
  163. return NULL;
  164. priv = kzalloc(sizeof(struct tua6100_priv), GFP_KERNEL);
  165. if (priv == NULL)
  166. return NULL;
  167. priv->i2c_address = addr;
  168. priv->i2c = i2c;
  169. memcpy(&fe->ops.tuner_ops, &tua6100_tuner_ops, sizeof(struct dvb_tuner_ops));
  170. fe->tuner_priv = priv;
  171. return fe;
  172. }
  173. EXPORT_SYMBOL(tua6100_attach);
  174. MODULE_DESCRIPTION("DVB tua6100 driver");
  175. MODULE_AUTHOR("Andrew de Quincey");
  176. MODULE_LICENSE("GPL");