s5h1411.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /*
  2. Samsung S5H1411 VSB/QAM demodulator driver
  3. Copyright (C) 2008 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 "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. enum fe_modulation current_modulation;
  30. unsigned int first_tune:1;
  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, 0x342c, },
  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, 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, 0x10d5);
  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. state->inversion = inversion;
  425. return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x24, val);
  426. }
  427. static int s5h1411_set_serialmode(struct dvb_frontend *fe, int serial)
  428. {
  429. struct s5h1411_state *state = fe->demodulator_priv;
  430. u16 val;
  431. dprintk("%s(%d)\n", __func__, serial);
  432. val = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xbd) & ~0x100;
  433. if (serial == 1)
  434. val |= 0x100;
  435. return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xbd, val);
  436. }
  437. static int s5h1411_enable_modulation(struct dvb_frontend *fe,
  438. enum fe_modulation m)
  439. {
  440. struct s5h1411_state *state = fe->demodulator_priv;
  441. dprintk("%s(0x%08x)\n", __func__, m);
  442. if ((state->first_tune == 0) && (m == state->current_modulation)) {
  443. dprintk("%s() Already at desired modulation. Skipping...\n",
  444. __func__);
  445. return 0;
  446. }
  447. switch (m) {
  448. case VSB_8:
  449. dprintk("%s() VSB_8\n", __func__);
  450. s5h1411_set_if_freq(fe, state->config->vsb_if);
  451. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x00, 0x71);
  452. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf6, 0x00);
  453. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xcd, 0xf1);
  454. break;
  455. case QAM_64:
  456. case QAM_256:
  457. case QAM_AUTO:
  458. dprintk("%s() QAM_AUTO (64/256)\n", __func__);
  459. s5h1411_set_if_freq(fe, state->config->qam_if);
  460. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0x00, 0x0171);
  461. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf6, 0x0001);
  462. s5h1411_writereg(state, S5H1411_I2C_QAM_ADDR, 0x16, 0x1101);
  463. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xcd, 0x00f0);
  464. break;
  465. default:
  466. dprintk("%s() Invalid modulation\n", __func__);
  467. return -EINVAL;
  468. }
  469. state->current_modulation = m;
  470. state->first_tune = 0;
  471. s5h1411_softreset(fe);
  472. return 0;
  473. }
  474. static int s5h1411_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  475. {
  476. struct s5h1411_state *state = fe->demodulator_priv;
  477. dprintk("%s(%d)\n", __func__, enable);
  478. if (enable)
  479. return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf5, 1);
  480. else
  481. return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf5, 0);
  482. }
  483. static int s5h1411_set_gpio(struct dvb_frontend *fe, int enable)
  484. {
  485. struct s5h1411_state *state = fe->demodulator_priv;
  486. u16 val;
  487. dprintk("%s(%d)\n", __func__, enable);
  488. val = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xe0) & ~0x02;
  489. if (enable)
  490. return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xe0,
  491. val | 0x02);
  492. else
  493. return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xe0, val);
  494. }
  495. static int s5h1411_set_powerstate(struct dvb_frontend *fe, int enable)
  496. {
  497. struct s5h1411_state *state = fe->demodulator_priv;
  498. dprintk("%s(%d)\n", __func__, enable);
  499. if (enable)
  500. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf4, 1);
  501. else {
  502. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf4, 0);
  503. s5h1411_softreset(fe);
  504. }
  505. return 0;
  506. }
  507. static int s5h1411_sleep(struct dvb_frontend *fe)
  508. {
  509. return s5h1411_set_powerstate(fe, 1);
  510. }
  511. static int s5h1411_register_reset(struct dvb_frontend *fe)
  512. {
  513. struct s5h1411_state *state = fe->demodulator_priv;
  514. dprintk("%s()\n", __func__);
  515. return s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf3, 0);
  516. }
  517. /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
  518. static int s5h1411_set_frontend(struct dvb_frontend *fe)
  519. {
  520. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  521. struct s5h1411_state *state = fe->demodulator_priv;
  522. dprintk("%s(frequency=%d)\n", __func__, p->frequency);
  523. s5h1411_softreset(fe);
  524. state->current_frequency = p->frequency;
  525. s5h1411_enable_modulation(fe, p->modulation);
  526. if (fe->ops.tuner_ops.set_params) {
  527. if (fe->ops.i2c_gate_ctrl)
  528. fe->ops.i2c_gate_ctrl(fe, 1);
  529. fe->ops.tuner_ops.set_params(fe);
  530. if (fe->ops.i2c_gate_ctrl)
  531. fe->ops.i2c_gate_ctrl(fe, 0);
  532. }
  533. /* Issue a reset to the demod so it knows to resync against the
  534. newly tuned frequency */
  535. s5h1411_softreset(fe);
  536. return 0;
  537. }
  538. /* Reset the demod hardware and reset all of the configuration registers
  539. to a default state. */
  540. static int s5h1411_init(struct dvb_frontend *fe)
  541. {
  542. struct s5h1411_state *state = fe->demodulator_priv;
  543. int i;
  544. dprintk("%s()\n", __func__);
  545. s5h1411_set_powerstate(fe, 0);
  546. s5h1411_register_reset(fe);
  547. for (i = 0; i < ARRAY_SIZE(init_tab); i++)
  548. s5h1411_writereg(state, init_tab[i].addr,
  549. init_tab[i].reg,
  550. init_tab[i].data);
  551. /* The datasheet says that after initialisation, VSB is default */
  552. state->current_modulation = VSB_8;
  553. /* Although the datasheet says it's in VSB, empirical evidence
  554. shows problems getting lock on the first tuning request. Make
  555. sure we call enable_modulation the first time around */
  556. state->first_tune = 1;
  557. if (state->config->output_mode == S5H1411_SERIAL_OUTPUT)
  558. /* Serial */
  559. s5h1411_set_serialmode(fe, 1);
  560. else
  561. /* Parallel */
  562. s5h1411_set_serialmode(fe, 0);
  563. s5h1411_set_spectralinversion(fe, state->config->inversion);
  564. s5h1411_set_if_freq(fe, state->config->vsb_if);
  565. s5h1411_set_gpio(fe, state->config->gpio);
  566. s5h1411_set_mpeg_timing(fe, state->config->mpeg_timing);
  567. s5h1411_softreset(fe);
  568. /* Note: Leaving the I2C gate closed. */
  569. s5h1411_i2c_gate_ctrl(fe, 0);
  570. return 0;
  571. }
  572. static int s5h1411_read_status(struct dvb_frontend *fe, enum fe_status *status)
  573. {
  574. struct s5h1411_state *state = fe->demodulator_priv;
  575. u16 reg;
  576. u32 tuner_status = 0;
  577. *status = 0;
  578. /* Register F2 bit 15 = Master Lock, removed */
  579. switch (state->current_modulation) {
  580. case QAM_64:
  581. case QAM_256:
  582. reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xf0);
  583. if (reg & 0x10) /* QAM FEC Lock */
  584. *status |= FE_HAS_SYNC | FE_HAS_LOCK;
  585. if (reg & 0x100) /* QAM EQ Lock */
  586. *status |= FE_HAS_VITERBI | FE_HAS_CARRIER | FE_HAS_SIGNAL;
  587. break;
  588. case VSB_8:
  589. reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xf2);
  590. if (reg & 0x1000) /* FEC Lock */
  591. *status |= FE_HAS_SYNC | FE_HAS_LOCK;
  592. if (reg & 0x2000) /* EQ Lock */
  593. *status |= FE_HAS_VITERBI | FE_HAS_CARRIER | FE_HAS_SIGNAL;
  594. reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0x53);
  595. if (reg & 0x1) /* AFC Lock */
  596. *status |= FE_HAS_SIGNAL;
  597. break;
  598. default:
  599. return -EINVAL;
  600. }
  601. switch (state->config->status_mode) {
  602. case S5H1411_DEMODLOCKING:
  603. if (*status & FE_HAS_VITERBI)
  604. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  605. break;
  606. case S5H1411_TUNERLOCKING:
  607. /* Get the tuner status */
  608. if (fe->ops.tuner_ops.get_status) {
  609. if (fe->ops.i2c_gate_ctrl)
  610. fe->ops.i2c_gate_ctrl(fe, 1);
  611. fe->ops.tuner_ops.get_status(fe, &tuner_status);
  612. if (fe->ops.i2c_gate_ctrl)
  613. fe->ops.i2c_gate_ctrl(fe, 0);
  614. }
  615. if (tuner_status)
  616. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  617. break;
  618. }
  619. dprintk("%s() status 0x%08x\n", __func__, *status);
  620. return 0;
  621. }
  622. static int s5h1411_qam256_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
  623. {
  624. int i, ret = -EINVAL;
  625. dprintk("%s()\n", __func__);
  626. for (i = 0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
  627. if (v < qam256_snr_tab[i].val) {
  628. *snr = qam256_snr_tab[i].data;
  629. ret = 0;
  630. break;
  631. }
  632. }
  633. return ret;
  634. }
  635. static int s5h1411_qam64_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
  636. {
  637. int i, ret = -EINVAL;
  638. dprintk("%s()\n", __func__);
  639. for (i = 0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
  640. if (v < qam64_snr_tab[i].val) {
  641. *snr = qam64_snr_tab[i].data;
  642. ret = 0;
  643. break;
  644. }
  645. }
  646. return ret;
  647. }
  648. static int s5h1411_vsb_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
  649. {
  650. int i, ret = -EINVAL;
  651. dprintk("%s()\n", __func__);
  652. for (i = 0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
  653. if (v > vsb_snr_tab[i].val) {
  654. *snr = vsb_snr_tab[i].data;
  655. ret = 0;
  656. break;
  657. }
  658. }
  659. dprintk("%s() snr=%d\n", __func__, *snr);
  660. return ret;
  661. }
  662. static int s5h1411_read_snr(struct dvb_frontend *fe, u16 *snr)
  663. {
  664. struct s5h1411_state *state = fe->demodulator_priv;
  665. u16 reg;
  666. dprintk("%s()\n", __func__);
  667. switch (state->current_modulation) {
  668. case QAM_64:
  669. reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xf1);
  670. return s5h1411_qam64_lookup_snr(fe, snr, reg);
  671. case QAM_256:
  672. reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xf1);
  673. return s5h1411_qam256_lookup_snr(fe, snr, reg);
  674. case VSB_8:
  675. reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR,
  676. 0xf2) & 0x3ff;
  677. return s5h1411_vsb_lookup_snr(fe, snr, reg);
  678. default:
  679. break;
  680. }
  681. return -EINVAL;
  682. }
  683. static int s5h1411_read_signal_strength(struct dvb_frontend *fe,
  684. u16 *signal_strength)
  685. {
  686. /* borrowed from lgdt330x.c
  687. *
  688. * Calculate strength from SNR up to 35dB
  689. * Even though the SNR can go higher than 35dB,
  690. * there is some comfort factor in having a range of
  691. * strong signals that can show at 100%
  692. */
  693. u16 snr;
  694. u32 tmp;
  695. int ret = s5h1411_read_snr(fe, &snr);
  696. *signal_strength = 0;
  697. if (0 == ret) {
  698. /* The following calculation method was chosen
  699. * purely for the sake of code re-use from the
  700. * other demod drivers that use this method */
  701. /* Convert from SNR in dB * 10 to 8.24 fixed-point */
  702. tmp = (snr * ((1 << 24) / 10));
  703. /* Convert from 8.24 fixed-point to
  704. * scale the range 0 - 35*2^24 into 0 - 65535*/
  705. if (tmp >= 8960 * 0x10000)
  706. *signal_strength = 0xffff;
  707. else
  708. *signal_strength = tmp / 8960;
  709. }
  710. return ret;
  711. }
  712. static int s5h1411_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  713. {
  714. struct s5h1411_state *state = fe->demodulator_priv;
  715. *ucblocks = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0xc9);
  716. return 0;
  717. }
  718. static int s5h1411_read_ber(struct dvb_frontend *fe, u32 *ber)
  719. {
  720. return s5h1411_read_ucblocks(fe, ber);
  721. }
  722. static int s5h1411_get_frontend(struct dvb_frontend *fe)
  723. {
  724. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  725. struct s5h1411_state *state = fe->demodulator_priv;
  726. p->frequency = state->current_frequency;
  727. p->modulation = state->current_modulation;
  728. return 0;
  729. }
  730. static int s5h1411_get_tune_settings(struct dvb_frontend *fe,
  731. struct dvb_frontend_tune_settings *tune)
  732. {
  733. tune->min_delay_ms = 1000;
  734. return 0;
  735. }
  736. static void s5h1411_release(struct dvb_frontend *fe)
  737. {
  738. struct s5h1411_state *state = fe->demodulator_priv;
  739. kfree(state);
  740. }
  741. static struct dvb_frontend_ops s5h1411_ops;
  742. struct dvb_frontend *s5h1411_attach(const struct s5h1411_config *config,
  743. struct i2c_adapter *i2c)
  744. {
  745. struct s5h1411_state *state = NULL;
  746. u16 reg;
  747. /* allocate memory for the internal state */
  748. state = kzalloc(sizeof(struct s5h1411_state), GFP_KERNEL);
  749. if (state == NULL)
  750. goto error;
  751. /* setup the state */
  752. state->config = config;
  753. state->i2c = i2c;
  754. state->current_modulation = VSB_8;
  755. state->inversion = state->config->inversion;
  756. /* check if the demod exists */
  757. reg = s5h1411_readreg(state, S5H1411_I2C_TOP_ADDR, 0x05);
  758. if (reg != 0x0066)
  759. goto error;
  760. /* create dvb_frontend */
  761. memcpy(&state->frontend.ops, &s5h1411_ops,
  762. sizeof(struct dvb_frontend_ops));
  763. state->frontend.demodulator_priv = state;
  764. if (s5h1411_init(&state->frontend) != 0) {
  765. printk(KERN_ERR "%s: Failed to initialize correctly\n",
  766. __func__);
  767. goto error;
  768. }
  769. /* Note: Leaving the I2C gate open here. */
  770. s5h1411_writereg(state, S5H1411_I2C_TOP_ADDR, 0xf5, 1);
  771. /* Put the device into low-power mode until first use */
  772. s5h1411_set_powerstate(&state->frontend, 1);
  773. return &state->frontend;
  774. error:
  775. kfree(state);
  776. return NULL;
  777. }
  778. EXPORT_SYMBOL(s5h1411_attach);
  779. static struct dvb_frontend_ops s5h1411_ops = {
  780. .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
  781. .info = {
  782. .name = "Samsung S5H1411 QAM/8VSB Frontend",
  783. .frequency_min = 54000000,
  784. .frequency_max = 858000000,
  785. .frequency_stepsize = 62500,
  786. .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
  787. },
  788. .init = s5h1411_init,
  789. .sleep = s5h1411_sleep,
  790. .i2c_gate_ctrl = s5h1411_i2c_gate_ctrl,
  791. .set_frontend = s5h1411_set_frontend,
  792. .get_frontend = s5h1411_get_frontend,
  793. .get_tune_settings = s5h1411_get_tune_settings,
  794. .read_status = s5h1411_read_status,
  795. .read_ber = s5h1411_read_ber,
  796. .read_signal_strength = s5h1411_read_signal_strength,
  797. .read_snr = s5h1411_read_snr,
  798. .read_ucblocks = s5h1411_read_ucblocks,
  799. .release = s5h1411_release,
  800. };
  801. module_param(debug, int, 0644);
  802. MODULE_PARM_DESC(debug, "Enable verbose debug messages");
  803. MODULE_DESCRIPTION("Samsung S5H1411 QAM-B/ATSC Demodulator driver");
  804. MODULE_AUTHOR("Steven Toth");
  805. MODULE_LICENSE("GPL");