tuner-xc2028.c 31 KB

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