root/sound/soc/codecs/wm8524.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. wm8524_startup
  2. wm8524_shutdown
  3. wm8524_set_dai_sysclk
  4. wm8524_set_fmt
  5. wm8524_mute_stream
  6. wm8524_probe
  7. wm8524_codec_probe

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * wm8524.c  --  WM8524 ALSA SoC Audio driver
   4  *
   5  * Copyright 2009 Wolfson Microelectronics plc
   6  * Copyright 2017 NXP
   7  *
   8  * Based on WM8523 ALSA SoC Audio driver written by Mark Brown
   9  */
  10 
  11 #include <linux/module.h>
  12 #include <linux/moduleparam.h>
  13 #include <linux/init.h>
  14 #include <linux/delay.h>
  15 #include <linux/slab.h>
  16 #include <linux/gpio/consumer.h>
  17 #include <linux/of_device.h>
  18 #include <sound/core.h>
  19 #include <sound/pcm.h>
  20 #include <sound/pcm_params.h>
  21 #include <sound/soc.h>
  22 #include <sound/initval.h>
  23 
  24 #define WM8524_NUM_RATES 7
  25 
  26 /* codec private data */
  27 struct wm8524_priv {
  28         struct gpio_desc *mute;
  29         unsigned int sysclk;
  30         unsigned int rate_constraint_list[WM8524_NUM_RATES];
  31         struct snd_pcm_hw_constraint_list rate_constraint;
  32 };
  33 
  34 
  35 static const struct snd_soc_dapm_widget wm8524_dapm_widgets[] = {
  36 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
  37 SND_SOC_DAPM_OUTPUT("LINEVOUTL"),
  38 SND_SOC_DAPM_OUTPUT("LINEVOUTR"),
  39 };
  40 
  41 static const struct snd_soc_dapm_route wm8524_dapm_routes[] = {
  42         { "LINEVOUTL", NULL, "DAC" },
  43         { "LINEVOUTR", NULL, "DAC" },
  44 };
  45 
  46 static const struct {
  47         int value;
  48         int ratio;
  49 } lrclk_ratios[WM8524_NUM_RATES] = {
  50         { 1, 128 },
  51         { 2, 192 },
  52         { 3, 256 },
  53         { 4, 384 },
  54         { 5, 512 },
  55         { 6, 768 },
  56         { 7, 1152 },
  57 };
  58 
  59 static int wm8524_startup(struct snd_pcm_substream *substream,
  60                           struct snd_soc_dai *dai)
  61 {
  62         struct snd_soc_component *component = dai->component;
  63         struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(component);
  64 
  65         /* The set of sample rates that can be supported depends on the
  66          * MCLK supplied to the CODEC - enforce this.
  67          */
  68         if (!wm8524->sysclk) {
  69                 dev_err(component->dev,
  70                         "No MCLK configured, call set_sysclk() on init\n");
  71                 return -EINVAL;
  72         }
  73 
  74         snd_pcm_hw_constraint_list(substream->runtime, 0,
  75                                    SNDRV_PCM_HW_PARAM_RATE,
  76                                    &wm8524->rate_constraint);
  77 
  78         gpiod_set_value_cansleep(wm8524->mute, 1);
  79 
  80         return 0;
  81 }
  82 
  83 static void wm8524_shutdown(struct snd_pcm_substream *substream,
  84                           struct snd_soc_dai *dai)
  85 {
  86         struct snd_soc_component *component = dai->component;
  87         struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(component);
  88 
  89         gpiod_set_value_cansleep(wm8524->mute, 0);
  90 }
  91 
  92 static int wm8524_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  93                 int clk_id, unsigned int freq, int dir)
  94 {
  95         struct snd_soc_component *component = codec_dai->component;
  96         struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(component);
  97         unsigned int val;
  98         int i, j = 0;
  99 
 100         wm8524->sysclk = freq;
 101 
 102         wm8524->rate_constraint.count = 0;
 103         for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
 104                 val = freq / lrclk_ratios[i].ratio;
 105                 /* Check that it's a standard rate since core can't
 106                  * cope with others and having the odd rates confuses
 107                  * constraint matching.
 108                  */
 109                 switch (val) {
 110                 case 8000:
 111                 case 32000:
 112                 case 44100:
 113                 case 48000:
 114                 case 88200:
 115                 case 96000:
 116                 case 176400:
 117                 case 192000:
 118                         dev_dbg(component->dev, "Supported sample rate: %dHz\n",
 119                                 val);
 120                         wm8524->rate_constraint_list[j++] = val;
 121                         wm8524->rate_constraint.count++;
 122                         break;
 123                 default:
 124                         dev_dbg(component->dev, "Skipping sample rate: %dHz\n",
 125                                 val);
 126                 }
 127         }
 128 
 129         /* Need at least one supported rate... */
 130         if (wm8524->rate_constraint.count == 0)
 131                 return -EINVAL;
 132 
 133         return 0;
 134 }
 135 
 136 static int wm8524_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
 137 {
 138         fmt &= (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK |
 139                 SND_SOC_DAIFMT_MASTER_MASK);
 140 
 141         if (fmt != (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
 142                     SND_SOC_DAIFMT_CBS_CFS)) {
 143                 dev_err(codec_dai->dev, "Invalid DAI format\n");
 144                 return -EINVAL;
 145         }
 146 
 147         return 0;
 148 }
 149 
 150 static int wm8524_mute_stream(struct snd_soc_dai *dai, int mute, int stream)
 151 {
 152         struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(dai->component);
 153 
 154         if (wm8524->mute)
 155                 gpiod_set_value_cansleep(wm8524->mute, mute);
 156 
 157         return 0;
 158 }
 159 
 160 #define WM8524_RATES SNDRV_PCM_RATE_8000_192000
 161 
 162 #define WM8524_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
 163 
 164 static const struct snd_soc_dai_ops wm8524_dai_ops = {
 165         .startup        = wm8524_startup,
 166         .shutdown       = wm8524_shutdown,
 167         .set_sysclk     = wm8524_set_dai_sysclk,
 168         .set_fmt        = wm8524_set_fmt,
 169         .mute_stream    = wm8524_mute_stream,
 170 };
 171 
 172 static struct snd_soc_dai_driver wm8524_dai = {
 173         .name = "wm8524-hifi",
 174         .playback = {
 175                 .stream_name = "Playback",
 176                 .channels_min = 2,
 177                 .channels_max = 2,
 178                 .rates = WM8524_RATES,
 179                 .formats = WM8524_FORMATS,
 180         },
 181         .ops = &wm8524_dai_ops,
 182 };
 183 
 184 static int wm8524_probe(struct snd_soc_component *component)
 185 {
 186         struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(component);
 187 
 188         wm8524->rate_constraint.list = &wm8524->rate_constraint_list[0];
 189         wm8524->rate_constraint.count =
 190                 ARRAY_SIZE(wm8524->rate_constraint_list);
 191 
 192         return 0;
 193 }
 194 
 195 static const struct snd_soc_component_driver soc_component_dev_wm8524 = {
 196         .probe                  = wm8524_probe,
 197         .dapm_widgets           = wm8524_dapm_widgets,
 198         .num_dapm_widgets       = ARRAY_SIZE(wm8524_dapm_widgets),
 199         .dapm_routes            = wm8524_dapm_routes,
 200         .num_dapm_routes        = ARRAY_SIZE(wm8524_dapm_routes),
 201         .idle_bias_on           = 1,
 202         .use_pmdown_time        = 1,
 203         .endianness             = 1,
 204         .non_legacy_dai_naming  = 1,
 205 };
 206 
 207 static const struct of_device_id wm8524_of_match[] = {
 208         { .compatible = "wlf,wm8524" },
 209         { /* sentinel*/ }
 210 };
 211 MODULE_DEVICE_TABLE(of, wm8524_of_match);
 212 
 213 static int wm8524_codec_probe(struct platform_device *pdev)
 214 {
 215         struct wm8524_priv *wm8524;
 216         int ret;
 217 
 218         wm8524 = devm_kzalloc(&pdev->dev, sizeof(struct wm8524_priv),
 219                                                   GFP_KERNEL);
 220         if (wm8524 == NULL)
 221                 return -ENOMEM;
 222 
 223         platform_set_drvdata(pdev, wm8524);
 224 
 225         wm8524->mute = devm_gpiod_get(&pdev->dev, "wlf,mute", GPIOD_OUT_LOW);
 226         if (IS_ERR(wm8524->mute)) {
 227                 ret = PTR_ERR(wm8524->mute);
 228                 dev_err(&pdev->dev, "Failed to get mute line: %d\n", ret);
 229                 return ret;
 230         }
 231 
 232         ret = devm_snd_soc_register_component(&pdev->dev,
 233                         &soc_component_dev_wm8524, &wm8524_dai, 1);
 234         if (ret < 0)
 235                 dev_err(&pdev->dev, "Failed to register component: %d\n", ret);
 236 
 237         return ret;
 238 }
 239 
 240 static struct platform_driver wm8524_codec_driver = {
 241         .probe          = wm8524_codec_probe,
 242         .driver         = {
 243                 .name   = "wm8524-codec",
 244                 .of_match_table = wm8524_of_match,
 245         },
 246 };
 247 module_platform_driver(wm8524_codec_driver);
 248 
 249 MODULE_DESCRIPTION("ASoC WM8524 driver");
 250 MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
 251 MODULE_ALIAS("platform:wm8524-codec");
 252 MODULE_LICENSE("GPL");

/* [<][>][^][v][top][bottom][index][help] */