fsl-asoc-card.c 25 KB

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