Linux Audio

Check our new training course

Loading...
v6.13.7
   1// 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>
   8
   9#include <linux/clk.h>
  10#include <linux/i2c.h>
  11#include <linux/module.h>
  12#include <linux/of_platform.h>
  13#if IS_ENABLED(CONFIG_SND_AC97_CODEC)
  14#include <sound/ac97_codec.h>
  15#endif
  16#include <sound/pcm_params.h>
  17#include <sound/soc.h>
  18#include <sound/jack.h>
  19#include <sound/simple_card_utils.h>
  20
  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"
  27#include "../codecs/wm8960.h"
  28#include "../codecs/wm8994.h"
  29#include "../codecs/tlv320aic31xx.h"
  30#include "../codecs/nau8822.h"
  31#include "../codecs/wm8904.h"
  32
  33#define DRIVER_NAME "fsl-asoc-card"
  34
  35#define CS427x_SYSCLK_MCLK 0
  36
  37#define RX 0
  38#define TX 1
  39
  40/* Default DAI format without Master and Slave flag */
  41#define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
  42
  43/**
  44 * struct codec_priv - CODEC private data
  45 * @mclk: Main clock of the CODEC
  46 * @mclk_freq: Clock rate of MCLK
  47 * @free_freq: Clock rate of MCLK for hw_free()
  48 * @mclk_id: MCLK (or main clock) id for set_sysclk()
  49 * @fll_id: FLL (or secordary clock) id for set_sysclk()
  50 * @pll_id: PLL id for set_pll()
  51 */
  52struct codec_priv {
  53	struct clk *mclk;
  54	unsigned long mclk_freq;
  55	unsigned long free_freq;
  56	u32 mclk_id;
  57	int fll_id;
  58	int pll_id;
  59};
  60
  61/**
  62 * struct cpu_priv - CPU private data
  63 * @sysclk_freq: SYSCLK rates for set_sysclk()
  64 * @sysclk_dir: SYSCLK directions for set_sysclk()
  65 * @sysclk_id: SYSCLK ids for set_sysclk()
 
  66 * @slot_width: Slot width of each frame
  67 * @slot_num: Number of slots of each frame
  68 *
  69 * Note: [1] for tx and [0] for rx
  70 */
  71struct cpu_priv {
  72	unsigned long sysclk_freq[2];
  73	u32 sysclk_dir[2];
  74	u32 sysclk_id[2];
  75	u32 slot_width;
  76	u32 slot_num;
  77};
  78
  79/**
  80 * struct fsl_asoc_card_priv - Freescale Generic ASOC card private data
  81 * @dai_link: DAI link structure including normal one and DPCM link
  82 * @hp_jack: Headphone Jack structure
  83 * @mic_jack: Microphone Jack structure
  84 * @pdev: platform device pointer
  85 * @codec_priv: CODEC private data
  86 * @cpu_priv: CPU private data
  87 * @card: ASoC card structure
  88 * @streams: Mask of current active streams
  89 * @sample_rate: Current sample rate
  90 * @sample_format: Current sample format
  91 * @asrc_rate: ASRC sample rate used by Back-Ends
  92 * @asrc_format: ASRC sample format used by Back-Ends
  93 * @dai_fmt: DAI format between CPU and CODEC
  94 * @name: Card name
  95 */
  96
  97struct fsl_asoc_card_priv {
  98	struct snd_soc_dai_link dai_link[3];
  99	struct simple_util_jack hp_jack;
 100	struct simple_util_jack mic_jack;
 101	struct platform_device *pdev;
 102	struct codec_priv codec_priv[2];
 103	struct cpu_priv cpu_priv;
 104	struct snd_soc_card card;
 105	u8 streams;
 106	u32 sample_rate;
 107	snd_pcm_format_t sample_format;
 108	u32 asrc_rate;
 109	snd_pcm_format_t asrc_format;
 110	u32 dai_fmt;
 111	char name[32];
 112};
 113
 114/*
 115 * This dapm route map exists for DPCM link only.
 116 * The other routes shall go through Device Tree.
 117 *
 118 * Note: keep all ASRC routes in the second half
 119 *	 to drop them easily for non-ASRC cases.
 120 */
 121static const struct snd_soc_dapm_route audio_map[] = {
 122	/* 1st half -- Normal DAPM routes */
 123	{"Playback",  NULL, "CPU-Playback"},
 124	{"CPU-Capture",  NULL, "Capture"},
 125	/* 2nd half -- ASRC DAPM routes */
 126	{"CPU-Playback",  NULL, "ASRC-Playback"},
 127	{"ASRC-Capture",  NULL, "CPU-Capture"},
 128};
 129
 130static const struct snd_soc_dapm_route audio_map_ac97[] = {
 131	/* 1st half -- Normal DAPM routes */
 132	{"AC97 Playback",  NULL, "CPU AC97 Playback"},
 133	{"CPU AC97 Capture",  NULL, "AC97 Capture"},
 134	/* 2nd half -- ASRC DAPM routes */
 135	{"CPU AC97 Playback",  NULL, "ASRC-Playback"},
 136	{"ASRC-Capture",  NULL, "CPU AC97 Capture"},
 137};
 138
 139static const struct snd_soc_dapm_route audio_map_tx[] = {
 140	/* 1st half -- Normal DAPM routes */
 141	{"Playback",  NULL, "CPU-Playback"},
 142	/* 2nd half -- ASRC DAPM routes */
 143	{"CPU-Playback",  NULL, "ASRC-Playback"},
 144};
 145
 146static const struct snd_soc_dapm_route audio_map_rx[] = {
 147	/* 1st half -- Normal DAPM routes */
 148	{"CPU-Capture",  NULL, "Capture"},
 149	/* 2nd half -- ASRC DAPM routes */
 150	{"ASRC-Capture",  NULL, "CPU-Capture"},
 151};
 152
 153/* Add all possible widgets into here without being redundant */
 154static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
 155	SND_SOC_DAPM_LINE("Line Out Jack", NULL),
 156	SND_SOC_DAPM_LINE("Line In Jack", NULL),
 157	SND_SOC_DAPM_HP("Headphone Jack", NULL),
 158	SND_SOC_DAPM_SPK("Ext Spk", NULL),
 159	SND_SOC_DAPM_MIC("Mic Jack", NULL),
 160	SND_SOC_DAPM_MIC("AMIC", NULL),
 161	SND_SOC_DAPM_MIC("DMIC", NULL),
 162};
 163
 164static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
 165{
 166	return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
 167}
 168
 169static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
 170				   struct snd_pcm_hw_params *params)
 171{
 172	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
 173	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
 174	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
 175	struct codec_priv *codec_priv;
 176	struct snd_soc_dai *codec_dai;
 177	struct cpu_priv *cpu_priv = &priv->cpu_priv;
 178	struct device *dev = rtd->card->dev;
 179	unsigned int pll_out;
 180	int codec_idx;
 181	int ret;
 182
 183	priv->sample_rate = params_rate(params);
 184	priv->sample_format = params_format(params);
 185	priv->streams |= BIT(substream->stream);
 186
 187	if (fsl_asoc_card_is_ac97(priv))
 
 
 
 
 
 
 
 188		return 0;
 189
 190	/* Specific configurations of DAIs starts from here */
 191	ret = snd_soc_dai_set_sysclk(snd_soc_rtd_to_cpu(rtd, 0), cpu_priv->sysclk_id[tx],
 192				     cpu_priv->sysclk_freq[tx],
 193				     cpu_priv->sysclk_dir[tx]);
 194	if (ret && ret != -ENOTSUPP) {
 195		dev_err(dev, "failed to set sysclk for cpu dai\n");
 196		goto fail;
 197	}
 198
 199	if (cpu_priv->slot_width) {
 200		if (!cpu_priv->slot_num)
 201			cpu_priv->slot_num = 2;
 202
 203		ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3,
 204					       cpu_priv->slot_num,
 205					       cpu_priv->slot_width);
 206		if (ret && ret != -ENOTSUPP) {
 207			dev_err(dev, "failed to set TDM slot for cpu dai\n");
 208			goto fail;
 209		}
 210	}
 211
 212	/* Specific configuration for PLL */
 213	for_each_rtd_codec_dais(rtd, codec_idx, codec_dai) {
 214		codec_priv = &priv->codec_priv[codec_idx];
 215
 216		if (codec_priv->pll_id >= 0 && codec_priv->fll_id >= 0) {
 217			if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
 218				pll_out = priv->sample_rate * 384;
 219			else
 220				pll_out = priv->sample_rate * 256;
 221
 222			ret = snd_soc_dai_set_pll(codec_dai,
 223						codec_priv->pll_id,
 224						codec_priv->mclk_id,
 225						codec_priv->mclk_freq, pll_out);
 226			if (ret) {
 227				dev_err(dev, "failed to start FLL: %d\n", ret);
 228				goto fail;
 229			}
 230
 231			ret = snd_soc_dai_set_sysclk(codec_dai,
 232						codec_priv->fll_id,
 233						pll_out, SND_SOC_CLOCK_IN);
 234
 235			if (ret && ret != -ENOTSUPP) {
 236				dev_err(dev, "failed to set SYSCLK: %d\n", ret);
 237				goto fail;
 238			}
 239		}
 240	}
 241
 242	return 0;
 243
 244fail:
 245	priv->streams &= ~BIT(substream->stream);
 246	return ret;
 247}
 248
 249static int fsl_asoc_card_hw_free(struct snd_pcm_substream *substream)
 250{
 251	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
 252	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
 253	struct codec_priv *codec_priv;
 254	struct snd_soc_dai *codec_dai;
 255	struct device *dev = rtd->card->dev;
 256	int codec_idx;
 257	int ret;
 258
 259	priv->streams &= ~BIT(substream->stream);
 260
 261	for_each_rtd_codec_dais(rtd, codec_idx, codec_dai) {
 262		codec_priv = &priv->codec_priv[codec_idx];
 263
 264		if (!priv->streams && codec_priv->pll_id >= 0 && codec_priv->fll_id >= 0) {
 265			/* Force freq to be free_freq to avoid error message in codec */
 266			ret = snd_soc_dai_set_sysclk(codec_dai,
 267						codec_priv->mclk_id,
 268						codec_priv->free_freq,
 269						SND_SOC_CLOCK_IN);
 270			if (ret) {
 271				dev_err(dev, "failed to switch away from FLL: %d\n", ret);
 272				return ret;
 273			}
 274
 275			ret = snd_soc_dai_set_pll(codec_dai,
 276						codec_priv->pll_id, 0, 0, 0);
 277			if (ret && ret != -ENOTSUPP) {
 278				dev_err(dev, "failed to stop FLL: %d\n", ret);
 279				return ret;
 280			}
 281		}
 282	}
 283
 284	return 0;
 285}
 286
 287static const struct snd_soc_ops fsl_asoc_card_ops = {
 288	.hw_params = fsl_asoc_card_hw_params,
 289	.hw_free = fsl_asoc_card_hw_free,
 290};
 291
 292static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
 293			      struct snd_pcm_hw_params *params)
 294{
 295	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
 296	struct snd_interval *rate;
 297	struct snd_mask *mask;
 298
 299	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
 300	rate->max = rate->min = priv->asrc_rate;
 301
 302	mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
 303	snd_mask_none(mask);
 304	snd_mask_set_format(mask, priv->asrc_format);
 305
 306	return 0;
 307}
 308
 309static const struct snd_soc_dai_link fsl_asoc_card_dai[] = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 310	/* Default ASoC DAI Link*/
 311	{
 312		.name = "HiFi",
 313		.stream_name = "HiFi",
 314		.ops = &fsl_asoc_card_ops,
 
 315	},
 316	/* DPCM Link between Front-End and Back-End (Optional) */
 317	{
 318		.name = "HiFi-ASRC-FE",
 319		.stream_name = "HiFi-ASRC-FE",
 
 
 320		.dynamic = 1,
 
 321	},
 322	{
 323		.name = "HiFi-ASRC-BE",
 324		.stream_name = "HiFi-ASRC-BE",
 325		.be_hw_params_fixup = be_hw_params_fixup,
 326		.ops = &fsl_asoc_card_ops,
 
 
 327		.no_pcm = 1,
 
 328	},
 329};
 330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 331static int fsl_asoc_card_audmux_init(struct device_node *np,
 332				     struct fsl_asoc_card_priv *priv)
 333{
 334	struct device *dev = &priv->pdev->dev;
 335	u32 int_ptcr = 0, ext_ptcr = 0;
 336	int int_port, ext_port;
 337	int ret;
 338
 339	ret = of_property_read_u32(np, "mux-int-port", &int_port);
 340	if (ret) {
 341		dev_err(dev, "mux-int-port missing or invalid\n");
 342		return ret;
 343	}
 344	ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
 345	if (ret) {
 346		dev_err(dev, "mux-ext-port missing or invalid\n");
 347		return ret;
 348	}
 349
 350	/*
 351	 * The port numbering in the hardware manual starts at 1, while
 352	 * the AUDMUX API expects it starts at 0.
 353	 */
 354	int_port--;
 355	ext_port--;
 356
 357	/*
 358	 * Use asynchronous mode (6 wires) for all cases except AC97.
 359	 * If only 4 wires are needed, just set SSI into
 360	 * synchronous mode and enable 4 PADs in IOMUX.
 361	 */
 362	switch (priv->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
 363	case SND_SOC_DAIFMT_CBP_CFP:
 364		int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
 365			   IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
 366			   IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
 367			   IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
 368			   IMX_AUDMUX_V2_PTCR_RFSDIR |
 369			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
 370			   IMX_AUDMUX_V2_PTCR_TFSDIR |
 371			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
 372		break;
 373	case SND_SOC_DAIFMT_CBP_CFC:
 374		int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
 375			   IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
 376			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
 377			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
 378		ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
 379			   IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
 380			   IMX_AUDMUX_V2_PTCR_RFSDIR |
 381			   IMX_AUDMUX_V2_PTCR_TFSDIR;
 382		break;
 383	case SND_SOC_DAIFMT_CBC_CFP:
 384		int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
 385			   IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
 386			   IMX_AUDMUX_V2_PTCR_RFSDIR |
 387			   IMX_AUDMUX_V2_PTCR_TFSDIR;
 388		ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
 389			   IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
 390			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
 391			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
 392		break;
 393	case SND_SOC_DAIFMT_CBC_CFC:
 394		ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
 395			   IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
 396			   IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
 397			   IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
 398			   IMX_AUDMUX_V2_PTCR_RFSDIR |
 399			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
 400			   IMX_AUDMUX_V2_PTCR_TFSDIR |
 401			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
 402		break;
 403	default:
 404		if (!fsl_asoc_card_is_ac97(priv))
 405			return -EINVAL;
 406	}
 407
 408	if (fsl_asoc_card_is_ac97(priv)) {
 409		int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
 410			   IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
 411			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
 412		ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
 413			   IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
 414			   IMX_AUDMUX_V2_PTCR_TFSDIR;
 415	}
 416
 417	/* Asynchronous mode can not be set along with RCLKDIR */
 418	if (!fsl_asoc_card_is_ac97(priv)) {
 419		unsigned int pdcr =
 420				IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
 421
 422		ret = imx_audmux_v2_configure_port(int_port, 0,
 423						   pdcr);
 424		if (ret) {
 425			dev_err(dev, "audmux internal port setup failed\n");
 426			return ret;
 427		}
 428	}
 429
 430	ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
 431					   IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
 432	if (ret) {
 433		dev_err(dev, "audmux internal port setup failed\n");
 434		return ret;
 435	}
 436
 437	if (!fsl_asoc_card_is_ac97(priv)) {
 438		unsigned int pdcr =
 439				IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
 440
 441		ret = imx_audmux_v2_configure_port(ext_port, 0,
 442						   pdcr);
 443		if (ret) {
 444			dev_err(dev, "audmux external port setup failed\n");
 445			return ret;
 446		}
 447	}
 448
 449	ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
 450					   IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
 451	if (ret) {
 452		dev_err(dev, "audmux external port setup failed\n");
 453		return ret;
 454	}
 455
 456	return 0;
 457}
 458
 459static int fsl_asoc_card_spdif_init(struct device_node *codec_np[],
 460				    struct device_node *cpu_np,
 461				    const char *codec_dai_name[],
 462				    struct fsl_asoc_card_priv *priv)
 463{
 464	struct device *dev = &priv->pdev->dev;
 465	struct device_node *np = dev->of_node;
 466
 467	if (!of_node_name_eq(cpu_np, "spdif")) {
 468		dev_err(dev, "CPU phandle invalid, should be an SPDIF device\n");
 469		return -EINVAL;
 470	}
 471
 472	priv->dai_link[0].playback_only = true;
 473	priv->dai_link[0].capture_only = true;
 474
 475	for (int i = 0; i < 2; i++) {
 476		if (!codec_np[i])
 477			break;
 478
 479		if (of_device_is_compatible(codec_np[i], "linux,spdif-dit")) {
 480			priv->dai_link[0].capture_only = false;
 481			codec_dai_name[i] = "dit-hifi";
 482		} else if (of_device_is_compatible(codec_np[i], "linux,spdif-dir")) {
 483			priv->dai_link[0].playback_only = false;
 484			codec_dai_name[i] = "dir-hifi";
 485		}
 486	}
 487
 488	// Old SPDIF DT binding
 489	if (!codec_np[0]) {
 490		codec_dai_name[0] = snd_soc_dummy_dlc.dai_name;
 491		if (of_property_read_bool(np, "spdif-out"))
 492			priv->dai_link[0].capture_only = false;
 493		if (of_property_read_bool(np, "spdif-in"))
 494			priv->dai_link[0].playback_only = false;
 495	}
 496
 497	if (priv->dai_link[0].playback_only && priv->dai_link[0].capture_only) {
 498		dev_err(dev, "no enabled S/PDIF DAI link\n");
 499		return -EINVAL;
 500	}
 501
 502	if (priv->dai_link[0].playback_only) {
 503		priv->dai_link[1].playback_only = true;
 504		priv->dai_link[2].playback_only = true;
 505		priv->card.dapm_routes = audio_map_tx;
 506		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
 507	} else if (priv->dai_link[0].capture_only) {
 508		priv->dai_link[1].capture_only = true;
 509		priv->dai_link[2].capture_only = true;
 510		priv->card.dapm_routes = audio_map_rx;
 511		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);
 512	}
 513
 514	// No DAPM routes with old bindings and dummy codec
 515	if (!codec_np[0]) {
 516		priv->card.dapm_routes = NULL;
 517		priv->card.num_dapm_routes = 0;
 518	}
 519
 520	if (codec_np[0] && codec_np[1]) {
 521		priv->dai_link[0].num_codecs = 2;
 522		priv->dai_link[2].num_codecs = 2;
 523	}
 524
 525	return 0;
 526}
 527
 528static int hp_jack_event(struct notifier_block *nb, unsigned long event,
 529			 void *data)
 530{
 531	struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
 532	struct snd_soc_dapm_context *dapm = &jack->card->dapm;
 533
 534	if (event & SND_JACK_HEADPHONE)
 535		/* Disable speaker if headphone is plugged in */
 536		return snd_soc_dapm_disable_pin(dapm, "Ext Spk");
 537	else
 538		return snd_soc_dapm_enable_pin(dapm, "Ext Spk");
 539}
 540
 541static struct notifier_block hp_jack_nb = {
 542	.notifier_call = hp_jack_event,
 543};
 544
 545static int mic_jack_event(struct notifier_block *nb, unsigned long event,
 546			  void *data)
 547{
 548	struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
 549	struct snd_soc_dapm_context *dapm = &jack->card->dapm;
 550
 551	if (event & SND_JACK_MICROPHONE)
 552		/* Disable dmic if microphone is plugged in */
 553		return snd_soc_dapm_disable_pin(dapm, "DMIC");
 554	else
 555		return snd_soc_dapm_enable_pin(dapm, "DMIC");
 556}
 557
 558static struct notifier_block mic_jack_nb = {
 559	.notifier_call = mic_jack_event,
 560};
 561
 562static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
 563{
 564	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
 565	struct snd_soc_pcm_runtime *rtd = list_first_entry(
 566			&card->rtd_list, struct snd_soc_pcm_runtime, list);
 567	struct snd_soc_dai *codec_dai;
 568	struct codec_priv *codec_priv;
 569	struct device *dev = card->dev;
 570	int codec_idx;
 571	int ret;
 572
 573	if (fsl_asoc_card_is_ac97(priv)) {
 574#if IS_ENABLED(CONFIG_SND_AC97_CODEC)
 575		struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
 576		struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
 577
 578		/*
 579		 * Use slots 3/4 for S/PDIF so SSI won't try to enable
 580		 * other slots and send some samples there
 581		 * due to SLOTREQ bits for S/PDIF received from codec
 582		 */
 583		snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
 584				     AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
 585#endif
 586
 587		return 0;
 588	}
 589
 590	for_each_rtd_codec_dais(rtd, codec_idx, codec_dai) {
 591		codec_priv = &priv->codec_priv[codec_idx];
 592
 593		ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
 594					codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
 595		if (ret && ret != -ENOTSUPP) {
 596			dev_err(dev, "failed to set sysclk in %s\n", __func__);
 597			return ret;
 598		}
 599
 600		if (!IS_ERR_OR_NULL(codec_priv->mclk))
 601			clk_prepare_enable(codec_priv->mclk);
 602	}
 603
 604	return 0;
 605}
 606
 607static int fsl_asoc_card_probe(struct platform_device *pdev)
 608{
 609	struct device_node *cpu_np, *asrc_np;
 610	struct snd_soc_dai_link_component *codec_comp;
 611	struct device_node *codec_np[2];
 612	struct device_node *np = pdev->dev.of_node;
 613	struct platform_device *asrc_pdev = NULL;
 614	struct device_node *bitclkprovider = NULL;
 615	struct device_node *frameprovider = NULL;
 616	struct platform_device *cpu_pdev;
 617	struct fsl_asoc_card_priv *priv;
 618	struct device *codec_dev[2] = { NULL, NULL };
 619	struct snd_soc_dai_link_component *dlc;
 620	const char *codec_dai_name[2];
 621	const char *codec_dev_name[2];
 622	u32 asrc_fmt = 0;
 623	int codec_idx;
 624	u32 width;
 625	int ret;
 626
 627	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
 628	if (!priv)
 629		return -ENOMEM;
 630
 631	priv->pdev = pdev;
 632
 633	cpu_np = of_parse_phandle(np, "audio-cpu", 0);
 634	/* Give a chance to old DT bindings */
 635	if (!cpu_np)
 636		cpu_np = of_parse_phandle(np, "ssi-controller", 0);
 637	if (!cpu_np)
 638		cpu_np = of_parse_phandle(np, "spdif-controller", 0);
 639	if (!cpu_np) {
 640		dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
 641		ret = -EINVAL;
 642		goto fail;
 643	}
 644
 645	cpu_pdev = of_find_device_by_node(cpu_np);
 646	if (!cpu_pdev) {
 647		dev_err(&pdev->dev, "failed to find CPU DAI device\n");
 648		ret = -EINVAL;
 649		goto fail;
 650	}
 651
 652	codec_np[0] = of_parse_phandle(np, "audio-codec", 0);
 653	codec_np[1] = of_parse_phandle(np, "audio-codec", 1);
 654
 655	for (codec_idx = 0; codec_idx < 2; codec_idx++) {
 656		if (codec_np[codec_idx]) {
 657			struct platform_device *codec_pdev;
 658			struct i2c_client *codec_i2c;
 659
 660			codec_i2c = of_find_i2c_device_by_node(codec_np[codec_idx]);
 661			if (codec_i2c) {
 662				codec_dev[codec_idx] = &codec_i2c->dev;
 663				codec_dev_name[codec_idx] = codec_i2c->name;
 664			}
 665			if (!codec_dev[codec_idx]) {
 666				codec_pdev = of_find_device_by_node(codec_np[codec_idx]);
 667				if (codec_pdev) {
 668					codec_dev[codec_idx] = &codec_pdev->dev;
 669					codec_dev_name[codec_idx] = codec_pdev->name;
 670				}
 671			}
 672		}
 673	}
 674
 675	asrc_np = of_parse_phandle(np, "audio-asrc", 0);
 676	if (asrc_np)
 677		asrc_pdev = of_find_device_by_node(asrc_np);
 678
 679	/* Get the MCLK rate only, and leave it controlled by CODEC drivers */
 680	for (codec_idx = 0; codec_idx < 2; codec_idx++) {
 681		if (codec_dev[codec_idx]) {
 682			struct clk *codec_clk = clk_get(codec_dev[codec_idx], NULL);
 683
 684			if (!IS_ERR(codec_clk)) {
 685				priv->codec_priv[codec_idx].mclk_freq = clk_get_rate(codec_clk);
 686				clk_put(codec_clk);
 687			}
 688		}
 689	}
 690
 691	/* Default sample rate and format, will be updated in hw_params() */
 692	priv->sample_rate = 44100;
 693	priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
 694
 695	/* Assign a default DAI format, and allow each card to overwrite it */
 696	priv->dai_fmt = DAI_FMT_BASE;
 697
 698	memcpy(priv->dai_link, fsl_asoc_card_dai,
 699	       sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
 700	/*
 701	 * "Default ASoC DAI Link": 1 cpus, 2 codecs, 1 platforms
 702	 * "DPCM Link Front-End":  1 cpus, 1 codecs (dummy), 1 platforms
 703	 * "DPCM Link Back-End": 1 cpus, 2 codecs
 704	 * totally 10 components
 705	 */
 706	dlc = devm_kcalloc(&pdev->dev, 10, sizeof(*dlc), GFP_KERNEL);
 707	if (!dlc) {
 708		ret = -ENOMEM;
 709		goto asrc_fail;
 710	}
 711
 712	priv->dai_link[0].cpus = &dlc[0];
 713	priv->dai_link[0].num_cpus = 1;
 714	priv->dai_link[0].codecs = &dlc[1];
 715	priv->dai_link[0].num_codecs = 1;
 716	priv->dai_link[0].platforms = &dlc[3];
 717	priv->dai_link[0].num_platforms = 1;
 718
 719	priv->dai_link[1].cpus = &dlc[4];
 720	priv->dai_link[1].num_cpus = 1;
 721	priv->dai_link[1].codecs = &dlc[5];
 722	priv->dai_link[1].num_codecs = 0; /* dummy */
 723	priv->dai_link[1].platforms = &dlc[6];
 724	priv->dai_link[1].num_platforms = 1;
 725
 726	priv->dai_link[2].cpus = &dlc[7];
 727	priv->dai_link[2].num_cpus = 1;
 728	priv->dai_link[2].codecs = &dlc[8];
 729	priv->dai_link[2].num_codecs = 1;
 730
 731	priv->card.dapm_routes = audio_map;
 732	priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
 733	priv->card.driver_name = DRIVER_NAME;
 734
 735	for (codec_idx = 0; codec_idx < 2; codec_idx++) {
 736		priv->codec_priv[codec_idx].fll_id = -1;
 737		priv->codec_priv[codec_idx].pll_id = -1;
 738	}
 739
 740	/* Diversify the card configurations */
 741	if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
 742		codec_dai_name[0] = "cs42888";
 743		priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv[0].mclk_freq;
 744		priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv[0].mclk_freq;
 
 745		priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
 746		priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
 747		priv->cpu_priv.slot_width = 32;
 748		priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
 749	} else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {
 750		codec_dai_name[0] = "cs4271-hifi";
 751		priv->codec_priv[0].mclk_id = CS427x_SYSCLK_MCLK;
 752		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
 753	} else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
 754		codec_dai_name[0] = "sgtl5000";
 755		priv->codec_priv[0].mclk_id = SGTL5000_SYSCLK;
 756		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
 757	} else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic32x4")) {
 758		codec_dai_name[0] = "tlv320aic32x4-hifi";
 759		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
 760	} else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic31xx")) {
 761		codec_dai_name[0] = "tlv320dac31xx-hifi";
 762		priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
 763		priv->dai_link[1].playback_only = 1;
 764		priv->dai_link[2].playback_only = 1;
 765		priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
 766		priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
 767		priv->card.dapm_routes = audio_map_tx;
 768		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
 769	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
 770		codec_dai_name[0] = "wm8962";
 771		priv->codec_priv[0].mclk_id = WM8962_SYSCLK_MCLK;
 772		priv->codec_priv[0].fll_id = WM8962_SYSCLK_FLL;
 773		priv->codec_priv[0].pll_id = WM8962_FLL;
 774		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
 
 775	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
 776		codec_dai_name[0] = "wm8960-hifi";
 777		priv->codec_priv[0].fll_id = WM8960_SYSCLK_AUTO;
 778		priv->codec_priv[0].pll_id = WM8960_SYSCLK_AUTO;
 779		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
 
 780	} else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
 781		codec_dai_name[0] = "ac97-hifi";
 
 782		priv->dai_fmt = SND_SOC_DAIFMT_AC97;
 783		priv->card.dapm_routes = audio_map_ac97;
 784		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_ac97);
 785	} else if (of_device_is_compatible(np, "fsl,imx-audio-mqs")) {
 786		codec_dai_name[0] = "fsl-mqs-dai";
 787		priv->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
 788				SND_SOC_DAIFMT_CBC_CFC |
 789				SND_SOC_DAIFMT_NB_NF;
 790		priv->dai_link[1].playback_only = 1;
 791		priv->dai_link[2].playback_only = 1;
 792		priv->card.dapm_routes = audio_map_tx;
 793		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
 794	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8524")) {
 795		codec_dai_name[0] = "wm8524-hifi";
 796		priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
 797		priv->dai_link[1].playback_only = 1;
 798		priv->dai_link[2].playback_only = 1;
 799		priv->cpu_priv.slot_width = 32;
 800		priv->card.dapm_routes = audio_map_tx;
 801		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
 802	} else if (of_device_is_compatible(np, "fsl,imx-audio-si476x")) {
 803		codec_dai_name[0] = "si476x-codec";
 804		priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
 805		priv->card.dapm_routes = audio_map_rx;
 806		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);
 807	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8958")) {
 808		codec_dai_name[0] = "wm8994-aif1";
 809		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
 810		priv->codec_priv[0].mclk_id = WM8994_FLL_SRC_MCLK1;
 811		priv->codec_priv[0].fll_id = WM8994_SYSCLK_FLL1;
 812		priv->codec_priv[0].pll_id = WM8994_FLL1;
 813		priv->codec_priv[0].free_freq = priv->codec_priv[0].mclk_freq;
 814		priv->card.dapm_routes = NULL;
 815		priv->card.num_dapm_routes = 0;
 816	} else if (of_device_is_compatible(np, "fsl,imx-audio-nau8822")) {
 817		codec_dai_name[0] = "nau8822-hifi";
 818		priv->codec_priv[0].mclk_id = NAU8822_CLK_MCLK;
 819		priv->codec_priv[0].fll_id = NAU8822_CLK_PLL;
 820		priv->codec_priv[0].pll_id = NAU8822_CLK_PLL;
 821		priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
 822		if (codec_dev[0])
 823			priv->codec_priv[0].mclk = devm_clk_get(codec_dev[0], NULL);
 824	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8904")) {
 825		codec_dai_name[0] = "wm8904-hifi";
 826		priv->codec_priv[0].mclk_id = WM8904_FLL_MCLK;
 827		priv->codec_priv[0].fll_id = WM8904_CLK_FLL;
 828		priv->codec_priv[0].pll_id = WM8904_FLL_MCLK;
 829		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
 830	} else if (of_device_is_compatible(np, "fsl,imx-audio-spdif")) {
 831		ret = fsl_asoc_card_spdif_init(codec_np, cpu_np, codec_dai_name, priv);
 832		if (ret)
 833			goto asrc_fail;
 834	} else {
 835		dev_err(&pdev->dev, "unknown Device Tree compatible\n");
 836		ret = -EINVAL;
 837		goto asrc_fail;
 838	}
 839
 840	/*
 841	 * Allow setting mclk-id from the device-tree node. Otherwise, the
 842	 * default value for each card configuration is used.
 843	 */
 844	for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {
 845		of_property_read_u32_index(np, "mclk-id", codec_idx,
 846					&priv->codec_priv[codec_idx].mclk_id);
 847	}
 848
 849	/* Format info from DT is optional. */
 850	snd_soc_daifmt_parse_clock_provider_as_phandle(np, NULL, &bitclkprovider, &frameprovider);
 851	if (bitclkprovider || frameprovider) {
 852		unsigned int daifmt = snd_soc_daifmt_parse_format(np, NULL);
 853		bool codec_bitclkprovider = false;
 854		bool codec_frameprovider = false;
 855
 856		for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {
 857			if (bitclkprovider && codec_np[codec_idx] == bitclkprovider)
 858				codec_bitclkprovider = true;
 859			if (frameprovider && codec_np[codec_idx] == frameprovider)
 860				codec_frameprovider = true;
 861		}
 862
 863		if (codec_bitclkprovider)
 864			daifmt |= (codec_frameprovider) ?
 865				SND_SOC_DAIFMT_CBP_CFP : SND_SOC_DAIFMT_CBP_CFC;
 866		else
 867			daifmt |= (codec_frameprovider) ?
 868				SND_SOC_DAIFMT_CBC_CFP : SND_SOC_DAIFMT_CBC_CFC;
 869
 870		/* Override dai_fmt with value from DT */
 871		priv->dai_fmt = daifmt;
 872	}
 873
 874	/* Change direction according to format */
 875	if (priv->dai_fmt & SND_SOC_DAIFMT_CBP_CFP) {
 876		priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_IN;
 877		priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_IN;
 878	}
 879
 880	of_node_put(bitclkprovider);
 881	of_node_put(frameprovider);
 882
 883	if (!fsl_asoc_card_is_ac97(priv) && !codec_dev[0]
 884	    && codec_dai_name[0] != snd_soc_dummy_dlc.dai_name) {
 885		dev_dbg(&pdev->dev, "failed to find codec device\n");
 886		ret = -EPROBE_DEFER;
 887		goto asrc_fail;
 888	}
 889
 890	/* Common settings for corresponding Freescale CPU DAI driver */
 891	if (of_node_name_eq(cpu_np, "ssi")) {
 892		/* Only SSI needs to configure AUDMUX */
 893		ret = fsl_asoc_card_audmux_init(np, priv);
 894		if (ret) {
 895			dev_err(&pdev->dev, "failed to init audmux\n");
 896			goto asrc_fail;
 897		}
 898	} else if (of_node_name_eq(cpu_np, "esai")) {
 899		struct clk *esai_clk = clk_get(&cpu_pdev->dev, "extal");
 900
 901		if (!IS_ERR(esai_clk)) {
 902			priv->cpu_priv.sysclk_freq[TX] = clk_get_rate(esai_clk);
 903			priv->cpu_priv.sysclk_freq[RX] = clk_get_rate(esai_clk);
 904			clk_put(esai_clk);
 905		} else if (PTR_ERR(esai_clk) == -EPROBE_DEFER) {
 906			ret = -EPROBE_DEFER;
 907			goto asrc_fail;
 908		}
 909
 910		priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
 911		priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
 912	} else if (of_node_name_eq(cpu_np, "sai")) {
 913		priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
 914		priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
 915	}
 916
 
 
 
 
 917	/* Initialize sound card */
 
 918	priv->card.dev = &pdev->dev;
 919	priv->card.owner = THIS_MODULE;
 920	ret = snd_soc_of_parse_card_name(&priv->card, "model");
 921	if (ret) {
 922		snprintf(priv->name, sizeof(priv->name), "%s-audio",
 923			 fsl_asoc_card_is_ac97(priv) ? "ac97" : codec_dev_name[0]);
 924		priv->card.name = priv->name;
 925	}
 926	priv->card.dai_link = priv->dai_link;
 
 
 927	priv->card.late_probe = fsl_asoc_card_late_probe;
 
 928	priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
 929	priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
 930
 931	/* Drop the second half of DAPM routes -- ASRC */
 932	if (!asrc_pdev)
 933		priv->card.num_dapm_routes /= 2;
 934
 935	if (of_property_read_bool(np, "audio-routing")) {
 936		ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
 937		if (ret) {
 938			dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
 939			goto asrc_fail;
 940		}
 
 941	}
 942
 943	/* Normal DAI Link */
 944	priv->dai_link[0].cpus->of_node = cpu_np;
 945	for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {
 946		codec_comp->dai_name = codec_dai_name[codec_idx];
 947	}
 948
 949	// Old SPDIF DT binding support
 950	if (codec_dai_name[0] == snd_soc_dummy_dlc.dai_name)
 951		priv->dai_link[0].codecs[0].name = snd_soc_dummy_dlc.name;
 952
 953	if (!fsl_asoc_card_is_ac97(priv)) {
 954		for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {
 955			codec_comp->of_node = codec_np[codec_idx];
 956		}
 957	} else {
 958		u32 idx;
 959
 960		ret = of_property_read_u32(cpu_np, "cell-index", &idx);
 961		if (ret) {
 962			dev_err(&pdev->dev,
 963				"cannot get CPU index property\n");
 964			goto asrc_fail;
 965		}
 966
 967		priv->dai_link[0].codecs[0].name =
 968				devm_kasprintf(&pdev->dev, GFP_KERNEL,
 969					       "ac97-codec.%u",
 970					       (unsigned int)idx);
 971		if (!priv->dai_link[0].codecs[0].name) {
 972			ret = -ENOMEM;
 973			goto asrc_fail;
 974		}
 975	}
 976
 977	priv->dai_link[0].platforms->of_node = cpu_np;
 978	priv->dai_link[0].dai_fmt = priv->dai_fmt;
 979	priv->card.num_links = 1;
 980
 981	if (asrc_pdev) {
 982		/* DPCM DAI Links only if ASRC exists */
 983		priv->dai_link[1].cpus->of_node = asrc_np;
 984		priv->dai_link[1].platforms->of_node = asrc_np;
 985		for_each_link_codecs((&(priv->dai_link[2])), codec_idx, codec_comp) {
 986			codec_comp->dai_name = priv->dai_link[0].codecs[codec_idx].dai_name;
 987			codec_comp->of_node = priv->dai_link[0].codecs[codec_idx].of_node;
 988			codec_comp->name = priv->dai_link[0].codecs[codec_idx].name;
 989		}
 990		priv->dai_link[2].cpus->of_node = cpu_np;
 991		priv->dai_link[2].dai_fmt = priv->dai_fmt;
 992		priv->card.num_links = 3;
 993
 994		ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
 995					   &priv->asrc_rate);
 996		if (ret) {
 997			dev_err(&pdev->dev, "failed to get output rate\n");
 998			ret = -EINVAL;
 999			goto asrc_fail;
1000		}
1001
1002		ret = of_property_read_u32(asrc_np, "fsl,asrc-format", &asrc_fmt);
1003		priv->asrc_format = (__force snd_pcm_format_t)asrc_fmt;
1004		if (ret) {
1005			/* Fallback to old binding; translate to asrc_format */
1006			ret = of_property_read_u32(asrc_np, "fsl,asrc-width",
1007						   &width);
1008			if (ret) {
1009				dev_err(&pdev->dev,
1010					"failed to decide output format\n");
1011				goto asrc_fail;
1012			}
1013
1014			if (width == 24)
1015				priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
1016			else
1017				priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
1018		}
 
 
 
 
 
