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",
458 goto error_fw_release
;
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",
478 goto error_fw_release
;
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
);
511 release_firmware(fw
);
513 dev_dbg(&s
->client
->dev
, "failed=%d\n", ret
);
517 static int si2168_sleep(struct dvb_frontend
*fe
)
519 struct si2168
*s
= fe
->demodulator_priv
;
521 struct si2168_cmd cmd
;
523 dev_dbg(&s
->client
->dev
, "\n");
527 memcpy(cmd
.args
, "\x13", 1);
530 ret
= si2168_cmd_execute(s
, &cmd
);
536 dev_dbg(&s
->client
->dev
, "failed=%d\n", ret
);
540 static int si2168_get_tune_settings(struct dvb_frontend
*fe
,
541 struct dvb_frontend_tune_settings
*s
)
543 s
->min_delay_ms
= 900;
550 * We must use unlocked i2c_transfer() here because I2C lock is already taken
553 static int si2168_select(struct i2c_adapter
*adap
, void *mux_priv
, u32 chan
)
555 struct si2168
*s
= mux_priv
;
557 struct i2c_msg gate_open_msg
= {
558 .addr
= s
->client
->addr
,
561 .buf
= "\xc0\x0d\x01",
564 mutex_lock(&s
->i2c_mutex
);
566 /* open tuner I2C gate */
567 ret
= __i2c_transfer(s
->client
->adapter
, &gate_open_msg
, 1);
569 dev_warn(&s
->client
->dev
, "i2c write failed=%d\n", ret
);
579 static int si2168_deselect(struct i2c_adapter
*adap
, void *mux_priv
, u32 chan
)
581 struct si2168
*s
= mux_priv
;
583 struct i2c_msg gate_close_msg
= {
584 .addr
= s
->client
->addr
,
587 .buf
= "\xc0\x0d\x00",
590 /* close tuner I2C gate */
591 ret
= __i2c_transfer(s
->client
->adapter
, &gate_close_msg
, 1);
593 dev_warn(&s
->client
->dev
, "i2c write failed=%d\n", ret
);
600 mutex_unlock(&s
->i2c_mutex
);
605 static const struct dvb_frontend_ops si2168_ops
= {
606 .delsys
= {SYS_DVBT
, SYS_DVBT2
, SYS_DVBC_ANNEX_A
},
608 .name
= "Silicon Labs Si2168",
609 .caps
= FE_CAN_FEC_1_2
|
622 FE_CAN_TRANSMISSION_MODE_AUTO
|
623 FE_CAN_GUARD_INTERVAL_AUTO
|
624 FE_CAN_HIERARCHY_AUTO
|
626 FE_CAN_2G_MODULATION
|
630 .get_tune_settings
= si2168_get_tune_settings
,
633 .sleep
= si2168_sleep
,
635 .set_frontend
= si2168_set_frontend
,
637 .read_status
= si2168_read_status
,
640 static int si2168_probe(struct i2c_client
*client
,
641 const struct i2c_device_id
*id
)
643 struct si2168_config
*config
= client
->dev
.platform_data
;
647 dev_dbg(&client
->dev
, "\n");
649 s
= kzalloc(sizeof(struct si2168
), GFP_KERNEL
);
652 dev_err(&client
->dev
, "kzalloc() failed\n");
657 mutex_init(&s
->i2c_mutex
);
659 /* create mux i2c adapter for tuner */
660 s
->adapter
= i2c_add_mux_adapter(client
->adapter
, &client
->dev
, s
,
661 0, 0, 0, si2168_select
, si2168_deselect
);
662 if (s
->adapter
== NULL
) {
667 /* create dvb_frontend */
668 memcpy(&s
->fe
.ops
, &si2168_ops
, sizeof(struct dvb_frontend_ops
));
669 s
->fe
.demodulator_priv
= s
;
671 *config
->i2c_adapter
= s
->adapter
;
672 *config
->fe
= &s
->fe
;
673 s
->ts_mode
= config
->ts_mode
;
674 s
->ts_clock_inv
= config
->ts_clock_inv
;
675 s
->fw_loaded
= false;
677 i2c_set_clientdata(client
, s
);
679 dev_info(&s
->client
->dev
,
680 "Silicon Labs Si2168 successfully attached\n");
684 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
688 static int si2168_remove(struct i2c_client
*client
)
690 struct si2168
*s
= i2c_get_clientdata(client
);
692 dev_dbg(&client
->dev
, "\n");
694 i2c_del_mux_adapter(s
->adapter
);
696 s
->fe
.ops
.release
= NULL
;
697 s
->fe
.demodulator_priv
= NULL
;
704 static const struct i2c_device_id si2168_id
[] = {
708 MODULE_DEVICE_TABLE(i2c
, si2168_id
);
710 static struct i2c_driver si2168_driver
= {
712 .owner
= THIS_MODULE
,
715 .probe
= si2168_probe
,
716 .remove
= si2168_remove
,
717 .id_table
= si2168_id
,
720 module_i2c_driver(si2168_driver
);
722 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
723 MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
724 MODULE_LICENSE("GPL");
725 MODULE_FIRMWARE(SI2168_A20_FIRMWARE
);
726 MODULE_FIRMWARE(SI2168_A30_FIRMWARE
);
727 MODULE_FIRMWARE(SI2168_B40_FIRMWARE
);