blob: 3bdf00a0ca0e34aafd651b70d781f00d6a9c956c [file] [log] [blame]
Antti Palosaariba92ae02014-04-14 21:51:32 -03001/*
Olli Salonen073f3842014-11-24 03:57:33 -03002 * Silicon Labs Si2146/2147/2157/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
Olli Salonen67d01132014-08-05 09:03:54 -030058 dev_dbg(&s->client->dev, "cmd execution took %d ms\n",
Antti Palosaarie6b43802014-07-10 06:02:53 -030059 jiffies_to_msecs(jiffies) -
60 (jiffies_to_msecs(timeout) - TIMEOUT));
61
62 if (!((cmd->args[0] >> 7) & 0x01)) {
63 ret = -ETIMEDOUT;
64 goto err_mutex_unlock;
65 }
Antti Palosaari930a8732014-04-10 21:58:10 -030066 }
67
Antti Palosaarie6b43802014-07-10 06:02:53 -030068 ret = 0;
Antti Palosaari930a8732014-04-10 21:58:10 -030069
70err_mutex_unlock:
71 mutex_unlock(&s->i2c_mutex);
72 if (ret)
73 goto err;
74
75 return 0;
76err:
Olli Salonen67d01132014-08-05 09:03:54 -030077 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaari930a8732014-04-10 21:58:10 -030078 return ret;
79}
80
81static int si2157_init(struct dvb_frontend *fe)
82{
83 struct si2157 *s = fe->tuner_priv;
Antti Palosaari7d6bc602014-07-13 20:05:28 -030084 int ret, len, remaining;
Olli Salonenb1541212014-07-13 10:52:19 -030085 struct si2157_cmd cmd;
Olli Salonen1b923732014-07-13 10:52:20 -030086 const struct firmware *fw = NULL;
87 u8 *fw_file;
Antti Palosaari7d6bc602014-07-13 20:05:28 -030088 unsigned int chip_id;
Antti Palosaari930a8732014-04-10 21:58:10 -030089
Olli Salonen67d01132014-08-05 09:03:54 -030090 dev_dbg(&s->client->dev, "\n");
Antti Palosaari930a8732014-04-10 21:58:10 -030091
Olli Salonen4cbf6ed2014-08-25 15:07:03 -030092 if (s->fw_loaded)
93 goto warm;
94
95 /* power up */
Olli Salonen073f3842014-11-24 03:57:33 -030096 if (s->chiptype == SI2157_CHIPTYPE_SI2146) {
97 memcpy(cmd.args, "\xc0\x05\x01\x00\x00\x0b\x00\x00\x01", 9);
98 cmd.wlen = 9;
99 } else {
100 memcpy(cmd.args, "\xc0\x00\x0c\x00\x00\x01\x01\x01\x01\x01\x01\x02\x00\x00\x01", 15);
101 cmd.wlen = 15;
102 }
Olli Salonenb1541212014-07-13 10:52:19 -0300103 cmd.rlen = 1;
104 ret = si2157_cmd_execute(s, &cmd);
105 if (ret)
106 goto err;
107
108 /* query chip revision */
109 memcpy(cmd.args, "\x02", 1);
110 cmd.wlen = 1;
111 cmd.rlen = 13;
112 ret = si2157_cmd_execute(s, &cmd);
113 if (ret)
114 goto err;
115
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300116 chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
117 cmd.args[4] << 0;
Olli Salonen1b923732014-07-13 10:52:20 -0300118
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300119 #define SI2158_A20 ('A' << 24 | 58 << 16 | '2' << 8 | '0' << 0)
120 #define SI2157_A30 ('A' << 24 | 57 << 16 | '3' << 8 | '0' << 0)
Olli Salonend87a5052014-09-11 17:01:38 -0300121 #define SI2147_A30 ('A' << 24 | 47 << 16 | '3' << 8 | '0' << 0)
Olli Salonen073f3842014-11-24 03:57:33 -0300122 #define SI2146_A10 ('A' << 24 | 46 << 16 | '1' << 8 | '0' << 0)
Olli Salonen1b923732014-07-13 10:52:20 -0300123
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300124 switch (chip_id) {
125 case SI2158_A20:
126 fw_file = SI2158_A20_FIRMWARE;
127 break;
128 case SI2157_A30:
Olli Salonend87a5052014-09-11 17:01:38 -0300129 case SI2147_A30:
Olli Salonen073f3842014-11-24 03:57:33 -0300130 case SI2146_A10:
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300131 goto skip_fw_download;
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300132 default:
133 dev_err(&s->client->dev,
Olli Salonen67d01132014-08-05 09:03:54 -0300134 "unknown chip version Si21%d-%c%c%c\n",
135 cmd.args[2], cmd.args[1],
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300136 cmd.args[3], cmd.args[4]);
137 ret = -EINVAL;
138 goto err;
Olli Salonen1b923732014-07-13 10:52:20 -0300139 }
140
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300141 /* cold state - try to download firmware */
Olli Salonen67d01132014-08-05 09:03:54 -0300142 dev_info(&s->client->dev, "found a '%s' in cold state\n",
143 si2157_ops.info.name);
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300144
145 /* request the firmware, this will block and timeout */
146 ret = request_firmware(&fw, fw_file, &s->client->dev);
147 if (ret) {
Olli Salonen67d01132014-08-05 09:03:54 -0300148 dev_err(&s->client->dev, "firmware file '%s' not found\n",
149 fw_file);
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300150 goto err;
151 }
152
153 /* firmware should be n chunks of 17 bytes */
154 if (fw->size % 17 != 0) {
Olli Salonen67d01132014-08-05 09:03:54 -0300155 dev_err(&s->client->dev, "firmware file '%s' is invalid\n",
156 fw_file);
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300157 ret = -EINVAL;
158 goto err;
159 }
160
Olli Salonen67d01132014-08-05 09:03:54 -0300161 dev_info(&s->client->dev, "downloading firmware from file '%s'\n",
162 fw_file);
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300163
164 for (remaining = fw->size; remaining > 0; remaining -= 17) {
165 len = fw->data[fw->size - remaining];
166 memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
167 cmd.wlen = len;
168 cmd.rlen = 1;
169 ret = si2157_cmd_execute(s, &cmd);
170 if (ret) {
171 dev_err(&s->client->dev,
Olli Salonen67d01132014-08-05 09:03:54 -0300172 "firmware download failed=%d\n",
173 ret);
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300174 goto err;
175 }
176 }
177
178 release_firmware(fw);
179 fw = NULL;
180
181skip_fw_download:
Olli Salonenb1541212014-07-13 10:52:19 -0300182 /* reboot the tuner with new firmware? */
183 memcpy(cmd.args, "\x01\x01", 2);
184 cmd.wlen = 2;
185 cmd.rlen = 1;
186 ret = si2157_cmd_execute(s, &cmd);
187 if (ret)
188 goto err;
189
Olli Salonen4cbf6ed2014-08-25 15:07:03 -0300190 s->fw_loaded = true;
Antti Palosaari930a8732014-04-10 21:58:10 -0300191
Olli Salonen4cbf6ed2014-08-25 15:07:03 -0300192warm:
193 s->active = true;
Antti Palosaari930a8732014-04-10 21:58:10 -0300194 return 0;
Olli Salonen4cbf6ed2014-08-25 15:07:03 -0300195
Olli Salonenb1541212014-07-13 10:52:19 -0300196err:
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300197 if (fw)
198 release_firmware(fw);
199
Olli Salonen67d01132014-08-05 09:03:54 -0300200 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Olli Salonenb1541212014-07-13 10:52:19 -0300201 return ret;
Antti Palosaari930a8732014-04-10 21:58:10 -0300202}
203
204static int si2157_sleep(struct dvb_frontend *fe)
205{
206 struct si2157 *s = fe->tuner_priv;
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300207 int ret;
208 struct si2157_cmd cmd;
Antti Palosaari930a8732014-04-10 21:58:10 -0300209
Olli Salonen67d01132014-08-05 09:03:54 -0300210 dev_dbg(&s->client->dev, "\n");
Antti Palosaari930a8732014-04-10 21:58:10 -0300211
212 s->active = false;
213
Olli Salonen0e382332014-08-25 15:07:02 -0300214 /* standby */
215 memcpy(cmd.args, "\x16\x00", 2);
216 cmd.wlen = 2;
217 cmd.rlen = 1;
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300218 ret = si2157_cmd_execute(s, &cmd);
219 if (ret)
220 goto err;
221
Antti Palosaari930a8732014-04-10 21:58:10 -0300222 return 0;
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300223err:
Olli Salonen67d01132014-08-05 09:03:54 -0300224 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300225 return ret;
Antti Palosaari930a8732014-04-10 21:58:10 -0300226}
227
228static int si2157_set_params(struct dvb_frontend *fe)
229{
230 struct si2157 *s = fe->tuner_priv;
231 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
232 int ret;
233 struct si2157_cmd cmd;
Olli Salonena1dad502014-07-13 10:52:21 -0300234 u8 bandwidth, delivery_system;
Antti Palosaari930a8732014-04-10 21:58:10 -0300235
236 dev_dbg(&s->client->dev,
Olli Salonen67d01132014-08-05 09:03:54 -0300237 "delivery_system=%d frequency=%u bandwidth_hz=%u\n",
238 c->delivery_system, c->frequency,
Antti Palosaari930a8732014-04-10 21:58:10 -0300239 c->bandwidth_hz);
240
241 if (!s->active) {
242 ret = -EAGAIN;
243 goto err;
244 }
245
Olli Salonena1dad502014-07-13 10:52:21 -0300246 if (c->bandwidth_hz <= 6000000)
247 bandwidth = 0x06;
248 else if (c->bandwidth_hz <= 7000000)
249 bandwidth = 0x07;
250 else if (c->bandwidth_hz <= 8000000)
251 bandwidth = 0x08;
252 else
253 bandwidth = 0x0f;
254
255 switch (c->delivery_system) {
Olli Salonen5cd62db2014-08-17 02:24:49 -0300256 case SYS_ATSC:
257 delivery_system = 0x00;
258 break;
Olli Salonen0d1165f2014-10-30 07:43:16 -0300259 case SYS_DVBC_ANNEX_B:
260 delivery_system = 0x10;
261 break;
Olli Salonena1dad502014-07-13 10:52:21 -0300262 case SYS_DVBT:
263 case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */
264 delivery_system = 0x20;
265 break;
266 case SYS_DVBC_ANNEX_A:
267 delivery_system = 0x30;
268 break;
269 default:
270 ret = -EINVAL;
271 goto err;
272 }
273
274 memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6);
275 cmd.args[4] = delivery_system | bandwidth;
Matthias Schwarzott05024ef2014-07-15 16:34:36 -0300276 if (s->inversion)
277 cmd.args[5] = 0x01;
Olli Salonena1dad502014-07-13 10:52:21 -0300278 cmd.wlen = 6;
Olli Salonend87a5052014-09-11 17:01:38 -0300279 cmd.rlen = 4;
280 ret = si2157_cmd_execute(s, &cmd);
281 if (ret)
282 goto err;
283
Olli Salonen073f3842014-11-24 03:57:33 -0300284 if (s->chiptype == SI2157_CHIPTYPE_SI2146)
285 memcpy(cmd.args, "\x14\x00\x02\x07\x00\x01", 6);
286 else
287 memcpy(cmd.args, "\x14\x00\x02\x07\x01\x00", 6);
Olli Salonend87a5052014-09-11 17:01:38 -0300288 cmd.wlen = 6;
289 cmd.rlen = 4;
Olli Salonena1dad502014-07-13 10:52:21 -0300290 ret = si2157_cmd_execute(s, &cmd);
291 if (ret)
292 goto err;
293
Antti Palosaari930a8732014-04-10 21:58:10 -0300294 /* set frequency */
Olli Salonenb1541212014-07-13 10:52:19 -0300295 memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8);
Antti Palosaari930a8732014-04-10 21:58:10 -0300296 cmd.args[4] = (c->frequency >> 0) & 0xff;
297 cmd.args[5] = (c->frequency >> 8) & 0xff;
298 cmd.args[6] = (c->frequency >> 16) & 0xff;
299 cmd.args[7] = (c->frequency >> 24) & 0xff;
Antti Palosaarie6b43802014-07-10 06:02:53 -0300300 cmd.wlen = 8;
301 cmd.rlen = 1;
Antti Palosaari930a8732014-04-10 21:58:10 -0300302 ret = si2157_cmd_execute(s, &cmd);
303 if (ret)
304 goto err;
305
306 return 0;
307err:
Olli Salonen67d01132014-08-05 09:03:54 -0300308 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaari930a8732014-04-10 21:58:10 -0300309 return ret;
310}
311
Matthias Schwarzott11405402014-07-15 04:58:40 -0300312static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
313{
314 *frequency = 5000000; /* default value of property 0x0706 */
315 return 0;
316}
317
Olli Salonen6cc8a352014-07-18 02:41:12 -0300318static const struct dvb_tuner_ops si2157_ops = {
Antti Palosaari930a8732014-04-10 21:58:10 -0300319 .info = {
Olli Salonen073f3842014-11-24 03:57:33 -0300320 .name = "Silicon Labs Si2146/2147/2157/2158",
Antti Palosaariae4c8912014-04-12 01:53:51 -0300321 .frequency_min = 110000000,
Antti Palosaari930a8732014-04-10 21:58:10 -0300322 .frequency_max = 862000000,
323 },
324
325 .init = si2157_init,
326 .sleep = si2157_sleep,
327 .set_params = si2157_set_params,
Matthias Schwarzott11405402014-07-15 04:58:40 -0300328 .get_if_frequency = si2157_get_if_frequency,
Antti Palosaari930a8732014-04-10 21:58:10 -0300329};
330
331static int si2157_probe(struct i2c_client *client,
332 const struct i2c_device_id *id)
333{
334 struct si2157_config *cfg = client->dev.platform_data;
335 struct dvb_frontend *fe = cfg->fe;
336 struct si2157 *s;
337 struct si2157_cmd cmd;
338 int ret;
339
340 s = kzalloc(sizeof(struct si2157), GFP_KERNEL);
341 if (!s) {
342 ret = -ENOMEM;
Olli Salonen67d01132014-08-05 09:03:54 -0300343 dev_err(&client->dev, "kzalloc() failed\n");
Antti Palosaari930a8732014-04-10 21:58:10 -0300344 goto err;
345 }
346
347 s->client = client;
348 s->fe = cfg->fe;
Matthias Schwarzott05024ef2014-07-15 16:34:36 -0300349 s->inversion = cfg->inversion;
Olli Salonen4cbf6ed2014-08-25 15:07:03 -0300350 s->fw_loaded = false;
Olli Salonen073f3842014-11-24 03:57:33 -0300351 s->chiptype = (u8)id->driver_data;
Antti Palosaari930a8732014-04-10 21:58:10 -0300352 mutex_init(&s->i2c_mutex);
353
354 /* check if the tuner is there */
Antti Palosaarie6b43802014-07-10 06:02:53 -0300355 cmd.wlen = 0;
356 cmd.rlen = 1;
Antti Palosaari930a8732014-04-10 21:58:10 -0300357 ret = si2157_cmd_execute(s, &cmd);
358 if (ret)
359 goto err;
360
361 fe->tuner_priv = s;
Olli Salonen6cc8a352014-07-18 02:41:12 -0300362 memcpy(&fe->ops.tuner_ops, &si2157_ops,
Antti Palosaari930a8732014-04-10 21:58:10 -0300363 sizeof(struct dvb_tuner_ops));
364
365 i2c_set_clientdata(client, s);
366
367 dev_info(&s->client->dev,
Olli Salonen073f3842014-11-24 03:57:33 -0300368 "Silicon Labs %s successfully attached\n",
369 s->chiptype == SI2157_CHIPTYPE_SI2146 ?
370 "Si2146" : "Si2147/2157/2158");
371
Antti Palosaari930a8732014-04-10 21:58:10 -0300372 return 0;
373err:
Olli Salonen67d01132014-08-05 09:03:54 -0300374 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari930a8732014-04-10 21:58:10 -0300375 kfree(s);
376
377 return ret;
378}
379
380static int si2157_remove(struct i2c_client *client)
381{
382 struct si2157 *s = i2c_get_clientdata(client);
383 struct dvb_frontend *fe = s->fe;
384
Olli Salonen67d01132014-08-05 09:03:54 -0300385 dev_dbg(&client->dev, "\n");
Antti Palosaari930a8732014-04-10 21:58:10 -0300386
387 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
388 fe->tuner_priv = NULL;
389 kfree(s);
390
391 return 0;
392}
393
394static const struct i2c_device_id si2157_id[] = {
395 {"si2157", 0},
Olli Salonen073f3842014-11-24 03:57:33 -0300396 {"si2146", 1},
Antti Palosaari930a8732014-04-10 21:58:10 -0300397 {}
398};
399MODULE_DEVICE_TABLE(i2c, si2157_id);
400
401static struct i2c_driver si2157_driver = {
402 .driver = {
403 .owner = THIS_MODULE,
404 .name = "si2157",
405 },
406 .probe = si2157_probe,
407 .remove = si2157_remove,
408 .id_table = si2157_id,
409};
410
411module_i2c_driver(si2157_driver);
412
Olli Salonen073f3842014-11-24 03:57:33 -0300413MODULE_DESCRIPTION("Silicon Labs Si2146/2147/2157/2158 silicon tuner driver");
Antti Palosaari930a8732014-04-10 21:58:10 -0300414MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
415MODULE_LICENSE("GPL");
Antti Palosaaribac53a22014-07-13 16:13:49 -0300416MODULE_FIRMWARE(SI2158_A20_FIRMWARE);