tuner-xc2028.c 28 KB

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