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 <linux/delay.h>
19 #include "si2168_priv.h"
21 static const struct dvb_frontend_ops si2168_ops;
23 /* Own I2C adapter locking is needed because of I2C gate logic. */
24 static int si2168_i2c_master_send_unlocked(const struct i2c_client *client,
25 const char *buf, int count)
28 struct i2c_msg msg = {
35 ret = __i2c_transfer(client->adapter, &msg, 1);
36 return (ret == 1) ? count : ret;
39 static int si2168_i2c_master_recv_unlocked(const struct i2c_client *client,
43 struct i2c_msg msg = {
50 ret = __i2c_transfer(client->adapter, &msg, 1);
51 return (ret == 1) ? count : ret;
54 /* execute firmware command */
55 static int si2168_cmd_execute_unlocked(struct i2c_client *client,
56 struct si2168_cmd *cmd)
59 unsigned long timeout;
62 /* write cmd and args for firmware */
63 ret = si2168_i2c_master_send_unlocked(client, cmd->args,
67 } else if (ret != cmd->wlen) {
74 /* wait cmd execution terminate */
76 timeout = jiffies + msecs_to_jiffies(TIMEOUT);
77 while (!time_after(jiffies, timeout)) {
78 ret = si2168_i2c_master_recv_unlocked(client, cmd->args,
82 } else if (ret != cmd->rlen) {
88 if ((cmd->args[0] >> 7) & 0x01)
92 dev_dbg(&client->dev, "cmd execution took %d ms\n",
93 jiffies_to_msecs(jiffies) -
94 (jiffies_to_msecs(timeout) - TIMEOUT));
97 if ((cmd->args[0] >> 6) & 0x01) {
102 if (!((cmd->args[0] >> 7) & 0x01)) {
110 dev_dbg(&client->dev, "failed=%d\n", ret);
114 static int si2168_cmd_execute(struct i2c_client *client, struct si2168_cmd *cmd)
118 i2c_lock_adapter(client->adapter);
119 ret = si2168_cmd_execute_unlocked(client, cmd);
120 i2c_unlock_adapter(client->adapter);
125 static int si2168_read_status(struct dvb_frontend *fe, enum fe_status *status)
127 struct i2c_client *client = fe->demodulator_priv;
128 struct si2168_dev *dev = i2c_get_clientdata(client);
129 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
131 struct si2168_cmd cmd;
140 switch (c->delivery_system) {
142 memcpy(cmd.args, "\xa0\x01", 2);
146 case SYS_DVBC_ANNEX_A:
147 memcpy(cmd.args, "\x90\x01", 2);
152 memcpy(cmd.args, "\x50\x01", 2);
161 ret = si2168_cmd_execute(client, &cmd);
165 switch ((cmd.args[2] >> 1) & 0x03) {
167 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
170 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
171 FE_HAS_SYNC | FE_HAS_LOCK;
175 dev->fe_status = *status;
177 if (*status & FE_HAS_LOCK) {
179 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
180 c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4;
183 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
186 dev_dbg(&client->dev, "status=%02x args=%*ph\n",
187 *status, cmd.rlen, cmd.args);
191 dev_dbg(&client->dev, "failed=%d\n", ret);
195 static int si2168_set_frontend(struct dvb_frontend *fe)
197 struct i2c_client *client = fe->demodulator_priv;
198 struct si2168_dev *dev = i2c_get_clientdata(client);
199 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
201 struct si2168_cmd cmd;
202 u8 bandwidth, delivery_system;
204 dev_dbg(&client->dev,
205 "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n",
206 c->delivery_system, c->modulation, c->frequency,
207 c->bandwidth_hz, c->symbol_rate, c->inversion,
215 switch (c->delivery_system) {
217 delivery_system = 0x20;
219 case SYS_DVBC_ANNEX_A:
220 delivery_system = 0x30;
223 delivery_system = 0x70;
230 if (c->bandwidth_hz == 0) {
233 } else if (c->bandwidth_hz <= 2000000)
235 else if (c->bandwidth_hz <= 5000000)
237 else if (c->bandwidth_hz <= 6000000)
239 else if (c->bandwidth_hz <= 7000000)
241 else if (c->bandwidth_hz <= 8000000)
243 else if (c->bandwidth_hz <= 9000000)
245 else if (c->bandwidth_hz <= 10000000)
251 if (fe->ops.tuner_ops.set_params) {
252 ret = fe->ops.tuner_ops.set_params(fe);
257 memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5);
260 ret = si2168_cmd_execute(client, &cmd);
264 /* that has no big effect */
265 if (c->delivery_system == SYS_DVBT)
266 memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6);
267 else if (c->delivery_system == SYS_DVBC_ANNEX_A)
268 memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6);
269 else if (c->delivery_system == SYS_DVBT2)
270 memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6);
273 ret = si2168_cmd_execute(client, &cmd);
277 if (c->delivery_system == SYS_DVBT2) {
280 cmd.args[1] = c->stream_id & 0xff;
281 cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1;
284 ret = si2168_cmd_execute(client, &cmd);
289 memcpy(cmd.args, "\x51\x03", 2);
292 ret = si2168_cmd_execute(client, &cmd);
296 memcpy(cmd.args, "\x12\x08\x04", 3);
299 ret = si2168_cmd_execute(client, &cmd);
303 memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6);
306 ret = si2168_cmd_execute(client, &cmd);
310 memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6);
313 ret = si2168_cmd_execute(client, &cmd);
317 memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6);
320 ret = si2168_cmd_execute(client, &cmd);
324 memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6);
325 cmd.args[4] = delivery_system | bandwidth;
328 ret = si2168_cmd_execute(client, &cmd);
332 /* set DVB-C symbol rate */
333 if (c->delivery_system == SYS_DVBC_ANNEX_A) {
334 memcpy(cmd.args, "\x14\x00\x02\x11", 4);
335 cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff;
336 cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
339 ret = si2168_cmd_execute(client, &cmd);
344 memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6);
347 ret = si2168_cmd_execute(client, &cmd);
351 memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6);
352 cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
355 ret = si2168_cmd_execute(client, &cmd);
359 memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6);
360 cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
363 ret = si2168_cmd_execute(client, &cmd);
367 memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6);
370 ret = si2168_cmd_execute(client, &cmd);
374 memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6);
377 ret = si2168_cmd_execute(client, &cmd);
381 memcpy(cmd.args, "\x85", 1);
384 ret = si2168_cmd_execute(client, &cmd);
388 dev->delivery_system = c->delivery_system;
392 dev_dbg(&client->dev, "failed=%d\n", ret);
396 static int si2168_init(struct dvb_frontend *fe)
398 struct i2c_client *client = fe->demodulator_priv;
399 struct si2168_dev *dev = i2c_get_clientdata(client);
400 int ret, len, remaining;
401 const struct firmware *fw;
403 struct si2168_cmd cmd;
404 unsigned int chip_id;
406 dev_dbg(&client->dev, "\n");
409 memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
412 ret = si2168_cmd_execute(client, &cmd);
416 if (dev->fw_loaded) {
418 memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
421 ret = si2168_cmd_execute(client, &cmd);
426 memcpy(cmd.args, "\x85", 1);
429 ret = si2168_cmd_execute(client, &cmd);
437 memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
440 ret = si2168_cmd_execute(client, &cmd);
444 /* query chip revision */
445 memcpy(cmd.args, "\x02", 1);
448 ret = si2168_cmd_execute(client, &cmd);
452 chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
455 #define SI2168_A20 ('A' << 24 | 68 << 16 | '2' << 8 | '0' << 0)
456 #define SI2168_A30 ('A' << 24 | 68 << 16 | '3' << 8 | '0' << 0)
457 #define SI2168_B40 ('B' << 24 | 68 << 16 | '4' << 8 | '0' << 0)
461 fw_name = SI2168_A20_FIRMWARE;
464 fw_name = SI2168_A30_FIRMWARE;
467 fw_name = SI2168_B40_FIRMWARE;
470 dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
471 cmd.args[2], cmd.args[1],
472 cmd.args[3], cmd.args[4]);
477 dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n",
478 cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
480 /* request the firmware, this will block and timeout */
481 ret = reject_firmware(&fw, fw_name, &client->dev);
483 /* fallback mechanism to handle old name for Si2168 B40 fw */
484 if (chip_id == SI2168_B40) {
485 fw_name = SI2168_B40_FIRMWARE_FALLBACK;
486 ret = reject_firmware(&fw, fw_name, &client->dev);
490 dev_notice(&client->dev,
491 "please install firmware file '%s'\n",
492 SI2168_B40_FIRMWARE);
494 dev_err(&client->dev,
495 "firmware file '%s' not found\n",
497 goto err_release_firmware;
501 dev_info(&client->dev, "downloading firmware from file '%s'\n",
504 if ((fw->size % 17 == 0) && (fw->data[0] > 5)) {
505 /* firmware is in the new format */
506 for (remaining = fw->size; remaining > 0; remaining -= 17) {
507 len = fw->data[fw->size - remaining];
508 if (len > SI2168_ARGLEN) {
512 memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
515 ret = si2168_cmd_execute(client, &cmd);
519 } else if (fw->size % 8 == 0) {
520 /* firmware is in the old format */
521 for (remaining = fw->size; remaining > 0; remaining -= 8) {
523 memcpy(cmd.args, &fw->data[fw->size - remaining], len);
526 ret = si2168_cmd_execute(client, &cmd);
531 /* bad or unknown firmware format */
536 dev_err(&client->dev, "firmware download failed %d\n", ret);
537 goto err_release_firmware;
540 release_firmware(fw);
542 memcpy(cmd.args, "\x01\x01", 2);
545 ret = si2168_cmd_execute(client, &cmd);
549 /* query firmware version */
550 memcpy(cmd.args, "\x11", 1);
553 ret = si2168_cmd_execute(client, &cmd);
557 dev_info(&client->dev, "firmware version: %c.%c.%d\n",
558 cmd.args[6], cmd.args[7], cmd.args[8]);
561 memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6);
562 cmd.args[4] |= dev->ts_mode;
563 if (dev->ts_clock_gapped)
567 ret = si2168_cmd_execute(client, &cmd);
571 dev->fw_loaded = true;
577 err_release_firmware:
578 release_firmware(fw);
580 dev_dbg(&client->dev, "failed=%d\n", ret);
584 static int si2168_sleep(struct dvb_frontend *fe)
586 struct i2c_client *client = fe->demodulator_priv;
587 struct si2168_dev *dev = i2c_get_clientdata(client);
589 struct si2168_cmd cmd;
591 dev_dbg(&client->dev, "\n");
595 memcpy(cmd.args, "\x13", 1);
598 ret = si2168_cmd_execute(client, &cmd);
604 dev_dbg(&client->dev, "failed=%d\n", ret);
608 static int si2168_get_tune_settings(struct dvb_frontend *fe,
609 struct dvb_frontend_tune_settings *s)
611 s->min_delay_ms = 900;
618 * We must use unlocked I2C I/O because I2C adapter lock is already taken
619 * by the caller (usually tuner driver).
621 static int si2168_select(struct i2c_adapter *adap, void *mux_priv, u32 chan)
623 struct i2c_client *client = mux_priv;
625 struct si2168_cmd cmd;
628 memcpy(cmd.args, "\xc0\x0d\x01", 3);
631 ret = si2168_cmd_execute_unlocked(client, &cmd);
637 dev_dbg(&client->dev, "failed=%d\n", ret);
641 static int si2168_deselect(struct i2c_adapter *adap, void *mux_priv, u32 chan)
643 struct i2c_client *client = mux_priv;
645 struct si2168_cmd cmd;
648 memcpy(cmd.args, "\xc0\x0d\x00", 3);
651 ret = si2168_cmd_execute_unlocked(client, &cmd);
657 dev_dbg(&client->dev, "failed=%d\n", ret);
661 static const struct dvb_frontend_ops si2168_ops = {
662 .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
664 .name = "Silicon Labs Si2168",
665 .symbol_rate_min = 1000000,
666 .symbol_rate_max = 7200000,
667 .caps = FE_CAN_FEC_1_2 |
680 FE_CAN_TRANSMISSION_MODE_AUTO |
681 FE_CAN_GUARD_INTERVAL_AUTO |
682 FE_CAN_HIERARCHY_AUTO |
684 FE_CAN_2G_MODULATION |
688 .get_tune_settings = si2168_get_tune_settings,
691 .sleep = si2168_sleep,
693 .set_frontend = si2168_set_frontend,
695 .read_status = si2168_read_status,
698 static int si2168_probe(struct i2c_client *client,
699 const struct i2c_device_id *id)
701 struct si2168_config *config = client->dev.platform_data;
702 struct si2168_dev *dev;
705 dev_dbg(&client->dev, "\n");
707 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
710 dev_err(&client->dev, "kzalloc() failed\n");
714 /* create mux i2c adapter for tuner */
715 dev->adapter = i2c_add_mux_adapter(client->adapter, &client->dev,
716 client, 0, 0, 0, si2168_select, si2168_deselect);
717 if (dev->adapter == NULL) {
722 /* create dvb_frontend */
723 memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
724 dev->fe.demodulator_priv = client;
725 *config->i2c_adapter = dev->adapter;
726 *config->fe = &dev->fe;
727 dev->ts_mode = config->ts_mode;
728 dev->ts_clock_inv = config->ts_clock_inv;
729 dev->ts_clock_gapped = config->ts_clock_gapped;
730 dev->fw_loaded = false;
732 i2c_set_clientdata(client, dev);
734 dev_info(&client->dev, "Silicon Labs Si2168 successfully attached\n");
739 dev_dbg(&client->dev, "failed=%d\n", ret);
743 static int si2168_remove(struct i2c_client *client)
745 struct si2168_dev *dev = i2c_get_clientdata(client);
747 dev_dbg(&client->dev, "\n");
749 i2c_del_mux_adapter(dev->adapter);
751 dev->fe.ops.release = NULL;
752 dev->fe.demodulator_priv = NULL;
759 static const struct i2c_device_id si2168_id_table[] = {
763 MODULE_DEVICE_TABLE(i2c, si2168_id_table);
765 static struct i2c_driver si2168_driver = {
769 .probe = si2168_probe,
770 .remove = si2168_remove,
771 .id_table = si2168_id_table,
774 module_i2c_driver(si2168_driver);
776 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
777 MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
778 MODULE_LICENSE("GPL");