mt2266.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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, struct dvb_frontend_parameters *params)
  103. {
  104. struct mt2266_priv *priv;
  105. int ret=0;
  106. u32 freq;
  107. u32 tune;
  108. u8 lnaband;
  109. u8 b[10];
  110. int i;
  111. u8 band;
  112. priv = fe->tuner_priv;
  113. freq = params->frequency / 1000; // Hz -> kHz
  114. if (freq < 470000 && freq > 230000)
  115. return -EINVAL; /* Gap between VHF and UHF bands */
  116. priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
  117. priv->frequency = freq * 1000;
  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 (params->u.ofdm.bandwidth) {
  123. case BANDWIDTH_6_MHZ:
  124. mt2266_writeregs(priv, mt2266_init_6mhz,
  125. sizeof(mt2266_init_6mhz));
  126. break;
  127. case BANDWIDTH_7_MHZ:
  128. mt2266_writeregs(priv, mt2266_init_7mhz,
  129. sizeof(mt2266_init_7mhz));
  130. break;
  131. case BANDWIDTH_8_MHZ:
  132. default:
  133. mt2266_writeregs(priv, mt2266_init_8mhz,
  134. sizeof(mt2266_init_8mhz));
  135. break;
  136. }
  137. if (band == MT2266_VHF && priv->band == MT2266_UHF) {
  138. dprintk("Switch from UHF to VHF");
  139. mt2266_writereg(priv, 0x05, 0x04);
  140. mt2266_writereg(priv, 0x19, 0x61);
  141. mt2266_writeregs(priv, mt2266_vhf, sizeof(mt2266_vhf));
  142. } else if (band == MT2266_UHF && priv->band == MT2266_VHF) {
  143. dprintk("Switch from VHF to UHF");
  144. mt2266_writereg(priv, 0x05, 0x52);
  145. mt2266_writereg(priv, 0x19, 0x61);
  146. mt2266_writeregs(priv, mt2266_uhf, sizeof(mt2266_uhf));
  147. }
  148. msleep(10);
  149. if (freq <= 495000)
  150. lnaband = 0xEE;
  151. else if (freq <= 525000)
  152. lnaband = 0xDD;
  153. else if (freq <= 550000)
  154. lnaband = 0xCC;
  155. else if (freq <= 580000)
  156. lnaband = 0xBB;
  157. else if (freq <= 605000)
  158. lnaband = 0xAA;
  159. else if (freq <= 630000)
  160. lnaband = 0x99;
  161. else if (freq <= 655000)
  162. lnaband = 0x88;
  163. else if (freq <= 685000)
  164. lnaband = 0x77;
  165. else if (freq <= 710000)
  166. lnaband = 0x66;
  167. else if (freq <= 735000)
  168. lnaband = 0x55;
  169. else if (freq <= 765000)
  170. lnaband = 0x44;
  171. else if (freq <= 802000)
  172. lnaband = 0x33;
  173. else if (freq <= 840000)
  174. lnaband = 0x22;
  175. else
  176. lnaband = 0x11;
  177. b[0] = REG_TUNE;
  178. b[1] = (tune >> 8) & 0x1F;
  179. b[2] = tune & 0xFF;
  180. b[3] = tune >> 13;
  181. mt2266_writeregs(priv,b,4);
  182. dprintk("set_parms: tune=%d band=%d %s",
  183. (int) tune, (int) lnaband,
  184. (band == MT2266_UHF) ? "UHF" : "VHF");
  185. dprintk("set_parms: [1..3]: %2x %2x %2x",
  186. (int) b[1], (int) b[2], (int)b[3]);
  187. if (band == MT2266_UHF) {
  188. b[0] = 0x05;
  189. b[1] = (priv->band == MT2266_VHF) ? 0x52 : 0x62;
  190. b[2] = lnaband;
  191. mt2266_writeregs(priv, b, 3);
  192. }
  193. /* Wait for pll lock or timeout */
  194. i = 0;
  195. do {
  196. mt2266_readreg(priv,REG_LOCK,b);
  197. if (b[0] & 0x40)
  198. break;
  199. msleep(10);
  200. i++;
  201. } while (i<10);
  202. dprintk("Lock when i=%i",(int)i);
  203. if (band == MT2266_UHF && priv->band == MT2266_VHF)
  204. mt2266_writereg(priv, 0x05, 0x62);
  205. priv->band = band;
  206. return ret;
  207. }
  208. static void mt2266_calibrate(struct mt2266_priv *priv)
  209. {
  210. mt2266_writereg(priv, 0x11, 0x03);
  211. mt2266_writereg(priv, 0x11, 0x01);
  212. mt2266_writeregs(priv, mt2266_init1, sizeof(mt2266_init1));
  213. mt2266_writeregs(priv, mt2266_init2, sizeof(mt2266_init2));
  214. mt2266_writereg(priv, 0x33, 0x5e);
  215. mt2266_writereg(priv, 0x10, 0x10);
  216. mt2266_writereg(priv, 0x10, 0x00);
  217. mt2266_writeregs(priv, mt2266_init_8mhz, sizeof(mt2266_init_8mhz));
  218. msleep(25);
  219. mt2266_writereg(priv, 0x17, 0x6d);
  220. mt2266_writereg(priv, 0x1c, 0x00);
  221. msleep(75);
  222. mt2266_writereg(priv, 0x17, 0x6d);
  223. mt2266_writereg(priv, 0x1c, 0xff);
  224. }
  225. static int mt2266_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  226. {
  227. struct mt2266_priv *priv = fe->tuner_priv;
  228. *frequency = priv->frequency;
  229. return 0;
  230. }
  231. static int mt2266_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  232. {
  233. struct mt2266_priv *priv = fe->tuner_priv;
  234. *bandwidth = priv->bandwidth;
  235. return 0;
  236. }
  237. static int mt2266_init(struct dvb_frontend *fe)
  238. {
  239. int ret;
  240. struct mt2266_priv *priv = fe->tuner_priv;
  241. ret = mt2266_writereg(priv, 0x17, 0x6d);
  242. if (ret < 0)
  243. return ret;
  244. ret = mt2266_writereg(priv, 0x1c, 0xff);
  245. if (ret < 0)
  246. return ret;
  247. return 0;
  248. }
  249. static int mt2266_sleep(struct dvb_frontend *fe)
  250. {
  251. struct mt2266_priv *priv = fe->tuner_priv;
  252. mt2266_writereg(priv, 0x17, 0x6d);
  253. mt2266_writereg(priv, 0x1c, 0x00);
  254. return 0;
  255. }
  256. static int mt2266_release(struct dvb_frontend *fe)
  257. {
  258. kfree(fe->tuner_priv);
  259. fe->tuner_priv = NULL;
  260. return 0;
  261. }
  262. static const struct dvb_tuner_ops mt2266_tuner_ops = {
  263. .info = {
  264. .name = "Microtune MT2266",
  265. .frequency_min = 174000000,
  266. .frequency_max = 862000000,
  267. .frequency_step = 50000,
  268. },
  269. .release = mt2266_release,
  270. .init = mt2266_init,
  271. .sleep = mt2266_sleep,
  272. .set_params = mt2266_set_params,
  273. .get_frequency = mt2266_get_frequency,
  274. .get_bandwidth = mt2266_get_bandwidth
  275. };
  276. struct dvb_frontend * mt2266_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2266_config *cfg)
  277. {
  278. struct mt2266_priv *priv = NULL;
  279. u8 id = 0;
  280. priv = kzalloc(sizeof(struct mt2266_priv), GFP_KERNEL);
  281. if (priv == NULL)
  282. return NULL;
  283. priv->cfg = cfg;
  284. priv->i2c = i2c;
  285. priv->band = MT2266_UHF;
  286. if (mt2266_readreg(priv, 0, &id)) {
  287. kfree(priv);
  288. return NULL;
  289. }
  290. if (id != PART_REV) {
  291. kfree(priv);
  292. return NULL;
  293. }
  294. printk(KERN_INFO "MT2266: successfully identified\n");
  295. memcpy(&fe->ops.tuner_ops, &mt2266_tuner_ops, sizeof(struct dvb_tuner_ops));
  296. fe->tuner_priv = priv;
  297. mt2266_calibrate(priv);
  298. return fe;
  299. }
  300. EXPORT_SYMBOL(mt2266_attach);
  301. MODULE_AUTHOR("Olivier DANET");
  302. MODULE_DESCRIPTION("Microtune MT2266 silicon tuner driver");
  303. MODULE_LICENSE("GPL");