cx24123.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /*
  2. Conexant cx24123/cx24109 - DVB QPSK Satellite demod/tuner driver
  3. Copyright (C) 2005 Steven Toth <stoth@hauppauge.com>
  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/slab.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/init.h>
  21. #include "dvb_frontend.h"
  22. #include "cx24123.h"
  23. static int debug;
  24. #define dprintk(args...) \
  25. do { \
  26. if (debug) printk (KERN_DEBUG "cx24123: " args); \
  27. } while (0)
  28. struct cx24123_state
  29. {
  30. struct i2c_adapter* i2c;
  31. struct dvb_frontend_ops ops;
  32. const struct cx24123_config* config;
  33. struct dvb_frontend frontend;
  34. u32 lastber;
  35. u16 snr;
  36. u8 lnbreg;
  37. /* Some PLL specifics for tuning */
  38. u32 VCAarg;
  39. u32 VGAarg;
  40. u32 bandselectarg;
  41. u32 pllarg;
  42. /* The Demod/Tuner can't easily provide these, we cache them */
  43. u32 currentfreq;
  44. u32 currentsymbolrate;
  45. };
  46. /* Various tuner defaults need to be established for a given symbol rate Sps */
  47. static struct
  48. {
  49. u32 symbolrate_low;
  50. u32 symbolrate_high;
  51. u32 VCAslope;
  52. u32 VCAoffset;
  53. u32 VGA1offset;
  54. u32 VGA2offset;
  55. u32 VCAprogdata;
  56. u32 VGAprogdata;
  57. } cx24123_AGC_vals[] =
  58. {
  59. {
  60. .symbolrate_low = 1000000,
  61. .symbolrate_high = 4999999,
  62. .VCAslope = 0x07,
  63. .VCAoffset = 0x0f,
  64. .VGA1offset = 0x1f8,
  65. .VGA2offset = 0x1f8,
  66. .VGAprogdata = (2 << 18) | (0x1f8 << 9) | 0x1f8,
  67. .VCAprogdata = (4 << 18) | (0x07 << 9) | 0x07,
  68. },
  69. {
  70. .symbolrate_low = 5000000,
  71. .symbolrate_high = 14999999,
  72. .VCAslope = 0x1f,
  73. .VCAoffset = 0x1f,
  74. .VGA1offset = 0x1e0,
  75. .VGA2offset = 0x180,
  76. .VGAprogdata = (2 << 18) | (0x180 << 9) | 0x1e0,
  77. .VCAprogdata = (4 << 18) | (0x07 << 9) | 0x1f,
  78. },
  79. {
  80. .symbolrate_low = 15000000,
  81. .symbolrate_high = 45000000,
  82. .VCAslope = 0x3f,
  83. .VCAoffset = 0x3f,
  84. .VGA1offset = 0x180,
  85. .VGA2offset = 0x100,
  86. .VGAprogdata = (2 << 18) | (0x100 << 9) | 0x180,
  87. .VCAprogdata = (4 << 18) | (0x07 << 9) | 0x3f,
  88. },
  89. };
  90. /*
  91. * Various tuner defaults need to be established for a given frequency kHz.
  92. * fixme: The bounds on the bands do not match the doc in real life.
  93. * fixme: Some of them have been moved, other might need adjustment.
  94. */
  95. static struct
  96. {
  97. u32 freq_low;
  98. u32 freq_high;
  99. u32 bandselect;
  100. u32 VCOdivider;
  101. u32 VCOnumber;
  102. u32 progdata;
  103. } cx24123_bandselect_vals[] =
  104. {
  105. {
  106. .freq_low = 950000,
  107. .freq_high = 1018999,
  108. .bandselect = 0x40,
  109. .VCOdivider = 4,
  110. .VCOnumber = 7,
  111. .progdata = (0 << 18) | (0 << 9) | 0x40,
  112. },
  113. {
  114. .freq_low = 1019000,
  115. .freq_high = 1074999,
  116. .bandselect = 0x80,
  117. .VCOdivider = 4,
  118. .VCOnumber = 8,
  119. .progdata = (0 << 18) | (0 << 9) | 0x80,
  120. },
  121. {
  122. .freq_low = 1075000,
  123. .freq_high = 1227999,
  124. .bandselect = 0x01,
  125. .VCOdivider = 2,
  126. .VCOnumber = 1,
  127. .progdata = (0 << 18) | (1 << 9) | 0x01,
  128. },
  129. {
  130. .freq_low = 1228000,
  131. .freq_high = 1349999,
  132. .bandselect = 0x02,
  133. .VCOdivider = 2,
  134. .VCOnumber = 2,
  135. .progdata = (0 << 18) | (1 << 9) | 0x02,
  136. },
  137. {
  138. .freq_low = 1350000,
  139. .freq_high = 1481999,
  140. .bandselect = 0x04,
  141. .VCOdivider = 2,
  142. .VCOnumber = 3,
  143. .progdata = (0 << 18) | (1 << 9) | 0x04,
  144. },
  145. {
  146. .freq_low = 1482000,
  147. .freq_high = 1595999,
  148. .bandselect = 0x08,
  149. .VCOdivider = 2,
  150. .VCOnumber = 4,
  151. .progdata = (0 << 18) | (1 << 9) | 0x08,
  152. },
  153. {
  154. .freq_low = 1596000,
  155. .freq_high = 1717999,
  156. .bandselect = 0x10,
  157. .VCOdivider = 2,
  158. .VCOnumber = 5,
  159. .progdata = (0 << 18) | (1 << 9) | 0x10,
  160. },
  161. {
  162. .freq_low = 1718000,
  163. .freq_high = 1855999,
  164. .bandselect = 0x20,
  165. .VCOdivider = 2,
  166. .VCOnumber = 6,
  167. .progdata = (0 << 18) | (1 << 9) | 0x20,
  168. },
  169. {
  170. .freq_low = 1856000,
  171. .freq_high = 2035999,
  172. .bandselect = 0x40,
  173. .VCOdivider = 2,
  174. .VCOnumber = 7,
  175. .progdata = (0 << 18) | (1 << 9) | 0x40,
  176. },
  177. {
  178. .freq_low = 2036000,
  179. .freq_high = 2149999,
  180. .bandselect = 0x80,
  181. .VCOdivider = 2,
  182. .VCOnumber = 8,
  183. .progdata = (0 << 18) | (1 << 9) | 0x80,
  184. },
  185. };
  186. static struct {
  187. u8 reg;
  188. u8 data;
  189. } cx24123_regdata[] =
  190. {
  191. {0x00, 0x03}, /* Reset system */
  192. {0x00, 0x00}, /* Clear reset */
  193. {0x01, 0x3b}, /* Apply sensible defaults, from an i2c sniffer */
  194. {0x03, 0x07},
  195. {0x04, 0x10},
  196. {0x05, 0x04},
  197. {0x06, 0x31},
  198. {0x0d, 0x02},
  199. {0x0e, 0x03},
  200. {0x0f, 0xfe},
  201. {0x10, 0x01},
  202. {0x14, 0x01},
  203. {0x15, 0x98},
  204. {0x16, 0x00},
  205. {0x17, 0x01},
  206. {0x1b, 0x05},
  207. {0x1c, 0x80},
  208. {0x1d, 0x00},
  209. {0x1e, 0x00},
  210. {0x20, 0x41},
  211. {0x21, 0x15},
  212. {0x27, 0x14},
  213. {0x28, 0x46},
  214. {0x29, 0x00},
  215. {0x2a, 0xb0},
  216. {0x2b, 0x73},
  217. {0x2c, 0x00},
  218. {0x2d, 0x00},
  219. {0x2e, 0x00},
  220. {0x2f, 0x00},
  221. {0x30, 0x00},
  222. {0x31, 0x00},
  223. {0x32, 0x8c},
  224. {0x33, 0x00},
  225. {0x34, 0x00},
  226. {0x35, 0x03},
  227. {0x36, 0x02},
  228. {0x37, 0x3a},
  229. {0x3a, 0x00}, /* Enable AGC accumulator */
  230. {0x44, 0x00},
  231. {0x45, 0x00},
  232. {0x46, 0x05},
  233. {0x56, 0x41},
  234. {0x57, 0xff},
  235. {0x67, 0x83},
  236. };
  237. static int cx24123_writereg(struct cx24123_state* state, int reg, int data)
  238. {
  239. u8 buf[] = { reg, data };
  240. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
  241. int err;
  242. if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
  243. printk("%s: writereg error(err == %i, reg == 0x%02x,"
  244. " data == 0x%02x)\n", __FUNCTION__, err, reg, data);
  245. return -EREMOTEIO;
  246. }
  247. return 0;
  248. }
  249. static int cx24123_writelnbreg(struct cx24123_state* state, int reg, int data)
  250. {
  251. u8 buf[] = { reg, data };
  252. /* fixme: put the intersil addr int the config */
  253. struct i2c_msg msg = { .addr = 0x08, .flags = 0, .buf = buf, .len = 2 };
  254. int err;
  255. if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
  256. printk("%s: writelnbreg error (err == %i, reg == 0x%02x,"
  257. " data == 0x%02x)\n", __FUNCTION__, err, reg, data);
  258. return -EREMOTEIO;
  259. }
  260. /* cache the write, no way to read back */
  261. state->lnbreg = data;
  262. return 0;
  263. }
  264. static int cx24123_readreg(struct cx24123_state* state, u8 reg)
  265. {
  266. int ret;
  267. u8 b0[] = { reg };
  268. u8 b1[] = { 0 };
  269. struct i2c_msg msg[] = {
  270. { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
  271. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 }
  272. };
  273. ret = i2c_transfer(state->i2c, msg, 2);
  274. if (ret != 2) {
  275. printk("%s: reg=0x%x (error=%d)\n", __FUNCTION__, reg, ret);
  276. return ret;
  277. }
  278. return b1[0];
  279. }
  280. static int cx24123_readlnbreg(struct cx24123_state* state, u8 reg)
  281. {
  282. return state->lnbreg;
  283. }
  284. static int cx24123_set_inversion(struct cx24123_state* state, fe_spectral_inversion_t inversion)
  285. {
  286. switch (inversion) {
  287. case INVERSION_OFF:
  288. cx24123_writereg(state, 0x0e, cx24123_readreg(state, 0x0e) & 0x7f);
  289. cx24123_writereg(state, 0x10, cx24123_readreg(state, 0x10) | 0x80);
  290. break;
  291. case INVERSION_ON:
  292. cx24123_writereg(state, 0x0e, cx24123_readreg(state, 0x0e) | 0x80);
  293. cx24123_writereg(state, 0x10, cx24123_readreg(state, 0x10) | 0x80);
  294. break;
  295. case INVERSION_AUTO:
  296. cx24123_writereg(state, 0x10, cx24123_readreg(state, 0x10) & 0x7f);
  297. break;
  298. default:
  299. return -EINVAL;
  300. }
  301. return 0;
  302. }
  303. static int cx24123_get_inversion(struct cx24123_state* state, fe_spectral_inversion_t *inversion)
  304. {
  305. u8 val;
  306. val = cx24123_readreg(state, 0x1b) >> 7;
  307. if (val == 0)
  308. *inversion = INVERSION_OFF;
  309. else
  310. *inversion = INVERSION_ON;
  311. return 0;
  312. }
  313. static int cx24123_set_fec(struct cx24123_state* state, fe_code_rate_t fec)
  314. {
  315. if ( (fec < FEC_NONE) || (fec > FEC_AUTO) )
  316. fec = FEC_AUTO;
  317. /* Hardware has 5/11 and 3/5 but are never unused */
  318. switch (fec) {
  319. case FEC_NONE:
  320. return cx24123_writereg(state, 0x0f, 0x01);
  321. case FEC_1_2:
  322. return cx24123_writereg(state, 0x0f, 0x02);
  323. case FEC_2_3:
  324. return cx24123_writereg(state, 0x0f, 0x04);
  325. case FEC_3_4:
  326. return cx24123_writereg(state, 0x0f, 0x08);
  327. case FEC_5_6:
  328. return cx24123_writereg(state, 0x0f, 0x20);
  329. case FEC_7_8:
  330. return cx24123_writereg(state, 0x0f, 0x80);
  331. case FEC_AUTO:
  332. return cx24123_writereg(state, 0x0f, 0xae);
  333. default:
  334. return -EOPNOTSUPP;
  335. }
  336. }
  337. static int cx24123_get_fec(struct cx24123_state* state, fe_code_rate_t *fec)
  338. {
  339. int ret;
  340. u8 val;
  341. ret = cx24123_readreg (state, 0x1b);
  342. if (ret < 0)
  343. return ret;
  344. val = ret & 0x07;
  345. switch (val) {
  346. case 1:
  347. *fec = FEC_1_2;
  348. break;
  349. case 3:
  350. *fec = FEC_2_3;
  351. break;
  352. case 4:
  353. *fec = FEC_3_4;
  354. break;
  355. case 5:
  356. *fec = FEC_4_5;
  357. break;
  358. case 6:
  359. *fec = FEC_5_6;
  360. break;
  361. case 7:
  362. *fec = FEC_7_8;
  363. break;
  364. case 2: /* *fec = FEC_3_5; break; */
  365. case 0: /* *fec = FEC_5_11; break; */
  366. *fec = FEC_AUTO;
  367. break;
  368. default:
  369. *fec = FEC_NONE; // can't happen
  370. }
  371. return 0;
  372. }
  373. /* fixme: Symbol rates < 3MSps may not work because of precision loss */
  374. static int cx24123_set_symbolrate(struct cx24123_state* state, u32 srate)
  375. {
  376. u32 val;
  377. val = (srate / 1185) * 100;
  378. /* Compensate for scaling up, by removing 17 symbols per 1Msps */
  379. val = val - (17 * (srate / 1000000));
  380. cx24123_writereg(state, 0x08, (val >> 16) & 0xff );
  381. cx24123_writereg(state, 0x09, (val >> 8) & 0xff );
  382. cx24123_writereg(state, 0x0a, (val ) & 0xff );
  383. return 0;
  384. }
  385. /*
  386. * Based on the required frequency and symbolrate, the tuner AGC has to be configured
  387. * and the correct band selected. Calculate those values
  388. */
  389. static int cx24123_pll_calculate(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  390. {
  391. struct cx24123_state *state = fe->demodulator_priv;
  392. u32 ndiv = 0, adiv = 0, vco_div = 0;
  393. int i = 0;
  394. /* Defaults for low freq, low rate */
  395. state->VCAarg = cx24123_AGC_vals[0].VCAprogdata;
  396. state->VGAarg = cx24123_AGC_vals[0].VGAprogdata;
  397. state->bandselectarg = cx24123_bandselect_vals[0].progdata;
  398. vco_div = cx24123_bandselect_vals[0].VCOdivider;
  399. /* For the given symbolerate, determine the VCA and VGA programming bits */
  400. for (i = 0; i < sizeof(cx24123_AGC_vals) / sizeof(cx24123_AGC_vals[0]); i++)
  401. {
  402. if ((cx24123_AGC_vals[i].symbolrate_low <= p->u.qpsk.symbol_rate) &&
  403. (cx24123_AGC_vals[i].symbolrate_high >= p->u.qpsk.symbol_rate) ) {
  404. state->VCAarg = cx24123_AGC_vals[i].VCAprogdata;
  405. state->VGAarg = cx24123_AGC_vals[i].VGAprogdata;
  406. }
  407. }
  408. /* For the given frequency, determine the bandselect programming bits */
  409. for (i = 0; i < sizeof(cx24123_bandselect_vals) / sizeof(cx24123_bandselect_vals[0]); i++)
  410. {
  411. if ((cx24123_bandselect_vals[i].freq_low <= p->frequency) &&
  412. (cx24123_bandselect_vals[i].freq_high >= p->frequency) ) {
  413. state->bandselectarg = cx24123_bandselect_vals[i].progdata;
  414. vco_div = cx24123_bandselect_vals[i].VCOdivider;
  415. }
  416. }
  417. /* Determine the N/A dividers for the requested lband freq (in kHz). */
  418. /* Note: 10111 (kHz) is the Crystal Freq and divider of 10. */
  419. ndiv = ( ((p->frequency * vco_div) / (10111 / 10) / 2) / 32) & 0x1ff;
  420. adiv = ( ((p->frequency * vco_div) / (10111 / 10) / 2) % 32) & 0x1f;
  421. if (adiv == 0)
  422. adiv++;
  423. /* determine the correct pll frequency values. */
  424. /* Command 11, refdiv 11, cpump polarity 1, cpump current 3mA 10. */
  425. state->pllarg = (3 << 19) | (3 << 17) | (1 << 16) | (2 << 14);
  426. state->pllarg |= (ndiv << 5) | adiv;
  427. return 0;
  428. }
  429. /*
  430. * Tuner data is 21 bits long, must be left-aligned in data.
  431. * Tuner cx24109 is written through a dedicated 3wire interface on the demod chip.
  432. */
  433. static int cx24123_pll_writereg(struct dvb_frontend* fe, struct dvb_frontend_parameters *p, u32 data)
  434. {
  435. struct cx24123_state *state = fe->demodulator_priv;
  436. u8 timeout = 0;
  437. /* align the 21 bytes into to bit23 boundary */
  438. data = data << 3;
  439. /* Reset the demod pll word length to 0x15 bits */
  440. cx24123_writereg(state, 0x21, 0x15);
  441. timeout = 0;
  442. /* write the msb 8 bits, wait for the send to be completed */
  443. cx24123_writereg(state, 0x22, (data >> 16) & 0xff);
  444. while ( ( cx24123_readreg(state, 0x20) & 0x40 ) == 0 )
  445. {
  446. /* Safety - No reason why the write should not complete, and we never get here, avoid hang */
  447. if (timeout++ >= 4) {
  448. printk("%s: demodulator is no longer responding, aborting.\n",__FUNCTION__);
  449. return -EREMOTEIO;
  450. }
  451. msleep(500);
  452. }
  453. timeout = 0;
  454. /* send another 8 bytes, wait for the send to be completed */
  455. cx24123_writereg(state, 0x22, (data>>8) & 0xff );
  456. while ( (cx24123_readreg(state, 0x20) & 0x40 ) == 0 )
  457. {
  458. /* Safety - No reason why the write should not complete, and we never get here, avoid hang */
  459. if (timeout++ >= 4) {
  460. printk("%s: demodulator is not responding, possibly hung, aborting.\n",__FUNCTION__);
  461. return -EREMOTEIO;
  462. }
  463. msleep(500);
  464. }
  465. timeout = 0;
  466. /* send the lower 5 bits of this byte, padded with 3 LBB, wait for the send to be completed */
  467. cx24123_writereg(state, 0x22, (data) & 0xff );
  468. while ((cx24123_readreg(state, 0x20) & 0x80))
  469. {
  470. /* Safety - No reason why the write should not complete, and we never get here, avoid hang */
  471. if (timeout++ >= 4) {
  472. printk("%s: demodulator is not responding, possibly hung, aborting.\n",__FUNCTION__);
  473. return -EREMOTEIO;
  474. }
  475. msleep(500);
  476. }
  477. /* Trigger the demod to configure the tuner */
  478. cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) | 2);
  479. cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) & 0xfd);
  480. return 0;
  481. }
  482. static int cx24123_pll_tune(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  483. {
  484. struct cx24123_state *state = fe->demodulator_priv;
  485. if (cx24123_pll_calculate(fe, p) != 0) {
  486. printk("%s: cx24123_pll_calcutate failed\n",__FUNCTION__);
  487. return -EINVAL;
  488. }
  489. /* Write the new VCO/VGA */
  490. cx24123_pll_writereg(fe, p, state->VCAarg);
  491. cx24123_pll_writereg(fe, p, state->VGAarg);
  492. /* Write the new bandselect and pll args */
  493. cx24123_pll_writereg(fe, p, state->bandselectarg);
  494. cx24123_pll_writereg(fe, p, state->pllarg);
  495. return 0;
  496. }
  497. static int cx24123_initfe(struct dvb_frontend* fe)
  498. {
  499. struct cx24123_state *state = fe->demodulator_priv;
  500. int i;
  501. /* Configure the demod to a good set of defaults */
  502. for (i = 0; i < sizeof(cx24123_regdata) / sizeof(cx24123_regdata[0]); i++)
  503. cx24123_writereg(state, cx24123_regdata[i].reg, cx24123_regdata[i].data);
  504. if (state->config->pll_init)
  505. state->config->pll_init(fe);
  506. /* Configure the LNB for 14V */
  507. cx24123_writelnbreg(state, 0x0, 0x2a);
  508. return 0;
  509. }
  510. static int cx24123_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
  511. {
  512. struct cx24123_state *state = fe->demodulator_priv;
  513. u8 val;
  514. val = cx24123_readlnbreg(state, 0x0);
  515. switch (voltage) {
  516. case SEC_VOLTAGE_13:
  517. return cx24123_writelnbreg(state, 0x0, val & 0x32); /* V 13v */
  518. case SEC_VOLTAGE_18:
  519. return cx24123_writelnbreg(state, 0x0, val | 0x04); /* H 18v */
  520. case SEC_VOLTAGE_OFF:
  521. return cx24123_writelnbreg(state, 0x0, val & 0x30);
  522. default:
  523. return -EINVAL;
  524. };
  525. }
  526. static int cx24123_send_diseqc_msg(struct dvb_frontend* fe,
  527. struct dvb_diseqc_master_cmd *cmd)
  528. {
  529. /* fixme: Implement diseqc */
  530. printk("%s: No support yet\n",__FUNCTION__);
  531. return -ENOTSUPP;
  532. }
  533. static int cx24123_read_status(struct dvb_frontend* fe, fe_status_t* status)
  534. {
  535. struct cx24123_state *state = fe->demodulator_priv;
  536. int sync = cx24123_readreg(state, 0x14);
  537. int lock = cx24123_readreg(state, 0x20);
  538. *status = 0;
  539. if (lock & 0x01)
  540. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  541. if (sync & 0x04)
  542. *status |= FE_HAS_VITERBI;
  543. if (sync & 0x08)
  544. *status |= FE_HAS_CARRIER;
  545. if (sync & 0x80)
  546. *status |= FE_HAS_SYNC | FE_HAS_LOCK;
  547. return 0;
  548. }
  549. /*
  550. * Configured to return the measurement of errors in blocks, because no UCBLOCKS value
  551. * is available, so this value doubles up to satisfy both measurements
  552. */
  553. static int cx24123_read_ber(struct dvb_frontend* fe, u32* ber)
  554. {
  555. struct cx24123_state *state = fe->demodulator_priv;
  556. state->lastber =
  557. ((cx24123_readreg(state, 0x1c) & 0x3f) << 16) |
  558. (cx24123_readreg(state, 0x1d) << 8 |
  559. cx24123_readreg(state, 0x1e));
  560. /* Do the signal quality processing here, it's derived from the BER. */
  561. /* Scale the BER from a 24bit to a SNR 16 bit where higher = better */
  562. if (state->lastber < 5000)
  563. state->snr = 655*100;
  564. else if ( (state->lastber >= 5000) && (state->lastber < 55000) )
  565. state->snr = 655*90;
  566. else if ( (state->lastber >= 55000) && (state->lastber < 150000) )
  567. state->snr = 655*80;
  568. else if ( (state->lastber >= 150000) && (state->lastber < 250000) )
  569. state->snr = 655*70;
  570. else if ( (state->lastber >= 250000) && (state->lastber < 450000) )
  571. state->snr = 655*65;
  572. else
  573. state->snr = 0;
  574. *ber = state->lastber;
  575. return 0;
  576. }
  577. static int cx24123_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
  578. {
  579. struct cx24123_state *state = fe->demodulator_priv;
  580. *signal_strength = cx24123_readreg(state, 0x3b) << 8; /* larger = better */
  581. return 0;
  582. }
  583. static int cx24123_read_snr(struct dvb_frontend* fe, u16* snr)
  584. {
  585. struct cx24123_state *state = fe->demodulator_priv;
  586. *snr = state->snr;
  587. return 0;
  588. }
  589. static int cx24123_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  590. {
  591. struct cx24123_state *state = fe->demodulator_priv;
  592. *ucblocks = state->lastber;
  593. return 0;
  594. }
  595. static int cx24123_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  596. {
  597. struct cx24123_state *state = fe->demodulator_priv;
  598. if (state->config->set_ts_params)
  599. state->config->set_ts_params(fe, 0);
  600. state->currentfreq=p->frequency;
  601. state->currentsymbolrate = p->u.qpsk.symbol_rate;
  602. cx24123_set_inversion(state, p->inversion);
  603. cx24123_set_fec(state, p->u.qpsk.fec_inner);
  604. cx24123_set_symbolrate(state, p->u.qpsk.symbol_rate);
  605. cx24123_pll_tune(fe, p);
  606. /* Enable automatic aquisition and reset cycle */
  607. cx24123_writereg(state, 0x03, (cx24123_readreg(state, 0x03) | 0x07));
  608. cx24123_writereg(state, 0x00, 0x10);
  609. cx24123_writereg(state, 0x00, 0);
  610. return 0;
  611. }
  612. static int cx24123_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  613. {
  614. struct cx24123_state *state = fe->demodulator_priv;
  615. if (cx24123_get_inversion(state, &p->inversion) != 0) {
  616. printk("%s: Failed to get inversion status\n",__FUNCTION__);
  617. return -EREMOTEIO;
  618. }
  619. if (cx24123_get_fec(state, &p->u.qpsk.fec_inner) != 0) {
  620. printk("%s: Failed to get fec status\n",__FUNCTION__);
  621. return -EREMOTEIO;
  622. }
  623. p->frequency = state->currentfreq;
  624. p->u.qpsk.symbol_rate = state->currentsymbolrate;
  625. return 0;
  626. }
  627. static int cx24123_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  628. {
  629. struct cx24123_state *state = fe->demodulator_priv;
  630. u8 val;
  631. val = cx24123_readlnbreg(state, 0x0);
  632. switch (tone) {
  633. case SEC_TONE_ON:
  634. return cx24123_writelnbreg(state, 0x0, val | 0x10);
  635. case SEC_TONE_OFF:
  636. return cx24123_writelnbreg(state, 0x0, val & 0x2f);
  637. default:
  638. printk("%s: CASE reached default with tone=%d\n", __FUNCTION__, tone);
  639. return -EINVAL;
  640. }
  641. }
  642. static void cx24123_release(struct dvb_frontend* fe)
  643. {
  644. struct cx24123_state* state = fe->demodulator_priv;
  645. dprintk("%s\n",__FUNCTION__);
  646. kfree(state);
  647. }
  648. static struct dvb_frontend_ops cx24123_ops;
  649. struct dvb_frontend* cx24123_attach(const struct cx24123_config* config,
  650. struct i2c_adapter* i2c)
  651. {
  652. struct cx24123_state* state = NULL;
  653. int ret;
  654. dprintk("%s\n",__FUNCTION__);
  655. /* allocate memory for the internal state */
  656. state = kmalloc(sizeof(struct cx24123_state), GFP_KERNEL);
  657. if (state == NULL) {
  658. printk("Unable to kmalloc\n");
  659. goto error;
  660. }
  661. /* setup the state */
  662. state->config = config;
  663. state->i2c = i2c;
  664. memcpy(&state->ops, &cx24123_ops, sizeof(struct dvb_frontend_ops));
  665. state->lastber = 0;
  666. state->snr = 0;
  667. state->lnbreg = 0;
  668. state->VCAarg = 0;
  669. state->VGAarg = 0;
  670. state->bandselectarg = 0;
  671. state->pllarg = 0;
  672. state->currentfreq = 0;
  673. state->currentsymbolrate = 0;
  674. /* check if the demod is there */
  675. ret = cx24123_readreg(state, 0x00);
  676. if ((ret != 0xd1) && (ret != 0xe1)) {
  677. printk("Version != d1 or e1\n");
  678. goto error;
  679. }
  680. /* create dvb_frontend */
  681. state->frontend.ops = &state->ops;
  682. state->frontend.demodulator_priv = state;
  683. return &state->frontend;
  684. error:
  685. kfree(state);
  686. return NULL;
  687. }
  688. static struct dvb_frontend_ops cx24123_ops = {
  689. .info = {
  690. .name = "Conexant CX24123/CX24109",
  691. .type = FE_QPSK,
  692. .frequency_min = 950000,
  693. .frequency_max = 2150000,
  694. .frequency_stepsize = 1011, /* kHz for QPSK frontends */
  695. .frequency_tolerance = 29500,
  696. .symbol_rate_min = 1000000,
  697. .symbol_rate_max = 45000000,
  698. .caps = FE_CAN_INVERSION_AUTO |
  699. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  700. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  701. FE_CAN_QPSK | FE_CAN_RECOVER
  702. },
  703. .release = cx24123_release,
  704. .init = cx24123_initfe,
  705. .set_frontend = cx24123_set_frontend,
  706. .get_frontend = cx24123_get_frontend,
  707. .read_status = cx24123_read_status,
  708. .read_ber = cx24123_read_ber,
  709. .read_signal_strength = cx24123_read_signal_strength,
  710. .read_snr = cx24123_read_snr,
  711. .read_ucblocks = cx24123_read_ucblocks,
  712. .diseqc_send_master_cmd = cx24123_send_diseqc_msg,
  713. .set_tone = cx24123_set_tone,
  714. .set_voltage = cx24123_set_voltage,
  715. };
  716. module_param(debug, int, 0644);
  717. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  718. MODULE_DESCRIPTION("DVB Frontend module for Conexant cx24123/cx24109 hardware");
  719. MODULE_AUTHOR("Steven Toth");
  720. MODULE_LICENSE("GPL");
  721. EXPORT_SYMBOL(cx24123_attach);