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