tuner-xc2028.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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. if (priv->ctrl.read_not_reliable)
  680. goto read_not_reliable;
  681. /* Check firmware version against what we downloaded. */
  682. if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
  683. if (!priv->ctrl.read_not_reliable) {
  684. tuner_err("Incorrect readback of firmware version.\n");
  685. goto fail;
  686. } else {
  687. tuner_err("Returned an incorrect version. However, "
  688. "read is not reliable enough. Ignoring it.\n");
  689. hwmodel = 3028;
  690. }
  691. }
  692. /* Check that the tuner hardware model remains consistent over time. */
  693. if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
  694. priv->hwmodel = hwmodel;
  695. priv->hwvers = version & 0xff00;
  696. } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
  697. priv->hwvers != (version & 0xff00)) {
  698. tuner_err("Read invalid device hardware information - tuner "
  699. "hung?\n");
  700. goto fail;
  701. }
  702. read_not_reliable:
  703. memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
  704. /*
  705. * By setting BASE in cur_fw.type only after successfully loading all
  706. * firmwares, we can:
  707. * 1. Identify that BASE firmware with type=0 has been loaded;
  708. * 2. Tell whether BASE firmware was just changed the next time through.
  709. */
  710. priv->cur_fw.type |= BASE;
  711. return 0;
  712. fail:
  713. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  714. if (!is_retry) {
  715. msleep(50);
  716. is_retry = 1;
  717. tuner_dbg("Retrying firmware load\n");
  718. goto retry;
  719. }
  720. if (rc == -ENOENT)
  721. rc = -EINVAL;
  722. return rc;
  723. }
  724. static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
  725. {
  726. struct xc2028_data *priv = fe->tuner_priv;
  727. u16 frq_lock, signal = 0;
  728. int rc;
  729. tuner_dbg("%s called\n", __func__);
  730. mutex_lock(&priv->lock);
  731. /* Sync Lock Indicator */
  732. rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
  733. if (rc < 0)
  734. goto ret;
  735. /* Frequency is locked */
  736. if (frq_lock == 1)
  737. signal = 32768;
  738. /* Get SNR of the video signal */
  739. rc = xc2028_get_reg(priv, 0x0040, &signal);
  740. if (rc < 0)
  741. goto ret;
  742. /* Use both frq_lock and signal to generate the result */
  743. signal = signal || ((signal & 0x07) << 12);
  744. ret:
  745. mutex_unlock(&priv->lock);
  746. *strength = signal;
  747. tuner_dbg("signal strength is %d\n", signal);
  748. return rc;
  749. }
  750. #define DIV 15625
  751. static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
  752. enum tuner_mode new_mode,
  753. unsigned int type,
  754. v4l2_std_id std,
  755. u16 int_freq)
  756. {
  757. struct xc2028_data *priv = fe->tuner_priv;
  758. int rc = -EINVAL;
  759. unsigned char buf[4];
  760. u32 div, offset = 0;
  761. tuner_dbg("%s called\n", __func__);
  762. mutex_lock(&priv->lock);
  763. tuner_dbg("should set frequency %d kHz\n", freq / 1000);
  764. if (check_firmware(fe, type, std, int_freq) < 0)
  765. goto ret;
  766. /* On some cases xc2028 can disable video output, if
  767. * very weak signals are received. By sending a soft
  768. * reset, this is re-enabled. So, it is better to always
  769. * send a soft reset before changing channels, to be sure
  770. * that xc2028 will be in a safe state.
  771. * Maybe this might also be needed for DTV.
  772. */
  773. if (new_mode == T_ANALOG_TV) {
  774. rc = send_seq(priv, {0x00, 0x00});
  775. /* Analog modes require offset = 0 */
  776. } else {
  777. /*
  778. * Digital modes require an offset to adjust to the
  779. * proper frequency. The offset depends on what
  780. * firmware version is used.
  781. */
  782. /*
  783. * Adjust to the center frequency. This is calculated by the
  784. * formula: offset = 1.25MHz - BW/2
  785. * For DTV 7/8, the firmware uses BW = 8000, so it needs a
  786. * further adjustment to get the frequency center on VHF
  787. */
  788. if (priv->cur_fw.type & DTV6)
  789. offset = 1750000;
  790. else if (priv->cur_fw.type & DTV7)
  791. offset = 2250000;
  792. else /* DTV8 or DTV78 */
  793. offset = 2750000;
  794. if ((priv->cur_fw.type & DTV78) && freq < 470000000)
  795. offset -= 500000;
  796. /*
  797. * xc3028 additional "magic"
  798. * Depending on the firmware version, it needs some adjustments
  799. * to properly centralize the frequency. This seems to be
  800. * needed to compensate the SCODE table adjustments made by
  801. * newer firmwares
  802. */
  803. #if 1
  804. /*
  805. * The proper adjustment would be to do it at s-code table.
  806. * However, this didn't work, as reported by
  807. * Robert Lowery <rglowery@exemail.com.au>
  808. */
  809. if (priv->cur_fw.type & DTV7)
  810. offset += 500000;
  811. #else
  812. /*
  813. * Still need tests for XC3028L (firmware 3.2 or upper)
  814. * So, for now, let's just comment the per-firmware
  815. * version of this change. Reports with xc3028l working
  816. * with and without the lines bellow are welcome
  817. */
  818. if (priv->firm_version < 0x0302) {
  819. if (priv->cur_fw.type & DTV7)
  820. offset += 500000;
  821. } else {
  822. if (priv->cur_fw.type & DTV7)
  823. offset -= 300000;
  824. else if (type != ATSC) /* DVB @6MHz, DTV 8 and DTV 7/8 */
  825. offset += 200000;
  826. }
  827. #endif
  828. }
  829. div = (freq - offset + DIV / 2) / DIV;
  830. /* CMD= Set frequency */
  831. if (priv->firm_version < 0x0202)
  832. rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
  833. else
  834. rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
  835. if (rc < 0)
  836. goto ret;
  837. /* Return code shouldn't be checked.
  838. The reset CLK is needed only with tm6000.
  839. Driver should work fine even if this fails.
  840. */
  841. msleep(priv->ctrl.msleep);
  842. do_tuner_callback(fe, XC2028_RESET_CLK, 1);
  843. msleep(10);
  844. buf[0] = 0xff & (div >> 24);
  845. buf[1] = 0xff & (div >> 16);
  846. buf[2] = 0xff & (div >> 8);
  847. buf[3] = 0xff & (div);
  848. rc = i2c_send(priv, buf, sizeof(buf));
  849. if (rc < 0)
  850. goto ret;
  851. msleep(100);
  852. priv->frequency = freq;
  853. tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
  854. buf[0], buf[1], buf[2], buf[3],
  855. freq / 1000000, (freq % 1000000) / 1000);
  856. rc = 0;
  857. ret:
  858. mutex_unlock(&priv->lock);
  859. return rc;
  860. }
  861. static int xc2028_set_analog_freq(struct dvb_frontend *fe,
  862. struct analog_parameters *p)
  863. {
  864. struct xc2028_data *priv = fe->tuner_priv;
  865. unsigned int type=0;
  866. tuner_dbg("%s called\n", __func__);
  867. if (p->mode == V4L2_TUNER_RADIO) {
  868. type |= FM;
  869. if (priv->ctrl.input1)
  870. type |= INPUT1;
  871. return generic_set_freq(fe, (625l * p->frequency) / 10,
  872. T_RADIO, type, 0, 0);
  873. }
  874. /* if std is not defined, choose one */
  875. if (!p->std)
  876. p->std = V4L2_STD_MN;
  877. /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
  878. if (!(p->std & V4L2_STD_MN))
  879. type |= F8MHZ;
  880. /* Add audio hack to std mask */
  881. p->std |= parse_audio_std_option();
  882. return generic_set_freq(fe, 62500l * p->frequency,
  883. T_ANALOG_TV, type, p->std, 0);
  884. }
  885. static int xc2028_set_params(struct dvb_frontend *fe,
  886. struct dvb_frontend_parameters *p)
  887. {
  888. struct xc2028_data *priv = fe->tuner_priv;
  889. unsigned int type=0;
  890. fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
  891. u16 demod = 0;
  892. tuner_dbg("%s called\n", __func__);
  893. switch(fe->ops.info.type) {
  894. case FE_OFDM:
  895. bw = p->u.ofdm.bandwidth;
  896. /*
  897. * The only countries with 6MHz seem to be Taiwan/Uruguay.
  898. * Both seem to require QAM firmware for OFDM decoding
  899. * Tested in Taiwan by Terry Wu <terrywu2009@gmail.com>
  900. */
  901. if (bw == BANDWIDTH_6_MHZ)
  902. type |= QAM;
  903. break;
  904. case FE_ATSC:
  905. bw = BANDWIDTH_6_MHZ;
  906. /* The only ATSC firmware (at least on v2.7) is D2633 */
  907. type |= ATSC | D2633;
  908. break;
  909. /* DVB-S and pure QAM (FE_QAM) are not supported */
  910. default:
  911. return -EINVAL;
  912. }
  913. switch (bw) {
  914. case BANDWIDTH_8_MHZ:
  915. if (p->frequency < 470000000)
  916. priv->ctrl.vhfbw7 = 0;
  917. else
  918. priv->ctrl.uhfbw8 = 1;
  919. type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
  920. type |= F8MHZ;
  921. break;
  922. case BANDWIDTH_7_MHZ:
  923. if (p->frequency < 470000000)
  924. priv->ctrl.vhfbw7 = 1;
  925. else
  926. priv->ctrl.uhfbw8 = 0;
  927. type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
  928. type |= F8MHZ;
  929. break;
  930. case BANDWIDTH_6_MHZ:
  931. type |= DTV6;
  932. priv->ctrl.vhfbw7 = 0;
  933. priv->ctrl.uhfbw8 = 0;
  934. break;
  935. default:
  936. tuner_err("error: bandwidth not supported.\n");
  937. };
  938. /*
  939. Selects between D2633 or D2620 firmware.
  940. It doesn't make sense for ATSC, since it should be D2633 on all cases
  941. */
  942. if (fe->ops.info.type != FE_ATSC) {
  943. switch (priv->ctrl.type) {
  944. case XC2028_D2633:
  945. type |= D2633;
  946. break;
  947. case XC2028_D2620:
  948. type |= D2620;
  949. break;
  950. case XC2028_AUTO:
  951. default:
  952. /* Zarlink seems to need D2633 */
  953. if (priv->ctrl.demod == XC3028_FE_ZARLINK456)
  954. type |= D2633;
  955. else
  956. type |= D2620;
  957. }
  958. }
  959. /* All S-code tables need a 200kHz shift */
  960. if (priv->ctrl.demod) {
  961. demod = priv->ctrl.demod;
  962. /*
  963. * Newer firmwares require a 200 kHz offset only for ATSC
  964. */
  965. if (type == ATSC || priv->firm_version < 0x0302)
  966. demod += 200;
  967. /*
  968. * The DTV7 S-code table needs a 700 kHz shift.
  969. *
  970. * DTV7 is only used in Australia. Germany or Italy may also
  971. * use this firmware after initialization, but a tune to a UHF
  972. * channel should then cause DTV78 to be used.
  973. *
  974. * Unfortunately, on real-field tests, the s-code offset
  975. * didn't work as expected, as reported by
  976. * Robert Lowery <rglowery@exemail.com.au>
  977. */
  978. }
  979. return generic_set_freq(fe, p->frequency,
  980. T_DIGITAL_TV, type, 0, demod);
  981. }
  982. static int xc2028_sleep(struct dvb_frontend *fe)
  983. {
  984. struct xc2028_data *priv = fe->tuner_priv;
  985. int rc = 0;
  986. /* Avoid firmware reload on slow devices or if PM disabled */
  987. if (no_poweroff || priv->ctrl.disable_power_mgmt)
  988. return 0;
  989. tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");
  990. if (debug > 1) {
  991. tuner_dbg("Printing sleep stack trace:\n");
  992. dump_stack();
  993. }
  994. mutex_lock(&priv->lock);
  995. if (priv->firm_version < 0x0202)
  996. rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
  997. else
  998. rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
  999. priv->cur_fw.type = 0; /* need firmware reload */
  1000. mutex_unlock(&priv->lock);
  1001. return rc;
  1002. }
  1003. static int xc2028_dvb_release(struct dvb_frontend *fe)
  1004. {
  1005. struct xc2028_data *priv = fe->tuner_priv;
  1006. tuner_dbg("%s called\n", __func__);
  1007. mutex_lock(&xc2028_list_mutex);
  1008. /* only perform final cleanup if this is the last instance */
  1009. if (hybrid_tuner_report_instance_count(priv) == 1) {
  1010. kfree(priv->ctrl.fname);
  1011. free_firmware(priv);
  1012. }
  1013. if (priv)
  1014. hybrid_tuner_release_state(priv);
  1015. mutex_unlock(&xc2028_list_mutex);
  1016. fe->tuner_priv = NULL;
  1017. return 0;
  1018. }
  1019. static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  1020. {
  1021. struct xc2028_data *priv = fe->tuner_priv;
  1022. tuner_dbg("%s called\n", __func__);
  1023. *frequency = priv->frequency;
  1024. return 0;
  1025. }
  1026. static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
  1027. {
  1028. struct xc2028_data *priv = fe->tuner_priv;
  1029. struct xc2028_ctrl *p = priv_cfg;
  1030. int rc = 0;
  1031. tuner_dbg("%s called\n", __func__);
  1032. mutex_lock(&priv->lock);
  1033. memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
  1034. if (priv->ctrl.max_len < 9)
  1035. priv->ctrl.max_len = 13;
  1036. if (p->fname) {
  1037. if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
  1038. kfree(priv->ctrl.fname);
  1039. free_firmware(priv);
  1040. }
  1041. priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
  1042. if (priv->ctrl.fname == NULL)
  1043. rc = -ENOMEM;
  1044. }
  1045. mutex_unlock(&priv->lock);
  1046. return rc;
  1047. }
  1048. static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
  1049. .info = {
  1050. .name = "Xceive XC3028",
  1051. .frequency_min = 42000000,
  1052. .frequency_max = 864000000,
  1053. .frequency_step = 50000,
  1054. },
  1055. .set_config = xc2028_set_config,
  1056. .set_analog_params = xc2028_set_analog_freq,
  1057. .release = xc2028_dvb_release,
  1058. .get_frequency = xc2028_get_frequency,
  1059. .get_rf_strength = xc2028_signal,
  1060. .set_params = xc2028_set_params,
  1061. .sleep = xc2028_sleep,
  1062. };
  1063. struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
  1064. struct xc2028_config *cfg)
  1065. {
  1066. struct xc2028_data *priv;
  1067. int instance;
  1068. if (debug)
  1069. printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
  1070. if (NULL == cfg)
  1071. return NULL;
  1072. if (!fe) {
  1073. printk(KERN_ERR "xc2028: No frontend!\n");
  1074. return NULL;
  1075. }
  1076. mutex_lock(&xc2028_list_mutex);
  1077. instance = hybrid_tuner_request_state(struct xc2028_data, priv,
  1078. hybrid_tuner_instance_list,
  1079. cfg->i2c_adap, cfg->i2c_addr,
  1080. "xc2028");
  1081. switch (instance) {
  1082. case 0:
  1083. /* memory allocation failure */
  1084. goto fail;
  1085. break;
  1086. case 1:
  1087. /* new tuner instance */
  1088. priv->ctrl.max_len = 13;
  1089. mutex_init(&priv->lock);
  1090. fe->tuner_priv = priv;
  1091. break;
  1092. case 2:
  1093. /* existing tuner instance */
  1094. fe->tuner_priv = priv;
  1095. break;
  1096. }
  1097. memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
  1098. sizeof(xc2028_dvb_tuner_ops));
  1099. tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
  1100. if (cfg->ctrl)
  1101. xc2028_set_config(fe, cfg->ctrl);
  1102. mutex_unlock(&xc2028_list_mutex);
  1103. return fe;
  1104. fail:
  1105. mutex_unlock(&xc2028_list_mutex);
  1106. xc2028_dvb_release(fe);
  1107. return NULL;
  1108. }
  1109. EXPORT_SYMBOL(xc2028_attach);
  1110. MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
  1111. MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
  1112. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  1113. MODULE_LICENSE("GPL");