au8522_dig.c 19 KB

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