itd1000.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Driver for the Integrant ITD1000 "Zero-IF Tuner IC for Direct Broadcast Satellite"
  3. *
  4. * Copyright (c) 2007-8 Patrick Boettcher <pb@linuxtv.org>
  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. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
  20. */
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/delay.h>
  24. #include <linux/dvb/frontend.h>
  25. #include <linux/i2c.h>
  26. #include <linux/slab.h>
  27. #include "dvb_frontend.h"
  28. #include "itd1000.h"
  29. #include "itd1000_priv.h"
  30. static int debug;
  31. module_param(debug, int, 0644);
  32. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  33. #define deb(args...) do { \
  34. if (debug) { \
  35. printk(KERN_DEBUG "ITD1000: " args);\
  36. printk("\n"); \
  37. } \
  38. } while (0)
  39. #define warn(args...) do { \
  40. printk(KERN_WARNING "ITD1000: " args); \
  41. printk("\n"); \
  42. } while (0)
  43. #define info(args...) do { \
  44. printk(KERN_INFO "ITD1000: " args); \
  45. printk("\n"); \
  46. } while (0)
  47. /* don't write more than one byte with flexcop behind */
  48. static int itd1000_write_regs(struct itd1000_state *state, u8 reg, u8 v[], u8 len)
  49. {
  50. u8 buf[1+len];
  51. struct i2c_msg msg = {
  52. .addr = state->cfg->i2c_address, .flags = 0, .buf = buf, .len = len+1
  53. };
  54. buf[0] = reg;
  55. memcpy(&buf[1], v, len);
  56. /* deb("wr %02x: %02x", reg, v[0]); */
  57. if (i2c_transfer(state->i2c, &msg, 1) != 1) {
  58. printk(KERN_WARNING "itd1000 I2C write failed\n");
  59. return -EREMOTEIO;
  60. }
  61. return 0;
  62. }
  63. static int itd1000_read_reg(struct itd1000_state *state, u8 reg)
  64. {
  65. u8 val;
  66. struct i2c_msg msg[2] = {
  67. { .addr = state->cfg->i2c_address, .flags = 0, .buf = &reg, .len = 1 },
  68. { .addr = state->cfg->i2c_address, .flags = I2C_M_RD, .buf = &val, .len = 1 },
  69. };
  70. /* ugly flexcop workaround */
  71. itd1000_write_regs(state, (reg - 1) & 0xff, &state->shadow[(reg - 1) & 0xff], 1);
  72. if (i2c_transfer(state->i2c, msg, 2) != 2) {
  73. warn("itd1000 I2C read failed");
  74. return -EREMOTEIO;
  75. }
  76. return val;
  77. }
  78. static inline int itd1000_write_reg(struct itd1000_state *state, u8 r, u8 v)
  79. {
  80. int ret = itd1000_write_regs(state, r, &v, 1);
  81. state->shadow[r] = v;
  82. return ret;
  83. }
  84. static struct {
  85. u32 symbol_rate;
  86. u8 pgaext : 4; /* PLLFH */
  87. u8 bbgvmin : 4; /* BBGVMIN */
  88. } itd1000_lpf_pga[] = {
  89. { 0, 0x8, 0x3 },
  90. { 5200000, 0x8, 0x3 },
  91. { 12200000, 0x4, 0x3 },
  92. { 15400000, 0x2, 0x3 },
  93. { 19800000, 0x2, 0x3 },
  94. { 21500000, 0x2, 0x3 },
  95. { 24500000, 0x2, 0x3 },
  96. { 28400000, 0x2, 0x3 },
  97. { 33400000, 0x2, 0x3 },
  98. { 34400000, 0x1, 0x4 },
  99. { 34400000, 0x1, 0x4 },
  100. { 38400000, 0x1, 0x4 },
  101. { 38400000, 0x1, 0x4 },
  102. { 40400000, 0x1, 0x4 },
  103. { 45400000, 0x1, 0x4 },
  104. };
  105. static void itd1000_set_lpf_bw(struct itd1000_state *state, u32 symbol_rate)
  106. {
  107. u8 i;
  108. u8 con1 = itd1000_read_reg(state, CON1) & 0xfd;
  109. u8 pllfh = itd1000_read_reg(state, PLLFH) & 0x0f;
  110. u8 bbgvmin = itd1000_read_reg(state, BBGVMIN) & 0xf0;
  111. u8 bw = itd1000_read_reg(state, BW) & 0xf0;
  112. deb("symbol_rate = %d", symbol_rate);
  113. /* not sure what is that ? - starting to download the table */
  114. itd1000_write_reg(state, CON1, con1 | (1 << 1));
  115. for (i = 0; i < ARRAY_SIZE(itd1000_lpf_pga); i++)
  116. if (symbol_rate < itd1000_lpf_pga[i].symbol_rate) {
  117. deb("symrate: index: %d pgaext: %x, bbgvmin: %x", i, itd1000_lpf_pga[i].pgaext, itd1000_lpf_pga[i].bbgvmin);
  118. itd1000_write_reg(state, PLLFH, pllfh | (itd1000_lpf_pga[i].pgaext << 4));
  119. itd1000_write_reg(state, BBGVMIN, bbgvmin | (itd1000_lpf_pga[i].bbgvmin));
  120. itd1000_write_reg(state, BW, bw | (i & 0x0f));
  121. break;
  122. }
  123. itd1000_write_reg(state, CON1, con1 | (0 << 1));
  124. }
  125. static struct {
  126. u8 vcorg;
  127. u32 fmax_rg;
  128. } itd1000_vcorg[] = {
  129. { 1, 920000 },
  130. { 2, 971000 },
  131. { 3, 1031000 },
  132. { 4, 1091000 },
  133. { 5, 1171000 },
  134. { 6, 1281000 },
  135. { 7, 1381000 },
  136. { 8, 500000 }, /* this is intentional. */
  137. { 9, 1451000 },
  138. { 10, 1531000 },
  139. { 11, 1631000 },
  140. { 12, 1741000 },
  141. { 13, 1891000 },
  142. { 14, 2071000 },
  143. { 15, 2250000 },
  144. };
  145. static void itd1000_set_vco(struct itd1000_state *state, u32 freq_khz)
  146. {
  147. u8 i;
  148. u8 gvbb_i2c = itd1000_read_reg(state, GVBB_I2C) & 0xbf;
  149. u8 vco_chp1_i2c = itd1000_read_reg(state, VCO_CHP1_I2C) & 0x0f;
  150. u8 adcout;
  151. /* reserved bit again (reset ?) */
  152. itd1000_write_reg(state, GVBB_I2C, gvbb_i2c | (1 << 6));
  153. for (i = 0; i < ARRAY_SIZE(itd1000_vcorg); i++) {
  154. if (freq_khz < itd1000_vcorg[i].fmax_rg) {
  155. itd1000_write_reg(state, VCO_CHP1_I2C, vco_chp1_i2c | (itd1000_vcorg[i].vcorg << 4));
  156. msleep(1);
  157. adcout = itd1000_read_reg(state, PLLLOCK) & 0x0f;
  158. deb("VCO: %dkHz: %d -> ADCOUT: %d %02x", freq_khz, itd1000_vcorg[i].vcorg, adcout, vco_chp1_i2c);
  159. if (adcout > 13) {
  160. if (!(itd1000_vcorg[i].vcorg == 7 || itd1000_vcorg[i].vcorg == 15))
  161. itd1000_write_reg(state, VCO_CHP1_I2C, vco_chp1_i2c | ((itd1000_vcorg[i].vcorg + 1) << 4));
  162. } else if (adcout < 2) {
  163. if (!(itd1000_vcorg[i].vcorg == 1 || itd1000_vcorg[i].vcorg == 9))
  164. itd1000_write_reg(state, VCO_CHP1_I2C, vco_chp1_i2c | ((itd1000_vcorg[i].vcorg - 1) << 4));
  165. }
  166. break;
  167. }
  168. }
  169. }
  170. static const struct {
  171. u32 freq;
  172. u8 values[10]; /* RFTR, RFST1 - RFST9 */
  173. } itd1000_fre_values[] = {
  174. { 1075000, { 0x59, 0x1d, 0x1c, 0x17, 0x16, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
  175. { 1250000, { 0x89, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
  176. { 1450000, { 0x89, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
  177. { 1650000, { 0x69, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
  178. { 1750000, { 0x69, 0x1e, 0x17, 0x15, 0x14, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
  179. { 1850000, { 0x69, 0x1d, 0x17, 0x16, 0x14, 0x0f, 0x0e, 0x0d, 0x0b, 0x0a } },
  180. { 1900000, { 0x69, 0x1d, 0x17, 0x15, 0x14, 0x0f, 0x0e, 0x0d, 0x0b, 0x0a } },
  181. { 1950000, { 0x69, 0x1d, 0x17, 0x16, 0x14, 0x13, 0x0e, 0x0d, 0x0b, 0x0a } },
  182. { 2050000, { 0x69, 0x1e, 0x1d, 0x17, 0x16, 0x14, 0x13, 0x0e, 0x0b, 0x0a } },
  183. { 2150000, { 0x69, 0x1d, 0x1c, 0x17, 0x15, 0x14, 0x13, 0x0f, 0x0e, 0x0b } }
  184. };
  185. #define FREF 16
  186. static void itd1000_set_lo(struct itd1000_state *state, u32 freq_khz)
  187. {
  188. int i, j;
  189. u32 plln, pllf;
  190. u64 tmp;
  191. plln = (freq_khz * 1000) / 2 / FREF;
  192. /* Compute the factional part times 1000 */
  193. tmp = plln % 1000000;
  194. plln /= 1000000;
  195. tmp *= 1048576;
  196. do_div(tmp, 1000000);
  197. pllf = (u32) tmp;
  198. state->frequency = ((plln * 1000) + (pllf * 1000)/1048576) * 2*FREF;
  199. deb("frequency: %dkHz (wanted) %dkHz (set), PLLF = %d, PLLN = %d", freq_khz, state->frequency, pllf, plln);
  200. itd1000_write_reg(state, PLLNH, 0x80); /* PLLNH */;
  201. itd1000_write_reg(state, PLLNL, plln & 0xff);
  202. itd1000_write_reg(state, PLLFH, (itd1000_read_reg(state, PLLFH) & 0xf0) | ((pllf >> 16) & 0x0f));
  203. itd1000_write_reg(state, PLLFM, (pllf >> 8) & 0xff);
  204. itd1000_write_reg(state, PLLFL, (pllf >> 0) & 0xff);
  205. for (i = 0; i < ARRAY_SIZE(itd1000_fre_values); i++) {
  206. if (freq_khz <= itd1000_fre_values[i].freq) {
  207. deb("fre_values: %d", i);
  208. itd1000_write_reg(state, RFTR, itd1000_fre_values[i].values[0]);
  209. for (j = 0; j < 9; j++)
  210. itd1000_write_reg(state, RFST1+j, itd1000_fre_values[i].values[j+1]);
  211. break;
  212. }
  213. }
  214. itd1000_set_vco(state, freq_khz);
  215. }
  216. static int itd1000_set_parameters(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
  217. {
  218. struct itd1000_state *state = fe->tuner_priv;
  219. u8 pllcon1;
  220. itd1000_set_lo(state, p->frequency);
  221. itd1000_set_lpf_bw(state, p->u.qpsk.symbol_rate);
  222. pllcon1 = itd1000_read_reg(state, PLLCON1) & 0x7f;
  223. itd1000_write_reg(state, PLLCON1, pllcon1 | (1 << 7));
  224. itd1000_write_reg(state, PLLCON1, pllcon1);
  225. return 0;
  226. }
  227. static int itd1000_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  228. {
  229. struct itd1000_state *state = fe->tuner_priv;
  230. *frequency = state->frequency;
  231. return 0;
  232. }
  233. static int itd1000_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  234. {
  235. return 0;
  236. }
  237. static u8 itd1000_init_tab[][2] = {
  238. { PLLCON1, 0x65 }, /* Register does not change */
  239. { PLLNH, 0x80 }, /* Bits [7:6] do not change */
  240. { RESERVED_0X6D, 0x3b },
  241. { VCO_CHP2_I2C, 0x12 },
  242. { 0x72, 0xf9 }, /* No such regsister defined */
  243. { RESERVED_0X73, 0xff },
  244. { RESERVED_0X74, 0xb2 },
  245. { RESERVED_0X75, 0xc7 },
  246. { EXTGVBBRF, 0xf0 },
  247. { DIVAGCCK, 0x80 },
  248. { BBTR, 0xa0 },
  249. { RESERVED_0X7E, 0x4f },
  250. { 0x82, 0x88 }, /* No such regsister defined */
  251. { 0x83, 0x80 }, /* No such regsister defined */
  252. { 0x84, 0x80 }, /* No such regsister defined */
  253. { RESERVED_0X85, 0x74 },
  254. { RESERVED_0X86, 0xff },
  255. { RESERVED_0X88, 0x02 },
  256. { RESERVED_0X89, 0x16 },
  257. { RFST0, 0x1f },
  258. { RESERVED_0X94, 0x66 },
  259. { RESERVED_0X95, 0x66 },
  260. { RESERVED_0X96, 0x77 },
  261. { RESERVED_0X97, 0x99 },
  262. { RESERVED_0X98, 0xff },
  263. { RESERVED_0X99, 0xfc },
  264. { RESERVED_0X9A, 0xba },
  265. { RESERVED_0X9B, 0xaa },
  266. };
  267. static u8 itd1000_reinit_tab[][2] = {
  268. { VCO_CHP1_I2C, 0x8a },
  269. { BW, 0x87 },
  270. { GVBB_I2C, 0x03 },
  271. { BBGVMIN, 0x03 },
  272. { CON1, 0x2e },
  273. };
  274. static int itd1000_init(struct dvb_frontend *fe)
  275. {
  276. struct itd1000_state *state = fe->tuner_priv;
  277. int i;
  278. for (i = 0; i < ARRAY_SIZE(itd1000_init_tab); i++)
  279. itd1000_write_reg(state, itd1000_init_tab[i][0], itd1000_init_tab[i][1]);
  280. for (i = 0; i < ARRAY_SIZE(itd1000_reinit_tab); i++)
  281. itd1000_write_reg(state, itd1000_reinit_tab[i][0], itd1000_reinit_tab[i][1]);
  282. return 0;
  283. }
  284. static int itd1000_sleep(struct dvb_frontend *fe)
  285. {
  286. return 0;
  287. }
  288. static int itd1000_release(struct dvb_frontend *fe)
  289. {
  290. kfree(fe->tuner_priv);
  291. fe->tuner_priv = NULL;
  292. return 0;
  293. }
  294. static const struct dvb_tuner_ops itd1000_tuner_ops = {
  295. .info = {
  296. .name = "Integrant ITD1000",
  297. .frequency_min = 950000,
  298. .frequency_max = 2150000,
  299. .frequency_step = 125, /* kHz for QPSK frontends */
  300. },
  301. .release = itd1000_release,
  302. .init = itd1000_init,
  303. .sleep = itd1000_sleep,
  304. .set_params = itd1000_set_parameters,
  305. .get_frequency = itd1000_get_frequency,
  306. .get_bandwidth = itd1000_get_bandwidth
  307. };
  308. struct dvb_frontend *itd1000_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct itd1000_config *cfg)
  309. {
  310. struct itd1000_state *state = NULL;
  311. u8 i = 0;
  312. state = kzalloc(sizeof(struct itd1000_state), GFP_KERNEL);
  313. if (state == NULL)
  314. return NULL;
  315. state->cfg = cfg;
  316. state->i2c = i2c;
  317. i = itd1000_read_reg(state, 0);
  318. if (i != 0) {
  319. kfree(state);
  320. return NULL;
  321. }
  322. info("successfully identified (ID: %d)", i);
  323. memset(state->shadow, 0xff, sizeof(state->shadow));
  324. for (i = 0x65; i < 0x9c; i++)
  325. state->shadow[i] = itd1000_read_reg(state, i);
  326. memcpy(&fe->ops.tuner_ops, &itd1000_tuner_ops, sizeof(struct dvb_tuner_ops));
  327. fe->tuner_priv = state;
  328. return fe;
  329. }
  330. EXPORT_SYMBOL(itd1000_attach);
  331. MODULE_AUTHOR("Patrick Boettcher <pb@linuxtv.org>");
  332. MODULE_DESCRIPTION("Integrant ITD1000 driver");
  333. MODULE_LICENSE("GPL");