blob: 59a4218459da0066e7e0242f5dac15fabdb602a8 [file] [log] [blame]
Antti Palosaari711615d2014-04-14 21:55:12 -03001/*
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
Antti Palosaari845f3502014-04-10 22:00:50 -030017#include "si2168_priv.h"
18
19static const struct dvb_frontend_ops si2168_ops;
20
21/* execute firmware command */
22static int si2168_cmd_execute(struct si2168 *s, struct si2168_cmd *cmd)
23{
24 int ret;
25 unsigned long timeout;
26
27 mutex_lock(&s->i2c_mutex);
28
29 if (cmd->wlen) {
30 /* write cmd and args for firmware */
31 ret = i2c_master_send(s->client, cmd->args, cmd->wlen);
32 if (ret < 0) {
33 goto err_mutex_unlock;
34 } else if (ret != cmd->wlen) {
35 ret = -EREMOTEIO;
36 goto err_mutex_unlock;
37 }
38 }
39
40 if (cmd->rlen) {
41 /* wait cmd execution terminate */
42 #define TIMEOUT 50
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;
56 }
57
Olli Salonen37b4e432014-08-05 08:54:08 -030058 dev_dbg(&s->client->dev, "cmd execution took %d ms\n",
Antti Palosaari845f3502014-04-10 22:00:50 -030059 jiffies_to_msecs(jiffies) -
60 (jiffies_to_msecs(timeout) - TIMEOUT));
61
Antti Palosaarieefae302014-06-13 11:08:25 -030062 if (!((cmd->args[0] >> 7) & 0x01)) {
Antti Palosaari845f3502014-04-10 22:00:50 -030063 ret = -ETIMEDOUT;
64 goto err_mutex_unlock;
65 }
66 }
67
68 ret = 0;
69
70err_mutex_unlock:
71 mutex_unlock(&s->i2c_mutex);
72 if (ret)
73 goto err;
74
75 return 0;
76err:
Olli Salonen37b4e432014-08-05 08:54:08 -030077 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -030078 return ret;
79}
80
81static int si2168_read_status(struct dvb_frontend *fe, fe_status_t *status)
82{
83 struct si2168 *s = fe->demodulator_priv;
Antti Palosaaribffab932014-04-11 20:35:27 -030084 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari845f3502014-04-10 22:00:50 -030085 int ret;
86 struct si2168_cmd cmd;
87
88 *status = 0;
89
90 if (!s->active) {
91 ret = -EAGAIN;
92 goto err;
93 }
94
Antti Palosaaribffab932014-04-11 20:35:27 -030095 switch (c->delivery_system) {
96 case SYS_DVBT:
Antti Palosaari888680f2014-07-09 19:36:58 -030097 memcpy(cmd.args, "\xa0\x01", 2);
Antti Palosaaribffab932014-04-11 20:35:27 -030098 cmd.wlen = 2;
99 cmd.rlen = 13;
100 break;
Antti Palosaaric7908852014-04-12 01:58:32 -0300101 case SYS_DVBC_ANNEX_A:
Antti Palosaari888680f2014-07-09 19:36:58 -0300102 memcpy(cmd.args, "\x90\x01", 2);
Antti Palosaaric7908852014-04-12 01:58:32 -0300103 cmd.wlen = 2;
104 cmd.rlen = 9;
105 break;
Antti Palosaaribffab932014-04-11 20:35:27 -0300106 case SYS_DVBT2:
Antti Palosaari888680f2014-07-09 19:36:58 -0300107 memcpy(cmd.args, "\x50\x01", 2);
Antti Palosaaribffab932014-04-11 20:35:27 -0300108 cmd.wlen = 2;
109 cmd.rlen = 14;
110 break;
111 default:
112 ret = -EINVAL;
113 goto err;
114 }
115
Antti Palosaari845f3502014-04-10 22:00:50 -0300116 ret = si2168_cmd_execute(s, &cmd);
117 if (ret)
118 goto err;
119
120 /*
121 * Possible values seen, in order from strong signal to weak:
122 * 16 0001 0110 full lock
123 * 1e 0001 1110 partial lock
124 * 1a 0001 1010 partial lock
125 * 18 0001 1000 no lock
126 *
127 * [b3:b1] lock bits
128 * [b4] statistics ready? Set in a few secs after lock is gained.
129 */
130
Antti Palosaari722a0422014-04-22 21:36:32 -0300131 switch ((cmd.args[2] >> 1) & 0x03) {
132 case 0x01:
Antti Palosaari845f3502014-04-10 22:00:50 -0300133 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
134 break;
Antti Palosaari722a0422014-04-22 21:36:32 -0300135 case 0x03:
Antti Palosaari845f3502014-04-10 22:00:50 -0300136 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
137 FE_HAS_SYNC | FE_HAS_LOCK;
138 break;
139 }
140
141 s->fe_status = *status;
142
Antti Palosaari88ac8f82014-07-09 20:22:27 -0300143 if (*status & FE_HAS_LOCK) {
144 c->cnr.len = 1;
145 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
146 c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4;
147 } else {
148 c->cnr.len = 1;
149 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
150 }
151
Olli Salonen37b4e432014-08-05 08:54:08 -0300152 dev_dbg(&s->client->dev, "status=%02x args=%*ph\n",
153 *status, cmd.rlen, cmd.args);
Antti Palosaari845f3502014-04-10 22:00:50 -0300154
155 return 0;
156err:
Olli Salonen37b4e432014-08-05 08:54:08 -0300157 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300158 return ret;
159}
160
161static int si2168_set_frontend(struct dvb_frontend *fe)
162{
163 struct si2168 *s = fe->demodulator_priv;
164 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
165 int ret;
166 struct si2168_cmd cmd;
Antti Palosaaribffab932014-04-11 20:35:27 -0300167 u8 bandwidth, delivery_system;
Antti Palosaari845f3502014-04-10 22:00:50 -0300168
169 dev_dbg(&s->client->dev,
Olli Salonen37b4e432014-08-05 08:54:08 -0300170 "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u\n",
171 c->delivery_system, c->modulation,
Antti Palosaari845f3502014-04-10 22:00:50 -0300172 c->frequency, c->bandwidth_hz, c->symbol_rate,
173 c->inversion);
174
175 if (!s->active) {
176 ret = -EAGAIN;
177 goto err;
178 }
179
Antti Palosaaribffab932014-04-11 20:35:27 -0300180 switch (c->delivery_system) {
181 case SYS_DVBT:
182 delivery_system = 0x20;
183 break;
Antti Palosaaric7908852014-04-12 01:58:32 -0300184 case SYS_DVBC_ANNEX_A:
185 delivery_system = 0x30;
186 break;
Antti Palosaaribffab932014-04-11 20:35:27 -0300187 case SYS_DVBT2:
188 delivery_system = 0x70;
189 break;
190 default:
191 ret = -EINVAL;
192 goto err;
193 }
194
Antti Palosaaric7908852014-04-12 01:58:32 -0300195 if (c->bandwidth_hz <= 5000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300196 bandwidth = 0x05;
Antti Palosaaric7908852014-04-12 01:58:32 -0300197 else if (c->bandwidth_hz <= 6000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300198 bandwidth = 0x06;
Antti Palosaaric7908852014-04-12 01:58:32 -0300199 else if (c->bandwidth_hz <= 7000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300200 bandwidth = 0x07;
Antti Palosaaric7908852014-04-12 01:58:32 -0300201 else if (c->bandwidth_hz <= 8000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300202 bandwidth = 0x08;
Antti Palosaaric7908852014-04-12 01:58:32 -0300203 else if (c->bandwidth_hz <= 9000000)
204 bandwidth = 0x09;
205 else if (c->bandwidth_hz <= 10000000)
206 bandwidth = 0x0a;
207 else
208 bandwidth = 0x0f;
Antti Palosaari845f3502014-04-10 22:00:50 -0300209
210 /* program tuner */
211 if (fe->ops.tuner_ops.set_params) {
212 ret = fe->ops.tuner_ops.set_params(fe);
213 if (ret)
214 goto err;
215 }
216
217 memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5);
218 cmd.wlen = 5;
219 cmd.rlen = 5;
220 ret = si2168_cmd_execute(s, &cmd);
221 if (ret)
222 goto err;
223
Antti Palosaaribffab932014-04-11 20:35:27 -0300224 /* that has no big effect */
225 if (c->delivery_system == SYS_DVBT)
226 memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6);
Antti Palosaaric7908852014-04-12 01:58:32 -0300227 else if (c->delivery_system == SYS_DVBC_ANNEX_A)
228 memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6);
Antti Palosaaribffab932014-04-11 20:35:27 -0300229 else if (c->delivery_system == SYS_DVBT2)
230 memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6);
Antti Palosaari845f3502014-04-10 22:00:50 -0300231 cmd.wlen = 6;
232 cmd.rlen = 3;
233 ret = si2168_cmd_execute(s, &cmd);
234 if (ret)
235 goto err;
236
237 memcpy(cmd.args, "\x51\x03", 2);
238 cmd.wlen = 2;
239 cmd.rlen = 12;
240 ret = si2168_cmd_execute(s, &cmd);
241 if (ret)
242 goto err;
243
244 memcpy(cmd.args, "\x12\x08\x04", 3);
245 cmd.wlen = 3;
246 cmd.rlen = 3;
247 ret = si2168_cmd_execute(s, &cmd);
248 if (ret)
249 goto err;
250
Antti Palosaari845f3502014-04-10 22:00:50 -0300251 memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6);
252 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300253 cmd.rlen = 4;
Antti Palosaari845f3502014-04-10 22:00:50 -0300254 ret = si2168_cmd_execute(s, &cmd);
255 if (ret)
256 goto err;
257
258 memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6);
259 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300260 cmd.rlen = 4;
Antti Palosaari845f3502014-04-10 22:00:50 -0300261 ret = si2168_cmd_execute(s, &cmd);
262 if (ret)
263 goto err;
264
Antti Palosaari845f3502014-04-10 22:00:50 -0300265 memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6);
266 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300267 cmd.rlen = 4;
Antti Palosaari845f3502014-04-10 22:00:50 -0300268 ret = si2168_cmd_execute(s, &cmd);
269 if (ret)
270 goto err;
271
272 memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6);
Antti Palosaaribffab932014-04-11 20:35:27 -0300273 cmd.args[4] = delivery_system | bandwidth;
Antti Palosaari845f3502014-04-10 22:00:50 -0300274 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300275 cmd.rlen = 4;
Antti Palosaari845f3502014-04-10 22:00:50 -0300276 ret = si2168_cmd_execute(s, &cmd);
277 if (ret)
278 goto err;
279
Luis Alves32bf8812014-07-17 14:31:28 -0300280 /* set DVB-C symbol rate */
281 if (c->delivery_system == SYS_DVBC_ANNEX_A) {
282 memcpy(cmd.args, "\x14\x00\x02\x11", 4);
283 cmd.args[4] = (c->symbol_rate / 1000) & 0xff;
284 cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
285 cmd.wlen = 6;
286 cmd.rlen = 4;
287 ret = si2168_cmd_execute(s, &cmd);
288 if (ret)
289 goto err;
290 }
291
Antti Palosaari845f3502014-04-10 22:00:50 -0300292 memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6);
293 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300294 cmd.rlen = 4;
Antti Palosaari845f3502014-04-10 22:00:50 -0300295 ret = si2168_cmd_execute(s, &cmd);
296 if (ret)
297 goto err;
298
Antti Palosaari93f72632014-07-10 20:21:59 -0300299 memcpy(cmd.args, "\x14\x00\x01\x10\x16\x00", 6);
Antti Palosaari845f3502014-04-10 22:00:50 -0300300 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300301 cmd.rlen = 4;
Antti Palosaari845f3502014-04-10 22:00:50 -0300302 ret = si2168_cmd_execute(s, &cmd);
303 if (ret)
304 goto err;
305
306 memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x18", 6);
307 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300308 cmd.rlen = 4;
Antti Palosaari845f3502014-04-10 22:00:50 -0300309 ret = si2168_cmd_execute(s, &cmd);
310 if (ret)
311 goto err;
312
313 memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x15", 6);
314 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300315 cmd.rlen = 4;
Antti Palosaari845f3502014-04-10 22:00:50 -0300316 ret = si2168_cmd_execute(s, &cmd);
317 if (ret)
318 goto err;
319
Antti Palosaari845f3502014-04-10 22:00:50 -0300320 memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6);
321 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300322 cmd.rlen = 4;
Antti Palosaari845f3502014-04-10 22:00:50 -0300323 ret = si2168_cmd_execute(s, &cmd);
324 if (ret)
325 goto err;
326
Olli Salonen43911772014-07-17 15:43:27 -0300327 memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6);
328 cmd.wlen = 6;
329 cmd.rlen = 4;
330 ret = si2168_cmd_execute(s, &cmd);
331 if (ret)
332 goto err;
333
Antti Palosaari888680f2014-07-09 19:36:58 -0300334 memcpy(cmd.args, "\x85", 1);
Antti Palosaari845f3502014-04-10 22:00:50 -0300335 cmd.wlen = 1;
336 cmd.rlen = 1;
337 ret = si2168_cmd_execute(s, &cmd);
338 if (ret)
339 goto err;
340
341 s->delivery_system = c->delivery_system;
342
343 return 0;
344err:
Olli Salonen37b4e432014-08-05 08:54:08 -0300345 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300346 return ret;
347}
348
349static int si2168_init(struct dvb_frontend *fe)
350{
351 struct si2168 *s = fe->demodulator_priv;
352 int ret, len, remaining;
353 const struct firmware *fw = NULL;
Olli Salonenc9cb0822014-07-13 10:52:18 -0300354 u8 *fw_file;
Antti Palosaari845f3502014-04-10 22:00:50 -0300355 const unsigned int i2c_wr_max = 8;
356 struct si2168_cmd cmd;
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300357 unsigned int chip_id;
Antti Palosaari845f3502014-04-10 22:00:50 -0300358
Olli Salonen37b4e432014-08-05 08:54:08 -0300359 dev_dbg(&s->client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300360
Antti Palosaari888680f2014-07-09 19:36:58 -0300361 memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
Antti Palosaari845f3502014-04-10 22:00:50 -0300362 cmd.wlen = 13;
363 cmd.rlen = 0;
364 ret = si2168_cmd_execute(s, &cmd);
365 if (ret)
366 goto err;
367
Antti Palosaari888680f2014-07-09 19:36:58 -0300368 memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
Antti Palosaari845f3502014-04-10 22:00:50 -0300369 cmd.wlen = 8;
370 cmd.rlen = 1;
371 ret = si2168_cmd_execute(s, &cmd);
372 if (ret)
373 goto err;
374
Olli Salonenc9cb0822014-07-13 10:52:18 -0300375 /* query chip revision */
Antti Palosaari888680f2014-07-09 19:36:58 -0300376 memcpy(cmd.args, "\x02", 1);
Antti Palosaari845f3502014-04-10 22:00:50 -0300377 cmd.wlen = 1;
378 cmd.rlen = 13;
379 ret = si2168_cmd_execute(s, &cmd);
380 if (ret)
381 goto err;
382
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300383 chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
384 cmd.args[4] << 0;
385
Luis Alves635a90c2014-07-17 19:43:37 -0300386 #define SI2168_A20 ('A' << 24 | 68 << 16 | '2' << 8 | '0' << 0)
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300387 #define SI2168_A30 ('A' << 24 | 68 << 16 | '3' << 8 | '0' << 0)
388 #define SI2168_B40 ('B' << 24 | 68 << 16 | '4' << 8 | '0' << 0)
389
390 switch (chip_id) {
Luis Alves635a90c2014-07-17 19:43:37 -0300391 case SI2168_A20:
392 fw_file = SI2168_A20_FIRMWARE;
393 break;
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300394 case SI2168_A30:
Olli Salonenc9cb0822014-07-13 10:52:18 -0300395 fw_file = SI2168_A30_FIRMWARE;
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300396 break;
397 case SI2168_B40:
398 fw_file = SI2168_B40_FIRMWARE;
399 break;
400 default:
Olli Salonenc9cb0822014-07-13 10:52:18 -0300401 dev_err(&s->client->dev,
Olli Salonen37b4e432014-08-05 08:54:08 -0300402 "unknown chip version Si21%d-%c%c%c\n",
403 cmd.args[2], cmd.args[1],
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300404 cmd.args[3], cmd.args[4]);
Olli Salonenc9cb0822014-07-13 10:52:18 -0300405 ret = -EINVAL;
406 goto err;
407 }
408
Antti Palosaari845f3502014-04-10 22:00:50 -0300409 /* cold state - try to download firmware */
Olli Salonen37b4e432014-08-05 08:54:08 -0300410 dev_info(&s->client->dev, "found a '%s' in cold state\n",
411 si2168_ops.info.name);
Antti Palosaari845f3502014-04-10 22:00:50 -0300412
413 /* request the firmware, this will block and timeout */
414 ret = request_firmware(&fw, fw_file, &s->client->dev);
415 if (ret) {
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300416 /* fallback mechanism to handle old name for Si2168 B40 fw */
417 if (chip_id == SI2168_B40) {
Olli Salonenc9cb0822014-07-13 10:52:18 -0300418 fw_file = SI2168_B40_FIRMWARE_FALLBACK;
419 ret = request_firmware(&fw, fw_file, &s->client->dev);
420 }
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300421
422 if (ret == 0) {
423 dev_notice(&s->client->dev,
Olli Salonen37b4e432014-08-05 08:54:08 -0300424 "please install firmware file '%s'\n",
425 SI2168_B40_FIRMWARE);
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300426 } else {
427 dev_err(&s->client->dev,
Olli Salonen37b4e432014-08-05 08:54:08 -0300428 "firmware file '%s' not found\n",
429 fw_file);
Olli Salonenc9cb0822014-07-13 10:52:18 -0300430 goto err;
431 }
Antti Palosaari845f3502014-04-10 22:00:50 -0300432 }
433
Olli Salonen37b4e432014-08-05 08:54:08 -0300434 dev_info(&s->client->dev, "downloading firmware from file '%s'\n",
435 fw_file);
Antti Palosaari845f3502014-04-10 22:00:50 -0300436
437 for (remaining = fw->size; remaining > 0; remaining -= i2c_wr_max) {
438 len = remaining;
439 if (len > i2c_wr_max)
440 len = i2c_wr_max;
441
442 memcpy(cmd.args, &fw->data[fw->size - remaining], len);
443 cmd.wlen = len;
444 cmd.rlen = 1;
445 ret = si2168_cmd_execute(s, &cmd);
446 if (ret) {
447 dev_err(&s->client->dev,
Olli Salonen37b4e432014-08-05 08:54:08 -0300448 "firmware download failed=%d\n",
449 ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300450 goto err;
451 }
452 }
453
454 release_firmware(fw);
455 fw = NULL;
456
Antti Palosaari888680f2014-07-09 19:36:58 -0300457 memcpy(cmd.args, "\x01\x01", 2);
Antti Palosaari845f3502014-04-10 22:00:50 -0300458 cmd.wlen = 2;
459 cmd.rlen = 1;
460 ret = si2168_cmd_execute(s, &cmd);
461 if (ret)
462 goto err;
463
Olli Salonen37b4e432014-08-05 08:54:08 -0300464 dev_info(&s->client->dev, "found a '%s' in warm state\n",
465 si2168_ops.info.name);
Antti Palosaari845f3502014-04-10 22:00:50 -0300466
467 s->active = true;
468
469 return 0;
470err:
471 if (fw)
472 release_firmware(fw);
473
Olli Salonen37b4e432014-08-05 08:54:08 -0300474 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300475 return ret;
476}
477
478static int si2168_sleep(struct dvb_frontend *fe)
479{
480 struct si2168 *s = fe->demodulator_priv;
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300481 int ret;
482 struct si2168_cmd cmd;
Antti Palosaari845f3502014-04-10 22:00:50 -0300483
Olli Salonen37b4e432014-08-05 08:54:08 -0300484 dev_dbg(&s->client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300485
486 s->active = false;
487
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300488 memcpy(cmd.args, "\x13", 1);
489 cmd.wlen = 1;
490 cmd.rlen = 0;
491 ret = si2168_cmd_execute(s, &cmd);
492 if (ret)
493 goto err;
494
Antti Palosaari845f3502014-04-10 22:00:50 -0300495 return 0;
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300496err:
Olli Salonen37b4e432014-08-05 08:54:08 -0300497 dev_dbg(&s->client->dev, "failed=%d\n", ret);
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300498 return ret;
Antti Palosaari845f3502014-04-10 22:00:50 -0300499}
500
501static int si2168_get_tune_settings(struct dvb_frontend *fe,
502 struct dvb_frontend_tune_settings *s)
503{
504 s->min_delay_ms = 900;
505
506 return 0;
507}
508
509/*
510 * I2C gate logic
511 * We must use unlocked i2c_transfer() here because I2C lock is already taken
512 * by tuner driver.
513 */
514static int si2168_select(struct i2c_adapter *adap, void *mux_priv, u32 chan)
515{
516 struct si2168 *s = mux_priv;
517 int ret;
518 struct i2c_msg gate_open_msg = {
519 .addr = s->client->addr,
520 .flags = 0,
521 .len = 3,
522 .buf = "\xc0\x0d\x01",
523 };
524
525 mutex_lock(&s->i2c_mutex);
526
527 /* open tuner I2C gate */
528 ret = __i2c_transfer(s->client->adapter, &gate_open_msg, 1);
529 if (ret != 1) {
Olli Salonen37b4e432014-08-05 08:54:08 -0300530 dev_warn(&s->client->dev, "i2c write failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300531 if (ret >= 0)
532 ret = -EREMOTEIO;
533 } else {
534 ret = 0;
535 }
536
537 return ret;
538}
539
540static int si2168_deselect(struct i2c_adapter *adap, void *mux_priv, u32 chan)
541{
542 struct si2168 *s = mux_priv;
543 int ret;
544 struct i2c_msg gate_close_msg = {
545 .addr = s->client->addr,
546 .flags = 0,
547 .len = 3,
548 .buf = "\xc0\x0d\x00",
549 };
550
551 /* close tuner I2C gate */
552 ret = __i2c_transfer(s->client->adapter, &gate_close_msg, 1);
553 if (ret != 1) {
Olli Salonen37b4e432014-08-05 08:54:08 -0300554 dev_warn(&s->client->dev, "i2c write failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300555 if (ret >= 0)
556 ret = -EREMOTEIO;
557 } else {
558 ret = 0;
559 }
560
561 mutex_unlock(&s->i2c_mutex);
562
563 return ret;
564}
565
566static const struct dvb_frontend_ops si2168_ops = {
Antti Palosaaric7908852014-04-12 01:58:32 -0300567 .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
Antti Palosaari845f3502014-04-10 22:00:50 -0300568 .info = {
569 .name = "Silicon Labs Si2168",
570 .caps = FE_CAN_FEC_1_2 |
571 FE_CAN_FEC_2_3 |
572 FE_CAN_FEC_3_4 |
573 FE_CAN_FEC_5_6 |
574 FE_CAN_FEC_7_8 |
575 FE_CAN_FEC_AUTO |
576 FE_CAN_QPSK |
577 FE_CAN_QAM_16 |
578 FE_CAN_QAM_32 |
579 FE_CAN_QAM_64 |
580 FE_CAN_QAM_128 |
581 FE_CAN_QAM_256 |
582 FE_CAN_QAM_AUTO |
583 FE_CAN_TRANSMISSION_MODE_AUTO |
584 FE_CAN_GUARD_INTERVAL_AUTO |
585 FE_CAN_HIERARCHY_AUTO |
586 FE_CAN_MUTE_TS |
587 FE_CAN_2G_MODULATION
588 },
589
590 .get_tune_settings = si2168_get_tune_settings,
591
592 .init = si2168_init,
593 .sleep = si2168_sleep,
594
595 .set_frontend = si2168_set_frontend,
596
597 .read_status = si2168_read_status,
598};
599
600static int si2168_probe(struct i2c_client *client,
601 const struct i2c_device_id *id)
602{
603 struct si2168_config *config = client->dev.platform_data;
604 struct si2168 *s;
605 int ret;
Antti Palosaari845f3502014-04-10 22:00:50 -0300606
Olli Salonen37b4e432014-08-05 08:54:08 -0300607 dev_dbg(&client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300608
609 s = kzalloc(sizeof(struct si2168), GFP_KERNEL);
610 if (!s) {
611 ret = -ENOMEM;
Olli Salonen37b4e432014-08-05 08:54:08 -0300612 dev_err(&client->dev, "kzalloc() failed\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300613 goto err;
614 }
615
616 s->client = client;
617 mutex_init(&s->i2c_mutex);
618
Antti Palosaari845f3502014-04-10 22:00:50 -0300619 /* create mux i2c adapter for tuner */
620 s->adapter = i2c_add_mux_adapter(client->adapter, &client->dev, s,
621 0, 0, 0, si2168_select, si2168_deselect);
Luis Alves4d6efc72014-07-17 16:38:08 -0300622 if (s->adapter == NULL) {
623 ret = -ENODEV;
Antti Palosaari845f3502014-04-10 22:00:50 -0300624 goto err;
Luis Alves4d6efc72014-07-17 16:38:08 -0300625 }
Antti Palosaari845f3502014-04-10 22:00:50 -0300626
627 /* create dvb_frontend */
628 memcpy(&s->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
629 s->fe.demodulator_priv = s;
630
631 *config->i2c_adapter = s->adapter;
632 *config->fe = &s->fe;
633
634 i2c_set_clientdata(client, s);
635
636 dev_info(&s->client->dev,
Olli Salonen37b4e432014-08-05 08:54:08 -0300637 "Silicon Labs Si2168 successfully attached\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300638 return 0;
639err:
640 kfree(s);
Olli Salonen37b4e432014-08-05 08:54:08 -0300641 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300642 return ret;
643}
644
645static int si2168_remove(struct i2c_client *client)
646{
647 struct si2168 *s = i2c_get_clientdata(client);
648
Olli Salonen37b4e432014-08-05 08:54:08 -0300649 dev_dbg(&client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300650
651 i2c_del_mux_adapter(s->adapter);
652
653 s->fe.ops.release = NULL;
654 s->fe.demodulator_priv = NULL;
655
656 kfree(s);
657
658 return 0;
659}
660
661static const struct i2c_device_id si2168_id[] = {
662 {"si2168", 0},
663 {}
664};
665MODULE_DEVICE_TABLE(i2c, si2168_id);
666
667static struct i2c_driver si2168_driver = {
668 .driver = {
669 .owner = THIS_MODULE,
670 .name = "si2168",
671 },
672 .probe = si2168_probe,
673 .remove = si2168_remove,
674 .id_table = si2168_id,
675};
676
677module_i2c_driver(si2168_driver);
678
679MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
680MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
681MODULE_LICENSE("GPL");
Luis Alves635a90c2014-07-17 19:43:37 -0300682MODULE_FIRMWARE(SI2168_A20_FIRMWARE);
Antti Palosaari668aa632014-07-13 16:09:03 -0300683MODULE_FIRMWARE(SI2168_A30_FIRMWARE);
Olli Salonenc9cb0822014-07-13 10:52:18 -0300684MODULE_FIRMWARE(SI2168_B40_FIRMWARE);