1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3     Driver for ST STV0299 demodulator
4 
5     Copyright (C) 2001-2002 Convergence Integrated Media GmbH
6 	<ralph@convergence.de>,
7 	<holger@convergence.de>,
8 	<js@convergence.de>
9 
10 
11     Philips SU1278/SH
12 
13     Copyright (C) 2002 by Peter Schildmann <peter.schildmann@web.de>
14 
15 
16     LG TDQF-S001F
17 
18     Copyright (C) 2002 Felix Domke <tmbinc@elitedvb.net>
19 		     & Andreas Oberritter <obi@linuxtv.org>
20 
21 
22     Support for Samsung TBMU24112IMB used on Technisat SkyStar2 rev. 2.6B
23 
24     Copyright (C) 2003 Vadim Catana <skystar@moldova.cc>:
25 
26     Support for Philips SU1278 on Technotrend hardware
27 
28     Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net>
29 
30 
31 */
32 
33 #include <linux/init.h>
34 #include <linux/kernel.h>
35 #include <linux/ktime.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/slab.h>
39 #include <linux/jiffies.h>
40 #include <asm/div64.h>
41 
42 #include <media/dvb_frontend.h>
43 #include "stv0299.h"
44 
45 struct stv0299_state {
46 	struct i2c_adapter* i2c;
47 	const struct stv0299_config* config;
48 	struct dvb_frontend frontend;
49 
50 	u8 initialised:1;
51 	u32 tuner_frequency;
52 	u32 symbol_rate;
53 	enum fe_code_rate fec_inner;
54 	int errmode;
55 	u32 ucblocks;
56 	u8 mcr_reg;
57 };
58 
59 #define STATUS_BER 0
60 #define STATUS_UCBLOCKS 1
61 
62 static int debug;
63 static int debug_legacy_dish_switch;
64 #define dprintk(args...) \
65 	do { \
66 		if (debug) printk(KERN_DEBUG "stv0299: " args); \
67 	} while (0)
68 
69 
stv0299_writeregI(struct stv0299_state * state,u8 reg,u8 data)70 static int stv0299_writeregI (struct stv0299_state* state, u8 reg, u8 data)
71 {
72 	int ret;
73 	u8 buf [] = { reg, data };
74 	struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
75 
76 	ret = i2c_transfer (state->i2c, &msg, 1);
77 
78 	if (ret != 1)
79 		dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
80 			__func__, reg, data, ret);
81 
82 	return (ret != 1) ? -EREMOTEIO : 0;
83 }
84 
stv0299_write(struct dvb_frontend * fe,const u8 buf[],int len)85 static int stv0299_write(struct dvb_frontend* fe, const u8 buf[], int len)
86 {
87 	struct stv0299_state* state = fe->demodulator_priv;
88 
89 	if (len != 2)
90 		return -EINVAL;
91 
92 	return stv0299_writeregI(state, buf[0], buf[1]);
93 }
94 
stv0299_readreg(struct stv0299_state * state,u8 reg)95 static u8 stv0299_readreg (struct stv0299_state* state, u8 reg)
96 {
97 	int ret;
98 	u8 b0 [] = { reg };
99 	u8 b1 [] = { 0 };
100 	struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
101 			   { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
102 
103 	ret = i2c_transfer (state->i2c, msg, 2);
104 
105 	if (ret != 2)
106 		dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
107 				__func__, reg, ret);
108 
109 	return b1[0];
110 }
111 
stv0299_readregs(struct stv0299_state * state,u8 reg1,u8 * b,u8 len)112 static int stv0299_readregs (struct stv0299_state* state, u8 reg1, u8 *b, u8 len)
113 {
114 	int ret;
115 	struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = &reg1, .len = 1 },
116 			   { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
117 
118 	ret = i2c_transfer (state->i2c, msg, 2);
119 
120 	if (ret != 2)
121 		dprintk("%s: readreg error (ret == %i)\n", __func__, ret);
122 
123 	return ret == 2 ? 0 : ret;
124 }
125 
stv0299_set_FEC(struct stv0299_state * state,enum fe_code_rate fec)126 static int stv0299_set_FEC(struct stv0299_state *state, enum fe_code_rate fec)
127 {
128 	dprintk ("%s\n", __func__);
129 
130 	switch (fec) {
131 	case FEC_AUTO:
132 	{
133 		return stv0299_writeregI (state, 0x31, 0x1f);
134 	}
135 	case FEC_1_2:
136 	{
137 		return stv0299_writeregI (state, 0x31, 0x01);
138 	}
139 	case FEC_2_3:
140 	{
141 		return stv0299_writeregI (state, 0x31, 0x02);
142 	}
143 	case FEC_3_4:
144 	{
145 		return stv0299_writeregI (state, 0x31, 0x04);
146 	}
147 	case FEC_5_6:
148 	{
149 		return stv0299_writeregI (state, 0x31, 0x08);
150 	}
151 	case FEC_7_8:
152 	{
153 		return stv0299_writeregI (state, 0x31, 0x10);
154 	}
155 	default:
156 	{
157 		return -EINVAL;
158 	}
159     }
160 }
161 
stv0299_get_fec(struct stv0299_state * state)162 static enum fe_code_rate stv0299_get_fec(struct stv0299_state *state)
163 {
164 	static const enum fe_code_rate fec_tab[] = {
165 		FEC_2_3, FEC_3_4, FEC_5_6, FEC_7_8, FEC_1_2
166 	};
167 	u8 index;
168 
169 	dprintk ("%s\n", __func__);
170 
171 	index = stv0299_readreg (state, 0x1b);
172 	index &= 0x7;
173 
174 	if (index > 4)
175 		return FEC_AUTO;
176 
177 	return fec_tab [index];
178 }
179 
stv0299_wait_diseqc_fifo(struct stv0299_state * state,int timeout)180 static int stv0299_wait_diseqc_fifo (struct stv0299_state* state, int timeout)
181 {
182 	unsigned long start = jiffies;
183 
184 	dprintk ("%s\n", __func__);
185 
186 	while (stv0299_readreg(state, 0x0a) & 1) {
187 		if (time_is_before_jiffies(start + timeout)) {
188 			dprintk ("%s: timeout!!\n", __func__);
189 			return -ETIMEDOUT;
190 		}
191 		msleep(10);
192 	}
193 
194 	return 0;
195 }
196 
stv0299_wait_diseqc_idle(struct stv0299_state * state,int timeout)197 static int stv0299_wait_diseqc_idle (struct stv0299_state* state, int timeout)
198 {
199 	unsigned long start = jiffies;
200 
201 	dprintk ("%s\n", __func__);
202 
203 	while ((stv0299_readreg(state, 0x0a) & 3) != 2 ) {
204 		if (time_is_before_jiffies(start + timeout)) {
205 			dprintk ("%s: timeout!!\n", __func__);
206 			return -ETIMEDOUT;
207 		}
208 		msleep(10);
209 	}
210 
211 	return 0;
212 }
213 
stv0299_set_symbolrate(struct dvb_frontend * fe,u32 srate)214 static int stv0299_set_symbolrate (struct dvb_frontend* fe, u32 srate)
215 {
216 	struct stv0299_state* state = fe->demodulator_priv;
217 	u64 big = srate;
218 	u32 ratio;
219 
220 	// check rate is within limits
221 	if ((srate < 1000000) || (srate > 45000000)) return -EINVAL;
222 
223 	// calculate value to program
224 	big = big << 20;
225 	big += (state->config->mclk-1); // round correctly
226 	do_div(big, state->config->mclk);
227 	ratio = big << 4;
228 
229 	return state->config->set_symbol_rate(fe, srate, ratio);
230 }
231 
stv0299_get_symbolrate(struct stv0299_state * state)232 static int stv0299_get_symbolrate (struct stv0299_state* state)
233 {
234 	u32 Mclk = state->config->mclk / 4096L;
235 	u32 srate;
236 	s32 offset;
237 	u8 sfr[3];
238 	s8 rtf;
239 
240 	dprintk ("%s\n", __func__);
241 
242 	stv0299_readregs (state, 0x1f, sfr, 3);
243 	stv0299_readregs (state, 0x1a, (u8 *)&rtf, 1);
244 
245 	srate = (sfr[0] << 8) | sfr[1];
246 	srate *= Mclk;
247 	srate /= 16;
248 	srate += (sfr[2] >> 4) * Mclk / 256;
249 	offset = (s32) rtf * (srate / 4096L);
250 	offset /= 128;
251 
252 	dprintk ("%s : srate = %i\n", __func__, srate);
253 	dprintk ("%s : ofset = %i\n", __func__, offset);
254 
255 	srate += offset;
256 
257 	srate += 1000;
258 	srate /= 2000;
259 	srate *= 2000;
260 
261 	return srate;
262 }
263 
stv0299_send_diseqc_msg(struct dvb_frontend * fe,struct dvb_diseqc_master_cmd * m)264 static int stv0299_send_diseqc_msg (struct dvb_frontend* fe,
265 				    struct dvb_diseqc_master_cmd *m)
266 {
267 	struct stv0299_state* state = fe->demodulator_priv;
268 	u8 val;
269 	int i;
270 
271 	dprintk ("%s\n", __func__);
272 
273 	if (stv0299_wait_diseqc_idle (state, 100) < 0)
274 		return -ETIMEDOUT;
275 
276 	val = stv0299_readreg (state, 0x08);
277 
278 	if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x6))  /* DiSEqC mode */
279 		return -EREMOTEIO;
280 
281 	for (i=0; i<m->msg_len; i++) {
282 		if (stv0299_wait_diseqc_fifo (state, 100) < 0)
283 			return -ETIMEDOUT;
284 
285 		if (stv0299_writeregI (state, 0x09, m->msg[i]))
286 			return -EREMOTEIO;
287 	}
288 
289 	if (stv0299_wait_diseqc_idle (state, 100) < 0)
290 		return -ETIMEDOUT;
291 
292 	return 0;
293 }
294 
stv0299_send_diseqc_burst(struct dvb_frontend * fe,enum fe_sec_mini_cmd burst)295 static int stv0299_send_diseqc_burst(struct dvb_frontend *fe,
296 				     enum fe_sec_mini_cmd burst)
297 {
298 	struct stv0299_state* state = fe->demodulator_priv;
299 	u8 val;
300 
301 	dprintk ("%s\n", __func__);
302 
303 	if (stv0299_wait_diseqc_idle (state, 100) < 0)
304 		return -ETIMEDOUT;
305 
306 	val = stv0299_readreg (state, 0x08);
307 
308 	if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x2))	/* burst mode */
309 		return -EREMOTEIO;
310 
311 	if (stv0299_writeregI (state, 0x09, burst == SEC_MINI_A ? 0x00 : 0xff))
312 		return -EREMOTEIO;
313 
314 	if (stv0299_wait_diseqc_idle (state, 100) < 0)
315 		return -ETIMEDOUT;
316 
317 	if (stv0299_writeregI (state, 0x08, val))
318 		return -EREMOTEIO;
319 
320 	return 0;
321 }
322 
stv0299_set_tone(struct dvb_frontend * fe,enum fe_sec_tone_mode tone)323 static int stv0299_set_tone(struct dvb_frontend *fe,
324 			    enum fe_sec_tone_mode tone)
325 {
326 	struct stv0299_state* state = fe->demodulator_priv;
327 	u8 val;
328 
329 	if (stv0299_wait_diseqc_idle (state, 100) < 0)
330 		return -ETIMEDOUT;
331 
332 	val = stv0299_readreg (state, 0x08);
333 
334 	switch (tone) {
335 	case SEC_TONE_ON:
336 		return stv0299_writeregI (state, 0x08, val | 0x3);
337 
338 	case SEC_TONE_OFF:
339 		return stv0299_writeregI (state, 0x08, (val & ~0x3) | 0x02);
340 
341 	default:
342 		return -EINVAL;
343 	}
344 }
345 
stv0299_set_voltage(struct dvb_frontend * fe,enum fe_sec_voltage voltage)346 static int stv0299_set_voltage(struct dvb_frontend *fe,
347 			       enum fe_sec_voltage voltage)
348 {
349 	struct stv0299_state* state = fe->demodulator_priv;
350 	u8 reg0x08;
351 	u8 reg0x0c;
352 
353 	dprintk("%s: %s\n", __func__,
354 		voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
355 		voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
356 
357 	reg0x08 = stv0299_readreg (state, 0x08);
358 	reg0x0c = stv0299_readreg (state, 0x0c);
359 
360 	/*
361 	 *  H/V switching over OP0, OP1 and OP2 are LNB power enable bits
362 	 */
363 	reg0x0c &= 0x0f;
364 	reg0x08 = (reg0x08 & 0x3f) | (state->config->lock_output << 6);
365 
366 	switch (voltage) {
367 	case SEC_VOLTAGE_13:
368 		if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
369 			reg0x0c |= 0x10; /* OP1 off, OP0 on */
370 		else
371 			reg0x0c |= 0x40; /* OP1 on, OP0 off */
372 		break;
373 	case SEC_VOLTAGE_18:
374 		reg0x0c |= 0x50; /* OP1 on, OP0 on */
375 		break;
376 	case SEC_VOLTAGE_OFF:
377 		/* LNB power off! */
378 		reg0x08 = 0x00;
379 		reg0x0c = 0x00;
380 		break;
381 	default:
382 		return -EINVAL;
383 	}
384 
385 	if (state->config->op0_off)
386 		reg0x0c &= ~0x10;
387 
388 	stv0299_writeregI(state, 0x08, reg0x08);
389 	return stv0299_writeregI(state, 0x0c, reg0x0c);
390 }
391 
stv0299_send_legacy_dish_cmd(struct dvb_frontend * fe,unsigned long cmd)392 static int stv0299_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long cmd)
393 {
394 	struct stv0299_state* state = fe->demodulator_priv;
395 	u8 reg0x08;
396 	u8 reg0x0c;
397 	u8 lv_mask = 0x40;
398 	u8 last = 1;
399 	int i;
400 	ktime_t nexttime;
401 	ktime_t tv[10];
402 
403 	reg0x08 = stv0299_readreg (state, 0x08);
404 	reg0x0c = stv0299_readreg (state, 0x0c);
405 	reg0x0c &= 0x0f;
406 	stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6));
407 	if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
408 		lv_mask = 0x10;
409 
410 	cmd = cmd << 1;
411 	if (debug_legacy_dish_switch)
412 		printk ("%s switch command: 0x%04lx\n",__func__, cmd);
413 
414 	nexttime = ktime_get_boottime();
415 	if (debug_legacy_dish_switch)
416 		tv[0] = nexttime;
417 	stv0299_writeregI (state, 0x0c, reg0x0c | 0x50); /* set LNB to 18V */
418 
419 	dvb_frontend_sleep_until(&nexttime, 32000);
420 
421 	for (i=0; i<9; i++) {
422 		if (debug_legacy_dish_switch)
423 			tv[i+1] = ktime_get_boottime();
424 		if((cmd & 0x01) != last) {
425 			/* set voltage to (last ? 13V : 18V) */
426 			stv0299_writeregI (state, 0x0c, reg0x0c | (last ? lv_mask : 0x50));
427 			last = (last) ? 0 : 1;
428 		}
429 
430 		cmd = cmd >> 1;
431 
432 		if (i != 8)
433 			dvb_frontend_sleep_until(&nexttime, 8000);
434 	}
435 	if (debug_legacy_dish_switch) {
436 		printk ("%s(%d): switch delay (should be 32k followed by all 8k\n",
437 			__func__, fe->dvb->num);
438 		for (i = 1; i < 10; i++)
439 			printk("%d: %d\n", i,
440 			       (int) ktime_us_delta(tv[i], tv[i-1]));
441 	}
442 
443 	return 0;
444 }
445 
stv0299_init(struct dvb_frontend * fe)446 static int stv0299_init (struct dvb_frontend* fe)
447 {
448 	struct stv0299_state* state = fe->demodulator_priv;
449 	int i;
450 	u8 reg;
451 	u8 val;
452 
453 	dprintk("stv0299: init chip\n");
454 
455 	stv0299_writeregI(state, 0x02, 0x30 | state->mcr_reg);
456 	msleep(50);
457 
458 	for (i = 0; ; i += 2)  {
459 		reg = state->config->inittab[i];
460 		val = state->config->inittab[i+1];
461 		if (reg == 0xff && val == 0xff)
462 			break;
463 		if (reg == 0x0c && state->config->op0_off)
464 			val &= ~0x10;
465 		if (reg == 0x2)
466 			state->mcr_reg = val & 0xf;
467 		stv0299_writeregI(state, reg, val);
468 	}
469 
470 	return 0;
471 }
472 
stv0299_read_status(struct dvb_frontend * fe,enum fe_status * status)473 static int stv0299_read_status(struct dvb_frontend *fe,
474 			       enum fe_status *status)
475 {
476 	struct stv0299_state* state = fe->demodulator_priv;
477 
478 	u8 signal = 0xff - stv0299_readreg (state, 0x18);
479 	u8 sync = stv0299_readreg (state, 0x1b);
480 
481 	dprintk ("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__, sync);
482 	*status = 0;
483 
484 	if (signal > 10)
485 		*status |= FE_HAS_SIGNAL;
486 
487 	if (sync & 0x80)
488 		*status |= FE_HAS_CARRIER;
489 
490 	if (sync & 0x10)
491 		*status |= FE_HAS_VITERBI;
492 
493 	if (sync & 0x08)
494 		*status |= FE_HAS_SYNC;
495 
496 	if ((sync & 0x98) == 0x98)
497 		*status |= FE_HAS_LOCK;
498 
499 	return 0;
500 }
501 
stv0299_read_ber(struct dvb_frontend * fe,u32 * ber)502 static int stv0299_read_ber(struct dvb_frontend* fe, u32* ber)
503 {
504 	struct stv0299_state* state = fe->demodulator_priv;
505 
506 	if (state->errmode != STATUS_BER)
507 		return -ENOSYS;
508 
509 	*ber = stv0299_readreg(state, 0x1e) | (stv0299_readreg(state, 0x1d) << 8);
510 
511 	return 0;
512 }
513 
stv0299_read_signal_strength(struct dvb_frontend * fe,u16 * strength)514 static int stv0299_read_signal_strength(struct dvb_frontend* fe, u16* strength)
515 {
516 	struct stv0299_state* state = fe->demodulator_priv;
517 
518 	s32 signal =  0xffff - ((stv0299_readreg (state, 0x18) << 8)
519 			       | stv0299_readreg (state, 0x19));
520 
521 	dprintk ("%s : FE_READ_SIGNAL_STRENGTH : AGC2I: 0x%02x%02x, signal=0x%04x\n", __func__,
522 		 stv0299_readreg (state, 0x18),
523 		 stv0299_readreg (state, 0x19), (int) signal);
524 
525 	signal = signal * 5 / 4;
526 	*strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal;
527 
528 	return 0;
529 }
530 
stv0299_read_snr(struct dvb_frontend * fe,u16 * snr)531 static int stv0299_read_snr(struct dvb_frontend* fe, u16* snr)
532 {
533 	struct stv0299_state* state = fe->demodulator_priv;
534 
535 	s32 xsnr = 0xffff - ((stv0299_readreg (state, 0x24) << 8)
536 			   | stv0299_readreg (state, 0x25));
537 	xsnr = 3 * (xsnr - 0xa100);
538 	*snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
539 
540 	return 0;
541 }
542 
stv0299_read_ucblocks(struct dvb_frontend * fe,u32 * ucblocks)543 static int stv0299_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
544 {
545 	struct stv0299_state* state = fe->demodulator_priv;
546 
547 	if (state->errmode != STATUS_UCBLOCKS)
548 		return -ENOSYS;
549 
550 	state->ucblocks += stv0299_readreg(state, 0x1e);
551 	state->ucblocks += (stv0299_readreg(state, 0x1d) << 8);
552 	*ucblocks = state->ucblocks;
553 
554 	return 0;
555 }
556 
stv0299_set_frontend(struct dvb_frontend * fe)557 static int stv0299_set_frontend(struct dvb_frontend *fe)
558 {
559 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
560 	struct stv0299_state* state = fe->demodulator_priv;
561 	int invval = 0;
562 
563 	dprintk ("%s : FE_SET_FRONTEND\n", __func__);
564 	if (state->config->set_ts_params)
565 		state->config->set_ts_params(fe, 0);
566 
567 	// set the inversion
568 	if (p->inversion == INVERSION_OFF) invval = 0;
569 	else if (p->inversion == INVERSION_ON) invval = 1;
570 	else {
571 		printk("stv0299 does not support auto-inversion\n");
572 		return -EINVAL;
573 	}
574 	if (state->config->invert) invval = (~invval) & 1;
575 	stv0299_writeregI(state, 0x0c, (stv0299_readreg(state, 0x0c) & 0xfe) | invval);
576 
577 	if (fe->ops.tuner_ops.set_params) {
578 		fe->ops.tuner_ops.set_params(fe);
579 		if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
580 	}
581 
582 	stv0299_set_FEC(state, p->fec_inner);
583 	stv0299_set_symbolrate(fe, p->symbol_rate);
584 	stv0299_writeregI(state, 0x22, 0x00);
585 	stv0299_writeregI(state, 0x23, 0x00);
586 
587 	state->tuner_frequency = p->frequency;
588 	state->fec_inner = p->fec_inner;
589 	state->symbol_rate = p->symbol_rate;
590 
591 	return 0;
592 }
593 
stv0299_get_frontend(struct dvb_frontend * fe,struct dtv_frontend_properties * p)594 static int stv0299_get_frontend(struct dvb_frontend *fe,
595 				struct dtv_frontend_properties *p)
596 {
597 	struct stv0299_state* state = fe->demodulator_priv;
598 	s32 derot_freq;
599 	int invval;
600 
601 	derot_freq = (s32)(s16) ((stv0299_readreg (state, 0x22) << 8)
602 				| stv0299_readreg (state, 0x23));
603 
604 	derot_freq *= (state->config->mclk >> 16);
605 	derot_freq += 500;
606 	derot_freq /= 1000;
607 
608 	p->frequency += derot_freq;
609 
610 	invval = stv0299_readreg (state, 0x0c) & 1;
611 	if (state->config->invert) invval = (~invval) & 1;
612 	p->inversion = invval ? INVERSION_ON : INVERSION_OFF;
613 
614 	p->fec_inner = stv0299_get_fec(state);
615 	p->symbol_rate = stv0299_get_symbolrate(state);
616 
617 	return 0;
618 }
619 
stv0299_sleep(struct dvb_frontend * fe)620 static int stv0299_sleep(struct dvb_frontend* fe)
621 {
622 	struct stv0299_state* state = fe->demodulator_priv;
623 
624 	stv0299_writeregI(state, 0x02, 0xb0 | state->mcr_reg);
625 	state->initialised = 0;
626 
627 	return 0;
628 }
629 
stv0299_i2c_gate_ctrl(struct dvb_frontend * fe,int enable)630 static int stv0299_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
631 {
632 	struct stv0299_state* state = fe->demodulator_priv;
633 
634 	if (enable) {
635 		stv0299_writeregI(state, 0x05, 0xb5);
636 	} else {
637 		stv0299_writeregI(state, 0x05, 0x35);
638 	}
639 	udelay(1);
640 	return 0;
641 }
642 
stv0299_get_tune_settings(struct dvb_frontend * fe,struct dvb_frontend_tune_settings * fesettings)643 static int stv0299_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
644 {
645 	struct stv0299_state* state = fe->demodulator_priv;
646 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
647 
648 	fesettings->min_delay_ms = state->config->min_delay_ms;
649 	if (p->symbol_rate < 10000000) {
650 		fesettings->step_size = p->symbol_rate / 32000;
651 		fesettings->max_drift = 5000;
652 	} else {
653 		fesettings->step_size = p->symbol_rate / 16000;
654 		fesettings->max_drift = p->symbol_rate / 2000;
655 	}
656 	return 0;
657 }
658 
stv0299_release(struct dvb_frontend * fe)659 static void stv0299_release(struct dvb_frontend* fe)
660 {
661 	struct stv0299_state* state = fe->demodulator_priv;
662 	kfree(state);
663 }
664 
665 static const struct dvb_frontend_ops stv0299_ops;
666 
stv0299_attach(const struct stv0299_config * config,struct i2c_adapter * i2c)667 struct dvb_frontend* stv0299_attach(const struct stv0299_config* config,
668 				    struct i2c_adapter* i2c)
669 {
670 	struct stv0299_state* state = NULL;
671 	int id;
672 
673 	/* allocate memory for the internal state */
674 	state = kzalloc(sizeof(struct stv0299_state), GFP_KERNEL);
675 	if (state == NULL) goto error;
676 
677 	/* setup the state */
678 	state->config = config;
679 	state->i2c = i2c;
680 	state->initialised = 0;
681 	state->tuner_frequency = 0;
682 	state->symbol_rate = 0;
683 	state->fec_inner = 0;
684 	state->errmode = STATUS_BER;
685 
686 	/* check if the demod is there */
687 	stv0299_writeregI(state, 0x02, 0x30); /* standby off */
688 	msleep(200);
689 	id = stv0299_readreg(state, 0x00);
690 
691 	/* register 0x00 contains 0xa1 for STV0299 and STV0299B */
692 	/* register 0x00 might contain 0x80 when returning from standby */
693 	if (id != 0xa1 && id != 0x80) goto error;
694 
695 	/* create dvb_frontend */
696 	memcpy(&state->frontend.ops, &stv0299_ops, sizeof(struct dvb_frontend_ops));
697 	state->frontend.demodulator_priv = state;
698 	return &state->frontend;
699 
700 error:
701 	kfree(state);
702 	return NULL;
703 }
704 
705 static const struct dvb_frontend_ops stv0299_ops = {
706 	.delsys = { SYS_DVBS },
707 	.info = {
708 		.name			= "ST STV0299 DVB-S",
709 		.frequency_min_hz	=  950 * MHz,
710 		.frequency_max_hz	= 2150 * MHz,
711 		.frequency_stepsize_hz	=  125 * kHz,
712 		.symbol_rate_min	= 1000000,
713 		.symbol_rate_max	= 45000000,
714 		.symbol_rate_tolerance	= 500,	/* ppm */
715 		.caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
716 		      FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
717 		      FE_CAN_QPSK |
718 		      FE_CAN_FEC_AUTO
719 	},
720 
721 	.release = stv0299_release,
722 
723 	.init = stv0299_init,
724 	.sleep = stv0299_sleep,
725 	.write = stv0299_write,
726 	.i2c_gate_ctrl = stv0299_i2c_gate_ctrl,
727 
728 	.set_frontend = stv0299_set_frontend,
729 	.get_frontend = stv0299_get_frontend,
730 	.get_tune_settings = stv0299_get_tune_settings,
731 
732 	.read_status = stv0299_read_status,
733 	.read_ber = stv0299_read_ber,
734 	.read_signal_strength = stv0299_read_signal_strength,
735 	.read_snr = stv0299_read_snr,
736 	.read_ucblocks = stv0299_read_ucblocks,
737 
738 	.diseqc_send_master_cmd = stv0299_send_diseqc_msg,
739 	.diseqc_send_burst = stv0299_send_diseqc_burst,
740 	.set_tone = stv0299_set_tone,
741 	.set_voltage = stv0299_set_voltage,
742 	.dishnetwork_send_legacy_command = stv0299_send_legacy_dish_cmd,
743 };
744 
745 module_param(debug_legacy_dish_switch, int, 0444);
746 MODULE_PARM_DESC(debug_legacy_dish_switch, "Enable timing analysis for Dish Network legacy switches");
747 
748 module_param(debug, int, 0644);
749 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
750 
751 MODULE_DESCRIPTION("ST STV0299 DVB Demodulator driver");
752 MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Peter Schildmann, Felix Domke, Andreas Oberritter, Andrew de Quincey, Kenneth Aafly");
753 MODULE_LICENSE("GPL");
754 
755 EXPORT_SYMBOL_GPL(stv0299_attach);
756