[media] si2157: trivial ID table changes
[deliverable/linux.git] / drivers / media / tuners / si2157.c
CommitLineData
ba92ae0f 1/*
17ad09f1 2 * Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner driver
ba92ae0f
AP
3 *
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
930a8730
AP
17#include "si2157_priv.h"
18
1b92373f
OS
19static const struct dvb_tuner_ops si2157_ops;
20
930a8730 21/* execute firmware command */
8fe51725 22static int si2157_cmd_execute(struct i2c_client *client, struct si2157_cmd *cmd)
930a8730 23{
8fe51725 24 struct si2157_dev *dev = i2c_get_clientdata(client);
930a8730 25 int ret;
930a8730
AP
26 unsigned long timeout;
27
70f13ff0 28 mutex_lock(&dev->i2c_mutex);
930a8730 29
e6b4380f 30 if (cmd->wlen) {
930a8730 31 /* write cmd and args for firmware */
8fe51725 32 ret = i2c_master_send(client, cmd->args, cmd->wlen);
930a8730
AP
33 if (ret < 0) {
34 goto err_mutex_unlock;
e6b4380f 35 } else if (ret != cmd->wlen) {
930a8730
AP
36 ret = -EREMOTEIO;
37 goto err_mutex_unlock;
38 }
39 }
40
e6b4380f
AP
41 if (cmd->rlen) {
42 /* wait cmd execution terminate */
43 #define TIMEOUT 80
44 timeout = jiffies + msecs_to_jiffies(TIMEOUT);
45 while (!time_after(jiffies, timeout)) {
8fe51725 46 ret = i2c_master_recv(client, cmd->args, cmd->rlen);
e6b4380f
AP
47 if (ret < 0) {
48 goto err_mutex_unlock;
49 } else if (ret != cmd->rlen) {
50 ret = -EREMOTEIO;
51 goto err_mutex_unlock;
52 }
53
54 /* firmware ready? */
55 if ((cmd->args[0] >> 7) & 0x01)
56 break;
930a8730
AP
57 }
58
8fe51725 59 dev_dbg(&client->dev, "cmd execution took %d ms\n",
e6b4380f
AP
60 jiffies_to_msecs(jiffies) -
61 (jiffies_to_msecs(timeout) - TIMEOUT));
930a8730 62
e6b4380f
AP
63 if (!((cmd->args[0] >> 7) & 0x01)) {
64 ret = -ETIMEDOUT;
65 goto err_mutex_unlock;
66 }
930a8730
AP
67 }
68
7cbf5d76
AP
69 mutex_unlock(&dev->i2c_mutex);
70 return 0;
e6b4380f 71
930a8730 72err_mutex_unlock:
70f13ff0 73 mutex_unlock(&dev->i2c_mutex);
8fe51725 74 dev_dbg(&client->dev, "failed=%d\n", ret);
930a8730
AP
75 return ret;
76}
77
78static int si2157_init(struct dvb_frontend *fe)
79{
8fe51725
AP
80 struct i2c_client *client = fe->tuner_priv;
81 struct si2157_dev *dev = i2c_get_clientdata(client);
7d6bc608 82 int ret, len, remaining;
b154121c 83 struct si2157_cmd cmd;
78dab40a 84 const struct firmware *fw;
1b92373f 85 u8 *fw_file;
7d6bc608 86 unsigned int chip_id;
930a8730 87
8fe51725 88 dev_dbg(&client->dev, "\n");
930a8730 89
70f13ff0 90 if (dev->fw_loaded)
4cbf6ed9
OS
91 goto warm;
92
93 /* power up */
70f13ff0 94 if (dev->chiptype == SI2157_CHIPTYPE_SI2146) {
073f3849
OS
95 memcpy(cmd.args, "\xc0\x05\x01\x00\x00\x0b\x00\x00\x01", 9);
96 cmd.wlen = 9;
97 } else {
98 memcpy(cmd.args, "\xc0\x00\x0c\x00\x00\x01\x01\x01\x01\x01\x01\x02\x00\x00\x01", 15);
99 cmd.wlen = 15;
100 }
b154121c 101 cmd.rlen = 1;
8fe51725 102 ret = si2157_cmd_execute(client, &cmd);
b154121c
OS
103 if (ret)
104 goto err;
105
106 /* query chip revision */
107 memcpy(cmd.args, "\x02", 1);
108 cmd.wlen = 1;
109 cmd.rlen = 13;
8fe51725 110 ret = si2157_cmd_execute(client, &cmd);
b154121c
OS
111 if (ret)
112 goto err;
113
7d6bc608
AP
114 chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
115 cmd.args[4] << 0;
116
117 #define SI2158_A20 ('A' << 24 | 58 << 16 | '2' << 8 | '0' << 0)
17ad09f1 118 #define SI2148_A20 ('A' << 24 | 48 << 16 | '2' << 8 | '0' << 0)
7d6bc608 119 #define SI2157_A30 ('A' << 24 | 57 << 16 | '3' << 8 | '0' << 0)
d87a5058 120 #define SI2147_A30 ('A' << 24 | 47 << 16 | '3' << 8 | '0' << 0)
073f3849 121 #define SI2146_A10 ('A' << 24 | 46 << 16 | '1' << 8 | '0' << 0)
7d6bc608
AP
122
123 switch (chip_id) {
124 case SI2158_A20:
17ad09f1 125 case SI2148_A20:
7d6bc608
AP
126 fw_file = SI2158_A20_FIRMWARE;
127 break;
128 case SI2157_A30:
d87a5058 129 case SI2147_A30:
073f3849 130 case SI2146_A10:
7d6bc608 131 goto skip_fw_download;
7d6bc608 132 default:
8fe51725 133 dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
67d0113a 134 cmd.args[2], cmd.args[1],
7d6bc608
AP
135 cmd.args[3], cmd.args[4]);
136 ret = -EINVAL;
137 goto err;
138 }
1b92373f 139
7d6bc608 140 /* cold state - try to download firmware */
8fe51725 141 dev_info(&client->dev, "found a '%s' in cold state\n",
67d0113a 142 si2157_ops.info.name);
1b92373f 143
7d6bc608 144 /* request the firmware, this will block and timeout */
8fe51725 145 ret = request_firmware(&fw, fw_file, &client->dev);
7d6bc608 146 if (ret) {
8fe51725 147 dev_err(&client->dev, "firmware file '%s' not found\n",
67d0113a 148 fw_file);
7d6bc608
AP
149 goto err;
150 }
1b92373f 151
7d6bc608
AP
152 /* firmware should be n chunks of 17 bytes */
153 if (fw->size % 17 != 0) {
8fe51725 154 dev_err(&client->dev, "firmware file '%s' is invalid\n",
67d0113a 155 fw_file);
7d6bc608 156 ret = -EINVAL;
78dab40a 157 goto err_release_firmware;
7d6bc608 158 }
1b92373f 159
8fe51725 160 dev_info(&client->dev, "downloading firmware from file '%s'\n",
67d0113a 161 fw_file);
1b92373f 162
7d6bc608
AP
163 for (remaining = fw->size; remaining > 0; remaining -= 17) {
164 len = fw->data[fw->size - remaining];
165 memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
166 cmd.wlen = len;
167 cmd.rlen = 1;
8fe51725 168 ret = si2157_cmd_execute(client, &cmd);
7d6bc608 169 if (ret) {
8fe51725 170 dev_err(&client->dev, "firmware download failed %d\n",
67d0113a 171 ret);
78dab40a 172 goto err_release_firmware;
1b92373f 173 }
1b92373f
OS
174 }
175
7d6bc608 176 release_firmware(fw);
7d6bc608
AP
177
178skip_fw_download:
b154121c
OS
179 /* reboot the tuner with new firmware? */
180 memcpy(cmd.args, "\x01\x01", 2);
181 cmd.wlen = 2;
182 cmd.rlen = 1;
8fe51725 183 ret = si2157_cmd_execute(client, &cmd);
b154121c
OS
184 if (ret)
185 goto err;
186
70f13ff0 187 dev->fw_loaded = true;
930a8730 188
4cbf6ed9 189warm:
70f13ff0 190 dev->active = true;
930a8730 191 return 0;
4cbf6ed9 192
78dab40a 193err_release_firmware:
dc89cfcf 194 release_firmware(fw);
e358c8a6 195err:
8fe51725 196 dev_dbg(&client->dev, "failed=%d\n", ret);
b154121c 197 return ret;
930a8730
AP
198}
199
200static int si2157_sleep(struct dvb_frontend *fe)
201{
8fe51725
AP
202 struct i2c_client *client = fe->tuner_priv;
203 struct si2157_dev *dev = i2c_get_clientdata(client);
a83d7d17
AP
204 int ret;
205 struct si2157_cmd cmd;
930a8730 206
8fe51725 207 dev_dbg(&client->dev, "\n");
930a8730 208
70f13ff0 209 dev->active = false;
930a8730 210
0e38233d
OS
211 /* standby */
212 memcpy(cmd.args, "\x16\x00", 2);
213 cmd.wlen = 2;
214 cmd.rlen = 1;
8fe51725 215 ret = si2157_cmd_execute(client, &cmd);
a83d7d17
AP
216 if (ret)
217 goto err;
218
930a8730 219 return 0;
a83d7d17 220err:
8fe51725 221 dev_dbg(&client->dev, "failed=%d\n", ret);
a83d7d17 222 return ret;
930a8730
AP
223}
224
225static int si2157_set_params(struct dvb_frontend *fe)
226{
8fe51725
AP
227 struct i2c_client *client = fe->tuner_priv;
228 struct si2157_dev *dev = i2c_get_clientdata(client);
930a8730
AP
229 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
230 int ret;
231 struct si2157_cmd cmd;
a1dad50d 232 u8 bandwidth, delivery_system;
930a8730 233
8fe51725 234 dev_dbg(&client->dev,
67d0113a 235 "delivery_system=%d frequency=%u bandwidth_hz=%u\n",
8fe51725 236 c->delivery_system, c->frequency, c->bandwidth_hz);
930a8730 237
70f13ff0 238 if (!dev->active) {
930a8730
AP
239 ret = -EAGAIN;
240 goto err;
241 }
242
a1dad50d
OS
243 if (c->bandwidth_hz <= 6000000)
244 bandwidth = 0x06;
245 else if (c->bandwidth_hz <= 7000000)
246 bandwidth = 0x07;
247 else if (c->bandwidth_hz <= 8000000)
248 bandwidth = 0x08;
249 else
250 bandwidth = 0x0f;
251
252 switch (c->delivery_system) {
5cd62db7
OS
253 case SYS_ATSC:
254 delivery_system = 0x00;
255 break;
0d1165fc
OS
256 case SYS_DVBC_ANNEX_B:
257 delivery_system = 0x10;
258 break;
a1dad50d
OS
259 case SYS_DVBT:
260 case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */
261 delivery_system = 0x20;
262 break;
263 case SYS_DVBC_ANNEX_A:
264 delivery_system = 0x30;
265 break;
266 default:
267 ret = -EINVAL;
268 goto err;
269 }
270
271 memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6);
272 cmd.args[4] = delivery_system | bandwidth;
70f13ff0 273 if (dev->inversion)
05024efe 274 cmd.args[5] = 0x01;
a1dad50d 275 cmd.wlen = 6;
d87a5058 276 cmd.rlen = 4;
8fe51725 277 ret = si2157_cmd_execute(client, &cmd);
d87a5058
OS
278 if (ret)
279 goto err;
280
70f13ff0 281 if (dev->chiptype == SI2157_CHIPTYPE_SI2146)
073f3849
OS
282 memcpy(cmd.args, "\x14\x00\x02\x07\x00\x01", 6);
283 else
284 memcpy(cmd.args, "\x14\x00\x02\x07\x01\x00", 6);
d87a5058
OS
285 cmd.wlen = 6;
286 cmd.rlen = 4;
8fe51725 287 ret = si2157_cmd_execute(client, &cmd);
a1dad50d
OS
288 if (ret)
289 goto err;
290
930a8730 291 /* set frequency */
b154121c 292 memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8);
930a8730
AP
293 cmd.args[4] = (c->frequency >> 0) & 0xff;
294 cmd.args[5] = (c->frequency >> 8) & 0xff;
295 cmd.args[6] = (c->frequency >> 16) & 0xff;
296 cmd.args[7] = (c->frequency >> 24) & 0xff;
e6b4380f
AP
297 cmd.wlen = 8;
298 cmd.rlen = 1;
8fe51725 299 ret = si2157_cmd_execute(client, &cmd);
930a8730
AP
300 if (ret)
301 goto err;
302
303 return 0;
304err:
8fe51725 305 dev_dbg(&client->dev, "failed=%d\n", ret);
930a8730
AP
306 return ret;
307}
308
1140540d
MS
309static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
310{
311 *frequency = 5000000; /* default value of property 0x0706 */
312 return 0;
313}
314
6cc8a35d 315static const struct dvb_tuner_ops si2157_ops = {
930a8730 316 .info = {
17ad09f1 317 .name = "Silicon Labs Si2146/2147/2148/2157/2158",
ae4c8919 318 .frequency_min = 110000000,
930a8730
AP
319 .frequency_max = 862000000,
320 },
321
322 .init = si2157_init,
323 .sleep = si2157_sleep,
324 .set_params = si2157_set_params,
1140540d 325 .get_if_frequency = si2157_get_if_frequency,
930a8730
AP
326};
327
328static int si2157_probe(struct i2c_client *client,
329 const struct i2c_device_id *id)
330{
331 struct si2157_config *cfg = client->dev.platform_data;
332 struct dvb_frontend *fe = cfg->fe;
70f13ff0 333 struct si2157_dev *dev;
930a8730
AP
334 struct si2157_cmd cmd;
335 int ret;
336
70f13ff0
AP
337 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
338 if (!dev) {
930a8730 339 ret = -ENOMEM;
67d0113a 340 dev_err(&client->dev, "kzalloc() failed\n");
930a8730
AP
341 goto err;
342 }
343
8fe51725 344 i2c_set_clientdata(client, dev);
70f13ff0
AP
345 dev->fe = cfg->fe;
346 dev->inversion = cfg->inversion;
347 dev->fw_loaded = false;
348 dev->chiptype = (u8)id->driver_data;
349 mutex_init(&dev->i2c_mutex);
930a8730
AP
350
351 /* check if the tuner is there */
e6b4380f
AP
352 cmd.wlen = 0;
353 cmd.rlen = 1;
8fe51725 354 ret = si2157_cmd_execute(client, &cmd);
930a8730
AP
355 if (ret)
356 goto err;
357
70f13ff0 358 memcpy(&fe->ops.tuner_ops, &si2157_ops, sizeof(struct dvb_tuner_ops));
8fe51725 359 fe->tuner_priv = client;
930a8730 360
8fe51725 361 dev_info(&client->dev, "Silicon Labs %s successfully attached\n",
70f13ff0 362 dev->chiptype == SI2157_CHIPTYPE_SI2146 ?
17ad09f1 363 "Si2146" : "Si2147/2148/2157/2158");
073f3849 364
930a8730
AP
365 return 0;
366err:
67d0113a 367 dev_dbg(&client->dev, "failed=%d\n", ret);
70f13ff0 368 kfree(dev);
930a8730
AP
369 return ret;
370}
371
372static int si2157_remove(struct i2c_client *client)
373{
70f13ff0
AP
374 struct si2157_dev *dev = i2c_get_clientdata(client);
375 struct dvb_frontend *fe = dev->fe;
930a8730 376
67d0113a 377 dev_dbg(&client->dev, "\n");
930a8730
AP
378
379 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
380 fe->tuner_priv = NULL;
70f13ff0 381 kfree(dev);
930a8730
AP
382
383 return 0;
384}
385
c1292233
AP
386static const struct i2c_device_id si2157_id_table[] = {
387 {"si2157", SI2157_CHIPTYPE_SI2157},
388 {"si2146", SI2157_CHIPTYPE_SI2146},
930a8730
AP
389 {}
390};
c1292233 391MODULE_DEVICE_TABLE(i2c, si2157_id_table);
930a8730
AP
392
393static struct i2c_driver si2157_driver = {
394 .driver = {
395 .owner = THIS_MODULE,
396 .name = "si2157",
397 },
398 .probe = si2157_probe,
399 .remove = si2157_remove,
c1292233 400 .id_table = si2157_id_table,
930a8730
AP
401};
402
403module_i2c_driver(si2157_driver);
404
17ad09f1 405MODULE_DESCRIPTION("Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner driver");
930a8730
AP
406MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
407MODULE_LICENSE("GPL");
bac53a2c 408MODULE_FIRMWARE(SI2158_A20_FIRMWARE);
This page took 0.084357 seconds and 5 git commands to generate.