nxt200x.c 30 KB

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