tuner-xc2028.c 28 KB

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