tuner-xc2028.c 28 KB

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