blob: 329004fbec76cddfbdf9e6392365ded607df6e90 [file] [log] [blame]
Antti Palosaariba92ae02014-04-14 21:51:32 -03001/*
Olli Salonen1b923732014-07-13 10:52:20 -03002 * Silicon Labs Si2157/2158 silicon tuner driver
Antti Palosaariba92ae02014-04-14 21:51:32 -03003 *
4 * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
5 *
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.
10 *
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.
15 */
16
Antti Palosaari930a8732014-04-10 21:58:10 -030017#include "si2157_priv.h"
18
Olli Salonen1b923732014-07-13 10:52:20 -030019static const struct dvb_tuner_ops si2157_ops;
20
Antti Palosaari930a8732014-04-10 21:58:10 -030021/* execute firmware command */
22static int si2157_cmd_execute(struct si2157 *s, struct si2157_cmd *cmd)
23{
24 int ret;
Antti Palosaari930a8732014-04-10 21:58:10 -030025 unsigned long timeout;
26
27 mutex_lock(&s->i2c_mutex);
28
Antti Palosaarie6b43802014-07-10 06:02:53 -030029 if (cmd->wlen) {
Antti Palosaari930a8732014-04-10 21:58:10 -030030 /* write cmd and args for firmware */
Antti Palosaarie6b43802014-07-10 06:02:53 -030031 ret = i2c_master_send(s->client, cmd->args, cmd->wlen);
Antti Palosaari930a8732014-04-10 21:58:10 -030032 if (ret < 0) {
33 goto err_mutex_unlock;
Antti Palosaarie6b43802014-07-10 06:02:53 -030034 } else if (ret != cmd->wlen) {
Antti Palosaari930a8732014-04-10 21:58:10 -030035 ret = -EREMOTEIO;
36 goto err_mutex_unlock;
37 }
38 }
39
Antti Palosaarie6b43802014-07-10 06:02:53 -030040 if (cmd->rlen) {
41 /* wait cmd execution terminate */
42 #define TIMEOUT 80
43 timeout = jiffies + msecs_to_jiffies(TIMEOUT);
44 while (!time_after(jiffies, timeout)) {
45 ret = i2c_master_recv(s->client, cmd->args, cmd->rlen);
46 if (ret < 0) {
47 goto err_mutex_unlock;
48 } else if (ret != cmd->rlen) {
49 ret = -EREMOTEIO;
50 goto err_mutex_unlock;
51 }
52
53 /* firmware ready? */
54 if ((cmd->args[0] >> 7) & 0x01)
55 break;
Antti Palosaari930a8732014-04-10 21:58:10 -030056 }
57
Antti Palosaarie6b43802014-07-10 06:02:53 -030058 dev_dbg(&s->client->dev, "%s: cmd execution took %d ms\n",
59 __func__,
60 jiffies_to_msecs(jiffies) -
61 (jiffies_to_msecs(timeout) - TIMEOUT));
62
63 if (!((cmd->args[0] >> 7) & 0x01)) {
64 ret = -ETIMEDOUT;
65 goto err_mutex_unlock;
66 }
Antti Palosaari930a8732014-04-10 21:58:10 -030067 }
68
Antti Palosaarie6b43802014-07-10 06:02:53 -030069 ret = 0;
Antti Palosaari930a8732014-04-10 21:58:10 -030070
71err_mutex_unlock:
72 mutex_unlock(&s->i2c_mutex);
73 if (ret)
74 goto err;
75
76 return 0;
77err:
78 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
79 return ret;
80}
81
82static int si2157_init(struct dvb_frontend *fe)
83{
84 struct si2157 *s = fe->tuner_priv;
Antti Palosaari7d6bc602014-07-13 20:05:28 -030085 int ret, len, remaining;
Olli Salonenb1541212014-07-13 10:52:19 -030086 struct si2157_cmd cmd;
Olli Salonen1b923732014-07-13 10:52:20 -030087 const struct firmware *fw = NULL;
88 u8 *fw_file;
Antti Palosaari7d6bc602014-07-13 20:05:28 -030089 unsigned int chip_id;
Antti Palosaari930a8732014-04-10 21:58:10 -030090
91 dev_dbg(&s->client->dev, "%s:\n", __func__);
92
Olli Salonenb1541212014-07-13 10:52:19 -030093 /* configure? */
94 memcpy(cmd.args, "\xc0\x00\x0c\x00\x00\x01\x01\x01\x01\x01\x01\x02\x00\x00\x01", 15);
95 cmd.wlen = 15;
96 cmd.rlen = 1;
97 ret = si2157_cmd_execute(s, &cmd);
98 if (ret)
99 goto err;
100
101 /* query chip revision */
102 memcpy(cmd.args, "\x02", 1);
103 cmd.wlen = 1;
104 cmd.rlen = 13;
105 ret = si2157_cmd_execute(s, &cmd);
106 if (ret)
107 goto err;
108
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300109 chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
110 cmd.args[4] << 0;
Olli Salonen1b923732014-07-13 10:52:20 -0300111
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300112 #define SI2158_A20 ('A' << 24 | 58 << 16 | '2' << 8 | '0' << 0)
113 #define SI2157_A30 ('A' << 24 | 57 << 16 | '3' << 8 | '0' << 0)
Olli Salonen1b923732014-07-13 10:52:20 -0300114
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300115 switch (chip_id) {
116 case SI2158_A20:
117 fw_file = SI2158_A20_FIRMWARE;
118 break;
119 case SI2157_A30:
120 goto skip_fw_download;
121 break;
122 default:
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]);
127 ret = -EINVAL;
128 goto err;
Olli Salonen1b923732014-07-13 10:52:20 -0300129 }
130
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300131 /* 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);
134
135 /* request the firmware, this will block and timeout */
136 ret = request_firmware(&fw, fw_file, &s->client->dev);
137 if (ret) {
138 dev_err(&s->client->dev, "%s: firmware file '%s' not found\n",
139 KBUILD_MODNAME, fw_file);
140 goto err;
141 }
142
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);
147 ret = -EINVAL;
148 goto err;
149 }
150
151 dev_info(&s->client->dev, "%s: downloading firmware from file '%s'\n",
152 KBUILD_MODNAME, fw_file);
153
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);
157 cmd.wlen = len;
158 cmd.rlen = 1;
159 ret = si2157_cmd_execute(s, &cmd);
160 if (ret) {
161 dev_err(&s->client->dev,
162 "%s: firmware download failed=%d\n",
163 KBUILD_MODNAME, ret);
164 goto err;
165 }
166 }
167
168 release_firmware(fw);
169 fw = NULL;
170
171skip_fw_download:
Olli Salonenb1541212014-07-13 10:52:19 -0300172 /* reboot the tuner with new firmware? */
173 memcpy(cmd.args, "\x01\x01", 2);
174 cmd.wlen = 2;
175 cmd.rlen = 1;
176 ret = si2157_cmd_execute(s, &cmd);
177 if (ret)
178 goto err;
179
Antti Palosaari930a8732014-04-10 21:58:10 -0300180 s->active = true;
181
182 return 0;
Olli Salonenb1541212014-07-13 10:52:19 -0300183err:
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300184 if (fw)
185 release_firmware(fw);
186
Olli Salonenb1541212014-07-13 10:52:19 -0300187 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
188 return ret;
Antti Palosaari930a8732014-04-10 21:58:10 -0300189}
190
191static int si2157_sleep(struct dvb_frontend *fe)
192{
193 struct si2157 *s = fe->tuner_priv;
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300194 int ret;
195 struct si2157_cmd cmd;
Antti Palosaari930a8732014-04-10 21:58:10 -0300196
197 dev_dbg(&s->client->dev, "%s:\n", __func__);
198
199 s->active = false;
200
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300201 memcpy(cmd.args, "\x13", 1);
Antti Palosaarie6b43802014-07-10 06:02:53 -0300202 cmd.wlen = 1;
203 cmd.rlen = 0;
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300204 ret = si2157_cmd_execute(s, &cmd);
205 if (ret)
206 goto err;
207
Antti Palosaari930a8732014-04-10 21:58:10 -0300208 return 0;
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300209err:
210 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
211 return ret;
Antti Palosaari930a8732014-04-10 21:58:10 -0300212}
213
214static int si2157_set_params(struct dvb_frontend *fe)
215{
216 struct si2157 *s = fe->tuner_priv;
217 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
218 int ret;
219 struct si2157_cmd cmd;
Olli Salonena1dad502014-07-13 10:52:21 -0300220 u8 bandwidth, delivery_system;
Antti Palosaari930a8732014-04-10 21:58:10 -0300221
222 dev_dbg(&s->client->dev,
223 "%s: delivery_system=%d frequency=%u bandwidth_hz=%u\n",
224 __func__, c->delivery_system, c->frequency,
225 c->bandwidth_hz);
226
227 if (!s->active) {
228 ret = -EAGAIN;
229 goto err;
230 }
231
Olli Salonena1dad502014-07-13 10:52:21 -0300232 if (c->bandwidth_hz <= 6000000)
233 bandwidth = 0x06;
234 else if (c->bandwidth_hz <= 7000000)
235 bandwidth = 0x07;
236 else if (c->bandwidth_hz <= 8000000)
237 bandwidth = 0x08;
238 else
239 bandwidth = 0x0f;
240
241 switch (c->delivery_system) {
242 case SYS_DVBT:
243 case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */
244 delivery_system = 0x20;
245 break;
246 case SYS_DVBC_ANNEX_A:
247 delivery_system = 0x30;
248 break;
249 default:
250 ret = -EINVAL;
251 goto err;
252 }
253
254 memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6);
255 cmd.args[4] = delivery_system | bandwidth;
256 cmd.wlen = 6;
257 cmd.rlen = 1;
258 ret = si2157_cmd_execute(s, &cmd);
259 if (ret)
260 goto err;
261
Antti Palosaari930a8732014-04-10 21:58:10 -0300262 /* set frequency */
Olli Salonenb1541212014-07-13 10:52:19 -0300263 memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8);
Antti Palosaari930a8732014-04-10 21:58:10 -0300264 cmd.args[4] = (c->frequency >> 0) & 0xff;
265 cmd.args[5] = (c->frequency >> 8) & 0xff;
266 cmd.args[6] = (c->frequency >> 16) & 0xff;
267 cmd.args[7] = (c->frequency >> 24) & 0xff;
Antti Palosaarie6b43802014-07-10 06:02:53 -0300268 cmd.wlen = 8;
269 cmd.rlen = 1;
Antti Palosaari930a8732014-04-10 21:58:10 -0300270 ret = si2157_cmd_execute(s, &cmd);
271 if (ret)
272 goto err;
273
274 return 0;
275err:
276 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
277 return ret;
278}
279
280static const struct dvb_tuner_ops si2157_tuner_ops = {
281 .info = {
Olli Salonen1b923732014-07-13 10:52:20 -0300282 .name = "Silicon Labs Si2157/Si2158",
Antti Palosaariae4c8912014-04-12 01:53:51 -0300283 .frequency_min = 110000000,
Antti Palosaari930a8732014-04-10 21:58:10 -0300284 .frequency_max = 862000000,
285 },
286
287 .init = si2157_init,
288 .sleep = si2157_sleep,
289 .set_params = si2157_set_params,
290};
291
292static int si2157_probe(struct i2c_client *client,
293 const struct i2c_device_id *id)
294{
295 struct si2157_config *cfg = client->dev.platform_data;
296 struct dvb_frontend *fe = cfg->fe;
297 struct si2157 *s;
298 struct si2157_cmd cmd;
299 int ret;
300
301 s = kzalloc(sizeof(struct si2157), GFP_KERNEL);
302 if (!s) {
303 ret = -ENOMEM;
304 dev_err(&client->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
305 goto err;
306 }
307
308 s->client = client;
309 s->fe = cfg->fe;
310 mutex_init(&s->i2c_mutex);
311
312 /* check if the tuner is there */
Antti Palosaarie6b43802014-07-10 06:02:53 -0300313 cmd.wlen = 0;
314 cmd.rlen = 1;
Antti Palosaari930a8732014-04-10 21:58:10 -0300315 ret = si2157_cmd_execute(s, &cmd);
316 if (ret)
317 goto err;
318
319 fe->tuner_priv = s;
320 memcpy(&fe->ops.tuner_ops, &si2157_tuner_ops,
321 sizeof(struct dvb_tuner_ops));
322
323 i2c_set_clientdata(client, s);
324
325 dev_info(&s->client->dev,
Olli Salonen1b923732014-07-13 10:52:20 -0300326 "%s: Silicon Labs Si2157/Si2158 successfully attached\n",
Antti Palosaari930a8732014-04-10 21:58:10 -0300327 KBUILD_MODNAME);
328 return 0;
329err:
330 dev_dbg(&client->dev, "%s: failed=%d\n", __func__, ret);
331 kfree(s);
332
333 return ret;
334}
335
336static int si2157_remove(struct i2c_client *client)
337{
338 struct si2157 *s = i2c_get_clientdata(client);
339 struct dvb_frontend *fe = s->fe;
340
341 dev_dbg(&client->dev, "%s:\n", __func__);
342
343 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
344 fe->tuner_priv = NULL;
345 kfree(s);
346
347 return 0;
348}
349
350static const struct i2c_device_id si2157_id[] = {
351 {"si2157", 0},
352 {}
353};
354MODULE_DEVICE_TABLE(i2c, si2157_id);
355
356static struct i2c_driver si2157_driver = {
357 .driver = {
358 .owner = THIS_MODULE,
359 .name = "si2157",
360 },
361 .probe = si2157_probe,
362 .remove = si2157_remove,
363 .id_table = si2157_id,
364};
365
366module_i2c_driver(si2157_driver);
367
Olli Salonen1b923732014-07-13 10:52:20 -0300368MODULE_DESCRIPTION("Silicon Labs Si2157/Si2158 silicon tuner driver");
Antti Palosaari930a8732014-04-10 21:58:10 -0300369MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
370MODULE_LICENSE("GPL");
Antti Palosaaribac53a22014-07-13 16:13:49 -0300371MODULE_FIRMWARE(SI2158_A20_FIRMWARE);