stv0297.c 17 KB

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