au8522_dig.c 21 KB

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