xref: /openbmc/linux/drivers/media/tuners/si2157.c (revision d7a3d85e)
1 /*
2  * Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner 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 
17 #include "si2157_priv.h"
18 
19 static const struct dvb_tuner_ops si2157_ops;
20 
21 /* execute firmware command */
22 static int si2157_cmd_execute(struct i2c_client *client, struct si2157_cmd *cmd)
23 {
24 	struct si2157_dev *dev = i2c_get_clientdata(client);
25 	int ret;
26 	unsigned long timeout;
27 
28 	mutex_lock(&dev->i2c_mutex);
29 
30 	if (cmd->wlen) {
31 		/* write cmd and args for firmware */
32 		ret = i2c_master_send(client, cmd->args, cmd->wlen);
33 		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 */
43 		#define TIMEOUT 80
44 		timeout = jiffies + msecs_to_jiffies(TIMEOUT);
45 		while (!time_after(jiffies, timeout)) {
46 			ret = i2c_master_recv(client, cmd->args, cmd->rlen);
47 			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 
59 		dev_dbg(&client->dev, "cmd execution took %d ms\n",
60 				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 		}
67 	}
68 
69 	mutex_unlock(&dev->i2c_mutex);
70 	return 0;
71 
72 err_mutex_unlock:
73 	mutex_unlock(&dev->i2c_mutex);
74 	dev_dbg(&client->dev, "failed=%d\n", ret);
75 	return ret;
76 }
77 
78 static int si2157_init(struct dvb_frontend *fe)
79 {
80 	struct i2c_client *client = fe->tuner_priv;
81 	struct si2157_dev *dev = i2c_get_clientdata(client);
82 	int ret, len, remaining;
83 	struct si2157_cmd cmd;
84 	const struct firmware *fw;
85 	const char *fw_name;
86 	unsigned int chip_id;
87 
88 	dev_dbg(&client->dev, "\n");
89 
90 	if (dev->fw_loaded)
91 		goto warm;
92 
93 	/* power up */
94 	if (dev->chiptype == SI2157_CHIPTYPE_SI2146) {
95 		memcpy(cmd.args, "\xc0\x05\x01\x00\x00\x0b\x00\x00\x01", 9);
96 		cmd.wlen = 9;
97 	} else {
98 		memcpy(cmd.args, "\xc0\x00\x0c\x00\x00\x01\x01\x01\x01\x01\x01\x02\x00\x00\x01", 15);
99 		cmd.wlen = 15;
100 	}
101 	cmd.rlen = 1;
102 	ret = si2157_cmd_execute(client, &cmd);
103 	if (ret)
104 		goto err;
105 
106 	/* query chip revision */
107 	memcpy(cmd.args, "\x02", 1);
108 	cmd.wlen = 1;
109 	cmd.rlen = 13;
110 	ret = si2157_cmd_execute(client, &cmd);
111 	if (ret)
112 		goto err;
113 
114 	chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
115 			cmd.args[4] << 0;
116 
117 	#define SI2158_A20 ('A' << 24 | 58 << 16 | '2' << 8 | '0' << 0)
118 	#define SI2148_A20 ('A' << 24 | 48 << 16 | '2' << 8 | '0' << 0)
119 	#define SI2157_A30 ('A' << 24 | 57 << 16 | '3' << 8 | '0' << 0)
120 	#define SI2147_A30 ('A' << 24 | 47 << 16 | '3' << 8 | '0' << 0)
121 	#define SI2146_A10 ('A' << 24 | 46 << 16 | '1' << 8 | '0' << 0)
122 
123 	switch (chip_id) {
124 	case SI2158_A20:
125 	case SI2148_A20:
126 		fw_name = SI2158_A20_FIRMWARE;
127 		break;
128 	case SI2157_A30:
129 	case SI2147_A30:
130 	case SI2146_A10:
131 		fw_name = NULL;
132 		break;
133 	default:
134 		dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
135 				cmd.args[2], cmd.args[1],
136 				cmd.args[3], cmd.args[4]);
137 		ret = -EINVAL;
138 		goto err;
139 	}
140 
141 	dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n",
142 			cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
143 
144 	if (fw_name == NULL)
145 		goto skip_fw_download;
146 
147 	/* request the firmware, this will block and timeout */
148 	ret = request_firmware(&fw, fw_name, &client->dev);
149 	if (ret) {
150 		dev_err(&client->dev, "firmware file '%s' not found\n",
151 				fw_name);
152 		goto err;
153 	}
154 
155 	/* firmware should be n chunks of 17 bytes */
156 	if (fw->size % 17 != 0) {
157 		dev_err(&client->dev, "firmware file '%s' is invalid\n",
158 				fw_name);
159 		ret = -EINVAL;
160 		goto err_release_firmware;
161 	}
162 
163 	dev_info(&client->dev, "downloading firmware from file '%s'\n",
164 			fw_name);
165 
166 	for (remaining = fw->size; remaining > 0; remaining -= 17) {
167 		len = fw->data[fw->size - remaining];
168 		memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
169 		cmd.wlen = len;
170 		cmd.rlen = 1;
171 		ret = si2157_cmd_execute(client, &cmd);
172 		if (ret) {
173 			dev_err(&client->dev, "firmware download failed %d\n",
174 					ret);
175 			goto err_release_firmware;
176 		}
177 	}
178 
179 	release_firmware(fw);
180 
181 skip_fw_download:
182 	/* reboot the tuner with new firmware? */
183 	memcpy(cmd.args, "\x01\x01", 2);
184 	cmd.wlen = 2;
185 	cmd.rlen = 1;
186 	ret = si2157_cmd_execute(client, &cmd);
187 	if (ret)
188 		goto err;
189 
190 	/* query firmware version */
191 	memcpy(cmd.args, "\x11", 1);
192 	cmd.wlen = 1;
193 	cmd.rlen = 10;
194 	ret = si2157_cmd_execute(client, &cmd);
195 	if (ret)
196 		goto err;
197 
198 	dev_info(&client->dev, "firmware version: %c.%c.%d\n",
199 			cmd.args[6], cmd.args[7], cmd.args[8]);
200 
201 	dev->fw_loaded = true;
202 
203 warm:
204 	dev->active = true;
205 	return 0;
206 
207 err_release_firmware:
208 	release_firmware(fw);
209 err:
210 	dev_dbg(&client->dev, "failed=%d\n", ret);
211 	return ret;
212 }
213 
214 static int si2157_sleep(struct dvb_frontend *fe)
215 {
216 	struct i2c_client *client = fe->tuner_priv;
217 	struct si2157_dev *dev = i2c_get_clientdata(client);
218 	int ret;
219 	struct si2157_cmd cmd;
220 
221 	dev_dbg(&client->dev, "\n");
222 
223 	dev->active = false;
224 
225 	/* standby */
226 	memcpy(cmd.args, "\x16\x00", 2);
227 	cmd.wlen = 2;
228 	cmd.rlen = 1;
229 	ret = si2157_cmd_execute(client, &cmd);
230 	if (ret)
231 		goto err;
232 
233 	return 0;
234 err:
235 	dev_dbg(&client->dev, "failed=%d\n", ret);
236 	return ret;
237 }
238 
239 static int si2157_set_params(struct dvb_frontend *fe)
240 {
241 	struct i2c_client *client = fe->tuner_priv;
242 	struct si2157_dev *dev = i2c_get_clientdata(client);
243 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
244 	int ret;
245 	struct si2157_cmd cmd;
246 	u8 bandwidth, delivery_system;
247 	u32 if_frequency = 5000000;
248 
249 	dev_dbg(&client->dev,
250 			"delivery_system=%d frequency=%u bandwidth_hz=%u\n",
251 			c->delivery_system, c->frequency, c->bandwidth_hz);
252 
253 	if (!dev->active) {
254 		ret = -EAGAIN;
255 		goto err;
256 	}
257 
258 	if (c->bandwidth_hz <= 6000000)
259 		bandwidth = 0x06;
260 	else if (c->bandwidth_hz <= 7000000)
261 		bandwidth = 0x07;
262 	else if (c->bandwidth_hz <= 8000000)
263 		bandwidth = 0x08;
264 	else
265 		bandwidth = 0x0f;
266 
267 	switch (c->delivery_system) {
268 	case SYS_ATSC:
269 			delivery_system = 0x00;
270 			if_frequency = 3250000;
271 			break;
272 	case SYS_DVBC_ANNEX_B:
273 			delivery_system = 0x10;
274 			if_frequency = 4000000;
275 			break;
276 	case SYS_DVBT:
277 	case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */
278 			delivery_system = 0x20;
279 			break;
280 	case SYS_DVBC_ANNEX_A:
281 			delivery_system = 0x30;
282 			break;
283 	default:
284 			ret = -EINVAL;
285 			goto err;
286 	}
287 
288 	memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6);
289 	cmd.args[4] = delivery_system | bandwidth;
290 	if (dev->inversion)
291 		cmd.args[5] = 0x01;
292 	cmd.wlen = 6;
293 	cmd.rlen = 4;
294 	ret = si2157_cmd_execute(client, &cmd);
295 	if (ret)
296 		goto err;
297 
298 	if (dev->chiptype == SI2157_CHIPTYPE_SI2146)
299 		memcpy(cmd.args, "\x14\x00\x02\x07\x00\x01", 6);
300 	else
301 		memcpy(cmd.args, "\x14\x00\x02\x07\x01\x00", 6);
302 	cmd.wlen = 6;
303 	cmd.rlen = 4;
304 	ret = si2157_cmd_execute(client, &cmd);
305 	if (ret)
306 		goto err;
307 
308 	/* set if frequency if needed */
309 	if (if_frequency != dev->if_frequency) {
310 		memcpy(cmd.args, "\x14\x00\x06\x07", 4);
311 		cmd.args[4] = (if_frequency / 1000) & 0xff;
312 		cmd.args[5] = ((if_frequency / 1000) >> 8) & 0xff;
313 		cmd.wlen = 6;
314 		cmd.rlen = 4;
315 		ret = si2157_cmd_execute(client, &cmd);
316 		if (ret)
317 			goto err;
318 
319 		dev->if_frequency = if_frequency;
320 	}
321 
322 	/* set frequency */
323 	memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8);
324 	cmd.args[4] = (c->frequency >>  0) & 0xff;
325 	cmd.args[5] = (c->frequency >>  8) & 0xff;
326 	cmd.args[6] = (c->frequency >> 16) & 0xff;
327 	cmd.args[7] = (c->frequency >> 24) & 0xff;
328 	cmd.wlen = 8;
329 	cmd.rlen = 1;
330 	ret = si2157_cmd_execute(client, &cmd);
331 	if (ret)
332 		goto err;
333 
334 	return 0;
335 err:
336 	dev_dbg(&client->dev, "failed=%d\n", ret);
337 	return ret;
338 }
339 
340 static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
341 {
342 	struct i2c_client *client = fe->tuner_priv;
343 	struct si2157_dev *dev = i2c_get_clientdata(client);
344 
345 	*frequency = dev->if_frequency;
346 	return 0;
347 }
348 
349 static const struct dvb_tuner_ops si2157_ops = {
350 	.info = {
351 		.name           = "Silicon Labs Si2146/2147/2148/2157/2158",
352 		.frequency_min  = 55000000,
353 		.frequency_max  = 862000000,
354 	},
355 
356 	.init = si2157_init,
357 	.sleep = si2157_sleep,
358 	.set_params = si2157_set_params,
359 	.get_if_frequency = si2157_get_if_frequency,
360 };
361 
362 static int si2157_probe(struct i2c_client *client,
363 		const struct i2c_device_id *id)
364 {
365 	struct si2157_config *cfg = client->dev.platform_data;
366 	struct dvb_frontend *fe = cfg->fe;
367 	struct si2157_dev *dev;
368 	struct si2157_cmd cmd;
369 	int ret;
370 
371 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
372 	if (!dev) {
373 		ret = -ENOMEM;
374 		dev_err(&client->dev, "kzalloc() failed\n");
375 		goto err;
376 	}
377 
378 	i2c_set_clientdata(client, dev);
379 	dev->fe = cfg->fe;
380 	dev->inversion = cfg->inversion;
381 	dev->fw_loaded = false;
382 	dev->chiptype = (u8)id->driver_data;
383 	dev->if_frequency = 5000000; /* default value of property 0x0706 */
384 	mutex_init(&dev->i2c_mutex);
385 
386 	/* check if the tuner is there */
387 	cmd.wlen = 0;
388 	cmd.rlen = 1;
389 	ret = si2157_cmd_execute(client, &cmd);
390 	if (ret)
391 		goto err_kfree;
392 
393 	memcpy(&fe->ops.tuner_ops, &si2157_ops, sizeof(struct dvb_tuner_ops));
394 	fe->tuner_priv = client;
395 
396 	dev_info(&client->dev, "Silicon Labs %s successfully attached\n",
397 			dev->chiptype == SI2157_CHIPTYPE_SI2146 ?
398 			"Si2146" : "Si2147/2148/2157/2158");
399 
400 	return 0;
401 
402 err_kfree:
403 	kfree(dev);
404 err:
405 	dev_dbg(&client->dev, "failed=%d\n", ret);
406 	return ret;
407 }
408 
409 static int si2157_remove(struct i2c_client *client)
410 {
411 	struct si2157_dev *dev = i2c_get_clientdata(client);
412 	struct dvb_frontend *fe = dev->fe;
413 
414 	dev_dbg(&client->dev, "\n");
415 
416 	memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
417 	fe->tuner_priv = NULL;
418 	kfree(dev);
419 
420 	return 0;
421 }
422 
423 static const struct i2c_device_id si2157_id_table[] = {
424 	{"si2157", SI2157_CHIPTYPE_SI2157},
425 	{"si2146", SI2157_CHIPTYPE_SI2146},
426 	{}
427 };
428 MODULE_DEVICE_TABLE(i2c, si2157_id_table);
429 
430 static struct i2c_driver si2157_driver = {
431 	.driver = {
432 		.owner	= THIS_MODULE,
433 		.name	= "si2157",
434 	},
435 	.probe		= si2157_probe,
436 	.remove		= si2157_remove,
437 	.id_table	= si2157_id_table,
438 };
439 
440 module_i2c_driver(si2157_driver);
441 
442 MODULE_DESCRIPTION("Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner driver");
443 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
444 MODULE_LICENSE("GPL");
445 MODULE_FIRMWARE(SI2158_A20_FIRMWARE);
446