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