tuner-xc2028.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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. /* struct for storing firmware table */
  41. struct firmware_description {
  42. unsigned int type;
  43. v4l2_std_id id;
  44. unsigned char *ptr;
  45. unsigned int size;
  46. };
  47. struct xc2028_data {
  48. struct list_head xc2028_list;
  49. struct tuner_i2c_props i2c_props;
  50. int (*tuner_callback) (void *dev,
  51. int command, int arg);
  52. void *video_dev;
  53. int count;
  54. __u32 frequency;
  55. struct firmware_description *firm;
  56. int firm_size;
  57. __u16 version;
  58. struct xc2028_ctrl ctrl;
  59. v4l2_std_id firm_type; /* video stds supported
  60. by current firmware */
  61. fe_bandwidth_t bandwidth; /* Firmware bandwidth:
  62. 6M, 7M or 8M */
  63. int need_load_generic; /* The generic firmware
  64. were loaded? */
  65. int max_len; /* Max firmware chunk */
  66. enum tuner_mode mode;
  67. struct i2c_client *i2c_client;
  68. struct mutex lock;
  69. };
  70. #define i2c_send(rc, priv, buf, size) do { \
  71. rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
  72. if (size != rc) \
  73. tuner_err("i2c output error: rc = %d (should be %d)\n", \
  74. rc, (int)size); \
  75. } while (0)
  76. #define i2c_rcv(rc, priv, buf, size) do { \
  77. rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
  78. if (size != rc) \
  79. tuner_err("i2c input error: rc = %d (should be %d)\n", \
  80. rc, (int)size); \
  81. } while (0)
  82. #define send_seq(priv, data...) do { \
  83. int rc; \
  84. static u8 _val[] = data; \
  85. if (sizeof(_val) != \
  86. (rc = tuner_i2c_xfer_send(&priv->i2c_props, \
  87. _val, sizeof(_val)))) { \
  88. tuner_err("Error on line %d: %d\n", __LINE__, rc); \
  89. return -EINVAL; \
  90. } \
  91. msleep(10); \
  92. } while (0)
  93. static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg)
  94. {
  95. int rc;
  96. unsigned char buf[2];
  97. tuner_dbg("%s called\n", __FUNCTION__);
  98. buf[0] = reg>>8;
  99. buf[1] = (unsigned char) reg;
  100. i2c_send(rc, priv, buf, 2);
  101. if (rc < 0)
  102. return rc;
  103. i2c_rcv(rc, priv, buf, 2);
  104. if (rc < 0)
  105. return rc;
  106. return (buf[1]) | (buf[0] << 8);
  107. }
  108. void dump_firm_type(unsigned int type)
  109. {
  110. if (type & BASE)
  111. printk("BASE ");
  112. if (type & INIT1)
  113. printk("INIT1 ");
  114. if (type & F8MHZ)
  115. printk("F8MHZ ");
  116. if (type & MTS)
  117. printk("MTS ");
  118. if (type & D2620)
  119. printk("D2620 ");
  120. if (type & D2633)
  121. printk("D2633 ");
  122. if (type & DTV6)
  123. printk("DTV6 ");
  124. if (type & QAM)
  125. printk("QAM ");
  126. if (type & DTV7)
  127. printk("DTV7 ");
  128. if (type & DTV78)
  129. printk("DTV78 ");
  130. if (type & DTV8)
  131. printk("DTV8 ");
  132. if (type & FM)
  133. printk("FM ");
  134. if (type & INPUT1)
  135. printk("INPUT1 ");
  136. if (type & LCD)
  137. printk("LCD ");
  138. if (type & NOGD)
  139. printk("NOGD ");
  140. if (type & MONO)
  141. printk("MONO ");
  142. if (type & ATSC)
  143. printk("ATSC ");
  144. if (type & IF)
  145. printk("IF ");
  146. if (type & LG60)
  147. printk("LG60 ");
  148. if (type & ATI638)
  149. printk("ATI638 ");
  150. if (type & OREN538)
  151. printk("OREN538 ");
  152. if (type & OREN36)
  153. printk("OREN36 ");
  154. if (type & TOYOTA388)
  155. printk("TOYOTA388 ");
  156. if (type & TOYOTA794)
  157. printk("TOYOTA794 ");
  158. if (type & DIBCOM52)
  159. printk("DIBCOM52 ");
  160. if (type & ZARLINK456)
  161. printk("ZARLINK456 ");
  162. if (type & CHINA)
  163. printk("CHINA ");
  164. if (type & F6MHZ)
  165. printk("F6MHZ ");
  166. if (type & INPUT2)
  167. printk("INPUT2 ");
  168. if (type & SCODE)
  169. printk("SCODE ");
  170. }
  171. static v4l2_std_id parse_audio_std_option(void)
  172. {
  173. if (strcasecmp(audio_std, "A2"))
  174. return V4L2_STD_A2;
  175. if (strcasecmp(audio_std, "A2/A"))
  176. return V4L2_STD_A2_A;
  177. if (strcasecmp(audio_std, "A2/B"))
  178. return V4L2_STD_A2_B;
  179. if (strcasecmp(audio_std, "NICAM"))
  180. return V4L2_STD_NICAM;
  181. if (strcasecmp(audio_std, "NICAM/A"))
  182. return V4L2_STD_NICAM_A;
  183. if (strcasecmp(audio_std, "NICAM/B"))
  184. return V4L2_STD_NICAM_B;
  185. return 0;
  186. }
  187. static void free_firmware(struct xc2028_data *priv)
  188. {
  189. int i;
  190. if (!priv->firm)
  191. return;
  192. for (i = 0; i < priv->firm_size; i++)
  193. kfree(priv->firm[i].ptr);
  194. kfree(priv->firm);
  195. priv->firm = NULL;
  196. priv->need_load_generic = 1;
  197. }
  198. static int load_all_firmwares(struct dvb_frontend *fe)
  199. {
  200. struct xc2028_data *priv = fe->tuner_priv;
  201. const struct firmware *fw = NULL;
  202. unsigned char *p, *endp;
  203. int rc = 0;
  204. int n, n_array;
  205. char name[33];
  206. tuner_dbg("%s called\n", __FUNCTION__);
  207. tuner_info("Reading firmware %s\n", priv->ctrl.fname);
  208. rc = request_firmware(&fw, priv->ctrl.fname,
  209. &priv->i2c_props.adap->dev);
  210. if (rc < 0) {
  211. if (rc == -ENOENT)
  212. tuner_err("Error: firmware %s not found.\n",
  213. priv->ctrl.fname);
  214. else
  215. tuner_err("Error %d while requesting firmware %s \n",
  216. rc, priv->ctrl.fname);
  217. return rc;
  218. }
  219. p = fw->data;
  220. endp = p + fw->size;
  221. if (fw->size < sizeof(name) - 1 + 2) {
  222. tuner_err("Error: firmware size is zero!\n");
  223. rc = -EINVAL;
  224. goto done;
  225. }
  226. memcpy(name, p, sizeof(name) - 1);
  227. name[sizeof(name) - 1] = 0;
  228. p += sizeof(name) - 1;
  229. priv->version = le16_to_cpu(*(__u16 *) p);
  230. p += 2;
  231. tuner_info("Firmware: %s, ver %d.%d\n", name,
  232. priv->version >> 8, priv->version & 0xff);
  233. if (p + 2 > endp)
  234. goto corrupt;
  235. n_array = le16_to_cpu(*(__u16 *) p);
  236. p += 2;
  237. tuner_info("There are %d firmwares at %s\n",
  238. n_array, priv->ctrl.fname);
  239. priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
  240. if (!fw) {
  241. tuner_err("Not enough memory for reading firmware.\n");
  242. rc = -ENOMEM;
  243. goto done;
  244. }
  245. priv->firm_size = n_array;
  246. n = -1;
  247. while (p < endp) {
  248. __u32 type, size;
  249. v4l2_std_id id;
  250. n++;
  251. if (n >= n_array) {
  252. tuner_err("Too much firmwares at the file\n");
  253. goto corrupt;
  254. }
  255. /* Checks if there's enough bytes to read */
  256. if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
  257. tuner_err("Firmware header is incomplete!\n");
  258. goto corrupt;
  259. }
  260. type = le32_to_cpu(*(__u32 *) p);
  261. p += sizeof(type);
  262. id = le64_to_cpu(*(v4l2_std_id *) p);
  263. p += sizeof(id);
  264. size = le32_to_cpu(*(__u32 *) p);
  265. p += sizeof(size);
  266. if ((!size) || (size + p > endp)) {
  267. tuner_err("Firmware type ");
  268. dump_firm_type(type);
  269. printk("(%x), id %llx is corrupted "
  270. "(size=%d, expected %d)\n",
  271. type, id,
  272. (unsigned)(endp - p), size);
  273. goto corrupt;
  274. }
  275. priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
  276. if (!priv->firm[n].ptr) {
  277. tuner_err("Not enough memory.\n");
  278. rc = -ENOMEM;
  279. goto err;
  280. }
  281. tuner_info("Reading firmware type ");
  282. dump_firm_type(type);
  283. printk("(%x), id %lx, size=%d.\n",
  284. type, (unsigned long)id, size);
  285. memcpy(priv->firm[n].ptr, p, size);
  286. priv->firm[n].type = type;
  287. priv->firm[n].id = id;
  288. priv->firm[n].size = size;
  289. p += size;
  290. }
  291. if (n + 1 != priv->firm_size) {
  292. tuner_err("Firmware file is incomplete!\n");
  293. goto corrupt;
  294. }
  295. goto done;
  296. corrupt:
  297. rc = -EINVAL;
  298. tuner_err("Error: firmware file is corrupted!\n");
  299. err:
  300. tuner_info("Releasing loaded firmware file.\n");
  301. free_firmware(priv);
  302. done:
  303. release_firmware(fw);
  304. tuner_dbg("Firmware files loaded.\n");
  305. return rc;
  306. }
  307. static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
  308. v4l2_std_id *id)
  309. {
  310. struct xc2028_data *priv = fe->tuner_priv;
  311. int i;
  312. tuner_dbg("%s called\n", __FUNCTION__);
  313. if (!priv->firm) {
  314. tuner_err("Error! firmware not loaded\n");
  315. return -EINVAL;
  316. }
  317. if (((type & ~SCODE) == 0) && (*id == 0))
  318. *id = V4L2_STD_PAL;
  319. /* Seek for exact match */
  320. for (i = 0; i < priv->firm_size; i++) {
  321. if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
  322. goto found;
  323. }
  324. /* Seek for generic video standard match */
  325. for (i = 0; i < priv->firm_size; i++) {
  326. if ((type == priv->firm[i].type) && (*id & priv->firm[i].id))
  327. goto found;
  328. }
  329. /*FIXME: Would make sense to seek for type "hint" match ? */
  330. i = -EINVAL;
  331. goto ret;
  332. found:
  333. *id = priv->firm[i].id;
  334. ret:
  335. tuner_dbg("%s firmware for type=", (i < 0)? "Can't find": "Found");
  336. if (debug) {
  337. dump_firm_type(type);
  338. printk("(%x), id %08lx.\n", type, (unsigned long)*id);
  339. }
  340. return i;
  341. }
  342. static int load_firmware(struct dvb_frontend *fe, unsigned int type,
  343. v4l2_std_id *id)
  344. {
  345. struct xc2028_data *priv = fe->tuner_priv;
  346. int pos, rc;
  347. unsigned char *p, *endp, buf[priv->max_len];
  348. tuner_dbg("%s called\n", __FUNCTION__);
  349. pos = seek_firmware(fe, type, id);
  350. if (pos < 0)
  351. return pos;
  352. tuner_info("Loading firmware for type=");
  353. dump_firm_type(type);
  354. printk("(%x), id %08lx.\n", type, (unsigned long)*id);
  355. p = priv->firm[pos].ptr;
  356. if (!p) {
  357. tuner_err("Firmware pointer were freed!");
  358. return -EINVAL;
  359. }
  360. endp = p + priv->firm[pos].size;
  361. while (p < endp) {
  362. __u16 size;
  363. /* Checks if there's enough bytes to read */
  364. if (p + sizeof(size) > endp) {
  365. tuner_err("Firmware chunk size is wrong\n");
  366. return -EINVAL;
  367. }
  368. size = le16_to_cpu(*(__u16 *) p);
  369. p += sizeof(size);
  370. if (size == 0xffff)
  371. return 0;
  372. if (!size) {
  373. /* Special callback command received */
  374. rc = priv->tuner_callback(priv->video_dev,
  375. XC2028_TUNER_RESET, 0);
  376. if (rc < 0) {
  377. tuner_err("Error at RESET code %d\n",
  378. (*p) & 0x7f);
  379. return -EINVAL;
  380. }
  381. continue;
  382. }
  383. if (size >= 0xff00) {
  384. switch (size) {
  385. case 0xff00:
  386. rc = priv->tuner_callback(priv->video_dev,
  387. XC2028_RESET_CLK, 0);
  388. if (rc < 0) {
  389. tuner_err("Error at RESET code %d\n",
  390. (*p) & 0x7f);
  391. return -EINVAL;
  392. }
  393. default:
  394. tuner_info("Invalid RESET code %d\n",
  395. size & 0x7f);
  396. return -EINVAL;
  397. }
  398. continue;
  399. }
  400. /* Checks for a sleep command */
  401. if (size & 0x8000) {
  402. msleep(size & 0x7fff);
  403. continue;
  404. }
  405. if ((size + p > endp)) {
  406. tuner_err("missing bytes: need %d, have %d\n",
  407. size, (int)(endp - p));
  408. return -EINVAL;
  409. }
  410. buf[0] = *p;
  411. p++;
  412. size--;
  413. /* Sends message chunks */
  414. while (size > 0) {
  415. int len = (size < priv->max_len - 1) ?
  416. size : priv->max_len - 1;
  417. memcpy(buf + 1, p, len);
  418. i2c_send(rc, priv, buf, len + 1);
  419. if (rc < 0) {
  420. tuner_err("%d returned from send\n", rc);
  421. return -EINVAL;
  422. }
  423. p += len;
  424. size -= len;
  425. }
  426. }
  427. return 0;
  428. }
  429. static int load_scode(struct dvb_frontend *fe, unsigned int type,
  430. v4l2_std_id *id, int scode)
  431. {
  432. struct xc2028_data *priv = fe->tuner_priv;
  433. int pos, rc;
  434. unsigned char *p;
  435. tuner_dbg("%s called\n", __FUNCTION__);
  436. pos = seek_firmware(fe, type, id);
  437. if (pos < 0)
  438. return pos;
  439. p = priv->firm[pos].ptr;
  440. if (!p) {
  441. tuner_err("Firmware pointer were freed!");
  442. return -EINVAL;
  443. }
  444. if ((priv->firm[pos].size != 12 * 16) || (scode >= 16))
  445. return -EINVAL;
  446. if (priv->version < 0x0202) {
  447. send_seq(priv, {0x20, 0x00, 0x00, 0x00});
  448. } else {
  449. send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
  450. }
  451. i2c_send(rc, priv, p + 12 * scode, 12);
  452. send_seq(priv, {0x00, 0x8c});
  453. return 0;
  454. }
  455. static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
  456. v4l2_std_id std, fe_bandwidth_t bandwidth)
  457. {
  458. struct xc2028_data *priv = fe->tuner_priv;
  459. int rc, version, hwmodel;
  460. v4l2_std_id std0 = 0;
  461. unsigned int type0 = 0, type = 0;
  462. int change_digital_bandwidth;
  463. tuner_dbg("%s called\n", __FUNCTION__);
  464. if (!priv->firm) {
  465. if (!priv->ctrl.fname) {
  466. tuner_info("xc2028/3028 firmware name not set!\n");
  467. return -EINVAL;
  468. }
  469. rc = load_all_firmwares(fe);
  470. if (rc < 0)
  471. return rc;
  472. }
  473. tuner_dbg("I am in mode %u and I should switch to mode %i\n",
  474. priv->mode, new_mode);
  475. /* first of all, determine whether we have switched the mode */
  476. if (new_mode != priv->mode) {
  477. priv->mode = new_mode;
  478. priv->need_load_generic = 1;
  479. }
  480. change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
  481. && bandwidth != priv->bandwidth) ? 1 : 0;
  482. tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
  483. bandwidth);
  484. if (priv->need_load_generic) {
  485. /* Reset is needed before loading firmware */
  486. rc = priv->tuner_callback(priv->video_dev,
  487. XC2028_TUNER_RESET, 0);
  488. if (rc < 0)
  489. return rc;
  490. type0 = BASE;
  491. if (priv->ctrl.type == XC2028_FIRM_MTS)
  492. type0 |= MTS;
  493. if (priv->bandwidth == 8)
  494. type0 |= F8MHZ;
  495. /* FIXME: How to load FM and FM|INPUT1 firmwares? */
  496. rc = load_firmware(fe, type0, &std0);
  497. if (rc < 0) {
  498. tuner_err("Error %d while loading generic firmware\n",
  499. rc);
  500. return rc;
  501. }
  502. priv->need_load_generic = 0;
  503. priv->firm_type = 0;
  504. if (priv->mode == T_DIGITAL_TV)
  505. change_digital_bandwidth = 1;
  506. }
  507. tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
  508. if (change_digital_bandwidth) {
  509. /*FIXME: Should allow selecting between D2620 and D2633 */
  510. type |= D2620;
  511. /* FIXME: When should select a DTV78 firmware?
  512. */
  513. switch (bandwidth) {
  514. case BANDWIDTH_8_MHZ:
  515. type |= DTV8;
  516. break;
  517. case BANDWIDTH_7_MHZ:
  518. type |= DTV7;
  519. break;
  520. case BANDWIDTH_6_MHZ:
  521. /* FIXME: Should allow select also ATSC */
  522. type |= DTV6 | QAM;
  523. break;
  524. default:
  525. tuner_err("error: bandwidth not supported.\n");
  526. };
  527. priv->bandwidth = bandwidth;
  528. }
  529. if (!change_digital_bandwidth && priv->mode == T_DIGITAL_TV)
  530. return 0;
  531. /* Load INIT1, if needed */
  532. tuner_dbg("Load init1 firmware, if exists\n");
  533. type0 = BASE | INIT1;
  534. if (priv->ctrl.type == XC2028_FIRM_MTS)
  535. type0 |= MTS;
  536. /* FIXME: Should handle errors - if INIT1 found */
  537. rc = load_firmware(fe, type0, &std0);
  538. /* FIXME: Should add support for FM radio
  539. */
  540. if (priv->ctrl.type == XC2028_FIRM_MTS)
  541. type |= MTS;
  542. if (priv->firm_type & std) {
  543. tuner_dbg("Std-specific firmware already loaded.\n");
  544. return 0;
  545. }
  546. /* Add audio hack to std mask */
  547. std |= parse_audio_std_option();
  548. rc = load_firmware(fe, type, &std);
  549. if (rc < 0)
  550. return rc;
  551. /* Load SCODE firmware, if exists */
  552. tuner_dbg("Trying to load scode 0\n");
  553. type |= SCODE;
  554. rc = load_scode(fe, type, &std, 0);
  555. version = xc2028_get_reg(priv, 0x0004);
  556. hwmodel = xc2028_get_reg(priv, 0x0008);
  557. tuner_info("Device is Xceive %d version %d.%d, "
  558. "firmware version %d.%d\n",
  559. hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
  560. (version & 0xf0) >> 4, version & 0xf);
  561. priv->firm_type = std;
  562. return 0;
  563. }
  564. static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
  565. {
  566. struct xc2028_data *priv = fe->tuner_priv;
  567. int frq_lock, signal = 0;
  568. tuner_dbg("%s called\n", __FUNCTION__);
  569. mutex_lock(&priv->lock);
  570. *strength = 0;
  571. /* Sync Lock Indicator */
  572. frq_lock = xc2028_get_reg(priv, 0x0002);
  573. if (frq_lock <= 0)
  574. goto ret;
  575. /* Frequency is locked. Return signal quality */
  576. /* Get SNR of the video signal */
  577. signal = xc2028_get_reg(priv, 0x0040);
  578. if (signal <= 0)
  579. signal = frq_lock;
  580. ret:
  581. mutex_unlock(&priv->lock);
  582. *strength = signal;
  583. return 0;
  584. }
  585. #define DIV 15625
  586. static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
  587. enum tuner_mode new_mode,
  588. v4l2_std_id std, fe_bandwidth_t bandwidth)
  589. {
  590. struct xc2028_data *priv = fe->tuner_priv;
  591. int rc = -EINVAL;
  592. unsigned char buf[5];
  593. u32 div, offset = 0;
  594. tuner_dbg("%s called\n", __FUNCTION__);
  595. mutex_lock(&priv->lock);
  596. /* HACK: It seems that specific firmware need to be reloaded
  597. when freq is changed */
  598. priv->firm_type = 0;
  599. /* Reset GPIO 1 */
  600. rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
  601. if (rc < 0)
  602. goto ret;
  603. msleep(10);
  604. tuner_dbg("should set frequency %d kHz)\n", freq / 1000);
  605. if (check_firmware(fe, new_mode, std, bandwidth) < 0)
  606. goto ret;
  607. if (new_mode == T_DIGITAL_TV)
  608. offset = 2750000;
  609. div = (freq - offset + DIV / 2) / DIV;
  610. /* CMD= Set frequency */
  611. if (priv->version < 0x0202) {
  612. send_seq(priv, {0x00, 0x02, 0x00, 0x00});
  613. } else {
  614. send_seq(priv, {0x80, 0x02, 0x00, 0x00});
  615. }
  616. rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
  617. if (rc < 0)
  618. goto ret;
  619. msleep(10);
  620. buf[0] = 0xff & (div >> 24);
  621. buf[1] = 0xff & (div >> 16);
  622. buf[2] = 0xff & (div >> 8);
  623. buf[3] = 0xff & (div);
  624. buf[4] = 0;
  625. i2c_send(rc, priv, buf, sizeof(buf));
  626. if (rc < 0)
  627. goto ret;
  628. msleep(100);
  629. priv->frequency = freq;
  630. tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
  631. buf[1], buf[2], buf[3], buf[4],
  632. freq / 1000000, (freq % 1000000) / 10000);
  633. rc = 0;
  634. ret:
  635. mutex_unlock(&priv->lock);
  636. return rc;
  637. }
  638. static int xc2028_set_tv_freq(struct dvb_frontend *fe,
  639. struct analog_parameters *p)
  640. {
  641. struct xc2028_data *priv = fe->tuner_priv;
  642. tuner_dbg("%s called\n", __FUNCTION__);
  643. return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
  644. p->std, BANDWIDTH_8_MHZ /* NOT USED */);
  645. }
  646. static int xc2028_set_params(struct dvb_frontend *fe,
  647. struct dvb_frontend_parameters *p)
  648. {
  649. struct xc2028_data *priv = fe->tuner_priv;
  650. tuner_dbg("%s called\n", __FUNCTION__);
  651. /* FIXME: Only OFDM implemented */
  652. if (fe->ops.info.type != FE_OFDM) {
  653. tuner_err("DTV type not implemented.\n");
  654. return -EINVAL;
  655. }
  656. return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
  657. 0 /* NOT USED */,
  658. p->u.ofdm.bandwidth);
  659. }
  660. static int xc2028_dvb_release(struct dvb_frontend *fe)
  661. {
  662. struct xc2028_data *priv = fe->tuner_priv;
  663. tuner_dbg("%s called\n", __FUNCTION__);
  664. priv->count--;
  665. if (!priv->count) {
  666. list_del(&priv->xc2028_list);
  667. kfree(priv->ctrl.fname);
  668. free_firmware(priv);
  669. kfree(priv);
  670. }
  671. return 0;
  672. }
  673. static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  674. {
  675. struct xc2028_data *priv = fe->tuner_priv;
  676. tuner_dbg("%s called\n", __FUNCTION__);
  677. *frequency = priv->frequency;
  678. return 0;
  679. }
  680. static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
  681. {
  682. struct xc2028_data *priv = fe->tuner_priv;
  683. struct xc2028_ctrl *p = priv_cfg;
  684. tuner_dbg("%s called\n", __FUNCTION__);
  685. priv->ctrl.type = p->type;
  686. if (p->fname) {
  687. kfree(priv->ctrl.fname);
  688. priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL);
  689. if (!priv->ctrl.fname)
  690. return -ENOMEM;
  691. free_firmware(priv);
  692. strcpy(priv->ctrl.fname, p->fname);
  693. }
  694. if (p->max_len > 0)
  695. priv->max_len = p->max_len;
  696. return 0;
  697. }
  698. static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
  699. .info = {
  700. .name = "Xceive XC3028",
  701. .frequency_min = 42000000,
  702. .frequency_max = 864000000,
  703. .frequency_step = 50000,
  704. },
  705. .set_config = xc2028_set_config,
  706. .set_analog_params = xc2028_set_tv_freq,
  707. .release = xc2028_dvb_release,
  708. .get_frequency = xc2028_get_frequency,
  709. .get_rf_strength = xc2028_signal,
  710. .set_params = xc2028_set_params,
  711. };
  712. void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
  713. {
  714. struct xc2028_data *priv;
  715. void *video_dev;
  716. if (debug)
  717. printk(KERN_DEBUG PREFIX "Xcv2028/3028 init called!\n");
  718. if (NULL == cfg->video_dev)
  719. return NULL;
  720. if (!fe) {
  721. printk(KERN_ERR PREFIX "No frontend!\n");
  722. return NULL;
  723. }
  724. video_dev = cfg->video_dev;
  725. list_for_each_entry(priv, &xc2028_list, xc2028_list) {
  726. if (priv->video_dev == cfg->video_dev) {
  727. video_dev = NULL;
  728. break;
  729. }
  730. }
  731. if (video_dev) {
  732. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  733. if (priv == NULL)
  734. return NULL;
  735. priv->bandwidth = BANDWIDTH_6_MHZ;
  736. priv->need_load_generic = 1;
  737. priv->mode = T_UNINITIALIZED;
  738. priv->i2c_props.addr = cfg->i2c_addr;
  739. priv->i2c_props.adap = cfg->i2c_adap;
  740. priv->video_dev = video_dev;
  741. priv->tuner_callback = cfg->callback;
  742. priv->max_len = 13;
  743. mutex_init(&priv->lock);
  744. list_add_tail(&priv->xc2028_list, &xc2028_list);
  745. }
  746. fe->tuner_priv = priv;
  747. priv->count++;
  748. memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
  749. sizeof(xc2028_dvb_tuner_ops));
  750. tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
  751. return fe;
  752. }
  753. EXPORT_SYMBOL(xc2028_attach);
  754. MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
  755. MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
  756. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  757. MODULE_LICENSE("GPL");