itd1000.c 11 KB

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