mt2131.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Driver for Microtune MT2131 "QAM/8VSB single chip tuner"
  3. *
  4. * Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/dvb/frontend.h>
  24. #include <linux/i2c.h>
  25. #include <linux/slab.h>
  26. #include "dvb_frontend.h"
  27. #include "mt2131.h"
  28. #include "mt2131_priv.h"
  29. static int debug;
  30. module_param(debug, int, 0644);
  31. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  32. #define dprintk(level,fmt, arg...) if (debug >= level) \
  33. printk(KERN_INFO "%s: " fmt, "mt2131", ## arg)
  34. static u8 mt2131_config1[] = {
  35. 0x01,
  36. 0x50, 0x00, 0x50, 0x80, 0x00, 0x49, 0xfa, 0x88,
  37. 0x08, 0x77, 0x41, 0x04, 0x00, 0x00, 0x00, 0x32,
  38. 0x7f, 0xda, 0x4c, 0x00, 0x10, 0xaa, 0x78, 0x80,
  39. 0xff, 0x68, 0xa0, 0xff, 0xdd, 0x00, 0x00
  40. };
  41. static u8 mt2131_config2[] = {
  42. 0x10,
  43. 0x7f, 0xc8, 0x0a, 0x5f, 0x00, 0x04
  44. };
  45. static int mt2131_readreg(struct mt2131_priv *priv, u8 reg, u8 *val)
  46. {
  47. struct i2c_msg msg[2] = {
  48. { .addr = priv->cfg->i2c_address, .flags = 0,
  49. .buf = &reg, .len = 1 },
  50. { .addr = priv->cfg->i2c_address, .flags = I2C_M_RD,
  51. .buf = val, .len = 1 },
  52. };
  53. if (i2c_transfer(priv->i2c, msg, 2) != 2) {
  54. printk(KERN_WARNING "mt2131 I2C read failed\n");
  55. return -EREMOTEIO;
  56. }
  57. return 0;
  58. }
  59. static int mt2131_writereg(struct mt2131_priv *priv, u8 reg, u8 val)
  60. {
  61. u8 buf[2] = { reg, val };
  62. struct i2c_msg msg = { .addr = priv->cfg->i2c_address, .flags = 0,
  63. .buf = buf, .len = 2 };
  64. if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
  65. printk(KERN_WARNING "mt2131 I2C write failed\n");
  66. return -EREMOTEIO;
  67. }
  68. return 0;
  69. }
  70. static int mt2131_writeregs(struct mt2131_priv *priv,u8 *buf, u8 len)
  71. {
  72. struct i2c_msg msg = { .addr = priv->cfg->i2c_address,
  73. .flags = 0, .buf = buf, .len = len };
  74. if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
  75. printk(KERN_WARNING "mt2131 I2C write failed (len=%i)\n",
  76. (int)len);
  77. return -EREMOTEIO;
  78. }
  79. return 0;
  80. }
  81. static int mt2131_set_params(struct dvb_frontend *fe)
  82. {
  83. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  84. struct mt2131_priv *priv;
  85. int ret=0, i;
  86. u32 freq;
  87. u8 if_band_center;
  88. u32 f_lo1, f_lo2;
  89. u32 div1, num1, div2, num2;
  90. u8 b[8];
  91. u8 lockval = 0;
  92. priv = fe->tuner_priv;
  93. freq = c->frequency / 1000; /* Hz -> kHz */
  94. dprintk(1, "%s() freq=%d\n", __func__, freq);
  95. f_lo1 = freq + MT2131_IF1 * 1000;
  96. f_lo1 = (f_lo1 / 250) * 250;
  97. f_lo2 = f_lo1 - freq - MT2131_IF2;
  98. priv->frequency = (f_lo1 - f_lo2 - MT2131_IF2) * 1000;
  99. /* Frequency LO1 = 16MHz * (DIV1 + NUM1/8192 ) */
  100. num1 = f_lo1 * 64 / (MT2131_FREF / 128);
  101. div1 = num1 / 8192;
  102. num1 &= 0x1fff;
  103. /* Frequency LO2 = 16MHz * (DIV2 + NUM2/8192 ) */
  104. num2 = f_lo2 * 64 / (MT2131_FREF / 128);
  105. div2 = num2 / 8192;
  106. num2 &= 0x1fff;
  107. if (freq <= 82500) if_band_center = 0x00; else
  108. if (freq <= 137500) if_band_center = 0x01; else
  109. if (freq <= 192500) if_band_center = 0x02; else
  110. if (freq <= 247500) if_band_center = 0x03; else
  111. if (freq <= 302500) if_band_center = 0x04; else
  112. if (freq <= 357500) if_band_center = 0x05; else
  113. if (freq <= 412500) if_band_center = 0x06; else
  114. if (freq <= 467500) if_band_center = 0x07; else
  115. if (freq <= 522500) if_band_center = 0x08; else
  116. if (freq <= 577500) if_band_center = 0x09; else
  117. if (freq <= 632500) if_band_center = 0x0A; else
  118. if (freq <= 687500) if_band_center = 0x0B; else
  119. if (freq <= 742500) if_band_center = 0x0C; else
  120. if (freq <= 797500) if_band_center = 0x0D; else
  121. if (freq <= 852500) if_band_center = 0x0E; else
  122. if (freq <= 907500) if_band_center = 0x0F; else
  123. if (freq <= 962500) if_band_center = 0x10; else
  124. if (freq <= 1017500) if_band_center = 0x11; else
  125. if (freq <= 1072500) if_band_center = 0x12; else if_band_center = 0x13;
  126. b[0] = 1;
  127. b[1] = (num1 >> 5) & 0xFF;
  128. b[2] = (num1 & 0x1F);
  129. b[3] = div1;
  130. b[4] = (num2 >> 5) & 0xFF;
  131. b[5] = num2 & 0x1F;
  132. b[6] = div2;
  133. dprintk(1, "IF1: %dMHz IF2: %dMHz\n", MT2131_IF1, MT2131_IF2);
  134. dprintk(1, "PLL freq=%dkHz band=%d\n", (int)freq, (int)if_band_center);
  135. dprintk(1, "PLL f_lo1=%dkHz f_lo2=%dkHz\n", (int)f_lo1, (int)f_lo2);
  136. dprintk(1, "PLL div1=%d num1=%d div2=%d num2=%d\n",
  137. (int)div1, (int)num1, (int)div2, (int)num2);
  138. dprintk(1, "PLL [1..6]: %2x %2x %2x %2x %2x %2x\n",
  139. (int)b[1], (int)b[2], (int)b[3], (int)b[4], (int)b[5],
  140. (int)b[6]);
  141. ret = mt2131_writeregs(priv,b,7);
  142. if (ret < 0)
  143. return ret;
  144. mt2131_writereg(priv, 0x0b, if_band_center);
  145. /* Wait for lock */
  146. i = 0;
  147. do {
  148. mt2131_readreg(priv, 0x08, &lockval);
  149. if ((lockval & 0x88) == 0x88)
  150. break;
  151. msleep(4);
  152. i++;
  153. } while (i < 10);
  154. return ret;
  155. }
  156. static int mt2131_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  157. {
  158. struct mt2131_priv *priv = fe->tuner_priv;
  159. dprintk(1, "%s()\n", __func__);
  160. *frequency = priv->frequency;
  161. return 0;
  162. }
  163. static int mt2131_get_status(struct dvb_frontend *fe, u32 *status)
  164. {
  165. struct mt2131_priv *priv = fe->tuner_priv;
  166. u8 lock_status = 0;
  167. u8 afc_status = 0;
  168. *status = 0;
  169. mt2131_readreg(priv, 0x08, &lock_status);
  170. if ((lock_status & 0x88) == 0x88)
  171. *status = TUNER_STATUS_LOCKED;
  172. mt2131_readreg(priv, 0x09, &afc_status);
  173. dprintk(1, "%s() - LO Status = 0x%x, AFC Status = 0x%x\n",
  174. __func__, lock_status, afc_status);
  175. return 0;
  176. }
  177. static int mt2131_init(struct dvb_frontend *fe)
  178. {
  179. struct mt2131_priv *priv = fe->tuner_priv;
  180. int ret;
  181. dprintk(1, "%s()\n", __func__);
  182. if ((ret = mt2131_writeregs(priv, mt2131_config1,
  183. sizeof(mt2131_config1))) < 0)
  184. return ret;
  185. mt2131_writereg(priv, 0x0b, 0x09);
  186. mt2131_writereg(priv, 0x15, 0x47);
  187. mt2131_writereg(priv, 0x07, 0xf2);
  188. mt2131_writereg(priv, 0x0b, 0x01);
  189. if ((ret = mt2131_writeregs(priv, mt2131_config2,
  190. sizeof(mt2131_config2))) < 0)
  191. return ret;
  192. return ret;
  193. }
  194. static int mt2131_release(struct dvb_frontend *fe)
  195. {
  196. dprintk(1, "%s()\n", __func__);
  197. kfree(fe->tuner_priv);
  198. fe->tuner_priv = NULL;
  199. return 0;
  200. }
  201. static const struct dvb_tuner_ops mt2131_tuner_ops = {
  202. .info = {
  203. .name = "Microtune MT2131",
  204. .frequency_min = 48000000,
  205. .frequency_max = 860000000,
  206. .frequency_step = 50000,
  207. },
  208. .release = mt2131_release,
  209. .init = mt2131_init,
  210. .set_params = mt2131_set_params,
  211. .get_frequency = mt2131_get_frequency,
  212. .get_status = mt2131_get_status
  213. };
  214. struct dvb_frontend * mt2131_attach(struct dvb_frontend *fe,
  215. struct i2c_adapter *i2c,
  216. struct mt2131_config *cfg, u16 if1)
  217. {
  218. struct mt2131_priv *priv = NULL;
  219. u8 id = 0;
  220. dprintk(1, "%s()\n", __func__);
  221. priv = kzalloc(sizeof(struct mt2131_priv), GFP_KERNEL);
  222. if (priv == NULL)
  223. return NULL;
  224. priv->cfg = cfg;
  225. priv->i2c = i2c;
  226. if (mt2131_readreg(priv, 0, &id) != 0) {
  227. kfree(priv);
  228. return NULL;
  229. }
  230. if ( (id != 0x3E) && (id != 0x3F) ) {
  231. printk(KERN_ERR "MT2131: Device not found at addr 0x%02x\n",
  232. cfg->i2c_address);
  233. kfree(priv);
  234. return NULL;
  235. }
  236. printk(KERN_INFO "MT2131: successfully identified at address 0x%02x\n",
  237. cfg->i2c_address);
  238. memcpy(&fe->ops.tuner_ops, &mt2131_tuner_ops,
  239. sizeof(struct dvb_tuner_ops));
  240. fe->tuner_priv = priv;
  241. return fe;
  242. }
  243. EXPORT_SYMBOL(mt2131_attach);
  244. MODULE_AUTHOR("Steven Toth");
  245. MODULE_DESCRIPTION("Microtune MT2131 silicon tuner driver");
  246. MODULE_LICENSE("GPL");