si2157.c 12 KB

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