af9005-fe.c 36 KB

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