1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020 Intel Corporation
3 
4 /*
5  *  sof_sdw_rt700 - Helpers to handle RT700 from generic machine driver
6  */
7 
8 #include <linux/device.h>
9 #include <linux/errno.h>
10 #include <linux/input.h>
11 #include <sound/control.h>
12 #include <sound/soc.h>
13 #include <sound/soc-acpi.h>
14 #include <sound/soc-dapm.h>
15 #include <sound/jack.h>
16 #include "sof_sdw_common.h"
17 
18 static const struct snd_soc_dapm_widget rt700_widgets[] = {
19 	SND_SOC_DAPM_HP("Headphones", NULL),
20 	SND_SOC_DAPM_MIC("AMIC", NULL),
21 	SND_SOC_DAPM_SPK("Speaker", NULL),
22 };
23 
24 static const struct snd_soc_dapm_route rt700_map[] = {
25 	/* Headphones */
26 	{ "Headphones", NULL, "rt700 HP" },
27 	{ "Speaker", NULL, "rt700 SPK" },
28 	{ "rt700 MIC2", NULL, "AMIC" },
29 };
30 
31 static const struct snd_kcontrol_new rt700_controls[] = {
32 	SOC_DAPM_PIN_SWITCH("Headphones"),
33 	SOC_DAPM_PIN_SWITCH("AMIC"),
34 	SOC_DAPM_PIN_SWITCH("Speaker"),
35 };
36 
37 static struct snd_soc_jack_pin rt700_jack_pins[] = {
38 	{
39 		.pin    = "Headphones",
40 		.mask   = SND_JACK_HEADPHONE,
41 	},
42 	{
43 		.pin    = "AMIC",
44 		.mask   = SND_JACK_MICROPHONE,
45 	},
46 };
47 
48 static int rt700_rtd_init(struct snd_soc_pcm_runtime *rtd)
49 {
50 	struct snd_soc_card *card = rtd->card;
51 	struct mc_private *ctx = snd_soc_card_get_drvdata(card);
52 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
53 	struct snd_soc_component *component = codec_dai->component;
54 	struct snd_soc_jack *jack;
55 	int ret;
56 
57 	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
58 					  "%s hs:rt700",
59 					  card->components);
60 	if (!card->components)
61 		return -ENOMEM;
62 
63 	ret = snd_soc_add_card_controls(card, rt700_controls,
64 					ARRAY_SIZE(rt700_controls));
65 	if (ret) {
66 		dev_err(card->dev, "rt700 controls addition failed: %d\n", ret);
67 		return ret;
68 	}
69 
70 	ret = snd_soc_dapm_new_controls(&card->dapm, rt700_widgets,
71 					ARRAY_SIZE(rt700_widgets));
72 	if (ret) {
73 		dev_err(card->dev, "rt700 widgets addition failed: %d\n", ret);
74 		return ret;
75 	}
76 
77 	ret = snd_soc_dapm_add_routes(&card->dapm, rt700_map,
78 				      ARRAY_SIZE(rt700_map));
79 
80 	if (ret) {
81 		dev_err(card->dev, "rt700 map addition failed: %d\n", ret);
82 		return ret;
83 	}
84 
85 	ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
86 				    SND_JACK_HEADSET | SND_JACK_BTN_0 |
87 				    SND_JACK_BTN_1 | SND_JACK_BTN_2 |
88 				    SND_JACK_BTN_3,
89 				    &ctx->sdw_headset,
90 				    rt700_jack_pins,
91 				    ARRAY_SIZE(rt700_jack_pins));
92 	if (ret) {
93 		dev_err(rtd->card->dev, "Headset Jack creation failed: %d\n",
94 			ret);
95 		return ret;
96 	}
97 
98 	jack = &ctx->sdw_headset;
99 
100 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
101 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
102 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
103 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
104 
105 	ret = snd_soc_component_set_jack(component, jack, NULL);
106 	if (ret)
107 		dev_err(rtd->card->dev, "Headset Jack call-back failed: %d\n",
108 			ret);
109 
110 	return ret;
111 }
112 
113 int sof_sdw_rt700_init(struct snd_soc_card *card,
114 		       const struct snd_soc_acpi_link_adr *link,
115 		       struct snd_soc_dai_link *dai_links,
116 		       struct sof_sdw_codec_info *info,
117 		       bool playback)
118 {
119 	/*
120 	 * headset should be initialized once.
121 	 * Do it with dai link for playback.
122 	 */
123 	if (!playback)
124 		return 0;
125 
126 	dai_links->init = rt700_rtd_init;
127 
128 	return 0;
129 }
130