linux/drivers/media/pci/ddbridge/ddbridge-max.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * ddbridge-max.c: Digital Devices bridge MAX card support
   4 *
   5 * Copyright (C) 2010-2017 Digital Devices GmbH
   6 *                         Ralph Metzler <rjkm@metzlerbros.de>
   7 *                         Marcus Metzler <mocm@metzlerbros.de>
   8 *
   9 * This program is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU General Public License
  11 * version 2 only, as published by the Free Software Foundation.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 */
  18
  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>
  25#include <linux/io.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>
  32
  33#include "ddbridge.h"
  34#include "ddbridge-regs.h"
  35#include "ddbridge-io.h"
  36#include "ddbridge-mci.h"
  37
  38#include "ddbridge-max.h"
  39#include "mxl5xx.h"
  40
  41/******************************************************************************/
  42
  43/* MaxS4/8 related modparams */
  44static int fmode;
  45module_param(fmode, int, 0444);
  46MODULE_PARM_DESC(fmode, "frontend emulation mode");
  47
  48static int fmode_sat = -1;
  49module_param(fmode_sat, int, 0444);
  50MODULE_PARM_DESC(fmode_sat, "set frontend emulation mode sat");
  51
  52static int old_quattro;
  53module_param(old_quattro, int, 0444);
  54MODULE_PARM_DESC(old_quattro, "old quattro LNB input order ");
  55
  56/******************************************************************************/
  57
  58static int lnb_command(struct ddb *dev, u32 link, u32 lnb, u32 cmd)
  59{
  60        u32 c, v = 0, tag = DDB_LINK_TAG(link);
  61
  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)
  67                        break;
  68                msleep(20);
  69        }
  70        if (c == 10)
  71                dev_info(dev->dev, "%s lnb = %08x  cmd = %08x\n",
  72                         __func__, lnb, cmd);
  73        return 0;
  74}
  75
  76static int max_send_master_cmd(struct dvb_frontend *fe,
  77                               struct dvb_diseqc_master_cmd *cmd)
  78{
  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);
  84        int i;
  85        u32 fmode = dev->link[port->lnr].lnb.fmode;
  86
  87        if (fmode == 2 || fmode == 1)
  88                return 0;
  89        if (dvb->diseqc_send_master_cmd)
  90                dvb->diseqc_send_master_cmd(fe, cmd);
  91
  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);
  98        return 0;
  99}
 100
 101static int lnb_send_diseqc(struct ddb *dev, u32 link, u32 input,
 102                           struct dvb_diseqc_master_cmd *cmd)
 103{
 104        u32 tag = DDB_LINK_TAG(link);
 105        int i;
 106
 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);
 111        return 0;
 112}
 113
 114static int lnb_set_sat(struct ddb *dev, u32 link, u32 input, u32 sat, u32 band,
 115                       u32 hor)
 116{
 117        struct dvb_diseqc_master_cmd cmd = {
 118                .msg = {0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00},
 119                .msg_len = 4
 120        };
 121        cmd.msg[3] = 0xf0 | (((sat << 2) & 0x0c) | (band ? 1 : 0) |
 122                (hor ? 2 : 0));
 123        return lnb_send_diseqc(dev, link, input, &cmd);
 124}
 125
 126static int lnb_set_tone(struct ddb *dev, u32 link, u32 input,
 127                        enum fe_sec_tone_mode tone)
 128{
 129        int s = 0;
 130        u32 mask = (1ULL << input);
 131
 132        switch (tone) {
 133        case SEC_TONE_OFF:
 134                if (!(dev->link[link].lnb.tone & mask))
 135                        return 0;
 136                dev->link[link].lnb.tone &= ~(1ULL << input);
 137                break;
 138        case SEC_TONE_ON:
 139                if (dev->link[link].lnb.tone & mask)
 140                        return 0;
 141                dev->link[link].lnb.tone |= (1ULL << input);
 142                break;
 143        default:
 144                s = -EINVAL;
 145                break;
 146        }
 147        if (!s)
 148                s = lnb_command(dev, link, input, LNB_CMD_NOP);
 149        return s;
 150}
 151
 152static int lnb_set_voltage(struct ddb *dev, u32 link, u32 input,
 153                           enum fe_sec_voltage voltage)
 154{
 155        int s = 0;
 156
 157        if (dev->link[link].lnb.oldvoltage[input] == voltage)
 158                return 0;
 159        switch (voltage) {
 160        case SEC_VOLTAGE_OFF:
 161                if (dev->link[link].lnb.voltage[input])
 162                        return 0;
 163                lnb_command(dev, link, input, LNB_CMD_OFF);
 164                break;
 165        case SEC_VOLTAGE_13:
 166                lnb_command(dev, link, input, LNB_CMD_LOW);
 167                break;
 168        case SEC_VOLTAGE_18:
 169                lnb_command(dev, link, input, LNB_CMD_HIGH);
 170                break;
 171        default:
 172                s = -EINVAL;
 173                break;
 174        }
 175        dev->link[link].lnb.oldvoltage[input] = voltage;
 176        return s;
 177}
 178
 179static int max_set_input_unlocked(struct dvb_frontend *fe, int in)
 180{
 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];
 185        int res = 0;
 186
 187        if (in > 3)
 188                return -EINVAL;
 189        if (dvb->input != in) {
 190                u32 bit = (1ULL << input->nr);
 191                u32 obit =
 192                        dev->link[port->lnr].lnb.voltage[dvb->input & 3] & bit;
 193
 194                dev->link[port->lnr].lnb.voltage[dvb->input & 3] &= ~bit;
 195                dvb->input = in;
 196                dev->link[port->lnr].lnb.voltage[dvb->input & 3] |= obit;
 197        }
 198        res = dvb->set_input(fe, in);
 199        return res;
 200}
 201
 202static int max_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
 203{
 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];
 208        int tuner = 0;
 209        int res = 0;
 210        u32 fmode = dev->link[port->lnr].lnb.fmode;
 211
 212        mutex_lock(&dev->link[port->lnr].lnb.lock);
 213        dvb->tone = tone;
 214        switch (fmode) {
 215        default:
 216        case 0:
 217        case 3:
 218                res = lnb_set_tone(dev, port->lnr, dvb->input, tone);
 219                break;
 220        case 1:
 221        case 2:
 222                if (old_quattro) {
 223                        if (dvb->tone == SEC_TONE_ON)
 224                                tuner |= 2;
 225                        if (dvb->voltage == SEC_VOLTAGE_18)
 226                                tuner |= 1;
 227                } else {
 228                        if (dvb->tone == SEC_TONE_ON)
 229                                tuner |= 1;
 230                        if (dvb->voltage == SEC_VOLTAGE_18)
 231                                tuner |= 2;
 232                }
 233                res = max_set_input_unlocked(fe, tuner);
 234                break;
 235        }
 236        mutex_unlock(&dev->link[port->lnr].lnb.lock);
 237        return res;
 238}
 239
 240static int max_set_voltage(struct dvb_frontend *fe, enum fe_sec_voltage voltage)
 241{
 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];
 246        int tuner = 0;
 247        u32 nv, ov = dev->link[port->lnr].lnb.voltages;
 248        int res = 0;
 249        u32 fmode = dev->link[port->lnr].lnb.fmode;
 250
 251        mutex_lock(&dev->link[port->lnr].lnb.lock);
 252        dvb->voltage = voltage;
 253
 254        switch (fmode) {
 255        case 3:
 256        default:
 257        case 0:
 258                if (fmode == 3)
 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);
 263                else
 264                        dev->link[port->lnr].lnb.voltage[dvb->input] |=
 265                                (1ULL << input->nr);
 266
 267                res = lnb_set_voltage(dev, port->lnr, dvb->input, voltage);
 268                break;
 269        case 1:
 270        case 2:
 271                if (voltage == SEC_VOLTAGE_OFF)
 272                        dev->link[port->lnr].lnb.voltages &=
 273                                ~(1ULL << input->nr);
 274                else
 275                        dev->link[port->lnr].lnb.voltages |=
 276                                (1ULL << input->nr);
 277
 278                nv = dev->link[port->lnr].lnb.voltages;
 279
 280                if (old_quattro) {
 281                        if (dvb->tone == SEC_TONE_ON)
 282                                tuner |= 2;
 283                        if (dvb->voltage == SEC_VOLTAGE_18)
 284                                tuner |= 1;
 285                } else {
 286                        if (dvb->tone == SEC_TONE_ON)
 287                                tuner |= 1;
 288                        if (dvb->voltage == SEC_VOLTAGE_18)
 289                                tuner |= 2;
 290                }
 291                res = max_set_input_unlocked(fe, tuner);
 292
 293                if (nv != ov) {
 294                        if (nv) {
 295                                lnb_set_voltage(
 296                                        dev, port->lnr,
 297                                        0, SEC_VOLTAGE_13);
 298                                if (fmode == 1) {
 299                                        lnb_set_voltage(
 300                                                dev, port->lnr,
 301                                                0, SEC_VOLTAGE_13);
 302                                        if (old_quattro) {
 303                                                lnb_set_voltage(
 304                                                        dev, port->lnr,
 305                                                        1, SEC_VOLTAGE_18);
 306                                                lnb_set_voltage(
 307                                                        dev, port->lnr,
 308                                                        2, SEC_VOLTAGE_13);
 309                                        } else {
 310                                                lnb_set_voltage(
 311                                                        dev, port->lnr,
 312                                                        1, SEC_VOLTAGE_13);
 313                                                lnb_set_voltage(
 314                                                        dev, port->lnr,
 315                                                        2, SEC_VOLTAGE_18);
 316                                        }
 317                                        lnb_set_voltage(
 318                                                dev, port->lnr,
 319                                                3, SEC_VOLTAGE_18);
 320                                }
 321                        } else {
 322                                lnb_set_voltage(
 323                                        dev, port->lnr,
 324                                        0, SEC_VOLTAGE_OFF);
 325                                if (fmode == 1) {
 326                                        lnb_set_voltage(
 327                                                dev, port->lnr,
 328                                                1, SEC_VOLTAGE_OFF);
 329                                        lnb_set_voltage(
 330                                                dev, port->lnr,
 331                                                2, SEC_VOLTAGE_OFF);
 332                                        lnb_set_voltage(
 333                                                dev, port->lnr,
 334                                                3, SEC_VOLTAGE_OFF);
 335                                }
 336                        }
 337                }
 338                break;
 339        }
 340        mutex_unlock(&dev->link[port->lnr].lnb.lock);
 341        return res;
 342}
 343
 344static int max_enable_high_lnb_voltage(struct dvb_frontend *fe, long arg)
 345{
 346        return 0;
 347}
 348
 349static int max_send_burst(struct dvb_frontend *fe, enum fe_sec_mini_cmd burst)
 350{
 351        return 0;
 352}
 353
 354static int mxl_fw_read(void *priv, u8 *buf, u32 len)
 355{
 356        struct ddb_link *link = priv;
 357        struct ddb *dev = link->dev;
 358
 359        dev_info(dev->dev, "Read mxl_fw from link %u\n", link->nr);
 360
 361        return ddbridge_flashread(dev, link->nr, buf, 0xc0000, len);
 362}
 363
 364int ddb_lnb_init_fmode(struct ddb *dev, struct ddb_link *link, u32 fm)
 365{
 366        u32 l = link->nr;
 367
 368        if (link->lnb.fmode == fm)
 369                return 0;
 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);
 375                        if (old_quattro) {
 376                                lnb_set_sat(dev, l, 1, fmode_sat, 0, 1);
 377                                lnb_set_sat(dev, l, 2, fmode_sat, 1, 0);
 378                        } else {
 379                                lnb_set_sat(dev, l, 1, fmode_sat, 1, 0);
 380                                lnb_set_sat(dev, l, 2, fmode_sat, 0, 1);
 381                        }
 382                        lnb_set_sat(dev, l, 3, fmode_sat, 1, 1);
 383                }
 384                lnb_set_tone(dev, l, 0, SEC_TONE_OFF);
 385                if (old_quattro) {
 386                        lnb_set_tone(dev, l, 1, SEC_TONE_OFF);
 387                        lnb_set_tone(dev, l, 2, SEC_TONE_ON);
 388                } else {
 389                        lnb_set_tone(dev, l, 1, SEC_TONE_ON);
 390                        lnb_set_tone(dev, l, 2, SEC_TONE_OFF);
 391                }
 392                lnb_set_tone(dev, l, 3, SEC_TONE_ON);
 393        }
 394        link->lnb.fmode = fm;
 395        mutex_unlock(&link->lnb.lock);
 396        return 0;
 397}
 398
 399static struct mxl5xx_cfg mxl5xx = {
 400        .adr      = 0x60,
 401        .type     = 0x01,
 402        .clk      = 27000000,
 403        .ts_clk   = 139,
 404        .cap      = 12,
 405        .fw_read  = mxl_fw_read,
 406};
 407
 408int ddb_fe_attach_mxl5xx(struct ddb_input *input)
 409{
 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;
 416        int demod, tuner;
 417
 418        cfg = mxl5xx;
 419        cfg.fw_priv = link;
 420        dvb->set_input = NULL;
 421
 422        demod = input->nr;
 423        tuner = demod & 3;
 424        if (fmode == 3)
 425                tuner = 0;
 426
 427        dvb->fe = dvb_attach(mxl5xx_attach, i2c, &cfg,
 428                             demod, tuner, &dvb->set_input);
 429
 430        if (!dvb->fe) {
 431                dev_err(dev->dev, "No MXL5XX found!\n");
 432                return -ENODEV;
 433        }
 434
 435        if (!dvb->set_input) {
 436                dev_err(dev->dev, "No mxl5xx_set_input function pointer!\n");
 437                return -ENODEV;
 438        }
 439
 440        if (input->nr < 4) {
 441                lnb_command(dev, port->lnr, input->nr, LNB_CMD_INIT);
 442                lnb_set_voltage(dev, port->lnr, input->nr, SEC_VOLTAGE_OFF);
 443        }
 444        ddb_lnb_init_fmode(dev, link, fmode);
 445
 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;
 453        dvb->input = tuner;
 454        return 0;
 455}
 456
 457/******************************************************************************/
 458/* MAX MCI related functions */
 459
 460int ddb_fe_attach_mci(struct ddb_input *input, u32 type)
 461{
 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];
 466        int demod, tuner;
 467        struct mci_cfg cfg;
 468
 469        demod = input->nr;
 470        tuner = demod & 3;
 471        switch (type) {
 472        case DDB_TUNER_MCI_SX8:
 473                cfg = ddb_max_sx8_cfg;
 474                if (fmode == 3)
 475                        tuner = 0;
 476                break;
 477        default:
 478                return -EINVAL;
 479        }
 480        dvb->fe = ddb_mci_attach(input, &cfg, demod, &dvb->set_input);
 481        if (!dvb->fe) {
 482                dev_err(dev->dev, "No MCI card found!\n");
 483                return -ENODEV;
 484        }
 485        if (!dvb->set_input) {
 486                dev_err(dev->dev, "No MCI set_input function pointer!\n");
 487                return -ENODEV;
 488        }
 489        if (input->nr < 4) {
 490                lnb_command(dev, port->lnr, input->nr, LNB_CMD_INIT);
 491                lnb_set_voltage(dev, port->lnr, input->nr, SEC_VOLTAGE_OFF);
 492        }
 493        ddb_lnb_init_fmode(dev, link, fmode);
 494
 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;
 502        dvb->input = tuner;
 503        return 0;
 504}
 505