cx24123.c 22 KB

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