af9005-fe.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  1. /* Frontend part of the Linux driver for the Afatech 9005
  2. * USB1.1 DVB-T receiver.
  3. *
  4. * Copyright (C) 2007 Luca Olivetti (luca@ventoso.org)
  5. *
  6. * Thanks to Afatech who kindly provided information.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * see Documentation/dvb/README.dvb-usb for more information
  23. */
  24. #include "af9005.h"
  25. #include "af9005-script.h"
  26. #include "mt2060.h"
  27. #include "qt1010.h"
  28. #include <asm/div64.h>
  29. struct af9005_fe_state {
  30. struct dvb_usb_device *d;
  31. enum fe_status stat;
  32. /* retraining parameters */
  33. u32 original_fcw;
  34. u16 original_rf_top;
  35. u16 original_if_top;
  36. u16 original_if_min;
  37. u16 original_aci0_if_top;
  38. u16 original_aci1_if_top;
  39. u16 original_aci0_if_min;
  40. u8 original_if_unplug_th;
  41. u8 original_rf_unplug_th;
  42. u8 original_dtop_if_unplug_th;
  43. u8 original_dtop_rf_unplug_th;
  44. /* statistics */
  45. u32 pre_vit_error_count;
  46. u32 pre_vit_bit_count;
  47. u32 ber;
  48. u32 post_vit_error_count;
  49. u32 post_vit_bit_count;
  50. u32 unc;
  51. u16 abort_count;
  52. int opened;
  53. int strong;
  54. unsigned long next_status_check;
  55. struct dvb_frontend frontend;
  56. };
  57. static int af9005_write_word_agc(struct dvb_usb_device *d, u16 reghi,
  58. u16 reglo, u8 pos, u8 len, u16 value)
  59. {
  60. int ret;
  61. if ((ret = af9005_write_ofdm_register(d, reglo, (u8) (value & 0xff))))
  62. return ret;
  63. return af9005_write_register_bits(d, reghi, pos, len,
  64. (u8) ((value & 0x300) >> 8));
  65. }
  66. static int af9005_read_word_agc(struct dvb_usb_device *d, u16 reghi,
  67. u16 reglo, u8 pos, u8 len, u16 * value)
  68. {
  69. int ret;
  70. u8 temp0, temp1;
  71. if ((ret = af9005_read_ofdm_register(d, reglo, &temp0)))
  72. return ret;
  73. if ((ret = af9005_read_ofdm_register(d, reghi, &temp1)))
  74. return ret;
  75. switch (pos) {
  76. case 0:
  77. *value = ((u16) (temp1 & 0x03) << 8) + (u16) temp0;
  78. break;
  79. case 2:
  80. *value = ((u16) (temp1 & 0x0C) << 6) + (u16) temp0;
  81. break;
  82. case 4:
  83. *value = ((u16) (temp1 & 0x30) << 4) + (u16) temp0;
  84. break;
  85. case 6:
  86. *value = ((u16) (temp1 & 0xC0) << 2) + (u16) temp0;
  87. break;
  88. default:
  89. err("invalid pos in read word agc");
  90. return -EINVAL;
  91. }
  92. return 0;
  93. }
  94. static int af9005_is_fecmon_available(struct dvb_frontend *fe, int *available)
  95. {
  96. struct af9005_fe_state *state = fe->demodulator_priv;
  97. int ret;
  98. u8 temp;
  99. *available = false;
  100. ret = af9005_read_register_bits(state->d, xd_p_fec_vtb_rsd_mon_en,
  101. fec_vtb_rsd_mon_en_pos,
  102. fec_vtb_rsd_mon_en_len, &temp);
  103. if (ret)
  104. return ret;
  105. if (temp & 1) {
  106. ret =
  107. af9005_read_register_bits(state->d,
  108. xd_p_reg_ofsm_read_rbc_en,
  109. reg_ofsm_read_rbc_en_pos,
  110. reg_ofsm_read_rbc_en_len, &temp);
  111. if (ret)
  112. return ret;
  113. if ((temp & 1) == 0)
  114. *available = true;
  115. }
  116. return 0;
  117. }
  118. static int af9005_get_post_vit_err_cw_count(struct dvb_frontend *fe,
  119. u32 * post_err_count,
  120. u32 * post_cw_count,
  121. u16 * abort_count)
  122. {
  123. struct af9005_fe_state *state = fe->demodulator_priv;
  124. int ret;
  125. u32 err_count;
  126. u32 cw_count;
  127. u8 temp, temp0, temp1, temp2;
  128. u16 loc_abort_count;
  129. *post_err_count = 0;
  130. *post_cw_count = 0;
  131. /* check if error bit count is ready */
  132. ret =
  133. af9005_read_register_bits(state->d, xd_r_fec_rsd_ber_rdy,
  134. fec_rsd_ber_rdy_pos, fec_rsd_ber_rdy_len,
  135. &temp);
  136. if (ret)
  137. return ret;
  138. if (!temp) {
  139. deb_info("rsd counter not ready\n");
  140. return 100;
  141. }
  142. /* get abort count */
  143. ret =
  144. af9005_read_ofdm_register(state->d,
  145. xd_r_fec_rsd_abort_packet_cnt_7_0,
  146. &temp0);
  147. if (ret)
  148. return ret;
  149. ret =
  150. af9005_read_ofdm_register(state->d,
  151. xd_r_fec_rsd_abort_packet_cnt_15_8,
  152. &temp1);
  153. if (ret)
  154. return ret;
  155. loc_abort_count = ((u16) temp1 << 8) + temp0;
  156. /* get error count */
  157. ret =
  158. af9005_read_ofdm_register(state->d, xd_r_fec_rsd_bit_err_cnt_7_0,
  159. &temp0);
  160. if (ret)
  161. return ret;
  162. ret =
  163. af9005_read_ofdm_register(state->d, xd_r_fec_rsd_bit_err_cnt_15_8,
  164. &temp1);
  165. if (ret)
  166. return ret;
  167. ret =
  168. af9005_read_ofdm_register(state->d, xd_r_fec_rsd_bit_err_cnt_23_16,
  169. &temp2);
  170. if (ret)
  171. return ret;
  172. err_count = ((u32) temp2 << 16) + ((u32) temp1 << 8) + temp0;
  173. *post_err_count = err_count - (u32) loc_abort_count *8 * 8;
  174. /* get RSD packet number */
  175. ret =
  176. af9005_read_ofdm_register(state->d, xd_p_fec_rsd_packet_unit_7_0,
  177. &temp0);
  178. if (ret)
  179. return ret;
  180. ret =
  181. af9005_read_ofdm_register(state->d, xd_p_fec_rsd_packet_unit_15_8,
  182. &temp1);
  183. if (ret)
  184. return ret;
  185. cw_count = ((u32) temp1 << 8) + temp0;
  186. if (cw_count == 0) {
  187. err("wrong RSD packet count");
  188. return -EIO;
  189. }
  190. deb_info("POST abort count %d err count %d rsd packets %d\n",
  191. loc_abort_count, err_count, cw_count);
  192. *post_cw_count = cw_count - (u32) loc_abort_count;
  193. *abort_count = loc_abort_count;
  194. return 0;
  195. }
  196. static int af9005_get_post_vit_ber(struct dvb_frontend *fe,
  197. u32 * post_err_count, u32 * post_cw_count,
  198. u16 * abort_count)
  199. {
  200. u32 loc_cw_count = 0, loc_err_count;
  201. u16 loc_abort_count = 0;
  202. int ret;
  203. ret =
  204. af9005_get_post_vit_err_cw_count(fe, &loc_err_count, &loc_cw_count,
  205. &loc_abort_count);
  206. if (ret)
  207. return ret;
  208. *post_err_count = loc_err_count;
  209. *post_cw_count = loc_cw_count * 204 * 8;
  210. *abort_count = loc_abort_count;
  211. return 0;
  212. }
  213. static int af9005_get_pre_vit_err_bit_count(struct dvb_frontend *fe,
  214. u32 * pre_err_count,
  215. u32 * pre_bit_count)
  216. {
  217. struct af9005_fe_state *state = fe->demodulator_priv;
  218. u8 temp, temp0, temp1, temp2;
  219. u32 super_frame_count, x, bits;
  220. int ret;
  221. ret =
  222. af9005_read_register_bits(state->d, xd_r_fec_vtb_ber_rdy,
  223. fec_vtb_ber_rdy_pos, fec_vtb_ber_rdy_len,
  224. &temp);
  225. if (ret)
  226. return ret;
  227. if (!temp) {
  228. deb_info("viterbi counter not ready\n");
  229. return 101; /* ERR_APO_VTB_COUNTER_NOT_READY; */
  230. }
  231. ret =
  232. af9005_read_ofdm_register(state->d, xd_r_fec_vtb_err_bit_cnt_7_0,
  233. &temp0);
  234. if (ret)
  235. return ret;
  236. ret =
  237. af9005_read_ofdm_register(state->d, xd_r_fec_vtb_err_bit_cnt_15_8,
  238. &temp1);
  239. if (ret)
  240. return ret;
  241. ret =
  242. af9005_read_ofdm_register(state->d, xd_r_fec_vtb_err_bit_cnt_23_16,
  243. &temp2);
  244. if (ret)
  245. return ret;
  246. *pre_err_count = ((u32) temp2 << 16) + ((u32) temp1 << 8) + temp0;
  247. ret =
  248. af9005_read_ofdm_register(state->d, xd_p_fec_super_frm_unit_7_0,
  249. &temp0);
  250. if (ret)
  251. return ret;
  252. ret =
  253. af9005_read_ofdm_register(state->d, xd_p_fec_super_frm_unit_15_8,
  254. &temp1);
  255. if (ret)
  256. return ret;
  257. super_frame_count = ((u32) temp1 << 8) + temp0;
  258. if (super_frame_count == 0) {
  259. deb_info("super frame count 0\n");
  260. return 102;
  261. }
  262. /* read fft mode */
  263. ret =
  264. af9005_read_register_bits(state->d, xd_g_reg_tpsd_txmod,
  265. reg_tpsd_txmod_pos, reg_tpsd_txmod_len,
  266. &temp);
  267. if (ret)
  268. return ret;
  269. if (temp == 0) {
  270. /* 2K */
  271. x = 1512;
  272. } else if (temp == 1) {
  273. /* 8k */
  274. x = 6048;
  275. } else {
  276. err("Invalid fft mode");
  277. return -EINVAL;
  278. }
  279. /* read modulation mode */
  280. ret =
  281. af9005_read_register_bits(state->d, xd_g_reg_tpsd_const,
  282. reg_tpsd_const_pos, reg_tpsd_const_len,
  283. &temp);
  284. if (ret)
  285. return ret;
  286. switch (temp) {
  287. case 0: /* QPSK */
  288. bits = 2;
  289. break;
  290. case 1: /* QAM_16 */
  291. bits = 4;
  292. break;
  293. case 2: /* QAM_64 */
  294. bits = 6;
  295. break;
  296. default:
  297. err("invalid modulation mode");
  298. return -EINVAL;
  299. }
  300. *pre_bit_count = super_frame_count * 68 * 4 * x * bits;
  301. deb_info("PRE err count %d frame count %d bit count %d\n",
  302. *pre_err_count, super_frame_count, *pre_bit_count);
  303. return 0;
  304. }
  305. static int af9005_reset_pre_viterbi(struct dvb_frontend *fe)
  306. {
  307. struct af9005_fe_state *state = fe->demodulator_priv;
  308. int ret;
  309. /* set super frame count to 1 */
  310. ret =
  311. af9005_write_ofdm_register(state->d, xd_p_fec_super_frm_unit_7_0,
  312. 1 & 0xff);
  313. if (ret)
  314. return ret;
  315. ret = af9005_write_ofdm_register(state->d, xd_p_fec_super_frm_unit_15_8,
  316. 1 >> 8);
  317. if (ret)
  318. return ret;
  319. /* reset pre viterbi error count */
  320. ret =
  321. af9005_write_register_bits(state->d, xd_p_fec_vtb_ber_rst,
  322. fec_vtb_ber_rst_pos, fec_vtb_ber_rst_len,
  323. 1);
  324. return ret;
  325. }
  326. static int af9005_reset_post_viterbi(struct dvb_frontend *fe)
  327. {
  328. struct af9005_fe_state *state = fe->demodulator_priv;
  329. int ret;
  330. /* set packet unit */
  331. ret =
  332. af9005_write_ofdm_register(state->d, xd_p_fec_rsd_packet_unit_7_0,
  333. 10000 & 0xff);
  334. if (ret)
  335. return ret;
  336. ret =
  337. af9005_write_ofdm_register(state->d, xd_p_fec_rsd_packet_unit_15_8,
  338. 10000 >> 8);
  339. if (ret)
  340. return ret;
  341. /* reset post viterbi error count */
  342. ret =
  343. af9005_write_register_bits(state->d, xd_p_fec_rsd_ber_rst,
  344. fec_rsd_ber_rst_pos, fec_rsd_ber_rst_len,
  345. 1);
  346. return ret;
  347. }
  348. static int af9005_get_statistic(struct dvb_frontend *fe)
  349. {
  350. struct af9005_fe_state *state = fe->demodulator_priv;
  351. int ret, fecavailable;
  352. u64 numerator, denominator;
  353. deb_info("GET STATISTIC\n");
  354. ret = af9005_is_fecmon_available(fe, &fecavailable);
  355. if (ret)
  356. return ret;
  357. if (!fecavailable) {
  358. deb_info("fecmon not available\n");
  359. return 0;
  360. }
  361. ret = af9005_get_pre_vit_err_bit_count(fe, &state->pre_vit_error_count,
  362. &state->pre_vit_bit_count);
  363. if (ret == 0) {
  364. af9005_reset_pre_viterbi(fe);
  365. if (state->pre_vit_bit_count > 0) {
  366. /* according to v 0.0.4 of the dvb api ber should be a multiple
  367. of 10E-9 so we have to multiply the error count by
  368. 10E9=1000000000 */
  369. numerator =
  370. (u64) state->pre_vit_error_count * (u64) 1000000000;
  371. denominator = (u64) state->pre_vit_bit_count;
  372. state->ber = do_div(numerator, denominator);
  373. } else {
  374. state->ber = 0xffffffff;
  375. }
  376. }
  377. ret = af9005_get_post_vit_ber(fe, &state->post_vit_error_count,
  378. &state->post_vit_bit_count,
  379. &state->abort_count);
  380. if (ret == 0) {
  381. ret = af9005_reset_post_viterbi(fe);
  382. state->unc += state->abort_count;
  383. if (ret)
  384. return ret;
  385. }
  386. return 0;
  387. }
  388. static int af9005_fe_refresh_state(struct dvb_frontend *fe)
  389. {
  390. struct af9005_fe_state *state = fe->demodulator_priv;
  391. if (time_after(jiffies, state->next_status_check)) {
  392. deb_info("REFRESH STATE\n");
  393. /* statistics */
  394. if (af9005_get_statistic(fe))
  395. err("get_statistic_failed");
  396. state->next_status_check = jiffies + 250 * HZ / 1000;
  397. }
  398. return 0;
  399. }
  400. static int af9005_fe_read_status(struct dvb_frontend *fe,
  401. enum fe_status *stat)
  402. {
  403. struct af9005_fe_state *state = fe->demodulator_priv;
  404. u8 temp;
  405. int ret;
  406. if (fe->ops.tuner_ops.release == NULL)
  407. return -ENODEV;
  408. *stat = 0;
  409. ret = af9005_read_register_bits(state->d, xd_p_agc_lock,
  410. agc_lock_pos, agc_lock_len, &temp);
  411. if (ret)
  412. return ret;
  413. if (temp)
  414. *stat |= FE_HAS_SIGNAL;
  415. ret = af9005_read_register_bits(state->d, xd_p_fd_tpsd_lock,
  416. fd_tpsd_lock_pos, fd_tpsd_lock_len,
  417. &temp);
  418. if (ret)
  419. return ret;
  420. if (temp)
  421. *stat |= FE_HAS_CARRIER;
  422. ret = af9005_read_register_bits(state->d,
  423. xd_r_mp2if_sync_byte_locked,
  424. mp2if_sync_byte_locked_pos,
  425. mp2if_sync_byte_locked_pos, &temp);
  426. if (ret)
  427. return ret;
  428. if (temp)
  429. *stat |= FE_HAS_SYNC | FE_HAS_VITERBI | FE_HAS_LOCK;
  430. if (state->opened)
  431. af9005_led_control(state->d, *stat & FE_HAS_LOCK);
  432. ret =
  433. af9005_read_register_bits(state->d, xd_p_reg_strong_sginal_detected,
  434. reg_strong_sginal_detected_pos,
  435. reg_strong_sginal_detected_len, &temp);
  436. if (ret)
  437. return ret;
  438. if (temp != state->strong) {
  439. deb_info("adjust for strong signal %d\n", temp);
  440. state->strong = temp;
  441. }
  442. return 0;
  443. }
  444. static int af9005_fe_read_ber(struct dvb_frontend *fe, u32 * ber)
  445. {
  446. struct af9005_fe_state *state = fe->demodulator_priv;
  447. if (fe->ops.tuner_ops.release == NULL)
  448. return -ENODEV;
  449. af9005_fe_refresh_state(fe);
  450. *ber = state->ber;
  451. return 0;
  452. }
  453. static int af9005_fe_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
  454. {
  455. struct af9005_fe_state *state = fe->demodulator_priv;
  456. if (fe->ops.tuner_ops.release == NULL)
  457. return -ENODEV;
  458. af9005_fe_refresh_state(fe);
  459. *unc = state->unc;
  460. return 0;
  461. }
  462. static int af9005_fe_read_signal_strength(struct dvb_frontend *fe,
  463. u16 * strength)
  464. {
  465. struct af9005_fe_state *state = fe->demodulator_priv;
  466. int ret;
  467. u8 if_gain, rf_gain;
  468. if (fe->ops.tuner_ops.release == NULL)
  469. return -ENODEV;
  470. ret =
  471. af9005_read_ofdm_register(state->d, xd_r_reg_aagc_rf_gain,
  472. &rf_gain);
  473. if (ret)
  474. return ret;
  475. ret =
  476. af9005_read_ofdm_register(state->d, xd_r_reg_aagc_if_gain,
  477. &if_gain);
  478. if (ret)
  479. return ret;
  480. /* this value has no real meaning, but i don't have the tables that relate
  481. the rf and if gain with the dbm, so I just scale the value */
  482. *strength = (512 - rf_gain - if_gain) << 7;
  483. return 0;
  484. }
  485. static int af9005_fe_read_snr(struct dvb_frontend *fe, u16 * snr)
  486. {
  487. /* the snr can be derived from the ber and the modulation
  488. but I don't think this kind of complex calculations belong
  489. in the driver. I may be wrong.... */
  490. return -ENOSYS;
  491. }
  492. static int af9005_fe_program_cfoe(struct dvb_usb_device *d, u32 bw)
  493. {
  494. u8 temp0, temp1, temp2, temp3, buf[4];
  495. int ret;
  496. u32 NS_coeff1_2048Nu;
  497. u32 NS_coeff1_8191Nu;
  498. u32 NS_coeff1_8192Nu;
  499. u32 NS_coeff1_8193Nu;
  500. u32 NS_coeff2_2k;
  501. u32 NS_coeff2_8k;
  502. switch (bw) {
  503. case 6000000:
  504. NS_coeff1_2048Nu = 0x2ADB6DC;
  505. NS_coeff1_8191Nu = 0xAB7313;
  506. NS_coeff1_8192Nu = 0xAB6DB7;
  507. NS_coeff1_8193Nu = 0xAB685C;
  508. NS_coeff2_2k = 0x156DB6E;
  509. NS_coeff2_8k = 0x55B6DC;
  510. break;
  511. case 7000000:
  512. NS_coeff1_2048Nu = 0x3200001;
  513. NS_coeff1_8191Nu = 0xC80640;
  514. NS_coeff1_8192Nu = 0xC80000;
  515. NS_coeff1_8193Nu = 0xC7F9C0;
  516. NS_coeff2_2k = 0x1900000;
  517. NS_coeff2_8k = 0x640000;
  518. break;
  519. case 8000000:
  520. NS_coeff1_2048Nu = 0x3924926;
  521. NS_coeff1_8191Nu = 0xE4996E;
  522. NS_coeff1_8192Nu = 0xE49249;
  523. NS_coeff1_8193Nu = 0xE48B25;
  524. NS_coeff2_2k = 0x1C92493;
  525. NS_coeff2_8k = 0x724925;
  526. break;
  527. default:
  528. err("Invalid bandwidth %d.", bw);
  529. return -EINVAL;
  530. }
  531. /*
  532. * write NS_coeff1_2048Nu
  533. */
  534. temp0 = (u8) (NS_coeff1_2048Nu & 0x000000FF);
  535. temp1 = (u8) ((NS_coeff1_2048Nu & 0x0000FF00) >> 8);
  536. temp2 = (u8) ((NS_coeff1_2048Nu & 0x00FF0000) >> 16);
  537. temp3 = (u8) ((NS_coeff1_2048Nu & 0x03000000) >> 24);
  538. /* big endian to make 8051 happy */
  539. buf[0] = temp3;
  540. buf[1] = temp2;
  541. buf[2] = temp1;
  542. buf[3] = temp0;
  543. /* cfoe_NS_2k_coeff1_25_24 */
  544. ret = af9005_write_ofdm_register(d, 0xAE00, buf[0]);
  545. if (ret)
  546. return ret;
  547. /* cfoe_NS_2k_coeff1_23_16 */
  548. ret = af9005_write_ofdm_register(d, 0xAE01, buf[1]);
  549. if (ret)
  550. return ret;
  551. /* cfoe_NS_2k_coeff1_15_8 */
  552. ret = af9005_write_ofdm_register(d, 0xAE02, buf[2]);
  553. if (ret)
  554. return ret;
  555. /* cfoe_NS_2k_coeff1_7_0 */
  556. ret = af9005_write_ofdm_register(d, 0xAE03, buf[3]);
  557. if (ret)
  558. return ret;
  559. /*
  560. * write NS_coeff2_2k
  561. */
  562. temp0 = (u8) ((NS_coeff2_2k & 0x0000003F));
  563. temp1 = (u8) ((NS_coeff2_2k & 0x00003FC0) >> 6);
  564. temp2 = (u8) ((NS_coeff2_2k & 0x003FC000) >> 14);
  565. temp3 = (u8) ((NS_coeff2_2k & 0x01C00000) >> 22);
  566. /* big endian to make 8051 happy */
  567. buf[0] = temp3;
  568. buf[1] = temp2;
  569. buf[2] = temp1;
  570. buf[3] = temp0;
  571. ret = af9005_write_ofdm_register(d, 0xAE04, buf[0]);
  572. if (ret)
  573. return ret;
  574. ret = af9005_write_ofdm_register(d, 0xAE05, buf[1]);
  575. if (ret)
  576. return ret;
  577. ret = af9005_write_ofdm_register(d, 0xAE06, buf[2]);
  578. if (ret)
  579. return ret;
  580. ret = af9005_write_ofdm_register(d, 0xAE07, buf[3]);
  581. if (ret)
  582. return ret;
  583. /*
  584. * write NS_coeff1_8191Nu
  585. */
  586. temp0 = (u8) ((NS_coeff1_8191Nu & 0x000000FF));
  587. temp1 = (u8) ((NS_coeff1_8191Nu & 0x0000FF00) >> 8);
  588. temp2 = (u8) ((NS_coeff1_8191Nu & 0x00FFC000) >> 16);
  589. temp3 = (u8) ((NS_coeff1_8191Nu & 0x03000000) >> 24);
  590. /* big endian to make 8051 happy */
  591. buf[0] = temp3;
  592. buf[1] = temp2;
  593. buf[2] = temp1;
  594. buf[3] = temp0;
  595. ret = af9005_write_ofdm_register(d, 0xAE08, buf[0]);
  596. if (ret)
  597. return ret;
  598. ret = af9005_write_ofdm_register(d, 0xAE09, buf[1]);
  599. if (ret)
  600. return ret;
  601. ret = af9005_write_ofdm_register(d, 0xAE0A, buf[2]);
  602. if (ret)
  603. return ret;
  604. ret = af9005_write_ofdm_register(d, 0xAE0B, buf[3]);
  605. if (ret)
  606. return ret;
  607. /*
  608. * write NS_coeff1_8192Nu
  609. */
  610. temp0 = (u8) (NS_coeff1_8192Nu & 0x000000FF);
  611. temp1 = (u8) ((NS_coeff1_8192Nu & 0x0000FF00) >> 8);
  612. temp2 = (u8) ((NS_coeff1_8192Nu & 0x00FFC000) >> 16);
  613. temp3 = (u8) ((NS_coeff1_8192Nu & 0x03000000) >> 24);
  614. /* big endian to make 8051 happy */
  615. buf[0] = temp3;
  616. buf[1] = temp2;
  617. buf[2] = temp1;
  618. buf[3] = temp0;
  619. ret = af9005_write_ofdm_register(d, 0xAE0C, buf[0]);
  620. if (ret)
  621. return ret;
  622. ret = af9005_write_ofdm_register(d, 0xAE0D, buf[1]);
  623. if (ret)
  624. return ret;
  625. ret = af9005_write_ofdm_register(d, 0xAE0E, buf[2]);
  626. if (ret)
  627. return ret;
  628. ret = af9005_write_ofdm_register(d, 0xAE0F, buf[3]);
  629. if (ret)
  630. return ret;
  631. /*
  632. * write NS_coeff1_8193Nu
  633. */
  634. temp0 = (u8) ((NS_coeff1_8193Nu & 0x000000FF));
  635. temp1 = (u8) ((NS_coeff1_8193Nu & 0x0000FF00) >> 8);
  636. temp2 = (u8) ((NS_coeff1_8193Nu & 0x00FFC000) >> 16);
  637. temp3 = (u8) ((NS_coeff1_8193Nu & 0x03000000) >> 24);
  638. /* big endian to make 8051 happy */
  639. buf[0] = temp3;
  640. buf[1] = temp2;
  641. buf[2] = temp1;
  642. buf[3] = temp0;
  643. ret = af9005_write_ofdm_register(d, 0xAE10, buf[0]);
  644. if (ret)
  645. return ret;
  646. ret = af9005_write_ofdm_register(d, 0xAE11, buf[1]);
  647. if (ret)
  648. return ret;
  649. ret = af9005_write_ofdm_register(d, 0xAE12, buf[2]);
  650. if (ret)
  651. return ret;
  652. ret = af9005_write_ofdm_register(d, 0xAE13, buf[3]);
  653. if (ret)
  654. return ret;
  655. /*
  656. * write NS_coeff2_8k
  657. */
  658. temp0 = (u8) ((NS_coeff2_8k & 0x0000003F));
  659. temp1 = (u8) ((NS_coeff2_8k & 0x00003FC0) >> 6);
  660. temp2 = (u8) ((NS_coeff2_8k & 0x003FC000) >> 14);
  661. temp3 = (u8) ((NS_coeff2_8k & 0x01C00000) >> 22);
  662. /* big endian to make 8051 happy */
  663. buf[0] = temp3;
  664. buf[1] = temp2;
  665. buf[2] = temp1;
  666. buf[3] = temp0;
  667. ret = af9005_write_ofdm_register(d, 0xAE14, buf[0]);
  668. if (ret)
  669. return ret;
  670. ret = af9005_write_ofdm_register(d, 0xAE15, buf[1]);
  671. if (ret)
  672. return ret;
  673. ret = af9005_write_ofdm_register(d, 0xAE16, buf[2]);
  674. if (ret)
  675. return ret;
  676. ret = af9005_write_ofdm_register(d, 0xAE17, buf[3]);
  677. return ret;
  678. }
  679. static int af9005_fe_select_bw(struct dvb_usb_device *d, u32 bw)
  680. {
  681. u8 temp;
  682. switch (bw) {
  683. case 6000000:
  684. temp = 0;
  685. break;
  686. case 7000000:
  687. temp = 1;
  688. break;
  689. case 8000000:
  690. temp = 2;
  691. break;
  692. default:
  693. err("Invalid bandwidth %d.", bw);
  694. return -EINVAL;
  695. }
  696. return af9005_write_register_bits(d, xd_g_reg_bw, reg_bw_pos,
  697. reg_bw_len, temp);
  698. }
  699. static int af9005_fe_power(struct dvb_frontend *fe, int on)
  700. {
  701. struct af9005_fe_state *state = fe->demodulator_priv;
  702. u8 temp = on;
  703. int ret;
  704. deb_info("power %s tuner\n", on ? "on" : "off");
  705. ret = af9005_send_command(state->d, 0x03, &temp, 1, NULL, 0);
  706. return ret;
  707. }
  708. static struct mt2060_config af9005_mt2060_config = {
  709. 0xC0
  710. };
  711. static struct qt1010_config af9005_qt1010_config = {
  712. 0xC4
  713. };
  714. static int af9005_fe_init(struct dvb_frontend *fe)
  715. {
  716. struct af9005_fe_state *state = fe->demodulator_priv;
  717. struct dvb_usb_adapter *adap = fe->dvb->priv;
  718. int ret, i, scriptlen;
  719. u8 temp, temp0 = 0, temp1 = 0, temp2 = 0;
  720. u8 buf[2];
  721. u16 if1;
  722. deb_info("in af9005_fe_init\n");
  723. /* reset */
  724. deb_info("reset\n");
  725. if ((ret =
  726. af9005_write_register_bits(state->d, xd_I2C_reg_ofdm_rst_en,
  727. 4, 1, 0x01)))
  728. return ret;
  729. if ((ret = af9005_write_ofdm_register(state->d, APO_REG_RESET, 0)))
  730. return ret;
  731. /* clear ofdm reset */
  732. deb_info("clear ofdm reset\n");
  733. for (i = 0; i < 150; i++) {
  734. if ((ret =
  735. af9005_read_ofdm_register(state->d,
  736. xd_I2C_reg_ofdm_rst, &temp)))
  737. return ret;
  738. if (temp & (regmask[reg_ofdm_rst_len - 1] << reg_ofdm_rst_pos))
  739. break;
  740. msleep(10);
  741. }
  742. if (i == 150)
  743. return -ETIMEDOUT;
  744. /*FIXME in the dump
  745. write B200 A9
  746. write xd_g_reg_ofsm_clk 7
  747. read eepr c6 (2)
  748. read eepr c7 (2)
  749. misc ctrl 3 -> 1
  750. read eepr ca (6)
  751. write xd_g_reg_ofsm_clk 0
  752. write B200 a1
  753. */
  754. ret = af9005_write_ofdm_register(state->d, 0xb200, 0xa9);
  755. if (ret)
  756. return ret;
  757. ret = af9005_write_ofdm_register(state->d, xd_g_reg_ofsm_clk, 0x07);
  758. if (ret)
  759. return ret;
  760. temp = 0x01;
  761. ret = af9005_send_command(state->d, 0x03, &temp, 1, NULL, 0);
  762. if (ret)
  763. return ret;
  764. ret = af9005_write_ofdm_register(state->d, xd_g_reg_ofsm_clk, 0x00);
  765. if (ret)
  766. return ret;
  767. ret = af9005_write_ofdm_register(state->d, 0xb200, 0xa1);
  768. if (ret)
  769. return ret;
  770. temp = regmask[reg_ofdm_rst_len - 1] << reg_ofdm_rst_pos;
  771. if ((ret =
  772. af9005_write_register_bits(state->d, xd_I2C_reg_ofdm_rst,
  773. reg_ofdm_rst_pos, reg_ofdm_rst_len, 1)))
  774. return ret;
  775. ret = af9005_write_register_bits(state->d, xd_I2C_reg_ofdm_rst,
  776. reg_ofdm_rst_pos, reg_ofdm_rst_len, 0);
  777. if (ret)
  778. return ret;
  779. /* don't know what register aefc is, but this is what the windows driver does */
  780. ret = af9005_write_ofdm_register(state->d, 0xaefc, 0);
  781. if (ret)
  782. return ret;
  783. /* set stand alone chip */
  784. deb_info("set stand alone chip\n");
  785. if ((ret =
  786. af9005_write_register_bits(state->d, xd_p_reg_dca_stand_alone,
  787. reg_dca_stand_alone_pos,
  788. reg_dca_stand_alone_len, 1)))
  789. return ret;
  790. /* set dca upper & lower chip */
  791. deb_info("set dca upper & lower chip\n");
  792. if ((ret =
  793. af9005_write_register_bits(state->d, xd_p_reg_dca_upper_chip,
  794. reg_dca_upper_chip_pos,
  795. reg_dca_upper_chip_len, 0)))
  796. return ret;
  797. if ((ret =
  798. af9005_write_register_bits(state->d, xd_p_reg_dca_lower_chip,
  799. reg_dca_lower_chip_pos,
  800. reg_dca_lower_chip_len, 0)))
  801. return ret;
  802. /* set 2wire master clock to 0x14 (for 60KHz) */
  803. deb_info("set 2wire master clock to 0x14 (for 60KHz)\n");
  804. if ((ret =
  805. af9005_write_ofdm_register(state->d, xd_I2C_i2c_m_period, 0x14)))
  806. return ret;
  807. /* clear dca enable chip */
  808. deb_info("clear dca enable chip\n");
  809. if ((ret =
  810. af9005_write_register_bits(state->d, xd_p_reg_dca_en,
  811. reg_dca_en_pos, reg_dca_en_len, 0)))
  812. return ret;
  813. /* FIXME these are register bits, but I don't know which ones */
  814. ret = af9005_write_ofdm_register(state->d, 0xa16c, 1);
  815. if (ret)
  816. return ret;
  817. ret = af9005_write_ofdm_register(state->d, 0xa3c1, 0);
  818. if (ret)
  819. return ret;
  820. /* init other parameters: program cfoe and select bandwidth */
  821. deb_info("program cfoe\n");
  822. ret = af9005_fe_program_cfoe(state->d, 6000000);
  823. if (ret)
  824. return ret;
  825. /* set read-update bit for modulation */
  826. deb_info("set read-update bit for modulation\n");
  827. if ((ret =
  828. af9005_write_register_bits(state->d, xd_p_reg_feq_read_update,
  829. reg_feq_read_update_pos,
  830. reg_feq_read_update_len, 1)))
  831. return ret;
  832. /* sample code has a set MPEG TS code here
  833. but sniffing reveals that it doesn't do it */
  834. /* set read-update bit to 1 for DCA modulation */
  835. deb_info("set read-update bit 1 for DCA modulation\n");
  836. if ((ret =
  837. af9005_write_register_bits(state->d, xd_p_reg_dca_read_update,
  838. reg_dca_read_update_pos,
  839. reg_dca_read_update_len, 1)))
  840. return ret;
  841. /* enable fec monitor */
  842. deb_info("enable fec monitor\n");
  843. if ((ret =
  844. af9005_write_register_bits(state->d, xd_p_fec_vtb_rsd_mon_en,
  845. fec_vtb_rsd_mon_en_pos,
  846. fec_vtb_rsd_mon_en_len, 1)))
  847. return ret;
  848. /* FIXME should be register bits, I don't know which ones */
  849. ret = af9005_write_ofdm_register(state->d, 0xa601, 0);
  850. /* set api_retrain_never_freeze */
  851. deb_info("set api_retrain_never_freeze\n");
  852. if ((ret = af9005_write_ofdm_register(state->d, 0xaefb, 0x01)))
  853. return ret;
  854. /* load init script */
  855. deb_info("load init script\n");
  856. scriptlen = sizeof(script) / sizeof(RegDesc);
  857. for (i = 0; i < scriptlen; i++) {
  858. if ((ret =
  859. af9005_write_register_bits(state->d, script[i].reg,
  860. script[i].pos,
  861. script[i].len, script[i].val)))
  862. return ret;
  863. /* save 3 bytes of original fcw */
  864. if (script[i].reg == 0xae18)
  865. temp2 = script[i].val;
  866. if (script[i].reg == 0xae19)
  867. temp1 = script[i].val;
  868. if (script[i].reg == 0xae1a)
  869. temp0 = script[i].val;
  870. /* save original unplug threshold */
  871. if (script[i].reg == xd_p_reg_unplug_th)
  872. state->original_if_unplug_th = script[i].val;
  873. if (script[i].reg == xd_p_reg_unplug_rf_gain_th)
  874. state->original_rf_unplug_th = script[i].val;
  875. if (script[i].reg == xd_p_reg_unplug_dtop_if_gain_th)
  876. state->original_dtop_if_unplug_th = script[i].val;
  877. if (script[i].reg == xd_p_reg_unplug_dtop_rf_gain_th)
  878. state->original_dtop_rf_unplug_th = script[i].val;
  879. }
  880. state->original_fcw =
  881. ((u32) temp2 << 16) + ((u32) temp1 << 8) + (u32) temp0;
  882. /* save original TOPs */
  883. deb_info("save original TOPs\n");
  884. /* RF TOP */
  885. ret =
  886. af9005_read_word_agc(state->d,
  887. xd_p_reg_aagc_rf_top_numerator_9_8,
  888. xd_p_reg_aagc_rf_top_numerator_7_0, 0, 2,
  889. &state->original_rf_top);
  890. if (ret)
  891. return ret;
  892. /* IF TOP */
  893. ret =
  894. af9005_read_word_agc(state->d,
  895. xd_p_reg_aagc_if_top_numerator_9_8,
  896. xd_p_reg_aagc_if_top_numerator_7_0, 0, 2,
  897. &state->original_if_top);
  898. if (ret)
  899. return ret;
  900. /* ACI 0 IF TOP */
  901. ret =
  902. af9005_read_word_agc(state->d, 0xA60E, 0xA60A, 4, 2,
  903. &state->original_aci0_if_top);
  904. if (ret)
  905. return ret;
  906. /* ACI 1 IF TOP */
  907. ret =
  908. af9005_read_word_agc(state->d, 0xA60E, 0xA60B, 6, 2,
  909. &state->original_aci1_if_top);
  910. if (ret)
  911. return ret;
  912. /* attach tuner and init */
  913. if (fe->ops.tuner_ops.release == NULL) {
  914. /* read tuner and board id from eeprom */
  915. ret = af9005_read_eeprom(adap->dev, 0xc6, buf, 2);
  916. if (ret) {
  917. err("Impossible to read EEPROM\n");
  918. return ret;
  919. }
  920. deb_info("Tuner id %d, board id %d\n", buf[0], buf[1]);
  921. switch (buf[0]) {
  922. case 2: /* MT2060 */
  923. /* read if1 from eeprom */
  924. ret = af9005_read_eeprom(adap->dev, 0xc8, buf, 2);
  925. if (ret) {
  926. err("Impossible to read EEPROM\n");
  927. return ret;
  928. }
  929. if1 = (u16) (buf[0] << 8) + buf[1];
  930. if (dvb_attach(mt2060_attach, fe, &adap->dev->i2c_adap,
  931. &af9005_mt2060_config, if1) == NULL) {
  932. deb_info("MT2060 attach failed\n");
  933. return -ENODEV;
  934. }
  935. break;
  936. case 3: /* QT1010 */
  937. case 9: /* QT1010B */
  938. if (dvb_attach(qt1010_attach, fe, &adap->dev->i2c_adap,
  939. &af9005_qt1010_config) ==NULL) {
  940. deb_info("QT1010 attach failed\n");
  941. return -ENODEV;
  942. }
  943. break;
  944. default:
  945. err("Unsupported tuner type %d", buf[0]);
  946. return -ENODEV;
  947. }
  948. ret = fe->ops.tuner_ops.init(fe);
  949. if (ret)
  950. return ret;
  951. }
  952. deb_info("profit!\n");
  953. return 0;
  954. }
  955. static int af9005_fe_sleep(struct dvb_frontend *fe)
  956. {
  957. return af9005_fe_power(fe, 0);
  958. }
  959. static int af9005_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
  960. {
  961. struct af9005_fe_state *state = fe->demodulator_priv;
  962. if (acquire) {
  963. state->opened++;
  964. } else {
  965. state->opened--;
  966. if (!state->opened)
  967. af9005_led_control(state->d, 0);
  968. }
  969. return 0;
  970. }
  971. static int af9005_fe_set_frontend(struct dvb_frontend *fe)
  972. {
  973. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  974. struct af9005_fe_state *state = fe->demodulator_priv;
  975. int ret;
  976. u8 temp, temp0, temp1, temp2;
  977. deb_info("af9005_fe_set_frontend freq %d bw %d\n", fep->frequency,
  978. fep->bandwidth_hz);
  979. if (fe->ops.tuner_ops.release == NULL) {
  980. err("Tuner not attached");
  981. return -ENODEV;
  982. }
  983. deb_info("turn off led\n");
  984. /* not in the log */
  985. ret = af9005_led_control(state->d, 0);
  986. if (ret)
  987. return ret;
  988. /* not sure about the bits */
  989. ret = af9005_write_register_bits(state->d, XD_MP2IF_MISC, 2, 1, 0);
  990. if (ret)
  991. return ret;
  992. /* set FCW to default value */
  993. deb_info("set FCW to default value\n");
  994. temp0 = (u8) (state->original_fcw & 0x000000ff);
  995. temp1 = (u8) ((state->original_fcw & 0x0000ff00) >> 8);
  996. temp2 = (u8) ((state->original_fcw & 0x00ff0000) >> 16);
  997. ret = af9005_write_ofdm_register(state->d, 0xae1a, temp0);
  998. if (ret)
  999. return ret;
  1000. ret = af9005_write_ofdm_register(state->d, 0xae19, temp1);
  1001. if (ret)
  1002. return ret;
  1003. ret = af9005_write_ofdm_register(state->d, 0xae18, temp2);
  1004. if (ret)
  1005. return ret;
  1006. /* restore original TOPs */
  1007. deb_info("restore original TOPs\n");
  1008. ret =
  1009. af9005_write_word_agc(state->d,
  1010. xd_p_reg_aagc_rf_top_numerator_9_8,
  1011. xd_p_reg_aagc_rf_top_numerator_7_0, 0, 2,
  1012. state->original_rf_top);
  1013. if (ret)
  1014. return ret;
  1015. ret =
  1016. af9005_write_word_agc(state->d,
  1017. xd_p_reg_aagc_if_top_numerator_9_8,
  1018. xd_p_reg_aagc_if_top_numerator_7_0, 0, 2,
  1019. state->original_if_top);
  1020. if (ret)
  1021. return ret;
  1022. ret =
  1023. af9005_write_word_agc(state->d, 0xA60E, 0xA60A, 4, 2,
  1024. state->original_aci0_if_top);
  1025. if (ret)
  1026. return ret;
  1027. ret =
  1028. af9005_write_word_agc(state->d, 0xA60E, 0xA60B, 6, 2,
  1029. state->original_aci1_if_top);
  1030. if (ret)
  1031. return ret;
  1032. /* select bandwidth */
  1033. deb_info("select bandwidth");
  1034. ret = af9005_fe_select_bw(state->d, fep->bandwidth_hz);
  1035. if (ret)
  1036. return ret;
  1037. ret = af9005_fe_program_cfoe(state->d, fep->bandwidth_hz);
  1038. if (ret)
  1039. return ret;
  1040. /* clear easy mode flag */
  1041. deb_info("clear easy mode flag\n");
  1042. ret = af9005_write_ofdm_register(state->d, 0xaefd, 0);
  1043. if (ret)
  1044. return ret;
  1045. /* set unplug threshold to original value */
  1046. deb_info("set unplug threshold to original value\n");
  1047. ret =
  1048. af9005_write_ofdm_register(state->d, xd_p_reg_unplug_th,
  1049. state->original_if_unplug_th);
  1050. if (ret)
  1051. return ret;
  1052. /* set tuner */
  1053. deb_info("set tuner\n");
  1054. ret = fe->ops.tuner_ops.set_params(fe);
  1055. if (ret)
  1056. return ret;
  1057. /* trigger ofsm */
  1058. deb_info("trigger ofsm\n");
  1059. temp = 0;
  1060. ret = af9005_write_tuner_registers(state->d, 0xffff, &temp, 1);
  1061. if (ret)
  1062. return ret;
  1063. /* clear retrain and freeze flag */
  1064. deb_info("clear retrain and freeze flag\n");
  1065. ret =
  1066. af9005_write_register_bits(state->d,
  1067. xd_p_reg_api_retrain_request,
  1068. reg_api_retrain_request_pos, 2, 0);
  1069. if (ret)
  1070. return ret;
  1071. /* reset pre viterbi and post viterbi registers and statistics */
  1072. af9005_reset_pre_viterbi(fe);
  1073. af9005_reset_post_viterbi(fe);
  1074. state->pre_vit_error_count = 0;
  1075. state->pre_vit_bit_count = 0;
  1076. state->ber = 0;
  1077. state->post_vit_error_count = 0;
  1078. /* state->unc = 0; commented out since it should be ever increasing */
  1079. state->abort_count = 0;
  1080. state->next_status_check = jiffies;
  1081. state->strong = -1;
  1082. return 0;
  1083. }
  1084. static int af9005_fe_get_frontend(struct dvb_frontend *fe)
  1085. {
  1086. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  1087. struct af9005_fe_state *state = fe->demodulator_priv;
  1088. int ret;
  1089. u8 temp;
  1090. /* mode */
  1091. ret =
  1092. af9005_read_register_bits(state->d, xd_g_reg_tpsd_const,
  1093. reg_tpsd_const_pos, reg_tpsd_const_len,
  1094. &temp);
  1095. if (ret)
  1096. return ret;
  1097. deb_info("===== fe_get_frontend_legacy = =============\n");
  1098. deb_info("CONSTELLATION ");
  1099. switch (temp) {
  1100. case 0:
  1101. fep->modulation = QPSK;
  1102. deb_info("QPSK\n");
  1103. break;
  1104. case 1:
  1105. fep->modulation = QAM_16;
  1106. deb_info("QAM_16\n");
  1107. break;
  1108. case 2:
  1109. fep->modulation = QAM_64;
  1110. deb_info("QAM_64\n");
  1111. break;
  1112. }
  1113. /* tps hierarchy and alpha value */
  1114. ret =
  1115. af9005_read_register_bits(state->d, xd_g_reg_tpsd_hier,
  1116. reg_tpsd_hier_pos, reg_tpsd_hier_len,
  1117. &temp);
  1118. if (ret)
  1119. return ret;
  1120. deb_info("HIERARCHY ");
  1121. switch (temp) {
  1122. case 0:
  1123. fep->hierarchy = HIERARCHY_NONE;
  1124. deb_info("NONE\n");
  1125. break;
  1126. case 1:
  1127. fep->hierarchy = HIERARCHY_1;
  1128. deb_info("1\n");
  1129. break;
  1130. case 2:
  1131. fep->hierarchy = HIERARCHY_2;
  1132. deb_info("2\n");
  1133. break;
  1134. case 3:
  1135. fep->hierarchy = HIERARCHY_4;
  1136. deb_info("4\n");
  1137. break;
  1138. }
  1139. /* high/low priority */
  1140. ret =
  1141. af9005_read_register_bits(state->d, xd_g_reg_dec_pri,
  1142. reg_dec_pri_pos, reg_dec_pri_len, &temp);
  1143. if (ret)
  1144. return ret;
  1145. /* if temp is set = high priority */
  1146. deb_info("PRIORITY %s\n", temp ? "high" : "low");
  1147. /* high coderate */
  1148. ret =
  1149. af9005_read_register_bits(state->d, xd_g_reg_tpsd_hpcr,
  1150. reg_tpsd_hpcr_pos, reg_tpsd_hpcr_len,
  1151. &temp);
  1152. if (ret)
  1153. return ret;
  1154. deb_info("CODERATE HP ");
  1155. switch (temp) {
  1156. case 0:
  1157. fep->code_rate_HP = FEC_1_2;
  1158. deb_info("FEC_1_2\n");
  1159. break;
  1160. case 1:
  1161. fep->code_rate_HP = FEC_2_3;
  1162. deb_info("FEC_2_3\n");
  1163. break;
  1164. case 2:
  1165. fep->code_rate_HP = FEC_3_4;
  1166. deb_info("FEC_3_4\n");
  1167. break;
  1168. case 3:
  1169. fep->code_rate_HP = FEC_5_6;
  1170. deb_info("FEC_5_6\n");
  1171. break;
  1172. case 4:
  1173. fep->code_rate_HP = FEC_7_8;
  1174. deb_info("FEC_7_8\n");
  1175. break;
  1176. }
  1177. /* low coderate */
  1178. ret =
  1179. af9005_read_register_bits(state->d, xd_g_reg_tpsd_lpcr,
  1180. reg_tpsd_lpcr_pos, reg_tpsd_lpcr_len,
  1181. &temp);
  1182. if (ret)
  1183. return ret;
  1184. deb_info("CODERATE LP ");
  1185. switch (temp) {
  1186. case 0:
  1187. fep->code_rate_LP = FEC_1_2;
  1188. deb_info("FEC_1_2\n");
  1189. break;
  1190. case 1:
  1191. fep->code_rate_LP = FEC_2_3;
  1192. deb_info("FEC_2_3\n");
  1193. break;
  1194. case 2:
  1195. fep->code_rate_LP = FEC_3_4;
  1196. deb_info("FEC_3_4\n");
  1197. break;
  1198. case 3:
  1199. fep->code_rate_LP = FEC_5_6;
  1200. deb_info("FEC_5_6\n");
  1201. break;
  1202. case 4:
  1203. fep->code_rate_LP = FEC_7_8;
  1204. deb_info("FEC_7_8\n");
  1205. break;
  1206. }
  1207. /* guard interval */
  1208. ret =
  1209. af9005_read_register_bits(state->d, xd_g_reg_tpsd_gi,
  1210. reg_tpsd_gi_pos, reg_tpsd_gi_len, &temp);
  1211. if (ret)
  1212. return ret;
  1213. deb_info("GUARD INTERVAL ");
  1214. switch (temp) {
  1215. case 0:
  1216. fep->guard_interval = GUARD_INTERVAL_1_32;
  1217. deb_info("1_32\n");
  1218. break;
  1219. case 1:
  1220. fep->guard_interval = GUARD_INTERVAL_1_16;
  1221. deb_info("1_16\n");
  1222. break;
  1223. case 2:
  1224. fep->guard_interval = GUARD_INTERVAL_1_8;
  1225. deb_info("1_8\n");
  1226. break;
  1227. case 3:
  1228. fep->guard_interval = GUARD_INTERVAL_1_4;
  1229. deb_info("1_4\n");
  1230. break;
  1231. }
  1232. /* fft */
  1233. ret =
  1234. af9005_read_register_bits(state->d, xd_g_reg_tpsd_txmod,
  1235. reg_tpsd_txmod_pos, reg_tpsd_txmod_len,
  1236. &temp);
  1237. if (ret)
  1238. return ret;
  1239. deb_info("TRANSMISSION MODE ");
  1240. switch (temp) {
  1241. case 0:
  1242. fep->transmission_mode = TRANSMISSION_MODE_2K;
  1243. deb_info("2K\n");
  1244. break;
  1245. case 1:
  1246. fep->transmission_mode = TRANSMISSION_MODE_8K;
  1247. deb_info("8K\n");
  1248. break;
  1249. }
  1250. /* bandwidth */
  1251. ret =
  1252. af9005_read_register_bits(state->d, xd_g_reg_bw, reg_bw_pos,
  1253. reg_bw_len, &temp);
  1254. deb_info("BANDWIDTH ");
  1255. switch (temp) {
  1256. case 0:
  1257. fep->bandwidth_hz = 6000000;
  1258. deb_info("6\n");
  1259. break;
  1260. case 1:
  1261. fep->bandwidth_hz = 7000000;
  1262. deb_info("7\n");
  1263. break;
  1264. case 2:
  1265. fep->bandwidth_hz = 8000000;
  1266. deb_info("8\n");
  1267. break;
  1268. }
  1269. return 0;
  1270. }
  1271. static void af9005_fe_release(struct dvb_frontend *fe)
  1272. {
  1273. struct af9005_fe_state *state =
  1274. (struct af9005_fe_state *)fe->demodulator_priv;
  1275. kfree(state);
  1276. }
  1277. static struct dvb_frontend_ops af9005_fe_ops;
  1278. struct dvb_frontend *af9005_fe_attach(struct dvb_usb_device *d)
  1279. {
  1280. struct af9005_fe_state *state = NULL;
  1281. /* allocate memory for the internal state */
  1282. state = kzalloc(sizeof(struct af9005_fe_state), GFP_KERNEL);
  1283. if (state == NULL)
  1284. goto error;
  1285. deb_info("attaching frontend af9005\n");
  1286. state->d = d;
  1287. state->opened = 0;
  1288. memcpy(&state->frontend.ops, &af9005_fe_ops,
  1289. sizeof(struct dvb_frontend_ops));
  1290. state->frontend.demodulator_priv = state;
  1291. return &state->frontend;
  1292. error:
  1293. return NULL;
  1294. }
  1295. static struct dvb_frontend_ops af9005_fe_ops = {
  1296. .delsys = { SYS_DVBT },
  1297. .info = {
  1298. .name = "AF9005 USB DVB-T",
  1299. .frequency_min = 44250000,
  1300. .frequency_max = 867250000,
  1301. .frequency_stepsize = 250000,
  1302. .caps = FE_CAN_INVERSION_AUTO |
  1303. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1304. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1305. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  1306. FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
  1307. FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER |
  1308. FE_CAN_HIERARCHY_AUTO,
  1309. },
  1310. .release = af9005_fe_release,
  1311. .init = af9005_fe_init,
  1312. .sleep = af9005_fe_sleep,
  1313. .ts_bus_ctrl = af9005_ts_bus_ctrl,
  1314. .set_frontend = af9005_fe_set_frontend,
  1315. .get_frontend = af9005_fe_get_frontend,
  1316. .read_status = af9005_fe_read_status,
  1317. .read_ber = af9005_fe_read_ber,
  1318. .read_signal_strength = af9005_fe_read_signal_strength,
  1319. .read_snr = af9005_fe_read_snr,
  1320. .read_ucblocks = af9005_fe_read_unc_blocks,
  1321. };