1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 //  wm1133-ev1.c - Audio for WM1133-EV1 on i.MX31ADS
4 //
5 //  Copyright (c) 2010 Wolfson Microelectronics plc
6 //  Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 //
8 //  Based on an earlier driver for the same hardware by Liam Girdwood.
9 
10 #include <linux/platform_device.h>
11 #include <linux/clk.h>
12 #include <linux/module.h>
13 #include <sound/core.h>
14 #include <sound/jack.h>
15 #include <sound/pcm.h>
16 #include <sound/pcm_params.h>
17 #include <sound/soc.h>
18 
19 #include "imx-ssi.h"
20 #include "../codecs/wm8350.h"
21 #include "imx-audmux.h"
22 
23 /* There is a silicon mic on the board optionally connected via a solder pad
24  * SP1.  Define this to enable it.
25  */
26 #undef USE_SIMIC
27 
28 struct _wm8350_audio {
29 	unsigned int channels;
30 	snd_pcm_format_t format;
31 	unsigned int rate;
32 	unsigned int sysclk;
33 	unsigned int bclkdiv;
34 	unsigned int clkdiv;
35 	unsigned int lr_rate;
36 };
37 
38 /* in order of power consumption per rate (lowest first) */
39 static const struct _wm8350_audio wm8350_audio[] = {
40 	/* 16bit mono modes */
41 	{1, SNDRV_PCM_FORMAT_S16_LE, 8000, 12288000 >> 1,
42 	 WM8350_BCLK_DIV_48, WM8350_DACDIV_3, 16,},
43 
44 	/* 16 bit stereo modes */
45 	{2, SNDRV_PCM_FORMAT_S16_LE, 8000, 12288000,
46 	 WM8350_BCLK_DIV_48, WM8350_DACDIV_6, 32,},
47 	{2, SNDRV_PCM_FORMAT_S16_LE, 16000, 12288000,
48 	 WM8350_BCLK_DIV_24, WM8350_DACDIV_3, 32,},
49 	{2, SNDRV_PCM_FORMAT_S16_LE, 32000, 12288000,
50 	 WM8350_BCLK_DIV_12, WM8350_DACDIV_1_5, 32,},
51 	{2, SNDRV_PCM_FORMAT_S16_LE, 48000, 12288000,
52 	 WM8350_BCLK_DIV_8, WM8350_DACDIV_1, 32,},
53 	{2, SNDRV_PCM_FORMAT_S16_LE, 96000, 24576000,
54 	 WM8350_BCLK_DIV_8, WM8350_DACDIV_1, 32,},
55 	{2, SNDRV_PCM_FORMAT_S16_LE, 11025, 11289600,
56 	 WM8350_BCLK_DIV_32, WM8350_DACDIV_4, 32,},
57 	{2, SNDRV_PCM_FORMAT_S16_LE, 22050, 11289600,
58 	 WM8350_BCLK_DIV_16, WM8350_DACDIV_2, 32,},
59 	{2, SNDRV_PCM_FORMAT_S16_LE, 44100, 11289600,
60 	 WM8350_BCLK_DIV_8, WM8350_DACDIV_1, 32,},
61 	{2, SNDRV_PCM_FORMAT_S16_LE, 88200, 22579200,
62 	 WM8350_BCLK_DIV_8, WM8350_DACDIV_1, 32,},
63 
64 	/* 24bit stereo modes */
65 	{2, SNDRV_PCM_FORMAT_S24_LE, 48000, 12288000,
66 	 WM8350_BCLK_DIV_4, WM8350_DACDIV_1, 64,},
67 	{2, SNDRV_PCM_FORMAT_S24_LE, 96000, 24576000,
68 	 WM8350_BCLK_DIV_4, WM8350_DACDIV_1, 64,},
69 	{2, SNDRV_PCM_FORMAT_S24_LE, 44100, 11289600,
70 	 WM8350_BCLK_DIV_4, WM8350_DACDIV_1, 64,},
71 	{2, SNDRV_PCM_FORMAT_S24_LE, 88200, 22579200,
72 	 WM8350_BCLK_DIV_4, WM8350_DACDIV_1, 64,},
73 };
74 
wm1133_ev1_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)75 static int wm1133_ev1_hw_params(struct snd_pcm_substream *substream,
76 				struct snd_pcm_hw_params *params)
77 {
78 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
79 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
80 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
81 	int i, found = 0;
82 	snd_pcm_format_t format = params_format(params);
83 	unsigned int rate = params_rate(params);
84 	unsigned int channels = params_channels(params);
85 
86 	/* find the correct audio parameters */
87 	for (i = 0; i < ARRAY_SIZE(wm8350_audio); i++) {
88 		if (rate == wm8350_audio[i].rate &&
89 		    format == wm8350_audio[i].format &&
90 		    channels == wm8350_audio[i].channels) {
91 			found = 1;
92 			break;
93 		}
94 	}
95 	if (!found)
96 		return -EINVAL;
97 
98 	/* codec FLL input is 14.75 MHz from MCLK */
99 	snd_soc_dai_set_pll(codec_dai, 0, 0, 14750000, wm8350_audio[i].sysclk);
100 
101 	/* TODO: The SSI driver should figure this out for us */
102 	switch (channels) {
103 	case 2:
104 		snd_soc_dai_set_tdm_slot(cpu_dai, 0x3, 0x3, 2, 0);
105 		break;
106 	case 1:
107 		snd_soc_dai_set_tdm_slot(cpu_dai, 0x1, 0x1, 1, 0);
108 		break;
109 	default:
110 		return -EINVAL;
111 	}
112 
113 	/* set MCLK as the codec system clock for DAC and ADC */
114 	snd_soc_dai_set_sysclk(codec_dai, WM8350_MCLK_SEL_PLL_MCLK,
115 			       wm8350_audio[i].sysclk, SND_SOC_CLOCK_IN);
116 
117 	/* set codec BCLK division for sample rate */
118 	snd_soc_dai_set_clkdiv(codec_dai, WM8350_BCLK_CLKDIV,
119 			       wm8350_audio[i].bclkdiv);
120 
121 	/* DAI is synchronous and clocked with DAC LRCLK & ADC LRC */
122 	snd_soc_dai_set_clkdiv(codec_dai,
123 			       WM8350_DACLR_CLKDIV, wm8350_audio[i].lr_rate);
124 	snd_soc_dai_set_clkdiv(codec_dai,
125 			       WM8350_ADCLR_CLKDIV, wm8350_audio[i].lr_rate);
126 
127 	/* now configure DAC and ADC clocks */
128 	snd_soc_dai_set_clkdiv(codec_dai,
129 			       WM8350_DAC_CLKDIV, wm8350_audio[i].clkdiv);
130 
131 	snd_soc_dai_set_clkdiv(codec_dai,
132 			       WM8350_ADC_CLKDIV, wm8350_audio[i].clkdiv);
133 
134 	return 0;
135 }
136 
137 static const struct snd_soc_ops wm1133_ev1_ops = {
138 	.hw_params = wm1133_ev1_hw_params,
139 };
140 
141 static const struct snd_soc_dapm_widget wm1133_ev1_widgets[] = {
142 #ifdef USE_SIMIC
143 	SND_SOC_DAPM_MIC("SiMIC", NULL),
144 #endif
145 	SND_SOC_DAPM_MIC("Mic1 Jack", NULL),
146 	SND_SOC_DAPM_MIC("Mic2 Jack", NULL),
147 	SND_SOC_DAPM_LINE("Line In Jack", NULL),
148 	SND_SOC_DAPM_LINE("Line Out Jack", NULL),
149 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
150 };
151 
152 /* imx32ads soc_card audio map */
153 static const struct snd_soc_dapm_route wm1133_ev1_map[] = {
154 
155 #ifdef USE_SIMIC
156 	/* SiMIC --> IN1LN (with automatic bias) via SP1 */
157 	{ "IN1LN", NULL, "Mic Bias" },
158 	{ "Mic Bias", NULL, "SiMIC" },
159 #endif
160 
161 	/* Mic 1 Jack --> IN1LN and IN1LP (with automatic bias) */
162 	{ "IN1LN", NULL, "Mic Bias" },
163 	{ "IN1LP", NULL, "Mic1 Jack" },
164 	{ "Mic Bias", NULL, "Mic1 Jack" },
165 
166 	/* Mic 2 Jack --> IN1RN and IN1RP (with automatic bias) */
167 	{ "IN1RN", NULL, "Mic Bias" },
168 	{ "IN1RP", NULL, "Mic2 Jack" },
169 	{ "Mic Bias", NULL, "Mic2 Jack" },
170 
171 	/* Line in Jack --> AUX (L+R) */
172 	{ "IN3R", NULL, "Line In Jack" },
173 	{ "IN3L", NULL, "Line In Jack" },
174 
175 	/* Out1 --> Headphone Jack */
176 	{ "Headphone Jack", NULL, "OUT1R" },
177 	{ "Headphone Jack", NULL, "OUT1L" },
178 
179 	/* Out1 --> Line Out Jack */
180 	{ "Line Out Jack", NULL, "OUT2R" },
181 	{ "Line Out Jack", NULL, "OUT2L" },
182 };
183 
184 static struct snd_soc_jack hp_jack;
185 
186 static struct snd_soc_jack_pin hp_jack_pins[] = {
187 	{ .pin = "Headphone Jack", .mask = SND_JACK_HEADPHONE },
188 };
189 
190 static struct snd_soc_jack mic_jack;
191 
192 static struct snd_soc_jack_pin mic_jack_pins[] = {
193 	{ .pin = "Mic1 Jack", .mask = SND_JACK_MICROPHONE },
194 	{ .pin = "Mic2 Jack", .mask = SND_JACK_MICROPHONE },
195 };
196 
wm1133_ev1_init(struct snd_soc_pcm_runtime * rtd)197 static int wm1133_ev1_init(struct snd_soc_pcm_runtime *rtd)
198 {
199 	struct snd_soc_component *component = rtd->codec_dai->component;
200 
201 	/* Headphone jack detection */
202 	snd_soc_card_jack_new(rtd->card, "Headphone", SND_JACK_HEADPHONE,
203 			      &hp_jack, hp_jack_pins, ARRAY_SIZE(hp_jack_pins));
204 	wm8350_hp_jack_detect(component, WM8350_JDR, &hp_jack, SND_JACK_HEADPHONE);
205 
206 	/* Microphone jack detection */
207 	snd_soc_card_jack_new(rtd->card, "Microphone",
208 			      SND_JACK_MICROPHONE | SND_JACK_BTN_0, &mic_jack,
209 			      mic_jack_pins, ARRAY_SIZE(mic_jack_pins));
210 	wm8350_mic_jack_detect(component, &mic_jack, SND_JACK_MICROPHONE,
211 			       SND_JACK_BTN_0);
212 
213 	snd_soc_dapm_force_enable_pin(&rtd->card->dapm, "Mic Bias");
214 
215 	return 0;
216 }
217 
218 
219 SND_SOC_DAILINK_DEFS(ev1,
220 	DAILINK_COMP_ARRAY(COMP_CPU("imx-ssi.0")),
221 	DAILINK_COMP_ARRAY(COMP_CODEC("wm8350-codec.0-0x1a", "wm8350-hifi")),
222 	DAILINK_COMP_ARRAY(COMP_PLATFORM("imx-ssi.0")));
223 
224 static struct snd_soc_dai_link wm1133_ev1_dai = {
225 	.name = "WM1133-EV1",
226 	.stream_name = "Audio",
227 	.init = wm1133_ev1_init,
228 	.ops = &wm1133_ev1_ops,
229 	.symmetric_rates = 1,
230 	.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
231 		   SND_SOC_DAIFMT_CBM_CFM,
232 	SND_SOC_DAILINK_REG(ev1),
233 };
234 
235 static struct snd_soc_card wm1133_ev1 = {
236 	.name = "WM1133-EV1",
237 	.owner = THIS_MODULE,
238 	.dai_link = &wm1133_ev1_dai,
239 	.num_links = 1,
240 
241 	.dapm_widgets = wm1133_ev1_widgets,
242 	.num_dapm_widgets = ARRAY_SIZE(wm1133_ev1_widgets),
243 	.dapm_routes = wm1133_ev1_map,
244 	.num_dapm_routes = ARRAY_SIZE(wm1133_ev1_map),
245 };
246 
247 static struct platform_device *wm1133_ev1_snd_device;
248 
wm1133_ev1_audio_init(void)249 static int __init wm1133_ev1_audio_init(void)
250 {
251 	int ret;
252 	unsigned int ptcr, pdcr;
253 
254 	/* SSI0 mastered by port 5 */
255 	ptcr = IMX_AUDMUX_V2_PTCR_SYN |
256 		IMX_AUDMUX_V2_PTCR_TFSDIR |
257 		IMX_AUDMUX_V2_PTCR_TFSEL(MX31_AUDMUX_PORT5_SSI_PINS_5) |
258 		IMX_AUDMUX_V2_PTCR_TCLKDIR |
259 		IMX_AUDMUX_V2_PTCR_TCSEL(MX31_AUDMUX_PORT5_SSI_PINS_5);
260 	pdcr = IMX_AUDMUX_V2_PDCR_RXDSEL(MX31_AUDMUX_PORT5_SSI_PINS_5);
261 	imx_audmux_v2_configure_port(MX31_AUDMUX_PORT1_SSI0, ptcr, pdcr);
262 
263 	ptcr = IMX_AUDMUX_V2_PTCR_SYN;
264 	pdcr = IMX_AUDMUX_V2_PDCR_RXDSEL(MX31_AUDMUX_PORT1_SSI0);
265 	imx_audmux_v2_configure_port(MX31_AUDMUX_PORT5_SSI_PINS_5, ptcr, pdcr);
266 
267 	wm1133_ev1_snd_device = platform_device_alloc("soc-audio", -1);
268 	if (!wm1133_ev1_snd_device)
269 		return -ENOMEM;
270 
271 	platform_set_drvdata(wm1133_ev1_snd_device, &wm1133_ev1);
272 	ret = platform_device_add(wm1133_ev1_snd_device);
273 
274 	if (ret)
275 		platform_device_put(wm1133_ev1_snd_device);
276 
277 	return ret;
278 }
279 module_init(wm1133_ev1_audio_init);
280 
wm1133_ev1_audio_exit(void)281 static void __exit wm1133_ev1_audio_exit(void)
282 {
283 	platform_device_unregister(wm1133_ev1_snd_device);
284 }
285 module_exit(wm1133_ev1_audio_exit);
286 
287 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
288 MODULE_DESCRIPTION("Audio for WM1133-EV1 on i.MX31ADS");
289 MODULE_LICENSE("GPL");
290