Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // siu_dai.c - ALSA SoC driver for Renesas SH7343, SH7722 SIU peripheral.
0004 //
0005 // Copyright (C) 2009-2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
0006 // Copyright (C) 2006 Carlos Munoz <carlos@kenati.com>
0007 
0008 #include <linux/delay.h>
0009 #include <linux/firmware.h>
0010 #include <linux/pm_runtime.h>
0011 #include <linux/slab.h>
0012 #include <linux/module.h>
0013 
0014 #include <asm/clock.h>
0015 #include <asm/siu.h>
0016 
0017 #include <sound/control.h>
0018 #include <sound/soc.h>
0019 
0020 #include "siu.h"
0021 
0022 /* Board specifics */
0023 #if defined(CONFIG_CPU_SUBTYPE_SH7722)
0024 # define SIU_MAX_VOLUME     0x1000
0025 #else
0026 # define SIU_MAX_VOLUME     0x7fff
0027 #endif
0028 
0029 #define PRAM_SIZE   0x2000
0030 #define XRAM_SIZE   0x800
0031 #define YRAM_SIZE   0x800
0032 
0033 #define XRAM_OFFSET 0x4000
0034 #define YRAM_OFFSET 0x6000
0035 #define REG_OFFSET  0xc000
0036 
0037 #define PLAYBACK_ENABLED    1
0038 #define CAPTURE_ENABLED     2
0039 
0040 #define VOLUME_CAPTURE      0
0041 #define VOLUME_PLAYBACK     1
0042 #define DFLT_VOLUME_LEVEL   0x08000800
0043 
0044 /*
0045  * SPDIF is only available on port A and on some SIU implementations it is only
0046  * available for input. Due to the lack of hardware to test it, SPDIF is left
0047  * disabled in this driver version
0048  */
0049 struct format_flag {
0050     u32 i2s;
0051     u32 pcm;
0052     u32 spdif;
0053     u32 mask;
0054 };
0055 
0056 struct port_flag {
0057     struct format_flag  playback;
0058     struct format_flag  capture;
0059 };
0060 
0061 struct siu_info *siu_i2s_data;
0062 
0063 static struct port_flag siu_flags[SIU_PORT_NUM] = {
0064     [SIU_PORT_A] = {
0065         .playback = {
0066             .i2s    = 0x50000000,
0067             .pcm    = 0x40000000,
0068             .spdif  = 0x80000000,   /* not on all SIU versions */
0069             .mask   = 0xd0000000,
0070         },
0071         .capture = {
0072             .i2s    = 0x05000000,
0073             .pcm    = 0x04000000,
0074             .spdif  = 0x08000000,
0075             .mask   = 0x0d000000,
0076         },
0077     },
0078     [SIU_PORT_B] = {
0079         .playback = {
0080             .i2s    = 0x00500000,
0081             .pcm    = 0x00400000,
0082             .spdif  = 0,        /* impossible - turn off */
0083             .mask   = 0x00500000,
0084         },
0085         .capture = {
0086             .i2s    = 0x00050000,
0087             .pcm    = 0x00040000,
0088             .spdif  = 0,        /* impossible - turn off */
0089             .mask   = 0x00050000,
0090         },
0091     },
0092 };
0093 
0094 static void siu_dai_start(struct siu_port *port_info)
0095 {
0096     struct siu_info *info = siu_i2s_data;
0097     u32 __iomem *base = info->reg;
0098 
0099     dev_dbg(port_info->pcm->card->dev, "%s\n", __func__);
0100 
0101     /* Issue software reset to siu */
0102     siu_write32(base + SIU_SRCTL, 0);
0103 
0104     /* Wait for the reset to take effect */
0105     udelay(1);
0106 
0107     port_info->stfifo = 0;
0108     port_info->trdat = 0;
0109 
0110     /* portA, portB, SIU operate */
0111     siu_write32(base + SIU_SRCTL, 0x301);
0112 
0113     /* portA=256fs, portB=256fs */
0114     siu_write32(base + SIU_CKCTL, 0x40400000);
0115 
0116     /* portA's BRG does not divide SIUCKA */
0117     siu_write32(base + SIU_BRGASEL, 0);
0118     siu_write32(base + SIU_BRRA, 0);
0119 
0120     /* portB's BRG divides SIUCKB by half */
0121     siu_write32(base + SIU_BRGBSEL, 1);
0122     siu_write32(base + SIU_BRRB, 0);
0123 
0124     siu_write32(base + SIU_IFCTL, 0x44440000);
0125 
0126     /* portA: 32 bit/fs, master; portB: 32 bit/fs, master */
0127     siu_write32(base + SIU_SFORM, 0x0c0c0000);
0128 
0129     /*
0130      * Volume levels: looks like the DSP firmware implements volume controls
0131      * differently from what's described in the datasheet
0132      */
0133     siu_write32(base + SIU_SBDVCA, port_info->playback.volume);
0134     siu_write32(base + SIU_SBDVCB, port_info->capture.volume);
0135 }
0136 
0137 static void siu_dai_stop(struct siu_port *port_info)
0138 {
0139     struct siu_info *info = siu_i2s_data;
0140     u32 __iomem *base = info->reg;
0141 
0142     /* SIU software reset */
0143     siu_write32(base + SIU_SRCTL, 0);
0144 }
0145 
0146 static void siu_dai_spbAselect(struct siu_port *port_info)
0147 {
0148     struct siu_info *info = siu_i2s_data;
0149     struct siu_firmware *fw = &info->fw;
0150     u32 *ydef = fw->yram0;
0151     u32 idx;
0152 
0153     /* path A use */
0154     if (!info->port_id)
0155         idx = 1;        /* portA */
0156     else
0157         idx = 2;        /* portB */
0158 
0159     ydef[0] = (fw->spbpar[idx].ab1a << 16) |
0160         (fw->spbpar[idx].ab0a << 8) |
0161         (fw->spbpar[idx].dir << 7) | 3;
0162     ydef[1] = fw->yram0[1]; /* 0x03000300 */
0163     ydef[2] = (16 / 2) << 24;
0164     ydef[3] = fw->yram0[3]; /* 0 */
0165     ydef[4] = fw->yram0[4]; /* 0 */
0166     ydef[7] = fw->spbpar[idx].event;
0167     port_info->stfifo |= fw->spbpar[idx].stfifo;
0168     port_info->trdat |= fw->spbpar[idx].trdat;
0169 }
0170 
0171 static void siu_dai_spbBselect(struct siu_port *port_info)
0172 {
0173     struct siu_info *info = siu_i2s_data;
0174     struct siu_firmware *fw = &info->fw;
0175     u32 *ydef = fw->yram0;
0176     u32 idx;
0177 
0178     /* path B use */
0179     if (!info->port_id)
0180         idx = 7;        /* portA */
0181     else
0182         idx = 8;        /* portB */
0183 
0184     ydef[5] = (fw->spbpar[idx].ab1a << 16) |
0185         (fw->spbpar[idx].ab0a << 8) | 1;
0186     ydef[6] = fw->spbpar[idx].event;
0187     port_info->stfifo |= fw->spbpar[idx].stfifo;
0188     port_info->trdat |= fw->spbpar[idx].trdat;
0189 }
0190 
0191 static void siu_dai_open(struct siu_stream *siu_stream)
0192 {
0193     struct siu_info *info = siu_i2s_data;
0194     u32 __iomem *base = info->reg;
0195     u32 srctl, ifctl;
0196 
0197     srctl = siu_read32(base + SIU_SRCTL);
0198     ifctl = siu_read32(base + SIU_IFCTL);
0199 
0200     switch (info->port_id) {
0201     case SIU_PORT_A:
0202         /* portA operates */
0203         srctl |= 0x200;
0204         ifctl &= ~0xc2;
0205         break;
0206     case SIU_PORT_B:
0207         /* portB operates */
0208         srctl |= 0x100;
0209         ifctl &= ~0x31;
0210         break;
0211     }
0212 
0213     siu_write32(base + SIU_SRCTL, srctl);
0214     /* Unmute and configure portA */
0215     siu_write32(base + SIU_IFCTL, ifctl);
0216 }
0217 
0218 /*
0219  * At the moment only fixed Left-upper, Left-lower, Right-upper, Right-lower
0220  * packing is supported
0221  */
0222 static void siu_dai_pcmdatapack(struct siu_stream *siu_stream)
0223 {
0224     struct siu_info *info = siu_i2s_data;
0225     u32 __iomem *base = info->reg;
0226     u32 dpak;
0227 
0228     dpak = siu_read32(base + SIU_DPAK);
0229 
0230     switch (info->port_id) {
0231     case SIU_PORT_A:
0232         dpak &= ~0xc0000000;
0233         break;
0234     case SIU_PORT_B:
0235         dpak &= ~0x00c00000;
0236         break;
0237     }
0238 
0239     siu_write32(base + SIU_DPAK, dpak);
0240 }
0241 
0242 static int siu_dai_spbstart(struct siu_port *port_info)
0243 {
0244     struct siu_info *info = siu_i2s_data;
0245     u32 __iomem *base = info->reg;
0246     struct siu_firmware *fw = &info->fw;
0247     u32 *ydef = fw->yram0;
0248     int cnt;
0249     u32 __iomem *add;
0250     u32 *ptr;
0251 
0252     /* Load SPB Program in PRAM */
0253     ptr = fw->pram0;
0254     add = info->pram;
0255     for (cnt = 0; cnt < PRAM0_SIZE; cnt++, add++, ptr++)
0256         siu_write32(add, *ptr);
0257 
0258     ptr = fw->pram1;
0259     add = info->pram + (0x0100 / sizeof(u32));
0260     for (cnt = 0; cnt < PRAM1_SIZE; cnt++, add++, ptr++)
0261         siu_write32(add, *ptr);
0262 
0263     /* XRAM initialization */
0264     add = info->xram;
0265     for (cnt = 0; cnt < XRAM0_SIZE + XRAM1_SIZE + XRAM2_SIZE; cnt++, add++)
0266         siu_write32(add, 0);
0267 
0268     /* YRAM variable area initialization */
0269     add = info->yram;
0270     for (cnt = 0; cnt < YRAM_DEF_SIZE; cnt++, add++)
0271         siu_write32(add, ydef[cnt]);
0272 
0273     /* YRAM FIR coefficient area initialization */
0274     add = info->yram + (0x0200 / sizeof(u32));
0275     for (cnt = 0; cnt < YRAM_FIR_SIZE; cnt++, add++)
0276         siu_write32(add, fw->yram_fir_coeff[cnt]);
0277 
0278     /* YRAM IIR coefficient area initialization */
0279     add = info->yram + (0x0600 / sizeof(u32));
0280     for (cnt = 0; cnt < YRAM_IIR_SIZE; cnt++, add++)
0281         siu_write32(add, 0);
0282 
0283     siu_write32(base + SIU_TRDAT, port_info->trdat);
0284     port_info->trdat = 0x0;
0285 
0286 
0287     /* SPB start condition: software */
0288     siu_write32(base + SIU_SBACTIV, 0);
0289     /* Start SPB */
0290     siu_write32(base + SIU_SBCTL, 0xc0000000);
0291     /* Wait for program to halt */
0292     cnt = 0x10000;
0293     while (--cnt && siu_read32(base + SIU_SBCTL) != 0x80000000)
0294         cpu_relax();
0295 
0296     if (!cnt)
0297         return -EBUSY;
0298 
0299     /* SPB program start address setting */
0300     siu_write32(base + SIU_SBPSET, 0x00400000);
0301     /* SPB hardware start(FIFOCTL source) */
0302     siu_write32(base + SIU_SBACTIV, 0xc0000000);
0303 
0304     return 0;
0305 }
0306 
0307 static void siu_dai_spbstop(struct siu_port *port_info)
0308 {
0309     struct siu_info *info = siu_i2s_data;
0310     u32 __iomem *base = info->reg;
0311 
0312     siu_write32(base + SIU_SBACTIV, 0);
0313     /* SPB stop */
0314     siu_write32(base + SIU_SBCTL, 0);
0315 
0316     port_info->stfifo = 0;
0317 }
0318 
0319 /*      API functions       */
0320 
0321 /* Playback and capture hardware properties are identical */
0322 static const struct snd_pcm_hardware siu_dai_pcm_hw = {
0323     .info           = SNDRV_PCM_INFO_INTERLEAVED,
0324     .formats        = SNDRV_PCM_FMTBIT_S16,
0325     .rates          = SNDRV_PCM_RATE_8000_48000,
0326     .rate_min       = 8000,
0327     .rate_max       = 48000,
0328     .channels_min       = 2,
0329     .channels_max       = 2,
0330     .buffer_bytes_max   = SIU_BUFFER_BYTES_MAX,
0331     .period_bytes_min   = SIU_PERIOD_BYTES_MIN,
0332     .period_bytes_max   = SIU_PERIOD_BYTES_MAX,
0333     .periods_min        = SIU_PERIODS_MIN,
0334     .periods_max        = SIU_PERIODS_MAX,
0335 };
0336 
0337 static int siu_dai_info_volume(struct snd_kcontrol *kctrl,
0338                    struct snd_ctl_elem_info *uinfo)
0339 {
0340     struct siu_port *port_info = snd_kcontrol_chip(kctrl);
0341 
0342     dev_dbg(port_info->pcm->card->dev, "%s\n", __func__);
0343 
0344     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0345     uinfo->count = 2;
0346     uinfo->value.integer.min = 0;
0347     uinfo->value.integer.max = SIU_MAX_VOLUME;
0348 
0349     return 0;
0350 }
0351 
0352 static int siu_dai_get_volume(struct snd_kcontrol *kctrl,
0353                   struct snd_ctl_elem_value *ucontrol)
0354 {
0355     struct siu_port *port_info = snd_kcontrol_chip(kctrl);
0356     struct device *dev = port_info->pcm->card->dev;
0357     u32 vol;
0358 
0359     dev_dbg(dev, "%s\n", __func__);
0360 
0361     switch (kctrl->private_value) {
0362     case VOLUME_PLAYBACK:
0363         /* Playback is always on port 0 */
0364         vol = port_info->playback.volume;
0365         ucontrol->value.integer.value[0] = vol & 0xffff;
0366         ucontrol->value.integer.value[1] = vol >> 16 & 0xffff;
0367         break;
0368     case VOLUME_CAPTURE:
0369         /* Capture is always on port 1 */
0370         vol = port_info->capture.volume;
0371         ucontrol->value.integer.value[0] = vol & 0xffff;
0372         ucontrol->value.integer.value[1] = vol >> 16 & 0xffff;
0373         break;
0374     default:
0375         dev_err(dev, "%s() invalid private_value=%ld\n",
0376             __func__, kctrl->private_value);
0377         return -EINVAL;
0378     }
0379 
0380     return 0;
0381 }
0382 
0383 static int siu_dai_put_volume(struct snd_kcontrol *kctrl,
0384                   struct snd_ctl_elem_value *ucontrol)
0385 {
0386     struct siu_port *port_info = snd_kcontrol_chip(kctrl);
0387     struct device *dev = port_info->pcm->card->dev;
0388     struct siu_info *info = siu_i2s_data;
0389     u32 __iomem *base = info->reg;
0390     u32 new_vol;
0391     u32 cur_vol;
0392 
0393     dev_dbg(dev, "%s\n", __func__);
0394 
0395     if (ucontrol->value.integer.value[0] < 0 ||
0396         ucontrol->value.integer.value[0] > SIU_MAX_VOLUME ||
0397         ucontrol->value.integer.value[1] < 0 ||
0398         ucontrol->value.integer.value[1] > SIU_MAX_VOLUME)
0399         return -EINVAL;
0400 
0401     new_vol = ucontrol->value.integer.value[0] |
0402         ucontrol->value.integer.value[1] << 16;
0403 
0404     /* See comment above - DSP firmware implementation */
0405     switch (kctrl->private_value) {
0406     case VOLUME_PLAYBACK:
0407         /* Playback is always on port 0 */
0408         cur_vol = port_info->playback.volume;
0409         siu_write32(base + SIU_SBDVCA, new_vol);
0410         port_info->playback.volume = new_vol;
0411         break;
0412     case VOLUME_CAPTURE:
0413         /* Capture is always on port 1 */
0414         cur_vol = port_info->capture.volume;
0415         siu_write32(base + SIU_SBDVCB, new_vol);
0416         port_info->capture.volume = new_vol;
0417         break;
0418     default:
0419         dev_err(dev, "%s() invalid private_value=%ld\n",
0420             __func__, kctrl->private_value);
0421         return -EINVAL;
0422     }
0423 
0424     if (cur_vol != new_vol)
0425         return 1;
0426 
0427     return 0;
0428 }
0429 
0430 static const struct snd_kcontrol_new playback_controls = {
0431     .iface      = SNDRV_CTL_ELEM_IFACE_MIXER,
0432     .name       = "PCM Playback Volume",
0433     .index      = 0,
0434     .info       = siu_dai_info_volume,
0435     .get        = siu_dai_get_volume,
0436     .put        = siu_dai_put_volume,
0437     .private_value  = VOLUME_PLAYBACK,
0438 };
0439 
0440 static const struct snd_kcontrol_new capture_controls = {
0441     .iface      = SNDRV_CTL_ELEM_IFACE_MIXER,
0442     .name       = "PCM Capture Volume",
0443     .index      = 0,
0444     .info       = siu_dai_info_volume,
0445     .get        = siu_dai_get_volume,
0446     .put        = siu_dai_put_volume,
0447     .private_value  = VOLUME_CAPTURE,
0448 };
0449 
0450 int siu_init_port(int port, struct siu_port **port_info, struct snd_card *card)
0451 {
0452     struct device *dev = card->dev;
0453     struct snd_kcontrol *kctrl;
0454     int ret;
0455 
0456     *port_info = kzalloc(sizeof(**port_info), GFP_KERNEL);
0457     if (!*port_info)
0458         return -ENOMEM;
0459 
0460     dev_dbg(dev, "%s: port #%d@%p\n", __func__, port, *port_info);
0461 
0462     (*port_info)->playback.volume = DFLT_VOLUME_LEVEL;
0463     (*port_info)->capture.volume = DFLT_VOLUME_LEVEL;
0464 
0465     /*
0466      * Add mixer support. The SPB is used to change the volume. Both
0467      * ports use the same SPB. Therefore, we only register one
0468      * control instance since it will be used by both channels.
0469      * In error case we continue without controls.
0470      */
0471     kctrl = snd_ctl_new1(&playback_controls, *port_info);
0472     ret = snd_ctl_add(card, kctrl);
0473     if (ret < 0)
0474         dev_err(dev,
0475             "failed to add playback controls %p port=%d err=%d\n",
0476             kctrl, port, ret);
0477 
0478     kctrl = snd_ctl_new1(&capture_controls, *port_info);
0479     ret = snd_ctl_add(card, kctrl);
0480     if (ret < 0)
0481         dev_err(dev,
0482             "failed to add capture controls %p port=%d err=%d\n",
0483             kctrl, port, ret);
0484 
0485     return 0;
0486 }
0487 
0488 void siu_free_port(struct siu_port *port_info)
0489 {
0490     kfree(port_info);
0491 }
0492 
0493 static int siu_dai_startup(struct snd_pcm_substream *substream,
0494                struct snd_soc_dai *dai)
0495 {
0496     struct siu_info *info = snd_soc_dai_get_drvdata(dai);
0497     struct snd_pcm_runtime *rt = substream->runtime;
0498     struct siu_port *port_info = siu_port_info(substream);
0499     int ret;
0500 
0501     dev_dbg(substream->pcm->card->dev, "%s: port=%d@%p\n", __func__,
0502         info->port_id, port_info);
0503 
0504     snd_soc_set_runtime_hwparams(substream, &siu_dai_pcm_hw);
0505 
0506     ret = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
0507     if (unlikely(ret < 0))
0508         return ret;
0509 
0510     siu_dai_start(port_info);
0511 
0512     return 0;
0513 }
0514 
0515 static void siu_dai_shutdown(struct snd_pcm_substream *substream,
0516                  struct snd_soc_dai *dai)
0517 {
0518     struct siu_info *info = snd_soc_dai_get_drvdata(dai);
0519     struct siu_port *port_info = siu_port_info(substream);
0520 
0521     dev_dbg(substream->pcm->card->dev, "%s: port=%d@%p\n", __func__,
0522         info->port_id, port_info);
0523 
0524     if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
0525         port_info->play_cap &= ~PLAYBACK_ENABLED;
0526     else
0527         port_info->play_cap &= ~CAPTURE_ENABLED;
0528 
0529     /* Stop the siu if the other stream is not using it */
0530     if (!port_info->play_cap) {
0531         /* during stmread or stmwrite ? */
0532         if (WARN_ON(port_info->playback.rw_flg || port_info->capture.rw_flg))
0533             return;
0534         siu_dai_spbstop(port_info);
0535         siu_dai_stop(port_info);
0536     }
0537 }
0538 
0539 /* PCM part of siu_dai_playback_prepare() / siu_dai_capture_prepare() */
0540 static int siu_dai_prepare(struct snd_pcm_substream *substream,
0541                struct snd_soc_dai *dai)
0542 {
0543     struct siu_info *info = snd_soc_dai_get_drvdata(dai);
0544     struct snd_pcm_runtime *rt = substream->runtime;
0545     struct siu_port *port_info = siu_port_info(substream);
0546     struct siu_stream *siu_stream;
0547     int self, ret;
0548 
0549     dev_dbg(substream->pcm->card->dev,
0550         "%s: port %d, active streams %lx, %d channels\n",
0551         __func__, info->port_id, port_info->play_cap, rt->channels);
0552 
0553     if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
0554         self = PLAYBACK_ENABLED;
0555         siu_stream = &port_info->playback;
0556     } else {
0557         self = CAPTURE_ENABLED;
0558         siu_stream = &port_info->capture;
0559     }
0560 
0561     /* Set up the siu if not already done */
0562     if (!port_info->play_cap) {
0563         siu_stream->rw_flg = 0; /* stream-data transfer flag */
0564 
0565         siu_dai_spbAselect(port_info);
0566         siu_dai_spbBselect(port_info);
0567 
0568         siu_dai_open(siu_stream);
0569 
0570         siu_dai_pcmdatapack(siu_stream);
0571 
0572         ret = siu_dai_spbstart(port_info);
0573         if (ret < 0)
0574             goto fail;
0575     } else {
0576         ret = 0;
0577     }
0578 
0579     port_info->play_cap |= self;
0580 
0581 fail:
0582     return ret;
0583 }
0584 
0585 /*
0586  * SIU can set bus format to I2S / PCM / SPDIF independently for playback and
0587  * capture, however, the current API sets the bus format globally for a DAI.
0588  */
0589 static int siu_dai_set_fmt(struct snd_soc_dai *dai,
0590                unsigned int fmt)
0591 {
0592     struct siu_info *info = snd_soc_dai_get_drvdata(dai);
0593     u32 __iomem *base = info->reg;
0594     u32 ifctl;
0595 
0596     dev_dbg(dai->dev, "%s: fmt 0x%x on port %d\n",
0597         __func__, fmt, info->port_id);
0598 
0599     if (info->port_id < 0)
0600         return -ENODEV;
0601 
0602     /* Here select between I2S / PCM / SPDIF */
0603     switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
0604     case SND_SOC_DAIFMT_I2S:
0605         ifctl = siu_flags[info->port_id].playback.i2s |
0606             siu_flags[info->port_id].capture.i2s;
0607         break;
0608     case SND_SOC_DAIFMT_LEFT_J:
0609         ifctl = siu_flags[info->port_id].playback.pcm |
0610             siu_flags[info->port_id].capture.pcm;
0611         break;
0612     /* SPDIF disabled - see comment at the top */
0613     default:
0614         return -EINVAL;
0615     }
0616 
0617     ifctl |= ~(siu_flags[info->port_id].playback.mask |
0618            siu_flags[info->port_id].capture.mask) &
0619         siu_read32(base + SIU_IFCTL);
0620     siu_write32(base + SIU_IFCTL, ifctl);
0621 
0622     return 0;
0623 }
0624 
0625 static int siu_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
0626                   unsigned int freq, int dir)
0627 {
0628     struct clk *siu_clk, *parent_clk;
0629     char *siu_name, *parent_name;
0630     int ret;
0631 
0632     if (dir != SND_SOC_CLOCK_IN)
0633         return -EINVAL;
0634 
0635     dev_dbg(dai->dev, "%s: using clock %d\n", __func__, clk_id);
0636 
0637     switch (clk_id) {
0638     case SIU_CLKA_PLL:
0639         siu_name = "siua_clk";
0640         parent_name = "pll_clk";
0641         break;
0642     case SIU_CLKA_EXT:
0643         siu_name = "siua_clk";
0644         parent_name = "siumcka_clk";
0645         break;
0646     case SIU_CLKB_PLL:
0647         siu_name = "siub_clk";
0648         parent_name = "pll_clk";
0649         break;
0650     case SIU_CLKB_EXT:
0651         siu_name = "siub_clk";
0652         parent_name = "siumckb_clk";
0653         break;
0654     default:
0655         return -EINVAL;
0656     }
0657 
0658     siu_clk = clk_get(dai->dev, siu_name);
0659     if (IS_ERR(siu_clk)) {
0660         dev_err(dai->dev, "%s: cannot get a SIU clock: %ld\n", __func__,
0661             PTR_ERR(siu_clk));
0662         return PTR_ERR(siu_clk);
0663     }
0664 
0665     parent_clk = clk_get(dai->dev, parent_name);
0666     if (IS_ERR(parent_clk)) {
0667         ret = PTR_ERR(parent_clk);
0668         dev_err(dai->dev, "cannot get a SIU clock parent: %d\n", ret);
0669         goto epclkget;
0670     }
0671 
0672     ret = clk_set_parent(siu_clk, parent_clk);
0673     if (ret < 0) {
0674         dev_err(dai->dev, "cannot reparent the SIU clock: %d\n", ret);
0675         goto eclksetp;
0676     }
0677 
0678     ret = clk_set_rate(siu_clk, freq);
0679     if (ret < 0)
0680         dev_err(dai->dev, "cannot set SIU clock rate: %d\n", ret);
0681 
0682     /* TODO: when clkdev gets reference counting we'll move these to siu_dai_shutdown() */
0683 eclksetp:
0684     clk_put(parent_clk);
0685 epclkget:
0686     clk_put(siu_clk);
0687 
0688     return ret;
0689 }
0690 
0691 static const struct snd_soc_dai_ops siu_dai_ops = {
0692     .startup    = siu_dai_startup,
0693     .shutdown   = siu_dai_shutdown,
0694     .prepare    = siu_dai_prepare,
0695     .set_sysclk = siu_dai_set_sysclk,
0696     .set_fmt    = siu_dai_set_fmt,
0697 };
0698 
0699 static struct snd_soc_dai_driver siu_i2s_dai = {
0700     .name   = "siu-i2s-dai",
0701     .playback = {
0702         .channels_min = 2,
0703         .channels_max = 2,
0704         .formats = SNDRV_PCM_FMTBIT_S16,
0705         .rates = SNDRV_PCM_RATE_8000_48000,
0706     },
0707     .capture = {
0708         .channels_min = 2,
0709         .channels_max = 2,
0710         .formats = SNDRV_PCM_FMTBIT_S16,
0711         .rates = SNDRV_PCM_RATE_8000_48000,
0712      },
0713     .ops = &siu_dai_ops,
0714 };
0715 
0716 static int siu_probe(struct platform_device *pdev)
0717 {
0718     const struct firmware *fw_entry;
0719     struct resource *res, *region;
0720     struct siu_info *info;
0721     int ret;
0722 
0723     info = devm_kmalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
0724     if (!info)
0725         return -ENOMEM;
0726     siu_i2s_data = info;
0727     info->dev = &pdev->dev;
0728 
0729     ret = request_firmware(&fw_entry, "siu_spb.bin", &pdev->dev);
0730     if (ret)
0731         return ret;
0732 
0733     /*
0734      * Loaded firmware is "const" - read only, but we have to modify it in
0735      * snd_siu_sh7343_spbAselect() and snd_siu_sh7343_spbBselect()
0736      */
0737     memcpy(&info->fw, fw_entry->data, fw_entry->size);
0738 
0739     release_firmware(fw_entry);
0740 
0741     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0742     if (!res)
0743         return -ENODEV;
0744 
0745     region = devm_request_mem_region(&pdev->dev, res->start,
0746                      resource_size(res), pdev->name);
0747     if (!region) {
0748         dev_err(&pdev->dev, "SIU region already claimed\n");
0749         return -EBUSY;
0750     }
0751 
0752     info->pram = devm_ioremap(&pdev->dev, res->start, PRAM_SIZE);
0753     if (!info->pram)
0754         return -ENOMEM;
0755     info->xram = devm_ioremap(&pdev->dev, res->start + XRAM_OFFSET,
0756                   XRAM_SIZE);
0757     if (!info->xram)
0758         return -ENOMEM;
0759     info->yram = devm_ioremap(&pdev->dev, res->start + YRAM_OFFSET,
0760                   YRAM_SIZE);
0761     if (!info->yram)
0762         return -ENOMEM;
0763     info->reg = devm_ioremap(&pdev->dev, res->start + REG_OFFSET,
0764                 resource_size(res) - REG_OFFSET);
0765     if (!info->reg)
0766         return -ENOMEM;
0767 
0768     dev_set_drvdata(&pdev->dev, info);
0769 
0770     /* register using ARRAY version so we can keep dai name */
0771     ret = devm_snd_soc_register_component(&pdev->dev, &siu_component,
0772                           &siu_i2s_dai, 1);
0773     if (ret < 0)
0774         return ret;
0775 
0776     pm_runtime_enable(&pdev->dev);
0777 
0778     return 0;
0779 }
0780 
0781 static int siu_remove(struct platform_device *pdev)
0782 {
0783     pm_runtime_disable(&pdev->dev);
0784     return 0;
0785 }
0786 
0787 static struct platform_driver siu_driver = {
0788     .driver     = {
0789         .name   = "siu-pcm-audio",
0790     },
0791     .probe      = siu_probe,
0792     .remove     = siu_remove,
0793 };
0794 
0795 module_platform_driver(siu_driver);
0796 
0797 MODULE_AUTHOR("Carlos Munoz <carlos@kenati.com>");
0798 MODULE_DESCRIPTION("ALSA SoC SH7722 SIU driver");
0799 MODULE_LICENSE("GPL");