tua6100.c 5.1 KB

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