PageRenderTime 68ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/media/dvb/frontends/dib0090.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 2539 lines | 2037 code | 399 blank | 103 comment | 444 complexity | 173aea7f46a970b97863768ab1ba62ea MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Linux-DVB Driver for DiBcom's DiB0090 base-band RF Tuner.
  3. *
  4. * Copyright (C) 2005-9 DiBcom (http://www.dibcom.fr/)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * 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. *
  22. * This code is more or less generated from another driver, please
  23. * excuse some codingstyle oddities.
  24. *
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/i2c.h>
  29. #include <linux/mutex.h>
  30. #include "dvb_frontend.h"
  31. #include "dib0090.h"
  32. #include "dibx000_common.h"
  33. static int debug;
  34. module_param(debug, int, 0644);
  35. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  36. #define dprintk(args...) do { \
  37. if (debug) { \
  38. printk(KERN_DEBUG "DiB0090: "); \
  39. printk(args); \
  40. printk("\n"); \
  41. } \
  42. } while (0)
  43. #define CONFIG_SYS_DVBT
  44. #define CONFIG_SYS_ISDBT
  45. #define CONFIG_BAND_CBAND
  46. #define CONFIG_BAND_VHF
  47. #define CONFIG_BAND_UHF
  48. #define CONFIG_DIB0090_USE_PWM_AGC
  49. #define EN_LNA0 0x8000
  50. #define EN_LNA1 0x4000
  51. #define EN_LNA2 0x2000
  52. #define EN_LNA3 0x1000
  53. #define EN_MIX0 0x0800
  54. #define EN_MIX1 0x0400
  55. #define EN_MIX2 0x0200
  56. #define EN_MIX3 0x0100
  57. #define EN_IQADC 0x0040
  58. #define EN_PLL 0x0020
  59. #define EN_TX 0x0010
  60. #define EN_BB 0x0008
  61. #define EN_LO 0x0004
  62. #define EN_BIAS 0x0001
  63. #define EN_IQANA 0x0002
  64. #define EN_DIGCLK 0x0080 /* not in the 0x24 reg, only in 0x1b */
  65. #define EN_CRYSTAL 0x0002
  66. #define EN_UHF 0x22E9
  67. #define EN_VHF 0x44E9
  68. #define EN_LBD 0x11E9
  69. #define EN_SBD 0x44E9
  70. #define EN_CAB 0x88E9
  71. /* Calibration defines */
  72. #define DC_CAL 0x1
  73. #define WBD_CAL 0x2
  74. #define TEMP_CAL 0x4
  75. #define CAPTRIM_CAL 0x8
  76. #define KROSUS_PLL_LOCKED 0x800
  77. #define KROSUS 0x2
  78. /* Use those defines to identify SOC version */
  79. #define SOC 0x02
  80. #define SOC_7090_P1G_11R1 0x82
  81. #define SOC_7090_P1G_21R1 0x8a
  82. #define SOC_8090_P1G_11R1 0x86
  83. #define SOC_8090_P1G_21R1 0x8e
  84. /* else use thos ones to check */
  85. #define P1A_B 0x0
  86. #define P1C 0x1
  87. #define P1D_E_F 0x3
  88. #define P1G 0x7
  89. #define P1G_21R2 0xf
  90. #define MP001 0x1 /* Single 9090/8096 */
  91. #define MP005 0x4 /* Single Sband */
  92. #define MP008 0x6 /* Dual diversity VHF-UHF-LBAND */
  93. #define MP009 0x7 /* Dual diversity 29098 CBAND-UHF-LBAND-SBAND */
  94. #define pgm_read_word(w) (*w)
  95. struct dc_calibration;
  96. struct dib0090_tuning {
  97. u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
  98. u8 switch_trim;
  99. u8 lna_tune;
  100. u16 lna_bias;
  101. u16 v2i;
  102. u16 mix;
  103. u16 load;
  104. u16 tuner_enable;
  105. };
  106. struct dib0090_pll {
  107. u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
  108. u8 vco_band;
  109. u8 hfdiv_code;
  110. u8 hfdiv;
  111. u8 topresc;
  112. };
  113. struct dib0090_identity {
  114. u8 version;
  115. u8 product;
  116. u8 p1g;
  117. u8 in_soc;
  118. };
  119. struct dib0090_state {
  120. struct i2c_adapter *i2c;
  121. struct dvb_frontend *fe;
  122. const struct dib0090_config *config;
  123. u8 current_band;
  124. enum frontend_tune_state tune_state;
  125. u32 current_rf;
  126. u16 wbd_offset;
  127. s16 wbd_target; /* in dB */
  128. s16 rf_gain_limit; /* take-over-point: where to split between bb and rf gain */
  129. s16 current_gain; /* keeps the currently programmed gain */
  130. u8 agc_step; /* new binary search */
  131. u16 gain[2]; /* for channel monitoring */
  132. const u16 *rf_ramp;
  133. const u16 *bb_ramp;
  134. /* for the software AGC ramps */
  135. u16 bb_1_def;
  136. u16 rf_lt_def;
  137. u16 gain_reg[4];
  138. /* for the captrim/dc-offset search */
  139. s8 step;
  140. s16 adc_diff;
  141. s16 min_adc_diff;
  142. s8 captrim;
  143. s8 fcaptrim;
  144. const struct dc_calibration *dc;
  145. u16 bb6, bb7;
  146. const struct dib0090_tuning *current_tune_table_index;
  147. const struct dib0090_pll *current_pll_table_index;
  148. u8 tuner_is_tuned;
  149. u8 agc_freeze;
  150. struct dib0090_identity identity;
  151. u32 rf_request;
  152. u8 current_standard;
  153. u8 calibrate;
  154. u32 rest;
  155. u16 bias;
  156. s16 temperature;
  157. u8 wbd_calibration_gain;
  158. const struct dib0090_wbd_slope *current_wbd_table;
  159. u16 wbdmux;
  160. /* for the I2C transfer */
  161. struct i2c_msg msg[2];
  162. u8 i2c_write_buffer[3];
  163. u8 i2c_read_buffer[2];
  164. struct mutex i2c_buffer_lock;
  165. };
  166. struct dib0090_fw_state {
  167. struct i2c_adapter *i2c;
  168. struct dvb_frontend *fe;
  169. struct dib0090_identity identity;
  170. const struct dib0090_config *config;
  171. /* for the I2C transfer */
  172. struct i2c_msg msg;
  173. u8 i2c_write_buffer[2];
  174. u8 i2c_read_buffer[2];
  175. struct mutex i2c_buffer_lock;
  176. };
  177. static u16 dib0090_read_reg(struct dib0090_state *state, u8 reg)
  178. {
  179. u16 ret;
  180. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  181. dprintk("could not acquire lock");
  182. return 0;
  183. }
  184. state->i2c_write_buffer[0] = reg;
  185. memset(state->msg, 0, 2 * sizeof(struct i2c_msg));
  186. state->msg[0].addr = state->config->i2c_address;
  187. state->msg[0].flags = 0;
  188. state->msg[0].buf = state->i2c_write_buffer;
  189. state->msg[0].len = 1;
  190. state->msg[1].addr = state->config->i2c_address;
  191. state->msg[1].flags = I2C_M_RD;
  192. state->msg[1].buf = state->i2c_read_buffer;
  193. state->msg[1].len = 2;
  194. if (i2c_transfer(state->i2c, state->msg, 2) != 2) {
  195. printk(KERN_WARNING "DiB0090 I2C read failed\n");
  196. ret = 0;
  197. } else
  198. ret = (state->i2c_read_buffer[0] << 8)
  199. | state->i2c_read_buffer[1];
  200. mutex_unlock(&state->i2c_buffer_lock);
  201. return ret;
  202. }
  203. static int dib0090_write_reg(struct dib0090_state *state, u32 reg, u16 val)
  204. {
  205. int ret;
  206. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  207. dprintk("could not acquire lock");
  208. return -EINVAL;
  209. }
  210. state->i2c_write_buffer[0] = reg & 0xff;
  211. state->i2c_write_buffer[1] = val >> 8;
  212. state->i2c_write_buffer[2] = val & 0xff;
  213. memset(state->msg, 0, sizeof(struct i2c_msg));
  214. state->msg[0].addr = state->config->i2c_address;
  215. state->msg[0].flags = 0;
  216. state->msg[0].buf = state->i2c_write_buffer;
  217. state->msg[0].len = 3;
  218. if (i2c_transfer(state->i2c, state->msg, 1) != 1) {
  219. printk(KERN_WARNING "DiB0090 I2C write failed\n");
  220. ret = -EREMOTEIO;
  221. } else
  222. ret = 0;
  223. mutex_unlock(&state->i2c_buffer_lock);
  224. return ret;
  225. }
  226. static u16 dib0090_fw_read_reg(struct dib0090_fw_state *state, u8 reg)
  227. {
  228. u16 ret;
  229. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  230. dprintk("could not acquire lock");
  231. return 0;
  232. }
  233. state->i2c_write_buffer[0] = reg;
  234. memset(&state->msg, 0, sizeof(struct i2c_msg));
  235. state->msg.addr = reg;
  236. state->msg.flags = I2C_M_RD;
  237. state->msg.buf = state->i2c_read_buffer;
  238. state->msg.len = 2;
  239. if (i2c_transfer(state->i2c, &state->msg, 1) != 1) {
  240. printk(KERN_WARNING "DiB0090 I2C read failed\n");
  241. ret = 0;
  242. } else
  243. ret = (state->i2c_read_buffer[0] << 8)
  244. | state->i2c_read_buffer[1];
  245. mutex_unlock(&state->i2c_buffer_lock);
  246. return ret;
  247. }
  248. static int dib0090_fw_write_reg(struct dib0090_fw_state *state, u8 reg, u16 val)
  249. {
  250. int ret;
  251. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  252. dprintk("could not acquire lock");
  253. return -EINVAL;
  254. }
  255. state->i2c_write_buffer[0] = val >> 8;
  256. state->i2c_write_buffer[1] = val & 0xff;
  257. memset(&state->msg, 0, sizeof(struct i2c_msg));
  258. state->msg.addr = reg;
  259. state->msg.flags = 0;
  260. state->msg.buf = state->i2c_write_buffer;
  261. state->msg.len = 2;
  262. if (i2c_transfer(state->i2c, &state->msg, 1) != 1) {
  263. printk(KERN_WARNING "DiB0090 I2C write failed\n");
  264. ret = -EREMOTEIO;
  265. } else
  266. ret = 0;
  267. mutex_unlock(&state->i2c_buffer_lock);
  268. return ret;
  269. }
  270. #define HARD_RESET(state) do { if (cfg->reset) { if (cfg->sleep) cfg->sleep(fe, 0); msleep(10); cfg->reset(fe, 1); msleep(10); cfg->reset(fe, 0); msleep(10); } } while (0)
  271. #define ADC_TARGET -220
  272. #define GAIN_ALPHA 5
  273. #define WBD_ALPHA 6
  274. #define LPF 100
  275. static void dib0090_write_regs(struct dib0090_state *state, u8 r, const u16 * b, u8 c)
  276. {
  277. do {
  278. dib0090_write_reg(state, r++, *b++);
  279. } while (--c);
  280. }
  281. static int dib0090_identify(struct dvb_frontend *fe)
  282. {
  283. struct dib0090_state *state = fe->tuner_priv;
  284. u16 v;
  285. struct dib0090_identity *identity = &state->identity;
  286. v = dib0090_read_reg(state, 0x1a);
  287. identity->p1g = 0;
  288. identity->in_soc = 0;
  289. dprintk("Tuner identification (Version = 0x%04x)", v);
  290. /* without PLL lock info */
  291. v &= ~KROSUS_PLL_LOCKED;
  292. identity->version = v & 0xff;
  293. identity->product = (v >> 8) & 0xf;
  294. if (identity->product != KROSUS)
  295. goto identification_error;
  296. if ((identity->version & 0x3) == SOC) {
  297. identity->in_soc = 1;
  298. switch (identity->version) {
  299. case SOC_8090_P1G_11R1:
  300. dprintk("SOC 8090 P1-G11R1 Has been detected");
  301. identity->p1g = 1;
  302. break;
  303. case SOC_8090_P1G_21R1:
  304. dprintk("SOC 8090 P1-G21R1 Has been detected");
  305. identity->p1g = 1;
  306. break;
  307. case SOC_7090_P1G_11R1:
  308. dprintk("SOC 7090 P1-G11R1 Has been detected");
  309. identity->p1g = 1;
  310. break;
  311. case SOC_7090_P1G_21R1:
  312. dprintk("SOC 7090 P1-G21R1 Has been detected");
  313. identity->p1g = 1;
  314. break;
  315. default:
  316. goto identification_error;
  317. }
  318. } else {
  319. switch ((identity->version >> 5) & 0x7) {
  320. case MP001:
  321. dprintk("MP001 : 9090/8096");
  322. break;
  323. case MP005:
  324. dprintk("MP005 : Single Sband");
  325. break;
  326. case MP008:
  327. dprintk("MP008 : diversity VHF-UHF-LBAND");
  328. break;
  329. case MP009:
  330. dprintk("MP009 : diversity 29098 CBAND-UHF-LBAND-SBAND");
  331. break;
  332. default:
  333. goto identification_error;
  334. }
  335. switch (identity->version & 0x1f) {
  336. case P1G_21R2:
  337. dprintk("P1G_21R2 detected");
  338. identity->p1g = 1;
  339. break;
  340. case P1G:
  341. dprintk("P1G detected");
  342. identity->p1g = 1;
  343. break;
  344. case P1D_E_F:
  345. dprintk("P1D/E/F detected");
  346. break;
  347. case P1C:
  348. dprintk("P1C detected");
  349. break;
  350. case P1A_B:
  351. dprintk("P1-A/B detected: driver is deactivated - not available");
  352. goto identification_error;
  353. break;
  354. default:
  355. goto identification_error;
  356. }
  357. }
  358. return 0;
  359. identification_error:
  360. return -EIO;
  361. }
  362. static int dib0090_fw_identify(struct dvb_frontend *fe)
  363. {
  364. struct dib0090_fw_state *state = fe->tuner_priv;
  365. struct dib0090_identity *identity = &state->identity;
  366. u16 v = dib0090_fw_read_reg(state, 0x1a);
  367. identity->p1g = 0;
  368. identity->in_soc = 0;
  369. dprintk("FE: Tuner identification (Version = 0x%04x)", v);
  370. /* without PLL lock info */
  371. v &= ~KROSUS_PLL_LOCKED;
  372. identity->version = v & 0xff;
  373. identity->product = (v >> 8) & 0xf;
  374. if (identity->product != KROSUS)
  375. goto identification_error;
  376. if ((identity->version & 0x3) == SOC) {
  377. identity->in_soc = 1;
  378. switch (identity->version) {
  379. case SOC_8090_P1G_11R1:
  380. dprintk("SOC 8090 P1-G11R1 Has been detected");
  381. identity->p1g = 1;
  382. break;
  383. case SOC_8090_P1G_21R1:
  384. dprintk("SOC 8090 P1-G21R1 Has been detected");
  385. identity->p1g = 1;
  386. break;
  387. case SOC_7090_P1G_11R1:
  388. dprintk("SOC 7090 P1-G11R1 Has been detected");
  389. identity->p1g = 1;
  390. break;
  391. case SOC_7090_P1G_21R1:
  392. dprintk("SOC 7090 P1-G21R1 Has been detected");
  393. identity->p1g = 1;
  394. break;
  395. default:
  396. goto identification_error;
  397. }
  398. } else {
  399. switch ((identity->version >> 5) & 0x7) {
  400. case MP001:
  401. dprintk("MP001 : 9090/8096");
  402. break;
  403. case MP005:
  404. dprintk("MP005 : Single Sband");
  405. break;
  406. case MP008:
  407. dprintk("MP008 : diversity VHF-UHF-LBAND");
  408. break;
  409. case MP009:
  410. dprintk("MP009 : diversity 29098 CBAND-UHF-LBAND-SBAND");
  411. break;
  412. default:
  413. goto identification_error;
  414. }
  415. switch (identity->version & 0x1f) {
  416. case P1G_21R2:
  417. dprintk("P1G_21R2 detected");
  418. identity->p1g = 1;
  419. break;
  420. case P1G:
  421. dprintk("P1G detected");
  422. identity->p1g = 1;
  423. break;
  424. case P1D_E_F:
  425. dprintk("P1D/E/F detected");
  426. break;
  427. case P1C:
  428. dprintk("P1C detected");
  429. break;
  430. case P1A_B:
  431. dprintk("P1-A/B detected: driver is deactivated - not available");
  432. goto identification_error;
  433. break;
  434. default:
  435. goto identification_error;
  436. }
  437. }
  438. return 0;
  439. identification_error:
  440. return -EIO;;
  441. }
  442. static void dib0090_reset_digital(struct dvb_frontend *fe, const struct dib0090_config *cfg)
  443. {
  444. struct dib0090_state *state = fe->tuner_priv;
  445. u16 PllCfg, i, v;
  446. HARD_RESET(state);
  447. dib0090_write_reg(state, 0x24, EN_PLL | EN_CRYSTAL);
  448. dib0090_write_reg(state, 0x1b, EN_DIGCLK | EN_PLL | EN_CRYSTAL); /* PLL, DIG_CLK and CRYSTAL remain */
  449. if (!cfg->in_soc) {
  450. /* adcClkOutRatio=8->7, release reset */
  451. dib0090_write_reg(state, 0x20, ((cfg->io.adc_clock_ratio - 1) << 11) | (0 << 10) | (1 << 9) | (1 << 8) | (0 << 4) | 0);
  452. if (cfg->clkoutdrive != 0)
  453. dib0090_write_reg(state, 0x23, (0 << 15) | ((!cfg->analog_output) << 14) | (2 << 10) | (1 << 9) | (0 << 8)
  454. | (cfg->clkoutdrive << 5) | (cfg->clkouttobamse << 4) | (0 << 2) | (0));
  455. else
  456. dib0090_write_reg(state, 0x23, (0 << 15) | ((!cfg->analog_output) << 14) | (2 << 10) | (1 << 9) | (0 << 8)
  457. | (7 << 5) | (cfg->clkouttobamse << 4) | (0 << 2) | (0));
  458. }
  459. /* Read Pll current config * */
  460. PllCfg = dib0090_read_reg(state, 0x21);
  461. /** Reconfigure PLL if current setting is different from default setting **/
  462. if ((PllCfg & 0x1FFF) != ((cfg->io.pll_range << 12) | (cfg->io.pll_loopdiv << 6) | (cfg->io.pll_prediv)) && (!cfg->in_soc)
  463. && !cfg->io.pll_bypass) {
  464. /* Set Bypass mode */
  465. PllCfg |= (1 << 15);
  466. dib0090_write_reg(state, 0x21, PllCfg);
  467. /* Set Reset Pll */
  468. PllCfg &= ~(1 << 13);
  469. dib0090_write_reg(state, 0x21, PllCfg);
  470. /*** Set new Pll configuration in bypass and reset state ***/
  471. PllCfg = (1 << 15) | (0 << 13) | (cfg->io.pll_range << 12) | (cfg->io.pll_loopdiv << 6) | (cfg->io.pll_prediv);
  472. dib0090_write_reg(state, 0x21, PllCfg);
  473. /* Remove Reset Pll */
  474. PllCfg |= (1 << 13);
  475. dib0090_write_reg(state, 0x21, PllCfg);
  476. /*** Wait for PLL lock ***/
  477. i = 100;
  478. do {
  479. v = !!(dib0090_read_reg(state, 0x1a) & 0x800);
  480. if (v)
  481. break;
  482. } while (--i);
  483. if (i == 0) {
  484. dprintk("Pll: Unable to lock Pll");
  485. return;
  486. }
  487. /* Finally Remove Bypass mode */
  488. PllCfg &= ~(1 << 15);
  489. dib0090_write_reg(state, 0x21, PllCfg);
  490. }
  491. if (cfg->io.pll_bypass) {
  492. PllCfg |= (cfg->io.pll_bypass << 15);
  493. dib0090_write_reg(state, 0x21, PllCfg);
  494. }
  495. }
  496. static int dib0090_fw_reset_digital(struct dvb_frontend *fe, const struct dib0090_config *cfg)
  497. {
  498. struct dib0090_fw_state *state = fe->tuner_priv;
  499. u16 PllCfg;
  500. u16 v;
  501. int i;
  502. dprintk("fw reset digital");
  503. HARD_RESET(state);
  504. dib0090_fw_write_reg(state, 0x24, EN_PLL | EN_CRYSTAL);
  505. dib0090_fw_write_reg(state, 0x1b, EN_DIGCLK | EN_PLL | EN_CRYSTAL); /* PLL, DIG_CLK and CRYSTAL remain */
  506. dib0090_fw_write_reg(state, 0x20,
  507. ((cfg->io.adc_clock_ratio - 1) << 11) | (0 << 10) | (1 << 9) | (1 << 8) | (cfg->data_tx_drv << 4) | cfg->ls_cfg_pad_drv);
  508. v = (0 << 15) | ((!cfg->analog_output) << 14) | (1 << 9) | (0 << 8) | (cfg->clkouttobamse << 4) | (0 << 2) | (0);
  509. if (cfg->clkoutdrive != 0)
  510. v |= cfg->clkoutdrive << 5;
  511. else
  512. v |= 7 << 5;
  513. v |= 2 << 10;
  514. dib0090_fw_write_reg(state, 0x23, v);
  515. /* Read Pll current config * */
  516. PllCfg = dib0090_fw_read_reg(state, 0x21);
  517. /** Reconfigure PLL if current setting is different from default setting **/
  518. if ((PllCfg & 0x1FFF) != ((cfg->io.pll_range << 12) | (cfg->io.pll_loopdiv << 6) | (cfg->io.pll_prediv)) && !cfg->io.pll_bypass) {
  519. /* Set Bypass mode */
  520. PllCfg |= (1 << 15);
  521. dib0090_fw_write_reg(state, 0x21, PllCfg);
  522. /* Set Reset Pll */
  523. PllCfg &= ~(1 << 13);
  524. dib0090_fw_write_reg(state, 0x21, PllCfg);
  525. /*** Set new Pll configuration in bypass and reset state ***/
  526. PllCfg = (1 << 15) | (0 << 13) | (cfg->io.pll_range << 12) | (cfg->io.pll_loopdiv << 6) | (cfg->io.pll_prediv);
  527. dib0090_fw_write_reg(state, 0x21, PllCfg);
  528. /* Remove Reset Pll */
  529. PllCfg |= (1 << 13);
  530. dib0090_fw_write_reg(state, 0x21, PllCfg);
  531. /*** Wait for PLL lock ***/
  532. i = 100;
  533. do {
  534. v = !!(dib0090_fw_read_reg(state, 0x1a) & 0x800);
  535. if (v)
  536. break;
  537. } while (--i);
  538. if (i == 0) {
  539. dprintk("Pll: Unable to lock Pll");
  540. return -EIO;
  541. }
  542. /* Finally Remove Bypass mode */
  543. PllCfg &= ~(1 << 15);
  544. dib0090_fw_write_reg(state, 0x21, PllCfg);
  545. }
  546. if (cfg->io.pll_bypass) {
  547. PllCfg |= (cfg->io.pll_bypass << 15);
  548. dib0090_fw_write_reg(state, 0x21, PllCfg);
  549. }
  550. return dib0090_fw_identify(fe);
  551. }
  552. static int dib0090_wakeup(struct dvb_frontend *fe)
  553. {
  554. struct dib0090_state *state = fe->tuner_priv;
  555. if (state->config->sleep)
  556. state->config->sleep(fe, 0);
  557. /* enable dataTX in case we have been restarted in the wrong moment */
  558. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) | (1 << 14));
  559. return 0;
  560. }
  561. static int dib0090_sleep(struct dvb_frontend *fe)
  562. {
  563. struct dib0090_state *state = fe->tuner_priv;
  564. if (state->config->sleep)
  565. state->config->sleep(fe, 1);
  566. return 0;
  567. }
  568. void dib0090_dcc_freq(struct dvb_frontend *fe, u8 fast)
  569. {
  570. struct dib0090_state *state = fe->tuner_priv;
  571. if (fast)
  572. dib0090_write_reg(state, 0x04, 0);
  573. else
  574. dib0090_write_reg(state, 0x04, 1);
  575. }
  576. EXPORT_SYMBOL(dib0090_dcc_freq);
  577. static const u16 bb_ramp_pwm_normal_socs[] = {
  578. 550, /* max BB gain in 10th of dB */
  579. (1 << 9) | 8, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> BB_RAMP2 */
  580. 440,
  581. (4 << 9) | 0, /* BB_RAMP3 = 26dB */
  582. (0 << 9) | 208, /* BB_RAMP4 */
  583. (4 << 9) | 208, /* BB_RAMP5 = 29dB */
  584. (0 << 9) | 440, /* BB_RAMP6 */
  585. };
  586. static const u16 rf_ramp_pwm_cband_7090[] = {
  587. 280, /* max RF gain in 10th of dB */
  588. 18, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  589. 504, /* ramp_max = maximum X used on the ramp */
  590. (29 << 10) | 364, /* RF_RAMP5, LNA 1 = 8dB */
  591. (0 << 10) | 504, /* RF_RAMP6, LNA 1 */
  592. (60 << 10) | 228, /* RF_RAMP7, LNA 2 = 7.7dB */
  593. (0 << 10) | 364, /* RF_RAMP8, LNA 2 */
  594. (34 << 10) | 109, /* GAIN_4_1, LNA 3 = 6.8dB */
  595. (0 << 10) | 228, /* GAIN_4_2, LNA 3 */
  596. (37 << 10) | 0, /* RF_RAMP3, LNA 4 = 6.2dB */
  597. (0 << 10) | 109, /* RF_RAMP4, LNA 4 */
  598. };
  599. static const u16 rf_ramp_pwm_cband_8090[] = {
  600. 345, /* max RF gain in 10th of dB */
  601. 29, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  602. 1000, /* ramp_max = maximum X used on the ramp */
  603. (35 << 10) | 772, /* RF_RAMP3, LNA 1 = 8dB */
  604. (0 << 10) | 1000, /* RF_RAMP4, LNA 1 */
  605. (58 << 10) | 496, /* RF_RAMP5, LNA 2 = 9.5dB */
  606. (0 << 10) | 772, /* RF_RAMP6, LNA 2 */
  607. (27 << 10) | 200, /* RF_RAMP7, LNA 3 = 10.5dB */
  608. (0 << 10) | 496, /* RF_RAMP8, LNA 3 */
  609. (40 << 10) | 0, /* GAIN_4_1, LNA 4 = 7dB */
  610. (0 << 10) | 200, /* GAIN_4_2, LNA 4 */
  611. };
  612. static const u16 rf_ramp_pwm_uhf_7090[] = {
  613. 407, /* max RF gain in 10th of dB */
  614. 13, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  615. 529, /* ramp_max = maximum X used on the ramp */
  616. (23 << 10) | 0, /* RF_RAMP3, LNA 1 = 14.7dB */
  617. (0 << 10) | 176, /* RF_RAMP4, LNA 1 */
  618. (63 << 10) | 400, /* RF_RAMP5, LNA 2 = 8dB */
  619. (0 << 10) | 529, /* RF_RAMP6, LNA 2 */
  620. (48 << 10) | 316, /* RF_RAMP7, LNA 3 = 6.8dB */
  621. (0 << 10) | 400, /* RF_RAMP8, LNA 3 */
  622. (29 << 10) | 176, /* GAIN_4_1, LNA 4 = 11.5dB */
  623. (0 << 10) | 316, /* GAIN_4_2, LNA 4 */
  624. };
  625. static const u16 rf_ramp_pwm_uhf_8090[] = {
  626. 388, /* max RF gain in 10th of dB */
  627. 26, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  628. 1008, /* ramp_max = maximum X used on the ramp */
  629. (11 << 10) | 0, /* RF_RAMP3, LNA 1 = 14.7dB */
  630. (0 << 10) | 369, /* RF_RAMP4, LNA 1 */
  631. (41 << 10) | 809, /* RF_RAMP5, LNA 2 = 8dB */
  632. (0 << 10) | 1008, /* RF_RAMP6, LNA 2 */
  633. (27 << 10) | 659, /* RF_RAMP7, LNA 3 = 6dB */
  634. (0 << 10) | 809, /* RF_RAMP8, LNA 3 */
  635. (14 << 10) | 369, /* GAIN_4_1, LNA 4 = 11.5dB */
  636. (0 << 10) | 659, /* GAIN_4_2, LNA 4 */
  637. };
  638. static const u16 rf_ramp_pwm_cband[] = {
  639. 0, /* max RF gain in 10th of dB */
  640. 0, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> 0x2b */
  641. 0, /* ramp_max = maximum X used on the ramp */
  642. (0 << 10) | 0, /* 0x2c, LNA 1 = 0dB */
  643. (0 << 10) | 0, /* 0x2d, LNA 1 */
  644. (0 << 10) | 0, /* 0x2e, LNA 2 = 0dB */
  645. (0 << 10) | 0, /* 0x2f, LNA 2 */
  646. (0 << 10) | 0, /* 0x30, LNA 3 = 0dB */
  647. (0 << 10) | 0, /* 0x31, LNA 3 */
  648. (0 << 10) | 0, /* GAIN_4_1, LNA 4 = 0dB */
  649. (0 << 10) | 0, /* GAIN_4_2, LNA 4 */
  650. };
  651. static const u16 rf_ramp_vhf[] = {
  652. 412, /* max RF gain in 10th of dB */
  653. 132, 307, 127, /* LNA1, 13.2dB */
  654. 105, 412, 255, /* LNA2, 10.5dB */
  655. 50, 50, 127, /* LNA3, 5dB */
  656. 125, 175, 127, /* LNA4, 12.5dB */
  657. 0, 0, 127, /* CBAND, 0dB */
  658. };
  659. static const u16 rf_ramp_uhf[] = {
  660. 412, /* max RF gain in 10th of dB */
  661. 132, 307, 127, /* LNA1 : total gain = 13.2dB, point on the ramp where this amp is full gain, value to write to get full gain */
  662. 105, 412, 255, /* LNA2 : 10.5 dB */
  663. 50, 50, 127, /* LNA3 : 5.0 dB */
  664. 125, 175, 127, /* LNA4 : 12.5 dB */
  665. 0, 0, 127, /* CBAND : 0.0 dB */
  666. };
  667. static const u16 rf_ramp_cband_broadmatching[] = /* for p1G only */
  668. {
  669. 314, /* Calibrated at 200MHz order has been changed g4-g3-g2-g1 */
  670. 84, 314, 127, /* LNA1 */
  671. 80, 230, 255, /* LNA2 */
  672. 80, 150, 127, /* LNA3 It was measured 12dB, do not lock if 120 */
  673. 70, 70, 127, /* LNA4 */
  674. 0, 0, 127, /* CBAND */
  675. };
  676. static const u16 rf_ramp_cband[] = {
  677. 332, /* max RF gain in 10th of dB */
  678. 132, 252, 127, /* LNA1, dB */
  679. 80, 332, 255, /* LNA2, dB */
  680. 0, 0, 127, /* LNA3, dB */
  681. 0, 0, 127, /* LNA4, dB */
  682. 120, 120, 127, /* LT1 CBAND */
  683. };
  684. static const u16 rf_ramp_pwm_vhf[] = {
  685. 404, /* max RF gain in 10th of dB */
  686. 25, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> 0x2b */
  687. 1011, /* ramp_max = maximum X used on the ramp */
  688. (6 << 10) | 417, /* 0x2c, LNA 1 = 13.2dB */
  689. (0 << 10) | 756, /* 0x2d, LNA 1 */
  690. (16 << 10) | 756, /* 0x2e, LNA 2 = 10.5dB */
  691. (0 << 10) | 1011, /* 0x2f, LNA 2 */
  692. (16 << 10) | 290, /* 0x30, LNA 3 = 5dB */
  693. (0 << 10) | 417, /* 0x31, LNA 3 */
  694. (7 << 10) | 0, /* GAIN_4_1, LNA 4 = 12.5dB */
  695. (0 << 10) | 290, /* GAIN_4_2, LNA 4 */
  696. };
  697. static const u16 rf_ramp_pwm_uhf[] = {
  698. 404, /* max RF gain in 10th of dB */
  699. 25, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> 0x2b */
  700. 1011, /* ramp_max = maximum X used on the ramp */
  701. (6 << 10) | 417, /* 0x2c, LNA 1 = 13.2dB */
  702. (0 << 10) | 756, /* 0x2d, LNA 1 */
  703. (16 << 10) | 756, /* 0x2e, LNA 2 = 10.5dB */
  704. (0 << 10) | 1011, /* 0x2f, LNA 2 */
  705. (16 << 10) | 0, /* 0x30, LNA 3 = 5dB */
  706. (0 << 10) | 127, /* 0x31, LNA 3 */
  707. (7 << 10) | 127, /* GAIN_4_1, LNA 4 = 12.5dB */
  708. (0 << 10) | 417, /* GAIN_4_2, LNA 4 */
  709. };
  710. static const u16 bb_ramp_boost[] = {
  711. 550, /* max BB gain in 10th of dB */
  712. 260, 260, 26, /* BB1, 26dB */
  713. 290, 550, 29, /* BB2, 29dB */
  714. };
  715. static const u16 bb_ramp_pwm_normal[] = {
  716. 500, /* max RF gain in 10th of dB */
  717. 8, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> 0x34 */
  718. 400,
  719. (2 << 9) | 0, /* 0x35 = 21dB */
  720. (0 << 9) | 168, /* 0x36 */
  721. (2 << 9) | 168, /* 0x37 = 29dB */
  722. (0 << 9) | 400, /* 0x38 */
  723. };
  724. struct slope {
  725. s16 range;
  726. s16 slope;
  727. };
  728. static u16 slopes_to_scale(const struct slope *slopes, u8 num, s16 val)
  729. {
  730. u8 i;
  731. u16 rest;
  732. u16 ret = 0;
  733. for (i = 0; i < num; i++) {
  734. if (val > slopes[i].range)
  735. rest = slopes[i].range;
  736. else
  737. rest = val;
  738. ret += (rest * slopes[i].slope) / slopes[i].range;
  739. val -= rest;
  740. }
  741. return ret;
  742. }
  743. static const struct slope dib0090_wbd_slopes[3] = {
  744. {66, 120}, /* -64,-52: offset - 65 */
  745. {600, 170}, /* -52,-35: 65 - 665 */
  746. {170, 250}, /* -45,-10: 665 - 835 */
  747. };
  748. static s16 dib0090_wbd_to_db(struct dib0090_state *state, u16 wbd)
  749. {
  750. wbd &= 0x3ff;
  751. if (wbd < state->wbd_offset)
  752. wbd = 0;
  753. else
  754. wbd -= state->wbd_offset;
  755. /* -64dB is the floor */
  756. return -640 + (s16) slopes_to_scale(dib0090_wbd_slopes, ARRAY_SIZE(dib0090_wbd_slopes), wbd);
  757. }
  758. static void dib0090_wbd_target(struct dib0090_state *state, u32 rf)
  759. {
  760. u16 offset = 250;
  761. /* TODO : DAB digital N+/-1 interferer perfs : offset = 10 */
  762. if (state->current_band == BAND_VHF)
  763. offset = 650;
  764. #ifndef FIRMWARE_FIREFLY
  765. if (state->current_band == BAND_VHF)
  766. offset = state->config->wbd_vhf_offset;
  767. if (state->current_band == BAND_CBAND)
  768. offset = state->config->wbd_cband_offset;
  769. #endif
  770. state->wbd_target = dib0090_wbd_to_db(state, state->wbd_offset + offset);
  771. dprintk("wbd-target: %d dB", (u32) state->wbd_target);
  772. }
  773. static const int gain_reg_addr[4] = {
  774. 0x08, 0x0a, 0x0f, 0x01
  775. };
  776. static void dib0090_gain_apply(struct dib0090_state *state, s16 gain_delta, s16 top_delta, u8 force)
  777. {
  778. u16 rf, bb, ref;
  779. u16 i, v, gain_reg[4] = { 0 }, gain;
  780. const u16 *g;
  781. if (top_delta < -511)
  782. top_delta = -511;
  783. if (top_delta > 511)
  784. top_delta = 511;
  785. if (force) {
  786. top_delta *= (1 << WBD_ALPHA);
  787. gain_delta *= (1 << GAIN_ALPHA);
  788. }
  789. if (top_delta >= ((s16) (state->rf_ramp[0] << WBD_ALPHA) - state->rf_gain_limit)) /* overflow */
  790. state->rf_gain_limit = state->rf_ramp[0] << WBD_ALPHA;
  791. else
  792. state->rf_gain_limit += top_delta;
  793. if (state->rf_gain_limit < 0) /*underflow */
  794. state->rf_gain_limit = 0;
  795. /* use gain as a temporary variable and correct current_gain */
  796. gain = ((state->rf_gain_limit >> WBD_ALPHA) + state->bb_ramp[0]) << GAIN_ALPHA;
  797. if (gain_delta >= ((s16) gain - state->current_gain)) /* overflow */
  798. state->current_gain = gain;
  799. else
  800. state->current_gain += gain_delta;
  801. /* cannot be less than 0 (only if gain_delta is less than 0 we can have current_gain < 0) */
  802. if (state->current_gain < 0)
  803. state->current_gain = 0;
  804. /* now split total gain to rf and bb gain */
  805. gain = state->current_gain >> GAIN_ALPHA;
  806. /* requested gain is bigger than rf gain limit - ACI/WBD adjustment */
  807. if (gain > (state->rf_gain_limit >> WBD_ALPHA)) {
  808. rf = state->rf_gain_limit >> WBD_ALPHA;
  809. bb = gain - rf;
  810. if (bb > state->bb_ramp[0])
  811. bb = state->bb_ramp[0];
  812. } else { /* high signal level -> all gains put on RF */
  813. rf = gain;
  814. bb = 0;
  815. }
  816. state->gain[0] = rf;
  817. state->gain[1] = bb;
  818. /* software ramp */
  819. /* Start with RF gains */
  820. g = state->rf_ramp + 1; /* point on RF LNA1 max gain */
  821. ref = rf;
  822. for (i = 0; i < 7; i++) { /* Go over all amplifiers => 5RF amps + 2 BB amps = 7 amps */
  823. if (g[0] == 0 || ref < (g[1] - g[0])) /* if total gain of the current amp is null or this amp is not concerned because it starts to work from an higher gain value */
  824. v = 0; /* force the gain to write for the current amp to be null */
  825. else if (ref >= g[1]) /* Gain to set is higher than the high working point of this amp */
  826. v = g[2]; /* force this amp to be full gain */
  827. else /* compute the value to set to this amp because we are somewhere in his range */
  828. v = ((ref - (g[1] - g[0])) * g[2]) / g[0];
  829. if (i == 0) /* LNA 1 reg mapping */
  830. gain_reg[0] = v;
  831. else if (i == 1) /* LNA 2 reg mapping */
  832. gain_reg[0] |= v << 7;
  833. else if (i == 2) /* LNA 3 reg mapping */
  834. gain_reg[1] = v;
  835. else if (i == 3) /* LNA 4 reg mapping */
  836. gain_reg[1] |= v << 7;
  837. else if (i == 4) /* CBAND LNA reg mapping */
  838. gain_reg[2] = v | state->rf_lt_def;
  839. else if (i == 5) /* BB gain 1 reg mapping */
  840. gain_reg[3] = v << 3;
  841. else if (i == 6) /* BB gain 2 reg mapping */
  842. gain_reg[3] |= v << 8;
  843. g += 3; /* go to next gain bloc */
  844. /* When RF is finished, start with BB */
  845. if (i == 4) {
  846. g = state->bb_ramp + 1; /* point on BB gain 1 max gain */
  847. ref = bb;
  848. }
  849. }
  850. gain_reg[3] |= state->bb_1_def;
  851. gain_reg[3] |= ((bb % 10) * 100) / 125;
  852. #ifdef DEBUG_AGC
  853. dprintk("GA CALC: DB: %3d(rf) + %3d(bb) = %3d gain_reg[0]=%04x gain_reg[1]=%04x gain_reg[2]=%04x gain_reg[0]=%04x", rf, bb, rf + bb,
  854. gain_reg[0], gain_reg[1], gain_reg[2], gain_reg[3]);
  855. #endif
  856. /* Write the amplifier regs */
  857. for (i = 0; i < 4; i++) {
  858. v = gain_reg[i];
  859. if (force || state->gain_reg[i] != v) {
  860. state->gain_reg[i] = v;
  861. dib0090_write_reg(state, gain_reg_addr[i], v);
  862. }
  863. }
  864. }
  865. static void dib0090_set_boost(struct dib0090_state *state, int onoff)
  866. {
  867. state->bb_1_def &= 0xdfff;
  868. state->bb_1_def |= onoff << 13;
  869. }
  870. static void dib0090_set_rframp(struct dib0090_state *state, const u16 * cfg)
  871. {
  872. state->rf_ramp = cfg;
  873. }
  874. static void dib0090_set_rframp_pwm(struct dib0090_state *state, const u16 * cfg)
  875. {
  876. state->rf_ramp = cfg;
  877. dib0090_write_reg(state, 0x2a, 0xffff);
  878. dprintk("total RF gain: %ddB, step: %d", (u32) cfg[0], dib0090_read_reg(state, 0x2a));
  879. dib0090_write_regs(state, 0x2c, cfg + 3, 6);
  880. dib0090_write_regs(state, 0x3e, cfg + 9, 2);
  881. }
  882. static void dib0090_set_bbramp(struct dib0090_state *state, const u16 * cfg)
  883. {
  884. state->bb_ramp = cfg;
  885. dib0090_set_boost(state, cfg[0] > 500); /* we want the boost if the gain is higher that 50dB */
  886. }
  887. static void dib0090_set_bbramp_pwm(struct dib0090_state *state, const u16 * cfg)
  888. {
  889. state->bb_ramp = cfg;
  890. dib0090_set_boost(state, cfg[0] > 500); /* we want the boost if the gain is higher that 50dB */
  891. dib0090_write_reg(state, 0x33, 0xffff);
  892. dprintk("total BB gain: %ddB, step: %d", (u32) cfg[0], dib0090_read_reg(state, 0x33));
  893. dib0090_write_regs(state, 0x35, cfg + 3, 4);
  894. }
  895. void dib0090_pwm_gain_reset(struct dvb_frontend *fe)
  896. {
  897. struct dib0090_state *state = fe->tuner_priv;
  898. /* reset the AGC */
  899. if (state->config->use_pwm_agc) {
  900. #ifdef CONFIG_BAND_SBAND
  901. if (state->current_band == BAND_SBAND) {
  902. dib0090_set_rframp_pwm(state, rf_ramp_pwm_sband);
  903. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_boost);
  904. } else
  905. #endif
  906. #ifdef CONFIG_BAND_CBAND
  907. if (state->current_band == BAND_CBAND) {
  908. if (state->identity.in_soc) {
  909. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal_socs);
  910. if (state->identity.version == SOC_8090_P1G_11R1 || state->identity.version == SOC_8090_P1G_21R1)
  911. dib0090_set_rframp_pwm(state, rf_ramp_pwm_cband_8090);
  912. else if (state->identity.version == SOC_7090_P1G_11R1 || state->identity.version == SOC_7090_P1G_21R1)
  913. dib0090_set_rframp_pwm(state, rf_ramp_pwm_cband_7090);
  914. } else {
  915. dib0090_set_rframp_pwm(state, rf_ramp_pwm_cband);
  916. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal);
  917. }
  918. } else
  919. #endif
  920. #ifdef CONFIG_BAND_VHF
  921. if (state->current_band == BAND_VHF) {
  922. if (state->identity.in_soc) {
  923. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal_socs);
  924. } else {
  925. dib0090_set_rframp_pwm(state, rf_ramp_pwm_vhf);
  926. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal);
  927. }
  928. } else
  929. #endif
  930. {
  931. if (state->identity.in_soc) {
  932. if (state->identity.version == SOC_8090_P1G_11R1 || state->identity.version == SOC_8090_P1G_21R1)
  933. dib0090_set_rframp_pwm(state, rf_ramp_pwm_uhf_8090);
  934. else if (state->identity.version == SOC_7090_P1G_11R1 || state->identity.version == SOC_7090_P1G_21R1)
  935. dib0090_set_rframp_pwm(state, rf_ramp_pwm_uhf_7090);
  936. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal_socs);
  937. } else {
  938. dib0090_set_rframp_pwm(state, rf_ramp_pwm_uhf);
  939. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal);
  940. }
  941. }
  942. if (state->rf_ramp[0] != 0)
  943. dib0090_write_reg(state, 0x32, (3 << 11));
  944. else
  945. dib0090_write_reg(state, 0x32, (0 << 11));
  946. dib0090_write_reg(state, 0x04, 0x01);
  947. dib0090_write_reg(state, 0x39, (1 << 10));
  948. }
  949. }
  950. EXPORT_SYMBOL(dib0090_pwm_gain_reset);
  951. static u32 dib0090_get_slow_adc_val(struct dib0090_state *state)
  952. {
  953. u16 adc_val = dib0090_read_reg(state, 0x1d);
  954. if (state->identity.in_soc)
  955. adc_val >>= 2;
  956. return adc_val;
  957. }
  958. int dib0090_gain_control(struct dvb_frontend *fe)
  959. {
  960. struct dib0090_state *state = fe->tuner_priv;
  961. enum frontend_tune_state *tune_state = &state->tune_state;
  962. int ret = 10;
  963. u16 wbd_val = 0;
  964. u8 apply_gain_immediatly = 1;
  965. s16 wbd_error = 0, adc_error = 0;
  966. if (*tune_state == CT_AGC_START) {
  967. state->agc_freeze = 0;
  968. dib0090_write_reg(state, 0x04, 0x0);
  969. #ifdef CONFIG_BAND_SBAND
  970. if (state->current_band == BAND_SBAND) {
  971. dib0090_set_rframp(state, rf_ramp_sband);
  972. dib0090_set_bbramp(state, bb_ramp_boost);
  973. } else
  974. #endif
  975. #ifdef CONFIG_BAND_VHF
  976. if (state->current_band == BAND_VHF && !state->identity.p1g) {
  977. dib0090_set_rframp(state, rf_ramp_vhf);
  978. dib0090_set_bbramp(state, bb_ramp_boost);
  979. } else
  980. #endif
  981. #ifdef CONFIG_BAND_CBAND
  982. if (state->current_band == BAND_CBAND && !state->identity.p1g) {
  983. dib0090_set_rframp(state, rf_ramp_cband);
  984. dib0090_set_bbramp(state, bb_ramp_boost);
  985. } else
  986. #endif
  987. if ((state->current_band == BAND_CBAND || state->current_band == BAND_VHF) && state->identity.p1g) {
  988. dib0090_set_rframp(state, rf_ramp_cband_broadmatching);
  989. dib0090_set_bbramp(state, bb_ramp_boost);
  990. } else {
  991. dib0090_set_rframp(state, rf_ramp_uhf);
  992. dib0090_set_bbramp(state, bb_ramp_boost);
  993. }
  994. dib0090_write_reg(state, 0x32, 0);
  995. dib0090_write_reg(state, 0x39, 0);
  996. dib0090_wbd_target(state, state->current_rf);
  997. state->rf_gain_limit = state->rf_ramp[0] << WBD_ALPHA;
  998. state->current_gain = ((state->rf_ramp[0] + state->bb_ramp[0]) / 2) << GAIN_ALPHA;
  999. *tune_state = CT_AGC_STEP_0;
  1000. } else if (!state->agc_freeze) {
  1001. s16 wbd = 0, i, cnt;
  1002. int adc;
  1003. wbd_val = dib0090_get_slow_adc_val(state);
  1004. if (*tune_state == CT_AGC_STEP_0)
  1005. cnt = 5;
  1006. else
  1007. cnt = 1;
  1008. for (i = 0; i < cnt; i++) {
  1009. wbd_val = dib0090_get_slow_adc_val(state);
  1010. wbd += dib0090_wbd_to_db(state, wbd_val);
  1011. }
  1012. wbd /= cnt;
  1013. wbd_error = state->wbd_target - wbd;
  1014. if (*tune_state == CT_AGC_STEP_0) {
  1015. if (wbd_error < 0 && state->rf_gain_limit > 0 && !state->identity.p1g) {
  1016. #ifdef CONFIG_BAND_CBAND
  1017. /* in case of CBAND tune reduce first the lt_gain2 before adjusting the RF gain */
  1018. u8 ltg2 = (state->rf_lt_def >> 10) & 0x7;
  1019. if (state->current_band == BAND_CBAND && ltg2) {
  1020. ltg2 >>= 1;
  1021. state->rf_lt_def &= ltg2 << 10; /* reduce in 3 steps from 7 to 0 */
  1022. }
  1023. #endif
  1024. } else {
  1025. state->agc_step = 0;
  1026. *tune_state = CT_AGC_STEP_1;
  1027. }
  1028. } else {
  1029. /* calc the adc power */
  1030. adc = state->config->get_adc_power(fe);
  1031. adc = (adc * ((s32) 355774) + (((s32) 1) << 20)) >> 21; /* included in [0:-700] */
  1032. adc_error = (s16) (((s32) ADC_TARGET) - adc);
  1033. #ifdef CONFIG_STANDARD_DAB
  1034. if (state->fe->dtv_property_cache.delivery_system == STANDARD_DAB)
  1035. adc_error -= 10;
  1036. #endif
  1037. #ifdef CONFIG_STANDARD_DVBT
  1038. if (state->fe->dtv_property_cache.delivery_system == STANDARD_DVBT &&
  1039. (state->fe->dtv_property_cache.modulation == QAM_64 || state->fe->dtv_property_cache.modulation == QAM_16))
  1040. adc_error += 60;
  1041. #endif
  1042. #ifdef CONFIG_SYS_ISDBT
  1043. if ((state->fe->dtv_property_cache.delivery_system == SYS_ISDBT) && (((state->fe->dtv_property_cache.layer[0].segment_count >
  1044. 0)
  1045. &&
  1046. ((state->fe->dtv_property_cache.layer[0].modulation ==
  1047. QAM_64)
  1048. || (state->fe->dtv_property_cache.
  1049. layer[0].modulation == QAM_16)))
  1050. ||
  1051. ((state->fe->dtv_property_cache.layer[1].segment_count >
  1052. 0)
  1053. &&
  1054. ((state->fe->dtv_property_cache.layer[1].modulation ==
  1055. QAM_64)
  1056. || (state->fe->dtv_property_cache.
  1057. layer[1].modulation == QAM_16)))
  1058. ||
  1059. ((state->fe->dtv_property_cache.layer[2].segment_count >
  1060. 0)
  1061. &&
  1062. ((state->fe->dtv_property_cache.layer[2].modulation ==
  1063. QAM_64)
  1064. || (state->fe->dtv_property_cache.
  1065. layer[2].modulation == QAM_16)))
  1066. )
  1067. )
  1068. adc_error += 60;
  1069. #endif
  1070. if (*tune_state == CT_AGC_STEP_1) { /* quickly go to the correct range of the ADC power */
  1071. if (ABS(adc_error) < 50 || state->agc_step++ > 5) {
  1072. #ifdef CONFIG_STANDARD_DAB
  1073. if (state->fe->dtv_property_cache.delivery_system == STANDARD_DAB) {
  1074. dib0090_write_reg(state, 0x02, (1 << 15) | (15 << 11) | (31 << 6) | (63)); /* cap value = 63 : narrow BB filter : Fc = 1.8MHz */
  1075. dib0090_write_reg(state, 0x04, 0x0);
  1076. } else
  1077. #endif
  1078. {
  1079. dib0090_write_reg(state, 0x02, (1 << 15) | (3 << 11) | (6 << 6) | (32));
  1080. dib0090_write_reg(state, 0x04, 0x01); /*0 = 1KHz ; 1 = 150Hz ; 2 = 50Hz ; 3 = 50KHz ; 4 = servo fast */
  1081. }
  1082. *tune_state = CT_AGC_STOP;
  1083. }
  1084. } else {
  1085. /* everything higher than or equal to CT_AGC_STOP means tracking */
  1086. ret = 100; /* 10ms interval */
  1087. apply_gain_immediatly = 0;
  1088. }
  1089. }
  1090. #ifdef DEBUG_AGC
  1091. dprintk
  1092. ("tune state %d, ADC = %3ddB (ADC err %3d) WBD %3ddB (WBD err %3d, WBD val SADC: %4d), RFGainLimit (TOP): %3d, signal: %3ddBm",
  1093. (u32) *tune_state, (u32) adc, (u32) adc_error, (u32) wbd, (u32) wbd_error, (u32) wbd_val,
  1094. (u32) state->rf_gain_limit >> WBD_ALPHA, (s32) 200 + adc - (state->current_gain >> GAIN_ALPHA));
  1095. #endif
  1096. }
  1097. /* apply gain */
  1098. if (!state->agc_freeze)
  1099. dib0090_gain_apply(state, adc_error, wbd_error, apply_gain_immediatly);
  1100. return ret;
  1101. }
  1102. EXPORT_SYMBOL(dib0090_gain_control);
  1103. void dib0090_get_current_gain(struct dvb_frontend *fe, u16 * rf, u16 * bb, u16 * rf_gain_limit, u16 * rflt)
  1104. {
  1105. struct dib0090_state *state = fe->tuner_priv;
  1106. if (rf)
  1107. *rf = state->gain[0];
  1108. if (bb)
  1109. *bb = state->gain[1];
  1110. if (rf_gain_limit)
  1111. *rf_gain_limit = state->rf_gain_limit;
  1112. if (rflt)
  1113. *rflt = (state->rf_lt_def >> 10) & 0x7;
  1114. }
  1115. EXPORT_SYMBOL(dib0090_get_current_gain);
  1116. u16 dib0090_get_wbd_offset(struct dvb_frontend *fe)
  1117. {
  1118. struct dib0090_state *state = fe->tuner_priv;
  1119. u32 f_MHz = state->fe->dtv_property_cache.frequency / 1000000;
  1120. s32 current_temp = state->temperature;
  1121. s32 wbd_thot, wbd_tcold;
  1122. const struct dib0090_wbd_slope *wbd = state->current_wbd_table;
  1123. while (f_MHz > wbd->max_freq)
  1124. wbd++;
  1125. dprintk("using wbd-table-entry with max freq %d", wbd->max_freq);
  1126. if (current_temp < 0)
  1127. current_temp = 0;
  1128. if (current_temp > 128)
  1129. current_temp = 128;
  1130. state->wbdmux &= ~(7 << 13);
  1131. if (wbd->wbd_gain != 0)
  1132. state->wbdmux |= (wbd->wbd_gain << 13);
  1133. else
  1134. state->wbdmux |= (4 << 13);
  1135. dib0090_write_reg(state, 0x10, state->wbdmux);
  1136. wbd_thot = wbd->offset_hot - (((u32) wbd->slope_hot * f_MHz) >> 6);
  1137. wbd_tcold = wbd->offset_cold - (((u32) wbd->slope_cold * f_MHz) >> 6);
  1138. wbd_tcold += ((wbd_thot - wbd_tcold) * current_temp) >> 7;
  1139. state->wbd_target = dib0090_wbd_to_db(state, state->wbd_offset + wbd_tcold);
  1140. dprintk("wbd-target: %d dB", (u32) state->wbd_target);
  1141. dprintk("wbd offset applied is %d", wbd_tcold);
  1142. return state->wbd_offset + wbd_tcold;
  1143. }
  1144. EXPORT_SYMBOL(dib0090_get_wbd_offset);
  1145. static const u16 dib0090_defaults[] = {
  1146. 25, 0x01,
  1147. 0x0000,
  1148. 0x99a0,
  1149. 0x6008,
  1150. 0x0000,
  1151. 0x8bcb,
  1152. 0x0000,
  1153. 0x0405,
  1154. 0x0000,
  1155. 0x0000,
  1156. 0x0000,
  1157. 0xb802,
  1158. 0x0300,
  1159. 0x2d12,
  1160. 0xbac0,
  1161. 0x7c00,
  1162. 0xdbb9,
  1163. 0x0954,
  1164. 0x0743,
  1165. 0x8000,
  1166. 0x0001,
  1167. 0x0040,
  1168. 0x0100,
  1169. 0x0000,
  1170. 0xe910,
  1171. 0x149e,
  1172. 1, 0x1c,
  1173. 0xff2d,
  1174. 1, 0x39,
  1175. 0x0000,
  1176. 2, 0x1e,
  1177. 0x07FF,
  1178. 0x0007,
  1179. 1, 0x24,
  1180. EN_UHF | EN_CRYSTAL,
  1181. 2, 0x3c,
  1182. 0x3ff,
  1183. 0x111,
  1184. 0
  1185. };
  1186. static const u16 dib0090_p1g_additionnal_defaults[] = {
  1187. 1, 0x05,
  1188. 0xabcd,
  1189. 1, 0x11,
  1190. 0x00b4,
  1191. 1, 0x1c,
  1192. 0xfffd,
  1193. 1, 0x40,
  1194. 0x108,
  1195. 0
  1196. };
  1197. static void dib0090_set_default_config(struct dib0090_state *state, const u16 * n)
  1198. {
  1199. u16 l, r;
  1200. l = pgm_read_word(n++);
  1201. while (l) {
  1202. r = pgm_read_word(n++);
  1203. do {
  1204. dib0090_write_reg(state, r, pgm_read_word(n++));
  1205. r++;
  1206. } while (--l);
  1207. l = pgm_read_word(n++);
  1208. }
  1209. }
  1210. #define CAP_VALUE_MIN (u8) 9
  1211. #define CAP_VALUE_MAX (u8) 40
  1212. #define HR_MIN (u8) 25
  1213. #define HR_MAX (u8) 40
  1214. #define POLY_MIN (u8) 0
  1215. #define POLY_MAX (u8) 8
  1216. void dib0090_set_EFUSE(struct dib0090_state *state)
  1217. {
  1218. u8 c, h, n;
  1219. u16 e2, e4;
  1220. u16 cal;
  1221. e2 = dib0090_read_reg(state, 0x26);
  1222. e4 = dib0090_read_reg(state, 0x28);
  1223. if ((state->identity.version == P1D_E_F) ||
  1224. (state->identity.version == P1G) || (e2 == 0xffff)) {
  1225. dib0090_write_reg(state, 0x22, 0x10);
  1226. cal = (dib0090_read_reg(state, 0x22) >> 6) & 0x3ff;
  1227. if ((cal < 670) || (cal == 1023))
  1228. cal = 850;
  1229. n = 165 - ((cal * 10)>>6) ;
  1230. e2 = e4 = (3<<12) | (34<<6) | (n);
  1231. }
  1232. if (e2 != e4)
  1233. e2 &= e4; /* Remove the redundancy */
  1234. if (e2 != 0xffff) {
  1235. c = e2 & 0x3f;
  1236. n = (e2 >> 12) & 0xf;
  1237. h = (e2 >> 6) & 0x3f;
  1238. if ((c >= CAP_VALUE_MAX) || (c <= CAP_VALUE_MIN))
  1239. c = 32;
  1240. if ((h >= HR_MAX) || (h <= HR_MIN))
  1241. h = 34;
  1242. if ((n >= POLY_MAX) || (n <= POLY_MIN))
  1243. n = 3;
  1244. dib0090_write_reg(state, 0x13, (h << 10)) ;
  1245. e2 = (n<<11) | ((h>>2)<<6) | (c);
  1246. dib0090_write_reg(state, 0x2, e2) ; /* Load the BB_2 */
  1247. }
  1248. }
  1249. static int dib0090_reset(struct dvb_frontend *fe)
  1250. {
  1251. struct dib0090_state *state = fe->tuner_priv;
  1252. dib0090_reset_digital(fe, state->config);
  1253. if (dib0090_identify(fe) < 0)
  1254. return -EIO;
  1255. #ifdef CONFIG_TUNER_DIB0090_P1B_SUPPORT
  1256. if (!(state->identity.version & 0x1)) /* it is P1B - reset is already done */
  1257. return 0;
  1258. #endif
  1259. if (!state->identity.in_soc) {
  1260. if ((dib0090_read_reg(state, 0x1a) >> 5) & 0x2)
  1261. dib0090_write_reg(state, 0x1b, (EN_IQADC | EN_BB | EN_BIAS | EN_DIGCLK | EN_PLL | EN_CRYSTAL));
  1262. else
  1263. dib0090_write_reg(state, 0x1b, (EN_DIGCLK | EN_PLL | EN_CRYSTAL));
  1264. }
  1265. dib0090_set_default_config(state, dib0090_defaults);
  1266. if (state->identity.in_soc)
  1267. dib0090_write_reg(state, 0x18, 0x2910); /* charge pump current = 0 */
  1268. if (state->identity.p1g)
  1269. dib0090_set_default_config(state, dib0090_p1g_additionnal_defaults);
  1270. /* Update the efuse : Only available for KROSUS > P1C and SOC as well*/
  1271. if (((state->identity.version & 0x1f) >= P1D_E_F) || (state->identity.in_soc))
  1272. dib0090_set_EFUSE(state);
  1273. /* Congigure in function of the crystal */
  1274. if (state->config->io.clock_khz >= 24000)
  1275. dib0090_write_reg(state, 0x14, 1);
  1276. else
  1277. dib0090_write_reg(state, 0x14, 2);
  1278. dprintk("Pll lock : %d", (dib0090_read_reg(state, 0x1a) >> 11) & 0x1);
  1279. state->calibrate = DC_CAL | WBD_CAL | TEMP_CAL; /* enable iq-offset-calibration and wbd-calibration when tuning next time */
  1280. return 0;
  1281. }
  1282. #define steps(u) (((u) > 15) ? ((u)-16) : (u))
  1283. #define INTERN_WAIT 10
  1284. static int dib0090_get_offset(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1285. {
  1286. int ret = INTERN_WAIT * 10;
  1287. switch (*tune_state) {
  1288. case CT_TUNER_STEP_2:
  1289. /* Turns to positive */
  1290. dib0090_write_reg(state, 0x1f, 0x7);
  1291. *tune_state = CT_TUNER_STEP_3;
  1292. break;
  1293. case CT_TUNER_STEP_3:
  1294. state->adc_diff = dib0090_read_reg(state, 0x1d);
  1295. /* Turns to negative */
  1296. dib0090_write_reg(state, 0x1f, 0x4);
  1297. *tune_state = CT_TUNER_STEP_4;
  1298. break;
  1299. case CT_TUNER_STEP_4:
  1300. state->adc_diff -= dib0090_read_reg(state, 0x1d);
  1301. *tune_state = CT_TUNER_STEP_5;
  1302. ret = 0;
  1303. break;
  1304. default:
  1305. break;
  1306. }
  1307. return ret;
  1308. }
  1309. struct dc_calibration {
  1310. u8 addr;
  1311. u8 offset;
  1312. u8 pga:1;
  1313. u16 bb1;
  1314. u8 i:1;
  1315. };
  1316. static const struct dc_calibration dc_table[] = {
  1317. /* Step1 BB gain1= 26 with boost 1, gain 2 = 0 */
  1318. {0x06, 5, 1, (1 << 13) | (0 << 8) | (26 << 3), 1},
  1319. {0x07, 11, 1, (1 << 13) | (0 << 8) | (26 << 3), 0},
  1320. /* Step 2 BB gain 1 = 26 with boost = 1 & gain 2 = 29 */
  1321. {0x06, 0, 0, (1 << 13) | (29 << 8) | (26 << 3), 1},
  1322. {0x06, 10, 0, (1 << 13) | (29 << 8) | (26 << 3), 0},
  1323. {0},
  1324. };
  1325. static const struct dc_calibration dc_p1g_table[] = {
  1326. /* Step1 BB gain1= 26 with boost 1, gain 2 = 0 */
  1327. /* addr ; trim reg offset ; pga ; CTRL_BB1 value ; i or q */
  1328. {0x06, 5, 1, (1 << 13) | (0 << 8) | (15 << 3), 1},
  1329. {0x07, 11, 1, (1 << 13) | (0 << 8) | (15 << 3), 0},
  1330. /* Step 2 BB gain 1 = 26 with boost = 1 & gain 2 = 29 */
  1331. {0x06, 0, 0, (1 << 13) | (29 << 8) | (15 << 3), 1},
  1332. {0x06, 10, 0, (1 << 13) | (29 << 8) | (15 << 3), 0},
  1333. {0},
  1334. };
  1335. static void dib0090_set_trim(struct dib0090_state *state)
  1336. {
  1337. u16 *val;
  1338. if (state->dc->addr == 0x07)
  1339. val = &state->bb7;
  1340. else
  1341. val = &state->bb6;
  1342. *val &= ~(0x1f << state->dc->offset);
  1343. *val |= state->step << state->dc->offset;
  1344. dib0090_write_reg(state, state->dc->addr, *val);
  1345. }
  1346. static int dib0090_dc_offset_calibration(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1347. {
  1348. int ret = 0;
  1349. u16 reg;
  1350. switch (*tune_state) {
  1351. case CT_TUNER_START:
  1352. dprintk("Start DC offset calibration");
  1353. /* force vcm2 = 0.8V */
  1354. state->bb6 = 0;
  1355. state->bb7 = 0x040d;
  1356. /* the LNA AND LO are off */
  1357. reg = dib0090_read_reg(state, 0x24) & 0x0ffb; /* shutdown lna and lo */
  1358. dib0090_write_reg(state, 0x24, reg);
  1359. state->wbdmux = dib0090_read_reg(state, 0x10);
  1360. dib0090_write_reg(state, 0x10, (state->wbdmux & ~(0xff << 3)) | (0x7 << 3) | 0x3);
  1361. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) & ~(1 << 14));
  1362. state->dc = dc_table;
  1363. if (state->identity.p1g)
  1364. state->dc = dc_p1g_table;
  1365. *tune_state = CT_TUNER_STEP_0;
  1366. /* fall through */
  1367. case CT_TUNER_STEP_0:
  1368. dprintk("Sart/continue DC calibration for %s path", (state->dc->i == 1) ? "I" : "Q");
  1369. dib0090_write_reg(state, 0x01, state->dc->bb1);
  1370. dib0090_write_reg(state, 0x07, state->bb7 | (state->dc->i << 7));
  1371. state->step = 0;
  1372. state->min_adc_diff = 1023;
  1373. *tune_state = CT_TUNER_STEP_1;
  1374. ret = 50;
  1375. break;
  1376. case CT_TUNER_STEP_1:
  1377. dib0090_set_trim(state);
  1378. *tune_state = CT_TUNER_STEP_2;
  1379. break;
  1380. case CT_TUNER_STEP_2:
  1381. case CT_TUNER_STEP_3:
  1382. case CT_TUNER_STEP_4:
  1383. ret = dib0090_get_offset(state, tune_state);
  1384. break;
  1385. case CT_TUNER_STEP_5: /* found an offset */
  1386. dprintk("adc_diff = %d, current step= %d", (u32) state->adc_diff, state->step);
  1387. if (state->step == 0 && state->adc_diff < 0) {
  1388. state->min_adc_diff = -1023;
  1389. dprintk("Change of sign of the minimum adc diff");
  1390. }
  1391. dprintk("adc_diff = %d, min_adc_diff = %d current_step = %d", state->adc_diff, state->min_adc_diff, state->step);
  1392. /* first turn for this frequency */
  1393. if (state->step == 0) {
  1394. if (state->dc->pga && state->adc_diff < 0)
  1395. state->step = 0x10;
  1396. if (state->dc->pga == 0 && state->adc_diff > 0)
  1397. state->step = 0x10;
  1398. }
  1399. /* Look for a change of Sign in the Adc_diff.min_adc_diff is used to STORE the setp N-1 */
  1400. if ((state->adc_diff & 0x8000) == (state->min_adc_diff & 0x8000) && steps(state->step) < 15) {
  1401. /* stop search when the delta the sign is changing and Steps =15 and Step=0 is force for continuance */
  1402. state->step++;
  1403. state->min_adc_diff = state->adc_diff;
  1404. *tune_state = CT_TUNER_STEP_1;
  1405. } else {
  1406. /* the minimum was what we have seen in the step before */
  1407. if (ABS(state->adc_diff) > ABS(state->min_adc_diff)) {
  1408. dprintk("Since adc_diff N = %d > adc_diff step N-1 = %d, Come back one step", state->adc_diff, state->min_adc_diff);
  1409. state->step--;
  1410. }
  1411. dib0090_set_trim(state);
  1412. dprintk("BB Offset Cal, BBreg=%hd,Offset=%hd,Value Set=%hd", state->dc->addr, state->adc_diff, state->step);
  1413. state->dc++;
  1414. if (state->dc->addr == 0) /* done */
  1415. *tune_state = CT_TUNER_STEP_6;
  1416. else
  1417. *tune_state = CT_TUNER_STEP_0;
  1418. }
  1419. break;
  1420. case CT_TUNER_STEP_6:
  1421. dib0090_write_reg(state, 0x07, state->bb7 & ~0x0008);
  1422. dib0090_write_reg(state, 0x1f, 0x7);
  1423. *tune_state = CT_TUNER_START; /* reset done -> real tuning can now begin */
  1424. state->calibrate &= ~DC_CAL;
  1425. default:
  1426. break;
  1427. }
  1428. return ret;
  1429. }
  1430. static int dib0090_wbd_calibration(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1431. {
  1432. u8 wbd_gain;
  1433. const struct dib0090_wbd_slope *wbd = state->current_wbd_table;
  1434. switch (*tune_state) {
  1435. case CT_TUNER_START:
  1436. while (state->current_rf / 1000 > wbd->max_freq)
  1437. wbd++;
  1438. if (wbd->wbd_gain != 0)
  1439. wbd_gain = wbd->wbd_gain;
  1440. else {
  1441. wbd_gain = 4;
  1442. #if defined(CONFIG_BAND_LBAND) || defined(CONFIG_BAND_SBAND)
  1443. if ((state->current_band == BAND_LBAND) || (state->current_band == BAND_SBAND))
  1444. wbd_gain = 2;
  1445. #endif
  1446. }
  1447. if (wbd_gain == state->wbd_calibration_gain) { /* the WBD calibration has already been done */
  1448. *tune_state = CT_TUNER_START;
  1449. state->calibrate &= ~WBD_CAL;
  1450. return 0;
  1451. }
  1452. dib0090_write_reg(state, 0x10, 0x1b81 | (1 << 10) | (wbd_gain << 13) | (1 << 3));
  1453. dib0090_write_reg(state, 0x24, ((EN_UHF & 0x0fff) | (1 << 1)));
  1454. *tune_state = CT_TUNER_STEP_0;
  1455. state->wbd_calibration_gain = wbd_gain;
  1456. return 90; /* wait for the WBDMUX to switch and for the ADC to sample */
  1457. case CT_TUNER_STEP_0:
  1458. state->wbd_offset = dib0090_get_slow_adc_val(state);
  1459. dprintk("WBD calibration offset = %d", state->wbd_offset);
  1460. *tune_state = CT_TUNER_START; /* reset done -> real tuning can now begin */
  1461. state->calibrate &= ~WBD_CAL;
  1462. break;
  1463. default:
  1464. break;
  1465. }
  1466. return 0;
  1467. }
  1468. static void dib0090_set_bandwidth(struct dib0090_state *state)
  1469. {
  1470. u16 tmp;
  1471. if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 5000)
  1472. tmp = (3 << 14);
  1473. else if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 6000)
  1474. tmp = (2 << 14);
  1475. else if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 7000)
  1476. tmp = (1 << 14);
  1477. else
  1478. tmp = (0 << 14);
  1479. state->bb_1_def &= 0x3fff;
  1480. state->bb_1_def |= tmp;
  1481. dib0090_write_reg(state, 0x01, state->bb_1_def); /* be sure that we have the right bb-filter */
  1482. dib0090_write_reg(state, 0x03, 0x6008); /* = 0x6008 : vcm3_trim = 1 ; filter2_gm1_trim = 8 ; filter2_cutoff_freq = 0 */
  1483. dib0090_write_reg(state, 0x04, 0x1); /* 0 = 1KHz ; 1 = 50Hz ; 2 = 150Hz ; 3 = 50KHz ; 4 = servo fast */
  1484. if (state->identity.in_soc) {
  1485. dib0090_write_reg(state, 0x05, 0x9bcf); /* attenuator_ibias_tri = 2 ; input_stage_ibias_tr = 1 ; nc = 11 ; ext_gm_trim = 1 ; obuf_ibias_trim = 4 ; filter13_gm2_ibias_t = 15 */
  1486. } else {
  1487. dib0090_write_reg(state, 0x02, (5 << 11) | (8 << 6) | (22 & 0x3f)); /* 22 = cap_value */
  1488. dib0090_write_reg(state, 0x05, 0xabcd); /* = 0xabcd : attenuator_ibias_tri = 2 ; input_stage_ibias_tr = 2 ; nc = 11 ; ext_gm_trim = 1 ; obuf_ibias_trim = 4 ; filter13_gm2_ibias_t = 13 */
  1489. }
  1490. }
  1491. static const struct dib0090_pll dib0090_pll_table[] = {
  1492. #ifdef CONFIG_BAND_CBAND
  1493. {56000, 0, 9, 48, 6},
  1494. {70000, 1, 9, 48, 6},
  1495. {87000, 0, 8, 32, 4},
  1496. {105000, 1, 8, 32, 4},
  1497. {115000, 0, 7, 24, 6},
  1498. {140000, 1, 7, 24, 6},
  1499. {170000, 0, 6, 16, 4},
  1500. #endif
  1501. #ifdef CONFIG_BAND_VHF
  1502. {200000, 1, 6, 16, 4},
  1503. {230000, 0, 5, 12, 6},
  1504. {280000, 1, 5, 12, 6},
  1505. {340000, 0, 4, 8, 4},
  1506. {380000, 1, 4, 8, 4},
  1507. {450000, 0, 3, 6, 6},
  1508. #endif
  1509. #ifdef CONFIG_BAND_UHF
  1510. {580000, 1, 3, 6, 6},
  1511. {700000, 0, 2, 4, 4},
  1512. {860000, 1, 2, 4, 4},
  1513. #endif
  1514. #ifdef CONFIG_BAND_LBAND
  1515. {1800000, 1, 0, 2, 4},
  1516. #endif
  1517. #ifdef CONFIG_BAND_SBAND
  1518. {2900000, 0, 14, 1, 4},
  1519. #endif
  1520. };
  1521. static const struct dib0090_tuning dib0090_tuning_table_fm_vhf_on_cband[] = {
  1522. #ifdef CONFIG_BAND_CBAND
  1523. {184000, 4, 1, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1524. {227000, 4, 3, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1525. {380000, 4, 7, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1526. #endif
  1527. #ifdef CONFIG_BAND_UHF
  1528. {520000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1529. {550000, 2, 2, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1530. {650000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1531. {750000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1532. {850000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1533. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1534. #endif
  1535. #ifdef CONFIG_BAND_LBAND
  1536. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1537. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1538. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1539. #endif
  1540. #ifdef CONFIG_BAND_SBAND
  1541. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1542. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1543. #endif
  1544. };
  1545. static const struct dib0090_tuning dib0090_tuning_table[] = {
  1546. #ifdef CONFIG_BAND_CBAND
  1547. {170000, 4, 1, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1548. #endif
  1549. #ifdef CONFIG_BAND_VHF
  1550. {184000, 1, 1, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1551. {227000, 1, 3, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1552. {380000, 1, 7, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1553. #endif
  1554. #ifdef CONFIG_BAND_UHF
  1555. {520000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1556. {550000, 2, 2, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1557. {650000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1558. {750000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1559. {850000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1560. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1561. #endif
  1562. #ifdef CONFIG_BAND_LBAND
  1563. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1564. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1565. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1566. #endif
  1567. #ifdef CONFIG_BAND_SBAND
  1568. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1569. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1570. #endif
  1571. };
  1572. static const struct dib0090_tuning dib0090_p1g_tuning_table[] = {
  1573. #ifdef CONFIG_BAND_CBAND
  1574. {170000, 4, 1, 0x820f, 0x300, 0x2d22, 0x82cb, EN_CAB},
  1575. #endif
  1576. #ifdef CONFIG_BAND_VHF
  1577. {184000, 1, 1, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1578. {227000, 1, 3, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1579. {380000, 1, 7, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1580. #endif
  1581. #ifdef CONFIG_BAND_UHF
  1582. {510000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1583. {540000, 2, 1, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1584. {600000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1585. {630000, 2, 4, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1586. {680000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1587. {720000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1588. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1589. #endif
  1590. #ifdef CONFIG_BAND_LBAND
  1591. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1592. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1593. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1594. #endif
  1595. #ifdef CONFIG_BAND_SBAND
  1596. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1597. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1598. #endif
  1599. };
  1600. static const struct dib0090_pll dib0090_p1g_pll_table[] = {
  1601. #ifdef CONFIG_BAND_CBAND
  1602. {57000, 0, 11, 48, 6},
  1603. {70000, 1, 11, 48, 6},
  1604. {86000, 0, 10, 32, 4},
  1605. {105000, 1, 10, 32, 4},
  1606. {115000, 0, 9, 24, 6},
  1607. {140000, 1, 9, 24, 6},
  1608. {170000, 0, 8, 16, 4},
  1609. #endif
  1610. #ifdef CONFIG_BAND_VHF
  1611. {200000, 1, 8, 16, 4},
  1612. {230000, 0, 7, 12, 6},
  1613. {280000, 1, 7, 12, 6},
  1614. {340000, 0, 6, 8, 4},
  1615. {380000, 1, 6, 8, 4},
  1616. {455000, 0, 5, 6, 6},
  1617. #endif
  1618. #ifdef CONFIG_BAND_UHF
  1619. {580000, 1, 5, 6, 6},
  1620. {680000, 0, 4, 4, 4},
  1621. {860000, 1, 4, 4, 4},
  1622. #endif
  1623. #ifdef CONFIG_BAND_LBAND
  1624. {1800000, 1, 2, 2, 4},
  1625. #endif
  1626. #ifdef CONFIG_BAND_SBAND
  1627. {2900000, 0, 1, 1, 6},
  1628. #endif
  1629. };
  1630. static const struct dib0090_tuning dib0090_p1g_tuning_table_fm_vhf_on_cband[] = {
  1631. #ifdef CONFIG_BAND_CBAND
  1632. {184000, 4, 3, 0x4187, 0x2c0, 0x2d22, 0x81cb, EN_CAB},
  1633. {227000, 4, 3, 0x4187, 0x2c0, 0x2d22, 0x81cb, EN_CAB},
  1634. {380000, 4, 3, 0x4187, 0x2c0, 0x2d22, 0x81cb, EN_CAB},
  1635. #endif
  1636. #ifdef CONFIG_BAND_UHF
  1637. {520000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1638. {550000, 2, 2, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1639. {650000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1640. {750000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1641. {850000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1642. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1643. #endif
  1644. #ifdef CONFIG_BAND_LBAND
  1645. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1646. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1647. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1648. #endif
  1649. #ifdef CONFIG_BAND_SBAND
  1650. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1651. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1652. #endif
  1653. };
  1654. static const struct dib0090_tuning dib0090_tuning_table_cband_7090[] = {
  1655. #ifdef CONFIG_BAND_CBAND
  1656. {300000, 4, 3, 0x018F, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1657. {380000, 4, 10, 0x018F, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1658. {570000, 4, 10, 0x8190, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1659. {858000, 4, 5, 0x8190, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1660. #endif
  1661. };
  1662. static int dib0090_captrim_search(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1663. {
  1664. int ret = 0;
  1665. u16 lo4 = 0xe900;
  1666. s16 adc_target;
  1667. u16 adc;
  1668. s8 step_sign;
  1669. u8 force_soft_search = 0;
  1670. if (state->identity.version == SOC_8090_P1G_11R1 || state->identity.version == SOC_8090_P1G_21R1)
  1671. force_soft_search = 1;
  1672. if (*tune_state == CT_TUNER_START) {
  1673. dprintk("Start Captrim search : %s", (force_soft_search == 1) ? "FORCE SOFT SEARCH" : "AUTO");
  1674. dib0090_write_reg(state, 0x10, 0x2B1);
  1675. dib0090_write_reg(state, 0x1e, 0x0032);
  1676. if (!state->tuner_is_tuned) {
  1677. /* prepare a complete captrim */
  1678. if (!state->identity.p1g || force_soft_search)
  1679. state->step = state->captrim = state->fcaptrim = 64;
  1680. state->current_rf = state->rf_request;
  1681. } else { /* we are already tuned to this frequency - the configuration is correct */
  1682. if (!state->identity.p1g || force_soft_search) {
  1683. /* do a minimal captrim even if the frequency has not changed */
  1684. state->step = 4;
  1685. state->captrim = state->fcaptrim = dib0090_read_reg(state, 0x18) & 0x7f;
  1686. }
  1687. }
  1688. state->adc_diff = 3000;
  1689. *tune_state = CT_TUNER_STEP_0;
  1690. } else if (*tune_state == CT_TUNER_STEP_0) {
  1691. if (state->identity.p1g && !force_soft_search) {
  1692. u8 ratio = 31;
  1693. dib0090_write_reg(state, 0x40, (3 << 7) | (ratio << 2) | (1 << 1) | 1);
  1694. dib0090_read_reg(state, 0x40);
  1695. ret = 50;
  1696. } else {
  1697. state->step /= 2;
  1698. dib0090_write_reg(state, 0x18, lo4 | state->captrim);
  1699. if (state->identity.in_soc)
  1700. ret = 25;
  1701. }
  1702. *tune_state = CT_TUNER_STEP_1;
  1703. } else if (*tune_state == CT_TUNER_STEP_1) {
  1704. if (state->identity.p1g && !force_soft_search) {
  1705. dib0090_write_reg(state, 0x40, 0x18c | (0 << 1) | 0);
  1706. dib0090_read_reg(state, 0x40);
  1707. state->fcaptrim = dib0090_read_reg(state, 0x18) & 0x7F;
  1708. dprintk("***Final Captrim= 0x%x", state->fcaptrim);
  1709. *tune_state = CT_TUNER_STEP_3;
  1710. } else {
  1711. /* MERGE for all krosus before P1G */
  1712. adc = dib0090_get_slow_adc_val(state);
  1713. dprintk("CAPTRIM=%d; ADC = %d (ADC) & %dmV", (u32) state->captrim, (u32) adc, (u32) (adc) * (u32) 1800 / (u32) 1024);
  1714. if (state->rest == 0 || state->identity.in_soc) { /* Just for 8090P SOCS where auto captrim HW bug : TO CHECK IN ACI for SOCS !!! if 400 for 8090p SOC => tune issue !!! */
  1715. adc_target = 200;
  1716. } else
  1717. adc_target = 400;
  1718. if (adc >= adc_target) {
  1719. adc -= adc_target;
  1720. step_sign = -1;
  1721. } else {
  1722. adc = adc_target - adc;
  1723. step_sign = 1;
  1724. }
  1725. if (adc < state->adc_diff) {
  1726. dprintk("CAPTRIM=%d is closer to target (%d/%d)", (u32) state->captrim, (u32) adc, (u32) state->adc_diff);
  1727. state->adc_diff = adc;
  1728. state->fcaptrim = state->captrim;
  1729. }
  1730. state->captrim += step_sign * state->step;
  1731. if (state->step >= 1)
  1732. *tune_state = CT_TUNER_STEP_0;
  1733. else
  1734. *tune_state = CT_TUNER_STEP_2;
  1735. ret = 25;
  1736. }
  1737. } else if (*tune_state == CT_TUNER_STEP_2) { /* this step is only used by krosus < P1G */
  1738. /*write the final cptrim config */
  1739. dib0090_write_reg(state, 0x18, lo4 | state->fcaptrim);
  1740. *tune_state = CT_TUNER_STEP_3;
  1741. } else if (*tune_state == CT_TUNER_STEP_3) {
  1742. state->calibrate &= ~CAPTRIM_CAL;
  1743. *tune_state = CT_TUNER_STEP_0;
  1744. }
  1745. return ret;
  1746. }
  1747. static int dib0090_get_temperature(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1748. {
  1749. int ret = 15;
  1750. s16 val;
  1751. switch (*tune_state) {
  1752. case CT_TUNER_START:
  1753. state->wbdmux = dib0090_read_reg(state, 0x10);
  1754. dib0090_write_reg(state, 0x10, (state->wbdmux & ~(0xff << 3)) | (0x8 << 3));
  1755. state->bias = dib0090_read_reg(state, 0x13);
  1756. dib0090_write_reg(state, 0x13, state->bias | (0x3 << 8));
  1757. *tune_state = CT_TUNER_STEP_0;
  1758. /* wait for the WBDMUX to switch and for the ADC to sample */
  1759. break;
  1760. case CT_TUNER_STEP_0:
  1761. state->adc_diff = dib0090_get_slow_adc_val(state);
  1762. dib0090_write_reg(state, 0x13, (state->bias & ~(0x3 << 8)) | (0x2 << 8));
  1763. *tune_state = CT_TUNER_STEP_1;
  1764. break;
  1765. case CT_TUNER_STEP_1:
  1766. val = dib0090_get_slow_adc_val(state);
  1767. state->temperature = ((s16) ((val - state->adc_diff) * 180) >> 8) + 55;
  1768. dprintk("temperature: %d C", state->temperature - 30);
  1769. *tune_state = CT_TUNER_STEP_2;
  1770. break;
  1771. case CT_TUNER_STEP_2:
  1772. dib0090_write_reg(state, 0x13, state->bias);
  1773. dib0090_write_reg(state, 0x10, state->wbdmux); /* write back original WBDMUX */
  1774. *tune_state = CT_TUNER_START;
  1775. state->calibrate &= ~TEMP_CAL;
  1776. if (state->config->analog_output == 0)
  1777. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) | (1 << 14));
  1778. break;
  1779. default:
  1780. ret = 0;
  1781. break;
  1782. }
  1783. return ret;
  1784. }
  1785. #define WBD 0x781 /* 1 1 1 1 0000 0 0 1 */
  1786. static int dib0090_tune(struct dvb_frontend *fe)
  1787. {
  1788. struct dib0090_state *state = fe->tuner_priv;
  1789. const struct dib0090_tuning *tune = state->current_tune_table_index;
  1790. const struct dib0090_pll *pll = state->current_pll_table_index;
  1791. enum frontend_tune_state *tune_state = &state->tune_state;
  1792. u16 lo5, lo6, Den, tmp;
  1793. u32 FBDiv, Rest, FREF, VCOF_kHz = 0;
  1794. int ret = 10; /* 1ms is the default delay most of the time */
  1795. u8 c, i;
  1796. /************************* VCO ***************************/
  1797. /* Default values for FG */
  1798. /* from these are needed : */
  1799. /* Cp,HFdiv,VCOband,SD,Num,Den,FB and REFDiv */
  1800. /* in any case we first need to do a calibration if needed */
  1801. if (*tune_state == CT_TUNER_START) {
  1802. /* deactivate DataTX before some calibrations */
  1803. if (state->calibrate & (DC_CAL | TEMP_CAL | WBD_CAL))
  1804. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) & ~(1 << 14));
  1805. else
  1806. /* Activate DataTX in case a calibration has been done before */
  1807. if (state->config->analog_output == 0)
  1808. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) | (1 << 14));
  1809. }
  1810. if (state->calibrate & DC_CAL)
  1811. return dib0090_dc_offset_calibration(state, tune_state);
  1812. else if (state->calibrate & WBD_CAL) {
  1813. if (state->current_rf == 0)
  1814. state->current_rf = state->fe->dtv_property_cache.frequency / 1000;
  1815. return dib0090_wbd_calibration(state, tune_state);
  1816. } else if (state->calibrate & TEMP_CAL)
  1817. return dib0090_get_temperature(state, tune_state);
  1818. else if (state->calibrate & CAPTRIM_CAL)
  1819. return dib0090_captrim_search(state, tune_state);
  1820. if (*tune_state == CT_TUNER_START) {
  1821. /* if soc and AGC pwm control, disengage mux to be able to R/W access to 0x01 register to set the right filter (cutoff_freq_select) during the tune sequence, otherwise, SOC SERPAR error when accessing to 0x01 */
  1822. if (state->config->use_pwm_agc && state->identity.in_soc) {
  1823. tmp = dib0090_read_reg(state, 0x39);
  1824. if ((tmp >> 10) & 0x1)
  1825. dib0090_write_reg(state, 0x39, tmp & ~(1 << 10));
  1826. }
  1827. state->current_band = (u8) BAND_OF_FREQUENCY(state->fe->dtv_property_cache.frequency / 1000);
  1828. state->rf_request =
  1829. state->fe->dtv_property_cache.frequency / 1000 + (state->current_band ==
  1830. BAND_UHF ? state->config->freq_offset_khz_uhf : state->config->
  1831. freq_offset_khz_vhf);
  1832. /* in ISDB-T 1seg we shift tuning frequency */
  1833. if ((state->fe->dtv_property_cache.delivery_system == SYS_ISDBT && state->fe->dtv_property_cache.isdbt_sb_mode == 1
  1834. && state->fe->dtv_property_cache.isdbt_partial_reception == 0)) {
  1835. const struct dib0090_low_if_offset_table *LUT_offset = state->config->low_if;
  1836. u8 found_offset = 0;
  1837. u32 margin_khz = 100;
  1838. if (LUT_offset != NULL) {
  1839. while (LUT_offset->RF_freq != 0xffff) {
  1840. if (((state->rf_request > (LUT_offset->RF_freq - margin_khz))
  1841. && (state->rf_request < (LUT_offset->RF_freq + margin_khz)))
  1842. && LUT_offset->std == state->fe->dtv_property_cache.delivery_system) {
  1843. state->rf_request += LUT_offset->offset_khz;
  1844. found_offset = 1;
  1845. break;
  1846. }
  1847. LUT_offset++;
  1848. }
  1849. }
  1850. if (found_offset == 0)
  1851. state->rf_request += 400;
  1852. }
  1853. if (state->current_rf != state->rf_request || (state->current_standard != state->fe->dtv_property_cache.delivery_system)) {
  1854. state->tuner_is_tuned = 0;
  1855. state->current_rf = 0;
  1856. state->current_standard = 0;
  1857. tune = dib0090_tuning_table;
  1858. if (state->identity.p1g)
  1859. tune = dib0090_p1g_tuning_table;
  1860. tmp = (state->identity.version >> 5) & 0x7;
  1861. if (state->identity.in_soc) {
  1862. if (state->config->force_cband_input) { /* Use the CBAND input for all band */
  1863. if (state->current_band & BAND_CBAND || state->current_band & BAND_FM || state->current_band & BAND_VHF
  1864. || state->current_band & BAND_UHF) {
  1865. state->current_band = BAND_CBAND;
  1866. tune = dib0090_tuning_table_cband_7090;
  1867. }
  1868. } else { /* Use the CBAND input for all band under UHF */
  1869. if (state->current_band & BAND_CBAND || state->current_band & BAND_FM || state->current_band & BAND_VHF) {
  1870. state->current_band = BAND_CBAND;
  1871. tune = dib0090_tuning_table_cband_7090;
  1872. }
  1873. }
  1874. } else
  1875. if (tmp == 0x4 || tmp == 0x7) {
  1876. /* CBAND tuner version for VHF */
  1877. if (state->current_band == BAND_FM || state->current_band == BAND_CBAND || state->current_band == BAND_VHF) {
  1878. state->current_band = BAND_CBAND; /* Force CBAND */
  1879. tune = dib0090_tuning_table_fm_vhf_on_cband;
  1880. if (state->identity.p1g)
  1881. tune = dib0090_p1g_tuning_table_fm_vhf_on_cband;
  1882. }
  1883. }
  1884. pll = dib0090_pll_table;
  1885. if (state->identity.p1g)
  1886. pll = dib0090_p1g_pll_table;
  1887. /* Look for the interval */
  1888. while (state->rf_request > tune->max_freq)
  1889. tune++;
  1890. while (state->rf_request > pll->max_freq)
  1891. pll++;
  1892. state->current_tune_table_index = tune;
  1893. state->current_pll_table_index = pll;
  1894. dib0090_write_reg(state, 0x0b, 0xb800 | (tune->switch_trim));
  1895. VCOF_kHz = (pll->hfdiv * state->rf_request) * 2;
  1896. FREF = state->config->io.clock_khz;
  1897. if (state->config->fref_clock_ratio != 0)
  1898. FREF /= state->config->fref_clock_ratio;
  1899. FBDiv = (VCOF_kHz / pll->topresc / FREF);
  1900. Rest = (VCOF_kHz / pll->topresc) - FBDiv * FREF;
  1901. if (Rest < LPF)
  1902. Rest = 0;
  1903. else if (Rest < 2 * LPF)
  1904. Rest = 2 * LPF;
  1905. else if (Rest > (FREF - LPF)) {
  1906. Rest = 0;
  1907. FBDiv += 1;
  1908. } else if (Rest > (FREF - 2 * LPF))
  1909. Rest = FREF - 2 * LPF;
  1910. Rest = (Rest * 6528) / (FREF / 10);
  1911. state->rest = Rest;
  1912. /* external loop filter, otherwise:
  1913. * lo5 = (0 << 15) | (0 << 12) | (0 << 11) | (3 << 9) | (4 << 6) | (3 << 4) | 4;
  1914. * lo6 = 0x0e34 */
  1915. if (Rest == 0) {
  1916. if (pll->vco_band)
  1917. lo5 = 0x049f;
  1918. else
  1919. lo5 = 0x041f;
  1920. } else {
  1921. if (pll->vco_band)
  1922. lo5 = 0x049e;
  1923. else if (state->config->analog_output)
  1924. lo5 = 0x041d;
  1925. else
  1926. lo5 = 0x041c;
  1927. }
  1928. if (state->identity.p1g) { /* Bias is done automatically in P1G */
  1929. if (state->identity.in_soc) {
  1930. if (state->identity.version == SOC_8090_P1G_11R1)
  1931. lo5 = 0x46f;
  1932. else
  1933. lo5 = 0x42f;
  1934. } else
  1935. lo5 = 0x42c;
  1936. }
  1937. lo5 |= (pll->hfdiv_code << 11) | (pll->vco_band << 7); /* bit 15 is the split to the slave, we do not do it here */
  1938. if (!state->config->io.pll_int_loop_filt) {
  1939. if (state->identity.in_soc)
  1940. lo6 = 0xff98;
  1941. else if (state->identity.p1g || (Rest == 0))
  1942. lo6 = 0xfff8;
  1943. else
  1944. lo6 = 0xff28;
  1945. } else
  1946. lo6 = (state->config->io.pll_int_loop_filt << 3);
  1947. Den = 1;
  1948. if (Rest > 0) {
  1949. if (state->config->analog_output)
  1950. lo6 |= (1 << 2) | 2;
  1951. else {
  1952. if (state->identity.in_soc)
  1953. lo6 |= (1 << 2) | 2;
  1954. else
  1955. lo6 |= (1 << 2) | 2;
  1956. }
  1957. Den = 255;
  1958. }
  1959. dib0090_write_reg(state, 0x15, (u16) FBDiv);
  1960. if (state->config->fref_clock_ratio != 0)
  1961. dib0090_write_reg(state, 0x16, (Den << 8) | state->config->fref_clock_ratio);
  1962. else
  1963. dib0090_write_reg(state, 0x16, (Den << 8) | 1);
  1964. dib0090_write_reg(state, 0x17, (u16) Rest);
  1965. dib0090_write_reg(state, 0x19, lo5);
  1966. dib0090_write_reg(state, 0x1c, lo6);
  1967. lo6 = tune->tuner_enable;
  1968. if (state->config->analog_output)
  1969. lo6 = (lo6 & 0xff9f) | 0x2;
  1970. dib0090_write_reg(state, 0x24, lo6 | EN_LO | state->config->use_pwm_agc * EN_CRYSTAL);
  1971. }
  1972. state->current_rf = state->rf_request;
  1973. state->current_standard = state->fe->dtv_property_cache.delivery_system;
  1974. ret = 20;
  1975. state->calibrate = CAPTRIM_CAL; /* captrim serach now */
  1976. }
  1977. else if (*tune_state == CT_TUNER_STEP_0) { /* Warning : because of captrim cal, if you change this step, change it also in _cal.c file because it is the step following captrim cal state machine */
  1978. const struct dib0090_wbd_slope *wbd = state->current_wbd_table;
  1979. while (state->current_rf / 1000 > wbd->max_freq)
  1980. wbd++;
  1981. dib0090_write_reg(state, 0x1e, 0x07ff);
  1982. dprintk("Final Captrim: %d", (u32) state->fcaptrim);
  1983. dprintk("HFDIV code: %d", (u32) pll->hfdiv_code);
  1984. dprintk("VCO = %d", (u32) pll->vco_band);
  1985. dprintk("VCOF in kHz: %d ((%d*%d) << 1))", (u32) ((pll->hfdiv * state->rf_request) * 2), (u32) pll->hfdiv, (u32) state->rf_request);
  1986. dprintk("REFDIV: %d, FREF: %d", (u32) 1, (u32) state->config->io.clock_khz);
  1987. dprintk("FBDIV: %d, Rest: %d", (u32) dib0090_read_reg(state, 0x15), (u32) dib0090_read_reg(state, 0x17));
  1988. dprintk("Num: %d, Den: %d, SD: %d", (u32) dib0090_read_reg(state, 0x17), (u32) (dib0090_read_reg(state, 0x16) >> 8),
  1989. (u32) dib0090_read_reg(state, 0x1c) & 0x3);
  1990. #define WBD 0x781 /* 1 1 1 1 0000 0 0 1 */
  1991. c = 4;
  1992. i = 3;
  1993. if (wbd->wbd_gain != 0)
  1994. c = wbd->wbd_gain;
  1995. state->wbdmux = (c << 13) | (i << 11) | (WBD | (state->config->use_pwm_agc << 1));
  1996. dib0090_write_reg(state, 0x10, state->wbdmux);
  1997. if ((tune->tuner_enable == EN_CAB) && state->identity.p1g) {
  1998. dprintk("P1G : The cable band is selected and lna_tune = %d", tune->lna_tune);
  1999. dib0090_write_reg(state, 0x09, tune->lna_bias);
  2000. dib0090_write_reg(state, 0x0b, 0xb800 | (tune->lna_tune << 6) | (tune->switch_trim));
  2001. } else
  2002. dib0090_write_reg(state, 0x09, (tune->lna_tune << 5) | tune->lna_bias);
  2003. dib0090_write_reg(state, 0x0c, tune->v2i);
  2004. dib0090_write_reg(state, 0x0d, tune->mix);
  2005. dib0090_write_reg(state, 0x0e, tune->load);
  2006. *tune_state = CT_TUNER_STEP_1;
  2007. } else if (*tune_state == CT_TUNER_STEP_1) {
  2008. /* initialize the lt gain register */
  2009. state->rf_lt_def = 0x7c00;
  2010. dib0090_set_bandwidth(state);
  2011. state->tuner_is_tuned = 1;
  2012. state->calibrate |= WBD_CAL;
  2013. state->calibrate |= TEMP_CAL;
  2014. *tune_state = CT_TUNER_STOP;
  2015. } else
  2016. ret = FE_CALLBACK_TIME_NEVER;
  2017. return ret;
  2018. }
  2019. static int dib0090_release(struct dvb_frontend *fe)
  2020. {
  2021. kfree(fe->tuner_priv);
  2022. fe->tuner_priv = NULL;
  2023. return 0;
  2024. }
  2025. enum frontend_tune_state dib0090_get_tune_state(struct dvb_frontend *fe)
  2026. {
  2027. struct dib0090_state *state = fe->tuner_priv;
  2028. return state->tune_state;
  2029. }
  2030. EXPORT_SYMBOL(dib0090_get_tune_state);
  2031. int dib0090_set_tune_state(struct dvb_frontend *fe, enum frontend_tune_state tune_state)
  2032. {
  2033. struct dib0090_state *state = fe->tuner_priv;
  2034. state->tune_state = tune_state;
  2035. return 0;
  2036. }
  2037. EXPORT_SYMBOL(dib0090_set_tune_state);
  2038. static int dib0090_get_frequency(struct dvb_frontend *fe, u32 * frequency)
  2039. {
  2040. struct dib0090_state *state = fe->tuner_priv;
  2041. *frequency = 1000 * state->current_rf;
  2042. return 0;
  2043. }
  2044. static int dib0090_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
  2045. {
  2046. struct dib0090_state *state = fe->tuner_priv;
  2047. u32 ret;
  2048. state->tune_state = CT_TUNER_START;
  2049. do {
  2050. ret = dib0090_tune(fe);
  2051. if (ret != FE_CALLBACK_TIME_NEVER)
  2052. msleep(ret / 10);
  2053. else
  2054. break;
  2055. } while (state->tune_state != CT_TUNER_STOP);
  2056. return 0;
  2057. }
  2058. static const struct dvb_tuner_ops dib0090_ops = {
  2059. .info = {
  2060. .name = "DiBcom DiB0090",
  2061. .frequency_min = 45000000,
  2062. .frequency_max = 860000000,
  2063. .frequency_step = 1000,
  2064. },
  2065. .release = dib0090_release,
  2066. .init = dib0090_wakeup,
  2067. .sleep = dib0090_sleep,
  2068. .set_params = dib0090_set_params,
  2069. .get_frequency = dib0090_get_frequency,
  2070. };
  2071. static const struct dvb_tuner_ops dib0090_fw_ops = {
  2072. .info = {
  2073. .name = "DiBcom DiB0090",
  2074. .frequency_min = 45000000,
  2075. .frequency_max = 860000000,
  2076. .frequency_step = 1000,
  2077. },
  2078. .release = dib0090_release,
  2079. .init = NULL,
  2080. .sleep = NULL,
  2081. .set_params = NULL,
  2082. .get_frequency = NULL,
  2083. };
  2084. static const struct dib0090_wbd_slope dib0090_wbd_table_default[] = {
  2085. {470, 0, 250, 0, 100, 4},
  2086. {860, 51, 866, 21, 375, 4},
  2087. {1700, 0, 800, 0, 850, 4},
  2088. {2900, 0, 250, 0, 100, 6},
  2089. {0xFFFF, 0, 0, 0, 0, 0},
  2090. };
  2091. struct dvb_frontend *dib0090_register(struct dvb_frontend *fe, struct i2c_adapter *i2c, const struct dib0090_config *config)
  2092. {
  2093. struct dib0090_state *st = kzalloc(sizeof(struct dib0090_state), GFP_KERNEL);
  2094. if (st == NULL)
  2095. return NULL;
  2096. st->config = config;
  2097. st->i2c = i2c;
  2098. st->fe = fe;
  2099. mutex_init(&st->i2c_buffer_lock);
  2100. fe->tuner_priv = st;
  2101. if (config->wbd == NULL)
  2102. st->current_wbd_table = dib0090_wbd_table_default;
  2103. else
  2104. st->current_wbd_table = config->wbd;
  2105. if (dib0090_reset(fe) != 0)
  2106. goto free_mem;
  2107. printk(KERN_INFO "DiB0090: successfully identified\n");
  2108. memcpy(&fe->ops.tuner_ops, &dib0090_ops, sizeof(struct dvb_tuner_ops));
  2109. return fe;
  2110. free_mem:
  2111. kfree(st);
  2112. fe->tuner_priv = NULL;
  2113. return NULL;
  2114. }
  2115. EXPORT_SYMBOL(dib0090_register);
  2116. struct dvb_frontend *dib0090_fw_register(struct dvb_frontend *fe, struct i2c_adapter *i2c, const struct dib0090_config *config)
  2117. {
  2118. struct dib0090_fw_state *st = kzalloc(sizeof(struct dib0090_fw_state), GFP_KERNEL);
  2119. if (st == NULL)
  2120. return NULL;
  2121. st->config = config;
  2122. st->i2c = i2c;
  2123. st->fe = fe;
  2124. mutex_init(&st->i2c_buffer_lock);
  2125. fe->tuner_priv = st;
  2126. if (dib0090_fw_reset_digital(fe, st->config) != 0)
  2127. goto free_mem;
  2128. dprintk("DiB0090 FW: successfully identified");
  2129. memcpy(&fe->ops.tuner_ops, &dib0090_fw_ops, sizeof(struct dvb_tuner_ops));
  2130. return fe;
  2131. free_mem:
  2132. kfree(st);
  2133. fe->tuner_priv = NULL;
  2134. return NULL;
  2135. }
  2136. EXPORT_SYMBOL(dib0090_fw_register);
  2137. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  2138. MODULE_AUTHOR("Olivier Grenie <olivier.grenie@dibcom.fr>");
  2139. MODULE_DESCRIPTION("Driver for the DiBcom 0090 base-band RF Tuner");
  2140. MODULE_LICENSE("GPL");