ds3000.c 26 KB

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