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 /* Own I2C adapter locking is needed because of I2C gate logic. */
22 static int si2168_i2c_master_send_unlocked(const struct i2c_client
*client
,
23 const char *buf
, int count
)
26 struct i2c_msg msg
= {
33 ret
= __i2c_transfer(client
->adapter
, &msg
, 1);
34 return (ret
== 1) ? count
: ret
;
37 static int si2168_i2c_master_recv_unlocked(const struct i2c_client
*client
,
41 struct i2c_msg msg
= {
48 ret
= __i2c_transfer(client
->adapter
, &msg
, 1);
49 return (ret
== 1) ? count
: ret
;
52 /* execute firmware command */
53 static int si2168_cmd_execute_unlocked(struct i2c_client
*client
,
54 struct si2168_cmd
*cmd
)
57 unsigned long timeout
;
60 /* write cmd and args for firmware */
61 ret
= si2168_i2c_master_send_unlocked(client
, cmd
->args
,
65 } else if (ret
!= cmd
->wlen
) {
72 /* wait cmd execution terminate */
74 timeout
= jiffies
+ msecs_to_jiffies(TIMEOUT
);
75 while (!time_after(jiffies
, timeout
)) {
76 ret
= si2168_i2c_master_recv_unlocked(client
, cmd
->args
,
80 } else if (ret
!= cmd
->rlen
) {
86 if ((cmd
->args
[0] >> 7) & 0x01)
90 dev_dbg(&client
->dev
, "cmd execution took %d ms\n",
91 jiffies_to_msecs(jiffies
) -
92 (jiffies_to_msecs(timeout
) - TIMEOUT
));
95 if ((cmd
->args
[0] >> 6) & 0x01) {
100 if (!((cmd
->args
[0] >> 7) & 0x01)) {
108 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
112 static int si2168_cmd_execute(struct i2c_client
*client
, struct si2168_cmd
*cmd
)
116 i2c_lock_adapter(client
->adapter
);
117 ret
= si2168_cmd_execute_unlocked(client
, cmd
);
118 i2c_unlock_adapter(client
->adapter
);
123 static int si2168_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
125 struct i2c_client
*client
= fe
->demodulator_priv
;
126 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
127 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
129 struct si2168_cmd cmd
;
138 switch (c
->delivery_system
) {
140 memcpy(cmd
.args
, "\xa0\x01", 2);
144 case SYS_DVBC_ANNEX_A
:
145 memcpy(cmd
.args
, "\x90\x01", 2);
150 memcpy(cmd
.args
, "\x50\x01", 2);
159 ret
= si2168_cmd_execute(client
, &cmd
);
163 switch ((cmd
.args
[2] >> 1) & 0x03) {
165 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
168 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
|
169 FE_HAS_SYNC
| FE_HAS_LOCK
;
173 dev
->fe_status
= *status
;
175 if (*status
& FE_HAS_LOCK
) {
177 c
->cnr
.stat
[0].scale
= FE_SCALE_DECIBEL
;
178 c
->cnr
.stat
[0].svalue
= cmd
.args
[3] * 1000 / 4;
181 c
->cnr
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
184 dev_dbg(&client
->dev
, "status=%02x args=%*ph\n",
185 *status
, cmd
.rlen
, cmd
.args
);
189 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
193 static int si2168_set_frontend(struct dvb_frontend
*fe
)
195 struct i2c_client
*client
= fe
->demodulator_priv
;
196 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
197 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
199 struct si2168_cmd cmd
;
200 u8 bandwidth
, delivery_system
;
202 dev_dbg(&client
->dev
,
203 "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n",
204 c
->delivery_system
, c
->modulation
, c
->frequency
,
205 c
->bandwidth_hz
, c
->symbol_rate
, c
->inversion
,
213 switch (c
->delivery_system
) {
215 delivery_system
= 0x20;
217 case SYS_DVBC_ANNEX_A
:
218 delivery_system
= 0x30;
221 delivery_system
= 0x70;
228 if (c
->bandwidth_hz
== 0) {
231 } else if (c
->bandwidth_hz
<= 2000000)
233 else if (c
->bandwidth_hz
<= 5000000)
235 else if (c
->bandwidth_hz
<= 6000000)
237 else if (c
->bandwidth_hz
<= 7000000)
239 else if (c
->bandwidth_hz
<= 8000000)
241 else if (c
->bandwidth_hz
<= 9000000)
243 else if (c
->bandwidth_hz
<= 10000000)
249 if (fe
->ops
.tuner_ops
.set_params
) {
250 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
255 memcpy(cmd
.args
, "\x88\x02\x02\x02\x02", 5);
258 ret
= si2168_cmd_execute(client
, &cmd
);
262 /* that has no big effect */
263 if (c
->delivery_system
== SYS_DVBT
)
264 memcpy(cmd
.args
, "\x89\x21\x06\x11\xff\x98", 6);
265 else if (c
->delivery_system
== SYS_DVBC_ANNEX_A
)
266 memcpy(cmd
.args
, "\x89\x21\x06\x11\x89\xf0", 6);
267 else if (c
->delivery_system
== SYS_DVBT2
)
268 memcpy(cmd
.args
, "\x89\x21\x06\x11\x89\x20", 6);
271 ret
= si2168_cmd_execute(client
, &cmd
);
275 if (c
->delivery_system
== SYS_DVBT2
) {
278 cmd
.args
[1] = c
->stream_id
& 0xff;
279 cmd
.args
[2] = c
->stream_id
== NO_STREAM_ID_FILTER
? 0 : 1;
282 ret
= si2168_cmd_execute(client
, &cmd
);
287 memcpy(cmd
.args
, "\x51\x03", 2);
290 ret
= si2168_cmd_execute(client
, &cmd
);
294 memcpy(cmd
.args
, "\x12\x08\x04", 3);
297 ret
= si2168_cmd_execute(client
, &cmd
);
301 memcpy(cmd
.args
, "\x14\x00\x0c\x10\x12\x00", 6);
304 ret
= si2168_cmd_execute(client
, &cmd
);
308 memcpy(cmd
.args
, "\x14\x00\x06\x10\x24\x00", 6);
311 ret
= si2168_cmd_execute(client
, &cmd
);
315 memcpy(cmd
.args
, "\x14\x00\x07\x10\x00\x24", 6);
318 ret
= si2168_cmd_execute(client
, &cmd
);
322 memcpy(cmd
.args
, "\x14\x00\x0a\x10\x00\x00", 6);
323 cmd
.args
[4] = delivery_system
| bandwidth
;
326 ret
= si2168_cmd_execute(client
, &cmd
);
330 /* set DVB-C symbol rate */
331 if (c
->delivery_system
== SYS_DVBC_ANNEX_A
) {
332 memcpy(cmd
.args
, "\x14\x00\x02\x11", 4);
333 cmd
.args
[4] = ((c
->symbol_rate
/ 1000) >> 0) & 0xff;
334 cmd
.args
[5] = ((c
->symbol_rate
/ 1000) >> 8) & 0xff;
337 ret
= si2168_cmd_execute(client
, &cmd
);
342 memcpy(cmd
.args
, "\x14\x00\x0f\x10\x10\x00", 6);
345 ret
= si2168_cmd_execute(client
, &cmd
);
349 memcpy(cmd
.args
, "\x14\x00\x09\x10\xe3\x08", 6);
350 cmd
.args
[5] |= dev
->ts_clock_inv
? 0x00 : 0x10;
353 ret
= si2168_cmd_execute(client
, &cmd
);
357 memcpy(cmd
.args
, "\x14\x00\x08\x10\xd7\x05", 6);
358 cmd
.args
[5] |= dev
->ts_clock_inv
? 0x00 : 0x10;
361 ret
= si2168_cmd_execute(client
, &cmd
);
365 memcpy(cmd
.args
, "\x14\x00\x01\x12\x00\x00", 6);
368 ret
= si2168_cmd_execute(client
, &cmd
);
372 memcpy(cmd
.args
, "\x14\x00\x01\x03\x0c\x00", 6);
375 ret
= si2168_cmd_execute(client
, &cmd
);
379 memcpy(cmd
.args
, "\x85", 1);
382 ret
= si2168_cmd_execute(client
, &cmd
);
386 dev
->delivery_system
= c
->delivery_system
;
390 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
394 static int si2168_init(struct dvb_frontend
*fe
)
396 struct i2c_client
*client
= fe
->demodulator_priv
;
397 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
398 int ret
, len
, remaining
;
399 const struct firmware
*fw
;
401 struct si2168_cmd cmd
;
402 unsigned int chip_id
;
404 dev_dbg(&client
->dev
, "\n");
407 memcpy(cmd
.args
, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
410 ret
= si2168_cmd_execute(client
, &cmd
);
414 if (dev
->fw_loaded
) {
416 memcpy(cmd
.args
, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
419 ret
= si2168_cmd_execute(client
, &cmd
);
423 memcpy(cmd
.args
, "\x85", 1);
426 ret
= si2168_cmd_execute(client
, &cmd
);
434 memcpy(cmd
.args
, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
437 ret
= si2168_cmd_execute(client
, &cmd
);
441 /* query chip revision */
442 memcpy(cmd
.args
, "\x02", 1);
445 ret
= si2168_cmd_execute(client
, &cmd
);
449 chip_id
= cmd
.args
[1] << 24 | cmd
.args
[2] << 16 | cmd
.args
[3] << 8 |
452 #define SI2168_A20 ('A' << 24 | 68 << 16 | '2' << 8 | '0' << 0)
453 #define SI2168_A30 ('A' << 24 | 68 << 16 | '3' << 8 | '0' << 0)
454 #define SI2168_B40 ('B' << 24 | 68 << 16 | '4' << 8 | '0' << 0)
458 fw_name
= SI2168_A20_FIRMWARE
;
461 fw_name
= SI2168_A30_FIRMWARE
;
464 fw_name
= SI2168_B40_FIRMWARE
;
467 dev_err(&client
->dev
, "unknown chip version Si21%d-%c%c%c\n",
468 cmd
.args
[2], cmd
.args
[1],
469 cmd
.args
[3], cmd
.args
[4]);
474 dev_info(&client
->dev
, "found a 'Silicon Labs Si21%d-%c%c%c'\n",
475 cmd
.args
[2], cmd
.args
[1], cmd
.args
[3], cmd
.args
[4]);
477 /* request the firmware, this will block and timeout */
478 ret
= request_firmware(&fw
, fw_name
, &client
->dev
);
480 /* fallback mechanism to handle old name for Si2168 B40 fw */
481 if (chip_id
== SI2168_B40
) {
482 fw_name
= SI2168_B40_FIRMWARE_FALLBACK
;
483 ret
= request_firmware(&fw
, fw_name
, &client
->dev
);
487 dev_notice(&client
->dev
,
488 "please install firmware file '%s'\n",
489 SI2168_B40_FIRMWARE
);
491 dev_err(&client
->dev
,
492 "firmware file '%s' not found\n",
494 goto err_release_firmware
;
498 dev_info(&client
->dev
, "downloading firmware from file '%s'\n",
501 if ((fw
->size
% 17 == 0) && (fw
->data
[0] > 5)) {
502 /* firmware is in the new format */
503 for (remaining
= fw
->size
; remaining
> 0; remaining
-= 17) {
504 len
= fw
->data
[fw
->size
- remaining
];
505 if (len
> SI2168_ARGLEN
) {
509 memcpy(cmd
.args
, &fw
->data
[(fw
->size
- remaining
) + 1], len
);
512 ret
= si2168_cmd_execute(client
, &cmd
);
516 } else if (fw
->size
% 8 == 0) {
517 /* firmware is in the old format */
518 for (remaining
= fw
->size
; remaining
> 0; remaining
-= 8) {
520 memcpy(cmd
.args
, &fw
->data
[fw
->size
- remaining
], len
);
523 ret
= si2168_cmd_execute(client
, &cmd
);
528 /* bad or unknown firmware format */
533 dev_err(&client
->dev
, "firmware download failed %d\n", ret
);
534 goto err_release_firmware
;
537 release_firmware(fw
);
539 memcpy(cmd
.args
, "\x01\x01", 2);
542 ret
= si2168_cmd_execute(client
, &cmd
);
546 /* query firmware version */
547 memcpy(cmd
.args
, "\x11", 1);
550 ret
= si2168_cmd_execute(client
, &cmd
);
554 dev_info(&client
->dev
, "firmware version: %c.%c.%d\n",
555 cmd
.args
[6], cmd
.args
[7], cmd
.args
[8]);
558 memcpy(cmd
.args
, "\x14\x00\x01\x10\x10\x00", 6);
559 cmd
.args
[4] |= dev
->ts_mode
;
560 if (dev
->ts_clock_gapped
)
564 ret
= si2168_cmd_execute(client
, &cmd
);
568 dev
->fw_loaded
= true;
574 err_release_firmware
:
575 release_firmware(fw
);
577 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
581 static int si2168_sleep(struct dvb_frontend
*fe
)
583 struct i2c_client
*client
= fe
->demodulator_priv
;
584 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
586 struct si2168_cmd cmd
;
588 dev_dbg(&client
->dev
, "\n");
592 memcpy(cmd
.args
, "\x13", 1);
595 ret
= si2168_cmd_execute(client
, &cmd
);
601 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
605 static int si2168_get_tune_settings(struct dvb_frontend
*fe
,
606 struct dvb_frontend_tune_settings
*s
)
608 s
->min_delay_ms
= 900;
615 * We must use unlocked I2C I/O because I2C adapter lock is already taken
616 * by the caller (usually tuner driver).
618 static int si2168_select(struct i2c_adapter
*adap
, void *mux_priv
, u32 chan
)
620 struct i2c_client
*client
= mux_priv
;
622 struct si2168_cmd cmd
;
625 memcpy(cmd
.args
, "\xc0\x0d\x01", 3);
628 ret
= si2168_cmd_execute_unlocked(client
, &cmd
);
634 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
638 static int si2168_deselect(struct i2c_adapter
*adap
, void *mux_priv
, u32 chan
)
640 struct i2c_client
*client
= mux_priv
;
642 struct si2168_cmd cmd
;
645 memcpy(cmd
.args
, "\xc0\x0d\x00", 3);
648 ret
= si2168_cmd_execute_unlocked(client
, &cmd
);
654 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
658 static const struct dvb_frontend_ops si2168_ops
= {
659 .delsys
= {SYS_DVBT
, SYS_DVBT2
, SYS_DVBC_ANNEX_A
},
661 .name
= "Silicon Labs Si2168",
662 .symbol_rate_min
= 1000000,
663 .symbol_rate_max
= 7200000,
664 .caps
= FE_CAN_FEC_1_2
|
677 FE_CAN_TRANSMISSION_MODE_AUTO
|
678 FE_CAN_GUARD_INTERVAL_AUTO
|
679 FE_CAN_HIERARCHY_AUTO
|
681 FE_CAN_2G_MODULATION
|
685 .get_tune_settings
= si2168_get_tune_settings
,
688 .sleep
= si2168_sleep
,
690 .set_frontend
= si2168_set_frontend
,
692 .read_status
= si2168_read_status
,
695 static int si2168_probe(struct i2c_client
*client
,
696 const struct i2c_device_id
*id
)
698 struct si2168_config
*config
= client
->dev
.platform_data
;
699 struct si2168_dev
*dev
;
702 dev_dbg(&client
->dev
, "\n");
704 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
707 dev_err(&client
->dev
, "kzalloc() failed\n");
711 /* create mux i2c adapter for tuner */
712 dev
->adapter
= i2c_add_mux_adapter(client
->adapter
, &client
->dev
,
713 client
, 0, 0, 0, si2168_select
, si2168_deselect
);
714 if (dev
->adapter
== NULL
) {
719 /* create dvb_frontend */
720 memcpy(&dev
->fe
.ops
, &si2168_ops
, sizeof(struct dvb_frontend_ops
));
721 dev
->fe
.demodulator_priv
= client
;
722 *config
->i2c_adapter
= dev
->adapter
;
723 *config
->fe
= &dev
->fe
;
724 dev
->ts_mode
= config
->ts_mode
;
725 dev
->ts_clock_inv
= config
->ts_clock_inv
;
726 dev
->ts_clock_gapped
= config
->ts_clock_gapped
;
727 dev
->fw_loaded
= false;
729 i2c_set_clientdata(client
, dev
);
731 dev_info(&client
->dev
, "Silicon Labs Si2168 successfully attached\n");
736 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
740 static int si2168_remove(struct i2c_client
*client
)
742 struct si2168_dev
*dev
= i2c_get_clientdata(client
);
744 dev_dbg(&client
->dev
, "\n");
746 i2c_del_mux_adapter(dev
->adapter
);
748 dev
->fe
.ops
.release
= NULL
;
749 dev
->fe
.demodulator_priv
= NULL
;
756 static const struct i2c_device_id si2168_id_table
[] = {
760 MODULE_DEVICE_TABLE(i2c
, si2168_id_table
);
762 static struct i2c_driver si2168_driver
= {
766 .probe
= si2168_probe
,
767 .remove
= si2168_remove
,
768 .id_table
= si2168_id_table
,
771 module_i2c_driver(si2168_driver
);
773 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
774 MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
775 MODULE_LICENSE("GPL");
776 MODULE_FIRMWARE(SI2168_A20_FIRMWARE
);
777 MODULE_FIRMWARE(SI2168_A30_FIRMWARE
);
778 MODULE_FIRMWARE(SI2168_B40_FIRMWARE
);