stv0299.c 18 KB

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