Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * drivers/media/radio/si4713-i2c.c
0004  *
0005  * Silicon Labs Si4713 FM Radio Transmitter I2C commands.
0006  *
0007  * Copyright (c) 2009 Nokia Corporation
0008  * Contact: Eduardo Valentin <eduardo.valentin@nokia.com>
0009  */
0010 
0011 #include <linux/completion.h>
0012 #include <linux/delay.h>
0013 #include <linux/err.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/i2c.h>
0016 #include <linux/slab.h>
0017 #include <linux/gpio.h>
0018 #include <linux/module.h>
0019 #include <media/v4l2-device.h>
0020 #include <media/v4l2-ioctl.h>
0021 #include <media/v4l2-common.h>
0022 
0023 #include "si4713.h"
0024 
0025 /* module parameters */
0026 static int debug;
0027 module_param(debug, int, S_IRUGO | S_IWUSR);
0028 MODULE_PARM_DESC(debug, "Debug level (0 - 2)");
0029 
0030 MODULE_LICENSE("GPL");
0031 MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
0032 MODULE_DESCRIPTION("I2C driver for Si4713 FM Radio Transmitter");
0033 MODULE_VERSION("0.0.1");
0034 
0035 #define DEFAULT_RDS_PI          0x00
0036 #define DEFAULT_RDS_PTY         0x00
0037 #define DEFAULT_RDS_DEVIATION       0x00C8
0038 #define DEFAULT_RDS_PS_REPEAT_COUNT 0x0003
0039 #define DEFAULT_LIMITER_RTIME       0x1392
0040 #define DEFAULT_LIMITER_DEV     0x102CA
0041 #define DEFAULT_PILOT_FREQUENCY     0x4A38
0042 #define DEFAULT_PILOT_DEVIATION     0x1A5E
0043 #define DEFAULT_ACOMP_ATIME     0x0000
0044 #define DEFAULT_ACOMP_RTIME     0xF4240L
0045 #define DEFAULT_ACOMP_GAIN      0x0F
0046 #define DEFAULT_ACOMP_THRESHOLD     (-0x28)
0047 #define DEFAULT_MUTE            0x01
0048 #define DEFAULT_POWER_LEVEL     88
0049 #define DEFAULT_FREQUENCY       8800
0050 #define DEFAULT_PREEMPHASIS     FMPE_EU
0051 #define DEFAULT_TUNE_RNL        0xFF
0052 
0053 #define to_si4713_device(sd)    container_of(sd, struct si4713_device, sd)
0054 
0055 /* frequency domain transformation (using times 10 to avoid floats) */
0056 #define FREQDEV_UNIT    100000
0057 #define FREQV4L2_MULTI  625
0058 #define si4713_to_v4l2(f)   ((f * FREQDEV_UNIT) / FREQV4L2_MULTI)
0059 #define v4l2_to_si4713(f)   ((f * FREQV4L2_MULTI) / FREQDEV_UNIT)
0060 #define FREQ_RANGE_LOW          7600
0061 #define FREQ_RANGE_HIGH         10800
0062 
0063 #define MAX_ARGS 7
0064 
0065 #define RDS_BLOCK           8
0066 #define RDS_BLOCK_CLEAR         0x03
0067 #define RDS_BLOCK_LOAD          0x04
0068 #define RDS_RADIOTEXT_2A        0x20
0069 #define RDS_RADIOTEXT_BLK_SIZE      4
0070 #define RDS_RADIOTEXT_INDEX_MAX     0x0F
0071 #define RDS_CARRIAGE_RETURN     0x0D
0072 
0073 #define rds_ps_nblocks(len) ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
0074 
0075 #define get_status_bit(p, b, m) (((p) & (m)) >> (b))
0076 #define set_bits(p, v, b, m)    (((p) & ~(m)) | ((v) << (b)))
0077 
0078 #define ATTACK_TIME_UNIT    500
0079 
0080 #define POWER_OFF           0x00
0081 #define POWER_ON            0x01
0082 
0083 #define msb(x)                  ((u8)((u16) x >> 8))
0084 #define lsb(x)                  ((u8)((u16) x &  0x00FF))
0085 #define compose_u16(msb, lsb)   (((u16)msb << 8) | lsb)
0086 #define check_command_failed(status)    (!(status & SI4713_CTS) || \
0087                     (status & SI4713_ERR))
0088 /* mute definition */
0089 #define set_mute(p) (((p) & 1) | (((p) & 1) << 1))
0090 
0091 #ifdef DEBUG
0092 #define DBG_BUFFER(device, message, buffer, size)           \
0093     {                               \
0094         int i;                          \
0095         char str[(size)*5];                 \
0096         for (i = 0; i < size; i++)              \
0097             sprintf(str + i * 5, " 0x%02x", buffer[i]); \
0098         v4l2_dbg(2, debug, device, "%s:%s\n", message, str);    \
0099     }
0100 #else
0101 #define DBG_BUFFER(device, message, buffer, size)
0102 #endif
0103 
0104 /*
0105  * Values for limiter release time (sorted by second column)
0106  *  device  release
0107  *  value   time (us)
0108  */
0109 static long limiter_times[] = {
0110     2000,   250,
0111     1000,   500,
0112     510,    1000,
0113     255,    2000,
0114     170,    3000,
0115     127,    4020,
0116     102,    5010,
0117     85, 6020,
0118     73, 7010,
0119     64, 7990,
0120     57, 8970,
0121     51, 10030,
0122     25, 20470,
0123     17, 30110,
0124     13, 39380,
0125     10, 51190,
0126     8,  63690,
0127     7,  73140,
0128     6,  85330,
0129     5,  102390,
0130 };
0131 
0132 /*
0133  * Values for audio compression release time (sorted by second column)
0134  *  device  release
0135  *  value   time (us)
0136  */
0137 static unsigned long acomp_rtimes[] = {
0138     0,  100000,
0139     1,  200000,
0140     2,  350000,
0141     3,  525000,
0142     4,  1000000,
0143 };
0144 
0145 /*
0146  * Values for preemphasis (sorted by second column)
0147  *  device  preemphasis
0148  *  value   value (v4l2)
0149  */
0150 static unsigned long preemphasis_values[] = {
0151     FMPE_DISABLED,  V4L2_PREEMPHASIS_DISABLED,
0152     FMPE_EU,    V4L2_PREEMPHASIS_50_uS,
0153     FMPE_USA,   V4L2_PREEMPHASIS_75_uS,
0154 };
0155 
0156 static int usecs_to_dev(unsigned long usecs, unsigned long const array[],
0157             int size)
0158 {
0159     int i;
0160     int rval = -EINVAL;
0161 
0162     for (i = 0; i < size / 2; i++)
0163         if (array[(i * 2) + 1] >= usecs) {
0164             rval = array[i * 2];
0165             break;
0166         }
0167 
0168     return rval;
0169 }
0170 
0171 /* si4713_handler: IRQ handler, just complete work */
0172 static irqreturn_t si4713_handler(int irq, void *dev)
0173 {
0174     struct si4713_device *sdev = dev;
0175 
0176     v4l2_dbg(2, debug, &sdev->sd,
0177             "%s: sending signal to completion work.\n", __func__);
0178     complete(&sdev->work);
0179 
0180     return IRQ_HANDLED;
0181 }
0182 
0183 /*
0184  * si4713_send_command - sends a command to si4713 and waits its response
0185  * @sdev: si4713_device structure for the device we are communicating
0186  * @command: command id
0187  * @args: command arguments we are sending (up to 7)
0188  * @argn: actual size of @args
0189  * @response: buffer to place the expected response from the device (up to 15)
0190  * @respn: actual size of @response
0191  * @usecs: amount of time to wait before reading the response (in usecs)
0192  */
0193 static int si4713_send_command(struct si4713_device *sdev, const u8 command,
0194                 const u8 args[], const int argn,
0195                 u8 response[], const int respn, const int usecs)
0196 {
0197     struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
0198     unsigned long until_jiffies;
0199     u8 data1[MAX_ARGS + 1];
0200     int err;
0201 
0202     if (!client->adapter)
0203         return -ENODEV;
0204 
0205     /* First send the command and its arguments */
0206     data1[0] = command;
0207     memcpy(data1 + 1, args, argn);
0208     DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1);
0209 
0210     err = i2c_master_send(client, data1, argn + 1);
0211     if (err != argn + 1) {
0212         v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n",
0213             command);
0214         return err < 0 ? err : -EIO;
0215     }
0216 
0217     until_jiffies = jiffies + usecs_to_jiffies(usecs) + 1;
0218 
0219     /* Wait response from interrupt */
0220     if (client->irq) {
0221         if (!wait_for_completion_timeout(&sdev->work,
0222                 usecs_to_jiffies(usecs) + 1))
0223             v4l2_warn(&sdev->sd,
0224                 "(%s) Device took too much time to answer.\n",
0225                 __func__);
0226     }
0227 
0228     do {
0229         err = i2c_master_recv(client, response, respn);
0230         if (err != respn) {
0231             v4l2_err(&sdev->sd,
0232                 "Error %d while reading response for command 0x%02x\n",
0233                 err, command);
0234             return err < 0 ? err : -EIO;
0235         }
0236 
0237         DBG_BUFFER(&sdev->sd, "Response", response, respn);
0238         if (!check_command_failed(response[0]))
0239             return 0;
0240 
0241         if (client->irq)
0242             return -EBUSY;
0243         if (usecs <= 1000)
0244             usleep_range(usecs, 1000);
0245         else
0246             usleep_range(1000, 2000);
0247     } while (time_is_after_jiffies(until_jiffies));
0248 
0249     return -EBUSY;
0250 }
0251 
0252 /*
0253  * si4713_read_property - reads a si4713 property
0254  * @sdev: si4713_device structure for the device we are communicating
0255  * @prop: property identification number
0256  * @pv: property value to be returned on success
0257  */
0258 static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv)
0259 {
0260     int err;
0261     u8 val[SI4713_GET_PROP_NRESP];
0262     /*
0263      *  .First byte = 0
0264      *  .Second byte = property's MSB
0265      *  .Third byte = property's LSB
0266      */
0267     const u8 args[SI4713_GET_PROP_NARGS] = {
0268         0x00,
0269         msb(prop),
0270         lsb(prop),
0271     };
0272 
0273     err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY,
0274                   args, ARRAY_SIZE(args), val,
0275                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
0276 
0277     if (err < 0)
0278         return err;
0279 
0280     *pv = compose_u16(val[2], val[3]);
0281 
0282     v4l2_dbg(1, debug, &sdev->sd,
0283             "%s: property=0x%02x value=0x%02x status=0x%02x\n",
0284             __func__, prop, *pv, val[0]);
0285 
0286     return err;
0287 }
0288 
0289 /*
0290  * si4713_write_property - modifies a si4713 property
0291  * @sdev: si4713_device structure for the device we are communicating
0292  * @prop: property identification number
0293  * @val: new value for that property
0294  */
0295 static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val)
0296 {
0297     int rval;
0298     u8 resp[SI4713_SET_PROP_NRESP];
0299     /*
0300      *  .First byte = 0
0301      *  .Second byte = property's MSB
0302      *  .Third byte = property's LSB
0303      *  .Fourth byte = value's MSB
0304      *  .Fifth byte = value's LSB
0305      */
0306     const u8 args[SI4713_SET_PROP_NARGS] = {
0307         0x00,
0308         msb(prop),
0309         lsb(prop),
0310         msb(val),
0311         lsb(val),
0312     };
0313 
0314     rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY,
0315                     args, ARRAY_SIZE(args),
0316                     resp, ARRAY_SIZE(resp),
0317                     DEFAULT_TIMEOUT);
0318 
0319     if (rval < 0)
0320         return rval;
0321 
0322     v4l2_dbg(1, debug, &sdev->sd,
0323             "%s: property=0x%02x value=0x%02x status=0x%02x\n",
0324             __func__, prop, val, resp[0]);
0325 
0326     /*
0327      * As there is no command response for SET_PROPERTY,
0328      * wait Tcomp time to finish before proceed, in order
0329      * to have property properly set.
0330      */
0331     msleep(TIMEOUT_SET_PROPERTY);
0332 
0333     return rval;
0334 }
0335 
0336 /*
0337  * si4713_powerup - Powers the device up
0338  * @sdev: si4713_device structure for the device we are communicating
0339  */
0340 static int si4713_powerup(struct si4713_device *sdev)
0341 {
0342     struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
0343     int err;
0344     u8 resp[SI4713_PWUP_NRESP];
0345     /*
0346      *  .First byte = Enabled interrupts and boot function
0347      *  .Second byte = Input operation mode
0348      */
0349     u8 args[SI4713_PWUP_NARGS] = {
0350         SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX,
0351         SI4713_PWUP_OPMOD_ANALOG,
0352     };
0353 
0354     if (sdev->power_state)
0355         return 0;
0356 
0357     if (sdev->vdd) {
0358         err = regulator_enable(sdev->vdd);
0359         if (err) {
0360             v4l2_err(&sdev->sd, "Failed to enable vdd: %d\n", err);
0361             return err;
0362         }
0363     }
0364 
0365     if (sdev->vio) {
0366         err = regulator_enable(sdev->vio);
0367         if (err) {
0368             v4l2_err(&sdev->sd, "Failed to enable vio: %d\n", err);
0369             return err;
0370         }
0371     }
0372 
0373     if (sdev->gpio_reset) {
0374         udelay(50);
0375         gpiod_set_value(sdev->gpio_reset, 1);
0376     }
0377 
0378     if (client->irq)
0379         args[0] |= SI4713_PWUP_CTSIEN;
0380 
0381     err = si4713_send_command(sdev, SI4713_CMD_POWER_UP,
0382                     args, ARRAY_SIZE(args),
0383                     resp, ARRAY_SIZE(resp),
0384                     TIMEOUT_POWER_UP);
0385 
0386     if (!err) {
0387         v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n",
0388                 resp[0]);
0389         v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n");
0390         sdev->power_state = POWER_ON;
0391 
0392         if (client->irq)
0393             err = si4713_write_property(sdev, SI4713_GPO_IEN,
0394                         SI4713_STC_INT | SI4713_CTS);
0395         return err;
0396     }
0397     gpiod_set_value(sdev->gpio_reset, 0);
0398 
0399 
0400     if (sdev->vdd) {
0401         err = regulator_disable(sdev->vdd);
0402         if (err)
0403             v4l2_err(&sdev->sd, "Failed to disable vdd: %d\n", err);
0404     }
0405 
0406     if (sdev->vio) {
0407         err = regulator_disable(sdev->vio);
0408         if (err)
0409             v4l2_err(&sdev->sd, "Failed to disable vio: %d\n", err);
0410     }
0411 
0412     return err;
0413 }
0414 
0415 /*
0416  * si4713_powerdown - Powers the device down
0417  * @sdev: si4713_device structure for the device we are communicating
0418  */
0419 static int si4713_powerdown(struct si4713_device *sdev)
0420 {
0421     int err;
0422     u8 resp[SI4713_PWDN_NRESP];
0423 
0424     if (!sdev->power_state)
0425         return 0;
0426 
0427     err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN,
0428                     NULL, 0,
0429                     resp, ARRAY_SIZE(resp),
0430                     DEFAULT_TIMEOUT);
0431 
0432     if (!err) {
0433         v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n",
0434                 resp[0]);
0435         v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n");
0436         if (sdev->gpio_reset)
0437             gpiod_set_value(sdev->gpio_reset, 0);
0438 
0439         if (sdev->vdd) {
0440             err = regulator_disable(sdev->vdd);
0441             if (err) {
0442                 v4l2_err(&sdev->sd,
0443                     "Failed to disable vdd: %d\n", err);
0444             }
0445         }
0446 
0447         if (sdev->vio) {
0448             err = regulator_disable(sdev->vio);
0449             if (err) {
0450                 v4l2_err(&sdev->sd,
0451                     "Failed to disable vio: %d\n", err);
0452             }
0453         }
0454         sdev->power_state = POWER_OFF;
0455     }
0456 
0457     return err;
0458 }
0459 
0460 /*
0461  * si4713_checkrev - Checks if we are treating a device with the correct rev.
0462  * @sdev: si4713_device structure for the device we are communicating
0463  */
0464 static int si4713_checkrev(struct si4713_device *sdev)
0465 {
0466     struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
0467     int rval;
0468     u8 resp[SI4713_GETREV_NRESP];
0469 
0470     rval = si4713_send_command(sdev, SI4713_CMD_GET_REV,
0471                     NULL, 0,
0472                     resp, ARRAY_SIZE(resp),
0473                     DEFAULT_TIMEOUT);
0474 
0475     if (rval < 0)
0476         return rval;
0477 
0478     if (resp[1] == SI4713_PRODUCT_NUMBER) {
0479         v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n",
0480                 client->addr << 1, client->adapter->name);
0481     } else {
0482         v4l2_err(&sdev->sd, "Invalid product number 0x%X\n", resp[1]);
0483         rval = -EINVAL;
0484     }
0485     return rval;
0486 }
0487 
0488 /*
0489  * si4713_wait_stc - Waits STC interrupt and clears status bits. Useful
0490  *           for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
0491  * @sdev: si4713_device structure for the device we are communicating
0492  * @usecs: timeout to wait for STC interrupt signal
0493  */
0494 static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
0495 {
0496     struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
0497     u8 resp[SI4713_GET_STATUS_NRESP];
0498     unsigned long start_jiffies = jiffies;
0499     int err;
0500 
0501     if (client->irq &&
0502         !wait_for_completion_timeout(&sdev->work, usecs_to_jiffies(usecs) + 1))
0503         v4l2_warn(&sdev->sd,
0504             "(%s) Device took too much time to answer.\n", __func__);
0505 
0506     for (;;) {
0507         /* Clear status bits */
0508         err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS,
0509                 NULL, 0,
0510                 resp, ARRAY_SIZE(resp),
0511                 DEFAULT_TIMEOUT);
0512         /* The USB device returns errors when it waits for the
0513          * STC bit to be set. Hence polling */
0514         if (err >= 0) {
0515             v4l2_dbg(1, debug, &sdev->sd,
0516                 "%s: status bits: 0x%02x\n", __func__, resp[0]);
0517 
0518             if (resp[0] & SI4713_STC_INT)
0519                 return 0;
0520         }
0521         if (jiffies_to_usecs(jiffies - start_jiffies) > usecs)
0522             return err < 0 ? err : -EIO;
0523         /* We sleep here for 3-4 ms in order to avoid flooding the device
0524          * with USB requests. The si4713 USB driver was developed
0525          * by reverse engineering the Windows USB driver. The windows
0526          * driver also has a ~2.5 ms delay between responses. */
0527         usleep_range(3000, 4000);
0528     }
0529 }
0530 
0531 /*
0532  * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
0533  *          frequency between 76 and 108 MHz in 10 kHz units and
0534  *          steps of 50 kHz.
0535  * @sdev: si4713_device structure for the device we are communicating
0536  * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
0537  */
0538 static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
0539 {
0540     int err;
0541     u8 val[SI4713_TXFREQ_NRESP];
0542     /*
0543      *  .First byte = 0
0544      *  .Second byte = frequency's MSB
0545      *  .Third byte = frequency's LSB
0546      */
0547     const u8 args[SI4713_TXFREQ_NARGS] = {
0548         0x00,
0549         msb(frequency),
0550         lsb(frequency),
0551     };
0552 
0553     err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ,
0554                   args, ARRAY_SIZE(args), val,
0555                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
0556 
0557     if (err < 0)
0558         return err;
0559 
0560     v4l2_dbg(1, debug, &sdev->sd,
0561             "%s: frequency=0x%02x status=0x%02x\n", __func__,
0562             frequency, val[0]);
0563 
0564     err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
0565     if (err < 0)
0566         return err;
0567 
0568     return compose_u16(args[1], args[2]);
0569 }
0570 
0571 /*
0572  * si4713_tx_tune_power - Sets the RF voltage level between 88 and 120 dBuV in
0573  *          1 dB units. A value of 0x00 indicates off. The command
0574  *          also sets the antenna tuning capacitance. A value of 0
0575  *          indicates autotuning, and a value of 1 - 191 indicates
0576  *          a manual override, which results in a tuning
0577  *          capacitance of 0.25 pF x @antcap.
0578  * @sdev: si4713_device structure for the device we are communicating
0579  * @power: tuning power (88 - 120 dBuV, unit/step 1 dB)
0580  * @antcap: value of antenna tuning capacitor (0 - 191)
0581  */
0582 static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power,
0583                 u8 antcap)
0584 {
0585     int err;
0586     u8 val[SI4713_TXPWR_NRESP];
0587     /*
0588      *  .First byte = 0
0589      *  .Second byte = 0
0590      *  .Third byte = power
0591      *  .Fourth byte = antcap
0592      */
0593     u8 args[SI4713_TXPWR_NARGS] = {
0594         0x00,
0595         0x00,
0596         power,
0597         antcap,
0598     };
0599 
0600     /* Map power values 1-87 to MIN_POWER (88) */
0601     if (power > 0 && power < SI4713_MIN_POWER)
0602         args[2] = power = SI4713_MIN_POWER;
0603 
0604     err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER,
0605                   args, ARRAY_SIZE(args), val,
0606                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
0607 
0608     if (err < 0)
0609         return err;
0610 
0611     v4l2_dbg(1, debug, &sdev->sd,
0612             "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
0613             __func__, power, antcap, val[0]);
0614 
0615     return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER);
0616 }
0617 
0618 /*
0619  * si4713_tx_tune_measure - Enters receive mode and measures the received noise
0620  *          level in units of dBuV on the selected frequency.
0621  *          The Frequency must be between 76 and 108 MHz in 10 kHz
0622  *          units and steps of 50 kHz. The command also sets the
0623  *          antenna tuning capacitance. A value of 0 means
0624  *          autotuning, and a value of 1 to 191 indicates manual
0625  *          override.
0626  * @sdev: si4713_device structure for the device we are communicating
0627  * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
0628  * @antcap: value of antenna tuning capacitor (0 - 191)
0629  */
0630 static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency,
0631                     u8 antcap)
0632 {
0633     int err;
0634     u8 val[SI4713_TXMEA_NRESP];
0635     /*
0636      *  .First byte = 0
0637      *  .Second byte = frequency's MSB
0638      *  .Third byte = frequency's LSB
0639      *  .Fourth byte = antcap
0640      */
0641     const u8 args[SI4713_TXMEA_NARGS] = {
0642         0x00,
0643         msb(frequency),
0644         lsb(frequency),
0645         antcap,
0646     };
0647 
0648     sdev->tune_rnl = DEFAULT_TUNE_RNL;
0649 
0650     if (antcap > SI4713_MAX_ANTCAP)
0651         return -EDOM;
0652 
0653     err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE,
0654                   args, ARRAY_SIZE(args), val,
0655                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
0656 
0657     if (err < 0)
0658         return err;
0659 
0660     v4l2_dbg(1, debug, &sdev->sd,
0661             "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
0662             __func__, frequency, antcap, val[0]);
0663 
0664     return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
0665 }
0666 
0667 /*
0668  * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
0669  *          tx_tune_power commands. This command return the current
0670  *          frequency, output voltage in dBuV, the antenna tunning
0671  *          capacitance value and the received noise level. The
0672  *          command also clears the stcint interrupt bit when the
0673  *          first bit of its arguments is high.
0674  * @sdev: si4713_device structure for the device we are communicating
0675  * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
0676  * @frequency: returned frequency
0677  * @power: returned power
0678  * @antcap: returned antenna capacitance
0679  * @noise: returned noise level
0680  */
0681 static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack,
0682                     u16 *frequency, u8 *power,
0683                     u8 *antcap, u8 *noise)
0684 {
0685     int err;
0686     u8 val[SI4713_TXSTATUS_NRESP];
0687     /*
0688      *  .First byte = intack bit
0689      */
0690     const u8 args[SI4713_TXSTATUS_NARGS] = {
0691         intack & SI4713_INTACK_MASK,
0692     };
0693 
0694     err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS,
0695                   args, ARRAY_SIZE(args), val,
0696                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
0697 
0698     if (!err) {
0699         v4l2_dbg(1, debug, &sdev->sd,
0700             "%s: status=0x%02x\n", __func__, val[0]);
0701         *frequency = compose_u16(val[2], val[3]);
0702         sdev->frequency = *frequency;
0703         *power = val[5];
0704         *antcap = val[6];
0705         *noise = val[7];
0706         v4l2_dbg(1, debug, &sdev->sd,
0707              "%s: response: %d x 10 kHz (power %d, antcap %d, rnl %d)\n",
0708              __func__, *frequency, *power, *antcap, *noise);
0709     }
0710 
0711     return err;
0712 }
0713 
0714 /*
0715  * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
0716  * @sdev: si4713_device structure for the device we are communicating
0717  * @mode: the buffer operation mode.
0718  * @rdsb: RDS Block B
0719  * @rdsc: RDS Block C
0720  * @rdsd: RDS Block D
0721  * @cbleft: returns the number of available circular buffer blocks minus the
0722  *          number of used circular buffer blocks.
0723  */
0724 static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb,
0725                 u16 rdsc, u16 rdsd, s8 *cbleft)
0726 {
0727     int err;
0728     u8 val[SI4713_RDSBUFF_NRESP];
0729 
0730     const u8 args[SI4713_RDSBUFF_NARGS] = {
0731         mode & SI4713_RDSBUFF_MODE_MASK,
0732         msb(rdsb),
0733         lsb(rdsb),
0734         msb(rdsc),
0735         lsb(rdsc),
0736         msb(rdsd),
0737         lsb(rdsd),
0738     };
0739 
0740     err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF,
0741                   args, ARRAY_SIZE(args), val,
0742                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
0743 
0744     if (!err) {
0745         v4l2_dbg(1, debug, &sdev->sd,
0746             "%s: status=0x%02x\n", __func__, val[0]);
0747         *cbleft = (s8)val[2] - val[3];
0748         v4l2_dbg(1, debug, &sdev->sd,
0749              "%s: response: interrupts 0x%02x cb avail: %d cb used %d fifo avail %d fifo used %d\n",
0750              __func__, val[1], val[2], val[3], val[4], val[5]);
0751     }
0752 
0753     return err;
0754 }
0755 
0756 /*
0757  * si4713_tx_rds_ps - Loads the program service buffer.
0758  * @sdev: si4713_device structure for the device we are communicating
0759  * @psid: program service id to be loaded.
0760  * @pschar: assumed 4 size char array to be loaded into the program service
0761  */
0762 static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
0763                 unsigned char *pschar)
0764 {
0765     int err;
0766     u8 val[SI4713_RDSPS_NRESP];
0767 
0768     const u8 args[SI4713_RDSPS_NARGS] = {
0769         psid & SI4713_RDSPS_PSID_MASK,
0770         pschar[0],
0771         pschar[1],
0772         pschar[2],
0773         pschar[3],
0774     };
0775 
0776     err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
0777                   args, ARRAY_SIZE(args), val,
0778                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
0779 
0780     if (err < 0)
0781         return err;
0782 
0783     v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
0784 
0785     return err;
0786 }
0787 
0788 static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
0789 {
0790     if (value)
0791         return si4713_powerup(sdev);
0792     return si4713_powerdown(sdev);
0793 }
0794 
0795 static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
0796 {
0797     int rval = 0;
0798 
0799     mute = set_mute(mute);
0800 
0801     if (sdev->power_state)
0802         rval = si4713_write_property(sdev,
0803                 SI4713_TX_LINE_INPUT_MUTE, mute);
0804 
0805     return rval;
0806 }
0807 
0808 static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
0809 {
0810     int rval = 0, i;
0811     u8 len = 0;
0812 
0813     /* We want to clear the whole thing */
0814     if (!strlen(ps_name))
0815         memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
0816 
0817     if (sdev->power_state) {
0818         /* Write the new ps name and clear the padding */
0819         for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
0820             rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
0821                         ps_name + i);
0822             if (rval < 0)
0823                 return rval;
0824         }
0825 
0826         /* Setup the size to be sent */
0827         if (strlen(ps_name))
0828             len = strlen(ps_name) - 1;
0829         else
0830             len = 1;
0831 
0832         rval = si4713_write_property(sdev,
0833                 SI4713_TX_RDS_PS_MESSAGE_COUNT,
0834                 rds_ps_nblocks(len));
0835         if (rval < 0)
0836             return rval;
0837 
0838         rval = si4713_write_property(sdev,
0839                 SI4713_TX_RDS_PS_REPEAT_COUNT,
0840                 DEFAULT_RDS_PS_REPEAT_COUNT * 2);
0841         if (rval < 0)
0842             return rval;
0843     }
0844 
0845     return rval;
0846 }
0847 
0848 static int si4713_set_rds_radio_text(struct si4713_device *sdev, const char *rt)
0849 {
0850     static const char cr[RDS_RADIOTEXT_BLK_SIZE] = { RDS_CARRIAGE_RETURN, 0 };
0851     int rval = 0, i;
0852     u16 t_index = 0;
0853     u8 b_index = 0, cr_inserted = 0;
0854     s8 left;
0855 
0856     if (!sdev->power_state)
0857         return rval;
0858 
0859     rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
0860     if (rval < 0)
0861         return rval;
0862 
0863     if (!strlen(rt))
0864         return rval;
0865 
0866     do {
0867         /* RDS spec says that if the last block isn't used,
0868          * then apply a carriage return
0869          */
0870         if (t_index < (RDS_RADIOTEXT_INDEX_MAX * RDS_RADIOTEXT_BLK_SIZE)) {
0871             for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
0872                 if (!rt[t_index + i] ||
0873                     rt[t_index + i] == RDS_CARRIAGE_RETURN) {
0874                     rt = cr;
0875                     cr_inserted = 1;
0876                     break;
0877                 }
0878             }
0879         }
0880 
0881         rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD,
0882                 compose_u16(RDS_RADIOTEXT_2A, b_index++),
0883                 compose_u16(rt[t_index], rt[t_index + 1]),
0884                 compose_u16(rt[t_index + 2], rt[t_index + 3]),
0885                 &left);
0886         if (rval < 0)
0887             return rval;
0888 
0889         t_index += RDS_RADIOTEXT_BLK_SIZE;
0890 
0891         if (cr_inserted)
0892             break;
0893     } while (left > 0);
0894 
0895     return rval;
0896 }
0897 
0898 /*
0899  * si4713_update_tune_status - update properties from tx_tune_status
0900  * command. Must be called with sdev->mutex held.
0901  * @sdev: si4713_device structure for the device we are communicating
0902  */
0903 static int si4713_update_tune_status(struct si4713_device *sdev)
0904 {
0905     int rval;
0906     u16 f = 0;
0907     u8 p = 0, a = 0, n = 0;
0908 
0909     rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
0910 
0911     if (rval < 0)
0912         goto exit;
0913 
0914 /*  TODO: check that power_level and antenna_capacitor really are not
0915     changed by the hardware. If they are, then these controls should become
0916     volatiles.
0917     sdev->power_level = p;
0918     sdev->antenna_capacitor = a;*/
0919     sdev->tune_rnl = n;
0920 
0921 exit:
0922     return rval;
0923 }
0924 
0925 static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
0926         s32 *bit, s32 *mask, u16 *property, int *mul,
0927         unsigned long **table, int *size)
0928 {
0929     s32 rval = 0;
0930 
0931     switch (id) {
0932     /* FM_TX class controls */
0933     case V4L2_CID_RDS_TX_PI:
0934         *property = SI4713_TX_RDS_PI;
0935         *mul = 1;
0936         break;
0937     case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
0938         *property = SI4713_TX_ACOMP_THRESHOLD;
0939         *mul = 1;
0940         break;
0941     case V4L2_CID_AUDIO_COMPRESSION_GAIN:
0942         *property = SI4713_TX_ACOMP_GAIN;
0943         *mul = 1;
0944         break;
0945     case V4L2_CID_PILOT_TONE_FREQUENCY:
0946         *property = SI4713_TX_PILOT_FREQUENCY;
0947         *mul = 1;
0948         break;
0949     case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
0950         *property = SI4713_TX_ACOMP_ATTACK_TIME;
0951         *mul = ATTACK_TIME_UNIT;
0952         break;
0953     case V4L2_CID_PILOT_TONE_DEVIATION:
0954         *property = SI4713_TX_PILOT_DEVIATION;
0955         *mul = 10;
0956         break;
0957     case V4L2_CID_AUDIO_LIMITER_DEVIATION:
0958         *property = SI4713_TX_AUDIO_DEVIATION;
0959         *mul = 10;
0960         break;
0961     case V4L2_CID_RDS_TX_DEVIATION:
0962         *property = SI4713_TX_RDS_DEVIATION;
0963         *mul = 1;
0964         break;
0965 
0966     case V4L2_CID_RDS_TX_PTY:
0967         *property = SI4713_TX_RDS_PS_MISC;
0968         *bit = 5;
0969         *mask = 0x1F << 5;
0970         break;
0971     case V4L2_CID_RDS_TX_DYNAMIC_PTY:
0972         *property = SI4713_TX_RDS_PS_MISC;
0973         *bit = 15;
0974         *mask = 1 << 15;
0975         break;
0976     case V4L2_CID_RDS_TX_COMPRESSED:
0977         *property = SI4713_TX_RDS_PS_MISC;
0978         *bit = 14;
0979         *mask = 1 << 14;
0980         break;
0981     case V4L2_CID_RDS_TX_ARTIFICIAL_HEAD:
0982         *property = SI4713_TX_RDS_PS_MISC;
0983         *bit = 13;
0984         *mask = 1 << 13;
0985         break;
0986     case V4L2_CID_RDS_TX_MONO_STEREO:
0987         *property = SI4713_TX_RDS_PS_MISC;
0988         *bit = 12;
0989         *mask = 1 << 12;
0990         break;
0991     case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM:
0992         *property = SI4713_TX_RDS_PS_MISC;
0993         *bit = 10;
0994         *mask = 1 << 10;
0995         break;
0996     case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT:
0997         *property = SI4713_TX_RDS_PS_MISC;
0998         *bit = 4;
0999         *mask = 1 << 4;
1000         break;
1001     case V4L2_CID_RDS_TX_MUSIC_SPEECH:
1002         *property = SI4713_TX_RDS_PS_MISC;
1003         *bit = 3;
1004         *mask = 1 << 3;
1005         break;
1006     case V4L2_CID_AUDIO_LIMITER_ENABLED:
1007         *property = SI4713_TX_ACOMP_ENABLE;
1008         *bit = 1;
1009         *mask = 1 << 1;
1010         break;
1011     case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
1012         *property = SI4713_TX_ACOMP_ENABLE;
1013         *bit = 0;
1014         *mask = 1 << 0;
1015         break;
1016     case V4L2_CID_PILOT_TONE_ENABLED:
1017         *property = SI4713_TX_COMPONENT_ENABLE;
1018         *bit = 0;
1019         *mask = 1 << 0;
1020         break;
1021 
1022     case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
1023         *property = SI4713_TX_LIMITER_RELEASE_TIME;
1024         *table = limiter_times;
1025         *size = ARRAY_SIZE(limiter_times);
1026         break;
1027     case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
1028         *property = SI4713_TX_ACOMP_RELEASE_TIME;
1029         *table = acomp_rtimes;
1030         *size = ARRAY_SIZE(acomp_rtimes);
1031         break;
1032     case V4L2_CID_TUNE_PREEMPHASIS:
1033         *property = SI4713_TX_PREEMPHASIS;
1034         *table = preemphasis_values;
1035         *size = ARRAY_SIZE(preemphasis_values);
1036         break;
1037 
1038     default:
1039         rval = -EINVAL;
1040         break;
1041     }
1042 
1043     return rval;
1044 }
1045 
1046 static int si4713_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f);
1047 static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *);
1048 /*
1049  * si4713_setup - Sets the device up with current configuration.
1050  * @sdev: si4713_device structure for the device we are communicating
1051  */
1052 static int si4713_setup(struct si4713_device *sdev)
1053 {
1054     struct v4l2_frequency f;
1055     struct v4l2_modulator vm;
1056     int rval;
1057 
1058     /* Device procedure needs to set frequency first */
1059     f.tuner = 0;
1060     f.frequency = sdev->frequency ? sdev->frequency : DEFAULT_FREQUENCY;
1061     f.frequency = si4713_to_v4l2(f.frequency);
1062     rval = si4713_s_frequency(&sdev->sd, &f);
1063 
1064     vm.index = 0;
1065     if (sdev->stereo)
1066         vm.txsubchans = V4L2_TUNER_SUB_STEREO;
1067     else
1068         vm.txsubchans = V4L2_TUNER_SUB_MONO;
1069     if (sdev->rds_enabled)
1070         vm.txsubchans |= V4L2_TUNER_SUB_RDS;
1071     si4713_s_modulator(&sdev->sd, &vm);
1072 
1073     return rval;
1074 }
1075 
1076 /*
1077  * si4713_initialize - Sets the device up with default configuration.
1078  * @sdev: si4713_device structure for the device we are communicating
1079  */
1080 static int si4713_initialize(struct si4713_device *sdev)
1081 {
1082     int rval;
1083 
1084     rval = si4713_set_power_state(sdev, POWER_ON);
1085     if (rval < 0)
1086         return rval;
1087 
1088     rval = si4713_checkrev(sdev);
1089     if (rval < 0)
1090         return rval;
1091 
1092     rval = si4713_set_power_state(sdev, POWER_OFF);
1093     if (rval < 0)
1094         return rval;
1095 
1096     sdev->frequency = DEFAULT_FREQUENCY;
1097     sdev->stereo = 1;
1098     sdev->tune_rnl = DEFAULT_TUNE_RNL;
1099     return 0;
1100 }
1101 
1102 /* si4713_s_ctrl - set the value of a control */
1103 static int si4713_s_ctrl(struct v4l2_ctrl *ctrl)
1104 {
1105     struct si4713_device *sdev =
1106         container_of(ctrl->handler, struct si4713_device, ctrl_handler);
1107     u32 val = 0;
1108     s32 bit = 0, mask = 0;
1109     u16 property = 0;
1110     int mul = 0;
1111     unsigned long *table = NULL;
1112     int size = 0;
1113     bool force = false;
1114     int c;
1115     int ret = 0;
1116 
1117     if (ctrl->id != V4L2_CID_AUDIO_MUTE)
1118         return -EINVAL;
1119     if (ctrl->is_new) {
1120         if (ctrl->val) {
1121             ret = si4713_set_mute(sdev, ctrl->val);
1122             if (!ret)
1123                 ret = si4713_set_power_state(sdev, POWER_DOWN);
1124             return ret;
1125         }
1126         ret = si4713_set_power_state(sdev, POWER_UP);
1127         if (!ret)
1128             ret = si4713_set_mute(sdev, ctrl->val);
1129         if (!ret)
1130             ret = si4713_setup(sdev);
1131         if (ret)
1132             return ret;
1133         force = true;
1134     }
1135 
1136     if (!sdev->power_state)
1137         return 0;
1138 
1139     for (c = 1; !ret && c < ctrl->ncontrols; c++) {
1140         ctrl = ctrl->cluster[c];
1141 
1142         if (!force && !ctrl->is_new)
1143             continue;
1144 
1145         switch (ctrl->id) {
1146         case V4L2_CID_RDS_TX_PS_NAME:
1147             ret = si4713_set_rds_ps_name(sdev, ctrl->p_new.p_char);
1148             break;
1149 
1150         case V4L2_CID_RDS_TX_RADIO_TEXT:
1151             ret = si4713_set_rds_radio_text(sdev, ctrl->p_new.p_char);
1152             break;
1153 
1154         case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1155             /* don't handle this control if we force setting all
1156              * controls since in that case it will be handled by
1157              * V4L2_CID_TUNE_POWER_LEVEL. */
1158             if (force)
1159                 break;
1160             fallthrough;
1161         case V4L2_CID_TUNE_POWER_LEVEL:
1162             ret = si4713_tx_tune_power(sdev,
1163                 sdev->tune_pwr_level->val, sdev->tune_ant_cap->val);
1164             if (!ret) {
1165                 /* Make sure we don't set this twice */
1166                 sdev->tune_ant_cap->is_new = false;
1167                 sdev->tune_pwr_level->is_new = false;
1168             }
1169             break;
1170 
1171         case V4L2_CID_RDS_TX_ALT_FREQS_ENABLE:
1172         case V4L2_CID_RDS_TX_ALT_FREQS:
1173             if (sdev->rds_alt_freqs_enable->val) {
1174                 val = sdev->rds_alt_freqs->p_new.p_u32[0];
1175                 val = val / 100 - 876 + 0xe101;
1176             } else {
1177                 val = 0xe0e0;
1178             }
1179             ret = si4713_write_property(sdev, SI4713_TX_RDS_PS_AF, val);
1180             break;
1181 
1182         default:
1183             ret = si4713_choose_econtrol_action(sdev, ctrl->id, &bit,
1184                     &mask, &property, &mul, &table, &size);
1185             if (ret < 0)
1186                 break;
1187 
1188             val = ctrl->val;
1189             if (mul) {
1190                 val = val / mul;
1191             } else if (table) {
1192                 ret = usecs_to_dev(val, table, size);
1193                 if (ret < 0)
1194                     break;
1195                 val = ret;
1196                 ret = 0;
1197             }
1198 
1199             if (mask) {
1200                 ret = si4713_read_property(sdev, property, &val);
1201                 if (ret < 0)
1202                     break;
1203                 val = set_bits(val, ctrl->val, bit, mask);
1204             }
1205 
1206             ret = si4713_write_property(sdev, property, val);
1207             if (ret < 0)
1208                 break;
1209             if (mask)
1210                 val = ctrl->val;
1211             break;
1212         }
1213     }
1214 
1215     return ret;
1216 }
1217 
1218 /* si4713_ioctl - deal with private ioctls (only rnl for now) */
1219 static long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1220 {
1221     struct si4713_device *sdev = to_si4713_device(sd);
1222     struct si4713_rnl *rnl = arg;
1223     u16 frequency;
1224     int rval = 0;
1225 
1226     if (!arg)
1227         return -EINVAL;
1228 
1229     switch (cmd) {
1230     case SI4713_IOC_MEASURE_RNL:
1231         frequency = v4l2_to_si4713(rnl->frequency);
1232 
1233         if (sdev->power_state) {
1234             /* Set desired measurement frequency */
1235             rval = si4713_tx_tune_measure(sdev, frequency, 0);
1236             if (rval < 0)
1237                 return rval;
1238             /* get results from tune status */
1239             rval = si4713_update_tune_status(sdev);
1240             if (rval < 0)
1241                 return rval;
1242         }
1243         rnl->rnl = sdev->tune_rnl;
1244         break;
1245 
1246     default:
1247         /* nothing */
1248         rval = -ENOIOCTLCMD;
1249     }
1250 
1251     return rval;
1252 }
1253 
1254 /* si4713_g_modulator - get modulator attributes */
1255 static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1256 {
1257     struct si4713_device *sdev = to_si4713_device(sd);
1258     int rval = 0;
1259 
1260     if (!sdev)
1261         return -ENODEV;
1262 
1263     if (vm->index > 0)
1264         return -EINVAL;
1265 
1266     strscpy(vm->name, "FM Modulator", sizeof(vm->name));
1267     vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
1268         V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS;
1269 
1270     /* Report current frequency range limits */
1271     vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
1272     vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
1273 
1274     if (sdev->power_state) {
1275         u32 comp_en = 0;
1276 
1277         rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
1278                         &comp_en);
1279         if (rval < 0)
1280             return rval;
1281 
1282         sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
1283     }
1284 
1285     /* Report current audio mode: mono or stereo */
1286     if (sdev->stereo)
1287         vm->txsubchans = V4L2_TUNER_SUB_STEREO;
1288     else
1289         vm->txsubchans = V4L2_TUNER_SUB_MONO;
1290 
1291     /* Report rds feature status */
1292     if (sdev->rds_enabled)
1293         vm->txsubchans |= V4L2_TUNER_SUB_RDS;
1294     else
1295         vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
1296 
1297     return rval;
1298 }
1299 
1300 /* si4713_s_modulator - set modulator attributes */
1301 static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *vm)
1302 {
1303     struct si4713_device *sdev = to_si4713_device(sd);
1304     int rval = 0;
1305     u16 stereo, rds;
1306     u32 p;
1307 
1308     if (!sdev)
1309         return -ENODEV;
1310 
1311     if (vm->index > 0)
1312         return -EINVAL;
1313 
1314     /* Set audio mode: mono or stereo */
1315     if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
1316         stereo = 1;
1317     else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
1318         stereo = 0;
1319     else
1320         return -EINVAL;
1321 
1322     rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
1323 
1324     if (sdev->power_state) {
1325         rval = si4713_read_property(sdev,
1326                         SI4713_TX_COMPONENT_ENABLE, &p);
1327         if (rval < 0)
1328             return rval;
1329 
1330         p = set_bits(p, stereo, 1, 1 << 1);
1331         p = set_bits(p, rds, 2, 1 << 2);
1332 
1333         rval = si4713_write_property(sdev,
1334                         SI4713_TX_COMPONENT_ENABLE, p);
1335         if (rval < 0)
1336             return rval;
1337     }
1338 
1339     sdev->stereo = stereo;
1340     sdev->rds_enabled = rds;
1341 
1342     return rval;
1343 }
1344 
1345 /* si4713_g_frequency - get tuner or modulator radio frequency */
1346 static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1347 {
1348     struct si4713_device *sdev = to_si4713_device(sd);
1349     int rval = 0;
1350 
1351     if (f->tuner)
1352         return -EINVAL;
1353 
1354     if (sdev->power_state) {
1355         u16 freq;
1356         u8 p, a, n;
1357 
1358         rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
1359         if (rval < 0)
1360             return rval;
1361 
1362         sdev->frequency = freq;
1363     }
1364 
1365     f->frequency = si4713_to_v4l2(sdev->frequency);
1366 
1367     return rval;
1368 }
1369 
1370 /* si4713_s_frequency - set tuner or modulator radio frequency */
1371 static int si4713_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f)
1372 {
1373     struct si4713_device *sdev = to_si4713_device(sd);
1374     int rval = 0;
1375     u16 frequency = v4l2_to_si4713(f->frequency);
1376 
1377     if (f->tuner)
1378         return -EINVAL;
1379 
1380     /* Check frequency range */
1381     frequency = clamp_t(u16, frequency, FREQ_RANGE_LOW, FREQ_RANGE_HIGH);
1382 
1383     if (sdev->power_state) {
1384         rval = si4713_tx_tune_freq(sdev, frequency);
1385         if (rval < 0)
1386             return rval;
1387         frequency = rval;
1388         rval = 0;
1389     }
1390     sdev->frequency = frequency;
1391 
1392     return rval;
1393 }
1394 
1395 static const struct v4l2_ctrl_ops si4713_ctrl_ops = {
1396     .s_ctrl = si4713_s_ctrl,
1397 };
1398 
1399 static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
1400     .ioctl      = si4713_ioctl,
1401 };
1402 
1403 static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
1404     .g_frequency    = si4713_g_frequency,
1405     .s_frequency    = si4713_s_frequency,
1406     .g_modulator    = si4713_g_modulator,
1407     .s_modulator    = si4713_s_modulator,
1408 };
1409 
1410 static const struct v4l2_subdev_ops si4713_subdev_ops = {
1411     .core       = &si4713_subdev_core_ops,
1412     .tuner      = &si4713_subdev_tuner_ops,
1413 };
1414 
1415 static const struct v4l2_ctrl_config si4713_alt_freqs_ctrl = {
1416     .id = V4L2_CID_RDS_TX_ALT_FREQS,
1417     .type = V4L2_CTRL_TYPE_U32,
1418     .min = 87600,
1419     .max = 107900,
1420     .step = 100,
1421     .def = 87600,
1422     .dims = { 1 },
1423     .elem_size = sizeof(u32),
1424 };
1425 
1426 /*
1427  * I2C driver interface
1428  */
1429 /* si4713_probe - probe for the device */
1430 static int si4713_probe(struct i2c_client *client)
1431 {
1432     struct si4713_device *sdev;
1433     struct v4l2_ctrl_handler *hdl;
1434     struct si4713_platform_data *pdata = client->dev.platform_data;
1435     struct device_node *np = client->dev.of_node;
1436     struct radio_si4713_platform_data si4713_pdev_pdata;
1437     struct platform_device *si4713_pdev;
1438     int rval;
1439 
1440     sdev = devm_kzalloc(&client->dev, sizeof(*sdev), GFP_KERNEL);
1441     if (!sdev) {
1442         dev_err(&client->dev, "Failed to alloc video device.\n");
1443         rval = -ENOMEM;
1444         goto exit;
1445     }
1446 
1447     sdev->gpio_reset = devm_gpiod_get_optional(&client->dev, "reset",
1448                            GPIOD_OUT_LOW);
1449     if (IS_ERR(sdev->gpio_reset)) {
1450         rval = PTR_ERR(sdev->gpio_reset);
1451         dev_err(&client->dev, "Failed to request gpio: %d\n", rval);
1452         goto exit;
1453     }
1454 
1455     sdev->vdd = devm_regulator_get_optional(&client->dev, "vdd");
1456     if (IS_ERR(sdev->vdd)) {
1457         rval = PTR_ERR(sdev->vdd);
1458         if (rval == -EPROBE_DEFER)
1459             goto exit;
1460 
1461         dev_dbg(&client->dev, "no vdd regulator found: %d\n", rval);
1462         sdev->vdd = NULL;
1463     }
1464 
1465     sdev->vio = devm_regulator_get_optional(&client->dev, "vio");
1466     if (IS_ERR(sdev->vio)) {
1467         rval = PTR_ERR(sdev->vio);
1468         if (rval == -EPROBE_DEFER)
1469             goto exit;
1470 
1471         dev_dbg(&client->dev, "no vio regulator found: %d\n", rval);
1472         sdev->vio = NULL;
1473     }
1474 
1475     v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
1476 
1477     init_completion(&sdev->work);
1478 
1479     hdl = &sdev->ctrl_handler;
1480     v4l2_ctrl_handler_init(hdl, 20);
1481     sdev->mute = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1482             V4L2_CID_AUDIO_MUTE, 0, 1, 1, DEFAULT_MUTE);
1483 
1484     sdev->rds_pi = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1485             V4L2_CID_RDS_TX_PI, 0, 0xffff, 1, DEFAULT_RDS_PI);
1486     sdev->rds_pty = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1487             V4L2_CID_RDS_TX_PTY, 0, 31, 1, DEFAULT_RDS_PTY);
1488     sdev->rds_compressed = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1489             V4L2_CID_RDS_TX_COMPRESSED, 0, 1, 1, 0);
1490     sdev->rds_art_head = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1491             V4L2_CID_RDS_TX_ARTIFICIAL_HEAD, 0, 1, 1, 0);
1492     sdev->rds_stereo = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1493             V4L2_CID_RDS_TX_MONO_STEREO, 0, 1, 1, 1);
1494     sdev->rds_tp = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1495             V4L2_CID_RDS_TX_TRAFFIC_PROGRAM, 0, 1, 1, 0);
1496     sdev->rds_ta = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1497             V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT, 0, 1, 1, 0);
1498     sdev->rds_ms = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1499             V4L2_CID_RDS_TX_MUSIC_SPEECH, 0, 1, 1, 1);
1500     sdev->rds_dyn_pty = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1501             V4L2_CID_RDS_TX_DYNAMIC_PTY, 0, 1, 1, 0);
1502     sdev->rds_alt_freqs_enable = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1503             V4L2_CID_RDS_TX_ALT_FREQS_ENABLE, 0, 1, 1, 0);
1504     sdev->rds_alt_freqs = v4l2_ctrl_new_custom(hdl, &si4713_alt_freqs_ctrl, NULL);
1505     sdev->rds_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1506             V4L2_CID_RDS_TX_DEVIATION, 0, MAX_RDS_DEVIATION,
1507             10, DEFAULT_RDS_DEVIATION);
1508     /*
1509      * Report step as 8. From RDS spec, psname
1510      * should be 8. But there are receivers which scroll strings
1511      * sized as 8xN.
1512      */
1513     sdev->rds_ps_name = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1514             V4L2_CID_RDS_TX_PS_NAME, 0, MAX_RDS_PS_NAME, 8, 0);
1515     /*
1516      * Report step as 32 (2A block). From RDS spec,
1517      * radio text should be 32 for 2A block. But there are receivers
1518      * which scroll strings sized as 32xN. Setting default to 32.
1519      */
1520     sdev->rds_radio_text = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1521             V4L2_CID_RDS_TX_RADIO_TEXT, 0, MAX_RDS_RADIO_TEXT, 32, 0);
1522 
1523     sdev->limiter_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1524             V4L2_CID_AUDIO_LIMITER_ENABLED, 0, 1, 1, 1);
1525     sdev->limiter_release_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1526             V4L2_CID_AUDIO_LIMITER_RELEASE_TIME, 250,
1527             MAX_LIMITER_RELEASE_TIME, 10, DEFAULT_LIMITER_RTIME);
1528     sdev->limiter_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1529             V4L2_CID_AUDIO_LIMITER_DEVIATION, 0,
1530             MAX_LIMITER_DEVIATION, 10, DEFAULT_LIMITER_DEV);
1531 
1532     sdev->compression_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1533             V4L2_CID_AUDIO_COMPRESSION_ENABLED, 0, 1, 1, 1);
1534     sdev->compression_gain = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1535             V4L2_CID_AUDIO_COMPRESSION_GAIN, 0, MAX_ACOMP_GAIN, 1,
1536             DEFAULT_ACOMP_GAIN);
1537     sdev->compression_threshold = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1538             V4L2_CID_AUDIO_COMPRESSION_THRESHOLD,
1539             MIN_ACOMP_THRESHOLD, MAX_ACOMP_THRESHOLD, 1,
1540             DEFAULT_ACOMP_THRESHOLD);
1541     sdev->compression_attack_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1542             V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME, 0,
1543             MAX_ACOMP_ATTACK_TIME, 500, DEFAULT_ACOMP_ATIME);
1544     sdev->compression_release_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1545             V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME, 100000,
1546             MAX_ACOMP_RELEASE_TIME, 100000, DEFAULT_ACOMP_RTIME);
1547 
1548     sdev->pilot_tone_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1549             V4L2_CID_PILOT_TONE_ENABLED, 0, 1, 1, 1);
1550     sdev->pilot_tone_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1551             V4L2_CID_PILOT_TONE_DEVIATION, 0, MAX_PILOT_DEVIATION,
1552             10, DEFAULT_PILOT_DEVIATION);
1553     sdev->pilot_tone_freq = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1554             V4L2_CID_PILOT_TONE_FREQUENCY, 0, MAX_PILOT_FREQUENCY,
1555             1, DEFAULT_PILOT_FREQUENCY);
1556 
1557     sdev->tune_preemphasis = v4l2_ctrl_new_std_menu(hdl, &si4713_ctrl_ops,
1558             V4L2_CID_TUNE_PREEMPHASIS,
1559             V4L2_PREEMPHASIS_75_uS, 0, V4L2_PREEMPHASIS_50_uS);
1560     sdev->tune_pwr_level = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1561             V4L2_CID_TUNE_POWER_LEVEL, 0, SI4713_MAX_POWER,
1562             1, DEFAULT_POWER_LEVEL);
1563     sdev->tune_ant_cap = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1564             V4L2_CID_TUNE_ANTENNA_CAPACITOR, 0, SI4713_MAX_ANTCAP,
1565             1, 0);
1566 
1567     if (hdl->error) {
1568         rval = hdl->error;
1569         goto free_ctrls;
1570     }
1571     v4l2_ctrl_cluster(29, &sdev->mute);
1572     sdev->sd.ctrl_handler = hdl;
1573 
1574     if (client->irq) {
1575         rval = devm_request_irq(&client->dev, client->irq,
1576             si4713_handler, IRQF_TRIGGER_FALLING,
1577             client->name, sdev);
1578         if (rval < 0) {
1579             v4l2_err(&sdev->sd, "Could not request IRQ\n");
1580             goto free_ctrls;
1581         }
1582         v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
1583     } else {
1584         v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
1585     }
1586 
1587     rval = si4713_initialize(sdev);
1588     if (rval < 0) {
1589         v4l2_err(&sdev->sd, "Failed to probe device information.\n");
1590         goto free_ctrls;
1591     }
1592 
1593     if (!np && (!pdata || !pdata->is_platform_device))
1594         return 0;
1595 
1596     si4713_pdev = platform_device_alloc("radio-si4713", -1);
1597     if (!si4713_pdev) {
1598         rval = -ENOMEM;
1599         goto put_main_pdev;
1600     }
1601 
1602     si4713_pdev_pdata.subdev = client;
1603     rval = platform_device_add_data(si4713_pdev, &si4713_pdev_pdata,
1604                     sizeof(si4713_pdev_pdata));
1605     if (rval)
1606         goto put_main_pdev;
1607 
1608     rval = platform_device_add(si4713_pdev);
1609     if (rval)
1610         goto put_main_pdev;
1611 
1612     sdev->pd = si4713_pdev;
1613 
1614     return 0;
1615 
1616 put_main_pdev:
1617     platform_device_put(si4713_pdev);
1618     v4l2_device_unregister_subdev(&sdev->sd);
1619 free_ctrls:
1620     v4l2_ctrl_handler_free(hdl);
1621 exit:
1622     return rval;
1623 }
1624 
1625 /* si4713_remove - remove the device */
1626 static int si4713_remove(struct i2c_client *client)
1627 {
1628     struct v4l2_subdev *sd = i2c_get_clientdata(client);
1629     struct si4713_device *sdev = to_si4713_device(sd);
1630 
1631     platform_device_unregister(sdev->pd);
1632 
1633     if (sdev->power_state)
1634         si4713_set_power_state(sdev, POWER_DOWN);
1635 
1636     v4l2_device_unregister_subdev(sd);
1637     v4l2_ctrl_handler_free(sd->ctrl_handler);
1638 
1639     return 0;
1640 }
1641 
1642 /* si4713_i2c_driver - i2c driver interface */
1643 static const struct i2c_device_id si4713_id[] = {
1644     { "si4713" , 0 },
1645     { },
1646 };
1647 MODULE_DEVICE_TABLE(i2c, si4713_id);
1648 
1649 #if IS_ENABLED(CONFIG_OF)
1650 static const struct of_device_id si4713_of_match[] = {
1651     { .compatible = "silabs,si4713" },
1652     { },
1653 };
1654 MODULE_DEVICE_TABLE(of, si4713_of_match);
1655 #endif
1656 
1657 static struct i2c_driver si4713_i2c_driver = {
1658     .driver     = {
1659         .name   = "si4713",
1660         .of_match_table = of_match_ptr(si4713_of_match),
1661     },
1662     .probe_new  = si4713_probe,
1663     .remove         = si4713_remove,
1664     .id_table       = si4713_id,
1665 };
1666 
1667 module_i2c_driver(si4713_i2c_driver);