blob: f62f81ceab0d2211227f2251d698658c150bd50d [file] [log] [blame]
Fabio Estevamaa624a02018-07-24 09:48:30 -03001// SPDX-License-Identifier: GPL-2.0
2//
3// Freescale Generic ASoC Sound Card driver with ASRC
4//
5// Copyright (C) 2014 Freescale Semiconductor, Inc.
6//
7// Author: Nicolin Chen <nicoleotsuka@gmail.com>
Nicolin Chen708b4352014-07-30 19:27:38 +08008
9#include <linux/clk.h>
10#include <linux/i2c.h>
11#include <linux/module.h>
12#include <linux/of_platform.h>
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +020013#if IS_ENABLED(CONFIG_SND_AC97_CODEC)
14#include <sound/ac97_codec.h>
15#endif
Nicolin Chen708b4352014-07-30 19:27:38 +080016#include <sound/pcm_params.h>
17#include <sound/soc.h>
Shengjiu Wang3b171192020-07-15 22:09:39 +080018#include <sound/jack.h>
19#include <sound/simple_card_utils.h>
Nicolin Chen708b4352014-07-30 19:27:38 +080020
21#include "fsl_esai.h"
22#include "fsl_sai.h"
23#include "imx-audmux.h"
24
25#include "../codecs/sgtl5000.h"
26#include "../codecs/wm8962.h"
Zidan Wang50e0ee02015-08-14 19:11:09 +080027#include "../codecs/wm8960.h"
Nicolin Chen708b4352014-07-30 19:27:38 +080028
Felipe F. Tonello57e756d2016-01-29 11:01:00 +000029#define CS427x_SYSCLK_MCLK 0
30
Nicolin Chen708b4352014-07-30 19:27:38 +080031#define RX 0
32#define TX 1
33
34/* Default DAI format without Master and Slave flag */
35#define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
36
37/**
Pierre-Louis Bossart31deacf2020-07-02 14:21:38 -050038 * struct codec_priv - CODEC private data
Nicolin Chen708b4352014-07-30 19:27:38 +080039 * @mclk_freq: Clock rate of MCLK
40 * @mclk_id: MCLK (or main clock) id for set_sysclk()
41 * @fll_id: FLL (or secordary clock) id for set_sysclk()
42 * @pll_id: PLL id for set_pll()
43 */
44struct codec_priv {
45 unsigned long mclk_freq;
46 u32 mclk_id;
47 u32 fll_id;
48 u32 pll_id;
49};
50
51/**
Pierre-Louis Bossart31deacf2020-07-02 14:21:38 -050052 * struct cpu_priv - CPU private data
53 * @sysclk_freq: SYSCLK rates for set_sysclk()
54 * @sysclk_dir: SYSCLK directions for set_sysclk()
55 * @sysclk_id: SYSCLK ids for set_sysclk()
Nicolin Chencb3fc1f2014-10-24 16:48:13 -070056 * @slot_width: Slot width of each frame
Nicolin Chen708b4352014-07-30 19:27:38 +080057 *
58 * Note: [1] for tx and [0] for rx
59 */
60struct cpu_priv {
61 unsigned long sysclk_freq[2];
62 u32 sysclk_dir[2];
63 u32 sysclk_id[2];
Nicolin Chencb3fc1f2014-10-24 16:48:13 -070064 u32 slot_width;
Nicolin Chen708b4352014-07-30 19:27:38 +080065};
66
67/**
Pierre-Louis Bossart31deacf2020-07-02 14:21:38 -050068 * struct fsl_asoc_card_priv - Freescale Generic ASOC card private data
69 * @dai_link: DAI link structure including normal one and DPCM link
Shengjiu Wang3b171192020-07-15 22:09:39 +080070 * @hp_jack: Headphone Jack structure
71 * @mic_jack: Microphone Jack structure
Nicolin Chen708b4352014-07-30 19:27:38 +080072 * @pdev: platform device pointer
73 * @codec_priv: CODEC private data
74 * @cpu_priv: CPU private data
75 * @card: ASoC card structure
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +080076 * @streams: Mask of current active streams
Nicolin Chen708b4352014-07-30 19:27:38 +080077 * @sample_rate: Current sample rate
78 * @sample_format: Current sample format
79 * @asrc_rate: ASRC sample rate used by Back-Ends
80 * @asrc_format: ASRC sample format used by Back-Ends
81 * @dai_fmt: DAI format between CPU and CODEC
82 * @name: Card name
83 */
84
85struct fsl_asoc_card_priv {
86 struct snd_soc_dai_link dai_link[3];
Shengjiu Wang3b171192020-07-15 22:09:39 +080087 struct asoc_simple_jack hp_jack;
88 struct asoc_simple_jack mic_jack;
Nicolin Chen708b4352014-07-30 19:27:38 +080089 struct platform_device *pdev;
90 struct codec_priv codec_priv;
91 struct cpu_priv cpu_priv;
92 struct snd_soc_card card;
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +080093 u8 streams;
Nicolin Chen708b4352014-07-30 19:27:38 +080094 u32 sample_rate;
Fabio Estevamfc734c242018-02-11 19:53:18 -020095 snd_pcm_format_t sample_format;
Nicolin Chen708b4352014-07-30 19:27:38 +080096 u32 asrc_rate;
Fabio Estevamfc734c242018-02-11 19:53:18 -020097 snd_pcm_format_t asrc_format;
Nicolin Chen708b4352014-07-30 19:27:38 +080098 u32 dai_fmt;
99 char name[32];
100};
101
Pierre-Louis Bossart31deacf2020-07-02 14:21:38 -0500102/*
Lee Jones1b582142020-07-15 16:00:09 +0100103 * This dapm route map exists for DPCM link only.
Nicolin Chen708b4352014-07-30 19:27:38 +0800104 * The other routes shall go through Device Tree.
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800105 *
106 * Note: keep all ASRC routes in the second half
107 * to drop them easily for non-ASRC cases.
Nicolin Chen708b4352014-07-30 19:27:38 +0800108 */
109static const struct snd_soc_dapm_route audio_map[] = {
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800110 /* 1st half -- Normal DAPM routes */
Nicolin Chen708b4352014-07-30 19:27:38 +0800111 {"Playback", NULL, "CPU-Playback"},
Nicolin Chen708b4352014-07-30 19:27:38 +0800112 {"CPU-Capture", NULL, "Capture"},
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800113 /* 2nd half -- ASRC DAPM routes */
114 {"CPU-Playback", NULL, "ASRC-Playback"},
115 {"ASRC-Capture", NULL, "CPU-Capture"},
Nicolin Chen708b4352014-07-30 19:27:38 +0800116};
117
Maciej S. Szmigiero25e5ef92015-12-20 21:34:29 +0100118static const struct snd_soc_dapm_route audio_map_ac97[] = {
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800119 /* 1st half -- Normal DAPM routes */
Maciej S. Szmigiero25e5ef92015-12-20 21:34:29 +0100120 {"Playback", NULL, "AC97 Playback"},
Maciej S. Szmigiero25e5ef92015-12-20 21:34:29 +0100121 {"AC97 Capture", NULL, "Capture"},
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800122 /* 2nd half -- ASRC DAPM routes */
123 {"AC97 Playback", NULL, "ASRC-Playback"},
124 {"ASRC-Capture", NULL, "AC97 Capture"},
Maciej S. Szmigiero25e5ef92015-12-20 21:34:29 +0100125};
126
Shengjiu Wang039652a2020-06-17 12:48:25 +0800127static const struct snd_soc_dapm_route audio_map_tx[] = {
128 /* 1st half -- Normal DAPM routes */
129 {"Playback", NULL, "CPU-Playback"},
130 /* 2nd half -- ASRC DAPM routes */
131 {"CPU-Playback", NULL, "ASRC-Playback"},
132};
133
Shengjiu Wang77f1ff752020-11-30 11:57:47 +0800134static const struct snd_soc_dapm_route audio_map_rx[] = {
135 /* 1st half -- Normal DAPM routes */
136 {"CPU-Capture", NULL, "Capture"},
137 /* 2nd half -- ASRC DAPM routes */
138 {"ASRC-Capture", NULL, "CPU-Capture"},
139};
140
Nicolin Chen708b4352014-07-30 19:27:38 +0800141/* Add all possible widgets into here without being redundant */
142static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
143 SND_SOC_DAPM_LINE("Line Out Jack", NULL),
144 SND_SOC_DAPM_LINE("Line In Jack", NULL),
145 SND_SOC_DAPM_HP("Headphone Jack", NULL),
146 SND_SOC_DAPM_SPK("Ext Spk", NULL),
147 SND_SOC_DAPM_MIC("Mic Jack", NULL),
148 SND_SOC_DAPM_MIC("AMIC", NULL),
149 SND_SOC_DAPM_MIC("DMIC", NULL),
150};
151
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200152static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
153{
154 return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
155}
156
Nicolin Chen708b4352014-07-30 19:27:38 +0800157static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
158 struct snd_pcm_hw_params *params)
159{
Kuninori Morimoto9f5f0782020-07-20 10:18:38 +0900160 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Nicolin Chen708b4352014-07-30 19:27:38 +0800161 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
162 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800163 struct codec_priv *codec_priv = &priv->codec_priv;
Nicolin Chen708b4352014-07-30 19:27:38 +0800164 struct cpu_priv *cpu_priv = &priv->cpu_priv;
165 struct device *dev = rtd->card->dev;
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800166 unsigned int pll_out;
Nicolin Chen708b4352014-07-30 19:27:38 +0800167 int ret;
168
169 priv->sample_rate = params_rate(params);
170 priv->sample_format = params_format(params);
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800171 priv->streams |= BIT(substream->stream);
Nicolin Chen708b4352014-07-30 19:27:38 +0800172
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800173 if (fsl_asoc_card_is_ac97(priv))
Nicolin Chen708b4352014-07-30 19:27:38 +0800174 return 0;
175
176 /* Specific configurations of DAIs starts from here */
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900177 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), cpu_priv->sysclk_id[tx],
Nicolin Chen708b4352014-07-30 19:27:38 +0800178 cpu_priv->sysclk_freq[tx],
179 cpu_priv->sysclk_dir[tx]);
Nicolin Chen758a3b02017-09-07 22:27:33 -0700180 if (ret && ret != -ENOTSUPP) {
Nicolin Chen708b4352014-07-30 19:27:38 +0800181 dev_err(dev, "failed to set sysclk for cpu dai\n");
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800182 goto fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800183 }
184
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700185 if (cpu_priv->slot_width) {
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900186 ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2,
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700187 cpu_priv->slot_width);
Nicolin Chen758a3b02017-09-07 22:27:33 -0700188 if (ret && ret != -ENOTSUPP) {
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700189 dev_err(dev, "failed to set TDM slot for cpu dai\n");
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800190 goto fail;
191 }
192 }
193
194 /* Specific configuration for PLL */
195 if (codec_priv->pll_id && codec_priv->fll_id) {
196 if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
197 pll_out = priv->sample_rate * 384;
198 else
199 pll_out = priv->sample_rate * 256;
200
201 ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
202 codec_priv->pll_id,
203 codec_priv->mclk_id,
204 codec_priv->mclk_freq, pll_out);
205 if (ret) {
206 dev_err(dev, "failed to start FLL: %d\n", ret);
207 goto fail;
208 }
209
210 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
211 codec_priv->fll_id,
212 pll_out, SND_SOC_CLOCK_IN);
213
214 if (ret && ret != -ENOTSUPP) {
215 dev_err(dev, "failed to set SYSCLK: %d\n", ret);
216 goto fail;
217 }
218 }
219
220 return 0;
221
222fail:
223 priv->streams &= ~BIT(substream->stream);
224 return ret;
225}
226
227static int fsl_asoc_card_hw_free(struct snd_pcm_substream *substream)
228{
229 struct snd_soc_pcm_runtime *rtd = substream->private_data;
230 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
231 struct codec_priv *codec_priv = &priv->codec_priv;
232 struct device *dev = rtd->card->dev;
233 int ret;
234
235 priv->streams &= ~BIT(substream->stream);
236
237 if (!priv->streams && codec_priv->pll_id && codec_priv->fll_id) {
238 /* Force freq to be 0 to avoid error message in codec */
239 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
240 codec_priv->mclk_id,
241 0,
242 SND_SOC_CLOCK_IN);
243 if (ret) {
244 dev_err(dev, "failed to switch away from FLL: %d\n", ret);
245 return ret;
246 }
247
248 ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
249 codec_priv->pll_id, 0, 0, 0);
250 if (ret && ret != -ENOTSUPP) {
251 dev_err(dev, "failed to stop FLL: %d\n", ret);
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700252 return ret;
253 }
254 }
255
Nicolin Chen708b4352014-07-30 19:27:38 +0800256 return 0;
257}
258
Julia Lawallddba7fa2016-10-15 16:55:50 +0200259static const struct snd_soc_ops fsl_asoc_card_ops = {
Nicolin Chen708b4352014-07-30 19:27:38 +0800260 .hw_params = fsl_asoc_card_hw_params,
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800261 .hw_free = fsl_asoc_card_hw_free,
Nicolin Chen708b4352014-07-30 19:27:38 +0800262};
263
264static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
265 struct snd_pcm_hw_params *params)
266{
267 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
268 struct snd_interval *rate;
269 struct snd_mask *mask;
270
271 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
272 rate->max = rate->min = priv->asrc_rate;
273
274 mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
275 snd_mask_none(mask);
Takashi Iwaiebc22af2018-07-25 23:17:20 +0200276 snd_mask_set_format(mask, priv->asrc_format);
Nicolin Chen708b4352014-07-30 19:27:38 +0800277
278 return 0;
279}
280
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900281SND_SOC_DAILINK_DEFS(hifi,
282 DAILINK_COMP_ARRAY(COMP_EMPTY()),
Kuninori Morimoto9998d3e2019-06-28 10:47:18 +0900283 DAILINK_COMP_ARRAY(COMP_EMPTY()),
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900284 DAILINK_COMP_ARRAY(COMP_EMPTY()));
285
286SND_SOC_DAILINK_DEFS(hifi_fe,
287 DAILINK_COMP_ARRAY(COMP_EMPTY()),
Kuninori Morimoto9998d3e2019-06-28 10:47:18 +0900288 DAILINK_COMP_ARRAY(COMP_DUMMY()),
289 DAILINK_COMP_ARRAY(COMP_EMPTY()));
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900290
291SND_SOC_DAILINK_DEFS(hifi_be,
292 DAILINK_COMP_ARRAY(COMP_EMPTY()),
293 DAILINK_COMP_ARRAY(COMP_EMPTY()),
294 DAILINK_COMP_ARRAY(COMP_DUMMY()));
295
Nicolin Chen708b4352014-07-30 19:27:38 +0800296static struct snd_soc_dai_link fsl_asoc_card_dai[] = {
297 /* Default ASoC DAI Link*/
298 {
299 .name = "HiFi",
300 .stream_name = "HiFi",
301 .ops = &fsl_asoc_card_ops,
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900302 SND_SOC_DAILINK_REG(hifi),
Nicolin Chen708b4352014-07-30 19:27:38 +0800303 },
304 /* DPCM Link between Front-End and Back-End (Optional) */
305 {
306 .name = "HiFi-ASRC-FE",
307 .stream_name = "HiFi-ASRC-FE",
Nicolin Chen708b4352014-07-30 19:27:38 +0800308 .dpcm_playback = 1,
309 .dpcm_capture = 1,
310 .dynamic = 1,
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900311 SND_SOC_DAILINK_REG(hifi_fe),
Nicolin Chen708b4352014-07-30 19:27:38 +0800312 },
313 {
314 .name = "HiFi-ASRC-BE",
315 .stream_name = "HiFi-ASRC-BE",
Nicolin Chen708b4352014-07-30 19:27:38 +0800316 .be_hw_params_fixup = be_hw_params_fixup,
317 .ops = &fsl_asoc_card_ops,
318 .dpcm_playback = 1,
319 .dpcm_capture = 1,
320 .no_pcm = 1,
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900321 SND_SOC_DAILINK_REG(hifi_be),
Nicolin Chen708b4352014-07-30 19:27:38 +0800322 },
323};
324
Nicolin Chen708b4352014-07-30 19:27:38 +0800325static int fsl_asoc_card_audmux_init(struct device_node *np,
326 struct fsl_asoc_card_priv *priv)
327{
328 struct device *dev = &priv->pdev->dev;
329 u32 int_ptcr = 0, ext_ptcr = 0;
330 int int_port, ext_port;
331 int ret;
332
333 ret = of_property_read_u32(np, "mux-int-port", &int_port);
334 if (ret) {
335 dev_err(dev, "mux-int-port missing or invalid\n");
336 return ret;
337 }
338 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
339 if (ret) {
340 dev_err(dev, "mux-ext-port missing or invalid\n");
341 return ret;
342 }
343
344 /*
345 * The port numbering in the hardware manual starts at 1, while
346 * the AUDMUX API expects it starts at 0.
347 */
348 int_port--;
349 ext_port--;
350
351 /*
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200352 * Use asynchronous mode (6 wires) for all cases except AC97.
Nicolin Chen708b4352014-07-30 19:27:38 +0800353 * If only 4 wires are needed, just set SSI into
354 * synchronous mode and enable 4 PADs in IOMUX.
355 */
356 switch (priv->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
357 case SND_SOC_DAIFMT_CBM_CFM:
358 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
359 IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
360 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
361 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
362 IMX_AUDMUX_V2_PTCR_RFSDIR |
363 IMX_AUDMUX_V2_PTCR_RCLKDIR |
364 IMX_AUDMUX_V2_PTCR_TFSDIR |
365 IMX_AUDMUX_V2_PTCR_TCLKDIR;
366 break;
367 case SND_SOC_DAIFMT_CBM_CFS:
368 int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
369 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
370 IMX_AUDMUX_V2_PTCR_RCLKDIR |
371 IMX_AUDMUX_V2_PTCR_TCLKDIR;
372 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
373 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
374 IMX_AUDMUX_V2_PTCR_RFSDIR |
375 IMX_AUDMUX_V2_PTCR_TFSDIR;
376 break;
377 case SND_SOC_DAIFMT_CBS_CFM:
378 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
379 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
380 IMX_AUDMUX_V2_PTCR_RFSDIR |
381 IMX_AUDMUX_V2_PTCR_TFSDIR;
382 ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
383 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
384 IMX_AUDMUX_V2_PTCR_RCLKDIR |
385 IMX_AUDMUX_V2_PTCR_TCLKDIR;
386 break;
387 case SND_SOC_DAIFMT_CBS_CFS:
388 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
389 IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
390 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
391 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
392 IMX_AUDMUX_V2_PTCR_RFSDIR |
393 IMX_AUDMUX_V2_PTCR_RCLKDIR |
394 IMX_AUDMUX_V2_PTCR_TFSDIR |
395 IMX_AUDMUX_V2_PTCR_TCLKDIR;
396 break;
397 default:
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200398 if (!fsl_asoc_card_is_ac97(priv))
399 return -EINVAL;
400 }
401
402 if (fsl_asoc_card_is_ac97(priv)) {
403 int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
404 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
405 IMX_AUDMUX_V2_PTCR_TCLKDIR;
406 ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
407 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
408 IMX_AUDMUX_V2_PTCR_TFSDIR;
Nicolin Chen708b4352014-07-30 19:27:38 +0800409 }
410
411 /* Asynchronous mode can not be set along with RCLKDIR */
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200412 if (!fsl_asoc_card_is_ac97(priv)) {
413 unsigned int pdcr =
414 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
415
416 ret = imx_audmux_v2_configure_port(int_port, 0,
417 pdcr);
418 if (ret) {
419 dev_err(dev, "audmux internal port setup failed\n");
420 return ret;
421 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800422 }
423
424 ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
425 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
426 if (ret) {
427 dev_err(dev, "audmux internal port setup failed\n");
428 return ret;
429 }
430
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200431 if (!fsl_asoc_card_is_ac97(priv)) {
432 unsigned int pdcr =
433 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
434
435 ret = imx_audmux_v2_configure_port(ext_port, 0,
436 pdcr);
437 if (ret) {
438 dev_err(dev, "audmux external port setup failed\n");
439 return ret;
440 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800441 }
442
443 ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
444 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
445 if (ret) {
446 dev_err(dev, "audmux external port setup failed\n");
447 return ret;
448 }
449
450 return 0;
451}
452
Shengjiu Wang3b171192020-07-15 22:09:39 +0800453static int hp_jack_event(struct notifier_block *nb, unsigned long event,
454 void *data)
455{
456 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
457 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
458
459 if (event & SND_JACK_HEADPHONE)
460 /* Disable speaker if headphone is plugged in */
461 snd_soc_dapm_disable_pin(dapm, "Ext Spk");
462 else
463 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
464
465 return 0;
466}
467
468static struct notifier_block hp_jack_nb = {
469 .notifier_call = hp_jack_event,
470};
471
472static int mic_jack_event(struct notifier_block *nb, unsigned long event,
473 void *data)
474{
475 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
476 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
477
478 if (event & SND_JACK_MICROPHONE)
479 /* Disable dmic if microphone is plugged in */
480 snd_soc_dapm_disable_pin(dapm, "DMIC");
481 else
482 snd_soc_dapm_enable_pin(dapm, "DMIC");
483
484 return 0;
485}
486
487static struct notifier_block mic_jack_nb = {
488 .notifier_call = mic_jack_event,
489};
490
Nicolin Chen708b4352014-07-30 19:27:38 +0800491static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
492{
493 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
Vinod Koulabd657b2015-11-20 22:45:20 +0530494 struct snd_soc_pcm_runtime *rtd = list_first_entry(
495 &card->rtd_list, struct snd_soc_pcm_runtime, list);
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900496 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
Nicolin Chen708b4352014-07-30 19:27:38 +0800497 struct codec_priv *codec_priv = &priv->codec_priv;
498 struct device *dev = card->dev;
499 int ret;
500
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200501 if (fsl_asoc_card_is_ac97(priv)) {
502#if IS_ENABLED(CONFIG_SND_AC97_CODEC)
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900503 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
Kuninori Morimoto845f80c2017-12-05 04:23:21 +0000504 struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200505
506 /*
507 * Use slots 3/4 for S/PDIF so SSI won't try to enable
508 * other slots and send some samples there
509 * due to SLOTREQ bits for S/PDIF received from codec
510 */
511 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
512 AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
513#endif
514
515 return 0;
516 }
517
Nicolin Chen708b4352014-07-30 19:27:38 +0800518 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
519 codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
Nicolin Chen758a3b02017-09-07 22:27:33 -0700520 if (ret && ret != -ENOTSUPP) {
Nicolin Chen708b4352014-07-30 19:27:38 +0800521 dev_err(dev, "failed to set sysclk in %s\n", __func__);
522 return ret;
523 }
524
525 return 0;
526}
527
528static int fsl_asoc_card_probe(struct platform_device *pdev)
529{
530 struct device_node *cpu_np, *codec_np, *asrc_np;
531 struct device_node *np = pdev->dev.of_node;
532 struct platform_device *asrc_pdev = NULL;
Shengjiu Wang08b54b5e2020-07-21 11:41:49 +0800533 struct device_node *bitclkmaster = NULL;
534 struct device_node *framemaster = NULL;
Nicolin Chen708b4352014-07-30 19:27:38 +0800535 struct platform_device *cpu_pdev;
536 struct fsl_asoc_card_priv *priv;
Shengjiu Wang039652a2020-06-17 12:48:25 +0800537 struct device *codec_dev = NULL;
Nicolin Chen114bb132015-08-12 13:06:12 -0700538 const char *codec_dai_name;
Shengjiu Wang039652a2020-06-17 12:48:25 +0800539 const char *codec_dev_name;
Shengjiu Wang08b54b5e2020-07-21 11:41:49 +0800540 unsigned int daifmt;
Nicolin Chen708b4352014-07-30 19:27:38 +0800541 u32 width;
542 int ret;
543
544 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
545 if (!priv)
546 return -ENOMEM;
547
548 cpu_np = of_parse_phandle(np, "audio-cpu", 0);
549 /* Give a chance to old DT binding */
550 if (!cpu_np)
551 cpu_np = of_parse_phandle(np, "ssi-controller", 0);
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200552 if (!cpu_np) {
553 dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
Nicolin Chen708b4352014-07-30 19:27:38 +0800554 ret = -EINVAL;
555 goto fail;
556 }
557
558 cpu_pdev = of_find_device_by_node(cpu_np);
559 if (!cpu_pdev) {
560 dev_err(&pdev->dev, "failed to find CPU DAI device\n");
561 ret = -EINVAL;
562 goto fail;
563 }
564
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200565 codec_np = of_parse_phandle(np, "audio-codec", 0);
Shengjiu Wang039652a2020-06-17 12:48:25 +0800566 if (codec_np) {
567 struct platform_device *codec_pdev;
568 struct i2c_client *codec_i2c;
569
570 codec_i2c = of_find_i2c_device_by_node(codec_np);
571 if (codec_i2c) {
572 codec_dev = &codec_i2c->dev;
573 codec_dev_name = codec_i2c->name;
574 }
575 if (!codec_dev) {
576 codec_pdev = of_find_device_by_node(codec_np);
577 if (codec_pdev) {
578 codec_dev = &codec_pdev->dev;
579 codec_dev_name = codec_pdev->name;
580 }
581 }
582 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800583
584 asrc_np = of_parse_phandle(np, "audio-asrc", 0);
585 if (asrc_np)
586 asrc_pdev = of_find_device_by_node(asrc_np);
587
588 /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200589 if (codec_dev) {
Shengjiu Wang039652a2020-06-17 12:48:25 +0800590 struct clk *codec_clk = clk_get(codec_dev, NULL);
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200591
592 if (!IS_ERR(codec_clk)) {
593 priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
594 clk_put(codec_clk);
595 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800596 }
597
598 /* Default sample rate and format, will be updated in hw_params() */
599 priv->sample_rate = 44100;
600 priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
601
602 /* Assign a default DAI format, and allow each card to overwrite it */
603 priv->dai_fmt = DAI_FMT_BASE;
604
Shengjiu Wang039652a2020-06-17 12:48:25 +0800605 memcpy(priv->dai_link, fsl_asoc_card_dai,
606 sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
607
608 priv->card.dapm_routes = audio_map;
609 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
Nicolin Chen708b4352014-07-30 19:27:38 +0800610 /* Diversify the card configurations */
611 if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
Nicolin Chen114bb132015-08-12 13:06:12 -0700612 codec_dai_name = "cs42888";
Nicolin Chen708b4352014-07-30 19:27:38 +0800613 priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
614 priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
615 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
616 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700617 priv->cpu_priv.slot_width = 32;
Nicolin Chen708b4352014-07-30 19:27:38 +0800618 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
Felipe F. Tonello57e756d2016-01-29 11:01:00 +0000619 } else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {
620 codec_dai_name = "cs4271-hifi";
621 priv->codec_priv.mclk_id = CS427x_SYSCLK_MCLK;
622 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Nicolin Chen708b4352014-07-30 19:27:38 +0800623 } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
Nicolin Chen114bb132015-08-12 13:06:12 -0700624 codec_dai_name = "sgtl5000";
Nicolin Chen708b4352014-07-30 19:27:38 +0800625 priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
626 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Matthias Schifferb5074752020-08-21 09:11:53 +0200627 } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic32x4")) {
628 codec_dai_name = "tlv320aic32x4-hifi";
629 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Nicolin Chen708b4352014-07-30 19:27:38 +0800630 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
Nicolin Chen114bb132015-08-12 13:06:12 -0700631 codec_dai_name = "wm8962";
Nicolin Chen708b4352014-07-30 19:27:38 +0800632 priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
633 priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
634 priv->codec_priv.pll_id = WM8962_FLL;
635 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Zidan Wang50e0ee02015-08-14 19:11:09 +0800636 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
637 codec_dai_name = "wm8960-hifi";
Zidan Wang50e0ee02015-08-14 19:11:09 +0800638 priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO;
639 priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO;
640 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200641 } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
642 codec_dai_name = "ac97-hifi";
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200643 priv->dai_fmt = SND_SOC_DAIFMT_AC97;
Shengjiu Wang039652a2020-06-17 12:48:25 +0800644 priv->card.dapm_routes = audio_map_ac97;
645 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_ac97);
646 } else if (of_device_is_compatible(np, "fsl,imx-audio-mqs")) {
647 codec_dai_name = "fsl-mqs-dai";
Shengjiu Wang039652a2020-06-17 12:48:25 +0800648 priv->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
649 SND_SOC_DAIFMT_CBS_CFS |
650 SND_SOC_DAIFMT_NB_NF;
651 priv->dai_link[1].dpcm_capture = 0;
652 priv->dai_link[2].dpcm_capture = 0;
653 priv->card.dapm_routes = audio_map_tx;
654 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
Shengjiu Wang3cd99022020-06-23 14:52:46 +0800655 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8524")) {
656 codec_dai_name = "wm8524-hifi";
Shengjiu Wang3cd99022020-06-23 14:52:46 +0800657 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
658 priv->dai_link[1].dpcm_capture = 0;
659 priv->dai_link[2].dpcm_capture = 0;
660 priv->cpu_priv.slot_width = 32;
661 priv->card.dapm_routes = audio_map_tx;
662 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
Shengjiu Wang77f1ff752020-11-30 11:57:47 +0800663 } else if (of_device_is_compatible(np, "fsl,imx-audio-si476x")) {
664 codec_dai_name = "si476x-codec";
665 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
666 priv->card.dapm_routes = audio_map_rx;
667 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);
Nicolin Chen708b4352014-07-30 19:27:38 +0800668 } else {
669 dev_err(&pdev->dev, "unknown Device Tree compatible\n");
Maciej S. Szmigiero6bd3c6f2015-08-31 17:07:12 +0200670 ret = -EINVAL;
671 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800672 }
673
Shengjiu Wang08b54b5e2020-07-21 11:41:49 +0800674 /* Format info from DT is optional. */
675 daifmt = snd_soc_of_parse_daifmt(np, NULL,
676 &bitclkmaster, &framemaster);
677 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
678 if (bitclkmaster || framemaster) {
679 if (codec_np == bitclkmaster)
680 daifmt |= (codec_np == framemaster) ?
681 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
682 else
683 daifmt |= (codec_np == framemaster) ?
684 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
685
686 /* Override dai_fmt with value from DT */
687 priv->dai_fmt = daifmt;
688 }
689
690 /* Change direction according to format */
691 if (priv->dai_fmt & SND_SOC_DAIFMT_CBM_CFM) {
692 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_IN;
693 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_IN;
694 }
695
696 of_node_put(bitclkmaster);
697 of_node_put(framemaster);
698
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200699 if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) {
700 dev_err(&pdev->dev, "failed to find codec device\n");
Shengjiu Wange396dec2020-06-04 14:25:30 +0800701 ret = -EPROBE_DEFER;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200702 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800703 }
704
705 /* Common settings for corresponding Freescale CPU DAI driver */
Rob Herring1d52a742018-12-05 13:50:49 -0600706 if (of_node_name_eq(cpu_np, "ssi")) {
Nicolin Chen708b4352014-07-30 19:27:38 +0800707 /* Only SSI needs to configure AUDMUX */
708 ret = fsl_asoc_card_audmux_init(np, priv);
709 if (ret) {
710 dev_err(&pdev->dev, "failed to init audmux\n");
Shengjiu Wang5f376712014-08-18 16:38:39 +0800711 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800712 }
Rob Herring1d52a742018-12-05 13:50:49 -0600713 } else if (of_node_name_eq(cpu_np, "esai")) {
Shengjiu Wanga8fd5ca2020-08-10 16:11:43 +0800714 struct clk *esai_clk = clk_get(&cpu_pdev->dev, "extal");
715
716 if (!IS_ERR(esai_clk)) {
717 priv->cpu_priv.sysclk_freq[TX] = clk_get_rate(esai_clk);
718 priv->cpu_priv.sysclk_freq[RX] = clk_get_rate(esai_clk);
719 clk_put(esai_clk);
720 } else if (PTR_ERR(esai_clk) == -EPROBE_DEFER) {
721 ret = -EPROBE_DEFER;
722 goto asrc_fail;
723 }
724
Nicolin Chen708b4352014-07-30 19:27:38 +0800725 priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
726 priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
Rob Herring1d52a742018-12-05 13:50:49 -0600727 } else if (of_node_name_eq(cpu_np, "sai")) {
Nicolin Chen708b4352014-07-30 19:27:38 +0800728 priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
729 priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
730 }
731
Nicolin Chen708b4352014-07-30 19:27:38 +0800732 /* Initialize sound card */
733 priv->pdev = pdev;
734 priv->card.dev = &pdev->dev;
Shengjiu Wang039652a2020-06-17 12:48:25 +0800735 ret = snd_soc_of_parse_card_name(&priv->card, "model");
736 if (ret) {
737 snprintf(priv->name, sizeof(priv->name), "%s-audio",
738 fsl_asoc_card_is_ac97(priv) ? "ac97" : codec_dev_name);
739 priv->card.name = priv->name;
740 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800741 priv->card.dai_link = priv->dai_link;
Nicolin Chen708b4352014-07-30 19:27:38 +0800742 priv->card.late_probe = fsl_asoc_card_late_probe;
Nicolin Chen708b4352014-07-30 19:27:38 +0800743 priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
744 priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
745
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800746 /* Drop the second half of DAPM routes -- ASRC */
747 if (!asrc_pdev)
748 priv->card.num_dapm_routes /= 2;
749
Shengjiu Wang039652a2020-06-17 12:48:25 +0800750 if (of_property_read_bool(np, "audio-routing")) {
751 ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
752 if (ret) {
753 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
754 goto asrc_fail;
755 }
Nicolin Chen31858782015-02-14 17:22:50 -0800756 }
757
Nicolin Chen708b4352014-07-30 19:27:38 +0800758 /* Normal DAI Link */
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900759 priv->dai_link[0].cpus->of_node = cpu_np;
760 priv->dai_link[0].codecs->dai_name = codec_dai_name;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200761
762 if (!fsl_asoc_card_is_ac97(priv))
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900763 priv->dai_link[0].codecs->of_node = codec_np;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200764 else {
765 u32 idx;
766
767 ret = of_property_read_u32(cpu_np, "cell-index", &idx);
768 if (ret) {
769 dev_err(&pdev->dev,
770 "cannot get CPU index property\n");
771 goto asrc_fail;
772 }
773
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900774 priv->dai_link[0].codecs->name =
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200775 devm_kasprintf(&pdev->dev, GFP_KERNEL,
776 "ac97-codec.%u",
777 (unsigned int)idx);
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900778 if (!priv->dai_link[0].codecs->name) {
Arvind Yadav7add71b2017-09-21 10:50:03 +0530779 ret = -ENOMEM;
780 goto asrc_fail;
781 }
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200782 }
783
Kuninori Morimoto9998d3e2019-06-28 10:47:18 +0900784 priv->dai_link[0].platforms->of_node = cpu_np;
Nicolin Chen708b4352014-07-30 19:27:38 +0800785 priv->dai_link[0].dai_fmt = priv->dai_fmt;
786 priv->card.num_links = 1;
787
788 if (asrc_pdev) {
789 /* DPCM DAI Links only if ASRC exsits */
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900790 priv->dai_link[1].cpus->of_node = asrc_np;
Kuninori Morimoto9998d3e2019-06-28 10:47:18 +0900791 priv->dai_link[1].platforms->of_node = asrc_np;
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900792 priv->dai_link[2].codecs->dai_name = codec_dai_name;
793 priv->dai_link[2].codecs->of_node = codec_np;
794 priv->dai_link[2].codecs->name =
795 priv->dai_link[0].codecs->name;
796 priv->dai_link[2].cpus->of_node = cpu_np;
Nicolin Chen708b4352014-07-30 19:27:38 +0800797 priv->dai_link[2].dai_fmt = priv->dai_fmt;
798 priv->card.num_links = 3;
799
800 ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
801 &priv->asrc_rate);
802 if (ret) {
803 dev_err(&pdev->dev, "failed to get output rate\n");
804 ret = -EINVAL;
Shengjiu Wang5f376712014-08-18 16:38:39 +0800805 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800806 }
807
Shengjiu Wang859e3642020-04-16 20:25:33 +0800808 ret = of_property_read_u32(asrc_np, "fsl,asrc-format",
809 &priv->asrc_format);
Nicolin Chen708b4352014-07-30 19:27:38 +0800810 if (ret) {
Shengjiu Wang859e3642020-04-16 20:25:33 +0800811 /* Fallback to old binding; translate to asrc_format */
812 ret = of_property_read_u32(asrc_np, "fsl,asrc-width",
813 &width);
814 if (ret) {
815 dev_err(&pdev->dev,
816 "failed to decide output format\n");
817 goto asrc_fail;
818 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800819
Shengjiu Wang859e3642020-04-16 20:25:33 +0800820 if (width == 24)
821 priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
822 else
823 priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
824 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800825 }
826
827 /* Finish card registering */
828 platform_set_drvdata(pdev, priv);
829 snd_soc_card_set_drvdata(&priv->card, priv);
830
831 ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
Shengjiu Wang3b171192020-07-15 22:09:39 +0800832 if (ret) {
833 if (ret != -EPROBE_DEFER)
834 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
835 goto asrc_fail;
836 }
837
838 /*
839 * Properties "hp-det-gpio" and "mic-det-gpio" are optional, and
840 * asoc_simple_init_jack uses these properties for creating
841 * Headphone Jack and Microphone Jack.
842 *
843 * The notifier is initialized in snd_soc_card_jack_new(), then
844 * snd_soc_jack_notifier_register can be called.
845 */
846 if (of_property_read_bool(np, "hp-det-gpio")) {
847 ret = asoc_simple_init_jack(&priv->card, &priv->hp_jack,
848 1, NULL, "Headphone Jack");
849 if (ret)
850 goto asrc_fail;
851
852 snd_soc_jack_notifier_register(&priv->hp_jack.jack, &hp_jack_nb);
853 }
854
855 if (of_property_read_bool(np, "mic-det-gpio")) {
856 ret = asoc_simple_init_jack(&priv->card, &priv->mic_jack,
857 0, NULL, "Mic Jack");
858 if (ret)
859 goto asrc_fail;
860
861 snd_soc_jack_notifier_register(&priv->mic_jack.jack, &mic_jack_nb);
862 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800863
Shengjiu Wang5f376712014-08-18 16:38:39 +0800864asrc_fail:
865 of_node_put(asrc_np);
Nicolin Chen708b4352014-07-30 19:27:38 +0800866 of_node_put(codec_np);
wen yang11907e9d2019-02-02 14:53:16 +0000867 put_device(&cpu_pdev->dev);
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200868fail:
Nicolin Chen708b4352014-07-30 19:27:38 +0800869 of_node_put(cpu_np);
870
871 return ret;
872}
873
874static const struct of_device_id fsl_asoc_card_dt_ids[] = {
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200875 { .compatible = "fsl,imx-audio-ac97", },
Nicolin Chen708b4352014-07-30 19:27:38 +0800876 { .compatible = "fsl,imx-audio-cs42888", },
Felipe F. Tonello57e756d2016-01-29 11:01:00 +0000877 { .compatible = "fsl,imx-audio-cs427x", },
Matthias Schifferb5074752020-08-21 09:11:53 +0200878 { .compatible = "fsl,imx-audio-tlv320aic32x4", },
Nicolin Chen708b4352014-07-30 19:27:38 +0800879 { .compatible = "fsl,imx-audio-sgtl5000", },
880 { .compatible = "fsl,imx-audio-wm8962", },
Zidan Wang50e0ee02015-08-14 19:11:09 +0800881 { .compatible = "fsl,imx-audio-wm8960", },
Shengjiu Wang039652a2020-06-17 12:48:25 +0800882 { .compatible = "fsl,imx-audio-mqs", },
Shengjiu Wang3cd99022020-06-23 14:52:46 +0800883 { .compatible = "fsl,imx-audio-wm8524", },
Shengjiu Wang77f1ff752020-11-30 11:57:47 +0800884 { .compatible = "fsl,imx-audio-si476x", },
Nicolin Chen708b4352014-07-30 19:27:38 +0800885 {}
886};
Luis de Bethencourt5226f232015-09-03 12:57:47 +0200887MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
Nicolin Chen708b4352014-07-30 19:27:38 +0800888
889static struct platform_driver fsl_asoc_card_driver = {
890 .probe = fsl_asoc_card_probe,
891 .driver = {
892 .name = "fsl-asoc-card",
893 .pm = &snd_soc_pm_ops,
894 .of_match_table = fsl_asoc_card_dt_ids,
895 },
896};
897module_platform_driver(fsl_asoc_card_driver);
898
899MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
900MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
901MODULE_ALIAS("platform:fsl-asoc-card");
902MODULE_LICENSE("GPL");