tuner-xc2028.c 28 KB

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