mt2131.c 7.5 KB

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