tuner-xc2028.c 26 KB

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