Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * wm8524.c  --  WM8524 ALSA SoC Audio driver
0004  *
0005  * Copyright 2009 Wolfson Microelectronics plc
0006  * Copyright 2017 NXP
0007  *
0008  * Based on WM8523 ALSA SoC Audio driver written by Mark Brown
0009  */
0010 
0011 #include <linux/module.h>
0012 #include <linux/moduleparam.h>
0013 #include <linux/init.h>
0014 #include <linux/delay.h>
0015 #include <linux/slab.h>
0016 #include <linux/gpio/consumer.h>
0017 #include <linux/of_device.h>
0018 #include <sound/core.h>
0019 #include <sound/pcm.h>
0020 #include <sound/pcm_params.h>
0021 #include <sound/soc.h>
0022 #include <sound/initval.h>
0023 
0024 #define WM8524_NUM_RATES 7
0025 
0026 /* codec private data */
0027 struct wm8524_priv {
0028     struct gpio_desc *mute;
0029     unsigned int sysclk;
0030     unsigned int rate_constraint_list[WM8524_NUM_RATES];
0031     struct snd_pcm_hw_constraint_list rate_constraint;
0032 };
0033 
0034 
0035 static const struct snd_soc_dapm_widget wm8524_dapm_widgets[] = {
0036 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
0037 SND_SOC_DAPM_OUTPUT("LINEVOUTL"),
0038 SND_SOC_DAPM_OUTPUT("LINEVOUTR"),
0039 };
0040 
0041 static const struct snd_soc_dapm_route wm8524_dapm_routes[] = {
0042     { "LINEVOUTL", NULL, "DAC" },
0043     { "LINEVOUTR", NULL, "DAC" },
0044 };
0045 
0046 static const struct {
0047     int value;
0048     int ratio;
0049 } lrclk_ratios[WM8524_NUM_RATES] = {
0050     { 1, 128 },
0051     { 2, 192 },
0052     { 3, 256 },
0053     { 4, 384 },
0054     { 5, 512 },
0055     { 6, 768 },
0056     { 7, 1152 },
0057 };
0058 
0059 static int wm8524_startup(struct snd_pcm_substream *substream,
0060               struct snd_soc_dai *dai)
0061 {
0062     struct snd_soc_component *component = dai->component;
0063     struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(component);
0064 
0065     /* The set of sample rates that can be supported depends on the
0066      * MCLK supplied to the CODEC - enforce this.
0067      */
0068     if (!wm8524->sysclk) {
0069         dev_err(component->dev,
0070             "No MCLK configured, call set_sysclk() on init\n");
0071         return -EINVAL;
0072     }
0073 
0074     snd_pcm_hw_constraint_list(substream->runtime, 0,
0075                    SNDRV_PCM_HW_PARAM_RATE,
0076                    &wm8524->rate_constraint);
0077 
0078     gpiod_set_value_cansleep(wm8524->mute, 1);
0079 
0080     return 0;
0081 }
0082 
0083 static void wm8524_shutdown(struct snd_pcm_substream *substream,
0084               struct snd_soc_dai *dai)
0085 {
0086     struct snd_soc_component *component = dai->component;
0087     struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(component);
0088 
0089     gpiod_set_value_cansleep(wm8524->mute, 0);
0090 }
0091 
0092 static int wm8524_set_dai_sysclk(struct snd_soc_dai *codec_dai,
0093         int clk_id, unsigned int freq, int dir)
0094 {
0095     struct snd_soc_component *component = codec_dai->component;
0096     struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(component);
0097     unsigned int val;
0098     int i, j = 0;
0099 
0100     wm8524->sysclk = freq;
0101 
0102     wm8524->rate_constraint.count = 0;
0103     for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
0104         val = freq / lrclk_ratios[i].ratio;
0105         /* Check that it's a standard rate since core can't
0106          * cope with others and having the odd rates confuses
0107          * constraint matching.
0108          */
0109         switch (val) {
0110         case 8000:
0111         case 32000:
0112         case 44100:
0113         case 48000:
0114         case 88200:
0115         case 96000:
0116         case 176400:
0117         case 192000:
0118             dev_dbg(component->dev, "Supported sample rate: %dHz\n",
0119                 val);
0120             wm8524->rate_constraint_list[j++] = val;
0121             wm8524->rate_constraint.count++;
0122             break;
0123         default:
0124             dev_dbg(component->dev, "Skipping sample rate: %dHz\n",
0125                 val);
0126         }
0127     }
0128 
0129     /* Need at least one supported rate... */
0130     if (wm8524->rate_constraint.count == 0)
0131         return -EINVAL;
0132 
0133     return 0;
0134 }
0135 
0136 static int wm8524_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
0137 {
0138     fmt &= (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK |
0139         SND_SOC_DAIFMT_MASTER_MASK);
0140 
0141     if (fmt != (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
0142             SND_SOC_DAIFMT_CBS_CFS)) {
0143         dev_err(codec_dai->dev, "Invalid DAI format\n");
0144         return -EINVAL;
0145     }
0146 
0147     return 0;
0148 }
0149 
0150 static int wm8524_mute_stream(struct snd_soc_dai *dai, int mute, int stream)
0151 {
0152     struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(dai->component);
0153 
0154     if (wm8524->mute)
0155         gpiod_set_value_cansleep(wm8524->mute, mute);
0156 
0157     return 0;
0158 }
0159 
0160 #define WM8524_RATES SNDRV_PCM_RATE_8000_192000
0161 
0162 #define WM8524_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
0163             SNDRV_PCM_FMTBIT_S24_LE |\
0164             SNDRV_PCM_FMTBIT_S32_LE)
0165 
0166 static const struct snd_soc_dai_ops wm8524_dai_ops = {
0167     .startup    = wm8524_startup,
0168     .shutdown   = wm8524_shutdown,
0169     .set_sysclk = wm8524_set_dai_sysclk,
0170     .set_fmt    = wm8524_set_fmt,
0171     .mute_stream    = wm8524_mute_stream,
0172 };
0173 
0174 static struct snd_soc_dai_driver wm8524_dai = {
0175     .name = "wm8524-hifi",
0176     .playback = {
0177         .stream_name = "Playback",
0178         .channels_min = 2,
0179         .channels_max = 2,
0180         .rates = WM8524_RATES,
0181         .formats = WM8524_FORMATS,
0182     },
0183     .ops = &wm8524_dai_ops,
0184 };
0185 
0186 static int wm8524_probe(struct snd_soc_component *component)
0187 {
0188     struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(component);
0189 
0190     wm8524->rate_constraint.list = &wm8524->rate_constraint_list[0];
0191     wm8524->rate_constraint.count =
0192         ARRAY_SIZE(wm8524->rate_constraint_list);
0193 
0194     return 0;
0195 }
0196 
0197 static const struct snd_soc_component_driver soc_component_dev_wm8524 = {
0198     .probe          = wm8524_probe,
0199     .dapm_widgets       = wm8524_dapm_widgets,
0200     .num_dapm_widgets   = ARRAY_SIZE(wm8524_dapm_widgets),
0201     .dapm_routes        = wm8524_dapm_routes,
0202     .num_dapm_routes    = ARRAY_SIZE(wm8524_dapm_routes),
0203     .idle_bias_on       = 1,
0204     .use_pmdown_time    = 1,
0205     .endianness     = 1,
0206 };
0207 
0208 static const struct of_device_id wm8524_of_match[] = {
0209     { .compatible = "wlf,wm8524" },
0210     { /* sentinel*/ }
0211 };
0212 MODULE_DEVICE_TABLE(of, wm8524_of_match);
0213 
0214 static int wm8524_codec_probe(struct platform_device *pdev)
0215 {
0216     struct wm8524_priv *wm8524;
0217     int ret;
0218 
0219     wm8524 = devm_kzalloc(&pdev->dev, sizeof(struct wm8524_priv),
0220                           GFP_KERNEL);
0221     if (wm8524 == NULL)
0222         return -ENOMEM;
0223 
0224     platform_set_drvdata(pdev, wm8524);
0225 
0226     wm8524->mute = devm_gpiod_get(&pdev->dev, "wlf,mute", GPIOD_OUT_LOW);
0227     if (IS_ERR(wm8524->mute)) {
0228         ret = PTR_ERR(wm8524->mute);
0229         dev_err_probe(&pdev->dev, ret, "Failed to get mute line\n");
0230         return ret;
0231     }
0232 
0233     ret = devm_snd_soc_register_component(&pdev->dev,
0234             &soc_component_dev_wm8524, &wm8524_dai, 1);
0235     if (ret < 0)
0236         dev_err(&pdev->dev, "Failed to register component: %d\n", ret);
0237 
0238     return ret;
0239 }
0240 
0241 static struct platform_driver wm8524_codec_driver = {
0242     .probe      = wm8524_codec_probe,
0243     .driver     = {
0244         .name   = "wm8524-codec",
0245         .of_match_table = wm8524_of_match,
0246     },
0247 };
0248 module_platform_driver(wm8524_codec_driver);
0249 
0250 MODULE_DESCRIPTION("ASoC WM8524 driver");
0251 MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
0252 MODULE_ALIAS("platform:wm8524-codec");
0253 MODULE_LICENSE("GPL");