au8522_dig.c 22 KB

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