2 * Silicon Labs Si2168 DVB-T/T2/C demodulator driver
4 * Copyright (C) 2014 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.
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 si2168 *s, struct si2168_cmd *cmd)
25 unsigned long timeout;
27 mutex_lock(&s->i2c_mutex);
30 /* write cmd and args for firmware */
31 ret = i2c_master_send(s->client, cmd->args, cmd->wlen);
33 goto err_mutex_unlock;
34 } else if (ret != cmd->wlen) {
36 goto err_mutex_unlock;
41 /* wait cmd execution terminate */
43 timeout = jiffies + msecs_to_jiffies(TIMEOUT);
44 while (!time_after(jiffies, timeout)) {
45 ret = i2c_master_recv(s->client, cmd->args, cmd->rlen);
47 goto err_mutex_unlock;
48 } else if (ret != cmd->rlen) {
50 goto err_mutex_unlock;
54 if ((cmd->args[0] >> 7) & 0x01)
58 dev_dbg(&s->client->dev, "cmd execution took %d ms\n",
59 jiffies_to_msecs(jiffies) -
60 (jiffies_to_msecs(timeout) - TIMEOUT));
62 if (!((cmd->args[0] >> 7) & 0x01)) {
64 goto err_mutex_unlock;
71 mutex_unlock(&s->i2c_mutex);
77 dev_dbg(&s->client->dev, "failed=%d\n", ret);
81 static int si2168_read_status(struct dvb_frontend *fe, fe_status_t *status)
83 struct si2168 *s = fe->demodulator_priv;
84 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
86 struct si2168_cmd cmd;
95 switch (c->delivery_system) {
97 memcpy(cmd.args, "\xa0\x01", 2);
101 case SYS_DVBC_ANNEX_A:
102 memcpy(cmd.args, "\x90\x01", 2);
107 memcpy(cmd.args, "\x50\x01", 2);
116 ret = si2168_cmd_execute(s, &cmd);
121 * Possible values seen, in order from strong signal to weak:
122 * 16 0001 0110 full lock
123 * 1e 0001 1110 partial lock
124 * 1a 0001 1010 partial lock
125 * 18 0001 1000 no lock
128 * [b4] statistics ready? Set in a few secs after lock is gained.
131 switch ((cmd.args[2] >> 1) & 0x03) {
133 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
136 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
137 FE_HAS_SYNC | FE_HAS_LOCK;
141 s->fe_status = *status;
143 if (*status & FE_HAS_LOCK) {
145 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
146 c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4;
149 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
152 dev_dbg(&s->client->dev, "status=%02x args=%*ph\n",
153 *status, cmd.rlen, cmd.args);
157 dev_dbg(&s->client->dev, "failed=%d\n", ret);
161 static int si2168_set_frontend(struct dvb_frontend *fe)
163 struct si2168 *s = fe->demodulator_priv;
164 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
166 struct si2168_cmd cmd;
167 u8 bandwidth, delivery_system;
169 dev_dbg(&s->client->dev,
170 "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u, stream_id=%d\n",
171 c->delivery_system, c->modulation,
172 c->frequency, c->bandwidth_hz, c->symbol_rate,
173 c->inversion, c->stream_id);
180 switch (c->delivery_system) {
182 delivery_system = 0x20;
184 case SYS_DVBC_ANNEX_A:
185 delivery_system = 0x30;
188 delivery_system = 0x70;
195 if (c->bandwidth_hz <= 5000000)
197 else if (c->bandwidth_hz <= 6000000)
199 else if (c->bandwidth_hz <= 7000000)
201 else if (c->bandwidth_hz <= 8000000)
203 else if (c->bandwidth_hz <= 9000000)
205 else if (c->bandwidth_hz <= 10000000)
211 if (fe->ops.tuner_ops.set_params) {
212 ret = fe->ops.tuner_ops.set_params(fe);
217 memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5);
220 ret = si2168_cmd_execute(s, &cmd);
224 /* that has no big effect */
225 if (c->delivery_system == SYS_DVBT)
226 memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6);
227 else if (c->delivery_system == SYS_DVBC_ANNEX_A)
228 memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6);
229 else if (c->delivery_system == SYS_DVBT2)
230 memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6);
233 ret = si2168_cmd_execute(s, &cmd);
237 if (c->delivery_system == SYS_DVBT2) {
240 cmd.args[1] = c->stream_id & 0xff;
241 cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1;
244 ret = si2168_cmd_execute(s, &cmd);
249 memcpy(cmd.args, "\x51\x03", 2);
252 ret = si2168_cmd_execute(s, &cmd);
256 memcpy(cmd.args, "\x12\x08\x04", 3);
259 ret = si2168_cmd_execute(s, &cmd);
263 memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6);
266 ret = si2168_cmd_execute(s, &cmd);
270 memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6);
273 ret = si2168_cmd_execute(s, &cmd);
277 memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6);
280 ret = si2168_cmd_execute(s, &cmd);
284 memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6);
285 cmd.args[4] = delivery_system | bandwidth;
288 ret = si2168_cmd_execute(s, &cmd);
292 /* set DVB-C symbol rate */
293 if (c->delivery_system == SYS_DVBC_ANNEX_A) {
294 memcpy(cmd.args, "\x14\x00\x02\x11", 4);
295 cmd.args[4] = (c->symbol_rate / 1000) & 0xff;
296 cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
299 ret = si2168_cmd_execute(s, &cmd);
304 memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6);
307 ret = si2168_cmd_execute(s, &cmd);
311 memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6);
312 cmd.args[5] |= s->ts_clock_inv ? 0x00 : 0x10;
315 ret = si2168_cmd_execute(s, &cmd);
319 memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6);
320 cmd.args[5] |= s->ts_clock_inv ? 0x00 : 0x10;
323 ret = si2168_cmd_execute(s, &cmd);
327 memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6);
330 ret = si2168_cmd_execute(s, &cmd);
334 memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6);
337 ret = si2168_cmd_execute(s, &cmd);
341 memcpy(cmd.args, "\x85", 1);
344 ret = si2168_cmd_execute(s, &cmd);
348 s->delivery_system = c->delivery_system;
352 dev_dbg(&s->client->dev, "failed=%d\n", ret);
356 static int si2168_init(struct dvb_frontend *fe)
358 struct si2168 *s = fe->demodulator_priv;
359 int ret, len, remaining;
360 const struct firmware *fw = NULL;
362 const unsigned int i2c_wr_max = 8;
363 struct si2168_cmd cmd;
364 unsigned int chip_id;
366 dev_dbg(&s->client->dev, "\n");
369 memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
372 ret = si2168_cmd_execute(s, &cmd);
378 memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
381 ret = si2168_cmd_execute(s, &cmd);
385 memcpy(cmd.args, "\x85", 1);
388 ret = si2168_cmd_execute(s, &cmd);
396 memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
399 ret = si2168_cmd_execute(s, &cmd);
403 /* query chip revision */
404 memcpy(cmd.args, "\x02", 1);
407 ret = si2168_cmd_execute(s, &cmd);
411 chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
414 #define SI2168_A20 ('A' << 24 | 68 << 16 | '2' << 8 | '0' << 0)
415 #define SI2168_A30 ('A' << 24 | 68 << 16 | '3' << 8 | '0' << 0)
416 #define SI2168_B40 ('B' << 24 | 68 << 16 | '4' << 8 | '0' << 0)
420 fw_file = SI2168_A20_FIRMWARE;
423 fw_file = SI2168_A30_FIRMWARE;
426 fw_file = SI2168_B40_FIRMWARE;
429 dev_err(&s->client->dev,
430 "unknown chip version Si21%d-%c%c%c\n",
431 cmd.args[2], cmd.args[1],
432 cmd.args[3], cmd.args[4]);
437 /* cold state - try to download firmware */
438 dev_info(&s->client->dev, "found a '%s' in cold state\n",
439 si2168_ops.info.name);
441 /* request the firmware, this will block and timeout */
442 ret = request_firmware(&fw, fw_file, &s->client->dev);
444 /* fallback mechanism to handle old name for Si2168 B40 fw */
445 if (chip_id == SI2168_B40) {
446 fw_file = SI2168_B40_FIRMWARE_FALLBACK;
447 ret = request_firmware(&fw, fw_file, &s->client->dev);
451 dev_notice(&s->client->dev,
452 "please install firmware file '%s'\n",
453 SI2168_B40_FIRMWARE);
455 dev_err(&s->client->dev,
456 "firmware file '%s' not found\n",
462 dev_info(&s->client->dev, "downloading firmware from file '%s'\n",
465 for (remaining = fw->size; remaining > 0; remaining -= i2c_wr_max) {
467 if (len > i2c_wr_max)
470 memcpy(cmd.args, &fw->data[fw->size - remaining], len);
473 ret = si2168_cmd_execute(s, &cmd);
475 dev_err(&s->client->dev,
476 "firmware download failed=%d\n",
482 release_firmware(fw);
485 memcpy(cmd.args, "\x01\x01", 2);
488 ret = si2168_cmd_execute(s, &cmd);
493 memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6);
494 cmd.args[4] |= s->ts_mode;
497 ret = si2168_cmd_execute(s, &cmd);
503 dev_info(&s->client->dev, "found a '%s' in warm state\n",
504 si2168_ops.info.name);
510 release_firmware(fw);
512 dev_dbg(&s->client->dev, "failed=%d\n", ret);
516 static int si2168_sleep(struct dvb_frontend *fe)
518 struct si2168 *s = fe->demodulator_priv;
520 struct si2168_cmd cmd;
522 dev_dbg(&s->client->dev, "\n");
526 memcpy(cmd.args, "\x13", 1);
529 ret = si2168_cmd_execute(s, &cmd);
535 dev_dbg(&s->client->dev, "failed=%d\n", ret);
539 static int si2168_get_tune_settings(struct dvb_frontend *fe,
540 struct dvb_frontend_tune_settings *s)
542 s->min_delay_ms = 900;
549 * We must use unlocked i2c_transfer() here because I2C lock is already taken
552 static int si2168_select(struct i2c_adapter *adap, void *mux_priv, u32 chan)
554 struct si2168 *s = mux_priv;
556 struct i2c_msg gate_open_msg = {
557 .addr = s->client->addr,
560 .buf = "\xc0\x0d\x01",
563 mutex_lock(&s->i2c_mutex);
565 /* open tuner I2C gate */
566 ret = __i2c_transfer(s->client->adapter, &gate_open_msg, 1);
568 dev_warn(&s->client->dev, "i2c write failed=%d\n", ret);
578 static int si2168_deselect(struct i2c_adapter *adap, void *mux_priv, u32 chan)
580 struct si2168 *s = mux_priv;
582 struct i2c_msg gate_close_msg = {
583 .addr = s->client->addr,
586 .buf = "\xc0\x0d\x00",
589 /* close tuner I2C gate */
590 ret = __i2c_transfer(s->client->adapter, &gate_close_msg, 1);
592 dev_warn(&s->client->dev, "i2c write failed=%d\n", ret);
599 mutex_unlock(&s->i2c_mutex);
604 static const struct dvb_frontend_ops si2168_ops = {
605 .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
607 .name = "Silicon Labs Si2168",
608 .caps = FE_CAN_FEC_1_2 |
621 FE_CAN_TRANSMISSION_MODE_AUTO |
622 FE_CAN_GUARD_INTERVAL_AUTO |
623 FE_CAN_HIERARCHY_AUTO |
625 FE_CAN_2G_MODULATION |
629 .get_tune_settings = si2168_get_tune_settings,
632 .sleep = si2168_sleep,
634 .set_frontend = si2168_set_frontend,
636 .read_status = si2168_read_status,
639 static int si2168_probe(struct i2c_client *client,
640 const struct i2c_device_id *id)
642 struct si2168_config *config = client->dev.platform_data;
646 dev_dbg(&client->dev, "\n");
648 s = kzalloc(sizeof(struct si2168), GFP_KERNEL);
651 dev_err(&client->dev, "kzalloc() failed\n");
656 mutex_init(&s->i2c_mutex);
658 /* create mux i2c adapter for tuner */
659 s->adapter = i2c_add_mux_adapter(client->adapter, &client->dev, s,
660 0, 0, 0, si2168_select, si2168_deselect);
661 if (s->adapter == NULL) {
666 /* create dvb_frontend */
667 memcpy(&s->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
668 s->fe.demodulator_priv = s;
670 *config->i2c_adapter = s->adapter;
671 *config->fe = &s->fe;
672 s->ts_mode = config->ts_mode;
673 s->ts_clock_inv = config->ts_clock_inv;
674 s->fw_loaded = false;
676 i2c_set_clientdata(client, s);
678 dev_info(&s->client->dev,
679 "Silicon Labs Si2168 successfully attached\n");
683 dev_dbg(&client->dev, "failed=%d\n", ret);
687 static int si2168_remove(struct i2c_client *client)
689 struct si2168 *s = i2c_get_clientdata(client);
691 dev_dbg(&client->dev, "\n");
693 i2c_del_mux_adapter(s->adapter);
695 s->fe.ops.release = NULL;
696 s->fe.demodulator_priv = NULL;
703 static const struct i2c_device_id si2168_id[] = {
707 MODULE_DEVICE_TABLE(i2c, si2168_id);
709 static struct i2c_driver si2168_driver = {
711 .owner = THIS_MODULE,
714 .probe = si2168_probe,
715 .remove = si2168_remove,
716 .id_table = si2168_id,
719 module_i2c_driver(si2168_driver);
721 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
722 MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
723 MODULE_LICENSE("GPL");
724 MODULE_FIRMWARE(SI2168_A20_FIRMWARE);
725 MODULE_FIRMWARE(SI2168_A30_FIRMWARE);
726 MODULE_FIRMWARE(SI2168_B40_FIRMWARE);