stv0299.c 20 KB

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