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