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