Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003     Driver for SAA6588 RDS decoder
0004 
0005     (c) 2005 Hans J. Koch
0006 
0007 */
0008 
0009 
0010 #include <linux/module.h>
0011 #include <linux/kernel.h>
0012 #include <linux/i2c.h>
0013 #include <linux/types.h>
0014 #include <linux/videodev2.h>
0015 #include <linux/init.h>
0016 #include <linux/errno.h>
0017 #include <linux/slab.h>
0018 #include <linux/poll.h>
0019 #include <linux/wait.h>
0020 #include <linux/uaccess.h>
0021 
0022 #include <media/i2c/saa6588.h>
0023 #include <media/v4l2-device.h>
0024 
0025 
0026 /* insmod options */
0027 static unsigned int debug;
0028 static unsigned int xtal;
0029 static unsigned int mmbs;
0030 static unsigned int plvl;
0031 static unsigned int bufblocks = 100;
0032 
0033 module_param(debug, int, 0644);
0034 MODULE_PARM_DESC(debug, "enable debug messages");
0035 module_param(xtal, int, 0);
0036 MODULE_PARM_DESC(xtal, "select oscillator frequency (0..3), default 0");
0037 module_param(mmbs, int, 0);
0038 MODULE_PARM_DESC(mmbs, "enable MMBS mode: 0=off (default), 1=on");
0039 module_param(plvl, int, 0);
0040 MODULE_PARM_DESC(plvl, "select pause level (0..3), default 0");
0041 module_param(bufblocks, int, 0);
0042 MODULE_PARM_DESC(bufblocks, "number of buffered blocks, default 100");
0043 
0044 MODULE_DESCRIPTION("v4l2 driver module for SAA6588 RDS decoder");
0045 MODULE_AUTHOR("Hans J. Koch <koch@hjk-az.de>");
0046 
0047 MODULE_LICENSE("GPL");
0048 
0049 /* ---------------------------------------------------------------------- */
0050 
0051 #define UNSET       (-1U)
0052 #define PREFIX      "saa6588: "
0053 #define dprintk     if (debug) printk
0054 
0055 struct saa6588 {
0056     struct v4l2_subdev sd;
0057     struct delayed_work work;
0058     spinlock_t lock;
0059     unsigned char *buffer;
0060     unsigned int buf_size;
0061     unsigned int rd_index;
0062     unsigned int wr_index;
0063     unsigned int block_count;
0064     unsigned char last_blocknum;
0065     wait_queue_head_t read_queue;
0066     int data_available_for_read;
0067     u8 sync;
0068 };
0069 
0070 static inline struct saa6588 *to_saa6588(struct v4l2_subdev *sd)
0071 {
0072     return container_of(sd, struct saa6588, sd);
0073 }
0074 
0075 /* ---------------------------------------------------------------------- */
0076 
0077 /*
0078  * SAA6588 defines
0079  */
0080 
0081 /* Initialization and mode control byte (0w) */
0082 
0083 /* bit 0+1 (DAC0/DAC1) */
0084 #define cModeStandard           0x00
0085 #define cModeFastPI             0x01
0086 #define cModeReducedRequest     0x02
0087 #define cModeInvalid            0x03
0088 
0089 /* bit 2 (RBDS) */
0090 #define cProcessingModeRDS      0x00
0091 #define cProcessingModeRBDS     0x04
0092 
0093 /* bit 3+4 (SYM0/SYM1) */
0094 #define cErrCorrectionNone      0x00
0095 #define cErrCorrection2Bits     0x08
0096 #define cErrCorrection5Bits     0x10
0097 #define cErrCorrectionNoneRBDS  0x18
0098 
0099 /* bit 5 (NWSY) */
0100 #define cSyncNormal             0x00
0101 #define cSyncRestart            0x20
0102 
0103 /* bit 6 (TSQD) */
0104 #define cSigQualityDetectOFF    0x00
0105 #define cSigQualityDetectON     0x40
0106 
0107 /* bit 7 (SQCM) */
0108 #define cSigQualityTriggered    0x00
0109 #define cSigQualityContinous    0x80
0110 
0111 /* Pause level and flywheel control byte (1w) */
0112 
0113 /* bits 0..5 (FEB0..FEB5) */
0114 #define cFlywheelMaxBlocksMask  0x3F
0115 #define cFlywheelDefault        0x20
0116 
0117 /* bits 6+7 (PL0/PL1) */
0118 #define cPauseLevel_11mV    0x00
0119 #define cPauseLevel_17mV        0x40
0120 #define cPauseLevel_27mV        0x80
0121 #define cPauseLevel_43mV        0xC0
0122 
0123 /* Pause time/oscillator frequency/quality detector control byte (1w) */
0124 
0125 /* bits 0..4 (SQS0..SQS4) */
0126 #define cQualityDetectSensMask  0x1F
0127 #define cQualityDetectDefault   0x0F
0128 
0129 /* bit 5 (SOSC) */
0130 #define cSelectOscFreqOFF   0x00
0131 #define cSelectOscFreqON    0x20
0132 
0133 /* bit 6+7 (PTF0/PTF1) */
0134 #define cOscFreq_4332kHz    0x00
0135 #define cOscFreq_8664kHz    0x40
0136 #define cOscFreq_12996kHz   0x80
0137 #define cOscFreq_17328kHz   0xC0
0138 
0139 /* ---------------------------------------------------------------------- */
0140 
0141 static bool block_from_buf(struct saa6588 *s, unsigned char *buf)
0142 {
0143     int i;
0144 
0145     if (s->rd_index == s->wr_index) {
0146         if (debug > 2)
0147             dprintk(PREFIX "Read: buffer empty.\n");
0148         return false;
0149     }
0150 
0151     if (debug > 2) {
0152         dprintk(PREFIX "Read: ");
0153         for (i = s->rd_index; i < s->rd_index + 3; i++)
0154             dprintk("0x%02x ", s->buffer[i]);
0155     }
0156 
0157     memcpy(buf, &s->buffer[s->rd_index], 3);
0158 
0159     s->rd_index += 3;
0160     if (s->rd_index >= s->buf_size)
0161         s->rd_index = 0;
0162     s->block_count--;
0163 
0164     if (debug > 2)
0165         dprintk("%d blocks total.\n", s->block_count);
0166 
0167     return true;
0168 }
0169 
0170 static void read_from_buf(struct saa6588 *s, struct saa6588_command *a)
0171 {
0172     unsigned char __user *buf_ptr = a->buffer;
0173     unsigned char buf[3];
0174     unsigned long flags;
0175     unsigned int rd_blocks;
0176     unsigned int i;
0177 
0178     a->result = 0;
0179     if (!a->buffer)
0180         return;
0181 
0182     while (!a->nonblocking && !s->data_available_for_read) {
0183         int ret = wait_event_interruptible(s->read_queue,
0184                          s->data_available_for_read);
0185         if (ret == -ERESTARTSYS) {
0186             a->result = -EINTR;
0187             return;
0188         }
0189     }
0190 
0191     rd_blocks = a->block_count;
0192     spin_lock_irqsave(&s->lock, flags);
0193     if (rd_blocks > s->block_count)
0194         rd_blocks = s->block_count;
0195     spin_unlock_irqrestore(&s->lock, flags);
0196 
0197     if (!rd_blocks)
0198         return;
0199 
0200     for (i = 0; i < rd_blocks; i++) {
0201         bool got_block;
0202 
0203         spin_lock_irqsave(&s->lock, flags);
0204         got_block = block_from_buf(s, buf);
0205         spin_unlock_irqrestore(&s->lock, flags);
0206         if (!got_block)
0207             break;
0208         if (copy_to_user(buf_ptr, buf, 3)) {
0209             a->result = -EFAULT;
0210             return;
0211         }
0212         buf_ptr += 3;
0213         a->result += 3;
0214     }
0215     spin_lock_irqsave(&s->lock, flags);
0216     s->data_available_for_read = (s->block_count > 0);
0217     spin_unlock_irqrestore(&s->lock, flags);
0218 }
0219 
0220 static void block_to_buf(struct saa6588 *s, unsigned char *blockbuf)
0221 {
0222     unsigned int i;
0223 
0224     if (debug > 3)
0225         dprintk(PREFIX "New block: ");
0226 
0227     for (i = 0; i < 3; ++i) {
0228         if (debug > 3)
0229             dprintk("0x%02x ", blockbuf[i]);
0230         s->buffer[s->wr_index] = blockbuf[i];
0231         s->wr_index++;
0232     }
0233 
0234     if (s->wr_index >= s->buf_size)
0235         s->wr_index = 0;
0236 
0237     if (s->wr_index == s->rd_index) {
0238         s->rd_index += 3;
0239         if (s->rd_index >= s->buf_size)
0240             s->rd_index = 0;
0241     } else
0242         s->block_count++;
0243 
0244     if (debug > 3)
0245         dprintk("%d blocks total.\n", s->block_count);
0246 }
0247 
0248 static void saa6588_i2c_poll(struct saa6588 *s)
0249 {
0250     struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
0251     unsigned long flags;
0252     unsigned char tmpbuf[6];
0253     unsigned char blocknum;
0254     unsigned char tmp;
0255 
0256     /* Although we only need 3 bytes, we have to read at least 6.
0257        SAA6588 returns garbage otherwise. */
0258     if (6 != i2c_master_recv(client, &tmpbuf[0], 6)) {
0259         if (debug > 1)
0260             dprintk(PREFIX "read error!\n");
0261         return;
0262     }
0263 
0264     s->sync = tmpbuf[0] & 0x10;
0265     if (!s->sync)
0266         return;
0267     blocknum = tmpbuf[0] >> 5;
0268     if (blocknum == s->last_blocknum) {
0269         if (debug > 3)
0270             dprintk("Saw block %d again.\n", blocknum);
0271         return;
0272     }
0273 
0274     s->last_blocknum = blocknum;
0275 
0276     /*
0277        Byte order according to v4l2 specification:
0278 
0279        Byte 0: Least Significant Byte of RDS Block
0280        Byte 1: Most Significant Byte of RDS Block
0281        Byte 2 Bit 7: Error bit. Indicates that an uncorrectable error
0282        occurred during reception of this block.
0283        Bit 6: Corrected bit. Indicates that an error was
0284        corrected for this data block.
0285        Bits 5-3: Same as bits 0-2.
0286        Bits 2-0: Block number.
0287 
0288        SAA6588 byte order is Status-MSB-LSB, so we have to swap the
0289        first and the last of the 3 bytes block.
0290      */
0291 
0292     swap(tmpbuf[2], tmpbuf[0]);
0293 
0294     /* Map 'Invalid block E' to 'Invalid Block' */
0295     if (blocknum == 6)
0296         blocknum = V4L2_RDS_BLOCK_INVALID;
0297     /* And if are not in mmbs mode, then 'Block E' is also mapped
0298        to 'Invalid Block'. As far as I can tell MMBS is discontinued,
0299        and if there is ever a need to support E blocks, then please
0300        contact the linux-media mailinglist. */
0301     else if (!mmbs && blocknum == 5)
0302         blocknum = V4L2_RDS_BLOCK_INVALID;
0303     tmp = blocknum;
0304     tmp |= blocknum << 3;   /* Received offset == Offset Name (OK ?) */
0305     if ((tmpbuf[2] & 0x03) == 0x03)
0306         tmp |= V4L2_RDS_BLOCK_ERROR;     /* uncorrectable error */
0307     else if ((tmpbuf[2] & 0x03) != 0x00)
0308         tmp |= V4L2_RDS_BLOCK_CORRECTED; /* corrected error */
0309     tmpbuf[2] = tmp;    /* Is this enough ? Should we also check other bits ? */
0310 
0311     spin_lock_irqsave(&s->lock, flags);
0312     block_to_buf(s, tmpbuf);
0313     spin_unlock_irqrestore(&s->lock, flags);
0314     s->data_available_for_read = 1;
0315     wake_up_interruptible(&s->read_queue);
0316 }
0317 
0318 static void saa6588_work(struct work_struct *work)
0319 {
0320     struct saa6588 *s = container_of(work, struct saa6588, work.work);
0321 
0322     saa6588_i2c_poll(s);
0323     schedule_delayed_work(&s->work, msecs_to_jiffies(20));
0324 }
0325 
0326 static void saa6588_configure(struct saa6588 *s)
0327 {
0328     struct i2c_client *client = v4l2_get_subdevdata(&s->sd);
0329     unsigned char buf[3];
0330     int rc;
0331 
0332     buf[0] = cSyncRestart;
0333     if (mmbs)
0334         buf[0] |= cProcessingModeRBDS;
0335 
0336     buf[1] = cFlywheelDefault;
0337     switch (plvl) {
0338     case 0:
0339         buf[1] |= cPauseLevel_11mV;
0340         break;
0341     case 1:
0342         buf[1] |= cPauseLevel_17mV;
0343         break;
0344     case 2:
0345         buf[1] |= cPauseLevel_27mV;
0346         break;
0347     case 3:
0348         buf[1] |= cPauseLevel_43mV;
0349         break;
0350     default:        /* nothing */
0351         break;
0352     }
0353 
0354     buf[2] = cQualityDetectDefault | cSelectOscFreqON;
0355 
0356     switch (xtal) {
0357     case 0:
0358         buf[2] |= cOscFreq_4332kHz;
0359         break;
0360     case 1:
0361         buf[2] |= cOscFreq_8664kHz;
0362         break;
0363     case 2:
0364         buf[2] |= cOscFreq_12996kHz;
0365         break;
0366     case 3:
0367         buf[2] |= cOscFreq_17328kHz;
0368         break;
0369     default:        /* nothing */
0370         break;
0371     }
0372 
0373     dprintk(PREFIX "writing: 0w=0x%02x 1w=0x%02x 2w=0x%02x\n",
0374         buf[0], buf[1], buf[2]);
0375 
0376     rc = i2c_master_send(client, buf, 3);
0377     if (rc != 3)
0378         printk(PREFIX "i2c i/o error: rc == %d (should be 3)\n", rc);
0379 }
0380 
0381 /* ---------------------------------------------------------------------- */
0382 
0383 static long saa6588_command(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
0384 {
0385     struct saa6588 *s = to_saa6588(sd);
0386     struct saa6588_command *a = arg;
0387 
0388     switch (cmd) {
0389         /* --- close() for /dev/radio --- */
0390     case SAA6588_CMD_CLOSE:
0391         s->data_available_for_read = 1;
0392         wake_up_interruptible(&s->read_queue);
0393         s->data_available_for_read = 0;
0394         a->result = 0;
0395         break;
0396         /* --- read() for /dev/radio --- */
0397     case SAA6588_CMD_READ:
0398         read_from_buf(s, a);
0399         break;
0400         /* --- poll() for /dev/radio --- */
0401     case SAA6588_CMD_POLL:
0402         a->poll_mask = 0;
0403         if (s->data_available_for_read)
0404             a->poll_mask |= EPOLLIN | EPOLLRDNORM;
0405         poll_wait(a->instance, &s->read_queue, a->event_list);
0406         break;
0407 
0408     default:
0409         /* nothing */
0410         return -ENOIOCTLCMD;
0411     }
0412     return 0;
0413 }
0414 
0415 static int saa6588_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
0416 {
0417     struct saa6588 *s = to_saa6588(sd);
0418 
0419     vt->capability |= V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO;
0420     if (s->sync)
0421         vt->rxsubchans |= V4L2_TUNER_SUB_RDS;
0422     return 0;
0423 }
0424 
0425 static int saa6588_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
0426 {
0427     struct saa6588 *s = to_saa6588(sd);
0428 
0429     saa6588_configure(s);
0430     return 0;
0431 }
0432 
0433 /* ----------------------------------------------------------------------- */
0434 
0435 static const struct v4l2_subdev_core_ops saa6588_core_ops = {
0436     .command = saa6588_command,
0437 };
0438 
0439 static const struct v4l2_subdev_tuner_ops saa6588_tuner_ops = {
0440     .g_tuner = saa6588_g_tuner,
0441     .s_tuner = saa6588_s_tuner,
0442 };
0443 
0444 static const struct v4l2_subdev_ops saa6588_ops = {
0445     .core = &saa6588_core_ops,
0446     .tuner = &saa6588_tuner_ops,
0447 };
0448 
0449 /* ---------------------------------------------------------------------- */
0450 
0451 static int saa6588_probe(struct i2c_client *client,
0452              const struct i2c_device_id *id)
0453 {
0454     struct saa6588 *s;
0455     struct v4l2_subdev *sd;
0456 
0457     v4l_info(client, "saa6588 found @ 0x%x (%s)\n",
0458             client->addr << 1, client->adapter->name);
0459 
0460     s = devm_kzalloc(&client->dev, sizeof(*s), GFP_KERNEL);
0461     if (s == NULL)
0462         return -ENOMEM;
0463 
0464     s->buf_size = bufblocks * 3;
0465 
0466     s->buffer = devm_kzalloc(&client->dev, s->buf_size, GFP_KERNEL);
0467     if (s->buffer == NULL)
0468         return -ENOMEM;
0469     sd = &s->sd;
0470     v4l2_i2c_subdev_init(sd, client, &saa6588_ops);
0471     spin_lock_init(&s->lock);
0472     s->block_count = 0;
0473     s->wr_index = 0;
0474     s->rd_index = 0;
0475     s->last_blocknum = 0xff;
0476     init_waitqueue_head(&s->read_queue);
0477     s->data_available_for_read = 0;
0478 
0479     saa6588_configure(s);
0480 
0481     /* start polling via eventd */
0482     INIT_DELAYED_WORK(&s->work, saa6588_work);
0483     schedule_delayed_work(&s->work, 0);
0484     return 0;
0485 }
0486 
0487 static int saa6588_remove(struct i2c_client *client)
0488 {
0489     struct v4l2_subdev *sd = i2c_get_clientdata(client);
0490     struct saa6588 *s = to_saa6588(sd);
0491 
0492     v4l2_device_unregister_subdev(sd);
0493 
0494     cancel_delayed_work_sync(&s->work);
0495 
0496     return 0;
0497 }
0498 
0499 /* ----------------------------------------------------------------------- */
0500 
0501 static const struct i2c_device_id saa6588_id[] = {
0502     { "saa6588", 0 },
0503     { }
0504 };
0505 MODULE_DEVICE_TABLE(i2c, saa6588_id);
0506 
0507 static struct i2c_driver saa6588_driver = {
0508     .driver = {
0509         .name   = "saa6588",
0510     },
0511     .probe      = saa6588_probe,
0512     .remove     = saa6588_remove,
0513     .id_table   = saa6588_id,
0514 };
0515 
0516 module_i2c_driver(saa6588_driver);