au8522.c 19 KB

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