tuner-xc2028.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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. static 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 && !(type & FM))
  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. if (new_fw.type & FM)
  635. goto check_device;
  636. /* Load SCODE firmware, if exists */
  637. tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
  638. rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
  639. new_fw.int_freq, new_fw.scode_nr);
  640. check_device:
  641. if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
  642. xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
  643. tuner_err("Unable to read tuner registers.\n");
  644. goto fail;
  645. }
  646. tuner_info("Device is Xceive %d version %d.%d, "
  647. "firmware version %d.%d\n",
  648. hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
  649. (version & 0xf0) >> 4, version & 0xf);
  650. /* Check firmware version against what we downloaded. */
  651. if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
  652. tuner_err("Incorrect readback of firmware version.\n");
  653. goto fail;
  654. }
  655. /* Check that the tuner hardware model remains consistent over time. */
  656. if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
  657. priv->hwmodel = hwmodel;
  658. priv->hwvers = version & 0xff00;
  659. } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
  660. priv->hwvers != (version & 0xff00)) {
  661. tuner_err("Read invalid device hardware information - tuner "
  662. "hung?\n");
  663. goto fail;
  664. }
  665. memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
  666. /*
  667. * By setting BASE in cur_fw.type only after successfully loading all
  668. * firmwares, we can:
  669. * 1. Identify that BASE firmware with type=0 has been loaded;
  670. * 2. Tell whether BASE firmware was just changed the next time through.
  671. */
  672. priv->cur_fw.type |= BASE;
  673. return 0;
  674. fail:
  675. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  676. if (!is_retry) {
  677. msleep(50);
  678. is_retry = 1;
  679. tuner_dbg("Retrying firmware load\n");
  680. goto retry;
  681. }
  682. if (rc == -ENOENT)
  683. rc = -EINVAL;
  684. return rc;
  685. }
  686. static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
  687. {
  688. struct xc2028_data *priv = fe->tuner_priv;
  689. u16 frq_lock, signal = 0;
  690. int rc;
  691. tuner_dbg("%s called\n", __FUNCTION__);
  692. mutex_lock(&priv->lock);
  693. /* Sync Lock Indicator */
  694. rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
  695. if (rc < 0 || frq_lock == 0)
  696. goto ret;
  697. /* Frequency is locked. Return signal quality */
  698. /* Get SNR of the video signal */
  699. rc = xc2028_get_reg(priv, 0x0040, &signal);
  700. if (rc < 0)
  701. signal = -frq_lock;
  702. ret:
  703. mutex_unlock(&priv->lock);
  704. *strength = signal;
  705. return rc;
  706. }
  707. #define DIV 15625
  708. static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
  709. enum tuner_mode new_mode,
  710. unsigned int type,
  711. v4l2_std_id std,
  712. u16 int_freq)
  713. {
  714. struct xc2028_data *priv = fe->tuner_priv;
  715. int rc = -EINVAL;
  716. unsigned char buf[4];
  717. u32 div, offset = 0;
  718. tuner_dbg("%s called\n", __FUNCTION__);
  719. mutex_lock(&priv->lock);
  720. tuner_dbg("should set frequency %d kHz\n", freq / 1000);
  721. if (check_firmware(fe, type, std, int_freq) < 0)
  722. goto ret;
  723. /* On some cases xc2028 can disable video output, if
  724. * very weak signals are received. By sending a soft
  725. * reset, this is re-enabled. So, it is better to always
  726. * send a soft reset before changing channels, to be sure
  727. * that xc2028 will be in a safe state.
  728. * Maybe this might also be needed for DTV.
  729. */
  730. if (new_mode == T_ANALOG_TV) {
  731. rc = send_seq(priv, {0x00, 0x00});
  732. } else if (priv->cur_fw.type & ATSC) {
  733. offset = 1750000;
  734. } else {
  735. offset = 2750000;
  736. /*
  737. * We must adjust the offset by 500kHz in two cases in order
  738. * to correctly center the IF output:
  739. * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
  740. * selected and a 7MHz channel is tuned;
  741. * 2) When tuning a VHF channel with DTV78 firmware.
  742. */
  743. if (((priv->cur_fw.type & DTV7) &&
  744. (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
  745. ((priv->cur_fw.type & DTV78) && freq < 470000000))
  746. offset -= 500000;
  747. }
  748. div = (freq - offset + DIV / 2) / DIV;
  749. /* CMD= Set frequency */
  750. if (priv->firm_version < 0x0202)
  751. rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
  752. else
  753. rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
  754. if (rc < 0)
  755. goto ret;
  756. rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
  757. if (rc < 0)
  758. goto ret;
  759. msleep(10);
  760. buf[0] = 0xff & (div >> 24);
  761. buf[1] = 0xff & (div >> 16);
  762. buf[2] = 0xff & (div >> 8);
  763. buf[3] = 0xff & (div);
  764. rc = i2c_send(priv, buf, sizeof(buf));
  765. if (rc < 0)
  766. goto ret;
  767. msleep(100);
  768. priv->frequency = freq;
  769. tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
  770. buf[0], buf[1], buf[2], buf[3],
  771. freq / 1000000, (freq % 1000000) / 1000);
  772. rc = 0;
  773. ret:
  774. mutex_unlock(&priv->lock);
  775. return rc;
  776. }
  777. static int xc2028_set_analog_freq(struct dvb_frontend *fe,
  778. struct analog_parameters *p)
  779. {
  780. struct xc2028_data *priv = fe->tuner_priv;
  781. unsigned int type=0;
  782. tuner_dbg("%s called\n", __FUNCTION__);
  783. if (p->mode == V4L2_TUNER_RADIO) {
  784. type |= FM;
  785. if (priv->ctrl.input1)
  786. type |= INPUT1;
  787. return generic_set_freq(fe, (625l * p->frequency) / 10,
  788. T_ANALOG_TV, type, 0, 0);
  789. }
  790. /* if std is not defined, choose one */
  791. if (!p->std)
  792. p->std = V4L2_STD_MN;
  793. /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
  794. if (!(p->std & V4L2_STD_MN))
  795. type |= F8MHZ;
  796. /* Add audio hack to std mask */
  797. p->std |= parse_audio_std_option();
  798. return generic_set_freq(fe, 62500l * p->frequency,
  799. T_ANALOG_TV, type, p->std, 0);
  800. }
  801. static int xc2028_set_params(struct dvb_frontend *fe,
  802. struct dvb_frontend_parameters *p)
  803. {
  804. struct xc2028_data *priv = fe->tuner_priv;
  805. unsigned int type=0;
  806. fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
  807. u16 demod = 0;
  808. tuner_dbg("%s called\n", __FUNCTION__);
  809. if (priv->ctrl.d2633)
  810. type |= D2633;
  811. else
  812. type |= D2620;
  813. switch(fe->ops.info.type) {
  814. case FE_OFDM:
  815. bw = p->u.ofdm.bandwidth;
  816. break;
  817. case FE_QAM:
  818. tuner_info("WARN: There are some reports that "
  819. "QAM 6 MHz doesn't work.\n"
  820. "If this works for you, please report by "
  821. "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
  822. bw = BANDWIDTH_6_MHZ;
  823. type |= QAM;
  824. break;
  825. case FE_ATSC:
  826. bw = BANDWIDTH_6_MHZ;
  827. /* The only ATSC firmware (at least on v2.7) is D2633,
  828. so overrides ctrl->d2633 */
  829. type |= ATSC| D2633;
  830. type &= ~D2620;
  831. break;
  832. /* DVB-S is not supported */
  833. default:
  834. return -EINVAL;
  835. }
  836. switch (bw) {
  837. case BANDWIDTH_8_MHZ:
  838. if (p->frequency < 470000000)
  839. priv->ctrl.vhfbw7 = 0;
  840. else
  841. priv->ctrl.uhfbw8 = 1;
  842. type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
  843. type |= F8MHZ;
  844. break;
  845. case BANDWIDTH_7_MHZ:
  846. if (p->frequency < 470000000)
  847. priv->ctrl.vhfbw7 = 1;
  848. else
  849. priv->ctrl.uhfbw8 = 0;
  850. type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
  851. type |= F8MHZ;
  852. break;
  853. case BANDWIDTH_6_MHZ:
  854. type |= DTV6;
  855. priv->ctrl.vhfbw7 = 0;
  856. priv->ctrl.uhfbw8 = 0;
  857. break;
  858. default:
  859. tuner_err("error: bandwidth not supported.\n");
  860. };
  861. /* All S-code tables need a 200kHz shift */
  862. if (priv->ctrl.demod)
  863. demod = priv->ctrl.demod + 200;
  864. return generic_set_freq(fe, p->frequency,
  865. T_DIGITAL_TV, type, 0, demod);
  866. }
  867. static int xc2028_sleep(struct dvb_frontend *fe)
  868. {
  869. struct xc2028_data *priv = fe->tuner_priv;
  870. int rc = 0;
  871. tuner_dbg("%s called\n", __FUNCTION__);
  872. mutex_lock(&priv->lock);
  873. if (priv->firm_version < 0x0202)
  874. rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
  875. else
  876. rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
  877. priv->cur_fw.type = 0; /* need firmware reload */
  878. mutex_unlock(&priv->lock);
  879. return rc;
  880. }
  881. static int xc2028_dvb_release(struct dvb_frontend *fe)
  882. {
  883. struct xc2028_data *priv = fe->tuner_priv;
  884. tuner_dbg("%s called\n", __FUNCTION__);
  885. mutex_lock(&xc2028_list_mutex);
  886. priv->count--;
  887. if (!priv->count) {
  888. list_del(&priv->xc2028_list);
  889. kfree(priv->ctrl.fname);
  890. free_firmware(priv);
  891. kfree(priv);
  892. fe->tuner_priv = NULL;
  893. }
  894. mutex_unlock(&xc2028_list_mutex);
  895. return 0;
  896. }
  897. static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  898. {
  899. struct xc2028_data *priv = fe->tuner_priv;
  900. tuner_dbg("%s called\n", __FUNCTION__);
  901. *frequency = priv->frequency;
  902. return 0;
  903. }
  904. static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
  905. {
  906. struct xc2028_data *priv = fe->tuner_priv;
  907. struct xc2028_ctrl *p = priv_cfg;
  908. int rc = 0;
  909. tuner_dbg("%s called\n", __FUNCTION__);
  910. mutex_lock(&priv->lock);
  911. kfree(priv->ctrl.fname);
  912. free_firmware(priv);
  913. memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
  914. priv->ctrl.fname = NULL;
  915. if (p->fname) {
  916. priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
  917. if (priv->ctrl.fname == NULL)
  918. rc = -ENOMEM;
  919. }
  920. if (priv->ctrl.max_len < 9)
  921. priv->ctrl.max_len = 13;
  922. mutex_unlock(&priv->lock);
  923. return rc;
  924. }
  925. static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
  926. .info = {
  927. .name = "Xceive XC3028",
  928. .frequency_min = 42000000,
  929. .frequency_max = 864000000,
  930. .frequency_step = 50000,
  931. },
  932. .set_config = xc2028_set_config,
  933. .set_analog_params = xc2028_set_analog_freq,
  934. .release = xc2028_dvb_release,
  935. .get_frequency = xc2028_get_frequency,
  936. .get_rf_strength = xc2028_signal,
  937. .set_params = xc2028_set_params,
  938. .sleep = xc2028_sleep,
  939. };
  940. struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
  941. struct xc2028_config *cfg)
  942. {
  943. struct xc2028_data *priv;
  944. void *video_dev;
  945. if (debug)
  946. printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
  947. if (NULL == cfg || NULL == cfg->video_dev)
  948. return NULL;
  949. if (!fe) {
  950. printk(KERN_ERR PREFIX ": No frontend!\n");
  951. return NULL;
  952. }
  953. video_dev = cfg->video_dev;
  954. mutex_lock(&xc2028_list_mutex);
  955. list_for_each_entry(priv, &xc2028_list, xc2028_list) {
  956. if (priv->video_dev == cfg->video_dev) {
  957. video_dev = NULL;
  958. break;
  959. }
  960. }
  961. if (video_dev) {
  962. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  963. if (priv == NULL) {
  964. mutex_unlock(&xc2028_list_mutex);
  965. return NULL;
  966. }
  967. priv->i2c_props.addr = cfg->i2c_addr;
  968. priv->i2c_props.adap = cfg->i2c_adap;
  969. priv->video_dev = video_dev;
  970. priv->tuner_callback = cfg->callback;
  971. priv->ctrl.max_len = 13;
  972. mutex_init(&priv->lock);
  973. list_add_tail(&priv->xc2028_list, &xc2028_list);
  974. }
  975. fe->tuner_priv = priv;
  976. priv->count++;
  977. memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
  978. sizeof(xc2028_dvb_tuner_ops));
  979. tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
  980. if (cfg->ctrl)
  981. xc2028_set_config(fe, cfg->ctrl);
  982. mutex_unlock(&xc2028_list_mutex);
  983. return fe;
  984. }
  985. EXPORT_SYMBOL(xc2028_attach);
  986. MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
  987. MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
  988. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  989. MODULE_LICENSE("GPL");