tuner-xc2028.c 20 KB

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