[media] rtl2830: implement DVBv5 BER statistic
[linux/fpc-iii.git] / drivers / media / dvb-frontends / rtl2830.c
blob147b3a6898e17e8005229842b8c6dcb722fddd0b
1 /*
2 * Realtek RTL2830 DVB-T demodulator driver
4 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include "rtl2830_priv.h"
20 /* Max transfer size done by I2C transfer functions */
21 #define MAX_XFER_SIZE 64
23 /* write multiple hardware registers */
24 static int rtl2830_wr(struct i2c_client *client, u8 reg, const u8 *val, int len)
26 int ret;
27 u8 buf[MAX_XFER_SIZE];
28 struct i2c_msg msg[1] = {
30 .addr = client->addr,
31 .flags = 0,
32 .len = 1 + len,
33 .buf = buf,
37 if (1 + len > sizeof(buf)) {
38 dev_warn(&client->dev, "i2c wr reg=%04x: len=%d is too big!\n",
39 reg, len);
40 return -EINVAL;
43 buf[0] = reg;
44 memcpy(&buf[1], val, len);
46 ret = i2c_transfer(client->adapter, msg, 1);
47 if (ret == 1) {
48 ret = 0;
49 } else {
50 dev_warn(&client->dev, "i2c wr failed=%d reg=%02x len=%d\n",
51 ret, reg, len);
52 ret = -EREMOTEIO;
55 return ret;
58 /* read multiple hardware registers */
59 static int rtl2830_rd(struct i2c_client *client, u8 reg, u8 *val, int len)
61 int ret;
62 struct i2c_msg msg[2] = {
64 .addr = client->addr,
65 .flags = 0,
66 .len = 1,
67 .buf = &reg,
68 }, {
69 .addr = client->addr,
70 .flags = I2C_M_RD,
71 .len = len,
72 .buf = val,
76 ret = i2c_transfer(client->adapter, msg, 2);
77 if (ret == 2) {
78 ret = 0;
79 } else {
80 dev_warn(&client->dev, "i2c rd failed=%d reg=%02x len=%d\n",
81 ret, reg, len);
82 ret = -EREMOTEIO;
85 return ret;
88 /* write multiple registers */
89 static int rtl2830_wr_regs(struct i2c_client *client, u16 reg, const u8 *val, int len)
91 struct rtl2830_dev *dev = i2c_get_clientdata(client);
92 int ret;
93 u8 reg2 = (reg >> 0) & 0xff;
94 u8 page = (reg >> 8) & 0xff;
96 /* switch bank if needed */
97 if (page != dev->page) {
98 ret = rtl2830_wr(client, 0x00, &page, 1);
99 if (ret)
100 return ret;
102 dev->page = page;
105 return rtl2830_wr(client, reg2, val, len);
108 /* read multiple registers */
109 static int rtl2830_rd_regs(struct i2c_client *client, u16 reg, u8 *val, int len)
111 struct rtl2830_dev *dev = i2c_get_clientdata(client);
112 int ret;
113 u8 reg2 = (reg >> 0) & 0xff;
114 u8 page = (reg >> 8) & 0xff;
116 /* switch bank if needed */
117 if (page != dev->page) {
118 ret = rtl2830_wr(client, 0x00, &page, 1);
119 if (ret)
120 return ret;
122 dev->page = page;
125 return rtl2830_rd(client, reg2, val, len);
128 /* read single register */
129 static int rtl2830_rd_reg(struct i2c_client *client, u16 reg, u8 *val)
131 return rtl2830_rd_regs(client, reg, val, 1);
134 /* write single register with mask */
135 static int rtl2830_wr_reg_mask(struct i2c_client *client, u16 reg, u8 val, u8 mask)
137 int ret;
138 u8 tmp;
140 /* no need for read if whole reg is written */
141 if (mask != 0xff) {
142 ret = rtl2830_rd_regs(client, reg, &tmp, 1);
143 if (ret)
144 return ret;
146 val &= mask;
147 tmp &= ~mask;
148 val |= tmp;
151 return rtl2830_wr_regs(client, reg, &val, 1);
154 /* read single register with mask */
155 static int rtl2830_rd_reg_mask(struct i2c_client *client, u16 reg, u8 *val, u8 mask)
157 int ret, i;
158 u8 tmp;
160 ret = rtl2830_rd_regs(client, reg, &tmp, 1);
161 if (ret)
162 return ret;
164 tmp &= mask;
166 /* find position of the first bit */
167 for (i = 0; i < 8; i++) {
168 if ((mask >> i) & 0x01)
169 break;
171 *val = tmp >> i;
173 return 0;
176 static int rtl2830_init(struct dvb_frontend *fe)
178 struct i2c_client *client = fe->demodulator_priv;
179 struct rtl2830_dev *dev = i2c_get_clientdata(client);
180 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
181 int ret, i;
182 struct rtl2830_reg_val_mask tab[] = {
183 {0x00d, 0x01, 0x03},
184 {0x00d, 0x10, 0x10},
185 {0x104, 0x00, 0x1e},
186 {0x105, 0x80, 0x80},
187 {0x110, 0x02, 0x03},
188 {0x110, 0x08, 0x0c},
189 {0x17b, 0x00, 0x40},
190 {0x17d, 0x05, 0x0f},
191 {0x17d, 0x50, 0xf0},
192 {0x18c, 0x08, 0x0f},
193 {0x18d, 0x00, 0xc0},
194 {0x188, 0x05, 0x0f},
195 {0x189, 0x00, 0xfc},
196 {0x2d5, 0x02, 0x02},
197 {0x2f1, 0x02, 0x06},
198 {0x2f1, 0x20, 0xf8},
199 {0x16d, 0x00, 0x01},
200 {0x1a6, 0x00, 0x80},
201 {0x106, dev->pdata->vtop, 0x3f},
202 {0x107, dev->pdata->krf, 0x3f},
203 {0x112, 0x28, 0xff},
204 {0x103, dev->pdata->agc_targ_val, 0xff},
205 {0x00a, 0x02, 0x07},
206 {0x140, 0x0c, 0x3c},
207 {0x140, 0x40, 0xc0},
208 {0x15b, 0x05, 0x07},
209 {0x15b, 0x28, 0x38},
210 {0x15c, 0x05, 0x07},
211 {0x15c, 0x28, 0x38},
212 {0x115, dev->pdata->spec_inv, 0x01},
213 {0x16f, 0x01, 0x07},
214 {0x170, 0x18, 0x38},
215 {0x172, 0x0f, 0x0f},
216 {0x173, 0x08, 0x38},
217 {0x175, 0x01, 0x07},
218 {0x176, 0x00, 0xc0},
221 for (i = 0; i < ARRAY_SIZE(tab); i++) {
222 ret = rtl2830_wr_reg_mask(client, tab[i].reg, tab[i].val,
223 tab[i].mask);
224 if (ret)
225 goto err;
228 ret = rtl2830_wr_regs(client, 0x18f, "\x28\x00", 2);
229 if (ret)
230 goto err;
232 ret = rtl2830_wr_regs(client, 0x195,
233 "\x04\x06\x0a\x12\x0a\x12\x1e\x28", 8);
234 if (ret)
235 goto err;
237 /* TODO: spec init */
239 /* soft reset */
240 ret = rtl2830_wr_reg_mask(client, 0x101, 0x04, 0x04);
241 if (ret)
242 goto err;
244 ret = rtl2830_wr_reg_mask(client, 0x101, 0x00, 0x04);
245 if (ret)
246 goto err;
248 /* init stats here in order signal app which stats are supported */
249 c->strength.len = 1;
250 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
251 c->cnr.len = 1;
252 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
253 c->post_bit_error.len = 1;
254 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
255 c->post_bit_count.len = 1;
256 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
257 /* start statistics polling */
258 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
260 dev->sleeping = false;
262 return ret;
263 err:
264 dev_dbg(&client->dev, "failed=%d\n", ret);
265 return ret;
268 static int rtl2830_sleep(struct dvb_frontend *fe)
270 struct i2c_client *client = fe->demodulator_priv;
271 struct rtl2830_dev *dev = i2c_get_clientdata(client);
273 dev->sleeping = true;
274 /* stop statistics polling */
275 cancel_delayed_work_sync(&dev->stat_work);
276 dev->fe_status = 0;
278 return 0;
281 static int rtl2830_get_tune_settings(struct dvb_frontend *fe,
282 struct dvb_frontend_tune_settings *s)
284 s->min_delay_ms = 500;
285 s->step_size = fe->ops.info.frequency_stepsize * 2;
286 s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
288 return 0;
291 static int rtl2830_set_frontend(struct dvb_frontend *fe)
293 struct i2c_client *client = fe->demodulator_priv;
294 struct rtl2830_dev *dev = i2c_get_clientdata(client);
295 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
296 int ret, i;
297 u64 num;
298 u8 buf[3], tmp;
299 u32 if_ctl, if_frequency;
300 static const u8 bw_params1[3][34] = {
302 0x1f, 0xf0, 0x1f, 0xf0, 0x1f, 0xfa, 0x00, 0x17, 0x00, 0x41,
303 0x00, 0x64, 0x00, 0x67, 0x00, 0x38, 0x1f, 0xde, 0x1f, 0x7a,
304 0x1f, 0x47, 0x1f, 0x7c, 0x00, 0x30, 0x01, 0x4b, 0x02, 0x82,
305 0x03, 0x73, 0x03, 0xcf, /* 6 MHz */
306 }, {
307 0x1f, 0xfa, 0x1f, 0xda, 0x1f, 0xc1, 0x1f, 0xb3, 0x1f, 0xca,
308 0x00, 0x07, 0x00, 0x4d, 0x00, 0x6d, 0x00, 0x40, 0x1f, 0xca,
309 0x1f, 0x4d, 0x1f, 0x2a, 0x1f, 0xb2, 0x00, 0xec, 0x02, 0x7e,
310 0x03, 0xd0, 0x04, 0x53, /* 7 MHz */
311 }, {
312 0x00, 0x10, 0x00, 0x0e, 0x1f, 0xf7, 0x1f, 0xc9, 0x1f, 0xa0,
313 0x1f, 0xa6, 0x1f, 0xec, 0x00, 0x4e, 0x00, 0x7d, 0x00, 0x3a,
314 0x1f, 0x98, 0x1f, 0x10, 0x1f, 0x40, 0x00, 0x75, 0x02, 0x5f,
315 0x04, 0x24, 0x04, 0xdb, /* 8 MHz */
318 static const u8 bw_params2[3][6] = {
319 {0xc3, 0x0c, 0x44, 0x33, 0x33, 0x30}, /* 6 MHz */
320 {0xb8, 0xe3, 0x93, 0x99, 0x99, 0x98}, /* 7 MHz */
321 {0xae, 0xba, 0xf3, 0x26, 0x66, 0x64}, /* 8 MHz */
324 dev_dbg(&client->dev, "frequency=%u bandwidth_hz=%u inversion=%u\n",
325 c->frequency, c->bandwidth_hz, c->inversion);
327 /* program tuner */
328 if (fe->ops.tuner_ops.set_params)
329 fe->ops.tuner_ops.set_params(fe);
331 switch (c->bandwidth_hz) {
332 case 6000000:
333 i = 0;
334 break;
335 case 7000000:
336 i = 1;
337 break;
338 case 8000000:
339 i = 2;
340 break;
341 default:
342 dev_err(&client->dev, "invalid bandwidth_hz %u\n",
343 c->bandwidth_hz);
344 return -EINVAL;
347 ret = rtl2830_wr_reg_mask(client, 0x008, i << 1, 0x06);
348 if (ret)
349 goto err;
351 /* program if frequency */
352 if (fe->ops.tuner_ops.get_if_frequency)
353 ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
354 else
355 ret = -EINVAL;
356 if (ret)
357 goto err;
359 num = if_frequency % dev->pdata->clk;
360 num *= 0x400000;
361 num = div_u64(num, dev->pdata->clk);
362 num = -num;
363 if_ctl = num & 0x3fffff;
364 dev_dbg(&client->dev, "if_frequency=%d if_ctl=%08x\n",
365 if_frequency, if_ctl);
367 ret = rtl2830_rd_reg_mask(client, 0x119, &tmp, 0xc0); /* b[7:6] */
368 if (ret)
369 goto err;
371 buf[0] = tmp << 6;
372 buf[0] |= (if_ctl >> 16) & 0x3f;
373 buf[1] = (if_ctl >> 8) & 0xff;
374 buf[2] = (if_ctl >> 0) & 0xff;
376 ret = rtl2830_wr_regs(client, 0x119, buf, 3);
377 if (ret)
378 goto err;
380 /* 1/2 split I2C write */
381 ret = rtl2830_wr_regs(client, 0x11c, &bw_params1[i][0], 17);
382 if (ret)
383 goto err;
385 /* 2/2 split I2C write */
386 ret = rtl2830_wr_regs(client, 0x12d, &bw_params1[i][17], 17);
387 if (ret)
388 goto err;
390 ret = rtl2830_wr_regs(client, 0x19d, bw_params2[i], 6);
391 if (ret)
392 goto err;
394 return ret;
395 err:
396 dev_dbg(&client->dev, "failed=%d\n", ret);
397 return ret;
400 static int rtl2830_get_frontend(struct dvb_frontend *fe)
402 struct i2c_client *client = fe->demodulator_priv;
403 struct rtl2830_dev *dev = i2c_get_clientdata(client);
404 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
405 int ret;
406 u8 buf[3];
408 if (dev->sleeping)
409 return 0;
411 ret = rtl2830_rd_regs(client, 0x33c, buf, 2);
412 if (ret)
413 goto err;
415 ret = rtl2830_rd_reg(client, 0x351, &buf[2]);
416 if (ret)
417 goto err;
419 dev_dbg(&client->dev, "TPS=%*ph\n", 3, buf);
421 switch ((buf[0] >> 2) & 3) {
422 case 0:
423 c->modulation = QPSK;
424 break;
425 case 1:
426 c->modulation = QAM_16;
427 break;
428 case 2:
429 c->modulation = QAM_64;
430 break;
433 switch ((buf[2] >> 2) & 1) {
434 case 0:
435 c->transmission_mode = TRANSMISSION_MODE_2K;
436 break;
437 case 1:
438 c->transmission_mode = TRANSMISSION_MODE_8K;
441 switch ((buf[2] >> 0) & 3) {
442 case 0:
443 c->guard_interval = GUARD_INTERVAL_1_32;
444 break;
445 case 1:
446 c->guard_interval = GUARD_INTERVAL_1_16;
447 break;
448 case 2:
449 c->guard_interval = GUARD_INTERVAL_1_8;
450 break;
451 case 3:
452 c->guard_interval = GUARD_INTERVAL_1_4;
453 break;
456 switch ((buf[0] >> 4) & 7) {
457 case 0:
458 c->hierarchy = HIERARCHY_NONE;
459 break;
460 case 1:
461 c->hierarchy = HIERARCHY_1;
462 break;
463 case 2:
464 c->hierarchy = HIERARCHY_2;
465 break;
466 case 3:
467 c->hierarchy = HIERARCHY_4;
468 break;
471 switch ((buf[1] >> 3) & 7) {
472 case 0:
473 c->code_rate_HP = FEC_1_2;
474 break;
475 case 1:
476 c->code_rate_HP = FEC_2_3;
477 break;
478 case 2:
479 c->code_rate_HP = FEC_3_4;
480 break;
481 case 3:
482 c->code_rate_HP = FEC_5_6;
483 break;
484 case 4:
485 c->code_rate_HP = FEC_7_8;
486 break;
489 switch ((buf[1] >> 0) & 7) {
490 case 0:
491 c->code_rate_LP = FEC_1_2;
492 break;
493 case 1:
494 c->code_rate_LP = FEC_2_3;
495 break;
496 case 2:
497 c->code_rate_LP = FEC_3_4;
498 break;
499 case 3:
500 c->code_rate_LP = FEC_5_6;
501 break;
502 case 4:
503 c->code_rate_LP = FEC_7_8;
504 break;
507 return 0;
508 err:
509 dev_dbg(&client->dev, "failed=%d\n", ret);
510 return ret;
513 static int rtl2830_read_status(struct dvb_frontend *fe, fe_status_t *status)
515 struct i2c_client *client = fe->demodulator_priv;
516 struct rtl2830_dev *dev = i2c_get_clientdata(client);
517 int ret;
518 u8 tmp;
520 *status = 0;
522 if (dev->sleeping)
523 return 0;
525 ret = rtl2830_rd_reg_mask(client, 0x351, &tmp, 0x78); /* [6:3] */
526 if (ret)
527 goto err;
529 if (tmp == 11) {
530 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
531 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
532 } else if (tmp == 10) {
533 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
534 FE_HAS_VITERBI;
537 dev->fe_status = *status;
539 return ret;
540 err:
541 dev_dbg(&client->dev, "failed=%d\n", ret);
542 return ret;
545 static int rtl2830_read_snr(struct dvb_frontend *fe, u16 *snr)
547 struct i2c_client *client = fe->demodulator_priv;
548 struct rtl2830_dev *dev = i2c_get_clientdata(client);
549 int ret, hierarchy, constellation;
550 u8 buf[2], tmp;
551 u16 tmp16;
552 #define CONSTELLATION_NUM 3
553 #define HIERARCHY_NUM 4
554 static const u32 snr_constant[CONSTELLATION_NUM][HIERARCHY_NUM] = {
555 {70705899, 70705899, 70705899, 70705899},
556 {82433173, 82433173, 87483115, 94445660},
557 {92888734, 92888734, 95487525, 99770748},
560 if (dev->sleeping)
561 return 0;
563 /* reports SNR in resolution of 0.1 dB */
565 ret = rtl2830_rd_reg(client, 0x33c, &tmp);
566 if (ret)
567 goto err;
569 constellation = (tmp >> 2) & 0x03; /* [3:2] */
570 if (constellation > CONSTELLATION_NUM - 1)
571 goto err;
573 hierarchy = (tmp >> 4) & 0x07; /* [6:4] */
574 if (hierarchy > HIERARCHY_NUM - 1)
575 goto err;
577 ret = rtl2830_rd_regs(client, 0x40c, buf, 2);
578 if (ret)
579 goto err;
581 tmp16 = buf[0] << 8 | buf[1];
583 if (tmp16)
584 *snr = (snr_constant[constellation][hierarchy] -
585 intlog10(tmp16)) / ((1 << 24) / 100);
586 else
587 *snr = 0;
589 return 0;
590 err:
591 dev_dbg(&client->dev, "failed=%d\n", ret);
592 return ret;
595 static int rtl2830_read_ber(struct dvb_frontend *fe, u32 *ber)
597 struct i2c_client *client = fe->demodulator_priv;
598 struct rtl2830_dev *dev = i2c_get_clientdata(client);
599 int ret;
600 u8 buf[2];
602 if (dev->sleeping)
603 return 0;
605 ret = rtl2830_rd_regs(client, 0x34e, buf, 2);
606 if (ret)
607 goto err;
609 *ber = buf[0] << 8 | buf[1];
611 return 0;
612 err:
613 dev_dbg(&client->dev, "failed=%d\n", ret);
614 return ret;
617 static int rtl2830_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
619 *ucblocks = 0;
621 return 0;
624 static int rtl2830_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
626 struct i2c_client *client = fe->demodulator_priv;
627 struct rtl2830_dev *dev = i2c_get_clientdata(client);
628 int ret;
629 u8 buf[2];
630 u16 if_agc_raw, if_agc;
632 if (dev->sleeping)
633 return 0;
635 ret = rtl2830_rd_regs(client, 0x359, buf, 2);
636 if (ret)
637 goto err;
639 if_agc_raw = (buf[0] << 8 | buf[1]) & 0x3fff;
641 if (if_agc_raw & (1 << 9))
642 if_agc = -(~(if_agc_raw - 1) & 0x1ff);
643 else
644 if_agc = if_agc_raw;
646 *strength = (u8)(55 - if_agc / 182);
647 *strength |= *strength << 8;
649 return 0;
650 err:
651 dev_dbg(&client->dev, "failed=%d\n", ret);
652 return ret;
655 static struct dvb_frontend_ops rtl2830_ops = {
656 .delsys = {SYS_DVBT},
657 .info = {
658 .name = "Realtek RTL2830 (DVB-T)",
659 .caps = FE_CAN_FEC_1_2 |
660 FE_CAN_FEC_2_3 |
661 FE_CAN_FEC_3_4 |
662 FE_CAN_FEC_5_6 |
663 FE_CAN_FEC_7_8 |
664 FE_CAN_FEC_AUTO |
665 FE_CAN_QPSK |
666 FE_CAN_QAM_16 |
667 FE_CAN_QAM_64 |
668 FE_CAN_QAM_AUTO |
669 FE_CAN_TRANSMISSION_MODE_AUTO |
670 FE_CAN_GUARD_INTERVAL_AUTO |
671 FE_CAN_HIERARCHY_AUTO |
672 FE_CAN_RECOVER |
673 FE_CAN_MUTE_TS
676 .init = rtl2830_init,
677 .sleep = rtl2830_sleep,
679 .get_tune_settings = rtl2830_get_tune_settings,
681 .set_frontend = rtl2830_set_frontend,
682 .get_frontend = rtl2830_get_frontend,
684 .read_status = rtl2830_read_status,
685 .read_snr = rtl2830_read_snr,
686 .read_ber = rtl2830_read_ber,
687 .read_ucblocks = rtl2830_read_ucblocks,
688 .read_signal_strength = rtl2830_read_signal_strength,
691 static void rtl2830_stat_work(struct work_struct *work)
693 struct rtl2830_dev *dev = container_of(work, struct rtl2830_dev, stat_work.work);
694 struct i2c_client *client = dev->client;
695 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
696 int ret, tmp;
697 u8 u8tmp, buf[2];
698 u16 u16tmp;
700 dev_dbg(&client->dev, "\n");
702 /* signal strength */
703 if (dev->fe_status & FE_HAS_SIGNAL) {
704 struct {signed int x:14; } s;
706 /* read IF AGC */
707 ret = rtl2830_rd_regs(client, 0x359, buf, 2);
708 if (ret)
709 goto err;
711 u16tmp = buf[0] << 8 | buf[1] << 0;
712 u16tmp &= 0x3fff; /* [13:0] */
713 tmp = s.x = u16tmp; /* 14-bit bin to 2 complement */
714 u16tmp = clamp_val(-4 * tmp + 32767, 0x0000, 0xffff);
716 dev_dbg(&client->dev, "IF AGC=%d\n", tmp);
718 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
719 c->strength.stat[0].uvalue = u16tmp;
720 } else {
721 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
724 /* CNR */
725 if (dev->fe_status & FE_HAS_VITERBI) {
726 unsigned hierarchy, constellation;
727 #define CONSTELLATION_NUM 3
728 #define HIERARCHY_NUM 4
729 static const u32 constant[CONSTELLATION_NUM][HIERARCHY_NUM] = {
730 {70705899, 70705899, 70705899, 70705899},
731 {82433173, 82433173, 87483115, 94445660},
732 {92888734, 92888734, 95487525, 99770748},
735 ret = rtl2830_rd_reg(client, 0x33c, &u8tmp);
736 if (ret)
737 goto err;
739 constellation = (u8tmp >> 2) & 0x03; /* [3:2] */
740 if (constellation > CONSTELLATION_NUM - 1)
741 goto err_schedule_delayed_work;
743 hierarchy = (u8tmp >> 4) & 0x07; /* [6:4] */
744 if (hierarchy > HIERARCHY_NUM - 1)
745 goto err_schedule_delayed_work;
747 ret = rtl2830_rd_regs(client, 0x40c, buf, 2);
748 if (ret)
749 goto err;
751 u16tmp = buf[0] << 8 | buf[1] << 0;
752 if (u16tmp)
753 tmp = (constant[constellation][hierarchy] -
754 intlog10(u16tmp)) / ((1 << 24) / 10000);
755 else
756 tmp = 0;
758 dev_dbg(&client->dev, "CNR raw=%u\n", u16tmp);
760 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
761 c->cnr.stat[0].svalue = tmp;
762 } else {
763 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
766 /* BER */
767 if (dev->fe_status & FE_HAS_LOCK) {
768 ret = rtl2830_rd_regs(client, 0x34e, buf, 2);
769 if (ret)
770 goto err;
772 u16tmp = buf[0] << 8 | buf[1] << 0;
773 dev->post_bit_error += u16tmp;
774 dev->post_bit_count += 1000000;
776 dev_dbg(&client->dev, "BER errors=%u total=1000000\n", u16tmp);
778 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
779 c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
780 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
781 c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
782 } else {
783 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
784 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
787 err_schedule_delayed_work:
788 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
789 return;
790 err:
791 dev_dbg(&client->dev, "failed=%d\n", ret);
795 * I2C gate/repeater logic
796 * We must use unlocked i2c_transfer() here because I2C lock is already taken
797 * by tuner driver. Gate is closed automatically after single I2C xfer.
799 static int rtl2830_select(struct i2c_adapter *adap, void *mux_priv, u32 chan_id)
801 struct i2c_client *client = mux_priv;
802 struct rtl2830_dev *dev = i2c_get_clientdata(client);
803 struct i2c_msg select_reg_page_msg[1] = {
805 .addr = client->addr,
806 .flags = 0,
807 .len = 2,
808 .buf = "\x00\x01",
811 struct i2c_msg gate_open_msg[1] = {
813 .addr = client->addr,
814 .flags = 0,
815 .len = 2,
816 .buf = "\x01\x08",
819 int ret;
821 /* select register page */
822 ret = __i2c_transfer(client->adapter, select_reg_page_msg, 1);
823 if (ret != 1) {
824 dev_warn(&client->dev, "i2c write failed %d\n", ret);
825 if (ret >= 0)
826 ret = -EREMOTEIO;
827 goto err;
830 dev->page = 1;
832 /* open tuner I2C repeater for 1 xfer, closes automatically */
833 ret = __i2c_transfer(client->adapter, gate_open_msg, 1);
834 if (ret != 1) {
835 dev_warn(&client->dev, "i2c write failed %d\n", ret);
836 if (ret >= 0)
837 ret = -EREMOTEIO;
838 goto err;
841 return 0;
842 err:
843 dev_dbg(&client->dev, "failed=%d\n", ret);
844 return ret;
847 static struct dvb_frontend *rtl2830_get_dvb_frontend(struct i2c_client *client)
849 struct rtl2830_dev *dev = i2c_get_clientdata(client);
851 dev_dbg(&client->dev, "\n");
853 return &dev->fe;
856 static struct i2c_adapter *rtl2830_get_i2c_adapter(struct i2c_client *client)
858 struct rtl2830_dev *dev = i2c_get_clientdata(client);
860 dev_dbg(&client->dev, "\n");
862 return dev->adapter;
865 static int rtl2830_probe(struct i2c_client *client,
866 const struct i2c_device_id *id)
868 struct rtl2830_platform_data *pdata = client->dev.platform_data;
869 struct rtl2830_dev *dev;
870 int ret;
871 u8 u8tmp;
873 dev_dbg(&client->dev, "\n");
875 if (pdata == NULL) {
876 ret = -EINVAL;
877 goto err;
880 /* allocate memory for the internal state */
881 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
882 if (dev == NULL) {
883 ret = -ENOMEM;
884 goto err;
887 /* setup the state */
888 i2c_set_clientdata(client, dev);
889 dev->client = client;
890 dev->pdata = client->dev.platform_data;
891 dev->sleeping = true;
892 INIT_DELAYED_WORK(&dev->stat_work, rtl2830_stat_work);
894 /* check if the demod is there */
895 ret = rtl2830_rd_reg(client, 0x000, &u8tmp);
896 if (ret)
897 goto err_kfree;
899 /* create muxed i2c adapter for tuner */
900 dev->adapter = i2c_add_mux_adapter(client->adapter, &client->dev,
901 client, 0, 0, 0, rtl2830_select, NULL);
902 if (dev->adapter == NULL) {
903 ret = -ENODEV;
904 goto err_kfree;
907 /* create dvb frontend */
908 memcpy(&dev->fe.ops, &rtl2830_ops, sizeof(dev->fe.ops));
909 dev->fe.demodulator_priv = client;
911 /* setup callbacks */
912 pdata->get_dvb_frontend = rtl2830_get_dvb_frontend;
913 pdata->get_i2c_adapter = rtl2830_get_i2c_adapter;
915 dev_info(&client->dev, "Realtek RTL2830 successfully attached\n");
917 return 0;
918 err_kfree:
919 kfree(dev);
920 err:
921 dev_dbg(&client->dev, "failed=%d\n", ret);
922 return ret;
925 static int rtl2830_remove(struct i2c_client *client)
927 struct rtl2830_dev *dev = i2c_get_clientdata(client);
929 dev_dbg(&client->dev, "\n");
931 i2c_del_mux_adapter(dev->adapter);
932 kfree(dev);
934 return 0;
937 static const struct i2c_device_id rtl2830_id_table[] = {
938 {"rtl2830", 0},
941 MODULE_DEVICE_TABLE(i2c, rtl2830_id_table);
943 static struct i2c_driver rtl2830_driver = {
944 .driver = {
945 .owner = THIS_MODULE,
946 .name = "rtl2830",
948 .probe = rtl2830_probe,
949 .remove = rtl2830_remove,
950 .id_table = rtl2830_id_table,
953 module_i2c_driver(rtl2830_driver);
955 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
956 MODULE_DESCRIPTION("Realtek RTL2830 DVB-T demodulator driver");
957 MODULE_LICENSE("GPL");