lgs8gxx.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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. module_param(debug, int, 0644);
  38. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  39. module_param(fake_signal_str, int, 0644);
  40. MODULE_PARM_DESC(fake_signal_str, "fake signal strength for LGS8913."
  41. "Signal strength calculation is slow.(default:on).");
  42. /* LGS8GXX internal helper functions */
  43. static int lgs8gxx_write_reg(struct lgs8gxx_state *priv, u8 reg, u8 data)
  44. {
  45. int ret;
  46. u8 buf[] = { reg, data };
  47. struct i2c_msg msg = { .flags = 0, .buf = buf, .len = 2 };
  48. msg.addr = priv->config->demod_address;
  49. if (priv->config->prod != LGS8GXX_PROD_LGS8G75 && reg >= 0xC0)
  50. msg.addr += 0x02;
  51. if (debug >= 2)
  52. dprintk("%s: reg=0x%02X, data=0x%02X\n", __func__, reg, data);
  53. ret = i2c_transfer(priv->i2c, &msg, 1);
  54. if (ret != 1)
  55. dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
  56. __func__, reg, data, ret);
  57. return (ret != 1) ? -1 : 0;
  58. }
  59. static int lgs8gxx_read_reg(struct lgs8gxx_state *priv, u8 reg, u8 *p_data)
  60. {
  61. int ret;
  62. u8 dev_addr;
  63. u8 b0[] = { reg };
  64. u8 b1[] = { 0 };
  65. struct i2c_msg msg[] = {
  66. { .flags = 0, .buf = b0, .len = 1 },
  67. { .flags = I2C_M_RD, .buf = b1, .len = 1 },
  68. };
  69. dev_addr = priv->config->demod_address;
  70. if (priv->config->prod != LGS8GXX_PROD_LGS8G75 && reg >= 0xC0)
  71. dev_addr += 0x02;
  72. msg[1].addr = msg[0].addr = dev_addr;
  73. ret = i2c_transfer(priv->i2c, msg, 2);
  74. if (ret != 2) {
  75. dprintk("%s: error reg=0x%x, ret=%i\n", __func__, reg, ret);
  76. return -1;
  77. }
  78. *p_data = b1[0];
  79. if (debug >= 2)
  80. dprintk("%s: reg=0x%02X, data=0x%02X\n", __func__, reg, b1[0]);
  81. return 0;
  82. }
  83. static int lgs8gxx_soft_reset(struct lgs8gxx_state *priv)
  84. {
  85. lgs8gxx_write_reg(priv, 0x02, 0x00);
  86. msleep(1);
  87. lgs8gxx_write_reg(priv, 0x02, 0x01);
  88. msleep(100);
  89. return 0;
  90. }
  91. static int wait_reg_mask(struct lgs8gxx_state *priv, u8 reg, u8 mask,
  92. u8 val, u8 delay, u8 tries)
  93. {
  94. u8 t;
  95. int i;
  96. for (i = 0; i < tries; i++) {
  97. lgs8gxx_read_reg(priv, reg, &t);
  98. if ((t & mask) == val)
  99. return 0;
  100. msleep(delay);
  101. }
  102. return 1;
  103. }
  104. static int lgs8gxx_set_ad_mode(struct lgs8gxx_state *priv)
  105. {
  106. const struct lgs8gxx_config *config = priv->config;
  107. u8 if_conf;
  108. if_conf = 0x10; /* AGC output on, RF_AGC output off; */
  109. if_conf |=
  110. ((config->ext_adc) ? 0x80 : 0x00) |
  111. ((config->if_neg_center) ? 0x04 : 0x00) |
  112. ((config->if_freq == 0) ? 0x08 : 0x00) | /* Baseband */
  113. ((config->adc_signed) ? 0x02 : 0x00) |
  114. ((config->if_neg_edge) ? 0x01 : 0x00);
  115. if (config->ext_adc &&
  116. (config->prod == LGS8GXX_PROD_LGS8G52)) {
  117. lgs8gxx_write_reg(priv, 0xBA, 0x40);
  118. }
  119. lgs8gxx_write_reg(priv, 0x07, if_conf);
  120. return 0;
  121. }
  122. static int lgs8gxx_set_if_freq(struct lgs8gxx_state *priv, u32 freq /*in kHz*/)
  123. {
  124. u64 val;
  125. u32 v32;
  126. u32 if_clk;
  127. if_clk = priv->config->if_clk_freq;
  128. val = freq;
  129. if (freq != 0) {
  130. val <<= 32;
  131. if (if_clk != 0)
  132. do_div(val, if_clk);
  133. v32 = val & 0xFFFFFFFF;
  134. dprintk("Set IF Freq to %dkHz\n", freq);
  135. } else {
  136. v32 = 0;
  137. dprintk("Set IF Freq to baseband\n");
  138. }
  139. dprintk("AFC_INIT_FREQ = 0x%08X\n", v32);
  140. if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
  141. lgs8gxx_write_reg(priv, 0x08, 0xFF & (v32));
  142. lgs8gxx_write_reg(priv, 0x09, 0xFF & (v32 >> 8));
  143. lgs8gxx_write_reg(priv, 0x0A, 0xFF & (v32 >> 16));
  144. lgs8gxx_write_reg(priv, 0x0B, 0xFF & (v32 >> 24));
  145. } else {
  146. lgs8gxx_write_reg(priv, 0x09, 0xFF & (v32));
  147. lgs8gxx_write_reg(priv, 0x0A, 0xFF & (v32 >> 8));
  148. lgs8gxx_write_reg(priv, 0x0B, 0xFF & (v32 >> 16));
  149. lgs8gxx_write_reg(priv, 0x0C, 0xFF & (v32 >> 24));
  150. }
  151. return 0;
  152. }
  153. static int lgs8gxx_get_afc_phase(struct lgs8gxx_state *priv)
  154. {
  155. u64 val;
  156. u32 v32 = 0;
  157. u8 reg_addr, t;
  158. int i;
  159. if (priv->config->prod == LGS8GXX_PROD_LGS8G75)
  160. reg_addr = 0x23;
  161. else
  162. reg_addr = 0x48;
  163. for (i = 0; i < 4; i++) {
  164. lgs8gxx_read_reg(priv, reg_addr, &t);
  165. v32 <<= 8;
  166. v32 |= t;
  167. reg_addr--;
  168. }
  169. val = v32;
  170. val *= priv->config->if_clk_freq;
  171. val >>= 32;
  172. dprintk("AFC = %u kHz\n", (u32)val);
  173. return 0;
  174. }
  175. static int lgs8gxx_set_mode_auto(struct lgs8gxx_state *priv)
  176. {
  177. u8 t;
  178. u8 prod = priv->config->prod;
  179. if (prod == LGS8GXX_PROD_LGS8913)
  180. lgs8gxx_write_reg(priv, 0xC6, 0x01);
  181. if (prod == LGS8GXX_PROD_LGS8G75) {
  182. lgs8gxx_read_reg(priv, 0x0C, &t);
  183. t &= (~0x04);
  184. lgs8gxx_write_reg(priv, 0x0C, t | 0x80);
  185. lgs8gxx_write_reg(priv, 0x39, 0x00);
  186. lgs8gxx_write_reg(priv, 0x3D, 0x04);
  187. } else if (prod == LGS8GXX_PROD_LGS8913 ||
  188. prod == LGS8GXX_PROD_LGS8GL5 ||
  189. prod == LGS8GXX_PROD_LGS8G42 ||
  190. prod == LGS8GXX_PROD_LGS8G52 ||
  191. prod == LGS8GXX_PROD_LGS8G54) {
  192. lgs8gxx_read_reg(priv, 0x7E, &t);
  193. lgs8gxx_write_reg(priv, 0x7E, t | 0x01);
  194. /* clear FEC self reset */
  195. lgs8gxx_read_reg(priv, 0xC5, &t);
  196. lgs8gxx_write_reg(priv, 0xC5, t & 0xE0);
  197. }
  198. if (prod == LGS8GXX_PROD_LGS8913) {
  199. /* FEC auto detect */
  200. lgs8gxx_write_reg(priv, 0xC1, 0x03);
  201. lgs8gxx_read_reg(priv, 0x7C, &t);
  202. t = (t & 0x8C) | 0x03;
  203. lgs8gxx_write_reg(priv, 0x7C, t);
  204. /* BER test mode */
  205. lgs8gxx_read_reg(priv, 0xC3, &t);
  206. t = (t & 0xEF) | 0x10;
  207. lgs8gxx_write_reg(priv, 0xC3, t);
  208. }
  209. if (priv->config->prod == LGS8GXX_PROD_LGS8G52)
  210. lgs8gxx_write_reg(priv, 0xD9, 0x40);
  211. return 0;
  212. }
  213. static int lgs8gxx_set_mode_manual(struct lgs8gxx_state *priv)
  214. {
  215. int ret = 0;
  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. ret = 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, "lgs8g75.fw", &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. struct dvb_frontend_parameters *fe_params)
  551. {
  552. struct lgs8gxx_state *priv = fe->demodulator_priv;
  553. dprintk("%s\n", __func__);
  554. /* set frequency */
  555. if (fe->ops.tuner_ops.set_params) {
  556. fe->ops.tuner_ops.set_params(fe, fe_params);
  557. if (fe->ops.i2c_gate_ctrl)
  558. fe->ops.i2c_gate_ctrl(fe, 0);
  559. }
  560. /* start auto lock */
  561. lgs8gxx_auto_lock(priv);
  562. msleep(10);
  563. return 0;
  564. }
  565. static int lgs8gxx_get_fe(struct dvb_frontend *fe,
  566. struct dvb_frontend_parameters *fe_params)
  567. {
  568. dprintk("%s\n", __func__);
  569. /* TODO: get real readings from device */
  570. /* inversion status */
  571. fe_params->inversion = INVERSION_OFF;
  572. /* bandwidth */
  573. fe_params->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
  574. fe_params->u.ofdm.code_rate_HP = FEC_AUTO;
  575. fe_params->u.ofdm.code_rate_LP = FEC_AUTO;
  576. fe_params->u.ofdm.constellation = QAM_AUTO;
  577. /* transmission mode */
  578. fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
  579. /* guard interval */
  580. fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
  581. /* hierarchy */
  582. fe_params->u.ofdm.hierarchy_information = HIERARCHY_NONE;
  583. return 0;
  584. }
  585. static
  586. int lgs8gxx_get_tune_settings(struct dvb_frontend *fe,
  587. struct dvb_frontend_tune_settings *fesettings)
  588. {
  589. /* FIXME: copy from tda1004x.c */
  590. fesettings->min_delay_ms = 800;
  591. fesettings->step_size = 0;
  592. fesettings->max_drift = 0;
  593. return 0;
  594. }
  595. static int lgs8gxx_read_status(struct dvb_frontend *fe, fe_status_t *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. .info = {
  811. .name = "Legend Silicon LGS8913/LGS8GXX DMB-TH",
  812. .type = FE_OFDM,
  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");