si21xx.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /* DVB compliant Linux driver for the DVB-S si2109/2110 demodulator
  2. *
  3. * Copyright (C) 2008 Igor M. Liplianin (liplianin@me.by)
  4. *
  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. *
  10. */
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/string.h>
  15. #include <linux/slab.h>
  16. #include <linux/jiffies.h>
  17. #include <asm/div64.h>
  18. #include "dvb_frontend.h"
  19. #include "si21xx.h"
  20. #define REVISION_REG 0x00
  21. #define SYSTEM_MODE_REG 0x01
  22. #define TS_CTRL_REG_1 0x02
  23. #define TS_CTRL_REG_2 0x03
  24. #define PIN_CTRL_REG_1 0x04
  25. #define PIN_CTRL_REG_2 0x05
  26. #define LOCK_STATUS_REG_1 0x0f
  27. #define LOCK_STATUS_REG_2 0x10
  28. #define ACQ_STATUS_REG 0x11
  29. #define ACQ_CTRL_REG_1 0x13
  30. #define ACQ_CTRL_REG_2 0x14
  31. #define PLL_DIVISOR_REG 0x15
  32. #define COARSE_TUNE_REG 0x16
  33. #define FINE_TUNE_REG_L 0x17
  34. #define FINE_TUNE_REG_H 0x18
  35. #define ANALOG_AGC_POWER_LEVEL_REG 0x28
  36. #define CFO_ESTIMATOR_CTRL_REG_1 0x29
  37. #define CFO_ESTIMATOR_CTRL_REG_2 0x2a
  38. #define CFO_ESTIMATOR_CTRL_REG_3 0x2b
  39. #define SYM_RATE_ESTIMATE_REG_L 0x31
  40. #define SYM_RATE_ESTIMATE_REG_M 0x32
  41. #define SYM_RATE_ESTIMATE_REG_H 0x33
  42. #define CFO_ESTIMATOR_OFFSET_REG_L 0x36
  43. #define CFO_ESTIMATOR_OFFSET_REG_H 0x37
  44. #define CFO_ERROR_REG_L 0x38
  45. #define CFO_ERROR_REG_H 0x39
  46. #define SYM_RATE_ESTIMATOR_CTRL_REG 0x3a
  47. #define SYM_RATE_REG_L 0x3f
  48. #define SYM_RATE_REG_M 0x40
  49. #define SYM_RATE_REG_H 0x41
  50. #define SYM_RATE_ESTIMATOR_MAXIMUM_REG 0x42
  51. #define SYM_RATE_ESTIMATOR_MINIMUM_REG 0x43
  52. #define C_N_ESTIMATOR_CTRL_REG 0x7c
  53. #define C_N_ESTIMATOR_THRSHLD_REG 0x7d
  54. #define C_N_ESTIMATOR_LEVEL_REG_L 0x7e
  55. #define C_N_ESTIMATOR_LEVEL_REG_H 0x7f
  56. #define BLIND_SCAN_CTRL_REG 0x80
  57. #define LSA_CTRL_REG_1 0x8D
  58. #define SPCTRM_TILT_CORR_THRSHLD_REG 0x8f
  59. #define ONE_DB_BNDWDTH_THRSHLD_REG 0x90
  60. #define TWO_DB_BNDWDTH_THRSHLD_REG 0x91
  61. #define THREE_DB_BNDWDTH_THRSHLD_REG 0x92
  62. #define INBAND_POWER_THRSHLD_REG 0x93
  63. #define REF_NOISE_LVL_MRGN_THRSHLD_REG 0x94
  64. #define VIT_SRCH_CTRL_REG_1 0xa0
  65. #define VIT_SRCH_CTRL_REG_2 0xa1
  66. #define VIT_SRCH_CTRL_REG_3 0xa2
  67. #define VIT_SRCH_STATUS_REG 0xa3
  68. #define VITERBI_BER_COUNT_REG_L 0xab
  69. #define REED_SOLOMON_CTRL_REG 0xb0
  70. #define REED_SOLOMON_ERROR_COUNT_REG_L 0xb1
  71. #define PRBS_CTRL_REG 0xb5
  72. #define LNB_CTRL_REG_1 0xc0
  73. #define LNB_CTRL_REG_2 0xc1
  74. #define LNB_CTRL_REG_3 0xc2
  75. #define LNB_CTRL_REG_4 0xc3
  76. #define LNB_CTRL_STATUS_REG 0xc4
  77. #define LNB_FIFO_REGS_0 0xc5
  78. #define LNB_FIFO_REGS_1 0xc6
  79. #define LNB_FIFO_REGS_2 0xc7
  80. #define LNB_FIFO_REGS_3 0xc8
  81. #define LNB_FIFO_REGS_4 0xc9
  82. #define LNB_FIFO_REGS_5 0xca
  83. #define LNB_SUPPLY_CTRL_REG_1 0xcb
  84. #define LNB_SUPPLY_CTRL_REG_2 0xcc
  85. #define LNB_SUPPLY_CTRL_REG_3 0xcd
  86. #define LNB_SUPPLY_CTRL_REG_4 0xce
  87. #define LNB_SUPPLY_STATUS_REG 0xcf
  88. #define FAIL -1
  89. #define PASS 0
  90. #define ALLOWABLE_FS_COUNT 10
  91. #define STATUS_BER 0
  92. #define STATUS_UCBLOCKS 1
  93. static int debug;
  94. #define dprintk(args...) \
  95. do { \
  96. if (debug) \
  97. printk(KERN_DEBUG "si21xx: " args); \
  98. } while (0)
  99. enum {
  100. ACTIVE_HIGH,
  101. ACTIVE_LOW
  102. };
  103. enum {
  104. BYTE_WIDE,
  105. BIT_WIDE
  106. };
  107. enum {
  108. CLK_GAPPED_MODE,
  109. CLK_CONTINUOUS_MODE
  110. };
  111. enum {
  112. RISING_EDGE,
  113. FALLING_EDGE
  114. };
  115. enum {
  116. MSB_FIRST,
  117. LSB_FIRST
  118. };
  119. enum {
  120. SERIAL,
  121. PARALLEL
  122. };
  123. struct si21xx_state {
  124. struct i2c_adapter *i2c;
  125. const struct si21xx_config *config;
  126. struct dvb_frontend frontend;
  127. u8 initialised:1;
  128. int errmode;
  129. int fs; /*Sampling rate of the ADC in MHz*/
  130. };
  131. /* register default initialization */
  132. static u8 serit_sp1511lhb_inittab[] = {
  133. 0x01, 0x28, /* set i2c_inc_disable */
  134. 0x20, 0x03,
  135. 0x27, 0x20,
  136. 0xe0, 0x45,
  137. 0xe1, 0x08,
  138. 0xfe, 0x01,
  139. 0x01, 0x28,
  140. 0x89, 0x09,
  141. 0x04, 0x80,
  142. 0x05, 0x01,
  143. 0x06, 0x00,
  144. 0x20, 0x03,
  145. 0x24, 0x88,
  146. 0x29, 0x09,
  147. 0x2a, 0x0f,
  148. 0x2c, 0x10,
  149. 0x2d, 0x19,
  150. 0x2e, 0x08,
  151. 0x2f, 0x10,
  152. 0x30, 0x19,
  153. 0x34, 0x20,
  154. 0x35, 0x03,
  155. 0x45, 0x02,
  156. 0x46, 0x45,
  157. 0x47, 0xd0,
  158. 0x48, 0x00,
  159. 0x49, 0x40,
  160. 0x4a, 0x03,
  161. 0x4c, 0xfd,
  162. 0x4f, 0x2e,
  163. 0x50, 0x2e,
  164. 0x51, 0x10,
  165. 0x52, 0x10,
  166. 0x56, 0x92,
  167. 0x59, 0x00,
  168. 0x5a, 0x2d,
  169. 0x5b, 0x33,
  170. 0x5c, 0x1f,
  171. 0x5f, 0x76,
  172. 0x62, 0xc0,
  173. 0x63, 0xc0,
  174. 0x64, 0xf3,
  175. 0x65, 0xf3,
  176. 0x79, 0x40,
  177. 0x6a, 0x40,
  178. 0x6b, 0x0a,
  179. 0x6c, 0x80,
  180. 0x6d, 0x27,
  181. 0x71, 0x06,
  182. 0x75, 0x60,
  183. 0x78, 0x00,
  184. 0x79, 0xb5,
  185. 0x7c, 0x05,
  186. 0x7d, 0x1a,
  187. 0x87, 0x55,
  188. 0x88, 0x72,
  189. 0x8f, 0x08,
  190. 0x90, 0xe0,
  191. 0x94, 0x40,
  192. 0xa0, 0x3f,
  193. 0xa1, 0xc0,
  194. 0xa4, 0xcc,
  195. 0xa5, 0x66,
  196. 0xa6, 0x66,
  197. 0xa7, 0x7b,
  198. 0xa8, 0x7b,
  199. 0xa9, 0x7b,
  200. 0xaa, 0x9a,
  201. 0xed, 0x04,
  202. 0xad, 0x00,
  203. 0xae, 0x03,
  204. 0xcc, 0xab,
  205. 0x01, 0x08,
  206. 0xff, 0xff
  207. };
  208. /* low level read/writes */
  209. static int si21_writeregs(struct si21xx_state *state, u8 reg1,
  210. u8 *data, int len)
  211. {
  212. int ret;
  213. u8 buf[60];/* = { reg1, data };*/
  214. struct i2c_msg msg = {
  215. .addr = state->config->demod_address,
  216. .flags = 0,
  217. .buf = buf,
  218. .len = len + 1
  219. };
  220. msg.buf[0] = reg1;
  221. memcpy(msg.buf + 1, data, len);
  222. ret = i2c_transfer(state->i2c, &msg, 1);
  223. if (ret != 1)
  224. dprintk("%s: writereg error (reg1 == 0x%02x, data == 0x%02x, "
  225. "ret == %i)\n", __func__, reg1, data[0], ret);
  226. return (ret != 1) ? -EREMOTEIO : 0;
  227. }
  228. static int si21_writereg(struct si21xx_state *state, u8 reg, u8 data)
  229. {
  230. int ret;
  231. u8 buf[] = { reg, data };
  232. struct i2c_msg msg = {
  233. .addr = state->config->demod_address,
  234. .flags = 0,
  235. .buf = buf,
  236. .len = 2
  237. };
  238. ret = i2c_transfer(state->i2c, &msg, 1);
  239. if (ret != 1)
  240. dprintk("%s: writereg error (reg == 0x%02x, data == 0x%02x, "
  241. "ret == %i)\n", __func__, reg, data, ret);
  242. return (ret != 1) ? -EREMOTEIO : 0;
  243. }
  244. static int si21_write(struct dvb_frontend *fe, const u8 buf[], int len)
  245. {
  246. struct si21xx_state *state = fe->demodulator_priv;
  247. if (len != 2)
  248. return -EINVAL;
  249. return si21_writereg(state, buf[0], buf[1]);
  250. }
  251. static u8 si21_readreg(struct si21xx_state *state, u8 reg)
  252. {
  253. int ret;
  254. u8 b0[] = { reg };
  255. u8 b1[] = { 0 };
  256. struct i2c_msg msg[] = {
  257. {
  258. .addr = state->config->demod_address,
  259. .flags = 0,
  260. .buf = b0,
  261. .len = 1
  262. }, {
  263. .addr = state->config->demod_address,
  264. .flags = I2C_M_RD,
  265. .buf = b1,
  266. .len = 1
  267. }
  268. };
  269. ret = i2c_transfer(state->i2c, msg, 2);
  270. if (ret != 2)
  271. dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
  272. __func__, reg, ret);
  273. return b1[0];
  274. }
  275. static int si21_readregs(struct si21xx_state *state, u8 reg1, u8 *b, u8 len)
  276. {
  277. int ret;
  278. struct i2c_msg msg[] = {
  279. {
  280. .addr = state->config->demod_address,
  281. .flags = 0,
  282. .buf = &reg1,
  283. .len = 1
  284. }, {
  285. .addr = state->config->demod_address,
  286. .flags = I2C_M_RD,
  287. .buf = b,
  288. .len = len
  289. }
  290. };
  291. ret = i2c_transfer(state->i2c, msg, 2);
  292. if (ret != 2)
  293. dprintk("%s: readreg error (ret == %i)\n", __func__, ret);
  294. return ret == 2 ? 0 : -1;
  295. }
  296. static int si21xx_wait_diseqc_idle(struct si21xx_state *state, int timeout)
  297. {
  298. unsigned long start = jiffies;
  299. dprintk("%s\n", __func__);
  300. while ((si21_readreg(state, LNB_CTRL_REG_1) & 0x8) == 8) {
  301. if (jiffies - start > timeout) {
  302. dprintk("%s: timeout!!\n", __func__);
  303. return -ETIMEDOUT;
  304. }
  305. msleep(10);
  306. };
  307. return 0;
  308. }
  309. static int si21xx_set_symbolrate(struct dvb_frontend *fe, u32 srate)
  310. {
  311. struct si21xx_state *state = fe->demodulator_priv;
  312. u32 sym_rate, data_rate;
  313. int i;
  314. u8 sym_rate_bytes[3];
  315. dprintk("%s : srate = %i\n", __func__ , srate);
  316. if ((srate < 1000000) || (srate > 45000000))
  317. return -EINVAL;
  318. data_rate = srate;
  319. sym_rate = 0;
  320. for (i = 0; i < 4; ++i) {
  321. sym_rate /= 100;
  322. sym_rate = sym_rate + ((data_rate % 100) * 0x800000) /
  323. state->fs;
  324. data_rate /= 100;
  325. }
  326. for (i = 0; i < 3; ++i)
  327. sym_rate_bytes[i] = (u8)((sym_rate >> (i * 8)) & 0xff);
  328. si21_writeregs(state, SYM_RATE_REG_L, sym_rate_bytes, 0x03);
  329. return 0;
  330. }
  331. static int si21xx_send_diseqc_msg(struct dvb_frontend *fe,
  332. struct dvb_diseqc_master_cmd *m)
  333. {
  334. struct si21xx_state *state = fe->demodulator_priv;
  335. u8 lnb_status;
  336. u8 LNB_CTRL_1;
  337. int status;
  338. dprintk("%s\n", __func__);
  339. status = PASS;
  340. LNB_CTRL_1 = 0;
  341. status |= si21_readregs(state, LNB_CTRL_STATUS_REG, &lnb_status, 0x01);
  342. status |= si21_readregs(state, LNB_CTRL_REG_1, &lnb_status, 0x01);
  343. /*fill the FIFO*/
  344. status |= si21_writeregs(state, LNB_FIFO_REGS_0, m->msg, m->msg_len);
  345. LNB_CTRL_1 = (lnb_status & 0x70);
  346. LNB_CTRL_1 |= m->msg_len;
  347. LNB_CTRL_1 |= 0x80; /* begin LNB signaling */
  348. status |= si21_writeregs(state, LNB_CTRL_REG_1, &LNB_CTRL_1, 0x01);
  349. return status;
  350. }
  351. static int si21xx_send_diseqc_burst(struct dvb_frontend *fe,
  352. fe_sec_mini_cmd_t burst)
  353. {
  354. struct si21xx_state *state = fe->demodulator_priv;
  355. u8 val;
  356. dprintk("%s\n", __func__);
  357. if (si21xx_wait_diseqc_idle(state, 100) < 0)
  358. return -ETIMEDOUT;
  359. val = (0x80 | si21_readreg(state, 0xc1));
  360. if (si21_writereg(state, LNB_CTRL_REG_1,
  361. burst == SEC_MINI_A ? (val & ~0x10) : (val | 0x10)))
  362. return -EREMOTEIO;
  363. if (si21xx_wait_diseqc_idle(state, 100) < 0)
  364. return -ETIMEDOUT;
  365. if (si21_writereg(state, LNB_CTRL_REG_1, val))
  366. return -EREMOTEIO;
  367. return 0;
  368. }
  369. /* 30.06.2008 */
  370. static int si21xx_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
  371. {
  372. struct si21xx_state *state = fe->demodulator_priv;
  373. u8 val;
  374. dprintk("%s\n", __func__);
  375. val = (0x80 | si21_readreg(state, LNB_CTRL_REG_1));
  376. switch (tone) {
  377. case SEC_TONE_ON:
  378. return si21_writereg(state, LNB_CTRL_REG_1, val | 0x20);
  379. case SEC_TONE_OFF:
  380. return si21_writereg(state, LNB_CTRL_REG_1, (val & ~0x20));
  381. default:
  382. return -EINVAL;
  383. }
  384. }
  385. static int si21xx_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t volt)
  386. {
  387. struct si21xx_state *state = fe->demodulator_priv;
  388. u8 val;
  389. dprintk("%s: %s\n", __func__,
  390. volt == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
  391. volt == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
  392. val = (0x80 | si21_readreg(state, LNB_CTRL_REG_1));
  393. switch (volt) {
  394. case SEC_VOLTAGE_18:
  395. return si21_writereg(state, LNB_CTRL_REG_1, val | 0x40);
  396. break;
  397. case SEC_VOLTAGE_13:
  398. return si21_writereg(state, LNB_CTRL_REG_1, (val & ~0x40));
  399. break;
  400. default:
  401. return -EINVAL;
  402. };
  403. }
  404. static int si21xx_init(struct dvb_frontend *fe)
  405. {
  406. struct si21xx_state *state = fe->demodulator_priv;
  407. int i;
  408. int status = 0;
  409. u8 reg1;
  410. u8 val;
  411. u8 reg2[2];
  412. dprintk("%s\n", __func__);
  413. for (i = 0; ; i += 2) {
  414. reg1 = serit_sp1511lhb_inittab[i];
  415. val = serit_sp1511lhb_inittab[i+1];
  416. if (reg1 == 0xff && val == 0xff)
  417. break;
  418. si21_writeregs(state, reg1, &val, 1);
  419. }
  420. /*DVB QPSK SYSTEM MODE REG*/
  421. reg1 = 0x08;
  422. si21_writeregs(state, SYSTEM_MODE_REG, &reg1, 0x01);
  423. /*transport stream config*/
  424. /*
  425. mode = PARALLEL;
  426. sdata_form = LSB_FIRST;
  427. clk_edge = FALLING_EDGE;
  428. clk_mode = CLK_GAPPED_MODE;
  429. strt_len = BYTE_WIDE;
  430. sync_pol = ACTIVE_HIGH;
  431. val_pol = ACTIVE_HIGH;
  432. err_pol = ACTIVE_HIGH;
  433. sclk_rate = 0x00;
  434. parity = 0x00 ;
  435. data_delay = 0x00;
  436. clk_delay = 0x00;
  437. pclk_smooth = 0x00;
  438. */
  439. reg2[0] =
  440. PARALLEL + (LSB_FIRST << 1)
  441. + (FALLING_EDGE << 2) + (CLK_GAPPED_MODE << 3)
  442. + (BYTE_WIDE << 4) + (ACTIVE_HIGH << 5)
  443. + (ACTIVE_HIGH << 6) + (ACTIVE_HIGH << 7);
  444. reg2[1] = 0;
  445. /* sclk_rate + (parity << 2)
  446. + (data_delay << 3) + (clk_delay << 4)
  447. + (pclk_smooth << 5);
  448. */
  449. status |= si21_writeregs(state, TS_CTRL_REG_1, reg2, 0x02);
  450. if (status != 0)
  451. dprintk(" %s : TS Set Error\n", __func__);
  452. return 0;
  453. }
  454. static int si21_read_status(struct dvb_frontend *fe, fe_status_t *status)
  455. {
  456. struct si21xx_state *state = fe->demodulator_priv;
  457. u8 regs_read[2];
  458. u8 reg_read;
  459. u8 i;
  460. u8 lock;
  461. u8 signal = si21_readreg(state, ANALOG_AGC_POWER_LEVEL_REG);
  462. si21_readregs(state, LOCK_STATUS_REG_1, regs_read, 0x02);
  463. reg_read = 0;
  464. for (i = 0; i < 7; ++i)
  465. reg_read |= ((regs_read[0] >> i) & 0x01) << (6 - i);
  466. lock = ((reg_read & 0x7f) | (regs_read[1] & 0x80));
  467. dprintk("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__, lock);
  468. *status = 0;
  469. if (signal > 10)
  470. *status |= FE_HAS_SIGNAL;
  471. if (lock & 0x2)
  472. *status |= FE_HAS_CARRIER;
  473. if (lock & 0x20)
  474. *status |= FE_HAS_VITERBI;
  475. if (lock & 0x40)
  476. *status |= FE_HAS_SYNC;
  477. if ((lock & 0x7b) == 0x7b)
  478. *status |= FE_HAS_LOCK;
  479. return 0;
  480. }
  481. static int si21_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  482. {
  483. struct si21xx_state *state = fe->demodulator_priv;
  484. /*status = si21_readreg(state, ANALOG_AGC_POWER_LEVEL_REG,
  485. (u8*)agclevel, 0x01);*/
  486. u16 signal = (3 * si21_readreg(state, 0x27) *
  487. si21_readreg(state, 0x28));
  488. dprintk("%s : AGCPWR: 0x%02x%02x, signal=0x%04x\n", __func__,
  489. si21_readreg(state, 0x27),
  490. si21_readreg(state, 0x28), (int) signal);
  491. signal <<= 4;
  492. *strength = signal;
  493. return 0;
  494. }
  495. static int si21_read_ber(struct dvb_frontend *fe, u32 *ber)
  496. {
  497. struct si21xx_state *state = fe->demodulator_priv;
  498. dprintk("%s\n", __func__);
  499. if (state->errmode != STATUS_BER)
  500. return 0;
  501. *ber = (si21_readreg(state, 0x1d) << 8) |
  502. si21_readreg(state, 0x1e);
  503. return 0;
  504. }
  505. static int si21_read_snr(struct dvb_frontend *fe, u16 *snr)
  506. {
  507. struct si21xx_state *state = fe->demodulator_priv;
  508. s32 xsnr = 0xffff - ((si21_readreg(state, 0x24) << 8) |
  509. si21_readreg(state, 0x25));
  510. xsnr = 3 * (xsnr - 0xa100);
  511. *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
  512. dprintk("%s\n", __func__);
  513. return 0;
  514. }
  515. static int si21_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  516. {
  517. struct si21xx_state *state = fe->demodulator_priv;
  518. dprintk("%s\n", __func__);
  519. if (state->errmode != STATUS_UCBLOCKS)
  520. *ucblocks = 0;
  521. else
  522. *ucblocks = (si21_readreg(state, 0x1d) << 8) |
  523. si21_readreg(state, 0x1e);
  524. return 0;
  525. }
  526. /* initiates a channel acquisition sequence
  527. using the specified symbol rate and code rate */
  528. static int si21xx_setacquire(struct dvb_frontend *fe, int symbrate,
  529. fe_code_rate_t crate)
  530. {
  531. struct si21xx_state *state = fe->demodulator_priv;
  532. u8 coderates[] = {
  533. 0x0, 0x01, 0x02, 0x04, 0x00,
  534. 0x8, 0x10, 0x20, 0x00, 0x3f
  535. };
  536. u8 coderate_ptr;
  537. int status;
  538. u8 start_acq = 0x80;
  539. u8 reg, regs[3];
  540. dprintk("%s\n", __func__);
  541. status = PASS;
  542. coderate_ptr = coderates[crate];
  543. si21xx_set_symbolrate(fe, symbrate);
  544. /* write code rates to use in the Viterbi search */
  545. status |= si21_writeregs(state,
  546. VIT_SRCH_CTRL_REG_1,
  547. &coderate_ptr, 0x01);
  548. /* clear acq_start bit */
  549. status |= si21_readregs(state, ACQ_CTRL_REG_2, &reg, 0x01);
  550. reg &= ~start_acq;
  551. status |= si21_writeregs(state, ACQ_CTRL_REG_2, &reg, 0x01);
  552. /* use new Carrier Frequency Offset Estimator (QuickLock) */
  553. regs[0] = 0xCB;
  554. regs[1] = 0x40;
  555. regs[2] = 0xCB;
  556. status |= si21_writeregs(state,
  557. TWO_DB_BNDWDTH_THRSHLD_REG,
  558. &regs[0], 0x03);
  559. reg = 0x56;
  560. status |= si21_writeregs(state,
  561. LSA_CTRL_REG_1, &reg, 1);
  562. reg = 0x05;
  563. status |= si21_writeregs(state,
  564. BLIND_SCAN_CTRL_REG, &reg, 1);
  565. /* start automatic acq */
  566. status |= si21_writeregs(state,
  567. ACQ_CTRL_REG_2, &start_acq, 0x01);
  568. return status;
  569. }
  570. static int si21xx_set_property(struct dvb_frontend *fe, struct dtv_property *p)
  571. {
  572. dprintk("%s(..)\n", __func__);
  573. return 0;
  574. }
  575. static int si21xx_get_property(struct dvb_frontend *fe, struct dtv_property *p)
  576. {
  577. dprintk("%s(..)\n", __func__);
  578. return 0;
  579. }
  580. static int si21xx_set_frontend(struct dvb_frontend *fe,
  581. struct dvb_frontend_parameters *dfp)
  582. {
  583. struct si21xx_state *state = fe->demodulator_priv;
  584. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  585. /* freq Channel carrier frequency in KHz (i.e. 1550000 KHz)
  586. datarate Channel symbol rate in Sps (i.e. 22500000 Sps)*/
  587. /* in MHz */
  588. unsigned char coarse_tune_freq;
  589. int fine_tune_freq;
  590. unsigned char sample_rate = 0;
  591. /* boolean */
  592. bool inband_interferer_ind;
  593. /* INTERMEDIATE VALUES */
  594. int icoarse_tune_freq; /* MHz */
  595. int ifine_tune_freq; /* MHz */
  596. unsigned int band_high;
  597. unsigned int band_low;
  598. unsigned int x1;
  599. unsigned int x2;
  600. int i;
  601. bool inband_interferer_div2[ALLOWABLE_FS_COUNT];
  602. bool inband_interferer_div4[ALLOWABLE_FS_COUNT];
  603. int status;
  604. /* allowable sample rates for ADC in MHz */
  605. int afs[ALLOWABLE_FS_COUNT] = { 200, 192, 193, 194, 195,
  606. 196, 204, 205, 206, 207
  607. };
  608. /* in MHz */
  609. int if_limit_high;
  610. int if_limit_low;
  611. int lnb_lo;
  612. int lnb_uncertanity;
  613. int rf_freq;
  614. int data_rate;
  615. unsigned char regs[4];
  616. dprintk("%s : FE_SET_FRONTEND\n", __func__);
  617. if (c->delivery_system != SYS_DVBS) {
  618. dprintk("%s: unsupported delivery system selected (%d)\n",
  619. __func__, c->delivery_system);
  620. return -EOPNOTSUPP;
  621. }
  622. for (i = 0; i < ALLOWABLE_FS_COUNT; ++i)
  623. inband_interferer_div2[i] = inband_interferer_div4[i] = false;
  624. if_limit_high = -700000;
  625. if_limit_low = -100000;
  626. /* in MHz */
  627. lnb_lo = 0;
  628. lnb_uncertanity = 0;
  629. rf_freq = 10 * c->frequency ;
  630. data_rate = c->symbol_rate / 100;
  631. status = PASS;
  632. band_low = (rf_freq - lnb_lo) - ((lnb_uncertanity * 200)
  633. + (data_rate * 135)) / 200;
  634. band_high = (rf_freq - lnb_lo) + ((lnb_uncertanity * 200)
  635. + (data_rate * 135)) / 200;
  636. icoarse_tune_freq = 100000 *
  637. (((rf_freq - lnb_lo) -
  638. (if_limit_low + if_limit_high) / 2)
  639. / 100000);
  640. ifine_tune_freq = (rf_freq - lnb_lo) - icoarse_tune_freq ;
  641. for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
  642. x1 = ((rf_freq - lnb_lo) / (afs[i] * 2500)) *
  643. (afs[i] * 2500) + afs[i] * 2500;
  644. x2 = ((rf_freq - lnb_lo) / (afs[i] * 2500)) *
  645. (afs[i] * 2500);
  646. if (((band_low < x1) && (x1 < band_high)) ||
  647. ((band_low < x2) && (x2 < band_high)))
  648. inband_interferer_div4[i] = true;
  649. }
  650. for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
  651. x1 = ((rf_freq - lnb_lo) / (afs[i] * 5000)) *
  652. (afs[i] * 5000) + afs[i] * 5000;
  653. x2 = ((rf_freq - lnb_lo) / (afs[i] * 5000)) *
  654. (afs[i] * 5000);
  655. if (((band_low < x1) && (x1 < band_high)) ||
  656. ((band_low < x2) && (x2 < band_high)))
  657. inband_interferer_div2[i] = true;
  658. }
  659. inband_interferer_ind = true;
  660. for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
  661. if (inband_interferer_div2[i] || inband_interferer_div4[i]) {
  662. inband_interferer_ind = false;
  663. break;
  664. }
  665. }
  666. if (inband_interferer_ind) {
  667. for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
  668. if (!inband_interferer_div2[i]) {
  669. sample_rate = (u8) afs[i];
  670. break;
  671. }
  672. }
  673. } else {
  674. for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
  675. if ((inband_interferer_div2[i] ||
  676. !inband_interferer_div4[i])) {
  677. sample_rate = (u8) afs[i];
  678. break;
  679. }
  680. }
  681. }
  682. if (sample_rate > 207 || sample_rate < 192)
  683. sample_rate = 200;
  684. fine_tune_freq = ((0x4000 * (ifine_tune_freq / 10)) /
  685. ((sample_rate) * 1000));
  686. coarse_tune_freq = (u8)(icoarse_tune_freq / 100000);
  687. regs[0] = sample_rate;
  688. regs[1] = coarse_tune_freq;
  689. regs[2] = fine_tune_freq & 0xFF;
  690. regs[3] = fine_tune_freq >> 8 & 0xFF;
  691. status |= si21_writeregs(state, PLL_DIVISOR_REG, &regs[0], 0x04);
  692. state->fs = sample_rate;/*ADC MHz*/
  693. si21xx_setacquire(fe, c->symbol_rate, c->fec_inner);
  694. return 0;
  695. }
  696. static int si21xx_sleep(struct dvb_frontend *fe)
  697. {
  698. struct si21xx_state *state = fe->demodulator_priv;
  699. u8 regdata;
  700. dprintk("%s\n", __func__);
  701. si21_readregs(state, SYSTEM_MODE_REG, &regdata, 0x01);
  702. regdata |= 1 << 6;
  703. si21_writeregs(state, SYSTEM_MODE_REG, &regdata, 0x01);
  704. state->initialised = 0;
  705. return 0;
  706. }
  707. static void si21xx_release(struct dvb_frontend *fe)
  708. {
  709. struct si21xx_state *state = fe->demodulator_priv;
  710. dprintk("%s\n", __func__);
  711. kfree(state);
  712. }
  713. static struct dvb_frontend_ops si21xx_ops = {
  714. .info = {
  715. .name = "SL SI21XX DVB-S",
  716. .type = FE_QPSK,
  717. .frequency_min = 950000,
  718. .frequency_max = 2150000,
  719. .frequency_stepsize = 125, /* kHz for QPSK frontends */
  720. .frequency_tolerance = 0,
  721. .symbol_rate_min = 1000000,
  722. .symbol_rate_max = 45000000,
  723. .symbol_rate_tolerance = 500, /* ppm */
  724. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  725. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  726. FE_CAN_QPSK |
  727. FE_CAN_FEC_AUTO
  728. },
  729. .release = si21xx_release,
  730. .init = si21xx_init,
  731. .sleep = si21xx_sleep,
  732. .write = si21_write,
  733. .read_status = si21_read_status,
  734. .read_ber = si21_read_ber,
  735. .read_signal_strength = si21_read_signal_strength,
  736. .read_snr = si21_read_snr,
  737. .read_ucblocks = si21_read_ucblocks,
  738. .diseqc_send_master_cmd = si21xx_send_diseqc_msg,
  739. .diseqc_send_burst = si21xx_send_diseqc_burst,
  740. .set_tone = si21xx_set_tone,
  741. .set_voltage = si21xx_set_voltage,
  742. .set_property = si21xx_set_property,
  743. .get_property = si21xx_get_property,
  744. .set_frontend = si21xx_set_frontend,
  745. };
  746. struct dvb_frontend *si21xx_attach(const struct si21xx_config *config,
  747. struct i2c_adapter *i2c)
  748. {
  749. struct si21xx_state *state = NULL;
  750. int id;
  751. dprintk("%s\n", __func__);
  752. /* allocate memory for the internal state */
  753. state = kzalloc(sizeof(struct si21xx_state), GFP_KERNEL);
  754. if (state == NULL)
  755. goto error;
  756. /* setup the state */
  757. state->config = config;
  758. state->i2c = i2c;
  759. state->initialised = 0;
  760. state->errmode = STATUS_BER;
  761. /* check if the demod is there */
  762. id = si21_readreg(state, SYSTEM_MODE_REG);
  763. si21_writereg(state, SYSTEM_MODE_REG, id | 0x40); /* standby off */
  764. msleep(200);
  765. id = si21_readreg(state, 0x00);
  766. /* register 0x00 contains:
  767. 0x34 for SI2107
  768. 0x24 for SI2108
  769. 0x14 for SI2109
  770. 0x04 for SI2110
  771. */
  772. if (id != 0x04 && id != 0x14)
  773. goto error;
  774. /* create dvb_frontend */
  775. memcpy(&state->frontend.ops, &si21xx_ops,
  776. sizeof(struct dvb_frontend_ops));
  777. state->frontend.demodulator_priv = state;
  778. return &state->frontend;
  779. error:
  780. kfree(state);
  781. return NULL;
  782. }
  783. EXPORT_SYMBOL(si21xx_attach);
  784. module_param(debug, int, 0644);
  785. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  786. MODULE_DESCRIPTION("SL SI21XX DVB Demodulator driver");
  787. MODULE_AUTHOR("Igor M. Liplianin");
  788. MODULE_LICENSE("GPL");