1019	}
1020
1021	/* Finish card registering */
1022	platform_set_drvdata(pdev, priv);
1023	snd_soc_card_set_drvdata(&priv->card, priv);
1024
1025	ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
1026	if (ret) {
1027		dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
1028		goto asrc_fail;
1029	}
1030
1031	/*
1032	 * Properties "hp-det-gpios" and "mic-det-gpios" are optional, and
1033	 * simple_util_init_jack() uses these properties for creating
1034	 * Headphone Jack and Microphone Jack.
1035	 *
1036	 * The notifier is initialized in snd_soc_card_jack_new(), then
1037	 * snd_soc_jack_notifier_register can be called.
1038	 */
1039	if (of_property_read_bool(np, "hp-det-gpios") ||
1040	    of_property_read_bool(np, "hp-det-gpio") /* deprecated */) {
1041		ret = simple_util_init_jack(&priv->card, &priv->hp_jack,
1042					    1, NULL, "Headphone Jack");
1043		if (ret)
1044			goto asrc_fail;
1045
1046		snd_soc_jack_notifier_register(&priv->hp_jack.jack, &hp_jack_nb);
1047	}
1048
1049	if (of_property_read_bool(np, "mic-det-gpios") ||
1050	    of_property_read_bool(np, "mic-det-gpio") /* deprecated */) {
1051		ret = simple_util_init_jack(&priv->card, &priv->mic_jack,
1052					    0, NULL, "Mic Jack");
1053		if (ret)
1054			goto asrc_fail;
1055
1056		snd_soc_jack_notifier_register(&priv->mic_jack.jack, &mic_jack_nb);
1057	}
1058
1059asrc_fail:
1060	of_node_put(asrc_np);
1061	of_node_put(codec_np[0]);
1062	of_node_put(codec_np[1]);
1063	put_device(&cpu_pdev->dev);
1064fail:
1065	of_node_put(cpu_np);
1066
1067	return ret;
1068}
1069
1070static const struct of_device_id fsl_asoc_card_dt_ids[] = {
1071	{ .compatible = "fsl,imx-audio-ac97", },
1072	{ .compatible = "fsl,imx-audio-cs42888", },
1073	{ .compatible = "fsl,imx-audio-cs427x", },
1074	{ .compatible = "fsl,imx-audio-tlv320aic32x4", },
1075	{ .compatible = "fsl,imx-audio-tlv320aic31xx", },
1076	{ .compatible = "fsl,imx-audio-sgtl5000", },
1077	{ .compatible = "fsl,imx-audio-wm8962", },
1078	{ .compatible = "fsl,imx-audio-wm8960", },
1079	{ .compatible = "fsl,imx-audio-mqs", },
1080	{ .compatible = "fsl,imx-audio-wm8524", },
1081	{ .compatible = "fsl,imx-audio-si476x", },
1082	{ .compatible = "fsl,imx-audio-wm8958", },
1083	{ .compatible = "fsl,imx-audio-nau8822", },
1084	{ .compatible = "fsl,imx-audio-wm8904", },
1085	{ .compatible = "fsl,imx-audio-spdif", },
1086	{}
1087};
1088MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
1089
1090static struct platform_driver fsl_asoc_card_driver = {
1091	.probe = fsl_asoc_card_probe,
1092	.driver = {
1093		.name = DRIVER_NAME,
1094		.pm = &snd_soc_pm_ops,
1095		.of_match_table = fsl_asoc_card_dt_ids,
1096	},
1097};
1098module_platform_driver(fsl_asoc_card_driver);
1099
1100MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
1101MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
1102MODULE_ALIAS("platform:" DRIVER_NAME);
1103MODULE_LICENSE("GPL");
v5.4
  1// 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>
  8
  9#include <linux/clk.h>
 10#include <linux/i2c.h>
 11#include <linux/module.h>
 12#include <linux/of_platform.h>
 13#if IS_ENABLED(CONFIG_SND_AC97_CODEC)
 14#include <sound/ac97_codec.h>
 15#endif
 16#include <sound/pcm_params.h>
 17#include <sound/soc.h>
 
 
 18
 19#include "fsl_esai.h"
 20#include "fsl_sai.h"
 21#include "imx-audmux.h"
 22
 23#include "../codecs/sgtl5000.h"
 24#include "../codecs/wm8962.h"
 25#include "../codecs/wm8960.h"
 
 
 
 
 
 
 26
 27#define CS427x_SYSCLK_MCLK 0
 28
 29#define RX 0
 30#define TX 1
 31
 32/* Default DAI format without Master and Slave flag */
 33#define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
 34
 35/**
 36 * CODEC private data
 37 *
 38 * @mclk_freq: Clock rate of MCLK
 
 39 * @mclk_id: MCLK (or main clock) id for set_sysclk()
 40 * @fll_id: FLL (or secordary clock) id for set_sysclk()
 41 * @pll_id: PLL id for set_pll()
 42 */
 43struct codec_priv {
 
 44	unsigned long mclk_freq;
 
 45	u32 mclk_id;
 46	u32 fll_id;
 47	u32 pll_id;
 48};
 49
 50/**
 51 * CPU private data
 52 *
 53 * @sysclk_freq[2]: SYSCLK rates for set_sysclk()
 54 * @sysclk_dir[2]: SYSCLK directions for set_sysclk()
 55 * @sysclk_id[2]: SYSCLK ids for set_sysclk()
 56 * @slot_width: Slot width of each frame
 
 57 *
 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];
 64	u32 slot_width;
 
 65};
 66
 67/**
 68 * Freescale Generic ASOC card private data
 69 *
 70 * @dai_link[3]: DAI link structure including normal one and DPCM link
 
 71 * @pdev: platform device pointer
 72 * @codec_priv: CODEC private data
 73 * @cpu_priv: CPU private data
 74 * @card: ASoC card structure
 
 75 * @sample_rate: Current sample rate
 76 * @sample_format: Current sample format
 77 * @asrc_rate: ASRC sample rate used by Back-Ends
 78 * @asrc_format: ASRC sample format used by Back-Ends
 79 * @dai_fmt: DAI format between CPU and CODEC
 80 * @name: Card name
 81 */
 82
 83struct fsl_asoc_card_priv {
 84	struct snd_soc_dai_link dai_link[3];
 
 
 85	struct platform_device *pdev;
 86	struct codec_priv codec_priv;
 87	struct cpu_priv cpu_priv;
 88	struct snd_soc_card card;
 
 89	u32 sample_rate;
 90	snd_pcm_format_t sample_format;
 91	u32 asrc_rate;
 92	snd_pcm_format_t asrc_format;
 93	u32 dai_fmt;
 94	char name[32];
 95};
 96
 97/**
 98 * This dapm route map exsits for DPCM link only.
 99 * The other routes shall go through Device Tree.
100 *
101 * Note: keep all ASRC routes in the second half
102 *	 to drop them easily for non-ASRC cases.
103 */
104static const struct snd_soc_dapm_route audio_map[] = {
105	/* 1st half -- Normal DAPM routes */
106	{"Playback",  NULL, "CPU-Playback"},
107	{"CPU-Capture",  NULL, "Capture"},
108	/* 2nd half -- ASRC DAPM routes */
109	{"CPU-Playback",  NULL, "ASRC-Playback"},
110	{"ASRC-Capture",  NULL, "CPU-Capture"},
111};
112
113static const struct snd_soc_dapm_route audio_map_ac97[] = {
114	/* 1st half -- Normal DAPM routes */
115	{"Playback",  NULL, "AC97 Playback"},
116	{"AC97 Capture",  NULL, "Capture"},
 
 
 
 
 
 
 
 
117	/* 2nd half -- ASRC DAPM routes */
118	{"AC97 Playback",  NULL, "ASRC-Playback"},
119	{"ASRC-Capture",  NULL, "AC97 Capture"},
 
 
 
 
 
 
120};
121
122/* Add all possible widgets into here without being redundant */
123static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
124	SND_SOC_DAPM_LINE("Line Out Jack", NULL),
125	SND_SOC_DAPM_LINE("Line In Jack", NULL),
126	SND_SOC_DAPM_HP("Headphone Jack", NULL),
127	SND_SOC_DAPM_SPK("Ext Spk", NULL),
128	SND_SOC_DAPM_MIC("Mic Jack", NULL),
129	SND_SOC_DAPM_MIC("AMIC", NULL),
130	SND_SOC_DAPM_MIC("DMIC", NULL),
131};
132
133static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
134{
135	return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
136}
137
138static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
139				   struct snd_pcm_hw_params *params)
140{
141	struct snd_soc_pcm_runtime *rtd = substream->private_data;
142	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
143	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
 
 
144	struct cpu_priv *cpu_priv = &priv->cpu_priv;
145	struct device *dev = rtd->card->dev;
 
 
146	int ret;
147
148	priv->sample_rate = params_rate(params);
149	priv->sample_format = params_format(params);
 
150
151	/*
152	 * If codec-dai is DAI Master and all configurations are already in the
153	 * set_bias_level(), bypass the remaining settings in hw_params().
154	 * Note: (dai_fmt & CBM_CFM) includes CBM_CFM and CBM_CFS.
155	 */
156	if ((priv->card.set_bias_level &&
157	     priv->dai_fmt & SND_SOC_DAIFMT_CBM_CFM) ||
158	    fsl_asoc_card_is_ac97(priv))
159		return 0;
160
161	/* Specific configurations of DAIs starts from here */
162	ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, cpu_priv->sysclk_id[tx],
163				     cpu_priv->sysclk_freq[tx],
164				     cpu_priv->sysclk_dir[tx]);
165	if (ret && ret != -ENOTSUPP) {
166		dev_err(dev, "failed to set sysclk for cpu dai\n");
167		return ret;
168	}
169
170	if (cpu_priv->slot_width) {
171		ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2,
 
 
 
 
172					       cpu_priv->slot_width);
173		if (ret && ret != -ENOTSUPP) {
174			dev_err(dev, "failed to set TDM slot for cpu dai\n");
175			return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176		}
177	}
178
179	return 0;
180}
181
182static const struct snd_soc_ops fsl_asoc_card_ops = {
183	.hw_params = fsl_asoc_card_hw_params,
 
184};
185
186static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
187			      struct snd_pcm_hw_params *params)
188{
189	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
190	struct snd_interval *rate;
191	struct snd_mask *mask;
192
193	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
194	rate->max = rate->min = priv->asrc_rate;
195
196	mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
197	snd_mask_none(mask);
198	snd_mask_set_format(mask, priv->asrc_format);
199
200	return 0;
201}
202
203SND_SOC_DAILINK_DEFS(hifi,
204	DAILINK_COMP_ARRAY(COMP_EMPTY()),
205	DAILINK_COMP_ARRAY(COMP_EMPTY()),
206	DAILINK_COMP_ARRAY(COMP_EMPTY()));
207
208SND_SOC_DAILINK_DEFS(hifi_fe,
209	DAILINK_COMP_ARRAY(COMP_EMPTY()),
210	DAILINK_COMP_ARRAY(COMP_DUMMY()),
211	DAILINK_COMP_ARRAY(COMP_EMPTY()));
212
213SND_SOC_DAILINK_DEFS(hifi_be,
214	DAILINK_COMP_ARRAY(COMP_EMPTY()),
215	DAILINK_COMP_ARRAY(COMP_EMPTY()),
216	DAILINK_COMP_ARRAY(COMP_DUMMY()));
217
218static struct snd_soc_dai_link fsl_asoc_card_dai[] = {
219	/* Default ASoC DAI Link*/
220	{
221		.name = "HiFi",
222		.stream_name = "HiFi",
223		.ops = &fsl_asoc_card_ops,
224		SND_SOC_DAILINK_REG(hifi),
225	},
226	/* DPCM Link between Front-End and Back-End (Optional) */
227	{
228		.name = "HiFi-ASRC-FE",
229		.stream_name = "HiFi-ASRC-FE",
230		.dpcm_playback = 1,
231		.dpcm_capture = 1,
232		.dynamic = 1,
233		SND_SOC_DAILINK_REG(hifi_fe),
234	},
235	{
236		.name = "HiFi-ASRC-BE",
237		.stream_name = "HiFi-ASRC-BE",
238		.be_hw_params_fixup = be_hw_params_fixup,
239		.ops = &fsl_asoc_card_ops,
240		.dpcm_playback = 1,
241		.dpcm_capture = 1,
242		.no_pcm = 1,
243		SND_SOC_DAILINK_REG(hifi_be),
244	},
245};
246
247static int fsl_asoc_card_set_bias_level(struct snd_soc_card *card,
248					struct snd_soc_dapm_context *dapm,
249					enum snd_soc_bias_level level)
250{
251	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
252	struct snd_soc_pcm_runtime *rtd;
253	struct snd_soc_dai *codec_dai;
254	struct codec_priv *codec_priv = &priv->codec_priv;
255	struct device *dev = card->dev;
256	unsigned int pll_out;
257	int ret;
258
259	rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
260	codec_dai = rtd->codec_dai;
261	if (dapm->dev != codec_dai->dev)
262		return 0;
263
264	switch (level) {
265	case SND_SOC_BIAS_PREPARE:
266		if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
267			break;
268
269		if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
270			pll_out = priv->sample_rate * 384;
271		else
272			pll_out = priv->sample_rate * 256;
273
274		ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id,
275					  codec_priv->mclk_id,
276					  codec_priv->mclk_freq, pll_out);
277		if (ret) {
278			dev_err(dev, "failed to start FLL: %d\n", ret);
279			return ret;
280		}
281
282		ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->fll_id,
283					     pll_out, SND_SOC_CLOCK_IN);
284		if (ret && ret != -ENOTSUPP) {
285			dev_err(dev, "failed to set SYSCLK: %d\n", ret);
286			return ret;
287		}
288		break;
289
290	case SND_SOC_BIAS_STANDBY:
291		if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
292			break;
293
294		ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
295					     codec_priv->mclk_freq,
296					     SND_SOC_CLOCK_IN);
297		if (ret && ret != -ENOTSUPP) {
298			dev_err(dev, "failed to switch away from FLL: %d\n", ret);
299			return ret;
300		}
301
302		ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id, 0, 0, 0);
303		if (ret) {
304			dev_err(dev, "failed to stop FLL: %d\n", ret);
305			return ret;
306		}
307		break;
308
309	default:
310		break;
311	}
312
313	return 0;
314}
315
316static int fsl_asoc_card_audmux_init(struct device_node *np,
317				     struct fsl_asoc_card_priv *priv)
318{
319	struct device *dev = &priv->pdev->dev;
320	u32 int_ptcr = 0, ext_ptcr = 0;
321	int int_port, ext_port;
322	int ret;
323
324	ret = of_property_read_u32(np, "mux-int-port", &int_port);
325	if (ret) {
326		dev_err(dev, "mux-int-port missing or invalid\n");
327		return ret;
328	}
329	ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
330	if (ret) {
331		dev_err(dev, "mux-ext-port missing or invalid\n");
332		return ret;
333	}
334
335	/*
336	 * The port numbering in the hardware manual starts at 1, while
337	 * the AUDMUX API expects it starts at 0.
338	 */
339	int_port--;
340	ext_port--;
341
342	/*
343	 * Use asynchronous mode (6 wires) for all cases except AC97.
344	 * If only 4 wires are needed, just set SSI into
345	 * synchronous mode and enable 4 PADs in IOMUX.
346	 */
347	switch (priv->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
348	case SND_SOC_DAIFMT_CBM_CFM:
349		int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
350			   IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
351			   IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
352			   IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
353			   IMX_AUDMUX_V2_PTCR_RFSDIR |
354			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
355			   IMX_AUDMUX_V2_PTCR_TFSDIR |
356			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
357		break;
358	case SND_SOC_DAIFMT_CBM_CFS:
359		int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
360			   IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
361			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
362			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
363		ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
364			   IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
365			   IMX_AUDMUX_V2_PTCR_RFSDIR |
366			   IMX_AUDMUX_V2_PTCR_TFSDIR;
367		break;
368	case SND_SOC_DAIFMT_CBS_CFM:
369		int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
370			   IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
371			   IMX_AUDMUX_V2_PTCR_RFSDIR |
372			   IMX_AUDMUX_V2_PTCR_TFSDIR;
373		ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
374			   IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
375			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
376			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
377		break;
378	case SND_SOC_DAIFMT_CBS_CFS:
379		ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
380			   IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
381			   IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
382			   IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
383			   IMX_AUDMUX_V2_PTCR_RFSDIR |
384			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
385			   IMX_AUDMUX_V2_PTCR_TFSDIR |
386			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
387		break;
388	default:
389		if (!fsl_asoc_card_is_ac97(priv))
390			return -EINVAL;
391	}
392
393	if (fsl_asoc_card_is_ac97(priv)) {
394		int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
395			   IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
396			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
397		ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
398			   IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
399			   IMX_AUDMUX_V2_PTCR_TFSDIR;
400	}
401
402	/* Asynchronous mode can not be set along with RCLKDIR */
403	if (!fsl_asoc_card_is_ac97(priv)) {
404		unsigned int pdcr =
405				IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
406
407		ret = imx_audmux_v2_configure_port(int_port, 0,
408						   pdcr);
409		if (ret) {
410			dev_err(dev, "audmux internal port setup failed\n");
411			return ret;
412		}
413	}
414
415	ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
416					   IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
417	if (ret) {
418		dev_err(dev, "audmux internal port setup failed\n");
419		return ret;
420	}
421
422	if (!fsl_asoc_card_is_ac97(priv)) {
423		unsigned int pdcr =
424				IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
425
426		ret = imx_audmux_v2_configure_port(ext_port, 0,
427						   pdcr);
428		if (ret) {
429			dev_err(dev, "audmux external port setup failed\n");
430			return ret;
431		}
432	}
433
434	ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
435					   IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
436	if (ret) {
437		dev_err(dev, "audmux external port setup failed\n");
438		return ret;
439	}
440
441	return 0;
442}
443
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
445{
446	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
447	struct snd_soc_pcm_runtime *rtd = list_first_entry(
448			&card->rtd_list, struct snd_soc_pcm_runtime, list);
449	struct snd_soc_dai *codec_dai = rtd->codec_dai;
450	struct codec_priv *codec_priv = &priv->codec_priv;
451	struct device *dev = card->dev;
 
452	int ret;
453
454	if (fsl_asoc_card_is_ac97(priv)) {
455#if IS_ENABLED(CONFIG_SND_AC97_CODEC)
456		struct snd_soc_component *component = rtd->codec_dai->component;
457		struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
458
459		/*
460		 * Use slots 3/4 for S/PDIF so SSI won't try to enable
461		 * other slots and send some samples there
462		 * due to SLOTREQ bits for S/PDIF received from codec
463		 */
464		snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
465				     AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
466#endif
467
468		return 0;
469	}
470
471	ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
472				     codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
473	if (ret && ret != -ENOTSUPP) {
474		dev_err(dev, "failed to set sysclk in %s\n", __func__);
475		return ret;
 
 
 
 
 
 
 
476	}
477
478	return 0;
479}
480
481static int fsl_asoc_card_probe(struct platform_device *pdev)
482{
483	struct device_node *cpu_np, *codec_np, *asrc_np;
 
 
484	struct device_node *np = pdev->dev.of_node;
485	struct platform_device *asrc_pdev = NULL;
 
 
486	struct platform_device *cpu_pdev;
487	struct fsl_asoc_card_priv *priv;
488	struct i2c_client *codec_dev;
489	const char *codec_dai_name;
 
 
 
 
490	u32 width;
491	int ret;
492
493	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
494	if (!priv)
495		return -ENOMEM;
496
 
 
497	cpu_np = of_parse_phandle(np, "audio-cpu", 0);
498	/* Give a chance to old DT binding */
499	if (!cpu_np)
500		cpu_np = of_parse_phandle(np, "ssi-controller", 0);
 
 
501	if (!cpu_np) {
502		dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
503		ret = -EINVAL;
504		goto fail;
505	}
506
507	cpu_pdev = of_find_device_by_node(cpu_np);
508	if (!cpu_pdev) {
509		dev_err(&pdev->dev, "failed to find CPU DAI device\n");
510		ret = -EINVAL;
511		goto fail;
512	}
513
514	codec_np = of_parse_phandle(np, "audio-codec", 0);
515	if (codec_np)
516		codec_dev = of_find_i2c_device_by_node(codec_np);
517	else
518		codec_dev = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
519
520	asrc_np = of_parse_phandle(np, "audio-asrc", 0);
521	if (asrc_np)
522		asrc_pdev = of_find_device_by_node(asrc_np);
523
524	/* Get the MCLK rate only, and leave it controlled by CODEC drivers */
525	if (codec_dev) {
526		struct clk *codec_clk = clk_get(&codec_dev->dev, NULL);
527
528		if (!IS_ERR(codec_clk)) {
529			priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
530			clk_put(codec_clk);
 
 
531		}
532	}
533
534	/* Default sample rate and format, will be updated in hw_params() */
535	priv->sample_rate = 44100;
536	priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
537
538	/* Assign a default DAI format, and allow each card to overwrite it */
539	priv->dai_fmt = DAI_FMT_BASE;
540
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
541	/* Diversify the card configurations */
542	if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
543		codec_dai_name = "cs42888";
544		priv->card.set_bias_level = NULL;
545		priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
546		priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
547		priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
548		priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
549		priv->cpu_priv.slot_width = 32;
550		priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
551	} else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {
552		codec_dai_name = "cs4271-hifi";
553		priv->codec_priv.mclk_id = CS427x_SYSCLK_MCLK;
554		priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
555	} else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
556		codec_dai_name = "sgtl5000";
557		priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
558		priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
 
 
 
 
 
 
 
 
 
 
 
 
559	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
560		codec_dai_name = "wm8962";
561		priv->card.set_bias_level = fsl_asoc_card_set_bias_level;
562		priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
563		priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
564		priv->codec_priv.pll_id = WM8962_FLL;
565		priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
566	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
567		codec_dai_name = "wm8960-hifi";
568		priv->card.set_bias_level = fsl_asoc_card_set_bias_level;
569		priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO;
570		priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO;
571		priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
572	} else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
573		codec_dai_name = "ac97-hifi";
574		priv->card.set_bias_level = NULL;
575		priv->dai_fmt = SND_SOC_DAIFMT_AC97;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
576	} else {
577		dev_err(&pdev->dev, "unknown Device Tree compatible\n");
578		ret = -EINVAL;
579		goto asrc_fail;
580	}
581
582	if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) {
583		dev_err(&pdev->dev, "failed to find codec device\n");
584		ret = -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
585		goto asrc_fail;
586	}
587
588	/* Common settings for corresponding Freescale CPU DAI driver */
589	if (of_node_name_eq(cpu_np, "ssi")) {
590		/* Only SSI needs to configure AUDMUX */
591		ret = fsl_asoc_card_audmux_init(np, priv);
592		if (ret) {
593			dev_err(&pdev->dev, "failed to init audmux\n");
594			goto asrc_fail;
595		}
596	} else if (of_node_name_eq(cpu_np, "esai")) {
 
 
 
 
 
 
 
 
 
 
 
597		priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
598		priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
599	} else if (of_node_name_eq(cpu_np, "sai")) {
600		priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
601		priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
602	}
603
604	snprintf(priv->name, sizeof(priv->name), "%s-audio",
605		 fsl_asoc_card_is_ac97(priv) ? "ac97" :
606		 codec_dev->name);
607
608	/* Initialize sound card */
609	priv->pdev = pdev;
610	priv->card.dev = &pdev->dev;
611	priv->card.name = priv->name;
 
 
 
 
 
 
612	priv->card.dai_link = priv->dai_link;
613	priv->card.dapm_routes = fsl_asoc_card_is_ac97(priv) ?
614				 audio_map_ac97 : audio_map;
615	priv->card.late_probe = fsl_asoc_card_late_probe;
616	priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
617	priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
618	priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
619
620	/* Drop the second half of DAPM routes -- ASRC */
621	if (!asrc_pdev)
622		priv->card.num_dapm_routes /= 2;
623
624	memcpy(priv->dai_link, fsl_asoc_card_dai,
625	       sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
626
627	ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
628	if (ret) {
629		dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
630		goto asrc_fail;
631	}
632
633	/* Normal DAI Link */
634	priv->dai_link[0].cpus->of_node = cpu_np;
635	priv->dai_link[0].codecs->dai_name = codec_dai_name;
 
 
 
 
 
 
636
637	if (!fsl_asoc_card_is_ac97(priv))
638		priv->dai_link[0].codecs->of_node = codec_np;
639	else {
 
 
640		u32 idx;
641
642		ret = of_property_read_u32(cpu_np, "cell-index", &idx);
643		if (ret) {
644			dev_err(&pdev->dev,
645				"cannot get CPU index property\n");
646			goto asrc_fail;
647		}
648
649		priv->dai_link[0].codecs->name =
650				devm_kasprintf(&pdev->dev, GFP_KERNEL,
651					       "ac97-codec.%u",
652					       (unsigned int)idx);
653		if (!priv->dai_link[0].codecs->name) {
654			ret = -ENOMEM;
655			goto asrc_fail;
656		}
657	}
658
659	priv->dai_link[0].platforms->of_node = cpu_np;
660	priv->dai_link[0].dai_fmt = priv->dai_fmt;
661	priv->card.num_links = 1;
662
663	if (asrc_pdev) {
664		/* DPCM DAI Links only if ASRC exsits */
665		priv->dai_link[1].cpus->of_node = asrc_np;
666		priv->dai_link[1].platforms->of_node = asrc_np;
667		priv->dai_link[2].codecs->dai_name = codec_dai_name;
668		priv->dai_link[2].codecs->of_node = codec_np;
669		priv->dai_link[2].codecs->name =
670				priv->dai_link[0].codecs->name;
 
671		priv->dai_link[2].cpus->of_node = cpu_np;
672		priv->dai_link[2].dai_fmt = priv->dai_fmt;
673		priv->card.num_links = 3;
674
675		ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
676					   &priv->asrc_rate);
677		if (ret) {
678			dev_err(&pdev->dev, "failed to get output rate\n");
679			ret = -EINVAL;
680			goto asrc_fail;
681		}
682
683		ret = of_property_read_u32(asrc_np, "fsl,asrc-width", &width);
 
684		if (ret) {
685			dev_err(&pdev->dev, "failed to get output rate\n");
686			ret = -EINVAL;
687			goto asrc_fail;
 
 
 
 
 
 
 
 
 
 
688		}
689
690		if (width == 24)
691			priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
692		else
693			priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
694	}
695
696	/* Finish card registering */
697	platform_set_drvdata(pdev, priv);
698	snd_soc_card_set_drvdata(&priv->card, priv);
699
700	ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
701	if (ret && ret != -EPROBE_DEFER)
702		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
703
704asrc_fail:
705	of_node_put(asrc_np);
706	of_node_put(codec_np);
 
707	put_device(&cpu_pdev->dev);
708fail:
709	of_node_put(cpu_np);
710
711	return ret;
712}
713
714static const struct of_device_id fsl_asoc_card_dt_ids[] = {
715	{ .compatible = "fsl,imx-audio-ac97", },
716	{ .compatible = "fsl,imx-audio-cs42888", },
717	{ .compatible = "fsl,imx-audio-cs427x", },
 
 
718	{ .compatible = "fsl,imx-audio-sgtl5000", },
719	{ .compatible = "fsl,imx-audio-wm8962", },
720	{ .compatible = "fsl,imx-audio-wm8960", },
 
 
 
 
 
 
 
721	{}
722};
723MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
724
725static struct platform_driver fsl_asoc_card_driver = {
726	.probe = fsl_asoc_card_probe,
727	.driver = {
728		.name = "fsl-asoc-card",
729		.pm = &snd_soc_pm_ops,
730		.of_match_table = fsl_asoc_card_dt_ids,
731	},
732};
733module_platform_driver(fsl_asoc_card_driver);
734
735MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
736MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
737MODULE_ALIAS("platform:fsl-asoc-card");
738MODULE_LICENSE("GPL");