or51211.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. /* Change only if we are actually changing the channel */
  190. if (state->current_frequency != param->frequency) {
  191. if (fe->ops.tuner_ops.set_params) {
  192. fe->ops.tuner_ops.set_params(fe, param);
  193. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  194. }
  195. /* Set to ATSC mode */
  196. or51211_setmode(fe,0);
  197. /* Update current frequency */
  198. state->current_frequency = param->frequency;
  199. }
  200. return 0;
  201. }
  202. static int or51211_read_status(struct dvb_frontend* fe, fe_status_t* status)
  203. {
  204. struct or51211_state* state = fe->demodulator_priv;
  205. unsigned char rec_buf[2];
  206. unsigned char snd_buf[] = {0x04,0x00,0x03,0x00};
  207. *status = 0;
  208. /* Receiver Status */
  209. if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
  210. printk(KERN_WARNING "or51132: read_status write error\n");
  211. return -1;
  212. }
  213. msleep(3);
  214. if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
  215. printk(KERN_WARNING "or51132: read_status read error\n");
  216. return -1;
  217. }
  218. dprintk("read_status %x %x\n",rec_buf[0],rec_buf[1]);
  219. if (rec_buf[0] & 0x01) { /* Receiver Lock */
  220. *status |= FE_HAS_SIGNAL;
  221. *status |= FE_HAS_CARRIER;
  222. *status |= FE_HAS_VITERBI;
  223. *status |= FE_HAS_SYNC;
  224. *status |= FE_HAS_LOCK;
  225. }
  226. return 0;
  227. }
  228. /* Calculate SNR estimation (scaled by 2^24)
  229. 8-VSB SNR equation from Oren datasheets
  230. For 8-VSB:
  231. SNR[dB] = 10 * log10(219037.9454 / MSE^2 )
  232. We re-write the snr equation as:
  233. SNR * 2^24 = 10*(c - 2*intlog10(MSE))
  234. Where for 8-VSB, c = log10(219037.9454) * 2^24 */
  235. static u32 calculate_snr(u32 mse, u32 c)
  236. {
  237. if (mse == 0) /* No signal */
  238. return 0;
  239. mse = 2*intlog10(mse);
  240. if (mse > c) {
  241. /* Negative SNR, which is possible, but realisticly the
  242. demod will lose lock before the signal gets this bad. The
  243. API only allows for unsigned values, so just return 0 */
  244. return 0;
  245. }
  246. return 10*(c - mse);
  247. }
  248. static int or51211_read_snr(struct dvb_frontend* fe, u16* snr)
  249. {
  250. struct or51211_state* state = fe->demodulator_priv;
  251. u8 rec_buf[2];
  252. u8 snd_buf[3];
  253. /* SNR after Equalizer */
  254. snd_buf[0] = 0x04;
  255. snd_buf[1] = 0x00;
  256. snd_buf[2] = 0x04;
  257. if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
  258. printk(KERN_WARNING "%s: error writing snr reg\n",
  259. __FUNCTION__);
  260. return -1;
  261. }
  262. if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
  263. printk(KERN_WARNING "%s: read_status read error\n",
  264. __FUNCTION__);
  265. return -1;
  266. }
  267. state->snr = calculate_snr(rec_buf[0], 89599047);
  268. *snr = (state->snr) >> 16;
  269. dprintk("%s: noise = 0x%02x, snr = %d.%02d dB\n", __FUNCTION__, rec_buf[0],
  270. state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
  271. return 0;
  272. }
  273. static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  274. {
  275. /* Calculate Strength from SNR up to 35dB */
  276. /* Even though the SNR can go higher than 35dB, there is some comfort */
  277. /* factor in having a range of strong signals that can show at 100% */
  278. struct or51211_state* state = (struct or51211_state*)fe->demodulator_priv;
  279. u16 snr;
  280. int ret;
  281. ret = fe->ops.read_snr(fe, &snr);
  282. if (ret != 0)
  283. return ret;
  284. /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
  285. /* scale the range 0 - 35*2^24 into 0 - 65535 */
  286. if (state->snr >= 8960 * 0x10000)
  287. *strength = 0xffff;
  288. else
  289. *strength = state->snr / 8960;
  290. return 0;
  291. }
  292. static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
  293. {
  294. *ber = -ENOSYS;
  295. return 0;
  296. }
  297. static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  298. {
  299. *ucblocks = -ENOSYS;
  300. return 0;
  301. }
  302. static int or51211_sleep(struct dvb_frontend* fe)
  303. {
  304. return 0;
  305. }
  306. static int or51211_init(struct dvb_frontend* fe)
  307. {
  308. struct or51211_state* state = fe->demodulator_priv;
  309. const struct or51211_config* config = state->config;
  310. const struct firmware* fw;
  311. unsigned char get_ver_buf[] = {0x04,0x00,0x30,0x00,0x00};
  312. unsigned char rec_buf[14];
  313. int ret,i;
  314. if (!state->initialized) {
  315. /* Request the firmware, this will block until it uploads */
  316. printk(KERN_INFO "or51211: Waiting for firmware upload "
  317. "(%s)...\n", OR51211_DEFAULT_FIRMWARE);
  318. ret = config->request_firmware(fe, &fw,
  319. OR51211_DEFAULT_FIRMWARE);
  320. printk(KERN_INFO "or51211:Got Hotplug firmware\n");
  321. if (ret) {
  322. printk(KERN_WARNING "or51211: No firmware uploaded "
  323. "(timeout or file not found?)\n");
  324. return ret;
  325. }
  326. ret = or51211_load_firmware(fe, fw);
  327. release_firmware(fw);
  328. if (ret) {
  329. printk(KERN_WARNING "or51211: Writing firmware to "
  330. "device failed!\n");
  331. return ret;
  332. }
  333. printk(KERN_INFO "or51211: Firmware upload complete.\n");
  334. /* Set operation mode in Receiver 1 register;
  335. * type 1:
  336. * data 0x50h Automatic sets receiver channel conditions
  337. * Automatic NTSC rejection filter
  338. * Enable MPEG serial data output
  339. * MPEG2tr
  340. * High tuner phase noise
  341. * normal +/-150kHz Carrier acquisition range
  342. */
  343. if (i2c_writebytes(state,state->config->demod_address,
  344. cmd_buf,3)) {
  345. printk(KERN_WARNING "or51211: Load DVR Error 5\n");
  346. return -1;
  347. }
  348. /* Read back ucode version to besure we loaded correctly */
  349. /* and are really up and running */
  350. rec_buf[0] = 0x04;
  351. rec_buf[1] = 0x00;
  352. rec_buf[2] = 0x03;
  353. rec_buf[3] = 0x00;
  354. msleep(30);
  355. if (i2c_writebytes(state,state->config->demod_address,
  356. rec_buf,3)) {
  357. printk(KERN_WARNING "or51211: Load DVR Error A\n");
  358. return -1;
  359. }
  360. msleep(3);
  361. if (i2c_readbytes(state,state->config->demod_address,
  362. &rec_buf[10],2)) {
  363. printk(KERN_WARNING "or51211: Load DVR Error B\n");
  364. return -1;
  365. }
  366. rec_buf[0] = 0x04;
  367. rec_buf[1] = 0x00;
  368. rec_buf[2] = 0x01;
  369. rec_buf[3] = 0x00;
  370. msleep(20);
  371. if (i2c_writebytes(state,state->config->demod_address,
  372. rec_buf,3)) {
  373. printk(KERN_WARNING "or51211: Load DVR Error C\n");
  374. return -1;
  375. }
  376. msleep(3);
  377. if (i2c_readbytes(state,state->config->demod_address,
  378. &rec_buf[12],2)) {
  379. printk(KERN_WARNING "or51211: Load DVR Error D\n");
  380. return -1;
  381. }
  382. for (i = 0; i < 8; i++)
  383. rec_buf[i]=0xed;
  384. for (i = 0; i < 5; i++) {
  385. msleep(30);
  386. get_ver_buf[4] = i+1;
  387. if (i2c_writebytes(state,state->config->demod_address,
  388. get_ver_buf,5)) {
  389. printk(KERN_WARNING "or51211:Load DVR Error 6"
  390. " - %d\n",i);
  391. return -1;
  392. }
  393. msleep(3);
  394. if (i2c_readbytes(state,state->config->demod_address,
  395. &rec_buf[i*2],2)) {
  396. printk(KERN_WARNING "or51211:Load DVR Error 7"
  397. " - %d\n",i);
  398. return -1;
  399. }
  400. /* If we didn't receive the right index, try again */
  401. if ((int)rec_buf[i*2+1]!=i+1){
  402. i--;
  403. }
  404. }
  405. dprintk("read_fwbits %x %x %x %x %x %x %x %x %x %x\n",
  406. rec_buf[0], rec_buf[1], rec_buf[2], rec_buf[3],
  407. rec_buf[4], rec_buf[5], rec_buf[6], rec_buf[7],
  408. rec_buf[8], rec_buf[9]);
  409. printk(KERN_INFO "or51211: ver TU%02x%02x%02x VSB mode %02x"
  410. " Status %02x\n",
  411. rec_buf[2], rec_buf[4],rec_buf[6],
  412. rec_buf[12],rec_buf[10]);
  413. rec_buf[0] = 0x04;
  414. rec_buf[1] = 0x00;
  415. rec_buf[2] = 0x03;
  416. rec_buf[3] = 0x00;
  417. msleep(20);
  418. if (i2c_writebytes(state,state->config->demod_address,
  419. rec_buf,3)) {
  420. printk(KERN_WARNING "or51211: Load DVR Error 8\n");
  421. return -1;
  422. }
  423. msleep(20);
  424. if (i2c_readbytes(state,state->config->demod_address,
  425. &rec_buf[8],2)) {
  426. printk(KERN_WARNING "or51211: Load DVR Error 9\n");
  427. return -1;
  428. }
  429. state->initialized = 1;
  430. }
  431. return 0;
  432. }
  433. static int or51211_get_tune_settings(struct dvb_frontend* fe,
  434. struct dvb_frontend_tune_settings* fesettings)
  435. {
  436. fesettings->min_delay_ms = 500;
  437. fesettings->step_size = 0;
  438. fesettings->max_drift = 0;
  439. return 0;
  440. }
  441. static void or51211_release(struct dvb_frontend* fe)
  442. {
  443. struct or51211_state* state = fe->demodulator_priv;
  444. state->config->sleep(fe);
  445. kfree(state);
  446. }
  447. static struct dvb_frontend_ops or51211_ops;
  448. struct dvb_frontend* or51211_attach(const struct or51211_config* config,
  449. struct i2c_adapter* i2c)
  450. {
  451. struct or51211_state* state = NULL;
  452. /* Allocate memory for the internal state */
  453. state = kmalloc(sizeof(struct or51211_state), GFP_KERNEL);
  454. if (state == NULL)
  455. goto error;
  456. /* Setup the state */
  457. state->config = config;
  458. state->i2c = i2c;
  459. state->initialized = 0;
  460. state->current_frequency = 0;
  461. /* Create dvb_frontend */
  462. memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops));
  463. state->frontend.demodulator_priv = state;
  464. return &state->frontend;
  465. error:
  466. kfree(state);
  467. return NULL;
  468. }
  469. static struct dvb_frontend_ops or51211_ops = {
  470. .info = {
  471. .name = "Oren OR51211 VSB Frontend",
  472. .type = FE_ATSC,
  473. .frequency_min = 44000000,
  474. .frequency_max = 958000000,
  475. .frequency_stepsize = 166666,
  476. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  477. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  478. FE_CAN_8VSB
  479. },
  480. .release = or51211_release,
  481. .init = or51211_init,
  482. .sleep = or51211_sleep,
  483. .set_frontend = or51211_set_parameters,
  484. .get_tune_settings = or51211_get_tune_settings,
  485. .read_status = or51211_read_status,
  486. .read_ber = or51211_read_ber,
  487. .read_signal_strength = or51211_read_signal_strength,
  488. .read_snr = or51211_read_snr,
  489. .read_ucblocks = or51211_read_ucblocks,
  490. };
  491. module_param(debug, int, 0644);
  492. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  493. MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
  494. MODULE_AUTHOR("Kirk Lapray");
  495. MODULE_LICENSE("GPL");
  496. EXPORT_SYMBOL(or51211_attach);