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