stv0299.c 18 KB

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