tuner-xc2028.c 25 KB

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