or51211.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * Support for OR51211 (pcHDTV HD-2000) - VSB
  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 external firmware. Please use the command
  26. * "<kerneldir>/Documentation/dvb/get_dvb_firmware or51211" to
  27. * download/extract it, and then copy it to /usr/lib/hotplug/firmware
  28. * or /lib/firmware (depending on configuration of firmware hotplug).
  29. */
  30. #define OR51211_DEFAULT_FIRMWARE "dvb-fe-or51211.fw"
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/device.h>
  35. #include <linux/firmware.h>
  36. #include <linux/string.h>
  37. #include <linux/slab.h>
  38. #include <asm/byteorder.h>
  39. #include "dvb_math.h"
  40. #include "dvb_frontend.h"
  41. #include "or51211.h"
  42. static int debug;
  43. #define dprintk(args...) \
  44. do { \
  45. if (debug) printk(KERN_DEBUG "or51211: " args); \
  46. } while (0)
  47. static u8 run_buf[] = {0x7f,0x01};
  48. static u8 cmd_buf[] = {0x04,0x01,0x50,0x80,0x06}; // ATSC
  49. struct or51211_state {
  50. struct i2c_adapter* i2c;
  51. /* Configuration settings */
  52. const struct or51211_config* config;
  53. struct dvb_frontend frontend;
  54. struct bt878* bt;
  55. /* Demodulator private data */
  56. u8 initialized:1;
  57. u32 snr; /* Result of last SNR claculation */
  58. /* Tuner private data */
  59. u32 current_frequency;
  60. };
  61. static int i2c_writebytes (struct or51211_state* state, u8 reg, u8 *buf,
  62. int len)
  63. {
  64. int err;
  65. struct i2c_msg msg;
  66. msg.addr = reg;
  67. msg.flags = 0;
  68. msg.len = len;
  69. msg.buf = buf;
  70. if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
  71. printk(KERN_WARNING "or51211: i2c_writebytes error "
  72. "(addr %02x, err == %i)\n", reg, err);
  73. return -EREMOTEIO;
  74. }
  75. return 0;
  76. }
  77. static u8 i2c_readbytes (struct or51211_state* state, u8 reg, u8* buf, int len)
  78. {
  79. int err;
  80. struct i2c_msg msg;
  81. msg.addr = reg;
  82. msg.flags = I2C_M_RD;
  83. msg.len = len;
  84. msg.buf = buf;
  85. if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
  86. printk(KERN_WARNING "or51211: i2c_readbytes error "
  87. "(addr %02x, err == %i)\n", reg, err);
  88. return -EREMOTEIO;
  89. }
  90. return 0;
  91. }
  92. static int or51211_load_firmware (struct dvb_frontend* fe,
  93. const struct firmware *fw)
  94. {
  95. struct or51211_state* state = fe->demodulator_priv;
  96. u8 tudata[585];
  97. int i;
  98. dprintk("Firmware is %zd bytes\n",fw->size);
  99. /* Get eprom data */
  100. tudata[0] = 17;
  101. if (i2c_writebytes(state,0x50,tudata,1)) {
  102. printk(KERN_WARNING "or51211:load_firmware error eprom addr\n");
  103. return -1;
  104. }
  105. if (i2c_readbytes(state,0x50,&tudata[145],192)) {
  106. printk(KERN_WARNING "or51211: load_firmware error eprom\n");
  107. return -1;
  108. }
  109. /* Create firmware buffer */
  110. for (i = 0; i < 145; i++)
  111. tudata[i] = fw->data[i];
  112. for (i = 0; i < 248; i++)
  113. tudata[i+337] = fw->data[145+i];
  114. state->config->reset(fe);
  115. if (i2c_writebytes(state,state->config->demod_address,tudata,585)) {
  116. printk(KERN_WARNING "or51211: load_firmware error 1\n");
  117. return -1;
  118. }
  119. msleep(1);
  120. if (i2c_writebytes(state,state->config->demod_address,
  121. &fw->data[393],8125)) {
  122. printk(KERN_WARNING "or51211: load_firmware error 2\n");
  123. return -1;
  124. }
  125. msleep(1);
  126. if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
  127. printk(KERN_WARNING "or51211: load_firmware error 3\n");
  128. return -1;
  129. }
  130. /* Wait at least 5 msec */
  131. msleep(10);
  132. if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
  133. printk(KERN_WARNING "or51211: load_firmware error 4\n");
  134. return -1;
  135. }
  136. msleep(10);
  137. printk("or51211: Done.\n");
  138. return 0;
  139. };
  140. static int or51211_setmode(struct dvb_frontend* fe, int mode)
  141. {
  142. struct or51211_state* state = fe->demodulator_priv;
  143. u8 rec_buf[14];
  144. state->config->setmode(fe, mode);
  145. if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
  146. printk(KERN_WARNING "or51211: setmode error 1\n");
  147. return -1;
  148. }
  149. /* Wait at least 5 msec */
  150. msleep(10);
  151. if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
  152. printk(KERN_WARNING "or51211: setmode error 2\n");
  153. return -1;
  154. }
  155. msleep(10);
  156. /* Set operation mode in Receiver 1 register;
  157. * type 1:
  158. * data 0x50h Automatic sets receiver channel conditions
  159. * Automatic NTSC rejection filter
  160. * Enable MPEG serial data output
  161. * MPEG2tr
  162. * High tuner phase noise
  163. * normal +/-150kHz Carrier acquisition range
  164. */
  165. if (i2c_writebytes(state,state->config->demod_address,cmd_buf,3)) {
  166. printk(KERN_WARNING "or51211: setmode error 3\n");
  167. return -1;
  168. }
  169. rec_buf[0] = 0x04;
  170. rec_buf[1] = 0x00;
  171. rec_buf[2] = 0x03;
  172. rec_buf[3] = 0x00;
  173. msleep(20);
  174. if (i2c_writebytes(state,state->config->demod_address,rec_buf,3)) {
  175. printk(KERN_WARNING "or51211: setmode error 5\n");
  176. }
  177. msleep(3);
  178. if (i2c_readbytes(state,state->config->demod_address,&rec_buf[10],2)) {
  179. printk(KERN_WARNING "or51211: setmode error 6");
  180. return -1;
  181. }
  182. dprintk("setmode rec status %02x %02x\n",rec_buf[10],rec_buf[11]);
  183. return 0;
  184. }
  185. static int or51211_set_parameters(struct dvb_frontend* fe,
  186. struct dvb_frontend_parameters *param)
  187. {
  188. struct or51211_state* state = fe->demodulator_priv;
  189. u32 freq = 0;
  190. u16 tunerfreq = 0;
  191. u8 buf[4];
  192. /* Change only if we are actually changing the channel */
  193. if (state->current_frequency != param->frequency) {
  194. freq = 44000 + (param->frequency/1000);
  195. tunerfreq = freq * 16/1000;
  196. dprintk("set_parameters frequency = %d (tunerfreq = %d)\n",
  197. param->frequency,tunerfreq);
  198. buf[0] = (tunerfreq >> 8) & 0x7F;
  199. buf[1] = (tunerfreq & 0xFF);
  200. buf[2] = 0x8E;
  201. if (param->frequency < 157250000) {
  202. buf[3] = 0xA0;
  203. dprintk("set_parameters VHF low range\n");
  204. } else if (param->frequency < 454000000) {
  205. buf[3] = 0x90;
  206. dprintk("set_parameters VHF high range\n");
  207. } else {
  208. buf[3] = 0x30;
  209. dprintk("set_parameters UHF range\n");
  210. }
  211. dprintk("set_parameters tuner bytes: 0x%02x 0x%02x "
  212. "0x%02x 0x%02x\n",buf[0],buf[1],buf[2],buf[3]);
  213. if (i2c_writebytes(state,0xC2>>1,buf,4))
  214. printk(KERN_WARNING "or51211:set_parameters error "
  215. "writing to tuner\n");
  216. /* Set to ATSC mode */
  217. or51211_setmode(fe,0);
  218. /* Update current frequency */
  219. state->current_frequency = param->frequency;
  220. }
  221. return 0;
  222. }
  223. static int or51211_read_status(struct dvb_frontend* fe, fe_status_t* status)
  224. {
  225. struct or51211_state* state = fe->demodulator_priv;
  226. unsigned char rec_buf[2];
  227. unsigned char snd_buf[] = {0x04,0x00,0x03,0x00};
  228. *status = 0;
  229. /* Receiver Status */
  230. if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
  231. printk(KERN_WARNING "or51132: read_status write error\n");
  232. return -1;
  233. }
  234. msleep(3);
  235. if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
  236. printk(KERN_WARNING "or51132: read_status read error\n");
  237. return -1;
  238. }
  239. dprintk("read_status %x %x\n",rec_buf[0],rec_buf[1]);
  240. if (rec_buf[0] & 0x01) { /* Receiver Lock */
  241. *status |= FE_HAS_SIGNAL;
  242. *status |= FE_HAS_CARRIER;
  243. *status |= FE_HAS_VITERBI;
  244. *status |= FE_HAS_SYNC;
  245. *status |= FE_HAS_LOCK;
  246. }
  247. return 0;
  248. }
  249. /* Calculate SNR estimation (scaled by 2^24)
  250. 8-VSB SNR equation from Oren datasheets
  251. For 8-VSB:
  252. SNR[dB] = 10 * log10(219037.9454 / MSE^2 )
  253. We re-write the snr equation as:
  254. SNR * 2^24 = 10*(c - 2*intlog10(MSE))
  255. Where for 8-VSB, c = log10(219037.9454) * 2^24 */
  256. static u32 calculate_snr(u32 mse, u32 c)
  257. {
  258. if (mse == 0) /* No signal */
  259. return 0;
  260. mse = 2*intlog10(mse);
  261. if (mse > c) {
  262. /* Negative SNR, which is possible, but realisticly the
  263. demod will lose lock before the signal gets this bad. The
  264. API only allows for unsigned values, so just return 0 */
  265. return 0;
  266. }
  267. return 10*(c - mse);
  268. }
  269. static int or51211_read_snr(struct dvb_frontend* fe, u16* snr)
  270. {
  271. struct or51211_state* state = fe->demodulator_priv;
  272. u8 rec_buf[2];
  273. u8 snd_buf[3];
  274. /* SNR after Equalizer */
  275. snd_buf[0] = 0x04;
  276. snd_buf[1] = 0x00;
  277. snd_buf[2] = 0x04;
  278. if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
  279. printk(KERN_WARNING "%s: error writing snr reg\n",
  280. __FUNCTION__);
  281. return -1;
  282. }
  283. if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
  284. printk(KERN_WARNING "%s: read_status read error\n",
  285. __FUNCTION__);
  286. return -1;
  287. }
  288. state->snr = calculate_snr(rec_buf[0], 89599047);
  289. *snr = (state->snr) >> 16;
  290. dprintk("%s: noise = 0x%02x, snr = %d.%02d dB\n", __FUNCTION__, rec_buf[0],
  291. state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
  292. return 0;
  293. }
  294. static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  295. {
  296. /* Calculate Strength from SNR up to 35dB */
  297. /* Even though the SNR can go higher than 35dB, there is some comfort */
  298. /* factor in having a range of strong signals that can show at 100% */
  299. struct or51211_state* state = (struct or51211_state*)fe->demodulator_priv;
  300. u16 snr;
  301. int ret;
  302. ret = fe->ops.read_snr(fe, &snr);
  303. if (ret != 0)
  304. return ret;
  305. /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
  306. /* scale the range 0 - 35*2^24 into 0 - 65535 */
  307. if (state->snr >= 8960 * 0x10000)
  308. *strength = 0xffff;
  309. else
  310. *strength = state->snr / 8960;
  311. return 0;
  312. }
  313. static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
  314. {
  315. *ber = -ENOSYS;
  316. return 0;
  317. }
  318. static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  319. {
  320. *ucblocks = -ENOSYS;
  321. return 0;
  322. }
  323. static int or51211_sleep(struct dvb_frontend* fe)
  324. {
  325. return 0;
  326. }
  327. static int or51211_init(struct dvb_frontend* fe)
  328. {
  329. struct or51211_state* state = fe->demodulator_priv;
  330. const struct or51211_config* config = state->config;
  331. const struct firmware* fw;
  332. unsigned char get_ver_buf[] = {0x04,0x00,0x30,0x00,0x00};
  333. unsigned char rec_buf[14];
  334. int ret,i;
  335. if (!state->initialized) {
  336. /* Request the firmware, this will block until it uploads */
  337. printk(KERN_INFO "or51211: Waiting for firmware upload "
  338. "(%s)...\n", OR51211_DEFAULT_FIRMWARE);
  339. ret = config->request_firmware(fe, &fw,
  340. OR51211_DEFAULT_FIRMWARE);
  341. printk(KERN_INFO "or51211:Got Hotplug firmware\n");
  342. if (ret) {
  343. printk(KERN_WARNING "or51211: No firmware uploaded "
  344. "(timeout or file not found?)\n");
  345. return ret;
  346. }
  347. ret = or51211_load_firmware(fe, fw);
  348. release_firmware(fw);
  349. if (ret) {
  350. printk(KERN_WARNING "or51211: Writing firmware to "
  351. "device failed!\n");
  352. return ret;
  353. }
  354. printk(KERN_INFO "or51211: Firmware upload complete.\n");
  355. /* Set operation mode in Receiver 1 register;
  356. * type 1:
  357. * data 0x50h Automatic sets receiver channel conditions
  358. * Automatic NTSC rejection filter
  359. * Enable MPEG serial data output
  360. * MPEG2tr
  361. * High tuner phase noise
  362. * normal +/-150kHz Carrier acquisition range
  363. */
  364. if (i2c_writebytes(state,state->config->demod_address,
  365. cmd_buf,3)) {
  366. printk(KERN_WARNING "or51211: Load DVR Error 5\n");
  367. return -1;
  368. }
  369. /* Read back ucode version to besure we loaded correctly */
  370. /* and are really up and running */
  371. rec_buf[0] = 0x04;
  372. rec_buf[1] = 0x00;
  373. rec_buf[2] = 0x03;
  374. rec_buf[3] = 0x00;
  375. msleep(30);
  376. if (i2c_writebytes(state,state->config->demod_address,
  377. rec_buf,3)) {
  378. printk(KERN_WARNING "or51211: Load DVR Error A\n");
  379. return -1;
  380. }
  381. msleep(3);
  382. if (i2c_readbytes(state,state->config->demod_address,
  383. &rec_buf[10],2)) {
  384. printk(KERN_WARNING "or51211: Load DVR Error B\n");
  385. return -1;
  386. }
  387. rec_buf[0] = 0x04;
  388. rec_buf[1] = 0x00;
  389. rec_buf[2] = 0x01;
  390. rec_buf[3] = 0x00;
  391. msleep(20);
  392. if (i2c_writebytes(state,state->config->demod_address,
  393. rec_buf,3)) {
  394. printk(KERN_WARNING "or51211: Load DVR Error C\n");
  395. return -1;
  396. }
  397. msleep(3);
  398. if (i2c_readbytes(state,state->config->demod_address,
  399. &rec_buf[12],2)) {
  400. printk(KERN_WARNING "or51211: Load DVR Error D\n");
  401. return -1;
  402. }
  403. for (i = 0; i < 8; i++)
  404. rec_buf[i]=0xed;
  405. for (i = 0; i < 5; i++) {
  406. msleep(30);
  407. get_ver_buf[4] = i+1;
  408. if (i2c_writebytes(state,state->config->demod_address,
  409. get_ver_buf,5)) {
  410. printk(KERN_WARNING "or51211:Load DVR Error 6"
  411. " - %d\n",i);
  412. return -1;
  413. }
  414. msleep(3);
  415. if (i2c_readbytes(state,state->config->demod_address,
  416. &rec_buf[i*2],2)) {
  417. printk(KERN_WARNING "or51211:Load DVR Error 7"
  418. " - %d\n",i);
  419. return -1;
  420. }
  421. /* If we didn't receive the right index, try again */
  422. if ((int)rec_buf[i*2+1]!=i+1){
  423. i--;
  424. }
  425. }
  426. dprintk("read_fwbits %x %x %x %x %x %x %x %x %x %x\n",
  427. rec_buf[0], rec_buf[1], rec_buf[2], rec_buf[3],
  428. rec_buf[4], rec_buf[5], rec_buf[6], rec_buf[7],
  429. rec_buf[8], rec_buf[9]);
  430. printk(KERN_INFO "or51211: ver TU%02x%02x%02x VSB mode %02x"
  431. " Status %02x\n",
  432. rec_buf[2], rec_buf[4],rec_buf[6],
  433. rec_buf[12],rec_buf[10]);
  434. rec_buf[0] = 0x04;
  435. rec_buf[1] = 0x00;
  436. rec_buf[2] = 0x03;
  437. rec_buf[3] = 0x00;
  438. msleep(20);
  439. if (i2c_writebytes(state,state->config->demod_address,
  440. rec_buf,3)) {
  441. printk(KERN_WARNING "or51211: Load DVR Error 8\n");
  442. return -1;
  443. }
  444. msleep(20);
  445. if (i2c_readbytes(state,state->config->demod_address,
  446. &rec_buf[8],2)) {
  447. printk(KERN_WARNING "or51211: Load DVR Error 9\n");
  448. return -1;
  449. }
  450. state->initialized = 1;
  451. }
  452. return 0;
  453. }
  454. static int or51211_get_tune_settings(struct dvb_frontend* fe,
  455. struct dvb_frontend_tune_settings* fesettings)
  456. {
  457. fesettings->min_delay_ms = 500;
  458. fesettings->step_size = 0;
  459. fesettings->max_drift = 0;
  460. return 0;
  461. }
  462. static void or51211_release(struct dvb_frontend* fe)
  463. {
  464. struct or51211_state* state = fe->demodulator_priv;
  465. state->config->sleep(fe);
  466. kfree(state);
  467. }
  468. static struct dvb_frontend_ops or51211_ops;
  469. struct dvb_frontend* or51211_attach(const struct or51211_config* config,
  470. struct i2c_adapter* i2c)
  471. {
  472. struct or51211_state* state = NULL;
  473. /* Allocate memory for the internal state */
  474. state = kmalloc(sizeof(struct or51211_state), GFP_KERNEL);
  475. if (state == NULL)
  476. goto error;
  477. /* Setup the state */
  478. state->config = config;
  479. state->i2c = i2c;
  480. state->initialized = 0;
  481. state->current_frequency = 0;
  482. /* Create dvb_frontend */
  483. memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops));
  484. state->frontend.demodulator_priv = state;
  485. return &state->frontend;
  486. error:
  487. kfree(state);
  488. return NULL;
  489. }
  490. static struct dvb_frontend_ops or51211_ops = {
  491. .info = {
  492. .name = "Oren OR51211 VSB Frontend",
  493. .type = FE_ATSC,
  494. .frequency_min = 44000000,
  495. .frequency_max = 958000000,
  496. .frequency_stepsize = 166666,
  497. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  498. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  499. FE_CAN_8VSB
  500. },
  501. .release = or51211_release,
  502. .init = or51211_init,
  503. .sleep = or51211_sleep,
  504. .set_frontend = or51211_set_parameters,
  505. .get_tune_settings = or51211_get_tune_settings,
  506. .read_status = or51211_read_status,
  507. .read_ber = or51211_read_ber,
  508. .read_signal_strength = or51211_read_signal_strength,
  509. .read_snr = or51211_read_snr,
  510. .read_ucblocks = or51211_read_ucblocks,
  511. };
  512. module_param(debug, int, 0644);
  513. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  514. MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
  515. MODULE_AUTHOR("Kirk Lapray");
  516. MODULE_LICENSE("GPL");
  517. EXPORT_SYMBOL(or51211_attach);