tuner-xc2028.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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 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 ign_firm_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 &= BASE_TYPES;
  359. else if (type & SCODE) {
  360. type &= SCODE_TYPES;
  361. ign_firm_type_mask = HAS_IF;
  362. } else if (type & DTV_TYPES)
  363. type &= DTV_TYPES;
  364. else if (type & STD_SPECIFIC_TYPES)
  365. type &= STD_SPECIFIC_TYPES;
  366. /* Seek for exact match */
  367. for (i = 0; i < priv->firm_size; i++) {
  368. if ((type == (priv->firm[i].type & ~ign_firm_type_mask)) &&
  369. (*id == priv->firm[i].id))
  370. goto found;
  371. }
  372. /* Seek for generic video standard match */
  373. for (i = 0; i < priv->firm_size; i++) {
  374. v4l2_std_id match_mask;
  375. int nr_matches;
  376. if (type != (priv->firm[i].type & ~ign_firm_type_mask))
  377. continue;
  378. match_mask = *id & priv->firm[i].id;
  379. if (!match_mask)
  380. continue;
  381. if ((*id & match_mask) == *id)
  382. goto found; /* Supports all the requested standards */
  383. nr_matches = hweight64(match_mask);
  384. if (nr_matches > best_nr_matches) {
  385. best_nr_matches = nr_matches;
  386. best_i = i;
  387. }
  388. }
  389. if (best_nr_matches > 0) {
  390. tuner_dbg("Selecting best matching firmware (%d bits) for "
  391. "type=", best_nr_matches);
  392. dump_firm_type(type);
  393. printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
  394. i = best_i;
  395. goto found;
  396. }
  397. /*FIXME: Would make sense to seek for type "hint" match ? */
  398. i = -ENOENT;
  399. goto ret;
  400. found:
  401. *id = priv->firm[i].id;
  402. ret:
  403. tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
  404. if (debug) {
  405. dump_firm_type(type);
  406. printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
  407. }
  408. return i;
  409. }
  410. static int load_firmware(struct dvb_frontend *fe, unsigned int type,
  411. v4l2_std_id *id)
  412. {
  413. struct xc2028_data *priv = fe->tuner_priv;
  414. int pos, rc;
  415. unsigned char *p, *endp, buf[priv->ctrl.max_len];
  416. tuner_dbg("%s called\n", __func__);
  417. pos = seek_firmware(fe, type, id);
  418. if (pos < 0)
  419. return pos;
  420. tuner_info("Loading firmware for type=");
  421. dump_firm_type(priv->firm[pos].type);
  422. printk("(%x), id %016llx.\n", priv->firm[pos].type,
  423. (unsigned long long)*id);
  424. p = priv->firm[pos].ptr;
  425. endp = p + priv->firm[pos].size;
  426. while (p < endp) {
  427. __u16 size;
  428. /* Checks if there's enough bytes to read */
  429. if (p + sizeof(size) > endp) {
  430. tuner_err("Firmware chunk size is wrong\n");
  431. return -EINVAL;
  432. }
  433. size = le16_to_cpu(*(__u16 *) p);
  434. p += sizeof(size);
  435. if (size == 0xffff)
  436. return 0;
  437. if (!size) {
  438. /* Special callback command received */
  439. rc = priv->tuner_callback(priv->video_dev,
  440. XC2028_TUNER_RESET, 0);
  441. if (rc < 0) {
  442. tuner_err("Error at RESET code %d\n",
  443. (*p) & 0x7f);
  444. return -EINVAL;
  445. }
  446. continue;
  447. }
  448. if (size >= 0xff00) {
  449. switch (size) {
  450. case 0xff00:
  451. rc = priv->tuner_callback(priv->video_dev,
  452. XC2028_RESET_CLK, 0);
  453. if (rc < 0) {
  454. tuner_err("Error at RESET code %d\n",
  455. (*p) & 0x7f);
  456. return -EINVAL;
  457. }
  458. break;
  459. default:
  460. tuner_info("Invalid RESET code %d\n",
  461. size & 0x7f);
  462. return -EINVAL;
  463. }
  464. continue;
  465. }
  466. /* Checks for a sleep command */
  467. if (size & 0x8000) {
  468. msleep(size & 0x7fff);
  469. continue;
  470. }
  471. if ((size + p > endp)) {
  472. tuner_err("missing bytes: need %d, have %d\n",
  473. size, (int)(endp - p));
  474. return -EINVAL;
  475. }
  476. buf[0] = *p;
  477. p++;
  478. size--;
  479. /* Sends message chunks */
  480. while (size > 0) {
  481. int len = (size < priv->ctrl.max_len - 1) ?
  482. size : priv->ctrl.max_len - 1;
  483. memcpy(buf + 1, p, len);
  484. rc = i2c_send(priv, buf, len + 1);
  485. if (rc < 0) {
  486. tuner_err("%d returned from send\n", rc);
  487. return -EINVAL;
  488. }
  489. p += len;
  490. size -= len;
  491. }
  492. }
  493. return 0;
  494. }
  495. static int load_scode(struct dvb_frontend *fe, unsigned int type,
  496. v4l2_std_id *id, __u16 int_freq, int scode)
  497. {
  498. struct xc2028_data *priv = fe->tuner_priv;
  499. int pos, rc;
  500. unsigned char *p;
  501. tuner_dbg("%s called\n", __func__);
  502. if (!int_freq) {
  503. pos = seek_firmware(fe, type, id);
  504. if (pos < 0)
  505. return pos;
  506. } else {
  507. for (pos = 0; pos < priv->firm_size; pos++) {
  508. if ((priv->firm[pos].int_freq == int_freq) &&
  509. (priv->firm[pos].type & HAS_IF))
  510. break;
  511. }
  512. if (pos == priv->firm_size)
  513. return -ENOENT;
  514. }
  515. p = priv->firm[pos].ptr;
  516. if (priv->firm[pos].type & HAS_IF) {
  517. if (priv->firm[pos].size != 12 * 16 || scode >= 16)
  518. return -EINVAL;
  519. p += 12 * scode;
  520. } else {
  521. /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
  522. * has a 2-byte size header in the firmware format. */
  523. if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
  524. le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
  525. return -EINVAL;
  526. p += 14 * scode + 2;
  527. }
  528. tuner_info("Loading SCODE for type=");
  529. dump_firm_type_and_int_freq(priv->firm[pos].type,
  530. priv->firm[pos].int_freq);
  531. printk("(%x), id %016llx.\n", priv->firm[pos].type,
  532. (unsigned long long)*id);
  533. if (priv->firm_version < 0x0202)
  534. rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
  535. else
  536. rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
  537. if (rc < 0)
  538. return -EIO;
  539. rc = i2c_send(priv, p, 12);
  540. if (rc < 0)
  541. return -EIO;
  542. rc = send_seq(priv, {0x00, 0x8c});
  543. if (rc < 0)
  544. return -EIO;
  545. return 0;
  546. }
  547. static int check_firmware(struct dvb_frontend *fe, unsigned int type,
  548. v4l2_std_id std, __u16 int_freq)
  549. {
  550. struct xc2028_data *priv = fe->tuner_priv;
  551. struct firmware_properties new_fw;
  552. int rc = 0, is_retry = 0;
  553. u16 version, hwmodel;
  554. v4l2_std_id std0;
  555. tuner_dbg("%s called\n", __func__);
  556. if (!priv->firm) {
  557. if (!priv->ctrl.fname) {
  558. tuner_info("xc2028/3028 firmware name not set!\n");
  559. return -EINVAL;
  560. }
  561. rc = load_all_firmwares(fe);
  562. if (rc < 0)
  563. return rc;
  564. }
  565. if (priv->ctrl.mts && !(type & FM))
  566. type |= MTS;
  567. retry:
  568. new_fw.type = type;
  569. new_fw.id = std;
  570. new_fw.std_req = std;
  571. new_fw.scode_table = SCODE | priv->ctrl.scode_table;
  572. new_fw.scode_nr = 0;
  573. new_fw.int_freq = int_freq;
  574. tuner_dbg("checking firmware, user requested type=");
  575. if (debug) {
  576. dump_firm_type(new_fw.type);
  577. printk("(%x), id %016llx, ", new_fw.type,
  578. (unsigned long long)new_fw.std_req);
  579. if (!int_freq) {
  580. printk("scode_tbl ");
  581. dump_firm_type(priv->ctrl.scode_table);
  582. printk("(%x), ", priv->ctrl.scode_table);
  583. } else
  584. printk("int_freq %d, ", new_fw.int_freq);
  585. printk("scode_nr %d\n", new_fw.scode_nr);
  586. }
  587. /* No need to reload base firmware if it matches */
  588. if (((BASE | new_fw.type) & BASE_TYPES) ==
  589. (priv->cur_fw.type & BASE_TYPES)) {
  590. tuner_dbg("BASE firmware not changed.\n");
  591. goto skip_base;
  592. }
  593. /* Updating BASE - forget about all currently loaded firmware */
  594. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  595. /* Reset is needed before loading firmware */
  596. rc = priv->tuner_callback(priv->video_dev,
  597. XC2028_TUNER_RESET, 0);
  598. if (rc < 0)
  599. goto fail;
  600. /* BASE firmwares are all std0 */
  601. std0 = 0;
  602. rc = load_firmware(fe, BASE | new_fw.type, &std0);
  603. if (rc < 0) {
  604. tuner_err("Error %d while loading base firmware\n",
  605. rc);
  606. goto fail;
  607. }
  608. /* Load INIT1, if needed */
  609. tuner_dbg("Load init1 firmware, if exists\n");
  610. rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
  611. if (rc == -ENOENT)
  612. rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
  613. &std0);
  614. if (rc < 0 && rc != -ENOENT) {
  615. tuner_err("Error %d while loading init1 firmware\n",
  616. rc);
  617. goto fail;
  618. }
  619. skip_base:
  620. /*
  621. * No need to reload standard specific firmware if base firmware
  622. * was not reloaded and requested video standards have not changed.
  623. */
  624. if (priv->cur_fw.type == (BASE | new_fw.type) &&
  625. priv->cur_fw.std_req == std) {
  626. tuner_dbg("Std-specific firmware already loaded.\n");
  627. goto skip_std_specific;
  628. }
  629. /* Reloading std-specific firmware forces a SCODE update */
  630. priv->cur_fw.scode_table = 0;
  631. rc = load_firmware(fe, new_fw.type, &new_fw.id);
  632. if (rc == -ENOENT)
  633. rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
  634. if (rc < 0)
  635. goto fail;
  636. skip_std_specific:
  637. if (priv->cur_fw.scode_table == new_fw.scode_table &&
  638. priv->cur_fw.scode_nr == new_fw.scode_nr) {
  639. tuner_dbg("SCODE firmware already loaded.\n");
  640. goto check_device;
  641. }
  642. if (new_fw.type & FM)
  643. goto check_device;
  644. /* Load SCODE firmware, if exists */
  645. tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
  646. rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
  647. new_fw.int_freq, new_fw.scode_nr);
  648. check_device:
  649. if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
  650. xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
  651. tuner_err("Unable to read tuner registers.\n");
  652. goto fail;
  653. }
  654. tuner_info("Device is Xceive %d version %d.%d, "
  655. "firmware version %d.%d\n",
  656. hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
  657. (version & 0xf0) >> 4, version & 0xf);
  658. /* Check firmware version against what we downloaded. */
  659. if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
  660. tuner_err("Incorrect readback of firmware version.\n");
  661. goto fail;
  662. }
  663. /* Check that the tuner hardware model remains consistent over time. */
  664. if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
  665. priv->hwmodel = hwmodel;
  666. priv->hwvers = version & 0xff00;
  667. } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
  668. priv->hwvers != (version & 0xff00)) {
  669. tuner_err("Read invalid device hardware information - tuner "
  670. "hung?\n");
  671. goto fail;
  672. }
  673. memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
  674. /*
  675. * By setting BASE in cur_fw.type only after successfully loading all
  676. * firmwares, we can:
  677. * 1. Identify that BASE firmware with type=0 has been loaded;
  678. * 2. Tell whether BASE firmware was just changed the next time through.
  679. */
  680. priv->cur_fw.type |= BASE;
  681. return 0;
  682. fail:
  683. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  684. if (!is_retry) {
  685. msleep(50);
  686. is_retry = 1;
  687. tuner_dbg("Retrying firmware load\n");
  688. goto retry;
  689. }
  690. if (rc == -ENOENT)
  691. rc = -EINVAL;
  692. return rc;
  693. }
  694. static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
  695. {
  696. struct xc2028_data *priv = fe->tuner_priv;
  697. u16 frq_lock, signal = 0;
  698. int rc;
  699. tuner_dbg("%s called\n", __func__);
  700. mutex_lock(&priv->lock);
  701. /* Sync Lock Indicator */
  702. rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
  703. if (rc < 0 || frq_lock == 0)
  704. goto ret;
  705. /* Frequency is locked. Return signal quality */
  706. /* Get SNR of the video signal */
  707. rc = xc2028_get_reg(priv, 0x0040, &signal);
  708. if (rc < 0)
  709. signal = -frq_lock;
  710. ret:
  711. mutex_unlock(&priv->lock);
  712. *strength = signal;
  713. return rc;
  714. }
  715. #define DIV 15625
  716. static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
  717. enum tuner_mode new_mode,
  718. unsigned int type,
  719. v4l2_std_id std,
  720. u16 int_freq)
  721. {
  722. struct xc2028_data *priv = fe->tuner_priv;
  723. int rc = -EINVAL;
  724. unsigned char buf[4];
  725. u32 div, offset = 0;
  726. tuner_dbg("%s called\n", __func__);
  727. mutex_lock(&priv->lock);
  728. tuner_dbg("should set frequency %d kHz\n", freq / 1000);
  729. if (check_firmware(fe, type, std, int_freq) < 0)
  730. goto ret;
  731. /* On some cases xc2028 can disable video output, if
  732. * very weak signals are received. By sending a soft
  733. * reset, this is re-enabled. So, it is better to always
  734. * send a soft reset before changing channels, to be sure
  735. * that xc2028 will be in a safe state.
  736. * Maybe this might also be needed for DTV.
  737. */
  738. if (new_mode == T_ANALOG_TV) {
  739. rc = send_seq(priv, {0x00, 0x00});
  740. } else if (priv->cur_fw.type & ATSC) {
  741. offset = 1750000;
  742. } else {
  743. offset = 2750000;
  744. /*
  745. * We must adjust the offset by 500kHz in two cases in order
  746. * to correctly center the IF output:
  747. * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
  748. * selected and a 7MHz channel is tuned;
  749. * 2) When tuning a VHF channel with DTV78 firmware.
  750. */
  751. if (((priv->cur_fw.type & DTV7) &&
  752. (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
  753. ((priv->cur_fw.type & DTV78) && freq < 470000000))
  754. offset -= 500000;
  755. }
  756. div = (freq - offset + DIV / 2) / DIV;
  757. /* CMD= Set frequency */
  758. if (priv->firm_version < 0x0202)
  759. rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
  760. else
  761. rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
  762. if (rc < 0)
  763. goto ret;
  764. /* Return code shouldn't be checked.
  765. The reset CLK is needed only with tm6000.
  766. Driver should work fine even if this fails.
  767. */
  768. priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
  769. msleep(10);
  770. buf[0] = 0xff & (div >> 24);
  771. buf[1] = 0xff & (div >> 16);
  772. buf[2] = 0xff & (div >> 8);
  773. buf[3] = 0xff & (div);
  774. rc = i2c_send(priv, buf, sizeof(buf));
  775. if (rc < 0)
  776. goto ret;
  777. msleep(100);
  778. priv->frequency = freq;
  779. tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
  780. buf[0], buf[1], buf[2], buf[3],
  781. freq / 1000000, (freq % 1000000) / 1000);
  782. rc = 0;
  783. ret:
  784. mutex_unlock(&priv->lock);
  785. return rc;
  786. }
  787. static int xc2028_set_analog_freq(struct dvb_frontend *fe,
  788. struct analog_parameters *p)
  789. {
  790. struct xc2028_data *priv = fe->tuner_priv;
  791. unsigned int type=0;
  792. tuner_dbg("%s called\n", __func__);
  793. if (p->mode == V4L2_TUNER_RADIO) {
  794. type |= FM;
  795. if (priv->ctrl.input1)
  796. type |= INPUT1;
  797. return generic_set_freq(fe, (625l * p->frequency) / 10,
  798. T_ANALOG_TV, type, 0, 0);
  799. }
  800. /* if std is not defined, choose one */
  801. if (!p->std)
  802. p->std = V4L2_STD_MN;
  803. /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
  804. if (!(p->std & V4L2_STD_MN))
  805. type |= F8MHZ;
  806. /* Add audio hack to std mask */
  807. p->std |= parse_audio_std_option();
  808. return generic_set_freq(fe, 62500l * p->frequency,
  809. T_ANALOG_TV, type, p->std, 0);
  810. }
  811. static int xc2028_set_params(struct dvb_frontend *fe,
  812. struct dvb_frontend_parameters *p)
  813. {
  814. struct xc2028_data *priv = fe->tuner_priv;
  815. unsigned int type=0;
  816. fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
  817. u16 demod = 0;
  818. tuner_dbg("%s called\n", __func__);
  819. if (priv->ctrl.d2633)
  820. type |= D2633;
  821. else
  822. type |= D2620;
  823. switch(fe->ops.info.type) {
  824. case FE_OFDM:
  825. bw = p->u.ofdm.bandwidth;
  826. break;
  827. case FE_QAM:
  828. tuner_info("WARN: There are some reports that "
  829. "QAM 6 MHz doesn't work.\n"
  830. "If this works for you, please report by "
  831. "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
  832. bw = BANDWIDTH_6_MHZ;
  833. type |= QAM;
  834. break;
  835. case FE_ATSC:
  836. bw = BANDWIDTH_6_MHZ;
  837. /* The only ATSC firmware (at least on v2.7) is D2633,
  838. so overrides ctrl->d2633 */
  839. type |= ATSC| D2633;
  840. type &= ~D2620;
  841. break;
  842. /* DVB-S is not supported */
  843. default:
  844. return -EINVAL;
  845. }
  846. switch (bw) {
  847. case BANDWIDTH_8_MHZ:
  848. if (p->frequency < 470000000)
  849. priv->ctrl.vhfbw7 = 0;
  850. else
  851. priv->ctrl.uhfbw8 = 1;
  852. type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
  853. type |= F8MHZ;
  854. break;
  855. case BANDWIDTH_7_MHZ:
  856. if (p->frequency < 470000000)
  857. priv->ctrl.vhfbw7 = 1;
  858. else
  859. priv->ctrl.uhfbw8 = 0;
  860. type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
  861. type |= F8MHZ;
  862. break;
  863. case BANDWIDTH_6_MHZ:
  864. type |= DTV6;
  865. priv->ctrl.vhfbw7 = 0;
  866. priv->ctrl.uhfbw8 = 0;
  867. break;
  868. default:
  869. tuner_err("error: bandwidth not supported.\n");
  870. };
  871. /* All S-code tables need a 200kHz shift */
  872. if (priv->ctrl.demod)
  873. demod = priv->ctrl.demod + 200;
  874. return generic_set_freq(fe, p->frequency,
  875. T_DIGITAL_TV, type, 0, demod);
  876. }
  877. static int xc2028_dvb_release(struct dvb_frontend *fe)
  878. {
  879. struct xc2028_data *priv = fe->tuner_priv;
  880. tuner_dbg("%s called\n", __func__);
  881. mutex_lock(&xc2028_list_mutex);
  882. priv->count--;
  883. if (!priv->count) {
  884. list_del(&priv->xc2028_list);
  885. kfree(priv->ctrl.fname);
  886. free_firmware(priv);
  887. kfree(priv);
  888. fe->tuner_priv = NULL;
  889. }
  890. mutex_unlock(&xc2028_list_mutex);
  891. return 0;
  892. }
  893. static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  894. {
  895. struct xc2028_data *priv = fe->tuner_priv;
  896. tuner_dbg("%s called\n", __func__);
  897. *frequency = priv->frequency;
  898. return 0;
  899. }
  900. static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
  901. {
  902. struct xc2028_data *priv = fe->tuner_priv;
  903. struct xc2028_ctrl *p = priv_cfg;
  904. int rc = 0;
  905. tuner_dbg("%s called\n", __func__);
  906. mutex_lock(&priv->lock);
  907. memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
  908. if (priv->ctrl.max_len < 9)
  909. priv->ctrl.max_len = 13;
  910. if (p->fname) {
  911. if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
  912. kfree(priv->ctrl.fname);
  913. free_firmware(priv);
  914. }
  915. priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
  916. if (priv->ctrl.fname == NULL)
  917. rc = -ENOMEM;
  918. }
  919. mutex_unlock(&priv->lock);
  920. return rc;
  921. }
  922. static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
  923. .info = {
  924. .name = "Xceive XC3028",
  925. .frequency_min = 42000000,
  926. .frequency_max = 864000000,
  927. .frequency_step = 50000,
  928. },
  929. .set_config = xc2028_set_config,
  930. .set_analog_params = xc2028_set_analog_freq,
  931. .release = xc2028_dvb_release,
  932. .get_frequency = xc2028_get_frequency,
  933. .get_rf_strength = xc2028_signal,
  934. .set_params = xc2028_set_params,
  935. };
  936. struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
  937. struct xc2028_config *cfg)
  938. {
  939. struct xc2028_data *priv;
  940. void *video_dev;
  941. if (debug)
  942. printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
  943. if (NULL == cfg)
  944. return NULL;
  945. if (!fe) {
  946. printk(KERN_ERR "xc2028: No frontend!\n");
  947. return NULL;
  948. }
  949. video_dev = cfg->i2c_adap->algo_data;
  950. if (debug)
  951. printk(KERN_DEBUG "xc2028: video_dev =%p\n", video_dev);
  952. mutex_lock(&xc2028_list_mutex);
  953. list_for_each_entry(priv, &xc2028_list, xc2028_list) {
  954. if (&priv->i2c_props.adap->dev == &cfg->i2c_adap->dev) {
  955. video_dev = NULL;
  956. if (debug)
  957. printk(KERN_DEBUG "xc2028: reusing device\n");
  958. break;
  959. }
  960. }
  961. if (video_dev) {
  962. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  963. if (priv == NULL) {
  964. mutex_unlock(&xc2028_list_mutex);
  965. return NULL;
  966. }
  967. priv->i2c_props.addr = cfg->i2c_addr;
  968. priv->i2c_props.adap = cfg->i2c_adap;
  969. priv->i2c_props.name = "xc2028";
  970. priv->video_dev = video_dev;
  971. priv->tuner_callback = cfg->callback;
  972. priv->ctrl.max_len = 13;
  973. mutex_init(&priv->lock);
  974. list_add_tail(&priv->xc2028_list, &xc2028_list);
  975. }
  976. fe->tuner_priv = priv;
  977. priv->count++;
  978. if (debug)
  979. printk(KERN_DEBUG "xc2028: usage count is %i\n", priv->count);
  980. memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
  981. sizeof(xc2028_dvb_tuner_ops));
  982. tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
  983. if (cfg->ctrl)
  984. xc2028_set_config(fe, cfg->ctrl);
  985. mutex_unlock(&xc2028_list_mutex);
  986. return fe;
  987. }
  988. EXPORT_SYMBOL(xc2028_attach);
  989. MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
  990. MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
  991. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  992. MODULE_LICENSE("GPL");