ds3000.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. /*
  2. Montage Technology DS3000 - DVBS/S2 Demodulator driver
  3. Copyright (C) 2009-2012 Konstantin Dimitrov <kosio.dimitrov@gmail.com>
  4. Copyright (C) 2009-2012 TurboSight.com
  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 <linux/firmware.h>
  23. #include "dvb_frontend.h"
  24. #include "ts2020.h"
  25. #include "ds3000.h"
  26. static int debug;
  27. #define dprintk(args...) \
  28. do { \
  29. if (debug) \
  30. printk(args); \
  31. } while (0)
  32. /* as of March 2009 current DS3000 firmware version is 1.78 */
  33. /* DS3000 FW v1.78 MD5: a32d17910c4f370073f9346e71d34b80 */
  34. #define DS3000_DEFAULT_FIRMWARE "dvb-fe-ds3000.fw"
  35. #define DS3000_SAMPLE_RATE 96000 /* in kHz */
  36. /* Register values to initialise the demod in DVB-S mode */
  37. static u8 ds3000_dvbs_init_tab[] = {
  38. 0x23, 0x05,
  39. 0x08, 0x03,
  40. 0x0c, 0x00,
  41. 0x21, 0x54,
  42. 0x25, 0x82,
  43. 0x27, 0x31,
  44. 0x30, 0x08,
  45. 0x31, 0x40,
  46. 0x32, 0x32,
  47. 0x33, 0x35,
  48. 0x35, 0xff,
  49. 0x3a, 0x00,
  50. 0x37, 0x10,
  51. 0x38, 0x10,
  52. 0x39, 0x02,
  53. 0x42, 0x60,
  54. 0x4a, 0x40,
  55. 0x4b, 0x04,
  56. 0x4d, 0x91,
  57. 0x5d, 0xc8,
  58. 0x50, 0x77,
  59. 0x51, 0x77,
  60. 0x52, 0x36,
  61. 0x53, 0x36,
  62. 0x56, 0x01,
  63. 0x63, 0x43,
  64. 0x64, 0x30,
  65. 0x65, 0x40,
  66. 0x68, 0x26,
  67. 0x69, 0x4c,
  68. 0x70, 0x20,
  69. 0x71, 0x70,
  70. 0x72, 0x04,
  71. 0x73, 0x00,
  72. 0x70, 0x40,
  73. 0x71, 0x70,
  74. 0x72, 0x04,
  75. 0x73, 0x00,
  76. 0x70, 0x60,
  77. 0x71, 0x70,
  78. 0x72, 0x04,
  79. 0x73, 0x00,
  80. 0x70, 0x80,
  81. 0x71, 0x70,
  82. 0x72, 0x04,
  83. 0x73, 0x00,
  84. 0x70, 0xa0,
  85. 0x71, 0x70,
  86. 0x72, 0x04,
  87. 0x73, 0x00,
  88. 0x70, 0x1f,
  89. 0x76, 0x00,
  90. 0x77, 0xd1,
  91. 0x78, 0x0c,
  92. 0x79, 0x80,
  93. 0x7f, 0x04,
  94. 0x7c, 0x00,
  95. 0x80, 0x86,
  96. 0x81, 0xa6,
  97. 0x85, 0x04,
  98. 0xcd, 0xf4,
  99. 0x90, 0x33,
  100. 0xa0, 0x44,
  101. 0xc0, 0x18,
  102. 0xc3, 0x10,
  103. 0xc4, 0x08,
  104. 0xc5, 0x80,
  105. 0xc6, 0x80,
  106. 0xc7, 0x0a,
  107. 0xc8, 0x1a,
  108. 0xc9, 0x80,
  109. 0xfe, 0x92,
  110. 0xe0, 0xf8,
  111. 0xe6, 0x8b,
  112. 0xd0, 0x40,
  113. 0xf8, 0x20,
  114. 0xfa, 0x0f,
  115. 0xfd, 0x20,
  116. 0xad, 0x20,
  117. 0xae, 0x07,
  118. 0xb8, 0x00,
  119. };
  120. /* Register values to initialise the demod in DVB-S2 mode */
  121. static u8 ds3000_dvbs2_init_tab[] = {
  122. 0x23, 0x0f,
  123. 0x08, 0x07,
  124. 0x0c, 0x00,
  125. 0x21, 0x54,
  126. 0x25, 0x82,
  127. 0x27, 0x31,
  128. 0x30, 0x08,
  129. 0x31, 0x32,
  130. 0x32, 0x32,
  131. 0x33, 0x35,
  132. 0x35, 0xff,
  133. 0x3a, 0x00,
  134. 0x37, 0x10,
  135. 0x38, 0x10,
  136. 0x39, 0x02,
  137. 0x42, 0x60,
  138. 0x4a, 0x80,
  139. 0x4b, 0x04,
  140. 0x4d, 0x81,
  141. 0x5d, 0x88,
  142. 0x50, 0x36,
  143. 0x51, 0x36,
  144. 0x52, 0x36,
  145. 0x53, 0x36,
  146. 0x63, 0x60,
  147. 0x64, 0x10,
  148. 0x65, 0x10,
  149. 0x68, 0x04,
  150. 0x69, 0x29,
  151. 0x70, 0x20,
  152. 0x71, 0x70,
  153. 0x72, 0x04,
  154. 0x73, 0x00,
  155. 0x70, 0x40,
  156. 0x71, 0x70,
  157. 0x72, 0x04,
  158. 0x73, 0x00,
  159. 0x70, 0x60,
  160. 0x71, 0x70,
  161. 0x72, 0x04,
  162. 0x73, 0x00,
  163. 0x70, 0x80,
  164. 0x71, 0x70,
  165. 0x72, 0x04,
  166. 0x73, 0x00,
  167. 0x70, 0xa0,
  168. 0x71, 0x70,
  169. 0x72, 0x04,
  170. 0x73, 0x00,
  171. 0x70, 0x1f,
  172. 0xa0, 0x44,
  173. 0xc0, 0x08,
  174. 0xc1, 0x10,
  175. 0xc2, 0x08,
  176. 0xc3, 0x10,
  177. 0xc4, 0x08,
  178. 0xc5, 0xf0,
  179. 0xc6, 0xf0,
  180. 0xc7, 0x0a,
  181. 0xc8, 0x1a,
  182. 0xc9, 0x80,
  183. 0xca, 0x23,
  184. 0xcb, 0x24,
  185. 0xce, 0x74,
  186. 0x90, 0x03,
  187. 0x76, 0x80,
  188. 0x77, 0x42,
  189. 0x78, 0x0a,
  190. 0x79, 0x80,
  191. 0xad, 0x40,
  192. 0xae, 0x07,
  193. 0x7f, 0xd4,
  194. 0x7c, 0x00,
  195. 0x80, 0xa8,
  196. 0x81, 0xda,
  197. 0x7c, 0x01,
  198. 0x80, 0xda,
  199. 0x81, 0xec,
  200. 0x7c, 0x02,
  201. 0x80, 0xca,
  202. 0x81, 0xeb,
  203. 0x7c, 0x03,
  204. 0x80, 0xba,
  205. 0x81, 0xdb,
  206. 0x85, 0x08,
  207. 0x86, 0x00,
  208. 0x87, 0x02,
  209. 0x89, 0x80,
  210. 0x8b, 0x44,
  211. 0x8c, 0xaa,
  212. 0x8a, 0x10,
  213. 0xba, 0x00,
  214. 0xf5, 0x04,
  215. 0xfe, 0x44,
  216. 0xd2, 0x32,
  217. 0xb8, 0x00,
  218. };
  219. struct ds3000_state {
  220. struct i2c_adapter *i2c;
  221. const struct ds3000_config *config;
  222. struct dvb_frontend frontend;
  223. /* previous uncorrected block counter for DVB-S2 */
  224. u16 prevUCBS2;
  225. };
  226. static int ds3000_writereg(struct ds3000_state *state, int reg, int data)
  227. {
  228. u8 buf[] = { reg, data };
  229. struct i2c_msg msg = { .addr = state->config->demod_address,
  230. .flags = 0, .buf = buf, .len = 2 };
  231. int err;
  232. dprintk("%s: write reg 0x%02x, value 0x%02x\n", __func__, reg, data);
  233. err = i2c_transfer(state->i2c, &msg, 1);
  234. if (err != 1) {
  235. printk(KERN_ERR "%s: writereg error(err == %i, reg == 0x%02x,"
  236. " value == 0x%02x)\n", __func__, err, reg, data);
  237. return -EREMOTEIO;
  238. }
  239. return 0;
  240. }
  241. static int ds3000_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  242. {
  243. struct ds3000_state *state = fe->demodulator_priv;
  244. if (enable)
  245. ds3000_writereg(state, 0x03, 0x12);
  246. else
  247. ds3000_writereg(state, 0x03, 0x02);
  248. return 0;
  249. }
  250. /* I2C write for 8k firmware load */
  251. static int ds3000_writeFW(struct ds3000_state *state, int reg,
  252. const u8 *data, u16 len)
  253. {
  254. int i, ret = 0;
  255. struct i2c_msg msg;
  256. u8 *buf;
  257. buf = kmalloc(33, GFP_KERNEL);
  258. if (buf == NULL) {
  259. printk(KERN_ERR "Unable to kmalloc\n");
  260. return -ENOMEM;
  261. }
  262. *(buf) = reg;
  263. msg.addr = state->config->demod_address;
  264. msg.flags = 0;
  265. msg.buf = buf;
  266. msg.len = 33;
  267. for (i = 0; i < len; i += 32) {
  268. memcpy(buf + 1, data + i, 32);
  269. dprintk("%s: write reg 0x%02x, len = %d\n", __func__, reg, len);
  270. ret = i2c_transfer(state->i2c, &msg, 1);
  271. if (ret != 1) {
  272. printk(KERN_ERR "%s: write error(err == %i, "
  273. "reg == 0x%02x\n", __func__, ret, reg);
  274. ret = -EREMOTEIO;
  275. goto error;
  276. }
  277. }
  278. ret = 0;
  279. error:
  280. kfree(buf);
  281. return ret;
  282. }
  283. static int ds3000_readreg(struct ds3000_state *state, u8 reg)
  284. {
  285. int ret;
  286. u8 b0[] = { reg };
  287. u8 b1[] = { 0 };
  288. struct i2c_msg msg[] = {
  289. {
  290. .addr = state->config->demod_address,
  291. .flags = 0,
  292. .buf = b0,
  293. .len = 1
  294. }, {
  295. .addr = state->config->demod_address,
  296. .flags = I2C_M_RD,
  297. .buf = b1,
  298. .len = 1
  299. }
  300. };
  301. ret = i2c_transfer(state->i2c, msg, 2);
  302. if (ret != 2) {
  303. printk(KERN_ERR "%s: reg=0x%x(error=%d)\n", __func__, reg, ret);
  304. return ret;
  305. }
  306. dprintk("%s: read reg 0x%02x, value 0x%02x\n", __func__, reg, b1[0]);
  307. return b1[0];
  308. }
  309. static int ds3000_load_firmware(struct dvb_frontend *fe,
  310. const struct firmware *fw);
  311. static int ds3000_firmware_ondemand(struct dvb_frontend *fe)
  312. {
  313. struct ds3000_state *state = fe->demodulator_priv;
  314. const struct firmware *fw;
  315. int ret = 0;
  316. dprintk("%s()\n", __func__);
  317. ret = ds3000_readreg(state, 0xb2);
  318. if (ret < 0)
  319. return ret;
  320. /* Load firmware */
  321. /* request the firmware, this will block until someone uploads it */
  322. printk(KERN_INFO "%s: Waiting for firmware upload (%s)...\n", __func__,
  323. DS3000_DEFAULT_FIRMWARE);
  324. ret = request_firmware(&fw, DS3000_DEFAULT_FIRMWARE,
  325. state->i2c->dev.parent);
  326. printk(KERN_INFO "%s: Waiting for firmware upload(2)...\n", __func__);
  327. if (ret) {
  328. printk(KERN_ERR "%s: No firmware uploaded (timeout or file not "
  329. "found?)\n", __func__);
  330. return ret;
  331. }
  332. ret = ds3000_load_firmware(fe, fw);
  333. if (ret)
  334. printk("%s: Writing firmware to device failed\n", __func__);
  335. release_firmware(fw);
  336. dprintk("%s: Firmware upload %s\n", __func__,
  337. ret == 0 ? "complete" : "failed");
  338. return ret;
  339. }
  340. static int ds3000_load_firmware(struct dvb_frontend *fe,
  341. const struct firmware *fw)
  342. {
  343. struct ds3000_state *state = fe->demodulator_priv;
  344. int ret = 0;
  345. dprintk("%s\n", __func__);
  346. dprintk("Firmware is %zu bytes (%02x %02x .. %02x %02x)\n",
  347. fw->size,
  348. fw->data[0],
  349. fw->data[1],
  350. fw->data[fw->size - 2],
  351. fw->data[fw->size - 1]);
  352. /* Begin the firmware load process */
  353. ds3000_writereg(state, 0xb2, 0x01);
  354. /* write the entire firmware */
  355. ret = ds3000_writeFW(state, 0xb0, fw->data, fw->size);
  356. ds3000_writereg(state, 0xb2, 0x00);
  357. return ret;
  358. }
  359. static int ds3000_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
  360. {
  361. struct ds3000_state *state = fe->demodulator_priv;
  362. u8 data;
  363. dprintk("%s(%d)\n", __func__, voltage);
  364. data = ds3000_readreg(state, 0xa2);
  365. data |= 0x03; /* bit0 V/H, bit1 off/on */
  366. switch (voltage) {
  367. case SEC_VOLTAGE_18:
  368. data &= ~0x03;
  369. break;
  370. case SEC_VOLTAGE_13:
  371. data &= ~0x03;
  372. data |= 0x01;
  373. break;
  374. case SEC_VOLTAGE_OFF:
  375. break;
  376. }
  377. ds3000_writereg(state, 0xa2, data);
  378. return 0;
  379. }
  380. static int ds3000_read_status(struct dvb_frontend *fe, fe_status_t* status)
  381. {
  382. struct ds3000_state *state = fe->demodulator_priv;
  383. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  384. int lock;
  385. *status = 0;
  386. switch (c->delivery_system) {
  387. case SYS_DVBS:
  388. lock = ds3000_readreg(state, 0xd1);
  389. if ((lock & 0x07) == 0x07)
  390. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  391. FE_HAS_VITERBI | FE_HAS_SYNC |
  392. FE_HAS_LOCK;
  393. break;
  394. case SYS_DVBS2:
  395. lock = ds3000_readreg(state, 0x0d);
  396. if ((lock & 0x8f) == 0x8f)
  397. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  398. FE_HAS_VITERBI | FE_HAS_SYNC |
  399. FE_HAS_LOCK;
  400. break;
  401. default:
  402. return 1;
  403. }
  404. if (state->config->set_lock_led)
  405. state->config->set_lock_led(fe, *status == 0 ? 0 : 1);
  406. dprintk("%s: status = 0x%02x\n", __func__, lock);
  407. return 0;
  408. }
  409. /* read DS3000 BER value */
  410. static int ds3000_read_ber(struct dvb_frontend *fe, u32* ber)
  411. {
  412. struct ds3000_state *state = fe->demodulator_priv;
  413. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  414. u8 data;
  415. u32 ber_reading, lpdc_frames;
  416. dprintk("%s()\n", __func__);
  417. switch (c->delivery_system) {
  418. case SYS_DVBS:
  419. /* set the number of bytes checked during
  420. BER estimation */
  421. ds3000_writereg(state, 0xf9, 0x04);
  422. /* read BER estimation status */
  423. data = ds3000_readreg(state, 0xf8);
  424. /* check if BER estimation is ready */
  425. if ((data & 0x10) == 0) {
  426. /* this is the number of error bits,
  427. to calculate the bit error rate
  428. divide to 8388608 */
  429. *ber = (ds3000_readreg(state, 0xf7) << 8) |
  430. ds3000_readreg(state, 0xf6);
  431. /* start counting error bits */
  432. /* need to be set twice
  433. otherwise it fails sometimes */
  434. data |= 0x10;
  435. ds3000_writereg(state, 0xf8, data);
  436. ds3000_writereg(state, 0xf8, data);
  437. } else
  438. /* used to indicate that BER estimation
  439. is not ready, i.e. BER is unknown */
  440. *ber = 0xffffffff;
  441. break;
  442. case SYS_DVBS2:
  443. /* read the number of LPDC decoded frames */
  444. lpdc_frames = (ds3000_readreg(state, 0xd7) << 16) |
  445. (ds3000_readreg(state, 0xd6) << 8) |
  446. ds3000_readreg(state, 0xd5);
  447. /* read the number of packets with bad CRC */
  448. ber_reading = (ds3000_readreg(state, 0xf8) << 8) |
  449. ds3000_readreg(state, 0xf7);
  450. if (lpdc_frames > 750) {
  451. /* clear LPDC frame counters */
  452. ds3000_writereg(state, 0xd1, 0x01);
  453. /* clear bad packets counter */
  454. ds3000_writereg(state, 0xf9, 0x01);
  455. /* enable bad packets counter */
  456. ds3000_writereg(state, 0xf9, 0x00);
  457. /* enable LPDC frame counters */
  458. ds3000_writereg(state, 0xd1, 0x00);
  459. *ber = ber_reading;
  460. } else
  461. /* used to indicate that BER estimation is not ready,
  462. i.e. BER is unknown */
  463. *ber = 0xffffffff;
  464. break;
  465. default:
  466. return 1;
  467. }
  468. return 0;
  469. }
  470. static int ds3000_read_signal_strength(struct dvb_frontend *fe,
  471. u16 *signal_strength)
  472. {
  473. if (fe->ops.tuner_ops.get_rf_strength)
  474. fe->ops.tuner_ops.get_rf_strength(fe, signal_strength);
  475. return 0;
  476. }
  477. /* calculate DS3000 snr value in dB */
  478. static int ds3000_read_snr(struct dvb_frontend *fe, u16 *snr)
  479. {
  480. struct ds3000_state *state = fe->demodulator_priv;
  481. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  482. u8 snr_reading, snr_value;
  483. u32 dvbs2_signal_reading, dvbs2_noise_reading, tmp;
  484. static const u16 dvbs_snr_tab[] = { /* 20 x Table (rounded up) */
  485. 0x0000, 0x1b13, 0x2aea, 0x3627, 0x3ede, 0x45fe, 0x4c03,
  486. 0x513a, 0x55d4, 0x59f2, 0x5dab, 0x6111, 0x6431, 0x6717,
  487. 0x69c9, 0x6c4e, 0x6eac, 0x70e8, 0x7304, 0x7505
  488. };
  489. static const u16 dvbs2_snr_tab[] = { /* 80 x Table (rounded up) */
  490. 0x0000, 0x0bc2, 0x12a3, 0x1785, 0x1b4e, 0x1e65, 0x2103,
  491. 0x2347, 0x2546, 0x2710, 0x28ae, 0x2a28, 0x2b83, 0x2cc5,
  492. 0x2df1, 0x2f09, 0x3010, 0x3109, 0x31f4, 0x32d2, 0x33a6,
  493. 0x3470, 0x3531, 0x35ea, 0x369b, 0x3746, 0x37ea, 0x3888,
  494. 0x3920, 0x39b3, 0x3a42, 0x3acc, 0x3b51, 0x3bd3, 0x3c51,
  495. 0x3ccb, 0x3d42, 0x3db6, 0x3e27, 0x3e95, 0x3f00, 0x3f68,
  496. 0x3fcf, 0x4033, 0x4094, 0x40f4, 0x4151, 0x41ac, 0x4206,
  497. 0x425e, 0x42b4, 0x4308, 0x435b, 0x43ac, 0x43fc, 0x444a,
  498. 0x4497, 0x44e2, 0x452d, 0x4576, 0x45bd, 0x4604, 0x4649,
  499. 0x468e, 0x46d1, 0x4713, 0x4755, 0x4795, 0x47d4, 0x4813,
  500. 0x4851, 0x488d, 0x48c9, 0x4904, 0x493f, 0x4978, 0x49b1,
  501. 0x49e9, 0x4a20, 0x4a57
  502. };
  503. dprintk("%s()\n", __func__);
  504. switch (c->delivery_system) {
  505. case SYS_DVBS:
  506. snr_reading = ds3000_readreg(state, 0xff);
  507. snr_reading /= 8;
  508. if (snr_reading == 0)
  509. *snr = 0x0000;
  510. else {
  511. if (snr_reading > 20)
  512. snr_reading = 20;
  513. snr_value = dvbs_snr_tab[snr_reading - 1] * 10 / 23026;
  514. /* cook the value to be suitable for szap-s2
  515. human readable output */
  516. *snr = snr_value * 8 * 655;
  517. }
  518. dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
  519. snr_reading, *snr);
  520. break;
  521. case SYS_DVBS2:
  522. dvbs2_noise_reading = (ds3000_readreg(state, 0x8c) & 0x3f) +
  523. (ds3000_readreg(state, 0x8d) << 4);
  524. dvbs2_signal_reading = ds3000_readreg(state, 0x8e);
  525. tmp = dvbs2_signal_reading * dvbs2_signal_reading >> 1;
  526. if (tmp == 0) {
  527. *snr = 0x0000;
  528. return 0;
  529. }
  530. if (dvbs2_noise_reading == 0) {
  531. snr_value = 0x0013;
  532. /* cook the value to be suitable for szap-s2
  533. human readable output */
  534. *snr = 0xffff;
  535. return 0;
  536. }
  537. if (tmp > dvbs2_noise_reading) {
  538. snr_reading = tmp / dvbs2_noise_reading;
  539. if (snr_reading > 80)
  540. snr_reading = 80;
  541. snr_value = dvbs2_snr_tab[snr_reading - 1] / 1000;
  542. /* cook the value to be suitable for szap-s2
  543. human readable output */
  544. *snr = snr_value * 5 * 655;
  545. } else {
  546. snr_reading = dvbs2_noise_reading / tmp;
  547. if (snr_reading > 80)
  548. snr_reading = 80;
  549. *snr = -(dvbs2_snr_tab[snr_reading] / 1000);
  550. }
  551. dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
  552. snr_reading, *snr);
  553. break;
  554. default:
  555. return 1;
  556. }
  557. return 0;
  558. }
  559. /* read DS3000 uncorrected blocks */
  560. static int ds3000_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  561. {
  562. struct ds3000_state *state = fe->demodulator_priv;
  563. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  564. u8 data;
  565. u16 _ucblocks;
  566. dprintk("%s()\n", __func__);
  567. switch (c->delivery_system) {
  568. case SYS_DVBS:
  569. *ucblocks = (ds3000_readreg(state, 0xf5) << 8) |
  570. ds3000_readreg(state, 0xf4);
  571. data = ds3000_readreg(state, 0xf8);
  572. /* clear packet counters */
  573. data &= ~0x20;
  574. ds3000_writereg(state, 0xf8, data);
  575. /* enable packet counters */
  576. data |= 0x20;
  577. ds3000_writereg(state, 0xf8, data);
  578. break;
  579. case SYS_DVBS2:
  580. _ucblocks = (ds3000_readreg(state, 0xe2) << 8) |
  581. ds3000_readreg(state, 0xe1);
  582. if (_ucblocks > state->prevUCBS2)
  583. *ucblocks = _ucblocks - state->prevUCBS2;
  584. else
  585. *ucblocks = state->prevUCBS2 - _ucblocks;
  586. state->prevUCBS2 = _ucblocks;
  587. break;
  588. default:
  589. return 1;
  590. }
  591. return 0;
  592. }
  593. static int ds3000_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
  594. {
  595. struct ds3000_state *state = fe->demodulator_priv;
  596. u8 data;
  597. dprintk("%s(%d)\n", __func__, tone);
  598. if ((tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF)) {
  599. printk(KERN_ERR "%s: Invalid, tone=%d\n", __func__, tone);
  600. return -EINVAL;
  601. }
  602. data = ds3000_readreg(state, 0xa2);
  603. data &= ~0xc0;
  604. ds3000_writereg(state, 0xa2, data);
  605. switch (tone) {
  606. case SEC_TONE_ON:
  607. dprintk("%s: setting tone on\n", __func__);
  608. data = ds3000_readreg(state, 0xa1);
  609. data &= ~0x43;
  610. data |= 0x04;
  611. ds3000_writereg(state, 0xa1, data);
  612. break;
  613. case SEC_TONE_OFF:
  614. dprintk("%s: setting tone off\n", __func__);
  615. data = ds3000_readreg(state, 0xa2);
  616. data |= 0x80;
  617. ds3000_writereg(state, 0xa2, data);
  618. break;
  619. }
  620. return 0;
  621. }
  622. static int ds3000_send_diseqc_msg(struct dvb_frontend *fe,
  623. struct dvb_diseqc_master_cmd *d)
  624. {
  625. struct ds3000_state *state = fe->demodulator_priv;
  626. int i;
  627. u8 data;
  628. /* Dump DiSEqC message */
  629. dprintk("%s(", __func__);
  630. for (i = 0 ; i < d->msg_len;) {
  631. dprintk("0x%02x", d->msg[i]);
  632. if (++i < d->msg_len)
  633. dprintk(", ");
  634. }
  635. /* enable DiSEqC message send pin */
  636. data = ds3000_readreg(state, 0xa2);
  637. data &= ~0xc0;
  638. ds3000_writereg(state, 0xa2, data);
  639. /* DiSEqC message */
  640. for (i = 0; i < d->msg_len; i++)
  641. ds3000_writereg(state, 0xa3 + i, d->msg[i]);
  642. data = ds3000_readreg(state, 0xa1);
  643. /* clear DiSEqC message length and status,
  644. enable DiSEqC message send */
  645. data &= ~0xf8;
  646. /* set DiSEqC mode, modulation active during 33 pulses,
  647. set DiSEqC message length */
  648. data |= ((d->msg_len - 1) << 3) | 0x07;
  649. ds3000_writereg(state, 0xa1, data);
  650. /* wait up to 150ms for DiSEqC transmission to complete */
  651. for (i = 0; i < 15; i++) {
  652. data = ds3000_readreg(state, 0xa1);
  653. if ((data & 0x40) == 0)
  654. break;
  655. msleep(10);
  656. }
  657. /* DiSEqC timeout after 150ms */
  658. if (i == 15) {
  659. data = ds3000_readreg(state, 0xa1);
  660. data &= ~0x80;
  661. data |= 0x40;
  662. ds3000_writereg(state, 0xa1, data);
  663. data = ds3000_readreg(state, 0xa2);
  664. data &= ~0xc0;
  665. data |= 0x80;
  666. ds3000_writereg(state, 0xa2, data);
  667. return 1;
  668. }
  669. data = ds3000_readreg(state, 0xa2);
  670. data &= ~0xc0;
  671. data |= 0x80;
  672. ds3000_writereg(state, 0xa2, data);
  673. return 0;
  674. }
  675. /* Send DiSEqC burst */
  676. static int ds3000_diseqc_send_burst(struct dvb_frontend *fe,
  677. fe_sec_mini_cmd_t burst)
  678. {
  679. struct ds3000_state *state = fe->demodulator_priv;
  680. int i;
  681. u8 data;
  682. dprintk("%s()\n", __func__);
  683. data = ds3000_readreg(state, 0xa2);
  684. data &= ~0xc0;
  685. ds3000_writereg(state, 0xa2, data);
  686. /* DiSEqC burst */
  687. if (burst == SEC_MINI_A)
  688. /* Unmodulated tone burst */
  689. ds3000_writereg(state, 0xa1, 0x02);
  690. else if (burst == SEC_MINI_B)
  691. /* Modulated tone burst */
  692. ds3000_writereg(state, 0xa1, 0x01);
  693. else
  694. return -EINVAL;
  695. msleep(13);
  696. for (i = 0; i < 5; i++) {
  697. data = ds3000_readreg(state, 0xa1);
  698. if ((data & 0x40) == 0)
  699. break;
  700. msleep(1);
  701. }
  702. if (i == 5) {
  703. data = ds3000_readreg(state, 0xa1);
  704. data &= ~0x80;
  705. data |= 0x40;
  706. ds3000_writereg(state, 0xa1, data);
  707. data = ds3000_readreg(state, 0xa2);
  708. data &= ~0xc0;
  709. data |= 0x80;
  710. ds3000_writereg(state, 0xa2, data);
  711. return 1;
  712. }
  713. data = ds3000_readreg(state, 0xa2);
  714. data &= ~0xc0;
  715. data |= 0x80;
  716. ds3000_writereg(state, 0xa2, data);
  717. return 0;
  718. }
  719. static void ds3000_release(struct dvb_frontend *fe)
  720. {
  721. struct ds3000_state *state = fe->demodulator_priv;
  722. if (state->config->set_lock_led)
  723. state->config->set_lock_led(fe, 0);
  724. dprintk("%s\n", __func__);
  725. kfree(state);
  726. }
  727. static struct dvb_frontend_ops ds3000_ops;
  728. struct dvb_frontend *ds3000_attach(const struct ds3000_config *config,
  729. struct i2c_adapter *i2c)
  730. {
  731. struct ds3000_state *state = NULL;
  732. int ret;
  733. dprintk("%s\n", __func__);
  734. /* allocate memory for the internal state */
  735. state = kzalloc(sizeof(struct ds3000_state), GFP_KERNEL);
  736. if (state == NULL) {
  737. printk(KERN_ERR "Unable to kmalloc\n");
  738. goto error2;
  739. }
  740. state->config = config;
  741. state->i2c = i2c;
  742. state->prevUCBS2 = 0;
  743. /* check if the demod is present */
  744. ret = ds3000_readreg(state, 0x00) & 0xfe;
  745. if (ret != 0xe0) {
  746. printk(KERN_ERR "Invalid probe, probably not a DS3000\n");
  747. goto error3;
  748. }
  749. printk(KERN_INFO "DS3000 chip version: %d.%d attached.\n",
  750. ds3000_readreg(state, 0x02),
  751. ds3000_readreg(state, 0x01));
  752. memcpy(&state->frontend.ops, &ds3000_ops,
  753. sizeof(struct dvb_frontend_ops));
  754. state->frontend.demodulator_priv = state;
  755. return &state->frontend;
  756. error3:
  757. kfree(state);
  758. error2:
  759. return NULL;
  760. }
  761. EXPORT_SYMBOL(ds3000_attach);
  762. static int ds3000_set_carrier_offset(struct dvb_frontend *fe,
  763. s32 carrier_offset_khz)
  764. {
  765. struct ds3000_state *state = fe->demodulator_priv;
  766. s32 tmp;
  767. tmp = carrier_offset_khz;
  768. tmp *= 65536;
  769. tmp = (2 * tmp + DS3000_SAMPLE_RATE) / (2 * DS3000_SAMPLE_RATE);
  770. if (tmp < 0)
  771. tmp += 65536;
  772. ds3000_writereg(state, 0x5f, tmp >> 8);
  773. ds3000_writereg(state, 0x5e, tmp & 0xff);
  774. return 0;
  775. }
  776. static int ds3000_set_frontend(struct dvb_frontend *fe)
  777. {
  778. struct ds3000_state *state = fe->demodulator_priv;
  779. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  780. int i;
  781. fe_status_t status;
  782. s32 offset_khz;
  783. u32 frequency;
  784. u16 value;
  785. dprintk("%s() ", __func__);
  786. if (state->config->set_ts_params)
  787. state->config->set_ts_params(fe, 0);
  788. /* Tune */
  789. if (fe->ops.tuner_ops.set_params)
  790. fe->ops.tuner_ops.set_params(fe);
  791. /* ds3000 global reset */
  792. ds3000_writereg(state, 0x07, 0x80);
  793. ds3000_writereg(state, 0x07, 0x00);
  794. /* ds3000 build-in uC reset */
  795. ds3000_writereg(state, 0xb2, 0x01);
  796. /* ds3000 software reset */
  797. ds3000_writereg(state, 0x00, 0x01);
  798. switch (c->delivery_system) {
  799. case SYS_DVBS:
  800. /* initialise the demod in DVB-S mode */
  801. for (i = 0; i < sizeof(ds3000_dvbs_init_tab); i += 2)
  802. ds3000_writereg(state,
  803. ds3000_dvbs_init_tab[i],
  804. ds3000_dvbs_init_tab[i + 1]);
  805. value = ds3000_readreg(state, 0xfe);
  806. value &= 0xc0;
  807. value |= 0x1b;
  808. ds3000_writereg(state, 0xfe, value);
  809. break;
  810. case SYS_DVBS2:
  811. /* initialise the demod in DVB-S2 mode */
  812. for (i = 0; i < sizeof(ds3000_dvbs2_init_tab); i += 2)
  813. ds3000_writereg(state,
  814. ds3000_dvbs2_init_tab[i],
  815. ds3000_dvbs2_init_tab[i + 1]);
  816. if (c->symbol_rate >= 30000000)
  817. ds3000_writereg(state, 0xfe, 0x54);
  818. else
  819. ds3000_writereg(state, 0xfe, 0x98);
  820. break;
  821. default:
  822. return 1;
  823. }
  824. /* enable 27MHz clock output */
  825. ds3000_writereg(state, 0x29, 0x80);
  826. /* enable ac coupling */
  827. ds3000_writereg(state, 0x25, 0x8a);
  828. /* enhance symbol rate performance */
  829. if ((c->symbol_rate / 1000) <= 5000) {
  830. value = 29777 / (c->symbol_rate / 1000) + 1;
  831. if (value % 2 != 0)
  832. value++;
  833. ds3000_writereg(state, 0xc3, 0x0d);
  834. ds3000_writereg(state, 0xc8, value);
  835. ds3000_writereg(state, 0xc4, 0x10);
  836. ds3000_writereg(state, 0xc7, 0x0e);
  837. } else if ((c->symbol_rate / 1000) <= 10000) {
  838. value = 92166 / (c->symbol_rate / 1000) + 1;
  839. if (value % 2 != 0)
  840. value++;
  841. ds3000_writereg(state, 0xc3, 0x07);
  842. ds3000_writereg(state, 0xc8, value);
  843. ds3000_writereg(state, 0xc4, 0x09);
  844. ds3000_writereg(state, 0xc7, 0x12);
  845. } else if ((c->symbol_rate / 1000) <= 20000) {
  846. value = 64516 / (c->symbol_rate / 1000) + 1;
  847. ds3000_writereg(state, 0xc3, value);
  848. ds3000_writereg(state, 0xc8, 0x0e);
  849. ds3000_writereg(state, 0xc4, 0x07);
  850. ds3000_writereg(state, 0xc7, 0x18);
  851. } else {
  852. value = 129032 / (c->symbol_rate / 1000) + 1;
  853. ds3000_writereg(state, 0xc3, value);
  854. ds3000_writereg(state, 0xc8, 0x0a);
  855. ds3000_writereg(state, 0xc4, 0x05);
  856. ds3000_writereg(state, 0xc7, 0x24);
  857. }
  858. /* normalized symbol rate rounded to the closest integer */
  859. value = (((c->symbol_rate / 1000) << 16) +
  860. (DS3000_SAMPLE_RATE / 2)) / DS3000_SAMPLE_RATE;
  861. ds3000_writereg(state, 0x61, value & 0x00ff);
  862. ds3000_writereg(state, 0x62, (value & 0xff00) >> 8);
  863. /* co-channel interference cancellation disabled */
  864. ds3000_writereg(state, 0x56, 0x00);
  865. /* equalizer disabled */
  866. ds3000_writereg(state, 0x76, 0x00);
  867. /*ds3000_writereg(state, 0x08, 0x03);
  868. ds3000_writereg(state, 0xfd, 0x22);
  869. ds3000_writereg(state, 0x08, 0x07);
  870. ds3000_writereg(state, 0xfd, 0x42);
  871. ds3000_writereg(state, 0x08, 0x07);*/
  872. if (state->config->ci_mode) {
  873. switch (c->delivery_system) {
  874. case SYS_DVBS:
  875. default:
  876. ds3000_writereg(state, 0xfd, 0x80);
  877. break;
  878. case SYS_DVBS2:
  879. ds3000_writereg(state, 0xfd, 0x01);
  880. break;
  881. }
  882. }
  883. /* ds3000 out of software reset */
  884. ds3000_writereg(state, 0x00, 0x00);
  885. /* start ds3000 build-in uC */
  886. ds3000_writereg(state, 0xb2, 0x00);
  887. if (fe->ops.tuner_ops.get_frequency) {
  888. fe->ops.tuner_ops.get_frequency(fe, &frequency);
  889. offset_khz = frequency - c->frequency;
  890. ds3000_set_carrier_offset(fe, offset_khz);
  891. }
  892. for (i = 0; i < 30 ; i++) {
  893. ds3000_read_status(fe, &status);
  894. if (status & FE_HAS_LOCK)
  895. break;
  896. msleep(10);
  897. }
  898. return 0;
  899. }
  900. static int ds3000_tune(struct dvb_frontend *fe,
  901. bool re_tune,
  902. unsigned int mode_flags,
  903. unsigned int *delay,
  904. fe_status_t *status)
  905. {
  906. if (re_tune) {
  907. int ret = ds3000_set_frontend(fe);
  908. if (ret)
  909. return ret;
  910. }
  911. *delay = HZ / 5;
  912. return ds3000_read_status(fe, status);
  913. }
  914. static enum dvbfe_algo ds3000_get_algo(struct dvb_frontend *fe)
  915. {
  916. struct ds3000_state *state = fe->demodulator_priv;
  917. if (state->config->set_lock_led)
  918. state->config->set_lock_led(fe, 0);
  919. dprintk("%s()\n", __func__);
  920. return DVBFE_ALGO_HW;
  921. }
  922. /*
  923. * Initialise or wake up device
  924. *
  925. * Power config will reset and load initial firmware if required
  926. */
  927. static int ds3000_initfe(struct dvb_frontend *fe)
  928. {
  929. struct ds3000_state *state = fe->demodulator_priv;
  930. int ret;
  931. dprintk("%s()\n", __func__);
  932. /* hard reset */
  933. ds3000_writereg(state, 0x08, 0x01 | ds3000_readreg(state, 0x08));
  934. msleep(1);
  935. /* Load the firmware if required */
  936. ret = ds3000_firmware_ondemand(fe);
  937. if (ret != 0) {
  938. printk(KERN_ERR "%s: Unable initialize firmware\n", __func__);
  939. return ret;
  940. }
  941. return 0;
  942. }
  943. static struct dvb_frontend_ops ds3000_ops = {
  944. .delsys = { SYS_DVBS, SYS_DVBS2 },
  945. .info = {
  946. .name = "Montage Technology DS3000",
  947. .frequency_min = 950000,
  948. .frequency_max = 2150000,
  949. .frequency_stepsize = 1011, /* kHz for QPSK frontends */
  950. .frequency_tolerance = 5000,
  951. .symbol_rate_min = 1000000,
  952. .symbol_rate_max = 45000000,
  953. .caps = FE_CAN_INVERSION_AUTO |
  954. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  955. FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
  956. FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  957. FE_CAN_2G_MODULATION |
  958. FE_CAN_QPSK | FE_CAN_RECOVER
  959. },
  960. .release = ds3000_release,
  961. .init = ds3000_initfe,
  962. .i2c_gate_ctrl = ds3000_i2c_gate_ctrl,
  963. .read_status = ds3000_read_status,
  964. .read_ber = ds3000_read_ber,
  965. .read_signal_strength = ds3000_read_signal_strength,
  966. .read_snr = ds3000_read_snr,
  967. .read_ucblocks = ds3000_read_ucblocks,
  968. .set_voltage = ds3000_set_voltage,
  969. .set_tone = ds3000_set_tone,
  970. .diseqc_send_master_cmd = ds3000_send_diseqc_msg,
  971. .diseqc_send_burst = ds3000_diseqc_send_burst,
  972. .get_frontend_algo = ds3000_get_algo,
  973. .set_frontend = ds3000_set_frontend,
  974. .tune = ds3000_tune,
  975. };
  976. module_param(debug, int, 0644);
  977. MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
  978. MODULE_DESCRIPTION("DVB Frontend module for Montage Technology "
  979. "DS3000 hardware");
  980. MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");
  981. MODULE_LICENSE("GPL");
  982. MODULE_FIRMWARE(DS3000_DEFAULT_FIRMWARE);