au8522_dig.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Auvitek AU8522 QAM/8VSB 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/delay.h>
  11. #include <media/dvb_frontend.h>
  12. #include "au8522.h"
  13. #include "au8522_priv.h"
  14. static int debug;
  15. static int zv_mode = 1; /* default to on */
  16. #define dprintk(arg...)\
  17. do { if (debug)\
  18. printk(arg);\
  19. } while (0)
  20. struct mse2snr_tab {
  21. u16 val;
  22. u16 data;
  23. };
  24. /* VSB SNR lookup table */
  25. static struct mse2snr_tab vsb_mse2snr_tab[] = {
  26. { 0, 270 },
  27. { 2, 250 },
  28. { 3, 240 },
  29. { 5, 230 },
  30. { 7, 220 },
  31. { 9, 210 },
  32. { 12, 200 },
  33. { 13, 195 },
  34. { 15, 190 },
  35. { 17, 185 },
  36. { 19, 180 },
  37. { 21, 175 },
  38. { 24, 170 },
  39. { 27, 165 },
  40. { 31, 160 },
  41. { 32, 158 },
  42. { 33, 156 },
  43. { 36, 152 },
  44. { 37, 150 },
  45. { 39, 148 },
  46. { 40, 146 },
  47. { 41, 144 },
  48. { 43, 142 },
  49. { 44, 140 },
  50. { 48, 135 },
  51. { 50, 130 },
  52. { 43, 142 },
  53. { 53, 125 },
  54. { 56, 120 },
  55. { 256, 115 },
  56. };
  57. /* QAM64 SNR lookup table */
  58. static struct mse2snr_tab qam64_mse2snr_tab[] = {
  59. { 15, 0 },
  60. { 16, 290 },
  61. { 17, 288 },
  62. { 18, 286 },
  63. { 19, 284 },
  64. { 20, 282 },
  65. { 21, 281 },
  66. { 22, 279 },
  67. { 23, 277 },
  68. { 24, 275 },
  69. { 25, 273 },
  70. { 26, 271 },
  71. { 27, 269 },
  72. { 28, 268 },
  73. { 29, 266 },
  74. { 30, 264 },
  75. { 31, 262 },
  76. { 32, 260 },
  77. { 33, 259 },
  78. { 34, 258 },
  79. { 35, 256 },
  80. { 36, 255 },
  81. { 37, 254 },
  82. { 38, 252 },
  83. { 39, 251 },
  84. { 40, 250 },
  85. { 41, 249 },
  86. { 42, 248 },
  87. { 43, 246 },
  88. { 44, 245 },
  89. { 45, 244 },
  90. { 46, 242 },
  91. { 47, 241 },
  92. { 48, 240 },
  93. { 50, 239 },
  94. { 51, 238 },
  95. { 53, 237 },
  96. { 54, 236 },
  97. { 56, 235 },
  98. { 57, 234 },
  99. { 59, 233 },
  100. { 60, 232 },
  101. { 62, 231 },
  102. { 63, 230 },
  103. { 65, 229 },
  104. { 67, 228 },
  105. { 68, 227 },
  106. { 70, 226 },
  107. { 71, 225 },
  108. { 73, 224 },
  109. { 74, 223 },
  110. { 76, 222 },
  111. { 78, 221 },
  112. { 80, 220 },
  113. { 82, 219 },
  114. { 85, 218 },
  115. { 88, 217 },
  116. { 90, 216 },
  117. { 92, 215 },
  118. { 93, 214 },
  119. { 94, 212 },
  120. { 95, 211 },
  121. { 97, 210 },
  122. { 99, 209 },
  123. { 101, 208 },
  124. { 102, 207 },
  125. { 104, 206 },
  126. { 107, 205 },
  127. { 111, 204 },
  128. { 114, 203 },
  129. { 118, 202 },
  130. { 122, 201 },
  131. { 125, 200 },
  132. { 128, 199 },
  133. { 130, 198 },
  134. { 132, 197 },
  135. { 256, 190 },
  136. };
  137. /* QAM256 SNR lookup table */
  138. static struct mse2snr_tab qam256_mse2snr_tab[] = {
  139. { 15, 0 },
  140. { 16, 400 },
  141. { 17, 398 },
  142. { 18, 396 },
  143. { 19, 394 },
  144. { 20, 392 },
  145. { 21, 390 },
  146. { 22, 388 },
  147. { 23, 386 },
  148. { 24, 384 },
  149. { 25, 382 },
  150. { 26, 380 },
  151. { 27, 379 },
  152. { 28, 378 },
  153. { 29, 377 },
  154. { 30, 376 },
  155. { 31, 375 },
  156. { 32, 374 },
  157. { 33, 373 },
  158. { 34, 372 },
  159. { 35, 371 },
  160. { 36, 370 },
  161. { 37, 362 },
  162. { 38, 354 },
  163. { 39, 346 },
  164. { 40, 338 },
  165. { 41, 330 },
  166. { 42, 328 },
  167. { 43, 326 },
  168. { 44, 324 },
  169. { 45, 322 },
  170. { 46, 320 },
  171. { 47, 319 },
  172. { 48, 318 },
  173. { 49, 317 },
  174. { 50, 316 },
  175. { 51, 315 },
  176. { 52, 314 },
  177. { 53, 313 },
  178. { 54, 312 },
  179. { 55, 311 },
  180. { 56, 310 },
  181. { 57, 308 },
  182. { 58, 306 },
  183. { 59, 304 },
  184. { 60, 302 },
  185. { 61, 300 },
  186. { 62, 298 },
  187. { 65, 295 },
  188. { 68, 294 },
  189. { 70, 293 },
  190. { 73, 292 },
  191. { 76, 291 },
  192. { 78, 290 },
  193. { 79, 289 },
  194. { 81, 288 },
  195. { 82, 287 },
  196. { 83, 286 },
  197. { 84, 285 },
  198. { 85, 284 },
  199. { 86, 283 },
  200. { 88, 282 },
  201. { 89, 281 },
  202. { 256, 280 },
  203. };
  204. static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
  205. u16 *snr)
  206. {
  207. int i, ret = -EINVAL;
  208. dprintk("%s()\n", __func__);
  209. for (i = 0; i < sz; i++) {
  210. if (mse < tab[i].val) {
  211. *snr = tab[i].data;
  212. ret = 0;
  213. break;
  214. }
  215. }
  216. dprintk("%s() snr=%d\n", __func__, *snr);
  217. return ret;
  218. }
  219. static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
  220. {
  221. struct au8522_state *state = fe->demodulator_priv;
  222. u8 r0b5, r0b6, r0b7;
  223. char *ifmhz;
  224. switch (if_freq) {
  225. case AU8522_IF_3_25MHZ:
  226. ifmhz = "3.25";
  227. r0b5 = 0x00;
  228. r0b6 = 0x3d;
  229. r0b7 = 0xa0;
  230. break;
  231. case AU8522_IF_4MHZ:
  232. ifmhz = "4.00";
  233. r0b5 = 0x00;
  234. r0b6 = 0x4b;
  235. r0b7 = 0xd9;
  236. break;
  237. case AU8522_IF_6MHZ:
  238. ifmhz = "6.00";
  239. r0b5 = 0xfb;
  240. r0b6 = 0x8e;
  241. r0b7 = 0x39;
  242. break;
  243. default:
  244. dprintk("%s() IF Frequency not supported\n", __func__);
  245. return -EINVAL;
  246. }
  247. dprintk("%s() %s MHz\n", __func__, ifmhz);
  248. au8522_writereg(state, 0x00b5, r0b5);
  249. au8522_writereg(state, 0x00b6, r0b6);
  250. au8522_writereg(state, 0x00b7, r0b7);
  251. return 0;
  252. }
  253. /* VSB Modulation table */
  254. static struct {
  255. u16 reg;
  256. u16 data;
  257. } VSB_mod_tab[] = {
  258. { 0x0090, 0x84 },
  259. { 0x2005, 0x00 },
  260. { 0x0091, 0x80 },
  261. { 0x00a3, 0x0c },
  262. { 0x00a4, 0xe8 },
  263. { 0x0081, 0xc4 },
  264. { 0x00a5, 0x40 },
  265. { 0x00a7, 0x40 },
  266. { 0x00a6, 0x67 },
  267. { 0x0262, 0x20 },
  268. { 0x021c, 0x30 },
  269. { 0x00d8, 0x1a },
  270. { 0x0227, 0xa0 },
  271. { 0x0121, 0xff },
  272. { 0x00a8, 0xf0 },
  273. { 0x00a9, 0x05 },
  274. { 0x00aa, 0x77 },
  275. { 0x00ab, 0xf0 },
  276. { 0x00ac, 0x05 },
  277. { 0x00ad, 0x77 },
  278. { 0x00ae, 0x41 },
  279. { 0x00af, 0x66 },
  280. { 0x021b, 0xcc },
  281. { 0x021d, 0x80 },
  282. { 0x00a4, 0xe8 },
  283. { 0x0231, 0x13 },
  284. };
  285. /* QAM64 Modulation table */
  286. static struct {
  287. u16 reg;
  288. u16 data;
  289. } QAM64_mod_tab[] = {
  290. { 0x00a3, 0x09 },
  291. { 0x00a4, 0x00 },
  292. { 0x0081, 0xc4 },
  293. { 0x00a5, 0x40 },
  294. { 0x00aa, 0x77 },
  295. { 0x00ad, 0x77 },
  296. { 0x00a6, 0x67 },
  297. { 0x0262, 0x20 },
  298. { 0x021c, 0x30 },
  299. { 0x00b8, 0x3e },
  300. { 0x00b9, 0xf0 },
  301. { 0x00ba, 0x01 },
  302. { 0x00bb, 0x18 },
  303. { 0x00bc, 0x50 },
  304. { 0x00bd, 0x00 },
  305. { 0x00be, 0xea },
  306. { 0x00bf, 0xef },
  307. { 0x00c0, 0xfc },
  308. { 0x00c1, 0xbd },
  309. { 0x00c2, 0x1f },
  310. { 0x00c3, 0xfc },
  311. { 0x00c4, 0xdd },
  312. { 0x00c5, 0xaf },
  313. { 0x00c6, 0x00 },
  314. { 0x00c7, 0x38 },
  315. { 0x00c8, 0x30 },
  316. { 0x00c9, 0x05 },
  317. { 0x00ca, 0x4a },
  318. { 0x00cb, 0xd0 },
  319. { 0x00cc, 0x01 },
  320. { 0x00cd, 0xd9 },
  321. { 0x00ce, 0x6f },
  322. { 0x00cf, 0xf9 },
  323. { 0x00d0, 0x70 },
  324. { 0x00d1, 0xdf },
  325. { 0x00d2, 0xf7 },
  326. { 0x00d3, 0xc2 },
  327. { 0x00d4, 0xdf },
  328. { 0x00d5, 0x02 },
  329. { 0x00d6, 0x9a },
  330. { 0x00d7, 0xd0 },
  331. { 0x0250, 0x0d },
  332. { 0x0251, 0xcd },
  333. { 0x0252, 0xe0 },
  334. { 0x0253, 0x05 },
  335. { 0x0254, 0xa7 },
  336. { 0x0255, 0xff },
  337. { 0x0256, 0xed },
  338. { 0x0257, 0x5b },
  339. { 0x0258, 0xae },
  340. { 0x0259, 0xe6 },
  341. { 0x025a, 0x3d },
  342. { 0x025b, 0x0f },
  343. { 0x025c, 0x0d },
  344. { 0x025d, 0xea },
  345. { 0x025e, 0xf2 },
  346. { 0x025f, 0x51 },
  347. { 0x0260, 0xf5 },
  348. { 0x0261, 0x06 },
  349. { 0x021a, 0x00 },
  350. { 0x0546, 0x40 },
  351. { 0x0210, 0xc7 },
  352. { 0x0211, 0xaa },
  353. { 0x0212, 0xab },
  354. { 0x0213, 0x02 },
  355. { 0x0502, 0x00 },
  356. { 0x0121, 0x04 },
  357. { 0x0122, 0x04 },
  358. { 0x052e, 0x10 },
  359. { 0x00a4, 0xca },
  360. { 0x00a7, 0x40 },
  361. { 0x0526, 0x01 },
  362. };
  363. /* QAM256 Modulation table */
  364. static struct {
  365. u16 reg;
  366. u16 data;
  367. } QAM256_mod_tab[] = {
  368. { 0x00a3, 0x09 },
  369. { 0x00a4, 0x00 },
  370. { 0x0081, 0xc4 },
  371. { 0x00a5, 0x40 },
  372. { 0x00aa, 0x77 },
  373. { 0x00ad, 0x77 },
  374. { 0x00a6, 0x67 },
  375. { 0x0262, 0x20 },
  376. { 0x021c, 0x30 },
  377. { 0x00b8, 0x3e },
  378. { 0x00b9, 0xf0 },
  379. { 0x00ba, 0x01 },
  380. { 0x00bb, 0x18 },
  381. { 0x00bc, 0x50 },
  382. { 0x00bd, 0x00 },
  383. { 0x00be, 0xea },
  384. { 0x00bf, 0xef },
  385. { 0x00c0, 0xfc },
  386. { 0x00c1, 0xbd },
  387. { 0x00c2, 0x1f },
  388. { 0x00c3, 0xfc },
  389. { 0x00c4, 0xdd },
  390. { 0x00c5, 0xaf },
  391. { 0x00c6, 0x00 },
  392. { 0x00c7, 0x38 },
  393. { 0x00c8, 0x30 },
  394. { 0x00c9, 0x05 },
  395. { 0x00ca, 0x4a },
  396. { 0x00cb, 0xd0 },
  397. { 0x00cc, 0x01 },
  398. { 0x00cd, 0xd9 },
  399. { 0x00ce, 0x6f },
  400. { 0x00cf, 0xf9 },
  401. { 0x00d0, 0x70 },
  402. { 0x00d1, 0xdf },
  403. { 0x00d2, 0xf7 },
  404. { 0x00d3, 0xc2 },
  405. { 0x00d4, 0xdf },
  406. { 0x00d5, 0x02 },
  407. { 0x00d6, 0x9a },
  408. { 0x00d7, 0xd0 },
  409. { 0x0250, 0x0d },
  410. { 0x0251, 0xcd },
  411. { 0x0252, 0xe0 },
  412. { 0x0253, 0x05 },
  413. { 0x0254, 0xa7 },
  414. { 0x0255, 0xff },
  415. { 0x0256, 0xed },
  416. { 0x0257, 0x5b },
  417. { 0x0258, 0xae },
  418. { 0x0259, 0xe6 },
  419. { 0x025a, 0x3d },
  420. { 0x025b, 0x0f },
  421. { 0x025c, 0x0d },
  422. { 0x025d, 0xea },
  423. { 0x025e, 0xf2 },
  424. { 0x025f, 0x51 },
  425. { 0x0260, 0xf5 },
  426. { 0x0261, 0x06 },
  427. { 0x021a, 0x00 },
  428. { 0x0546, 0x40 },
  429. { 0x0210, 0x26 },
  430. { 0x0211, 0xf6 },
  431. { 0x0212, 0x84 },
  432. { 0x0213, 0x02 },
  433. { 0x0502, 0x01 },
  434. { 0x0121, 0x04 },
  435. { 0x0122, 0x04 },
  436. { 0x052e, 0x10 },
  437. { 0x00a4, 0xca },
  438. { 0x00a7, 0x40 },
  439. { 0x0526, 0x01 },
  440. };
  441. static struct {
  442. u16 reg;
  443. u16 data;
  444. } QAM256_mod_tab_zv_mode[] = {
  445. { 0x80a3, 0x09 },
  446. { 0x80a4, 0x00 },
  447. { 0x8081, 0xc4 },
  448. { 0x80a5, 0x40 },
  449. { 0x80b5, 0xfb },
  450. { 0x80b6, 0x8e },
  451. { 0x80b7, 0x39 },
  452. { 0x80aa, 0x77 },
  453. { 0x80ad, 0x77 },
  454. { 0x80a6, 0x67 },
  455. { 0x8262, 0x20 },
  456. { 0x821c, 0x30 },
  457. { 0x80b8, 0x3e },
  458. { 0x80b9, 0xf0 },
  459. { 0x80ba, 0x01 },
  460. { 0x80bb, 0x18 },
  461. { 0x80bc, 0x50 },
  462. { 0x80bd, 0x00 },
  463. { 0x80be, 0xea },
  464. { 0x80bf, 0xef },
  465. { 0x80c0, 0xfc },
  466. { 0x80c1, 0xbd },
  467. { 0x80c2, 0x1f },
  468. { 0x80c3, 0xfc },
  469. { 0x80c4, 0xdd },
  470. { 0x80c5, 0xaf },
  471. { 0x80c6, 0x00 },
  472. { 0x80c7, 0x38 },
  473. { 0x80c8, 0x30 },
  474. { 0x80c9, 0x05 },
  475. { 0x80ca, 0x4a },
  476. { 0x80cb, 0xd0 },
  477. { 0x80cc, 0x01 },
  478. { 0x80cd, 0xd9 },
  479. { 0x80ce, 0x6f },
  480. { 0x80cf, 0xf9 },
  481. { 0x80d0, 0x70 },
  482. { 0x80d1, 0xdf },
  483. { 0x80d2, 0xf7 },
  484. { 0x80d3, 0xc2 },
  485. { 0x80d4, 0xdf },
  486. { 0x80d5, 0x02 },
  487. { 0x80d6, 0x9a },
  488. { 0x80d7, 0xd0 },
  489. { 0x8250, 0x0d },
  490. { 0x8251, 0xcd },
  491. { 0x8252, 0xe0 },
  492. { 0x8253, 0x05 },
  493. { 0x8254, 0xa7 },
  494. { 0x8255, 0xff },
  495. { 0x8256, 0xed },
  496. { 0x8257, 0x5b },
  497. { 0x8258, 0xae },
  498. { 0x8259, 0xe6 },
  499. { 0x825a, 0x3d },
  500. { 0x825b, 0x0f },
  501. { 0x825c, 0x0d },
  502. { 0x825d, 0xea },
  503. { 0x825e, 0xf2 },
  504. { 0x825f, 0x51 },
  505. { 0x8260, 0xf5 },
  506. { 0x8261, 0x06 },
  507. { 0x821a, 0x01 },
  508. { 0x8546, 0x40 },
  509. { 0x8210, 0x26 },
  510. { 0x8211, 0xf6 },
  511. { 0x8212, 0x84 },
  512. { 0x8213, 0x02 },
  513. { 0x8502, 0x01 },
  514. { 0x8121, 0x04 },
  515. { 0x8122, 0x04 },
  516. { 0x852e, 0x10 },
  517. { 0x80a4, 0xca },
  518. { 0x80a7, 0x40 },
  519. { 0x8526, 0x01 },
  520. };
  521. static int au8522_enable_modulation(struct dvb_frontend *fe,
  522. enum fe_modulation m)
  523. {
  524. struct au8522_state *state = fe->demodulator_priv;
  525. int i;
  526. dprintk("%s(0x%08x)\n", __func__, m);
  527. switch (m) {
  528. case VSB_8:
  529. dprintk("%s() VSB_8\n", __func__);
  530. for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
  531. au8522_writereg(state,
  532. VSB_mod_tab[i].reg,
  533. VSB_mod_tab[i].data);
  534. au8522_set_if(fe, state->config.vsb_if);
  535. break;
  536. case QAM_64:
  537. dprintk("%s() QAM 64\n", __func__);
  538. for (i = 0; i < ARRAY_SIZE(QAM64_mod_tab); i++)
  539. au8522_writereg(state,
  540. QAM64_mod_tab[i].reg,
  541. QAM64_mod_tab[i].data);
  542. au8522_set_if(fe, state->config.qam_if);
  543. break;
  544. case QAM_256:
  545. if (zv_mode) {
  546. dprintk("%s() QAM 256 (zv_mode)\n", __func__);
  547. for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab_zv_mode); i++)
  548. au8522_writereg(state,
  549. QAM256_mod_tab_zv_mode[i].reg,
  550. QAM256_mod_tab_zv_mode[i].data);
  551. au8522_set_if(fe, state->config.qam_if);
  552. msleep(100);
  553. au8522_writereg(state, 0x821a, 0x00);
  554. } else {
  555. dprintk("%s() QAM 256\n", __func__);
  556. for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab); i++)
  557. au8522_writereg(state,
  558. QAM256_mod_tab[i].reg,
  559. QAM256_mod_tab[i].data);
  560. au8522_set_if(fe, state->config.qam_if);
  561. }
  562. break;
  563. default:
  564. dprintk("%s() Invalid modulation\n", __func__);
  565. return -EINVAL;
  566. }
  567. state->current_modulation = m;
  568. return 0;
  569. }
  570. /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
  571. static int au8522_set_frontend(struct dvb_frontend *fe)
  572. {
  573. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  574. struct au8522_state *state = fe->demodulator_priv;
  575. int ret = -EINVAL;
  576. dprintk("%s(frequency=%d)\n", __func__, c->frequency);
  577. if ((state->current_frequency == c->frequency) &&
  578. (state->current_modulation == c->modulation))
  579. return 0;
  580. if (fe->ops.tuner_ops.set_params) {
  581. if (fe->ops.i2c_gate_ctrl)
  582. fe->ops.i2c_gate_ctrl(fe, 1);
  583. ret = fe->ops.tuner_ops.set_params(fe);
  584. if (fe->ops.i2c_gate_ctrl)
  585. fe->ops.i2c_gate_ctrl(fe, 0);
  586. }
  587. if (ret < 0)
  588. return ret;
  589. /* Allow the tuner to settle */
  590. if (zv_mode) {
  591. dprintk("%s() increase tuner settling time for zv_mode\n",
  592. __func__);
  593. msleep(250);
  594. } else
  595. msleep(100);
  596. au8522_enable_modulation(fe, c->modulation);
  597. state->current_frequency = c->frequency;
  598. return 0;
  599. }
  600. static int au8522_read_status(struct dvb_frontend *fe, enum fe_status *status)
  601. {
  602. struct au8522_state *state = fe->demodulator_priv;
  603. u8 reg;
  604. u32 tuner_status = 0;
  605. *status = 0;
  606. if (state->current_modulation == VSB_8) {
  607. dprintk("%s() Checking VSB_8\n", __func__);
  608. reg = au8522_readreg(state, 0x0088);
  609. if ((reg & 0x03) == 0x03)
  610. *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
  611. } else {
  612. dprintk("%s() Checking QAM\n", __func__);
  613. reg = au8522_readreg(state, 0x0541);
  614. if (reg & 0x80)
  615. *status |= FE_HAS_VITERBI;
  616. if (reg & 0x20)
  617. *status |= FE_HAS_LOCK | FE_HAS_SYNC;
  618. }
  619. switch (state->config.status_mode) {
  620. case AU8522_DEMODLOCKING:
  621. dprintk("%s() DEMODLOCKING\n", __func__);
  622. if (*status & FE_HAS_VITERBI)
  623. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  624. break;
  625. case AU8522_TUNERLOCKING:
  626. /* Get the tuner status */
  627. dprintk("%s() TUNERLOCKING\n", __func__);
  628. if (fe->ops.tuner_ops.get_status) {
  629. if (fe->ops.i2c_gate_ctrl)
  630. fe->ops.i2c_gate_ctrl(fe, 1);
  631. fe->ops.tuner_ops.get_status(fe, &tuner_status);
  632. if (fe->ops.i2c_gate_ctrl)
  633. fe->ops.i2c_gate_ctrl(fe, 0);
  634. }
  635. if (tuner_status)
  636. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  637. break;
  638. }
  639. state->fe_status = *status;
  640. if (*status & FE_HAS_LOCK)
  641. /* turn on LED, if it isn't on already */
  642. au8522_led_ctrl(state, -1);
  643. else
  644. /* turn off LED */
  645. au8522_led_ctrl(state, 0);
  646. dprintk("%s() status 0x%08x\n", __func__, *status);
  647. return 0;
  648. }
  649. static int au8522_led_status(struct au8522_state *state, const u16 *snr)
  650. {
  651. struct au8522_led_config *led_config = state->config.led_cfg;
  652. int led;
  653. u16 strong;
  654. /* bail out if we can't control an LED */
  655. if (!led_config)
  656. return 0;
  657. if (0 == (state->fe_status & FE_HAS_LOCK))
  658. return au8522_led_ctrl(state, 0);
  659. else if (state->current_modulation == QAM_256)
  660. strong = led_config->qam256_strong;
  661. else if (state->current_modulation == QAM_64)
  662. strong = led_config->qam64_strong;
  663. else /* (state->current_modulation == VSB_8) */
  664. strong = led_config->vsb8_strong;
  665. if (*snr >= strong)
  666. led = 2;
  667. else
  668. led = 1;
  669. if ((state->led_state) &&
  670. (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
  671. /* snr didn't change enough to bother
  672. * changing the color of the led */
  673. return 0;
  674. return au8522_led_ctrl(state, led);
  675. }
  676. static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
  677. {
  678. struct au8522_state *state = fe->demodulator_priv;
  679. int ret = -EINVAL;
  680. dprintk("%s()\n", __func__);
  681. if (state->current_modulation == QAM_256)
  682. ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
  683. ARRAY_SIZE(qam256_mse2snr_tab),
  684. au8522_readreg(state, 0x0522),
  685. snr);
  686. else if (state->current_modulation == QAM_64)
  687. ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
  688. ARRAY_SIZE(qam64_mse2snr_tab),
  689. au8522_readreg(state, 0x0522),
  690. snr);
  691. else /* VSB_8 */
  692. ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
  693. ARRAY_SIZE(vsb_mse2snr_tab),
  694. au8522_readreg(state, 0x0311),
  695. snr);
  696. if (state->config.led_cfg)
  697. au8522_led_status(state, snr);
  698. return ret;
  699. }
  700. static int au8522_read_signal_strength(struct dvb_frontend *fe,
  701. u16 *signal_strength)
  702. {
  703. /* borrowed from lgdt330x.c
  704. *
  705. * Calculate strength from SNR up to 35dB
  706. * Even though the SNR can go higher than 35dB,
  707. * there is some comfort factor in having a range of
  708. * strong signals that can show at 100%
  709. */
  710. u16 snr;
  711. u32 tmp;
  712. int ret = au8522_read_snr(fe, &snr);
  713. *signal_strength = 0;
  714. if (0 == ret) {
  715. /* The following calculation method was chosen
  716. * purely for the sake of code re-use from the
  717. * other demod drivers that use this method */
  718. /* Convert from SNR in dB * 10 to 8.24 fixed-point */
  719. tmp = (snr * ((1 << 24) / 10));
  720. /* Convert from 8.24 fixed-point to
  721. * scale the range 0 - 35*2^24 into 0 - 65535*/
  722. if (tmp >= 8960 * 0x10000)
  723. *signal_strength = 0xffff;
  724. else
  725. *signal_strength = tmp / 8960;
  726. }
  727. return ret;
  728. }
  729. static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  730. {
  731. struct au8522_state *state = fe->demodulator_priv;
  732. if (state->current_modulation == VSB_8)
  733. *ucblocks = au8522_readreg(state, 0x0087);
  734. else
  735. *ucblocks = au8522_readreg(state, 0x0543);
  736. return 0;
  737. }
  738. static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
  739. {
  740. return au8522_read_ucblocks(fe, ber);
  741. }
  742. static int au8522_get_frontend(struct dvb_frontend *fe,
  743. struct dtv_frontend_properties *c)
  744. {
  745. struct au8522_state *state = fe->demodulator_priv;
  746. c->frequency = state->current_frequency;
  747. c->modulation = state->current_modulation;
  748. return 0;
  749. }
  750. static int au8522_get_tune_settings(struct dvb_frontend *fe,
  751. struct dvb_frontend_tune_settings *tune)
  752. {
  753. tune->min_delay_ms = 1000;
  754. return 0;
  755. }
  756. static const struct dvb_frontend_ops au8522_ops;
  757. static void au8522_release(struct dvb_frontend *fe)
  758. {
  759. struct au8522_state *state = fe->demodulator_priv;
  760. au8522_release_state(state);
  761. }
  762. struct dvb_frontend *au8522_attach(const struct au8522_config *config,
  763. struct i2c_adapter *i2c)
  764. {
  765. struct au8522_state *state = NULL;
  766. int instance;
  767. /* allocate memory for the internal state */
  768. instance = au8522_get_state(&state, i2c, config->demod_address);
  769. switch (instance) {
  770. case 0:
  771. dprintk("%s state allocation failed\n", __func__);
  772. break;
  773. case 1:
  774. /* new demod instance */
  775. dprintk("%s using new instance\n", __func__);
  776. break;
  777. default:
  778. /* existing demod instance */
  779. dprintk("%s using existing instance\n", __func__);
  780. break;
  781. }
  782. /* setup the state */
  783. state->config = *config;
  784. state->i2c = i2c;
  785. state->operational_mode = AU8522_DIGITAL_MODE;
  786. /* create dvb_frontend */
  787. memcpy(&state->frontend.ops, &au8522_ops,
  788. sizeof(struct dvb_frontend_ops));
  789. state->frontend.demodulator_priv = state;
  790. state->frontend.ops.analog_ops.i2c_gate_ctrl = au8522_analog_i2c_gate_ctrl;
  791. if (au8522_init(&state->frontend) != 0) {
  792. printk(KERN_ERR "%s: Failed to initialize correctly\n",
  793. __func__);
  794. goto error;
  795. }
  796. /* Note: Leaving the I2C gate open here. */
  797. au8522_i2c_gate_ctrl(&state->frontend, 1);
  798. return &state->frontend;
  799. error:
  800. au8522_release_state(state);
  801. return NULL;
  802. }
  803. EXPORT_SYMBOL(au8522_attach);
  804. static const struct dvb_frontend_ops au8522_ops = {
  805. .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
  806. .info = {
  807. .name = "Auvitek AU8522 QAM/8VSB Frontend",
  808. .frequency_min_hz = 54 * MHz,
  809. .frequency_max_hz = 858 * MHz,
  810. .frequency_stepsize_hz = 62500,
  811. .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
  812. },
  813. .init = au8522_init,
  814. .sleep = au8522_sleep,
  815. .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
  816. .set_frontend = au8522_set_frontend,
  817. .get_frontend = au8522_get_frontend,
  818. .get_tune_settings = au8522_get_tune_settings,
  819. .read_status = au8522_read_status,
  820. .read_ber = au8522_read_ber,
  821. .read_signal_strength = au8522_read_signal_strength,
  822. .read_snr = au8522_read_snr,
  823. .read_ucblocks = au8522_read_ucblocks,
  824. .release = au8522_release,
  825. };
  826. module_param(debug, int, 0644);
  827. MODULE_PARM_DESC(debug, "Enable verbose debug messages");
  828. module_param(zv_mode, int, 0644);
  829. MODULE_PARM_DESC(zv_mode, "Turn on/off ZeeVee modulator compatibility mode (default:on).\n"
  830. "\t\ton - modified AU8522 QAM256 initialization.\n"
  831. "\t\tProvides faster lock when using ZeeVee modulator based sources");
  832. MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
  833. MODULE_AUTHOR("Steven Toth");
  834. MODULE_LICENSE("GPL");