au8522_dig.c 21 KB

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