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
) {
103 case SYS_DVBC_ANNEX_A
:
120 ret
= si2168_cmd_execute(s
, &cmd
);
125 * Possible values seen, in order from strong signal to weak:
126 * 16 0001 0110 full lock
127 * 1e 0001 1110 partial lock
128 * 1a 0001 1010 partial lock
129 * 18 0001 1000 no lock
132 * [b4] statistics ready? Set in a few secs after lock is gained.
135 switch ((cmd
.args
[2] >> 1) & 0x03) {
137 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
140 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
|
141 FE_HAS_SYNC
| FE_HAS_LOCK
;
145 s
->fe_status
= *status
;
147 dev_dbg(&s
->client
->dev
, "%s: status=%02x args=%*ph\n",
148 __func__
, *status
, cmd
.rlen
, cmd
.args
);
152 dev_dbg(&s
->client
->dev
, "%s: failed=%d\n", __func__
, ret
);
156 static int si2168_set_frontend(struct dvb_frontend
*fe
)
158 struct si2168
*s
= fe
->demodulator_priv
;
159 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
161 struct si2168_cmd cmd
;
162 u8 bandwidth
, delivery_system
;
164 dev_dbg(&s
->client
->dev
,
165 "%s: delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u\n",
166 __func__
, c
->delivery_system
, c
->modulation
,
167 c
->frequency
, c
->bandwidth_hz
, c
->symbol_rate
,
175 switch (c
->delivery_system
) {
177 delivery_system
= 0x20;
179 case SYS_DVBC_ANNEX_A
:
180 delivery_system
= 0x30;
183 delivery_system
= 0x70;
190 if (c
->bandwidth_hz
<= 5000000)
192 else if (c
->bandwidth_hz
<= 6000000)
194 else if (c
->bandwidth_hz
<= 7000000)
196 else if (c
->bandwidth_hz
<= 8000000)
198 else if (c
->bandwidth_hz
<= 9000000)
200 else if (c
->bandwidth_hz
<= 10000000)
206 if (fe
->ops
.tuner_ops
.set_params
) {
207 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
212 memcpy(cmd
.args
, "\x88\x02\x02\x02\x02", 5);
215 ret
= si2168_cmd_execute(s
, &cmd
);
219 /* that has no big effect */
220 if (c
->delivery_system
== SYS_DVBT
)
221 memcpy(cmd
.args
, "\x89\x21\x06\x11\xff\x98", 6);
222 else if (c
->delivery_system
== SYS_DVBC_ANNEX_A
)
223 memcpy(cmd
.args
, "\x89\x21\x06\x11\x89\xf0", 6);
224 else if (c
->delivery_system
== SYS_DVBT2
)
225 memcpy(cmd
.args
, "\x89\x21\x06\x11\x89\x20", 6);
228 ret
= si2168_cmd_execute(s
, &cmd
);
232 memcpy(cmd
.args
, "\x51\x03", 2);
235 ret
= si2168_cmd_execute(s
, &cmd
);
239 memcpy(cmd
.args
, "\x12\x08\x04", 3);
242 ret
= si2168_cmd_execute(s
, &cmd
);
246 memcpy(cmd
.args
, "\x14\x00\x01\x04\x00\x00", 6);
249 ret
= si2168_cmd_execute(s
, &cmd
);
253 memcpy(cmd
.args
, "\x14\x00\x03\x10\x17\x00", 6);
256 ret
= si2168_cmd_execute(s
, &cmd
);
260 memcpy(cmd
.args
, "\x14\x00\x02\x10\x15\x00", 6);
263 ret
= si2168_cmd_execute(s
, &cmd
);
267 memcpy(cmd
.args
, "\x14\x00\x0c\x10\x12\x00", 6);
270 ret
= si2168_cmd_execute(s
, &cmd
);
274 memcpy(cmd
.args
, "\x14\x00\x06\x10\x24\x00", 6);
277 ret
= si2168_cmd_execute(s
, &cmd
);
281 memcpy(cmd
.args
, "\x14\x00\x0b\x10\x88\x13", 6);
284 ret
= si2168_cmd_execute(s
, &cmd
);
288 memcpy(cmd
.args
, "\x14\x00\x07\x10\x00\x24", 6);
291 ret
= si2168_cmd_execute(s
, &cmd
);
295 memcpy(cmd
.args
, "\x14\x00\x0a\x10\x00\x00", 6);
296 cmd
.args
[4] = delivery_system
| bandwidth
;
299 ret
= si2168_cmd_execute(s
, &cmd
);
303 memcpy(cmd
.args
, "\x14\x00\x04\x10\x15\x00", 6);
306 ret
= si2168_cmd_execute(s
, &cmd
);
310 memcpy(cmd
.args
, "\x14\x00\x05\x10\xa1\x00", 6);
313 ret
= si2168_cmd_execute(s
, &cmd
);
317 memcpy(cmd
.args
, "\x14\x00\x0f\x10\x10\x00", 6);
320 ret
= si2168_cmd_execute(s
, &cmd
);
324 memcpy(cmd
.args
, "\x14\x00\x0d\x10\xd0\x02", 6);
327 ret
= si2168_cmd_execute(s
, &cmd
);
331 memcpy(cmd
.args
, "\x14\x00\x01\x10\x00\x00", 6);
334 ret
= si2168_cmd_execute(s
, &cmd
);
338 memcpy(cmd
.args
, "\x14\x00\x09\x10\xe3\x18", 6);
341 ret
= si2168_cmd_execute(s
, &cmd
);
345 memcpy(cmd
.args
, "\x14\x00\x08\x10\xd7\x15", 6);
348 ret
= si2168_cmd_execute(s
, &cmd
);
352 memcpy(cmd
.args
, "\x14\x00\x04\x03\x00\x00", 6);
355 ret
= si2168_cmd_execute(s
, &cmd
);
359 memcpy(cmd
.args
, "\x14\x00\x03\x03\x00\x00", 6);
362 ret
= si2168_cmd_execute(s
, &cmd
);
366 memcpy(cmd
.args
, "\x14\x00\x08\x03\x00\x00", 6);
369 ret
= si2168_cmd_execute(s
, &cmd
);
373 memcpy(cmd
.args
, "\x14\x00\x07\x03\x01\x02", 6);
376 ret
= si2168_cmd_execute(s
, &cmd
);
380 memcpy(cmd
.args
, "\x14\x00\x06\x03\x00\x00", 6);
383 ret
= si2168_cmd_execute(s
, &cmd
);
387 memcpy(cmd
.args
, "\x14\x00\x05\x03\x00\x00", 6);
390 ret
= si2168_cmd_execute(s
, &cmd
);
394 memcpy(cmd
.args
, "\x14\x00\x01\x03\x0c\x40", 6);
397 ret
= si2168_cmd_execute(s
, &cmd
);
401 memcpy(cmd
.args
, "\x14\x00\x01\x10\x16\x00", 6);
404 ret
= si2168_cmd_execute(s
, &cmd
);
408 memcpy(cmd
.args
, "\x14\x00\x01\x12\x00\x00", 6);
411 ret
= si2168_cmd_execute(s
, &cmd
);
418 ret
= si2168_cmd_execute(s
, &cmd
);
422 s
->delivery_system
= c
->delivery_system
;
426 dev_dbg(&s
->client
->dev
, "%s: failed=%d\n", __func__
, ret
);
430 static int si2168_init(struct dvb_frontend
*fe
)
432 struct si2168
*s
= fe
->demodulator_priv
;
433 int ret
, len
, remaining
;
434 const struct firmware
*fw
= NULL
;
435 u8
*fw_file
= SI2168_FIRMWARE
;
436 const unsigned int i2c_wr_max
= 8;
437 struct si2168_cmd cmd
;
439 dev_dbg(&s
->client
->dev
, "%s:\n", __func__
);
444 ret
= si2168_cmd_execute(s
, &cmd
);
463 ret
= si2168_cmd_execute(s
, &cmd
);
477 ret
= si2168_cmd_execute(s
, &cmd
);
484 ret
= si2168_cmd_execute(s
, &cmd
);
488 /* cold state - try to download firmware */
489 dev_info(&s
->client
->dev
, "%s: found a '%s' in cold state\n",
490 KBUILD_MODNAME
, si2168_ops
.info
.name
);
492 /* request the firmware, this will block and timeout */
493 ret
= request_firmware(&fw
, fw_file
, &s
->client
->dev
);
495 dev_err(&s
->client
->dev
, "%s: firmare file '%s' not found\n",
496 KBUILD_MODNAME
, fw_file
);
500 dev_info(&s
->client
->dev
, "%s: downloading firmware from file '%s'\n",
501 KBUILD_MODNAME
, fw_file
);
503 for (remaining
= fw
->size
; remaining
> 0; remaining
-= i2c_wr_max
) {
505 if (len
> i2c_wr_max
)
508 memcpy(cmd
.args
, &fw
->data
[fw
->size
- remaining
], len
);
511 ret
= si2168_cmd_execute(s
, &cmd
);
513 dev_err(&s
->client
->dev
,
514 "%s: firmware download failed=%d\n",
515 KBUILD_MODNAME
, ret
);
520 release_firmware(fw
);
527 ret
= si2168_cmd_execute(s
, &cmd
);
531 dev_info(&s
->client
->dev
, "%s: found a '%s' in warm state\n",
532 KBUILD_MODNAME
, si2168_ops
.info
.name
);
539 release_firmware(fw
);
541 dev_dbg(&s
->client
->dev
, "%s: failed=%d\n", __func__
, ret
);
545 static int si2168_sleep(struct dvb_frontend
*fe
)
547 struct si2168
*s
= fe
->demodulator_priv
;
549 dev_dbg(&s
->client
->dev
, "%s:\n", __func__
);
556 static int si2168_get_tune_settings(struct dvb_frontend
*fe
,
557 struct dvb_frontend_tune_settings
*s
)
559 s
->min_delay_ms
= 900;
566 * We must use unlocked i2c_transfer() here because I2C lock is already taken
569 static int si2168_select(struct i2c_adapter
*adap
, void *mux_priv
, u32 chan
)
571 struct si2168
*s
= mux_priv
;
573 struct i2c_msg gate_open_msg
= {
574 .addr
= s
->client
->addr
,
577 .buf
= "\xc0\x0d\x01",
580 mutex_lock(&s
->i2c_mutex
);
582 /* open tuner I2C gate */
583 ret
= __i2c_transfer(s
->client
->adapter
, &gate_open_msg
, 1);
585 dev_warn(&s
->client
->dev
, "%s: i2c write failed=%d\n",
586 KBUILD_MODNAME
, ret
);
596 static int si2168_deselect(struct i2c_adapter
*adap
, void *mux_priv
, u32 chan
)
598 struct si2168
*s
= mux_priv
;
600 struct i2c_msg gate_close_msg
= {
601 .addr
= s
->client
->addr
,
604 .buf
= "\xc0\x0d\x00",
607 /* close tuner I2C gate */
608 ret
= __i2c_transfer(s
->client
->adapter
, &gate_close_msg
, 1);
610 dev_warn(&s
->client
->dev
, "%s: i2c write failed=%d\n",
611 KBUILD_MODNAME
, ret
);
618 mutex_unlock(&s
->i2c_mutex
);
623 static const struct dvb_frontend_ops si2168_ops
= {
624 .delsys
= {SYS_DVBT
, SYS_DVBT2
, SYS_DVBC_ANNEX_A
},
626 .name
= "Silicon Labs Si2168",
627 .caps
= FE_CAN_FEC_1_2
|
640 FE_CAN_TRANSMISSION_MODE_AUTO
|
641 FE_CAN_GUARD_INTERVAL_AUTO
|
642 FE_CAN_HIERARCHY_AUTO
|
647 .get_tune_settings
= si2168_get_tune_settings
,
650 .sleep
= si2168_sleep
,
652 .set_frontend
= si2168_set_frontend
,
654 .read_status
= si2168_read_status
,
657 static int si2168_probe(struct i2c_client
*client
,
658 const struct i2c_device_id
*id
)
660 struct si2168_config
*config
= client
->dev
.platform_data
;
663 struct si2168_cmd cmd
;
665 dev_dbg(&client
->dev
, "%s:\n", __func__
);
667 s
= kzalloc(sizeof(struct si2168
), GFP_KERNEL
);
670 dev_err(&client
->dev
, "%s: kzalloc() failed\n", KBUILD_MODNAME
);
675 mutex_init(&s
->i2c_mutex
);
677 /* check if the demod is there */
680 ret
= si2168_cmd_execute(s
, &cmd
);
684 /* create mux i2c adapter for tuner */
685 s
->adapter
= i2c_add_mux_adapter(client
->adapter
, &client
->dev
, s
,
686 0, 0, 0, si2168_select
, si2168_deselect
);
687 if (s
->adapter
== NULL
)
690 /* create dvb_frontend */
691 memcpy(&s
->fe
.ops
, &si2168_ops
, sizeof(struct dvb_frontend_ops
));
692 s
->fe
.demodulator_priv
= s
;
694 *config
->i2c_adapter
= s
->adapter
;
695 *config
->fe
= &s
->fe
;
697 i2c_set_clientdata(client
, s
);
699 dev_info(&s
->client
->dev
,
700 "%s: Silicon Labs Si2168 successfully attached\n",
705 dev_dbg(&client
->dev
, "%s: failed=%d\n", __func__
, ret
);
709 static int si2168_remove(struct i2c_client
*client
)
711 struct si2168
*s
= i2c_get_clientdata(client
);
713 dev_dbg(&client
->dev
, "%s:\n", __func__
);
715 i2c_del_mux_adapter(s
->adapter
);
717 s
->fe
.ops
.release
= NULL
;
718 s
->fe
.demodulator_priv
= NULL
;
725 static const struct i2c_device_id si2168_id
[] = {
729 MODULE_DEVICE_TABLE(i2c
, si2168_id
);
731 static struct i2c_driver si2168_driver
= {
733 .owner
= THIS_MODULE
,
736 .probe
= si2168_probe
,
737 .remove
= si2168_remove
,
738 .id_table
= si2168_id
,
741 module_i2c_driver(si2168_driver
);
743 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
744 MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
745 MODULE_LICENSE("GPL");
746 MODULE_FIRMWARE(SI2168_FIRMWARE
);