si2168.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Silicon Labs Si2168 DVB-T/T2/C demodulator driver
  4. *
  5. * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
  6. */
  7. #include <linux/delay.h>
  8. #include "si2168_priv.h"
  9. static const struct dvb_frontend_ops si2168_ops;
  10. static void cmd_init(struct si2168_cmd *cmd, const u8 *buf, int wlen, int rlen)
  11. {
  12. memcpy(cmd->args, buf, wlen);
  13. cmd->wlen = wlen;
  14. cmd->rlen = rlen;
  15. }
  16. /* execute firmware command */
  17. static int si2168_cmd_execute(struct i2c_client *client, struct si2168_cmd *cmd)
  18. {
  19. struct si2168_dev *dev = i2c_get_clientdata(client);
  20. int ret;
  21. unsigned long timeout;
  22. mutex_lock(&dev->i2c_mutex);
  23. if (cmd->wlen) {
  24. /* write cmd and args for firmware */
  25. ret = i2c_master_send(client, cmd->args, cmd->wlen);
  26. if (ret < 0) {
  27. goto err_mutex_unlock;
  28. } else if (ret != cmd->wlen) {
  29. ret = -EREMOTEIO;
  30. goto err_mutex_unlock;
  31. }
  32. }
  33. if (cmd->rlen) {
  34. /* wait cmd execution terminate */
  35. #define TIMEOUT 70
  36. timeout = jiffies + msecs_to_jiffies(TIMEOUT);
  37. while (!time_after(jiffies, timeout)) {
  38. ret = i2c_master_recv(client, cmd->args, cmd->rlen);
  39. if (ret < 0) {
  40. goto err_mutex_unlock;
  41. } else if (ret != cmd->rlen) {
  42. ret = -EREMOTEIO;
  43. goto err_mutex_unlock;
  44. }
  45. /* firmware ready? */
  46. if ((cmd->args[0] >> 7) & 0x01)
  47. break;
  48. }
  49. dev_dbg(&client->dev, "cmd execution took %d ms\n",
  50. jiffies_to_msecs(jiffies) -
  51. (jiffies_to_msecs(timeout) - TIMEOUT));
  52. /* error bit set? */
  53. if ((cmd->args[0] >> 6) & 0x01) {
  54. ret = -EREMOTEIO;
  55. goto err_mutex_unlock;
  56. }
  57. if (!((cmd->args[0] >> 7) & 0x01)) {
  58. ret = -ETIMEDOUT;
  59. goto err_mutex_unlock;
  60. }
  61. }
  62. mutex_unlock(&dev->i2c_mutex);
  63. return 0;
  64. err_mutex_unlock:
  65. mutex_unlock(&dev->i2c_mutex);
  66. dev_dbg(&client->dev, "failed=%d\n", ret);
  67. return ret;
  68. }
  69. static int si2168_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
  70. {
  71. struct i2c_client *client = fe->demodulator_priv;
  72. struct si2168_dev *dev = i2c_get_clientdata(client);
  73. struct si2168_cmd cmd;
  74. int ret = 0;
  75. dev_dbg(&client->dev, "%s acquire: %d\n", __func__, acquire);
  76. /* set manual value */
  77. if (dev->ts_mode & SI2168_TS_CLK_MANUAL) {
  78. cmd_init(&cmd, "\x14\x00\x0d\x10\xe8\x03", 6, 4);
  79. ret = si2168_cmd_execute(client, &cmd);
  80. if (ret)
  81. return ret;
  82. }
  83. /* set TS_MODE property */
  84. cmd_init(&cmd, "\x14\x00\x01\x10\x10\x00", 6, 4);
  85. if (dev->ts_mode & SI2168_TS_CLK_MANUAL)
  86. cmd.args[4] = SI2168_TS_CLK_MANUAL;
  87. if (acquire)
  88. cmd.args[4] |= dev->ts_mode;
  89. else
  90. cmd.args[4] |= SI2168_TS_TRISTATE;
  91. if (dev->ts_clock_gapped)
  92. cmd.args[4] |= 0x40;
  93. ret = si2168_cmd_execute(client, &cmd);
  94. return ret;
  95. }
  96. static int si2168_read_status(struct dvb_frontend *fe, enum fe_status *status)
  97. {
  98. struct i2c_client *client = fe->demodulator_priv;
  99. struct si2168_dev *dev = i2c_get_clientdata(client);
  100. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  101. int ret, i;
  102. unsigned int utmp, utmp1, utmp2;
  103. struct si2168_cmd cmd;
  104. *status = 0;
  105. if (!dev->active) {
  106. ret = -EAGAIN;
  107. goto err;
  108. }
  109. switch (c->delivery_system) {
  110. case SYS_DVBT:
  111. cmd_init(&cmd, "\xa0\x01", 2, 13);
  112. break;
  113. case SYS_DVBC_ANNEX_A:
  114. cmd_init(&cmd, "\x90\x01", 2, 9);
  115. break;
  116. case SYS_DVBT2:
  117. cmd_init(&cmd, "\x50\x01", 2, 14);
  118. break;
  119. default:
  120. ret = -EINVAL;
  121. goto err;
  122. }
  123. ret = si2168_cmd_execute(client, &cmd);
  124. if (ret)
  125. goto err;
  126. switch ((cmd.args[2] >> 1) & 0x03) {
  127. case 0x01:
  128. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
  129. break;
  130. case 0x03:
  131. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
  132. FE_HAS_SYNC | FE_HAS_LOCK;
  133. break;
  134. }
  135. dev->fe_status = *status;
  136. if (*status & FE_HAS_LOCK) {
  137. c->cnr.len = 1;
  138. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  139. c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4;
  140. } else {
  141. c->cnr.len = 1;
  142. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  143. }
  144. dev_dbg(&client->dev, "status=%02x args=%*ph\n",
  145. *status, cmd.rlen, cmd.args);
  146. /* BER */
  147. if (*status & FE_HAS_VITERBI) {
  148. cmd_init(&cmd, "\x82\x00", 2, 3);
  149. ret = si2168_cmd_execute(client, &cmd);
  150. if (ret)
  151. goto err;
  152. /*
  153. * Firmware returns [0, 255] mantissa and [0, 8] exponent.
  154. * Convert to DVB API: mantissa * 10^(8 - exponent) / 10^8
  155. */
  156. utmp = clamp(8 - cmd.args[1], 0, 8);
  157. for (i = 0, utmp1 = 1; i < utmp; i++)
  158. utmp1 = utmp1 * 10;
  159. utmp1 = cmd.args[2] * utmp1;
  160. utmp2 = 100000000; /* 10^8 */
  161. dev_dbg(&client->dev,
  162. "post_bit_error=%u post_bit_count=%u ber=%u*10^-%u\n",
  163. utmp1, utmp2, cmd.args[2], cmd.args[1]);
  164. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  165. c->post_bit_error.stat[0].uvalue += utmp1;
  166. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  167. c->post_bit_count.stat[0].uvalue += utmp2;
  168. } else {
  169. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  170. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  171. }
  172. /* UCB */
  173. if (*status & FE_HAS_SYNC) {
  174. cmd_init(&cmd, "\x84\x01", 2, 3);
  175. ret = si2168_cmd_execute(client, &cmd);
  176. if (ret)
  177. goto err;
  178. utmp1 = cmd.args[2] << 8 | cmd.args[1] << 0;
  179. dev_dbg(&client->dev, "block_error=%u\n", utmp1);
  180. /* Sometimes firmware returns bogus value */
  181. if (utmp1 == 0xffff)
  182. utmp1 = 0;
  183. c->block_error.stat[0].scale = FE_SCALE_COUNTER;
  184. c->block_error.stat[0].uvalue += utmp1;
  185. } else {
  186. c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  187. }
  188. return 0;
  189. err:
  190. dev_dbg(&client->dev, "failed=%d\n", ret);
  191. return ret;
  192. }
  193. static int si2168_set_frontend(struct dvb_frontend *fe)
  194. {
  195. struct i2c_client *client = fe->demodulator_priv;
  196. struct si2168_dev *dev = i2c_get_clientdata(client);
  197. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  198. int ret;
  199. struct si2168_cmd cmd;
  200. u8 bandwidth, delivery_system;
  201. dev_dbg(&client->dev,
  202. "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n",
  203. c->delivery_system, c->modulation, c->frequency,
  204. c->bandwidth_hz, c->symbol_rate, c->inversion,
  205. c->stream_id);
  206. if (!dev->active) {
  207. ret = -EAGAIN;
  208. goto err;
  209. }
  210. switch (c->delivery_system) {
  211. case SYS_DVBT:
  212. delivery_system = 0x20;
  213. break;
  214. case SYS_DVBC_ANNEX_A:
  215. delivery_system = 0x30;
  216. break;
  217. case SYS_DVBT2:
  218. delivery_system = 0x70;
  219. break;
  220. default:
  221. ret = -EINVAL;
  222. goto err;
  223. }
  224. if (c->bandwidth_hz == 0) {
  225. ret = -EINVAL;
  226. goto err;
  227. } else if (c->bandwidth_hz <= 2000000)
  228. bandwidth = 0x02;
  229. else if (c->bandwidth_hz <= 5000000)
  230. bandwidth = 0x05;
  231. else if (c->bandwidth_hz <= 6000000)
  232. bandwidth = 0x06;
  233. else if (c->bandwidth_hz <= 7000000)
  234. bandwidth = 0x07;
  235. else if (c->bandwidth_hz <= 8000000)
  236. bandwidth = 0x08;
  237. else if (c->bandwidth_hz <= 9000000)
  238. bandwidth = 0x09;
  239. else if (c->bandwidth_hz <= 10000000)
  240. bandwidth = 0x0a;
  241. else
  242. bandwidth = 0x0f;
  243. /* program tuner */
  244. if (fe->ops.tuner_ops.set_params) {
  245. ret = fe->ops.tuner_ops.set_params(fe);
  246. if (ret)
  247. goto err;
  248. }
  249. cmd_init(&cmd, "\x88\x02\x02\x02\x02", 5, 5);
  250. ret = si2168_cmd_execute(client, &cmd);
  251. if (ret)
  252. goto err;
  253. /* that has no big effect */
  254. if (c->delivery_system == SYS_DVBT)
  255. cmd_init(&cmd, "\x89\x21\x06\x11\xff\x98", 6, 3);
  256. else if (c->delivery_system == SYS_DVBC_ANNEX_A)
  257. cmd_init(&cmd, "\x89\x21\x06\x11\x89\xf0", 6, 3);
  258. else if (c->delivery_system == SYS_DVBT2)
  259. cmd_init(&cmd, "\x89\x21\x06\x11\x89\x20", 6, 3);
  260. ret = si2168_cmd_execute(client, &cmd);
  261. if (ret)
  262. goto err;
  263. if (c->delivery_system == SYS_DVBT2) {
  264. /* select PLP */
  265. cmd.args[0] = 0x52;
  266. cmd.args[1] = c->stream_id & 0xff;
  267. cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1;
  268. cmd.wlen = 3;
  269. cmd.rlen = 1;
  270. ret = si2168_cmd_execute(client, &cmd);
  271. if (ret)
  272. goto err;
  273. }
  274. cmd_init(&cmd, "\x51\x03", 2, 12);
  275. ret = si2168_cmd_execute(client, &cmd);
  276. if (ret)
  277. goto err;
  278. cmd_init(&cmd, "\x12\x08\x04", 3, 3);
  279. ret = si2168_cmd_execute(client, &cmd);
  280. if (ret)
  281. goto err;
  282. cmd_init(&cmd, "\x14\x00\x0c\x10\x12\x00", 6, 4);
  283. ret = si2168_cmd_execute(client, &cmd);
  284. if (ret)
  285. goto err;
  286. cmd_init(&cmd, "\x14\x00\x06\x10\x24\x00", 6, 4);
  287. ret = si2168_cmd_execute(client, &cmd);
  288. if (ret)
  289. goto err;
  290. cmd_init(&cmd, "\x14\x00\x07\x10\x00\x24", 6, 4);
  291. ret = si2168_cmd_execute(client, &cmd);
  292. if (ret)
  293. goto err;
  294. cmd_init(&cmd, "\x14\x00\x0a\x10\x00\x00", 6, 4);
  295. cmd.args[4] = delivery_system | bandwidth;
  296. if (dev->spectral_inversion)
  297. cmd.args[5] |= 1;
  298. ret = si2168_cmd_execute(client, &cmd);
  299. if (ret)
  300. goto err;
  301. /* set DVB-C symbol rate */
  302. if (c->delivery_system == SYS_DVBC_ANNEX_A) {
  303. cmd_init(&cmd, "\x14\x00\x02\x11\x00\x00", 6, 4);
  304. cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff;
  305. cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
  306. ret = si2168_cmd_execute(client, &cmd);
  307. if (ret)
  308. goto err;
  309. }
  310. cmd_init(&cmd, "\x14\x00\x0f\x10\x10\x00", 6, 4);
  311. ret = si2168_cmd_execute(client, &cmd);
  312. if (ret)
  313. goto err;
  314. cmd_init(&cmd, "\x14\x00\x09\x10\xe3\x08", 6, 4);
  315. cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
  316. ret = si2168_cmd_execute(client, &cmd);
  317. if (ret)
  318. goto err;
  319. cmd_init(&cmd, "\x14\x00\x08\x10\xd7\x05", 6, 4);
  320. cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
  321. ret = si2168_cmd_execute(client, &cmd);
  322. if (ret)
  323. goto err;
  324. cmd_init(&cmd, "\x14\x00\x01\x12\x00\x00", 6, 4);
  325. ret = si2168_cmd_execute(client, &cmd);
  326. if (ret)
  327. goto err;
  328. cmd_init(&cmd, "\x14\x00\x01\x03\x0c\x00", 6, 4);
  329. ret = si2168_cmd_execute(client, &cmd);
  330. if (ret)
  331. goto err;
  332. cmd_init(&cmd, "\x85", 1, 1);
  333. ret = si2168_cmd_execute(client, &cmd);
  334. if (ret)
  335. goto err;
  336. dev->delivery_system = c->delivery_system;
  337. /* enable ts bus */
  338. ret = si2168_ts_bus_ctrl(fe, 1);
  339. if (ret)
  340. goto err;
  341. return 0;
  342. err:
  343. dev_dbg(&client->dev, "failed=%d\n", ret);
  344. return ret;
  345. }
  346. static int si2168_init(struct dvb_frontend *fe)
  347. {
  348. struct i2c_client *client = fe->demodulator_priv;
  349. struct si2168_dev *dev = i2c_get_clientdata(client);
  350. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  351. int ret, len, remaining;
  352. const struct firmware *fw;
  353. struct si2168_cmd cmd;
  354. dev_dbg(&client->dev, "\n");
  355. /* initialize */
  356. cmd_init(&cmd, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00",
  357. 13, 0);
  358. ret = si2168_cmd_execute(client, &cmd);
  359. if (ret)
  360. goto err;
  361. if (dev->warm) {
  362. /* resume */
  363. cmd_init(&cmd, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8, 1);
  364. ret = si2168_cmd_execute(client, &cmd);
  365. if (ret)
  366. goto err;
  367. udelay(100);
  368. cmd_init(&cmd, "\x85", 1, 1);
  369. ret = si2168_cmd_execute(client, &cmd);
  370. if (ret)
  371. goto err;
  372. goto warm;
  373. }
  374. /* power up */
  375. cmd_init(&cmd, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8, 1);
  376. ret = si2168_cmd_execute(client, &cmd);
  377. if (ret)
  378. goto err;
  379. /* request the firmware, this will block and timeout */
  380. ret = request_firmware(&fw, dev->firmware_name, &client->dev);
  381. if (ret) {
  382. /* fallback mechanism to handle old name for Si2168 B40 fw */
  383. if (dev->chip_id == SI2168_CHIP_ID_B40) {
  384. dev->firmware_name = SI2168_B40_FIRMWARE_FALLBACK;
  385. ret = request_firmware(&fw, dev->firmware_name,
  386. &client->dev);
  387. }
  388. if (ret == 0) {
  389. dev_notice(&client->dev,
  390. "please install firmware file '%s'\n",
  391. SI2168_B40_FIRMWARE);
  392. } else {
  393. dev_err(&client->dev,
  394. "firmware file '%s' not found\n",
  395. dev->firmware_name);
  396. goto err_release_firmware;
  397. }
  398. }
  399. dev_info(&client->dev, "downloading firmware from file '%s'\n",
  400. dev->firmware_name);
  401. if ((fw->size % 17 == 0) && (fw->data[0] > 5)) {
  402. /* firmware is in the new format */
  403. for (remaining = fw->size; remaining > 0; remaining -= 17) {
  404. len = fw->data[fw->size - remaining];
  405. if (len > SI2168_ARGLEN) {
  406. ret = -EINVAL;
  407. break;
  408. }
  409. cmd_init(&cmd, &fw->data[(fw->size - remaining) + 1],
  410. len, 1);
  411. ret = si2168_cmd_execute(client, &cmd);
  412. if (ret)
  413. break;
  414. }
  415. } else if (fw->size % 8 == 0) {
  416. /* firmware is in the old format */
  417. for (remaining = fw->size; remaining > 0; remaining -= 8) {
  418. cmd_init(&cmd, &fw->data[fw->size - remaining], 8, 1);
  419. ret = si2168_cmd_execute(client, &cmd);
  420. if (ret)
  421. break;
  422. }
  423. } else {
  424. /* bad or unknown firmware format */
  425. ret = -EINVAL;
  426. }
  427. if (ret) {
  428. dev_err(&client->dev, "firmware download failed %d\n", ret);
  429. goto err_release_firmware;
  430. }
  431. release_firmware(fw);
  432. cmd_init(&cmd, "\x01\x01", 2, 1);
  433. ret = si2168_cmd_execute(client, &cmd);
  434. if (ret)
  435. goto err;
  436. /* query firmware version */
  437. cmd_init(&cmd, "\x11", 1, 10);
  438. ret = si2168_cmd_execute(client, &cmd);
  439. if (ret)
  440. goto err;
  441. dev->version = (cmd.args[9] + '@') << 24 | (cmd.args[6] - '0') << 16 |
  442. (cmd.args[7] - '0') << 8 | (cmd.args[8]) << 0;
  443. dev_info(&client->dev, "firmware version: %c %d.%d.%d\n",
  444. dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
  445. dev->version >> 8 & 0xff, dev->version >> 0 & 0xff);
  446. /* set ts mode */
  447. ret = si2168_ts_bus_ctrl(fe, 1);
  448. if (ret)
  449. goto err;
  450. dev->warm = true;
  451. warm:
  452. /* Init stats here to indicate which stats are supported */
  453. c->cnr.len = 1;
  454. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  455. c->post_bit_error.len = 1;
  456. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  457. c->post_bit_count.len = 1;
  458. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  459. c->block_error.len = 1;
  460. c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  461. dev->active = true;
  462. return 0;
  463. err_release_firmware:
  464. release_firmware(fw);
  465. err:
  466. dev_dbg(&client->dev, "failed=%d\n", ret);
  467. return ret;
  468. }
  469. static int si2168_sleep(struct dvb_frontend *fe)
  470. {
  471. struct i2c_client *client = fe->demodulator_priv;
  472. struct si2168_dev *dev = i2c_get_clientdata(client);
  473. int ret;
  474. struct si2168_cmd cmd;
  475. dev_dbg(&client->dev, "\n");
  476. dev->active = false;
  477. /* tri-state data bus */
  478. ret = si2168_ts_bus_ctrl(fe, 0);
  479. if (ret)
  480. goto err;
  481. /* Firmware later than B 4.0-11 loses warm state during sleep */
  482. if (dev->version > ('B' << 24 | 4 << 16 | 0 << 8 | 11 << 0))
  483. dev->warm = false;
  484. cmd_init(&cmd, "\x13", 1, 0);
  485. ret = si2168_cmd_execute(client, &cmd);
  486. if (ret)
  487. goto err;
  488. return 0;
  489. err:
  490. dev_dbg(&client->dev, "failed=%d\n", ret);
  491. return ret;
  492. }
  493. static int si2168_get_tune_settings(struct dvb_frontend *fe,
  494. struct dvb_frontend_tune_settings *s)
  495. {
  496. s->min_delay_ms = 900;
  497. return 0;
  498. }
  499. static int si2168_select(struct i2c_mux_core *muxc, u32 chan)
  500. {
  501. struct i2c_client *client = i2c_mux_priv(muxc);
  502. int ret;
  503. struct si2168_cmd cmd;
  504. /* open I2C gate */
  505. cmd_init(&cmd, "\xc0\x0d\x01", 3, 0);
  506. ret = si2168_cmd_execute(client, &cmd);
  507. if (ret)
  508. goto err;
  509. return 0;
  510. err:
  511. dev_dbg(&client->dev, "failed=%d\n", ret);
  512. return ret;
  513. }
  514. static int si2168_deselect(struct i2c_mux_core *muxc, u32 chan)
  515. {
  516. struct i2c_client *client = i2c_mux_priv(muxc);
  517. int ret;
  518. struct si2168_cmd cmd;
  519. /* close I2C gate */
  520. cmd_init(&cmd, "\xc0\x0d\x00", 3, 0);
  521. ret = si2168_cmd_execute(client, &cmd);
  522. if (ret)
  523. goto err;
  524. return 0;
  525. err:
  526. dev_dbg(&client->dev, "failed=%d\n", ret);
  527. return ret;
  528. }
  529. static const struct dvb_frontend_ops si2168_ops = {
  530. .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
  531. .info = {
  532. .name = "Silicon Labs Si2168",
  533. .frequency_min_hz = 48 * MHz,
  534. .frequency_max_hz = 870 * MHz,
  535. .frequency_stepsize_hz = 62500,
  536. .symbol_rate_min = 1000000,
  537. .symbol_rate_max = 7200000,
  538. .caps = FE_CAN_FEC_1_2 |
  539. FE_CAN_FEC_2_3 |
  540. FE_CAN_FEC_3_4 |
  541. FE_CAN_FEC_5_6 |
  542. FE_CAN_FEC_7_8 |
  543. FE_CAN_FEC_AUTO |
  544. FE_CAN_QPSK |
  545. FE_CAN_QAM_16 |
  546. FE_CAN_QAM_32 |
  547. FE_CAN_QAM_64 |
  548. FE_CAN_QAM_128 |
  549. FE_CAN_QAM_256 |
  550. FE_CAN_QAM_AUTO |
  551. FE_CAN_TRANSMISSION_MODE_AUTO |
  552. FE_CAN_GUARD_INTERVAL_AUTO |
  553. FE_CAN_HIERARCHY_AUTO |
  554. FE_CAN_MUTE_TS |
  555. FE_CAN_2G_MODULATION |
  556. FE_CAN_MULTISTREAM
  557. },
  558. .get_tune_settings = si2168_get_tune_settings,
  559. .init = si2168_init,
  560. .sleep = si2168_sleep,
  561. .set_frontend = si2168_set_frontend,
  562. .read_status = si2168_read_status,
  563. };
  564. static int si2168_probe(struct i2c_client *client,
  565. const struct i2c_device_id *id)
  566. {
  567. struct si2168_config *config = client->dev.platform_data;
  568. struct si2168_dev *dev;
  569. int ret;
  570. struct si2168_cmd cmd;
  571. dev_dbg(&client->dev, "\n");
  572. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  573. if (!dev) {
  574. ret = -ENOMEM;
  575. goto err;
  576. }
  577. i2c_set_clientdata(client, dev);
  578. mutex_init(&dev->i2c_mutex);
  579. /* Initialize */
  580. cmd_init(&cmd, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00",
  581. 13, 0);
  582. ret = si2168_cmd_execute(client, &cmd);
  583. if (ret)
  584. goto err_kfree;
  585. /* Power up */
  586. cmd_init(&cmd, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8, 1);
  587. ret = si2168_cmd_execute(client, &cmd);
  588. if (ret)
  589. goto err_kfree;
  590. /* Query chip revision */
  591. cmd_init(&cmd, "\x02", 1, 13);
  592. ret = si2168_cmd_execute(client, &cmd);
  593. if (ret)
  594. goto err_kfree;
  595. dev->chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 |
  596. cmd.args[3] << 8 | cmd.args[4] << 0;
  597. switch (dev->chip_id) {
  598. case SI2168_CHIP_ID_A20:
  599. dev->firmware_name = SI2168_A20_FIRMWARE;
  600. break;
  601. case SI2168_CHIP_ID_A30:
  602. dev->firmware_name = SI2168_A30_FIRMWARE;
  603. break;
  604. case SI2168_CHIP_ID_B40:
  605. dev->firmware_name = SI2168_B40_FIRMWARE;
  606. break;
  607. case SI2168_CHIP_ID_D60:
  608. dev->firmware_name = SI2168_D60_FIRMWARE;
  609. break;
  610. default:
  611. dev_dbg(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
  612. cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
  613. ret = -ENODEV;
  614. goto err_kfree;
  615. }
  616. dev->version = (cmd.args[1]) << 24 | (cmd.args[3] - '0') << 16 |
  617. (cmd.args[4] - '0') << 8 | (cmd.args[5]) << 0;
  618. /* create mux i2c adapter for tuner */
  619. dev->muxc = i2c_mux_alloc(client->adapter, &client->dev,
  620. 1, 0, I2C_MUX_LOCKED,
  621. si2168_select, si2168_deselect);
  622. if (!dev->muxc) {
  623. ret = -ENOMEM;
  624. goto err_kfree;
  625. }
  626. dev->muxc->priv = client;
  627. ret = i2c_mux_add_adapter(dev->muxc, 0, 0, 0);
  628. if (ret)
  629. goto err_kfree;
  630. /* create dvb_frontend */
  631. memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
  632. dev->fe.demodulator_priv = client;
  633. *config->i2c_adapter = dev->muxc->adapter[0];
  634. *config->fe = &dev->fe;
  635. dev->ts_mode = config->ts_mode;
  636. dev->ts_clock_inv = config->ts_clock_inv;
  637. dev->ts_clock_gapped = config->ts_clock_gapped;
  638. dev->spectral_inversion = config->spectral_inversion;
  639. dev_info(&client->dev, "Silicon Labs Si2168-%c%d%d successfully identified\n",
  640. dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
  641. dev->version >> 8 & 0xff);
  642. dev_info(&client->dev, "firmware version: %c %d.%d.%d\n",
  643. dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
  644. dev->version >> 8 & 0xff, dev->version >> 0 & 0xff);
  645. return 0;
  646. err_kfree:
  647. kfree(dev);
  648. err:
  649. dev_warn(&client->dev, "probe failed = %d\n", ret);
  650. return ret;
  651. }
  652. static int si2168_remove(struct i2c_client *client)
  653. {
  654. struct si2168_dev *dev = i2c_get_clientdata(client);
  655. dev_dbg(&client->dev, "\n");
  656. i2c_mux_del_adapters(dev->muxc);
  657. dev->fe.ops.release = NULL;
  658. dev->fe.demodulator_priv = NULL;
  659. kfree(dev);
  660. return 0;
  661. }
  662. static const struct i2c_device_id si2168_id_table[] = {
  663. {"si2168", 0},
  664. {}
  665. };
  666. MODULE_DEVICE_TABLE(i2c, si2168_id_table);
  667. static struct i2c_driver si2168_driver = {
  668. .driver = {
  669. .name = "si2168",
  670. .suppress_bind_attrs = true,
  671. },
  672. .probe = si2168_probe,
  673. .remove = si2168_remove,
  674. .id_table = si2168_id_table,
  675. };
  676. module_i2c_driver(si2168_driver);
  677. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  678. MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
  679. MODULE_LICENSE("GPL");
  680. MODULE_FIRMWARE(SI2168_A20_FIRMWARE);
  681. MODULE_FIRMWARE(SI2168_A30_FIRMWARE);
  682. MODULE_FIRMWARE(SI2168_B40_FIRMWARE);
  683. MODULE_FIRMWARE(SI2168_D60_FIRMWARE);