nxt200x.c 29 KB

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