or51211.c 13 KB

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