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 /* execute firmware command */
24 static int si2168_cmd_execute(struct i2c_client
*client
, struct si2168_cmd
*cmd
)
26 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
28 unsigned long timeout
;
30 mutex_lock(&dev
->i2c_mutex
);
33 /* write cmd and args for firmware */
34 ret
= i2c_master_send(client
, cmd
->args
, cmd
->wlen
);
36 goto err_mutex_unlock
;
37 } else if (ret
!= cmd
->wlen
) {
39 goto err_mutex_unlock
;
44 /* wait cmd execution terminate */
46 timeout
= jiffies
+ msecs_to_jiffies(TIMEOUT
);
47 while (!time_after(jiffies
, timeout
)) {
48 ret
= i2c_master_recv(client
, cmd
->args
, cmd
->rlen
);
50 goto err_mutex_unlock
;
51 } else if (ret
!= cmd
->rlen
) {
53 goto err_mutex_unlock
;
57 if ((cmd
->args
[0] >> 7) & 0x01)
61 dev_dbg(&client
->dev
, "cmd execution took %d ms\n",
62 jiffies_to_msecs(jiffies
) -
63 (jiffies_to_msecs(timeout
) - TIMEOUT
));
66 if ((cmd
->args
[0] >> 6) & 0x01) {
68 goto err_mutex_unlock
;
71 if (!((cmd
->args
[0] >> 7) & 0x01)) {
73 goto err_mutex_unlock
;
77 mutex_unlock(&dev
->i2c_mutex
);
80 mutex_unlock(&dev
->i2c_mutex
);
81 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
85 static int si2168_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
87 struct i2c_client
*client
= fe
->demodulator_priv
;
88 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
89 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
91 unsigned int utmp
, utmp1
, utmp2
;
92 struct si2168_cmd cmd
;
101 switch (c
->delivery_system
) {
103 memcpy(cmd
.args
, "\xa0\x01", 2);
107 case SYS_DVBC_ANNEX_A
:
108 memcpy(cmd
.args
, "\x90\x01", 2);
113 memcpy(cmd
.args
, "\x50\x01", 2);
122 ret
= si2168_cmd_execute(client
, &cmd
);
126 switch ((cmd
.args
[2] >> 1) & 0x03) {
128 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
131 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
|
132 FE_HAS_SYNC
| FE_HAS_LOCK
;
136 dev
->fe_status
= *status
;
138 if (*status
& FE_HAS_LOCK
) {
140 c
->cnr
.stat
[0].scale
= FE_SCALE_DECIBEL
;
141 c
->cnr
.stat
[0].svalue
= cmd
.args
[3] * 1000 / 4;
144 c
->cnr
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
147 dev_dbg(&client
->dev
, "status=%02x args=%*ph\n",
148 *status
, cmd
.rlen
, cmd
.args
);
151 if (*status
& FE_HAS_VITERBI
) {
152 memcpy(cmd
.args
, "\x82\x00", 2);
155 ret
= si2168_cmd_execute(client
, &cmd
);
160 * Firmware returns [0, 255] mantissa and [0, 8] exponent.
161 * Convert to DVB API: mantissa * 10^(8 - exponent) / 10^8
163 utmp
= clamp(8 - cmd
.args
[1], 0, 8);
164 for (i
= 0, utmp1
= 1; i
< utmp
; i
++)
167 utmp1
= cmd
.args
[2] * utmp1
;
168 utmp2
= 100000000; /* 10^8 */
170 dev_dbg(&client
->dev
,
171 "post_bit_error=%u post_bit_count=%u ber=%u*10^-%u\n",
172 utmp1
, utmp2
, cmd
.args
[2], cmd
.args
[1]);
174 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
175 c
->post_bit_error
.stat
[0].uvalue
+= utmp1
;
176 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
177 c
->post_bit_count
.stat
[0].uvalue
+= utmp2
;
179 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
180 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
184 if (*status
& FE_HAS_SYNC
) {
185 memcpy(cmd
.args
, "\x84\x01", 2);
188 ret
= si2168_cmd_execute(client
, &cmd
);
192 utmp1
= cmd
.args
[2] << 8 | cmd
.args
[1] << 0;
193 dev_dbg(&client
->dev
, "block_error=%u\n", utmp1
);
195 /* Sometimes firmware returns bogus value */
199 c
->block_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
200 c
->block_error
.stat
[0].uvalue
+= utmp1
;
202 c
->block_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
207 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
211 static int si2168_set_frontend(struct dvb_frontend
*fe
)
213 struct i2c_client
*client
= fe
->demodulator_priv
;
214 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
215 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
217 struct si2168_cmd cmd
;
218 u8 bandwidth
, delivery_system
;
220 dev_dbg(&client
->dev
,
221 "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n",
222 c
->delivery_system
, c
->modulation
, c
->frequency
,
223 c
->bandwidth_hz
, c
->symbol_rate
, c
->inversion
,
231 switch (c
->delivery_system
) {
233 delivery_system
= 0x20;
235 case SYS_DVBC_ANNEX_A
:
236 delivery_system
= 0x30;
239 delivery_system
= 0x70;
246 if (c
->bandwidth_hz
== 0) {
249 } else if (c
->bandwidth_hz
<= 2000000)
251 else if (c
->bandwidth_hz
<= 5000000)
253 else if (c
->bandwidth_hz
<= 6000000)
255 else if (c
->bandwidth_hz
<= 7000000)
257 else if (c
->bandwidth_hz
<= 8000000)
259 else if (c
->bandwidth_hz
<= 9000000)
261 else if (c
->bandwidth_hz
<= 10000000)
267 if (fe
->ops
.tuner_ops
.set_params
) {
268 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
273 memcpy(cmd
.args
, "\x88\x02\x02\x02\x02", 5);
276 ret
= si2168_cmd_execute(client
, &cmd
);
280 /* that has no big effect */
281 if (c
->delivery_system
== SYS_DVBT
)
282 memcpy(cmd
.args
, "\x89\x21\x06\x11\xff\x98", 6);
283 else if (c
->delivery_system
== SYS_DVBC_ANNEX_A
)
284 memcpy(cmd
.args
, "\x89\x21\x06\x11\x89\xf0", 6);
285 else if (c
->delivery_system
== SYS_DVBT2
)
286 memcpy(cmd
.args
, "\x89\x21\x06\x11\x89\x20", 6);
289 ret
= si2168_cmd_execute(client
, &cmd
);
293 if (c
->delivery_system
== SYS_DVBT2
) {
296 cmd
.args
[1] = c
->stream_id
& 0xff;
297 cmd
.args
[2] = c
->stream_id
== NO_STREAM_ID_FILTER
? 0 : 1;
300 ret
= si2168_cmd_execute(client
, &cmd
);
305 memcpy(cmd
.args
, "\x51\x03", 2);
308 ret
= si2168_cmd_execute(client
, &cmd
);
312 memcpy(cmd
.args
, "\x12\x08\x04", 3);
315 ret
= si2168_cmd_execute(client
, &cmd
);
319 memcpy(cmd
.args
, "\x14\x00\x0c\x10\x12\x00", 6);
322 ret
= si2168_cmd_execute(client
, &cmd
);
326 memcpy(cmd
.args
, "\x14\x00\x06\x10\x24\x00", 6);
329 ret
= si2168_cmd_execute(client
, &cmd
);
333 memcpy(cmd
.args
, "\x14\x00\x07\x10\x00\x24", 6);
336 ret
= si2168_cmd_execute(client
, &cmd
);
340 memcpy(cmd
.args
, "\x14\x00\x0a\x10\x00\x00", 6);
341 cmd
.args
[4] = delivery_system
| bandwidth
;
344 ret
= si2168_cmd_execute(client
, &cmd
);
348 /* set DVB-C symbol rate */
349 if (c
->delivery_system
== SYS_DVBC_ANNEX_A
) {
350 memcpy(cmd
.args
, "\x14\x00\x02\x11", 4);
351 cmd
.args
[4] = ((c
->symbol_rate
/ 1000) >> 0) & 0xff;
352 cmd
.args
[5] = ((c
->symbol_rate
/ 1000) >> 8) & 0xff;
355 ret
= si2168_cmd_execute(client
, &cmd
);
360 memcpy(cmd
.args
, "\x14\x00\x0f\x10\x10\x00", 6);
363 ret
= si2168_cmd_execute(client
, &cmd
);
367 memcpy(cmd
.args
, "\x14\x00\x09\x10\xe3\x08", 6);
368 cmd
.args
[5] |= dev
->ts_clock_inv
? 0x00 : 0x10;
371 ret
= si2168_cmd_execute(client
, &cmd
);
375 memcpy(cmd
.args
, "\x14\x00\x08\x10\xd7\x05", 6);
376 cmd
.args
[5] |= dev
->ts_clock_inv
? 0x00 : 0x10;
379 ret
= si2168_cmd_execute(client
, &cmd
);
383 memcpy(cmd
.args
, "\x14\x00\x01\x12\x00\x00", 6);
386 ret
= si2168_cmd_execute(client
, &cmd
);
390 memcpy(cmd
.args
, "\x14\x00\x01\x03\x0c\x00", 6);
393 ret
= si2168_cmd_execute(client
, &cmd
);
397 memcpy(cmd
.args
, "\x85", 1);
400 ret
= si2168_cmd_execute(client
, &cmd
);
404 dev
->delivery_system
= c
->delivery_system
;
408 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
412 static int si2168_init(struct dvb_frontend
*fe
)
414 struct i2c_client
*client
= fe
->demodulator_priv
;
415 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
416 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
417 int ret
, len
, remaining
;
418 const struct firmware
*fw
;
419 struct si2168_cmd cmd
;
421 dev_dbg(&client
->dev
, "\n");
424 memcpy(cmd
.args
, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
427 ret
= si2168_cmd_execute(client
, &cmd
);
433 memcpy(cmd
.args
, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
436 ret
= si2168_cmd_execute(client
, &cmd
);
441 memcpy(cmd
.args
, "\x85", 1);
444 ret
= si2168_cmd_execute(client
, &cmd
);
452 memcpy(cmd
.args
, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
455 ret
= si2168_cmd_execute(client
, &cmd
);
459 /* request the firmware, this will block and timeout */
460 ret
= request_firmware(&fw
, dev
->firmware_name
, &client
->dev
);
462 /* fallback mechanism to handle old name for Si2168 B40 fw */
463 if (dev
->chip_id
== SI2168_CHIP_ID_B40
) {
464 dev
->firmware_name
= SI2168_B40_FIRMWARE_FALLBACK
;
465 ret
= request_firmware(&fw
, dev
->firmware_name
,
470 dev_notice(&client
->dev
,
471 "please install firmware file '%s'\n",
472 SI2168_B40_FIRMWARE
);
474 dev_err(&client
->dev
,
475 "firmware file '%s' not found\n",
477 goto err_release_firmware
;
481 dev_info(&client
->dev
, "downloading firmware from file '%s'\n",
484 if ((fw
->size
% 17 == 0) && (fw
->data
[0] > 5)) {
485 /* firmware is in the new format */
486 for (remaining
= fw
->size
; remaining
> 0; remaining
-= 17) {
487 len
= fw
->data
[fw
->size
- remaining
];
488 if (len
> SI2168_ARGLEN
) {
492 memcpy(cmd
.args
, &fw
->data
[(fw
->size
- remaining
) + 1], len
);
495 ret
= si2168_cmd_execute(client
, &cmd
);
499 } else if (fw
->size
% 8 == 0) {
500 /* firmware is in the old format */
501 for (remaining
= fw
->size
; remaining
> 0; remaining
-= 8) {
503 memcpy(cmd
.args
, &fw
->data
[fw
->size
- remaining
], len
);
506 ret
= si2168_cmd_execute(client
, &cmd
);
511 /* bad or unknown firmware format */
516 dev_err(&client
->dev
, "firmware download failed %d\n", ret
);
517 goto err_release_firmware
;
520 release_firmware(fw
);
522 memcpy(cmd
.args
, "\x01\x01", 2);
525 ret
= si2168_cmd_execute(client
, &cmd
);
529 /* query firmware version */
530 memcpy(cmd
.args
, "\x11", 1);
533 ret
= si2168_cmd_execute(client
, &cmd
);
537 dev
->version
= (cmd
.args
[9] + '@') << 24 | (cmd
.args
[6] - '0') << 16 |
538 (cmd
.args
[7] - '0') << 8 | (cmd
.args
[8]) << 0;
539 dev_info(&client
->dev
, "firmware version: %c %d.%d.%d\n",
540 dev
->version
>> 24 & 0xff, dev
->version
>> 16 & 0xff,
541 dev
->version
>> 8 & 0xff, dev
->version
>> 0 & 0xff);
544 memcpy(cmd
.args
, "\x14\x00\x01\x10\x10\x00", 6);
545 cmd
.args
[4] |= dev
->ts_mode
;
546 if (dev
->ts_clock_gapped
)
550 ret
= si2168_cmd_execute(client
, &cmd
);
556 /* Init stats here to indicate which stats are supported */
558 c
->cnr
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
559 c
->post_bit_error
.len
= 1;
560 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
561 c
->post_bit_count
.len
= 1;
562 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
563 c
->block_error
.len
= 1;
564 c
->block_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
569 err_release_firmware
:
570 release_firmware(fw
);
572 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
576 static int si2168_sleep(struct dvb_frontend
*fe
)
578 struct i2c_client
*client
= fe
->demodulator_priv
;
579 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
581 struct si2168_cmd cmd
;
583 dev_dbg(&client
->dev
, "\n");
587 /* Firmware B 4.0-11 or later loses warm state during sleep */
588 if (dev
->version
> ('B' << 24 | 4 << 16 | 0 << 8 | 11 << 0))
591 memcpy(cmd
.args
, "\x13", 1);
594 ret
= si2168_cmd_execute(client
, &cmd
);
600 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
604 static int si2168_get_tune_settings(struct dvb_frontend
*fe
,
605 struct dvb_frontend_tune_settings
*s
)
607 s
->min_delay_ms
= 900;
612 static int si2168_select(struct i2c_mux_core
*muxc
, u32 chan
)
614 struct i2c_client
*client
= i2c_mux_priv(muxc
);
616 struct si2168_cmd cmd
;
619 memcpy(cmd
.args
, "\xc0\x0d\x01", 3);
622 ret
= si2168_cmd_execute(client
, &cmd
);
628 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
632 static int si2168_deselect(struct i2c_mux_core
*muxc
, u32 chan
)
634 struct i2c_client
*client
= i2c_mux_priv(muxc
);
636 struct si2168_cmd cmd
;
639 memcpy(cmd
.args
, "\xc0\x0d\x00", 3);
642 ret
= si2168_cmd_execute(client
, &cmd
);
648 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
652 static const struct dvb_frontend_ops si2168_ops
= {
653 .delsys
= {SYS_DVBT
, SYS_DVBT2
, SYS_DVBC_ANNEX_A
},
655 .name
= "Silicon Labs Si2168",
656 .symbol_rate_min
= 1000000,
657 .symbol_rate_max
= 7200000,
658 .caps
= FE_CAN_FEC_1_2
|
671 FE_CAN_TRANSMISSION_MODE_AUTO
|
672 FE_CAN_GUARD_INTERVAL_AUTO
|
673 FE_CAN_HIERARCHY_AUTO
|
675 FE_CAN_2G_MODULATION
|
679 .get_tune_settings
= si2168_get_tune_settings
,
682 .sleep
= si2168_sleep
,
684 .set_frontend
= si2168_set_frontend
,
686 .read_status
= si2168_read_status
,
689 static int si2168_probe(struct i2c_client
*client
,
690 const struct i2c_device_id
*id
)
692 struct si2168_config
*config
= client
->dev
.platform_data
;
693 struct si2168_dev
*dev
;
695 struct si2168_cmd cmd
;
697 dev_dbg(&client
->dev
, "\n");
699 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
705 i2c_set_clientdata(client
, dev
);
706 mutex_init(&dev
->i2c_mutex
);
709 memcpy(cmd
.args
, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
712 ret
= si2168_cmd_execute(client
, &cmd
);
717 memcpy(cmd
.args
, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
720 ret
= si2168_cmd_execute(client
, &cmd
);
724 /* Query chip revision */
725 memcpy(cmd
.args
, "\x02", 1);
728 ret
= si2168_cmd_execute(client
, &cmd
);
732 dev
->chip_id
= cmd
.args
[1] << 24 | cmd
.args
[2] << 16 |
733 cmd
.args
[3] << 8 | cmd
.args
[4] << 0;
735 switch (dev
->chip_id
) {
736 case SI2168_CHIP_ID_A20
:
737 dev
->firmware_name
= SI2168_A20_FIRMWARE
;
739 case SI2168_CHIP_ID_A30
:
740 dev
->firmware_name
= SI2168_A30_FIRMWARE
;
742 case SI2168_CHIP_ID_B40
:
743 dev
->firmware_name
= SI2168_B40_FIRMWARE
;
745 case SI2168_CHIP_ID_D60
:
746 dev
->firmware_name
= SI2168_D60_FIRMWARE
;
749 dev_dbg(&client
->dev
, "unknown chip version Si21%d-%c%c%c\n",
750 cmd
.args
[2], cmd
.args
[1], cmd
.args
[3], cmd
.args
[4]);
755 dev
->version
= (cmd
.args
[1]) << 24 | (cmd
.args
[3] - '0') << 16 |
756 (cmd
.args
[4] - '0') << 8 | (cmd
.args
[5]) << 0;
758 /* create mux i2c adapter for tuner */
759 dev
->muxc
= i2c_mux_alloc(client
->adapter
, &client
->dev
,
760 1, 0, I2C_MUX_LOCKED
,
761 si2168_select
, si2168_deselect
);
766 dev
->muxc
->priv
= client
;
767 ret
= i2c_mux_add_adapter(dev
->muxc
, 0, 0, 0);
771 /* create dvb_frontend */
772 memcpy(&dev
->fe
.ops
, &si2168_ops
, sizeof(struct dvb_frontend_ops
));
773 dev
->fe
.demodulator_priv
= client
;
774 *config
->i2c_adapter
= dev
->muxc
->adapter
[0];
775 *config
->fe
= &dev
->fe
;
776 dev
->ts_mode
= config
->ts_mode
;
777 dev
->ts_clock_inv
= config
->ts_clock_inv
;
778 dev
->ts_clock_gapped
= config
->ts_clock_gapped
;
780 dev_info(&client
->dev
, "Silicon Labs Si2168-%c%d%d successfully identified\n",
781 dev
->version
>> 24 & 0xff, dev
->version
>> 16 & 0xff,
782 dev
->version
>> 8 & 0xff);
783 dev_info(&client
->dev
, "firmware version: %c %d.%d.%d\n",
784 dev
->version
>> 24 & 0xff, dev
->version
>> 16 & 0xff,
785 dev
->version
>> 8 & 0xff, dev
->version
>> 0 & 0xff);
791 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
795 static int si2168_remove(struct i2c_client
*client
)
797 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
799 dev_dbg(&client
->dev
, "\n");
801 i2c_mux_del_adapters(dev
->muxc
);
803 dev
->fe
.ops
.release
= NULL
;
804 dev
->fe
.demodulator_priv
= NULL
;
811 static const struct i2c_device_id si2168_id_table
[] = {
815 MODULE_DEVICE_TABLE(i2c
, si2168_id_table
);
817 static struct i2c_driver si2168_driver
= {
820 .suppress_bind_attrs
= true,
822 .probe
= si2168_probe
,
823 .remove
= si2168_remove
,
824 .id_table
= si2168_id_table
,
827 module_i2c_driver(si2168_driver
);
829 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
830 MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
831 MODULE_LICENSE("GPL");
832 MODULE_FIRMWARE(SI2168_A20_FIRMWARE
);
833 MODULE_FIRMWARE(SI2168_A30_FIRMWARE
);
834 MODULE_FIRMWARE(SI2168_B40_FIRMWARE
);
835 MODULE_FIRMWARE(SI2168_D60_FIRMWARE
);