blob: 507382160e5e7524022c782240c36f98c1b54871 [file] [log] [blame]
Antti Palosaariba92ae02014-04-14 21:51:32 -03001/*
CrazyCat17ad09f2014-11-14 18:19:37 -03002 * Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner driver
Antti Palosaariba92ae02014-04-14 21:51:32 -03003 *
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 Palosaari930a8732014-04-10 21:58:10 -030017#include "si2157_priv.h"
18
Olli Salonen1b923732014-07-13 10:52:20 -030019static const struct dvb_tuner_ops si2157_ops;
20
Antti Palosaari930a8732014-04-10 21:58:10 -030021/* execute firmware command */
Antti Palosaari8fe51722014-12-06 14:40:06 -030022static int si2157_cmd_execute(struct i2c_client *client, struct si2157_cmd *cmd)
Antti Palosaari930a8732014-04-10 21:58:10 -030023{
Antti Palosaari8fe51722014-12-06 14:40:06 -030024 struct si2157_dev *dev = i2c_get_clientdata(client);
Antti Palosaari930a8732014-04-10 21:58:10 -030025 int ret;
Antti Palosaari930a8732014-04-10 21:58:10 -030026 unsigned long timeout;
27
Antti Palosaari70f13ff2014-12-05 17:46:30 -030028 mutex_lock(&dev->i2c_mutex);
Antti Palosaari930a8732014-04-10 21:58:10 -030029
Antti Palosaarie6b43802014-07-10 06:02:53 -030030 if (cmd->wlen) {
Antti Palosaari930a8732014-04-10 21:58:10 -030031 /* write cmd and args for firmware */
Antti Palosaari8fe51722014-12-06 14:40:06 -030032 ret = i2c_master_send(client, cmd->args, cmd->wlen);
Antti Palosaari930a8732014-04-10 21:58:10 -030033 if (ret < 0) {
34 goto err_mutex_unlock;
Antti Palosaarie6b43802014-07-10 06:02:53 -030035 } else if (ret != cmd->wlen) {
Antti Palosaari930a8732014-04-10 21:58:10 -030036 ret = -EREMOTEIO;
37 goto err_mutex_unlock;
38 }
39 }
40
Antti Palosaarie6b43802014-07-10 06:02:53 -030041 if (cmd->rlen) {
42 /* wait cmd execution terminate */
43 #define TIMEOUT 80
44 timeout = jiffies + msecs_to_jiffies(TIMEOUT);
45 while (!time_after(jiffies, timeout)) {
Antti Palosaari8fe51722014-12-06 14:40:06 -030046 ret = i2c_master_recv(client, cmd->args, cmd->rlen);
Antti Palosaarie6b43802014-07-10 06:02:53 -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;
Antti Palosaari930a8732014-04-10 21:58:10 -030057 }
58
Antti Palosaari8fe51722014-12-06 14:40:06 -030059 dev_dbg(&client->dev, "cmd execution took %d ms\n",
Antti Palosaarie6b43802014-07-10 06:02:53 -030060 jiffies_to_msecs(jiffies) -
61 (jiffies_to_msecs(timeout) - TIMEOUT));
62
63 if (!((cmd->args[0] >> 7) & 0x01)) {
64 ret = -ETIMEDOUT;
65 goto err_mutex_unlock;
66 }
Antti Palosaari930a8732014-04-10 21:58:10 -030067 }
68
Antti Palosaari7cbf5d72014-12-06 14:04:05 -030069 mutex_unlock(&dev->i2c_mutex);
70 return 0;
Antti Palosaari930a8732014-04-10 21:58:10 -030071
72err_mutex_unlock:
Antti Palosaari70f13ff2014-12-05 17:46:30 -030073 mutex_unlock(&dev->i2c_mutex);
Antti Palosaari8fe51722014-12-06 14:40:06 -030074 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari930a8732014-04-10 21:58:10 -030075 return ret;
76}
77
78static int si2157_init(struct dvb_frontend *fe)
79{
Antti Palosaari8fe51722014-12-06 14:40:06 -030080 struct i2c_client *client = fe->tuner_priv;
81 struct si2157_dev *dev = i2c_get_clientdata(client);
Antti Palosaari2f1ea292014-09-07 11:20:34 -030082 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari7d6bc602014-07-13 20:05:28 -030083 int ret, len, remaining;
Olli Salonenb1541212014-07-13 10:52:19 -030084 struct si2157_cmd cmd;
Antti Palosaari78dab402014-12-06 14:51:17 -030085 const struct firmware *fw;
Antti Palosaari08272db2014-12-06 18:15:18 -030086 const char *fw_name;
Antti Palosaari7d6bc602014-07-13 20:05:28 -030087 unsigned int chip_id;
Antti Palosaari930a8732014-04-10 21:58:10 -030088
Antti Palosaari8fe51722014-12-06 14:40:06 -030089 dev_dbg(&client->dev, "\n");
Antti Palosaari930a8732014-04-10 21:58:10 -030090
Antti Palosaari70f13ff2014-12-05 17:46:30 -030091 if (dev->fw_loaded)
Olli Salonen4cbf6ed2014-08-25 15:07:03 -030092 goto warm;
93
94 /* power up */
Antti Palosaari70f13ff2014-12-05 17:46:30 -030095 if (dev->chiptype == SI2157_CHIPTYPE_SI2146) {
Olli Salonen073f3842014-11-24 03:57:33 -030096 memcpy(cmd.args, "\xc0\x05\x01\x00\x00\x0b\x00\x00\x01", 9);
97 cmd.wlen = 9;
98 } else {
99 memcpy(cmd.args, "\xc0\x00\x0c\x00\x00\x01\x01\x01\x01\x01\x01\x02\x00\x00\x01", 15);
100 cmd.wlen = 15;
101 }
Olli Salonenb1541212014-07-13 10:52:19 -0300102 cmd.rlen = 1;
Antti Palosaari8fe51722014-12-06 14:40:06 -0300103 ret = si2157_cmd_execute(client, &cmd);
Olli Salonenb1541212014-07-13 10:52:19 -0300104 if (ret)
105 goto err;
106
107 /* query chip revision */
108 memcpy(cmd.args, "\x02", 1);
109 cmd.wlen = 1;
110 cmd.rlen = 13;
Antti Palosaari8fe51722014-12-06 14:40:06 -0300111 ret = si2157_cmd_execute(client, &cmd);
Olli Salonenb1541212014-07-13 10:52:19 -0300112 if (ret)
113 goto err;
114
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300115 chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
116 cmd.args[4] << 0;
Olli Salonen1b923732014-07-13 10:52:20 -0300117
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300118 #define SI2158_A20 ('A' << 24 | 58 << 16 | '2' << 8 | '0' << 0)
CrazyCat17ad09f2014-11-14 18:19:37 -0300119 #define SI2148_A20 ('A' << 24 | 48 << 16 | '2' << 8 | '0' << 0)
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300120 #define SI2157_A30 ('A' << 24 | 57 << 16 | '3' << 8 | '0' << 0)
Olli Salonend87a5052014-09-11 17:01:38 -0300121 #define SI2147_A30 ('A' << 24 | 47 << 16 | '3' << 8 | '0' << 0)
Olli Salonen073f3842014-11-24 03:57:33 -0300122 #define SI2146_A10 ('A' << 24 | 46 << 16 | '1' << 8 | '0' << 0)
Olli Salonen1b923732014-07-13 10:52:20 -0300123
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300124 switch (chip_id) {
125 case SI2158_A20:
CrazyCat17ad09f2014-11-14 18:19:37 -0300126 case SI2148_A20:
Antti Palosaari08272db2014-12-06 18:15:18 -0300127 fw_name = SI2158_A20_FIRMWARE;
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300128 break;
129 case SI2157_A30:
Olli Salonend87a5052014-09-11 17:01:38 -0300130 case SI2147_A30:
Olli Salonen073f3842014-11-24 03:57:33 -0300131 case SI2146_A10:
Antti Palosaari08272db2014-12-06 18:15:18 -0300132 fw_name = NULL;
Antti Palosaari6c372532014-12-06 18:12:39 -0300133 break;
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300134 default:
Antti Palosaari8fe51722014-12-06 14:40:06 -0300135 dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
Olli Salonen67d01132014-08-05 09:03:54 -0300136 cmd.args[2], cmd.args[1],
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300137 cmd.args[3], cmd.args[4]);
138 ret = -EINVAL;
139 goto err;
Olli Salonen1b923732014-07-13 10:52:20 -0300140 }
141
Antti Palosaari6c372532014-12-06 18:12:39 -0300142 dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n",
143 cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
144
Antti Palosaari08272db2014-12-06 18:15:18 -0300145 if (fw_name == NULL)
Antti Palosaari6c372532014-12-06 18:12:39 -0300146 goto skip_fw_download;
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300147
148 /* request the firmware, this will block and timeout */
Antti Palosaari08272db2014-12-06 18:15:18 -0300149 ret = request_firmware(&fw, fw_name, &client->dev);
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300150 if (ret) {
Antti Palosaari8fe51722014-12-06 14:40:06 -0300151 dev_err(&client->dev, "firmware file '%s' not found\n",
Antti Palosaari08272db2014-12-06 18:15:18 -0300152 fw_name);
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300153 goto err;
154 }
155
156 /* firmware should be n chunks of 17 bytes */
157 if (fw->size % 17 != 0) {
Antti Palosaari8fe51722014-12-06 14:40:06 -0300158 dev_err(&client->dev, "firmware file '%s' is invalid\n",
Antti Palosaari08272db2014-12-06 18:15:18 -0300159 fw_name);
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300160 ret = -EINVAL;
Antti Palosaari78dab402014-12-06 14:51:17 -0300161 goto err_release_firmware;
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300162 }
163
Antti Palosaari8fe51722014-12-06 14:40:06 -0300164 dev_info(&client->dev, "downloading firmware from file '%s'\n",
Antti Palosaari08272db2014-12-06 18:15:18 -0300165 fw_name);
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300166
167 for (remaining = fw->size; remaining > 0; remaining -= 17) {
168 len = fw->data[fw->size - remaining];
169 memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
170 cmd.wlen = len;
171 cmd.rlen = 1;
Antti Palosaari8fe51722014-12-06 14:40:06 -0300172 ret = si2157_cmd_execute(client, &cmd);
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300173 if (ret) {
Antti Palosaari8fe51722014-12-06 14:40:06 -0300174 dev_err(&client->dev, "firmware download failed %d\n",
Olli Salonen67d01132014-08-05 09:03:54 -0300175 ret);
Antti Palosaari78dab402014-12-06 14:51:17 -0300176 goto err_release_firmware;
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300177 }
178 }
179
180 release_firmware(fw);
Antti Palosaari7d6bc602014-07-13 20:05:28 -0300181
182skip_fw_download:
Olli Salonenb1541212014-07-13 10:52:19 -0300183 /* reboot the tuner with new firmware? */
184 memcpy(cmd.args, "\x01\x01", 2);
185 cmd.wlen = 2;
186 cmd.rlen = 1;
Antti Palosaari8fe51722014-12-06 14:40:06 -0300187 ret = si2157_cmd_execute(client, &cmd);
Olli Salonenb1541212014-07-13 10:52:19 -0300188 if (ret)
189 goto err;
190
Antti Palosaaric7011842014-12-06 17:25:24 -0300191 /* query firmware version */
192 memcpy(cmd.args, "\x11", 1);
193 cmd.wlen = 1;
194 cmd.rlen = 10;
195 ret = si2157_cmd_execute(client, &cmd);
196 if (ret)
197 goto err;
198
199 dev_info(&client->dev, "firmware version: %c.%c.%d\n",
200 cmd.args[6], cmd.args[7], cmd.args[8]);
201
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300202 dev->fw_loaded = true;
Antti Palosaari930a8732014-04-10 21:58:10 -0300203
Olli Salonen4cbf6ed2014-08-25 15:07:03 -0300204warm:
Antti Palosaari2f1ea292014-09-07 11:20:34 -0300205 /* init statistics in order signal app which are supported */
206 c->strength.len = 1;
207 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
208 /* start statistics polling */
209 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(1000));
210
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300211 dev->active = true;
Antti Palosaari930a8732014-04-10 21:58:10 -0300212 return 0;
Antti Palosaari78dab402014-12-06 14:51:17 -0300213err_release_firmware:
Markus Elfringdc89cfc2014-11-30 15:05:48 -0300214 release_firmware(fw);
Markus Elfringe358c8a2014-11-30 16:48:24 -0300215err:
Antti Palosaari8fe51722014-12-06 14:40:06 -0300216 dev_dbg(&client->dev, "failed=%d\n", ret);
Olli Salonenb1541212014-07-13 10:52:19 -0300217 return ret;
Antti Palosaari930a8732014-04-10 21:58:10 -0300218}
219
220static int si2157_sleep(struct dvb_frontend *fe)
221{
Antti Palosaari8fe51722014-12-06 14:40:06 -0300222 struct i2c_client *client = fe->tuner_priv;
223 struct si2157_dev *dev = i2c_get_clientdata(client);
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300224 int ret;
225 struct si2157_cmd cmd;
Antti Palosaari930a8732014-04-10 21:58:10 -0300226
Antti Palosaari8fe51722014-12-06 14:40:06 -0300227 dev_dbg(&client->dev, "\n");
Antti Palosaari930a8732014-04-10 21:58:10 -0300228
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300229 dev->active = false;
Antti Palosaari930a8732014-04-10 21:58:10 -0300230
Antti Palosaari2f1ea292014-09-07 11:20:34 -0300231 /* stop statistics polling */
232 cancel_delayed_work_sync(&dev->stat_work);
233
Olli Salonen0e382332014-08-25 15:07:02 -0300234 /* standby */
235 memcpy(cmd.args, "\x16\x00", 2);
236 cmd.wlen = 2;
237 cmd.rlen = 1;
Antti Palosaari8fe51722014-12-06 14:40:06 -0300238 ret = si2157_cmd_execute(client, &cmd);
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300239 if (ret)
240 goto err;
241
Antti Palosaari930a8732014-04-10 21:58:10 -0300242 return 0;
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300243err:
Antti Palosaari8fe51722014-12-06 14:40:06 -0300244 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaaria83d7d12014-07-09 18:37:30 -0300245 return ret;
Antti Palosaari930a8732014-04-10 21:58:10 -0300246}
247
248static int si2157_set_params(struct dvb_frontend *fe)
249{
Antti Palosaari8fe51722014-12-06 14:40:06 -0300250 struct i2c_client *client = fe->tuner_priv;
251 struct si2157_dev *dev = i2c_get_clientdata(client);
Antti Palosaari930a8732014-04-10 21:58:10 -0300252 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
253 int ret;
254 struct si2157_cmd cmd;
Olli Salonena1dad502014-07-13 10:52:21 -0300255 u8 bandwidth, delivery_system;
Olli Salonenc28aebe2015-02-28 12:25:23 -0300256 u32 if_frequency = 5000000;
Antti Palosaari930a8732014-04-10 21:58:10 -0300257
Antti Palosaari8fe51722014-12-06 14:40:06 -0300258 dev_dbg(&client->dev,
Olli Salonen67d01132014-08-05 09:03:54 -0300259 "delivery_system=%d frequency=%u bandwidth_hz=%u\n",
Antti Palosaari8fe51722014-12-06 14:40:06 -0300260 c->delivery_system, c->frequency, c->bandwidth_hz);
Antti Palosaari930a8732014-04-10 21:58:10 -0300261
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300262 if (!dev->active) {
Antti Palosaari930a8732014-04-10 21:58:10 -0300263 ret = -EAGAIN;
264 goto err;
265 }
266
Olli Salonena1dad502014-07-13 10:52:21 -0300267 if (c->bandwidth_hz <= 6000000)
268 bandwidth = 0x06;
269 else if (c->bandwidth_hz <= 7000000)
270 bandwidth = 0x07;
271 else if (c->bandwidth_hz <= 8000000)
272 bandwidth = 0x08;
273 else
274 bandwidth = 0x0f;
275
276 switch (c->delivery_system) {
Olli Salonen5cd62db2014-08-17 02:24:49 -0300277 case SYS_ATSC:
278 delivery_system = 0x00;
Olli Salonenc28aebe2015-02-28 12:25:23 -0300279 if_frequency = 3250000;
Olli Salonen5cd62db2014-08-17 02:24:49 -0300280 break;
Olli Salonen0d1165f2014-10-30 07:43:16 -0300281 case SYS_DVBC_ANNEX_B:
282 delivery_system = 0x10;
Olli Salonenc28aebe2015-02-28 12:25:23 -0300283 if_frequency = 4000000;
Olli Salonen0d1165f2014-10-30 07:43:16 -0300284 break;
Olli Salonena1dad502014-07-13 10:52:21 -0300285 case SYS_DVBT:
286 case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */
287 delivery_system = 0x20;
288 break;
289 case SYS_DVBC_ANNEX_A:
290 delivery_system = 0x30;
291 break;
292 default:
293 ret = -EINVAL;
294 goto err;
295 }
296
297 memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6);
298 cmd.args[4] = delivery_system | bandwidth;
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300299 if (dev->inversion)
Matthias Schwarzott05024ef2014-07-15 16:34:36 -0300300 cmd.args[5] = 0x01;
Olli Salonena1dad502014-07-13 10:52:21 -0300301 cmd.wlen = 6;
Olli Salonend87a5052014-09-11 17:01:38 -0300302 cmd.rlen = 4;
Antti Palosaari8fe51722014-12-06 14:40:06 -0300303 ret = si2157_cmd_execute(client, &cmd);
Olli Salonend87a5052014-09-11 17:01:38 -0300304 if (ret)
305 goto err;
306
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300307 if (dev->chiptype == SI2157_CHIPTYPE_SI2146)
Olli Salonen073f3842014-11-24 03:57:33 -0300308 memcpy(cmd.args, "\x14\x00\x02\x07\x00\x01", 6);
309 else
Olli Salonenee3c3e42015-05-05 13:54:17 -0300310 memcpy(cmd.args, "\x14\x00\x02\x07\x00\x00", 6);
311 cmd.args[4] = dev->if_port;
Olli Salonend87a5052014-09-11 17:01:38 -0300312 cmd.wlen = 6;
313 cmd.rlen = 4;
Antti Palosaari8fe51722014-12-06 14:40:06 -0300314 ret = si2157_cmd_execute(client, &cmd);
Olli Salonena1dad502014-07-13 10:52:21 -0300315 if (ret)
316 goto err;
317
Olli Salonenc28aebe2015-02-28 12:25:23 -0300318 /* set if frequency if needed */
319 if (if_frequency != dev->if_frequency) {
320 memcpy(cmd.args, "\x14\x00\x06\x07", 4);
321 cmd.args[4] = (if_frequency / 1000) & 0xff;
322 cmd.args[5] = ((if_frequency / 1000) >> 8) & 0xff;
323 cmd.wlen = 6;
324 cmd.rlen = 4;
325 ret = si2157_cmd_execute(client, &cmd);
326 if (ret)
327 goto err;
328
329 dev->if_frequency = if_frequency;
330 }
331
Antti Palosaari930a8732014-04-10 21:58:10 -0300332 /* set frequency */
Olli Salonenb1541212014-07-13 10:52:19 -0300333 memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8);
Antti Palosaari930a8732014-04-10 21:58:10 -0300334 cmd.args[4] = (c->frequency >> 0) & 0xff;
335 cmd.args[5] = (c->frequency >> 8) & 0xff;
336 cmd.args[6] = (c->frequency >> 16) & 0xff;
337 cmd.args[7] = (c->frequency >> 24) & 0xff;
Antti Palosaarie6b43802014-07-10 06:02:53 -0300338 cmd.wlen = 8;
339 cmd.rlen = 1;
Antti Palosaari8fe51722014-12-06 14:40:06 -0300340 ret = si2157_cmd_execute(client, &cmd);
Antti Palosaari930a8732014-04-10 21:58:10 -0300341 if (ret)
342 goto err;
343
344 return 0;
345err:
Antti Palosaari8fe51722014-12-06 14:40:06 -0300346 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari930a8732014-04-10 21:58:10 -0300347 return ret;
348}
349
Matthias Schwarzott11405402014-07-15 04:58:40 -0300350static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
351{
Olli Salonenc28aebe2015-02-28 12:25:23 -0300352 struct i2c_client *client = fe->tuner_priv;
353 struct si2157_dev *dev = i2c_get_clientdata(client);
354
355 *frequency = dev->if_frequency;
Matthias Schwarzott11405402014-07-15 04:58:40 -0300356 return 0;
357}
358
Olli Salonen6cc8a352014-07-18 02:41:12 -0300359static const struct dvb_tuner_ops si2157_ops = {
Antti Palosaari930a8732014-04-10 21:58:10 -0300360 .info = {
CrazyCat17ad09f2014-11-14 18:19:37 -0300361 .name = "Silicon Labs Si2146/2147/2148/2157/2158",
Olli Salonen18acc6a2015-03-03 13:28:35 -0300362 .frequency_min = 55000000,
Antti Palosaari930a8732014-04-10 21:58:10 -0300363 .frequency_max = 862000000,
364 },
365
366 .init = si2157_init,
367 .sleep = si2157_sleep,
368 .set_params = si2157_set_params,
Matthias Schwarzott11405402014-07-15 04:58:40 -0300369 .get_if_frequency = si2157_get_if_frequency,
Antti Palosaari930a8732014-04-10 21:58:10 -0300370};
371
Antti Palosaari2f1ea292014-09-07 11:20:34 -0300372static void si2157_stat_work(struct work_struct *work)
373{
374 struct si2157_dev *dev = container_of(work, struct si2157_dev, stat_work.work);
375 struct dvb_frontend *fe = dev->fe;
376 struct i2c_client *client = fe->tuner_priv;
377 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
378 struct si2157_cmd cmd;
379 int ret;
380
381 dev_dbg(&client->dev, "\n");
382
383 memcpy(cmd.args, "\x42\x00", 2);
384 cmd.wlen = 2;
385 cmd.rlen = 12;
386 ret = si2157_cmd_execute(client, &cmd);
387 if (ret)
388 goto err;
389
390 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
391 c->strength.stat[0].svalue = (s8) cmd.args[3] * 1000;
392
393 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
394 return;
395err:
396 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
397 dev_dbg(&client->dev, "failed=%d\n", ret);
398}
399
Antti Palosaari930a8732014-04-10 21:58:10 -0300400static int si2157_probe(struct i2c_client *client,
401 const struct i2c_device_id *id)
402{
403 struct si2157_config *cfg = client->dev.platform_data;
404 struct dvb_frontend *fe = cfg->fe;
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300405 struct si2157_dev *dev;
Antti Palosaari930a8732014-04-10 21:58:10 -0300406 struct si2157_cmd cmd;
407 int ret;
408
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300409 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
410 if (!dev) {
Antti Palosaari930a8732014-04-10 21:58:10 -0300411 ret = -ENOMEM;
Olli Salonen67d01132014-08-05 09:03:54 -0300412 dev_err(&client->dev, "kzalloc() failed\n");
Antti Palosaari930a8732014-04-10 21:58:10 -0300413 goto err;
414 }
415
Antti Palosaari8fe51722014-12-06 14:40:06 -0300416 i2c_set_clientdata(client, dev);
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300417 dev->fe = cfg->fe;
418 dev->inversion = cfg->inversion;
Olli Salonenee3c3e42015-05-05 13:54:17 -0300419 dev->if_port = cfg->if_port;
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300420 dev->fw_loaded = false;
421 dev->chiptype = (u8)id->driver_data;
Olli Salonenc28aebe2015-02-28 12:25:23 -0300422 dev->if_frequency = 5000000; /* default value of property 0x0706 */
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300423 mutex_init(&dev->i2c_mutex);
Antti Palosaari2f1ea292014-09-07 11:20:34 -0300424 INIT_DELAYED_WORK(&dev->stat_work, si2157_stat_work);
Antti Palosaari930a8732014-04-10 21:58:10 -0300425
426 /* check if the tuner is there */
Antti Palosaarie6b43802014-07-10 06:02:53 -0300427 cmd.wlen = 0;
428 cmd.rlen = 1;
Antti Palosaari8fe51722014-12-06 14:40:06 -0300429 ret = si2157_cmd_execute(client, &cmd);
Antti Palosaari930a8732014-04-10 21:58:10 -0300430 if (ret)
Antti Palosaarie7f3f9f32014-12-06 15:13:31 -0300431 goto err_kfree;
Antti Palosaari930a8732014-04-10 21:58:10 -0300432
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300433 memcpy(&fe->ops.tuner_ops, &si2157_ops, sizeof(struct dvb_tuner_ops));
Antti Palosaari8fe51722014-12-06 14:40:06 -0300434 fe->tuner_priv = client;
Antti Palosaari930a8732014-04-10 21:58:10 -0300435
Antti Palosaari8fe51722014-12-06 14:40:06 -0300436 dev_info(&client->dev, "Silicon Labs %s successfully attached\n",
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300437 dev->chiptype == SI2157_CHIPTYPE_SI2146 ?
CrazyCat17ad09f2014-11-14 18:19:37 -0300438 "Si2146" : "Si2147/2148/2157/2158");
Olli Salonen073f3842014-11-24 03:57:33 -0300439
Antti Palosaari930a8732014-04-10 21:58:10 -0300440 return 0;
Antti Palosaarie7f3f9f32014-12-06 15:13:31 -0300441
442err_kfree:
443 kfree(dev);
Antti Palosaari930a8732014-04-10 21:58:10 -0300444err:
Olli Salonen67d01132014-08-05 09:03:54 -0300445 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari930a8732014-04-10 21:58:10 -0300446 return ret;
447}
448
449static int si2157_remove(struct i2c_client *client)
450{
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300451 struct si2157_dev *dev = i2c_get_clientdata(client);
452 struct dvb_frontend *fe = dev->fe;
Antti Palosaari930a8732014-04-10 21:58:10 -0300453
Olli Salonen67d01132014-08-05 09:03:54 -0300454 dev_dbg(&client->dev, "\n");
Antti Palosaari930a8732014-04-10 21:58:10 -0300455
456 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
457 fe->tuner_priv = NULL;
Antti Palosaari70f13ff2014-12-05 17:46:30 -0300458 kfree(dev);
Antti Palosaari930a8732014-04-10 21:58:10 -0300459
460 return 0;
461}
462
Antti Palosaaric1292232014-12-06 15:07:45 -0300463static const struct i2c_device_id si2157_id_table[] = {
464 {"si2157", SI2157_CHIPTYPE_SI2157},
465 {"si2146", SI2157_CHIPTYPE_SI2146},
Antti Palosaari930a8732014-04-10 21:58:10 -0300466 {}
467};
Antti Palosaaric1292232014-12-06 15:07:45 -0300468MODULE_DEVICE_TABLE(i2c, si2157_id_table);
Antti Palosaari930a8732014-04-10 21:58:10 -0300469
470static struct i2c_driver si2157_driver = {
471 .driver = {
Antti Palosaari930a8732014-04-10 21:58:10 -0300472 .name = "si2157",
473 },
474 .probe = si2157_probe,
475 .remove = si2157_remove,
Antti Palosaaric1292232014-12-06 15:07:45 -0300476 .id_table = si2157_id_table,
Antti Palosaari930a8732014-04-10 21:58:10 -0300477};
478
479module_i2c_driver(si2157_driver);
480
CrazyCat17ad09f2014-11-14 18:19:37 -0300481MODULE_DESCRIPTION("Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner driver");
Antti Palosaari930a8732014-04-10 21:58:10 -0300482MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
483MODULE_LICENSE("GPL");
Antti Palosaaribac53a22014-07-13 16:13:49 -0300484MODULE_FIRMWARE(SI2158_A20_FIRMWARE);