blob: 29a59369ea7bdf7d3132b41c1ec85e630f4314ba [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 */
Antti Palosaari6307b562014-11-25 17:31:43 -030022static int si2168_cmd_execute(struct i2c_client *client, struct si2168_cmd *cmd)
Antti Palosaari845f3502014-04-10 22:00:50 -030023{
Antti Palosaari6307b562014-11-25 17:31:43 -030024 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari845f3502014-04-10 22:00:50 -030025 int ret;
26 unsigned long timeout;
27
Antti Palosaaribd01c762014-11-25 16:46:16 -030028 mutex_lock(&dev->i2c_mutex);
Antti Palosaari845f3502014-04-10 22:00:50 -030029
30 if (cmd->wlen) {
31 /* write cmd and args for firmware */
Antti Palosaari3de35832014-11-25 17:53:21 -030032 ret = i2c_master_send(client, cmd->args, cmd->wlen);
Antti Palosaari845f3502014-04-10 22:00:50 -030033 if (ret < 0) {
34 goto err_mutex_unlock;
35 } else if (ret != cmd->wlen) {
36 ret = -EREMOTEIO;
37 goto err_mutex_unlock;
38 }
39 }
40
41 if (cmd->rlen) {
42 /* wait cmd execution terminate */
Jurgen Kramer551c33e2014-12-08 05:30:44 -030043 #define TIMEOUT 70
Antti Palosaari845f3502014-04-10 22:00:50 -030044 timeout = jiffies + msecs_to_jiffies(TIMEOUT);
45 while (!time_after(jiffies, timeout)) {
Antti Palosaari3de35832014-11-25 17:53:21 -030046 ret = i2c_master_recv(client, cmd->args, cmd->rlen);
Antti Palosaari845f3502014-04-10 22:00:50 -030047 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;
57 }
58
Antti Palosaari3de35832014-11-25 17:53:21 -030059 dev_dbg(&client->dev, "cmd execution took %d ms\n",
Antti Palosaari845f3502014-04-10 22:00:50 -030060 jiffies_to_msecs(jiffies) -
61 (jiffies_to_msecs(timeout) - TIMEOUT));
62
Antti Palosaarieefae302014-06-13 11:08:25 -030063 if (!((cmd->args[0] >> 7) & 0x01)) {
Antti Palosaari845f3502014-04-10 22:00:50 -030064 ret = -ETIMEDOUT;
65 goto err_mutex_unlock;
66 }
67 }
68
Antti Palosaari4affbe12014-12-05 14:30:44 -030069 mutex_unlock(&dev->i2c_mutex);
70 return 0;
Antti Palosaari845f3502014-04-10 22:00:50 -030071
72err_mutex_unlock:
Antti Palosaaribd01c762014-11-25 16:46:16 -030073 mutex_unlock(&dev->i2c_mutex);
Antti Palosaari3de35832014-11-25 17:53:21 -030074 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -030075 return ret;
76}
77
78static int si2168_read_status(struct dvb_frontend *fe, fe_status_t *status)
79{
Antti Palosaari6307b562014-11-25 17:31:43 -030080 struct i2c_client *client = fe->demodulator_priv;
81 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaaribffab932014-04-11 20:35:27 -030082 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari845f3502014-04-10 22:00:50 -030083 int ret;
84 struct si2168_cmd cmd;
85
86 *status = 0;
87
Antti Palosaaribd01c762014-11-25 16:46:16 -030088 if (!dev->active) {
Antti Palosaari845f3502014-04-10 22:00:50 -030089 ret = -EAGAIN;
90 goto err;
91 }
92
Antti Palosaaribffab932014-04-11 20:35:27 -030093 switch (c->delivery_system) {
94 case SYS_DVBT:
Antti Palosaari888680f2014-07-09 19:36:58 -030095 memcpy(cmd.args, "\xa0\x01", 2);
Antti Palosaaribffab932014-04-11 20:35:27 -030096 cmd.wlen = 2;
97 cmd.rlen = 13;
98 break;
Antti Palosaaric7908852014-04-12 01:58:32 -030099 case SYS_DVBC_ANNEX_A:
Antti Palosaari888680f2014-07-09 19:36:58 -0300100 memcpy(cmd.args, "\x90\x01", 2);
Antti Palosaaric7908852014-04-12 01:58:32 -0300101 cmd.wlen = 2;
102 cmd.rlen = 9;
103 break;
Antti Palosaaribffab932014-04-11 20:35:27 -0300104 case SYS_DVBT2:
Antti Palosaari888680f2014-07-09 19:36:58 -0300105 memcpy(cmd.args, "\x50\x01", 2);
Antti Palosaaribffab932014-04-11 20:35:27 -0300106 cmd.wlen = 2;
107 cmd.rlen = 14;
108 break;
109 default:
110 ret = -EINVAL;
111 goto err;
112 }
113
Antti Palosaari6307b562014-11-25 17:31:43 -0300114 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300115 if (ret)
116 goto err;
117
Antti Palosaari722a0422014-04-22 21:36:32 -0300118 switch ((cmd.args[2] >> 1) & 0x03) {
119 case 0x01:
Antti Palosaari845f3502014-04-10 22:00:50 -0300120 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
121 break;
Antti Palosaari722a0422014-04-22 21:36:32 -0300122 case 0x03:
Antti Palosaari845f3502014-04-10 22:00:50 -0300123 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
124 FE_HAS_SYNC | FE_HAS_LOCK;
125 break;
126 }
127
Antti Palosaaribd01c762014-11-25 16:46:16 -0300128 dev->fe_status = *status;
Antti Palosaari845f3502014-04-10 22:00:50 -0300129
Antti Palosaari88ac8f82014-07-09 20:22:27 -0300130 if (*status & FE_HAS_LOCK) {
131 c->cnr.len = 1;
132 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
133 c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4;
134 } else {
135 c->cnr.len = 1;
136 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
137 }
138
Antti Palosaari3de35832014-11-25 17:53:21 -0300139 dev_dbg(&client->dev, "status=%02x args=%*ph\n",
Olli Salonen37b4e432014-08-05 08:54:08 -0300140 *status, cmd.rlen, cmd.args);
Antti Palosaari845f3502014-04-10 22:00:50 -0300141
142 return 0;
143err:
Antti Palosaari3de35832014-11-25 17:53:21 -0300144 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300145 return ret;
146}
147
148static int si2168_set_frontend(struct dvb_frontend *fe)
149{
Antti Palosaari6307b562014-11-25 17:31:43 -0300150 struct i2c_client *client = fe->demodulator_priv;
151 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari845f3502014-04-10 22:00:50 -0300152 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
153 int ret;
154 struct si2168_cmd cmd;
Antti Palosaaribffab932014-04-11 20:35:27 -0300155 u8 bandwidth, delivery_system;
Antti Palosaari845f3502014-04-10 22:00:50 -0300156
Antti Palosaari3de35832014-11-25 17:53:21 -0300157 dev_dbg(&client->dev,
Antti Palosaarie5dd1102014-12-05 15:57:03 -0300158 "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n",
159 c->delivery_system, c->modulation, c->frequency,
160 c->bandwidth_hz, c->symbol_rate, c->inversion,
161 c->stream_id);
Antti Palosaari845f3502014-04-10 22:00:50 -0300162
Antti Palosaaribd01c762014-11-25 16:46:16 -0300163 if (!dev->active) {
Antti Palosaari845f3502014-04-10 22:00:50 -0300164 ret = -EAGAIN;
165 goto err;
166 }
167
Antti Palosaaribffab932014-04-11 20:35:27 -0300168 switch (c->delivery_system) {
169 case SYS_DVBT:
170 delivery_system = 0x20;
171 break;
Antti Palosaaric7908852014-04-12 01:58:32 -0300172 case SYS_DVBC_ANNEX_A:
173 delivery_system = 0x30;
174 break;
Antti Palosaaribffab932014-04-11 20:35:27 -0300175 case SYS_DVBT2:
176 delivery_system = 0x70;
177 break;
178 default:
179 ret = -EINVAL;
180 goto err;
181 }
182
Olli Salonen683e98b2015-01-16 09:35:19 -0300183 if (c->bandwidth_hz == 0) {
184 ret = -EINVAL;
185 goto err;
Olli Salonen17d4d6a2015-01-16 09:35:20 -0300186 } else if (c->bandwidth_hz <= 2000000)
187 bandwidth = 0x02;
188 else if (c->bandwidth_hz <= 5000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300189 bandwidth = 0x05;
Antti Palosaaric7908852014-04-12 01:58:32 -0300190 else if (c->bandwidth_hz <= 6000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300191 bandwidth = 0x06;
Antti Palosaaric7908852014-04-12 01:58:32 -0300192 else if (c->bandwidth_hz <= 7000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300193 bandwidth = 0x07;
Antti Palosaaric7908852014-04-12 01:58:32 -0300194 else if (c->bandwidth_hz <= 8000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300195 bandwidth = 0x08;
Antti Palosaaric7908852014-04-12 01:58:32 -0300196 else if (c->bandwidth_hz <= 9000000)
197 bandwidth = 0x09;
198 else if (c->bandwidth_hz <= 10000000)
199 bandwidth = 0x0a;
200 else
201 bandwidth = 0x0f;
Antti Palosaari845f3502014-04-10 22:00:50 -0300202
203 /* program tuner */
204 if (fe->ops.tuner_ops.set_params) {
205 ret = fe->ops.tuner_ops.set_params(fe);
206 if (ret)
207 goto err;
208 }
209
210 memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5);
211 cmd.wlen = 5;
212 cmd.rlen = 5;
Antti Palosaari6307b562014-11-25 17:31:43 -0300213 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300214 if (ret)
215 goto err;
216
Antti Palosaaribffab932014-04-11 20:35:27 -0300217 /* that has no big effect */
218 if (c->delivery_system == SYS_DVBT)
219 memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6);
Antti Palosaaric7908852014-04-12 01:58:32 -0300220 else if (c->delivery_system == SYS_DVBC_ANNEX_A)
221 memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6);
Antti Palosaaribffab932014-04-11 20:35:27 -0300222 else if (c->delivery_system == SYS_DVBT2)
223 memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6);
Antti Palosaari845f3502014-04-10 22:00:50 -0300224 cmd.wlen = 6;
225 cmd.rlen = 3;
Antti Palosaari6307b562014-11-25 17:31:43 -0300226 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300227 if (ret)
228 goto err;
229
CrazyCate395e572014-08-16 18:33:14 -0300230 if (c->delivery_system == SYS_DVBT2) {
231 /* select PLP */
232 cmd.args[0] = 0x52;
233 cmd.args[1] = c->stream_id & 0xff;
234 cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1;
235 cmd.wlen = 3;
236 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300237 ret = si2168_cmd_execute(client, &cmd);
CrazyCate395e572014-08-16 18:33:14 -0300238 if (ret)
239 goto err;
240 }
241
Antti Palosaari845f3502014-04-10 22:00:50 -0300242 memcpy(cmd.args, "\x51\x03", 2);
243 cmd.wlen = 2;
244 cmd.rlen = 12;
Antti Palosaari6307b562014-11-25 17:31:43 -0300245 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300246 if (ret)
247 goto err;
248
249 memcpy(cmd.args, "\x12\x08\x04", 3);
250 cmd.wlen = 3;
251 cmd.rlen = 3;
Antti Palosaari6307b562014-11-25 17:31:43 -0300252 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300253 if (ret)
254 goto err;
255
Antti Palosaari845f3502014-04-10 22:00:50 -0300256 memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6);
257 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300258 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300259 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300260 if (ret)
261 goto err;
262
263 memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6);
264 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300265 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300266 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300267 if (ret)
268 goto err;
269
Antti Palosaari845f3502014-04-10 22:00:50 -0300270 memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6);
271 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300272 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300273 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300274 if (ret)
275 goto err;
276
277 memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6);
Antti Palosaaribffab932014-04-11 20:35:27 -0300278 cmd.args[4] = delivery_system | bandwidth;
Antti Palosaari845f3502014-04-10 22:00:50 -0300279 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300280 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300281 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300282 if (ret)
283 goto err;
284
Luis Alves32bf8812014-07-17 14:31:28 -0300285 /* set DVB-C symbol rate */
286 if (c->delivery_system == SYS_DVBC_ANNEX_A) {
287 memcpy(cmd.args, "\x14\x00\x02\x11", 4);
Antti Palosaari346d4902014-12-05 14:54:14 -0300288 cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff;
Luis Alves32bf8812014-07-17 14:31:28 -0300289 cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
290 cmd.wlen = 6;
291 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300292 ret = si2168_cmd_execute(client, &cmd);
Luis Alves32bf8812014-07-17 14:31:28 -0300293 if (ret)
294 goto err;
295 }
296
Antti Palosaari845f3502014-04-10 22:00:50 -0300297 memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6);
298 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300299 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300300 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300301 if (ret)
302 goto err;
303
CrazyCat52791972014-11-14 18:22:10 -0300304 memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6);
Antti Palosaaribd01c762014-11-25 16:46:16 -0300305 cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
Antti Palosaari845f3502014-04-10 22:00:50 -0300306 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300307 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300308 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300309 if (ret)
310 goto err;
311
CrazyCat52791972014-11-14 18:22:10 -0300312 memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6);
Antti Palosaaribd01c762014-11-25 16:46:16 -0300313 cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
Antti Palosaari845f3502014-04-10 22:00:50 -0300314 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300315 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300316 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300317 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 Palosaari6307b562014-11-25 17:31:43 -0300323 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300324 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;
Antti Palosaari6307b562014-11-25 17:31:43 -0300330 ret = si2168_cmd_execute(client, &cmd);
Olli Salonen43911772014-07-17 15:43:27 -0300331 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;
Antti Palosaari6307b562014-11-25 17:31:43 -0300337 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300338 if (ret)
339 goto err;
340
Antti Palosaaribd01c762014-11-25 16:46:16 -0300341 dev->delivery_system = c->delivery_system;
Antti Palosaari845f3502014-04-10 22:00:50 -0300342
343 return 0;
344err:
Antti Palosaari3de35832014-11-25 17:53:21 -0300345 dev_dbg(&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{
Antti Palosaari6307b562014-11-25 17:31:43 -0300351 struct i2c_client *client = fe->demodulator_priv;
352 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari845f3502014-04-10 22:00:50 -0300353 int ret, len, remaining;
Antti Palosaari58f66932014-12-05 17:22:42 -0300354 const struct firmware *fw;
Antti Palosaaric32b2812014-12-06 17:53:16 -0300355 const char *fw_name;
Antti Palosaari845f3502014-04-10 22:00:50 -0300356 struct si2168_cmd cmd;
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300357 unsigned int chip_id;
Antti Palosaari845f3502014-04-10 22:00:50 -0300358
Antti Palosaari3de35832014-11-25 17:53:21 -0300359 dev_dbg(&client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300360
Olli Salonen8e417222014-08-25 15:07:04 -0300361 /* initialize */
Antti Palosaari888680f2014-07-09 19:36:58 -0300362 memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
Antti Palosaari845f3502014-04-10 22:00:50 -0300363 cmd.wlen = 13;
364 cmd.rlen = 0;
Antti Palosaari6307b562014-11-25 17:31:43 -0300365 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300366 if (ret)
367 goto err;
368
Antti Palosaaribd01c762014-11-25 16:46:16 -0300369 if (dev->fw_loaded) {
Olli Salonen8e417222014-08-25 15:07:04 -0300370 /* resume */
371 memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
372 cmd.wlen = 8;
373 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300374 ret = si2168_cmd_execute(client, &cmd);
Olli Salonen8e417222014-08-25 15:07:04 -0300375 if (ret)
376 goto err;
377
378 memcpy(cmd.args, "\x85", 1);
379 cmd.wlen = 1;
380 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300381 ret = si2168_cmd_execute(client, &cmd);
Olli Salonen8e417222014-08-25 15:07:04 -0300382 if (ret)
383 goto err;
384
385 goto warm;
386 }
387
388 /* power up */
Antti Palosaari888680f2014-07-09 19:36:58 -0300389 memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
Antti Palosaari845f3502014-04-10 22:00:50 -0300390 cmd.wlen = 8;
391 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300392 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300393 if (ret)
394 goto err;
395
Olli Salonenc9cb0822014-07-13 10:52:18 -0300396 /* query chip revision */
Antti Palosaari888680f2014-07-09 19:36:58 -0300397 memcpy(cmd.args, "\x02", 1);
Antti Palosaari845f3502014-04-10 22:00:50 -0300398 cmd.wlen = 1;
399 cmd.rlen = 13;
Antti Palosaari6307b562014-11-25 17:31:43 -0300400 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300401 if (ret)
402 goto err;
403
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300404 chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
405 cmd.args[4] << 0;
406
Luis Alves635a90c2014-07-17 19:43:37 -0300407 #define SI2168_A20 ('A' << 24 | 68 << 16 | '2' << 8 | '0' << 0)
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300408 #define SI2168_A30 ('A' << 24 | 68 << 16 | '3' << 8 | '0' << 0)
409 #define SI2168_B40 ('B' << 24 | 68 << 16 | '4' << 8 | '0' << 0)
410
411 switch (chip_id) {
Luis Alves635a90c2014-07-17 19:43:37 -0300412 case SI2168_A20:
Antti Palosaaric32b2812014-12-06 17:53:16 -0300413 fw_name = SI2168_A20_FIRMWARE;
Luis Alves635a90c2014-07-17 19:43:37 -0300414 break;
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300415 case SI2168_A30:
Antti Palosaaric32b2812014-12-06 17:53:16 -0300416 fw_name = SI2168_A30_FIRMWARE;
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300417 break;
418 case SI2168_B40:
Antti Palosaaric32b2812014-12-06 17:53:16 -0300419 fw_name = SI2168_B40_FIRMWARE;
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300420 break;
421 default:
Antti Palosaari9b7839c2014-12-06 17:43:27 -0300422 dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
Olli Salonen37b4e432014-08-05 08:54:08 -0300423 cmd.args[2], cmd.args[1],
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300424 cmd.args[3], cmd.args[4]);
Olli Salonenc9cb0822014-07-13 10:52:18 -0300425 ret = -EINVAL;
426 goto err;
427 }
428
Antti Palosaari9b7839c2014-12-06 17:43:27 -0300429 dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n",
430 cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
Antti Palosaari845f3502014-04-10 22:00:50 -0300431
432 /* request the firmware, this will block and timeout */
Antti Palosaaric32b2812014-12-06 17:53:16 -0300433 ret = request_firmware(&fw, fw_name, &client->dev);
Antti Palosaari845f3502014-04-10 22:00:50 -0300434 if (ret) {
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300435 /* fallback mechanism to handle old name for Si2168 B40 fw */
436 if (chip_id == SI2168_B40) {
Antti Palosaaric32b2812014-12-06 17:53:16 -0300437 fw_name = SI2168_B40_FIRMWARE_FALLBACK;
438 ret = request_firmware(&fw, fw_name, &client->dev);
Olli Salonenc9cb0822014-07-13 10:52:18 -0300439 }
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300440
441 if (ret == 0) {
Antti Palosaari3de35832014-11-25 17:53:21 -0300442 dev_notice(&client->dev,
Olli Salonen37b4e432014-08-05 08:54:08 -0300443 "please install firmware file '%s'\n",
444 SI2168_B40_FIRMWARE);
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300445 } else {
Antti Palosaari3de35832014-11-25 17:53:21 -0300446 dev_err(&client->dev,
Olli Salonen37b4e432014-08-05 08:54:08 -0300447 "firmware file '%s' not found\n",
Antti Palosaaric32b2812014-12-06 17:53:16 -0300448 fw_name);
Antti Palosaari346d4902014-12-05 14:54:14 -0300449 goto err_release_firmware;
Olli Salonenc9cb0822014-07-13 10:52:18 -0300450 }
Antti Palosaari845f3502014-04-10 22:00:50 -0300451 }
452
Antti Palosaari3de35832014-11-25 17:53:21 -0300453 dev_info(&client->dev, "downloading firmware from file '%s'\n",
Antti Palosaaric32b2812014-12-06 17:53:16 -0300454 fw_name);
Antti Palosaari845f3502014-04-10 22:00:50 -0300455
Olli Salonen1b97dc92014-11-27 16:42:23 -0300456 if ((fw->size % 17 == 0) && (fw->data[0] > 5)) {
457 /* firmware is in the new format */
458 for (remaining = fw->size; remaining > 0; remaining -= 17) {
459 len = fw->data[fw->size - remaining];
460 memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
461 cmd.wlen = len;
462 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300463 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari68c16a72014-12-05 17:08:22 -0300464 if (ret)
465 break;
Olli Salonen1b97dc92014-11-27 16:42:23 -0300466 }
Antti Palosaari68c16a72014-12-05 17:08:22 -0300467 } else if (fw->size % 8 == 0) {
Olli Salonen1b97dc92014-11-27 16:42:23 -0300468 /* firmware is in the old format */
Antti Palosaari68c16a72014-12-05 17:08:22 -0300469 for (remaining = fw->size; remaining > 0; remaining -= 8) {
470 len = 8;
Olli Salonen1b97dc92014-11-27 16:42:23 -0300471 memcpy(cmd.args, &fw->data[fw->size - remaining], len);
472 cmd.wlen = len;
473 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300474 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari68c16a72014-12-05 17:08:22 -0300475 if (ret)
476 break;
Antti Palosaari845f3502014-04-10 22:00:50 -0300477 }
Antti Palosaari68c16a72014-12-05 17:08:22 -0300478 } else {
479 /* bad or unknown firmware format */
480 ret = -EINVAL;
481 }
482
483 if (ret) {
484 dev_err(&client->dev, "firmware download failed %d\n", ret);
485 goto err_release_firmware;
Antti Palosaari845f3502014-04-10 22:00:50 -0300486 }
487
488 release_firmware(fw);
Antti Palosaari845f3502014-04-10 22:00:50 -0300489
Antti Palosaari888680f2014-07-09 19:36:58 -0300490 memcpy(cmd.args, "\x01\x01", 2);
Antti Palosaari845f3502014-04-10 22:00:50 -0300491 cmd.wlen = 2;
492 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300493 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300494 if (ret)
495 goto err;
496
Olli Salonena594cf22014-11-27 16:42:22 -0300497 /* query firmware version */
498 memcpy(cmd.args, "\x11", 1);
499 cmd.wlen = 1;
500 cmd.rlen = 10;
Antti Palosaari6307b562014-11-25 17:31:43 -0300501 ret = si2168_cmd_execute(client, &cmd);
Olli Salonena594cf22014-11-27 16:42:22 -0300502 if (ret)
503 goto err;
504
Antti Palosaari955e0ab2014-12-05 15:04:12 -0300505 dev_info(&client->dev, "firmware version: %c.%c.%d\n",
Olli Salonena594cf22014-11-27 16:42:22 -0300506 cmd.args[6], cmd.args[7], cmd.args[8]);
507
Olli Salonen389ce392014-08-11 16:58:10 -0300508 /* set ts mode */
509 memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6);
Antti Palosaaribd01c762014-11-25 16:46:16 -0300510 cmd.args[4] |= dev->ts_mode;
Olli Salonen8117a312015-05-05 13:54:14 -0300511 if (dev->ts_clock_gapped)
512 cmd.args[4] |= 0x40;
Olli Salonen389ce392014-08-11 16:58:10 -0300513 cmd.wlen = 6;
514 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300515 ret = si2168_cmd_execute(client, &cmd);
Olli Salonen389ce392014-08-11 16:58:10 -0300516 if (ret)
517 goto err;
518
Antti Palosaaribd01c762014-11-25 16:46:16 -0300519 dev->fw_loaded = true;
Antti Palosaari45c3cbb2014-11-03 18:28:39 -0300520warm:
Antti Palosaaribd01c762014-11-25 16:46:16 -0300521 dev->active = true;
Antti Palosaari845f3502014-04-10 22:00:50 -0300522
523 return 0;
Antti Palosaari9b7839c2014-12-06 17:43:27 -0300524
Antti Palosaari346d4902014-12-05 14:54:14 -0300525err_release_firmware:
Markus Elfring034e1ec2014-11-19 19:23:15 -0300526 release_firmware(fw);
527err:
Antti Palosaari3de35832014-11-25 17:53:21 -0300528 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300529 return ret;
530}
531
532static int si2168_sleep(struct dvb_frontend *fe)
533{
Antti Palosaari6307b562014-11-25 17:31:43 -0300534 struct i2c_client *client = fe->demodulator_priv;
535 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300536 int ret;
537 struct si2168_cmd cmd;
Antti Palosaari845f3502014-04-10 22:00:50 -0300538
Antti Palosaari3de35832014-11-25 17:53:21 -0300539 dev_dbg(&client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300540
Antti Palosaaribd01c762014-11-25 16:46:16 -0300541 dev->active = false;
Antti Palosaari845f3502014-04-10 22:00:50 -0300542
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300543 memcpy(cmd.args, "\x13", 1);
544 cmd.wlen = 1;
545 cmd.rlen = 0;
Antti Palosaari6307b562014-11-25 17:31:43 -0300546 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300547 if (ret)
548 goto err;
549
Antti Palosaari845f3502014-04-10 22:00:50 -0300550 return 0;
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300551err:
Antti Palosaari3de35832014-11-25 17:53:21 -0300552 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300553 return ret;
Antti Palosaari845f3502014-04-10 22:00:50 -0300554}
555
556static int si2168_get_tune_settings(struct dvb_frontend *fe,
557 struct dvb_frontend_tune_settings *s)
558{
559 s->min_delay_ms = 900;
560
561 return 0;
562}
563
564/*
565 * I2C gate logic
566 * We must use unlocked i2c_transfer() here because I2C lock is already taken
567 * by tuner driver.
568 */
569static int si2168_select(struct i2c_adapter *adap, void *mux_priv, u32 chan)
570{
Antti Palosaari6307b562014-11-25 17:31:43 -0300571 struct i2c_client *client = mux_priv;
572 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari845f3502014-04-10 22:00:50 -0300573 int ret;
574 struct i2c_msg gate_open_msg = {
Antti Palosaari3de35832014-11-25 17:53:21 -0300575 .addr = client->addr,
Antti Palosaari845f3502014-04-10 22:00:50 -0300576 .flags = 0,
577 .len = 3,
578 .buf = "\xc0\x0d\x01",
579 };
580
Antti Palosaaribd01c762014-11-25 16:46:16 -0300581 mutex_lock(&dev->i2c_mutex);
Antti Palosaari845f3502014-04-10 22:00:50 -0300582
583 /* open tuner I2C gate */
Antti Palosaari3de35832014-11-25 17:53:21 -0300584 ret = __i2c_transfer(client->adapter, &gate_open_msg, 1);
Antti Palosaari845f3502014-04-10 22:00:50 -0300585 if (ret != 1) {
Antti Palosaari3de35832014-11-25 17:53:21 -0300586 dev_warn(&client->dev, "i2c write failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300587 if (ret >= 0)
588 ret = -EREMOTEIO;
589 } else {
590 ret = 0;
591 }
592
593 return ret;
594}
595
596static int si2168_deselect(struct i2c_adapter *adap, void *mux_priv, u32 chan)
597{
Antti Palosaari6307b562014-11-25 17:31:43 -0300598 struct i2c_client *client = mux_priv;
599 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari845f3502014-04-10 22:00:50 -0300600 int ret;
601 struct i2c_msg gate_close_msg = {
Antti Palosaari3de35832014-11-25 17:53:21 -0300602 .addr = client->addr,
Antti Palosaari845f3502014-04-10 22:00:50 -0300603 .flags = 0,
604 .len = 3,
605 .buf = "\xc0\x0d\x00",
606 };
607
608 /* close tuner I2C gate */
Antti Palosaari3de35832014-11-25 17:53:21 -0300609 ret = __i2c_transfer(client->adapter, &gate_close_msg, 1);
Antti Palosaari845f3502014-04-10 22:00:50 -0300610 if (ret != 1) {
Antti Palosaari3de35832014-11-25 17:53:21 -0300611 dev_warn(&client->dev, "i2c write failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300612 if (ret >= 0)
613 ret = -EREMOTEIO;
614 } else {
615 ret = 0;
616 }
617
Antti Palosaaribd01c762014-11-25 16:46:16 -0300618 mutex_unlock(&dev->i2c_mutex);
Antti Palosaari845f3502014-04-10 22:00:50 -0300619
620 return ret;
621}
622
623static const struct dvb_frontend_ops si2168_ops = {
Antti Palosaaric7908852014-04-12 01:58:32 -0300624 .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
Antti Palosaari845f3502014-04-10 22:00:50 -0300625 .info = {
626 .name = "Silicon Labs Si2168",
Antti Palosaarif1ecc5d2014-11-25 16:26:49 -0300627 .symbol_rate_min = 1000000,
628 .symbol_rate_max = 7200000,
Antti Palosaari845f3502014-04-10 22:00:50 -0300629 .caps = FE_CAN_FEC_1_2 |
630 FE_CAN_FEC_2_3 |
631 FE_CAN_FEC_3_4 |
632 FE_CAN_FEC_5_6 |
633 FE_CAN_FEC_7_8 |
634 FE_CAN_FEC_AUTO |
635 FE_CAN_QPSK |
636 FE_CAN_QAM_16 |
637 FE_CAN_QAM_32 |
638 FE_CAN_QAM_64 |
639 FE_CAN_QAM_128 |
640 FE_CAN_QAM_256 |
641 FE_CAN_QAM_AUTO |
642 FE_CAN_TRANSMISSION_MODE_AUTO |
643 FE_CAN_GUARD_INTERVAL_AUTO |
644 FE_CAN_HIERARCHY_AUTO |
645 FE_CAN_MUTE_TS |
Olli Salonen327eeb32014-09-23 13:53:09 -0300646 FE_CAN_2G_MODULATION |
647 FE_CAN_MULTISTREAM
Antti Palosaari845f3502014-04-10 22:00:50 -0300648 },
649
650 .get_tune_settings = si2168_get_tune_settings,
651
652 .init = si2168_init,
653 .sleep = si2168_sleep,
654
655 .set_frontend = si2168_set_frontend,
656
657 .read_status = si2168_read_status,
658};
659
660static int si2168_probe(struct i2c_client *client,
661 const struct i2c_device_id *id)
662{
663 struct si2168_config *config = client->dev.platform_data;
Antti Palosaaribd01c762014-11-25 16:46:16 -0300664 struct si2168_dev *dev;
Antti Palosaari845f3502014-04-10 22:00:50 -0300665 int ret;
Antti Palosaari845f3502014-04-10 22:00:50 -0300666
Olli Salonen37b4e432014-08-05 08:54:08 -0300667 dev_dbg(&client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300668
Antti Palosaaribd01c762014-11-25 16:46:16 -0300669 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
670 if (!dev) {
Antti Palosaari845f3502014-04-10 22:00:50 -0300671 ret = -ENOMEM;
Olli Salonen37b4e432014-08-05 08:54:08 -0300672 dev_err(&client->dev, "kzalloc() failed\n");
Antti Palosaari1ee5e7d2014-12-05 16:02:42 -0300673 goto err;
Antti Palosaari845f3502014-04-10 22:00:50 -0300674 }
675
Antti Palosaaribd01c762014-11-25 16:46:16 -0300676 mutex_init(&dev->i2c_mutex);
Antti Palosaari845f3502014-04-10 22:00:50 -0300677
Antti Palosaari845f3502014-04-10 22:00:50 -0300678 /* create mux i2c adapter for tuner */
Antti Palosaari6307b562014-11-25 17:31:43 -0300679 dev->adapter = i2c_add_mux_adapter(client->adapter, &client->dev,
680 client, 0, 0, 0, si2168_select, si2168_deselect);
Antti Palosaaribd01c762014-11-25 16:46:16 -0300681 if (dev->adapter == NULL) {
Luis Alves4d6efc72014-07-17 16:38:08 -0300682 ret = -ENODEV;
Antti Palosaari346d4902014-12-05 14:54:14 -0300683 goto err_kfree;
Luis Alves4d6efc72014-07-17 16:38:08 -0300684 }
Antti Palosaari845f3502014-04-10 22:00:50 -0300685
686 /* create dvb_frontend */
Antti Palosaaribd01c762014-11-25 16:46:16 -0300687 memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
Antti Palosaari6307b562014-11-25 17:31:43 -0300688 dev->fe.demodulator_priv = client;
Antti Palosaaribd01c762014-11-25 16:46:16 -0300689 *config->i2c_adapter = dev->adapter;
690 *config->fe = &dev->fe;
691 dev->ts_mode = config->ts_mode;
692 dev->ts_clock_inv = config->ts_clock_inv;
Olli Salonen8117a312015-05-05 13:54:14 -0300693 dev->ts_clock_gapped = config->ts_clock_gapped;
Antti Palosaaribd01c762014-11-25 16:46:16 -0300694 dev->fw_loaded = false;
Antti Palosaari845f3502014-04-10 22:00:50 -0300695
Antti Palosaaribd01c762014-11-25 16:46:16 -0300696 i2c_set_clientdata(client, dev);
Antti Palosaari845f3502014-04-10 22:00:50 -0300697
Antti Palosaari346d4902014-12-05 14:54:14 -0300698 dev_info(&client->dev, "Silicon Labs Si2168 successfully attached\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300699 return 0;
Antti Palosaari346d4902014-12-05 14:54:14 -0300700err_kfree:
Antti Palosaaribd01c762014-11-25 16:46:16 -0300701 kfree(dev);
Antti Palosaari1ee5e7d2014-12-05 16:02:42 -0300702err:
Olli Salonen37b4e432014-08-05 08:54:08 -0300703 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300704 return ret;
705}
706
707static int si2168_remove(struct i2c_client *client)
708{
Antti Palosaaribd01c762014-11-25 16:46:16 -0300709 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari845f3502014-04-10 22:00:50 -0300710
Olli Salonen37b4e432014-08-05 08:54:08 -0300711 dev_dbg(&client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300712
Antti Palosaaribd01c762014-11-25 16:46:16 -0300713 i2c_del_mux_adapter(dev->adapter);
Antti Palosaari845f3502014-04-10 22:00:50 -0300714
Antti Palosaaribd01c762014-11-25 16:46:16 -0300715 dev->fe.ops.release = NULL;
716 dev->fe.demodulator_priv = NULL;
Antti Palosaari845f3502014-04-10 22:00:50 -0300717
Antti Palosaaribd01c762014-11-25 16:46:16 -0300718 kfree(dev);
Antti Palosaari845f3502014-04-10 22:00:50 -0300719
720 return 0;
721}
722
Antti Palosaari346d4902014-12-05 14:54:14 -0300723static const struct i2c_device_id si2168_id_table[] = {
Antti Palosaari845f3502014-04-10 22:00:50 -0300724 {"si2168", 0},
725 {}
726};
Antti Palosaari346d4902014-12-05 14:54:14 -0300727MODULE_DEVICE_TABLE(i2c, si2168_id_table);
Antti Palosaari845f3502014-04-10 22:00:50 -0300728
729static struct i2c_driver si2168_driver = {
730 .driver = {
731 .owner = THIS_MODULE,
732 .name = "si2168",
733 },
734 .probe = si2168_probe,
735 .remove = si2168_remove,
Antti Palosaari346d4902014-12-05 14:54:14 -0300736 .id_table = si2168_id_table,
Antti Palosaari845f3502014-04-10 22:00:50 -0300737};
738
739module_i2c_driver(si2168_driver);
740
741MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
742MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
743MODULE_LICENSE("GPL");
Luis Alves635a90c2014-07-17 19:43:37 -0300744MODULE_FIRMWARE(SI2168_A20_FIRMWARE);
Antti Palosaari668aa632014-07-13 16:09:03 -0300745MODULE_FIRMWARE(SI2168_A30_FIRMWARE);
Olli Salonenc9cb0822014-07-13 10:52:18 -0300746MODULE_FIRMWARE(SI2168_B40_FIRMWARE);