af9033.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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. /* Max transfer size done by I2C transfer functions */
  23. #define MAX_XFER_SIZE 64
  24. struct af9033_dev {
  25. struct i2c_client *client;
  26. struct dvb_frontend fe;
  27. struct af9033_config cfg;
  28. bool is_af9035;
  29. bool is_it9135;
  30. u32 bandwidth_hz;
  31. bool ts_mode_parallel;
  32. bool ts_mode_serial;
  33. enum fe_status fe_status;
  34. u64 post_bit_error_prev; /* for old read_ber we return (curr - prev) */
  35. u64 post_bit_error;
  36. u64 post_bit_count;
  37. u64 error_block_count;
  38. u64 total_block_count;
  39. struct delayed_work stat_work;
  40. };
  41. /* write multiple registers */
  42. static int af9033_wr_regs(struct af9033_dev *dev, u32 reg, const u8 *val,
  43. int len)
  44. {
  45. int ret;
  46. u8 buf[MAX_XFER_SIZE];
  47. struct i2c_msg msg[1] = {
  48. {
  49. .addr = dev->client->addr,
  50. .flags = 0,
  51. .len = 3 + len,
  52. .buf = buf,
  53. }
  54. };
  55. if (3 + len > sizeof(buf)) {
  56. dev_warn(&dev->client->dev,
  57. "i2c wr reg=%04x: len=%d is too big!\n",
  58. reg, len);
  59. return -EINVAL;
  60. }
  61. buf[0] = (reg >> 16) & 0xff;
  62. buf[1] = (reg >> 8) & 0xff;
  63. buf[2] = (reg >> 0) & 0xff;
  64. memcpy(&buf[3], val, len);
  65. ret = i2c_transfer(dev->client->adapter, msg, 1);
  66. if (ret == 1) {
  67. ret = 0;
  68. } else {
  69. dev_warn(&dev->client->dev, "i2c wr failed=%d reg=%06x len=%d\n",
  70. ret, reg, len);
  71. ret = -EREMOTEIO;
  72. }
  73. return ret;
  74. }
  75. /* read multiple registers */
  76. static int af9033_rd_regs(struct af9033_dev *dev, u32 reg, u8 *val, int len)
  77. {
  78. int ret;
  79. u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff,
  80. (reg >> 0) & 0xff };
  81. struct i2c_msg msg[2] = {
  82. {
  83. .addr = dev->client->addr,
  84. .flags = 0,
  85. .len = sizeof(buf),
  86. .buf = buf
  87. }, {
  88. .addr = dev->client->addr,
  89. .flags = I2C_M_RD,
  90. .len = len,
  91. .buf = val
  92. }
  93. };
  94. ret = i2c_transfer(dev->client->adapter, msg, 2);
  95. if (ret == 2) {
  96. ret = 0;
  97. } else {
  98. dev_warn(&dev->client->dev, "i2c rd failed=%d reg=%06x len=%d\n",
  99. ret, reg, len);
  100. ret = -EREMOTEIO;
  101. }
  102. return ret;
  103. }
  104. /* write single register */
  105. static int af9033_wr_reg(struct af9033_dev *dev, u32 reg, u8 val)
  106. {
  107. return af9033_wr_regs(dev, reg, &val, 1);
  108. }
  109. /* read single register */
  110. static int af9033_rd_reg(struct af9033_dev *dev, u32 reg, u8 *val)
  111. {
  112. return af9033_rd_regs(dev, reg, val, 1);
  113. }
  114. /* write single register with mask */
  115. static int af9033_wr_reg_mask(struct af9033_dev *dev, u32 reg, u8 val,
  116. u8 mask)
  117. {
  118. int ret;
  119. u8 tmp;
  120. /* no need for read if whole reg is written */
  121. if (mask != 0xff) {
  122. ret = af9033_rd_regs(dev, reg, &tmp, 1);
  123. if (ret)
  124. return ret;
  125. val &= mask;
  126. tmp &= ~mask;
  127. val |= tmp;
  128. }
  129. return af9033_wr_regs(dev, reg, &val, 1);
  130. }
  131. /* read single register with mask */
  132. static int af9033_rd_reg_mask(struct af9033_dev *dev, u32 reg, u8 *val,
  133. u8 mask)
  134. {
  135. int ret, i;
  136. u8 tmp;
  137. ret = af9033_rd_regs(dev, reg, &tmp, 1);
  138. if (ret)
  139. return ret;
  140. tmp &= mask;
  141. /* find position of the first bit */
  142. for (i = 0; i < 8; i++) {
  143. if ((mask >> i) & 0x01)
  144. break;
  145. }
  146. *val = tmp >> i;
  147. return 0;
  148. }
  149. /* write reg val table using reg addr auto increment */
  150. static int af9033_wr_reg_val_tab(struct af9033_dev *dev,
  151. const struct reg_val *tab, int tab_len)
  152. {
  153. #define MAX_TAB_LEN 212
  154. int ret, i, j;
  155. u8 buf[1 + MAX_TAB_LEN];
  156. dev_dbg(&dev->client->dev, "tab_len=%d\n", tab_len);
  157. if (tab_len > sizeof(buf)) {
  158. dev_warn(&dev->client->dev, "tab len %d is too big\n", tab_len);
  159. return -EINVAL;
  160. }
  161. for (i = 0, j = 0; i < tab_len; i++) {
  162. buf[j] = tab[i].val;
  163. if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) {
  164. ret = af9033_wr_regs(dev, tab[i].reg - j, buf, j + 1);
  165. if (ret < 0)
  166. goto err;
  167. j = 0;
  168. } else {
  169. j++;
  170. }
  171. }
  172. return 0;
  173. err:
  174. dev_dbg(&dev->client->dev, "failed=%d\n", ret);
  175. return ret;
  176. }
  177. static u32 af9033_div(struct af9033_dev *dev, u32 a, u32 b, u32 x)
  178. {
  179. u32 r = 0, c = 0, i;
  180. dev_dbg(&dev->client->dev, "a=%d b=%d x=%d\n", a, b, x);
  181. if (a > b) {
  182. c = a / b;
  183. a = a - c * b;
  184. }
  185. for (i = 0; i < x; i++) {
  186. if (a >= b) {
  187. r += 1;
  188. a -= b;
  189. }
  190. a <<= 1;
  191. r <<= 1;
  192. }
  193. r = (c << (u32)x) + r;
  194. dev_dbg(&dev->client->dev, "a=%d b=%d x=%d r=%d r=%x\n", a, b, x, r, r);
  195. return r;
  196. }
  197. static int af9033_init(struct dvb_frontend *fe)
  198. {
  199. struct af9033_dev *dev = fe->demodulator_priv;
  200. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  201. int ret, i, len;
  202. const struct reg_val *init;
  203. u8 buf[4];
  204. u32 adc_cw, clock_cw;
  205. struct reg_val_mask tab[] = {
  206. { 0x80fb24, 0x00, 0x08 },
  207. { 0x80004c, 0x00, 0xff },
  208. { 0x00f641, dev->cfg.tuner, 0xff },
  209. { 0x80f5ca, 0x01, 0x01 },
  210. { 0x80f715, 0x01, 0x01 },
  211. { 0x00f41f, 0x04, 0x04 },
  212. { 0x00f41a, 0x01, 0x01 },
  213. { 0x80f731, 0x00, 0x01 },
  214. { 0x00d91e, 0x00, 0x01 },
  215. { 0x00d919, 0x00, 0x01 },
  216. { 0x80f732, 0x00, 0x01 },
  217. { 0x00d91f, 0x00, 0x01 },
  218. { 0x00d91a, 0x00, 0x01 },
  219. { 0x80f730, 0x00, 0x01 },
  220. { 0x80f778, 0x00, 0xff },
  221. { 0x80f73c, 0x01, 0x01 },
  222. { 0x80f776, 0x00, 0x01 },
  223. { 0x00d8fd, 0x01, 0xff },
  224. { 0x00d830, 0x01, 0xff },
  225. { 0x00d831, 0x00, 0xff },
  226. { 0x00d832, 0x00, 0xff },
  227. { 0x80f985, dev->ts_mode_serial, 0x01 },
  228. { 0x80f986, dev->ts_mode_parallel, 0x01 },
  229. { 0x00d827, 0x00, 0xff },
  230. { 0x00d829, 0x00, 0xff },
  231. { 0x800045, dev->cfg.adc_multiplier, 0xff },
  232. };
  233. /* program clock control */
  234. clock_cw = af9033_div(dev, dev->cfg.clock, 1000000ul, 19ul);
  235. buf[0] = (clock_cw >> 0) & 0xff;
  236. buf[1] = (clock_cw >> 8) & 0xff;
  237. buf[2] = (clock_cw >> 16) & 0xff;
  238. buf[3] = (clock_cw >> 24) & 0xff;
  239. dev_dbg(&dev->client->dev, "clock=%d clock_cw=%08x\n",
  240. dev->cfg.clock, clock_cw);
  241. ret = af9033_wr_regs(dev, 0x800025, buf, 4);
  242. if (ret < 0)
  243. goto err;
  244. /* program ADC control */
  245. for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
  246. if (clock_adc_lut[i].clock == dev->cfg.clock)
  247. break;
  248. }
  249. if (i == ARRAY_SIZE(clock_adc_lut)) {
  250. dev_err(&dev->client->dev,
  251. "Couldn't find ADC config for clock=%d\n",
  252. dev->cfg.clock);
  253. goto err;
  254. }
  255. adc_cw = af9033_div(dev, clock_adc_lut[i].adc, 1000000ul, 19ul);
  256. buf[0] = (adc_cw >> 0) & 0xff;
  257. buf[1] = (adc_cw >> 8) & 0xff;
  258. buf[2] = (adc_cw >> 16) & 0xff;
  259. dev_dbg(&dev->client->dev, "adc=%d adc_cw=%06x\n",
  260. clock_adc_lut[i].adc, adc_cw);
  261. ret = af9033_wr_regs(dev, 0x80f1cd, buf, 3);
  262. if (ret < 0)
  263. goto err;
  264. /* program register table */
  265. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  266. ret = af9033_wr_reg_mask(dev, tab[i].reg, tab[i].val,
  267. tab[i].mask);
  268. if (ret < 0)
  269. goto err;
  270. }
  271. /* clock output */
  272. if (dev->cfg.dyn0_clk) {
  273. ret = af9033_wr_reg(dev, 0x80fba8, 0x00);
  274. if (ret < 0)
  275. goto err;
  276. }
  277. /* settings for TS interface */
  278. if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) {
  279. ret = af9033_wr_reg_mask(dev, 0x80f9a5, 0x00, 0x01);
  280. if (ret < 0)
  281. goto err;
  282. ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x01, 0x01);
  283. if (ret < 0)
  284. goto err;
  285. } else {
  286. ret = af9033_wr_reg_mask(dev, 0x80f990, 0x00, 0x01);
  287. if (ret < 0)
  288. goto err;
  289. ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x00, 0x01);
  290. if (ret < 0)
  291. goto err;
  292. }
  293. /* load OFSM settings */
  294. dev_dbg(&dev->client->dev, "load ofsm settings\n");
  295. switch (dev->cfg.tuner) {
  296. case AF9033_TUNER_IT9135_38:
  297. case AF9033_TUNER_IT9135_51:
  298. case AF9033_TUNER_IT9135_52:
  299. len = ARRAY_SIZE(ofsm_init_it9135_v1);
  300. init = ofsm_init_it9135_v1;
  301. break;
  302. case AF9033_TUNER_IT9135_60:
  303. case AF9033_TUNER_IT9135_61:
  304. case AF9033_TUNER_IT9135_62:
  305. len = ARRAY_SIZE(ofsm_init_it9135_v2);
  306. init = ofsm_init_it9135_v2;
  307. break;
  308. default:
  309. len = ARRAY_SIZE(ofsm_init);
  310. init = ofsm_init;
  311. break;
  312. }
  313. ret = af9033_wr_reg_val_tab(dev, init, len);
  314. if (ret < 0)
  315. goto err;
  316. /* load tuner specific settings */
  317. dev_dbg(&dev->client->dev, "load tuner specific settings\n");
  318. switch (dev->cfg.tuner) {
  319. case AF9033_TUNER_TUA9001:
  320. len = ARRAY_SIZE(tuner_init_tua9001);
  321. init = tuner_init_tua9001;
  322. break;
  323. case AF9033_TUNER_FC0011:
  324. len = ARRAY_SIZE(tuner_init_fc0011);
  325. init = tuner_init_fc0011;
  326. break;
  327. case AF9033_TUNER_MXL5007T:
  328. len = ARRAY_SIZE(tuner_init_mxl5007t);
  329. init = tuner_init_mxl5007t;
  330. break;
  331. case AF9033_TUNER_TDA18218:
  332. len = ARRAY_SIZE(tuner_init_tda18218);
  333. init = tuner_init_tda18218;
  334. break;
  335. case AF9033_TUNER_FC2580:
  336. len = ARRAY_SIZE(tuner_init_fc2580);
  337. init = tuner_init_fc2580;
  338. break;
  339. case AF9033_TUNER_FC0012:
  340. len = ARRAY_SIZE(tuner_init_fc0012);
  341. init = tuner_init_fc0012;
  342. break;
  343. case AF9033_TUNER_IT9135_38:
  344. len = ARRAY_SIZE(tuner_init_it9135_38);
  345. init = tuner_init_it9135_38;
  346. break;
  347. case AF9033_TUNER_IT9135_51:
  348. len = ARRAY_SIZE(tuner_init_it9135_51);
  349. init = tuner_init_it9135_51;
  350. break;
  351. case AF9033_TUNER_IT9135_52:
  352. len = ARRAY_SIZE(tuner_init_it9135_52);
  353. init = tuner_init_it9135_52;
  354. break;
  355. case AF9033_TUNER_IT9135_60:
  356. len = ARRAY_SIZE(tuner_init_it9135_60);
  357. init = tuner_init_it9135_60;
  358. break;
  359. case AF9033_TUNER_IT9135_61:
  360. len = ARRAY_SIZE(tuner_init_it9135_61);
  361. init = tuner_init_it9135_61;
  362. break;
  363. case AF9033_TUNER_IT9135_62:
  364. len = ARRAY_SIZE(tuner_init_it9135_62);
  365. init = tuner_init_it9135_62;
  366. break;
  367. default:
  368. dev_dbg(&dev->client->dev, "unsupported tuner ID=%d\n",
  369. dev->cfg.tuner);
  370. ret = -ENODEV;
  371. goto err;
  372. }
  373. ret = af9033_wr_reg_val_tab(dev, init, len);
  374. if (ret < 0)
  375. goto err;
  376. if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
  377. ret = af9033_wr_reg_mask(dev, 0x00d91c, 0x01, 0x01);
  378. if (ret < 0)
  379. goto err;
  380. ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
  381. if (ret < 0)
  382. goto err;
  383. ret = af9033_wr_reg_mask(dev, 0x00d916, 0x00, 0x01);
  384. if (ret < 0)
  385. goto err;
  386. }
  387. switch (dev->cfg.tuner) {
  388. case AF9033_TUNER_IT9135_60:
  389. case AF9033_TUNER_IT9135_61:
  390. case AF9033_TUNER_IT9135_62:
  391. ret = af9033_wr_reg(dev, 0x800000, 0x01);
  392. if (ret < 0)
  393. goto err;
  394. }
  395. dev->bandwidth_hz = 0; /* force to program all parameters */
  396. /* init stats here in order signal app which stats are supported */
  397. c->strength.len = 1;
  398. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  399. c->cnr.len = 1;
  400. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  401. c->block_count.len = 1;
  402. c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  403. c->block_error.len = 1;
  404. c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  405. c->post_bit_count.len = 1;
  406. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  407. c->post_bit_error.len = 1;
  408. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  409. /* start statistics polling */
  410. schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
  411. return 0;
  412. err:
  413. dev_dbg(&dev->client->dev, "failed=%d\n", ret);
  414. return ret;
  415. }
  416. static int af9033_sleep(struct dvb_frontend *fe)
  417. {
  418. struct af9033_dev *dev = fe->demodulator_priv;
  419. int ret, i;
  420. u8 tmp;
  421. /* stop statistics polling */
  422. cancel_delayed_work_sync(&dev->stat_work);
  423. ret = af9033_wr_reg(dev, 0x80004c, 1);
  424. if (ret < 0)
  425. goto err;
  426. ret = af9033_wr_reg(dev, 0x800000, 0);
  427. if (ret < 0)
  428. goto err;
  429. for (i = 100, tmp = 1; i && tmp; i--) {
  430. ret = af9033_rd_reg(dev, 0x80004c, &tmp);
  431. if (ret < 0)
  432. goto err;
  433. usleep_range(200, 10000);
  434. }
  435. dev_dbg(&dev->client->dev, "loop=%d\n", i);
  436. if (i == 0) {
  437. ret = -ETIMEDOUT;
  438. goto err;
  439. }
  440. ret = af9033_wr_reg_mask(dev, 0x80fb24, 0x08, 0x08);
  441. if (ret < 0)
  442. goto err;
  443. /* prevent current leak (?) */
  444. if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
  445. /* enable parallel TS */
  446. ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
  447. if (ret < 0)
  448. goto err;
  449. ret = af9033_wr_reg_mask(dev, 0x00d916, 0x01, 0x01);
  450. if (ret < 0)
  451. goto err;
  452. }
  453. return 0;
  454. err:
  455. dev_dbg(&dev->client->dev, "failed=%d\n", ret);
  456. return ret;
  457. }
  458. static int af9033_get_tune_settings(struct dvb_frontend *fe,
  459. struct dvb_frontend_tune_settings *fesettings)
  460. {
  461. /* 800 => 2000 because IT9135 v2 is slow to gain lock */
  462. fesettings->min_delay_ms = 2000;
  463. fesettings->step_size = 0;
  464. fesettings->max_drift = 0;
  465. return 0;
  466. }
  467. static int af9033_set_frontend(struct dvb_frontend *fe)
  468. {
  469. struct af9033_dev *dev = fe->demodulator_priv;
  470. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  471. int ret, i, spec_inv, sampling_freq;
  472. u8 tmp, buf[3], bandwidth_reg_val;
  473. u32 if_frequency, freq_cw, adc_freq;
  474. dev_dbg(&dev->client->dev, "frequency=%d bandwidth_hz=%d\n",
  475. c->frequency, c->bandwidth_hz);
  476. /* check bandwidth */
  477. switch (c->bandwidth_hz) {
  478. case 6000000:
  479. bandwidth_reg_val = 0x00;
  480. break;
  481. case 7000000:
  482. bandwidth_reg_val = 0x01;
  483. break;
  484. case 8000000:
  485. bandwidth_reg_val = 0x02;
  486. break;
  487. default:
  488. dev_dbg(&dev->client->dev, "invalid bandwidth_hz\n");
  489. ret = -EINVAL;
  490. goto err;
  491. }
  492. /* program tuner */
  493. if (fe->ops.tuner_ops.set_params)
  494. fe->ops.tuner_ops.set_params(fe);
  495. /* program CFOE coefficients */
  496. if (c->bandwidth_hz != dev->bandwidth_hz) {
  497. for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
  498. if (coeff_lut[i].clock == dev->cfg.clock &&
  499. coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
  500. break;
  501. }
  502. }
  503. if (i == ARRAY_SIZE(coeff_lut)) {
  504. dev_err(&dev->client->dev,
  505. "Couldn't find LUT config for clock=%d\n",
  506. dev->cfg.clock);
  507. ret = -EINVAL;
  508. goto err;
  509. }
  510. ret = af9033_wr_regs(dev, 0x800001,
  511. coeff_lut[i].val, sizeof(coeff_lut[i].val));
  512. }
  513. /* program frequency control */
  514. if (c->bandwidth_hz != dev->bandwidth_hz) {
  515. spec_inv = dev->cfg.spec_inv ? -1 : 1;
  516. for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
  517. if (clock_adc_lut[i].clock == dev->cfg.clock)
  518. break;
  519. }
  520. if (i == ARRAY_SIZE(clock_adc_lut)) {
  521. dev_err(&dev->client->dev,
  522. "Couldn't find ADC clock for clock=%d\n",
  523. dev->cfg.clock);
  524. ret = -EINVAL;
  525. goto err;
  526. }
  527. adc_freq = clock_adc_lut[i].adc;
  528. /* get used IF frequency */
  529. if (fe->ops.tuner_ops.get_if_frequency)
  530. fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
  531. else
  532. if_frequency = 0;
  533. sampling_freq = if_frequency;
  534. while (sampling_freq > (adc_freq / 2))
  535. sampling_freq -= adc_freq;
  536. if (sampling_freq >= 0)
  537. spec_inv *= -1;
  538. else
  539. sampling_freq *= -1;
  540. freq_cw = af9033_div(dev, sampling_freq, adc_freq, 23ul);
  541. if (spec_inv == -1)
  542. freq_cw = 0x800000 - freq_cw;
  543. if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X)
  544. freq_cw /= 2;
  545. buf[0] = (freq_cw >> 0) & 0xff;
  546. buf[1] = (freq_cw >> 8) & 0xff;
  547. buf[2] = (freq_cw >> 16) & 0x7f;
  548. /* FIXME: there seems to be calculation error here... */
  549. if (if_frequency == 0)
  550. buf[2] = 0;
  551. ret = af9033_wr_regs(dev, 0x800029, buf, 3);
  552. if (ret < 0)
  553. goto err;
  554. dev->bandwidth_hz = c->bandwidth_hz;
  555. }
  556. ret = af9033_wr_reg_mask(dev, 0x80f904, bandwidth_reg_val, 0x03);
  557. if (ret < 0)
  558. goto err;
  559. ret = af9033_wr_reg(dev, 0x800040, 0x00);
  560. if (ret < 0)
  561. goto err;
  562. ret = af9033_wr_reg(dev, 0x800047, 0x00);
  563. if (ret < 0)
  564. goto err;
  565. ret = af9033_wr_reg_mask(dev, 0x80f999, 0x00, 0x01);
  566. if (ret < 0)
  567. goto err;
  568. if (c->frequency <= 230000000)
  569. tmp = 0x00; /* VHF */
  570. else
  571. tmp = 0x01; /* UHF */
  572. ret = af9033_wr_reg(dev, 0x80004b, tmp);
  573. if (ret < 0)
  574. goto err;
  575. ret = af9033_wr_reg(dev, 0x800000, 0x00);
  576. if (ret < 0)
  577. goto err;
  578. return 0;
  579. err:
  580. dev_dbg(&dev->client->dev, "failed=%d\n", ret);
  581. return ret;
  582. }
  583. static int af9033_get_frontend(struct dvb_frontend *fe)
  584. {
  585. struct af9033_dev *dev = fe->demodulator_priv;
  586. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  587. int ret;
  588. u8 buf[8];
  589. dev_dbg(&dev->client->dev, "\n");
  590. /* read all needed registers */
  591. ret = af9033_rd_regs(dev, 0x80f900, buf, sizeof(buf));
  592. if (ret < 0)
  593. goto err;
  594. switch ((buf[0] >> 0) & 3) {
  595. case 0:
  596. c->transmission_mode = TRANSMISSION_MODE_2K;
  597. break;
  598. case 1:
  599. c->transmission_mode = TRANSMISSION_MODE_8K;
  600. break;
  601. }
  602. switch ((buf[1] >> 0) & 3) {
  603. case 0:
  604. c->guard_interval = GUARD_INTERVAL_1_32;
  605. break;
  606. case 1:
  607. c->guard_interval = GUARD_INTERVAL_1_16;
  608. break;
  609. case 2:
  610. c->guard_interval = GUARD_INTERVAL_1_8;
  611. break;
  612. case 3:
  613. c->guard_interval = GUARD_INTERVAL_1_4;
  614. break;
  615. }
  616. switch ((buf[2] >> 0) & 7) {
  617. case 0:
  618. c->hierarchy = HIERARCHY_NONE;
  619. break;
  620. case 1:
  621. c->hierarchy = HIERARCHY_1;
  622. break;
  623. case 2:
  624. c->hierarchy = HIERARCHY_2;
  625. break;
  626. case 3:
  627. c->hierarchy = HIERARCHY_4;
  628. break;
  629. }
  630. switch ((buf[3] >> 0) & 3) {
  631. case 0:
  632. c->modulation = QPSK;
  633. break;
  634. case 1:
  635. c->modulation = QAM_16;
  636. break;
  637. case 2:
  638. c->modulation = QAM_64;
  639. break;
  640. }
  641. switch ((buf[4] >> 0) & 3) {
  642. case 0:
  643. c->bandwidth_hz = 6000000;
  644. break;
  645. case 1:
  646. c->bandwidth_hz = 7000000;
  647. break;
  648. case 2:
  649. c->bandwidth_hz = 8000000;
  650. break;
  651. }
  652. switch ((buf[6] >> 0) & 7) {
  653. case 0:
  654. c->code_rate_HP = FEC_1_2;
  655. break;
  656. case 1:
  657. c->code_rate_HP = FEC_2_3;
  658. break;
  659. case 2:
  660. c->code_rate_HP = FEC_3_4;
  661. break;
  662. case 3:
  663. c->code_rate_HP = FEC_5_6;
  664. break;
  665. case 4:
  666. c->code_rate_HP = FEC_7_8;
  667. break;
  668. case 5:
  669. c->code_rate_HP = FEC_NONE;
  670. break;
  671. }
  672. switch ((buf[7] >> 0) & 7) {
  673. case 0:
  674. c->code_rate_LP = FEC_1_2;
  675. break;
  676. case 1:
  677. c->code_rate_LP = FEC_2_3;
  678. break;
  679. case 2:
  680. c->code_rate_LP = FEC_3_4;
  681. break;
  682. case 3:
  683. c->code_rate_LP = FEC_5_6;
  684. break;
  685. case 4:
  686. c->code_rate_LP = FEC_7_8;
  687. break;
  688. case 5:
  689. c->code_rate_LP = FEC_NONE;
  690. break;
  691. }
  692. return 0;
  693. err:
  694. dev_dbg(&dev->client->dev, "failed=%d\n", ret);
  695. return ret;
  696. }
  697. static int af9033_read_status(struct dvb_frontend *fe, enum fe_status *status)
  698. {
  699. struct af9033_dev *dev = fe->demodulator_priv;
  700. int ret;
  701. u8 tmp;
  702. *status = 0;
  703. /* radio channel status, 0=no result, 1=has signal, 2=no signal */
  704. ret = af9033_rd_reg(dev, 0x800047, &tmp);
  705. if (ret < 0)
  706. goto err;
  707. /* has signal */
  708. if (tmp == 0x01)
  709. *status |= FE_HAS_SIGNAL;
  710. if (tmp != 0x02) {
  711. /* TPS lock */
  712. ret = af9033_rd_reg_mask(dev, 0x80f5a9, &tmp, 0x01);
  713. if (ret < 0)
  714. goto err;
  715. if (tmp)
  716. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  717. FE_HAS_VITERBI;
  718. /* full lock */
  719. ret = af9033_rd_reg_mask(dev, 0x80f999, &tmp, 0x01);
  720. if (ret < 0)
  721. goto err;
  722. if (tmp)
  723. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  724. FE_HAS_VITERBI | FE_HAS_SYNC |
  725. FE_HAS_LOCK;
  726. }
  727. dev->fe_status = *status;
  728. return 0;
  729. err:
  730. dev_dbg(&dev->client->dev, "failed=%d\n", ret);
  731. return ret;
  732. }
  733. static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
  734. {
  735. struct af9033_dev *dev = fe->demodulator_priv;
  736. struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
  737. int ret;
  738. u8 u8tmp;
  739. /* use DVBv5 CNR */
  740. if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL) {
  741. /* Return 0.1 dB for AF9030 and 0-0xffff for IT9130. */
  742. if (dev->is_af9035) {
  743. /* 1000x => 10x (0.1 dB) */
  744. *snr = div_s64(c->cnr.stat[0].svalue, 100);
  745. } else {
  746. /* 1000x => 1x (1 dB) */
  747. *snr = div_s64(c->cnr.stat[0].svalue, 1000);
  748. /* read current modulation */
  749. ret = af9033_rd_reg(dev, 0x80f903, &u8tmp);
  750. if (ret)
  751. goto err;
  752. /* scale value to 0x0000-0xffff */
  753. switch ((u8tmp >> 0) & 3) {
  754. case 0:
  755. *snr = *snr * 0xffff / 23;
  756. break;
  757. case 1:
  758. *snr = *snr * 0xffff / 26;
  759. break;
  760. case 2:
  761. *snr = *snr * 0xffff / 32;
  762. break;
  763. default:
  764. goto err;
  765. }
  766. }
  767. } else {
  768. *snr = 0;
  769. }
  770. return 0;
  771. err:
  772. dev_dbg(&dev->client->dev, "failed=%d\n", ret);
  773. return ret;
  774. }
  775. static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  776. {
  777. struct af9033_dev *dev = fe->demodulator_priv;
  778. struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
  779. int ret, tmp, power_real;
  780. u8 u8tmp, gain_offset, buf[7];
  781. if (dev->is_af9035) {
  782. /* read signal strength of 0-100 scale */
  783. ret = af9033_rd_reg(dev, 0x800048, &u8tmp);
  784. if (ret < 0)
  785. goto err;
  786. /* scale value to 0x0000-0xffff */
  787. *strength = u8tmp * 0xffff / 100;
  788. } else {
  789. ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp);
  790. if (ret < 0)
  791. goto err;
  792. ret = af9033_rd_regs(dev, 0x80f900, buf, 7);
  793. if (ret < 0)
  794. goto err;
  795. if (c->frequency <= 300000000)
  796. gain_offset = 7; /* VHF */
  797. else
  798. gain_offset = 4; /* UHF */
  799. power_real = (u8tmp - 100 - gain_offset) -
  800. power_reference[((buf[3] >> 0) & 3)][((buf[6] >> 0) & 7)];
  801. if (power_real < -15)
  802. tmp = 0;
  803. else if ((power_real >= -15) && (power_real < 0))
  804. tmp = (2 * (power_real + 15)) / 3;
  805. else if ((power_real >= 0) && (power_real < 20))
  806. tmp = 4 * power_real + 10;
  807. else if ((power_real >= 20) && (power_real < 35))
  808. tmp = (2 * (power_real - 20)) / 3 + 90;
  809. else
  810. tmp = 100;
  811. /* scale value to 0x0000-0xffff */
  812. *strength = tmp * 0xffff / 100;
  813. }
  814. return 0;
  815. err:
  816. dev_dbg(&dev->client->dev, "failed=%d\n", ret);
  817. return ret;
  818. }
  819. static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
  820. {
  821. struct af9033_dev *dev = fe->demodulator_priv;
  822. *ber = (dev->post_bit_error - dev->post_bit_error_prev);
  823. dev->post_bit_error_prev = dev->post_bit_error;
  824. return 0;
  825. }
  826. static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  827. {
  828. struct af9033_dev *dev = fe->demodulator_priv;
  829. *ucblocks = dev->error_block_count;
  830. return 0;
  831. }
  832. static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  833. {
  834. struct af9033_dev *dev = fe->demodulator_priv;
  835. int ret;
  836. dev_dbg(&dev->client->dev, "enable=%d\n", enable);
  837. ret = af9033_wr_reg_mask(dev, 0x00fa04, enable, 0x01);
  838. if (ret < 0)
  839. goto err;
  840. return 0;
  841. err:
  842. dev_dbg(&dev->client->dev, "failed=%d\n", ret);
  843. return ret;
  844. }
  845. static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
  846. {
  847. struct af9033_dev *dev = fe->demodulator_priv;
  848. int ret;
  849. dev_dbg(&dev->client->dev, "onoff=%d\n", onoff);
  850. ret = af9033_wr_reg_mask(dev, 0x80f993, onoff, 0x01);
  851. if (ret < 0)
  852. goto err;
  853. return 0;
  854. err:
  855. dev_dbg(&dev->client->dev, "failed=%d\n", ret);
  856. return ret;
  857. }
  858. static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid,
  859. int onoff)
  860. {
  861. struct af9033_dev *dev = fe->demodulator_priv;
  862. int ret;
  863. u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
  864. dev_dbg(&dev->client->dev, "index=%d pid=%04x onoff=%d\n",
  865. index, pid, onoff);
  866. if (pid > 0x1fff)
  867. return 0;
  868. ret = af9033_wr_regs(dev, 0x80f996, wbuf, 2);
  869. if (ret < 0)
  870. goto err;
  871. ret = af9033_wr_reg(dev, 0x80f994, onoff);
  872. if (ret < 0)
  873. goto err;
  874. ret = af9033_wr_reg(dev, 0x80f995, index);
  875. if (ret < 0)
  876. goto err;
  877. return 0;
  878. err:
  879. dev_dbg(&dev->client->dev, "failed=%d\n", ret);
  880. return ret;
  881. }
  882. static void af9033_stat_work(struct work_struct *work)
  883. {
  884. struct af9033_dev *dev = container_of(work, struct af9033_dev, stat_work.work);
  885. struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
  886. int ret, tmp, i, len;
  887. u8 u8tmp, buf[7];
  888. dev_dbg(&dev->client->dev, "\n");
  889. /* signal strength */
  890. if (dev->fe_status & FE_HAS_SIGNAL) {
  891. if (dev->is_af9035) {
  892. ret = af9033_rd_reg(dev, 0x80004a, &u8tmp);
  893. tmp = -u8tmp * 1000;
  894. } else {
  895. ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp);
  896. tmp = (u8tmp - 100) * 1000;
  897. }
  898. if (ret)
  899. goto err;
  900. c->strength.len = 1;
  901. c->strength.stat[0].scale = FE_SCALE_DECIBEL;
  902. c->strength.stat[0].svalue = tmp;
  903. } else {
  904. c->strength.len = 1;
  905. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  906. }
  907. /* CNR */
  908. if (dev->fe_status & FE_HAS_VITERBI) {
  909. u32 snr_val;
  910. const struct val_snr *snr_lut;
  911. /* read value */
  912. ret = af9033_rd_regs(dev, 0x80002c, buf, 3);
  913. if (ret)
  914. goto err;
  915. snr_val = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0);
  916. /* read superframe number */
  917. ret = af9033_rd_reg(dev, 0x80f78b, &u8tmp);
  918. if (ret)
  919. goto err;
  920. if (u8tmp)
  921. snr_val /= u8tmp;
  922. /* read current transmission mode */
  923. ret = af9033_rd_reg(dev, 0x80f900, &u8tmp);
  924. if (ret)
  925. goto err;
  926. switch ((u8tmp >> 0) & 3) {
  927. case 0:
  928. snr_val *= 4;
  929. break;
  930. case 1:
  931. snr_val *= 1;
  932. break;
  933. case 2:
  934. snr_val *= 2;
  935. break;
  936. default:
  937. goto err_schedule_delayed_work;
  938. }
  939. /* read current modulation */
  940. ret = af9033_rd_reg(dev, 0x80f903, &u8tmp);
  941. if (ret)
  942. goto err;
  943. switch ((u8tmp >> 0) & 3) {
  944. case 0:
  945. len = ARRAY_SIZE(qpsk_snr_lut);
  946. snr_lut = qpsk_snr_lut;
  947. break;
  948. case 1:
  949. len = ARRAY_SIZE(qam16_snr_lut);
  950. snr_lut = qam16_snr_lut;
  951. break;
  952. case 2:
  953. len = ARRAY_SIZE(qam64_snr_lut);
  954. snr_lut = qam64_snr_lut;
  955. break;
  956. default:
  957. goto err_schedule_delayed_work;
  958. }
  959. for (i = 0; i < len; i++) {
  960. tmp = snr_lut[i].snr * 1000;
  961. if (snr_val < snr_lut[i].val)
  962. break;
  963. }
  964. c->cnr.len = 1;
  965. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  966. c->cnr.stat[0].svalue = tmp;
  967. } else {
  968. c->cnr.len = 1;
  969. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  970. }
  971. /* UCB/PER/BER */
  972. if (dev->fe_status & FE_HAS_LOCK) {
  973. /* outer FEC, 204 byte packets */
  974. u16 abort_packet_count, rsd_packet_count;
  975. /* inner FEC, bits */
  976. u32 rsd_bit_err_count;
  977. /*
  978. * Packet count used for measurement is 10000
  979. * (rsd_packet_count). Maybe it should be increased?
  980. */
  981. ret = af9033_rd_regs(dev, 0x800032, buf, 7);
  982. if (ret)
  983. goto err;
  984. abort_packet_count = (buf[1] << 8) | (buf[0] << 0);
  985. rsd_bit_err_count = (buf[4] << 16) | (buf[3] << 8) | buf[2];
  986. rsd_packet_count = (buf[6] << 8) | (buf[5] << 0);
  987. dev->error_block_count += abort_packet_count;
  988. dev->total_block_count += rsd_packet_count;
  989. dev->post_bit_error += rsd_bit_err_count;
  990. dev->post_bit_count += rsd_packet_count * 204 * 8;
  991. c->block_count.len = 1;
  992. c->block_count.stat[0].scale = FE_SCALE_COUNTER;
  993. c->block_count.stat[0].uvalue = dev->total_block_count;
  994. c->block_error.len = 1;
  995. c->block_error.stat[0].scale = FE_SCALE_COUNTER;
  996. c->block_error.stat[0].uvalue = dev->error_block_count;
  997. c->post_bit_count.len = 1;
  998. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  999. c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
  1000. c->post_bit_error.len = 1;
  1001. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  1002. c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
  1003. }
  1004. err_schedule_delayed_work:
  1005. schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
  1006. return;
  1007. err:
  1008. dev_dbg(&dev->client->dev, "failed=%d\n", ret);
  1009. }
  1010. static struct dvb_frontend_ops af9033_ops = {
  1011. .delsys = { SYS_DVBT },
  1012. .info = {
  1013. .name = "Afatech AF9033 (DVB-T)",
  1014. .frequency_min = 174000000,
  1015. .frequency_max = 862000000,
  1016. .frequency_stepsize = 250000,
  1017. .frequency_tolerance = 0,
  1018. .caps = FE_CAN_FEC_1_2 |
  1019. FE_CAN_FEC_2_3 |
  1020. FE_CAN_FEC_3_4 |
  1021. FE_CAN_FEC_5_6 |
  1022. FE_CAN_FEC_7_8 |
  1023. FE_CAN_FEC_AUTO |
  1024. FE_CAN_QPSK |
  1025. FE_CAN_QAM_16 |
  1026. FE_CAN_QAM_64 |
  1027. FE_CAN_QAM_AUTO |
  1028. FE_CAN_TRANSMISSION_MODE_AUTO |
  1029. FE_CAN_GUARD_INTERVAL_AUTO |
  1030. FE_CAN_HIERARCHY_AUTO |
  1031. FE_CAN_RECOVER |
  1032. FE_CAN_MUTE_TS
  1033. },
  1034. .init = af9033_init,
  1035. .sleep = af9033_sleep,
  1036. .get_tune_settings = af9033_get_tune_settings,
  1037. .set_frontend = af9033_set_frontend,
  1038. .get_frontend = af9033_get_frontend,
  1039. .read_status = af9033_read_status,
  1040. .read_snr = af9033_read_snr,
  1041. .read_signal_strength = af9033_read_signal_strength,
  1042. .read_ber = af9033_read_ber,
  1043. .read_ucblocks = af9033_read_ucblocks,
  1044. .i2c_gate_ctrl = af9033_i2c_gate_ctrl,
  1045. };
  1046. static int af9033_probe(struct i2c_client *client,
  1047. const struct i2c_device_id *id)
  1048. {
  1049. struct af9033_config *cfg = client->dev.platform_data;
  1050. struct af9033_dev *dev;
  1051. int ret;
  1052. u8 buf[8];
  1053. u32 reg;
  1054. /* allocate memory for the internal state */
  1055. dev = kzalloc(sizeof(struct af9033_dev), GFP_KERNEL);
  1056. if (dev == NULL) {
  1057. ret = -ENOMEM;
  1058. dev_err(&client->dev, "Could not allocate memory for state\n");
  1059. goto err;
  1060. }
  1061. /* setup the state */
  1062. dev->client = client;
  1063. INIT_DELAYED_WORK(&dev->stat_work, af9033_stat_work);
  1064. memcpy(&dev->cfg, cfg, sizeof(struct af9033_config));
  1065. if (dev->cfg.clock != 12000000) {
  1066. ret = -ENODEV;
  1067. dev_err(&dev->client->dev,
  1068. "unsupported clock %d Hz, only 12000000 Hz is supported currently\n",
  1069. dev->cfg.clock);
  1070. goto err_kfree;
  1071. }
  1072. /* firmware version */
  1073. switch (dev->cfg.tuner) {
  1074. case AF9033_TUNER_IT9135_38:
  1075. case AF9033_TUNER_IT9135_51:
  1076. case AF9033_TUNER_IT9135_52:
  1077. case AF9033_TUNER_IT9135_60:
  1078. case AF9033_TUNER_IT9135_61:
  1079. case AF9033_TUNER_IT9135_62:
  1080. dev->is_it9135 = true;
  1081. reg = 0x004bfc;
  1082. break;
  1083. default:
  1084. dev->is_af9035 = true;
  1085. reg = 0x0083e9;
  1086. break;
  1087. }
  1088. ret = af9033_rd_regs(dev, reg, &buf[0], 4);
  1089. if (ret < 0)
  1090. goto err_kfree;
  1091. ret = af9033_rd_regs(dev, 0x804191, &buf[4], 4);
  1092. if (ret < 0)
  1093. goto err_kfree;
  1094. dev_info(&dev->client->dev,
  1095. "firmware version: LINK %d.%d.%d.%d - OFDM %d.%d.%d.%d\n",
  1096. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
  1097. buf[7]);
  1098. /* sleep */
  1099. switch (dev->cfg.tuner) {
  1100. case AF9033_TUNER_IT9135_38:
  1101. case AF9033_TUNER_IT9135_51:
  1102. case AF9033_TUNER_IT9135_52:
  1103. case AF9033_TUNER_IT9135_60:
  1104. case AF9033_TUNER_IT9135_61:
  1105. case AF9033_TUNER_IT9135_62:
  1106. /* IT9135 did not like to sleep at that early */
  1107. break;
  1108. default:
  1109. ret = af9033_wr_reg(dev, 0x80004c, 1);
  1110. if (ret < 0)
  1111. goto err_kfree;
  1112. ret = af9033_wr_reg(dev, 0x800000, 0);
  1113. if (ret < 0)
  1114. goto err_kfree;
  1115. }
  1116. /* configure internal TS mode */
  1117. switch (dev->cfg.ts_mode) {
  1118. case AF9033_TS_MODE_PARALLEL:
  1119. dev->ts_mode_parallel = true;
  1120. break;
  1121. case AF9033_TS_MODE_SERIAL:
  1122. dev->ts_mode_serial = true;
  1123. break;
  1124. case AF9033_TS_MODE_USB:
  1125. /* usb mode for AF9035 */
  1126. default:
  1127. break;
  1128. }
  1129. /* create dvb_frontend */
  1130. memcpy(&dev->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops));
  1131. dev->fe.demodulator_priv = dev;
  1132. *cfg->fe = &dev->fe;
  1133. if (cfg->ops) {
  1134. cfg->ops->pid_filter = af9033_pid_filter;
  1135. cfg->ops->pid_filter_ctrl = af9033_pid_filter_ctrl;
  1136. }
  1137. i2c_set_clientdata(client, dev);
  1138. dev_info(&dev->client->dev, "Afatech AF9033 successfully attached\n");
  1139. return 0;
  1140. err_kfree:
  1141. kfree(dev);
  1142. err:
  1143. dev_dbg(&client->dev, "failed=%d\n", ret);
  1144. return ret;
  1145. }
  1146. static int af9033_remove(struct i2c_client *client)
  1147. {
  1148. struct af9033_dev *dev = i2c_get_clientdata(client);
  1149. dev_dbg(&dev->client->dev, "\n");
  1150. dev->fe.ops.release = NULL;
  1151. dev->fe.demodulator_priv = NULL;
  1152. kfree(dev);
  1153. return 0;
  1154. }
  1155. static const struct i2c_device_id af9033_id_table[] = {
  1156. {"af9033", 0},
  1157. {}
  1158. };
  1159. MODULE_DEVICE_TABLE(i2c, af9033_id_table);
  1160. static struct i2c_driver af9033_driver = {
  1161. .driver = {
  1162. .name = "af9033",
  1163. },
  1164. .probe = af9033_probe,
  1165. .remove = af9033_remove,
  1166. .id_table = af9033_id_table,
  1167. };
  1168. module_i2c_driver(af9033_driver);
  1169. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1170. MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
  1171. MODULE_LICENSE("GPL");