stv0297.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. Driver for STV0297 demodulator
  3. Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net>
  4. Copyright (C) 2003-2004 Dennis Noermann <dennis.noermann@noernet.de>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/string.h>
  21. #include <linux/delay.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/slab.h>
  24. #include "dvb_frontend.h"
  25. #include "stv0297.h"
  26. struct stv0297_state {
  27. struct i2c_adapter *i2c;
  28. const struct stv0297_config *config;
  29. struct dvb_frontend frontend;
  30. unsigned long last_ber;
  31. unsigned long base_freq;
  32. };
  33. #if 1
  34. #define dprintk(x...) printk(x)
  35. #else
  36. #define dprintk(x...)
  37. #endif
  38. #define STV0297_CLOCK_KHZ 28900
  39. static int stv0297_writereg(struct stv0297_state *state, u8 reg, u8 data)
  40. {
  41. int ret;
  42. u8 buf[] = { reg, data };
  43. struct i2c_msg msg = {.addr = state->config->demod_address,.flags = 0,.buf = buf,.len = 2 };
  44. ret = i2c_transfer(state->i2c, &msg, 1);
  45. if (ret != 1)
  46. dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
  47. "ret == %i)\n", __func__, reg, data, ret);
  48. return (ret != 1) ? -1 : 0;
  49. }
  50. static int stv0297_readreg(struct stv0297_state *state, u8 reg)
  51. {
  52. int ret;
  53. u8 b0[] = { reg };
  54. u8 b1[] = { 0 };
  55. struct i2c_msg msg[] = { {.addr = state->config->demod_address,.flags = 0,.buf = b0,.len = 1},
  56. {.addr = state->config->demod_address,.flags = I2C_M_RD,.buf = b1,.len = 1}
  57. };
  58. // this device needs a STOP between the register and data
  59. if (state->config->stop_during_read) {
  60. if ((ret = i2c_transfer(state->i2c, &msg[0], 1)) != 1) {
  61. dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__, reg, ret);
  62. return -1;
  63. }
  64. if ((ret = i2c_transfer(state->i2c, &msg[1], 1)) != 1) {
  65. dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__, reg, ret);
  66. return -1;
  67. }
  68. } else {
  69. if ((ret = i2c_transfer(state->i2c, msg, 2)) != 2) {
  70. dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__, reg, ret);
  71. return -1;
  72. }
  73. }
  74. return b1[0];
  75. }
  76. static int stv0297_writereg_mask(struct stv0297_state *state, u8 reg, u8 mask, u8 data)
  77. {
  78. int val;
  79. val = stv0297_readreg(state, reg);
  80. val &= ~mask;
  81. val |= (data & mask);
  82. stv0297_writereg(state, reg, val);
  83. return 0;
  84. }
  85. static int stv0297_readregs(struct stv0297_state *state, u8 reg1, u8 * b, u8 len)
  86. {
  87. int ret;
  88. struct i2c_msg msg[] = { {.addr = state->config->demod_address,.flags = 0,.buf =
  89. &reg1,.len = 1},
  90. {.addr = state->config->demod_address,.flags = I2C_M_RD,.buf = b,.len = len}
  91. };
  92. // this device needs a STOP between the register and data
  93. if (state->config->stop_during_read) {
  94. if ((ret = i2c_transfer(state->i2c, &msg[0], 1)) != 1) {
  95. dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__, reg1, ret);
  96. return -1;
  97. }
  98. if ((ret = i2c_transfer(state->i2c, &msg[1], 1)) != 1) {
  99. dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__, reg1, ret);
  100. return -1;
  101. }
  102. } else {
  103. if ((ret = i2c_transfer(state->i2c, msg, 2)) != 2) {
  104. dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__, reg1, ret);
  105. return -1;
  106. }
  107. }
  108. return 0;
  109. }
  110. static u32 stv0297_get_symbolrate(struct stv0297_state *state)
  111. {
  112. u64 tmp;
  113. tmp = (u64)(stv0297_readreg(state, 0x55)
  114. | (stv0297_readreg(state, 0x56) << 8)
  115. | (stv0297_readreg(state, 0x57) << 16)
  116. | (stv0297_readreg(state, 0x58) << 24));
  117. tmp *= STV0297_CLOCK_KHZ;
  118. tmp >>= 32;
  119. return (u32) tmp;
  120. }
  121. static void stv0297_set_symbolrate(struct stv0297_state *state, u32 srate)
  122. {
  123. long tmp;
  124. tmp = 131072L * srate; /* 131072 = 2^17 */
  125. tmp = tmp / (STV0297_CLOCK_KHZ / 4); /* 1/4 = 2^-2 */
  126. tmp = tmp * 8192L; /* 8192 = 2^13 */
  127. stv0297_writereg(state, 0x55, (unsigned char) (tmp & 0xFF));
  128. stv0297_writereg(state, 0x56, (unsigned char) (tmp >> 8));
  129. stv0297_writereg(state, 0x57, (unsigned char) (tmp >> 16));
  130. stv0297_writereg(state, 0x58, (unsigned char) (tmp >> 24));
  131. }
  132. static void stv0297_set_sweeprate(struct stv0297_state *state, short fshift, long symrate)
  133. {
  134. long tmp;
  135. tmp = (long) fshift *262144L; /* 262144 = 2*18 */
  136. tmp /= symrate;
  137. tmp *= 1024; /* 1024 = 2*10 */
  138. // adjust
  139. if (tmp >= 0) {
  140. tmp += 500000;
  141. } else {
  142. tmp -= 500000;
  143. }
  144. tmp /= 1000000;
  145. stv0297_writereg(state, 0x60, tmp & 0xFF);
  146. stv0297_writereg_mask(state, 0x69, 0xF0, (tmp >> 4) & 0xf0);
  147. }
  148. static void stv0297_set_carrieroffset(struct stv0297_state *state, long offset)
  149. {
  150. long tmp;
  151. /* symrate is hardcoded to 10000 */
  152. tmp = offset * 26844L; /* (2**28)/10000 */
  153. if (tmp < 0)
  154. tmp += 0x10000000;
  155. tmp &= 0x0FFFFFFF;
  156. stv0297_writereg(state, 0x66, (unsigned char) (tmp & 0xFF));
  157. stv0297_writereg(state, 0x67, (unsigned char) (tmp >> 8));
  158. stv0297_writereg(state, 0x68, (unsigned char) (tmp >> 16));
  159. stv0297_writereg_mask(state, 0x69, 0x0F, (tmp >> 24) & 0x0f);
  160. }
  161. /*
  162. static long stv0297_get_carrieroffset(struct stv0297_state *state)
  163. {
  164. s64 tmp;
  165. stv0297_writereg(state, 0x6B, 0x00);
  166. tmp = stv0297_readreg(state, 0x66);
  167. tmp |= (stv0297_readreg(state, 0x67) << 8);
  168. tmp |= (stv0297_readreg(state, 0x68) << 16);
  169. tmp |= (stv0297_readreg(state, 0x69) & 0x0F) << 24;
  170. tmp *= stv0297_get_symbolrate(state);
  171. tmp >>= 28;
  172. return (s32) tmp;
  173. }
  174. */
  175. static void stv0297_set_initialdemodfreq(struct stv0297_state *state, long freq)
  176. {
  177. s32 tmp;
  178. if (freq > 10000)
  179. freq -= STV0297_CLOCK_KHZ;
  180. tmp = (STV0297_CLOCK_KHZ * 1000) / (1 << 16);
  181. tmp = (freq * 1000) / tmp;
  182. if (tmp > 0xffff)
  183. tmp = 0xffff;
  184. stv0297_writereg_mask(state, 0x25, 0x80, 0x80);
  185. stv0297_writereg(state, 0x21, tmp >> 8);
  186. stv0297_writereg(state, 0x20, tmp);
  187. }
  188. static int stv0297_set_qam(struct stv0297_state *state,
  189. enum fe_modulation modulation)
  190. {
  191. int val = 0;
  192. switch (modulation) {
  193. case QAM_16:
  194. val = 0;
  195. break;
  196. case QAM_32:
  197. val = 1;
  198. break;
  199. case QAM_64:
  200. val = 4;
  201. break;
  202. case QAM_128:
  203. val = 2;
  204. break;
  205. case QAM_256:
  206. val = 3;
  207. break;
  208. default:
  209. return -EINVAL;
  210. }
  211. stv0297_writereg_mask(state, 0x00, 0x70, val << 4);
  212. return 0;
  213. }
  214. static int stv0297_set_inversion(struct stv0297_state *state,
  215. enum fe_spectral_inversion inversion)
  216. {
  217. int val = 0;
  218. switch (inversion) {
  219. case INVERSION_OFF:
  220. val = 0;
  221. break;
  222. case INVERSION_ON:
  223. val = 1;
  224. break;
  225. default:
  226. return -EINVAL;
  227. }
  228. stv0297_writereg_mask(state, 0x83, 0x08, val << 3);
  229. return 0;
  230. }
  231. static int stv0297_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  232. {
  233. struct stv0297_state *state = fe->demodulator_priv;
  234. if (enable) {
  235. stv0297_writereg(state, 0x87, 0x78);
  236. stv0297_writereg(state, 0x86, 0xc8);
  237. }
  238. return 0;
  239. }
  240. static int stv0297_init(struct dvb_frontend *fe)
  241. {
  242. struct stv0297_state *state = fe->demodulator_priv;
  243. int i;
  244. /* load init table */
  245. for (i=0; !(state->config->inittab[i] == 0xff && state->config->inittab[i+1] == 0xff); i+=2)
  246. stv0297_writereg(state, state->config->inittab[i], state->config->inittab[i+1]);
  247. msleep(200);
  248. state->last_ber = 0;
  249. return 0;
  250. }
  251. static int stv0297_sleep(struct dvb_frontend *fe)
  252. {
  253. struct stv0297_state *state = fe->demodulator_priv;
  254. stv0297_writereg_mask(state, 0x80, 1, 1);
  255. return 0;
  256. }
  257. static int stv0297_read_status(struct dvb_frontend *fe,
  258. enum fe_status *status)
  259. {
  260. struct stv0297_state *state = fe->demodulator_priv;
  261. u8 sync = stv0297_readreg(state, 0xDF);
  262. *status = 0;
  263. if (sync & 0x80)
  264. *status |=
  265. FE_HAS_SYNC | FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_LOCK;
  266. return 0;
  267. }
  268. static int stv0297_read_ber(struct dvb_frontend *fe, u32 * ber)
  269. {
  270. struct stv0297_state *state = fe->demodulator_priv;
  271. u8 BER[3];
  272. stv0297_readregs(state, 0xA0, BER, 3);
  273. if (!(BER[0] & 0x80)) {
  274. state->last_ber = BER[2] << 8 | BER[1];
  275. stv0297_writereg_mask(state, 0xA0, 0x80, 0x80);
  276. }
  277. *ber = state->last_ber;
  278. return 0;
  279. }
  280. static int stv0297_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  281. {
  282. struct stv0297_state *state = fe->demodulator_priv;
  283. u8 STRENGTH[3];
  284. u16 tmp;
  285. stv0297_readregs(state, 0x41, STRENGTH, 3);
  286. tmp = (STRENGTH[1] & 0x03) << 8 | STRENGTH[0];
  287. if (STRENGTH[2] & 0x20) {
  288. if (tmp < 0x200)
  289. tmp = 0;
  290. else
  291. tmp = tmp - 0x200;
  292. } else {
  293. if (tmp > 0x1ff)
  294. tmp = 0;
  295. else
  296. tmp = 0x1ff - tmp;
  297. }
  298. *strength = (tmp << 7) | (tmp >> 2);
  299. return 0;
  300. }
  301. static int stv0297_read_snr(struct dvb_frontend *fe, u16 * snr)
  302. {
  303. struct stv0297_state *state = fe->demodulator_priv;
  304. u8 SNR[2];
  305. stv0297_readregs(state, 0x07, SNR, 2);
  306. *snr = SNR[1] << 8 | SNR[0];
  307. return 0;
  308. }
  309. static int stv0297_read_ucblocks(struct dvb_frontend *fe, u32 * ucblocks)
  310. {
  311. struct stv0297_state *state = fe->demodulator_priv;
  312. stv0297_writereg_mask(state, 0xDF, 0x03, 0x03); /* freeze the counters */
  313. *ucblocks = (stv0297_readreg(state, 0xD5) << 8)
  314. | stv0297_readreg(state, 0xD4);
  315. stv0297_writereg_mask(state, 0xDF, 0x03, 0x02); /* clear the counters */
  316. stv0297_writereg_mask(state, 0xDF, 0x03, 0x01); /* re-enable the counters */
  317. return 0;
  318. }
  319. static int stv0297_set_frontend(struct dvb_frontend *fe)
  320. {
  321. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  322. struct stv0297_state *state = fe->demodulator_priv;
  323. int u_threshold;
  324. int initial_u;
  325. int blind_u;
  326. int delay;
  327. int sweeprate;
  328. int carrieroffset;
  329. unsigned long timeout;
  330. enum fe_spectral_inversion inversion;
  331. switch (p->modulation) {
  332. case QAM_16:
  333. case QAM_32:
  334. case QAM_64:
  335. delay = 100;
  336. sweeprate = 1000;
  337. break;
  338. case QAM_128:
  339. case QAM_256:
  340. delay = 200;
  341. sweeprate = 500;
  342. break;
  343. default:
  344. return -EINVAL;
  345. }
  346. // determine inversion dependent parameters
  347. inversion = p->inversion;
  348. if (state->config->invert)
  349. inversion = (inversion == INVERSION_ON) ? INVERSION_OFF : INVERSION_ON;
  350. carrieroffset = -330;
  351. switch (inversion) {
  352. case INVERSION_OFF:
  353. break;
  354. case INVERSION_ON:
  355. sweeprate = -sweeprate;
  356. carrieroffset = -carrieroffset;
  357. break;
  358. default:
  359. return -EINVAL;
  360. }
  361. stv0297_init(fe);
  362. if (fe->ops.tuner_ops.set_params) {
  363. fe->ops.tuner_ops.set_params(fe);
  364. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  365. }
  366. /* clear software interrupts */
  367. stv0297_writereg(state, 0x82, 0x0);
  368. /* set initial demodulation frequency */
  369. stv0297_set_initialdemodfreq(state, 7250);
  370. /* setup AGC */
  371. stv0297_writereg_mask(state, 0x43, 0x10, 0x00);
  372. stv0297_writereg(state, 0x41, 0x00);
  373. stv0297_writereg_mask(state, 0x42, 0x03, 0x01);
  374. stv0297_writereg_mask(state, 0x36, 0x60, 0x00);
  375. stv0297_writereg_mask(state, 0x36, 0x18, 0x00);
  376. stv0297_writereg_mask(state, 0x71, 0x80, 0x80);
  377. stv0297_writereg(state, 0x72, 0x00);
  378. stv0297_writereg(state, 0x73, 0x00);
  379. stv0297_writereg_mask(state, 0x74, 0x0F, 0x00);
  380. stv0297_writereg_mask(state, 0x43, 0x08, 0x00);
  381. stv0297_writereg_mask(state, 0x71, 0x80, 0x00);
  382. /* setup STL */
  383. stv0297_writereg_mask(state, 0x5a, 0x20, 0x20);
  384. stv0297_writereg_mask(state, 0x5b, 0x02, 0x02);
  385. stv0297_writereg_mask(state, 0x5b, 0x02, 0x00);
  386. stv0297_writereg_mask(state, 0x5b, 0x01, 0x00);
  387. stv0297_writereg_mask(state, 0x5a, 0x40, 0x40);
  388. /* disable frequency sweep */
  389. stv0297_writereg_mask(state, 0x6a, 0x01, 0x00);
  390. /* reset deinterleaver */
  391. stv0297_writereg_mask(state, 0x81, 0x01, 0x01);
  392. stv0297_writereg_mask(state, 0x81, 0x01, 0x00);
  393. /* ??? */
  394. stv0297_writereg_mask(state, 0x83, 0x20, 0x20);
  395. stv0297_writereg_mask(state, 0x83, 0x20, 0x00);
  396. /* reset equaliser */
  397. u_threshold = stv0297_readreg(state, 0x00) & 0xf;
  398. initial_u = stv0297_readreg(state, 0x01) >> 4;
  399. blind_u = stv0297_readreg(state, 0x01) & 0xf;
  400. stv0297_writereg_mask(state, 0x84, 0x01, 0x01);
  401. stv0297_writereg_mask(state, 0x84, 0x01, 0x00);
  402. stv0297_writereg_mask(state, 0x00, 0x0f, u_threshold);
  403. stv0297_writereg_mask(state, 0x01, 0xf0, initial_u << 4);
  404. stv0297_writereg_mask(state, 0x01, 0x0f, blind_u);
  405. /* data comes from internal A/D */
  406. stv0297_writereg_mask(state, 0x87, 0x80, 0x00);
  407. /* clear phase registers */
  408. stv0297_writereg(state, 0x63, 0x00);
  409. stv0297_writereg(state, 0x64, 0x00);
  410. stv0297_writereg(state, 0x65, 0x00);
  411. stv0297_writereg(state, 0x66, 0x00);
  412. stv0297_writereg(state, 0x67, 0x00);
  413. stv0297_writereg(state, 0x68, 0x00);
  414. stv0297_writereg_mask(state, 0x69, 0x0f, 0x00);
  415. /* set parameters */
  416. stv0297_set_qam(state, p->modulation);
  417. stv0297_set_symbolrate(state, p->symbol_rate / 1000);
  418. stv0297_set_sweeprate(state, sweeprate, p->symbol_rate / 1000);
  419. stv0297_set_carrieroffset(state, carrieroffset);
  420. stv0297_set_inversion(state, inversion);
  421. /* kick off lock */
  422. /* Disable corner detection for higher QAMs */
  423. if (p->modulation == QAM_128 ||
  424. p->modulation == QAM_256)
  425. stv0297_writereg_mask(state, 0x88, 0x08, 0x00);
  426. else
  427. stv0297_writereg_mask(state, 0x88, 0x08, 0x08);
  428. stv0297_writereg_mask(state, 0x5a, 0x20, 0x00);
  429. stv0297_writereg_mask(state, 0x6a, 0x01, 0x01);
  430. stv0297_writereg_mask(state, 0x43, 0x40, 0x40);
  431. stv0297_writereg_mask(state, 0x5b, 0x30, 0x00);
  432. stv0297_writereg_mask(state, 0x03, 0x0c, 0x0c);
  433. stv0297_writereg_mask(state, 0x03, 0x03, 0x03);
  434. stv0297_writereg_mask(state, 0x43, 0x10, 0x10);
  435. /* wait for WGAGC lock */
  436. timeout = jiffies + msecs_to_jiffies(2000);
  437. while (time_before(jiffies, timeout)) {
  438. msleep(10);
  439. if (stv0297_readreg(state, 0x43) & 0x08)
  440. break;
  441. }
  442. if (time_after(jiffies, timeout)) {
  443. goto timeout;
  444. }
  445. msleep(20);
  446. /* wait for equaliser partial convergence */
  447. timeout = jiffies + msecs_to_jiffies(500);
  448. while (time_before(jiffies, timeout)) {
  449. msleep(10);
  450. if (stv0297_readreg(state, 0x82) & 0x04) {
  451. break;
  452. }
  453. }
  454. if (time_after(jiffies, timeout)) {
  455. goto timeout;
  456. }
  457. /* wait for equaliser full convergence */
  458. timeout = jiffies + msecs_to_jiffies(delay);
  459. while (time_before(jiffies, timeout)) {
  460. msleep(10);
  461. if (stv0297_readreg(state, 0x82) & 0x08) {
  462. break;
  463. }
  464. }
  465. if (time_after(jiffies, timeout)) {
  466. goto timeout;
  467. }
  468. /* disable sweep */
  469. stv0297_writereg_mask(state, 0x6a, 1, 0);
  470. stv0297_writereg_mask(state, 0x88, 8, 0);
  471. /* wait for main lock */
  472. timeout = jiffies + msecs_to_jiffies(20);
  473. while (time_before(jiffies, timeout)) {
  474. msleep(10);
  475. if (stv0297_readreg(state, 0xDF) & 0x80) {
  476. break;
  477. }
  478. }
  479. if (time_after(jiffies, timeout)) {
  480. goto timeout;
  481. }
  482. msleep(100);
  483. /* is it still locked after that delay? */
  484. if (!(stv0297_readreg(state, 0xDF) & 0x80)) {
  485. goto timeout;
  486. }
  487. /* success!! */
  488. stv0297_writereg_mask(state, 0x5a, 0x40, 0x00);
  489. state->base_freq = p->frequency;
  490. return 0;
  491. timeout:
  492. stv0297_writereg_mask(state, 0x6a, 0x01, 0x00);
  493. return 0;
  494. }
  495. static int stv0297_get_frontend(struct dvb_frontend *fe)
  496. {
  497. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  498. struct stv0297_state *state = fe->demodulator_priv;
  499. int reg_00, reg_83;
  500. reg_00 = stv0297_readreg(state, 0x00);
  501. reg_83 = stv0297_readreg(state, 0x83);
  502. p->frequency = state->base_freq;
  503. p->inversion = (reg_83 & 0x08) ? INVERSION_ON : INVERSION_OFF;
  504. if (state->config->invert)
  505. p->inversion = (p->inversion == INVERSION_ON) ? INVERSION_OFF : INVERSION_ON;
  506. p->symbol_rate = stv0297_get_symbolrate(state) * 1000;
  507. p->fec_inner = FEC_NONE;
  508. switch ((reg_00 >> 4) & 0x7) {
  509. case 0:
  510. p->modulation = QAM_16;
  511. break;
  512. case 1:
  513. p->modulation = QAM_32;
  514. break;
  515. case 2:
  516. p->modulation = QAM_128;
  517. break;
  518. case 3:
  519. p->modulation = QAM_256;
  520. break;
  521. case 4:
  522. p->modulation = QAM_64;
  523. break;
  524. }
  525. return 0;
  526. }
  527. static void stv0297_release(struct dvb_frontend *fe)
  528. {
  529. struct stv0297_state *state = fe->demodulator_priv;
  530. kfree(state);
  531. }
  532. static struct dvb_frontend_ops stv0297_ops;
  533. struct dvb_frontend *stv0297_attach(const struct stv0297_config *config,
  534. struct i2c_adapter *i2c)
  535. {
  536. struct stv0297_state *state = NULL;
  537. /* allocate memory for the internal state */
  538. state = kzalloc(sizeof(struct stv0297_state), GFP_KERNEL);
  539. if (state == NULL)
  540. goto error;
  541. /* setup the state */
  542. state->config = config;
  543. state->i2c = i2c;
  544. state->last_ber = 0;
  545. state->base_freq = 0;
  546. /* check if the demod is there */
  547. if ((stv0297_readreg(state, 0x80) & 0x70) != 0x20)
  548. goto error;
  549. /* create dvb_frontend */
  550. memcpy(&state->frontend.ops, &stv0297_ops, sizeof(struct dvb_frontend_ops));
  551. state->frontend.demodulator_priv = state;
  552. return &state->frontend;
  553. error:
  554. kfree(state);
  555. return NULL;
  556. }
  557. static struct dvb_frontend_ops stv0297_ops = {
  558. .delsys = { SYS_DVBC_ANNEX_A },
  559. .info = {
  560. .name = "ST STV0297 DVB-C",
  561. .frequency_min = 47000000,
  562. .frequency_max = 862000000,
  563. .frequency_stepsize = 62500,
  564. .symbol_rate_min = 870000,
  565. .symbol_rate_max = 11700000,
  566. .caps = FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
  567. FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_FEC_AUTO},
  568. .release = stv0297_release,
  569. .init = stv0297_init,
  570. .sleep = stv0297_sleep,
  571. .i2c_gate_ctrl = stv0297_i2c_gate_ctrl,
  572. .set_frontend = stv0297_set_frontend,
  573. .get_frontend = stv0297_get_frontend,
  574. .read_status = stv0297_read_status,
  575. .read_ber = stv0297_read_ber,
  576. .read_signal_strength = stv0297_read_signal_strength,
  577. .read_snr = stv0297_read_snr,
  578. .read_ucblocks = stv0297_read_ucblocks,
  579. };
  580. MODULE_DESCRIPTION("ST STV0297 DVB-C Demodulator driver");
  581. MODULE_AUTHOR("Dennis Noermann and Andrew de Quincey");
  582. MODULE_LICENSE("GPL");
  583. EXPORT_SYMBOL(stv0297_attach);