nxt200x.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. /*
  2. * Support for NXT2002 and NXT2004 - VSB/QAM
  3. *
  4. * Copyright (C) 2005 Kirk Lapray (kirk.lapray@gmail.com)
  5. * based on nxt2002 by Taylor Jacob <rtjacob@earthlink.net>
  6. * and nxt2004 by Jean-Francois Thibert (jeanfrancois@sagetv.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. */
  23. /*
  24. * NOTES ABOUT THIS DRIVER
  25. *
  26. * This Linux driver supports:
  27. * B2C2/BBTI Technisat Air2PC - ATSC (NXT2002)
  28. * AverTVHD MCE A180 (NXT2004)
  29. * ATI HDTV Wonder (NXT2004)
  30. *
  31. * This driver needs external firmware. Please use the command
  32. * "<kerneldir>/Documentation/dvb/get_dvb_firmware nxt2002" or
  33. * "<kerneldir>/Documentation/dvb/get_dvb_firmware nxt2004" to
  34. * download/extract the appropriate firmware, and then copy it to
  35. * /usr/lib/hotplug/firmware/ or /lib/firmware/
  36. * (depending on configuration of firmware hotplug).
  37. */
  38. #define NXT2002_DEFAULT_FIRMWARE "dvb-fe-nxt2002.fw"
  39. #define NXT2004_DEFAULT_FIRMWARE "dvb-fe-nxt2004.fw"
  40. #define CRC_CCIT_MASK 0x1021
  41. #include <linux/kernel.h>
  42. #include <linux/init.h>
  43. #include <linux/module.h>
  44. #include <linux/moduleparam.h>
  45. #include <linux/slab.h>
  46. #include <linux/string.h>
  47. #include "dvb_frontend.h"
  48. #include "dvb-pll.h"
  49. #include "nxt200x.h"
  50. struct nxt200x_state {
  51. struct i2c_adapter* i2c;
  52. struct dvb_frontend_ops ops;
  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[0], data[1], data[2], data[3]);
  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, state->config->pll_address, data, 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, state->config->pll_address, &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, 4);
  309. /* set tuner i2c address */
  310. buf = state->config->pll_address;
  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[4];
  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. dvb_pll_configure(state->config->pll_desc, buf, p->frequency, 0);
  458. /* set additional params */
  459. switch (p->u.vsb.modulation) {
  460. case QAM_64:
  461. case QAM_256:
  462. /* Set punctured clock for QAM */
  463. /* This is just a guess since I am unable to test it */
  464. if (state->config->set_ts_params)
  465. state->config->set_ts_params(fe, 1);
  466. /* set input */
  467. if (state->config->set_pll_input)
  468. state->config->set_pll_input(buf, 1);
  469. break;
  470. case VSB_8:
  471. /* Set non-punctured clock for VSB */
  472. if (state->config->set_ts_params)
  473. state->config->set_ts_params(fe, 0);
  474. /* set input */
  475. if (state->config->set_pll_input)
  476. state->config->set_pll_input(buf, 0);
  477. break;
  478. default:
  479. return -EINVAL;
  480. break;
  481. }
  482. /* write frequency information */
  483. nxt200x_writetuner(state, buf);
  484. /* reset the agc now that tuning has been completed */
  485. nxt200x_agc_reset(state);
  486. /* set target power level */
  487. switch (p->u.vsb.modulation) {
  488. case QAM_64:
  489. case QAM_256:
  490. buf[0] = 0x74;
  491. break;
  492. case VSB_8:
  493. buf[0] = 0x70;
  494. break;
  495. default:
  496. return -EINVAL;
  497. break;
  498. }
  499. nxt200x_writebytes(state, 0x42, buf, 1);
  500. /* configure sdm */
  501. switch (state->demod_chip) {
  502. case NXT2002:
  503. buf[0] = 0x87;
  504. break;
  505. case NXT2004:
  506. buf[0] = 0x07;
  507. break;
  508. default:
  509. return -EINVAL;
  510. break;
  511. }
  512. nxt200x_writebytes(state, 0x57, buf, 1);
  513. /* write sdm1 input */
  514. buf[0] = 0x10;
  515. buf[1] = 0x00;
  516. nxt200x_writebytes(state, 0x58, buf, 2);
  517. /* write sdmx input */
  518. switch (p->u.vsb.modulation) {
  519. case QAM_64:
  520. buf[0] = 0x68;
  521. break;
  522. case QAM_256:
  523. buf[0] = 0x64;
  524. break;
  525. case VSB_8:
  526. buf[0] = 0x60;
  527. break;
  528. default:
  529. return -EINVAL;
  530. break;
  531. }
  532. buf[1] = 0x00;
  533. nxt200x_writebytes(state, 0x5C, buf, 2);
  534. /* write adc power lpf fc */
  535. buf[0] = 0x05;
  536. nxt200x_writebytes(state, 0x43, buf, 1);
  537. if (state->demod_chip == NXT2004) {
  538. /* write ??? */
  539. buf[0] = 0x00;
  540. buf[1] = 0x00;
  541. nxt200x_writebytes(state, 0x46, buf, 2);
  542. }
  543. /* write accumulator2 input */
  544. buf[0] = 0x80;
  545. buf[1] = 0x00;
  546. nxt200x_writebytes(state, 0x4B, buf, 2);
  547. /* write kg1 */
  548. buf[0] = 0x00;
  549. nxt200x_writebytes(state, 0x4D, buf, 1);
  550. /* write sdm12 lpf fc */
  551. buf[0] = 0x44;
  552. nxt200x_writebytes(state, 0x55, buf, 1);
  553. /* write agc control reg */
  554. buf[0] = 0x04;
  555. nxt200x_writebytes(state, 0x41, buf, 1);
  556. if (state->demod_chip == NXT2004) {
  557. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  558. buf[0] = 0x24;
  559. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  560. /* soft reset? */
  561. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  562. buf[0] = 0x10;
  563. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  564. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  565. buf[0] = 0x00;
  566. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  567. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  568. buf[0] = 0x04;
  569. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  570. buf[0] = 0x00;
  571. nxt200x_writereg_multibyte(state, 0x81, buf, 1);
  572. buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
  573. nxt200x_writereg_multibyte(state, 0x82, buf, 3);
  574. nxt200x_readreg_multibyte(state, 0x88, buf, 1);
  575. buf[0] = 0x11;
  576. nxt200x_writereg_multibyte(state, 0x88, buf, 1);
  577. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  578. buf[0] = 0x44;
  579. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  580. }
  581. /* write agc ucgp0 */
  582. switch (p->u.vsb.modulation) {
  583. case QAM_64:
  584. buf[0] = 0x02;
  585. break;
  586. case QAM_256:
  587. buf[0] = 0x03;
  588. break;
  589. case VSB_8:
  590. buf[0] = 0x00;
  591. break;
  592. default:
  593. return -EINVAL;
  594. break;
  595. }
  596. nxt200x_writebytes(state, 0x30, buf, 1);
  597. /* write agc control reg */
  598. buf[0] = 0x00;
  599. nxt200x_writebytes(state, 0x41, buf, 1);
  600. /* write accumulator2 input */
  601. buf[0] = 0x80;
  602. buf[1] = 0x00;
  603. nxt200x_writebytes(state, 0x49, buf,2);
  604. nxt200x_writebytes(state, 0x4B, buf,2);
  605. /* write agc control reg */
  606. buf[0] = 0x04;
  607. nxt200x_writebytes(state, 0x41, buf, 1);
  608. nxt200x_microcontroller_start(state);
  609. if (state->demod_chip == NXT2004) {
  610. nxt2004_microcontroller_init(state);
  611. /* ???? */
  612. buf[0] = 0xF0;
  613. buf[1] = 0x00;
  614. nxt200x_writebytes(state, 0x5C, buf, 2);
  615. }
  616. /* adjacent channel detection should be done here, but I don't
  617. have any stations with this need so I cannot test it */
  618. return 0;
  619. }
  620. static int nxt200x_read_status(struct dvb_frontend* fe, fe_status_t* status)
  621. {
  622. struct nxt200x_state* state = fe->demodulator_priv;
  623. u8 lock;
  624. nxt200x_readbytes(state, 0x31, &lock, 1);
  625. *status = 0;
  626. if (lock & 0x20) {
  627. *status |= FE_HAS_SIGNAL;
  628. *status |= FE_HAS_CARRIER;
  629. *status |= FE_HAS_VITERBI;
  630. *status |= FE_HAS_SYNC;
  631. *status |= FE_HAS_LOCK;
  632. }
  633. return 0;
  634. }
  635. static int nxt200x_read_ber(struct dvb_frontend* fe, u32* ber)
  636. {
  637. struct nxt200x_state* state = fe->demodulator_priv;
  638. u8 b[3];
  639. nxt200x_readreg_multibyte(state, 0xE6, b, 3);
  640. *ber = ((b[0] << 8) + b[1]) * 8;
  641. return 0;
  642. }
  643. static int nxt200x_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  644. {
  645. struct nxt200x_state* state = fe->demodulator_priv;
  646. u8 b[2];
  647. u16 temp = 0;
  648. /* setup to read cluster variance */
  649. b[0] = 0x00;
  650. nxt200x_writebytes(state, 0xA1, b, 1);
  651. /* get multreg val */
  652. nxt200x_readreg_multibyte(state, 0xA6, b, 2);
  653. temp = (b[0] << 8) | b[1];
  654. *strength = ((0x7FFF - temp) & 0x0FFF) * 16;
  655. return 0;
  656. }
  657. static int nxt200x_read_snr(struct dvb_frontend* fe, u16* snr)
  658. {
  659. struct nxt200x_state* state = fe->demodulator_priv;
  660. u8 b[2];
  661. u16 temp = 0, temp2;
  662. u32 snrdb = 0;
  663. /* setup to read cluster variance */
  664. b[0] = 0x00;
  665. nxt200x_writebytes(state, 0xA1, b, 1);
  666. /* get multreg val from 0xA6 */
  667. nxt200x_readreg_multibyte(state, 0xA6, b, 2);
  668. temp = (b[0] << 8) | b[1];
  669. temp2 = 0x7FFF - temp;
  670. /* snr will be in db */
  671. if (temp2 > 0x7F00)
  672. snrdb = 1000*24 + ( 1000*(30-24) * ( temp2 - 0x7F00 ) / ( 0x7FFF - 0x7F00 ) );
  673. else if (temp2 > 0x7EC0)
  674. snrdb = 1000*18 + ( 1000*(24-18) * ( temp2 - 0x7EC0 ) / ( 0x7F00 - 0x7EC0 ) );
  675. else if (temp2 > 0x7C00)
  676. snrdb = 1000*12 + ( 1000*(18-12) * ( temp2 - 0x7C00 ) / ( 0x7EC0 - 0x7C00 ) );
  677. else
  678. snrdb = 1000*0 + ( 1000*(12-0) * ( temp2 - 0 ) / ( 0x7C00 - 0 ) );
  679. /* the value reported back from the frontend will be FFFF=32db 0000=0db */
  680. *snr = snrdb * (0xFFFF/32000);
  681. return 0;
  682. }
  683. static int nxt200x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  684. {
  685. struct nxt200x_state* state = fe->demodulator_priv;
  686. u8 b[3];
  687. nxt200x_readreg_multibyte(state, 0xE6, b, 3);
  688. *ucblocks = b[2];
  689. return 0;
  690. }
  691. static int nxt200x_sleep(struct dvb_frontend* fe)
  692. {
  693. return 0;
  694. }
  695. static int nxt2002_init(struct dvb_frontend* fe)
  696. {
  697. struct nxt200x_state* state = fe->demodulator_priv;
  698. const struct firmware *fw;
  699. int ret;
  700. u8 buf[2];
  701. /* request the firmware, this will block until someone uploads it */
  702. printk("nxt2002: Waiting for firmware upload (%s)...\n", NXT2002_DEFAULT_FIRMWARE);
  703. ret = request_firmware(&fw, NXT2002_DEFAULT_FIRMWARE, &state->i2c->dev);
  704. printk("nxt2002: Waiting for firmware upload(2)...\n");
  705. if (ret) {
  706. printk("nxt2002: No firmware uploaded (timeout or file not found?)\n");
  707. return ret;
  708. }
  709. ret = nxt2002_load_firmware(fe, fw);
  710. if (ret) {
  711. printk("nxt2002: Writing firmware to device failed\n");
  712. release_firmware(fw);
  713. return ret;
  714. }
  715. printk("nxt2002: Firmware upload complete\n");
  716. /* Put the micro into reset */
  717. nxt200x_microcontroller_stop(state);
  718. /* ensure transfer is complete */
  719. buf[0]=0x00;
  720. nxt200x_writebytes(state, 0x2B, buf, 1);
  721. /* Put the micro into reset for real this time */
  722. nxt200x_microcontroller_stop(state);
  723. /* soft reset everything (agc,frontend,eq,fec)*/
  724. buf[0] = 0x0F;
  725. nxt200x_writebytes(state, 0x08, buf, 1);
  726. buf[0] = 0x00;
  727. nxt200x_writebytes(state, 0x08, buf, 1);
  728. /* write agc sdm configure */
  729. buf[0] = 0xF1;
  730. nxt200x_writebytes(state, 0x57, buf, 1);
  731. /* write mod output format */
  732. buf[0] = 0x20;
  733. nxt200x_writebytes(state, 0x09, buf, 1);
  734. /* write fec mpeg mode */
  735. buf[0] = 0x7E;
  736. buf[1] = 0x00;
  737. nxt200x_writebytes(state, 0xE9, buf, 2);
  738. /* write mux selection */
  739. buf[0] = 0x00;
  740. nxt200x_writebytes(state, 0xCC, buf, 1);
  741. return 0;
  742. }
  743. static int nxt2004_init(struct dvb_frontend* fe)
  744. {
  745. struct nxt200x_state* state = fe->demodulator_priv;
  746. const struct firmware *fw;
  747. int ret;
  748. u8 buf[3];
  749. /* ??? */
  750. buf[0]=0x00;
  751. nxt200x_writebytes(state, 0x1E, buf, 1);
  752. /* request the firmware, this will block until someone uploads it */
  753. printk("nxt2004: Waiting for firmware upload (%s)...\n", NXT2004_DEFAULT_FIRMWARE);
  754. ret = request_firmware(&fw, NXT2004_DEFAULT_FIRMWARE, &state->i2c->dev);
  755. printk("nxt2004: Waiting for firmware upload(2)...\n");
  756. if (ret) {
  757. printk("nxt2004: No firmware uploaded (timeout or file not found?)\n");
  758. return ret;
  759. }
  760. ret = nxt2004_load_firmware(fe, fw);
  761. if (ret) {
  762. printk("nxt2004: Writing firmware to device failed\n");
  763. release_firmware(fw);
  764. return ret;
  765. }
  766. printk("nxt2004: Firmware upload complete\n");
  767. /* ensure transfer is complete */
  768. buf[0] = 0x01;
  769. nxt200x_writebytes(state, 0x19, buf, 1);
  770. nxt2004_microcontroller_init(state);
  771. nxt200x_microcontroller_stop(state);
  772. nxt200x_microcontroller_stop(state);
  773. nxt2004_microcontroller_init(state);
  774. nxt200x_microcontroller_stop(state);
  775. /* soft reset everything (agc,frontend,eq,fec)*/
  776. buf[0] = 0xFF;
  777. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  778. buf[0] = 0x00;
  779. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  780. /* write agc sdm configure */
  781. buf[0] = 0xD7;
  782. nxt200x_writebytes(state, 0x57, buf, 1);
  783. /* ???*/
  784. buf[0] = 0x07;
  785. buf[1] = 0xfe;
  786. nxt200x_writebytes(state, 0x35, buf, 2);
  787. buf[0] = 0x12;
  788. nxt200x_writebytes(state, 0x34, buf, 1);
  789. buf[0] = 0x80;
  790. nxt200x_writebytes(state, 0x21, buf, 1);
  791. /* ???*/
  792. buf[0] = 0x21;
  793. nxt200x_writebytes(state, 0x0A, buf, 1);
  794. /* ???*/
  795. buf[0] = 0x01;
  796. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  797. /* write fec mpeg mode */
  798. buf[0] = 0x7E;
  799. buf[1] = 0x00;
  800. nxt200x_writebytes(state, 0xE9, buf, 2);
  801. /* write mux selection */
  802. buf[0] = 0x00;
  803. nxt200x_writebytes(state, 0xCC, buf, 1);
  804. /* ???*/
  805. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  806. buf[0] = 0x00;
  807. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  808. /* soft reset? */
  809. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  810. buf[0] = 0x10;
  811. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  812. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  813. buf[0] = 0x00;
  814. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  815. /* ???*/
  816. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  817. buf[0] = 0x01;
  818. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  819. buf[0] = 0x70;
  820. nxt200x_writereg_multibyte(state, 0x81, buf, 1);
  821. buf[0] = 0x31; buf[1] = 0x5E; buf[2] = 0x66;
  822. nxt200x_writereg_multibyte(state, 0x82, buf, 3);
  823. nxt200x_readreg_multibyte(state, 0x88, buf, 1);
  824. buf[0] = 0x11;
  825. nxt200x_writereg_multibyte(state, 0x88, buf, 1);
  826. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  827. buf[0] = 0x40;
  828. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  829. nxt200x_readbytes(state, 0x10, buf, 1);
  830. buf[0] = 0x10;
  831. nxt200x_writebytes(state, 0x10, buf, 1);
  832. nxt200x_readbytes(state, 0x0A, buf, 1);
  833. buf[0] = 0x21;
  834. nxt200x_writebytes(state, 0x0A, buf, 1);
  835. nxt2004_microcontroller_init(state);
  836. buf[0] = 0x21;
  837. nxt200x_writebytes(state, 0x0A, buf, 1);
  838. buf[0] = 0x7E;
  839. nxt200x_writebytes(state, 0xE9, buf, 1);
  840. buf[0] = 0x00;
  841. nxt200x_writebytes(state, 0xEA, buf, 1);
  842. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  843. buf[0] = 0x00;
  844. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  845. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  846. buf[0] = 0x00;
  847. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  848. /* soft reset? */
  849. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  850. buf[0] = 0x10;
  851. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  852. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  853. buf[0] = 0x00;
  854. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  855. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  856. buf[0] = 0x04;
  857. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  858. buf[0] = 0x00;
  859. nxt200x_writereg_multibyte(state, 0x81, buf, 1);
  860. buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
  861. nxt200x_writereg_multibyte(state, 0x82, buf, 3);
  862. nxt200x_readreg_multibyte(state, 0x88, buf, 1);
  863. buf[0] = 0x11;
  864. nxt200x_writereg_multibyte(state, 0x88, buf, 1);
  865. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  866. buf[0] = 0x44;
  867. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  868. /* initialize tuner */
  869. nxt200x_readbytes(state, 0x10, buf, 1);
  870. buf[0] = 0x12;
  871. nxt200x_writebytes(state, 0x10, buf, 1);
  872. buf[0] = 0x04;
  873. nxt200x_writebytes(state, 0x13, buf, 1);
  874. buf[0] = 0x00;
  875. nxt200x_writebytes(state, 0x16, buf, 1);
  876. buf[0] = 0x04;
  877. nxt200x_writebytes(state, 0x14, buf, 1);
  878. buf[0] = 0x00;
  879. nxt200x_writebytes(state, 0x14, buf, 1);
  880. nxt200x_writebytes(state, 0x17, buf, 1);
  881. nxt200x_writebytes(state, 0x14, buf, 1);
  882. nxt200x_writebytes(state, 0x17, buf, 1);
  883. return 0;
  884. }
  885. static int nxt200x_init(struct dvb_frontend* fe)
  886. {
  887. struct nxt200x_state* state = fe->demodulator_priv;
  888. int ret = 0;
  889. if (!state->initialised) {
  890. switch (state->demod_chip) {
  891. case NXT2002:
  892. ret = nxt2002_init(fe);
  893. break;
  894. case NXT2004:
  895. ret = nxt2004_init(fe);
  896. break;
  897. default:
  898. return -EINVAL;
  899. break;
  900. }
  901. state->initialised = 1;
  902. }
  903. return ret;
  904. }
  905. static int nxt200x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  906. {
  907. fesettings->min_delay_ms = 500;
  908. fesettings->step_size = 0;
  909. fesettings->max_drift = 0;
  910. return 0;
  911. }
  912. static void nxt200x_release(struct dvb_frontend* fe)
  913. {
  914. struct nxt200x_state* state = fe->demodulator_priv;
  915. kfree(state);
  916. }
  917. static struct dvb_frontend_ops nxt200x_ops;
  918. struct dvb_frontend* nxt200x_attach(const struct nxt200x_config* config,
  919. struct i2c_adapter* i2c)
  920. {
  921. struct nxt200x_state* state = NULL;
  922. u8 buf [] = {0,0,0,0,0};
  923. /* allocate memory for the internal state */
  924. state = kzalloc(sizeof(struct nxt200x_state), GFP_KERNEL);
  925. if (state == NULL)
  926. goto error;
  927. /* setup the state */
  928. state->config = config;
  929. state->i2c = i2c;
  930. memcpy(&state->ops, &nxt200x_ops, sizeof(struct dvb_frontend_ops));
  931. state->initialised = 0;
  932. /* read card id */
  933. nxt200x_readbytes(state, 0x00, buf, 5);
  934. dprintk("NXT info: %02X %02X %02X %02X %02X\n",
  935. buf[0], buf[1], buf[2], buf[3], buf[4]);
  936. /* set demod chip */
  937. switch (buf[0]) {
  938. case 0x04:
  939. state->demod_chip = NXT2002;
  940. printk("nxt200x: NXT2002 Detected\n");
  941. break;
  942. case 0x05:
  943. state->demod_chip = NXT2004;
  944. printk("nxt200x: NXT2004 Detected\n");
  945. break;
  946. default:
  947. goto error;
  948. }
  949. /* make sure demod chip is supported */
  950. switch (state->demod_chip) {
  951. case NXT2002:
  952. if (buf[0] != 0x04) goto error; /* device id */
  953. if (buf[1] != 0x02) goto error; /* fab id */
  954. if (buf[2] != 0x11) goto error; /* month */
  955. if (buf[3] != 0x20) goto error; /* year msb */
  956. if (buf[4] != 0x00) goto error; /* year lsb */
  957. break;
  958. case NXT2004:
  959. if (buf[0] != 0x05) goto error; /* device id */
  960. break;
  961. default:
  962. goto error;
  963. }
  964. /* create dvb_frontend */
  965. state->frontend.ops = &state->ops;
  966. state->frontend.demodulator_priv = state;
  967. return &state->frontend;
  968. error:
  969. kfree(state);
  970. printk("Unknown/Unsupported NXT chip: %02X %02X %02X %02X %02X\n",
  971. buf[0], buf[1], buf[2], buf[3], buf[4]);
  972. return NULL;
  973. }
  974. static struct dvb_frontend_ops nxt200x_ops = {
  975. .info = {
  976. .name = "Nextwave NXT200X VSB/QAM frontend",
  977. .type = FE_ATSC,
  978. .frequency_min = 54000000,
  979. .frequency_max = 860000000,
  980. .frequency_stepsize = 166666, /* stepsize is just a guess */
  981. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  982. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  983. FE_CAN_8VSB | FE_CAN_QAM_64 | FE_CAN_QAM_256
  984. },
  985. .release = nxt200x_release,
  986. .init = nxt200x_init,
  987. .sleep = nxt200x_sleep,
  988. .set_frontend = nxt200x_setup_frontend_parameters,
  989. .get_tune_settings = nxt200x_get_tune_settings,
  990. .read_status = nxt200x_read_status,
  991. .read_ber = nxt200x_read_ber,
  992. .read_signal_strength = nxt200x_read_signal_strength,
  993. .read_snr = nxt200x_read_snr,
  994. .read_ucblocks = nxt200x_read_ucblocks,
  995. };
  996. module_param(debug, int, 0644);
  997. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  998. MODULE_DESCRIPTION("NXT200X (ATSC 8VSB & ITU-T J.83 AnnexB 64/256 QAM) Demodulator Driver");
  999. MODULE_AUTHOR("Kirk Lapray, Jean-Francois Thibert, and Taylor Jacob");
  1000. MODULE_LICENSE("GPL");
  1001. EXPORT_SYMBOL(nxt200x_attach);