af9013.c 32 KB

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