tuner-xc2028.c 27 KB

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