si2157.c 13 KB

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