mt2131.c 7.1 KB

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