ds3000.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. /* calculate DS3000 snr value in dB */
  471. static int ds3000_read_snr(struct dvb_frontend *fe, u16 *snr)
  472. {
  473. struct ds3000_state *state = fe->demodulator_priv;
  474. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  475. u8 snr_reading, snr_value;
  476. u32 dvbs2_signal_reading, dvbs2_noise_reading, tmp;
  477. static const u16 dvbs_snr_tab[] = { /* 20 x Table (rounded up) */
  478. 0x0000, 0x1b13, 0x2aea, 0x3627, 0x3ede, 0x45fe, 0x4c03,
  479. 0x513a, 0x55d4, 0x59f2, 0x5dab, 0x6111, 0x6431, 0x6717,
  480. 0x69c9, 0x6c4e, 0x6eac, 0x70e8, 0x7304, 0x7505
  481. };
  482. static const u16 dvbs2_snr_tab[] = { /* 80 x Table (rounded up) */
  483. 0x0000, 0x0bc2, 0x12a3, 0x1785, 0x1b4e, 0x1e65, 0x2103,
  484. 0x2347, 0x2546, 0x2710, 0x28ae, 0x2a28, 0x2b83, 0x2cc5,
  485. 0x2df1, 0x2f09, 0x3010, 0x3109, 0x31f4, 0x32d2, 0x33a6,
  486. 0x3470, 0x3531, 0x35ea, 0x369b, 0x3746, 0x37ea, 0x3888,
  487. 0x3920, 0x39b3, 0x3a42, 0x3acc, 0x3b51, 0x3bd3, 0x3c51,
  488. 0x3ccb, 0x3d42, 0x3db6, 0x3e27, 0x3e95, 0x3f00, 0x3f68,
  489. 0x3fcf, 0x4033, 0x4094, 0x40f4, 0x4151, 0x41ac, 0x4206,
  490. 0x425e, 0x42b4, 0x4308, 0x435b, 0x43ac, 0x43fc, 0x444a,
  491. 0x4497, 0x44e2, 0x452d, 0x4576, 0x45bd, 0x4604, 0x4649,
  492. 0x468e, 0x46d1, 0x4713, 0x4755, 0x4795, 0x47d4, 0x4813,
  493. 0x4851, 0x488d, 0x48c9, 0x4904, 0x493f, 0x4978, 0x49b1,
  494. 0x49e9, 0x4a20, 0x4a57
  495. };
  496. dprintk("%s()\n", __func__);
  497. switch (c->delivery_system) {
  498. case SYS_DVBS:
  499. snr_reading = ds3000_readreg(state, 0xff);
  500. snr_reading /= 8;
  501. if (snr_reading == 0)
  502. *snr = 0x0000;
  503. else {
  504. if (snr_reading > 20)
  505. snr_reading = 20;
  506. snr_value = dvbs_snr_tab[snr_reading - 1] * 10 / 23026;
  507. /* cook the value to be suitable for szap-s2
  508. human readable output */
  509. *snr = snr_value * 8 * 655;
  510. }
  511. dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
  512. snr_reading, *snr);
  513. break;
  514. case SYS_DVBS2:
  515. dvbs2_noise_reading = (ds3000_readreg(state, 0x8c) & 0x3f) +
  516. (ds3000_readreg(state, 0x8d) << 4);
  517. dvbs2_signal_reading = ds3000_readreg(state, 0x8e);
  518. tmp = dvbs2_signal_reading * dvbs2_signal_reading >> 1;
  519. if (tmp == 0) {
  520. *snr = 0x0000;
  521. return 0;
  522. }
  523. if (dvbs2_noise_reading == 0) {
  524. snr_value = 0x0013;
  525. /* cook the value to be suitable for szap-s2
  526. human readable output */
  527. *snr = 0xffff;
  528. return 0;
  529. }
  530. if (tmp > dvbs2_noise_reading) {
  531. snr_reading = tmp / dvbs2_noise_reading;
  532. if (snr_reading > 80)
  533. snr_reading = 80;
  534. snr_value = dvbs2_snr_tab[snr_reading - 1] / 1000;
  535. /* cook the value to be suitable for szap-s2
  536. human readable output */
  537. *snr = snr_value * 5 * 655;
  538. } else {
  539. snr_reading = dvbs2_noise_reading / tmp;
  540. if (snr_reading > 80)
  541. snr_reading = 80;
  542. *snr = -(dvbs2_snr_tab[snr_reading] / 1000);
  543. }
  544. dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
  545. snr_reading, *snr);
  546. break;
  547. default:
  548. return 1;
  549. }
  550. return 0;
  551. }
  552. /* read DS3000 uncorrected blocks */
  553. static int ds3000_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  554. {
  555. struct ds3000_state *state = fe->demodulator_priv;
  556. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  557. u8 data;
  558. u16 _ucblocks;
  559. dprintk("%s()\n", __func__);
  560. switch (c->delivery_system) {
  561. case SYS_DVBS:
  562. *ucblocks = (ds3000_readreg(state, 0xf5) << 8) |
  563. ds3000_readreg(state, 0xf4);
  564. data = ds3000_readreg(state, 0xf8);
  565. /* clear packet counters */
  566. data &= ~0x20;
  567. ds3000_writereg(state, 0xf8, data);
  568. /* enable packet counters */
  569. data |= 0x20;
  570. ds3000_writereg(state, 0xf8, data);
  571. break;
  572. case SYS_DVBS2:
  573. _ucblocks = (ds3000_readreg(state, 0xe2) << 8) |
  574. ds3000_readreg(state, 0xe1);
  575. if (_ucblocks > state->prevUCBS2)
  576. *ucblocks = _ucblocks - state->prevUCBS2;
  577. else
  578. *ucblocks = state->prevUCBS2 - _ucblocks;
  579. state->prevUCBS2 = _ucblocks;
  580. break;
  581. default:
  582. return 1;
  583. }
  584. return 0;
  585. }
  586. static int ds3000_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
  587. {
  588. struct ds3000_state *state = fe->demodulator_priv;
  589. u8 data;
  590. dprintk("%s(%d)\n", __func__, tone);
  591. if ((tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF)) {
  592. printk(KERN_ERR "%s: Invalid, tone=%d\n", __func__, tone);
  593. return -EINVAL;
  594. }
  595. data = ds3000_readreg(state, 0xa2);
  596. data &= ~0xc0;
  597. ds3000_writereg(state, 0xa2, data);
  598. switch (tone) {
  599. case SEC_TONE_ON:
  600. dprintk("%s: setting tone on\n", __func__);
  601. data = ds3000_readreg(state, 0xa1);
  602. data &= ~0x43;
  603. data |= 0x04;
  604. ds3000_writereg(state, 0xa1, data);
  605. break;
  606. case SEC_TONE_OFF:
  607. dprintk("%s: setting tone off\n", __func__);
  608. data = ds3000_readreg(state, 0xa2);
  609. data |= 0x80;
  610. ds3000_writereg(state, 0xa2, data);
  611. break;
  612. }
  613. return 0;
  614. }
  615. static int ds3000_send_diseqc_msg(struct dvb_frontend *fe,
  616. struct dvb_diseqc_master_cmd *d)
  617. {
  618. struct ds3000_state *state = fe->demodulator_priv;
  619. int i;
  620. u8 data;
  621. /* Dump DiSEqC message */
  622. dprintk("%s(", __func__);
  623. for (i = 0 ; i < d->msg_len;) {
  624. dprintk("0x%02x", d->msg[i]);
  625. if (++i < d->msg_len)
  626. dprintk(", ");
  627. }
  628. /* enable DiSEqC message send pin */
  629. data = ds3000_readreg(state, 0xa2);
  630. data &= ~0xc0;
  631. ds3000_writereg(state, 0xa2, data);
  632. /* DiSEqC message */
  633. for (i = 0; i < d->msg_len; i++)
  634. ds3000_writereg(state, 0xa3 + i, d->msg[i]);
  635. data = ds3000_readreg(state, 0xa1);
  636. /* clear DiSEqC message length and status,
  637. enable DiSEqC message send */
  638. data &= ~0xf8;
  639. /* set DiSEqC mode, modulation active during 33 pulses,
  640. set DiSEqC message length */
  641. data |= ((d->msg_len - 1) << 3) | 0x07;
  642. ds3000_writereg(state, 0xa1, data);
  643. /* wait up to 150ms for DiSEqC transmission to complete */
  644. for (i = 0; i < 15; i++) {
  645. data = ds3000_readreg(state, 0xa1);
  646. if ((data & 0x40) == 0)
  647. break;
  648. msleep(10);
  649. }
  650. /* DiSEqC timeout after 150ms */
  651. if (i == 15) {
  652. data = ds3000_readreg(state, 0xa1);
  653. data &= ~0x80;
  654. data |= 0x40;
  655. ds3000_writereg(state, 0xa1, data);
  656. data = ds3000_readreg(state, 0xa2);
  657. data &= ~0xc0;
  658. data |= 0x80;
  659. ds3000_writereg(state, 0xa2, data);
  660. return 1;
  661. }
  662. data = ds3000_readreg(state, 0xa2);
  663. data &= ~0xc0;
  664. data |= 0x80;
  665. ds3000_writereg(state, 0xa2, data);
  666. return 0;
  667. }
  668. /* Send DiSEqC burst */
  669. static int ds3000_diseqc_send_burst(struct dvb_frontend *fe,
  670. fe_sec_mini_cmd_t burst)
  671. {
  672. struct ds3000_state *state = fe->demodulator_priv;
  673. int i;
  674. u8 data;
  675. dprintk("%s()\n", __func__);
  676. data = ds3000_readreg(state, 0xa2);
  677. data &= ~0xc0;
  678. ds3000_writereg(state, 0xa2, data);
  679. /* DiSEqC burst */
  680. if (burst == SEC_MINI_A)
  681. /* Unmodulated tone burst */
  682. ds3000_writereg(state, 0xa1, 0x02);
  683. else if (burst == SEC_MINI_B)
  684. /* Modulated tone burst */
  685. ds3000_writereg(state, 0xa1, 0x01);
  686. else
  687. return -EINVAL;
  688. msleep(13);
  689. for (i = 0; i < 5; i++) {
  690. data = ds3000_readreg(state, 0xa1);
  691. if ((data & 0x40) == 0)
  692. break;
  693. msleep(1);
  694. }
  695. if (i == 5) {
  696. data = ds3000_readreg(state, 0xa1);
  697. data &= ~0x80;
  698. data |= 0x40;
  699. ds3000_writereg(state, 0xa1, data);
  700. data = ds3000_readreg(state, 0xa2);
  701. data &= ~0xc0;
  702. data |= 0x80;
  703. ds3000_writereg(state, 0xa2, data);
  704. return 1;
  705. }
  706. data = ds3000_readreg(state, 0xa2);
  707. data &= ~0xc0;
  708. data |= 0x80;
  709. ds3000_writereg(state, 0xa2, data);
  710. return 0;
  711. }
  712. static void ds3000_release(struct dvb_frontend *fe)
  713. {
  714. struct ds3000_state *state = fe->demodulator_priv;
  715. if (state->config->set_lock_led)
  716. state->config->set_lock_led(fe, 0);
  717. dprintk("%s\n", __func__);
  718. kfree(state);
  719. }
  720. static struct dvb_frontend_ops ds3000_ops;
  721. struct dvb_frontend *ds3000_attach(const struct ds3000_config *config,
  722. struct i2c_adapter *i2c)
  723. {
  724. struct ds3000_state *state = NULL;
  725. int ret;
  726. dprintk("%s\n", __func__);
  727. /* allocate memory for the internal state */
  728. state = kzalloc(sizeof(struct ds3000_state), GFP_KERNEL);
  729. if (state == NULL) {
  730. printk(KERN_ERR "Unable to kmalloc\n");
  731. goto error2;
  732. }
  733. state->config = config;
  734. state->i2c = i2c;
  735. state->prevUCBS2 = 0;
  736. /* check if the demod is present */
  737. ret = ds3000_readreg(state, 0x00) & 0xfe;
  738. if (ret != 0xe0) {
  739. printk(KERN_ERR "Invalid probe, probably not a DS3000\n");
  740. goto error3;
  741. }
  742. printk(KERN_INFO "DS3000 chip version: %d.%d attached.\n",
  743. ds3000_readreg(state, 0x02),
  744. ds3000_readreg(state, 0x01));
  745. memcpy(&state->frontend.ops, &ds3000_ops,
  746. sizeof(struct dvb_frontend_ops));
  747. state->frontend.demodulator_priv = state;
  748. return &state->frontend;
  749. error3:
  750. kfree(state);
  751. error2:
  752. return NULL;
  753. }
  754. EXPORT_SYMBOL(ds3000_attach);
  755. static int ds3000_set_carrier_offset(struct dvb_frontend *fe,
  756. s32 carrier_offset_khz)
  757. {
  758. struct ds3000_state *state = fe->demodulator_priv;
  759. s32 tmp;
  760. tmp = carrier_offset_khz;
  761. tmp *= 65536;
  762. tmp = (2 * tmp + DS3000_SAMPLE_RATE) / (2 * DS3000_SAMPLE_RATE);
  763. if (tmp < 0)
  764. tmp += 65536;
  765. ds3000_writereg(state, 0x5f, tmp >> 8);
  766. ds3000_writereg(state, 0x5e, tmp & 0xff);
  767. return 0;
  768. }
  769. static int ds3000_set_frontend(struct dvb_frontend *fe)
  770. {
  771. struct ds3000_state *state = fe->demodulator_priv;
  772. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  773. int i;
  774. fe_status_t status;
  775. s32 offset_khz;
  776. u32 frequency;
  777. u16 value;
  778. dprintk("%s() ", __func__);
  779. if (state->config->set_ts_params)
  780. state->config->set_ts_params(fe, 0);
  781. /* Tune */
  782. if (fe->ops.tuner_ops.set_params)
  783. fe->ops.tuner_ops.set_params(fe);
  784. /* ds3000 global reset */
  785. ds3000_writereg(state, 0x07, 0x80);
  786. ds3000_writereg(state, 0x07, 0x00);
  787. /* ds3000 build-in uC reset */
  788. ds3000_writereg(state, 0xb2, 0x01);
  789. /* ds3000 software reset */
  790. ds3000_writereg(state, 0x00, 0x01);
  791. switch (c->delivery_system) {
  792. case SYS_DVBS:
  793. /* initialise the demod in DVB-S mode */
  794. for (i = 0; i < sizeof(ds3000_dvbs_init_tab); i += 2)
  795. ds3000_writereg(state,
  796. ds3000_dvbs_init_tab[i],
  797. ds3000_dvbs_init_tab[i + 1]);
  798. value = ds3000_readreg(state, 0xfe);
  799. value &= 0xc0;
  800. value |= 0x1b;
  801. ds3000_writereg(state, 0xfe, value);
  802. break;
  803. case SYS_DVBS2:
  804. /* initialise the demod in DVB-S2 mode */
  805. for (i = 0; i < sizeof(ds3000_dvbs2_init_tab); i += 2)
  806. ds3000_writereg(state,
  807. ds3000_dvbs2_init_tab[i],
  808. ds3000_dvbs2_init_tab[i + 1]);
  809. if (c->symbol_rate >= 30000000)
  810. ds3000_writereg(state, 0xfe, 0x54);
  811. else
  812. ds3000_writereg(state, 0xfe, 0x98);
  813. break;
  814. default:
  815. return 1;
  816. }
  817. /* enable 27MHz clock output */
  818. ds3000_writereg(state, 0x29, 0x80);
  819. /* enable ac coupling */
  820. ds3000_writereg(state, 0x25, 0x8a);
  821. /* enhance symbol rate performance */
  822. if ((c->symbol_rate / 1000) <= 5000) {
  823. value = 29777 / (c->symbol_rate / 1000) + 1;
  824. if (value % 2 != 0)
  825. value++;
  826. ds3000_writereg(state, 0xc3, 0x0d);
  827. ds3000_writereg(state, 0xc8, value);
  828. ds3000_writereg(state, 0xc4, 0x10);
  829. ds3000_writereg(state, 0xc7, 0x0e);
  830. } else if ((c->symbol_rate / 1000) <= 10000) {
  831. value = 92166 / (c->symbol_rate / 1000) + 1;
  832. if (value % 2 != 0)
  833. value++;
  834. ds3000_writereg(state, 0xc3, 0x07);
  835. ds3000_writereg(state, 0xc8, value);
  836. ds3000_writereg(state, 0xc4, 0x09);
  837. ds3000_writereg(state, 0xc7, 0x12);
  838. } else if ((c->symbol_rate / 1000) <= 20000) {
  839. value = 64516 / (c->symbol_rate / 1000) + 1;
  840. ds3000_writereg(state, 0xc3, value);
  841. ds3000_writereg(state, 0xc8, 0x0e);
  842. ds3000_writereg(state, 0xc4, 0x07);
  843. ds3000_writereg(state, 0xc7, 0x18);
  844. } else {
  845. value = 129032 / (c->symbol_rate / 1000) + 1;
  846. ds3000_writereg(state, 0xc3, value);
  847. ds3000_writereg(state, 0xc8, 0x0a);
  848. ds3000_writereg(state, 0xc4, 0x05);
  849. ds3000_writereg(state, 0xc7, 0x24);
  850. }
  851. /* normalized symbol rate rounded to the closest integer */
  852. value = (((c->symbol_rate / 1000) << 16) +
  853. (DS3000_SAMPLE_RATE / 2)) / DS3000_SAMPLE_RATE;
  854. ds3000_writereg(state, 0x61, value & 0x00ff);
  855. ds3000_writereg(state, 0x62, (value & 0xff00) >> 8);
  856. /* co-channel interference cancellation disabled */
  857. ds3000_writereg(state, 0x56, 0x00);
  858. /* equalizer disabled */
  859. ds3000_writereg(state, 0x76, 0x00);
  860. /*ds3000_writereg(state, 0x08, 0x03);
  861. ds3000_writereg(state, 0xfd, 0x22);
  862. ds3000_writereg(state, 0x08, 0x07);
  863. ds3000_writereg(state, 0xfd, 0x42);
  864. ds3000_writereg(state, 0x08, 0x07);*/
  865. if (state->config->ci_mode) {
  866. switch (c->delivery_system) {
  867. case SYS_DVBS:
  868. default:
  869. ds3000_writereg(state, 0xfd, 0x80);
  870. break;
  871. case SYS_DVBS2:
  872. ds3000_writereg(state, 0xfd, 0x01);
  873. break;
  874. }
  875. }
  876. /* ds3000 out of software reset */
  877. ds3000_writereg(state, 0x00, 0x00);
  878. /* start ds3000 build-in uC */
  879. ds3000_writereg(state, 0xb2, 0x00);
  880. if (fe->ops.tuner_ops.get_frequency) {
  881. fe->ops.tuner_ops.get_frequency(fe, &frequency);
  882. offset_khz = frequency - c->frequency;
  883. ds3000_set_carrier_offset(fe, offset_khz);
  884. }
  885. for (i = 0; i < 30 ; i++) {
  886. ds3000_read_status(fe, &status);
  887. if (status & FE_HAS_LOCK)
  888. break;
  889. msleep(10);
  890. }
  891. return 0;
  892. }
  893. static int ds3000_tune(struct dvb_frontend *fe,
  894. bool re_tune,
  895. unsigned int mode_flags,
  896. unsigned int *delay,
  897. fe_status_t *status)
  898. {
  899. if (re_tune) {
  900. int ret = ds3000_set_frontend(fe);
  901. if (ret)
  902. return ret;
  903. }
  904. *delay = HZ / 5;
  905. return ds3000_read_status(fe, status);
  906. }
  907. static enum dvbfe_algo ds3000_get_algo(struct dvb_frontend *fe)
  908. {
  909. struct ds3000_state *state = fe->demodulator_priv;
  910. if (state->config->set_lock_led)
  911. state->config->set_lock_led(fe, 0);
  912. dprintk("%s()\n", __func__);
  913. return DVBFE_ALGO_HW;
  914. }
  915. /*
  916. * Initialise or wake up device
  917. *
  918. * Power config will reset and load initial firmware if required
  919. */
  920. static int ds3000_initfe(struct dvb_frontend *fe)
  921. {
  922. struct ds3000_state *state = fe->demodulator_priv;
  923. int ret;
  924. dprintk("%s()\n", __func__);
  925. /* hard reset */
  926. ds3000_writereg(state, 0x08, 0x01 | ds3000_readreg(state, 0x08));
  927. msleep(1);
  928. /* Load the firmware if required */
  929. ret = ds3000_firmware_ondemand(fe);
  930. if (ret != 0) {
  931. printk(KERN_ERR "%s: Unable initialize firmware\n", __func__);
  932. return ret;
  933. }
  934. return 0;
  935. }
  936. static struct dvb_frontend_ops ds3000_ops = {
  937. .delsys = { SYS_DVBS, SYS_DVBS2 },
  938. .info = {
  939. .name = "Montage Technology DS3000",
  940. .frequency_min = 950000,
  941. .frequency_max = 2150000,
  942. .frequency_stepsize = 1011, /* kHz for QPSK frontends */
  943. .frequency_tolerance = 5000,
  944. .symbol_rate_min = 1000000,
  945. .symbol_rate_max = 45000000,
  946. .caps = FE_CAN_INVERSION_AUTO |
  947. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  948. FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
  949. FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  950. FE_CAN_2G_MODULATION |
  951. FE_CAN_QPSK | FE_CAN_RECOVER
  952. },
  953. .release = ds3000_release,
  954. .init = ds3000_initfe,
  955. .i2c_gate_ctrl = ds3000_i2c_gate_ctrl,
  956. .read_status = ds3000_read_status,
  957. .read_ber = ds3000_read_ber,
  958. .read_snr = ds3000_read_snr,
  959. .read_ucblocks = ds3000_read_ucblocks,
  960. .set_voltage = ds3000_set_voltage,
  961. .set_tone = ds3000_set_tone,
  962. .diseqc_send_master_cmd = ds3000_send_diseqc_msg,
  963. .diseqc_send_burst = ds3000_diseqc_send_burst,
  964. .get_frontend_algo = ds3000_get_algo,
  965. .set_frontend = ds3000_set_frontend,
  966. .tune = ds3000_tune,
  967. };
  968. module_param(debug, int, 0644);
  969. MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
  970. MODULE_DESCRIPTION("DVB Frontend module for Montage Technology "
  971. "DS3000 hardware");
  972. MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");
  973. MODULE_LICENSE("GPL");
  974. MODULE_FIRMWARE(DS3000_DEFAULT_FIRMWARE);