stv0299.c 18 KB

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