s5h1409.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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;
  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, 0x1000, },
  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", __func__, 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", __func__, 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", __func__);
  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", __func__, 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", __func__, 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", __func__, m);
  358. switch(m) {
  359. case VSB_8:
  360. dprintk("%s() VSB_8\n", __func__);
  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", __func__);
  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", __func__);
  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", __func__, 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", __func__, enable);
  394. if (enable)
  395. return s5h1409_writereg(state, 0xe3,
  396. s5h1409_readreg(state, 0xe3) | 0x1100);
  397. else
  398. return s5h1409_writereg(state, 0xe3,
  399. s5h1409_readreg(state, 0xe3) & 0xfeff);
  400. }
  401. static int s5h1409_sleep(struct dvb_frontend* fe, int enable)
  402. {
  403. struct s5h1409_state* state = fe->demodulator_priv;
  404. dprintk("%s(%d)\n", __func__, enable);
  405. return s5h1409_writereg(state, 0xf2, enable);
  406. }
  407. static int s5h1409_register_reset(struct dvb_frontend* fe)
  408. {
  409. struct s5h1409_state* state = fe->demodulator_priv;
  410. dprintk("%s()\n", __func__);
  411. return s5h1409_writereg(state, 0xfa, 0);
  412. }
  413. static void s5h1409_set_qam_amhum_mode(struct dvb_frontend *fe)
  414. {
  415. struct s5h1409_state *state = fe->demodulator_priv;
  416. u16 reg;
  417. if (state->is_qam_locked)
  418. return;
  419. /* QAM EQ lock check */
  420. reg = s5h1409_readreg(state, 0xf0);
  421. if ((reg >> 13) & 0x1) {
  422. state->is_qam_locked = 1;
  423. reg &= 0xff;
  424. s5h1409_writereg(state, 0x96, 0x00c);
  425. if ((reg < 0x38) || (reg > 0x68) ) {
  426. s5h1409_writereg(state, 0x93, 0x3332);
  427. s5h1409_writereg(state, 0x9e, 0x2c37);
  428. } else {
  429. s5h1409_writereg(state, 0x93, 0x3130);
  430. s5h1409_writereg(state, 0x9e, 0x2836);
  431. }
  432. } else {
  433. s5h1409_writereg(state, 0x96, 0x0008);
  434. s5h1409_writereg(state, 0x93, 0x3332);
  435. s5h1409_writereg(state, 0x9e, 0x2c37);
  436. }
  437. }
  438. static void s5h1409_set_qam_interleave_mode(struct dvb_frontend *fe)
  439. {
  440. struct s5h1409_state *state = fe->demodulator_priv;
  441. u16 reg, reg1, reg2;
  442. reg = s5h1409_readreg(state, 0xf1);
  443. /* Master lock */
  444. if ((reg >> 15) & 0x1) {
  445. if (state->qam_state != 2) {
  446. state->qam_state = 2;
  447. reg1 = s5h1409_readreg(state, 0xb2);
  448. reg2 = s5h1409_readreg(state, 0xad);
  449. s5h1409_writereg(state, 0x96, 0x20);
  450. s5h1409_writereg(state, 0xad,
  451. ( ((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)) );
  452. s5h1409_writereg(state, 0xab,
  453. s5h1409_readreg(state, 0xab) & 0xeffe);
  454. }
  455. } else {
  456. if (state->qam_state != 1) {
  457. state->qam_state = 1;
  458. s5h1409_writereg(state, 0x96, 0x08);
  459. s5h1409_writereg(state, 0xab,
  460. s5h1409_readreg(state, 0xab) | 0x1001);
  461. }
  462. }
  463. }
  464. /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
  465. static int s5h1409_set_frontend (struct dvb_frontend* fe,
  466. struct dvb_frontend_parameters *p)
  467. {
  468. struct s5h1409_state* state = fe->demodulator_priv;
  469. dprintk("%s(frequency=%d)\n", __func__, p->frequency);
  470. s5h1409_softreset(fe);
  471. state->current_frequency = p->frequency;
  472. s5h1409_enable_modulation(fe, p->u.vsb.modulation);
  473. /* Allow the demod to settle */
  474. msleep(100);
  475. if (fe->ops.tuner_ops.set_params) {
  476. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 1);
  477. fe->ops.tuner_ops.set_params(fe, p);
  478. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  479. }
  480. /* Optimize the demod for QAM */
  481. if (p->u.vsb.modulation != VSB_8) {
  482. s5h1409_set_qam_amhum_mode(fe);
  483. s5h1409_set_qam_interleave_mode(fe);
  484. }
  485. return 0;
  486. }
  487. static int s5h1409_set_mpeg_timing(struct dvb_frontend *fe, int mode)
  488. {
  489. struct s5h1409_state *state = fe->demodulator_priv;
  490. u16 val;
  491. dprintk("%s(%d)\n", __func__, mode);
  492. val = s5h1409_readreg(state, 0xac) & 0xcfff;
  493. switch (mode) {
  494. case S5H1409_MPEGTIMING_CONTINOUS_INVERTING_CLOCK:
  495. val |= 0x0000;
  496. break;
  497. case S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK:
  498. dprintk("%s(%d) Mode1 or Defaulting\n", __func__, mode);
  499. val |= 0x1000;
  500. break;
  501. case S5H1409_MPEGTIMING_NONCONTINOUS_INVERTING_CLOCK:
  502. val |= 0x2000;
  503. break;
  504. case S5H1409_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK:
  505. val |= 0x3000;
  506. break;
  507. default:
  508. return -EINVAL;
  509. }
  510. /* Configure MPEG Signal Timing charactistics */
  511. return s5h1409_writereg(state, 0xac, val);
  512. }
  513. /* Reset the demod hardware and reset all of the configuration registers
  514. to a default state. */
  515. static int s5h1409_init (struct dvb_frontend* fe)
  516. {
  517. int i;
  518. struct s5h1409_state* state = fe->demodulator_priv;
  519. dprintk("%s()\n", __func__);
  520. s5h1409_sleep(fe, 0);
  521. s5h1409_register_reset(fe);
  522. for (i=0; i < ARRAY_SIZE(init_tab); i++)
  523. s5h1409_writereg(state, init_tab[i].reg, init_tab[i].data);
  524. /* The datasheet says that after initialisation, VSB is default */
  525. state->current_modulation = VSB_8;
  526. if (state->config->output_mode == S5H1409_SERIAL_OUTPUT)
  527. s5h1409_writereg(state, 0xab,
  528. s5h1409_readreg(state, 0xab) | 0x100); /* Serial */
  529. else
  530. s5h1409_writereg(state, 0xab,
  531. s5h1409_readreg(state, 0xab) & 0xfeff); /* Parallel */
  532. s5h1409_set_spectralinversion(fe, state->config->inversion);
  533. s5h1409_set_if_freq(fe, state->if_freq);
  534. s5h1409_set_gpio(fe, state->config->gpio);
  535. s5h1409_set_mpeg_timing(fe, state->config->mpeg_timing);
  536. s5h1409_softreset(fe);
  537. /* Note: Leaving the I2C gate closed. */
  538. s5h1409_i2c_gate_ctrl(fe, 0);
  539. return 0;
  540. }
  541. static int s5h1409_read_status(struct dvb_frontend* fe, fe_status_t* status)
  542. {
  543. struct s5h1409_state* state = fe->demodulator_priv;
  544. u16 reg;
  545. u32 tuner_status = 0;
  546. *status = 0;
  547. /* Get the demodulator status */
  548. reg = s5h1409_readreg(state, 0xf1);
  549. if(reg & 0x1000)
  550. *status |= FE_HAS_VITERBI;
  551. if(reg & 0x8000)
  552. *status |= FE_HAS_LOCK | FE_HAS_SYNC;
  553. switch(state->config->status_mode) {
  554. case S5H1409_DEMODLOCKING:
  555. if (*status & FE_HAS_VITERBI)
  556. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  557. break;
  558. case S5H1409_TUNERLOCKING:
  559. /* Get the tuner status */
  560. if (fe->ops.tuner_ops.get_status) {
  561. if (fe->ops.i2c_gate_ctrl)
  562. fe->ops.i2c_gate_ctrl(fe, 1);
  563. fe->ops.tuner_ops.get_status(fe, &tuner_status);
  564. if (fe->ops.i2c_gate_ctrl)
  565. fe->ops.i2c_gate_ctrl(fe, 0);
  566. }
  567. if (tuner_status)
  568. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  569. break;
  570. }
  571. dprintk("%s() status 0x%08x\n", __func__, *status);
  572. return 0;
  573. }
  574. static int s5h1409_qam256_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
  575. {
  576. int i, ret = -EINVAL;
  577. dprintk("%s()\n", __func__);
  578. for (i=0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
  579. if (v < qam256_snr_tab[i].val) {
  580. *snr = qam256_snr_tab[i].data;
  581. ret = 0;
  582. break;
  583. }
  584. }
  585. return ret;
  586. }
  587. static int s5h1409_qam64_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
  588. {
  589. int i, ret = -EINVAL;
  590. dprintk("%s()\n", __func__);
  591. for (i=0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
  592. if (v < qam64_snr_tab[i].val) {
  593. *snr = qam64_snr_tab[i].data;
  594. ret = 0;
  595. break;
  596. }
  597. }
  598. return ret;
  599. }
  600. static int s5h1409_vsb_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
  601. {
  602. int i, ret = -EINVAL;
  603. dprintk("%s()\n", __func__);
  604. for (i=0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
  605. if (v > vsb_snr_tab[i].val) {
  606. *snr = vsb_snr_tab[i].data;
  607. ret = 0;
  608. break;
  609. }
  610. }
  611. dprintk("%s() snr=%d\n", __func__, *snr);
  612. return ret;
  613. }
  614. static int s5h1409_read_snr(struct dvb_frontend* fe, u16* snr)
  615. {
  616. struct s5h1409_state* state = fe->demodulator_priv;
  617. u16 reg;
  618. dprintk("%s()\n", __func__);
  619. switch(state->current_modulation) {
  620. case QAM_64:
  621. reg = s5h1409_readreg(state, 0xf0) & 0xff;
  622. return s5h1409_qam64_lookup_snr(fe, snr, reg);
  623. case QAM_256:
  624. reg = s5h1409_readreg(state, 0xf0) & 0xff;
  625. return s5h1409_qam256_lookup_snr(fe, snr, reg);
  626. case VSB_8:
  627. reg = s5h1409_readreg(state, 0xf1) & 0x3ff;
  628. return s5h1409_vsb_lookup_snr(fe, snr, reg);
  629. default:
  630. break;
  631. }
  632. return -EINVAL;
  633. }
  634. static int s5h1409_read_signal_strength(struct dvb_frontend* fe,
  635. u16* signal_strength)
  636. {
  637. return s5h1409_read_snr(fe, signal_strength);
  638. }
  639. static int s5h1409_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  640. {
  641. struct s5h1409_state* state = fe->demodulator_priv;
  642. *ucblocks = s5h1409_readreg(state, 0xb5);
  643. return 0;
  644. }
  645. static int s5h1409_read_ber(struct dvb_frontend* fe, u32* ber)
  646. {
  647. return s5h1409_read_ucblocks(fe, ber);
  648. }
  649. static int s5h1409_get_frontend(struct dvb_frontend* fe,
  650. struct dvb_frontend_parameters *p)
  651. {
  652. struct s5h1409_state* state = fe->demodulator_priv;
  653. p->frequency = state->current_frequency;
  654. p->u.vsb.modulation = state->current_modulation;
  655. return 0;
  656. }
  657. static int s5h1409_get_tune_settings(struct dvb_frontend* fe,
  658. struct dvb_frontend_tune_settings *tune)
  659. {
  660. tune->min_delay_ms = 1000;
  661. return 0;
  662. }
  663. static void s5h1409_release(struct dvb_frontend* fe)
  664. {
  665. struct s5h1409_state* state = fe->demodulator_priv;
  666. kfree(state);
  667. }
  668. static struct dvb_frontend_ops s5h1409_ops;
  669. struct dvb_frontend* s5h1409_attach(const struct s5h1409_config* config,
  670. struct i2c_adapter* i2c)
  671. {
  672. struct s5h1409_state* state = NULL;
  673. u16 reg;
  674. /* allocate memory for the internal state */
  675. state = kmalloc(sizeof(struct s5h1409_state), GFP_KERNEL);
  676. if (state == NULL)
  677. goto error;
  678. /* setup the state */
  679. state->config = config;
  680. state->i2c = i2c;
  681. state->current_modulation = 0;
  682. state->if_freq = S5H1409_VSB_IF_FREQ;
  683. /* check if the demod exists */
  684. reg = s5h1409_readreg(state, 0x04);
  685. if ((reg != 0x0066) && (reg != 0x007f))
  686. goto error;
  687. /* create dvb_frontend */
  688. memcpy(&state->frontend.ops, &s5h1409_ops,
  689. sizeof(struct dvb_frontend_ops));
  690. state->frontend.demodulator_priv = state;
  691. if (s5h1409_init(&state->frontend) != 0) {
  692. printk(KERN_ERR "%s: Failed to initialize correctly\n",
  693. __func__);
  694. goto error;
  695. }
  696. /* Note: Leaving the I2C gate open here. */
  697. s5h1409_i2c_gate_ctrl(&state->frontend, 1);
  698. return &state->frontend;
  699. error:
  700. kfree(state);
  701. return NULL;
  702. }
  703. static struct dvb_frontend_ops s5h1409_ops = {
  704. .info = {
  705. .name = "Samsung S5H1409 QAM/8VSB Frontend",
  706. .type = FE_ATSC,
  707. .frequency_min = 54000000,
  708. .frequency_max = 858000000,
  709. .frequency_stepsize = 62500,
  710. .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
  711. },
  712. .init = s5h1409_init,
  713. .i2c_gate_ctrl = s5h1409_i2c_gate_ctrl,
  714. .set_frontend = s5h1409_set_frontend,
  715. .get_frontend = s5h1409_get_frontend,
  716. .get_tune_settings = s5h1409_get_tune_settings,
  717. .read_status = s5h1409_read_status,
  718. .read_ber = s5h1409_read_ber,
  719. .read_signal_strength = s5h1409_read_signal_strength,
  720. .read_snr = s5h1409_read_snr,
  721. .read_ucblocks = s5h1409_read_ucblocks,
  722. .release = s5h1409_release,
  723. };
  724. module_param(debug, int, 0644);
  725. MODULE_PARM_DESC(debug, "Enable verbose debug messages");
  726. MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
  727. MODULE_AUTHOR("Steven Toth");
  728. MODULE_LICENSE("GPL");
  729. EXPORT_SYMBOL(s5h1409_attach);
  730. /*
  731. * Local variables:
  732. * c-basic-offset: 8
  733. */