tuner-xc2028.c 27 KB

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