af9013.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. /*
  2. * Afatech AF9013 demodulator driver
  3. *
  4. * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
  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. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/init.h>
  27. #include <linux/delay.h>
  28. #include <linux/string.h>
  29. #include <linux/slab.h>
  30. #include <linux/firmware.h>
  31. #include "dvb_frontend.h"
  32. #include "af9013_priv.h"
  33. #include "af9013.h"
  34. int af9013_debug;
  35. struct af9013_state {
  36. struct i2c_adapter *i2c;
  37. struct dvb_frontend frontend;
  38. struct af9013_config config;
  39. /* tuner/demod RF and IF AGC limits used for signal strength calc */
  40. u8 signal_strength_en, rf_50, rf_80, if_50, if_80;
  41. u16 signal_strength;
  42. u32 ber;
  43. u32 ucblocks;
  44. u16 snr;
  45. u32 frequency;
  46. unsigned long next_statistics_check;
  47. };
  48. static u8 regmask[8] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
  49. static int af9013_write_regs(struct af9013_state *state, u8 mbox, u16 reg,
  50. u8 *val, u8 len)
  51. {
  52. u8 buf[3+len];
  53. struct i2c_msg msg = {
  54. .addr = state->config.demod_address,
  55. .flags = 0,
  56. .len = sizeof(buf),
  57. .buf = buf };
  58. buf[0] = reg >> 8;
  59. buf[1] = reg & 0xff;
  60. buf[2] = mbox;
  61. memcpy(&buf[3], val, len);
  62. if (i2c_transfer(state->i2c, &msg, 1) != 1) {
  63. warn("I2C write failed reg:%04x len:%d", reg, len);
  64. return -EREMOTEIO;
  65. }
  66. return 0;
  67. }
  68. static int af9013_write_ofdm_regs(struct af9013_state *state, u16 reg, u8 *val,
  69. u8 len)
  70. {
  71. u8 mbox = (1 << 0)|(1 << 1)|((len - 1) << 2)|(0 << 6)|(0 << 7);
  72. return af9013_write_regs(state, mbox, reg, val, len);
  73. }
  74. static int af9013_write_ofsm_regs(struct af9013_state *state, u16 reg, u8 *val,
  75. u8 len)
  76. {
  77. u8 mbox = (1 << 0)|(1 << 1)|((len - 1) << 2)|(1 << 6)|(1 << 7);
  78. return af9013_write_regs(state, mbox, reg, val, len);
  79. }
  80. /* write single register */
  81. static int af9013_write_reg(struct af9013_state *state, u16 reg, u8 val)
  82. {
  83. return af9013_write_ofdm_regs(state, reg, &val, 1);
  84. }
  85. /* read single register */
  86. static int af9013_read_reg(struct af9013_state *state, u16 reg, u8 *val)
  87. {
  88. u8 obuf[3] = { reg >> 8, reg & 0xff, 0 };
  89. u8 ibuf[1];
  90. struct i2c_msg msg[2] = {
  91. {
  92. .addr = state->config.demod_address,
  93. .flags = 0,
  94. .len = sizeof(obuf),
  95. .buf = obuf
  96. }, {
  97. .addr = state->config.demod_address,
  98. .flags = I2C_M_RD,
  99. .len = sizeof(ibuf),
  100. .buf = ibuf
  101. }
  102. };
  103. if (i2c_transfer(state->i2c, msg, 2) != 2) {
  104. warn("I2C read failed reg:%04x", reg);
  105. return -EREMOTEIO;
  106. }
  107. *val = ibuf[0];
  108. return 0;
  109. }
  110. static int af9013_write_reg_bits(struct af9013_state *state, u16 reg, u8 pos,
  111. u8 len, u8 val)
  112. {
  113. int ret;
  114. u8 tmp, mask;
  115. ret = af9013_read_reg(state, reg, &tmp);
  116. if (ret)
  117. return ret;
  118. mask = regmask[len - 1] << pos;
  119. tmp = (tmp & ~mask) | ((val << pos) & mask);
  120. return af9013_write_reg(state, reg, tmp);
  121. }
  122. static int af9013_read_reg_bits(struct af9013_state *state, u16 reg, u8 pos,
  123. u8 len, u8 *val)
  124. {
  125. int ret;
  126. u8 tmp;
  127. ret = af9013_read_reg(state, reg, &tmp);
  128. if (ret)
  129. return ret;
  130. *val = (tmp >> pos) & regmask[len - 1];
  131. return 0;
  132. }
  133. static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval)
  134. {
  135. int ret;
  136. u8 pos;
  137. u16 addr;
  138. deb_info("%s: gpio:%d gpioval:%02x\n", __func__, gpio, gpioval);
  139. /* GPIO0 & GPIO1 0xd735
  140. GPIO2 & GPIO3 0xd736 */
  141. switch (gpio) {
  142. case 0:
  143. case 1:
  144. addr = 0xd735;
  145. break;
  146. case 2:
  147. case 3:
  148. addr = 0xd736;
  149. break;
  150. default:
  151. err("invalid gpio:%d\n", gpio);
  152. ret = -EINVAL;
  153. goto error;
  154. };
  155. switch (gpio) {
  156. case 0:
  157. case 2:
  158. pos = 0;
  159. break;
  160. case 1:
  161. case 3:
  162. default:
  163. pos = 4;
  164. break;
  165. };
  166. ret = af9013_write_reg_bits(state, addr, pos, 4, gpioval);
  167. error:
  168. return ret;
  169. }
  170. static u32 af913_div(u32 a, u32 b, u32 x)
  171. {
  172. u32 r = 0, c = 0, i;
  173. deb_info("%s: a:%d b:%d x:%d\n", __func__, a, b, x);
  174. if (a > b) {
  175. c = a / b;
  176. a = a - c * b;
  177. }
  178. for (i = 0; i < x; i++) {
  179. if (a >= b) {
  180. r += 1;
  181. a -= b;
  182. }
  183. a <<= 1;
  184. r <<= 1;
  185. }
  186. r = (c << (u32)x) + r;
  187. deb_info("%s: a:%d b:%d x:%d r:%d r:%x\n", __func__, a, b, x, r, r);
  188. return r;
  189. }
  190. static int af9013_set_coeff(struct af9013_state *state, fe_bandwidth_t bw)
  191. {
  192. int ret, i, j, found;
  193. deb_info("%s: adc_clock:%d bw:%d\n", __func__,
  194. state->config.adc_clock, bw);
  195. /* lookup coeff from table */
  196. for (i = 0, found = 0; i < ARRAY_SIZE(coeff_table); i++) {
  197. if (coeff_table[i].adc_clock == state->config.adc_clock &&
  198. coeff_table[i].bw == bw) {
  199. found = 1;
  200. break;
  201. }
  202. }
  203. if (!found) {
  204. err("invalid bw or clock");
  205. ret = -EINVAL;
  206. goto error;
  207. }
  208. deb_info("%s: coeff: ", __func__);
  209. debug_dump(coeff_table[i].val, sizeof(coeff_table[i].val), deb_info);
  210. /* program */
  211. for (j = 0; j < sizeof(coeff_table[i].val); j++) {
  212. ret = af9013_write_reg(state, 0xae00 + j,
  213. coeff_table[i].val[j]);
  214. if (ret)
  215. break;
  216. }
  217. error:
  218. return ret;
  219. }
  220. static int af9013_set_adc_ctrl(struct af9013_state *state)
  221. {
  222. int ret;
  223. u8 buf[3], tmp, i;
  224. u32 adc_cw;
  225. deb_info("%s: adc_clock:%d\n", __func__, state->config.adc_clock);
  226. /* adc frequency type */
  227. switch (state->config.adc_clock) {
  228. case 28800: /* 28.800 MHz */
  229. tmp = 0;
  230. break;
  231. case 20480: /* 20.480 MHz */
  232. tmp = 1;
  233. break;
  234. case 28000: /* 28.000 MHz */
  235. tmp = 2;
  236. break;
  237. case 25000: /* 25.000 MHz */
  238. tmp = 3;
  239. break;
  240. default:
  241. err("invalid xtal");
  242. return -EINVAL;
  243. }
  244. adc_cw = af913_div(state->config.adc_clock*1000, 1000000ul, 19ul);
  245. buf[0] = (u8) ((adc_cw & 0x000000ff));
  246. buf[1] = (u8) ((adc_cw & 0x0000ff00) >> 8);
  247. buf[2] = (u8) ((adc_cw & 0x00ff0000) >> 16);
  248. deb_info("%s: adc_cw:", __func__);
  249. debug_dump(buf, sizeof(buf), deb_info);
  250. /* program */
  251. for (i = 0; i < sizeof(buf); i++) {
  252. ret = af9013_write_reg(state, 0xd180 + i, buf[i]);
  253. if (ret)
  254. goto error;
  255. }
  256. ret = af9013_write_reg_bits(state, 0x9bd2, 0, 4, tmp);
  257. error:
  258. return ret;
  259. }
  260. static int af9013_set_freq_ctrl(struct af9013_state *state, fe_bandwidth_t bw)
  261. {
  262. int ret;
  263. u16 addr;
  264. u8 buf[3], i, j;
  265. u32 adc_freq, freq_cw;
  266. s8 bfs_spec_inv;
  267. int if_sample_freq;
  268. for (j = 0; j < 3; j++) {
  269. if (j == 0) {
  270. addr = 0xd140; /* fcw normal */
  271. bfs_spec_inv = state->config.rf_spec_inv ? -1 : 1;
  272. } else if (j == 1) {
  273. addr = 0x9be7; /* fcw dummy ram */
  274. bfs_spec_inv = state->config.rf_spec_inv ? -1 : 1;
  275. } else {
  276. addr = 0x9bea; /* fcw inverted */
  277. bfs_spec_inv = state->config.rf_spec_inv ? 1 : -1;
  278. }
  279. adc_freq = state->config.adc_clock * 1000;
  280. if_sample_freq = state->config.tuner_if * 1000;
  281. /* TDA18271 uses different sampling freq for every bw */
  282. if (state->config.tuner == AF9013_TUNER_TDA18271) {
  283. switch (bw) {
  284. case BANDWIDTH_6_MHZ:
  285. if_sample_freq = 3300000; /* 3.3 MHz */
  286. break;
  287. case BANDWIDTH_7_MHZ:
  288. if_sample_freq = 3500000; /* 3.5 MHz */
  289. break;
  290. case BANDWIDTH_8_MHZ:
  291. default:
  292. if_sample_freq = 4000000; /* 4.0 MHz */
  293. break;
  294. }
  295. } else if (state->config.tuner == AF9013_TUNER_TDA18218) {
  296. switch (bw) {
  297. case BANDWIDTH_6_MHZ:
  298. if_sample_freq = 3000000; /* 3 MHz */
  299. break;
  300. case BANDWIDTH_7_MHZ:
  301. if_sample_freq = 3500000; /* 3.5 MHz */
  302. break;
  303. case BANDWIDTH_8_MHZ:
  304. default:
  305. if_sample_freq = 4000000; /* 4 MHz */
  306. break;
  307. }
  308. }
  309. while (if_sample_freq > (adc_freq / 2))
  310. if_sample_freq = if_sample_freq - adc_freq;
  311. if (if_sample_freq >= 0)
  312. bfs_spec_inv = bfs_spec_inv * (-1);
  313. else
  314. if_sample_freq = if_sample_freq * (-1);
  315. freq_cw = af913_div(if_sample_freq, adc_freq, 23ul);
  316. if (bfs_spec_inv == -1)
  317. freq_cw = 0x00800000 - freq_cw;
  318. buf[0] = (u8) ((freq_cw & 0x000000ff));
  319. buf[1] = (u8) ((freq_cw & 0x0000ff00) >> 8);
  320. buf[2] = (u8) ((freq_cw & 0x007f0000) >> 16);
  321. deb_info("%s: freq_cw:", __func__);
  322. debug_dump(buf, sizeof(buf), deb_info);
  323. /* program */
  324. for (i = 0; i < sizeof(buf); i++) {
  325. ret = af9013_write_reg(state, addr++, buf[i]);
  326. if (ret)
  327. goto error;
  328. }
  329. }
  330. error:
  331. return ret;
  332. }
  333. static int af9013_set_ofdm_params(struct af9013_state *state,
  334. struct dvb_ofdm_parameters *params, u8 *auto_mode)
  335. {
  336. int ret;
  337. u8 i, buf[3] = {0, 0, 0};
  338. *auto_mode = 0; /* set if parameters are requested to auto set */
  339. /* Try auto-detect transmission parameters in case of AUTO requested or
  340. garbage parameters given by application for compatibility.
  341. MPlayer seems to provide garbage parameters currently. */
  342. switch (params->transmission_mode) {
  343. case TRANSMISSION_MODE_AUTO:
  344. *auto_mode = 1;
  345. case TRANSMISSION_MODE_2K:
  346. break;
  347. case TRANSMISSION_MODE_8K:
  348. buf[0] |= (1 << 0);
  349. break;
  350. default:
  351. deb_info("%s: invalid transmission_mode\n", __func__);
  352. *auto_mode = 1;
  353. }
  354. switch (params->guard_interval) {
  355. case GUARD_INTERVAL_AUTO:
  356. *auto_mode = 1;
  357. case GUARD_INTERVAL_1_32:
  358. break;
  359. case GUARD_INTERVAL_1_16:
  360. buf[0] |= (1 << 2);
  361. break;
  362. case GUARD_INTERVAL_1_8:
  363. buf[0] |= (2 << 2);
  364. break;
  365. case GUARD_INTERVAL_1_4:
  366. buf[0] |= (3 << 2);
  367. break;
  368. default:
  369. deb_info("%s: invalid guard_interval\n", __func__);
  370. *auto_mode = 1;
  371. }
  372. switch (params->hierarchy_information) {
  373. case HIERARCHY_AUTO:
  374. *auto_mode = 1;
  375. case HIERARCHY_NONE:
  376. break;
  377. case HIERARCHY_1:
  378. buf[0] |= (1 << 4);
  379. break;
  380. case HIERARCHY_2:
  381. buf[0] |= (2 << 4);
  382. break;
  383. case HIERARCHY_4:
  384. buf[0] |= (3 << 4);
  385. break;
  386. default:
  387. deb_info("%s: invalid hierarchy_information\n", __func__);
  388. *auto_mode = 1;
  389. };
  390. switch (params->constellation) {
  391. case QAM_AUTO:
  392. *auto_mode = 1;
  393. case QPSK:
  394. break;
  395. case QAM_16:
  396. buf[1] |= (1 << 6);
  397. break;
  398. case QAM_64:
  399. buf[1] |= (2 << 6);
  400. break;
  401. default:
  402. deb_info("%s: invalid constellation\n", __func__);
  403. *auto_mode = 1;
  404. }
  405. /* Use HP. How and which case we can switch to LP? */
  406. buf[1] |= (1 << 4);
  407. switch (params->code_rate_HP) {
  408. case FEC_AUTO:
  409. *auto_mode = 1;
  410. case FEC_1_2:
  411. break;
  412. case FEC_2_3:
  413. buf[2] |= (1 << 0);
  414. break;
  415. case FEC_3_4:
  416. buf[2] |= (2 << 0);
  417. break;
  418. case FEC_5_6:
  419. buf[2] |= (3 << 0);
  420. break;
  421. case FEC_7_8:
  422. buf[2] |= (4 << 0);
  423. break;
  424. default:
  425. deb_info("%s: invalid code_rate_HP\n", __func__);
  426. *auto_mode = 1;
  427. }
  428. switch (params->code_rate_LP) {
  429. case FEC_AUTO:
  430. /* if HIERARCHY_NONE and FEC_NONE then LP FEC is set to FEC_AUTO
  431. by dvb_frontend.c for compatibility */
  432. if (params->hierarchy_information != HIERARCHY_NONE)
  433. *auto_mode = 1;
  434. case FEC_1_2:
  435. break;
  436. case FEC_2_3:
  437. buf[2] |= (1 << 3);
  438. break;
  439. case FEC_3_4:
  440. buf[2] |= (2 << 3);
  441. break;
  442. case FEC_5_6:
  443. buf[2] |= (3 << 3);
  444. break;
  445. case FEC_7_8:
  446. buf[2] |= (4 << 3);
  447. break;
  448. case FEC_NONE:
  449. if (params->hierarchy_information == HIERARCHY_AUTO)
  450. break;
  451. default:
  452. deb_info("%s: invalid code_rate_LP\n", __func__);
  453. *auto_mode = 1;
  454. }
  455. switch (params->bandwidth) {
  456. case BANDWIDTH_6_MHZ:
  457. break;
  458. case BANDWIDTH_7_MHZ:
  459. buf[1] |= (1 << 2);
  460. break;
  461. case BANDWIDTH_8_MHZ:
  462. buf[1] |= (2 << 2);
  463. break;
  464. default:
  465. deb_info("%s: invalid bandwidth\n", __func__);
  466. buf[1] |= (2 << 2); /* cannot auto-detect BW, try 8 MHz */
  467. }
  468. /* program */
  469. for (i = 0; i < sizeof(buf); i++) {
  470. ret = af9013_write_reg(state, 0xd3c0 + i, buf[i]);
  471. if (ret)
  472. break;
  473. }
  474. return ret;
  475. }
  476. static int af9013_reset(struct af9013_state *state, u8 sleep)
  477. {
  478. int ret;
  479. u8 tmp, i;
  480. deb_info("%s\n", __func__);
  481. /* enable OFDM reset */
  482. ret = af9013_write_reg_bits(state, 0xd417, 4, 1, 1);
  483. if (ret)
  484. goto error;
  485. /* start reset mechanism */
  486. ret = af9013_write_reg(state, 0xaeff, 1);
  487. if (ret)
  488. goto error;
  489. /* reset is done when bit 1 is set */
  490. for (i = 0; i < 150; i++) {
  491. ret = af9013_read_reg_bits(state, 0xd417, 1, 1, &tmp);
  492. if (ret)
  493. goto error;
  494. if (tmp)
  495. break; /* reset done */
  496. msleep(10);
  497. }
  498. if (!tmp)
  499. return -ETIMEDOUT;
  500. /* don't clear reset when going to sleep */
  501. if (!sleep) {
  502. /* clear OFDM reset */
  503. ret = af9013_write_reg_bits(state, 0xd417, 1, 1, 0);
  504. if (ret)
  505. goto error;
  506. /* disable OFDM reset */
  507. ret = af9013_write_reg_bits(state, 0xd417, 4, 1, 0);
  508. }
  509. error:
  510. return ret;
  511. }
  512. static int af9013_power_ctrl(struct af9013_state *state, u8 onoff)
  513. {
  514. int ret;
  515. deb_info("%s: onoff:%d\n", __func__, onoff);
  516. if (onoff) {
  517. /* power on */
  518. ret = af9013_write_reg_bits(state, 0xd73a, 3, 1, 0);
  519. if (ret)
  520. goto error;
  521. ret = af9013_write_reg_bits(state, 0xd417, 1, 1, 0);
  522. if (ret)
  523. goto error;
  524. ret = af9013_write_reg_bits(state, 0xd417, 4, 1, 0);
  525. } else {
  526. /* power off */
  527. ret = af9013_reset(state, 1);
  528. if (ret)
  529. goto error;
  530. ret = af9013_write_reg_bits(state, 0xd73a, 3, 1, 1);
  531. }
  532. error:
  533. return ret;
  534. }
  535. static int af9013_lock_led(struct af9013_state *state, u8 onoff)
  536. {
  537. deb_info("%s: onoff:%d\n", __func__, onoff);
  538. return af9013_write_reg_bits(state, 0xd730, 0, 1, onoff);
  539. }
  540. static int af9013_set_frontend(struct dvb_frontend *fe,
  541. struct dvb_frontend_parameters *params)
  542. {
  543. struct af9013_state *state = fe->demodulator_priv;
  544. int ret;
  545. u8 auto_mode; /* auto set TPS */
  546. deb_info("%s: freq:%d bw:%d\n", __func__, params->frequency,
  547. params->u.ofdm.bandwidth);
  548. state->frequency = params->frequency;
  549. /* program tuner */
  550. if (fe->ops.tuner_ops.set_params)
  551. fe->ops.tuner_ops.set_params(fe, params);
  552. /* program CFOE coefficients */
  553. ret = af9013_set_coeff(state, params->u.ofdm.bandwidth);
  554. if (ret)
  555. goto error;
  556. /* program frequency control */
  557. ret = af9013_set_freq_ctrl(state, params->u.ofdm.bandwidth);
  558. if (ret)
  559. goto error;
  560. /* clear TPS lock flag (inverted flag) */
  561. ret = af9013_write_reg_bits(state, 0xd330, 3, 1, 1);
  562. if (ret)
  563. goto error;
  564. /* clear MPEG2 lock flag */
  565. ret = af9013_write_reg_bits(state, 0xd507, 6, 1, 0);
  566. if (ret)
  567. goto error;
  568. /* empty channel function */
  569. ret = af9013_write_reg_bits(state, 0x9bfe, 0, 1, 0);
  570. if (ret)
  571. goto error;
  572. /* empty DVB-T channel function */
  573. ret = af9013_write_reg_bits(state, 0x9bc2, 0, 1, 0);
  574. if (ret)
  575. goto error;
  576. /* program TPS and bandwidth, check if auto mode needed */
  577. ret = af9013_set_ofdm_params(state, &params->u.ofdm, &auto_mode);
  578. if (ret)
  579. goto error;
  580. if (auto_mode) {
  581. /* clear easy mode flag */
  582. ret = af9013_write_reg(state, 0xaefd, 0);
  583. deb_info("%s: auto TPS\n", __func__);
  584. } else {
  585. /* set easy mode flag */
  586. ret = af9013_write_reg(state, 0xaefd, 1);
  587. if (ret)
  588. goto error;
  589. ret = af9013_write_reg(state, 0xaefe, 0);
  590. deb_info("%s: manual TPS\n", __func__);
  591. }
  592. if (ret)
  593. goto error;
  594. /* everything is set, lets try to receive channel - OFSM GO! */
  595. ret = af9013_write_reg(state, 0xffff, 0);
  596. if (ret)
  597. goto error;
  598. error:
  599. return ret;
  600. }
  601. static int af9013_get_frontend(struct dvb_frontend *fe,
  602. struct dvb_frontend_parameters *p)
  603. {
  604. struct af9013_state *state = fe->demodulator_priv;
  605. int ret;
  606. u8 i, buf[3];
  607. deb_info("%s\n", __func__);
  608. /* read TPS registers */
  609. for (i = 0; i < 3; i++) {
  610. ret = af9013_read_reg(state, 0xd3c0 + i, &buf[i]);
  611. if (ret)
  612. goto error;
  613. }
  614. switch ((buf[1] >> 6) & 3) {
  615. case 0:
  616. p->u.ofdm.constellation = QPSK;
  617. break;
  618. case 1:
  619. p->u.ofdm.constellation = QAM_16;
  620. break;
  621. case 2:
  622. p->u.ofdm.constellation = QAM_64;
  623. break;
  624. }
  625. switch ((buf[0] >> 0) & 3) {
  626. case 0:
  627. p->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K;
  628. break;
  629. case 1:
  630. p->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
  631. }
  632. switch ((buf[0] >> 2) & 3) {
  633. case 0:
  634. p->u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
  635. break;
  636. case 1:
  637. p->u.ofdm.guard_interval = GUARD_INTERVAL_1_16;
  638. break;
  639. case 2:
  640. p->u.ofdm.guard_interval = GUARD_INTERVAL_1_8;
  641. break;
  642. case 3:
  643. p->u.ofdm.guard_interval = GUARD_INTERVAL_1_4;
  644. break;
  645. }
  646. switch ((buf[0] >> 4) & 7) {
  647. case 0:
  648. p->u.ofdm.hierarchy_information = HIERARCHY_NONE;
  649. break;
  650. case 1:
  651. p->u.ofdm.hierarchy_information = HIERARCHY_1;
  652. break;
  653. case 2:
  654. p->u.ofdm.hierarchy_information = HIERARCHY_2;
  655. break;
  656. case 3:
  657. p->u.ofdm.hierarchy_information = HIERARCHY_4;
  658. break;
  659. }
  660. switch ((buf[2] >> 0) & 7) {
  661. case 0:
  662. p->u.ofdm.code_rate_HP = FEC_1_2;
  663. break;
  664. case 1:
  665. p->u.ofdm.code_rate_HP = FEC_2_3;
  666. break;
  667. case 2:
  668. p->u.ofdm.code_rate_HP = FEC_3_4;
  669. break;
  670. case 3:
  671. p->u.ofdm.code_rate_HP = FEC_5_6;
  672. break;
  673. case 4:
  674. p->u.ofdm.code_rate_HP = FEC_7_8;
  675. break;
  676. }
  677. switch ((buf[2] >> 3) & 7) {
  678. case 0:
  679. p->u.ofdm.code_rate_LP = FEC_1_2;
  680. break;
  681. case 1:
  682. p->u.ofdm.code_rate_LP = FEC_2_3;
  683. break;
  684. case 2:
  685. p->u.ofdm.code_rate_LP = FEC_3_4;
  686. break;
  687. case 3:
  688. p->u.ofdm.code_rate_LP = FEC_5_6;
  689. break;
  690. case 4:
  691. p->u.ofdm.code_rate_LP = FEC_7_8;
  692. break;
  693. }
  694. switch ((buf[1] >> 2) & 3) {
  695. case 0:
  696. p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
  697. break;
  698. case 1:
  699. p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
  700. break;
  701. case 2:
  702. p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
  703. break;
  704. }
  705. p->inversion = INVERSION_AUTO;
  706. p->frequency = state->frequency;
  707. error:
  708. return ret;
  709. }
  710. static int af9013_update_ber_unc(struct dvb_frontend *fe)
  711. {
  712. struct af9013_state *state = fe->demodulator_priv;
  713. int ret;
  714. u8 buf[3], i;
  715. u32 error_bit_count = 0;
  716. u32 total_bit_count = 0;
  717. u32 abort_packet_count = 0;
  718. state->ber = 0;
  719. /* check if error bit count is ready */
  720. ret = af9013_read_reg_bits(state, 0xd391, 4, 1, &buf[0]);
  721. if (ret)
  722. goto error;
  723. if (!buf[0])
  724. goto exit;
  725. /* get RSD packet abort count */
  726. for (i = 0; i < 2; i++) {
  727. ret = af9013_read_reg(state, 0xd38a + i, &buf[i]);
  728. if (ret)
  729. goto error;
  730. }
  731. abort_packet_count = (buf[1] << 8) + buf[0];
  732. /* get error bit count */
  733. for (i = 0; i < 3; i++) {
  734. ret = af9013_read_reg(state, 0xd387 + i, &buf[i]);
  735. if (ret)
  736. goto error;
  737. }
  738. error_bit_count = (buf[2] << 16) + (buf[1] << 8) + buf[0];
  739. error_bit_count = error_bit_count - abort_packet_count * 8 * 8;
  740. /* get used RSD counting period (10000 RSD packets used) */
  741. for (i = 0; i < 2; i++) {
  742. ret = af9013_read_reg(state, 0xd385 + i, &buf[i]);
  743. if (ret)
  744. goto error;
  745. }
  746. total_bit_count = (buf[1] << 8) + buf[0];
  747. total_bit_count = total_bit_count - abort_packet_count;
  748. total_bit_count = total_bit_count * 204 * 8;
  749. if (total_bit_count)
  750. state->ber = error_bit_count * 1000000000 / total_bit_count;
  751. state->ucblocks += abort_packet_count;
  752. deb_info("%s: err bits:%d total bits:%d abort count:%d\n", __func__,
  753. error_bit_count, total_bit_count, abort_packet_count);
  754. /* set BER counting range */
  755. ret = af9013_write_reg(state, 0xd385, 10000 & 0xff);
  756. if (ret)
  757. goto error;
  758. ret = af9013_write_reg(state, 0xd386, 10000 >> 8);
  759. if (ret)
  760. goto error;
  761. /* reset and start BER counter */
  762. ret = af9013_write_reg_bits(state, 0xd391, 4, 1, 1);
  763. if (ret)
  764. goto error;
  765. exit:
  766. error:
  767. return ret;
  768. }
  769. static int af9013_update_snr(struct dvb_frontend *fe)
  770. {
  771. struct af9013_state *state = fe->demodulator_priv;
  772. int ret;
  773. u8 buf[3], i, len;
  774. u32 quant = 0;
  775. struct snr_table *uninitialized_var(snr_table);
  776. /* check if quantizer ready (for snr) */
  777. ret = af9013_read_reg_bits(state, 0xd2e1, 3, 1, &buf[0]);
  778. if (ret)
  779. goto error;
  780. if (buf[0]) {
  781. /* quantizer ready - read it */
  782. for (i = 0; i < 3; i++) {
  783. ret = af9013_read_reg(state, 0xd2e3 + i, &buf[i]);
  784. if (ret)
  785. goto error;
  786. }
  787. quant = (buf[2] << 16) + (buf[1] << 8) + buf[0];
  788. /* read current constellation */
  789. ret = af9013_read_reg(state, 0xd3c1, &buf[0]);
  790. if (ret)
  791. goto error;
  792. switch ((buf[0] >> 6) & 3) {
  793. case 0:
  794. len = ARRAY_SIZE(qpsk_snr_table);
  795. snr_table = qpsk_snr_table;
  796. break;
  797. case 1:
  798. len = ARRAY_SIZE(qam16_snr_table);
  799. snr_table = qam16_snr_table;
  800. break;
  801. case 2:
  802. len = ARRAY_SIZE(qam64_snr_table);
  803. snr_table = qam64_snr_table;
  804. break;
  805. default:
  806. len = 0;
  807. break;
  808. }
  809. if (len) {
  810. for (i = 0; i < len; i++) {
  811. if (quant < snr_table[i].val) {
  812. state->snr = snr_table[i].snr * 10;
  813. break;
  814. }
  815. }
  816. }
  817. /* set quantizer super frame count */
  818. ret = af9013_write_reg(state, 0xd2e2, 1);
  819. if (ret)
  820. goto error;
  821. /* check quantizer availability */
  822. for (i = 0; i < 10; i++) {
  823. msleep(10);
  824. ret = af9013_read_reg_bits(state, 0xd2e6, 0, 1,
  825. &buf[0]);
  826. if (ret)
  827. goto error;
  828. if (!buf[0])
  829. break;
  830. }
  831. /* reset quantizer */
  832. ret = af9013_write_reg_bits(state, 0xd2e1, 3, 1, 1);
  833. if (ret)
  834. goto error;
  835. }
  836. error:
  837. return ret;
  838. }
  839. static int af9013_update_signal_strength(struct dvb_frontend *fe)
  840. {
  841. struct af9013_state *state = fe->demodulator_priv;
  842. int ret = 0;
  843. u8 rf_gain, if_gain;
  844. int signal_strength;
  845. deb_info("%s\n", __func__);
  846. if (state->signal_strength_en) {
  847. ret = af9013_read_reg(state, 0xd07c, &rf_gain);
  848. if (ret)
  849. goto error;
  850. ret = af9013_read_reg(state, 0xd07d, &if_gain);
  851. if (ret)
  852. goto error;
  853. signal_strength = (0xffff / \
  854. (9 * (state->rf_50 + state->if_50) - \
  855. 11 * (state->rf_80 + state->if_80))) * \
  856. (10 * (rf_gain + if_gain) - \
  857. 11 * (state->rf_80 + state->if_80));
  858. if (signal_strength < 0)
  859. signal_strength = 0;
  860. else if (signal_strength > 0xffff)
  861. signal_strength = 0xffff;
  862. state->signal_strength = signal_strength;
  863. } else {
  864. state->signal_strength = 0;
  865. }
  866. error:
  867. return ret;
  868. }
  869. static int af9013_update_statistics(struct dvb_frontend *fe)
  870. {
  871. struct af9013_state *state = fe->demodulator_priv;
  872. int ret;
  873. if (time_before(jiffies, state->next_statistics_check))
  874. return 0;
  875. /* set minimum statistic update interval */
  876. state->next_statistics_check = jiffies + msecs_to_jiffies(1200);
  877. ret = af9013_update_signal_strength(fe);
  878. if (ret)
  879. goto error;
  880. ret = af9013_update_snr(fe);
  881. if (ret)
  882. goto error;
  883. ret = af9013_update_ber_unc(fe);
  884. if (ret)
  885. goto error;
  886. error:
  887. return ret;
  888. }
  889. static int af9013_get_tune_settings(struct dvb_frontend *fe,
  890. struct dvb_frontend_tune_settings *fesettings)
  891. {
  892. fesettings->min_delay_ms = 800;
  893. fesettings->step_size = 0;
  894. fesettings->max_drift = 0;
  895. return 0;
  896. }
  897. static int af9013_read_status(struct dvb_frontend *fe, fe_status_t *status)
  898. {
  899. struct af9013_state *state = fe->demodulator_priv;
  900. int ret = 0;
  901. u8 tmp;
  902. *status = 0;
  903. /* MPEG2 lock */
  904. ret = af9013_read_reg_bits(state, 0xd507, 6, 1, &tmp);
  905. if (ret)
  906. goto error;
  907. if (tmp)
  908. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
  909. FE_HAS_SYNC | FE_HAS_LOCK;
  910. if (!*status) {
  911. /* TPS lock */
  912. ret = af9013_read_reg_bits(state, 0xd330, 3, 1, &tmp);
  913. if (ret)
  914. goto error;
  915. if (tmp)
  916. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  917. FE_HAS_VITERBI;
  918. }
  919. if (!*status) {
  920. /* CFO lock */
  921. ret = af9013_read_reg_bits(state, 0xd333, 7, 1, &tmp);
  922. if (ret)
  923. goto error;
  924. if (tmp)
  925. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER;
  926. }
  927. if (!*status) {
  928. /* SFOE lock */
  929. ret = af9013_read_reg_bits(state, 0xd334, 6, 1, &tmp);
  930. if (ret)
  931. goto error;
  932. if (tmp)
  933. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER;
  934. }
  935. if (!*status) {
  936. /* AGC lock */
  937. ret = af9013_read_reg_bits(state, 0xd1a0, 6, 1, &tmp);
  938. if (ret)
  939. goto error;
  940. if (tmp)
  941. *status |= FE_HAS_SIGNAL;
  942. }
  943. ret = af9013_update_statistics(fe);
  944. error:
  945. return ret;
  946. }
  947. static int af9013_read_ber(struct dvb_frontend *fe, u32 *ber)
  948. {
  949. struct af9013_state *state = fe->demodulator_priv;
  950. int ret;
  951. ret = af9013_update_statistics(fe);
  952. *ber = state->ber;
  953. return ret;
  954. }
  955. static int af9013_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  956. {
  957. struct af9013_state *state = fe->demodulator_priv;
  958. int ret;
  959. ret = af9013_update_statistics(fe);
  960. *strength = state->signal_strength;
  961. return ret;
  962. }
  963. static int af9013_read_snr(struct dvb_frontend *fe, u16 *snr)
  964. {
  965. struct af9013_state *state = fe->demodulator_priv;
  966. int ret;
  967. ret = af9013_update_statistics(fe);
  968. *snr = state->snr;
  969. return ret;
  970. }
  971. static int af9013_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  972. {
  973. struct af9013_state *state = fe->demodulator_priv;
  974. int ret;
  975. ret = af9013_update_statistics(fe);
  976. *ucblocks = state->ucblocks;
  977. return ret;
  978. }
  979. static int af9013_sleep(struct dvb_frontend *fe)
  980. {
  981. struct af9013_state *state = fe->demodulator_priv;
  982. int ret;
  983. deb_info("%s\n", __func__);
  984. ret = af9013_lock_led(state, 0);
  985. if (ret)
  986. goto error;
  987. ret = af9013_power_ctrl(state, 0);
  988. error:
  989. return ret;
  990. }
  991. static int af9013_init(struct dvb_frontend *fe)
  992. {
  993. struct af9013_state *state = fe->demodulator_priv;
  994. int ret, i, len;
  995. u8 tmp0, tmp1;
  996. struct regdesc *init;
  997. deb_info("%s\n", __func__);
  998. /* reset OFDM */
  999. ret = af9013_reset(state, 0);
  1000. if (ret)
  1001. goto error;
  1002. /* power on */
  1003. ret = af9013_power_ctrl(state, 1);
  1004. if (ret)
  1005. goto error;
  1006. /* enable ADC */
  1007. ret = af9013_write_reg(state, 0xd73a, 0xa4);
  1008. if (ret)
  1009. goto error;
  1010. /* write API version to firmware */
  1011. for (i = 0; i < sizeof(state->config.api_version); i++) {
  1012. ret = af9013_write_reg(state, 0x9bf2 + i,
  1013. state->config.api_version[i]);
  1014. if (ret)
  1015. goto error;
  1016. }
  1017. /* program ADC control */
  1018. ret = af9013_set_adc_ctrl(state);
  1019. if (ret)
  1020. goto error;
  1021. /* set I2C master clock */
  1022. ret = af9013_write_reg(state, 0xd416, 0x14);
  1023. if (ret)
  1024. goto error;
  1025. /* set 16 embx */
  1026. ret = af9013_write_reg_bits(state, 0xd700, 1, 1, 1);
  1027. if (ret)
  1028. goto error;
  1029. /* set no trigger */
  1030. ret = af9013_write_reg_bits(state, 0xd700, 2, 1, 0);
  1031. if (ret)
  1032. goto error;
  1033. /* set read-update bit for constellation */
  1034. ret = af9013_write_reg_bits(state, 0xd371, 1, 1, 1);
  1035. if (ret)
  1036. goto error;
  1037. /* enable FEC monitor */
  1038. ret = af9013_write_reg_bits(state, 0xd392, 1, 1, 1);
  1039. if (ret)
  1040. goto error;
  1041. /* load OFSM settings */
  1042. deb_info("%s: load ofsm settings\n", __func__);
  1043. len = ARRAY_SIZE(ofsm_init);
  1044. init = ofsm_init;
  1045. for (i = 0; i < len; i++) {
  1046. ret = af9013_write_reg_bits(state, init[i].addr, init[i].pos,
  1047. init[i].len, init[i].val);
  1048. if (ret)
  1049. goto error;
  1050. }
  1051. /* load tuner specific settings */
  1052. deb_info("%s: load tuner specific settings\n", __func__);
  1053. switch (state->config.tuner) {
  1054. case AF9013_TUNER_MXL5003D:
  1055. len = ARRAY_SIZE(tuner_init_mxl5003d);
  1056. init = tuner_init_mxl5003d;
  1057. break;
  1058. case AF9013_TUNER_MXL5005D:
  1059. case AF9013_TUNER_MXL5005R:
  1060. case AF9013_TUNER_MXL5007T:
  1061. len = ARRAY_SIZE(tuner_init_mxl5005);
  1062. init = tuner_init_mxl5005;
  1063. break;
  1064. case AF9013_TUNER_ENV77H11D5:
  1065. len = ARRAY_SIZE(tuner_init_env77h11d5);
  1066. init = tuner_init_env77h11d5;
  1067. break;
  1068. case AF9013_TUNER_MT2060:
  1069. len = ARRAY_SIZE(tuner_init_mt2060);
  1070. init = tuner_init_mt2060;
  1071. break;
  1072. case AF9013_TUNER_MC44S803:
  1073. len = ARRAY_SIZE(tuner_init_mc44s803);
  1074. init = tuner_init_mc44s803;
  1075. break;
  1076. case AF9013_TUNER_QT1010:
  1077. case AF9013_TUNER_QT1010A:
  1078. len = ARRAY_SIZE(tuner_init_qt1010);
  1079. init = tuner_init_qt1010;
  1080. break;
  1081. case AF9013_TUNER_MT2060_2:
  1082. len = ARRAY_SIZE(tuner_init_mt2060_2);
  1083. init = tuner_init_mt2060_2;
  1084. break;
  1085. case AF9013_TUNER_TDA18271:
  1086. case AF9013_TUNER_TDA18218:
  1087. len = ARRAY_SIZE(tuner_init_tda18271);
  1088. init = tuner_init_tda18271;
  1089. break;
  1090. case AF9013_TUNER_UNKNOWN:
  1091. default:
  1092. len = ARRAY_SIZE(tuner_init_unknown);
  1093. init = tuner_init_unknown;
  1094. break;
  1095. }
  1096. for (i = 0; i < len; i++) {
  1097. ret = af9013_write_reg_bits(state, init[i].addr, init[i].pos,
  1098. init[i].len, init[i].val);
  1099. if (ret)
  1100. goto error;
  1101. }
  1102. /* set TS mode */
  1103. deb_info("%s: setting ts mode\n", __func__);
  1104. tmp0 = 0; /* parallel mode */
  1105. tmp1 = 0; /* serial mode */
  1106. switch (state->config.output_mode) {
  1107. case AF9013_OUTPUT_MODE_PARALLEL:
  1108. tmp0 = 1;
  1109. break;
  1110. case AF9013_OUTPUT_MODE_SERIAL:
  1111. tmp1 = 1;
  1112. break;
  1113. case AF9013_OUTPUT_MODE_USB:
  1114. /* usb mode for AF9015 */
  1115. default:
  1116. break;
  1117. }
  1118. ret = af9013_write_reg_bits(state, 0xd500, 1, 1, tmp0); /* parallel */
  1119. if (ret)
  1120. goto error;
  1121. ret = af9013_write_reg_bits(state, 0xd500, 2, 1, tmp1); /* serial */
  1122. if (ret)
  1123. goto error;
  1124. /* enable lock led */
  1125. ret = af9013_lock_led(state, 1);
  1126. if (ret)
  1127. goto error;
  1128. /* read values needed for signal strength calculation */
  1129. ret = af9013_read_reg_bits(state, 0x9bee, 0, 1,
  1130. &state->signal_strength_en);
  1131. if (ret)
  1132. goto error;
  1133. if (state->signal_strength_en) {
  1134. ret = af9013_read_reg(state, 0x9bbd, &state->rf_50);
  1135. if (ret)
  1136. goto error;
  1137. ret = af9013_read_reg(state, 0x9bd0, &state->rf_80);
  1138. if (ret)
  1139. goto error;
  1140. ret = af9013_read_reg(state, 0x9be2, &state->if_50);
  1141. if (ret)
  1142. goto error;
  1143. ret = af9013_read_reg(state, 0x9be4, &state->if_80);
  1144. if (ret)
  1145. goto error;
  1146. }
  1147. error:
  1148. return ret;
  1149. }
  1150. static struct dvb_frontend_ops af9013_ops;
  1151. static int af9013_download_firmware(struct af9013_state *state)
  1152. {
  1153. int i, len, remaining, ret;
  1154. const struct firmware *fw;
  1155. u16 checksum = 0;
  1156. u8 val;
  1157. u8 fw_params[4];
  1158. u8 *fw_file = AF9013_DEFAULT_FIRMWARE;
  1159. msleep(100);
  1160. /* check whether firmware is already running */
  1161. ret = af9013_read_reg(state, 0x98be, &val);
  1162. if (ret)
  1163. goto error;
  1164. else
  1165. deb_info("%s: firmware status:%02x\n", __func__, val);
  1166. if (val == 0x0c) /* fw is running, no need for download */
  1167. goto exit;
  1168. info("found a '%s' in cold state, will try to load a firmware",
  1169. af9013_ops.info.name);
  1170. /* request the firmware, this will block and timeout */
  1171. ret = request_firmware(&fw, fw_file, state->i2c->dev.parent);
  1172. if (ret) {
  1173. err("did not find the firmware file. (%s) "
  1174. "Please see linux/Documentation/dvb/ for more details" \
  1175. " on firmware-problems. (%d)",
  1176. fw_file, ret);
  1177. goto error;
  1178. }
  1179. info("downloading firmware from file '%s'", fw_file);
  1180. /* calc checksum */
  1181. for (i = 0; i < fw->size; i++)
  1182. checksum += fw->data[i];
  1183. fw_params[0] = checksum >> 8;
  1184. fw_params[1] = checksum & 0xff;
  1185. fw_params[2] = fw->size >> 8;
  1186. fw_params[3] = fw->size & 0xff;
  1187. /* write fw checksum & size */
  1188. ret = af9013_write_ofsm_regs(state, 0x50fc,
  1189. fw_params, sizeof(fw_params));
  1190. if (ret)
  1191. goto error_release;
  1192. #define FW_ADDR 0x5100 /* firmware start address */
  1193. #define LEN_MAX 16 /* max packet size */
  1194. for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
  1195. len = remaining;
  1196. if (len > LEN_MAX)
  1197. len = LEN_MAX;
  1198. ret = af9013_write_ofsm_regs(state,
  1199. FW_ADDR + fw->size - remaining,
  1200. (u8 *) &fw->data[fw->size - remaining], len);
  1201. if (ret) {
  1202. err("firmware download failed:%d", ret);
  1203. goto error_release;
  1204. }
  1205. }
  1206. /* request boot firmware */
  1207. ret = af9013_write_reg(state, 0xe205, 1);
  1208. if (ret)
  1209. goto error_release;
  1210. for (i = 0; i < 15; i++) {
  1211. msleep(100);
  1212. /* check firmware status */
  1213. ret = af9013_read_reg(state, 0x98be, &val);
  1214. if (ret)
  1215. goto error_release;
  1216. deb_info("%s: firmware status:%02x\n", __func__, val);
  1217. if (val == 0x0c || val == 0x04) /* success or fail */
  1218. break;
  1219. }
  1220. if (val == 0x04) {
  1221. err("firmware did not run");
  1222. ret = -1;
  1223. } else if (val != 0x0c) {
  1224. err("firmware boot timeout");
  1225. ret = -1;
  1226. }
  1227. error_release:
  1228. release_firmware(fw);
  1229. error:
  1230. exit:
  1231. if (!ret)
  1232. info("found a '%s' in warm state.", af9013_ops.info.name);
  1233. return ret;
  1234. }
  1235. static int af9013_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  1236. {
  1237. int ret;
  1238. struct af9013_state *state = fe->demodulator_priv;
  1239. deb_info("%s: enable:%d\n", __func__, enable);
  1240. if (state->config.output_mode == AF9013_OUTPUT_MODE_USB)
  1241. ret = af9013_write_reg_bits(state, 0xd417, 3, 1, enable);
  1242. else
  1243. ret = af9013_write_reg_bits(state, 0xd607, 2, 1, enable);
  1244. return ret;
  1245. }
  1246. static void af9013_release(struct dvb_frontend *fe)
  1247. {
  1248. struct af9013_state *state = fe->demodulator_priv;
  1249. kfree(state);
  1250. }
  1251. static struct dvb_frontend_ops af9013_ops;
  1252. struct dvb_frontend *af9013_attach(const struct af9013_config *config,
  1253. struct i2c_adapter *i2c)
  1254. {
  1255. int ret;
  1256. struct af9013_state *state = NULL;
  1257. u8 buf[4], i;
  1258. /* allocate memory for the internal state */
  1259. state = kzalloc(sizeof(struct af9013_state), GFP_KERNEL);
  1260. if (state == NULL)
  1261. goto error;
  1262. /* setup the state */
  1263. state->i2c = i2c;
  1264. memcpy(&state->config, config, sizeof(struct af9013_config));
  1265. /* download firmware */
  1266. if (state->config.output_mode != AF9013_OUTPUT_MODE_USB) {
  1267. ret = af9013_download_firmware(state);
  1268. if (ret)
  1269. goto error;
  1270. }
  1271. /* firmware version */
  1272. for (i = 0; i < 4; i++) {
  1273. ret = af9013_read_reg(state, 0x5103 + i, &buf[i]);
  1274. if (ret)
  1275. goto error;
  1276. }
  1277. info("firmware version:%d.%d.%d.%d", buf[0], buf[1], buf[2], buf[3]);
  1278. /* chip version */
  1279. ret = af9013_read_reg_bits(state, 0xd733, 4, 4, &buf[2]);
  1280. if (ret)
  1281. goto error;
  1282. /* ROM version */
  1283. for (i = 0; i < 2; i++) {
  1284. ret = af9013_read_reg(state, 0x116b + i, &buf[i]);
  1285. if (ret)
  1286. goto error;
  1287. }
  1288. deb_info("%s: chip version:%d ROM version:%d.%d\n", __func__,
  1289. buf[2], buf[0], buf[1]);
  1290. /* settings for mp2if */
  1291. if (state->config.output_mode == AF9013_OUTPUT_MODE_USB) {
  1292. /* AF9015 split PSB to 1.5k + 0.5k */
  1293. ret = af9013_write_reg_bits(state, 0xd50b, 2, 1, 1);
  1294. } else {
  1295. /* AF9013 change the output bit to data7 */
  1296. ret = af9013_write_reg_bits(state, 0xd500, 3, 1, 1);
  1297. if (ret)
  1298. goto error;
  1299. /* AF9013 set mpeg to full speed */
  1300. ret = af9013_write_reg_bits(state, 0xd502, 4, 1, 1);
  1301. }
  1302. if (ret)
  1303. goto error;
  1304. ret = af9013_write_reg_bits(state, 0xd520, 4, 1, 1);
  1305. if (ret)
  1306. goto error;
  1307. /* set GPIOs */
  1308. for (i = 0; i < sizeof(state->config.gpio); i++) {
  1309. ret = af9013_set_gpio(state, i, state->config.gpio[i]);
  1310. if (ret)
  1311. goto error;
  1312. }
  1313. /* create dvb_frontend */
  1314. memcpy(&state->frontend.ops, &af9013_ops,
  1315. sizeof(struct dvb_frontend_ops));
  1316. state->frontend.demodulator_priv = state;
  1317. return &state->frontend;
  1318. error:
  1319. kfree(state);
  1320. return NULL;
  1321. }
  1322. EXPORT_SYMBOL(af9013_attach);
  1323. static struct dvb_frontend_ops af9013_ops = {
  1324. .info = {
  1325. .name = "Afatech AF9013 DVB-T",
  1326. .type = FE_OFDM,
  1327. .frequency_min = 174000000,
  1328. .frequency_max = 862000000,
  1329. .frequency_stepsize = 250000,
  1330. .frequency_tolerance = 0,
  1331. .caps =
  1332. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1333. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1334. FE_CAN_QPSK | FE_CAN_QAM_16 |
  1335. FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  1336. FE_CAN_TRANSMISSION_MODE_AUTO |
  1337. FE_CAN_GUARD_INTERVAL_AUTO |
  1338. FE_CAN_HIERARCHY_AUTO |
  1339. FE_CAN_RECOVER |
  1340. FE_CAN_MUTE_TS
  1341. },
  1342. .release = af9013_release,
  1343. .init = af9013_init,
  1344. .sleep = af9013_sleep,
  1345. .i2c_gate_ctrl = af9013_i2c_gate_ctrl,
  1346. .set_frontend = af9013_set_frontend,
  1347. .get_frontend = af9013_get_frontend,
  1348. .get_tune_settings = af9013_get_tune_settings,
  1349. .read_status = af9013_read_status,
  1350. .read_ber = af9013_read_ber,
  1351. .read_signal_strength = af9013_read_signal_strength,
  1352. .read_snr = af9013_read_snr,
  1353. .read_ucblocks = af9013_read_ucblocks,
  1354. };
  1355. module_param_named(debug, af9013_debug, int, 0644);
  1356. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  1357. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1358. MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver");
  1359. MODULE_LICENSE("GPL");