Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / dvb-frontends / lgs8gxx.c
blob84af8a12f26ae5ef5a1fdf9f0cfe72c39954726e
1 /*
2 * Support for Legend Silicon GB20600 (a.k.a DMB-TH) demodulator
3 * LGS8913, LGS8GL5, LGS8G75
4 * experimental support LGS8G42, LGS8G52
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)
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.
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.
22 #include <asm/div64.h>
23 #include <linux/firmware.h>
25 #include <media/dvb_frontend.h>
27 #include "lgs8gxx.h"
28 #include "lgs8gxx_priv.h"
30 #define dprintk(args...) \
31 do { \
32 if (debug) \
33 printk(KERN_DEBUG "lgs8gxx: " args); \
34 } while (0)
36 static int debug;
37 static int fake_signal_str = 1;
39 #define LGS8GXX_FIRMWARE "lgs8g75.fw"
41 module_param(debug, int, 0644);
42 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
44 module_param(fake_signal_str, int, 0644);
45 MODULE_PARM_DESC(fake_signal_str, "fake signal strength for LGS8913."
46 "Signal strength calculation is slow.(default:on).");
48 /* LGS8GXX internal helper functions */
50 static int lgs8gxx_write_reg(struct lgs8gxx_state *priv, u8 reg, u8 data)
52 int ret;
53 u8 buf[] = { reg, data };
54 struct i2c_msg msg = { .flags = 0, .buf = buf, .len = 2 };
56 msg.addr = priv->config->demod_address;
57 if (priv->config->prod != LGS8GXX_PROD_LGS8G75 && reg >= 0xC0)
58 msg.addr += 0x02;
60 if (debug >= 2)
61 dprintk("%s: reg=0x%02X, data=0x%02X\n", __func__, reg, data);
63 ret = i2c_transfer(priv->i2c, &msg, 1);
65 if (ret != 1)
66 dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
67 __func__, reg, data, ret);
69 return (ret != 1) ? -1 : 0;
72 static int lgs8gxx_read_reg(struct lgs8gxx_state *priv, u8 reg, u8 *p_data)
74 int ret;
75 u8 dev_addr;
77 u8 b0[] = { reg };
78 u8 b1[] = { 0 };
79 struct i2c_msg msg[] = {
80 { .flags = 0, .buf = b0, .len = 1 },
81 { .flags = I2C_M_RD, .buf = b1, .len = 1 },
84 dev_addr = priv->config->demod_address;
85 if (priv->config->prod != LGS8GXX_PROD_LGS8G75 && reg >= 0xC0)
86 dev_addr += 0x02;
87 msg[1].addr = msg[0].addr = dev_addr;
89 ret = i2c_transfer(priv->i2c, msg, 2);
90 if (ret != 2) {
91 dprintk("%s: error reg=0x%x, ret=%i\n", __func__, reg, ret);
92 return -1;
95 *p_data = b1[0];
96 if (debug >= 2)
97 dprintk("%s: reg=0x%02X, data=0x%02X\n", __func__, reg, b1[0]);
98 return 0;
101 static int lgs8gxx_soft_reset(struct lgs8gxx_state *priv)
103 lgs8gxx_write_reg(priv, 0x02, 0x00);
104 msleep(1);
105 lgs8gxx_write_reg(priv, 0x02, 0x01);
106 msleep(100);
108 return 0;
111 static int wait_reg_mask(struct lgs8gxx_state *priv, u8 reg, u8 mask,
112 u8 val, u8 delay, u8 tries)
114 u8 t;
115 int i;
117 for (i = 0; i < tries; i++) {
118 lgs8gxx_read_reg(priv, reg, &t);
120 if ((t & mask) == val)
121 return 0;
122 msleep(delay);
125 return 1;
128 static int lgs8gxx_set_ad_mode(struct lgs8gxx_state *priv)
130 const struct lgs8gxx_config *config = priv->config;
131 u8 if_conf;
133 if_conf = 0x10; /* AGC output on, RF_AGC output off; */
135 if_conf |=
136 ((config->ext_adc) ? 0x80 : 0x00) |
137 ((config->if_neg_center) ? 0x04 : 0x00) |
138 ((config->if_freq == 0) ? 0x08 : 0x00) | /* Baseband */
139 ((config->adc_signed) ? 0x02 : 0x00) |
140 ((config->if_neg_edge) ? 0x01 : 0x00);
142 if (config->ext_adc &&
143 (config->prod == LGS8GXX_PROD_LGS8G52)) {
144 lgs8gxx_write_reg(priv, 0xBA, 0x40);
147 lgs8gxx_write_reg(priv, 0x07, if_conf);
149 return 0;
152 static int lgs8gxx_set_if_freq(struct lgs8gxx_state *priv, u32 freq /*in kHz*/)
154 u64 val;
155 u32 v32;
156 u32 if_clk;
158 if_clk = priv->config->if_clk_freq;
160 val = freq;
161 if (freq != 0) {
162 val <<= 32;
163 if (if_clk != 0)
164 do_div(val, if_clk);
165 v32 = val & 0xFFFFFFFF;
166 dprintk("Set IF Freq to %dkHz\n", freq);
167 } else {
168 v32 = 0;
169 dprintk("Set IF Freq to baseband\n");
171 dprintk("AFC_INIT_FREQ = 0x%08X\n", v32);
173 if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
174 lgs8gxx_write_reg(priv, 0x08, 0xFF & (v32));
175 lgs8gxx_write_reg(priv, 0x09, 0xFF & (v32 >> 8));
176 lgs8gxx_write_reg(priv, 0x0A, 0xFF & (v32 >> 16));
177 lgs8gxx_write_reg(priv, 0x0B, 0xFF & (v32 >> 24));
178 } else {
179 lgs8gxx_write_reg(priv, 0x09, 0xFF & (v32));
180 lgs8gxx_write_reg(priv, 0x0A, 0xFF & (v32 >> 8));
181 lgs8gxx_write_reg(priv, 0x0B, 0xFF & (v32 >> 16));
182 lgs8gxx_write_reg(priv, 0x0C, 0xFF & (v32 >> 24));
185 return 0;
188 static int lgs8gxx_get_afc_phase(struct lgs8gxx_state *priv)
190 u64 val;
191 u32 v32 = 0;
192 u8 reg_addr, t;
193 int i;
195 if (priv->config->prod == LGS8GXX_PROD_LGS8G75)
196 reg_addr = 0x23;
197 else
198 reg_addr = 0x48;
200 for (i = 0; i < 4; i++) {
201 lgs8gxx_read_reg(priv, reg_addr, &t);
202 v32 <<= 8;
203 v32 |= t;
204 reg_addr--;
207 val = v32;
208 val *= priv->config->if_clk_freq;
209 val >>= 32;
210 dprintk("AFC = %u kHz\n", (u32)val);
211 return 0;
214 static int lgs8gxx_set_mode_auto(struct lgs8gxx_state *priv)
216 u8 t;
217 u8 prod = priv->config->prod;
219 if (prod == LGS8GXX_PROD_LGS8913)
220 lgs8gxx_write_reg(priv, 0xC6, 0x01);
222 if (prod == LGS8GXX_PROD_LGS8G75) {
223 lgs8gxx_read_reg(priv, 0x0C, &t);
224 t &= (~0x04);
225 lgs8gxx_write_reg(priv, 0x0C, t | 0x80);
226 lgs8gxx_write_reg(priv, 0x39, 0x00);
227 lgs8gxx_write_reg(priv, 0x3D, 0x04);
228 } else if (prod == LGS8GXX_PROD_LGS8913 ||
229 prod == LGS8GXX_PROD_LGS8GL5 ||
230 prod == LGS8GXX_PROD_LGS8G42 ||
231 prod == LGS8GXX_PROD_LGS8G52 ||
232 prod == LGS8GXX_PROD_LGS8G54) {
233 lgs8gxx_read_reg(priv, 0x7E, &t);
234 lgs8gxx_write_reg(priv, 0x7E, t | 0x01);
236 /* clear FEC self reset */
237 lgs8gxx_read_reg(priv, 0xC5, &t);
238 lgs8gxx_write_reg(priv, 0xC5, t & 0xE0);
241 if (prod == LGS8GXX_PROD_LGS8913) {
242 /* FEC auto detect */
243 lgs8gxx_write_reg(priv, 0xC1, 0x03);
245 lgs8gxx_read_reg(priv, 0x7C, &t);
246 t = (t & 0x8C) | 0x03;
247 lgs8gxx_write_reg(priv, 0x7C, t);
249 /* BER test mode */
250 lgs8gxx_read_reg(priv, 0xC3, &t);
251 t = (t & 0xEF) | 0x10;
252 lgs8gxx_write_reg(priv, 0xC3, t);
255 if (priv->config->prod == LGS8GXX_PROD_LGS8G52)
256 lgs8gxx_write_reg(priv, 0xD9, 0x40);
258 return 0;
261 static int lgs8gxx_set_mode_manual(struct lgs8gxx_state *priv)
263 u8 t;
265 if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
266 u8 t2;
267 lgs8gxx_read_reg(priv, 0x0C, &t);
268 t &= (~0x80);
269 lgs8gxx_write_reg(priv, 0x0C, t);
271 lgs8gxx_read_reg(priv, 0x0C, &t);
272 lgs8gxx_read_reg(priv, 0x19, &t2);
274 if (((t&0x03) == 0x01) && (t2&0x01)) {
275 lgs8gxx_write_reg(priv, 0x6E, 0x05);
276 lgs8gxx_write_reg(priv, 0x39, 0x02);
277 lgs8gxx_write_reg(priv, 0x39, 0x03);
278 lgs8gxx_write_reg(priv, 0x3D, 0x05);
279 lgs8gxx_write_reg(priv, 0x3E, 0x28);
280 lgs8gxx_write_reg(priv, 0x53, 0x80);
281 } else {
282 lgs8gxx_write_reg(priv, 0x6E, 0x3F);
283 lgs8gxx_write_reg(priv, 0x39, 0x00);
284 lgs8gxx_write_reg(priv, 0x3D, 0x04);
287 lgs8gxx_soft_reset(priv);
288 return 0;
291 /* turn off auto-detect; manual settings */
292 lgs8gxx_write_reg(priv, 0x7E, 0);
293 if (priv->config->prod == LGS8GXX_PROD_LGS8913)
294 lgs8gxx_write_reg(priv, 0xC1, 0);
296 lgs8gxx_read_reg(priv, 0xC5, &t);
297 t = (t & 0xE0) | 0x06;
298 lgs8gxx_write_reg(priv, 0xC5, t);
300 lgs8gxx_soft_reset(priv);
302 return 0;
305 static int lgs8gxx_is_locked(struct lgs8gxx_state *priv, u8 *locked)
307 int ret = 0;
308 u8 t;
310 if (priv->config->prod == LGS8GXX_PROD_LGS8G75)
311 ret = lgs8gxx_read_reg(priv, 0x13, &t);
312 else
313 ret = lgs8gxx_read_reg(priv, 0x4B, &t);
314 if (ret != 0)
315 return ret;
317 if (priv->config->prod == LGS8GXX_PROD_LGS8G75)
318 *locked = ((t & 0x80) == 0x80) ? 1 : 0;
319 else
320 *locked = ((t & 0xC0) == 0xC0) ? 1 : 0;
321 return 0;
324 /* Wait for Code Acquisition Lock */
325 static int lgs8gxx_wait_ca_lock(struct lgs8gxx_state *priv, u8 *locked)
327 int ret = 0;
328 u8 reg, mask, val;
330 if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
331 reg = 0x13;
332 mask = 0x80;
333 val = 0x80;
334 } else {
335 reg = 0x4B;
336 mask = 0xC0;
337 val = 0xC0;
340 ret = wait_reg_mask(priv, reg, mask, val, 50, 40);
341 *locked = (ret == 0) ? 1 : 0;
343 return 0;
346 static int lgs8gxx_is_autodetect_finished(struct lgs8gxx_state *priv,
347 u8 *finished)
349 int ret = 0;
350 u8 reg, mask, val;
352 if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
353 reg = 0x1f;
354 mask = 0xC0;
355 val = 0x80;
356 } else {
357 reg = 0xA4;
358 mask = 0x03;
359 val = 0x01;
362 ret = wait_reg_mask(priv, reg, mask, val, 10, 20);
363 *finished = (ret == 0) ? 1 : 0;
365 return 0;
368 static int lgs8gxx_autolock_gi(struct lgs8gxx_state *priv, u8 gi, u8 cpn,
369 u8 *locked)
371 int err = 0;
372 u8 ad_fini = 0;
373 u8 t1, t2;
375 if (gi == GI_945)
376 dprintk("try GI 945\n");
377 else if (gi == GI_595)
378 dprintk("try GI 595\n");
379 else if (gi == GI_420)
380 dprintk("try GI 420\n");
381 if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
382 lgs8gxx_read_reg(priv, 0x0C, &t1);
383 lgs8gxx_read_reg(priv, 0x18, &t2);
384 t1 &= ~(GI_MASK);
385 t1 |= gi;
386 t2 &= 0xFE;
387 t2 |= cpn ? 0x01 : 0x00;
388 lgs8gxx_write_reg(priv, 0x0C, t1);
389 lgs8gxx_write_reg(priv, 0x18, t2);
390 } else {
391 lgs8gxx_write_reg(priv, 0x04, gi);
393 lgs8gxx_soft_reset(priv);
394 err = lgs8gxx_wait_ca_lock(priv, locked);
395 if (err || !(*locked))
396 return err;
397 err = lgs8gxx_is_autodetect_finished(priv, &ad_fini);
398 if (err != 0)
399 return err;
400 if (ad_fini) {
401 dprintk("auto detect finished\n");
402 } else
403 *locked = 0;
405 return 0;
408 static int lgs8gxx_auto_detect(struct lgs8gxx_state *priv,
409 u8 *detected_param, u8 *gi)
411 int i, j;
412 int err = 0;
413 u8 locked = 0, tmp_gi;
415 dprintk("%s\n", __func__);
417 lgs8gxx_set_mode_auto(priv);
418 if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
419 lgs8gxx_write_reg(priv, 0x67, 0xAA);
420 lgs8gxx_write_reg(priv, 0x6E, 0x3F);
421 } else {
422 /* Guard Interval */
423 lgs8gxx_write_reg(priv, 0x03, 00);
426 for (i = 0; i < 2; i++) {
427 for (j = 0; j < 2; j++) {
428 tmp_gi = GI_945;
429 err = lgs8gxx_autolock_gi(priv, GI_945, j, &locked);
430 if (err)
431 goto out;
432 if (locked)
433 goto locked;
435 for (j = 0; j < 2; j++) {
436 tmp_gi = GI_420;
437 err = lgs8gxx_autolock_gi(priv, GI_420, j, &locked);
438 if (err)
439 goto out;
440 if (locked)
441 goto locked;
443 tmp_gi = GI_595;
444 err = lgs8gxx_autolock_gi(priv, GI_595, 1, &locked);
445 if (err)
446 goto out;
447 if (locked)
448 goto locked;
451 locked:
452 if ((err == 0) && (locked == 1)) {
453 u8 t;
455 if (priv->config->prod != LGS8GXX_PROD_LGS8G75) {
456 lgs8gxx_read_reg(priv, 0xA2, &t);
457 *detected_param = t;
458 } else {
459 lgs8gxx_read_reg(priv, 0x1F, &t);
460 *detected_param = t & 0x3F;
463 if (tmp_gi == GI_945)
464 dprintk("GI 945 locked\n");
465 else if (tmp_gi == GI_595)
466 dprintk("GI 595 locked\n");
467 else if (tmp_gi == GI_420)
468 dprintk("GI 420 locked\n");
469 *gi = tmp_gi;
471 if (!locked)
472 err = -1;
474 out:
475 return err;
478 static void lgs8gxx_auto_lock(struct lgs8gxx_state *priv)
480 s8 err;
481 u8 gi = 0x2;
482 u8 detected_param = 0;
484 err = lgs8gxx_auto_detect(priv, &detected_param, &gi);
486 if (err != 0) {
487 dprintk("lgs8gxx_auto_detect failed\n");
488 } else
489 dprintk("detected param = 0x%02X\n", detected_param);
491 /* Apply detected parameters */
492 if (priv->config->prod == LGS8GXX_PROD_LGS8913) {
493 u8 inter_leave_len = detected_param & TIM_MASK ;
494 /* Fix 8913 time interleaver detection bug */
495 inter_leave_len = (inter_leave_len == TIM_MIDDLE) ? 0x60 : 0x40;
496 detected_param &= CF_MASK | SC_MASK | LGS_FEC_MASK;
497 detected_param |= inter_leave_len;
499 if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
500 u8 t;
501 lgs8gxx_read_reg(priv, 0x19, &t);
502 t &= 0x81;
503 t |= detected_param << 1;
504 lgs8gxx_write_reg(priv, 0x19, t);
505 } else {
506 lgs8gxx_write_reg(priv, 0x7D, detected_param);
507 if (priv->config->prod == LGS8GXX_PROD_LGS8913)
508 lgs8gxx_write_reg(priv, 0xC0, detected_param);
510 /* lgs8gxx_soft_reset(priv); */
512 /* Enter manual mode */
513 lgs8gxx_set_mode_manual(priv);
515 switch (gi) {
516 case GI_945:
517 priv->curr_gi = 945; break;
518 case GI_595:
519 priv->curr_gi = 595; break;
520 case GI_420:
521 priv->curr_gi = 420; break;
522 default:
523 priv->curr_gi = 945; break;
527 static int lgs8gxx_set_mpeg_mode(struct lgs8gxx_state *priv,
528 u8 serial, u8 clk_pol, u8 clk_gated)
530 int ret = 0;
531 u8 t, reg_addr;
533 reg_addr = (priv->config->prod == LGS8GXX_PROD_LGS8G75) ? 0x30 : 0xC2;
534 ret = lgs8gxx_read_reg(priv, reg_addr, &t);
535 if (ret != 0)
536 return ret;
538 t &= 0xF8;
539 t |= serial ? TS_SERIAL : TS_PARALLEL;
540 t |= clk_pol ? TS_CLK_INVERTED : TS_CLK_NORMAL;
541 t |= clk_gated ? TS_CLK_GATED : TS_CLK_FREERUN;
543 ret = lgs8gxx_write_reg(priv, reg_addr, t);
544 if (ret != 0)
545 return ret;
547 return 0;
550 /* A/D input peak-to-peak voltage range */
551 static int lgs8g75_set_adc_vpp(struct lgs8gxx_state *priv,
552 u8 sel)
554 u8 r26 = 0x73, r27 = 0x90;
556 if (priv->config->prod != LGS8GXX_PROD_LGS8G75)
557 return 0;
559 r26 |= (sel & 0x01) << 7;
560 r27 |= (sel & 0x02) >> 1;
561 lgs8gxx_write_reg(priv, 0x26, r26);
562 lgs8gxx_write_reg(priv, 0x27, r27);
564 return 0;
567 /* LGS8913 demod frontend functions */
569 static int lgs8913_init(struct lgs8gxx_state *priv)
571 u8 t;
573 /* LGS8913 specific */
574 lgs8gxx_write_reg(priv, 0xc1, 0x3);
576 lgs8gxx_read_reg(priv, 0x7c, &t);
577 lgs8gxx_write_reg(priv, 0x7c, (t&0x8c) | 0x3);
579 /* LGS8913 specific */
580 lgs8gxx_read_reg(priv, 0xc3, &t);
581 lgs8gxx_write_reg(priv, 0xc3, t&0x10);
584 return 0;
587 static int lgs8g75_init_data(struct lgs8gxx_state *priv)
589 const struct firmware *fw;
590 int rc;
591 int i;
593 rc = request_firmware(&fw, LGS8GXX_FIRMWARE, &priv->i2c->dev);
594 if (rc)
595 return rc;
597 lgs8gxx_write_reg(priv, 0xC6, 0x40);
599 lgs8gxx_write_reg(priv, 0x3D, 0x04);
600 lgs8gxx_write_reg(priv, 0x39, 0x00);
602 lgs8gxx_write_reg(priv, 0x3A, 0x00);
603 lgs8gxx_write_reg(priv, 0x38, 0x00);
604 lgs8gxx_write_reg(priv, 0x3B, 0x00);
605 lgs8gxx_write_reg(priv, 0x38, 0x00);
607 for (i = 0; i < fw->size; i++) {
608 lgs8gxx_write_reg(priv, 0x38, 0x00);
609 lgs8gxx_write_reg(priv, 0x3A, (u8)(i&0xff));
610 lgs8gxx_write_reg(priv, 0x3B, (u8)(i>>8));
611 lgs8gxx_write_reg(priv, 0x3C, fw->data[i]);
614 lgs8gxx_write_reg(priv, 0x38, 0x00);
616 release_firmware(fw);
617 return 0;
620 static int lgs8gxx_init(struct dvb_frontend *fe)
622 struct lgs8gxx_state *priv =
623 (struct lgs8gxx_state *)fe->demodulator_priv;
624 const struct lgs8gxx_config *config = priv->config;
625 u8 data = 0;
626 s8 err;
627 dprintk("%s\n", __func__);
629 lgs8gxx_read_reg(priv, 0, &data);
630 dprintk("reg 0 = 0x%02X\n", data);
632 if (config->prod == LGS8GXX_PROD_LGS8G75)
633 lgs8g75_set_adc_vpp(priv, config->adc_vpp);
635 /* Setup MPEG output format */
636 err = lgs8gxx_set_mpeg_mode(priv, config->serial_ts,
637 config->ts_clk_pol,
638 config->ts_clk_gated);
639 if (err != 0)
640 return -EIO;
642 if (config->prod == LGS8GXX_PROD_LGS8913)
643 lgs8913_init(priv);
644 lgs8gxx_set_if_freq(priv, priv->config->if_freq);
645 lgs8gxx_set_ad_mode(priv);
647 return 0;
650 static void lgs8gxx_release(struct dvb_frontend *fe)
652 struct lgs8gxx_state *state = fe->demodulator_priv;
653 dprintk("%s\n", __func__);
655 kfree(state);
659 static int lgs8gxx_write(struct dvb_frontend *fe, const u8 buf[], int len)
661 struct lgs8gxx_state *priv = fe->demodulator_priv;
663 if (len != 2)
664 return -EINVAL;
666 return lgs8gxx_write_reg(priv, buf[0], buf[1]);
669 static int lgs8gxx_set_fe(struct dvb_frontend *fe)
671 struct dtv_frontend_properties *fe_params = &fe->dtv_property_cache;
672 struct lgs8gxx_state *priv = fe->demodulator_priv;
674 dprintk("%s\n", __func__);
676 /* set frequency */
677 if (fe->ops.tuner_ops.set_params) {
678 fe->ops.tuner_ops.set_params(fe);
679 if (fe->ops.i2c_gate_ctrl)
680 fe->ops.i2c_gate_ctrl(fe, 0);
683 /* start auto lock */
684 lgs8gxx_auto_lock(priv);
686 msleep(10);
688 /* TODO: get real readings from device */
690 /* bandwidth */
691 fe_params->bandwidth_hz = 8000000;
693 fe_params->code_rate_HP = FEC_AUTO;
694 fe_params->code_rate_LP = FEC_AUTO;
696 fe_params->modulation = QAM_AUTO;
698 /* transmission mode */
699 fe_params->transmission_mode = TRANSMISSION_MODE_AUTO;
701 /* guard interval */
702 fe_params->guard_interval = GUARD_INTERVAL_AUTO;
704 /* hierarchy */
705 fe_params->hierarchy = HIERARCHY_NONE;
707 return 0;
710 static
711 int lgs8gxx_get_tune_settings(struct dvb_frontend *fe,
712 struct dvb_frontend_tune_settings *fesettings)
714 /* FIXME: copy from tda1004x.c */
715 fesettings->min_delay_ms = 800;
716 fesettings->step_size = 0;
717 fesettings->max_drift = 0;
718 return 0;
721 static int lgs8gxx_read_status(struct dvb_frontend *fe,
722 enum fe_status *fe_status)
724 struct lgs8gxx_state *priv = fe->demodulator_priv;
725 s8 ret;
726 u8 t, locked = 0;
728 dprintk("%s\n", __func__);
729 *fe_status = 0;
731 lgs8gxx_get_afc_phase(priv);
732 lgs8gxx_is_locked(priv, &locked);
733 if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
734 if (locked)
735 *fe_status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
736 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
737 return 0;
740 ret = lgs8gxx_read_reg(priv, 0x4B, &t);
741 if (ret != 0)
742 return -EIO;
744 dprintk("Reg 0x4B: 0x%02X\n", t);
746 *fe_status = 0;
747 if (priv->config->prod == LGS8GXX_PROD_LGS8913) {
748 if ((t & 0x40) == 0x40)
749 *fe_status |= FE_HAS_SIGNAL | FE_HAS_CARRIER;
750 if ((t & 0x80) == 0x80)
751 *fe_status |= FE_HAS_VITERBI | FE_HAS_SYNC |
752 FE_HAS_LOCK;
753 } else {
754 if ((t & 0x80) == 0x80)
755 *fe_status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
756 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
759 /* success */
760 dprintk("%s: fe_status=0x%x\n", __func__, *fe_status);
761 return 0;
764 static int lgs8gxx_read_signal_agc(struct lgs8gxx_state *priv, u16 *signal)
766 u16 v;
767 u8 agc_lvl[2], cat;
769 dprintk("%s()\n", __func__);
770 lgs8gxx_read_reg(priv, 0x3F, &agc_lvl[0]);
771 lgs8gxx_read_reg(priv, 0x3E, &agc_lvl[1]);
773 v = agc_lvl[0];
774 v <<= 8;
775 v |= agc_lvl[1];
777 dprintk("agc_lvl: 0x%04X\n", v);
779 if (v < 0x100)
780 cat = 0;
781 else if (v < 0x190)
782 cat = 5;
783 else if (v < 0x2A8)
784 cat = 4;
785 else if (v < 0x381)
786 cat = 3;
787 else if (v < 0x400)
788 cat = 2;
789 else if (v == 0x400)
790 cat = 1;
791 else
792 cat = 0;
794 *signal = cat * 65535 / 5;
796 return 0;
799 static int lgs8913_read_signal_strength(struct lgs8gxx_state *priv, u16 *signal)
801 u8 t; s8 ret;
802 s16 max_strength = 0;
803 u8 str;
804 u16 i, gi = priv->curr_gi;
806 dprintk("%s\n", __func__);
808 ret = lgs8gxx_read_reg(priv, 0x4B, &t);
809 if (ret != 0)
810 return -EIO;
812 if (fake_signal_str) {
813 if ((t & 0xC0) == 0xC0) {
814 dprintk("Fake signal strength\n");
815 *signal = 0x7FFF;
816 } else
817 *signal = 0;
818 return 0;
821 dprintk("gi = %d\n", gi);
822 for (i = 0; i < gi; i++) {
824 if ((i & 0xFF) == 0)
825 lgs8gxx_write_reg(priv, 0x84, 0x03 & (i >> 8));
826 lgs8gxx_write_reg(priv, 0x83, i & 0xFF);
828 lgs8gxx_read_reg(priv, 0x94, &str);
829 if (max_strength < str)
830 max_strength = str;
833 *signal = max_strength;
834 dprintk("%s: signal=0x%02X\n", __func__, *signal);
836 lgs8gxx_read_reg(priv, 0x95, &t);
837 dprintk("%s: AVG Noise=0x%02X\n", __func__, t);
839 return 0;
842 static int lgs8g75_read_signal_strength(struct lgs8gxx_state *priv, u16 *signal)
844 u8 t;
845 s16 v = 0;
847 dprintk("%s\n", __func__);
849 lgs8gxx_read_reg(priv, 0xB1, &t);
850 v |= t;
851 v <<= 8;
852 lgs8gxx_read_reg(priv, 0xB0, &t);
853 v |= t;
855 *signal = v;
856 dprintk("%s: signal=0x%02X\n", __func__, *signal);
858 return 0;
861 static int lgs8gxx_read_signal_strength(struct dvb_frontend *fe, u16 *signal)
863 struct lgs8gxx_state *priv = fe->demodulator_priv;
865 if (priv->config->prod == LGS8GXX_PROD_LGS8913)
866 return lgs8913_read_signal_strength(priv, signal);
867 else if (priv->config->prod == LGS8GXX_PROD_LGS8G75)
868 return lgs8g75_read_signal_strength(priv, signal);
869 else
870 return lgs8gxx_read_signal_agc(priv, signal);
873 static int lgs8gxx_read_snr(struct dvb_frontend *fe, u16 *snr)
875 struct lgs8gxx_state *priv = fe->demodulator_priv;
876 u8 t;
877 *snr = 0;
879 if (priv->config->prod == LGS8GXX_PROD_LGS8G75)
880 lgs8gxx_read_reg(priv, 0x34, &t);
881 else
882 lgs8gxx_read_reg(priv, 0x95, &t);
883 dprintk("AVG Noise=0x%02X\n", t);
884 *snr = 256 - t;
885 *snr <<= 8;
886 dprintk("snr=0x%x\n", *snr);
888 return 0;
891 static int lgs8gxx_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
893 *ucblocks = 0;
894 dprintk("%s: ucblocks=0x%x\n", __func__, *ucblocks);
895 return 0;
898 static void packet_counter_start(struct lgs8gxx_state *priv)
900 u8 orig, t;
902 if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
903 lgs8gxx_read_reg(priv, 0x30, &orig);
904 orig &= 0xE7;
905 t = orig | 0x10;
906 lgs8gxx_write_reg(priv, 0x30, t);
907 t = orig | 0x18;
908 lgs8gxx_write_reg(priv, 0x30, t);
909 t = orig | 0x10;
910 lgs8gxx_write_reg(priv, 0x30, t);
911 } else {
912 lgs8gxx_write_reg(priv, 0xC6, 0x01);
913 lgs8gxx_write_reg(priv, 0xC6, 0x41);
914 lgs8gxx_write_reg(priv, 0xC6, 0x01);
918 static void packet_counter_stop(struct lgs8gxx_state *priv)
920 u8 t;
922 if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
923 lgs8gxx_read_reg(priv, 0x30, &t);
924 t &= 0xE7;
925 lgs8gxx_write_reg(priv, 0x30, t);
926 } else {
927 lgs8gxx_write_reg(priv, 0xC6, 0x81);
931 static int lgs8gxx_read_ber(struct dvb_frontend *fe, u32 *ber)
933 struct lgs8gxx_state *priv = fe->demodulator_priv;
934 u8 reg_err, reg_total, t;
935 u32 total_cnt = 0, err_cnt = 0;
936 int i;
938 dprintk("%s\n", __func__);
940 packet_counter_start(priv);
941 msleep(200);
942 packet_counter_stop(priv);
944 if (priv->config->prod == LGS8GXX_PROD_LGS8G75) {
945 reg_total = 0x28; reg_err = 0x2C;
946 } else {
947 reg_total = 0xD0; reg_err = 0xD4;
950 for (i = 0; i < 4; i++) {
951 total_cnt <<= 8;
952 lgs8gxx_read_reg(priv, reg_total+3-i, &t);
953 total_cnt |= t;
955 for (i = 0; i < 4; i++) {
956 err_cnt <<= 8;
957 lgs8gxx_read_reg(priv, reg_err+3-i, &t);
958 err_cnt |= t;
960 dprintk("error=%d total=%d\n", err_cnt, total_cnt);
962 if (total_cnt == 0)
963 *ber = 0;
964 else
965 *ber = err_cnt * 100 / total_cnt;
967 dprintk("%s: ber=0x%x\n", __func__, *ber);
968 return 0;
971 static int lgs8gxx_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
973 struct lgs8gxx_state *priv = fe->demodulator_priv;
975 if (priv->config->tuner_address == 0)
976 return 0;
977 if (enable) {
978 u8 v = 0x80 | priv->config->tuner_address;
979 return lgs8gxx_write_reg(priv, 0x01, v);
981 return lgs8gxx_write_reg(priv, 0x01, 0);
984 static const struct dvb_frontend_ops lgs8gxx_ops = {
985 .delsys = { SYS_DTMB },
986 .info = {
987 .name = "Legend Silicon LGS8913/LGS8GXX DMB-TH",
988 .frequency_min = 474000000,
989 .frequency_max = 858000000,
990 .frequency_stepsize = 10000,
991 .caps =
992 FE_CAN_FEC_AUTO |
993 FE_CAN_QAM_AUTO |
994 FE_CAN_TRANSMISSION_MODE_AUTO |
995 FE_CAN_GUARD_INTERVAL_AUTO
998 .release = lgs8gxx_release,
1000 .init = lgs8gxx_init,
1001 .write = lgs8gxx_write,
1002 .i2c_gate_ctrl = lgs8gxx_i2c_gate_ctrl,
1004 .set_frontend = lgs8gxx_set_fe,
1005 .get_tune_settings = lgs8gxx_get_tune_settings,
1007 .read_status = lgs8gxx_read_status,
1008 .read_ber = lgs8gxx_read_ber,
1009 .read_signal_strength = lgs8gxx_read_signal_strength,
1010 .read_snr = lgs8gxx_read_snr,
1011 .read_ucblocks = lgs8gxx_read_ucblocks,
1014 struct dvb_frontend *lgs8gxx_attach(const struct lgs8gxx_config *config,
1015 struct i2c_adapter *i2c)
1017 struct lgs8gxx_state *priv = NULL;
1018 u8 data = 0;
1020 dprintk("%s()\n", __func__);
1022 if (config == NULL || i2c == NULL)
1023 return NULL;
1025 priv = kzalloc(sizeof(struct lgs8gxx_state), GFP_KERNEL);
1026 if (priv == NULL)
1027 goto error_out;
1029 priv->config = config;
1030 priv->i2c = i2c;
1032 /* check if the demod is there */
1033 if (lgs8gxx_read_reg(priv, 0, &data) != 0) {
1034 dprintk("%s lgs8gxx not found at i2c addr 0x%02X\n",
1035 __func__, priv->config->demod_address);
1036 goto error_out;
1039 lgs8gxx_read_reg(priv, 1, &data);
1041 memcpy(&priv->frontend.ops, &lgs8gxx_ops,
1042 sizeof(struct dvb_frontend_ops));
1043 priv->frontend.demodulator_priv = priv;
1045 if (config->prod == LGS8GXX_PROD_LGS8G75)
1046 lgs8g75_init_data(priv);
1048 return &priv->frontend;
1050 error_out:
1051 dprintk("%s() error_out\n", __func__);
1052 kfree(priv);
1053 return NULL;
1056 EXPORT_SYMBOL(lgs8gxx_attach);
1058 MODULE_DESCRIPTION("Legend Silicon LGS8913/LGS8GXX DMB-TH demodulator driver");
1059 MODULE_AUTHOR("David T. L. Wong <davidtlwong@gmail.com>");
1060 MODULE_LICENSE("GPL");
1061 MODULE_FIRMWARE(LGS8GXX_FIRMWARE);