or51132.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. * Support for OR51132 (pcHDTV HD-3000) - VSB/QAM
  3. *
  4. * Copyright (C) 2005 Kirk Lapray <kirk_lapray@bigfoot.com>
  5. *
  6. * Based on code from Jack Kelliher (kelliher@xmission.com)
  7. * Copyright (C) 2002 & pcHDTV, inc.
  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. * This driver needs two external firmware files. Please copy
  26. * "dvb-fe-or51132-vsb.fw" and "dvb-fe-or51132-qam.fw" to
  27. * /usr/lib/hotplug/firmware/ or /lib/firmware/
  28. * (depending on configuration of firmware hotplug).
  29. */
  30. #define OR51132_VSB_FIRMWARE "dvb-fe-or51132-vsb.fw"
  31. #define OR51132_QAM_FIRMWARE "dvb-fe-or51132-qam.fw"
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/init.h>
  36. #include <linux/delay.h>
  37. #include <linux/string.h>
  38. #include <linux/slab.h>
  39. #include <asm/byteorder.h>
  40. #include "dvb_frontend.h"
  41. #include "dvb-pll.h"
  42. #include "or51132.h"
  43. static int debug;
  44. #define dprintk(args...) \
  45. do { \
  46. if (debug) printk(KERN_DEBUG "or51132: " args); \
  47. } while (0)
  48. struct or51132_state
  49. {
  50. struct i2c_adapter* i2c;
  51. struct dvb_frontend_ops ops;
  52. /* Configuration settings */
  53. const struct or51132_config* config;
  54. struct dvb_frontend frontend;
  55. /* Demodulator private data */
  56. fe_modulation_t current_modulation;
  57. /* Tuner private data */
  58. u32 current_frequency;
  59. };
  60. static int i2c_writebytes (struct or51132_state* state, u8 reg, u8 *buf, int len)
  61. {
  62. int err;
  63. struct i2c_msg msg;
  64. msg.addr = reg;
  65. msg.flags = 0;
  66. msg.len = len;
  67. msg.buf = buf;
  68. if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
  69. printk(KERN_WARNING "or51132: i2c_writebytes error (addr %02x, err == %i)\n", reg, err);
  70. return -EREMOTEIO;
  71. }
  72. return 0;
  73. }
  74. static u8 i2c_readbytes (struct or51132_state* state, u8 reg, u8* buf, int len)
  75. {
  76. int err;
  77. struct i2c_msg msg;
  78. msg.addr = reg;
  79. msg.flags = I2C_M_RD;
  80. msg.len = len;
  81. msg.buf = buf;
  82. if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
  83. printk(KERN_WARNING "or51132: i2c_readbytes error (addr %02x, err == %i)\n", reg, err);
  84. return -EREMOTEIO;
  85. }
  86. return 0;
  87. }
  88. static int or51132_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
  89. {
  90. struct or51132_state* state = fe->demodulator_priv;
  91. static u8 run_buf[] = {0x7F,0x01};
  92. u8 rec_buf[8];
  93. u8 cmd_buf[3];
  94. u32 firmwareAsize, firmwareBsize;
  95. int i,ret;
  96. dprintk("Firmware is %Zd bytes\n",fw->size);
  97. /* Get size of firmware A and B */
  98. firmwareAsize = le32_to_cpu(*((u32*)fw->data));
  99. dprintk("FirmwareA is %i bytes\n",firmwareAsize);
  100. firmwareBsize = le32_to_cpu(*((u32*)(fw->data+4)));
  101. dprintk("FirmwareB is %i bytes\n",firmwareBsize);
  102. /* Upload firmware */
  103. if ((ret = i2c_writebytes(state,state->config->demod_address,
  104. &fw->data[8],firmwareAsize))) {
  105. printk(KERN_WARNING "or51132: load_firmware error 1\n");
  106. return ret;
  107. }
  108. msleep(1); /* 1ms */
  109. if ((ret = i2c_writebytes(state,state->config->demod_address,
  110. &fw->data[8+firmwareAsize],firmwareBsize))) {
  111. printk(KERN_WARNING "or51132: load_firmware error 2\n");
  112. return ret;
  113. }
  114. msleep(1); /* 1ms */
  115. if ((ret = i2c_writebytes(state,state->config->demod_address,
  116. run_buf,2))) {
  117. printk(KERN_WARNING "or51132: load_firmware error 3\n");
  118. return ret;
  119. }
  120. /* Wait at least 5 msec */
  121. msleep(20); /* 10ms */
  122. if ((ret = i2c_writebytes(state,state->config->demod_address,
  123. run_buf,2))) {
  124. printk(KERN_WARNING "or51132: load_firmware error 4\n");
  125. return ret;
  126. }
  127. /* 50ms for operation to begin */
  128. msleep(50);
  129. /* Read back ucode version to besure we loaded correctly and are really up and running */
  130. /* Get uCode version */
  131. cmd_buf[0] = 0x10;
  132. cmd_buf[1] = 0x10;
  133. cmd_buf[2] = 0x00;
  134. msleep(20); /* 20ms */
  135. if ((ret = i2c_writebytes(state,state->config->demod_address,
  136. cmd_buf,3))) {
  137. printk(KERN_WARNING "or51132: load_firmware error a\n");
  138. return ret;
  139. }
  140. cmd_buf[0] = 0x04;
  141. cmd_buf[1] = 0x17;
  142. msleep(20); /* 20ms */
  143. if ((ret = i2c_writebytes(state,state->config->demod_address,
  144. cmd_buf,2))) {
  145. printk(KERN_WARNING "or51132: load_firmware error b\n");
  146. return ret;
  147. }
  148. cmd_buf[0] = 0x00;
  149. cmd_buf[1] = 0x00;
  150. msleep(20); /* 20ms */
  151. if ((ret = i2c_writebytes(state,state->config->demod_address,
  152. cmd_buf,2))) {
  153. printk(KERN_WARNING "or51132: load_firmware error c\n");
  154. return ret;
  155. }
  156. for(i=0;i<4;i++) {
  157. msleep(20); /* 20ms */
  158. /* Once upon a time, this command might have had something
  159. to do with getting the firmware version, but it's
  160. not used anymore:
  161. {0x04,0x00,0x30,0x00,i+1} */
  162. /* Read 8 bytes, two bytes at a time */
  163. if ((ret = i2c_readbytes(state,state->config->demod_address,
  164. &rec_buf[i*2],2))) {
  165. printk(KERN_WARNING
  166. "or51132: load_firmware error d - %d\n",i);
  167. return ret;
  168. }
  169. }
  170. printk(KERN_WARNING
  171. "or51132: Version: %02X%02X%02X%02X-%02X%02X%02X%02X (%02X%01X-%01X-%02X%01X-%01X)\n",
  172. rec_buf[1],rec_buf[0],rec_buf[3],rec_buf[2],
  173. rec_buf[5],rec_buf[4],rec_buf[7],rec_buf[6],
  174. rec_buf[3],rec_buf[2]>>4,rec_buf[2]&0x0f,
  175. rec_buf[5],rec_buf[4]>>4,rec_buf[4]&0x0f);
  176. cmd_buf[0] = 0x10;
  177. cmd_buf[1] = 0x00;
  178. cmd_buf[2] = 0x00;
  179. msleep(20); /* 20ms */
  180. if ((ret = i2c_writebytes(state,state->config->demod_address,
  181. cmd_buf,3))) {
  182. printk(KERN_WARNING "or51132: load_firmware error e\n");
  183. return ret;
  184. }
  185. return 0;
  186. };
  187. static int or51132_init(struct dvb_frontend* fe)
  188. {
  189. return 0;
  190. }
  191. static int or51132_read_ber(struct dvb_frontend* fe, u32* ber)
  192. {
  193. *ber = 0;
  194. return 0;
  195. }
  196. static int or51132_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  197. {
  198. *ucblocks = 0;
  199. return 0;
  200. }
  201. static int or51132_sleep(struct dvb_frontend* fe)
  202. {
  203. return 0;
  204. }
  205. static int or51132_setmode(struct dvb_frontend* fe)
  206. {
  207. struct or51132_state* state = fe->demodulator_priv;
  208. unsigned char cmd_buf[3];
  209. dprintk("setmode %d\n",(int)state->current_modulation);
  210. /* set operation mode in Receiver 1 register; */
  211. cmd_buf[0] = 0x04;
  212. cmd_buf[1] = 0x01;
  213. switch (state->current_modulation) {
  214. case QAM_256:
  215. case QAM_64:
  216. case QAM_AUTO:
  217. /* Auto-deinterleave; MPEG ser, MPEG2tr, phase noise-high*/
  218. cmd_buf[2] = 0x5F;
  219. break;
  220. case VSB_8:
  221. /* Auto CH, Auto NTSC rej, MPEGser, MPEG2tr, phase noise-high*/
  222. cmd_buf[2] = 0x50;
  223. break;
  224. default:
  225. printk("setmode:Modulation set to unsupported value\n");
  226. };
  227. if (i2c_writebytes(state,state->config->demod_address,
  228. cmd_buf,3)) {
  229. printk(KERN_WARNING "or51132: set_mode error 1\n");
  230. return -1;
  231. }
  232. dprintk("or51132: set #1 to %02x\n", cmd_buf[2]);
  233. /* Set operation mode in Receiver 6 register */
  234. cmd_buf[0] = 0x1C;
  235. switch (state->current_modulation) {
  236. case QAM_AUTO:
  237. /* REC MODE Normal Carrier Lock */
  238. cmd_buf[1] = 0x00;
  239. /* Channel MODE Auto QAM64/256 */
  240. cmd_buf[2] = 0x4f;
  241. break;
  242. case QAM_256:
  243. /* REC MODE Normal Carrier Lock */
  244. cmd_buf[1] = 0x00;
  245. /* Channel MODE QAM256 */
  246. cmd_buf[2] = 0x45;
  247. break;
  248. case QAM_64:
  249. /* REC MODE Normal Carrier Lock */
  250. cmd_buf[1] = 0x00;
  251. /* Channel MODE QAM64 */
  252. cmd_buf[2] = 0x43;
  253. break;
  254. case VSB_8:
  255. /* REC MODE inv IF spectrum, Normal */
  256. cmd_buf[1] = 0x03;
  257. /* Channel MODE ATSC/VSB8 */
  258. cmd_buf[2] = 0x06;
  259. break;
  260. default:
  261. printk("setmode: Modulation set to unsupported value\n");
  262. };
  263. msleep(20); /* 20ms */
  264. if (i2c_writebytes(state,state->config->demod_address,
  265. cmd_buf,3)) {
  266. printk(KERN_WARNING "or51132: set_mode error 2\n");
  267. return -1;
  268. }
  269. dprintk("or51132: set #6 to 0x%02x%02x\n", cmd_buf[1], cmd_buf[2]);
  270. return 0;
  271. }
  272. /* Some modulations use the same firmware. This classifies modulations
  273. by the firmware they use. */
  274. #define MOD_FWCLASS_UNKNOWN 0
  275. #define MOD_FWCLASS_VSB 1
  276. #define MOD_FWCLASS_QAM 2
  277. static int modulation_fw_class(fe_modulation_t modulation)
  278. {
  279. switch(modulation) {
  280. case VSB_8:
  281. return MOD_FWCLASS_VSB;
  282. case QAM_AUTO:
  283. case QAM_64:
  284. case QAM_256:
  285. return MOD_FWCLASS_QAM;
  286. default:
  287. return MOD_FWCLASS_UNKNOWN;
  288. }
  289. }
  290. static int or51132_set_parameters(struct dvb_frontend* fe,
  291. struct dvb_frontend_parameters *param)
  292. {
  293. int ret;
  294. u8 buf[4];
  295. struct or51132_state* state = fe->demodulator_priv;
  296. const struct firmware *fw;
  297. const char *fwname;
  298. int clock_mode;
  299. /* Upload new firmware only if we need a different one */
  300. if (modulation_fw_class(state->current_modulation) !=
  301. modulation_fw_class(param->u.vsb.modulation)) {
  302. switch(modulation_fw_class(param->u.vsb.modulation)) {
  303. case MOD_FWCLASS_VSB:
  304. dprintk("set_parameters VSB MODE\n");
  305. fwname = OR51132_VSB_FIRMWARE;
  306. /* Set non-punctured clock for VSB */
  307. clock_mode = 0;
  308. break;
  309. case MOD_FWCLASS_QAM:
  310. dprintk("set_parameters QAM MODE\n");
  311. fwname = OR51132_QAM_FIRMWARE;
  312. /* Set punctured clock for QAM */
  313. clock_mode = 1;
  314. break;
  315. default:
  316. printk("or51132: Modulation type(%d) UNSUPPORTED\n",
  317. param->u.vsb.modulation);
  318. return -1;
  319. }
  320. printk("or51132: Waiting for firmware upload(%s)...\n",
  321. fwname);
  322. ret = request_firmware(&fw, fwname, &state->i2c->dev);
  323. if (ret) {
  324. printk(KERN_WARNING "or51132: No firmware up"
  325. "loaded(timeout or file not found?)\n");
  326. return ret;
  327. }
  328. ret = or51132_load_firmware(fe, fw);
  329. release_firmware(fw);
  330. if (ret) {
  331. printk(KERN_WARNING "or51132: Writing firmware to "
  332. "device failed!\n");
  333. return ret;
  334. }
  335. printk("or51132: Firmware upload complete.\n");
  336. state->config->set_ts_params(fe, clock_mode);
  337. }
  338. /* Change only if we are actually changing the modulation */
  339. if (state->current_modulation != param->u.vsb.modulation) {
  340. state->current_modulation = param->u.vsb.modulation;
  341. or51132_setmode(fe);
  342. }
  343. dvb_pll_configure(state->config->pll_desc, buf,
  344. param->frequency, 0);
  345. dprintk("set_parameters tuner bytes: 0x%02x 0x%02x "
  346. "0x%02x 0x%02x\n",buf[0],buf[1],buf[2],buf[3]);
  347. if (i2c_writebytes(state, state->config->pll_address, buf, 4))
  348. printk(KERN_WARNING "or51132: set_parameters error "
  349. "writing to tuner\n");
  350. /* Set to current mode */
  351. or51132_setmode(fe);
  352. /* Update current frequency */
  353. state->current_frequency = param->frequency;
  354. return 0;
  355. }
  356. static int or51132_get_parameters(struct dvb_frontend* fe,
  357. struct dvb_frontend_parameters *param)
  358. {
  359. struct or51132_state* state = fe->demodulator_priv;
  360. u8 buf[2];
  361. /* Receiver Status */
  362. buf[0]=0x04;
  363. buf[1]=0x00;
  364. msleep(30); /* 30ms */
  365. if (i2c_writebytes(state,state->config->demod_address,buf,2)) {
  366. printk(KERN_WARNING "or51132: get_parameters write error\n");
  367. return -EREMOTEIO;
  368. }
  369. msleep(30); /* 30ms */
  370. if (i2c_readbytes(state,state->config->demod_address,buf,2)) {
  371. printk(KERN_WARNING "or51132: get_parameters read error\n");
  372. return -EREMOTEIO;
  373. }
  374. switch(buf[0]) {
  375. case 0x06: param->u.vsb.modulation = VSB_8; break;
  376. case 0x43: param->u.vsb.modulation = QAM_64; break;
  377. case 0x45: param->u.vsb.modulation = QAM_256; break;
  378. default:
  379. printk(KERN_WARNING "or51132: unknown status 0x%02x\n",
  380. buf[0]);
  381. return -EREMOTEIO;
  382. }
  383. /* FIXME: Read frequency from frontend, take AFC into account */
  384. param->frequency = state->current_frequency;
  385. /* FIXME: How to read inversion setting? Receiver 6 register? */
  386. param->inversion = INVERSION_AUTO;
  387. return 0;
  388. }
  389. static int or51132_read_status(struct dvb_frontend* fe, fe_status_t* status)
  390. {
  391. struct or51132_state* state = fe->demodulator_priv;
  392. unsigned char rec_buf[2];
  393. unsigned char snd_buf[2];
  394. *status = 0;
  395. /* Receiver Status */
  396. snd_buf[0]=0x04;
  397. snd_buf[1]=0x00;
  398. msleep(30); /* 30ms */
  399. if (i2c_writebytes(state,state->config->demod_address,snd_buf,2)) {
  400. printk(KERN_WARNING "or51132: read_status write error\n");
  401. return -1;
  402. }
  403. msleep(30); /* 30ms */
  404. if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
  405. printk(KERN_WARNING "or51132: read_status read error\n");
  406. return -1;
  407. }
  408. dprintk("read_status %x %x\n",rec_buf[0],rec_buf[1]);
  409. if (rec_buf[1] & 0x01) { /* Receiver Lock */
  410. *status |= FE_HAS_SIGNAL;
  411. *status |= FE_HAS_CARRIER;
  412. *status |= FE_HAS_VITERBI;
  413. *status |= FE_HAS_SYNC;
  414. *status |= FE_HAS_LOCK;
  415. }
  416. return 0;
  417. }
  418. /* log10-1 table at .5 increments from 1 to 100.5 */
  419. static unsigned int i100x20log10[] = {
  420. 0, 352, 602, 795, 954, 1088, 1204, 1306, 1397, 1480,
  421. 1556, 1625, 1690, 1750, 1806, 1858, 1908, 1955, 2000, 2042,
  422. 2082, 2121, 2158, 2193, 2227, 2260, 2292, 2322, 2352, 2380,
  423. 2408, 2434, 2460, 2486, 2510, 2534, 2557, 2580, 2602, 2623,
  424. 2644, 2664, 2684, 2704, 2723, 2742, 2760, 2778, 2795, 2813,
  425. 2829, 2846, 2862, 2878, 2894, 2909, 2924, 2939, 2954, 2968,
  426. 2982, 2996, 3010, 3023, 3037, 3050, 3062, 3075, 3088, 3100,
  427. 3112, 3124, 3136, 3148, 3159, 3170, 3182, 3193, 3204, 3214,
  428. 3225, 3236, 3246, 3256, 3266, 3276, 3286, 3296, 3306, 3316,
  429. 3325, 3334, 3344, 3353, 3362, 3371, 3380, 3389, 3397, 3406,
  430. 3415, 3423, 3432, 3440, 3448, 3456, 3464, 3472, 3480, 3488,
  431. 3496, 3504, 3511, 3519, 3526, 3534, 3541, 3549, 3556, 3563,
  432. 3570, 3577, 3584, 3591, 3598, 3605, 3612, 3619, 3625, 3632,
  433. 3639, 3645, 3652, 3658, 3665, 3671, 3677, 3683, 3690, 3696,
  434. 3702, 3708, 3714, 3720, 3726, 3732, 3738, 3744, 3750, 3755,
  435. 3761, 3767, 3772, 3778, 3784, 3789, 3795, 3800, 3806, 3811,
  436. 3816, 3822, 3827, 3832, 3838, 3843, 3848, 3853, 3858, 3863,
  437. 3868, 3874, 3879, 3884, 3888, 3893, 3898, 3903, 3908, 3913,
  438. 3918, 3922, 3927, 3932, 3936, 3941, 3946, 3950, 3955, 3960,
  439. 3964, 3969, 3973, 3978, 3982, 3986, 3991, 3995, 4000, 4004,
  440. };
  441. static unsigned int denom[] = {1,1,100,1000,10000,100000,1000000,10000000,100000000};
  442. static unsigned int i20Log10(unsigned short val)
  443. {
  444. unsigned int rntval = 100;
  445. unsigned int tmp = val;
  446. unsigned int exp = 1;
  447. while(tmp > 100) {tmp /= 100; exp++;}
  448. val = (2 * val)/denom[exp];
  449. if (exp > 1) rntval = 2000*exp;
  450. rntval += i100x20log10[val];
  451. return rntval;
  452. }
  453. static int or51132_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  454. {
  455. struct or51132_state* state = fe->demodulator_priv;
  456. unsigned char rec_buf[2];
  457. unsigned char snd_buf[2];
  458. u8 rcvr_stat;
  459. u16 snr_equ;
  460. u32 signal_strength;
  461. int usK;
  462. snd_buf[0]=0x04;
  463. snd_buf[1]=0x02; /* SNR after Equalizer */
  464. msleep(30); /* 30ms */
  465. if (i2c_writebytes(state,state->config->demod_address,snd_buf,2)) {
  466. printk(KERN_WARNING "or51132: read_status write error\n");
  467. return -1;
  468. }
  469. msleep(30); /* 30ms */
  470. if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
  471. printk(KERN_WARNING "or51132: read_status read error\n");
  472. return -1;
  473. }
  474. snr_equ = rec_buf[0] | (rec_buf[1] << 8);
  475. dprintk("read_signal_strength snr_equ %x %x (%i)\n",rec_buf[0],rec_buf[1],snr_equ);
  476. /* Receiver Status */
  477. snd_buf[0]=0x04;
  478. snd_buf[1]=0x00;
  479. msleep(30); /* 30ms */
  480. if (i2c_writebytes(state,state->config->demod_address,snd_buf,2)) {
  481. printk(KERN_WARNING "or51132: read_signal_strength read_status write error\n");
  482. return -1;
  483. }
  484. msleep(30); /* 30ms */
  485. if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
  486. printk(KERN_WARNING "or51132: read_signal_strength read_status read error\n");
  487. return -1;
  488. }
  489. dprintk("read_signal_strength read_status %x %x\n",rec_buf[0],rec_buf[1]);
  490. rcvr_stat = rec_buf[1];
  491. usK = (rcvr_stat & 0x10) ? 3 : 0;
  492. /* The value reported back from the frontend will be FFFF=100% 0000=0% */
  493. signal_strength = (((8952 - i20Log10(snr_equ) - usK*100)/3+5)*65535)/1000;
  494. if (signal_strength > 0xffff)
  495. *strength = 0xffff;
  496. else
  497. *strength = signal_strength;
  498. dprintk("read_signal_strength %i\n",*strength);
  499. return 0;
  500. }
  501. static int or51132_read_snr(struct dvb_frontend* fe, u16* snr)
  502. {
  503. struct or51132_state* state = fe->demodulator_priv;
  504. unsigned char rec_buf[2];
  505. unsigned char snd_buf[2];
  506. u16 snr_equ;
  507. snd_buf[0]=0x04;
  508. snd_buf[1]=0x02; /* SNR after Equalizer */
  509. msleep(30); /* 30ms */
  510. if (i2c_writebytes(state,state->config->demod_address,snd_buf,2)) {
  511. printk(KERN_WARNING "or51132: read_snr write error\n");
  512. return -1;
  513. }
  514. msleep(30); /* 30ms */
  515. if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
  516. printk(KERN_WARNING "or51132: read_snr dvr read error\n");
  517. return -1;
  518. }
  519. snr_equ = rec_buf[0] | (rec_buf[1] << 8);
  520. dprintk("read_snr snr_equ %x %x (%i)\n",rec_buf[0],rec_buf[1],snr_equ);
  521. *snr = 0xFFFF - snr_equ;
  522. dprintk("read_snr %i\n",*snr);
  523. return 0;
  524. }
  525. static int or51132_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fe_tune_settings)
  526. {
  527. fe_tune_settings->min_delay_ms = 500;
  528. fe_tune_settings->step_size = 0;
  529. fe_tune_settings->max_drift = 0;
  530. return 0;
  531. }
  532. static void or51132_release(struct dvb_frontend* fe)
  533. {
  534. struct or51132_state* state = fe->demodulator_priv;
  535. kfree(state);
  536. }
  537. static struct dvb_frontend_ops or51132_ops;
  538. struct dvb_frontend* or51132_attach(const struct or51132_config* config,
  539. struct i2c_adapter* i2c)
  540. {
  541. struct or51132_state* state = NULL;
  542. /* Allocate memory for the internal state */
  543. state = kmalloc(sizeof(struct or51132_state), GFP_KERNEL);
  544. if (state == NULL)
  545. goto error;
  546. /* Setup the state */
  547. state->config = config;
  548. state->i2c = i2c;
  549. memcpy(&state->ops, &or51132_ops, sizeof(struct dvb_frontend_ops));
  550. state->current_frequency = -1;
  551. state->current_modulation = -1;
  552. /* Create dvb_frontend */
  553. state->frontend.ops = &state->ops;
  554. state->frontend.demodulator_priv = state;
  555. return &state->frontend;
  556. error:
  557. kfree(state);
  558. return NULL;
  559. }
  560. static struct dvb_frontend_ops or51132_ops = {
  561. .info = {
  562. .name = "Oren OR51132 VSB/QAM Frontend",
  563. .type = FE_ATSC,
  564. .frequency_min = 44000000,
  565. .frequency_max = 958000000,
  566. .frequency_stepsize = 166666,
  567. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  568. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  569. FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_QAM_AUTO |
  570. FE_CAN_8VSB
  571. },
  572. .release = or51132_release,
  573. .init = or51132_init,
  574. .sleep = or51132_sleep,
  575. .set_frontend = or51132_set_parameters,
  576. .get_frontend = or51132_get_parameters,
  577. .get_tune_settings = or51132_get_tune_settings,
  578. .read_status = or51132_read_status,
  579. .read_ber = or51132_read_ber,
  580. .read_signal_strength = or51132_read_signal_strength,
  581. .read_snr = or51132_read_snr,
  582. .read_ucblocks = or51132_read_ucblocks,
  583. };
  584. module_param(debug, int, 0644);
  585. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  586. MODULE_DESCRIPTION("OR51132 ATSC [pcHDTV HD-3000] (8VSB & ITU J83 AnnexB FEC QAM64/256) Demodulator Driver");
  587. MODULE_AUTHOR("Kirk Lapray");
  588. MODULE_LICENSE("GPL");
  589. EXPORT_SYMBOL(or51132_attach);
  590. /*
  591. * Local variables:
  592. * c-basic-offset: 8
  593. * End:
  594. */