si2157.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Silicon Labs Si2147/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. memcpy(cmd.args, "\xc0\x00\x0c\x00\x00\x01\x01\x01\x01\x01\x01\x02\x00\x00\x01", 15);
  81. cmd.wlen = 15;
  82. cmd.rlen = 1;
  83. ret = si2157_cmd_execute(s, &cmd);
  84. if (ret)
  85. goto err;
  86. /* query chip revision */
  87. memcpy(cmd.args, "\x02", 1);
  88. cmd.wlen = 1;
  89. cmd.rlen = 13;
  90. ret = si2157_cmd_execute(s, &cmd);
  91. if (ret)
  92. goto err;
  93. chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
  94. cmd.args[4] << 0;
  95. #define SI2158_A20 ('A' << 24 | 58 << 16 | '2' << 8 | '0' << 0)
  96. #define SI2157_A30 ('A' << 24 | 57 << 16 | '3' << 8 | '0' << 0)
  97. #define SI2147_A30 ('A' << 24 | 47 << 16 | '3' << 8 | '0' << 0)
  98. switch (chip_id) {
  99. case SI2158_A20:
  100. fw_file = SI2158_A20_FIRMWARE;
  101. break;
  102. case SI2157_A30:
  103. case SI2147_A30:
  104. goto skip_fw_download;
  105. break;
  106. default:
  107. dev_err(&s->client->dev,
  108. "unknown chip version Si21%d-%c%c%c\n",
  109. cmd.args[2], cmd.args[1],
  110. cmd.args[3], cmd.args[4]);
  111. ret = -EINVAL;
  112. goto err;
  113. }
  114. /* cold state - try to download firmware */
  115. dev_info(&s->client->dev, "found a '%s' in cold state\n",
  116. si2157_ops.info.name);
  117. /* request the firmware, this will block and timeout */
  118. ret = request_firmware(&fw, fw_file, &s->client->dev);
  119. if (ret) {
  120. dev_err(&s->client->dev, "firmware file '%s' not found\n",
  121. fw_file);
  122. goto err;
  123. }
  124. /* firmware should be n chunks of 17 bytes */
  125. if (fw->size % 17 != 0) {
  126. dev_err(&s->client->dev, "firmware file '%s' is invalid\n",
  127. fw_file);
  128. ret = -EINVAL;
  129. goto err;
  130. }
  131. dev_info(&s->client->dev, "downloading firmware from file '%s'\n",
  132. fw_file);
  133. for (remaining = fw->size; remaining > 0; remaining -= 17) {
  134. len = fw->data[fw->size - remaining];
  135. memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
  136. cmd.wlen = len;
  137. cmd.rlen = 1;
  138. ret = si2157_cmd_execute(s, &cmd);
  139. if (ret) {
  140. dev_err(&s->client->dev,
  141. "firmware download failed=%d\n",
  142. ret);
  143. goto err;
  144. }
  145. }
  146. release_firmware(fw);
  147. fw = NULL;
  148. skip_fw_download:
  149. /* reboot the tuner with new firmware? */
  150. memcpy(cmd.args, "\x01\x01", 2);
  151. cmd.wlen = 2;
  152. cmd.rlen = 1;
  153. ret = si2157_cmd_execute(s, &cmd);
  154. if (ret)
  155. goto err;
  156. s->fw_loaded = true;
  157. warm:
  158. s->active = true;
  159. return 0;
  160. err:
  161. if (fw)
  162. release_firmware(fw);
  163. dev_dbg(&s->client->dev, "failed=%d\n", ret);
  164. return ret;
  165. }
  166. static int si2157_sleep(struct dvb_frontend *fe)
  167. {
  168. struct si2157 *s = fe->tuner_priv;
  169. int ret;
  170. struct si2157_cmd cmd;
  171. dev_dbg(&s->client->dev, "\n");
  172. s->active = false;
  173. /* standby */
  174. memcpy(cmd.args, "\x16\x00", 2);
  175. cmd.wlen = 2;
  176. cmd.rlen = 1;
  177. ret = si2157_cmd_execute(s, &cmd);
  178. if (ret)
  179. goto err;
  180. return 0;
  181. err:
  182. dev_dbg(&s->client->dev, "failed=%d\n", ret);
  183. return ret;
  184. }
  185. static int si2157_set_params(struct dvb_frontend *fe)
  186. {
  187. struct si2157 *s = fe->tuner_priv;
  188. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  189. int ret;
  190. struct si2157_cmd cmd;
  191. u8 bandwidth, delivery_system;
  192. dev_dbg(&s->client->dev,
  193. "delivery_system=%d frequency=%u bandwidth_hz=%u\n",
  194. c->delivery_system, c->frequency,
  195. c->bandwidth_hz);
  196. if (!s->active) {
  197. ret = -EAGAIN;
  198. goto err;
  199. }
  200. if (c->bandwidth_hz <= 6000000)
  201. bandwidth = 0x06;
  202. else if (c->bandwidth_hz <= 7000000)
  203. bandwidth = 0x07;
  204. else if (c->bandwidth_hz <= 8000000)
  205. bandwidth = 0x08;
  206. else
  207. bandwidth = 0x0f;
  208. switch (c->delivery_system) {
  209. case SYS_ATSC:
  210. delivery_system = 0x00;
  211. break;
  212. case SYS_DVBT:
  213. case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */
  214. delivery_system = 0x20;
  215. break;
  216. case SYS_DVBC_ANNEX_A:
  217. delivery_system = 0x30;
  218. break;
  219. default:
  220. ret = -EINVAL;
  221. goto err;
  222. }
  223. memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6);
  224. cmd.args[4] = delivery_system | bandwidth;
  225. if (s->inversion)
  226. cmd.args[5] = 0x01;
  227. cmd.wlen = 6;
  228. cmd.rlen = 4;
  229. ret = si2157_cmd_execute(s, &cmd);
  230. if (ret)
  231. goto err;
  232. memcpy(cmd.args, "\x14\x00\x02\x07\x01\x00", 6);
  233. cmd.wlen = 6;
  234. cmd.rlen = 4;
  235. ret = si2157_cmd_execute(s, &cmd);
  236. if (ret)
  237. goto err;
  238. /* set frequency */
  239. memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8);
  240. cmd.args[4] = (c->frequency >> 0) & 0xff;
  241. cmd.args[5] = (c->frequency >> 8) & 0xff;
  242. cmd.args[6] = (c->frequency >> 16) & 0xff;
  243. cmd.args[7] = (c->frequency >> 24) & 0xff;
  244. cmd.wlen = 8;
  245. cmd.rlen = 1;
  246. ret = si2157_cmd_execute(s, &cmd);
  247. if (ret)
  248. goto err;
  249. return 0;
  250. err:
  251. dev_dbg(&s->client->dev, "failed=%d\n", ret);
  252. return ret;
  253. }
  254. static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  255. {
  256. *frequency = 5000000; /* default value of property 0x0706 */
  257. return 0;
  258. }
  259. static const struct dvb_tuner_ops si2157_ops = {
  260. .info = {
  261. .name = "Silicon Labs Si2157/Si2158",
  262. .frequency_min = 110000000,
  263. .frequency_max = 862000000,
  264. },
  265. .init = si2157_init,
  266. .sleep = si2157_sleep,
  267. .set_params = si2157_set_params,
  268. .get_if_frequency = si2157_get_if_frequency,
  269. };
  270. static int si2157_probe(struct i2c_client *client,
  271. const struct i2c_device_id *id)
  272. {
  273. struct si2157_config *cfg = client->dev.platform_data;
  274. struct dvb_frontend *fe = cfg->fe;
  275. struct si2157 *s;
  276. struct si2157_cmd cmd;
  277. int ret;
  278. s = kzalloc(sizeof(struct si2157), GFP_KERNEL);
  279. if (!s) {
  280. ret = -ENOMEM;
  281. dev_err(&client->dev, "kzalloc() failed\n");
  282. goto err;
  283. }
  284. s->client = client;
  285. s->fe = cfg->fe;
  286. s->inversion = cfg->inversion;
  287. s->fw_loaded = false;
  288. mutex_init(&s->i2c_mutex);
  289. /* check if the tuner is there */
  290. cmd.wlen = 0;
  291. cmd.rlen = 1;
  292. ret = si2157_cmd_execute(s, &cmd);
  293. if (ret)
  294. goto err;
  295. fe->tuner_priv = s;
  296. memcpy(&fe->ops.tuner_ops, &si2157_ops,
  297. sizeof(struct dvb_tuner_ops));
  298. i2c_set_clientdata(client, s);
  299. dev_info(&s->client->dev,
  300. "Silicon Labs Si2157/Si2158 successfully attached\n");
  301. return 0;
  302. err:
  303. dev_dbg(&client->dev, "failed=%d\n", ret);
  304. kfree(s);
  305. return ret;
  306. }
  307. static int si2157_remove(struct i2c_client *client)
  308. {
  309. struct si2157 *s = i2c_get_clientdata(client);
  310. struct dvb_frontend *fe = s->fe;
  311. dev_dbg(&client->dev, "\n");
  312. memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
  313. fe->tuner_priv = NULL;
  314. kfree(s);
  315. return 0;
  316. }
  317. static const struct i2c_device_id si2157_id[] = {
  318. {"si2157", 0},
  319. {}
  320. };
  321. MODULE_DEVICE_TABLE(i2c, si2157_id);
  322. static struct i2c_driver si2157_driver = {
  323. .driver = {
  324. .owner = THIS_MODULE,
  325. .name = "si2157",
  326. },
  327. .probe = si2157_probe,
  328. .remove = si2157_remove,
  329. .id_table = si2157_id,
  330. };
  331. module_i2c_driver(si2157_driver);
  332. MODULE_DESCRIPTION("Silicon Labs Si2157/Si2158 silicon tuner driver");
  333. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  334. MODULE_LICENSE("GPL");
  335. MODULE_FIRMWARE(SI2158_A20_FIRMWARE);