nxt200x.c 30 KB

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