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_bit *init;
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 	/* load OFSM settings */
902 	dev_dbg(&client->dev, "load ofsm settings\n");
903 	len = ARRAY_SIZE(ofsm_init);
904 	init = ofsm_init;
905 	for (i = 0; i < len; i++) {
906 		u16 reg = init[i].addr;
907 		u8 mask = GENMASK(init[i].pos + init[i].len - 1, init[i].pos);
908 		u8 val = init[i].val << init[i].pos;
909 
910 		ret = regmap_update_bits(state->regmap, reg, mask, val);
911 		if (ret)
912 			goto err;
913 	}
914 
915 	/* load tuner specific settings */
916 	dev_dbg(&client->dev, "load tuner specific settings\n");
917 	switch (state->tuner) {
918 	case AF9013_TUNER_MXL5003D:
919 		len = ARRAY_SIZE(tuner_init_mxl5003d);
920 		init = tuner_init_mxl5003d;
921 		break;
922 	case AF9013_TUNER_MXL5005D:
923 	case AF9013_TUNER_MXL5005R:
924 	case AF9013_TUNER_MXL5007T:
925 		len = ARRAY_SIZE(tuner_init_mxl5005);
926 		init = tuner_init_mxl5005;
927 		break;
928 	case AF9013_TUNER_ENV77H11D5:
929 		len = ARRAY_SIZE(tuner_init_env77h11d5);
930 		init = tuner_init_env77h11d5;
931 		break;
932 	case AF9013_TUNER_MT2060:
933 		len = ARRAY_SIZE(tuner_init_mt2060);
934 		init = tuner_init_mt2060;
935 		break;
936 	case AF9013_TUNER_MC44S803:
937 		len = ARRAY_SIZE(tuner_init_mc44s803);
938 		init = tuner_init_mc44s803;
939 		break;
940 	case AF9013_TUNER_QT1010:
941 	case AF9013_TUNER_QT1010A:
942 		len = ARRAY_SIZE(tuner_init_qt1010);
943 		init = tuner_init_qt1010;
944 		break;
945 	case AF9013_TUNER_MT2060_2:
946 		len = ARRAY_SIZE(tuner_init_mt2060_2);
947 		init = tuner_init_mt2060_2;
948 		break;
949 	case AF9013_TUNER_TDA18271:
950 	case AF9013_TUNER_TDA18218:
951 		len = ARRAY_SIZE(tuner_init_tda18271);
952 		init = tuner_init_tda18271;
953 		break;
954 	case AF9013_TUNER_UNKNOWN:
955 	default:
956 		len = ARRAY_SIZE(tuner_init_unknown);
957 		init = tuner_init_unknown;
958 		break;
959 	}
960 
961 	for (i = 0; i < len; i++) {
962 		u16 reg = init[i].addr;
963 		u8 mask = GENMASK(init[i].pos + init[i].len - 1, init[i].pos);
964 		u8 val = init[i].val << init[i].pos;
965 
966 		ret = regmap_update_bits(state->regmap, reg, mask, val);
967 		if (ret)
968 			goto err;
969 	}
970 
971 	/* TS interface */
972 	if (state->ts_output_pin == 7)
973 		utmp = 1 << 3 | state->ts_mode << 1;
974 	else
975 		utmp = 0 << 3 | state->ts_mode << 1;
976 	ret = regmap_update_bits(state->regmap, 0xd500, 0x0e, utmp);
977 	if (ret)
978 		goto err;
979 
980 	/* enable lock led */
981 	ret = regmap_update_bits(state->regmap, 0xd730, 0x01, 0x01);
982 	if (ret)
983 		goto err;
984 
985 	state->first_tune = true;
986 
987 	return 0;
988 err:
989 	dev_dbg(&client->dev, "failed %d\n", ret);
990 	return ret;
991 }
992 
993 static int af9013_sleep(struct dvb_frontend *fe)
994 {
995 	struct af9013_state *state = fe->demodulator_priv;
996 	struct i2c_client *client = state->client;
997 	int ret;
998 	unsigned int utmp;
999 
1000 	dev_dbg(&client->dev, "\n");
1001 
1002 	/* disable lock led */
1003 	ret = regmap_update_bits(state->regmap, 0xd730, 0x01, 0x00);
1004 	if (ret)
1005 		goto err;
1006 
1007 	/* Enable reset */
1008 	ret = regmap_update_bits(state->regmap, 0xd417, 0x10, 0x10);
1009 	if (ret)
1010 		goto err;
1011 
1012 	/* Start reset execution */
1013 	ret = regmap_write(state->regmap, 0xaeff, 0x01);
1014 	if (ret)
1015 		goto err;
1016 
1017 	/* Wait reset performs */
1018 	ret = regmap_read_poll_timeout(state->regmap, 0xd417, utmp,
1019 				       (utmp >> 1) & 0x01, 5000, 1000000);
1020 	if (ret)
1021 		goto err;
1022 
1023 	if (!((utmp >> 1) & 0x01)) {
1024 		ret = -ETIMEDOUT;
1025 		goto err;
1026 	}
1027 
1028 	/* ADC off */
1029 	ret = regmap_update_bits(state->regmap, 0xd73a, 0x08, 0x08);
1030 	if (ret)
1031 		goto err;
1032 
1033 	return 0;
1034 err:
1035 	dev_dbg(&client->dev, "failed %d\n", ret);
1036 	return ret;
1037 }
1038 
1039 static int af9013_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1040 {
1041 	int ret;
1042 	struct af9013_state *state = fe->demodulator_priv;
1043 	struct i2c_client *client = state->client;
1044 
1045 	dev_dbg(&client->dev, "enable %d\n", enable);
1046 
1047 	/* gate already open or close */
1048 	if (state->i2c_gate_state == enable)
1049 		return 0;
1050 
1051 	if (state->ts_mode == AF9013_TS_MODE_USB)
1052 		ret = regmap_update_bits(state->regmap, 0xd417, 0x08,
1053 					 enable << 3);
1054 	else
1055 		ret = regmap_update_bits(state->regmap, 0xd607, 0x04,
1056 					 enable << 2);
1057 	if (ret)
1058 		goto err;
1059 
1060 	state->i2c_gate_state = enable;
1061 
1062 	return 0;
1063 err:
1064 	dev_dbg(&client->dev, "failed %d\n", ret);
1065 	return ret;
1066 }
1067 
1068 static void af9013_release(struct dvb_frontend *fe)
1069 {
1070 	struct af9013_state *state = fe->demodulator_priv;
1071 	struct i2c_client *client = state->client;
1072 
1073 	dev_dbg(&client->dev, "\n");
1074 
1075 	i2c_unregister_device(client);
1076 }
1077 
1078 static const struct dvb_frontend_ops af9013_ops;
1079 
1080 static int af9013_download_firmware(struct af9013_state *state)
1081 {
1082 	struct i2c_client *client = state->client;
1083 	int ret, i, len, rem;
1084 	unsigned int utmp;
1085 	u8 buf[4];
1086 	u16 checksum = 0;
1087 	const struct firmware *firmware;
1088 	const char *name = AF9013_FIRMWARE;
1089 
1090 	dev_dbg(&client->dev, "\n");
1091 
1092 	/* Check whether firmware is already running */
1093 	ret = regmap_read(state->regmap, 0x98be, &utmp);
1094 	if (ret)
1095 		goto err;
1096 
1097 	dev_dbg(&client->dev, "firmware status %02x\n", utmp);
1098 
1099 	if (utmp == 0x0c)
1100 		return 0;
1101 
1102 	dev_info(&client->dev, "found a '%s' in cold state, will try to load a firmware\n",
1103 		 af9013_ops.info.name);
1104 
1105 	/* Request the firmware, will block and timeout */
1106 	ret = request_firmware(&firmware, name, &client->dev);
1107 	if (ret) {
1108 		dev_info(&client->dev, "firmware file '%s' not found %d\n",
1109 			 name, ret);
1110 		goto err;
1111 	}
1112 
1113 	dev_info(&client->dev, "downloading firmware from file '%s'\n",
1114 		 name);
1115 
1116 	/* Write firmware checksum & size */
1117 	for (i = 0; i < firmware->size; i++)
1118 		checksum += firmware->data[i];
1119 
1120 	buf[0] = (checksum >> 8) & 0xff;
1121 	buf[1] = (checksum >> 0) & 0xff;
1122 	buf[2] = (firmware->size >> 8) & 0xff;
1123 	buf[3] = (firmware->size >> 0) & 0xff;
1124 	ret = regmap_bulk_write(state->regmap, 0x50fc, buf, 4);
1125 	if (ret)
1126 		goto err_release_firmware;
1127 
1128 	/* Download firmware */
1129 	#define LEN_MAX 16
1130 	for (rem = firmware->size; rem > 0; rem -= LEN_MAX) {
1131 		len = min(LEN_MAX, rem);
1132 		ret = regmap_bulk_write(state->regmap,
1133 					0x5100 + firmware->size - rem,
1134 					&firmware->data[firmware->size - rem],
1135 					len);
1136 		if (ret) {
1137 			dev_err(&client->dev, "firmware download failed %d\n",
1138 				ret);
1139 			goto err_release_firmware;
1140 		}
1141 	}
1142 
1143 	release_firmware(firmware);
1144 
1145 	/* Boot firmware */
1146 	ret = regmap_write(state->regmap, 0xe205, 0x01);
1147 	if (ret)
1148 		goto err;
1149 
1150 	/* Check firmware status. 0c=OK, 04=fail */
1151 	ret = regmap_read_poll_timeout(state->regmap, 0x98be, utmp,
1152 				       (utmp == 0x0c || utmp == 0x04),
1153 				       5000, 1000000);
1154 	if (ret)
1155 		goto err;
1156 
1157 	dev_dbg(&client->dev, "firmware status %02x\n", utmp);
1158 
1159 	if (utmp == 0x04) {
1160 		ret = -ENODEV;
1161 		dev_err(&client->dev, "firmware did not run\n");
1162 		goto err;
1163 	} else if (utmp != 0x0c) {
1164 		ret = -ENODEV;
1165 		dev_err(&client->dev, "firmware boot timeout\n");
1166 		goto err;
1167 	}
1168 
1169 	dev_info(&client->dev, "found a '%s' in warm state\n",
1170 		 af9013_ops.info.name);
1171 
1172 	return 0;
1173 err_release_firmware:
1174 	release_firmware(firmware);
1175 err:
1176 	dev_dbg(&client->dev, "failed %d\n", ret);
1177 	return ret;
1178 }
1179 
1180 /*
1181  * XXX: That is wrapper to af9013_probe() via driver core in order to provide
1182  * proper I2C client for legacy media attach binding.
1183  * New users must use I2C client binding directly!
1184  */
1185 struct dvb_frontend *af9013_attach(const struct af9013_config *config,
1186 				   struct i2c_adapter *i2c)
1187 {
1188 	struct i2c_client *client;
1189 	struct i2c_board_info board_info;
1190 	struct af9013_platform_data pdata;
1191 
1192 	pdata.clk = config->clock;
1193 	pdata.tuner = config->tuner;
1194 	pdata.if_frequency = config->if_frequency;
1195 	pdata.ts_mode = config->ts_mode;
1196 	pdata.ts_output_pin = 7;
1197 	pdata.spec_inv = config->spec_inv;
1198 	memcpy(&pdata.api_version, config->api_version, sizeof(pdata.api_version));
1199 	memcpy(&pdata.gpio, config->gpio, sizeof(pdata.gpio));
1200 	pdata.attach_in_use = true;
1201 
1202 	memset(&board_info, 0, sizeof(board_info));
1203 	strlcpy(board_info.type, "af9013", sizeof(board_info.type));
1204 	board_info.addr = config->i2c_addr;
1205 	board_info.platform_data = &pdata;
1206 	client = i2c_new_device(i2c, &board_info);
1207 	if (!client || !client->dev.driver)
1208 		return NULL;
1209 
1210 	return pdata.get_dvb_frontend(client);
1211 }
1212 EXPORT_SYMBOL(af9013_attach);
1213 
1214 static const struct dvb_frontend_ops af9013_ops = {
1215 	.delsys = { SYS_DVBT },
1216 	.info = {
1217 		.name = "Afatech AF9013",
1218 		.frequency_min = 174000000,
1219 		.frequency_max = 862000000,
1220 		.frequency_stepsize = 250000,
1221 		.frequency_tolerance = 0,
1222 		.caps =	FE_CAN_FEC_1_2 |
1223 			FE_CAN_FEC_2_3 |
1224 			FE_CAN_FEC_3_4 |
1225 			FE_CAN_FEC_5_6 |
1226 			FE_CAN_FEC_7_8 |
1227 			FE_CAN_FEC_AUTO |
1228 			FE_CAN_QPSK |
1229 			FE_CAN_QAM_16 |
1230 			FE_CAN_QAM_64 |
1231 			FE_CAN_QAM_AUTO |
1232 			FE_CAN_TRANSMISSION_MODE_AUTO |
1233 			FE_CAN_GUARD_INTERVAL_AUTO |
1234 			FE_CAN_HIERARCHY_AUTO |
1235 			FE_CAN_RECOVER |
1236 			FE_CAN_MUTE_TS
1237 	},
1238 
1239 	.release = af9013_release,
1240 
1241 	.init = af9013_init,
1242 	.sleep = af9013_sleep,
1243 
1244 	.get_tune_settings = af9013_get_tune_settings,
1245 	.set_frontend = af9013_set_frontend,
1246 	.get_frontend = af9013_get_frontend,
1247 
1248 	.read_status = af9013_read_status,
1249 	.read_snr = af9013_read_snr,
1250 	.read_signal_strength = af9013_read_signal_strength,
1251 	.read_ber = af9013_read_ber,
1252 	.read_ucblocks = af9013_read_ucblocks,
1253 
1254 	.i2c_gate_ctrl = af9013_i2c_gate_ctrl,
1255 };
1256 
1257 static struct dvb_frontend *af9013_get_dvb_frontend(struct i2c_client *client)
1258 {
1259 	struct af9013_state *state = i2c_get_clientdata(client);
1260 
1261 	dev_dbg(&client->dev, "\n");
1262 
1263 	return &state->fe;
1264 }
1265 
1266 /* Own I2C access routines needed for regmap as chip uses extra command byte */
1267 static int af9013_wregs(struct i2c_client *client, u8 cmd, u16 reg,
1268 			const u8 *val, int len)
1269 {
1270 	int ret;
1271 	u8 buf[21];
1272 	struct i2c_msg msg[1] = {
1273 		{
1274 			.addr = client->addr,
1275 			.flags = 0,
1276 			.len = 3 + len,
1277 			.buf = buf,
1278 		}
1279 	};
1280 
1281 	if (3 + len > sizeof(buf)) {
1282 		ret = -EINVAL;
1283 		goto err;
1284 	}
1285 
1286 	buf[0] = (reg >> 8) & 0xff;
1287 	buf[1] = (reg >> 0) & 0xff;
1288 	buf[2] = cmd;
1289 	memcpy(&buf[3], val, len);
1290 	ret = i2c_transfer(client->adapter, msg, 1);
1291 	if (ret < 0) {
1292 		goto err;
1293 	} else if (ret != 1) {
1294 		ret = -EREMOTEIO;
1295 		goto err;
1296 	}
1297 
1298 	return 0;
1299 err:
1300 	dev_dbg(&client->dev, "failed %d\n", ret);
1301 	return ret;
1302 }
1303 
1304 static int af9013_rregs(struct i2c_client *client, u8 cmd, u16 reg,
1305 			u8 *val, int len)
1306 {
1307 	int ret;
1308 	u8 buf[3];
1309 	struct i2c_msg msg[2] = {
1310 		{
1311 			.addr = client->addr,
1312 			.flags = 0,
1313 			.len = 3,
1314 			.buf = buf,
1315 		}, {
1316 			.addr = client->addr,
1317 			.flags = I2C_M_RD,
1318 			.len = len,
1319 			.buf = val,
1320 		}
1321 	};
1322 
1323 	buf[0] = (reg >> 8) & 0xff;
1324 	buf[1] = (reg >> 0) & 0xff;
1325 	buf[2] = cmd;
1326 	ret = i2c_transfer(client->adapter, msg, 2);
1327 	if (ret < 0) {
1328 		goto err;
1329 	} else if (ret != 2) {
1330 		ret = -EREMOTEIO;
1331 		goto err;
1332 	}
1333 
1334 	return 0;
1335 err:
1336 	dev_dbg(&client->dev, "failed %d\n", ret);
1337 	return ret;
1338 }
1339 
1340 static int af9013_regmap_write(void *context, const void *data, size_t count)
1341 {
1342 	struct i2c_client *client = context;
1343 	struct af9013_state *state = i2c_get_clientdata(client);
1344 	int ret, i;
1345 	u8 cmd;
1346 	u16 reg = ((u8 *)data)[0] << 8|((u8 *)data)[1] << 0;
1347 	u8 *val = &((u8 *)data)[2];
1348 	const unsigned int len = count - 2;
1349 
1350 	if (state->ts_mode == AF9013_TS_MODE_USB && (reg & 0xff00) != 0xae00) {
1351 		cmd = 0 << 7|0 << 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 if (reg >= 0x5100 && reg < 0x8fff) {
1356 		/* Firmware download */
1357 		cmd = 1 << 7|1 << 6|(len - 1) << 2|1 << 1|1 << 0;
1358 		ret = af9013_wregs(client, cmd, reg, val, len);
1359 		if (ret)
1360 			goto err;
1361 	} else {
1362 		cmd = 0 << 7|0 << 6|(1 - 1) << 2|1 << 1|1 << 0;
1363 		for (i = 0; i < len; i++) {
1364 			ret = af9013_wregs(client, cmd, reg + i, val + i, 1);
1365 			if (ret)
1366 				goto err;
1367 		}
1368 	}
1369 
1370 	return 0;
1371 err:
1372 	dev_dbg(&client->dev, "failed %d\n", ret);
1373 	return ret;
1374 }
1375 
1376 static int af9013_regmap_read(void *context, const void *reg_buf,
1377 			      size_t reg_size, void *val_buf, size_t val_size)
1378 {
1379 	struct i2c_client *client = context;
1380 	struct af9013_state *state = i2c_get_clientdata(client);
1381 	int ret, i;
1382 	u8 cmd;
1383 	u16 reg = ((u8 *)reg_buf)[0] << 8|((u8 *)reg_buf)[1] << 0;
1384 	u8 *val = &((u8 *)val_buf)[0];
1385 	const unsigned int len = val_size;
1386 
1387 	if (state->ts_mode == AF9013_TS_MODE_USB && (reg & 0xff00) != 0xae00) {
1388 		cmd = 0 << 7|0 << 6|(len - 1) << 2|1 << 1|0 << 0;
1389 		ret = af9013_rregs(client, cmd, reg, val_buf, len);
1390 		if (ret)
1391 			goto err;
1392 	} else {
1393 		cmd = 0 << 7|0 << 6|(1 - 1) << 2|1 << 1|0 << 0;
1394 		for (i = 0; i < len; i++) {
1395 			ret = af9013_rregs(client, cmd, reg + i, val + i, 1);
1396 			if (ret)
1397 				goto err;
1398 		}
1399 	}
1400 
1401 	return 0;
1402 err:
1403 	dev_dbg(&client->dev, "failed %d\n", ret);
1404 	return ret;
1405 }
1406 
1407 static int af9013_probe(struct i2c_client *client,
1408 			const struct i2c_device_id *id)
1409 {
1410 	struct af9013_state *state;
1411 	struct af9013_platform_data *pdata = client->dev.platform_data;
1412 	struct dtv_frontend_properties *c;
1413 	int ret, i;
1414 	u8 firmware_version[4];
1415 	static const struct regmap_bus regmap_bus = {
1416 		.read = af9013_regmap_read,
1417 		.write = af9013_regmap_write,
1418 	};
1419 	static const struct regmap_config regmap_config = {
1420 		.reg_bits    =  16,
1421 		.val_bits    =  8,
1422 	};
1423 
1424 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1425 	if (!state) {
1426 		ret = -ENOMEM;
1427 		goto err;
1428 	}
1429 
1430 	/* Setup the state */
1431 	state->client = client;
1432 	i2c_set_clientdata(client, state);
1433 	state->clk = pdata->clk;
1434 	state->tuner = pdata->tuner;
1435 	state->if_frequency = pdata->if_frequency;
1436 	state->ts_mode = pdata->ts_mode;
1437 	state->ts_output_pin = pdata->ts_output_pin;
1438 	state->spec_inv = pdata->spec_inv;
1439 	memcpy(&state->api_version, pdata->api_version, sizeof(state->api_version));
1440 	memcpy(&state->gpio, pdata->gpio, sizeof(state->gpio));
1441 	state->regmap = regmap_init(&client->dev, &regmap_bus, client,
1442 				  &regmap_config);
1443 	if (IS_ERR(state->regmap)) {
1444 		ret = PTR_ERR(state->regmap);
1445 		goto err_kfree;
1446 	}
1447 
1448 	/* Download firmware */
1449 	if (state->ts_mode != AF9013_TS_MODE_USB) {
1450 		ret = af9013_download_firmware(state);
1451 		if (ret)
1452 			goto err_regmap_exit;
1453 	}
1454 
1455 	/* Firmware version */
1456 	ret = regmap_bulk_read(state->regmap, 0x5103, firmware_version,
1457 			       sizeof(firmware_version));
1458 	if (ret)
1459 		goto err_regmap_exit;
1460 
1461 	/* Set GPIOs */
1462 	for (i = 0; i < sizeof(state->gpio); i++) {
1463 		ret = af9013_set_gpio(state, i, state->gpio[i]);
1464 		if (ret)
1465 			goto err_regmap_exit;
1466 	}
1467 
1468 	/* Create dvb frontend */
1469 	memcpy(&state->fe.ops, &af9013_ops, sizeof(state->fe.ops));
1470 	if (!pdata->attach_in_use)
1471 		state->fe.ops.release = NULL;
1472 	state->fe.demodulator_priv = state;
1473 
1474 	/* Setup callbacks */
1475 	pdata->get_dvb_frontend = af9013_get_dvb_frontend;
1476 
1477 	/* Init stats to indicate which stats are supported */
1478 	c = &state->fe.dtv_property_cache;
1479 	c->strength.len = 1;
1480 	c->cnr.len = 1;
1481 	c->post_bit_error.len = 1;
1482 	c->post_bit_count.len = 1;
1483 	c->block_error.len = 1;
1484 	c->block_count.len = 1;
1485 
1486 	dev_info(&client->dev, "Afatech AF9013 successfully attached\n");
1487 	dev_info(&client->dev, "firmware version: %d.%d.%d.%d\n",
1488 		 firmware_version[0], firmware_version[1],
1489 		 firmware_version[2], firmware_version[3]);
1490 	return 0;
1491 err_regmap_exit:
1492 	regmap_exit(state->regmap);
1493 err_kfree:
1494 	kfree(state);
1495 err:
1496 	dev_dbg(&client->dev, "failed %d\n", ret);
1497 	return ret;
1498 }
1499 
1500 static int af9013_remove(struct i2c_client *client)
1501 {
1502 	struct af9013_state *state = i2c_get_clientdata(client);
1503 
1504 	dev_dbg(&client->dev, "\n");
1505 
1506 	regmap_exit(state->regmap);
1507 
1508 	kfree(state);
1509 
1510 	return 0;
1511 }
1512 
1513 static const struct i2c_device_id af9013_id_table[] = {
1514 	{"af9013", 0},
1515 	{}
1516 };
1517 MODULE_DEVICE_TABLE(i2c, af9013_id_table);
1518 
1519 static struct i2c_driver af9013_driver = {
1520 	.driver = {
1521 		.name	= "af9013",
1522 		.suppress_bind_attrs = true,
1523 	},
1524 	.probe		= af9013_probe,
1525 	.remove		= af9013_remove,
1526 	.id_table	= af9013_id_table,
1527 };
1528 
1529 module_i2c_driver(af9013_driver);
1530 
1531 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1532 MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver");
1533 MODULE_LICENSE("GPL");
1534 MODULE_FIRMWARE(AF9013_FIRMWARE);
1535