apq8016_sbc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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/jack.h>
  24. #include <sound/soc.h>
  25. #include <uapi/linux/input-event-codes.h>
  26. #include <dt-bindings/sound/apq8016-lpass.h>
  27. struct apq8016_sbc_data {
  28. void __iomem *mic_iomux;
  29. void __iomem *spkr_iomux;
  30. struct snd_soc_jack jack;
  31. bool jack_setup;
  32. struct snd_soc_dai_link dai_link[]; /* dynamically allocated */
  33. };
  34. #define MIC_CTRL_TER_WS_SLAVE_SEL BIT(21)
  35. #define MIC_CTRL_QUA_WS_SLAVE_SEL_10 BIT(17)
  36. #define MIC_CTRL_TLMM_SCLK_EN BIT(1)
  37. #define SPKR_CTL_PRI_WS_SLAVE_SEL_11 (BIT(17) | BIT(16))
  38. #define DEFAULT_MCLK_RATE 9600000
  39. static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
  40. {
  41. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  42. struct snd_soc_component *component;
  43. struct snd_soc_dai_link *dai_link = rtd->dai_link;
  44. struct snd_soc_card *card = rtd->card;
  45. struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
  46. int i, rval;
  47. switch (cpu_dai->id) {
  48. case MI2S_PRIMARY:
  49. writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
  50. pdata->spkr_iomux);
  51. break;
  52. case MI2S_QUATERNARY:
  53. /* Configure the Quat MI2S to TLMM */
  54. writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
  55. MIC_CTRL_TLMM_SCLK_EN,
  56. pdata->mic_iomux);
  57. break;
  58. case MI2S_TERTIARY:
  59. writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
  60. MIC_CTRL_TLMM_SCLK_EN,
  61. pdata->mic_iomux);
  62. break;
  63. default:
  64. dev_err(card->dev, "unsupported cpu dai configuration\n");
  65. return -EINVAL;
  66. }
  67. if (!pdata->jack_setup) {
  68. struct snd_jack *jack;
  69. rval = snd_soc_card_jack_new(card, "Headset Jack",
  70. SND_JACK_HEADSET |
  71. SND_JACK_HEADPHONE |
  72. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  73. SND_JACK_BTN_2 | SND_JACK_BTN_3 |
  74. SND_JACK_BTN_4,
  75. &pdata->jack, NULL, 0);
  76. if (rval < 0) {
  77. dev_err(card->dev, "Unable to add Headphone Jack\n");
  78. return rval;
  79. }
  80. jack = pdata->jack.jack;
  81. snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
  82. snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
  83. snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
  84. snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
  85. pdata->jack_setup = true;
  86. }
  87. for (i = 0 ; i < dai_link->num_codecs; i++) {
  88. struct snd_soc_dai *dai = rtd->codec_dais[i];
  89. component = dai->component;
  90. /* Set default mclk for internal codec */
  91. rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE,
  92. SND_SOC_CLOCK_IN);
  93. if (rval != 0 && rval != -ENOTSUPP) {
  94. dev_warn(card->dev, "Failed to set mclk: %d\n", rval);
  95. return rval;
  96. }
  97. rval = snd_soc_component_set_jack(component, &pdata->jack, NULL);
  98. if (rval != 0 && rval != -ENOTSUPP) {
  99. dev_warn(card->dev, "Failed to set jack: %d\n", rval);
  100. return rval;
  101. }
  102. }
  103. return 0;
  104. }
  105. static struct apq8016_sbc_data *apq8016_sbc_parse_of(struct snd_soc_card *card)
  106. {
  107. struct device *dev = card->dev;
  108. struct snd_soc_dai_link *link;
  109. struct device_node *np, *codec, *cpu, *node = dev->of_node;
  110. struct apq8016_sbc_data *data;
  111. int ret, num_links;
  112. ret = snd_soc_of_parse_card_name(card, "qcom,model");
  113. if (ret) {
  114. dev_err(dev, "Error parsing card name: %d\n", ret);
  115. return ERR_PTR(ret);
  116. }
  117. /* DAPM routes */
  118. if (of_property_read_bool(node, "qcom,audio-routing")) {
  119. ret = snd_soc_of_parse_audio_routing(card,
  120. "qcom,audio-routing");
  121. if (ret)
  122. return ERR_PTR(ret);
  123. }
  124. /* Populate links */
  125. num_links = of_get_child_count(node);
  126. /* Allocate the private data and the DAI link array */
  127. data = devm_kzalloc(dev,
  128. struct_size(data, dai_link, num_links),
  129. GFP_KERNEL);
  130. if (!data)
  131. return ERR_PTR(-ENOMEM);
  132. card->dai_link = &data->dai_link[0];
  133. card->num_links = num_links;
  134. link = data->dai_link;
  135. for_each_child_of_node(node, np) {
  136. cpu = of_get_child_by_name(np, "cpu");
  137. codec = of_get_child_by_name(np, "codec");
  138. if (!cpu || !codec) {
  139. dev_err(dev, "Can't find cpu/codec DT node\n");
  140. ret = -EINVAL;
  141. goto error;
  142. }
  143. link->cpu_of_node = of_parse_phandle(cpu, "sound-dai", 0);
  144. if (!link->cpu_of_node) {
  145. dev_err(card->dev, "error getting cpu phandle\n");
  146. ret = -EINVAL;
  147. goto error;
  148. }
  149. ret = snd_soc_of_get_dai_name(cpu, &link->cpu_dai_name);
  150. if (ret) {
  151. dev_err(card->dev, "error getting cpu dai name\n");
  152. goto error;
  153. }
  154. ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
  155. if (ret < 0) {
  156. dev_err(card->dev, "error getting codec dai name\n");
  157. goto error;
  158. }
  159. link->platform_of_node = link->cpu_of_node;
  160. ret = of_property_read_string(np, "link-name", &link->name);
  161. if (ret) {
  162. dev_err(card->dev, "error getting codec dai_link name\n");
  163. goto error;
  164. }
  165. link->stream_name = link->name;
  166. link->init = apq8016_sbc_dai_init;
  167. link++;
  168. of_node_put(cpu);
  169. of_node_put(codec);
  170. }
  171. return data;
  172. error:
  173. of_node_put(np);
  174. of_node_put(cpu);
  175. of_node_put(codec);
  176. return ERR_PTR(ret);
  177. }
  178. static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
  179. SND_SOC_DAPM_MIC("Handset Mic", NULL),
  180. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  181. SND_SOC_DAPM_MIC("Secondary Mic", NULL),
  182. SND_SOC_DAPM_MIC("Digital Mic1", NULL),
  183. SND_SOC_DAPM_MIC("Digital Mic2", NULL),
  184. };
  185. static int apq8016_sbc_platform_probe(struct platform_device *pdev)
  186. {
  187. struct device *dev = &pdev->dev;
  188. struct snd_soc_card *card;
  189. struct apq8016_sbc_data *data;
  190. struct resource *res;
  191. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  192. if (!card)
  193. return -ENOMEM;
  194. card->dev = dev;
  195. card->owner = THIS_MODULE;
  196. card->dapm_widgets = apq8016_sbc_dapm_widgets;
  197. card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
  198. data = apq8016_sbc_parse_of(card);
  199. if (IS_ERR(data)) {
  200. dev_err(&pdev->dev, "Error resolving dai links: %ld\n",
  201. PTR_ERR(data));
  202. return PTR_ERR(data);
  203. }
  204. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux");
  205. data->mic_iomux = devm_ioremap_resource(dev, res);
  206. if (IS_ERR(data->mic_iomux))
  207. return PTR_ERR(data->mic_iomux);
  208. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux");
  209. data->spkr_iomux = devm_ioremap_resource(dev, res);
  210. if (IS_ERR(data->spkr_iomux))
  211. return PTR_ERR(data->spkr_iomux);
  212. snd_soc_card_set_drvdata(card, data);
  213. return devm_snd_soc_register_card(&pdev->dev, card);
  214. }
  215. static const struct of_device_id apq8016_sbc_device_id[] = {
  216. { .compatible = "qcom,apq8016-sbc-sndcard" },
  217. {},
  218. };
  219. MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
  220. static struct platform_driver apq8016_sbc_platform_driver = {
  221. .driver = {
  222. .name = "qcom-apq8016-sbc",
  223. .of_match_table = of_match_ptr(apq8016_sbc_device_id),
  224. },
  225. .probe = apq8016_sbc_platform_probe,
  226. };
  227. module_platform_driver(apq8016_sbc_platform_driver);
  228. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
  229. MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
  230. MODULE_LICENSE("GPL v2");