s5h1409.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /*
  2. Samsung S5H1409 VSB/QAM demodulator driver
  3. Copyright (C) 2006 Steven Toth <stoth@hauppauge.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include "dvb_frontend.h"
  23. #include "dvb-pll.h"
  24. #include "s5h1409.h"
  25. struct s5h1409_state {
  26. struct i2c_adapter* i2c;
  27. /* configuration settings */
  28. const struct s5h1409_config* config;
  29. struct dvb_frontend frontend;
  30. /* previous uncorrected block counter */
  31. fe_modulation_t current_modulation;
  32. u32 current_frequency;
  33. int if_freq;
  34. u32 is_qam_locked;
  35. u32 qam_state;
  36. };
  37. static int debug = 0;
  38. #define dprintk if (debug) printk
  39. /* Register values to initialise the demod, this will set VSB by default */
  40. static struct init_tab {
  41. u8 reg;
  42. u16 data;
  43. } init_tab[] = {
  44. { 0x00, 0x0071, },
  45. { 0x01, 0x3213, },
  46. { 0x09, 0x0025, },
  47. { 0x1c, 0x001d, },
  48. { 0x1f, 0x002d, },
  49. { 0x20, 0x001d, },
  50. { 0x22, 0x0022, },
  51. { 0x23, 0x0020, },
  52. { 0x29, 0x110f, },
  53. { 0x2a, 0x10b4, },
  54. { 0x2b, 0x10ae, },
  55. { 0x2c, 0x0031, },
  56. { 0x31, 0x010d, },
  57. { 0x32, 0x0100, },
  58. { 0x44, 0x0510, },
  59. { 0x54, 0x0104, },
  60. { 0x58, 0x2222, },
  61. { 0x59, 0x1162, },
  62. { 0x5a, 0x3211, },
  63. { 0x5d, 0x0370, },
  64. { 0x5e, 0x0296, },
  65. { 0x61, 0x0010, },
  66. { 0x63, 0x4a00, },
  67. { 0x65, 0x0800, },
  68. { 0x71, 0x0003, },
  69. { 0x72, 0x0470, },
  70. { 0x81, 0x0002, },
  71. { 0x82, 0x0600, },
  72. { 0x86, 0x0002, },
  73. { 0x8a, 0x2c38, },
  74. { 0x8b, 0x2a37, },
  75. { 0x92, 0x302f, },
  76. { 0x93, 0x3332, },
  77. { 0x96, 0x000c, },
  78. { 0x99, 0x0101, },
  79. { 0x9c, 0x2e37, },
  80. { 0x9d, 0x2c37, },
  81. { 0x9e, 0x2c37, },
  82. { 0xab, 0x0100, },
  83. { 0xac, 0x1003, },
  84. { 0xad, 0x103f, },
  85. { 0xe2, 0x0100, },
  86. { 0xe3, 0x0000, },
  87. { 0x28, 0x1010, },
  88. { 0xb1, 0x000e, },
  89. };
  90. /* VSB SNR lookup table */
  91. static struct vsb_snr_tab {
  92. u16 val;
  93. u16 data;
  94. } vsb_snr_tab[] = {
  95. { 924, 300, },
  96. { 923, 300, },
  97. { 918, 295, },
  98. { 915, 290, },
  99. { 911, 285, },
  100. { 906, 280, },
  101. { 901, 275, },
  102. { 896, 270, },
  103. { 891, 265, },
  104. { 885, 260, },
  105. { 879, 255, },
  106. { 873, 250, },
  107. { 864, 245, },
  108. { 858, 240, },
  109. { 850, 235, },
  110. { 841, 230, },
  111. { 832, 225, },
  112. { 823, 220, },
  113. { 812, 215, },
  114. { 802, 210, },
  115. { 788, 205, },
  116. { 778, 200, },
  117. { 767, 195, },
  118. { 753, 190, },
  119. { 740, 185, },
  120. { 725, 180, },
  121. { 707, 175, },
  122. { 689, 170, },
  123. { 671, 165, },
  124. { 656, 160, },
  125. { 637, 155, },
  126. { 616, 150, },
  127. { 542, 145, },
  128. { 519, 140, },
  129. { 507, 135, },
  130. { 497, 130, },
  131. { 492, 125, },
  132. { 474, 120, },
  133. { 300, 111, },
  134. { 0, 0, },
  135. };
  136. /* QAM64 SNR lookup table */
  137. static struct qam64_snr_tab {
  138. u16 val;
  139. u16 data;
  140. } qam64_snr_tab[] = {
  141. { 1, 0, },
  142. { 12, 300, },
  143. { 15, 290, },
  144. { 18, 280, },
  145. { 22, 270, },
  146. { 23, 268, },
  147. { 24, 266, },
  148. { 25, 264, },
  149. { 27, 262, },
  150. { 28, 260, },
  151. { 29, 258, },
  152. { 30, 256, },
  153. { 32, 254, },
  154. { 33, 252, },
  155. { 34, 250, },
  156. { 35, 249, },
  157. { 36, 248, },
  158. { 37, 247, },
  159. { 38, 246, },
  160. { 39, 245, },
  161. { 40, 244, },
  162. { 41, 243, },
  163. { 42, 241, },
  164. { 43, 240, },
  165. { 44, 239, },
  166. { 45, 238, },
  167. { 46, 237, },
  168. { 47, 236, },
  169. { 48, 235, },
  170. { 49, 234, },
  171. { 50, 233, },
  172. { 51, 232, },
  173. { 52, 231, },
  174. { 53, 230, },
  175. { 55, 229, },
  176. { 56, 228, },
  177. { 57, 227, },
  178. { 58, 226, },
  179. { 59, 225, },
  180. { 60, 224, },
  181. { 62, 223, },
  182. { 63, 222, },
  183. { 65, 221, },
  184. { 66, 220, },
  185. { 68, 219, },
  186. { 69, 218, },
  187. { 70, 217, },
  188. { 72, 216, },
  189. { 73, 215, },
  190. { 75, 214, },
  191. { 76, 213, },
  192. { 78, 212, },
  193. { 80, 211, },
  194. { 81, 210, },
  195. { 83, 209, },
  196. { 84, 208, },
  197. { 85, 207, },
  198. { 87, 206, },
  199. { 89, 205, },
  200. { 91, 204, },
  201. { 93, 203, },
  202. { 95, 202, },
  203. { 96, 201, },
  204. { 104, 200, },
  205. { 255, 0, },
  206. };
  207. /* QAM256 SNR lookup table */
  208. static struct qam256_snr_tab {
  209. u16 val;
  210. u16 data;
  211. } qam256_snr_tab[] = {
  212. { 1, 0, },
  213. { 12, 400, },
  214. { 13, 390, },
  215. { 15, 380, },
  216. { 17, 360, },
  217. { 19, 350, },
  218. { 22, 348, },
  219. { 23, 346, },
  220. { 24, 344, },
  221. { 25, 342, },
  222. { 26, 340, },
  223. { 27, 336, },
  224. { 28, 334, },
  225. { 29, 332, },
  226. { 30, 330, },
  227. { 31, 328, },
  228. { 32, 326, },
  229. { 33, 325, },
  230. { 34, 322, },
  231. { 35, 320, },
  232. { 37, 318, },
  233. { 39, 316, },
  234. { 40, 314, },
  235. { 41, 312, },
  236. { 42, 310, },
  237. { 43, 308, },
  238. { 46, 306, },
  239. { 47, 304, },
  240. { 49, 302, },
  241. { 51, 300, },
  242. { 53, 298, },
  243. { 54, 297, },
  244. { 55, 296, },
  245. { 56, 295, },
  246. { 57, 294, },
  247. { 59, 293, },
  248. { 60, 292, },
  249. { 61, 291, },
  250. { 63, 290, },
  251. { 64, 289, },
  252. { 65, 288, },
  253. { 66, 287, },
  254. { 68, 286, },
  255. { 69, 285, },
  256. { 71, 284, },
  257. { 72, 283, },
  258. { 74, 282, },
  259. { 75, 281, },
  260. { 76, 280, },
  261. { 77, 279, },
  262. { 78, 278, },
  263. { 81, 277, },
  264. { 83, 276, },
  265. { 84, 275, },
  266. { 86, 274, },
  267. { 87, 273, },
  268. { 89, 272, },
  269. { 90, 271, },
  270. { 92, 270, },
  271. { 93, 269, },
  272. { 95, 268, },
  273. { 96, 267, },
  274. { 98, 266, },
  275. { 100, 265, },
  276. { 102, 264, },
  277. { 104, 263, },
  278. { 105, 262, },
  279. { 106, 261, },
  280. { 110, 260, },
  281. { 255, 0, },
  282. };
  283. /* 8 bit registers, 16 bit values */
  284. static int s5h1409_writereg(struct s5h1409_state* state, u8 reg, u16 data)
  285. {
  286. int ret;
  287. u8 buf [] = { reg, data >> 8, data & 0xff };
  288. struct i2c_msg msg = { .addr = state->config->demod_address,
  289. .flags = 0, .buf = buf, .len = 3 };
  290. ret = i2c_transfer(state->i2c, &msg, 1);
  291. if (ret != 1)
  292. printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
  293. "ret == %i)\n", __FUNCTION__, reg, data, ret);
  294. return (ret != 1) ? -1 : 0;
  295. }
  296. static u16 s5h1409_readreg(struct s5h1409_state* state, u8 reg)
  297. {
  298. int ret;
  299. u8 b0 [] = { reg };
  300. u8 b1 [] = { 0, 0 };
  301. struct i2c_msg msg [] = {
  302. { .addr = state->config->demod_address, .flags = 0,
  303. .buf = b0, .len = 1 },
  304. { .addr = state->config->demod_address, .flags = I2C_M_RD,
  305. .buf = b1, .len = 2 } };
  306. ret = i2c_transfer(state->i2c, msg, 2);
  307. if (ret != 2)
  308. printk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
  309. return (b1[0] << 8) | b1[1];
  310. }
  311. static int s5h1409_softreset(struct dvb_frontend* fe)
  312. {
  313. struct s5h1409_state* state = fe->demodulator_priv;
  314. dprintk("%s()\n", __FUNCTION__);
  315. s5h1409_writereg(state, 0xf5, 0);
  316. s5h1409_writereg(state, 0xf5, 1);
  317. state->is_qam_locked = 0;
  318. state->qam_state = 0;
  319. return 0;
  320. }
  321. #define S5H1409_VSB_IF_FREQ 5380
  322. #define S5H1409_QAM_IF_FREQ state->config->qam_if
  323. static int s5h1409_set_if_freq(struct dvb_frontend* fe, int KHz)
  324. {
  325. struct s5h1409_state* state = fe->demodulator_priv;
  326. dprintk("%s(%d KHz)\n", __FUNCTION__, KHz);
  327. switch (KHz) {
  328. case 4000:
  329. s5h1409_writereg(state, 0x87, 0x014b);
  330. s5h1409_writereg(state, 0x88, 0x0cb5);
  331. s5h1409_writereg(state, 0x89, 0x03e2);
  332. break;
  333. case 5380:
  334. case 44000:
  335. default:
  336. s5h1409_writereg(state, 0x87, 0x01be);
  337. s5h1409_writereg(state, 0x88, 0x0436);
  338. s5h1409_writereg(state, 0x89, 0x054d);
  339. break;
  340. }
  341. state->if_freq = KHz;
  342. return 0;
  343. }
  344. static int s5h1409_set_spectralinversion(struct dvb_frontend* fe, int inverted)
  345. {
  346. struct s5h1409_state* state = fe->demodulator_priv;
  347. dprintk("%s(%d)\n", __FUNCTION__, inverted);
  348. if(inverted == 1)
  349. return s5h1409_writereg(state, 0x1b, 0x1101); /* Inverted */
  350. else
  351. return s5h1409_writereg(state, 0x1b, 0x0110); /* Normal */
  352. }
  353. static int s5h1409_enable_modulation(struct dvb_frontend* fe,
  354. fe_modulation_t m)
  355. {
  356. struct s5h1409_state* state = fe->demodulator_priv;
  357. dprintk("%s(0x%08x)\n", __FUNCTION__, m);
  358. switch(m) {
  359. case VSB_8:
  360. dprintk("%s() VSB_8\n", __FUNCTION__);
  361. if (state->if_freq != S5H1409_VSB_IF_FREQ)
  362. s5h1409_set_if_freq(fe, S5H1409_VSB_IF_FREQ);
  363. s5h1409_writereg(state, 0xf4, 0);
  364. break;
  365. case QAM_64:
  366. case QAM_256:
  367. dprintk("%s() QAM_AUTO (64/256)\n", __FUNCTION__);
  368. if (state->if_freq != S5H1409_QAM_IF_FREQ)
  369. s5h1409_set_if_freq(fe, S5H1409_QAM_IF_FREQ);
  370. s5h1409_writereg(state, 0xf4, 1);
  371. s5h1409_writereg(state, 0x85, 0x110);
  372. break;
  373. default:
  374. dprintk("%s() Invalid modulation\n", __FUNCTION__);
  375. return -EINVAL;
  376. }
  377. state->current_modulation = m;
  378. s5h1409_softreset(fe);
  379. return 0;
  380. }
  381. static int s5h1409_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  382. {
  383. struct s5h1409_state* state = fe->demodulator_priv;
  384. dprintk("%s(%d)\n", __FUNCTION__, enable);
  385. if (enable)
  386. return s5h1409_writereg(state, 0xf3, 1);
  387. else
  388. return s5h1409_writereg(state, 0xf3, 0);
  389. }
  390. static int s5h1409_set_gpio(struct dvb_frontend* fe, int enable)
  391. {
  392. struct s5h1409_state* state = fe->demodulator_priv;
  393. dprintk("%s(%d)\n", __FUNCTION__, enable);
  394. if (enable)
  395. return s5h1409_writereg(state, 0xe3, 0x1100);
  396. else
  397. return s5h1409_writereg(state, 0xe3, 0x1000);
  398. }
  399. static int s5h1409_sleep(struct dvb_frontend* fe, int enable)
  400. {
  401. struct s5h1409_state* state = fe->demodulator_priv;
  402. dprintk("%s(%d)\n", __FUNCTION__, enable);
  403. return s5h1409_writereg(state, 0xf2, enable);
  404. }
  405. static int s5h1409_register_reset(struct dvb_frontend* fe)
  406. {
  407. struct s5h1409_state* state = fe->demodulator_priv;
  408. dprintk("%s()\n", __FUNCTION__);
  409. return s5h1409_writereg(state, 0xfa, 0);
  410. }
  411. static void s5h1409_set_qam_amhum_mode(struct dvb_frontend *fe)
  412. {
  413. struct s5h1409_state *state = fe->demodulator_priv;
  414. u16 reg;
  415. if (state->is_qam_locked)
  416. return;
  417. /* QAM EQ lock check */
  418. reg = s5h1409_readreg(state, 0xf0);
  419. if ((reg >> 13) & 0x1) {
  420. state->is_qam_locked = 1;
  421. reg &= 0xff;
  422. s5h1409_writereg(state, 0x96, 0x00c);
  423. if ((reg < 0x38) || (reg > 0x68) ) {
  424. s5h1409_writereg(state, 0x93, 0x3332);
  425. s5h1409_writereg(state, 0x9e, 0x2c37);
  426. } else {
  427. s5h1409_writereg(state, 0x93, 0x3130);
  428. s5h1409_writereg(state, 0x9e, 0x2836);
  429. }
  430. } else {
  431. s5h1409_writereg(state, 0x96, 0x0008);
  432. s5h1409_writereg(state, 0x93, 0x3332);
  433. s5h1409_writereg(state, 0x9e, 0x2c37);
  434. }
  435. }
  436. static void s5h1409_set_qam_interleave_mode(struct dvb_frontend *fe)
  437. {
  438. struct s5h1409_state *state = fe->demodulator_priv;
  439. u16 reg, reg1, reg2;
  440. reg = s5h1409_readreg(state, 0xf1);
  441. /* Master lock */
  442. if ((reg >> 15) & 0x1) {
  443. if (state->qam_state != 2) {
  444. state->qam_state = 2;
  445. reg1 = s5h1409_readreg(state, 0xb2);
  446. reg2 = s5h1409_readreg(state, 0xad);
  447. s5h1409_writereg(state, 0x96, 0x20);
  448. s5h1409_writereg(state, 0xad,
  449. ( ((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)) );
  450. s5h1409_writereg(state, 0xab, 0x1100);
  451. }
  452. } else {
  453. if (state->qam_state != 1) {
  454. state->qam_state = 1;
  455. s5h1409_writereg(state, 0x96, 0x08);
  456. s5h1409_writereg(state, 0xab, 0x1101);
  457. }
  458. }
  459. }
  460. /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
  461. static int s5h1409_set_frontend (struct dvb_frontend* fe,
  462. struct dvb_frontend_parameters *p)
  463. {
  464. struct s5h1409_state* state = fe->demodulator_priv;
  465. dprintk("%s(frequency=%d)\n", __FUNCTION__, p->frequency);
  466. s5h1409_softreset(fe);
  467. state->current_frequency = p->frequency;
  468. s5h1409_enable_modulation(fe, p->u.vsb.modulation);
  469. /* Allow the demod to settle */
  470. msleep(100);
  471. if (fe->ops.tuner_ops.set_params) {
  472. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 1);
  473. fe->ops.tuner_ops.set_params(fe, p);
  474. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  475. }
  476. /* Optimize the demod for QAM */
  477. if (p->u.vsb.modulation != VSB_8) {
  478. s5h1409_set_qam_amhum_mode(fe);
  479. s5h1409_set_qam_interleave_mode(fe);
  480. }
  481. return 0;
  482. }
  483. /* Reset the demod hardware and reset all of the configuration registers
  484. to a default state. */
  485. static int s5h1409_init (struct dvb_frontend* fe)
  486. {
  487. int i;
  488. struct s5h1409_state* state = fe->demodulator_priv;
  489. dprintk("%s()\n", __FUNCTION__);
  490. s5h1409_sleep(fe, 0);
  491. s5h1409_register_reset(fe);
  492. for (i=0; i < ARRAY_SIZE(init_tab); i++)
  493. s5h1409_writereg(state, init_tab[i].reg, init_tab[i].data);
  494. /* The datasheet says that after initialisation, VSB is default */
  495. state->current_modulation = VSB_8;
  496. if (state->config->output_mode == S5H1409_SERIAL_OUTPUT)
  497. s5h1409_writereg(state, 0xab, 0x100); /* Serial */
  498. else
  499. s5h1409_writereg(state, 0xab, 0x0); /* Parallel */
  500. s5h1409_set_spectralinversion(fe, state->config->inversion);
  501. s5h1409_set_if_freq(fe, state->if_freq);
  502. s5h1409_set_gpio(fe, state->config->gpio);
  503. s5h1409_softreset(fe);
  504. /* Note: Leaving the I2C gate closed. */
  505. s5h1409_i2c_gate_ctrl(fe, 0);
  506. return 0;
  507. }
  508. static int s5h1409_read_status(struct dvb_frontend* fe, fe_status_t* status)
  509. {
  510. struct s5h1409_state* state = fe->demodulator_priv;
  511. u16 reg;
  512. u32 tuner_status = 0;
  513. *status = 0;
  514. /* Get the demodulator status */
  515. reg = s5h1409_readreg(state, 0xf1);
  516. if(reg & 0x1000)
  517. *status |= FE_HAS_VITERBI;
  518. if(reg & 0x8000)
  519. *status |= FE_HAS_LOCK | FE_HAS_SYNC;
  520. switch(state->config->status_mode) {
  521. case S5H1409_DEMODLOCKING:
  522. if (*status & FE_HAS_VITERBI)
  523. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  524. break;
  525. case S5H1409_TUNERLOCKING:
  526. /* Get the tuner status */
  527. if (fe->ops.tuner_ops.get_status) {
  528. if (fe->ops.i2c_gate_ctrl)
  529. fe->ops.i2c_gate_ctrl(fe, 1);
  530. fe->ops.tuner_ops.get_status(fe, &tuner_status);
  531. if (fe->ops.i2c_gate_ctrl)
  532. fe->ops.i2c_gate_ctrl(fe, 0);
  533. }
  534. if (tuner_status)
  535. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  536. break;
  537. }
  538. dprintk("%s() status 0x%08x\n", __FUNCTION__, *status);
  539. return 0;
  540. }
  541. static int s5h1409_qam256_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
  542. {
  543. int i, ret = -EINVAL;
  544. dprintk("%s()\n", __FUNCTION__);
  545. for (i=0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
  546. if (v < qam256_snr_tab[i].val) {
  547. *snr = qam256_snr_tab[i].data;
  548. ret = 0;
  549. break;
  550. }
  551. }
  552. return ret;
  553. }
  554. static int s5h1409_qam64_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
  555. {
  556. int i, ret = -EINVAL;
  557. dprintk("%s()\n", __FUNCTION__);
  558. for (i=0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
  559. if (v < qam64_snr_tab[i].val) {
  560. *snr = qam64_snr_tab[i].data;
  561. ret = 0;
  562. break;
  563. }
  564. }
  565. return ret;
  566. }
  567. static int s5h1409_vsb_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
  568. {
  569. int i, ret = -EINVAL;
  570. dprintk("%s()\n", __FUNCTION__);
  571. for (i=0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
  572. if (v > vsb_snr_tab[i].val) {
  573. *snr = vsb_snr_tab[i].data;
  574. ret = 0;
  575. break;
  576. }
  577. }
  578. dprintk("%s() snr=%d\n", __FUNCTION__, *snr);
  579. return ret;
  580. }
  581. static int s5h1409_read_snr(struct dvb_frontend* fe, u16* snr)
  582. {
  583. struct s5h1409_state* state = fe->demodulator_priv;
  584. u16 reg;
  585. dprintk("%s()\n", __FUNCTION__);
  586. switch(state->current_modulation) {
  587. case QAM_64:
  588. reg = s5h1409_readreg(state, 0xf0) & 0xff;
  589. return s5h1409_qam64_lookup_snr(fe, snr, reg);
  590. case QAM_256:
  591. reg = s5h1409_readreg(state, 0xf0) & 0xff;
  592. return s5h1409_qam256_lookup_snr(fe, snr, reg);
  593. case VSB_8:
  594. reg = s5h1409_readreg(state, 0xf1) & 0x3ff;
  595. return s5h1409_vsb_lookup_snr(fe, snr, reg);
  596. default:
  597. break;
  598. }
  599. return -EINVAL;
  600. }
  601. static int s5h1409_read_signal_strength(struct dvb_frontend* fe,
  602. u16* signal_strength)
  603. {
  604. return s5h1409_read_snr(fe, signal_strength);
  605. }
  606. static int s5h1409_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  607. {
  608. struct s5h1409_state* state = fe->demodulator_priv;
  609. *ucblocks = s5h1409_readreg(state, 0xb5);
  610. return 0;
  611. }
  612. static int s5h1409_read_ber(struct dvb_frontend* fe, u32* ber)
  613. {
  614. return s5h1409_read_ucblocks(fe, ber);
  615. }
  616. static int s5h1409_get_frontend(struct dvb_frontend* fe,
  617. struct dvb_frontend_parameters *p)
  618. {
  619. struct s5h1409_state* state = fe->demodulator_priv;
  620. p->frequency = state->current_frequency;
  621. p->u.vsb.modulation = state->current_modulation;
  622. return 0;
  623. }
  624. static int s5h1409_get_tune_settings(struct dvb_frontend* fe,
  625. struct dvb_frontend_tune_settings *tune)
  626. {
  627. tune->min_delay_ms = 1000;
  628. return 0;
  629. }
  630. static void s5h1409_release(struct dvb_frontend* fe)
  631. {
  632. struct s5h1409_state* state = fe->demodulator_priv;
  633. kfree(state);
  634. }
  635. static struct dvb_frontend_ops s5h1409_ops;
  636. struct dvb_frontend* s5h1409_attach(const struct s5h1409_config* config,
  637. struct i2c_adapter* i2c)
  638. {
  639. struct s5h1409_state* state = NULL;
  640. /* allocate memory for the internal state */
  641. state = kmalloc(sizeof(struct s5h1409_state), GFP_KERNEL);
  642. if (state == NULL)
  643. goto error;
  644. /* setup the state */
  645. state->config = config;
  646. state->i2c = i2c;
  647. state->current_modulation = 0;
  648. state->if_freq = S5H1409_VSB_IF_FREQ;
  649. /* check if the demod exists */
  650. if (s5h1409_readreg(state, 0x04) != 0x0066)
  651. goto error;
  652. /* create dvb_frontend */
  653. memcpy(&state->frontend.ops, &s5h1409_ops,
  654. sizeof(struct dvb_frontend_ops));
  655. state->frontend.demodulator_priv = state;
  656. /* Note: Leaving the I2C gate open here. */
  657. s5h1409_writereg(state, 0xf3, 1);
  658. return &state->frontend;
  659. error:
  660. kfree(state);
  661. return NULL;
  662. }
  663. static struct dvb_frontend_ops s5h1409_ops = {
  664. .info = {
  665. .name = "Samsung S5H1409 QAM/8VSB Frontend",
  666. .type = FE_ATSC,
  667. .frequency_min = 54000000,
  668. .frequency_max = 858000000,
  669. .frequency_stepsize = 62500,
  670. .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
  671. },
  672. .init = s5h1409_init,
  673. .i2c_gate_ctrl = s5h1409_i2c_gate_ctrl,
  674. .set_frontend = s5h1409_set_frontend,
  675. .get_frontend = s5h1409_get_frontend,
  676. .get_tune_settings = s5h1409_get_tune_settings,
  677. .read_status = s5h1409_read_status,
  678. .read_ber = s5h1409_read_ber,
  679. .read_signal_strength = s5h1409_read_signal_strength,
  680. .read_snr = s5h1409_read_snr,
  681. .read_ucblocks = s5h1409_read_ucblocks,
  682. .release = s5h1409_release,
  683. };
  684. module_param(debug, int, 0644);
  685. MODULE_PARM_DESC(debug, "Enable verbose debug messages");
  686. MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
  687. MODULE_AUTHOR("Steven Toth");
  688. MODULE_LICENSE("GPL");
  689. EXPORT_SYMBOL(s5h1409_attach);
  690. /*
  691. * Local variables:
  692. * c-basic-offset: 8
  693. */