tuner-xc2028.c 28 KB

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