tuner-xc2028.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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 = type & DTV_TYPES;
  343. /* Seek for exact match */
  344. for (i = 0; i < priv->firm_size; i++) {
  345. if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
  346. goto found;
  347. }
  348. /* Seek for generic video standard match */
  349. for (i = 0; i < priv->firm_size; i++) {
  350. v4l2_std_id match_mask;
  351. int nr_matches;
  352. if (type != priv->firm[i].type)
  353. continue;
  354. match_mask = *id & priv->firm[i].id;
  355. if (!match_mask)
  356. continue;
  357. if ((*id & match_mask) == *id)
  358. goto found; /* Supports all the requested standards */
  359. nr_matches = hweight64(match_mask);
  360. if (nr_matches > best_nr_matches) {
  361. best_nr_matches = nr_matches;
  362. best_i = i;
  363. }
  364. }
  365. if (best_nr_matches > 0) {
  366. tuner_dbg("Selecting best matching firmware (%d bits) for "
  367. "type=", best_nr_matches);
  368. dump_firm_type(type);
  369. printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
  370. i = best_i;
  371. goto found;
  372. }
  373. /*FIXME: Would make sense to seek for type "hint" match ? */
  374. i = -ENOENT;
  375. goto ret;
  376. found:
  377. *id = priv->firm[i].id;
  378. ret:
  379. tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
  380. if (debug) {
  381. dump_firm_type(type);
  382. printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
  383. }
  384. return i;
  385. }
  386. static int load_firmware(struct dvb_frontend *fe, unsigned int type,
  387. v4l2_std_id *id)
  388. {
  389. struct xc2028_data *priv = fe->tuner_priv;
  390. int pos, rc;
  391. unsigned char *p, *endp, buf[priv->ctrl.max_len];
  392. tuner_dbg("%s called\n", __FUNCTION__);
  393. pos = seek_firmware(fe, type, id);
  394. if (pos < 0)
  395. return pos;
  396. tuner_info("Loading firmware for type=");
  397. dump_firm_type(priv->firm[pos].type);
  398. printk("(%x), id %016llx.\n", priv->firm[pos].type,
  399. (unsigned long long)*id);
  400. p = priv->firm[pos].ptr;
  401. endp = p + priv->firm[pos].size;
  402. while (p < endp) {
  403. __u16 size;
  404. /* Checks if there's enough bytes to read */
  405. if (p + sizeof(size) > endp) {
  406. tuner_err("Firmware chunk size is wrong\n");
  407. return -EINVAL;
  408. }
  409. size = le16_to_cpu(*(__u16 *) p);
  410. p += sizeof(size);
  411. if (size == 0xffff)
  412. return 0;
  413. if (!size) {
  414. /* Special callback command received */
  415. rc = priv->tuner_callback(priv->video_dev,
  416. XC2028_TUNER_RESET, 0);
  417. if (rc < 0) {
  418. tuner_err("Error at RESET code %d\n",
  419. (*p) & 0x7f);
  420. return -EINVAL;
  421. }
  422. continue;
  423. }
  424. if (size >= 0xff00) {
  425. switch (size) {
  426. case 0xff00:
  427. rc = priv->tuner_callback(priv->video_dev,
  428. XC2028_RESET_CLK, 0);
  429. if (rc < 0) {
  430. tuner_err("Error at RESET code %d\n",
  431. (*p) & 0x7f);
  432. return -EINVAL;
  433. }
  434. break;
  435. default:
  436. tuner_info("Invalid RESET code %d\n",
  437. size & 0x7f);
  438. return -EINVAL;
  439. }
  440. continue;
  441. }
  442. /* Checks for a sleep command */
  443. if (size & 0x8000) {
  444. msleep(size & 0x7fff);
  445. continue;
  446. }
  447. if ((size + p > endp)) {
  448. tuner_err("missing bytes: need %d, have %d\n",
  449. size, (int)(endp - p));
  450. return -EINVAL;
  451. }
  452. buf[0] = *p;
  453. p++;
  454. size--;
  455. /* Sends message chunks */
  456. while (size > 0) {
  457. int len = (size < priv->ctrl.max_len - 1) ?
  458. size : priv->ctrl.max_len - 1;
  459. memcpy(buf + 1, p, len);
  460. rc = i2c_send(priv, buf, len + 1);
  461. if (rc < 0) {
  462. tuner_err("%d returned from send\n", rc);
  463. return -EINVAL;
  464. }
  465. p += len;
  466. size -= len;
  467. }
  468. }
  469. return 0;
  470. }
  471. static int load_scode(struct dvb_frontend *fe, unsigned int type,
  472. v4l2_std_id *id, int scode)
  473. {
  474. struct xc2028_data *priv = fe->tuner_priv;
  475. int pos, rc;
  476. unsigned char *p;
  477. tuner_dbg("%s called\n", __FUNCTION__);
  478. pos = seek_firmware(fe, type, id);
  479. if (pos < 0)
  480. return pos;
  481. p = priv->firm[pos].ptr;
  482. /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
  483. * has a 2-byte size header in the firmware format. */
  484. if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
  485. le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
  486. return -EINVAL;
  487. tuner_info("Loading SCODE for type=");
  488. dump_firm_type(priv->firm[pos].type);
  489. printk("(%x), id %016llx.\n", priv->firm[pos].type,
  490. (unsigned long long)*id);
  491. if (priv->firm_version < 0x0202)
  492. rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
  493. else
  494. rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
  495. if (rc < 0)
  496. return -EIO;
  497. rc = i2c_send(priv, p + 14 * scode + 2, 12);
  498. if (rc < 0)
  499. return -EIO;
  500. rc = send_seq(priv, {0x00, 0x8c});
  501. if (rc < 0)
  502. return -EIO;
  503. return 0;
  504. }
  505. static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
  506. v4l2_std_id std, fe_bandwidth_t bandwidth)
  507. {
  508. struct xc2028_data *priv = fe->tuner_priv;
  509. int rc = 0, is_retry = 0;
  510. unsigned int type = 0;
  511. struct firmware_properties new_fw;
  512. u16 version, hwmodel;
  513. tuner_dbg("%s called\n", __FUNCTION__);
  514. if (!priv->firm) {
  515. if (!priv->ctrl.fname) {
  516. tuner_info("xc2028/3028 firmware name not set!\n");
  517. return -EINVAL;
  518. }
  519. rc = load_all_firmwares(fe);
  520. if (rc < 0)
  521. return rc;
  522. }
  523. if (priv->ctrl.type == XC2028_FIRM_MTS)
  524. type |= MTS;
  525. if (bandwidth == BANDWIDTH_7_MHZ || bandwidth == BANDWIDTH_8_MHZ)
  526. type |= F8MHZ;
  527. /* FIXME: How to load FM and FM|INPUT1 firmwares? */
  528. if (new_mode == T_DIGITAL_TV) {
  529. if (priv->ctrl.d2633)
  530. type |= D2633;
  531. else
  532. type |= D2620;
  533. switch (bandwidth) {
  534. case BANDWIDTH_8_MHZ:
  535. type |= DTV8;
  536. break;
  537. case BANDWIDTH_7_MHZ:
  538. type |= DTV7;
  539. break;
  540. case BANDWIDTH_6_MHZ:
  541. /* FIXME: Should allow select also ATSC */
  542. type |= DTV6 | QAM;
  543. break;
  544. default:
  545. tuner_err("error: bandwidth not supported.\n");
  546. };
  547. }
  548. retry:
  549. new_fw.type = type;
  550. new_fw.id = std;
  551. new_fw.std_req = std;
  552. new_fw.scode_table = SCODE | priv->ctrl.scode_table;
  553. new_fw.scode_nr = 0;
  554. tuner_dbg("checking firmware, user requested type=");
  555. if (debug) {
  556. dump_firm_type(new_fw.type);
  557. printk("(%x), id %016llx, scode_tbl ", new_fw.type,
  558. (unsigned long long)new_fw.std_req);
  559. dump_firm_type(priv->ctrl.scode_table);
  560. printk("(%x), scode_nr %d\n", priv->ctrl.scode_table,
  561. new_fw.scode_nr);
  562. }
  563. /* No need to reload base firmware if it matches */
  564. if (((BASE | new_fw.type) & BASE_TYPES) ==
  565. (priv->cur_fw.type & BASE_TYPES)) {
  566. tuner_dbg("BASE firmware not changed.\n");
  567. goto skip_base;
  568. }
  569. /* Updating BASE - forget about all currently loaded firmware */
  570. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  571. /* Reset is needed before loading firmware */
  572. rc = priv->tuner_callback(priv->video_dev,
  573. XC2028_TUNER_RESET, 0);
  574. if (rc < 0)
  575. goto fail;
  576. rc = load_firmware(fe, BASE | new_fw.type, &new_fw.id);
  577. if (rc < 0) {
  578. tuner_err("Error %d while loading base firmware\n",
  579. rc);
  580. goto fail;
  581. }
  582. /* Load INIT1, if needed */
  583. tuner_dbg("Load init1 firmware, if exists\n");
  584. rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &new_fw.id);
  585. if (rc < 0 && rc != -ENOENT) {
  586. tuner_err("Error %d while loading init1 firmware\n",
  587. rc);
  588. goto fail;
  589. }
  590. skip_base:
  591. /*
  592. * No need to reload standard specific firmware if base firmware
  593. * was not reloaded and requested video standards have not changed.
  594. */
  595. if (priv->cur_fw.type == (BASE | new_fw.type) &&
  596. priv->cur_fw.std_req == std) {
  597. tuner_dbg("Std-specific firmware already loaded.\n");
  598. goto skip_std_specific;
  599. }
  600. /* Reloading std-specific firmware forces a SCODE update */
  601. priv->cur_fw.scode_table = 0;
  602. /* Add audio hack to std mask */
  603. if (new_mode == T_ANALOG_TV)
  604. new_fw.id |= parse_audio_std_option();
  605. rc = load_firmware(fe, new_fw.type, &new_fw.id);
  606. if (rc < 0)
  607. goto fail;
  608. skip_std_specific:
  609. if (priv->cur_fw.scode_table == new_fw.scode_table &&
  610. priv->cur_fw.scode_nr == new_fw.scode_nr) {
  611. tuner_dbg("SCODE firmware already loaded.\n");
  612. goto check_device;
  613. }
  614. /* Load SCODE firmware, if exists */
  615. tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
  616. rc = load_scode(fe, new_fw.type | new_fw.scode_table,
  617. &new_fw.id, new_fw.scode_nr);
  618. check_device:
  619. if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
  620. xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
  621. tuner_err("Unable to read tuner registers.\n");
  622. goto fail;
  623. }
  624. tuner_info("Device is Xceive %d version %d.%d, "
  625. "firmware version %d.%d\n",
  626. hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
  627. (version & 0xf0) >> 4, version & 0xf);
  628. /* Check firmware version against what we downloaded. */
  629. if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
  630. tuner_err("Incorrect readback of firmware version.\n");
  631. goto fail;
  632. }
  633. /* Check that the tuner hardware model remains consistent over time. */
  634. if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
  635. priv->hwmodel = hwmodel;
  636. priv->hwvers = version & 0xff00;
  637. } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
  638. priv->hwvers != (version & 0xff00)) {
  639. tuner_err("Read invalid device hardware information - tuner "
  640. "hung?\n");
  641. goto fail;
  642. }
  643. memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
  644. /*
  645. * By setting BASE in cur_fw.type only after successfully loading all
  646. * firmwares, we can:
  647. * 1. Identify that BASE firmware with type=0 has been loaded;
  648. * 2. Tell whether BASE firmware was just changed the next time through.
  649. */
  650. priv->cur_fw.type |= BASE;
  651. return 0;
  652. fail:
  653. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  654. if (!is_retry) {
  655. msleep(50);
  656. is_retry = 1;
  657. tuner_dbg("Retrying firmware load\n");
  658. goto retry;
  659. }
  660. if (rc == -ENOENT)
  661. rc = -EINVAL;
  662. return rc;
  663. }
  664. static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
  665. {
  666. struct xc2028_data *priv = fe->tuner_priv;
  667. u16 frq_lock, signal = 0;
  668. int rc;
  669. tuner_dbg("%s called\n", __FUNCTION__);
  670. mutex_lock(&priv->lock);
  671. /* Sync Lock Indicator */
  672. rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
  673. if (rc < 0 || frq_lock == 0)
  674. goto ret;
  675. /* Frequency is locked. Return signal quality */
  676. /* Get SNR of the video signal */
  677. rc = xc2028_get_reg(priv, 0x0040, &signal);
  678. if (rc < 0)
  679. signal = -frq_lock;
  680. ret:
  681. mutex_unlock(&priv->lock);
  682. *strength = signal;
  683. return rc;
  684. }
  685. #define DIV 15625
  686. static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
  687. enum tuner_mode new_mode,
  688. v4l2_std_id std, fe_bandwidth_t bandwidth)
  689. {
  690. struct xc2028_data *priv = fe->tuner_priv;
  691. int rc = -EINVAL;
  692. unsigned char buf[4];
  693. u32 div, offset = 0;
  694. tuner_dbg("%s called\n", __FUNCTION__);
  695. mutex_lock(&priv->lock);
  696. /* HACK: It seems that specific firmware need to be reloaded
  697. when watching analog TV and freq is changed */
  698. if (new_mode != T_DIGITAL_TV)
  699. priv->cur_fw.type = 0;
  700. tuner_dbg("should set frequency %d kHz\n", freq / 1000);
  701. if (check_firmware(fe, new_mode, std, bandwidth) < 0)
  702. goto ret;
  703. if (new_mode == T_DIGITAL_TV) {
  704. offset = 2750000;
  705. if (priv->cur_fw.type & DTV7)
  706. offset -= 500000;
  707. }
  708. div = (freq - offset + DIV / 2) / DIV;
  709. /* CMD= Set frequency */
  710. if (priv->firm_version < 0x0202)
  711. rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
  712. else
  713. rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
  714. if (rc < 0)
  715. goto ret;
  716. rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
  717. if (rc < 0)
  718. goto ret;
  719. msleep(10);
  720. buf[0] = 0xff & (div >> 24);
  721. buf[1] = 0xff & (div >> 16);
  722. buf[2] = 0xff & (div >> 8);
  723. buf[3] = 0xff & (div);
  724. rc = i2c_send(priv, buf, sizeof(buf));
  725. if (rc < 0)
  726. goto ret;
  727. msleep(100);
  728. priv->frequency = freq;
  729. tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
  730. buf[0], buf[1], buf[2], buf[3],
  731. freq / 1000000, (freq % 1000000) / 1000);
  732. rc = 0;
  733. ret:
  734. mutex_unlock(&priv->lock);
  735. return rc;
  736. }
  737. static int xc2028_set_tv_freq(struct dvb_frontend *fe,
  738. struct analog_parameters *p)
  739. {
  740. struct xc2028_data *priv = fe->tuner_priv;
  741. tuner_dbg("%s called\n", __FUNCTION__);
  742. return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
  743. p->std, BANDWIDTH_8_MHZ);
  744. /* XXX Are some analog standards 6MHz? */
  745. }
  746. static int xc2028_set_params(struct dvb_frontend *fe,
  747. struct dvb_frontend_parameters *p)
  748. {
  749. struct xc2028_data *priv = fe->tuner_priv;
  750. tuner_dbg("%s called\n", __FUNCTION__);
  751. /* FIXME: Only OFDM implemented */
  752. if (fe->ops.info.type != FE_OFDM) {
  753. tuner_err("DTV type not implemented.\n");
  754. return -EINVAL;
  755. }
  756. return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
  757. 0 /* NOT USED */,
  758. p->u.ofdm.bandwidth);
  759. }
  760. static int xc2028_sleep(struct dvb_frontend *fe)
  761. {
  762. struct xc2028_data *priv = fe->tuner_priv;
  763. int rc = 0;
  764. tuner_dbg("%s called\n", __FUNCTION__);
  765. mutex_lock(&priv->lock);
  766. if (priv->firm_version < 0x0202)
  767. rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
  768. else
  769. rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
  770. priv->cur_fw.type = 0; /* need firmware reload */
  771. mutex_unlock(&priv->lock);
  772. return rc;
  773. }
  774. static int xc2028_dvb_release(struct dvb_frontend *fe)
  775. {
  776. struct xc2028_data *priv = fe->tuner_priv;
  777. tuner_dbg("%s called\n", __FUNCTION__);
  778. mutex_lock(&xc2028_list_mutex);
  779. priv->count--;
  780. if (!priv->count) {
  781. list_del(&priv->xc2028_list);
  782. kfree(priv->ctrl.fname);
  783. free_firmware(priv);
  784. kfree(priv);
  785. fe->tuner_priv = NULL;
  786. }
  787. mutex_unlock(&xc2028_list_mutex);
  788. return 0;
  789. }
  790. static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  791. {
  792. struct xc2028_data *priv = fe->tuner_priv;
  793. tuner_dbg("%s called\n", __FUNCTION__);
  794. *frequency = priv->frequency;
  795. return 0;
  796. }
  797. static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
  798. {
  799. struct xc2028_data *priv = fe->tuner_priv;
  800. struct xc2028_ctrl *p = priv_cfg;
  801. int rc = 0;
  802. tuner_dbg("%s called\n", __FUNCTION__);
  803. mutex_lock(&priv->lock);
  804. kfree(priv->ctrl.fname);
  805. free_firmware(priv);
  806. memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
  807. priv->ctrl.fname = NULL;
  808. if (p->fname) {
  809. priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
  810. if (priv->ctrl.fname == NULL)
  811. rc = -ENOMEM;
  812. }
  813. if (priv->ctrl.max_len < 9)
  814. priv->ctrl.max_len = 13;
  815. mutex_unlock(&priv->lock);
  816. return rc;
  817. }
  818. static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
  819. .info = {
  820. .name = "Xceive XC3028",
  821. .frequency_min = 42000000,
  822. .frequency_max = 864000000,
  823. .frequency_step = 50000,
  824. },
  825. .set_config = xc2028_set_config,
  826. .set_analog_params = xc2028_set_tv_freq,
  827. .release = xc2028_dvb_release,
  828. .get_frequency = xc2028_get_frequency,
  829. .get_rf_strength = xc2028_signal,
  830. .set_params = xc2028_set_params,
  831. .sleep = xc2028_sleep,
  832. };
  833. void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
  834. {
  835. struct xc2028_data *priv;
  836. void *video_dev;
  837. if (debug)
  838. printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
  839. if (NULL == cfg->video_dev)
  840. return NULL;
  841. if (!fe) {
  842. printk(KERN_ERR PREFIX ": No frontend!\n");
  843. return NULL;
  844. }
  845. video_dev = cfg->video_dev;
  846. mutex_lock(&xc2028_list_mutex);
  847. list_for_each_entry(priv, &xc2028_list, xc2028_list) {
  848. if (priv->video_dev == cfg->video_dev) {
  849. video_dev = NULL;
  850. break;
  851. }
  852. }
  853. if (video_dev) {
  854. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  855. if (priv == NULL) {
  856. mutex_unlock(&xc2028_list_mutex);
  857. return NULL;
  858. }
  859. priv->i2c_props.addr = cfg->i2c_addr;
  860. priv->i2c_props.adap = cfg->i2c_adap;
  861. priv->video_dev = video_dev;
  862. priv->tuner_callback = cfg->callback;
  863. priv->ctrl.max_len = 13;
  864. mutex_init(&priv->lock);
  865. list_add_tail(&priv->xc2028_list, &xc2028_list);
  866. }
  867. fe->tuner_priv = priv;
  868. priv->count++;
  869. memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
  870. sizeof(xc2028_dvb_tuner_ops));
  871. tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
  872. mutex_unlock(&xc2028_list_mutex);
  873. return fe;
  874. }
  875. EXPORT_SYMBOL(xc2028_attach);
  876. MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
  877. MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
  878. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  879. MODULE_LICENSE("GPL");