1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020 Intel Corporation
5 * sof_sdw_rt711_sdca - Helpers to handle RT711-SDCA from generic machine driver
8 #include <linux/device.h>
9 #include <linux/errno.h>
10 #include <linux/input.h>
11 #include <linux/soundwire/sdw.h>
12 #include <linux/soundwire/sdw_type.h>
13 #include <sound/control.h>
14 #include <sound/soc.h>
15 #include <sound/soc-acpi.h>
16 #include <sound/soc-dapm.h>
17 #include <sound/jack.h>
18 #include "sof_sdw_common.h"
21 * Note this MUST be called before snd_soc_register_card(), so that the props
22 * are in place before the codec component driver's probe function parses them.
24 static int rt_sdca_jack_add_codec_device_props(struct device *sdw_dev)
26 struct property_entry props[MAX_NO_PROPS] = {};
27 struct fwnode_handle *fwnode;
30 if (!SOF_JACK_JDSRC(sof_sdw_quirk))
33 props[0] = PROPERTY_ENTRY_U32("realtek,jd-src", SOF_JACK_JDSRC(sof_sdw_quirk));
35 fwnode = fwnode_create_software_node(props, NULL);
37 return PTR_ERR(fwnode);
39 ret = device_add_software_node(sdw_dev, to_software_node(fwnode));
41 fwnode_handle_put(fwnode);
46 static const struct snd_soc_dapm_widget rt_sdca_jack_widgets[] = {
47 SND_SOC_DAPM_HP("Headphone", NULL),
48 SND_SOC_DAPM_MIC("Headset Mic", NULL),
51 static const struct snd_soc_dapm_route rt711_sdca_map[] = {
52 { "Headphone", NULL, "rt711 HP" },
53 { "rt711 MIC2", NULL, "Headset Mic" },
56 static const struct snd_soc_dapm_route rt712_sdca_map[] = {
57 { "Headphone", NULL, "rt712 HP" },
58 { "rt712 MIC2", NULL, "Headset Mic" },
61 static const struct snd_soc_dapm_route rt713_sdca_map[] = {
62 { "Headphone", NULL, "rt713 HP" },
63 { "rt713 MIC2", NULL, "Headset Mic" },
66 static const struct snd_kcontrol_new rt_sdca_jack_controls[] = {
67 SOC_DAPM_PIN_SWITCH("Headphone"),
68 SOC_DAPM_PIN_SWITCH("Headset Mic"),
71 static struct snd_soc_jack_pin rt_sdca_jack_pins[] = {
74 .mask = SND_JACK_HEADPHONE,
78 .mask = SND_JACK_MICROPHONE,
82 static int rt_sdca_jack_rtd_init(struct snd_soc_pcm_runtime *rtd)
84 struct snd_soc_card *card = rtd->card;
85 struct mc_private *ctx = snd_soc_card_get_drvdata(card);
86 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
87 struct snd_soc_component *component = codec_dai->component;
88 struct snd_soc_jack *jack;
91 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
93 card->components, component->name_prefix);
94 if (!card->components)
97 ret = snd_soc_add_card_controls(card, rt_sdca_jack_controls,
98 ARRAY_SIZE(rt_sdca_jack_controls));
100 dev_err(card->dev, "rt sdca jack controls addition failed: %d\n", ret);
104 ret = snd_soc_dapm_new_controls(&card->dapm, rt_sdca_jack_widgets,
105 ARRAY_SIZE(rt_sdca_jack_widgets));
107 dev_err(card->dev, "rt sdca jack widgets addition failed: %d\n", ret);
111 if (strstr(component->name_prefix, "rt711")) {
112 ret = snd_soc_dapm_add_routes(&card->dapm, rt711_sdca_map,
113 ARRAY_SIZE(rt711_sdca_map));
114 } else if (strstr(component->name_prefix, "rt712")) {
115 ret = snd_soc_dapm_add_routes(&card->dapm, rt712_sdca_map,
116 ARRAY_SIZE(rt712_sdca_map));
117 } else if (strstr(component->name_prefix, "rt713")) {
118 ret = snd_soc_dapm_add_routes(&card->dapm, rt713_sdca_map,
119 ARRAY_SIZE(rt713_sdca_map));
121 dev_err(card->dev, "%s is not supported\n", component->name_prefix);
126 dev_err(card->dev, "rt sdca jack map addition failed: %d\n", ret);
130 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
131 SND_JACK_HEADSET | SND_JACK_BTN_0 |
132 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
136 ARRAY_SIZE(rt_sdca_jack_pins));
138 dev_err(rtd->card->dev, "Headset Jack creation failed: %d\n",
143 jack = &ctx->sdw_headset;
145 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
146 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
147 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
148 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
150 ret = snd_soc_component_set_jack(component, jack, NULL);
153 dev_err(rtd->card->dev, "Headset Jack call-back failed: %d\n",
159 int sof_sdw_rt_sdca_jack_exit(struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
161 struct mc_private *ctx = snd_soc_card_get_drvdata(card);
163 if (!ctx->headset_codec_dev)
166 if (!SOF_JACK_JDSRC(sof_sdw_quirk))
169 device_remove_software_node(ctx->headset_codec_dev);
170 put_device(ctx->headset_codec_dev);
175 int sof_sdw_rt_sdca_jack_init(struct snd_soc_card *card,
176 const struct snd_soc_acpi_link_adr *link,
177 struct snd_soc_dai_link *dai_links,
178 struct sof_sdw_codec_info *info,
181 struct mc_private *ctx = snd_soc_card_get_drvdata(card);
182 struct device *sdw_dev;
186 * headset should be initialized once.
187 * Do it with dai link for playback.
192 sdw_dev = bus_find_device_by_name(&sdw_bus_type, NULL, dai_links->codecs[0].name);
194 return -EPROBE_DEFER;
196 ret = rt_sdca_jack_add_codec_device_props(sdw_dev);
201 ctx->headset_codec_dev = sdw_dev;
203 dai_links->init = rt_sdca_jack_rtd_init;