1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright (c) 2020 Intel Corporation 3 4 /* 5 * sof_sdw_rt711 - Helpers to handle RT711 from generic machine driver 6 */ 7 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" 19 20 /* 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. 23 */ 24 static int rt711_add_codec_device_props(struct device *sdw_dev) 25 { 26 struct property_entry props[MAX_NO_PROPS] = {}; 27 struct fwnode_handle *fwnode; 28 int ret; 29 30 if (!SOF_JACK_JDSRC(sof_sdw_quirk)) 31 return 0; 32 props[0] = PROPERTY_ENTRY_U32("realtek,jd-src", SOF_JACK_JDSRC(sof_sdw_quirk)); 33 34 fwnode = fwnode_create_software_node(props, NULL); 35 if (IS_ERR(fwnode)) 36 return PTR_ERR(fwnode); 37 38 ret = device_add_software_node(sdw_dev, to_software_node(fwnode)); 39 40 fwnode_handle_put(fwnode); 41 42 return ret; 43 } 44 45 static const struct snd_soc_dapm_widget rt711_widgets[] = { 46 SND_SOC_DAPM_HP("Headphone", NULL), 47 SND_SOC_DAPM_MIC("Headset Mic", NULL), 48 }; 49 50 static const struct snd_soc_dapm_route rt711_map[] = { 51 /* Headphones */ 52 { "Headphone", NULL, "rt711 HP" }, 53 { "rt711 MIC2", NULL, "Headset Mic" }, 54 }; 55 56 static const struct snd_kcontrol_new rt711_controls[] = { 57 SOC_DAPM_PIN_SWITCH("Headphone"), 58 SOC_DAPM_PIN_SWITCH("Headset Mic"), 59 }; 60 61 static struct snd_soc_jack_pin rt711_jack_pins[] = { 62 { 63 .pin = "Headphone", 64 .mask = SND_JACK_HEADPHONE, 65 }, 66 { 67 .pin = "Headset Mic", 68 .mask = SND_JACK_MICROPHONE, 69 }, 70 }; 71 72 static int rt711_rtd_init(struct snd_soc_pcm_runtime *rtd) 73 { 74 struct snd_soc_card *card = rtd->card; 75 struct mc_private *ctx = snd_soc_card_get_drvdata(card); 76 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 77 struct snd_soc_component *component = codec_dai->component; 78 struct snd_soc_jack *jack; 79 int ret; 80 81 card->components = devm_kasprintf(card->dev, GFP_KERNEL, 82 "%s hs:rt711", 83 card->components); 84 if (!card->components) 85 return -ENOMEM; 86 87 ret = snd_soc_add_card_controls(card, rt711_controls, 88 ARRAY_SIZE(rt711_controls)); 89 if (ret) { 90 dev_err(card->dev, "rt711 controls addition failed: %d\n", ret); 91 return ret; 92 } 93 94 ret = snd_soc_dapm_new_controls(&card->dapm, rt711_widgets, 95 ARRAY_SIZE(rt711_widgets)); 96 if (ret) { 97 dev_err(card->dev, "rt711 widgets addition failed: %d\n", ret); 98 return ret; 99 } 100 101 ret = snd_soc_dapm_add_routes(&card->dapm, rt711_map, 102 ARRAY_SIZE(rt711_map)); 103 104 if (ret) { 105 dev_err(card->dev, "rt711 map addition failed: %d\n", ret); 106 return ret; 107 } 108 109 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack", 110 SND_JACK_HEADSET | SND_JACK_BTN_0 | 111 SND_JACK_BTN_1 | SND_JACK_BTN_2 | 112 SND_JACK_BTN_3, 113 &ctx->sdw_headset, 114 rt711_jack_pins, 115 ARRAY_SIZE(rt711_jack_pins)); 116 if (ret) { 117 dev_err(rtd->card->dev, "Headset Jack creation failed: %d\n", 118 ret); 119 return ret; 120 } 121 122 jack = &ctx->sdw_headset; 123 124 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); 125 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND); 126 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP); 127 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN); 128 129 ret = snd_soc_component_set_jack(component, jack, NULL); 130 131 if (ret) 132 dev_err(rtd->card->dev, "Headset Jack call-back failed: %d\n", 133 ret); 134 135 return ret; 136 } 137 138 int sof_sdw_rt711_exit(struct snd_soc_card *card, struct snd_soc_dai_link *dai_link) 139 { 140 struct mc_private *ctx = snd_soc_card_get_drvdata(card); 141 142 if (!ctx->headset_codec_dev) 143 return 0; 144 145 device_remove_software_node(ctx->headset_codec_dev); 146 put_device(ctx->headset_codec_dev); 147 148 return 0; 149 } 150 151 int sof_sdw_rt711_init(struct snd_soc_card *card, 152 const struct snd_soc_acpi_link_adr *link, 153 struct snd_soc_dai_link *dai_links, 154 struct sof_sdw_codec_info *info, 155 bool playback) 156 { 157 struct mc_private *ctx = snd_soc_card_get_drvdata(card); 158 struct device *sdw_dev; 159 int ret; 160 161 /* 162 * headset should be initialized once. 163 * Do it with dai link for playback. 164 */ 165 if (!playback) 166 return 0; 167 168 sdw_dev = bus_find_device_by_name(&sdw_bus_type, NULL, dai_links->codecs[0].name); 169 if (!sdw_dev) 170 return -EPROBE_DEFER; 171 172 ret = rt711_add_codec_device_props(sdw_dev); 173 if (ret < 0) { 174 put_device(sdw_dev); 175 return ret; 176 } 177 ctx->headset_codec_dev = sdw_dev; 178 179 dai_links->init = rt711_rtd_init; 180 181 return 0; 182 } 183