apq8016_sbc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_QUA_WS_SLAVE_SEL_10 BIT(17)
  31. #define MIC_CTRL_TLMM_SCLK_EN BIT(1)
  32. #define SPKR_CTL_PRI_WS_SLAVE_SEL_11 (BIT(17) | BIT(16))
  33. static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
  34. {
  35. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  36. struct snd_soc_card *card = rtd->card;
  37. struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
  38. int rval = 0;
  39. switch (cpu_dai->id) {
  40. case MI2S_PRIMARY:
  41. writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
  42. pdata->spkr_iomux);
  43. break;
  44. case MI2S_QUATERNARY:
  45. /* Configure the Quat MI2S to TLMM */
  46. writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
  47. MIC_CTRL_TLMM_SCLK_EN,
  48. pdata->mic_iomux);
  49. break;
  50. default:
  51. dev_err(card->dev, "unsupported cpu dai configuration\n");
  52. rval = -EINVAL;
  53. break;
  54. }
  55. return rval;
  56. }
  57. static struct apq8016_sbc_data *apq8016_sbc_parse_of(struct snd_soc_card *card)
  58. {
  59. struct device *dev = card->dev;
  60. struct snd_soc_dai_link *link;
  61. struct device_node *np, *codec, *cpu, *node = dev->of_node;
  62. struct apq8016_sbc_data *data;
  63. int ret, num_links;
  64. ret = snd_soc_of_parse_card_name(card, "qcom,model");
  65. if (ret) {
  66. dev_err(dev, "Error parsing card name: %d\n", ret);
  67. return ERR_PTR(ret);
  68. }
  69. /* Populate links */
  70. num_links = of_get_child_count(node);
  71. /* Allocate the private data and the DAI link array */
  72. data = devm_kzalloc(dev, sizeof(*data) + sizeof(*link) * num_links,
  73. GFP_KERNEL);
  74. if (!data)
  75. return ERR_PTR(-ENOMEM);
  76. card->dai_link = &data->dai_link[0];
  77. card->num_links = num_links;
  78. link = data->dai_link;
  79. for_each_child_of_node(node, np) {
  80. cpu = of_get_child_by_name(np, "cpu");
  81. codec = of_get_child_by_name(np, "codec");
  82. if (!cpu || !codec) {
  83. dev_err(dev, "Can't find cpu/codec DT node\n");
  84. return ERR_PTR(-EINVAL);
  85. }
  86. link->cpu_of_node = of_parse_phandle(cpu, "sound-dai", 0);
  87. if (!link->cpu_of_node) {
  88. dev_err(card->dev, "error getting cpu phandle\n");
  89. return ERR_PTR(-EINVAL);
  90. }
  91. link->codec_of_node = of_parse_phandle(codec, "sound-dai", 0);
  92. if (!link->codec_of_node) {
  93. dev_err(card->dev, "error getting codec phandle\n");
  94. return ERR_PTR(-EINVAL);
  95. }
  96. ret = snd_soc_of_get_dai_name(cpu, &link->cpu_dai_name);
  97. if (ret) {
  98. dev_err(card->dev, "error getting cpu dai name\n");
  99. return ERR_PTR(ret);
  100. }
  101. ret = snd_soc_of_get_dai_name(codec, &link->codec_dai_name);
  102. if (ret) {
  103. dev_err(card->dev, "error getting codec dai name\n");
  104. return ERR_PTR(ret);
  105. }
  106. link->platform_of_node = link->cpu_of_node;
  107. /* For now we only support playback */
  108. link->playback_only = true;
  109. ret = of_property_read_string(np, "link-name", &link->name);
  110. if (ret) {
  111. dev_err(card->dev, "error getting codec dai_link name\n");
  112. return ERR_PTR(ret);
  113. }
  114. link->stream_name = link->name;
  115. link->init = apq8016_sbc_dai_init;
  116. link++;
  117. }
  118. return data;
  119. }
  120. static int apq8016_sbc_platform_probe(struct platform_device *pdev)
  121. {
  122. struct device *dev = &pdev->dev;
  123. struct snd_soc_card *card;
  124. struct apq8016_sbc_data *data;
  125. struct resource *res;
  126. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  127. if (!card)
  128. return -ENOMEM;
  129. card->dev = dev;
  130. data = apq8016_sbc_parse_of(card);
  131. if (IS_ERR(data)) {
  132. dev_err(&pdev->dev, "Error resolving dai links: %ld\n",
  133. PTR_ERR(data));
  134. return PTR_ERR(data);
  135. }
  136. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux");
  137. data->mic_iomux = devm_ioremap_resource(dev, res);
  138. if (IS_ERR(data->mic_iomux))
  139. return PTR_ERR(data->mic_iomux);
  140. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux");
  141. data->spkr_iomux = devm_ioremap_resource(dev, res);
  142. if (IS_ERR(data->spkr_iomux))
  143. return PTR_ERR(data->spkr_iomux);
  144. platform_set_drvdata(pdev, data);
  145. snd_soc_card_set_drvdata(card, data);
  146. return devm_snd_soc_register_card(&pdev->dev, card);
  147. }
  148. static const struct of_device_id apq8016_sbc_device_id[] = {
  149. { .compatible = "qcom,apq8016-sbc-sndcard" },
  150. {},
  151. };
  152. MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
  153. static struct platform_driver apq8016_sbc_platform_driver = {
  154. .driver = {
  155. .name = "qcom-apq8016-sbc",
  156. .of_match_table = of_match_ptr(apq8016_sbc_device_id),
  157. },
  158. .probe = apq8016_sbc_platform_probe,
  159. };
  160. module_platform_driver(apq8016_sbc_platform_driver);
  161. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
  162. MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
  163. MODULE_LICENSE("GPL v2");