linux/drivers/staging/media/mn88473/mn88473.c
<<
>>
Prefs
   1/*
   2 * Panasonic MN88473 DVB-T/T2/C demodulator driver
   3 *
   4 * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
   5 *
   6 *    This program is free software; you can redistribute it and/or modify
   7 *    it under the terms of the GNU General Public License as published by
   8 *    the Free Software Foundation; either version 2 of the License, or
   9 *    (at your option) any later version.
  10 *
  11 *    This program is distributed in the hope that it will be useful,
  12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 *    GNU General Public License for more details.
  15 */
  16
  17#include "mn88473_priv.h"
  18
  19static int mn88473_get_tune_settings(struct dvb_frontend *fe,
  20                                     struct dvb_frontend_tune_settings *s)
  21{
  22        s->min_delay_ms = 1000;
  23        return 0;
  24}
  25
  26static int mn88473_set_frontend(struct dvb_frontend *fe)
  27{
  28        struct i2c_client *client = fe->demodulator_priv;
  29        struct mn88473_dev *dev = i2c_get_clientdata(client);
  30        struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  31        int ret, i;
  32        u32 if_frequency;
  33        u64 tmp;
  34        u8 delivery_system_val, if_val[3], bw_val[7];
  35
  36        dev_dbg(&client->dev,
  37                "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%d stream_id=%d\n",
  38                c->delivery_system,
  39                c->modulation,
  40                c->frequency,
  41                c->bandwidth_hz,
  42                c->symbol_rate,
  43                c->inversion,
  44                c->stream_id);
  45
  46        if (!dev->warm) {
  47                ret = -EAGAIN;
  48                goto err;
  49        }
  50
  51        switch (c->delivery_system) {
  52        case SYS_DVBT:
  53                delivery_system_val = 0x02;
  54                break;
  55        case SYS_DVBT2:
  56                delivery_system_val = 0x03;
  57                break;
  58        case SYS_DVBC_ANNEX_A:
  59                delivery_system_val = 0x04;
  60                break;
  61        default:
  62                ret = -EINVAL;
  63                goto err;
  64        }
  65
  66        if (c->bandwidth_hz <= 6000000) {
  67                memcpy(bw_val, "\xe9\x55\x55\x1c\x29\x1c\x29", 7);
  68        } else if (c->bandwidth_hz <= 7000000) {
  69                memcpy(bw_val, "\xc8\x00\x00\x17\x0a\x17\x0a", 7);
  70        } else if (c->bandwidth_hz <= 8000000) {
  71                memcpy(bw_val, "\xaf\x00\x00\x11\xec\x11\xec", 7);
  72        } else {
  73                ret = -EINVAL;
  74                goto err;
  75        }
  76
  77        /* program tuner */
  78        if (fe->ops.tuner_ops.set_params) {
  79                ret = fe->ops.tuner_ops.set_params(fe);
  80                if (ret)
  81                        goto err;
  82        }
  83
  84        if (fe->ops.tuner_ops.get_if_frequency) {
  85                ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
  86                if (ret)
  87                        goto err;
  88
  89                dev_dbg(&client->dev, "get_if_frequency=%d\n", if_frequency);
  90        } else {
  91                if_frequency = 0;
  92        }
  93
  94        /* Calculate IF registers ( (1<<24)*IF / Xtal ) */
  95        tmp =  div_u64(if_frequency * (u64)(1<<24) + (dev->xtal / 2),
  96                                   dev->xtal);
  97        if_val[0] = ((tmp >> 16) & 0xff);
  98        if_val[1] = ((tmp >>  8) & 0xff);
  99        if_val[2] = ((tmp >>  0) & 0xff);
 100
 101        ret = regmap_write(dev->regmap[2], 0x05, 0x00);
 102        ret = regmap_write(dev->regmap[2], 0xfb, 0x13);
 103        ret = regmap_write(dev->regmap[2], 0xef, 0x13);
 104        ret = regmap_write(dev->regmap[2], 0xf9, 0x13);
 105        ret = regmap_write(dev->regmap[2], 0x00, 0x18);
 106        ret = regmap_write(dev->regmap[2], 0x01, 0x01);
 107        ret = regmap_write(dev->regmap[2], 0x02, 0x21);
 108        ret = regmap_write(dev->regmap[2], 0x03, delivery_system_val);
 109        ret = regmap_write(dev->regmap[2], 0x0b, 0x00);
 110
 111        for (i = 0; i < sizeof(if_val); i++) {
 112                ret = regmap_write(dev->regmap[2], 0x10 + i, if_val[i]);
 113                if (ret)
 114                        goto err;
 115        }
 116
 117        for (i = 0; i < sizeof(bw_val); i++) {
 118                ret = regmap_write(dev->regmap[2], 0x13 + i, bw_val[i]);
 119                if (ret)
 120                        goto err;
 121        }
 122
 123        ret = regmap_write(dev->regmap[2], 0x2d, 0x3b);
 124        ret = regmap_write(dev->regmap[2], 0x2e, 0x00);
 125        ret = regmap_write(dev->regmap[2], 0x56, 0x0d);
 126        ret = regmap_write(dev->regmap[0], 0x01, 0xba);
 127        ret = regmap_write(dev->regmap[0], 0x02, 0x13);
 128        ret = regmap_write(dev->regmap[0], 0x03, 0x80);
 129        ret = regmap_write(dev->regmap[0], 0x04, 0xba);
 130        ret = regmap_write(dev->regmap[0], 0x05, 0x91);
 131        ret = regmap_write(dev->regmap[0], 0x07, 0xe7);
 132        ret = regmap_write(dev->regmap[0], 0x08, 0x28);
 133        ret = regmap_write(dev->regmap[0], 0x0a, 0x1a);
 134        ret = regmap_write(dev->regmap[0], 0x13, 0x1f);
 135        ret = regmap_write(dev->regmap[0], 0x19, 0x03);
 136        ret = regmap_write(dev->regmap[0], 0x1d, 0xb0);
 137        ret = regmap_write(dev->regmap[0], 0x2a, 0x72);
 138        ret = regmap_write(dev->regmap[0], 0x2d, 0x00);
 139        ret = regmap_write(dev->regmap[0], 0x3c, 0x00);
 140        ret = regmap_write(dev->regmap[0], 0x3f, 0xf8);
 141        ret = regmap_write(dev->regmap[0], 0x40, 0xf4);
 142        ret = regmap_write(dev->regmap[0], 0x41, 0x08);
 143        ret = regmap_write(dev->regmap[0], 0xd2, 0x29);
 144        ret = regmap_write(dev->regmap[0], 0xd4, 0x55);
 145        ret = regmap_write(dev->regmap[1], 0x10, 0x10);
 146        ret = regmap_write(dev->regmap[1], 0x11, 0xab);
 147        ret = regmap_write(dev->regmap[1], 0x12, 0x0d);
 148        ret = regmap_write(dev->regmap[1], 0x13, 0xae);
 149        ret = regmap_write(dev->regmap[1], 0x14, 0x1d);
 150        ret = regmap_write(dev->regmap[1], 0x15, 0x9d);
 151        ret = regmap_write(dev->regmap[1], 0xbe, 0x08);
 152        ret = regmap_write(dev->regmap[2], 0x09, 0x08);
 153        ret = regmap_write(dev->regmap[2], 0x08, 0x1d);
 154        ret = regmap_write(dev->regmap[0], 0xb2, 0x37);
 155        ret = regmap_write(dev->regmap[0], 0xd7, 0x04);
 156        ret = regmap_write(dev->regmap[2], 0x32, 0x80);
 157        ret = regmap_write(dev->regmap[2], 0x36, 0x00);
 158        ret = regmap_write(dev->regmap[2], 0xf8, 0x9f);
 159        if (ret)
 160                goto err;
 161
 162        dev->delivery_system = c->delivery_system;
 163
 164        return 0;
 165err:
 166        dev_dbg(&client->dev, "failed=%d\n", ret);
 167        return ret;
 168}
 169
 170static int mn88473_read_status(struct dvb_frontend *fe, enum fe_status *status)
 171{
 172        struct i2c_client *client = fe->demodulator_priv;
 173        struct mn88473_dev *dev = i2c_get_clientdata(client);
 174        struct dtv_frontend_properties *c = &fe->dtv_property_cache;
 175        int ret;
 176        unsigned int utmp;
 177        int lock = 0;
 178
 179        *status = 0;
 180
 181        if (!dev->warm) {
 182                ret = -EAGAIN;
 183                goto err;
 184        }
 185
 186        switch (c->delivery_system) {
 187        case SYS_DVBT:
 188                ret = regmap_read(dev->regmap[0], 0x62, &utmp);
 189                if (ret)
 190                        goto err;
 191                if (!(utmp & 0xA0)) {
 192                        if ((utmp & 0xF) >= 0x03)
 193                                *status |= FE_HAS_SIGNAL;
 194                        if ((utmp & 0xF) >= 0x09)
 195                                lock = 1;
 196                }
 197                break;
 198        case SYS_DVBT2:
 199                ret = regmap_read(dev->regmap[2], 0x8B, &utmp);
 200                if (ret)
 201                        goto err;
 202                if (!(utmp & 0x40)) {
 203                        if ((utmp & 0xF) >= 0x07)
 204                                *status |= FE_HAS_SIGNAL;
 205                        if ((utmp & 0xF) >= 0x0a)
 206                                *status |= FE_HAS_CARRIER;
 207                        if ((utmp & 0xF) >= 0x0d)
 208                                *status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
 209                }
 210                break;
 211        case SYS_DVBC_ANNEX_A:
 212                ret = regmap_read(dev->regmap[1], 0x85, &utmp);
 213                if (ret)
 214                        goto err;
 215                if (!(utmp & 0x40)) {
 216                        ret = regmap_read(dev->regmap[1], 0x89, &utmp);
 217                        if (ret)
 218                                goto err;
 219                        if (utmp & 0x01)
 220                                lock = 1;
 221                }
 222                break;
 223        default:
 224                ret = -EINVAL;
 225                goto err;
 226        }
 227
 228        if (lock)
 229                *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
 230                                FE_HAS_SYNC | FE_HAS_LOCK;
 231
 232        return 0;
 233err:
 234        dev_dbg(&client->dev, "failed=%d\n", ret);
 235        return ret;
 236}
 237
 238static int mn88473_init(struct dvb_frontend *fe)
 239{
 240        struct i2c_client *client = fe->demodulator_priv;
 241        struct mn88473_dev *dev = i2c_get_clientdata(client);
 242        int ret, len, remaining;
 243        const struct firmware *fw = NULL;
 244        u8 *fw_file = MN88473_FIRMWARE;
 245        unsigned int tmp;
 246
 247        dev_dbg(&client->dev, "\n");
 248
 249        /* set cold state by default */
 250        dev->warm = false;
 251
 252        /* check if firmware is already running */
 253        ret = regmap_read(dev->regmap[0], 0xf5, &tmp);
 254        if (ret)
 255                goto err;
 256
 257        if (!(tmp & 0x1)) {
 258                dev_info(&client->dev, "firmware already running\n");
 259                dev->warm = true;
 260                return 0;
 261        }
 262
 263        /* request the firmware, this will block and timeout */
 264        ret = request_firmware(&fw, fw_file, &client->dev);
 265        if (ret) {
 266                dev_err(&client->dev, "firmare file '%s' not found\n", fw_file);
 267                goto err_request_firmware;
 268        }
 269
 270        dev_info(&client->dev, "downloading firmware from file '%s'\n",
 271                 fw_file);
 272
 273        ret = regmap_write(dev->regmap[0], 0xf5, 0x03);
 274        if (ret)
 275                goto err;
 276
 277        for (remaining = fw->size; remaining > 0;
 278                        remaining -= (dev->i2c_wr_max - 1)) {
 279                len = remaining;
 280                if (len > (dev->i2c_wr_max - 1))
 281                        len = dev->i2c_wr_max - 1;
 282
 283                ret = regmap_bulk_write(dev->regmap[0], 0xf6,
 284                                        &fw->data[fw->size - remaining], len);
 285                if (ret) {
 286                        dev_err(&client->dev, "firmware download failed=%d\n",
 287                                ret);
 288                        goto err;
 289                }
 290        }
 291
 292        /* parity check of firmware */
 293        ret = regmap_read(dev->regmap[0], 0xf8, &tmp);
 294        if (ret) {
 295                dev_err(&client->dev,
 296                                "parity reg read failed=%d\n", ret);
 297                goto err;
 298        }
 299        if (tmp & 0x10) {
 300                dev_err(&client->dev,
 301                                "firmware parity check failed=0x%x\n", tmp);
 302                goto err;
 303        }
 304        dev_err(&client->dev, "firmware parity check succeeded=0x%x\n", tmp);
 305
 306        ret = regmap_write(dev->regmap[0], 0xf5, 0x00);
 307        if (ret)
 308                goto err;
 309
 310        release_firmware(fw);
 311        fw = NULL;
 312
 313        /* warm state */
 314        dev->warm = true;
 315
 316        return 0;
 317
 318err:
 319        release_firmware(fw);
 320err_request_firmware:
 321        dev_dbg(&client->dev, "failed=%d\n", ret);
 322        return ret;
 323}
 324
 325static int mn88473_sleep(struct dvb_frontend *fe)
 326{
 327        struct i2c_client *client = fe->demodulator_priv;
 328        struct mn88473_dev *dev = i2c_get_clientdata(client);
 329        int ret;
 330
 331        dev_dbg(&client->dev, "\n");
 332
 333        ret = regmap_write(dev->regmap[2], 0x05, 0x3e);
 334        if (ret)
 335                goto err;
 336
 337        dev->delivery_system = SYS_UNDEFINED;
 338
 339        return 0;
 340err:
 341        dev_dbg(&client->dev, "failed=%d\n", ret);
 342        return ret;
 343}
 344
 345static struct dvb_frontend_ops mn88473_ops = {
 346        .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_AC},
 347        .info = {
 348                .name = "Panasonic MN88473",
 349                .symbol_rate_min = 1000000,
 350                .symbol_rate_max = 7200000,
 351                .caps = FE_CAN_FEC_1_2                 |
 352                        FE_CAN_FEC_2_3                 |
 353                        FE_CAN_FEC_3_4                 |
 354                        FE_CAN_FEC_5_6                 |
 355                        FE_CAN_FEC_7_8                 |
 356                        FE_CAN_FEC_AUTO                |
 357                        FE_CAN_QPSK                    |
 358                        FE_CAN_QAM_16                  |
 359                        FE_CAN_QAM_32                  |
 360                        FE_CAN_QAM_64                  |
 361                        FE_CAN_QAM_128                 |
 362                        FE_CAN_QAM_256                 |
 363                        FE_CAN_QAM_AUTO                |
 364                        FE_CAN_TRANSMISSION_MODE_AUTO  |
 365                        FE_CAN_GUARD_INTERVAL_AUTO     |
 366                        FE_CAN_HIERARCHY_AUTO          |
 367                        FE_CAN_MUTE_TS                 |
 368                        FE_CAN_2G_MODULATION           |
 369                        FE_CAN_MULTISTREAM
 370        },
 371
 372        .get_tune_settings = mn88473_get_tune_settings,
 373
 374        .init = mn88473_init,
 375        .sleep = mn88473_sleep,
 376
 377        .set_frontend = mn88473_set_frontend,
 378
 379        .read_status = mn88473_read_status,
 380};
 381
 382static int mn88473_probe(struct i2c_client *client,
 383                         const struct i2c_device_id *id)
 384{
 385        struct mn88473_config *config = client->dev.platform_data;
 386        struct mn88473_dev *dev;
 387        int ret;
 388        unsigned int utmp;
 389        static const struct regmap_config regmap_config = {
 390                .reg_bits = 8,
 391                .val_bits = 8,
 392        };
 393
 394        dev_dbg(&client->dev, "\n");
 395
 396        /* Caller really need to provide pointer for frontend we create. */
 397        if (config->fe == NULL) {
 398                dev_err(&client->dev, "frontend pointer not defined\n");
 399                ret = -EINVAL;
 400                goto err;
 401        }
 402
 403        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 404        if (dev == NULL) {
 405                ret = -ENOMEM;
 406                goto err;
 407        }
 408
 409        dev->i2c_wr_max = config->i2c_wr_max;
 410        if (!config->xtal)
 411                dev->xtal = 25000000;
 412        else
 413                dev->xtal = config->xtal;
 414        dev->client[0] = client;
 415        dev->regmap[0] = regmap_init_i2c(dev->client[0], &regmap_config);
 416        if (IS_ERR(dev->regmap[0])) {
 417                ret = PTR_ERR(dev->regmap[0]);
 418                goto err_kfree;
 419        }
 420
 421        /* check demod answers to I2C */
 422        ret = regmap_read(dev->regmap[0], 0x00, &utmp);
 423        if (ret)
 424                goto err_regmap_0_regmap_exit;
 425
 426        /*
 427         * Chip has three I2C addresses for different register pages. Used
 428         * addresses are 0x18, 0x1a and 0x1c. We register two dummy clients,
 429         * 0x1a and 0x1c, in order to get own I2C client for each register page.
 430         */
 431        dev->client[1] = i2c_new_dummy(client->adapter, 0x1a);
 432        if (dev->client[1] == NULL) {
 433                ret = -ENODEV;
 434                dev_err(&client->dev, "I2C registration failed\n");
 435                if (ret)
 436                        goto err_regmap_0_regmap_exit;
 437        }
 438        dev->regmap[1] = regmap_init_i2c(dev->client[1], &regmap_config);
 439        if (IS_ERR(dev->regmap[1])) {
 440                ret = PTR_ERR(dev->regmap[1]);
 441                goto err_client_1_i2c_unregister_device;
 442        }
 443        i2c_set_clientdata(dev->client[1], dev);
 444
 445        dev->client[2] = i2c_new_dummy(client->adapter, 0x1c);
 446        if (dev->client[2] == NULL) {
 447                ret = -ENODEV;
 448                dev_err(&client->dev, "2nd I2C registration failed\n");
 449                if (ret)
 450                        goto err_regmap_1_regmap_exit;
 451        }
 452        dev->regmap[2] = regmap_init_i2c(dev->client[2], &regmap_config);
 453        if (IS_ERR(dev->regmap[2])) {
 454                ret = PTR_ERR(dev->regmap[2]);
 455                goto err_client_2_i2c_unregister_device;
 456        }
 457        i2c_set_clientdata(dev->client[2], dev);
 458
 459        /* create dvb_frontend */
 460        memcpy(&dev->fe.ops, &mn88473_ops, sizeof(struct dvb_frontend_ops));
 461        dev->fe.demodulator_priv = client;
 462        *config->fe = &dev->fe;
 463        i2c_set_clientdata(client, dev);
 464
 465        dev_info(&dev->client[0]->dev, "Panasonic MN88473 successfully attached\n");
 466        return 0;
 467
 468err_client_2_i2c_unregister_device:
 469        i2c_unregister_device(dev->client[2]);
 470err_regmap_1_regmap_exit:
 471        regmap_exit(dev->regmap[1]);
 472err_client_1_i2c_unregister_device:
 473        i2c_unregister_device(dev->client[1]);
 474err_regmap_0_regmap_exit:
 475        regmap_exit(dev->regmap[0]);
 476err_kfree:
 477        kfree(dev);
 478err:
 479        dev_dbg(&client->dev, "failed=%d\n", ret);
 480        return ret;
 481}
 482
 483static int mn88473_remove(struct i2c_client *client)
 484{
 485        struct mn88473_dev *dev = i2c_get_clientdata(client);
 486
 487        dev_dbg(&client->dev, "\n");
 488
 489        regmap_exit(dev->regmap[2]);
 490        i2c_unregister_device(dev->client[2]);
 491
 492        regmap_exit(dev->regmap[1]);
 493        i2c_unregister_device(dev->client[1]);
 494
 495        regmap_exit(dev->regmap[0]);
 496
 497        kfree(dev);
 498
 499        return 0;
 500}
 501
 502static const struct i2c_device_id mn88473_id_table[] = {
 503        {"mn88473", 0},
 504        {}
 505};
 506MODULE_DEVICE_TABLE(i2c, mn88473_id_table);
 507
 508static struct i2c_driver mn88473_driver = {
 509        .driver = {
 510                .name   = "mn88473",
 511        },
 512        .probe          = mn88473_probe,
 513        .remove         = mn88473_remove,
 514        .id_table       = mn88473_id_table,
 515};
 516
 517module_i2c_driver(mn88473_driver);
 518
 519MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
 520MODULE_DESCRIPTION("Panasonic MN88473 DVB-T/T2/C demodulator driver");
 521MODULE_LICENSE("GPL");
 522MODULE_FIRMWARE(MN88473_FIRMWARE);
 523