tuner-xc2028.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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(no_poweroff, "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[30];
  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.parent);
  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. /*
  764. * Digital modes require an offset to adjust to the
  765. * proper frequency.
  766. * Analog modes require offset = 0
  767. */
  768. if (new_mode == T_DIGITAL_TV) {
  769. /* Sets the offset according with firmware */
  770. if (priv->cur_fw.type & DTV6)
  771. offset = 1750000;
  772. else if (priv->cur_fw.type & DTV7)
  773. offset = 2250000;
  774. else /* DTV8 or DTV78 */
  775. offset = 2750000;
  776. /*
  777. * We must adjust the offset by 500kHz when
  778. * tuning a 7MHz VHF channel with DTV78 firmware
  779. * (used in Australia, Italy and Germany)
  780. */
  781. if ((priv->cur_fw.type & DTV78) && freq < 470000000)
  782. offset -= 500000;
  783. }
  784. div = (freq - offset + DIV / 2) / DIV;
  785. /* CMD= Set frequency */
  786. if (priv->firm_version < 0x0202)
  787. rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
  788. else
  789. rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
  790. if (rc < 0)
  791. goto ret;
  792. /* Return code shouldn't be checked.
  793. The reset CLK is needed only with tm6000.
  794. Driver should work fine even if this fails.
  795. */
  796. do_tuner_callback(fe, XC2028_RESET_CLK, 1);
  797. msleep(10);
  798. buf[0] = 0xff & (div >> 24);
  799. buf[1] = 0xff & (div >> 16);
  800. buf[2] = 0xff & (div >> 8);
  801. buf[3] = 0xff & (div);
  802. rc = i2c_send(priv, buf, sizeof(buf));
  803. if (rc < 0)
  804. goto ret;
  805. msleep(100);
  806. priv->frequency = freq;
  807. tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
  808. buf[0], buf[1], buf[2], buf[3],
  809. freq / 1000000, (freq % 1000000) / 1000);
  810. rc = 0;
  811. ret:
  812. mutex_unlock(&priv->lock);
  813. return rc;
  814. }
  815. static int xc2028_set_analog_freq(struct dvb_frontend *fe,
  816. struct analog_parameters *p)
  817. {
  818. struct xc2028_data *priv = fe->tuner_priv;
  819. unsigned int type=0;
  820. tuner_dbg("%s called\n", __func__);
  821. if (p->mode == V4L2_TUNER_RADIO) {
  822. type |= FM;
  823. if (priv->ctrl.input1)
  824. type |= INPUT1;
  825. return generic_set_freq(fe, (625l * p->frequency) / 10,
  826. T_RADIO, type, 0, 0);
  827. }
  828. /* if std is not defined, choose one */
  829. if (!p->std)
  830. p->std = V4L2_STD_MN;
  831. /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
  832. if (!(p->std & V4L2_STD_MN))
  833. type |= F8MHZ;
  834. /* Add audio hack to std mask */
  835. p->std |= parse_audio_std_option();
  836. return generic_set_freq(fe, 62500l * p->frequency,
  837. T_ANALOG_TV, type, p->std, 0);
  838. }
  839. static int xc2028_set_params(struct dvb_frontend *fe,
  840. struct dvb_frontend_parameters *p)
  841. {
  842. struct xc2028_data *priv = fe->tuner_priv;
  843. unsigned int type=0;
  844. fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
  845. u16 demod = 0;
  846. tuner_dbg("%s called\n", __func__);
  847. switch(fe->ops.info.type) {
  848. case FE_OFDM:
  849. bw = p->u.ofdm.bandwidth;
  850. /*
  851. * The only countries with 6MHz seem to be Taiwan/Uruguay.
  852. * Both seem to require QAM firmware for OFDM decoding
  853. * Tested in Taiwan by Terry Wu <terrywu2009@gmail.com>
  854. */
  855. if (bw == BANDWIDTH_6_MHZ)
  856. type |= QAM;
  857. break;
  858. case FE_ATSC:
  859. bw = BANDWIDTH_6_MHZ;
  860. /* The only ATSC firmware (at least on v2.7) is D2633 */
  861. type |= ATSC | D2633;
  862. break;
  863. /* DVB-S and pure QAM (FE_QAM) are not supported */
  864. default:
  865. return -EINVAL;
  866. }
  867. switch (bw) {
  868. case BANDWIDTH_8_MHZ:
  869. if (p->frequency < 470000000)
  870. priv->ctrl.vhfbw7 = 0;
  871. else
  872. priv->ctrl.uhfbw8 = 1;
  873. type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
  874. type |= F8MHZ;
  875. break;
  876. case BANDWIDTH_7_MHZ:
  877. if (p->frequency < 470000000)
  878. priv->ctrl.vhfbw7 = 1;
  879. else
  880. priv->ctrl.uhfbw8 = 0;
  881. type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
  882. type |= F8MHZ;
  883. break;
  884. case BANDWIDTH_6_MHZ:
  885. type |= DTV6;
  886. priv->ctrl.vhfbw7 = 0;
  887. priv->ctrl.uhfbw8 = 0;
  888. break;
  889. default:
  890. tuner_err("error: bandwidth not supported.\n");
  891. };
  892. /*
  893. Selects between D2633 or D2620 firmware.
  894. It doesn't make sense for ATSC, since it should be D2633 on all cases
  895. */
  896. if (fe->ops.info.type != FE_ATSC) {
  897. switch (priv->ctrl.type) {
  898. case XC2028_D2633:
  899. type |= D2633;
  900. break;
  901. case XC2028_D2620:
  902. type |= D2620;
  903. break;
  904. case XC2028_AUTO:
  905. default:
  906. /* Zarlink seems to need D2633 */
  907. if (priv->ctrl.demod == XC3028_FE_ZARLINK456)
  908. type |= D2633;
  909. else
  910. type |= D2620;
  911. }
  912. }
  913. /* All S-code tables need a 200kHz shift */
  914. if (priv->ctrl.demod) {
  915. demod = priv->ctrl.demod + 200;
  916. /*
  917. * The DTV7 S-code table needs a 700 kHz shift.
  918. * Thanks to Terry Wu <terrywu2009@gmail.com> for reporting this
  919. *
  920. * DTV7 is only used in Australia. Germany or Italy may also
  921. * use this firmware after initialization, but a tune to a UHF
  922. * channel should then cause DTV78 to be used.
  923. */
  924. if (type & DTV7)
  925. demod += 500;
  926. }
  927. return generic_set_freq(fe, p->frequency,
  928. T_DIGITAL_TV, type, 0, demod);
  929. }
  930. static int xc2028_sleep(struct dvb_frontend *fe)
  931. {
  932. struct xc2028_data *priv = fe->tuner_priv;
  933. int rc = 0;
  934. /* Avoid firmware reload on slow devices or if PM disabled */
  935. if (no_poweroff || priv->ctrl.disable_power_mgmt)
  936. return 0;
  937. tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");
  938. if (debug > 1) {
  939. tuner_dbg("Printing sleep stack trace:\n");
  940. dump_stack();
  941. }
  942. mutex_lock(&priv->lock);
  943. if (priv->firm_version < 0x0202)
  944. rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
  945. else
  946. rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
  947. priv->cur_fw.type = 0; /* need firmware reload */
  948. mutex_unlock(&priv->lock);
  949. return rc;
  950. }
  951. static int xc2028_dvb_release(struct dvb_frontend *fe)
  952. {
  953. struct xc2028_data *priv = fe->tuner_priv;
  954. tuner_dbg("%s called\n", __func__);
  955. mutex_lock(&xc2028_list_mutex);
  956. /* only perform final cleanup if this is the last instance */
  957. if (hybrid_tuner_report_instance_count(priv) == 1) {
  958. kfree(priv->ctrl.fname);
  959. free_firmware(priv);
  960. }
  961. if (priv)
  962. hybrid_tuner_release_state(priv);
  963. mutex_unlock(&xc2028_list_mutex);
  964. fe->tuner_priv = NULL;
  965. return 0;
  966. }
  967. static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  968. {
  969. struct xc2028_data *priv = fe->tuner_priv;
  970. tuner_dbg("%s called\n", __func__);
  971. *frequency = priv->frequency;
  972. return 0;
  973. }
  974. static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
  975. {
  976. struct xc2028_data *priv = fe->tuner_priv;
  977. struct xc2028_ctrl *p = priv_cfg;
  978. int rc = 0;
  979. tuner_dbg("%s called\n", __func__);
  980. mutex_lock(&priv->lock);
  981. memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
  982. if (priv->ctrl.max_len < 9)
  983. priv->ctrl.max_len = 13;
  984. if (p->fname) {
  985. if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
  986. kfree(priv->ctrl.fname);
  987. free_firmware(priv);
  988. }
  989. priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
  990. if (priv->ctrl.fname == NULL)
  991. rc = -ENOMEM;
  992. }
  993. mutex_unlock(&priv->lock);
  994. return rc;
  995. }
  996. static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
  997. .info = {
  998. .name = "Xceive XC3028",
  999. .frequency_min = 42000000,
  1000. .frequency_max = 864000000,
  1001. .frequency_step = 50000,
  1002. },
  1003. .set_config = xc2028_set_config,
  1004. .set_analog_params = xc2028_set_analog_freq,
  1005. .release = xc2028_dvb_release,
  1006. .get_frequency = xc2028_get_frequency,
  1007. .get_rf_strength = xc2028_signal,
  1008. .set_params = xc2028_set_params,
  1009. .sleep = xc2028_sleep,
  1010. };
  1011. struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
  1012. struct xc2028_config *cfg)
  1013. {
  1014. struct xc2028_data *priv;
  1015. int instance;
  1016. if (debug)
  1017. printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
  1018. if (NULL == cfg)
  1019. return NULL;
  1020. if (!fe) {
  1021. printk(KERN_ERR "xc2028: No frontend!\n");
  1022. return NULL;
  1023. }
  1024. mutex_lock(&xc2028_list_mutex);
  1025. instance = hybrid_tuner_request_state(struct xc2028_data, priv,
  1026. hybrid_tuner_instance_list,
  1027. cfg->i2c_adap, cfg->i2c_addr,
  1028. "xc2028");
  1029. switch (instance) {
  1030. case 0:
  1031. /* memory allocation failure */
  1032. goto fail;
  1033. break;
  1034. case 1:
  1035. /* new tuner instance */
  1036. priv->ctrl.max_len = 13;
  1037. mutex_init(&priv->lock);
  1038. fe->tuner_priv = priv;
  1039. break;
  1040. case 2:
  1041. /* existing tuner instance */
  1042. fe->tuner_priv = priv;
  1043. break;
  1044. }
  1045. memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
  1046. sizeof(xc2028_dvb_tuner_ops));
  1047. tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
  1048. if (cfg->ctrl)
  1049. xc2028_set_config(fe, cfg->ctrl);
  1050. mutex_unlock(&xc2028_list_mutex);
  1051. return fe;
  1052. fail:
  1053. mutex_unlock(&xc2028_list_mutex);
  1054. xc2028_dvb_release(fe);
  1055. return NULL;
  1056. }
  1057. EXPORT_SYMBOL(xc2028_attach);
  1058. MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
  1059. MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
  1060. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  1061. MODULE_LICENSE("GPL");