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