xref: /openbmc/linux/drivers/media/tuners/si2157.c (revision 6ab70300)
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 static int tuner_lock_debug;
13 module_param(tuner_lock_debug, int, 0644);
14 MODULE_PARM_DESC(tuner_lock_debug, "if set, signal lock is briefly waited on after setting params");
15 
16 /* execute firmware command */
17 static int si2157_cmd_execute(struct i2c_client *client, struct si2157_cmd *cmd)
18 {
19 	struct si2157_dev *dev = i2c_get_clientdata(client);
20 	int ret;
21 	unsigned long timeout;
22 
23 	mutex_lock(&dev->i2c_mutex);
24 
25 	if (cmd->wlen) {
26 		/* write cmd and args for firmware */
27 		ret = i2c_master_send(client, cmd->args, cmd->wlen);
28 		if (ret < 0) {
29 			goto err_mutex_unlock;
30 		} else if (ret != cmd->wlen) {
31 			ret = -EREMOTEIO;
32 			goto err_mutex_unlock;
33 		}
34 	}
35 
36 	if (cmd->rlen) {
37 		/* wait cmd execution terminate */
38 		#define TIMEOUT 80
39 		timeout = jiffies + msecs_to_jiffies(TIMEOUT);
40 		while (!time_after(jiffies, timeout)) {
41 			ret = i2c_master_recv(client, cmd->args, cmd->rlen);
42 			if (ret < 0) {
43 				goto err_mutex_unlock;
44 			} else if (ret != cmd->rlen) {
45 				ret = -EREMOTEIO;
46 				goto err_mutex_unlock;
47 			}
48 
49 			/* firmware ready? */
50 			if ((cmd->args[0] >> 7) & 0x01)
51 				break;
52 		}
53 
54 		dev_dbg(&client->dev, "cmd execution took %d ms, status=%x\n",
55 			jiffies_to_msecs(jiffies) -
56 			(jiffies_to_msecs(timeout) - TIMEOUT),
57 			cmd->args[0]);
58 
59 		if (!((cmd->args[0] >> 7) & 0x01)) {
60 			ret = -ETIMEDOUT;
61 			goto err_mutex_unlock;
62 		}
63 		/* check error status bit */
64 		if (cmd->args[0] & 0x40) {
65 			ret = -EAGAIN;
66 			goto err_mutex_unlock;
67 		}
68 	}
69 
70 	mutex_unlock(&dev->i2c_mutex);
71 	return 0;
72 
73 err_mutex_unlock:
74 	mutex_unlock(&dev->i2c_mutex);
75 	dev_dbg(&client->dev, "failed=%d\n", ret);
76 	return ret;
77 }
78 
79 static const struct si2157_tuner_info si2157_tuners[] = {
80 	{ SI2141, false, 0x60, SI2141_60_FIRMWARE, SI2141_A10_FIRMWARE },
81 	{ SI2141, false, 0x61, SI2141_61_FIRMWARE, SI2141_A10_FIRMWARE },
82 	{ SI2146, false, 0x11, SI2146_11_FIRMWARE, NULL },
83 	{ SI2147, false, 0x50, SI2147_50_FIRMWARE, NULL },
84 	{ SI2148, true,  0x32, SI2148_32_FIRMWARE, SI2158_A20_FIRMWARE },
85 	{ SI2148, true,  0x33, SI2148_33_FIRMWARE, SI2158_A20_FIRMWARE },
86 	{ SI2157, false, 0x50, SI2157_50_FIRMWARE, SI2157_A30_FIRMWARE },
87 	{ SI2158, false, 0x50, SI2158_50_FIRMWARE, SI2158_A20_FIRMWARE },
88 	{ SI2158, false, 0x51, SI2158_51_FIRMWARE, SI2158_A20_FIRMWARE },
89 	{ SI2177, false, 0x50, SI2177_50_FIRMWARE, SI2157_A30_FIRMWARE },
90 };
91 
92 static int si2157_load_firmware(struct dvb_frontend *fe,
93 				const char *fw_name)
94 {
95 	struct i2c_client *client = fe->tuner_priv;
96 	const struct firmware *fw;
97 	int ret, len, remaining;
98 	struct si2157_cmd cmd;
99 
100 	/* request the firmware, this will block and timeout */
101 	ret = firmware_request_nowarn(&fw, fw_name, &client->dev);
102 	if (ret)
103 		return ret;
104 
105 	/* firmware should be n chunks of 17 bytes */
106 	if (fw->size % 17 != 0) {
107 		dev_err(&client->dev, "firmware file '%s' is invalid\n",
108 			fw_name);
109 		ret = -EINVAL;
110 		goto err_release_firmware;
111 	}
112 
113 	dev_info(&client->dev, "downloading firmware from file '%s'\n",
114 		 fw_name);
115 
116 	for (remaining = fw->size; remaining > 0; remaining -= 17) {
117 		len = fw->data[fw->size - remaining];
118 		if (len > SI2157_ARGLEN) {
119 			dev_err(&client->dev, "Bad firmware length\n");
120 			ret = -EINVAL;
121 			goto err_release_firmware;
122 		}
123 		memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
124 		cmd.wlen = len;
125 		cmd.rlen = 1;
126 		ret = si2157_cmd_execute(client, &cmd);
127 		if (ret) {
128 			dev_err(&client->dev, "firmware download failed %d\n",
129 					ret);
130 			goto err_release_firmware;
131 		}
132 	}
133 
134 err_release_firmware:
135 	release_firmware(fw);
136 
137 	return ret;
138 }
139 
140 static int si2157_find_and_load_firmware(struct dvb_frontend *fe)
141 {
142 	struct i2c_client *client = fe->tuner_priv;
143 	struct si2157_dev *dev = i2c_get_clientdata(client);
144 	const char *fw_alt_name = NULL;
145 	unsigned char part_id, rom_id;
146 	const char *fw_name = NULL;
147 	struct si2157_cmd cmd;
148 	bool required = true;
149 	int ret, i;
150 
151 	if (dev->dont_load_firmware) {
152 		dev_info(&client->dev,
153 			 "device is buggy, skipping firmware download\n");
154 		return 0;
155 	}
156 
157 	/* query chip revision */
158 	memcpy(cmd.args, "\x02", 1);
159 	cmd.wlen = 1;
160 	cmd.rlen = 13;
161 	ret = si2157_cmd_execute(client, &cmd);
162 	if (ret)
163 		return ret;
164 
165 	part_id = cmd.args[2];
166 	rom_id = cmd.args[12];
167 
168 	for (i = 0; i < ARRAY_SIZE(si2157_tuners); i++) {
169 		if (si2157_tuners[i].part_id != part_id)
170 			continue;
171 		required = si2157_tuners[i].required;
172 		fw_alt_name = si2157_tuners[i].fw_alt_name;
173 
174 		/* Both part and rom ID match */
175 		if (si2157_tuners[i].rom_id == rom_id) {
176 			fw_name = si2157_tuners[i].fw_name;
177 			break;
178 		}
179 	}
180 
181 	if (!fw_name && !fw_alt_name) {
182 		dev_err(&client->dev,
183 			"unknown chip version Si21%d-%c%c%c ROM 0x%02x\n",
184 			part_id, cmd.args[1], cmd.args[3], cmd.args[4], rom_id);
185 		return -EINVAL;
186 	}
187 
188 	/* Update the part id based on device's report */
189 	dev->part_id = part_id;
190 
191 	dev_info(&client->dev,
192 		 "found a 'Silicon Labs Si21%d-%c%c%c ROM 0x%02x'\n",
193 		 part_id, cmd.args[1], cmd.args[3], cmd.args[4], rom_id);
194 
195 	if (fw_name)
196 		ret = si2157_load_firmware(fe, fw_name);
197 	else
198 		ret = -ENOENT;
199 
200 	/* Try alternate name, if any */
201 	if (ret == -ENOENT && fw_alt_name)
202 		ret = si2157_load_firmware(fe, fw_alt_name);
203 
204 	if (ret == -ENOENT) {
205 		if (!required) {
206 			dev_info(&client->dev, "Using ROM firmware.\n");
207 			return 0;
208 		}
209 		dev_err(&client->dev, "Can't continue without a firmware.\n");
210 	} else if (ret < 0) {
211 		dev_err(&client->dev, "error %d when loading firmware\n", ret);
212 	}
213 	return ret;
214 }
215 
216 static int si2157_init(struct dvb_frontend *fe)
217 {
218 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
219 	struct i2c_client *client = fe->tuner_priv;
220 	struct si2157_dev *dev = i2c_get_clientdata(client);
221 	unsigned int xtal_trim;
222 	struct si2157_cmd cmd;
223 	int ret;
224 
225 	dev_dbg(&client->dev, "\n");
226 
227 	/* Try to get Xtal trim property, to verify tuner still running */
228 	memcpy(cmd.args, "\x15\x00\x02\x04", 4);
229 	cmd.wlen = 4;
230 	cmd.rlen = 4;
231 	ret = si2157_cmd_execute(client, &cmd);
232 
233 	xtal_trim = cmd.args[2] | (cmd.args[3] << 8);
234 
235 	if (ret == 0 && xtal_trim < 16)
236 		goto warm;
237 
238 	dev->if_frequency = 0; /* we no longer know current tuner state */
239 
240 	/* power up */
241 	if (dev->part_id == SI2146) {
242 		/* clock_mode = XTAL, clock_freq = 24MHz */
243 		memcpy(cmd.args, "\xc0\x05\x01\x00\x00\x0b\x00\x00\x01", 9);
244 		cmd.wlen = 9;
245 	} else if (dev->part_id == SI2141) {
246 		/* clock_mode: XTAL, xout enabled */
247 		memcpy(cmd.args, "\xc0\x00\x0d\x0e\x00\x01\x01\x01\x01\x03", 10);
248 		cmd.wlen = 10;
249 	} else {
250 		memcpy(cmd.args, "\xc0\x00\x0c\x00\x00\x01\x01\x01\x01\x01\x01\x02\x00\x00\x01", 15);
251 		cmd.wlen = 15;
252 	}
253 	cmd.rlen = 1;
254 	ret = si2157_cmd_execute(client, &cmd);
255 	if (ret && (dev->part_id != SI2141 || ret != -EAGAIN))
256 		goto err;
257 
258 	/* Si2141 needs a wake up command */
259 	if (dev->part_id == SI2141) {
260 		memcpy(cmd.args, "\xc0\x08\x01\x02\x00\x00\x01", 7);
261 		cmd.wlen = 7;
262 		ret = si2157_cmd_execute(client, &cmd);
263 		if (ret)
264 			goto err;
265 	}
266 
267 	/* Try to load the firmware */
268 	ret = si2157_find_and_load_firmware(fe);
269 	if (ret < 0)
270 		goto err;
271 
272 	/* reboot the tuner with new firmware? */
273 	memcpy(cmd.args, "\x01\x01", 2);
274 	cmd.wlen = 2;
275 	cmd.rlen = 1;
276 	ret = si2157_cmd_execute(client, &cmd);
277 	if (ret)
278 		goto err;
279 
280 	/* query firmware version */
281 	memcpy(cmd.args, "\x11", 1);
282 	cmd.wlen = 1;
283 	cmd.rlen = 10;
284 	ret = si2157_cmd_execute(client, &cmd);
285 	if (ret)
286 		goto err;
287 
288 	dev_info(&client->dev, "firmware version: %c.%c.%d\n",
289 			cmd.args[6], cmd.args[7], cmd.args[8]);
290 
291 	/* enable tuner status flags */
292 	memcpy(cmd.args, "\x14\x00\x01\x05\x01\x00", 6);
293 	cmd.wlen = 6;
294 	cmd.rlen = 1;
295 	ret = si2157_cmd_execute(client, &cmd);
296 	if (ret)
297 		goto err;
298 
299 	memcpy(cmd.args, "\x14\x00\x01\x06\x01\x00", 6);
300 	cmd.wlen = 6;
301 	cmd.rlen = 1;
302 	ret = si2157_cmd_execute(client, &cmd);
303 	if (ret)
304 		goto err;
305 
306 	memcpy(cmd.args, "\x14\x00\x01\x07\x01\x00", 6);
307 	cmd.wlen = 6;
308 	cmd.rlen = 1;
309 	ret = si2157_cmd_execute(client, &cmd);
310 	if (ret)
311 		goto err;
312 warm:
313 	/* init statistics in order signal app which are supported */
314 	c->strength.len = 1;
315 	c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
316 	/* start statistics polling */
317 	schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(1000));
318 
319 	dev->active = true;
320 	return 0;
321 
322 err:
323 	dev_dbg(&client->dev, "failed=%d\n", ret);
324 	return ret;
325 }
326 
327 static int si2157_sleep(struct dvb_frontend *fe)
328 {
329 	struct i2c_client *client = fe->tuner_priv;
330 	struct si2157_dev *dev = i2c_get_clientdata(client);
331 	int ret;
332 	struct si2157_cmd cmd;
333 
334 	dev_dbg(&client->dev, "\n");
335 
336 	dev->active = false;
337 
338 	/* stop statistics polling */
339 	cancel_delayed_work_sync(&dev->stat_work);
340 
341 	/* standby */
342 	memcpy(cmd.args, "\x16\x00", 2);
343 	cmd.wlen = 2;
344 	cmd.rlen = 1;
345 	ret = si2157_cmd_execute(client, &cmd);
346 	if (ret)
347 		goto err;
348 
349 	return 0;
350 err:
351 	dev_dbg(&client->dev, "failed=%d\n", ret);
352 	return ret;
353 }
354 
355 static int si2157_tune_wait(struct i2c_client *client, u8 is_digital)
356 {
357 #define TUN_TIMEOUT 40
358 #define DIG_TIMEOUT 30
359 #define ANALOG_TIMEOUT 150
360 	struct si2157_dev *dev = i2c_get_clientdata(client);
361 	int ret;
362 	unsigned long timeout;
363 	unsigned long start_time;
364 	u8 wait_status;
365 	u8  tune_lock_mask;
366 
367 	if (is_digital)
368 		tune_lock_mask = 0x04;
369 	else
370 		tune_lock_mask = 0x02;
371 
372 	mutex_lock(&dev->i2c_mutex);
373 
374 	/* wait tuner command complete */
375 	start_time = jiffies;
376 	timeout = start_time + msecs_to_jiffies(TUN_TIMEOUT);
377 	while (1) {
378 		ret = i2c_master_recv(client, &wait_status,
379 				      sizeof(wait_status));
380 		if (ret < 0) {
381 			goto err_mutex_unlock;
382 		} else if (ret != sizeof(wait_status)) {
383 			ret = -EREMOTEIO;
384 			goto err_mutex_unlock;
385 		}
386 
387 		if (time_after(jiffies, timeout))
388 			break;
389 
390 		/* tuner done? */
391 		if ((wait_status & 0x81) == 0x81)
392 			break;
393 		usleep_range(5000, 10000);
394 	}
395 
396 	dev_dbg(&client->dev, "tuning took %d ms, status=0x%x\n",
397 		jiffies_to_msecs(jiffies) - jiffies_to_msecs(start_time),
398 		wait_status);
399 
400 	/* if we tuned ok, wait a bit for tuner lock */
401 	if (tuner_lock_debug && (wait_status & 0x81) == 0x81) {
402 		if (is_digital)
403 			timeout = jiffies + msecs_to_jiffies(DIG_TIMEOUT);
404 		else
405 			timeout = jiffies + msecs_to_jiffies(ANALOG_TIMEOUT);
406 
407 		while (!time_after(jiffies, timeout)) {
408 			ret = i2c_master_recv(client, &wait_status,
409 					      sizeof(wait_status));
410 			if (ret < 0) {
411 				goto err_mutex_unlock;
412 			} else if (ret != sizeof(wait_status)) {
413 				ret = -EREMOTEIO;
414 				goto err_mutex_unlock;
415 			}
416 
417 			/* tuner locked? */
418 			if (wait_status & tune_lock_mask)
419 				break;
420 			usleep_range(5000, 10000);
421 		}
422 
423 		dev_dbg(&client->dev, "tuning+lock took %d ms, status=0x%x\n",
424 			jiffies_to_msecs(jiffies) - jiffies_to_msecs(start_time),
425 			wait_status);
426 	}
427 
428 	if ((wait_status & 0xc0) != 0x80) {
429 		ret = -ETIMEDOUT;
430 		goto err_mutex_unlock;
431 	}
432 
433 	mutex_unlock(&dev->i2c_mutex);
434 	return 0;
435 
436 err_mutex_unlock:
437 	mutex_unlock(&dev->i2c_mutex);
438 	dev_err(&client->dev, "failed=%d\n", ret);
439 	return ret;
440 }
441 
442 static int si2157_set_params(struct dvb_frontend *fe)
443 {
444 	struct i2c_client *client = fe->tuner_priv;
445 	struct si2157_dev *dev = i2c_get_clientdata(client);
446 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
447 	int ret;
448 	struct si2157_cmd cmd;
449 	u8 bandwidth, delivery_system;
450 	u32 if_frequency = 5000000;
451 
452 	dev_dbg(&client->dev,
453 			"delivery_system=%d frequency=%u bandwidth_hz=%u\n",
454 			c->delivery_system, c->frequency, c->bandwidth_hz);
455 
456 	if (!dev->active) {
457 		ret = -EAGAIN;
458 		goto err;
459 	}
460 
461 	if (SUPPORTS_1700KHz(dev) && c->bandwidth_hz <= 1700000)
462 		bandwidth = 0x09;
463 	if (c->bandwidth_hz <= 6000000)
464 		bandwidth = 0x06;
465 	if (SUPPORTS_1700KHz(dev) && c->bandwidth_hz <= 6100000)
466 		bandwidth = 0x10;
467 	else if (c->bandwidth_hz <= 7000000)
468 		bandwidth = 0x07;
469 	else if (c->bandwidth_hz <= 8000000)
470 		bandwidth = 0x08;
471 	else
472 		bandwidth = 0x0f;
473 
474 	switch (c->delivery_system) {
475 	case SYS_ATSC:
476 			delivery_system = 0x00;
477 			if_frequency = 3250000;
478 			break;
479 	case SYS_DVBC_ANNEX_B:
480 			delivery_system = 0x10;
481 			if_frequency = 4000000;
482 			break;
483 	case SYS_DVBT:
484 	case SYS_DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */
485 			delivery_system = 0x20;
486 			break;
487 	case SYS_DVBC_ANNEX_A:
488 			delivery_system = 0x30;
489 			break;
490 	case SYS_ISDBT:
491 			delivery_system = 0x40;
492 			break;
493 	case SYS_DTMB:
494 			delivery_system = 0x60;
495 			break;
496 	default:
497 			ret = -EINVAL;
498 			goto err;
499 	}
500 
501 	memcpy(cmd.args, "\x14\x00\x03\x07\x00\x00", 6);
502 	cmd.args[4] = delivery_system | bandwidth;
503 	if (dev->inversion)
504 		cmd.args[5] = 0x01;
505 	cmd.wlen = 6;
506 	cmd.rlen = 4;
507 	ret = si2157_cmd_execute(client, &cmd);
508 	if (ret)
509 		goto err;
510 
511 	/* On SI2146, set DTV AGC source to DLIF_AGC_3DB */
512 	if (dev->part_id == SI2146)
513 		memcpy(cmd.args, "\x14\x00\x02\x07\x00\x01", 6);
514 	else
515 		memcpy(cmd.args, "\x14\x00\x02\x07\x00\x00", 6);
516 	cmd.args[4] = dev->if_port;
517 	cmd.wlen = 6;
518 	cmd.rlen = 4;
519 	ret = si2157_cmd_execute(client, &cmd);
520 	if (ret)
521 		goto err;
522 
523 	/* set digital if frequency if needed */
524 	if (if_frequency != dev->if_frequency) {
525 		memcpy(cmd.args, "\x14\x00\x06\x07", 4);
526 		cmd.args[4] = (if_frequency / 1000) & 0xff;
527 		cmd.args[5] = ((if_frequency / 1000) >> 8) & 0xff;
528 		cmd.wlen = 6;
529 		cmd.rlen = 4;
530 		ret = si2157_cmd_execute(client, &cmd);
531 		if (ret)
532 			goto err;
533 
534 		dev->if_frequency = if_frequency;
535 	}
536 
537 	/* set digital frequency */
538 	memcpy(cmd.args, "\x41\x00\x00\x00\x00\x00\x00\x00", 8);
539 	cmd.args[4] = (c->frequency >>  0) & 0xff;
540 	cmd.args[5] = (c->frequency >>  8) & 0xff;
541 	cmd.args[6] = (c->frequency >> 16) & 0xff;
542 	cmd.args[7] = (c->frequency >> 24) & 0xff;
543 	cmd.wlen = 8;
544 	cmd.rlen = 1;
545 	ret = si2157_cmd_execute(client, &cmd);
546 	if (ret)
547 		goto err;
548 
549 	dev->bandwidth = bandwidth;
550 	dev->frequency = c->frequency;
551 
552 	si2157_tune_wait(client, 1); /* wait to complete, ignore any errors */
553 
554 	return 0;
555 err:
556 	dev->bandwidth = 0;
557 	dev->frequency = 0;
558 	dev->if_frequency = 0;
559 	dev_dbg(&client->dev, "failed=%d\n", ret);
560 	return ret;
561 }
562 
563 static int si2157_set_analog_params(struct dvb_frontend *fe,
564 				    struct analog_parameters *params)
565 {
566 	struct i2c_client *client = fe->tuner_priv;
567 	struct si2157_dev *dev = i2c_get_clientdata(client);
568 	char *std; /* for debugging */
569 	int ret;
570 	struct si2157_cmd cmd;
571 	u32 bandwidth = 0;
572 	u32 if_frequency = 0;
573 	u32 freq = 0;
574 	u64 tmp_lval = 0;
575 	u8 system = 0;
576 	u8 color = 0;    /* 0=NTSC/PAL, 0x10=SECAM */
577 	u8 invert_analog = 1; /* analog tuner spectrum; 0=normal, 1=inverted */
578 
579 	if (!SUPPORTS_ATV_IF(dev)) {
580 		dev_info(&client->dev, "Analog tuning not supported yet for Si21%d\n",
581 			 dev->part_id);
582 		ret = -EINVAL;
583 		goto err;
584 	}
585 
586 	if (!dev->active)
587 		si2157_init(fe);
588 
589 	if (!dev->active) {
590 		ret = -EAGAIN;
591 		goto err;
592 	}
593 	if (params->mode == V4L2_TUNER_RADIO) {
594 	/*
595 	 * std = "fm";
596 	 * bandwidth = 1700000; //best can do for FM, AGC will be a mess though
597 	 * if_frequency = 1250000;  //HVR-225x(saa7164), HVR-12xx(cx23885)
598 	 * if_frequency = 6600000;  //HVR-9xx(cx231xx)
599 	 * if_frequency = 5500000;  //HVR-19xx(pvrusb2)
600 	 */
601 		dev_err(&client->dev, "si2157 does not currently support FM radio\n");
602 		ret = -EINVAL;
603 		goto err;
604 	}
605 	tmp_lval = params->frequency * 625LL;
606 	do_div(tmp_lval, 10); /* convert to HZ */
607 	freq = (u32)tmp_lval;
608 
609 	if (freq < 1000000) /* is freq in KHz */
610 		freq = freq * 1000;
611 	dev->frequency = freq;
612 
613 	/* if_frequency values based on tda187271C2 */
614 	if (params->std & (V4L2_STD_B | V4L2_STD_GH)) {
615 		if (freq >= 470000000) {
616 			std = "palGH";
617 			bandwidth = 8000000;
618 			if_frequency = 6000000;
619 			system = 1;
620 			if (params->std &
621 			    (V4L2_STD_SECAM_G | V4L2_STD_SECAM_H)) {
622 				std = "secamGH";
623 				color = 0x10;
624 			}
625 		} else {
626 			std = "palB";
627 			bandwidth = 7000000;
628 			if_frequency = 6000000;
629 			system = 0;
630 			if (params->std & V4L2_STD_SECAM_B) {
631 				std = "secamB";
632 				color = 0x10;
633 			}
634 		}
635 	} else if (params->std & V4L2_STD_MN) {
636 		std = "MN";
637 		bandwidth = 6000000;
638 		if_frequency = 5400000;
639 		system = 2;
640 	} else if (params->std & V4L2_STD_PAL_I) {
641 		std = "palI";
642 		bandwidth = 8000000;
643 		if_frequency = 7250000; /* TODO: does not work yet */
644 		system = 4;
645 	} else if (params->std & V4L2_STD_DK) {
646 		std = "palDK";
647 		bandwidth = 8000000;
648 		if_frequency = 6900000; /* TODO: does not work yet */
649 		system = 5;
650 		if (params->std & V4L2_STD_SECAM_DK) {
651 			std = "secamDK";
652 			color = 0x10;
653 		}
654 	} else if (params->std & V4L2_STD_SECAM_L) {
655 		std = "secamL";
656 		bandwidth = 8000000;
657 		if_frequency = 6750000; /* TODO: untested */
658 		system = 6;
659 		color = 0x10;
660 	} else if (params->std & V4L2_STD_SECAM_LC) {
661 		std = "secamL'";
662 		bandwidth = 7000000;
663 		if_frequency = 1250000; /* TODO: untested */
664 		system = 7;
665 		color = 0x10;
666 	} else {
667 		std = "unknown";
668 	}
669 	/* calc channel center freq */
670 	freq = freq - 1250000 + (bandwidth / 2);
671 
672 	dev_dbg(&client->dev,
673 		"mode=%d system=%u std='%s' params->frequency=%u center freq=%u if=%u bandwidth=%u\n",
674 		params->mode, system, std, params->frequency,
675 		freq, if_frequency, bandwidth);
676 
677 	/* set analog IF port */
678 	memcpy(cmd.args, "\x14\x00\x03\x06\x08\x02", 6);
679 	/* in using dev->if_port, we assume analog and digital IF's */
680 	/*   are always on different ports */
681 	/* assumes if_port definition is 0 or 1 for digital out */
682 	cmd.args[4] = (dev->if_port == 1) ? 8 : 10;
683 	/* Analog AGC assumed external */
684 	cmd.args[5] = (dev->if_port == 1) ? 2 : 1;
685 	cmd.wlen = 6;
686 	cmd.rlen = 4;
687 	ret = si2157_cmd_execute(client, &cmd);
688 	if (ret)
689 		goto err;
690 
691 	/* set analog IF output config */
692 	memcpy(cmd.args, "\x14\x00\x0d\x06\x94\x64", 6);
693 	cmd.wlen = 6;
694 	cmd.rlen = 4;
695 	ret = si2157_cmd_execute(client, &cmd);
696 	if (ret)
697 		goto err;
698 
699 	/* make this distinct from a digital IF */
700 	dev->if_frequency = if_frequency | 1;
701 
702 	/* calc and set tuner analog if center frequency */
703 	if_frequency = if_frequency + 1250000 - (bandwidth / 2);
704 	dev_dbg(&client->dev, "IF Ctr freq=%d\n", if_frequency);
705 
706 	memcpy(cmd.args, "\x14\x00\x0C\x06", 4);
707 	cmd.args[4] = (if_frequency / 1000) & 0xff;
708 	cmd.args[5] = ((if_frequency / 1000) >> 8) & 0xff;
709 	cmd.wlen = 6;
710 	cmd.rlen = 4;
711 	ret = si2157_cmd_execute(client, &cmd);
712 	if (ret)
713 		goto err;
714 
715 	/* set analog AGC config */
716 	memcpy(cmd.args, "\x14\x00\x07\x06\x32\xc8", 6);
717 	cmd.wlen = 6;
718 	cmd.rlen = 4;
719 	ret = si2157_cmd_execute(client, &cmd);
720 	if (ret)
721 		goto err;
722 
723 	/* set analog video mode */
724 	memcpy(cmd.args, "\x14\x00\x04\x06\x00\x00", 6);
725 	cmd.args[4] = system | color;
726 	/* can use dev->inversion if assumed applies to both digital/analog */
727 	if (invert_analog)
728 		cmd.args[5] |= 0x02;
729 	cmd.wlen = 6;
730 	cmd.rlen = 1;
731 	ret = si2157_cmd_execute(client, &cmd);
732 	if (ret)
733 		goto err;
734 
735 	/* set analog frequency */
736 	memcpy(cmd.args, "\x41\x01\x00\x00\x00\x00\x00\x00", 8);
737 	cmd.args[4] = (freq >>  0) & 0xff;
738 	cmd.args[5] = (freq >>  8) & 0xff;
739 	cmd.args[6] = (freq >> 16) & 0xff;
740 	cmd.args[7] = (freq >> 24) & 0xff;
741 	cmd.wlen = 8;
742 	cmd.rlen = 1;
743 	ret = si2157_cmd_execute(client, &cmd);
744 	if (ret)
745 		goto err;
746 
747 	dev->bandwidth = bandwidth;
748 
749 	si2157_tune_wait(client, 0); /* wait to complete, ignore any errors */
750 
751 	return 0;
752 err:
753 	dev->bandwidth = 0;
754 	dev->frequency = 0;
755 	dev->if_frequency = 0;
756 	dev_dbg(&client->dev, "failed=%d\n", ret);
757 	return ret;
758 }
759 
760 static int si2157_get_frequency(struct dvb_frontend *fe, u32 *frequency)
761 {
762 	struct i2c_client *client = fe->tuner_priv;
763 	struct si2157_dev *dev = i2c_get_clientdata(client);
764 
765 	*frequency = dev->frequency;
766 	dev_dbg(&client->dev, "freq=%u\n", dev->frequency);
767 	return 0;
768 }
769 
770 static int si2157_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
771 {
772 	struct i2c_client *client = fe->tuner_priv;
773 	struct si2157_dev *dev = i2c_get_clientdata(client);
774 
775 	*bandwidth = dev->bandwidth;
776 	dev_dbg(&client->dev, "bandwidth=%u\n", dev->bandwidth);
777 	return 0;
778 }
779 
780 static int si2157_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
781 {
782 	struct i2c_client *client = fe->tuner_priv;
783 	struct si2157_dev *dev = i2c_get_clientdata(client);
784 
785 	*frequency = dev->if_frequency & ~1; /* strip analog IF indicator bit */
786 	dev_dbg(&client->dev, "if_frequency=%u\n", *frequency);
787 	return 0;
788 }
789 
790 static int si2157_get_rf_strength(struct dvb_frontend *fe, u16 *rssi)
791 {
792 	struct i2c_client *client = fe->tuner_priv;
793 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
794 	struct si2157_cmd cmd;
795 	int ret;
796 	int strength;
797 
798 	dev_dbg(&client->dev, "\n");
799 
800 	memcpy(cmd.args, "\x42\x00", 2);
801 	cmd.wlen = 2;
802 	cmd.rlen = 12;
803 	ret = si2157_cmd_execute(client, &cmd);
804 	if (ret)
805 		goto err;
806 
807 	c->strength.stat[0].scale = FE_SCALE_DECIBEL;
808 	c->strength.stat[0].svalue = (s8)cmd.args[3] * 1000;
809 
810 	/* normalize values based on Silicon Labs reference
811 	 * add 100, then anything > 80 is 100% signal
812 	 */
813 	strength = (s8)cmd.args[3] + 100;
814 	strength = clamp_val(strength, 0, 80);
815 	*rssi = (u16)(strength * 0xffff / 80);
816 
817 	dev_dbg(&client->dev, "strength=%d rssi=%u\n",
818 		(s8)cmd.args[3], *rssi);
819 
820 	return 0;
821 err:
822 	dev_dbg(&client->dev, "failed=%d\n", ret);
823 	return ret;
824 }
825 
826 static const struct dvb_tuner_ops si2157_ops = {
827 	.info = {
828 		.name             = "Silicon Labs Si2141/Si2146/2147/2148/2157/2158",
829 		.frequency_min_hz =  42 * MHz,
830 		.frequency_max_hz = 870 * MHz,
831 	},
832 
833 	.init = si2157_init,
834 	.sleep = si2157_sleep,
835 	.set_params = si2157_set_params,
836 	.set_analog_params = si2157_set_analog_params,
837 	.get_frequency     = si2157_get_frequency,
838 	.get_bandwidth     = si2157_get_bandwidth,
839 	.get_if_frequency  = si2157_get_if_frequency,
840 
841 	.get_rf_strength   = si2157_get_rf_strength,
842 };
843 
844 static void si2157_stat_work(struct work_struct *work)
845 {
846 	struct si2157_dev *dev = container_of(work, struct si2157_dev, stat_work.work);
847 	struct dvb_frontend *fe = dev->fe;
848 	struct i2c_client *client = fe->tuner_priv;
849 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
850 	struct si2157_cmd cmd;
851 	int ret;
852 
853 	dev_dbg(&client->dev, "\n");
854 
855 	memcpy(cmd.args, "\x42\x00", 2);
856 	cmd.wlen = 2;
857 	cmd.rlen = 12;
858 	ret = si2157_cmd_execute(client, &cmd);
859 	if (ret)
860 		goto err;
861 
862 	c->strength.stat[0].scale = FE_SCALE_DECIBEL;
863 	c->strength.stat[0].svalue = (s8) cmd.args[3] * 1000;
864 
865 	schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
866 	return;
867 err:
868 	c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
869 	dev_dbg(&client->dev, "failed=%d\n", ret);
870 }
871 
872 static int si2157_probe(struct i2c_client *client,
873 			const struct i2c_device_id *id)
874 {
875 	struct si2157_config *cfg = client->dev.platform_data;
876 	struct dvb_frontend *fe = cfg->fe;
877 	struct si2157_dev *dev;
878 	struct si2157_cmd cmd;
879 	int ret;
880 
881 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
882 	if (!dev) {
883 		ret = -ENOMEM;
884 		dev_err(&client->dev, "kzalloc() failed\n");
885 		goto err;
886 	}
887 
888 	i2c_set_clientdata(client, dev);
889 	dev->fe = cfg->fe;
890 	dev->inversion = cfg->inversion;
891 	dev->dont_load_firmware = cfg->dont_load_firmware;
892 	dev->if_port = cfg->if_port;
893 	dev->part_id = (u8)id->driver_data;
894 	dev->if_frequency = 5000000; /* default value of property 0x0706 */
895 	mutex_init(&dev->i2c_mutex);
896 	INIT_DELAYED_WORK(&dev->stat_work, si2157_stat_work);
897 
898 	/* check if the tuner is there */
899 	cmd.wlen = 0;
900 	cmd.rlen = 1;
901 	ret = si2157_cmd_execute(client, &cmd);
902 	if (ret && ret != -EAGAIN)
903 		goto err_kfree;
904 
905 	memcpy(&fe->ops.tuner_ops, &si2157_ops, sizeof(struct dvb_tuner_ops));
906 	fe->tuner_priv = client;
907 
908 #ifdef CONFIG_MEDIA_CONTROLLER
909 	if (cfg->mdev) {
910 		dev->mdev = cfg->mdev;
911 
912 		dev->ent.name = KBUILD_MODNAME;
913 		dev->ent.function = MEDIA_ENT_F_TUNER;
914 
915 		dev->pad[SI2157_PAD_RF_INPUT].flags = MEDIA_PAD_FL_SINK;
916 		dev->pad[SI2157_PAD_RF_INPUT].sig_type = PAD_SIGNAL_ANALOG;
917 		dev->pad[SI2157_PAD_VID_OUT].flags = MEDIA_PAD_FL_SOURCE;
918 		dev->pad[SI2157_PAD_VID_OUT].sig_type = PAD_SIGNAL_ANALOG;
919 		dev->pad[SI2157_PAD_AUD_OUT].flags = MEDIA_PAD_FL_SOURCE;
920 		dev->pad[SI2157_PAD_AUD_OUT].sig_type = PAD_SIGNAL_AUDIO;
921 
922 		ret = media_entity_pads_init(&dev->ent, SI2157_NUM_PADS,
923 					     &dev->pad[0]);
924 
925 		if (ret)
926 			goto err_kfree;
927 
928 		ret = media_device_register_entity(cfg->mdev, &dev->ent);
929 		if (ret) {
930 			media_entity_cleanup(&dev->ent);
931 			goto err_kfree;
932 		}
933 	}
934 #endif
935 
936 	dev_info(&client->dev, "Silicon Labs Si21%d successfully attached\n",
937 		 dev->part_id);
938 
939 	return 0;
940 
941 err_kfree:
942 	kfree(dev);
943 err:
944 	dev_dbg(&client->dev, "failed=%d\n", ret);
945 	return ret;
946 }
947 
948 static int si2157_remove(struct i2c_client *client)
949 {
950 	struct si2157_dev *dev = i2c_get_clientdata(client);
951 	struct dvb_frontend *fe = dev->fe;
952 
953 	dev_dbg(&client->dev, "\n");
954 
955 	/* stop statistics polling */
956 	cancel_delayed_work_sync(&dev->stat_work);
957 
958 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
959 	if (dev->mdev)
960 		media_device_unregister_entity(&dev->ent);
961 #endif
962 
963 	memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
964 	fe->tuner_priv = NULL;
965 	kfree(dev);
966 
967 	return 0;
968 }
969 
970 /*
971  * The part_id used here will only be used on buggy devices that don't
972  * accept firmware uploads. Non-buggy devices should just use "si2157" for
973  * all SiLabs TER tuners, as the driver should auto-detect it.
974  */
975 static const struct i2c_device_id si2157_id_table[] = {
976 	{"si2157", SI2157},
977 	{"si2146", SI2146},
978 	{"si2141", SI2141},
979 	{"si2177", SI2177},
980 	{}
981 };
982 MODULE_DEVICE_TABLE(i2c, si2157_id_table);
983 
984 static struct i2c_driver si2157_driver = {
985 	.driver = {
986 		.name		     = "si2157",
987 		.suppress_bind_attrs = true,
988 	},
989 	.probe		= si2157_probe,
990 	.remove		= si2157_remove,
991 	.id_table	= si2157_id_table,
992 };
993 
994 module_i2c_driver(si2157_driver);
995 
996 MODULE_DESCRIPTION("Silicon Labs Si2141/Si2146/2147/2148/2157/2158 silicon tuner driver");
997 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
998 MODULE_LICENSE("GPL");
999 MODULE_FIRMWARE(SI2158_A20_FIRMWARE);
1000 MODULE_FIRMWARE(SI2141_A10_FIRMWARE);
1001 MODULE_FIRMWARE(SI2157_A30_FIRMWARE);
1002 MODULE_FIRMWARE(SI2141_60_FIRMWARE);
1003 MODULE_FIRMWARE(SI2141_61_FIRMWARE);
1004 MODULE_FIRMWARE(SI2146_11_FIRMWARE);
1005 MODULE_FIRMWARE(SI2147_50_FIRMWARE);
1006 MODULE_FIRMWARE(SI2148_32_FIRMWARE);
1007 MODULE_FIRMWARE(SI2148_33_FIRMWARE);
1008 MODULE_FIRMWARE(SI2157_50_FIRMWARE);
1009 MODULE_FIRMWARE(SI2158_50_FIRMWARE);
1010 MODULE_FIRMWARE(SI2158_51_FIRMWARE);
1011 MODULE_FIRMWARE(SI2177_50_FIRMWARE);
1012