Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Driver for the Texas Instruments WL1273 FM radio.
0004  *
0005  * Copyright (C) 2011 Nokia Corporation
0006  * Author: Matti J. Aaltonen <matti.j.aaltonen@nokia.com>
0007  */
0008 
0009 #include <linux/delay.h>
0010 #include <linux/firmware.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/mfd/wl1273-core.h>
0013 #include <linux/slab.h>
0014 #include <linux/module.h>
0015 #include <media/v4l2-common.h>
0016 #include <media/v4l2-ctrls.h>
0017 #include <media/v4l2-device.h>
0018 #include <media/v4l2-ioctl.h>
0019 
0020 #define DRIVER_DESC "Wl1273 FM Radio"
0021 
0022 #define WL1273_POWER_SET_OFF        0
0023 #define WL1273_POWER_SET_FM     BIT(0)
0024 #define WL1273_POWER_SET_RDS        BIT(1)
0025 #define WL1273_POWER_SET_RETENTION  BIT(4)
0026 
0027 #define WL1273_PUPD_SET_OFF     0x00
0028 #define WL1273_PUPD_SET_ON      0x01
0029 #define WL1273_PUPD_SET_RETENTION   0x10
0030 
0031 #define WL1273_FREQ(x)      (x * 10000 / 625)
0032 #define WL1273_INV_FREQ(x)  (x * 625 / 10000)
0033 
0034 /*
0035  * static int radio_nr - The number of the radio device
0036  *
0037  * The default is 0.
0038  */
0039 static int radio_nr;
0040 module_param(radio_nr, int, 0);
0041 MODULE_PARM_DESC(radio_nr, "The number of the radio device. Default = 0");
0042 
0043 struct wl1273_device {
0044     char *bus_type;
0045 
0046     u8 forbidden;
0047     unsigned int preemphasis;
0048     unsigned int spacing;
0049     unsigned int tx_power;
0050     unsigned int rx_frequency;
0051     unsigned int tx_frequency;
0052     unsigned int rangelow;
0053     unsigned int rangehigh;
0054     unsigned int band;
0055     bool stereo;
0056 
0057     /* RDS */
0058     unsigned int rds_on;
0059 
0060     wait_queue_head_t read_queue;
0061     struct mutex lock; /* for serializing fm radio operations */
0062     struct completion busy;
0063 
0064     unsigned char *buffer;
0065     unsigned int buf_size;
0066     unsigned int rd_index;
0067     unsigned int wr_index;
0068 
0069     /* Selected interrupts */
0070     u16 irq_flags;
0071     u16 irq_received;
0072 
0073     struct v4l2_ctrl_handler ctrl_handler;
0074     struct v4l2_device v4l2dev;
0075     struct video_device videodev;
0076     struct device *dev;
0077     struct wl1273_core *core;
0078     struct file *owner;
0079     char *write_buf;
0080     unsigned int rds_users;
0081 };
0082 
0083 #define WL1273_IRQ_MASK  (WL1273_FR_EVENT       |   \
0084               WL1273_POW_ENB_EVENT)
0085 
0086 /*
0087  * static unsigned int rds_buf - the number of RDS buffer blocks used.
0088  *
0089  * The default number is 100.
0090  */
0091 static unsigned int rds_buf = 100;
0092 module_param(rds_buf, uint, 0);
0093 MODULE_PARM_DESC(rds_buf, "Number of RDS buffer entries. Default = 100");
0094 
0095 static int wl1273_fm_write_fw(struct wl1273_core *core,
0096                   __u8 *fw, int len)
0097 {
0098     struct i2c_client *client = core->client;
0099     struct i2c_msg msg;
0100     int i, r = 0;
0101 
0102     msg.addr = client->addr;
0103     msg.flags = 0;
0104 
0105     for (i = 0; i <= len; i++) {
0106         msg.len = fw[0];
0107         msg.buf = fw + 1;
0108 
0109         fw += msg.len + 1;
0110         dev_dbg(&client->dev, "%s:len[%d]: %d\n", __func__, i, msg.len);
0111 
0112         r = i2c_transfer(client->adapter, &msg, 1);
0113         if (r < 0 && i < len + 1)
0114             break;
0115     }
0116 
0117     dev_dbg(&client->dev, "%s: i: %d\n", __func__, i);
0118     dev_dbg(&client->dev, "%s: len + 1: %d\n", __func__, len + 1);
0119 
0120     /* Last transfer always fails. */
0121     if (i == len || r == 1)
0122         r = 0;
0123 
0124     return r;
0125 }
0126 
0127 #define WL1273_FIFO_HAS_DATA(status)    (1 << 5 & status)
0128 #define WL1273_RDS_CORRECTABLE_ERROR    (1 << 3)
0129 #define WL1273_RDS_UNCORRECTABLE_ERROR  (1 << 4)
0130 
0131 static int wl1273_fm_rds(struct wl1273_device *radio)
0132 {
0133     struct wl1273_core *core = radio->core;
0134     struct i2c_client *client = core->client;
0135     u16 val;
0136     u8 b0 = WL1273_RDS_DATA_GET, status;
0137     struct v4l2_rds_data rds = { 0, 0, 0 };
0138     struct i2c_msg msg[] = {
0139         {
0140             .addr = client->addr,
0141             .flags = 0,
0142             .buf = &b0,
0143             .len = 1,
0144         },
0145         {
0146             .addr = client->addr,
0147             .flags = I2C_M_RD,
0148             .buf = (u8 *) &rds,
0149             .len = sizeof(rds),
0150         }
0151     };
0152     int r;
0153 
0154     if (core->mode != WL1273_MODE_RX)
0155         return 0;
0156 
0157     r = core->read(core, WL1273_RDS_SYNC_GET, &val);
0158     if (r)
0159         return r;
0160 
0161     if ((val & 0x01) == 0) {
0162         /* RDS decoder not synchronized */
0163         return -EAGAIN;
0164     }
0165 
0166     /* copy all four RDS blocks to internal buffer */
0167     do {
0168         r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
0169         if (r != ARRAY_SIZE(msg)) {
0170             dev_err(radio->dev, WL1273_FM_DRIVER_NAME
0171                 ": %s: read_rds error r == %i)\n",
0172                 __func__, r);
0173         }
0174 
0175         status = rds.block;
0176 
0177         if (!WL1273_FIFO_HAS_DATA(status))
0178             break;
0179 
0180         /* copy bits 0-2 (the block ID) to bits 3-5 */
0181         rds.block = V4L2_RDS_BLOCK_MSK & status;
0182         rds.block |= rds.block << 3;
0183 
0184         /* copy the error bits to standard positions */
0185         if (WL1273_RDS_UNCORRECTABLE_ERROR & status) {
0186             rds.block |= V4L2_RDS_BLOCK_ERROR;
0187             rds.block &= ~V4L2_RDS_BLOCK_CORRECTED;
0188         } else if  (WL1273_RDS_CORRECTABLE_ERROR & status) {
0189             rds.block &= ~V4L2_RDS_BLOCK_ERROR;
0190             rds.block |= V4L2_RDS_BLOCK_CORRECTED;
0191         }
0192 
0193         /* copy RDS block to internal buffer */
0194         memcpy(&radio->buffer[radio->wr_index], &rds, RDS_BLOCK_SIZE);
0195         radio->wr_index += 3;
0196 
0197         /* wrap write pointer */
0198         if (radio->wr_index >= radio->buf_size)
0199             radio->wr_index = 0;
0200 
0201         /* check for overflow & start over */
0202         if (radio->wr_index == radio->rd_index) {
0203             dev_dbg(radio->dev, "RDS OVERFLOW");
0204 
0205             radio->rd_index = 0;
0206             radio->wr_index = 0;
0207             break;
0208         }
0209     } while (WL1273_FIFO_HAS_DATA(status));
0210 
0211     /* wake up read queue */
0212     if (radio->wr_index != radio->rd_index)
0213         wake_up_interruptible(&radio->read_queue);
0214 
0215     return 0;
0216 }
0217 
0218 static irqreturn_t wl1273_fm_irq_thread_handler(int irq, void *dev_id)
0219 {
0220     struct wl1273_device *radio = dev_id;
0221     struct wl1273_core *core = radio->core;
0222     u16 flags;
0223     int r;
0224 
0225     r = core->read(core, WL1273_FLAG_GET, &flags);
0226     if (r)
0227         goto out;
0228 
0229     if (flags & WL1273_BL_EVENT) {
0230         radio->irq_received = flags;
0231         dev_dbg(radio->dev, "IRQ: BL\n");
0232     }
0233 
0234     if (flags & WL1273_RDS_EVENT) {
0235         msleep(200);
0236 
0237         wl1273_fm_rds(radio);
0238     }
0239 
0240     if (flags & WL1273_BBLK_EVENT)
0241         dev_dbg(radio->dev, "IRQ: BBLK\n");
0242 
0243     if (flags & WL1273_LSYNC_EVENT)
0244         dev_dbg(radio->dev, "IRQ: LSYNC\n");
0245 
0246     if (flags & WL1273_LEV_EVENT) {
0247         u16 level;
0248 
0249         r = core->read(core, WL1273_RSSI_LVL_GET, &level);
0250         if (r)
0251             goto out;
0252 
0253         if (level > 14)
0254             dev_dbg(radio->dev, "IRQ: LEV: 0x%x04\n", level);
0255     }
0256 
0257     if (flags & WL1273_IFFR_EVENT)
0258         dev_dbg(radio->dev, "IRQ: IFFR\n");
0259 
0260     if (flags & WL1273_PI_EVENT)
0261         dev_dbg(radio->dev, "IRQ: PI\n");
0262 
0263     if (flags & WL1273_PD_EVENT)
0264         dev_dbg(radio->dev, "IRQ: PD\n");
0265 
0266     if (flags & WL1273_STIC_EVENT)
0267         dev_dbg(radio->dev, "IRQ: STIC\n");
0268 
0269     if (flags & WL1273_MAL_EVENT)
0270         dev_dbg(radio->dev, "IRQ: MAL\n");
0271 
0272     if (flags & WL1273_POW_ENB_EVENT) {
0273         complete(&radio->busy);
0274         dev_dbg(radio->dev, "NOT BUSY\n");
0275         dev_dbg(radio->dev, "IRQ: POW_ENB\n");
0276     }
0277 
0278     if (flags & WL1273_SCAN_OVER_EVENT)
0279         dev_dbg(radio->dev, "IRQ: SCAN_OVER\n");
0280 
0281     if (flags & WL1273_ERROR_EVENT)
0282         dev_dbg(radio->dev, "IRQ: ERROR\n");
0283 
0284     if (flags & WL1273_FR_EVENT) {
0285         u16 freq;
0286 
0287         dev_dbg(radio->dev, "IRQ: FR:\n");
0288 
0289         if (core->mode == WL1273_MODE_RX) {
0290             r = core->write(core, WL1273_TUNER_MODE_SET,
0291                     TUNER_MODE_STOP_SEARCH);
0292             if (r) {
0293                 dev_err(radio->dev,
0294                     "%s: TUNER_MODE_SET fails: %d\n",
0295                     __func__, r);
0296                 goto out;
0297             }
0298 
0299             r = core->read(core, WL1273_FREQ_SET, &freq);
0300             if (r)
0301                 goto out;
0302 
0303             if (radio->band == WL1273_BAND_JAPAN)
0304                 radio->rx_frequency = WL1273_BAND_JAPAN_LOW +
0305                     freq * 50;
0306             else
0307                 radio->rx_frequency = WL1273_BAND_OTHER_LOW +
0308                     freq * 50;
0309             /*
0310              *  The driver works better with this msleep,
0311              *  the documentation doesn't mention it.
0312              */
0313             usleep_range(10000, 15000);
0314 
0315             dev_dbg(radio->dev, "%dkHz\n", radio->rx_frequency);
0316 
0317         } else {
0318             r = core->read(core, WL1273_CHANL_SET, &freq);
0319             if (r)
0320                 goto out;
0321 
0322             dev_dbg(radio->dev, "%dkHz\n", freq);
0323         }
0324         dev_dbg(radio->dev, "%s: NOT BUSY\n", __func__);
0325     }
0326 
0327 out:
0328     core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
0329     complete(&radio->busy);
0330 
0331     return IRQ_HANDLED;
0332 }
0333 
0334 static int wl1273_fm_set_tx_freq(struct wl1273_device *radio, unsigned int freq)
0335 {
0336     struct wl1273_core *core = radio->core;
0337     int r = 0;
0338     unsigned long t;
0339 
0340     if (freq < WL1273_BAND_TX_LOW) {
0341         dev_err(radio->dev,
0342             "Frequency out of range: %d < %d\n", freq,
0343             WL1273_BAND_TX_LOW);
0344         return -ERANGE;
0345     }
0346 
0347     if (freq > WL1273_BAND_TX_HIGH) {
0348         dev_err(radio->dev,
0349             "Frequency out of range: %d > %d\n", freq,
0350             WL1273_BAND_TX_HIGH);
0351         return -ERANGE;
0352     }
0353 
0354     /*
0355      *  The driver works better with this sleep,
0356      *  the documentation doesn't mention it.
0357      */
0358     usleep_range(5000, 10000);
0359 
0360     dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq);
0361 
0362     /* Set the current tx channel */
0363     r = core->write(core, WL1273_CHANL_SET, freq / 10);
0364     if (r)
0365         return r;
0366 
0367     reinit_completion(&radio->busy);
0368 
0369     /* wait for the FR IRQ */
0370     t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
0371     if (!t)
0372         return -ETIMEDOUT;
0373 
0374     dev_dbg(radio->dev, "WL1273_CHANL_SET: %lu\n", t);
0375 
0376     /* Enable the output power */
0377     r = core->write(core, WL1273_POWER_ENB_SET, 1);
0378     if (r)
0379         return r;
0380 
0381     reinit_completion(&radio->busy);
0382 
0383     /* wait for the POWER_ENB IRQ */
0384     t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
0385     if (!t)
0386         return -ETIMEDOUT;
0387 
0388     radio->tx_frequency = freq;
0389     dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %lu\n", t);
0390 
0391     return  0;
0392 }
0393 
0394 static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq)
0395 {
0396     struct wl1273_core *core = radio->core;
0397     int r, f;
0398     unsigned long t;
0399 
0400     if (freq < radio->rangelow) {
0401         dev_err(radio->dev,
0402             "Frequency out of range: %d < %d\n", freq,
0403             radio->rangelow);
0404         r = -ERANGE;
0405         goto err;
0406     }
0407 
0408     if (freq > radio->rangehigh) {
0409         dev_err(radio->dev,
0410             "Frequency out of range: %d > %d\n", freq,
0411             radio->rangehigh);
0412         r = -ERANGE;
0413         goto err;
0414     }
0415 
0416     dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq);
0417 
0418     core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
0419 
0420     if (radio->band == WL1273_BAND_JAPAN)
0421         f = (freq - WL1273_BAND_JAPAN_LOW) / 50;
0422     else
0423         f = (freq - WL1273_BAND_OTHER_LOW) / 50;
0424 
0425     r = core->write(core, WL1273_FREQ_SET, f);
0426     if (r) {
0427         dev_err(radio->dev, "FREQ_SET fails\n");
0428         goto err;
0429     }
0430 
0431     r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET);
0432     if (r) {
0433         dev_err(radio->dev, "TUNER_MODE_SET fails\n");
0434         goto err;
0435     }
0436 
0437     reinit_completion(&radio->busy);
0438 
0439     t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
0440     if (!t) {
0441         dev_err(radio->dev, "%s: TIMEOUT\n", __func__);
0442         return -ETIMEDOUT;
0443     }
0444 
0445     radio->rd_index = 0;
0446     radio->wr_index = 0;
0447     radio->rx_frequency = freq;
0448     return 0;
0449 err:
0450     return r;
0451 }
0452 
0453 static int wl1273_fm_get_freq(struct wl1273_device *radio)
0454 {
0455     struct wl1273_core *core = radio->core;
0456     unsigned int freq;
0457     u16 f;
0458     int r;
0459 
0460     if (core->mode == WL1273_MODE_RX) {
0461         r = core->read(core, WL1273_FREQ_SET, &f);
0462         if (r)
0463             return r;
0464 
0465         dev_dbg(radio->dev, "Freq get: 0x%04x\n", f);
0466         if (radio->band == WL1273_BAND_JAPAN)
0467             freq = WL1273_BAND_JAPAN_LOW + 50 * f;
0468         else
0469             freq = WL1273_BAND_OTHER_LOW + 50 * f;
0470     } else {
0471         r = core->read(core, WL1273_CHANL_SET, &f);
0472         if (r)
0473             return r;
0474 
0475         freq = f * 10;
0476     }
0477 
0478     return freq;
0479 }
0480 
0481 /**
0482  * wl1273_fm_upload_firmware_patch() -  Upload the firmware.
0483  * @radio:              A pointer to the device struct.
0484  *
0485  * The firmware file consists of arrays of bytes where the first byte
0486  * gives the array length. The first byte in the file gives the
0487  * number of these arrays.
0488  */
0489 static int wl1273_fm_upload_firmware_patch(struct wl1273_device *radio)
0490 {
0491     struct wl1273_core *core = radio->core;
0492     unsigned int packet_num;
0493     const struct firmware *fw_p;
0494     const char *fw_name = "radio-wl1273-fw.bin";
0495     struct device *dev = radio->dev;
0496     __u8 *ptr;
0497     int r;
0498 
0499     dev_dbg(dev, "%s:\n", __func__);
0500 
0501     /*
0502      * Uploading the firmware patch is not always necessary,
0503      * so we only print an info message.
0504      */
0505     if (request_firmware(&fw_p, fw_name, dev)) {
0506         dev_info(dev, "%s - %s not found\n", __func__, fw_name);
0507 
0508         return 0;
0509     }
0510 
0511     ptr = (__u8 *) fw_p->data;
0512     packet_num = ptr[0];
0513     dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num);
0514 
0515     r = wl1273_fm_write_fw(core, ptr + 1, packet_num);
0516     if (r) {
0517         dev_err(dev, "FW upload error: %d\n", r);
0518         goto out;
0519     }
0520 
0521     /* ignore possible error here */
0522     core->write(core, WL1273_RESET, 0);
0523 
0524     dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r);
0525 out:
0526     release_firmware(fw_p);
0527     return r;
0528 }
0529 
0530 static int wl1273_fm_stop(struct wl1273_device *radio)
0531 {
0532     struct wl1273_core *core = radio->core;
0533 
0534     if (core->mode == WL1273_MODE_RX) {
0535         int r = core->write(core, WL1273_POWER_SET,
0536                     WL1273_POWER_SET_OFF);
0537         if (r)
0538             dev_err(radio->dev, "%s: POWER_SET fails: %d\n",
0539                 __func__, r);
0540     } else if (core->mode == WL1273_MODE_TX) {
0541         int r = core->write(core, WL1273_PUPD_SET,
0542                     WL1273_PUPD_SET_OFF);
0543         if (r)
0544             dev_err(radio->dev,
0545                 "%s: PUPD_SET fails: %d\n", __func__, r);
0546     }
0547 
0548     if (core->pdata->disable) {
0549         core->pdata->disable();
0550         dev_dbg(radio->dev, "Back to reset\n");
0551     }
0552 
0553     return 0;
0554 }
0555 
0556 static int wl1273_fm_start(struct wl1273_device *radio, int new_mode)
0557 {
0558     struct wl1273_core *core = radio->core;
0559     struct wl1273_fm_platform_data *pdata = core->pdata;
0560     struct device *dev = radio->dev;
0561     int r = -EINVAL;
0562 
0563     if (pdata->enable && core->mode == WL1273_MODE_OFF) {
0564         dev_dbg(radio->dev, "Out of reset\n");
0565 
0566         pdata->enable();
0567         msleep(250);
0568     }
0569 
0570     if (new_mode == WL1273_MODE_RX) {
0571         u16 val = WL1273_POWER_SET_FM;
0572 
0573         if (radio->rds_on)
0574             val |= WL1273_POWER_SET_RDS;
0575 
0576         /* If this fails try again */
0577         r = core->write(core, WL1273_POWER_SET, val);
0578         if (r) {
0579             msleep(100);
0580 
0581             r = core->write(core, WL1273_POWER_SET, val);
0582             if (r) {
0583                 dev_err(dev, "%s: POWER_SET fails\n", __func__);
0584                 goto fail;
0585             }
0586         }
0587 
0588         /* rds buffer configuration */
0589         radio->wr_index = 0;
0590         radio->rd_index = 0;
0591 
0592     } else if (new_mode == WL1273_MODE_TX) {
0593         /* If this fails try again once */
0594         r = core->write(core, WL1273_PUPD_SET, WL1273_PUPD_SET_ON);
0595         if (r) {
0596             msleep(100);
0597             r = core->write(core, WL1273_PUPD_SET,
0598                     WL1273_PUPD_SET_ON);
0599             if (r) {
0600                 dev_err(dev, "%s: PUPD_SET fails\n", __func__);
0601                 goto fail;
0602             }
0603         }
0604 
0605         if (radio->rds_on) {
0606             r = core->write(core, WL1273_RDS_DATA_ENB, 1);
0607             if (r) {
0608                 dev_err(dev, "%s: RDS_DATA_ENB ON fails\n",
0609                     __func__);
0610                 goto fail;
0611             }
0612         } else {
0613             r = core->write(core, WL1273_RDS_DATA_ENB, 0);
0614             if (r) {
0615                 dev_err(dev, "%s: RDS_DATA_ENB OFF fails\n",
0616                     __func__);
0617                 goto fail;
0618             }
0619         }
0620     } else {
0621         dev_warn(dev, "%s: Illegal mode.\n", __func__);
0622     }
0623 
0624     if (core->mode == WL1273_MODE_OFF) {
0625         r = wl1273_fm_upload_firmware_patch(radio);
0626         if (r)
0627             dev_warn(dev, "Firmware upload failed.\n");
0628 
0629         /*
0630          * Sometimes the chip is in a wrong power state at this point.
0631          * So we set the power once again.
0632          */
0633         if (new_mode == WL1273_MODE_RX) {
0634             u16 val = WL1273_POWER_SET_FM;
0635 
0636             if (radio->rds_on)
0637                 val |= WL1273_POWER_SET_RDS;
0638 
0639             r = core->write(core, WL1273_POWER_SET, val);
0640             if (r) {
0641                 dev_err(dev, "%s: POWER_SET fails\n", __func__);
0642                 goto fail;
0643             }
0644         } else if (new_mode == WL1273_MODE_TX) {
0645             r = core->write(core, WL1273_PUPD_SET,
0646                     WL1273_PUPD_SET_ON);
0647             if (r) {
0648                 dev_err(dev, "%s: PUPD_SET fails\n", __func__);
0649                 goto fail;
0650             }
0651         }
0652     }
0653 
0654     return 0;
0655 fail:
0656     if (pdata->disable)
0657         pdata->disable();
0658 
0659     dev_dbg(dev, "%s: return: %d\n", __func__, r);
0660     return r;
0661 }
0662 
0663 static int wl1273_fm_suspend(struct wl1273_device *radio)
0664 {
0665     struct wl1273_core *core = radio->core;
0666     int r;
0667 
0668     /* Cannot go from OFF to SUSPENDED */
0669     if (core->mode == WL1273_MODE_RX)
0670         r = core->write(core, WL1273_POWER_SET,
0671                 WL1273_POWER_SET_RETENTION);
0672     else if (core->mode == WL1273_MODE_TX)
0673         r = core->write(core, WL1273_PUPD_SET,
0674                 WL1273_PUPD_SET_RETENTION);
0675     else
0676         r = -EINVAL;
0677 
0678     if (r) {
0679         dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
0680         goto out;
0681     }
0682 
0683 out:
0684     return r;
0685 }
0686 
0687 static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
0688 {
0689     struct wl1273_core *core = radio->core;
0690     struct device *dev = radio->dev;
0691     int old_mode;
0692     int r;
0693 
0694     dev_dbg(dev, "%s\n", __func__);
0695     dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);
0696 
0697     old_mode = core->mode;
0698     if (mode & radio->forbidden) {
0699         r = -EPERM;
0700         goto out;
0701     }
0702 
0703     switch (mode) {
0704     case WL1273_MODE_RX:
0705     case WL1273_MODE_TX:
0706         r = wl1273_fm_start(radio, mode);
0707         if (r) {
0708             dev_err(dev, "%s: Cannot start.\n", __func__);
0709             wl1273_fm_stop(radio);
0710             goto out;
0711         }
0712 
0713         core->mode = mode;
0714         r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
0715         if (r) {
0716             dev_err(dev, "INT_MASK_SET fails.\n");
0717             goto out;
0718         }
0719 
0720         /* remember previous settings */
0721         if (mode == WL1273_MODE_RX) {
0722             r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
0723             if (r) {
0724                 dev_err(dev, "set freq fails: %d.\n", r);
0725                 goto out;
0726             }
0727 
0728             r = core->set_volume(core, core->volume);
0729             if (r) {
0730                 dev_err(dev, "set volume fails: %d.\n", r);
0731                 goto out;
0732             }
0733 
0734             dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
0735                 core->volume);
0736         } else {
0737             r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
0738             if (r) {
0739                 dev_err(dev, "set freq fails: %d.\n", r);
0740                 goto out;
0741             }
0742         }
0743 
0744         dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);
0745 
0746         r = core->set_audio(core, core->audio_mode);
0747         if (r)
0748             dev_err(dev, "Cannot set audio mode.\n");
0749         break;
0750 
0751     case WL1273_MODE_OFF:
0752         r = wl1273_fm_stop(radio);
0753         if (r)
0754             dev_err(dev, "%s: Off fails: %d\n", __func__, r);
0755         else
0756             core->mode = WL1273_MODE_OFF;
0757 
0758         break;
0759 
0760     case WL1273_MODE_SUSPENDED:
0761         r = wl1273_fm_suspend(radio);
0762         if (r)
0763             dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
0764         else
0765             core->mode = WL1273_MODE_SUSPENDED;
0766 
0767         break;
0768 
0769     default:
0770         dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
0771         r = -EINVAL;
0772         break;
0773     }
0774 out:
0775     if (r)
0776         core->mode = old_mode;
0777 
0778     return r;
0779 }
0780 
0781 static int wl1273_fm_set_seek(struct wl1273_device *radio,
0782                   unsigned int wrap_around,
0783                   unsigned int seek_upward,
0784                   int level)
0785 {
0786     struct wl1273_core *core = radio->core;
0787     int r = 0;
0788     unsigned int dir = (seek_upward == 0) ? 0 : 1;
0789     unsigned int f;
0790 
0791     f = radio->rx_frequency;
0792     dev_dbg(radio->dev, "rx_frequency: %d\n", f);
0793 
0794     if (dir && f + radio->spacing <= radio->rangehigh)
0795         r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
0796     else if (dir && wrap_around)
0797         r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
0798     else if (f - radio->spacing >= radio->rangelow)
0799         r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
0800     else if (wrap_around)
0801         r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);
0802 
0803     if (r)
0804         goto out;
0805 
0806     if (level < SCHAR_MIN || level > SCHAR_MAX)
0807         return -EINVAL;
0808 
0809     reinit_completion(&radio->busy);
0810     dev_dbg(radio->dev, "%s: BUSY\n", __func__);
0811 
0812     r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
0813     if (r)
0814         goto out;
0815 
0816     dev_dbg(radio->dev, "%s\n", __func__);
0817 
0818     r = core->write(core, WL1273_SEARCH_LVL_SET, level);
0819     if (r)
0820         goto out;
0821 
0822     r = core->write(core, WL1273_SEARCH_DIR_SET, dir);
0823     if (r)
0824         goto out;
0825 
0826     r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
0827     if (r)
0828         goto out;
0829 
0830     /* wait for the FR IRQ */
0831     wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
0832     if (!(radio->irq_received & WL1273_BL_EVENT)) {
0833         r = -ETIMEDOUT;
0834         goto out;
0835     }
0836 
0837     radio->irq_received &= ~WL1273_BL_EVENT;
0838 
0839     if (!wrap_around)
0840         goto out;
0841 
0842     /* Wrap around */
0843     dev_dbg(radio->dev, "Wrap around in HW seek.\n");
0844 
0845     if (seek_upward)
0846         f = radio->rangelow;
0847     else
0848         f = radio->rangehigh;
0849 
0850     r = wl1273_fm_set_rx_freq(radio, f);
0851     if (r)
0852         goto out;
0853 
0854     reinit_completion(&radio->busy);
0855     dev_dbg(radio->dev, "%s: BUSY\n", __func__);
0856 
0857     r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
0858     if (r)
0859         goto out;
0860 
0861     /* wait for the FR IRQ */
0862     if (!wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000)))
0863         r = -ETIMEDOUT;
0864 out:
0865     dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
0866     return r;
0867 }
0868 
0869 /**
0870  * wl1273_fm_get_tx_ctune() -   Get the TX tuning capacitor value.
0871  * @radio:          A pointer to the device struct.
0872  */
0873 static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
0874 {
0875     struct wl1273_core *core = radio->core;
0876     struct device *dev = radio->dev;
0877     u16 val;
0878     int r;
0879 
0880     if (core->mode == WL1273_MODE_OFF ||
0881         core->mode == WL1273_MODE_SUSPENDED)
0882         return -EPERM;
0883 
0884     r = core->read(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
0885     if (r) {
0886         dev_err(dev, "%s: read error: %d\n", __func__, r);
0887         goto out;
0888     }
0889 
0890 out:
0891     return val;
0892 }
0893 
0894 /**
0895  * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
0896  * @radio:           A pointer to the device struct.
0897  * @preemphasis:         The new pre-amphasis value.
0898  *
0899  * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
0900  * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
0901  */
0902 static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
0903                      unsigned int preemphasis)
0904 {
0905     struct wl1273_core *core = radio->core;
0906     int r;
0907     u16 em;
0908 
0909     if (core->mode == WL1273_MODE_OFF ||
0910         core->mode == WL1273_MODE_SUSPENDED)
0911         return -EPERM;
0912 
0913     mutex_lock(&core->lock);
0914 
0915     switch (preemphasis) {
0916     case V4L2_PREEMPHASIS_DISABLED:
0917         em = 1;
0918         break;
0919     case V4L2_PREEMPHASIS_50_uS:
0920         em = 0;
0921         break;
0922     case V4L2_PREEMPHASIS_75_uS:
0923         em = 2;
0924         break;
0925     default:
0926         r = -EINVAL;
0927         goto out;
0928     }
0929 
0930     r = core->write(core, WL1273_PREMPH_SET, em);
0931     if (r)
0932         goto out;
0933 
0934     radio->preemphasis = preemphasis;
0935 
0936 out:
0937     mutex_unlock(&core->lock);
0938     return r;
0939 }
0940 
0941 static int wl1273_fm_rds_on(struct wl1273_device *radio)
0942 {
0943     struct wl1273_core *core = radio->core;
0944     int r;
0945 
0946     dev_dbg(radio->dev, "%s\n", __func__);
0947     if (radio->rds_on)
0948         return 0;
0949 
0950     r = core->write(core, WL1273_POWER_SET,
0951             WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
0952     if (r)
0953         goto out;
0954 
0955     r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
0956     if (r)
0957         dev_err(radio->dev, "set freq fails: %d.\n", r);
0958 out:
0959     return r;
0960 }
0961 
0962 static int wl1273_fm_rds_off(struct wl1273_device *radio)
0963 {
0964     struct wl1273_core *core = radio->core;
0965     int r;
0966 
0967     if (!radio->rds_on)
0968         return 0;
0969 
0970     radio->irq_flags &= ~WL1273_RDS_EVENT;
0971 
0972     r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
0973     if (r)
0974         goto out;
0975 
0976     /* Service pending read */
0977     wake_up_interruptible(&radio->read_queue);
0978 
0979     dev_dbg(radio->dev, "%s\n", __func__);
0980 
0981     r = core->write(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
0982     if (r)
0983         goto out;
0984 
0985     r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
0986     if (r)
0987         dev_err(radio->dev, "set freq fails: %d.\n", r);
0988 out:
0989     dev_dbg(radio->dev, "%s: exiting...\n", __func__);
0990 
0991     return r;
0992 }
0993 
0994 static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode)
0995 {
0996     int r = 0;
0997     struct wl1273_core *core = radio->core;
0998 
0999     if (core->mode == WL1273_MODE_OFF ||
1000         core->mode == WL1273_MODE_SUSPENDED)
1001         return -EPERM;
1002 
1003     if (new_mode == WL1273_RDS_RESET) {
1004         r = core->write(core, WL1273_RDS_CNTRL_SET, 1);
1005         return r;
1006     }
1007 
1008     if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) {
1009         r = core->write(core, WL1273_RDS_DATA_ENB, 0);
1010     } else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) {
1011         r = core->write(core, WL1273_RDS_DATA_ENB, 1);
1012     } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) {
1013         r = wl1273_fm_rds_off(radio);
1014     } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) {
1015         r = wl1273_fm_rds_on(radio);
1016     } else {
1017         dev_err(radio->dev, "%s: Unknown mode: %d\n",
1018             __func__, new_mode);
1019         r = -EINVAL;
1020     }
1021 
1022     if (!r)
1023         radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false;
1024 
1025     return r;
1026 }
1027 
1028 static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf,
1029                     size_t count, loff_t *ppos)
1030 {
1031     struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1032     struct wl1273_core *core = radio->core;
1033     u16 val;
1034     int r;
1035 
1036     dev_dbg(radio->dev, "%s\n", __func__);
1037 
1038     if (core->mode != WL1273_MODE_TX)
1039         return count;
1040 
1041     if (radio->rds_users == 0) {
1042         dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1043         return 0;
1044     }
1045 
1046     if (mutex_lock_interruptible(&core->lock))
1047         return -EINTR;
1048     /*
1049      * Multiple processes can open the device, but only
1050      * one gets to write to it.
1051      */
1052     if (radio->owner && radio->owner != file) {
1053         r = -EBUSY;
1054         goto out;
1055     }
1056     radio->owner = file;
1057 
1058     /* Manual Mode */
1059     if (count > 255)
1060         val = 255;
1061     else
1062         val = count;
1063 
1064     core->write(core, WL1273_RDS_CONFIG_DATA_SET, val);
1065 
1066     if (copy_from_user(radio->write_buf + 1, buf, val)) {
1067         r = -EFAULT;
1068         goto out;
1069     }
1070 
1071     dev_dbg(radio->dev, "Count: %d\n", val);
1072     dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf);
1073 
1074     radio->write_buf[0] = WL1273_RDS_DATA_SET;
1075     core->write_data(core, radio->write_buf, val + 1);
1076 
1077     r = val;
1078 out:
1079     mutex_unlock(&core->lock);
1080 
1081     return r;
1082 }
1083 
1084 static __poll_t wl1273_fm_fops_poll(struct file *file,
1085                     struct poll_table_struct *pts)
1086 {
1087     struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1088     struct wl1273_core *core = radio->core;
1089 
1090     if (radio->owner && radio->owner != file)
1091         return EPOLLERR;
1092 
1093     radio->owner = file;
1094 
1095     if (core->mode == WL1273_MODE_RX) {
1096         poll_wait(file, &radio->read_queue, pts);
1097 
1098         if (radio->rd_index != radio->wr_index)
1099             return EPOLLIN | EPOLLRDNORM;
1100 
1101     } else if (core->mode == WL1273_MODE_TX) {
1102         return EPOLLOUT | EPOLLWRNORM;
1103     }
1104 
1105     return 0;
1106 }
1107 
1108 static int wl1273_fm_fops_open(struct file *file)
1109 {
1110     struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1111     struct wl1273_core *core = radio->core;
1112     int r = 0;
1113 
1114     dev_dbg(radio->dev, "%s\n", __func__);
1115 
1116     if (core->mode == WL1273_MODE_RX && radio->rds_on &&
1117         !radio->rds_users) {
1118         dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode);
1119 
1120         if (mutex_lock_interruptible(&core->lock))
1121             return -EINTR;
1122 
1123         radio->irq_flags |= WL1273_RDS_EVENT;
1124 
1125         r = core->write(core, WL1273_INT_MASK_SET,
1126                 radio->irq_flags);
1127         if (r) {
1128             mutex_unlock(&core->lock);
1129             goto out;
1130         }
1131 
1132         radio->rds_users++;
1133 
1134         mutex_unlock(&core->lock);
1135     }
1136 out:
1137     return r;
1138 }
1139 
1140 static int wl1273_fm_fops_release(struct file *file)
1141 {
1142     struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1143     struct wl1273_core *core = radio->core;
1144     int r = 0;
1145 
1146     dev_dbg(radio->dev, "%s\n", __func__);
1147 
1148     if (radio->rds_users > 0) {
1149         radio->rds_users--;
1150         if (radio->rds_users == 0) {
1151             mutex_lock(&core->lock);
1152 
1153             radio->irq_flags &= ~WL1273_RDS_EVENT;
1154 
1155             if (core->mode == WL1273_MODE_RX) {
1156                 r = core->write(core,
1157                         WL1273_INT_MASK_SET,
1158                         radio->irq_flags);
1159                 if (r) {
1160                     mutex_unlock(&core->lock);
1161                     goto out;
1162                 }
1163             }
1164             mutex_unlock(&core->lock);
1165         }
1166     }
1167 
1168     if (file == radio->owner)
1169         radio->owner = NULL;
1170 out:
1171     return r;
1172 }
1173 
1174 static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf,
1175                    size_t count, loff_t *ppos)
1176 {
1177     int r = 0;
1178     struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1179     struct wl1273_core *core = radio->core;
1180     unsigned int block_count = 0;
1181     u16 val;
1182 
1183     dev_dbg(radio->dev, "%s\n", __func__);
1184 
1185     if (core->mode != WL1273_MODE_RX)
1186         return 0;
1187 
1188     if (radio->rds_users == 0) {
1189         dev_warn(radio->dev, "%s: RDS not on.\n", __func__);
1190         return 0;
1191     }
1192 
1193     if (mutex_lock_interruptible(&core->lock))
1194         return -EINTR;
1195 
1196     /*
1197      * Multiple processes can open the device, but only
1198      * one at a time gets read access.
1199      */
1200     if (radio->owner && radio->owner != file) {
1201         r = -EBUSY;
1202         goto out;
1203     }
1204     radio->owner = file;
1205 
1206     r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1207     if (r) {
1208         dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__);
1209         goto out;
1210     } else if (val == 0) {
1211         dev_info(radio->dev, "RDS_SYNC: Not synchronized\n");
1212         r = -ENODATA;
1213         goto out;
1214     }
1215 
1216     /* block if no new data available */
1217     while (radio->wr_index == radio->rd_index) {
1218         if (file->f_flags & O_NONBLOCK) {
1219             r = -EWOULDBLOCK;
1220             goto out;
1221         }
1222 
1223         dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__);
1224         if (wait_event_interruptible(radio->read_queue,
1225                          radio->wr_index !=
1226                          radio->rd_index) < 0) {
1227             r = -EINTR;
1228             goto out;
1229         }
1230     }
1231 
1232     /* calculate block count from byte count */
1233     count /= RDS_BLOCK_SIZE;
1234 
1235     /* copy RDS blocks from the internal buffer and to user buffer */
1236     while (block_count < count) {
1237         if (radio->rd_index == radio->wr_index)
1238             break;
1239 
1240         /* always transfer complete RDS blocks */
1241         if (copy_to_user(buf, &radio->buffer[radio->rd_index],
1242                  RDS_BLOCK_SIZE))
1243             break;
1244 
1245         /* increment and wrap the read pointer */
1246         radio->rd_index += RDS_BLOCK_SIZE;
1247         if (radio->rd_index >= radio->buf_size)
1248             radio->rd_index = 0;
1249 
1250         /* increment counters */
1251         block_count++;
1252         buf += RDS_BLOCK_SIZE;
1253         r += RDS_BLOCK_SIZE;
1254     }
1255 
1256 out:
1257     dev_dbg(radio->dev, "%s: exit\n", __func__);
1258     mutex_unlock(&core->lock);
1259 
1260     return r;
1261 }
1262 
1263 static const struct v4l2_file_operations wl1273_fops = {
1264     .owner      = THIS_MODULE,
1265     .read       = wl1273_fm_fops_read,
1266     .write      = wl1273_fm_fops_write,
1267     .poll       = wl1273_fm_fops_poll,
1268     .unlocked_ioctl = video_ioctl2,
1269     .open       = wl1273_fm_fops_open,
1270     .release    = wl1273_fm_fops_release,
1271 };
1272 
1273 static int wl1273_fm_vidioc_querycap(struct file *file, void *priv,
1274                      struct v4l2_capability *capability)
1275 {
1276     struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1277 
1278     dev_dbg(radio->dev, "%s\n", __func__);
1279 
1280     strscpy(capability->driver, WL1273_FM_DRIVER_NAME,
1281         sizeof(capability->driver));
1282     strscpy(capability->card, "TI Wl1273 FM Radio",
1283         sizeof(capability->card));
1284     strscpy(capability->bus_info, radio->bus_type,
1285         sizeof(capability->bus_info));
1286     return 0;
1287 }
1288 
1289 static 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 
1301 static 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  * @radio:          A pointer to the device struct.
1317  * @power:          The new power value.
1318  */
1319 static 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 
1337 out:
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 
1346 static 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 
1373 static 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 
1405 static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl)
1406 {
1407     return container_of(ctrl->handler, struct wl1273_device, ctrl_handler);
1408 }
1409 
1410 static 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 
1464 static 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     strscpy(audio->name, "Radio", sizeof(audio->name));
1475     audio->capability = V4L2_AUDCAP_STEREO;
1476 
1477     return 0;
1478 }
1479 
1480 static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv,
1481                     const 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 
1496 static 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     strscpy(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         V4L2_TUNER_CAP_HWSEEK_BOUNDED | V4L2_TUNER_CAP_HWSEEK_WRAP;
1518 
1519     if (radio->stereo)
1520         tuner->audmode = V4L2_TUNER_MODE_STEREO;
1521     else
1522         tuner->audmode = V4L2_TUNER_MODE_MONO;
1523 
1524     if (core->mode != WL1273_MODE_RX)
1525         return 0;
1526 
1527     if (mutex_lock_interruptible(&core->lock))
1528         return -EINTR;
1529 
1530     r = core->read(core, WL1273_STEREO_GET, &val);
1531     if (r)
1532         goto out;
1533 
1534     if (val == 1)
1535         tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
1536     else
1537         tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
1538 
1539     r = core->read(core, WL1273_RSSI_LVL_GET, &val);
1540     if (r)
1541         goto out;
1542 
1543     tuner->signal = (s16) val;
1544     dev_dbg(radio->dev, "Signal: %d\n", tuner->signal);
1545 
1546     tuner->afc = 0;
1547 
1548     r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1549     if (r)
1550         goto out;
1551 
1552     if (val == WL1273_RDS_SYNCHRONIZED)
1553         tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
1554 out:
1555     mutex_unlock(&core->lock);
1556 
1557     return r;
1558 }
1559 
1560 static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv,
1561                     const struct v4l2_tuner *tuner)
1562 {
1563     struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1564     struct wl1273_core *core = radio->core;
1565     int r = 0;
1566 
1567     dev_dbg(radio->dev, "%s\n", __func__);
1568     dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index);
1569     dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name);
1570     dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability);
1571     dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans);
1572     dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow);
1573     dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh);
1574 
1575     if (tuner->index > 0)
1576         return -EINVAL;
1577 
1578     if (mutex_lock_interruptible(&core->lock))
1579         return -EINTR;
1580 
1581     r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1582     if (r)
1583         goto out;
1584 
1585     if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS)
1586         r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1587     else
1588         r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1589 
1590     if (r)
1591         dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r);
1592 
1593     if (tuner->audmode == V4L2_TUNER_MODE_MONO) {
1594         r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
1595         if (r < 0) {
1596             dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1597                  __func__, r);
1598             goto out;
1599         }
1600         radio->stereo = false;
1601     } else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) {
1602         r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
1603         if (r < 0) {
1604             dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n",
1605                  __func__, r);
1606             goto out;
1607         }
1608         radio->stereo = true;
1609     } else {
1610         dev_err(radio->dev, "%s: tuner->audmode: %d\n",
1611              __func__, tuner->audmode);
1612         r = -EINVAL;
1613         goto out;
1614     }
1615 
1616 out:
1617     mutex_unlock(&core->lock);
1618 
1619     return r;
1620 }
1621 
1622 static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv,
1623                     struct v4l2_frequency *freq)
1624 {
1625     struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1626     struct wl1273_core *core = radio->core;
1627 
1628     dev_dbg(radio->dev, "%s\n", __func__);
1629 
1630     if (mutex_lock_interruptible(&core->lock))
1631         return -EINTR;
1632 
1633     freq->type = V4L2_TUNER_RADIO;
1634     freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio));
1635 
1636     mutex_unlock(&core->lock);
1637 
1638     return 0;
1639 }
1640 
1641 static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv,
1642                     const struct v4l2_frequency *freq)
1643 {
1644     struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1645     struct wl1273_core *core = radio->core;
1646     int r;
1647 
1648     dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency);
1649 
1650     if (freq->type != V4L2_TUNER_RADIO) {
1651         dev_dbg(radio->dev,
1652             "freq->type != V4L2_TUNER_RADIO: %d\n", freq->type);
1653         return -EINVAL;
1654     }
1655 
1656     if (mutex_lock_interruptible(&core->lock))
1657         return -EINTR;
1658 
1659     if (core->mode == WL1273_MODE_RX) {
1660         dev_dbg(radio->dev, "freq: %d\n", freq->frequency);
1661 
1662         r = wl1273_fm_set_rx_freq(radio,
1663                       WL1273_INV_FREQ(freq->frequency));
1664         if (r)
1665             dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1666                  ": set frequency failed with %d\n", r);
1667     } else {
1668         r = wl1273_fm_set_tx_freq(radio,
1669                       WL1273_INV_FREQ(freq->frequency));
1670         if (r)
1671             dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1672                  ": set frequency failed with %d\n", r);
1673     }
1674 
1675     mutex_unlock(&core->lock);
1676 
1677     dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n");
1678     return r;
1679 }
1680 
1681 #define WL1273_DEFAULT_SEEK_LEVEL   7
1682 
1683 static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv,
1684                        const struct v4l2_hw_freq_seek *seek)
1685 {
1686     struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1687     struct wl1273_core *core = radio->core;
1688     int r;
1689 
1690     dev_dbg(radio->dev, "%s\n", __func__);
1691 
1692     if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO)
1693         return -EINVAL;
1694 
1695     if (file->f_flags & O_NONBLOCK)
1696         return -EWOULDBLOCK;
1697 
1698     if (mutex_lock_interruptible(&core->lock))
1699         return -EINTR;
1700 
1701     r = wl1273_fm_set_mode(radio, WL1273_MODE_RX);
1702     if (r)
1703         goto out;
1704 
1705     r = wl1273_fm_tx_set_spacing(radio, seek->spacing);
1706     if (r)
1707         dev_warn(radio->dev, "HW seek failed: %d\n", r);
1708 
1709     r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward,
1710                    WL1273_DEFAULT_SEEK_LEVEL);
1711     if (r)
1712         dev_warn(radio->dev, "HW seek failed: %d\n", r);
1713 
1714 out:
1715     mutex_unlock(&core->lock);
1716     return r;
1717 }
1718 
1719 static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv,
1720                     const struct v4l2_modulator *modulator)
1721 {
1722     struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1723     struct wl1273_core *core = radio->core;
1724     int r = 0;
1725 
1726     dev_dbg(radio->dev, "%s\n", __func__);
1727 
1728     if (modulator->index > 0)
1729         return -EINVAL;
1730 
1731     if (mutex_lock_interruptible(&core->lock))
1732         return -EINTR;
1733 
1734     r = wl1273_fm_set_mode(radio, WL1273_MODE_TX);
1735     if (r)
1736         goto out;
1737 
1738     if (modulator->txsubchans & V4L2_TUNER_SUB_RDS)
1739         r = wl1273_fm_set_rds(radio, WL1273_RDS_ON);
1740     else
1741         r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF);
1742 
1743     if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
1744         r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
1745     else
1746         r = core->write(core, WL1273_MONO_SET,
1747                 WL1273_RX_STEREO);
1748     if (r < 0)
1749         dev_warn(radio->dev, WL1273_FM_DRIVER_NAME
1750              "MONO_SET fails: %d\n", r);
1751 out:
1752     mutex_unlock(&core->lock);
1753 
1754     return r;
1755 }
1756 
1757 static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv,
1758                     struct v4l2_modulator *modulator)
1759 {
1760     struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1761     struct wl1273_core *core = radio->core;
1762     u16 val;
1763     int r;
1764 
1765     dev_dbg(radio->dev, "%s\n", __func__);
1766 
1767     strscpy(modulator->name, WL1273_FM_DRIVER_NAME,
1768         sizeof(modulator->name));
1769 
1770     modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW);
1771     modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH);
1772 
1773     modulator->capability =  V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS |
1774         V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO;
1775 
1776     if (core->mode != WL1273_MODE_TX)
1777         return 0;
1778 
1779     if (mutex_lock_interruptible(&core->lock))
1780         return -EINTR;
1781 
1782     r = core->read(core, WL1273_MONO_SET, &val);
1783     if (r)
1784         goto out;
1785 
1786     if (val == WL1273_TX_STEREO)
1787         modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
1788     else
1789         modulator->txsubchans = V4L2_TUNER_SUB_MONO;
1790 
1791     if (radio->rds_on)
1792         modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
1793 out:
1794     mutex_unlock(&core->lock);
1795 
1796     return 0;
1797 }
1798 
1799 static int wl1273_fm_vidioc_log_status(struct file *file, void *priv)
1800 {
1801     struct wl1273_device *radio = video_get_drvdata(video_devdata(file));
1802     struct wl1273_core *core = radio->core;
1803     struct device *dev = radio->dev;
1804     u16 val;
1805     int r;
1806 
1807     dev_info(dev, DRIVER_DESC);
1808 
1809     if (core->mode == WL1273_MODE_OFF) {
1810         dev_info(dev, "Mode: Off\n");
1811         return 0;
1812     }
1813 
1814     if (core->mode == WL1273_MODE_SUSPENDED) {
1815         dev_info(dev, "Mode: Suspended\n");
1816         return 0;
1817     }
1818 
1819     r = core->read(core, WL1273_ASIC_ID_GET, &val);
1820     if (r)
1821         dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__);
1822     else
1823         dev_info(dev, "ASIC_ID: 0x%04x\n", val);
1824 
1825     r = core->read(core, WL1273_ASIC_VER_GET, &val);
1826     if (r)
1827         dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__);
1828     else
1829         dev_info(dev, "ASIC Version: 0x%04x\n", val);
1830 
1831     r = core->read(core, WL1273_FIRM_VER_GET, &val);
1832     if (r)
1833         dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__);
1834     else
1835         dev_info(dev, "FW version: %d(0x%04x)\n", val, val);
1836 
1837     r = core->read(core, WL1273_BAND_SET, &val);
1838     if (r)
1839         dev_err(dev, "%s: Get BAND fails.\n", __func__);
1840     else
1841         dev_info(dev, "BAND: %d\n", val);
1842 
1843     if (core->mode == WL1273_MODE_TX) {
1844         r = core->read(core, WL1273_PUPD_SET, &val);
1845         if (r)
1846             dev_err(dev, "%s: Get PUPD fails.\n", __func__);
1847         else
1848             dev_info(dev, "PUPD: 0x%04x\n", val);
1849 
1850         r = core->read(core, WL1273_CHANL_SET, &val);
1851         if (r)
1852             dev_err(dev, "%s: Get CHANL fails.\n", __func__);
1853         else
1854             dev_info(dev, "Tx frequency: %dkHz\n", val*10);
1855     } else if (core->mode == WL1273_MODE_RX) {
1856         int bf = radio->rangelow;
1857 
1858         r = core->read(core, WL1273_FREQ_SET, &val);
1859         if (r)
1860             dev_err(dev, "%s: Get FREQ fails.\n", __func__);
1861         else
1862             dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50);
1863 
1864         r = core->read(core, WL1273_MOST_MODE_SET, &val);
1865         if (r)
1866             dev_err(dev, "%s: Get MOST_MODE fails.\n",
1867                 __func__);
1868         else if (val == 0)
1869             dev_info(dev, "MOST_MODE: Stereo according to blend\n");
1870         else if (val == 1)
1871             dev_info(dev, "MOST_MODE: Force mono output\n");
1872         else
1873             dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val);
1874 
1875         r = core->read(core, WL1273_MOST_BLEND_SET, &val);
1876         if (r)
1877             dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__);
1878         else if (val == 0)
1879             dev_info(dev,
1880                  "MOST_BLEND: Switched blend & hysteresis.\n");
1881         else if (val == 1)
1882             dev_info(dev, "MOST_BLEND: Soft blend.\n");
1883         else
1884             dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val);
1885 
1886         r = core->read(core, WL1273_STEREO_GET, &val);
1887         if (r)
1888             dev_err(dev, "%s: Get STEREO fails.\n", __func__);
1889         else if (val == 0)
1890             dev_info(dev, "STEREO: Not detected\n");
1891         else if (val == 1)
1892             dev_info(dev, "STEREO: Detected\n");
1893         else
1894             dev_info(dev, "STEREO: Unexpected value: %d\n", val);
1895 
1896         r = core->read(core, WL1273_RSSI_LVL_GET, &val);
1897         if (r)
1898             dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__);
1899         else
1900             dev_info(dev, "RX signal strength: %d\n", (s16) val);
1901 
1902         r = core->read(core, WL1273_POWER_SET, &val);
1903         if (r)
1904             dev_err(dev, "%s: Get POWER fails.\n", __func__);
1905         else
1906             dev_info(dev, "POWER: 0x%04x\n", val);
1907 
1908         r = core->read(core, WL1273_INT_MASK_SET, &val);
1909         if (r)
1910             dev_err(dev, "%s: Get INT_MASK fails.\n", __func__);
1911         else
1912             dev_info(dev, "INT_MASK: 0x%04x\n", val);
1913 
1914         r = core->read(core, WL1273_RDS_SYNC_GET, &val);
1915         if (r)
1916             dev_err(dev, "%s: Get RDS_SYNC fails.\n",
1917                 __func__);
1918         else if (val == 0)
1919             dev_info(dev, "RDS_SYNC: Not synchronized\n");
1920 
1921         else if (val == 1)
1922             dev_info(dev, "RDS_SYNC: Synchronized\n");
1923         else
1924             dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val);
1925 
1926         r = core->read(core, WL1273_I2S_MODE_CONFIG_SET, &val);
1927         if (r)
1928             dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n",
1929                 __func__);
1930         else
1931             dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val);
1932 
1933         r = core->read(core, WL1273_VOLUME_SET, &val);
1934         if (r)
1935             dev_err(dev, "%s: Get VOLUME fails.\n", __func__);
1936         else
1937             dev_info(dev, "VOLUME: 0x%04x\n", val);
1938     }
1939 
1940     return 0;
1941 }
1942 
1943 static void wl1273_vdev_release(struct video_device *dev)
1944 {
1945 }
1946 
1947 static const struct v4l2_ctrl_ops wl1273_ctrl_ops = {
1948     .s_ctrl = wl1273_fm_vidioc_s_ctrl,
1949     .g_volatile_ctrl = wl1273_fm_g_volatile_ctrl,
1950 };
1951 
1952 static const struct v4l2_ioctl_ops wl1273_ioctl_ops = {
1953     .vidioc_querycap    = wl1273_fm_vidioc_querycap,
1954     .vidioc_g_input     = wl1273_fm_vidioc_g_input,
1955     .vidioc_s_input     = wl1273_fm_vidioc_s_input,
1956     .vidioc_g_audio     = wl1273_fm_vidioc_g_audio,
1957     .vidioc_s_audio     = wl1273_fm_vidioc_s_audio,
1958     .vidioc_g_tuner     = wl1273_fm_vidioc_g_tuner,
1959     .vidioc_s_tuner     = wl1273_fm_vidioc_s_tuner,
1960     .vidioc_g_frequency = wl1273_fm_vidioc_g_frequency,
1961     .vidioc_s_frequency = wl1273_fm_vidioc_s_frequency,
1962     .vidioc_s_hw_freq_seek  = wl1273_fm_vidioc_s_hw_freq_seek,
1963     .vidioc_g_modulator = wl1273_fm_vidioc_g_modulator,
1964     .vidioc_s_modulator = wl1273_fm_vidioc_s_modulator,
1965     .vidioc_log_status      = wl1273_fm_vidioc_log_status,
1966 };
1967 
1968 static const struct video_device wl1273_viddev_template = {
1969     .fops           = &wl1273_fops,
1970     .ioctl_ops      = &wl1273_ioctl_ops,
1971     .name           = WL1273_FM_DRIVER_NAME,
1972     .release        = wl1273_vdev_release,
1973     .vfl_dir        = VFL_DIR_TX,
1974     .device_caps        = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_TUNER |
1975                   V4L2_CAP_RADIO | V4L2_CAP_AUDIO |
1976                   V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR |
1977                   V4L2_CAP_RDS_OUTPUT,
1978 };
1979 
1980 static int wl1273_fm_radio_remove(struct platform_device *pdev)
1981 {
1982     struct wl1273_device *radio = platform_get_drvdata(pdev);
1983     struct wl1273_core *core = radio->core;
1984 
1985     dev_info(&pdev->dev, "%s.\n", __func__);
1986 
1987     free_irq(core->client->irq, radio);
1988     core->pdata->free_resources();
1989 
1990     v4l2_ctrl_handler_free(&radio->ctrl_handler);
1991     video_unregister_device(&radio->videodev);
1992     v4l2_device_unregister(&radio->v4l2dev);
1993 
1994     return 0;
1995 }
1996 
1997 static int wl1273_fm_radio_probe(struct platform_device *pdev)
1998 {
1999     struct wl1273_core **core = pdev->dev.platform_data;
2000     struct wl1273_device *radio;
2001     struct v4l2_ctrl *ctrl;
2002     int r = 0;
2003 
2004     pr_debug("%s\n", __func__);
2005 
2006     if (!core) {
2007         dev_err(&pdev->dev, "No platform data.\n");
2008         r = -EINVAL;
2009         goto pdata_err;
2010     }
2011 
2012     radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
2013     if (!radio) {
2014         r = -ENOMEM;
2015         goto pdata_err;
2016     }
2017 
2018     /* RDS buffer allocation */
2019     radio->buf_size = rds_buf * RDS_BLOCK_SIZE;
2020     radio->buffer = devm_kzalloc(&pdev->dev, radio->buf_size, GFP_KERNEL);
2021     if (!radio->buffer) {
2022         pr_err("Cannot allocate memory for RDS buffer.\n");
2023         r = -ENOMEM;
2024         goto pdata_err;
2025     }
2026 
2027     radio->core = *core;
2028     radio->irq_flags = WL1273_IRQ_MASK;
2029     radio->dev = &radio->core->client->dev;
2030     radio->rds_on = false;
2031     radio->core->mode = WL1273_MODE_OFF;
2032     radio->tx_power = 118;
2033     radio->core->audio_mode = WL1273_AUDIO_ANALOG;
2034     radio->band = WL1273_BAND_OTHER;
2035     radio->core->i2s_mode = WL1273_I2S_DEF_MODE;
2036     radio->core->channel_number = 2;
2037     radio->core->volume = WL1273_DEFAULT_VOLUME;
2038     radio->rx_frequency = WL1273_BAND_OTHER_LOW;
2039     radio->tx_frequency = WL1273_BAND_OTHER_HIGH;
2040     radio->rangelow = WL1273_BAND_OTHER_LOW;
2041     radio->rangehigh = WL1273_BAND_OTHER_HIGH;
2042     radio->stereo = true;
2043     radio->bus_type = "I2C";
2044 
2045     if (radio->core->pdata->request_resources) {
2046         r = radio->core->pdata->request_resources(radio->core->client);
2047         if (r) {
2048             dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2049                 ": Cannot get platform data\n");
2050             goto pdata_err;
2051         }
2052 
2053         dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq);
2054 
2055         r = request_threaded_irq(radio->core->client->irq, NULL,
2056                      wl1273_fm_irq_thread_handler,
2057                      IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
2058                      "wl1273-fm", radio);
2059         if (r < 0) {
2060             dev_err(radio->dev, WL1273_FM_DRIVER_NAME
2061                 ": Unable to register IRQ handler: %d\n", r);
2062             goto err_request_irq;
2063         }
2064     } else {
2065         dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ not configured");
2066         r = -EINVAL;
2067         goto pdata_err;
2068     }
2069 
2070     init_completion(&radio->busy);
2071     init_waitqueue_head(&radio->read_queue);
2072 
2073     radio->write_buf = devm_kzalloc(&pdev->dev, 256, GFP_KERNEL);
2074     if (!radio->write_buf) {
2075         r = -ENOMEM;
2076         goto write_buf_err;
2077     }
2078 
2079     radio->dev = &pdev->dev;
2080     radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
2081     radio->rds_users = 0;
2082 
2083     r = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
2084     if (r) {
2085         dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
2086         goto write_buf_err;
2087     }
2088 
2089     /* V4L2 configuration */
2090     radio->videodev = wl1273_viddev_template;
2091 
2092     radio->videodev.v4l2_dev = &radio->v4l2dev;
2093 
2094     v4l2_ctrl_handler_init(&radio->ctrl_handler, 6);
2095 
2096     /* add in ascending ID order */
2097     v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2098               V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1,
2099               WL1273_DEFAULT_VOLUME);
2100 
2101     v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2102               V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
2103 
2104     v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops,
2105                    V4L2_CID_TUNE_PREEMPHASIS,
2106                    V4L2_PREEMPHASIS_75_uS, 0x03,
2107                    V4L2_PREEMPHASIS_50_uS);
2108 
2109     v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2110               V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118);
2111 
2112     ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops,
2113                  V4L2_CID_TUNE_ANTENNA_CAPACITOR,
2114                  0, 255, 1, 255);
2115     if (ctrl)
2116         ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
2117 
2118     if (radio->ctrl_handler.error) {
2119         r = radio->ctrl_handler.error;
2120         dev_err(&pdev->dev, "Ctrl handler error: %d\n", r);
2121         goto handler_init_err;
2122     }
2123 
2124     video_set_drvdata(&radio->videodev, radio);
2125     platform_set_drvdata(pdev, radio);
2126 
2127     /* register video device */
2128     r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr);
2129     if (r) {
2130         dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME
2131             ": Could not register video device\n");
2132         goto handler_init_err;
2133     }
2134 
2135     return 0;
2136 
2137 handler_init_err:
2138     v4l2_ctrl_handler_free(&radio->ctrl_handler);
2139     v4l2_device_unregister(&radio->v4l2dev);
2140 write_buf_err:
2141     free_irq(radio->core->client->irq, radio);
2142 err_request_irq:
2143     radio->core->pdata->free_resources();
2144 pdata_err:
2145     return r;
2146 }
2147 
2148 static struct platform_driver wl1273_fm_radio_driver = {
2149     .probe      = wl1273_fm_radio_probe,
2150     .remove     = wl1273_fm_radio_remove,
2151     .driver     = {
2152         .name   = "wl1273_fm_radio",
2153     },
2154 };
2155 
2156 module_platform_driver(wl1273_fm_radio_driver);
2157 
2158 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
2159 MODULE_DESCRIPTION(DRIVER_DESC);
2160 MODULE_LICENSE("GPL");
2161 MODULE_ALIAS("platform:wl1273_fm_radio");