imx-wm8962.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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_pcm_runtime *rtd;
  62. struct snd_soc_dai *codec_dai;
  63. struct imx_priv *priv = &card_priv;
  64. struct imx_wm8962_data *data = snd_soc_card_get_drvdata(card);
  65. struct device *dev = &priv->pdev->dev;
  66. unsigned int pll_out;
  67. int ret;
  68. rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
  69. codec_dai = rtd->codec_dai;
  70. if (dapm->dev != codec_dai->dev)
  71. return 0;
  72. switch (level) {
  73. case SND_SOC_BIAS_PREPARE:
  74. if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
  75. if (sample_format == SNDRV_PCM_FORMAT_S24_LE)
  76. pll_out = sample_rate * 384;
  77. else
  78. pll_out = sample_rate * 256;
  79. ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
  80. WM8962_FLL_MCLK, data->clk_frequency,
  81. pll_out);
  82. if (ret < 0) {
  83. dev_err(dev, "failed to start FLL: %d\n", ret);
  84. return ret;
  85. }
  86. ret = snd_soc_dai_set_sysclk(codec_dai,
  87. WM8962_SYSCLK_FLL, pll_out,
  88. SND_SOC_CLOCK_IN);
  89. if (ret < 0) {
  90. dev_err(dev, "failed to set SYSCLK: %d\n", ret);
  91. return ret;
  92. }
  93. }
  94. break;
  95. case SND_SOC_BIAS_STANDBY:
  96. if (dapm->bias_level == SND_SOC_BIAS_PREPARE) {
  97. ret = snd_soc_dai_set_sysclk(codec_dai,
  98. WM8962_SYSCLK_MCLK, data->clk_frequency,
  99. SND_SOC_CLOCK_IN);
  100. if (ret < 0) {
  101. dev_err(dev,
  102. "failed to switch away from FLL: %d\n",
  103. ret);
  104. return ret;
  105. }
  106. ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
  107. 0, 0, 0);
  108. if (ret < 0) {
  109. dev_err(dev, "failed to stop FLL: %d\n", ret);
  110. return ret;
  111. }
  112. }
  113. break;
  114. default:
  115. break;
  116. }
  117. return 0;
  118. }
  119. static int imx_wm8962_late_probe(struct snd_soc_card *card)
  120. {
  121. struct snd_soc_pcm_runtime *rtd;
  122. struct snd_soc_dai *codec_dai;
  123. struct imx_priv *priv = &card_priv;
  124. struct imx_wm8962_data *data = snd_soc_card_get_drvdata(card);
  125. struct device *dev = &priv->pdev->dev;
  126. int ret;
  127. rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
  128. codec_dai = rtd->codec_dai;
  129. ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
  130. data->clk_frequency, SND_SOC_CLOCK_IN);
  131. if (ret < 0)
  132. dev_err(dev, "failed to set sysclk in %s\n", __func__);
  133. return ret;
  134. }
  135. static int imx_wm8962_probe(struct platform_device *pdev)
  136. {
  137. struct device_node *np = pdev->dev.of_node;
  138. struct device_node *ssi_np, *codec_np;
  139. struct platform_device *ssi_pdev;
  140. struct imx_priv *priv = &card_priv;
  141. struct i2c_client *codec_dev;
  142. struct imx_wm8962_data *data;
  143. int int_port, ext_port;
  144. int ret;
  145. priv->pdev = pdev;
  146. ret = of_property_read_u32(np, "mux-int-port", &int_port);
  147. if (ret) {
  148. dev_err(&pdev->dev, "mux-int-port missing or invalid\n");
  149. return ret;
  150. }
  151. ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
  152. if (ret) {
  153. dev_err(&pdev->dev, "mux-ext-port missing or invalid\n");
  154. return ret;
  155. }
  156. /*
  157. * The port numbering in the hardware manual starts at 1, while
  158. * the audmux API expects it starts at 0.
  159. */
  160. int_port--;
  161. ext_port--;
  162. ret = imx_audmux_v2_configure_port(int_port,
  163. IMX_AUDMUX_V2_PTCR_SYN |
  164. IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
  165. IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
  166. IMX_AUDMUX_V2_PTCR_TFSDIR |
  167. IMX_AUDMUX_V2_PTCR_TCLKDIR,
  168. IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
  169. if (ret) {
  170. dev_err(&pdev->dev, "audmux internal port setup failed\n");
  171. return ret;
  172. }
  173. ret = imx_audmux_v2_configure_port(ext_port,
  174. IMX_AUDMUX_V2_PTCR_SYN,
  175. IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
  176. if (ret) {
  177. dev_err(&pdev->dev, "audmux external port setup failed\n");
  178. return ret;
  179. }
  180. ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0);
  181. codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
  182. if (!ssi_np || !codec_np) {
  183. dev_err(&pdev->dev, "phandle missing or invalid\n");
  184. ret = -EINVAL;
  185. goto fail;
  186. }
  187. ssi_pdev = of_find_device_by_node(ssi_np);
  188. if (!ssi_pdev) {
  189. dev_err(&pdev->dev, "failed to find SSI platform device\n");
  190. ret = -EINVAL;
  191. goto fail;
  192. }
  193. codec_dev = of_find_i2c_device_by_node(codec_np);
  194. if (!codec_dev || !codec_dev->dev.driver) {
  195. dev_err(&pdev->dev, "failed to find codec platform device\n");
  196. ret = -EINVAL;
  197. goto fail;
  198. }
  199. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  200. if (!data) {
  201. ret = -ENOMEM;
  202. goto fail;
  203. }
  204. data->codec_clk = devm_clk_get(&codec_dev->dev, NULL);
  205. if (IS_ERR(data->codec_clk)) {
  206. ret = PTR_ERR(data->codec_clk);
  207. dev_err(&codec_dev->dev, "failed to get codec clk: %d\n", ret);
  208. goto fail;
  209. }
  210. data->clk_frequency = clk_get_rate(data->codec_clk);
  211. ret = clk_prepare_enable(data->codec_clk);
  212. if (ret) {
  213. dev_err(&codec_dev->dev, "failed to enable codec clk: %d\n", ret);
  214. goto fail;
  215. }
  216. data->dai.name = "HiFi";
  217. data->dai.stream_name = "HiFi";
  218. data->dai.codec_dai_name = "wm8962";
  219. data->dai.codec_of_node = codec_np;
  220. data->dai.cpu_dai_name = dev_name(&ssi_pdev->dev);
  221. data->dai.platform_of_node = ssi_np;
  222. data->dai.ops = &imx_hifi_ops;
  223. data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  224. SND_SOC_DAIFMT_CBM_CFM;
  225. data->card.dev = &pdev->dev;
  226. ret = snd_soc_of_parse_card_name(&data->card, "model");
  227. if (ret)
  228. goto clk_fail;
  229. ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
  230. if (ret)
  231. goto clk_fail;
  232. data->card.num_links = 1;
  233. data->card.owner = THIS_MODULE;
  234. data->card.dai_link = &data->dai;
  235. data->card.dapm_widgets = imx_wm8962_dapm_widgets;
  236. data->card.num_dapm_widgets = ARRAY_SIZE(imx_wm8962_dapm_widgets);
  237. data->card.late_probe = imx_wm8962_late_probe;
  238. data->card.set_bias_level = imx_wm8962_set_bias_level;
  239. platform_set_drvdata(pdev, &data->card);
  240. snd_soc_card_set_drvdata(&data->card, data);
  241. ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
  242. if (ret) {
  243. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
  244. goto clk_fail;
  245. }
  246. of_node_put(ssi_np);
  247. of_node_put(codec_np);
  248. return 0;
  249. clk_fail:
  250. clk_disable_unprepare(data->codec_clk);
  251. fail:
  252. of_node_put(ssi_np);
  253. of_node_put(codec_np);
  254. return ret;
  255. }
  256. static int imx_wm8962_remove(struct platform_device *pdev)
  257. {
  258. struct snd_soc_card *card = platform_get_drvdata(pdev);
  259. struct imx_wm8962_data *data = snd_soc_card_get_drvdata(card);
  260. if (!IS_ERR(data->codec_clk))
  261. clk_disable_unprepare(data->codec_clk);
  262. return 0;
  263. }
  264. static const struct of_device_id imx_wm8962_dt_ids[] = {
  265. { .compatible = "fsl,imx-audio-wm8962", },
  266. { /* sentinel */ }
  267. };
  268. MODULE_DEVICE_TABLE(of, imx_wm8962_dt_ids);
  269. static struct platform_driver imx_wm8962_driver = {
  270. .driver = {
  271. .name = "imx-wm8962",
  272. .pm = &snd_soc_pm_ops,
  273. .of_match_table = imx_wm8962_dt_ids,
  274. },
  275. .probe = imx_wm8962_probe,
  276. .remove = imx_wm8962_remove,
  277. };
  278. module_platform_driver(imx_wm8962_driver);
  279. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  280. MODULE_DESCRIPTION("Freescale i.MX WM8962 ASoC machine driver");
  281. MODULE_LICENSE("GPL v2");
  282. MODULE_ALIAS("platform:imx-wm8962");