apq8016_sbc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015 The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/io.h>
  9. #include <linux/of.h>
  10. #include <linux/clk.h>
  11. #include <linux/platform_device.h>
  12. #include <sound/pcm.h>
  13. #include <sound/pcm_params.h>
  14. #include <sound/jack.h>
  15. #include <sound/soc.h>
  16. #include <uapi/linux/input-event-codes.h>
  17. #include <dt-bindings/sound/apq8016-lpass.h>
  18. #include "common.h"
  19. #include "qdsp6/q6afe.h"
  20. #define MI2S_COUNT (MI2S_QUATERNARY + 1)
  21. struct apq8016_sbc_data {
  22. struct snd_soc_card card;
  23. void __iomem *mic_iomux;
  24. void __iomem *spkr_iomux;
  25. struct snd_soc_jack jack;
  26. bool jack_setup;
  27. int mi2s_clk_count[MI2S_COUNT];
  28. };
  29. #define MIC_CTRL_TER_WS_SLAVE_SEL BIT(21)
  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. #define SPKR_CTL_TLMM_MCLK_EN BIT(1)
  34. #define SPKR_CTL_TLMM_SCLK_EN BIT(2)
  35. #define SPKR_CTL_TLMM_DATA1_EN BIT(3)
  36. #define SPKR_CTL_TLMM_WS_OUT_SEL_MASK GENMASK(7, 6)
  37. #define SPKR_CTL_TLMM_WS_OUT_SEL_SEC BIT(6)
  38. #define SPKR_CTL_TLMM_WS_EN_SEL_MASK GENMASK(19, 18)
  39. #define SPKR_CTL_TLMM_WS_EN_SEL_SEC BIT(18)
  40. #define DEFAULT_MCLK_RATE 9600000
  41. #define MI2S_BCLK_RATE 1536000
  42. static int apq8016_dai_init(struct snd_soc_pcm_runtime *rtd, int mi2s)
  43. {
  44. struct snd_soc_dai *codec_dai;
  45. struct snd_soc_component *component;
  46. struct snd_soc_card *card = rtd->card;
  47. struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
  48. int i, rval;
  49. u32 value;
  50. switch (mi2s) {
  51. case MI2S_PRIMARY:
  52. writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
  53. pdata->spkr_iomux);
  54. break;
  55. case MI2S_QUATERNARY:
  56. /* Configure the Quat MI2S to TLMM */
  57. writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
  58. MIC_CTRL_TLMM_SCLK_EN,
  59. pdata->mic_iomux);
  60. break;
  61. case MI2S_SECONDARY:
  62. /* Clear TLMM_WS_OUT_SEL and TLMM_WS_EN_SEL fields */
  63. value = readl(pdata->spkr_iomux) &
  64. ~(SPKR_CTL_TLMM_WS_OUT_SEL_MASK | SPKR_CTL_TLMM_WS_EN_SEL_MASK);
  65. /* Configure the Sec MI2S to TLMM */
  66. writel(value | SPKR_CTL_TLMM_MCLK_EN | SPKR_CTL_TLMM_SCLK_EN |
  67. SPKR_CTL_TLMM_DATA1_EN | SPKR_CTL_TLMM_WS_OUT_SEL_SEC |
  68. SPKR_CTL_TLMM_WS_EN_SEL_SEC, pdata->spkr_iomux);
  69. break;
  70. case MI2S_TERTIARY:
  71. writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
  72. MIC_CTRL_TLMM_SCLK_EN,
  73. pdata->mic_iomux);
  74. break;
  75. default:
  76. dev_err(card->dev, "unsupported cpu dai configuration\n");
  77. return -EINVAL;
  78. }
  79. if (!pdata->jack_setup) {
  80. struct snd_jack *jack;
  81. rval = snd_soc_card_jack_new(card, "Headset Jack",
  82. SND_JACK_HEADSET |
  83. SND_JACK_HEADPHONE |
  84. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  85. SND_JACK_BTN_2 | SND_JACK_BTN_3 |
  86. SND_JACK_BTN_4,
  87. &pdata->jack);
  88. if (rval < 0) {
  89. dev_err(card->dev, "Unable to add Headphone Jack\n");
  90. return rval;
  91. }
  92. jack = pdata->jack.jack;
  93. snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
  94. snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
  95. snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
  96. snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
  97. pdata->jack_setup = true;
  98. }
  99. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  100. component = codec_dai->component;
  101. /* Set default mclk for internal codec */
  102. rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE,
  103. SND_SOC_CLOCK_IN);
  104. if (rval != 0 && rval != -ENOTSUPP) {
  105. dev_warn(card->dev, "Failed to set mclk: %d\n", rval);
  106. return rval;
  107. }
  108. rval = snd_soc_component_set_jack(component, &pdata->jack, NULL);
  109. if (rval != 0 && rval != -ENOTSUPP) {
  110. dev_warn(card->dev, "Failed to set jack: %d\n", rval);
  111. return rval;
  112. }
  113. }
  114. return 0;
  115. }
  116. static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
  117. {
  118. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  119. return apq8016_dai_init(rtd, cpu_dai->id);
  120. }
  121. static void apq8016_sbc_add_ops(struct snd_soc_card *card)
  122. {
  123. struct snd_soc_dai_link *link;
  124. int i;
  125. for_each_card_prelinks(card, i, link)
  126. link->init = apq8016_sbc_dai_init;
  127. }
  128. static int qdsp6_dai_get_lpass_id(struct snd_soc_dai *cpu_dai)
  129. {
  130. switch (cpu_dai->id) {
  131. case PRIMARY_MI2S_RX:
  132. case PRIMARY_MI2S_TX:
  133. return MI2S_PRIMARY;
  134. case SECONDARY_MI2S_RX:
  135. case SECONDARY_MI2S_TX:
  136. return MI2S_SECONDARY;
  137. case TERTIARY_MI2S_RX:
  138. case TERTIARY_MI2S_TX:
  139. return MI2S_TERTIARY;
  140. case QUATERNARY_MI2S_RX:
  141. case QUATERNARY_MI2S_TX:
  142. return MI2S_QUATERNARY;
  143. default:
  144. return -EINVAL;
  145. }
  146. }
  147. static int msm8916_qdsp6_dai_init(struct snd_soc_pcm_runtime *rtd)
  148. {
  149. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  150. snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_BP_FP);
  151. return apq8016_dai_init(rtd, qdsp6_dai_get_lpass_id(cpu_dai));
  152. }
  153. static int msm8916_qdsp6_startup(struct snd_pcm_substream *substream)
  154. {
  155. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  156. struct snd_soc_card *card = rtd->card;
  157. struct apq8016_sbc_data *data = snd_soc_card_get_drvdata(card);
  158. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  159. int mi2s, ret;
  160. mi2s = qdsp6_dai_get_lpass_id(cpu_dai);
  161. if (mi2s < 0)
  162. return mi2s;
  163. if (++data->mi2s_clk_count[mi2s] > 1)
  164. return 0;
  165. ret = snd_soc_dai_set_sysclk(cpu_dai, LPAIF_BIT_CLK, MI2S_BCLK_RATE, 0);
  166. if (ret)
  167. dev_err(card->dev, "Failed to enable LPAIF bit clk: %d\n", ret);
  168. return ret;
  169. }
  170. static void msm8916_qdsp6_shutdown(struct snd_pcm_substream *substream)
  171. {
  172. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  173. struct snd_soc_card *card = rtd->card;
  174. struct apq8016_sbc_data *data = snd_soc_card_get_drvdata(card);
  175. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  176. int mi2s, ret;
  177. mi2s = qdsp6_dai_get_lpass_id(cpu_dai);
  178. if (mi2s < 0)
  179. return;
  180. if (--data->mi2s_clk_count[mi2s] > 0)
  181. return;
  182. ret = snd_soc_dai_set_sysclk(cpu_dai, LPAIF_BIT_CLK, 0, 0);
  183. if (ret)
  184. dev_err(card->dev, "Failed to disable LPAIF bit clk: %d\n", ret);
  185. }
  186. static const struct snd_soc_ops msm8916_qdsp6_be_ops = {
  187. .startup = msm8916_qdsp6_startup,
  188. .shutdown = msm8916_qdsp6_shutdown,
  189. };
  190. static int msm8916_qdsp6_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
  191. struct snd_pcm_hw_params *params)
  192. {
  193. struct snd_interval *rate = hw_param_interval(params,
  194. SNDRV_PCM_HW_PARAM_RATE);
  195. struct snd_interval *channels = hw_param_interval(params,
  196. SNDRV_PCM_HW_PARAM_CHANNELS);
  197. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  198. rate->min = rate->max = 48000;
  199. channels->min = channels->max = 2;
  200. snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
  201. return 0;
  202. }
  203. static void msm8916_qdsp6_add_ops(struct snd_soc_card *card)
  204. {
  205. struct snd_soc_dai_link *link;
  206. int i;
  207. /* Make it obvious to userspace that QDSP6 is used */
  208. card->components = "qdsp6";
  209. for_each_card_prelinks(card, i, link) {
  210. if (link->no_pcm) {
  211. link->init = msm8916_qdsp6_dai_init;
  212. link->ops = &msm8916_qdsp6_be_ops;
  213. link->be_hw_params_fixup = msm8916_qdsp6_be_hw_params_fixup;
  214. }
  215. }
  216. }
  217. static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
  218. SND_SOC_DAPM_MIC("Handset Mic", NULL),
  219. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  220. SND_SOC_DAPM_MIC("Secondary Mic", NULL),
  221. SND_SOC_DAPM_MIC("Digital Mic1", NULL),
  222. SND_SOC_DAPM_MIC("Digital Mic2", NULL),
  223. };
  224. static int apq8016_sbc_platform_probe(struct platform_device *pdev)
  225. {
  226. void (*add_ops)(struct snd_soc_card *card);
  227. struct device *dev = &pdev->dev;
  228. struct snd_soc_card *card;
  229. struct apq8016_sbc_data *data;
  230. int ret;
  231. add_ops = device_get_match_data(&pdev->dev);
  232. if (!add_ops)
  233. return -EINVAL;
  234. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  235. if (!data)
  236. return -ENOMEM;
  237. card = &data->card;
  238. card->dev = dev;
  239. card->owner = THIS_MODULE;
  240. card->dapm_widgets = apq8016_sbc_dapm_widgets;
  241. card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
  242. ret = qcom_snd_parse_of(card);
  243. if (ret)
  244. return ret;
  245. data->mic_iomux = devm_platform_ioremap_resource_byname(pdev, "mic-iomux");
  246. if (IS_ERR(data->mic_iomux))
  247. return PTR_ERR(data->mic_iomux);
  248. data->spkr_iomux = devm_platform_ioremap_resource_byname(pdev, "spkr-iomux");
  249. if (IS_ERR(data->spkr_iomux))
  250. return PTR_ERR(data->spkr_iomux);
  251. snd_soc_card_set_drvdata(card, data);
  252. add_ops(card);
  253. return devm_snd_soc_register_card(&pdev->dev, card);
  254. }
  255. static const struct of_device_id apq8016_sbc_device_id[] __maybe_unused = {
  256. { .compatible = "qcom,apq8016-sbc-sndcard", .data = apq8016_sbc_add_ops },
  257. { .compatible = "qcom,msm8916-qdsp6-sndcard", .data = msm8916_qdsp6_add_ops },
  258. {},
  259. };
  260. MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
  261. static struct platform_driver apq8016_sbc_platform_driver = {
  262. .driver = {
  263. .name = "qcom-apq8016-sbc",
  264. .of_match_table = of_match_ptr(apq8016_sbc_device_id),
  265. },
  266. .probe = apq8016_sbc_platform_probe,
  267. };
  268. module_platform_driver(apq8016_sbc_platform_driver);
  269. MODULE_AUTHOR("Srinivas Kandagatla <[email protected]");
  270. MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
  271. MODULE_LICENSE("GPL v2");