mt2266.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Microtune MT2266 "Direct conversion low power broadband tuner"
  4. *
  5. * Copyright (c) 2007 Olivier DANET <odanet@caramail.com>
  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 "mt2266.h"
  14. #define I2C_ADDRESS 0x60
  15. #define REG_PART_REV 0
  16. #define REG_TUNE 1
  17. #define REG_BAND 6
  18. #define REG_BANDWIDTH 8
  19. #define REG_LOCK 0x12
  20. #define PART_REV 0x85
  21. struct mt2266_priv {
  22. struct mt2266_config *cfg;
  23. struct i2c_adapter *i2c;
  24. u32 frequency;
  25. u32 bandwidth;
  26. u8 band;
  27. };
  28. #define MT2266_VHF 1
  29. #define MT2266_UHF 0
  30. /* Here, frequencies are expressed in kiloHertz to avoid 32 bits overflows */
  31. static int debug;
  32. module_param(debug, int, 0644);
  33. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  34. #define dprintk(args...) do { if (debug) {printk(KERN_DEBUG "MT2266: " args); printk("\n"); }} while (0)
  35. // Reads a single register
  36. static int mt2266_readreg(struct mt2266_priv *priv, u8 reg, u8 *val)
  37. {
  38. struct i2c_msg msg[2] = {
  39. { .addr = priv->cfg->i2c_address, .flags = 0, .buf = &reg, .len = 1 },
  40. { .addr = priv->cfg->i2c_address, .flags = I2C_M_RD, .buf = val, .len = 1 },
  41. };
  42. if (i2c_transfer(priv->i2c, msg, 2) != 2) {
  43. printk(KERN_WARNING "MT2266 I2C read failed\n");
  44. return -EREMOTEIO;
  45. }
  46. return 0;
  47. }
  48. // Writes a single register
  49. static int mt2266_writereg(struct mt2266_priv *priv, u8 reg, u8 val)
  50. {
  51. u8 buf[2] = { reg, val };
  52. struct i2c_msg msg = {
  53. .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = 2
  54. };
  55. if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
  56. printk(KERN_WARNING "MT2266 I2C write failed\n");
  57. return -EREMOTEIO;
  58. }
  59. return 0;
  60. }
  61. // Writes a set of consecutive registers
  62. static int mt2266_writeregs(struct mt2266_priv *priv,u8 *buf, u8 len)
  63. {
  64. struct i2c_msg msg = {
  65. .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = len
  66. };
  67. if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
  68. printk(KERN_WARNING "MT2266 I2C write failed (len=%i)\n",(int)len);
  69. return -EREMOTEIO;
  70. }
  71. return 0;
  72. }
  73. // Initialisation sequences
  74. static u8 mt2266_init1[] = { REG_TUNE, 0x00, 0x00, 0x28,
  75. 0x00, 0x52, 0x99, 0x3f };
  76. static u8 mt2266_init2[] = {
  77. 0x17, 0x6d, 0x71, 0x61, 0xc0, 0xbf, 0xff, 0xdc, 0x00, 0x0a, 0xd4,
  78. 0x03, 0x64, 0x64, 0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14,
  79. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x7f, 0x5e, 0x3f, 0xff, 0xff,
  80. 0xff, 0x00, 0x77, 0x0f, 0x2d
  81. };
  82. static u8 mt2266_init_8mhz[] = { REG_BANDWIDTH, 0x22, 0x22, 0x22, 0x22,
  83. 0x22, 0x22, 0x22, 0x22 };
  84. static u8 mt2266_init_7mhz[] = { REG_BANDWIDTH, 0x32, 0x32, 0x32, 0x32,
  85. 0x32, 0x32, 0x32, 0x32 };
  86. static u8 mt2266_init_6mhz[] = { REG_BANDWIDTH, 0xa7, 0xa7, 0xa7, 0xa7,
  87. 0xa7, 0xa7, 0xa7, 0xa7 };
  88. static u8 mt2266_uhf[] = { 0x1d, 0xdc, 0x00, 0x0a, 0xd4, 0x03, 0x64, 0x64,
  89. 0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14 };
  90. static u8 mt2266_vhf[] = { 0x1d, 0xfe, 0x00, 0x00, 0xb4, 0x03, 0xa5, 0xa5,
  91. 0xa5, 0xa5, 0x82, 0xaa, 0xf1, 0x17, 0x80, 0x1f };
  92. #define FREF 30000 // Quartz oscillator 30 MHz
  93. static int mt2266_set_params(struct dvb_frontend *fe)
  94. {
  95. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  96. struct mt2266_priv *priv;
  97. int ret=0;
  98. u32 freq;
  99. u32 tune;
  100. u8 lnaband;
  101. u8 b[10];
  102. int i;
  103. u8 band;
  104. priv = fe->tuner_priv;
  105. freq = priv->frequency / 1000; /* Hz -> kHz */
  106. if (freq < 470000 && freq > 230000)
  107. return -EINVAL; /* Gap between VHF and UHF bands */
  108. priv->frequency = c->frequency;
  109. tune = 2 * freq * (8192/16) / (FREF/16);
  110. band = (freq < 300000) ? MT2266_VHF : MT2266_UHF;
  111. if (band == MT2266_VHF)
  112. tune *= 2;
  113. switch (c->bandwidth_hz) {
  114. case 6000000:
  115. mt2266_writeregs(priv, mt2266_init_6mhz,
  116. sizeof(mt2266_init_6mhz));
  117. break;
  118. case 8000000:
  119. mt2266_writeregs(priv, mt2266_init_8mhz,
  120. sizeof(mt2266_init_8mhz));
  121. break;
  122. case 7000000:
  123. default:
  124. mt2266_writeregs(priv, mt2266_init_7mhz,
  125. sizeof(mt2266_init_7mhz));
  126. break;
  127. }
  128. priv->bandwidth = c->bandwidth_hz;
  129. if (band == MT2266_VHF && priv->band == MT2266_UHF) {
  130. dprintk("Switch from UHF to VHF");
  131. mt2266_writereg(priv, 0x05, 0x04);
  132. mt2266_writereg(priv, 0x19, 0x61);
  133. mt2266_writeregs(priv, mt2266_vhf, sizeof(mt2266_vhf));
  134. } else if (band == MT2266_UHF && priv->band == MT2266_VHF) {
  135. dprintk("Switch from VHF to UHF");
  136. mt2266_writereg(priv, 0x05, 0x52);
  137. mt2266_writereg(priv, 0x19, 0x61);
  138. mt2266_writeregs(priv, mt2266_uhf, sizeof(mt2266_uhf));
  139. }
  140. msleep(10);
  141. if (freq <= 495000)
  142. lnaband = 0xEE;
  143. else if (freq <= 525000)
  144. lnaband = 0xDD;
  145. else if (freq <= 550000)
  146. lnaband = 0xCC;
  147. else if (freq <= 580000)
  148. lnaband = 0xBB;
  149. else if (freq <= 605000)
  150. lnaband = 0xAA;
  151. else if (freq <= 630000)
  152. lnaband = 0x99;
  153. else if (freq <= 655000)
  154. lnaband = 0x88;
  155. else if (freq <= 685000)
  156. lnaband = 0x77;
  157. else if (freq <= 710000)
  158. lnaband = 0x66;
  159. else if (freq <= 735000)
  160. lnaband = 0x55;
  161. else if (freq <= 765000)
  162. lnaband = 0x44;
  163. else if (freq <= 802000)
  164. lnaband = 0x33;
  165. else if (freq <= 840000)
  166. lnaband = 0x22;
  167. else
  168. lnaband = 0x11;
  169. b[0] = REG_TUNE;
  170. b[1] = (tune >> 8) & 0x1F;
  171. b[2] = tune & 0xFF;
  172. b[3] = tune >> 13;
  173. mt2266_writeregs(priv,b,4);
  174. dprintk("set_parms: tune=%d band=%d %s",
  175. (int) tune, (int) lnaband,
  176. (band == MT2266_UHF) ? "UHF" : "VHF");
  177. dprintk("set_parms: [1..3]: %2x %2x %2x",
  178. (int) b[1], (int) b[2], (int)b[3]);
  179. if (band == MT2266_UHF) {
  180. b[0] = 0x05;
  181. b[1] = (priv->band == MT2266_VHF) ? 0x52 : 0x62;
  182. b[2] = lnaband;
  183. mt2266_writeregs(priv, b, 3);
  184. }
  185. /* Wait for pll lock or timeout */
  186. i = 0;
  187. do {
  188. mt2266_readreg(priv,REG_LOCK,b);
  189. if (b[0] & 0x40)
  190. break;
  191. msleep(10);
  192. i++;
  193. } while (i<10);
  194. dprintk("Lock when i=%i",(int)i);
  195. if (band == MT2266_UHF && priv->band == MT2266_VHF)
  196. mt2266_writereg(priv, 0x05, 0x62);
  197. priv->band = band;
  198. return ret;
  199. }
  200. static void mt2266_calibrate(struct mt2266_priv *priv)
  201. {
  202. mt2266_writereg(priv, 0x11, 0x03);
  203. mt2266_writereg(priv, 0x11, 0x01);
  204. mt2266_writeregs(priv, mt2266_init1, sizeof(mt2266_init1));
  205. mt2266_writeregs(priv, mt2266_init2, sizeof(mt2266_init2));
  206. mt2266_writereg(priv, 0x33, 0x5e);
  207. mt2266_writereg(priv, 0x10, 0x10);
  208. mt2266_writereg(priv, 0x10, 0x00);
  209. mt2266_writeregs(priv, mt2266_init_8mhz, sizeof(mt2266_init_8mhz));
  210. msleep(25);
  211. mt2266_writereg(priv, 0x17, 0x6d);
  212. mt2266_writereg(priv, 0x1c, 0x00);
  213. msleep(75);
  214. mt2266_writereg(priv, 0x17, 0x6d);
  215. mt2266_writereg(priv, 0x1c, 0xff);
  216. }
  217. static int mt2266_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  218. {
  219. struct mt2266_priv *priv = fe->tuner_priv;
  220. *frequency = priv->frequency;
  221. return 0;
  222. }
  223. static int mt2266_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  224. {
  225. struct mt2266_priv *priv = fe->tuner_priv;
  226. *bandwidth = priv->bandwidth;
  227. return 0;
  228. }
  229. static int mt2266_init(struct dvb_frontend *fe)
  230. {
  231. int ret;
  232. struct mt2266_priv *priv = fe->tuner_priv;
  233. ret = mt2266_writereg(priv, 0x17, 0x6d);
  234. if (ret < 0)
  235. return ret;
  236. ret = mt2266_writereg(priv, 0x1c, 0xff);
  237. if (ret < 0)
  238. return ret;
  239. return 0;
  240. }
  241. static int mt2266_sleep(struct dvb_frontend *fe)
  242. {
  243. struct mt2266_priv *priv = fe->tuner_priv;
  244. mt2266_writereg(priv, 0x17, 0x6d);
  245. mt2266_writereg(priv, 0x1c, 0x00);
  246. return 0;
  247. }
  248. static void mt2266_release(struct dvb_frontend *fe)
  249. {
  250. kfree(fe->tuner_priv);
  251. fe->tuner_priv = NULL;
  252. }
  253. static const struct dvb_tuner_ops mt2266_tuner_ops = {
  254. .info = {
  255. .name = "Microtune MT2266",
  256. .frequency_min_hz = 174 * MHz,
  257. .frequency_max_hz = 862 * MHz,
  258. .frequency_step_hz = 50 * kHz,
  259. },
  260. .release = mt2266_release,
  261. .init = mt2266_init,
  262. .sleep = mt2266_sleep,
  263. .set_params = mt2266_set_params,
  264. .get_frequency = mt2266_get_frequency,
  265. .get_bandwidth = mt2266_get_bandwidth
  266. };
  267. struct dvb_frontend * mt2266_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2266_config *cfg)
  268. {
  269. struct mt2266_priv *priv = NULL;
  270. u8 id = 0;
  271. priv = kzalloc(sizeof(struct mt2266_priv), GFP_KERNEL);
  272. if (priv == NULL)
  273. return NULL;
  274. priv->cfg = cfg;
  275. priv->i2c = i2c;
  276. priv->band = MT2266_UHF;
  277. if (mt2266_readreg(priv, 0, &id)) {
  278. kfree(priv);
  279. return NULL;
  280. }
  281. if (id != PART_REV) {
  282. kfree(priv);
  283. return NULL;
  284. }
  285. printk(KERN_INFO "MT2266: successfully identified\n");
  286. memcpy(&fe->ops.tuner_ops, &mt2266_tuner_ops, sizeof(struct dvb_tuner_ops));
  287. fe->tuner_priv = priv;
  288. mt2266_calibrate(priv);
  289. return fe;
  290. }
  291. EXPORT_SYMBOL(mt2266_attach);
  292. MODULE_AUTHOR("Olivier DANET");
  293. MODULE_DESCRIPTION("Microtune MT2266 silicon tuner driver");
  294. MODULE_LICENSE("GPL");