itd1000.c 11 KB

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