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