fsl-asoc-card.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. * Freescale Generic ASoC Sound Card driver with ASRC
  3. *
  4. * Copyright (C) 2014 Freescale Semiconductor, Inc.
  5. *
  6. * Author: Nicolin Chen <nicoleotsuka@gmail.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/i2c.h>
  14. #include <linux/module.h>
  15. #include <linux/of_platform.h>
  16. #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
  17. #include <sound/ac97_codec.h>
  18. #endif
  19. #include <sound/pcm_params.h>
  20. #include <sound/soc.h>
  21. #include "fsl_esai.h"
  22. #include "fsl_sai.h"
  23. #include "imx-audmux.h"
  24. #include "../codecs/sgtl5000.h"
  25. #include "../codecs/wm8962.h"
  26. #include "../codecs/wm8960.h"
  27. #define CS427x_SYSCLK_MCLK 0
  28. #define RX 0
  29. #define TX 1
  30. /* Default DAI format without Master and Slave flag */
  31. #define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
  32. /**
  33. * CODEC private data
  34. *
  35. * @mclk_freq: Clock rate of MCLK
  36. * @mclk_id: MCLK (or main clock) id for set_sysclk()
  37. * @fll_id: FLL (or secordary clock) id for set_sysclk()
  38. * @pll_id: PLL id for set_pll()
  39. */
  40. struct codec_priv {
  41. unsigned long mclk_freq;
  42. u32 mclk_id;
  43. u32 fll_id;
  44. u32 pll_id;
  45. };
  46. /**
  47. * CPU private data
  48. *
  49. * @sysclk_freq[2]: SYSCLK rates for set_sysclk()
  50. * @sysclk_dir[2]: SYSCLK directions for set_sysclk()
  51. * @sysclk_id[2]: SYSCLK ids for set_sysclk()
  52. * @slot_width: Slot width of each frame
  53. *
  54. * Note: [1] for tx and [0] for rx
  55. */
  56. struct cpu_priv {
  57. unsigned long sysclk_freq[2];
  58. u32 sysclk_dir[2];
  59. u32 sysclk_id[2];
  60. u32 slot_width;
  61. };
  62. /**
  63. * Freescale Generic ASOC card private data
  64. *
  65. * @dai_link[3]: DAI link structure including normal one and DPCM link
  66. * @pdev: platform device pointer
  67. * @codec_priv: CODEC private data
  68. * @cpu_priv: CPU private data
  69. * @card: ASoC card structure
  70. * @sample_rate: Current sample rate
  71. * @sample_format: Current sample format
  72. * @asrc_rate: ASRC sample rate used by Back-Ends
  73. * @asrc_format: ASRC sample format used by Back-Ends
  74. * @dai_fmt: DAI format between CPU and CODEC
  75. * @name: Card name
  76. */
  77. struct fsl_asoc_card_priv {
  78. struct snd_soc_dai_link dai_link[3];
  79. struct platform_device *pdev;
  80. struct codec_priv codec_priv;
  81. struct cpu_priv cpu_priv;
  82. struct snd_soc_card card;
  83. u32 sample_rate;
  84. u32 sample_format;
  85. u32 asrc_rate;
  86. u32 asrc_format;
  87. u32 dai_fmt;
  88. char name[32];
  89. };
  90. /**
  91. * This dapm route map exsits for DPCM link only.
  92. * The other routes shall go through Device Tree.
  93. *
  94. * Note: keep all ASRC routes in the second half
  95. * to drop them easily for non-ASRC cases.
  96. */
  97. static const struct snd_soc_dapm_route audio_map[] = {
  98. /* 1st half -- Normal DAPM routes */
  99. {"Playback", NULL, "CPU-Playback"},
  100. {"CPU-Capture", NULL, "Capture"},
  101. /* 2nd half -- ASRC DAPM routes */
  102. {"CPU-Playback", NULL, "ASRC-Playback"},
  103. {"ASRC-Capture", NULL, "CPU-Capture"},
  104. };
  105. static const struct snd_soc_dapm_route audio_map_ac97[] = {
  106. /* 1st half -- Normal DAPM routes */
  107. {"Playback", NULL, "AC97 Playback"},
  108. {"AC97 Capture", NULL, "Capture"},
  109. /* 2nd half -- ASRC DAPM routes */
  110. {"AC97 Playback", NULL, "ASRC-Playback"},
  111. {"ASRC-Capture", NULL, "AC97 Capture"},
  112. };
  113. /* Add all possible widgets into here without being redundant */
  114. static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
  115. SND_SOC_DAPM_LINE("Line Out Jack", NULL),
  116. SND_SOC_DAPM_LINE("Line In Jack", NULL),
  117. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  118. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  119. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  120. SND_SOC_DAPM_MIC("AMIC", NULL),
  121. SND_SOC_DAPM_MIC("DMIC", NULL),
  122. };
  123. static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
  124. {
  125. return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
  126. }
  127. static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
  128. struct snd_pcm_hw_params *params)
  129. {
  130. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  131. struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  132. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  133. struct cpu_priv *cpu_priv = &priv->cpu_priv;
  134. struct device *dev = rtd->card->dev;
  135. int ret;
  136. priv->sample_rate = params_rate(params);
  137. priv->sample_format = params_format(params);
  138. /*
  139. * If codec-dai is DAI Master and all configurations are already in the
  140. * set_bias_level(), bypass the remaining settings in hw_params().
  141. * Note: (dai_fmt & CBM_CFM) includes CBM_CFM and CBM_CFS.
  142. */
  143. if ((priv->card.set_bias_level &&
  144. priv->dai_fmt & SND_SOC_DAIFMT_CBM_CFM) ||
  145. fsl_asoc_card_is_ac97(priv))
  146. return 0;
  147. /* Specific configurations of DAIs starts from here */
  148. ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, cpu_priv->sysclk_id[tx],
  149. cpu_priv->sysclk_freq[tx],
  150. cpu_priv->sysclk_dir[tx]);
  151. if (ret) {
  152. dev_err(dev, "failed to set sysclk for cpu dai\n");
  153. return ret;
  154. }
  155. if (cpu_priv->slot_width) {
  156. ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2,
  157. cpu_priv->slot_width);
  158. if (ret) {
  159. dev_err(dev, "failed to set TDM slot for cpu dai\n");
  160. return ret;
  161. }
  162. }
  163. return 0;
  164. }
  165. static struct snd_soc_ops fsl_asoc_card_ops = {
  166. .hw_params = fsl_asoc_card_hw_params,
  167. };
  168. static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
  169. struct snd_pcm_hw_params *params)
  170. {
  171. struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  172. struct snd_interval *rate;
  173. struct snd_mask *mask;
  174. rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  175. rate->max = rate->min = priv->asrc_rate;
  176. mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  177. snd_mask_none(mask);
  178. snd_mask_set(mask, priv->asrc_format);
  179. return 0;
  180. }
  181. static struct snd_soc_dai_link fsl_asoc_card_dai[] = {
  182. /* Default ASoC DAI Link*/
  183. {
  184. .name = "HiFi",
  185. .stream_name = "HiFi",
  186. .ops = &fsl_asoc_card_ops,
  187. },
  188. /* DPCM Link between Front-End and Back-End (Optional) */
  189. {
  190. .name = "HiFi-ASRC-FE",
  191. .stream_name = "HiFi-ASRC-FE",
  192. .codec_name = "snd-soc-dummy",
  193. .codec_dai_name = "snd-soc-dummy-dai",
  194. .dpcm_playback = 1,
  195. .dpcm_capture = 1,
  196. .dynamic = 1,
  197. },
  198. {
  199. .name = "HiFi-ASRC-BE",
  200. .stream_name = "HiFi-ASRC-BE",
  201. .platform_name = "snd-soc-dummy",
  202. .be_hw_params_fixup = be_hw_params_fixup,
  203. .ops = &fsl_asoc_card_ops,
  204. .dpcm_playback = 1,
  205. .dpcm_capture = 1,
  206. .no_pcm = 1,
  207. },
  208. };
  209. static int fsl_asoc_card_set_bias_level(struct snd_soc_card *card,
  210. struct snd_soc_dapm_context *dapm,
  211. enum snd_soc_bias_level level)
  212. {
  213. struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
  214. struct snd_soc_pcm_runtime *rtd;
  215. struct snd_soc_dai *codec_dai;
  216. struct codec_priv *codec_priv = &priv->codec_priv;
  217. struct device *dev = card->dev;
  218. unsigned int pll_out;
  219. int ret;
  220. rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
  221. codec_dai = rtd->codec_dai;
  222. if (dapm->dev != codec_dai->dev)
  223. return 0;
  224. switch (level) {
  225. case SND_SOC_BIAS_PREPARE:
  226. if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
  227. break;
  228. if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
  229. pll_out = priv->sample_rate * 384;
  230. else
  231. pll_out = priv->sample_rate * 256;
  232. ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id,
  233. codec_priv->mclk_id,
  234. codec_priv->mclk_freq, pll_out);
  235. if (ret) {
  236. dev_err(dev, "failed to start FLL: %d\n", ret);
  237. return ret;
  238. }
  239. ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->fll_id,
  240. pll_out, SND_SOC_CLOCK_IN);
  241. if (ret) {
  242. dev_err(dev, "failed to set SYSCLK: %d\n", ret);
  243. return ret;
  244. }
  245. break;
  246. case SND_SOC_BIAS_STANDBY:
  247. if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
  248. break;
  249. ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
  250. codec_priv->mclk_freq,
  251. SND_SOC_CLOCK_IN);
  252. if (ret) {
  253. dev_err(dev, "failed to switch away from FLL: %d\n", ret);
  254. return ret;
  255. }
  256. ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id, 0, 0, 0);
  257. if (ret) {
  258. dev_err(dev, "failed to stop FLL: %d\n", ret);
  259. return ret;
  260. }
  261. break;
  262. default:
  263. break;
  264. }
  265. return 0;
  266. }
  267. static int fsl_asoc_card_audmux_init(struct device_node *np,
  268. struct fsl_asoc_card_priv *priv)
  269. {
  270. struct device *dev = &priv->pdev->dev;
  271. u32 int_ptcr = 0, ext_ptcr = 0;
  272. int int_port, ext_port;
  273. int ret;
  274. ret = of_property_read_u32(np, "mux-int-port", &int_port);
  275. if (ret) {
  276. dev_err(dev, "mux-int-port missing or invalid\n");
  277. return ret;
  278. }
  279. ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
  280. if (ret) {
  281. dev_err(dev, "mux-ext-port missing or invalid\n");
  282. return ret;
  283. }
  284. /*
  285. * The port numbering in the hardware manual starts at 1, while
  286. * the AUDMUX API expects it starts at 0.
  287. */
  288. int_port--;
  289. ext_port--;
  290. /*
  291. * Use asynchronous mode (6 wires) for all cases except AC97.
  292. * If only 4 wires are needed, just set SSI into
  293. * synchronous mode and enable 4 PADs in IOMUX.
  294. */
  295. switch (priv->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  296. case SND_SOC_DAIFMT_CBM_CFM:
  297. int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
  298. IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
  299. IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
  300. IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
  301. IMX_AUDMUX_V2_PTCR_RFSDIR |
  302. IMX_AUDMUX_V2_PTCR_RCLKDIR |
  303. IMX_AUDMUX_V2_PTCR_TFSDIR |
  304. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  305. break;
  306. case SND_SOC_DAIFMT_CBM_CFS:
  307. int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
  308. IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
  309. IMX_AUDMUX_V2_PTCR_RCLKDIR |
  310. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  311. ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
  312. IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
  313. IMX_AUDMUX_V2_PTCR_RFSDIR |
  314. IMX_AUDMUX_V2_PTCR_TFSDIR;
  315. break;
  316. case SND_SOC_DAIFMT_CBS_CFM:
  317. int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
  318. IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
  319. IMX_AUDMUX_V2_PTCR_RFSDIR |
  320. IMX_AUDMUX_V2_PTCR_TFSDIR;
  321. ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
  322. IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
  323. IMX_AUDMUX_V2_PTCR_RCLKDIR |
  324. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  325. break;
  326. case SND_SOC_DAIFMT_CBS_CFS:
  327. ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
  328. IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
  329. IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
  330. IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
  331. IMX_AUDMUX_V2_PTCR_RFSDIR |
  332. IMX_AUDMUX_V2_PTCR_RCLKDIR |
  333. IMX_AUDMUX_V2_PTCR_TFSDIR |
  334. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  335. break;
  336. default:
  337. if (!fsl_asoc_card_is_ac97(priv))
  338. return -EINVAL;
  339. }
  340. if (fsl_asoc_card_is_ac97(priv)) {
  341. int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
  342. IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
  343. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  344. ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
  345. IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
  346. IMX_AUDMUX_V2_PTCR_TFSDIR;
  347. }
  348. /* Asynchronous mode can not be set along with RCLKDIR */
  349. if (!fsl_asoc_card_is_ac97(priv)) {
  350. unsigned int pdcr =
  351. IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
  352. ret = imx_audmux_v2_configure_port(int_port, 0,
  353. pdcr);
  354. if (ret) {
  355. dev_err(dev, "audmux internal port setup failed\n");
  356. return ret;
  357. }
  358. }
  359. ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
  360. IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
  361. if (ret) {
  362. dev_err(dev, "audmux internal port setup failed\n");
  363. return ret;
  364. }
  365. if (!fsl_asoc_card_is_ac97(priv)) {
  366. unsigned int pdcr =
  367. IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
  368. ret = imx_audmux_v2_configure_port(ext_port, 0,
  369. pdcr);
  370. if (ret) {
  371. dev_err(dev, "audmux external port setup failed\n");
  372. return ret;
  373. }
  374. }
  375. ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
  376. IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
  377. if (ret) {
  378. dev_err(dev, "audmux external port setup failed\n");
  379. return ret;
  380. }
  381. return 0;
  382. }
  383. static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
  384. {
  385. struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
  386. struct snd_soc_pcm_runtime *rtd = list_first_entry(
  387. &card->rtd_list, struct snd_soc_pcm_runtime, list);
  388. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  389. struct codec_priv *codec_priv = &priv->codec_priv;
  390. struct device *dev = card->dev;
  391. int ret;
  392. if (fsl_asoc_card_is_ac97(priv)) {
  393. #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
  394. struct snd_soc_codec *codec = rtd->codec;
  395. struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
  396. /*
  397. * Use slots 3/4 for S/PDIF so SSI won't try to enable
  398. * other slots and send some samples there
  399. * due to SLOTREQ bits for S/PDIF received from codec
  400. */
  401. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
  402. AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
  403. #endif
  404. return 0;
  405. }
  406. ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
  407. codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
  408. if (ret) {
  409. dev_err(dev, "failed to set sysclk in %s\n", __func__);
  410. return ret;
  411. }
  412. return 0;
  413. }
  414. static int fsl_asoc_card_probe(struct platform_device *pdev)
  415. {
  416. struct device_node *cpu_np, *codec_np, *asrc_np;
  417. struct device_node *np = pdev->dev.of_node;
  418. struct platform_device *asrc_pdev = NULL;
  419. struct platform_device *cpu_pdev;
  420. struct fsl_asoc_card_priv *priv;
  421. struct i2c_client *codec_dev;
  422. const char *codec_dai_name;
  423. u32 width;
  424. int ret;
  425. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  426. if (!priv)
  427. return -ENOMEM;
  428. cpu_np = of_parse_phandle(np, "audio-cpu", 0);
  429. /* Give a chance to old DT binding */
  430. if (!cpu_np)
  431. cpu_np = of_parse_phandle(np, "ssi-controller", 0);
  432. if (!cpu_np) {
  433. dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
  434. ret = -EINVAL;
  435. goto fail;
  436. }
  437. cpu_pdev = of_find_device_by_node(cpu_np);
  438. if (!cpu_pdev) {
  439. dev_err(&pdev->dev, "failed to find CPU DAI device\n");
  440. ret = -EINVAL;
  441. goto fail;
  442. }
  443. codec_np = of_parse_phandle(np, "audio-codec", 0);
  444. if (codec_np)
  445. codec_dev = of_find_i2c_device_by_node(codec_np);
  446. else
  447. codec_dev = NULL;
  448. asrc_np = of_parse_phandle(np, "audio-asrc", 0);
  449. if (asrc_np)
  450. asrc_pdev = of_find_device_by_node(asrc_np);
  451. /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
  452. if (codec_dev) {
  453. struct clk *codec_clk = clk_get(&codec_dev->dev, NULL);
  454. if (!IS_ERR(codec_clk)) {
  455. priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
  456. clk_put(codec_clk);
  457. }
  458. }
  459. /* Default sample rate and format, will be updated in hw_params() */
  460. priv->sample_rate = 44100;
  461. priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
  462. /* Assign a default DAI format, and allow each card to overwrite it */
  463. priv->dai_fmt = DAI_FMT_BASE;
  464. /* Diversify the card configurations */
  465. if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
  466. codec_dai_name = "cs42888";
  467. priv->card.set_bias_level = NULL;
  468. priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
  469. priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
  470. priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
  471. priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
  472. priv->cpu_priv.slot_width = 32;
  473. priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
  474. } else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {
  475. codec_dai_name = "cs4271-hifi";
  476. priv->codec_priv.mclk_id = CS427x_SYSCLK_MCLK;
  477. priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  478. } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
  479. codec_dai_name = "sgtl5000";
  480. priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
  481. priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  482. } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
  483. codec_dai_name = "wm8962";
  484. priv->card.set_bias_level = fsl_asoc_card_set_bias_level;
  485. priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
  486. priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
  487. priv->codec_priv.pll_id = WM8962_FLL;
  488. priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  489. } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
  490. codec_dai_name = "wm8960-hifi";
  491. priv->card.set_bias_level = fsl_asoc_card_set_bias_level;
  492. priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO;
  493. priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO;
  494. priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  495. } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
  496. codec_dai_name = "ac97-hifi";
  497. priv->card.set_bias_level = NULL;
  498. priv->dai_fmt = SND_SOC_DAIFMT_AC97;
  499. } else {
  500. dev_err(&pdev->dev, "unknown Device Tree compatible\n");
  501. ret = -EINVAL;
  502. goto asrc_fail;
  503. }
  504. if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) {
  505. dev_err(&pdev->dev, "failed to find codec device\n");
  506. ret = -EINVAL;
  507. goto asrc_fail;
  508. }
  509. /* Common settings for corresponding Freescale CPU DAI driver */
  510. if (strstr(cpu_np->name, "ssi")) {
  511. /* Only SSI needs to configure AUDMUX */
  512. ret = fsl_asoc_card_audmux_init(np, priv);
  513. if (ret) {
  514. dev_err(&pdev->dev, "failed to init audmux\n");
  515. goto asrc_fail;
  516. }
  517. } else if (strstr(cpu_np->name, "esai")) {
  518. priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
  519. priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
  520. } else if (strstr(cpu_np->name, "sai")) {
  521. priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
  522. priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
  523. }
  524. snprintf(priv->name, sizeof(priv->name), "%s-audio",
  525. fsl_asoc_card_is_ac97(priv) ? "ac97" :
  526. codec_dev->name);
  527. /* Initialize sound card */
  528. priv->pdev = pdev;
  529. priv->card.dev = &pdev->dev;
  530. priv->card.name = priv->name;
  531. priv->card.dai_link = priv->dai_link;
  532. priv->card.dapm_routes = fsl_asoc_card_is_ac97(priv) ?
  533. audio_map_ac97 : audio_map;
  534. priv->card.late_probe = fsl_asoc_card_late_probe;
  535. priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
  536. priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
  537. priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
  538. /* Drop the second half of DAPM routes -- ASRC */
  539. if (!asrc_pdev)
  540. priv->card.num_dapm_routes /= 2;
  541. memcpy(priv->dai_link, fsl_asoc_card_dai,
  542. sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
  543. ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
  544. if (ret) {
  545. dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
  546. goto asrc_fail;
  547. }
  548. /* Normal DAI Link */
  549. priv->dai_link[0].cpu_of_node = cpu_np;
  550. priv->dai_link[0].codec_dai_name = codec_dai_name;
  551. if (!fsl_asoc_card_is_ac97(priv))
  552. priv->dai_link[0].codec_of_node = codec_np;
  553. else {
  554. u32 idx;
  555. ret = of_property_read_u32(cpu_np, "cell-index", &idx);
  556. if (ret) {
  557. dev_err(&pdev->dev,
  558. "cannot get CPU index property\n");
  559. goto asrc_fail;
  560. }
  561. priv->dai_link[0].codec_name =
  562. devm_kasprintf(&pdev->dev, GFP_KERNEL,
  563. "ac97-codec.%u",
  564. (unsigned int)idx);
  565. }
  566. priv->dai_link[0].platform_of_node = cpu_np;
  567. priv->dai_link[0].dai_fmt = priv->dai_fmt;
  568. priv->card.num_links = 1;
  569. if (asrc_pdev) {
  570. /* DPCM DAI Links only if ASRC exsits */
  571. priv->dai_link[1].cpu_of_node = asrc_np;
  572. priv->dai_link[1].platform_of_node = asrc_np;
  573. priv->dai_link[2].codec_dai_name = codec_dai_name;
  574. priv->dai_link[2].codec_of_node = codec_np;
  575. priv->dai_link[2].codec_name =
  576. priv->dai_link[0].codec_name;
  577. priv->dai_link[2].cpu_of_node = cpu_np;
  578. priv->dai_link[2].dai_fmt = priv->dai_fmt;
  579. priv->card.num_links = 3;
  580. ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
  581. &priv->asrc_rate);
  582. if (ret) {
  583. dev_err(&pdev->dev, "failed to get output rate\n");
  584. ret = -EINVAL;
  585. goto asrc_fail;
  586. }
  587. ret = of_property_read_u32(asrc_np, "fsl,asrc-width", &width);
  588. if (ret) {
  589. dev_err(&pdev->dev, "failed to get output rate\n");
  590. ret = -EINVAL;
  591. goto asrc_fail;
  592. }
  593. if (width == 24)
  594. priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
  595. else
  596. priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
  597. }
  598. /* Finish card registering */
  599. platform_set_drvdata(pdev, priv);
  600. snd_soc_card_set_drvdata(&priv->card, priv);
  601. ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
  602. if (ret)
  603. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
  604. asrc_fail:
  605. of_node_put(asrc_np);
  606. of_node_put(codec_np);
  607. fail:
  608. of_node_put(cpu_np);
  609. return ret;
  610. }
  611. static const struct of_device_id fsl_asoc_card_dt_ids[] = {
  612. { .compatible = "fsl,imx-audio-ac97", },
  613. { .compatible = "fsl,imx-audio-cs42888", },
  614. { .compatible = "fsl,imx-audio-cs427x", },
  615. { .compatible = "fsl,imx-audio-sgtl5000", },
  616. { .compatible = "fsl,imx-audio-wm8962", },
  617. { .compatible = "fsl,imx-audio-wm8960", },
  618. {}
  619. };
  620. MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
  621. static struct platform_driver fsl_asoc_card_driver = {
  622. .probe = fsl_asoc_card_probe,
  623. .driver = {
  624. .name = "fsl-asoc-card",
  625. .pm = &snd_soc_pm_ops,
  626. .of_match_table = fsl_asoc_card_dt_ids,
  627. },
  628. };
  629. module_platform_driver(fsl_asoc_card_driver);
  630. MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
  631. MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
  632. MODULE_ALIAS("platform:fsl-asoc-card");
  633. MODULE_LICENSE("GPL");