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/mod_devicetable.h>
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/init.h>
15#include <linux/delay.h>
16#include <linux/slab.h>
17#include <linux/gpio/consumer.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 */
27struct 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
35static const struct snd_soc_dapm_widget wm8524_dapm_widgets[] = {
36SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
37SND_SOC_DAPM_OUTPUT("LINEVOUTL"),
38SND_SOC_DAPM_OUTPUT("LINEVOUTR"),
39};
40
41static const struct snd_soc_dapm_route wm8524_dapm_routes[] = {
42 { "LINEVOUTL", NULL, "DAC" },
43 { "LINEVOUTR", NULL, "DAC" },
44};
45
46static 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
59static 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(c: 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(runtime: substream->runtime, cond: 0,
75 SNDRV_PCM_HW_PARAM_RATE,
76 l: &wm8524->rate_constraint);
77
78 gpiod_set_value_cansleep(desc: wm8524->mute, value: 1);
79
80 return 0;
81}
82
83static 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(c: component);
88
89 gpiod_set_value_cansleep(desc: wm8524->mute, value: 0);
90}
91
92static 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(c: 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
136static 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
150static int wm8524_mute_stream(struct snd_soc_dai *dai, int mute, int stream)
151{
152 struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(c: dai->component);
153
154 if (wm8524->mute)
155 gpiod_set_value_cansleep(desc: wm8524->mute, value: 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 |\
163 SNDRV_PCM_FMTBIT_S24_LE |\
164 SNDRV_PCM_FMTBIT_S32_LE)
165
166static const struct snd_soc_dai_ops wm8524_dai_ops = {
167 .startup = wm8524_startup,
168 .shutdown = wm8524_shutdown,
169 .set_sysclk = wm8524_set_dai_sysclk,
170 .set_fmt = wm8524_set_fmt,
171 .mute_stream = wm8524_mute_stream,
172};
173
174static struct snd_soc_dai_driver wm8524_dai = {
175 .name = "wm8524-hifi",
176 .playback = {
177 .stream_name = "Playback",
178 .channels_min = 2,
179 .channels_max = 2,
180 .rates = WM8524_RATES,
181 .formats = WM8524_FORMATS,
182 },
183 .ops = &wm8524_dai_ops,
184};
185
186static int wm8524_probe(struct snd_soc_component *component)
187{
188 struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(c: component);
189
190 wm8524->rate_constraint.list = &wm8524->rate_constraint_list[0];
191 wm8524->rate_constraint.count =
192 ARRAY_SIZE(wm8524->rate_constraint_list);
193
194 return 0;
195}
196
197static const struct snd_soc_component_driver soc_component_dev_wm8524 = {
198 .probe = wm8524_probe,
199 .dapm_widgets = wm8524_dapm_widgets,
200 .num_dapm_widgets = ARRAY_SIZE(wm8524_dapm_widgets),
201 .dapm_routes = wm8524_dapm_routes,
202 .num_dapm_routes = ARRAY_SIZE(wm8524_dapm_routes),
203 .idle_bias_on = 1,
204 .use_pmdown_time = 1,
205 .endianness = 1,
206};
207
208static const struct of_device_id wm8524_of_match[] = {
209 { .compatible = "wlf,wm8524" },
210 { /* sentinel*/ }
211};
212MODULE_DEVICE_TABLE(of, wm8524_of_match);
213
214static int wm8524_codec_probe(struct platform_device *pdev)
215{
216 struct wm8524_priv *wm8524;
217 int ret;
218
219 wm8524 = devm_kzalloc(dev: &pdev->dev, size: sizeof(struct wm8524_priv),
220 GFP_KERNEL);
221 if (wm8524 == NULL)
222 return -ENOMEM;
223
224 platform_set_drvdata(pdev, data: wm8524);
225
226 wm8524->mute = devm_gpiod_get(dev: &pdev->dev, con_id: "wlf,mute", flags: GPIOD_OUT_LOW);
227 if (IS_ERR(ptr: wm8524->mute)) {
228 ret = PTR_ERR(ptr: wm8524->mute);
229 dev_err_probe(dev: &pdev->dev, err: ret, fmt: "Failed to get mute line\n");
230 return ret;
231 }
232
233 ret = devm_snd_soc_register_component(dev: &pdev->dev,
234 component_driver: &soc_component_dev_wm8524, dai_drv: &wm8524_dai, num_dai: 1);
235 if (ret < 0)
236 dev_err(&pdev->dev, "Failed to register component: %d\n", ret);
237
238 return ret;
239}
240
241static struct platform_driver wm8524_codec_driver = {
242 .probe = wm8524_codec_probe,
243 .driver = {
244 .name = "wm8524-codec",
245 .of_match_table = wm8524_of_match,
246 },
247};
248module_platform_driver(wm8524_codec_driver);
249
250MODULE_DESCRIPTION("ASoC WM8524 driver");
251MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
252MODULE_ALIAS("platform:wm8524-codec");
253MODULE_LICENSE("GPL");
254

source code of linux/sound/soc/codecs/wm8524.c