tuner-xc2028.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /* tuner-xc2028
  2. *
  3. * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
  4. *
  5. * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
  6. * - frontend interface
  7. *
  8. * This code is placed under the terms of the GNU General Public License v2
  9. */
  10. #include <linux/i2c.h>
  11. #include <asm/div64.h>
  12. #include <linux/firmware.h>
  13. #include <linux/videodev2.h>
  14. #include <linux/delay.h>
  15. #include <media/tuner.h>
  16. #include <linux/mutex.h>
  17. #include "tuner-i2c.h"
  18. #include "tuner-xc2028.h"
  19. #include "tuner-xc2028-types.h"
  20. #include <linux/dvb/frontend.h>
  21. #include "dvb_frontend.h"
  22. #define PREFIX "xc2028"
  23. static int debug;
  24. module_param(debug, int, 0644);
  25. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  26. static char audio_std[8];
  27. module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
  28. MODULE_PARM_DESC(audio_std,
  29. "Audio standard. XC3028 audio decoder explicitly "
  30. "needs to know what audio\n"
  31. "standard is needed for some video standards with audio A2 or NICAM.\n"
  32. "The valid values are:\n"
  33. "A2\n"
  34. "A2/A\n"
  35. "A2/B\n"
  36. "NICAM\n"
  37. "NICAM/A\n"
  38. "NICAM/B\n");
  39. static LIST_HEAD(xc2028_list);
  40. static DEFINE_MUTEX(xc2028_list_mutex);
  41. /* struct for storing firmware table */
  42. struct firmware_description {
  43. unsigned int type;
  44. v4l2_std_id id;
  45. unsigned char *ptr;
  46. unsigned int size;
  47. };
  48. struct firmware_properties {
  49. unsigned int type;
  50. v4l2_std_id id;
  51. v4l2_std_id std_req;
  52. unsigned int scode_table;
  53. int scode_nr;
  54. };
  55. struct xc2028_data {
  56. struct list_head xc2028_list;
  57. struct tuner_i2c_props i2c_props;
  58. int (*tuner_callback) (void *dev,
  59. int command, int arg);
  60. void *video_dev;
  61. int count;
  62. __u32 frequency;
  63. struct firmware_description *firm;
  64. int firm_size;
  65. __u16 firm_version;
  66. __u16 hwmodel;
  67. __u16 hwvers;
  68. struct xc2028_ctrl ctrl;
  69. struct firmware_properties cur_fw;
  70. struct mutex lock;
  71. };
  72. #define i2c_send(priv, buf, size) ({ \
  73. int _rc; \
  74. _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
  75. if (size != _rc) \
  76. tuner_info("i2c output error: rc = %d (should be %d)\n",\
  77. _rc, (int)size); \
  78. _rc; \
  79. })
  80. #define i2c_rcv(priv, buf, size) ({ \
  81. int _rc; \
  82. _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
  83. if (size != _rc) \
  84. tuner_err("i2c input error: rc = %d (should be %d)\n", \
  85. _rc, (int)size); \
  86. _rc; \
  87. })
  88. #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
  89. int _rc; \
  90. _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
  91. ibuf, isize); \
  92. if (isize != _rc) \
  93. tuner_err("i2c input error: rc = %d (should be %d)\n", \
  94. _rc, (int)isize); \
  95. _rc; \
  96. })
  97. #define send_seq(priv, data...) ({ \
  98. static u8 _val[] = data; \
  99. int _rc; \
  100. if (sizeof(_val) != \
  101. (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
  102. _val, sizeof(_val)))) { \
  103. tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
  104. } else \
  105. msleep(10); \
  106. _rc; \
  107. })
  108. static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
  109. {
  110. unsigned char buf[2];
  111. unsigned char ibuf[2];
  112. tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
  113. buf[0] = reg >> 8;
  114. buf[1] = (unsigned char) reg;
  115. if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
  116. return -EIO;
  117. *val = (ibuf[1]) | (ibuf[0] << 8);
  118. return 0;
  119. }
  120. void dump_firm_type(unsigned int type)
  121. {
  122. if (type & BASE)
  123. printk("BASE ");
  124. if (type & INIT1)
  125. printk("INIT1 ");
  126. if (type & F8MHZ)
  127. printk("F8MHZ ");
  128. if (type & MTS)
  129. printk("MTS ");
  130. if (type & D2620)
  131. printk("D2620 ");
  132. if (type & D2633)
  133. printk("D2633 ");
  134. if (type & DTV6)
  135. printk("DTV6 ");
  136. if (type & QAM)
  137. printk("QAM ");
  138. if (type & DTV7)
  139. printk("DTV7 ");
  140. if (type & DTV78)
  141. printk("DTV78 ");
  142. if (type & DTV8)
  143. printk("DTV8 ");
  144. if (type & FM)
  145. printk("FM ");
  146. if (type & INPUT1)
  147. printk("INPUT1 ");
  148. if (type & LCD)
  149. printk("LCD ");
  150. if (type & NOGD)
  151. printk("NOGD ");
  152. if (type & MONO)
  153. printk("MONO ");
  154. if (type & ATSC)
  155. printk("ATSC ");
  156. if (type & IF)
  157. printk("IF ");
  158. if (type & LG60)
  159. printk("LG60 ");
  160. if (type & ATI638)
  161. printk("ATI638 ");
  162. if (type & OREN538)
  163. printk("OREN538 ");
  164. if (type & OREN36)
  165. printk("OREN36 ");
  166. if (type & TOYOTA388)
  167. printk("TOYOTA388 ");
  168. if (type & TOYOTA794)
  169. printk("TOYOTA794 ");
  170. if (type & DIBCOM52)
  171. printk("DIBCOM52 ");
  172. if (type & ZARLINK456)
  173. printk("ZARLINK456 ");
  174. if (type & CHINA)
  175. printk("CHINA ");
  176. if (type & F6MHZ)
  177. printk("F6MHZ ");
  178. if (type & INPUT2)
  179. printk("INPUT2 ");
  180. if (type & SCODE)
  181. printk("SCODE ");
  182. }
  183. static v4l2_std_id parse_audio_std_option(void)
  184. {
  185. if (strcasecmp(audio_std, "A2") == 0)
  186. return V4L2_STD_A2;
  187. if (strcasecmp(audio_std, "A2/A") == 0)
  188. return V4L2_STD_A2_A;
  189. if (strcasecmp(audio_std, "A2/B") == 0)
  190. return V4L2_STD_A2_B;
  191. if (strcasecmp(audio_std, "NICAM") == 0)
  192. return V4L2_STD_NICAM;
  193. if (strcasecmp(audio_std, "NICAM/A") == 0)
  194. return V4L2_STD_NICAM_A;
  195. if (strcasecmp(audio_std, "NICAM/B") == 0)
  196. return V4L2_STD_NICAM_B;
  197. return 0;
  198. }
  199. static void free_firmware(struct xc2028_data *priv)
  200. {
  201. int i;
  202. if (!priv->firm)
  203. return;
  204. for (i = 0; i < priv->firm_size; i++)
  205. kfree(priv->firm[i].ptr);
  206. kfree(priv->firm);
  207. priv->firm = NULL;
  208. priv->firm_size = 0;
  209. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  210. }
  211. static int load_all_firmwares(struct dvb_frontend *fe)
  212. {
  213. struct xc2028_data *priv = fe->tuner_priv;
  214. const struct firmware *fw = NULL;
  215. unsigned char *p, *endp;
  216. int rc = 0;
  217. int n, n_array;
  218. char name[33];
  219. tuner_dbg("%s called\n", __FUNCTION__);
  220. tuner_dbg("Reading firmware %s\n", priv->ctrl.fname);
  221. rc = request_firmware(&fw, priv->ctrl.fname,
  222. &priv->i2c_props.adap->dev);
  223. if (rc < 0) {
  224. if (rc == -ENOENT)
  225. tuner_err("Error: firmware %s not found.\n",
  226. priv->ctrl.fname);
  227. else
  228. tuner_err("Error %d while requesting firmware %s \n",
  229. rc, priv->ctrl.fname);
  230. return rc;
  231. }
  232. p = fw->data;
  233. endp = p + fw->size;
  234. if (fw->size < sizeof(name) - 1 + 2 + 2) {
  235. tuner_err("Error: firmware file %s has invalid size!\n",
  236. priv->ctrl.fname);
  237. goto corrupt;
  238. }
  239. memcpy(name, p, sizeof(name) - 1);
  240. name[sizeof(name) - 1] = 0;
  241. p += sizeof(name) - 1;
  242. priv->firm_version = le16_to_cpu(*(__u16 *) p);
  243. p += 2;
  244. n_array = le16_to_cpu(*(__u16 *) p);
  245. p += 2;
  246. tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
  247. n_array, priv->ctrl.fname, name,
  248. priv->firm_version >> 8, priv->firm_version & 0xff);
  249. priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
  250. if (priv->firm == NULL) {
  251. tuner_err("Not enough memory to load firmware file.\n");
  252. rc = -ENOMEM;
  253. goto err;
  254. }
  255. priv->firm_size = n_array;
  256. n = -1;
  257. while (p < endp) {
  258. __u32 type, size;
  259. v4l2_std_id id;
  260. n++;
  261. if (n >= n_array) {
  262. tuner_err("More firmware images in file than "
  263. "were expected!\n");
  264. goto corrupt;
  265. }
  266. /* Checks if there's enough bytes to read */
  267. if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
  268. tuner_err("Firmware header is incomplete!\n");
  269. goto corrupt;
  270. }
  271. type = le32_to_cpu(*(__u32 *) p);
  272. p += sizeof(type);
  273. id = le64_to_cpu(*(v4l2_std_id *) p);
  274. p += sizeof(id);
  275. size = le32_to_cpu(*(__u32 *) p);
  276. p += sizeof(size);
  277. if ((!size) || (size + p > endp)) {
  278. tuner_err("Firmware type ");
  279. dump_firm_type(type);
  280. printk("(%x), id %llx is corrupted "
  281. "(size=%d, expected %d)\n",
  282. type, (unsigned long long)id,
  283. (unsigned)(endp - p), size);
  284. goto corrupt;
  285. }
  286. priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
  287. if (priv->firm[n].ptr == NULL) {
  288. tuner_err("Not enough memory to load firmware file.\n");
  289. rc = -ENOMEM;
  290. goto err;
  291. }
  292. tuner_dbg("Reading firmware type ");
  293. if (debug) {
  294. dump_firm_type(type);
  295. printk("(%x), id %llx, size=%d.\n",
  296. type, (unsigned long long)id, size);
  297. }
  298. memcpy(priv->firm[n].ptr, p, size);
  299. priv->firm[n].type = type;
  300. priv->firm[n].id = id;
  301. priv->firm[n].size = size;
  302. p += size;
  303. }
  304. if (n + 1 != priv->firm_size) {
  305. tuner_err("Firmware file is incomplete!\n");
  306. goto corrupt;
  307. }
  308. goto done;
  309. corrupt:
  310. rc = -EINVAL;
  311. tuner_err("Error: firmware file is corrupted!\n");
  312. err:
  313. tuner_info("Releasing partially loaded firmware file.\n");
  314. free_firmware(priv);
  315. done:
  316. release_firmware(fw);
  317. if (rc == 0)
  318. tuner_dbg("Firmware files loaded.\n");
  319. return rc;
  320. }
  321. static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
  322. v4l2_std_id *id)
  323. {
  324. struct xc2028_data *priv = fe->tuner_priv;
  325. int i, best_i = -1, best_nr_matches = 0;
  326. tuner_dbg("%s called, want type=", __FUNCTION__);
  327. if (debug) {
  328. dump_firm_type(type);
  329. printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
  330. }
  331. if (!priv->firm) {
  332. tuner_err("Error! firmware not loaded\n");
  333. return -EINVAL;
  334. }
  335. if (((type & ~SCODE) == 0) && (*id == 0))
  336. *id = V4L2_STD_PAL;
  337. if (type & BASE)
  338. type &= BASE_TYPES;
  339. else if (type & SCODE)
  340. type &= SCODE_TYPES;
  341. else if (type & DTV_TYPES)
  342. type &= DTV_TYPES;
  343. else if (type & STD_SPECIFIC_TYPES)
  344. type &= STD_SPECIFIC_TYPES;
  345. /* Seek for exact match */
  346. for (i = 0; i < priv->firm_size; i++) {
  347. if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
  348. goto found;
  349. }
  350. /* Seek for generic video standard match */
  351. for (i = 0; i < priv->firm_size; i++) {
  352. v4l2_std_id match_mask;
  353. int nr_matches;
  354. if (type != priv->firm[i].type)
  355. continue;
  356. match_mask = *id & priv->firm[i].id;
  357. if (!match_mask)
  358. continue;
  359. if ((*id & match_mask) == *id)
  360. goto found; /* Supports all the requested standards */
  361. nr_matches = hweight64(match_mask);
  362. if (nr_matches > best_nr_matches) {
  363. best_nr_matches = nr_matches;
  364. best_i = i;
  365. }
  366. }
  367. if (best_nr_matches > 0) {
  368. tuner_dbg("Selecting best matching firmware (%d bits) for "
  369. "type=", best_nr_matches);
  370. dump_firm_type(type);
  371. printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
  372. i = best_i;
  373. goto found;
  374. }
  375. /*FIXME: Would make sense to seek for type "hint" match ? */
  376. i = -ENOENT;
  377. goto ret;
  378. found:
  379. *id = priv->firm[i].id;
  380. ret:
  381. tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
  382. if (debug) {
  383. dump_firm_type(type);
  384. printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
  385. }
  386. return i;
  387. }
  388. static int load_firmware(struct dvb_frontend *fe, unsigned int type,
  389. v4l2_std_id *id)
  390. {
  391. struct xc2028_data *priv = fe->tuner_priv;
  392. int pos, rc;
  393. unsigned char *p, *endp, buf[priv->ctrl.max_len];
  394. tuner_dbg("%s called\n", __FUNCTION__);
  395. pos = seek_firmware(fe, type, id);
  396. if (pos < 0)
  397. return pos;
  398. tuner_info("Loading firmware for type=");
  399. dump_firm_type(priv->firm[pos].type);
  400. printk("(%x), id %016llx.\n", priv->firm[pos].type,
  401. (unsigned long long)*id);
  402. p = priv->firm[pos].ptr;
  403. endp = p + priv->firm[pos].size;
  404. while (p < endp) {
  405. __u16 size;
  406. /* Checks if there's enough bytes to read */
  407. if (p + sizeof(size) > endp) {
  408. tuner_err("Firmware chunk size is wrong\n");
  409. return -EINVAL;
  410. }
  411. size = le16_to_cpu(*(__u16 *) p);
  412. p += sizeof(size);
  413. if (size == 0xffff)
  414. return 0;
  415. if (!size) {
  416. /* Special callback command received */
  417. rc = priv->tuner_callback(priv->video_dev,
  418. XC2028_TUNER_RESET, 0);
  419. if (rc < 0) {
  420. tuner_err("Error at RESET code %d\n",
  421. (*p) & 0x7f);
  422. return -EINVAL;
  423. }
  424. continue;
  425. }
  426. if (size >= 0xff00) {
  427. switch (size) {
  428. case 0xff00:
  429. rc = priv->tuner_callback(priv->video_dev,
  430. XC2028_RESET_CLK, 0);
  431. if (rc < 0) {
  432. tuner_err("Error at RESET code %d\n",
  433. (*p) & 0x7f);
  434. return -EINVAL;
  435. }
  436. break;
  437. default:
  438. tuner_info("Invalid RESET code %d\n",
  439. size & 0x7f);
  440. return -EINVAL;
  441. }
  442. continue;
  443. }
  444. /* Checks for a sleep command */
  445. if (size & 0x8000) {
  446. msleep(size & 0x7fff);
  447. continue;
  448. }
  449. if ((size + p > endp)) {
  450. tuner_err("missing bytes: need %d, have %d\n",
  451. size, (int)(endp - p));
  452. return -EINVAL;
  453. }
  454. buf[0] = *p;
  455. p++;
  456. size--;
  457. /* Sends message chunks */
  458. while (size > 0) {
  459. int len = (size < priv->ctrl.max_len - 1) ?
  460. size : priv->ctrl.max_len - 1;
  461. memcpy(buf + 1, p, len);
  462. rc = i2c_send(priv, buf, len + 1);
  463. if (rc < 0) {
  464. tuner_err("%d returned from send\n", rc);
  465. return -EINVAL;
  466. }
  467. p += len;
  468. size -= len;
  469. }
  470. }
  471. return 0;
  472. }
  473. static int load_scode(struct dvb_frontend *fe, unsigned int type,
  474. v4l2_std_id *id, int scode)
  475. {
  476. struct xc2028_data *priv = fe->tuner_priv;
  477. int pos, rc;
  478. unsigned char *p;
  479. tuner_dbg("%s called\n", __FUNCTION__);
  480. pos = seek_firmware(fe, type, id);
  481. if (pos < 0)
  482. return pos;
  483. p = priv->firm[pos].ptr;
  484. /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
  485. * has a 2-byte size header in the firmware format. */
  486. if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
  487. le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
  488. return -EINVAL;
  489. tuner_info("Loading SCODE for type=");
  490. dump_firm_type(priv->firm[pos].type);
  491. printk("(%x), id %016llx.\n", priv->firm[pos].type,
  492. (unsigned long long)*id);
  493. if (priv->firm_version < 0x0202)
  494. rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
  495. else
  496. rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
  497. if (rc < 0)
  498. return -EIO;
  499. rc = i2c_send(priv, p + 14 * scode + 2, 12);
  500. if (rc < 0)
  501. return -EIO;
  502. rc = send_seq(priv, {0x00, 0x8c});
  503. if (rc < 0)
  504. return -EIO;
  505. return 0;
  506. }
  507. static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
  508. v4l2_std_id std, fe_bandwidth_t bandwidth)
  509. {
  510. struct xc2028_data *priv = fe->tuner_priv;
  511. int rc = 0, is_retry = 0;
  512. unsigned int type = 0;
  513. struct firmware_properties new_fw;
  514. u16 version, hwmodel;
  515. v4l2_std_id std0;
  516. tuner_dbg("%s called\n", __FUNCTION__);
  517. if (!priv->firm) {
  518. if (!priv->ctrl.fname) {
  519. tuner_info("xc2028/3028 firmware name not set!\n");
  520. return -EINVAL;
  521. }
  522. rc = load_all_firmwares(fe);
  523. if (rc < 0)
  524. return rc;
  525. }
  526. if (priv->ctrl.type == XC2028_FIRM_MTS)
  527. type |= MTS;
  528. if (bandwidth == BANDWIDTH_7_MHZ || bandwidth == BANDWIDTH_8_MHZ)
  529. type |= F8MHZ;
  530. /* FIXME: How to load FM and FM|INPUT1 firmwares? */
  531. if (new_mode == T_DIGITAL_TV) {
  532. if (priv->ctrl.d2633)
  533. type |= D2633;
  534. else
  535. type |= D2620;
  536. switch (bandwidth) {
  537. case BANDWIDTH_8_MHZ:
  538. type |= DTV8;
  539. break;
  540. case BANDWIDTH_7_MHZ:
  541. type |= DTV7;
  542. break;
  543. case BANDWIDTH_6_MHZ:
  544. /* FIXME: Should allow select also ATSC */
  545. type |= DTV6 | QAM;
  546. break;
  547. default:
  548. tuner_err("error: bandwidth not supported.\n");
  549. };
  550. }
  551. retry:
  552. new_fw.type = type;
  553. new_fw.id = std;
  554. new_fw.std_req = std;
  555. new_fw.scode_table = SCODE | priv->ctrl.scode_table;
  556. new_fw.scode_nr = 0;
  557. tuner_dbg("checking firmware, user requested type=");
  558. if (debug) {
  559. dump_firm_type(new_fw.type);
  560. printk("(%x), id %016llx, scode_tbl ", new_fw.type,
  561. (unsigned long long)new_fw.std_req);
  562. dump_firm_type(priv->ctrl.scode_table);
  563. printk("(%x), scode_nr %d\n", priv->ctrl.scode_table,
  564. new_fw.scode_nr);
  565. }
  566. /* No need to reload base firmware if it matches */
  567. if (((BASE | new_fw.type) & BASE_TYPES) ==
  568. (priv->cur_fw.type & BASE_TYPES)) {
  569. tuner_dbg("BASE firmware not changed.\n");
  570. goto skip_base;
  571. }
  572. /* Updating BASE - forget about all currently loaded firmware */
  573. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  574. /* Reset is needed before loading firmware */
  575. rc = priv->tuner_callback(priv->video_dev,
  576. XC2028_TUNER_RESET, 0);
  577. if (rc < 0)
  578. goto fail;
  579. /* BASE firmwares are all std0 */
  580. std0 = 0;
  581. rc = load_firmware(fe, BASE | new_fw.type, &std0);
  582. if (rc < 0) {
  583. tuner_err("Error %d while loading base firmware\n",
  584. rc);
  585. goto fail;
  586. }
  587. /* Load INIT1, if needed */
  588. tuner_dbg("Load init1 firmware, if exists\n");
  589. rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
  590. if (rc == -ENOENT)
  591. rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
  592. &std0);
  593. if (rc < 0 && rc != -ENOENT) {
  594. tuner_err("Error %d while loading init1 firmware\n",
  595. rc);
  596. goto fail;
  597. }
  598. skip_base:
  599. /*
  600. * No need to reload standard specific firmware if base firmware
  601. * was not reloaded and requested video standards have not changed.
  602. */
  603. if (priv->cur_fw.type == (BASE | new_fw.type) &&
  604. priv->cur_fw.std_req == std) {
  605. tuner_dbg("Std-specific firmware already loaded.\n");
  606. goto skip_std_specific;
  607. }
  608. /* Reloading std-specific firmware forces a SCODE update */
  609. priv->cur_fw.scode_table = 0;
  610. /* Add audio hack to std mask */
  611. if (new_mode == T_ANALOG_TV)
  612. new_fw.id |= parse_audio_std_option();
  613. rc = load_firmware(fe, new_fw.type, &new_fw.id);
  614. if (rc < 0)
  615. goto fail;
  616. skip_std_specific:
  617. if (priv->cur_fw.scode_table == new_fw.scode_table &&
  618. priv->cur_fw.scode_nr == new_fw.scode_nr) {
  619. tuner_dbg("SCODE firmware already loaded.\n");
  620. goto check_device;
  621. }
  622. /* Load SCODE firmware, if exists */
  623. tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
  624. rc = load_scode(fe, new_fw.type | new_fw.scode_table,
  625. &new_fw.id, new_fw.scode_nr);
  626. check_device:
  627. if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
  628. xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
  629. tuner_err("Unable to read tuner registers.\n");
  630. goto fail;
  631. }
  632. tuner_info("Device is Xceive %d version %d.%d, "
  633. "firmware version %d.%d\n",
  634. hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
  635. (version & 0xf0) >> 4, version & 0xf);
  636. /* Check firmware version against what we downloaded. */
  637. if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
  638. tuner_err("Incorrect readback of firmware version.\n");
  639. goto fail;
  640. }
  641. /* Check that the tuner hardware model remains consistent over time. */
  642. if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
  643. priv->hwmodel = hwmodel;
  644. priv->hwvers = version & 0xff00;
  645. } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
  646. priv->hwvers != (version & 0xff00)) {
  647. tuner_err("Read invalid device hardware information - tuner "
  648. "hung?\n");
  649. goto fail;
  650. }
  651. memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
  652. /*
  653. * By setting BASE in cur_fw.type only after successfully loading all
  654. * firmwares, we can:
  655. * 1. Identify that BASE firmware with type=0 has been loaded;
  656. * 2. Tell whether BASE firmware was just changed the next time through.
  657. */
  658. priv->cur_fw.type |= BASE;
  659. return 0;
  660. fail:
  661. memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
  662. if (!is_retry) {
  663. msleep(50);
  664. is_retry = 1;
  665. tuner_dbg("Retrying firmware load\n");
  666. goto retry;
  667. }
  668. if (rc == -ENOENT)
  669. rc = -EINVAL;
  670. return rc;
  671. }
  672. static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
  673. {
  674. struct xc2028_data *priv = fe->tuner_priv;
  675. u16 frq_lock, signal = 0;
  676. int rc;
  677. tuner_dbg("%s called\n", __FUNCTION__);
  678. mutex_lock(&priv->lock);
  679. /* Sync Lock Indicator */
  680. rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
  681. if (rc < 0 || frq_lock == 0)
  682. goto ret;
  683. /* Frequency is locked. Return signal quality */
  684. /* Get SNR of the video signal */
  685. rc = xc2028_get_reg(priv, 0x0040, &signal);
  686. if (rc < 0)
  687. signal = -frq_lock;
  688. ret:
  689. mutex_unlock(&priv->lock);
  690. *strength = signal;
  691. return rc;
  692. }
  693. #define DIV 15625
  694. static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
  695. enum tuner_mode new_mode,
  696. v4l2_std_id std, fe_bandwidth_t bandwidth)
  697. {
  698. struct xc2028_data *priv = fe->tuner_priv;
  699. int rc = -EINVAL;
  700. unsigned char buf[4];
  701. u32 div, offset = 0;
  702. tuner_dbg("%s called\n", __FUNCTION__);
  703. mutex_lock(&priv->lock);
  704. /* HACK: It seems that specific firmware need to be reloaded
  705. when watching analog TV and freq is changed */
  706. if (new_mode != T_DIGITAL_TV)
  707. priv->cur_fw.type = 0;
  708. tuner_dbg("should set frequency %d kHz\n", freq / 1000);
  709. if (check_firmware(fe, new_mode, std, bandwidth) < 0)
  710. goto ret;
  711. if (new_mode == T_DIGITAL_TV) {
  712. offset = 2750000;
  713. if (priv->cur_fw.type & DTV7)
  714. offset -= 500000;
  715. }
  716. div = (freq - offset + DIV / 2) / DIV;
  717. /* CMD= Set frequency */
  718. if (priv->firm_version < 0x0202)
  719. rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
  720. else
  721. rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
  722. if (rc < 0)
  723. goto ret;
  724. rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
  725. if (rc < 0)
  726. goto ret;
  727. msleep(10);
  728. buf[0] = 0xff & (div >> 24);
  729. buf[1] = 0xff & (div >> 16);
  730. buf[2] = 0xff & (div >> 8);
  731. buf[3] = 0xff & (div);
  732. rc = i2c_send(priv, buf, sizeof(buf));
  733. if (rc < 0)
  734. goto ret;
  735. msleep(100);
  736. priv->frequency = freq;
  737. tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
  738. buf[0], buf[1], buf[2], buf[3],
  739. freq / 1000000, (freq % 1000000) / 1000);
  740. rc = 0;
  741. ret:
  742. mutex_unlock(&priv->lock);
  743. return rc;
  744. }
  745. static int xc2028_set_tv_freq(struct dvb_frontend *fe,
  746. struct analog_parameters *p)
  747. {
  748. struct xc2028_data *priv = fe->tuner_priv;
  749. tuner_dbg("%s called\n", __FUNCTION__);
  750. return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
  751. p->std, BANDWIDTH_8_MHZ);
  752. /* XXX Are some analog standards 6MHz? */
  753. }
  754. static int xc2028_set_params(struct dvb_frontend *fe,
  755. struct dvb_frontend_parameters *p)
  756. {
  757. struct xc2028_data *priv = fe->tuner_priv;
  758. tuner_dbg("%s called\n", __FUNCTION__);
  759. /* FIXME: Only OFDM implemented */
  760. if (fe->ops.info.type != FE_OFDM) {
  761. tuner_err("DTV type not implemented.\n");
  762. return -EINVAL;
  763. }
  764. return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
  765. 0 /* NOT USED */,
  766. p->u.ofdm.bandwidth);
  767. }
  768. static int xc2028_sleep(struct dvb_frontend *fe)
  769. {
  770. struct xc2028_data *priv = fe->tuner_priv;
  771. int rc = 0;
  772. tuner_dbg("%s called\n", __FUNCTION__);
  773. mutex_lock(&priv->lock);
  774. if (priv->firm_version < 0x0202)
  775. rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
  776. else
  777. rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
  778. priv->cur_fw.type = 0; /* need firmware reload */
  779. mutex_unlock(&priv->lock);
  780. return rc;
  781. }
  782. static int xc2028_dvb_release(struct dvb_frontend *fe)
  783. {
  784. struct xc2028_data *priv = fe->tuner_priv;
  785. tuner_dbg("%s called\n", __FUNCTION__);
  786. mutex_lock(&xc2028_list_mutex);
  787. priv->count--;
  788. if (!priv->count) {
  789. list_del(&priv->xc2028_list);
  790. kfree(priv->ctrl.fname);
  791. free_firmware(priv);
  792. kfree(priv);
  793. fe->tuner_priv = NULL;
  794. }
  795. mutex_unlock(&xc2028_list_mutex);
  796. return 0;
  797. }
  798. static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  799. {
  800. struct xc2028_data *priv = fe->tuner_priv;
  801. tuner_dbg("%s called\n", __FUNCTION__);
  802. *frequency = priv->frequency;
  803. return 0;
  804. }
  805. static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
  806. {
  807. struct xc2028_data *priv = fe->tuner_priv;
  808. struct xc2028_ctrl *p = priv_cfg;
  809. int rc = 0;
  810. tuner_dbg("%s called\n", __FUNCTION__);
  811. mutex_lock(&priv->lock);
  812. kfree(priv->ctrl.fname);
  813. free_firmware(priv);
  814. memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
  815. priv->ctrl.fname = NULL;
  816. if (p->fname) {
  817. priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
  818. if (priv->ctrl.fname == NULL)
  819. rc = -ENOMEM;
  820. }
  821. if (priv->ctrl.max_len < 9)
  822. priv->ctrl.max_len = 13;
  823. mutex_unlock(&priv->lock);
  824. return rc;
  825. }
  826. static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
  827. .info = {
  828. .name = "Xceive XC3028",
  829. .frequency_min = 42000000,
  830. .frequency_max = 864000000,
  831. .frequency_step = 50000,
  832. },
  833. .set_config = xc2028_set_config,
  834. .set_analog_params = xc2028_set_tv_freq,
  835. .release = xc2028_dvb_release,
  836. .get_frequency = xc2028_get_frequency,
  837. .get_rf_strength = xc2028_signal,
  838. .set_params = xc2028_set_params,
  839. .sleep = xc2028_sleep,
  840. };
  841. void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
  842. {
  843. struct xc2028_data *priv;
  844. void *video_dev;
  845. if (debug)
  846. printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
  847. if (NULL == cfg->video_dev)
  848. return NULL;
  849. if (!fe) {
  850. printk(KERN_ERR PREFIX ": No frontend!\n");
  851. return NULL;
  852. }
  853. video_dev = cfg->video_dev;
  854. mutex_lock(&xc2028_list_mutex);
  855. list_for_each_entry(priv, &xc2028_list, xc2028_list) {
  856. if (priv->video_dev == cfg->video_dev) {
  857. video_dev = NULL;
  858. break;
  859. }
  860. }
  861. if (video_dev) {
  862. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  863. if (priv == NULL) {
  864. mutex_unlock(&xc2028_list_mutex);
  865. return NULL;
  866. }
  867. priv->i2c_props.addr = cfg->i2c_addr;
  868. priv->i2c_props.adap = cfg->i2c_adap;
  869. priv->video_dev = video_dev;
  870. priv->tuner_callback = cfg->callback;
  871. priv->ctrl.max_len = 13;
  872. mutex_init(&priv->lock);
  873. list_add_tail(&priv->xc2028_list, &xc2028_list);
  874. }
  875. fe->tuner_priv = priv;
  876. priv->count++;
  877. memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
  878. sizeof(xc2028_dvb_tuner_ops));
  879. tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
  880. mutex_unlock(&xc2028_list_mutex);
  881. return fe;
  882. }
  883. EXPORT_SYMBOL(xc2028_attach);
  884. MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
  885. MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
  886. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  887. MODULE_LICENSE("GPL");