]>
Commit | Line | Data |
---|---|---|
711615df AP |
1 | /* |
2 | * Silicon Labs Si2168 DVB-T/T2/C demodulator driver | |
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 | ||
845f3505 AP |
17 | #include "si2168_priv.h" |
18 | ||
19 | static const struct dvb_frontend_ops si2168_ops; | |
20 | ||
d2b72f64 AP |
21 | /* Own I2C adapter locking is needed because of I2C gate logic. */ |
22 | static int si2168_i2c_master_send_unlocked(const struct i2c_client *client, | |
23 | const char *buf, int count) | |
24 | { | |
25 | int ret; | |
26 | struct i2c_msg msg = { | |
27 | .addr = client->addr, | |
28 | .flags = 0, | |
29 | .len = count, | |
30 | .buf = (char *)buf, | |
31 | }; | |
32 | ||
33 | ret = __i2c_transfer(client->adapter, &msg, 1); | |
34 | return (ret == 1) ? count : ret; | |
35 | } | |
36 | ||
37 | static int si2168_i2c_master_recv_unlocked(const struct i2c_client *client, | |
38 | char *buf, int count) | |
39 | { | |
40 | int ret; | |
41 | struct i2c_msg msg = { | |
42 | .addr = client->addr, | |
43 | .flags = I2C_M_RD, | |
44 | .len = count, | |
45 | .buf = buf, | |
46 | }; | |
47 | ||
48 | ret = __i2c_transfer(client->adapter, &msg, 1); | |
49 | return (ret == 1) ? count : ret; | |
50 | } | |
51 | ||
845f3505 | 52 | /* execute firmware command */ |
d2b72f64 AP |
53 | static int si2168_cmd_execute_unlocked(struct i2c_client *client, |
54 | struct si2168_cmd *cmd) | |
845f3505 AP |
55 | { |
56 | int ret; | |
57 | unsigned long timeout; | |
58 | ||
845f3505 AP |
59 | if (cmd->wlen) { |
60 | /* write cmd and args for firmware */ | |
d2b72f64 AP |
61 | ret = si2168_i2c_master_send_unlocked(client, cmd->args, |
62 | cmd->wlen); | |
845f3505 | 63 | if (ret < 0) { |
d2b72f64 | 64 | goto err; |
845f3505 AP |
65 | } else if (ret != cmd->wlen) { |
66 | ret = -EREMOTEIO; | |
d2b72f64 | 67 | goto err; |
845f3505 AP |
68 | } |
69 | } | |
70 | ||
71 | if (cmd->rlen) { | |
72 | /* wait cmd execution terminate */ | |
551c33e7 | 73 | #define TIMEOUT 70 |
845f3505 AP |
74 | timeout = jiffies + msecs_to_jiffies(TIMEOUT); |
75 | while (!time_after(jiffies, timeout)) { | |
d2b72f64 AP |
76 | ret = si2168_i2c_master_recv_unlocked(client, cmd->args, |
77 | cmd->rlen); | |
845f3505 | 78 | if (ret < 0) { |
d2b72f64 | 79 | goto err; |
845f3505 AP |
80 | } else if (ret != cmd->rlen) { |
81 | ret = -EREMOTEIO; | |
d2b72f64 | 82 | goto err; |
845f3505 AP |
83 | } |
84 | ||
85 | /* firmware ready? */ | |
86 | if ((cmd->args[0] >> 7) & 0x01) | |
87 | break; | |
88 | } | |
89 | ||
3de35835 | 90 | dev_dbg(&client->dev, "cmd execution took %d ms\n", |
845f3505 AP |
91 | jiffies_to_msecs(jiffies) - |
92 | (jiffies_to_msecs(timeout) - TIMEOUT)); | |
93 | ||
7adf99d2 OS |
94 | /* error bit set? */ |
95 | if ((cmd->args[0] >> 6) & 0x01) { | |
96 | ret = -EREMOTEIO; | |
d2b72f64 | 97 | goto err; |
7adf99d2 OS |
98 | } |
99 | ||
eefae30a | 100 | if (!((cmd->args[0] >> 7) & 0x01)) { |
845f3505 | 101 | ret = -ETIMEDOUT; |
d2b72f64 | 102 | goto err; |
845f3505 AP |
103 | } |
104 | } | |
105 | ||
4affbe1d | 106 | return 0; |
d2b72f64 | 107 | err: |
3de35835 | 108 | dev_dbg(&client->dev, "failed=%d\n", ret); |
845f3505 AP |
109 | return ret; |
110 | } | |
111 | ||
d2b72f64 AP |
112 | static int si2168_cmd_execute(struct i2c_client *client, struct si2168_cmd *cmd) |
113 | { | |
114 | int ret; | |
115 | ||
116 | i2c_lock_adapter(client->adapter); | |
117 | ret = si2168_cmd_execute_unlocked(client, cmd); | |
118 | i2c_unlock_adapter(client->adapter); | |
119 | ||
120 | return ret; | |
121 | } | |
122 | ||
0df289a2 | 123 | static int si2168_read_status(struct dvb_frontend *fe, enum fe_status *status) |
845f3505 | 124 | { |
6307b560 AP |
125 | struct i2c_client *client = fe->demodulator_priv; |
126 | struct si2168_dev *dev = i2c_get_clientdata(client); | |
bffab93c | 127 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
845f3505 AP |
128 | int ret; |
129 | struct si2168_cmd cmd; | |
130 | ||
131 | *status = 0; | |
132 | ||
bd01c766 | 133 | if (!dev->active) { |
845f3505 AP |
134 | ret = -EAGAIN; |
135 | goto err; | |
136 | } | |
137 | ||
bffab93c AP |
138 | switch (c->delivery_system) { |
139 | case SYS_DVBT: | |
888680ff | 140 | memcpy(cmd.args, "\xa0\x01", 2); |
bffab93c AP |
141 | cmd.wlen = 2; |
142 | cmd.rlen = 13; | |
143 | break; | |
c790885b | 144 | case SYS_DVBC_ANNEX_A: |
888680ff | 145 | memcpy(cmd.args, "\x90\x01", 2); |
c790885b AP |
146 | cmd.wlen = 2; |
147 | cmd.rlen = 9; | |
148 | break; | |
bffab93c | 149 | case SYS_DVBT2: |
888680ff | 150 | memcpy(cmd.args, "\x50\x01", 2); |
bffab93c AP |
151 | cmd.wlen = 2; |
152 | cmd.rlen = 14; | |
153 | break; | |
154 | default: | |
155 | ret = -EINVAL; | |
156 | goto err; | |
157 | } | |
158 | ||
6307b560 | 159 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
160 | if (ret) |
161 | goto err; | |
162 | ||
722a042d AP |
163 | switch ((cmd.args[2] >> 1) & 0x03) { |
164 | case 0x01: | |
845f3505 AP |
165 | *status = FE_HAS_SIGNAL | FE_HAS_CARRIER; |
166 | break; | |
722a042d | 167 | case 0x03: |
845f3505 AP |
168 | *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | |
169 | FE_HAS_SYNC | FE_HAS_LOCK; | |
170 | break; | |
171 | } | |
172 | ||
bd01c766 | 173 | dev->fe_status = *status; |
845f3505 | 174 | |
88ac8f86 AP |
175 | if (*status & FE_HAS_LOCK) { |
176 | c->cnr.len = 1; | |
177 | c->cnr.stat[0].scale = FE_SCALE_DECIBEL; | |
178 | c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4; | |
179 | } else { | |
180 | c->cnr.len = 1; | |
181 | c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; | |
182 | } | |
183 | ||
3de35835 | 184 | dev_dbg(&client->dev, "status=%02x args=%*ph\n", |
37b4e43d | 185 | *status, cmd.rlen, cmd.args); |
845f3505 AP |
186 | |
187 | return 0; | |
188 | err: | |
3de35835 | 189 | dev_dbg(&client->dev, "failed=%d\n", ret); |
845f3505 AP |
190 | return ret; |
191 | } | |
192 | ||
193 | static int si2168_set_frontend(struct dvb_frontend *fe) | |
194 | { | |
6307b560 AP |
195 | struct i2c_client *client = fe->demodulator_priv; |
196 | struct si2168_dev *dev = i2c_get_clientdata(client); | |
845f3505 AP |
197 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
198 | int ret; | |
199 | struct si2168_cmd cmd; | |
bffab93c | 200 | u8 bandwidth, delivery_system; |
845f3505 | 201 | |
3de35835 | 202 | dev_dbg(&client->dev, |
e5dd1100 AP |
203 | "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n", |
204 | c->delivery_system, c->modulation, c->frequency, | |
205 | c->bandwidth_hz, c->symbol_rate, c->inversion, | |
206 | c->stream_id); | |
845f3505 | 207 | |
bd01c766 | 208 | if (!dev->active) { |
845f3505 AP |
209 | ret = -EAGAIN; |
210 | goto err; | |
211 | } | |
212 | ||
bffab93c AP |
213 | switch (c->delivery_system) { |
214 | case SYS_DVBT: | |
215 | delivery_system = 0x20; | |
216 | break; | |
c790885b AP |
217 | case SYS_DVBC_ANNEX_A: |
218 | delivery_system = 0x30; | |
219 | break; | |
bffab93c AP |
220 | case SYS_DVBT2: |
221 | delivery_system = 0x70; | |
222 | break; | |
223 | default: | |
224 | ret = -EINVAL; | |
225 | goto err; | |
226 | } | |
227 | ||
683e98b6 OS |
228 | if (c->bandwidth_hz == 0) { |
229 | ret = -EINVAL; | |
230 | goto err; | |
17d4d6ae OS |
231 | } else if (c->bandwidth_hz <= 2000000) |
232 | bandwidth = 0x02; | |
233 | else if (c->bandwidth_hz <= 5000000) | |
bffab93c | 234 | bandwidth = 0x05; |
c790885b | 235 | else if (c->bandwidth_hz <= 6000000) |
bffab93c | 236 | bandwidth = 0x06; |
c790885b | 237 | else if (c->bandwidth_hz <= 7000000) |
bffab93c | 238 | bandwidth = 0x07; |
c790885b | 239 | else if (c->bandwidth_hz <= 8000000) |
bffab93c | 240 | bandwidth = 0x08; |
c790885b AP |
241 | else if (c->bandwidth_hz <= 9000000) |
242 | bandwidth = 0x09; | |
243 | else if (c->bandwidth_hz <= 10000000) | |
244 | bandwidth = 0x0a; | |
245 | else | |
246 | bandwidth = 0x0f; | |
845f3505 AP |
247 | |
248 | /* program tuner */ | |
249 | if (fe->ops.tuner_ops.set_params) { | |
250 | ret = fe->ops.tuner_ops.set_params(fe); | |
251 | if (ret) | |
252 | goto err; | |
253 | } | |
254 | ||
255 | memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5); | |
256 | cmd.wlen = 5; | |
257 | cmd.rlen = 5; | |
6307b560 | 258 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
259 | if (ret) |
260 | goto err; | |
261 | ||
bffab93c AP |
262 | /* that has no big effect */ |
263 | if (c->delivery_system == SYS_DVBT) | |
264 | memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6); | |
c790885b AP |
265 | else if (c->delivery_system == SYS_DVBC_ANNEX_A) |
266 | memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6); | |
bffab93c AP |
267 | else if (c->delivery_system == SYS_DVBT2) |
268 | memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6); | |
845f3505 AP |
269 | cmd.wlen = 6; |
270 | cmd.rlen = 3; | |
6307b560 | 271 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
272 | if (ret) |
273 | goto err; | |
274 | ||
e395e573 C |
275 | if (c->delivery_system == SYS_DVBT2) { |
276 | /* select PLP */ | |
277 | cmd.args[0] = 0x52; | |
278 | cmd.args[1] = c->stream_id & 0xff; | |
279 | cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1; | |
280 | cmd.wlen = 3; | |
281 | cmd.rlen = 1; | |
6307b560 | 282 | ret = si2168_cmd_execute(client, &cmd); |
e395e573 C |
283 | if (ret) |
284 | goto err; | |
285 | } | |
286 | ||
845f3505 AP |
287 | memcpy(cmd.args, "\x51\x03", 2); |
288 | cmd.wlen = 2; | |
289 | cmd.rlen = 12; | |
6307b560 | 290 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
291 | if (ret) |
292 | goto err; | |
293 | ||
294 | memcpy(cmd.args, "\x12\x08\x04", 3); | |
295 | cmd.wlen = 3; | |
296 | cmd.rlen = 3; | |
6307b560 | 297 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
298 | if (ret) |
299 | goto err; | |
300 | ||
845f3505 AP |
301 | memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6); |
302 | cmd.wlen = 6; | |
1d518c27 | 303 | cmd.rlen = 4; |
6307b560 | 304 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
305 | if (ret) |
306 | goto err; | |
307 | ||
308 | memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6); | |
309 | cmd.wlen = 6; | |
1d518c27 | 310 | cmd.rlen = 4; |
6307b560 | 311 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
312 | if (ret) |
313 | goto err; | |
314 | ||
845f3505 AP |
315 | memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6); |
316 | cmd.wlen = 6; | |
1d518c27 | 317 | cmd.rlen = 4; |
6307b560 | 318 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
319 | if (ret) |
320 | goto err; | |
321 | ||
322 | memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6); | |
bffab93c | 323 | cmd.args[4] = delivery_system | bandwidth; |
845f3505 | 324 | cmd.wlen = 6; |
1d518c27 | 325 | cmd.rlen = 4; |
6307b560 | 326 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
327 | if (ret) |
328 | goto err; | |
329 | ||
32bf8818 LA |
330 | /* set DVB-C symbol rate */ |
331 | if (c->delivery_system == SYS_DVBC_ANNEX_A) { | |
332 | memcpy(cmd.args, "\x14\x00\x02\x11", 4); | |
346d4900 | 333 | cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff; |
32bf8818 LA |
334 | cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff; |
335 | cmd.wlen = 6; | |
336 | cmd.rlen = 4; | |
6307b560 | 337 | ret = si2168_cmd_execute(client, &cmd); |
32bf8818 LA |
338 | if (ret) |
339 | goto err; | |
340 | } | |
341 | ||
845f3505 AP |
342 | memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6); |
343 | cmd.wlen = 6; | |
1d518c27 | 344 | cmd.rlen = 4; |
6307b560 | 345 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
346 | if (ret) |
347 | goto err; | |
348 | ||
52791979 | 349 | memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6); |
bd01c766 | 350 | cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10; |
845f3505 | 351 | cmd.wlen = 6; |
1d518c27 | 352 | cmd.rlen = 4; |
6307b560 | 353 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
354 | if (ret) |
355 | goto err; | |
356 | ||
52791979 | 357 | memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6); |
bd01c766 | 358 | cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10; |
845f3505 | 359 | cmd.wlen = 6; |
1d518c27 | 360 | cmd.rlen = 4; |
6307b560 | 361 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
362 | if (ret) |
363 | goto err; | |
364 | ||
365 | memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6); | |
366 | cmd.wlen = 6; | |
1d518c27 | 367 | cmd.rlen = 4; |
6307b560 | 368 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
369 | if (ret) |
370 | goto err; | |
371 | ||
43911776 OS |
372 | memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6); |
373 | cmd.wlen = 6; | |
374 | cmd.rlen = 4; | |
6307b560 | 375 | ret = si2168_cmd_execute(client, &cmd); |
43911776 OS |
376 | if (ret) |
377 | goto err; | |
378 | ||
888680ff | 379 | memcpy(cmd.args, "\x85", 1); |
845f3505 AP |
380 | cmd.wlen = 1; |
381 | cmd.rlen = 1; | |
6307b560 | 382 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
383 | if (ret) |
384 | goto err; | |
385 | ||
bd01c766 | 386 | dev->delivery_system = c->delivery_system; |
845f3505 AP |
387 | |
388 | return 0; | |
389 | err: | |
3de35835 | 390 | dev_dbg(&client->dev, "failed=%d\n", ret); |
845f3505 AP |
391 | return ret; |
392 | } | |
393 | ||
394 | static int si2168_init(struct dvb_frontend *fe) | |
395 | { | |
6307b560 AP |
396 | struct i2c_client *client = fe->demodulator_priv; |
397 | struct si2168_dev *dev = i2c_get_clientdata(client); | |
845f3505 | 398 | int ret, len, remaining; |
58f6693f | 399 | const struct firmware *fw; |
c32b2816 | 400 | const char *fw_name; |
845f3505 | 401 | struct si2168_cmd cmd; |
b6b6fd6f | 402 | unsigned int chip_id; |
845f3505 | 403 | |
3de35835 | 404 | dev_dbg(&client->dev, "\n"); |
845f3505 | 405 | |
8e417224 | 406 | /* initialize */ |
888680ff | 407 | memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13); |
845f3505 AP |
408 | cmd.wlen = 13; |
409 | cmd.rlen = 0; | |
6307b560 | 410 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
411 | if (ret) |
412 | goto err; | |
413 | ||
bd01c766 | 414 | if (dev->fw_loaded) { |
8e417224 OS |
415 | /* resume */ |
416 | memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8); | |
417 | cmd.wlen = 8; | |
418 | cmd.rlen = 1; | |
6307b560 | 419 | ret = si2168_cmd_execute(client, &cmd); |
8e417224 OS |
420 | if (ret) |
421 | goto err; | |
422 | ||
423 | memcpy(cmd.args, "\x85", 1); | |
424 | cmd.wlen = 1; | |
425 | cmd.rlen = 1; | |
6307b560 | 426 | ret = si2168_cmd_execute(client, &cmd); |
8e417224 OS |
427 | if (ret) |
428 | goto err; | |
429 | ||
430 | goto warm; | |
431 | } | |
432 | ||
433 | /* power up */ | |
888680ff | 434 | memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8); |
845f3505 AP |
435 | cmd.wlen = 8; |
436 | cmd.rlen = 1; | |
6307b560 | 437 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
438 | if (ret) |
439 | goto err; | |
440 | ||
c9cb0820 | 441 | /* query chip revision */ |
888680ff | 442 | memcpy(cmd.args, "\x02", 1); |
845f3505 AP |
443 | cmd.wlen = 1; |
444 | cmd.rlen = 13; | |
6307b560 | 445 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
446 | if (ret) |
447 | goto err; | |
448 | ||
b6b6fd6f AP |
449 | chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 | |
450 | cmd.args[4] << 0; | |
451 | ||
635a90cf | 452 | #define SI2168_A20 ('A' << 24 | 68 << 16 | '2' << 8 | '0' << 0) |
b6b6fd6f AP |
453 | #define SI2168_A30 ('A' << 24 | 68 << 16 | '3' << 8 | '0' << 0) |
454 | #define SI2168_B40 ('B' << 24 | 68 << 16 | '4' << 8 | '0' << 0) | |
455 | ||
456 | switch (chip_id) { | |
635a90cf | 457 | case SI2168_A20: |
c32b2816 | 458 | fw_name = SI2168_A20_FIRMWARE; |
635a90cf | 459 | break; |
b6b6fd6f | 460 | case SI2168_A30: |
c32b2816 | 461 | fw_name = SI2168_A30_FIRMWARE; |
b6b6fd6f AP |
462 | break; |
463 | case SI2168_B40: | |
c32b2816 | 464 | fw_name = SI2168_B40_FIRMWARE; |
b6b6fd6f AP |
465 | break; |
466 | default: | |
9b7839c3 | 467 | dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n", |
37b4e43d | 468 | cmd.args[2], cmd.args[1], |
b6b6fd6f | 469 | cmd.args[3], cmd.args[4]); |
c9cb0820 OS |
470 | ret = -EINVAL; |
471 | goto err; | |
472 | } | |
473 | ||
9b7839c3 AP |
474 | dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n", |
475 | cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]); | |
845f3505 AP |
476 | |
477 | /* request the firmware, this will block and timeout */ | |
c32b2816 | 478 | ret = request_firmware(&fw, fw_name, &client->dev); |
845f3505 | 479 | if (ret) { |
b6b6fd6f AP |
480 | /* fallback mechanism to handle old name for Si2168 B40 fw */ |
481 | if (chip_id == SI2168_B40) { | |
c32b2816 AP |
482 | fw_name = SI2168_B40_FIRMWARE_FALLBACK; |
483 | ret = request_firmware(&fw, fw_name, &client->dev); | |
c9cb0820 | 484 | } |
b6b6fd6f AP |
485 | |
486 | if (ret == 0) { | |
3de35835 | 487 | dev_notice(&client->dev, |
37b4e43d OS |
488 | "please install firmware file '%s'\n", |
489 | SI2168_B40_FIRMWARE); | |
b6b6fd6f | 490 | } else { |
3de35835 | 491 | dev_err(&client->dev, |
37b4e43d | 492 | "firmware file '%s' not found\n", |
c32b2816 | 493 | fw_name); |
346d4900 | 494 | goto err_release_firmware; |
c9cb0820 | 495 | } |
845f3505 AP |
496 | } |
497 | ||
3de35835 | 498 | dev_info(&client->dev, "downloading firmware from file '%s'\n", |
c32b2816 | 499 | fw_name); |
845f3505 | 500 | |
1b97dc98 OS |
501 | if ((fw->size % 17 == 0) && (fw->data[0] > 5)) { |
502 | /* firmware is in the new format */ | |
503 | for (remaining = fw->size; remaining > 0; remaining -= 17) { | |
504 | len = fw->data[fw->size - remaining]; | |
47810b43 LA |
505 | if (len > SI2168_ARGLEN) { |
506 | ret = -EINVAL; | |
507 | break; | |
508 | } | |
1b97dc98 OS |
509 | memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len); |
510 | cmd.wlen = len; | |
511 | cmd.rlen = 1; | |
6307b560 | 512 | ret = si2168_cmd_execute(client, &cmd); |
68c16a76 AP |
513 | if (ret) |
514 | break; | |
1b97dc98 | 515 | } |
68c16a76 | 516 | } else if (fw->size % 8 == 0) { |
1b97dc98 | 517 | /* firmware is in the old format */ |
68c16a76 AP |
518 | for (remaining = fw->size; remaining > 0; remaining -= 8) { |
519 | len = 8; | |
1b97dc98 OS |
520 | memcpy(cmd.args, &fw->data[fw->size - remaining], len); |
521 | cmd.wlen = len; | |
522 | cmd.rlen = 1; | |
6307b560 | 523 | ret = si2168_cmd_execute(client, &cmd); |
68c16a76 AP |
524 | if (ret) |
525 | break; | |
845f3505 | 526 | } |
68c16a76 AP |
527 | } else { |
528 | /* bad or unknown firmware format */ | |
529 | ret = -EINVAL; | |
530 | } | |
531 | ||
532 | if (ret) { | |
533 | dev_err(&client->dev, "firmware download failed %d\n", ret); | |
534 | goto err_release_firmware; | |
845f3505 AP |
535 | } |
536 | ||
537 | release_firmware(fw); | |
845f3505 | 538 | |
888680ff | 539 | memcpy(cmd.args, "\x01\x01", 2); |
845f3505 AP |
540 | cmd.wlen = 2; |
541 | cmd.rlen = 1; | |
6307b560 | 542 | ret = si2168_cmd_execute(client, &cmd); |
845f3505 AP |
543 | if (ret) |
544 | goto err; | |
545 | ||
a594cf21 OS |
546 | /* query firmware version */ |
547 | memcpy(cmd.args, "\x11", 1); | |
548 | cmd.wlen = 1; | |
549 | cmd.rlen = 10; | |
6307b560 | 550 | ret = si2168_cmd_execute(client, &cmd); |
a594cf21 OS |
551 | if (ret) |
552 | goto err; | |
553 | ||
955e0ab8 | 554 | dev_info(&client->dev, "firmware version: %c.%c.%d\n", |
a594cf21 OS |
555 | cmd.args[6], cmd.args[7], cmd.args[8]); |
556 | ||
389ce398 OS |
557 | /* set ts mode */ |
558 | memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6); | |
bd01c766 | 559 | cmd.args[4] |= dev->ts_mode; |
8117a312 OS |
560 | if (dev->ts_clock_gapped) |
561 | cmd.args[4] |= 0x40; | |
389ce398 OS |
562 | cmd.wlen = 6; |
563 | cmd.rlen = 4; | |
6307b560 | 564 | ret = si2168_cmd_execute(client, &cmd); |
389ce398 OS |
565 | if (ret) |
566 | goto err; | |
567 | ||
bd01c766 | 568 | dev->fw_loaded = true; |
45c3cbb1 | 569 | warm: |
bd01c766 | 570 | dev->active = true; |
845f3505 AP |
571 | |
572 | return 0; | |
9b7839c3 | 573 | |
346d4900 | 574 | err_release_firmware: |
034e1ec0 ME |
575 | release_firmware(fw); |
576 | err: | |
3de35835 | 577 | dev_dbg(&client->dev, "failed=%d\n", ret); |
845f3505 AP |
578 | return ret; |
579 | } | |
580 | ||
581 | static int si2168_sleep(struct dvb_frontend *fe) | |
582 | { | |
6307b560 AP |
583 | struct i2c_client *client = fe->demodulator_priv; |
584 | struct si2168_dev *dev = i2c_get_clientdata(client); | |
4de0ed7c AP |
585 | int ret; |
586 | struct si2168_cmd cmd; | |
845f3505 | 587 | |
3de35835 | 588 | dev_dbg(&client->dev, "\n"); |
845f3505 | 589 | |
bd01c766 | 590 | dev->active = false; |
845f3505 | 591 | |
4de0ed7c AP |
592 | memcpy(cmd.args, "\x13", 1); |
593 | cmd.wlen = 1; | |
594 | cmd.rlen = 0; | |
6307b560 | 595 | ret = si2168_cmd_execute(client, &cmd); |
4de0ed7c AP |
596 | if (ret) |
597 | goto err; | |
598 | ||
845f3505 | 599 | return 0; |
4de0ed7c | 600 | err: |
3de35835 | 601 | dev_dbg(&client->dev, "failed=%d\n", ret); |
4de0ed7c | 602 | return ret; |
845f3505 AP |
603 | } |
604 | ||
605 | static int si2168_get_tune_settings(struct dvb_frontend *fe, | |
606 | struct dvb_frontend_tune_settings *s) | |
607 | { | |
608 | s->min_delay_ms = 900; | |
609 | ||
610 | return 0; | |
611 | } | |
612 | ||
613 | /* | |
614 | * I2C gate logic | |
d2b72f64 AP |
615 | * We must use unlocked I2C I/O because I2C adapter lock is already taken |
616 | * by the caller (usually tuner driver). | |
845f3505 AP |
617 | */ |
618 | static int si2168_select(struct i2c_adapter *adap, void *mux_priv, u32 chan) | |
619 | { | |
6307b560 | 620 | struct i2c_client *client = mux_priv; |
845f3505 | 621 | int ret; |
d2b72f64 | 622 | struct si2168_cmd cmd; |
845f3505 | 623 | |
d2b72f64 AP |
624 | /* open I2C gate */ |
625 | memcpy(cmd.args, "\xc0\x0d\x01", 3); | |
626 | cmd.wlen = 3; | |
627 | cmd.rlen = 0; | |
628 | ret = si2168_cmd_execute_unlocked(client, &cmd); | |
629 | if (ret) | |
630 | goto err; | |
845f3505 | 631 | |
d2b72f64 AP |
632 | return 0; |
633 | err: | |
634 | dev_dbg(&client->dev, "failed=%d\n", ret); | |
845f3505 AP |
635 | return ret; |
636 | } | |
637 | ||
638 | static int si2168_deselect(struct i2c_adapter *adap, void *mux_priv, u32 chan) | |
639 | { | |
6307b560 | 640 | struct i2c_client *client = mux_priv; |
845f3505 | 641 | int ret; |
d2b72f64 | 642 | struct si2168_cmd cmd; |
845f3505 | 643 | |
d2b72f64 AP |
644 | /* close I2C gate */ |
645 | memcpy(cmd.args, "\xc0\x0d\x00", 3); | |
646 | cmd.wlen = 3; | |
647 | cmd.rlen = 0; | |
648 | ret = si2168_cmd_execute_unlocked(client, &cmd); | |
649 | if (ret) | |
650 | goto err; | |
845f3505 | 651 | |
d2b72f64 AP |
652 | return 0; |
653 | err: | |
654 | dev_dbg(&client->dev, "failed=%d\n", ret); | |
845f3505 AP |
655 | return ret; |
656 | } | |
657 | ||
658 | static const struct dvb_frontend_ops si2168_ops = { | |
c790885b | 659 | .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A}, |
845f3505 AP |
660 | .info = { |
661 | .name = "Silicon Labs Si2168", | |
f1ecc5d1 AP |
662 | .symbol_rate_min = 1000000, |
663 | .symbol_rate_max = 7200000, | |
845f3505 AP |
664 | .caps = FE_CAN_FEC_1_2 | |
665 | FE_CAN_FEC_2_3 | | |
666 | FE_CAN_FEC_3_4 | | |
667 | FE_CAN_FEC_5_6 | | |
668 | FE_CAN_FEC_7_8 | | |
669 | FE_CAN_FEC_AUTO | | |
670 | FE_CAN_QPSK | | |
671 | FE_CAN_QAM_16 | | |
672 | FE_CAN_QAM_32 | | |
673 | FE_CAN_QAM_64 | | |
674 | FE_CAN_QAM_128 | | |
675 | FE_CAN_QAM_256 | | |
676 | FE_CAN_QAM_AUTO | | |
677 | FE_CAN_TRANSMISSION_MODE_AUTO | | |
678 | FE_CAN_GUARD_INTERVAL_AUTO | | |
679 | FE_CAN_HIERARCHY_AUTO | | |
680 | FE_CAN_MUTE_TS | | |
327eeb3a OS |
681 | FE_CAN_2G_MODULATION | |
682 | FE_CAN_MULTISTREAM | |
845f3505 AP |
683 | }, |
684 | ||
685 | .get_tune_settings = si2168_get_tune_settings, | |
686 | ||
687 | .init = si2168_init, | |
688 | .sleep = si2168_sleep, | |
689 | ||
690 | .set_frontend = si2168_set_frontend, | |
691 | ||
692 | .read_status = si2168_read_status, | |
693 | }; | |
694 | ||
695 | static int si2168_probe(struct i2c_client *client, | |
696 | const struct i2c_device_id *id) | |
697 | { | |
698 | struct si2168_config *config = client->dev.platform_data; | |
bd01c766 | 699 | struct si2168_dev *dev; |
845f3505 | 700 | int ret; |
845f3505 | 701 | |
37b4e43d | 702 | dev_dbg(&client->dev, "\n"); |
845f3505 | 703 | |
bd01c766 AP |
704 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
705 | if (!dev) { | |
845f3505 | 706 | ret = -ENOMEM; |
37b4e43d | 707 | dev_err(&client->dev, "kzalloc() failed\n"); |
1ee5e7dd | 708 | goto err; |
845f3505 AP |
709 | } |
710 | ||
845f3505 | 711 | /* create mux i2c adapter for tuner */ |
6307b560 AP |
712 | dev->adapter = i2c_add_mux_adapter(client->adapter, &client->dev, |
713 | client, 0, 0, 0, si2168_select, si2168_deselect); | |
bd01c766 | 714 | if (dev->adapter == NULL) { |
4d6efc7a | 715 | ret = -ENODEV; |
346d4900 | 716 | goto err_kfree; |
4d6efc7a | 717 | } |
845f3505 AP |
718 | |
719 | /* create dvb_frontend */ | |
bd01c766 | 720 | memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops)); |
6307b560 | 721 | dev->fe.demodulator_priv = client; |
bd01c766 AP |
722 | *config->i2c_adapter = dev->adapter; |
723 | *config->fe = &dev->fe; | |
724 | dev->ts_mode = config->ts_mode; | |
725 | dev->ts_clock_inv = config->ts_clock_inv; | |
8117a312 | 726 | dev->ts_clock_gapped = config->ts_clock_gapped; |
bd01c766 | 727 | dev->fw_loaded = false; |
845f3505 | 728 | |
bd01c766 | 729 | i2c_set_clientdata(client, dev); |
845f3505 | 730 | |
346d4900 | 731 | dev_info(&client->dev, "Silicon Labs Si2168 successfully attached\n"); |
845f3505 | 732 | return 0; |
346d4900 | 733 | err_kfree: |
bd01c766 | 734 | kfree(dev); |
1ee5e7dd | 735 | err: |
37b4e43d | 736 | dev_dbg(&client->dev, "failed=%d\n", ret); |
845f3505 AP |
737 | return ret; |
738 | } | |
739 | ||
740 | static int si2168_remove(struct i2c_client *client) | |
741 | { | |
bd01c766 | 742 | struct si2168_dev *dev = i2c_get_clientdata(client); |
845f3505 | 743 | |
37b4e43d | 744 | dev_dbg(&client->dev, "\n"); |
845f3505 | 745 | |
bd01c766 | 746 | i2c_del_mux_adapter(dev->adapter); |
845f3505 | 747 | |
bd01c766 AP |
748 | dev->fe.ops.release = NULL; |
749 | dev->fe.demodulator_priv = NULL; | |
845f3505 | 750 | |
bd01c766 | 751 | kfree(dev); |
845f3505 AP |
752 | |
753 | return 0; | |
754 | } | |
755 | ||
346d4900 | 756 | static const struct i2c_device_id si2168_id_table[] = { |
845f3505 AP |
757 | {"si2168", 0}, |
758 | {} | |
759 | }; | |
346d4900 | 760 | MODULE_DEVICE_TABLE(i2c, si2168_id_table); |
845f3505 AP |
761 | |
762 | static struct i2c_driver si2168_driver = { | |
763 | .driver = { | |
845f3505 AP |
764 | .name = "si2168", |
765 | }, | |
766 | .probe = si2168_probe, | |
767 | .remove = si2168_remove, | |
346d4900 | 768 | .id_table = si2168_id_table, |
845f3505 AP |
769 | }; |
770 | ||
771 | module_i2c_driver(si2168_driver); | |
772 | ||
773 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); | |
774 | MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver"); | |
775 | MODULE_LICENSE("GPL"); | |
635a90cf | 776 | MODULE_FIRMWARE(SI2168_A20_FIRMWARE); |
668aa63c | 777 | MODULE_FIRMWARE(SI2168_A30_FIRMWARE); |
c9cb0820 | 778 | MODULE_FIRMWARE(SI2168_B40_FIRMWARE); |