tda10086.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /*
  2. Driver for Philips tda10086 DVBS Demodulator
  3. (c) 2006 Andrew de Quincey
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/string.h>
  21. #include <linux/slab.h>
  22. #include "dvb_frontend.h"
  23. #include "tda10086.h"
  24. #define SACLK 96000000
  25. struct tda10086_state {
  26. struct i2c_adapter* i2c;
  27. const struct tda10086_config* config;
  28. struct dvb_frontend frontend;
  29. /* private demod data */
  30. u32 frequency;
  31. u32 symbol_rate;
  32. bool has_lock;
  33. };
  34. static int debug;
  35. #define dprintk(args...) \
  36. do { \
  37. if (debug) printk(KERN_DEBUG "tda10086: " args); \
  38. } while (0)
  39. static int tda10086_write_byte(struct tda10086_state *state, int reg, int data)
  40. {
  41. int ret;
  42. u8 b0[] = { reg, data };
  43. struct i2c_msg msg = { .flags = 0, .buf = b0, .len = 2 };
  44. msg.addr = state->config->demod_address;
  45. ret = i2c_transfer(state->i2c, &msg, 1);
  46. if (ret != 1)
  47. dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
  48. __func__, reg, data, ret);
  49. return (ret != 1) ? ret : 0;
  50. }
  51. static int tda10086_read_byte(struct tda10086_state *state, int reg)
  52. {
  53. int ret;
  54. u8 b0[] = { reg };
  55. u8 b1[] = { 0 };
  56. struct i2c_msg msg[] = {{ .flags = 0, .buf = b0, .len = 1 },
  57. { .flags = I2C_M_RD, .buf = b1, .len = 1 }};
  58. msg[0].addr = state->config->demod_address;
  59. msg[1].addr = state->config->demod_address;
  60. ret = i2c_transfer(state->i2c, msg, 2);
  61. if (ret != 2) {
  62. dprintk("%s: error reg=0x%x, ret=%i\n", __func__, reg,
  63. ret);
  64. return ret;
  65. }
  66. return b1[0];
  67. }
  68. static int tda10086_write_mask(struct tda10086_state *state, int reg, int mask, int data)
  69. {
  70. int val;
  71. /* read a byte and check */
  72. val = tda10086_read_byte(state, reg);
  73. if (val < 0)
  74. return val;
  75. /* mask if off */
  76. val = val & ~mask;
  77. val |= data & 0xff;
  78. /* write it out again */
  79. return tda10086_write_byte(state, reg, val);
  80. }
  81. static int tda10086_init(struct dvb_frontend* fe)
  82. {
  83. struct tda10086_state* state = fe->demodulator_priv;
  84. u8 t22k_off = 0x80;
  85. dprintk ("%s\n", __func__);
  86. if (state->config->diseqc_tone)
  87. t22k_off = 0;
  88. /* reset */
  89. tda10086_write_byte(state, 0x00, 0x00);
  90. msleep(10);
  91. /* misc setup */
  92. tda10086_write_byte(state, 0x01, 0x94);
  93. tda10086_write_byte(state, 0x02, 0x35); /* NOTE: TT drivers appear to disable CSWP */
  94. tda10086_write_byte(state, 0x03, 0xe4);
  95. tda10086_write_byte(state, 0x04, 0x43);
  96. tda10086_write_byte(state, 0x0c, 0x0c);
  97. tda10086_write_byte(state, 0x1b, 0xb0); /* noise threshold */
  98. tda10086_write_byte(state, 0x20, 0x89); /* misc */
  99. tda10086_write_byte(state, 0x30, 0x04); /* acquisition period length */
  100. tda10086_write_byte(state, 0x32, 0x00); /* irq off */
  101. tda10086_write_byte(state, 0x31, 0x56); /* setup AFC */
  102. /* setup PLL (this assumes SACLK = 96MHz) */
  103. tda10086_write_byte(state, 0x55, 0x2c); /* misc PLL setup */
  104. if (state->config->xtal_freq == TDA10086_XTAL_16M) {
  105. tda10086_write_byte(state, 0x3a, 0x0b); /* M=12 */
  106. tda10086_write_byte(state, 0x3b, 0x01); /* P=2 */
  107. } else {
  108. tda10086_write_byte(state, 0x3a, 0x17); /* M=24 */
  109. tda10086_write_byte(state, 0x3b, 0x00); /* P=1 */
  110. }
  111. tda10086_write_mask(state, 0x55, 0x20, 0x00); /* powerup PLL */
  112. /* setup TS interface */
  113. tda10086_write_byte(state, 0x11, 0x81);
  114. tda10086_write_byte(state, 0x12, 0x81);
  115. tda10086_write_byte(state, 0x19, 0x40); /* parallel mode A + MSBFIRST */
  116. tda10086_write_byte(state, 0x56, 0x80); /* powerdown WPLL - unused in the mode we use */
  117. tda10086_write_byte(state, 0x57, 0x08); /* bypass WPLL - unused in the mode we use */
  118. tda10086_write_byte(state, 0x10, 0x2a);
  119. /* setup ADC */
  120. tda10086_write_byte(state, 0x58, 0x61); /* ADC setup */
  121. tda10086_write_mask(state, 0x58, 0x01, 0x00); /* powerup ADC */
  122. /* setup AGC */
  123. tda10086_write_byte(state, 0x05, 0x0B);
  124. tda10086_write_byte(state, 0x37, 0x63);
  125. tda10086_write_byte(state, 0x3f, 0x0a); /* NOTE: flydvb varies it */
  126. tda10086_write_byte(state, 0x40, 0x64);
  127. tda10086_write_byte(state, 0x41, 0x4f);
  128. tda10086_write_byte(state, 0x42, 0x43);
  129. /* setup viterbi */
  130. tda10086_write_byte(state, 0x1a, 0x11); /* VBER 10^6, DVB, QPSK */
  131. /* setup carrier recovery */
  132. tda10086_write_byte(state, 0x3d, 0x80);
  133. /* setup SEC */
  134. tda10086_write_byte(state, 0x36, t22k_off); /* all SEC off, 22k tone */
  135. tda10086_write_byte(state, 0x34, (((1<<19) * (22000/1000)) / (SACLK/1000)));
  136. tda10086_write_byte(state, 0x35, (((1<<19) * (22000/1000)) / (SACLK/1000)) >> 8);
  137. return 0;
  138. }
  139. static void tda10086_diseqc_wait(struct tda10086_state *state)
  140. {
  141. unsigned long timeout = jiffies + msecs_to_jiffies(200);
  142. while (!(tda10086_read_byte(state, 0x50) & 0x01)) {
  143. if(time_after(jiffies, timeout)) {
  144. printk("%s: diseqc queue not ready, command may be lost.\n", __func__);
  145. break;
  146. }
  147. msleep(10);
  148. }
  149. }
  150. static int tda10086_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  151. {
  152. struct tda10086_state* state = fe->demodulator_priv;
  153. u8 t22k_off = 0x80;
  154. dprintk ("%s\n", __func__);
  155. if (state->config->diseqc_tone)
  156. t22k_off = 0;
  157. switch (tone) {
  158. case SEC_TONE_OFF:
  159. tda10086_write_byte(state, 0x36, t22k_off);
  160. break;
  161. case SEC_TONE_ON:
  162. tda10086_write_byte(state, 0x36, 0x01 + t22k_off);
  163. break;
  164. }
  165. return 0;
  166. }
  167. static int tda10086_send_master_cmd (struct dvb_frontend* fe,
  168. struct dvb_diseqc_master_cmd* cmd)
  169. {
  170. struct tda10086_state* state = fe->demodulator_priv;
  171. int i;
  172. u8 oldval;
  173. u8 t22k_off = 0x80;
  174. dprintk ("%s\n", __func__);
  175. if (state->config->diseqc_tone)
  176. t22k_off = 0;
  177. if (cmd->msg_len > 6)
  178. return -EINVAL;
  179. oldval = tda10086_read_byte(state, 0x36);
  180. for(i=0; i< cmd->msg_len; i++) {
  181. tda10086_write_byte(state, 0x48+i, cmd->msg[i]);
  182. }
  183. tda10086_write_byte(state, 0x36, (0x08 + t22k_off)
  184. | ((cmd->msg_len - 1) << 4));
  185. tda10086_diseqc_wait(state);
  186. tda10086_write_byte(state, 0x36, oldval);
  187. return 0;
  188. }
  189. static int tda10086_send_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd)
  190. {
  191. struct tda10086_state* state = fe->demodulator_priv;
  192. u8 oldval = tda10086_read_byte(state, 0x36);
  193. u8 t22k_off = 0x80;
  194. dprintk ("%s\n", __func__);
  195. if (state->config->diseqc_tone)
  196. t22k_off = 0;
  197. switch(minicmd) {
  198. case SEC_MINI_A:
  199. tda10086_write_byte(state, 0x36, 0x04 + t22k_off);
  200. break;
  201. case SEC_MINI_B:
  202. tda10086_write_byte(state, 0x36, 0x06 + t22k_off);
  203. break;
  204. }
  205. tda10086_diseqc_wait(state);
  206. tda10086_write_byte(state, 0x36, oldval);
  207. return 0;
  208. }
  209. static int tda10086_set_inversion(struct tda10086_state *state,
  210. struct dtv_frontend_properties *fe_params)
  211. {
  212. u8 invval = 0x80;
  213. dprintk ("%s %i %i\n", __func__, fe_params->inversion, state->config->invert);
  214. switch(fe_params->inversion) {
  215. case INVERSION_OFF:
  216. if (state->config->invert)
  217. invval = 0x40;
  218. break;
  219. case INVERSION_ON:
  220. if (!state->config->invert)
  221. invval = 0x40;
  222. break;
  223. case INVERSION_AUTO:
  224. invval = 0x00;
  225. break;
  226. }
  227. tda10086_write_mask(state, 0x0c, 0xc0, invval);
  228. return 0;
  229. }
  230. static int tda10086_set_symbol_rate(struct tda10086_state *state,
  231. struct dtv_frontend_properties *fe_params)
  232. {
  233. u8 dfn = 0;
  234. u8 afs = 0;
  235. u8 byp = 0;
  236. u8 reg37 = 0x43;
  237. u8 reg42 = 0x43;
  238. u64 big;
  239. u32 tmp;
  240. u32 bdr;
  241. u32 bdri;
  242. u32 symbol_rate = fe_params->symbol_rate;
  243. dprintk ("%s %i\n", __func__, symbol_rate);
  244. /* setup the decimation and anti-aliasing filters.. */
  245. if (symbol_rate < (u32) (SACLK * 0.0137)) {
  246. dfn=4;
  247. afs=1;
  248. } else if (symbol_rate < (u32) (SACLK * 0.0208)) {
  249. dfn=4;
  250. afs=0;
  251. } else if (symbol_rate < (u32) (SACLK * 0.0270)) {
  252. dfn=3;
  253. afs=1;
  254. } else if (symbol_rate < (u32) (SACLK * 0.0416)) {
  255. dfn=3;
  256. afs=0;
  257. } else if (symbol_rate < (u32) (SACLK * 0.0550)) {
  258. dfn=2;
  259. afs=1;
  260. } else if (symbol_rate < (u32) (SACLK * 0.0833)) {
  261. dfn=2;
  262. afs=0;
  263. } else if (symbol_rate < (u32) (SACLK * 0.1100)) {
  264. dfn=1;
  265. afs=1;
  266. } else if (symbol_rate < (u32) (SACLK * 0.1666)) {
  267. dfn=1;
  268. afs=0;
  269. } else if (symbol_rate < (u32) (SACLK * 0.2200)) {
  270. dfn=0;
  271. afs=1;
  272. } else if (symbol_rate < (u32) (SACLK * 0.3333)) {
  273. dfn=0;
  274. afs=0;
  275. } else {
  276. reg37 = 0x63;
  277. reg42 = 0x4f;
  278. byp=1;
  279. }
  280. /* calculate BDR */
  281. big = (1ULL<<21) * ((u64) symbol_rate/1000ULL) * (1ULL<<dfn);
  282. big += ((SACLK/1000ULL)-1ULL);
  283. do_div(big, (SACLK/1000ULL));
  284. bdr = big & 0xfffff;
  285. /* calculate BDRI */
  286. tmp = (1<<dfn)*(symbol_rate/1000);
  287. bdri = ((32 * (SACLK/1000)) + (tmp-1)) / tmp;
  288. tda10086_write_byte(state, 0x21, (afs << 7) | dfn);
  289. tda10086_write_mask(state, 0x20, 0x08, byp << 3);
  290. tda10086_write_byte(state, 0x06, bdr);
  291. tda10086_write_byte(state, 0x07, bdr >> 8);
  292. tda10086_write_byte(state, 0x08, bdr >> 16);
  293. tda10086_write_byte(state, 0x09, bdri);
  294. tda10086_write_byte(state, 0x37, reg37);
  295. tda10086_write_byte(state, 0x42, reg42);
  296. return 0;
  297. }
  298. static int tda10086_set_fec(struct tda10086_state *state,
  299. struct dtv_frontend_properties *fe_params)
  300. {
  301. u8 fecval;
  302. dprintk("%s %i\n", __func__, fe_params->fec_inner);
  303. switch (fe_params->fec_inner) {
  304. case FEC_1_2:
  305. fecval = 0x00;
  306. break;
  307. case FEC_2_3:
  308. fecval = 0x01;
  309. break;
  310. case FEC_3_4:
  311. fecval = 0x02;
  312. break;
  313. case FEC_4_5:
  314. fecval = 0x03;
  315. break;
  316. case FEC_5_6:
  317. fecval = 0x04;
  318. break;
  319. case FEC_6_7:
  320. fecval = 0x05;
  321. break;
  322. case FEC_7_8:
  323. fecval = 0x06;
  324. break;
  325. case FEC_8_9:
  326. fecval = 0x07;
  327. break;
  328. case FEC_AUTO:
  329. fecval = 0x08;
  330. break;
  331. default:
  332. return -1;
  333. }
  334. tda10086_write_byte(state, 0x0d, fecval);
  335. return 0;
  336. }
  337. static int tda10086_set_frontend(struct dvb_frontend *fe)
  338. {
  339. struct dtv_frontend_properties *fe_params = &fe->dtv_property_cache;
  340. struct tda10086_state *state = fe->demodulator_priv;
  341. int ret;
  342. u32 freq = 0;
  343. int freqoff;
  344. dprintk ("%s\n", __func__);
  345. /* modify parameters for tuning */
  346. tda10086_write_byte(state, 0x02, 0x35);
  347. state->has_lock = false;
  348. /* set params */
  349. if (fe->ops.tuner_ops.set_params) {
  350. fe->ops.tuner_ops.set_params(fe);
  351. if (fe->ops.i2c_gate_ctrl)
  352. fe->ops.i2c_gate_ctrl(fe, 0);
  353. if (fe->ops.tuner_ops.get_frequency)
  354. fe->ops.tuner_ops.get_frequency(fe, &freq);
  355. if (fe->ops.i2c_gate_ctrl)
  356. fe->ops.i2c_gate_ctrl(fe, 0);
  357. }
  358. /* calcluate the frequency offset (in *Hz* not kHz) */
  359. freqoff = fe_params->frequency - freq;
  360. freqoff = ((1<<16) * freqoff) / (SACLK/1000);
  361. tda10086_write_byte(state, 0x3d, 0x80 | ((freqoff >> 8) & 0x7f));
  362. tda10086_write_byte(state, 0x3e, freqoff);
  363. if ((ret = tda10086_set_inversion(state, fe_params)) < 0)
  364. return ret;
  365. if ((ret = tda10086_set_symbol_rate(state, fe_params)) < 0)
  366. return ret;
  367. if ((ret = tda10086_set_fec(state, fe_params)) < 0)
  368. return ret;
  369. /* soft reset + disable TS output until lock */
  370. tda10086_write_mask(state, 0x10, 0x40, 0x40);
  371. tda10086_write_mask(state, 0x00, 0x01, 0x00);
  372. state->symbol_rate = fe_params->symbol_rate;
  373. state->frequency = fe_params->frequency;
  374. return 0;
  375. }
  376. static int tda10086_get_frontend(struct dvb_frontend *fe)
  377. {
  378. struct dtv_frontend_properties *fe_params = &fe->dtv_property_cache;
  379. struct tda10086_state* state = fe->demodulator_priv;
  380. u8 val;
  381. int tmp;
  382. u64 tmp64;
  383. dprintk ("%s\n", __func__);
  384. /* check for invalid symbol rate */
  385. if (fe_params->symbol_rate < 500000)
  386. return -EINVAL;
  387. /* calculate the updated frequency (note: we convert from Hz->kHz) */
  388. tmp64 = tda10086_read_byte(state, 0x52);
  389. tmp64 |= (tda10086_read_byte(state, 0x51) << 8);
  390. if (tmp64 & 0x8000)
  391. tmp64 |= 0xffffffffffff0000ULL;
  392. tmp64 = (tmp64 * (SACLK/1000ULL));
  393. do_div(tmp64, (1ULL<<15) * (1ULL<<1));
  394. fe_params->frequency = (int) state->frequency + (int) tmp64;
  395. /* the inversion */
  396. val = tda10086_read_byte(state, 0x0c);
  397. if (val & 0x80) {
  398. switch(val & 0x40) {
  399. case 0x00:
  400. fe_params->inversion = INVERSION_OFF;
  401. if (state->config->invert)
  402. fe_params->inversion = INVERSION_ON;
  403. break;
  404. default:
  405. fe_params->inversion = INVERSION_ON;
  406. if (state->config->invert)
  407. fe_params->inversion = INVERSION_OFF;
  408. break;
  409. }
  410. } else {
  411. tda10086_read_byte(state, 0x0f);
  412. switch(val & 0x02) {
  413. case 0x00:
  414. fe_params->inversion = INVERSION_OFF;
  415. if (state->config->invert)
  416. fe_params->inversion = INVERSION_ON;
  417. break;
  418. default:
  419. fe_params->inversion = INVERSION_ON;
  420. if (state->config->invert)
  421. fe_params->inversion = INVERSION_OFF;
  422. break;
  423. }
  424. }
  425. /* calculate the updated symbol rate */
  426. tmp = tda10086_read_byte(state, 0x1d);
  427. if (tmp & 0x80)
  428. tmp |= 0xffffff00;
  429. tmp = (tmp * 480 * (1<<1)) / 128;
  430. tmp = ((state->symbol_rate/1000) * tmp) / (1000000/1000);
  431. fe_params->symbol_rate = state->symbol_rate + tmp;
  432. /* the FEC */
  433. val = (tda10086_read_byte(state, 0x0d) & 0x70) >> 4;
  434. switch(val) {
  435. case 0x00:
  436. fe_params->fec_inner = FEC_1_2;
  437. break;
  438. case 0x01:
  439. fe_params->fec_inner = FEC_2_3;
  440. break;
  441. case 0x02:
  442. fe_params->fec_inner = FEC_3_4;
  443. break;
  444. case 0x03:
  445. fe_params->fec_inner = FEC_4_5;
  446. break;
  447. case 0x04:
  448. fe_params->fec_inner = FEC_5_6;
  449. break;
  450. case 0x05:
  451. fe_params->fec_inner = FEC_6_7;
  452. break;
  453. case 0x06:
  454. fe_params->fec_inner = FEC_7_8;
  455. break;
  456. case 0x07:
  457. fe_params->fec_inner = FEC_8_9;
  458. break;
  459. }
  460. return 0;
  461. }
  462. static int tda10086_read_status(struct dvb_frontend* fe, fe_status_t *fe_status)
  463. {
  464. struct tda10086_state* state = fe->demodulator_priv;
  465. u8 val;
  466. dprintk ("%s\n", __func__);
  467. val = tda10086_read_byte(state, 0x0e);
  468. *fe_status = 0;
  469. if (val & 0x01)
  470. *fe_status |= FE_HAS_SIGNAL;
  471. if (val & 0x02)
  472. *fe_status |= FE_HAS_CARRIER;
  473. if (val & 0x04)
  474. *fe_status |= FE_HAS_VITERBI;
  475. if (val & 0x08)
  476. *fe_status |= FE_HAS_SYNC;
  477. if (val & 0x10) {
  478. *fe_status |= FE_HAS_LOCK;
  479. if (!state->has_lock) {
  480. state->has_lock = true;
  481. /* modify parameters for stable reception */
  482. tda10086_write_byte(state, 0x02, 0x00);
  483. }
  484. }
  485. return 0;
  486. }
  487. static int tda10086_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
  488. {
  489. struct tda10086_state* state = fe->demodulator_priv;
  490. u8 _str;
  491. dprintk ("%s\n", __func__);
  492. _str = 0xff - tda10086_read_byte(state, 0x43);
  493. *signal = (_str << 8) | _str;
  494. return 0;
  495. }
  496. static int tda10086_read_snr(struct dvb_frontend* fe, u16 * snr)
  497. {
  498. struct tda10086_state* state = fe->demodulator_priv;
  499. u8 _snr;
  500. dprintk ("%s\n", __func__);
  501. _snr = 0xff - tda10086_read_byte(state, 0x1c);
  502. *snr = (_snr << 8) | _snr;
  503. return 0;
  504. }
  505. static int tda10086_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  506. {
  507. struct tda10086_state* state = fe->demodulator_priv;
  508. dprintk ("%s\n", __func__);
  509. /* read it */
  510. *ucblocks = tda10086_read_byte(state, 0x18) & 0x7f;
  511. /* reset counter */
  512. tda10086_write_byte(state, 0x18, 0x00);
  513. tda10086_write_byte(state, 0x18, 0x80);
  514. return 0;
  515. }
  516. static int tda10086_read_ber(struct dvb_frontend* fe, u32* ber)
  517. {
  518. struct tda10086_state* state = fe->demodulator_priv;
  519. dprintk ("%s\n", __func__);
  520. /* read it */
  521. *ber = 0;
  522. *ber |= tda10086_read_byte(state, 0x15);
  523. *ber |= tda10086_read_byte(state, 0x16) << 8;
  524. *ber |= (tda10086_read_byte(state, 0x17) & 0xf) << 16;
  525. return 0;
  526. }
  527. static int tda10086_sleep(struct dvb_frontend* fe)
  528. {
  529. struct tda10086_state* state = fe->demodulator_priv;
  530. dprintk ("%s\n", __func__);
  531. tda10086_write_mask(state, 0x00, 0x08, 0x08);
  532. return 0;
  533. }
  534. static int tda10086_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  535. {
  536. struct tda10086_state* state = fe->demodulator_priv;
  537. dprintk ("%s\n", __func__);
  538. if (enable) {
  539. tda10086_write_mask(state, 0x00, 0x10, 0x10);
  540. } else {
  541. tda10086_write_mask(state, 0x00, 0x10, 0x00);
  542. }
  543. return 0;
  544. }
  545. static int tda10086_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  546. {
  547. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  548. if (p->symbol_rate > 20000000) {
  549. fesettings->min_delay_ms = 50;
  550. fesettings->step_size = 2000;
  551. fesettings->max_drift = 8000;
  552. } else if (p->symbol_rate > 12000000) {
  553. fesettings->min_delay_ms = 100;
  554. fesettings->step_size = 1500;
  555. fesettings->max_drift = 9000;
  556. } else if (p->symbol_rate > 8000000) {
  557. fesettings->min_delay_ms = 100;
  558. fesettings->step_size = 1000;
  559. fesettings->max_drift = 8000;
  560. } else if (p->symbol_rate > 4000000) {
  561. fesettings->min_delay_ms = 100;
  562. fesettings->step_size = 500;
  563. fesettings->max_drift = 7000;
  564. } else if (p->symbol_rate > 2000000) {
  565. fesettings->min_delay_ms = 200;
  566. fesettings->step_size = p->symbol_rate / 8000;
  567. fesettings->max_drift = 14 * fesettings->step_size;
  568. } else {
  569. fesettings->min_delay_ms = 200;
  570. fesettings->step_size = p->symbol_rate / 8000;
  571. fesettings->max_drift = 18 * fesettings->step_size;
  572. }
  573. return 0;
  574. }
  575. static void tda10086_release(struct dvb_frontend* fe)
  576. {
  577. struct tda10086_state *state = fe->demodulator_priv;
  578. tda10086_sleep(fe);
  579. kfree(state);
  580. }
  581. static struct dvb_frontend_ops tda10086_ops = {
  582. .delsys = { SYS_DVBS },
  583. .info = {
  584. .name = "Philips TDA10086 DVB-S",
  585. .frequency_min = 950000,
  586. .frequency_max = 2150000,
  587. .frequency_stepsize = 125, /* kHz for QPSK frontends */
  588. .symbol_rate_min = 1000000,
  589. .symbol_rate_max = 45000000,
  590. .caps = FE_CAN_INVERSION_AUTO |
  591. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  592. FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  593. FE_CAN_QPSK
  594. },
  595. .release = tda10086_release,
  596. .init = tda10086_init,
  597. .sleep = tda10086_sleep,
  598. .i2c_gate_ctrl = tda10086_i2c_gate_ctrl,
  599. .set_frontend = tda10086_set_frontend,
  600. .get_frontend = tda10086_get_frontend,
  601. .get_tune_settings = tda10086_get_tune_settings,
  602. .read_status = tda10086_read_status,
  603. .read_ber = tda10086_read_ber,
  604. .read_signal_strength = tda10086_read_signal_strength,
  605. .read_snr = tda10086_read_snr,
  606. .read_ucblocks = tda10086_read_ucblocks,
  607. .diseqc_send_master_cmd = tda10086_send_master_cmd,
  608. .diseqc_send_burst = tda10086_send_burst,
  609. .set_tone = tda10086_set_tone,
  610. };
  611. struct dvb_frontend* tda10086_attach(const struct tda10086_config* config,
  612. struct i2c_adapter* i2c)
  613. {
  614. struct tda10086_state *state;
  615. dprintk ("%s\n", __func__);
  616. /* allocate memory for the internal state */
  617. state = kzalloc(sizeof(struct tda10086_state), GFP_KERNEL);
  618. if (!state)
  619. return NULL;
  620. /* setup the state */
  621. state->config = config;
  622. state->i2c = i2c;
  623. /* check if the demod is there */
  624. if (tda10086_read_byte(state, 0x1e) != 0xe1) {
  625. kfree(state);
  626. return NULL;
  627. }
  628. /* create dvb_frontend */
  629. memcpy(&state->frontend.ops, &tda10086_ops, sizeof(struct dvb_frontend_ops));
  630. state->frontend.demodulator_priv = state;
  631. return &state->frontend;
  632. }
  633. module_param(debug, int, 0644);
  634. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  635. MODULE_DESCRIPTION("Philips TDA10086 DVB-S Demodulator");
  636. MODULE_AUTHOR("Andrew de Quincey");
  637. MODULE_LICENSE("GPL");
  638. EXPORT_SYMBOL(tda10086_attach);