tuner_e4k.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /*
  2. * Elonics E4000 tuner driver
  3. *
  4. * (C) 2011-2012 by Harald Welte <laforge@gnumonks.org>
  5. * (C) 2012 by Sylvain Munaut <tnt@246tNt.com>
  6. * (C) 2012 by Hoernchen <la@tfc-server.de>
  7. *
  8. * All Rights Reserved
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include <limits.h>
  24. #include <stdint.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include <reg_field.h>
  29. #include <tuner_e4k.h>
  30. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  31. /* If this is defined, the limits are somewhat relaxed compared to what the
  32. * vendor claims is possible */
  33. #define OUT_OF_SPEC
  34. #define MHZ(x) ((x)*1000*1000)
  35. #define KHZ(x) ((x)*1000)
  36. uint32_t unsigned_delta(uint32_t a, uint32_t b)
  37. {
  38. if (a > b)
  39. return a - b;
  40. else
  41. return b - a;
  42. }
  43. /* look-up table bit-width -> mask */
  44. static const uint8_t width2mask[] = {
  45. 0, 1, 3, 7, 0xf, 0x1f, 0x3f, 0x7f, 0xff
  46. };
  47. /***********************************************************************
  48. * Register Access */
  49. #if 0
  50. /*! \brief Write a register of the tuner chip
  51. * \param[in] e4k reference to the tuner
  52. * \param[in] reg number of the register
  53. * \param[in] val value to be written
  54. * \returns 0 on success, negative in case of error
  55. */
  56. int e4k_reg_write(struct e4k_state *e4k, uint8_t reg, uint8_t val)
  57. {
  58. /* FIXME */
  59. return 0;
  60. }
  61. /*! \brief Read a register of the tuner chip
  62. * \param[in] e4k reference to the tuner
  63. * \param[in] reg number of the register
  64. * \returns positive 8bit register contents on success, negative in case of error
  65. */
  66. int e4k_reg_read(struct e4k_state *e4k, uint8_t reg)
  67. {
  68. /* FIXME */
  69. return 0;
  70. }
  71. #endif
  72. /*! \brief Set or clear some (masked) bits inside a register
  73. * \param[in] e4k reference to the tuner
  74. * \param[in] reg number of the register
  75. * \param[in] mask bit-mask of the value
  76. * \param[in] val data value to be written to register
  77. * \returns 0 on success, negative in case of error
  78. */
  79. static int e4k_reg_set_mask(struct e4k_state *e4k, uint8_t reg,
  80. uint8_t mask, uint8_t val)
  81. {
  82. uint8_t tmp = e4k_reg_read(e4k, reg);
  83. if ((tmp & mask) == val)
  84. return 0;
  85. return e4k_reg_write(e4k, reg, (tmp & ~mask) | (val & mask));
  86. }
  87. /*! \brief Write a given field inside a register
  88. * \param[in] e4k reference to the tuner
  89. * \param[in] field structure describing the field
  90. * \param[in] val value to be written
  91. * \returns 0 on success, negative in case of error
  92. */
  93. static int e4k_field_write(struct e4k_state *e4k, const struct reg_field *field, uint8_t val)
  94. {
  95. int rc;
  96. uint8_t mask;
  97. rc = e4k_reg_read(e4k, field->reg);
  98. if (rc < 0)
  99. return rc;
  100. mask = width2mask[field->width] << field->shift;
  101. return e4k_reg_set_mask(e4k, field->reg, mask, val << field->shift);
  102. }
  103. /*! \brief Read a given field inside a register
  104. * \param[in] e4k reference to the tuner
  105. * \param[in] field structure describing the field
  106. * \returns positive value of the field, negative in case of error
  107. */
  108. static int e4k_field_read(struct e4k_state *e4k, const struct reg_field *field)
  109. {
  110. int rc;
  111. rc = e4k_reg_read(e4k, field->reg);
  112. if (rc < 0)
  113. return rc;
  114. rc = (rc >> field->shift) & width2mask[field->width];
  115. return rc;
  116. }
  117. /***********************************************************************
  118. * Filter Control */
  119. static const uint32_t rf_filt_center_uhf[] = {
  120. MHZ(360), MHZ(380), MHZ(405), MHZ(425),
  121. MHZ(450), MHZ(475), MHZ(505), MHZ(540),
  122. MHZ(575), MHZ(615), MHZ(670), MHZ(720),
  123. MHZ(760), MHZ(840), MHZ(890), MHZ(970)
  124. };
  125. static const uint32_t rf_filt_center_l[] = {
  126. MHZ(1300), MHZ(1320), MHZ(1360), MHZ(1410),
  127. MHZ(1445), MHZ(1460), MHZ(1490), MHZ(1530),
  128. MHZ(1560), MHZ(1590), MHZ(1640), MHZ(1660),
  129. MHZ(1680), MHZ(1700), MHZ(1720), MHZ(1750)
  130. };
  131. static int closest_arr_idx(const uint32_t *arr, unsigned int arr_size, uint32_t freq)
  132. {
  133. unsigned int i, bi = 0;
  134. uint32_t best_delta = 0xffffffff;
  135. /* iterate over the array containing a list of the center
  136. * frequencies, selecting the closest one */
  137. for (i = 0; i < arr_size; i++) {
  138. uint32_t delta = unsigned_delta(freq, arr[i]);
  139. if (delta < best_delta) {
  140. best_delta = delta;
  141. bi = i;
  142. }
  143. }
  144. return bi;
  145. }
  146. /* return 4-bit index as to which RF filter to select */
  147. static int choose_rf_filter(enum e4k_band band, uint32_t freq)
  148. {
  149. int rc;
  150. switch (band) {
  151. case E4K_BAND_VHF2:
  152. case E4K_BAND_VHF3:
  153. rc = 0;
  154. break;
  155. case E4K_BAND_UHF:
  156. rc = closest_arr_idx(rf_filt_center_uhf,
  157. ARRAY_SIZE(rf_filt_center_uhf),
  158. freq);
  159. break;
  160. case E4K_BAND_L:
  161. rc = closest_arr_idx(rf_filt_center_l,
  162. ARRAY_SIZE(rf_filt_center_l),
  163. freq);
  164. break;
  165. default:
  166. rc = -EINVAL;
  167. break;
  168. }
  169. return rc;
  170. }
  171. /* \brief Automatically select apropriate RF filter based on e4k state */
  172. int e4k_rf_filter_set(struct e4k_state *e4k)
  173. {
  174. int rc;
  175. rc = choose_rf_filter(e4k->band, e4k->vco.flo);
  176. if (rc < 0)
  177. return rc;
  178. return e4k_reg_set_mask(e4k, E4K_REG_FILT1, 0xF, rc);
  179. }
  180. /* Mixer Filter */
  181. static const uint32_t mix_filter_bw[] = {
  182. KHZ(27000), KHZ(27000), KHZ(27000), KHZ(27000),
  183. KHZ(27000), KHZ(27000), KHZ(27000), KHZ(27000),
  184. KHZ(4600), KHZ(4200), KHZ(3800), KHZ(3400),
  185. KHZ(3300), KHZ(2700), KHZ(2300), KHZ(1900)
  186. };
  187. /* IF RC Filter */
  188. static const uint32_t ifrc_filter_bw[] = {
  189. KHZ(21400), KHZ(21000), KHZ(17600), KHZ(14700),
  190. KHZ(12400), KHZ(10600), KHZ(9000), KHZ(7700),
  191. KHZ(6400), KHZ(5300), KHZ(4400), KHZ(3400),
  192. KHZ(2600), KHZ(1800), KHZ(1200), KHZ(1000)
  193. };
  194. /* IF Channel Filter */
  195. static const uint32_t ifch_filter_bw[] = {
  196. KHZ(5500), KHZ(5300), KHZ(5000), KHZ(4800),
  197. KHZ(4600), KHZ(4400), KHZ(4300), KHZ(4100),
  198. KHZ(3900), KHZ(3800), KHZ(3700), KHZ(3600),
  199. KHZ(3400), KHZ(3300), KHZ(3200), KHZ(3100),
  200. KHZ(3000), KHZ(2950), KHZ(2900), KHZ(2800),
  201. KHZ(2750), KHZ(2700), KHZ(2600), KHZ(2550),
  202. KHZ(2500), KHZ(2450), KHZ(2400), KHZ(2300),
  203. KHZ(2280), KHZ(2240), KHZ(2200), KHZ(2150)
  204. };
  205. static const uint32_t *if_filter_bw[] = {
  206. mix_filter_bw,
  207. ifch_filter_bw,
  208. ifrc_filter_bw,
  209. };
  210. static const uint32_t if_filter_bw_len[] = {
  211. ARRAY_SIZE(mix_filter_bw),
  212. ARRAY_SIZE(ifch_filter_bw),
  213. ARRAY_SIZE(ifrc_filter_bw),
  214. };
  215. static const struct reg_field if_filter_fields[] = {
  216. {
  217. E4K_REG_FILT2, 4, 4,
  218. },
  219. {
  220. E4K_REG_FILT3, 0, 5,
  221. },
  222. {
  223. E4K_REG_FILT2, 0, 4,
  224. }
  225. };
  226. static int find_if_bw(enum e4k_if_filter filter, uint32_t bw)
  227. {
  228. if (filter >= ARRAY_SIZE(if_filter_bw))
  229. return -EINVAL;
  230. return closest_arr_idx(if_filter_bw[filter],
  231. if_filter_bw_len[filter], bw);
  232. }
  233. /*! \brief Set the filter band-width of any of the IF filters
  234. * \param[in] e4k reference to the tuner chip
  235. * \param[in] filter filter to be configured
  236. * \param[in] bandwidth bandwidth to be configured
  237. * \returns positive actual filter band-width, negative in case of error
  238. */
  239. int e4k_if_filter_bw_set(struct e4k_state *e4k, enum e4k_if_filter filter,
  240. uint32_t bandwidth)
  241. {
  242. int bw_idx;
  243. const struct reg_field *field;
  244. if (filter >= ARRAY_SIZE(if_filter_bw))
  245. return -EINVAL;
  246. bw_idx = find_if_bw(filter, bandwidth);
  247. field = &if_filter_fields[filter];
  248. return e4k_field_write(e4k, field, bw_idx);
  249. }
  250. /*! \brief Enables / Disables the channel filter
  251. * \param[in] e4k reference to the tuner chip
  252. * \param[in] on 1=filter enabled, 0=filter disabled
  253. * \returns 0 success, negative errors
  254. */
  255. int e4k_if_filter_chan_enable(struct e4k_state *e4k, int on)
  256. {
  257. return e4k_reg_set_mask(e4k, E4K_REG_FILT3, E4K_FILT3_DISABLE,
  258. on ? 0 : E4K_FILT3_DISABLE);
  259. }
  260. int e4k_if_filter_bw_get(struct e4k_state *e4k, enum e4k_if_filter filter)
  261. {
  262. const uint32_t *arr;
  263. int rc;
  264. const struct reg_field *field;
  265. if (filter >= ARRAY_SIZE(if_filter_bw))
  266. return -EINVAL;
  267. field = &if_filter_fields[filter];
  268. rc = e4k_field_read(e4k, field);
  269. if (rc < 0)
  270. return rc;
  271. arr = if_filter_bw[filter];
  272. return arr[rc];
  273. }
  274. /***********************************************************************
  275. * Frequency Control */
  276. #define E4K_FVCO_MIN_KHZ 2600000 /* 2.6 GHz */
  277. #define E4K_FVCO_MAX_KHZ 3900000 /* 3.9 GHz */
  278. #define E4K_PLL_Y 65536
  279. #ifdef OUT_OF_SPEC
  280. #define E4K_FLO_MIN_MHZ 50
  281. #define E4K_FLO_MAX_MHZ 2200UL
  282. #else
  283. #define E4K_FLO_MIN_MHZ 64
  284. #define E4K_FLO_MAX_MHZ 1700
  285. #endif
  286. struct pll_settings {
  287. uint32_t freq;
  288. uint8_t reg_synth7;
  289. uint8_t mult;
  290. };
  291. static const struct pll_settings pll_vars[] = {
  292. {KHZ(72400), (1 << 3) | 7, 48},
  293. {KHZ(81200), (1 << 3) | 6, 40},
  294. {KHZ(108300), (1 << 3) | 5, 32},
  295. {KHZ(162500), (1 << 3) | 4, 24},
  296. {KHZ(216600), (1 << 3) | 3, 16},
  297. {KHZ(325000), (1 << 3) | 2, 12},
  298. {KHZ(350000), (1 << 3) | 1, 8},
  299. {KHZ(432000), (0 << 3) | 3, 8},
  300. {KHZ(667000), (0 << 3) | 2, 6},
  301. {KHZ(1200000), (0 << 3) | 1, 4}
  302. };
  303. static int is_fvco_valid(uint32_t fvco_z)
  304. {
  305. /* check if the resulting fosc is valid */
  306. if (fvco_z/1000 < E4K_FVCO_MIN_KHZ ||
  307. fvco_z/1000 > E4K_FVCO_MAX_KHZ) {
  308. fprintf(stderr, "[E4K] Fvco %u invalid\n", fvco_z);
  309. return 0;
  310. }
  311. return 1;
  312. }
  313. static int is_fosc_valid(uint32_t fosc)
  314. {
  315. if (fosc < MHZ(16) || fosc > MHZ(30)) {
  316. fprintf(stderr, "[E4K] Fosc %u invalid\n", fosc);
  317. return 0;
  318. }
  319. return 1;
  320. }
  321. static int is_z_valid(uint32_t z)
  322. {
  323. if (z > 255) {
  324. fprintf(stderr, "[E4K] Z %u invalid\n", z);
  325. return 0;
  326. }
  327. return 1;
  328. }
  329. /*! \brief Determine if 3-phase mixing shall be used or not */
  330. static int use_3ph_mixing(uint32_t flo)
  331. {
  332. /* this is a magic number somewhre between VHF and UHF */
  333. if (flo < MHZ(350))
  334. return 1;
  335. return 0;
  336. }
  337. /* \brief compute Fvco based on Fosc, Z and X
  338. * \returns positive value (Fvco in Hz), 0 in case of error */
  339. static uint64_t compute_fvco(uint32_t f_osc, uint8_t z, uint16_t x)
  340. {
  341. uint64_t fvco_z, fvco_x, fvco;
  342. /* We use the following transformation in order to
  343. * handle the fractional part with integer arithmetic:
  344. * Fvco = Fosc * (Z + X/Y) <=> Fvco = Fosc * Z + (Fosc * X)/Y
  345. * This avoids X/Y = 0. However, then we would overflow a 32bit
  346. * integer, as we cannot hold e.g. 26 MHz * 65536 either.
  347. */
  348. fvco_z = (uint64_t)f_osc * z;
  349. #if 0
  350. if (!is_fvco_valid(fvco_z))
  351. return 0;
  352. #endif
  353. fvco_x = ((uint64_t)f_osc * x) / E4K_PLL_Y;
  354. fvco = fvco_z + fvco_x;
  355. return fvco;
  356. }
  357. static uint32_t compute_flo(uint32_t f_osc, uint8_t z, uint16_t x, uint8_t r)
  358. {
  359. uint64_t fvco = compute_fvco(f_osc, z, x);
  360. if (fvco == 0)
  361. return -EINVAL;
  362. return fvco / r;
  363. }
  364. static int e4k_band_set(struct e4k_state *e4k, enum e4k_band band)
  365. {
  366. int rc;
  367. switch (band) {
  368. case E4K_BAND_VHF2:
  369. case E4K_BAND_VHF3:
  370. case E4K_BAND_UHF:
  371. e4k_reg_write(e4k, E4K_REG_BIAS, 3);
  372. break;
  373. case E4K_BAND_L:
  374. e4k_reg_write(e4k, E4K_REG_BIAS, 0);
  375. break;
  376. }
  377. /* workaround: if we don't reset this register before writing to it,
  378. * we get a gap between 325-350 MHz */
  379. rc = e4k_reg_set_mask(e4k, E4K_REG_SYNTH1, 0x06, 0);
  380. rc = e4k_reg_set_mask(e4k, E4K_REG_SYNTH1, 0x06, band << 1);
  381. if (rc >= 0)
  382. e4k->band = band;
  383. return rc;
  384. }
  385. /*! \brief Compute PLL parameters for givent target frequency
  386. * \param[out] oscp Oscillator parameters, if computation successful
  387. * \param[in] fosc Clock input frequency applied to the chip (Hz)
  388. * \param[in] intended_flo target tuning frequency (Hz)
  389. * \returns actual PLL frequency, as close as possible to intended_flo,
  390. * 0 in case of error
  391. */
  392. uint32_t e4k_compute_pll_params(struct e4k_pll_params *oscp, uint32_t fosc, uint32_t intended_flo)
  393. {
  394. uint32_t i;
  395. uint8_t r = 2;
  396. uint64_t intended_fvco, remainder;
  397. uint64_t z = 0;
  398. uint32_t x;
  399. int flo;
  400. int three_phase_mixing = 0;
  401. oscp->r_idx = 0;
  402. if (!is_fosc_valid(fosc))
  403. return 0;
  404. for(i = 0; i < ARRAY_SIZE(pll_vars); ++i) {
  405. if(intended_flo < pll_vars[i].freq) {
  406. three_phase_mixing = (pll_vars[i].reg_synth7 & 0x08) ? 1 : 0;
  407. oscp->r_idx = pll_vars[i].reg_synth7;
  408. r = pll_vars[i].mult;
  409. break;
  410. }
  411. }
  412. //fprintf(stderr, "[E4K] Fint=%u, R=%u\n", intended_flo, r);
  413. /* flo(max) = 1700MHz, R(max) = 48, we need 64bit! */
  414. intended_fvco = (uint64_t)intended_flo * r;
  415. /* compute integral component of multiplier */
  416. z = intended_fvco / fosc;
  417. /* compute fractional part. this will not overflow,
  418. * as fosc(max) = 30MHz and z(max) = 255 */
  419. remainder = intended_fvco - (fosc * z);
  420. /* remainder(max) = 30MHz, E4K_PLL_Y = 65536 -> 64bit! */
  421. x = (remainder * E4K_PLL_Y) / fosc;
  422. /* x(max) as result of this computation is 65536 */
  423. flo = compute_flo(fosc, z, x, r);
  424. oscp->fosc = fosc;
  425. oscp->flo = flo;
  426. oscp->intended_flo = intended_flo;
  427. oscp->r = r;
  428. // oscp->r_idx = pll_vars[i].reg_synth7 & 0x0;
  429. oscp->threephase = three_phase_mixing;
  430. oscp->x = x;
  431. oscp->z = z;
  432. return flo;
  433. }
  434. int e4k_tune_params(struct e4k_state *e4k, struct e4k_pll_params *p)
  435. {
  436. uint8_t val;
  437. /* program R + 3phase/2phase */
  438. e4k_reg_write(e4k, E4K_REG_SYNTH7, p->r_idx);
  439. /* program Z */
  440. e4k_reg_write(e4k, E4K_REG_SYNTH3, p->z);
  441. /* program X */
  442. e4k_reg_write(e4k, E4K_REG_SYNTH4, p->x & 0xff);
  443. e4k_reg_write(e4k, E4K_REG_SYNTH5, p->x >> 8);
  444. /* we're in auto calibration mode, so there's no need to trigger it */
  445. memcpy(&e4k->vco, p, sizeof(e4k->vco));
  446. /* set the band */
  447. if (e4k->vco.flo < MHZ(140))
  448. e4k_band_set(e4k, E4K_BAND_VHF2);
  449. else if (e4k->vco.flo < MHZ(350))
  450. e4k_band_set(e4k, E4K_BAND_VHF3);
  451. else if (e4k->vco.flo < MHZ(1135))
  452. e4k_band_set(e4k, E4K_BAND_UHF);
  453. else
  454. e4k_band_set(e4k, E4K_BAND_L);
  455. /* select and set proper RF filter */
  456. e4k_rf_filter_set(e4k);
  457. return e4k->vco.flo;
  458. }
  459. /*! \brief High-level tuning API, just specify frquency
  460. *
  461. * This function will compute matching PLL parameters, program them into the
  462. * hardware and set the band as well as RF filter.
  463. *
  464. * \param[in] e4k reference to tuner
  465. * \param[in] freq frequency in Hz
  466. * \returns actual tuned frequency, negative in case of error
  467. */
  468. int e4k_tune_freq(struct e4k_state *e4k, uint32_t freq)
  469. {
  470. uint32_t rc;
  471. struct e4k_pll_params p;
  472. /* determine PLL parameters */
  473. rc = e4k_compute_pll_params(&p, e4k->vco.fosc, freq);
  474. if (!rc)
  475. return -EINVAL;
  476. /* actually tune to those parameters */
  477. rc = e4k_tune_params(e4k, &p);
  478. /* check PLL lock */
  479. rc = e4k_reg_read(e4k, E4K_REG_SYNTH1);
  480. if (!(rc & 0x01)) {
  481. fprintf(stderr, "[E4K] PLL not locked for %u Hz!\n", freq);
  482. return -1;
  483. }
  484. return 0;
  485. }
  486. /***********************************************************************
  487. * Gain Control */
  488. static const int8_t if_stage1_gain[] = {
  489. -3, 6
  490. };
  491. static const int8_t if_stage23_gain[] = {
  492. 0, 3, 6, 9
  493. };
  494. static const int8_t if_stage4_gain[] = {
  495. 0, 1, 2, 2
  496. };
  497. static const int8_t if_stage56_gain[] = {
  498. 3, 6, 9, 12, 15, 15, 15, 15
  499. };
  500. static const int8_t *if_stage_gain[] = {
  501. 0,
  502. if_stage1_gain,
  503. if_stage23_gain,
  504. if_stage23_gain,
  505. if_stage4_gain,
  506. if_stage56_gain,
  507. if_stage56_gain
  508. };
  509. static const uint8_t if_stage_gain_len[] = {
  510. 0,
  511. ARRAY_SIZE(if_stage1_gain),
  512. ARRAY_SIZE(if_stage23_gain),
  513. ARRAY_SIZE(if_stage23_gain),
  514. ARRAY_SIZE(if_stage4_gain),
  515. ARRAY_SIZE(if_stage56_gain),
  516. ARRAY_SIZE(if_stage56_gain)
  517. };
  518. static const struct reg_field if_stage_gain_regs[] = {
  519. { 0, 0, 0 },
  520. { E4K_REG_GAIN3, 0, 1 },
  521. { E4K_REG_GAIN3, 1, 2 },
  522. { E4K_REG_GAIN3, 3, 2 },
  523. { E4K_REG_GAIN3, 5, 2 },
  524. { E4K_REG_GAIN4, 0, 3 },
  525. { E4K_REG_GAIN4, 3, 3 }
  526. };
  527. static const int32_t lnagain[] = {
  528. -50, 0,
  529. -25, 1,
  530. 0, 4,
  531. 25, 5,
  532. 50, 6,
  533. 75, 7,
  534. 100, 8,
  535. 125, 9,
  536. 150, 10,
  537. 175, 11,
  538. 200, 12,
  539. 250, 13,
  540. 300, 14,
  541. };
  542. static const int32_t enhgain[] = {
  543. 10, 30, 50, 70
  544. };
  545. int e4k_set_lna_gain(struct e4k_state *e4k, int32_t gain)
  546. {
  547. uint32_t i;
  548. for(i = 0; i < ARRAY_SIZE(lnagain)/2; ++i) {
  549. if(lnagain[i*2] == gain) {
  550. e4k_reg_set_mask(e4k, E4K_REG_GAIN1, 0xf, lnagain[i*2+1]);
  551. return gain;
  552. }
  553. }
  554. return -EINVAL;
  555. }
  556. int e4k_set_enh_gain(struct e4k_state *e4k, int32_t gain)
  557. {
  558. uint32_t i;
  559. for(i = 0; i < ARRAY_SIZE(enhgain); ++i) {
  560. if(enhgain[i] == gain) {
  561. e4k_reg_set_mask(e4k, E4K_REG_AGC11, 0x7, E4K_AGC11_LNA_GAIN_ENH | (i << 1));
  562. return gain;
  563. }
  564. }
  565. e4k_reg_set_mask(e4k, E4K_REG_AGC11, 0x7, 0);
  566. /* special case: 0 = off*/
  567. if(0 == gain)
  568. return 0;
  569. else
  570. return -EINVAL;
  571. }
  572. int e4k_enable_manual_gain(struct e4k_state *e4k, uint8_t manual)
  573. {
  574. if (manual) {
  575. /* Set LNA mode to manual */
  576. e4k_reg_set_mask(e4k, E4K_REG_AGC1, E4K_AGC1_MOD_MASK, E4K_AGC_MOD_SERIAL);
  577. /* Set Mixer Gain Control to manual */
  578. e4k_reg_set_mask(e4k, E4K_REG_AGC7, E4K_AGC7_MIX_GAIN_AUTO, 0);
  579. } else {
  580. /* Set LNA mode to auto */
  581. e4k_reg_set_mask(e4k, E4K_REG_AGC1, E4K_AGC1_MOD_MASK, E4K_AGC_MOD_IF_SERIAL_LNA_AUTON);
  582. /* Set Mixer Gain Control to auto */
  583. e4k_reg_set_mask(e4k, E4K_REG_AGC7, E4K_AGC7_MIX_GAIN_AUTO, 1);
  584. e4k_reg_set_mask(e4k, E4K_REG_AGC11, 0x7, 0);
  585. }
  586. return 0;
  587. }
  588. static int find_stage_gain(uint8_t stage, int8_t val)
  589. {
  590. const int8_t *arr;
  591. int i;
  592. if (stage >= ARRAY_SIZE(if_stage_gain))
  593. return -EINVAL;
  594. arr = if_stage_gain[stage];
  595. for (i = 0; i < if_stage_gain_len[stage]; i++) {
  596. if (arr[i] == val)
  597. return i;
  598. }
  599. return -EINVAL;
  600. }
  601. /*! \brief Set the gain of one of the IF gain stages
  602. * \param [e4k] handle to the tuner chip
  603. * \param [stage] number of the stage (1..6)
  604. * \param [value] gain value in dB
  605. * \returns 0 on success, negative in case of error
  606. */
  607. int e4k_if_gain_set(struct e4k_state *e4k, uint8_t stage, int8_t value)
  608. {
  609. int rc;
  610. uint8_t mask;
  611. const struct reg_field *field;
  612. rc = find_stage_gain(stage, value);
  613. if (rc < 0)
  614. return rc;
  615. /* compute the bit-mask for the given gain field */
  616. field = &if_stage_gain_regs[stage];
  617. mask = width2mask[field->width] << field->shift;
  618. return e4k_reg_set_mask(e4k, field->reg, mask, rc << field->shift);
  619. }
  620. int e4k_mixer_gain_set(struct e4k_state *e4k, int8_t value)
  621. {
  622. uint8_t bit;
  623. switch (value) {
  624. case 4:
  625. bit = 0;
  626. break;
  627. case 12:
  628. bit = 1;
  629. break;
  630. default:
  631. return -EINVAL;
  632. }
  633. return e4k_reg_set_mask(e4k, E4K_REG_GAIN2, 1, bit);
  634. }
  635. int e4k_commonmode_set(struct e4k_state *e4k, int8_t value)
  636. {
  637. if(value < 0)
  638. return -EINVAL;
  639. else if(value > 7)
  640. return -EINVAL;
  641. return e4k_reg_set_mask(e4k, E4K_REG_DC7, 7, value);
  642. }
  643. /***********************************************************************
  644. * DC Offset */
  645. int e4k_manual_dc_offset(struct e4k_state *e4k, int8_t iofs, int8_t irange, int8_t qofs, int8_t qrange)
  646. {
  647. int res;
  648. if((iofs < 0x00) || (iofs > 0x3f))
  649. return -EINVAL;
  650. if((irange < 0x00) || (irange > 0x03))
  651. return -EINVAL;
  652. if((qofs < 0x00) || (qofs > 0x3f))
  653. return -EINVAL;
  654. if((qrange < 0x00) || (qrange > 0x03))
  655. return -EINVAL;
  656. res = e4k_reg_set_mask(e4k, E4K_REG_DC2, 0x3f, iofs);
  657. if(res < 0)
  658. return res;
  659. res = e4k_reg_set_mask(e4k, E4K_REG_DC3, 0x3f, qofs);
  660. if(res < 0)
  661. return res;
  662. res = e4k_reg_set_mask(e4k, E4K_REG_DC4, 0x33, (qrange << 4) | irange);
  663. return res;
  664. }
  665. /*! \brief Perform a DC offset calibration right now
  666. * \param [e4k] handle to the tuner chip
  667. */
  668. int e4k_dc_offset_calibrate(struct e4k_state *e4k)
  669. {
  670. /* make sure the DC range detector is enabled */
  671. e4k_reg_set_mask(e4k, E4K_REG_DC5, E4K_DC5_RANGE_DET_EN, E4K_DC5_RANGE_DET_EN);
  672. return e4k_reg_write(e4k, E4K_REG_DC1, 0x01);
  673. }
  674. static const int8_t if_gains_max[] = {
  675. 0, 6, 9, 9, 2, 15, 15
  676. };
  677. struct gain_comb {
  678. int8_t mixer_gain;
  679. int8_t if1_gain;
  680. uint8_t reg;
  681. };
  682. static const struct gain_comb dc_gain_comb[] = {
  683. { 4, -3, 0x50 },
  684. { 4, 6, 0x51 },
  685. { 12, -3, 0x52 },
  686. { 12, 6, 0x53 },
  687. };
  688. #define TO_LUT(offset, range) (offset | (range << 6))
  689. int e4k_dc_offset_gen_table(struct e4k_state *e4k)
  690. {
  691. uint32_t i;
  692. /* FIXME: read ont current gain values and write them back
  693. * before returning to the caller */
  694. /* disable auto mixer gain */
  695. e4k_reg_set_mask(e4k, E4K_REG_AGC7, E4K_AGC7_MIX_GAIN_AUTO, 0);
  696. /* set LNA/IF gain to full manual */
  697. e4k_reg_set_mask(e4k, E4K_REG_AGC1, E4K_AGC1_MOD_MASK,
  698. E4K_AGC_MOD_SERIAL);
  699. /* set all 'other' gains to maximum */
  700. for (i = 2; i <= 6; i++)
  701. e4k_if_gain_set(e4k, i, if_gains_max[i]);
  702. /* iterate over all mixer + if_stage_1 gain combinations */
  703. for (i = 0; i < ARRAY_SIZE(dc_gain_comb); i++) {
  704. uint8_t offs_i, offs_q, range, range_i, range_q;
  705. /* set the combination of mixer / if1 gain */
  706. e4k_mixer_gain_set(e4k, dc_gain_comb[i].mixer_gain);
  707. e4k_if_gain_set(e4k, 1, dc_gain_comb[i].if1_gain);
  708. /* perform actual calibration */
  709. e4k_dc_offset_calibrate(e4k);
  710. /* extract I/Q offset and range values */
  711. offs_i = e4k_reg_read(e4k, E4K_REG_DC2) & 0x3f;
  712. offs_q = e4k_reg_read(e4k, E4K_REG_DC3) & 0x3f;
  713. range = e4k_reg_read(e4k, E4K_REG_DC4);
  714. range_i = range & 0x3;
  715. range_q = (range >> 4) & 0x3;
  716. fprintf(stderr, "[E4K] Table %u I=%u/%u, Q=%u/%u\n",
  717. i, range_i, offs_i, range_q, offs_q);
  718. /* write into the table */
  719. e4k_reg_write(e4k, dc_gain_comb[i].reg,
  720. TO_LUT(offs_q, range_q));
  721. e4k_reg_write(e4k, dc_gain_comb[i].reg + 0x10,
  722. TO_LUT(offs_i, range_i));
  723. }
  724. return 0;
  725. }
  726. /***********************************************************************
  727. * Initialization */
  728. static int magic_init(struct e4k_state *e4k)
  729. {
  730. e4k_reg_write(e4k, 0x7e, 0x01);
  731. e4k_reg_write(e4k, 0x7f, 0xfe);
  732. e4k_reg_write(e4k, 0x82, 0x00);
  733. e4k_reg_write(e4k, 0x86, 0x50); /* polarity A */
  734. e4k_reg_write(e4k, 0x87, 0x20);
  735. e4k_reg_write(e4k, 0x88, 0x01);
  736. e4k_reg_write(e4k, 0x9f, 0x7f);
  737. e4k_reg_write(e4k, 0xa0, 0x07);
  738. return 0;
  739. }
  740. /*! \brief Initialize the E4K tuner
  741. */
  742. int e4k_init(struct e4k_state *e4k)
  743. {
  744. /* make a dummy i2c read or write command, will not be ACKed! */
  745. e4k_reg_read(e4k, 0);
  746. /* Make sure we reset everything and clear POR indicator */
  747. e4k_reg_write(e4k, E4K_REG_MASTER1,
  748. E4K_MASTER1_RESET |
  749. E4K_MASTER1_NORM_STBY |
  750. E4K_MASTER1_POR_DET
  751. );
  752. /* Configure clock input */
  753. e4k_reg_write(e4k, E4K_REG_CLK_INP, 0x00);
  754. /* Disable clock output */
  755. e4k_reg_write(e4k, E4K_REG_REF_CLK, 0x00);
  756. e4k_reg_write(e4k, E4K_REG_CLKOUT_PWDN, 0x96);
  757. /* Write some magic values into registers */
  758. magic_init(e4k);
  759. #if 0
  760. /* Set common mode voltage a bit higher for more margin 850 mv */
  761. e4k_commonmode_set(e4k, 4);
  762. /* Initialize DC offset lookup tables */
  763. e4k_dc_offset_gen_table(e4k);
  764. /* Enable time variant DC correction */
  765. e4k_reg_write(e4k, E4K_REG_DCTIME1, 0x01);
  766. e4k_reg_write(e4k, E4K_REG_DCTIME2, 0x01);
  767. #endif
  768. /* Set LNA mode to manual */
  769. e4k_reg_write(e4k, E4K_REG_AGC4, 0x10); /* High threshold */
  770. e4k_reg_write(e4k, E4K_REG_AGC5, 0x04); /* Low threshold */
  771. e4k_reg_write(e4k, E4K_REG_AGC6, 0x1a); /* LNA calib + loop rate */
  772. e4k_reg_set_mask(e4k, E4K_REG_AGC1, E4K_AGC1_MOD_MASK,
  773. E4K_AGC_MOD_SERIAL);
  774. /* Set Mixer Gain Control to manual */
  775. e4k_reg_set_mask(e4k, E4K_REG_AGC7, E4K_AGC7_MIX_GAIN_AUTO, 0);
  776. #if 0
  777. /* Enable LNA Gain enhancement */
  778. e4k_reg_set_mask(e4k, E4K_REG_AGC11, 0x7,
  779. E4K_AGC11_LNA_GAIN_ENH | (2 << 1));
  780. /* Enable automatic IF gain mode switching */
  781. e4k_reg_set_mask(e4k, E4K_REG_AGC8, 0x1, E4K_AGC8_SENS_LIN_AUTO);
  782. #endif
  783. /* Use auto-gain as default */
  784. e4k_enable_manual_gain(e4k, 0);
  785. /* Select moderate gain levels */
  786. e4k_if_gain_set(e4k, 1, 6);
  787. e4k_if_gain_set(e4k, 2, 0);
  788. e4k_if_gain_set(e4k, 3, 0);
  789. e4k_if_gain_set(e4k, 4, 0);
  790. e4k_if_gain_set(e4k, 5, 9);
  791. e4k_if_gain_set(e4k, 6, 9);
  792. /* Set the most narrow filter we can possibly use */
  793. e4k_if_filter_bw_set(e4k, E4K_IF_FILTER_MIX, KHZ(1900));
  794. e4k_if_filter_bw_set(e4k, E4K_IF_FILTER_RC, KHZ(1000));
  795. e4k_if_filter_bw_set(e4k, E4K_IF_FILTER_CHAN, KHZ(2150));
  796. e4k_if_filter_chan_enable(e4k, 1);
  797. /* Disable time variant DC correction and LUT */
  798. e4k_reg_set_mask(e4k, E4K_REG_DC5, 0x03, 0);
  799. e4k_reg_set_mask(e4k, E4K_REG_DCTIME1, 0x03, 0);
  800. e4k_reg_set_mask(e4k, E4K_REG_DCTIME2, 0x03, 0);
  801. return 0;
  802. }