xref: /openbmc/linux/drivers/media/tuners/si2157.c (revision 21615365)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Silicon Labs Si2146/2147/2148/2157/2158 silicon tuner driver
4  *
5  * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
6  */
7 
8 #include "si2157_priv.h"
9 
10 static const struct dvb_tuner_ops si2157_ops;
11 
12 /* execute firmware command */
13 static int si2157_cmd_execute(struct i2c_client *client, struct si2157_cmd *cmd)
14 {
15 	struct si2157_dev *dev = i2c_get_clientdata(client);
16 	int ret;
17 	unsigned long timeout;
18 
19 	mutex_lock(&dev->i2c_mutex);
20 
21 	if (cmd->wlen) {
22 		/* write cmd and args for firmware */
23 		ret = i2c_master_send(client, cmd->args, cmd->wlen);
24 		if (ret < 0) {
25 			goto err_mutex_unlock;
26 		} else if (ret != cmd->wlen) {
27 			ret = -EREMOTEIO;
28 			goto err_mutex_unlock;
29 		}
30 	}
31 
32 	if (cmd->rlen) {
33 		/* wait cmd execution terminate */
34 		#define TIMEOUT 80
35 		timeout = jiffies + msecs_to_jiffies(TIMEOUT);
36 		while (!time_after(jiffies, timeout)) {
37 			ret = i2c_master_recv(client, cmd->args, cmd->rlen);
38 			if (ret < 0) {
39 				goto err_mutex_unlock;
40 			} else if (ret != cmd->rlen) {
41 				ret = -EREMOTEIO;
42 				goto err_mutex_unlock;
43 			}
44 
45 			/* firmware ready? */
46 			if ((cmd->args[0] >> 7) & 0x01)
47 				break;
48 		}
49 
50 		dev_dbg(&client->dev, "cmd execution took %d ms\n",
51 				jiffies_to_msecs(jiffies) -
52 				(jiffies_to_msecs(timeout) - TIMEOUT));
53 
54 		if (!((cmd->args[0] >> 7) & 0x01)) {
55 			ret = -ETIMEDOUT;
56 			goto err_mutex_unlock;
57 		}
58 	}
59 
60 	mutex_unlock(&dev->i2c_mutex);
61 	return 0;
62 
63 err_mutex_unlock:
64 	mutex_unlock(&dev->i2c_mutex);
65 	dev_dbg(&client->dev, "failed=%d\n", ret);
66 	return ret;
67 }
68 
69 static int si2157_init(struct dvb_frontend *fe)
70 {
71 	struct i2c_client *client = fe->tuner_priv;
72 	struct si2157_dev *dev = i2c_get_clientdata(client);
73 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
74 	int ret, len, remaining;
75 	struct si2157_cmd cmd;
76 	const struct firmware *fw;
77 	const char *fw_name;
78 	unsigned int uitmp, chip_id;
79 
80 	dev_dbg(&client->dev, "\n");
81 
82 	/* Returned IF frequency is garbage when firmware is not running */
83 	memcpy(cmd.args, "\x15\x00\x06\x07", 4);
84 	cmd.wlen = 4;
85 	cmd.rlen = 4;
86 	ret = si2157_cmd_execute(client, &cmd);
87 	if (ret)
88 		goto err;
89 
90 	uitmp = cmd.args[2] << 0 | cmd.args[3] << 8;
91 	dev_dbg(&client->dev, "if_frequency kHz=%u\n", uitmp);
92 
93 	if (uitmp == dev->if_frequency / 1000)
94 		goto warm;
95 
96 	/* power up */
97 	if (dev->chiptype == SI2157_CHIPTYPE_SI2146) {
98 		memcpy(cmd.args, "\xc0\x05\x01\x00\x00\x0b\x00\x00\x01", 9);
99 		cmd.wlen = 9;
100 	} else if (dev->chiptype == SI2157_CHIPTYPE_SI2141) {
101 		memcpy(cmd.args, "\xc0\x00\x0d\x0e\x00\x01\x01\x01\x01\x03", 10);
102 		cmd.wlen = 10;
103 	} else {
104 		memcpy(cmd.args, "\xc0\x00\x0c\x00\x00\x01\x01\x01\x01\x01\x01\x02\x00\x00\x01", 15);
105 		cmd.wlen = 15;
106 	}
107 	cmd.rlen = 1;
108 	ret = si2157_cmd_execute(client, &cmd);
109 	if (ret)
110 		goto err;
111 
112 	/* Si2141 needs a second command before it answers the revision query */
113 	if (dev->chiptype == SI2157_CHIPTYPE_SI2141) {
114 		memcpy(cmd.args, "\xc0\x08\x01\x02\x00\x00\x01", 7);
115 		cmd.wlen = 7;
116 		ret = si2157_cmd_execute(client, &cmd);
117 		if (ret)
118 			goto err;
119 	}
120 
121 	/* query chip revision */
122 	memcpy(cmd.args, "\x02", 1);
123 	cmd.wlen = 1;
124 	cmd.rlen = 13;
125 	ret = si2157_cmd_execute(client, &cmd);
126 	if (ret)
127 		goto err;
128 
129 	chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
130 			cmd.args[4] << 0;
131 
132 	#define SI2177_A30 ('A' << 24 | 77 << 16 | '3' << 8 | '0' << 0)
133 	#define SI2158_A20 ('A' << 24 | 58 << 16 | '2' << 8 | '0' << 0)
134 	#define SI2148_A20 ('A' << 24 | 48 << 16 | '2' << 8 | '0' << 0)
135 	#define SI2157_A30 ('A' << 24 | 57 << 16 | '3' << 8 | '0' << 0)
136 	#define SI2147_A30 ('A' << 24 | 47 << 16 | '3' << 8 | '0' << 0)
137 	#define SI2146_A10 ('A' << 24 | 46 << 16 | '1' << 8 | '0' << 0)
138 	#define SI2141_A10 ('A' << 24 | 41 << 16 | '1' << 8 | '0' << 0)
139 
140 	switch (chip_id) {
141 	case SI2158_A20:
142 	case SI2148_A20:
143 		fw_name = SI2158_A20_FIRMWARE;
144 		break;
145 	case SI2141_A10:
146 		fw_name = SI2141_A10_FIRMWARE;
147 		break;
148 	case SI2177_A30:
149 		fw_name = SI2157_A30_FIRMWARE;
150 		break;
151 	case SI2157_A30:
152 	case SI2147_A30:
153 	case SI2146_A10:
154 		fw_name = NULL;
155 		break;
156 	default:
157 		dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
158 				cmd.args[2], cmd.args[1],
159 				cmd.args[3], cmd.args[4]);
160 		ret = -EINVAL;
161 		goto err;
162 	}
163 
164 	dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n",
165 			cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
166 
167 	if (fw_name == NULL)
168 		goto skip_fw_download;
169 
170 	/* request the firmware, this will block and timeout */
171 	ret = request_firmware(&fw, fw_name, &client->dev);
172 	if (ret) {
173 		dev_err(&client->dev, "firmware file '%s' not found\n",
174 				fw_name);
175 		goto err;
176 	}
177 
178 	/* firmware should be n chunks of 17 bytes */
179 	if (fw->size % 17 != 0) {
180 		dev_err(&client->dev, "firmware file '%s' is invalid\n",
181 				fw_name);
182 		ret = -EINVAL;
183 		goto err_release_firmware;
184 	}
185 
186 	dev_info(&client->dev, "downloading firmware from file '%s'\n",
187 			fw_name);
188 
189 	for (remaining = fw->size; remaining > 0; remaining -= 17) {
190 		len = fw->data[fw->size - remaining];
191 		if (len > SI2157_ARGLEN) {
192 			dev_err(&client->dev, "Bad firmware length\n");
193 			ret = -EINVAL;
194 			goto err_release_firmware;
195 		}
196 		memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
197 		cmd.wlen = len;
198 		cmd.rlen = 1;
199 		ret = si2157_cmd_execute(client, &cmd);
200 		if (ret) {
201 			dev_err(&client->dev, "firmware download failed %d\n",
202 					ret);
203 			goto err_release_firmware;
204 		}
205 	}
206 
207 	release_firmware(fw);
208 
209 skip_fw_download:
210 	/* reboot the tuner with new firmware? */
211 	memcpy(cmd.args, "\x01\x01", 2);
212 	cmd.wlen = 2;
213 	cmd.rlen = 1;
214 	ret = si2157_cmd_execute(client, &cmd);
215 	if (ret)
216 		goto err;
217 
218 	/* query firmware version */
219 	memcpy(cmd.args, "\x11", 1);
220 	cmd.wlen = 1;
221 	cmd.rlen = 10;
222 	ret = si2157_cmd_execute(client, &cmd);
223 	if (ret)
224 		goto err;
225 
226 	dev_info(&client->dev, "firmware version: %c.%c.%d\n",
227 			cmd.args[6], cmd.args[7], cmd.args[8]);
228 warm:
229 	/* init statistics in order signal app which are supported */
230 	c->strength.len = 1;
231 	c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
232 	/* start statistics polling */
233 	schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(1000));
234 
235 	dev->active = true;
236 	return 0;
237 err_release_firmware:
238 	release_firmware(fw);
239 err:
240 	dev_dbg(&client->dev, "failed=%d\n", ret);
241 	return ret;
242 }
243 
244 static int si2157_sleep(struct dvb_frontend *fe)
245 {
246 	struct i2c_client *client = fe->tuner_priv;
247 	struct si2157_dev *dev = i2c_get_clientdata(client);
248 	int ret;
249 	struct si2157_cmd cmd;
250 
251 	dev_dbg(&client->dev, "\n");
252 
253 	dev->active = false;
254 
255 	/* stop statistics polling */
256 	cancel_delayed_work_sync(&dev->stat_work);
257 
258 	/* standby */
259 	memcpy(cmd.args, "\x16\x00", 2);
260 	cmd.wlen = 2;
261 	cmd.rlen = 1;
262 	ret = si2157_cmd_execute(client, &cmd);
263 	if (ret)
264 		goto err;
265 
266 	return 0;
267 err:
268 	dev_dbg(&client->dev, "failed=%d\n", ret);
269 	return ret;
270 }
271 
272 static int si2157_set_params(struct dvb_frontend *fe)
273 {
274 	struct i2c_client *client = fe->tuner_priv;
275 	struct si2157_dev *dev = i2c_get_clientdata(client);
276 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
277 	int ret;
278 	struct si2157_cmd cmd;
279 	u8 bandwidth, delivery_system;
280 	u32 if_frequency = 5000000;
281 
282 	dev_dbg(&client->dev,
283 			"delivery_system=%d frequency=%u bandwidth_hz=%u\n",
284 			c->delivery_system, c->frequency, c->bandwidth_hz);
285 
286 	if (!dev->active) {
287 		ret = -EAGAIN;
288 		goto err;
289 	}
290 
291 	if (c->bandwidth_hz <= 6000000)
292 		bandwidth = 0x06;
293 	else if (c->bandwidth_hz <= 7000000)
294 		bandwidth = 0x07;
295 	else if (c->bandwidth_hz <= 8000000)
296 		bandwidth = 0x08;
297 	else
298 		bandwidth = 0x0f;
299 
300 	switch (c->delivery_system) {
301 	case SYS_ATSC:
302 			delivery_system = 0x00;
303 			if_frequency = 3250000;
304 			break;
305 	case SYS_DVBC_ANNEX_B:
306 			delivery_system = 0x10;
307 			if_frequency = 4000000;
308 			break;
309 	case SYS_DVBT:
310 	case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */
311 			delivery_system = 0x20;
312 			break;
313 	case SYS_DVBC_ANNEX_A:
314 			delivery_system = 0x30;
315 			break;
316 	default:
317 			ret = -EINVAL;
318 			goto err;
319 	}
320 
321 	memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6);
322 	cmd.args[4] = delivery_system | bandwidth;
323 	if (dev->inversion)
324 		cmd.args[5] = 0x01;
325 	cmd.wlen = 6;
326 	cmd.rlen = 4;
327 	ret = si2157_cmd_execute(client, &cmd);
328 	if (ret)
329 		goto err;
330 
331 	if (dev->chiptype == SI2157_CHIPTYPE_SI2146)
332 		memcpy(cmd.args, "\x14\x00\x02\x07\x00\x01", 6);
333 	else
334 		memcpy(cmd.args, "\x14\x00\x02\x07\x00\x00", 6);
335 	cmd.args[4] = dev->if_port;
336 	cmd.wlen = 6;
337 	cmd.rlen = 4;
338 	ret = si2157_cmd_execute(client, &cmd);
339 	if (ret)
340 		goto err;
341 
342 	/* set if frequency if needed */
343 	if (if_frequency != dev->if_frequency) {
344 		memcpy(cmd.args, "\x14\x00\x06\x07", 4);
345 		cmd.args[4] = (if_frequency / 1000) & 0xff;
346 		cmd.args[5] = ((if_frequency / 1000) >> 8) & 0xff;
347 		cmd.wlen = 6;
348 		cmd.rlen = 4;
349 		ret = si2157_cmd_execute(client, &cmd);
350 		if (ret)
351 			goto err;
352 
353 		dev->if_frequency = if_frequency;
354 	}
355 
356 	/* set frequency */
357 	memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8);
358 	cmd.args[4] = (c->frequency >>  0) & 0xff;
359 	cmd.args[5] = (c->frequency >>  8) & 0xff;
360 	cmd.args[6] = (c->frequency >> 16) & 0xff;
361 	cmd.args[7] = (c->frequency >> 24) & 0xff;
362 	cmd.wlen = 8;
363 	cmd.rlen = 1;
364 	ret = si2157_cmd_execute(client, &cmd);
365 	if (ret)
366 		goto err;
367 
368 	return 0;
369 err:
370 	dev_dbg(&client->dev, "failed=%d\n", ret);
371 	return ret;
372 }
373 
374 static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
375 {
376 	struct i2c_client *client = fe->tuner_priv;
377 	struct si2157_dev *dev = i2c_get_clientdata(client);
378 
379 	*frequency = dev->if_frequency;
380 	return 0;
381 }
382 
383 static const struct dvb_tuner_ops si2157_ops = {
384 	.info = {
385 		.name             = "Silicon Labs Si2141/Si2146/2147/2148/2157/2158",
386 		.frequency_min_hz =  42 * MHz,
387 		.frequency_max_hz = 870 * MHz,
388 	},
389 
390 	.init = si2157_init,
391 	.sleep = si2157_sleep,
392 	.set_params = si2157_set_params,
393 	.get_if_frequency = si2157_get_if_frequency,
394 };
395 
396 static void si2157_stat_work(struct work_struct *work)
397 {
398 	struct si2157_dev *dev = container_of(work, struct si2157_dev, stat_work.work);
399 	struct dvb_frontend *fe = dev->fe;
400 	struct i2c_client *client = fe->tuner_priv;
401 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
402 	struct si2157_cmd cmd;
403 	int ret;
404 
405 	dev_dbg(&client->dev, "\n");
406 
407 	memcpy(cmd.args, "\x42\x00", 2);
408 	cmd.wlen = 2;
409 	cmd.rlen = 12;
410 	ret = si2157_cmd_execute(client, &cmd);
411 	if (ret)
412 		goto err;
413 
414 	c->strength.stat[0].scale = FE_SCALE_DECIBEL;
415 	c->strength.stat[0].svalue = (s8) cmd.args[3] * 1000;
416 
417 	schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
418 	return;
419 err:
420 	c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
421 	dev_dbg(&client->dev, "failed=%d\n", ret);
422 }
423 
424 static int si2157_probe(struct i2c_client *client,
425 			const struct i2c_device_id *id)
426 {
427 	struct si2157_config *cfg = client->dev.platform_data;
428 	struct dvb_frontend *fe = cfg->fe;
429 	struct si2157_dev *dev;
430 	struct si2157_cmd cmd;
431 	int ret;
432 
433 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
434 	if (!dev) {
435 		ret = -ENOMEM;
436 		dev_err(&client->dev, "kzalloc() failed\n");
437 		goto err;
438 	}
439 
440 	i2c_set_clientdata(client, dev);
441 	dev->fe = cfg->fe;
442 	dev->inversion = cfg->inversion;
443 	dev->if_port = cfg->if_port;
444 	dev->chiptype = (u8)id->driver_data;
445 	dev->if_frequency = 5000000; /* default value of property 0x0706 */
446 	mutex_init(&dev->i2c_mutex);
447 	INIT_DELAYED_WORK(&dev->stat_work, si2157_stat_work);
448 
449 	/* check if the tuner is there */
450 	cmd.wlen = 0;
451 	cmd.rlen = 1;
452 	ret = si2157_cmd_execute(client, &cmd);
453 	if (ret)
454 		goto err_kfree;
455 
456 	memcpy(&fe->ops.tuner_ops, &si2157_ops, sizeof(struct dvb_tuner_ops));
457 	fe->tuner_priv = client;
458 
459 #ifdef CONFIG_MEDIA_CONTROLLER
460 	if (cfg->mdev) {
461 		dev->mdev = cfg->mdev;
462 
463 		dev->ent.name = KBUILD_MODNAME;
464 		dev->ent.function = MEDIA_ENT_F_TUNER;
465 
466 		dev->pad[SI2157_PAD_RF_INPUT].flags = MEDIA_PAD_FL_SINK;
467 		dev->pad[SI2157_PAD_RF_INPUT].sig_type = PAD_SIGNAL_ANALOG;
468 		dev->pad[SI2157_PAD_VID_OUT].flags = MEDIA_PAD_FL_SOURCE;
469 		dev->pad[SI2157_PAD_VID_OUT].sig_type = PAD_SIGNAL_ANALOG;
470 		dev->pad[SI2157_PAD_AUD_OUT].flags = MEDIA_PAD_FL_SOURCE;
471 		dev->pad[SI2157_PAD_AUD_OUT].sig_type = PAD_SIGNAL_AUDIO;
472 
473 		ret = media_entity_pads_init(&dev->ent, SI2157_NUM_PADS,
474 					     &dev->pad[0]);
475 
476 		if (ret)
477 			goto err_kfree;
478 
479 		ret = media_device_register_entity(cfg->mdev, &dev->ent);
480 		if (ret) {
481 			media_entity_cleanup(&dev->ent);
482 			goto err_kfree;
483 		}
484 	}
485 #endif
486 
487 	dev_info(&client->dev, "Silicon Labs %s successfully attached\n",
488 			dev->chiptype == SI2157_CHIPTYPE_SI2141 ?  "Si2141" :
489 			dev->chiptype == SI2157_CHIPTYPE_SI2146 ?
490 			"Si2146" : "Si2147/2148/2157/2158");
491 
492 	return 0;
493 
494 err_kfree:
495 	kfree(dev);
496 err:
497 	dev_dbg(&client->dev, "failed=%d\n", ret);
498 	return ret;
499 }
500 
501 static int si2157_remove(struct i2c_client *client)
502 {
503 	struct si2157_dev *dev = i2c_get_clientdata(client);
504 	struct dvb_frontend *fe = dev->fe;
505 
506 	dev_dbg(&client->dev, "\n");
507 
508 	/* stop statistics polling */
509 	cancel_delayed_work_sync(&dev->stat_work);
510 
511 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
512 	if (dev->mdev)
513 		media_device_unregister_entity(&dev->ent);
514 #endif
515 
516 	memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
517 	fe->tuner_priv = NULL;
518 	kfree(dev);
519 
520 	return 0;
521 }
522 
523 static const struct i2c_device_id si2157_id_table[] = {
524 	{"si2157", SI2157_CHIPTYPE_SI2157},
525 	{"si2146", SI2157_CHIPTYPE_SI2146},
526 	{"si2141", SI2157_CHIPTYPE_SI2141},
527 	{"si2177", SI2157_CHIPTYPE_SI2177},
528 	{}
529 };
530 MODULE_DEVICE_TABLE(i2c, si2157_id_table);
531 
532 static struct i2c_driver si2157_driver = {
533 	.driver = {
534 		.name		     = "si2157",
535 		.suppress_bind_attrs = true,
536 	},
537 	.probe		= si2157_probe,
538 	.remove		= si2157_remove,
539 	.id_table	= si2157_id_table,
540 };
541 
542 module_i2c_driver(si2157_driver);
543 
544 MODULE_DESCRIPTION("Silicon Labs Si2141/Si2146/2147/2148/2157/2158 silicon tuner driver");
545 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
546 MODULE_LICENSE("GPL");
547 MODULE_FIRMWARE(SI2158_A20_FIRMWARE);
548 MODULE_FIRMWARE(SI2141_A10_FIRMWARE);
549 MODULE_FIRMWARE(SI2157_A30_FIRMWARE);
550