tda10071.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. * NXP TDA10071 + Conexant CX24118A DVB-S/S2 demodulator + tuner driver
  3. *
  4. * Copyright (C) 2011 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. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "tda10071_priv.h"
  21. static struct dvb_frontend_ops tda10071_ops;
  22. /*
  23. * XXX: regmap_update_bits() does not fit our needs as it does not support
  24. * partially volatile registers. Also it performs register read even mask is as
  25. * wide as register value.
  26. */
  27. /* write single register with mask */
  28. static int tda10071_wr_reg_mask(struct tda10071_dev *dev,
  29. u8 reg, u8 val, u8 mask)
  30. {
  31. int ret;
  32. u8 tmp;
  33. /* no need for read if whole reg is written */
  34. if (mask != 0xff) {
  35. ret = regmap_bulk_read(dev->regmap, reg, &tmp, 1);
  36. if (ret)
  37. return ret;
  38. val &= mask;
  39. tmp &= ~mask;
  40. val |= tmp;
  41. }
  42. return regmap_bulk_write(dev->regmap, reg, &val, 1);
  43. }
  44. /* execute firmware command */
  45. static int tda10071_cmd_execute(struct tda10071_dev *dev,
  46. struct tda10071_cmd *cmd)
  47. {
  48. struct i2c_client *client = dev->client;
  49. int ret, i;
  50. unsigned int uitmp;
  51. if (!dev->warm) {
  52. ret = -EFAULT;
  53. goto error;
  54. }
  55. mutex_lock(&dev->cmd_execute_mutex);
  56. /* write cmd and args for firmware */
  57. ret = regmap_bulk_write(dev->regmap, 0x00, cmd->args, cmd->len);
  58. if (ret)
  59. goto error_mutex_unlock;
  60. /* start cmd execution */
  61. ret = regmap_write(dev->regmap, 0x1f, 1);
  62. if (ret)
  63. goto error_mutex_unlock;
  64. /* wait cmd execution terminate */
  65. for (i = 1000, uitmp = 1; i && uitmp; i--) {
  66. ret = regmap_read(dev->regmap, 0x1f, &uitmp);
  67. if (ret)
  68. goto error_mutex_unlock;
  69. usleep_range(200, 5000);
  70. }
  71. mutex_unlock(&dev->cmd_execute_mutex);
  72. dev_dbg(&client->dev, "loop=%d\n", i);
  73. if (i == 0) {
  74. ret = -ETIMEDOUT;
  75. goto error;
  76. }
  77. return ret;
  78. error_mutex_unlock:
  79. mutex_unlock(&dev->cmd_execute_mutex);
  80. error:
  81. dev_dbg(&client->dev, "failed=%d\n", ret);
  82. return ret;
  83. }
  84. static int tda10071_set_tone(struct dvb_frontend *fe,
  85. enum fe_sec_tone_mode fe_sec_tone_mode)
  86. {
  87. struct tda10071_dev *dev = fe->demodulator_priv;
  88. struct i2c_client *client = dev->client;
  89. struct tda10071_cmd cmd;
  90. int ret;
  91. u8 tone;
  92. if (!dev->warm) {
  93. ret = -EFAULT;
  94. goto error;
  95. }
  96. dev_dbg(&client->dev, "tone_mode=%d\n", fe_sec_tone_mode);
  97. switch (fe_sec_tone_mode) {
  98. case SEC_TONE_ON:
  99. tone = 1;
  100. break;
  101. case SEC_TONE_OFF:
  102. tone = 0;
  103. break;
  104. default:
  105. dev_dbg(&client->dev, "invalid fe_sec_tone_mode\n");
  106. ret = -EINVAL;
  107. goto error;
  108. }
  109. cmd.args[0] = CMD_LNB_PCB_CONFIG;
  110. cmd.args[1] = 0;
  111. cmd.args[2] = 0x00;
  112. cmd.args[3] = 0x00;
  113. cmd.args[4] = tone;
  114. cmd.len = 5;
  115. ret = tda10071_cmd_execute(dev, &cmd);
  116. if (ret)
  117. goto error;
  118. return ret;
  119. error:
  120. dev_dbg(&client->dev, "failed=%d\n", ret);
  121. return ret;
  122. }
  123. static int tda10071_set_voltage(struct dvb_frontend *fe,
  124. enum fe_sec_voltage fe_sec_voltage)
  125. {
  126. struct tda10071_dev *dev = fe->demodulator_priv;
  127. struct i2c_client *client = dev->client;
  128. struct tda10071_cmd cmd;
  129. int ret;
  130. u8 voltage;
  131. if (!dev->warm) {
  132. ret = -EFAULT;
  133. goto error;
  134. }
  135. dev_dbg(&client->dev, "voltage=%d\n", fe_sec_voltage);
  136. switch (fe_sec_voltage) {
  137. case SEC_VOLTAGE_13:
  138. voltage = 0;
  139. break;
  140. case SEC_VOLTAGE_18:
  141. voltage = 1;
  142. break;
  143. case SEC_VOLTAGE_OFF:
  144. voltage = 0;
  145. break;
  146. default:
  147. dev_dbg(&client->dev, "invalid fe_sec_voltage\n");
  148. ret = -EINVAL;
  149. goto error;
  150. }
  151. cmd.args[0] = CMD_LNB_SET_DC_LEVEL;
  152. cmd.args[1] = 0;
  153. cmd.args[2] = voltage;
  154. cmd.len = 3;
  155. ret = tda10071_cmd_execute(dev, &cmd);
  156. if (ret)
  157. goto error;
  158. return ret;
  159. error:
  160. dev_dbg(&client->dev, "failed=%d\n", ret);
  161. return ret;
  162. }
  163. static int tda10071_diseqc_send_master_cmd(struct dvb_frontend *fe,
  164. struct dvb_diseqc_master_cmd *diseqc_cmd)
  165. {
  166. struct tda10071_dev *dev = fe->demodulator_priv;
  167. struct i2c_client *client = dev->client;
  168. struct tda10071_cmd cmd;
  169. int ret, i;
  170. unsigned int uitmp;
  171. if (!dev->warm) {
  172. ret = -EFAULT;
  173. goto error;
  174. }
  175. dev_dbg(&client->dev, "msg_len=%d\n", diseqc_cmd->msg_len);
  176. if (diseqc_cmd->msg_len < 3 || diseqc_cmd->msg_len > 6) {
  177. ret = -EINVAL;
  178. goto error;
  179. }
  180. /* wait LNB TX */
  181. for (i = 500, uitmp = 0; i && !uitmp; i--) {
  182. ret = regmap_read(dev->regmap, 0x47, &uitmp);
  183. if (ret)
  184. goto error;
  185. uitmp = (uitmp >> 0) & 1;
  186. usleep_range(10000, 20000);
  187. }
  188. dev_dbg(&client->dev, "loop=%d\n", i);
  189. if (i == 0) {
  190. ret = -ETIMEDOUT;
  191. goto error;
  192. }
  193. ret = regmap_update_bits(dev->regmap, 0x47, 0x01, 0x00);
  194. if (ret)
  195. goto error;
  196. cmd.args[0] = CMD_LNB_SEND_DISEQC;
  197. cmd.args[1] = 0;
  198. cmd.args[2] = 0;
  199. cmd.args[3] = 0;
  200. cmd.args[4] = 2;
  201. cmd.args[5] = 0;
  202. cmd.args[6] = diseqc_cmd->msg_len;
  203. memcpy(&cmd.args[7], diseqc_cmd->msg, diseqc_cmd->msg_len);
  204. cmd.len = 7 + diseqc_cmd->msg_len;
  205. ret = tda10071_cmd_execute(dev, &cmd);
  206. if (ret)
  207. goto error;
  208. return ret;
  209. error:
  210. dev_dbg(&client->dev, "failed=%d\n", ret);
  211. return ret;
  212. }
  213. static int tda10071_diseqc_recv_slave_reply(struct dvb_frontend *fe,
  214. struct dvb_diseqc_slave_reply *reply)
  215. {
  216. struct tda10071_dev *dev = fe->demodulator_priv;
  217. struct i2c_client *client = dev->client;
  218. struct tda10071_cmd cmd;
  219. int ret, i;
  220. unsigned int uitmp;
  221. if (!dev->warm) {
  222. ret = -EFAULT;
  223. goto error;
  224. }
  225. dev_dbg(&client->dev, "\n");
  226. /* wait LNB RX */
  227. for (i = 500, uitmp = 0; i && !uitmp; i--) {
  228. ret = regmap_read(dev->regmap, 0x47, &uitmp);
  229. if (ret)
  230. goto error;
  231. uitmp = (uitmp >> 1) & 1;
  232. usleep_range(10000, 20000);
  233. }
  234. dev_dbg(&client->dev, "loop=%d\n", i);
  235. if (i == 0) {
  236. ret = -ETIMEDOUT;
  237. goto error;
  238. }
  239. /* reply len */
  240. ret = regmap_read(dev->regmap, 0x46, &uitmp);
  241. if (ret)
  242. goto error;
  243. reply->msg_len = uitmp & 0x1f; /* [4:0] */
  244. if (reply->msg_len > sizeof(reply->msg))
  245. reply->msg_len = sizeof(reply->msg); /* truncate API max */
  246. /* read reply */
  247. cmd.args[0] = CMD_LNB_UPDATE_REPLY;
  248. cmd.args[1] = 0;
  249. cmd.len = 2;
  250. ret = tda10071_cmd_execute(dev, &cmd);
  251. if (ret)
  252. goto error;
  253. ret = regmap_bulk_read(dev->regmap, cmd.len, reply->msg,
  254. reply->msg_len);
  255. if (ret)
  256. goto error;
  257. return ret;
  258. error:
  259. dev_dbg(&client->dev, "failed=%d\n", ret);
  260. return ret;
  261. }
  262. static int tda10071_diseqc_send_burst(struct dvb_frontend *fe,
  263. enum fe_sec_mini_cmd fe_sec_mini_cmd)
  264. {
  265. struct tda10071_dev *dev = fe->demodulator_priv;
  266. struct i2c_client *client = dev->client;
  267. struct tda10071_cmd cmd;
  268. int ret, i;
  269. unsigned int uitmp;
  270. u8 burst;
  271. if (!dev->warm) {
  272. ret = -EFAULT;
  273. goto error;
  274. }
  275. dev_dbg(&client->dev, "fe_sec_mini_cmd=%d\n", fe_sec_mini_cmd);
  276. switch (fe_sec_mini_cmd) {
  277. case SEC_MINI_A:
  278. burst = 0;
  279. break;
  280. case SEC_MINI_B:
  281. burst = 1;
  282. break;
  283. default:
  284. dev_dbg(&client->dev, "invalid fe_sec_mini_cmd\n");
  285. ret = -EINVAL;
  286. goto error;
  287. }
  288. /* wait LNB TX */
  289. for (i = 500, uitmp = 0; i && !uitmp; i--) {
  290. ret = regmap_read(dev->regmap, 0x47, &uitmp);
  291. if (ret)
  292. goto error;
  293. uitmp = (uitmp >> 0) & 1;
  294. usleep_range(10000, 20000);
  295. }
  296. dev_dbg(&client->dev, "loop=%d\n", i);
  297. if (i == 0) {
  298. ret = -ETIMEDOUT;
  299. goto error;
  300. }
  301. ret = regmap_update_bits(dev->regmap, 0x47, 0x01, 0x00);
  302. if (ret)
  303. goto error;
  304. cmd.args[0] = CMD_LNB_SEND_TONEBURST;
  305. cmd.args[1] = 0;
  306. cmd.args[2] = burst;
  307. cmd.len = 3;
  308. ret = tda10071_cmd_execute(dev, &cmd);
  309. if (ret)
  310. goto error;
  311. return ret;
  312. error:
  313. dev_dbg(&client->dev, "failed=%d\n", ret);
  314. return ret;
  315. }
  316. static int tda10071_read_status(struct dvb_frontend *fe, enum fe_status *status)
  317. {
  318. struct tda10071_dev *dev = fe->demodulator_priv;
  319. struct i2c_client *client = dev->client;
  320. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  321. struct tda10071_cmd cmd;
  322. int ret;
  323. unsigned int uitmp;
  324. u8 buf[8];
  325. *status = 0;
  326. if (!dev->warm) {
  327. ret = 0;
  328. goto error;
  329. }
  330. ret = regmap_read(dev->regmap, 0x39, &uitmp);
  331. if (ret)
  332. goto error;
  333. /* 0x39[0] tuner PLL */
  334. if (uitmp & 0x02) /* demod PLL */
  335. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER;
  336. if (uitmp & 0x04) /* viterbi or LDPC*/
  337. *status |= FE_HAS_VITERBI;
  338. if (uitmp & 0x08) /* RS or BCH */
  339. *status |= FE_HAS_SYNC | FE_HAS_LOCK;
  340. dev->fe_status = *status;
  341. /* signal strength */
  342. if (dev->fe_status & FE_HAS_SIGNAL) {
  343. cmd.args[0] = CMD_GET_AGCACC;
  344. cmd.args[1] = 0;
  345. cmd.len = 2;
  346. ret = tda10071_cmd_execute(dev, &cmd);
  347. if (ret)
  348. goto error;
  349. /* input power estimate dBm */
  350. ret = regmap_read(dev->regmap, 0x50, &uitmp);
  351. if (ret)
  352. goto error;
  353. c->strength.stat[0].scale = FE_SCALE_DECIBEL;
  354. c->strength.stat[0].svalue = (int) (uitmp - 256) * 1000;
  355. } else {
  356. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  357. }
  358. /* CNR */
  359. if (dev->fe_status & FE_HAS_VITERBI) {
  360. /* Es/No */
  361. ret = regmap_bulk_read(dev->regmap, 0x3a, buf, 2);
  362. if (ret)
  363. goto error;
  364. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  365. c->cnr.stat[0].svalue = (buf[0] << 8 | buf[1] << 0) * 100;
  366. } else {
  367. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  368. }
  369. /* UCB/PER/BER */
  370. if (dev->fe_status & FE_HAS_LOCK) {
  371. /* TODO: report total bits/packets */
  372. u8 delivery_system, reg, len;
  373. switch (dev->delivery_system) {
  374. case SYS_DVBS:
  375. reg = 0x4c;
  376. len = 8;
  377. delivery_system = 1;
  378. break;
  379. case SYS_DVBS2:
  380. reg = 0x4d;
  381. len = 4;
  382. delivery_system = 0;
  383. break;
  384. default:
  385. ret = -EINVAL;
  386. goto error;
  387. }
  388. ret = regmap_read(dev->regmap, reg, &uitmp);
  389. if (ret)
  390. goto error;
  391. if (dev->meas_count == uitmp) {
  392. dev_dbg(&client->dev, "meas not ready=%02x\n", uitmp);
  393. ret = 0;
  394. goto error;
  395. } else {
  396. dev->meas_count = uitmp;
  397. }
  398. cmd.args[0] = CMD_BER_UPDATE_COUNTERS;
  399. cmd.args[1] = 0;
  400. cmd.args[2] = delivery_system;
  401. cmd.len = 3;
  402. ret = tda10071_cmd_execute(dev, &cmd);
  403. if (ret)
  404. goto error;
  405. ret = regmap_bulk_read(dev->regmap, cmd.len, buf, len);
  406. if (ret)
  407. goto error;
  408. if (dev->delivery_system == SYS_DVBS) {
  409. dev->dvbv3_ber = buf[0] << 24 | buf[1] << 16 |
  410. buf[2] << 8 | buf[3] << 0;
  411. dev->post_bit_error += buf[0] << 24 | buf[1] << 16 |
  412. buf[2] << 8 | buf[3] << 0;
  413. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  414. c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
  415. dev->block_error += buf[4] << 8 | buf[5] << 0;
  416. c->block_error.stat[0].scale = FE_SCALE_COUNTER;
  417. c->block_error.stat[0].uvalue = dev->block_error;
  418. } else {
  419. dev->dvbv3_ber = buf[0] << 8 | buf[1] << 0;
  420. dev->post_bit_error += buf[0] << 8 | buf[1] << 0;
  421. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  422. c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
  423. c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  424. }
  425. } else {
  426. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  427. c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  428. }
  429. return ret;
  430. error:
  431. dev_dbg(&client->dev, "failed=%d\n", ret);
  432. return ret;
  433. }
  434. static int tda10071_read_snr(struct dvb_frontend *fe, u16 *snr)
  435. {
  436. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  437. if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL)
  438. *snr = div_s64(c->cnr.stat[0].svalue, 100);
  439. else
  440. *snr = 0;
  441. return 0;
  442. }
  443. static int tda10071_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  444. {
  445. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  446. unsigned int uitmp;
  447. if (c->strength.stat[0].scale == FE_SCALE_DECIBEL) {
  448. uitmp = div_s64(c->strength.stat[0].svalue, 1000) + 256;
  449. uitmp = clamp(uitmp, 181U, 236U); /* -75dBm - -20dBm */
  450. /* scale value to 0x0000-0xffff */
  451. *strength = (uitmp-181) * 0xffff / (236-181);
  452. } else {
  453. *strength = 0;
  454. }
  455. return 0;
  456. }
  457. static int tda10071_read_ber(struct dvb_frontend *fe, u32 *ber)
  458. {
  459. struct tda10071_dev *dev = fe->demodulator_priv;
  460. *ber = dev->dvbv3_ber;
  461. return 0;
  462. }
  463. static int tda10071_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  464. {
  465. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  466. if (c->block_error.stat[0].scale == FE_SCALE_COUNTER)
  467. *ucblocks = c->block_error.stat[0].uvalue;
  468. else
  469. *ucblocks = 0;
  470. return 0;
  471. }
  472. static int tda10071_set_frontend(struct dvb_frontend *fe)
  473. {
  474. struct tda10071_dev *dev = fe->demodulator_priv;
  475. struct i2c_client *client = dev->client;
  476. struct tda10071_cmd cmd;
  477. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  478. int ret, i;
  479. u8 mode, rolloff, pilot, inversion, div;
  480. enum fe_modulation modulation;
  481. dev_dbg(&client->dev,
  482. "delivery_system=%d modulation=%d frequency=%u symbol_rate=%d inversion=%d pilot=%d rolloff=%d\n",
  483. c->delivery_system, c->modulation, c->frequency, c->symbol_rate,
  484. c->inversion, c->pilot, c->rolloff);
  485. dev->delivery_system = SYS_UNDEFINED;
  486. if (!dev->warm) {
  487. ret = -EFAULT;
  488. goto error;
  489. }
  490. switch (c->inversion) {
  491. case INVERSION_OFF:
  492. inversion = 1;
  493. break;
  494. case INVERSION_ON:
  495. inversion = 0;
  496. break;
  497. case INVERSION_AUTO:
  498. /* 2 = auto; try first on then off
  499. * 3 = auto; try first off then on */
  500. inversion = 3;
  501. break;
  502. default:
  503. dev_dbg(&client->dev, "invalid inversion\n");
  504. ret = -EINVAL;
  505. goto error;
  506. }
  507. switch (c->delivery_system) {
  508. case SYS_DVBS:
  509. modulation = QPSK;
  510. rolloff = 0;
  511. pilot = 2;
  512. break;
  513. case SYS_DVBS2:
  514. modulation = c->modulation;
  515. switch (c->rolloff) {
  516. case ROLLOFF_20:
  517. rolloff = 2;
  518. break;
  519. case ROLLOFF_25:
  520. rolloff = 1;
  521. break;
  522. case ROLLOFF_35:
  523. rolloff = 0;
  524. break;
  525. case ROLLOFF_AUTO:
  526. default:
  527. dev_dbg(&client->dev, "invalid rolloff\n");
  528. ret = -EINVAL;
  529. goto error;
  530. }
  531. switch (c->pilot) {
  532. case PILOT_OFF:
  533. pilot = 0;
  534. break;
  535. case PILOT_ON:
  536. pilot = 1;
  537. break;
  538. case PILOT_AUTO:
  539. pilot = 2;
  540. break;
  541. default:
  542. dev_dbg(&client->dev, "invalid pilot\n");
  543. ret = -EINVAL;
  544. goto error;
  545. }
  546. break;
  547. default:
  548. dev_dbg(&client->dev, "invalid delivery_system\n");
  549. ret = -EINVAL;
  550. goto error;
  551. }
  552. for (i = 0, mode = 0xff; i < ARRAY_SIZE(TDA10071_MODCOD); i++) {
  553. if (c->delivery_system == TDA10071_MODCOD[i].delivery_system &&
  554. modulation == TDA10071_MODCOD[i].modulation &&
  555. c->fec_inner == TDA10071_MODCOD[i].fec) {
  556. mode = TDA10071_MODCOD[i].val;
  557. dev_dbg(&client->dev, "mode found=%02x\n", mode);
  558. break;
  559. }
  560. }
  561. if (mode == 0xff) {
  562. dev_dbg(&client->dev, "invalid parameter combination\n");
  563. ret = -EINVAL;
  564. goto error;
  565. }
  566. if (c->symbol_rate <= 5000000)
  567. div = 14;
  568. else
  569. div = 4;
  570. ret = regmap_write(dev->regmap, 0x81, div);
  571. if (ret)
  572. goto error;
  573. ret = regmap_write(dev->regmap, 0xe3, div);
  574. if (ret)
  575. goto error;
  576. cmd.args[0] = CMD_CHANGE_CHANNEL;
  577. cmd.args[1] = 0;
  578. cmd.args[2] = mode;
  579. cmd.args[3] = (c->frequency >> 16) & 0xff;
  580. cmd.args[4] = (c->frequency >> 8) & 0xff;
  581. cmd.args[5] = (c->frequency >> 0) & 0xff;
  582. cmd.args[6] = ((c->symbol_rate / 1000) >> 8) & 0xff;
  583. cmd.args[7] = ((c->symbol_rate / 1000) >> 0) & 0xff;
  584. cmd.args[8] = (tda10071_ops.info.frequency_tolerance >> 8) & 0xff;
  585. cmd.args[9] = (tda10071_ops.info.frequency_tolerance >> 0) & 0xff;
  586. cmd.args[10] = rolloff;
  587. cmd.args[11] = inversion;
  588. cmd.args[12] = pilot;
  589. cmd.args[13] = 0x00;
  590. cmd.args[14] = 0x00;
  591. cmd.len = 15;
  592. ret = tda10071_cmd_execute(dev, &cmd);
  593. if (ret)
  594. goto error;
  595. dev->delivery_system = c->delivery_system;
  596. return ret;
  597. error:
  598. dev_dbg(&client->dev, "failed=%d\n", ret);
  599. return ret;
  600. }
  601. static int tda10071_get_frontend(struct dvb_frontend *fe)
  602. {
  603. struct tda10071_dev *dev = fe->demodulator_priv;
  604. struct i2c_client *client = dev->client;
  605. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  606. int ret, i;
  607. u8 buf[5], tmp;
  608. if (!dev->warm || !(dev->fe_status & FE_HAS_LOCK)) {
  609. ret = 0;
  610. goto error;
  611. }
  612. ret = regmap_bulk_read(dev->regmap, 0x30, buf, 5);
  613. if (ret)
  614. goto error;
  615. tmp = buf[0] & 0x3f;
  616. for (i = 0; i < ARRAY_SIZE(TDA10071_MODCOD); i++) {
  617. if (tmp == TDA10071_MODCOD[i].val) {
  618. c->modulation = TDA10071_MODCOD[i].modulation;
  619. c->fec_inner = TDA10071_MODCOD[i].fec;
  620. c->delivery_system = TDA10071_MODCOD[i].delivery_system;
  621. }
  622. }
  623. switch ((buf[1] >> 0) & 0x01) {
  624. case 0:
  625. c->inversion = INVERSION_ON;
  626. break;
  627. case 1:
  628. c->inversion = INVERSION_OFF;
  629. break;
  630. }
  631. switch ((buf[1] >> 7) & 0x01) {
  632. case 0:
  633. c->pilot = PILOT_OFF;
  634. break;
  635. case 1:
  636. c->pilot = PILOT_ON;
  637. break;
  638. }
  639. c->frequency = (buf[2] << 16) | (buf[3] << 8) | (buf[4] << 0);
  640. ret = regmap_bulk_read(dev->regmap, 0x52, buf, 3);
  641. if (ret)
  642. goto error;
  643. c->symbol_rate = ((buf[0] << 16) | (buf[1] << 8) | (buf[2] << 0)) * 1000;
  644. return ret;
  645. error:
  646. dev_dbg(&client->dev, "failed=%d\n", ret);
  647. return ret;
  648. }
  649. static int tda10071_init(struct dvb_frontend *fe)
  650. {
  651. struct tda10071_dev *dev = fe->demodulator_priv;
  652. struct i2c_client *client = dev->client;
  653. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  654. struct tda10071_cmd cmd;
  655. int ret, i, len, remaining, fw_size;
  656. unsigned int uitmp;
  657. const struct firmware *fw;
  658. u8 *fw_file = TDA10071_FIRMWARE;
  659. u8 tmp, buf[4];
  660. struct tda10071_reg_val_mask tab[] = {
  661. { 0xcd, 0x00, 0x07 },
  662. { 0x80, 0x00, 0x02 },
  663. { 0xcd, 0x00, 0xc0 },
  664. { 0xce, 0x00, 0x1b },
  665. { 0x9d, 0x00, 0x01 },
  666. { 0x9d, 0x00, 0x02 },
  667. { 0x9e, 0x00, 0x01 },
  668. { 0x87, 0x00, 0x80 },
  669. { 0xce, 0x00, 0x08 },
  670. { 0xce, 0x00, 0x10 },
  671. };
  672. struct tda10071_reg_val_mask tab2[] = {
  673. { 0xf1, 0x70, 0xff },
  674. { 0x88, dev->pll_multiplier, 0x3f },
  675. { 0x89, 0x00, 0x10 },
  676. { 0x89, 0x10, 0x10 },
  677. { 0xc0, 0x01, 0x01 },
  678. { 0xc0, 0x00, 0x01 },
  679. { 0xe0, 0xff, 0xff },
  680. { 0xe0, 0x00, 0xff },
  681. { 0x96, 0x1e, 0x7e },
  682. { 0x8b, 0x08, 0x08 },
  683. { 0x8b, 0x00, 0x08 },
  684. { 0x8f, 0x1a, 0x7e },
  685. { 0x8c, 0x68, 0xff },
  686. { 0x8d, 0x08, 0xff },
  687. { 0x8e, 0x4c, 0xff },
  688. { 0x8f, 0x01, 0x01 },
  689. { 0x8b, 0x04, 0x04 },
  690. { 0x8b, 0x00, 0x04 },
  691. { 0x87, 0x05, 0x07 },
  692. { 0x80, 0x00, 0x20 },
  693. { 0xc8, 0x01, 0xff },
  694. { 0xb4, 0x47, 0xff },
  695. { 0xb5, 0x9c, 0xff },
  696. { 0xb6, 0x7d, 0xff },
  697. { 0xba, 0x00, 0x03 },
  698. { 0xb7, 0x47, 0xff },
  699. { 0xb8, 0x9c, 0xff },
  700. { 0xb9, 0x7d, 0xff },
  701. { 0xba, 0x00, 0x0c },
  702. { 0xc8, 0x00, 0xff },
  703. { 0xcd, 0x00, 0x04 },
  704. { 0xcd, 0x00, 0x20 },
  705. { 0xe8, 0x02, 0xff },
  706. { 0xcf, 0x20, 0xff },
  707. { 0x9b, 0xd7, 0xff },
  708. { 0x9a, 0x01, 0x03 },
  709. { 0xa8, 0x05, 0x0f },
  710. { 0xa8, 0x65, 0xf0 },
  711. { 0xa6, 0xa0, 0xf0 },
  712. { 0x9d, 0x50, 0xfc },
  713. { 0x9e, 0x20, 0xe0 },
  714. { 0xa3, 0x1c, 0x7c },
  715. { 0xd5, 0x03, 0x03 },
  716. };
  717. if (dev->warm) {
  718. /* warm state - wake up device from sleep */
  719. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  720. ret = tda10071_wr_reg_mask(dev, tab[i].reg,
  721. tab[i].val, tab[i].mask);
  722. if (ret)
  723. goto error;
  724. }
  725. cmd.args[0] = CMD_SET_SLEEP_MODE;
  726. cmd.args[1] = 0;
  727. cmd.args[2] = 0;
  728. cmd.len = 3;
  729. ret = tda10071_cmd_execute(dev, &cmd);
  730. if (ret)
  731. goto error;
  732. } else {
  733. /* cold state - try to download firmware */
  734. /* request the firmware, this will block and timeout */
  735. ret = request_firmware(&fw, fw_file, &client->dev);
  736. if (ret) {
  737. dev_err(&client->dev,
  738. "did not find the firmware file. (%s) Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)\n",
  739. fw_file, ret);
  740. goto error;
  741. }
  742. /* init */
  743. for (i = 0; i < ARRAY_SIZE(tab2); i++) {
  744. ret = tda10071_wr_reg_mask(dev, tab2[i].reg,
  745. tab2[i].val, tab2[i].mask);
  746. if (ret)
  747. goto error_release_firmware;
  748. }
  749. /* download firmware */
  750. ret = regmap_write(dev->regmap, 0xe0, 0x7f);
  751. if (ret)
  752. goto error_release_firmware;
  753. ret = regmap_write(dev->regmap, 0xf7, 0x81);
  754. if (ret)
  755. goto error_release_firmware;
  756. ret = regmap_write(dev->regmap, 0xf8, 0x00);
  757. if (ret)
  758. goto error_release_firmware;
  759. ret = regmap_write(dev->regmap, 0xf9, 0x00);
  760. if (ret)
  761. goto error_release_firmware;
  762. dev_info(&client->dev,
  763. "found a '%s' in cold state, will try to load a firmware\n",
  764. tda10071_ops.info.name);
  765. dev_info(&client->dev, "downloading firmware from file '%s'\n",
  766. fw_file);
  767. /* do not download last byte */
  768. fw_size = fw->size - 1;
  769. for (remaining = fw_size; remaining > 0;
  770. remaining -= (dev->i2c_wr_max - 1)) {
  771. len = remaining;
  772. if (len > (dev->i2c_wr_max - 1))
  773. len = (dev->i2c_wr_max - 1);
  774. ret = regmap_bulk_write(dev->regmap, 0xfa,
  775. (u8 *) &fw->data[fw_size - remaining], len);
  776. if (ret) {
  777. dev_err(&client->dev,
  778. "firmware download failed=%d\n", ret);
  779. goto error_release_firmware;
  780. }
  781. }
  782. release_firmware(fw);
  783. ret = regmap_write(dev->regmap, 0xf7, 0x0c);
  784. if (ret)
  785. goto error;
  786. ret = regmap_write(dev->regmap, 0xe0, 0x00);
  787. if (ret)
  788. goto error;
  789. /* wait firmware start */
  790. msleep(250);
  791. /* firmware status */
  792. ret = regmap_read(dev->regmap, 0x51, &uitmp);
  793. if (ret)
  794. goto error;
  795. if (uitmp) {
  796. dev_info(&client->dev, "firmware did not run\n");
  797. ret = -EFAULT;
  798. goto error;
  799. } else {
  800. dev->warm = true;
  801. }
  802. cmd.args[0] = CMD_GET_FW_VERSION;
  803. cmd.len = 1;
  804. ret = tda10071_cmd_execute(dev, &cmd);
  805. if (ret)
  806. goto error;
  807. ret = regmap_bulk_read(dev->regmap, cmd.len, buf, 4);
  808. if (ret)
  809. goto error;
  810. dev_info(&client->dev, "firmware version %d.%d.%d.%d\n",
  811. buf[0], buf[1], buf[2], buf[3]);
  812. dev_info(&client->dev, "found a '%s' in warm state\n",
  813. tda10071_ops.info.name);
  814. ret = regmap_bulk_read(dev->regmap, 0x81, buf, 2);
  815. if (ret)
  816. goto error;
  817. cmd.args[0] = CMD_DEMOD_INIT;
  818. cmd.args[1] = ((dev->clk / 1000) >> 8) & 0xff;
  819. cmd.args[2] = ((dev->clk / 1000) >> 0) & 0xff;
  820. cmd.args[3] = buf[0];
  821. cmd.args[4] = buf[1];
  822. cmd.args[5] = dev->pll_multiplier;
  823. cmd.args[6] = dev->spec_inv;
  824. cmd.args[7] = 0x00;
  825. cmd.len = 8;
  826. ret = tda10071_cmd_execute(dev, &cmd);
  827. if (ret)
  828. goto error;
  829. if (dev->tuner_i2c_addr)
  830. tmp = dev->tuner_i2c_addr;
  831. else
  832. tmp = 0x14;
  833. cmd.args[0] = CMD_TUNER_INIT;
  834. cmd.args[1] = 0x00;
  835. cmd.args[2] = 0x00;
  836. cmd.args[3] = 0x00;
  837. cmd.args[4] = 0x00;
  838. cmd.args[5] = tmp;
  839. cmd.args[6] = 0x00;
  840. cmd.args[7] = 0x03;
  841. cmd.args[8] = 0x02;
  842. cmd.args[9] = 0x02;
  843. cmd.args[10] = 0x00;
  844. cmd.args[11] = 0x00;
  845. cmd.args[12] = 0x00;
  846. cmd.args[13] = 0x00;
  847. cmd.args[14] = 0x00;
  848. cmd.len = 15;
  849. ret = tda10071_cmd_execute(dev, &cmd);
  850. if (ret)
  851. goto error;
  852. cmd.args[0] = CMD_MPEG_CONFIG;
  853. cmd.args[1] = 0;
  854. cmd.args[2] = dev->ts_mode;
  855. cmd.args[3] = 0x00;
  856. cmd.args[4] = 0x04;
  857. cmd.args[5] = 0x00;
  858. cmd.len = 6;
  859. ret = tda10071_cmd_execute(dev, &cmd);
  860. if (ret)
  861. goto error;
  862. ret = regmap_update_bits(dev->regmap, 0xf0, 0x01, 0x01);
  863. if (ret)
  864. goto error;
  865. cmd.args[0] = CMD_LNB_CONFIG;
  866. cmd.args[1] = 0;
  867. cmd.args[2] = 150;
  868. cmd.args[3] = 3;
  869. cmd.args[4] = 22;
  870. cmd.args[5] = 1;
  871. cmd.args[6] = 1;
  872. cmd.args[7] = 30;
  873. cmd.args[8] = 30;
  874. cmd.args[9] = 30;
  875. cmd.args[10] = 30;
  876. cmd.len = 11;
  877. ret = tda10071_cmd_execute(dev, &cmd);
  878. if (ret)
  879. goto error;
  880. cmd.args[0] = CMD_BER_CONTROL;
  881. cmd.args[1] = 0;
  882. cmd.args[2] = 14;
  883. cmd.args[3] = 14;
  884. cmd.len = 4;
  885. ret = tda10071_cmd_execute(dev, &cmd);
  886. if (ret)
  887. goto error;
  888. }
  889. /* init stats here in order signal app which stats are supported */
  890. c->strength.len = 1;
  891. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  892. c->cnr.len = 1;
  893. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  894. c->post_bit_error.len = 1;
  895. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  896. c->block_error.len = 1;
  897. c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  898. return ret;
  899. error_release_firmware:
  900. release_firmware(fw);
  901. error:
  902. dev_dbg(&client->dev, "failed=%d\n", ret);
  903. return ret;
  904. }
  905. static int tda10071_sleep(struct dvb_frontend *fe)
  906. {
  907. struct tda10071_dev *dev = fe->demodulator_priv;
  908. struct i2c_client *client = dev->client;
  909. struct tda10071_cmd cmd;
  910. int ret, i;
  911. struct tda10071_reg_val_mask tab[] = {
  912. { 0xcd, 0x07, 0x07 },
  913. { 0x80, 0x02, 0x02 },
  914. { 0xcd, 0xc0, 0xc0 },
  915. { 0xce, 0x1b, 0x1b },
  916. { 0x9d, 0x01, 0x01 },
  917. { 0x9d, 0x02, 0x02 },
  918. { 0x9e, 0x01, 0x01 },
  919. { 0x87, 0x80, 0x80 },
  920. { 0xce, 0x08, 0x08 },
  921. { 0xce, 0x10, 0x10 },
  922. };
  923. if (!dev->warm) {
  924. ret = -EFAULT;
  925. goto error;
  926. }
  927. cmd.args[0] = CMD_SET_SLEEP_MODE;
  928. cmd.args[1] = 0;
  929. cmd.args[2] = 1;
  930. cmd.len = 3;
  931. ret = tda10071_cmd_execute(dev, &cmd);
  932. if (ret)
  933. goto error;
  934. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  935. ret = tda10071_wr_reg_mask(dev, tab[i].reg, tab[i].val,
  936. tab[i].mask);
  937. if (ret)
  938. goto error;
  939. }
  940. return ret;
  941. error:
  942. dev_dbg(&client->dev, "failed=%d\n", ret);
  943. return ret;
  944. }
  945. static int tda10071_get_tune_settings(struct dvb_frontend *fe,
  946. struct dvb_frontend_tune_settings *s)
  947. {
  948. s->min_delay_ms = 8000;
  949. s->step_size = 0;
  950. s->max_drift = 0;
  951. return 0;
  952. }
  953. static struct dvb_frontend_ops tda10071_ops = {
  954. .delsys = { SYS_DVBS, SYS_DVBS2 },
  955. .info = {
  956. .name = "NXP TDA10071",
  957. .frequency_min = 950000,
  958. .frequency_max = 2150000,
  959. .frequency_tolerance = 5000,
  960. .symbol_rate_min = 1000000,
  961. .symbol_rate_max = 45000000,
  962. .caps = FE_CAN_INVERSION_AUTO |
  963. FE_CAN_FEC_1_2 |
  964. FE_CAN_FEC_2_3 |
  965. FE_CAN_FEC_3_4 |
  966. FE_CAN_FEC_4_5 |
  967. FE_CAN_FEC_5_6 |
  968. FE_CAN_FEC_6_7 |
  969. FE_CAN_FEC_7_8 |
  970. FE_CAN_FEC_8_9 |
  971. FE_CAN_FEC_AUTO |
  972. FE_CAN_QPSK |
  973. FE_CAN_RECOVER |
  974. FE_CAN_2G_MODULATION
  975. },
  976. .get_tune_settings = tda10071_get_tune_settings,
  977. .init = tda10071_init,
  978. .sleep = tda10071_sleep,
  979. .set_frontend = tda10071_set_frontend,
  980. .get_frontend = tda10071_get_frontend,
  981. .read_status = tda10071_read_status,
  982. .read_snr = tda10071_read_snr,
  983. .read_signal_strength = tda10071_read_signal_strength,
  984. .read_ber = tda10071_read_ber,
  985. .read_ucblocks = tda10071_read_ucblocks,
  986. .diseqc_send_master_cmd = tda10071_diseqc_send_master_cmd,
  987. .diseqc_recv_slave_reply = tda10071_diseqc_recv_slave_reply,
  988. .diseqc_send_burst = tda10071_diseqc_send_burst,
  989. .set_tone = tda10071_set_tone,
  990. .set_voltage = tda10071_set_voltage,
  991. };
  992. static struct dvb_frontend *tda10071_get_dvb_frontend(struct i2c_client *client)
  993. {
  994. struct tda10071_dev *dev = i2c_get_clientdata(client);
  995. dev_dbg(&client->dev, "\n");
  996. return &dev->fe;
  997. }
  998. static int tda10071_probe(struct i2c_client *client,
  999. const struct i2c_device_id *id)
  1000. {
  1001. struct tda10071_dev *dev;
  1002. struct tda10071_platform_data *pdata = client->dev.platform_data;
  1003. int ret;
  1004. unsigned int uitmp;
  1005. static const struct regmap_config regmap_config = {
  1006. .reg_bits = 8,
  1007. .val_bits = 8,
  1008. };
  1009. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1010. if (!dev) {
  1011. ret = -ENOMEM;
  1012. goto err;
  1013. }
  1014. dev->client = client;
  1015. mutex_init(&dev->cmd_execute_mutex);
  1016. dev->clk = pdata->clk;
  1017. dev->i2c_wr_max = pdata->i2c_wr_max;
  1018. dev->ts_mode = pdata->ts_mode;
  1019. dev->spec_inv = pdata->spec_inv;
  1020. dev->pll_multiplier = pdata->pll_multiplier;
  1021. dev->tuner_i2c_addr = pdata->tuner_i2c_addr;
  1022. dev->regmap = devm_regmap_init_i2c(client, &regmap_config);
  1023. if (IS_ERR(dev->regmap)) {
  1024. ret = PTR_ERR(dev->regmap);
  1025. goto err_kfree;
  1026. }
  1027. /* chip ID */
  1028. ret = regmap_read(dev->regmap, 0xff, &uitmp);
  1029. if (ret)
  1030. goto err_kfree;
  1031. if (uitmp != 0x0f) {
  1032. ret = -ENODEV;
  1033. goto err_kfree;
  1034. }
  1035. /* chip type */
  1036. ret = regmap_read(dev->regmap, 0xdd, &uitmp);
  1037. if (ret)
  1038. goto err_kfree;
  1039. if (uitmp != 0x00) {
  1040. ret = -ENODEV;
  1041. goto err_kfree;
  1042. }
  1043. /* chip version */
  1044. ret = regmap_read(dev->regmap, 0xfe, &uitmp);
  1045. if (ret)
  1046. goto err_kfree;
  1047. if (uitmp != 0x01) {
  1048. ret = -ENODEV;
  1049. goto err_kfree;
  1050. }
  1051. /* create dvb_frontend */
  1052. memcpy(&dev->fe.ops, &tda10071_ops, sizeof(struct dvb_frontend_ops));
  1053. dev->fe.demodulator_priv = dev;
  1054. i2c_set_clientdata(client, dev);
  1055. /* setup callbacks */
  1056. pdata->get_dvb_frontend = tda10071_get_dvb_frontend;
  1057. dev_info(&client->dev, "NXP TDA10071 successfully identified\n");
  1058. return 0;
  1059. err_kfree:
  1060. kfree(dev);
  1061. err:
  1062. dev_dbg(&client->dev, "failed=%d\n", ret);
  1063. return ret;
  1064. }
  1065. static int tda10071_remove(struct i2c_client *client)
  1066. {
  1067. struct tda10071_dev *dev = i2c_get_clientdata(client);
  1068. dev_dbg(&client->dev, "\n");
  1069. kfree(dev);
  1070. return 0;
  1071. }
  1072. static const struct i2c_device_id tda10071_id_table[] = {
  1073. {"tda10071_cx24118", 0},
  1074. {}
  1075. };
  1076. MODULE_DEVICE_TABLE(i2c, tda10071_id_table);
  1077. static struct i2c_driver tda10071_driver = {
  1078. .driver = {
  1079. .name = "tda10071",
  1080. .suppress_bind_attrs = true,
  1081. },
  1082. .probe = tda10071_probe,
  1083. .remove = tda10071_remove,
  1084. .id_table = tda10071_id_table,
  1085. };
  1086. module_i2c_driver(tda10071_driver);
  1087. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1088. MODULE_DESCRIPTION("NXP TDA10071 DVB-S/S2 demodulator driver");
  1089. MODULE_LICENSE("GPL");
  1090. MODULE_FIRMWARE(TDA10071_FIRMWARE);