stv0299.c 20 KB

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