blob: be19afeed7a96190265127cd17ee5b5028d090e8 [file] [log] [blame]
Alan Nisota9bbe0762006-05-14 13:23:56 -03001/* DVB USB compliant Linux driver for the
Alan Nisota458b6342007-08-18 17:52:35 -03002 * - GENPIX 8pks/qpsk/DCII USB2.0 DVB-S module
Alan Nisota9bbe0762006-05-14 13:23:56 -03003 *
Alan Nisota458b6342007-08-18 17:52:35 -03004 * Copyright (C) 2006,2007 Alan Nisota (alannisota@gmail.com)
5 * Copyright (C) 2006,2007 Genpix Electronics (genpix@genpix-electronics.com)
Alan Nisota9bbe0762006-05-14 13:23:56 -03006 *
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 * see Documentation/dvb/README.dvb-usb for more information
16 */
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -020017
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20#include "gp8psk-fe.h"
21#include "dvb_frontend.h"
22
23static int debug;
24module_param(debug, int, 0644);
25MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
26
27#define dprintk(fmt, arg...) do { \
28 if (debug) \
29 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
30 __func__, ##arg); \
31} while (0)
Alan Nisota9bbe0762006-05-14 13:23:56 -030032
33struct gp8psk_fe_state {
34 struct dvb_frontend fe;
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -020035 void *priv;
36 const struct gp8psk_fe_ops *ops;
37 bool is_rev1;
Alan Nisota458b6342007-08-18 17:52:35 -030038 u8 lock;
Alan Nisota9bbe0762006-05-14 13:23:56 -030039 u16 snr;
Alan Nisota458b6342007-08-18 17:52:35 -030040 unsigned long next_status_check;
41 unsigned long status_check_interval;
Alan Nisota9bbe0762006-05-14 13:23:56 -030042};
43
Alan Nisota02ebf232008-12-20 12:03:06 -030044static int gp8psk_tuned_to_DCII(struct dvb_frontend *fe)
45{
46 struct gp8psk_fe_state *st = fe->demodulator_priv;
47 u8 status;
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -020048
49 st->ops->in(st->priv, GET_8PSK_CONFIG, 0, 0, &status, 1);
Alan Nisota02ebf232008-12-20 12:03:06 -030050 return status & bmDCtuned;
51}
52
53static int gp8psk_set_tuner_mode(struct dvb_frontend *fe, int mode)
54{
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -020055 struct gp8psk_fe_state *st = fe->demodulator_priv;
56
57 return st->ops->out(st->priv, SET_8PSK_CONFIG, mode, 0, NULL, 0);
Alan Nisota02ebf232008-12-20 12:03:06 -030058}
59
Alan Nisota458b6342007-08-18 17:52:35 -030060static int gp8psk_fe_update_status(struct gp8psk_fe_state *st)
61{
62 u8 buf[6];
63 if (time_after(jiffies,st->next_status_check)) {
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -020064 st->ops->in(st->priv, GET_SIGNAL_LOCK, 0, 0, &st->lock, 1);
65 st->ops->in(st->priv, GET_SIGNAL_STRENGTH, 0, 0, buf, 6);
Alan Nisota458b6342007-08-18 17:52:35 -030066 st->snr = (buf[1]) << 8 | buf[0];
67 st->next_status_check = jiffies + (st->status_check_interval*HZ)/1000;
68 }
69 return 0;
70}
71
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030072static int gp8psk_fe_read_status(struct dvb_frontend *fe,
73 enum fe_status *status)
Alan Nisota9bbe0762006-05-14 13:23:56 -030074{
75 struct gp8psk_fe_state *st = fe->demodulator_priv;
Alan Nisota458b6342007-08-18 17:52:35 -030076 gp8psk_fe_update_status(st);
Alan Nisota9bbe0762006-05-14 13:23:56 -030077
Alan Nisota458b6342007-08-18 17:52:35 -030078 if (st->lock)
Alan Nisota9bbe0762006-05-14 13:23:56 -030079 *status = FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI | FE_HAS_SIGNAL | FE_HAS_CARRIER;
80 else
81 *status = 0;
82
Alan Nisota458b6342007-08-18 17:52:35 -030083 if (*status & FE_HAS_LOCK)
84 st->status_check_interval = 1000;
85 else
86 st->status_check_interval = 100;
Alan Nisota9bbe0762006-05-14 13:23:56 -030087 return 0;
88}
89
90/* not supported by this Frontend */
91static int gp8psk_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
92{
93 (void) fe;
94 *ber = 0;
95 return 0;
96}
97
98/* not supported by this Frontend */
99static int gp8psk_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
100{
101 (void) fe;
102 *unc = 0;
103 return 0;
104}
105
106static int gp8psk_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
107{
108 struct gp8psk_fe_state *st = fe->demodulator_priv;
Alan Nisota458b6342007-08-18 17:52:35 -0300109 gp8psk_fe_update_status(st);
110 /* snr is reported in dBu*256 */
111 *snr = st->snr;
Alan Nisota9bbe0762006-05-14 13:23:56 -0300112 return 0;
113}
114
115static int gp8psk_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
116{
Alan Nisota458b6342007-08-18 17:52:35 -0300117 struct gp8psk_fe_state *st = fe->demodulator_priv;
118 gp8psk_fe_update_status(st);
119 /* snr is reported in dBu*256 */
120 /* snr / 38.4 ~= 100% strength */
121 /* snr * 17 returns 100% strength as 65535 */
122 if (st->snr > 0xf00)
123 *strength = 0xffff;
124 else
125 *strength = (st->snr << 4) + st->snr; /* snr*17 */
126 return 0;
Alan Nisota9bbe0762006-05-14 13:23:56 -0300127}
128
129static int gp8psk_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
130{
Derek Kellyfb249ca2010-10-16 16:07:47 -0300131 tune->min_delay_ms = 800;
Alan Nisota9bbe0762006-05-14 13:23:56 -0300132 return 0;
133}
134
Mauro Carvalho Chehab6e07d5c2011-12-26 15:49:16 -0300135static int gp8psk_fe_set_frontend(struct dvb_frontend *fe)
Alan Nisota9bbe0762006-05-14 13:23:56 -0300136{
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200137 struct gp8psk_fe_state *st = fe->demodulator_priv;
Alan Nisota02ebf232008-12-20 12:03:06 -0300138 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Alan Nisota9bbe0762006-05-14 13:23:56 -0300139 u8 cmd[10];
Mauro Carvalho Chehab6e07d5c2011-12-26 15:49:16 -0300140 u32 freq = c->frequency * 1000;
Alan Nisota02ebf232008-12-20 12:03:06 -0300141
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200142 dprintk("%s()\n", __func__);
Alan Nisota9bbe0762006-05-14 13:23:56 -0300143
144 cmd[4] = freq & 0xff;
145 cmd[5] = (freq >> 8) & 0xff;
146 cmd[6] = (freq >> 16) & 0xff;
147 cmd[7] = (freq >> 24) & 0xff;
148
Andreas Oberritter8fc74fd2011-08-08 11:54:37 -0300149 /* backwards compatibility: DVB-S + 8-PSK were used for Turbo-FEC */
150 if (c->delivery_system == SYS_DVBS && c->modulation == PSK_8)
151 c->delivery_system = SYS_TURBO;
152
Alan Nisota02ebf232008-12-20 12:03:06 -0300153 switch (c->delivery_system) {
154 case SYS_DVBS:
Andreas Oberritter8fc74fd2011-08-08 11:54:37 -0300155 if (c->modulation != QPSK) {
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200156 dprintk("%s: unsupported modulation selected (%d)\n",
Alan Nisota02ebf232008-12-20 12:03:06 -0300157 __func__, c->modulation);
158 return -EOPNOTSUPP;
159 }
160 c->fec_inner = FEC_AUTO;
Alan Nisota9bbe0762006-05-14 13:23:56 -0300161 break;
Andreas Oberritter8fc74fd2011-08-08 11:54:37 -0300162 case SYS_DVBS2: /* kept for backwards compatibility */
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200163 dprintk("%s: DVB-S2 delivery system selected\n", __func__);
Alan Nisota02ebf232008-12-20 12:03:06 -0300164 break;
Andreas Oberritter8fc74fd2011-08-08 11:54:37 -0300165 case SYS_TURBO:
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200166 dprintk("%s: Turbo-FEC delivery system selected\n", __func__);
Andreas Oberritter8fc74fd2011-08-08 11:54:37 -0300167 break;
Alan Nisota02ebf232008-12-20 12:03:06 -0300168
Alan Nisota9bbe0762006-05-14 13:23:56 -0300169 default:
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200170 dprintk("%s: unsupported delivery system selected (%d)\n",
Alan Nisota02ebf232008-12-20 12:03:06 -0300171 __func__, c->delivery_system);
172 return -EOPNOTSUPP;
Alan Nisota9bbe0762006-05-14 13:23:56 -0300173 }
174
Alan Nisota02ebf232008-12-20 12:03:06 -0300175 cmd[0] = c->symbol_rate & 0xff;
176 cmd[1] = (c->symbol_rate >> 8) & 0xff;
177 cmd[2] = (c->symbol_rate >> 16) & 0xff;
178 cmd[3] = (c->symbol_rate >> 24) & 0xff;
179 switch (c->modulation) {
180 case QPSK:
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200181 if (st->is_rev1)
Alan Nisota02ebf232008-12-20 12:03:06 -0300182 if (gp8psk_tuned_to_DCII(fe))
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200183 st->ops->reload(st->priv);
Alan Nisota02ebf232008-12-20 12:03:06 -0300184 switch (c->fec_inner) {
185 case FEC_1_2:
186 cmd[9] = 0; break;
187 case FEC_2_3:
188 cmd[9] = 1; break;
189 case FEC_3_4:
190 cmd[9] = 2; break;
191 case FEC_5_6:
192 cmd[9] = 3; break;
193 case FEC_7_8:
194 cmd[9] = 4; break;
195 case FEC_AUTO:
196 cmd[9] = 5; break;
197 default:
198 cmd[9] = 5; break;
199 }
Andreas Oberritter8fc74fd2011-08-08 11:54:37 -0300200 if (c->delivery_system == SYS_TURBO)
201 cmd[8] = ADV_MOD_TURBO_QPSK;
202 else
203 cmd[8] = ADV_MOD_DVB_QPSK;
Alan Nisota02ebf232008-12-20 12:03:06 -0300204 break;
205 case PSK_8: /* PSK_8 is for compatibility with DN */
206 cmd[8] = ADV_MOD_TURBO_8PSK;
207 switch (c->fec_inner) {
208 case FEC_2_3:
209 cmd[9] = 0; break;
210 case FEC_3_4:
211 cmd[9] = 1; break;
212 case FEC_3_5:
213 cmd[9] = 2; break;
214 case FEC_5_6:
215 cmd[9] = 3; break;
216 case FEC_8_9:
217 cmd[9] = 4; break;
218 default:
219 cmd[9] = 0; break;
220 }
221 break;
222 case QAM_16: /* QAM_16 is for compatibility with DN */
223 cmd[8] = ADV_MOD_TURBO_16QAM;
224 cmd[9] = 0;
225 break;
226 default: /* Unknown modulation */
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200227 dprintk("%s: unsupported modulation selected (%d)\n",
Alan Nisota02ebf232008-12-20 12:03:06 -0300228 __func__, c->modulation);
229 return -EOPNOTSUPP;
230 }
231
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200232 if (st->is_rev1)
Alan Nisota02ebf232008-12-20 12:03:06 -0300233 gp8psk_set_tuner_mode(fe, 0);
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200234 st->ops->out(st->priv, TUNE_8PSK, 0, 0, cmd, 10);
Alan Nisota9bbe0762006-05-14 13:23:56 -0300235
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200236 st->lock = 0;
237 st->next_status_check = jiffies;
238 st->status_check_interval = 200;
Alan Nisota9bbe0762006-05-14 13:23:56 -0300239
240 return 0;
241}
242
Alan Nisota9bbe0762006-05-14 13:23:56 -0300243static int gp8psk_fe_send_diseqc_msg (struct dvb_frontend* fe,
244 struct dvb_diseqc_master_cmd *m)
245{
246 struct gp8psk_fe_state *st = fe->demodulator_priv;
247
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200248 dprintk("%s\n", __func__);
Alan Nisota9bbe0762006-05-14 13:23:56 -0300249
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200250 if (st->ops->out(st->priv, SEND_DISEQC_COMMAND, m->msg[0], 0,
Alan Nisota9bbe0762006-05-14 13:23:56 -0300251 m->msg, m->msg_len)) {
252 return -EINVAL;
253 }
254 return 0;
255}
256
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300257static int gp8psk_fe_send_diseqc_burst(struct dvb_frontend *fe,
258 enum fe_sec_mini_cmd burst)
Alan Nisota9bbe0762006-05-14 13:23:56 -0300259{
260 struct gp8psk_fe_state *st = fe->demodulator_priv;
261 u8 cmd;
262
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200263 dprintk("%s\n", __func__);
Alan Nisota9bbe0762006-05-14 13:23:56 -0300264
265 /* These commands are certainly wrong */
266 cmd = (burst == SEC_MINI_A) ? 0x00 : 0x01;
267
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200268 if (st->ops->out(st->priv, SEND_DISEQC_COMMAND, cmd, 0,
Alan Nisota9bbe0762006-05-14 13:23:56 -0300269 &cmd, 0)) {
270 return -EINVAL;
271 }
272 return 0;
273}
274
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300275static int gp8psk_fe_set_tone(struct dvb_frontend *fe,
276 enum fe_sec_tone_mode tone)
Alan Nisota9bbe0762006-05-14 13:23:56 -0300277{
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200278 struct gp8psk_fe_state *st = fe->demodulator_priv;
Alan Nisota9bbe0762006-05-14 13:23:56 -0300279
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200280 if (st->ops->out(st->priv, SET_22KHZ_TONE,
281 (tone == SEC_TONE_ON), 0, NULL, 0)) {
Alan Nisota9bbe0762006-05-14 13:23:56 -0300282 return -EINVAL;
283 }
284 return 0;
285}
286
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300287static int gp8psk_fe_set_voltage(struct dvb_frontend *fe,
288 enum fe_sec_voltage voltage)
Alan Nisota9bbe0762006-05-14 13:23:56 -0300289{
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200290 struct gp8psk_fe_state *st = fe->demodulator_priv;
Alan Nisota9bbe0762006-05-14 13:23:56 -0300291
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200292 if (st->ops->out(st->priv, SET_LNB_VOLTAGE,
Alan Nisota9bbe0762006-05-14 13:23:56 -0300293 voltage == SEC_VOLTAGE_18, 0, NULL, 0)) {
294 return -EINVAL;
295 }
296 return 0;
297}
298
Alan Nisota458b6342007-08-18 17:52:35 -0300299static int gp8psk_fe_enable_high_lnb_voltage(struct dvb_frontend* fe, long onoff)
300{
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200301 struct gp8psk_fe_state *st = fe->demodulator_priv;
302
303 return st->ops->out(st->priv, USE_EXTRA_VOLT, onoff, 0, NULL, 0);
Alan Nisota458b6342007-08-18 17:52:35 -0300304}
305
Alan Nisota9bbe0762006-05-14 13:23:56 -0300306static int gp8psk_fe_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long sw_cmd)
307{
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200308 struct gp8psk_fe_state *st = fe->demodulator_priv;
Alan Nisota9bbe0762006-05-14 13:23:56 -0300309 u8 cmd = sw_cmd & 0x7f;
310
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200311 if (st->ops->out(st->priv, SET_DN_SWITCH, cmd, 0, NULL, 0))
Alan Nisota9bbe0762006-05-14 13:23:56 -0300312 return -EINVAL;
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200313
314 if (st->ops->out(st->priv, SET_LNB_VOLTAGE, !!(sw_cmd & 0x80),
315 0, NULL, 0))
Alan Nisota9bbe0762006-05-14 13:23:56 -0300316 return -EINVAL;
Alan Nisota9bbe0762006-05-14 13:23:56 -0300317
318 return 0;
319}
320
321static void gp8psk_fe_release(struct dvb_frontend* fe)
322{
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200323 struct gp8psk_fe_state *st = fe->demodulator_priv;
324
325 kfree(st);
Alan Nisota9bbe0762006-05-14 13:23:56 -0300326}
327
328static struct dvb_frontend_ops gp8psk_fe_ops;
329
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200330struct dvb_frontend *gp8psk_fe_attach(const struct gp8psk_fe_ops *ops,
331 void *priv, bool is_rev1)
Alan Nisota9bbe0762006-05-14 13:23:56 -0300332{
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200333 struct gp8psk_fe_state *st;
Alan Nisota9bbe0762006-05-14 13:23:56 -0300334
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200335 if (!ops || !ops->in || !ops->out || !ops->reload) {
336 pr_err("Error! gp8psk-fe ops not defined.\n");
337 return NULL;
338 }
Alan Nisota9bbe0762006-05-14 13:23:56 -0300339
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200340 st = kzalloc(sizeof(struct gp8psk_fe_state), GFP_KERNEL);
341 if (!st)
342 return NULL;
343
344 memcpy(&st->fe.ops, &gp8psk_fe_ops, sizeof(struct dvb_frontend_ops));
345 st->fe.demodulator_priv = st;
346 st->ops = ops;
347 st->priv = priv;
348 st->is_rev1 = is_rev1;
349
350 pr_info("Frontend %sattached\n", is_rev1 ? "revision 1 " : "");
351
352 return &st->fe;
Alan Nisota9bbe0762006-05-14 13:23:56 -0300353}
Mauro Carvalho Chehab7a0786c2016-11-12 12:46:28 -0200354EXPORT_SYMBOL_GPL(gp8psk_fe_attach);
Alan Nisota9bbe0762006-05-14 13:23:56 -0300355
356static struct dvb_frontend_ops gp8psk_fe_ops = {
Mauro Carvalho Chehab6e07d5c2011-12-26 15:49:16 -0300357 .delsys = { SYS_DVBS },
Alan Nisota9bbe0762006-05-14 13:23:56 -0300358 .info = {
Derek Kellyd12da8e2010-10-16 16:18:15 -0300359 .name = "Genpix DVB-S",
Alan Nisota458b6342007-08-18 17:52:35 -0300360 .frequency_min = 800000,
361 .frequency_max = 2250000,
Alan Nisota9bbe0762006-05-14 13:23:56 -0300362 .frequency_stepsize = 100,
363 .symbol_rate_min = 1000000,
364 .symbol_rate_max = 45000000,
365 .symbol_rate_tolerance = 500, /* ppm */
366 .caps = FE_CAN_INVERSION_AUTO |
Alan Nisota02ebf232008-12-20 12:03:06 -0300367 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
368 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
369 /*
370 * FE_CAN_QAM_16 is for compatibility
371 * (Myth incorrectly detects Turbo-QPSK as plain QAM-16)
372 */
Klaus Schmidingerf6a20eb2010-07-01 01:37:34 -0300373 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_TURBO_FEC
Alan Nisota9bbe0762006-05-14 13:23:56 -0300374 },
375
376 .release = gp8psk_fe_release,
377
378 .init = NULL,
379 .sleep = NULL,
380
Mauro Carvalho Chehab6e07d5c2011-12-26 15:49:16 -0300381 .set_frontend = gp8psk_fe_set_frontend,
Alan Nisota02ebf232008-12-20 12:03:06 -0300382
Alan Nisota9bbe0762006-05-14 13:23:56 -0300383 .get_tune_settings = gp8psk_fe_get_tune_settings,
384
385 .read_status = gp8psk_fe_read_status,
386 .read_ber = gp8psk_fe_read_ber,
387 .read_signal_strength = gp8psk_fe_read_signal_strength,
388 .read_snr = gp8psk_fe_read_snr,
389 .read_ucblocks = gp8psk_fe_read_unc_blocks,
390
391 .diseqc_send_master_cmd = gp8psk_fe_send_diseqc_msg,
392 .diseqc_send_burst = gp8psk_fe_send_diseqc_burst,
393 .set_tone = gp8psk_fe_set_tone,
394 .set_voltage = gp8psk_fe_set_voltage,
395 .dishnetwork_send_legacy_command = gp8psk_fe_send_legacy_dish_cmd,
Alan Nisota458b6342007-08-18 17:52:35 -0300396 .enable_high_lnb_voltage = gp8psk_fe_enable_high_lnb_voltage
Alan Nisota9bbe0762006-05-14 13:23:56 -0300397};