si2157.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 si2157 *s, struct si2157_cmd *cmd)
  20. {
  21. int ret;
  22. unsigned long timeout;
  23. mutex_lock(&s->i2c_mutex);
  24. if (cmd->wlen) {
  25. /* write cmd and args for firmware */
  26. ret = i2c_master_send(s->client, cmd->args, cmd->wlen);
  27. if (ret < 0) {
  28. goto err_mutex_unlock;
  29. } else if (ret != cmd->wlen) {
  30. ret = -EREMOTEIO;
  31. goto err_mutex_unlock;
  32. }
  33. }
  34. if (cmd->rlen) {
  35. /* wait cmd execution terminate */
  36. #define TIMEOUT 80
  37. timeout = jiffies + msecs_to_jiffies(TIMEOUT);
  38. while (!time_after(jiffies, timeout)) {
  39. ret = i2c_master_recv(s->client, cmd->args, cmd->rlen);
  40. if (ret < 0) {
  41. goto err_mutex_unlock;
  42. } else if (ret != cmd->rlen) {
  43. ret = -EREMOTEIO;
  44. goto err_mutex_unlock;
  45. }
  46. /* firmware ready? */
  47. if ((cmd->args[0] >> 7) & 0x01)
  48. break;
  49. }
  50. dev_dbg(&s->client->dev, "cmd execution took %d ms\n",
  51. jiffies_to_msecs(jiffies) -
  52. (jiffies_to_msecs(timeout) - TIMEOUT));
  53. if (!((cmd->args[0] >> 7) & 0x01)) {
  54. ret = -ETIMEDOUT;
  55. goto err_mutex_unlock;
  56. }
  57. }
  58. ret = 0;
  59. err_mutex_unlock:
  60. mutex_unlock(&s->i2c_mutex);
  61. if (ret)
  62. goto err;
  63. return 0;
  64. err:
  65. dev_dbg(&s->client->dev, "failed=%d\n", ret);
  66. return ret;
  67. }
  68. static int si2157_init(struct dvb_frontend *fe)
  69. {
  70. struct si2157 *s = fe->tuner_priv;
  71. int ret, len, remaining;
  72. struct si2157_cmd cmd;
  73. const struct firmware *fw = NULL;
  74. u8 *fw_file;
  75. unsigned int chip_id;
  76. dev_dbg(&s->client->dev, "\n");
  77. if (s->fw_loaded)
  78. goto warm;
  79. /* power up */
  80. if (s->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(s, &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(s, &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_file = SI2158_A20_FIRMWARE;
  109. break;
  110. case SI2157_A30:
  111. case SI2147_A30:
  112. case SI2146_A10:
  113. goto skip_fw_download;
  114. default:
  115. dev_err(&s->client->dev,
  116. "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. /* cold state - try to download firmware */
  123. dev_info(&s->client->dev, "found a '%s' in cold state\n",
  124. si2157_ops.info.name);
  125. /* request the firmware, this will block and timeout */
  126. ret = request_firmware(&fw, fw_file, &s->client->dev);
  127. if (ret) {
  128. dev_err(&s->client->dev, "firmware file '%s' not found\n",
  129. fw_file);
  130. goto err;
  131. }
  132. /* firmware should be n chunks of 17 bytes */
  133. if (fw->size % 17 != 0) {
  134. dev_err(&s->client->dev, "firmware file '%s' is invalid\n",
  135. fw_file);
  136. ret = -EINVAL;
  137. goto fw_release_exit;
  138. }
  139. dev_info(&s->client->dev, "downloading firmware from file '%s'\n",
  140. fw_file);
  141. for (remaining = fw->size; remaining > 0; remaining -= 17) {
  142. len = fw->data[fw->size - remaining];
  143. memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
  144. cmd.wlen = len;
  145. cmd.rlen = 1;
  146. ret = si2157_cmd_execute(s, &cmd);
  147. if (ret) {
  148. dev_err(&s->client->dev,
  149. "firmware download failed=%d\n",
  150. ret);
  151. goto fw_release_exit;
  152. }
  153. }
  154. release_firmware(fw);
  155. fw = NULL;
  156. skip_fw_download:
  157. /* reboot the tuner with new firmware? */
  158. memcpy(cmd.args, "\x01\x01", 2);
  159. cmd.wlen = 2;
  160. cmd.rlen = 1;
  161. ret = si2157_cmd_execute(s, &cmd);
  162. if (ret)
  163. goto err;
  164. s->fw_loaded = true;
  165. warm:
  166. s->active = true;
  167. return 0;
  168. fw_release_exit:
  169. release_firmware(fw);
  170. err:
  171. dev_dbg(&s->client->dev, "failed=%d\n", ret);
  172. return ret;
  173. }
  174. static int si2157_sleep(struct dvb_frontend *fe)
  175. {
  176. struct si2157 *s = fe->tuner_priv;
  177. int ret;
  178. struct si2157_cmd cmd;
  179. dev_dbg(&s->client->dev, "\n");
  180. s->active = false;
  181. /* standby */
  182. memcpy(cmd.args, "\x16\x00", 2);
  183. cmd.wlen = 2;
  184. cmd.rlen = 1;
  185. ret = si2157_cmd_execute(s, &cmd);
  186. if (ret)
  187. goto err;
  188. return 0;
  189. err:
  190. dev_dbg(&s->client->dev, "failed=%d\n", ret);
  191. return ret;
  192. }
  193. static int si2157_set_params(struct dvb_frontend *fe)
  194. {
  195. struct si2157 *s = fe->tuner_priv;
  196. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  197. int ret;
  198. struct si2157_cmd cmd;
  199. u8 bandwidth, delivery_system;
  200. dev_dbg(&s->client->dev,
  201. "delivery_system=%d frequency=%u bandwidth_hz=%u\n",
  202. c->delivery_system, c->frequency,
  203. c->bandwidth_hz);
  204. if (!s->active) {
  205. ret = -EAGAIN;
  206. goto err;
  207. }
  208. if (c->bandwidth_hz <= 6000000)
  209. bandwidth = 0x06;
  210. else if (c->bandwidth_hz <= 7000000)
  211. bandwidth = 0x07;
  212. else if (c->bandwidth_hz <= 8000000)
  213. bandwidth = 0x08;
  214. else
  215. bandwidth = 0x0f;
  216. switch (c->delivery_system) {
  217. case SYS_ATSC:
  218. delivery_system = 0x00;
  219. break;
  220. case SYS_DVBC_ANNEX_B:
  221. delivery_system = 0x10;
  222. break;
  223. case SYS_DVBT:
  224. case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */
  225. delivery_system = 0x20;
  226. break;
  227. case SYS_DVBC_ANNEX_A:
  228. delivery_system = 0x30;
  229. break;
  230. default:
  231. ret = -EINVAL;
  232. goto err;
  233. }
  234. memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6);
  235. cmd.args[4] = delivery_system | bandwidth;
  236. if (s->inversion)
  237. cmd.args[5] = 0x01;
  238. cmd.wlen = 6;
  239. cmd.rlen = 4;
  240. ret = si2157_cmd_execute(s, &cmd);
  241. if (ret)
  242. goto err;
  243. if (s->chiptype == SI2157_CHIPTYPE_SI2146)
  244. memcpy(cmd.args, "\x14\x00\x02\x07\x00\x01", 6);
  245. else
  246. memcpy(cmd.args, "\x14\x00\x02\x07\x01\x00", 6);
  247. cmd.wlen = 6;
  248. cmd.rlen = 4;
  249. ret = si2157_cmd_execute(s, &cmd);
  250. if (ret)
  251. goto err;
  252. /* set frequency */
  253. memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8);
  254. cmd.args[4] = (c->frequency >> 0) & 0xff;
  255. cmd.args[5] = (c->frequency >> 8) & 0xff;
  256. cmd.args[6] = (c->frequency >> 16) & 0xff;
  257. cmd.args[7] = (c->frequency >> 24) & 0xff;
  258. cmd.wlen = 8;
  259. cmd.rlen = 1;
  260. ret = si2157_cmd_execute(s, &cmd);
  261. if (ret)
  262. goto err;
  263. return 0;
  264. err:
  265. dev_dbg(&s->client->dev, "failed=%d\n", ret);
  266. return ret;
  267. }
  268. static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  269. {
  270. *frequency = 5000000; /* default value of property 0x0706 */
  271. return 0;
  272. }
  273. static const struct dvb_tuner_ops si2157_ops = {
  274. .info = {
  275. .name = "Silicon Labs Si2146/2147/2148/2157/2158",
  276. .frequency_min = 110000000,
  277. .frequency_max = 862000000,
  278. },
  279. .init = si2157_init,
  280. .sleep = si2157_sleep,
  281. .set_params = si2157_set_params,
  282. .get_if_frequency = si2157_get_if_frequency,
  283. };
  284. static int si2157_probe(struct i2c_client *client,
  285. const struct i2c_device_id *id)
  286. {
  287. struct si2157_config *cfg = client->dev.platform_data;
  288. struct dvb_frontend *fe = cfg->fe;
  289. struct si2157 *s;
  290. struct si2157_cmd cmd;
  291. int ret;
  292. s = kzalloc(sizeof(struct si2157), GFP_KERNEL);
  293. if (!s) {
  294. ret = -ENOMEM;
  295. dev_err(&client->dev, "kzalloc() failed\n");
  296. goto err;
  297. }
  298. s->client = client;
  299. s->fe = cfg->fe;
  300. s->inversion = cfg->inversion;
  301. s->fw_loaded = false;
  302. s->chiptype = (u8)id->driver_data;
  303. mutex_init(&s->i2c_mutex);
  304. /* check if the tuner is there */
  305. cmd.wlen = 0;
  306. cmd.rlen = 1;
  307. ret = si2157_cmd_execute(s, &cmd);
  308. if (ret)
  309. goto err;
  310. fe->tuner_priv = s;
  311. memcpy(&fe->ops.tuner_ops, &si2157_ops,
  312. sizeof(struct dvb_tuner_ops));
  313. i2c_set_clientdata(client, s);
  314. dev_info(&s->client->dev,
  315. "Silicon Labs %s successfully attached\n",
  316. s->chiptype == SI2157_CHIPTYPE_SI2146 ?
  317. "Si2146" : "Si2147/2148/2157/2158");
  318. return 0;
  319. err:
  320. dev_dbg(&client->dev, "failed=%d\n", ret);
  321. kfree(s);
  322. return ret;
  323. }
  324. static int si2157_remove(struct i2c_client *client)
  325. {
  326. struct si2157 *s = i2c_get_clientdata(client);
  327. struct dvb_frontend *fe = s->fe;
  328. dev_dbg(&client->dev, "\n");
  329. memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
  330. fe->tuner_priv = NULL;
  331. kfree(s);
  332. return 0;
  333. }
  334. static const struct i2c_device_id si2157_id[] = {
  335. {"si2157", 0},
  336. {"si2146", 1},
  337. {}
  338. };
  339. MODULE_DEVICE_TABLE(i2c, si2157_id);
  340. static struct i2c_driver si2157_driver = {
  341. .driver = {
  342. .owner = THIS_MODULE,
  343. .name = "si2157",
  344. },
  345. .probe = si2157_probe,
  346. .remove = si2157_remove,
  347. .id_table = si2157_id,
  348. };
  349. module_i2c_driver(si2157_driver);
  350. MODULE_DESCRIPTION("Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner driver");
  351. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  352. MODULE_LICENSE("GPL");
  353. MODULE_FIRMWARE(SI2158_A20_FIRMWARE);