blob: 5dae571e2f62861c952b6b23fcdcb3ce34b5c861 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Antti Palosaari711615d2014-04-14 21:55:12 -03002/*
3 * Silicon Labs Si2168 DVB-T/T2/C demodulator driver
4 *
5 * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
Antti Palosaari711615d2014-04-14 21:55:12 -03006 */
7
Ron Economos380a6c82017-12-11 19:51:53 -05008#include <linux/delay.h>
9
Antti Palosaari845f3502014-04-10 22:00:50 -030010#include "si2168_priv.h"
11
12static const struct dvb_frontend_ops si2168_ops;
13
Antti Palosaarid2b72f62015-05-29 16:42:33 -030014/* execute firmware command */
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +020015static int si2168_cmd_execute(struct i2c_client *client, struct si2168_cmd *cmd)
Antti Palosaarid2b72f62015-05-29 16:42:33 -030016{
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +020017 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari845f3502014-04-10 22:00:50 -030018 int ret;
19 unsigned long timeout;
20
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +020021 mutex_lock(&dev->i2c_mutex);
22
Antti Palosaari845f3502014-04-10 22:00:50 -030023 if (cmd->wlen) {
24 /* write cmd and args for firmware */
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +020025 ret = i2c_master_send(client, cmd->args, cmd->wlen);
Antti Palosaari845f3502014-04-10 22:00:50 -030026 if (ret < 0) {
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +020027 goto err_mutex_unlock;
Antti Palosaari845f3502014-04-10 22:00:50 -030028 } else if (ret != cmd->wlen) {
29 ret = -EREMOTEIO;
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +020030 goto err_mutex_unlock;
Antti Palosaari845f3502014-04-10 22:00:50 -030031 }
32 }
33
34 if (cmd->rlen) {
35 /* wait cmd execution terminate */
Jurgen Kramer551c33e2014-12-08 05:30:44 -030036 #define TIMEOUT 70
Antti Palosaari845f3502014-04-10 22:00:50 -030037 timeout = jiffies + msecs_to_jiffies(TIMEOUT);
38 while (!time_after(jiffies, timeout)) {
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +020039 ret = i2c_master_recv(client, cmd->args, cmd->rlen);
Antti Palosaari845f3502014-04-10 22:00:50 -030040 if (ret < 0) {
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +020041 goto err_mutex_unlock;
Antti Palosaari845f3502014-04-10 22:00:50 -030042 } else if (ret != cmd->rlen) {
43 ret = -EREMOTEIO;
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +020044 goto err_mutex_unlock;
Antti Palosaari845f3502014-04-10 22:00:50 -030045 }
46
47 /* firmware ready? */
48 if ((cmd->args[0] >> 7) & 0x01)
49 break;
50 }
51
Antti Palosaari3de35832014-11-25 17:53:21 -030052 dev_dbg(&client->dev, "cmd execution took %d ms\n",
Antti Palosaari845f3502014-04-10 22:00:50 -030053 jiffies_to_msecs(jiffies) -
54 (jiffies_to_msecs(timeout) - TIMEOUT));
55
Olli Salonen7adf99d2015-05-05 13:54:16 -030056 /* error bit set? */
57 if ((cmd->args[0] >> 6) & 0x01) {
58 ret = -EREMOTEIO;
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +020059 goto err_mutex_unlock;
Olli Salonen7adf99d2015-05-05 13:54:16 -030060 }
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;
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +020064 goto err_mutex_unlock;
Antti Palosaari845f3502014-04-10 22:00:50 -030065 }
66 }
67
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +020068 mutex_unlock(&dev->i2c_mutex);
Antti Palosaari4affbe12014-12-05 14:30:44 -030069 return 0;
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +020070err_mutex_unlock:
71 mutex_unlock(&dev->i2c_mutex);
Antti Palosaari3de35832014-11-25 17:53:21 -030072 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -030073 return ret;
74}
75
Brad Love1844f492018-02-12 15:19:04 -050076static int si2168_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
77{
78 struct i2c_client *client = fe->demodulator_priv;
79 struct si2168_dev *dev = i2c_get_clientdata(client);
80 struct si2168_cmd cmd;
81 int ret = 0;
82
83 dev_dbg(&client->dev, "%s acquire: %d\n", __func__, acquire);
84
85 /* set TS_MODE property */
86 memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6);
87 if (acquire)
88 cmd.args[4] |= dev->ts_mode;
89 else
90 cmd.args[4] |= SI2168_TS_TRISTATE;
91 if (dev->ts_clock_gapped)
92 cmd.args[4] |= 0x40;
93 cmd.wlen = 6;
94 cmd.rlen = 4;
95 ret = si2168_cmd_execute(client, &cmd);
96
97 return ret;
98}
99
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300100static int si2168_read_status(struct dvb_frontend *fe, enum fe_status *status)
Antti Palosaari845f3502014-04-10 22:00:50 -0300101{
Antti Palosaari6307b562014-11-25 17:31:43 -0300102 struct i2c_client *client = fe->demodulator_priv;
103 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaaribffab932014-04-11 20:35:27 -0300104 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari8393c002016-12-08 22:19:19 -0200105 int ret, i;
106 unsigned int utmp, utmp1, utmp2;
Antti Palosaari845f3502014-04-10 22:00:50 -0300107 struct si2168_cmd cmd;
108
109 *status = 0;
110
Antti Palosaaribd01c762014-11-25 16:46:16 -0300111 if (!dev->active) {
Antti Palosaari845f3502014-04-10 22:00:50 -0300112 ret = -EAGAIN;
113 goto err;
114 }
115
Antti Palosaaribffab932014-04-11 20:35:27 -0300116 switch (c->delivery_system) {
117 case SYS_DVBT:
Antti Palosaari888680f2014-07-09 19:36:58 -0300118 memcpy(cmd.args, "\xa0\x01", 2);
Antti Palosaaribffab932014-04-11 20:35:27 -0300119 cmd.wlen = 2;
120 cmd.rlen = 13;
121 break;
Antti Palosaaric7908852014-04-12 01:58:32 -0300122 case SYS_DVBC_ANNEX_A:
Antti Palosaari888680f2014-07-09 19:36:58 -0300123 memcpy(cmd.args, "\x90\x01", 2);
Antti Palosaaric7908852014-04-12 01:58:32 -0300124 cmd.wlen = 2;
125 cmd.rlen = 9;
126 break;
Antti Palosaaribffab932014-04-11 20:35:27 -0300127 case SYS_DVBT2:
Antti Palosaari888680f2014-07-09 19:36:58 -0300128 memcpy(cmd.args, "\x50\x01", 2);
Antti Palosaaribffab932014-04-11 20:35:27 -0300129 cmd.wlen = 2;
130 cmd.rlen = 14;
131 break;
132 default:
133 ret = -EINVAL;
134 goto err;
135 }
136
Antti Palosaari6307b562014-11-25 17:31:43 -0300137 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300138 if (ret)
139 goto err;
140
Antti Palosaari722a0422014-04-22 21:36:32 -0300141 switch ((cmd.args[2] >> 1) & 0x03) {
142 case 0x01:
Antti Palosaari845f3502014-04-10 22:00:50 -0300143 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
144 break;
Antti Palosaari722a0422014-04-22 21:36:32 -0300145 case 0x03:
Antti Palosaari845f3502014-04-10 22:00:50 -0300146 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
147 FE_HAS_SYNC | FE_HAS_LOCK;
148 break;
149 }
150
Antti Palosaaribd01c762014-11-25 16:46:16 -0300151 dev->fe_status = *status;
Antti Palosaari845f3502014-04-10 22:00:50 -0300152
Antti Palosaari88ac8f82014-07-09 20:22:27 -0300153 if (*status & FE_HAS_LOCK) {
154 c->cnr.len = 1;
155 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
156 c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4;
157 } else {
158 c->cnr.len = 1;
159 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
160 }
161
Antti Palosaari3de35832014-11-25 17:53:21 -0300162 dev_dbg(&client->dev, "status=%02x args=%*ph\n",
Olli Salonen37b4e432014-08-05 08:54:08 -0300163 *status, cmd.rlen, cmd.args);
Antti Palosaari845f3502014-04-10 22:00:50 -0300164
Antti Palosaari8393c002016-12-08 22:19:19 -0200165 /* BER */
166 if (*status & FE_HAS_VITERBI) {
167 memcpy(cmd.args, "\x82\x00", 2);
168 cmd.wlen = 2;
169 cmd.rlen = 3;
170 ret = si2168_cmd_execute(client, &cmd);
171 if (ret)
172 goto err;
173
174 /*
175 * Firmware returns [0, 255] mantissa and [0, 8] exponent.
176 * Convert to DVB API: mantissa * 10^(8 - exponent) / 10^8
177 */
178 utmp = clamp(8 - cmd.args[1], 0, 8);
179 for (i = 0, utmp1 = 1; i < utmp; i++)
180 utmp1 = utmp1 * 10;
181
182 utmp1 = cmd.args[2] * utmp1;
183 utmp2 = 100000000; /* 10^8 */
184
185 dev_dbg(&client->dev,
186 "post_bit_error=%u post_bit_count=%u ber=%u*10^-%u\n",
187 utmp1, utmp2, cmd.args[2], cmd.args[1]);
188
189 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
190 c->post_bit_error.stat[0].uvalue += utmp1;
191 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
192 c->post_bit_count.stat[0].uvalue += utmp2;
193 } else {
194 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
195 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
196 }
197
Antti Palosaaric62d29c2016-12-09 11:11:32 -0200198 /* UCB */
199 if (*status & FE_HAS_SYNC) {
200 memcpy(cmd.args, "\x84\x01", 2);
201 cmd.wlen = 2;
202 cmd.rlen = 3;
203 ret = si2168_cmd_execute(client, &cmd);
204 if (ret)
205 goto err;
206
207 utmp1 = cmd.args[2] << 8 | cmd.args[1] << 0;
208 dev_dbg(&client->dev, "block_error=%u\n", utmp1);
209
210 /* Sometimes firmware returns bogus value */
211 if (utmp1 == 0xffff)
212 utmp1 = 0;
213
214 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
215 c->block_error.stat[0].uvalue += utmp1;
216 } else {
217 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
218 }
219
Antti Palosaari845f3502014-04-10 22:00:50 -0300220 return 0;
221err:
Antti Palosaari3de35832014-11-25 17:53:21 -0300222 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300223 return ret;
224}
225
226static int si2168_set_frontend(struct dvb_frontend *fe)
227{
Antti Palosaari6307b562014-11-25 17:31:43 -0300228 struct i2c_client *client = fe->demodulator_priv;
229 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari845f3502014-04-10 22:00:50 -0300230 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
231 int ret;
232 struct si2168_cmd cmd;
Antti Palosaaribffab932014-04-11 20:35:27 -0300233 u8 bandwidth, delivery_system;
Antti Palosaari845f3502014-04-10 22:00:50 -0300234
Antti Palosaari3de35832014-11-25 17:53:21 -0300235 dev_dbg(&client->dev,
Antti Palosaarie5dd1102014-12-05 15:57:03 -0300236 "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n",
237 c->delivery_system, c->modulation, c->frequency,
238 c->bandwidth_hz, c->symbol_rate, c->inversion,
239 c->stream_id);
Antti Palosaari845f3502014-04-10 22:00:50 -0300240
Antti Palosaaribd01c762014-11-25 16:46:16 -0300241 if (!dev->active) {
Antti Palosaari845f3502014-04-10 22:00:50 -0300242 ret = -EAGAIN;
243 goto err;
244 }
245
Antti Palosaaribffab932014-04-11 20:35:27 -0300246 switch (c->delivery_system) {
247 case SYS_DVBT:
248 delivery_system = 0x20;
249 break;
Antti Palosaaric7908852014-04-12 01:58:32 -0300250 case SYS_DVBC_ANNEX_A:
251 delivery_system = 0x30;
252 break;
Antti Palosaaribffab932014-04-11 20:35:27 -0300253 case SYS_DVBT2:
254 delivery_system = 0x70;
255 break;
256 default:
257 ret = -EINVAL;
258 goto err;
259 }
260
Olli Salonen683e98b2015-01-16 09:35:19 -0300261 if (c->bandwidth_hz == 0) {
262 ret = -EINVAL;
263 goto err;
Olli Salonen17d4d6a2015-01-16 09:35:20 -0300264 } else if (c->bandwidth_hz <= 2000000)
265 bandwidth = 0x02;
266 else if (c->bandwidth_hz <= 5000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300267 bandwidth = 0x05;
Antti Palosaaric7908852014-04-12 01:58:32 -0300268 else if (c->bandwidth_hz <= 6000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300269 bandwidth = 0x06;
Antti Palosaaric7908852014-04-12 01:58:32 -0300270 else if (c->bandwidth_hz <= 7000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300271 bandwidth = 0x07;
Antti Palosaaric7908852014-04-12 01:58:32 -0300272 else if (c->bandwidth_hz <= 8000000)
Antti Palosaaribffab932014-04-11 20:35:27 -0300273 bandwidth = 0x08;
Antti Palosaaric7908852014-04-12 01:58:32 -0300274 else if (c->bandwidth_hz <= 9000000)
275 bandwidth = 0x09;
276 else if (c->bandwidth_hz <= 10000000)
277 bandwidth = 0x0a;
278 else
279 bandwidth = 0x0f;
Antti Palosaari845f3502014-04-10 22:00:50 -0300280
281 /* program tuner */
282 if (fe->ops.tuner_ops.set_params) {
283 ret = fe->ops.tuner_ops.set_params(fe);
284 if (ret)
285 goto err;
286 }
287
288 memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5);
289 cmd.wlen = 5;
290 cmd.rlen = 5;
Antti Palosaari6307b562014-11-25 17:31:43 -0300291 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300292 if (ret)
293 goto err;
294
Antti Palosaaribffab932014-04-11 20:35:27 -0300295 /* that has no big effect */
296 if (c->delivery_system == SYS_DVBT)
297 memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6);
Antti Palosaaric7908852014-04-12 01:58:32 -0300298 else if (c->delivery_system == SYS_DVBC_ANNEX_A)
299 memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6);
Antti Palosaaribffab932014-04-11 20:35:27 -0300300 else if (c->delivery_system == SYS_DVBT2)
301 memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6);
Antti Palosaari845f3502014-04-10 22:00:50 -0300302 cmd.wlen = 6;
303 cmd.rlen = 3;
Antti Palosaari6307b562014-11-25 17:31:43 -0300304 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300305 if (ret)
306 goto err;
307
CrazyCate395e572014-08-16 18:33:14 -0300308 if (c->delivery_system == SYS_DVBT2) {
309 /* select PLP */
310 cmd.args[0] = 0x52;
311 cmd.args[1] = c->stream_id & 0xff;
312 cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1;
313 cmd.wlen = 3;
314 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300315 ret = si2168_cmd_execute(client, &cmd);
CrazyCate395e572014-08-16 18:33:14 -0300316 if (ret)
317 goto err;
318 }
319
Antti Palosaari845f3502014-04-10 22:00:50 -0300320 memcpy(cmd.args, "\x51\x03", 2);
321 cmd.wlen = 2;
322 cmd.rlen = 12;
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
327 memcpy(cmd.args, "\x12\x08\x04", 3);
328 cmd.wlen = 3;
329 cmd.rlen = 3;
Antti Palosaari6307b562014-11-25 17:31:43 -0300330 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300331 if (ret)
332 goto err;
333
Antti Palosaari845f3502014-04-10 22:00:50 -0300334 memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6);
335 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300336 cmd.rlen = 4;
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
341 memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6);
342 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300343 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300344 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300345 if (ret)
346 goto err;
347
Antti Palosaari845f3502014-04-10 22:00:50 -0300348 memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6);
349 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300350 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300351 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300352 if (ret)
353 goto err;
354
355 memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6);
Antti Palosaaribffab932014-04-11 20:35:27 -0300356 cmd.args[4] = delivery_system | bandwidth;
Brad Loved4c779b2018-01-17 17:31:58 -0500357 if (dev->spectral_inversion)
358 cmd.args[5] |= 1;
Antti Palosaari845f3502014-04-10 22:00:50 -0300359 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300360 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300361 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300362 if (ret)
363 goto err;
364
Luis Alves32bf8812014-07-17 14:31:28 -0300365 /* set DVB-C symbol rate */
366 if (c->delivery_system == SYS_DVBC_ANNEX_A) {
367 memcpy(cmd.args, "\x14\x00\x02\x11", 4);
Antti Palosaari346d4902014-12-05 14:54:14 -0300368 cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff;
Luis Alves32bf8812014-07-17 14:31:28 -0300369 cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
370 cmd.wlen = 6;
371 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300372 ret = si2168_cmd_execute(client, &cmd);
Luis Alves32bf8812014-07-17 14:31:28 -0300373 if (ret)
374 goto err;
375 }
376
Antti Palosaari845f3502014-04-10 22:00:50 -0300377 memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6);
378 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300379 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300380 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300381 if (ret)
382 goto err;
383
CrazyCat52791972014-11-14 18:22:10 -0300384 memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6);
Antti Palosaaribd01c762014-11-25 16:46:16 -0300385 cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
Antti Palosaari845f3502014-04-10 22:00:50 -0300386 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300387 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300388 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300389 if (ret)
390 goto err;
391
CrazyCat52791972014-11-14 18:22:10 -0300392 memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6);
Antti Palosaaribd01c762014-11-25 16:46:16 -0300393 cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
Antti Palosaari845f3502014-04-10 22:00:50 -0300394 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300395 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300396 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300397 if (ret)
398 goto err;
399
Antti Palosaari845f3502014-04-10 22:00:50 -0300400 memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6);
401 cmd.wlen = 6;
Antti Palosaari1d518c22014-07-11 10:40:29 -0300402 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300403 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300404 if (ret)
405 goto err;
406
Olli Salonen43911772014-07-17 15:43:27 -0300407 memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6);
408 cmd.wlen = 6;
409 cmd.rlen = 4;
Antti Palosaari6307b562014-11-25 17:31:43 -0300410 ret = si2168_cmd_execute(client, &cmd);
Olli Salonen43911772014-07-17 15:43:27 -0300411 if (ret)
412 goto err;
413
Antti Palosaari888680f2014-07-09 19:36:58 -0300414 memcpy(cmd.args, "\x85", 1);
Antti Palosaari845f3502014-04-10 22:00:50 -0300415 cmd.wlen = 1;
416 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300417 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300418 if (ret)
419 goto err;
420
Antti Palosaaribd01c762014-11-25 16:46:16 -0300421 dev->delivery_system = c->delivery_system;
Antti Palosaari845f3502014-04-10 22:00:50 -0300422
Brad Love1844f492018-02-12 15:19:04 -0500423 /* enable ts bus */
424 ret = si2168_ts_bus_ctrl(fe, 1);
425 if (ret)
426 goto err;
427
Antti Palosaari845f3502014-04-10 22:00:50 -0300428 return 0;
429err:
Antti Palosaari3de35832014-11-25 17:53:21 -0300430 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300431 return ret;
432}
433
434static int si2168_init(struct dvb_frontend *fe)
435{
Antti Palosaari6307b562014-11-25 17:31:43 -0300436 struct i2c_client *client = fe->demodulator_priv;
437 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari8393c002016-12-08 22:19:19 -0200438 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari845f3502014-04-10 22:00:50 -0300439 int ret, len, remaining;
Antti Palosaari58f66932014-12-05 17:22:42 -0300440 const struct firmware *fw;
Antti Palosaari845f3502014-04-10 22:00:50 -0300441 struct si2168_cmd cmd;
442
Antti Palosaari3de35832014-11-25 17:53:21 -0300443 dev_dbg(&client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300444
Olli Salonen8e417222014-08-25 15:07:04 -0300445 /* initialize */
Antti Palosaari888680f2014-07-09 19:36:58 -0300446 memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
Antti Palosaari845f3502014-04-10 22:00:50 -0300447 cmd.wlen = 13;
448 cmd.rlen = 0;
Antti Palosaari6307b562014-11-25 17:31:43 -0300449 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300450 if (ret)
451 goto err;
452
Antti Palosaari6ab1e942016-06-29 20:38:17 -0300453 if (dev->warm) {
Olli Salonen8e417222014-08-25 15:07:04 -0300454 /* resume */
455 memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
456 cmd.wlen = 8;
457 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300458 ret = si2168_cmd_execute(client, &cmd);
Olli Salonen8e417222014-08-25 15:07:04 -0300459 if (ret)
460 goto err;
461
Ron Economos380a6c82017-12-11 19:51:53 -0500462 udelay(100);
Olli Salonen8e417222014-08-25 15:07:04 -0300463 memcpy(cmd.args, "\x85", 1);
464 cmd.wlen = 1;
465 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300466 ret = si2168_cmd_execute(client, &cmd);
Olli Salonen8e417222014-08-25 15:07:04 -0300467 if (ret)
468 goto err;
469
470 goto warm;
471 }
472
473 /* power up */
Antti Palosaari888680f2014-07-09 19:36:58 -0300474 memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
Antti Palosaari845f3502014-04-10 22:00:50 -0300475 cmd.wlen = 8;
476 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300477 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300478 if (ret)
479 goto err;
480
Antti Palosaari845f3502014-04-10 22:00:50 -0300481 /* request the firmware, this will block and timeout */
Antti Palosaari6ab1e942016-06-29 20:38:17 -0300482 ret = request_firmware(&fw, dev->firmware_name, &client->dev);
Antti Palosaari845f3502014-04-10 22:00:50 -0300483 if (ret) {
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300484 /* fallback mechanism to handle old name for Si2168 B40 fw */
Antti Palosaari6ab1e942016-06-29 20:38:17 -0300485 if (dev->chip_id == SI2168_CHIP_ID_B40) {
486 dev->firmware_name = SI2168_B40_FIRMWARE_FALLBACK;
487 ret = request_firmware(&fw, dev->firmware_name,
488 &client->dev);
Olli Salonenc9cb0822014-07-13 10:52:18 -0300489 }
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300490
491 if (ret == 0) {
Antti Palosaari3de35832014-11-25 17:53:21 -0300492 dev_notice(&client->dev,
Olli Salonen37b4e432014-08-05 08:54:08 -0300493 "please install firmware file '%s'\n",
494 SI2168_B40_FIRMWARE);
Antti Palosaarib6b6fd62014-07-13 19:22:19 -0300495 } else {
Antti Palosaari3de35832014-11-25 17:53:21 -0300496 dev_err(&client->dev,
Olli Salonen37b4e432014-08-05 08:54:08 -0300497 "firmware file '%s' not found\n",
Antti Palosaari6ab1e942016-06-29 20:38:17 -0300498 dev->firmware_name);
Antti Palosaari346d4902014-12-05 14:54:14 -0300499 goto err_release_firmware;
Olli Salonenc9cb0822014-07-13 10:52:18 -0300500 }
Antti Palosaari845f3502014-04-10 22:00:50 -0300501 }
502
Antti Palosaari3de35832014-11-25 17:53:21 -0300503 dev_info(&client->dev, "downloading firmware from file '%s'\n",
Antti Palosaari6ab1e942016-06-29 20:38:17 -0300504 dev->firmware_name);
Antti Palosaari845f3502014-04-10 22:00:50 -0300505
Olli Salonen1b97dc92014-11-27 16:42:23 -0300506 if ((fw->size % 17 == 0) && (fw->data[0] > 5)) {
507 /* firmware is in the new format */
508 for (remaining = fw->size; remaining > 0; remaining -= 17) {
509 len = fw->data[fw->size - remaining];
Laura Abbott47810b42015-09-29 21:10:09 -0300510 if (len > SI2168_ARGLEN) {
511 ret = -EINVAL;
512 break;
513 }
Olli Salonen1b97dc92014-11-27 16:42:23 -0300514 memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
515 cmd.wlen = len;
516 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300517 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari68c16a72014-12-05 17:08:22 -0300518 if (ret)
519 break;
Olli Salonen1b97dc92014-11-27 16:42:23 -0300520 }
Antti Palosaari68c16a72014-12-05 17:08:22 -0300521 } else if (fw->size % 8 == 0) {
Olli Salonen1b97dc92014-11-27 16:42:23 -0300522 /* firmware is in the old format */
Antti Palosaari68c16a72014-12-05 17:08:22 -0300523 for (remaining = fw->size; remaining > 0; remaining -= 8) {
524 len = 8;
Olli Salonen1b97dc92014-11-27 16:42:23 -0300525 memcpy(cmd.args, &fw->data[fw->size - remaining], len);
526 cmd.wlen = len;
527 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300528 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari68c16a72014-12-05 17:08:22 -0300529 if (ret)
530 break;
Antti Palosaari845f3502014-04-10 22:00:50 -0300531 }
Antti Palosaari68c16a72014-12-05 17:08:22 -0300532 } else {
533 /* bad or unknown firmware format */
534 ret = -EINVAL;
535 }
536
537 if (ret) {
538 dev_err(&client->dev, "firmware download failed %d\n", ret);
539 goto err_release_firmware;
Antti Palosaari845f3502014-04-10 22:00:50 -0300540 }
541
542 release_firmware(fw);
Antti Palosaari845f3502014-04-10 22:00:50 -0300543
Antti Palosaari888680f2014-07-09 19:36:58 -0300544 memcpy(cmd.args, "\x01\x01", 2);
Antti Palosaari845f3502014-04-10 22:00:50 -0300545 cmd.wlen = 2;
546 cmd.rlen = 1;
Antti Palosaari6307b562014-11-25 17:31:43 -0300547 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari845f3502014-04-10 22:00:50 -0300548 if (ret)
549 goto err;
550
Olli Salonena594cf22014-11-27 16:42:22 -0300551 /* query firmware version */
552 memcpy(cmd.args, "\x11", 1);
553 cmd.wlen = 1;
554 cmd.rlen = 10;
Antti Palosaari6307b562014-11-25 17:31:43 -0300555 ret = si2168_cmd_execute(client, &cmd);
Olli Salonena594cf22014-11-27 16:42:22 -0300556 if (ret)
557 goto err;
558
Antti Palosaari6ab1e942016-06-29 20:38:17 -0300559 dev->version = (cmd.args[9] + '@') << 24 | (cmd.args[6] - '0') << 16 |
560 (cmd.args[7] - '0') << 8 | (cmd.args[8]) << 0;
561 dev_info(&client->dev, "firmware version: %c %d.%d.%d\n",
562 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
563 dev->version >> 8 & 0xff, dev->version >> 0 & 0xff);
Olli Salonena594cf22014-11-27 16:42:22 -0300564
Brad Love1844f492018-02-12 15:19:04 -0500565 /* set ts mode */
Brad Love44587772018-01-12 11:19:39 -0500566 ret = si2168_ts_bus_ctrl(fe, 1);
Olli Salonen389ce392014-08-11 16:58:10 -0300567 if (ret)
568 goto err;
569
Antti Palosaari6ab1e942016-06-29 20:38:17 -0300570 dev->warm = true;
Antti Palosaari45c3cbb2014-11-03 18:28:39 -0300571warm:
Antti Palosaari8393c002016-12-08 22:19:19 -0200572 /* Init stats here to indicate which stats are supported */
573 c->cnr.len = 1;
574 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
575 c->post_bit_error.len = 1;
576 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
577 c->post_bit_count.len = 1;
578 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Antti Palosaaric62d29c2016-12-09 11:11:32 -0200579 c->block_error.len = 1;
580 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Antti Palosaari8393c002016-12-08 22:19:19 -0200581
Antti Palosaaribd01c762014-11-25 16:46:16 -0300582 dev->active = true;
Antti Palosaari845f3502014-04-10 22:00:50 -0300583
584 return 0;
Antti Palosaari346d4902014-12-05 14:54:14 -0300585err_release_firmware:
Markus Elfring034e1ec2014-11-19 19:23:15 -0300586 release_firmware(fw);
587err:
Antti Palosaari3de35832014-11-25 17:53:21 -0300588 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300589 return ret;
590}
591
592static int si2168_sleep(struct dvb_frontend *fe)
593{
Antti Palosaari6307b562014-11-25 17:31:43 -0300594 struct i2c_client *client = fe->demodulator_priv;
595 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300596 int ret;
597 struct si2168_cmd cmd;
Antti Palosaari845f3502014-04-10 22:00:50 -0300598
Antti Palosaari3de35832014-11-25 17:53:21 -0300599 dev_dbg(&client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300600
Antti Palosaaribd01c762014-11-25 16:46:16 -0300601 dev->active = false;
Antti Palosaari845f3502014-04-10 22:00:50 -0300602
Brad Love44587772018-01-12 11:19:39 -0500603 /* tri-state data bus */
Brad Love1844f492018-02-12 15:19:04 -0500604 ret = si2168_ts_bus_ctrl(fe, 0);
605 if (ret)
606 goto err;
Brad Love44587772018-01-12 11:19:39 -0500607
Mauro Carvalho Chehabfce61d12018-03-07 08:57:05 -0500608 /* Firmware later than B 4.0-11 loses warm state during sleep */
Antti Palosaari6ab1e942016-06-29 20:38:17 -0300609 if (dev->version > ('B' << 24 | 4 << 16 | 0 << 8 | 11 << 0))
610 dev->warm = false;
611
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300612 memcpy(cmd.args, "\x13", 1);
613 cmd.wlen = 1;
614 cmd.rlen = 0;
Antti Palosaari6307b562014-11-25 17:31:43 -0300615 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300616 if (ret)
617 goto err;
618
Antti Palosaari845f3502014-04-10 22:00:50 -0300619 return 0;
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300620err:
Antti Palosaari3de35832014-11-25 17:53:21 -0300621 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4de0ed72014-07-09 18:45:31 -0300622 return ret;
Antti Palosaari845f3502014-04-10 22:00:50 -0300623}
624
625static int si2168_get_tune_settings(struct dvb_frontend *fe,
626 struct dvb_frontend_tune_settings *s)
627{
628 s->min_delay_ms = 900;
629
630 return 0;
631}
632
Peter Rosin58d7b542016-04-20 08:41:36 +0200633static int si2168_select(struct i2c_mux_core *muxc, u32 chan)
Antti Palosaari845f3502014-04-10 22:00:50 -0300634{
Peter Rosin58d7b542016-04-20 08:41:36 +0200635 struct i2c_client *client = i2c_mux_priv(muxc);
Antti Palosaari845f3502014-04-10 22:00:50 -0300636 int ret;
Antti Palosaarid2b72f62015-05-29 16:42:33 -0300637 struct si2168_cmd cmd;
Antti Palosaari845f3502014-04-10 22:00:50 -0300638
Antti Palosaarid2b72f62015-05-29 16:42:33 -0300639 /* open I2C gate */
640 memcpy(cmd.args, "\xc0\x0d\x01", 3);
641 cmd.wlen = 3;
642 cmd.rlen = 0;
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +0200643 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaarid2b72f62015-05-29 16:42:33 -0300644 if (ret)
645 goto err;
Antti Palosaari845f3502014-04-10 22:00:50 -0300646
Antti Palosaarid2b72f62015-05-29 16:42:33 -0300647 return 0;
648err:
649 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300650 return ret;
651}
652
Peter Rosin58d7b542016-04-20 08:41:36 +0200653static int si2168_deselect(struct i2c_mux_core *muxc, u32 chan)
Antti Palosaari845f3502014-04-10 22:00:50 -0300654{
Peter Rosin58d7b542016-04-20 08:41:36 +0200655 struct i2c_client *client = i2c_mux_priv(muxc);
Antti Palosaari845f3502014-04-10 22:00:50 -0300656 int ret;
Antti Palosaarid2b72f62015-05-29 16:42:33 -0300657 struct si2168_cmd cmd;
Antti Palosaari845f3502014-04-10 22:00:50 -0300658
Antti Palosaarid2b72f62015-05-29 16:42:33 -0300659 /* close I2C gate */
660 memcpy(cmd.args, "\xc0\x0d\x00", 3);
661 cmd.wlen = 3;
662 cmd.rlen = 0;
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +0200663 ret = si2168_cmd_execute(client, &cmd);
Antti Palosaarid2b72f62015-05-29 16:42:33 -0300664 if (ret)
665 goto err;
Antti Palosaari845f3502014-04-10 22:00:50 -0300666
Antti Palosaarid2b72f62015-05-29 16:42:33 -0300667 return 0;
668err:
669 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300670 return ret;
671}
672
673static const struct dvb_frontend_ops si2168_ops = {
Antti Palosaaric7908852014-04-12 01:58:32 -0300674 .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
Antti Palosaari845f3502014-04-10 22:00:50 -0300675 .info = {
676 .name = "Silicon Labs Si2168",
Antti Palosaarif1ecc5d2014-11-25 16:26:49 -0300677 .symbol_rate_min = 1000000,
678 .symbol_rate_max = 7200000,
Antti Palosaari845f3502014-04-10 22:00:50 -0300679 .caps = FE_CAN_FEC_1_2 |
680 FE_CAN_FEC_2_3 |
681 FE_CAN_FEC_3_4 |
682 FE_CAN_FEC_5_6 |
683 FE_CAN_FEC_7_8 |
684 FE_CAN_FEC_AUTO |
685 FE_CAN_QPSK |
686 FE_CAN_QAM_16 |
687 FE_CAN_QAM_32 |
688 FE_CAN_QAM_64 |
689 FE_CAN_QAM_128 |
690 FE_CAN_QAM_256 |
691 FE_CAN_QAM_AUTO |
692 FE_CAN_TRANSMISSION_MODE_AUTO |
693 FE_CAN_GUARD_INTERVAL_AUTO |
694 FE_CAN_HIERARCHY_AUTO |
695 FE_CAN_MUTE_TS |
Olli Salonen327eeb32014-09-23 13:53:09 -0300696 FE_CAN_2G_MODULATION |
697 FE_CAN_MULTISTREAM
Antti Palosaari845f3502014-04-10 22:00:50 -0300698 },
699
700 .get_tune_settings = si2168_get_tune_settings,
701
702 .init = si2168_init,
703 .sleep = si2168_sleep,
704
705 .set_frontend = si2168_set_frontend,
706
707 .read_status = si2168_read_status,
708};
709
710static int si2168_probe(struct i2c_client *client,
711 const struct i2c_device_id *id)
712{
713 struct si2168_config *config = client->dev.platform_data;
Antti Palosaaribd01c762014-11-25 16:46:16 -0300714 struct si2168_dev *dev;
Antti Palosaari845f3502014-04-10 22:00:50 -0300715 int ret;
Antti Palosaari6ab1e942016-06-29 20:38:17 -0300716 struct si2168_cmd cmd;
Antti Palosaari845f3502014-04-10 22:00:50 -0300717
Olli Salonen37b4e432014-08-05 08:54:08 -0300718 dev_dbg(&client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300719
Antti Palosaaribd01c762014-11-25 16:46:16 -0300720 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
721 if (!dev) {
Antti Palosaari845f3502014-04-10 22:00:50 -0300722 ret = -ENOMEM;
Antti Palosaari1ee5e7d2014-12-05 16:02:42 -0300723 goto err;
Antti Palosaari845f3502014-04-10 22:00:50 -0300724 }
725
Antti Palosaari6ab1e942016-06-29 20:38:17 -0300726 i2c_set_clientdata(client, dev);
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +0200727 mutex_init(&dev->i2c_mutex);
728
Antti Palosaari6ab1e942016-06-29 20:38:17 -0300729 /* Initialize */
730 memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
731 cmd.wlen = 13;
732 cmd.rlen = 0;
733 ret = si2168_cmd_execute(client, &cmd);
734 if (ret)
735 goto err_kfree;
736
737 /* Power up */
738 memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
739 cmd.wlen = 8;
740 cmd.rlen = 1;
741 ret = si2168_cmd_execute(client, &cmd);
742 if (ret)
743 goto err_kfree;
744
745 /* Query chip revision */
746 memcpy(cmd.args, "\x02", 1);
747 cmd.wlen = 1;
748 cmd.rlen = 13;
749 ret = si2168_cmd_execute(client, &cmd);
750 if (ret)
751 goto err_kfree;
752
753 dev->chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 |
754 cmd.args[3] << 8 | cmd.args[4] << 0;
755
756 switch (dev->chip_id) {
757 case SI2168_CHIP_ID_A20:
758 dev->firmware_name = SI2168_A20_FIRMWARE;
759 break;
760 case SI2168_CHIP_ID_A30:
761 dev->firmware_name = SI2168_A30_FIRMWARE;
762 break;
763 case SI2168_CHIP_ID_B40:
764 dev->firmware_name = SI2168_B40_FIRMWARE;
765 break;
Evgeny Plehov50d64462017-02-07 19:33:47 -0200766 case SI2168_CHIP_ID_D60:
767 dev->firmware_name = SI2168_D60_FIRMWARE;
768 break;
Antti Palosaari6ab1e942016-06-29 20:38:17 -0300769 default:
770 dev_dbg(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
771 cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
772 ret = -ENODEV;
773 goto err_kfree;
774 }
775
776 dev->version = (cmd.args[1]) << 24 | (cmd.args[3] - '0') << 16 |
777 (cmd.args[4] - '0') << 8 | (cmd.args[5]) << 0;
778
Antti Palosaari845f3502014-04-10 22:00:50 -0300779 /* create mux i2c adapter for tuner */
Peter Rosin58d7b542016-04-20 08:41:36 +0200780 dev->muxc = i2c_mux_alloc(client->adapter, &client->dev,
Antti Palosaarie6d7ffc2016-05-04 22:15:32 +0200781 1, 0, I2C_MUX_LOCKED,
Peter Rosin58d7b542016-04-20 08:41:36 +0200782 si2168_select, si2168_deselect);
783 if (!dev->muxc) {
784 ret = -ENOMEM;
Antti Palosaari346d4902014-12-05 14:54:14 -0300785 goto err_kfree;
Luis Alves4d6efc72014-07-17 16:38:08 -0300786 }
Peter Rosin58d7b542016-04-20 08:41:36 +0200787 dev->muxc->priv = client;
788 ret = i2c_mux_add_adapter(dev->muxc, 0, 0, 0);
789 if (ret)
790 goto err_kfree;
Antti Palosaari845f3502014-04-10 22:00:50 -0300791
792 /* create dvb_frontend */
Antti Palosaaribd01c762014-11-25 16:46:16 -0300793 memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
Antti Palosaari6307b562014-11-25 17:31:43 -0300794 dev->fe.demodulator_priv = client;
Peter Rosin58d7b542016-04-20 08:41:36 +0200795 *config->i2c_adapter = dev->muxc->adapter[0];
Antti Palosaaribd01c762014-11-25 16:46:16 -0300796 *config->fe = &dev->fe;
797 dev->ts_mode = config->ts_mode;
798 dev->ts_clock_inv = config->ts_clock_inv;
Olli Salonen8117a312015-05-05 13:54:14 -0300799 dev->ts_clock_gapped = config->ts_clock_gapped;
Brad Loved4c779b2018-01-17 17:31:58 -0500800 dev->spectral_inversion = config->spectral_inversion;
Antti Palosaari845f3502014-04-10 22:00:50 -0300801
Antti Palosaari6ab1e942016-06-29 20:38:17 -0300802 dev_info(&client->dev, "Silicon Labs Si2168-%c%d%d successfully identified\n",
803 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
804 dev->version >> 8 & 0xff);
805 dev_info(&client->dev, "firmware version: %c %d.%d.%d\n",
806 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
807 dev->version >> 8 & 0xff, dev->version >> 0 & 0xff);
Antti Palosaari845f3502014-04-10 22:00:50 -0300808
Antti Palosaari845f3502014-04-10 22:00:50 -0300809 return 0;
Antti Palosaari346d4902014-12-05 14:54:14 -0300810err_kfree:
Antti Palosaaribd01c762014-11-25 16:46:16 -0300811 kfree(dev);
Antti Palosaari1ee5e7d2014-12-05 16:02:42 -0300812err:
Brad Love3061df02018-01-12 11:19:40 -0500813 dev_warn(&client->dev, "probe failed = %d\n", ret);
Antti Palosaari845f3502014-04-10 22:00:50 -0300814 return ret;
815}
816
817static int si2168_remove(struct i2c_client *client)
818{
Antti Palosaaribd01c762014-11-25 16:46:16 -0300819 struct si2168_dev *dev = i2c_get_clientdata(client);
Antti Palosaari845f3502014-04-10 22:00:50 -0300820
Olli Salonen37b4e432014-08-05 08:54:08 -0300821 dev_dbg(&client->dev, "\n");
Antti Palosaari845f3502014-04-10 22:00:50 -0300822
Peter Rosin58d7b542016-04-20 08:41:36 +0200823 i2c_mux_del_adapters(dev->muxc);
Antti Palosaari845f3502014-04-10 22:00:50 -0300824
Antti Palosaaribd01c762014-11-25 16:46:16 -0300825 dev->fe.ops.release = NULL;
826 dev->fe.demodulator_priv = NULL;
Antti Palosaari845f3502014-04-10 22:00:50 -0300827
Antti Palosaaribd01c762014-11-25 16:46:16 -0300828 kfree(dev);
Antti Palosaari845f3502014-04-10 22:00:50 -0300829
830 return 0;
831}
832
Antti Palosaari346d4902014-12-05 14:54:14 -0300833static const struct i2c_device_id si2168_id_table[] = {
Antti Palosaari845f3502014-04-10 22:00:50 -0300834 {"si2168", 0},
835 {}
836};
Antti Palosaari346d4902014-12-05 14:54:14 -0300837MODULE_DEVICE_TABLE(i2c, si2168_id_table);
Antti Palosaari845f3502014-04-10 22:00:50 -0300838
839static struct i2c_driver si2168_driver = {
840 .driver = {
Antti Palosaarie06be1d2016-06-29 20:38:18 -0300841 .name = "si2168",
842 .suppress_bind_attrs = true,
Antti Palosaari845f3502014-04-10 22:00:50 -0300843 },
844 .probe = si2168_probe,
845 .remove = si2168_remove,
Antti Palosaari346d4902014-12-05 14:54:14 -0300846 .id_table = si2168_id_table,
Antti Palosaari845f3502014-04-10 22:00:50 -0300847};
848
849module_i2c_driver(si2168_driver);
850
851MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
852MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
853MODULE_LICENSE("GPL");
Luis Alves635a90c2014-07-17 19:43:37 -0300854MODULE_FIRMWARE(SI2168_A20_FIRMWARE);
Antti Palosaari668aa632014-07-13 16:09:03 -0300855MODULE_FIRMWARE(SI2168_A30_FIRMWARE);
Olli Salonenc9cb0822014-07-13 10:52:18 -0300856MODULE_FIRMWARE(SI2168_B40_FIRMWARE);
Evgeny Plehov50d64462017-02-07 19:33:47 -0200857MODULE_FIRMWARE(SI2168_D60_FIRMWARE);