Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * linux/sound/pxa2xx-ac97.c -- AC97 support for the Intel PXA2xx chip.
0004  *
0005  * Author:  Nicolas Pitre
0006  * Created: Dec 02, 2004
0007  * Copyright:   MontaVista Software Inc.
0008  */
0009 
0010 #include <linux/init.h>
0011 #include <linux/io.h>
0012 #include <linux/module.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/dmaengine.h>
0015 #include <linux/dma-mapping.h>
0016 
0017 #include <sound/core.h>
0018 #include <sound/pcm.h>
0019 #include <sound/ac97_codec.h>
0020 #include <sound/initval.h>
0021 #include <sound/pxa2xx-lib.h>
0022 #include <sound/dmaengine_pcm.h>
0023 
0024 #include <linux/platform_data/asoc-pxa.h>
0025 
0026 static void pxa2xx_ac97_legacy_reset(struct snd_ac97 *ac97)
0027 {
0028     if (!pxa2xx_ac97_try_cold_reset())
0029         pxa2xx_ac97_try_warm_reset();
0030 
0031     pxa2xx_ac97_finish_reset();
0032 }
0033 
0034 static unsigned short pxa2xx_ac97_legacy_read(struct snd_ac97 *ac97,
0035                           unsigned short reg)
0036 {
0037     int ret;
0038 
0039     ret = pxa2xx_ac97_read(ac97->num, reg);
0040     if (ret < 0)
0041         return 0;
0042     else
0043         return (unsigned short)(ret & 0xffff);
0044 }
0045 
0046 static void pxa2xx_ac97_legacy_write(struct snd_ac97 *ac97,
0047                      unsigned short reg, unsigned short val)
0048 {
0049     pxa2xx_ac97_write(ac97->num, reg, val);
0050 }
0051 
0052 static const struct snd_ac97_bus_ops pxa2xx_ac97_ops = {
0053     .read   = pxa2xx_ac97_legacy_read,
0054     .write  = pxa2xx_ac97_legacy_write,
0055     .reset  = pxa2xx_ac97_legacy_reset,
0056 };
0057 
0058 static struct snd_pcm *pxa2xx_ac97_pcm;
0059 static struct snd_ac97 *pxa2xx_ac97_ac97;
0060 
0061 static int pxa2xx_ac97_pcm_open(struct snd_pcm_substream *substream)
0062 {
0063     struct snd_pcm_runtime *runtime = substream->runtime;
0064     pxa2xx_audio_ops_t *platform_ops;
0065     int ret, i;
0066 
0067     ret = pxa2xx_pcm_open(substream);
0068     if (ret)
0069         return ret;
0070 
0071     runtime->hw.channels_min = 2;
0072     runtime->hw.channels_max = 2;
0073 
0074     i = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
0075         AC97_RATES_FRONT_DAC : AC97_RATES_ADC;
0076     runtime->hw.rates = pxa2xx_ac97_ac97->rates[i];
0077     snd_pcm_limit_hw_rates(runtime);
0078 
0079     platform_ops = substream->pcm->card->dev->platform_data;
0080     if (platform_ops && platform_ops->startup) {
0081         ret = platform_ops->startup(substream, platform_ops->priv);
0082         if (ret < 0)
0083             pxa2xx_pcm_close(substream);
0084     }
0085 
0086     return ret;
0087 }
0088 
0089 static int pxa2xx_ac97_pcm_close(struct snd_pcm_substream *substream)
0090 {
0091     pxa2xx_audio_ops_t *platform_ops;
0092 
0093     platform_ops = substream->pcm->card->dev->platform_data;
0094     if (platform_ops && platform_ops->shutdown)
0095         platform_ops->shutdown(substream, platform_ops->priv);
0096 
0097     return 0;
0098 }
0099 
0100 static int pxa2xx_ac97_pcm_prepare(struct snd_pcm_substream *substream)
0101 {
0102     struct snd_pcm_runtime *runtime = substream->runtime;
0103     int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
0104           AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
0105     int ret;
0106 
0107     ret = pxa2xx_pcm_prepare(substream);
0108     if (ret < 0)
0109         return ret;
0110 
0111     return snd_ac97_set_rate(pxa2xx_ac97_ac97, reg, runtime->rate);
0112 }
0113 
0114 #ifdef CONFIG_PM_SLEEP
0115 
0116 static int pxa2xx_ac97_do_suspend(struct snd_card *card)
0117 {
0118     pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
0119 
0120     snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
0121     snd_ac97_suspend(pxa2xx_ac97_ac97);
0122     if (platform_ops && platform_ops->suspend)
0123         platform_ops->suspend(platform_ops->priv);
0124 
0125     return pxa2xx_ac97_hw_suspend();
0126 }
0127 
0128 static int pxa2xx_ac97_do_resume(struct snd_card *card)
0129 {
0130     pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
0131     int rc;
0132 
0133     rc = pxa2xx_ac97_hw_resume();
0134     if (rc)
0135         return rc;
0136 
0137     if (platform_ops && platform_ops->resume)
0138         platform_ops->resume(platform_ops->priv);
0139     snd_ac97_resume(pxa2xx_ac97_ac97);
0140     snd_power_change_state(card, SNDRV_CTL_POWER_D0);
0141 
0142     return 0;
0143 }
0144 
0145 static int pxa2xx_ac97_suspend(struct device *dev)
0146 {
0147     struct snd_card *card = dev_get_drvdata(dev);
0148     int ret = 0;
0149 
0150     if (card)
0151         ret = pxa2xx_ac97_do_suspend(card);
0152 
0153     return ret;
0154 }
0155 
0156 static int pxa2xx_ac97_resume(struct device *dev)
0157 {
0158     struct snd_card *card = dev_get_drvdata(dev);
0159     int ret = 0;
0160 
0161     if (card)
0162         ret = pxa2xx_ac97_do_resume(card);
0163 
0164     return ret;
0165 }
0166 
0167 static SIMPLE_DEV_PM_OPS(pxa2xx_ac97_pm_ops, pxa2xx_ac97_suspend, pxa2xx_ac97_resume);
0168 #endif
0169 
0170 static const struct snd_pcm_ops pxa2xx_ac97_pcm_ops = {
0171     .open       = pxa2xx_ac97_pcm_open,
0172     .close      = pxa2xx_ac97_pcm_close,
0173     .hw_params  = pxa2xx_pcm_hw_params,
0174     .prepare    = pxa2xx_ac97_pcm_prepare,
0175     .trigger    = pxa2xx_pcm_trigger,
0176     .pointer    = pxa2xx_pcm_pointer,
0177 };
0178 
0179 
0180 static int pxa2xx_ac97_pcm_new(struct snd_card *card)
0181 {
0182     struct snd_pcm *pcm;
0183     int ret;
0184 
0185     ret = snd_pcm_new(card, "PXA2xx-PCM", 0, 1, 1, &pcm);
0186     if (ret)
0187         goto out;
0188 
0189     ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
0190     if (ret)
0191         goto out;
0192 
0193     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pxa2xx_ac97_pcm_ops);
0194     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pxa2xx_ac97_pcm_ops);
0195     ret = pxa2xx_pcm_preallocate_dma_buffer(pcm);
0196     if (ret)
0197         goto out;
0198 
0199     pxa2xx_ac97_pcm = pcm;
0200     ret = 0;
0201 
0202  out:
0203     return ret;
0204 }
0205 
0206 static int pxa2xx_ac97_probe(struct platform_device *dev)
0207 {
0208     struct snd_card *card;
0209     struct snd_ac97_bus *ac97_bus;
0210     struct snd_ac97_template ac97_template;
0211     int ret;
0212     pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;
0213 
0214     if (dev->id >= 0) {
0215         dev_err(&dev->dev, "PXA2xx has only one AC97 port.\n");
0216         ret = -ENXIO;
0217         goto err_dev;
0218     }
0219 
0220     ret = snd_card_new(&dev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
0221                THIS_MODULE, 0, &card);
0222     if (ret < 0)
0223         goto err;
0224 
0225     strscpy(card->driver, dev->dev.driver->name, sizeof(card->driver));
0226 
0227     ret = pxa2xx_ac97_pcm_new(card);
0228     if (ret)
0229         goto err;
0230 
0231     ret = pxa2xx_ac97_hw_probe(dev);
0232     if (ret)
0233         goto err;
0234 
0235     ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);
0236     if (ret)
0237         goto err_remove;
0238     memset(&ac97_template, 0, sizeof(ac97_template));
0239     ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);
0240     if (ret)
0241         goto err_remove;
0242 
0243     snprintf(card->shortname, sizeof(card->shortname),
0244          "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));
0245     snprintf(card->longname, sizeof(card->longname),
0246          "%s (%s)", dev->dev.driver->name, card->mixername);
0247 
0248     if (pdata && pdata->codec_pdata[0])
0249         snd_ac97_dev_add_pdata(ac97_bus->codec[0], pdata->codec_pdata[0]);
0250     ret = snd_card_register(card);
0251     if (ret == 0) {
0252         platform_set_drvdata(dev, card);
0253         return 0;
0254     }
0255 
0256 err_remove:
0257     pxa2xx_ac97_hw_remove(dev);
0258 err:
0259     if (card)
0260         snd_card_free(card);
0261 err_dev:
0262     return ret;
0263 }
0264 
0265 static int pxa2xx_ac97_remove(struct platform_device *dev)
0266 {
0267     struct snd_card *card = platform_get_drvdata(dev);
0268 
0269     if (card) {
0270         snd_card_free(card);
0271         pxa2xx_ac97_hw_remove(dev);
0272     }
0273 
0274     return 0;
0275 }
0276 
0277 static struct platform_driver pxa2xx_ac97_driver = {
0278     .probe      = pxa2xx_ac97_probe,
0279     .remove     = pxa2xx_ac97_remove,
0280     .driver     = {
0281         .name   = "pxa2xx-ac97",
0282 #ifdef CONFIG_PM_SLEEP
0283         .pm = &pxa2xx_ac97_pm_ops,
0284 #endif
0285     },
0286 };
0287 
0288 module_platform_driver(pxa2xx_ac97_driver);
0289 
0290 MODULE_AUTHOR("Nicolas Pitre");
0291 MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
0292 MODULE_LICENSE("GPL");
0293 MODULE_ALIAS("platform:pxa2xx-ac97");