tuner-xc2028.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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 %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. tuner_info("xc2028/3028 firmware name not set!\n");
  449. return -EINVAL;
  450. }
  451. rc = load_all_firmwares(fe);
  452. if (rc < 0)
  453. return rc;
  454. }
  455. tuner_dbg("I am in mode %u and I should switch to mode %i\n",
  456. priv->mode, new_mode);
  457. /* first of all, determine whether we have switched the mode */
  458. if (new_mode != priv->mode) {
  459. priv->mode = new_mode;
  460. priv->need_load_generic = 1;
  461. }
  462. change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
  463. && bandwidth != priv->bandwidth) ? 1 : 0;
  464. tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
  465. bandwidth);
  466. if (priv->need_load_generic) {
  467. /* Reset is needed before loading firmware */
  468. rc = priv->tuner_callback(priv->video_dev,
  469. XC2028_TUNER_RESET, 0);
  470. if (rc < 0)
  471. return rc;
  472. type0 = BASE;
  473. if (priv->ctrl.type == XC2028_FIRM_MTS)
  474. type0 |= MTS;
  475. if (priv->bandwidth == 8)
  476. type0 |= F8MHZ;
  477. /* FIXME: How to load FM and FM|INPUT1 firmwares? */
  478. rc = load_firmware(fe, type0, &std0);
  479. if (rc < 0) {
  480. tuner_err("Error %d while loading generic firmware\n",
  481. rc);
  482. return rc;
  483. }
  484. priv->need_load_generic = 0;
  485. priv->firm_type = 0;
  486. if (priv->mode == T_DIGITAL_TV)
  487. change_digital_bandwidth = 1;
  488. }
  489. tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
  490. if (change_digital_bandwidth) {
  491. /*FIXME: Should allow selecting between D2620 and D2633 */
  492. type |= D2620;
  493. /* FIXME: When should select a DTV78 firmware?
  494. */
  495. switch (bandwidth) {
  496. case BANDWIDTH_8_MHZ:
  497. type |= DTV8;
  498. break;
  499. case BANDWIDTH_7_MHZ:
  500. type |= DTV7;
  501. break;
  502. case BANDWIDTH_6_MHZ:
  503. /* FIXME: Should allow select also ATSC */
  504. type |= DTV6 | QAM;
  505. break;
  506. default:
  507. tuner_err("error: bandwidth not supported.\n");
  508. };
  509. priv->bandwidth = bandwidth;
  510. }
  511. /* Load INIT1, if needed */
  512. tuner_dbg("Load init1 firmware, if exists\n");
  513. type0 = BASE | INIT1;
  514. if (priv->ctrl.type == XC2028_FIRM_MTS)
  515. type0 |= MTS;
  516. /* FIXME: Should handle errors - if INIT1 found */
  517. rc = load_firmware(fe, type0, &std0);
  518. /* FIXME: Should add support for FM radio
  519. */
  520. if (priv->ctrl.type == XC2028_FIRM_MTS)
  521. type |= MTS;
  522. if (priv->firm_type & std) {
  523. tuner_dbg("Std-specific firmware already loaded.\n");
  524. return 0;
  525. }
  526. /* Add audio hack to std mask */
  527. std |= parse_audio_std_option();
  528. rc = load_firmware(fe, type, &std);
  529. if (rc < 0)
  530. return rc;
  531. /* Load SCODE firmware, if exists */
  532. tuner_dbg("Trying to load scode 0\n");
  533. type |= SCODE;
  534. rc = load_scode(fe, type, &std, 0);
  535. version = xc2028_get_reg(priv, 0x0004);
  536. hwmodel = xc2028_get_reg(priv, 0x0008);
  537. tuner_info("Device is Xceive %d version %d.%d, "
  538. "firmware version %d.%d\n",
  539. hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
  540. (version & 0xf0) >> 4, version & 0xf);
  541. priv->firm_type = std;
  542. return 0;
  543. }
  544. static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
  545. {
  546. struct xc2028_data *priv = fe->tuner_priv;
  547. int frq_lock, signal = 0;
  548. tuner_dbg("%s called\n", __FUNCTION__);
  549. mutex_lock(&priv->lock);
  550. *strength = 0;
  551. /* Sync Lock Indicator */
  552. frq_lock = xc2028_get_reg(priv, 0x0002);
  553. if (frq_lock <= 0)
  554. goto ret;
  555. /* Frequency is locked. Return signal quality */
  556. /* Get SNR of the video signal */
  557. signal = xc2028_get_reg(priv, 0x0040);
  558. if (signal <= 0)
  559. signal = frq_lock;
  560. ret:
  561. mutex_unlock(&priv->lock);
  562. *strength = signal;
  563. return 0;
  564. }
  565. #define DIV 15625
  566. static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
  567. enum tuner_mode new_mode,
  568. v4l2_std_id std, fe_bandwidth_t bandwidth)
  569. {
  570. struct xc2028_data *priv = fe->tuner_priv;
  571. int rc = -EINVAL;
  572. unsigned char buf[5];
  573. u32 div, offset = 0;
  574. tuner_dbg("%s called\n", __FUNCTION__);
  575. mutex_lock(&priv->lock);
  576. /* HACK: It seems that specific firmware need to be reloaded
  577. when freq is changed */
  578. priv->firm_type = 0;
  579. /* Reset GPIO 1 */
  580. rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
  581. if (rc < 0)
  582. goto ret;
  583. msleep(10);
  584. tuner_dbg("should set frequency %d kHz)\n", freq / 1000);
  585. if (check_firmware(fe, new_mode, std, bandwidth) < 0)
  586. goto ret;
  587. if (new_mode == T_DIGITAL_TV)
  588. offset = 2750000;
  589. div = (freq - offset + DIV / 2) / DIV;
  590. /* CMD= Set frequency */
  591. if (priv->version < 0x0202) {
  592. send_seq(priv, {0x00, 0x02, 0x00, 0x00});
  593. } else {
  594. send_seq(priv, {0x80, 0x02, 0x00, 0x00});
  595. }
  596. rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
  597. if (rc < 0)
  598. goto ret;
  599. msleep(10);
  600. buf[0] = 0xff & (div >> 24);
  601. buf[1] = 0xff & (div >> 16);
  602. buf[2] = 0xff & (div >> 8);
  603. buf[3] = 0xff & (div);
  604. buf[4] = 0;
  605. i2c_send(rc, priv, buf, sizeof(buf));
  606. if (rc < 0)
  607. goto ret;
  608. msleep(100);
  609. priv->frequency = freq;
  610. tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
  611. buf[1], buf[2], buf[3], buf[4],
  612. freq / 1000000, (freq % 1000000) / 10000);
  613. rc = 0;
  614. ret:
  615. mutex_unlock(&priv->lock);
  616. return rc;
  617. }
  618. static int xc2028_set_tv_freq(struct dvb_frontend *fe,
  619. struct analog_parameters *p)
  620. {
  621. struct xc2028_data *priv = fe->tuner_priv;
  622. tuner_dbg("%s called\n", __FUNCTION__);
  623. return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
  624. p->std, BANDWIDTH_8_MHZ /* NOT USED */);
  625. }
  626. static int xc2028_set_params(struct dvb_frontend *fe,
  627. struct dvb_frontend_parameters *p)
  628. {
  629. struct xc2028_data *priv = fe->tuner_priv;
  630. tuner_dbg("%s called\n", __FUNCTION__);
  631. /* FIXME: Only OFDM implemented */
  632. if (fe->ops.info.type != FE_OFDM) {
  633. tuner_err("DTV type not implemented.\n");
  634. return -EINVAL;
  635. }
  636. return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
  637. 0 /* NOT USED */,
  638. p->u.ofdm.bandwidth);
  639. }
  640. static int xc2028_dvb_release(struct dvb_frontend *fe)
  641. {
  642. struct xc2028_data *priv = fe->tuner_priv;
  643. tuner_dbg("%s called\n", __FUNCTION__);
  644. priv->count--;
  645. if (!priv->count) {
  646. list_del(&priv->xc2028_list);
  647. kfree(priv->ctrl.fname);
  648. free_firmware(priv);
  649. kfree(priv);
  650. }
  651. return 0;
  652. }
  653. static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  654. {
  655. struct xc2028_data *priv = fe->tuner_priv;
  656. tuner_dbg("%s called\n", __FUNCTION__);
  657. *frequency = priv->frequency;
  658. return 0;
  659. }
  660. static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
  661. {
  662. struct xc2028_data *priv = fe->tuner_priv;
  663. struct xc2028_ctrl *p = priv_cfg;
  664. tuner_dbg("%s called\n", __FUNCTION__);
  665. priv->ctrl.type = p->type;
  666. if (p->fname) {
  667. kfree(priv->ctrl.fname);
  668. priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL);
  669. if (!priv->ctrl.fname)
  670. return -ENOMEM;
  671. free_firmware(priv);
  672. strcpy(priv->ctrl.fname, p->fname);
  673. }
  674. if (p->max_len > 0)
  675. priv->max_len = p->max_len;
  676. return 0;
  677. }
  678. static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
  679. .info = {
  680. .name = "Xceive XC3028",
  681. .frequency_min = 42000000,
  682. .frequency_max = 864000000,
  683. .frequency_step = 50000,
  684. },
  685. .set_config = xc2028_set_config,
  686. .set_analog_params = xc2028_set_tv_freq,
  687. .release = xc2028_dvb_release,
  688. .get_frequency = xc2028_get_frequency,
  689. .get_rf_strength = xc2028_signal,
  690. .set_params = xc2028_set_params,
  691. };
  692. void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
  693. {
  694. struct xc2028_data *priv;
  695. void *video_dev;
  696. if (debug)
  697. printk(KERN_DEBUG PREFIX "Xcv2028/3028 init called!\n");
  698. if (NULL == cfg->video_dev)
  699. return NULL;
  700. if (!fe) {
  701. printk(KERN_ERR PREFIX "No frontend!\n");
  702. return NULL;
  703. }
  704. video_dev = cfg->video_dev;
  705. list_for_each_entry(priv, &xc2028_list, xc2028_list) {
  706. if (priv->video_dev == cfg->video_dev) {
  707. video_dev = NULL;
  708. break;
  709. }
  710. }
  711. if (video_dev) {
  712. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  713. if (priv == NULL)
  714. return NULL;
  715. priv->bandwidth = BANDWIDTH_6_MHZ;
  716. priv->need_load_generic = 1;
  717. priv->mode = T_UNINITIALIZED;
  718. priv->i2c_props.addr = cfg->i2c_addr;
  719. priv->i2c_props.adap = cfg->i2c_adap;
  720. priv->video_dev = video_dev;
  721. priv->tuner_callback = cfg->callback;
  722. priv->max_len = 13;
  723. mutex_init(&priv->lock);
  724. list_add_tail(&priv->xc2028_list, &xc2028_list);
  725. }
  726. fe->tuner_priv = priv;
  727. priv->count++;
  728. memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
  729. sizeof(xc2028_dvb_tuner_ops));
  730. tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
  731. return fe;
  732. }
  733. EXPORT_SYMBOL(xc2028_attach);
  734. MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
  735. MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
  736. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  737. MODULE_LICENSE("GPL");