tuner-xc2028.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. /* tuner-xc2028
  2. *
  3. * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
  4. *
  5. * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
  6. * - frontend interface
  7. *
  8. * This code is placed under the terms of the GNU General Public License v2
  9. */
  10. #include <linux/i2c.h>
  11. #include <asm/div64.h>
  12. #include <linux/firmware.h>
  13. #include <linux/videodev2.h>
  14. #include <linux/delay.h>
  15. #include <media/tuner.h>
  16. #include <linux/mutex.h>
  17. #include "tuner-i2c.h"
  18. #include "tuner-xc2028.h"
  19. #include "tuner-xc2028-types.h"
  20. #include <linux/dvb/frontend.h>
  21. #include "dvb_frontend.h"
  22. #define PREFIX "xc2028"
  23. static int debug;
  24. module_param(debug, int, 0644);
  25. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  26. static char audio_std[8];
  27. module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
  28. MODULE_PARM_DESC(audio_std,
  29. "Audio standard. XC3028 audio decoder explicitly "
  30. "needs to know what audio\n"
  31. "standard is needed for some video standards with audio A2 or NICAM.\n"
  32. "The valid values are:\n"
  33. "A2\n"
  34. "A2/A\n"
  35. "A2/B\n"
  36. "NICAM\n"
  37. "NICAM/A\n"
  38. "NICAM/B\n");
  39. static LIST_HEAD(xc2028_list);
  40. static DEFINE_MUTEX(xc2028_list_mutex);
  41. /* struct for storing firmware table */
  42. struct firmware_description {
  43. unsigned int type;
  44. v4l2_std_id id;
  45. __u16 int_freq;
  46. unsigned char *ptr;
  47. unsigned int size;
  48. };
  49. struct firmware_properties {
  50. unsigned int type;
  51. v4l2_std_id id;
  52. v4l2_std_id std_req;
  53. __u16 int_freq;
  54. unsigned int scode_table;
  55. int scode_nr;
  56. };
  57. struct xc2028_data {
  58. struct list_head xc2028_list;
  59. struct tuner_i2c_props i2c_props;
  60. int (*tuner_callback) (void *dev,
  61. int command, int arg);
  62. void *video_dev;
  63. int count;
  64. __u32 frequency;
  65. struct firmware_description *firm;
  66. int firm_size;
  67. __u16 firm_version;
  68. __u16 hwmodel;
  69. __u16 hwvers;
  70. struct xc2028_ctrl ctrl;
  71. struct firmware_properties cur_fw;
  72. struct mutex lock;
  73. };
  74. #define i2c_send(priv, buf, size) ({ \
  75. int _rc; \
  76. _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
  77. if (size != _rc) \
  78. tuner_info("i2c output error: rc = %d (should be %d)\n",\
  79. _rc, (int)size); \
  80. _rc; \
  81. })
  82. #define i2c_rcv(priv, buf, size) ({ \
  83. int _rc; \
  84. _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
  85. if (size != _rc) \
  86. tuner_err("i2c input error: rc = %d (should be %d)\n", \
  87. _rc, (int)size); \
  88. _rc; \
  89. })
  90. #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
  91. int _rc; \
  92. _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
  93. ibuf, isize); \
  94. if (isize != _rc) \
  95. tuner_err("i2c input error: rc = %d (should be %d)\n", \
  96. _rc, (int)isize); \
  97. _rc; \
  98. })
  99. #define send_seq(priv, data...) ({ \
  100. static u8 _val[] = data; \
  101. int _rc; \
  102. if (sizeof(_val) != \
  103. (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
  104. _val, sizeof(_val)))) { \
  105. tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
  106. } else \
  107. msleep(10); \
  108. _rc; \
  109. })
  110. static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
  111. {
  112. unsigned char buf[2];
  113. unsigned char ibuf[2];
  114. tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
  115. buf[0] = reg >> 8;
  116. buf[1] = (unsigned char) reg;
  117. if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
  118. return -EIO;
  119. *val = (ibuf[1]) | (ibuf[0] << 8);
  120. return 0;
  121. }
  122. void dump_firm_type(unsigned int type)
  123. {
  124. if (type & BASE)
  125. printk("BASE ");
  126. if (type & INIT1)
  127. printk("INIT1 ");
  128. if (type & F8MHZ)
  129. printk("F8MHZ ");
  130. if (type & MTS)
  131. printk("MTS ");
  132. if (type & D2620)
  133. printk("D2620 ");
  134. if (type & D2633)
  135. printk("D2633 ");
  136. if (type & DTV6)
  137. printk("DTV6 ");
  138. if (type & QAM)
  139. printk("QAM ");
  140. if (type & DTV7)
  141. printk("DTV7 ");
  142. if (type & DTV78)
  143. printk("DTV78 ");
  144. if (type & DTV8)
  145. printk("DTV8 ");
  146. if (type & FM)
  147. printk("FM ");
  148. if (type & INPUT1)
  149. printk("INPUT1 ");
  150. if (type & LCD)
  151. printk("LCD ");
  152. if (type & NOGD)
  153. printk("NOGD ");
  154. if (type & MONO)
  155. printk("MONO ");
  156. if (type & ATSC)
  157. printk("ATSC ");
  158. if (type & IF)
  159. printk("IF ");
  160. if (type & LG60)
  161. printk("LG60 ");
  162. if (type & ATI638)
  163. printk("ATI638 ");
  164. if (type & OREN538)
  165. printk("OREN538 ");
  166. if (type & OREN36)
  167. printk("OREN36 ");
  168. if (type & TOYOTA388)
  169. printk("TOYOTA388 ");
  170. if (type & TOYOTA794)
  171. printk("TOYOTA794 ");
  172. if (type & DIBCOM52)
  173. printk("DIBCOM52 ");
  174. if (type & ZARLINK456)
  175. printk("ZARLINK456 ");
  176. if (type & CHINA)
  177. printk("CHINA ");
  178. if (type & F6MHZ)
  179. printk("F6MHZ ");
  180. if (type & INPUT2)
  181. printk("INPUT2 ");
  182. if (type & SCODE)
  183. printk("SCODE ");
  184. }
  185. static v4l2_std_id parse_audio_std_option(void)
  186. {
  187. if (strcasecmp(audio_std, "A2") == 0)
  188. return V4L2_STD_A2;
  189. if (strcasecmp(audio_std, "A2/A") == 0)
  190. return V4L2_STD_A2_A;
  191. if (strcasecmp(audio_std, "A2/B") == 0)
  192. return V4L2_STD_A2_B;
  193. if (strcasecmp(audio_std, "NICAM") == 0)
  194. return V4L2_STD_NICAM;
  195. if (strcasecmp(audio_std, "NICAM/A") == 0)
  196. return V4L2_STD_NICAM_A;
  197. if (strcasecmp(audio_std, "NICAM/B") == 0)
  198. return V4L2_STD_NICAM_B;
  199. return 0;
  200. }
  201. static void free_firmware(struct xc2028_data *priv)
  202. {
  203. int i;
  204. if (!priv->firm)
  205. return;
  206. for (i = 0; i < priv->firm_size; i++)
  207. kfree(priv->firm[i].ptr);
  208. kfree(priv->firm);
  209. priv->firm = NULL;
  210. priv->firm_size = 0;
  211. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  212. }
  213. static int load_all_firmwares(struct dvb_frontend *fe)
  214. {
  215. struct xc2028_data *priv = fe->tuner_priv;
  216. const struct firmware *fw = NULL;
  217. unsigned char *p, *endp;
  218. int rc = 0;
  219. int n, n_array;
  220. char name[33];
  221. tuner_dbg("%s called\n", __FUNCTION__);
  222. tuner_dbg("Reading firmware %s\n", priv->ctrl.fname);
  223. rc = request_firmware(&fw, priv->ctrl.fname,
  224. &priv->i2c_props.adap->dev);
  225. if (rc < 0) {
  226. if (rc == -ENOENT)
  227. tuner_err("Error: firmware %s not found.\n",
  228. priv->ctrl.fname);
  229. else
  230. tuner_err("Error %d while requesting firmware %s \n",
  231. rc, priv->ctrl.fname);
  232. return rc;
  233. }
  234. p = fw->data;
  235. endp = p + fw->size;
  236. if (fw->size < sizeof(name) - 1 + 2 + 2) {
  237. tuner_err("Error: firmware file %s has invalid size!\n",
  238. priv->ctrl.fname);
  239. goto corrupt;
  240. }
  241. memcpy(name, p, sizeof(name) - 1);
  242. name[sizeof(name) - 1] = 0;
  243. p += sizeof(name) - 1;
  244. priv->firm_version = le16_to_cpu(*(__u16 *) p);
  245. p += 2;
  246. n_array = le16_to_cpu(*(__u16 *) p);
  247. p += 2;
  248. tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
  249. n_array, priv->ctrl.fname, name,
  250. priv->firm_version >> 8, priv->firm_version & 0xff);
  251. priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
  252. if (priv->firm == NULL) {
  253. tuner_err("Not enough memory to load firmware file.\n");
  254. rc = -ENOMEM;
  255. goto err;
  256. }
  257. priv->firm_size = n_array;
  258. n = -1;
  259. while (p < endp) {
  260. __u32 type, size;
  261. v4l2_std_id id;
  262. __u16 int_freq = 0;
  263. n++;
  264. if (n >= n_array) {
  265. tuner_err("More firmware images in file than "
  266. "were expected!\n");
  267. goto corrupt;
  268. }
  269. /* Checks if there's enough bytes to read */
  270. if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
  271. tuner_err("Firmware header is incomplete!\n");
  272. goto corrupt;
  273. }
  274. type = le32_to_cpu(*(__u32 *) p);
  275. p += sizeof(type);
  276. id = le64_to_cpu(*(v4l2_std_id *) p);
  277. p += sizeof(id);
  278. if (type & HAS_IF) {
  279. int_freq = le16_to_cpu(*(__u16 *) p);
  280. p += sizeof(int_freq);
  281. }
  282. size = le32_to_cpu(*(__u32 *) p);
  283. p += sizeof(size);
  284. if ((!size) || (size + p > endp)) {
  285. tuner_err("Firmware type ");
  286. dump_firm_type(type);
  287. printk("(%x), id %llx is corrupted "
  288. "(size=%d, expected %d)\n",
  289. type, (unsigned long long)id,
  290. (unsigned)(endp - p), size);
  291. goto corrupt;
  292. }
  293. priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
  294. if (priv->firm[n].ptr == NULL) {
  295. tuner_err("Not enough memory to load firmware file.\n");
  296. rc = -ENOMEM;
  297. goto err;
  298. }
  299. tuner_dbg("Reading firmware type ");
  300. if (debug) {
  301. dump_firm_type(type);
  302. printk("(%x), id %llx, size=%d.\n",
  303. type, (unsigned long long)id, size);
  304. }
  305. memcpy(priv->firm[n].ptr, p, size);
  306. priv->firm[n].type = type;
  307. priv->firm[n].id = id;
  308. priv->firm[n].size = size;
  309. priv->firm[n].int_freq = int_freq;
  310. p += size;
  311. }
  312. if (n + 1 != priv->firm_size) {
  313. tuner_err("Firmware file is incomplete!\n");
  314. goto corrupt;
  315. }
  316. goto done;
  317. corrupt:
  318. rc = -EINVAL;
  319. tuner_err("Error: firmware file is corrupted!\n");
  320. err:
  321. tuner_info("Releasing partially loaded firmware file.\n");
  322. free_firmware(priv);
  323. done:
  324. release_firmware(fw);
  325. if (rc == 0)
  326. tuner_dbg("Firmware files loaded.\n");
  327. return rc;
  328. }
  329. static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
  330. v4l2_std_id *id)
  331. {
  332. struct xc2028_data *priv = fe->tuner_priv;
  333. int i, best_i = -1, best_nr_matches = 0;
  334. tuner_dbg("%s called, want type=", __FUNCTION__);
  335. if (debug) {
  336. dump_firm_type(type);
  337. printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
  338. }
  339. if (!priv->firm) {
  340. tuner_err("Error! firmware not loaded\n");
  341. return -EINVAL;
  342. }
  343. if (((type & ~SCODE) == 0) && (*id == 0))
  344. *id = V4L2_STD_PAL;
  345. if (type & BASE)
  346. type &= BASE_TYPES;
  347. else if (type & SCODE)
  348. type &= SCODE_TYPES;
  349. else if (type & DTV_TYPES)
  350. type &= DTV_TYPES;
  351. else if (type & STD_SPECIFIC_TYPES)
  352. type &= STD_SPECIFIC_TYPES;
  353. /* Seek for exact match */
  354. for (i = 0; i < priv->firm_size; i++) {
  355. if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
  356. goto found;
  357. }
  358. /* Seek for generic video standard match */
  359. for (i = 0; i < priv->firm_size; i++) {
  360. v4l2_std_id match_mask;
  361. int nr_matches;
  362. if (type != priv->firm[i].type)
  363. continue;
  364. match_mask = *id & priv->firm[i].id;
  365. if (!match_mask)
  366. continue;
  367. if ((*id & match_mask) == *id)
  368. goto found; /* Supports all the requested standards */
  369. nr_matches = hweight64(match_mask);
  370. if (nr_matches > best_nr_matches) {
  371. best_nr_matches = nr_matches;
  372. best_i = i;
  373. }
  374. }
  375. if (best_nr_matches > 0) {
  376. tuner_dbg("Selecting best matching firmware (%d bits) for "
  377. "type=", best_nr_matches);
  378. dump_firm_type(type);
  379. printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
  380. i = best_i;
  381. goto found;
  382. }
  383. /*FIXME: Would make sense to seek for type "hint" match ? */
  384. i = -ENOENT;
  385. goto ret;
  386. found:
  387. *id = priv->firm[i].id;
  388. ret:
  389. tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
  390. if (debug) {
  391. dump_firm_type(type);
  392. printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
  393. }
  394. return i;
  395. }
  396. static int load_firmware(struct dvb_frontend *fe, unsigned int type,
  397. v4l2_std_id *id)
  398. {
  399. struct xc2028_data *priv = fe->tuner_priv;
  400. int pos, rc;
  401. unsigned char *p, *endp, buf[priv->ctrl.max_len];
  402. tuner_dbg("%s called\n", __FUNCTION__);
  403. pos = seek_firmware(fe, type, id);
  404. if (pos < 0)
  405. return pos;
  406. tuner_info("Loading firmware for type=");
  407. dump_firm_type(priv->firm[pos].type);
  408. printk("(%x), id %016llx.\n", priv->firm[pos].type,
  409. (unsigned long long)*id);
  410. p = priv->firm[pos].ptr;
  411. endp = p + priv->firm[pos].size;
  412. while (p < endp) {
  413. __u16 size;
  414. /* Checks if there's enough bytes to read */
  415. if (p + sizeof(size) > endp) {
  416. tuner_err("Firmware chunk size is wrong\n");
  417. return -EINVAL;
  418. }
  419. size = le16_to_cpu(*(__u16 *) p);
  420. p += sizeof(size);
  421. if (size == 0xffff)
  422. return 0;
  423. if (!size) {
  424. /* Special callback command received */
  425. rc = priv->tuner_callback(priv->video_dev,
  426. XC2028_TUNER_RESET, 0);
  427. if (rc < 0) {
  428. tuner_err("Error at RESET code %d\n",
  429. (*p) & 0x7f);
  430. return -EINVAL;
  431. }
  432. continue;
  433. }
  434. if (size >= 0xff00) {
  435. switch (size) {
  436. case 0xff00:
  437. rc = priv->tuner_callback(priv->video_dev,
  438. XC2028_RESET_CLK, 0);
  439. if (rc < 0) {
  440. tuner_err("Error at RESET code %d\n",
  441. (*p) & 0x7f);
  442. return -EINVAL;
  443. }
  444. break;
  445. default:
  446. tuner_info("Invalid RESET code %d\n",
  447. size & 0x7f);
  448. return -EINVAL;
  449. }
  450. continue;
  451. }
  452. /* Checks for a sleep command */
  453. if (size & 0x8000) {
  454. msleep(size & 0x7fff);
  455. continue;
  456. }
  457. if ((size + p > endp)) {
  458. tuner_err("missing bytes: need %d, have %d\n",
  459. size, (int)(endp - p));
  460. return -EINVAL;
  461. }
  462. buf[0] = *p;
  463. p++;
  464. size--;
  465. /* Sends message chunks */
  466. while (size > 0) {
  467. int len = (size < priv->ctrl.max_len - 1) ?
  468. size : priv->ctrl.max_len - 1;
  469. memcpy(buf + 1, p, len);
  470. rc = i2c_send(priv, buf, len + 1);
  471. if (rc < 0) {
  472. tuner_err("%d returned from send\n", rc);
  473. return -EINVAL;
  474. }
  475. p += len;
  476. size -= len;
  477. }
  478. }
  479. return 0;
  480. }
  481. static int load_scode(struct dvb_frontend *fe, unsigned int type,
  482. v4l2_std_id *id, __u16 int_freq, int scode)
  483. {
  484. struct xc2028_data *priv = fe->tuner_priv;
  485. int pos, rc;
  486. unsigned char *p;
  487. tuner_dbg("%s called\n", __FUNCTION__);
  488. if (!int_freq) {
  489. pos = seek_firmware(fe, type, id);
  490. if (pos < 0)
  491. return pos;
  492. } else {
  493. for (pos = 0; pos < priv->firm_size; pos++) {
  494. if ((priv->firm[pos].int_freq == int_freq) &&
  495. (type & HAS_IF))
  496. break;
  497. }
  498. if (pos == priv->firm_size)
  499. return -ENOENT;
  500. }
  501. p = priv->firm[pos].ptr;
  502. if (type & HAS_IF) {
  503. if (priv->firm[pos].size != 12 * 16 || scode >= 16)
  504. return -EINVAL;
  505. p += 12 * scode;
  506. } else {
  507. /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
  508. * has a 2-byte size header in the firmware format. */
  509. if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
  510. le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
  511. return -EINVAL;
  512. p += 14 * scode + 2;
  513. }
  514. tuner_info("Loading SCODE for type=");
  515. dump_firm_type(priv->firm[pos].type);
  516. printk("(%x), id %016llx.\n", priv->firm[pos].type,
  517. (unsigned long long)*id);
  518. if (priv->firm_version < 0x0202)
  519. rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
  520. else
  521. rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
  522. if (rc < 0)
  523. return -EIO;
  524. rc = i2c_send(priv, p, 12);
  525. if (rc < 0)
  526. return -EIO;
  527. rc = send_seq(priv, {0x00, 0x8c});
  528. if (rc < 0)
  529. return -EIO;
  530. return 0;
  531. }
  532. static int check_firmware(struct dvb_frontend *fe, unsigned int type,
  533. v4l2_std_id std, __u16 int_freq)
  534. {
  535. struct xc2028_data *priv = fe->tuner_priv;
  536. struct firmware_properties new_fw;
  537. int rc = 0, is_retry = 0;
  538. u16 version, hwmodel;
  539. v4l2_std_id std0;
  540. tuner_dbg("%s called\n", __FUNCTION__);
  541. if (!priv->firm) {
  542. if (!priv->ctrl.fname) {
  543. tuner_info("xc2028/3028 firmware name not set!\n");
  544. return -EINVAL;
  545. }
  546. rc = load_all_firmwares(fe);
  547. if (rc < 0)
  548. return rc;
  549. }
  550. if (priv->ctrl.mts)
  551. type |= MTS;
  552. retry:
  553. new_fw.type = type;
  554. new_fw.id = std;
  555. new_fw.std_req = std;
  556. new_fw.scode_table = SCODE | priv->ctrl.scode_table;
  557. new_fw.scode_nr = 0;
  558. new_fw.int_freq = int_freq;
  559. tuner_dbg("checking firmware, user requested type=");
  560. if (debug) {
  561. dump_firm_type(new_fw.type);
  562. printk("(%x), id %016llx, scode_tbl ", new_fw.type,
  563. (unsigned long long)new_fw.std_req);
  564. dump_firm_type(priv->ctrl.scode_table);
  565. printk("(%x), scode_nr %d\n", priv->ctrl.scode_table,
  566. new_fw.scode_nr);
  567. }
  568. /* No need to reload base firmware if it matches */
  569. if (((BASE | new_fw.type) & BASE_TYPES) ==
  570. (priv->cur_fw.type & BASE_TYPES)) {
  571. tuner_dbg("BASE firmware not changed.\n");
  572. goto skip_base;
  573. }
  574. /* Updating BASE - forget about all currently loaded firmware */
  575. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  576. /* Reset is needed before loading firmware */
  577. rc = priv->tuner_callback(priv->video_dev,
  578. XC2028_TUNER_RESET, 0);
  579. if (rc < 0)
  580. goto fail;
  581. /* BASE firmwares are all std0 */
  582. std0 = 0;
  583. rc = load_firmware(fe, BASE | new_fw.type, &std0);
  584. if (rc < 0) {
  585. tuner_err("Error %d while loading base firmware\n",
  586. rc);
  587. goto fail;
  588. }
  589. /* Load INIT1, if needed */
  590. tuner_dbg("Load init1 firmware, if exists\n");
  591. rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
  592. if (rc == -ENOENT)
  593. rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
  594. &std0);
  595. if (rc < 0 && rc != -ENOENT) {
  596. tuner_err("Error %d while loading init1 firmware\n",
  597. rc);
  598. goto fail;
  599. }
  600. skip_base:
  601. /*
  602. * No need to reload standard specific firmware if base firmware
  603. * was not reloaded and requested video standards have not changed.
  604. */
  605. if (priv->cur_fw.type == (BASE | new_fw.type) &&
  606. priv->cur_fw.std_req == std) {
  607. tuner_dbg("Std-specific firmware already loaded.\n");
  608. goto skip_std_specific;
  609. }
  610. /* Reloading std-specific firmware forces a SCODE update */
  611. priv->cur_fw.scode_table = 0;
  612. rc = load_firmware(fe, new_fw.type, &new_fw.id);
  613. if (rc == -ENOENT)
  614. rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
  615. if (rc < 0)
  616. goto fail;
  617. skip_std_specific:
  618. if (priv->cur_fw.scode_table == new_fw.scode_table &&
  619. priv->cur_fw.scode_nr == new_fw.scode_nr) {
  620. tuner_dbg("SCODE firmware already loaded.\n");
  621. goto check_device;
  622. }
  623. /* Load SCODE firmware, if exists */
  624. tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
  625. rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
  626. new_fw.int_freq, new_fw.scode_nr);
  627. check_device:
  628. if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
  629. xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
  630. tuner_err("Unable to read tuner registers.\n");
  631. goto fail;
  632. }
  633. tuner_info("Device is Xceive %d version %d.%d, "
  634. "firmware version %d.%d\n",
  635. hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
  636. (version & 0xf0) >> 4, version & 0xf);
  637. /* Check firmware version against what we downloaded. */
  638. if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
  639. tuner_err("Incorrect readback of firmware version.\n");
  640. goto fail;
  641. }
  642. /* Check that the tuner hardware model remains consistent over time. */
  643. if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
  644. priv->hwmodel = hwmodel;
  645. priv->hwvers = version & 0xff00;
  646. } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
  647. priv->hwvers != (version & 0xff00)) {
  648. tuner_err("Read invalid device hardware information - tuner "
  649. "hung?\n");
  650. goto fail;
  651. }
  652. memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
  653. /*
  654. * By setting BASE in cur_fw.type only after successfully loading all
  655. * firmwares, we can:
  656. * 1. Identify that BASE firmware with type=0 has been loaded;
  657. * 2. Tell whether BASE firmware was just changed the next time through.
  658. */
  659. priv->cur_fw.type |= BASE;
  660. return 0;
  661. fail:
  662. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  663. if (!is_retry) {
  664. msleep(50);
  665. is_retry = 1;
  666. tuner_dbg("Retrying firmware load\n");
  667. goto retry;
  668. }
  669. if (rc == -ENOENT)
  670. rc = -EINVAL;
  671. return rc;
  672. }
  673. static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
  674. {
  675. struct xc2028_data *priv = fe->tuner_priv;
  676. u16 frq_lock, signal = 0;
  677. int rc;
  678. tuner_dbg("%s called\n", __FUNCTION__);
  679. mutex_lock(&priv->lock);
  680. /* Sync Lock Indicator */
  681. rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
  682. if (rc < 0 || frq_lock == 0)
  683. goto ret;
  684. /* Frequency is locked. Return signal quality */
  685. /* Get SNR of the video signal */
  686. rc = xc2028_get_reg(priv, 0x0040, &signal);
  687. if (rc < 0)
  688. signal = -frq_lock;
  689. ret:
  690. mutex_unlock(&priv->lock);
  691. *strength = signal;
  692. return rc;
  693. }
  694. #define DIV 15625
  695. static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
  696. enum tuner_mode new_mode,
  697. unsigned int type,
  698. v4l2_std_id std,
  699. u16 int_freq)
  700. {
  701. struct xc2028_data *priv = fe->tuner_priv;
  702. int rc = -EINVAL;
  703. unsigned char buf[4];
  704. u32 div, offset = 0;
  705. tuner_dbg("%s called\n", __FUNCTION__);
  706. mutex_lock(&priv->lock);
  707. tuner_dbg("should set frequency %d kHz\n", freq / 1000);
  708. if (check_firmware(fe, type, std, int_freq) < 0)
  709. goto ret;
  710. /* On some cases xc2028 can disable video output, if
  711. * very weak signals are received. By sending a soft
  712. * reset, this is re-enabled. So, it is better to always
  713. * send a soft reset before changing channels, to be sure
  714. * that xc2028 will be in a safe state.
  715. * Maybe this might also be needed for DTV.
  716. */
  717. if (new_mode == T_ANALOG_TV) {
  718. rc = send_seq(priv, {0x00, 0x00});
  719. } else {
  720. offset = 2750000;
  721. if (priv->cur_fw.type & DTV7)
  722. offset -= 500000;
  723. }
  724. div = (freq - offset + DIV / 2) / DIV;
  725. /* CMD= Set frequency */
  726. if (priv->firm_version < 0x0202)
  727. rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
  728. else
  729. rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
  730. if (rc < 0)
  731. goto ret;
  732. rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
  733. if (rc < 0)
  734. goto ret;
  735. msleep(10);
  736. buf[0] = 0xff & (div >> 24);
  737. buf[1] = 0xff & (div >> 16);
  738. buf[2] = 0xff & (div >> 8);
  739. buf[3] = 0xff & (div);
  740. rc = i2c_send(priv, buf, sizeof(buf));
  741. if (rc < 0)
  742. goto ret;
  743. msleep(100);
  744. priv->frequency = freq;
  745. tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
  746. buf[0], buf[1], buf[2], buf[3],
  747. freq / 1000000, (freq % 1000000) / 1000);
  748. rc = 0;
  749. ret:
  750. mutex_unlock(&priv->lock);
  751. return rc;
  752. }
  753. static int xc2028_set_analog_freq(struct dvb_frontend *fe,
  754. struct analog_parameters *p)
  755. {
  756. struct xc2028_data *priv = fe->tuner_priv;
  757. unsigned int type=0;
  758. tuner_dbg("%s called\n", __FUNCTION__);
  759. if (p->mode == V4L2_TUNER_RADIO) {
  760. type |= FM;
  761. if (priv->ctrl.input1)
  762. type |= INPUT1;
  763. return generic_set_freq(fe, (625l * p->frequency) / 10,
  764. T_ANALOG_TV, type, 0, 0);
  765. }
  766. /* if std is not defined, choose one */
  767. if (!p->std)
  768. p->std = V4L2_STD_MN;
  769. /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
  770. if (!(p->std & V4L2_STD_MN))
  771. type |= F8MHZ;
  772. /* Add audio hack to std mask */
  773. p->std |= parse_audio_std_option();
  774. return generic_set_freq(fe, 62500l * p->frequency,
  775. T_ANALOG_TV, type, p->std, 0);
  776. }
  777. static int xc2028_set_params(struct dvb_frontend *fe,
  778. struct dvb_frontend_parameters *p)
  779. {
  780. struct xc2028_data *priv = fe->tuner_priv;
  781. unsigned int type=0;
  782. fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
  783. tuner_dbg("%s called\n", __FUNCTION__);
  784. if (priv->ctrl.d2633)
  785. type |= D2633;
  786. else
  787. type |= D2620;
  788. switch(fe->ops.info.type) {
  789. case FE_OFDM:
  790. bw = p->u.ofdm.bandwidth;
  791. break;
  792. case FE_QAM:
  793. tuner_info("WARN: There are some reports that "
  794. "QAM 6 MHz doesn't work.\n"
  795. "If this works for you, please report by "
  796. "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
  797. bw = BANDWIDTH_6_MHZ;
  798. type |= QAM;
  799. break;
  800. case FE_ATSC:
  801. bw = BANDWIDTH_6_MHZ;
  802. type |= ATSC| D2633;
  803. break;
  804. /* DVB-S is not supported */
  805. default:
  806. return -EINVAL;
  807. }
  808. /* FIXME:
  809. There are two Scodes that will never be selected:
  810. DTV78 ZARLINK456, DTV78 DIBCOM52
  811. When it should opt for DTV78 instead of DTV7 or DTV8?
  812. */
  813. switch (bw) {
  814. case BANDWIDTH_8_MHZ:
  815. type |= DTV8 | F8MHZ;
  816. break;
  817. case BANDWIDTH_7_MHZ:
  818. type |= DTV7 | F8MHZ;
  819. break;
  820. case BANDWIDTH_6_MHZ:
  821. type |= DTV6 ;
  822. break;
  823. default:
  824. tuner_err("error: bandwidth not supported.\n");
  825. };
  826. /* All S-code tables need a 200kHz shift */
  827. if (priv->ctrl.demod)
  828. priv->ctrl.demod += 200;
  829. return generic_set_freq(fe, p->frequency,
  830. T_DIGITAL_TV, type, 0, priv->ctrl.demod);
  831. }
  832. static int xc2028_sleep(struct dvb_frontend *fe)
  833. {
  834. struct xc2028_data *priv = fe->tuner_priv;
  835. int rc = 0;
  836. tuner_dbg("%s called\n", __FUNCTION__);
  837. mutex_lock(&priv->lock);
  838. if (priv->firm_version < 0x0202)
  839. rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
  840. else
  841. rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
  842. priv->cur_fw.type = 0; /* need firmware reload */
  843. mutex_unlock(&priv->lock);
  844. return rc;
  845. }
  846. static int xc2028_dvb_release(struct dvb_frontend *fe)
  847. {
  848. struct xc2028_data *priv = fe->tuner_priv;
  849. tuner_dbg("%s called\n", __FUNCTION__);
  850. mutex_lock(&xc2028_list_mutex);
  851. priv->count--;
  852. if (!priv->count) {
  853. list_del(&priv->xc2028_list);
  854. kfree(priv->ctrl.fname);
  855. free_firmware(priv);
  856. kfree(priv);
  857. fe->tuner_priv = NULL;
  858. }
  859. mutex_unlock(&xc2028_list_mutex);
  860. return 0;
  861. }
  862. static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  863. {
  864. struct xc2028_data *priv = fe->tuner_priv;
  865. tuner_dbg("%s called\n", __FUNCTION__);
  866. *frequency = priv->frequency;
  867. return 0;
  868. }
  869. static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
  870. {
  871. struct xc2028_data *priv = fe->tuner_priv;
  872. struct xc2028_ctrl *p = priv_cfg;
  873. int rc = 0;
  874. tuner_dbg("%s called\n", __FUNCTION__);
  875. mutex_lock(&priv->lock);
  876. kfree(priv->ctrl.fname);
  877. free_firmware(priv);
  878. memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
  879. priv->ctrl.fname = NULL;
  880. if (p->fname) {
  881. priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
  882. if (priv->ctrl.fname == NULL)
  883. rc = -ENOMEM;
  884. }
  885. if (priv->ctrl.max_len < 9)
  886. priv->ctrl.max_len = 13;
  887. mutex_unlock(&priv->lock);
  888. return rc;
  889. }
  890. static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
  891. .info = {
  892. .name = "Xceive XC3028",
  893. .frequency_min = 42000000,
  894. .frequency_max = 864000000,
  895. .frequency_step = 50000,
  896. },
  897. .set_config = xc2028_set_config,
  898. .set_analog_params = xc2028_set_analog_freq,
  899. .release = xc2028_dvb_release,
  900. .get_frequency = xc2028_get_frequency,
  901. .get_rf_strength = xc2028_signal,
  902. .set_params = xc2028_set_params,
  903. .sleep = xc2028_sleep,
  904. };
  905. void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
  906. {
  907. struct xc2028_data *priv;
  908. void *video_dev;
  909. if (debug)
  910. printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
  911. if (NULL == cfg || NULL == cfg->video_dev)
  912. return NULL;
  913. if (!fe) {
  914. printk(KERN_ERR PREFIX ": No frontend!\n");
  915. return NULL;
  916. }
  917. video_dev = cfg->video_dev;
  918. mutex_lock(&xc2028_list_mutex);
  919. list_for_each_entry(priv, &xc2028_list, xc2028_list) {
  920. if (priv->video_dev == cfg->video_dev) {
  921. video_dev = NULL;
  922. break;
  923. }
  924. }
  925. if (video_dev) {
  926. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  927. if (priv == NULL) {
  928. mutex_unlock(&xc2028_list_mutex);
  929. return NULL;
  930. }
  931. priv->i2c_props.addr = cfg->i2c_addr;
  932. priv->i2c_props.adap = cfg->i2c_adap;
  933. priv->video_dev = video_dev;
  934. priv->tuner_callback = cfg->callback;
  935. priv->ctrl.max_len = 13;
  936. mutex_init(&priv->lock);
  937. list_add_tail(&priv->xc2028_list, &xc2028_list);
  938. }
  939. fe->tuner_priv = priv;
  940. priv->count++;
  941. memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
  942. sizeof(xc2028_dvb_tuner_ops));
  943. tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
  944. if (cfg->ctrl)
  945. xc2028_set_config(fe, cfg->ctrl);
  946. mutex_unlock(&xc2028_list_mutex);
  947. return fe;
  948. }
  949. EXPORT_SYMBOL(xc2028_attach);
  950. MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
  951. MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
  952. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  953. MODULE_LICENSE("GPL");