s5h1411.c 22 KB

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