s5h1409.c 23 KB

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