ds3000.c 25 KB

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