tua6100.c 5.3 KB

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