si21xx.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. if (len > sizeof(buf) - 1)
  221. return -EINVAL;
  222. msg.buf[0] = reg1;
  223. memcpy(msg.buf + 1, data, len);
  224. ret = i2c_transfer(state->i2c, &msg, 1);
  225. if (ret != 1)
  226. dprintk("%s: writereg error (reg1 == 0x%02x, data == 0x%02x, "
  227. "ret == %i)\n", __func__, reg1, data[0], ret);
  228. return (ret != 1) ? -EREMOTEIO : 0;
  229. }
  230. static int si21_writereg(struct si21xx_state *state, u8 reg, u8 data)
  231. {
  232. int ret;
  233. u8 buf[] = { reg, data };
  234. struct i2c_msg msg = {
  235. .addr = state->config->demod_address,
  236. .flags = 0,
  237. .buf = buf,
  238. .len = 2
  239. };
  240. ret = i2c_transfer(state->i2c, &msg, 1);
  241. if (ret != 1)
  242. dprintk("%s: writereg error (reg == 0x%02x, data == 0x%02x, "
  243. "ret == %i)\n", __func__, reg, data, ret);
  244. return (ret != 1) ? -EREMOTEIO : 0;
  245. }
  246. static int si21_write(struct dvb_frontend *fe, const u8 buf[], int len)
  247. {
  248. struct si21xx_state *state = fe->demodulator_priv;
  249. if (len != 2)
  250. return -EINVAL;
  251. return si21_writereg(state, buf[0], buf[1]);
  252. }
  253. static u8 si21_readreg(struct si21xx_state *state, u8 reg)
  254. {
  255. int ret;
  256. u8 b0[] = { reg };
  257. u8 b1[] = { 0 };
  258. struct i2c_msg msg[] = {
  259. {
  260. .addr = state->config->demod_address,
  261. .flags = 0,
  262. .buf = b0,
  263. .len = 1
  264. }, {
  265. .addr = state->config->demod_address,
  266. .flags = I2C_M_RD,
  267. .buf = b1,
  268. .len = 1
  269. }
  270. };
  271. ret = i2c_transfer(state->i2c, msg, 2);
  272. if (ret != 2)
  273. dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
  274. __func__, reg, ret);
  275. return b1[0];
  276. }
  277. static int si21_readregs(struct si21xx_state *state, u8 reg1, u8 *b, u8 len)
  278. {
  279. int ret;
  280. struct i2c_msg msg[] = {
  281. {
  282. .addr = state->config->demod_address,
  283. .flags = 0,
  284. .buf = &reg1,
  285. .len = 1
  286. }, {
  287. .addr = state->config->demod_address,
  288. .flags = I2C_M_RD,
  289. .buf = b,
  290. .len = len
  291. }
  292. };
  293. ret = i2c_transfer(state->i2c, msg, 2);
  294. if (ret != 2)
  295. dprintk("%s: readreg error (ret == %i)\n", __func__, ret);
  296. return ret == 2 ? 0 : -1;
  297. }
  298. static int si21xx_wait_diseqc_idle(struct si21xx_state *state, int timeout)
  299. {
  300. unsigned long start = jiffies;
  301. dprintk("%s\n", __func__);
  302. while ((si21_readreg(state, LNB_CTRL_REG_1) & 0x8) == 8) {
  303. if (jiffies - start > timeout) {
  304. dprintk("%s: timeout!!\n", __func__);
  305. return -ETIMEDOUT;
  306. }
  307. msleep(10);
  308. }
  309. return 0;
  310. }
  311. static int si21xx_set_symbolrate(struct dvb_frontend *fe, u32 srate)
  312. {
  313. struct si21xx_state *state = fe->demodulator_priv;
  314. u32 sym_rate, data_rate;
  315. int i;
  316. u8 sym_rate_bytes[3];
  317. dprintk("%s : srate = %i\n", __func__ , srate);
  318. if ((srate < 1000000) || (srate > 45000000))
  319. return -EINVAL;
  320. data_rate = srate;
  321. sym_rate = 0;
  322. for (i = 0; i < 4; ++i) {
  323. sym_rate /= 100;
  324. sym_rate = sym_rate + ((data_rate % 100) * 0x800000) /
  325. state->fs;
  326. data_rate /= 100;
  327. }
  328. for (i = 0; i < 3; ++i)
  329. sym_rate_bytes[i] = (u8)((sym_rate >> (i * 8)) & 0xff);
  330. si21_writeregs(state, SYM_RATE_REG_L, sym_rate_bytes, 0x03);
  331. return 0;
  332. }
  333. static int si21xx_send_diseqc_msg(struct dvb_frontend *fe,
  334. struct dvb_diseqc_master_cmd *m)
  335. {
  336. struct si21xx_state *state = fe->demodulator_priv;
  337. u8 lnb_status;
  338. u8 LNB_CTRL_1;
  339. int status;
  340. dprintk("%s\n", __func__);
  341. status = PASS;
  342. LNB_CTRL_1 = 0;
  343. status |= si21_readregs(state, LNB_CTRL_STATUS_REG, &lnb_status, 0x01);
  344. status |= si21_readregs(state, LNB_CTRL_REG_1, &lnb_status, 0x01);
  345. /*fill the FIFO*/
  346. status |= si21_writeregs(state, LNB_FIFO_REGS_0, m->msg, m->msg_len);
  347. LNB_CTRL_1 = (lnb_status & 0x70);
  348. LNB_CTRL_1 |= m->msg_len;
  349. LNB_CTRL_1 |= 0x80; /* begin LNB signaling */
  350. status |= si21_writeregs(state, LNB_CTRL_REG_1, &LNB_CTRL_1, 0x01);
  351. return status;
  352. }
  353. static int si21xx_send_diseqc_burst(struct dvb_frontend *fe,
  354. enum fe_sec_mini_cmd burst)
  355. {
  356. struct si21xx_state *state = fe->demodulator_priv;
  357. u8 val;
  358. dprintk("%s\n", __func__);
  359. if (si21xx_wait_diseqc_idle(state, 100) < 0)
  360. return -ETIMEDOUT;
  361. val = (0x80 | si21_readreg(state, 0xc1));
  362. if (si21_writereg(state, LNB_CTRL_REG_1,
  363. burst == SEC_MINI_A ? (val & ~0x10) : (val | 0x10)))
  364. return -EREMOTEIO;
  365. if (si21xx_wait_diseqc_idle(state, 100) < 0)
  366. return -ETIMEDOUT;
  367. if (si21_writereg(state, LNB_CTRL_REG_1, val))
  368. return -EREMOTEIO;
  369. return 0;
  370. }
  371. /* 30.06.2008 */
  372. static int si21xx_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
  373. {
  374. struct si21xx_state *state = fe->demodulator_priv;
  375. u8 val;
  376. dprintk("%s\n", __func__);
  377. val = (0x80 | si21_readreg(state, LNB_CTRL_REG_1));
  378. switch (tone) {
  379. case SEC_TONE_ON:
  380. return si21_writereg(state, LNB_CTRL_REG_1, val | 0x20);
  381. case SEC_TONE_OFF:
  382. return si21_writereg(state, LNB_CTRL_REG_1, (val & ~0x20));
  383. default:
  384. return -EINVAL;
  385. }
  386. }
  387. static int si21xx_set_voltage(struct dvb_frontend *fe, enum fe_sec_voltage volt)
  388. {
  389. struct si21xx_state *state = fe->demodulator_priv;
  390. u8 val;
  391. dprintk("%s: %s\n", __func__,
  392. volt == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
  393. volt == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
  394. val = (0x80 | si21_readreg(state, LNB_CTRL_REG_1));
  395. switch (volt) {
  396. case SEC_VOLTAGE_18:
  397. return si21_writereg(state, LNB_CTRL_REG_1, val | 0x40);
  398. break;
  399. case SEC_VOLTAGE_13:
  400. return si21_writereg(state, LNB_CTRL_REG_1, (val & ~0x40));
  401. break;
  402. default:
  403. return -EINVAL;
  404. }
  405. }
  406. static int si21xx_init(struct dvb_frontend *fe)
  407. {
  408. struct si21xx_state *state = fe->demodulator_priv;
  409. int i;
  410. int status = 0;
  411. u8 reg1;
  412. u8 val;
  413. u8 reg2[2];
  414. dprintk("%s\n", __func__);
  415. for (i = 0; ; i += 2) {
  416. reg1 = serit_sp1511lhb_inittab[i];
  417. val = serit_sp1511lhb_inittab[i+1];
  418. if (reg1 == 0xff && val == 0xff)
  419. break;
  420. si21_writeregs(state, reg1, &val, 1);
  421. }
  422. /*DVB QPSK SYSTEM MODE REG*/
  423. reg1 = 0x08;
  424. si21_writeregs(state, SYSTEM_MODE_REG, &reg1, 0x01);
  425. /*transport stream config*/
  426. /*
  427. mode = PARALLEL;
  428. sdata_form = LSB_FIRST;
  429. clk_edge = FALLING_EDGE;
  430. clk_mode = CLK_GAPPED_MODE;
  431. strt_len = BYTE_WIDE;
  432. sync_pol = ACTIVE_HIGH;
  433. val_pol = ACTIVE_HIGH;
  434. err_pol = ACTIVE_HIGH;
  435. sclk_rate = 0x00;
  436. parity = 0x00 ;
  437. data_delay = 0x00;
  438. clk_delay = 0x00;
  439. pclk_smooth = 0x00;
  440. */
  441. reg2[0] =
  442. PARALLEL + (LSB_FIRST << 1)
  443. + (FALLING_EDGE << 2) + (CLK_GAPPED_MODE << 3)
  444. + (BYTE_WIDE << 4) + (ACTIVE_HIGH << 5)
  445. + (ACTIVE_HIGH << 6) + (ACTIVE_HIGH << 7);
  446. reg2[1] = 0;
  447. /* sclk_rate + (parity << 2)
  448. + (data_delay << 3) + (clk_delay << 4)
  449. + (pclk_smooth << 5);
  450. */
  451. status |= si21_writeregs(state, TS_CTRL_REG_1, reg2, 0x02);
  452. if (status != 0)
  453. dprintk(" %s : TS Set Error\n", __func__);
  454. return 0;
  455. }
  456. static int si21_read_status(struct dvb_frontend *fe, enum fe_status *status)
  457. {
  458. struct si21xx_state *state = fe->demodulator_priv;
  459. u8 regs_read[2];
  460. u8 reg_read;
  461. u8 i;
  462. u8 lock;
  463. u8 signal = si21_readreg(state, ANALOG_AGC_POWER_LEVEL_REG);
  464. si21_readregs(state, LOCK_STATUS_REG_1, regs_read, 0x02);
  465. reg_read = 0;
  466. for (i = 0; i < 7; ++i)
  467. reg_read |= ((regs_read[0] >> i) & 0x01) << (6 - i);
  468. lock = ((reg_read & 0x7f) | (regs_read[1] & 0x80));
  469. dprintk("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__, lock);
  470. *status = 0;
  471. if (signal > 10)
  472. *status |= FE_HAS_SIGNAL;
  473. if (lock & 0x2)
  474. *status |= FE_HAS_CARRIER;
  475. if (lock & 0x20)
  476. *status |= FE_HAS_VITERBI;
  477. if (lock & 0x40)
  478. *status |= FE_HAS_SYNC;
  479. if ((lock & 0x7b) == 0x7b)
  480. *status |= FE_HAS_LOCK;
  481. return 0;
  482. }
  483. static int si21_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  484. {
  485. struct si21xx_state *state = fe->demodulator_priv;
  486. /*status = si21_readreg(state, ANALOG_AGC_POWER_LEVEL_REG,
  487. (u8*)agclevel, 0x01);*/
  488. u16 signal = (3 * si21_readreg(state, 0x27) *
  489. si21_readreg(state, 0x28));
  490. dprintk("%s : AGCPWR: 0x%02x%02x, signal=0x%04x\n", __func__,
  491. si21_readreg(state, 0x27),
  492. si21_readreg(state, 0x28), (int) signal);
  493. signal <<= 4;
  494. *strength = signal;
  495. return 0;
  496. }
  497. static int si21_read_ber(struct dvb_frontend *fe, u32 *ber)
  498. {
  499. struct si21xx_state *state = fe->demodulator_priv;
  500. dprintk("%s\n", __func__);
  501. if (state->errmode != STATUS_BER)
  502. return 0;
  503. *ber = (si21_readreg(state, 0x1d) << 8) |
  504. si21_readreg(state, 0x1e);
  505. return 0;
  506. }
  507. static int si21_read_snr(struct dvb_frontend *fe, u16 *snr)
  508. {
  509. struct si21xx_state *state = fe->demodulator_priv;
  510. s32 xsnr = 0xffff - ((si21_readreg(state, 0x24) << 8) |
  511. si21_readreg(state, 0x25));
  512. xsnr = 3 * (xsnr - 0xa100);
  513. *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
  514. dprintk("%s\n", __func__);
  515. return 0;
  516. }
  517. static int si21_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  518. {
  519. struct si21xx_state *state = fe->demodulator_priv;
  520. dprintk("%s\n", __func__);
  521. if (state->errmode != STATUS_UCBLOCKS)
  522. *ucblocks = 0;
  523. else
  524. *ucblocks = (si21_readreg(state, 0x1d) << 8) |
  525. si21_readreg(state, 0x1e);
  526. return 0;
  527. }
  528. /* initiates a channel acquisition sequence
  529. using the specified symbol rate and code rate */
  530. static int si21xx_setacquire(struct dvb_frontend *fe, int symbrate,
  531. enum fe_code_rate crate)
  532. {
  533. struct si21xx_state *state = fe->demodulator_priv;
  534. u8 coderates[] = {
  535. 0x0, 0x01, 0x02, 0x04, 0x00,
  536. 0x8, 0x10, 0x20, 0x00, 0x3f
  537. };
  538. u8 coderate_ptr;
  539. int status;
  540. u8 start_acq = 0x80;
  541. u8 reg, regs[3];
  542. dprintk("%s\n", __func__);
  543. status = PASS;
  544. coderate_ptr = coderates[crate];
  545. si21xx_set_symbolrate(fe, symbrate);
  546. /* write code rates to use in the Viterbi search */
  547. status |= si21_writeregs(state,
  548. VIT_SRCH_CTRL_REG_1,
  549. &coderate_ptr, 0x01);
  550. /* clear acq_start bit */
  551. status |= si21_readregs(state, ACQ_CTRL_REG_2, &reg, 0x01);
  552. reg &= ~start_acq;
  553. status |= si21_writeregs(state, ACQ_CTRL_REG_2, &reg, 0x01);
  554. /* use new Carrier Frequency Offset Estimator (QuickLock) */
  555. regs[0] = 0xCB;
  556. regs[1] = 0x40;
  557. regs[2] = 0xCB;
  558. status |= si21_writeregs(state,
  559. TWO_DB_BNDWDTH_THRSHLD_REG,
  560. &regs[0], 0x03);
  561. reg = 0x56;
  562. status |= si21_writeregs(state,
  563. LSA_CTRL_REG_1, &reg, 1);
  564. reg = 0x05;
  565. status |= si21_writeregs(state,
  566. BLIND_SCAN_CTRL_REG, &reg, 1);
  567. /* start automatic acq */
  568. status |= si21_writeregs(state,
  569. ACQ_CTRL_REG_2, &start_acq, 0x01);
  570. return status;
  571. }
  572. static int si21xx_set_frontend(struct dvb_frontend *fe)
  573. {
  574. struct si21xx_state *state = fe->demodulator_priv;
  575. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  576. /* freq Channel carrier frequency in KHz (i.e. 1550000 KHz)
  577. datarate Channel symbol rate in Sps (i.e. 22500000 Sps)*/
  578. /* in MHz */
  579. unsigned char coarse_tune_freq;
  580. int fine_tune_freq;
  581. unsigned char sample_rate = 0;
  582. /* boolean */
  583. bool inband_interferer_ind;
  584. /* INTERMEDIATE VALUES */
  585. int icoarse_tune_freq; /* MHz */
  586. int ifine_tune_freq; /* MHz */
  587. unsigned int band_high;
  588. unsigned int band_low;
  589. unsigned int x1;
  590. unsigned int x2;
  591. int i;
  592. bool inband_interferer_div2[ALLOWABLE_FS_COUNT];
  593. bool inband_interferer_div4[ALLOWABLE_FS_COUNT];
  594. int status;
  595. /* allowable sample rates for ADC in MHz */
  596. int afs[ALLOWABLE_FS_COUNT] = { 200, 192, 193, 194, 195,
  597. 196, 204, 205, 206, 207
  598. };
  599. /* in MHz */
  600. int if_limit_high;
  601. int if_limit_low;
  602. int lnb_lo;
  603. int lnb_uncertanity;
  604. int rf_freq;
  605. int data_rate;
  606. unsigned char regs[4];
  607. dprintk("%s : FE_SET_FRONTEND\n", __func__);
  608. if (c->delivery_system != SYS_DVBS) {
  609. dprintk("%s: unsupported delivery system selected (%d)\n",
  610. __func__, c->delivery_system);
  611. return -EOPNOTSUPP;
  612. }
  613. for (i = 0; i < ALLOWABLE_FS_COUNT; ++i)
  614. inband_interferer_div2[i] = inband_interferer_div4[i] = false;
  615. if_limit_high = -700000;
  616. if_limit_low = -100000;
  617. /* in MHz */
  618. lnb_lo = 0;
  619. lnb_uncertanity = 0;
  620. rf_freq = 10 * c->frequency ;
  621. data_rate = c->symbol_rate / 100;
  622. status = PASS;
  623. band_low = (rf_freq - lnb_lo) - ((lnb_uncertanity * 200)
  624. + (data_rate * 135)) / 200;
  625. band_high = (rf_freq - lnb_lo) + ((lnb_uncertanity * 200)
  626. + (data_rate * 135)) / 200;
  627. icoarse_tune_freq = 100000 *
  628. (((rf_freq - lnb_lo) -
  629. (if_limit_low + if_limit_high) / 2)
  630. / 100000);
  631. ifine_tune_freq = (rf_freq - lnb_lo) - icoarse_tune_freq ;
  632. for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
  633. x1 = ((rf_freq - lnb_lo) / (afs[i] * 2500)) *
  634. (afs[i] * 2500) + afs[i] * 2500;
  635. x2 = ((rf_freq - lnb_lo) / (afs[i] * 2500)) *
  636. (afs[i] * 2500);
  637. if (((band_low < x1) && (x1 < band_high)) ||
  638. ((band_low < x2) && (x2 < band_high)))
  639. inband_interferer_div4[i] = true;
  640. }
  641. for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
  642. x1 = ((rf_freq - lnb_lo) / (afs[i] * 5000)) *
  643. (afs[i] * 5000) + afs[i] * 5000;
  644. x2 = ((rf_freq - lnb_lo) / (afs[i] * 5000)) *
  645. (afs[i] * 5000);
  646. if (((band_low < x1) && (x1 < band_high)) ||
  647. ((band_low < x2) && (x2 < band_high)))
  648. inband_interferer_div2[i] = true;
  649. }
  650. inband_interferer_ind = true;
  651. for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
  652. if (inband_interferer_div2[i] || inband_interferer_div4[i]) {
  653. inband_interferer_ind = false;
  654. break;
  655. }
  656. }
  657. if (inband_interferer_ind) {
  658. for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
  659. if (!inband_interferer_div2[i]) {
  660. sample_rate = (u8) afs[i];
  661. break;
  662. }
  663. }
  664. } else {
  665. for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
  666. if ((inband_interferer_div2[i] ||
  667. !inband_interferer_div4[i])) {
  668. sample_rate = (u8) afs[i];
  669. break;
  670. }
  671. }
  672. }
  673. if (sample_rate > 207 || sample_rate < 192)
  674. sample_rate = 200;
  675. fine_tune_freq = ((0x4000 * (ifine_tune_freq / 10)) /
  676. ((sample_rate) * 1000));
  677. coarse_tune_freq = (u8)(icoarse_tune_freq / 100000);
  678. regs[0] = sample_rate;
  679. regs[1] = coarse_tune_freq;
  680. regs[2] = fine_tune_freq & 0xFF;
  681. regs[3] = fine_tune_freq >> 8 & 0xFF;
  682. status |= si21_writeregs(state, PLL_DIVISOR_REG, &regs[0], 0x04);
  683. state->fs = sample_rate;/*ADC MHz*/
  684. si21xx_setacquire(fe, c->symbol_rate, c->fec_inner);
  685. return 0;
  686. }
  687. static int si21xx_sleep(struct dvb_frontend *fe)
  688. {
  689. struct si21xx_state *state = fe->demodulator_priv;
  690. u8 regdata;
  691. dprintk("%s\n", __func__);
  692. si21_readregs(state, SYSTEM_MODE_REG, &regdata, 0x01);
  693. regdata |= 1 << 6;
  694. si21_writeregs(state, SYSTEM_MODE_REG, &regdata, 0x01);
  695. state->initialised = 0;
  696. return 0;
  697. }
  698. static void si21xx_release(struct dvb_frontend *fe)
  699. {
  700. struct si21xx_state *state = fe->demodulator_priv;
  701. dprintk("%s\n", __func__);
  702. kfree(state);
  703. }
  704. static struct dvb_frontend_ops si21xx_ops = {
  705. .delsys = { SYS_DVBS },
  706. .info = {
  707. .name = "SL SI21XX DVB-S",
  708. .frequency_min = 950000,
  709. .frequency_max = 2150000,
  710. .frequency_stepsize = 125, /* kHz for QPSK frontends */
  711. .frequency_tolerance = 0,
  712. .symbol_rate_min = 1000000,
  713. .symbol_rate_max = 45000000,
  714. .symbol_rate_tolerance = 500, /* ppm */
  715. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  716. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  717. FE_CAN_QPSK |
  718. FE_CAN_FEC_AUTO
  719. },
  720. .release = si21xx_release,
  721. .init = si21xx_init,
  722. .sleep = si21xx_sleep,
  723. .write = si21_write,
  724. .read_status = si21_read_status,
  725. .read_ber = si21_read_ber,
  726. .read_signal_strength = si21_read_signal_strength,
  727. .read_snr = si21_read_snr,
  728. .read_ucblocks = si21_read_ucblocks,
  729. .diseqc_send_master_cmd = si21xx_send_diseqc_msg,
  730. .diseqc_send_burst = si21xx_send_diseqc_burst,
  731. .set_tone = si21xx_set_tone,
  732. .set_voltage = si21xx_set_voltage,
  733. .set_frontend = si21xx_set_frontend,
  734. };
  735. struct dvb_frontend *si21xx_attach(const struct si21xx_config *config,
  736. struct i2c_adapter *i2c)
  737. {
  738. struct si21xx_state *state = NULL;
  739. int id;
  740. dprintk("%s\n", __func__);
  741. /* allocate memory for the internal state */
  742. state = kzalloc(sizeof(struct si21xx_state), GFP_KERNEL);
  743. if (state == NULL)
  744. goto error;
  745. /* setup the state */
  746. state->config = config;
  747. state->i2c = i2c;
  748. state->initialised = 0;
  749. state->errmode = STATUS_BER;
  750. /* check if the demod is there */
  751. id = si21_readreg(state, SYSTEM_MODE_REG);
  752. si21_writereg(state, SYSTEM_MODE_REG, id | 0x40); /* standby off */
  753. msleep(200);
  754. id = si21_readreg(state, 0x00);
  755. /* register 0x00 contains:
  756. 0x34 for SI2107
  757. 0x24 for SI2108
  758. 0x14 for SI2109
  759. 0x04 for SI2110
  760. */
  761. if (id != 0x04 && id != 0x14)
  762. goto error;
  763. /* create dvb_frontend */
  764. memcpy(&state->frontend.ops, &si21xx_ops,
  765. sizeof(struct dvb_frontend_ops));
  766. state->frontend.demodulator_priv = state;
  767. return &state->frontend;
  768. error:
  769. kfree(state);
  770. return NULL;
  771. }
  772. EXPORT_SYMBOL(si21xx_attach);
  773. module_param(debug, int, 0644);
  774. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  775. MODULE_DESCRIPTION("SL SI21XX DVB Demodulator driver");
  776. MODULE_AUTHOR("Igor M. Liplianin");
  777. MODULE_LICENSE("GPL");