si2168.c 17 KB

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