2 * Silicon Labs Si2157/2158 silicon tuner 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 "si2157_priv.h"
19 static const struct dvb_tuner_ops si2157_ops;
21 /* execute firmware command */
22 static int si2157_cmd_execute(struct si2157 *s, struct si2157_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 si2157_init(struct dvb_frontend *fe)
84 struct si2157 *s = fe->tuner_priv;
85 int ret, len, remaining;
86 struct si2157_cmd cmd;
87 const struct firmware *fw = NULL;
91 dev_dbg(&s->client->dev, "%s:\n", __func__);
94 memcpy(cmd.args, "\xc0\x00\x0c\x00\x00\x01\x01\x01\x01\x01\x01\x02\x00\x00\x01", 15);
97 ret = si2157_cmd_execute(s, &cmd);
101 /* query chip revision */
102 memcpy(cmd.args, "\x02", 1);
105 ret = si2157_cmd_execute(s, &cmd);
109 chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
112 #define SI2158_A20 ('A' << 24 | 58 << 16 | '2' << 8 | '0' << 0)
113 #define SI2157_A30 ('A' << 24 | 57 << 16 | '3' << 8 | '0' << 0)
117 fw_file = SI2158_A20_FIRMWARE;
120 goto skip_fw_download;
123 dev_err(&s->client->dev,
124 "%s: unkown chip version Si21%d-%c%c%c\n",
125 KBUILD_MODNAME, cmd.args[2], cmd.args[1],
126 cmd.args[3], cmd.args[4]);
131 /* cold state - try to download firmware */
132 dev_info(&s->client->dev, "%s: found a '%s' in cold state\n",
133 KBUILD_MODNAME, si2157_ops.info.name);
135 /* request the firmware, this will block and timeout */
136 ret = request_firmware(&fw, fw_file, &s->client->dev);
138 dev_err(&s->client->dev, "%s: firmware file '%s' not found\n",
139 KBUILD_MODNAME, fw_file);
143 /* firmware should be n chunks of 17 bytes */
144 if (fw->size % 17 != 0) {
145 dev_err(&s->client->dev, "%s: firmware file '%s' is invalid\n",
146 KBUILD_MODNAME, fw_file);
151 dev_info(&s->client->dev, "%s: downloading firmware from file '%s'\n",
152 KBUILD_MODNAME, fw_file);
154 for (remaining = fw->size; remaining > 0; remaining -= 17) {
155 len = fw->data[fw->size - remaining];
156 memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
159 ret = si2157_cmd_execute(s, &cmd);
161 dev_err(&s->client->dev,
162 "%s: firmware download failed=%d\n",
163 KBUILD_MODNAME, ret);
168 release_firmware(fw);
172 /* reboot the tuner with new firmware? */
173 memcpy(cmd.args, "\x01\x01", 2);
176 ret = si2157_cmd_execute(s, &cmd);
185 release_firmware(fw);
187 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
191 static int si2157_sleep(struct dvb_frontend *fe)
193 struct si2157 *s = fe->tuner_priv;
195 struct si2157_cmd cmd;
197 dev_dbg(&s->client->dev, "%s:\n", __func__);
201 memcpy(cmd.args, "\x13", 1);
204 ret = si2157_cmd_execute(s, &cmd);
210 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
214 static int si2157_set_params(struct dvb_frontend *fe)
216 struct si2157 *s = fe->tuner_priv;
217 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
219 struct si2157_cmd cmd;
220 u8 bandwidth, delivery_system;
222 dev_dbg(&s->client->dev,
223 "%s: delivery_system=%d frequency=%u bandwidth_hz=%u\n",
224 __func__, c->delivery_system, c->frequency,
232 if (c->bandwidth_hz <= 6000000)
234 else if (c->bandwidth_hz <= 7000000)
236 else if (c->bandwidth_hz <= 8000000)
241 switch (c->delivery_system) {
243 case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */
244 delivery_system = 0x20;
246 case SYS_DVBC_ANNEX_A:
247 delivery_system = 0x30;
254 memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6);
255 cmd.args[4] = delivery_system | bandwidth;
260 ret = si2157_cmd_execute(s, &cmd);
265 memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8);
266 cmd.args[4] = (c->frequency >> 0) & 0xff;
267 cmd.args[5] = (c->frequency >> 8) & 0xff;
268 cmd.args[6] = (c->frequency >> 16) & 0xff;
269 cmd.args[7] = (c->frequency >> 24) & 0xff;
272 ret = si2157_cmd_execute(s, &cmd);
278 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
282 static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
284 *frequency = 5000000; /* default value of property 0x0706 */
288 static const struct dvb_tuner_ops si2157_ops = {
290 .name = "Silicon Labs Si2157/Si2158",
291 .frequency_min = 110000000,
292 .frequency_max = 862000000,
296 .sleep = si2157_sleep,
297 .set_params = si2157_set_params,
298 .get_if_frequency = si2157_get_if_frequency,
301 static int si2157_probe(struct i2c_client *client,
302 const struct i2c_device_id *id)
304 struct si2157_config *cfg = client->dev.platform_data;
305 struct dvb_frontend *fe = cfg->fe;
307 struct si2157_cmd cmd;
310 s = kzalloc(sizeof(struct si2157), GFP_KERNEL);
313 dev_err(&client->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
319 s->inversion = cfg->inversion;
320 mutex_init(&s->i2c_mutex);
322 /* check if the tuner is there */
325 ret = si2157_cmd_execute(s, &cmd);
330 memcpy(&fe->ops.tuner_ops, &si2157_ops,
331 sizeof(struct dvb_tuner_ops));
333 i2c_set_clientdata(client, s);
335 dev_info(&s->client->dev,
336 "%s: Silicon Labs Si2157/Si2158 successfully attached\n",
340 dev_dbg(&client->dev, "%s: failed=%d\n", __func__, ret);
346 static int si2157_remove(struct i2c_client *client)
348 struct si2157 *s = i2c_get_clientdata(client);
349 struct dvb_frontend *fe = s->fe;
351 dev_dbg(&client->dev, "%s:\n", __func__);
353 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
354 fe->tuner_priv = NULL;
360 static const struct i2c_device_id si2157_id[] = {
364 MODULE_DEVICE_TABLE(i2c, si2157_id);
366 static struct i2c_driver si2157_driver = {
368 .owner = THIS_MODULE,
371 .probe = si2157_probe,
372 .remove = si2157_remove,
373 .id_table = si2157_id,
376 module_i2c_driver(si2157_driver);
378 MODULE_DESCRIPTION("Silicon Labs Si2157/Si2158 silicon tuner driver");
379 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
380 MODULE_LICENSE("GPL");
381 MODULE_FIRMWARE(SI2158_A20_FIRMWARE);