1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020 Intel Corporation
3 
4 /*
5  *  sof_sdw_rt5682 - Helpers to handle RT5682 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 static const struct snd_soc_dapm_widget rt5682_widgets[] = {
19 	SND_SOC_DAPM_HP("Headphone", NULL),
20 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
21 };
22 
23 static const struct snd_soc_dapm_route rt5682_map[] = {
24 	/*Headphones*/
25 	{ "Headphone", NULL, "rt5682 HPOL" },
26 	{ "Headphone", NULL, "rt5682 HPOR" },
27 	{ "rt5682 IN1P", NULL, "Headset Mic" },
28 };
29 
30 static const struct snd_kcontrol_new rt5682_controls[] = {
31 	SOC_DAPM_PIN_SWITCH("Headphone"),
32 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
33 };
34 
35 static struct snd_soc_jack_pin rt5682_jack_pins[] = {
36 	{
37 		.pin    = "Headphone",
38 		.mask   = SND_JACK_HEADPHONE,
39 	},
40 	{
41 		.pin    = "Headset Mic",
42 		.mask   = SND_JACK_MICROPHONE,
43 	},
44 };
45 
46 static int rt5682_rtd_init(struct snd_soc_pcm_runtime *rtd)
47 {
48 	struct snd_soc_card *card = rtd->card;
49 	struct mc_private *ctx = snd_soc_card_get_drvdata(card);
50 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
51 	struct snd_soc_component *component = codec_dai->component;
52 	struct snd_soc_jack *jack;
53 	int ret;
54 
55 	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
56 					  "%s hs:rt5682",
57 					  card->components);
58 	if (!card->components)
59 		return -ENOMEM;
60 
61 	ret = snd_soc_add_card_controls(card, rt5682_controls,
62 					ARRAY_SIZE(rt5682_controls));
63 	if (ret) {
64 		dev_err(card->dev, "rt5682 control addition failed: %d\n", ret);
65 		return ret;
66 	}
67 
68 	ret = snd_soc_dapm_new_controls(&card->dapm, rt5682_widgets,
69 					ARRAY_SIZE(rt5682_widgets));
70 	if (ret) {
71 		dev_err(card->dev, "rt5682 widgets addition failed: %d\n", ret);
72 		return ret;
73 	}
74 
75 	ret = snd_soc_dapm_add_routes(&card->dapm, rt5682_map,
76 				      ARRAY_SIZE(rt5682_map));
77 
78 	if (ret) {
79 		dev_err(card->dev, "rt5682 map addition failed: %d\n", ret);
80 		return ret;
81 	}
82 
83 	ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
84 				    SND_JACK_HEADSET | SND_JACK_BTN_0 |
85 				    SND_JACK_BTN_1 | SND_JACK_BTN_2 |
86 				    SND_JACK_BTN_3,
87 				    &ctx->sdw_headset,
88 				    rt5682_jack_pins,
89 				    ARRAY_SIZE(rt5682_jack_pins));
90 	if (ret) {
91 		dev_err(rtd->card->dev, "Headset Jack creation failed: %d\n",
92 			ret);
93 		return ret;
94 	}
95 
96 	jack = &ctx->sdw_headset;
97 
98 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
99 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
100 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
101 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
102 
103 	ret = snd_soc_component_set_jack(component, jack, NULL);
104 
105 	if (ret)
106 		dev_err(rtd->card->dev, "Headset Jack call-back failed: %d\n",
107 			ret);
108 
109 	return ret;
110 }
111 
112 int sof_sdw_rt5682_init(const struct snd_soc_acpi_link_adr *link,
113 			struct snd_soc_dai_link *dai_links,
114 			struct sof_sdw_codec_info *info,
115 			bool playback)
116 {
117 	/*
118 	 * headset should be initialized once.
119 	 * Do it with dai link for playback.
120 	 */
121 	if (!playback)
122 		return 0;
123 
124 	dai_links->init = rt5682_rtd_init;
125 
126 	return 0;
127 }
128