1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Silicon Labs Si2168 DVB-T/T2/C demodulator driver
8 #include <linux/delay.h>
10 #include "si2168_priv.h"
12 static const struct dvb_frontend_ops si2168_ops;
14 static void cmd_init(struct si2168_cmd *cmd, const u8 *buf, int wlen, int rlen)
16 memcpy(cmd->args, buf, wlen);
21 /* execute firmware command */
22 static int si2168_cmd_execute(struct i2c_client *client, struct si2168_cmd *cmd)
24 struct si2168_dev *dev = i2c_get_clientdata(client);
26 unsigned long timeout;
28 mutex_lock(&dev->i2c_mutex);
31 /* write cmd and args for firmware */
32 ret = i2c_master_send(client, cmd->args, cmd->wlen);
34 goto err_mutex_unlock;
35 } else if (ret != cmd->wlen) {
37 goto err_mutex_unlock;
42 /* wait cmd execution terminate */
44 timeout = jiffies + msecs_to_jiffies(TIMEOUT);
45 while (!time_after(jiffies, timeout)) {
46 ret = i2c_master_recv(client, cmd->args, cmd->rlen);
48 goto err_mutex_unlock;
49 } else if (ret != cmd->rlen) {
51 goto err_mutex_unlock;
55 if ((cmd->args[0] >> 7) & 0x01)
59 dev_dbg(&client->dev, "cmd execution took %d ms\n",
60 jiffies_to_msecs(jiffies) -
61 (jiffies_to_msecs(timeout) - TIMEOUT));
64 if ((cmd->args[0] >> 6) & 0x01) {
66 goto err_mutex_unlock;
69 if (!((cmd->args[0] >> 7) & 0x01)) {
71 goto err_mutex_unlock;
75 mutex_unlock(&dev->i2c_mutex);
78 mutex_unlock(&dev->i2c_mutex);
79 dev_dbg(&client->dev, "failed=%d\n", ret);
83 static int si2168_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
85 struct i2c_client *client = fe->demodulator_priv;
86 struct si2168_dev *dev = i2c_get_clientdata(client);
87 struct si2168_cmd cmd;
90 dev_dbg(&client->dev, "%s acquire: %d\n", __func__, acquire);
92 /* set manual value */
93 if (dev->ts_mode & SI2168_TS_CLK_MANUAL) {
94 cmd_init(&cmd, "\x14\x00\x0d\x10\xe8\x03", 6, 4);
95 ret = si2168_cmd_execute(client, &cmd);
99 /* set TS_MODE property */
100 cmd_init(&cmd, "\x14\x00\x01\x10\x10\x00", 6, 4);
101 if (dev->ts_mode & SI2168_TS_CLK_MANUAL)
102 cmd.args[4] = SI2168_TS_CLK_MANUAL;
104 cmd.args[4] |= dev->ts_mode;
106 cmd.args[4] |= SI2168_TS_TRISTATE;
107 if (dev->ts_clock_gapped)
109 ret = si2168_cmd_execute(client, &cmd);
114 static int si2168_read_status(struct dvb_frontend *fe, enum fe_status *status)
116 struct i2c_client *client = fe->demodulator_priv;
117 struct si2168_dev *dev = i2c_get_clientdata(client);
118 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
120 unsigned int utmp, utmp1, utmp2;
121 struct si2168_cmd cmd;
130 switch (c->delivery_system) {
132 cmd_init(&cmd, "\xa0\x01", 2, 13);
134 case SYS_DVBC_ANNEX_A:
135 cmd_init(&cmd, "\x90\x01", 2, 9);
138 cmd_init(&cmd, "\x50\x01", 2, 14);
145 ret = si2168_cmd_execute(client, &cmd);
149 switch ((cmd.args[2] >> 1) & 0x03) {
151 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
154 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
155 FE_HAS_SYNC | FE_HAS_LOCK;
159 dev->fe_status = *status;
161 if (*status & FE_HAS_LOCK) {
163 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
164 c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4;
167 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
170 dev_dbg(&client->dev, "status=%02x args=%*ph\n",
171 *status, cmd.rlen, cmd.args);
174 if (*status & FE_HAS_VITERBI) {
175 cmd_init(&cmd, "\x82\x00", 2, 3);
176 ret = si2168_cmd_execute(client, &cmd);
181 * Firmware returns [0, 255] mantissa and [0, 8] exponent.
182 * Convert to DVB API: mantissa * 10^(8 - exponent) / 10^8
184 utmp = clamp(8 - cmd.args[1], 0, 8);
185 for (i = 0, utmp1 = 1; i < utmp; i++)
188 utmp1 = cmd.args[2] * utmp1;
189 utmp2 = 100000000; /* 10^8 */
191 dev_dbg(&client->dev,
192 "post_bit_error=%u post_bit_count=%u ber=%u*10^-%u\n",
193 utmp1, utmp2, cmd.args[2], cmd.args[1]);
195 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
196 c->post_bit_error.stat[0].uvalue += utmp1;
197 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
198 c->post_bit_count.stat[0].uvalue += utmp2;
200 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
201 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
205 if (*status & FE_HAS_SYNC) {
206 cmd_init(&cmd, "\x84\x01", 2, 3);
207 ret = si2168_cmd_execute(client, &cmd);
211 utmp1 = cmd.args[2] << 8 | cmd.args[1] << 0;
212 dev_dbg(&client->dev, "block_error=%u\n", utmp1);
214 /* Sometimes firmware returns bogus value */
218 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
219 c->block_error.stat[0].uvalue += utmp1;
221 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
226 dev_dbg(&client->dev, "failed=%d\n", ret);
230 static int si2168_set_frontend(struct dvb_frontend *fe)
232 struct i2c_client *client = fe->demodulator_priv;
233 struct si2168_dev *dev = i2c_get_clientdata(client);
234 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
236 struct si2168_cmd cmd;
237 u8 bandwidth, delivery_system;
239 dev_dbg(&client->dev,
240 "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n",
241 c->delivery_system, c->modulation, c->frequency,
242 c->bandwidth_hz, c->symbol_rate, c->inversion,
250 switch (c->delivery_system) {
252 delivery_system = 0x20;
254 case SYS_DVBC_ANNEX_A:
255 delivery_system = 0x30;
258 delivery_system = 0x70;
265 if (c->bandwidth_hz == 0) {
268 } else if (c->bandwidth_hz <= 2000000)
270 else if (c->bandwidth_hz <= 5000000)
272 else if (c->bandwidth_hz <= 6000000)
274 else if (c->bandwidth_hz <= 7000000)
276 else if (c->bandwidth_hz <= 8000000)
278 else if (c->bandwidth_hz <= 9000000)
280 else if (c->bandwidth_hz <= 10000000)
286 if (fe->ops.tuner_ops.set_params) {
287 ret = fe->ops.tuner_ops.set_params(fe);
292 cmd_init(&cmd, "\x88\x02\x02\x02\x02", 5, 5);
293 ret = si2168_cmd_execute(client, &cmd);
297 /* that has no big effect */
298 if (c->delivery_system == SYS_DVBT)
299 cmd_init(&cmd, "\x89\x21\x06\x11\xff\x98", 6, 3);
300 else if (c->delivery_system == SYS_DVBC_ANNEX_A)
301 cmd_init(&cmd, "\x89\x21\x06\x11\x89\xf0", 6, 3);
302 else if (c->delivery_system == SYS_DVBT2)
303 cmd_init(&cmd, "\x89\x21\x06\x11\x89\x20", 6, 3);
304 ret = si2168_cmd_execute(client, &cmd);
308 if (c->delivery_system == SYS_DVBT2) {
311 cmd.args[1] = c->stream_id & 0xff;
312 cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1;
315 ret = si2168_cmd_execute(client, &cmd);
320 cmd_init(&cmd, "\x51\x03", 2, 12);
321 ret = si2168_cmd_execute(client, &cmd);
325 cmd_init(&cmd, "\x12\x08\x04", 3, 3);
326 ret = si2168_cmd_execute(client, &cmd);
330 cmd_init(&cmd, "\x14\x00\x0c\x10\x12\x00", 6, 4);
331 ret = si2168_cmd_execute(client, &cmd);
335 cmd_init(&cmd, "\x14\x00\x06\x10\x24\x00", 6, 4);
336 ret = si2168_cmd_execute(client, &cmd);
340 cmd_init(&cmd, "\x14\x00\x07\x10\x00\x24", 6, 4);
341 ret = si2168_cmd_execute(client, &cmd);
345 cmd_init(&cmd, "\x14\x00\x0a\x10\x00\x00", 6, 4);
346 cmd.args[4] = delivery_system | bandwidth;
347 if (dev->spectral_inversion)
349 ret = si2168_cmd_execute(client, &cmd);
353 /* set DVB-C symbol rate */
354 if (c->delivery_system == SYS_DVBC_ANNEX_A) {
355 cmd_init(&cmd, "\x14\x00\x02\x11\x00\x00", 6, 4);
356 cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff;
357 cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
358 ret = si2168_cmd_execute(client, &cmd);
363 cmd_init(&cmd, "\x14\x00\x0f\x10\x10\x00", 6, 4);
364 ret = si2168_cmd_execute(client, &cmd);
368 cmd_init(&cmd, "\x14\x00\x09\x10\xe3\x08", 6, 4);
369 cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
370 ret = si2168_cmd_execute(client, &cmd);
374 cmd_init(&cmd, "\x14\x00\x08\x10\xd7\x05", 6, 4);
375 cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
376 ret = si2168_cmd_execute(client, &cmd);
380 cmd_init(&cmd, "\x14\x00\x01\x12\x00\x00", 6, 4);
381 ret = si2168_cmd_execute(client, &cmd);
385 cmd_init(&cmd, "\x14\x00\x01\x03\x0c\x00", 6, 4);
386 ret = si2168_cmd_execute(client, &cmd);
390 cmd_init(&cmd, "\x85", 1, 1);
391 ret = si2168_cmd_execute(client, &cmd);
395 dev->delivery_system = c->delivery_system;
398 ret = si2168_ts_bus_ctrl(fe, 1);
404 dev_dbg(&client->dev, "failed=%d\n", ret);
408 static int si2168_init(struct dvb_frontend *fe)
410 struct i2c_client *client = fe->demodulator_priv;
411 struct si2168_dev *dev = i2c_get_clientdata(client);
412 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
413 int ret, len, remaining;
414 const struct firmware *fw;
415 struct si2168_cmd cmd;
417 dev_dbg(&client->dev, "\n");
420 cmd_init(&cmd, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00",
422 ret = si2168_cmd_execute(client, &cmd);
428 cmd_init(&cmd, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8, 1);
429 ret = si2168_cmd_execute(client, &cmd);
434 cmd_init(&cmd, "\x85", 1, 1);
435 ret = si2168_cmd_execute(client, &cmd);
443 cmd_init(&cmd, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8, 1);
444 ret = si2168_cmd_execute(client, &cmd);
448 /* request the firmware, this will block and timeout */
449 ret = request_firmware(&fw, dev->firmware_name, &client->dev);
451 /* fallback mechanism to handle old name for Si2168 B40 fw */
452 if (dev->chip_id == SI2168_CHIP_ID_B40) {
453 dev->firmware_name = SI2168_B40_FIRMWARE_FALLBACK;
454 ret = request_firmware(&fw, dev->firmware_name,
459 dev_notice(&client->dev,
460 "please install firmware file '%s'\n",
461 SI2168_B40_FIRMWARE);
463 dev_err(&client->dev,
464 "firmware file '%s' not found\n",
466 goto err_release_firmware;
470 dev_info(&client->dev, "downloading firmware from file '%s'\n",
473 if ((fw->size % 17 == 0) && (fw->data[0] > 5)) {
474 /* firmware is in the new format */
475 for (remaining = fw->size; remaining > 0; remaining -= 17) {
476 len = fw->data[fw->size - remaining];
477 if (len > SI2168_ARGLEN) {
481 cmd_init(&cmd, &fw->data[(fw->size - remaining) + 1],
483 ret = si2168_cmd_execute(client, &cmd);
487 } else if (fw->size % 8 == 0) {
488 /* firmware is in the old format */
489 for (remaining = fw->size; remaining > 0; remaining -= 8) {
490 cmd_init(&cmd, &fw->data[fw->size - remaining], 8, 1);
491 ret = si2168_cmd_execute(client, &cmd);
496 /* bad or unknown firmware format */
501 dev_err(&client->dev, "firmware download failed %d\n", ret);
502 goto err_release_firmware;
505 release_firmware(fw);
507 cmd_init(&cmd, "\x01\x01", 2, 1);
508 ret = si2168_cmd_execute(client, &cmd);
512 /* query firmware version */
513 cmd_init(&cmd, "\x11", 1, 10);
514 ret = si2168_cmd_execute(client, &cmd);
518 dev->version = (cmd.args[9] + '@') << 24 | (cmd.args[6] - '0') << 16 |
519 (cmd.args[7] - '0') << 8 | (cmd.args[8]) << 0;
520 dev_info(&client->dev, "firmware version: %c %d.%d.%d\n",
521 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
522 dev->version >> 8 & 0xff, dev->version >> 0 & 0xff);
525 ret = si2168_ts_bus_ctrl(fe, 1);
531 /* Init stats here to indicate which stats are supported */
533 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
534 c->post_bit_error.len = 1;
535 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
536 c->post_bit_count.len = 1;
537 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
538 c->block_error.len = 1;
539 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
544 err_release_firmware:
545 release_firmware(fw);
547 dev_dbg(&client->dev, "failed=%d\n", ret);
551 static int si2168_sleep(struct dvb_frontend *fe)
553 struct i2c_client *client = fe->demodulator_priv;
554 struct si2168_dev *dev = i2c_get_clientdata(client);
556 struct si2168_cmd cmd;
558 dev_dbg(&client->dev, "\n");
562 /* tri-state data bus */
563 ret = si2168_ts_bus_ctrl(fe, 0);
567 /* Firmware later than B 4.0-11 loses warm state during sleep */
568 if (dev->version > ('B' << 24 | 4 << 16 | 0 << 8 | 11 << 0))
571 cmd_init(&cmd, "\x13", 1, 0);
572 ret = si2168_cmd_execute(client, &cmd);
578 dev_dbg(&client->dev, "failed=%d\n", ret);
582 static int si2168_get_tune_settings(struct dvb_frontend *fe,
583 struct dvb_frontend_tune_settings *s)
585 s->min_delay_ms = 900;
590 static int si2168_select(struct i2c_mux_core *muxc, u32 chan)
592 struct i2c_client *client = i2c_mux_priv(muxc);
594 struct si2168_cmd cmd;
597 cmd_init(&cmd, "\xc0\x0d\x01", 3, 0);
598 ret = si2168_cmd_execute(client, &cmd);
604 dev_dbg(&client->dev, "failed=%d\n", ret);
608 static int si2168_deselect(struct i2c_mux_core *muxc, u32 chan)
610 struct i2c_client *client = i2c_mux_priv(muxc);
612 struct si2168_cmd cmd;
615 cmd_init(&cmd, "\xc0\x0d\x00", 3, 0);
616 ret = si2168_cmd_execute(client, &cmd);
622 dev_dbg(&client->dev, "failed=%d\n", ret);
626 static const struct dvb_frontend_ops si2168_ops = {
627 .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
629 .name = "Silicon Labs Si2168",
630 .frequency_min_hz = 48 * MHz,
631 .frequency_max_hz = 870 * MHz,
632 .frequency_stepsize_hz = 62500,
633 .symbol_rate_min = 1000000,
634 .symbol_rate_max = 7200000,
635 .caps = FE_CAN_FEC_1_2 |
648 FE_CAN_TRANSMISSION_MODE_AUTO |
649 FE_CAN_GUARD_INTERVAL_AUTO |
650 FE_CAN_HIERARCHY_AUTO |
652 FE_CAN_2G_MODULATION |
656 .get_tune_settings = si2168_get_tune_settings,
659 .sleep = si2168_sleep,
661 .set_frontend = si2168_set_frontend,
663 .read_status = si2168_read_status,
666 static int si2168_probe(struct i2c_client *client,
667 const struct i2c_device_id *id)
669 struct si2168_config *config = client->dev.platform_data;
670 struct si2168_dev *dev;
672 struct si2168_cmd cmd;
674 dev_dbg(&client->dev, "\n");
676 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
682 i2c_set_clientdata(client, dev);
683 mutex_init(&dev->i2c_mutex);
686 cmd_init(&cmd, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00",
688 ret = si2168_cmd_execute(client, &cmd);
693 cmd_init(&cmd, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8, 1);
694 ret = si2168_cmd_execute(client, &cmd);
698 /* Query chip revision */
699 cmd_init(&cmd, "\x02", 1, 13);
700 ret = si2168_cmd_execute(client, &cmd);
704 dev->chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 |
705 cmd.args[3] << 8 | cmd.args[4] << 0;
707 switch (dev->chip_id) {
708 case SI2168_CHIP_ID_A20:
709 dev->firmware_name = SI2168_A20_FIRMWARE;
711 case SI2168_CHIP_ID_A30:
712 dev->firmware_name = SI2168_A30_FIRMWARE;
714 case SI2168_CHIP_ID_B40:
715 dev->firmware_name = SI2168_B40_FIRMWARE;
717 case SI2168_CHIP_ID_D60:
718 dev->firmware_name = SI2168_D60_FIRMWARE;
721 dev_dbg(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
722 cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
727 dev->version = (cmd.args[1]) << 24 | (cmd.args[3] - '0') << 16 |
728 (cmd.args[4] - '0') << 8 | (cmd.args[5]) << 0;
730 /* create mux i2c adapter for tuner */
731 dev->muxc = i2c_mux_alloc(client->adapter, &client->dev,
732 1, 0, I2C_MUX_LOCKED,
733 si2168_select, si2168_deselect);
738 dev->muxc->priv = client;
739 ret = i2c_mux_add_adapter(dev->muxc, 0, 0, 0);
743 /* create dvb_frontend */
744 memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
745 dev->fe.demodulator_priv = client;
746 *config->i2c_adapter = dev->muxc->adapter[0];
747 *config->fe = &dev->fe;
748 dev->ts_mode = config->ts_mode;
749 dev->ts_clock_inv = config->ts_clock_inv;
750 dev->ts_clock_gapped = config->ts_clock_gapped;
751 dev->spectral_inversion = config->spectral_inversion;
753 dev_info(&client->dev, "Silicon Labs Si2168-%c%d%d successfully identified\n",
754 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
755 dev->version >> 8 & 0xff);
756 dev_info(&client->dev, "firmware version: %c %d.%d.%d\n",
757 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
758 dev->version >> 8 & 0xff, dev->version >> 0 & 0xff);
764 dev_warn(&client->dev, "probe failed = %d\n", ret);
768 static int si2168_remove(struct i2c_client *client)
770 struct si2168_dev *dev = i2c_get_clientdata(client);
772 dev_dbg(&client->dev, "\n");
774 i2c_mux_del_adapters(dev->muxc);
776 dev->fe.ops.release = NULL;
777 dev->fe.demodulator_priv = NULL;
784 static const struct i2c_device_id si2168_id_table[] = {
788 MODULE_DEVICE_TABLE(i2c, si2168_id_table);
790 static struct i2c_driver si2168_driver = {
793 .suppress_bind_attrs = true,
795 .probe = si2168_probe,
796 .remove = si2168_remove,
797 .id_table = si2168_id_table,
800 module_i2c_driver(si2168_driver);
803 MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
804 MODULE_LICENSE("GPL");
805 MODULE_FIRMWARE(SI2168_A20_FIRMWARE);
806 MODULE_FIRMWARE(SI2168_A30_FIRMWARE);
807 MODULE_FIRMWARE(SI2168_B40_FIRMWARE);
808 MODULE_FIRMWARE(SI2168_D60_FIRMWARE);