tua6100.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. struct dvb_frontend_parameters *params)
  64. {
  65. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  66. struct tua6100_priv *priv = fe->tuner_priv;
  67. u32 div;
  68. u32 prediv;
  69. u8 reg0[] = { 0x00, 0x00 };
  70. u8 reg1[] = { 0x01, 0x00, 0x00, 0x00 };
  71. u8 reg2[] = { 0x02, 0x00, 0x00 };
  72. struct i2c_msg msg0 = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };
  73. struct i2c_msg msg1 = { .addr = priv->i2c_address, .flags = 0, .buf = reg1, .len = 4 };
  74. struct i2c_msg msg2 = { .addr = priv->i2c_address, .flags = 0, .buf = reg2, .len = 3 };
  75. #define _R 4
  76. #define _P 32
  77. #define _ri 4000000
  78. // setup register 0
  79. if (c->frequency < 2000000)
  80. reg0[1] = 0x03;
  81. else
  82. reg0[1] = 0x07;
  83. // setup register 1
  84. if (c->frequency < 1630000)
  85. reg1[1] = 0x2c;
  86. else
  87. reg1[1] = 0x0c;
  88. if (_P == 64)
  89. reg1[1] |= 0x40;
  90. if (c->frequency >= 1525000)
  91. reg1[1] |= 0x80;
  92. // register 2
  93. reg2[1] = (_R >> 8) & 0x03;
  94. reg2[2] = _R;
  95. if (c->frequency < 1455000)
  96. reg2[1] |= 0x1c;
  97. else if (c->frequency < 1630000)
  98. reg2[1] |= 0x0c;
  99. else
  100. reg2[1] |= 0x1c;
  101. /*
  102. * The N divisor ratio (note: c->frequency is in kHz, but we
  103. * need it in Hz)
  104. */
  105. prediv = (c->frequency * _R) / (_ri / 1000);
  106. div = prediv / _P;
  107. reg1[1] |= (div >> 9) & 0x03;
  108. reg1[2] = div >> 1;
  109. reg1[3] = (div << 7);
  110. priv->frequency = ((div * _P) * (_ri / 1000)) / _R;
  111. // Finally, calculate and store the value for A
  112. reg1[3] |= (prediv - (div*_P)) & 0x7f;
  113. #undef _R
  114. #undef _P
  115. #undef _ri
  116. if (fe->ops.i2c_gate_ctrl)
  117. fe->ops.i2c_gate_ctrl(fe, 1);
  118. if (i2c_transfer(priv->i2c, &msg0, 1) != 1)
  119. return -EIO;
  120. if (fe->ops.i2c_gate_ctrl)
  121. fe->ops.i2c_gate_ctrl(fe, 1);
  122. if (i2c_transfer(priv->i2c, &msg2, 1) != 1)
  123. return -EIO;
  124. if (fe->ops.i2c_gate_ctrl)
  125. fe->ops.i2c_gate_ctrl(fe, 1);
  126. if (i2c_transfer(priv->i2c, &msg1, 1) != 1)
  127. return -EIO;
  128. if (fe->ops.i2c_gate_ctrl)
  129. fe->ops.i2c_gate_ctrl(fe, 0);
  130. return 0;
  131. }
  132. static int tua6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  133. {
  134. struct tua6100_priv *priv = fe->tuner_priv;
  135. *frequency = priv->frequency;
  136. return 0;
  137. }
  138. static struct dvb_tuner_ops tua6100_tuner_ops = {
  139. .info = {
  140. .name = "Infineon TUA6100",
  141. .frequency_min = 950000,
  142. .frequency_max = 2150000,
  143. .frequency_step = 1000,
  144. },
  145. .release = tua6100_release,
  146. .sleep = tua6100_sleep,
  147. .set_params = tua6100_set_params,
  148. .get_frequency = tua6100_get_frequency,
  149. };
  150. struct dvb_frontend *tua6100_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c)
  151. {
  152. struct tua6100_priv *priv = NULL;
  153. u8 b1 [] = { 0x80 };
  154. u8 b2 [] = { 0x00 };
  155. struct i2c_msg msg [] = { { .addr = addr, .flags = 0, .buf = b1, .len = 1 },
  156. { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 } };
  157. int ret;
  158. if (fe->ops.i2c_gate_ctrl)
  159. fe->ops.i2c_gate_ctrl(fe, 1);
  160. ret = i2c_transfer (i2c, msg, 2);
  161. if (fe->ops.i2c_gate_ctrl)
  162. fe->ops.i2c_gate_ctrl(fe, 0);
  163. if (ret != 2)
  164. return NULL;
  165. priv = kzalloc(sizeof(struct tua6100_priv), GFP_KERNEL);
  166. if (priv == NULL)
  167. return NULL;
  168. priv->i2c_address = addr;
  169. priv->i2c = i2c;
  170. memcpy(&fe->ops.tuner_ops, &tua6100_tuner_ops, sizeof(struct dvb_tuner_ops));
  171. fe->tuner_priv = priv;
  172. return fe;
  173. }
  174. EXPORT_SYMBOL(tua6100_attach);
  175. MODULE_DESCRIPTION("DVB tua6100 driver");
  176. MODULE_AUTHOR("Andrew de Quincey");
  177. MODULE_LICENSE("GPL");