stv0299.c 18 KB

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