2 * Silicon Labs Si2168 DVB-T/T2/C demodulator driver
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.
17 #include "si2168_priv.h"
19 static const struct dvb_frontend_ops si2168_ops;
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));
63 if (!((cmd->args[0] >> 7) & 0x01)) {
65 goto err_mutex_unlock;
69 mutex_unlock(&dev->i2c_mutex);
73 mutex_unlock(&dev->i2c_mutex);
74 dev_dbg(&client->dev, "failed=%d\n", ret);
78 static int si2168_read_status(struct dvb_frontend *fe, fe_status_t *status)
80 struct i2c_client *client = fe->demodulator_priv;
81 struct si2168_dev *dev = i2c_get_clientdata(client);
82 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
84 struct si2168_cmd cmd;
93 switch (c->delivery_system) {
95 memcpy(cmd.args, "\xa0\x01", 2);
99 case SYS_DVBC_ANNEX_A:
100 memcpy(cmd.args, "\x90\x01", 2);
105 memcpy(cmd.args, "\x50\x01", 2);
114 ret = si2168_cmd_execute(client, &cmd);
118 switch ((cmd.args[2] >> 1) & 0x03) {
120 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
123 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
124 FE_HAS_SYNC | FE_HAS_LOCK;
128 dev->fe_status = *status;
130 if (*status & FE_HAS_LOCK) {
132 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
133 c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4;
136 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
139 dev_dbg(&client->dev, "status=%02x args=%*ph\n",
140 *status, cmd.rlen, cmd.args);
144 dev_dbg(&client->dev, "failed=%d\n", ret);
148 static int si2168_set_frontend(struct dvb_frontend *fe)
150 struct i2c_client *client = fe->demodulator_priv;
151 struct si2168_dev *dev = i2c_get_clientdata(client);
152 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
154 struct si2168_cmd cmd;
155 u8 bandwidth, delivery_system;
157 dev_dbg(&client->dev,
158 "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n",
159 c->delivery_system, c->modulation, c->frequency,
160 c->bandwidth_hz, c->symbol_rate, c->inversion,
168 switch (c->delivery_system) {
170 delivery_system = 0x20;
172 case SYS_DVBC_ANNEX_A:
173 delivery_system = 0x30;
176 delivery_system = 0x70;
183 if (c->bandwidth_hz == 0) {
186 } else if (c->bandwidth_hz <= 2000000)
188 else if (c->bandwidth_hz <= 5000000)
190 else if (c->bandwidth_hz <= 6000000)
192 else if (c->bandwidth_hz <= 7000000)
194 else if (c->bandwidth_hz <= 8000000)
196 else if (c->bandwidth_hz <= 9000000)
198 else if (c->bandwidth_hz <= 10000000)
204 if (fe->ops.tuner_ops.set_params) {
205 ret = fe->ops.tuner_ops.set_params(fe);
210 memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5);
213 ret = si2168_cmd_execute(client, &cmd);
217 /* that has no big effect */
218 if (c->delivery_system == SYS_DVBT)
219 memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6);
220 else if (c->delivery_system == SYS_DVBC_ANNEX_A)
221 memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6);
222 else if (c->delivery_system == SYS_DVBT2)
223 memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6);
226 ret = si2168_cmd_execute(client, &cmd);
230 if (c->delivery_system == SYS_DVBT2) {
233 cmd.args[1] = c->stream_id & 0xff;
234 cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1;
237 ret = si2168_cmd_execute(client, &cmd);
242 memcpy(cmd.args, "\x51\x03", 2);
245 ret = si2168_cmd_execute(client, &cmd);
249 memcpy(cmd.args, "\x12\x08\x04", 3);
252 ret = si2168_cmd_execute(client, &cmd);
256 memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6);
259 ret = si2168_cmd_execute(client, &cmd);
263 memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6);
266 ret = si2168_cmd_execute(client, &cmd);
270 memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6);
273 ret = si2168_cmd_execute(client, &cmd);
277 memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6);
278 cmd.args[4] = delivery_system | bandwidth;
281 ret = si2168_cmd_execute(client, &cmd);
285 /* set DVB-C symbol rate */
286 if (c->delivery_system == SYS_DVBC_ANNEX_A) {
287 memcpy(cmd.args, "\x14\x00\x02\x11", 4);
288 cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff;
289 cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
292 ret = si2168_cmd_execute(client, &cmd);
297 memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6);
300 ret = si2168_cmd_execute(client, &cmd);
304 memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6);
305 cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
308 ret = si2168_cmd_execute(client, &cmd);
312 memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6);
313 cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
316 ret = si2168_cmd_execute(client, &cmd);
320 memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6);
323 ret = si2168_cmd_execute(client, &cmd);
327 memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6);
330 ret = si2168_cmd_execute(client, &cmd);
334 memcpy(cmd.args, "\x85", 1);
337 ret = si2168_cmd_execute(client, &cmd);
341 dev->delivery_system = c->delivery_system;
345 dev_dbg(&client->dev, "failed=%d\n", ret);
349 static int si2168_init(struct dvb_frontend *fe)
351 struct i2c_client *client = fe->demodulator_priv;
352 struct si2168_dev *dev = i2c_get_clientdata(client);
353 int ret, len, remaining;
354 const struct firmware *fw;
356 struct si2168_cmd cmd;
357 unsigned int chip_id;
359 dev_dbg(&client->dev, "\n");
362 memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
365 ret = si2168_cmd_execute(client, &cmd);
369 if (dev->fw_loaded) {
371 memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
374 ret = si2168_cmd_execute(client, &cmd);
378 memcpy(cmd.args, "\x85", 1);
381 ret = si2168_cmd_execute(client, &cmd);
389 memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
392 ret = si2168_cmd_execute(client, &cmd);
396 /* query chip revision */
397 memcpy(cmd.args, "\x02", 1);
400 ret = si2168_cmd_execute(client, &cmd);
404 chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
407 #define SI2168_A20 ('A' << 24 | 68 << 16 | '2' << 8 | '0' << 0)
408 #define SI2168_A30 ('A' << 24 | 68 << 16 | '3' << 8 | '0' << 0)
409 #define SI2168_B40 ('B' << 24 | 68 << 16 | '4' << 8 | '0' << 0)
413 fw_name = SI2168_A20_FIRMWARE;
416 fw_name = SI2168_A30_FIRMWARE;
419 fw_name = SI2168_B40_FIRMWARE;
422 dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
423 cmd.args[2], cmd.args[1],
424 cmd.args[3], cmd.args[4]);
429 dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n",
430 cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
432 /* request the firmware, this will block and timeout */
433 ret = request_firmware(&fw, fw_name, &client->dev);
435 /* fallback mechanism to handle old name for Si2168 B40 fw */
436 if (chip_id == SI2168_B40) {
437 fw_name = SI2168_B40_FIRMWARE_FALLBACK;
438 ret = request_firmware(&fw, fw_name, &client->dev);
442 dev_notice(&client->dev,
443 "please install firmware file '%s'\n",
444 SI2168_B40_FIRMWARE);
446 dev_err(&client->dev,
447 "firmware file '%s' not found\n",
449 goto err_release_firmware;
453 dev_info(&client->dev, "downloading firmware from file '%s'\n",
456 if ((fw->size % 17 == 0) && (fw->data[0] > 5)) {
457 /* firmware is in the new format */
458 for (remaining = fw->size; remaining > 0; remaining -= 17) {
459 len = fw->data[fw->size - remaining];
460 memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
463 ret = si2168_cmd_execute(client, &cmd);
467 } else if (fw->size % 8 == 0) {
468 /* firmware is in the old format */
469 for (remaining = fw->size; remaining > 0; remaining -= 8) {
471 memcpy(cmd.args, &fw->data[fw->size - remaining], len);
474 ret = si2168_cmd_execute(client, &cmd);
479 /* bad or unknown firmware format */
484 dev_err(&client->dev, "firmware download failed %d\n", ret);
485 goto err_release_firmware;
488 release_firmware(fw);
490 memcpy(cmd.args, "\x01\x01", 2);
493 ret = si2168_cmd_execute(client, &cmd);
497 /* query firmware version */
498 memcpy(cmd.args, "\x11", 1);
501 ret = si2168_cmd_execute(client, &cmd);
505 dev_info(&client->dev, "firmware version: %c.%c.%d\n",
506 cmd.args[6], cmd.args[7], cmd.args[8]);
509 memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6);
510 cmd.args[4] |= dev->ts_mode;
513 ret = si2168_cmd_execute(client, &cmd);
517 dev->fw_loaded = true;
523 err_release_firmware:
524 release_firmware(fw);
526 dev_dbg(&client->dev, "failed=%d\n", ret);
530 static int si2168_sleep(struct dvb_frontend *fe)
532 struct i2c_client *client = fe->demodulator_priv;
533 struct si2168_dev *dev = i2c_get_clientdata(client);
535 struct si2168_cmd cmd;
537 dev_dbg(&client->dev, "\n");
541 memcpy(cmd.args, "\x13", 1);
544 ret = si2168_cmd_execute(client, &cmd);
550 dev_dbg(&client->dev, "failed=%d\n", ret);
554 static int si2168_get_tune_settings(struct dvb_frontend *fe,
555 struct dvb_frontend_tune_settings *s)
557 s->min_delay_ms = 900;
564 * We must use unlocked i2c_transfer() here because I2C lock is already taken
567 static int si2168_select(struct i2c_adapter *adap, void *mux_priv, u32 chan)
569 struct i2c_client *client = mux_priv;
570 struct si2168_dev *dev = i2c_get_clientdata(client);
572 struct i2c_msg gate_open_msg = {
573 .addr = client->addr,
576 .buf = "\xc0\x0d\x01",
579 mutex_lock(&dev->i2c_mutex);
581 /* open tuner I2C gate */
582 ret = __i2c_transfer(client->adapter, &gate_open_msg, 1);
584 dev_warn(&client->dev, "i2c write failed=%d\n", ret);
594 static int si2168_deselect(struct i2c_adapter *adap, void *mux_priv, u32 chan)
596 struct i2c_client *client = mux_priv;
597 struct si2168_dev *dev = i2c_get_clientdata(client);
599 struct i2c_msg gate_close_msg = {
600 .addr = client->addr,
603 .buf = "\xc0\x0d\x00",
606 /* close tuner I2C gate */
607 ret = __i2c_transfer(client->adapter, &gate_close_msg, 1);
609 dev_warn(&client->dev, "i2c write failed=%d\n", ret);
616 mutex_unlock(&dev->i2c_mutex);
621 static const struct dvb_frontend_ops si2168_ops = {
622 .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
624 .name = "Silicon Labs Si2168",
625 .symbol_rate_min = 1000000,
626 .symbol_rate_max = 7200000,
627 .caps = FE_CAN_FEC_1_2 |
640 FE_CAN_TRANSMISSION_MODE_AUTO |
641 FE_CAN_GUARD_INTERVAL_AUTO |
642 FE_CAN_HIERARCHY_AUTO |
644 FE_CAN_2G_MODULATION |
648 .get_tune_settings = si2168_get_tune_settings,
651 .sleep = si2168_sleep,
653 .set_frontend = si2168_set_frontend,
655 .read_status = si2168_read_status,
658 static int si2168_probe(struct i2c_client *client,
659 const struct i2c_device_id *id)
661 struct si2168_config *config = client->dev.platform_data;
662 struct si2168_dev *dev;
665 dev_dbg(&client->dev, "\n");
667 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
670 dev_err(&client->dev, "kzalloc() failed\n");
674 mutex_init(&dev->i2c_mutex);
676 /* create mux i2c adapter for tuner */
677 dev->adapter = i2c_add_mux_adapter(client->adapter, &client->dev,
678 client, 0, 0, 0, si2168_select, si2168_deselect);
679 if (dev->adapter == NULL) {
684 /* create dvb_frontend */
685 memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
686 dev->fe.demodulator_priv = client;
687 *config->i2c_adapter = dev->adapter;
688 *config->fe = &dev->fe;
689 dev->ts_mode = config->ts_mode;
690 dev->ts_clock_inv = config->ts_clock_inv;
691 dev->fw_loaded = false;
693 i2c_set_clientdata(client, dev);
695 dev_info(&client->dev, "Silicon Labs Si2168 successfully attached\n");
700 dev_dbg(&client->dev, "failed=%d\n", ret);
704 static int si2168_remove(struct i2c_client *client)
706 struct si2168_dev *dev = i2c_get_clientdata(client);
708 dev_dbg(&client->dev, "\n");
710 i2c_del_mux_adapter(dev->adapter);
712 dev->fe.ops.release = NULL;
713 dev->fe.demodulator_priv = NULL;
720 static const struct i2c_device_id si2168_id_table[] = {
724 MODULE_DEVICE_TABLE(i2c, si2168_id_table);
726 static struct i2c_driver si2168_driver = {
728 .owner = THIS_MODULE,
731 .probe = si2168_probe,
732 .remove = si2168_remove,
733 .id_table = si2168_id_table,
736 module_i2c_driver(si2168_driver);
739 MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
740 MODULE_LICENSE("GPL");
741 MODULE_FIRMWARE(SI2168_A20_FIRMWARE);
742 MODULE_FIRMWARE(SI2168_A30_FIRMWARE);
743 MODULE_FIRMWARE(SI2168_B40_FIRMWARE);