tuner-xc2028.c 28 KB

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