1 /*
2  * Frontend driver for the GENPIX 8pks/qpsk/DCII USB2.0 DVB-S module
3  *
4  * Copyright (C) 2006,2007 Alan Nisota (alannisota@gmail.com)
5  * Copyright (C) 2006,2007 Genpix Electronics (genpix@genpix-electronics.com)
6  *
7  * Thanks to GENPIX for the sample code used to implement this module.
8  *
9  * This module is based off the vp7045 and vp702x modules
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License as published by the Free
13  * Software Foundation, version 2.
14  */
15 
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 
18 #include "gp8psk-fe.h"
19 #include "dvb_frontend.h"
20 
21 static int debug;
22 module_param(debug, int, 0644);
23 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
24 
25 #define dprintk(fmt, arg...) do {					\
26 	if (debug)							\
27 		printk(KERN_DEBUG pr_fmt("%s: " fmt),			\
28 		       __func__, ##arg);				\
29 } while (0)
30 
31 struct gp8psk_fe_state {
32 	struct dvb_frontend fe;
33 	void *priv;
34 	const struct gp8psk_fe_ops *ops;
35 	bool is_rev1;
36 	u8 lock;
37 	u16 snr;
38 	unsigned long next_status_check;
39 	unsigned long status_check_interval;
40 };
41 
42 static int gp8psk_tuned_to_DCII(struct dvb_frontend *fe)
43 {
44 	struct gp8psk_fe_state *st = fe->demodulator_priv;
45 	u8 status;
46 
47 	st->ops->in(st->priv, GET_8PSK_CONFIG, 0, 0, &status, 1);
48 	return status & bmDCtuned;
49 }
50 
51 static int gp8psk_set_tuner_mode(struct dvb_frontend *fe, int mode)
52 {
53 	struct gp8psk_fe_state *st = fe->demodulator_priv;
54 
55 	return st->ops->out(st->priv, SET_8PSK_CONFIG, mode, 0, NULL, 0);
56 }
57 
58 static int gp8psk_fe_update_status(struct gp8psk_fe_state *st)
59 {
60 	u8 buf[6];
61 	if (time_after(jiffies,st->next_status_check)) {
62 		st->ops->in(st->priv, GET_SIGNAL_LOCK, 0, 0, &st->lock, 1);
63 		st->ops->in(st->priv, GET_SIGNAL_STRENGTH, 0, 0, buf, 6);
64 		st->snr = (buf[1]) << 8 | buf[0];
65 		st->next_status_check = jiffies + (st->status_check_interval*HZ)/1000;
66 	}
67 	return 0;
68 }
69 
70 static int gp8psk_fe_read_status(struct dvb_frontend *fe,
71 				 enum fe_status *status)
72 {
73 	struct gp8psk_fe_state *st = fe->demodulator_priv;
74 	gp8psk_fe_update_status(st);
75 
76 	if (st->lock)
77 		*status = FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI | FE_HAS_SIGNAL | FE_HAS_CARRIER;
78 	else
79 		*status = 0;
80 
81 	if (*status & FE_HAS_LOCK)
82 		st->status_check_interval = 1000;
83 	else
84 		st->status_check_interval = 100;
85 	return 0;
86 }
87 
88 /* not supported by this Frontend */
89 static int gp8psk_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
90 {
91 	(void) fe;
92 	*ber = 0;
93 	return 0;
94 }
95 
96 /* not supported by this Frontend */
97 static int gp8psk_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
98 {
99 	(void) fe;
100 	*unc = 0;
101 	return 0;
102 }
103 
104 static int gp8psk_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
105 {
106 	struct gp8psk_fe_state *st = fe->demodulator_priv;
107 	gp8psk_fe_update_status(st);
108 	/* snr is reported in dBu*256 */
109 	*snr = st->snr;
110 	return 0;
111 }
112 
113 static int gp8psk_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
114 {
115 	struct gp8psk_fe_state *st = fe->demodulator_priv;
116 	gp8psk_fe_update_status(st);
117 	/* snr is reported in dBu*256 */
118 	/* snr / 38.4 ~= 100% strength */
119 	/* snr * 17 returns 100% strength as 65535 */
120 	if (st->snr > 0xf00)
121 		*strength = 0xffff;
122 	else
123 		*strength = (st->snr << 4) + st->snr; /* snr*17 */
124 	return 0;
125 }
126 
127 static int gp8psk_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
128 {
129 	tune->min_delay_ms = 800;
130 	return 0;
131 }
132 
133 static int gp8psk_fe_set_frontend(struct dvb_frontend *fe)
134 {
135 	struct gp8psk_fe_state *st = fe->demodulator_priv;
136 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
137 	u8 cmd[10];
138 	u32 freq = c->frequency * 1000;
139 
140 	dprintk("%s()\n", __func__);
141 
142 	cmd[4] = freq         & 0xff;
143 	cmd[5] = (freq >> 8)  & 0xff;
144 	cmd[6] = (freq >> 16) & 0xff;
145 	cmd[7] = (freq >> 24) & 0xff;
146 
147 	/* backwards compatibility: DVB-S + 8-PSK were used for Turbo-FEC */
148 	if (c->delivery_system == SYS_DVBS && c->modulation == PSK_8)
149 		c->delivery_system = SYS_TURBO;
150 
151 	switch (c->delivery_system) {
152 	case SYS_DVBS:
153 		if (c->modulation != QPSK) {
154 			dprintk("%s: unsupported modulation selected (%d)\n",
155 				__func__, c->modulation);
156 			return -EOPNOTSUPP;
157 		}
158 		c->fec_inner = FEC_AUTO;
159 		break;
160 	case SYS_DVBS2: /* kept for backwards compatibility */
161 		dprintk("%s: DVB-S2 delivery system selected\n", __func__);
162 		break;
163 	case SYS_TURBO:
164 		dprintk("%s: Turbo-FEC delivery system selected\n", __func__);
165 		break;
166 
167 	default:
168 		dprintk("%s: unsupported delivery system selected (%d)\n",
169 			__func__, c->delivery_system);
170 		return -EOPNOTSUPP;
171 	}
172 
173 	cmd[0] =  c->symbol_rate        & 0xff;
174 	cmd[1] = (c->symbol_rate >>  8) & 0xff;
175 	cmd[2] = (c->symbol_rate >> 16) & 0xff;
176 	cmd[3] = (c->symbol_rate >> 24) & 0xff;
177 	switch (c->modulation) {
178 	case QPSK:
179 		if (st->is_rev1)
180 			if (gp8psk_tuned_to_DCII(fe))
181 				st->ops->reload(st->priv);
182 		switch (c->fec_inner) {
183 		case FEC_1_2:
184 			cmd[9] = 0; break;
185 		case FEC_2_3:
186 			cmd[9] = 1; break;
187 		case FEC_3_4:
188 			cmd[9] = 2; break;
189 		case FEC_5_6:
190 			cmd[9] = 3; break;
191 		case FEC_7_8:
192 			cmd[9] = 4; break;
193 		case FEC_AUTO:
194 			cmd[9] = 5; break;
195 		default:
196 			cmd[9] = 5; break;
197 		}
198 		if (c->delivery_system == SYS_TURBO)
199 			cmd[8] = ADV_MOD_TURBO_QPSK;
200 		else
201 			cmd[8] = ADV_MOD_DVB_QPSK;
202 		break;
203 	case PSK_8: /* PSK_8 is for compatibility with DN */
204 		cmd[8] = ADV_MOD_TURBO_8PSK;
205 		switch (c->fec_inner) {
206 		case FEC_2_3:
207 			cmd[9] = 0; break;
208 		case FEC_3_4:
209 			cmd[9] = 1; break;
210 		case FEC_3_5:
211 			cmd[9] = 2; break;
212 		case FEC_5_6:
213 			cmd[9] = 3; break;
214 		case FEC_8_9:
215 			cmd[9] = 4; break;
216 		default:
217 			cmd[9] = 0; break;
218 		}
219 		break;
220 	case QAM_16: /* QAM_16 is for compatibility with DN */
221 		cmd[8] = ADV_MOD_TURBO_16QAM;
222 		cmd[9] = 0;
223 		break;
224 	default: /* Unknown modulation */
225 		dprintk("%s: unsupported modulation selected (%d)\n",
226 			__func__, c->modulation);
227 		return -EOPNOTSUPP;
228 	}
229 
230 	if (st->is_rev1)
231 		gp8psk_set_tuner_mode(fe, 0);
232 	st->ops->out(st->priv, TUNE_8PSK, 0, 0, cmd, 10);
233 
234 	st->lock = 0;
235 	st->next_status_check = jiffies;
236 	st->status_check_interval = 200;
237 
238 	return 0;
239 }
240 
241 static int gp8psk_fe_send_diseqc_msg (struct dvb_frontend* fe,
242 				    struct dvb_diseqc_master_cmd *m)
243 {
244 	struct gp8psk_fe_state *st = fe->demodulator_priv;
245 
246 	dprintk("%s\n", __func__);
247 
248 	if (st->ops->out(st->priv, SEND_DISEQC_COMMAND, m->msg[0], 0,
249 			m->msg, m->msg_len)) {
250 		return -EINVAL;
251 	}
252 	return 0;
253 }
254 
255 static int gp8psk_fe_send_diseqc_burst(struct dvb_frontend *fe,
256 				       enum fe_sec_mini_cmd burst)
257 {
258 	struct gp8psk_fe_state *st = fe->demodulator_priv;
259 	u8 cmd;
260 
261 	dprintk("%s\n", __func__);
262 
263 	/* These commands are certainly wrong */
264 	cmd = (burst == SEC_MINI_A) ? 0x00 : 0x01;
265 
266 	if (st->ops->out(st->priv, SEND_DISEQC_COMMAND, cmd, 0,
267 			&cmd, 0)) {
268 		return -EINVAL;
269 	}
270 	return 0;
271 }
272 
273 static int gp8psk_fe_set_tone(struct dvb_frontend *fe,
274 			      enum fe_sec_tone_mode tone)
275 {
276 	struct gp8psk_fe_state *st = fe->demodulator_priv;
277 
278 	if (st->ops->out(st->priv, SET_22KHZ_TONE,
279 			 (tone == SEC_TONE_ON), 0, NULL, 0)) {
280 		return -EINVAL;
281 	}
282 	return 0;
283 }
284 
285 static int gp8psk_fe_set_voltage(struct dvb_frontend *fe,
286 				 enum fe_sec_voltage voltage)
287 {
288 	struct gp8psk_fe_state *st = fe->demodulator_priv;
289 
290 	if (st->ops->out(st->priv, SET_LNB_VOLTAGE,
291 			 voltage == SEC_VOLTAGE_18, 0, NULL, 0)) {
292 		return -EINVAL;
293 	}
294 	return 0;
295 }
296 
297 static int gp8psk_fe_enable_high_lnb_voltage(struct dvb_frontend* fe, long onoff)
298 {
299 	struct gp8psk_fe_state *st = fe->demodulator_priv;
300 
301 	return st->ops->out(st->priv, USE_EXTRA_VOLT, onoff, 0, NULL, 0);
302 }
303 
304 static int gp8psk_fe_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long sw_cmd)
305 {
306 	struct gp8psk_fe_state *st = fe->demodulator_priv;
307 	u8 cmd = sw_cmd & 0x7f;
308 
309 	if (st->ops->out(st->priv, SET_DN_SWITCH, cmd, 0, NULL, 0))
310 		return -EINVAL;
311 
312 	if (st->ops->out(st->priv, SET_LNB_VOLTAGE, !!(sw_cmd & 0x80),
313 			0, NULL, 0))
314 		return -EINVAL;
315 
316 	return 0;
317 }
318 
319 static void gp8psk_fe_release(struct dvb_frontend* fe)
320 {
321 	struct gp8psk_fe_state *st = fe->demodulator_priv;
322 
323 	kfree(st);
324 }
325 
326 static const struct dvb_frontend_ops gp8psk_fe_ops;
327 
328 struct dvb_frontend *gp8psk_fe_attach(const struct gp8psk_fe_ops *ops,
329 				      void *priv, bool is_rev1)
330 {
331 	struct gp8psk_fe_state *st;
332 
333 	if (!ops || !ops->in || !ops->out || !ops->reload) {
334 		pr_err("Error! gp8psk-fe ops not defined.\n");
335 		return NULL;
336 	}
337 
338 	st = kzalloc(sizeof(struct gp8psk_fe_state), GFP_KERNEL);
339 	if (!st)
340 		return NULL;
341 
342 	memcpy(&st->fe.ops, &gp8psk_fe_ops, sizeof(struct dvb_frontend_ops));
343 	st->fe.demodulator_priv = st;
344 	st->ops = ops;
345 	st->priv = priv;
346 	st->is_rev1 = is_rev1;
347 
348 	pr_info("Frontend %sattached\n", is_rev1 ? "revision 1 " : "");
349 
350 	return &st->fe;
351 }
352 EXPORT_SYMBOL_GPL(gp8psk_fe_attach);
353 
354 static const struct dvb_frontend_ops gp8psk_fe_ops = {
355 	.delsys = { SYS_DVBS },
356 	.info = {
357 		.name			= "Genpix DVB-S",
358 		.frequency_min		= 800000,
359 		.frequency_max		= 2250000,
360 		.frequency_stepsize	= 100,
361 		.symbol_rate_min        = 1000000,
362 		.symbol_rate_max        = 45000000,
363 		.symbol_rate_tolerance  = 500,  /* ppm */
364 		.caps = FE_CAN_INVERSION_AUTO |
365 			FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
366 			FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
367 			/*
368 			 * FE_CAN_QAM_16 is for compatibility
369 			 * (Myth incorrectly detects Turbo-QPSK as plain QAM-16)
370 			 */
371 			FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_TURBO_FEC
372 	},
373 
374 	.release = gp8psk_fe_release,
375 
376 	.init = NULL,
377 	.sleep = NULL,
378 
379 	.set_frontend = gp8psk_fe_set_frontend,
380 
381 	.get_tune_settings = gp8psk_fe_get_tune_settings,
382 
383 	.read_status = gp8psk_fe_read_status,
384 	.read_ber = gp8psk_fe_read_ber,
385 	.read_signal_strength = gp8psk_fe_read_signal_strength,
386 	.read_snr = gp8psk_fe_read_snr,
387 	.read_ucblocks = gp8psk_fe_read_unc_blocks,
388 
389 	.diseqc_send_master_cmd = gp8psk_fe_send_diseqc_msg,
390 	.diseqc_send_burst = gp8psk_fe_send_diseqc_burst,
391 	.set_tone = gp8psk_fe_set_tone,
392 	.set_voltage = gp8psk_fe_set_voltage,
393 	.dishnetwork_send_legacy_command = gp8psk_fe_send_legacy_dish_cmd,
394 	.enable_high_lnb_voltage = gp8psk_fe_enable_high_lnb_voltage
395 };
396 
397 MODULE_AUTHOR("Alan Nisota <alannisota@gamil.com>");
398 MODULE_DESCRIPTION("Frontend Driver for Genpix DVB-S");
399 MODULE_VERSION("1.1");
400 MODULE_LICENSE("GPL");
401