itd1000.c 11 KB

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