af9033.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*
  2. * Afatech AF9033 demodulator driver
  3. *
  4. * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
  5. * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include "af9033_priv.h"
  22. struct af9033_state {
  23. struct i2c_adapter *i2c;
  24. struct dvb_frontend fe;
  25. struct af9033_config cfg;
  26. u32 bandwidth_hz;
  27. bool ts_mode_parallel;
  28. bool ts_mode_serial;
  29. };
  30. /* write multiple registers */
  31. static int af9033_wr_regs(struct af9033_state *state, u32 reg, const u8 *val,
  32. int len)
  33. {
  34. int ret;
  35. u8 buf[3 + len];
  36. struct i2c_msg msg[1] = {
  37. {
  38. .addr = state->cfg.i2c_addr,
  39. .flags = 0,
  40. .len = sizeof(buf),
  41. .buf = buf,
  42. }
  43. };
  44. buf[0] = (reg >> 16) & 0xff;
  45. buf[1] = (reg >> 8) & 0xff;
  46. buf[2] = (reg >> 0) & 0xff;
  47. memcpy(&buf[3], val, len);
  48. ret = i2c_transfer(state->i2c, msg, 1);
  49. if (ret == 1) {
  50. ret = 0;
  51. } else {
  52. printk(KERN_WARNING "%s: i2c wr failed=%d reg=%06x len=%d\n",
  53. __func__, ret, reg, len);
  54. ret = -EREMOTEIO;
  55. }
  56. return ret;
  57. }
  58. /* read multiple registers */
  59. static int af9033_rd_regs(struct af9033_state *state, u32 reg, u8 *val, int len)
  60. {
  61. int ret;
  62. u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff,
  63. (reg >> 0) & 0xff };
  64. struct i2c_msg msg[2] = {
  65. {
  66. .addr = state->cfg.i2c_addr,
  67. .flags = 0,
  68. .len = sizeof(buf),
  69. .buf = buf
  70. }, {
  71. .addr = state->cfg.i2c_addr,
  72. .flags = I2C_M_RD,
  73. .len = len,
  74. .buf = val
  75. }
  76. };
  77. ret = i2c_transfer(state->i2c, msg, 2);
  78. if (ret == 2) {
  79. ret = 0;
  80. } else {
  81. printk(KERN_WARNING "%s: i2c rd failed=%d reg=%06x len=%d\n",
  82. __func__, ret, reg, len);
  83. ret = -EREMOTEIO;
  84. }
  85. return ret;
  86. }
  87. /* write single register */
  88. static int af9033_wr_reg(struct af9033_state *state, u32 reg, u8 val)
  89. {
  90. return af9033_wr_regs(state, reg, &val, 1);
  91. }
  92. /* read single register */
  93. static int af9033_rd_reg(struct af9033_state *state, u32 reg, u8 *val)
  94. {
  95. return af9033_rd_regs(state, reg, val, 1);
  96. }
  97. /* write single register with mask */
  98. static int af9033_wr_reg_mask(struct af9033_state *state, u32 reg, u8 val,
  99. u8 mask)
  100. {
  101. int ret;
  102. u8 tmp;
  103. /* no need for read if whole reg is written */
  104. if (mask != 0xff) {
  105. ret = af9033_rd_regs(state, reg, &tmp, 1);
  106. if (ret)
  107. return ret;
  108. val &= mask;
  109. tmp &= ~mask;
  110. val |= tmp;
  111. }
  112. return af9033_wr_regs(state, reg, &val, 1);
  113. }
  114. /* read single register with mask */
  115. static int af9033_rd_reg_mask(struct af9033_state *state, u32 reg, u8 *val,
  116. u8 mask)
  117. {
  118. int ret, i;
  119. u8 tmp;
  120. ret = af9033_rd_regs(state, reg, &tmp, 1);
  121. if (ret)
  122. return ret;
  123. tmp &= mask;
  124. /* find position of the first bit */
  125. for (i = 0; i < 8; i++) {
  126. if ((mask >> i) & 0x01)
  127. break;
  128. }
  129. *val = tmp >> i;
  130. return 0;
  131. }
  132. static u32 af9033_div(u32 a, u32 b, u32 x)
  133. {
  134. u32 r = 0, c = 0, i;
  135. pr_debug("%s: a=%d b=%d x=%d\n", __func__, a, b, x);
  136. if (a > b) {
  137. c = a / b;
  138. a = a - c * b;
  139. }
  140. for (i = 0; i < x; i++) {
  141. if (a >= b) {
  142. r += 1;
  143. a -= b;
  144. }
  145. a <<= 1;
  146. r <<= 1;
  147. }
  148. r = (c << (u32)x) + r;
  149. pr_debug("%s: a=%d b=%d x=%d r=%d r=%x\n", __func__, a, b, x, r, r);
  150. return r;
  151. }
  152. static void af9033_release(struct dvb_frontend *fe)
  153. {
  154. struct af9033_state *state = fe->demodulator_priv;
  155. kfree(state);
  156. }
  157. static int af9033_init(struct dvb_frontend *fe)
  158. {
  159. struct af9033_state *state = fe->demodulator_priv;
  160. int ret, i, len;
  161. const struct reg_val *init;
  162. u8 buf[4];
  163. u32 adc_cw, clock_cw;
  164. struct reg_val_mask tab[] = {
  165. { 0x80fb24, 0x00, 0x08 },
  166. { 0x80004c, 0x00, 0xff },
  167. { 0x00f641, state->cfg.tuner, 0xff },
  168. { 0x80f5ca, 0x01, 0x01 },
  169. { 0x80f715, 0x01, 0x01 },
  170. { 0x00f41f, 0x04, 0x04 },
  171. { 0x00f41a, 0x01, 0x01 },
  172. { 0x80f731, 0x00, 0x01 },
  173. { 0x00d91e, 0x00, 0x01 },
  174. { 0x00d919, 0x00, 0x01 },
  175. { 0x80f732, 0x00, 0x01 },
  176. { 0x00d91f, 0x00, 0x01 },
  177. { 0x00d91a, 0x00, 0x01 },
  178. { 0x80f730, 0x00, 0x01 },
  179. { 0x80f778, 0x00, 0xff },
  180. { 0x80f73c, 0x01, 0x01 },
  181. { 0x80f776, 0x00, 0x01 },
  182. { 0x00d8fd, 0x01, 0xff },
  183. { 0x00d830, 0x01, 0xff },
  184. { 0x00d831, 0x00, 0xff },
  185. { 0x00d832, 0x00, 0xff },
  186. { 0x80f985, state->ts_mode_serial, 0x01 },
  187. { 0x80f986, state->ts_mode_parallel, 0x01 },
  188. { 0x00d827, 0x00, 0xff },
  189. { 0x00d829, 0x00, 0xff },
  190. };
  191. /* program clock control */
  192. clock_cw = af9033_div(state->cfg.clock, 1000000ul, 19ul);
  193. buf[0] = (clock_cw >> 0) & 0xff;
  194. buf[1] = (clock_cw >> 8) & 0xff;
  195. buf[2] = (clock_cw >> 16) & 0xff;
  196. buf[3] = (clock_cw >> 24) & 0xff;
  197. pr_debug("%s: clock=%d clock_cw=%08x\n", __func__, state->cfg.clock,
  198. clock_cw);
  199. ret = af9033_wr_regs(state, 0x800025, buf, 4);
  200. if (ret < 0)
  201. goto err;
  202. /* program ADC control */
  203. for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
  204. if (clock_adc_lut[i].clock == state->cfg.clock)
  205. break;
  206. }
  207. adc_cw = af9033_div(clock_adc_lut[i].adc, 1000000ul, 19ul);
  208. buf[0] = (adc_cw >> 0) & 0xff;
  209. buf[1] = (adc_cw >> 8) & 0xff;
  210. buf[2] = (adc_cw >> 16) & 0xff;
  211. pr_debug("%s: adc=%d adc_cw=%06x\n", __func__, clock_adc_lut[i].adc,
  212. adc_cw);
  213. ret = af9033_wr_regs(state, 0x80f1cd, buf, 3);
  214. if (ret < 0)
  215. goto err;
  216. /* program register table */
  217. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  218. ret = af9033_wr_reg_mask(state, tab[i].reg, tab[i].val,
  219. tab[i].mask);
  220. if (ret < 0)
  221. goto err;
  222. }
  223. /* settings for TS interface */
  224. if (state->cfg.ts_mode == AF9033_TS_MODE_USB) {
  225. ret = af9033_wr_reg_mask(state, 0x80f9a5, 0x00, 0x01);
  226. if (ret < 0)
  227. goto err;
  228. ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x01, 0x01);
  229. if (ret < 0)
  230. goto err;
  231. } else {
  232. ret = af9033_wr_reg_mask(state, 0x80f990, 0x00, 0x01);
  233. if (ret < 0)
  234. goto err;
  235. ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x00, 0x01);
  236. if (ret < 0)
  237. goto err;
  238. }
  239. /* load OFSM settings */
  240. pr_debug("%s: load ofsm settings\n", __func__);
  241. len = ARRAY_SIZE(ofsm_init);
  242. init = ofsm_init;
  243. for (i = 0; i < len; i++) {
  244. ret = af9033_wr_reg(state, init[i].reg, init[i].val);
  245. if (ret < 0)
  246. goto err;
  247. }
  248. /* load tuner specific settings */
  249. pr_debug("%s: load tuner specific settings\n",
  250. __func__);
  251. switch (state->cfg.tuner) {
  252. case AF9033_TUNER_TUA9001:
  253. len = ARRAY_SIZE(tuner_init_tua9001);
  254. init = tuner_init_tua9001;
  255. break;
  256. case AF9033_TUNER_FC0011:
  257. len = ARRAY_SIZE(tuner_init_fc0011);
  258. init = tuner_init_fc0011;
  259. break;
  260. case AF9033_TUNER_MXL5007T:
  261. len = ARRAY_SIZE(tuner_init_mxl5007t);
  262. init = tuner_init_mxl5007t;
  263. break;
  264. case AF9033_TUNER_TDA18218:
  265. len = ARRAY_SIZE(tuner_init_tda18218);
  266. init = tuner_init_tda18218;
  267. break;
  268. default:
  269. pr_debug("%s: unsupported tuner ID=%d\n", __func__,
  270. state->cfg.tuner);
  271. ret = -ENODEV;
  272. goto err;
  273. }
  274. for (i = 0; i < len; i++) {
  275. ret = af9033_wr_reg(state, init[i].reg, init[i].val);
  276. if (ret < 0)
  277. goto err;
  278. }
  279. state->bandwidth_hz = 0; /* force to program all parameters */
  280. return 0;
  281. err:
  282. pr_debug("%s: failed=%d\n", __func__, ret);
  283. return ret;
  284. }
  285. static int af9033_sleep(struct dvb_frontend *fe)
  286. {
  287. struct af9033_state *state = fe->demodulator_priv;
  288. int ret, i;
  289. u8 tmp;
  290. ret = af9033_wr_reg(state, 0x80004c, 1);
  291. if (ret < 0)
  292. goto err;
  293. ret = af9033_wr_reg(state, 0x800000, 0);
  294. if (ret < 0)
  295. goto err;
  296. for (i = 100, tmp = 1; i && tmp; i--) {
  297. ret = af9033_rd_reg(state, 0x80004c, &tmp);
  298. if (ret < 0)
  299. goto err;
  300. usleep_range(200, 10000);
  301. }
  302. pr_debug("%s: loop=%d\n", __func__, i);
  303. if (i == 0) {
  304. ret = -ETIMEDOUT;
  305. goto err;
  306. }
  307. ret = af9033_wr_reg_mask(state, 0x80fb24, 0x08, 0x08);
  308. if (ret < 0)
  309. goto err;
  310. /* prevent current leak (?) */
  311. if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
  312. /* enable parallel TS */
  313. ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01);
  314. if (ret < 0)
  315. goto err;
  316. ret = af9033_wr_reg_mask(state, 0x00d916, 0x01, 0x01);
  317. if (ret < 0)
  318. goto err;
  319. }
  320. return 0;
  321. err:
  322. pr_debug("%s: failed=%d\n", __func__, ret);
  323. return ret;
  324. }
  325. static int af9033_get_tune_settings(struct dvb_frontend *fe,
  326. struct dvb_frontend_tune_settings *fesettings)
  327. {
  328. fesettings->min_delay_ms = 800;
  329. fesettings->step_size = 0;
  330. fesettings->max_drift = 0;
  331. return 0;
  332. }
  333. static int af9033_set_frontend(struct dvb_frontend *fe)
  334. {
  335. struct af9033_state *state = fe->demodulator_priv;
  336. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  337. int ret, i, spec_inv;
  338. u8 tmp, buf[3], bandwidth_reg_val;
  339. u32 if_frequency, freq_cw, adc_freq;
  340. pr_debug("%s: frequency=%d bandwidth_hz=%d\n", __func__, c->frequency,
  341. c->bandwidth_hz);
  342. /* check bandwidth */
  343. switch (c->bandwidth_hz) {
  344. case 6000000:
  345. bandwidth_reg_val = 0x00;
  346. break;
  347. case 7000000:
  348. bandwidth_reg_val = 0x01;
  349. break;
  350. case 8000000:
  351. bandwidth_reg_val = 0x02;
  352. break;
  353. default:
  354. pr_debug("%s: invalid bandwidth_hz\n", __func__);
  355. ret = -EINVAL;
  356. goto err;
  357. }
  358. /* program tuner */
  359. if (fe->ops.tuner_ops.set_params)
  360. fe->ops.tuner_ops.set_params(fe);
  361. /* program CFOE coefficients */
  362. if (c->bandwidth_hz != state->bandwidth_hz) {
  363. for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
  364. if (coeff_lut[i].clock == state->cfg.clock &&
  365. coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
  366. break;
  367. }
  368. }
  369. ret = af9033_wr_regs(state, 0x800001,
  370. coeff_lut[i].val, sizeof(coeff_lut[i].val));
  371. }
  372. /* program frequency control */
  373. if (c->bandwidth_hz != state->bandwidth_hz) {
  374. spec_inv = state->cfg.spec_inv ? -1 : 1;
  375. for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
  376. if (clock_adc_lut[i].clock == state->cfg.clock)
  377. break;
  378. }
  379. adc_freq = clock_adc_lut[i].adc;
  380. /* get used IF frequency */
  381. if (fe->ops.tuner_ops.get_if_frequency)
  382. fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
  383. else
  384. if_frequency = 0;
  385. while (if_frequency > (adc_freq / 2))
  386. if_frequency -= adc_freq;
  387. if (if_frequency >= 0)
  388. spec_inv *= -1;
  389. else
  390. if_frequency *= -1;
  391. freq_cw = af9033_div(if_frequency, adc_freq, 23ul);
  392. if (spec_inv == -1)
  393. freq_cw *= -1;
  394. /* get adc multiplies */
  395. ret = af9033_rd_reg(state, 0x800045, &tmp);
  396. if (ret < 0)
  397. goto err;
  398. if (tmp == 1)
  399. freq_cw /= 2;
  400. buf[0] = (freq_cw >> 0) & 0xff;
  401. buf[1] = (freq_cw >> 8) & 0xff;
  402. buf[2] = (freq_cw >> 16) & 0x7f;
  403. ret = af9033_wr_regs(state, 0x800029, buf, 3);
  404. if (ret < 0)
  405. goto err;
  406. state->bandwidth_hz = c->bandwidth_hz;
  407. }
  408. ret = af9033_wr_reg_mask(state, 0x80f904, bandwidth_reg_val, 0x03);
  409. if (ret < 0)
  410. goto err;
  411. ret = af9033_wr_reg(state, 0x800040, 0x00);
  412. if (ret < 0)
  413. goto err;
  414. ret = af9033_wr_reg(state, 0x800047, 0x00);
  415. if (ret < 0)
  416. goto err;
  417. ret = af9033_wr_reg_mask(state, 0x80f999, 0x00, 0x01);
  418. if (ret < 0)
  419. goto err;
  420. if (c->frequency <= 230000000)
  421. tmp = 0x00; /* VHF */
  422. else
  423. tmp = 0x01; /* UHF */
  424. ret = af9033_wr_reg(state, 0x80004b, tmp);
  425. if (ret < 0)
  426. goto err;
  427. ret = af9033_wr_reg(state, 0x800000, 0x00);
  428. if (ret < 0)
  429. goto err;
  430. return 0;
  431. err:
  432. pr_debug("%s: failed=%d\n", __func__, ret);
  433. return ret;
  434. }
  435. static int af9033_get_frontend(struct dvb_frontend *fe)
  436. {
  437. struct af9033_state *state = fe->demodulator_priv;
  438. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  439. int ret;
  440. u8 buf[8];
  441. pr_debug("%s\n", __func__);
  442. /* read all needed registers */
  443. ret = af9033_rd_regs(state, 0x80f900, buf, sizeof(buf));
  444. if (ret < 0)
  445. goto err;
  446. switch ((buf[0] >> 0) & 3) {
  447. case 0:
  448. c->transmission_mode = TRANSMISSION_MODE_2K;
  449. break;
  450. case 1:
  451. c->transmission_mode = TRANSMISSION_MODE_8K;
  452. break;
  453. }
  454. switch ((buf[1] >> 0) & 3) {
  455. case 0:
  456. c->guard_interval = GUARD_INTERVAL_1_32;
  457. break;
  458. case 1:
  459. c->guard_interval = GUARD_INTERVAL_1_16;
  460. break;
  461. case 2:
  462. c->guard_interval = GUARD_INTERVAL_1_8;
  463. break;
  464. case 3:
  465. c->guard_interval = GUARD_INTERVAL_1_4;
  466. break;
  467. }
  468. switch ((buf[2] >> 0) & 7) {
  469. case 0:
  470. c->hierarchy = HIERARCHY_NONE;
  471. break;
  472. case 1:
  473. c->hierarchy = HIERARCHY_1;
  474. break;
  475. case 2:
  476. c->hierarchy = HIERARCHY_2;
  477. break;
  478. case 3:
  479. c->hierarchy = HIERARCHY_4;
  480. break;
  481. }
  482. switch ((buf[3] >> 0) & 3) {
  483. case 0:
  484. c->modulation = QPSK;
  485. break;
  486. case 1:
  487. c->modulation = QAM_16;
  488. break;
  489. case 2:
  490. c->modulation = QAM_64;
  491. break;
  492. }
  493. switch ((buf[4] >> 0) & 3) {
  494. case 0:
  495. c->bandwidth_hz = 6000000;
  496. break;
  497. case 1:
  498. c->bandwidth_hz = 7000000;
  499. break;
  500. case 2:
  501. c->bandwidth_hz = 8000000;
  502. break;
  503. }
  504. switch ((buf[6] >> 0) & 7) {
  505. case 0:
  506. c->code_rate_HP = FEC_1_2;
  507. break;
  508. case 1:
  509. c->code_rate_HP = FEC_2_3;
  510. break;
  511. case 2:
  512. c->code_rate_HP = FEC_3_4;
  513. break;
  514. case 3:
  515. c->code_rate_HP = FEC_5_6;
  516. break;
  517. case 4:
  518. c->code_rate_HP = FEC_7_8;
  519. break;
  520. case 5:
  521. c->code_rate_HP = FEC_NONE;
  522. break;
  523. }
  524. switch ((buf[7] >> 0) & 7) {
  525. case 0:
  526. c->code_rate_LP = FEC_1_2;
  527. break;
  528. case 1:
  529. c->code_rate_LP = FEC_2_3;
  530. break;
  531. case 2:
  532. c->code_rate_LP = FEC_3_4;
  533. break;
  534. case 3:
  535. c->code_rate_LP = FEC_5_6;
  536. break;
  537. case 4:
  538. c->code_rate_LP = FEC_7_8;
  539. break;
  540. case 5:
  541. c->code_rate_LP = FEC_NONE;
  542. break;
  543. }
  544. return 0;
  545. err:
  546. pr_debug("%s: failed=%d\n", __func__, ret);
  547. return ret;
  548. }
  549. static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status)
  550. {
  551. struct af9033_state *state = fe->demodulator_priv;
  552. int ret;
  553. u8 tmp;
  554. *status = 0;
  555. /* radio channel status, 0=no result, 1=has signal, 2=no signal */
  556. ret = af9033_rd_reg(state, 0x800047, &tmp);
  557. if (ret < 0)
  558. goto err;
  559. /* has signal */
  560. if (tmp == 0x01)
  561. *status |= FE_HAS_SIGNAL;
  562. if (tmp != 0x02) {
  563. /* TPS lock */
  564. ret = af9033_rd_reg_mask(state, 0x80f5a9, &tmp, 0x01);
  565. if (ret < 0)
  566. goto err;
  567. if (tmp)
  568. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  569. FE_HAS_VITERBI;
  570. /* full lock */
  571. ret = af9033_rd_reg_mask(state, 0x80f999, &tmp, 0x01);
  572. if (ret < 0)
  573. goto err;
  574. if (tmp)
  575. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  576. FE_HAS_VITERBI | FE_HAS_SYNC |
  577. FE_HAS_LOCK;
  578. }
  579. return 0;
  580. err:
  581. pr_debug("%s: failed=%d\n", __func__, ret);
  582. return ret;
  583. }
  584. static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
  585. {
  586. struct af9033_state *state = fe->demodulator_priv;
  587. int ret, i, len;
  588. u8 buf[3], tmp;
  589. u32 snr_val;
  590. const struct val_snr *uninitialized_var(snr_lut);
  591. /* read value */
  592. ret = af9033_rd_regs(state, 0x80002c, buf, 3);
  593. if (ret < 0)
  594. goto err;
  595. snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0];
  596. /* read current modulation */
  597. ret = af9033_rd_reg(state, 0x80f903, &tmp);
  598. if (ret < 0)
  599. goto err;
  600. switch ((tmp >> 0) & 3) {
  601. case 0:
  602. len = ARRAY_SIZE(qpsk_snr_lut);
  603. snr_lut = qpsk_snr_lut;
  604. break;
  605. case 1:
  606. len = ARRAY_SIZE(qam16_snr_lut);
  607. snr_lut = qam16_snr_lut;
  608. break;
  609. case 2:
  610. len = ARRAY_SIZE(qam64_snr_lut);
  611. snr_lut = qam64_snr_lut;
  612. break;
  613. default:
  614. goto err;
  615. }
  616. for (i = 0; i < len; i++) {
  617. tmp = snr_lut[i].snr;
  618. if (snr_val < snr_lut[i].val)
  619. break;
  620. }
  621. *snr = tmp * 10; /* dB/10 */
  622. return 0;
  623. err:
  624. pr_debug("%s: failed=%d\n", __func__, ret);
  625. return ret;
  626. }
  627. static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  628. {
  629. struct af9033_state *state = fe->demodulator_priv;
  630. int ret;
  631. u8 strength2;
  632. /* read signal strength of 0-100 scale */
  633. ret = af9033_rd_reg(state, 0x800048, &strength2);
  634. if (ret < 0)
  635. goto err;
  636. /* scale value to 0x0000-0xffff */
  637. *strength = strength2 * 0xffff / 100;
  638. return 0;
  639. err:
  640. pr_debug("%s: failed=%d\n", __func__, ret);
  641. return ret;
  642. }
  643. static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
  644. {
  645. *ber = 0;
  646. return 0;
  647. }
  648. static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  649. {
  650. *ucblocks = 0;
  651. return 0;
  652. }
  653. static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  654. {
  655. struct af9033_state *state = fe->demodulator_priv;
  656. int ret;
  657. pr_debug("%s: enable=%d\n", __func__, enable);
  658. ret = af9033_wr_reg_mask(state, 0x00fa04, enable, 0x01);
  659. if (ret < 0)
  660. goto err;
  661. return 0;
  662. err:
  663. pr_debug("%s: failed=%d\n", __func__, ret);
  664. return ret;
  665. }
  666. static struct dvb_frontend_ops af9033_ops;
  667. struct dvb_frontend *af9033_attach(const struct af9033_config *config,
  668. struct i2c_adapter *i2c)
  669. {
  670. int ret;
  671. struct af9033_state *state;
  672. u8 buf[8];
  673. pr_debug("%s:\n", __func__);
  674. /* allocate memory for the internal state */
  675. state = kzalloc(sizeof(struct af9033_state), GFP_KERNEL);
  676. if (state == NULL)
  677. goto err;
  678. /* setup the state */
  679. state->i2c = i2c;
  680. memcpy(&state->cfg, config, sizeof(struct af9033_config));
  681. if (state->cfg.clock != 12000000) {
  682. printk(KERN_INFO "af9033: unsupported clock=%d, only " \
  683. "12000000 Hz is supported currently\n",
  684. state->cfg.clock);
  685. goto err;
  686. }
  687. /* firmware version */
  688. ret = af9033_rd_regs(state, 0x0083e9, &buf[0], 4);
  689. if (ret < 0)
  690. goto err;
  691. ret = af9033_rd_regs(state, 0x804191, &buf[4], 4);
  692. if (ret < 0)
  693. goto err;
  694. printk(KERN_INFO "af9033: firmware version: LINK=%d.%d.%d.%d " \
  695. "OFDM=%d.%d.%d.%d\n", buf[0], buf[1], buf[2], buf[3],
  696. buf[4], buf[5], buf[6], buf[7]);
  697. /* configure internal TS mode */
  698. switch (state->cfg.ts_mode) {
  699. case AF9033_TS_MODE_PARALLEL:
  700. state->ts_mode_parallel = true;
  701. break;
  702. case AF9033_TS_MODE_SERIAL:
  703. state->ts_mode_serial = true;
  704. break;
  705. case AF9033_TS_MODE_USB:
  706. /* usb mode for AF9035 */
  707. default:
  708. break;
  709. }
  710. /* create dvb_frontend */
  711. memcpy(&state->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops));
  712. state->fe.demodulator_priv = state;
  713. return &state->fe;
  714. err:
  715. kfree(state);
  716. return NULL;
  717. }
  718. EXPORT_SYMBOL(af9033_attach);
  719. static struct dvb_frontend_ops af9033_ops = {
  720. .delsys = { SYS_DVBT },
  721. .info = {
  722. .name = "Afatech AF9033 (DVB-T)",
  723. .frequency_min = 174000000,
  724. .frequency_max = 862000000,
  725. .frequency_stepsize = 250000,
  726. .frequency_tolerance = 0,
  727. .caps = FE_CAN_FEC_1_2 |
  728. FE_CAN_FEC_2_3 |
  729. FE_CAN_FEC_3_4 |
  730. FE_CAN_FEC_5_6 |
  731. FE_CAN_FEC_7_8 |
  732. FE_CAN_FEC_AUTO |
  733. FE_CAN_QPSK |
  734. FE_CAN_QAM_16 |
  735. FE_CAN_QAM_64 |
  736. FE_CAN_QAM_AUTO |
  737. FE_CAN_TRANSMISSION_MODE_AUTO |
  738. FE_CAN_GUARD_INTERVAL_AUTO |
  739. FE_CAN_HIERARCHY_AUTO |
  740. FE_CAN_RECOVER |
  741. FE_CAN_MUTE_TS
  742. },
  743. .release = af9033_release,
  744. .init = af9033_init,
  745. .sleep = af9033_sleep,
  746. .get_tune_settings = af9033_get_tune_settings,
  747. .set_frontend = af9033_set_frontend,
  748. .get_frontend = af9033_get_frontend,
  749. .read_status = af9033_read_status,
  750. .read_snr = af9033_read_snr,
  751. .read_signal_strength = af9033_read_signal_strength,
  752. .read_ber = af9033_read_ber,
  753. .read_ucblocks = af9033_read_ucblocks,
  754. .i2c_gate_ctrl = af9033_i2c_gate_ctrl,
  755. };
  756. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  757. MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
  758. MODULE_LICENSE("GPL");