2 * ddbridge-max.c: Digital Devices bridge MAX card support
4 * Copyright (C) 2010-2017 Digital Devices GmbH
5 * Ralph Metzler <rjkm@metzlerbros.de>
6 * Marcus Metzler <mocm@metzlerbros.de>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 only, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/poll.h>
26 #include <linux/pci.h>
27 #include <linux/pci_ids.h>
28 #include <linux/timer.h>
29 #include <linux/i2c.h>
30 #include <linux/swab.h>
31 #include <linux/vmalloc.h>
34 #include "ddbridge-regs.h"
35 #include "ddbridge-io.h"
36 #include "ddbridge-mci.h"
38 #include "ddbridge-max.h"
41 /******************************************************************************/
43 /* MaxS4/8 related modparams */
45 module_param(fmode
, int, 0444);
46 MODULE_PARM_DESC(fmode
, "frontend emulation mode");
48 static int fmode_sat
= -1;
49 module_param(fmode_sat
, int, 0444);
50 MODULE_PARM_DESC(fmode_sat
, "set frontend emulation mode sat");
52 static int old_quattro
;
53 module_param(old_quattro
, int, 0444);
54 MODULE_PARM_DESC(old_quattro
, "old quattro LNB input order ");
56 /******************************************************************************/
58 static int lnb_command(struct ddb
*dev
, u32 link
, u32 lnb
, u32 cmd
)
60 u32 c
, v
= 0, tag
= DDB_LINK_TAG(link
);
62 v
= LNB_TONE
& (dev
->link
[link
].lnb
.tone
<< (15 - lnb
));
63 ddbwritel(dev
, cmd
| v
, tag
| LNB_CONTROL(lnb
));
64 for (c
= 0; c
< 10; c
++) {
65 v
= ddbreadl(dev
, tag
| LNB_CONTROL(lnb
));
66 if ((v
& LNB_BUSY
) == 0)
71 dev_info(dev
->dev
, "%s lnb = %08x cmd = %08x\n",
76 static int max_send_master_cmd(struct dvb_frontend
*fe
,
77 struct dvb_diseqc_master_cmd
*cmd
)
79 struct ddb_input
*input
= fe
->sec_priv
;
80 struct ddb_port
*port
= input
->port
;
81 struct ddb
*dev
= port
->dev
;
82 struct ddb_dvb
*dvb
= &port
->dvb
[input
->nr
& 1];
83 u32 tag
= DDB_LINK_TAG(port
->lnr
);
85 u32 fmode
= dev
->link
[port
->lnr
].lnb
.fmode
;
87 if (fmode
== 2 || fmode
== 1)
89 if (dvb
->diseqc_send_master_cmd
)
90 dvb
->diseqc_send_master_cmd(fe
, cmd
);
92 mutex_lock(&dev
->link
[port
->lnr
].lnb
.lock
);
93 ddbwritel(dev
, 0, tag
| LNB_BUF_LEVEL(dvb
->input
));
94 for (i
= 0; i
< cmd
->msg_len
; i
++)
95 ddbwritel(dev
, cmd
->msg
[i
], tag
| LNB_BUF_WRITE(dvb
->input
));
96 lnb_command(dev
, port
->lnr
, dvb
->input
, LNB_CMD_DISEQC
);
97 mutex_unlock(&dev
->link
[port
->lnr
].lnb
.lock
);
101 static int lnb_send_diseqc(struct ddb
*dev
, u32 link
, u32 input
,
102 struct dvb_diseqc_master_cmd
*cmd
)
104 u32 tag
= DDB_LINK_TAG(link
);
107 ddbwritel(dev
, 0, tag
| LNB_BUF_LEVEL(input
));
108 for (i
= 0; i
< cmd
->msg_len
; i
++)
109 ddbwritel(dev
, cmd
->msg
[i
], tag
| LNB_BUF_WRITE(input
));
110 lnb_command(dev
, link
, input
, LNB_CMD_DISEQC
);
114 static int lnb_set_sat(struct ddb
*dev
, u32 link
, u32 input
, u32 sat
, u32 band
,
117 struct dvb_diseqc_master_cmd cmd
= {
118 .msg
= {0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00},
121 cmd
.msg
[3] = 0xf0 | (((sat
<< 2) & 0x0c) | (band
? 1 : 0) |
123 return lnb_send_diseqc(dev
, link
, input
, &cmd
);
126 static int lnb_set_tone(struct ddb
*dev
, u32 link
, u32 input
,
127 enum fe_sec_tone_mode tone
)
130 u32 mask
= (1ULL << input
);
134 if (!(dev
->link
[link
].lnb
.tone
& mask
))
136 dev
->link
[link
].lnb
.tone
&= ~(1ULL << input
);
139 if (dev
->link
[link
].lnb
.tone
& mask
)
141 dev
->link
[link
].lnb
.tone
|= (1ULL << input
);
148 s
= lnb_command(dev
, link
, input
, LNB_CMD_NOP
);
152 static int lnb_set_voltage(struct ddb
*dev
, u32 link
, u32 input
,
153 enum fe_sec_voltage voltage
)
157 if (dev
->link
[link
].lnb
.oldvoltage
[input
] == voltage
)
160 case SEC_VOLTAGE_OFF
:
161 if (dev
->link
[link
].lnb
.voltage
[input
])
163 lnb_command(dev
, link
, input
, LNB_CMD_OFF
);
166 lnb_command(dev
, link
, input
, LNB_CMD_LOW
);
169 lnb_command(dev
, link
, input
, LNB_CMD_HIGH
);
175 dev
->link
[link
].lnb
.oldvoltage
[input
] = voltage
;
179 static int max_set_input_unlocked(struct dvb_frontend
*fe
, int in
)
181 struct ddb_input
*input
= fe
->sec_priv
;
182 struct ddb_port
*port
= input
->port
;
183 struct ddb
*dev
= port
->dev
;
184 struct ddb_dvb
*dvb
= &port
->dvb
[input
->nr
& 1];
189 if (dvb
->input
!= in
) {
190 u32 bit
= (1ULL << input
->nr
);
192 dev
->link
[port
->lnr
].lnb
.voltage
[dvb
->input
& 3] & bit
;
194 dev
->link
[port
->lnr
].lnb
.voltage
[dvb
->input
& 3] &= ~bit
;
196 dev
->link
[port
->lnr
].lnb
.voltage
[dvb
->input
& 3] |= obit
;
198 res
= dvb
->set_input(fe
, in
);
202 static int max_set_tone(struct dvb_frontend
*fe
, enum fe_sec_tone_mode tone
)
204 struct ddb_input
*input
= fe
->sec_priv
;
205 struct ddb_port
*port
= input
->port
;
206 struct ddb
*dev
= port
->dev
;
207 struct ddb_dvb
*dvb
= &port
->dvb
[input
->nr
& 1];
210 u32 fmode
= dev
->link
[port
->lnr
].lnb
.fmode
;
212 mutex_lock(&dev
->link
[port
->lnr
].lnb
.lock
);
218 res
= lnb_set_tone(dev
, port
->lnr
, dvb
->input
, tone
);
223 if (dvb
->tone
== SEC_TONE_ON
)
225 if (dvb
->voltage
== SEC_VOLTAGE_18
)
228 if (dvb
->tone
== SEC_TONE_ON
)
230 if (dvb
->voltage
== SEC_VOLTAGE_18
)
233 res
= max_set_input_unlocked(fe
, tuner
);
236 mutex_unlock(&dev
->link
[port
->lnr
].lnb
.lock
);
240 static int max_set_voltage(struct dvb_frontend
*fe
, enum fe_sec_voltage voltage
)
242 struct ddb_input
*input
= fe
->sec_priv
;
243 struct ddb_port
*port
= input
->port
;
244 struct ddb
*dev
= port
->dev
;
245 struct ddb_dvb
*dvb
= &port
->dvb
[input
->nr
& 1];
247 u32 nv
, ov
= dev
->link
[port
->lnr
].lnb
.voltages
;
249 u32 fmode
= dev
->link
[port
->lnr
].lnb
.fmode
;
251 mutex_lock(&dev
->link
[port
->lnr
].lnb
.lock
);
252 dvb
->voltage
= voltage
;
259 max_set_input_unlocked(fe
, 0);
260 if (voltage
== SEC_VOLTAGE_OFF
)
261 dev
->link
[port
->lnr
].lnb
.voltage
[dvb
->input
] &=
262 ~(1ULL << input
->nr
);
264 dev
->link
[port
->lnr
].lnb
.voltage
[dvb
->input
] |=
267 res
= lnb_set_voltage(dev
, port
->lnr
, dvb
->input
, voltage
);
271 if (voltage
== SEC_VOLTAGE_OFF
)
272 dev
->link
[port
->lnr
].lnb
.voltages
&=
273 ~(1ULL << input
->nr
);
275 dev
->link
[port
->lnr
].lnb
.voltages
|=
278 nv
= dev
->link
[port
->lnr
].lnb
.voltages
;
281 if (dvb
->tone
== SEC_TONE_ON
)
283 if (dvb
->voltage
== SEC_VOLTAGE_18
)
286 if (dvb
->tone
== SEC_TONE_ON
)
288 if (dvb
->voltage
== SEC_VOLTAGE_18
)
291 res
= max_set_input_unlocked(fe
, tuner
);
340 mutex_unlock(&dev
->link
[port
->lnr
].lnb
.lock
);
344 static int max_enable_high_lnb_voltage(struct dvb_frontend
*fe
, long arg
)
349 static int max_send_burst(struct dvb_frontend
*fe
, enum fe_sec_mini_cmd burst
)
354 static int mxl_fw_read(void *priv
, u8
*buf
, u32 len
)
356 struct ddb_link
*link
= priv
;
357 struct ddb
*dev
= link
->dev
;
359 dev_info(dev
->dev
, "Read mxl_fw from link %u\n", link
->nr
);
361 return ddbridge_flashread(dev
, link
->nr
, buf
, 0xc0000, len
);
364 int ddb_lnb_init_fmode(struct ddb
*dev
, struct ddb_link
*link
, u32 fm
)
368 if (link
->lnb
.fmode
== fm
)
370 dev_info(dev
->dev
, "Set fmode link %u = %u\n", l
, fm
);
371 mutex_lock(&link
->lnb
.lock
);
372 if (fm
== 2 || fm
== 1) {
373 if (fmode_sat
>= 0) {
374 lnb_set_sat(dev
, l
, 0, fmode_sat
, 0, 0);
376 lnb_set_sat(dev
, l
, 1, fmode_sat
, 0, 1);
377 lnb_set_sat(dev
, l
, 2, fmode_sat
, 1, 0);
379 lnb_set_sat(dev
, l
, 1, fmode_sat
, 1, 0);
380 lnb_set_sat(dev
, l
, 2, fmode_sat
, 0, 1);
382 lnb_set_sat(dev
, l
, 3, fmode_sat
, 1, 1);
384 lnb_set_tone(dev
, l
, 0, SEC_TONE_OFF
);
386 lnb_set_tone(dev
, l
, 1, SEC_TONE_OFF
);
387 lnb_set_tone(dev
, l
, 2, SEC_TONE_ON
);
389 lnb_set_tone(dev
, l
, 1, SEC_TONE_ON
);
390 lnb_set_tone(dev
, l
, 2, SEC_TONE_OFF
);
392 lnb_set_tone(dev
, l
, 3, SEC_TONE_ON
);
394 link
->lnb
.fmode
= fm
;
395 mutex_unlock(&link
->lnb
.lock
);
399 static struct mxl5xx_cfg mxl5xx
= {
405 .fw_read
= mxl_fw_read
,
408 int ddb_fe_attach_mxl5xx(struct ddb_input
*input
)
410 struct ddb
*dev
= input
->port
->dev
;
411 struct i2c_adapter
*i2c
= &input
->port
->i2c
->adap
;
412 struct ddb_dvb
*dvb
= &input
->port
->dvb
[input
->nr
& 1];
413 struct ddb_port
*port
= input
->port
;
414 struct ddb_link
*link
= &dev
->link
[port
->lnr
];
415 struct mxl5xx_cfg cfg
;
420 dvb
->set_input
= NULL
;
427 dvb
->fe
= dvb_attach(mxl5xx_attach
, i2c
, &cfg
,
428 demod
, tuner
, &dvb
->set_input
);
431 dev_err(dev
->dev
, "No MXL5XX found!\n");
435 if (!dvb
->set_input
) {
436 dev_err(dev
->dev
, "No mxl5xx_set_input function pointer!\n");
441 lnb_command(dev
, port
->lnr
, input
->nr
, LNB_CMD_INIT
);
442 lnb_set_voltage(dev
, port
->lnr
, input
->nr
, SEC_VOLTAGE_OFF
);
444 ddb_lnb_init_fmode(dev
, link
, fmode
);
446 dvb
->fe
->ops
.set_voltage
= max_set_voltage
;
447 dvb
->fe
->ops
.enable_high_lnb_voltage
= max_enable_high_lnb_voltage
;
448 dvb
->fe
->ops
.set_tone
= max_set_tone
;
449 dvb
->diseqc_send_master_cmd
= dvb
->fe
->ops
.diseqc_send_master_cmd
;
450 dvb
->fe
->ops
.diseqc_send_master_cmd
= max_send_master_cmd
;
451 dvb
->fe
->ops
.diseqc_send_burst
= max_send_burst
;
452 dvb
->fe
->sec_priv
= input
;
457 /******************************************************************************/
458 /* MAX MCI related functions */
460 int ddb_fe_attach_mci(struct ddb_input
*input
, u32 type
)
462 struct ddb
*dev
= input
->port
->dev
;
463 struct ddb_dvb
*dvb
= &input
->port
->dvb
[input
->nr
& 1];
464 struct ddb_port
*port
= input
->port
;
465 struct ddb_link
*link
= &dev
->link
[port
->lnr
];
472 case DDB_TUNER_MCI_SX8
:
473 cfg
= ddb_max_sx8_cfg
;
480 dvb
->fe
= ddb_mci_attach(input
, &cfg
, demod
, &dvb
->set_input
);
482 dev_err(dev
->dev
, "No MCI card found!\n");
485 if (!dvb
->set_input
) {
486 dev_err(dev
->dev
, "No MCI set_input function pointer!\n");
490 lnb_command(dev
, port
->lnr
, input
->nr
, LNB_CMD_INIT
);
491 lnb_set_voltage(dev
, port
->lnr
, input
->nr
, SEC_VOLTAGE_OFF
);
493 ddb_lnb_init_fmode(dev
, link
, fmode
);
495 dvb
->fe
->ops
.set_voltage
= max_set_voltage
;
496 dvb
->fe
->ops
.enable_high_lnb_voltage
= max_enable_high_lnb_voltage
;
497 dvb
->fe
->ops
.set_tone
= max_set_tone
;
498 dvb
->diseqc_send_master_cmd
= dvb
->fe
->ops
.diseqc_send_master_cmd
;
499 dvb
->fe
->ops
.diseqc_send_master_cmd
= max_send_master_cmd
;
500 dvb
->fe
->ops
.diseqc_send_burst
= max_send_burst
;
501 dvb
->fe
->sec_priv
= input
;