lgs8gxx.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. /*
  2. * Support for Legend Silicon GB20600 (a.k.a DMB-TH) demodulator
  3. * LGS8913, LGS8GL5, LGS8G75
  4. * experimental support LGS8G42, LGS8G52
  5. *
  6. * Copyright (C) 2007-2009 David T.L. Wong <davidtlwong@gmail.com>
  7. * Copyright (C) 2008 Sirius International (Hong Kong) Limited
  8. * Timothy Lee <timothy.lee@siriushk.com> (for initial work on LGS8GL5)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. */
  25. #include <asm/div64.h>
  26. #include <linux/firmware.h>
  27. #include "dvb_frontend.h"
  28. #include "lgs8gxx.h"
  29. #include "lgs8gxx_priv.h"
  30. #define dprintk(args...) \
  31. do { \
  32. if (debug) \
  33. printk(KERN_DEBUG "lgs8gxx: " args); \
  34. } while (0)
  35. static int debug;
  36. static int fake_signal_str = 1;
  37. #define LGS8GXX_FIRMWARE "lgs8g75.fw"
  38. module_param(debug, int, 0644);
  39. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  40. module_param(fake_signal_str, int, 0644);
  41. MODULE_PARM_DESC(fake_signal_str, "fake signal strength for LGS8913."
  42. "Signal strength calculation is slow.(default:on).");
  43. /* LGS8GXX internal helper functions */
  44. static int lgs8gxx_write_reg(struct lgs8gxx_state *priv, u8 reg, u8 data)
  45. {
  46. int ret;
  47. u8 buf[] = { reg, data };
  48. struct i2c_msg msg = { .flags = 0, .buf = buf, .len = 2 };
  49. msg.addr = priv->config->demod_address;
  50. if (priv->config->prod != LGS8GXX_PROD_LGS8G75 && reg >= 0xC0)
  51. msg.addr += 0x02;
  52. if (debug >= 2)
  53. dprintk("%s: reg=0x%02X, data=0x%02X\n", __func__, reg, data);
  54. ret = i2c_transfer(priv->i2c, &msg, 1);
  55. if (ret != 1)
  56. dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
  57. __func__, reg, data, ret);
  58. return (ret != 1) ? -1 : 0;
  59. }
  60. static int lgs8gxx_read_reg(struct lgs8gxx_state *priv, u8 reg, u8 *p_data)
  61. {
  62. int ret;
  63. u8 dev_addr;
  64. u8 b0[] = { reg };
  65. u8 b1[] = { 0 };
  66. struct i2c_msg msg[] = {
  67. { .flags = 0, .buf = b0, .len = 1 },
  68. { .flags = I2C_M_RD, .buf = b1, .len = 1 },
  69. };
  70. dev_addr = priv->config->demod_address;
  71. if (priv->config->prod != LGS8GXX_PROD_LGS8G75 && reg >= 0xC0)
  72. dev_addr += 0x02;
  73. msg[1].addr = msg[0].addr = dev_addr;
  74. ret = i2c_transfer(priv->i2c, msg, 2);
  75. if (ret != 2) {
  76. dprintk("%s: error reg=0x%x, ret=%i\n", __func__, reg, ret);
  77. return -1;
  78. }
  79. *p_data = b1[0];
  80. if (debug >= 2)
  81. dprintk("%s: reg=0x%02X, data=0x%02X\n", __func__, reg, b1[0]);
  82. return 0;
  83. }
  84. static int lgs8gxx_soft_reset(struct lgs8gxx_state *priv)
  85. {
  86. lgs8gxx_write_reg(priv, 0x02, 0x00);
  87. msleep(1);
  88. lgs8gxx_write_reg(priv, 0x02, 0x01);
  89. msleep(100);
  90. return 0;
  91. }
  92. static int wait_reg_mask(struct lgs8gxx_state *priv, u8 reg, u8 mask,
  93. u8 val, u8 delay, u8 tries)
  94. {
  95. u8 t;
  96. int i;
  97. for (i = 0; i < tries; i++) {
  98. lgs8gxx_read_reg(priv, reg, &t);
  99. if ((t & mask) == val)
  100. return 0;
  101. msleep(delay);
  102. }
  103. return 1;
  104. }
  105. static int lgs8gxx_set_ad_mode(struct lgs8gxx_state *priv)
  106. {
  107. const struct lgs8gxx_config *config = priv->config;
  108. u8 if_conf;
  109. if_conf = 0x10; /* AGC output on, RF_AGC output off; */
  110. if_conf |=
  111. ((config->ext_adc) ? 0x80 : 0x00) |
  112. ((config->if_neg_center) ? 0x04 : 0x00) |
  113. ((config->if_freq == 0) ? 0x08 : 0x00) | /* Baseband */
  114. ((config->adc_signed) ? 0x02 : 0x00) |
  115. ((config->if_neg_edge) ? 0x01 : 0x00);
  116. if (config->ext_adc &&
  117. (config->prod == LGS8GXX_PROD_LGS8G52)) {
  118. lgs8gxx_write_reg(priv, 0xBA, 0x40);
  119. }
  120. lgs8gxx_write_reg(priv, 0x07, if_conf);
  121. return 0;
  122. }
  123. static int lgs8gxx_set_if_freq(struct lgs8gxx_state *priv, u32 freq /*in kHz*/)
  124. {
  125. u64 val;
  126. u32 v32;
  127. u32 if_clk;
  128. if_clk = priv->config->if_clk_freq;
  129. val = freq;
  130. if (freq != 0) {
  131. val <<= 32;
  132. if (if_clk != 0)
  133. do_div(val, if_clk);
  134. v32 = val & 0xFFFFFFFF;
  135. dprintk("Set IF Freq to %dkHz\n", freq);
  136. } else {
  137. v32 = 0;
  138. dprintk("Set IF Freq to baseband\n");
  139. }
  140. dprintk("AFC_INIT_FREQ = 0x%08X\n", v32);
  141. if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
  142. lgs8gxx_write_reg(priv, 0x08, 0xFF & (v32));
  143. lgs8gxx_write_reg(priv, 0x09, 0xFF & (v32 >> 8));
  144. lgs8gxx_write_reg(priv, 0x0A, 0xFF & (v32 >> 16));
  145. lgs8gxx_write_reg(priv, 0x0B, 0xFF & (v32 >> 24));
  146. } else {
  147. lgs8gxx_write_reg(priv, 0x09, 0xFF & (v32));
  148. lgs8gxx_write_reg(priv, 0x0A, 0xFF & (v32 >> 8));
  149. lgs8gxx_write_reg(priv, 0x0B, 0xFF & (v32 >> 16));
  150. lgs8gxx_write_reg(priv, 0x0C, 0xFF & (v32 >> 24));
  151. }
  152. return 0;
  153. }
  154. static int lgs8gxx_get_afc_phase(struct lgs8gxx_state *priv)
  155. {
  156. u64 val;
  157. u32 v32 = 0;
  158. u8 reg_addr, t;
  159. int i;
  160. if (priv->config->prod == LGS8GXX_PROD_LGS8G75)
  161. reg_addr = 0x23;
  162. else
  163. reg_addr = 0x48;
  164. for (i = 0; i < 4; i++) {
  165. lgs8gxx_read_reg(priv, reg_addr, &t);
  166. v32 <<= 8;
  167. v32 |= t;
  168. reg_addr--;
  169. }
  170. val = v32;
  171. val *= priv->config->if_clk_freq;
  172. val >>= 32;
  173. dprintk("AFC = %u kHz\n", (u32)val);
  174. return 0;
  175. }
  176. static int lgs8gxx_set_mode_auto(struct lgs8gxx_state *priv)
  177. {
  178. u8 t;
  179. u8 prod = priv->config->prod;
  180. if (prod == LGS8GXX_PROD_LGS8913)
  181. lgs8gxx_write_reg(priv, 0xC6, 0x01);
  182. if (prod == LGS8GXX_PROD_LGS8G75) {
  183. lgs8gxx_read_reg(priv, 0x0C, &t);
  184. t &= (~0x04);
  185. lgs8gxx_write_reg(priv, 0x0C, t | 0x80);
  186. lgs8gxx_write_reg(priv, 0x39, 0x00);
  187. lgs8gxx_write_reg(priv, 0x3D, 0x04);
  188. } else if (prod == LGS8GXX_PROD_LGS8913 ||
  189. prod == LGS8GXX_PROD_LGS8GL5 ||
  190. prod == LGS8GXX_PROD_LGS8G42 ||
  191. prod == LGS8GXX_PROD_LGS8G52 ||
  192. prod == LGS8GXX_PROD_LGS8G54) {
  193. lgs8gxx_read_reg(priv, 0x7E, &t);
  194. lgs8gxx_write_reg(priv, 0x7E, t | 0x01);
  195. /* clear FEC self reset */
  196. lgs8gxx_read_reg(priv, 0xC5, &t);
  197. lgs8gxx_write_reg(priv, 0xC5, t & 0xE0);
  198. }
  199. if (prod == LGS8GXX_PROD_LGS8913) {
  200. /* FEC auto detect */
  201. lgs8gxx_write_reg(priv, 0xC1, 0x03);
  202. lgs8gxx_read_reg(priv, 0x7C, &t);
  203. t = (t & 0x8C) | 0x03;
  204. lgs8gxx_write_reg(priv, 0x7C, t);
  205. /* BER test mode */
  206. lgs8gxx_read_reg(priv, 0xC3, &t);
  207. t = (t & 0xEF) | 0x10;
  208. lgs8gxx_write_reg(priv, 0xC3, t);
  209. }
  210. if (priv->config->prod == LGS8GXX_PROD_LGS8G52)
  211. lgs8gxx_write_reg(priv, 0xD9, 0x40);
  212. return 0;
  213. }
  214. static int lgs8gxx_set_mode_manual(struct lgs8gxx_state *priv)
  215. {
  216. u8 t;
  217. if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
  218. u8 t2;
  219. lgs8gxx_read_reg(priv, 0x0C, &t);
  220. t &= (~0x80);
  221. lgs8gxx_write_reg(priv, 0x0C, t);
  222. lgs8gxx_read_reg(priv, 0x0C, &t);
  223. lgs8gxx_read_reg(priv, 0x19, &t2);
  224. if (((t&0x03) == 0x01) && (t2&0x01)) {
  225. lgs8gxx_write_reg(priv, 0x6E, 0x05);
  226. lgs8gxx_write_reg(priv, 0x39, 0x02);
  227. lgs8gxx_write_reg(priv, 0x39, 0x03);
  228. lgs8gxx_write_reg(priv, 0x3D, 0x05);
  229. lgs8gxx_write_reg(priv, 0x3E, 0x28);
  230. lgs8gxx_write_reg(priv, 0x53, 0x80);
  231. } else {
  232. lgs8gxx_write_reg(priv, 0x6E, 0x3F);
  233. lgs8gxx_write_reg(priv, 0x39, 0x00);
  234. lgs8gxx_write_reg(priv, 0x3D, 0x04);
  235. }
  236. lgs8gxx_soft_reset(priv);
  237. return 0;
  238. }
  239. /* turn off auto-detect; manual settings */
  240. lgs8gxx_write_reg(priv, 0x7E, 0);
  241. if (priv->config->prod == LGS8GXX_PROD_LGS8913)
  242. lgs8gxx_write_reg(priv, 0xC1, 0);
  243. lgs8gxx_read_reg(priv, 0xC5, &t);
  244. t = (t & 0xE0) | 0x06;
  245. lgs8gxx_write_reg(priv, 0xC5, t);
  246. lgs8gxx_soft_reset(priv);
  247. return 0;
  248. }
  249. static int lgs8gxx_is_locked(struct lgs8gxx_state *priv, u8 *locked)
  250. {
  251. int ret = 0;
  252. u8 t;
  253. if (priv->config->prod == LGS8GXX_PROD_LGS8G75)
  254. ret = lgs8gxx_read_reg(priv, 0x13, &t);
  255. else
  256. ret = lgs8gxx_read_reg(priv, 0x4B, &t);
  257. if (ret != 0)
  258. return ret;
  259. if (priv->config->prod == LGS8GXX_PROD_LGS8G75)
  260. *locked = ((t & 0x80) == 0x80) ? 1 : 0;
  261. else
  262. *locked = ((t & 0xC0) == 0xC0) ? 1 : 0;
  263. return 0;
  264. }
  265. /* Wait for Code Acquisition Lock */
  266. static int lgs8gxx_wait_ca_lock(struct lgs8gxx_state *priv, u8 *locked)
  267. {
  268. int ret = 0;
  269. u8 reg, mask, val;
  270. if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
  271. reg = 0x13;
  272. mask = 0x80;
  273. val = 0x80;
  274. } else {
  275. reg = 0x4B;
  276. mask = 0xC0;
  277. val = 0xC0;
  278. }
  279. ret = wait_reg_mask(priv, reg, mask, val, 50, 40);
  280. *locked = (ret == 0) ? 1 : 0;
  281. return 0;
  282. }
  283. static int lgs8gxx_is_autodetect_finished(struct lgs8gxx_state *priv,
  284. u8 *finished)
  285. {
  286. int ret = 0;
  287. u8 reg, mask, val;
  288. if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
  289. reg = 0x1f;
  290. mask = 0xC0;
  291. val = 0x80;
  292. } else {
  293. reg = 0xA4;
  294. mask = 0x03;
  295. val = 0x01;
  296. }
  297. ret = wait_reg_mask(priv, reg, mask, val, 10, 20);
  298. *finished = (ret == 0) ? 1 : 0;
  299. return 0;
  300. }
  301. static int lgs8gxx_autolock_gi(struct lgs8gxx_state *priv, u8 gi, u8 cpn,
  302. u8 *locked)
  303. {
  304. int err = 0;
  305. u8 ad_fini = 0;
  306. u8 t1, t2;
  307. if (gi == GI_945)
  308. dprintk("try GI 945\n");
  309. else if (gi == GI_595)
  310. dprintk("try GI 595\n");
  311. else if (gi == GI_420)
  312. dprintk("try GI 420\n");
  313. if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
  314. lgs8gxx_read_reg(priv, 0x0C, &t1);
  315. lgs8gxx_read_reg(priv, 0x18, &t2);
  316. t1 &= ~(GI_MASK);
  317. t1 |= gi;
  318. t2 &= 0xFE;
  319. t2 |= cpn ? 0x01 : 0x00;
  320. lgs8gxx_write_reg(priv, 0x0C, t1);
  321. lgs8gxx_write_reg(priv, 0x18, t2);
  322. } else {
  323. lgs8gxx_write_reg(priv, 0x04, gi);
  324. }
  325. lgs8gxx_soft_reset(priv);
  326. err = lgs8gxx_wait_ca_lock(priv, locked);
  327. if (err || !(*locked))
  328. return err;
  329. err = lgs8gxx_is_autodetect_finished(priv, &ad_fini);
  330. if (err != 0)
  331. return err;
  332. if (ad_fini) {
  333. dprintk("auto detect finished\n");
  334. } else
  335. *locked = 0;
  336. return 0;
  337. }
  338. static int lgs8gxx_auto_detect(struct lgs8gxx_state *priv,
  339. u8 *detected_param, u8 *gi)
  340. {
  341. int i, j;
  342. int err = 0;
  343. u8 locked = 0, tmp_gi;
  344. dprintk("%s\n", __func__);
  345. lgs8gxx_set_mode_auto(priv);
  346. if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
  347. lgs8gxx_write_reg(priv, 0x67, 0xAA);
  348. lgs8gxx_write_reg(priv, 0x6E, 0x3F);
  349. } else {
  350. /* Guard Interval */
  351. lgs8gxx_write_reg(priv, 0x03, 00);
  352. }
  353. for (i = 0; i < 2; i++) {
  354. for (j = 0; j < 2; j++) {
  355. tmp_gi = GI_945;
  356. err = lgs8gxx_autolock_gi(priv, GI_945, j, &locked);
  357. if (err)
  358. goto out;
  359. if (locked)
  360. goto locked;
  361. }
  362. for (j = 0; j < 2; j++) {
  363. tmp_gi = GI_420;
  364. err = lgs8gxx_autolock_gi(priv, GI_420, j, &locked);
  365. if (err)
  366. goto out;
  367. if (locked)
  368. goto locked;
  369. }
  370. tmp_gi = GI_595;
  371. err = lgs8gxx_autolock_gi(priv, GI_595, 1, &locked);
  372. if (err)
  373. goto out;
  374. if (locked)
  375. goto locked;
  376. }
  377. locked:
  378. if ((err == 0) && (locked == 1)) {
  379. u8 t;
  380. if (priv->config->prod != LGS8GXX_PROD_LGS8G75) {
  381. lgs8gxx_read_reg(priv, 0xA2, &t);
  382. *detected_param = t;
  383. } else {
  384. lgs8gxx_read_reg(priv, 0x1F, &t);
  385. *detected_param = t & 0x3F;
  386. }
  387. if (tmp_gi == GI_945)
  388. dprintk("GI 945 locked\n");
  389. else if (tmp_gi == GI_595)
  390. dprintk("GI 595 locked\n");
  391. else if (tmp_gi == GI_420)
  392. dprintk("GI 420 locked\n");
  393. *gi = tmp_gi;
  394. }
  395. if (!locked)
  396. err = -1;
  397. out:
  398. return err;
  399. }
  400. static void lgs8gxx_auto_lock(struct lgs8gxx_state *priv)
  401. {
  402. s8 err;
  403. u8 gi = 0x2;
  404. u8 detected_param = 0;
  405. err = lgs8gxx_auto_detect(priv, &detected_param, &gi);
  406. if (err != 0) {
  407. dprintk("lgs8gxx_auto_detect failed\n");
  408. } else
  409. dprintk("detected param = 0x%02X\n", detected_param);
  410. /* Apply detected parameters */
  411. if (priv->config->prod == LGS8GXX_PROD_LGS8913) {
  412. u8 inter_leave_len = detected_param & TIM_MASK ;
  413. /* Fix 8913 time interleaver detection bug */
  414. inter_leave_len = (inter_leave_len == TIM_MIDDLE) ? 0x60 : 0x40;
  415. detected_param &= CF_MASK | SC_MASK | LGS_FEC_MASK;
  416. detected_param |= inter_leave_len;
  417. }
  418. if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
  419. u8 t;
  420. lgs8gxx_read_reg(priv, 0x19, &t);
  421. t &= 0x81;
  422. t |= detected_param << 1;
  423. lgs8gxx_write_reg(priv, 0x19, t);
  424. } else {
  425. lgs8gxx_write_reg(priv, 0x7D, detected_param);
  426. if (priv->config->prod == LGS8GXX_PROD_LGS8913)
  427. lgs8gxx_write_reg(priv, 0xC0, detected_param);
  428. }
  429. /* lgs8gxx_soft_reset(priv); */
  430. /* Enter manual mode */
  431. lgs8gxx_set_mode_manual(priv);
  432. switch (gi) {
  433. case GI_945:
  434. priv->curr_gi = 945; break;
  435. case GI_595:
  436. priv->curr_gi = 595; break;
  437. case GI_420:
  438. priv->curr_gi = 420; break;
  439. default:
  440. priv->curr_gi = 945; break;
  441. }
  442. }
  443. static int lgs8gxx_set_mpeg_mode(struct lgs8gxx_state *priv,
  444. u8 serial, u8 clk_pol, u8 clk_gated)
  445. {
  446. int ret = 0;
  447. u8 t, reg_addr;
  448. reg_addr = (priv->config->prod == LGS8GXX_PROD_LGS8G75) ? 0x30 : 0xC2;
  449. ret = lgs8gxx_read_reg(priv, reg_addr, &t);
  450. if (ret != 0)
  451. return ret;
  452. t &= 0xF8;
  453. t |= serial ? TS_SERIAL : TS_PARALLEL;
  454. t |= clk_pol ? TS_CLK_INVERTED : TS_CLK_NORMAL;
  455. t |= clk_gated ? TS_CLK_GATED : TS_CLK_FREERUN;
  456. ret = lgs8gxx_write_reg(priv, reg_addr, t);
  457. if (ret != 0)
  458. return ret;
  459. return 0;
  460. }
  461. /* A/D input peak-to-peak voltage range */
  462. static int lgs8g75_set_adc_vpp(struct lgs8gxx_state *priv,
  463. u8 sel)
  464. {
  465. u8 r26 = 0x73, r27 = 0x90;
  466. if (priv->config->prod != LGS8GXX_PROD_LGS8G75)
  467. return 0;
  468. r26 |= (sel & 0x01) << 7;
  469. r27 |= (sel & 0x02) >> 1;
  470. lgs8gxx_write_reg(priv, 0x26, r26);
  471. lgs8gxx_write_reg(priv, 0x27, r27);
  472. return 0;
  473. }
  474. /* LGS8913 demod frontend functions */
  475. static int lgs8913_init(struct lgs8gxx_state *priv)
  476. {
  477. u8 t;
  478. /* LGS8913 specific */
  479. lgs8gxx_write_reg(priv, 0xc1, 0x3);
  480. lgs8gxx_read_reg(priv, 0x7c, &t);
  481. lgs8gxx_write_reg(priv, 0x7c, (t&0x8c) | 0x3);
  482. /* LGS8913 specific */
  483. lgs8gxx_read_reg(priv, 0xc3, &t);
  484. lgs8gxx_write_reg(priv, 0xc3, t&0x10);
  485. return 0;
  486. }
  487. static int lgs8g75_init_data(struct lgs8gxx_state *priv)
  488. {
  489. const struct firmware *fw;
  490. int rc;
  491. int i;
  492. rc = request_firmware(&fw, LGS8GXX_FIRMWARE, &priv->i2c->dev);
  493. if (rc)
  494. return rc;
  495. lgs8gxx_write_reg(priv, 0xC6, 0x40);
  496. lgs8gxx_write_reg(priv, 0x3D, 0x04);
  497. lgs8gxx_write_reg(priv, 0x39, 0x00);
  498. lgs8gxx_write_reg(priv, 0x3A, 0x00);
  499. lgs8gxx_write_reg(priv, 0x38, 0x00);
  500. lgs8gxx_write_reg(priv, 0x3B, 0x00);
  501. lgs8gxx_write_reg(priv, 0x38, 0x00);
  502. for (i = 0; i < fw->size; i++) {
  503. lgs8gxx_write_reg(priv, 0x38, 0x00);
  504. lgs8gxx_write_reg(priv, 0x3A, (u8)(i&0xff));
  505. lgs8gxx_write_reg(priv, 0x3B, (u8)(i>>8));
  506. lgs8gxx_write_reg(priv, 0x3C, fw->data[i]);
  507. }
  508. lgs8gxx_write_reg(priv, 0x38, 0x00);
  509. release_firmware(fw);
  510. return 0;
  511. }
  512. static int lgs8gxx_init(struct dvb_frontend *fe)
  513. {
  514. struct lgs8gxx_state *priv =
  515. (struct lgs8gxx_state *)fe->demodulator_priv;
  516. const struct lgs8gxx_config *config = priv->config;
  517. u8 data = 0;
  518. s8 err;
  519. dprintk("%s\n", __func__);
  520. lgs8gxx_read_reg(priv, 0, &data);
  521. dprintk("reg 0 = 0x%02X\n", data);
  522. if (config->prod == LGS8GXX_PROD_LGS8G75)
  523. lgs8g75_set_adc_vpp(priv, config->adc_vpp);
  524. /* Setup MPEG output format */
  525. err = lgs8gxx_set_mpeg_mode(priv, config->serial_ts,
  526. config->ts_clk_pol,
  527. config->ts_clk_gated);
  528. if (err != 0)
  529. return -EIO;
  530. if (config->prod == LGS8GXX_PROD_LGS8913)
  531. lgs8913_init(priv);
  532. lgs8gxx_set_if_freq(priv, priv->config->if_freq);
  533. lgs8gxx_set_ad_mode(priv);
  534. return 0;
  535. }
  536. static void lgs8gxx_release(struct dvb_frontend *fe)
  537. {
  538. struct lgs8gxx_state *state = fe->demodulator_priv;
  539. dprintk("%s\n", __func__);
  540. kfree(state);
  541. }
  542. static int lgs8gxx_write(struct dvb_frontend *fe, const u8 buf[], int len)
  543. {
  544. struct lgs8gxx_state *priv = fe->demodulator_priv;
  545. if (len != 2)
  546. return -EINVAL;
  547. return lgs8gxx_write_reg(priv, buf[0], buf[1]);
  548. }
  549. static int lgs8gxx_set_fe(struct dvb_frontend *fe)
  550. {
  551. struct lgs8gxx_state *priv = fe->demodulator_priv;
  552. dprintk("%s\n", __func__);
  553. /* set frequency */
  554. if (fe->ops.tuner_ops.set_params) {
  555. fe->ops.tuner_ops.set_params(fe);
  556. if (fe->ops.i2c_gate_ctrl)
  557. fe->ops.i2c_gate_ctrl(fe, 0);
  558. }
  559. /* start auto lock */
  560. lgs8gxx_auto_lock(priv);
  561. msleep(10);
  562. return 0;
  563. }
  564. static int lgs8gxx_get_fe(struct dvb_frontend *fe)
  565. {
  566. struct dtv_frontend_properties *fe_params = &fe->dtv_property_cache;
  567. dprintk("%s\n", __func__);
  568. /* TODO: get real readings from device */
  569. /* inversion status */
  570. fe_params->inversion = INVERSION_OFF;
  571. /* bandwidth */
  572. fe_params->bandwidth_hz = 8000000;
  573. fe_params->code_rate_HP = FEC_AUTO;
  574. fe_params->code_rate_LP = FEC_AUTO;
  575. fe_params->modulation = QAM_AUTO;
  576. /* transmission mode */
  577. fe_params->transmission_mode = TRANSMISSION_MODE_AUTO;
  578. /* guard interval */
  579. fe_params->guard_interval = GUARD_INTERVAL_AUTO;
  580. /* hierarchy */
  581. fe_params->hierarchy = HIERARCHY_NONE;
  582. return 0;
  583. }
  584. static
  585. int lgs8gxx_get_tune_settings(struct dvb_frontend *fe,
  586. struct dvb_frontend_tune_settings *fesettings)
  587. {
  588. /* FIXME: copy from tda1004x.c */
  589. fesettings->min_delay_ms = 800;
  590. fesettings->step_size = 0;
  591. fesettings->max_drift = 0;
  592. return 0;
  593. }
  594. static int lgs8gxx_read_status(struct dvb_frontend *fe,
  595. enum fe_status *fe_status)
  596. {
  597. struct lgs8gxx_state *priv = fe->demodulator_priv;
  598. s8 ret;
  599. u8 t, locked = 0;
  600. dprintk("%s\n", __func__);
  601. *fe_status = 0;
  602. lgs8gxx_get_afc_phase(priv);
  603. lgs8gxx_is_locked(priv, &locked);
  604. if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
  605. if (locked)
  606. *fe_status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  607. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  608. return 0;
  609. }
  610. ret = lgs8gxx_read_reg(priv, 0x4B, &t);
  611. if (ret != 0)
  612. return -EIO;
  613. dprintk("Reg 0x4B: 0x%02X\n", t);
  614. *fe_status = 0;
  615. if (priv->config->prod == LGS8GXX_PROD_LGS8913) {
  616. if ((t & 0x40) == 0x40)
  617. *fe_status |= FE_HAS_SIGNAL | FE_HAS_CARRIER;
  618. if ((t & 0x80) == 0x80)
  619. *fe_status |= FE_HAS_VITERBI | FE_HAS_SYNC |
  620. FE_HAS_LOCK;
  621. } else {
  622. if ((t & 0x80) == 0x80)
  623. *fe_status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  624. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  625. }
  626. /* success */
  627. dprintk("%s: fe_status=0x%x\n", __func__, *fe_status);
  628. return 0;
  629. }
  630. static int lgs8gxx_read_signal_agc(struct lgs8gxx_state *priv, u16 *signal)
  631. {
  632. u16 v;
  633. u8 agc_lvl[2], cat;
  634. dprintk("%s()\n", __func__);
  635. lgs8gxx_read_reg(priv, 0x3F, &agc_lvl[0]);
  636. lgs8gxx_read_reg(priv, 0x3E, &agc_lvl[1]);
  637. v = agc_lvl[0];
  638. v <<= 8;
  639. v |= agc_lvl[1];
  640. dprintk("agc_lvl: 0x%04X\n", v);
  641. if (v < 0x100)
  642. cat = 0;
  643. else if (v < 0x190)
  644. cat = 5;
  645. else if (v < 0x2A8)
  646. cat = 4;
  647. else if (v < 0x381)
  648. cat = 3;
  649. else if (v < 0x400)
  650. cat = 2;
  651. else if (v == 0x400)
  652. cat = 1;
  653. else
  654. cat = 0;
  655. *signal = cat * 65535 / 5;
  656. return 0;
  657. }
  658. static int lgs8913_read_signal_strength(struct lgs8gxx_state *priv, u16 *signal)
  659. {
  660. u8 t; s8 ret;
  661. s16 max_strength = 0;
  662. u8 str;
  663. u16 i, gi = priv->curr_gi;
  664. dprintk("%s\n", __func__);
  665. ret = lgs8gxx_read_reg(priv, 0x4B, &t);
  666. if (ret != 0)
  667. return -EIO;
  668. if (fake_signal_str) {
  669. if ((t & 0xC0) == 0xC0) {
  670. dprintk("Fake signal strength\n");
  671. *signal = 0x7FFF;
  672. } else
  673. *signal = 0;
  674. return 0;
  675. }
  676. dprintk("gi = %d\n", gi);
  677. for (i = 0; i < gi; i++) {
  678. if ((i & 0xFF) == 0)
  679. lgs8gxx_write_reg(priv, 0x84, 0x03 & (i >> 8));
  680. lgs8gxx_write_reg(priv, 0x83, i & 0xFF);
  681. lgs8gxx_read_reg(priv, 0x94, &str);
  682. if (max_strength < str)
  683. max_strength = str;
  684. }
  685. *signal = max_strength;
  686. dprintk("%s: signal=0x%02X\n", __func__, *signal);
  687. lgs8gxx_read_reg(priv, 0x95, &t);
  688. dprintk("%s: AVG Noise=0x%02X\n", __func__, t);
  689. return 0;
  690. }
  691. static int lgs8g75_read_signal_strength(struct lgs8gxx_state *priv, u16 *signal)
  692. {
  693. u8 t;
  694. s16 v = 0;
  695. dprintk("%s\n", __func__);
  696. lgs8gxx_read_reg(priv, 0xB1, &t);
  697. v |= t;
  698. v <<= 8;
  699. lgs8gxx_read_reg(priv, 0xB0, &t);
  700. v |= t;
  701. *signal = v;
  702. dprintk("%s: signal=0x%02X\n", __func__, *signal);
  703. return 0;
  704. }
  705. static int lgs8gxx_read_signal_strength(struct dvb_frontend *fe, u16 *signal)
  706. {
  707. struct lgs8gxx_state *priv = fe->demodulator_priv;
  708. if (priv->config->prod == LGS8GXX_PROD_LGS8913)
  709. return lgs8913_read_signal_strength(priv, signal);
  710. else if (priv->config->prod == LGS8GXX_PROD_LGS8G75)
  711. return lgs8g75_read_signal_strength(priv, signal);
  712. else
  713. return lgs8gxx_read_signal_agc(priv, signal);
  714. }
  715. static int lgs8gxx_read_snr(struct dvb_frontend *fe, u16 *snr)
  716. {
  717. struct lgs8gxx_state *priv = fe->demodulator_priv;
  718. u8 t;
  719. *snr = 0;
  720. if (priv->config->prod == LGS8GXX_PROD_LGS8G75)
  721. lgs8gxx_read_reg(priv, 0x34, &t);
  722. else
  723. lgs8gxx_read_reg(priv, 0x95, &t);
  724. dprintk("AVG Noise=0x%02X\n", t);
  725. *snr = 256 - t;
  726. *snr <<= 8;
  727. dprintk("snr=0x%x\n", *snr);
  728. return 0;
  729. }
  730. static int lgs8gxx_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  731. {
  732. *ucblocks = 0;
  733. dprintk("%s: ucblocks=0x%x\n", __func__, *ucblocks);
  734. return 0;
  735. }
  736. static void packet_counter_start(struct lgs8gxx_state *priv)
  737. {
  738. u8 orig, t;
  739. if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
  740. lgs8gxx_read_reg(priv, 0x30, &orig);
  741. orig &= 0xE7;
  742. t = orig | 0x10;
  743. lgs8gxx_write_reg(priv, 0x30, t);
  744. t = orig | 0x18;
  745. lgs8gxx_write_reg(priv, 0x30, t);
  746. t = orig | 0x10;
  747. lgs8gxx_write_reg(priv, 0x30, t);
  748. } else {
  749. lgs8gxx_write_reg(priv, 0xC6, 0x01);
  750. lgs8gxx_write_reg(priv, 0xC6, 0x41);
  751. lgs8gxx_write_reg(priv, 0xC6, 0x01);
  752. }
  753. }
  754. static void packet_counter_stop(struct lgs8gxx_state *priv)
  755. {
  756. u8 t;
  757. if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
  758. lgs8gxx_read_reg(priv, 0x30, &t);
  759. t &= 0xE7;
  760. lgs8gxx_write_reg(priv, 0x30, t);
  761. } else {
  762. lgs8gxx_write_reg(priv, 0xC6, 0x81);
  763. }
  764. }
  765. static int lgs8gxx_read_ber(struct dvb_frontend *fe, u32 *ber)
  766. {
  767. struct lgs8gxx_state *priv = fe->demodulator_priv;
  768. u8 reg_err, reg_total, t;
  769. u32 total_cnt = 0, err_cnt = 0;
  770. int i;
  771. dprintk("%s\n", __func__);
  772. packet_counter_start(priv);
  773. msleep(200);
  774. packet_counter_stop(priv);
  775. if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
  776. reg_total = 0x28; reg_err = 0x2C;
  777. } else {
  778. reg_total = 0xD0; reg_err = 0xD4;
  779. }
  780. for (i = 0; i < 4; i++) {
  781. total_cnt <<= 8;
  782. lgs8gxx_read_reg(priv, reg_total+3-i, &t);
  783. total_cnt |= t;
  784. }
  785. for (i = 0; i < 4; i++) {
  786. err_cnt <<= 8;
  787. lgs8gxx_read_reg(priv, reg_err+3-i, &t);
  788. err_cnt |= t;
  789. }
  790. dprintk("error=%d total=%d\n", err_cnt, total_cnt);
  791. if (total_cnt == 0)
  792. *ber = 0;
  793. else
  794. *ber = err_cnt * 100 / total_cnt;
  795. dprintk("%s: ber=0x%x\n", __func__, *ber);
  796. return 0;
  797. }
  798. static int lgs8gxx_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  799. {
  800. struct lgs8gxx_state *priv = fe->demodulator_priv;
  801. if (priv->config->tuner_address == 0)
  802. return 0;
  803. if (enable) {
  804. u8 v = 0x80 | priv->config->tuner_address;
  805. return lgs8gxx_write_reg(priv, 0x01, v);
  806. }
  807. return lgs8gxx_write_reg(priv, 0x01, 0);
  808. }
  809. static struct dvb_frontend_ops lgs8gxx_ops = {
  810. .delsys = { SYS_DTMB },
  811. .info = {
  812. .name = "Legend Silicon LGS8913/LGS8GXX DMB-TH",
  813. .frequency_min = 474000000,
  814. .frequency_max = 858000000,
  815. .frequency_stepsize = 10000,
  816. .caps =
  817. FE_CAN_FEC_AUTO |
  818. FE_CAN_QAM_AUTO |
  819. FE_CAN_TRANSMISSION_MODE_AUTO |
  820. FE_CAN_GUARD_INTERVAL_AUTO
  821. },
  822. .release = lgs8gxx_release,
  823. .init = lgs8gxx_init,
  824. .write = lgs8gxx_write,
  825. .i2c_gate_ctrl = lgs8gxx_i2c_gate_ctrl,
  826. .set_frontend = lgs8gxx_set_fe,
  827. .get_frontend = lgs8gxx_get_fe,
  828. .get_tune_settings = lgs8gxx_get_tune_settings,
  829. .read_status = lgs8gxx_read_status,
  830. .read_ber = lgs8gxx_read_ber,
  831. .read_signal_strength = lgs8gxx_read_signal_strength,
  832. .read_snr = lgs8gxx_read_snr,
  833. .read_ucblocks = lgs8gxx_read_ucblocks,
  834. };
  835. struct dvb_frontend *lgs8gxx_attach(const struct lgs8gxx_config *config,
  836. struct i2c_adapter *i2c)
  837. {
  838. struct lgs8gxx_state *priv = NULL;
  839. u8 data = 0;
  840. dprintk("%s()\n", __func__);
  841. if (config == NULL || i2c == NULL)
  842. return NULL;
  843. priv = kzalloc(sizeof(struct lgs8gxx_state), GFP_KERNEL);
  844. if (priv == NULL)
  845. goto error_out;
  846. priv->config = config;
  847. priv->i2c = i2c;
  848. /* check if the demod is there */
  849. if (lgs8gxx_read_reg(priv, 0, &data) != 0) {
  850. dprintk("%s lgs8gxx not found at i2c addr 0x%02X\n",
  851. __func__, priv->config->demod_address);
  852. goto error_out;
  853. }
  854. lgs8gxx_read_reg(priv, 1, &data);
  855. memcpy(&priv->frontend.ops, &lgs8gxx_ops,
  856. sizeof(struct dvb_frontend_ops));
  857. priv->frontend.demodulator_priv = priv;
  858. if (config->prod == LGS8GXX_PROD_LGS8G75)
  859. lgs8g75_init_data(priv);
  860. return &priv->frontend;
  861. error_out:
  862. dprintk("%s() error_out\n", __func__);
  863. kfree(priv);
  864. return NULL;
  865. }
  866. EXPORT_SYMBOL(lgs8gxx_attach);
  867. MODULE_DESCRIPTION("Legend Silicon LGS8913/LGS8GXX DMB-TH demodulator driver");
  868. MODULE_AUTHOR("David T. L. Wong <davidtlwong@gmail.com>");
  869. MODULE_LICENSE("GPL");
  870. MODULE_FIRMWARE(LGS8GXX_FIRMWARE);