s5h1411.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. Samsung S5H1411 VSB/QAM demodulator driver
  3. Copyright (C) 2008 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 "s5h1411.h"
  25. struct s5h1411_state {
  26. struct i2c_adapter *i2c;
  27. /* configuration settings */
  28. const struct s5h1411_config *config;
  29. struct dvb_frontend frontend;
  30. fe_modulation_t current_modulation;
  31. u32 current_frequency;
  32. int if_freq;
  33. u8 inversion;
  34. };
  35. static int debug;
  36. #define dprintk(arg...) do { \
  37. if (debug) \
  38. printk(arg); \
  39. } while (0)
  40. /* Register values to initialise the demod, defaults to VSB */
  41. static struct init_tab {
  42. u8 addr;
  43. u8 reg;
  44. u16 data;
  45. } init_tab[] = {
  46. { S5H1411_I2C_TOP_ADDR, 0x00, 0x0071, },
  47. { S5H1411_I2C_TOP_ADDR, 0x08, 0x0047, },
  48. { S5H1411_I2C_TOP_ADDR, 0x1c, 0x0400, },
  49. { S5H1411_I2C_TOP_ADDR, 0x1e, 0x0370, },
  50. { S5H1411_I2C_TOP_ADDR, 0x1f, 0x342a, },
  51. { S5H1411_I2C_TOP_ADDR, 0x24, 0x0231, },
  52. { S5H1411_I2C_TOP_ADDR, 0x25, 0x1011, },
  53. { S5H1411_I2C_TOP_ADDR, 0x26, 0x0f07, },
  54. { S5H1411_I2C_TOP_ADDR, 0x27, 0x0f04, },
  55. { S5H1411_I2C_TOP_ADDR, 0x28, 0x070f, },
  56. { S5H1411_I2C_TOP_ADDR, 0x29, 0x2820, },
  57. { S5H1411_I2C_TOP_ADDR, 0x2a, 0x102e, },
  58. { S5H1411_I2C_TOP_ADDR, 0x2b, 0x0220, },
  59. { S5H1411_I2C_TOP_ADDR, 0x2e, 0x0d0e, },
  60. { S5H1411_I2C_TOP_ADDR, 0x2f, 0x1013, },
  61. { S5H1411_I2C_TOP_ADDR, 0x31, 0x171b, },
  62. { S5H1411_I2C_TOP_ADDR, 0x32, 0x0e0f, },
  63. { S5H1411_I2C_TOP_ADDR, 0x33, 0x0f10, },
  64. { S5H1411_I2C_TOP_ADDR, 0x34, 0x170e, },
  65. { S5H1411_I2C_TOP_ADDR, 0x35, 0x4b10, },
  66. { S5H1411_I2C_TOP_ADDR, 0x36, 0x0f17, },
  67. { S5H1411_I2C_TOP_ADDR, 0x3c, 0x1577, },
  68. { S5H1411_I2C_TOP_ADDR, 0x3d, 0x081a, },
  69. { S5H1411_I2C_TOP_ADDR, 0x3e, 0x77ee, },
  70. { S5H1411_I2C_TOP_ADDR, 0x40, 0x1e09, },
  71. { S5H1411_I2C_TOP_ADDR, 0x41, 0x0f0c, },
  72. { S5H1411_I2C_TOP_ADDR, 0x42, 0x1f10, },
  73. { S5H1411_I2C_TOP_ADDR, 0x4d, 0x0509, },
  74. { S5H1411_I2C_TOP_ADDR, 0x4e, 0x0a00, },
  75. { S5H1411_I2C_TOP_ADDR, 0x50, 0x0000, },
  76. { S5H1411_I2C_TOP_ADDR, 0x5b, 0x0000, },
  77. { S5H1411_I2C_TOP_ADDR, 0x5c, 0x0008, },
  78. { S5H1411_I2C_TOP_ADDR, 0x57, 0x1101, },
  79. { S5H1411_I2C_TOP_ADDR, 0x65, 0x007c, },
  80. { S5H1411_I2C_TOP_ADDR, 0x68, 0x0512, },
  81. { S5H1411_I2C_TOP_ADDR, 0x69, 0x0258, },
  82. { S5H1411_I2C_TOP_ADDR, 0x70, 0x0004, },
  83. { S5H1411_I2C_TOP_ADDR, 0x71, 0x0007, },
  84. { S5H1411_I2C_TOP_ADDR, 0x76, 0x00a9, },
  85. { S5H1411_I2C_TOP_ADDR, 0x78, 0x3141, },
  86. { S5H1411_I2C_TOP_ADDR, 0x7a, 0x3141, },
  87. { S5H1411_I2C_TOP_ADDR, 0xb3, 0x8003, },
  88. { S5H1411_I2C_TOP_ADDR, 0xb5, 0xafbb, },
  89. { S5H1411_I2C_TOP_ADDR, 0xb5, 0xa6bb, },
  90. { S5H1411_I2C_TOP_ADDR, 0xb6, 0x0609, },
  91. { S5H1411_I2C_TOP_ADDR, 0xb7, 0x2f06, },
  92. { S5H1411_I2C_TOP_ADDR, 0xb8, 0x003f, },
  93. { S5H1411_I2C_TOP_ADDR, 0xb9, 0x2700, },
  94. { S5H1411_I2C_TOP_ADDR, 0xba, 0xfac8, },
  95. { S5H1411_I2C_TOP_ADDR, 0xbe, 0x1003, },
  96. { S5H1411_I2C_TOP_ADDR, 0xbf, 0x103f, },
  97. { S5H1411_I2C_TOP_ADDR, 0xce, 0x2000, },
  98. { S5H1411_I2C_TOP_ADDR, 0xcf, 0x0800, },
  99. { S5H1411_I2C_TOP_ADDR, 0xd0, 0x0800, },
  100. { S5H1411_I2C_TOP_ADDR, 0xd1, 0x0400, },
  101. { S5H1411_I2C_TOP_ADDR, 0xd2, 0x0800, },
  102. { S5H1411_I2C_TOP_ADDR, 0xd3, 0x2000, },
  103. { S5H1411_I2C_TOP_ADDR, 0xd4, 0x3000, },
  104. { S5H1411_I2C_TOP_ADDR, 0xdb, 0x4a9b, },
  105. { S5H1411_I2C_TOP_ADDR, 0xdc, 0x1000, },
  106. { S5H1411_I2C_TOP_ADDR, 0xde, 0x0001, },
  107. { S5H1411_I2C_TOP_ADDR, 0xdf, 0x0000, },
  108. { S5H1411_I2C_TOP_ADDR, 0xe3, 0x0301, },
  109. { S5H1411_I2C_QAM_ADDR, 0xf3, 0x0000, },
  110. { S5H1411_I2C_QAM_ADDR, 0xf3, 0x0001, },
  111. { S5H1411_I2C_QAM_ADDR, 0x08, 0x0600, },
  112. { S5H1411_I2C_QAM_ADDR, 0x18, 0x4201, },
  113. { S5H1411_I2C_QAM_ADDR, 0x1e, 0x6476, },
  114. { S5H1411_I2C_QAM_ADDR, 0x21, 0x0830, },
  115. { S5H1411_I2C_QAM_ADDR, 0x0c, 0x5679, },
  116. { S5H1411_I2C_QAM_ADDR, 0x0d, 0x579b, },
  117. { S5H1411_I2C_QAM_ADDR, 0x24, 0x0102, },
  118. { S5H1411_I2C_QAM_ADDR, 0x31, 0x7488, },
  119. { S5H1411_I2C_QAM_ADDR, 0x32, 0x0a08, },
  120. { S5H1411_I2C_QAM_ADDR, 0x3d, 0x8689, },
  121. { S5H1411_I2C_QAM_ADDR, 0x49, 0x0048, },
  122. { S5H1411_I2C_QAM_ADDR, 0x57, 0x2012, },
  123. { S5H1411_I2C_QAM_ADDR, 0x5d, 0x7676, },
  124. { S5H1411_I2C_QAM_ADDR, 0x04, 0x0400, },
  125. { S5H1411_I2C_QAM_ADDR, 0x58, 0x00c0, },
  126. { S5H1411_I2C_QAM_ADDR, 0x5b, 0x0100, },
  127. };
  128. /* VSB SNR lookup table */
  129. static struct vsb_snr_tab {
  130. u16 val;
  131. u16 data;
  132. } vsb_snr_tab[] = {
  133. { 0x39f, 300, },
  134. { 0x39b, 295, },
  135. { 0x397, 290, },
  136. { 0x394, 285, },
  137. { 0x38f, 280, },
  138. { 0x38b, 275, },
  139. { 0x387, 270, },
  140. { 0x382, 265, },
  141. { 0x37d, 260, },
  142. { 0x377, 255, },
  143. { 0x370, 250, },
  144. { 0x36a, 245, },
  145. { 0x364, 240, },
  146. { 0x35b, 235, },
  147. { 0x353, 230, },
  148. { 0x349, 225, },
  149. { 0x340, 320, },
  150. { 0x337, 215, },
  151. { 0x327, 210, },
  152. { 0x31b, 205, },
  153. { 0x310, 200, },
  154. { 0x302, 195, },
  155. { 0x2f3, 190, },
  156. { 0x2e4, 185, },
  157. { 0x2d7, 180, },
  158. { 0x2cd, 175, },
  159. { 0x2bb, 170, },
  160. { 0x2a9, 165, },
  161. { 0x29e, 160, },
  162. { 0x284, 155, },
  163. { 0x27a, 150, },
  164. { 0x260, 145, },
  165. { 0x23a, 140, },
  166. { 0x224, 135, },
  167. { 0x213, 130, },
  168. { 0x204, 125, },
  169. { 0x1fe, 120, },
  170. { 0, 0, },
  171. };
  172. /* QAM64 SNR lookup table */
  173. static struct qam64_snr_tab {
  174. u16 val;
  175. u16 data;
  176. } qam64_snr_tab[] = {
  177. { 0x0001, 0, },
  178. { 0x0af0, 300, },
  179. { 0x0d80, 290, },
  180. { 0x10a0, 280, },
  181. { 0x14b5, 270, },
  182. { 0x1590, 268, },
  183. { 0x1680, 266, },
  184. { 0x17b0, 264, },
  185. { 0x18c0, 262, },
  186. { 0x19b0, 260, },
  187. { 0x1ad0, 258, },
  188. { 0x1d00, 256, },
  189. { 0x1da0, 254, },
  190. { 0x1ef0, 252, },
  191. { 0x2050, 250, },
  192. { 0x20f0, 249, },
  193. { 0x21d0, 248, },
  194. { 0x22b0, 247, },
  195. { 0x23a0, 246, },
  196. { 0x2470, 245, },
  197. { 0x24f0, 244, },
  198. { 0x25a0, 243, },
  199. { 0x26c0, 242, },
  200. { 0x27b0, 241, },
  201. { 0x28d0, 240, },
  202. { 0x29b0, 239, },
  203. { 0x2ad0, 238, },
  204. { 0x2ba0, 237, },
  205. { 0x2c80, 236, },
  206. { 0x2d20, 235, },
  207. { 0x2e00, 234, },
  208. { 0x2f10, 233, },
  209. { 0x3050, 232, },
  210. { 0x3190, 231, },
  211. { 0x3300, 230, },
  212. { 0x3340, 229, },
  213. { 0x3200, 228, },
  214. { 0x3550, 227, },
  215. { 0x3610, 226, },
  216. { 0x3600, 225, },
  217. { 0x3700, 224, },
  218. { 0x3800, 223, },
  219. { 0x3920, 222, },
  220. { 0x3a20, 221, },
  221. { 0x3b30, 220, },
  222. { 0x3d00, 219, },
  223. { 0x3e00, 218, },
  224. { 0x4000, 217, },
  225. { 0x4100, 216, },
  226. { 0x4300, 215, },
  227. { 0x4400, 214, },
  228. { 0x4600, 213, },
  229. { 0x4700, 212, },
  230. { 0x4800, 211, },
  231. { 0x4a00, 210, },
  232. { 0x4b00, 209, },
  233. { 0x4d00, 208, },
  234. { 0x4f00, 207, },
  235. { 0x5050, 206, },
  236. { 0x5200, 205, },
  237. { 0x53c0, 204, },
  238. { 0x5450, 203, },
  239. { 0x5650, 202, },
  240. { 0x5820, 201, },
  241. { 0x6000, 200, },
  242. { 0xffff, 0, },
  243. };
  244. /* QAM256 SNR lookup table */
  245. static struct qam256_snr_tab {
  246. u16 val;
  247. u16 data;
  248. } qam256_snr_tab[] = {
  249. { 0x0001, 0, },
  250. { 0x0970, 400, },
  251. { 0x0a90, 390, },
  252. { 0x0b90, 380, },
  253. { 0x0d90, 370, },
  254. { 0x0ff0, 360, },
  255. { 0x1240, 350, },
  256. { 0x1345, 348, },
  257. { 0x13c0, 346, },
  258. { 0x14c0, 344, },
  259. { 0x1500, 342, },
  260. { 0x1610, 340, },
  261. { 0x1700, 338, },
  262. { 0x1800, 336, },
  263. { 0x18b0, 334, },
  264. { 0x1900, 332, },
  265. { 0x1ab0, 330, },
  266. { 0x1bc0, 328, },
  267. { 0x1cb0, 326, },
  268. { 0x1db0, 324, },
  269. { 0x1eb0, 322, },
  270. { 0x2030, 320, },
  271. { 0x2200, 318, },
  272. { 0x2280, 316, },
  273. { 0x2410, 314, },
  274. { 0x25b0, 312, },
  275. { 0x27a0, 310, },
  276. { 0x2840, 308, },
  277. { 0x29d0, 306, },
  278. { 0x2b10, 304, },
  279. { 0x2d30, 302, },
  280. { 0x2f20, 300, },
  281. { 0x30c0, 298, },
  282. { 0x3260, 297, },
  283. { 0x32c0, 296, },
  284. { 0x3300, 295, },
  285. { 0x33b0, 294, },
  286. { 0x34b0, 293, },
  287. { 0x35a0, 292, },
  288. { 0x3650, 291, },
  289. { 0x3800, 290, },
  290. { 0x3900, 289, },
  291. { 0x3a50, 288, },
  292. { 0x3b30, 287, },
  293. { 0x3cb0, 286, },
  294. { 0x3e20, 285, },
  295. { 0x3fa0, 284, },
  296. { 0x40a0, 283, },
  297. { 0x41c0, 282, },
  298. { 0x42f0, 281, },
  299. { 0x44a0, 280, },
  300. { 0x4600, 279, },
  301. { 0x47b0, 278, },
  302. { 0x4900, 277, },
  303. { 0x4a00, 276, },
  304. { 0x4ba0, 275, },
  305. { 0x4d00, 274, },
  306. { 0x4f00, 273, },
  307. { 0x5000, 272, },
  308. { 0x51f0, 272, },
  309. { 0x53a0, 270, },
  310. { 0x5520, 269, },
  311. { 0x5700, 268, },
  312. { 0x5800, 267, },
  313. { 0x5a00, 266, },
  314. { 0x5c00, 265, },
  315. { 0x5d00, 264, },
  316. { 0x5f00, 263, },
  317. { 0x6000, 262, },
  318. { 0x6200, 261, },
  319. { 0x6400, 260, },
  320. { 0xffff, 0, },
  321. };
  322. /* 8 bit registers, 16 bit values */
  323. static int s5h1411_writereg(struct s5h1411_state *state,
  324. u8 addr, u8 reg, u16 data)
  325. {
  326. int ret;
  327. u8 buf [] = { reg, data >> 8, data & 0xff };
  328. struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = 3 };
  329. ret = i2c_transfer(state->i2c, &msg, 1);
  330. if (ret != 1)
  331. printk(KERN_ERR "%s: writereg error 0x%02x 0x%02x 0x%04x, "
  332. "ret == %i)\n", __func__, addr, reg, data, ret);
  333. return (ret != 1) ? -1 : 0;
  334. }
  335. static u16 s5h1411_readreg(struct s5h1411_state *state, u8 addr, u8 reg)
  336. {
  337. int ret;
  338. u8 b0 [] = { reg };
  339. u8 b1 [] = { 0, 0 };
  340. struct i2c_msg msg [] = {
  341. { .addr = addr, .flags = 0, .buf = b0, .len = 1 },
  342. { .addr = addr, .flags = I2C_M_RD, .buf = b1, .len = 2 } };
  343. ret = i2c_transfer(state->i2c, msg, 2);
  344. if (ret != 2)
  345. printk(KERN_ERR "%s: readreg error (ret == %i)\n",
  346. __func__, ret);
  347. return (b1[0] << 8) | b1[1];
  348. }
  349. static int s5h1411_softreset(struct dvb_frontend *fe)
  350. {
  351. struct s5h1411_state *state = fe->demodulator_priv;
  352. dprintk("%s()\n", __func__);
  353. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf7, 0);
  354. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf7, 1);
  355. return 0;
  356. }
  357. static int s5h1411_set_if_freq(struct dvb_frontend *fe, int KHz)
  358. {
  359. struct s5h1411_state *state = fe->demodulator_priv;
  360. dprintk("%s(%d KHz)\n", __func__, KHz);
  361. switch (KHz) {
  362. case 3250:
  363. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x38, 0x10d9);
  364. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x39, 0x5342);
  365. s5h1411_writereg(state, S5H1411_I2C_QAM_ADDR, 0x2c, 0x10d9);
  366. break;
  367. case 3500:
  368. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x38, 0x1225);
  369. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x39, 0x1e96);
  370. s5h1411_writereg(state, S5H1411_I2C_QAM_ADDR, 0x2c, 0x1225);
  371. break;
  372. case 4000:
  373. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x38, 0x14bc);
  374. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x39, 0xb53e);
  375. s5h1411_writereg(state, S5H1411_I2C_QAM_ADDR, 0x2c, 0x14bd);
  376. break;
  377. default:
  378. dprintk("%s(%d KHz) Invalid, defaulting to 5380\n",
  379. __func__, KHz);
  380. /* no break, need to continue */
  381. case 5380:
  382. case 44000:
  383. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x38, 0x1be4);
  384. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x39, 0x3655);
  385. s5h1411_writereg(state, S5H1411_I2C_QAM_ADDR, 0x2c, 0x1be4);
  386. break;
  387. }
  388. state->if_freq = KHz;
  389. return 0;
  390. }
  391. static int s5h1411_set_mpeg_timing(struct dvb_frontend *fe, int mode)
  392. {
  393. struct s5h1411_state *state = fe->demodulator_priv;
  394. u16 val;
  395. dprintk("%s(%d)\n", __func__, mode);
  396. val = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xbe) & 0xcfff;
  397. switch (mode) {
  398. case S5H1411_MPEGTIMING_CONTINOUS_INVERTING_CLOCK:
  399. val |= 0x0000;
  400. break;
  401. case S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK:
  402. dprintk("%s(%d) Mode1 or Defaulting\n", __func__, mode);
  403. val |= 0x1000;
  404. break;
  405. case S5H1411_MPEGTIMING_NONCONTINOUS_INVERTING_CLOCK:
  406. val |= 0x2000;
  407. break;
  408. case S5H1411_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK:
  409. val |= 0x3000;
  410. break;
  411. default:
  412. return -EINVAL;
  413. }
  414. /* Configure MPEG Signal Timing charactistics */
  415. return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xbe, val);
  416. }
  417. static int s5h1411_set_spectralinversion(struct dvb_frontend *fe, int inversion)
  418. {
  419. struct s5h1411_state *state = fe->demodulator_priv;
  420. u16 val;
  421. dprintk("%s(%d)\n", __func__, inversion);
  422. val = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0x24) & ~0x1000;
  423. if (inversion == 1)
  424. val |= 0x1000; /* Inverted */
  425. else
  426. val |= 0x0000;
  427. state->inversion = inversion;
  428. return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x24, val);
  429. }
  430. static int s5h1411_enable_modulation(struct dvb_frontend *fe,
  431. fe_modulation_t m)
  432. {
  433. struct s5h1411_state *state = fe->demodulator_priv;
  434. dprintk("%s(0x%08x)\n", __func__, m);
  435. switch (m) {
  436. case VSB_8:
  437. dprintk("%s() VSB_8\n", __func__);
  438. s5h1411_set_if_freq(fe, state->config->vsb_if);
  439. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x00, 0x71);
  440. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf6, 0x00);
  441. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xcd, 0xf1);
  442. break;
  443. case QAM_64:
  444. case QAM_256:
  445. dprintk("%s() QAM_AUTO (64/256)\n", __func__);
  446. s5h1411_set_if_freq(fe, state->config->qam_if);
  447. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x00, 0x0171);
  448. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf6, 0x0001);
  449. s5h1411_writereg(state, S5H1411_I2C_QAM_ADDR, 0x16, 0x1101);
  450. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xcd, 0x00f0);
  451. break;
  452. default:
  453. dprintk("%s() Invalid modulation\n", __func__);
  454. return -EINVAL;
  455. }
  456. state->current_modulation = m;
  457. s5h1411_softreset(fe);
  458. return 0;
  459. }
  460. static int s5h1411_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  461. {
  462. struct s5h1411_state *state = fe->demodulator_priv;
  463. dprintk("%s(%d)\n", __func__, enable);
  464. if (enable)
  465. return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf5, 1);
  466. else
  467. return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf5, 0);
  468. }
  469. static int s5h1411_set_gpio(struct dvb_frontend *fe, int enable)
  470. {
  471. struct s5h1411_state *state = fe->demodulator_priv;
  472. u16 val;
  473. dprintk("%s(%d)\n", __func__, enable);
  474. val = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xe0) & ~0x02;
  475. if (enable)
  476. return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xe0,
  477. val | 0x02);
  478. else
  479. return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xe0, val);
  480. }
  481. static int s5h1411_sleep(struct dvb_frontend *fe, int enable)
  482. {
  483. struct s5h1411_state *state = fe->demodulator_priv;
  484. dprintk("%s(%d)\n", __func__, enable);
  485. if (enable)
  486. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf4, 1);
  487. else {
  488. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf4, 0);
  489. s5h1411_softreset(fe);
  490. }
  491. return 0;
  492. }
  493. static int s5h1411_register_reset(struct dvb_frontend *fe)
  494. {
  495. struct s5h1411_state *state = fe->demodulator_priv;
  496. dprintk("%s()\n", __func__);
  497. return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf3, 0);
  498. }
  499. /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
  500. static int s5h1411_set_frontend(struct dvb_frontend *fe,
  501. struct dvb_frontend_parameters *p)
  502. {
  503. struct s5h1411_state *state = fe->demodulator_priv;
  504. dprintk("%s(frequency=%d)\n", __func__, p->frequency);
  505. s5h1411_softreset(fe);
  506. state->current_frequency = p->frequency;
  507. s5h1411_enable_modulation(fe, p->u.vsb.modulation);
  508. /* Allow the demod to settle */
  509. msleep(100);
  510. if (fe->ops.tuner_ops.set_params) {
  511. if (fe->ops.i2c_gate_ctrl)
  512. fe->ops.i2c_gate_ctrl(fe, 1);
  513. fe->ops.tuner_ops.set_params(fe, p);
  514. if (fe->ops.i2c_gate_ctrl)
  515. fe->ops.i2c_gate_ctrl(fe, 0);
  516. }
  517. return 0;
  518. }
  519. /* Reset the demod hardware and reset all of the configuration registers
  520. to a default state. */
  521. static int s5h1411_init(struct dvb_frontend *fe)
  522. {
  523. struct s5h1411_state *state = fe->demodulator_priv;
  524. int i;
  525. dprintk("%s()\n", __func__);
  526. s5h1411_sleep(fe, 0);
  527. s5h1411_register_reset(fe);
  528. for (i = 0; i < ARRAY_SIZE(init_tab); i++)
  529. s5h1411_writereg(state, init_tab[i].addr,
  530. init_tab[i].reg,
  531. init_tab[i].data);
  532. /* The datasheet says that after initialisation, VSB is default */
  533. state->current_modulation = VSB_8;
  534. if (state->config->output_mode == S5H1411_SERIAL_OUTPUT)
  535. /* Serial */
  536. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xbd, 0x1101);
  537. else
  538. /* Parallel */
  539. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xbd, 0x1001);
  540. s5h1411_set_spectralinversion(fe, state->config->inversion);
  541. s5h1411_set_if_freq(fe, state->config->vsb_if);
  542. s5h1411_set_gpio(fe, state->config->gpio);
  543. s5h1411_set_mpeg_timing(fe, state->config->mpeg_timing);
  544. s5h1411_softreset(fe);
  545. /* Note: Leaving the I2C gate closed. */
  546. s5h1411_i2c_gate_ctrl(fe, 0);
  547. return 0;
  548. }
  549. static int s5h1411_read_status(struct dvb_frontend *fe, fe_status_t *status)
  550. {
  551. struct s5h1411_state *state = fe->demodulator_priv;
  552. u16 reg;
  553. u32 tuner_status = 0;
  554. *status = 0;
  555. /* Get the demodulator status */
  556. reg = (s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xf2) >> 15)
  557. & 0x0001;
  558. if (reg)
  559. *status |= FE_HAS_LOCK | FE_HAS_CARRIER | FE_HAS_SIGNAL;
  560. switch (state->current_modulation) {
  561. case QAM_64:
  562. case QAM_256:
  563. reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xf0);
  564. if (reg & 0x100)
  565. *status |= FE_HAS_VITERBI;
  566. if (reg & 0x10)
  567. *status |= FE_HAS_SYNC;
  568. break;
  569. case VSB_8:
  570. reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0x5e);
  571. if (reg & 0x0001)
  572. *status |= FE_HAS_SYNC;
  573. reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xf2);
  574. if (reg & 0x1000)
  575. *status |= FE_HAS_VITERBI;
  576. break;
  577. default:
  578. return -EINVAL;
  579. }
  580. switch (state->config->status_mode) {
  581. case S5H1411_DEMODLOCKING:
  582. if (*status & FE_HAS_VITERBI)
  583. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  584. break;
  585. case S5H1411_TUNERLOCKING:
  586. /* Get the tuner status */
  587. if (fe->ops.tuner_ops.get_status) {
  588. if (fe->ops.i2c_gate_ctrl)
  589. fe->ops.i2c_gate_ctrl(fe, 1);
  590. fe->ops.tuner_ops.get_status(fe, &tuner_status);
  591. if (fe->ops.i2c_gate_ctrl)
  592. fe->ops.i2c_gate_ctrl(fe, 0);
  593. }
  594. if (tuner_status)
  595. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  596. break;
  597. }
  598. dprintk("%s() status 0x%08x\n", __func__, *status);
  599. return 0;
  600. }
  601. static int s5h1411_qam256_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
  602. {
  603. int i, ret = -EINVAL;
  604. dprintk("%s()\n", __func__);
  605. for (i = 0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
  606. if (v < qam256_snr_tab[i].val) {
  607. *snr = qam256_snr_tab[i].data;
  608. ret = 0;
  609. break;
  610. }
  611. }
  612. return ret;
  613. }
  614. static int s5h1411_qam64_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
  615. {
  616. int i, ret = -EINVAL;
  617. dprintk("%s()\n", __func__);
  618. for (i = 0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
  619. if (v < qam64_snr_tab[i].val) {
  620. *snr = qam64_snr_tab[i].data;
  621. ret = 0;
  622. break;
  623. }
  624. }
  625. return ret;
  626. }
  627. static int s5h1411_vsb_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
  628. {
  629. int i, ret = -EINVAL;
  630. dprintk("%s()\n", __func__);
  631. for (i = 0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
  632. if (v > vsb_snr_tab[i].val) {
  633. *snr = vsb_snr_tab[i].data;
  634. ret = 0;
  635. break;
  636. }
  637. }
  638. dprintk("%s() snr=%d\n", __func__, *snr);
  639. return ret;
  640. }
  641. static int s5h1411_read_snr(struct dvb_frontend *fe, u16 *snr)
  642. {
  643. struct s5h1411_state *state = fe->demodulator_priv;
  644. u16 reg;
  645. dprintk("%s()\n", __func__);
  646. switch (state->current_modulation) {
  647. case QAM_64:
  648. reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xf1);
  649. return s5h1411_qam64_lookup_snr(fe, snr, reg);
  650. case QAM_256:
  651. reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xf1);
  652. return s5h1411_qam256_lookup_snr(fe, snr, reg);
  653. case VSB_8:
  654. reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR,
  655. 0xf2) & 0x3ff;
  656. return s5h1411_vsb_lookup_snr(fe, snr, reg);
  657. default:
  658. break;
  659. }
  660. return -EINVAL;
  661. }
  662. static int s5h1411_read_signal_strength(struct dvb_frontend *fe,
  663. u16 *signal_strength)
  664. {
  665. return s5h1411_read_snr(fe, signal_strength);
  666. }
  667. static int s5h1411_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  668. {
  669. struct s5h1411_state *state = fe->demodulator_priv;
  670. *ucblocks = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xc9);
  671. return 0;
  672. }
  673. static int s5h1411_read_ber(struct dvb_frontend *fe, u32 *ber)
  674. {
  675. return s5h1411_read_ucblocks(fe, ber);
  676. }
  677. static int s5h1411_get_frontend(struct dvb_frontend *fe,
  678. struct dvb_frontend_parameters *p)
  679. {
  680. struct s5h1411_state *state = fe->demodulator_priv;
  681. p->frequency = state->current_frequency;
  682. p->u.vsb.modulation = state->current_modulation;
  683. return 0;
  684. }
  685. static int s5h1411_get_tune_settings(struct dvb_frontend *fe,
  686. struct dvb_frontend_tune_settings *tune)
  687. {
  688. tune->min_delay_ms = 1000;
  689. return 0;
  690. }
  691. static void s5h1411_release(struct dvb_frontend *fe)
  692. {
  693. struct s5h1411_state *state = fe->demodulator_priv;
  694. kfree(state);
  695. }
  696. static struct dvb_frontend_ops s5h1411_ops;
  697. struct dvb_frontend *s5h1411_attach(const struct s5h1411_config *config,
  698. struct i2c_adapter *i2c)
  699. {
  700. struct s5h1411_state *state = NULL;
  701. u16 reg;
  702. /* allocate memory for the internal state */
  703. state = kmalloc(sizeof(struct s5h1411_state), GFP_KERNEL);
  704. if (state == NULL)
  705. goto error;
  706. /* setup the state */
  707. state->config = config;
  708. state->i2c = i2c;
  709. state->current_modulation = VSB_8;
  710. state->inversion = state->config->inversion;
  711. /* check if the demod exists */
  712. reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0x05);
  713. if (reg != 0x0066)
  714. goto error;
  715. /* create dvb_frontend */
  716. memcpy(&state->frontend.ops, &s5h1411_ops,
  717. sizeof(struct dvb_frontend_ops));
  718. state->frontend.demodulator_priv = state;
  719. if (s5h1411_init(&state->frontend) != 0) {
  720. printk(KERN_ERR "%s: Failed to initialize correctly\n",
  721. __func__);
  722. goto error;
  723. }
  724. /* Note: Leaving the I2C gate open here. */
  725. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf5, 1);
  726. return &state->frontend;
  727. error:
  728. kfree(state);
  729. return NULL;
  730. }
  731. EXPORT_SYMBOL(s5h1411_attach);
  732. static struct dvb_frontend_ops s5h1411_ops = {
  733. .info = {
  734. .name = "Samsung S5H1411 QAM/8VSB Frontend",
  735. .type = FE_ATSC,
  736. .frequency_min = 54000000,
  737. .frequency_max = 858000000,
  738. .frequency_stepsize = 62500,
  739. .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
  740. },
  741. .init = s5h1411_init,
  742. .i2c_gate_ctrl = s5h1411_i2c_gate_ctrl,
  743. .set_frontend = s5h1411_set_frontend,
  744. .get_frontend = s5h1411_get_frontend,
  745. .get_tune_settings = s5h1411_get_tune_settings,
  746. .read_status = s5h1411_read_status,
  747. .read_ber = s5h1411_read_ber,
  748. .read_signal_strength = s5h1411_read_signal_strength,
  749. .read_snr = s5h1411_read_snr,
  750. .read_ucblocks = s5h1411_read_ucblocks,
  751. .release = s5h1411_release,
  752. };
  753. module_param(debug, int, 0644);
  754. MODULE_PARM_DESC(debug, "Enable verbose debug messages");
  755. MODULE_DESCRIPTION("Samsung S5H1411 QAM-B/ATSC Demodulator driver");
  756. MODULE_AUTHOR("Steven Toth");
  757. MODULE_LICENSE("GPL");
  758. /*
  759. * Local variables:
  760. * c-basic-offset: 8
  761. */