stv0297.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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 = stv0297_readreg(state, 0x55);
  114. tmp |= stv0297_readreg(state, 0x56) << 8;
  115. tmp |= stv0297_readreg(state, 0x57) << 16;
  116. tmp |= 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, fe_modulation_t modulation)
  189. {
  190. int val = 0;
  191. switch (modulation) {
  192. case QAM_16:
  193. val = 0;
  194. break;
  195. case QAM_32:
  196. val = 1;
  197. break;
  198. case QAM_64:
  199. val = 4;
  200. break;
  201. case QAM_128:
  202. val = 2;
  203. break;
  204. case QAM_256:
  205. val = 3;
  206. break;
  207. default:
  208. return -EINVAL;
  209. }
  210. stv0297_writereg_mask(state, 0x00, 0x70, val << 4);
  211. return 0;
  212. }
  213. static int stv0297_set_inversion(struct stv0297_state *state, fe_spectral_inversion_t inversion)
  214. {
  215. int val = 0;
  216. switch (inversion) {
  217. case INVERSION_OFF:
  218. val = 0;
  219. break;
  220. case INVERSION_ON:
  221. val = 1;
  222. break;
  223. default:
  224. return -EINVAL;
  225. }
  226. stv0297_writereg_mask(state, 0x83, 0x08, val << 3);
  227. return 0;
  228. }
  229. static int stv0297_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  230. {
  231. struct stv0297_state *state = fe->demodulator_priv;
  232. if (enable) {
  233. stv0297_writereg(state, 0x87, 0x78);
  234. stv0297_writereg(state, 0x86, 0xc8);
  235. }
  236. return 0;
  237. }
  238. static int stv0297_init(struct dvb_frontend *fe)
  239. {
  240. struct stv0297_state *state = fe->demodulator_priv;
  241. int i;
  242. /* load init table */
  243. for (i=0; !(state->config->inittab[i] == 0xff && state->config->inittab[i+1] == 0xff); i+=2)
  244. stv0297_writereg(state, state->config->inittab[i], state->config->inittab[i+1]);
  245. msleep(200);
  246. state->last_ber = 0;
  247. return 0;
  248. }
  249. static int stv0297_sleep(struct dvb_frontend *fe)
  250. {
  251. struct stv0297_state *state = fe->demodulator_priv;
  252. stv0297_writereg_mask(state, 0x80, 1, 1);
  253. return 0;
  254. }
  255. static int stv0297_read_status(struct dvb_frontend *fe, fe_status_t * status)
  256. {
  257. struct stv0297_state *state = fe->demodulator_priv;
  258. u8 sync = stv0297_readreg(state, 0xDF);
  259. *status = 0;
  260. if (sync & 0x80)
  261. *status |=
  262. FE_HAS_SYNC | FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_LOCK;
  263. return 0;
  264. }
  265. static int stv0297_read_ber(struct dvb_frontend *fe, u32 * ber)
  266. {
  267. struct stv0297_state *state = fe->demodulator_priv;
  268. u8 BER[3];
  269. stv0297_readregs(state, 0xA0, BER, 3);
  270. if (!(BER[0] & 0x80)) {
  271. state->last_ber = BER[2] << 8 | BER[1];
  272. stv0297_writereg_mask(state, 0xA0, 0x80, 0x80);
  273. }
  274. *ber = state->last_ber;
  275. return 0;
  276. }
  277. static int stv0297_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  278. {
  279. struct stv0297_state *state = fe->demodulator_priv;
  280. u8 STRENGTH[3];
  281. u16 tmp;
  282. stv0297_readregs(state, 0x41, STRENGTH, 3);
  283. tmp = (STRENGTH[1] & 0x03) << 8 | STRENGTH[0];
  284. if (STRENGTH[2] & 0x20) {
  285. if (tmp < 0x200)
  286. tmp = 0;
  287. else
  288. tmp = tmp - 0x200;
  289. } else {
  290. if (tmp > 0x1ff)
  291. tmp = 0;
  292. else
  293. tmp = 0x1ff - tmp;
  294. }
  295. *strength = (tmp << 7) | (tmp >> 2);
  296. return 0;
  297. }
  298. static int stv0297_read_snr(struct dvb_frontend *fe, u16 * snr)
  299. {
  300. struct stv0297_state *state = fe->demodulator_priv;
  301. u8 SNR[2];
  302. stv0297_readregs(state, 0x07, SNR, 2);
  303. *snr = SNR[1] << 8 | SNR[0];
  304. return 0;
  305. }
  306. static int stv0297_read_ucblocks(struct dvb_frontend *fe, u32 * ucblocks)
  307. {
  308. struct stv0297_state *state = fe->demodulator_priv;
  309. stv0297_writereg_mask(state, 0xDF, 0x03, 0x03); /* freeze the counters */
  310. *ucblocks = (stv0297_readreg(state, 0xD5) << 8)
  311. | stv0297_readreg(state, 0xD4);
  312. stv0297_writereg_mask(state, 0xDF, 0x03, 0x02); /* clear the counters */
  313. stv0297_writereg_mask(state, 0xDF, 0x03, 0x01); /* re-enable the counters */
  314. return 0;
  315. }
  316. static int stv0297_set_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
  317. {
  318. struct stv0297_state *state = fe->demodulator_priv;
  319. int u_threshold;
  320. int initial_u;
  321. int blind_u;
  322. int delay;
  323. int sweeprate;
  324. int carrieroffset;
  325. unsigned long starttime;
  326. unsigned long timeout;
  327. fe_spectral_inversion_t inversion;
  328. switch (p->u.qam.modulation) {
  329. case QAM_16:
  330. case QAM_32:
  331. case QAM_64:
  332. delay = 100;
  333. sweeprate = 1000;
  334. break;
  335. case QAM_128:
  336. case QAM_256:
  337. delay = 200;
  338. sweeprate = 500;
  339. break;
  340. default:
  341. return -EINVAL;
  342. }
  343. // determine inversion dependent parameters
  344. inversion = p->inversion;
  345. if (state->config->invert)
  346. inversion = (inversion == INVERSION_ON) ? INVERSION_OFF : INVERSION_ON;
  347. carrieroffset = -330;
  348. switch (inversion) {
  349. case INVERSION_OFF:
  350. break;
  351. case INVERSION_ON:
  352. sweeprate = -sweeprate;
  353. carrieroffset = -carrieroffset;
  354. break;
  355. default:
  356. return -EINVAL;
  357. }
  358. stv0297_init(fe);
  359. if (fe->ops.tuner_ops.set_params) {
  360. fe->ops.tuner_ops.set_params(fe, p);
  361. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  362. }
  363. /* clear software interrupts */
  364. stv0297_writereg(state, 0x82, 0x0);
  365. /* set initial demodulation frequency */
  366. stv0297_set_initialdemodfreq(state, 7250);
  367. /* setup AGC */
  368. stv0297_writereg_mask(state, 0x43, 0x10, 0x00);
  369. stv0297_writereg(state, 0x41, 0x00);
  370. stv0297_writereg_mask(state, 0x42, 0x03, 0x01);
  371. stv0297_writereg_mask(state, 0x36, 0x60, 0x00);
  372. stv0297_writereg_mask(state, 0x36, 0x18, 0x00);
  373. stv0297_writereg_mask(state, 0x71, 0x80, 0x80);
  374. stv0297_writereg(state, 0x72, 0x00);
  375. stv0297_writereg(state, 0x73, 0x00);
  376. stv0297_writereg_mask(state, 0x74, 0x0F, 0x00);
  377. stv0297_writereg_mask(state, 0x43, 0x08, 0x00);
  378. stv0297_writereg_mask(state, 0x71, 0x80, 0x00);
  379. /* setup STL */
  380. stv0297_writereg_mask(state, 0x5a, 0x20, 0x20);
  381. stv0297_writereg_mask(state, 0x5b, 0x02, 0x02);
  382. stv0297_writereg_mask(state, 0x5b, 0x02, 0x00);
  383. stv0297_writereg_mask(state, 0x5b, 0x01, 0x00);
  384. stv0297_writereg_mask(state, 0x5a, 0x40, 0x40);
  385. /* disable frequency sweep */
  386. stv0297_writereg_mask(state, 0x6a, 0x01, 0x00);
  387. /* reset deinterleaver */
  388. stv0297_writereg_mask(state, 0x81, 0x01, 0x01);
  389. stv0297_writereg_mask(state, 0x81, 0x01, 0x00);
  390. /* ??? */
  391. stv0297_writereg_mask(state, 0x83, 0x20, 0x20);
  392. stv0297_writereg_mask(state, 0x83, 0x20, 0x00);
  393. /* reset equaliser */
  394. u_threshold = stv0297_readreg(state, 0x00) & 0xf;
  395. initial_u = stv0297_readreg(state, 0x01) >> 4;
  396. blind_u = stv0297_readreg(state, 0x01) & 0xf;
  397. stv0297_writereg_mask(state, 0x84, 0x01, 0x01);
  398. stv0297_writereg_mask(state, 0x84, 0x01, 0x00);
  399. stv0297_writereg_mask(state, 0x00, 0x0f, u_threshold);
  400. stv0297_writereg_mask(state, 0x01, 0xf0, initial_u << 4);
  401. stv0297_writereg_mask(state, 0x01, 0x0f, blind_u);
  402. /* data comes from internal A/D */
  403. stv0297_writereg_mask(state, 0x87, 0x80, 0x00);
  404. /* clear phase registers */
  405. stv0297_writereg(state, 0x63, 0x00);
  406. stv0297_writereg(state, 0x64, 0x00);
  407. stv0297_writereg(state, 0x65, 0x00);
  408. stv0297_writereg(state, 0x66, 0x00);
  409. stv0297_writereg(state, 0x67, 0x00);
  410. stv0297_writereg(state, 0x68, 0x00);
  411. stv0297_writereg_mask(state, 0x69, 0x0f, 0x00);
  412. /* set parameters */
  413. stv0297_set_qam(state, p->u.qam.modulation);
  414. stv0297_set_symbolrate(state, p->u.qam.symbol_rate / 1000);
  415. stv0297_set_sweeprate(state, sweeprate, p->u.qam.symbol_rate / 1000);
  416. stv0297_set_carrieroffset(state, carrieroffset);
  417. stv0297_set_inversion(state, inversion);
  418. /* kick off lock */
  419. /* Disable corner detection for higher QAMs */
  420. if (p->u.qam.modulation == QAM_128 ||
  421. p->u.qam.modulation == QAM_256)
  422. stv0297_writereg_mask(state, 0x88, 0x08, 0x00);
  423. else
  424. stv0297_writereg_mask(state, 0x88, 0x08, 0x08);
  425. stv0297_writereg_mask(state, 0x5a, 0x20, 0x00);
  426. stv0297_writereg_mask(state, 0x6a, 0x01, 0x01);
  427. stv0297_writereg_mask(state, 0x43, 0x40, 0x40);
  428. stv0297_writereg_mask(state, 0x5b, 0x30, 0x00);
  429. stv0297_writereg_mask(state, 0x03, 0x0c, 0x0c);
  430. stv0297_writereg_mask(state, 0x03, 0x03, 0x03);
  431. stv0297_writereg_mask(state, 0x43, 0x10, 0x10);
  432. /* wait for WGAGC lock */
  433. starttime = jiffies;
  434. timeout = jiffies + msecs_to_jiffies(2000);
  435. while (time_before(jiffies, timeout)) {
  436. msleep(10);
  437. if (stv0297_readreg(state, 0x43) & 0x08)
  438. break;
  439. }
  440. if (time_after(jiffies, timeout)) {
  441. goto timeout;
  442. }
  443. msleep(20);
  444. /* wait for equaliser partial convergence */
  445. timeout = jiffies + msecs_to_jiffies(500);
  446. while (time_before(jiffies, timeout)) {
  447. msleep(10);
  448. if (stv0297_readreg(state, 0x82) & 0x04) {
  449. break;
  450. }
  451. }
  452. if (time_after(jiffies, timeout)) {
  453. goto timeout;
  454. }
  455. /* wait for equaliser full convergence */
  456. timeout = jiffies + msecs_to_jiffies(delay);
  457. while (time_before(jiffies, timeout)) {
  458. msleep(10);
  459. if (stv0297_readreg(state, 0x82) & 0x08) {
  460. break;
  461. }
  462. }
  463. if (time_after(jiffies, timeout)) {
  464. goto timeout;
  465. }
  466. /* disable sweep */
  467. stv0297_writereg_mask(state, 0x6a, 1, 0);
  468. stv0297_writereg_mask(state, 0x88, 8, 0);
  469. /* wait for main lock */
  470. timeout = jiffies + msecs_to_jiffies(20);
  471. while (time_before(jiffies, timeout)) {
  472. msleep(10);
  473. if (stv0297_readreg(state, 0xDF) & 0x80) {
  474. break;
  475. }
  476. }
  477. if (time_after(jiffies, timeout)) {
  478. goto timeout;
  479. }
  480. msleep(100);
  481. /* is it still locked after that delay? */
  482. if (!(stv0297_readreg(state, 0xDF) & 0x80)) {
  483. goto timeout;
  484. }
  485. /* success!! */
  486. stv0297_writereg_mask(state, 0x5a, 0x40, 0x00);
  487. state->base_freq = p->frequency;
  488. return 0;
  489. timeout:
  490. stv0297_writereg_mask(state, 0x6a, 0x01, 0x00);
  491. return 0;
  492. }
  493. static int stv0297_get_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
  494. {
  495. struct stv0297_state *state = fe->demodulator_priv;
  496. int reg_00, reg_83;
  497. reg_00 = stv0297_readreg(state, 0x00);
  498. reg_83 = stv0297_readreg(state, 0x83);
  499. p->frequency = state->base_freq;
  500. p->inversion = (reg_83 & 0x08) ? INVERSION_ON : INVERSION_OFF;
  501. if (state->config->invert)
  502. p->inversion = (p->inversion == INVERSION_ON) ? INVERSION_OFF : INVERSION_ON;
  503. p->u.qam.symbol_rate = stv0297_get_symbolrate(state) * 1000;
  504. p->u.qam.fec_inner = FEC_NONE;
  505. switch ((reg_00 >> 4) & 0x7) {
  506. case 0:
  507. p->u.qam.modulation = QAM_16;
  508. break;
  509. case 1:
  510. p->u.qam.modulation = QAM_32;
  511. break;
  512. case 2:
  513. p->u.qam.modulation = QAM_128;
  514. break;
  515. case 3:
  516. p->u.qam.modulation = QAM_256;
  517. break;
  518. case 4:
  519. p->u.qam.modulation = QAM_64;
  520. break;
  521. }
  522. return 0;
  523. }
  524. static void stv0297_release(struct dvb_frontend *fe)
  525. {
  526. struct stv0297_state *state = fe->demodulator_priv;
  527. kfree(state);
  528. }
  529. static struct dvb_frontend_ops stv0297_ops;
  530. struct dvb_frontend *stv0297_attach(const struct stv0297_config *config,
  531. struct i2c_adapter *i2c)
  532. {
  533. struct stv0297_state *state = NULL;
  534. /* allocate memory for the internal state */
  535. state = kzalloc(sizeof(struct stv0297_state), GFP_KERNEL);
  536. if (state == NULL)
  537. goto error;
  538. /* setup the state */
  539. state->config = config;
  540. state->i2c = i2c;
  541. state->last_ber = 0;
  542. state->base_freq = 0;
  543. /* check if the demod is there */
  544. if ((stv0297_readreg(state, 0x80) & 0x70) != 0x20)
  545. goto error;
  546. /* create dvb_frontend */
  547. memcpy(&state->frontend.ops, &stv0297_ops, sizeof(struct dvb_frontend_ops));
  548. state->frontend.demodulator_priv = state;
  549. return &state->frontend;
  550. error:
  551. kfree(state);
  552. return NULL;
  553. }
  554. static struct dvb_frontend_ops stv0297_ops = {
  555. .info = {
  556. .name = "ST STV0297 DVB-C",
  557. .type = FE_QAM,
  558. .frequency_min = 47000000,
  559. .frequency_max = 862000000,
  560. .frequency_stepsize = 62500,
  561. .symbol_rate_min = 870000,
  562. .symbol_rate_max = 11700000,
  563. .caps = FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
  564. FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_FEC_AUTO},
  565. .release = stv0297_release,
  566. .init = stv0297_init,
  567. .sleep = stv0297_sleep,
  568. .i2c_gate_ctrl = stv0297_i2c_gate_ctrl,
  569. .set_frontend = stv0297_set_frontend,
  570. .get_frontend = stv0297_get_frontend,
  571. .read_status = stv0297_read_status,
  572. .read_ber = stv0297_read_ber,
  573. .read_signal_strength = stv0297_read_signal_strength,
  574. .read_snr = stv0297_read_snr,
  575. .read_ucblocks = stv0297_read_ucblocks,
  576. };
  577. MODULE_DESCRIPTION("ST STV0297 DVB-C Demodulator driver");
  578. MODULE_AUTHOR("Dennis Noermann and Andrew de Quincey");
  579. MODULE_LICENSE("GPL");
  580. EXPORT_SYMBOL(stv0297_attach);