1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * NXP TDA18250 silicon tuner driver
8 #include "tda18250_priv.h"
9 #include <linux/regmap.h>
11 static const struct dvb_tuner_ops tda18250_ops;
13 static int tda18250_power_control(struct dvb_frontend *fe,
14 unsigned int power_state)
16 struct i2c_client *client = fe->tuner_priv;
17 struct tda18250_dev *dev = i2c_get_clientdata(client);
21 dev_dbg(&client->dev, "power state: %d", power_state);
23 switch (power_state) {
24 case TDA18250_POWER_NORMAL:
25 ret = regmap_write_bits(dev->regmap, R06_POWER2, 0x07, 0x00);
28 ret = regmap_write_bits(dev->regmap, R25_REF, 0xc0, 0xc0);
32 case TDA18250_POWER_STANDBY:
33 if (dev->loopthrough) {
34 ret = regmap_write_bits(dev->regmap,
38 ret = regmap_write_bits(dev->regmap,
39 R06_POWER2, 0x07, 0x02);
42 ret = regmap_write_bits(dev->regmap,
47 ret = regmap_write_bits(dev->regmap,
51 ret = regmap_write_bits(dev->regmap,
52 R06_POWER2, 0x07, 0x01);
55 ret = regmap_read(dev->regmap,
59 ret = regmap_write_bits(dev->regmap,
60 R0D_AGC12, 0x03, 0x03);
63 ret = regmap_write_bits(dev->regmap,
67 ret = regmap_write_bits(dev->regmap,
68 R0D_AGC12, 0x03, utmp & 0x03);
83 static int tda18250_wait_for_irq(struct dvb_frontend *fe,
84 int maxwait, int step, u8 irq)
86 struct i2c_client *client = fe->tuner_priv;
87 struct tda18250_dev *dev = i2c_get_clientdata(client);
89 unsigned long timeout;
94 timeout = jiffies + msecs_to_jiffies(maxwait);
95 while (!time_after(jiffies, timeout)) {
97 ret = regmap_read(dev->regmap, R08_IRQ1, &utmp);
100 if ((utmp & irq) == irq) {
107 dev_dbg(&client->dev, "waited IRQ (0x%02x) %d ms, triggered: %s", irq,
108 jiffies_to_msecs(jiffies) -
109 (jiffies_to_msecs(timeout) - maxwait),
110 triggered ? "true" : "false");
120 static int tda18250_init(struct dvb_frontend *fe)
122 struct i2c_client *client = fe->tuner_priv;
123 struct tda18250_dev *dev = i2c_get_clientdata(client);
126 /* default values for various regs */
127 static const u8 init_regs[][2] = {
140 { R1F_RF_BPF, 0x06 },
141 { R20_IR_MIX, 0xc6 },
142 { R21_IF_AGC, 0x00 },
147 { R31_IRQ_CTRL, 0x00 },
150 { R3C_RCCAL1, 0xa7 },
151 { R3F_IRCAL2, 0x85 },
152 { R40_IRCAL3, 0x87 },
153 { R41_IRCAL4, 0xc0 },
157 { R47_LNAPOL, 0x64 },
158 { R4B_XTALOSC1, 0x30 },
159 { R59_AGC2_UP2, 0x05 },
160 { R5B_AGC_AUTO, 0x07 },
161 { R5C_AGC_DEBUG, 0x00 },
164 /* crystal related regs depend on frequency */
165 static const u8 xtal_regs[][5] = {
166 /* reg: 4d 4e 4f 50 51 */
167 [TDA18250_XTAL_FREQ_16MHZ] = { 0x3e, 0x80, 0x50, 0x00, 0x20 },
168 [TDA18250_XTAL_FREQ_24MHZ] = { 0x5d, 0xc0, 0xec, 0x00, 0x18 },
169 [TDA18250_XTAL_FREQ_25MHZ] = { 0x61, 0xa8, 0xec, 0x80, 0x19 },
170 [TDA18250_XTAL_FREQ_27MHZ] = { 0x69, 0x78, 0x8d, 0x80, 0x1b },
171 [TDA18250_XTAL_FREQ_30MHZ] = { 0x75, 0x30, 0x8f, 0x00, 0x1e },
174 dev_dbg(&client->dev, "\n");
176 ret = tda18250_power_control(fe, TDA18250_POWER_NORMAL);
185 /* set initial register values */
186 for (i = 0; i < ARRAY_SIZE(init_regs); i++) {
187 ret = regmap_write(dev->regmap, init_regs[i][0],
193 /* set xtal related regs */
194 ret = regmap_bulk_write(dev->regmap, R4D_XTALFLX1,
195 xtal_regs[dev->xtal_freq], 5);
199 ret = regmap_write_bits(dev->regmap, R10_LT1, 0x80,
200 dev->loopthrough ? 0x00 : 0x80);
205 ret = regmap_write(dev->regmap, R0A_IRQ3, TDA18250_IRQ_HW_INIT);
210 ret = regmap_write(dev->regmap, R2A_MSM1, 0x70);
214 ret = regmap_write(dev->regmap, R2B_MSM2, 0x01);
218 ret = tda18250_wait_for_irq(fe, 500, 10, TDA18250_IRQ_HW_INIT);
222 /* tuner calibration */
223 ret = regmap_write(dev->regmap, R2A_MSM1, 0x02);
227 ret = regmap_write(dev->regmap, R2B_MSM2, 0x01);
231 ret = tda18250_wait_for_irq(fe, 500, 10, TDA18250_IRQ_CAL);
239 ret = regmap_write_bits(dev->regmap, R0C_AGC11, 0x80, 0x00);
245 dev_dbg(&client->dev, "failed=%d", ret);
249 static int tda18250_set_agc(struct dvb_frontend *fe)
251 struct i2c_client *client = fe->tuner_priv;
252 struct tda18250_dev *dev = i2c_get_clientdata(client);
253 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
257 dev_dbg(&client->dev, "\n");
259 ret = regmap_write_bits(dev->regmap, R1F_RF_BPF, 0x87, 0x06);
263 utmp = ((c->frequency < 100000000) &&
264 ((c->delivery_system == SYS_DVBC_ANNEX_A) ||
265 (c->delivery_system == SYS_DVBC_ANNEX_C)) &&
266 (c->bandwidth_hz == 6000000)) ? 0x80 : 0x00;
267 ret = regmap_write(dev->regmap, R5A_H3H5, utmp);
272 switch (c->delivery_system) {
278 default: /* DVB-C/QAM */
279 switch (c->bandwidth_hz) {
281 utmp = (c->frequency < 800000000) ? 6 : 4;
283 default: /* 7.935 and 8 MHz */
284 utmp = (c->frequency < 100000000) ? 2 : 3;
290 ret = regmap_write_bits(dev->regmap, R0C_AGC11, 0x07, utmp);
295 switch (c->delivery_system) {
299 utmp = (c->frequency < 320000000) ? 20 : 16;
300 utmp2 = (c->frequency < 320000000) ? 22 : 18;
302 default: /* DVB-C/QAM */
303 switch (c->bandwidth_hz) {
305 if (c->frequency < 600000000) {
308 } else if (c->frequency < 800000000) {
316 default: /* 7.935 and 8 MHz */
317 utmp = (c->frequency < 320000000) ? 16 : 18;
318 utmp2 = (c->frequency < 320000000) ? 18 : 20;
323 ret = regmap_write_bits(dev->regmap, R58_AGC2_UP1, 0x1f, utmp2+8);
326 ret = regmap_write_bits(dev->regmap, R13_AGC22, 0x1f, utmp);
329 ret = regmap_write_bits(dev->regmap, R14_AGC23, 0x1f, utmp2);
333 switch (c->delivery_system) {
339 default: /* DVB-C/QAM */
343 ret = regmap_write_bits(dev->regmap, R16_AGC25, 0xf8, utmp);
347 ret = regmap_write_bits(dev->regmap, R12_AGC21, 0x60,
348 (c->frequency > 800000000) ? 0x40 : 0x20);
353 switch (c->delivery_system) {
357 utmp = (c->frequency < 320000000) ? 5 : 7;
358 utmp2 = (c->frequency < 320000000) ? 10 : 12;
360 default: /* DVB-C/QAM */
365 ret = regmap_write(dev->regmap, R17_AGC31, (utmp << 4) | utmp2);
370 switch (c->delivery_system) {
374 if (c->bandwidth_hz == 8000000)
377 utmp = (c->frequency < 320000000) ? 0x04 : 0x02;
379 default: /* DVB-C/QAM */
380 if (c->bandwidth_hz == 6000000)
381 utmp = ((c->frequency > 172544000) &&
382 (c->frequency < 320000000)) ? 0x04 : 0x02;
383 else /* 7.935 and 8 MHz */
384 utmp = ((c->frequency > 320000000) &&
385 (c->frequency < 600000000)) ? 0x02 : 0x04;
388 ret = regmap_write_bits(dev->regmap, R20_IR_MIX, 0x06, utmp);
392 switch (c->delivery_system) {
398 default: /* DVB-C/QAM */
399 utmp = (c->frequency < 600000000) ? 0 : 3;
402 ret = regmap_write_bits(dev->regmap, R16_AGC25, 0x03, utmp);
407 switch (c->delivery_system) {
411 if (c->bandwidth_hz == 8000000)
414 default: /* DVB-C/QAM */
418 ret = regmap_write_bits(dev->regmap, R0F_AGC14, 0x3f, utmp);
424 dev_dbg(&client->dev, "failed=%d", ret);
428 static int tda18250_pll_calc(struct dvb_frontend *fe, u8 *rdiv,
431 struct i2c_client *client = fe->tuner_priv;
432 struct tda18250_dev *dev = i2c_get_clientdata(client);
433 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
435 unsigned int uval, exp, lopd, scale;
438 ret = regmap_read(dev->regmap, R34_MD1, &uval);
442 exp = (uval & 0x70) >> 4;
445 lopd = 1 << (exp - 1);
447 fvco = lopd * scale * ((c->frequency / 1000) + dev->if_frequency);
449 switch (dev->xtal_freq) {
450 case TDA18250_XTAL_FREQ_16MHZ:
453 *icp = (fvco < 6622000) ? 0x05 : 0x02;
455 case TDA18250_XTAL_FREQ_24MHZ:
456 case TDA18250_XTAL_FREQ_25MHZ:
459 *icp = (fvco < 6622000) ? 0x05 : 0x02;
461 case TDA18250_XTAL_FREQ_27MHZ:
462 if (fvco < 6643000) {
466 } else if (fvco < 6811000) {
476 case TDA18250_XTAL_FREQ_30MHZ:
479 *icp = (fvco < 6811000) ? 0x05 : 0x02;
485 dev_dbg(&client->dev,
486 "lopd=%d scale=%u fvco=%lu, rdiv=%d ndiv=%d icp=%d",
487 lopd, scale, fvco, *rdiv, *ndiv, *icp);
493 static int tda18250_set_params(struct dvb_frontend *fe)
495 struct i2c_client *client = fe->tuner_priv;
496 struct tda18250_dev *dev = i2c_get_clientdata(client);
497 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
513 static const u8 delsys_params[][16] = {
514 [REG] = { 0x22, 0x23, 0x24, 0x21, 0x0d, 0x0c, 0x0f, 0x14,
515 0x0e, 0x12, 0x58, 0x59, 0x1a, 0x19, 0x1e, 0x30 },
516 [MASK] = { 0x77, 0xff, 0xff, 0x87, 0xf0, 0x78, 0x07, 0xe0,
517 0x60, 0x0f, 0x60, 0x0f, 0x33, 0x30, 0x80, 0x06 },
518 [DVBT_6] = { 0x51, 0x03, 0x83, 0x82, 0x40, 0x48, 0x01, 0xe0,
519 0x60, 0x0f, 0x60, 0x05, 0x03, 0x10, 0x00, 0x04 },
520 [DVBT_7] = { 0x52, 0x03, 0x85, 0x82, 0x40, 0x48, 0x01, 0xe0,
521 0x60, 0x0f, 0x60, 0x05, 0x03, 0x10, 0x00, 0x04 },
522 [DVBT_8] = { 0x53, 0x03, 0x87, 0x82, 0x40, 0x48, 0x06, 0xe0,
523 0x60, 0x07, 0x60, 0x05, 0x03, 0x10, 0x00, 0x04 },
524 [DVBC_6] = { 0x32, 0x05, 0x86, 0x82, 0x50, 0x00, 0x06, 0x60,
525 0x40, 0x0e, 0x60, 0x05, 0x33, 0x10, 0x00, 0x04 },
526 [DVBC_8] = { 0x53, 0x03, 0x88, 0x82, 0x50, 0x00, 0x06, 0x60,
527 0x40, 0x0e, 0x60, 0x05, 0x33, 0x10, 0x00, 0x04 },
528 [ATSC] = { 0x51, 0x03, 0x83, 0x82, 0x40, 0x48, 0x01, 0xe0,
529 0x40, 0x0e, 0x60, 0x05, 0x03, 0x00, 0x80, 0x04 },
532 dev_dbg(&client->dev,
533 "delivery_system=%d frequency=%u bandwidth_hz=%u",
534 c->delivery_system, c->frequency, c->bandwidth_hz);
537 switch (c->delivery_system) {
540 if_khz = dev->if_atsc;
544 if (c->bandwidth_hz == 0) {
547 } else if (c->bandwidth_hz <= 6000000) {
549 if_khz = dev->if_dvbt_6;
550 } else if (c->bandwidth_hz <= 7000000) {
552 if_khz = dev->if_dvbt_7;
553 } else if (c->bandwidth_hz <= 8000000) {
555 if_khz = dev->if_dvbt_8;
561 case SYS_DVBC_ANNEX_A:
562 case SYS_DVBC_ANNEX_C:
563 if (c->bandwidth_hz == 0) {
566 } else if (c->bandwidth_hz <= 6000000) {
568 if_khz = dev->if_dvbc_6;
569 } else if (c->bandwidth_hz <= 8000000) {
571 if_khz = dev->if_dvbc_8;
579 dev_err(&client->dev, "unsupported delivery system=%d",
584 /* set delivery system dependent registers */
585 for (i = 0; i < 16; i++) {
586 ret = regmap_write_bits(dev->regmap, delsys_params[REG][i],
587 delsys_params[MASK][i], delsys_params[j][i]);
592 /* set IF if needed */
593 if (dev->if_frequency != if_khz) {
594 utmp = DIV_ROUND_CLOSEST(if_khz, 50);
595 ret = regmap_write(dev->regmap, R26_IF, utmp);
598 dev->if_frequency = if_khz;
599 dev_dbg(&client->dev, "set IF=%u kHz", if_khz);
603 ret = tda18250_set_agc(fe);
607 ret = regmap_write_bits(dev->regmap, R1A_AGCK, 0x03, 0x01);
611 ret = regmap_write_bits(dev->regmap, R14_AGC23, 0x40, 0x00);
616 buf[0] = ((c->frequency / 1000) >> 16) & 0xff;
617 buf[1] = ((c->frequency / 1000) >> 8) & 0xff;
618 buf[2] = ((c->frequency / 1000) >> 0) & 0xff;
619 ret = regmap_bulk_write(dev->regmap, R27_RF1, buf, 3);
623 ret = regmap_write(dev->regmap, R0A_IRQ3, TDA18250_IRQ_TUNE);
628 ret = regmap_write(dev->regmap, R2A_MSM1, 0x01);
632 ret = regmap_write(dev->regmap, R2B_MSM2, 0x01);
636 ret = tda18250_wait_for_irq(fe, 500, 10, TDA18250_IRQ_TUNE);
640 /* calc ndiv and rdiv */
641 ret = tda18250_pll_calc(fe, &buf[0], &buf[1], &buf[2]);
645 ret = regmap_write_bits(dev->regmap, R4F_XTALFLX3, 0xe0,
646 (buf[0] << 6) | (buf[1] << 5));
651 ret = regmap_write(dev->regmap, R0A_IRQ3, TDA18250_IRQ_TUNE);
655 ret = regmap_write_bits(dev->regmap, R46_CPUMP, 0x07, 0x00);
659 ret = regmap_write_bits(dev->regmap, R39_SD5, 0x03, 0x00);
664 ret = regmap_write(dev->regmap, R2A_MSM1, 0x01); /* tune */
668 ret = regmap_write(dev->regmap, R2B_MSM2, 0x01); /* go */
672 ret = tda18250_wait_for_irq(fe, 500, 10, TDA18250_IRQ_TUNE);
679 ret = regmap_write_bits(dev->regmap, R2B_MSM2, 0x04, 0x04);
686 ret = regmap_write_bits(dev->regmap, R1A_AGCK, 0x03, 0x03);
690 ret = regmap_write_bits(dev->regmap, R14_AGC23, 0x40, 0x40);
695 ret = regmap_write_bits(dev->regmap, R46_CPUMP, 0x07, buf[2]);
702 static int tda18250_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
704 struct i2c_client *client = fe->tuner_priv;
705 struct tda18250_dev *dev = i2c_get_clientdata(client);
707 *frequency = dev->if_frequency * 1000;
711 static int tda18250_sleep(struct dvb_frontend *fe)
713 struct i2c_client *client = fe->tuner_priv;
714 struct tda18250_dev *dev = i2c_get_clientdata(client);
717 dev_dbg(&client->dev, "\n");
720 ret = regmap_write_bits(dev->regmap, R0C_AGC11, 0x80, 0x00);
724 /* set if freq to 0 in order to make sure it's set after wake up */
725 dev->if_frequency = 0;
727 ret = tda18250_power_control(fe, TDA18250_POWER_STANDBY);
731 static const struct dvb_tuner_ops tda18250_ops = {
733 .name = "NXP TDA18250",
734 .frequency_min_hz = 42 * MHz,
735 .frequency_max_hz = 870 * MHz,
738 .init = tda18250_init,
739 .set_params = tda18250_set_params,
740 .get_if_frequency = tda18250_get_if_frequency,
741 .sleep = tda18250_sleep,
744 static int tda18250_probe(struct i2c_client *client)
746 struct tda18250_config *cfg = client->dev.platform_data;
747 struct dvb_frontend *fe = cfg->fe;
748 struct tda18250_dev *dev;
750 unsigned char chip_id[3];
752 /* some registers are always read from HW */
753 static const struct regmap_range tda18250_yes_ranges[] = {
754 regmap_reg_range(R05_POWER1, R0B_IRQ4),
755 regmap_reg_range(R21_IF_AGC, R21_IF_AGC),
756 regmap_reg_range(R2A_MSM1, R2B_MSM2),
757 regmap_reg_range(R2F_RSSI1, R31_IRQ_CTRL),
760 static const struct regmap_access_table tda18250_volatile_table = {
761 .yes_ranges = tda18250_yes_ranges,
762 .n_yes_ranges = ARRAY_SIZE(tda18250_yes_ranges),
765 static const struct regmap_config tda18250_regmap_config = {
768 .max_register = TDA18250_NUM_REGS - 1,
769 .volatile_table = &tda18250_volatile_table,
772 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
778 i2c_set_clientdata(client, dev);
781 dev->loopthrough = cfg->loopthrough;
782 if (cfg->xtal_freq < TDA18250_XTAL_FREQ_MAX) {
783 dev->xtal_freq = cfg->xtal_freq;
786 dev_err(&client->dev, "xtal_freq invalid=%d", cfg->xtal_freq);
789 dev->if_dvbt_6 = cfg->if_dvbt_6;
790 dev->if_dvbt_7 = cfg->if_dvbt_7;
791 dev->if_dvbt_8 = cfg->if_dvbt_8;
792 dev->if_dvbc_6 = cfg->if_dvbc_6;
793 dev->if_dvbc_8 = cfg->if_dvbc_8;
794 dev->if_atsc = cfg->if_atsc;
796 dev->if_frequency = 0;
799 dev->regmap = devm_regmap_init_i2c(client, &tda18250_regmap_config);
800 if (IS_ERR(dev->regmap)) {
801 ret = PTR_ERR(dev->regmap);
805 /* read the three chip ID registers */
806 regmap_bulk_read(dev->regmap, R00_ID1, &chip_id, 3);
807 dev_dbg(&client->dev, "chip_id=%02x:%02x:%02x",
808 chip_id[0], chip_id[1], chip_id[2]);
810 switch (chip_id[0]) {
822 if (chip_id[1] != 0x4a) {
827 switch (chip_id[2]) {
829 dev_info(&client->dev,
830 "NXP TDA18250AHN/%s successfully identified",
831 dev->slave ? "S" : "M");
834 dev_info(&client->dev,
835 "NXP TDA18250BHN/%s successfully identified",
836 dev->slave ? "S" : "M");
843 fe->tuner_priv = client;
844 memcpy(&fe->ops.tuner_ops, &tda18250_ops,
845 sizeof(struct dvb_tuner_ops));
847 /* put the tuner in standby */
848 tda18250_power_control(fe, TDA18250_POWER_STANDBY);
854 dev_dbg(&client->dev, "failed=%d", ret);
858 static void tda18250_remove(struct i2c_client *client)
860 struct tda18250_dev *dev = i2c_get_clientdata(client);
861 struct dvb_frontend *fe = dev->fe;
863 dev_dbg(&client->dev, "\n");
865 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
866 fe->tuner_priv = NULL;
870 static const struct i2c_device_id tda18250_id_table[] = {
874 MODULE_DEVICE_TABLE(i2c, tda18250_id_table);
876 static struct i2c_driver tda18250_driver = {
880 .probe_new = tda18250_probe,
881 .remove = tda18250_remove,
882 .id_table = tda18250_id_table,
885 module_i2c_driver(tda18250_driver);
887 MODULE_DESCRIPTION("NXP TDA18250 silicon tuner driver");
889 MODULE_LICENSE("GPL");