s5h1409.c 18 KB

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