ds3000.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. /*
  2. Montage Technology DS3000/TS2020 - DVBS/S2 Demodulator/Tuner driver
  3. Copyright (C) 2009 Konstantin Dimitrov <kosio.dimitrov@gmail.com>
  4. Copyright (C) 2009 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. #define DS3000_XTAL_FREQ 27000 /* 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. u8 skip_fw_load;
  224. /* previous uncorrected block counter for DVB-S2 */
  225. u16 prevUCBS2;
  226. };
  227. static int ds3000_writereg(struct ds3000_state *state, int reg, int data)
  228. {
  229. u8 buf[] = { reg, data };
  230. struct i2c_msg msg = { .addr = state->config->demod_address,
  231. .flags = 0, .buf = buf, .len = 2 };
  232. int err;
  233. dprintk("%s: write reg 0x%02x, value 0x%02x\n", __func__, reg, data);
  234. err = i2c_transfer(state->i2c, &msg, 1);
  235. if (err != 1) {
  236. printk(KERN_ERR "%s: writereg error(err == %i, reg == 0x%02x,"
  237. " value == 0x%02x)\n", __func__, err, reg, data);
  238. return -EREMOTEIO;
  239. }
  240. return 0;
  241. }
  242. static int ds3000_tuner_writereg(struct ds3000_state *state, int reg, int data)
  243. {
  244. u8 buf[] = { reg, data };
  245. struct i2c_msg msg = { .addr = 0x60,
  246. .flags = 0, .buf = buf, .len = 2 };
  247. int err;
  248. dprintk("%s: write reg 0x%02x, value 0x%02x\n", __func__, reg, data);
  249. ds3000_writereg(state, 0x03, 0x11);
  250. err = i2c_transfer(state->i2c, &msg, 1);
  251. if (err != 1) {
  252. printk("%s: writereg error(err == %i, reg == 0x%02x,"
  253. " value == 0x%02x)\n", __func__, err, reg, data);
  254. return -EREMOTEIO;
  255. }
  256. return 0;
  257. }
  258. /* I2C write for 8k firmware load */
  259. static int ds3000_writeFW(struct ds3000_state *state, int reg,
  260. const u8 *data, u16 len)
  261. {
  262. int i, ret = -EREMOTEIO;
  263. struct i2c_msg msg;
  264. u8 *buf;
  265. buf = kmalloc(33, GFP_KERNEL);
  266. if (buf == NULL) {
  267. printk(KERN_ERR "Unable to kmalloc\n");
  268. ret = -ENOMEM;
  269. goto error;
  270. }
  271. *(buf) = reg;
  272. msg.addr = state->config->demod_address;
  273. msg.flags = 0;
  274. msg.buf = buf;
  275. msg.len = 33;
  276. for (i = 0; i < len; i += 32) {
  277. memcpy(buf + 1, data + i, 32);
  278. dprintk("%s: write reg 0x%02x, len = %d\n", __func__, reg, len);
  279. ret = i2c_transfer(state->i2c, &msg, 1);
  280. if (ret != 1) {
  281. printk(KERN_ERR "%s: write error(err == %i, "
  282. "reg == 0x%02x\n", __func__, ret, reg);
  283. ret = -EREMOTEIO;
  284. }
  285. }
  286. error:
  287. kfree(buf);
  288. return ret;
  289. }
  290. static int ds3000_readreg(struct ds3000_state *state, u8 reg)
  291. {
  292. int ret;
  293. u8 b0[] = { reg };
  294. u8 b1[] = { 0 };
  295. struct i2c_msg msg[] = {
  296. {
  297. .addr = state->config->demod_address,
  298. .flags = 0,
  299. .buf = b0,
  300. .len = 1
  301. }, {
  302. .addr = state->config->demod_address,
  303. .flags = I2C_M_RD,
  304. .buf = b1,
  305. .len = 1
  306. }
  307. };
  308. ret = i2c_transfer(state->i2c, msg, 2);
  309. if (ret != 2) {
  310. printk(KERN_ERR "%s: reg=0x%x(error=%d)\n", __func__, reg, ret);
  311. return ret;
  312. }
  313. dprintk("%s: read reg 0x%02x, value 0x%02x\n", __func__, reg, b1[0]);
  314. return b1[0];
  315. }
  316. static int ds3000_tuner_readreg(struct ds3000_state *state, u8 reg)
  317. {
  318. int ret;
  319. u8 b0[] = { reg };
  320. u8 b1[] = { 0 };
  321. struct i2c_msg msg[] = {
  322. {
  323. .addr = 0x60,
  324. .flags = 0,
  325. .buf = b0,
  326. .len = 1
  327. }, {
  328. .addr = 0x60,
  329. .flags = I2C_M_RD,
  330. .buf = b1,
  331. .len = 1
  332. }
  333. };
  334. ds3000_writereg(state, 0x03, 0x12);
  335. ret = i2c_transfer(state->i2c, msg, 2);
  336. if (ret != 2) {
  337. printk(KERN_ERR "%s: reg=0x%x(error=%d)\n", __func__, reg, ret);
  338. return ret;
  339. }
  340. dprintk("%s: read reg 0x%02x, value 0x%02x\n", __func__, reg, b1[0]);
  341. return b1[0];
  342. }
  343. static int ds3000_load_firmware(struct dvb_frontend *fe,
  344. const struct firmware *fw);
  345. static int ds3000_firmware_ondemand(struct dvb_frontend *fe)
  346. {
  347. struct ds3000_state *state = fe->demodulator_priv;
  348. const struct firmware *fw;
  349. int ret = 0;
  350. dprintk("%s()\n", __func__);
  351. if (ds3000_readreg(state, 0xb2) <= 0)
  352. return ret;
  353. if (state->skip_fw_load)
  354. return 0;
  355. /* Load firmware */
  356. /* request the firmware, this will block until someone uploads it */
  357. printk(KERN_INFO "%s: Waiting for firmware upload (%s)...\n", __func__,
  358. DS3000_DEFAULT_FIRMWARE);
  359. ret = request_firmware(&fw, DS3000_DEFAULT_FIRMWARE,
  360. state->i2c->dev.parent);
  361. printk(KERN_INFO "%s: Waiting for firmware upload(2)...\n", __func__);
  362. if (ret) {
  363. printk(KERN_ERR "%s: No firmware uploaded (timeout or file not "
  364. "found?)\n", __func__);
  365. return ret;
  366. }
  367. /* Make sure we don't recurse back through here during loading */
  368. state->skip_fw_load = 1;
  369. ret = ds3000_load_firmware(fe, fw);
  370. if (ret)
  371. printk("%s: Writing firmware to device failed\n", __func__);
  372. release_firmware(fw);
  373. dprintk("%s: Firmware upload %s\n", __func__,
  374. ret == 0 ? "complete" : "failed");
  375. /* Ensure firmware is always loaded if required */
  376. state->skip_fw_load = 0;
  377. return ret;
  378. }
  379. static int ds3000_load_firmware(struct dvb_frontend *fe,
  380. const struct firmware *fw)
  381. {
  382. struct ds3000_state *state = fe->demodulator_priv;
  383. dprintk("%s\n", __func__);
  384. dprintk("Firmware is %zu bytes (%02x %02x .. %02x %02x)\n",
  385. fw->size,
  386. fw->data[0],
  387. fw->data[1],
  388. fw->data[fw->size - 2],
  389. fw->data[fw->size - 1]);
  390. /* Begin the firmware load process */
  391. ds3000_writereg(state, 0xb2, 0x01);
  392. /* write the entire firmware */
  393. ds3000_writeFW(state, 0xb0, fw->data, fw->size);
  394. ds3000_writereg(state, 0xb2, 0x00);
  395. return 0;
  396. }
  397. static int ds3000_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
  398. {
  399. struct ds3000_state *state = fe->demodulator_priv;
  400. u8 data;
  401. dprintk("%s(%d)\n", __func__, voltage);
  402. data = ds3000_readreg(state, 0xa2);
  403. data |= 0x03; /* bit0 V/H, bit1 off/on */
  404. switch (voltage) {
  405. case SEC_VOLTAGE_18:
  406. data &= ~0x03;
  407. break;
  408. case SEC_VOLTAGE_13:
  409. data &= ~0x03;
  410. data |= 0x01;
  411. break;
  412. case SEC_VOLTAGE_OFF:
  413. break;
  414. }
  415. ds3000_writereg(state, 0xa2, data);
  416. return 0;
  417. }
  418. static int ds3000_read_status(struct dvb_frontend *fe, fe_status_t* status)
  419. {
  420. struct ds3000_state *state = fe->demodulator_priv;
  421. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  422. int lock;
  423. *status = 0;
  424. switch (c->delivery_system) {
  425. case SYS_DVBS:
  426. lock = ds3000_readreg(state, 0xd1);
  427. if ((lock & 0x07) == 0x07)
  428. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  429. FE_HAS_VITERBI | FE_HAS_SYNC |
  430. FE_HAS_LOCK;
  431. break;
  432. case SYS_DVBS2:
  433. lock = ds3000_readreg(state, 0x0d);
  434. if ((lock & 0x8f) == 0x8f)
  435. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  436. FE_HAS_VITERBI | FE_HAS_SYNC |
  437. FE_HAS_LOCK;
  438. break;
  439. default:
  440. return 1;
  441. }
  442. dprintk("%s: status = 0x%02x\n", __func__, lock);
  443. return 0;
  444. }
  445. /* read DS3000 BER value */
  446. static int ds3000_read_ber(struct dvb_frontend *fe, u32* ber)
  447. {
  448. struct ds3000_state *state = fe->demodulator_priv;
  449. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  450. u8 data;
  451. u32 ber_reading, lpdc_frames;
  452. dprintk("%s()\n", __func__);
  453. switch (c->delivery_system) {
  454. case SYS_DVBS:
  455. /* set the number of bytes checked during
  456. BER estimation */
  457. ds3000_writereg(state, 0xf9, 0x04);
  458. /* read BER estimation status */
  459. data = ds3000_readreg(state, 0xf8);
  460. /* check if BER estimation is ready */
  461. if ((data & 0x10) == 0) {
  462. /* this is the number of error bits,
  463. to calculate the bit error rate
  464. divide to 8388608 */
  465. *ber = (ds3000_readreg(state, 0xf7) << 8) |
  466. ds3000_readreg(state, 0xf6);
  467. /* start counting error bits */
  468. /* need to be set twice
  469. otherwise it fails sometimes */
  470. data |= 0x10;
  471. ds3000_writereg(state, 0xf8, data);
  472. ds3000_writereg(state, 0xf8, data);
  473. } else
  474. /* used to indicate that BER estimation
  475. is not ready, i.e. BER is unknown */
  476. *ber = 0xffffffff;
  477. break;
  478. case SYS_DVBS2:
  479. /* read the number of LPDC decoded frames */
  480. lpdc_frames = (ds3000_readreg(state, 0xd7) << 16) |
  481. (ds3000_readreg(state, 0xd6) << 8) |
  482. ds3000_readreg(state, 0xd5);
  483. /* read the number of packets with bad CRC */
  484. ber_reading = (ds3000_readreg(state, 0xf8) << 8) |
  485. ds3000_readreg(state, 0xf7);
  486. if (lpdc_frames > 750) {
  487. /* clear LPDC frame counters */
  488. ds3000_writereg(state, 0xd1, 0x01);
  489. /* clear bad packets counter */
  490. ds3000_writereg(state, 0xf9, 0x01);
  491. /* enable bad packets counter */
  492. ds3000_writereg(state, 0xf9, 0x00);
  493. /* enable LPDC frame counters */
  494. ds3000_writereg(state, 0xd1, 0x00);
  495. *ber = ber_reading;
  496. } else
  497. /* used to indicate that BER estimation is not ready,
  498. i.e. BER is unknown */
  499. *ber = 0xffffffff;
  500. break;
  501. default:
  502. return 1;
  503. }
  504. return 0;
  505. }
  506. /* read TS2020 signal strength */
  507. static int ds3000_read_signal_strength(struct dvb_frontend *fe,
  508. u16 *signal_strength)
  509. {
  510. struct ds3000_state *state = fe->demodulator_priv;
  511. u16 sig_reading, sig_strength;
  512. u8 rfgain, bbgain;
  513. dprintk("%s()\n", __func__);
  514. rfgain = ds3000_tuner_readreg(state, 0x3d) & 0x1f;
  515. bbgain = ds3000_tuner_readreg(state, 0x21) & 0x1f;
  516. if (rfgain > 15)
  517. rfgain = 15;
  518. if (bbgain > 13)
  519. bbgain = 13;
  520. sig_reading = rfgain * 2 + bbgain * 3;
  521. sig_strength = 40 + (64 - sig_reading) * 50 / 64 ;
  522. /* cook the value to be suitable for szap-s2 human readable output */
  523. *signal_strength = sig_strength * 1000;
  524. dprintk("%s: raw / cooked = 0x%04x / 0x%04x\n", __func__,
  525. sig_reading, *signal_strength);
  526. return 0;
  527. }
  528. /* calculate DS3000 snr value in dB */
  529. static int ds3000_read_snr(struct dvb_frontend *fe, u16 *snr)
  530. {
  531. struct ds3000_state *state = fe->demodulator_priv;
  532. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  533. u8 snr_reading, snr_value;
  534. u32 dvbs2_signal_reading, dvbs2_noise_reading, tmp;
  535. static const u16 dvbs_snr_tab[] = { /* 20 x Table (rounded up) */
  536. 0x0000, 0x1b13, 0x2aea, 0x3627, 0x3ede, 0x45fe, 0x4c03,
  537. 0x513a, 0x55d4, 0x59f2, 0x5dab, 0x6111, 0x6431, 0x6717,
  538. 0x69c9, 0x6c4e, 0x6eac, 0x70e8, 0x7304, 0x7505
  539. };
  540. static const u16 dvbs2_snr_tab[] = { /* 80 x Table (rounded up) */
  541. 0x0000, 0x0bc2, 0x12a3, 0x1785, 0x1b4e, 0x1e65, 0x2103,
  542. 0x2347, 0x2546, 0x2710, 0x28ae, 0x2a28, 0x2b83, 0x2cc5,
  543. 0x2df1, 0x2f09, 0x3010, 0x3109, 0x31f4, 0x32d2, 0x33a6,
  544. 0x3470, 0x3531, 0x35ea, 0x369b, 0x3746, 0x37ea, 0x3888,
  545. 0x3920, 0x39b3, 0x3a42, 0x3acc, 0x3b51, 0x3bd3, 0x3c51,
  546. 0x3ccb, 0x3d42, 0x3db6, 0x3e27, 0x3e95, 0x3f00, 0x3f68,
  547. 0x3fcf, 0x4033, 0x4094, 0x40f4, 0x4151, 0x41ac, 0x4206,
  548. 0x425e, 0x42b4, 0x4308, 0x435b, 0x43ac, 0x43fc, 0x444a,
  549. 0x4497, 0x44e2, 0x452d, 0x4576, 0x45bd, 0x4604, 0x4649,
  550. 0x468e, 0x46d1, 0x4713, 0x4755, 0x4795, 0x47d4, 0x4813,
  551. 0x4851, 0x488d, 0x48c9, 0x4904, 0x493f, 0x4978, 0x49b1,
  552. 0x49e9, 0x4a20, 0x4a57
  553. };
  554. dprintk("%s()\n", __func__);
  555. switch (c->delivery_system) {
  556. case SYS_DVBS:
  557. snr_reading = ds3000_readreg(state, 0xff);
  558. snr_reading /= 8;
  559. if (snr_reading == 0)
  560. *snr = 0x0000;
  561. else {
  562. if (snr_reading > 20)
  563. snr_reading = 20;
  564. snr_value = dvbs_snr_tab[snr_reading - 1] * 10 / 23026;
  565. /* cook the value to be suitable for szap-s2
  566. human readable output */
  567. *snr = snr_value * 8 * 655;
  568. }
  569. dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
  570. snr_reading, *snr);
  571. break;
  572. case SYS_DVBS2:
  573. dvbs2_noise_reading = (ds3000_readreg(state, 0x8c) & 0x3f) +
  574. (ds3000_readreg(state, 0x8d) << 4);
  575. dvbs2_signal_reading = ds3000_readreg(state, 0x8e);
  576. tmp = dvbs2_signal_reading * dvbs2_signal_reading >> 1;
  577. if (tmp == 0) {
  578. *snr = 0x0000;
  579. return 0;
  580. }
  581. if (dvbs2_noise_reading == 0) {
  582. snr_value = 0x0013;
  583. /* cook the value to be suitable for szap-s2
  584. human readable output */
  585. *snr = 0xffff;
  586. return 0;
  587. }
  588. if (tmp > dvbs2_noise_reading) {
  589. snr_reading = tmp / dvbs2_noise_reading;
  590. if (snr_reading > 80)
  591. snr_reading = 80;
  592. snr_value = dvbs2_snr_tab[snr_reading - 1] / 1000;
  593. /* cook the value to be suitable for szap-s2
  594. human readable output */
  595. *snr = snr_value * 5 * 655;
  596. } else {
  597. snr_reading = dvbs2_noise_reading / tmp;
  598. if (snr_reading > 80)
  599. snr_reading = 80;
  600. *snr = -(dvbs2_snr_tab[snr_reading] / 1000);
  601. }
  602. dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
  603. snr_reading, *snr);
  604. break;
  605. default:
  606. return 1;
  607. }
  608. return 0;
  609. }
  610. /* read DS3000 uncorrected blocks */
  611. static int ds3000_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  612. {
  613. struct ds3000_state *state = fe->demodulator_priv;
  614. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  615. u8 data;
  616. u16 _ucblocks;
  617. dprintk("%s()\n", __func__);
  618. switch (c->delivery_system) {
  619. case SYS_DVBS:
  620. *ucblocks = (ds3000_readreg(state, 0xf5) << 8) |
  621. ds3000_readreg(state, 0xf4);
  622. data = ds3000_readreg(state, 0xf8);
  623. /* clear packet counters */
  624. data &= ~0x20;
  625. ds3000_writereg(state, 0xf8, data);
  626. /* enable packet counters */
  627. data |= 0x20;
  628. ds3000_writereg(state, 0xf8, data);
  629. break;
  630. case SYS_DVBS2:
  631. _ucblocks = (ds3000_readreg(state, 0xe2) << 8) |
  632. ds3000_readreg(state, 0xe1);
  633. if (_ucblocks > state->prevUCBS2)
  634. *ucblocks = _ucblocks - state->prevUCBS2;
  635. else
  636. *ucblocks = state->prevUCBS2 - _ucblocks;
  637. state->prevUCBS2 = _ucblocks;
  638. break;
  639. default:
  640. return 1;
  641. }
  642. return 0;
  643. }
  644. static int ds3000_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
  645. {
  646. struct ds3000_state *state = fe->demodulator_priv;
  647. u8 data;
  648. dprintk("%s(%d)\n", __func__, tone);
  649. if ((tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF)) {
  650. printk(KERN_ERR "%s: Invalid, tone=%d\n", __func__, tone);
  651. return -EINVAL;
  652. }
  653. data = ds3000_readreg(state, 0xa2);
  654. data &= ~0xc0;
  655. ds3000_writereg(state, 0xa2, data);
  656. switch (tone) {
  657. case SEC_TONE_ON:
  658. dprintk("%s: setting tone on\n", __func__);
  659. data = ds3000_readreg(state, 0xa1);
  660. data &= ~0x43;
  661. data |= 0x04;
  662. ds3000_writereg(state, 0xa1, data);
  663. break;
  664. case SEC_TONE_OFF:
  665. dprintk("%s: setting tone off\n", __func__);
  666. data = ds3000_readreg(state, 0xa2);
  667. data |= 0x80;
  668. ds3000_writereg(state, 0xa2, data);
  669. break;
  670. }
  671. return 0;
  672. }
  673. static int ds3000_send_diseqc_msg(struct dvb_frontend *fe,
  674. struct dvb_diseqc_master_cmd *d)
  675. {
  676. struct ds3000_state *state = fe->demodulator_priv;
  677. int i;
  678. u8 data;
  679. /* Dump DiSEqC message */
  680. dprintk("%s(", __func__);
  681. for (i = 0 ; i < d->msg_len;) {
  682. dprintk("0x%02x", d->msg[i]);
  683. if (++i < d->msg_len)
  684. dprintk(", ");
  685. }
  686. /* enable DiSEqC message send pin */
  687. data = ds3000_readreg(state, 0xa2);
  688. data &= ~0xc0;
  689. ds3000_writereg(state, 0xa2, data);
  690. /* DiSEqC message */
  691. for (i = 0; i < d->msg_len; i++)
  692. ds3000_writereg(state, 0xa3 + i, d->msg[i]);
  693. data = ds3000_readreg(state, 0xa1);
  694. /* clear DiSEqC message length and status,
  695. enable DiSEqC message send */
  696. data &= ~0xf8;
  697. /* set DiSEqC mode, modulation active during 33 pulses,
  698. set DiSEqC message length */
  699. data |= ((d->msg_len - 1) << 3) | 0x07;
  700. ds3000_writereg(state, 0xa1, data);
  701. /* wait up to 150ms for DiSEqC transmission to complete */
  702. for (i = 0; i < 15; i++) {
  703. data = ds3000_readreg(state, 0xa1);
  704. if ((data & 0x40) == 0)
  705. break;
  706. msleep(10);
  707. }
  708. /* DiSEqC timeout after 150ms */
  709. if (i == 15) {
  710. data = ds3000_readreg(state, 0xa1);
  711. data &= ~0x80;
  712. data |= 0x40;
  713. ds3000_writereg(state, 0xa1, data);
  714. data = ds3000_readreg(state, 0xa2);
  715. data &= ~0xc0;
  716. data |= 0x80;
  717. ds3000_writereg(state, 0xa2, data);
  718. return 1;
  719. }
  720. data = ds3000_readreg(state, 0xa2);
  721. data &= ~0xc0;
  722. data |= 0x80;
  723. ds3000_writereg(state, 0xa2, data);
  724. return 0;
  725. }
  726. /* Send DiSEqC burst */
  727. static int ds3000_diseqc_send_burst(struct dvb_frontend *fe,
  728. fe_sec_mini_cmd_t burst)
  729. {
  730. struct ds3000_state *state = fe->demodulator_priv;
  731. int i;
  732. u8 data;
  733. dprintk("%s()\n", __func__);
  734. data = ds3000_readreg(state, 0xa2);
  735. data &= ~0xc0;
  736. ds3000_writereg(state, 0xa2, data);
  737. /* DiSEqC burst */
  738. if (burst == SEC_MINI_A)
  739. /* Unmodulated tone burst */
  740. ds3000_writereg(state, 0xa1, 0x02);
  741. else if (burst == SEC_MINI_B)
  742. /* Modulated tone burst */
  743. ds3000_writereg(state, 0xa1, 0x01);
  744. else
  745. return -EINVAL;
  746. msleep(13);
  747. for (i = 0; i < 5; i++) {
  748. data = ds3000_readreg(state, 0xa1);
  749. if ((data & 0x40) == 0)
  750. break;
  751. msleep(1);
  752. }
  753. if (i == 5) {
  754. data = ds3000_readreg(state, 0xa1);
  755. data &= ~0x80;
  756. data |= 0x40;
  757. ds3000_writereg(state, 0xa1, data);
  758. data = ds3000_readreg(state, 0xa2);
  759. data &= ~0xc0;
  760. data |= 0x80;
  761. ds3000_writereg(state, 0xa2, data);
  762. return 1;
  763. }
  764. data = ds3000_readreg(state, 0xa2);
  765. data &= ~0xc0;
  766. data |= 0x80;
  767. ds3000_writereg(state, 0xa2, data);
  768. return 0;
  769. }
  770. static void ds3000_release(struct dvb_frontend *fe)
  771. {
  772. struct ds3000_state *state = fe->demodulator_priv;
  773. dprintk("%s\n", __func__);
  774. kfree(state);
  775. }
  776. static struct dvb_frontend_ops ds3000_ops;
  777. struct dvb_frontend *ds3000_attach(const struct ds3000_config *config,
  778. struct i2c_adapter *i2c)
  779. {
  780. struct ds3000_state *state = NULL;
  781. int ret;
  782. dprintk("%s\n", __func__);
  783. /* allocate memory for the internal state */
  784. state = kzalloc(sizeof(struct ds3000_state), GFP_KERNEL);
  785. if (state == NULL) {
  786. printk(KERN_ERR "Unable to kmalloc\n");
  787. goto error2;
  788. }
  789. state->config = config;
  790. state->i2c = i2c;
  791. state->prevUCBS2 = 0;
  792. /* check if the demod is present */
  793. ret = ds3000_readreg(state, 0x00) & 0xfe;
  794. if (ret != 0xe0) {
  795. printk(KERN_ERR "Invalid probe, probably not a DS3000\n");
  796. goto error3;
  797. }
  798. printk(KERN_INFO "DS3000 chip version: %d.%d attached.\n",
  799. ds3000_readreg(state, 0x02),
  800. ds3000_readreg(state, 0x01));
  801. memcpy(&state->frontend.ops, &ds3000_ops,
  802. sizeof(struct dvb_frontend_ops));
  803. state->frontend.demodulator_priv = state;
  804. return &state->frontend;
  805. error3:
  806. kfree(state);
  807. error2:
  808. return NULL;
  809. }
  810. EXPORT_SYMBOL(ds3000_attach);
  811. static int ds3000_set_property(struct dvb_frontend *fe,
  812. struct dtv_property *tvp)
  813. {
  814. dprintk("%s(..)\n", __func__);
  815. return 0;
  816. }
  817. static int ds3000_get_property(struct dvb_frontend *fe,
  818. struct dtv_property *tvp)
  819. {
  820. dprintk("%s(..)\n", __func__);
  821. return 0;
  822. }
  823. static int ds3000_set_carrier_offset(struct dvb_frontend *fe,
  824. s32 carrier_offset_khz)
  825. {
  826. struct ds3000_state *state = fe->demodulator_priv;
  827. s32 tmp;
  828. tmp = carrier_offset_khz;
  829. tmp *= 65536;
  830. tmp = (2 * tmp + DS3000_SAMPLE_RATE) / (2 * DS3000_SAMPLE_RATE);
  831. if (tmp < 0)
  832. tmp += 65536;
  833. ds3000_writereg(state, 0x5f, tmp >> 8);
  834. ds3000_writereg(state, 0x5e, tmp & 0xff);
  835. return 0;
  836. }
  837. static int ds3000_set_frontend(struct dvb_frontend *fe,
  838. struct dvb_frontend_parameters *p)
  839. {
  840. struct ds3000_state *state = fe->demodulator_priv;
  841. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  842. int i;
  843. fe_status_t status;
  844. u8 mlpf, mlpf_new, mlpf_max, mlpf_min, nlpf, div4;
  845. s32 offset_khz;
  846. u16 value, ndiv;
  847. u32 f3db;
  848. dprintk("%s() ", __func__);
  849. if (state->config->set_ts_params)
  850. state->config->set_ts_params(fe, 0);
  851. /* Tune */
  852. /* unknown */
  853. ds3000_tuner_writereg(state, 0x07, 0x02);
  854. ds3000_tuner_writereg(state, 0x10, 0x00);
  855. ds3000_tuner_writereg(state, 0x60, 0x79);
  856. ds3000_tuner_writereg(state, 0x08, 0x01);
  857. ds3000_tuner_writereg(state, 0x00, 0x01);
  858. div4 = 0;
  859. /* calculate and set freq divider */
  860. if (p->frequency < 1146000) {
  861. ds3000_tuner_writereg(state, 0x10, 0x11);
  862. div4 = 1;
  863. ndiv = ((p->frequency * (6 + 8) * 4) +
  864. (DS3000_XTAL_FREQ / 2)) /
  865. DS3000_XTAL_FREQ - 1024;
  866. } else {
  867. ds3000_tuner_writereg(state, 0x10, 0x01);
  868. ndiv = ((p->frequency * (6 + 8) * 2) +
  869. (DS3000_XTAL_FREQ / 2)) /
  870. DS3000_XTAL_FREQ - 1024;
  871. }
  872. ds3000_tuner_writereg(state, 0x01, (ndiv & 0x0f00) >> 8);
  873. ds3000_tuner_writereg(state, 0x02, ndiv & 0x00ff);
  874. /* set pll */
  875. ds3000_tuner_writereg(state, 0x03, 0x06);
  876. ds3000_tuner_writereg(state, 0x51, 0x0f);
  877. ds3000_tuner_writereg(state, 0x51, 0x1f);
  878. ds3000_tuner_writereg(state, 0x50, 0x10);
  879. ds3000_tuner_writereg(state, 0x50, 0x00);
  880. msleep(5);
  881. /* unknown */
  882. ds3000_tuner_writereg(state, 0x51, 0x17);
  883. ds3000_tuner_writereg(state, 0x51, 0x1f);
  884. ds3000_tuner_writereg(state, 0x50, 0x08);
  885. ds3000_tuner_writereg(state, 0x50, 0x00);
  886. msleep(5);
  887. value = ds3000_tuner_readreg(state, 0x3d);
  888. value &= 0x0f;
  889. if ((value > 4) && (value < 15)) {
  890. value -= 3;
  891. if (value < 4)
  892. value = 4;
  893. value = ((value << 3) | 0x01) & 0x79;
  894. }
  895. ds3000_tuner_writereg(state, 0x60, value);
  896. ds3000_tuner_writereg(state, 0x51, 0x17);
  897. ds3000_tuner_writereg(state, 0x51, 0x1f);
  898. ds3000_tuner_writereg(state, 0x50, 0x08);
  899. ds3000_tuner_writereg(state, 0x50, 0x00);
  900. /* set low-pass filter period */
  901. ds3000_tuner_writereg(state, 0x04, 0x2e);
  902. ds3000_tuner_writereg(state, 0x51, 0x1b);
  903. ds3000_tuner_writereg(state, 0x51, 0x1f);
  904. ds3000_tuner_writereg(state, 0x50, 0x04);
  905. ds3000_tuner_writereg(state, 0x50, 0x00);
  906. msleep(5);
  907. f3db = ((c->symbol_rate / 1000) << 2) / 5 + 2000;
  908. if ((c->symbol_rate / 1000) < 5000)
  909. f3db += 3000;
  910. if (f3db < 7000)
  911. f3db = 7000;
  912. if (f3db > 40000)
  913. f3db = 40000;
  914. /* set low-pass filter baseband */
  915. value = ds3000_tuner_readreg(state, 0x26);
  916. mlpf = 0x2e * 207 / ((value << 1) + 151);
  917. mlpf_max = mlpf * 135 / 100;
  918. mlpf_min = mlpf * 78 / 100;
  919. if (mlpf_max > 63)
  920. mlpf_max = 63;
  921. /* rounded to the closest integer */
  922. nlpf = ((mlpf * f3db * 1000) + (2766 * DS3000_XTAL_FREQ / 2))
  923. / (2766 * DS3000_XTAL_FREQ);
  924. if (nlpf > 23)
  925. nlpf = 23;
  926. if (nlpf < 1)
  927. nlpf = 1;
  928. /* rounded to the closest integer */
  929. mlpf_new = ((DS3000_XTAL_FREQ * nlpf * 2766) +
  930. (1000 * f3db / 2)) / (1000 * f3db);
  931. if (mlpf_new < mlpf_min) {
  932. nlpf++;
  933. mlpf_new = ((DS3000_XTAL_FREQ * nlpf * 2766) +
  934. (1000 * f3db / 2)) / (1000 * f3db);
  935. }
  936. if (mlpf_new > mlpf_max)
  937. mlpf_new = mlpf_max;
  938. ds3000_tuner_writereg(state, 0x04, mlpf_new);
  939. ds3000_tuner_writereg(state, 0x06, nlpf);
  940. ds3000_tuner_writereg(state, 0x51, 0x1b);
  941. ds3000_tuner_writereg(state, 0x51, 0x1f);
  942. ds3000_tuner_writereg(state, 0x50, 0x04);
  943. ds3000_tuner_writereg(state, 0x50, 0x00);
  944. msleep(5);
  945. /* unknown */
  946. ds3000_tuner_writereg(state, 0x51, 0x1e);
  947. ds3000_tuner_writereg(state, 0x51, 0x1f);
  948. ds3000_tuner_writereg(state, 0x50, 0x01);
  949. ds3000_tuner_writereg(state, 0x50, 0x00);
  950. msleep(60);
  951. offset_khz = (ndiv - ndiv % 2 + 1024) * DS3000_XTAL_FREQ
  952. / (6 + 8) / (div4 + 1) / 2 - p->frequency;
  953. /* ds3000 global reset */
  954. ds3000_writereg(state, 0x07, 0x80);
  955. ds3000_writereg(state, 0x07, 0x00);
  956. /* ds3000 build-in uC reset */
  957. ds3000_writereg(state, 0xb2, 0x01);
  958. /* ds3000 software reset */
  959. ds3000_writereg(state, 0x00, 0x01);
  960. switch (c->delivery_system) {
  961. case SYS_DVBS:
  962. /* initialise the demod in DVB-S mode */
  963. for (i = 0; i < sizeof(ds3000_dvbs_init_tab); i += 2)
  964. ds3000_writereg(state,
  965. ds3000_dvbs_init_tab[i],
  966. ds3000_dvbs_init_tab[i + 1]);
  967. value = ds3000_readreg(state, 0xfe);
  968. value &= 0xc0;
  969. value |= 0x1b;
  970. ds3000_writereg(state, 0xfe, value);
  971. break;
  972. case SYS_DVBS2:
  973. /* initialise the demod in DVB-S2 mode */
  974. for (i = 0; i < sizeof(ds3000_dvbs2_init_tab); i += 2)
  975. ds3000_writereg(state,
  976. ds3000_dvbs2_init_tab[i],
  977. ds3000_dvbs2_init_tab[i + 1]);
  978. ds3000_writereg(state, 0xfe, 0x98);
  979. break;
  980. default:
  981. return 1;
  982. }
  983. /* enable 27MHz clock output */
  984. ds3000_writereg(state, 0x29, 0x80);
  985. /* enable ac coupling */
  986. ds3000_writereg(state, 0x25, 0x8a);
  987. /* enhance symbol rate performance */
  988. if ((c->symbol_rate / 1000) <= 5000) {
  989. value = 29777 / (c->symbol_rate / 1000) + 1;
  990. if (value % 2 != 0)
  991. value++;
  992. ds3000_writereg(state, 0xc3, 0x0d);
  993. ds3000_writereg(state, 0xc8, value);
  994. ds3000_writereg(state, 0xc4, 0x10);
  995. ds3000_writereg(state, 0xc7, 0x0e);
  996. } else if ((c->symbol_rate / 1000) <= 10000) {
  997. value = 92166 / (c->symbol_rate / 1000) + 1;
  998. if (value % 2 != 0)
  999. value++;
  1000. ds3000_writereg(state, 0xc3, 0x07);
  1001. ds3000_writereg(state, 0xc8, value);
  1002. ds3000_writereg(state, 0xc4, 0x09);
  1003. ds3000_writereg(state, 0xc7, 0x12);
  1004. } else if ((c->symbol_rate / 1000) <= 20000) {
  1005. value = 64516 / (c->symbol_rate / 1000) + 1;
  1006. ds3000_writereg(state, 0xc3, value);
  1007. ds3000_writereg(state, 0xc8, 0x0e);
  1008. ds3000_writereg(state, 0xc4, 0x07);
  1009. ds3000_writereg(state, 0xc7, 0x18);
  1010. } else {
  1011. value = 129032 / (c->symbol_rate / 1000) + 1;
  1012. ds3000_writereg(state, 0xc3, value);
  1013. ds3000_writereg(state, 0xc8, 0x0a);
  1014. ds3000_writereg(state, 0xc4, 0x05);
  1015. ds3000_writereg(state, 0xc7, 0x24);
  1016. }
  1017. /* normalized symbol rate rounded to the closest integer */
  1018. value = (((c->symbol_rate / 1000) << 16) +
  1019. (DS3000_SAMPLE_RATE / 2)) / DS3000_SAMPLE_RATE;
  1020. ds3000_writereg(state, 0x61, value & 0x00ff);
  1021. ds3000_writereg(state, 0x62, (value & 0xff00) >> 8);
  1022. /* co-channel interference cancellation disabled */
  1023. ds3000_writereg(state, 0x56, 0x00);
  1024. /* equalizer disabled */
  1025. ds3000_writereg(state, 0x76, 0x00);
  1026. /*ds3000_writereg(state, 0x08, 0x03);
  1027. ds3000_writereg(state, 0xfd, 0x22);
  1028. ds3000_writereg(state, 0x08, 0x07);
  1029. ds3000_writereg(state, 0xfd, 0x42);
  1030. ds3000_writereg(state, 0x08, 0x07);*/
  1031. if (state->config->ci_mode) {
  1032. switch (c->delivery_system) {
  1033. case SYS_DVBS:
  1034. default:
  1035. ds3000_writereg(state, 0xfd, 0x80);
  1036. break;
  1037. case SYS_DVBS2:
  1038. ds3000_writereg(state, 0xfd, 0x01);
  1039. break;
  1040. }
  1041. }
  1042. /* ds3000 out of software reset */
  1043. ds3000_writereg(state, 0x00, 0x00);
  1044. /* start ds3000 build-in uC */
  1045. ds3000_writereg(state, 0xb2, 0x00);
  1046. ds3000_set_carrier_offset(fe, offset_khz);
  1047. for (i = 0; i < 30 ; i++) {
  1048. ds3000_read_status(fe, &status);
  1049. if (status && FE_HAS_LOCK)
  1050. break;
  1051. msleep(10);
  1052. }
  1053. return 0;
  1054. }
  1055. static int ds3000_tune(struct dvb_frontend *fe,
  1056. struct dvb_frontend_parameters *p,
  1057. unsigned int mode_flags,
  1058. unsigned int *delay,
  1059. fe_status_t *status)
  1060. {
  1061. if (p) {
  1062. int ret = ds3000_set_frontend(fe, p);
  1063. if (ret)
  1064. return ret;
  1065. }
  1066. *delay = HZ / 5;
  1067. return ds3000_read_status(fe, status);
  1068. }
  1069. static enum dvbfe_algo ds3000_get_algo(struct dvb_frontend *fe)
  1070. {
  1071. dprintk("%s()\n", __func__);
  1072. return DVBFE_ALGO_HW;
  1073. }
  1074. /*
  1075. * Initialise or wake up device
  1076. *
  1077. * Power config will reset and load initial firmware if required
  1078. */
  1079. static int ds3000_initfe(struct dvb_frontend *fe)
  1080. {
  1081. struct ds3000_state *state = fe->demodulator_priv;
  1082. int ret;
  1083. dprintk("%s()\n", __func__);
  1084. /* hard reset */
  1085. ds3000_writereg(state, 0x08, 0x01 | ds3000_readreg(state, 0x08));
  1086. msleep(1);
  1087. /* TS2020 init */
  1088. ds3000_tuner_writereg(state, 0x42, 0x73);
  1089. ds3000_tuner_writereg(state, 0x05, 0x01);
  1090. ds3000_tuner_writereg(state, 0x62, 0xf5);
  1091. /* Load the firmware if required */
  1092. ret = ds3000_firmware_ondemand(fe);
  1093. if (ret != 0) {
  1094. printk(KERN_ERR "%s: Unable initialize firmware\n", __func__);
  1095. return ret;
  1096. }
  1097. return 0;
  1098. }
  1099. /* Put device to sleep */
  1100. static int ds3000_sleep(struct dvb_frontend *fe)
  1101. {
  1102. dprintk("%s()\n", __func__);
  1103. return 0;
  1104. }
  1105. static struct dvb_frontend_ops ds3000_ops = {
  1106. .info = {
  1107. .name = "Montage Technology DS3000/TS2020",
  1108. .type = FE_QPSK,
  1109. .frequency_min = 950000,
  1110. .frequency_max = 2150000,
  1111. .frequency_stepsize = 1011, /* kHz for QPSK frontends */
  1112. .frequency_tolerance = 5000,
  1113. .symbol_rate_min = 1000000,
  1114. .symbol_rate_max = 45000000,
  1115. .caps = FE_CAN_INVERSION_AUTO |
  1116. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1117. FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
  1118. FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1119. FE_CAN_2G_MODULATION |
  1120. FE_CAN_QPSK | FE_CAN_RECOVER
  1121. },
  1122. .release = ds3000_release,
  1123. .init = ds3000_initfe,
  1124. .sleep = ds3000_sleep,
  1125. .read_status = ds3000_read_status,
  1126. .read_ber = ds3000_read_ber,
  1127. .read_signal_strength = ds3000_read_signal_strength,
  1128. .read_snr = ds3000_read_snr,
  1129. .read_ucblocks = ds3000_read_ucblocks,
  1130. .set_voltage = ds3000_set_voltage,
  1131. .set_tone = ds3000_set_tone,
  1132. .diseqc_send_master_cmd = ds3000_send_diseqc_msg,
  1133. .diseqc_send_burst = ds3000_diseqc_send_burst,
  1134. .get_frontend_algo = ds3000_get_algo,
  1135. .set_property = ds3000_set_property,
  1136. .get_property = ds3000_get_property,
  1137. .set_frontend = ds3000_set_frontend,
  1138. .tune = ds3000_tune,
  1139. };
  1140. module_param(debug, int, 0644);
  1141. MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
  1142. MODULE_DESCRIPTION("DVB Frontend module for Montage Technology "
  1143. "DS3000/TS2020 hardware");
  1144. MODULE_AUTHOR("Konstantin Dimitrov");
  1145. MODULE_LICENSE("GPL");