tuner-xc2028.c 19 KB

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