s5h1411.c 23 KB

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