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