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, "%s: 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;
72 mutex_unlock(&s->i2c_mutex);
78 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
82 static int si2168_read_status(struct dvb_frontend *fe, fe_status_t *status)
84 struct si2168 *s = fe->demodulator_priv;
85 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
87 struct si2168_cmd cmd;
96 switch (c->delivery_system) {
98 memcpy(cmd.args, "\xa0\x01", 2);
102 case SYS_DVBC_ANNEX_A:
103 memcpy(cmd.args, "\x90\x01", 2);
108 memcpy(cmd.args, "\x50\x01", 2);
117 ret = si2168_cmd_execute(s, &cmd);
122 * Possible values seen, in order from strong signal to weak:
123 * 16 0001 0110 full lock
124 * 1e 0001 1110 partial lock
125 * 1a 0001 1010 partial lock
126 * 18 0001 1000 no lock
129 * [b4] statistics ready? Set in a few secs after lock is gained.
132 switch ((cmd.args[2] >> 1) & 0x03) {
134 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
137 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
138 FE_HAS_SYNC | FE_HAS_LOCK;
142 s->fe_status = *status;
144 if (*status & FE_HAS_LOCK) {
146 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
147 c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4;
150 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
153 dev_dbg(&s->client->dev, "%s: status=%02x args=%*ph\n",
154 __func__, *status, cmd.rlen, cmd.args);
158 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
162 static int si2168_set_frontend(struct dvb_frontend *fe)
164 struct si2168 *s = fe->demodulator_priv;
165 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
167 struct si2168_cmd cmd;
168 u8 bandwidth, delivery_system;
170 dev_dbg(&s->client->dev,
171 "%s: delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u\n",
172 __func__, c->delivery_system, c->modulation,
173 c->frequency, c->bandwidth_hz, c->symbol_rate,
181 switch (c->delivery_system) {
183 delivery_system = 0x20;
185 case SYS_DVBC_ANNEX_A:
186 delivery_system = 0x30;
189 delivery_system = 0x70;
196 if (c->bandwidth_hz <= 5000000)
198 else if (c->bandwidth_hz <= 6000000)
200 else if (c->bandwidth_hz <= 7000000)
202 else if (c->bandwidth_hz <= 8000000)
204 else if (c->bandwidth_hz <= 9000000)
206 else if (c->bandwidth_hz <= 10000000)
212 if (fe->ops.tuner_ops.set_params) {
213 ret = fe->ops.tuner_ops.set_params(fe);
218 memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5);
221 ret = si2168_cmd_execute(s, &cmd);
225 /* that has no big effect */
226 if (c->delivery_system == SYS_DVBT)
227 memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6);
228 else if (c->delivery_system == SYS_DVBC_ANNEX_A)
229 memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6);
230 else if (c->delivery_system == SYS_DVBT2)
231 memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6);
234 ret = si2168_cmd_execute(s, &cmd);
238 memcpy(cmd.args, "\x51\x03", 2);
241 ret = si2168_cmd_execute(s, &cmd);
245 memcpy(cmd.args, "\x12\x08\x04", 3);
248 ret = si2168_cmd_execute(s, &cmd);
252 memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6);
255 ret = si2168_cmd_execute(s, &cmd);
259 memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6);
262 ret = si2168_cmd_execute(s, &cmd);
266 memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6);
269 ret = si2168_cmd_execute(s, &cmd);
273 memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6);
274 cmd.args[4] = delivery_system | bandwidth;
277 ret = si2168_cmd_execute(s, &cmd);
281 memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6);
284 ret = si2168_cmd_execute(s, &cmd);
288 memcpy(cmd.args, "\x14\x00\x01\x10\x16\x00", 6);
291 ret = si2168_cmd_execute(s, &cmd);
295 memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x18", 6);
298 ret = si2168_cmd_execute(s, &cmd);
302 memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x15", 6);
305 ret = si2168_cmd_execute(s, &cmd);
309 memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6);
312 ret = si2168_cmd_execute(s, &cmd);
316 memcpy(cmd.args, "\x85", 1);
319 ret = si2168_cmd_execute(s, &cmd);
323 s->delivery_system = c->delivery_system;
327 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
331 static int si2168_init(struct dvb_frontend *fe)
333 struct si2168 *s = fe->demodulator_priv;
334 int ret, len, remaining;
335 const struct firmware *fw = NULL;
337 const unsigned int i2c_wr_max = 8;
338 struct si2168_cmd cmd;
340 dev_dbg(&s->client->dev, "%s:\n", __func__);
342 memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
345 ret = si2168_cmd_execute(s, &cmd);
349 memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
352 ret = si2168_cmd_execute(s, &cmd);
356 /* query chip revision */
357 memcpy(cmd.args, "\x02", 1);
360 ret = si2168_cmd_execute(s, &cmd);
374 ret = si2168_cmd_execute(s, &cmd);
378 if (((cmd.args[1] & 0x0f) == 2) && (cmd.args[3] == '4') &&
379 (cmd.args[4] == '0'))
380 fw_file = SI2168_B40_FIRMWARE;
381 else if (((cmd.args[1] & 0x0f) == 1) && (cmd.args[3] == '3') &&
382 (cmd.args[4] == '0'))
383 fw_file = SI2168_A30_FIRMWARE;
385 dev_err(&s->client->dev,
386 "%s: no firmware file for Si2168-%c%c defined\n",
387 KBUILD_MODNAME, cmd.args[3], cmd.args[4]);
392 /* cold state - try to download firmware */
393 dev_info(&s->client->dev, "%s: found a '%s' in cold state\n",
394 KBUILD_MODNAME, si2168_ops.info.name);
396 /* request the firmware, this will block and timeout */
397 ret = request_firmware(&fw, fw_file, &s->client->dev);
399 /* fallback mechanism to handle old name for
400 SI2168_B40_FIRMWARE */
401 if (((cmd.args[1] & 0x0f) == 2) && (cmd.args[3] == '4') &&
402 (cmd.args[4] == '0')) {
403 fw_file = SI2168_B40_FIRMWARE_FALLBACK;
404 ret = request_firmware(&fw, fw_file, &s->client->dev);
407 dev_err(&s->client->dev, "%s: firmware file '%s' not found\n",
408 KBUILD_MODNAME, fw_file);
413 dev_info(&s->client->dev, "%s: downloading firmware from file '%s'\n",
414 KBUILD_MODNAME, fw_file);
416 for (remaining = fw->size; remaining > 0; remaining -= i2c_wr_max) {
418 if (len > i2c_wr_max)
421 memcpy(cmd.args, &fw->data[fw->size - remaining], len);
424 ret = si2168_cmd_execute(s, &cmd);
426 dev_err(&s->client->dev,
427 "%s: firmware download failed=%d\n",
428 KBUILD_MODNAME, ret);
433 release_firmware(fw);
436 memcpy(cmd.args, "\x01\x01", 2);
439 ret = si2168_cmd_execute(s, &cmd);
443 dev_info(&s->client->dev, "%s: found a '%s' in warm state\n",
444 KBUILD_MODNAME, si2168_ops.info.name);
451 release_firmware(fw);
453 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
457 static int si2168_sleep(struct dvb_frontend *fe)
459 struct si2168 *s = fe->demodulator_priv;
461 struct si2168_cmd cmd;
463 dev_dbg(&s->client->dev, "%s:\n", __func__);
467 memcpy(cmd.args, "\x13", 1);
470 ret = si2168_cmd_execute(s, &cmd);
476 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
480 static int si2168_get_tune_settings(struct dvb_frontend *fe,
481 struct dvb_frontend_tune_settings *s)
483 s->min_delay_ms = 900;
490 * We must use unlocked i2c_transfer() here because I2C lock is already taken
493 static int si2168_select(struct i2c_adapter *adap, void *mux_priv, u32 chan)
495 struct si2168 *s = mux_priv;
497 struct i2c_msg gate_open_msg = {
498 .addr = s->client->addr,
501 .buf = "\xc0\x0d\x01",
504 mutex_lock(&s->i2c_mutex);
506 /* open tuner I2C gate */
507 ret = __i2c_transfer(s->client->adapter, &gate_open_msg, 1);
509 dev_warn(&s->client->dev, "%s: i2c write failed=%d\n",
510 KBUILD_MODNAME, ret);
520 static int si2168_deselect(struct i2c_adapter *adap, void *mux_priv, u32 chan)
522 struct si2168 *s = mux_priv;
524 struct i2c_msg gate_close_msg = {
525 .addr = s->client->addr,
528 .buf = "\xc0\x0d\x00",
531 /* close tuner I2C gate */
532 ret = __i2c_transfer(s->client->adapter, &gate_close_msg, 1);
534 dev_warn(&s->client->dev, "%s: i2c write failed=%d\n",
535 KBUILD_MODNAME, ret);
542 mutex_unlock(&s->i2c_mutex);
547 static const struct dvb_frontend_ops si2168_ops = {
548 .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
550 .name = "Silicon Labs Si2168",
551 .caps = FE_CAN_FEC_1_2 |
564 FE_CAN_TRANSMISSION_MODE_AUTO |
565 FE_CAN_GUARD_INTERVAL_AUTO |
566 FE_CAN_HIERARCHY_AUTO |
571 .get_tune_settings = si2168_get_tune_settings,
574 .sleep = si2168_sleep,
576 .set_frontend = si2168_set_frontend,
578 .read_status = si2168_read_status,
581 static int si2168_probe(struct i2c_client *client,
582 const struct i2c_device_id *id)
584 struct si2168_config *config = client->dev.platform_data;
587 struct si2168_cmd cmd;
589 dev_dbg(&client->dev, "%s:\n", __func__);
591 s = kzalloc(sizeof(struct si2168), GFP_KERNEL);
594 dev_err(&client->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
599 mutex_init(&s->i2c_mutex);
601 /* check if the demod is there */
604 ret = si2168_cmd_execute(s, &cmd);
608 /* create mux i2c adapter for tuner */
609 s->adapter = i2c_add_mux_adapter(client->adapter, &client->dev, s,
610 0, 0, 0, si2168_select, si2168_deselect);
611 if (s->adapter == NULL)
614 /* create dvb_frontend */
615 memcpy(&s->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
616 s->fe.demodulator_priv = s;
618 *config->i2c_adapter = s->adapter;
619 *config->fe = &s->fe;
621 i2c_set_clientdata(client, s);
623 dev_info(&s->client->dev,
624 "%s: Silicon Labs Si2168 successfully attached\n",
629 dev_dbg(&client->dev, "%s: failed=%d\n", __func__, ret);
633 static int si2168_remove(struct i2c_client *client)
635 struct si2168 *s = i2c_get_clientdata(client);
637 dev_dbg(&client->dev, "%s:\n", __func__);
639 i2c_del_mux_adapter(s->adapter);
641 s->fe.ops.release = NULL;
642 s->fe.demodulator_priv = NULL;
649 static const struct i2c_device_id si2168_id[] = {
653 MODULE_DEVICE_TABLE(i2c, si2168_id);
655 static struct i2c_driver si2168_driver = {
657 .owner = THIS_MODULE,
660 .probe = si2168_probe,
661 .remove = si2168_remove,
662 .id_table = si2168_id,
665 module_i2c_driver(si2168_driver);
667 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
668 MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
669 MODULE_LICENSE("GPL");
670 MODULE_FIRMWARE(SI2168_A30_FIRMWARE);
671 MODULE_FIRMWARE(SI2168_B40_FIRMWARE);