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