tuner-xc2028.c 28 KB

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