blob: 63341f485f9fbb929fd36d014f6fe7ab527ad2cc [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
Antti Palosaaric7908852014-04-12 01:58:32 -0300183 if (c->bandwidth_hz <= 5000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300184 bandwidth = 0x05;
Antti Palosaaric7908852014-04-12 01:58:32 -0300185 else if (c->bandwidth_hz <= 6000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300186 bandwidth = 0x06;
Antti Palosaaric7908852014-04-12 01:58:32 -0300187 else if (c->bandwidth_hz <= 7000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300188 bandwidth = 0x07;
Antti Palosaaric7908852014-04-12 01:58:32 -0300189 else if (c->bandwidth_hz <= 8000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300190 bandwidth = 0x08;
Antti Palosaaric7908852014-04-12 01:58:32 -0300191 else if (c->bandwidth_hz <= 9000000)
192 bandwidth = 0x09;
193 else if (c->bandwidth_hz <= 10000000)
194 bandwidth = 0x0a;
195 else
196 bandwidth = 0x0f;
Antti Palosaari845f3502014-04-10 22:00:50 -0300197
198 /* program tuner */
199 if (fe->ops.tuner_ops.set_params) {
200 ret = fe->ops.tuner_ops.set_params(fe);
201 if (ret)
202 goto err;
203 }
204
205 memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5);
206 cmd.wlen = 5;
207 cmd.rlen = 5;
Antti Palosaari6307b562014-11-25 17:31:43 -0300208 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300209 if (ret)
210 goto err;
211
Antti Palosaaribffab932014-04-11 20:35:27 -0300212 /* that has no big effect */
213 if (c->delivery_system == SYS_DVBT)
214 memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6);
Antti Palosaaric7908852014-04-12 01:58:32 -0300215 else if (c->delivery_system == SYS_DVBC_ANNEX_A)
216 memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6);
Antti Palosaaribffab932014-04-11 20:35:27 -0300217 else if (c->delivery_system == SYS_DVBT2)
218 memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6);
Antti Palosaari845f3502014-04-10 22:00:50 -0300219 cmd.wlen = 6;
220 cmd.rlen = 3;
Antti Palosaari6307b562014-11-25 17:31:43 -0300221 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300222 if (ret)
223 goto err;
224
CrazyCate395e572014-08-16 18:33:14 -0300225 if (c->delivery_system == SYS_DVBT2) {
226 /* select PLP */
227 cmd.args[0] = 0x52;
228 cmd.args[1] = c->stream_id & 0xff;
229 cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1;
230 cmd.wlen = 3;
231 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300232 ret = si2168_cmd_execute(client, &cmd);
CrazyCate395e572014-08-16 18:33:14 -0300233 if (ret)
234 goto err;
235 }
236
Antti Palosaari845f3502014-04-10 22:00:50 -0300237 memcpy(cmd.args, "\x51\x03", 2);
238 cmd.wlen = 2;
239 cmd.rlen = 12;
Antti Palosaari6307b562014-11-25 17:31:43 -0300240 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300241 if (ret)
242 goto err;
243
244 memcpy(cmd.args, "\x12\x08\x04", 3);
245 cmd.wlen = 3;
246 cmd.rlen = 3;
Antti Palosaari6307b562014-11-25 17:31:43 -0300247 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300248 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 Palosaari6307b562014-11-25 17:31:43 -0300254 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300255 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 Palosaari6307b562014-11-25 17:31:43 -0300261 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300262 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 Palosaari6307b562014-11-25 17:31:43 -0300268 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300269 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 Palosaari6307b562014-11-25 17:31:43 -0300276 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300277 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);
Antti Palosaari346d4902014-12-05 14:54:14 -0300283 cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff;
Luis Alves32bf8812014-07-17 14:31:28 -0300284 cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
285 cmd.wlen = 6;
286 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300287 ret = si2168_cmd_execute(client, &cmd);
Luis Alves32bf8812014-07-17 14:31:28 -0300288 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 Palosaari6307b562014-11-25 17:31:43 -0300295 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300296 if (ret)
297 goto err;
298
CrazyCat52791972014-11-14 18:22:10 -0300299 memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6);
Antti Palosaaribd01c762014-11-25 16:46:16 -0300300 cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
Antti Palosaari845f3502014-04-10 22:00:50 -0300301 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300302 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300303 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300304 if (ret)
305 goto err;
306
CrazyCat52791972014-11-14 18:22:10 -0300307 memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6);
Antti Palosaaribd01c762014-11-25 16:46:16 -0300308 cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
Antti Palosaari845f3502014-04-10 22:00:50 -0300309 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300310 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300311 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300312 if (ret)
313 goto err;
314
Antti Palosaari845f3502014-04-10 22:00:50 -0300315 memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6);
316 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300317 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300318 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300319 if (ret)
320 goto err;
321
Olli Salonen43911772014-07-17 15:43:27 -0300322 memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6);
323 cmd.wlen = 6;
324 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300325 ret = si2168_cmd_execute(client, &cmd);
Olli Salonen43911772014-07-17 15:43:27 -0300326 if (ret)
327 goto err;
328
Antti Palosaari888680f2014-07-09 19:36:58 -0300329 memcpy(cmd.args, "\x85", 1);
Antti Palosaari845f3502014-04-10 22:00:50 -0300330 cmd.wlen = 1;
331 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300332 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300333 if (ret)
334 goto err;
335
Antti Palosaaribd01c762014-11-25 16:46:16 -0300336 dev->delivery_system = c->delivery_system;
Antti Palosaari845f3502014-04-10 22:00:50 -0300337
338 return 0;
339err:
Antti Palosaari3de35832014-11-25 17:53:21 -0300340 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300341 return ret;
342}
343
344static int si2168_init(struct dvb_frontend *fe)
345{
Antti Palosaari6307b562014-11-25 17:31:43 -0300346 struct i2c_client *client = fe->demodulator_priv;
347 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari845f3502014-04-10 22:00:50 -0300348 int ret, len, remaining;
Antti Palosaari58f66932014-12-05 17:22:42 -0300349 const struct firmware *fw;
Antti Palosaaric32b2812014-12-06 17:53:16 -0300350 const char *fw_name;
Antti Palosaari845f3502014-04-10 22:00:50 -0300351 struct si2168_cmd cmd;
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300352 unsigned int chip_id;
Antti Palosaari845f3502014-04-10 22:00:50 -0300353
Antti Palosaari3de35832014-11-25 17:53:21 -0300354 dev_dbg(&client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300355
Olli Salonen8e417222014-08-25 15:07:04 -0300356 /* initialize */
Antti Palosaari888680f2014-07-09 19:36:58 -0300357 memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
Antti Palosaari845f3502014-04-10 22:00:50 -0300358 cmd.wlen = 13;
359 cmd.rlen = 0;
Antti Palosaari6307b562014-11-25 17:31:43 -0300360 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300361 if (ret)
362 goto err;
363
Antti Palosaaribd01c762014-11-25 16:46:16 -0300364 if (dev->fw_loaded) {
Olli Salonen8e417222014-08-25 15:07:04 -0300365 /* resume */
366 memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
367 cmd.wlen = 8;
368 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300369 ret = si2168_cmd_execute(client, &cmd);
Olli Salonen8e417222014-08-25 15:07:04 -0300370 if (ret)
371 goto err;
372
373 memcpy(cmd.args, "\x85", 1);
374 cmd.wlen = 1;
375 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300376 ret = si2168_cmd_execute(client, &cmd);
Olli Salonen8e417222014-08-25 15:07:04 -0300377 if (ret)
378 goto err;
379
380 goto warm;
381 }
382
383 /* power up */
Antti Palosaari888680f2014-07-09 19:36:58 -0300384 memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
Antti Palosaari845f3502014-04-10 22:00:50 -0300385 cmd.wlen = 8;
386 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300387 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300388 if (ret)
389 goto err;
390
Olli Salonenc9cb0822014-07-13 10:52:18 -0300391 /* query chip revision */
Antti Palosaari888680f2014-07-09 19:36:58 -0300392 memcpy(cmd.args, "\x02", 1);
Antti Palosaari845f3502014-04-10 22:00:50 -0300393 cmd.wlen = 1;
394 cmd.rlen = 13;
Antti Palosaari6307b562014-11-25 17:31:43 -0300395 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300396 if (ret)
397 goto err;
398
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300399 chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
400 cmd.args[4] << 0;
401
Luis Alves635a90c2014-07-17 19:43:37 -0300402 #define SI2168_A20 ('A' << 24 | 68 << 16 | '2' << 8 | '0' << 0)
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300403 #define SI2168_A30 ('A' << 24 | 68 << 16 | '3' << 8 | '0' << 0)
404 #define SI2168_B40 ('B' << 24 | 68 << 16 | '4' << 8 | '0' << 0)
405
406 switch (chip_id) {
Luis Alves635a90c2014-07-17 19:43:37 -0300407 case SI2168_A20:
Antti Palosaaric32b2812014-12-06 17:53:16 -0300408 fw_name = SI2168_A20_FIRMWARE;
Luis Alves635a90c2014-07-17 19:43:37 -0300409 break;
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300410 case SI2168_A30:
Antti Palosaaric32b2812014-12-06 17:53:16 -0300411 fw_name = SI2168_A30_FIRMWARE;
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300412 break;
413 case SI2168_B40:
Antti Palosaaric32b2812014-12-06 17:53:16 -0300414 fw_name = SI2168_B40_FIRMWARE;
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300415 break;
416 default:
Antti Palosaari9b7839c2014-12-06 17:43:27 -0300417 dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
Olli Salonen37b4e432014-08-05 08:54:08 -0300418 cmd.args[2], cmd.args[1],
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300419 cmd.args[3], cmd.args[4]);
Olli Salonenc9cb0822014-07-13 10:52:18 -0300420 ret = -EINVAL;
421 goto err;
422 }
423
Antti Palosaari9b7839c2014-12-06 17:43:27 -0300424 dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n",
425 cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
Antti Palosaari845f3502014-04-10 22:00:50 -0300426
427 /* request the firmware, this will block and timeout */
Antti Palosaaric32b2812014-12-06 17:53:16 -0300428 ret = request_firmware(&fw, fw_name, &client->dev);
Antti Palosaari845f3502014-04-10 22:00:50 -0300429 if (ret) {
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300430 /* fallback mechanism to handle old name for Si2168 B40 fw */
431 if (chip_id == SI2168_B40) {
Antti Palosaaric32b2812014-12-06 17:53:16 -0300432 fw_name = SI2168_B40_FIRMWARE_FALLBACK;
433 ret = request_firmware(&fw, fw_name, &client->dev);
Olli Salonenc9cb0822014-07-13 10:52:18 -0300434 }
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300435
436 if (ret == 0) {
Antti Palosaari3de35832014-11-25 17:53:21 -0300437 dev_notice(&client->dev,
Olli Salonen37b4e432014-08-05 08:54:08 -0300438 "please install firmware file '%s'\n",
439 SI2168_B40_FIRMWARE);
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300440 } else {
Antti Palosaari3de35832014-11-25 17:53:21 -0300441 dev_err(&client->dev,
Olli Salonen37b4e432014-08-05 08:54:08 -0300442 "firmware file '%s' not found\n",
Antti Palosaaric32b2812014-12-06 17:53:16 -0300443 fw_name);
Antti Palosaari346d4902014-12-05 14:54:14 -0300444 goto err_release_firmware;
Olli Salonenc9cb0822014-07-13 10:52:18 -0300445 }
Antti Palosaari845f3502014-04-10 22:00:50 -0300446 }
447
Antti Palosaari3de35832014-11-25 17:53:21 -0300448 dev_info(&client->dev, "downloading firmware from file '%s'\n",
Antti Palosaaric32b2812014-12-06 17:53:16 -0300449 fw_name);
Antti Palosaari845f3502014-04-10 22:00:50 -0300450
Olli Salonen1b97dc92014-11-27 16:42:23 -0300451 if ((fw->size % 17 == 0) && (fw->data[0] > 5)) {
452 /* firmware is in the new format */
453 for (remaining = fw->size; remaining > 0; remaining -= 17) {
454 len = fw->data[fw->size - remaining];
455 memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
456 cmd.wlen = len;
457 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300458 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari68c16a72014-12-05 17:08:22 -0300459 if (ret)
460 break;
Olli Salonen1b97dc92014-11-27 16:42:23 -0300461 }
Antti Palosaari68c16a72014-12-05 17:08:22 -0300462 } else if (fw->size % 8 == 0) {
Olli Salonen1b97dc92014-11-27 16:42:23 -0300463 /* firmware is in the old format */
Antti Palosaari68c16a72014-12-05 17:08:22 -0300464 for (remaining = fw->size; remaining > 0; remaining -= 8) {
465 len = 8;
Olli Salonen1b97dc92014-11-27 16:42:23 -0300466 memcpy(cmd.args, &fw->data[fw->size - remaining], len);
467 cmd.wlen = len;
468 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300469 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari68c16a72014-12-05 17:08:22 -0300470 if (ret)
471 break;
Antti Palosaari845f3502014-04-10 22:00:50 -0300472 }
Antti Palosaari68c16a72014-12-05 17:08:22 -0300473 } else {
474 /* bad or unknown firmware format */
475 ret = -EINVAL;
476 }
477
478 if (ret) {
479 dev_err(&client->dev, "firmware download failed %d\n", ret);
480 goto err_release_firmware;
Antti Palosaari845f3502014-04-10 22:00:50 -0300481 }
482
483 release_firmware(fw);
Antti Palosaari845f3502014-04-10 22:00:50 -0300484
Antti Palosaari888680f2014-07-09 19:36:58 -0300485 memcpy(cmd.args, "\x01\x01", 2);
Antti Palosaari845f3502014-04-10 22:00:50 -0300486 cmd.wlen = 2;
487 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300488 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300489 if (ret)
490 goto err;
491
Olli Salonena594cf22014-11-27 16:42:22 -0300492 /* query firmware version */
493 memcpy(cmd.args, "\x11", 1);
494 cmd.wlen = 1;
495 cmd.rlen = 10;
Antti Palosaari6307b562014-11-25 17:31:43 -0300496 ret = si2168_cmd_execute(client, &cmd);
Olli Salonena594cf22014-11-27 16:42:22 -0300497 if (ret)
498 goto err;
499
Antti Palosaari955e0ab2014-12-05 15:04:12 -0300500 dev_info(&client->dev, "firmware version: %c.%c.%d\n",
Olli Salonena594cf22014-11-27 16:42:22 -0300501 cmd.args[6], cmd.args[7], cmd.args[8]);
502
Olli Salonen389ce392014-08-11 16:58:10 -0300503 /* set ts mode */
504 memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6);
Antti Palosaaribd01c762014-11-25 16:46:16 -0300505 cmd.args[4] |= dev->ts_mode;
Olli Salonen389ce392014-08-11 16:58:10 -0300506 cmd.wlen = 6;
507 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300508 ret = si2168_cmd_execute(client, &cmd);
Olli Salonen389ce392014-08-11 16:58:10 -0300509 if (ret)
510 goto err;
511
Antti Palosaaribd01c762014-11-25 16:46:16 -0300512 dev->fw_loaded = true;
Antti Palosaari45c3cbb2014-11-03 18:28:39 -0300513warm:
Antti Palosaaribd01c762014-11-25 16:46:16 -0300514 dev->active = true;
Antti Palosaari845f3502014-04-10 22:00:50 -0300515
516 return 0;
Antti Palosaari9b7839c2014-12-06 17:43:27 -0300517
Antti Palosaari346d4902014-12-05 14:54:14 -0300518err_release_firmware:
Markus Elfring034e1ec2014-11-19 19:23:15 -0300519 release_firmware(fw);
520err:
Antti Palosaari3de35832014-11-25 17:53:21 -0300521 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300522 return ret;
523}
524
525static int si2168_sleep(struct dvb_frontend *fe)
526{
Antti Palosaari6307b562014-11-25 17:31:43 -0300527 struct i2c_client *client = fe->demodulator_priv;
528 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300529 int ret;
530 struct si2168_cmd cmd;
Antti Palosaari845f3502014-04-10 22:00:50 -0300531
Antti Palosaari3de35832014-11-25 17:53:21 -0300532 dev_dbg(&client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300533
Antti Palosaaribd01c762014-11-25 16:46:16 -0300534 dev->active = false;
Antti Palosaari845f3502014-04-10 22:00:50 -0300535
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300536 memcpy(cmd.args, "\x13", 1);
537 cmd.wlen = 1;
538 cmd.rlen = 0;
Antti Palosaari6307b562014-11-25 17:31:43 -0300539 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300540 if (ret)
541 goto err;
542
Antti Palosaari845f3502014-04-10 22:00:50 -0300543 return 0;
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300544err:
Antti Palosaari3de35832014-11-25 17:53:21 -0300545 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300546 return ret;
Antti Palosaari845f3502014-04-10 22:00:50 -0300547}
548
549static int si2168_get_tune_settings(struct dvb_frontend *fe,
550 struct dvb_frontend_tune_settings *s)
551{
552 s->min_delay_ms = 900;
553
554 return 0;
555}
556
557/*
558 * I2C gate logic
559 * We must use unlocked i2c_transfer() here because I2C lock is already taken
560 * by tuner driver.
561 */
562static int si2168_select(struct i2c_adapter *adap, void *mux_priv, u32 chan)
563{
Antti Palosaari6307b562014-11-25 17:31:43 -0300564 struct i2c_client *client = mux_priv;
565 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari845f3502014-04-10 22:00:50 -0300566 int ret;
567 struct i2c_msg gate_open_msg = {
Antti Palosaari3de35832014-11-25 17:53:21 -0300568 .addr = client->addr,
Antti Palosaari845f3502014-04-10 22:00:50 -0300569 .flags = 0,
570 .len = 3,
571 .buf = "\xc0\x0d\x01",
572 };
573
Antti Palosaaribd01c762014-11-25 16:46:16 -0300574 mutex_lock(&dev->i2c_mutex);
Antti Palosaari845f3502014-04-10 22:00:50 -0300575
576 /* open tuner I2C gate */
Antti Palosaari3de35832014-11-25 17:53:21 -0300577 ret = __i2c_transfer(client->adapter, &gate_open_msg, 1);
Antti Palosaari845f3502014-04-10 22:00:50 -0300578 if (ret != 1) {
Antti Palosaari3de35832014-11-25 17:53:21 -0300579 dev_warn(&client->dev, "i2c write failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300580 if (ret >= 0)
581 ret = -EREMOTEIO;
582 } else {
583 ret = 0;
584 }
585
586 return ret;
587}
588
589static int si2168_deselect(struct i2c_adapter *adap, void *mux_priv, u32 chan)
590{
Antti Palosaari6307b562014-11-25 17:31:43 -0300591 struct i2c_client *client = mux_priv;
592 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari845f3502014-04-10 22:00:50 -0300593 int ret;
594 struct i2c_msg gate_close_msg = {
Antti Palosaari3de35832014-11-25 17:53:21 -0300595 .addr = client->addr,
Antti Palosaari845f3502014-04-10 22:00:50 -0300596 .flags = 0,
597 .len = 3,
598 .buf = "\xc0\x0d\x00",
599 };
600
601 /* close tuner I2C gate */
Antti Palosaari3de35832014-11-25 17:53:21 -0300602 ret = __i2c_transfer(client->adapter, &gate_close_msg, 1);
Antti Palosaari845f3502014-04-10 22:00:50 -0300603 if (ret != 1) {
Antti Palosaari3de35832014-11-25 17:53:21 -0300604 dev_warn(&client->dev, "i2c write failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300605 if (ret >= 0)
606 ret = -EREMOTEIO;
607 } else {
608 ret = 0;
609 }
610
Antti Palosaaribd01c762014-11-25 16:46:16 -0300611 mutex_unlock(&dev->i2c_mutex);
Antti Palosaari845f3502014-04-10 22:00:50 -0300612
613 return ret;
614}
615
616static const struct dvb_frontend_ops si2168_ops = {
Antti Palosaaric7908852014-04-12 01:58:32 -0300617 .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
Antti Palosaari845f3502014-04-10 22:00:50 -0300618 .info = {
619 .name = "Silicon Labs Si2168",
Antti Palosaarif1ecc5d2014-11-25 16:26:49 -0300620 .symbol_rate_min = 1000000,
621 .symbol_rate_max = 7200000,
Antti Palosaari845f3502014-04-10 22:00:50 -0300622 .caps = FE_CAN_FEC_1_2 |
623 FE_CAN_FEC_2_3 |
624 FE_CAN_FEC_3_4 |
625 FE_CAN_FEC_5_6 |
626 FE_CAN_FEC_7_8 |
627 FE_CAN_FEC_AUTO |
628 FE_CAN_QPSK |
629 FE_CAN_QAM_16 |
630 FE_CAN_QAM_32 |
631 FE_CAN_QAM_64 |
632 FE_CAN_QAM_128 |
633 FE_CAN_QAM_256 |
634 FE_CAN_QAM_AUTO |
635 FE_CAN_TRANSMISSION_MODE_AUTO |
636 FE_CAN_GUARD_INTERVAL_AUTO |
637 FE_CAN_HIERARCHY_AUTO |
638 FE_CAN_MUTE_TS |
Olli Salonen327eeb32014-09-23 13:53:09 -0300639 FE_CAN_2G_MODULATION |
640 FE_CAN_MULTISTREAM
Antti Palosaari845f3502014-04-10 22:00:50 -0300641 },
642
643 .get_tune_settings = si2168_get_tune_settings,
644
645 .init = si2168_init,
646 .sleep = si2168_sleep,
647
648 .set_frontend = si2168_set_frontend,
649
650 .read_status = si2168_read_status,
651};
652
653static int si2168_probe(struct i2c_client *client,
654 const struct i2c_device_id *id)
655{
656 struct si2168_config *config = client->dev.platform_data;
Antti Palosaaribd01c762014-11-25 16:46:16 -0300657 struct si2168_dev *dev;
Antti Palosaari845f3502014-04-10 22:00:50 -0300658 int ret;
Antti Palosaari845f3502014-04-10 22:00:50 -0300659
Olli Salonen37b4e432014-08-05 08:54:08 -0300660 dev_dbg(&client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300661
Antti Palosaaribd01c762014-11-25 16:46:16 -0300662 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
663 if (!dev) {
Antti Palosaari845f3502014-04-10 22:00:50 -0300664 ret = -ENOMEM;
Olli Salonen37b4e432014-08-05 08:54:08 -0300665 dev_err(&client->dev, "kzalloc() failed\n");
Antti Palosaari1ee5e7d2014-12-05 16:02:42 -0300666 goto err;
Antti Palosaari845f3502014-04-10 22:00:50 -0300667 }
668
Antti Palosaaribd01c762014-11-25 16:46:16 -0300669 mutex_init(&dev->i2c_mutex);
Antti Palosaari845f3502014-04-10 22:00:50 -0300670
Antti Palosaari845f3502014-04-10 22:00:50 -0300671 /* create mux i2c adapter for tuner */
Antti Palosaari6307b562014-11-25 17:31:43 -0300672 dev->adapter = i2c_add_mux_adapter(client->adapter, &client->dev,
673 client, 0, 0, 0, si2168_select, si2168_deselect);
Antti Palosaaribd01c762014-11-25 16:46:16 -0300674 if (dev->adapter == NULL) {
Luis Alves4d6efc72014-07-17 16:38:08 -0300675 ret = -ENODEV;
Antti Palosaari346d4902014-12-05 14:54:14 -0300676 goto err_kfree;
Luis Alves4d6efc72014-07-17 16:38:08 -0300677 }
Antti Palosaari845f3502014-04-10 22:00:50 -0300678
679 /* create dvb_frontend */
Antti Palosaaribd01c762014-11-25 16:46:16 -0300680 memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
Antti Palosaari6307b562014-11-25 17:31:43 -0300681 dev->fe.demodulator_priv = client;
Antti Palosaaribd01c762014-11-25 16:46:16 -0300682 *config->i2c_adapter = dev->adapter;
683 *config->fe = &dev->fe;
684 dev->ts_mode = config->ts_mode;
685 dev->ts_clock_inv = config->ts_clock_inv;
686 dev->fw_loaded = false;
Antti Palosaari845f3502014-04-10 22:00:50 -0300687
Antti Palosaaribd01c762014-11-25 16:46:16 -0300688 i2c_set_clientdata(client, dev);
Antti Palosaari845f3502014-04-10 22:00:50 -0300689
Antti Palosaari346d4902014-12-05 14:54:14 -0300690 dev_info(&client->dev, "Silicon Labs Si2168 successfully attached\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300691 return 0;
Antti Palosaari346d4902014-12-05 14:54:14 -0300692err_kfree:
Antti Palosaaribd01c762014-11-25 16:46:16 -0300693 kfree(dev);
Antti Palosaari1ee5e7d2014-12-05 16:02:42 -0300694err:
Olli Salonen37b4e432014-08-05 08:54:08 -0300695 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300696 return ret;
697}
698
699static int si2168_remove(struct i2c_client *client)
700{
Antti Palosaaribd01c762014-11-25 16:46:16 -0300701 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari845f3502014-04-10 22:00:50 -0300702
Olli Salonen37b4e432014-08-05 08:54:08 -0300703 dev_dbg(&client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300704
Antti Palosaaribd01c762014-11-25 16:46:16 -0300705 i2c_del_mux_adapter(dev->adapter);
Antti Palosaari845f3502014-04-10 22:00:50 -0300706
Antti Palosaaribd01c762014-11-25 16:46:16 -0300707 dev->fe.ops.release = NULL;
708 dev->fe.demodulator_priv = NULL;
Antti Palosaari845f3502014-04-10 22:00:50 -0300709
Antti Palosaaribd01c762014-11-25 16:46:16 -0300710 kfree(dev);
Antti Palosaari845f3502014-04-10 22:00:50 -0300711
712 return 0;
713}
714
Antti Palosaari346d4902014-12-05 14:54:14 -0300715static const struct i2c_device_id si2168_id_table[] = {
Antti Palosaari845f3502014-04-10 22:00:50 -0300716 {"si2168", 0},
717 {}
718};
Antti Palosaari346d4902014-12-05 14:54:14 -0300719MODULE_DEVICE_TABLE(i2c, si2168_id_table);
Antti Palosaari845f3502014-04-10 22:00:50 -0300720
721static struct i2c_driver si2168_driver = {
722 .driver = {
723 .owner = THIS_MODULE,
724 .name = "si2168",
725 },
726 .probe = si2168_probe,
727 .remove = si2168_remove,
Antti Palosaari346d4902014-12-05 14:54:14 -0300728 .id_table = si2168_id_table,
Antti Palosaari845f3502014-04-10 22:00:50 -0300729};
730
731module_i2c_driver(si2168_driver);
732
733MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
734MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
735MODULE_LICENSE("GPL");
Luis Alves635a90c2014-07-17 19:43:37 -0300736MODULE_FIRMWARE(SI2168_A20_FIRMWARE);
Antti Palosaari668aa632014-07-13 16:09:03 -0300737MODULE_FIRMWARE(SI2168_A30_FIRMWARE);
Olli Salonenc9cb0822014-07-13 10:52:18 -0300738MODULE_FIRMWARE(SI2168_B40_FIRMWARE);