au8522_dig.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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. #define dprintk(arg...) do { \
  30. 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. /* QAM Modulation table */
  338. static struct {
  339. u16 reg;
  340. u16 data;
  341. } QAM_mod_tab[] = {
  342. { 0x80a3, 0x09 },
  343. { 0x80a4, 0x00 },
  344. { 0x8081, 0xc4 },
  345. { 0x80a5, 0x40 },
  346. { 0x80aa, 0x77 },
  347. { 0x80ad, 0x77 },
  348. { 0x80a6, 0x67 },
  349. { 0x8262, 0x20 },
  350. { 0x821c, 0x30 },
  351. { 0x80b8, 0x3e },
  352. { 0x80b9, 0xf0 },
  353. { 0x80ba, 0x01 },
  354. { 0x80bb, 0x18 },
  355. { 0x80bc, 0x50 },
  356. { 0x80bd, 0x00 },
  357. { 0x80be, 0xea },
  358. { 0x80bf, 0xef },
  359. { 0x80c0, 0xfc },
  360. { 0x80c1, 0xbd },
  361. { 0x80c2, 0x1f },
  362. { 0x80c3, 0xfc },
  363. { 0x80c4, 0xdd },
  364. { 0x80c5, 0xaf },
  365. { 0x80c6, 0x00 },
  366. { 0x80c7, 0x38 },
  367. { 0x80c8, 0x30 },
  368. { 0x80c9, 0x05 },
  369. { 0x80ca, 0x4a },
  370. { 0x80cb, 0xd0 },
  371. { 0x80cc, 0x01 },
  372. { 0x80cd, 0xd9 },
  373. { 0x80ce, 0x6f },
  374. { 0x80cf, 0xf9 },
  375. { 0x80d0, 0x70 },
  376. { 0x80d1, 0xdf },
  377. { 0x80d2, 0xf7 },
  378. { 0x80d3, 0xc2 },
  379. { 0x80d4, 0xdf },
  380. { 0x80d5, 0x02 },
  381. { 0x80d6, 0x9a },
  382. { 0x80d7, 0xd0 },
  383. { 0x8250, 0x0d },
  384. { 0x8251, 0xcd },
  385. { 0x8252, 0xe0 },
  386. { 0x8253, 0x05 },
  387. { 0x8254, 0xa7 },
  388. { 0x8255, 0xff },
  389. { 0x8256, 0xed },
  390. { 0x8257, 0x5b },
  391. { 0x8258, 0xae },
  392. { 0x8259, 0xe6 },
  393. { 0x825a, 0x3d },
  394. { 0x825b, 0x0f },
  395. { 0x825c, 0x0d },
  396. { 0x825d, 0xea },
  397. { 0x825e, 0xf2 },
  398. { 0x825f, 0x51 },
  399. { 0x8260, 0xf5 },
  400. { 0x8261, 0x06 },
  401. { 0x821a, 0x00 },
  402. { 0x8546, 0x40 },
  403. { 0x8210, 0x26 },
  404. { 0x8211, 0xf6 },
  405. { 0x8212, 0x84 },
  406. { 0x8213, 0x02 },
  407. { 0x8502, 0x01 },
  408. { 0x8121, 0x04 },
  409. { 0x8122, 0x04 },
  410. { 0x852e, 0x10 },
  411. { 0x80a4, 0xca },
  412. { 0x80a7, 0x40 },
  413. { 0x8526, 0x01 },
  414. };
  415. static int au8522_enable_modulation(struct dvb_frontend *fe,
  416. fe_modulation_t m)
  417. {
  418. struct au8522_state *state = fe->demodulator_priv;
  419. int i;
  420. dprintk("%s(0x%08x)\n", __func__, m);
  421. switch (m) {
  422. case VSB_8:
  423. dprintk("%s() VSB_8\n", __func__);
  424. for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
  425. au8522_writereg(state,
  426. VSB_mod_tab[i].reg,
  427. VSB_mod_tab[i].data);
  428. au8522_set_if(fe, state->config->vsb_if);
  429. break;
  430. case QAM_64:
  431. case QAM_256:
  432. dprintk("%s() QAM 64/256\n", __func__);
  433. for (i = 0; i < ARRAY_SIZE(QAM_mod_tab); i++)
  434. au8522_writereg(state,
  435. QAM_mod_tab[i].reg,
  436. QAM_mod_tab[i].data);
  437. au8522_set_if(fe, state->config->qam_if);
  438. break;
  439. default:
  440. dprintk("%s() Invalid modulation\n", __func__);
  441. return -EINVAL;
  442. }
  443. state->current_modulation = m;
  444. return 0;
  445. }
  446. /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
  447. static int au8522_set_frontend(struct dvb_frontend *fe,
  448. struct dvb_frontend_parameters *p)
  449. {
  450. struct au8522_state *state = fe->demodulator_priv;
  451. int ret = -EINVAL;
  452. dprintk("%s(frequency=%d)\n", __func__, p->frequency);
  453. if ((state->current_frequency == p->frequency) &&
  454. (state->current_modulation == p->u.vsb.modulation))
  455. return 0;
  456. au8522_enable_modulation(fe, p->u.vsb.modulation);
  457. /* Allow the demod to settle */
  458. msleep(100);
  459. if (fe->ops.tuner_ops.set_params) {
  460. if (fe->ops.i2c_gate_ctrl)
  461. fe->ops.i2c_gate_ctrl(fe, 1);
  462. ret = fe->ops.tuner_ops.set_params(fe, p);
  463. if (fe->ops.i2c_gate_ctrl)
  464. fe->ops.i2c_gate_ctrl(fe, 0);
  465. }
  466. if (ret < 0)
  467. return ret;
  468. state->current_frequency = p->frequency;
  469. return 0;
  470. }
  471. /* Reset the demod hardware and reset all of the configuration registers
  472. to a default state. */
  473. int au8522_init(struct dvb_frontend *fe)
  474. {
  475. struct au8522_state *state = fe->demodulator_priv;
  476. dprintk("%s()\n", __func__);
  477. au8522_writereg(state, 0xa4, 1 << 5);
  478. au8522_i2c_gate_ctrl(fe, 1);
  479. return 0;
  480. }
  481. static int au8522_led_gpio_enable(struct au8522_state *state, int onoff)
  482. {
  483. struct au8522_led_config *led_config = state->config->led_cfg;
  484. u8 val;
  485. /* bail out if we cant control an LED */
  486. if (!led_config || !led_config->gpio_output ||
  487. !led_config->gpio_output_enable || !led_config->gpio_output_disable)
  488. return 0;
  489. val = au8522_readreg(state, 0x4000 |
  490. (led_config->gpio_output & ~0xc000));
  491. if (onoff) {
  492. /* enable GPIO output */
  493. val &= ~((led_config->gpio_output_enable >> 8) & 0xff);
  494. val |= (led_config->gpio_output_enable & 0xff);
  495. } else {
  496. /* disable GPIO output */
  497. val &= ~((led_config->gpio_output_disable >> 8) & 0xff);
  498. val |= (led_config->gpio_output_disable & 0xff);
  499. }
  500. return au8522_writereg(state, 0x8000 |
  501. (led_config->gpio_output & ~0xc000), val);
  502. }
  503. /* led = 0 | off
  504. * led = 1 | signal ok
  505. * led = 2 | signal strong
  506. * led < 0 | only light led if leds are currently off
  507. */
  508. static int au8522_led_ctrl(struct au8522_state *state, int led)
  509. {
  510. struct au8522_led_config *led_config = state->config->led_cfg;
  511. int i, ret = 0;
  512. /* bail out if we cant control an LED */
  513. if (!led_config || !led_config->gpio_leds ||
  514. !led_config->num_led_states || !led_config->led_states)
  515. return 0;
  516. if (led < 0) {
  517. /* if LED is already lit, then leave it as-is */
  518. if (state->led_state)
  519. return 0;
  520. else
  521. led *= -1;
  522. }
  523. /* toggle LED if changing state */
  524. if (state->led_state != led) {
  525. u8 val;
  526. dprintk("%s: %d\n", __func__, led);
  527. au8522_led_gpio_enable(state, 1);
  528. val = au8522_readreg(state, 0x4000 |
  529. (led_config->gpio_leds & ~0xc000));
  530. /* start with all leds off */
  531. for (i = 0; i < led_config->num_led_states; i++)
  532. val &= ~led_config->led_states[i];
  533. /* set selected LED state */
  534. if (led < led_config->num_led_states)
  535. val |= led_config->led_states[led];
  536. else if (led_config->num_led_states)
  537. val |=
  538. led_config->led_states[led_config->num_led_states - 1];
  539. ret = au8522_writereg(state, 0x8000 |
  540. (led_config->gpio_leds & ~0xc000), val);
  541. if (ret < 0)
  542. return ret;
  543. state->led_state = led;
  544. if (led == 0)
  545. au8522_led_gpio_enable(state, 0);
  546. }
  547. return 0;
  548. }
  549. int au8522_sleep(struct dvb_frontend *fe)
  550. {
  551. struct au8522_state *state = fe->demodulator_priv;
  552. dprintk("%s()\n", __func__);
  553. /* turn off led */
  554. au8522_led_ctrl(state, 0);
  555. /* Power down the chip */
  556. au8522_writereg(state, 0xa4, 1 << 5);
  557. state->current_frequency = 0;
  558. return 0;
  559. }
  560. static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
  561. {
  562. struct au8522_state *state = fe->demodulator_priv;
  563. u8 reg;
  564. u32 tuner_status = 0;
  565. *status = 0;
  566. if (state->current_modulation == VSB_8) {
  567. dprintk("%s() Checking VSB_8\n", __func__);
  568. reg = au8522_readreg(state, 0x4088);
  569. if ((reg & 0x03) == 0x03)
  570. *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
  571. } else {
  572. dprintk("%s() Checking QAM\n", __func__);
  573. reg = au8522_readreg(state, 0x4541);
  574. if (reg & 0x80)
  575. *status |= FE_HAS_VITERBI;
  576. if (reg & 0x20)
  577. *status |= FE_HAS_LOCK | FE_HAS_SYNC;
  578. }
  579. switch (state->config->status_mode) {
  580. case AU8522_DEMODLOCKING:
  581. dprintk("%s() DEMODLOCKING\n", __func__);
  582. if (*status & FE_HAS_VITERBI)
  583. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  584. break;
  585. case AU8522_TUNERLOCKING:
  586. /* Get the tuner status */
  587. dprintk("%s() TUNERLOCKING\n", __func__);
  588. if (fe->ops.tuner_ops.get_status) {
  589. if (fe->ops.i2c_gate_ctrl)
  590. fe->ops.i2c_gate_ctrl(fe, 1);
  591. fe->ops.tuner_ops.get_status(fe, &tuner_status);
  592. if (fe->ops.i2c_gate_ctrl)
  593. fe->ops.i2c_gate_ctrl(fe, 0);
  594. }
  595. if (tuner_status)
  596. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  597. break;
  598. }
  599. state->fe_status = *status;
  600. if (*status & FE_HAS_LOCK)
  601. /* turn on LED, if it isn't on already */
  602. au8522_led_ctrl(state, -1);
  603. else
  604. /* turn off LED */
  605. au8522_led_ctrl(state, 0);
  606. dprintk("%s() status 0x%08x\n", __func__, *status);
  607. return 0;
  608. }
  609. static int au8522_led_status(struct au8522_state *state, const u16 *snr)
  610. {
  611. struct au8522_led_config *led_config = state->config->led_cfg;
  612. int led;
  613. u16 strong;
  614. /* bail out if we cant control an LED */
  615. if (!led_config)
  616. return 0;
  617. if (0 == (state->fe_status & FE_HAS_LOCK))
  618. return au8522_led_ctrl(state, 0);
  619. else if (state->current_modulation == QAM_256)
  620. strong = led_config->qam256_strong;
  621. else if (state->current_modulation == QAM_64)
  622. strong = led_config->qam64_strong;
  623. else /* (state->current_modulation == VSB_8) */
  624. strong = led_config->vsb8_strong;
  625. if (*snr >= strong)
  626. led = 2;
  627. else
  628. led = 1;
  629. if ((state->led_state) &&
  630. (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
  631. /* snr didn't change enough to bother
  632. * changing the color of the led */
  633. return 0;
  634. return au8522_led_ctrl(state, led);
  635. }
  636. static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
  637. {
  638. struct au8522_state *state = fe->demodulator_priv;
  639. int ret = -EINVAL;
  640. dprintk("%s()\n", __func__);
  641. if (state->current_modulation == QAM_256)
  642. ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
  643. ARRAY_SIZE(qam256_mse2snr_tab),
  644. au8522_readreg(state, 0x4522),
  645. snr);
  646. else if (state->current_modulation == QAM_64)
  647. ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
  648. ARRAY_SIZE(qam64_mse2snr_tab),
  649. au8522_readreg(state, 0x4522),
  650. snr);
  651. else /* VSB_8 */
  652. ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
  653. ARRAY_SIZE(vsb_mse2snr_tab),
  654. au8522_readreg(state, 0x4311),
  655. snr);
  656. if (state->config->led_cfg)
  657. au8522_led_status(state, snr);
  658. return ret;
  659. }
  660. static int au8522_read_signal_strength(struct dvb_frontend *fe,
  661. u16 *signal_strength)
  662. {
  663. return au8522_read_snr(fe, signal_strength);
  664. }
  665. static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  666. {
  667. struct au8522_state *state = fe->demodulator_priv;
  668. if (state->current_modulation == VSB_8)
  669. *ucblocks = au8522_readreg(state, 0x4087);
  670. else
  671. *ucblocks = au8522_readreg(state, 0x4543);
  672. return 0;
  673. }
  674. static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
  675. {
  676. return au8522_read_ucblocks(fe, ber);
  677. }
  678. static int au8522_get_frontend(struct dvb_frontend *fe,
  679. struct dvb_frontend_parameters *p)
  680. {
  681. struct au8522_state *state = fe->demodulator_priv;
  682. p->frequency = state->current_frequency;
  683. p->u.vsb.modulation = state->current_modulation;
  684. return 0;
  685. }
  686. static int au8522_get_tune_settings(struct dvb_frontend *fe,
  687. struct dvb_frontend_tune_settings *tune)
  688. {
  689. tune->min_delay_ms = 1000;
  690. return 0;
  691. }
  692. static struct dvb_frontend_ops au8522_ops;
  693. int au8522_get_state(struct au8522_state **state, struct i2c_adapter *i2c,
  694. u8 client_address)
  695. {
  696. return hybrid_tuner_request_state(struct au8522_state, (*state),
  697. hybrid_tuner_instance_list,
  698. i2c, client_address, "au8522");
  699. }
  700. void au8522_release_state(struct au8522_state *state)
  701. {
  702. if (state != NULL)
  703. hybrid_tuner_release_state(state);
  704. }
  705. static void au8522_release(struct dvb_frontend *fe)
  706. {
  707. struct au8522_state *state = fe->demodulator_priv;
  708. au8522_release_state(state);
  709. }
  710. struct dvb_frontend *au8522_attach(const struct au8522_config *config,
  711. struct i2c_adapter *i2c)
  712. {
  713. struct au8522_state *state = NULL;
  714. int instance;
  715. /* allocate memory for the internal state */
  716. instance = au8522_get_state(&state, i2c, config->demod_address);
  717. switch (instance) {
  718. case 0:
  719. dprintk("%s state allocation failed\n", __func__);
  720. break;
  721. case 1:
  722. /* new demod instance */
  723. dprintk("%s using new instance\n", __func__);
  724. break;
  725. default:
  726. /* existing demod instance */
  727. dprintk("%s using existing instance\n", __func__);
  728. break;
  729. }
  730. /* setup the state */
  731. state->config = config;
  732. state->i2c = i2c;
  733. /* create dvb_frontend */
  734. memcpy(&state->frontend.ops, &au8522_ops,
  735. sizeof(struct dvb_frontend_ops));
  736. state->frontend.demodulator_priv = state;
  737. if (au8522_init(&state->frontend) != 0) {
  738. printk(KERN_ERR "%s: Failed to initialize correctly\n",
  739. __func__);
  740. goto error;
  741. }
  742. /* Note: Leaving the I2C gate open here. */
  743. au8522_i2c_gate_ctrl(&state->frontend, 1);
  744. return &state->frontend;
  745. error:
  746. au8522_release_state(state);
  747. return NULL;
  748. }
  749. EXPORT_SYMBOL(au8522_attach);
  750. static struct dvb_frontend_ops au8522_ops = {
  751. .info = {
  752. .name = "Auvitek AU8522 QAM/8VSB Frontend",
  753. .type = FE_ATSC,
  754. .frequency_min = 54000000,
  755. .frequency_max = 858000000,
  756. .frequency_stepsize = 62500,
  757. .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
  758. },
  759. .init = au8522_init,
  760. .sleep = au8522_sleep,
  761. .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
  762. .set_frontend = au8522_set_frontend,
  763. .get_frontend = au8522_get_frontend,
  764. .get_tune_settings = au8522_get_tune_settings,
  765. .read_status = au8522_read_status,
  766. .read_ber = au8522_read_ber,
  767. .read_signal_strength = au8522_read_signal_strength,
  768. .read_snr = au8522_read_snr,
  769. .read_ucblocks = au8522_read_ucblocks,
  770. .release = au8522_release,
  771. };
  772. module_param(debug, int, 0644);
  773. MODULE_PARM_DESC(debug, "Enable verbose debug messages");
  774. MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
  775. MODULE_AUTHOR("Steven Toth");
  776. MODULE_LICENSE("GPL");