tuner-xc2028.c 27 KB

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