or51211.c 14 KB

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