s5h1409.c 19 KB

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