nxt200x.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. /*
  2. * Support for NXT2002 and NXT2004 - VSB/QAM
  3. *
  4. * Copyright (C) 2005 Kirk Lapray <kirk.lapray@gmail.com>
  5. * Copyright (C) 2006 Michael Krufky <mkrufky@m1k.net>
  6. * based on nxt2002 by Taylor Jacob <rtjacob@earthlink.net>
  7. * and nxt2004 by Jean-Francois Thibert <jeanfrancois@sagetv.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. */
  24. /*
  25. * NOTES ABOUT THIS DRIVER
  26. *
  27. * This Linux driver supports:
  28. * B2C2/BBTI Technisat Air2PC - ATSC (NXT2002)
  29. * AverTVHD MCE A180 (NXT2004)
  30. * ATI HDTV Wonder (NXT2004)
  31. *
  32. * This driver needs external firmware. Please use the command
  33. * "<kerneldir>/Documentation/dvb/get_dvb_firmware nxt2002" or
  34. * "<kerneldir>/Documentation/dvb/get_dvb_firmware nxt2004" to
  35. * download/extract the appropriate firmware, and then copy it to
  36. * /usr/lib/hotplug/firmware/ or /lib/firmware/
  37. * (depending on configuration of firmware hotplug).
  38. */
  39. #define NXT2002_DEFAULT_FIRMWARE "dvb-fe-nxt2002.fw"
  40. #define NXT2004_DEFAULT_FIRMWARE "dvb-fe-nxt2004.fw"
  41. #define CRC_CCIT_MASK 0x1021
  42. #include <linux/kernel.h>
  43. #include <linux/init.h>
  44. #include <linux/module.h>
  45. #include <linux/moduleparam.h>
  46. #include <linux/slab.h>
  47. #include <linux/string.h>
  48. #include "dvb_frontend.h"
  49. #include "dvb-pll.h"
  50. #include "nxt200x.h"
  51. struct nxt200x_state {
  52. struct i2c_adapter* i2c;
  53. struct dvb_frontend_ops ops;
  54. const struct nxt200x_config* config;
  55. struct dvb_frontend frontend;
  56. /* demodulator private data */
  57. nxt_chip_type demod_chip;
  58. u8 initialised:1;
  59. };
  60. static int debug;
  61. #define dprintk(args...) \
  62. do { \
  63. if (debug) printk(KERN_DEBUG "nxt200x: " args); \
  64. } while (0)
  65. static int i2c_writebytes (struct nxt200x_state* state, u8 addr, u8 *buf, u8 len)
  66. {
  67. int err;
  68. struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = len };
  69. if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
  70. printk (KERN_WARNING "nxt200x: %s: i2c write error (addr 0x%02x, err == %i)\n",
  71. __FUNCTION__, addr, err);
  72. return -EREMOTEIO;
  73. }
  74. return 0;
  75. }
  76. static u8 i2c_readbytes (struct nxt200x_state* state, u8 addr, u8* buf, u8 len)
  77. {
  78. int err;
  79. struct i2c_msg msg = { .addr = addr, .flags = I2C_M_RD, .buf = buf, .len = len };
  80. if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
  81. printk (KERN_WARNING "nxt200x: %s: i2c read error (addr 0x%02x, err == %i)\n",
  82. __FUNCTION__, addr, err);
  83. return -EREMOTEIO;
  84. }
  85. return 0;
  86. }
  87. static int nxt200x_writebytes (struct nxt200x_state* state, u8 reg, u8 *buf, u8 len)
  88. {
  89. u8 buf2 [len+1];
  90. int err;
  91. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf2, .len = len + 1 };
  92. buf2[0] = reg;
  93. memcpy(&buf2[1], buf, len);
  94. if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
  95. printk (KERN_WARNING "nxt200x: %s: i2c write error (addr 0x%02x, err == %i)\n",
  96. __FUNCTION__, state->config->demod_address, err);
  97. return -EREMOTEIO;
  98. }
  99. return 0;
  100. }
  101. static u8 nxt200x_readbytes (struct nxt200x_state* state, u8 reg, u8* buf, u8 len)
  102. {
  103. u8 reg2 [] = { reg };
  104. struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = reg2, .len = 1 },
  105. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = buf, .len = len } };
  106. int err;
  107. if ((err = i2c_transfer (state->i2c, msg, 2)) != 2) {
  108. printk (KERN_WARNING "nxt200x: %s: i2c read error (addr 0x%02x, err == %i)\n",
  109. __FUNCTION__, state->config->demod_address, err);
  110. return -EREMOTEIO;
  111. }
  112. return 0;
  113. }
  114. static u16 nxt200x_crc(u16 crc, u8 c)
  115. {
  116. u8 i;
  117. u16 input = (u16) c & 0xFF;
  118. input<<=8;
  119. for(i=0; i<8; i++) {
  120. if((crc^input) & 0x8000)
  121. crc=(crc<<1)^CRC_CCIT_MASK;
  122. else
  123. crc<<=1;
  124. input<<=1;
  125. }
  126. return crc;
  127. }
  128. static int nxt200x_writereg_multibyte (struct nxt200x_state* state, u8 reg, u8* data, u8 len)
  129. {
  130. u8 attr, len2, buf;
  131. dprintk("%s\n", __FUNCTION__);
  132. /* set mutli register register */
  133. nxt200x_writebytes(state, 0x35, &reg, 1);
  134. /* send the actual data */
  135. nxt200x_writebytes(state, 0x36, data, len);
  136. switch (state->demod_chip) {
  137. case NXT2002:
  138. len2 = len;
  139. buf = 0x02;
  140. break;
  141. case NXT2004:
  142. /* probably not right, but gives correct values */
  143. attr = 0x02;
  144. if (reg & 0x80) {
  145. attr = attr << 1;
  146. if (reg & 0x04)
  147. attr = attr >> 1;
  148. }
  149. /* set write bit */
  150. len2 = ((attr << 4) | 0x10) | len;
  151. buf = 0x80;
  152. break;
  153. default:
  154. return -EINVAL;
  155. break;
  156. }
  157. /* set multi register length */
  158. nxt200x_writebytes(state, 0x34, &len2, 1);
  159. /* toggle the multireg write bit */
  160. nxt200x_writebytes(state, 0x21, &buf, 1);
  161. nxt200x_readbytes(state, 0x21, &buf, 1);
  162. switch (state->demod_chip) {
  163. case NXT2002:
  164. if ((buf & 0x02) == 0)
  165. return 0;
  166. break;
  167. case NXT2004:
  168. if (buf == 0)
  169. return 0;
  170. break;
  171. default:
  172. return -EINVAL;
  173. break;
  174. }
  175. printk(KERN_WARNING "nxt200x: Error writing multireg register 0x%02X\n",reg);
  176. return 0;
  177. }
  178. static int nxt200x_readreg_multibyte (struct nxt200x_state* state, u8 reg, u8* data, u8 len)
  179. {
  180. int i;
  181. u8 buf, len2, attr;
  182. dprintk("%s\n", __FUNCTION__);
  183. /* set mutli register register */
  184. nxt200x_writebytes(state, 0x35, &reg, 1);
  185. switch (state->demod_chip) {
  186. case NXT2002:
  187. /* set multi register length */
  188. len2 = len & 0x80;
  189. nxt200x_writebytes(state, 0x34, &len2, 1);
  190. /* read the actual data */
  191. nxt200x_readbytes(state, reg, data, len);
  192. return 0;
  193. break;
  194. case NXT2004:
  195. /* probably not right, but gives correct values */
  196. attr = 0x02;
  197. if (reg & 0x80) {
  198. attr = attr << 1;
  199. if (reg & 0x04)
  200. attr = attr >> 1;
  201. }
  202. /* set multi register length */
  203. len2 = (attr << 4) | len;
  204. nxt200x_writebytes(state, 0x34, &len2, 1);
  205. /* toggle the multireg bit*/
  206. buf = 0x80;
  207. nxt200x_writebytes(state, 0x21, &buf, 1);
  208. /* read the actual data */
  209. for(i = 0; i < len; i++) {
  210. nxt200x_readbytes(state, 0x36 + i, &data[i], 1);
  211. }
  212. return 0;
  213. break;
  214. default:
  215. return -EINVAL;
  216. break;
  217. }
  218. }
  219. static void nxt200x_microcontroller_stop (struct nxt200x_state* state)
  220. {
  221. u8 buf, stopval, counter = 0;
  222. dprintk("%s\n", __FUNCTION__);
  223. /* set correct stop value */
  224. switch (state->demod_chip) {
  225. case NXT2002:
  226. stopval = 0x40;
  227. break;
  228. case NXT2004:
  229. stopval = 0x10;
  230. break;
  231. default:
  232. stopval = 0;
  233. break;
  234. }
  235. buf = 0x80;
  236. nxt200x_writebytes(state, 0x22, &buf, 1);
  237. while (counter < 20) {
  238. nxt200x_readbytes(state, 0x31, &buf, 1);
  239. if (buf & stopval)
  240. return;
  241. msleep(10);
  242. counter++;
  243. }
  244. printk(KERN_WARNING "nxt200x: Timeout waiting for nxt200x to stop. This is ok after firmware upload.\n");
  245. return;
  246. }
  247. static void nxt200x_microcontroller_start (struct nxt200x_state* state)
  248. {
  249. u8 buf;
  250. dprintk("%s\n", __FUNCTION__);
  251. buf = 0x00;
  252. nxt200x_writebytes(state, 0x22, &buf, 1);
  253. }
  254. static void nxt2004_microcontroller_init (struct nxt200x_state* state)
  255. {
  256. u8 buf[9];
  257. u8 counter = 0;
  258. dprintk("%s\n", __FUNCTION__);
  259. buf[0] = 0x00;
  260. nxt200x_writebytes(state, 0x2b, buf, 1);
  261. buf[0] = 0x70;
  262. nxt200x_writebytes(state, 0x34, buf, 1);
  263. buf[0] = 0x04;
  264. nxt200x_writebytes(state, 0x35, buf, 1);
  265. buf[0] = 0x01; buf[1] = 0x23; buf[2] = 0x45; buf[3] = 0x67; buf[4] = 0x89;
  266. buf[5] = 0xAB; buf[6] = 0xCD; buf[7] = 0xEF; buf[8] = 0xC0;
  267. nxt200x_writebytes(state, 0x36, buf, 9);
  268. buf[0] = 0x80;
  269. nxt200x_writebytes(state, 0x21, buf, 1);
  270. while (counter < 20) {
  271. nxt200x_readbytes(state, 0x21, buf, 1);
  272. if (buf[0] == 0)
  273. return;
  274. msleep(10);
  275. counter++;
  276. }
  277. printk(KERN_WARNING "nxt200x: Timeout waiting for nxt2004 to init.\n");
  278. return;
  279. }
  280. static int nxt200x_writetuner (struct nxt200x_state* state, u8* data)
  281. {
  282. u8 buf, count = 0;
  283. dprintk("%s\n", __FUNCTION__);
  284. dprintk("Tuner Bytes: %02X %02X %02X %02X\n", data[0], data[1], data[2], data[3]);
  285. /* if NXT2004, write directly to tuner. if NXT2002, write through NXT chip.
  286. * direct write is required for Philips TUV1236D and ALPS TDHU2 */
  287. switch (state->demod_chip) {
  288. case NXT2004:
  289. if (i2c_writebytes(state, state->config->pll_address, data, 4))
  290. printk(KERN_WARNING "nxt200x: error writing to tuner\n");
  291. /* wait until we have a lock */
  292. while (count < 20) {
  293. i2c_readbytes(state, state->config->pll_address, &buf, 1);
  294. if (buf & 0x40)
  295. return 0;
  296. msleep(100);
  297. count++;
  298. }
  299. printk("nxt2004: timeout waiting for tuner lock\n");
  300. break;
  301. case NXT2002:
  302. /* set the i2c transfer speed to the tuner */
  303. buf = 0x03;
  304. nxt200x_writebytes(state, 0x20, &buf, 1);
  305. /* setup to transfer 4 bytes via i2c */
  306. buf = 0x04;
  307. nxt200x_writebytes(state, 0x34, &buf, 1);
  308. /* write actual tuner bytes */
  309. nxt200x_writebytes(state, 0x36, data, 4);
  310. /* set tuner i2c address */
  311. buf = state->config->pll_address;
  312. nxt200x_writebytes(state, 0x35, &buf, 1);
  313. /* write UC Opmode to begin transfer */
  314. buf = 0x80;
  315. nxt200x_writebytes(state, 0x21, &buf, 1);
  316. while (count < 20) {
  317. nxt200x_readbytes(state, 0x21, &buf, 1);
  318. if ((buf & 0x80)== 0x00)
  319. return 0;
  320. msleep(100);
  321. count++;
  322. }
  323. printk("nxt2002: timeout error writing tuner\n");
  324. break;
  325. default:
  326. return -EINVAL;
  327. break;
  328. }
  329. return 0;
  330. }
  331. static void nxt200x_agc_reset(struct nxt200x_state* state)
  332. {
  333. u8 buf;
  334. dprintk("%s\n", __FUNCTION__);
  335. switch (state->demod_chip) {
  336. case NXT2002:
  337. buf = 0x08;
  338. nxt200x_writebytes(state, 0x08, &buf, 1);
  339. buf = 0x00;
  340. nxt200x_writebytes(state, 0x08, &buf, 1);
  341. break;
  342. case NXT2004:
  343. nxt200x_readreg_multibyte(state, 0x08, &buf, 1);
  344. buf = 0x08;
  345. nxt200x_writereg_multibyte(state, 0x08, &buf, 1);
  346. buf = 0x00;
  347. nxt200x_writereg_multibyte(state, 0x08, &buf, 1);
  348. break;
  349. default:
  350. break;
  351. }
  352. return;
  353. }
  354. static int nxt2002_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
  355. {
  356. struct nxt200x_state* state = fe->demodulator_priv;
  357. u8 buf[3], written = 0, chunkpos = 0;
  358. u16 rambase, position, crc = 0;
  359. dprintk("%s\n", __FUNCTION__);
  360. dprintk("Firmware is %zu bytes\n", fw->size);
  361. /* Get the RAM base for this nxt2002 */
  362. nxt200x_readbytes(state, 0x10, buf, 1);
  363. if (buf[0] & 0x10)
  364. rambase = 0x1000;
  365. else
  366. rambase = 0x0000;
  367. dprintk("rambase on this nxt2002 is %04X\n", rambase);
  368. /* Hold the micro in reset while loading firmware */
  369. buf[0] = 0x80;
  370. nxt200x_writebytes(state, 0x2B, buf, 1);
  371. for (position = 0; position < fw->size; position++) {
  372. if (written == 0) {
  373. crc = 0;
  374. chunkpos = 0x28;
  375. buf[0] = ((rambase + position) >> 8);
  376. buf[1] = (rambase + position) & 0xFF;
  377. buf[2] = 0x81;
  378. /* write starting address */
  379. nxt200x_writebytes(state, 0x29, buf, 3);
  380. }
  381. written++;
  382. chunkpos++;
  383. if ((written % 4) == 0)
  384. nxt200x_writebytes(state, chunkpos, &fw->data[position-3], 4);
  385. crc = nxt200x_crc(crc, fw->data[position]);
  386. if ((written == 255) || (position+1 == fw->size)) {
  387. /* write remaining bytes of firmware */
  388. nxt200x_writebytes(state, chunkpos+4-(written %4),
  389. &fw->data[position-(written %4) + 1],
  390. written %4);
  391. buf[0] = crc << 8;
  392. buf[1] = crc & 0xFF;
  393. /* write crc */
  394. nxt200x_writebytes(state, 0x2C, buf, 2);
  395. /* do a read to stop things */
  396. nxt200x_readbytes(state, 0x2A, buf, 1);
  397. /* set transfer mode to complete */
  398. buf[0] = 0x80;
  399. nxt200x_writebytes(state, 0x2B, buf, 1);
  400. written = 0;
  401. }
  402. }
  403. return 0;
  404. };
  405. static int nxt2004_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
  406. {
  407. struct nxt200x_state* state = fe->demodulator_priv;
  408. u8 buf[3];
  409. u16 rambase, position, crc=0;
  410. dprintk("%s\n", __FUNCTION__);
  411. dprintk("Firmware is %zu bytes\n", fw->size);
  412. /* set rambase */
  413. rambase = 0x1000;
  414. /* hold the micro in reset while loading firmware */
  415. buf[0] = 0x80;
  416. nxt200x_writebytes(state, 0x2B, buf,1);
  417. /* calculate firmware CRC */
  418. for (position = 0; position < fw->size; position++) {
  419. crc = nxt200x_crc(crc, fw->data[position]);
  420. }
  421. buf[0] = rambase >> 8;
  422. buf[1] = rambase & 0xFF;
  423. buf[2] = 0x81;
  424. /* write starting address */
  425. nxt200x_writebytes(state,0x29,buf,3);
  426. for (position = 0; position < fw->size;) {
  427. nxt200x_writebytes(state, 0x2C, &fw->data[position],
  428. fw->size-position > 255 ? 255 : fw->size-position);
  429. position += (fw->size-position > 255 ? 255 : fw->size-position);
  430. }
  431. buf[0] = crc >> 8;
  432. buf[1] = crc & 0xFF;
  433. dprintk("firmware crc is 0x%02X 0x%02X\n", buf[0], buf[1]);
  434. /* write crc */
  435. nxt200x_writebytes(state, 0x2C, buf,2);
  436. /* do a read to stop things */
  437. nxt200x_readbytes(state, 0x2C, buf, 1);
  438. /* set transfer mode to complete */
  439. buf[0] = 0x80;
  440. nxt200x_writebytes(state, 0x2B, buf,1);
  441. return 0;
  442. };
  443. static int nxt200x_setup_frontend_parameters (struct dvb_frontend* fe,
  444. struct dvb_frontend_parameters *p)
  445. {
  446. struct nxt200x_state* state = fe->demodulator_priv;
  447. u8 buf[4];
  448. /* stop the micro first */
  449. nxt200x_microcontroller_stop(state);
  450. if (state->demod_chip == NXT2004) {
  451. /* make sure demod is set to digital */
  452. buf[0] = 0x04;
  453. nxt200x_writebytes(state, 0x14, buf, 1);
  454. buf[0] = 0x00;
  455. nxt200x_writebytes(state, 0x17, buf, 1);
  456. }
  457. /* get tuning information */
  458. dvb_pll_configure(state->config->pll_desc, buf, p->frequency, 0);
  459. /* set additional params */
  460. switch (p->u.vsb.modulation) {
  461. case QAM_64:
  462. case QAM_256:
  463. /* Set punctured clock for QAM */
  464. /* This is just a guess since I am unable to test it */
  465. if (state->config->set_ts_params)
  466. state->config->set_ts_params(fe, 1);
  467. /* set input */
  468. if (state->config->set_pll_input)
  469. state->config->set_pll_input(buf, 1);
  470. break;
  471. case VSB_8:
  472. /* Set non-punctured clock for VSB */
  473. if (state->config->set_ts_params)
  474. state->config->set_ts_params(fe, 0);
  475. /* set input */
  476. if (state->config->set_pll_input)
  477. state->config->set_pll_input(buf, 0);
  478. break;
  479. default:
  480. return -EINVAL;
  481. break;
  482. }
  483. /* write frequency information */
  484. nxt200x_writetuner(state, buf);
  485. /* reset the agc now that tuning has been completed */
  486. nxt200x_agc_reset(state);
  487. /* set target power level */
  488. switch (p->u.vsb.modulation) {
  489. case QAM_64:
  490. case QAM_256:
  491. buf[0] = 0x74;
  492. break;
  493. case VSB_8:
  494. buf[0] = 0x70;
  495. break;
  496. default:
  497. return -EINVAL;
  498. break;
  499. }
  500. nxt200x_writebytes(state, 0x42, buf, 1);
  501. /* configure sdm */
  502. switch (state->demod_chip) {
  503. case NXT2002:
  504. buf[0] = 0x87;
  505. break;
  506. case NXT2004:
  507. buf[0] = 0x07;
  508. break;
  509. default:
  510. return -EINVAL;
  511. break;
  512. }
  513. nxt200x_writebytes(state, 0x57, buf, 1);
  514. /* write sdm1 input */
  515. buf[0] = 0x10;
  516. buf[1] = 0x00;
  517. switch (state->demod_chip) {
  518. case NXT2002:
  519. nxt200x_writereg_multibyte(state, 0x58, buf, 2);
  520. break;
  521. case NXT2004:
  522. nxt200x_writebytes(state, 0x58, buf, 2);
  523. break;
  524. default:
  525. return -EINVAL;
  526. break;
  527. }
  528. /* write sdmx input */
  529. switch (p->u.vsb.modulation) {
  530. case QAM_64:
  531. buf[0] = 0x68;
  532. break;
  533. case QAM_256:
  534. buf[0] = 0x64;
  535. break;
  536. case VSB_8:
  537. buf[0] = 0x60;
  538. break;
  539. default:
  540. return -EINVAL;
  541. break;
  542. }
  543. buf[1] = 0x00;
  544. switch (state->demod_chip) {
  545. case NXT2002:
  546. nxt200x_writereg_multibyte(state, 0x5C, buf, 2);
  547. break;
  548. case NXT2004:
  549. nxt200x_writebytes(state, 0x5C, buf, 2);
  550. break;
  551. default:
  552. return -EINVAL;
  553. break;
  554. }
  555. /* write adc power lpf fc */
  556. buf[0] = 0x05;
  557. nxt200x_writebytes(state, 0x43, buf, 1);
  558. if (state->demod_chip == NXT2004) {
  559. /* write ??? */
  560. buf[0] = 0x00;
  561. buf[1] = 0x00;
  562. nxt200x_writebytes(state, 0x46, buf, 2);
  563. }
  564. /* write accumulator2 input */
  565. buf[0] = 0x80;
  566. buf[1] = 0x00;
  567. switch (state->demod_chip) {
  568. case NXT2002:
  569. nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
  570. break;
  571. case NXT2004:
  572. nxt200x_writebytes(state, 0x4B, buf, 2);
  573. break;
  574. default:
  575. return -EINVAL;
  576. break;
  577. }
  578. /* write kg1 */
  579. buf[0] = 0x00;
  580. nxt200x_writebytes(state, 0x4D, buf, 1);
  581. /* write sdm12 lpf fc */
  582. buf[0] = 0x44;
  583. nxt200x_writebytes(state, 0x55, buf, 1);
  584. /* write agc control reg */
  585. buf[0] = 0x04;
  586. nxt200x_writebytes(state, 0x41, buf, 1);
  587. if (state->demod_chip == NXT2004) {
  588. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  589. buf[0] = 0x24;
  590. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  591. /* soft reset? */
  592. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  593. buf[0] = 0x10;
  594. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  595. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  596. buf[0] = 0x00;
  597. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  598. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  599. buf[0] = 0x04;
  600. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  601. buf[0] = 0x00;
  602. nxt200x_writereg_multibyte(state, 0x81, buf, 1);
  603. buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
  604. nxt200x_writereg_multibyte(state, 0x82, buf, 3);
  605. nxt200x_readreg_multibyte(state, 0x88, buf, 1);
  606. buf[0] = 0x11;
  607. nxt200x_writereg_multibyte(state, 0x88, buf, 1);
  608. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  609. buf[0] = 0x44;
  610. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  611. }
  612. /* write agc ucgp0 */
  613. switch (p->u.vsb.modulation) {
  614. case QAM_64:
  615. buf[0] = 0x02;
  616. break;
  617. case QAM_256:
  618. buf[0] = 0x03;
  619. break;
  620. case VSB_8:
  621. buf[0] = 0x00;
  622. break;
  623. default:
  624. return -EINVAL;
  625. break;
  626. }
  627. nxt200x_writebytes(state, 0x30, buf, 1);
  628. /* write agc control reg */
  629. buf[0] = 0x00;
  630. nxt200x_writebytes(state, 0x41, buf, 1);
  631. /* write accumulator2 input */
  632. buf[0] = 0x80;
  633. buf[1] = 0x00;
  634. switch (state->demod_chip) {
  635. case NXT2002:
  636. nxt200x_writereg_multibyte(state, 0x49, buf, 2);
  637. nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
  638. break;
  639. case NXT2004:
  640. nxt200x_writebytes(state, 0x49, buf, 2);
  641. nxt200x_writebytes(state, 0x4B, buf, 2);
  642. break;
  643. default:
  644. return -EINVAL;
  645. break;
  646. }
  647. /* write agc control reg */
  648. buf[0] = 0x04;
  649. nxt200x_writebytes(state, 0x41, buf, 1);
  650. nxt200x_microcontroller_start(state);
  651. if (state->demod_chip == NXT2004) {
  652. nxt2004_microcontroller_init(state);
  653. /* ???? */
  654. buf[0] = 0xF0;
  655. buf[1] = 0x00;
  656. nxt200x_writebytes(state, 0x5C, buf, 2);
  657. }
  658. /* adjacent channel detection should be done here, but I don't
  659. have any stations with this need so I cannot test it */
  660. return 0;
  661. }
  662. static int nxt200x_read_status(struct dvb_frontend* fe, fe_status_t* status)
  663. {
  664. struct nxt200x_state* state = fe->demodulator_priv;
  665. u8 lock;
  666. nxt200x_readbytes(state, 0x31, &lock, 1);
  667. *status = 0;
  668. if (lock & 0x20) {
  669. *status |= FE_HAS_SIGNAL;
  670. *status |= FE_HAS_CARRIER;
  671. *status |= FE_HAS_VITERBI;
  672. *status |= FE_HAS_SYNC;
  673. *status |= FE_HAS_LOCK;
  674. }
  675. return 0;
  676. }
  677. static int nxt200x_read_ber(struct dvb_frontend* fe, u32* ber)
  678. {
  679. struct nxt200x_state* state = fe->demodulator_priv;
  680. u8 b[3];
  681. nxt200x_readreg_multibyte(state, 0xE6, b, 3);
  682. *ber = ((b[0] << 8) + b[1]) * 8;
  683. return 0;
  684. }
  685. static int nxt200x_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  686. {
  687. struct nxt200x_state* state = fe->demodulator_priv;
  688. u8 b[2];
  689. u16 temp = 0;
  690. /* setup to read cluster variance */
  691. b[0] = 0x00;
  692. nxt200x_writebytes(state, 0xA1, b, 1);
  693. /* get multreg val */
  694. nxt200x_readreg_multibyte(state, 0xA6, b, 2);
  695. temp = (b[0] << 8) | b[1];
  696. *strength = ((0x7FFF - temp) & 0x0FFF) * 16;
  697. return 0;
  698. }
  699. static int nxt200x_read_snr(struct dvb_frontend* fe, u16* snr)
  700. {
  701. struct nxt200x_state* state = fe->demodulator_priv;
  702. u8 b[2];
  703. u16 temp = 0, temp2;
  704. u32 snrdb = 0;
  705. /* setup to read cluster variance */
  706. b[0] = 0x00;
  707. nxt200x_writebytes(state, 0xA1, b, 1);
  708. /* get multreg val from 0xA6 */
  709. nxt200x_readreg_multibyte(state, 0xA6, b, 2);
  710. temp = (b[0] << 8) | b[1];
  711. temp2 = 0x7FFF - temp;
  712. /* snr will be in db */
  713. if (temp2 > 0x7F00)
  714. snrdb = 1000*24 + ( 1000*(30-24) * ( temp2 - 0x7F00 ) / ( 0x7FFF - 0x7F00 ) );
  715. else if (temp2 > 0x7EC0)
  716. snrdb = 1000*18 + ( 1000*(24-18) * ( temp2 - 0x7EC0 ) / ( 0x7F00 - 0x7EC0 ) );
  717. else if (temp2 > 0x7C00)
  718. snrdb = 1000*12 + ( 1000*(18-12) * ( temp2 - 0x7C00 ) / ( 0x7EC0 - 0x7C00 ) );
  719. else
  720. snrdb = 1000*0 + ( 1000*(12-0) * ( temp2 - 0 ) / ( 0x7C00 - 0 ) );
  721. /* the value reported back from the frontend will be FFFF=32db 0000=0db */
  722. *snr = snrdb * (0xFFFF/32000);
  723. return 0;
  724. }
  725. static int nxt200x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  726. {
  727. struct nxt200x_state* state = fe->demodulator_priv;
  728. u8 b[3];
  729. nxt200x_readreg_multibyte(state, 0xE6, b, 3);
  730. *ucblocks = b[2];
  731. return 0;
  732. }
  733. static int nxt200x_sleep(struct dvb_frontend* fe)
  734. {
  735. return 0;
  736. }
  737. static int nxt2002_init(struct dvb_frontend* fe)
  738. {
  739. struct nxt200x_state* state = fe->demodulator_priv;
  740. const struct firmware *fw;
  741. int ret;
  742. u8 buf[2];
  743. /* request the firmware, this will block until someone uploads it */
  744. printk("nxt2002: Waiting for firmware upload (%s)...\n", NXT2002_DEFAULT_FIRMWARE);
  745. ret = request_firmware(&fw, NXT2002_DEFAULT_FIRMWARE, &state->i2c->dev);
  746. printk("nxt2002: Waiting for firmware upload(2)...\n");
  747. if (ret) {
  748. printk("nxt2002: No firmware uploaded (timeout or file not found?)\n");
  749. return ret;
  750. }
  751. ret = nxt2002_load_firmware(fe, fw);
  752. if (ret) {
  753. printk("nxt2002: Writing firmware to device failed\n");
  754. release_firmware(fw);
  755. return ret;
  756. }
  757. printk("nxt2002: Firmware upload complete\n");
  758. /* Put the micro into reset */
  759. nxt200x_microcontroller_stop(state);
  760. /* ensure transfer is complete */
  761. buf[0]=0x00;
  762. nxt200x_writebytes(state, 0x2B, buf, 1);
  763. /* Put the micro into reset for real this time */
  764. nxt200x_microcontroller_stop(state);
  765. /* soft reset everything (agc,frontend,eq,fec)*/
  766. buf[0] = 0x0F;
  767. nxt200x_writebytes(state, 0x08, buf, 1);
  768. buf[0] = 0x00;
  769. nxt200x_writebytes(state, 0x08, buf, 1);
  770. /* write agc sdm configure */
  771. buf[0] = 0xF1;
  772. nxt200x_writebytes(state, 0x57, buf, 1);
  773. /* write mod output format */
  774. buf[0] = 0x20;
  775. nxt200x_writebytes(state, 0x09, buf, 1);
  776. /* write fec mpeg mode */
  777. buf[0] = 0x7E;
  778. buf[1] = 0x00;
  779. nxt200x_writebytes(state, 0xE9, buf, 2);
  780. /* write mux selection */
  781. buf[0] = 0x00;
  782. nxt200x_writebytes(state, 0xCC, buf, 1);
  783. return 0;
  784. }
  785. static int nxt2004_init(struct dvb_frontend* fe)
  786. {
  787. struct nxt200x_state* state = fe->demodulator_priv;
  788. const struct firmware *fw;
  789. int ret;
  790. u8 buf[3];
  791. /* ??? */
  792. buf[0]=0x00;
  793. nxt200x_writebytes(state, 0x1E, buf, 1);
  794. /* request the firmware, this will block until someone uploads it */
  795. printk("nxt2004: Waiting for firmware upload (%s)...\n", NXT2004_DEFAULT_FIRMWARE);
  796. ret = request_firmware(&fw, NXT2004_DEFAULT_FIRMWARE, &state->i2c->dev);
  797. printk("nxt2004: Waiting for firmware upload(2)...\n");
  798. if (ret) {
  799. printk("nxt2004: No firmware uploaded (timeout or file not found?)\n");
  800. return ret;
  801. }
  802. ret = nxt2004_load_firmware(fe, fw);
  803. if (ret) {
  804. printk("nxt2004: Writing firmware to device failed\n");
  805. release_firmware(fw);
  806. return ret;
  807. }
  808. printk("nxt2004: Firmware upload complete\n");
  809. /* ensure transfer is complete */
  810. buf[0] = 0x01;
  811. nxt200x_writebytes(state, 0x19, buf, 1);
  812. nxt2004_microcontroller_init(state);
  813. nxt200x_microcontroller_stop(state);
  814. nxt200x_microcontroller_stop(state);
  815. nxt2004_microcontroller_init(state);
  816. nxt200x_microcontroller_stop(state);
  817. /* soft reset everything (agc,frontend,eq,fec)*/
  818. buf[0] = 0xFF;
  819. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  820. buf[0] = 0x00;
  821. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  822. /* write agc sdm configure */
  823. buf[0] = 0xD7;
  824. nxt200x_writebytes(state, 0x57, buf, 1);
  825. /* ???*/
  826. buf[0] = 0x07;
  827. buf[1] = 0xfe;
  828. nxt200x_writebytes(state, 0x35, buf, 2);
  829. buf[0] = 0x12;
  830. nxt200x_writebytes(state, 0x34, buf, 1);
  831. buf[0] = 0x80;
  832. nxt200x_writebytes(state, 0x21, buf, 1);
  833. /* ???*/
  834. buf[0] = 0x21;
  835. nxt200x_writebytes(state, 0x0A, buf, 1);
  836. /* ???*/
  837. buf[0] = 0x01;
  838. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  839. /* write fec mpeg mode */
  840. buf[0] = 0x7E;
  841. buf[1] = 0x00;
  842. nxt200x_writebytes(state, 0xE9, buf, 2);
  843. /* write mux selection */
  844. buf[0] = 0x00;
  845. nxt200x_writebytes(state, 0xCC, buf, 1);
  846. /* ???*/
  847. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  848. buf[0] = 0x00;
  849. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  850. /* soft reset? */
  851. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  852. buf[0] = 0x10;
  853. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  854. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  855. buf[0] = 0x00;
  856. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  857. /* ???*/
  858. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  859. buf[0] = 0x01;
  860. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  861. buf[0] = 0x70;
  862. nxt200x_writereg_multibyte(state, 0x81, buf, 1);
  863. buf[0] = 0x31; buf[1] = 0x5E; buf[2] = 0x66;
  864. nxt200x_writereg_multibyte(state, 0x82, buf, 3);
  865. nxt200x_readreg_multibyte(state, 0x88, buf, 1);
  866. buf[0] = 0x11;
  867. nxt200x_writereg_multibyte(state, 0x88, buf, 1);
  868. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  869. buf[0] = 0x40;
  870. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  871. nxt200x_readbytes(state, 0x10, buf, 1);
  872. buf[0] = 0x10;
  873. nxt200x_writebytes(state, 0x10, buf, 1);
  874. nxt200x_readbytes(state, 0x0A, buf, 1);
  875. buf[0] = 0x21;
  876. nxt200x_writebytes(state, 0x0A, buf, 1);
  877. nxt2004_microcontroller_init(state);
  878. buf[0] = 0x21;
  879. nxt200x_writebytes(state, 0x0A, buf, 1);
  880. buf[0] = 0x7E;
  881. nxt200x_writebytes(state, 0xE9, buf, 1);
  882. buf[0] = 0x00;
  883. nxt200x_writebytes(state, 0xEA, buf, 1);
  884. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  885. buf[0] = 0x00;
  886. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  887. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  888. buf[0] = 0x00;
  889. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  890. /* soft reset? */
  891. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  892. buf[0] = 0x10;
  893. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  894. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  895. buf[0] = 0x00;
  896. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  897. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  898. buf[0] = 0x04;
  899. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  900. buf[0] = 0x00;
  901. nxt200x_writereg_multibyte(state, 0x81, buf, 1);
  902. buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
  903. nxt200x_writereg_multibyte(state, 0x82, buf, 3);
  904. nxt200x_readreg_multibyte(state, 0x88, buf, 1);
  905. buf[0] = 0x11;
  906. nxt200x_writereg_multibyte(state, 0x88, buf, 1);
  907. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  908. buf[0] = 0x44;
  909. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  910. /* initialize tuner */
  911. nxt200x_readbytes(state, 0x10, buf, 1);
  912. buf[0] = 0x12;
  913. nxt200x_writebytes(state, 0x10, buf, 1);
  914. buf[0] = 0x04;
  915. nxt200x_writebytes(state, 0x13, buf, 1);
  916. buf[0] = 0x00;
  917. nxt200x_writebytes(state, 0x16, buf, 1);
  918. buf[0] = 0x04;
  919. nxt200x_writebytes(state, 0x14, buf, 1);
  920. buf[0] = 0x00;
  921. nxt200x_writebytes(state, 0x14, buf, 1);
  922. nxt200x_writebytes(state, 0x17, buf, 1);
  923. nxt200x_writebytes(state, 0x14, buf, 1);
  924. nxt200x_writebytes(state, 0x17, buf, 1);
  925. return 0;
  926. }
  927. static int nxt200x_init(struct dvb_frontend* fe)
  928. {
  929. struct nxt200x_state* state = fe->demodulator_priv;
  930. int ret = 0;
  931. if (!state->initialised) {
  932. switch (state->demod_chip) {
  933. case NXT2002:
  934. ret = nxt2002_init(fe);
  935. break;
  936. case NXT2004:
  937. ret = nxt2004_init(fe);
  938. break;
  939. default:
  940. return -EINVAL;
  941. break;
  942. }
  943. state->initialised = 1;
  944. }
  945. return ret;
  946. }
  947. static int nxt200x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  948. {
  949. fesettings->min_delay_ms = 500;
  950. fesettings->step_size = 0;
  951. fesettings->max_drift = 0;
  952. return 0;
  953. }
  954. static void nxt200x_release(struct dvb_frontend* fe)
  955. {
  956. struct nxt200x_state* state = fe->demodulator_priv;
  957. kfree(state);
  958. }
  959. static struct dvb_frontend_ops nxt200x_ops;
  960. struct dvb_frontend* nxt200x_attach(const struct nxt200x_config* config,
  961. struct i2c_adapter* i2c)
  962. {
  963. struct nxt200x_state* state = NULL;
  964. u8 buf [] = {0,0,0,0,0};
  965. /* allocate memory for the internal state */
  966. state = kzalloc(sizeof(struct nxt200x_state), GFP_KERNEL);
  967. if (state == NULL)
  968. goto error;
  969. /* setup the state */
  970. state->config = config;
  971. state->i2c = i2c;
  972. memcpy(&state->ops, &nxt200x_ops, sizeof(struct dvb_frontend_ops));
  973. state->initialised = 0;
  974. /* read card id */
  975. nxt200x_readbytes(state, 0x00, buf, 5);
  976. dprintk("NXT info: %02X %02X %02X %02X %02X\n",
  977. buf[0], buf[1], buf[2], buf[3], buf[4]);
  978. /* set demod chip */
  979. switch (buf[0]) {
  980. case 0x04:
  981. state->demod_chip = NXT2002;
  982. printk("nxt200x: NXT2002 Detected\n");
  983. break;
  984. case 0x05:
  985. state->demod_chip = NXT2004;
  986. printk("nxt200x: NXT2004 Detected\n");
  987. break;
  988. default:
  989. goto error;
  990. }
  991. /* make sure demod chip is supported */
  992. switch (state->demod_chip) {
  993. case NXT2002:
  994. if (buf[0] != 0x04) goto error; /* device id */
  995. if (buf[1] != 0x02) goto error; /* fab id */
  996. if (buf[2] != 0x11) goto error; /* month */
  997. if (buf[3] != 0x20) goto error; /* year msb */
  998. if (buf[4] != 0x00) goto error; /* year lsb */
  999. break;
  1000. case NXT2004:
  1001. if (buf[0] != 0x05) goto error; /* device id */
  1002. break;
  1003. default:
  1004. goto error;
  1005. }
  1006. /* create dvb_frontend */
  1007. state->frontend.ops = &state->ops;
  1008. state->frontend.demodulator_priv = state;
  1009. return &state->frontend;
  1010. error:
  1011. kfree(state);
  1012. printk("Unknown/Unsupported NXT chip: %02X %02X %02X %02X %02X\n",
  1013. buf[0], buf[1], buf[2], buf[3], buf[4]);
  1014. return NULL;
  1015. }
  1016. static struct dvb_frontend_ops nxt200x_ops = {
  1017. .info = {
  1018. .name = "Nextwave NXT200X VSB/QAM frontend",
  1019. .type = FE_ATSC,
  1020. .frequency_min = 54000000,
  1021. .frequency_max = 860000000,
  1022. .frequency_stepsize = 166666, /* stepsize is just a guess */
  1023. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1024. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1025. FE_CAN_8VSB | FE_CAN_QAM_64 | FE_CAN_QAM_256
  1026. },
  1027. .release = nxt200x_release,
  1028. .init = nxt200x_init,
  1029. .sleep = nxt200x_sleep,
  1030. .set_frontend = nxt200x_setup_frontend_parameters,
  1031. .get_tune_settings = nxt200x_get_tune_settings,
  1032. .read_status = nxt200x_read_status,
  1033. .read_ber = nxt200x_read_ber,
  1034. .read_signal_strength = nxt200x_read_signal_strength,
  1035. .read_snr = nxt200x_read_snr,
  1036. .read_ucblocks = nxt200x_read_ucblocks,
  1037. };
  1038. module_param(debug, int, 0644);
  1039. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  1040. MODULE_DESCRIPTION("NXT200X (ATSC 8VSB & ITU-T J.83 AnnexB 64/256 QAM) Demodulator Driver");
  1041. MODULE_AUTHOR("Kirk Lapray, Michael Krufky, Jean-Francois Thibert, and Taylor Jacob");
  1042. MODULE_LICENSE("GPL");
  1043. EXPORT_SYMBOL(nxt200x_attach);