m88ds3103.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. /*
  2. * Montage Technology M88DS3103/M88RS6000 demodulator driver
  3. *
  4. * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include "m88ds3103_priv.h"
  17. static struct dvb_frontend_ops m88ds3103_ops;
  18. /* write single register with mask */
  19. static int m88ds3103_update_bits(struct m88ds3103_dev *dev,
  20. u8 reg, u8 mask, u8 val)
  21. {
  22. int ret;
  23. u8 tmp;
  24. /* no need for read if whole reg is written */
  25. if (mask != 0xff) {
  26. ret = regmap_bulk_read(dev->regmap, reg, &tmp, 1);
  27. if (ret)
  28. return ret;
  29. val &= mask;
  30. tmp &= ~mask;
  31. val |= tmp;
  32. }
  33. return regmap_bulk_write(dev->regmap, reg, &val, 1);
  34. }
  35. /* write reg val table using reg addr auto increment */
  36. static int m88ds3103_wr_reg_val_tab(struct m88ds3103_dev *dev,
  37. const struct m88ds3103_reg_val *tab, int tab_len)
  38. {
  39. struct i2c_client *client = dev->client;
  40. int ret, i, j;
  41. u8 buf[83];
  42. dev_dbg(&client->dev, "tab_len=%d\n", tab_len);
  43. if (tab_len > 86) {
  44. ret = -EINVAL;
  45. goto err;
  46. }
  47. for (i = 0, j = 0; i < tab_len; i++, j++) {
  48. buf[j] = tab[i].val;
  49. if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1 ||
  50. !((j + 1) % (dev->cfg->i2c_wr_max - 1))) {
  51. ret = regmap_bulk_write(dev->regmap, tab[i].reg - j, buf, j + 1);
  52. if (ret)
  53. goto err;
  54. j = -1;
  55. }
  56. }
  57. return 0;
  58. err:
  59. dev_dbg(&client->dev, "failed=%d\n", ret);
  60. return ret;
  61. }
  62. /*
  63. * Get the demodulator AGC PWM voltage setting supplied to the tuner.
  64. */
  65. int m88ds3103_get_agc_pwm(struct dvb_frontend *fe, u8 *_agc_pwm)
  66. {
  67. struct m88ds3103_dev *dev = fe->demodulator_priv;
  68. unsigned tmp;
  69. int ret;
  70. ret = regmap_read(dev->regmap, 0x3f, &tmp);
  71. if (ret == 0)
  72. *_agc_pwm = tmp;
  73. return ret;
  74. }
  75. EXPORT_SYMBOL(m88ds3103_get_agc_pwm);
  76. static int m88ds3103_read_status(struct dvb_frontend *fe,
  77. enum fe_status *status)
  78. {
  79. struct m88ds3103_dev *dev = fe->demodulator_priv;
  80. struct i2c_client *client = dev->client;
  81. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  82. int ret, i, itmp;
  83. unsigned int utmp;
  84. u8 buf[3];
  85. *status = 0;
  86. if (!dev->warm) {
  87. ret = -EAGAIN;
  88. goto err;
  89. }
  90. switch (c->delivery_system) {
  91. case SYS_DVBS:
  92. ret = regmap_read(dev->regmap, 0xd1, &utmp);
  93. if (ret)
  94. goto err;
  95. if ((utmp & 0x07) == 0x07)
  96. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  97. FE_HAS_VITERBI | FE_HAS_SYNC |
  98. FE_HAS_LOCK;
  99. break;
  100. case SYS_DVBS2:
  101. ret = regmap_read(dev->regmap, 0x0d, &utmp);
  102. if (ret)
  103. goto err;
  104. if ((utmp & 0x8f) == 0x8f)
  105. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  106. FE_HAS_VITERBI | FE_HAS_SYNC |
  107. FE_HAS_LOCK;
  108. break;
  109. default:
  110. dev_dbg(&client->dev, "invalid delivery_system\n");
  111. ret = -EINVAL;
  112. goto err;
  113. }
  114. dev->fe_status = *status;
  115. dev_dbg(&client->dev, "lock=%02x status=%02x\n", utmp, *status);
  116. /* CNR */
  117. if (dev->fe_status & FE_HAS_VITERBI) {
  118. unsigned int cnr, noise, signal, noise_tot, signal_tot;
  119. cnr = 0;
  120. /* more iterations for more accurate estimation */
  121. #define M88DS3103_SNR_ITERATIONS 3
  122. switch (c->delivery_system) {
  123. case SYS_DVBS:
  124. itmp = 0;
  125. for (i = 0; i < M88DS3103_SNR_ITERATIONS; i++) {
  126. ret = regmap_read(dev->regmap, 0xff, &utmp);
  127. if (ret)
  128. goto err;
  129. itmp += utmp;
  130. }
  131. /* use of single register limits max value to 15 dB */
  132. /* SNR(X) dB = 10 * ln(X) / ln(10) dB */
  133. itmp = DIV_ROUND_CLOSEST(itmp, 8 * M88DS3103_SNR_ITERATIONS);
  134. if (itmp)
  135. cnr = div_u64((u64) 10000 * intlog2(itmp), intlog2(10));
  136. break;
  137. case SYS_DVBS2:
  138. noise_tot = 0;
  139. signal_tot = 0;
  140. for (i = 0; i < M88DS3103_SNR_ITERATIONS; i++) {
  141. ret = regmap_bulk_read(dev->regmap, 0x8c, buf, 3);
  142. if (ret)
  143. goto err;
  144. noise = buf[1] << 6; /* [13:6] */
  145. noise |= buf[0] & 0x3f; /* [5:0] */
  146. noise >>= 2;
  147. signal = buf[2] * buf[2];
  148. signal >>= 1;
  149. noise_tot += noise;
  150. signal_tot += signal;
  151. }
  152. noise = noise_tot / M88DS3103_SNR_ITERATIONS;
  153. signal = signal_tot / M88DS3103_SNR_ITERATIONS;
  154. /* SNR(X) dB = 10 * log10(X) dB */
  155. if (signal > noise) {
  156. itmp = signal / noise;
  157. cnr = div_u64((u64) 10000 * intlog10(itmp), (1 << 24));
  158. }
  159. break;
  160. default:
  161. dev_dbg(&client->dev, "invalid delivery_system\n");
  162. ret = -EINVAL;
  163. goto err;
  164. }
  165. if (cnr) {
  166. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  167. c->cnr.stat[0].svalue = cnr;
  168. } else {
  169. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  170. }
  171. } else {
  172. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  173. }
  174. /* BER */
  175. if (dev->fe_status & FE_HAS_LOCK) {
  176. unsigned int utmp, post_bit_error, post_bit_count;
  177. switch (c->delivery_system) {
  178. case SYS_DVBS:
  179. ret = regmap_write(dev->regmap, 0xf9, 0x04);
  180. if (ret)
  181. goto err;
  182. ret = regmap_read(dev->regmap, 0xf8, &utmp);
  183. if (ret)
  184. goto err;
  185. /* measurement ready? */
  186. if (!(utmp & 0x10)) {
  187. ret = regmap_bulk_read(dev->regmap, 0xf6, buf, 2);
  188. if (ret)
  189. goto err;
  190. post_bit_error = buf[1] << 8 | buf[0] << 0;
  191. post_bit_count = 0x800000;
  192. dev->post_bit_error += post_bit_error;
  193. dev->post_bit_count += post_bit_count;
  194. dev->dvbv3_ber = post_bit_error;
  195. /* restart measurement */
  196. utmp |= 0x10;
  197. ret = regmap_write(dev->regmap, 0xf8, utmp);
  198. if (ret)
  199. goto err;
  200. }
  201. break;
  202. case SYS_DVBS2:
  203. ret = regmap_bulk_read(dev->regmap, 0xd5, buf, 3);
  204. if (ret)
  205. goto err;
  206. utmp = buf[2] << 16 | buf[1] << 8 | buf[0] << 0;
  207. /* enough data? */
  208. if (utmp > 4000) {
  209. ret = regmap_bulk_read(dev->regmap, 0xf7, buf, 2);
  210. if (ret)
  211. goto err;
  212. post_bit_error = buf[1] << 8 | buf[0] << 0;
  213. post_bit_count = 32 * utmp; /* TODO: FEC */
  214. dev->post_bit_error += post_bit_error;
  215. dev->post_bit_count += post_bit_count;
  216. dev->dvbv3_ber = post_bit_error;
  217. /* restart measurement */
  218. ret = regmap_write(dev->regmap, 0xd1, 0x01);
  219. if (ret)
  220. goto err;
  221. ret = regmap_write(dev->regmap, 0xf9, 0x01);
  222. if (ret)
  223. goto err;
  224. ret = regmap_write(dev->regmap, 0xf9, 0x00);
  225. if (ret)
  226. goto err;
  227. ret = regmap_write(dev->regmap, 0xd1, 0x00);
  228. if (ret)
  229. goto err;
  230. }
  231. break;
  232. default:
  233. dev_dbg(&client->dev, "invalid delivery_system\n");
  234. ret = -EINVAL;
  235. goto err;
  236. }
  237. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  238. c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
  239. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  240. c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
  241. } else {
  242. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  243. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  244. }
  245. return 0;
  246. err:
  247. dev_dbg(&client->dev, "failed=%d\n", ret);
  248. return ret;
  249. }
  250. static int m88ds3103_set_frontend(struct dvb_frontend *fe)
  251. {
  252. struct m88ds3103_dev *dev = fe->demodulator_priv;
  253. struct i2c_client *client = dev->client;
  254. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  255. int ret, len;
  256. const struct m88ds3103_reg_val *init;
  257. u8 u8tmp, u8tmp1 = 0, u8tmp2 = 0; /* silence compiler warning */
  258. u8 buf[3];
  259. u16 u16tmp, divide_ratio = 0;
  260. u32 tuner_frequency, target_mclk;
  261. s32 s32tmp;
  262. dev_dbg(&client->dev,
  263. "delivery_system=%d modulation=%d frequency=%u symbol_rate=%d inversion=%d pilot=%d rolloff=%d\n",
  264. c->delivery_system, c->modulation, c->frequency, c->symbol_rate,
  265. c->inversion, c->pilot, c->rolloff);
  266. if (!dev->warm) {
  267. ret = -EAGAIN;
  268. goto err;
  269. }
  270. /* reset */
  271. ret = regmap_write(dev->regmap, 0x07, 0x80);
  272. if (ret)
  273. goto err;
  274. ret = regmap_write(dev->regmap, 0x07, 0x00);
  275. if (ret)
  276. goto err;
  277. /* Disable demod clock path */
  278. if (dev->chip_id == M88RS6000_CHIP_ID) {
  279. ret = regmap_write(dev->regmap, 0x06, 0xe0);
  280. if (ret)
  281. goto err;
  282. }
  283. /* program tuner */
  284. if (fe->ops.tuner_ops.set_params) {
  285. ret = fe->ops.tuner_ops.set_params(fe);
  286. if (ret)
  287. goto err;
  288. }
  289. if (fe->ops.tuner_ops.get_frequency) {
  290. ret = fe->ops.tuner_ops.get_frequency(fe, &tuner_frequency);
  291. if (ret)
  292. goto err;
  293. } else {
  294. /*
  295. * Use nominal target frequency as tuner driver does not provide
  296. * actual frequency used. Carrier offset calculation is not
  297. * valid.
  298. */
  299. tuner_frequency = c->frequency;
  300. }
  301. /* select M88RS6000 demod main mclk and ts mclk from tuner die. */
  302. if (dev->chip_id == M88RS6000_CHIP_ID) {
  303. if (c->symbol_rate > 45010000)
  304. dev->mclk_khz = 110250;
  305. else
  306. dev->mclk_khz = 96000;
  307. if (c->delivery_system == SYS_DVBS)
  308. target_mclk = 96000;
  309. else
  310. target_mclk = 144000;
  311. /* Enable demod clock path */
  312. ret = regmap_write(dev->regmap, 0x06, 0x00);
  313. if (ret)
  314. goto err;
  315. usleep_range(10000, 20000);
  316. } else {
  317. /* set M88DS3103 mclk and ts mclk. */
  318. dev->mclk_khz = 96000;
  319. switch (dev->cfg->ts_mode) {
  320. case M88DS3103_TS_SERIAL:
  321. case M88DS3103_TS_SERIAL_D7:
  322. target_mclk = dev->cfg->ts_clk;
  323. break;
  324. case M88DS3103_TS_PARALLEL:
  325. case M88DS3103_TS_CI:
  326. if (c->delivery_system == SYS_DVBS)
  327. target_mclk = 96000;
  328. else {
  329. if (c->symbol_rate < 18000000)
  330. target_mclk = 96000;
  331. else if (c->symbol_rate < 28000000)
  332. target_mclk = 144000;
  333. else
  334. target_mclk = 192000;
  335. }
  336. break;
  337. default:
  338. dev_dbg(&client->dev, "invalid ts_mode\n");
  339. ret = -EINVAL;
  340. goto err;
  341. }
  342. switch (target_mclk) {
  343. case 96000:
  344. u8tmp1 = 0x02; /* 0b10 */
  345. u8tmp2 = 0x01; /* 0b01 */
  346. break;
  347. case 144000:
  348. u8tmp1 = 0x00; /* 0b00 */
  349. u8tmp2 = 0x01; /* 0b01 */
  350. break;
  351. case 192000:
  352. u8tmp1 = 0x03; /* 0b11 */
  353. u8tmp2 = 0x00; /* 0b00 */
  354. break;
  355. }
  356. ret = m88ds3103_update_bits(dev, 0x22, 0xc0, u8tmp1 << 6);
  357. if (ret)
  358. goto err;
  359. ret = m88ds3103_update_bits(dev, 0x24, 0xc0, u8tmp2 << 6);
  360. if (ret)
  361. goto err;
  362. }
  363. ret = regmap_write(dev->regmap, 0xb2, 0x01);
  364. if (ret)
  365. goto err;
  366. ret = regmap_write(dev->regmap, 0x00, 0x01);
  367. if (ret)
  368. goto err;
  369. switch (c->delivery_system) {
  370. case SYS_DVBS:
  371. if (dev->chip_id == M88RS6000_CHIP_ID) {
  372. len = ARRAY_SIZE(m88rs6000_dvbs_init_reg_vals);
  373. init = m88rs6000_dvbs_init_reg_vals;
  374. } else {
  375. len = ARRAY_SIZE(m88ds3103_dvbs_init_reg_vals);
  376. init = m88ds3103_dvbs_init_reg_vals;
  377. }
  378. break;
  379. case SYS_DVBS2:
  380. if (dev->chip_id == M88RS6000_CHIP_ID) {
  381. len = ARRAY_SIZE(m88rs6000_dvbs2_init_reg_vals);
  382. init = m88rs6000_dvbs2_init_reg_vals;
  383. } else {
  384. len = ARRAY_SIZE(m88ds3103_dvbs2_init_reg_vals);
  385. init = m88ds3103_dvbs2_init_reg_vals;
  386. }
  387. break;
  388. default:
  389. dev_dbg(&client->dev, "invalid delivery_system\n");
  390. ret = -EINVAL;
  391. goto err;
  392. }
  393. /* program init table */
  394. if (c->delivery_system != dev->delivery_system) {
  395. ret = m88ds3103_wr_reg_val_tab(dev, init, len);
  396. if (ret)
  397. goto err;
  398. }
  399. if (dev->chip_id == M88RS6000_CHIP_ID) {
  400. if ((c->delivery_system == SYS_DVBS2)
  401. && ((c->symbol_rate / 1000) <= 5000)) {
  402. ret = regmap_write(dev->regmap, 0xc0, 0x04);
  403. if (ret)
  404. goto err;
  405. buf[0] = 0x09;
  406. buf[1] = 0x22;
  407. buf[2] = 0x88;
  408. ret = regmap_bulk_write(dev->regmap, 0x8a, buf, 3);
  409. if (ret)
  410. goto err;
  411. }
  412. ret = m88ds3103_update_bits(dev, 0x9d, 0x08, 0x08);
  413. if (ret)
  414. goto err;
  415. ret = regmap_write(dev->regmap, 0xf1, 0x01);
  416. if (ret)
  417. goto err;
  418. ret = m88ds3103_update_bits(dev, 0x30, 0x80, 0x80);
  419. if (ret)
  420. goto err;
  421. }
  422. switch (dev->cfg->ts_mode) {
  423. case M88DS3103_TS_SERIAL:
  424. u8tmp1 = 0x00;
  425. u8tmp = 0x06;
  426. break;
  427. case M88DS3103_TS_SERIAL_D7:
  428. u8tmp1 = 0x20;
  429. u8tmp = 0x06;
  430. break;
  431. case M88DS3103_TS_PARALLEL:
  432. u8tmp = 0x02;
  433. break;
  434. case M88DS3103_TS_CI:
  435. u8tmp = 0x03;
  436. break;
  437. default:
  438. dev_dbg(&client->dev, "invalid ts_mode\n");
  439. ret = -EINVAL;
  440. goto err;
  441. }
  442. if (dev->cfg->ts_clk_pol)
  443. u8tmp |= 0x40;
  444. /* TS mode */
  445. ret = regmap_write(dev->regmap, 0xfd, u8tmp);
  446. if (ret)
  447. goto err;
  448. switch (dev->cfg->ts_mode) {
  449. case M88DS3103_TS_SERIAL:
  450. case M88DS3103_TS_SERIAL_D7:
  451. ret = m88ds3103_update_bits(dev, 0x29, 0x20, u8tmp1);
  452. if (ret)
  453. goto err;
  454. u8tmp1 = 0;
  455. u8tmp2 = 0;
  456. break;
  457. default:
  458. if (dev->cfg->ts_clk) {
  459. divide_ratio = DIV_ROUND_UP(target_mclk, dev->cfg->ts_clk);
  460. u8tmp1 = divide_ratio / 2;
  461. u8tmp2 = DIV_ROUND_UP(divide_ratio, 2);
  462. }
  463. }
  464. dev_dbg(&client->dev,
  465. "target_mclk=%d ts_clk=%d divide_ratio=%d\n",
  466. target_mclk, dev->cfg->ts_clk, divide_ratio);
  467. u8tmp1--;
  468. u8tmp2--;
  469. /* u8tmp1[5:2] => fe[3:0], u8tmp1[1:0] => ea[7:6] */
  470. u8tmp1 &= 0x3f;
  471. /* u8tmp2[5:0] => ea[5:0] */
  472. u8tmp2 &= 0x3f;
  473. ret = regmap_bulk_read(dev->regmap, 0xfe, &u8tmp, 1);
  474. if (ret)
  475. goto err;
  476. u8tmp = ((u8tmp & 0xf0) << 0) | u8tmp1 >> 2;
  477. ret = regmap_write(dev->regmap, 0xfe, u8tmp);
  478. if (ret)
  479. goto err;
  480. u8tmp = ((u8tmp1 & 0x03) << 6) | u8tmp2 >> 0;
  481. ret = regmap_write(dev->regmap, 0xea, u8tmp);
  482. if (ret)
  483. goto err;
  484. if (c->symbol_rate <= 3000000)
  485. u8tmp = 0x20;
  486. else if (c->symbol_rate <= 10000000)
  487. u8tmp = 0x10;
  488. else
  489. u8tmp = 0x06;
  490. ret = regmap_write(dev->regmap, 0xc3, 0x08);
  491. if (ret)
  492. goto err;
  493. ret = regmap_write(dev->regmap, 0xc8, u8tmp);
  494. if (ret)
  495. goto err;
  496. ret = regmap_write(dev->regmap, 0xc4, 0x08);
  497. if (ret)
  498. goto err;
  499. ret = regmap_write(dev->regmap, 0xc7, 0x00);
  500. if (ret)
  501. goto err;
  502. u16tmp = DIV_ROUND_CLOSEST((c->symbol_rate / 1000) << 15, dev->mclk_khz / 2);
  503. buf[0] = (u16tmp >> 0) & 0xff;
  504. buf[1] = (u16tmp >> 8) & 0xff;
  505. ret = regmap_bulk_write(dev->regmap, 0x61, buf, 2);
  506. if (ret)
  507. goto err;
  508. ret = m88ds3103_update_bits(dev, 0x4d, 0x02, dev->cfg->spec_inv << 1);
  509. if (ret)
  510. goto err;
  511. ret = m88ds3103_update_bits(dev, 0x30, 0x10, dev->cfg->agc_inv << 4);
  512. if (ret)
  513. goto err;
  514. ret = regmap_write(dev->regmap, 0x33, dev->cfg->agc);
  515. if (ret)
  516. goto err;
  517. dev_dbg(&client->dev, "carrier offset=%d\n",
  518. (tuner_frequency - c->frequency));
  519. s32tmp = 0x10000 * (tuner_frequency - c->frequency);
  520. s32tmp = DIV_ROUND_CLOSEST(s32tmp, dev->mclk_khz);
  521. if (s32tmp < 0)
  522. s32tmp += 0x10000;
  523. buf[0] = (s32tmp >> 0) & 0xff;
  524. buf[1] = (s32tmp >> 8) & 0xff;
  525. ret = regmap_bulk_write(dev->regmap, 0x5e, buf, 2);
  526. if (ret)
  527. goto err;
  528. ret = regmap_write(dev->regmap, 0x00, 0x00);
  529. if (ret)
  530. goto err;
  531. ret = regmap_write(dev->regmap, 0xb2, 0x00);
  532. if (ret)
  533. goto err;
  534. dev->delivery_system = c->delivery_system;
  535. return 0;
  536. err:
  537. dev_dbg(&client->dev, "failed=%d\n", ret);
  538. return ret;
  539. }
  540. static int m88ds3103_init(struct dvb_frontend *fe)
  541. {
  542. struct m88ds3103_dev *dev = fe->demodulator_priv;
  543. struct i2c_client *client = dev->client;
  544. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  545. int ret, len, remaining;
  546. unsigned int utmp;
  547. const struct firmware *fw = NULL;
  548. u8 *fw_file;
  549. dev_dbg(&client->dev, "\n");
  550. /* set cold state by default */
  551. dev->warm = false;
  552. /* wake up device from sleep */
  553. ret = m88ds3103_update_bits(dev, 0x08, 0x01, 0x01);
  554. if (ret)
  555. goto err;
  556. ret = m88ds3103_update_bits(dev, 0x04, 0x01, 0x00);
  557. if (ret)
  558. goto err;
  559. ret = m88ds3103_update_bits(dev, 0x23, 0x10, 0x00);
  560. if (ret)
  561. goto err;
  562. /* firmware status */
  563. ret = regmap_read(dev->regmap, 0xb9, &utmp);
  564. if (ret)
  565. goto err;
  566. dev_dbg(&client->dev, "firmware=%02x\n", utmp);
  567. if (utmp)
  568. goto skip_fw_download;
  569. /* global reset, global diseqc reset, golbal fec reset */
  570. ret = regmap_write(dev->regmap, 0x07, 0xe0);
  571. if (ret)
  572. goto err;
  573. ret = regmap_write(dev->regmap, 0x07, 0x00);
  574. if (ret)
  575. goto err;
  576. /* cold state - try to download firmware */
  577. dev_info(&client->dev, "found a '%s' in cold state\n",
  578. m88ds3103_ops.info.name);
  579. if (dev->chip_id == M88RS6000_CHIP_ID)
  580. fw_file = M88RS6000_FIRMWARE;
  581. else
  582. fw_file = M88DS3103_FIRMWARE;
  583. /* request the firmware, this will block and timeout */
  584. ret = request_firmware(&fw, fw_file, &client->dev);
  585. if (ret) {
  586. dev_err(&client->dev, "firmare file '%s' not found\n", fw_file);
  587. goto err;
  588. }
  589. dev_info(&client->dev, "downloading firmware from file '%s'\n",
  590. fw_file);
  591. ret = regmap_write(dev->regmap, 0xb2, 0x01);
  592. if (ret)
  593. goto error_fw_release;
  594. for (remaining = fw->size; remaining > 0;
  595. remaining -= (dev->cfg->i2c_wr_max - 1)) {
  596. len = remaining;
  597. if (len > (dev->cfg->i2c_wr_max - 1))
  598. len = (dev->cfg->i2c_wr_max - 1);
  599. ret = regmap_bulk_write(dev->regmap, 0xb0,
  600. &fw->data[fw->size - remaining], len);
  601. if (ret) {
  602. dev_err(&client->dev, "firmware download failed=%d\n",
  603. ret);
  604. goto error_fw_release;
  605. }
  606. }
  607. ret = regmap_write(dev->regmap, 0xb2, 0x00);
  608. if (ret)
  609. goto error_fw_release;
  610. release_firmware(fw);
  611. fw = NULL;
  612. ret = regmap_read(dev->regmap, 0xb9, &utmp);
  613. if (ret)
  614. goto err;
  615. if (!utmp) {
  616. dev_info(&client->dev, "firmware did not run\n");
  617. ret = -EFAULT;
  618. goto err;
  619. }
  620. dev_info(&client->dev, "found a '%s' in warm state\n",
  621. m88ds3103_ops.info.name);
  622. dev_info(&client->dev, "firmware version: %X.%X\n",
  623. (utmp >> 4) & 0xf, (utmp >> 0 & 0xf));
  624. skip_fw_download:
  625. /* warm state */
  626. dev->warm = true;
  627. /* init stats here in order signal app which stats are supported */
  628. c->cnr.len = 1;
  629. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  630. c->post_bit_error.len = 1;
  631. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  632. c->post_bit_count.len = 1;
  633. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  634. return 0;
  635. error_fw_release:
  636. release_firmware(fw);
  637. err:
  638. dev_dbg(&client->dev, "failed=%d\n", ret);
  639. return ret;
  640. }
  641. static int m88ds3103_sleep(struct dvb_frontend *fe)
  642. {
  643. struct m88ds3103_dev *dev = fe->demodulator_priv;
  644. struct i2c_client *client = dev->client;
  645. int ret;
  646. unsigned int utmp;
  647. dev_dbg(&client->dev, "\n");
  648. dev->fe_status = 0;
  649. dev->delivery_system = SYS_UNDEFINED;
  650. /* TS Hi-Z */
  651. if (dev->chip_id == M88RS6000_CHIP_ID)
  652. utmp = 0x29;
  653. else
  654. utmp = 0x27;
  655. ret = m88ds3103_update_bits(dev, utmp, 0x01, 0x00);
  656. if (ret)
  657. goto err;
  658. /* sleep */
  659. ret = m88ds3103_update_bits(dev, 0x08, 0x01, 0x00);
  660. if (ret)
  661. goto err;
  662. ret = m88ds3103_update_bits(dev, 0x04, 0x01, 0x01);
  663. if (ret)
  664. goto err;
  665. ret = m88ds3103_update_bits(dev, 0x23, 0x10, 0x10);
  666. if (ret)
  667. goto err;
  668. return 0;
  669. err:
  670. dev_dbg(&client->dev, "failed=%d\n", ret);
  671. return ret;
  672. }
  673. static int m88ds3103_get_frontend(struct dvb_frontend *fe)
  674. {
  675. struct m88ds3103_dev *dev = fe->demodulator_priv;
  676. struct i2c_client *client = dev->client;
  677. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  678. int ret;
  679. u8 buf[3];
  680. dev_dbg(&client->dev, "\n");
  681. if (!dev->warm || !(dev->fe_status & FE_HAS_LOCK)) {
  682. ret = 0;
  683. goto err;
  684. }
  685. switch (c->delivery_system) {
  686. case SYS_DVBS:
  687. ret = regmap_bulk_read(dev->regmap, 0xe0, &buf[0], 1);
  688. if (ret)
  689. goto err;
  690. ret = regmap_bulk_read(dev->regmap, 0xe6, &buf[1], 1);
  691. if (ret)
  692. goto err;
  693. switch ((buf[0] >> 2) & 0x01) {
  694. case 0:
  695. c->inversion = INVERSION_OFF;
  696. break;
  697. case 1:
  698. c->inversion = INVERSION_ON;
  699. break;
  700. }
  701. switch ((buf[1] >> 5) & 0x07) {
  702. case 0:
  703. c->fec_inner = FEC_7_8;
  704. break;
  705. case 1:
  706. c->fec_inner = FEC_5_6;
  707. break;
  708. case 2:
  709. c->fec_inner = FEC_3_4;
  710. break;
  711. case 3:
  712. c->fec_inner = FEC_2_3;
  713. break;
  714. case 4:
  715. c->fec_inner = FEC_1_2;
  716. break;
  717. default:
  718. dev_dbg(&client->dev, "invalid fec_inner\n");
  719. }
  720. c->modulation = QPSK;
  721. break;
  722. case SYS_DVBS2:
  723. ret = regmap_bulk_read(dev->regmap, 0x7e, &buf[0], 1);
  724. if (ret)
  725. goto err;
  726. ret = regmap_bulk_read(dev->regmap, 0x89, &buf[1], 1);
  727. if (ret)
  728. goto err;
  729. ret = regmap_bulk_read(dev->regmap, 0xf2, &buf[2], 1);
  730. if (ret)
  731. goto err;
  732. switch ((buf[0] >> 0) & 0x0f) {
  733. case 2:
  734. c->fec_inner = FEC_2_5;
  735. break;
  736. case 3:
  737. c->fec_inner = FEC_1_2;
  738. break;
  739. case 4:
  740. c->fec_inner = FEC_3_5;
  741. break;
  742. case 5:
  743. c->fec_inner = FEC_2_3;
  744. break;
  745. case 6:
  746. c->fec_inner = FEC_3_4;
  747. break;
  748. case 7:
  749. c->fec_inner = FEC_4_5;
  750. break;
  751. case 8:
  752. c->fec_inner = FEC_5_6;
  753. break;
  754. case 9:
  755. c->fec_inner = FEC_8_9;
  756. break;
  757. case 10:
  758. c->fec_inner = FEC_9_10;
  759. break;
  760. default:
  761. dev_dbg(&client->dev, "invalid fec_inner\n");
  762. }
  763. switch ((buf[0] >> 5) & 0x01) {
  764. case 0:
  765. c->pilot = PILOT_OFF;
  766. break;
  767. case 1:
  768. c->pilot = PILOT_ON;
  769. break;
  770. }
  771. switch ((buf[0] >> 6) & 0x07) {
  772. case 0:
  773. c->modulation = QPSK;
  774. break;
  775. case 1:
  776. c->modulation = PSK_8;
  777. break;
  778. case 2:
  779. c->modulation = APSK_16;
  780. break;
  781. case 3:
  782. c->modulation = APSK_32;
  783. break;
  784. default:
  785. dev_dbg(&client->dev, "invalid modulation\n");
  786. }
  787. switch ((buf[1] >> 7) & 0x01) {
  788. case 0:
  789. c->inversion = INVERSION_OFF;
  790. break;
  791. case 1:
  792. c->inversion = INVERSION_ON;
  793. break;
  794. }
  795. switch ((buf[2] >> 0) & 0x03) {
  796. case 0:
  797. c->rolloff = ROLLOFF_35;
  798. break;
  799. case 1:
  800. c->rolloff = ROLLOFF_25;
  801. break;
  802. case 2:
  803. c->rolloff = ROLLOFF_20;
  804. break;
  805. default:
  806. dev_dbg(&client->dev, "invalid rolloff\n");
  807. }
  808. break;
  809. default:
  810. dev_dbg(&client->dev, "invalid delivery_system\n");
  811. ret = -EINVAL;
  812. goto err;
  813. }
  814. ret = regmap_bulk_read(dev->regmap, 0x6d, buf, 2);
  815. if (ret)
  816. goto err;
  817. c->symbol_rate = 1ull * ((buf[1] << 8) | (buf[0] << 0)) *
  818. dev->mclk_khz * 1000 / 0x10000;
  819. return 0;
  820. err:
  821. dev_dbg(&client->dev, "failed=%d\n", ret);
  822. return ret;
  823. }
  824. static int m88ds3103_read_snr(struct dvb_frontend *fe, u16 *snr)
  825. {
  826. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  827. if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL)
  828. *snr = div_s64(c->cnr.stat[0].svalue, 100);
  829. else
  830. *snr = 0;
  831. return 0;
  832. }
  833. static int m88ds3103_read_ber(struct dvb_frontend *fe, u32 *ber)
  834. {
  835. struct m88ds3103_dev *dev = fe->demodulator_priv;
  836. *ber = dev->dvbv3_ber;
  837. return 0;
  838. }
  839. static int m88ds3103_set_tone(struct dvb_frontend *fe,
  840. enum fe_sec_tone_mode fe_sec_tone_mode)
  841. {
  842. struct m88ds3103_dev *dev = fe->demodulator_priv;
  843. struct i2c_client *client = dev->client;
  844. int ret;
  845. unsigned int utmp, tone, reg_a1_mask;
  846. dev_dbg(&client->dev, "fe_sec_tone_mode=%d\n", fe_sec_tone_mode);
  847. if (!dev->warm) {
  848. ret = -EAGAIN;
  849. goto err;
  850. }
  851. switch (fe_sec_tone_mode) {
  852. case SEC_TONE_ON:
  853. tone = 0;
  854. reg_a1_mask = 0x47;
  855. break;
  856. case SEC_TONE_OFF:
  857. tone = 1;
  858. reg_a1_mask = 0x00;
  859. break;
  860. default:
  861. dev_dbg(&client->dev, "invalid fe_sec_tone_mode\n");
  862. ret = -EINVAL;
  863. goto err;
  864. }
  865. utmp = tone << 7 | dev->cfg->envelope_mode << 5;
  866. ret = m88ds3103_update_bits(dev, 0xa2, 0xe0, utmp);
  867. if (ret)
  868. goto err;
  869. utmp = 1 << 2;
  870. ret = m88ds3103_update_bits(dev, 0xa1, reg_a1_mask, utmp);
  871. if (ret)
  872. goto err;
  873. return 0;
  874. err:
  875. dev_dbg(&client->dev, "failed=%d\n", ret);
  876. return ret;
  877. }
  878. static int m88ds3103_set_voltage(struct dvb_frontend *fe,
  879. enum fe_sec_voltage fe_sec_voltage)
  880. {
  881. struct m88ds3103_dev *dev = fe->demodulator_priv;
  882. struct i2c_client *client = dev->client;
  883. int ret;
  884. unsigned int utmp;
  885. bool voltage_sel, voltage_dis;
  886. dev_dbg(&client->dev, "fe_sec_voltage=%d\n", fe_sec_voltage);
  887. if (!dev->warm) {
  888. ret = -EAGAIN;
  889. goto err;
  890. }
  891. switch (fe_sec_voltage) {
  892. case SEC_VOLTAGE_18:
  893. voltage_sel = true;
  894. voltage_dis = false;
  895. break;
  896. case SEC_VOLTAGE_13:
  897. voltage_sel = false;
  898. voltage_dis = false;
  899. break;
  900. case SEC_VOLTAGE_OFF:
  901. voltage_sel = false;
  902. voltage_dis = true;
  903. break;
  904. default:
  905. dev_dbg(&client->dev, "invalid fe_sec_voltage\n");
  906. ret = -EINVAL;
  907. goto err;
  908. }
  909. /* output pin polarity */
  910. voltage_sel ^= dev->cfg->lnb_hv_pol;
  911. voltage_dis ^= dev->cfg->lnb_en_pol;
  912. utmp = voltage_dis << 1 | voltage_sel << 0;
  913. ret = m88ds3103_update_bits(dev, 0xa2, 0x03, utmp);
  914. if (ret)
  915. goto err;
  916. return 0;
  917. err:
  918. dev_dbg(&client->dev, "failed=%d\n", ret);
  919. return ret;
  920. }
  921. static int m88ds3103_diseqc_send_master_cmd(struct dvb_frontend *fe,
  922. struct dvb_diseqc_master_cmd *diseqc_cmd)
  923. {
  924. struct m88ds3103_dev *dev = fe->demodulator_priv;
  925. struct i2c_client *client = dev->client;
  926. int ret;
  927. unsigned int utmp;
  928. unsigned long timeout;
  929. dev_dbg(&client->dev, "msg=%*ph\n",
  930. diseqc_cmd->msg_len, diseqc_cmd->msg);
  931. if (!dev->warm) {
  932. ret = -EAGAIN;
  933. goto err;
  934. }
  935. if (diseqc_cmd->msg_len < 3 || diseqc_cmd->msg_len > 6) {
  936. ret = -EINVAL;
  937. goto err;
  938. }
  939. utmp = dev->cfg->envelope_mode << 5;
  940. ret = m88ds3103_update_bits(dev, 0xa2, 0xe0, utmp);
  941. if (ret)
  942. goto err;
  943. ret = regmap_bulk_write(dev->regmap, 0xa3, diseqc_cmd->msg,
  944. diseqc_cmd->msg_len);
  945. if (ret)
  946. goto err;
  947. ret = regmap_write(dev->regmap, 0xa1,
  948. (diseqc_cmd->msg_len - 1) << 3 | 0x07);
  949. if (ret)
  950. goto err;
  951. /* wait DiSEqC TX ready */
  952. #define SEND_MASTER_CMD_TIMEOUT 120
  953. timeout = jiffies + msecs_to_jiffies(SEND_MASTER_CMD_TIMEOUT);
  954. /* DiSEqC message typical period is 54 ms */
  955. usleep_range(50000, 54000);
  956. for (utmp = 1; !time_after(jiffies, timeout) && utmp;) {
  957. ret = regmap_read(dev->regmap, 0xa1, &utmp);
  958. if (ret)
  959. goto err;
  960. utmp = (utmp >> 6) & 0x1;
  961. }
  962. if (utmp == 0) {
  963. dev_dbg(&client->dev, "diseqc tx took %u ms\n",
  964. jiffies_to_msecs(jiffies) -
  965. (jiffies_to_msecs(timeout) - SEND_MASTER_CMD_TIMEOUT));
  966. } else {
  967. dev_dbg(&client->dev, "diseqc tx timeout\n");
  968. ret = m88ds3103_update_bits(dev, 0xa1, 0xc0, 0x40);
  969. if (ret)
  970. goto err;
  971. }
  972. ret = m88ds3103_update_bits(dev, 0xa2, 0xc0, 0x80);
  973. if (ret)
  974. goto err;
  975. if (utmp == 1) {
  976. ret = -ETIMEDOUT;
  977. goto err;
  978. }
  979. return 0;
  980. err:
  981. dev_dbg(&client->dev, "failed=%d\n", ret);
  982. return ret;
  983. }
  984. static int m88ds3103_diseqc_send_burst(struct dvb_frontend *fe,
  985. enum fe_sec_mini_cmd fe_sec_mini_cmd)
  986. {
  987. struct m88ds3103_dev *dev = fe->demodulator_priv;
  988. struct i2c_client *client = dev->client;
  989. int ret;
  990. unsigned int utmp, burst;
  991. unsigned long timeout;
  992. dev_dbg(&client->dev, "fe_sec_mini_cmd=%d\n", fe_sec_mini_cmd);
  993. if (!dev->warm) {
  994. ret = -EAGAIN;
  995. goto err;
  996. }
  997. utmp = dev->cfg->envelope_mode << 5;
  998. ret = m88ds3103_update_bits(dev, 0xa2, 0xe0, utmp);
  999. if (ret)
  1000. goto err;
  1001. switch (fe_sec_mini_cmd) {
  1002. case SEC_MINI_A:
  1003. burst = 0x02;
  1004. break;
  1005. case SEC_MINI_B:
  1006. burst = 0x01;
  1007. break;
  1008. default:
  1009. dev_dbg(&client->dev, "invalid fe_sec_mini_cmd\n");
  1010. ret = -EINVAL;
  1011. goto err;
  1012. }
  1013. ret = regmap_write(dev->regmap, 0xa1, burst);
  1014. if (ret)
  1015. goto err;
  1016. /* wait DiSEqC TX ready */
  1017. #define SEND_BURST_TIMEOUT 40
  1018. timeout = jiffies + msecs_to_jiffies(SEND_BURST_TIMEOUT);
  1019. /* DiSEqC ToneBurst period is 12.5 ms */
  1020. usleep_range(8500, 12500);
  1021. for (utmp = 1; !time_after(jiffies, timeout) && utmp;) {
  1022. ret = regmap_read(dev->regmap, 0xa1, &utmp);
  1023. if (ret)
  1024. goto err;
  1025. utmp = (utmp >> 6) & 0x1;
  1026. }
  1027. if (utmp == 0) {
  1028. dev_dbg(&client->dev, "diseqc tx took %u ms\n",
  1029. jiffies_to_msecs(jiffies) -
  1030. (jiffies_to_msecs(timeout) - SEND_BURST_TIMEOUT));
  1031. } else {
  1032. dev_dbg(&client->dev, "diseqc tx timeout\n");
  1033. ret = m88ds3103_update_bits(dev, 0xa1, 0xc0, 0x40);
  1034. if (ret)
  1035. goto err;
  1036. }
  1037. ret = m88ds3103_update_bits(dev, 0xa2, 0xc0, 0x80);
  1038. if (ret)
  1039. goto err;
  1040. if (utmp == 1) {
  1041. ret = -ETIMEDOUT;
  1042. goto err;
  1043. }
  1044. return 0;
  1045. err:
  1046. dev_dbg(&client->dev, "failed=%d\n", ret);
  1047. return ret;
  1048. }
  1049. static int m88ds3103_get_tune_settings(struct dvb_frontend *fe,
  1050. struct dvb_frontend_tune_settings *s)
  1051. {
  1052. s->min_delay_ms = 3000;
  1053. return 0;
  1054. }
  1055. static void m88ds3103_release(struct dvb_frontend *fe)
  1056. {
  1057. struct m88ds3103_dev *dev = fe->demodulator_priv;
  1058. struct i2c_client *client = dev->client;
  1059. i2c_unregister_device(client);
  1060. }
  1061. static int m88ds3103_select(struct i2c_adapter *adap, void *mux_priv, u32 chan)
  1062. {
  1063. struct m88ds3103_dev *dev = mux_priv;
  1064. struct i2c_client *client = dev->client;
  1065. int ret;
  1066. struct i2c_msg msg = {
  1067. .addr = client->addr,
  1068. .flags = 0,
  1069. .len = 2,
  1070. .buf = "\x03\x11",
  1071. };
  1072. /* Open tuner I2C repeater for 1 xfer, closes automatically */
  1073. ret = __i2c_transfer(client->adapter, &msg, 1);
  1074. if (ret != 1) {
  1075. dev_warn(&client->dev, "i2c wr failed=%d\n", ret);
  1076. if (ret >= 0)
  1077. ret = -EREMOTEIO;
  1078. return ret;
  1079. }
  1080. return 0;
  1081. }
  1082. /*
  1083. * XXX: That is wrapper to m88ds3103_probe() via driver core in order to provide
  1084. * proper I2C client for legacy media attach binding.
  1085. * New users must use I2C client binding directly!
  1086. */
  1087. struct dvb_frontend *m88ds3103_attach(const struct m88ds3103_config *cfg,
  1088. struct i2c_adapter *i2c,
  1089. struct i2c_adapter **tuner_i2c_adapter)
  1090. {
  1091. struct i2c_client *client;
  1092. struct i2c_board_info board_info;
  1093. struct m88ds3103_platform_data pdata = {};
  1094. pdata.clk = cfg->clock;
  1095. pdata.i2c_wr_max = cfg->i2c_wr_max;
  1096. pdata.ts_mode = cfg->ts_mode;
  1097. pdata.ts_clk = cfg->ts_clk;
  1098. pdata.ts_clk_pol = cfg->ts_clk_pol;
  1099. pdata.spec_inv = cfg->spec_inv;
  1100. pdata.agc = cfg->agc;
  1101. pdata.agc_inv = cfg->agc_inv;
  1102. pdata.clk_out = cfg->clock_out;
  1103. pdata.envelope_mode = cfg->envelope_mode;
  1104. pdata.lnb_hv_pol = cfg->lnb_hv_pol;
  1105. pdata.lnb_en_pol = cfg->lnb_en_pol;
  1106. pdata.attach_in_use = true;
  1107. memset(&board_info, 0, sizeof(board_info));
  1108. strlcpy(board_info.type, "m88ds3103", I2C_NAME_SIZE);
  1109. board_info.addr = cfg->i2c_addr;
  1110. board_info.platform_data = &pdata;
  1111. client = i2c_new_device(i2c, &board_info);
  1112. if (!client || !client->dev.driver)
  1113. return NULL;
  1114. *tuner_i2c_adapter = pdata.get_i2c_adapter(client);
  1115. return pdata.get_dvb_frontend(client);
  1116. }
  1117. EXPORT_SYMBOL(m88ds3103_attach);
  1118. static struct dvb_frontend_ops m88ds3103_ops = {
  1119. .delsys = {SYS_DVBS, SYS_DVBS2},
  1120. .info = {
  1121. .name = "Montage Technology M88DS3103",
  1122. .frequency_min = 950000,
  1123. .frequency_max = 2150000,
  1124. .frequency_tolerance = 5000,
  1125. .symbol_rate_min = 1000000,
  1126. .symbol_rate_max = 45000000,
  1127. .caps = FE_CAN_INVERSION_AUTO |
  1128. FE_CAN_FEC_1_2 |
  1129. FE_CAN_FEC_2_3 |
  1130. FE_CAN_FEC_3_4 |
  1131. FE_CAN_FEC_4_5 |
  1132. FE_CAN_FEC_5_6 |
  1133. FE_CAN_FEC_6_7 |
  1134. FE_CAN_FEC_7_8 |
  1135. FE_CAN_FEC_8_9 |
  1136. FE_CAN_FEC_AUTO |
  1137. FE_CAN_QPSK |
  1138. FE_CAN_RECOVER |
  1139. FE_CAN_2G_MODULATION
  1140. },
  1141. .release = m88ds3103_release,
  1142. .get_tune_settings = m88ds3103_get_tune_settings,
  1143. .init = m88ds3103_init,
  1144. .sleep = m88ds3103_sleep,
  1145. .set_frontend = m88ds3103_set_frontend,
  1146. .get_frontend = m88ds3103_get_frontend,
  1147. .read_status = m88ds3103_read_status,
  1148. .read_snr = m88ds3103_read_snr,
  1149. .read_ber = m88ds3103_read_ber,
  1150. .diseqc_send_master_cmd = m88ds3103_diseqc_send_master_cmd,
  1151. .diseqc_send_burst = m88ds3103_diseqc_send_burst,
  1152. .set_tone = m88ds3103_set_tone,
  1153. .set_voltage = m88ds3103_set_voltage,
  1154. };
  1155. static struct dvb_frontend *m88ds3103_get_dvb_frontend(struct i2c_client *client)
  1156. {
  1157. struct m88ds3103_dev *dev = i2c_get_clientdata(client);
  1158. dev_dbg(&client->dev, "\n");
  1159. return &dev->fe;
  1160. }
  1161. static struct i2c_adapter *m88ds3103_get_i2c_adapter(struct i2c_client *client)
  1162. {
  1163. struct m88ds3103_dev *dev = i2c_get_clientdata(client);
  1164. dev_dbg(&client->dev, "\n");
  1165. return dev->i2c_adapter;
  1166. }
  1167. static int m88ds3103_probe(struct i2c_client *client,
  1168. const struct i2c_device_id *id)
  1169. {
  1170. struct m88ds3103_dev *dev;
  1171. struct m88ds3103_platform_data *pdata = client->dev.platform_data;
  1172. int ret;
  1173. unsigned int utmp;
  1174. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1175. if (!dev) {
  1176. ret = -ENOMEM;
  1177. goto err;
  1178. }
  1179. dev->client = client;
  1180. dev->config.clock = pdata->clk;
  1181. dev->config.i2c_wr_max = pdata->i2c_wr_max;
  1182. dev->config.ts_mode = pdata->ts_mode;
  1183. dev->config.ts_clk = pdata->ts_clk;
  1184. dev->config.ts_clk_pol = pdata->ts_clk_pol;
  1185. dev->config.spec_inv = pdata->spec_inv;
  1186. dev->config.agc_inv = pdata->agc_inv;
  1187. dev->config.clock_out = pdata->clk_out;
  1188. dev->config.envelope_mode = pdata->envelope_mode;
  1189. dev->config.agc = pdata->agc;
  1190. dev->config.lnb_hv_pol = pdata->lnb_hv_pol;
  1191. dev->config.lnb_en_pol = pdata->lnb_en_pol;
  1192. dev->cfg = &dev->config;
  1193. /* create regmap */
  1194. dev->regmap_config.reg_bits = 8,
  1195. dev->regmap_config.val_bits = 8,
  1196. dev->regmap_config.lock_arg = dev,
  1197. dev->regmap = devm_regmap_init_i2c(client, &dev->regmap_config);
  1198. if (IS_ERR(dev->regmap)) {
  1199. ret = PTR_ERR(dev->regmap);
  1200. goto err_kfree;
  1201. }
  1202. /* 0x00: chip id[6:0], 0x01: chip ver[7:0], 0x02: chip ver[15:8] */
  1203. ret = regmap_read(dev->regmap, 0x00, &utmp);
  1204. if (ret)
  1205. goto err_kfree;
  1206. dev->chip_id = utmp >> 1;
  1207. dev_dbg(&client->dev, "chip_id=%02x\n", dev->chip_id);
  1208. switch (dev->chip_id) {
  1209. case M88RS6000_CHIP_ID:
  1210. case M88DS3103_CHIP_ID:
  1211. break;
  1212. default:
  1213. ret = -ENODEV;
  1214. dev_err(&client->dev, "Unknown device. Chip_id=%02x\n", dev->chip_id);
  1215. goto err_kfree;
  1216. }
  1217. switch (dev->cfg->clock_out) {
  1218. case M88DS3103_CLOCK_OUT_DISABLED:
  1219. utmp = 0x80;
  1220. break;
  1221. case M88DS3103_CLOCK_OUT_ENABLED:
  1222. utmp = 0x00;
  1223. break;
  1224. case M88DS3103_CLOCK_OUT_ENABLED_DIV2:
  1225. utmp = 0x10;
  1226. break;
  1227. default:
  1228. ret = -EINVAL;
  1229. goto err_kfree;
  1230. }
  1231. /* 0x29 register is defined differently for m88rs6000. */
  1232. /* set internal tuner address to 0x21 */
  1233. if (dev->chip_id == M88RS6000_CHIP_ID)
  1234. utmp = 0x00;
  1235. ret = regmap_write(dev->regmap, 0x29, utmp);
  1236. if (ret)
  1237. goto err_kfree;
  1238. /* sleep */
  1239. ret = m88ds3103_update_bits(dev, 0x08, 0x01, 0x00);
  1240. if (ret)
  1241. goto err_kfree;
  1242. ret = m88ds3103_update_bits(dev, 0x04, 0x01, 0x01);
  1243. if (ret)
  1244. goto err_kfree;
  1245. ret = m88ds3103_update_bits(dev, 0x23, 0x10, 0x10);
  1246. if (ret)
  1247. goto err_kfree;
  1248. /* create mux i2c adapter for tuner */
  1249. dev->i2c_adapter = i2c_add_mux_adapter(client->adapter, &client->dev,
  1250. dev, 0, 0, 0, m88ds3103_select,
  1251. NULL);
  1252. if (dev->i2c_adapter == NULL) {
  1253. ret = -ENOMEM;
  1254. goto err_kfree;
  1255. }
  1256. /* create dvb_frontend */
  1257. memcpy(&dev->fe.ops, &m88ds3103_ops, sizeof(struct dvb_frontend_ops));
  1258. if (dev->chip_id == M88RS6000_CHIP_ID)
  1259. strncpy(dev->fe.ops.info.name, "Montage Technology M88RS6000",
  1260. sizeof(dev->fe.ops.info.name));
  1261. if (!pdata->attach_in_use)
  1262. dev->fe.ops.release = NULL;
  1263. dev->fe.demodulator_priv = dev;
  1264. i2c_set_clientdata(client, dev);
  1265. /* setup callbacks */
  1266. pdata->get_dvb_frontend = m88ds3103_get_dvb_frontend;
  1267. pdata->get_i2c_adapter = m88ds3103_get_i2c_adapter;
  1268. return 0;
  1269. err_kfree:
  1270. kfree(dev);
  1271. err:
  1272. dev_dbg(&client->dev, "failed=%d\n", ret);
  1273. return ret;
  1274. }
  1275. static int m88ds3103_remove(struct i2c_client *client)
  1276. {
  1277. struct m88ds3103_dev *dev = i2c_get_clientdata(client);
  1278. dev_dbg(&client->dev, "\n");
  1279. i2c_del_mux_adapter(dev->i2c_adapter);
  1280. kfree(dev);
  1281. return 0;
  1282. }
  1283. static const struct i2c_device_id m88ds3103_id_table[] = {
  1284. {"m88ds3103", 0},
  1285. {}
  1286. };
  1287. MODULE_DEVICE_TABLE(i2c, m88ds3103_id_table);
  1288. static struct i2c_driver m88ds3103_driver = {
  1289. .driver = {
  1290. .name = "m88ds3103",
  1291. .suppress_bind_attrs = true,
  1292. },
  1293. .probe = m88ds3103_probe,
  1294. .remove = m88ds3103_remove,
  1295. .id_table = m88ds3103_id_table,
  1296. };
  1297. module_i2c_driver(m88ds3103_driver);
  1298. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1299. MODULE_DESCRIPTION("Montage Technology M88DS3103 DVB-S/S2 demodulator driver");
  1300. MODULE_LICENSE("GPL");
  1301. MODULE_FIRMWARE(M88DS3103_FIRMWARE);
  1302. MODULE_FIRMWARE(M88RS6000_FIRMWARE);