linux/drivers/media/radio/radio-wl1273.c
<<
>>
Prefs
   1/*
   2 * Driver for the Texas Instruments WL1273 FM radio.
   3 *
   4 * Copyright (C) 2011 Nokia Corporation
   5 * Author: Matti J. Aaltonen <matti.j.aaltonen@nokia.com>
   6 *
   7 * This program is free software; you can redistribute it and/or
   8 * modify it under the terms of the GNU General Public License
   9 * version 2 as published by the Free Software Foundation.
  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 * You should have received a copy of the GNU General Public License
  17 * along with this program; if not, write to the Free Software
  18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 */
  20
  21#include <linux/delay.h>
  22#include <linux/firmware.h>
  23#include <linux/interrupt.h>
  24#include <linux/mfd/wl1273-core.h>
  25#include <linux/slab.h>
  26#include <media/v4l2-common.h>
  27#include <media/v4l2-ctrls.h>
  28#include <media/v4l2-device.h>
  29#include <media/v4l2-ioctl.h>
  30
  31#define DRIVER_DESC "Wl1273 FM Radio"
  32
  33#define WL1273_POWER_SET_OFF            0
  34#define WL1273_POWER_SET_FM             BIT(0)
  35#define WL1273_POWER_SET_RDS            BIT(1)
  36#define WL1273_POWER_SET_RETENTION      BIT(4)
  37
  38#define WL1273_PUPD_SET_OFF             0x00
  39#define WL1273_PUPD_SET_ON              0x01
  40#define WL1273_PUPD_SET_RETENTION       0x10
  41
  42#define WL1273_FREQ(x)          (x * 10000 / 625)
  43#define WL1273_INV_FREQ(x)      (x * 625 / 10000)
  44
  45/*
  46 * static int radio_nr - The number of the radio device
  47 *
  48 * The default is 0.
  49 */
  50static int radio_nr;
  51module_param(radio_nr, int, 0);
  52MODULE_PARM_DESC(radio_nr, "The number of the radio device. Default = 0");
  53
  54struct wl1273_device {
  55        char *bus_type;
  56
  57        u8 forbidden;
  58        unsigned int preemphasis;
  59        unsigned int spacing;
  60        unsigned int tx_power;
  61        unsigned int rx_frequency;
  62        unsigned int tx_frequency;
  63        unsigned int rangelow;
  64        unsigned int rangehigh;
  65        unsigned int band;
  66        bool stereo;
  67
  68        /* RDS */
  69        unsigned int rds_on;
  70
  71        wait_queue_head_t read_queue;
  72        struct mutex lock; /* for serializing fm radio operations */
  73        struct completion busy;
  74
  75        unsigned char *buffer;
  76        unsigned int buf_size;
  77        unsigned int rd_index;
  78        unsigned int wr_index;
  79
  80        /* Selected interrupts */
  81        u16 irq_flags;
  82        u16 irq_received;
  83
  84        struct v4l2_ctrl_handler ctrl_handler;
  85        struct v4l2_device v4l2dev;
  86        struct video_device videodev;
  87        struct device *dev;
  88        struct wl1273_core *core;
  89        struct file *owner;
  90        char *write_buf;
  91        unsigned int rds_users;
  92};
  93
  94#define WL1273_IRQ_MASK  (WL1273_FR_EVENT               |       \
  95                          WL1273_POW_ENB_EVENT)
  96
  97/*
  98 * static unsigned int rds_buf - the number of RDS buffer blocks used.
  99 *
 100 * The default number is 100.
 101 */
 102static unsigned int rds_buf = 100;
 103module_param(rds_buf, uint, 0);
 104MODULE_PARM_DESC(rds_buf, "Number of RDS buffer entries. Default = 100");
 105
 106static int wl1273_fm_write_fw(struct wl1273_core *core,
 107                              __u8 *fw, int len)
 108{
 109        struct i2c_client *client = core->client;
 110        struct i2c_msg msg;
 111        int i, r = 0;
 112
 113        msg.addr = client->addr;
 114        msg.flags = 0;
 115
 116        for (i = 0; i <= len; i++) {
 117                msg.len = fw[0];
 118                msg.buf = fw + 1;
 119
 120                fw += msg.len + 1;
 121                dev_dbg(&client->dev, "%s:len[%d]: %d\n", __func__, i, msg.len);
 122
 123                r = i2c_transfer(client->adapter, &msg, 1);
 124                if (r < 0 && i < len + 1)
 125                        break;
 126        }
 127
 128        dev_dbg(&client->dev, "%s: i: %d\n", __func__, i);
 129        dev_dbg(&client->dev, "%s: len + 1: %d\n", __func__, len + 1);
 130
 131        /* Last transfer always fails. */
 132        if (i == len || r == 1)
 133                r = 0;
 134
 135        return r;
 136}
 137
 138#define WL1273_FIFO_HAS_DATA(status)    (1 << 5 & status)
 139#define WL1273_RDS_CORRECTABLE_ERROR    (1 << 3)
 140#define WL1273_RDS_UNCORRECTABLE_ERROR  (1 << 4)
 141
 142static int wl1273_fm_rds(struct wl1273_device *radio)
 143{
 144        struct wl1273_core *core = radio->core;
 145        struct i2c_client *client = core->client;
 146        u16 val;
 147        u8 b0 = WL1273_RDS_DATA_GET, status;
 148        struct v4l2_rds_data rds = { 0, 0, 0 };
 149        struct i2c_msg msg[] = {
 150                {
 151                        .addr = client->addr,
 152                        .flags = 0,
 153                        .buf = &b0,
 154                        .len = 1,
 155                },
 156                {
 157                        .addr = client->addr,
 158                        .flags = I2C_M_RD,
 159                        .buf = (u8 *) &rds,
 160                        .len = sizeof(rds),
 161                }
 162        };
 163        int r;
 164
 165        if (core->mode != WL1273_MODE_RX)
 166                return 0;
 167
 168        r = core->read(core, WL1273_RDS_SYNC_GET, &val);
 169        if (r)
 170                return r;
 171
 172        if ((val & 0x01) == 0) {
 173                /* RDS decoder not synchronized */
 174                return -EAGAIN;
 175        }
 176
 177        /* copy all four RDS blocks to internal buffer */
 178        do {
 179                r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
 180                if (r != ARRAY_SIZE(msg)) {
 181                        dev_err(radio->dev, WL1273_FM_DRIVER_NAME
 182                                ": %s: read_rds error r == %i)\n",
 183                                __func__, r);
 184                }
 185
 186                status = rds.block;
 187
 188                if (!WL1273_FIFO_HAS_DATA(status))
 189                        break;
 190
 191                /* copy bits 0-2 (the block ID) to bits 3-5 */
 192                rds.block = V4L2_RDS_BLOCK_MSK & status;
 193                rds.block |= rds.block << 3;
 194
 195                /* copy the error bits to standard positions */
 196                if (WL1273_RDS_UNCORRECTABLE_ERROR & status) {
 197                        rds.block |= V4L2_RDS_BLOCK_ERROR;
 198                        rds.block &= ~V4L2_RDS_BLOCK_CORRECTED;
 199                } else if  (WL1273_RDS_CORRECTABLE_ERROR & status) {
 200                        rds.block &= ~V4L2_RDS_BLOCK_ERROR;
 201                        rds.block |= V4L2_RDS_BLOCK_CORRECTED;
 202                }
 203
 204                /* copy RDS block to internal buffer */
 205                memcpy(&radio->buffer[radio->wr_index], &rds, RDS_BLOCK_SIZE);
 206                radio->wr_index += 3;
 207
 208                /* wrap write pointer */
 209                if (radio->wr_index >= radio->buf_size)
 210                        radio->wr_index = 0;
 211
 212                /* check for overflow & start over */
 213                if (radio->wr_index == radio->rd_index) {
 214                        dev_dbg(radio->dev, "RDS OVERFLOW");
 215
 216                        radio->rd_index = 0;
 217                        radio->wr_index = 0;
 218                        break;
 219                }
 220        } while (WL1273_FIFO_HAS_DATA(status));
 221
 222        /* wake up read queue */
 223        if (radio->wr_index != radio->rd_index)
 224                wake_up_interruptible(&radio->read_queue);
 225
 226        return 0;
 227}
 228
 229static irqreturn_t wl1273_fm_irq_thread_handler(int irq, void *dev_id)
 230{
 231        struct wl1273_device *radio = dev_id;
 232        struct wl1273_core *core = radio->core;
 233        u16 flags;
 234        int r;
 235
 236        r = core->read(core, WL1273_FLAG_GET, &flags);
 237        if (r)
 238                goto out;
 239
 240        if (flags & WL1273_BL_EVENT) {
 241                radio->irq_received = flags;
 242                dev_dbg(radio->dev, "IRQ: BL\n");
 243        }
 244
 245        if (flags & WL1273_RDS_EVENT) {
 246                msleep(200);
 247
 248                wl1273_fm_rds(radio);
 249        }
 250
 251        if (flags & WL1273_BBLK_EVENT)
 252                dev_dbg(radio->dev, "IRQ: BBLK\n");
 253
 254        if (flags & WL1273_LSYNC_EVENT)
 255                dev_dbg(radio->dev, "IRQ: LSYNC\n");
 256
 257        if (flags & WL1273_LEV_EVENT) {
 258                u16 level;
 259
 260                r = core->read(core, WL1273_RSSI_LVL_GET, &level);
 261                if (r)
 262                        goto out;
 263
 264                if (level > 14)
 265                        dev_dbg(radio->dev, "IRQ: LEV: 0x%x04\n", level);
 266        }
 267
 268        if (flags & WL1273_IFFR_EVENT)
 269                dev_dbg(radio->dev, "IRQ: IFFR\n");
 270
 271        if (flags & WL1273_PI_EVENT)
 272                dev_dbg(radio->dev, "IRQ: PI\n");
 273
 274        if (flags & WL1273_PD_EVENT)
 275                dev_dbg(radio->dev, "IRQ: PD\n");
 276
 277        if (flags & WL1273_STIC_EVENT)
 278                dev_dbg(radio->dev, "IRQ: STIC\n");
 279
 280        if (flags & WL1273_MAL_EVENT)
 281                dev_dbg(radio->dev, "IRQ: MAL\n");
 282
 283        if (flags & WL1273_POW_ENB_EVENT) {
 284                complete(&radio->busy);
 285                dev_dbg(radio->dev, "NOT BUSY\n");
 286                dev_dbg(radio->dev, "IRQ: POW_ENB\n");
 287        }
 288
 289        if (flags & WL1273_SCAN_OVER_EVENT)
 290                dev_dbg(radio->dev, "IRQ: SCAN_OVER\n");
 291
 292        if (flags & WL1273_ERROR_EVENT)
 293                dev_dbg(radio->dev, "IRQ: ERROR\n");
 294
 295        if (flags & WL1273_FR_EVENT) {
 296                u16 freq;
 297
 298                dev_dbg(radio->dev, "IRQ: FR:\n");
 299
 300                if (core->mode == WL1273_MODE_RX) {
 301                        r = core->write(core, WL1273_TUNER_MODE_SET,
 302                                        TUNER_MODE_STOP_SEARCH);
 303                        if (r) {
 304                                dev_err(radio->dev,
 305                                        "%s: TUNER_MODE_SET fails: %d\n",
 306                                        __func__, r);
 307                                goto out;
 308                        }
 309
 310                        r = core->read(core, WL1273_FREQ_SET, &freq);
 311                        if (r)
 312                                goto out;
 313
 314                        if (radio->band == WL1273_BAND_JAPAN)
 315                                radio->rx_frequency = WL1273_BAND_JAPAN_LOW +
 316                                        freq * 50;
 317                        else
 318                                radio->rx_frequency = WL1273_BAND_OTHER_LOW +
 319                                        freq * 50;
 320                        /*
 321                         *  The driver works better with this msleep,
 322                         *  the documentation doesn't mention it.
 323                         */
 324                        usleep_range(10000, 15000);
 325
 326                        dev_dbg(radio->dev, "%dkHz\n", radio->rx_frequency);
 327
 328                } else {
 329                        r = core->read(core, WL1273_CHANL_SET, &freq);
 330                        if (r)
 331                                goto out;
 332
 333                        dev_dbg(radio->dev, "%dkHz\n", freq);
 334                }
 335                dev_dbg(radio->dev, "%s: NOT BUSY\n", __func__);
 336        }
 337
 338out:
 339        core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
 340        complete(&radio->busy);
 341
 342        return IRQ_HANDLED;
 343}
 344
 345static int wl1273_fm_set_tx_freq(struct wl1273_device *radio, unsigned int freq)
 346{
 347        struct wl1273_core *core = radio->core;
 348        int r = 0;
 349
 350        if (freq < WL1273_BAND_TX_LOW) {
 351                dev_err(radio->dev,
 352                        "Frequency out of range: %d < %d\n", freq,
 353                        WL1273_BAND_TX_LOW);
 354                return -ERANGE;
 355        }
 356
 357        if (freq > WL1273_BAND_TX_HIGH) {
 358                dev_err(radio->dev,
 359                        "Frequency out of range: %d > %d\n", freq,
 360                        WL1273_BAND_TX_HIGH);
 361                return -ERANGE;
 362        }
 363
 364        /*
 365         *  The driver works better with this sleep,
 366         *  the documentation doesn't mention it.
 367         */
 368        usleep_range(5000, 10000);
 369
 370        dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq);
 371
 372        /* Set the current tx channel */
 373        r = core->write(core, WL1273_CHANL_SET, freq / 10);
 374        if (r)
 375                return r;
 376
 377        INIT_COMPLETION(radio->busy);
 378
 379        /* wait for the FR IRQ */
 380        r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
 381        if (!r)
 382                return -ETIMEDOUT;
 383
 384        dev_dbg(radio->dev, "WL1273_CHANL_SET: %d\n", r);
 385
 386        /* Enable the output power */
 387        r = core->write(core, WL1273_POWER_ENB_SET, 1);
 388        if (r)
 389                return r;
 390
 391        INIT_COMPLETION(radio->busy);
 392
 393        /* wait for the POWER_ENB IRQ */
 394        r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
 395        if (!r)
 396                return -ETIMEDOUT;
 397
 398        radio->tx_frequency = freq;
 399        dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %d\n", r);
 400
 401        return  0;
 402}
 403
 404static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq)
 405{
 406        struct wl1273_core *core = radio->core;
 407        int r, f;
 408
 409        if (freq < radio->rangelow) {
 410                dev_err(radio->dev,
 411                        "Frequency out of range: %d < %d\n", freq,
 412                        radio->rangelow);
 413                r = -ERANGE;
 414                goto err;
 415        }
 416
 417        if (freq > radio->rangehigh) {
 418                dev_err(radio->dev,
 419                        "Frequency out of range: %d > %d\n", freq,
 420                        radio->rangehigh);
 421                r = -ERANGE;
 422                goto err;
 423        }
 424
 425        dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq);
 426
 427        core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
 428
 429        if (radio->band == WL1273_BAND_JAPAN)
 430                f = (freq - WL1273_BAND_JAPAN_LOW) / 50;
 431        else
 432                f = (freq - WL1273_BAND_OTHER_LOW) / 50;
 433
 434        r = core->write(core, WL1273_FREQ_SET, f);
 435        if (r) {
 436                dev_err(radio->dev, "FREQ_SET fails\n");
 437                goto err;
 438        }
 439
 440        r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET);
 441        if (r) {
 442                dev_err(radio->dev, "TUNER_MODE_SET fails\n");
 443                goto err;
 444        }
 445
 446        INIT_COMPLETION(radio->busy);
 447
 448        r = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
 449        if (!r) {
 450                dev_err(radio->dev, "%s: TIMEOUT\n", __func__);
 451                return -ETIMEDOUT;
 452        }
 453
 454        radio->rd_index = 0;
 455        radio->wr_index = 0;
 456        radio->rx_frequency = freq;
 457        return 0;
 458err:
 459        return r;
 460}
 461
 462static int wl1273_fm_get_freq(struct wl1273_device *radio)
 463{
 464        struct wl1273_core *core = radio->core;
 465        unsigned int freq;
 466        u16 f;
 467        int r;
 468
 469        if (core->mode == WL1273_MODE_RX) {
 470                r = core->read(core, WL1273_FREQ_SET, &f);
 471                if (r)
 472                        return r;
 473
 474                dev_dbg(radio->dev, "Freq get: 0x%04x\n", f);
 475                if (radio->band == WL1273_BAND_JAPAN)
 476                        freq = WL1273_BAND_JAPAN_LOW + 50 * f;
 477                else
 478                        freq = WL1273_BAND_OTHER_LOW + 50 * f;
 479        } else {
 480                r = core->read(core, WL1273_CHANL_SET, &f);
 481                if (r)
 482                        return r;
 483
 484                freq = f * 10;
 485        }
 486
 487        return freq;
 488}
 489
 490/**
 491 * wl1273_fm_upload_firmware_patch() -  Upload the firmware.
 492 * @radio:                              A pointer to the device struct.
 493 *
 494 * The firmware file consists of arrays of bytes where the first byte
 495 * gives the array length. The first byte in the file gives the
 496 * number of these arrays.
 497 */
 498static int wl1273_fm_upload_firmware_patch(struct wl1273_device *radio)
 499{
 500        struct wl1273_core *core = radio->core;
 501        unsigned int packet_num;
 502        const struct firmware *fw_p;
 503        const char *fw_name = "radio-wl1273-fw.bin";
 504        struct device *dev = radio->dev;
 505        __u8 *ptr;
 506        int r;
 507
 508        dev_dbg(dev, "%s:\n", __func__);
 509
 510        /*
 511         * Uploading the firmware patch is not always necessary,
 512         * so we only print an info message.
 513         */
 514        if (request_firmware(&fw_p, fw_name, dev)) {
 515                dev_info(dev, "%s - %s not found\n", __func__, fw_name);
 516
 517                return 0;
 518        }
 519
 520        ptr = (__u8 *) fw_p->data;
 521        packet_num = ptr[0];
 522        dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num);
 523
 524        r = wl1273_fm_write_fw(core, ptr + 1, packet_num);
 525        if (r) {
 526                dev_err(dev, "FW upload error: %d\n", r);
 527                goto out;
 528        }
 529
 530        /* ignore possible error here */
 531        core->write(core, WL1273_RESET, 0);
 532
 533        dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r);
 534out:
 535        release_firmware(fw_p);
 536        return r;
 537}
 538
 539static int wl1273_fm_stop(struct wl1273_device *radio)
 540{
 541        struct wl1273_core *core = radio->core;
 542
 543        if (core->mode == WL1273_MODE_RX) {
 544                int r = core->write(core, WL1273_POWER_SET,
 545                                    WL1273_POWER_SET_OFF);
 546                if (r)
 547                        dev_err(radio->dev, "%s: POWER_SET fails: %d\n",
 548                                __func__, r);
 549        } else if (core->mode == WL1273_MODE_TX) {
 550                int r = core->write(core, WL1273_PUPD_SET,
 551                                    WL1273_PUPD_SET_OFF);
 552                if (r)
 553                        dev_err(radio->dev,
 554                                "%s: PUPD_SET fails: %d\n", __func__, r);
 555        }
 556
 557        if (core->pdata->disable) {
 558                core->pdata->disable();
 559                dev_dbg(radio->dev, "Back to reset\n");
 560        }
 561
 562        return 0;
 563}
 564
 565static int wl1273_fm_start(struct wl1273_device *radio, int new_mode)
 566{
 567        struct wl1273_core *core = radio->core;
 568        struct wl1273_fm_platform_data *pdata = core->pdata;
 569        struct device *dev = radio->dev;
 570        int r = -EINVAL;
 571
 572        if (pdata->enable && core->mode == WL1273_MODE_OFF) {
 573                dev_dbg(radio->dev, "Out of reset\n");
 574
 575                pdata->enable();
 576                msleep(250);
 577        }
 578
 579        if (new_mode == WL1273_MODE_RX) {
 580                u16 val = WL1273_POWER_SET_FM;
 581
 582                if (radio->rds_on)
 583                        val |= WL1273_POWER_SET_RDS;
 584
 585                /* If this fails try again */
 586                r = core->write(core, WL1273_POWER_SET, val);
 587                if (r) {
 588                        msleep(100);
 589
 590                        r = core->write(core, WL1273_POWER_SET, val);
 591                        if (r) {
 592                                dev_err(dev, "%s: POWER_SET fails\n", __func__);
 593                                goto fail;
 594                        }
 595                }
 596
 597                /* rds buffer configuration */
 598                radio->wr_index = 0;
 599                radio->rd_index = 0;
 600
 601        } else if (new_mode == WL1273_MODE_TX) {
 602                /* If this fails try again once */
 603                r = core->write(core, WL1273_PUPD_SET, WL1273_PUPD_SET_ON);
 604                if (r) {
 605                        msleep(100);
 606                        r = core->write(core, WL1273_PUPD_SET,
 607                                        WL1273_PUPD_SET_ON);
 608                        if (r) {
 609                                dev_err(dev, "%s: PUPD_SET fails\n", __func__);
 610                                goto fail;
 611                        }
 612                }
 613
 614                if (radio->rds_on)
 615                        r = core->write(core, WL1273_RDS_DATA_ENB, 1);
 616                else
 617                        r = core->write(core, WL1273_RDS_DATA_ENB, 0);
 618        } else {
 619                dev_warn(dev, "%s: Illegal mode.\n", __func__);
 620        }
 621
 622        if (core->mode == WL1273_MODE_OFF) {
 623                r = wl1273_fm_upload_firmware_patch(radio);
 624                if (r)
 625                        dev_warn(dev, "Firmware upload failed.\n");
 626
 627                /*
 628                 * Sometimes the chip is in a wrong power state at this point.
 629                 * So we set the power once again.
 630                 */
 631                if (new_mode == WL1273_MODE_RX) {
 632                        u16 val = WL1273_POWER_SET_FM;
 633
 634                        if (radio->rds_on)
 635                                val |= WL1273_POWER_SET_RDS;
 636
 637                        r = core->write(core, WL1273_POWER_SET, val);
 638                        if (r) {
 639                                dev_err(dev, "%s: POWER_SET fails\n", __func__);
 640                                goto fail;
 641                        }
 642                } else if (new_mode == WL1273_MODE_TX) {
 643                        r = core->write(core, WL1273_PUPD_SET,
 644                                        WL1273_PUPD_SET_ON);
 645                        if (r) {
 646                                dev_err(dev, "%s: PUPD_SET fails\n", __func__);
 647                                goto fail;
 648                        }
 649                }
 650        }
 651
 652        return 0;
 653fail:
 654        if (pdata->disable)
 655                pdata->disable();
 656
 657        dev_dbg(dev, "%s: return: %d\n", __func__, r);
 658        return r;
 659}
 660
 661static int wl1273_fm_suspend(struct wl1273_device *radio)
 662{
 663        struct wl1273_core *core = radio->core;
 664        int r = 0;
 665
 666        /* Cannot go from OFF to SUSPENDED */
 667        if (core->mode == WL1273_MODE_RX)
 668                r = core->write(core, WL1273_POWER_SET,
 669                                WL1273_POWER_SET_RETENTION);
 670        else if (core->mode == WL1273_MODE_TX)
 671                r = core->write(core, WL1273_PUPD_SET,
 672                                WL1273_PUPD_SET_RETENTION);
 673        else
 674                r = -EINVAL;
 675
 676        if (r) {
 677                dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
 678                goto out;
 679        }
 680
 681out:
 682        return r;
 683}
 684
 685static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
 686{
 687        struct wl1273_core *core = radio->core;
 688        struct device *dev = radio->dev;
 689        int old_mode;
 690        int r;
 691
 692        dev_dbg(dev, "%s\n", __func__);
 693        dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);
 694
 695        old_mode = core->mode;
 696        if (mode & radio->forbidden) {
 697                r = -EPERM;
 698                goto out;
 699        }
 700
 701        switch (mode) {
 702        case WL1273_MODE_RX:
 703        case WL1273_MODE_TX:
 704                r = wl1273_fm_start(radio, mode);
 705                if (r) {
 706                        dev_err(dev, "%s: Cannot start.\n", __func__);
 707                        wl1273_fm_stop(radio);
 708                        goto out;
 709                }
 710
 711                core->mode = mode;
 712                r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
 713                if (r) {
 714                        dev_err(dev, "INT_MASK_SET fails.\n");
 715                        goto out;
 716                }
 717
 718                /* remember previous settings */
 719                if (mode == WL1273_MODE_RX) {
 720                        r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
 721                        if (r) {
 722                                dev_err(dev, "set freq fails: %d.\n", r);
 723                                goto out;
 724                        }
 725
 726                        r = core->set_volume(core, core->volume);
 727                        if (r) {
 728                                dev_err(dev, "set volume fails: %d.\n", r);
 729                                goto out;
 730                        }
 731
 732                        dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
 733                                core->volume);
 734                } else {
 735                        r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
 736                        if (r) {
 737                                dev_err(dev, "set freq fails: %d.\n", r);
 738                                goto out;
 739                        }
 740                }
 741
 742                dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);
 743
 744                r = core->set_audio(core, core->audio_mode);
 745                if (r)
 746                        dev_err(dev, "Cannot set audio mode.\n");
 747                break;
 748
 749        case WL1273_MODE_OFF:
 750                r = wl1273_fm_stop(radio);
 751                if (r)
 752                        dev_err(dev, "%s: Off fails: %d\n", __func__, r);
 753                else
 754                        core->mode = WL1273_MODE_OFF;
 755
 756                break;
 757
 758        case WL1273_MODE_SUSPENDED:
 759                r = wl1273_fm_suspend(radio);
 760                if (r)
 761                        dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
 762                else
 763                        core->mode = WL1273_MODE_SUSPENDED;
 764
 765                break;
 766
 767        default:
 768                dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
 769                r = -EINVAL;
 770                break;
 771        }
 772out:
 773        if (r)
 774                core->mode = old_mode;
 775
 776        return r;
 777}
 778
 779static int wl1273_fm_set_seek(struct wl1273_device *radio,
 780                              unsigned int wrap_around,
 781                              unsigned int seek_upward,
 782                              int level)
 783{
 784        struct wl1273_core *core = radio->core;
 785        int r = 0;
 786        unsigned int dir = (seek_upward == 0) ? 0 : 1;
 787        unsigned int f;
 788
 789        f = radio->rx_frequency;
 790        dev_dbg(radio->dev, "rx_frequency: %d\n", f);
 791
 792        if (dir && f + radio->spacing <= radio->rangehigh)
 793                r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
 794        else if (dir && wrap_around)
 795                r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
 796        else if (f - radio->spacing >= radio->rangelow)
 797                r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
 798        else if (wrap_around)
 799                r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);
 800
 801        if (r)
 802                goto out;
 803
 804        if (level < SCHAR_MIN || level > SCHAR_MAX)
 805                return -EINVAL;
 806
 807        INIT_COMPLETION(radio->busy);
 808        dev_dbg(radio->dev, "%s: BUSY\n", __func__);
 809
 810        r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
 811        if (r)
 812                goto out;
 813
 814        dev_dbg(radio->dev, "%s\n", __func__);
 815
 816        r = core->write(core, WL1273_SEARCH_LVL_SET, level);
 817        if (r)
 818                goto out;
 819
 820        r = core->write(core, WL1273_SEARCH_DIR_SET, dir);
 821        if (r)
 822                goto out;
 823
 824        r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
 825        if (r)
 826                goto out;
 827
 828        wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
 829        if (!(radio->irq_received & WL1273_BL_EVENT))
 830                goto out;
 831
 832        radio->irq_received &= ~WL1273_BL_EVENT;
 833
 834        if (!wrap_around)
 835                goto out;
 836
 837        /* Wrap around */
 838        dev_dbg(radio->dev, "Wrap around in HW seek.\n");
 839
 840        if (seek_upward)
 841                f = radio->rangelow;
 842        else
 843                f = radio->rangehigh;
 844
 845        r = wl1273_fm_set_rx_freq(radio, f);
 846        if (r)
 847                goto out;
 848
 849        INIT_COMPLETION(radio->busy);
 850        dev_dbg(radio->dev, "%s: BUSY\n", __func__);
 851
 852        r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
 853        if (r)
 854                goto out;
 855
 856        wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
 857out:
 858        dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
 859        return r;
 860}
 861
 862/**
 863 * wl1273_fm_get_tx_ctune() -   Get the TX tuning capacitor value.
 864 * @radio:                      A pointer to the device struct.
 865 */
 866static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
 867{
 868        struct wl1273_core *core = radio->core;
 869        struct device *dev = radio->dev;
 870        u16 val;
 871        int r;
 872
 873        if (core->mode == WL1273_MODE_OFF ||
 874            core->mode == WL1273_MODE_SUSPENDED)
 875                return -EPERM;
 876
 877        r = core->read(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
 878        if (r) {
 879                dev_err(dev, "%s: read error: %d\n", __func__, r);
 880                goto out;
 881        }
 882
 883out:
 884        return val;
 885}
 886
 887/**
 888 * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
 889 * @radio:                       A pointer to the device struct.
 890 * @preemphasis:                 The new pre-amphasis value.
 891 *
 892 * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
 893 * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
 894 */
 895static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
 896                                     unsigned int preemphasis)
 897{
 898        struct wl1273_core *core = radio->core;
 899        int r;
 900        u16 em;
 901
 902        if (core->mode == WL1273_MODE_OFF ||
 903            core->mode == WL1273_MODE_SUSPENDED)
 904                return -EPERM;
 905
 906        mutex_lock(&core->lock);
 907
 908        switch (preemphasis) {
 909        case V4L2_PREEMPHASIS_DISABLED:
 910                em = 1;
 911                break;
 912        case V4L2_PREEMPHASIS_50_uS:
 913                em = 0;
 914                break;
 915        case V4L2_PREEMPHASIS_75_uS:
 916                em = 2;
 917                break;
 918        default:
 919                r = -EINVAL;
 920                goto out;
 921        }
 922
 923        r = core->write(core, WL1273_PREMPH_SET, em);
 924        if (r)
 925                goto out;
 926
 927        radio->preemphasis = preemphasis;
 928
 929out:
 930        mutex_unlock(&core->lock);
 931        return r;
 932}
 933
 934static int wl1273_fm_rds_on(struct wl1273_device *radio)
 935{
 936        struct wl1273_core *core = radio->core;
 937        int r;
 938
 939        dev_dbg(radio->dev, "%s\n", __func__);
 940        if (radio->rds_on)
 941                return 0;
 942
 943        r = core->write(core, WL1273_POWER_SET,
 944                        WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
 945        if (r)
 946                goto out;
 947
 948        r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
 949        if (r)
 950                dev_err(radio->dev, "set freq fails: %d.\n", r);
 951out:
 952        return r;
 953}
 954
 955static int wl1273_fm_rds_off(struct wl1273_device *radio)
 956{
 957        struct wl1273_core *core = radio->core;
 958        int r;
 959
 960        if (!radio->rds_on)
 961                return 0;
 962
 963        radio->irq_flags &= ~WL1273_RDS_EVENT;
 964
 965        r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
 966        if (r)
 967                goto out;
 968
 969        /* Service pending read */
 970        wake_up_interruptible(&radio->read_queue);
 971
 972        dev_dbg(radio->dev, "%s\n", __func__);
 973
 974        r = core->write(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
 975        if (r)
 976                goto out;
 977
 978        r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
 979        if (r)
 980                dev_err(radio->dev, "set freq fails: %d.\n", r);
 981out:
 982        dev_dbg(radio->dev, "%s: exiting...\n", __func__);
 983
 984        return r;
 985}
 986
 987static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode)
 988{
 989        int r = 0;
 990        struct wl1273_core *core = radio->core;
 991
 992        if (core->mode == WL1273_MODE_OFF ||
 993            core->mode == WL1273_MODE_SUSPENDED)
 994                return -EPERM;
 995
 996        if (new_mode == WL1273_RDS_RESET) {
 997                r = core->write(core, WL1273_RDS_CNTRL_SET, 1);
 998                return r;
 999        }
1000
1001        if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) {
1002                r = core->write(core, WL1273_RDS_DATA_ENB, 0);
1003        } else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) {
1004                r = core->write(core, WL1273_RDS_DATA_ENB, 1);
1005        } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) {
1006                r = wl1273_fm_rds_off(radio);
1007        } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) {
1008                r = wl1273_fm_rds_on(radio);
1009        } else {
1010                dev_err(radio->dev, "%s: Unknown mode: %d\n",
1011                        __func__, new_mode);
1012                r = -EINVAL;
1013        }
1014
1015        if (!r)
1016                radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false;
1017
1018        return r;
1019}
1020
1021static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
1022                                    size_t count, loff_t *ppos)
1023{
1024        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1025        struct wl1273_core *core = radio->core;
1026        u16 val;
1027        int r;
1028
1029        dev_dbg(radio->dev, "%s\n", __func__);
1030
1031        if (core->mode != WL1273_MODE_TX)
1032                return count;
1033
1034        if (radio->rds_users == 0) {
1035                dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1036                return 0;
1037        }
1038
1039        if (mutex_lock_interruptible(&core->lock))
1040                return -EINTR;
1041        /*
1042         * Multiple processes can open the device, but only
1043         * one gets to write to it.
1044         */
1045        if (radio->owner && radio->owner != file) {
1046                r = -EBUSY;
1047                goto out;
1048        }
1049        radio->owner = file;
1050
1051        /* Manual Mode */
1052        if (count > 255)
1053                val = 255;
1054        else
1055                val = count;
1056
1057        core->write(core, WL1273_RDS_CONFIG_DATA_SET, val);
1058
1059        if (copy_from_user(radio->write_buf + 1, buf, val)) {
1060                r = -EFAULT;
1061                goto out;
1062        }
1063
1064        dev_dbg(radio->dev, "Count: %d\n", val);
1065        dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf);
1066
1067        radio->write_buf[0] = WL1273_RDS_DATA_SET;
1068        core->write_data(core, radio->write_buf, val + 1);
1069
1070        r = val;
1071out:
1072        mutex_unlock(&core->lock);
1073
1074        return r;
1075}
1076
1077static unsigned int wl1273_fm_fops_poll(struct file *file,
1078                                        struct poll_table_struct *pts)
1079{
1080        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1081        struct wl1273_core *core = radio->core;
1082
1083        if (radio->owner && radio->owner != file)
1084                return -EBUSY;
1085
1086        radio->owner = file;
1087
1088        if (core->mode == WL1273_MODE_RX) {
1089                poll_wait(file, &radio->read_queue, pts);
1090
1091                if (radio->rd_index != radio->wr_index)
1092                        return POLLIN | POLLRDNORM;
1093
1094        } else if (core->mode == WL1273_MODE_TX) {
1095                return POLLOUT | POLLWRNORM;
1096        }
1097
1098        return 0;
1099}
1100
1101static int wl1273_fm_fops_open(struct file *file)
1102{
1103        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1104        struct wl1273_core *core = radio->core;
1105        int r = 0;
1106
1107        dev_dbg(radio->dev, "%s\n", __func__);
1108
1109        if (core->mode == WL1273_MODE_RX && radio->rds_on &&
1110            !radio->rds_users) {
1111                dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode);
1112
1113                if (mutex_lock_interruptible(&core->lock))
1114                        return -EINTR;
1115
1116                radio->irq_flags |= WL1273_RDS_EVENT;
1117
1118                r = core->write(core, WL1273_INT_MASK_SET,
1119                                radio->irq_flags);
1120                if (r) {
1121                        mutex_unlock(&core->lock);
1122                        goto out;
1123                }
1124
1125                radio->rds_users++;
1126
1127                mutex_unlock(&core->lock);
1128        }
1129out:
1130        return r;
1131}
1132
1133static int wl1273_fm_fops_release(struct file *file)
1134{
1135        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1136        struct wl1273_core *core = radio->core;
1137        int r = 0;
1138
1139        dev_dbg(radio->dev, "%s\n", __func__);
1140
1141        if (radio->rds_users > 0) {
1142                radio->rds_users--;
1143                if (radio->rds_users == 0) {
1144                        if (mutex_lock_interruptible(&core->lock))
1145                                return -EINTR;
1146
1147                        radio->irq_flags &= ~WL1273_RDS_EVENT;
1148
1149                        if (core->mode == WL1273_MODE_RX) {
1150                                r = core->write(core,
1151                                                WL1273_INT_MASK_SET,
1152                                                radio->irq_flags);
1153                                if (r) {
1154                                        mutex_unlock(&core->lock);
1155                                        goto out;
1156                                }
1157                        }
1158                        mutex_unlock(&core->lock);
1159                }
1160        }
1161
1162        if (file == radio->owner)
1163                radio->owner = NULL;
1164out:
1165        return r;
1166}
1167
1168static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
1169                                   size_t count, loff_t *ppos)
1170{
1171        int r = 0;
1172        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1173        struct wl1273_core *core = radio->core;
1174        unsigned int block_count = 0;
1175        u16 val;
1176
1177        dev_dbg(radio->dev, "%s\n", __func__);
1178
1179        if (core->mode != WL1273_MODE_RX)
1180                return 0;
1181
1182        if (radio->rds_users == 0) {
1183                dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1184                return 0;
1185        }
1186
1187        if (mutex_lock_interruptible(&core->lock))
1188                return -EINTR;
1189
1190        /*
1191         * Multiple processes can open the device, but only
1192         * one at a time gets read access.
1193         */
1194        if (radio->owner && radio->owner != file) {
1195                r = -EBUSY;
1196                goto out;
1197        }
1198        radio->owner = file;
1199
1200        r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1201        if (r) {
1202                dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
1203                goto out;
1204        } else if (val == 0) {
1205                dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
1206                r = -ENODATA;
1207                goto out;
1208        }
1209
1210        /* block if no new data available */
1211        while (radio->wr_index == radio->rd_index) {
1212                if (file->f_flags & O_NONBLOCK) {
1213                        r = -EWOULDBLOCK;
1214                        goto out;
1215                }
1216
1217                dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
1218                if (wait_event_interruptible(radio->read_queue,
1219                                             radio->wr_index !=
1220                                             radio->rd_index) < 0) {
1221                        r = -EINTR;
1222                        goto out;
1223                }
1224        }
1225
1226        /* calculate block count from byte count */
1227        count /= RDS_BLOCK_SIZE;
1228
1229        /* copy RDS blocks from the internal buffer and to user buffer */
1230        while (block_count < count) {
1231                if (radio->rd_index == radio->wr_index)
1232                        break;
1233
1234                /* always transfer complete RDS blocks */
1235                if (copy_to_user(buf, &radio->buffer[radio->rd_index],
1236                                 RDS_BLOCK_SIZE))
1237                        break;
1238
1239                /* increment and wrap the read pointer */
1240                radio->rd_index += RDS_BLOCK_SIZE;
1241                if (radio->rd_index >= radio->buf_size)
1242                        radio->rd_index = 0;
1243
1244                /* increment counters */
1245                block_count++;
1246                buf += RDS_BLOCK_SIZE;
1247                r += RDS_BLOCK_SIZE;
1248        }
1249
1250out:
1251        dev_dbg(radio->dev, "%s: exit\n", __func__);
1252        mutex_unlock(&core->lock);
1253
1254        return r;
1255}
1256
1257static const struct v4l2_file_operations wl1273_fops = {
1258        .owner          = THIS_MODULE,
1259        .read           = wl1273_fm_fops_read,
1260        .write          = wl1273_fm_fops_write,
1261        .poll           = wl1273_fm_fops_poll,
1262        .unlocked_ioctl = video_ioctl2,
1263        .open           = wl1273_fm_fops_open,
1264        .release        = wl1273_fm_fops_release,
1265};
1266
1267static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
1268                                     struct v4l2_capability *capability)
1269{
1270        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1271
1272        dev_dbg(radio->dev, "%s\n", __func__);
1273
1274        strlcpy(capability->driver, WL1273_FM_DRIVER_NAME,
1275                sizeof(capability->driver));
1276        strlcpy(capability->card, "Texas Instruments Wl1273 FM Radio",
1277                sizeof(capability->card));
1278        strlcpy(capability->bus_info, radio->bus_type,
1279                sizeof(capability->bus_info));
1280
1281        capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
1282                V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
1283                V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
1284                V4L2_CAP_RDS_OUTPUT;
1285
1286        return 0;
1287}
1288
1289static int wl1273_fm_vidioc_g_input(struct file *file, void *priv,
1290                                    unsigned int *i)
1291{
1292        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1293
1294        dev_dbg(radio->dev, "%s\n", __func__);
1295
1296        *i = 0;
1297
1298        return 0;
1299}
1300
1301static int wl1273_fm_vidioc_s_input(struct file *file, void *priv,
1302                                    unsigned int i)
1303{
1304        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1305
1306        dev_dbg(radio->dev, "%s\n", __func__);
1307
1308        if (i != 0)
1309                return -EINVAL;
1310
1311        return 0;
1312}
1313
1314/**
1315 * wl1273_fm_set_tx_power() -   Set the transmission power value.
1316 * @core:                       A pointer to the device struct.
1317 * @power:                      The new power value.
1318 */
1319static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power)
1320{
1321        struct wl1273_core *core = radio->core;
1322        int r;
1323
1324        if (core->mode == WL1273_MODE_OFF ||
1325            core->mode == WL1273_MODE_SUSPENDED)
1326                return -EPERM;
1327
1328        mutex_lock(&core->lock);
1329
1330        /* Convert the dBuV value to chip presentation */
1331        r = core->write(core, WL1273_POWER_LEV_SET, 122 - power);
1332        if (r)
1333                goto out;
1334
1335        radio->tx_power = power;
1336
1337out:
1338        mutex_unlock(&core->lock);
1339        return r;
1340}
1341
1342#define WL1273_SPACING_50kHz    1
1343#define WL1273_SPACING_100kHz   2
1344#define WL1273_SPACING_200kHz   4
1345
1346static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio,
1347                                    unsigned int spacing)
1348{
1349        struct wl1273_core *core = radio->core;
1350        int r;
1351
1352        if (spacing == 0) {
1353                r = core->write(core, WL1273_SCAN_SPACING_SET,
1354                                WL1273_SPACING_100kHz);
1355                radio->spacing = 100;
1356        } else if (spacing - 50000 < 25000) {
1357                r = core->write(core, WL1273_SCAN_SPACING_SET,
1358                                WL1273_SPACING_50kHz);
1359                radio->spacing = 50;
1360        } else if (spacing - 100000 < 50000) {
1361                r = core->write(core, WL1273_SCAN_SPACING_SET,
1362                                WL1273_SPACING_100kHz);
1363                radio->spacing = 100;
1364        } else {
1365                r = core->write(core, WL1273_SCAN_SPACING_SET,
1366                                WL1273_SPACING_200kHz);
1367                radio->spacing = 200;
1368        }
1369
1370        return r;
1371}
1372
1373static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1374{
1375        struct wl1273_device *radio = ctrl->priv;
1376        struct wl1273_core *core = radio->core;
1377
1378        dev_dbg(radio->dev, "%s\n", __func__);
1379
1380        if (mutex_lock_interruptible(&core->lock))
1381                return -EINTR;
1382
1383        switch (ctrl->id) {
1384        case  V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1385                ctrl->val = wl1273_fm_get_tx_ctune(radio);
1386                break;
1387
1388        default:
1389                dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
1390                         __func__, ctrl->id);
1391                break;
1392        }
1393
1394        mutex_unlock(&core->lock);
1395
1396        return 0;
1397}
1398
1399#define WL1273_MUTE_SOFT_ENABLE    (1 << 0)
1400#define WL1273_MUTE_AC             (1 << 1)
1401#define WL1273_MUTE_HARD_LEFT      (1 << 2)
1402#define WL1273_MUTE_HARD_RIGHT     (1 << 3)
1403#define WL1273_MUTE_SOFT_FORCE     (1 << 4)
1404
1405static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
1406{
1407        return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
1408}
1409
1410static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl)
1411{
1412        struct wl1273_device *radio = to_radio(ctrl);
1413        struct wl1273_core *core = radio->core;
1414        int r = 0;
1415
1416        dev_dbg(radio->dev, "%s\n", __func__);
1417
1418        switch (ctrl->id) {
1419        case V4L2_CID_AUDIO_MUTE:
1420                if (mutex_lock_interruptible(&core->lock))
1421                        return -EINTR;
1422
1423                if (core->mode == WL1273_MODE_RX && ctrl->val)
1424                        r = core->write(core,
1425                                        WL1273_MUTE_STATUS_SET,
1426                                        WL1273_MUTE_HARD_LEFT |
1427                                        WL1273_MUTE_HARD_RIGHT);
1428                else if (core->mode == WL1273_MODE_RX)
1429                        r = core->write(core,
1430                                        WL1273_MUTE_STATUS_SET, 0x0);
1431                else if (core->mode == WL1273_MODE_TX && ctrl->val)
1432                        r = core->write(core, WL1273_MUTE, 1);
1433                else if (core->mode == WL1273_MODE_TX)
1434                        r = core->write(core, WL1273_MUTE, 0);
1435
1436                mutex_unlock(&core->lock);
1437                break;
1438
1439        case V4L2_CID_AUDIO_VOLUME:
1440                if (ctrl->val == 0)
1441                        r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF);
1442                else
1443                        r =  core->set_volume(core, core->volume);
1444                break;
1445
1446        case V4L2_CID_TUNE_PREEMPHASIS:
1447                r = wl1273_fm_set_preemphasis(radio, ctrl->val);
1448                break;
1449
1450        case V4L2_CID_TUNE_POWER_LEVEL:
1451                r = wl1273_fm_set_tx_power(radio, ctrl->val);
1452                break;
1453
1454        default:
1455                dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n",
1456                         __func__, ctrl->id);
1457                break;
1458        }
1459
1460        dev_dbg(radio->dev, "%s\n", __func__);
1461        return r;
1462}
1463
1464static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv,
1465                                    struct v4l2_audio *audio)
1466{
1467        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1468
1469        dev_dbg(radio->dev, "%s\n", __func__);
1470
1471        if (audio->index > 1)
1472                return -EINVAL;
1473
1474        strlcpy(audio->name, "Radio", sizeof(audio->name));
1475        audio->capability = V4L2_AUDCAP_STEREO;
1476
1477        return 0;
1478}
1479
1480static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
1481                                    struct v4l2_audio *audio)
1482{
1483        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1484
1485        dev_dbg(radio->dev, "%s\n", __func__);
1486
1487        if (audio->index != 0)
1488                return -EINVAL;
1489
1490        return 0;
1491}
1492
1493#define WL1273_RDS_NOT_SYNCHRONIZED 0
1494#define WL1273_RDS_SYNCHRONIZED 1
1495
1496static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv,
1497                                    struct v4l2_tuner *tuner)
1498{
1499        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1500        struct wl1273_core *core = radio->core;
1501        u16 val;
1502        int r;
1503
1504        dev_dbg(radio->dev, "%s\n", __func__);
1505
1506        if (tuner->index > 0)
1507                return -EINVAL;
1508
1509        strlcpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name));
1510        tuner->type = V4L2_TUNER_RADIO;
1511
1512        tuner->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1513        tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1514
1515        tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
1516                V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
1517
1518        if (radio->stereo)
1519                tuner->audmode = V4L2_TUNER_MODE_STEREO;
1520        else
1521                tuner->audmode = V4L2_TUNER_MODE_MONO;
1522
1523        if (core->mode != WL1273_MODE_RX)
1524                return 0;
1525
1526        if (mutex_lock_interruptible(&core->lock))
1527                return -EINTR;
1528
1529        r = core->read(core, WL1273_STEREO_GET, &val);
1530        if (r)
1531                goto out;
1532
1533        if (val == 1)
1534                tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
1535        else
1536                tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1537
1538        r = core->read(core, WL1273_RSSI_LVL_GET, &val);
1539        if (r)
1540                goto out;
1541
1542        tuner->signal = (s16) val;
1543        dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
1544
1545        tuner->afc = 0;
1546
1547        r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1548        if (r)
1549                goto out;
1550
1551        if (val == WL1273_RDS_SYNCHRONIZED)
1552                tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
1553out:
1554        mutex_unlock(&core->lock);
1555
1556        return r;
1557}
1558
1559static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
1560                                    struct v4l2_tuner *tuner)
1561{
1562        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1563        struct wl1273_core *core = radio->core;
1564        int r = 0;
1565
1566        dev_dbg(radio->dev, "%s\n", __func__);
1567        dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
1568        dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
1569        dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
1570        dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
1571        dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
1572        dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);
1573
1574        if (tuner->index > 0)
1575                return -EINVAL;
1576
1577        if (mutex_lock_interruptible(&core->lock))
1578                return -EINTR;
1579
1580        r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1581        if (r)
1582                goto out;
1583
1584        if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
1585                r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1586        else
1587                r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1588
1589        if (r)
1590                dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);
1591
1592        if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
1593                r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
1594                if (r < 0) {
1595                        dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1596                                 __func__, r);
1597                        goto out;
1598                }
1599                radio->stereo = false;
1600        } else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
1601                r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
1602                if (r < 0) {
1603                        dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1604                                 __func__, r);
1605                        goto out;
1606                }
1607                radio->stereo = true;
1608        } else {
1609                dev_err(radio->dev, "%s: tuner->audmode: %d\n",
1610                         __func__, tuner->audmode);
1611                r = -EINVAL;
1612                goto out;
1613        }
1614
1615out:
1616        mutex_unlock(&core->lock);
1617
1618        return r;
1619}
1620
1621static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
1622                                        struct v4l2_frequency *freq)
1623{
1624        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1625        struct wl1273_core *core = radio->core;
1626
1627        dev_dbg(radio->dev, "%s\n", __func__);
1628
1629        if (mutex_lock_interruptible(&core->lock))
1630                return -EINTR;
1631
1632        freq->type = V4L2_TUNER_RADIO;
1633        freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));
1634
1635        mutex_unlock(&core->lock);
1636
1637        return 0;
1638}
1639
1640static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
1641                                        struct v4l2_frequency *freq)
1642{
1643        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1644        struct wl1273_core *core = radio->core;
1645        int r;
1646
1647        dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
1648
1649        if (freq->type != V4L2_TUNER_RADIO) {
1650                dev_dbg(radio->dev,
1651                        "freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
1652                return -EINVAL;
1653        }
1654
1655        if (mutex_lock_interruptible(&core->lock))
1656                return -EINTR;
1657
1658        if (core->mode == WL1273_MODE_RX) {
1659                dev_dbg(radio->dev, "freq: %d\n", freq->frequency);
1660
1661                r = wl1273_fm_set_rx_freq(radio,
1662                                          WL1273_INV_FREQ(freq->frequency));
1663                if (r)
1664                        dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1665                                 ": set frequency failed with %d\n", r);
1666        } else {
1667                r = wl1273_fm_set_tx_freq(radio,
1668                                          WL1273_INV_FREQ(freq->frequency));
1669                if (r)
1670                        dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1671                                 ": set frequency failed with %d\n", r);
1672        }
1673
1674        mutex_unlock(&core->lock);
1675
1676        dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
1677        return r;
1678}
1679
1680#define WL1273_DEFAULT_SEEK_LEVEL       7
1681
1682static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1683                                           struct v4l2_hw_freq_seek *seek)
1684{
1685        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1686        struct wl1273_core *core = radio->core;
1687        int r;
1688
1689        dev_dbg(radio->dev, "%s\n", __func__);
1690
1691        if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
1692                return -EINVAL;
1693
1694        if (mutex_lock_interruptible(&core->lock))
1695                return -EINTR;
1696
1697        r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1698        if (r)
1699                goto out;
1700
1701        r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
1702        if (r)
1703                dev_warn(radio->dev, "HW seek failed: %d\n", r);
1704
1705        r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
1706                               WL1273_DEFAULT_SEEK_LEVEL);
1707        if (r)
1708                dev_warn(radio->dev, "HW seek failed: %d\n", r);
1709
1710out:
1711        mutex_unlock(&core->lock);
1712        return r;
1713}
1714
1715static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
1716                                        struct v4l2_modulator *modulator)
1717{
1718        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1719        struct wl1273_core *core = radio->core;
1720        int r = 0;
1721
1722        dev_dbg(radio->dev, "%s\n", __func__);
1723
1724        if (modulator->index > 0)
1725                return -EINVAL;
1726
1727        if (mutex_lock_interruptible(&core->lock))
1728                return -EINTR;
1729
1730        r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
1731        if (r)
1732                goto out;
1733
1734        if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
1735                r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1736        else
1737                r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1738
1739        if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
1740                r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
1741        else
1742                r = core->write(core, WL1273_MONO_SET,
1743                                WL1273_RX_STEREO);
1744        if (r < 0)
1745                dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1746                         "MONO_SET fails: %d\n", r);
1747out:
1748        mutex_unlock(&core->lock);
1749
1750        return r;
1751}
1752
1753static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
1754                                        struct v4l2_modulator *modulator)
1755{
1756        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1757        struct wl1273_core *core = radio->core;
1758        u16 val;
1759        int r;
1760
1761        dev_dbg(radio->dev, "%s\n", __func__);
1762
1763        strlcpy(modulator->name, WL1273_FM_DRIVER_NAME,
1764                sizeof(modulator->name));
1765
1766        modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1767        modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1768
1769        modulator->capability =  V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
1770                V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
1771
1772        if (core->mode != WL1273_MODE_TX)
1773                return 0;
1774
1775        if (mutex_lock_interruptible(&core->lock))
1776                return -EINTR;
1777
1778        r = core->read(core, WL1273_MONO_SET, &val);
1779        if (r)
1780                goto out;
1781
1782        if (val == WL1273_TX_STEREO)
1783                modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
1784        else
1785                modulator->txsubchans = V4L2_TUNER_SUB_MONO;
1786
1787        if (radio->rds_on)
1788                modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
1789out:
1790        mutex_unlock(&core->lock);
1791
1792        return 0;
1793}
1794
1795static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
1796{
1797        struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1798        struct wl1273_core *core = radio->core;
1799        struct device *dev = radio->dev;
1800        u16 val;
1801        int r;
1802
1803        dev_info(dev, DRIVER_DESC);
1804
1805        if (core->mode == WL1273_MODE_OFF) {
1806                dev_info(dev, "Mode: Off\n");
1807                return 0;
1808        }
1809
1810        if (core->mode == WL1273_MODE_SUSPENDED) {
1811                dev_info(dev, "Mode: Suspended\n");
1812                return 0;
1813        }
1814
1815        r = core->read(core, WL1273_ASIC_ID_GET, &val);
1816        if (r)
1817                dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
1818        else
1819                dev_info(dev, "ASIC_ID: 0x%04x\n", val);
1820
1821        r = core->read(core, WL1273_ASIC_VER_GET, &val);
1822        if (r)
1823                dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
1824        else
1825                dev_info(dev, "ASIC Version: 0x%04x\n", val);
1826
1827        r = core->read(core, WL1273_FIRM_VER_GET, &val);
1828        if (r)
1829                dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
1830        else
1831                dev_info(dev, "FW version: %d(0x%04x)\n", val, val);
1832
1833        r = core->read(core, WL1273_BAND_SET, &val);
1834        if (r)
1835                dev_err(dev, "%s: Get BAND fails.\n", __func__);
1836        else
1837                dev_info(dev, "BAND: %d\n", val);
1838
1839        if (core->mode == WL1273_MODE_TX) {
1840                r = core->read(core, WL1273_PUPD_SET, &val);
1841                if (r)
1842                        dev_err(dev, "%s: Get PUPD fails.\n", __func__);
1843                else
1844                        dev_info(dev, "PUPD: 0x%04x\n", val);
1845
1846                r = core->read(core, WL1273_CHANL_SET, &val);
1847                if (r)
1848                        dev_err(dev, "%s: Get CHANL fails.\n", __func__);
1849                else
1850                        dev_info(dev, "Tx frequency: %dkHz\n", val*10);
1851        } else if (core->mode == WL1273_MODE_RX) {
1852                int bf = radio->rangelow;
1853
1854                r = core->read(core, WL1273_FREQ_SET, &val);
1855                if (r)
1856                        dev_err(dev, "%s: Get FREQ fails.\n", __func__);
1857                else
1858                        dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);
1859
1860                r = core->read(core, WL1273_MOST_MODE_SET, &val);
1861                if (r)
1862                        dev_err(dev, "%s: Get MOST_MODE fails.\n",
1863                                __func__);
1864                else if (val == 0)
1865                        dev_info(dev, "MOST_MODE: Stereo according to blend\n");
1866                else if (val == 1)
1867                        dev_info(dev, "MOST_MODE: Force mono output\n");
1868                else
1869                        dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);
1870
1871                r = core->read(core, WL1273_MOST_BLEND_SET, &val);
1872                if (r)
1873                        dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
1874                else if (val == 0)
1875                        dev_info(dev,
1876                                 "MOST_BLEND: Switched blend & hysteresis.\n");
1877                else if (val == 1)
1878                        dev_info(dev, "MOST_BLEND: Soft blend.\n");
1879                else
1880                        dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);
1881
1882                r = core->read(core, WL1273_STEREO_GET, &val);
1883                if (r)
1884                        dev_err(dev, "%s: Get STEREO fails.\n", __func__);
1885                else if (val == 0)
1886                        dev_info(dev, "STEREO: Not detected\n");
1887                else if (val == 1)
1888                        dev_info(dev, "STEREO: Detected\n");
1889                else
1890                        dev_info(dev, "STEREO: Unexpected value: %d\n", val);
1891
1892                r = core->read(core, WL1273_RSSI_LVL_GET, &val);
1893                if (r)
1894                        dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
1895                else
1896                        dev_info(dev, "RX signal strength: %d\n", (s16) val);
1897
1898                r = core->read(core, WL1273_POWER_SET, &val);
1899                if (r)
1900                        dev_err(dev, "%s: Get POWER fails.\n", __func__);
1901                else
1902                        dev_info(dev, "POWER: 0x%04x\n", val);
1903
1904                r = core->read(core, WL1273_INT_MASK_SET, &val);
1905                if (r)
1906                        dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
1907                else
1908                        dev_info(dev, "INT_MASK: 0x%04x\n", val);
1909
1910                r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1911                if (r)
1912                        dev_err(dev, "%s: Get RDS_SYNC fails.\n",
1913                                __func__);
1914                else if (val == 0)
1915                        dev_info(dev, "RDS_SYNC: Not synchronized\n");
1916
1917                else if (val == 1)
1918                        dev_info(dev, "RDS_SYNC: Synchronized\n");
1919                else
1920                        dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);
1921
1922                r = core->read(core, WL1273_I2S_MODE_CONFIG_SET, &val);
1923                if (r)
1924                        dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
1925                                __func__);
1926                else
1927                        dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);
1928
1929                r = core->read(core, WL1273_VOLUME_SET, &val);
1930                if (r)
1931                        dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
1932                else
1933                        dev_info(dev, "VOLUME: 0x%04x\n", val);
1934        }
1935
1936        return 0;
1937}
1938
1939static void wl1273_vdev_release(struct video_device *dev)
1940{
1941}
1942
1943static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
1944        .s_ctrl = wl1273_fm_vidioc_s_ctrl,
1945        .g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
1946};
1947
1948static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
1949        .vidioc_querycap        = wl1273_fm_vidioc_querycap,
1950        .vidioc_g_input         = wl1273_fm_vidioc_g_input,
1951        .vidioc_s_input         = wl1273_fm_vidioc_s_input,
1952        .vidioc_g_audio         = wl1273_fm_vidioc_g_audio,
1953        .vidioc_s_audio         = wl1273_fm_vidioc_s_audio,
1954        .vidioc_g_tuner         = wl1273_fm_vidioc_g_tuner,
1955        .vidioc_s_tuner         = wl1273_fm_vidioc_s_tuner,
1956        .vidioc_g_frequency     = wl1273_fm_vidioc_g_frequency,
1957        .vidioc_s_frequency     = wl1273_fm_vidioc_s_frequency,
1958        .vidioc_s_hw_freq_seek  = wl1273_fm_vidioc_s_hw_freq_seek,
1959        .vidioc_g_modulator     = wl1273_fm_vidioc_g_modulator,
1960        .vidioc_s_modulator     = wl1273_fm_vidioc_s_modulator,
1961        .vidioc_log_status      = wl1273_fm_vidioc_log_status,
1962};
1963
1964static struct video_device wl1273_viddev_template = {
1965        .fops                   = &wl1273_fops,
1966        .ioctl_ops              = &wl1273_ioctl_ops,
1967        .name                   = WL1273_FM_DRIVER_NAME,
1968        .release                = wl1273_vdev_release,
1969};
1970
1971static int wl1273_fm_radio_remove(struct platform_device *pdev)
1972{
1973        struct wl1273_device *radio = platform_get_drvdata(pdev);
1974        struct wl1273_core *core = radio->core;
1975
1976        dev_info(&pdev->dev, "%s.\n", __func__);
1977
1978        free_irq(core->client->irq, radio);
1979        core->pdata->free_resources();
1980
1981        v4l2_ctrl_handler_free(&radio->ctrl_handler);
1982        video_unregister_device(&radio->videodev);
1983        v4l2_device_unregister(&radio->v4l2dev);
1984        kfree(radio->buffer);
1985        kfree(radio->write_buf);
1986        kfree(radio);
1987
1988        return 0;
1989}
1990
1991static int __devinit wl1273_fm_radio_probe(struct platform_device *pdev)
1992{
1993        struct wl1273_core **core = mfd_get_data(pdev);
1994        struct wl1273_device *radio;
1995        struct v4l2_ctrl *ctrl;
1996        int r = 0;
1997
1998        pr_debug("%s\n", __func__);
1999
2000        if (!core) {
2001                dev_err(&pdev->dev, "No platform data.\n");
2002                r = -EINVAL;
2003                goto pdata_err;
2004        }
2005
2006        radio = kzalloc(sizeof(*radio), GFP_KERNEL);
2007        if (!radio) {
2008                r = -ENOMEM;
2009                goto pdata_err;
2010        }
2011
2012        /* RDS buffer allocation */
2013        radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
2014        radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
2015        if (!radio->buffer) {
2016                pr_err("Cannot allocate memory for RDS buffer.\n");
2017                r = -ENOMEM;
2018                goto err_kmalloc;
2019        }
2020
2021        radio->core = *core;
2022        radio->irq_flags = WL1273_IRQ_MASK;
2023        radio->dev = &radio->core->client->dev;
2024        radio->rds_on = false;
2025        radio->core->mode = WL1273_MODE_OFF;
2026        radio->tx_power = 118;
2027        radio->core->audio_mode = WL1273_AUDIO_ANALOG;
2028        radio->band = WL1273_BAND_OTHER;
2029        radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
2030        radio->core->channel_number = 2;
2031        radio->core->volume = WL1273_DEFAULT_VOLUME;
2032        radio->rx_frequency = WL1273_BAND_OTHER_LOW;
2033        radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
2034        radio->rangelow = WL1273_BAND_OTHER_LOW;
2035        radio->rangehigh = WL1273_BAND_OTHER_HIGH;
2036        radio->stereo = true;
2037        radio->bus_type = "I2C";
2038
2039        if (radio->core->pdata->request_resources) {
2040                r = radio->core->pdata->request_resources(radio->core->client);
2041                if (r) {
2042                        dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2043                                ": Cannot get platform data\n");
2044                        goto err_resources;
2045                }
2046
2047                dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);
2048
2049                r = request_threaded_irq(radio->core->client->irq, NULL,
2050                                         wl1273_fm_irq_thread_handler,
2051                                         IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
2052                                         "wl1273-fm", radio);
2053                if (r < 0) {
2054                        dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2055                                ": Unable to register IRQ handler: %d\n", r);
2056                        goto err_request_irq;
2057                }
2058        } else {
2059                dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ"
2060                        " not configured");
2061                r = -EINVAL;
2062                goto err_resources;
2063        }
2064
2065        init_completion(&radio->busy);
2066        init_waitqueue_head(&radio->read_queue);
2067
2068        radio->write_buf = kmalloc(256, GFP_KERNEL);
2069        if (!radio->write_buf) {
2070                r = -ENOMEM;
2071                goto write_buf_err;
2072        }
2073
2074        radio->dev = &pdev->dev;
2075        radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
2076        radio->rds_users = 0;
2077
2078        r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
2079        if (r) {
2080                dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
2081                goto device_register_err;
2082        }
2083
2084        /* V4L2 configuration */
2085        memcpy(&radio->videodev, &wl1273_viddev_template,
2086               sizeof(wl1273_viddev_template));
2087
2088        radio->videodev.v4l2_dev = &radio->v4l2dev;
2089
2090        v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);
2091
2092        /* add in ascending ID order */
2093        v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2094                          V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
2095                          WL1273_DEFAULT_VOLUME);
2096
2097        v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2098                          V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
2099
2100        v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
2101                               V4L2_CID_TUNE_PREEMPHASIS,
2102                               V4L2_PREEMPHASIS_75_uS, 0x03,
2103                               V4L2_PREEMPHASIS_50_uS);
2104
2105        v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2106                          V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);
2107
2108        ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2109                                 V4L2_CID_TUNE_ANTENNA_CAPACITOR,
2110                                 0, 255, 1, 255);
2111        if (ctrl)
2112                ctrl->is_volatile = 1;
2113
2114        if (radio->ctrl_handler.error) {
2115                r = radio->ctrl_handler.error;
2116                dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
2117                goto handler_init_err;
2118        }
2119
2120        video_set_drvdata(&radio->videodev, radio);
2121        platform_set_drvdata(pdev, radio);
2122
2123        /* register video device */
2124        r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
2125        if (r) {
2126                dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
2127                        ": Could not register video device\n");
2128                goto handler_init_err;
2129        }
2130
2131        return 0;
2132
2133handler_init_err:
2134        v4l2_ctrl_handler_free(&radio->ctrl_handler);
2135        v4l2_device_unregister(&radio->v4l2dev);
2136device_register_err:
2137        kfree(radio->write_buf);
2138write_buf_err:
2139        free_irq(radio->core->client->irq, radio);
2140err_request_irq:
2141        radio->core->pdata->free_resources();
2142err_resources:
2143        kfree(radio->buffer);
2144err_kmalloc:
2145        kfree(radio);
2146pdata_err:
2147        return r;
2148}
2149
2150MODULE_ALIAS("platform:wl1273_fm_radio");
2151
2152static struct platform_driver wl1273_fm_radio_driver = {
2153        .probe          = wl1273_fm_radio_probe,
2154        .remove         = __devexit_p(wl1273_fm_radio_remove),
2155        .driver         = {
2156                .name   = "wl1273_fm_radio",
2157                .owner  = THIS_MODULE,
2158        },
2159};
2160
2161static int __init wl1273_fm_module_init(void)
2162{
2163        pr_info("%s\n", __func__);
2164        return platform_driver_register(&wl1273_fm_radio_driver);
2165}
2166module_init(wl1273_fm_module_init);
2167
2168static void __exit wl1273_fm_module_exit(void)
2169{
2170        platform_driver_unregister(&wl1273_fm_radio_driver);
2171        pr_info(DRIVER_DESC ", Exiting.\n");
2172}
2173module_exit(wl1273_fm_module_exit);
2174
2175MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
2176MODULE_DESCRIPTION(DRIVER_DESC);
2177MODULE_LICENSE("GPL");
2178