imx-wm8962.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 = platform_get_drvdata(priv->pdev);
  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. dapm->bias_level = level;
  115. return 0;
  116. }
  117. static int imx_wm8962_late_probe(struct snd_soc_card *card)
  118. {
  119. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  120. struct imx_priv *priv = &card_priv;
  121. struct imx_wm8962_data *data = platform_get_drvdata(priv->pdev);
  122. struct device *dev = &priv->pdev->dev;
  123. int ret;
  124. ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
  125. data->clk_frequency, SND_SOC_CLOCK_IN);
  126. if (ret < 0)
  127. dev_err(dev, "failed to set sysclk in %s\n", __func__);
  128. return ret;
  129. }
  130. static int imx_wm8962_probe(struct platform_device *pdev)
  131. {
  132. struct device_node *np = pdev->dev.of_node;
  133. struct device_node *ssi_np, *codec_np;
  134. struct platform_device *ssi_pdev;
  135. struct imx_priv *priv = &card_priv;
  136. struct i2c_client *codec_dev;
  137. struct imx_wm8962_data *data;
  138. int int_port, ext_port;
  139. int ret;
  140. priv->pdev = pdev;
  141. ret = of_property_read_u32(np, "mux-int-port", &int_port);
  142. if (ret) {
  143. dev_err(&pdev->dev, "mux-int-port missing or invalid\n");
  144. return ret;
  145. }
  146. ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
  147. if (ret) {
  148. dev_err(&pdev->dev, "mux-ext-port missing or invalid\n");
  149. return ret;
  150. }
  151. /*
  152. * The port numbering in the hardware manual starts at 1, while
  153. * the audmux API expects it starts at 0.
  154. */
  155. int_port--;
  156. ext_port--;
  157. ret = imx_audmux_v2_configure_port(int_port,
  158. IMX_AUDMUX_V2_PTCR_SYN |
  159. IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
  160. IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
  161. IMX_AUDMUX_V2_PTCR_TFSDIR |
  162. IMX_AUDMUX_V2_PTCR_TCLKDIR,
  163. IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
  164. if (ret) {
  165. dev_err(&pdev->dev, "audmux internal port setup failed\n");
  166. return ret;
  167. }
  168. imx_audmux_v2_configure_port(ext_port,
  169. IMX_AUDMUX_V2_PTCR_SYN,
  170. IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
  171. if (ret) {
  172. dev_err(&pdev->dev, "audmux external port setup failed\n");
  173. return ret;
  174. }
  175. ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0);
  176. codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
  177. if (!ssi_np || !codec_np) {
  178. dev_err(&pdev->dev, "phandle missing or invalid\n");
  179. ret = -EINVAL;
  180. goto fail;
  181. }
  182. ssi_pdev = of_find_device_by_node(ssi_np);
  183. if (!ssi_pdev) {
  184. dev_err(&pdev->dev, "failed to find SSI platform device\n");
  185. ret = -EINVAL;
  186. goto fail;
  187. }
  188. codec_dev = of_find_i2c_device_by_node(codec_np);
  189. if (!codec_dev || !codec_dev->driver) {
  190. dev_err(&pdev->dev, "failed to find codec platform device\n");
  191. ret = -EINVAL;
  192. goto fail;
  193. }
  194. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  195. if (!data) {
  196. ret = -ENOMEM;
  197. goto fail;
  198. }
  199. data->codec_clk = devm_clk_get(&codec_dev->dev, NULL);
  200. if (IS_ERR(data->codec_clk)) {
  201. ret = PTR_ERR(data->codec_clk);
  202. dev_err(&codec_dev->dev, "failed to get codec clk: %d\n", ret);
  203. goto fail;
  204. }
  205. data->clk_frequency = clk_get_rate(data->codec_clk);
  206. ret = clk_prepare_enable(data->codec_clk);
  207. if (ret) {
  208. dev_err(&codec_dev->dev, "failed to enable codec clk: %d\n", ret);
  209. goto fail;
  210. }
  211. data->dai.name = "HiFi";
  212. data->dai.stream_name = "HiFi";
  213. data->dai.codec_dai_name = "wm8962";
  214. data->dai.codec_of_node = codec_np;
  215. data->dai.cpu_dai_name = dev_name(&ssi_pdev->dev);
  216. data->dai.platform_of_node = ssi_np;
  217. data->dai.ops = &imx_hifi_ops;
  218. data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  219. SND_SOC_DAIFMT_CBM_CFM;
  220. data->card.dev = &pdev->dev;
  221. ret = snd_soc_of_parse_card_name(&data->card, "model");
  222. if (ret)
  223. goto clk_fail;
  224. ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
  225. if (ret)
  226. goto clk_fail;
  227. data->card.num_links = 1;
  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. ret = snd_soc_register_card(&data->card);
  234. if (ret) {
  235. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
  236. goto clk_fail;
  237. }
  238. platform_set_drvdata(pdev, data);
  239. of_node_put(ssi_np);
  240. of_node_put(codec_np);
  241. return 0;
  242. clk_fail:
  243. if (!IS_ERR(data->codec_clk))
  244. clk_disable_unprepare(data->codec_clk);
  245. fail:
  246. if (ssi_np)
  247. of_node_put(ssi_np);
  248. if (codec_np)
  249. of_node_put(codec_np);
  250. return ret;
  251. }
  252. static int imx_wm8962_remove(struct platform_device *pdev)
  253. {
  254. struct imx_wm8962_data *data = platform_get_drvdata(pdev);
  255. if (!IS_ERR(data->codec_clk))
  256. clk_disable_unprepare(data->codec_clk);
  257. snd_soc_unregister_card(&data->card);
  258. return 0;
  259. }
  260. static const struct of_device_id imx_wm8962_dt_ids[] = {
  261. { .compatible = "fsl,imx-audio-wm8962", },
  262. { /* sentinel */ }
  263. };
  264. MODULE_DEVICE_TABLE(of, imx_wm8962_dt_ids);
  265. static struct platform_driver imx_wm8962_driver = {
  266. .driver = {
  267. .name = "imx-wm8962",
  268. .owner = THIS_MODULE,
  269. .of_match_table = imx_wm8962_dt_ids,
  270. },
  271. .probe = imx_wm8962_probe,
  272. .remove = imx_wm8962_remove,
  273. };
  274. module_platform_driver(imx_wm8962_driver);
  275. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  276. MODULE_DESCRIPTION("Freescale i.MX WM8962 ASoC machine driver");
  277. MODULE_LICENSE("GPL v2");
  278. MODULE_ALIAS("platform:imx-wm8962");