mt2266.c 8.9 KB

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