1 /*
2  * Afatech AF9013 demodulator driver
3  *
4  * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
5  * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
6  *
7  * Thanks to Afatech who kindly provided information.
8  *
9  *    This program is free software; you can redistribute it and/or modify
10  *    it under the terms of the GNU General Public License as published by
11  *    the Free Software Foundation; either version 2 of the License, or
12  *    (at your option) any later version.
13  *
14  *    This program is distributed in the hope that it will be useful,
15  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *    GNU General Public License for more details.
18  *
19  */
20 
21 #include "af9013_priv.h"
22 
23 struct af9013_state {
24 	struct i2c_client *client;
25 	struct regmap *regmap;
26 	struct dvb_frontend fe;
27 	u32 clk;
28 	u8 tuner;
29 	u32 if_frequency;
30 	u8 ts_mode;
31 	u8 ts_output_pin;
32 	bool spec_inv;
33 	u8 api_version[4];
34 	u8 gpio[4];
35 
36 	/* tuner/demod RF and IF AGC limits used for signal strength calc */
37 	u8 signal_strength_en, rf_50, rf_80, if_50, if_80;
38 	u16 signal_strength;
39 	u32 ber;
40 	u32 ucblocks;
41 	u16 snr;
42 	u32 bandwidth_hz;
43 	enum fe_status fe_status;
44 	/* RF and IF AGC limits used for signal strength calc */
45 	u8 strength_en, rf_agc_50, rf_agc_80, if_agc_50, if_agc_80;
46 	unsigned long set_frontend_jiffies;
47 	unsigned long read_status_jiffies;
48 	unsigned long strength_jiffies;
49 	unsigned long cnr_jiffies;
50 	unsigned long ber_ucb_jiffies;
51 	bool first_tune;
52 	bool i2c_gate_state;
53 	unsigned int statistics_step:3;
54 	struct delayed_work statistics_work;
55 };
56 
57 static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval)
58 {
59 	struct i2c_client *client = state->client;
60 	int ret;
61 	u8 pos;
62 	u16 addr;
63 
64 	dev_dbg(&client->dev, "gpio %u, gpioval %02x\n", gpio, gpioval);
65 
66 	/*
67 	 * GPIO0 & GPIO1 0xd735
68 	 * GPIO2 & GPIO3 0xd736
69 	 */
70 
71 	switch (gpio) {
72 	case 0:
73 	case 1:
74 		addr = 0xd735;
75 		break;
76 	case 2:
77 	case 3:
78 		addr = 0xd736;
79 		break;
80 
81 	default:
82 		ret = -EINVAL;
83 		goto err;
84 	}
85 
86 	switch (gpio) {
87 	case 0:
88 	case 2:
89 		pos = 0;
90 		break;
91 	case 1:
92 	case 3:
93 	default:
94 		pos = 4;
95 		break;
96 	}
97 
98 	ret = regmap_update_bits(state->regmap, addr, 0x0f << pos,
99 				 gpioval << pos);
100 	if (ret)
101 		goto err;
102 
103 	return 0;
104 err:
105 	dev_dbg(&client->dev, "failed %d\n", ret);
106 	return ret;
107 }
108 
109 static int af9013_statistics_ber_unc_start(struct dvb_frontend *fe)
110 {
111 	struct af9013_state *state = fe->demodulator_priv;
112 	struct i2c_client *client = state->client;
113 	int ret;
114 
115 	dev_dbg(&client->dev, "\n");
116 
117 	/* reset and start BER counter */
118 	ret = regmap_update_bits(state->regmap, 0xd391, 0x10, 0x10);
119 	if (ret)
120 		goto err;
121 
122 	return 0;
123 err:
124 	dev_dbg(&client->dev, "failed %d\n", ret);
125 	return ret;
126 }
127 
128 static int af9013_statistics_ber_unc_result(struct dvb_frontend *fe)
129 {
130 	struct af9013_state *state = fe->demodulator_priv;
131 	struct i2c_client *client = state->client;
132 	int ret;
133 	unsigned int utmp;
134 	u8 buf[5];
135 
136 	dev_dbg(&client->dev, "\n");
137 
138 	/* check if error bit count is ready */
139 	ret = regmap_read(state->regmap, 0xd391, &utmp);
140 	if (ret)
141 		goto err;
142 
143 	if (!((utmp >> 4) & 0x01)) {
144 		dev_dbg(&client->dev, "not ready\n");
145 		return 0;
146 	}
147 
148 	ret = regmap_bulk_read(state->regmap, 0xd387, buf, 5);
149 	if (ret)
150 		goto err;
151 
152 	state->ber = (buf[2] << 16) | (buf[1] << 8) | buf[0];
153 	state->ucblocks += (buf[4] << 8) | buf[3];
154 
155 	return 0;
156 err:
157 	dev_dbg(&client->dev, "failed %d\n", ret);
158 	return ret;
159 }
160 
161 static int af9013_statistics_snr_start(struct dvb_frontend *fe)
162 {
163 	struct af9013_state *state = fe->demodulator_priv;
164 	struct i2c_client *client = state->client;
165 	int ret;
166 
167 	dev_dbg(&client->dev, "\n");
168 
169 	/* start SNR meas */
170 	ret = regmap_update_bits(state->regmap, 0xd2e1, 0x08, 0x08);
171 	if (ret)
172 		goto err;
173 
174 	return 0;
175 err:
176 	dev_dbg(&client->dev, "failed %d\n", ret);
177 	return ret;
178 }
179 
180 static int af9013_statistics_snr_result(struct dvb_frontend *fe)
181 {
182 	struct af9013_state *state = fe->demodulator_priv;
183 	struct i2c_client *client = state->client;
184 	int ret, i, len;
185 	unsigned int utmp;
186 	u8 buf[3];
187 	u32 snr_val;
188 	const struct af9013_snr *uninitialized_var(snr_lut);
189 
190 	dev_dbg(&client->dev, "\n");
191 
192 	/* check if SNR ready */
193 	ret = regmap_read(state->regmap, 0xd2e1, &utmp);
194 	if (ret)
195 		goto err;
196 
197 	if (!((utmp >> 3) & 0x01)) {
198 		dev_dbg(&client->dev, "not ready\n");
199 		return 0;
200 	}
201 
202 	/* read value */
203 	ret = regmap_bulk_read(state->regmap, 0xd2e3, buf, 3);
204 	if (ret)
205 		goto err;
206 
207 	snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0];
208 
209 	/* read current modulation */
210 	ret = regmap_read(state->regmap, 0xd3c1, &utmp);
211 	if (ret)
212 		goto err;
213 
214 	switch ((utmp >> 6) & 3) {
215 	case 0:
216 		len = ARRAY_SIZE(qpsk_snr_lut);
217 		snr_lut = qpsk_snr_lut;
218 		break;
219 	case 1:
220 		len = ARRAY_SIZE(qam16_snr_lut);
221 		snr_lut = qam16_snr_lut;
222 		break;
223 	case 2:
224 		len = ARRAY_SIZE(qam64_snr_lut);
225 		snr_lut = qam64_snr_lut;
226 		break;
227 	default:
228 		goto err;
229 	}
230 
231 	for (i = 0; i < len; i++) {
232 		utmp = snr_lut[i].snr;
233 
234 		if (snr_val < snr_lut[i].val)
235 			break;
236 	}
237 	state->snr = utmp * 10; /* dB/10 */
238 
239 	return 0;
240 err:
241 	dev_dbg(&client->dev, "failed %d\n", ret);
242 	return ret;
243 }
244 
245 static int af9013_statistics_signal_strength(struct dvb_frontend *fe)
246 {
247 	struct af9013_state *state = fe->demodulator_priv;
248 	struct i2c_client *client = state->client;
249 	int ret = 0;
250 	u8 buf[2], rf_gain, if_gain;
251 	int signal_strength;
252 
253 	dev_dbg(&client->dev, "\n");
254 
255 	if (!state->signal_strength_en)
256 		return 0;
257 
258 	ret = regmap_bulk_read(state->regmap, 0xd07c, buf, 2);
259 	if (ret)
260 		goto err;
261 
262 	rf_gain = buf[0];
263 	if_gain = buf[1];
264 
265 	signal_strength = (0xffff / \
266 		(9 * (state->rf_50 + state->if_50) - \
267 		11 * (state->rf_80 + state->if_80))) * \
268 		(10 * (rf_gain + if_gain) - \
269 		11 * (state->rf_80 + state->if_80));
270 	if (signal_strength < 0)
271 		signal_strength = 0;
272 	else if (signal_strength > 0xffff)
273 		signal_strength = 0xffff;
274 
275 	state->signal_strength = signal_strength;
276 
277 	return 0;
278 err:
279 	dev_dbg(&client->dev, "failed %d\n", ret);
280 	return ret;
281 }
282 
283 static void af9013_statistics_work(struct work_struct *work)
284 {
285 	struct af9013_state *state = container_of(work,
286 		struct af9013_state, statistics_work.work);
287 	unsigned int next_msec;
288 
289 	/* update only signal strength when demod is not locked */
290 	if (!(state->fe_status & FE_HAS_LOCK)) {
291 		state->statistics_step = 0;
292 		state->ber = 0;
293 		state->snr = 0;
294 	}
295 
296 	switch (state->statistics_step) {
297 	default:
298 		state->statistics_step = 0;
299 		/* fall-through */
300 	case 0:
301 		af9013_statistics_signal_strength(&state->fe);
302 		state->statistics_step++;
303 		next_msec = 300;
304 		break;
305 	case 1:
306 		af9013_statistics_snr_start(&state->fe);
307 		state->statistics_step++;
308 		next_msec = 200;
309 		break;
310 	case 2:
311 		af9013_statistics_ber_unc_start(&state->fe);
312 		state->statistics_step++;
313 		next_msec = 1000;
314 		break;
315 	case 3:
316 		af9013_statistics_snr_result(&state->fe);
317 		state->statistics_step++;
318 		next_msec = 400;
319 		break;
320 	case 4:
321 		af9013_statistics_ber_unc_result(&state->fe);
322 		state->statistics_step++;
323 		next_msec = 100;
324 		break;
325 	}
326 
327 	schedule_delayed_work(&state->statistics_work,
328 		msecs_to_jiffies(next_msec));
329 }
330 
331 static int af9013_get_tune_settings(struct dvb_frontend *fe,
332 	struct dvb_frontend_tune_settings *fesettings)
333 {
334 	fesettings->min_delay_ms = 800;
335 	fesettings->step_size = 0;
336 	fesettings->max_drift = 0;
337 
338 	return 0;
339 }
340 
341 static int af9013_set_frontend(struct dvb_frontend *fe)
342 {
343 	struct af9013_state *state = fe->demodulator_priv;
344 	struct i2c_client *client = state->client;
345 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
346 	int ret, i, sampling_freq;
347 	bool auto_mode, spec_inv;
348 	u8 buf[6];
349 	u32 if_frequency, freq_cw;
350 
351 	dev_dbg(&client->dev, "frequency %u, bandwidth_hz %u\n",
352 		c->frequency, c->bandwidth_hz);
353 
354 	/* program tuner */
355 	if (fe->ops.tuner_ops.set_params) {
356 		ret = fe->ops.tuner_ops.set_params(fe);
357 		if (ret)
358 			goto err;
359 	}
360 
361 	/* program CFOE coefficients */
362 	if (c->bandwidth_hz != state->bandwidth_hz) {
363 		for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
364 			if (coeff_lut[i].clock == state->clk &&
365 				coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
366 				break;
367 			}
368 		}
369 
370 		/* Return an error if can't find bandwidth or the right clock */
371 		if (i == ARRAY_SIZE(coeff_lut)) {
372 			ret = -EINVAL;
373 			goto err;
374 		}
375 
376 		ret = regmap_bulk_write(state->regmap, 0xae00, coeff_lut[i].val,
377 					sizeof(coeff_lut[i].val));
378 		if (ret)
379 			goto err;
380 	}
381 
382 	/* program frequency control */
383 	if (c->bandwidth_hz != state->bandwidth_hz || state->first_tune) {
384 		/* get used IF frequency */
385 		if (fe->ops.tuner_ops.get_if_frequency) {
386 			ret = fe->ops.tuner_ops.get_if_frequency(fe,
387 								 &if_frequency);
388 			if (ret)
389 				goto err;
390 		} else {
391 			if_frequency = state->if_frequency;
392 		}
393 
394 		dev_dbg(&client->dev, "if_frequency %u\n", if_frequency);
395 
396 		sampling_freq = if_frequency;
397 
398 		while (sampling_freq > (state->clk / 2))
399 			sampling_freq -= state->clk;
400 
401 		if (sampling_freq < 0) {
402 			sampling_freq *= -1;
403 			spec_inv = state->spec_inv;
404 		} else {
405 			spec_inv = !state->spec_inv;
406 		}
407 
408 		freq_cw = DIV_ROUND_CLOSEST_ULL((u64)sampling_freq * 0x800000,
409 						state->clk);
410 
411 		if (spec_inv)
412 			freq_cw = 0x800000 - freq_cw;
413 
414 		buf[0] = (freq_cw >>  0) & 0xff;
415 		buf[1] = (freq_cw >>  8) & 0xff;
416 		buf[2] = (freq_cw >> 16) & 0x7f;
417 
418 		freq_cw = 0x800000 - freq_cw;
419 
420 		buf[3] = (freq_cw >>  0) & 0xff;
421 		buf[4] = (freq_cw >>  8) & 0xff;
422 		buf[5] = (freq_cw >> 16) & 0x7f;
423 
424 		ret = regmap_bulk_write(state->regmap, 0xd140, buf, 3);
425 		if (ret)
426 			goto err;
427 
428 		ret = regmap_bulk_write(state->regmap, 0x9be7, buf, 6);
429 		if (ret)
430 			goto err;
431 	}
432 
433 	/* clear TPS lock flag */
434 	ret = regmap_update_bits(state->regmap, 0xd330, 0x08, 0x08);
435 	if (ret)
436 		goto err;
437 
438 	/* clear MPEG2 lock flag */
439 	ret = regmap_update_bits(state->regmap, 0xd507, 0x40, 0x00);
440 	if (ret)
441 		goto err;
442 
443 	/* empty channel function */
444 	ret = regmap_update_bits(state->regmap, 0x9bfe, 0x01, 0x00);
445 	if (ret)
446 		goto err;
447 
448 	/* empty DVB-T channel function */
449 	ret = regmap_update_bits(state->regmap, 0x9bc2, 0x01, 0x00);
450 	if (ret)
451 		goto err;
452 
453 	/* transmission parameters */
454 	auto_mode = false;
455 	memset(buf, 0, 3);
456 
457 	switch (c->transmission_mode) {
458 	case TRANSMISSION_MODE_AUTO:
459 		auto_mode = true;
460 		break;
461 	case TRANSMISSION_MODE_2K:
462 		break;
463 	case TRANSMISSION_MODE_8K:
464 		buf[0] |= (1 << 0);
465 		break;
466 	default:
467 		dev_dbg(&client->dev, "invalid transmission_mode\n");
468 		auto_mode = true;
469 	}
470 
471 	switch (c->guard_interval) {
472 	case GUARD_INTERVAL_AUTO:
473 		auto_mode = true;
474 		break;
475 	case GUARD_INTERVAL_1_32:
476 		break;
477 	case GUARD_INTERVAL_1_16:
478 		buf[0] |= (1 << 2);
479 		break;
480 	case GUARD_INTERVAL_1_8:
481 		buf[0] |= (2 << 2);
482 		break;
483 	case GUARD_INTERVAL_1_4:
484 		buf[0] |= (3 << 2);
485 		break;
486 	default:
487 		dev_dbg(&client->dev, "invalid guard_interval\n");
488 		auto_mode = true;
489 	}
490 
491 	switch (c->hierarchy) {
492 	case HIERARCHY_AUTO:
493 		auto_mode = true;
494 		break;
495 	case HIERARCHY_NONE:
496 		break;
497 	case HIERARCHY_1:
498 		buf[0] |= (1 << 4);
499 		break;
500 	case HIERARCHY_2:
501 		buf[0] |= (2 << 4);
502 		break;
503 	case HIERARCHY_4:
504 		buf[0] |= (3 << 4);
505 		break;
506 	default:
507 		dev_dbg(&client->dev, "invalid hierarchy\n");
508 		auto_mode = true;
509 	}
510 
511 	switch (c->modulation) {
512 	case QAM_AUTO:
513 		auto_mode = true;
514 		break;
515 	case QPSK:
516 		break;
517 	case QAM_16:
518 		buf[1] |= (1 << 6);
519 		break;
520 	case QAM_64:
521 		buf[1] |= (2 << 6);
522 		break;
523 	default:
524 		dev_dbg(&client->dev, "invalid modulation\n");
525 		auto_mode = true;
526 	}
527 
528 	/* Use HP. How and which case we can switch to LP? */
529 	buf[1] |= (1 << 4);
530 
531 	switch (c->code_rate_HP) {
532 	case FEC_AUTO:
533 		auto_mode = true;
534 		break;
535 	case FEC_1_2:
536 		break;
537 	case FEC_2_3:
538 		buf[2] |= (1 << 0);
539 		break;
540 	case FEC_3_4:
541 		buf[2] |= (2 << 0);
542 		break;
543 	case FEC_5_6:
544 		buf[2] |= (3 << 0);
545 		break;
546 	case FEC_7_8:
547 		buf[2] |= (4 << 0);
548 		break;
549 	default:
550 		dev_dbg(&client->dev, "invalid code_rate_HP\n");
551 		auto_mode = true;
552 	}
553 
554 	switch (c->code_rate_LP) {
555 	case FEC_AUTO:
556 		auto_mode = true;
557 		break;
558 	case FEC_1_2:
559 		break;
560 	case FEC_2_3:
561 		buf[2] |= (1 << 3);
562 		break;
563 	case FEC_3_4:
564 		buf[2] |= (2 << 3);
565 		break;
566 	case FEC_5_6:
567 		buf[2] |= (3 << 3);
568 		break;
569 	case FEC_7_8:
570 		buf[2] |= (4 << 3);
571 		break;
572 	case FEC_NONE:
573 		break;
574 	default:
575 		dev_dbg(&client->dev, "invalid code_rate_LP\n");
576 		auto_mode = true;
577 	}
578 
579 	switch (c->bandwidth_hz) {
580 	case 6000000:
581 		break;
582 	case 7000000:
583 		buf[1] |= (1 << 2);
584 		break;
585 	case 8000000:
586 		buf[1] |= (2 << 2);
587 		break;
588 	default:
589 		dev_dbg(&client->dev, "invalid bandwidth_hz\n");
590 		ret = -EINVAL;
591 		goto err;
592 	}
593 
594 	ret = regmap_bulk_write(state->regmap, 0xd3c0, buf, 3);
595 	if (ret)
596 		goto err;
597 
598 	if (auto_mode) {
599 		/* clear easy mode flag */
600 		ret = regmap_write(state->regmap, 0xaefd, 0x00);
601 		if (ret)
602 			goto err;
603 
604 		dev_dbg(&client->dev, "auto params\n");
605 	} else {
606 		/* set easy mode flag */
607 		ret = regmap_write(state->regmap, 0xaefd, 0x01);
608 		if (ret)
609 			goto err;
610 
611 		ret = regmap_write(state->regmap, 0xaefe, 0x00);
612 		if (ret)
613 			goto err;
614 
615 		dev_dbg(&client->dev, "manual params\n");
616 	}
617 
618 	/* Reset FSM */
619 	ret = regmap_write(state->regmap, 0xffff, 0x00);
620 	if (ret)
621 		goto err;
622 
623 	state->bandwidth_hz = c->bandwidth_hz;
624 	state->set_frontend_jiffies = jiffies;
625 	state->first_tune = false;
626 
627 	return 0;
628 err:
629 	dev_dbg(&client->dev, "failed %d\n", ret);
630 	return ret;
631 }
632 
633 static int af9013_get_frontend(struct dvb_frontend *fe,
634 			       struct dtv_frontend_properties *c)
635 {
636 	struct af9013_state *state = fe->demodulator_priv;
637 	struct i2c_client *client = state->client;
638 	int ret;
639 	u8 buf[3];
640 
641 	dev_dbg(&client->dev, "\n");
642 
643 	ret = regmap_bulk_read(state->regmap, 0xd3c0, buf, 3);
644 	if (ret)
645 		goto err;
646 
647 	switch ((buf[1] >> 6) & 3) {
648 	case 0:
649 		c->modulation = QPSK;
650 		break;
651 	case 1:
652 		c->modulation = QAM_16;
653 		break;
654 	case 2:
655 		c->modulation = QAM_64;
656 		break;
657 	}
658 
659 	switch ((buf[0] >> 0) & 3) {
660 	case 0:
661 		c->transmission_mode = TRANSMISSION_MODE_2K;
662 		break;
663 	case 1:
664 		c->transmission_mode = TRANSMISSION_MODE_8K;
665 	}
666 
667 	switch ((buf[0] >> 2) & 3) {
668 	case 0:
669 		c->guard_interval = GUARD_INTERVAL_1_32;
670 		break;
671 	case 1:
672 		c->guard_interval = GUARD_INTERVAL_1_16;
673 		break;
674 	case 2:
675 		c->guard_interval = GUARD_INTERVAL_1_8;
676 		break;
677 	case 3:
678 		c->guard_interval = GUARD_INTERVAL_1_4;
679 		break;
680 	}
681 
682 	switch ((buf[0] >> 4) & 7) {
683 	case 0:
684 		c->hierarchy = HIERARCHY_NONE;
685 		break;
686 	case 1:
687 		c->hierarchy = HIERARCHY_1;
688 		break;
689 	case 2:
690 		c->hierarchy = HIERARCHY_2;
691 		break;
692 	case 3:
693 		c->hierarchy = HIERARCHY_4;
694 		break;
695 	}
696 
697 	switch ((buf[2] >> 0) & 7) {
698 	case 0:
699 		c->code_rate_HP = FEC_1_2;
700 		break;
701 	case 1:
702 		c->code_rate_HP = FEC_2_3;
703 		break;
704 	case 2:
705 		c->code_rate_HP = FEC_3_4;
706 		break;
707 	case 3:
708 		c->code_rate_HP = FEC_5_6;
709 		break;
710 	case 4:
711 		c->code_rate_HP = FEC_7_8;
712 		break;
713 	}
714 
715 	switch ((buf[2] >> 3) & 7) {
716 	case 0:
717 		c->code_rate_LP = FEC_1_2;
718 		break;
719 	case 1:
720 		c->code_rate_LP = FEC_2_3;
721 		break;
722 	case 2:
723 		c->code_rate_LP = FEC_3_4;
724 		break;
725 	case 3:
726 		c->code_rate_LP = FEC_5_6;
727 		break;
728 	case 4:
729 		c->code_rate_LP = FEC_7_8;
730 		break;
731 	}
732 
733 	switch ((buf[1] >> 2) & 3) {
734 	case 0:
735 		c->bandwidth_hz = 6000000;
736 		break;
737 	case 1:
738 		c->bandwidth_hz = 7000000;
739 		break;
740 	case 2:
741 		c->bandwidth_hz = 8000000;
742 		break;
743 	}
744 
745 	return 0;
746 err:
747 	dev_dbg(&client->dev, "failed %d\n", ret);
748 	return ret;
749 }
750 
751 static int af9013_read_status(struct dvb_frontend *fe, enum fe_status *status)
752 {
753 	struct af9013_state *state = fe->demodulator_priv;
754 	struct i2c_client *client = state->client;
755 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
756 	int ret, stmp1;
757 	unsigned int utmp, utmp1, utmp2, utmp3, utmp4;
758 	u8 buf[7];
759 
760 	dev_dbg(&client->dev, "\n");
761 
762 	/*
763 	 * Return status from the cache if it is younger than 2000ms with the
764 	 * exception of last tune is done during 4000ms.
765 	 */
766 	if (time_is_after_jiffies(state->read_status_jiffies + msecs_to_jiffies(2000)) &&
767 	    time_is_before_jiffies(state->set_frontend_jiffies + msecs_to_jiffies(4000))) {
768 		*status = state->fe_status;
769 	} else {
770 		/* MPEG2 lock */
771 		ret = regmap_read(state->regmap, 0xd507, &utmp);
772 		if (ret)
773 			goto err;
774 
775 		if ((utmp >> 6) & 0x01) {
776 			utmp1 = FE_HAS_SIGNAL | FE_HAS_CARRIER |
777 				FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
778 		} else {
779 			/* TPS lock */
780 			ret = regmap_read(state->regmap, 0xd330, &utmp);
781 			if (ret)
782 				goto err;
783 
784 			if ((utmp >> 3) & 0x01)
785 				utmp1 = FE_HAS_SIGNAL | FE_HAS_CARRIER |
786 					FE_HAS_VITERBI;
787 			else
788 				utmp1 = 0;
789 		}
790 
791 		dev_dbg(&client->dev, "fe_status %02x\n", utmp1);
792 
793 		state->read_status_jiffies = jiffies;
794 
795 		state->fe_status = utmp1;
796 		*status = utmp1;
797 	}
798 
799 	/* Signal strength */
800 	switch (state->strength_en) {
801 	case 0:
802 		/* Check if we support signal strength */
803 		ret = regmap_read(state->regmap, 0x9bee, &utmp);
804 		if (ret)
805 			goto err;
806 
807 		if ((utmp >> 0) & 0x01) {
808 			/* Read agc values for signal strength estimation */
809 			ret = regmap_read(state->regmap, 0x9bbd, &utmp1);
810 			if (ret)
811 				goto err;
812 			ret = regmap_read(state->regmap, 0x9bd0, &utmp2);
813 			if (ret)
814 				goto err;
815 			ret = regmap_read(state->regmap, 0x9be2, &utmp3);
816 			if (ret)
817 				goto err;
818 			ret = regmap_read(state->regmap, 0x9be4, &utmp4);
819 			if (ret)
820 				goto err;
821 
822 			state->rf_agc_50 = utmp1;
823 			state->rf_agc_80 = utmp2;
824 			state->if_agc_50 = utmp3;
825 			state->if_agc_80 = utmp4;
826 			dev_dbg(&client->dev,
827 				"rf_agc_50 %u, rf_agc_80 %u, if_agc_50 %u, if_agc_80 %u\n",
828 				utmp1, utmp2, utmp3, utmp4);
829 
830 			state->strength_en = 1;
831 		} else {
832 			/* Signal strength is not supported */
833 			state->strength_en = 2;
834 			break;
835 		}
836 		/* Fall through */
837 	case 1:
838 		if (time_is_after_jiffies(state->strength_jiffies + msecs_to_jiffies(2000)))
839 			break;
840 
841 		/* Read value */
842 		ret = regmap_bulk_read(state->regmap, 0xd07c, buf, 2);
843 		if (ret)
844 			goto err;
845 
846 		/*
847 		 * Construct line equation from tuner dependent -80/-50 dBm agc
848 		 * limits and use it to map current agc value to dBm estimate
849 		 */
850 		#define agc_gain (buf[0] + buf[1])
851 		#define agc_gain_50dbm (state->rf_agc_50 + state->if_agc_50)
852 		#define agc_gain_80dbm (state->rf_agc_80 + state->if_agc_80)
853 		stmp1 = 30000 * (agc_gain - agc_gain_80dbm) /
854 			(agc_gain_50dbm - agc_gain_80dbm) - 80000;
855 
856 		dev_dbg(&client->dev,
857 			"strength %d, agc_gain %d, agc_gain_50dbm %d, agc_gain_80dbm %d\n",
858 			stmp1, agc_gain, agc_gain_50dbm, agc_gain_80dbm);
859 
860 		state->strength_jiffies = jiffies;
861 
862 		c->strength.stat[0].scale = FE_SCALE_DECIBEL;
863 		c->strength.stat[0].svalue = stmp1;
864 		break;
865 	default:
866 		c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
867 		break;
868 	}
869 
870 	/* CNR */
871 	switch (state->fe_status & FE_HAS_VITERBI) {
872 	case FE_HAS_VITERBI:
873 		if (time_is_after_jiffies(state->cnr_jiffies + msecs_to_jiffies(2000)))
874 			break;
875 
876 		/* Check if cnr ready */
877 		ret = regmap_read(state->regmap, 0xd2e1, &utmp);
878 		if (ret)
879 			goto err;
880 
881 		if (!((utmp >> 3) & 0x01)) {
882 			dev_dbg(&client->dev, "cnr not ready\n");
883 			break;
884 		}
885 
886 		/* Read value */
887 		ret = regmap_bulk_read(state->regmap, 0xd2e3, buf, 3);
888 		if (ret)
889 			goto err;
890 
891 		utmp1 = buf[2] << 16 | buf[1] << 8 | buf[0] << 0;
892 
893 		/* Read current modulation */
894 		ret = regmap_read(state->regmap, 0xd3c1, &utmp);
895 		if (ret)
896 			goto err;
897 
898 		switch ((utmp >> 6) & 3) {
899 		case 0:
900 			/*
901 			 * QPSK
902 			 * CNR[dB] 13 * -log10((1690000 - value) / value) + 2.6
903 			 * value [653799, 1689999], 2.6 / 13 = 3355443
904 			 */
905 			utmp1 = clamp(utmp1, 653799U, 1689999U);
906 			utmp1 = ((u64)(intlog10(utmp1)
907 				- intlog10(1690000 - utmp1)
908 				+ 3355443) * 13 * 1000) >> 24;
909 			break;
910 		case 1:
911 			/*
912 			 * QAM-16
913 			 * CNR[dB] 6 * log10((value - 370000) / (828000 - value)) + 15.7
914 			 * value [371105, 827999], 15.7 / 6 = 43900382
915 			 */
916 			utmp1 = clamp(utmp1, 371105U, 827999U);
917 			utmp1 = ((u64)(intlog10(utmp1 - 370000)
918 				- intlog10(828000 - utmp1)
919 				+ 43900382) * 6 * 1000) >> 24;
920 			break;
921 		case 2:
922 			/*
923 			 * QAM-64
924 			 * CNR[dB] 8 * log10((value - 193000) / (425000 - value)) + 23.8
925 			 * value [193246, 424999], 23.8 / 8 = 49912218
926 			 */
927 			utmp1 = clamp(utmp1, 193246U, 424999U);
928 			utmp1 = ((u64)(intlog10(utmp1 - 193000)
929 				- intlog10(425000 - utmp1)
930 				+ 49912218) * 8 * 1000) >> 24;
931 			break;
932 		default:
933 			dev_dbg(&client->dev, "invalid modulation %u\n",
934 				(utmp >> 6) & 3);
935 			utmp1 = 0;
936 			break;
937 		}
938 
939 		dev_dbg(&client->dev, "cnr %u\n", utmp1);
940 
941 		state->cnr_jiffies = jiffies;
942 
943 		c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
944 		c->cnr.stat[0].svalue = utmp1;
945 		break;
946 	default:
947 		c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
948 		break;
949 	}
950 
951 	/* BER / PER */
952 	switch (state->fe_status & FE_HAS_SYNC) {
953 	case FE_HAS_SYNC:
954 		if (time_is_after_jiffies(state->ber_ucb_jiffies + msecs_to_jiffies(2000)))
955 			break;
956 
957 		/* Check if ber / ucb is ready */
958 		ret = regmap_read(state->regmap, 0xd391, &utmp);
959 		if (ret)
960 			goto err;
961 
962 		if (!((utmp >> 4) & 0x01)) {
963 			dev_dbg(&client->dev, "ber not ready\n");
964 			break;
965 		}
966 
967 		/* Read value */
968 		ret = regmap_bulk_read(state->regmap, 0xd385, buf, 7);
969 		if (ret)
970 			goto err;
971 
972 		utmp1 = buf[4] << 16 | buf[3] << 8 | buf[2] << 0;
973 		utmp2 = (buf[1] << 8 | buf[0] << 0) * 204 * 8;
974 		utmp3 = buf[6] << 8 | buf[5] << 0;
975 		utmp4 = buf[1] << 8 | buf[0] << 0;
976 
977 		/* Use 10000 TS packets for measure */
978 		if (utmp4 != 10000) {
979 			buf[0] = (10000 >> 0) & 0xff;
980 			buf[1] = (10000 >> 8) & 0xff;
981 			ret = regmap_bulk_write(state->regmap, 0xd385, buf, 2);
982 			if (ret)
983 				goto err;
984 		}
985 
986 		/* Reset ber / ucb counter */
987 		ret = regmap_update_bits(state->regmap, 0xd391, 0x20, 0x20);
988 		if (ret)
989 			goto err;
990 
991 		dev_dbg(&client->dev, "post_bit_error %u, post_bit_count %u\n",
992 			utmp1, utmp2);
993 		dev_dbg(&client->dev, "block_error %u, block_count %u\n",
994 			utmp3, utmp4);
995 
996 		state->ber_ucb_jiffies = jiffies;
997 
998 		c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
999 		c->post_bit_error.stat[0].uvalue += utmp1;
1000 		c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1001 		c->post_bit_count.stat[0].uvalue += utmp2;
1002 
1003 		c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1004 		c->block_error.stat[0].uvalue += utmp3;
1005 		c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1006 		c->block_count.stat[0].uvalue += utmp4;
1007 		break;
1008 	default:
1009 		c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1010 		c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1011 
1012 		c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1013 		c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1014 		break;
1015 	}
1016 
1017 	return 0;
1018 err:
1019 	dev_dbg(&client->dev, "failed %d\n", ret);
1020 	return ret;
1021 }
1022 
1023 static int af9013_read_snr(struct dvb_frontend *fe, u16 *snr)
1024 {
1025 	struct af9013_state *state = fe->demodulator_priv;
1026 	*snr = state->snr;
1027 	return 0;
1028 }
1029 
1030 static int af9013_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
1031 {
1032 	struct af9013_state *state = fe->demodulator_priv;
1033 	*strength = state->signal_strength;
1034 	return 0;
1035 }
1036 
1037 static int af9013_read_ber(struct dvb_frontend *fe, u32 *ber)
1038 {
1039 	struct af9013_state *state = fe->demodulator_priv;
1040 	*ber = state->ber;
1041 	return 0;
1042 }
1043 
1044 static int af9013_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1045 {
1046 	struct af9013_state *state = fe->demodulator_priv;
1047 	*ucblocks = state->ucblocks;
1048 	return 0;
1049 }
1050 
1051 static int af9013_init(struct dvb_frontend *fe)
1052 {
1053 	struct af9013_state *state = fe->demodulator_priv;
1054 	struct i2c_client *client = state->client;
1055 	int ret, i, len;
1056 	unsigned int utmp;
1057 	u8 buf[3];
1058 	const struct af9013_reg_bit *init;
1059 
1060 	dev_dbg(&client->dev, "\n");
1061 
1062 	/* ADC on */
1063 	ret = regmap_update_bits(state->regmap, 0xd73a, 0x08, 0x00);
1064 	if (ret)
1065 		goto err;
1066 
1067 	/* Clear reset */
1068 	ret = regmap_update_bits(state->regmap, 0xd417, 0x02, 0x00);
1069 	if (ret)
1070 		goto err;
1071 
1072 	/* Disable reset */
1073 	ret = regmap_update_bits(state->regmap, 0xd417, 0x10, 0x00);
1074 	if (ret)
1075 		goto err;
1076 
1077 	/* write API version to firmware */
1078 	ret = regmap_bulk_write(state->regmap, 0x9bf2, state->api_version, 4);
1079 	if (ret)
1080 		goto err;
1081 
1082 	/* program ADC control */
1083 	switch (state->clk) {
1084 	case 28800000: /* 28.800 MHz */
1085 		utmp = 0;
1086 		break;
1087 	case 20480000: /* 20.480 MHz */
1088 		utmp = 1;
1089 		break;
1090 	case 28000000: /* 28.000 MHz */
1091 		utmp = 2;
1092 		break;
1093 	case 25000000: /* 25.000 MHz */
1094 		utmp = 3;
1095 		break;
1096 	default:
1097 		ret = -EINVAL;
1098 		goto err;
1099 	}
1100 
1101 	ret = regmap_update_bits(state->regmap, 0x9bd2, 0x0f, utmp);
1102 	if (ret)
1103 		goto err;
1104 
1105 	utmp = div_u64((u64)state->clk * 0x80000, 1000000);
1106 	buf[0] = (utmp >>  0) & 0xff;
1107 	buf[1] = (utmp >>  8) & 0xff;
1108 	buf[2] = (utmp >> 16) & 0xff;
1109 	ret = regmap_bulk_write(state->regmap, 0xd180, buf, 3);
1110 	if (ret)
1111 		goto err;
1112 
1113 	/* load OFSM settings */
1114 	dev_dbg(&client->dev, "load ofsm settings\n");
1115 	len = ARRAY_SIZE(ofsm_init);
1116 	init = ofsm_init;
1117 	for (i = 0; i < len; i++) {
1118 		u16 reg = init[i].addr;
1119 		u8 mask = GENMASK(init[i].pos + init[i].len - 1, init[i].pos);
1120 		u8 val = init[i].val << init[i].pos;
1121 
1122 		ret = regmap_update_bits(state->regmap, reg, mask, val);
1123 		if (ret)
1124 			goto err;
1125 	}
1126 
1127 	/* load tuner specific settings */
1128 	dev_dbg(&client->dev, "load tuner specific settings\n");
1129 	switch (state->tuner) {
1130 	case AF9013_TUNER_MXL5003D:
1131 		len = ARRAY_SIZE(tuner_init_mxl5003d);
1132 		init = tuner_init_mxl5003d;
1133 		break;
1134 	case AF9013_TUNER_MXL5005D:
1135 	case AF9013_TUNER_MXL5005R:
1136 	case AF9013_TUNER_MXL5007T:
1137 		len = ARRAY_SIZE(tuner_init_mxl5005);
1138 		init = tuner_init_mxl5005;
1139 		break;
1140 	case AF9013_TUNER_ENV77H11D5:
1141 		len = ARRAY_SIZE(tuner_init_env77h11d5);
1142 		init = tuner_init_env77h11d5;
1143 		break;
1144 	case AF9013_TUNER_MT2060:
1145 		len = ARRAY_SIZE(tuner_init_mt2060);
1146 		init = tuner_init_mt2060;
1147 		break;
1148 	case AF9013_TUNER_MC44S803:
1149 		len = ARRAY_SIZE(tuner_init_mc44s803);
1150 		init = tuner_init_mc44s803;
1151 		break;
1152 	case AF9013_TUNER_QT1010:
1153 	case AF9013_TUNER_QT1010A:
1154 		len = ARRAY_SIZE(tuner_init_qt1010);
1155 		init = tuner_init_qt1010;
1156 		break;
1157 	case AF9013_TUNER_MT2060_2:
1158 		len = ARRAY_SIZE(tuner_init_mt2060_2);
1159 		init = tuner_init_mt2060_2;
1160 		break;
1161 	case AF9013_TUNER_TDA18271:
1162 	case AF9013_TUNER_TDA18218:
1163 		len = ARRAY_SIZE(tuner_init_tda18271);
1164 		init = tuner_init_tda18271;
1165 		break;
1166 	case AF9013_TUNER_UNKNOWN:
1167 	default:
1168 		len = ARRAY_SIZE(tuner_init_unknown);
1169 		init = tuner_init_unknown;
1170 		break;
1171 	}
1172 
1173 	for (i = 0; i < len; i++) {
1174 		u16 reg = init[i].addr;
1175 		u8 mask = GENMASK(init[i].pos + init[i].len - 1, init[i].pos);
1176 		u8 val = init[i].val << init[i].pos;
1177 
1178 		ret = regmap_update_bits(state->regmap, reg, mask, val);
1179 		if (ret)
1180 			goto err;
1181 	}
1182 
1183 	/* TS interface */
1184 	if (state->ts_output_pin == 7)
1185 		utmp = 1 << 3 | state->ts_mode << 1;
1186 	else
1187 		utmp = 0 << 3 | state->ts_mode << 1;
1188 	ret = regmap_update_bits(state->regmap, 0xd500, 0x0e, utmp);
1189 	if (ret)
1190 		goto err;
1191 
1192 	/* enable lock led */
1193 	ret = regmap_update_bits(state->regmap, 0xd730, 0x01, 0x01);
1194 	if (ret)
1195 		goto err;
1196 
1197 	/* check if we support signal strength */
1198 	if (!state->signal_strength_en) {
1199 		ret = regmap_read(state->regmap, 0x9bee, &utmp);
1200 		if (ret)
1201 			goto err;
1202 
1203 		state->signal_strength_en = (utmp >> 0) & 0x01;
1204 	}
1205 
1206 	/* read values needed for signal strength calculation */
1207 	if (state->signal_strength_en && !state->rf_50) {
1208 		ret = regmap_bulk_read(state->regmap, 0x9bbd, &state->rf_50, 1);
1209 		if (ret)
1210 			goto err;
1211 		ret = regmap_bulk_read(state->regmap, 0x9bd0, &state->rf_80, 1);
1212 		if (ret)
1213 			goto err;
1214 		ret = regmap_bulk_read(state->regmap, 0x9be2, &state->if_50, 1);
1215 		if (ret)
1216 			goto err;
1217 		ret = regmap_bulk_read(state->regmap, 0x9be4, &state->if_80, 1);
1218 		if (ret)
1219 			goto err;
1220 	}
1221 
1222 	/* SNR */
1223 	ret = regmap_write(state->regmap, 0xd2e2, 0x01);
1224 	if (ret)
1225 		goto err;
1226 
1227 	/* BER / UCB */
1228 	buf[0] = (10000 >> 0) & 0xff;
1229 	buf[1] = (10000 >> 8) & 0xff;
1230 	ret = regmap_bulk_write(state->regmap, 0xd385, buf, 2);
1231 	if (ret)
1232 		goto err;
1233 
1234 	/* enable FEC monitor */
1235 	ret = regmap_update_bits(state->regmap, 0xd392, 0x02, 0x02);
1236 	if (ret)
1237 		goto err;
1238 
1239 	state->first_tune = true;
1240 	schedule_delayed_work(&state->statistics_work, msecs_to_jiffies(400));
1241 
1242 	return 0;
1243 err:
1244 	dev_dbg(&client->dev, "failed %d\n", ret);
1245 	return ret;
1246 }
1247 
1248 static int af9013_sleep(struct dvb_frontend *fe)
1249 {
1250 	struct af9013_state *state = fe->demodulator_priv;
1251 	struct i2c_client *client = state->client;
1252 	int ret;
1253 	unsigned int utmp;
1254 
1255 	dev_dbg(&client->dev, "\n");
1256 
1257 	/* stop statistics polling */
1258 	cancel_delayed_work_sync(&state->statistics_work);
1259 
1260 	/* disable lock led */
1261 	ret = regmap_update_bits(state->regmap, 0xd730, 0x01, 0x00);
1262 	if (ret)
1263 		goto err;
1264 
1265 	/* Enable reset */
1266 	ret = regmap_update_bits(state->regmap, 0xd417, 0x10, 0x10);
1267 	if (ret)
1268 		goto err;
1269 
1270 	/* Start reset execution */
1271 	ret = regmap_write(state->regmap, 0xaeff, 0x01);
1272 	if (ret)
1273 		goto err;
1274 
1275 	/* Wait reset performs */
1276 	ret = regmap_read_poll_timeout(state->regmap, 0xd417, utmp,
1277 				       (utmp >> 1) & 0x01, 5000, 1000000);
1278 	if (ret)
1279 		goto err;
1280 
1281 	if (!((utmp >> 1) & 0x01)) {
1282 		ret = -ETIMEDOUT;
1283 		goto err;
1284 	}
1285 
1286 	/* ADC off */
1287 	ret = regmap_update_bits(state->regmap, 0xd73a, 0x08, 0x08);
1288 	if (ret)
1289 		goto err;
1290 
1291 	return 0;
1292 err:
1293 	dev_dbg(&client->dev, "failed %d\n", ret);
1294 	return ret;
1295 }
1296 
1297 static int af9013_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1298 {
1299 	int ret;
1300 	struct af9013_state *state = fe->demodulator_priv;
1301 	struct i2c_client *client = state->client;
1302 
1303 	dev_dbg(&client->dev, "enable %d\n", enable);
1304 
1305 	/* gate already open or close */
1306 	if (state->i2c_gate_state == enable)
1307 		return 0;
1308 
1309 	if (state->ts_mode == AF9013_TS_MODE_USB)
1310 		ret = regmap_update_bits(state->regmap, 0xd417, 0x08,
1311 					 enable << 3);
1312 	else
1313 		ret = regmap_update_bits(state->regmap, 0xd607, 0x04,
1314 					 enable << 2);
1315 	if (ret)
1316 		goto err;
1317 
1318 	state->i2c_gate_state = enable;
1319 
1320 	return 0;
1321 err:
1322 	dev_dbg(&client->dev, "failed %d\n", ret);
1323 	return ret;
1324 }
1325 
1326 static void af9013_release(struct dvb_frontend *fe)
1327 {
1328 	struct af9013_state *state = fe->demodulator_priv;
1329 	struct i2c_client *client = state->client;
1330 
1331 	dev_dbg(&client->dev, "\n");
1332 
1333 	i2c_unregister_device(client);
1334 }
1335 
1336 static const struct dvb_frontend_ops af9013_ops;
1337 
1338 static int af9013_download_firmware(struct af9013_state *state)
1339 {
1340 	struct i2c_client *client = state->client;
1341 	int ret, i, len, rem;
1342 	unsigned int utmp;
1343 	u8 buf[4];
1344 	u16 checksum = 0;
1345 	const struct firmware *firmware;
1346 	const char *name = AF9013_FIRMWARE;
1347 
1348 	dev_dbg(&client->dev, "\n");
1349 
1350 	/* Check whether firmware is already running */
1351 	ret = regmap_read(state->regmap, 0x98be, &utmp);
1352 	if (ret)
1353 		goto err;
1354 
1355 	dev_dbg(&client->dev, "firmware status %02x\n", utmp);
1356 
1357 	if (utmp == 0x0c)
1358 		return 0;
1359 
1360 	dev_info(&client->dev, "found a '%s' in cold state, will try to load a firmware\n",
1361 		 af9013_ops.info.name);
1362 
1363 	/* Request the firmware, will block and timeout */
1364 	ret = request_firmware(&firmware, name, &client->dev);
1365 	if (ret) {
1366 		dev_info(&client->dev, "firmware file '%s' not found %d\n",
1367 			 name, ret);
1368 		goto err;
1369 	}
1370 
1371 	dev_info(&client->dev, "downloading firmware from file '%s'\n",
1372 		 name);
1373 
1374 	/* Write firmware checksum & size */
1375 	for (i = 0; i < firmware->size; i++)
1376 		checksum += firmware->data[i];
1377 
1378 	buf[0] = (checksum >> 8) & 0xff;
1379 	buf[1] = (checksum >> 0) & 0xff;
1380 	buf[2] = (firmware->size >> 8) & 0xff;
1381 	buf[3] = (firmware->size >> 0) & 0xff;
1382 	ret = regmap_bulk_write(state->regmap, 0x50fc, buf, 4);
1383 	if (ret)
1384 		goto err_release_firmware;
1385 
1386 	/* Download firmware */
1387 	#define LEN_MAX 16
1388 	for (rem = firmware->size; rem > 0; rem -= LEN_MAX) {
1389 		len = min(LEN_MAX, rem);
1390 		ret = regmap_bulk_write(state->regmap,
1391 					0x5100 + firmware->size - rem,
1392 					&firmware->data[firmware->size - rem],
1393 					len);
1394 		if (ret) {
1395 			dev_err(&client->dev, "firmware download failed %d\n",
1396 				ret);
1397 			goto err_release_firmware;
1398 		}
1399 	}
1400 
1401 	release_firmware(firmware);
1402 
1403 	/* Boot firmware */
1404 	ret = regmap_write(state->regmap, 0xe205, 0x01);
1405 	if (ret)
1406 		goto err;
1407 
1408 	/* Check firmware status. 0c=OK, 04=fail */
1409 	ret = regmap_read_poll_timeout(state->regmap, 0x98be, utmp,
1410 				       (utmp == 0x0c || utmp == 0x04),
1411 				       5000, 1000000);
1412 	if (ret)
1413 		goto err;
1414 
1415 	dev_dbg(&client->dev, "firmware status %02x\n", utmp);
1416 
1417 	if (utmp == 0x04) {
1418 		ret = -ENODEV;
1419 		dev_err(&client->dev, "firmware did not run\n");
1420 		goto err;
1421 	} else if (utmp != 0x0c) {
1422 		ret = -ENODEV;
1423 		dev_err(&client->dev, "firmware boot timeout\n");
1424 		goto err;
1425 	}
1426 
1427 	dev_info(&client->dev, "found a '%s' in warm state\n",
1428 		 af9013_ops.info.name);
1429 
1430 	return 0;
1431 err_release_firmware:
1432 	release_firmware(firmware);
1433 err:
1434 	dev_dbg(&client->dev, "failed %d\n", ret);
1435 	return ret;
1436 }
1437 
1438 /*
1439  * XXX: That is wrapper to af9013_probe() via driver core in order to provide
1440  * proper I2C client for legacy media attach binding.
1441  * New users must use I2C client binding directly!
1442  */
1443 struct dvb_frontend *af9013_attach(const struct af9013_config *config,
1444 				   struct i2c_adapter *i2c)
1445 {
1446 	struct i2c_client *client;
1447 	struct i2c_board_info board_info;
1448 	struct af9013_platform_data pdata;
1449 
1450 	pdata.clk = config->clock;
1451 	pdata.tuner = config->tuner;
1452 	pdata.if_frequency = config->if_frequency;
1453 	pdata.ts_mode = config->ts_mode;
1454 	pdata.ts_output_pin = 7;
1455 	pdata.spec_inv = config->spec_inv;
1456 	memcpy(&pdata.api_version, config->api_version, sizeof(pdata.api_version));
1457 	memcpy(&pdata.gpio, config->gpio, sizeof(pdata.gpio));
1458 	pdata.attach_in_use = true;
1459 
1460 	memset(&board_info, 0, sizeof(board_info));
1461 	strlcpy(board_info.type, "af9013", sizeof(board_info.type));
1462 	board_info.addr = config->i2c_addr;
1463 	board_info.platform_data = &pdata;
1464 	client = i2c_new_device(i2c, &board_info);
1465 	if (!client || !client->dev.driver)
1466 		return NULL;
1467 
1468 	return pdata.get_dvb_frontend(client);
1469 }
1470 EXPORT_SYMBOL(af9013_attach);
1471 
1472 static const struct dvb_frontend_ops af9013_ops = {
1473 	.delsys = { SYS_DVBT },
1474 	.info = {
1475 		.name = "Afatech AF9013",
1476 		.frequency_min = 174000000,
1477 		.frequency_max = 862000000,
1478 		.frequency_stepsize = 250000,
1479 		.frequency_tolerance = 0,
1480 		.caps =	FE_CAN_FEC_1_2 |
1481 			FE_CAN_FEC_2_3 |
1482 			FE_CAN_FEC_3_4 |
1483 			FE_CAN_FEC_5_6 |
1484 			FE_CAN_FEC_7_8 |
1485 			FE_CAN_FEC_AUTO |
1486 			FE_CAN_QPSK |
1487 			FE_CAN_QAM_16 |
1488 			FE_CAN_QAM_64 |
1489 			FE_CAN_QAM_AUTO |
1490 			FE_CAN_TRANSMISSION_MODE_AUTO |
1491 			FE_CAN_GUARD_INTERVAL_AUTO |
1492 			FE_CAN_HIERARCHY_AUTO |
1493 			FE_CAN_RECOVER |
1494 			FE_CAN_MUTE_TS
1495 	},
1496 
1497 	.release = af9013_release,
1498 
1499 	.init = af9013_init,
1500 	.sleep = af9013_sleep,
1501 
1502 	.get_tune_settings = af9013_get_tune_settings,
1503 	.set_frontend = af9013_set_frontend,
1504 	.get_frontend = af9013_get_frontend,
1505 
1506 	.read_status = af9013_read_status,
1507 	.read_snr = af9013_read_snr,
1508 	.read_signal_strength = af9013_read_signal_strength,
1509 	.read_ber = af9013_read_ber,
1510 	.read_ucblocks = af9013_read_ucblocks,
1511 
1512 	.i2c_gate_ctrl = af9013_i2c_gate_ctrl,
1513 };
1514 
1515 static struct dvb_frontend *af9013_get_dvb_frontend(struct i2c_client *client)
1516 {
1517 	struct af9013_state *state = i2c_get_clientdata(client);
1518 
1519 	dev_dbg(&client->dev, "\n");
1520 
1521 	return &state->fe;
1522 }
1523 
1524 /* Own I2C access routines needed for regmap as chip uses extra command byte */
1525 static int af9013_wregs(struct i2c_client *client, u8 cmd, u16 reg,
1526 			const u8 *val, int len)
1527 {
1528 	int ret;
1529 	u8 buf[21];
1530 	struct i2c_msg msg[1] = {
1531 		{
1532 			.addr = client->addr,
1533 			.flags = 0,
1534 			.len = 3 + len,
1535 			.buf = buf,
1536 		}
1537 	};
1538 
1539 	if (3 + len > sizeof(buf)) {
1540 		ret = -EINVAL;
1541 		goto err;
1542 	}
1543 
1544 	buf[0] = (reg >> 8) & 0xff;
1545 	buf[1] = (reg >> 0) & 0xff;
1546 	buf[2] = cmd;
1547 	memcpy(&buf[3], val, len);
1548 	ret = i2c_transfer(client->adapter, msg, 1);
1549 	if (ret < 0) {
1550 		goto err;
1551 	} else if (ret != 1) {
1552 		ret = -EREMOTEIO;
1553 		goto err;
1554 	}
1555 
1556 	return 0;
1557 err:
1558 	dev_dbg(&client->dev, "failed %d\n", ret);
1559 	return ret;
1560 }
1561 
1562 static int af9013_rregs(struct i2c_client *client, u8 cmd, u16 reg,
1563 			u8 *val, int len)
1564 {
1565 	int ret;
1566 	u8 buf[3];
1567 	struct i2c_msg msg[2] = {
1568 		{
1569 			.addr = client->addr,
1570 			.flags = 0,
1571 			.len = 3,
1572 			.buf = buf,
1573 		}, {
1574 			.addr = client->addr,
1575 			.flags = I2C_M_RD,
1576 			.len = len,
1577 			.buf = val,
1578 		}
1579 	};
1580 
1581 	buf[0] = (reg >> 8) & 0xff;
1582 	buf[1] = (reg >> 0) & 0xff;
1583 	buf[2] = cmd;
1584 	ret = i2c_transfer(client->adapter, msg, 2);
1585 	if (ret < 0) {
1586 		goto err;
1587 	} else if (ret != 2) {
1588 		ret = -EREMOTEIO;
1589 		goto err;
1590 	}
1591 
1592 	return 0;
1593 err:
1594 	dev_dbg(&client->dev, "failed %d\n", ret);
1595 	return ret;
1596 }
1597 
1598 static int af9013_regmap_write(void *context, const void *data, size_t count)
1599 {
1600 	struct i2c_client *client = context;
1601 	struct af9013_state *state = i2c_get_clientdata(client);
1602 	int ret, i;
1603 	u8 cmd;
1604 	u16 reg = ((u8 *)data)[0] << 8|((u8 *)data)[1] << 0;
1605 	u8 *val = &((u8 *)data)[2];
1606 	const unsigned int len = count - 2;
1607 
1608 	if (state->ts_mode == AF9013_TS_MODE_USB && (reg & 0xff00) != 0xae00) {
1609 		cmd = 0 << 7|0 << 6|(len - 1) << 2|1 << 1|1 << 0;
1610 		ret = af9013_wregs(client, cmd, reg, val, len);
1611 		if (ret)
1612 			goto err;
1613 	} else if (reg >= 0x5100 && reg < 0x8fff) {
1614 		/* Firmware download */
1615 		cmd = 1 << 7|1 << 6|(len - 1) << 2|1 << 1|1 << 0;
1616 		ret = af9013_wregs(client, cmd, reg, val, len);
1617 		if (ret)
1618 			goto err;
1619 	} else {
1620 		cmd = 0 << 7|0 << 6|(1 - 1) << 2|1 << 1|1 << 0;
1621 		for (i = 0; i < len; i++) {
1622 			ret = af9013_wregs(client, cmd, reg + i, val + i, 1);
1623 			if (ret)
1624 				goto err;
1625 		}
1626 	}
1627 
1628 	return 0;
1629 err:
1630 	dev_dbg(&client->dev, "failed %d\n", ret);
1631 	return ret;
1632 }
1633 
1634 static int af9013_regmap_read(void *context, const void *reg_buf,
1635 			      size_t reg_size, void *val_buf, size_t val_size)
1636 {
1637 	struct i2c_client *client = context;
1638 	struct af9013_state *state = i2c_get_clientdata(client);
1639 	int ret, i;
1640 	u8 cmd;
1641 	u16 reg = ((u8 *)reg_buf)[0] << 8|((u8 *)reg_buf)[1] << 0;
1642 	u8 *val = &((u8 *)val_buf)[0];
1643 	const unsigned int len = val_size;
1644 
1645 	if (state->ts_mode == AF9013_TS_MODE_USB && (reg & 0xff00) != 0xae00) {
1646 		cmd = 0 << 7|0 << 6|(len - 1) << 2|1 << 1|0 << 0;
1647 		ret = af9013_rregs(client, cmd, reg, val_buf, len);
1648 		if (ret)
1649 			goto err;
1650 	} else {
1651 		cmd = 0 << 7|0 << 6|(1 - 1) << 2|1 << 1|0 << 0;
1652 		for (i = 0; i < len; i++) {
1653 			ret = af9013_rregs(client, cmd, reg + i, val + i, 1);
1654 			if (ret)
1655 				goto err;
1656 		}
1657 	}
1658 
1659 	return 0;
1660 err:
1661 	dev_dbg(&client->dev, "failed %d\n", ret);
1662 	return ret;
1663 }
1664 
1665 static int af9013_probe(struct i2c_client *client,
1666 			const struct i2c_device_id *id)
1667 {
1668 	struct af9013_state *state;
1669 	struct af9013_platform_data *pdata = client->dev.platform_data;
1670 	struct dtv_frontend_properties *c;
1671 	int ret, i;
1672 	u8 firmware_version[4];
1673 	static const struct regmap_bus regmap_bus = {
1674 		.read = af9013_regmap_read,
1675 		.write = af9013_regmap_write,
1676 	};
1677 	static const struct regmap_config regmap_config = {
1678 		.reg_bits    =  16,
1679 		.val_bits    =  8,
1680 	};
1681 
1682 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1683 	if (!state) {
1684 		ret = -ENOMEM;
1685 		goto err;
1686 	}
1687 
1688 	/* Setup the state */
1689 	state->client = client;
1690 	i2c_set_clientdata(client, state);
1691 	state->clk = pdata->clk;
1692 	state->tuner = pdata->tuner;
1693 	state->if_frequency = pdata->if_frequency;
1694 	state->ts_mode = pdata->ts_mode;
1695 	state->ts_output_pin = pdata->ts_output_pin;
1696 	state->spec_inv = pdata->spec_inv;
1697 	memcpy(&state->api_version, pdata->api_version, sizeof(state->api_version));
1698 	memcpy(&state->gpio, pdata->gpio, sizeof(state->gpio));
1699 	INIT_DELAYED_WORK(&state->statistics_work, af9013_statistics_work);
1700 	state->regmap = regmap_init(&client->dev, &regmap_bus, client,
1701 				  &regmap_config);
1702 	if (IS_ERR(state->regmap)) {
1703 		ret = PTR_ERR(state->regmap);
1704 		goto err_kfree;
1705 	}
1706 
1707 	/* Download firmware */
1708 	if (state->ts_mode != AF9013_TS_MODE_USB) {
1709 		ret = af9013_download_firmware(state);
1710 		if (ret)
1711 			goto err_regmap_exit;
1712 	}
1713 
1714 	/* Firmware version */
1715 	ret = regmap_bulk_read(state->regmap, 0x5103, firmware_version,
1716 			       sizeof(firmware_version));
1717 	if (ret)
1718 		goto err_regmap_exit;
1719 
1720 	/* Set GPIOs */
1721 	for (i = 0; i < sizeof(state->gpio); i++) {
1722 		ret = af9013_set_gpio(state, i, state->gpio[i]);
1723 		if (ret)
1724 			goto err_regmap_exit;
1725 	}
1726 
1727 	/* Create dvb frontend */
1728 	memcpy(&state->fe.ops, &af9013_ops, sizeof(state->fe.ops));
1729 	if (!pdata->attach_in_use)
1730 		state->fe.ops.release = NULL;
1731 	state->fe.demodulator_priv = state;
1732 
1733 	/* Setup callbacks */
1734 	pdata->get_dvb_frontend = af9013_get_dvb_frontend;
1735 
1736 	/* Init stats to indicate which stats are supported */
1737 	c = &state->fe.dtv_property_cache;
1738 	c->strength.len = 1;
1739 	c->cnr.len = 1;
1740 	c->post_bit_error.len = 1;
1741 	c->post_bit_count.len = 1;
1742 	c->block_error.len = 1;
1743 	c->block_count.len = 1;
1744 
1745 	dev_info(&client->dev, "Afatech AF9013 successfully attached\n");
1746 	dev_info(&client->dev, "firmware version: %d.%d.%d.%d\n",
1747 		 firmware_version[0], firmware_version[1],
1748 		 firmware_version[2], firmware_version[3]);
1749 	return 0;
1750 err_regmap_exit:
1751 	regmap_exit(state->regmap);
1752 err_kfree:
1753 	kfree(state);
1754 err:
1755 	dev_dbg(&client->dev, "failed %d\n", ret);
1756 	return ret;
1757 }
1758 
1759 static int af9013_remove(struct i2c_client *client)
1760 {
1761 	struct af9013_state *state = i2c_get_clientdata(client);
1762 
1763 	dev_dbg(&client->dev, "\n");
1764 
1765 	/* Stop statistics polling */
1766 	cancel_delayed_work_sync(&state->statistics_work);
1767 
1768 	regmap_exit(state->regmap);
1769 
1770 	kfree(state);
1771 
1772 	return 0;
1773 }
1774 
1775 static const struct i2c_device_id af9013_id_table[] = {
1776 	{"af9013", 0},
1777 	{}
1778 };
1779 MODULE_DEVICE_TABLE(i2c, af9013_id_table);
1780 
1781 static struct i2c_driver af9013_driver = {
1782 	.driver = {
1783 		.name	= "af9013",
1784 		.suppress_bind_attrs = true,
1785 	},
1786 	.probe		= af9013_probe,
1787 	.remove		= af9013_remove,
1788 	.id_table	= af9013_id_table,
1789 };
1790 
1791 module_i2c_driver(af9013_driver);
1792 
1793 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1794 MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver");
1795 MODULE_LICENSE("GPL");
1796 MODULE_FIRMWARE(AF9013_FIRMWARE);
1797