linux/drivers/media/pci/ddbridge/ddbridge-mci.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * ddbridge-mci.c: Digital Devices microcode interface
   4 *
   5 * Copyright (C) 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 "ddbridge.h"
  20#include "ddbridge-io.h"
  21#include "ddbridge-mci.h"
  22
  23static LIST_HEAD(mci_list);
  24
  25static const u32 MCLK = (1550000000 / 12);
  26static const u32 MAX_DEMOD_LDPC_BITRATE = (1550000000 / 6);
  27static const u32 MAX_LDPC_BITRATE = (720000000);
  28
  29struct mci_base {
  30        struct list_head     mci_list;
  31        void                *key;
  32        struct ddb_link     *link;
  33        struct completion    completion;
  34
  35        struct device       *dev;
  36        struct mutex         tuner_lock; /* concurrent tuner access lock */
  37        u8                   adr;
  38        struct mutex         mci_lock; /* concurrent MCI access lock */
  39        int                  count;
  40
  41        u8                   tuner_use_count[MCI_TUNER_MAX];
  42        u8                   assigned_demod[MCI_DEMOD_MAX];
  43        u32                  used_ldpc_bitrate[MCI_DEMOD_MAX];
  44        u8                   demod_in_use[MCI_DEMOD_MAX];
  45        u32                  iq_mode;
  46};
  47
  48struct mci {
  49        struct mci_base     *base;
  50        struct dvb_frontend  fe;
  51        int                  nr;
  52        int                  demod;
  53        int                  tuner;
  54        int                  first_time_lock;
  55        int                  started;
  56        struct mci_result    signal_info;
  57
  58        u32                  bb_mode;
  59};
  60
  61static int mci_reset(struct mci *state)
  62{
  63        struct ddb_link *link = state->base->link;
  64        u32 status = 0;
  65        u32 timeout = 40;
  66
  67        ddblwritel(link, MCI_CONTROL_RESET, MCI_CONTROL);
  68        ddblwritel(link, 0, MCI_CONTROL + 4); /* 1= no internal init */
  69        msleep(300);
  70        ddblwritel(link, 0, MCI_CONTROL);
  71
  72        while (1) {
  73                status = ddblreadl(link, MCI_CONTROL);
  74                if ((status & MCI_CONTROL_READY) == MCI_CONTROL_READY)
  75                        break;
  76                if (--timeout == 0)
  77                        break;
  78                msleep(50);
  79        }
  80        if ((status & MCI_CONTROL_READY) == 0)
  81                return -1;
  82        if (link->ids.device == 0x0009)
  83                ddblwritel(link, SX8_TSCONFIG_MODE_NORMAL, SX8_TSCONFIG);
  84        return 0;
  85}
  86
  87static int mci_config(struct mci *state, u32 config)
  88{
  89        struct ddb_link *link = state->base->link;
  90
  91        if (link->ids.device != 0x0009)
  92                return -EINVAL;
  93        ddblwritel(link, config, SX8_TSCONFIG);
  94        return 0;
  95}
  96
  97static int _mci_cmd_unlocked(struct mci *state,
  98                             u32 *cmd, u32 cmd_len,
  99                             u32 *res, u32 res_len)
 100{
 101        struct ddb_link *link = state->base->link;
 102        u32 i, val;
 103        unsigned long stat;
 104
 105        val = ddblreadl(link, MCI_CONTROL);
 106        if (val & (MCI_CONTROL_RESET | MCI_CONTROL_START_COMMAND))
 107                return -EIO;
 108        if (cmd && cmd_len)
 109                for (i = 0; i < cmd_len; i++)
 110                        ddblwritel(link, cmd[i], MCI_COMMAND + i * 4);
 111        val |= (MCI_CONTROL_START_COMMAND | MCI_CONTROL_ENABLE_DONE_INTERRUPT);
 112        ddblwritel(link, val, MCI_CONTROL);
 113
 114        stat = wait_for_completion_timeout(&state->base->completion, HZ);
 115        if (stat == 0) {
 116                dev_warn(state->base->dev, "MCI-%d: MCI timeout\n", state->nr);
 117                return -EIO;
 118        }
 119        if (res && res_len)
 120                for (i = 0; i < res_len; i++)
 121                        res[i] = ddblreadl(link, MCI_RESULT + i * 4);
 122        return 0;
 123}
 124
 125static int mci_cmd(struct mci *state,
 126                   struct mci_command *command,
 127                   struct mci_result *result)
 128{
 129        int stat;
 130
 131        mutex_lock(&state->base->mci_lock);
 132        stat = _mci_cmd_unlocked(state,
 133                                 (u32 *)command, sizeof(*command) / sizeof(u32),
 134                                 (u32 *)result, sizeof(*result) / sizeof(u32));
 135        mutex_unlock(&state->base->mci_lock);
 136        return stat;
 137}
 138
 139static void mci_handler(void *priv)
 140{
 141        struct mci_base *base = (struct mci_base *)priv;
 142
 143        complete(&base->completion);
 144}
 145
 146static void release(struct dvb_frontend *fe)
 147{
 148        struct mci *state = fe->demodulator_priv;
 149
 150        state->base->count--;
 151        if (state->base->count == 0) {
 152                list_del(&state->base->mci_list);
 153                kfree(state->base);
 154        }
 155        kfree(state);
 156}
 157
 158static int read_status(struct dvb_frontend *fe, enum fe_status *status)
 159{
 160        int stat;
 161        struct mci *state = fe->demodulator_priv;
 162        struct mci_command cmd;
 163        struct mci_result res;
 164
 165        cmd.command = MCI_CMD_GETSTATUS;
 166        cmd.demod = state->demod;
 167        stat = mci_cmd(state, &cmd, &res);
 168        if (stat)
 169                return stat;
 170        *status = 0x00;
 171        if (res.status == SX8_DEMOD_WAIT_MATYPE)
 172                *status = 0x0f;
 173        if (res.status == SX8_DEMOD_LOCKED)
 174                *status = 0x1f;
 175        return stat;
 176}
 177
 178static int mci_set_tuner(struct dvb_frontend *fe, u32 tuner, u32 on)
 179{
 180        struct mci *state = fe->demodulator_priv;
 181        struct mci_command cmd;
 182
 183        memset(&cmd, 0, sizeof(cmd));
 184        cmd.tuner = state->tuner;
 185        cmd.command = on ? SX8_CMD_INPUT_ENABLE : SX8_CMD_INPUT_DISABLE;
 186        return mci_cmd(state, &cmd, NULL);
 187}
 188
 189static int stop(struct dvb_frontend *fe)
 190{
 191        struct mci *state = fe->demodulator_priv;
 192        struct mci_command cmd;
 193        u32 input = state->tuner;
 194
 195        memset(&cmd, 0, sizeof(cmd));
 196        if (state->demod != DEMOD_UNUSED) {
 197                cmd.command = MCI_CMD_STOP;
 198                cmd.demod = state->demod;
 199                mci_cmd(state, &cmd, NULL);
 200                if (state->base->iq_mode) {
 201                        cmd.command = MCI_CMD_STOP;
 202                        cmd.demod = state->demod;
 203                        cmd.output = 0;
 204                        mci_cmd(state, &cmd, NULL);
 205                        mci_config(state, SX8_TSCONFIG_MODE_NORMAL);
 206                }
 207        }
 208        mutex_lock(&state->base->tuner_lock);
 209        state->base->tuner_use_count[input]--;
 210        if (!state->base->tuner_use_count[input])
 211                mci_set_tuner(fe, input, 0);
 212        if (state->demod < MCI_DEMOD_MAX)
 213                state->base->demod_in_use[state->demod] = 0;
 214        state->base->used_ldpc_bitrate[state->nr] = 0;
 215        state->demod = DEMOD_UNUSED;
 216        state->base->assigned_demod[state->nr] = DEMOD_UNUSED;
 217        state->base->iq_mode = 0;
 218        mutex_unlock(&state->base->tuner_lock);
 219        state->started = 0;
 220        return 0;
 221}
 222
 223static int start(struct dvb_frontend *fe, u32 flags, u32 modmask, u32 ts_config)
 224{
 225        struct mci *state = fe->demodulator_priv;
 226        struct dtv_frontend_properties *p = &fe->dtv_property_cache;
 227        u32 used_ldpc_bitrate = 0, free_ldpc_bitrate;
 228        u32 used_demods = 0;
 229        struct mci_command cmd;
 230        u32 input = state->tuner;
 231        u32 bits_per_symbol = 0;
 232        int i, stat = 0;
 233
 234        if (p->symbol_rate >= (MCLK / 2))
 235                flags &= ~1;
 236        if ((flags & 3) == 0)
 237                return -EINVAL;
 238
 239        if (flags & 2) {
 240                u32 tmp = modmask;
 241
 242                bits_per_symbol = 1;
 243                while (tmp & 1) {
 244                        tmp >>= 1;
 245                        bits_per_symbol++;
 246                }
 247        }
 248
 249        mutex_lock(&state->base->tuner_lock);
 250        if (state->base->iq_mode) {
 251                stat = -EBUSY;
 252                goto unlock;
 253        }
 254        for (i = 0; i < MCI_DEMOD_MAX; i++) {
 255                used_ldpc_bitrate += state->base->used_ldpc_bitrate[i];
 256                if (state->base->demod_in_use[i])
 257                        used_demods++;
 258        }
 259        if (used_ldpc_bitrate >= MAX_LDPC_BITRATE ||
 260            ((ts_config & SX8_TSCONFIG_MODE_MASK) >
 261             SX8_TSCONFIG_MODE_NORMAL && used_demods > 0)) {
 262                stat = -EBUSY;
 263                goto unlock;
 264        }
 265        free_ldpc_bitrate = MAX_LDPC_BITRATE - used_ldpc_bitrate;
 266        if (free_ldpc_bitrate > MAX_DEMOD_LDPC_BITRATE)
 267                free_ldpc_bitrate = MAX_DEMOD_LDPC_BITRATE;
 268
 269        while (p->symbol_rate * bits_per_symbol > free_ldpc_bitrate)
 270                bits_per_symbol--;
 271
 272        if (bits_per_symbol < 2) {
 273                stat = -EBUSY;
 274                goto unlock;
 275        }
 276        i = (p->symbol_rate > (MCLK / 2)) ? 3 : 7;
 277        while (i >= 0 && state->base->demod_in_use[i])
 278                i--;
 279        if (i < 0) {
 280                stat = -EBUSY;
 281                goto unlock;
 282        }
 283        state->base->demod_in_use[i] = 1;
 284        state->base->used_ldpc_bitrate[state->nr] = p->symbol_rate
 285                                                    * bits_per_symbol;
 286        state->demod = i;
 287        state->base->assigned_demod[state->nr] = i;
 288
 289        if (!state->base->tuner_use_count[input])
 290                mci_set_tuner(fe, input, 1);
 291        state->base->tuner_use_count[input]++;
 292        state->base->iq_mode = (ts_config > 1);
 293unlock:
 294        mutex_unlock(&state->base->tuner_lock);
 295        if (stat)
 296                return stat;
 297        memset(&cmd, 0, sizeof(cmd));
 298
 299        if (state->base->iq_mode) {
 300                cmd.command = SX8_CMD_SELECT_IQOUT;
 301                cmd.demod = state->demod;
 302                cmd.output = 0;
 303                mci_cmd(state, &cmd, NULL);
 304                mci_config(state, ts_config);
 305        }
 306        if (p->stream_id != NO_STREAM_ID_FILTER && p->stream_id != 0x80000000)
 307                flags |= 0x80;
 308        dev_dbg(state->base->dev, "MCI-%d: tuner=%d demod=%d\n",
 309                state->nr, state->tuner, state->demod);
 310        cmd.command = MCI_CMD_SEARCH_DVBS;
 311        cmd.dvbs2_search.flags = flags;
 312        cmd.dvbs2_search.s2_modulation_mask =
 313                modmask & ((1 << (bits_per_symbol - 1)) - 1);
 314        cmd.dvbs2_search.retry = 2;
 315        cmd.dvbs2_search.frequency = p->frequency * 1000;
 316        cmd.dvbs2_search.symbol_rate = p->symbol_rate;
 317        cmd.dvbs2_search.scrambling_sequence_index =
 318                p->scrambling_sequence_index;
 319        cmd.dvbs2_search.input_stream_id =
 320                (p->stream_id != NO_STREAM_ID_FILTER) ? p->stream_id : 0;
 321        cmd.tuner = state->tuner;
 322        cmd.demod = state->demod;
 323        cmd.output = state->nr;
 324        if (p->stream_id == 0x80000000)
 325                cmd.output |= 0x80;
 326        stat = mci_cmd(state, &cmd, NULL);
 327        if (stat)
 328                stop(fe);
 329        return stat;
 330}
 331
 332static int start_iq(struct dvb_frontend *fe, u32 ts_config)
 333{
 334        struct mci *state = fe->demodulator_priv;
 335        struct dtv_frontend_properties *p = &fe->dtv_property_cache;
 336        u32 used_demods = 0;
 337        struct mci_command cmd;
 338        u32 input = state->tuner;
 339        int i, stat = 0;
 340
 341        mutex_lock(&state->base->tuner_lock);
 342        if (state->base->iq_mode) {
 343                stat = -EBUSY;
 344                goto unlock;
 345        }
 346        for (i = 0; i < MCI_DEMOD_MAX; i++)
 347                if (state->base->demod_in_use[i])
 348                        used_demods++;
 349        if (used_demods > 0) {
 350                stat = -EBUSY;
 351                goto unlock;
 352        }
 353        state->demod = 0;
 354        state->base->assigned_demod[state->nr] = 0;
 355        if (!state->base->tuner_use_count[input])
 356                mci_set_tuner(fe, input, 1);
 357        state->base->tuner_use_count[input]++;
 358        state->base->iq_mode = (ts_config > 1);
 359unlock:
 360        mutex_unlock(&state->base->tuner_lock);
 361        if (stat)
 362                return stat;
 363
 364        memset(&cmd, 0, sizeof(cmd));
 365        cmd.command = SX8_CMD_START_IQ;
 366        cmd.dvbs2_search.frequency = p->frequency * 1000;
 367        cmd.dvbs2_search.symbol_rate = p->symbol_rate;
 368        cmd.tuner = state->tuner;
 369        cmd.demod = state->demod;
 370        cmd.output = 7;
 371        mci_config(state, ts_config);
 372        stat = mci_cmd(state, &cmd, NULL);
 373        if (stat)
 374                stop(fe);
 375        return stat;
 376}
 377
 378static int set_parameters(struct dvb_frontend *fe)
 379{
 380        int stat = 0;
 381        struct mci *state = fe->demodulator_priv;
 382        struct dtv_frontend_properties *p = &fe->dtv_property_cache;
 383        u32 ts_config, iq_mode = 0, isi;
 384
 385        if (state->started)
 386                stop(fe);
 387
 388        isi = p->stream_id;
 389        if (isi != NO_STREAM_ID_FILTER)
 390                iq_mode = (isi & 0x30000000) >> 28;
 391
 392        switch (iq_mode) {
 393        case 1:
 394                ts_config = (SX8_TSCONFIG_TSHEADER | SX8_TSCONFIG_MODE_IQ);
 395                break;
 396        case 2:
 397                ts_config = (SX8_TSCONFIG_TSHEADER | SX8_TSCONFIG_MODE_IQ);
 398                break;
 399        default:
 400                ts_config = SX8_TSCONFIG_MODE_NORMAL;
 401                break;
 402        }
 403
 404        if (iq_mode != 2) {
 405                u32 flags = 3;
 406                u32 mask = 3;
 407
 408                if (p->modulation == APSK_16 ||
 409                    p->modulation == APSK_32) {
 410                        flags = 2;
 411                        mask = 15;
 412                }
 413                stat = start(fe, flags, mask, ts_config);
 414        } else {
 415                stat = start_iq(fe, ts_config);
 416        }
 417
 418        if (!stat) {
 419                state->started = 1;
 420                state->first_time_lock = 1;
 421                state->signal_info.status = SX8_DEMOD_WAIT_SIGNAL;
 422        }
 423
 424        return stat;
 425}
 426
 427static int tune(struct dvb_frontend *fe, bool re_tune,
 428                unsigned int mode_flags,
 429                unsigned int *delay, enum fe_status *status)
 430{
 431        int r;
 432
 433        if (re_tune) {
 434                r = set_parameters(fe);
 435                if (r)
 436                        return r;
 437        }
 438        r = read_status(fe, status);
 439        if (r)
 440                return r;
 441
 442        if (*status & FE_HAS_LOCK)
 443                return 0;
 444        *delay = HZ / 10;
 445        return 0;
 446}
 447
 448static enum dvbfe_algo get_algo(struct dvb_frontend *fe)
 449{
 450        return DVBFE_ALGO_HW;
 451}
 452
 453static int set_input(struct dvb_frontend *fe, int input)
 454{
 455        struct mci *state = fe->demodulator_priv;
 456
 457        state->tuner = input;
 458        dev_dbg(state->base->dev, "MCI-%d: input=%d\n", state->nr, input);
 459        return 0;
 460}
 461
 462static struct dvb_frontend_ops mci_ops = {
 463        .delsys = { SYS_DVBS, SYS_DVBS2 },
 464        .info = {
 465                .name                   = "Digital Devices MaxSX8 MCI DVB-S/S2/S2X",
 466                .frequency_min          = 950000,
 467                .frequency_max          = 2150000,
 468                .frequency_stepsize     = 0,
 469                .frequency_tolerance    = 0,
 470                .symbol_rate_min        = 100000,
 471                .symbol_rate_max        = 100000000,
 472                .caps                   = FE_CAN_INVERSION_AUTO |
 473                                          FE_CAN_FEC_AUTO       |
 474                                          FE_CAN_QPSK           |
 475                                          FE_CAN_2G_MODULATION  |
 476                                          FE_CAN_MULTISTREAM,
 477        },
 478        .get_frontend_algo              = get_algo,
 479        .tune                           = tune,
 480        .release                        = release,
 481        .read_status                    = read_status,
 482};
 483
 484static struct mci_base *match_base(void *key)
 485{
 486        struct mci_base *p;
 487
 488        list_for_each_entry(p, &mci_list, mci_list)
 489                if (p->key == key)
 490                        return p;
 491        return NULL;
 492}
 493
 494static int probe(struct mci *state)
 495{
 496        mci_reset(state);
 497        return 0;
 498}
 499
 500struct dvb_frontend
 501*ddb_mci_attach(struct ddb_input *input,
 502                int mci_type, int nr,
 503                int (**fn_set_input)(struct dvb_frontend *fe, int input))
 504{
 505        struct ddb_port *port = input->port;
 506        struct ddb *dev = port->dev;
 507        struct ddb_link *link = &dev->link[port->lnr];
 508        struct mci_base *base;
 509        struct mci *state;
 510        void *key = mci_type ? (void *)port : (void *)link;
 511
 512        state = kzalloc(sizeof(*state), GFP_KERNEL);
 513        if (!state)
 514                return NULL;
 515
 516        base = match_base(key);
 517        if (base) {
 518                base->count++;
 519                state->base = base;
 520        } else {
 521                base = kzalloc(sizeof(*base), GFP_KERNEL);
 522                if (!base)
 523                        goto fail;
 524                base->key = key;
 525                base->count = 1;
 526                base->link = link;
 527                base->dev = dev->dev;
 528                mutex_init(&base->mci_lock);
 529                mutex_init(&base->tuner_lock);
 530                ddb_irq_set(dev, link->nr, 0, mci_handler, base);
 531                init_completion(&base->completion);
 532                state->base = base;
 533                if (probe(state) < 0) {
 534                        kfree(base);
 535                        goto fail;
 536                }
 537                list_add(&base->mci_list, &mci_list);
 538        }
 539        state->fe.ops = mci_ops;
 540        state->fe.demodulator_priv = state;
 541        state->nr = nr;
 542        *fn_set_input = set_input;
 543
 544        state->tuner = nr;
 545        state->demod = nr;
 546
 547        return &state->fe;
 548fail:
 549        kfree(state);
 550        return NULL;
 551}
 552