cx24110.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. cx24110 - Single Chip Satellite Channel Receiver driver module
  3. Copyright (C) 2002 Peter Hettkamp <peter.hettkamp@htp-tel.de> based on
  4. work
  5. Copyright (C) 1999 Convergence Integrated Media GmbH <ralph@convergence.de>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/slab.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/init.h>
  23. #include "dvb_frontend.h"
  24. #include "cx24110.h"
  25. struct cx24110_state {
  26. struct i2c_adapter* i2c;
  27. struct dvb_frontend_ops ops;
  28. const struct cx24110_config* config;
  29. struct dvb_frontend frontend;
  30. u32 lastber;
  31. u32 lastbler;
  32. u32 lastesn0;
  33. };
  34. static int debug;
  35. #define dprintk(args...) \
  36. do { \
  37. if (debug) printk(KERN_DEBUG "cx24110: " args); \
  38. } while (0)
  39. static struct {u8 reg; u8 data;} cx24110_regdata[]=
  40. /* Comments beginning with @ denote this value should
  41. be the default */
  42. {{0x09,0x01}, /* SoftResetAll */
  43. {0x09,0x00}, /* release reset */
  44. {0x01,0xe8}, /* MSB of code rate 27.5MS/s */
  45. {0x02,0x17}, /* middle byte " */
  46. {0x03,0x29}, /* LSB " */
  47. {0x05,0x03}, /* @ DVB mode, standard code rate 3/4 */
  48. {0x06,0xa5}, /* @ PLL 60MHz */
  49. {0x07,0x01}, /* @ Fclk, i.e. sampling clock, 60MHz */
  50. {0x0a,0x00}, /* @ partial chip disables, do not set */
  51. {0x0b,0x01}, /* set output clock in gapped mode, start signal low
  52. active for first byte */
  53. {0x0c,0x11}, /* no parity bytes, large hold time, serial data out */
  54. {0x0d,0x6f}, /* @ RS Sync/Unsync thresholds */
  55. {0x10,0x40}, /* chip doc is misleading here: write bit 6 as 1
  56. to avoid starting the BER counter. Reset the
  57. CRC test bit. Finite counting selected */
  58. {0x15,0xff}, /* @ size of the limited time window for RS BER
  59. estimation. It is <value>*256 RS blocks, this
  60. gives approx. 2.6 sec at 27.5MS/s, rate 3/4 */
  61. {0x16,0x00}, /* @ enable all RS output ports */
  62. {0x17,0x04}, /* @ time window allowed for the RS to sync */
  63. {0x18,0xae}, /* @ allow all standard DVB code rates to be scanned
  64. for automatically */
  65. /* leave the current code rate and normalization
  66. registers as they are after reset... */
  67. {0x21,0x10}, /* @ during AutoAcq, search each viterbi setting
  68. only once */
  69. {0x23,0x18}, /* @ size of the limited time window for Viterbi BER
  70. estimation. It is <value>*65536 channel bits, i.e.
  71. approx. 38ms at 27.5MS/s, rate 3/4 */
  72. {0x24,0x24}, /* do not trigger Viterbi CRC test. Finite count window */
  73. /* leave front-end AGC parameters at default values */
  74. /* leave decimation AGC parameters at default values */
  75. {0x35,0x40}, /* disable all interrupts. They are not connected anyway */
  76. {0x36,0xff}, /* clear all interrupt pending flags */
  77. {0x37,0x00}, /* @ fully enable AutoAcqq state machine */
  78. {0x38,0x07}, /* @ enable fade recovery, but not autostart AutoAcq */
  79. /* leave the equalizer parameters on their default values */
  80. /* leave the final AGC parameters on their default values */
  81. {0x41,0x00}, /* @ MSB of front-end derotator frequency */
  82. {0x42,0x00}, /* @ middle bytes " */
  83. {0x43,0x00}, /* @ LSB " */
  84. /* leave the carrier tracking loop parameters on default */
  85. /* leave the bit timing loop parameters at gefault */
  86. {0x56,0x4d}, /* set the filtune voltage to 2.7V, as recommended by */
  87. /* the cx24108 data sheet for symbol rates above 15MS/s */
  88. {0x57,0x00}, /* @ Filter sigma delta enabled, positive */
  89. {0x61,0x95}, /* GPIO pins 1-4 have special function */
  90. {0x62,0x05}, /* GPIO pin 5 has special function, pin 6 is GPIO */
  91. {0x63,0x00}, /* All GPIO pins use CMOS output characteristics */
  92. {0x64,0x20}, /* GPIO 6 is input, all others are outputs */
  93. {0x6d,0x30}, /* tuner auto mode clock freq 62kHz */
  94. {0x70,0x15}, /* use auto mode, tuner word is 21 bits long */
  95. {0x73,0x00}, /* @ disable several demod bypasses */
  96. {0x74,0x00}, /* @ " */
  97. {0x75,0x00} /* @ " */
  98. /* the remaining registers are for SEC */
  99. };
  100. static int cx24110_writereg (struct cx24110_state* state, int reg, int data)
  101. {
  102. u8 buf [] = { reg, data };
  103. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
  104. int err;
  105. if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
  106. dprintk ("%s: writereg error (err == %i, reg == 0x%02x,"
  107. " data == 0x%02x)\n", __FUNCTION__, err, reg, data);
  108. return -EREMOTEIO;
  109. }
  110. return 0;
  111. }
  112. static int cx24110_readreg (struct cx24110_state* state, u8 reg)
  113. {
  114. int ret;
  115. u8 b0 [] = { reg };
  116. u8 b1 [] = { 0 };
  117. struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
  118. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
  119. ret = i2c_transfer(state->i2c, msg, 2);
  120. if (ret != 2) return ret;
  121. return b1[0];
  122. }
  123. static int cx24110_set_inversion (struct cx24110_state* state, fe_spectral_inversion_t inversion)
  124. {
  125. /* fixme (low): error handling */
  126. switch (inversion) {
  127. case INVERSION_OFF:
  128. cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)|0x1);
  129. /* AcqSpectrInvDis on. No idea why someone should want this */
  130. cx24110_writereg(state,0x5,cx24110_readreg(state,0x5)&0xf7);
  131. /* Initial value 0 at start of acq */
  132. cx24110_writereg(state,0x22,cx24110_readreg(state,0x22)&0xef);
  133. /* current value 0 */
  134. /* The cx24110 manual tells us this reg is read-only.
  135. But what the heck... set it ayways */
  136. break;
  137. case INVERSION_ON:
  138. cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)|0x1);
  139. /* AcqSpectrInvDis on. No idea why someone should want this */
  140. cx24110_writereg(state,0x5,cx24110_readreg(state,0x5)|0x08);
  141. /* Initial value 1 at start of acq */
  142. cx24110_writereg(state,0x22,cx24110_readreg(state,0x22)|0x10);
  143. /* current value 1 */
  144. break;
  145. case INVERSION_AUTO:
  146. cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)&0xfe);
  147. /* AcqSpectrInvDis off. Leave initial & current states as is */
  148. break;
  149. default:
  150. return -EINVAL;
  151. }
  152. return 0;
  153. }
  154. static int cx24110_set_fec (struct cx24110_state* state, fe_code_rate_t fec)
  155. {
  156. /* fixme (low): error handling */
  157. static const int rate[]={-1,1,2,3,5,7,-1};
  158. static const int g1[]={-1,0x01,0x02,0x05,0x15,0x45,-1};
  159. static const int g2[]={-1,0x01,0x03,0x06,0x1a,0x7a,-1};
  160. /* Well, the AutoAcq engine of the cx24106 and 24110 automatically
  161. searches all enabled viterbi rates, and can handle non-standard
  162. rates as well. */
  163. if (fec>FEC_AUTO)
  164. fec=FEC_AUTO;
  165. if (fec==FEC_AUTO) { /* (re-)establish AutoAcq behaviour */
  166. cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)&0xdf);
  167. /* clear AcqVitDis bit */
  168. cx24110_writereg(state,0x18,0xae);
  169. /* allow all DVB standard code rates */
  170. cx24110_writereg(state,0x05,(cx24110_readreg(state,0x05)&0xf0)|0x3);
  171. /* set nominal Viterbi rate 3/4 */
  172. cx24110_writereg(state,0x22,(cx24110_readreg(state,0x22)&0xf0)|0x3);
  173. /* set current Viterbi rate 3/4 */
  174. cx24110_writereg(state,0x1a,0x05); cx24110_writereg(state,0x1b,0x06);
  175. /* set the puncture registers for code rate 3/4 */
  176. return 0;
  177. } else {
  178. cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)|0x20);
  179. /* set AcqVitDis bit */
  180. if(rate[fec]>0) {
  181. cx24110_writereg(state,0x05,(cx24110_readreg(state,0x05)&0xf0)|rate[fec]);
  182. /* set nominal Viterbi rate */
  183. cx24110_writereg(state,0x22,(cx24110_readreg(state,0x22)&0xf0)|rate[fec]);
  184. /* set current Viterbi rate */
  185. cx24110_writereg(state,0x1a,g1[fec]);
  186. cx24110_writereg(state,0x1b,g2[fec]);
  187. /* not sure if this is the right way: I always used AutoAcq mode */
  188. } else
  189. return -EOPNOTSUPP;
  190. /* fixme (low): which is the correct return code? */
  191. };
  192. return 0;
  193. }
  194. static fe_code_rate_t cx24110_get_fec (struct cx24110_state* state)
  195. {
  196. int i;
  197. i=cx24110_readreg(state,0x22)&0x0f;
  198. if(!(i&0x08)) {
  199. return FEC_1_2 + i - 1;
  200. } else {
  201. /* fixme (low): a special code rate has been selected. In theory, we need to
  202. return a denominator value, a numerator value, and a pair of puncture
  203. maps to correctly describe this mode. But this should never happen in
  204. practice, because it cannot be set by cx24110_get_fec. */
  205. return FEC_NONE;
  206. }
  207. }
  208. static int cx24110_set_symbolrate (struct cx24110_state* state, u32 srate)
  209. {
  210. /* fixme (low): add error handling */
  211. u32 ratio;
  212. u32 tmp, fclk, BDRI;
  213. static const u32 bands[]={5000000UL,15000000UL,90999000UL/2};
  214. int i;
  215. dprintk("cx24110 debug: entering %s(%d)\n",__FUNCTION__,srate);
  216. if (srate>90999000UL/2)
  217. srate=90999000UL/2;
  218. if (srate<500000)
  219. srate=500000;
  220. for(i=0;(i<sizeof(bands)/sizeof(bands[0]))&&(srate>bands[i]);i++)
  221. ;
  222. /* first, check which sample rate is appropriate: 45, 60 80 or 90 MHz,
  223. and set the PLL accordingly (R07[1:0] Fclk, R06[7:4] PLLmult,
  224. R06[3:0] PLLphaseDetGain */
  225. tmp=cx24110_readreg(state,0x07)&0xfc;
  226. if(srate<90999000UL/4) { /* sample rate 45MHz*/
  227. cx24110_writereg(state,0x07,tmp);
  228. cx24110_writereg(state,0x06,0x78);
  229. fclk=90999000UL/2;
  230. } else if(srate<60666000UL/2) { /* sample rate 60MHz */
  231. cx24110_writereg(state,0x07,tmp|0x1);
  232. cx24110_writereg(state,0x06,0xa5);
  233. fclk=60666000UL;
  234. } else if(srate<80888000UL/2) { /* sample rate 80MHz */
  235. cx24110_writereg(state,0x07,tmp|0x2);
  236. cx24110_writereg(state,0x06,0x87);
  237. fclk=80888000UL;
  238. } else { /* sample rate 90MHz */
  239. cx24110_writereg(state,0x07,tmp|0x3);
  240. cx24110_writereg(state,0x06,0x78);
  241. fclk=90999000UL;
  242. };
  243. dprintk("cx24110 debug: fclk %d Hz\n",fclk);
  244. /* we need to divide two integers with approx. 27 bits in 32 bit
  245. arithmetic giving a 25 bit result */
  246. /* the maximum dividend is 90999000/2, 0x02b6446c, this number is
  247. also the most complex divisor. Hence, the dividend has,
  248. assuming 32bit unsigned arithmetic, 6 clear bits on top, the
  249. divisor 2 unused bits at the bottom. Also, the quotient is
  250. always less than 1/2. Borrowed from VES1893.c, of course */
  251. tmp=srate<<6;
  252. BDRI=fclk>>2;
  253. ratio=(tmp/BDRI);
  254. tmp=(tmp%BDRI)<<8;
  255. ratio=(ratio<<8)+(tmp/BDRI);
  256. tmp=(tmp%BDRI)<<8;
  257. ratio=(ratio<<8)+(tmp/BDRI);
  258. tmp=(tmp%BDRI)<<1;
  259. ratio=(ratio<<1)+(tmp/BDRI);
  260. dprintk("srate= %d (range %d, up to %d)\n", srate,i,bands[i]);
  261. dprintk("fclk = %d\n", fclk);
  262. dprintk("ratio= %08x\n", ratio);
  263. cx24110_writereg(state, 0x1, (ratio>>16)&0xff);
  264. cx24110_writereg(state, 0x2, (ratio>>8)&0xff);
  265. cx24110_writereg(state, 0x3, (ratio)&0xff);
  266. return 0;
  267. }
  268. int cx24110_pll_write (struct dvb_frontend* fe, u32 data)
  269. {
  270. struct cx24110_state *state = fe->demodulator_priv;
  271. /* tuner data is 21 bits long, must be left-aligned in data */
  272. /* tuner cx24108 is written through a dedicated 3wire interface on the demod chip */
  273. /* FIXME (low): add error handling, avoid infinite loops if HW fails... */
  274. dprintk("cx24110 debug: cx24108_write(%8.8x)\n",data);
  275. cx24110_writereg(state,0x6d,0x30); /* auto mode at 62kHz */
  276. cx24110_writereg(state,0x70,0x15); /* auto mode 21 bits */
  277. /* if the auto tuner writer is still busy, clear it out */
  278. while (cx24110_readreg(state,0x6d)&0x80)
  279. cx24110_writereg(state,0x72,0);
  280. /* write the topmost 8 bits */
  281. cx24110_writereg(state,0x72,(data>>24)&0xff);
  282. /* wait for the send to be completed */
  283. while ((cx24110_readreg(state,0x6d)&0xc0)==0x80)
  284. ;
  285. /* send another 8 bytes */
  286. cx24110_writereg(state,0x72,(data>>16)&0xff);
  287. while ((cx24110_readreg(state,0x6d)&0xc0)==0x80)
  288. ;
  289. /* and the topmost 5 bits of this byte */
  290. cx24110_writereg(state,0x72,(data>>8)&0xff);
  291. while ((cx24110_readreg(state,0x6d)&0xc0)==0x80)
  292. ;
  293. /* now strobe the enable line once */
  294. cx24110_writereg(state,0x6d,0x32);
  295. cx24110_writereg(state,0x6d,0x30);
  296. return 0;
  297. }
  298. static int cx24110_initfe(struct dvb_frontend* fe)
  299. {
  300. struct cx24110_state *state = fe->demodulator_priv;
  301. /* fixme (low): error handling */
  302. int i;
  303. dprintk("%s: init chip\n", __FUNCTION__);
  304. for(i=0;i<sizeof(cx24110_regdata)/sizeof(cx24110_regdata[0]);i++) {
  305. cx24110_writereg(state, cx24110_regdata[i].reg, cx24110_regdata[i].data);
  306. };
  307. if (state->config->pll_init) state->config->pll_init(fe);
  308. return 0;
  309. }
  310. static int cx24110_sleep(struct dvb_frontend *fe)
  311. {
  312. struct cx24110_state *state = fe->demodulator_priv;
  313. if (state->config->pll_sleep)
  314. return state->config->pll_sleep(fe);
  315. return 0;
  316. }
  317. static int cx24110_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage)
  318. {
  319. struct cx24110_state *state = fe->demodulator_priv;
  320. switch (voltage) {
  321. case SEC_VOLTAGE_13:
  322. return cx24110_writereg(state,0x76,(cx24110_readreg(state,0x76)&0x3b)|0xc0);
  323. case SEC_VOLTAGE_18:
  324. return cx24110_writereg(state,0x76,(cx24110_readreg(state,0x76)&0x3b)|0x40);
  325. default:
  326. return -EINVAL;
  327. };
  328. }
  329. static int cx24110_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
  330. {
  331. int rv, bit;
  332. struct cx24110_state *state = fe->demodulator_priv;
  333. unsigned long timeout;
  334. if (burst == SEC_MINI_A)
  335. bit = 0x00;
  336. else if (burst == SEC_MINI_B)
  337. bit = 0x08;
  338. else
  339. return -EINVAL;
  340. rv = cx24110_readreg(state, 0x77);
  341. if (!(rv & 0x04))
  342. cx24110_writereg(state, 0x77, rv | 0x04);
  343. rv = cx24110_readreg(state, 0x76);
  344. cx24110_writereg(state, 0x76, ((rv & 0x90) | 0x40 | bit));
  345. timeout = jiffies + msecs_to_jiffies(100);
  346. while (!time_after(jiffies, timeout) && !(cx24110_readreg(state, 0x76) & 0x40))
  347. ; /* wait for LNB ready */
  348. return 0;
  349. }
  350. static int cx24110_send_diseqc_msg(struct dvb_frontend* fe,
  351. struct dvb_diseqc_master_cmd *cmd)
  352. {
  353. int i, rv;
  354. struct cx24110_state *state = fe->demodulator_priv;
  355. unsigned long timeout;
  356. if (cmd->msg_len < 3 || cmd->msg_len > 6)
  357. return -EINVAL; /* not implemented */
  358. for (i = 0; i < cmd->msg_len; i++)
  359. cx24110_writereg(state, 0x79 + i, cmd->msg[i]);
  360. rv = cx24110_readreg(state, 0x77);
  361. if (rv & 0x04) {
  362. cx24110_writereg(state, 0x77, rv & ~0x04);
  363. msleep(30); /* reportedly fixes switching problems */
  364. }
  365. rv = cx24110_readreg(state, 0x76);
  366. cx24110_writereg(state, 0x76, ((rv & 0x90) | 0x40) | ((cmd->msg_len-3) & 3));
  367. timeout = jiffies + msecs_to_jiffies(100);
  368. while (!time_after(jiffies, timeout) && !(cx24110_readreg(state, 0x76) & 0x40))
  369. ; /* wait for LNB ready */
  370. return 0;
  371. }
  372. static int cx24110_read_status(struct dvb_frontend* fe, fe_status_t* status)
  373. {
  374. struct cx24110_state *state = fe->demodulator_priv;
  375. int sync = cx24110_readreg (state, 0x55);
  376. *status = 0;
  377. if (sync & 0x10)
  378. *status |= FE_HAS_SIGNAL;
  379. if (sync & 0x08)
  380. *status |= FE_HAS_CARRIER;
  381. sync = cx24110_readreg (state, 0x08);
  382. if (sync & 0x40)
  383. *status |= FE_HAS_VITERBI;
  384. if (sync & 0x20)
  385. *status |= FE_HAS_SYNC;
  386. if ((sync & 0x60) == 0x60)
  387. *status |= FE_HAS_LOCK;
  388. return 0;
  389. }
  390. static int cx24110_read_ber(struct dvb_frontend* fe, u32* ber)
  391. {
  392. struct cx24110_state *state = fe->demodulator_priv;
  393. /* fixme (maybe): value range is 16 bit. Scale? */
  394. if(cx24110_readreg(state,0x24)&0x10) {
  395. /* the Viterbi error counter has finished one counting window */
  396. cx24110_writereg(state,0x24,0x04); /* select the ber reg */
  397. state->lastber=cx24110_readreg(state,0x25)|
  398. (cx24110_readreg(state,0x26)<<8);
  399. cx24110_writereg(state,0x24,0x04); /* start new count window */
  400. cx24110_writereg(state,0x24,0x14);
  401. }
  402. *ber = state->lastber;
  403. return 0;
  404. }
  405. static int cx24110_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
  406. {
  407. struct cx24110_state *state = fe->demodulator_priv;
  408. /* no provision in hardware. Read the frontend AGC accumulator. No idea how to scale this, but I know it is 2s complement */
  409. u8 signal = cx24110_readreg (state, 0x27)+128;
  410. *signal_strength = (signal << 8) | signal;
  411. return 0;
  412. }
  413. static int cx24110_read_snr(struct dvb_frontend* fe, u16* snr)
  414. {
  415. struct cx24110_state *state = fe->demodulator_priv;
  416. /* no provision in hardware. Can be computed from the Es/N0 estimator, but I don't know how. */
  417. if(cx24110_readreg(state,0x6a)&0x80) {
  418. /* the Es/N0 error counter has finished one counting window */
  419. state->lastesn0=cx24110_readreg(state,0x69)|
  420. (cx24110_readreg(state,0x68)<<8);
  421. cx24110_writereg(state,0x6a,0x84); /* start new count window */
  422. }
  423. *snr = state->lastesn0;
  424. return 0;
  425. }
  426. static int cx24110_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  427. {
  428. struct cx24110_state *state = fe->demodulator_priv;
  429. u32 lastbyer;
  430. if(cx24110_readreg(state,0x10)&0x40) {
  431. /* the RS error counter has finished one counting window */
  432. cx24110_writereg(state,0x10,0x60); /* select the byer reg */
  433. lastbyer=cx24110_readreg(state,0x12)|
  434. (cx24110_readreg(state,0x13)<<8)|
  435. (cx24110_readreg(state,0x14)<<16);
  436. cx24110_writereg(state,0x10,0x70); /* select the bler reg */
  437. state->lastbler=cx24110_readreg(state,0x12)|
  438. (cx24110_readreg(state,0x13)<<8)|
  439. (cx24110_readreg(state,0x14)<<16);
  440. cx24110_writereg(state,0x10,0x20); /* start new count window */
  441. }
  442. *ucblocks = state->lastbler;
  443. return 0;
  444. }
  445. static int cx24110_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  446. {
  447. struct cx24110_state *state = fe->demodulator_priv;
  448. state->config->pll_set(fe, p);
  449. cx24110_set_inversion (state, p->inversion);
  450. cx24110_set_fec (state, p->u.qpsk.fec_inner);
  451. cx24110_set_symbolrate (state, p->u.qpsk.symbol_rate);
  452. cx24110_writereg(state,0x04,0x05); /* start aquisition */
  453. return 0;
  454. }
  455. static int cx24110_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  456. {
  457. struct cx24110_state *state = fe->demodulator_priv;
  458. s32 afc; unsigned sclk;
  459. /* cannot read back tuner settings (freq). Need to have some private storage */
  460. sclk = cx24110_readreg (state, 0x07) & 0x03;
  461. /* ok, real AFC (FEDR) freq. is afc/2^24*fsamp, fsamp=45/60/80/90MHz.
  462. * Need 64 bit arithmetic. Is thiss possible in the kernel? */
  463. if (sclk==0) sclk=90999000L/2L;
  464. else if (sclk==1) sclk=60666000L;
  465. else if (sclk==2) sclk=80888000L;
  466. else sclk=90999000L;
  467. sclk>>=8;
  468. afc = sclk*(cx24110_readreg (state, 0x44)&0x1f)+
  469. ((sclk*cx24110_readreg (state, 0x45))>>8)+
  470. ((sclk*cx24110_readreg (state, 0x46))>>16);
  471. p->frequency += afc;
  472. p->inversion = (cx24110_readreg (state, 0x22) & 0x10) ?
  473. INVERSION_ON : INVERSION_OFF;
  474. p->u.qpsk.fec_inner = cx24110_get_fec (state);
  475. return 0;
  476. }
  477. static int cx24110_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  478. {
  479. struct cx24110_state *state = fe->demodulator_priv;
  480. return cx24110_writereg(state,0x76,(cx24110_readreg(state,0x76)&~0x10)|(((tone==SEC_TONE_ON))?0x10:0));
  481. }
  482. static void cx24110_release(struct dvb_frontend* fe)
  483. {
  484. struct cx24110_state* state = fe->demodulator_priv;
  485. kfree(state);
  486. }
  487. static struct dvb_frontend_ops cx24110_ops;
  488. struct dvb_frontend* cx24110_attach(const struct cx24110_config* config,
  489. struct i2c_adapter* i2c)
  490. {
  491. struct cx24110_state* state = NULL;
  492. int ret;
  493. /* allocate memory for the internal state */
  494. state = kmalloc(sizeof(struct cx24110_state), GFP_KERNEL);
  495. if (state == NULL) goto error;
  496. /* setup the state */
  497. state->config = config;
  498. state->i2c = i2c;
  499. memcpy(&state->ops, &cx24110_ops, sizeof(struct dvb_frontend_ops));
  500. state->lastber = 0;
  501. state->lastbler = 0;
  502. state->lastesn0 = 0;
  503. /* check if the demod is there */
  504. ret = cx24110_readreg(state, 0x00);
  505. if ((ret != 0x5a) && (ret != 0x69)) goto error;
  506. /* create dvb_frontend */
  507. state->frontend.ops = &state->ops;
  508. state->frontend.demodulator_priv = state;
  509. return &state->frontend;
  510. error:
  511. kfree(state);
  512. return NULL;
  513. }
  514. static struct dvb_frontend_ops cx24110_ops = {
  515. .info = {
  516. .name = "Conexant CX24110 DVB-S",
  517. .type = FE_QPSK,
  518. .frequency_min = 950000,
  519. .frequency_max = 2150000,
  520. .frequency_stepsize = 1011, /* kHz for QPSK frontends */
  521. .frequency_tolerance = 29500,
  522. .symbol_rate_min = 1000000,
  523. .symbol_rate_max = 45000000,
  524. .caps = FE_CAN_INVERSION_AUTO |
  525. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  526. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  527. FE_CAN_QPSK | FE_CAN_RECOVER
  528. },
  529. .release = cx24110_release,
  530. .init = cx24110_initfe,
  531. .sleep = cx24110_sleep,
  532. .set_frontend = cx24110_set_frontend,
  533. .get_frontend = cx24110_get_frontend,
  534. .read_status = cx24110_read_status,
  535. .read_ber = cx24110_read_ber,
  536. .read_signal_strength = cx24110_read_signal_strength,
  537. .read_snr = cx24110_read_snr,
  538. .read_ucblocks = cx24110_read_ucblocks,
  539. .diseqc_send_master_cmd = cx24110_send_diseqc_msg,
  540. .set_tone = cx24110_set_tone,
  541. .set_voltage = cx24110_set_voltage,
  542. .diseqc_send_burst = cx24110_diseqc_send_burst,
  543. };
  544. module_param(debug, int, 0644);
  545. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  546. MODULE_DESCRIPTION("Conexant CX24110 DVB-S Demodulator driver");
  547. MODULE_AUTHOR("Peter Hettkamp");
  548. MODULE_LICENSE("GPL");
  549. EXPORT_SYMBOL(cx24110_attach);
  550. EXPORT_SYMBOL(cx24110_pll_write);