si2157.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner 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 "si2157_priv.h"
  17. static const struct dvb_tuner_ops si2157_ops;
  18. /* execute firmware command */
  19. static int si2157_cmd_execute(struct i2c_client *client, struct si2157_cmd *cmd)
  20. {
  21. struct si2157_dev *dev = i2c_get_clientdata(client);
  22. int ret;
  23. unsigned long timeout;
  24. mutex_lock(&dev->i2c_mutex);
  25. if (cmd->wlen) {
  26. /* write cmd and args for firmware */
  27. ret = i2c_master_send(client, cmd->args, cmd->wlen);
  28. if (ret < 0) {
  29. goto err_mutex_unlock;
  30. } else if (ret != cmd->wlen) {
  31. ret = -EREMOTEIO;
  32. goto err_mutex_unlock;
  33. }
  34. }
  35. if (cmd->rlen) {
  36. /* wait cmd execution terminate */
  37. #define TIMEOUT 80
  38. timeout = jiffies + msecs_to_jiffies(TIMEOUT);
  39. while (!time_after(jiffies, timeout)) {
  40. ret = i2c_master_recv(client, cmd->args, cmd->rlen);
  41. if (ret < 0) {
  42. goto err_mutex_unlock;
  43. } else if (ret != cmd->rlen) {
  44. ret = -EREMOTEIO;
  45. goto err_mutex_unlock;
  46. }
  47. /* firmware ready? */
  48. if ((cmd->args[0] >> 7) & 0x01)
  49. break;
  50. }
  51. dev_dbg(&client->dev, "cmd execution took %d ms\n",
  52. jiffies_to_msecs(jiffies) -
  53. (jiffies_to_msecs(timeout) - TIMEOUT));
  54. if (!((cmd->args[0] >> 7) & 0x01)) {
  55. ret = -ETIMEDOUT;
  56. goto err_mutex_unlock;
  57. }
  58. }
  59. mutex_unlock(&dev->i2c_mutex);
  60. return 0;
  61. err_mutex_unlock:
  62. mutex_unlock(&dev->i2c_mutex);
  63. dev_dbg(&client->dev, "failed=%d\n", ret);
  64. return ret;
  65. }
  66. static int si2157_init(struct dvb_frontend *fe)
  67. {
  68. struct i2c_client *client = fe->tuner_priv;
  69. struct si2157_dev *dev = i2c_get_clientdata(client);
  70. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  71. int ret, len, remaining;
  72. struct si2157_cmd cmd;
  73. const struct firmware *fw;
  74. const char *fw_name;
  75. unsigned int uitmp, chip_id;
  76. dev_dbg(&client->dev, "\n");
  77. /* Returned IF frequency is garbage when firmware is not running */
  78. memcpy(cmd.args, "\x15\x00\x06\x07", 4);
  79. cmd.wlen = 4;
  80. cmd.rlen = 4;
  81. ret = si2157_cmd_execute(client, &cmd);
  82. if (ret)
  83. goto err;
  84. uitmp = cmd.args[2] << 0 | cmd.args[3] << 8;
  85. dev_dbg(&client->dev, "if_frequency kHz=%u\n", uitmp);
  86. if (uitmp == dev->if_frequency / 1000)
  87. goto warm;
  88. /* power up */
  89. if (dev->chiptype == SI2157_CHIPTYPE_SI2146) {
  90. memcpy(cmd.args, "\xc0\x05\x01\x00\x00\x0b\x00\x00\x01", 9);
  91. cmd.wlen = 9;
  92. } else if (dev->chiptype == SI2157_CHIPTYPE_SI2141) {
  93. memcpy(cmd.args, "\xc0\x00\x0d\x0e\x00\x01\x01\x01\x01\x03", 10);
  94. cmd.wlen = 10;
  95. } else {
  96. memcpy(cmd.args, "\xc0\x00\x0c\x00\x00\x01\x01\x01\x01\x01\x01\x02\x00\x00\x01", 15);
  97. cmd.wlen = 15;
  98. }
  99. cmd.rlen = 1;
  100. ret = si2157_cmd_execute(client, &cmd);
  101. if (ret)
  102. goto err;
  103. /* Si2141 needs a second command before it answers the revision query */
  104. if (dev->chiptype == SI2157_CHIPTYPE_SI2141) {
  105. memcpy(cmd.args, "\xc0\x08\x01\x02\x00\x00\x01", 7);
  106. cmd.wlen = 7;
  107. ret = si2157_cmd_execute(client, &cmd);
  108. if (ret)
  109. goto err;
  110. }
  111. /* query chip revision */
  112. memcpy(cmd.args, "\x02", 1);
  113. cmd.wlen = 1;
  114. cmd.rlen = 13;
  115. ret = si2157_cmd_execute(client, &cmd);
  116. if (ret)
  117. goto err;
  118. chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
  119. cmd.args[4] << 0;
  120. #define SI2158_A20 ('A' << 24 | 58 << 16 | '2' << 8 | '0' << 0)
  121. #define SI2148_A20 ('A' << 24 | 48 << 16 | '2' << 8 | '0' << 0)
  122. #define SI2157_A30 ('A' << 24 | 57 << 16 | '3' << 8 | '0' << 0)
  123. #define SI2147_A30 ('A' << 24 | 47 << 16 | '3' << 8 | '0' << 0)
  124. #define SI2146_A10 ('A' << 24 | 46 << 16 | '1' << 8 | '0' << 0)
  125. #define SI2141_A10 ('A' << 24 | 41 << 16 | '1' << 8 | '0' << 0)
  126. switch (chip_id) {
  127. case SI2158_A20:
  128. case SI2148_A20:
  129. fw_name = SI2158_A20_FIRMWARE;
  130. break;
  131. case SI2141_A10:
  132. fw_name = SI2141_A10_FIRMWARE;
  133. break;
  134. case SI2157_A30:
  135. case SI2147_A30:
  136. case SI2146_A10:
  137. fw_name = NULL;
  138. break;
  139. default:
  140. dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
  141. cmd.args[2], cmd.args[1],
  142. cmd.args[3], cmd.args[4]);
  143. ret = -EINVAL;
  144. goto err;
  145. }
  146. dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n",
  147. cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
  148. if (fw_name == NULL)
  149. goto skip_fw_download;
  150. /* request the firmware, this will block and timeout */
  151. ret = request_firmware(&fw, fw_name, &client->dev);
  152. if (ret) {
  153. dev_err(&client->dev, "firmware file '%s' not found\n",
  154. fw_name);
  155. goto err;
  156. }
  157. /* firmware should be n chunks of 17 bytes */
  158. if (fw->size % 17 != 0) {
  159. dev_err(&client->dev, "firmware file '%s' is invalid\n",
  160. fw_name);
  161. ret = -EINVAL;
  162. goto err_release_firmware;
  163. }
  164. dev_info(&client->dev, "downloading firmware from file '%s'\n",
  165. fw_name);
  166. for (remaining = fw->size; remaining > 0; remaining -= 17) {
  167. len = fw->data[fw->size - remaining];
  168. if (len > SI2157_ARGLEN) {
  169. dev_err(&client->dev, "Bad firmware length\n");
  170. ret = -EINVAL;
  171. goto err_release_firmware;
  172. }
  173. memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
  174. cmd.wlen = len;
  175. cmd.rlen = 1;
  176. ret = si2157_cmd_execute(client, &cmd);
  177. if (ret) {
  178. dev_err(&client->dev, "firmware download failed %d\n",
  179. ret);
  180. goto err_release_firmware;
  181. }
  182. }
  183. release_firmware(fw);
  184. skip_fw_download:
  185. /* reboot the tuner with new firmware? */
  186. memcpy(cmd.args, "\x01\x01", 2);
  187. cmd.wlen = 2;
  188. cmd.rlen = 1;
  189. ret = si2157_cmd_execute(client, &cmd);
  190. if (ret)
  191. goto err;
  192. /* query firmware version */
  193. memcpy(cmd.args, "\x11", 1);
  194. cmd.wlen = 1;
  195. cmd.rlen = 10;
  196. ret = si2157_cmd_execute(client, &cmd);
  197. if (ret)
  198. goto err;
  199. dev_info(&client->dev, "firmware version: %c.%c.%d\n",
  200. cmd.args[6], cmd.args[7], cmd.args[8]);
  201. warm:
  202. /* init statistics in order signal app which are supported */
  203. c->strength.len = 1;
  204. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  205. /* start statistics polling */
  206. schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(1000));
  207. dev->active = true;
  208. return 0;
  209. err_release_firmware:
  210. release_firmware(fw);
  211. err:
  212. dev_dbg(&client->dev, "failed=%d\n", ret);
  213. return ret;
  214. }
  215. static int si2157_sleep(struct dvb_frontend *fe)
  216. {
  217. struct i2c_client *client = fe->tuner_priv;
  218. struct si2157_dev *dev = i2c_get_clientdata(client);
  219. int ret;
  220. struct si2157_cmd cmd;
  221. dev_dbg(&client->dev, "\n");
  222. dev->active = false;
  223. /* stop statistics polling */
  224. cancel_delayed_work_sync(&dev->stat_work);
  225. /* standby */
  226. memcpy(cmd.args, "\x16\x00", 2);
  227. cmd.wlen = 2;
  228. cmd.rlen = 1;
  229. ret = si2157_cmd_execute(client, &cmd);
  230. if (ret)
  231. goto err;
  232. return 0;
  233. err:
  234. dev_dbg(&client->dev, "failed=%d\n", ret);
  235. return ret;
  236. }
  237. static int si2157_set_params(struct dvb_frontend *fe)
  238. {
  239. struct i2c_client *client = fe->tuner_priv;
  240. struct si2157_dev *dev = i2c_get_clientdata(client);
  241. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  242. int ret;
  243. struct si2157_cmd cmd;
  244. u8 bandwidth, delivery_system;
  245. u32 if_frequency = 5000000;
  246. dev_dbg(&client->dev,
  247. "delivery_system=%d frequency=%u bandwidth_hz=%u\n",
  248. c->delivery_system, c->frequency, c->bandwidth_hz);
  249. if (!dev->active) {
  250. ret = -EAGAIN;
  251. goto err;
  252. }
  253. if (c->bandwidth_hz <= 6000000)
  254. bandwidth = 0x06;
  255. else if (c->bandwidth_hz <= 7000000)
  256. bandwidth = 0x07;
  257. else if (c->bandwidth_hz <= 8000000)
  258. bandwidth = 0x08;
  259. else
  260. bandwidth = 0x0f;
  261. switch (c->delivery_system) {
  262. case SYS_ATSC:
  263. delivery_system = 0x00;
  264. if_frequency = 3250000;
  265. break;
  266. case SYS_DVBC_ANNEX_B:
  267. delivery_system = 0x10;
  268. if_frequency = 4000000;
  269. break;
  270. case SYS_DVBT:
  271. case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */
  272. delivery_system = 0x20;
  273. break;
  274. case SYS_DVBC_ANNEX_A:
  275. delivery_system = 0x30;
  276. break;
  277. default:
  278. ret = -EINVAL;
  279. goto err;
  280. }
  281. memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6);
  282. cmd.args[4] = delivery_system | bandwidth;
  283. if (dev->inversion)
  284. cmd.args[5] = 0x01;
  285. cmd.wlen = 6;
  286. cmd.rlen = 4;
  287. ret = si2157_cmd_execute(client, &cmd);
  288. if (ret)
  289. goto err;
  290. if (dev->chiptype == SI2157_CHIPTYPE_SI2146)
  291. memcpy(cmd.args, "\x14\x00\x02\x07\x00\x01", 6);
  292. else
  293. memcpy(cmd.args, "\x14\x00\x02\x07\x00\x00", 6);
  294. cmd.args[4] = dev->if_port;
  295. cmd.wlen = 6;
  296. cmd.rlen = 4;
  297. ret = si2157_cmd_execute(client, &cmd);
  298. if (ret)
  299. goto err;
  300. /* set if frequency if needed */
  301. if (if_frequency != dev->if_frequency) {
  302. memcpy(cmd.args, "\x14\x00\x06\x07", 4);
  303. cmd.args[4] = (if_frequency / 1000) & 0xff;
  304. cmd.args[5] = ((if_frequency / 1000) >> 8) & 0xff;
  305. cmd.wlen = 6;
  306. cmd.rlen = 4;
  307. ret = si2157_cmd_execute(client, &cmd);
  308. if (ret)
  309. goto err;
  310. dev->if_frequency = if_frequency;
  311. }
  312. /* set frequency */
  313. memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8);
  314. cmd.args[4] = (c->frequency >> 0) & 0xff;
  315. cmd.args[5] = (c->frequency >> 8) & 0xff;
  316. cmd.args[6] = (c->frequency >> 16) & 0xff;
  317. cmd.args[7] = (c->frequency >> 24) & 0xff;
  318. cmd.wlen = 8;
  319. cmd.rlen = 1;
  320. ret = si2157_cmd_execute(client, &cmd);
  321. if (ret)
  322. goto err;
  323. return 0;
  324. err:
  325. dev_dbg(&client->dev, "failed=%d\n", ret);
  326. return ret;
  327. }
  328. static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  329. {
  330. struct i2c_client *client = fe->tuner_priv;
  331. struct si2157_dev *dev = i2c_get_clientdata(client);
  332. *frequency = dev->if_frequency;
  333. return 0;
  334. }
  335. static const struct dvb_tuner_ops si2157_ops = {
  336. .info = {
  337. .name = "Silicon Labs Si2141/Si2146/2147/2148/2157/2158",
  338. .frequency_min_hz = 42 * MHz,
  339. .frequency_max_hz = 870 * MHz,
  340. },
  341. .init = si2157_init,
  342. .sleep = si2157_sleep,
  343. .set_params = si2157_set_params,
  344. .get_if_frequency = si2157_get_if_frequency,
  345. };
  346. static void si2157_stat_work(struct work_struct *work)
  347. {
  348. struct si2157_dev *dev = container_of(work, struct si2157_dev, stat_work.work);
  349. struct dvb_frontend *fe = dev->fe;
  350. struct i2c_client *client = fe->tuner_priv;
  351. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  352. struct si2157_cmd cmd;
  353. int ret;
  354. dev_dbg(&client->dev, "\n");
  355. memcpy(cmd.args, "\x42\x00", 2);
  356. cmd.wlen = 2;
  357. cmd.rlen = 12;
  358. ret = si2157_cmd_execute(client, &cmd);
  359. if (ret)
  360. goto err;
  361. c->strength.stat[0].scale = FE_SCALE_DECIBEL;
  362. c->strength.stat[0].svalue = (s8) cmd.args[3] * 1000;
  363. schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
  364. return;
  365. err:
  366. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  367. dev_dbg(&client->dev, "failed=%d\n", ret);
  368. }
  369. static int si2157_probe(struct i2c_client *client,
  370. const struct i2c_device_id *id)
  371. {
  372. struct si2157_config *cfg = client->dev.platform_data;
  373. struct dvb_frontend *fe = cfg->fe;
  374. struct si2157_dev *dev;
  375. struct si2157_cmd cmd;
  376. int ret;
  377. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  378. if (!dev) {
  379. ret = -ENOMEM;
  380. dev_err(&client->dev, "kzalloc() failed\n");
  381. goto err;
  382. }
  383. i2c_set_clientdata(client, dev);
  384. dev->fe = cfg->fe;
  385. dev->inversion = cfg->inversion;
  386. dev->if_port = cfg->if_port;
  387. dev->chiptype = (u8)id->driver_data;
  388. dev->if_frequency = 5000000; /* default value of property 0x0706 */
  389. mutex_init(&dev->i2c_mutex);
  390. INIT_DELAYED_WORK(&dev->stat_work, si2157_stat_work);
  391. /* check if the tuner is there */
  392. cmd.wlen = 0;
  393. cmd.rlen = 1;
  394. ret = si2157_cmd_execute(client, &cmd);
  395. if (ret)
  396. goto err_kfree;
  397. memcpy(&fe->ops.tuner_ops, &si2157_ops, sizeof(struct dvb_tuner_ops));
  398. fe->tuner_priv = client;
  399. #ifdef CONFIG_MEDIA_CONTROLLER
  400. if (cfg->mdev) {
  401. dev->mdev = cfg->mdev;
  402. dev->ent.name = KBUILD_MODNAME;
  403. dev->ent.function = MEDIA_ENT_F_TUNER;
  404. dev->pad[TUNER_PAD_RF_INPUT].flags = MEDIA_PAD_FL_SINK;
  405. dev->pad[TUNER_PAD_OUTPUT].flags = MEDIA_PAD_FL_SOURCE;
  406. dev->pad[TUNER_PAD_AUD_OUT].flags = MEDIA_PAD_FL_SOURCE;
  407. ret = media_entity_pads_init(&dev->ent, TUNER_NUM_PADS,
  408. &dev->pad[0]);
  409. if (ret)
  410. goto err_kfree;
  411. ret = media_device_register_entity(cfg->mdev, &dev->ent);
  412. if (ret) {
  413. media_entity_cleanup(&dev->ent);
  414. goto err_kfree;
  415. }
  416. }
  417. #endif
  418. dev_info(&client->dev, "Silicon Labs %s successfully attached\n",
  419. dev->chiptype == SI2157_CHIPTYPE_SI2141 ? "Si2141" :
  420. dev->chiptype == SI2157_CHIPTYPE_SI2146 ?
  421. "Si2146" : "Si2147/2148/2157/2158");
  422. return 0;
  423. err_kfree:
  424. kfree(dev);
  425. err:
  426. dev_dbg(&client->dev, "failed=%d\n", ret);
  427. return ret;
  428. }
  429. static int si2157_remove(struct i2c_client *client)
  430. {
  431. struct si2157_dev *dev = i2c_get_clientdata(client);
  432. struct dvb_frontend *fe = dev->fe;
  433. dev_dbg(&client->dev, "\n");
  434. /* stop statistics polling */
  435. cancel_delayed_work_sync(&dev->stat_work);
  436. #ifdef CONFIG_MEDIA_CONTROLLER_DVB
  437. if (dev->mdev)
  438. media_device_unregister_entity(&dev->ent);
  439. #endif
  440. memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
  441. fe->tuner_priv = NULL;
  442. kfree(dev);
  443. return 0;
  444. }
  445. static const struct i2c_device_id si2157_id_table[] = {
  446. {"si2157", SI2157_CHIPTYPE_SI2157},
  447. {"si2146", SI2157_CHIPTYPE_SI2146},
  448. {"si2141", SI2157_CHIPTYPE_SI2141},
  449. {}
  450. };
  451. MODULE_DEVICE_TABLE(i2c, si2157_id_table);
  452. static struct i2c_driver si2157_driver = {
  453. .driver = {
  454. .name = "si2157",
  455. .suppress_bind_attrs = true,
  456. },
  457. .probe = si2157_probe,
  458. .remove = si2157_remove,
  459. .id_table = si2157_id_table,
  460. };
  461. module_i2c_driver(si2157_driver);
  462. MODULE_DESCRIPTION("Silicon Labs Si2141/Si2146/2147/2148/2157/2158 silicon tuner driver");
  463. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  464. MODULE_LICENSE("GPL");
  465. MODULE_FIRMWARE(SI2158_A20_FIRMWARE);
  466. MODULE_FIRMWARE(SI2141_A10_FIRMWARE);