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  */
rt711_add_codec_device_props(struct device * sdw_dev)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_RT711_JDSRC(sof_sdw_quirk))
31 		return 0;
32 	props[0] = PROPERTY_ENTRY_U32("realtek,jd-src", SOF_RT711_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 
rt711_rtd_init(struct snd_soc_pcm_runtime * rtd)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 
sof_sdw_rt711_exit(struct snd_soc_card * card,struct snd_soc_dai_link * dai_link)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 	device_remove_software_node(ctx->headset_codec_dev);
143 	put_device(ctx->headset_codec_dev);
144 
145 	return 0;
146 }
147 
sof_sdw_rt711_init(struct snd_soc_card * card,const struct snd_soc_acpi_link_adr * link,struct snd_soc_dai_link * dai_links,struct sof_sdw_codec_info * info,bool playback)148 int sof_sdw_rt711_init(struct snd_soc_card *card,
149 		       const struct snd_soc_acpi_link_adr *link,
150 		       struct snd_soc_dai_link *dai_links,
151 		       struct sof_sdw_codec_info *info,
152 		       bool playback)
153 {
154 	struct mc_private *ctx = snd_soc_card_get_drvdata(card);
155 	struct device *sdw_dev;
156 	int ret;
157 
158 	/*
159 	 * headset should be initialized once.
160 	 * Do it with dai link for playback.
161 	 */
162 	if (!playback)
163 		return 0;
164 
165 	sdw_dev = bus_find_device_by_name(&sdw_bus_type, NULL, dai_links->codecs[0].name);
166 	if (!sdw_dev)
167 		return -EPROBE_DEFER;
168 
169 	ret = rt711_add_codec_device_props(sdw_dev);
170 	if (ret < 0) {
171 		put_device(sdw_dev);
172 		return ret;
173 	}
174 	ctx->headset_codec_dev = sdw_dev;
175 
176 	dai_links->init = rt711_rtd_init;
177 
178 	return 0;
179 }
180