sc7180.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright (c) 2020, The Linux Foundation. All rights reserved.
  4. //
  5. // sc7180.c -- ALSA SoC Machine driver for SC7180
  6. #include <dt-bindings/sound/sc7180-lpass.h>
  7. #include <linux/gpio.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/module.h>
  10. #include <linux/of_device.h>
  11. #include <linux/platform_device.h>
  12. #include <sound/core.h>
  13. #include <sound/jack.h>
  14. #include <sound/pcm.h>
  15. #include <sound/soc.h>
  16. #include <uapi/linux/input-event-codes.h>
  17. #include "../codecs/rt5682.h"
  18. #include "../codecs/rt5682s.h"
  19. #include "common.h"
  20. #include "lpass.h"
  21. #define DEFAULT_MCLK_RATE 19200000
  22. #define RT5682_PLL1_FREQ (48000 * 512)
  23. #define DRIVER_NAME "SC7180"
  24. struct sc7180_snd_data {
  25. struct snd_soc_card card;
  26. u32 pri_mi2s_clk_count;
  27. struct snd_soc_jack hs_jack;
  28. struct snd_soc_jack hdmi_jack;
  29. struct gpio_desc *dmic_sel;
  30. int dmic_switch;
  31. };
  32. static void sc7180_jack_free(struct snd_jack *jack)
  33. {
  34. struct snd_soc_component *component = jack->private_data;
  35. snd_soc_component_set_jack(component, NULL, NULL);
  36. }
  37. static int sc7180_headset_init(struct snd_soc_pcm_runtime *rtd)
  38. {
  39. struct snd_soc_card *card = rtd->card;
  40. struct sc7180_snd_data *pdata = snd_soc_card_get_drvdata(card);
  41. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  42. struct snd_soc_component *component = codec_dai->component;
  43. struct snd_jack *jack;
  44. int rval;
  45. rval = snd_soc_card_jack_new(
  46. card, "Headset Jack",
  47. SND_JACK_HEADSET |
  48. SND_JACK_HEADPHONE |
  49. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  50. SND_JACK_BTN_2 | SND_JACK_BTN_3,
  51. &pdata->hs_jack);
  52. if (rval < 0) {
  53. dev_err(card->dev, "Unable to add Headset Jack\n");
  54. return rval;
  55. }
  56. jack = pdata->hs_jack.jack;
  57. snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
  58. snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
  59. snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
  60. snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
  61. jack->private_data = component;
  62. jack->private_free = sc7180_jack_free;
  63. return snd_soc_component_set_jack(component, &pdata->hs_jack, NULL);
  64. }
  65. static int sc7180_hdmi_init(struct snd_soc_pcm_runtime *rtd)
  66. {
  67. struct snd_soc_card *card = rtd->card;
  68. struct sc7180_snd_data *pdata = snd_soc_card_get_drvdata(card);
  69. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  70. struct snd_soc_component *component = codec_dai->component;
  71. struct snd_jack *jack;
  72. int rval;
  73. rval = snd_soc_card_jack_new(
  74. card, "HDMI Jack",
  75. SND_JACK_LINEOUT,
  76. &pdata->hdmi_jack);
  77. if (rval < 0) {
  78. dev_err(card->dev, "Unable to add HDMI Jack\n");
  79. return rval;
  80. }
  81. jack = pdata->hdmi_jack.jack;
  82. jack->private_data = component;
  83. jack->private_free = sc7180_jack_free;
  84. return snd_soc_component_set_jack(component, &pdata->hdmi_jack, NULL);
  85. }
  86. static int sc7180_init(struct snd_soc_pcm_runtime *rtd)
  87. {
  88. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  89. switch (cpu_dai->id) {
  90. case MI2S_PRIMARY:
  91. return sc7180_headset_init(rtd);
  92. case MI2S_SECONDARY:
  93. return 0;
  94. case LPASS_DP_RX:
  95. return sc7180_hdmi_init(rtd);
  96. default:
  97. dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
  98. cpu_dai->id);
  99. return -EINVAL;
  100. }
  101. return 0;
  102. }
  103. static int sc7180_snd_startup(struct snd_pcm_substream *substream)
  104. {
  105. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  106. struct snd_soc_card *card = rtd->card;
  107. struct sc7180_snd_data *data = snd_soc_card_get_drvdata(card);
  108. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  109. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  110. int pll_id, pll_source, pll_in, pll_out, clk_id, ret;
  111. if (!strcmp(codec_dai->name, "rt5682-aif1")) {
  112. pll_source = RT5682_PLL1_S_MCLK;
  113. pll_id = 0;
  114. clk_id = RT5682_SCLK_S_PLL1;
  115. pll_out = RT5682_PLL1_FREQ;
  116. pll_in = DEFAULT_MCLK_RATE;
  117. } else if (!strcmp(codec_dai->name, "rt5682s-aif1")) {
  118. pll_source = RT5682S_PLL_S_MCLK;
  119. pll_id = RT5682S_PLL2;
  120. clk_id = RT5682S_SCLK_S_PLL2;
  121. pll_out = RT5682_PLL1_FREQ;
  122. pll_in = DEFAULT_MCLK_RATE;
  123. }
  124. switch (cpu_dai->id) {
  125. case MI2S_PRIMARY:
  126. if (++data->pri_mi2s_clk_count == 1) {
  127. snd_soc_dai_set_sysclk(cpu_dai,
  128. LPASS_MCLK0,
  129. DEFAULT_MCLK_RATE,
  130. SNDRV_PCM_STREAM_PLAYBACK);
  131. }
  132. snd_soc_dai_set_fmt(codec_dai,
  133. SND_SOC_DAIFMT_BC_FC |
  134. SND_SOC_DAIFMT_NB_NF |
  135. SND_SOC_DAIFMT_I2S);
  136. /* Configure PLL1 for codec */
  137. ret = snd_soc_dai_set_pll(codec_dai, pll_id, pll_source,
  138. pll_in, pll_out);
  139. if (ret) {
  140. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  141. return ret;
  142. }
  143. /* Configure sysclk for codec */
  144. ret = snd_soc_dai_set_sysclk(codec_dai, clk_id, pll_out,
  145. SND_SOC_CLOCK_IN);
  146. if (ret)
  147. dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n",
  148. ret);
  149. break;
  150. case MI2S_SECONDARY:
  151. break;
  152. case LPASS_DP_RX:
  153. break;
  154. default:
  155. dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
  156. cpu_dai->id);
  157. return -EINVAL;
  158. }
  159. return 0;
  160. }
  161. static int dmic_get(struct snd_kcontrol *kcontrol,
  162. struct snd_ctl_elem_value *ucontrol)
  163. {
  164. struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
  165. struct sc7180_snd_data *data = snd_soc_card_get_drvdata(dapm->card);
  166. ucontrol->value.integer.value[0] = data->dmic_switch;
  167. return 0;
  168. }
  169. static int dmic_set(struct snd_kcontrol *kcontrol,
  170. struct snd_ctl_elem_value *ucontrol)
  171. {
  172. struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
  173. struct sc7180_snd_data *data = snd_soc_card_get_drvdata(dapm->card);
  174. data->dmic_switch = ucontrol->value.integer.value[0];
  175. gpiod_set_value(data->dmic_sel, data->dmic_switch);
  176. return 0;
  177. }
  178. static void sc7180_snd_shutdown(struct snd_pcm_substream *substream)
  179. {
  180. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  181. struct snd_soc_card *card = rtd->card;
  182. struct sc7180_snd_data *data = snd_soc_card_get_drvdata(card);
  183. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  184. switch (cpu_dai->id) {
  185. case MI2S_PRIMARY:
  186. if (--data->pri_mi2s_clk_count == 0) {
  187. snd_soc_dai_set_sysclk(cpu_dai,
  188. LPASS_MCLK0,
  189. 0,
  190. SNDRV_PCM_STREAM_PLAYBACK);
  191. }
  192. break;
  193. case MI2S_SECONDARY:
  194. break;
  195. case LPASS_DP_RX:
  196. break;
  197. default:
  198. dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
  199. cpu_dai->id);
  200. break;
  201. }
  202. }
  203. static int sc7180_adau7002_init(struct snd_soc_pcm_runtime *rtd)
  204. {
  205. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  206. switch (cpu_dai->id) {
  207. case MI2S_PRIMARY:
  208. return 0;
  209. case MI2S_SECONDARY:
  210. return 0;
  211. case LPASS_DP_RX:
  212. return sc7180_hdmi_init(rtd);
  213. default:
  214. dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
  215. cpu_dai->id);
  216. return -EINVAL;
  217. }
  218. return 0;
  219. }
  220. static int sc7180_adau7002_snd_startup(struct snd_pcm_substream *substream)
  221. {
  222. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  223. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  224. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  225. struct snd_pcm_runtime *runtime = substream->runtime;
  226. switch (cpu_dai->id) {
  227. case MI2S_PRIMARY:
  228. snd_soc_dai_set_fmt(codec_dai,
  229. SND_SOC_DAIFMT_CBS_CFS |
  230. SND_SOC_DAIFMT_NB_NF |
  231. SND_SOC_DAIFMT_I2S);
  232. runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
  233. snd_pcm_hw_constraint_msbits(runtime, 0, 32, 32);
  234. break;
  235. case MI2S_SECONDARY:
  236. break;
  237. case LPASS_DP_RX:
  238. break;
  239. default:
  240. dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
  241. cpu_dai->id);
  242. return -EINVAL;
  243. }
  244. return 0;
  245. }
  246. static const struct snd_soc_ops sc7180_ops = {
  247. .startup = sc7180_snd_startup,
  248. .shutdown = sc7180_snd_shutdown,
  249. };
  250. static const struct snd_soc_ops sc7180_adau7002_ops = {
  251. .startup = sc7180_adau7002_snd_startup,
  252. };
  253. static const struct snd_soc_dapm_widget sc7180_snd_widgets[] = {
  254. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  255. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  256. };
  257. static const struct snd_soc_dapm_widget sc7180_adau7002_snd_widgets[] = {
  258. SND_SOC_DAPM_MIC("DMIC", NULL),
  259. };
  260. static const char * const dmic_mux_text[] = {
  261. "Front Mic",
  262. "Rear Mic",
  263. };
  264. static SOC_ENUM_SINGLE_DECL(sc7180_dmic_enum,
  265. SND_SOC_NOPM, 0, dmic_mux_text);
  266. static const struct snd_kcontrol_new sc7180_dmic_mux_control =
  267. SOC_DAPM_ENUM_EXT("DMIC Select Mux", sc7180_dmic_enum,
  268. dmic_get, dmic_set);
  269. static const struct snd_soc_dapm_widget sc7180_snd_dual_mic_widgets[] = {
  270. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  271. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  272. SND_SOC_DAPM_MIC("DMIC", NULL),
  273. SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0, &sc7180_dmic_mux_control),
  274. };
  275. static const struct snd_soc_dapm_route sc7180_snd_dual_mic_audio_route[] = {
  276. {"Dmic Mux", "Front Mic", "DMIC"},
  277. {"Dmic Mux", "Rear Mic", "DMIC"},
  278. };
  279. static int sc7180_snd_platform_probe(struct platform_device *pdev)
  280. {
  281. struct snd_soc_card *card;
  282. struct sc7180_snd_data *data;
  283. struct device *dev = &pdev->dev;
  284. struct snd_soc_dai_link *link;
  285. int ret;
  286. int i;
  287. bool no_headphone = false;
  288. /* Allocate the private data */
  289. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  290. if (!data)
  291. return -ENOMEM;
  292. card = &data->card;
  293. snd_soc_card_set_drvdata(card, data);
  294. card->owner = THIS_MODULE;
  295. card->driver_name = DRIVER_NAME;
  296. card->dev = dev;
  297. card->dapm_widgets = sc7180_snd_widgets;
  298. card->num_dapm_widgets = ARRAY_SIZE(sc7180_snd_widgets);
  299. if (of_property_read_bool(dev->of_node, "dmic-gpios")) {
  300. card->dapm_widgets = sc7180_snd_dual_mic_widgets,
  301. card->num_dapm_widgets = ARRAY_SIZE(sc7180_snd_dual_mic_widgets),
  302. card->dapm_routes = sc7180_snd_dual_mic_audio_route,
  303. card->num_dapm_routes = ARRAY_SIZE(sc7180_snd_dual_mic_audio_route),
  304. data->dmic_sel = devm_gpiod_get(&pdev->dev, "dmic", GPIOD_OUT_LOW);
  305. if (IS_ERR(data->dmic_sel)) {
  306. dev_err(&pdev->dev, "DMIC gpio failed err=%ld\n", PTR_ERR(data->dmic_sel));
  307. return PTR_ERR(data->dmic_sel);
  308. }
  309. }
  310. if (of_device_is_compatible(dev->of_node, "google,sc7180-coachz")) {
  311. no_headphone = true;
  312. card->dapm_widgets = sc7180_adau7002_snd_widgets;
  313. card->num_dapm_widgets = ARRAY_SIZE(sc7180_adau7002_snd_widgets);
  314. }
  315. ret = qcom_snd_parse_of(card);
  316. if (ret)
  317. return ret;
  318. for_each_card_prelinks(card, i, link) {
  319. if (no_headphone) {
  320. link->ops = &sc7180_adau7002_ops;
  321. link->init = sc7180_adau7002_init;
  322. } else {
  323. link->ops = &sc7180_ops;
  324. link->init = sc7180_init;
  325. }
  326. }
  327. return devm_snd_soc_register_card(dev, card);
  328. }
  329. static const struct of_device_id sc7180_snd_device_id[] = {
  330. {.compatible = "google,sc7180-trogdor"},
  331. {.compatible = "google,sc7180-coachz"},
  332. {},
  333. };
  334. MODULE_DEVICE_TABLE(of, sc7180_snd_device_id);
  335. static struct platform_driver sc7180_snd_driver = {
  336. .probe = sc7180_snd_platform_probe,
  337. .driver = {
  338. .name = "msm-snd-sc7180",
  339. .of_match_table = sc7180_snd_device_id,
  340. .pm = &snd_soc_pm_ops,
  341. },
  342. };
  343. module_platform_driver(sc7180_snd_driver);
  344. MODULE_DESCRIPTION("sc7180 ASoC Machine Driver");
  345. MODULE_LICENSE("GPL v2");