stv0299.c 18 KB

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