stv0299.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. Driver for ST STV0299 demodulator
  3. Copyright (C) 2001-2002 Convergence Integrated Media GmbH
  4. <ralph@convergence.de>,
  5. <holger@convergence.de>,
  6. <js@convergence.de>
  7. Philips SU1278/SH
  8. Copyright (C) 2002 by Peter Schildmann <peter.schildmann@web.de>
  9. LG TDQF-S001F
  10. Copyright (C) 2002 Felix Domke <tmbinc@elitedvb.net>
  11. & Andreas Oberritter <obi@linuxtv.org>
  12. Support for Samsung TBMU24112IMB used on Technisat SkyStar2 rev. 2.6B
  13. Copyright (C) 2003 Vadim Catana <skystar@moldova.cc>:
  14. Support for Philips SU1278 on Technotrend hardware
  15. Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net>
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; either version 2 of the License, or
  19. (at your option) any later version.
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. GNU General Public License for more details.
  24. You should have received a copy of the GNU General Public License
  25. along with this program; if not, write to the Free Software
  26. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. */
  28. #include <linux/init.h>
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/string.h>
  33. #include <linux/slab.h>
  34. #include <linux/jiffies.h>
  35. #include <asm/div64.h>
  36. #include "dvb_frontend.h"
  37. #include "stv0299.h"
  38. struct stv0299_state {
  39. struct i2c_adapter* i2c;
  40. struct dvb_frontend_ops ops;
  41. const struct stv0299_config* config;
  42. struct dvb_frontend frontend;
  43. u8 initialised:1;
  44. u32 tuner_frequency;
  45. u32 symbol_rate;
  46. fe_code_rate_t fec_inner;
  47. int errmode;
  48. };
  49. #define STATUS_BER 0
  50. #define STATUS_UCBLOCKS 1
  51. static int debug;
  52. static int debug_legacy_dish_switch;
  53. #define dprintk(args...) \
  54. do { \
  55. if (debug) printk(KERN_DEBUG "stv0299: " args); \
  56. } while (0)
  57. static int stv0299_writeregI (struct stv0299_state* state, u8 reg, u8 data)
  58. {
  59. int ret;
  60. u8 buf [] = { reg, data };
  61. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
  62. ret = i2c_transfer (state->i2c, &msg, 1);
  63. if (ret != 1)
  64. dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
  65. "ret == %i)\n", __FUNCTION__, reg, data, ret);
  66. return (ret != 1) ? -EREMOTEIO : 0;
  67. }
  68. int stv0299_writereg (struct dvb_frontend* fe, u8 reg, u8 data)
  69. {
  70. struct stv0299_state* state = fe->demodulator_priv;
  71. return stv0299_writeregI(state, reg, data);
  72. }
  73. static u8 stv0299_readreg (struct stv0299_state* state, u8 reg)
  74. {
  75. int ret;
  76. u8 b0 [] = { reg };
  77. u8 b1 [] = { 0 };
  78. struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
  79. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
  80. ret = i2c_transfer (state->i2c, msg, 2);
  81. if (ret != 2)
  82. dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
  83. __FUNCTION__, reg, ret);
  84. return b1[0];
  85. }
  86. static int stv0299_readregs (struct stv0299_state* state, u8 reg1, u8 *b, u8 len)
  87. {
  88. int ret;
  89. struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = &reg1, .len = 1 },
  90. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
  91. ret = i2c_transfer (state->i2c, msg, 2);
  92. if (ret != 2)
  93. dprintk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
  94. return ret == 2 ? 0 : ret;
  95. }
  96. int stv0299_enable_plli2c (struct dvb_frontend* fe)
  97. {
  98. struct stv0299_state* state = fe->demodulator_priv;
  99. return stv0299_writeregI(state, 0x05, 0xb5); /* enable i2c repeater on stv0299 */
  100. }
  101. static int stv0299_set_FEC (struct stv0299_state* state, fe_code_rate_t fec)
  102. {
  103. dprintk ("%s\n", __FUNCTION__);
  104. switch (fec) {
  105. case FEC_AUTO:
  106. {
  107. return stv0299_writeregI (state, 0x31, 0x1f);
  108. }
  109. case FEC_1_2:
  110. {
  111. return stv0299_writeregI (state, 0x31, 0x01);
  112. }
  113. case FEC_2_3:
  114. {
  115. return stv0299_writeregI (state, 0x31, 0x02);
  116. }
  117. case FEC_3_4:
  118. {
  119. return stv0299_writeregI (state, 0x31, 0x04);
  120. }
  121. case FEC_5_6:
  122. {
  123. return stv0299_writeregI (state, 0x31, 0x08);
  124. }
  125. case FEC_7_8:
  126. {
  127. return stv0299_writeregI (state, 0x31, 0x10);
  128. }
  129. default:
  130. {
  131. return -EINVAL;
  132. }
  133. }
  134. }
  135. static fe_code_rate_t stv0299_get_fec (struct stv0299_state* state)
  136. {
  137. static fe_code_rate_t fec_tab [] = { FEC_2_3, FEC_3_4, FEC_5_6,
  138. FEC_7_8, FEC_1_2 };
  139. u8 index;
  140. dprintk ("%s\n", __FUNCTION__);
  141. index = stv0299_readreg (state, 0x1b);
  142. index &= 0x7;
  143. if (index > 4)
  144. return FEC_AUTO;
  145. return fec_tab [index];
  146. }
  147. static int stv0299_wait_diseqc_fifo (struct stv0299_state* state, int timeout)
  148. {
  149. unsigned long start = jiffies;
  150. dprintk ("%s\n", __FUNCTION__);
  151. while (stv0299_readreg(state, 0x0a) & 1) {
  152. if (jiffies - start > timeout) {
  153. dprintk ("%s: timeout!!\n", __FUNCTION__);
  154. return -ETIMEDOUT;
  155. }
  156. msleep(10);
  157. };
  158. return 0;
  159. }
  160. static int stv0299_wait_diseqc_idle (struct stv0299_state* state, int timeout)
  161. {
  162. unsigned long start = jiffies;
  163. dprintk ("%s\n", __FUNCTION__);
  164. while ((stv0299_readreg(state, 0x0a) & 3) != 2 ) {
  165. if (jiffies - start > timeout) {
  166. dprintk ("%s: timeout!!\n", __FUNCTION__);
  167. return -ETIMEDOUT;
  168. }
  169. msleep(10);
  170. };
  171. return 0;
  172. }
  173. static int stv0299_set_symbolrate (struct dvb_frontend* fe, u32 srate)
  174. {
  175. struct stv0299_state* state = fe->demodulator_priv;
  176. u64 big = srate;
  177. u32 ratio;
  178. // check rate is within limits
  179. if ((srate < 1000000) || (srate > 45000000)) return -EINVAL;
  180. // calculate value to program
  181. big = big << 20;
  182. big += (state->config->mclk-1); // round correctly
  183. do_div(big, state->config->mclk);
  184. ratio = big << 4;
  185. return state->config->set_symbol_rate(fe, srate, ratio);
  186. }
  187. static int stv0299_get_symbolrate (struct stv0299_state* state)
  188. {
  189. u32 Mclk = state->config->mclk / 4096L;
  190. u32 srate;
  191. s32 offset;
  192. u8 sfr[3];
  193. s8 rtf;
  194. dprintk ("%s\n", __FUNCTION__);
  195. stv0299_readregs (state, 0x1f, sfr, 3);
  196. stv0299_readregs (state, 0x1a, &rtf, 1);
  197. srate = (sfr[0] << 8) | sfr[1];
  198. srate *= Mclk;
  199. srate /= 16;
  200. srate += (sfr[2] >> 4) * Mclk / 256;
  201. offset = (s32) rtf * (srate / 4096L);
  202. offset /= 128;
  203. dprintk ("%s : srate = %i\n", __FUNCTION__, srate);
  204. dprintk ("%s : ofset = %i\n", __FUNCTION__, offset);
  205. srate += offset;
  206. srate += 1000;
  207. srate /= 2000;
  208. srate *= 2000;
  209. return srate;
  210. }
  211. static int stv0299_send_diseqc_msg (struct dvb_frontend* fe,
  212. struct dvb_diseqc_master_cmd *m)
  213. {
  214. struct stv0299_state* state = fe->demodulator_priv;
  215. u8 val;
  216. int i;
  217. dprintk ("%s\n", __FUNCTION__);
  218. if (stv0299_wait_diseqc_idle (state, 100) < 0)
  219. return -ETIMEDOUT;
  220. val = stv0299_readreg (state, 0x08);
  221. if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x6)) /* DiSEqC mode */
  222. return -EREMOTEIO;
  223. for (i=0; i<m->msg_len; i++) {
  224. if (stv0299_wait_diseqc_fifo (state, 100) < 0)
  225. return -ETIMEDOUT;
  226. if (stv0299_writeregI (state, 0x09, m->msg[i]))
  227. return -EREMOTEIO;
  228. }
  229. if (stv0299_wait_diseqc_idle (state, 100) < 0)
  230. return -ETIMEDOUT;
  231. return 0;
  232. }
  233. static int stv0299_send_diseqc_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
  234. {
  235. struct stv0299_state* state = fe->demodulator_priv;
  236. u8 val;
  237. dprintk ("%s\n", __FUNCTION__);
  238. if (stv0299_wait_diseqc_idle (state, 100) < 0)
  239. return -ETIMEDOUT;
  240. val = stv0299_readreg (state, 0x08);
  241. if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x2)) /* burst mode */
  242. return -EREMOTEIO;
  243. if (stv0299_writeregI (state, 0x09, burst == SEC_MINI_A ? 0x00 : 0xff))
  244. return -EREMOTEIO;
  245. if (stv0299_wait_diseqc_idle (state, 100) < 0)
  246. return -ETIMEDOUT;
  247. if (stv0299_writeregI (state, 0x08, val))
  248. return -EREMOTEIO;
  249. return 0;
  250. }
  251. static int stv0299_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  252. {
  253. struct stv0299_state* state = fe->demodulator_priv;
  254. u8 val;
  255. if (stv0299_wait_diseqc_idle (state, 100) < 0)
  256. return -ETIMEDOUT;
  257. val = stv0299_readreg (state, 0x08);
  258. switch (tone) {
  259. case SEC_TONE_ON:
  260. return stv0299_writeregI (state, 0x08, val | 0x3);
  261. case SEC_TONE_OFF:
  262. return stv0299_writeregI (state, 0x08, (val & ~0x3) | 0x02);
  263. default:
  264. return -EINVAL;
  265. }
  266. }
  267. static int stv0299_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage)
  268. {
  269. struct stv0299_state* state = fe->demodulator_priv;
  270. u8 reg0x08;
  271. u8 reg0x0c;
  272. dprintk("%s: %s\n", __FUNCTION__,
  273. voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
  274. voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
  275. reg0x08 = stv0299_readreg (state, 0x08);
  276. reg0x0c = stv0299_readreg (state, 0x0c);
  277. /**
  278. * H/V switching over OP0, OP1 and OP2 are LNB power enable bits
  279. */
  280. reg0x0c &= 0x0f;
  281. if (voltage == SEC_VOLTAGE_OFF) {
  282. stv0299_writeregI (state, 0x0c, 0x00); /* LNB power off! */
  283. return stv0299_writeregI (state, 0x08, 0x00); /* LNB power off! */
  284. }
  285. stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6));
  286. switch (voltage) {
  287. case SEC_VOLTAGE_13:
  288. if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0) reg0x0c |= 0x10;
  289. else reg0x0c |= 0x40;
  290. return stv0299_writeregI(state, 0x0c, reg0x0c);
  291. case SEC_VOLTAGE_18:
  292. return stv0299_writeregI(state, 0x0c, reg0x0c | 0x50);
  293. default:
  294. return -EINVAL;
  295. };
  296. }
  297. static int stv0299_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long cmd)
  298. {
  299. struct stv0299_state* state = fe->demodulator_priv;
  300. u8 reg0x08;
  301. u8 reg0x0c;
  302. u8 lv_mask = 0x40;
  303. u8 last = 1;
  304. int i;
  305. struct timeval nexttime;
  306. struct timeval tv[10];
  307. reg0x08 = stv0299_readreg (state, 0x08);
  308. reg0x0c = stv0299_readreg (state, 0x0c);
  309. reg0x0c &= 0x0f;
  310. stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6));
  311. if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
  312. lv_mask = 0x10;
  313. cmd = cmd << 1;
  314. if (debug_legacy_dish_switch)
  315. printk ("%s switch command: 0x%04lx\n",__FUNCTION__, cmd);
  316. do_gettimeofday (&nexttime);
  317. if (debug_legacy_dish_switch)
  318. memcpy (&tv[0], &nexttime, sizeof (struct timeval));
  319. stv0299_writeregI (state, 0x0c, reg0x0c | 0x50); /* set LNB to 18V */
  320. dvb_frontend_sleep_until(&nexttime, 32000);
  321. for (i=0; i<9; i++) {
  322. if (debug_legacy_dish_switch)
  323. do_gettimeofday (&tv[i+1]);
  324. if((cmd & 0x01) != last) {
  325. /* set voltage to (last ? 13V : 18V) */
  326. stv0299_writeregI (state, 0x0c, reg0x0c | (last ? lv_mask : 0x50));
  327. last = (last) ? 0 : 1;
  328. }
  329. cmd = cmd >> 1;
  330. if (i != 8)
  331. dvb_frontend_sleep_until(&nexttime, 8000);
  332. }
  333. if (debug_legacy_dish_switch) {
  334. printk ("%s(%d): switch delay (should be 32k followed by all 8k\n",
  335. __FUNCTION__, fe->dvb->num);
  336. for (i = 1; i < 10; i++)
  337. printk ("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
  338. }
  339. return 0;
  340. }
  341. static int stv0299_init (struct dvb_frontend* fe)
  342. {
  343. struct stv0299_state* state = fe->demodulator_priv;
  344. int i;
  345. dprintk("stv0299: init chip\n");
  346. for (i=0; !(state->config->inittab[i] == 0xff && state->config->inittab[i+1] == 0xff); i+=2)
  347. stv0299_writeregI(state, state->config->inittab[i], state->config->inittab[i+1]);
  348. if (state->config->pll_init) {
  349. stv0299_writeregI(state, 0x05, 0xb5); /* enable i2c repeater on stv0299 */
  350. state->config->pll_init(fe, state->i2c);
  351. stv0299_writeregI(state, 0x05, 0x35); /* disable i2c repeater on stv0299 */
  352. }
  353. return 0;
  354. }
  355. static int stv0299_read_status(struct dvb_frontend* fe, fe_status_t* status)
  356. {
  357. struct stv0299_state* state = fe->demodulator_priv;
  358. u8 signal = 0xff - stv0299_readreg (state, 0x18);
  359. u8 sync = stv0299_readreg (state, 0x1b);
  360. dprintk ("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __FUNCTION__, sync);
  361. *status = 0;
  362. if (signal > 10)
  363. *status |= FE_HAS_SIGNAL;
  364. if (sync & 0x80)
  365. *status |= FE_HAS_CARRIER;
  366. if (sync & 0x10)
  367. *status |= FE_HAS_VITERBI;
  368. if (sync & 0x08)
  369. *status |= FE_HAS_SYNC;
  370. if ((sync & 0x98) == 0x98)
  371. *status |= FE_HAS_LOCK;
  372. return 0;
  373. }
  374. static int stv0299_read_ber(struct dvb_frontend* fe, u32* ber)
  375. {
  376. struct stv0299_state* state = fe->demodulator_priv;
  377. if (state->errmode != STATUS_BER) return 0;
  378. *ber = (stv0299_readreg (state, 0x1d) << 8) | stv0299_readreg (state, 0x1e);
  379. return 0;
  380. }
  381. static int stv0299_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  382. {
  383. struct stv0299_state* state = fe->demodulator_priv;
  384. s32 signal = 0xffff - ((stv0299_readreg (state, 0x18) << 8)
  385. | stv0299_readreg (state, 0x19));
  386. dprintk ("%s : FE_READ_SIGNAL_STRENGTH : AGC2I: 0x%02x%02x, signal=0x%04x\n", __FUNCTION__,
  387. stv0299_readreg (state, 0x18),
  388. stv0299_readreg (state, 0x19), (int) signal);
  389. signal = signal * 5 / 4;
  390. *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal;
  391. return 0;
  392. }
  393. static int stv0299_read_snr(struct dvb_frontend* fe, u16* snr)
  394. {
  395. struct stv0299_state* state = fe->demodulator_priv;
  396. s32 xsnr = 0xffff - ((stv0299_readreg (state, 0x24) << 8)
  397. | stv0299_readreg (state, 0x25));
  398. xsnr = 3 * (xsnr - 0xa100);
  399. *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
  400. return 0;
  401. }
  402. static int stv0299_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  403. {
  404. struct stv0299_state* state = fe->demodulator_priv;
  405. if (state->errmode != STATUS_UCBLOCKS) *ucblocks = 0;
  406. else *ucblocks = (stv0299_readreg (state, 0x1d) << 8) | stv0299_readreg (state, 0x1e);
  407. return 0;
  408. }
  409. static int stv0299_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p)
  410. {
  411. struct stv0299_state* state = fe->demodulator_priv;
  412. int invval = 0;
  413. dprintk ("%s : FE_SET_FRONTEND\n", __FUNCTION__);
  414. // set the inversion
  415. if (p->inversion == INVERSION_OFF) invval = 0;
  416. else if (p->inversion == INVERSION_ON) invval = 1;
  417. else {
  418. printk("stv0299 does not support auto-inversion\n");
  419. return -EINVAL;
  420. }
  421. if (state->config->invert) invval = (~invval) & 1;
  422. stv0299_writeregI(state, 0x0c, (stv0299_readreg(state, 0x0c) & 0xfe) | invval);
  423. stv0299_writeregI(state, 0x05, 0xb5); /* enable i2c repeater on stv0299 */
  424. state->config->pll_set(fe, state->i2c, p);
  425. stv0299_writeregI(state, 0x05, 0x35); /* disable i2c repeater on stv0299 */
  426. stv0299_set_FEC (state, p->u.qpsk.fec_inner);
  427. stv0299_set_symbolrate (fe, p->u.qpsk.symbol_rate);
  428. stv0299_writeregI(state, 0x22, 0x00);
  429. stv0299_writeregI(state, 0x23, 0x00);
  430. state->tuner_frequency = p->frequency;
  431. state->fec_inner = p->u.qpsk.fec_inner;
  432. state->symbol_rate = p->u.qpsk.symbol_rate;
  433. return 0;
  434. }
  435. static int stv0299_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p)
  436. {
  437. struct stv0299_state* state = fe->demodulator_priv;
  438. s32 derot_freq;
  439. int invval;
  440. derot_freq = (s32)(s16) ((stv0299_readreg (state, 0x22) << 8)
  441. | stv0299_readreg (state, 0x23));
  442. derot_freq *= (state->config->mclk >> 16);
  443. derot_freq += 500;
  444. derot_freq /= 1000;
  445. p->frequency += derot_freq;
  446. invval = stv0299_readreg (state, 0x0c) & 1;
  447. if (state->config->invert) invval = (~invval) & 1;
  448. p->inversion = invval ? INVERSION_ON : INVERSION_OFF;
  449. p->u.qpsk.fec_inner = stv0299_get_fec (state);
  450. p->u.qpsk.symbol_rate = stv0299_get_symbolrate (state);
  451. return 0;
  452. }
  453. static int stv0299_sleep(struct dvb_frontend* fe)
  454. {
  455. struct stv0299_state* state = fe->demodulator_priv;
  456. stv0299_writeregI(state, 0x02, 0x80);
  457. state->initialised = 0;
  458. return 0;
  459. }
  460. static int stv0299_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  461. {
  462. struct stv0299_state* state = fe->demodulator_priv;
  463. fesettings->min_delay_ms = state->config->min_delay_ms;
  464. if (fesettings->parameters.u.qpsk.symbol_rate < 10000000) {
  465. fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 32000;
  466. fesettings->max_drift = 5000;
  467. } else {
  468. fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 16000;
  469. fesettings->max_drift = fesettings->parameters.u.qpsk.symbol_rate / 2000;
  470. }
  471. return 0;
  472. }
  473. static void stv0299_release(struct dvb_frontend* fe)
  474. {
  475. struct stv0299_state* state = fe->demodulator_priv;
  476. kfree(state);
  477. }
  478. static struct dvb_frontend_ops stv0299_ops;
  479. struct dvb_frontend* stv0299_attach(const struct stv0299_config* config,
  480. struct i2c_adapter* i2c)
  481. {
  482. struct stv0299_state* state = NULL;
  483. int id;
  484. /* allocate memory for the internal state */
  485. state = kmalloc(sizeof(struct stv0299_state), GFP_KERNEL);
  486. if (state == NULL) goto error;
  487. /* setup the state */
  488. state->config = config;
  489. state->i2c = i2c;
  490. memcpy(&state->ops, &stv0299_ops, sizeof(struct dvb_frontend_ops));
  491. state->initialised = 0;
  492. state->tuner_frequency = 0;
  493. state->symbol_rate = 0;
  494. state->fec_inner = 0;
  495. state->errmode = STATUS_BER;
  496. /* check if the demod is there */
  497. stv0299_writeregI(state, 0x02, 0x34); /* standby off */
  498. msleep(200);
  499. id = stv0299_readreg(state, 0x00);
  500. /* register 0x00 contains 0xa1 for STV0299 and STV0299B */
  501. /* register 0x00 might contain 0x80 when returning from standby */
  502. if (id != 0xa1 && id != 0x80) goto error;
  503. /* create dvb_frontend */
  504. state->frontend.ops = &state->ops;
  505. state->frontend.demodulator_priv = state;
  506. return &state->frontend;
  507. error:
  508. kfree(state);
  509. return NULL;
  510. }
  511. static struct dvb_frontend_ops stv0299_ops = {
  512. .info = {
  513. .name = "ST STV0299 DVB-S",
  514. .type = FE_QPSK,
  515. .frequency_min = 950000,
  516. .frequency_max = 2150000,
  517. .frequency_stepsize = 125, /* kHz for QPSK frontends */
  518. .frequency_tolerance = 0,
  519. .symbol_rate_min = 1000000,
  520. .symbol_rate_max = 45000000,
  521. .symbol_rate_tolerance = 500, /* ppm */
  522. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  523. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  524. FE_CAN_QPSK |
  525. FE_CAN_FEC_AUTO
  526. },
  527. .release = stv0299_release,
  528. .init = stv0299_init,
  529. .sleep = stv0299_sleep,
  530. .set_frontend = stv0299_set_frontend,
  531. .get_frontend = stv0299_get_frontend,
  532. .get_tune_settings = stv0299_get_tune_settings,
  533. .read_status = stv0299_read_status,
  534. .read_ber = stv0299_read_ber,
  535. .read_signal_strength = stv0299_read_signal_strength,
  536. .read_snr = stv0299_read_snr,
  537. .read_ucblocks = stv0299_read_ucblocks,
  538. .diseqc_send_master_cmd = stv0299_send_diseqc_msg,
  539. .diseqc_send_burst = stv0299_send_diseqc_burst,
  540. .set_tone = stv0299_set_tone,
  541. .set_voltage = stv0299_set_voltage,
  542. .dishnetwork_send_legacy_command = stv0299_send_legacy_dish_cmd,
  543. };
  544. module_param(debug_legacy_dish_switch, int, 0444);
  545. MODULE_PARM_DESC(debug_legacy_dish_switch, "Enable timing analysis for Dish Network legacy switches");
  546. module_param(debug, int, 0644);
  547. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  548. MODULE_DESCRIPTION("ST STV0299 DVB Demodulator driver");
  549. MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Peter Schildmann, Felix Domke, "
  550. "Andreas Oberritter, Andrew de Quincey, Kenneth Aafløy");
  551. MODULE_LICENSE("GPL");
  552. EXPORT_SYMBOL(stv0299_enable_plli2c);
  553. EXPORT_SYMBOL(stv0299_writereg);
  554. EXPORT_SYMBOL(stv0299_attach);