1 // SPDX-License-Identifier: GPL-2.0 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_component *component = rtd->codec_dai->component; 75 struct snd_soc_jack *jack; 76 int ret; 77 78 card->components = devm_kasprintf(card->dev, GFP_KERNEL, 79 "%s hs:rt711", 80 card->components); 81 if (!card->components) 82 return -ENOMEM; 83 84 ret = snd_soc_add_card_controls(card, rt711_controls, 85 ARRAY_SIZE(rt711_controls)); 86 if (ret) { 87 dev_err(card->dev, "rt711 controls addition failed: %d\n", ret); 88 return ret; 89 } 90 91 ret = snd_soc_dapm_new_controls(&card->dapm, rt711_widgets, 92 ARRAY_SIZE(rt711_widgets)); 93 if (ret) { 94 dev_err(card->dev, "rt711 widgets addition failed: %d\n", ret); 95 return ret; 96 } 97 98 ret = snd_soc_dapm_add_routes(&card->dapm, rt711_map, 99 ARRAY_SIZE(rt711_map)); 100 101 if (ret) { 102 dev_err(card->dev, "rt711 map addition failed: %d\n", ret); 103 return ret; 104 } 105 106 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack", 107 SND_JACK_HEADSET | SND_JACK_BTN_0 | 108 SND_JACK_BTN_1 | SND_JACK_BTN_2 | 109 SND_JACK_BTN_3, 110 &ctx->sdw_headset, 111 rt711_jack_pins, 112 ARRAY_SIZE(rt711_jack_pins)); 113 if (ret) { 114 dev_err(rtd->card->dev, "Headset Jack creation failed: %d\n", 115 ret); 116 return ret; 117 } 118 119 jack = &ctx->sdw_headset; 120 121 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_VOLUMEUP); 122 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_PLAYPAUSE); 123 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN); 124 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND); 125 126 ret = snd_soc_component_set_jack(component, jack, NULL); 127 128 if (ret) 129 dev_err(rtd->card->dev, "Headset Jack call-back failed: %d\n", 130 ret); 131 132 return ret; 133 } 134 135 int sof_sdw_rt711_init(const struct snd_soc_acpi_link_adr *link, 136 struct snd_soc_dai_link *dai_links, 137 struct sof_sdw_codec_info *info, 138 bool playback) 139 { 140 int ret; 141 142 /* 143 * headset should be initialized once. 144 * Do it with dai link for playback. 145 */ 146 if (!playback) 147 return 0; 148 149 ret = rt711_add_codec_device_props("sdw:0:25d:711:0"); 150 if (ret < 0) 151 return ret; 152 153 dai_links->init = rt711_rtd_init; 154 155 return 0; 156 } 157