xref: /openbmc/linux/sound/soc/codecs/wm8524.c (revision 8ec90bfd)
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 |\
163 			SNDRV_PCM_FMTBIT_S24_LE |\
164 			SNDRV_PCM_FMTBIT_S32_LE)
165 
166 static 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 
174 static 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 
186 static int wm8524_probe(struct snd_soc_component *component)
187 {
188 	struct wm8524_priv *wm8524 = snd_soc_component_get_drvdata(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 
197 static 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 	.non_legacy_dai_naming	= 1,
207 };
208 
209 static const struct of_device_id wm8524_of_match[] = {
210 	{ .compatible = "wlf,wm8524" },
211 	{ /* sentinel*/ }
212 };
213 MODULE_DEVICE_TABLE(of, wm8524_of_match);
214 
215 static int wm8524_codec_probe(struct platform_device *pdev)
216 {
217 	struct wm8524_priv *wm8524;
218 	int ret;
219 
220 	wm8524 = devm_kzalloc(&pdev->dev, sizeof(struct wm8524_priv),
221 						  GFP_KERNEL);
222 	if (wm8524 == NULL)
223 		return -ENOMEM;
224 
225 	platform_set_drvdata(pdev, wm8524);
226 
227 	wm8524->mute = devm_gpiod_get(&pdev->dev, "wlf,mute", GPIOD_OUT_LOW);
228 	if (IS_ERR(wm8524->mute)) {
229 		ret = PTR_ERR(wm8524->mute);
230 		dev_err(&pdev->dev, "Failed to get mute line: %d\n", ret);
231 		return ret;
232 	}
233 
234 	ret = devm_snd_soc_register_component(&pdev->dev,
235 			&soc_component_dev_wm8524, &wm8524_dai, 1);
236 	if (ret < 0)
237 		dev_err(&pdev->dev, "Failed to register component: %d\n", ret);
238 
239 	return ret;
240 }
241 
242 static struct platform_driver wm8524_codec_driver = {
243 	.probe		= wm8524_codec_probe,
244 	.driver		= {
245 		.name	= "wm8524-codec",
246 		.of_match_table = wm8524_of_match,
247 	},
248 };
249 module_platform_driver(wm8524_codec_driver);
250 
251 MODULE_DESCRIPTION("ASoC WM8524 driver");
252 MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
253 MODULE_ALIAS("platform:wm8524-codec");
254 MODULE_LICENSE("GPL");
255