blob: 5ee94550528135113d892dbdcff6d1d5c4d0865c [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"
Ariel D'Alessandro8c9b9cfb2021-11-19 12:32:48 -030029#include "../codecs/tlv320aic31xx.h"
Nicolin Chen708b4352014-07-30 19:27:38 +080030
Felipe F. Tonello57e756d2016-01-29 11:01:00 +000031#define CS427x_SYSCLK_MCLK 0
32
Nicolin Chen708b4352014-07-30 19:27:38 +080033#define RX 0
34#define TX 1
35
36/* Default DAI format without Master and Slave flag */
37#define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
38
39/**
Pierre-Louis Bossart31deacf2020-07-02 14:21:38 -050040 * struct codec_priv - CODEC private data
Nicolin Chen708b4352014-07-30 19:27:38 +080041 * @mclk_freq: Clock rate of MCLK
Shengjiu Wangefd0b162021-03-17 21:05:02 +080042 * @free_freq: Clock rate of MCLK for hw_free()
Nicolin Chen708b4352014-07-30 19:27:38 +080043 * @mclk_id: MCLK (or main clock) id for set_sysclk()
44 * @fll_id: FLL (or secordary clock) id for set_sysclk()
45 * @pll_id: PLL id for set_pll()
46 */
47struct codec_priv {
48 unsigned long mclk_freq;
Shengjiu Wangefd0b162021-03-17 21:05:02 +080049 unsigned long free_freq;
Nicolin Chen708b4352014-07-30 19:27:38 +080050 u32 mclk_id;
51 u32 fll_id;
52 u32 pll_id;
53};
54
55/**
Pierre-Louis Bossart31deacf2020-07-02 14:21:38 -050056 * struct cpu_priv - CPU private data
57 * @sysclk_freq: SYSCLK rates for set_sysclk()
58 * @sysclk_dir: SYSCLK directions for set_sysclk()
59 * @sysclk_id: SYSCLK ids for set_sysclk()
Nicolin Chencb3fc1f2014-10-24 16:48:13 -070060 * @slot_width: Slot width of each frame
Nicolin Chen708b4352014-07-30 19:27:38 +080061 *
62 * Note: [1] for tx and [0] for rx
63 */
64struct cpu_priv {
65 unsigned long sysclk_freq[2];
66 u32 sysclk_dir[2];
67 u32 sysclk_id[2];
Nicolin Chencb3fc1f2014-10-24 16:48:13 -070068 u32 slot_width;
Nicolin Chen708b4352014-07-30 19:27:38 +080069};
70
71/**
Pierre-Louis Bossart31deacf2020-07-02 14:21:38 -050072 * struct fsl_asoc_card_priv - Freescale Generic ASOC card private data
73 * @dai_link: DAI link structure including normal one and DPCM link
Shengjiu Wang3b171192020-07-15 22:09:39 +080074 * @hp_jack: Headphone Jack structure
75 * @mic_jack: Microphone Jack structure
Nicolin Chen708b4352014-07-30 19:27:38 +080076 * @pdev: platform device pointer
77 * @codec_priv: CODEC private data
78 * @cpu_priv: CPU private data
79 * @card: ASoC card structure
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +080080 * @streams: Mask of current active streams
Nicolin Chen708b4352014-07-30 19:27:38 +080081 * @sample_rate: Current sample rate
82 * @sample_format: Current sample format
83 * @asrc_rate: ASRC sample rate used by Back-Ends
84 * @asrc_format: ASRC sample format used by Back-Ends
85 * @dai_fmt: DAI format between CPU and CODEC
86 * @name: Card name
87 */
88
89struct fsl_asoc_card_priv {
90 struct snd_soc_dai_link dai_link[3];
Shengjiu Wang3b171192020-07-15 22:09:39 +080091 struct asoc_simple_jack hp_jack;
92 struct asoc_simple_jack mic_jack;
Nicolin Chen708b4352014-07-30 19:27:38 +080093 struct platform_device *pdev;
94 struct codec_priv codec_priv;
95 struct cpu_priv cpu_priv;
96 struct snd_soc_card card;
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +080097 u8 streams;
Nicolin Chen708b4352014-07-30 19:27:38 +080098 u32 sample_rate;
Fabio Estevamfc734c242018-02-11 19:53:18 -020099 snd_pcm_format_t sample_format;
Nicolin Chen708b4352014-07-30 19:27:38 +0800100 u32 asrc_rate;
Fabio Estevamfc734c242018-02-11 19:53:18 -0200101 snd_pcm_format_t asrc_format;
Nicolin Chen708b4352014-07-30 19:27:38 +0800102 u32 dai_fmt;
103 char name[32];
104};
105
Pierre-Louis Bossart31deacf2020-07-02 14:21:38 -0500106/*
Lee Jones1b582142020-07-15 16:00:09 +0100107 * This dapm route map exists for DPCM link only.
Nicolin Chen708b4352014-07-30 19:27:38 +0800108 * The other routes shall go through Device Tree.
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800109 *
110 * Note: keep all ASRC routes in the second half
111 * to drop them easily for non-ASRC cases.
Nicolin Chen708b4352014-07-30 19:27:38 +0800112 */
113static const struct snd_soc_dapm_route audio_map[] = {
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800114 /* 1st half -- Normal DAPM routes */
Nicolin Chen708b4352014-07-30 19:27:38 +0800115 {"Playback", NULL, "CPU-Playback"},
Nicolin Chen708b4352014-07-30 19:27:38 +0800116 {"CPU-Capture", NULL, "Capture"},
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800117 /* 2nd half -- ASRC DAPM routes */
118 {"CPU-Playback", NULL, "ASRC-Playback"},
119 {"ASRC-Capture", NULL, "CPU-Capture"},
Nicolin Chen708b4352014-07-30 19:27:38 +0800120};
121
Maciej S. Szmigiero25e5ef92015-12-20 21:34:29 +0100122static const struct snd_soc_dapm_route audio_map_ac97[] = {
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800123 /* 1st half -- Normal DAPM routes */
Maciej S. Szmigiero25e5ef92015-12-20 21:34:29 +0100124 {"Playback", NULL, "AC97 Playback"},
Maciej S. Szmigiero25e5ef92015-12-20 21:34:29 +0100125 {"AC97 Capture", NULL, "Capture"},
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800126 /* 2nd half -- ASRC DAPM routes */
127 {"AC97 Playback", NULL, "ASRC-Playback"},
128 {"ASRC-Capture", NULL, "AC97 Capture"},
Maciej S. Szmigiero25e5ef92015-12-20 21:34:29 +0100129};
130
Shengjiu Wang039652a2020-06-17 12:48:25 +0800131static const struct snd_soc_dapm_route audio_map_tx[] = {
132 /* 1st half -- Normal DAPM routes */
133 {"Playback", NULL, "CPU-Playback"},
134 /* 2nd half -- ASRC DAPM routes */
135 {"CPU-Playback", NULL, "ASRC-Playback"},
136};
137
Shengjiu Wang77f1ff752020-11-30 11:57:47 +0800138static const struct snd_soc_dapm_route audio_map_rx[] = {
139 /* 1st half -- Normal DAPM routes */
140 {"CPU-Capture", NULL, "Capture"},
141 /* 2nd half -- ASRC DAPM routes */
142 {"ASRC-Capture", NULL, "CPU-Capture"},
143};
144
Nicolin Chen708b4352014-07-30 19:27:38 +0800145/* Add all possible widgets into here without being redundant */
146static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
147 SND_SOC_DAPM_LINE("Line Out Jack", NULL),
148 SND_SOC_DAPM_LINE("Line In Jack", NULL),
149 SND_SOC_DAPM_HP("Headphone Jack", NULL),
150 SND_SOC_DAPM_SPK("Ext Spk", NULL),
151 SND_SOC_DAPM_MIC("Mic Jack", NULL),
152 SND_SOC_DAPM_MIC("AMIC", NULL),
153 SND_SOC_DAPM_MIC("DMIC", NULL),
154};
155
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200156static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
157{
158 return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
159}
160
Nicolin Chen708b4352014-07-30 19:27:38 +0800161static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
162 struct snd_pcm_hw_params *params)
163{
Kuninori Morimoto9f5f0782020-07-20 10:18:38 +0900164 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Nicolin Chen708b4352014-07-30 19:27:38 +0800165 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
166 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800167 struct codec_priv *codec_priv = &priv->codec_priv;
Nicolin Chen708b4352014-07-30 19:27:38 +0800168 struct cpu_priv *cpu_priv = &priv->cpu_priv;
169 struct device *dev = rtd->card->dev;
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800170 unsigned int pll_out;
Nicolin Chen708b4352014-07-30 19:27:38 +0800171 int ret;
172
173 priv->sample_rate = params_rate(params);
174 priv->sample_format = params_format(params);
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800175 priv->streams |= BIT(substream->stream);
Nicolin Chen708b4352014-07-30 19:27:38 +0800176
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800177 if (fsl_asoc_card_is_ac97(priv))
Nicolin Chen708b4352014-07-30 19:27:38 +0800178 return 0;
179
180 /* Specific configurations of DAIs starts from here */
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900181 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), cpu_priv->sysclk_id[tx],
Nicolin Chen708b4352014-07-30 19:27:38 +0800182 cpu_priv->sysclk_freq[tx],
183 cpu_priv->sysclk_dir[tx]);
Nicolin Chen758a3b02017-09-07 22:27:33 -0700184 if (ret && ret != -ENOTSUPP) {
Nicolin Chen708b4352014-07-30 19:27:38 +0800185 dev_err(dev, "failed to set sysclk for cpu dai\n");
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800186 goto fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800187 }
188
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700189 if (cpu_priv->slot_width) {
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900190 ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2,
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700191 cpu_priv->slot_width);
Nicolin Chen758a3b02017-09-07 22:27:33 -0700192 if (ret && ret != -ENOTSUPP) {
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700193 dev_err(dev, "failed to set TDM slot for cpu dai\n");
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800194 goto fail;
195 }
196 }
197
198 /* Specific configuration for PLL */
199 if (codec_priv->pll_id && codec_priv->fll_id) {
200 if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
201 pll_out = priv->sample_rate * 384;
202 else
203 pll_out = priv->sample_rate * 256;
204
205 ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
206 codec_priv->pll_id,
207 codec_priv->mclk_id,
208 codec_priv->mclk_freq, pll_out);
209 if (ret) {
210 dev_err(dev, "failed to start FLL: %d\n", ret);
211 goto fail;
212 }
213
214 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
215 codec_priv->fll_id,
216 pll_out, SND_SOC_CLOCK_IN);
217
218 if (ret && ret != -ENOTSUPP) {
219 dev_err(dev, "failed to set SYSCLK: %d\n", ret);
220 goto fail;
221 }
222 }
223
224 return 0;
225
226fail:
227 priv->streams &= ~BIT(substream->stream);
228 return ret;
229}
230
231static int fsl_asoc_card_hw_free(struct snd_pcm_substream *substream)
232{
233 struct snd_soc_pcm_runtime *rtd = substream->private_data;
234 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
235 struct codec_priv *codec_priv = &priv->codec_priv;
236 struct device *dev = rtd->card->dev;
237 int ret;
238
239 priv->streams &= ~BIT(substream->stream);
240
241 if (!priv->streams && codec_priv->pll_id && codec_priv->fll_id) {
Shengjiu Wangefd0b162021-03-17 21:05:02 +0800242 /* Force freq to be free_freq to avoid error message in codec */
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800243 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
244 codec_priv->mclk_id,
Shengjiu Wangefd0b162021-03-17 21:05:02 +0800245 codec_priv->free_freq,
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800246 SND_SOC_CLOCK_IN);
247 if (ret) {
248 dev_err(dev, "failed to switch away from FLL: %d\n", ret);
249 return ret;
250 }
251
252 ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
253 codec_priv->pll_id, 0, 0, 0);
254 if (ret && ret != -ENOTSUPP) {
255 dev_err(dev, "failed to stop FLL: %d\n", ret);
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700256 return ret;
257 }
258 }
259
Nicolin Chen708b4352014-07-30 19:27:38 +0800260 return 0;
261}
262
Julia Lawallddba7fa2016-10-15 16:55:50 +0200263static const struct snd_soc_ops fsl_asoc_card_ops = {
Nicolin Chen708b4352014-07-30 19:27:38 +0800264 .hw_params = fsl_asoc_card_hw_params,
Shengjiu Wangf36e8ed2020-08-03 10:13:31 +0800265 .hw_free = fsl_asoc_card_hw_free,
Nicolin Chen708b4352014-07-30 19:27:38 +0800266};
267
268static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
269 struct snd_pcm_hw_params *params)
270{
271 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
272 struct snd_interval *rate;
273 struct snd_mask *mask;
274
275 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
276 rate->max = rate->min = priv->asrc_rate;
277
278 mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
279 snd_mask_none(mask);
Takashi Iwaiebc22af2018-07-25 23:17:20 +0200280 snd_mask_set_format(mask, priv->asrc_format);
Nicolin Chen708b4352014-07-30 19:27:38 +0800281
282 return 0;
283}
284
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900285SND_SOC_DAILINK_DEFS(hifi,
286 DAILINK_COMP_ARRAY(COMP_EMPTY()),
Kuninori Morimoto9998d3e2019-06-28 10:47:18 +0900287 DAILINK_COMP_ARRAY(COMP_EMPTY()),
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900288 DAILINK_COMP_ARRAY(COMP_EMPTY()));
289
290SND_SOC_DAILINK_DEFS(hifi_fe,
291 DAILINK_COMP_ARRAY(COMP_EMPTY()),
Kuninori Morimoto9998d3e2019-06-28 10:47:18 +0900292 DAILINK_COMP_ARRAY(COMP_DUMMY()),
293 DAILINK_COMP_ARRAY(COMP_EMPTY()));
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900294
295SND_SOC_DAILINK_DEFS(hifi_be,
296 DAILINK_COMP_ARRAY(COMP_EMPTY()),
297 DAILINK_COMP_ARRAY(COMP_EMPTY()),
298 DAILINK_COMP_ARRAY(COMP_DUMMY()));
299
Nicolin Chen708b4352014-07-30 19:27:38 +0800300static struct snd_soc_dai_link fsl_asoc_card_dai[] = {
301 /* Default ASoC DAI Link*/
302 {
303 .name = "HiFi",
304 .stream_name = "HiFi",
305 .ops = &fsl_asoc_card_ops,
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900306 SND_SOC_DAILINK_REG(hifi),
Nicolin Chen708b4352014-07-30 19:27:38 +0800307 },
308 /* DPCM Link between Front-End and Back-End (Optional) */
309 {
310 .name = "HiFi-ASRC-FE",
311 .stream_name = "HiFi-ASRC-FE",
Nicolin Chen708b4352014-07-30 19:27:38 +0800312 .dpcm_playback = 1,
313 .dpcm_capture = 1,
314 .dynamic = 1,
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900315 SND_SOC_DAILINK_REG(hifi_fe),
Nicolin Chen708b4352014-07-30 19:27:38 +0800316 },
317 {
318 .name = "HiFi-ASRC-BE",
319 .stream_name = "HiFi-ASRC-BE",
Nicolin Chen708b4352014-07-30 19:27:38 +0800320 .be_hw_params_fixup = be_hw_params_fixup,
321 .ops = &fsl_asoc_card_ops,
322 .dpcm_playback = 1,
323 .dpcm_capture = 1,
324 .no_pcm = 1,
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900325 SND_SOC_DAILINK_REG(hifi_be),
Nicolin Chen708b4352014-07-30 19:27:38 +0800326 },
327};
328
Nicolin Chen708b4352014-07-30 19:27:38 +0800329static int fsl_asoc_card_audmux_init(struct device_node *np,
330 struct fsl_asoc_card_priv *priv)
331{
332 struct device *dev = &priv->pdev->dev;
333 u32 int_ptcr = 0, ext_ptcr = 0;
334 int int_port, ext_port;
335 int ret;
336
337 ret = of_property_read_u32(np, "mux-int-port", &int_port);
338 if (ret) {
339 dev_err(dev, "mux-int-port missing or invalid\n");
340 return ret;
341 }
342 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
343 if (ret) {
344 dev_err(dev, "mux-ext-port missing or invalid\n");
345 return ret;
346 }
347
348 /*
349 * The port numbering in the hardware manual starts at 1, while
350 * the AUDMUX API expects it starts at 0.
351 */
352 int_port--;
353 ext_port--;
354
355 /*
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200356 * Use asynchronous mode (6 wires) for all cases except AC97.
Nicolin Chen708b4352014-07-30 19:27:38 +0800357 * If only 4 wires are needed, just set SSI into
358 * synchronous mode and enable 4 PADs in IOMUX.
359 */
Mark Brown8fcfd342021-09-21 22:35:28 +0100360 switch (priv->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
361 case SND_SOC_DAIFMT_CBP_CFP:
Nicolin Chen708b4352014-07-30 19:27:38 +0800362 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
363 IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
364 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
365 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
366 IMX_AUDMUX_V2_PTCR_RFSDIR |
367 IMX_AUDMUX_V2_PTCR_RCLKDIR |
368 IMX_AUDMUX_V2_PTCR_TFSDIR |
369 IMX_AUDMUX_V2_PTCR_TCLKDIR;
370 break;
Mark Brown8fcfd342021-09-21 22:35:28 +0100371 case SND_SOC_DAIFMT_CBP_CFC:
Nicolin Chen708b4352014-07-30 19:27:38 +0800372 int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
373 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
374 IMX_AUDMUX_V2_PTCR_RCLKDIR |
375 IMX_AUDMUX_V2_PTCR_TCLKDIR;
376 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
377 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
378 IMX_AUDMUX_V2_PTCR_RFSDIR |
379 IMX_AUDMUX_V2_PTCR_TFSDIR;
380 break;
Mark Brown8fcfd342021-09-21 22:35:28 +0100381 case SND_SOC_DAIFMT_CBC_CFP:
Nicolin Chen708b4352014-07-30 19:27:38 +0800382 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
383 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
384 IMX_AUDMUX_V2_PTCR_RFSDIR |
385 IMX_AUDMUX_V2_PTCR_TFSDIR;
386 ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
387 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
388 IMX_AUDMUX_V2_PTCR_RCLKDIR |
389 IMX_AUDMUX_V2_PTCR_TCLKDIR;
390 break;
Mark Brown8fcfd342021-09-21 22:35:28 +0100391 case SND_SOC_DAIFMT_CBC_CFC:
Nicolin Chen708b4352014-07-30 19:27:38 +0800392 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
393 IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
394 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
395 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
396 IMX_AUDMUX_V2_PTCR_RFSDIR |
397 IMX_AUDMUX_V2_PTCR_RCLKDIR |
398 IMX_AUDMUX_V2_PTCR_TFSDIR |
399 IMX_AUDMUX_V2_PTCR_TCLKDIR;
400 break;
401 default:
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200402 if (!fsl_asoc_card_is_ac97(priv))
403 return -EINVAL;
404 }
405
406 if (fsl_asoc_card_is_ac97(priv)) {
407 int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
408 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
409 IMX_AUDMUX_V2_PTCR_TCLKDIR;
410 ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
411 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
412 IMX_AUDMUX_V2_PTCR_TFSDIR;
Nicolin Chen708b4352014-07-30 19:27:38 +0800413 }
414
415 /* Asynchronous mode can not be set along with RCLKDIR */
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200416 if (!fsl_asoc_card_is_ac97(priv)) {
417 unsigned int pdcr =
418 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
419
420 ret = imx_audmux_v2_configure_port(int_port, 0,
421 pdcr);
422 if (ret) {
423 dev_err(dev, "audmux internal port setup failed\n");
424 return ret;
425 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800426 }
427
428 ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
429 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
430 if (ret) {
431 dev_err(dev, "audmux internal port setup failed\n");
432 return ret;
433 }
434
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200435 if (!fsl_asoc_card_is_ac97(priv)) {
436 unsigned int pdcr =
437 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
438
439 ret = imx_audmux_v2_configure_port(ext_port, 0,
440 pdcr);
441 if (ret) {
442 dev_err(dev, "audmux external port setup failed\n");
443 return ret;
444 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800445 }
446
447 ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
448 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
449 if (ret) {
450 dev_err(dev, "audmux external port setup failed\n");
451 return ret;
452 }
453
454 return 0;
455}
456
Shengjiu Wang3b171192020-07-15 22:09:39 +0800457static int hp_jack_event(struct notifier_block *nb, unsigned long event,
458 void *data)
459{
460 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
461 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
462
463 if (event & SND_JACK_HEADPHONE)
464 /* Disable speaker if headphone is plugged in */
465 snd_soc_dapm_disable_pin(dapm, "Ext Spk");
466 else
467 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
468
469 return 0;
470}
471
472static struct notifier_block hp_jack_nb = {
473 .notifier_call = hp_jack_event,
474};
475
476static int mic_jack_event(struct notifier_block *nb, unsigned long event,
477 void *data)
478{
479 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
480 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
481
482 if (event & SND_JACK_MICROPHONE)
483 /* Disable dmic if microphone is plugged in */
484 snd_soc_dapm_disable_pin(dapm, "DMIC");
485 else
486 snd_soc_dapm_enable_pin(dapm, "DMIC");
487
488 return 0;
489}
490
491static struct notifier_block mic_jack_nb = {
492 .notifier_call = mic_jack_event,
493};
494
Nicolin Chen708b4352014-07-30 19:27:38 +0800495static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
496{
497 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
Vinod Koulabd657b2015-11-20 22:45:20 +0530498 struct snd_soc_pcm_runtime *rtd = list_first_entry(
499 &card->rtd_list, struct snd_soc_pcm_runtime, list);
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900500 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
Nicolin Chen708b4352014-07-30 19:27:38 +0800501 struct codec_priv *codec_priv = &priv->codec_priv;
502 struct device *dev = card->dev;
503 int ret;
504
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200505 if (fsl_asoc_card_is_ac97(priv)) {
506#if IS_ENABLED(CONFIG_SND_AC97_CODEC)
Kuninori Morimoto17198ae2020-03-23 14:18:30 +0900507 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
Kuninori Morimoto845f80c2017-12-05 04:23:21 +0000508 struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200509
510 /*
511 * Use slots 3/4 for S/PDIF so SSI won't try to enable
512 * other slots and send some samples there
513 * due to SLOTREQ bits for S/PDIF received from codec
514 */
515 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
516 AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
517#endif
518
519 return 0;
520 }
521
Nicolin Chen708b4352014-07-30 19:27:38 +0800522 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
523 codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
Nicolin Chen758a3b02017-09-07 22:27:33 -0700524 if (ret && ret != -ENOTSUPP) {
Nicolin Chen708b4352014-07-30 19:27:38 +0800525 dev_err(dev, "failed to set sysclk in %s\n", __func__);
526 return ret;
527 }
528
529 return 0;
530}
531
532static int fsl_asoc_card_probe(struct platform_device *pdev)
533{
534 struct device_node *cpu_np, *codec_np, *asrc_np;
535 struct device_node *np = pdev->dev.of_node;
536 struct platform_device *asrc_pdev = NULL;
Mark Brown8fcfd342021-09-21 22:35:28 +0100537 struct device_node *bitclkprovider = NULL;
538 struct device_node *frameprovider = NULL;
Nicolin Chen708b4352014-07-30 19:27:38 +0800539 struct platform_device *cpu_pdev;
540 struct fsl_asoc_card_priv *priv;
Shengjiu Wang039652a2020-06-17 12:48:25 +0800541 struct device *codec_dev = NULL;
Nicolin Chen114bb132015-08-12 13:06:12 -0700542 const char *codec_dai_name;
Shengjiu Wang039652a2020-06-17 12:48:25 +0800543 const char *codec_dev_name;
Nicolin Chen708b4352014-07-30 19:27:38 +0800544 u32 width;
545 int ret;
546
547 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
548 if (!priv)
549 return -ENOMEM;
550
551 cpu_np = of_parse_phandle(np, "audio-cpu", 0);
552 /* Give a chance to old DT binding */
553 if (!cpu_np)
554 cpu_np = of_parse_phandle(np, "ssi-controller", 0);
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200555 if (!cpu_np) {
556 dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
Nicolin Chen708b4352014-07-30 19:27:38 +0800557 ret = -EINVAL;
558 goto fail;
559 }
560
561 cpu_pdev = of_find_device_by_node(cpu_np);
562 if (!cpu_pdev) {
563 dev_err(&pdev->dev, "failed to find CPU DAI device\n");
564 ret = -EINVAL;
565 goto fail;
566 }
567
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200568 codec_np = of_parse_phandle(np, "audio-codec", 0);
Shengjiu Wang039652a2020-06-17 12:48:25 +0800569 if (codec_np) {
570 struct platform_device *codec_pdev;
571 struct i2c_client *codec_i2c;
572
573 codec_i2c = of_find_i2c_device_by_node(codec_np);
574 if (codec_i2c) {
575 codec_dev = &codec_i2c->dev;
576 codec_dev_name = codec_i2c->name;
577 }
578 if (!codec_dev) {
579 codec_pdev = of_find_device_by_node(codec_np);
580 if (codec_pdev) {
581 codec_dev = &codec_pdev->dev;
582 codec_dev_name = codec_pdev->name;
583 }
584 }
585 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800586
587 asrc_np = of_parse_phandle(np, "audio-asrc", 0);
588 if (asrc_np)
589 asrc_pdev = of_find_device_by_node(asrc_np);
590
591 /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200592 if (codec_dev) {
Shengjiu Wang039652a2020-06-17 12:48:25 +0800593 struct clk *codec_clk = clk_get(codec_dev, NULL);
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200594
595 if (!IS_ERR(codec_clk)) {
596 priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
597 clk_put(codec_clk);
598 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800599 }
600
601 /* Default sample rate and format, will be updated in hw_params() */
602 priv->sample_rate = 44100;
603 priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
604
605 /* Assign a default DAI format, and allow each card to overwrite it */
606 priv->dai_fmt = DAI_FMT_BASE;
607
Shengjiu Wang039652a2020-06-17 12:48:25 +0800608 memcpy(priv->dai_link, fsl_asoc_card_dai,
609 sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
610
611 priv->card.dapm_routes = audio_map;
612 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
Nicolin Chen708b4352014-07-30 19:27:38 +0800613 /* Diversify the card configurations */
614 if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
Nicolin Chen114bb132015-08-12 13:06:12 -0700615 codec_dai_name = "cs42888";
Nicolin Chen708b4352014-07-30 19:27:38 +0800616 priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
617 priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
618 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
619 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700620 priv->cpu_priv.slot_width = 32;
Mark Brown8fcfd342021-09-21 22:35:28 +0100621 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
Felipe F. Tonello57e756d2016-01-29 11:01:00 +0000622 } else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {
623 codec_dai_name = "cs4271-hifi";
624 priv->codec_priv.mclk_id = CS427x_SYSCLK_MCLK;
Mark Brown8fcfd342021-09-21 22:35:28 +0100625 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
Nicolin Chen708b4352014-07-30 19:27:38 +0800626 } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
Nicolin Chen114bb132015-08-12 13:06:12 -0700627 codec_dai_name = "sgtl5000";
Nicolin Chen708b4352014-07-30 19:27:38 +0800628 priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
Mark Brown8fcfd342021-09-21 22:35:28 +0100629 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
Matthias Schifferb5074752020-08-21 09:11:53 +0200630 } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic32x4")) {
631 codec_dai_name = "tlv320aic32x4-hifi";
Mark Brown8fcfd342021-09-21 22:35:28 +0100632 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
Ariel D'Alessandro8c9b9cfb2021-11-19 12:32:48 -0300633 } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic31xx")) {
634 codec_dai_name = "tlv320dac31xx-hifi";
635 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
636 priv->dai_link[1].dpcm_capture = 0;
637 priv->dai_link[2].dpcm_capture = 0;
638 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
639 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
640 priv->codec_priv.mclk_id = AIC31XX_PLL_CLKIN_BCLK;
641 priv->card.dapm_routes = audio_map_tx;
642 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
Nicolin Chen708b4352014-07-30 19:27:38 +0800643 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
Nicolin Chen114bb132015-08-12 13:06:12 -0700644 codec_dai_name = "wm8962";
Nicolin Chen708b4352014-07-30 19:27:38 +0800645 priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
646 priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
647 priv->codec_priv.pll_id = WM8962_FLL;
Mark Brown8fcfd342021-09-21 22:35:28 +0100648 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
Zidan Wang50e0ee02015-08-14 19:11:09 +0800649 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
650 codec_dai_name = "wm8960-hifi";
Zidan Wang50e0ee02015-08-14 19:11:09 +0800651 priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO;
652 priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO;
Mark Brown8fcfd342021-09-21 22:35:28 +0100653 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200654 } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
655 codec_dai_name = "ac97-hifi";
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200656 priv->dai_fmt = SND_SOC_DAIFMT_AC97;
Shengjiu Wang039652a2020-06-17 12:48:25 +0800657 priv->card.dapm_routes = audio_map_ac97;
658 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_ac97);
659 } else if (of_device_is_compatible(np, "fsl,imx-audio-mqs")) {
660 codec_dai_name = "fsl-mqs-dai";
Shengjiu Wang039652a2020-06-17 12:48:25 +0800661 priv->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
Mark Brown8fcfd342021-09-21 22:35:28 +0100662 SND_SOC_DAIFMT_CBC_CFC |
Shengjiu Wang039652a2020-06-17 12:48:25 +0800663 SND_SOC_DAIFMT_NB_NF;
664 priv->dai_link[1].dpcm_capture = 0;
665 priv->dai_link[2].dpcm_capture = 0;
666 priv->card.dapm_routes = audio_map_tx;
667 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
Shengjiu Wang3cd99022020-06-23 14:52:46 +0800668 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8524")) {
669 codec_dai_name = "wm8524-hifi";
Mark Brown8fcfd342021-09-21 22:35:28 +0100670 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
Shengjiu Wang3cd99022020-06-23 14:52:46 +0800671 priv->dai_link[1].dpcm_capture = 0;
672 priv->dai_link[2].dpcm_capture = 0;
673 priv->cpu_priv.slot_width = 32;
674 priv->card.dapm_routes = audio_map_tx;
675 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
Shengjiu Wang77f1ff752020-11-30 11:57:47 +0800676 } else if (of_device_is_compatible(np, "fsl,imx-audio-si476x")) {
677 codec_dai_name = "si476x-codec";
Mark Brown8fcfd342021-09-21 22:35:28 +0100678 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
Shengjiu Wang77f1ff752020-11-30 11:57:47 +0800679 priv->card.dapm_routes = audio_map_rx;
680 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);
Shengjiu Wangefd0b162021-03-17 21:05:02 +0800681 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8958")) {
682 codec_dai_name = "wm8994-aif1";
Mark Brown8fcfd342021-09-21 22:35:28 +0100683 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
Shengjiu Wangefd0b162021-03-17 21:05:02 +0800684 priv->codec_priv.mclk_id = WM8994_FLL_SRC_MCLK1;
685 priv->codec_priv.fll_id = WM8994_SYSCLK_FLL1;
686 priv->codec_priv.pll_id = WM8994_FLL1;
687 priv->codec_priv.free_freq = priv->codec_priv.mclk_freq;
688 priv->card.dapm_routes = NULL;
689 priv->card.num_dapm_routes = 0;
Nicolin Chen708b4352014-07-30 19:27:38 +0800690 } else {
691 dev_err(&pdev->dev, "unknown Device Tree compatible\n");
Maciej S. Szmigiero6bd3c6f2015-08-31 17:07:12 +0200692 ret = -EINVAL;
693 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800694 }
695
Shengjiu Wang08b54b5e2020-07-21 11:41:49 +0800696 /* Format info from DT is optional. */
Mark Brown8fcfd342021-09-21 22:35:28 +0100697 snd_soc_daifmt_parse_clock_provider_as_phandle(np, NULL, &bitclkprovider, &frameprovider);
698 if (bitclkprovider || frameprovider) {
Kuninori Morimoto3bba9412021-06-14 09:57:51 +0900699 unsigned int daifmt = snd_soc_daifmt_parse_format(np, NULL);
700
Mark Brown8fcfd342021-09-21 22:35:28 +0100701 if (codec_np == bitclkprovider)
702 daifmt |= (codec_np == frameprovider) ?
703 SND_SOC_DAIFMT_CBP_CFP : SND_SOC_DAIFMT_CBP_CFC;
Shengjiu Wang08b54b5e2020-07-21 11:41:49 +0800704 else
Mark Brown8fcfd342021-09-21 22:35:28 +0100705 daifmt |= (codec_np == frameprovider) ?
706 SND_SOC_DAIFMT_CBC_CFP : SND_SOC_DAIFMT_CBC_CFC;
Shengjiu Wang08b54b5e2020-07-21 11:41:49 +0800707
708 /* Override dai_fmt with value from DT */
709 priv->dai_fmt = daifmt;
710 }
711
712 /* Change direction according to format */
Mark Brown8fcfd342021-09-21 22:35:28 +0100713 if (priv->dai_fmt & SND_SOC_DAIFMT_CBP_CFP) {
Shengjiu Wang08b54b5e2020-07-21 11:41:49 +0800714 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_IN;
715 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_IN;
716 }
717
Mark Brown8fcfd342021-09-21 22:35:28 +0100718 of_node_put(bitclkprovider);
719 of_node_put(frameprovider);
Shengjiu Wang08b54b5e2020-07-21 11:41:49 +0800720
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200721 if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) {
Shengjiu Wang4b1d5172021-06-02 14:42:12 +0800722 dev_dbg(&pdev->dev, "failed to find codec device\n");
Shengjiu Wange396dec2020-06-04 14:25:30 +0800723 ret = -EPROBE_DEFER;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200724 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800725 }
726
727 /* Common settings for corresponding Freescale CPU DAI driver */
Rob Herring1d52a742018-12-05 13:50:49 -0600728 if (of_node_name_eq(cpu_np, "ssi")) {
Nicolin Chen708b4352014-07-30 19:27:38 +0800729 /* Only SSI needs to configure AUDMUX */
730 ret = fsl_asoc_card_audmux_init(np, priv);
731 if (ret) {
732 dev_err(&pdev->dev, "failed to init audmux\n");
Shengjiu Wang5f376712014-08-18 16:38:39 +0800733 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800734 }
Rob Herring1d52a742018-12-05 13:50:49 -0600735 } else if (of_node_name_eq(cpu_np, "esai")) {
Shengjiu Wanga8fd5ca2020-08-10 16:11:43 +0800736 struct clk *esai_clk = clk_get(&cpu_pdev->dev, "extal");
737
738 if (!IS_ERR(esai_clk)) {
739 priv->cpu_priv.sysclk_freq[TX] = clk_get_rate(esai_clk);
740 priv->cpu_priv.sysclk_freq[RX] = clk_get_rate(esai_clk);
741 clk_put(esai_clk);
742 } else if (PTR_ERR(esai_clk) == -EPROBE_DEFER) {
743 ret = -EPROBE_DEFER;
744 goto asrc_fail;
745 }
746
Nicolin Chen708b4352014-07-30 19:27:38 +0800747 priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
748 priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
Rob Herring1d52a742018-12-05 13:50:49 -0600749 } else if (of_node_name_eq(cpu_np, "sai")) {
Nicolin Chen708b4352014-07-30 19:27:38 +0800750 priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
751 priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
752 }
753
Nicolin Chen708b4352014-07-30 19:27:38 +0800754 /* Initialize sound card */
755 priv->pdev = pdev;
756 priv->card.dev = &pdev->dev;
Nicolas Cavallaria8437f02021-05-27 18:34:09 +0200757 priv->card.owner = THIS_MODULE;
Shengjiu Wang039652a2020-06-17 12:48:25 +0800758 ret = snd_soc_of_parse_card_name(&priv->card, "model");
759 if (ret) {
760 snprintf(priv->name, sizeof(priv->name), "%s-audio",
761 fsl_asoc_card_is_ac97(priv) ? "ac97" : codec_dev_name);
762 priv->card.name = priv->name;
763 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800764 priv->card.dai_link = priv->dai_link;
Nicolin Chen708b4352014-07-30 19:27:38 +0800765 priv->card.late_probe = fsl_asoc_card_late_probe;
Nicolin Chen708b4352014-07-30 19:27:38 +0800766 priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
767 priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
768
Nicolin Chen089dfaf2016-01-30 23:07:00 -0800769 /* Drop the second half of DAPM routes -- ASRC */
770 if (!asrc_pdev)
771 priv->card.num_dapm_routes /= 2;
772
Shengjiu Wang039652a2020-06-17 12:48:25 +0800773 if (of_property_read_bool(np, "audio-routing")) {
774 ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
775 if (ret) {
776 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
777 goto asrc_fail;
778 }
Nicolin Chen31858782015-02-14 17:22:50 -0800779 }
780
Nicolin Chen708b4352014-07-30 19:27:38 +0800781 /* Normal DAI Link */
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900782 priv->dai_link[0].cpus->of_node = cpu_np;
783 priv->dai_link[0].codecs->dai_name = codec_dai_name;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200784
785 if (!fsl_asoc_card_is_ac97(priv))
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900786 priv->dai_link[0].codecs->of_node = codec_np;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200787 else {
788 u32 idx;
789
790 ret = of_property_read_u32(cpu_np, "cell-index", &idx);
791 if (ret) {
792 dev_err(&pdev->dev,
793 "cannot get CPU index property\n");
794 goto asrc_fail;
795 }
796
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900797 priv->dai_link[0].codecs->name =
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200798 devm_kasprintf(&pdev->dev, GFP_KERNEL,
799 "ac97-codec.%u",
800 (unsigned int)idx);
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900801 if (!priv->dai_link[0].codecs->name) {
Arvind Yadav7add71b2017-09-21 10:50:03 +0530802 ret = -ENOMEM;
803 goto asrc_fail;
804 }
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200805 }
806
Kuninori Morimoto9998d3e2019-06-28 10:47:18 +0900807 priv->dai_link[0].platforms->of_node = cpu_np;
Nicolin Chen708b4352014-07-30 19:27:38 +0800808 priv->dai_link[0].dai_fmt = priv->dai_fmt;
809 priv->card.num_links = 1;
810
811 if (asrc_pdev) {
812 /* DPCM DAI Links only if ASRC exsits */
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900813 priv->dai_link[1].cpus->of_node = asrc_np;
Kuninori Morimoto9998d3e2019-06-28 10:47:18 +0900814 priv->dai_link[1].platforms->of_node = asrc_np;
Kuninori Morimoto893f1952019-06-06 13:15:11 +0900815 priv->dai_link[2].codecs->dai_name = codec_dai_name;
816 priv->dai_link[2].codecs->of_node = codec_np;
817 priv->dai_link[2].codecs->name =
818 priv->dai_link[0].codecs->name;
819 priv->dai_link[2].cpus->of_node = cpu_np;
Nicolin Chen708b4352014-07-30 19:27:38 +0800820 priv->dai_link[2].dai_fmt = priv->dai_fmt;
821 priv->card.num_links = 3;
822
823 ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
824 &priv->asrc_rate);
825 if (ret) {
826 dev_err(&pdev->dev, "failed to get output rate\n");
827 ret = -EINVAL;
Shengjiu Wang5f376712014-08-18 16:38:39 +0800828 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800829 }
830
Shengjiu Wang859e3642020-04-16 20:25:33 +0800831 ret = of_property_read_u32(asrc_np, "fsl,asrc-format",
832 &priv->asrc_format);
Nicolin Chen708b4352014-07-30 19:27:38 +0800833 if (ret) {
Shengjiu Wang859e3642020-04-16 20:25:33 +0800834 /* Fallback to old binding; translate to asrc_format */
835 ret = of_property_read_u32(asrc_np, "fsl,asrc-width",
836 &width);
837 if (ret) {
838 dev_err(&pdev->dev,
839 "failed to decide output format\n");
840 goto asrc_fail;
841 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800842
Shengjiu Wang859e3642020-04-16 20:25:33 +0800843 if (width == 24)
844 priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
845 else
846 priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
847 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800848 }
849
850 /* Finish card registering */
851 platform_set_drvdata(pdev, priv);
852 snd_soc_card_set_drvdata(&priv->card, priv);
853
854 ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
Shengjiu Wang3b171192020-07-15 22:09:39 +0800855 if (ret) {
Kuninori Morimoto2e6f5572021-12-14 11:08:34 +0900856 dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
Shengjiu Wang3b171192020-07-15 22:09:39 +0800857 goto asrc_fail;
858 }
859
860 /*
861 * Properties "hp-det-gpio" and "mic-det-gpio" are optional, and
862 * asoc_simple_init_jack uses these properties for creating
863 * Headphone Jack and Microphone Jack.
864 *
865 * The notifier is initialized in snd_soc_card_jack_new(), then
866 * snd_soc_jack_notifier_register can be called.
867 */
868 if (of_property_read_bool(np, "hp-det-gpio")) {
869 ret = asoc_simple_init_jack(&priv->card, &priv->hp_jack,
870 1, NULL, "Headphone Jack");
871 if (ret)
872 goto asrc_fail;
873
874 snd_soc_jack_notifier_register(&priv->hp_jack.jack, &hp_jack_nb);
875 }
876
877 if (of_property_read_bool(np, "mic-det-gpio")) {
878 ret = asoc_simple_init_jack(&priv->card, &priv->mic_jack,
879 0, NULL, "Mic Jack");
880 if (ret)
881 goto asrc_fail;
882
883 snd_soc_jack_notifier_register(&priv->mic_jack.jack, &mic_jack_nb);
884 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800885
Shengjiu Wang5f376712014-08-18 16:38:39 +0800886asrc_fail:
887 of_node_put(asrc_np);
Nicolin Chen708b4352014-07-30 19:27:38 +0800888 of_node_put(codec_np);
wen yang11907e9d2019-02-02 14:53:16 +0000889 put_device(&cpu_pdev->dev);
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200890fail:
Nicolin Chen708b4352014-07-30 19:27:38 +0800891 of_node_put(cpu_np);
892
893 return ret;
894}
895
896static const struct of_device_id fsl_asoc_card_dt_ids[] = {
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200897 { .compatible = "fsl,imx-audio-ac97", },
Nicolin Chen708b4352014-07-30 19:27:38 +0800898 { .compatible = "fsl,imx-audio-cs42888", },
Felipe F. Tonello57e756d2016-01-29 11:01:00 +0000899 { .compatible = "fsl,imx-audio-cs427x", },
Matthias Schifferb5074752020-08-21 09:11:53 +0200900 { .compatible = "fsl,imx-audio-tlv320aic32x4", },
Ariel D'Alessandro8c9b9cfb2021-11-19 12:32:48 -0300901 { .compatible = "fsl,imx-audio-tlv320aic31xx", },
Nicolin Chen708b4352014-07-30 19:27:38 +0800902 { .compatible = "fsl,imx-audio-sgtl5000", },
903 { .compatible = "fsl,imx-audio-wm8962", },
Zidan Wang50e0ee02015-08-14 19:11:09 +0800904 { .compatible = "fsl,imx-audio-wm8960", },
Shengjiu Wang039652a2020-06-17 12:48:25 +0800905 { .compatible = "fsl,imx-audio-mqs", },
Shengjiu Wang3cd99022020-06-23 14:52:46 +0800906 { .compatible = "fsl,imx-audio-wm8524", },
Shengjiu Wang77f1ff752020-11-30 11:57:47 +0800907 { .compatible = "fsl,imx-audio-si476x", },
Shengjiu Wangefd0b162021-03-17 21:05:02 +0800908 { .compatible = "fsl,imx-audio-wm8958", },
Nicolin Chen708b4352014-07-30 19:27:38 +0800909 {}
910};
Luis de Bethencourt5226f232015-09-03 12:57:47 +0200911MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
Nicolin Chen708b4352014-07-30 19:27:38 +0800912
913static struct platform_driver fsl_asoc_card_driver = {
914 .probe = fsl_asoc_card_probe,
915 .driver = {
916 .name = "fsl-asoc-card",
917 .pm = &snd_soc_pm_ops,
918 .of_match_table = fsl_asoc_card_dt_ids,
919 },
920};
921module_platform_driver(fsl_asoc_card_driver);
922
923MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
924MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
925MODULE_ALIAS("platform:fsl-asoc-card");
926MODULE_LICENSE("GPL");