blob: 14d2956d0da3e74c7f32c27fba0335f81c949404 [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"
Shengjiu Wangefd0b162021-03-17 21:05:02 +080028#include "../codecs/wm8994.h"
Nicolin Chen708b4352014-07-30 19:27:38 +080029
Felipe F. Tonello57e756d2016-01-29 11:01:00 +000030#define CS427x_SYSCLK_MCLK 0
31
Nicolin Chen708b4352014-07-30 19:27:38 +080032#define RX 0
33#define TX 1
34
35/* Default DAI format without Master and Slave flag */
36#define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
37
38/**
Pierre-Louis Bossart31deacf2020-07-02 14:21:38 -050039 * struct codec_priv - CODEC private data
Nicolin Chen708b4352014-07-30 19:27:38 +080040 * @mclk_freq: Clock rate of MCLK
Shengjiu Wangefd0b162021-03-17 21:05:02 +080041 * @free_freq: Clock rate of MCLK for hw_free()
Nicolin Chen708b4352014-07-30 19:27:38 +080042 * @mclk_id: MCLK (or main clock) id for set_sysclk()
43 * @fll_id: FLL (or secordary clock) id for set_sysclk()
44 * @pll_id: PLL id for set_pll()
45 */
46struct codec_priv {
47 unsigned long mclk_freq;
Shengjiu Wangefd0b162021-03-17 21:05:02 +080048 unsigned long free_freq;
Nicolin Chen708b4352014-07-30 19:27:38 +080049 u32 mclk_id;
50 u32 fll_id;
51 u32 pll_id;
52};
53
54/**
Pierre-Louis Bossart31deacf2020-07-02 14:21:38 -050055 * struct cpu_priv - CPU private data
56 * @sysclk_freq: SYSCLK rates for set_sysclk()
57 * @sysclk_dir: SYSCLK directions for set_sysclk()
58 * @sysclk_id: SYSCLK ids for set_sysclk()
Nicolin Chencb3fc1f2014-10-24 16:48:13 -070059 * @slot_width: Slot width of each frame
Nicolin Chen708b4352014-07-30 19:27:38 +080060 *
61 * Note: [1] for tx and [0] for rx
62 */
63struct cpu_priv {
64 unsigned long sysclk_freq[2];
65 u32 sysclk_dir[2];
66 u32 sysclk_id[2];
Nicolin Chencb3fc1f2014-10-24 16:48:13 -070067 u32 slot_width;
Nicolin Chen708b4352014-07-30 19:27:38 +080068};
69
70/**
Pierre-Louis Bossart31deacf2020-07-02 14:21:38 -050071 * struct fsl_asoc_card_priv - Freescale Generic ASOC card private data
72 * @dai_link: DAI link structure including normal one and DPCM link
Shengjiu Wang3b171192020-07-15 22:09:39 +080073 * @hp_jack: Headphone Jack structure
74 * @mic_jack: Microphone Jack structure
Nicolin Chen708b4352014-07-30 19:27:38 +080075 * @pdev: platform device pointer
76 * @codec_priv: CODEC private data
77 * @cpu_priv: CPU private data
78 * @card: ASoC card structure
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +080079 * @streams: Mask of current active streams
Nicolin Chen708b4352014-07-30 19:27:38 +080080 * @sample_rate: Current sample rate
81 * @sample_format: Current sample format
82 * @asrc_rate: ASRC sample rate used by Back-Ends
83 * @asrc_format: ASRC sample format used by Back-Ends
84 * @dai_fmt: DAI format between CPU and CODEC
85 * @name: Card name
86 */
87
88struct fsl_asoc_card_priv {
89 struct snd_soc_dai_link dai_link[3];
Shengjiu Wang3b171192020-07-15 22:09:39 +080090 struct asoc_simple_jack hp_jack;
91 struct asoc_simple_jack mic_jack;
Nicolin Chen708b4352014-07-30 19:27:38 +080092 struct platform_device *pdev;
93 struct codec_priv codec_priv;
94 struct cpu_priv cpu_priv;
95 struct snd_soc_card card;
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +080096 u8 streams;
Nicolin Chen708b4352014-07-30 19:27:38 +080097 u32 sample_rate;
Fabio Estevamfc734c242018-02-11 19:53:18 -020098 snd_pcm_format_t sample_format;
Nicolin Chen708b4352014-07-30 19:27:38 +080099 u32 asrc_rate;
Fabio Estevamfc734c242018-02-11 19:53:18 -0200100 snd_pcm_format_t asrc_format;
Nicolin Chen708b4352014-07-30 19:27:38 +0800101 u32 dai_fmt;
102 char name[32];
103};
104
Pierre-Louis Bossart31deacf2020-07-02 14:21:38 -0500105/*
Lee Jones1b582142020-07-15 16:00:09 +0100106 * This dapm route map exists for DPCM link only.
Nicolin Chen708b4352014-07-30 19:27:38 +0800107 * The other routes shall go through Device Tree.
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800108 *
109 * Note: keep all ASRC routes in the second half
110 * to drop them easily for non-ASRC cases.
Nicolin Chen708b4352014-07-30 19:27:38 +0800111 */
112static const struct snd_soc_dapm_route audio_map[] = {
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800113 /* 1st half -- Normal DAPM routes */
Nicolin Chen708b4352014-07-30 19:27:38 +0800114 {"Playback", NULL, "CPU-Playback"},
Nicolin Chen708b4352014-07-30 19:27:38 +0800115 {"CPU-Capture", NULL, "Capture"},
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800116 /* 2nd half -- ASRC DAPM routes */
117 {"CPU-Playback", NULL, "ASRC-Playback"},
118 {"ASRC-Capture", NULL, "CPU-Capture"},
Nicolin Chen708b4352014-07-30 19:27:38 +0800119};
120
Maciej S. Szmigiero25e5ef92015-12-20 21:34:29 +0100121static const struct snd_soc_dapm_route audio_map_ac97[] = {
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800122 /* 1st half -- Normal DAPM routes */
Maciej S. Szmigiero25e5ef92015-12-20 21:34:29 +0100123 {"Playback", NULL, "AC97 Playback"},
Maciej S. Szmigiero25e5ef92015-12-20 21:34:29 +0100124 {"AC97 Capture", NULL, "Capture"},
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800125 /* 2nd half -- ASRC DAPM routes */
126 {"AC97 Playback", NULL, "ASRC-Playback"},
127 {"ASRC-Capture", NULL, "AC97 Capture"},
Maciej S. Szmigiero25e5ef92015-12-20 21:34:29 +0100128};
129
Shengjiu Wang039652a2020-06-17 12:48:25 +0800130static const struct snd_soc_dapm_route audio_map_tx[] = {
131 /* 1st half -- Normal DAPM routes */
132 {"Playback", NULL, "CPU-Playback"},
133 /* 2nd half -- ASRC DAPM routes */
134 {"CPU-Playback", NULL, "ASRC-Playback"},
135};
136
Shengjiu Wang77f1ff752020-11-30 11:57:47 +0800137static const struct snd_soc_dapm_route audio_map_rx[] = {
138 /* 1st half -- Normal DAPM routes */
139 {"CPU-Capture", NULL, "Capture"},
140 /* 2nd half -- ASRC DAPM routes */
141 {"ASRC-Capture", NULL, "CPU-Capture"},
142};
143
Nicolin Chen708b4352014-07-30 19:27:38 +0800144/* Add all possible widgets into here without being redundant */
145static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
146 SND_SOC_DAPM_LINE("Line Out Jack", NULL),
147 SND_SOC_DAPM_LINE("Line In Jack", NULL),
148 SND_SOC_DAPM_HP("Headphone Jack", NULL),
149 SND_SOC_DAPM_SPK("Ext Spk", NULL),
150 SND_SOC_DAPM_MIC("Mic Jack", NULL),
151 SND_SOC_DAPM_MIC("AMIC", NULL),
152 SND_SOC_DAPM_MIC("DMIC", NULL),
153};
154
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200155static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
156{
157 return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
158}
159
Nicolin Chen708b4352014-07-30 19:27:38 +0800160static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
161 struct snd_pcm_hw_params *params)
162{
Kuninori Morimoto9f5f0782020-07-20 10:18:38 +0900163 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Nicolin Chen708b4352014-07-30 19:27:38 +0800164 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
165 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800166 struct codec_priv *codec_priv = &priv->codec_priv;
Nicolin Chen708b4352014-07-30 19:27:38 +0800167 struct cpu_priv *cpu_priv = &priv->cpu_priv;
168 struct device *dev = rtd->card->dev;
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800169 unsigned int pll_out;
Nicolin Chen708b4352014-07-30 19:27:38 +0800170 int ret;
171
172 priv->sample_rate = params_rate(params);
173 priv->sample_format = params_format(params);
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800174 priv->streams |= BIT(substream->stream);
Nicolin Chen708b4352014-07-30 19:27:38 +0800175
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800176 if (fsl_asoc_card_is_ac97(priv))
Nicolin Chen708b4352014-07-30 19:27:38 +0800177 return 0;
178
179 /* Specific configurations of DAIs starts from here */
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900180 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), cpu_priv->sysclk_id[tx],
Nicolin Chen708b4352014-07-30 19:27:38 +0800181 cpu_priv->sysclk_freq[tx],
182 cpu_priv->sysclk_dir[tx]);
Nicolin Chen758a3b02017-09-07 22:27:33 -0700183 if (ret && ret != -ENOTSUPP) {
Nicolin Chen708b4352014-07-30 19:27:38 +0800184 dev_err(dev, "failed to set sysclk for cpu dai\n");
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800185 goto fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800186 }
187
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700188 if (cpu_priv->slot_width) {
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900189 ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2,
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700190 cpu_priv->slot_width);
Nicolin Chen758a3b02017-09-07 22:27:33 -0700191 if (ret && ret != -ENOTSUPP) {
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700192 dev_err(dev, "failed to set TDM slot for cpu dai\n");
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800193 goto fail;
194 }
195 }
196
197 /* Specific configuration for PLL */
198 if (codec_priv->pll_id && codec_priv->fll_id) {
199 if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
200 pll_out = priv->sample_rate * 384;
201 else
202 pll_out = priv->sample_rate * 256;
203
204 ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
205 codec_priv->pll_id,
206 codec_priv->mclk_id,
207 codec_priv->mclk_freq, pll_out);
208 if (ret) {
209 dev_err(dev, "failed to start FLL: %d\n", ret);
210 goto fail;
211 }
212
213 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
214 codec_priv->fll_id,
215 pll_out, SND_SOC_CLOCK_IN);
216
217 if (ret && ret != -ENOTSUPP) {
218 dev_err(dev, "failed to set SYSCLK: %d\n", ret);
219 goto fail;
220 }
221 }
222
223 return 0;
224
225fail:
226 priv->streams &= ~BIT(substream->stream);
227 return ret;
228}
229
230static int fsl_asoc_card_hw_free(struct snd_pcm_substream *substream)
231{
232 struct snd_soc_pcm_runtime *rtd = substream->private_data;
233 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
234 struct codec_priv *codec_priv = &priv->codec_priv;
235 struct device *dev = rtd->card->dev;
236 int ret;
237
238 priv->streams &= ~BIT(substream->stream);
239
240 if (!priv->streams && codec_priv->pll_id && codec_priv->fll_id) {
Shengjiu Wangefd0b162021-03-17 21:05:02 +0800241 /* Force freq to be free_freq to avoid error message in codec */
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800242 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
243 codec_priv->mclk_id,
Shengjiu Wangefd0b162021-03-17 21:05:02 +0800244 codec_priv->free_freq,
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800245 SND_SOC_CLOCK_IN);
246 if (ret) {
247 dev_err(dev, "failed to switch away from FLL: %d\n", ret);
248 return ret;
249 }
250
251 ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
252 codec_priv->pll_id, 0, 0, 0);
253 if (ret && ret != -ENOTSUPP) {
254 dev_err(dev, "failed to stop FLL: %d\n", ret);
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700255 return ret;
256 }
257 }
258
Nicolin Chen708b4352014-07-30 19:27:38 +0800259 return 0;
260}
261
Julia Lawallddba7fa2016-10-15 16:55:50 +0200262static const struct snd_soc_ops fsl_asoc_card_ops = {
Nicolin Chen708b4352014-07-30 19:27:38 +0800263 .hw_params = fsl_asoc_card_hw_params,
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800264 .hw_free = fsl_asoc_card_hw_free,
Nicolin Chen708b4352014-07-30 19:27:38 +0800265};
266
267static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
268 struct snd_pcm_hw_params *params)
269{
270 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
271 struct snd_interval *rate;
272 struct snd_mask *mask;
273
274 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
275 rate->max = rate->min = priv->asrc_rate;
276
277 mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
278 snd_mask_none(mask);
Takashi Iwaiebc22af2018-07-25 23:17:20 +0200279 snd_mask_set_format(mask, priv->asrc_format);
Nicolin Chen708b4352014-07-30 19:27:38 +0800280
281 return 0;
282}
283
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900284SND_SOC_DAILINK_DEFS(hifi,
285 DAILINK_COMP_ARRAY(COMP_EMPTY()),
Kuninori Morimoto9998d3e2019-06-28 10:47:18 +0900286 DAILINK_COMP_ARRAY(COMP_EMPTY()),
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900287 DAILINK_COMP_ARRAY(COMP_EMPTY()));
288
289SND_SOC_DAILINK_DEFS(hifi_fe,
290 DAILINK_COMP_ARRAY(COMP_EMPTY()),
Kuninori Morimoto9998d3e2019-06-28 10:47:18 +0900291 DAILINK_COMP_ARRAY(COMP_DUMMY()),
292 DAILINK_COMP_ARRAY(COMP_EMPTY()));
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900293
294SND_SOC_DAILINK_DEFS(hifi_be,
295 DAILINK_COMP_ARRAY(COMP_EMPTY()),
296 DAILINK_COMP_ARRAY(COMP_EMPTY()),
297 DAILINK_COMP_ARRAY(COMP_DUMMY()));
298
Nicolin Chen708b4352014-07-30 19:27:38 +0800299static struct snd_soc_dai_link fsl_asoc_card_dai[] = {
300 /* Default ASoC DAI Link*/
301 {
302 .name = "HiFi",
303 .stream_name = "HiFi",
304 .ops = &fsl_asoc_card_ops,
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900305 SND_SOC_DAILINK_REG(hifi),
Nicolin Chen708b4352014-07-30 19:27:38 +0800306 },
307 /* DPCM Link between Front-End and Back-End (Optional) */
308 {
309 .name = "HiFi-ASRC-FE",
310 .stream_name = "HiFi-ASRC-FE",
Nicolin Chen708b4352014-07-30 19:27:38 +0800311 .dpcm_playback = 1,
312 .dpcm_capture = 1,
313 .dynamic = 1,
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900314 SND_SOC_DAILINK_REG(hifi_fe),
Nicolin Chen708b4352014-07-30 19:27:38 +0800315 },
316 {
317 .name = "HiFi-ASRC-BE",
318 .stream_name = "HiFi-ASRC-BE",
Nicolin Chen708b4352014-07-30 19:27:38 +0800319 .be_hw_params_fixup = be_hw_params_fixup,
320 .ops = &fsl_asoc_card_ops,
321 .dpcm_playback = 1,
322 .dpcm_capture = 1,
323 .no_pcm = 1,
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900324 SND_SOC_DAILINK_REG(hifi_be),
Nicolin Chen708b4352014-07-30 19:27:38 +0800325 },
326};
327
Nicolin Chen708b4352014-07-30 19:27:38 +0800328static int fsl_asoc_card_audmux_init(struct device_node *np,
329 struct fsl_asoc_card_priv *priv)
330{
331 struct device *dev = &priv->pdev->dev;
332 u32 int_ptcr = 0, ext_ptcr = 0;
333 int int_port, ext_port;
334 int ret;
335
336 ret = of_property_read_u32(np, "mux-int-port", &int_port);
337 if (ret) {
338 dev_err(dev, "mux-int-port missing or invalid\n");
339 return ret;
340 }
341 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
342 if (ret) {
343 dev_err(dev, "mux-ext-port missing or invalid\n");
344 return ret;
345 }
346
347 /*
348 * The port numbering in the hardware manual starts at 1, while
349 * the AUDMUX API expects it starts at 0.
350 */
351 int_port--;
352 ext_port--;
353
354 /*
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200355 * Use asynchronous mode (6 wires) for all cases except AC97.
Nicolin Chen708b4352014-07-30 19:27:38 +0800356 * If only 4 wires are needed, just set SSI into
357 * synchronous mode and enable 4 PADs in IOMUX.
358 */
359 switch (priv->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
360 case SND_SOC_DAIFMT_CBM_CFM:
361 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
362 IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
363 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
364 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
365 IMX_AUDMUX_V2_PTCR_RFSDIR |
366 IMX_AUDMUX_V2_PTCR_RCLKDIR |
367 IMX_AUDMUX_V2_PTCR_TFSDIR |
368 IMX_AUDMUX_V2_PTCR_TCLKDIR;
369 break;
370 case SND_SOC_DAIFMT_CBM_CFS:
371 int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
372 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
373 IMX_AUDMUX_V2_PTCR_RCLKDIR |
374 IMX_AUDMUX_V2_PTCR_TCLKDIR;
375 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
376 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
377 IMX_AUDMUX_V2_PTCR_RFSDIR |
378 IMX_AUDMUX_V2_PTCR_TFSDIR;
379 break;
380 case SND_SOC_DAIFMT_CBS_CFM:
381 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
382 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
383 IMX_AUDMUX_V2_PTCR_RFSDIR |
384 IMX_AUDMUX_V2_PTCR_TFSDIR;
385 ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
386 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
387 IMX_AUDMUX_V2_PTCR_RCLKDIR |
388 IMX_AUDMUX_V2_PTCR_TCLKDIR;
389 break;
390 case SND_SOC_DAIFMT_CBS_CFS:
391 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
392 IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
393 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
394 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
395 IMX_AUDMUX_V2_PTCR_RFSDIR |
396 IMX_AUDMUX_V2_PTCR_RCLKDIR |
397 IMX_AUDMUX_V2_PTCR_TFSDIR |
398 IMX_AUDMUX_V2_PTCR_TCLKDIR;
399 break;
400 default:
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200401 if (!fsl_asoc_card_is_ac97(priv))
402 return -EINVAL;
403 }
404
405 if (fsl_asoc_card_is_ac97(priv)) {
406 int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
407 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
408 IMX_AUDMUX_V2_PTCR_TCLKDIR;
409 ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
410 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
411 IMX_AUDMUX_V2_PTCR_TFSDIR;
Nicolin Chen708b4352014-07-30 19:27:38 +0800412 }
413
414 /* Asynchronous mode can not be set along with RCLKDIR */
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200415 if (!fsl_asoc_card_is_ac97(priv)) {
416 unsigned int pdcr =
417 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
418
419 ret = imx_audmux_v2_configure_port(int_port, 0,
420 pdcr);
421 if (ret) {
422 dev_err(dev, "audmux internal port setup failed\n");
423 return ret;
424 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800425 }
426
427 ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
428 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
429 if (ret) {
430 dev_err(dev, "audmux internal port setup failed\n");
431 return ret;
432 }
433
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200434 if (!fsl_asoc_card_is_ac97(priv)) {
435 unsigned int pdcr =
436 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
437
438 ret = imx_audmux_v2_configure_port(ext_port, 0,
439 pdcr);
440 if (ret) {
441 dev_err(dev, "audmux external port setup failed\n");
442 return ret;
443 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800444 }
445
446 ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
447 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
448 if (ret) {
449 dev_err(dev, "audmux external port setup failed\n");
450 return ret;
451 }
452
453 return 0;
454}
455
Shengjiu Wang3b171192020-07-15 22:09:39 +0800456static int hp_jack_event(struct notifier_block *nb, unsigned long event,
457 void *data)
458{
459 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
460 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
461
462 if (event & SND_JACK_HEADPHONE)
463 /* Disable speaker if headphone is plugged in */
464 snd_soc_dapm_disable_pin(dapm, "Ext Spk");
465 else
466 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
467
468 return 0;
469}
470
471static struct notifier_block hp_jack_nb = {
472 .notifier_call = hp_jack_event,
473};
474
475static int mic_jack_event(struct notifier_block *nb, unsigned long event,
476 void *data)
477{
478 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
479 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
480
481 if (event & SND_JACK_MICROPHONE)
482 /* Disable dmic if microphone is plugged in */
483 snd_soc_dapm_disable_pin(dapm, "DMIC");
484 else
485 snd_soc_dapm_enable_pin(dapm, "DMIC");
486
487 return 0;
488}
489
490static struct notifier_block mic_jack_nb = {
491 .notifier_call = mic_jack_event,
492};
493
Nicolin Chen708b4352014-07-30 19:27:38 +0800494static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
495{
496 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
Vinod Koulabd657b2015-11-20 22:45:20 +0530497 struct snd_soc_pcm_runtime *rtd = list_first_entry(
498 &card->rtd_list, struct snd_soc_pcm_runtime, list);
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900499 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
Nicolin Chen708b4352014-07-30 19:27:38 +0800500 struct codec_priv *codec_priv = &priv->codec_priv;
501 struct device *dev = card->dev;
502 int ret;
503
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200504 if (fsl_asoc_card_is_ac97(priv)) {
505#if IS_ENABLED(CONFIG_SND_AC97_CODEC)
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900506 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
Kuninori Morimoto845f80c2017-12-05 04:23:21 +0000507 struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200508
509 /*
510 * Use slots 3/4 for S/PDIF so SSI won't try to enable
511 * other slots and send some samples there
512 * due to SLOTREQ bits for S/PDIF received from codec
513 */
514 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
515 AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
516#endif
517
518 return 0;
519 }
520
Nicolin Chen708b4352014-07-30 19:27:38 +0800521 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
522 codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
Nicolin Chen758a3b02017-09-07 22:27:33 -0700523 if (ret && ret != -ENOTSUPP) {
Nicolin Chen708b4352014-07-30 19:27:38 +0800524 dev_err(dev, "failed to set sysclk in %s\n", __func__);
525 return ret;
526 }
527
528 return 0;
529}
530
531static int fsl_asoc_card_probe(struct platform_device *pdev)
532{
533 struct device_node *cpu_np, *codec_np, *asrc_np;
534 struct device_node *np = pdev->dev.of_node;
535 struct platform_device *asrc_pdev = NULL;
Shengjiu Wang08b54b5e2020-07-21 11:41:49 +0800536 struct device_node *bitclkmaster = NULL;
537 struct device_node *framemaster = NULL;
Nicolin Chen708b4352014-07-30 19:27:38 +0800538 struct platform_device *cpu_pdev;
539 struct fsl_asoc_card_priv *priv;
Shengjiu Wang039652a2020-06-17 12:48:25 +0800540 struct device *codec_dev = NULL;
Nicolin Chen114bb132015-08-12 13:06:12 -0700541 const char *codec_dai_name;
Shengjiu Wang039652a2020-06-17 12:48:25 +0800542 const char *codec_dev_name;
Nicolin Chen708b4352014-07-30 19:27:38 +0800543 u32 width;
544 int ret;
545
546 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
547 if (!priv)
548 return -ENOMEM;
549
550 cpu_np = of_parse_phandle(np, "audio-cpu", 0);
551 /* Give a chance to old DT binding */
552 if (!cpu_np)
553 cpu_np = of_parse_phandle(np, "ssi-controller", 0);
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200554 if (!cpu_np) {
555 dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
Nicolin Chen708b4352014-07-30 19:27:38 +0800556 ret = -EINVAL;
557 goto fail;
558 }
559
560 cpu_pdev = of_find_device_by_node(cpu_np);
561 if (!cpu_pdev) {
562 dev_err(&pdev->dev, "failed to find CPU DAI device\n");
563 ret = -EINVAL;
564 goto fail;
565 }
566
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200567 codec_np = of_parse_phandle(np, "audio-codec", 0);
Shengjiu Wang039652a2020-06-17 12:48:25 +0800568 if (codec_np) {
569 struct platform_device *codec_pdev;
570 struct i2c_client *codec_i2c;
571
572 codec_i2c = of_find_i2c_device_by_node(codec_np);
573 if (codec_i2c) {
574 codec_dev = &codec_i2c->dev;
575 codec_dev_name = codec_i2c->name;
576 }
577 if (!codec_dev) {
578 codec_pdev = of_find_device_by_node(codec_np);
579 if (codec_pdev) {
580 codec_dev = &codec_pdev->dev;
581 codec_dev_name = codec_pdev->name;
582 }
583 }
584 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800585
586 asrc_np = of_parse_phandle(np, "audio-asrc", 0);
587 if (asrc_np)
588 asrc_pdev = of_find_device_by_node(asrc_np);
589
590 /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200591 if (codec_dev) {
Shengjiu Wang039652a2020-06-17 12:48:25 +0800592 struct clk *codec_clk = clk_get(codec_dev, NULL);
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200593
594 if (!IS_ERR(codec_clk)) {
595 priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
596 clk_put(codec_clk);
597 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800598 }
599
600 /* Default sample rate and format, will be updated in hw_params() */
601 priv->sample_rate = 44100;
602 priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
603
604 /* Assign a default DAI format, and allow each card to overwrite it */
605 priv->dai_fmt = DAI_FMT_BASE;
606
Shengjiu Wang039652a2020-06-17 12:48:25 +0800607 memcpy(priv->dai_link, fsl_asoc_card_dai,
608 sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
609
610 priv->card.dapm_routes = audio_map;
611 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
Nicolin Chen708b4352014-07-30 19:27:38 +0800612 /* Diversify the card configurations */
613 if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
Nicolin Chen114bb132015-08-12 13:06:12 -0700614 codec_dai_name = "cs42888";
Nicolin Chen708b4352014-07-30 19:27:38 +0800615 priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
616 priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
617 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
618 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700619 priv->cpu_priv.slot_width = 32;
Nicolin Chen708b4352014-07-30 19:27:38 +0800620 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
Felipe F. Tonello57e756d2016-01-29 11:01:00 +0000621 } else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {
622 codec_dai_name = "cs4271-hifi";
623 priv->codec_priv.mclk_id = CS427x_SYSCLK_MCLK;
624 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Nicolin Chen708b4352014-07-30 19:27:38 +0800625 } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
Nicolin Chen114bb132015-08-12 13:06:12 -0700626 codec_dai_name = "sgtl5000";
Nicolin Chen708b4352014-07-30 19:27:38 +0800627 priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
628 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Matthias Schifferb5074752020-08-21 09:11:53 +0200629 } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic32x4")) {
630 codec_dai_name = "tlv320aic32x4-hifi";
631 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Nicolin Chen708b4352014-07-30 19:27:38 +0800632 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
Nicolin Chen114bb132015-08-12 13:06:12 -0700633 codec_dai_name = "wm8962";
Nicolin Chen708b4352014-07-30 19:27:38 +0800634 priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
635 priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
636 priv->codec_priv.pll_id = WM8962_FLL;
637 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Zidan Wang50e0ee02015-08-14 19:11:09 +0800638 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
639 codec_dai_name = "wm8960-hifi";
Zidan Wang50e0ee02015-08-14 19:11:09 +0800640 priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO;
641 priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO;
642 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200643 } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
644 codec_dai_name = "ac97-hifi";
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200645 priv->dai_fmt = SND_SOC_DAIFMT_AC97;
Shengjiu Wang039652a2020-06-17 12:48:25 +0800646 priv->card.dapm_routes = audio_map_ac97;
647 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_ac97);
648 } else if (of_device_is_compatible(np, "fsl,imx-audio-mqs")) {
649 codec_dai_name = "fsl-mqs-dai";
Shengjiu Wang039652a2020-06-17 12:48:25 +0800650 priv->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
651 SND_SOC_DAIFMT_CBS_CFS |
652 SND_SOC_DAIFMT_NB_NF;
653 priv->dai_link[1].dpcm_capture = 0;
654 priv->dai_link[2].dpcm_capture = 0;
655 priv->card.dapm_routes = audio_map_tx;
656 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
Shengjiu Wang3cd99022020-06-23 14:52:46 +0800657 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8524")) {
658 codec_dai_name = "wm8524-hifi";
Shengjiu Wang3cd99022020-06-23 14:52:46 +0800659 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
660 priv->dai_link[1].dpcm_capture = 0;
661 priv->dai_link[2].dpcm_capture = 0;
662 priv->cpu_priv.slot_width = 32;
663 priv->card.dapm_routes = audio_map_tx;
664 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
Shengjiu Wang77f1ff752020-11-30 11:57:47 +0800665 } else if (of_device_is_compatible(np, "fsl,imx-audio-si476x")) {
666 codec_dai_name = "si476x-codec";
667 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
668 priv->card.dapm_routes = audio_map_rx;
669 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);
Shengjiu Wangefd0b162021-03-17 21:05:02 +0800670 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8958")) {
671 codec_dai_name = "wm8994-aif1";
672 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
673 priv->codec_priv.mclk_id = WM8994_FLL_SRC_MCLK1;
674 priv->codec_priv.fll_id = WM8994_SYSCLK_FLL1;
675 priv->codec_priv.pll_id = WM8994_FLL1;
676 priv->codec_priv.free_freq = priv->codec_priv.mclk_freq;
677 priv->card.dapm_routes = NULL;
678 priv->card.num_dapm_routes = 0;
Nicolin Chen708b4352014-07-30 19:27:38 +0800679 } else {
680 dev_err(&pdev->dev, "unknown Device Tree compatible\n");
Maciej S. Szmigiero6bd3c6f2015-08-31 17:07:12 +0200681 ret = -EINVAL;
682 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800683 }
684
Shengjiu Wang08b54b5e2020-07-21 11:41:49 +0800685 /* Format info from DT is optional. */
Kuninori Morimoto3bba9412021-06-14 09:57:51 +0900686 snd_soc_daifmt_parse_clock_provider_as_phandle(np, NULL, &bitclkmaster, &framemaster);
Shengjiu Wang08b54b5e2020-07-21 11:41:49 +0800687 if (bitclkmaster || framemaster) {
Kuninori Morimoto3bba9412021-06-14 09:57:51 +0900688 unsigned int daifmt = snd_soc_daifmt_parse_format(np, NULL);
689
Shengjiu Wang08b54b5e2020-07-21 11:41:49 +0800690 if (codec_np == bitclkmaster)
691 daifmt |= (codec_np == framemaster) ?
692 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
693 else
694 daifmt |= (codec_np == framemaster) ?
695 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
696
697 /* Override dai_fmt with value from DT */
698 priv->dai_fmt = daifmt;
699 }
700
701 /* Change direction according to format */
702 if (priv->dai_fmt & SND_SOC_DAIFMT_CBM_CFM) {
703 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_IN;
704 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_IN;
705 }
706
707 of_node_put(bitclkmaster);
708 of_node_put(framemaster);
709
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200710 if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) {
711 dev_err(&pdev->dev, "failed to find codec device\n");
Shengjiu Wange396dec2020-06-04 14:25:30 +0800712 ret = -EPROBE_DEFER;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200713 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800714 }
715
716 /* Common settings for corresponding Freescale CPU DAI driver */
Rob Herring1d52a742018-12-05 13:50:49 -0600717 if (of_node_name_eq(cpu_np, "ssi")) {
Nicolin Chen708b4352014-07-30 19:27:38 +0800718 /* Only SSI needs to configure AUDMUX */
719 ret = fsl_asoc_card_audmux_init(np, priv);
720 if (ret) {
721 dev_err(&pdev->dev, "failed to init audmux\n");
Shengjiu Wang5f376712014-08-18 16:38:39 +0800722 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800723 }
Rob Herring1d52a742018-12-05 13:50:49 -0600724 } else if (of_node_name_eq(cpu_np, "esai")) {
Shengjiu Wanga8fd5ca2020-08-10 16:11:43 +0800725 struct clk *esai_clk = clk_get(&cpu_pdev->dev, "extal");
726
727 if (!IS_ERR(esai_clk)) {
728 priv->cpu_priv.sysclk_freq[TX] = clk_get_rate(esai_clk);
729 priv->cpu_priv.sysclk_freq[RX] = clk_get_rate(esai_clk);
730 clk_put(esai_clk);
731 } else if (PTR_ERR(esai_clk) == -EPROBE_DEFER) {
732 ret = -EPROBE_DEFER;
733 goto asrc_fail;
734 }
735
Nicolin Chen708b4352014-07-30 19:27:38 +0800736 priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
737 priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
Rob Herring1d52a742018-12-05 13:50:49 -0600738 } else if (of_node_name_eq(cpu_np, "sai")) {
Nicolin Chen708b4352014-07-30 19:27:38 +0800739 priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
740 priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
741 }
742
Nicolin Chen708b4352014-07-30 19:27:38 +0800743 /* Initialize sound card */
744 priv->pdev = pdev;
745 priv->card.dev = &pdev->dev;
Shengjiu Wang039652a2020-06-17 12:48:25 +0800746 ret = snd_soc_of_parse_card_name(&priv->card, "model");
747 if (ret) {
748 snprintf(priv->name, sizeof(priv->name), "%s-audio",
749 fsl_asoc_card_is_ac97(priv) ? "ac97" : codec_dev_name);
750 priv->card.name = priv->name;
751 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800752 priv->card.dai_link = priv->dai_link;
Nicolin Chen708b4352014-07-30 19:27:38 +0800753 priv->card.late_probe = fsl_asoc_card_late_probe;
Nicolin Chen708b4352014-07-30 19:27:38 +0800754 priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
755 priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
756
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800757 /* Drop the second half of DAPM routes -- ASRC */
758 if (!asrc_pdev)
759 priv->card.num_dapm_routes /= 2;
760
Shengjiu Wang039652a2020-06-17 12:48:25 +0800761 if (of_property_read_bool(np, "audio-routing")) {
762 ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
763 if (ret) {
764 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
765 goto asrc_fail;
766 }
Nicolin Chen31858782015-02-14 17:22:50 -0800767 }
768
Nicolin Chen708b4352014-07-30 19:27:38 +0800769 /* Normal DAI Link */
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900770 priv->dai_link[0].cpus->of_node = cpu_np;
771 priv->dai_link[0].codecs->dai_name = codec_dai_name;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200772
773 if (!fsl_asoc_card_is_ac97(priv))
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900774 priv->dai_link[0].codecs->of_node = codec_np;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200775 else {
776 u32 idx;
777
778 ret = of_property_read_u32(cpu_np, "cell-index", &idx);
779 if (ret) {
780 dev_err(&pdev->dev,
781 "cannot get CPU index property\n");
782 goto asrc_fail;
783 }
784
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900785 priv->dai_link[0].codecs->name =
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200786 devm_kasprintf(&pdev->dev, GFP_KERNEL,
787 "ac97-codec.%u",
788 (unsigned int)idx);
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900789 if (!priv->dai_link[0].codecs->name) {
Arvind Yadav7add71b2017-09-21 10:50:03 +0530790 ret = -ENOMEM;
791 goto asrc_fail;
792 }
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200793 }
794
Kuninori Morimoto9998d3e2019-06-28 10:47:18 +0900795 priv->dai_link[0].platforms->of_node = cpu_np;
Nicolin Chen708b4352014-07-30 19:27:38 +0800796 priv->dai_link[0].dai_fmt = priv->dai_fmt;
797 priv->card.num_links = 1;
798
799 if (asrc_pdev) {
800 /* DPCM DAI Links only if ASRC exsits */
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900801 priv->dai_link[1].cpus->of_node = asrc_np;
Kuninori Morimoto9998d3e2019-06-28 10:47:18 +0900802 priv->dai_link[1].platforms->of_node = asrc_np;
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900803 priv->dai_link[2].codecs->dai_name = codec_dai_name;
804 priv->dai_link[2].codecs->of_node = codec_np;
805 priv->dai_link[2].codecs->name =
806 priv->dai_link[0].codecs->name;
807 priv->dai_link[2].cpus->of_node = cpu_np;
Nicolin Chen708b4352014-07-30 19:27:38 +0800808 priv->dai_link[2].dai_fmt = priv->dai_fmt;
809 priv->card.num_links = 3;
810
811 ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
812 &priv->asrc_rate);
813 if (ret) {
814 dev_err(&pdev->dev, "failed to get output rate\n");
815 ret = -EINVAL;
Shengjiu Wang5f376712014-08-18 16:38:39 +0800816 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800817 }
818
Shengjiu Wang859e3642020-04-16 20:25:33 +0800819 ret = of_property_read_u32(asrc_np, "fsl,asrc-format",
820 &priv->asrc_format);
Nicolin Chen708b4352014-07-30 19:27:38 +0800821 if (ret) {
Shengjiu Wang859e3642020-04-16 20:25:33 +0800822 /* Fallback to old binding; translate to asrc_format */
823 ret = of_property_read_u32(asrc_np, "fsl,asrc-width",
824 &width);
825 if (ret) {
826 dev_err(&pdev->dev,
827 "failed to decide output format\n");
828 goto asrc_fail;
829 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800830
Shengjiu Wang859e3642020-04-16 20:25:33 +0800831 if (width == 24)
832 priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
833 else
834 priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
835 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800836 }
837
838 /* Finish card registering */
839 platform_set_drvdata(pdev, priv);
840 snd_soc_card_set_drvdata(&priv->card, priv);
841
842 ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
Shengjiu Wang3b171192020-07-15 22:09:39 +0800843 if (ret) {
844 if (ret != -EPROBE_DEFER)
845 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
846 goto asrc_fail;
847 }
848
849 /*
850 * Properties "hp-det-gpio" and "mic-det-gpio" are optional, and
851 * asoc_simple_init_jack uses these properties for creating
852 * Headphone Jack and Microphone Jack.
853 *
854 * The notifier is initialized in snd_soc_card_jack_new(), then
855 * snd_soc_jack_notifier_register can be called.
856 */
857 if (of_property_read_bool(np, "hp-det-gpio")) {
858 ret = asoc_simple_init_jack(&priv->card, &priv->hp_jack,
859 1, NULL, "Headphone Jack");
860 if (ret)
861 goto asrc_fail;
862
863 snd_soc_jack_notifier_register(&priv->hp_jack.jack, &hp_jack_nb);
864 }
865
866 if (of_property_read_bool(np, "mic-det-gpio")) {
867 ret = asoc_simple_init_jack(&priv->card, &priv->mic_jack,
868 0, NULL, "Mic Jack");
869 if (ret)
870 goto asrc_fail;
871
872 snd_soc_jack_notifier_register(&priv->mic_jack.jack, &mic_jack_nb);
873 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800874
Shengjiu Wang5f376712014-08-18 16:38:39 +0800875asrc_fail:
876 of_node_put(asrc_np);
Nicolin Chen708b4352014-07-30 19:27:38 +0800877 of_node_put(codec_np);
wen yang11907e9d2019-02-02 14:53:16 +0000878 put_device(&cpu_pdev->dev);
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200879fail:
Nicolin Chen708b4352014-07-30 19:27:38 +0800880 of_node_put(cpu_np);
881
882 return ret;
883}
884
885static const struct of_device_id fsl_asoc_card_dt_ids[] = {
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200886 { .compatible = "fsl,imx-audio-ac97", },
Nicolin Chen708b4352014-07-30 19:27:38 +0800887 { .compatible = "fsl,imx-audio-cs42888", },
Felipe F. Tonello57e756d2016-01-29 11:01:00 +0000888 { .compatible = "fsl,imx-audio-cs427x", },
Matthias Schifferb5074752020-08-21 09:11:53 +0200889 { .compatible = "fsl,imx-audio-tlv320aic32x4", },
Nicolin Chen708b4352014-07-30 19:27:38 +0800890 { .compatible = "fsl,imx-audio-sgtl5000", },
891 { .compatible = "fsl,imx-audio-wm8962", },
Zidan Wang50e0ee02015-08-14 19:11:09 +0800892 { .compatible = "fsl,imx-audio-wm8960", },
Shengjiu Wang039652a2020-06-17 12:48:25 +0800893 { .compatible = "fsl,imx-audio-mqs", },
Shengjiu Wang3cd99022020-06-23 14:52:46 +0800894 { .compatible = "fsl,imx-audio-wm8524", },
Shengjiu Wang77f1ff752020-11-30 11:57:47 +0800895 { .compatible = "fsl,imx-audio-si476x", },
Shengjiu Wangefd0b162021-03-17 21:05:02 +0800896 { .compatible = "fsl,imx-audio-wm8958", },
Nicolin Chen708b4352014-07-30 19:27:38 +0800897 {}
898};
Luis de Bethencourt5226f232015-09-03 12:57:47 +0200899MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
Nicolin Chen708b4352014-07-30 19:27:38 +0800900
901static struct platform_driver fsl_asoc_card_driver = {
902 .probe = fsl_asoc_card_probe,
903 .driver = {
904 .name = "fsl-asoc-card",
905 .pm = &snd_soc_pm_ops,
906 .of_match_table = fsl_asoc_card_dt_ids,
907 },
908};
909module_platform_driver(fsl_asoc_card_driver);
910
911MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
912MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
913MODULE_ALIAS("platform:fsl-asoc-card");
914MODULE_LICENSE("GPL");