lgs8gxx.c 23 KB

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