mt2266.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "dvb_frontend.h"
  21. #include "mt2266.h"
  22. #define I2C_ADDRESS 0x60
  23. #define REG_PART_REV 0
  24. #define REG_TUNE 1
  25. #define REG_BAND 6
  26. #define REG_BANDWIDTH 8
  27. #define REG_LOCK 0x12
  28. #define PART_REV 0x85
  29. struct mt2266_priv {
  30. struct mt2266_config *cfg;
  31. struct i2c_adapter *i2c;
  32. u32 frequency;
  33. u32 bandwidth;
  34. };
  35. /* Here, frequencies are expressed in kiloHertz to avoid 32 bits overflows */
  36. static int debug;
  37. module_param(debug, int, 0644);
  38. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  39. #define dprintk(args...) do { if (debug) {printk(KERN_DEBUG "MT2266: " args); printk("\n"); }} while (0)
  40. // Reads a single register
  41. static int mt2266_readreg(struct mt2266_priv *priv, u8 reg, u8 *val)
  42. {
  43. struct i2c_msg msg[2] = {
  44. { .addr = priv->cfg->i2c_address, .flags = 0, .buf = &reg, .len = 1 },
  45. { .addr = priv->cfg->i2c_address, .flags = I2C_M_RD, .buf = val, .len = 1 },
  46. };
  47. if (i2c_transfer(priv->i2c, msg, 2) != 2) {
  48. printk(KERN_WARNING "MT2266 I2C read failed\n");
  49. return -EREMOTEIO;
  50. }
  51. return 0;
  52. }
  53. // Writes a single register
  54. static int mt2266_writereg(struct mt2266_priv *priv, u8 reg, u8 val)
  55. {
  56. u8 buf[2] = { reg, val };
  57. struct i2c_msg msg = {
  58. .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = 2
  59. };
  60. if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
  61. printk(KERN_WARNING "MT2266 I2C write failed\n");
  62. return -EREMOTEIO;
  63. }
  64. return 0;
  65. }
  66. // Writes a set of consecutive registers
  67. static int mt2266_writeregs(struct mt2266_priv *priv,u8 *buf, u8 len)
  68. {
  69. struct i2c_msg msg = {
  70. .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = len
  71. };
  72. if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
  73. printk(KERN_WARNING "MT2266 I2C write failed (len=%i)\n",(int)len);
  74. return -EREMOTEIO;
  75. }
  76. return 0;
  77. }
  78. // Initialisation sequences
  79. static u8 mt2266_init1[] = {
  80. REG_TUNE,
  81. 0x00, 0x00, 0x28, 0x00, 0x52, 0x99, 0x3f };
  82. static u8 mt2266_init2[] = {
  83. 0x17, 0x6d, 0x71, 0x61, 0xc0, 0xbf, 0xff, 0xdc, 0x00, 0x0a,
  84. 0xd4, 0x03, 0x64, 0x64, 0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14, 0x01, 0x01, 0x01, 0x01,
  85. 0x01, 0x01, 0x7f, 0x5e, 0x3f, 0xff, 0xff, 0xff, 0x00, 0x77, 0x0f, 0x2d };
  86. static u8 mt2266_init_8mhz[] = {
  87. REG_BANDWIDTH,
  88. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 };
  89. static u8 mt2266_init_7mhz[] = {
  90. REG_BANDWIDTH,
  91. 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32 };
  92. static u8 mt2266_init_6mhz[] = {
  93. REG_BANDWIDTH,
  94. 0xa7, 0xa7, 0xa7, 0xa7, 0xa7, 0xa7, 0xa7, 0xa7 };
  95. #define FREF 30000 // Quartz oscillator 30 MHz
  96. static int mt2266_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
  97. {
  98. struct mt2266_priv *priv;
  99. int ret=0;
  100. u32 freq;
  101. u32 tune;
  102. u8 lnaband;
  103. u8 b[10];
  104. int i;
  105. priv = fe->tuner_priv;
  106. mt2266_writereg(priv,0x17,0x6d);
  107. mt2266_writereg(priv,0x1c,0xff);
  108. freq = params->frequency / 1000; // Hz -> kHz
  109. priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
  110. priv->frequency = freq * 1000;
  111. tune=2 * freq * (8192/16) / (FREF/16);
  112. if (freq <= 495000) lnaband = 0xEE; else
  113. if (freq <= 525000) lnaband = 0xDD; else
  114. if (freq <= 550000) lnaband = 0xCC; else
  115. if (freq <= 580000) lnaband = 0xBB; else
  116. if (freq <= 605000) lnaband = 0xAA; else
  117. if (freq <= 630000) lnaband = 0x99; else
  118. if (freq <= 655000) lnaband = 0x88; else
  119. if (freq <= 685000) lnaband = 0x77; else
  120. if (freq <= 710000) lnaband = 0x66; else
  121. if (freq <= 735000) lnaband = 0x55; else
  122. if (freq <= 765000) lnaband = 0x44; else
  123. if (freq <= 802000) lnaband = 0x33; else
  124. if (freq <= 840000) lnaband = 0x22; else lnaband = 0x11;
  125. msleep(100);
  126. mt2266_writeregs(priv,(params->u.ofdm.bandwidth==BANDWIDTH_6_MHZ)?mt2266_init_6mhz:
  127. (params->u.ofdm.bandwidth==BANDWIDTH_7_MHZ)?mt2266_init_7mhz:
  128. mt2266_init_8mhz,sizeof(mt2266_init_8mhz));
  129. b[0] = REG_TUNE;
  130. b[1] = (tune >> 8) & 0x1F;
  131. b[2] = tune & 0xFF;
  132. b[3] = tune >> 13;
  133. mt2266_writeregs(priv,b,4);
  134. dprintk("set_parms: tune=%d band=%d",(int)tune,(int)lnaband);
  135. dprintk("set_parms: [1..3]: %2x %2x %2x",(int)b[1],(int)b[2],(int)b[3]);
  136. b[0] = 0x05;
  137. b[1] = 0x62;
  138. b[2] = lnaband;
  139. mt2266_writeregs(priv,b,3);
  140. //Waits for pll lock or timeout
  141. i = 0;
  142. do {
  143. mt2266_readreg(priv,REG_LOCK,b);
  144. if ((b[0] & 0x40)==0x40)
  145. break;
  146. msleep(10);
  147. i++;
  148. } while (i<10);
  149. dprintk("Lock when i=%i",(int)i);
  150. return ret;
  151. }
  152. static void mt2266_calibrate(struct mt2266_priv *priv)
  153. {
  154. mt2266_writereg(priv,0x11,0x03);
  155. mt2266_writereg(priv,0x11,0x01);
  156. mt2266_writeregs(priv,mt2266_init1,sizeof(mt2266_init1));
  157. mt2266_writeregs(priv,mt2266_init2,sizeof(mt2266_init2));
  158. mt2266_writereg(priv,0x33,0x5e);
  159. mt2266_writereg(priv,0x10,0x10);
  160. mt2266_writereg(priv,0x10,0x00);
  161. mt2266_writeregs(priv,mt2266_init_8mhz,sizeof(mt2266_init_8mhz));
  162. msleep(25);
  163. mt2266_writereg(priv,0x17,0x6d);
  164. mt2266_writereg(priv,0x1c,0x00);
  165. msleep(75);
  166. mt2266_writereg(priv,0x17,0x6d);
  167. mt2266_writereg(priv,0x1c,0xff);
  168. }
  169. static int mt2266_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  170. {
  171. struct mt2266_priv *priv = fe->tuner_priv;
  172. *frequency = priv->frequency;
  173. return 0;
  174. }
  175. static int mt2266_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  176. {
  177. struct mt2266_priv *priv = fe->tuner_priv;
  178. *bandwidth = priv->bandwidth;
  179. return 0;
  180. }
  181. static int mt2266_init(struct dvb_frontend *fe)
  182. {
  183. struct mt2266_priv *priv = fe->tuner_priv;
  184. mt2266_writereg(priv,0x17,0x6d);
  185. mt2266_writereg(priv,0x1c,0xff);
  186. return 0;
  187. }
  188. static int mt2266_sleep(struct dvb_frontend *fe)
  189. {
  190. struct mt2266_priv *priv = fe->tuner_priv;
  191. mt2266_writereg(priv,0x17,0x6d);
  192. mt2266_writereg(priv,0x1c,0x00);
  193. return 0;
  194. }
  195. static int mt2266_release(struct dvb_frontend *fe)
  196. {
  197. kfree(fe->tuner_priv);
  198. fe->tuner_priv = NULL;
  199. return 0;
  200. }
  201. static const struct dvb_tuner_ops mt2266_tuner_ops = {
  202. .info = {
  203. .name = "Microtune MT2266",
  204. .frequency_min = 470000000,
  205. .frequency_max = 860000000,
  206. .frequency_step = 50000,
  207. },
  208. .release = mt2266_release,
  209. .init = mt2266_init,
  210. .sleep = mt2266_sleep,
  211. .set_params = mt2266_set_params,
  212. .get_frequency = mt2266_get_frequency,
  213. .get_bandwidth = mt2266_get_bandwidth
  214. };
  215. struct dvb_frontend * mt2266_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2266_config *cfg)
  216. {
  217. struct mt2266_priv *priv = NULL;
  218. u8 id = 0;
  219. priv = kzalloc(sizeof(struct mt2266_priv), GFP_KERNEL);
  220. if (priv == NULL)
  221. return NULL;
  222. priv->cfg = cfg;
  223. priv->i2c = i2c;
  224. if (mt2266_readreg(priv,0,&id) != 0) {
  225. kfree(priv);
  226. return NULL;
  227. }
  228. if (id != PART_REV) {
  229. kfree(priv);
  230. return NULL;
  231. }
  232. printk(KERN_INFO "MT2266: successfully identified\n");
  233. memcpy(&fe->ops.tuner_ops, &mt2266_tuner_ops, sizeof(struct dvb_tuner_ops));
  234. fe->tuner_priv = priv;
  235. mt2266_calibrate(priv);
  236. return fe;
  237. }
  238. EXPORT_SYMBOL(mt2266_attach);
  239. MODULE_AUTHOR("Olivier DANET");
  240. MODULE_DESCRIPTION("Microtune MT2266 silicon tuner driver");
  241. MODULE_LICENSE("GPL");