tuner-xc2028.c 29 KB

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