nxt200x.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /*
  2. * Support for NXT2002 and NXT2004 - VSB/QAM
  3. *
  4. * Copyright (C) 2005 Kirk Lapray <kirk.lapray@gmail.com>
  5. * Copyright (C) 2006-2014 Michael Krufky <mkrufky@linuxtv.org>
  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. */
  20. /*
  21. * NOTES ABOUT THIS DRIVER
  22. *
  23. * This Linux driver supports:
  24. * B2C2/BBTI Technisat Air2PC - ATSC (NXT2002)
  25. * AverTVHD MCE A180 (NXT2004)
  26. * ATI HDTV Wonder (NXT2004)
  27. *
  28. * This driver needs external firmware. Please use the command
  29. * "<kerneldir>/scripts/get_dvb_firmware nxt2002" or
  30. * "<kerneldir>/scripts/get_dvb_firmware nxt2004" to
  31. * download/extract the appropriate firmware, and then copy it to
  32. * /usr/lib/hotplug/firmware/ or /lib/firmware/
  33. * (depending on configuration of firmware hotplug).
  34. */
  35. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  36. /* Max transfer size done by I2C transfer functions */
  37. #define MAX_XFER_SIZE 256
  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/slab.h>
  45. #include <linux/string.h>
  46. #include <media/dvb_frontend.h>
  47. #include "nxt200x.h"
  48. struct nxt200x_state {
  49. struct i2c_adapter* i2c;
  50. const struct nxt200x_config* config;
  51. struct dvb_frontend frontend;
  52. /* demodulator private data */
  53. nxt_chip_type demod_chip;
  54. u8 initialised:1;
  55. };
  56. static int debug;
  57. #define dprintk(args...) do { if (debug) pr_debug(args); } while (0)
  58. static int i2c_writebytes (struct nxt200x_state* state, u8 addr, u8 *buf, u8 len)
  59. {
  60. int err;
  61. struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = len };
  62. if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
  63. pr_warn("%s: i2c write error (addr 0x%02x, err == %i)\n",
  64. __func__, addr, err);
  65. return -EREMOTEIO;
  66. }
  67. return 0;
  68. }
  69. static int i2c_readbytes(struct nxt200x_state *state, u8 addr, u8 *buf, u8 len)
  70. {
  71. int err;
  72. struct i2c_msg msg = { .addr = addr, .flags = I2C_M_RD, .buf = buf, .len = len };
  73. if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
  74. pr_warn("%s: i2c read error (addr 0x%02x, err == %i)\n",
  75. __func__, addr, err);
  76. return -EREMOTEIO;
  77. }
  78. return 0;
  79. }
  80. static int nxt200x_writebytes (struct nxt200x_state* state, u8 reg,
  81. const u8 *buf, u8 len)
  82. {
  83. u8 buf2[MAX_XFER_SIZE];
  84. int err;
  85. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf2, .len = len + 1 };
  86. if (1 + len > sizeof(buf2)) {
  87. pr_warn("%s: i2c wr reg=%04x: len=%d is too big!\n",
  88. __func__, reg, len);
  89. return -EINVAL;
  90. }
  91. buf2[0] = reg;
  92. memcpy(&buf2[1], buf, len);
  93. if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
  94. pr_warn("%s: i2c write error (addr 0x%02x, err == %i)\n",
  95. __func__, state->config->demod_address, err);
  96. return -EREMOTEIO;
  97. }
  98. return 0;
  99. }
  100. static int 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. pr_warn("%s: i2c read error (addr 0x%02x, err == %i)\n",
  108. __func__, 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", __func__);
  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. pr_warn("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", __func__);
  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", __func__);
  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. pr_warn("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", __func__);
  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", __func__);
  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. pr_warn("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", __func__);
  283. dprintk("Tuner Bytes: %*ph\n", 4, data + 1);
  284. /* if NXT2004, write directly to tuner. if NXT2002, write through NXT chip.
  285. * direct write is required for Philips TUV1236D and ALPS TDHU2 */
  286. switch (state->demod_chip) {
  287. case NXT2004:
  288. if (i2c_writebytes(state, data[0], data+1, 4))
  289. pr_warn("error writing to tuner\n");
  290. /* wait until we have a lock */
  291. while (count < 20) {
  292. i2c_readbytes(state, data[0], &buf, 1);
  293. if (buf & 0x40)
  294. return 0;
  295. msleep(100);
  296. count++;
  297. }
  298. pr_warn("timeout waiting for tuner lock\n");
  299. break;
  300. case NXT2002:
  301. /* set the i2c transfer speed to the tuner */
  302. buf = 0x03;
  303. nxt200x_writebytes(state, 0x20, &buf, 1);
  304. /* setup to transfer 4 bytes via i2c */
  305. buf = 0x04;
  306. nxt200x_writebytes(state, 0x34, &buf, 1);
  307. /* write actual tuner bytes */
  308. nxt200x_writebytes(state, 0x36, data+1, 4);
  309. /* set tuner i2c address */
  310. buf = data[0] << 1;
  311. nxt200x_writebytes(state, 0x35, &buf, 1);
  312. /* write UC Opmode to begin transfer */
  313. buf = 0x80;
  314. nxt200x_writebytes(state, 0x21, &buf, 1);
  315. while (count < 20) {
  316. nxt200x_readbytes(state, 0x21, &buf, 1);
  317. if ((buf & 0x80)== 0x00)
  318. return 0;
  319. msleep(100);
  320. count++;
  321. }
  322. pr_warn("timeout error writing to 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", __func__);
  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", __func__);
  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", __func__);
  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. {
  444. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  445. struct nxt200x_state* state = fe->demodulator_priv;
  446. u8 buf[5];
  447. /* stop the micro first */
  448. nxt200x_microcontroller_stop(state);
  449. if (state->demod_chip == NXT2004) {
  450. /* make sure demod is set to digital */
  451. buf[0] = 0x04;
  452. nxt200x_writebytes(state, 0x14, buf, 1);
  453. buf[0] = 0x00;
  454. nxt200x_writebytes(state, 0x17, buf, 1);
  455. }
  456. /* set additional params */
  457. switch (p->modulation) {
  458. case QAM_64:
  459. case QAM_256:
  460. /* Set punctured clock for QAM */
  461. /* This is just a guess since I am unable to test it */
  462. if (state->config->set_ts_params)
  463. state->config->set_ts_params(fe, 1);
  464. break;
  465. case VSB_8:
  466. /* Set non-punctured clock for VSB */
  467. if (state->config->set_ts_params)
  468. state->config->set_ts_params(fe, 0);
  469. break;
  470. default:
  471. return -EINVAL;
  472. break;
  473. }
  474. if (fe->ops.tuner_ops.calc_regs) {
  475. /* get tuning information */
  476. fe->ops.tuner_ops.calc_regs(fe, buf, 5);
  477. /* write frequency information */
  478. nxt200x_writetuner(state, buf);
  479. }
  480. /* reset the agc now that tuning has been completed */
  481. nxt200x_agc_reset(state);
  482. /* set target power level */
  483. switch (p->modulation) {
  484. case QAM_64:
  485. case QAM_256:
  486. buf[0] = 0x74;
  487. break;
  488. case VSB_8:
  489. buf[0] = 0x70;
  490. break;
  491. default:
  492. return -EINVAL;
  493. break;
  494. }
  495. nxt200x_writebytes(state, 0x42, buf, 1);
  496. /* configure sdm */
  497. switch (state->demod_chip) {
  498. case NXT2002:
  499. buf[0] = 0x87;
  500. break;
  501. case NXT2004:
  502. buf[0] = 0x07;
  503. break;
  504. default:
  505. return -EINVAL;
  506. break;
  507. }
  508. nxt200x_writebytes(state, 0x57, buf, 1);
  509. /* write sdm1 input */
  510. buf[0] = 0x10;
  511. buf[1] = 0x00;
  512. switch (state->demod_chip) {
  513. case NXT2002:
  514. nxt200x_writereg_multibyte(state, 0x58, buf, 2);
  515. break;
  516. case NXT2004:
  517. nxt200x_writebytes(state, 0x58, buf, 2);
  518. break;
  519. default:
  520. return -EINVAL;
  521. break;
  522. }
  523. /* write sdmx input */
  524. switch (p->modulation) {
  525. case QAM_64:
  526. buf[0] = 0x68;
  527. break;
  528. case QAM_256:
  529. buf[0] = 0x64;
  530. break;
  531. case VSB_8:
  532. buf[0] = 0x60;
  533. break;
  534. default:
  535. return -EINVAL;
  536. break;
  537. }
  538. buf[1] = 0x00;
  539. switch (state->demod_chip) {
  540. case NXT2002:
  541. nxt200x_writereg_multibyte(state, 0x5C, buf, 2);
  542. break;
  543. case NXT2004:
  544. nxt200x_writebytes(state, 0x5C, buf, 2);
  545. break;
  546. default:
  547. return -EINVAL;
  548. break;
  549. }
  550. /* write adc power lpf fc */
  551. buf[0] = 0x05;
  552. nxt200x_writebytes(state, 0x43, buf, 1);
  553. if (state->demod_chip == NXT2004) {
  554. /* write ??? */
  555. buf[0] = 0x00;
  556. buf[1] = 0x00;
  557. nxt200x_writebytes(state, 0x46, buf, 2);
  558. }
  559. /* write accumulator2 input */
  560. buf[0] = 0x80;
  561. buf[1] = 0x00;
  562. switch (state->demod_chip) {
  563. case NXT2002:
  564. nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
  565. break;
  566. case NXT2004:
  567. nxt200x_writebytes(state, 0x4B, buf, 2);
  568. break;
  569. default:
  570. return -EINVAL;
  571. break;
  572. }
  573. /* write kg1 */
  574. buf[0] = 0x00;
  575. nxt200x_writebytes(state, 0x4D, buf, 1);
  576. /* write sdm12 lpf fc */
  577. buf[0] = 0x44;
  578. nxt200x_writebytes(state, 0x55, buf, 1);
  579. /* write agc control reg */
  580. buf[0] = 0x04;
  581. nxt200x_writebytes(state, 0x41, buf, 1);
  582. if (state->demod_chip == NXT2004) {
  583. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  584. buf[0] = 0x24;
  585. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  586. /* soft reset? */
  587. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  588. buf[0] = 0x10;
  589. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  590. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  591. buf[0] = 0x00;
  592. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  593. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  594. buf[0] = 0x04;
  595. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  596. buf[0] = 0x00;
  597. nxt200x_writereg_multibyte(state, 0x81, buf, 1);
  598. buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
  599. nxt200x_writereg_multibyte(state, 0x82, buf, 3);
  600. nxt200x_readreg_multibyte(state, 0x88, buf, 1);
  601. buf[0] = 0x11;
  602. nxt200x_writereg_multibyte(state, 0x88, buf, 1);
  603. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  604. buf[0] = 0x44;
  605. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  606. }
  607. /* write agc ucgp0 */
  608. switch (p->modulation) {
  609. case QAM_64:
  610. buf[0] = 0x02;
  611. break;
  612. case QAM_256:
  613. buf[0] = 0x03;
  614. break;
  615. case VSB_8:
  616. buf[0] = 0x00;
  617. break;
  618. default:
  619. return -EINVAL;
  620. break;
  621. }
  622. nxt200x_writebytes(state, 0x30, buf, 1);
  623. /* write agc control reg */
  624. buf[0] = 0x00;
  625. nxt200x_writebytes(state, 0x41, buf, 1);
  626. /* write accumulator2 input */
  627. buf[0] = 0x80;
  628. buf[1] = 0x00;
  629. switch (state->demod_chip) {
  630. case NXT2002:
  631. nxt200x_writereg_multibyte(state, 0x49, buf, 2);
  632. nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
  633. break;
  634. case NXT2004:
  635. nxt200x_writebytes(state, 0x49, buf, 2);
  636. nxt200x_writebytes(state, 0x4B, buf, 2);
  637. break;
  638. default:
  639. return -EINVAL;
  640. break;
  641. }
  642. /* write agc control reg */
  643. buf[0] = 0x04;
  644. nxt200x_writebytes(state, 0x41, buf, 1);
  645. nxt200x_microcontroller_start(state);
  646. if (state->demod_chip == NXT2004) {
  647. nxt2004_microcontroller_init(state);
  648. /* ???? */
  649. buf[0] = 0xF0;
  650. buf[1] = 0x00;
  651. nxt200x_writebytes(state, 0x5C, buf, 2);
  652. }
  653. /* adjacent channel detection should be done here, but I don't
  654. have any stations with this need so I cannot test it */
  655. return 0;
  656. }
  657. static int nxt200x_read_status(struct dvb_frontend *fe, enum fe_status *status)
  658. {
  659. struct nxt200x_state* state = fe->demodulator_priv;
  660. u8 lock;
  661. nxt200x_readbytes(state, 0x31, &lock, 1);
  662. *status = 0;
  663. if (lock & 0x20) {
  664. *status |= FE_HAS_SIGNAL;
  665. *status |= FE_HAS_CARRIER;
  666. *status |= FE_HAS_VITERBI;
  667. *status |= FE_HAS_SYNC;
  668. *status |= FE_HAS_LOCK;
  669. }
  670. return 0;
  671. }
  672. static int nxt200x_read_ber(struct dvb_frontend* fe, u32* ber)
  673. {
  674. struct nxt200x_state* state = fe->demodulator_priv;
  675. u8 b[3];
  676. nxt200x_readreg_multibyte(state, 0xE6, b, 3);
  677. *ber = ((b[0] << 8) + b[1]) * 8;
  678. return 0;
  679. }
  680. static int nxt200x_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  681. {
  682. struct nxt200x_state* state = fe->demodulator_priv;
  683. u8 b[2];
  684. u16 temp = 0;
  685. /* setup to read cluster variance */
  686. b[0] = 0x00;
  687. nxt200x_writebytes(state, 0xA1, b, 1);
  688. /* get multreg val */
  689. nxt200x_readreg_multibyte(state, 0xA6, b, 2);
  690. temp = (b[0] << 8) | b[1];
  691. *strength = ((0x7FFF - temp) & 0x0FFF) * 16;
  692. return 0;
  693. }
  694. static int nxt200x_read_snr(struct dvb_frontend* fe, u16* snr)
  695. {
  696. struct nxt200x_state* state = fe->demodulator_priv;
  697. u8 b[2];
  698. u16 temp = 0, temp2;
  699. u32 snrdb = 0;
  700. /* setup to read cluster variance */
  701. b[0] = 0x00;
  702. nxt200x_writebytes(state, 0xA1, b, 1);
  703. /* get multreg val from 0xA6 */
  704. nxt200x_readreg_multibyte(state, 0xA6, b, 2);
  705. temp = (b[0] << 8) | b[1];
  706. temp2 = 0x7FFF - temp;
  707. /* snr will be in db */
  708. if (temp2 > 0x7F00)
  709. snrdb = 1000*24 + ( 1000*(30-24) * ( temp2 - 0x7F00 ) / ( 0x7FFF - 0x7F00 ) );
  710. else if (temp2 > 0x7EC0)
  711. snrdb = 1000*18 + ( 1000*(24-18) * ( temp2 - 0x7EC0 ) / ( 0x7F00 - 0x7EC0 ) );
  712. else if (temp2 > 0x7C00)
  713. snrdb = 1000*12 + ( 1000*(18-12) * ( temp2 - 0x7C00 ) / ( 0x7EC0 - 0x7C00 ) );
  714. else
  715. snrdb = 1000*0 + ( 1000*(12-0) * ( temp2 - 0 ) / ( 0x7C00 - 0 ) );
  716. /* the value reported back from the frontend will be FFFF=32db 0000=0db */
  717. *snr = snrdb * (0xFFFF/32000);
  718. return 0;
  719. }
  720. static int nxt200x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  721. {
  722. struct nxt200x_state* state = fe->demodulator_priv;
  723. u8 b[3];
  724. nxt200x_readreg_multibyte(state, 0xE6, b, 3);
  725. *ucblocks = b[2];
  726. return 0;
  727. }
  728. static int nxt200x_sleep(struct dvb_frontend* fe)
  729. {
  730. return 0;
  731. }
  732. static int nxt2002_init(struct dvb_frontend* fe)
  733. {
  734. struct nxt200x_state* state = fe->demodulator_priv;
  735. const struct firmware *fw;
  736. int ret;
  737. u8 buf[2];
  738. /* request the firmware, this will block until someone uploads it */
  739. pr_debug("%s: Waiting for firmware upload (%s)...\n",
  740. __func__, NXT2002_DEFAULT_FIRMWARE);
  741. ret = request_firmware(&fw, NXT2002_DEFAULT_FIRMWARE,
  742. state->i2c->dev.parent);
  743. if (ret)
  744. return ret;
  745. ret = nxt2002_load_firmware(fe, fw);
  746. release_firmware(fw);
  747. if (ret) {
  748. pr_err("%s: Writing firmware to device failed\n", __func__);
  749. return ret;
  750. }
  751. pr_info("%s: Firmware upload complete\n", __func__);
  752. /* Put the micro into reset */
  753. nxt200x_microcontroller_stop(state);
  754. /* ensure transfer is complete */
  755. buf[0]=0x00;
  756. nxt200x_writebytes(state, 0x2B, buf, 1);
  757. /* Put the micro into reset for real this time */
  758. nxt200x_microcontroller_stop(state);
  759. /* soft reset everything (agc,frontend,eq,fec)*/
  760. buf[0] = 0x0F;
  761. nxt200x_writebytes(state, 0x08, buf, 1);
  762. buf[0] = 0x00;
  763. nxt200x_writebytes(state, 0x08, buf, 1);
  764. /* write agc sdm configure */
  765. buf[0] = 0xF1;
  766. nxt200x_writebytes(state, 0x57, buf, 1);
  767. /* write mod output format */
  768. buf[0] = 0x20;
  769. nxt200x_writebytes(state, 0x09, buf, 1);
  770. /* write fec mpeg mode */
  771. buf[0] = 0x7E;
  772. buf[1] = 0x00;
  773. nxt200x_writebytes(state, 0xE9, buf, 2);
  774. /* write mux selection */
  775. buf[0] = 0x00;
  776. nxt200x_writebytes(state, 0xCC, buf, 1);
  777. return 0;
  778. }
  779. static int nxt2004_init(struct dvb_frontend* fe)
  780. {
  781. struct nxt200x_state* state = fe->demodulator_priv;
  782. const struct firmware *fw;
  783. int ret;
  784. u8 buf[3];
  785. /* ??? */
  786. buf[0]=0x00;
  787. nxt200x_writebytes(state, 0x1E, buf, 1);
  788. /* request the firmware, this will block until someone uploads it */
  789. pr_debug("%s: Waiting for firmware upload (%s)...\n",
  790. __func__, NXT2004_DEFAULT_FIRMWARE);
  791. ret = request_firmware(&fw, NXT2004_DEFAULT_FIRMWARE,
  792. state->i2c->dev.parent);
  793. if (ret)
  794. return ret;
  795. ret = nxt2004_load_firmware(fe, fw);
  796. release_firmware(fw);
  797. if (ret) {
  798. pr_err("%s: Writing firmware to device failed\n", __func__);
  799. return ret;
  800. }
  801. pr_info("%s: Firmware upload complete\n", __func__);
  802. /* ensure transfer is complete */
  803. buf[0] = 0x01;
  804. nxt200x_writebytes(state, 0x19, buf, 1);
  805. nxt2004_microcontroller_init(state);
  806. nxt200x_microcontroller_stop(state);
  807. nxt200x_microcontroller_stop(state);
  808. nxt2004_microcontroller_init(state);
  809. nxt200x_microcontroller_stop(state);
  810. /* soft reset everything (agc,frontend,eq,fec)*/
  811. buf[0] = 0xFF;
  812. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  813. buf[0] = 0x00;
  814. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  815. /* write agc sdm configure */
  816. buf[0] = 0xD7;
  817. nxt200x_writebytes(state, 0x57, buf, 1);
  818. /* ???*/
  819. buf[0] = 0x07;
  820. buf[1] = 0xfe;
  821. nxt200x_writebytes(state, 0x35, buf, 2);
  822. buf[0] = 0x12;
  823. nxt200x_writebytes(state, 0x34, buf, 1);
  824. buf[0] = 0x80;
  825. nxt200x_writebytes(state, 0x21, buf, 1);
  826. /* ???*/
  827. buf[0] = 0x21;
  828. nxt200x_writebytes(state, 0x0A, buf, 1);
  829. /* ???*/
  830. buf[0] = 0x01;
  831. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  832. /* write fec mpeg mode */
  833. buf[0] = 0x7E;
  834. buf[1] = 0x00;
  835. nxt200x_writebytes(state, 0xE9, buf, 2);
  836. /* write mux selection */
  837. buf[0] = 0x00;
  838. nxt200x_writebytes(state, 0xCC, buf, 1);
  839. /* ???*/
  840. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  841. buf[0] = 0x00;
  842. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  843. /* soft reset? */
  844. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  845. buf[0] = 0x10;
  846. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  847. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  848. buf[0] = 0x00;
  849. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  850. /* ???*/
  851. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  852. buf[0] = 0x01;
  853. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  854. buf[0] = 0x70;
  855. nxt200x_writereg_multibyte(state, 0x81, buf, 1);
  856. buf[0] = 0x31; buf[1] = 0x5E; buf[2] = 0x66;
  857. nxt200x_writereg_multibyte(state, 0x82, buf, 3);
  858. nxt200x_readreg_multibyte(state, 0x88, buf, 1);
  859. buf[0] = 0x11;
  860. nxt200x_writereg_multibyte(state, 0x88, buf, 1);
  861. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  862. buf[0] = 0x40;
  863. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  864. nxt200x_readbytes(state, 0x10, buf, 1);
  865. buf[0] = 0x10;
  866. nxt200x_writebytes(state, 0x10, buf, 1);
  867. nxt200x_readbytes(state, 0x0A, buf, 1);
  868. buf[0] = 0x21;
  869. nxt200x_writebytes(state, 0x0A, buf, 1);
  870. nxt2004_microcontroller_init(state);
  871. buf[0] = 0x21;
  872. nxt200x_writebytes(state, 0x0A, buf, 1);
  873. buf[0] = 0x7E;
  874. nxt200x_writebytes(state, 0xE9, buf, 1);
  875. buf[0] = 0x00;
  876. nxt200x_writebytes(state, 0xEA, buf, 1);
  877. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  878. buf[0] = 0x00;
  879. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  880. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  881. buf[0] = 0x00;
  882. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  883. /* soft reset? */
  884. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  885. buf[0] = 0x10;
  886. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  887. nxt200x_readreg_multibyte(state, 0x08, buf, 1);
  888. buf[0] = 0x00;
  889. nxt200x_writereg_multibyte(state, 0x08, buf, 1);
  890. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  891. buf[0] = 0x04;
  892. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  893. buf[0] = 0x00;
  894. nxt200x_writereg_multibyte(state, 0x81, buf, 1);
  895. buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
  896. nxt200x_writereg_multibyte(state, 0x82, buf, 3);
  897. nxt200x_readreg_multibyte(state, 0x88, buf, 1);
  898. buf[0] = 0x11;
  899. nxt200x_writereg_multibyte(state, 0x88, buf, 1);
  900. nxt200x_readreg_multibyte(state, 0x80, buf, 1);
  901. buf[0] = 0x44;
  902. nxt200x_writereg_multibyte(state, 0x80, buf, 1);
  903. /* initialize tuner */
  904. nxt200x_readbytes(state, 0x10, buf, 1);
  905. buf[0] = 0x12;
  906. nxt200x_writebytes(state, 0x10, buf, 1);
  907. buf[0] = 0x04;
  908. nxt200x_writebytes(state, 0x13, buf, 1);
  909. buf[0] = 0x00;
  910. nxt200x_writebytes(state, 0x16, buf, 1);
  911. buf[0] = 0x04;
  912. nxt200x_writebytes(state, 0x14, buf, 1);
  913. buf[0] = 0x00;
  914. nxt200x_writebytes(state, 0x14, buf, 1);
  915. nxt200x_writebytes(state, 0x17, buf, 1);
  916. nxt200x_writebytes(state, 0x14, buf, 1);
  917. nxt200x_writebytes(state, 0x17, buf, 1);
  918. return 0;
  919. }
  920. static int nxt200x_init(struct dvb_frontend* fe)
  921. {
  922. struct nxt200x_state* state = fe->demodulator_priv;
  923. int ret = 0;
  924. if (!state->initialised) {
  925. switch (state->demod_chip) {
  926. case NXT2002:
  927. ret = nxt2002_init(fe);
  928. break;
  929. case NXT2004:
  930. ret = nxt2004_init(fe);
  931. break;
  932. default:
  933. return -EINVAL;
  934. break;
  935. }
  936. state->initialised = 1;
  937. }
  938. return ret;
  939. }
  940. static int nxt200x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  941. {
  942. fesettings->min_delay_ms = 500;
  943. fesettings->step_size = 0;
  944. fesettings->max_drift = 0;
  945. return 0;
  946. }
  947. static void nxt200x_release(struct dvb_frontend* fe)
  948. {
  949. struct nxt200x_state* state = fe->demodulator_priv;
  950. kfree(state);
  951. }
  952. static const struct dvb_frontend_ops nxt200x_ops;
  953. struct dvb_frontend* nxt200x_attach(const struct nxt200x_config* config,
  954. struct i2c_adapter* i2c)
  955. {
  956. struct nxt200x_state* state = NULL;
  957. u8 buf [] = {0,0,0,0,0};
  958. /* allocate memory for the internal state */
  959. state = kzalloc(sizeof(struct nxt200x_state), GFP_KERNEL);
  960. if (state == NULL)
  961. goto error;
  962. /* setup the state */
  963. state->config = config;
  964. state->i2c = i2c;
  965. state->initialised = 0;
  966. /* read card id */
  967. nxt200x_readbytes(state, 0x00, buf, 5);
  968. dprintk("NXT info: %*ph\n", 5, buf);
  969. /* set demod chip */
  970. switch (buf[0]) {
  971. case 0x04:
  972. state->demod_chip = NXT2002;
  973. pr_info("NXT2002 Detected\n");
  974. break;
  975. case 0x05:
  976. state->demod_chip = NXT2004;
  977. pr_info("NXT2004 Detected\n");
  978. break;
  979. default:
  980. goto error;
  981. }
  982. /* make sure demod chip is supported */
  983. switch (state->demod_chip) {
  984. case NXT2002:
  985. if (buf[0] != 0x04) goto error; /* device id */
  986. if (buf[1] != 0x02) goto error; /* fab id */
  987. if (buf[2] != 0x11) goto error; /* month */
  988. if (buf[3] != 0x20) goto error; /* year msb */
  989. if (buf[4] != 0x00) goto error; /* year lsb */
  990. break;
  991. case NXT2004:
  992. if (buf[0] != 0x05) goto error; /* device id */
  993. break;
  994. default:
  995. goto error;
  996. }
  997. /* create dvb_frontend */
  998. memcpy(&state->frontend.ops, &nxt200x_ops, sizeof(struct dvb_frontend_ops));
  999. state->frontend.demodulator_priv = state;
  1000. return &state->frontend;
  1001. error:
  1002. kfree(state);
  1003. pr_err("Unknown/Unsupported NXT chip: %*ph\n", 5, buf);
  1004. return NULL;
  1005. }
  1006. static const struct dvb_frontend_ops nxt200x_ops = {
  1007. .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
  1008. .info = {
  1009. .name = "Nextwave NXT200X VSB/QAM frontend",
  1010. .frequency_min_hz = 54 * MHz,
  1011. .frequency_max_hz = 860 * MHz,
  1012. .frequency_stepsize_hz = 166666, /* stepsize is just a guess */
  1013. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1014. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1015. FE_CAN_8VSB | FE_CAN_QAM_64 | FE_CAN_QAM_256
  1016. },
  1017. .release = nxt200x_release,
  1018. .init = nxt200x_init,
  1019. .sleep = nxt200x_sleep,
  1020. .set_frontend = nxt200x_setup_frontend_parameters,
  1021. .get_tune_settings = nxt200x_get_tune_settings,
  1022. .read_status = nxt200x_read_status,
  1023. .read_ber = nxt200x_read_ber,
  1024. .read_signal_strength = nxt200x_read_signal_strength,
  1025. .read_snr = nxt200x_read_snr,
  1026. .read_ucblocks = nxt200x_read_ucblocks,
  1027. };
  1028. module_param(debug, int, 0644);
  1029. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  1030. MODULE_DESCRIPTION("NXT200X (ATSC 8VSB & ITU-T J.83 AnnexB 64/256 QAM) Demodulator Driver");
  1031. MODULE_AUTHOR("Kirk Lapray, Michael Krufky, Jean-Francois Thibert, and Taylor Jacob");
  1032. MODULE_LICENSE("GPL");
  1033. EXPORT_SYMBOL(nxt200x_attach);