tuner-xc2028.c 26 KB

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