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 /* Our regmap is bypassing I2C adapter lock, thus we do it! */
21 static int rtl2830_bulk_write(struct i2c_client *client, unsigned int reg,
22 const void *val, size_t val_count)
24 struct rtl2830_dev *dev = i2c_get_clientdata(client);
27 i2c_lock_adapter(client->adapter);
28 ret = regmap_bulk_write(dev->regmap, reg, val, val_count);
29 i2c_unlock_adapter(client->adapter);
33 static int rtl2830_update_bits(struct i2c_client *client, unsigned int reg,
34 unsigned int mask, unsigned int val)
36 struct rtl2830_dev *dev = i2c_get_clientdata(client);
39 i2c_lock_adapter(client->adapter);
40 ret = regmap_update_bits(dev->regmap, reg, mask, val);
41 i2c_unlock_adapter(client->adapter);
45 static int rtl2830_bulk_read(struct i2c_client *client, unsigned int reg,
46 void *val, size_t val_count)
48 struct rtl2830_dev *dev = i2c_get_clientdata(client);
51 i2c_lock_adapter(client->adapter);
52 ret = regmap_bulk_read(dev->regmap, reg, val, val_count);
53 i2c_unlock_adapter(client->adapter);
57 static int rtl2830_init(struct dvb_frontend *fe)
59 struct i2c_client *client = fe->demodulator_priv;
60 struct rtl2830_dev *dev = i2c_get_clientdata(client);
61 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
63 struct rtl2830_reg_val_mask tab[] = {
82 {0x106, dev->pdata->vtop, 0x3f},
83 {0x107, dev->pdata->krf, 0x3f},
85 {0x103, dev->pdata->agc_targ_val, 0xff},
93 {0x115, dev->pdata->spec_inv, 0x01},
102 for (i = 0; i < ARRAY_SIZE(tab); i++) {
103 ret = rtl2830_update_bits(client, tab[i].reg, tab[i].mask,
109 ret = rtl2830_bulk_write(client, 0x18f, "\x28\x00", 2);
113 ret = rtl2830_bulk_write(client, 0x195,
114 "\x04\x06\x0a\x12\x0a\x12\x1e\x28", 8);
118 /* TODO: spec init */
121 ret = rtl2830_update_bits(client, 0x101, 0x04, 0x04);
125 ret = rtl2830_update_bits(client, 0x101, 0x04, 0x00);
129 /* init stats here in order signal app which stats are supported */
131 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
133 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
134 c->post_bit_error.len = 1;
135 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
136 c->post_bit_count.len = 1;
137 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
138 /* start statistics polling */
139 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
141 dev->sleeping = false;
145 dev_dbg(&client->dev, "failed=%d\n", ret);
149 static int rtl2830_sleep(struct dvb_frontend *fe)
151 struct i2c_client *client = fe->demodulator_priv;
152 struct rtl2830_dev *dev = i2c_get_clientdata(client);
154 dev->sleeping = true;
155 /* stop statistics polling */
156 cancel_delayed_work_sync(&dev->stat_work);
162 static int rtl2830_get_tune_settings(struct dvb_frontend *fe,
163 struct dvb_frontend_tune_settings *s)
165 s->min_delay_ms = 500;
166 s->step_size = fe->ops.info.frequency_stepsize * 2;
167 s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
172 static int rtl2830_set_frontend(struct dvb_frontend *fe)
174 struct i2c_client *client = fe->demodulator_priv;
175 struct rtl2830_dev *dev = i2c_get_clientdata(client);
176 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
180 u32 if_ctl, if_frequency;
181 static const u8 bw_params1[3][34] = {
183 0x1f, 0xf0, 0x1f, 0xf0, 0x1f, 0xfa, 0x00, 0x17, 0x00, 0x41,
184 0x00, 0x64, 0x00, 0x67, 0x00, 0x38, 0x1f, 0xde, 0x1f, 0x7a,
185 0x1f, 0x47, 0x1f, 0x7c, 0x00, 0x30, 0x01, 0x4b, 0x02, 0x82,
186 0x03, 0x73, 0x03, 0xcf, /* 6 MHz */
188 0x1f, 0xfa, 0x1f, 0xda, 0x1f, 0xc1, 0x1f, 0xb3, 0x1f, 0xca,
189 0x00, 0x07, 0x00, 0x4d, 0x00, 0x6d, 0x00, 0x40, 0x1f, 0xca,
190 0x1f, 0x4d, 0x1f, 0x2a, 0x1f, 0xb2, 0x00, 0xec, 0x02, 0x7e,
191 0x03, 0xd0, 0x04, 0x53, /* 7 MHz */
193 0x00, 0x10, 0x00, 0x0e, 0x1f, 0xf7, 0x1f, 0xc9, 0x1f, 0xa0,
194 0x1f, 0xa6, 0x1f, 0xec, 0x00, 0x4e, 0x00, 0x7d, 0x00, 0x3a,
195 0x1f, 0x98, 0x1f, 0x10, 0x1f, 0x40, 0x00, 0x75, 0x02, 0x5f,
196 0x04, 0x24, 0x04, 0xdb, /* 8 MHz */
199 static const u8 bw_params2[3][6] = {
200 {0xc3, 0x0c, 0x44, 0x33, 0x33, 0x30}, /* 6 MHz */
201 {0xb8, 0xe3, 0x93, 0x99, 0x99, 0x98}, /* 7 MHz */
202 {0xae, 0xba, 0xf3, 0x26, 0x66, 0x64}, /* 8 MHz */
205 dev_dbg(&client->dev, "frequency=%u bandwidth_hz=%u inversion=%u\n",
206 c->frequency, c->bandwidth_hz, c->inversion);
209 if (fe->ops.tuner_ops.set_params)
210 fe->ops.tuner_ops.set_params(fe);
212 switch (c->bandwidth_hz) {
223 dev_err(&client->dev, "invalid bandwidth_hz %u\n",
228 ret = rtl2830_update_bits(client, 0x008, 0x06, i << 1);
232 /* program if frequency */
233 if (fe->ops.tuner_ops.get_if_frequency)
234 ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
240 num = if_frequency % dev->pdata->clk;
242 num = div_u64(num, dev->pdata->clk);
244 if_ctl = num & 0x3fffff;
245 dev_dbg(&client->dev, "if_frequency=%d if_ctl=%08x\n",
246 if_frequency, if_ctl);
248 buf[0] = (if_ctl >> 16) & 0x3f;
249 buf[1] = (if_ctl >> 8) & 0xff;
250 buf[2] = (if_ctl >> 0) & 0xff;
252 ret = rtl2830_bulk_read(client, 0x119, &u8tmp, 1);
256 buf[0] |= u8tmp & 0xc0; /* [7:6] */
258 ret = rtl2830_bulk_write(client, 0x119, buf, 3);
262 /* 1/2 split I2C write */
263 ret = rtl2830_bulk_write(client, 0x11c, &bw_params1[i][0], 17);
267 /* 2/2 split I2C write */
268 ret = rtl2830_bulk_write(client, 0x12d, &bw_params1[i][17], 17);
272 ret = rtl2830_bulk_write(client, 0x19d, bw_params2[i], 6);
278 dev_dbg(&client->dev, "failed=%d\n", ret);
282 static int rtl2830_get_frontend(struct dvb_frontend *fe)
284 struct i2c_client *client = fe->demodulator_priv;
285 struct rtl2830_dev *dev = i2c_get_clientdata(client);
286 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
293 ret = rtl2830_bulk_read(client, 0x33c, buf, 2);
297 ret = rtl2830_bulk_read(client, 0x351, &buf[2], 1);
301 dev_dbg(&client->dev, "TPS=%*ph\n", 3, buf);
303 switch ((buf[0] >> 2) & 3) {
305 c->modulation = QPSK;
308 c->modulation = QAM_16;
311 c->modulation = QAM_64;
315 switch ((buf[2] >> 2) & 1) {
317 c->transmission_mode = TRANSMISSION_MODE_2K;
320 c->transmission_mode = TRANSMISSION_MODE_8K;
323 switch ((buf[2] >> 0) & 3) {
325 c->guard_interval = GUARD_INTERVAL_1_32;
328 c->guard_interval = GUARD_INTERVAL_1_16;
331 c->guard_interval = GUARD_INTERVAL_1_8;
334 c->guard_interval = GUARD_INTERVAL_1_4;
338 switch ((buf[0] >> 4) & 7) {
340 c->hierarchy = HIERARCHY_NONE;
343 c->hierarchy = HIERARCHY_1;
346 c->hierarchy = HIERARCHY_2;
349 c->hierarchy = HIERARCHY_4;
353 switch ((buf[1] >> 3) & 7) {
355 c->code_rate_HP = FEC_1_2;
358 c->code_rate_HP = FEC_2_3;
361 c->code_rate_HP = FEC_3_4;
364 c->code_rate_HP = FEC_5_6;
367 c->code_rate_HP = FEC_7_8;
371 switch ((buf[1] >> 0) & 7) {
373 c->code_rate_LP = FEC_1_2;
376 c->code_rate_LP = FEC_2_3;
379 c->code_rate_LP = FEC_3_4;
382 c->code_rate_LP = FEC_5_6;
385 c->code_rate_LP = FEC_7_8;
391 dev_dbg(&client->dev, "failed=%d\n", ret);
395 static int rtl2830_read_status(struct dvb_frontend *fe, enum fe_status *status)
397 struct i2c_client *client = fe->demodulator_priv;
398 struct rtl2830_dev *dev = i2c_get_clientdata(client);
407 ret = rtl2830_bulk_read(client, 0x351, &u8tmp, 1);
411 u8tmp = (u8tmp >> 3) & 0x0f; /* [6:3] */
413 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
414 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
415 } else if (u8tmp == 10) {
416 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
420 dev->fe_status = *status;
424 dev_dbg(&client->dev, "failed=%d\n", ret);
428 static int rtl2830_read_snr(struct dvb_frontend *fe, u16 *snr)
430 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
432 if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL)
433 *snr = div_s64(c->cnr.stat[0].svalue, 100);
440 static int rtl2830_read_ber(struct dvb_frontend *fe, u32 *ber)
442 struct i2c_client *client = fe->demodulator_priv;
443 struct rtl2830_dev *dev = i2c_get_clientdata(client);
445 *ber = (dev->post_bit_error - dev->post_bit_error_prev);
446 dev->post_bit_error_prev = dev->post_bit_error;
451 static int rtl2830_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
458 static int rtl2830_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
460 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
462 if (c->strength.stat[0].scale == FE_SCALE_RELATIVE)
463 *strength = c->strength.stat[0].uvalue;
470 static struct dvb_frontend_ops rtl2830_ops = {
471 .delsys = {SYS_DVBT},
473 .name = "Realtek RTL2830 (DVB-T)",
474 .caps = FE_CAN_FEC_1_2 |
484 FE_CAN_TRANSMISSION_MODE_AUTO |
485 FE_CAN_GUARD_INTERVAL_AUTO |
486 FE_CAN_HIERARCHY_AUTO |
491 .init = rtl2830_init,
492 .sleep = rtl2830_sleep,
494 .get_tune_settings = rtl2830_get_tune_settings,
496 .set_frontend = rtl2830_set_frontend,
497 .get_frontend = rtl2830_get_frontend,
499 .read_status = rtl2830_read_status,
500 .read_snr = rtl2830_read_snr,
501 .read_ber = rtl2830_read_ber,
502 .read_ucblocks = rtl2830_read_ucblocks,
503 .read_signal_strength = rtl2830_read_signal_strength,
506 static void rtl2830_stat_work(struct work_struct *work)
508 struct rtl2830_dev *dev = container_of(work, struct rtl2830_dev, stat_work.work);
509 struct i2c_client *client = dev->client;
510 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
515 dev_dbg(&client->dev, "\n");
517 /* signal strength */
518 if (dev->fe_status & FE_HAS_SIGNAL) {
519 struct {signed int x:14; } s;
522 ret = rtl2830_bulk_read(client, 0x359, buf, 2);
526 u16tmp = buf[0] << 8 | buf[1] << 0;
527 u16tmp &= 0x3fff; /* [13:0] */
528 tmp = s.x = u16tmp; /* 14-bit bin to 2 complement */
529 u16tmp = clamp_val(-4 * tmp + 32767, 0x0000, 0xffff);
531 dev_dbg(&client->dev, "IF AGC=%d\n", tmp);
533 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
534 c->strength.stat[0].uvalue = u16tmp;
536 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
540 if (dev->fe_status & FE_HAS_VITERBI) {
541 unsigned hierarchy, constellation;
542 #define CONSTELLATION_NUM 3
543 #define HIERARCHY_NUM 4
544 static const u32 constant[CONSTELLATION_NUM][HIERARCHY_NUM] = {
545 {70705899, 70705899, 70705899, 70705899},
546 {82433173, 82433173, 87483115, 94445660},
547 {92888734, 92888734, 95487525, 99770748},
550 ret = rtl2830_bulk_read(client, 0x33c, &u8tmp, 1);
554 constellation = (u8tmp >> 2) & 0x03; /* [3:2] */
555 if (constellation > CONSTELLATION_NUM - 1)
556 goto err_schedule_delayed_work;
558 hierarchy = (u8tmp >> 4) & 0x07; /* [6:4] */
559 if (hierarchy > HIERARCHY_NUM - 1)
560 goto err_schedule_delayed_work;
562 ret = rtl2830_bulk_read(client, 0x40c, buf, 2);
566 u16tmp = buf[0] << 8 | buf[1] << 0;
568 tmp = (constant[constellation][hierarchy] -
569 intlog10(u16tmp)) / ((1 << 24) / 10000);
573 dev_dbg(&client->dev, "CNR raw=%u\n", u16tmp);
575 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
576 c->cnr.stat[0].svalue = tmp;
578 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
582 if (dev->fe_status & FE_HAS_LOCK) {
583 ret = rtl2830_bulk_read(client, 0x34e, buf, 2);
587 u16tmp = buf[0] << 8 | buf[1] << 0;
588 dev->post_bit_error += u16tmp;
589 dev->post_bit_count += 1000000;
591 dev_dbg(&client->dev, "BER errors=%u total=1000000\n", u16tmp);
593 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
594 c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
595 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
596 c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
598 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
599 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
602 err_schedule_delayed_work:
603 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
606 dev_dbg(&client->dev, "failed=%d\n", ret);
609 static int rtl2830_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
611 struct i2c_client *client = fe->demodulator_priv;
615 dev_dbg(&client->dev, "onoff=%d\n", onoff);
617 /* enable / disable PID filter */
623 ret = rtl2830_update_bits(client, 0x061, 0x80, u8tmp);
629 dev_dbg(&client->dev, "failed=%d\n", ret);
633 static int rtl2830_pid_filter(struct dvb_frontend *fe, u8 index, u16 pid, int onoff)
635 struct i2c_client *client = fe->demodulator_priv;
636 struct rtl2830_dev *dev = i2c_get_clientdata(client);
640 dev_dbg(&client->dev, "index=%d pid=%04x onoff=%d\n",
643 /* skip invalid PIDs (0x2000) */
644 if (pid > 0x1fff || index > 32)
648 set_bit(index, &dev->filters);
650 clear_bit(index, &dev->filters);
652 /* enable / disable PIDs */
653 buf[0] = (dev->filters >> 0) & 0xff;
654 buf[1] = (dev->filters >> 8) & 0xff;
655 buf[2] = (dev->filters >> 16) & 0xff;
656 buf[3] = (dev->filters >> 24) & 0xff;
657 ret = rtl2830_bulk_write(client, 0x062, buf, 4);
662 buf[0] = (pid >> 8) & 0xff;
663 buf[1] = (pid >> 0) & 0xff;
664 ret = rtl2830_bulk_write(client, 0x066 + 2 * index, buf, 2);
670 dev_dbg(&client->dev, "failed=%d\n", ret);
675 * I2C gate/mux/repeater logic
676 * We must use unlocked __i2c_transfer() here (through regmap) because of I2C
677 * adapter lock is already taken by tuner driver.
678 * Gate is closed automatically after single I2C transfer.
680 static int rtl2830_select(struct i2c_adapter *adap, void *mux_priv, u32 chan_id)
682 struct i2c_client *client = mux_priv;
683 struct rtl2830_dev *dev = i2c_get_clientdata(client);
686 dev_dbg(&client->dev, "\n");
688 /* open I2C repeater for 1 transfer, closes automatically */
689 /* XXX: regmap_update_bits() does not lock I2C adapter */
690 ret = regmap_update_bits(dev->regmap, 0x101, 0x08, 0x08);
696 dev_dbg(&client->dev, "failed=%d\n", ret);
700 static struct dvb_frontend *rtl2830_get_dvb_frontend(struct i2c_client *client)
702 struct rtl2830_dev *dev = i2c_get_clientdata(client);
704 dev_dbg(&client->dev, "\n");
709 static struct i2c_adapter *rtl2830_get_i2c_adapter(struct i2c_client *client)
711 struct rtl2830_dev *dev = i2c_get_clientdata(client);
713 dev_dbg(&client->dev, "\n");
719 * We implement own I2C access routines for regmap in order to get manual access
720 * to I2C adapter lock, which is needed for I2C mux adapter.
722 static int rtl2830_regmap_read(void *context, const void *reg_buf,
723 size_t reg_size, void *val_buf, size_t val_size)
725 struct i2c_client *client = context;
727 struct i2c_msg msg[2] = {
729 .addr = client->addr,
732 .buf = (u8 *)reg_buf,
734 .addr = client->addr,
741 ret = __i2c_transfer(client->adapter, msg, 2);
743 dev_warn(&client->dev, "i2c reg read failed %d\n", ret);
751 static int rtl2830_regmap_write(void *context, const void *data, size_t count)
753 struct i2c_client *client = context;
755 struct i2c_msg msg[1] = {
757 .addr = client->addr,
764 ret = __i2c_transfer(client->adapter, msg, 1);
766 dev_warn(&client->dev, "i2c reg write failed %d\n", ret);
774 static int rtl2830_regmap_gather_write(void *context, const void *reg,
775 size_t reg_len, const void *val,
778 struct i2c_client *client = context;
781 struct i2c_msg msg[1] = {
783 .addr = client->addr,
790 buf[0] = *(u8 const *)reg;
791 memcpy(&buf[1], val, val_len);
793 ret = __i2c_transfer(client->adapter, msg, 1);
795 dev_warn(&client->dev, "i2c reg write failed %d\n", ret);
803 static int rtl2830_probe(struct i2c_client *client,
804 const struct i2c_device_id *id)
806 struct rtl2830_platform_data *pdata = client->dev.platform_data;
807 struct rtl2830_dev *dev;
810 static const struct regmap_bus regmap_bus = {
811 .read = rtl2830_regmap_read,
812 .write = rtl2830_regmap_write,
813 .gather_write = rtl2830_regmap_gather_write,
814 .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
816 static const struct regmap_range_cfg regmap_range_cfg[] = {
818 .selector_reg = 0x00,
819 .selector_mask = 0xff,
823 .range_min = 0 * 0x100,
824 .range_max = 5 * 0x100,
827 static const struct regmap_config regmap_config = {
830 .max_register = 5 * 0x100,
831 .ranges = regmap_range_cfg,
832 .num_ranges = ARRAY_SIZE(regmap_range_cfg),
835 dev_dbg(&client->dev, "\n");
842 /* allocate memory for the internal state */
843 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
849 /* setup the state */
850 i2c_set_clientdata(client, dev);
851 dev->client = client;
852 dev->pdata = client->dev.platform_data;
853 dev->sleeping = true;
854 INIT_DELAYED_WORK(&dev->stat_work, rtl2830_stat_work);
855 dev->regmap = regmap_init(&client->dev, ®map_bus, client,
857 if (IS_ERR(dev->regmap)) {
858 ret = PTR_ERR(dev->regmap);
862 /* check if the demod is there */
863 ret = rtl2830_bulk_read(client, 0x000, &u8tmp, 1);
865 goto err_regmap_exit;
867 /* create muxed i2c adapter for tuner */
868 dev->adapter = i2c_add_mux_adapter(client->adapter, &client->dev,
869 client, 0, 0, 0, rtl2830_select, NULL);
870 if (dev->adapter == NULL) {
872 goto err_regmap_exit;
875 /* create dvb frontend */
876 memcpy(&dev->fe.ops, &rtl2830_ops, sizeof(dev->fe.ops));
877 dev->fe.demodulator_priv = client;
879 /* setup callbacks */
880 pdata->get_dvb_frontend = rtl2830_get_dvb_frontend;
881 pdata->get_i2c_adapter = rtl2830_get_i2c_adapter;
882 pdata->pid_filter = rtl2830_pid_filter;
883 pdata->pid_filter_ctrl = rtl2830_pid_filter_ctrl;
885 dev_info(&client->dev, "Realtek RTL2830 successfully attached\n");
889 regmap_exit(dev->regmap);
893 dev_dbg(&client->dev, "failed=%d\n", ret);
897 static int rtl2830_remove(struct i2c_client *client)
899 struct rtl2830_dev *dev = i2c_get_clientdata(client);
901 dev_dbg(&client->dev, "\n");
903 i2c_del_mux_adapter(dev->adapter);
904 regmap_exit(dev->regmap);
910 static const struct i2c_device_id rtl2830_id_table[] = {
914 MODULE_DEVICE_TABLE(i2c, rtl2830_id_table);
916 static struct i2c_driver rtl2830_driver = {
920 .probe = rtl2830_probe,
921 .remove = rtl2830_remove,
922 .id_table = rtl2830_id_table,
925 module_i2c_driver(rtl2830_driver);
927 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
928 MODULE_DESCRIPTION("Realtek RTL2830 DVB-T demodulator driver");
929 MODULE_LICENSE("GPL");