tuner-xc2028.c 31 KB

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