ds3000.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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 - 1] / 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. /*
  756. * Some devices like T480 starts with voltage on. Be sure
  757. * to turn voltage off during init, as this can otherwise
  758. * interfere with Unicable SCR systems.
  759. */
  760. ds3000_set_voltage(&state->frontend, SEC_VOLTAGE_OFF);
  761. return &state->frontend;
  762. error3:
  763. kfree(state);
  764. error2:
  765. return NULL;
  766. }
  767. EXPORT_SYMBOL(ds3000_attach);
  768. static int ds3000_set_carrier_offset(struct dvb_frontend *fe,
  769. s32 carrier_offset_khz)
  770. {
  771. struct ds3000_state *state = fe->demodulator_priv;
  772. s32 tmp;
  773. tmp = carrier_offset_khz;
  774. tmp *= 65536;
  775. tmp = (2 * tmp + DS3000_SAMPLE_RATE) / (2 * DS3000_SAMPLE_RATE);
  776. if (tmp < 0)
  777. tmp += 65536;
  778. ds3000_writereg(state, 0x5f, tmp >> 8);
  779. ds3000_writereg(state, 0x5e, tmp & 0xff);
  780. return 0;
  781. }
  782. static int ds3000_set_frontend(struct dvb_frontend *fe)
  783. {
  784. struct ds3000_state *state = fe->demodulator_priv;
  785. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  786. int i;
  787. fe_status_t status;
  788. s32 offset_khz;
  789. u32 frequency;
  790. u16 value;
  791. dprintk("%s() ", __func__);
  792. if (state->config->set_ts_params)
  793. state->config->set_ts_params(fe, 0);
  794. /* Tune */
  795. if (fe->ops.tuner_ops.set_params)
  796. fe->ops.tuner_ops.set_params(fe);
  797. /* ds3000 global reset */
  798. ds3000_writereg(state, 0x07, 0x80);
  799. ds3000_writereg(state, 0x07, 0x00);
  800. /* ds3000 build-in uC reset */
  801. ds3000_writereg(state, 0xb2, 0x01);
  802. /* ds3000 software reset */
  803. ds3000_writereg(state, 0x00, 0x01);
  804. switch (c->delivery_system) {
  805. case SYS_DVBS:
  806. /* initialise the demod in DVB-S mode */
  807. for (i = 0; i < sizeof(ds3000_dvbs_init_tab); i += 2)
  808. ds3000_writereg(state,
  809. ds3000_dvbs_init_tab[i],
  810. ds3000_dvbs_init_tab[i + 1]);
  811. value = ds3000_readreg(state, 0xfe);
  812. value &= 0xc0;
  813. value |= 0x1b;
  814. ds3000_writereg(state, 0xfe, value);
  815. break;
  816. case SYS_DVBS2:
  817. /* initialise the demod in DVB-S2 mode */
  818. for (i = 0; i < sizeof(ds3000_dvbs2_init_tab); i += 2)
  819. ds3000_writereg(state,
  820. ds3000_dvbs2_init_tab[i],
  821. ds3000_dvbs2_init_tab[i + 1]);
  822. if (c->symbol_rate >= 30000000)
  823. ds3000_writereg(state, 0xfe, 0x54);
  824. else
  825. ds3000_writereg(state, 0xfe, 0x98);
  826. break;
  827. default:
  828. return 1;
  829. }
  830. /* enable 27MHz clock output */
  831. ds3000_writereg(state, 0x29, 0x80);
  832. /* enable ac coupling */
  833. ds3000_writereg(state, 0x25, 0x8a);
  834. /* enhance symbol rate performance */
  835. if ((c->symbol_rate / 1000) <= 5000) {
  836. value = 29777 / (c->symbol_rate / 1000) + 1;
  837. if (value % 2 != 0)
  838. value++;
  839. ds3000_writereg(state, 0xc3, 0x0d);
  840. ds3000_writereg(state, 0xc8, value);
  841. ds3000_writereg(state, 0xc4, 0x10);
  842. ds3000_writereg(state, 0xc7, 0x0e);
  843. } else if ((c->symbol_rate / 1000) <= 10000) {
  844. value = 92166 / (c->symbol_rate / 1000) + 1;
  845. if (value % 2 != 0)
  846. value++;
  847. ds3000_writereg(state, 0xc3, 0x07);
  848. ds3000_writereg(state, 0xc8, value);
  849. ds3000_writereg(state, 0xc4, 0x09);
  850. ds3000_writereg(state, 0xc7, 0x12);
  851. } else if ((c->symbol_rate / 1000) <= 20000) {
  852. value = 64516 / (c->symbol_rate / 1000) + 1;
  853. ds3000_writereg(state, 0xc3, value);
  854. ds3000_writereg(state, 0xc8, 0x0e);
  855. ds3000_writereg(state, 0xc4, 0x07);
  856. ds3000_writereg(state, 0xc7, 0x18);
  857. } else {
  858. value = 129032 / (c->symbol_rate / 1000) + 1;
  859. ds3000_writereg(state, 0xc3, value);
  860. ds3000_writereg(state, 0xc8, 0x0a);
  861. ds3000_writereg(state, 0xc4, 0x05);
  862. ds3000_writereg(state, 0xc7, 0x24);
  863. }
  864. /* normalized symbol rate rounded to the closest integer */
  865. value = (((c->symbol_rate / 1000) << 16) +
  866. (DS3000_SAMPLE_RATE / 2)) / DS3000_SAMPLE_RATE;
  867. ds3000_writereg(state, 0x61, value & 0x00ff);
  868. ds3000_writereg(state, 0x62, (value & 0xff00) >> 8);
  869. /* co-channel interference cancellation disabled */
  870. ds3000_writereg(state, 0x56, 0x00);
  871. /* equalizer disabled */
  872. ds3000_writereg(state, 0x76, 0x00);
  873. /*ds3000_writereg(state, 0x08, 0x03);
  874. ds3000_writereg(state, 0xfd, 0x22);
  875. ds3000_writereg(state, 0x08, 0x07);
  876. ds3000_writereg(state, 0xfd, 0x42);
  877. ds3000_writereg(state, 0x08, 0x07);*/
  878. if (state->config->ci_mode) {
  879. switch (c->delivery_system) {
  880. case SYS_DVBS:
  881. default:
  882. ds3000_writereg(state, 0xfd, 0x80);
  883. break;
  884. case SYS_DVBS2:
  885. ds3000_writereg(state, 0xfd, 0x01);
  886. break;
  887. }
  888. }
  889. /* ds3000 out of software reset */
  890. ds3000_writereg(state, 0x00, 0x00);
  891. /* start ds3000 build-in uC */
  892. ds3000_writereg(state, 0xb2, 0x00);
  893. if (fe->ops.tuner_ops.get_frequency) {
  894. fe->ops.tuner_ops.get_frequency(fe, &frequency);
  895. offset_khz = frequency - c->frequency;
  896. ds3000_set_carrier_offset(fe, offset_khz);
  897. }
  898. for (i = 0; i < 30 ; i++) {
  899. ds3000_read_status(fe, &status);
  900. if (status & FE_HAS_LOCK)
  901. break;
  902. msleep(10);
  903. }
  904. return 0;
  905. }
  906. static int ds3000_tune(struct dvb_frontend *fe,
  907. bool re_tune,
  908. unsigned int mode_flags,
  909. unsigned int *delay,
  910. fe_status_t *status)
  911. {
  912. if (re_tune) {
  913. int ret = ds3000_set_frontend(fe);
  914. if (ret)
  915. return ret;
  916. }
  917. *delay = HZ / 5;
  918. return ds3000_read_status(fe, status);
  919. }
  920. static enum dvbfe_algo ds3000_get_algo(struct dvb_frontend *fe)
  921. {
  922. struct ds3000_state *state = fe->demodulator_priv;
  923. if (state->config->set_lock_led)
  924. state->config->set_lock_led(fe, 0);
  925. dprintk("%s()\n", __func__);
  926. return DVBFE_ALGO_HW;
  927. }
  928. /*
  929. * Initialise or wake up device
  930. *
  931. * Power config will reset and load initial firmware if required
  932. */
  933. static int ds3000_initfe(struct dvb_frontend *fe)
  934. {
  935. struct ds3000_state *state = fe->demodulator_priv;
  936. int ret;
  937. dprintk("%s()\n", __func__);
  938. /* hard reset */
  939. ds3000_writereg(state, 0x08, 0x01 | ds3000_readreg(state, 0x08));
  940. msleep(1);
  941. /* Load the firmware if required */
  942. ret = ds3000_firmware_ondemand(fe);
  943. if (ret != 0) {
  944. printk(KERN_ERR "%s: Unable initialize firmware\n", __func__);
  945. return ret;
  946. }
  947. return 0;
  948. }
  949. static struct dvb_frontend_ops ds3000_ops = {
  950. .delsys = { SYS_DVBS, SYS_DVBS2 },
  951. .info = {
  952. .name = "Montage Technology DS3000",
  953. .frequency_min = 950000,
  954. .frequency_max = 2150000,
  955. .frequency_stepsize = 1011, /* kHz for QPSK frontends */
  956. .frequency_tolerance = 5000,
  957. .symbol_rate_min = 1000000,
  958. .symbol_rate_max = 45000000,
  959. .caps = FE_CAN_INVERSION_AUTO |
  960. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  961. FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
  962. FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  963. FE_CAN_2G_MODULATION |
  964. FE_CAN_QPSK | FE_CAN_RECOVER
  965. },
  966. .release = ds3000_release,
  967. .init = ds3000_initfe,
  968. .i2c_gate_ctrl = ds3000_i2c_gate_ctrl,
  969. .read_status = ds3000_read_status,
  970. .read_ber = ds3000_read_ber,
  971. .read_signal_strength = ds3000_read_signal_strength,
  972. .read_snr = ds3000_read_snr,
  973. .read_ucblocks = ds3000_read_ucblocks,
  974. .set_voltage = ds3000_set_voltage,
  975. .set_tone = ds3000_set_tone,
  976. .diseqc_send_master_cmd = ds3000_send_diseqc_msg,
  977. .diseqc_send_burst = ds3000_diseqc_send_burst,
  978. .get_frontend_algo = ds3000_get_algo,
  979. .set_frontend = ds3000_set_frontend,
  980. .tune = ds3000_tune,
  981. };
  982. module_param(debug, int, 0644);
  983. MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
  984. MODULE_DESCRIPTION("DVB Frontend module for Montage Technology "
  985. "DS3000 hardware");
  986. MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");
  987. MODULE_LICENSE("GPL");
  988. MODULE_FIRMWARE(DS3000_DEFAULT_FIRMWARE);