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(const char *sdw_dev_name)
25 {
26 	struct property_entry props[MAX_NO_PROPS] = {};
27 	struct device *sdw_dev;
28 	int ret;
29 
30 	sdw_dev = bus_find_device_by_name(&sdw_bus_type, NULL, sdw_dev_name);
31 	if (!sdw_dev)
32 		return -EPROBE_DEFER;
33 
34 	if (SOF_RT711_JDSRC(sof_sdw_quirk)) {
35 		props[0] = PROPERTY_ENTRY_U32("realtek,jd-src",
36 					      SOF_RT711_JDSRC(sof_sdw_quirk));
37 	}
38 
39 	ret = device_add_properties(sdw_dev, props);
40 	put_device(sdw_dev);
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(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 device *dev, struct snd_soc_dai_link *dai_link)
139 {
140 	struct device *sdw_dev;
141 
142 	sdw_dev = bus_find_device_by_name(&sdw_bus_type, NULL,
143 					  dai_link->codecs[0].name);
144 	if (!sdw_dev)
145 		return -EINVAL;
146 
147 	device_remove_properties(sdw_dev);
148 	put_device(sdw_dev);
149 
150 	return 0;
151 }
152 
153 int sof_sdw_rt711_init(const struct snd_soc_acpi_link_adr *link,
154 		       struct snd_soc_dai_link *dai_links,
155 		       struct sof_sdw_codec_info *info,
156 		       bool playback)
157 {
158 	int ret;
159 
160 	/*
161 	 * headset should be initialized once.
162 	 * Do it with dai link for playback.
163 	 */
164 	if (!playback)
165 		return 0;
166 
167 	ret = rt711_add_codec_device_props(dai_links->codecs[0].name);
168 	if (ret < 0)
169 		return ret;
170 
171 	dai_links->init = rt711_rtd_init;
172 
173 	return 0;
174 }
175