cx24110.c 20 KB

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