Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Routines for control of the CS8427 via i2c bus
0004  *  IEC958 (S/PDIF) receiver & transmitter by Cirrus Logic
0005  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
0006  */
0007 
0008 #include <linux/slab.h>
0009 #include <linux/delay.h>
0010 #include <linux/init.h>
0011 #include <linux/bitrev.h>
0012 #include <linux/module.h>
0013 #include <asm/unaligned.h>
0014 #include <sound/core.h>
0015 #include <sound/control.h>
0016 #include <sound/pcm.h>
0017 #include <sound/cs8427.h>
0018 #include <sound/asoundef.h>
0019 
0020 static void snd_cs8427_reset(struct snd_i2c_device *cs8427);
0021 
0022 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
0023 MODULE_DESCRIPTION("IEC958 (S/PDIF) receiver & transmitter by Cirrus Logic");
0024 MODULE_LICENSE("GPL");
0025 
0026 #define CS8427_ADDR         (0x20>>1) /* fixed address */
0027 
0028 struct cs8427_stream {
0029     struct snd_pcm_substream *substream;
0030     char hw_status[24];     /* hardware status */
0031     char def_status[24];        /* default status */
0032     char pcm_status[24];        /* PCM private status */
0033     char hw_udata[32];
0034     struct snd_kcontrol *pcm_ctl;
0035 };
0036 
0037 struct cs8427 {
0038     unsigned char regmap[0x14]; /* map of first 1 + 13 registers */
0039     unsigned int rate;
0040     unsigned int reset_timeout;
0041     struct cs8427_stream playback;
0042     struct cs8427_stream capture;
0043 };
0044 
0045 int snd_cs8427_reg_write(struct snd_i2c_device *device, unsigned char reg,
0046              unsigned char val)
0047 {
0048     int err;
0049     unsigned char buf[2];
0050 
0051     buf[0] = reg & 0x7f;
0052     buf[1] = val;
0053     err = snd_i2c_sendbytes(device, buf, 2);
0054     if (err != 2) {
0055         snd_printk(KERN_ERR "unable to send bytes 0x%02x:0x%02x "
0056                "to CS8427 (%i)\n", buf[0], buf[1], err);
0057         return err < 0 ? err : -EIO;
0058     }
0059     return 0;
0060 }
0061 
0062 EXPORT_SYMBOL(snd_cs8427_reg_write);
0063 
0064 static int snd_cs8427_reg_read(struct snd_i2c_device *device, unsigned char reg)
0065 {
0066     int err;
0067     unsigned char buf;
0068 
0069     err = snd_i2c_sendbytes(device, &reg, 1);
0070     if (err != 1) {
0071         snd_printk(KERN_ERR "unable to send register 0x%x byte "
0072                "to CS8427\n", reg);
0073         return err < 0 ? err : -EIO;
0074     }
0075     err = snd_i2c_readbytes(device, &buf, 1);
0076     if (err != 1) {
0077         snd_printk(KERN_ERR "unable to read register 0x%x byte "
0078                "from CS8427\n", reg);
0079         return err < 0 ? err : -EIO;
0080     }
0081     return buf;
0082 }
0083 
0084 static int snd_cs8427_select_corudata(struct snd_i2c_device *device, int udata)
0085 {
0086     struct cs8427 *chip = device->private_data;
0087     int err;
0088 
0089     udata = udata ? CS8427_BSEL : 0;
0090     if (udata != (chip->regmap[CS8427_REG_CSDATABUF] & udata)) {
0091         chip->regmap[CS8427_REG_CSDATABUF] &= ~CS8427_BSEL;
0092         chip->regmap[CS8427_REG_CSDATABUF] |= udata;
0093         err = snd_cs8427_reg_write(device, CS8427_REG_CSDATABUF,
0094                        chip->regmap[CS8427_REG_CSDATABUF]);
0095         if (err < 0)
0096             return err;
0097     }
0098     return 0;
0099 }
0100 
0101 static int snd_cs8427_send_corudata(struct snd_i2c_device *device,
0102                     int udata,
0103                     unsigned char *ndata,
0104                     int count)
0105 {
0106     struct cs8427 *chip = device->private_data;
0107     char *hw_data = udata ?
0108         chip->playback.hw_udata : chip->playback.hw_status;
0109     unsigned char data[32];
0110     int err, idx;
0111 
0112     if (!memcmp(hw_data, ndata, count))
0113         return 0;
0114     err = snd_cs8427_select_corudata(device, udata);
0115     if (err < 0)
0116         return err;
0117     memcpy(hw_data, ndata, count);
0118     if (udata) {
0119         memset(data, 0, sizeof(data));
0120         if (memcmp(hw_data, data, count) == 0) {
0121             chip->regmap[CS8427_REG_UDATABUF] &= ~CS8427_UBMMASK;
0122             chip->regmap[CS8427_REG_UDATABUF] |= CS8427_UBMZEROS |
0123                 CS8427_EFTUI;
0124             err = snd_cs8427_reg_write(device, CS8427_REG_UDATABUF,
0125                            chip->regmap[CS8427_REG_UDATABUF]);
0126             return err < 0 ? err : 0;
0127         }
0128     }
0129     data[0] = CS8427_REG_AUTOINC | CS8427_REG_CORU_DATABUF;
0130     for (idx = 0; idx < count; idx++)
0131         data[idx + 1] = bitrev8(ndata[idx]);
0132     if (snd_i2c_sendbytes(device, data, count + 1) != count + 1)
0133         return -EIO;
0134     return 1;
0135 }
0136 
0137 static void snd_cs8427_free(struct snd_i2c_device *device)
0138 {
0139     kfree(device->private_data);
0140 }
0141 
0142 int snd_cs8427_init(struct snd_i2c_bus *bus,
0143             struct snd_i2c_device *device)
0144 {
0145     static unsigned char initvals1[] = {
0146       CS8427_REG_CONTROL1 | CS8427_REG_AUTOINC,
0147       /* CS8427_REG_CONTROL1: RMCK to OMCK, valid PCM audio, disable mutes,
0148          TCBL=output */
0149       CS8427_SWCLK | CS8427_TCBLDIR,
0150       /* CS8427_REG_CONTROL2: hold last valid audio sample, RMCK=256*Fs,
0151          normal stereo operation */
0152       0x00,
0153       /* CS8427_REG_DATAFLOW: output drivers normal operation, Tx<=serial,
0154          Rx=>serial */
0155       CS8427_TXDSERIAL | CS8427_SPDAES3RECEIVER,
0156       /* CS8427_REG_CLOCKSOURCE: Run off, CMCK=256*Fs,
0157          output time base = OMCK, input time base = recovered input clock,
0158          recovered input clock source is ILRCK changed to AES3INPUT
0159          (workaround, see snd_cs8427_reset) */
0160       CS8427_RXDILRCK,
0161       /* CS8427_REG_SERIALINPUT: Serial audio input port data format = I2S,
0162          24-bit, 64*Fsi */
0163       CS8427_SIDEL | CS8427_SILRPOL,
0164       /* CS8427_REG_SERIALOUTPUT: Serial audio output port data format
0165          = I2S, 24-bit, 64*Fsi */
0166       CS8427_SODEL | CS8427_SOLRPOL,
0167     };
0168     static unsigned char initvals2[] = {
0169       CS8427_REG_RECVERRMASK | CS8427_REG_AUTOINC,
0170       /* CS8427_REG_RECVERRMASK: unmask the input PLL clock, V, confidence,
0171          biphase, parity status bits */
0172       /* CS8427_UNLOCK | CS8427_V | CS8427_CONF | CS8427_BIP | CS8427_PAR,*/
0173       0xff, /* set everything */
0174       /* CS8427_REG_CSDATABUF:
0175          Registers 32-55 window to CS buffer
0176          Inhibit D->E transfers from overwriting first 5 bytes of CS data.
0177          Inhibit D->E transfers (all) of CS data.
0178          Allow E->F transfer of CS data.
0179          One byte mode; both A/B channels get same written CB data.
0180          A channel info is output to chip's EMPH* pin. */
0181       CS8427_CBMR | CS8427_DETCI,
0182       /* CS8427_REG_UDATABUF:
0183          Use internal buffer to transmit User (U) data.
0184          Chip's U pin is an output.
0185          Transmit all O's for user data.
0186          Inhibit D->E transfers.
0187          Inhibit E->F transfers. */
0188       CS8427_UD | CS8427_EFTUI | CS8427_DETUI,
0189     };
0190     struct cs8427 *chip = device->private_data;
0191     int err;
0192     unsigned char buf[24];
0193 
0194     snd_i2c_lock(bus);
0195     err = snd_cs8427_reg_read(device, CS8427_REG_ID_AND_VER);
0196     if (err != CS8427_VER8427A) {
0197         /* give second chance */
0198         snd_printk(KERN_WARNING "invalid CS8427 signature 0x%x: "
0199                "let me try again...\n", err);
0200         err = snd_cs8427_reg_read(device, CS8427_REG_ID_AND_VER);
0201     }
0202     if (err != CS8427_VER8427A) {
0203         snd_i2c_unlock(bus);
0204         snd_printk(KERN_ERR "unable to find CS8427 signature "
0205                "(expected 0x%x, read 0x%x),\n",
0206                CS8427_VER8427A, err);
0207         snd_printk(KERN_ERR "   initialization is not completed\n");
0208         return -EFAULT;
0209     }
0210     /* turn off run bit while making changes to configuration */
0211     err = snd_cs8427_reg_write(device, CS8427_REG_CLOCKSOURCE, 0x00);
0212     if (err < 0)
0213         goto __fail;
0214     /* send initial values */
0215     memcpy(chip->regmap + (initvals1[0] & 0x7f), initvals1 + 1, 6);
0216     err = snd_i2c_sendbytes(device, initvals1, 7);
0217     if (err != 7) {
0218         err = err < 0 ? err : -EIO;
0219         goto __fail;
0220     }
0221     /* Turn off CS8427 interrupt stuff that is not used in hardware */
0222     memset(buf, 0, 7);
0223     /* from address 9 to 15 */
0224     buf[0] = 9; /* register */
0225     err = snd_i2c_sendbytes(device, buf, 7);
0226     if (err != 7)
0227         goto __fail;
0228     /* send transfer initialization sequence */
0229     memcpy(chip->regmap + (initvals2[0] & 0x7f), initvals2 + 1, 3);
0230     err = snd_i2c_sendbytes(device, initvals2, 4);
0231     if (err != 4) {
0232         err = err < 0 ? err : -EIO;
0233         goto __fail;
0234     }
0235     /* write default channel status bytes */
0236     put_unaligned_le32(SNDRV_PCM_DEFAULT_CON_SPDIF, buf);
0237     memset(buf + 4, 0, 24 - 4);
0238     if (snd_cs8427_send_corudata(device, 0, buf, 24) < 0)
0239         goto __fail;
0240     memcpy(chip->playback.def_status, buf, 24);
0241     memcpy(chip->playback.pcm_status, buf, 24);
0242     snd_i2c_unlock(bus);
0243 
0244     /* turn on run bit and rock'n'roll */
0245     snd_cs8427_reset(device);
0246 
0247     return 0;
0248 
0249 __fail:
0250     snd_i2c_unlock(bus);
0251 
0252     return err;
0253 }
0254 EXPORT_SYMBOL(snd_cs8427_init);
0255 
0256 int snd_cs8427_create(struct snd_i2c_bus *bus,
0257               unsigned char addr,
0258               unsigned int reset_timeout,
0259               struct snd_i2c_device **r_cs8427)
0260 {
0261     int err;
0262     struct cs8427 *chip;
0263     struct snd_i2c_device *device;
0264 
0265     err = snd_i2c_device_create(bus, "CS8427", CS8427_ADDR | (addr & 7),
0266                     &device);
0267     if (err < 0)
0268         return err;
0269     chip = device->private_data = kzalloc(sizeof(*chip), GFP_KERNEL);
0270     if (chip == NULL) {
0271         snd_i2c_device_free(device);
0272         return -ENOMEM;
0273     }
0274     device->private_free = snd_cs8427_free;
0275 
0276     if (reset_timeout < 1)
0277         reset_timeout = 1;
0278     chip->reset_timeout = reset_timeout;
0279 
0280     err = snd_cs8427_init(bus, device);
0281     if (err)
0282         goto __fail;
0283 
0284 #if 0   // it's nice for read tests
0285     {
0286     char buf[128];
0287     int xx;
0288     buf[0] = 0x81;
0289     snd_i2c_sendbytes(device, buf, 1);
0290     snd_i2c_readbytes(device, buf, 127);
0291     for (xx = 0; xx < 127; xx++)
0292         printk(KERN_DEBUG "reg[0x%x] = 0x%x\n", xx+1, buf[xx]);
0293     }
0294 #endif
0295     
0296     if (r_cs8427)
0297         *r_cs8427 = device;
0298     return 0;
0299 
0300       __fail:
0301         snd_i2c_device_free(device);
0302         return err < 0 ? err : -EIO;
0303 }
0304 
0305 EXPORT_SYMBOL(snd_cs8427_create);
0306 
0307 /*
0308  * Reset the chip using run bit, also lock PLL using ILRCK and
0309  * put back AES3INPUT. This workaround is described in latest
0310  * CS8427 datasheet, otherwise TXDSERIAL will not work.
0311  */
0312 static void snd_cs8427_reset(struct snd_i2c_device *cs8427)
0313 {
0314     struct cs8427 *chip;
0315     unsigned long end_time;
0316     int data, aes3input = 0;
0317 
0318     if (snd_BUG_ON(!cs8427))
0319         return;
0320     chip = cs8427->private_data;
0321     snd_i2c_lock(cs8427->bus);
0322     if ((chip->regmap[CS8427_REG_CLOCKSOURCE] & CS8427_RXDAES3INPUT) ==
0323         CS8427_RXDAES3INPUT)  /* AES3 bit is set */
0324         aes3input = 1;
0325     chip->regmap[CS8427_REG_CLOCKSOURCE] &= ~(CS8427_RUN | CS8427_RXDMASK);
0326     snd_cs8427_reg_write(cs8427, CS8427_REG_CLOCKSOURCE,
0327                  chip->regmap[CS8427_REG_CLOCKSOURCE]);
0328     udelay(200);
0329     chip->regmap[CS8427_REG_CLOCKSOURCE] |= CS8427_RUN | CS8427_RXDILRCK;
0330     snd_cs8427_reg_write(cs8427, CS8427_REG_CLOCKSOURCE,
0331                  chip->regmap[CS8427_REG_CLOCKSOURCE]);
0332     udelay(200);
0333     snd_i2c_unlock(cs8427->bus);
0334     end_time = jiffies + chip->reset_timeout;
0335     while (time_after_eq(end_time, jiffies)) {
0336         snd_i2c_lock(cs8427->bus);
0337         data = snd_cs8427_reg_read(cs8427, CS8427_REG_RECVERRORS);
0338         snd_i2c_unlock(cs8427->bus);
0339         if (!(data & CS8427_UNLOCK))
0340             break;
0341         schedule_timeout_uninterruptible(1);
0342     }
0343     snd_i2c_lock(cs8427->bus);
0344     chip->regmap[CS8427_REG_CLOCKSOURCE] &= ~CS8427_RXDMASK;
0345     if (aes3input)
0346         chip->regmap[CS8427_REG_CLOCKSOURCE] |= CS8427_RXDAES3INPUT;
0347     snd_cs8427_reg_write(cs8427, CS8427_REG_CLOCKSOURCE,
0348                  chip->regmap[CS8427_REG_CLOCKSOURCE]);
0349     snd_i2c_unlock(cs8427->bus);
0350 }
0351 
0352 static int snd_cs8427_in_status_info(struct snd_kcontrol *kcontrol,
0353                      struct snd_ctl_elem_info *uinfo)
0354 {
0355     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0356     uinfo->count = 1;
0357     uinfo->value.integer.min = 0;
0358     uinfo->value.integer.max = 255;
0359     return 0;
0360 }
0361 
0362 static int snd_cs8427_in_status_get(struct snd_kcontrol *kcontrol,
0363                     struct snd_ctl_elem_value *ucontrol)
0364 {
0365     struct snd_i2c_device *device = snd_kcontrol_chip(kcontrol);
0366     int data;
0367 
0368     snd_i2c_lock(device->bus);
0369     data = snd_cs8427_reg_read(device, kcontrol->private_value);
0370     snd_i2c_unlock(device->bus);
0371     if (data < 0)
0372         return data;
0373     ucontrol->value.integer.value[0] = data;
0374     return 0;
0375 }
0376 
0377 static int snd_cs8427_qsubcode_info(struct snd_kcontrol *kcontrol,
0378                     struct snd_ctl_elem_info *uinfo)
0379 {
0380     uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
0381     uinfo->count = 10;
0382     return 0;
0383 }
0384 
0385 static int snd_cs8427_qsubcode_get(struct snd_kcontrol *kcontrol,
0386                    struct snd_ctl_elem_value *ucontrol)
0387 {
0388     struct snd_i2c_device *device = snd_kcontrol_chip(kcontrol);
0389     unsigned char reg = CS8427_REG_QSUBCODE;
0390     int err;
0391 
0392     snd_i2c_lock(device->bus);
0393     err = snd_i2c_sendbytes(device, &reg, 1);
0394     if (err != 1) {
0395         snd_printk(KERN_ERR "unable to send register 0x%x byte "
0396                "to CS8427\n", reg);
0397         snd_i2c_unlock(device->bus);
0398         return err < 0 ? err : -EIO;
0399     }
0400     err = snd_i2c_readbytes(device, ucontrol->value.bytes.data, 10);
0401     if (err != 10) {
0402         snd_printk(KERN_ERR "unable to read Q-subcode bytes "
0403                "from CS8427\n");
0404         snd_i2c_unlock(device->bus);
0405         return err < 0 ? err : -EIO;
0406     }
0407     snd_i2c_unlock(device->bus);
0408     return 0;
0409 }
0410 
0411 static int snd_cs8427_spdif_info(struct snd_kcontrol *kcontrol,
0412                  struct snd_ctl_elem_info *uinfo)
0413 {
0414     uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
0415     uinfo->count = 1;
0416     return 0;
0417 }
0418 
0419 static int snd_cs8427_spdif_get(struct snd_kcontrol *kcontrol,
0420                 struct snd_ctl_elem_value *ucontrol)
0421 {
0422     struct snd_i2c_device *device = snd_kcontrol_chip(kcontrol);
0423     struct cs8427 *chip = device->private_data;
0424     
0425     snd_i2c_lock(device->bus);
0426     memcpy(ucontrol->value.iec958.status, chip->playback.def_status, 24);
0427     snd_i2c_unlock(device->bus);
0428     return 0;
0429 }
0430 
0431 static int snd_cs8427_spdif_put(struct snd_kcontrol *kcontrol,
0432                 struct snd_ctl_elem_value *ucontrol)
0433 {
0434     struct snd_i2c_device *device = snd_kcontrol_chip(kcontrol);
0435     struct cs8427 *chip = device->private_data;
0436     unsigned char *status = kcontrol->private_value ?
0437         chip->playback.pcm_status : chip->playback.def_status;
0438     struct snd_pcm_runtime *runtime = chip->playback.substream ?
0439         chip->playback.substream->runtime : NULL;
0440     int err, change;
0441 
0442     snd_i2c_lock(device->bus);
0443     change = memcmp(ucontrol->value.iec958.status, status, 24) != 0;
0444     memcpy(status, ucontrol->value.iec958.status, 24);
0445     if (change && (kcontrol->private_value ?
0446                runtime != NULL : runtime == NULL)) {
0447         err = snd_cs8427_send_corudata(device, 0, status, 24);
0448         if (err < 0)
0449             change = err;
0450     }
0451     snd_i2c_unlock(device->bus);
0452     return change;
0453 }
0454 
0455 static int snd_cs8427_spdif_mask_info(struct snd_kcontrol *kcontrol,
0456                       struct snd_ctl_elem_info *uinfo)
0457 {
0458     uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
0459     uinfo->count = 1;
0460     return 0;
0461 }
0462 
0463 static int snd_cs8427_spdif_mask_get(struct snd_kcontrol *kcontrol,
0464                       struct snd_ctl_elem_value *ucontrol)
0465 {
0466     memset(ucontrol->value.iec958.status, 0xff, 24);
0467     return 0;
0468 }
0469 
0470 static const struct snd_kcontrol_new snd_cs8427_iec958_controls[] = {
0471 {
0472     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
0473     .info =     snd_cs8427_in_status_info,
0474     .name =     "IEC958 CS8427 Input Status",
0475     .access =   (SNDRV_CTL_ELEM_ACCESS_READ |
0476              SNDRV_CTL_ELEM_ACCESS_VOLATILE),
0477     .get =      snd_cs8427_in_status_get,
0478     .private_value = 15,
0479 },
0480 {
0481     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
0482     .info =     snd_cs8427_in_status_info,
0483     .name =     "IEC958 CS8427 Error Status",
0484     .access =   (SNDRV_CTL_ELEM_ACCESS_READ |
0485              SNDRV_CTL_ELEM_ACCESS_VOLATILE),
0486     .get =      snd_cs8427_in_status_get,
0487     .private_value = 16,
0488 },
0489 {
0490     .access =   SNDRV_CTL_ELEM_ACCESS_READ,
0491     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
0492     .name =     SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
0493     .info =     snd_cs8427_spdif_mask_info,
0494     .get =      snd_cs8427_spdif_mask_get,
0495 },
0496 {
0497     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
0498     .name =     SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
0499     .info =     snd_cs8427_spdif_info,
0500     .get =      snd_cs8427_spdif_get,
0501     .put =      snd_cs8427_spdif_put,
0502     .private_value = 0
0503 },
0504 {
0505     .access =   (SNDRV_CTL_ELEM_ACCESS_READWRITE |
0506              SNDRV_CTL_ELEM_ACCESS_INACTIVE),
0507     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
0508     .name =     SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
0509     .info =     snd_cs8427_spdif_info,
0510     .get =      snd_cs8427_spdif_get,
0511     .put =      snd_cs8427_spdif_put,
0512     .private_value = 1
0513 },
0514 {
0515     .iface =    SNDRV_CTL_ELEM_IFACE_PCM,
0516     .info =     snd_cs8427_qsubcode_info,
0517     .name =     "IEC958 Q-subcode Capture Default",
0518     .access =   (SNDRV_CTL_ELEM_ACCESS_READ |
0519              SNDRV_CTL_ELEM_ACCESS_VOLATILE),
0520     .get =      snd_cs8427_qsubcode_get
0521 }};
0522 
0523 int snd_cs8427_iec958_build(struct snd_i2c_device *cs8427,
0524                 struct snd_pcm_substream *play_substream,
0525                 struct snd_pcm_substream *cap_substream)
0526 {
0527     struct cs8427 *chip = cs8427->private_data;
0528     struct snd_kcontrol *kctl;
0529     unsigned int idx;
0530     int err;
0531 
0532     if (snd_BUG_ON(!play_substream || !cap_substream))
0533         return -EINVAL;
0534     for (idx = 0; idx < ARRAY_SIZE(snd_cs8427_iec958_controls); idx++) {
0535         kctl = snd_ctl_new1(&snd_cs8427_iec958_controls[idx], cs8427);
0536         if (kctl == NULL)
0537             return -ENOMEM;
0538         kctl->id.device = play_substream->pcm->device;
0539         kctl->id.subdevice = play_substream->number;
0540         err = snd_ctl_add(cs8427->bus->card, kctl);
0541         if (err < 0)
0542             return err;
0543         if (! strcmp(kctl->id.name,
0544                  SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM)))
0545             chip->playback.pcm_ctl = kctl;
0546     }
0547 
0548     chip->playback.substream = play_substream;
0549     chip->capture.substream = cap_substream;
0550     if (snd_BUG_ON(!chip->playback.pcm_ctl))
0551         return -EIO;
0552     return 0;
0553 }
0554 
0555 EXPORT_SYMBOL(snd_cs8427_iec958_build);
0556 
0557 int snd_cs8427_iec958_active(struct snd_i2c_device *cs8427, int active)
0558 {
0559     struct cs8427 *chip;
0560 
0561     if (snd_BUG_ON(!cs8427))
0562         return -ENXIO;
0563     chip = cs8427->private_data;
0564     if (active)
0565         memcpy(chip->playback.pcm_status,
0566                chip->playback.def_status, 24);
0567     chip->playback.pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
0568     snd_ctl_notify(cs8427->bus->card,
0569                SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
0570                &chip->playback.pcm_ctl->id);
0571     return 0;
0572 }
0573 
0574 EXPORT_SYMBOL(snd_cs8427_iec958_active);
0575 
0576 int snd_cs8427_iec958_pcm(struct snd_i2c_device *cs8427, unsigned int rate)
0577 {
0578     struct cs8427 *chip;
0579     char *status;
0580     int err, reset;
0581 
0582     if (snd_BUG_ON(!cs8427))
0583         return -ENXIO;
0584     chip = cs8427->private_data;
0585     status = chip->playback.pcm_status;
0586     snd_i2c_lock(cs8427->bus);
0587     if (status[0] & IEC958_AES0_PROFESSIONAL) {
0588         status[0] &= ~IEC958_AES0_PRO_FS;
0589         switch (rate) {
0590         case 32000: status[0] |= IEC958_AES0_PRO_FS_32000; break;
0591         case 44100: status[0] |= IEC958_AES0_PRO_FS_44100; break;
0592         case 48000: status[0] |= IEC958_AES0_PRO_FS_48000; break;
0593         default: status[0] |= IEC958_AES0_PRO_FS_NOTID; break;
0594         }
0595     } else {
0596         status[3] &= ~IEC958_AES3_CON_FS;
0597         switch (rate) {
0598         case 32000: status[3] |= IEC958_AES3_CON_FS_32000; break;
0599         case 44100: status[3] |= IEC958_AES3_CON_FS_44100; break;
0600         case 48000: status[3] |= IEC958_AES3_CON_FS_48000; break;
0601         }
0602     }
0603     err = snd_cs8427_send_corudata(cs8427, 0, status, 24);
0604     if (err > 0)
0605         snd_ctl_notify(cs8427->bus->card,
0606                    SNDRV_CTL_EVENT_MASK_VALUE,
0607                    &chip->playback.pcm_ctl->id);
0608     reset = chip->rate != rate;
0609     chip->rate = rate;
0610     snd_i2c_unlock(cs8427->bus);
0611     if (reset)
0612         snd_cs8427_reset(cs8427);
0613     return err < 0 ? err : 0;
0614 }
0615 
0616 EXPORT_SYMBOL(snd_cs8427_iec958_pcm);