or51211.c 15 KB

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