apq8016_sbc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2015 The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/device.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/io.h>
  18. #include <linux/of.h>
  19. #include <linux/clk.h>
  20. #include <linux/platform_device.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <dt-bindings/sound/apq8016-lpass.h>
  25. struct apq8016_sbc_data {
  26. void __iomem *mic_iomux;
  27. void __iomem *spkr_iomux;
  28. struct snd_soc_dai_link dai_link[]; /* dynamically allocated */
  29. };
  30. #define MIC_CTRL_TER_WS_SLAVE_SEL BIT(21)
  31. #define MIC_CTRL_QUA_WS_SLAVE_SEL_10 BIT(17)
  32. #define MIC_CTRL_TLMM_SCLK_EN BIT(1)
  33. #define SPKR_CTL_PRI_WS_SLAVE_SEL_11 (BIT(17) | BIT(16))
  34. static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
  35. {
  36. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  37. struct snd_soc_card *card = rtd->card;
  38. struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
  39. int rval = 0;
  40. switch (cpu_dai->id) {
  41. case MI2S_PRIMARY:
  42. writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
  43. pdata->spkr_iomux);
  44. break;
  45. case MI2S_QUATERNARY:
  46. /* Configure the Quat MI2S to TLMM */
  47. writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
  48. MIC_CTRL_TLMM_SCLK_EN,
  49. pdata->mic_iomux);
  50. break;
  51. case MI2S_TERTIARY:
  52. writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
  53. MIC_CTRL_TLMM_SCLK_EN,
  54. pdata->mic_iomux);
  55. break;
  56. default:
  57. dev_err(card->dev, "unsupported cpu dai configuration\n");
  58. rval = -EINVAL;
  59. break;
  60. }
  61. return rval;
  62. }
  63. static struct apq8016_sbc_data *apq8016_sbc_parse_of(struct snd_soc_card *card)
  64. {
  65. struct device *dev = card->dev;
  66. struct snd_soc_dai_link *link;
  67. struct device_node *np, *codec, *cpu, *node = dev->of_node;
  68. struct apq8016_sbc_data *data;
  69. int ret, num_links;
  70. ret = snd_soc_of_parse_card_name(card, "qcom,model");
  71. if (ret) {
  72. dev_err(dev, "Error parsing card name: %d\n", ret);
  73. return ERR_PTR(ret);
  74. }
  75. /* DAPM routes */
  76. if (of_property_read_bool(node, "qcom,audio-routing")) {
  77. ret = snd_soc_of_parse_audio_routing(card,
  78. "qcom,audio-routing");
  79. if (ret)
  80. return ERR_PTR(ret);
  81. }
  82. /* Populate links */
  83. num_links = of_get_child_count(node);
  84. /* Allocate the private data and the DAI link array */
  85. data = devm_kzalloc(dev, sizeof(*data) + sizeof(*link) * num_links,
  86. GFP_KERNEL);
  87. if (!data)
  88. return ERR_PTR(-ENOMEM);
  89. card->dai_link = &data->dai_link[0];
  90. card->num_links = num_links;
  91. link = data->dai_link;
  92. for_each_child_of_node(node, np) {
  93. cpu = of_get_child_by_name(np, "cpu");
  94. codec = of_get_child_by_name(np, "codec");
  95. if (!cpu || !codec) {
  96. dev_err(dev, "Can't find cpu/codec DT node\n");
  97. return ERR_PTR(-EINVAL);
  98. }
  99. link->cpu_of_node = of_parse_phandle(cpu, "sound-dai", 0);
  100. if (!link->cpu_of_node) {
  101. dev_err(card->dev, "error getting cpu phandle\n");
  102. return ERR_PTR(-EINVAL);
  103. }
  104. link->codec_of_node = of_parse_phandle(codec, "sound-dai", 0);
  105. if (!link->codec_of_node) {
  106. dev_err(card->dev, "error getting codec phandle\n");
  107. return ERR_PTR(-EINVAL);
  108. }
  109. ret = snd_soc_of_get_dai_name(cpu, &link->cpu_dai_name);
  110. if (ret) {
  111. dev_err(card->dev, "error getting cpu dai name\n");
  112. return ERR_PTR(ret);
  113. }
  114. ret = snd_soc_of_get_dai_name(codec, &link->codec_dai_name);
  115. if (ret) {
  116. dev_err(card->dev, "error getting codec dai name\n");
  117. return ERR_PTR(ret);
  118. }
  119. link->platform_of_node = link->cpu_of_node;
  120. ret = of_property_read_string(np, "link-name", &link->name);
  121. if (ret) {
  122. dev_err(card->dev, "error getting codec dai_link name\n");
  123. return ERR_PTR(ret);
  124. }
  125. link->stream_name = link->name;
  126. link->init = apq8016_sbc_dai_init;
  127. link++;
  128. }
  129. return data;
  130. }
  131. static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
  132. SND_SOC_DAPM_MIC("Handset Mic", NULL),
  133. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  134. SND_SOC_DAPM_MIC("Secondary Mic", NULL),
  135. SND_SOC_DAPM_MIC("Digital Mic1", NULL),
  136. SND_SOC_DAPM_MIC("Digital Mic2", NULL),
  137. };
  138. static int apq8016_sbc_platform_probe(struct platform_device *pdev)
  139. {
  140. struct device *dev = &pdev->dev;
  141. struct snd_soc_card *card;
  142. struct apq8016_sbc_data *data;
  143. struct resource *res;
  144. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  145. if (!card)
  146. return -ENOMEM;
  147. card->dev = dev;
  148. card->dapm_widgets = apq8016_sbc_dapm_widgets;
  149. card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
  150. data = apq8016_sbc_parse_of(card);
  151. if (IS_ERR(data)) {
  152. dev_err(&pdev->dev, "Error resolving dai links: %ld\n",
  153. PTR_ERR(data));
  154. return PTR_ERR(data);
  155. }
  156. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux");
  157. data->mic_iomux = devm_ioremap_resource(dev, res);
  158. if (IS_ERR(data->mic_iomux))
  159. return PTR_ERR(data->mic_iomux);
  160. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux");
  161. data->spkr_iomux = devm_ioremap_resource(dev, res);
  162. if (IS_ERR(data->spkr_iomux))
  163. return PTR_ERR(data->spkr_iomux);
  164. platform_set_drvdata(pdev, data);
  165. snd_soc_card_set_drvdata(card, data);
  166. return devm_snd_soc_register_card(&pdev->dev, card);
  167. }
  168. static const struct of_device_id apq8016_sbc_device_id[] = {
  169. { .compatible = "qcom,apq8016-sbc-sndcard" },
  170. {},
  171. };
  172. MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
  173. static struct platform_driver apq8016_sbc_platform_driver = {
  174. .driver = {
  175. .name = "qcom-apq8016-sbc",
  176. .of_match_table = of_match_ptr(apq8016_sbc_device_id),
  177. },
  178. .probe = apq8016_sbc_platform_probe,
  179. };
  180. module_platform_driver(apq8016_sbc_platform_driver);
  181. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
  182. MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
  183. MODULE_LICENSE("GPL v2");