Merge tag 'mac80211-next-for-davem-2015-05-19' of git://git.kernel.org/pub/scm/linux...
[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;
08272db6 85 const char *fw_name;
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:
08272db6 126 fw_name = SI2158_A20_FIRMWARE;
7d6bc608
AP
127 break;
128 case SI2157_A30:
d87a5058 129 case SI2147_A30:
073f3849 130 case SI2146_A10:
08272db6 131 fw_name = NULL;
6c372533 132 break;
7d6bc608 133 default:
8fe51725 134 dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
67d0113a 135 cmd.args[2], cmd.args[1],
7d6bc608
AP
136 cmd.args[3], cmd.args[4]);
137 ret = -EINVAL;
138 goto err;
139 }
1b92373f 140
6c372533
AP
141 dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n",
142 cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
143
08272db6 144 if (fw_name == NULL)
6c372533 145 goto skip_fw_download;
1b92373f 146
7d6bc608 147 /* request the firmware, this will block and timeout */
08272db6 148 ret = request_firmware(&fw, fw_name, &client->dev);
7d6bc608 149 if (ret) {
8fe51725 150 dev_err(&client->dev, "firmware file '%s' not found\n",
08272db6 151 fw_name);
7d6bc608
AP
152 goto err;
153 }
1b92373f 154
7d6bc608
AP
155 /* firmware should be n chunks of 17 bytes */
156 if (fw->size % 17 != 0) {
8fe51725 157 dev_err(&client->dev, "firmware file '%s' is invalid\n",
08272db6 158 fw_name);
7d6bc608 159 ret = -EINVAL;
78dab40a 160 goto err_release_firmware;
7d6bc608 161 }
1b92373f 162
8fe51725 163 dev_info(&client->dev, "downloading firmware from file '%s'\n",
08272db6 164 fw_name);
1b92373f 165
7d6bc608
AP
166 for (remaining = fw->size; remaining > 0; remaining -= 17) {
167 len = fw->data[fw->size - remaining];
168 memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
169 cmd.wlen = len;
170 cmd.rlen = 1;
8fe51725 171 ret = si2157_cmd_execute(client, &cmd);
7d6bc608 172 if (ret) {
8fe51725 173 dev_err(&client->dev, "firmware download failed %d\n",
67d0113a 174 ret);
78dab40a 175 goto err_release_firmware;
1b92373f 176 }
1b92373f
OS
177 }
178
7d6bc608 179 release_firmware(fw);
7d6bc608
AP
180
181skip_fw_download:
b154121c
OS
182 /* reboot the tuner with new firmware? */
183 memcpy(cmd.args, "\x01\x01", 2);
184 cmd.wlen = 2;
185 cmd.rlen = 1;
8fe51725 186 ret = si2157_cmd_execute(client, &cmd);
b154121c
OS
187 if (ret)
188 goto err;
189
c7011845
AP
190 /* query firmware version */
191 memcpy(cmd.args, "\x11", 1);
192 cmd.wlen = 1;
193 cmd.rlen = 10;
194 ret = si2157_cmd_execute(client, &cmd);
195 if (ret)
196 goto err;
197
198 dev_info(&client->dev, "firmware version: %c.%c.%d\n",
199 cmd.args[6], cmd.args[7], cmd.args[8]);
200
70f13ff0 201 dev->fw_loaded = true;
930a8730 202
4cbf6ed9 203warm:
70f13ff0 204 dev->active = true;
930a8730 205 return 0;
4cbf6ed9 206
78dab40a 207err_release_firmware:
dc89cfcf 208 release_firmware(fw);
e358c8a6 209err:
8fe51725 210 dev_dbg(&client->dev, "failed=%d\n", ret);
b154121c 211 return ret;
930a8730
AP
212}
213
214static int si2157_sleep(struct dvb_frontend *fe)
215{
8fe51725
AP
216 struct i2c_client *client = fe->tuner_priv;
217 struct si2157_dev *dev = i2c_get_clientdata(client);
a83d7d17
AP
218 int ret;
219 struct si2157_cmd cmd;
930a8730 220
8fe51725 221 dev_dbg(&client->dev, "\n");
930a8730 222
70f13ff0 223 dev->active = false;
930a8730 224
0e38233d
OS
225 /* standby */
226 memcpy(cmd.args, "\x16\x00", 2);
227 cmd.wlen = 2;
228 cmd.rlen = 1;
8fe51725 229 ret = si2157_cmd_execute(client, &cmd);
a83d7d17
AP
230 if (ret)
231 goto err;
232
930a8730 233 return 0;
a83d7d17 234err:
8fe51725 235 dev_dbg(&client->dev, "failed=%d\n", ret);
a83d7d17 236 return ret;
930a8730
AP
237}
238
239static int si2157_set_params(struct dvb_frontend *fe)
240{
8fe51725
AP
241 struct i2c_client *client = fe->tuner_priv;
242 struct si2157_dev *dev = i2c_get_clientdata(client);
930a8730
AP
243 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
244 int ret;
245 struct si2157_cmd cmd;
a1dad50d 246 u8 bandwidth, delivery_system;
c28aebe1 247 u32 if_frequency = 5000000;
930a8730 248
8fe51725 249 dev_dbg(&client->dev,
67d0113a 250 "delivery_system=%d frequency=%u bandwidth_hz=%u\n",
8fe51725 251 c->delivery_system, c->frequency, c->bandwidth_hz);
930a8730 252
70f13ff0 253 if (!dev->active) {
930a8730
AP
254 ret = -EAGAIN;
255 goto err;
256 }
257
a1dad50d
OS
258 if (c->bandwidth_hz <= 6000000)
259 bandwidth = 0x06;
260 else if (c->bandwidth_hz <= 7000000)
261 bandwidth = 0x07;
262 else if (c->bandwidth_hz <= 8000000)
263 bandwidth = 0x08;
264 else
265 bandwidth = 0x0f;
266
267 switch (c->delivery_system) {
5cd62db7
OS
268 case SYS_ATSC:
269 delivery_system = 0x00;
c28aebe1 270 if_frequency = 3250000;
5cd62db7 271 break;
0d1165fc
OS
272 case SYS_DVBC_ANNEX_B:
273 delivery_system = 0x10;
c28aebe1 274 if_frequency = 4000000;
0d1165fc 275 break;
a1dad50d
OS
276 case SYS_DVBT:
277 case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */
278 delivery_system = 0x20;
279 break;
280 case SYS_DVBC_ANNEX_A:
281 delivery_system = 0x30;
282 break;
283 default:
284 ret = -EINVAL;
285 goto err;
286 }
287
288 memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6);
289 cmd.args[4] = delivery_system | bandwidth;
70f13ff0 290 if (dev->inversion)
05024efe 291 cmd.args[5] = 0x01;
a1dad50d 292 cmd.wlen = 6;
d87a5058 293 cmd.rlen = 4;
8fe51725 294 ret = si2157_cmd_execute(client, &cmd);
d87a5058
OS
295 if (ret)
296 goto err;
297
70f13ff0 298 if (dev->chiptype == SI2157_CHIPTYPE_SI2146)
073f3849
OS
299 memcpy(cmd.args, "\x14\x00\x02\x07\x00\x01", 6);
300 else
301 memcpy(cmd.args, "\x14\x00\x02\x07\x01\x00", 6);
d87a5058
OS
302 cmd.wlen = 6;
303 cmd.rlen = 4;
8fe51725 304 ret = si2157_cmd_execute(client, &cmd);
a1dad50d
OS
305 if (ret)
306 goto err;
307
c28aebe1
OS
308 /* set if frequency if needed */
309 if (if_frequency != dev->if_frequency) {
310 memcpy(cmd.args, "\x14\x00\x06\x07", 4);
311 cmd.args[4] = (if_frequency / 1000) & 0xff;
312 cmd.args[5] = ((if_frequency / 1000) >> 8) & 0xff;
313 cmd.wlen = 6;
314 cmd.rlen = 4;
315 ret = si2157_cmd_execute(client, &cmd);
316 if (ret)
317 goto err;
318
319 dev->if_frequency = if_frequency;
320 }
321
930a8730 322 /* set frequency */
b154121c 323 memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8);
930a8730
AP
324 cmd.args[4] = (c->frequency >> 0) & 0xff;
325 cmd.args[5] = (c->frequency >> 8) & 0xff;
326 cmd.args[6] = (c->frequency >> 16) & 0xff;
327 cmd.args[7] = (c->frequency >> 24) & 0xff;
e6b4380f
AP
328 cmd.wlen = 8;
329 cmd.rlen = 1;
8fe51725 330 ret = si2157_cmd_execute(client, &cmd);
930a8730
AP
331 if (ret)
332 goto err;
333
334 return 0;
335err:
8fe51725 336 dev_dbg(&client->dev, "failed=%d\n", ret);
930a8730
AP
337 return ret;
338}
339
1140540d
MS
340static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
341{
c28aebe1
OS
342 struct i2c_client *client = fe->tuner_priv;
343 struct si2157_dev *dev = i2c_get_clientdata(client);
344
345 *frequency = dev->if_frequency;
1140540d
MS
346 return 0;
347}
348
6cc8a35d 349static const struct dvb_tuner_ops si2157_ops = {
930a8730 350 .info = {
17ad09f1 351 .name = "Silicon Labs Si2146/2147/2148/2157/2158",
18acc6ac 352 .frequency_min = 55000000,
930a8730
AP
353 .frequency_max = 862000000,
354 },
355
356 .init = si2157_init,
357 .sleep = si2157_sleep,
358 .set_params = si2157_set_params,
1140540d 359 .get_if_frequency = si2157_get_if_frequency,
930a8730
AP
360};
361
362static int si2157_probe(struct i2c_client *client,
363 const struct i2c_device_id *id)
364{
365 struct si2157_config *cfg = client->dev.platform_data;
366 struct dvb_frontend *fe = cfg->fe;
70f13ff0 367 struct si2157_dev *dev;
930a8730
AP
368 struct si2157_cmd cmd;
369 int ret;
370
70f13ff0
AP
371 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
372 if (!dev) {
930a8730 373 ret = -ENOMEM;
67d0113a 374 dev_err(&client->dev, "kzalloc() failed\n");
930a8730
AP
375 goto err;
376 }
377
8fe51725 378 i2c_set_clientdata(client, dev);
70f13ff0
AP
379 dev->fe = cfg->fe;
380 dev->inversion = cfg->inversion;
381 dev->fw_loaded = false;
382 dev->chiptype = (u8)id->driver_data;
c28aebe1 383 dev->if_frequency = 5000000; /* default value of property 0x0706 */
70f13ff0 384 mutex_init(&dev->i2c_mutex);
930a8730
AP
385
386 /* check if the tuner is there */
e6b4380f
AP
387 cmd.wlen = 0;
388 cmd.rlen = 1;
8fe51725 389 ret = si2157_cmd_execute(client, &cmd);
930a8730 390 if (ret)
e7f3f9f3 391 goto err_kfree;
930a8730 392
70f13ff0 393 memcpy(&fe->ops.tuner_ops, &si2157_ops, sizeof(struct dvb_tuner_ops));
8fe51725 394 fe->tuner_priv = client;
930a8730 395
8fe51725 396 dev_info(&client->dev, "Silicon Labs %s successfully attached\n",
70f13ff0 397 dev->chiptype == SI2157_CHIPTYPE_SI2146 ?
17ad09f1 398 "Si2146" : "Si2147/2148/2157/2158");
073f3849 399
930a8730 400 return 0;
e7f3f9f3
AP
401
402err_kfree:
403 kfree(dev);
930a8730 404err:
67d0113a 405 dev_dbg(&client->dev, "failed=%d\n", ret);
930a8730
AP
406 return ret;
407}
408
409static int si2157_remove(struct i2c_client *client)
410{
70f13ff0
AP
411 struct si2157_dev *dev = i2c_get_clientdata(client);
412 struct dvb_frontend *fe = dev->fe;
930a8730 413
67d0113a 414 dev_dbg(&client->dev, "\n");
930a8730
AP
415
416 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
417 fe->tuner_priv = NULL;
70f13ff0 418 kfree(dev);
930a8730
AP
419
420 return 0;
421}
422
c1292233
AP
423static const struct i2c_device_id si2157_id_table[] = {
424 {"si2157", SI2157_CHIPTYPE_SI2157},
425 {"si2146", SI2157_CHIPTYPE_SI2146},
930a8730
AP
426 {}
427};
c1292233 428MODULE_DEVICE_TABLE(i2c, si2157_id_table);
930a8730
AP
429
430static struct i2c_driver si2157_driver = {
431 .driver = {
432 .owner = THIS_MODULE,
433 .name = "si2157",
434 },
435 .probe = si2157_probe,
436 .remove = si2157_remove,
c1292233 437 .id_table = si2157_id_table,
930a8730
AP
438};
439
440module_i2c_driver(si2157_driver);
441
17ad09f1 442MODULE_DESCRIPTION("Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner driver");
930a8730
AP
443MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
444MODULE_LICENSE("GPL");
bac53a2c 445MODULE_FIRMWARE(SI2158_A20_FIRMWARE);
This page took 0.204059 seconds and 5 git commands to generate.