1106 lines · c
1// SPDX-License-Identifier: GPL-2.02//3// Freescale Generic ASoC Sound Card driver with ASRC4//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#endif16#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 036 37#define RX 038#define TX 139 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 data45 * @mclk: Main clock of the CODEC46 * @mclk_freq: Clock rate of MCLK47 * @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 data63 * @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 frame67 * @slot_num: Number of slots of each frame68 *69 * Note: [1] for tx and [0] for rx70 */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 data81 * @dai_link: DAI link structure including normal one and DPCM link82 * @hp_jack: Headphone Jack structure83 * @mic_jack: Microphone Jack structure84 * @pdev: platform device pointer85 * @codec_priv: CODEC private data86 * @cpu_priv: CPU private data87 * @card: ASoC card structure88 * @streams: Mask of current active streams89 * @sample_rate: Current sample rate90 * @sample_format: Current sample format91 * @asrc_rate: ASRC sample rate used by Back-Ends92 * @asrc_format: ASRC sample format used by Back-Ends93 * @dai_fmt: DAI format between CPU and CODEC94 * @name: Card name95 */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 half119 * 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 else220 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 .dpcm_playback = 1,321 .dpcm_capture = 1,322 .dynamic = 1,323 },324 {325 .name = "HiFi-ASRC-BE",326 .stream_name = "HiFi-ASRC-BE",327 .be_hw_params_fixup = be_hw_params_fixup,328 .ops = &fsl_asoc_card_ops,329 .dpcm_playback = 1,330 .dpcm_capture = 1,331 .no_pcm = 1,332 },333};334 335static int fsl_asoc_card_audmux_init(struct device_node *np,336 struct fsl_asoc_card_priv *priv)337{338 struct device *dev = &priv->pdev->dev;339 u32 int_ptcr = 0, ext_ptcr = 0;340 int int_port, ext_port;341 int ret;342 343 ret = of_property_read_u32(np, "mux-int-port", &int_port);344 if (ret) {345 dev_err(dev, "mux-int-port missing or invalid\n");346 return ret;347 }348 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);349 if (ret) {350 dev_err(dev, "mux-ext-port missing or invalid\n");351 return ret;352 }353 354 /*355 * The port numbering in the hardware manual starts at 1, while356 * the AUDMUX API expects it starts at 0.357 */358 int_port--;359 ext_port--;360 361 /*362 * Use asynchronous mode (6 wires) for all cases except AC97.363 * If only 4 wires are needed, just set SSI into364 * synchronous mode and enable 4 PADs in IOMUX.365 */366 switch (priv->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {367 case SND_SOC_DAIFMT_CBP_CFP:368 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |369 IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |370 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |371 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |372 IMX_AUDMUX_V2_PTCR_RFSDIR |373 IMX_AUDMUX_V2_PTCR_RCLKDIR |374 IMX_AUDMUX_V2_PTCR_TFSDIR |375 IMX_AUDMUX_V2_PTCR_TCLKDIR;376 break;377 case SND_SOC_DAIFMT_CBP_CFC:378 int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |379 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |380 IMX_AUDMUX_V2_PTCR_RCLKDIR |381 IMX_AUDMUX_V2_PTCR_TCLKDIR;382 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |383 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |384 IMX_AUDMUX_V2_PTCR_RFSDIR |385 IMX_AUDMUX_V2_PTCR_TFSDIR;386 break;387 case SND_SOC_DAIFMT_CBC_CFP:388 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |389 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |390 IMX_AUDMUX_V2_PTCR_RFSDIR |391 IMX_AUDMUX_V2_PTCR_TFSDIR;392 ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |393 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |394 IMX_AUDMUX_V2_PTCR_RCLKDIR |395 IMX_AUDMUX_V2_PTCR_TCLKDIR;396 break;397 case SND_SOC_DAIFMT_CBC_CFC:398 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |399 IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |400 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |401 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |402 IMX_AUDMUX_V2_PTCR_RFSDIR |403 IMX_AUDMUX_V2_PTCR_RCLKDIR |404 IMX_AUDMUX_V2_PTCR_TFSDIR |405 IMX_AUDMUX_V2_PTCR_TCLKDIR;406 break;407 default:408 if (!fsl_asoc_card_is_ac97(priv))409 return -EINVAL;410 }411 412 if (fsl_asoc_card_is_ac97(priv)) {413 int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |414 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |415 IMX_AUDMUX_V2_PTCR_TCLKDIR;416 ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |417 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |418 IMX_AUDMUX_V2_PTCR_TFSDIR;419 }420 421 /* Asynchronous mode can not be set along with RCLKDIR */422 if (!fsl_asoc_card_is_ac97(priv)) {423 unsigned int pdcr =424 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);425 426 ret = imx_audmux_v2_configure_port(int_port, 0,427 pdcr);428 if (ret) {429 dev_err(dev, "audmux internal port setup failed\n");430 return ret;431 }432 }433 434 ret = imx_audmux_v2_configure_port(int_port, int_ptcr,435 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));436 if (ret) {437 dev_err(dev, "audmux internal port setup failed\n");438 return ret;439 }440 441 if (!fsl_asoc_card_is_ac97(priv)) {442 unsigned int pdcr =443 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);444 445 ret = imx_audmux_v2_configure_port(ext_port, 0,446 pdcr);447 if (ret) {448 dev_err(dev, "audmux external port setup failed\n");449 return ret;450 }451 }452 453 ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,454 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));455 if (ret) {456 dev_err(dev, "audmux external port setup failed\n");457 return ret;458 }459 460 return 0;461}462 463static int fsl_asoc_card_spdif_init(struct device_node *codec_np[],464 struct device_node *cpu_np,465 const char *codec_dai_name[],466 struct fsl_asoc_card_priv *priv)467{468 struct device *dev = &priv->pdev->dev;469 struct device_node *np = dev->of_node;470 471 if (!of_node_name_eq(cpu_np, "spdif")) {472 dev_err(dev, "CPU phandle invalid, should be an SPDIF device\n");473 return -EINVAL;474 }475 476 priv->dai_link[0].playback_only = true;477 priv->dai_link[0].capture_only = true;478 479 for (int i = 0; i < 2; i++) {480 if (!codec_np[i])481 break;482 483 if (of_device_is_compatible(codec_np[i], "linux,spdif-dit")) {484 priv->dai_link[0].capture_only = false;485 codec_dai_name[i] = "dit-hifi";486 } else if (of_device_is_compatible(codec_np[i], "linux,spdif-dir")) {487 priv->dai_link[0].playback_only = false;488 codec_dai_name[i] = "dir-hifi";489 }490 }491 492 // Old SPDIF DT binding493 if (!codec_np[0]) {494 codec_dai_name[0] = snd_soc_dummy_dlc.dai_name;495 if (of_property_read_bool(np, "spdif-out"))496 priv->dai_link[0].capture_only = false;497 if (of_property_read_bool(np, "spdif-in"))498 priv->dai_link[0].playback_only = false;499 }500 501 if (priv->dai_link[0].playback_only && priv->dai_link[0].capture_only) {502 dev_err(dev, "no enabled S/PDIF DAI link\n");503 return -EINVAL;504 }505 506 if (priv->dai_link[0].playback_only) {507 priv->dai_link[1].dpcm_capture = false;508 priv->dai_link[2].dpcm_capture = false;509 priv->card.dapm_routes = audio_map_tx;510 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);511 } else if (priv->dai_link[0].capture_only) {512 priv->dai_link[1].dpcm_playback = false;513 priv->dai_link[2].dpcm_playback = false;514 priv->card.dapm_routes = audio_map_rx;515 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);516 }517 518 // No DAPM routes with old bindings and dummy codec519 if (!codec_np[0]) {520 priv->card.dapm_routes = NULL;521 priv->card.num_dapm_routes = 0;522 }523 524 if (codec_np[0] && codec_np[1]) {525 priv->dai_link[0].num_codecs = 2;526 priv->dai_link[2].num_codecs = 2;527 }528 529 return 0;530}531 532static int hp_jack_event(struct notifier_block *nb, unsigned long event,533 void *data)534{535 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;536 struct snd_soc_dapm_context *dapm = &jack->card->dapm;537 538 if (event & SND_JACK_HEADPHONE)539 /* Disable speaker if headphone is plugged in */540 return snd_soc_dapm_disable_pin(dapm, "Ext Spk");541 else542 return snd_soc_dapm_enable_pin(dapm, "Ext Spk");543}544 545static struct notifier_block hp_jack_nb = {546 .notifier_call = hp_jack_event,547};548 549static int mic_jack_event(struct notifier_block *nb, unsigned long event,550 void *data)551{552 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;553 struct snd_soc_dapm_context *dapm = &jack->card->dapm;554 555 if (event & SND_JACK_MICROPHONE)556 /* Disable dmic if microphone is plugged in */557 return snd_soc_dapm_disable_pin(dapm, "DMIC");558 else559 return snd_soc_dapm_enable_pin(dapm, "DMIC");560}561 562static struct notifier_block mic_jack_nb = {563 .notifier_call = mic_jack_event,564};565 566static int fsl_asoc_card_late_probe(struct snd_soc_card *card)567{568 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);569 struct snd_soc_pcm_runtime *rtd = list_first_entry(570 &card->rtd_list, struct snd_soc_pcm_runtime, list);571 struct snd_soc_dai *codec_dai;572 struct codec_priv *codec_priv;573 struct device *dev = card->dev;574 int codec_idx;575 int ret;576 577 if (fsl_asoc_card_is_ac97(priv)) {578#if IS_ENABLED(CONFIG_SND_AC97_CODEC)579 struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;580 struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);581 582 /*583 * Use slots 3/4 for S/PDIF so SSI won't try to enable584 * other slots and send some samples there585 * due to SLOTREQ bits for S/PDIF received from codec586 */587 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,588 AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);589#endif590 591 return 0;592 }593 594 for_each_rtd_codec_dais(rtd, codec_idx, codec_dai) {595 codec_priv = &priv->codec_priv[codec_idx];596 597 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,598 codec_priv->mclk_freq, SND_SOC_CLOCK_IN);599 if (ret && ret != -ENOTSUPP) {600 dev_err(dev, "failed to set sysclk in %s\n", __func__);601 return ret;602 }603 604 if (!IS_ERR_OR_NULL(codec_priv->mclk))605 clk_prepare_enable(codec_priv->mclk);606 }607 608 return 0;609}610 611static int fsl_asoc_card_probe(struct platform_device *pdev)612{613 struct device_node *cpu_np, *asrc_np;614 struct snd_soc_dai_link_component *codec_comp;615 struct device_node *codec_np[2];616 struct device_node *np = pdev->dev.of_node;617 struct platform_device *asrc_pdev = NULL;618 struct device_node *bitclkprovider = NULL;619 struct device_node *frameprovider = NULL;620 struct platform_device *cpu_pdev;621 struct fsl_asoc_card_priv *priv;622 struct device *codec_dev[2] = { NULL, NULL };623 struct snd_soc_dai_link_component *dlc;624 const char *codec_dai_name[2];625 const char *codec_dev_name[2];626 u32 asrc_fmt = 0;627 int codec_idx;628 u32 width;629 int ret;630 631 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);632 if (!priv)633 return -ENOMEM;634 635 priv->pdev = pdev;636 637 cpu_np = of_parse_phandle(np, "audio-cpu", 0);638 /* Give a chance to old DT bindings */639 if (!cpu_np)640 cpu_np = of_parse_phandle(np, "ssi-controller", 0);641 if (!cpu_np)642 cpu_np = of_parse_phandle(np, "spdif-controller", 0);643 if (!cpu_np) {644 dev_err(&pdev->dev, "CPU phandle missing or invalid\n");645 ret = -EINVAL;646 goto fail;647 }648 649 cpu_pdev = of_find_device_by_node(cpu_np);650 if (!cpu_pdev) {651 dev_err(&pdev->dev, "failed to find CPU DAI device\n");652 ret = -EINVAL;653 goto fail;654 }655 656 codec_np[0] = of_parse_phandle(np, "audio-codec", 0);657 codec_np[1] = of_parse_phandle(np, "audio-codec", 1);658 659 for (codec_idx = 0; codec_idx < 2; codec_idx++) {660 if (codec_np[codec_idx]) {661 struct platform_device *codec_pdev;662 struct i2c_client *codec_i2c;663 664 codec_i2c = of_find_i2c_device_by_node(codec_np[codec_idx]);665 if (codec_i2c) {666 codec_dev[codec_idx] = &codec_i2c->dev;667 codec_dev_name[codec_idx] = codec_i2c->name;668 }669 if (!codec_dev[codec_idx]) {670 codec_pdev = of_find_device_by_node(codec_np[codec_idx]);671 if (codec_pdev) {672 codec_dev[codec_idx] = &codec_pdev->dev;673 codec_dev_name[codec_idx] = codec_pdev->name;674 }675 }676 }677 }678 679 asrc_np = of_parse_phandle(np, "audio-asrc", 0);680 if (asrc_np)681 asrc_pdev = of_find_device_by_node(asrc_np);682 683 /* Get the MCLK rate only, and leave it controlled by CODEC drivers */684 for (codec_idx = 0; codec_idx < 2; codec_idx++) {685 if (codec_dev[codec_idx]) {686 struct clk *codec_clk = clk_get(codec_dev[codec_idx], NULL);687 688 if (!IS_ERR(codec_clk)) {689 priv->codec_priv[codec_idx].mclk_freq = clk_get_rate(codec_clk);690 clk_put(codec_clk);691 }692 }693 }694 695 /* Default sample rate and format, will be updated in hw_params() */696 priv->sample_rate = 44100;697 priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;698 699 /* Assign a default DAI format, and allow each card to overwrite it */700 priv->dai_fmt = DAI_FMT_BASE;701 702 memcpy(priv->dai_link, fsl_asoc_card_dai,703 sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));704 /*705 * "Default ASoC DAI Link": 1 cpus, 2 codecs, 1 platforms706 * "DPCM Link Front-End": 1 cpus, 1 codecs (dummy), 1 platforms707 * "DPCM Link Back-End": 1 cpus, 2 codecs708 * totally 10 components709 */710 dlc = devm_kcalloc(&pdev->dev, 10, sizeof(*dlc), GFP_KERNEL);711 if (!dlc) {712 ret = -ENOMEM;713 goto asrc_fail;714 }715 716 priv->dai_link[0].cpus = &dlc[0];717 priv->dai_link[0].num_cpus = 1;718 priv->dai_link[0].codecs = &dlc[1];719 priv->dai_link[0].num_codecs = 1;720 priv->dai_link[0].platforms = &dlc[3];721 priv->dai_link[0].num_platforms = 1;722 723 priv->dai_link[1].cpus = &dlc[4];724 priv->dai_link[1].num_cpus = 1;725 priv->dai_link[1].codecs = &dlc[5];726 priv->dai_link[1].num_codecs = 0; /* dummy */727 priv->dai_link[1].platforms = &dlc[6];728 priv->dai_link[1].num_platforms = 1;729 730 priv->dai_link[2].cpus = &dlc[7];731 priv->dai_link[2].num_cpus = 1;732 priv->dai_link[2].codecs = &dlc[8];733 priv->dai_link[2].num_codecs = 1;734 735 priv->card.dapm_routes = audio_map;736 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);737 priv->card.driver_name = DRIVER_NAME;738 739 for (codec_idx = 0; codec_idx < 2; codec_idx++) {740 priv->codec_priv[codec_idx].fll_id = -1;741 priv->codec_priv[codec_idx].pll_id = -1;742 }743 744 /* Diversify the card configurations */745 if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {746 codec_dai_name[0] = "cs42888";747 priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv[0].mclk_freq;748 priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv[0].mclk_freq;749 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;750 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;751 priv->cpu_priv.slot_width = 32;752 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;753 } else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {754 codec_dai_name[0] = "cs4271-hifi";755 priv->codec_priv[0].mclk_id = CS427x_SYSCLK_MCLK;756 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;757 } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {758 codec_dai_name[0] = "sgtl5000";759 priv->codec_priv[0].mclk_id = SGTL5000_SYSCLK;760 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;761 } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic32x4")) {762 codec_dai_name[0] = "tlv320aic32x4-hifi";763 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;764 } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic31xx")) {765 codec_dai_name[0] = "tlv320dac31xx-hifi";766 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;767 priv->dai_link[1].dpcm_capture = 0;768 priv->dai_link[2].dpcm_capture = 0;769 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;770 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;771 priv->card.dapm_routes = audio_map_tx;772 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);773 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {774 codec_dai_name[0] = "wm8962";775 priv->codec_priv[0].mclk_id = WM8962_SYSCLK_MCLK;776 priv->codec_priv[0].fll_id = WM8962_SYSCLK_FLL;777 priv->codec_priv[0].pll_id = WM8962_FLL;778 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;779 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {780 codec_dai_name[0] = "wm8960-hifi";781 priv->codec_priv[0].fll_id = WM8960_SYSCLK_AUTO;782 priv->codec_priv[0].pll_id = WM8960_SYSCLK_AUTO;783 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;784 } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {785 codec_dai_name[0] = "ac97-hifi";786 priv->dai_fmt = SND_SOC_DAIFMT_AC97;787 priv->card.dapm_routes = audio_map_ac97;788 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_ac97);789 } else if (of_device_is_compatible(np, "fsl,imx-audio-mqs")) {790 codec_dai_name[0] = "fsl-mqs-dai";791 priv->dai_fmt = SND_SOC_DAIFMT_LEFT_J |792 SND_SOC_DAIFMT_CBC_CFC |793 SND_SOC_DAIFMT_NB_NF;794 priv->dai_link[1].dpcm_capture = 0;795 priv->dai_link[2].dpcm_capture = 0;796 priv->card.dapm_routes = audio_map_tx;797 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);798 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8524")) {799 codec_dai_name[0] = "wm8524-hifi";800 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;801 priv->dai_link[1].dpcm_capture = 0;802 priv->dai_link[2].dpcm_capture = 0;803 priv->cpu_priv.slot_width = 32;804 priv->card.dapm_routes = audio_map_tx;805 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);806 } else if (of_device_is_compatible(np, "fsl,imx-audio-si476x")) {807 codec_dai_name[0] = "si476x-codec";808 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;809 priv->card.dapm_routes = audio_map_rx;810 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);811 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8958")) {812 codec_dai_name[0] = "wm8994-aif1";813 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;814 priv->codec_priv[0].mclk_id = WM8994_FLL_SRC_MCLK1;815 priv->codec_priv[0].fll_id = WM8994_SYSCLK_FLL1;816 priv->codec_priv[0].pll_id = WM8994_FLL1;817 priv->codec_priv[0].free_freq = priv->codec_priv[0].mclk_freq;818 priv->card.dapm_routes = NULL;819 priv->card.num_dapm_routes = 0;820 } else if (of_device_is_compatible(np, "fsl,imx-audio-nau8822")) {821 codec_dai_name[0] = "nau8822-hifi";822 priv->codec_priv[0].mclk_id = NAU8822_CLK_MCLK;823 priv->codec_priv[0].fll_id = NAU8822_CLK_PLL;824 priv->codec_priv[0].pll_id = NAU8822_CLK_PLL;825 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;826 if (codec_dev[0])827 priv->codec_priv[0].mclk = devm_clk_get(codec_dev[0], NULL);828 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8904")) {829 codec_dai_name[0] = "wm8904-hifi";830 priv->codec_priv[0].mclk_id = WM8904_FLL_MCLK;831 priv->codec_priv[0].fll_id = WM8904_CLK_FLL;832 priv->codec_priv[0].pll_id = WM8904_FLL_MCLK;833 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;834 } else if (of_device_is_compatible(np, "fsl,imx-audio-spdif")) {835 ret = fsl_asoc_card_spdif_init(codec_np, cpu_np, codec_dai_name, priv);836 if (ret)837 goto asrc_fail;838 } else {839 dev_err(&pdev->dev, "unknown Device Tree compatible\n");840 ret = -EINVAL;841 goto asrc_fail;842 }843 844 /*845 * Allow setting mclk-id from the device-tree node. Otherwise, the846 * default value for each card configuration is used.847 */848 for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {849 of_property_read_u32_index(np, "mclk-id", codec_idx,850 &priv->codec_priv[codec_idx].mclk_id);851 }852 853 /* Format info from DT is optional. */854 snd_soc_daifmt_parse_clock_provider_as_phandle(np, NULL, &bitclkprovider, &frameprovider);855 if (bitclkprovider || frameprovider) {856 unsigned int daifmt = snd_soc_daifmt_parse_format(np, NULL);857 bool codec_bitclkprovider = false;858 bool codec_frameprovider = false;859 860 for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {861 if (bitclkprovider && codec_np[codec_idx] == bitclkprovider)862 codec_bitclkprovider = true;863 if (frameprovider && codec_np[codec_idx] == frameprovider)864 codec_frameprovider = true;865 }866 867 if (codec_bitclkprovider)868 daifmt |= (codec_frameprovider) ?869 SND_SOC_DAIFMT_CBP_CFP : SND_SOC_DAIFMT_CBP_CFC;870 else871 daifmt |= (codec_frameprovider) ?872 SND_SOC_DAIFMT_CBC_CFP : SND_SOC_DAIFMT_CBC_CFC;873 874 /* Override dai_fmt with value from DT */875 priv->dai_fmt = daifmt;876 }877 878 /* Change direction according to format */879 if (priv->dai_fmt & SND_SOC_DAIFMT_CBP_CFP) {880 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_IN;881 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_IN;882 }883 884 of_node_put(bitclkprovider);885 of_node_put(frameprovider);886 887 if (!fsl_asoc_card_is_ac97(priv) && !codec_dev[0]888 && codec_dai_name[0] != snd_soc_dummy_dlc.dai_name) {889 dev_dbg(&pdev->dev, "failed to find codec device\n");890 ret = -EPROBE_DEFER;891 goto asrc_fail;892 }893 894 /* Common settings for corresponding Freescale CPU DAI driver */895 if (of_node_name_eq(cpu_np, "ssi")) {896 /* Only SSI needs to configure AUDMUX */897 ret = fsl_asoc_card_audmux_init(np, priv);898 if (ret) {899 dev_err(&pdev->dev, "failed to init audmux\n");900 goto asrc_fail;901 }902 } else if (of_node_name_eq(cpu_np, "esai")) {903 struct clk *esai_clk = clk_get(&cpu_pdev->dev, "extal");904 905 if (!IS_ERR(esai_clk)) {906 priv->cpu_priv.sysclk_freq[TX] = clk_get_rate(esai_clk);907 priv->cpu_priv.sysclk_freq[RX] = clk_get_rate(esai_clk);908 clk_put(esai_clk);909 } else if (PTR_ERR(esai_clk) == -EPROBE_DEFER) {910 ret = -EPROBE_DEFER;911 goto asrc_fail;912 }913 914 priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;915 priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;916 } else if (of_node_name_eq(cpu_np, "sai")) {917 priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;918 priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;919 }920 921 /* Initialize sound card */922 priv->card.dev = &pdev->dev;923 priv->card.owner = THIS_MODULE;924 ret = snd_soc_of_parse_card_name(&priv->card, "model");925 if (ret) {926 snprintf(priv->name, sizeof(priv->name), "%s-audio",927 fsl_asoc_card_is_ac97(priv) ? "ac97" : codec_dev_name[0]);928 priv->card.name = priv->name;929 }930 priv->card.dai_link = priv->dai_link;931 priv->card.late_probe = fsl_asoc_card_late_probe;932 priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;933 priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);934 935 /* Drop the second half of DAPM routes -- ASRC */936 if (!asrc_pdev)937 priv->card.num_dapm_routes /= 2;938 939 if (of_property_read_bool(np, "audio-routing")) {940 ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");941 if (ret) {942 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);943 goto asrc_fail;944 }945 }946 947 /* Normal DAI Link */948 priv->dai_link[0].cpus->of_node = cpu_np;949 for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {950 codec_comp->dai_name = codec_dai_name[codec_idx];951 }952 953 // Old SPDIF DT binding support954 if (codec_dai_name[0] == snd_soc_dummy_dlc.dai_name)955 priv->dai_link[0].codecs[0].name = snd_soc_dummy_dlc.name;956 957 if (!fsl_asoc_card_is_ac97(priv)) {958 for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {959 codec_comp->of_node = codec_np[codec_idx];960 }961 } else {962 u32 idx;963 964 ret = of_property_read_u32(cpu_np, "cell-index", &idx);965 if (ret) {966 dev_err(&pdev->dev,967 "cannot get CPU index property\n");968 goto asrc_fail;969 }970 971 priv->dai_link[0].codecs[0].name =972 devm_kasprintf(&pdev->dev, GFP_KERNEL,973 "ac97-codec.%u",974 (unsigned int)idx);975 if (!priv->dai_link[0].codecs[0].name) {976 ret = -ENOMEM;977 goto asrc_fail;978 }979 }980 981 priv->dai_link[0].platforms->of_node = cpu_np;982 priv->dai_link[0].dai_fmt = priv->dai_fmt;983 priv->card.num_links = 1;984 985 if (asrc_pdev) {986 /* DPCM DAI Links only if ASRC exists */987 priv->dai_link[1].cpus->of_node = asrc_np;988 priv->dai_link[1].platforms->of_node = asrc_np;989 for_each_link_codecs((&(priv->dai_link[2])), codec_idx, codec_comp) {990 codec_comp->dai_name = priv->dai_link[0].codecs[codec_idx].dai_name;991 codec_comp->of_node = priv->dai_link[0].codecs[codec_idx].of_node;992 codec_comp->name = priv->dai_link[0].codecs[codec_idx].name;993 }994 priv->dai_link[2].cpus->of_node = cpu_np;995 priv->dai_link[2].dai_fmt = priv->dai_fmt;996 priv->card.num_links = 3;997 998 ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",999 &priv->asrc_rate);1000 if (ret) {1001 dev_err(&pdev->dev, "failed to get output rate\n");1002 ret = -EINVAL;1003 goto asrc_fail;1004 }1005 1006 ret = of_property_read_u32(asrc_np, "fsl,asrc-format", &asrc_fmt);1007 priv->asrc_format = (__force snd_pcm_format_t)asrc_fmt;1008 if (ret) {1009 /* Fallback to old binding; translate to asrc_format */1010 ret = of_property_read_u32(asrc_np, "fsl,asrc-width",1011 &width);1012 if (ret) {1013 dev_err(&pdev->dev,1014 "failed to decide output format\n");1015 goto asrc_fail;1016 }1017 1018 if (width == 24)1019 priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;1020 else1021 priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;1022 }1023 }1024 1025 /* Finish card registering */1026 platform_set_drvdata(pdev, priv);1027 snd_soc_card_set_drvdata(&priv->card, priv);1028 1029 ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);1030 if (ret) {1031 dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");1032 goto asrc_fail;1033 }1034 1035 /*1036 * Properties "hp-det-gpio" and "mic-det-gpio" are optional, and1037 * simple_util_init_jack() uses these properties for creating1038 * Headphone Jack and Microphone Jack.1039 *1040 * The notifier is initialized in snd_soc_card_jack_new(), then1041 * snd_soc_jack_notifier_register can be called.1042 */1043 if (of_property_read_bool(np, "hp-det-gpio")) {1044 ret = simple_util_init_jack(&priv->card, &priv->hp_jack,1045 1, NULL, "Headphone Jack");1046 if (ret)1047 goto asrc_fail;1048 1049 snd_soc_jack_notifier_register(&priv->hp_jack.jack, &hp_jack_nb);1050 }1051 1052 if (of_property_read_bool(np, "mic-det-gpio")) {1053 ret = simple_util_init_jack(&priv->card, &priv->mic_jack,1054 0, NULL, "Mic Jack");1055 if (ret)1056 goto asrc_fail;1057 1058 snd_soc_jack_notifier_register(&priv->mic_jack.jack, &mic_jack_nb);1059 }1060 1061asrc_fail:1062 of_node_put(asrc_np);1063 of_node_put(codec_np[0]);1064 of_node_put(codec_np[1]);1065 put_device(&cpu_pdev->dev);1066fail:1067 of_node_put(cpu_np);1068 1069 return ret;1070}1071 1072static const struct of_device_id fsl_asoc_card_dt_ids[] = {1073 { .compatible = "fsl,imx-audio-ac97", },1074 { .compatible = "fsl,imx-audio-cs42888", },1075 { .compatible = "fsl,imx-audio-cs427x", },1076 { .compatible = "fsl,imx-audio-tlv320aic32x4", },1077 { .compatible = "fsl,imx-audio-tlv320aic31xx", },1078 { .compatible = "fsl,imx-audio-sgtl5000", },1079 { .compatible = "fsl,imx-audio-wm8962", },1080 { .compatible = "fsl,imx-audio-wm8960", },1081 { .compatible = "fsl,imx-audio-mqs", },1082 { .compatible = "fsl,imx-audio-wm8524", },1083 { .compatible = "fsl,imx-audio-si476x", },1084 { .compatible = "fsl,imx-audio-wm8958", },1085 { .compatible = "fsl,imx-audio-nau8822", },1086 { .compatible = "fsl,imx-audio-wm8904", },1087 { .compatible = "fsl,imx-audio-spdif", },1088 {}1089};1090MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);1091 1092static struct platform_driver fsl_asoc_card_driver = {1093 .probe = fsl_asoc_card_probe,1094 .driver = {1095 .name = DRIVER_NAME,1096 .pm = &snd_soc_pm_ops,1097 .of_match_table = fsl_asoc_card_dt_ids,1098 },1099};1100module_platform_driver(fsl_asoc_card_driver);1101 1102MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");1103MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");1104MODULE_ALIAS("platform:" DRIVER_NAME);1105MODULE_LICENSE("GPL");1106