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/soc.h> 14 #include <sound/soc-acpi.h> 15 #include <sound/jack.h> 16 #include "sof_sdw_common.h" 17 18 /* 19 * Note this MUST be called before snd_soc_register_card(), so that the props 20 * are in place before the codec component driver's probe function parses them. 21 */ 22 static int rt711_add_codec_device_props(const char *sdw_dev_name) 23 { 24 struct property_entry props[MAX_NO_PROPS] = {}; 25 struct device *sdw_dev; 26 int ret; 27 28 sdw_dev = bus_find_device_by_name(&sdw_bus_type, NULL, sdw_dev_name); 29 if (!sdw_dev) 30 return -EPROBE_DEFER; 31 32 if (SOF_RT711_JDSRC(sof_sdw_quirk)) { 33 props[0] = PROPERTY_ENTRY_U32("realtek,jd-src", 34 SOF_RT711_JDSRC(sof_sdw_quirk)); 35 } 36 37 ret = device_add_properties(sdw_dev, props); 38 put_device(sdw_dev); 39 40 return ret; 41 } 42 43 static const struct snd_soc_dapm_widget rt711_widgets[] = { 44 SND_SOC_DAPM_HP("Headphone", NULL), 45 SND_SOC_DAPM_MIC("Headset Mic", NULL), 46 }; 47 48 static const struct snd_soc_dapm_route rt711_map[] = { 49 /* Headphones */ 50 { "Headphone", NULL, "rt711 HP" }, 51 { "rt711 MIC2", NULL, "Headset Mic" }, 52 }; 53 54 static const struct snd_kcontrol_new rt711_controls[] = { 55 SOC_DAPM_PIN_SWITCH("Headphone"), 56 SOC_DAPM_PIN_SWITCH("Headset Mic"), 57 }; 58 59 static struct snd_soc_jack_pin rt711_jack_pins[] = { 60 { 61 .pin = "Headphone", 62 .mask = SND_JACK_HEADPHONE, 63 }, 64 { 65 .pin = "Headset Mic", 66 .mask = SND_JACK_MICROPHONE, 67 }, 68 }; 69 70 static int rt711_rtd_init(struct snd_soc_pcm_runtime *rtd) 71 { 72 struct snd_soc_card *card = rtd->card; 73 struct mc_private *ctx = snd_soc_card_get_drvdata(card); 74 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 75 struct snd_soc_component *component = codec_dai->component; 76 struct snd_soc_jack *jack; 77 int ret; 78 79 card->components = devm_kasprintf(card->dev, GFP_KERNEL, 80 "%s hs:rt711", 81 card->components); 82 if (!card->components) 83 return -ENOMEM; 84 85 ret = snd_soc_add_card_controls(card, rt711_controls, 86 ARRAY_SIZE(rt711_controls)); 87 if (ret) { 88 dev_err(card->dev, "rt711 controls addition failed: %d\n", ret); 89 return ret; 90 } 91 92 ret = snd_soc_dapm_new_controls(&card->dapm, rt711_widgets, 93 ARRAY_SIZE(rt711_widgets)); 94 if (ret) { 95 dev_err(card->dev, "rt711 widgets addition failed: %d\n", ret); 96 return ret; 97 } 98 99 ret = snd_soc_dapm_add_routes(&card->dapm, rt711_map, 100 ARRAY_SIZE(rt711_map)); 101 102 if (ret) { 103 dev_err(card->dev, "rt711 map addition failed: %d\n", ret); 104 return ret; 105 } 106 107 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack", 108 SND_JACK_HEADSET | SND_JACK_BTN_0 | 109 SND_JACK_BTN_1 | SND_JACK_BTN_2 | 110 SND_JACK_BTN_3, 111 &ctx->sdw_headset, 112 rt711_jack_pins, 113 ARRAY_SIZE(rt711_jack_pins)); 114 if (ret) { 115 dev_err(rtd->card->dev, "Headset Jack creation failed: %d\n", 116 ret); 117 return ret; 118 } 119 120 jack = &ctx->sdw_headset; 121 122 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); 123 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND); 124 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP); 125 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN); 126 127 ret = snd_soc_component_set_jack(component, jack, NULL); 128 129 if (ret) 130 dev_err(rtd->card->dev, "Headset Jack call-back failed: %d\n", 131 ret); 132 133 return ret; 134 } 135 136 int sof_sdw_rt711_init(const struct snd_soc_acpi_link_adr *link, 137 struct snd_soc_dai_link *dai_links, 138 struct sof_sdw_codec_info *info, 139 bool playback) 140 { 141 int ret; 142 143 /* 144 * headset should be initialized once. 145 * Do it with dai link for playback. 146 */ 147 if (!playback) 148 return 0; 149 150 ret = rt711_add_codec_device_props("sdw:0:25d:711:0"); 151 if (ret < 0) 152 return ret; 153 154 dai_links->init = rt711_rtd_init; 155 156 return 0; 157 } 158