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 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
));
64 if ((cmd
->args
[0] >> 6) & 0x01) {
66 goto err_mutex_unlock
;
69 if (!((cmd
->args
[0] >> 7) & 0x01)) {
71 goto err_mutex_unlock
;
75 mutex_unlock(&dev
->i2c_mutex
);
78 mutex_unlock(&dev
->i2c_mutex
);
79 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
83 static int si2168_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
85 struct i2c_client
*client
= fe
->demodulator_priv
;
86 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
87 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
89 struct si2168_cmd cmd
;
98 switch (c
->delivery_system
) {
100 memcpy(cmd
.args
, "\xa0\x01", 2);
104 case SYS_DVBC_ANNEX_A
:
105 memcpy(cmd
.args
, "\x90\x01", 2);
110 memcpy(cmd
.args
, "\x50\x01", 2);
119 ret
= si2168_cmd_execute(client
, &cmd
);
123 switch ((cmd
.args
[2] >> 1) & 0x03) {
125 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
128 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
|
129 FE_HAS_SYNC
| FE_HAS_LOCK
;
133 dev
->fe_status
= *status
;
135 if (*status
& FE_HAS_LOCK
) {
137 c
->cnr
.stat
[0].scale
= FE_SCALE_DECIBEL
;
138 c
->cnr
.stat
[0].svalue
= cmd
.args
[3] * 1000 / 4;
141 c
->cnr
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
144 dev_dbg(&client
->dev
, "status=%02x args=%*ph\n",
145 *status
, cmd
.rlen
, cmd
.args
);
149 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
153 static int si2168_set_frontend(struct dvb_frontend
*fe
)
155 struct i2c_client
*client
= fe
->demodulator_priv
;
156 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
157 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
159 struct si2168_cmd cmd
;
160 u8 bandwidth
, delivery_system
;
162 dev_dbg(&client
->dev
,
163 "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n",
164 c
->delivery_system
, c
->modulation
, c
->frequency
,
165 c
->bandwidth_hz
, c
->symbol_rate
, c
->inversion
,
173 switch (c
->delivery_system
) {
175 delivery_system
= 0x20;
177 case SYS_DVBC_ANNEX_A
:
178 delivery_system
= 0x30;
181 delivery_system
= 0x70;
188 if (c
->bandwidth_hz
== 0) {
191 } else if (c
->bandwidth_hz
<= 2000000)
193 else if (c
->bandwidth_hz
<= 5000000)
195 else if (c
->bandwidth_hz
<= 6000000)
197 else if (c
->bandwidth_hz
<= 7000000)
199 else if (c
->bandwidth_hz
<= 8000000)
201 else if (c
->bandwidth_hz
<= 9000000)
203 else if (c
->bandwidth_hz
<= 10000000)
209 if (fe
->ops
.tuner_ops
.set_params
) {
210 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
215 memcpy(cmd
.args
, "\x88\x02\x02\x02\x02", 5);
218 ret
= si2168_cmd_execute(client
, &cmd
);
222 /* that has no big effect */
223 if (c
->delivery_system
== SYS_DVBT
)
224 memcpy(cmd
.args
, "\x89\x21\x06\x11\xff\x98", 6);
225 else if (c
->delivery_system
== SYS_DVBC_ANNEX_A
)
226 memcpy(cmd
.args
, "\x89\x21\x06\x11\x89\xf0", 6);
227 else if (c
->delivery_system
== SYS_DVBT2
)
228 memcpy(cmd
.args
, "\x89\x21\x06\x11\x89\x20", 6);
231 ret
= si2168_cmd_execute(client
, &cmd
);
235 if (c
->delivery_system
== SYS_DVBT2
) {
238 cmd
.args
[1] = c
->stream_id
& 0xff;
239 cmd
.args
[2] = c
->stream_id
== NO_STREAM_ID_FILTER
? 0 : 1;
242 ret
= si2168_cmd_execute(client
, &cmd
);
247 memcpy(cmd
.args
, "\x51\x03", 2);
250 ret
= si2168_cmd_execute(client
, &cmd
);
254 memcpy(cmd
.args
, "\x12\x08\x04", 3);
257 ret
= si2168_cmd_execute(client
, &cmd
);
261 memcpy(cmd
.args
, "\x14\x00\x0c\x10\x12\x00", 6);
264 ret
= si2168_cmd_execute(client
, &cmd
);
268 memcpy(cmd
.args
, "\x14\x00\x06\x10\x24\x00", 6);
271 ret
= si2168_cmd_execute(client
, &cmd
);
275 memcpy(cmd
.args
, "\x14\x00\x07\x10\x00\x24", 6);
278 ret
= si2168_cmd_execute(client
, &cmd
);
282 memcpy(cmd
.args
, "\x14\x00\x0a\x10\x00\x00", 6);
283 cmd
.args
[4] = delivery_system
| bandwidth
;
286 ret
= si2168_cmd_execute(client
, &cmd
);
290 /* set DVB-C symbol rate */
291 if (c
->delivery_system
== SYS_DVBC_ANNEX_A
) {
292 memcpy(cmd
.args
, "\x14\x00\x02\x11", 4);
293 cmd
.args
[4] = ((c
->symbol_rate
/ 1000) >> 0) & 0xff;
294 cmd
.args
[5] = ((c
->symbol_rate
/ 1000) >> 8) & 0xff;
297 ret
= si2168_cmd_execute(client
, &cmd
);
302 memcpy(cmd
.args
, "\x14\x00\x0f\x10\x10\x00", 6);
305 ret
= si2168_cmd_execute(client
, &cmd
);
309 memcpy(cmd
.args
, "\x14\x00\x09\x10\xe3\x08", 6);
310 cmd
.args
[5] |= dev
->ts_clock_inv
? 0x00 : 0x10;
313 ret
= si2168_cmd_execute(client
, &cmd
);
317 memcpy(cmd
.args
, "\x14\x00\x08\x10\xd7\x05", 6);
318 cmd
.args
[5] |= dev
->ts_clock_inv
? 0x00 : 0x10;
321 ret
= si2168_cmd_execute(client
, &cmd
);
325 memcpy(cmd
.args
, "\x14\x00\x01\x12\x00\x00", 6);
328 ret
= si2168_cmd_execute(client
, &cmd
);
332 memcpy(cmd
.args
, "\x14\x00\x01\x03\x0c\x00", 6);
335 ret
= si2168_cmd_execute(client
, &cmd
);
339 memcpy(cmd
.args
, "\x85", 1);
342 ret
= si2168_cmd_execute(client
, &cmd
);
346 dev
->delivery_system
= c
->delivery_system
;
350 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
354 static int si2168_init(struct dvb_frontend
*fe
)
356 struct i2c_client
*client
= fe
->demodulator_priv
;
357 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
358 int ret
, len
, remaining
;
359 const struct firmware
*fw
;
360 struct si2168_cmd cmd
;
362 dev_dbg(&client
->dev
, "\n");
365 memcpy(cmd
.args
, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
368 ret
= si2168_cmd_execute(client
, &cmd
);
374 memcpy(cmd
.args
, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
377 ret
= si2168_cmd_execute(client
, &cmd
);
381 memcpy(cmd
.args
, "\x85", 1);
384 ret
= si2168_cmd_execute(client
, &cmd
);
392 memcpy(cmd
.args
, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
395 ret
= si2168_cmd_execute(client
, &cmd
);
399 /* request the firmware, this will block and timeout */
400 ret
= request_firmware(&fw
, dev
->firmware_name
, &client
->dev
);
402 /* fallback mechanism to handle old name for Si2168 B40 fw */
403 if (dev
->chip_id
== SI2168_CHIP_ID_B40
) {
404 dev
->firmware_name
= SI2168_B40_FIRMWARE_FALLBACK
;
405 ret
= request_firmware(&fw
, dev
->firmware_name
,
410 dev_notice(&client
->dev
,
411 "please install firmware file '%s'\n",
412 SI2168_B40_FIRMWARE
);
414 dev_err(&client
->dev
,
415 "firmware file '%s' not found\n",
417 goto err_release_firmware
;
421 dev_info(&client
->dev
, "downloading firmware from file '%s'\n",
424 if ((fw
->size
% 17 == 0) && (fw
->data
[0] > 5)) {
425 /* firmware is in the new format */
426 for (remaining
= fw
->size
; remaining
> 0; remaining
-= 17) {
427 len
= fw
->data
[fw
->size
- remaining
];
428 if (len
> SI2168_ARGLEN
) {
432 memcpy(cmd
.args
, &fw
->data
[(fw
->size
- remaining
) + 1], len
);
435 ret
= si2168_cmd_execute(client
, &cmd
);
439 } else if (fw
->size
% 8 == 0) {
440 /* firmware is in the old format */
441 for (remaining
= fw
->size
; remaining
> 0; remaining
-= 8) {
443 memcpy(cmd
.args
, &fw
->data
[fw
->size
- remaining
], len
);
446 ret
= si2168_cmd_execute(client
, &cmd
);
451 /* bad or unknown firmware format */
456 dev_err(&client
->dev
, "firmware download failed %d\n", ret
);
457 goto err_release_firmware
;
460 release_firmware(fw
);
462 memcpy(cmd
.args
, "\x01\x01", 2);
465 ret
= si2168_cmd_execute(client
, &cmd
);
469 /* query firmware version */
470 memcpy(cmd
.args
, "\x11", 1);
473 ret
= si2168_cmd_execute(client
, &cmd
);
477 dev
->version
= (cmd
.args
[9] + '@') << 24 | (cmd
.args
[6] - '0') << 16 |
478 (cmd
.args
[7] - '0') << 8 | (cmd
.args
[8]) << 0;
479 dev_info(&client
->dev
, "firmware version: %c %d.%d.%d\n",
480 dev
->version
>> 24 & 0xff, dev
->version
>> 16 & 0xff,
481 dev
->version
>> 8 & 0xff, dev
->version
>> 0 & 0xff);
484 memcpy(cmd
.args
, "\x14\x00\x01\x10\x10\x00", 6);
485 cmd
.args
[4] |= dev
->ts_mode
;
486 if (dev
->ts_clock_gapped
)
490 ret
= si2168_cmd_execute(client
, &cmd
);
500 err_release_firmware
:
501 release_firmware(fw
);
503 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
507 static int si2168_sleep(struct dvb_frontend
*fe
)
509 struct i2c_client
*client
= fe
->demodulator_priv
;
510 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
512 struct si2168_cmd cmd
;
514 dev_dbg(&client
->dev
, "\n");
518 /* Firmware B 4.0-11 or later loses warm state during sleep */
519 if (dev
->version
> ('B' << 24 | 4 << 16 | 0 << 8 | 11 << 0))
522 memcpy(cmd
.args
, "\x13", 1);
525 ret
= si2168_cmd_execute(client
, &cmd
);
531 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
535 static int si2168_get_tune_settings(struct dvb_frontend
*fe
,
536 struct dvb_frontend_tune_settings
*s
)
538 s
->min_delay_ms
= 900;
543 static int si2168_select(struct i2c_mux_core
*muxc
, u32 chan
)
545 struct i2c_client
*client
= i2c_mux_priv(muxc
);
547 struct si2168_cmd cmd
;
550 memcpy(cmd
.args
, "\xc0\x0d\x01", 3);
553 ret
= si2168_cmd_execute(client
, &cmd
);
559 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
563 static int si2168_deselect(struct i2c_mux_core
*muxc
, u32 chan
)
565 struct i2c_client
*client
= i2c_mux_priv(muxc
);
567 struct si2168_cmd cmd
;
570 memcpy(cmd
.args
, "\xc0\x0d\x00", 3);
573 ret
= si2168_cmd_execute(client
, &cmd
);
579 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
583 static const struct dvb_frontend_ops si2168_ops
= {
584 .delsys
= {SYS_DVBT
, SYS_DVBT2
, SYS_DVBC_ANNEX_A
},
586 .name
= "Silicon Labs Si2168",
587 .symbol_rate_min
= 1000000,
588 .symbol_rate_max
= 7200000,
589 .caps
= FE_CAN_FEC_1_2
|
602 FE_CAN_TRANSMISSION_MODE_AUTO
|
603 FE_CAN_GUARD_INTERVAL_AUTO
|
604 FE_CAN_HIERARCHY_AUTO
|
606 FE_CAN_2G_MODULATION
|
610 .get_tune_settings
= si2168_get_tune_settings
,
613 .sleep
= si2168_sleep
,
615 .set_frontend
= si2168_set_frontend
,
617 .read_status
= si2168_read_status
,
620 static int si2168_probe(struct i2c_client
*client
,
621 const struct i2c_device_id
*id
)
623 struct si2168_config
*config
= client
->dev
.platform_data
;
624 struct si2168_dev
*dev
;
626 struct si2168_cmd cmd
;
628 dev_dbg(&client
->dev
, "\n");
630 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
633 dev_err(&client
->dev
, "kzalloc() failed\n");
637 i2c_set_clientdata(client
, dev
);
638 mutex_init(&dev
->i2c_mutex
);
641 memcpy(cmd
.args
, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
644 ret
= si2168_cmd_execute(client
, &cmd
);
649 memcpy(cmd
.args
, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
652 ret
= si2168_cmd_execute(client
, &cmd
);
656 /* Query chip revision */
657 memcpy(cmd
.args
, "\x02", 1);
660 ret
= si2168_cmd_execute(client
, &cmd
);
664 dev
->chip_id
= cmd
.args
[1] << 24 | cmd
.args
[2] << 16 |
665 cmd
.args
[3] << 8 | cmd
.args
[4] << 0;
667 switch (dev
->chip_id
) {
668 case SI2168_CHIP_ID_A20
:
669 dev
->firmware_name
= SI2168_A20_FIRMWARE
;
671 case SI2168_CHIP_ID_A30
:
672 dev
->firmware_name
= SI2168_A30_FIRMWARE
;
674 case SI2168_CHIP_ID_B40
:
675 dev
->firmware_name
= SI2168_B40_FIRMWARE
;
678 dev_dbg(&client
->dev
, "unknown chip version Si21%d-%c%c%c\n",
679 cmd
.args
[2], cmd
.args
[1], cmd
.args
[3], cmd
.args
[4]);
684 dev
->version
= (cmd
.args
[1]) << 24 | (cmd
.args
[3] - '0') << 16 |
685 (cmd
.args
[4] - '0') << 8 | (cmd
.args
[5]) << 0;
687 /* create mux i2c adapter for tuner */
688 dev
->muxc
= i2c_mux_alloc(client
->adapter
, &client
->dev
,
689 1, 0, I2C_MUX_LOCKED
,
690 si2168_select
, si2168_deselect
);
695 dev
->muxc
->priv
= client
;
696 ret
= i2c_mux_add_adapter(dev
->muxc
, 0, 0, 0);
700 /* create dvb_frontend */
701 memcpy(&dev
->fe
.ops
, &si2168_ops
, sizeof(struct dvb_frontend_ops
));
702 dev
->fe
.demodulator_priv
= client
;
703 *config
->i2c_adapter
= dev
->muxc
->adapter
[0];
704 *config
->fe
= &dev
->fe
;
705 dev
->ts_mode
= config
->ts_mode
;
706 dev
->ts_clock_inv
= config
->ts_clock_inv
;
707 dev
->ts_clock_gapped
= config
->ts_clock_gapped
;
709 dev_info(&client
->dev
, "Silicon Labs Si2168-%c%d%d successfully identified\n",
710 dev
->version
>> 24 & 0xff, dev
->version
>> 16 & 0xff,
711 dev
->version
>> 8 & 0xff);
712 dev_info(&client
->dev
, "firmware version: %c %d.%d.%d\n",
713 dev
->version
>> 24 & 0xff, dev
->version
>> 16 & 0xff,
714 dev
->version
>> 8 & 0xff, dev
->version
>> 0 & 0xff);
720 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
724 static int si2168_remove(struct i2c_client
*client
)
726 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
728 dev_dbg(&client
->dev
, "\n");
730 i2c_mux_del_adapters(dev
->muxc
);
732 dev
->fe
.ops
.release
= NULL
;
733 dev
->fe
.demodulator_priv
= NULL
;
740 static const struct i2c_device_id si2168_id_table
[] = {
744 MODULE_DEVICE_TABLE(i2c
, si2168_id_table
);
746 static struct i2c_driver si2168_driver
= {
749 .suppress_bind_attrs
= true,
751 .probe
= si2168_probe
,
752 .remove
= si2168_remove
,
753 .id_table
= si2168_id_table
,
756 module_i2c_driver(si2168_driver
);
758 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
759 MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
760 MODULE_LICENSE("GPL");
761 MODULE_FIRMWARE(SI2168_A20_FIRMWARE
);
762 MODULE_FIRMWARE(SI2168_A30_FIRMWARE
);
763 MODULE_FIRMWARE(SI2168_B40_FIRMWARE
);