imx-wm8962.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright 2013 Freescale Semiconductor, Inc.
  3. *
  4. * Based on imx-sgtl5000.c
  5. * Copyright 2012 Freescale Semiconductor, Inc.
  6. * Copyright 2012 Linaro Ltd.
  7. *
  8. * The code contained herein is licensed under the GNU General Public
  9. * License. You may obtain a copy of the GNU General Public License
  10. * Version 2 or later at the following locations:
  11. *
  12. * http://www.opensource.org/licenses/gpl-license.html
  13. * http://www.gnu.org/copyleft/gpl.html
  14. */
  15. #include <linux/module.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/i2c.h>
  18. #include <linux/slab.h>
  19. #include <linux/clk.h>
  20. #include <sound/soc.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/soc-dapm.h>
  23. #include <linux/pinctrl/consumer.h>
  24. #include "../codecs/wm8962.h"
  25. #include "imx-audmux.h"
  26. #define DAI_NAME_SIZE 32
  27. struct imx_wm8962_data {
  28. struct snd_soc_dai_link dai;
  29. struct snd_soc_card card;
  30. char codec_dai_name[DAI_NAME_SIZE];
  31. char platform_name[DAI_NAME_SIZE];
  32. struct clk *codec_clk;
  33. unsigned int clk_frequency;
  34. };
  35. struct imx_priv {
  36. struct platform_device *pdev;
  37. };
  38. static struct imx_priv card_priv;
  39. static const struct snd_soc_dapm_widget imx_wm8962_dapm_widgets[] = {
  40. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  41. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  42. SND_SOC_DAPM_MIC("AMIC", NULL),
  43. SND_SOC_DAPM_MIC("DMIC", NULL),
  44. };
  45. static int sample_rate = 44100;
  46. static snd_pcm_format_t sample_format = SNDRV_PCM_FORMAT_S16_LE;
  47. static int imx_hifi_hw_params(struct snd_pcm_substream *substream,
  48. struct snd_pcm_hw_params *params)
  49. {
  50. sample_rate = params_rate(params);
  51. sample_format = params_format(params);
  52. return 0;
  53. }
  54. static struct snd_soc_ops imx_hifi_ops = {
  55. .hw_params = imx_hifi_hw_params,
  56. };
  57. static int imx_wm8962_set_bias_level(struct snd_soc_card *card,
  58. struct snd_soc_dapm_context *dapm,
  59. enum snd_soc_bias_level level)
  60. {
  61. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  62. struct imx_priv *priv = &card_priv;
  63. struct imx_wm8962_data *data = snd_soc_card_get_drvdata(card);
  64. struct device *dev = &priv->pdev->dev;
  65. unsigned int pll_out;
  66. int ret;
  67. if (dapm->dev != codec_dai->dev)
  68. return 0;
  69. switch (level) {
  70. case SND_SOC_BIAS_PREPARE:
  71. if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
  72. if (sample_format == SNDRV_PCM_FORMAT_S24_LE)
  73. pll_out = sample_rate * 384;
  74. else
  75. pll_out = sample_rate * 256;
  76. ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
  77. WM8962_FLL_MCLK, data->clk_frequency,
  78. pll_out);
  79. if (ret < 0) {
  80. dev_err(dev, "failed to start FLL: %d\n", ret);
  81. return ret;
  82. }
  83. ret = snd_soc_dai_set_sysclk(codec_dai,
  84. WM8962_SYSCLK_FLL, pll_out,
  85. SND_SOC_CLOCK_IN);
  86. if (ret < 0) {
  87. dev_err(dev, "failed to set SYSCLK: %d\n", ret);
  88. return ret;
  89. }
  90. }
  91. break;
  92. case SND_SOC_BIAS_STANDBY:
  93. if (dapm->bias_level == SND_SOC_BIAS_PREPARE) {
  94. ret = snd_soc_dai_set_sysclk(codec_dai,
  95. WM8962_SYSCLK_MCLK, data->clk_frequency,
  96. SND_SOC_CLOCK_IN);
  97. if (ret < 0) {
  98. dev_err(dev,
  99. "failed to switch away from FLL: %d\n",
  100. ret);
  101. return ret;
  102. }
  103. ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
  104. 0, 0, 0);
  105. if (ret < 0) {
  106. dev_err(dev, "failed to stop FLL: %d\n", ret);
  107. return ret;
  108. }
  109. }
  110. break;
  111. default:
  112. break;
  113. }
  114. return 0;
  115. }
  116. static int imx_wm8962_late_probe(struct snd_soc_card *card)
  117. {
  118. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  119. struct imx_priv *priv = &card_priv;
  120. struct imx_wm8962_data *data = snd_soc_card_get_drvdata(card);
  121. struct device *dev = &priv->pdev->dev;
  122. int ret;
  123. ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
  124. data->clk_frequency, SND_SOC_CLOCK_IN);
  125. if (ret < 0)
  126. dev_err(dev, "failed to set sysclk in %s\n", __func__);
  127. return ret;
  128. }
  129. static int imx_wm8962_probe(struct platform_device *pdev)
  130. {
  131. struct device_node *np = pdev->dev.of_node;
  132. struct device_node *ssi_np, *codec_np;
  133. struct platform_device *ssi_pdev;
  134. struct imx_priv *priv = &card_priv;
  135. struct i2c_client *codec_dev;
  136. struct imx_wm8962_data *data;
  137. int int_port, ext_port;
  138. int ret;
  139. priv->pdev = pdev;
  140. ret = of_property_read_u32(np, "mux-int-port", &int_port);
  141. if (ret) {
  142. dev_err(&pdev->dev, "mux-int-port missing or invalid\n");
  143. return ret;
  144. }
  145. ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
  146. if (ret) {
  147. dev_err(&pdev->dev, "mux-ext-port missing or invalid\n");
  148. return ret;
  149. }
  150. /*
  151. * The port numbering in the hardware manual starts at 1, while
  152. * the audmux API expects it starts at 0.
  153. */
  154. int_port--;
  155. ext_port--;
  156. ret = imx_audmux_v2_configure_port(int_port,
  157. IMX_AUDMUX_V2_PTCR_SYN |
  158. IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
  159. IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
  160. IMX_AUDMUX_V2_PTCR_TFSDIR |
  161. IMX_AUDMUX_V2_PTCR_TCLKDIR,
  162. IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
  163. if (ret) {
  164. dev_err(&pdev->dev, "audmux internal port setup failed\n");
  165. return ret;
  166. }
  167. ret = imx_audmux_v2_configure_port(ext_port,
  168. IMX_AUDMUX_V2_PTCR_SYN,
  169. IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
  170. if (ret) {
  171. dev_err(&pdev->dev, "audmux external port setup failed\n");
  172. return ret;
  173. }
  174. ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0);
  175. codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
  176. if (!ssi_np || !codec_np) {
  177. dev_err(&pdev->dev, "phandle missing or invalid\n");
  178. ret = -EINVAL;
  179. goto fail;
  180. }
  181. ssi_pdev = of_find_device_by_node(ssi_np);
  182. if (!ssi_pdev) {
  183. dev_err(&pdev->dev, "failed to find SSI platform device\n");
  184. ret = -EINVAL;
  185. goto fail;
  186. }
  187. codec_dev = of_find_i2c_device_by_node(codec_np);
  188. if (!codec_dev || !codec_dev->dev.driver) {
  189. dev_err(&pdev->dev, "failed to find codec platform device\n");
  190. ret = -EINVAL;
  191. goto fail;
  192. }
  193. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  194. if (!data) {
  195. ret = -ENOMEM;
  196. goto fail;
  197. }
  198. data->codec_clk = devm_clk_get(&codec_dev->dev, NULL);
  199. if (IS_ERR(data->codec_clk)) {
  200. ret = PTR_ERR(data->codec_clk);
  201. dev_err(&codec_dev->dev, "failed to get codec clk: %d\n", ret);
  202. goto fail;
  203. }
  204. data->clk_frequency = clk_get_rate(data->codec_clk);
  205. ret = clk_prepare_enable(data->codec_clk);
  206. if (ret) {
  207. dev_err(&codec_dev->dev, "failed to enable codec clk: %d\n", ret);
  208. goto fail;
  209. }
  210. data->dai.name = "HiFi";
  211. data->dai.stream_name = "HiFi";
  212. data->dai.codec_dai_name = "wm8962";
  213. data->dai.codec_of_node = codec_np;
  214. data->dai.cpu_dai_name = dev_name(&ssi_pdev->dev);
  215. data->dai.platform_of_node = ssi_np;
  216. data->dai.ops = &imx_hifi_ops;
  217. data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  218. SND_SOC_DAIFMT_CBM_CFM;
  219. data->card.dev = &pdev->dev;
  220. ret = snd_soc_of_parse_card_name(&data->card, "model");
  221. if (ret)
  222. goto clk_fail;
  223. ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
  224. if (ret)
  225. goto clk_fail;
  226. data->card.num_links = 1;
  227. data->card.owner = THIS_MODULE;
  228. data->card.dai_link = &data->dai;
  229. data->card.dapm_widgets = imx_wm8962_dapm_widgets;
  230. data->card.num_dapm_widgets = ARRAY_SIZE(imx_wm8962_dapm_widgets);
  231. data->card.late_probe = imx_wm8962_late_probe;
  232. data->card.set_bias_level = imx_wm8962_set_bias_level;
  233. platform_set_drvdata(pdev, &data->card);
  234. snd_soc_card_set_drvdata(&data->card, data);
  235. ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
  236. if (ret) {
  237. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
  238. goto clk_fail;
  239. }
  240. of_node_put(ssi_np);
  241. of_node_put(codec_np);
  242. return 0;
  243. clk_fail:
  244. clk_disable_unprepare(data->codec_clk);
  245. fail:
  246. of_node_put(ssi_np);
  247. of_node_put(codec_np);
  248. return ret;
  249. }
  250. static int imx_wm8962_remove(struct platform_device *pdev)
  251. {
  252. struct snd_soc_card *card = platform_get_drvdata(pdev);
  253. struct imx_wm8962_data *data = snd_soc_card_get_drvdata(card);
  254. if (!IS_ERR(data->codec_clk))
  255. clk_disable_unprepare(data->codec_clk);
  256. return 0;
  257. }
  258. static const struct of_device_id imx_wm8962_dt_ids[] = {
  259. { .compatible = "fsl,imx-audio-wm8962", },
  260. { /* sentinel */ }
  261. };
  262. MODULE_DEVICE_TABLE(of, imx_wm8962_dt_ids);
  263. static struct platform_driver imx_wm8962_driver = {
  264. .driver = {
  265. .name = "imx-wm8962",
  266. .pm = &snd_soc_pm_ops,
  267. .of_match_table = imx_wm8962_dt_ids,
  268. },
  269. .probe = imx_wm8962_probe,
  270. .remove = imx_wm8962_remove,
  271. };
  272. module_platform_driver(imx_wm8962_driver);
  273. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  274. MODULE_DESCRIPTION("Freescale i.MX WM8962 ASoC machine driver");
  275. MODULE_LICENSE("GPL v2");
  276. MODULE_ALIAS("platform:imx-wm8962");