blob: 58c5ef5863832b456e9724051a048a5fa3c2a755 [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;
Olli Salonen1b923732014-07-13 10:52:20 -030085 int ret, remaining;
Olli Salonenb1541212014-07-13 10:52:19 -030086 struct si2157_cmd cmd;
Olli Salonen1b923732014-07-13 10:52:20 -030087 u8 chip, len = 0;
88 const struct firmware *fw = NULL;
89 u8 *fw_file;
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
Olli Salonen1b923732014-07-13 10:52:20 -0300109 chip = cmd.args[2]; /* 57 for Si2157, 58 for Si2158 */
110
111 /* Si2158 requires firmware download */
112 if (chip == 58) {
113 if (((cmd.args[1] & 0x0f) == 1) && (cmd.args[3] == '2') &&
114 (cmd.args[4] == '0'))
115 fw_file = SI2158_A20_FIRMWARE;
116 else {
117 dev_err(&s->client->dev,
118 "%s: no firmware file for Si%d-%c%c defined\n",
119 KBUILD_MODNAME, chip, cmd.args[3], cmd.args[4]);
120 ret = -EINVAL;
121 goto err;
122 }
123
124 /* cold state - try to download firmware */
125 dev_info(&s->client->dev, "%s: found a '%s' in cold state\n",
126 KBUILD_MODNAME, si2157_ops.info.name);
127
128 /* request the firmware, this will block and timeout */
129 ret = request_firmware(&fw, fw_file, &s->client->dev);
130 if (ret) {
131 dev_err(&s->client->dev, "%s: firmware file '%s' not found\n",
132 KBUILD_MODNAME, fw_file);
133 goto err;
134 }
135
136 dev_info(&s->client->dev, "%s: downloading firmware from file '%s'\n",
137 KBUILD_MODNAME, fw_file);
138
139 /* firmware should be n chunks of 17 bytes */
140 if (fw->size % 17 != 0) {
141 dev_err(&s->client->dev, "%s: firmware file '%s' is invalid\n",
142 KBUILD_MODNAME, fw_file);
143 ret = -EINVAL;
144 goto err;
145 }
146
147 for (remaining = fw->size; remaining > 0; remaining -= 17) {
148 memcpy(&len, &fw->data[fw->size - remaining], 1);
149 memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1],
150 len);
151 cmd.wlen = len;
152 cmd.rlen = 1;
153 ret = si2157_cmd_execute(s, &cmd);
154 if (ret) {
155 dev_err(&s->client->dev,
156 "%s: firmware download failed=%d\n",
157 KBUILD_MODNAME, ret);
158 goto err;
159 }
160 }
161
162 release_firmware(fw);
163 fw = NULL;
164
165 }
166
Olli Salonenb1541212014-07-13 10:52:19 -0300167 /* reboot the tuner with new firmware? */
168 memcpy(cmd.args, "\x01\x01", 2);
169 cmd.wlen = 2;
170 cmd.rlen = 1;
171 ret = si2157_cmd_execute(s, &cmd);
172 if (ret)
173 goto err;
174
Antti Palosaari930a8732014-04-10 21:58:10 -0300175 s->active = true;
176
177 return 0;
Olli Salonenb1541212014-07-13 10:52:19 -0300178err:
179 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
180 return ret;
Antti Palosaari930a8732014-04-10 21:58:10 -0300181}
182
183static int si2157_sleep(struct dvb_frontend *fe)
184{
185 struct si2157 *s = fe->tuner_priv;
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300186 int ret;
187 struct si2157_cmd cmd;
Antti Palosaari930a8732014-04-10 21:58:10 -0300188
189 dev_dbg(&s->client->dev, "%s:\n", __func__);
190
191 s->active = false;
192
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300193 memcpy(cmd.args, "\x13", 1);
Antti Palosaarie6b43802014-07-10 06:02:53 -0300194 cmd.wlen = 1;
195 cmd.rlen = 0;
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300196 ret = si2157_cmd_execute(s, &cmd);
197 if (ret)
198 goto err;
199
Antti Palosaari930a8732014-04-10 21:58:10 -0300200 return 0;
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300201err:
202 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
203 return ret;
Antti Palosaari930a8732014-04-10 21:58:10 -0300204}
205
206static int si2157_set_params(struct dvb_frontend *fe)
207{
208 struct si2157 *s = fe->tuner_priv;
209 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
210 int ret;
211 struct si2157_cmd cmd;
212
213 dev_dbg(&s->client->dev,
214 "%s: delivery_system=%d frequency=%u bandwidth_hz=%u\n",
215 __func__, c->delivery_system, c->frequency,
216 c->bandwidth_hz);
217
218 if (!s->active) {
219 ret = -EAGAIN;
220 goto err;
221 }
222
Antti Palosaari930a8732014-04-10 21:58:10 -0300223 /* set frequency */
Olli Salonenb1541212014-07-13 10:52:19 -0300224 memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8);
Antti Palosaari930a8732014-04-10 21:58:10 -0300225 cmd.args[4] = (c->frequency >> 0) & 0xff;
226 cmd.args[5] = (c->frequency >> 8) & 0xff;
227 cmd.args[6] = (c->frequency >> 16) & 0xff;
228 cmd.args[7] = (c->frequency >> 24) & 0xff;
Antti Palosaarie6b43802014-07-10 06:02:53 -0300229 cmd.wlen = 8;
230 cmd.rlen = 1;
Antti Palosaari930a8732014-04-10 21:58:10 -0300231 ret = si2157_cmd_execute(s, &cmd);
232 if (ret)
233 goto err;
234
235 return 0;
236err:
237 dev_dbg(&s->client->dev, "%s: failed=%d\n", __func__, ret);
238 return ret;
239}
240
241static const struct dvb_tuner_ops si2157_tuner_ops = {
242 .info = {
Olli Salonen1b923732014-07-13 10:52:20 -0300243 .name = "Silicon Labs Si2157/Si2158",
Antti Palosaariae4c8912014-04-12 01:53:51 -0300244 .frequency_min = 110000000,
Antti Palosaari930a8732014-04-10 21:58:10 -0300245 .frequency_max = 862000000,
246 },
247
248 .init = si2157_init,
249 .sleep = si2157_sleep,
250 .set_params = si2157_set_params,
251};
252
253static int si2157_probe(struct i2c_client *client,
254 const struct i2c_device_id *id)
255{
256 struct si2157_config *cfg = client->dev.platform_data;
257 struct dvb_frontend *fe = cfg->fe;
258 struct si2157 *s;
259 struct si2157_cmd cmd;
260 int ret;
261
262 s = kzalloc(sizeof(struct si2157), GFP_KERNEL);
263 if (!s) {
264 ret = -ENOMEM;
265 dev_err(&client->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
266 goto err;
267 }
268
269 s->client = client;
270 s->fe = cfg->fe;
271 mutex_init(&s->i2c_mutex);
272
273 /* check if the tuner is there */
Antti Palosaarie6b43802014-07-10 06:02:53 -0300274 cmd.wlen = 0;
275 cmd.rlen = 1;
Antti Palosaari930a8732014-04-10 21:58:10 -0300276 ret = si2157_cmd_execute(s, &cmd);
277 if (ret)
278 goto err;
279
280 fe->tuner_priv = s;
281 memcpy(&fe->ops.tuner_ops, &si2157_tuner_ops,
282 sizeof(struct dvb_tuner_ops));
283
284 i2c_set_clientdata(client, s);
285
286 dev_info(&s->client->dev,
Olli Salonen1b923732014-07-13 10:52:20 -0300287 "%s: Silicon Labs Si2157/Si2158 successfully attached\n",
Antti Palosaari930a8732014-04-10 21:58:10 -0300288 KBUILD_MODNAME);
289 return 0;
290err:
291 dev_dbg(&client->dev, "%s: failed=%d\n", __func__, ret);
292 kfree(s);
293
294 return ret;
295}
296
297static int si2157_remove(struct i2c_client *client)
298{
299 struct si2157 *s = i2c_get_clientdata(client);
300 struct dvb_frontend *fe = s->fe;
301
302 dev_dbg(&client->dev, "%s:\n", __func__);
303
304 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
305 fe->tuner_priv = NULL;
306 kfree(s);
307
308 return 0;
309}
310
311static const struct i2c_device_id si2157_id[] = {
312 {"si2157", 0},
313 {}
314};
315MODULE_DEVICE_TABLE(i2c, si2157_id);
316
317static struct i2c_driver si2157_driver = {
318 .driver = {
319 .owner = THIS_MODULE,
320 .name = "si2157",
321 },
322 .probe = si2157_probe,
323 .remove = si2157_remove,
324 .id_table = si2157_id,
325};
326
327module_i2c_driver(si2157_driver);
328
Olli Salonen1b923732014-07-13 10:52:20 -0300329MODULE_DESCRIPTION("Silicon Labs Si2157/Si2158 silicon tuner driver");
Antti Palosaari930a8732014-04-10 21:58:10 -0300330MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
331MODULE_LICENSE("GPL");