si21xx.c 20 KB

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